5e09ba08dab41ec7683cfcf04884bf9b43b04c83
[fw/sdcc] / src / SDCCcse.c
1 /*-------------------------------------------------------------------------
2   SDCCcse.c - source file for Common Subexpressions and other utility
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26 #include "newalloc.h"
27
28 /*-----------------------------------------------------------------*/
29 /* newCseDef - new cseDef                                          */
30 /*-----------------------------------------------------------------*/
31 cseDef *
32 newCseDef (operand * sym, iCode * ic)
33 {
34   cseDef *cdp;
35
36   assert (sym);
37   cdp = Safe_calloc (1, sizeof (cseDef));
38
39   cdp->sym = sym;
40   cdp->diCode = ic;
41   cdp->key = sym->key;
42
43   return cdp;
44 }
45
46
47
48 /*-----------------------------------------------------------------*/
49 /* int isCseDefEqual - two definitions are equal                   */
50 /*-----------------------------------------------------------------*/
51 int 
52 isCseDefEqual (void *vsrc, void *vdest)
53 {
54   cseDef *src = vsrc;
55   cseDef *dest = vdest;
56
57   if (src == dest)
58     return 1;
59
60   return (src->key == dest->key &&
61           src->diCode == dest->diCode);
62
63 }
64
65 /*-----------------------------------------------------------------*/
66 /* pcseDef - in the cseDef                                         */
67 /*-----------------------------------------------------------------*/
68 int 
69 pcseDef (void *item, va_list ap)
70 {
71   cseDef *cdp = item;
72   iCodeTable *icTab;
73
74   (void) ap;
75
76   if (!cdp->sym)
77     fprintf (stdout, "**null op**");
78   printOperand (cdp->sym, stdout);
79   icTab = getTableEntry (cdp->diCode->op);
80   icTab->iCodePrint (stdout, cdp->diCode, icTab->printName);
81   return 1;
82 }
83
84 /*-----------------------------------------------------------------*/
85 /* replaceAllSymBySym - replaces all operands by operand in an     */
86 /*                      instruction chain                          */
87 /*-----------------------------------------------------------------*/
88 void 
89 replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
90 {
91   iCode *lic;
92
93   for (lic = ic; lic; lic = lic->next)
94     {
95       int siaddr;
96
97       /* do the special cases first */
98       if (lic->op == IFX)
99         {
100           if (IS_SYMOP (to) &&
101               IC_COND (lic)->key == from->key)
102             {
103
104               bitVectUnSetBit (OP_USES (from), lic->key);
105               OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
106               siaddr = IC_COND (lic)->isaddr;
107               IC_COND (lic) = operandFromOperand (to);
108               IC_COND (lic)->isaddr = siaddr;
109
110             }
111           continue;
112         }
113
114       if (lic->op == JUMPTABLE)
115         {
116           if (IS_SYMOP (to) &&
117               IC_JTCOND (lic)->key == from->key)
118             {
119
120               bitVectUnSetBit (OP_USES (from), lic->key);
121               OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
122               siaddr = IC_COND (lic)->isaddr;
123               IC_JTCOND (lic) = operandFromOperand (to);
124               IC_JTCOND (lic)->isaddr = siaddr;
125
126             }
127           continue;
128         }
129
130       if (IC_RESULT (lic) && IC_RESULT (lic)->key == from->key)
131         {
132           /* maintain du chains */
133           if (POINTER_SET (lic))
134             {
135               bitVectUnSetBit (OP_USES (from), lic->key);
136               OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
137
138               /* also check if the "from" was in the non-dominating
139                  pointer sets and replace it with "to" in the bitVector */
140               if (bitVectBitValue (*ndpset, from->key))
141                 {
142                   bitVectUnSetBit (*ndpset, from->key);
143                   bitVectSetBit (*ndpset, to->key);
144                 }
145
146             }
147           else
148             {
149               bitVectUnSetBit (OP_DEFS (from), lic->key);
150               OP_DEFS (to) = bitVectSetBit (OP_DEFS (to), lic->key);
151             }
152           siaddr = IC_RESULT (lic)->isaddr;
153           IC_RESULT (lic) = operandFromOperand (to);
154           IC_RESULT (lic)->isaddr = siaddr;
155         }
156
157       if (IS_SYMOP (to) &&
158           IC_RIGHT (lic) && IC_RIGHT (lic)->key == from->key)
159         {
160           bitVectUnSetBit (OP_USES (from), lic->key);
161           OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
162           siaddr = IC_RIGHT (lic)->isaddr;
163           IC_RIGHT (lic) = operandFromOperand (to);
164           IC_RIGHT (lic)->isaddr = siaddr;
165         }
166
167       if (IS_SYMOP (to) &&
168           IC_LEFT (lic) && IC_LEFT (lic)->key == from->key)
169         {
170           bitVectUnSetBit (OP_USES (from), lic->key);
171           OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
172           siaddr = IC_LEFT (lic)->isaddr;
173           IC_LEFT (lic) = operandFromOperand (to);
174           IC_LEFT (lic)->isaddr = siaddr;
175         }
176     }
177 }
178
179 /*-----------------------------------------------------------------*/
180 /* iCodeKeyIs - if the icode keys match then return 1              */
181 /*-----------------------------------------------------------------*/
182 DEFSETFUNC (iCodeKeyIs)
183 {
184   cseDef *cdp = item;
185   V_ARG (int, key);
186
187   if (cdp->diCode->key == key)
188     return 1;
189   else
190     return 0;
191 }
192
193 /*-----------------------------------------------------------------*/
194 /* removeFromInExprs - removes an icode from inexpressions         */
195 /*-----------------------------------------------------------------*/
196 DEFSETFUNC (removeFromInExprs)
197 {
198   eBBlock *ebp = item;
199   V_ARG (iCode *, ic);
200   V_ARG (operand *, from);
201   V_ARG (operand *, to);
202   V_ARG (eBBlock *, cbp);
203
204   if (ebp->visited)
205     return 0;
206
207   ebp->visited = 1;
208   deleteItemIf (&ebp->inExprs, iCodeKeyIs, ic->key);
209   if (ebp != cbp && !bitVectBitValue (cbp->domVect, ebp->bbnum))
210     replaceAllSymBySym (ebp->sch, from, to, &ebp->ndompset);
211
212   applyToSet (ebp->succList, removeFromInExprs, ic, from, to, cbp);
213   return 0;
214 }
215
216 /*-----------------------------------------------------------------*/
217 /* isGlobalInNearSpace - return TRUE if valriable is a globalin data */
218 /*-----------------------------------------------------------------*/
219 static bool 
220 isGlobalInNearSpace (operand * op)
221 {
222   sym_link *type = getSpec (operandType (op));
223   /* this is 8051 specific: optimization
224      suggested by Jean-Louis VERN, with 8051s we have no
225      advantage of putting variables in near space into
226      registers */
227   if (isOperandGlobal (op) && !IN_FARSPACE (SPEC_OCLS (type)) &&
228       IN_DIRSPACE (SPEC_OCLS (type)))
229     return TRUE;
230   else
231     return FALSE;
232 }
233
234 /*-----------------------------------------------------------------*/
235 /* findCheaperOp - cseBBlock support routine, will check to see if */
236 /*              we have a operand previously defined               */
237 /*-----------------------------------------------------------------*/
238 DEFSETFUNC (findCheaperOp)
239 {
240   cseDef *cdp = item;
241   V_ARG (operand *, cop);
242   V_ARG (operand **, opp);
243
244   /* if we have already found it */
245   if (*opp)
246     return 1;
247
248   /* not found it yet check if this is the one */
249   /* and this is not the defining one          */
250   if (cop->key == cdp->key)
251     {
252
253       /* do a special check this will help in */
254       /* constant propagation & dead code elim */
255       /* for assignments only                 */
256       if (cdp->diCode->op == '=')
257         {
258           /* if the result is volatile then return result */
259           if (IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
260             *opp = IC_RESULT (cdp->diCode);
261           else
262             /* if this is a straight assignment and
263                left is a temp then prefer the temporary to the
264                true symbol */
265             if (!POINTER_SET (cdp->diCode) &&
266                 IS_ITEMP (IC_RESULT (cdp->diCode)) &&
267                 IS_TRUE_SYMOP (IC_RIGHT (cdp->diCode)))
268             *opp = IC_RESULT (cdp->diCode);
269           else
270             /* if straight assignement && and both
271                are temps then prefer the one that
272                will not need extra space to spil, also
273                take into consideration if right side
274                an induction variable
275              */
276             if (!POINTER_SET (cdp->diCode) &&
277                 IS_ITEMP (IC_RESULT (cdp->diCode)) &&
278                 IS_ITEMP (IC_RIGHT (cdp->diCode)) &&
279                 !OP_SYMBOL (IC_RIGHT (cdp->diCode))->isind &&
280                 ((!SPIL_LOC (IC_RIGHT (cdp->diCode)) &&
281                   SPIL_LOC (IC_RESULT (cdp->diCode))) ||
282                  (SPIL_LOC (IC_RESULT (cdp->diCode)) &&
283                   SPIL_LOC (IC_RESULT (cdp->diCode)) ==
284                   SPIL_LOC (IC_RIGHT (cdp->diCode))))
285             )
286             *opp = IC_RESULT (cdp->diCode);
287           else
288             *opp = IC_RIGHT (cdp->diCode);
289         }
290       else
291         *opp = IC_RESULT (cdp->diCode);
292     }
293
294   /* if this is an assign to a temp. then check
295      if the right side is this then return this */
296   if (IS_TRUE_SYMOP (cop) &&
297       cdp->diCode->op == '=' &&
298       !POINTER_SET (cdp->diCode) &&
299       cop->key == IC_RIGHT (cdp->diCode)->key &&
300       !isGlobalInNearSpace (IC_RIGHT (cdp->diCode)) &&
301       IS_ITEMP (IC_RESULT (cdp->diCode)))
302     *opp = IC_RESULT (cdp->diCode);
303
304   if (*opp)
305     {
306
307       if ((isGlobalInNearSpace (cop) &&
308            !isOperandLiteral (*opp)) ||
309           isOperandVolatile (*opp, FALSE)
310         )
311         {
312           *opp = NULL;
313           return 0;
314         }
315
316       if (cop->key == (*opp)->key)
317         {
318           *opp = NULL;
319           return 0;
320         }
321
322       if ((*opp)->isaddr != cop->isaddr && IS_ITEMP (cop))
323         {
324           *opp = operandFromOperand (*opp);
325           (*opp)->isaddr = cop->isaddr;
326         }
327
328       return 1;
329
330     }
331
332   return 0;
333 }
334
335 /*-----------------------------------------------------------------*/
336 /* findPointerSet - finds the right side of a pointer set op       */
337 /*-----------------------------------------------------------------*/
338 DEFSETFUNC (findPointerSet)
339 {
340   cseDef *cdp = item;
341   V_ARG (operand *, op);
342   V_ARG (operand **, opp);
343   V_ARG (operand *, rop);
344
345   if (POINTER_SET (cdp->diCode) &&
346       IC_RESULT (cdp->diCode)->key == op->key &&
347       !isOperandVolatile (IC_RESULT (cdp->diCode), TRUE) &&
348       !isOperandVolatile (IC_RIGHT (cdp->diCode), TRUE) &&
349       getSize (operandType (IC_RIGHT (cdp->diCode))) ==
350       getSize (operandType (rop)))
351     {
352       *opp = IC_RIGHT (cdp->diCode);
353       return 1;
354     }
355
356   return 0;
357 }
358
359 /*-----------------------------------------------------------------*/
360 /* findPrevIc - cseBBlock support function will return the iCode   */
361 /*              which matches the current one                      */
362 /*-----------------------------------------------------------------*/
363 DEFSETFUNC (findPrevIc)
364 {
365   cseDef *cdp = item;
366   V_ARG (iCode *, ic);
367   V_ARG (iCode **, icp);
368
369   /* if already found */
370   if (*icp)
371     return 1;
372
373   /* if the iCodes are the same */
374   if (isiCodeEqual (ic, cdp->diCode) &&
375       isOperandEqual (cdp->sym, IC_RESULT (cdp->diCode)))
376     {
377       *icp = cdp->diCode;
378       return 1;
379     }
380
381   /* if iCodes are not the same */
382   /* see the operands maybe interchanged */
383   if (ic->op == cdp->diCode->op &&
384       (ic->op == '+' || ic->op == '*') &&
385       isOperandEqual (IC_LEFT (ic), IC_RIGHT (cdp->diCode)) &&
386       isOperandEqual (IC_RIGHT (ic), IC_LEFT (cdp->diCode)))
387     {
388       *icp = cdp->diCode;
389       return 1;
390     }
391
392   return 0;
393 }
394
395 /*-----------------------------------------------------------------*/
396 /* ifDefGlobal - if definition is global                           */
397 /*-----------------------------------------------------------------*/
398 DEFSETFUNC (ifDefGlobal)
399 {
400   cseDef *cdp = item;
401
402   return (isOperandGlobal (cdp->sym));
403 }
404
405 /*-----------------------------------------------------------------*/
406 /* ifAnyGetPointer - if get pointer icode                          */
407 /*-----------------------------------------------------------------*/
408 DEFSETFUNC (ifAnyGetPointer)
409 {
410   cseDef *cdp = item;
411
412   if (cdp->diCode && POINTER_GET (cdp->diCode))
413     return 1;
414   return 0;
415 }
416
417 /*-----------------------------------------------------------------*/
418 /* ifOperandsHave - if any of the operand are the same as this     */
419 /*-----------------------------------------------------------------*/
420 DEFSETFUNC (ifOperandsHave)
421 {
422   cseDef *cdp = item;
423   V_ARG (operand *, op);
424
425
426   if (IC_LEFT (cdp->diCode) &&
427       IS_SYMOP (IC_LEFT (cdp->diCode)) &&
428       IC_LEFT (cdp->diCode)->key == op->key)
429     return 1;
430
431   if (IC_RIGHT (cdp->diCode) &&
432       IS_SYMOP (IC_RIGHT (cdp->diCode)) &&
433       IC_RIGHT (cdp->diCode)->key == op->key)
434     return 1;
435
436   /* or if any of the operands are volatile */
437   if (IC_LEFT (cdp->diCode) &&
438       IS_OP_VOLATILE (IC_LEFT (cdp->diCode)))
439     return 1;
440
441   if (IC_RIGHT (cdp->diCode) &&
442       IS_OP_VOLATILE (IC_RIGHT (cdp->diCode)))
443     return 1;
444
445
446   if (IC_RESULT (cdp->diCode) &&
447       IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
448     return 1;
449
450   return 0;
451 }
452
453 /*-----------------------------------------------------------------*/
454 /* ifDefSymIs - if a definition is found in the set                */
455 /*-----------------------------------------------------------------*/
456 int 
457 ifDefSymIs (set * cseSet, operand * sym)
458 {
459   cseDef *loop;
460   set *sl;
461
462   if (!sym || !IS_SYMOP (sym))
463     return 0;
464   for (sl = cseSet; sl; sl = sl->next)
465     {
466       loop = sl->item;
467       if (loop->sym->key == sym->key)
468         return 1;
469     }
470   return 0;
471 }
472
473
474 /*-----------------------------------------------------------------*/
475 /* ifDefSymIsX - will return 1 if the symbols match                */
476 /*-----------------------------------------------------------------*/
477 DEFSETFUNC (ifDefSymIsX)
478 {
479   cseDef *cdp = item;
480   V_ARG (operand *, op);
481
482   if (op && cdp->sym)
483     return cdp->sym->key == op->key;
484   else
485     return (isOperandEqual (cdp->sym, op));
486
487 }
488
489
490 /*-----------------------------------------------------------------*/
491 /* ifDiCodeIs - returns truw if diCode is same                     */
492 /*-----------------------------------------------------------------*/
493 int 
494 ifDiCodeIs (set * cseSet, iCode * ic)
495 {
496   cseDef *loop;
497   set *sl;
498
499   if (!ic)
500     return 0;
501
502   for (sl = cseSet; sl; sl = sl->next)
503     {
504       loop = sl->item;
505       if (loop->diCode == ic)
506         return 1;
507     }
508   return 0;
509
510 }
511
512 /*-----------------------------------------------------------------*/
513 /* ifPointerGet - returns true if the icode is pointer get sym     */
514 /*-----------------------------------------------------------------*/
515 DEFSETFUNC (ifPointerGet)
516 {
517   cseDef *cdp = item;
518   V_ARG (operand *, op);
519   iCode *dic = cdp->diCode;
520   operand *left = IC_LEFT (cdp->diCode);
521
522   if (POINTER_GET (dic) && left->key == op->key)
523     return 1;
524
525   return 0;
526 }
527
528 /*-----------------------------------------------------------------*/
529 /* ifPointerSet - returns true if the icode is pointer set sym     */
530 /*-----------------------------------------------------------------*/
531 DEFSETFUNC (ifPointerSet)
532 {
533   cseDef *cdp = item;
534   V_ARG (operand *, op);
535
536   if (POINTER_SET (cdp->diCode) &&
537       IC_RESULT (cdp->diCode)->key == op->key)
538     return 1;
539
540   return 0;
541 }
542
543 /*-----------------------------------------------------------------*/
544 /* ifDiCodeIsX - will return 1 if the symbols match                 */
545 /*-----------------------------------------------------------------*/
546 DEFSETFUNC (ifDiCodeIsX)
547 {
548   cseDef *cdp = item;
549   V_ARG (iCode *, ic);
550
551   return cdp->diCode == ic;
552
553 }
554
555 /*-----------------------------------------------------------------*/
556 /* algebraicOpts - does some algebraic optimizations               */
557 /*-----------------------------------------------------------------*/
558 void 
559 algebraicOpts (iCode * ic)
560 {
561   /* we don't deal with the following iCodes
562      here */
563   if (ic->op == IFX ||
564       ic->op == IPUSH ||
565       ic->op == IPOP ||
566       ic->op == CALL ||
567       ic->op == PCALL ||
568       ic->op == RETURN ||
569       POINTER_GET (ic))
570     return;
571
572   /* if both operands present & ! IFX */
573   /* then if they are both literal we */
574   /* perform the operation right now  */
575   if (IC_RESULT (ic) &&
576       IC_RIGHT (ic) &&
577       IC_LEFT (ic) &&
578       IS_OP_LITERAL (IC_LEFT (ic)) &&
579       IS_OP_LITERAL (IC_RIGHT (ic)))
580     {
581
582       IC_RIGHT (ic) = operandOperation (IC_LEFT (ic),
583                                         IC_RIGHT (ic),
584                                         ic->op,
585                                         operandType (IC_RESULT (ic)));
586       ic->op = '=';
587       IC_LEFT (ic) = NULL;
588       SET_RESULT_RIGHT (ic);
589       return;
590
591     }
592   /* if not ifx & only one operand present */
593   if (IC_RESULT (ic) &&
594       IC_LEFT (ic) &&
595       IS_OP_LITERAL (IC_LEFT (ic)) &&
596       !IC_RIGHT (ic))
597     {
598
599       IC_RIGHT (ic) = operandOperation (IC_LEFT (ic),
600                                         IC_RIGHT (ic),
601                                         ic->op,
602                                         operandType (IC_RESULT (ic)));
603       ic->op = '=';
604       IC_LEFT (ic) = NULL;
605       SET_RESULT_RIGHT (ic);
606       return;
607     }
608
609
610   /* a special case : or in short a kludgy solution will think
611      about a better solution over a glass of wine someday */
612   if (ic->op == GET_VALUE_AT_ADDRESS)
613     {
614
615       if (IS_ITEMP (IC_RESULT (ic)) &&
616           IS_TRUE_SYMOP (IC_LEFT (ic)))
617         {
618
619           ic->op = '=';
620           IC_RIGHT (ic) = operandFromOperand (IC_LEFT (ic));
621           IC_RIGHT (ic)->isaddr = 0;
622           IC_LEFT (ic) = NULL;
623           IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
624           IC_RESULT (ic)->isaddr = 0;
625           setOperandType (IC_RESULT (ic), operandType (IC_RIGHT (ic)));
626           return;
627         }
628
629       if (IS_ITEMP (IC_LEFT (ic)) &&
630           IS_ITEMP (IC_RESULT (ic)) &&
631 /*      !OP_SYMBOL(IC_RESULT(ic))->isreqv && */
632 /*      !OP_SYMBOL(IC_LEFT(ic))->isreqv && */
633           !IC_LEFT (ic)->isaddr)
634         {
635           ic->op = '=';
636           IC_RIGHT (ic) = operandFromOperand (IC_LEFT (ic));
637           IC_RIGHT (ic)->isaddr = 0;
638           IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
639           IC_RESULT (ic)->isaddr = 0;
640           IC_LEFT (ic) = NULL;
641           return;
642         }
643
644     }
645
646
647   /* depending on the operation */
648   switch (ic->op)
649     {
650     case '+':
651       /* if adding the same thing change to left shift by 1 */
652       if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key &&
653           !IS_FLOAT (operandType (IC_RESULT (ic))))
654         {
655           ic->op = LEFT_OP;
656           IC_RIGHT (ic) = operandFromLit (1);
657           return;
658         }
659       /* if addition then check if one of them is a zero */
660       /* if yes turn it  into assignmnt */
661       if (IS_OP_LITERAL (IC_LEFT (ic)) &&
662           operandLitValue (IC_LEFT (ic)) == 0.0)
663         {
664
665           ic->op = '=';
666           IC_LEFT (ic) = NULL;
667           SET_ISADDR (IC_RESULT (ic), 0);
668           SET_ISADDR (IC_RIGHT (ic), 0);
669           return;
670         }
671       if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
672           operandLitValue (IC_RIGHT (ic)) == 0.0)
673         {
674
675           ic->op = '=';
676           IC_RIGHT (ic) = IC_LEFT (ic);
677           IC_LEFT (ic) = 0;
678           SET_ISADDR (IC_RIGHT (ic), 0);
679           SET_ISADDR (IC_RESULT (ic), 0);
680           return;
681         }
682       break;
683     case '-':
684       /* if subtracting the the same thing then zero     */
685       if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key)
686         {
687           ic->op = '=';
688           IC_RIGHT (ic) = operandFromLit (0);
689           IC_LEFT (ic) = NULL;
690           IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
691           IC_RESULT (ic)->isaddr = 0;
692           return;
693         }
694
695       /* if subtraction then check if one of the operand */
696       /* is zero then depending on which operand change  */
697       /* to assignment or unary minus                    */
698       if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
699           operandLitValue (IC_RIGHT (ic)) == 0.0)
700         {
701           /* right size zero change to assignment */
702           ic->op = '=';
703           IC_RIGHT (ic) = IC_LEFT (ic);
704           IC_LEFT (ic) = NULL;
705           SET_ISADDR (IC_RIGHT (ic), 0);
706           SET_ISADDR (IC_RESULT (ic), 0);
707           return;
708         }
709       if (IS_OP_LITERAL (IC_LEFT (ic)) &&
710           operandLitValue (IC_LEFT (ic)) == 0.0)
711         {
712           /* left zero turn into an unary minus */
713           ic->op = UNARYMINUS;
714           IC_LEFT (ic) = IC_RIGHT (ic);
715           IC_RIGHT (ic) = NULL;
716           return;
717         }
718       break;
719       /* if multiplication then check if either of */
720       /* them is zero then the result is zero      */
721       /* if either of them is one then result is   */
722       /* the other one                             */
723     case '*':
724       if (IS_OP_LITERAL (IC_LEFT (ic)))
725         {
726
727           if (operandLitValue (IC_LEFT (ic)) == 0.0)
728             {
729               ic->op = '=';
730               IC_RIGHT (ic) = IC_LEFT (ic);
731               IC_LEFT (ic) = NULL;
732               SET_RESULT_RIGHT (ic);
733               return;
734             }
735           if (operandLitValue (IC_LEFT (ic)) == 1.0)
736             {
737               ic->op = '=';
738               IC_LEFT (ic) = NULL;
739               SET_RESULT_RIGHT (ic);
740               return;
741             }
742         }
743
744       if (IS_OP_LITERAL (IC_RIGHT (ic)))
745         {
746
747           if (operandLitValue (IC_RIGHT (ic)) == 0.0)
748             {
749               ic->op = '=';
750               IC_LEFT (ic) = NULL;
751               SET_RESULT_RIGHT (ic);
752               return;
753             }
754
755           if (operandLitValue (IC_RIGHT (ic)) == 1.0)
756             {
757               ic->op = '=';
758               IC_RIGHT (ic) = IC_LEFT (ic);
759               IC_LEFT (ic) = NULL;
760               SET_RESULT_RIGHT (ic);
761               return;
762             }
763         }
764       break;
765     case '/':
766       /* if division by self then 1 */
767       if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key)
768         {
769           ic->op = '=';
770           IC_RIGHT (ic) = operandFromLit (1);
771           IC_LEFT (ic) = NULL;
772           IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
773           IC_RESULT (ic)->isaddr = 0;
774         }
775       /* if this is a division then check if right */
776       /* is one then change it to an assignment    */
777       if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
778           operandLitValue (IC_RIGHT (ic)) == 1.0)
779         {
780
781           ic->op = '=';
782           IC_RIGHT (ic) = IC_LEFT (ic);
783           IC_LEFT (ic) = NULL;
784           SET_RESULT_RIGHT (ic);
785           return;
786         }
787       break;
788       /* if both are the same for an comparison operators */
789     case EQ_OP:
790     case LE_OP:
791     case GE_OP:
792       if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
793         {
794           ic->op = '=';
795           IC_RIGHT (ic) = operandFromLit (1);
796           IC_LEFT (ic) = NULL;
797           SET_RESULT_RIGHT (ic);
798         }
799       break;
800     case NE_OP:
801     case '>':
802     case '<':
803       if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
804         {
805           ic->op = '=';
806           IC_RIGHT (ic) = operandFromLit (0);
807           IC_LEFT (ic) = NULL;
808           SET_RESULT_RIGHT (ic);
809         }
810       break;
811     case CAST:
812       /* if this is a cast of a literal value */
813       if (IS_OP_LITERAL (IC_RIGHT (ic)))
814         {
815           ic->op = '=';
816           IC_RIGHT (ic) =
817             operandFromValue (valCastLiteral (operandType (IC_LEFT (ic)),
818                                           operandLitValue (IC_RIGHT (ic))));
819           IC_LEFT (ic) = NULL;
820           SET_ISADDR (IC_RESULT (ic), 0);
821         }
822       /* if casting to the same */
823       if (checkType (operandType (IC_RESULT (ic)),
824                      operandType (IC_RIGHT (ic))) == 1)
825         {
826           ic->op = '=';
827           IC_LEFT (ic) = NULL;
828           SET_ISADDR (IC_RESULT (ic), 0);
829         }
830       break;
831     case '!':
832       if (IS_OP_LITERAL (IC_LEFT (ic)))
833         {
834           ic->op = '=';
835           IC_RIGHT (ic) =
836             (operandLitValue (IC_LEFT (ic)) == 0 ?
837              operandFromLit (1) : operandFromLit (0));
838           IC_LEFT (ic) = NULL;
839           SET_ISADDR (IC_RESULT (ic), 0);
840         }
841     }
842
843   return;
844 }
845 #define OTHERS_PARM(s) (s->_isparm && !s->ismyparm)
846 /*-----------------------------------------------------------------*/
847 /* updateSpillLocation - keeps track of register spill location    */
848 /*-----------------------------------------------------------------*/
849 void 
850 updateSpillLocation (iCode * ic)
851 {
852
853   sym_link *setype;
854
855   if (POINTER_SET (ic))
856     return;
857
858   if (ic->nosupdate)
859     return;
860
861   /* for the form true_symbol := iTempNN */
862   if (ASSIGN_ITEMP_TO_SYM (ic)
863       && !SPIL_LOC (IC_RIGHT (ic)))
864     {
865
866       setype = getSpec (operandType (IC_RESULT (ic)));
867
868       if (!IC_RIGHT (ic)->noSpilLoc &&
869           !IS_VOLATILE (setype) &&
870           !IN_FARSPACE (SPEC_OCLS (setype)) &&
871           !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
872
873         SPIL_LOC (IC_RIGHT (ic)) =
874           IC_RESULT (ic)->operand.symOperand;
875     }
876
877   if (ASSIGN_ITEMP_TO_ITEMP (ic) &&
878       !SPIL_LOC (IC_RIGHT (ic)) &&
879   !bitVectBitsInCommon (OP_DEFS (IC_RIGHT (ic)), OP_USES (IC_RESULT (ic))) &&
880       OP_SYMBOL (IC_RESULT (ic))->isreqv)
881     {
882
883       setype = getSpec (operandType (IC_RESULT (ic)));
884
885       if (!IC_RIGHT (ic)->noSpilLoc &&
886           !IS_VOLATILE (setype) &&
887           !IN_FARSPACE (SPEC_OCLS (setype)) &&
888           !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
889
890         SPIL_LOC (IC_RIGHT (ic)) =
891           SPIL_LOC (IC_RESULT (ic));
892     }
893 }
894
895 /*-----------------------------------------------------------------*/
896 /* setUsesDef - sets the uses def bitvector for a given operand    */
897 /*-----------------------------------------------------------------*/
898 void 
899 setUsesDefs (operand * op, bitVect * bdefs,
900              bitVect * idefs, bitVect ** oud)
901 {
902   /* compute the definitions alive at this point */
903   bitVect *adefs = bitVectUnion (bdefs, idefs);
904
905   /* of these definitions find the ones that are */
906   /* for this operand */
907   adefs = bitVectIntersect (adefs, OP_DEFS (op));
908
909   /* these are the definitions that this operand can use */
910   op->usesDefs = adefs;
911
912   /* the out defs is an union */
913   *oud = bitVectUnion (*oud, adefs);
914 }
915
916 /*-----------------------------------------------------------------*/
917 /* unsetDefsAndUses - clear this operation for the operands        */
918 /*-----------------------------------------------------------------*/
919 void 
920 unsetDefsAndUses (iCode * ic)
921 {
922   if (ic->op == JUMPTABLE)
923     return;
924
925   /* take away this definition from the def chain of the */
926   /* result & take away from use set of the operands */
927   if (ic->op != IFX)
928     {
929       /* turn off def set */
930       if (IS_SYMOP (IC_RESULT (ic)))
931         {
932           if (!POINTER_SET (ic))
933             bitVectUnSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
934           else
935             bitVectUnSetBit (OP_USES (IC_RESULT (ic)), ic->key);
936         }
937       /* turn off the useSet for the operands */
938       if (IS_SYMOP (IC_LEFT (ic)))
939         bitVectUnSetBit (OP_USES (IC_LEFT (ic)), ic->key);
940
941       if (IS_SYMOP (IC_RIGHT (ic)))
942         bitVectUnSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
943     }
944   else
945     /* must be ifx turn off the use */ if (IS_SYMOP (IC_COND (ic)))
946     bitVectUnSetBit (OP_USES (IC_COND (ic)), ic->key);
947 }
948
949 /*-----------------------------------------------------------------*/
950 /* ifxOptimize - changes ifx conditions if it can                  */
951 /*-----------------------------------------------------------------*/
952 void 
953 ifxOptimize (iCode * ic, set * cseSet,
954              int computeOnly,
955              eBBlock * ebb, int *change,
956              eBBlock ** ebbs, int count)
957 {
958   operand *pdop;
959   symbol *label;
960
961   /* if the condition can be replaced */
962   if (!computeOnly)
963     {
964       pdop = NULL;
965       applyToSetFTrue (cseSet, findCheaperOp, IC_COND (ic), &pdop);
966       if (pdop)
967         {
968           IC_COND (ic) = pdop;
969           (*change)++;
970         }
971     }
972
973   /* if the conditional is a literal then */
974   if (IS_OP_LITERAL (IC_COND (ic)))
975     {
976
977       if ((operandLitValue (IC_COND (ic)) != 0.0) && IC_TRUE (ic))
978         {
979
980           /* change to a goto */
981           ic->op = GOTO;
982           IC_LABEL (ic) = IC_TRUE (ic);
983           (*change)++;
984
985         }
986       else
987         {
988
989           if (!operandLitValue (IC_COND (ic)) && IC_FALSE (ic))
990             {
991               ic->op = GOTO;
992               IC_LABEL (ic) = IC_FALSE (ic);
993               (*change)++;
994
995             }
996           else
997             {
998               /* then kill this if condition */
999               remiCodeFromeBBlock (ebb, ic);
1000             }
1001         }
1002
1003       /* now we need to recompute the control flow */
1004       /* since the control flow has changed        */
1005       /* this is very expensive but it does not happen */
1006       /* too often, if it does happen then the user pays */
1007       /* the price */
1008       computeControlFlow (ebbs, count, 1);
1009       werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
1010       return;
1011     }
1012
1013   /* if there is only one successor and that successor
1014      is the same one we are conditionally going to then
1015      we can remove this conditional statement */
1016   label = (IC_TRUE (ic) ? IC_TRUE (ic) : IC_FALSE (ic));
1017   if (elementsInSet (ebb->succList) == 1 &&
1018       isinSet (ebb->succList, eBBWithEntryLabel (ebbs, label, count)))
1019     {
1020
1021       remiCodeFromeBBlock (ebb, ic);
1022       computeControlFlow (ebbs, count, 1);
1023       werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
1024       return;
1025     }
1026
1027
1028   /* if it remains an IFX the update the use Set */
1029   OP_USES (IC_COND (ic)) = bitVectSetBit (OP_USES (IC_COND (ic)), ic->key);
1030   setUsesDefs (IC_COND (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1031   return;
1032 }
1033
1034 /*-----------------------------------------------------------------*/
1035 /* diCodeForSym - finds the definiting instruction for a symbol    */
1036 /*-----------------------------------------------------------------*/
1037 DEFSETFUNC (diCodeForSym)
1038 {
1039   cseDef *cdp = item;
1040   V_ARG (operand *, sym);
1041   V_ARG (iCode **, dic);
1042
1043   /* if already found */
1044   if (*dic)
1045     return 0;
1046
1047   /* if not if this is the defining iCode */
1048   if (sym->key == cdp->key)
1049     {
1050       *dic = cdp->diCode;
1051       return 1;
1052     }
1053
1054   return 0;
1055 }
1056
1057 /*-----------------------------------------------------------------*/
1058 /* constFold - does some constant folding                          */
1059 /*-----------------------------------------------------------------*/
1060 int 
1061 constFold (iCode * ic, set * cseSet)
1062 {
1063   iCode *dic = NULL;
1064   iCode *ldic = NULL;
1065   /* this routine will change
1066      a = b + 10;
1067      c = a + 10;
1068      to
1069      c = b + 20; */
1070
1071   /* deal with only + & - */
1072   if (ic->op != '+' &&
1073       ic->op != '-')
1074     return 0;
1075
1076   /* this check is a hueristic to prevent live ranges
1077      from becoming too long */
1078   if (IS_PTR (operandType (IC_RESULT (ic))))
1079     return 0;
1080
1081   /* check if operation with a literal */
1082   if (!IS_OP_LITERAL (IC_RIGHT (ic)))
1083     return 0;
1084
1085   /* check if we can find a definition for the
1086      right hand side */
1087   if (!(applyToSet (cseSet, diCodeForSym, IC_LEFT (ic), &dic)))
1088     return 0;
1089
1090   /* check that this is also a +/-  */
1091   if (dic->op != '+' && dic->op != '-')
1092     return 0;
1093
1094   /* with a literal */
1095   if (!IS_OP_LITERAL (IC_RIGHT (dic)))
1096     return 0;
1097
1098   /* find the definition of the left operand
1099      of dic.then check if this defined with a
1100      get_pointer return 0 if the pointer size is
1101      less than 2 (MCS51 specific) */
1102   if (!(applyToSet (cseSet, diCodeForSym, IC_LEFT (dic), &ldic)))
1103     return 0;
1104
1105   if (POINTER_GET (ldic) && getSize (operandType (IC_LEFT (ldic))) <= 1)
1106     return 0;
1107
1108   /* it is if the operations are the same */
1109   /* the literal parts need to be added  */
1110   IC_LEFT (ic) = operandFromOperand (IC_LEFT (dic));
1111   if (ic->op == dic->op)
1112     IC_RIGHT (ic) = operandFromLit (operandLitValue (IC_RIGHT (ic)) +
1113                                     operandLitValue (IC_RIGHT (dic)));
1114   else
1115     IC_RIGHT (ic) = operandFromLit (operandLitValue (IC_RIGHT (ic)) -
1116                                     operandLitValue (IC_RIGHT (dic)));
1117
1118   if (IS_ITEMP (IC_RESULT (ic)))
1119     {
1120       SPIL_LOC (IC_RESULT (ic)) = NULL;
1121       IC_RESULT (ic)->noSpilLoc = 1;
1122     }
1123
1124
1125   return 1;
1126 }
1127
1128 /*-----------------------------------------------------------------*/
1129 /* deleteGetPointers - called when a pointer is passed as parm     */
1130 /* will delete from cseSet all get pointers computed from this     */
1131 /* pointer. A simple ifOperandsHave is not good enough here        */
1132 /*-----------------------------------------------------------------*/
1133 static void 
1134 deleteGetPointers (set ** cseSet, set ** pss, operand * op, eBBlock * ebb)
1135 {
1136   set *compItems = NULL;
1137   cseDef *cdp;
1138   operand *cop;
1139
1140   /* easy return */
1141   if (!*cseSet && !*pss)
1142     return;
1143
1144   /* first find all items computed from this operand .
1145      This done fairly simply go thru the list and find
1146      those that are computed by arthimetic with this
1147      op */
1148   for (cdp = setFirstItem (*cseSet); cdp; cdp = setNextItem (*cseSet))
1149     {
1150       if (IS_ARITHMETIC_OP (cdp->diCode))
1151         {
1152           if (isOperandEqual (IC_LEFT (cdp->diCode), op) ||
1153               isOperandEqual (IC_RIGHT (cdp->diCode), op))
1154             {
1155               /* save it in our list of items */
1156               addSet (&compItems, IC_RESULT (cdp->diCode));
1157             }
1158           /* also check for those computed from our computed
1159              list . This will take care of situations like
1160              iTemp1 = iTemp0 + 8;
1161              iTemp2 = iTemp1 + 8; */
1162           if (isinSetWith (compItems, IC_LEFT (cdp->diCode), isOperandEqual) ||
1163             isinSetWith (compItems, IC_RIGHT (cdp->diCode), isOperandEqual))
1164             {
1165               addSet (&compItems, IC_RESULT (cdp->diCode));
1166             }
1167         }
1168     }
1169
1170   /* now delete all pointer gets with this op */
1171   deleteItemIf (cseSet, ifPointerGet, op);
1172   deleteItemIf (pss, ifPointerSet, op);
1173
1174   /* set the bit vector used by dataFlow computation later */
1175   ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, op->key);
1176   /* now for the computed items */
1177   for (cop = setFirstItem (compItems); cop; cop = setNextItem (compItems))
1178     {
1179       ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, cop->key);
1180       deleteItemIf (cseSet, ifPointerGet, cop);
1181       deleteItemIf (pss, ifPointerSet, cop);
1182     }
1183 }
1184
1185 /*-----------------------------------------------------------------*/
1186 /* delGetPointerSucc - delete get pointer from inExprs of succ with */
1187 /*                     dfnum > supplied                            */
1188 /*-----------------------------------------------------------------*/
1189 DEFSETFUNC (delGetPointerSucc)
1190 {
1191   eBBlock *ebp = item;
1192   V_ARG (operand *, op);
1193   V_ARG (int, dfnum);
1194
1195   if (ebp->visited)
1196     return 0;
1197
1198   ebp->visited = 1;
1199   if (ebp->dfnum > dfnum)
1200     {
1201       deleteItemIf (&ebp->inExprs, ifPointerGet, op);
1202     }
1203
1204   return applyToSet (ebp->succList, delGetPointerSucc, op, dfnum);
1205 }
1206
1207 /*-----------------------------------------------------------------*/
1208 /* fixUpTypes - KLUGE HACK fixup a lowering problem                */
1209 /*-----------------------------------------------------------------*/
1210 static void 
1211 fixUpTypes (iCode * ic)
1212 {
1213   sym_link *t1 = operandType (IC_LEFT (ic)), *t2;
1214
1215   if (IS_DS390_PORT)
1216     {
1217       /* hack-o-matic! */
1218       return;
1219     }
1220
1221   /* for pointer_gets if the types of result & left r the
1222      same then change it type of result to next */
1223   if (IS_PTR (t1) &&
1224       checkType (t2 = operandType (IC_RESULT (ic)), t1) == 1)
1225     {
1226       setOperandType (IC_RESULT (ic), t2->next);
1227     }
1228 }
1229
1230 /*-----------------------------------------------------------------*/
1231 /* cseBBlock - common subexpression elimination for basic blocks   */
1232 /*             this is the hackiest kludgiest routine in the whole */
1233 /*             system. also the most important, since almost all   */
1234 /*             data flow related information is computed by it     */
1235 /*-----------------------------------------------------------------*/
1236 int 
1237 cseBBlock (eBBlock * ebb, int computeOnly,
1238            eBBlock ** ebbs, int count)
1239 {
1240   set *cseSet;
1241   iCode *ic;
1242   int change = 0;
1243   int i;
1244   set *ptrSetSet = NULL;
1245
1246   /* if this block is not reachable */
1247   if (ebb->noPath)
1248     return change;
1249
1250   /* set of common subexpressions */
1251   cseSet = setFromSet (ebb->inExprs);
1252
1253   /* these will be computed by this routine */
1254   setToNull ((void **) &ebb->outDefs);
1255   setToNull ((void **) &ebb->defSet);
1256   setToNull ((void **) &ebb->usesDefs);
1257   setToNull ((void **) &ebb->ptrsSet);
1258   setToNull ((void **) &ebb->addrOf);
1259   setToNull ((void **) &ebb->ldefs);
1260
1261   ebb->outDefs = bitVectCopy (ebb->inDefs);
1262   bitVectDefault = iCodeKey;
1263   ebb->defSet = newBitVect (iCodeKey);
1264   ebb->usesDefs = newBitVect (iCodeKey);
1265
1266   /* for all the instructions in this block do */
1267   for (ic = ebb->sch; ic; ic = ic->next)
1268     {
1269
1270       iCode *pdic;
1271       operand *pdop;
1272       iCode *defic;
1273
1274       if (SKIP_IC2 (ic))
1275         continue;
1276
1277       /* if this is an assignment from true symbol
1278          to a temp then do pointer post inc/dec optimzation */
1279       if (ic->op == '=' && !POINTER_SET (ic) &&
1280           IS_PTR (operandType (IC_RESULT (ic))))
1281         {
1282           ptrPostIncDecOpt (ic);
1283         }
1284
1285       /* clear the def & use chains for the operands involved */
1286       /* in this operation . since it can change due to opts  */
1287       unsetDefsAndUses (ic);
1288
1289       if (ic->op == PCALL || ic->op == CALL || ic->op == RECEIVE)
1290         {
1291           /* add to defSet of the symbol */
1292           OP_DEFS (IC_RESULT (ic)) =
1293             bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
1294           /* add to the definition set of this block */
1295           ebb->defSet = bitVectSetBit (ebb->defSet, ic->key);
1296           ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key);
1297           ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic)));
1298           setUsesDefs (IC_RESULT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1299           /* delete global variables from the cseSet
1300              since they can be modified by the function call */
1301           deleteItemIf (&cseSet, ifDefGlobal);
1302           /* delete all getpointer iCodes from cseSet, this should
1303              be done only for global arrays & pointers but at this
1304              point we don't know if globals, so to be safe do all */
1305           deleteItemIf (&cseSet, ifAnyGetPointer);
1306         }
1307
1308       /* for pcall & ipush we need to add to the useSet */
1309       if ((ic->op == PCALL ||
1310            ic->op == IPUSH ||
1311            ic->op == IPOP ||
1312            ic->op == SEND) &&
1313           IS_SYMOP (IC_LEFT (ic)))
1314         {
1315
1316           /* check if they can be replaced */
1317           if (!computeOnly)
1318             {
1319               pdop = NULL;
1320               applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop);
1321               if (pdop)
1322                 IC_LEFT (ic) = pdop;
1323             }
1324           /* the lookup could have changed it */
1325           if (IS_SYMOP (IC_LEFT (ic)))
1326             {
1327               OP_USES (IC_LEFT (ic)) =
1328                 bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1329               setUsesDefs (IC_LEFT (ic), ebb->defSet,
1330                            ebb->outDefs, &ebb->usesDefs);
1331             }
1332
1333
1334           /* if we a sending a pointer as a parameter
1335              then kill all cse since the pointed to item
1336              might be changed in the function being called */
1337           if ((ic->op == IPUSH || ic->op == SEND) &&
1338               IS_PTR (operandType (IC_LEFT (ic))))
1339             {
1340               deleteGetPointers (&cseSet, &ptrSetSet, IC_LEFT (ic), ebb);
1341               ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_LEFT (ic)->key);
1342               for (i = 0; i < count; ebbs[i++]->visited = 0);
1343               applyToSet (ebb->succList, delGetPointerSucc,
1344                           IC_LEFT (ic), ebb->dfnum);
1345             }
1346           continue;
1347         }
1348
1349       /* if jumptable then mark the usage */
1350       if (ic->op == JUMPTABLE)
1351         {
1352           OP_USES (IC_JTCOND (ic)) =
1353             bitVectSetBit (OP_USES (IC_JTCOND (ic)), ic->key);
1354           setUsesDefs (IC_JTCOND (ic), ebb->defSet,
1355                        ebb->outDefs, &ebb->usesDefs);
1356           continue;
1357         }
1358
1359       if (SKIP_IC (ic))
1360         continue;
1361
1362       /* do some algebraic optimizations if possible */
1363       algebraicOpts (ic);
1364       while (constFold (ic, cseSet));
1365
1366       /* small klugde */
1367       if (POINTER_GET (ic) && !IS_PTR (operandType (IC_LEFT (ic))))
1368         {
1369           setOperandType (IC_LEFT (ic),
1370                           aggrToPtr (operandType (IC_LEFT (ic)), FALSE));
1371           fixUpTypes (ic);
1372
1373         }
1374       if (POINTER_SET (ic) && !IS_PTR (operandType (IC_RESULT (ic))))
1375         {
1376           setOperandType (IC_RESULT (ic),
1377                           aggrToPtr (operandType (IC_RESULT (ic)), FALSE));
1378         }
1379
1380       /* if this is a condition statment then */
1381       /* check if the condition can be replaced */
1382       if (ic->op == IFX)
1383         {
1384           ifxOptimize (ic, cseSet, computeOnly,
1385                        ebb, &change,
1386                        ebbs, count);
1387           continue;
1388         }
1389
1390       /* if the assignment & result is a temp */
1391       /* see if we can replace it             */
1392       if (ic->op == '=')
1393         {
1394
1395           /* update the spill location for this */
1396           updateSpillLocation (ic);
1397
1398           if (POINTER_SET (ic) &&
1399               !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype)))
1400             {
1401               pdop = NULL;
1402               applyToSetFTrue (cseSet, findCheaperOp, IC_RESULT (ic), &pdop);
1403               if (pdop && IS_ITEMP (pdop) && !computeOnly)
1404                 IC_RESULT (ic) = pdop;
1405             }
1406         }
1407
1408       /* do the operand lookup i.e. for both the */
1409       /* right & left operand : check the cseSet */
1410       /* to see if they have been replaced if yes */
1411       /* then replace them with those from cseSet */
1412       /* left operand */
1413       /* and left is a symbol  */
1414       if (IS_SYMOP (IC_LEFT (ic)) &&
1415           !computeOnly && ic->op != ADDRESS_OF)
1416         {
1417
1418           pdop = NULL;
1419           applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop);
1420           if (pdop)
1421             {
1422               if (POINTER_GET (ic))
1423                 {
1424                   if (IS_ITEMP (pdop) || IS_OP_LITERAL (pdop))
1425                     {
1426                       IC_LEFT (ic) = pdop;
1427                       change = 1;
1428                     }
1429                   /* check if there is a pointer set
1430                      for the same pointer visible if yes
1431                      then change this into an assignment */
1432                   pdop = NULL;
1433                   if (applyToSetFTrue (cseSet, findPointerSet, IC_LEFT (ic), &pdop, IC_RESULT (ic)) &&
1434                       !bitVectBitValue (ebb->ptrsSet, pdop->key))
1435                     {
1436                       ic->op = '=';
1437                       IC_LEFT (ic) = NULL;
1438                       IC_RIGHT (ic) = pdop;
1439                       SET_ISADDR (IC_RESULT (ic), 0);
1440                     }
1441
1442                 }
1443               else
1444                 {
1445                   IC_LEFT (ic) = pdop;
1446                   change = 1;
1447                 }
1448             }
1449         }
1450
1451       /*right operand */
1452       if (IS_SYMOP (IC_RIGHT (ic)) && !computeOnly)
1453         {
1454
1455           pdop = NULL;
1456           applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop);
1457           if (pdop)
1458             {
1459
1460               IC_RIGHT (ic) = pdop;
1461               change = 1;
1462             }
1463         }
1464
1465       /* if left or right changed then do algebraic */
1466       if (change)
1467         {
1468           algebraicOpts (ic);
1469           while (constFold (ic, cseSet));
1470         }
1471
1472       /* if after all this it becomes a assignment to self
1473          then delete it and continue */
1474       if (ASSIGNMENT_TO_SELF (ic))
1475         {
1476           remiCodeFromeBBlock (ebb, ic);
1477           continue;
1478         }
1479
1480       /* now we will check to see if the entire */
1481       /* operation has been performed before    */
1482       /* and is available                       */
1483       /* don't do assignments they will be killed */
1484       /* by dead code elimination if required  do */
1485       /* it only if result is a temporary         */
1486       pdic = NULL;
1487       if (!(POINTER_GET (ic) &&
1488             (IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype) ||
1489              isOperandVolatile (IC_LEFT (ic), TRUE) ||
1490              bitVectBitValue (ebb->ndompset, IC_LEFT (ic)->key))) &&
1491           !ASSIGNMENT (ic) &&
1492           IS_ITEMP (IC_RESULT (ic)) &&
1493           !computeOnly)
1494         {
1495           applyToSet (cseSet, findPrevIc, ic, &pdic);
1496           if (pdic && checkType (operandType (IC_RESULT (pdic)),
1497                                  operandType (IC_RESULT (ic))) != 1)
1498             pdic = NULL;
1499         }
1500
1501       /* if found then eliminate this and add to */
1502       /* to cseSet an element containing result */
1503       /* of this with previous opcode           */
1504       if (pdic)
1505         {
1506
1507           if (IS_ITEMP (IC_RESULT (ic)))
1508             {
1509
1510               /* replace in the remaining of this block */
1511               replaceAllSymBySym (ic->next, IC_RESULT (ic), IC_RESULT (pdic), &ebb->ndompset);
1512               /* remove this iCode from inexpressions of all
1513                  its successors, it cannot be in the in expressions
1514                  of any of the predecessors */
1515               for (i = 0; i < count; ebbs[i++]->visited = 0);
1516               applyToSet (ebb->succList, removeFromInExprs, ic, IC_RESULT (ic),
1517                           IC_RESULT (pdic), ebb);
1518
1519               /* if this was moved from another block */
1520               /* then replace in those blocks too     */
1521               if (ic->movedFrom)
1522                 {
1523                   eBBlock *owner;
1524                   for (owner = setFirstItem (ic->movedFrom); owner;
1525                        owner = setNextItem (ic->movedFrom))
1526                     replaceAllSymBySym (owner->sch, IC_RESULT (ic), IC_RESULT (pdic), &owner->ndompset);
1527                 }
1528               pdic->movedFrom = unionSets (pdic->movedFrom, ic->movedFrom, THROW_NONE);
1529             }
1530           else
1531             addSetHead (&cseSet, newCseDef (IC_RESULT (ic), pdic));
1532
1533           if (!computeOnly)
1534             /* eliminate this */
1535             remiCodeFromeBBlock (ebb, ic);
1536
1537           defic = pdic;
1538           change++;
1539
1540           if (IS_ITEMP (IC_RESULT (ic)))
1541             continue;
1542
1543         }
1544       else
1545         {
1546
1547           /* just add this as a previous expression except in */
1548           /* case of a pointer access in which case this is a */
1549           /* usage not a definition                           */
1550           if (!(POINTER_SET (ic)) && IC_RESULT (ic))
1551             {
1552               deleteItemIf (&cseSet, ifDefSymIsX, IC_RESULT (ic));
1553               addSetHead (&cseSet, newCseDef (IC_RESULT (ic), ic));
1554             }
1555           defic = ic;
1556
1557         }
1558
1559       /* if assignment to a parameter which is not
1560          mine and type is a pointer then delete
1561          pointerGets to take care of aliasing */
1562       if (ASSIGNMENT (ic) &&
1563           OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))) &&
1564           IS_PTR (operandType (IC_RESULT (ic))))
1565         {
1566           deleteGetPointers (&cseSet, &ptrSetSet, IC_RIGHT (ic), ebb);
1567           for (i = 0; i < count; ebbs[i++]->visited = 0);
1568           applyToSet (ebb->succList, delGetPointerSucc, IC_RIGHT (ic), ebb->dfnum);
1569           ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_RIGHT (ic)->key);
1570         }
1571
1572       /* if this is a pointerget then see if we can replace
1573          this with a previously assigned pointer value */
1574       if (POINTER_GET (ic) &&
1575           !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype) ||
1576             isOperandVolatile (IC_LEFT (ic), TRUE)))
1577         {
1578           pdop = NULL;
1579           applyToSet (ptrSetSet, findPointerSet, IC_LEFT (ic), &pdop, IC_RESULT (ic));
1580           /* if we find it then locally replace all
1581              references to the result with what we assigned */
1582           if (pdop)
1583             {
1584               replaceAllSymBySym (ic->next, IC_RESULT (ic), pdop, &ebb->ndompset);
1585             }
1586         }
1587
1588       /* delete from the cseSet anything that has */
1589       /* operands matching the result of this     */
1590       /* except in case of pointer access         */
1591       if (!(POINTER_SET (ic)) && IC_RESULT (ic))
1592         {
1593           deleteItemIf (&cseSet, ifOperandsHave, IC_RESULT (ic));
1594           /* delete any previous definitions */
1595           ebb->defSet = bitVectCplAnd (ebb->defSet, OP_DEFS (IC_RESULT (ic)));
1596
1597         }
1598
1599       /* add the left & right to the defUse set */
1600       if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)))
1601         {
1602           OP_USES (IC_LEFT (ic)) =
1603             bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1604           setUsesDefs (IC_LEFT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1605
1606         }
1607
1608       if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)))
1609         {
1610           OP_USES (IC_RIGHT (ic)) =
1611             bitVectSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
1612           setUsesDefs (IC_RIGHT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1613
1614         }
1615
1616       /* for the result it is special case, put the result */
1617       /* in the defuseSet if it a pointer or array access  */
1618       if (POINTER_SET (defic))
1619         {
1620           OP_USES (IC_RESULT (ic)) =
1621             bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
1622           setUsesDefs (IC_RESULT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1623           deleteItemIf (&cseSet, ifPointerGet, IC_RESULT (ic));
1624           ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_RESULT (ic)->key);
1625           /* delete from inexpressions of all successors which
1626              have dfNum > than this block */
1627           for (i = 0; i < count; ebbs[i++]->visited = 0);
1628           applyToSet (ebb->succList, delGetPointerSucc, IC_RESULT (ic), ebb->dfnum);
1629
1630           /* delete from cseSet all other pointer sets
1631              for this operand */
1632           deleteItemIf (&ptrSetSet, ifPointerSet, IC_RESULT (ic));
1633           /* add to the local pointerset set */
1634           addSetHead (&ptrSetSet, newCseDef (IC_RESULT (ic), ic));
1635         }
1636       else
1637         /* add the result to defintion set */ if (IC_RESULT (ic))
1638         {
1639           OP_DEFS (IC_RESULT (ic)) =
1640             bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
1641           ebb->defSet = bitVectSetBit (ebb->defSet, ic->key);
1642           ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic)));
1643           ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key);
1644         }
1645
1646
1647       /* if this is an addressof instruction then */
1648       /* put the symbol in the address of list &  */
1649       /* delete it from the cseSet                */
1650       if (defic->op == ADDRESS_OF)
1651         {
1652           addSetHead (&ebb->addrOf, IC_LEFT (ic));
1653           deleteItemIf (&cseSet, ifDefSymIsX, IC_LEFT (ic));
1654         }
1655     }
1656
1657   setToNull ((void **) &ebb->outExprs);
1658   ebb->outExprs = cseSet;
1659   ebb->outDefs = bitVectUnion (ebb->outDefs, ebb->defSet);
1660   ebb->ptrsSet = bitVectUnion (ebb->ptrsSet, ebb->inPtrsSet);
1661   return change;
1662 }
1663
1664 /*-----------------------------------------------------------------*/
1665 /* cseAllBlocks - will sequentially go thru & do cse for all blocks */
1666 /*-----------------------------------------------------------------*/
1667 int 
1668 cseAllBlocks (eBBlock ** ebbs, int count)
1669 {
1670   int i;
1671   int change = 0;
1672
1673   /* if optimization turned off */
1674
1675   for (i = 0; i < count; i++)
1676     change += cseBBlock (ebbs[i], FALSE, ebbs, count);
1677
1678   return change;
1679 }