PageRenderTime 486ms CodeModel.GetById 241ms app.highlight 115ms RepoModel.GetById 127ms app.codeStats 0ms

/misc/pascal/insn32/popt/pjopt.c

https://github.com/markangelo/PX4NuttX
C | 449 lines | 341 code | 67 blank | 41 comment | 16 complexity | ae0c81aba993769f76c0cb204b60006b MD5 | raw file
  1/**********************************************************************
  2 * pjopt.c
  3 * Branch Optimizations
  4 *
  5 *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
  6 *   Author: Gregory Nutt <gnutt@nuttx.org>
  7 *
  8 * Redistribution and use in source and binary forms, with or without
  9 * modification, are permitted provided that the following conditions
 10 * are met:
 11 *
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions and the following disclaimer.
 14 * 2. Redistributions in binary form must reproduce the above copyright
 15 *    notice, this list of conditions and the following disclaimer in
 16 *    the documentation and/or other materials provided with the
 17 *    distribution.
 18 * 3. Neither the name NuttX nor the names of its contributors may be
 19 *    used to endorse or promote products derived from this software
 20 *    without specific prior written permission.
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 33 * POSSIBILITY OF SUCH DAMAGE.
 34 *
 35 **********************************************************************/
 36
 37/**********************************************************************
 38 * Included Files
 39 **********************************************************************/
 40
 41#include <stdio.h>
 42
 43#include "keywords.h"
 44#include "pdefs.h"
 45#include "pinsn32.h"
 46
 47#include "popt.h"
 48#include "polocal.h"
 49#include "pjopt.h"
 50
 51/**********************************************************************/
 52
 53int BranchOptimize (void)
 54{
 55  int nchanges = 0;
 56  register int i;
 57
 58  TRACE(stderr, "[BranchOptimize]");
 59
 60  /* At least two pcodes are need to perform branch optimizations */
 61
 62  i = 0;
 63  while (i < nops-1)
 64    {
 65      switch (pptr[i]->op)
 66	{
 67	case oNOT :
 68	  switch (pptr[i+1]->op)
 69	    {
 70	    case oJEQUZ :
 71	      pptr[i+1]->op = oJNEQZ;
 72	      deletePcode(i);
 73	      nchanges++;
 74	      break;
 75
 76	    case oJNEQZ :
 77	      pptr[i+1]->op = oJEQUZ;
 78	      deletePcode(i);
 79	      nchanges++;
 80	      break;
 81
 82	    default     :
 83	      i++;
 84	      break;
 85	    } /* end switch */
 86	  break;
 87
 88	case oNEG :
 89	  switch (pptr[i+1]->op)
 90	    {
 91	    case oJLTZ  :
 92	      pptr[i+1]->op = oJGTZ;
 93	      deletePcode(i);
 94	      nchanges++;
 95	      break;
 96
 97	    case oJGTEZ :
 98	      pptr[i+1]->op = oJLTEZ;
 99	      deletePcode(i);
100	      nchanges++;
101	      break;
102
103	    case oJGTZ  :
104	      pptr[i+1]->op = oJLTZ;
105	      deletePcode(i);
106	      nchanges++;
107	      break;
108
109	    case oJLTEZ :
110	      pptr[i+1]->op = oJGTEZ;
111	      deletePcode(i);
112	      nchanges++;
113	      break;
114
115	    default     :
116	      i++;
117	      break;
118	    } /* end switch */
119	  break;
120
121	case oEQU  :
122	  switch (pptr[i+1]->op)
123	    {
124	    case oNOT :
125	      pptr[i]->op = oNEQ;
126	      deletePcode(i+1);
127	      nchanges++;
128	      break;
129
130	    case oJEQUZ :
131	      pptr[i+1]->op = oJNEQ;
132	      deletePcode(i);
133	      nchanges++;
134	      break;
135
136	    case oJNEQZ :
137	      pptr[i+1]->op = oJEQU;
138	      deletePcode(i);
139	      nchanges++;
140	      break;
141
142	    default     :
143	      i++;
144	      break;
145	    } /* end switch */
146	  break;
147
148	case oNEQ  :
149	  switch (pptr[i+1]->op)
150	    {
151	    case oNOT :
152	      pptr[i]->op = oEQU;
153	      deletePcode(i+1);
154	      nchanges++;
155	      break;
156
157	    case oJEQUZ :
158	      pptr[i+1]->op = oJEQU;
159	      deletePcode(i);
160	      nchanges++;
161	      break;
162
163	    case oJNEQZ :
164	      pptr[i+1]->op = oJNEQ;
165	      deletePcode(i);
166	      nchanges++;
167	      break;
168
169	    default     :
170	      i++;
171	      break;
172	    } /* end switch */
173	  break;
174
175	case oLT   :
176	  switch (pptr[i+1]->op)
177	    {
178	    case oNOT :
179	      pptr[i]->op = oGTE;
180	      deletePcode(i+1);
181	      nchanges++;
182	      break;
183
184	    case oJEQUZ :
185	      pptr[i+1]->op = oJGTE;
186	      deletePcode(i);
187	      nchanges++;
188	      break;
189
190	    case oJNEQZ :
191	      pptr[i+1]->op = oJLT;
192	      deletePcode(i);
193	      nchanges++;
194	      break;
195
196	    default     :
197	      i++;
198	      break;
199	    } /* end switch */
200	  break;
201
202	case oGTE  :
203	  switch (pptr[i+1]->op)
204	    {
205	    case oNOT :
206	      pptr[i]->op = oLT;
207	      deletePcode(i+1);
208	      nchanges++;
209	      break;
210
211	    case oJEQUZ :
212	      pptr[i+1]->op = oJLT;
213	      deletePcode(i);
214	      nchanges++;
215	      break;
216
217	    case oJNEQZ :
218	      pptr[i+1]->op = oJGTE;
219	      deletePcode(i);
220	      nchanges++;
221	      break;
222
223	    default     :
224	      i++;
225	      break;
226	    } /* end switch */
227	  break;
228
229	case oGT   :
230	  switch (pptr[i+1]->op)
231	    {
232	    case oNOT :
233	      pptr[i]->op = oLTE;
234	      deletePcode(i+1);
235	      nchanges++;
236	      break;
237
238	    case oJEQUZ :
239	      pptr[i+1]->op = oJLTE;
240	      deletePcode(i);
241	      nchanges++;
242	      break;
243
244	    case oJNEQZ :
245	      pptr[i+1]->op = oJGT;
246	      deletePcode(i);
247	      nchanges++;
248	      break;
249
250	    default     :
251	      i++;
252	      break;
253	    } /* end switch */
254	  break;
255
256	case oLTE :
257	  switch (pptr[i+1]->op)
258	    {
259	    case oNOT :
260	      pptr[i]->op = oGT;
261	      deletePcode(i+1);
262	      nchanges++;
263	      break;
264
265	    case oJEQUZ :
266	      pptr[i+1]->op = oJGT;
267	      deletePcode(i);
268	      nchanges++;
269	      break;
270
271	    case oJNEQZ :
272	      pptr[i+1]->op = oJLTE;
273	      deletePcode(i);
274	      nchanges++;
275	      break;
276
277	    default     :
278	      i++;
279	      break;
280	    } /* end switch */
281	  break;
282
283	case oEQUZ  :
284	  switch (pptr[i+1]->op)
285	    {
286	    case oNOT :
287	      pptr[i]->op = oNEQZ;
288	      deletePcode(i+1);
289	      nchanges++;
290	      break;
291
292	    case oJEQUZ :
293	      pptr[i+1]->op = oJNEQZ;
294	      deletePcode(i);
295	      nchanges++;
296	      break;
297
298	    case oJNEQZ :
299	      pptr[i+1]->op = oJEQUZ;
300	      deletePcode(i);
301	      nchanges++;
302	      break;
303
304	    default     :
305	      i++;
306	      break;
307	    } /* end switch */
308	  break;
309
310	case oNEQZ  :
311	  switch (pptr[i+1]->op)
312	    {
313	    case oNOT :
314	      pptr[i]->op = oEQUZ;
315	      deletePcode(i+1);
316	      nchanges++;
317	      break;
318
319	    case oJEQUZ :
320	    case oJNEQZ :
321	      deletePcode(i);
322	      nchanges++;
323	      break;
324
325	    default     :
326	      i++;
327	      break;
328	    } /* end switch */
329	  break;
330
331	case oLTZ   :
332	  switch (pptr[i+1]->op)
333	    {
334	    case oNOT :
335	      pptr[i]->op = oGTEZ;
336	      deletePcode(i+1);
337	      nchanges++;
338	      break;
339
340	    case oJEQUZ :
341	      pptr[i+1]->op = oJGTEZ;
342	      deletePcode(i);
343	      nchanges++;
344	      break;
345
346	    case oJNEQZ :
347	      pptr[i+1]->op = oJLTZ;
348	      deletePcode(i);
349	      nchanges++;
350	      break;
351
352	    default     :
353	      i++;
354	      break;
355	    } /* end switch */
356	  break;
357
358	case oGTEZ  :
359	  switch (pptr[i+1]->op)
360	    {
361	    case oNOT :
362	      pptr[i]->op = oLTZ;
363	      deletePcode(i+1);
364	      nchanges++;
365	      break;
366
367	    case oJEQUZ :
368	      pptr[i+1]->op = oJLTZ;
369	      deletePcode(i);
370	      nchanges++;
371	      break;
372
373	    case oJNEQZ :
374	      pptr[i+1]->op = oJGTEZ;
375	      deletePcode(i);
376	      nchanges++;
377	      break;
378
379	    default     :
380	      i++;
381	      break;
382	    } /* end switch */
383	  break;
384
385	case oGTZ   :
386	  switch (pptr[i+1]->op)
387	    {
388	    case oNOT :
389	      pptr[i]->op = oLTEZ;
390	      deletePcode(i+1);
391	      nchanges++;
392	      break;
393
394	    case oJEQUZ :
395	      pptr[i+1]->op = oJLTEZ;
396	      deletePcode(i);
397	      nchanges++;
398	      break;
399
400	    case oJNEQZ :
401	      pptr[i+1]->op = oJGTZ;
402	      deletePcode(i);
403	      nchanges++;
404	      break;
405
406	    default     :
407	      i++;
408	      break;
409	    } /* end switch */
410	  break;
411
412	case oLTEZ :
413	  switch (pptr[i+1]->op)
414	    {
415	    case oNOT :
416	      pptr[i]->op = oGTZ;
417	      deletePcode(i+1);
418	      nchanges++;
419	      break;
420
421	    case oJEQUZ :
422	      pptr[i+1]->op = oJGTZ;
423	      deletePcode(i);
424	      nchanges++;
425	      break;
426
427	    case oJNEQZ :
428	      pptr[i+1]->op = oJLTEZ;
429	      deletePcode(i);
430	      nchanges++;
431	      break;
432
433	    default     :
434	      i++;
435	      break;
436	    } /* end switch */
437	  break;
438
439	default     :
440	  i++;
441	  break;
442	} /* end switch */
443    } /* end while */
444  return (nchanges);
445
446} /* end BranchOptimize */
447
448/**********************************************************************/
449