1 /*-------------------------------------------------------------------------
2 SDCCcse.c - source file for Common Subexpressions and other utility
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
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
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.
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.
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 -------------------------------------------------------------------------*/
28 /*-----------------------------------------------------------------*/
29 /* newCseDef - new cseDef */
30 /*-----------------------------------------------------------------*/
32 newCseDef (operand * sym, iCode * ic)
37 cdp = Safe_alloc (sizeof (cseDef));
48 /*-----------------------------------------------------------------*/
49 /* int isCseDefEqual - two definitions are equal */
50 /*-----------------------------------------------------------------*/
52 isCseDefEqual (void *vsrc, void *vdest)
60 return (src->key == dest->key &&
61 src->diCode == dest->diCode);
65 /*-----------------------------------------------------------------*/
66 /* pcseDef - in the cseDef */
67 /*-----------------------------------------------------------------*/
69 pcseDef (void *item, va_list ap)
77 fprintf (stdout, "**null op**");
78 printOperand (cdp->sym, stdout);
79 icTab = getTableEntry (cdp->diCode->op);
80 icTab->iCodePrint (stdout, cdp->diCode, icTab->printName);
84 void ReplaceOpWithCheaperOp(operand **op, operand *cop) {
86 printf ("ReplaceOpWithCheaperOp %s with %s: ",
87 IS_SYMOP((*op)) ? OP_SYMBOL((*op))->name : "!SYM",
88 IS_SYMOP(cop) ? OP_SYMBOL(cop)->name : "!SYM");
89 // if op is a register equivalent
90 if (IS_ITEMP(cop) && OP_SYMBOL((*op))->isreqv) {
91 operand **rop = &OP_SYMBOL((*op))->usl.spillLoc->reqv;
92 if (isOperandEqual(*rop, *op)) {
95 OP_SYMBOL((*op))->isreqv=0;
96 OP_SYMBOL(cop)->isreqv=1;
106 /*-----------------------------------------------------------------*/
107 /* replaceAllSymBySym - replaces all operands by operand in an */
108 /* instruction chain */
109 /*-----------------------------------------------------------------*/
111 replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
115 for (lic = ic; lic; lic = lic->next)
119 /* do the special cases first */
123 IC_COND (lic)->key == from->key)
126 bitVectUnSetBit (OP_USES (from), lic->key);
127 OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
128 siaddr = IC_COND (lic)->isaddr;
129 IC_COND (lic) = operandFromOperand (to);
130 IC_COND (lic)->isaddr = siaddr;
136 if (lic->op == JUMPTABLE)
139 IC_JTCOND (lic)->key == from->key)
142 bitVectUnSetBit (OP_USES (from), lic->key);
143 OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
144 siaddr = IC_COND (lic)->isaddr;
145 IC_JTCOND (lic) = operandFromOperand (to);
146 IC_JTCOND (lic)->isaddr = siaddr;
153 IC_RESULT (lic) && IC_RESULT (lic)->key == from->key)
155 /* maintain du chains */
156 if (POINTER_SET (lic))
158 bitVectUnSetBit (OP_USES (from), lic->key);
159 OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
161 /* also check if the "from" was in the non-dominating
162 pointer sets and replace it with "to" in the bitVector */
163 if (bitVectBitValue (*ndpset, from->key))
165 bitVectUnSetBit (*ndpset, from->key);
166 bitVectSetBit (*ndpset, to->key);
172 bitVectUnSetBit (OP_DEFS (from), lic->key);
173 OP_DEFS(to)=bitVectSetBit (OP_DEFS (to), lic->key);
175 siaddr = IC_RESULT (lic)->isaddr;
176 IC_RESULT (lic) = operandFromOperand (to);
177 IC_RESULT (lic)->isaddr = siaddr;
181 IC_RIGHT (lic) && IC_RIGHT (lic)->key == from->key)
183 bitVectUnSetBit (OP_USES (from), lic->key);
184 OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
185 siaddr = IC_RIGHT (lic)->isaddr;
186 IC_RIGHT (lic) = operandFromOperand (to);
187 IC_RIGHT (lic)->isaddr = siaddr;
191 IC_LEFT (lic) && IC_LEFT (lic)->key == from->key)
193 bitVectUnSetBit (OP_USES (from), lic->key);
194 OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
195 siaddr = IC_LEFT (lic)->isaddr;
196 IC_LEFT (lic) = operandFromOperand (to);
197 IC_LEFT (lic)->isaddr = siaddr;
202 /*-----------------------------------------------------------------*/
203 /* iCodeKeyIs - if the icode keys match then return 1 */
204 /*-----------------------------------------------------------------*/
205 DEFSETFUNC (iCodeKeyIs)
210 if (cdp->diCode->key == key)
216 /*-----------------------------------------------------------------*/
217 /* removeFromInExprs - removes an icode from inexpressions */
218 /*-----------------------------------------------------------------*/
219 DEFSETFUNC (removeFromInExprs)
223 V_ARG (operand *, from);
224 V_ARG (operand *, to);
225 V_ARG (eBBlock *, cbp);
231 deleteItemIf (&ebp->inExprs, iCodeKeyIs, ic->key);
232 if (ebp != cbp && !bitVectBitValue (cbp->domVect, ebp->bbnum))
233 replaceAllSymBySym (ebp->sch, from, to, &ebp->ndompset);
235 applyToSet (ebp->succList, removeFromInExprs, ic, from, to, cbp);
239 /*-----------------------------------------------------------------*/
240 /* isGlobalInNearSpace - return TRUE if valriable is a globalin data */
241 /*-----------------------------------------------------------------*/
243 isGlobalInNearSpace (operand * op)
245 sym_link *type = getSpec (operandType (op));
246 /* this is 8051 specific: optimization
247 suggested by Jean-Louis VERN, with 8051s we have no
248 advantage of putting variables in near space into
250 if (isOperandGlobal (op) && !IN_FARSPACE (SPEC_OCLS (type)) &&
251 IN_DIRSPACE (SPEC_OCLS (type)))
257 /*-----------------------------------------------------------------*/
258 /* findCheaperOp - cseBBlock support routine, will check to see if */
259 /* we have a operand previously defined */
260 /*-----------------------------------------------------------------*/
261 DEFSETFUNC (findCheaperOp)
264 V_ARG (operand *, cop);
265 V_ARG (operand **, opp);
266 V_ARG (int, checkSign);
268 /* if we have already found it */
272 /* not found it yet check if this is the one */
273 /* and this is not the defining one */
274 if (cop->key == cdp->key)
277 /* do a special check this will help in */
278 /* constant propagation & dead code elim */
279 /* for assignments only */
280 if (cdp->diCode->op == '=') {
281 /* if the result is volatile then return result */
282 if (IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
283 *opp = IC_RESULT (cdp->diCode);
285 /* if this is a straight assignment and
286 left is a temp then prefer the temporary to the
288 if (!POINTER_SET (cdp->diCode) &&
289 IS_ITEMP (IC_RESULT (cdp->diCode)) &&
290 IS_TRUE_SYMOP (IC_RIGHT (cdp->diCode)))
291 *opp = IC_RESULT (cdp->diCode);
293 /* if straight assignement && and both
294 are temps then prefer the one that
295 will not need extra space to spil, also
296 take into consideration if right side
297 an induction variable
299 if (!POINTER_SET (cdp->diCode) &&
300 IS_ITEMP (IC_RESULT (cdp->diCode)) &&
301 IS_ITEMP (IC_RIGHT (cdp->diCode)) &&
302 !OP_SYMBOL (IC_RIGHT (cdp->diCode))->isind &&
303 !OP_SYMBOL(IC_RIGHT (cdp->diCode))->isreqv &&
304 ((!SPIL_LOC (IC_RIGHT (cdp->diCode)) &&
305 SPIL_LOC (IC_RESULT (cdp->diCode))) ||
306 (SPIL_LOC (IC_RESULT (cdp->diCode)) &&
307 SPIL_LOC (IC_RESULT (cdp->diCode)) ==
308 SPIL_LOC (IC_RIGHT (cdp->diCode)))))
309 *opp = IC_RESULT (cdp->diCode);
311 *opp = IC_RIGHT (cdp->diCode);
315 *opp = IC_RESULT (cdp->diCode);
318 /* if this is an assign to a temp. then check
319 if the right side is this then return this */
320 if (IS_TRUE_SYMOP (cop) &&
321 cdp->diCode->op == '=' &&
322 !POINTER_SET (cdp->diCode) &&
323 cop->key == IC_RIGHT (cdp->diCode)->key &&
324 !isGlobalInNearSpace (IC_RIGHT (cdp->diCode)) &&
325 IS_ITEMP (IC_RESULT (cdp->diCode)))
326 *opp = IC_RESULT (cdp->diCode);
329 (isOperandLiteral(*opp) || !checkSign ||
331 IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) &&
332 (SPEC_USIGN(operandType (cop))==SPEC_USIGN(operandType (*opp)) &&
333 (SPEC_LONG(operandType (cop))==SPEC_LONG(operandType (*opp)))))))
336 if ((isGlobalInNearSpace (cop) &&
337 !isOperandLiteral (*opp)) ||
338 isOperandVolatile (*opp, FALSE)
345 if (cop->key == (*opp)->key)
351 if ((*opp)->isaddr != cop->isaddr && IS_ITEMP (cop))
353 *opp = operandFromOperand (*opp);
354 (*opp)->isaddr = cop->isaddr;
357 if (IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) &&
358 SPEC_NOUN(operandType(cop)) != SPEC_NOUN(operandType(*opp)))
360 // special case: we can make an unsigned char literal
361 // into an int literal with no cost.
362 if (isOperandLiteral(*opp)
363 && SPEC_NOUN(operandType(*opp)) == V_CHAR
364 && SPEC_NOUN(operandType(cop)) == V_INT)
366 *opp = operandFromOperand (*opp);
367 SPEC_NOUN(operandType(*opp)) = V_INT;
385 /*-----------------------------------------------------------------*/
386 /* findPointerSet - finds the right side of a pointer set op */
387 /*-----------------------------------------------------------------*/
388 DEFSETFUNC (findPointerSet)
391 V_ARG (operand *, op);
392 V_ARG (operand **, opp);
393 V_ARG (operand *, rop);
395 if (POINTER_SET (cdp->diCode) &&
396 IC_RESULT (cdp->diCode)->key == op->key &&
397 !isOperandVolatile (IC_RESULT (cdp->diCode), TRUE) &&
398 !isOperandVolatile (IC_RIGHT (cdp->diCode), TRUE) &&
399 getSize (operandType (IC_RIGHT (cdp->diCode))) ==
400 getSize (operandType (rop)))
402 *opp = IC_RIGHT (cdp->diCode);
409 /*-----------------------------------------------------------------*/
410 /* findPrevIc - cseBBlock support function will return the iCode */
411 /* which matches the current one */
412 /*-----------------------------------------------------------------*/
413 DEFSETFUNC (findPrevIc)
417 V_ARG (iCode **, icp);
419 /* if already found */
423 /* if the iCodes are the same */
424 if (isiCodeEqual (ic, cdp->diCode) &&
425 isOperandEqual (cdp->sym, IC_RESULT (cdp->diCode)))
431 /* if iCodes are not the same */
432 /* see the operands maybe interchanged */
433 if (ic->op == cdp->diCode->op &&
434 (ic->op == '+' || ic->op == '*') &&
435 isOperandEqual (IC_LEFT (ic), IC_RIGHT (cdp->diCode)) &&
436 isOperandEqual (IC_RIGHT (ic), IC_LEFT (cdp->diCode)))
445 /*-------------------------------------------------------------------*/
446 /* ifAssignedFromGlobal - if definition is an assignment from global */
447 /*-------------------------------------------------------------------*/
448 DEFSETFUNC (ifAssignedFromGlobal)
451 iCode *dic=cdp->diCode;
453 if (dic->op=='=' && isOperandGlobal(IC_RIGHT(dic))) {
459 /*-----------------------------------------------------------------*/
460 /* ifDefGlobal - if definition is global */
461 /*-----------------------------------------------------------------*/
462 DEFSETFUNC (ifDefGlobal)
466 return (isOperandGlobal (cdp->sym));
469 /*-----------------------------------------------------------------*/
470 /* ifAnyGetPointer - if get pointer icode */
471 /*-----------------------------------------------------------------*/
472 DEFSETFUNC (ifAnyGetPointer)
476 if (cdp->diCode && POINTER_GET (cdp->diCode))
481 /*-----------------------------------------------------------------*/
482 /* ifOperandsHave - if any of the operand are the same as this */
483 /*-----------------------------------------------------------------*/
484 DEFSETFUNC (ifOperandsHave)
487 V_ARG (operand *, op);
490 if (IC_LEFT (cdp->diCode) &&
491 IS_SYMOP (IC_LEFT (cdp->diCode)) &&
492 IC_LEFT (cdp->diCode)->key == op->key)
495 if (IC_RIGHT (cdp->diCode) &&
496 IS_SYMOP (IC_RIGHT (cdp->diCode)) &&
497 IC_RIGHT (cdp->diCode)->key == op->key)
500 /* or if any of the operands are volatile */
501 if (IC_LEFT (cdp->diCode) &&
502 IS_OP_VOLATILE (IC_LEFT (cdp->diCode)))
505 if (IC_RIGHT (cdp->diCode) &&
506 IS_OP_VOLATILE (IC_RIGHT (cdp->diCode)))
510 if (IC_RESULT (cdp->diCode) &&
511 IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
517 /*-----------------------------------------------------------------*/
518 /* ifDefSymIs - if a definition is found in the set */
519 /*-----------------------------------------------------------------*/
521 ifDefSymIs (set * cseSet, operand * sym)
526 if (!sym || !IS_SYMOP (sym))
528 for (sl = cseSet; sl; sl = sl->next)
531 if (loop->sym->key == sym->key)
538 /*-----------------------------------------------------------------*/
539 /* ifDefSymIsX - will return 1 if the symbols match */
540 /*-----------------------------------------------------------------*/
541 DEFSETFUNC (ifDefSymIsX)
544 V_ARG (operand *, op);
547 return cdp->sym->key == op->key;
549 return (isOperandEqual (cdp->sym, op));
554 /*-----------------------------------------------------------------*/
555 /* ifDiCodeIs - returns truw if diCode is same */
556 /*-----------------------------------------------------------------*/
558 ifDiCodeIs (set * cseSet, iCode * ic)
566 for (sl = cseSet; sl; sl = sl->next)
569 if (loop->diCode == ic)
576 /*-----------------------------------------------------------------*/
577 /* ifPointerGet - returns true if the icode is pointer get sym */
578 /*-----------------------------------------------------------------*/
579 DEFSETFUNC (ifPointerGet)
582 V_ARG (operand *, op);
583 iCode *dic = cdp->diCode;
584 operand *left = IC_LEFT (cdp->diCode);
586 if (POINTER_GET (dic) && left->key == op->key)
592 /*-----------------------------------------------------------------*/
593 /* ifPointerSet - returns true if the icode is pointer set sym */
594 /*-----------------------------------------------------------------*/
595 DEFSETFUNC (ifPointerSet)
598 V_ARG (operand *, op);
600 if (POINTER_SET (cdp->diCode) &&
601 IC_RESULT (cdp->diCode)->key == op->key)
607 /*-----------------------------------------------------------------*/
608 /* ifDiCodeIsX - will return 1 if the symbols match */
609 /*-----------------------------------------------------------------*/
610 DEFSETFUNC (ifDiCodeIsX)
615 return cdp->diCode == ic;
619 /*-----------------------------------------------------------------*/
620 /* findBackwardDef - scan backwards to find deinition of operand */
621 /*-----------------------------------------------------------------*/
622 iCode *findBackwardDef(operand *op,iCode *ic)
626 for (lic = ic; lic ; lic = lic->prev) {
627 if (IC_RESULT(lic) && isOperandEqual(op,IC_RESULT(lic)))
633 /*-----------------------------------------------------------------*/
634 /* algebraicOpts - does some algebraic optimizations */
635 /*-----------------------------------------------------------------*/
637 algebraicOpts (iCode * ic, eBBlock * ebp)
639 /* we don't deal with the following iCodes
650 /* if both operands present & ! IFX */
651 /* then if they are both literal we */
652 /* perform the operation right now */
653 if (IC_RESULT (ic) &&
656 IS_OP_LITERAL (IC_LEFT (ic)) &&
657 IS_OP_LITERAL (IC_RIGHT (ic)))
660 IC_RIGHT (ic) = operandOperation (IC_LEFT (ic),
663 operandType (IC_RESULT (ic)));
666 SET_RESULT_RIGHT (ic);
670 /* if not ifx & only one operand present */
671 if (IC_RESULT (ic) &&
673 IS_OP_LITERAL (IC_LEFT (ic)) &&
677 IC_RIGHT (ic) = operandOperation (IC_LEFT (ic),
680 operandType (IC_RESULT (ic)));
683 SET_RESULT_RIGHT (ic);
688 /* a special case : or in short a kludgy solution will think
689 about a better solution over a glass of wine someday */
690 if (ic->op == GET_VALUE_AT_ADDRESS)
693 if (IS_ITEMP (IC_RESULT (ic)) &&
694 IS_TRUE_SYMOP (IC_LEFT (ic)))
698 IC_RIGHT (ic) = operandFromOperand (IC_LEFT (ic));
699 IC_RIGHT (ic)->isaddr = 0;
701 IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
702 IC_RESULT (ic)->isaddr = 0;
703 setOperandType (IC_RESULT (ic), operandType (IC_RIGHT (ic)));
707 if (IS_ITEMP (IC_LEFT (ic)) &&
708 IS_ITEMP (IC_RESULT (ic)) &&
709 /* !OP_SYMBOL(IC_RESULT(ic))->isreqv && */
710 /* !OP_SYMBOL(IC_LEFT(ic))->isreqv && */
711 !IC_LEFT (ic)->isaddr)
714 IC_RIGHT (ic) = operandFromOperand (IC_LEFT (ic));
715 IC_RIGHT (ic)->isaddr = 0;
716 IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
717 IC_RESULT (ic)->isaddr = 0;
725 /* depending on the operation */
729 /* if adding the same thing change to left shift by 1 */
730 if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key &&
731 !IS_FLOAT (operandType (IC_RESULT (ic))))
734 IC_RIGHT (ic) = operandFromLit (1);
737 /* if addition then check if one of them is a zero */
738 /* if yes turn it into assignmnt */
739 if (IS_OP_LITERAL (IC_LEFT (ic)) &&
740 operandLitValue (IC_LEFT (ic)) == 0.0)
745 SET_ISADDR (IC_RESULT (ic), 0);
746 SET_ISADDR (IC_RIGHT (ic), 0);
749 if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
750 operandLitValue (IC_RIGHT (ic)) == 0.0)
754 IC_RIGHT (ic) = IC_LEFT (ic);
756 SET_ISADDR (IC_RIGHT (ic), 0);
757 SET_ISADDR (IC_RESULT (ic), 0);
762 /* if subtracting the the same thing then zero */
763 if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key)
766 IC_RIGHT (ic) = operandFromLit (0);
768 IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
769 IC_RESULT (ic)->isaddr = 0;
773 /* if subtraction then check if one of the operand */
774 /* is zero then depending on which operand change */
775 /* to assignment or unary minus */
776 if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
777 operandLitValue (IC_RIGHT (ic)) == 0.0)
779 /* right size zero change to assignment */
781 IC_RIGHT (ic) = IC_LEFT (ic);
783 SET_ISADDR (IC_RIGHT (ic), 0);
784 SET_ISADDR (IC_RESULT (ic), 0);
787 if (IS_OP_LITERAL (IC_LEFT (ic)) &&
788 operandLitValue (IC_LEFT (ic)) == 0.0)
790 /* left zero turn into an unary minus */
792 IC_LEFT (ic) = IC_RIGHT (ic);
793 IC_RIGHT (ic) = NULL;
797 /* if multiplication then check if either of */
798 /* them is zero then the result is zero */
799 /* if either of them is one then result is */
802 if (IS_OP_LITERAL (IC_LEFT (ic)))
805 if (operandLitValue (IC_LEFT (ic)) == 0.0)
808 IC_RIGHT (ic) = IC_LEFT (ic);
810 SET_RESULT_RIGHT (ic);
813 if (operandLitValue (IC_LEFT (ic)) == 1.0)
815 /* '*' can have two unsigned chars as operands */
816 /* and an unsigned int as result. */
817 if (compareType (operandType (IC_RESULT (ic)),
818 operandType (IC_RIGHT (ic))) == 1)
822 SET_RESULT_RIGHT (ic);
827 IC_LEFT (ic)->type = TYPE;
828 setOperandType (IC_LEFT (ic), operandType (IC_RESULT (ic)));
834 if (IS_OP_LITERAL (IC_RIGHT (ic)))
837 if (operandLitValue (IC_RIGHT (ic)) == 0.0)
841 SET_RESULT_RIGHT (ic);
845 if (operandLitValue (IC_RIGHT (ic)) == 1.0)
847 /* '*' can have two unsigned chars as operands */
848 /* and an unsigned int as result. */
849 if (compareType (operandType (IC_RESULT (ic)),
850 operandType (IC_LEFT (ic))) == 1)
853 IC_RIGHT (ic) = IC_LEFT (ic);
855 SET_RESULT_RIGHT (ic);
863 IC_RIGHT (ic) = IC_LEFT (ic);
865 IC_LEFT (ic)->type = TYPE;
866 setOperandType (IC_LEFT (ic), operandType (IC_RESULT (ic)));
873 /* if division by self then 1 */
874 if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key)
877 IC_RIGHT (ic) = operandFromLit (1);
879 IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
880 IC_RESULT (ic)->isaddr = 0;
883 /* if this is a division then check if right */
884 /* is one then change it to an assignment */
885 if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
886 operandLitValue (IC_RIGHT (ic)) == 1.0)
890 IC_RIGHT (ic) = IC_LEFT (ic);
892 SET_RESULT_RIGHT (ic);
896 /* if both are the same for an comparison operators */
900 if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
903 IC_RIGHT (ic) = operandFromLit (1);
905 SET_RESULT_RIGHT (ic);
911 if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
914 IC_RIGHT (ic) = operandFromLit (0);
916 SET_RESULT_RIGHT (ic);
921 sym_link *otype = operandType(IC_RIGHT(ic));
922 sym_link *ctype = operandType(IC_LEFT(ic));
923 /* if this is a cast of a literal value */
924 if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
925 !(IS_GENPTR(ctype) && (IS_PTR(otype) && !IS_GENPTR(otype)))) {
928 operandFromValue (valCastLiteral (operandType (IC_LEFT (ic)),
929 operandLitValue (IC_RIGHT (ic))));
931 SET_ISADDR (IC_RESULT (ic), 0);
933 /* if casting to the same */
934 if (compareType (operandType (IC_RESULT (ic)),
935 operandType (IC_RIGHT (ic))) == 1) {
938 SET_ISADDR (IC_RESULT (ic), 0);
943 if (IS_OP_LITERAL (IC_LEFT (ic)))
947 (operandLitValue (IC_LEFT (ic)) == 0 ?
948 operandFromLit (1) : operandFromLit (0));
950 SET_ISADDR (IC_RESULT (ic), 0);
954 /* if both operands are equal */
955 /* if yes turn it into assignment */
956 if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
958 if (IS_OP_VOLATILE (IC_LEFT (ic)))
960 iCode *newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
961 IC_RESULT (newic) = IC_LEFT (ic);
962 newic->lineno = ic->lineno;
963 addiCodeToeBBlock (ebp, newic, ic->next);
967 SET_RESULT_RIGHT (ic);
970 /* swap literal to right ic */
971 if (IS_OP_LITERAL (IC_LEFT (ic)))
976 IC_LEFT (ic) = IC_RIGHT (ic);
979 if (IS_OP_LITERAL (IC_RIGHT (ic)))
981 /* if BITWISEAND then check if one of them is a zero */
982 /* if yes turn it into 0 assignment */
983 if (operandLitValue (IC_RIGHT (ic)) == 0.0)
985 if (IS_OP_VOLATILE (IC_LEFT (ic)))
987 iCode *newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
988 IC_RESULT (newic) = IC_LEFT (ic);
989 newic->lineno = ic->lineno;
990 addiCodeToeBBlock (ebp, newic, ic->next);
994 SET_RESULT_RIGHT (ic);
997 /* if BITWISEAND then check if one of them is 0xff... */
998 /* if yes turn it into assignment */
1002 switch (getSize (operandType (IC_RIGHT (ic))))
1016 if (((unsigned) operandLitValue (IC_RIGHT (ic)) & val) == val)
1019 IC_RIGHT (ic) = IC_LEFT (ic);
1020 IC_LEFT (ic) = NULL;
1021 SET_RESULT_RIGHT (ic);
1028 /* if both operands are equal */
1029 /* if yes turn it into assignment */
1030 if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
1032 if (IS_OP_VOLATILE (IC_LEFT (ic)))
1034 iCode *newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
1035 IC_RESULT (newic) = IC_LEFT (ic);
1036 newic->lineno = ic->lineno;
1037 addiCodeToeBBlock (ebp, newic, ic->next);
1040 IC_LEFT (ic) = NULL;
1041 SET_RESULT_RIGHT (ic);
1044 /* swap literal to right ic */
1045 if (IS_OP_LITERAL (IC_LEFT (ic)))
1050 IC_LEFT (ic) = IC_RIGHT (ic);
1053 if (IS_OP_LITERAL (IC_RIGHT (ic)))
1055 /* if BITWISEOR then check if one of them is a zero */
1056 /* if yes turn it into assignment */
1057 if (operandLitValue (IC_RIGHT (ic)) == 0.0)
1060 IC_RIGHT (ic) = IC_LEFT (ic);
1061 IC_LEFT (ic) = NULL;
1062 SET_RESULT_RIGHT (ic);
1065 /* if BITWISEOR then check if one of them is 0xff... */
1066 /* if yes turn it into 0xff... assignment */
1070 switch (getSize (operandType (IC_RIGHT (ic))))
1084 if (((unsigned) operandLitValue (IC_RIGHT (ic)) & val) == val)
1086 if (IS_OP_VOLATILE (IC_LEFT (ic)))
1088 iCode *newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
1089 IC_RESULT (newic) = IC_LEFT (ic);
1090 newic->lineno = ic->lineno;
1091 addiCodeToeBBlock (ebp, newic, ic->next);
1094 IC_LEFT (ic) = NULL;
1095 SET_RESULT_RIGHT (ic);
1102 /* if both operands are equal */
1103 /* if yes turn it into 0 assignment */
1104 if (isOperandEqual (IC_LEFT (ic), IC_RIGHT (ic)))
1106 if (IS_OP_VOLATILE (IC_LEFT (ic)))
1108 iCode *newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
1109 IC_RESULT (newic) = IC_LEFT (ic);
1110 newic->lineno = ic->lineno;
1111 addiCodeToeBBlock (ebp, newic, ic->next);
1113 newic = newiCode (DUMMY_READ_VOLATILE, NULL, IC_LEFT (ic));
1114 IC_RESULT (newic) = IC_LEFT (ic);
1115 newic->lineno = ic->lineno;
1116 addiCodeToeBBlock (ebp, newic, ic->next);
1119 IC_RIGHT (ic) = operandFromLit (0);
1120 IC_LEFT (ic) = NULL;
1121 SET_RESULT_RIGHT (ic);
1124 /* swap literal to right ic */
1125 if (IS_OP_LITERAL (IC_LEFT (ic)))
1130 IC_LEFT (ic) = IC_RIGHT (ic);
1133 /* if XOR then check if one of them is a zero */
1134 /* if yes turn it into assignment */
1135 if (IS_OP_LITERAL (IC_RIGHT (ic)))
1137 if (operandLitValue (IC_RIGHT (ic)) == 0.0)
1140 IC_RIGHT (ic) = IC_LEFT (ic);
1141 IC_LEFT (ic) = NULL;
1142 SET_RESULT_RIGHT (ic);
1151 #define OTHERS_PARM(s) (s->_isparm && !s->ismyparm)
1152 /*-----------------------------------------------------------------*/
1153 /* updateSpillLocation - keeps track of register spill location */
1154 /*-----------------------------------------------------------------*/
1156 updateSpillLocation (iCode * ic, int induction)
1160 if (POINTER_SET (ic))
1166 /* for the form true_symbol := iTempNN */
1167 if (ASSIGN_ITEMP_TO_SYM (ic) &&
1168 !SPIL_LOC (IC_RIGHT (ic))) {
1170 setype = getSpec (operandType (IC_RESULT (ic)));
1172 if (!OP_SYMBOL(IC_RIGHT (ic))->noSpilLoc &&
1173 !IS_VOLATILE (setype) &&
1174 !IN_FARSPACE (SPEC_OCLS (setype)) &&
1175 !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
1177 wassert(IS_SYMOP(IC_RESULT (ic)));
1178 wassert(IS_SYMOP(IC_RIGHT (ic)));
1179 SPIL_LOC (IC_RIGHT (ic)) =
1180 IC_RESULT (ic)->operand.symOperand;
1185 #if 0 /* this needs furthur investigation can save a lot of code */
1186 if (ASSIGN_SYM_TO_ITEMP(ic) &&
1187 !SPIL_LOC(IC_RESULT(ic))) {
1188 if (!OTHERS_PARM (OP_SYMBOL (IC_RIGHT (ic))))
1189 SPIL_LOC (IC_RESULT (ic)) =
1190 IC_RIGHT (ic)->operand.symOperand;
1193 if (ASSIGN_ITEMP_TO_ITEMP (ic)) {
1195 if (!SPIL_LOC (IC_RIGHT (ic)) &&
1196 !bitVectBitsInCommon (OP_DEFS (IC_RIGHT (ic)), OP_USES (IC_RESULT (ic))) &&
1197 OP_SYMBOL (IC_RESULT (ic))->isreqv) {
1199 setype = getSpec (operandType (IC_RESULT (ic)));
1201 if (!OP_SYMBOL(IC_RIGHT (ic))->noSpilLoc &&
1202 !IS_VOLATILE (setype) &&
1203 !IN_FARSPACE (SPEC_OCLS (setype)) &&
1204 !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
1206 SPIL_LOC (IC_RIGHT (ic)) =
1207 SPIL_LOC (IC_RESULT (ic));
1209 /* special case for inductions */
1211 OP_SYMBOL(IC_RIGHT(ic))->isreqv &&
1212 !OP_SYMBOL(IC_RESULT (ic))->noSpilLoc &&
1213 !SPIL_LOC(IC_RESULT(ic))) {
1214 SPIL_LOC (IC_RESULT (ic)) = SPIL_LOC (IC_RIGHT (ic));
1218 /*-----------------------------------------------------------------*/
1219 /* setUsesDef - sets the uses def bitvector for a given operand */
1220 /*-----------------------------------------------------------------*/
1222 setUsesDefs (operand * op, bitVect * bdefs,
1223 bitVect * idefs, bitVect ** oud)
1225 /* compute the definitions alive at this point */
1226 bitVect *adefs = bitVectUnion (bdefs, idefs);
1228 /* of these definitions find the ones that are */
1229 /* for this operand */
1230 adefs = bitVectIntersect (adefs, OP_DEFS (op));
1232 /* these are the definitions that this operand can use */
1233 op->usesDefs = adefs;
1235 /* the out defs is an union */
1236 *oud = bitVectUnion (*oud, adefs);
1239 /*-----------------------------------------------------------------*/
1240 /* unsetDefsAndUses - clear this operation for the operands */
1241 /*-----------------------------------------------------------------*/
1243 unsetDefsAndUses (iCode * ic)
1245 if (ic->op == JUMPTABLE)
1248 /* take away this definition from the def chain of the */
1249 /* result & take away from use set of the operands */
1252 /* turn off def set */
1253 if (IS_SYMOP (IC_RESULT (ic)))
1255 if (!POINTER_SET (ic))
1256 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
1258 bitVectUnSetBit (OP_USES (IC_RESULT (ic)), ic->key);
1260 /* turn off the useSet for the operands */
1261 if (IS_SYMOP (IC_LEFT (ic)))
1262 bitVectUnSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1264 if (IS_SYMOP (IC_RIGHT (ic)))
1265 bitVectUnSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
1268 /* must be ifx turn off the use */ if (IS_SYMOP (IC_COND (ic)))
1269 bitVectUnSetBit (OP_USES (IC_COND (ic)), ic->key);
1272 /*-----------------------------------------------------------------*/
1273 /* ifxOptimize - changes ifx conditions if it can */
1274 /*-----------------------------------------------------------------*/
1276 ifxOptimize (iCode * ic, set * cseSet,
1278 eBBlock * ebb, int *change,
1279 eBBlock ** ebbs, int count)
1284 /* if the condition can be replaced */
1288 applyToSetFTrue (cseSet, findCheaperOp, IC_COND (ic), &pdop, 0);
1291 ReplaceOpWithCheaperOp(&IC_COND (ic), pdop);
1296 /* if the conditional is a literal then */
1297 if (IS_OP_LITERAL (IC_COND (ic)))
1300 if ((operandLitValue (IC_COND (ic)) != 0.0) && IC_TRUE (ic))
1303 /* change to a goto */
1305 IC_LABEL (ic) = IC_TRUE (ic);
1312 if (!operandLitValue (IC_COND (ic)) && IC_FALSE (ic))
1315 IC_LABEL (ic) = IC_FALSE (ic);
1321 /* then kill this if condition */
1322 remiCodeFromeBBlock (ebb, ic);
1326 /* now we need to recompute the control flow */
1327 /* since the control flow has changed */
1328 /* this is very expensive but it does not happen */
1329 /* too often, if it does happen then the user pays */
1331 computeControlFlow (ebbs, count, 1);
1332 if (!options.lessPedantic) {
1333 werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
1338 /* if there is only one successor and that successor
1339 is the same one we are conditionally going to then
1340 we can remove this conditional statement */
1341 label = (IC_TRUE (ic) ? IC_TRUE (ic) : IC_FALSE (ic));
1342 if (elementsInSet (ebb->succList) == 1 &&
1343 isinSet (ebb->succList, eBBWithEntryLabel (ebbs, label, count)))
1346 remiCodeFromeBBlock (ebb, ic);
1347 computeControlFlow (ebbs, count, 1);
1348 if (!options.lessPedantic) {
1349 werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
1355 /* if it remains an IFX the update the use Set */
1356 OP_USES(IC_COND (ic))=bitVectSetBit (OP_USES (IC_COND (ic)), ic->key);
1357 setUsesDefs (IC_COND (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1361 /*-----------------------------------------------------------------*/
1362 /* diCodeForSym - finds the definiting instruction for a symbol */
1363 /*-----------------------------------------------------------------*/
1364 DEFSETFUNC (diCodeForSym)
1367 V_ARG (operand *, sym);
1368 V_ARG (iCode **, dic);
1370 /* if already found */
1374 /* if not if this is the defining iCode */
1375 if (sym->key == cdp->key)
1384 /*-----------------------------------------------------------------*/
1385 /* constFold - does some constant folding */
1386 /*-----------------------------------------------------------------*/
1388 constFold (iCode * ic, set * cseSet)
1392 /* this routine will change
1398 /* deal with only + & - */
1399 if (ic->op != '+' &&
1403 /* this check is a hueristic to prevent live ranges
1404 from becoming too long */
1405 if (IS_PTR (operandType (IC_RESULT (ic))))
1408 /* check if operation with a literal */
1409 if (!IS_OP_LITERAL (IC_RIGHT (ic)))
1412 /* check if we can find a definition for the
1414 if (!(applyToSet (cseSet, diCodeForSym, IC_LEFT (ic), &dic)))
1417 /* check that this is also a +/- */
1418 if (dic->op != '+' && dic->op != '-')
1421 /* with a literal */
1422 if (!IS_OP_LITERAL (IC_RIGHT (dic)))
1425 /* find the definition of the left operand
1426 of dic.then check if this defined with a
1427 get_pointer return 0 if the pointer size is
1428 less than 2 (MCS51 specific) */
1429 if (!(applyToSet (cseSet, diCodeForSym, IC_LEFT (dic), &ldic)))
1432 if (POINTER_GET (ldic) && getSize (operandType (IC_LEFT (ldic))) <= 1)
1435 /* it is if the operations are the same */
1436 /* the literal parts need to be added */
1437 IC_LEFT (ic) = operandFromOperand (IC_LEFT (dic));
1438 if (ic->op == dic->op)
1439 IC_RIGHT (ic) = operandFromLit (operandLitValue (IC_RIGHT (ic)) +
1440 operandLitValue (IC_RIGHT (dic)));
1442 IC_RIGHT (ic) = operandFromLit (operandLitValue (IC_RIGHT (ic)) -
1443 operandLitValue (IC_RIGHT (dic)));
1445 if (IS_ITEMP (IC_RESULT (ic)))
1447 SPIL_LOC (IC_RESULT (ic)) = NULL;
1448 OP_SYMBOL(IC_RESULT (ic))->noSpilLoc = 1;
1455 /*-----------------------------------------------------------------*/
1456 /* deleteGetPointers - called when a pointer is passed as parm */
1457 /* will delete from cseSet all get pointers computed from this */
1458 /* pointer. A simple ifOperandsHave is not good enough here */
1459 /*-----------------------------------------------------------------*/
1461 deleteGetPointers (set ** cseSet, set ** pss, operand * op, eBBlock * ebb)
1463 set *compItems = NULL;
1468 if (!*cseSet && !*pss)
1471 /* first find all items computed from this operand .
1472 This done fairly simply go thru the list and find
1473 those that are computed by arthimetic with this
1475 for (cdp = setFirstItem (*cseSet); cdp; cdp = setNextItem (*cseSet))
1477 if (IS_ARITHMETIC_OP (cdp->diCode))
1479 if (isOperandEqual (IC_LEFT (cdp->diCode), op) ||
1480 isOperandEqual (IC_RIGHT (cdp->diCode), op))
1482 /* save it in our list of items */
1483 addSet (&compItems, IC_RESULT (cdp->diCode));
1485 /* also check for those computed from our computed
1486 list . This will take care of situations like
1487 iTemp1 = iTemp0 + 8;
1488 iTemp2 = iTemp1 + 8; */
1489 if (isinSetWith (compItems, (void*)IC_LEFT (cdp->diCode),
1490 (insetwithFunc)isOperandEqual) ||
1491 isinSetWith (compItems, (void*)IC_RIGHT (cdp->diCode),
1492 (insetwithFunc)isOperandEqual))
1494 addSet (&compItems, IC_RESULT (cdp->diCode));
1499 /* now delete all pointer gets with this op */
1500 deleteItemIf (cseSet, ifPointerGet, op);
1501 deleteItemIf (pss, ifPointerSet, op);
1503 /* set the bit vector used by dataFlow computation later */
1504 ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, op->key);
1505 /* now for the computed items */
1506 for (cop = setFirstItem (compItems); cop; cop = setNextItem (compItems))
1508 ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, cop->key);
1509 deleteItemIf (cseSet, ifPointerGet, cop);
1510 deleteItemIf (pss, ifPointerSet, cop);
1514 /*-----------------------------------------------------------------*/
1515 /* delGetPointerSucc - delete get pointer from inExprs of succ with */
1516 /* dfnum > supplied */
1517 /*-----------------------------------------------------------------*/
1518 DEFSETFUNC (delGetPointerSucc)
1520 eBBlock *ebp = item;
1521 V_ARG (operand *, op);
1528 if (ebp->dfnum > dfnum)
1530 deleteItemIf (&ebp->inExprs, ifPointerGet, op);
1533 return applyToSet (ebp->succList, delGetPointerSucc, op, dfnum);
1536 /*-----------------------------------------------------------------*/
1537 /* fixUpTypes - KLUGE HACK fixup a lowering problem */
1538 /*-----------------------------------------------------------------*/
1540 fixUpTypes (iCode * ic)
1542 sym_link *t1 = operandType (IC_LEFT (ic)), *t2;
1544 /* if (TARGET_IS_DS390) */
1545 if (options.model == MODEL_FLAT24)
1551 /* for pointer_gets if the types of result & left r the
1552 same then change it type of result to next */
1554 compareType (t2 = operandType (IC_RESULT (ic)), t1) == 1)
1556 setOperandType (IC_RESULT (ic), t2->next);
1560 /*-----------------------------------------------------------------*/
1561 /* isSignedOp - will return 1 if sign is important to operation */
1562 /*-----------------------------------------------------------------*/
1563 static int isSignedOp (iCode *ic)
1584 case GET_VALUE_AT_ADDRESS:
1610 /*-----------------------------------------------------------------*/
1611 /* cseBBlock - common subexpression elimination for basic blocks */
1612 /* this is the hackiest kludgiest routine in the whole */
1613 /* system. also the most important, since almost all */
1614 /* data flow related information is computed by it */
1615 /*-----------------------------------------------------------------*/
1617 cseBBlock (eBBlock * ebb, int computeOnly,
1618 eBBlock ** ebbs, int count)
1624 set *ptrSetSet = NULL;
1626 /* if this block is not reachable */
1630 /* set of common subexpressions */
1631 cseSet = setFromSet (ebb->inExprs);
1633 /* these will be computed by this routine */
1634 setToNull ((void **) &ebb->outDefs);
1635 setToNull ((void **) &ebb->defSet);
1636 setToNull ((void **) &ebb->usesDefs);
1637 setToNull ((void **) &ebb->ptrsSet);
1638 setToNull ((void **) &ebb->addrOf);
1639 setToNull ((void **) &ebb->ldefs);
1641 ebb->outDefs = bitVectCopy (ebb->inDefs);
1642 bitVectDefault = iCodeKey;
1643 ebb->defSet = newBitVect (iCodeKey);
1644 ebb->usesDefs = newBitVect (iCodeKey);
1646 /* for all the instructions in this block do */
1647 for (ic = ebb->sch; ic; ic = ic->next)
1654 ic->eBBlockNum = ebb->bbnum;
1659 /* if this is an assignment from true symbol
1660 to a temp then do pointer post inc/dec optimzation */
1661 if (ic->op == '=' && !POINTER_SET (ic) &&
1662 IS_PTR (operandType (IC_RESULT (ic))))
1664 ptrPostIncDecOpt (ic);
1667 /* clear the def & use chains for the operands involved */
1668 /* in this operation . since it can change due to opts */
1669 unsetDefsAndUses (ic);
1671 if (ic->op == PCALL || ic->op == CALL || ic->op == RECEIVE)
1673 /* add to defSet of the symbol */
1674 OP_DEFS(IC_RESULT (ic))=
1675 bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
1676 /* add to the definition set of this block */
1677 ebb->defSet = bitVectSetBit (ebb->defSet, ic->key);
1678 ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key);
1679 ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic)));
1680 setUsesDefs (IC_RESULT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1681 /* delete global variables from the cseSet
1682 since they can be modified by the function call */
1683 deleteItemIf (&cseSet, ifDefGlobal);
1685 /* and also itemps assigned from globals */
1686 deleteItemIf (&cseSet, ifAssignedFromGlobal);
1688 /* delete all getpointer iCodes from cseSet, this should
1689 be done only for global arrays & pointers but at this
1690 point we don't know if globals, so to be safe do all */
1691 deleteItemIf (&cseSet, ifAnyGetPointer);
1694 /* for pcall & ipush we need to add to the useSet */
1695 if ((ic->op == PCALL ||
1699 IS_SYMOP (IC_LEFT (ic)))
1702 /* check if they can be replaced */
1706 applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop, 0);
1708 ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
1710 /* the lookup could have changed it */
1711 if (IS_SYMOP (IC_LEFT (ic)))
1713 OP_USES(IC_LEFT (ic))=
1714 bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1715 setUsesDefs (IC_LEFT (ic), ebb->defSet,
1716 ebb->outDefs, &ebb->usesDefs);
1720 /* if we a sending a pointer as a parameter
1721 then kill all cse since the pointed to item
1722 might be changed in the function being called */
1723 if ((ic->op == IPUSH || ic->op == SEND) &&
1724 IS_PTR (operandType (IC_LEFT (ic))))
1726 deleteGetPointers (&cseSet, &ptrSetSet, IC_LEFT (ic), ebb);
1727 ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_LEFT (ic)->key);
1728 for (i = 0; i < count; ebbs[i++]->visited = 0);
1729 applyToSet (ebb->succList, delGetPointerSucc,
1730 IC_LEFT (ic), ebb->dfnum);
1735 /* if jumptable then mark the usage */
1736 if (ic->op == JUMPTABLE)
1738 OP_USES(IC_JTCOND (ic))=
1739 bitVectSetBit (OP_USES (IC_JTCOND (ic)), ic->key);
1740 setUsesDefs (IC_JTCOND (ic), ebb->defSet,
1741 ebb->outDefs, &ebb->usesDefs);
1749 /* do some algebraic optimizations if possible */
1750 algebraicOpts (ic, ebb);
1751 while (constFold (ic, cseSet));
1755 if (POINTER_GET (ic) && !IS_PTR (operandType (IC_LEFT (ic))))
1757 setOperandType (IC_LEFT (ic),
1758 aggrToPtr (operandType (IC_LEFT (ic)), FALSE));
1762 if (POINTER_SET (ic) && !IS_PTR (operandType (IC_RESULT (ic))))
1764 setOperandType (IC_RESULT (ic),
1765 aggrToPtr (operandType (IC_RESULT (ic)), FALSE));
1768 /* if this is a condition statement then */
1769 /* check if the condition can be replaced */
1772 ifxOptimize (ic, cseSet, computeOnly,
1778 /* if the assignment & result is a temp */
1779 /* see if we can replace it */
1780 if (!computeOnly && ic->op == '=')
1783 /* update the spill location for this */
1784 updateSpillLocation (ic,0);
1786 if (POINTER_SET (ic) &&
1787 !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype)))
1790 applyToSetFTrue (cseSet, findCheaperOp, IC_RESULT (ic), &pdop, 0);
1791 if (pdop && !computeOnly &&
1792 IS_ITEMP (pdop) && IS_PTR(operandType(pdop)))
1793 ReplaceOpWithCheaperOp (&IC_RESULT(ic), pdop);
1797 checkSign = isSignedOp(ic);
1799 /* do the operand lookup i.e. for both the */
1800 /* right & left operand : check the cseSet */
1801 /* to see if they have been replaced if yes */
1802 /* then replace them with those from cseSet */
1804 /* and left is a symbol */
1805 if (IS_SYMOP (IC_LEFT (ic)) &&
1806 !computeOnly && ic->op != ADDRESS_OF)
1810 applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop, checkSign);
1813 if (POINTER_GET (ic))
1815 if (IS_ITEMP (pdop) || IS_OP_LITERAL (pdop))
1817 /* some non dominating block does POINTER_SET with
1818 this variable .. unsafe to remove any POINTER_GETs */
1819 if (bitVectBitValue(ebb->ndompset,IC_LEFT(ic)->key))
1820 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,pdop->key);
1821 ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
1824 /* check if there is a pointer set
1825 for the same pointer visible if yes
1826 then change this into an assignment */
1828 if (applyToSetFTrue (cseSet, findPointerSet, IC_LEFT (ic), &pdop, IC_RESULT (ic)) &&
1829 !bitVectBitValue (ebb->ptrsSet, pdop->key))
1832 IC_LEFT (ic) = NULL;
1833 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), pdop);
1834 SET_ISADDR (IC_RESULT (ic), 0);
1840 ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
1847 if (IS_SYMOP (IC_RIGHT (ic)) && !computeOnly)
1851 applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop, checkSign);
1853 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), pdop);
1858 /* if left or right changed then do algebraic */
1859 if (!computeOnly && change)
1861 algebraicOpts (ic, ebb);
1862 while (constFold (ic, cseSet));
1865 /* if after all this it becomes an assignment to self
1866 then delete it and continue */
1867 if (ASSIGNMENT_TO_SELF (ic))
1869 remiCodeFromeBBlock (ebb, ic);
1873 /* now we will check to see if the entire */
1874 /* operation has been performed before */
1875 /* and is available */
1876 /* don't do assignments they will be killed */
1877 /* by dead code elimination if required do */
1878 /* it only if result is a temporary */
1880 if (!(POINTER_GET (ic) &&
1881 (IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype) ||
1882 isOperandVolatile (IC_LEFT (ic), TRUE) ||
1883 bitVectBitValue (ebb->ndompset, IC_LEFT (ic)->key))) &&
1885 IS_ITEMP (IC_RESULT (ic)) &&
1888 applyToSet (cseSet, findPrevIc, ic, &pdic);
1889 if (pdic && compareType (operandType (IC_RESULT (pdic)),
1890 operandType (IC_RESULT (ic))) != 1)
1892 if (pdic && port->cseOk && (*port->cseOk)(ic,pdic) == 0)
1896 /* Alternate code */
1897 if (pdic && IS_ITEMP(IC_RESULT(ic))) {
1898 if (POINTER_GET(ic) && bitVectBitValue(ebb->ptrsSet,IC_LEFT(ic)->key)) {
1899 /* Mmm, found an equivalent pointer get at a lower level.
1900 This could be a loop however with the same pointer set
1903 /* if previous definition found change this to an assignment */
1906 IC_RIGHT(ic) = operandFromOperand(IC_RESULT(pdic));
1907 SET_ISADDR(IC_RESULT(ic),0);
1908 SET_ISADDR(IC_RIGHT (ic),0);
1912 if (!(POINTER_SET (ic)) && IC_RESULT (ic)) {
1913 deleteItemIf (&cseSet, ifDefSymIsX, IC_RESULT (ic));
1914 addSetHead (&cseSet, newCseDef (IC_RESULT (ic), ic));
1918 /* if assignment to a parameter which is not
1919 mine and type is a pointer then delete
1920 pointerGets to take care of aliasing */
1921 if (ASSIGNMENT (ic) &&
1922 OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))) &&
1923 IS_PTR (operandType (IC_RESULT (ic))))
1925 deleteGetPointers (&cseSet, &ptrSetSet, IC_RIGHT (ic), ebb);
1926 for (i = 0; i < count; ebbs[i++]->visited = 0);
1927 applyToSet (ebb->succList, delGetPointerSucc, IC_RIGHT (ic), ebb->dfnum);
1928 ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_RIGHT (ic)->key);
1931 /* if this is a pointerget then see if we can replace
1932 this with a previously assigned pointer value */
1933 if (POINTER_GET (ic) &&
1934 !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype) ||
1935 isOperandVolatile (IC_LEFT (ic), TRUE)))
1938 applyToSet (ptrSetSet, findPointerSet, IC_LEFT (ic), &pdop, IC_RESULT (ic));
1939 /* if we find it then locally replace all
1940 references to the result with what we assigned */
1943 replaceAllSymBySym (ic->next, IC_RESULT (ic), pdop, &ebb->ndompset);
1947 /* delete from the cseSet anything that has */
1948 /* operands matching the result of this */
1949 /* except in case of pointer access */
1950 if (!(POINTER_SET (ic)) && IC_RESULT (ic))
1952 deleteItemIf (&cseSet, ifOperandsHave, IC_RESULT (ic));
1953 /* delete any previous definitions */
1954 ebb->defSet = bitVectCplAnd (ebb->defSet, OP_DEFS (IC_RESULT (ic)));
1958 /* add the left & right to the defUse set */
1959 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)))
1961 OP_USES(IC_LEFT (ic))=
1962 bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1963 setUsesDefs (IC_LEFT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1967 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)))
1969 OP_USES(IC_RIGHT (ic))=
1970 bitVectSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
1971 setUsesDefs (IC_RIGHT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1975 /* for the result it is special case, put the result */
1976 /* in the defuseSet if it a pointer or array access */
1977 if (POINTER_SET (defic))
1979 OP_USES(IC_RESULT (ic))=
1980 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
1981 setUsesDefs (IC_RESULT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
1982 deleteItemIf (&cseSet, ifPointerGet, IC_RESULT (ic));
1983 ebb->ptrsSet = bitVectSetBit (ebb->ptrsSet, IC_RESULT (ic)->key);
1984 /* delete from inexpressions of all successors which
1985 have dfNum > than this block */
1986 for (i = 0; i < count; ebbs[i++]->visited = 0);
1987 applyToSet (ebb->succList, delGetPointerSucc, IC_RESULT (ic), ebb->dfnum);
1989 /* delete from cseSet all other pointer sets
1991 deleteItemIf (&ptrSetSet, ifPointerSet, IC_RESULT (ic));
1992 /* add to the local pointerset set */
1993 addSetHead (&ptrSetSet, newCseDef (IC_RESULT (ic), ic));
1996 /* add the result to defintion set */ if (IC_RESULT (ic))
1998 OP_DEFS(IC_RESULT (ic))=
1999 bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
2000 ebb->defSet = bitVectSetBit (ebb->defSet, ic->key);
2001 ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic)));
2002 ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key);
2006 /* if this is an addressof instruction then */
2007 /* put the symbol in the address of list & */
2008 /* delete it from the cseSet */
2009 if (defic->op == ADDRESS_OF)
2011 addSetHead (&ebb->addrOf, IC_LEFT (ic));
2012 deleteItemIf (&cseSet, ifDefSymIsX, IC_LEFT (ic));
2016 setToNull ((void **) &ebb->outExprs);
2017 ebb->outExprs = cseSet;
2018 ebb->outDefs = bitVectUnion (ebb->outDefs, ebb->defSet);
2019 ebb->ptrsSet = bitVectUnion (ebb->ptrsSet, ebb->inPtrsSet);
2023 /*-----------------------------------------------------------------*/
2024 /* cseAllBlocks - will sequentially go thru & do cse for all blocks */
2025 /*-----------------------------------------------------------------*/
2027 cseAllBlocks (eBBlock ** ebbs, int count, int computeOnly)
2032 /* if optimization turned off */
2034 for (i = 0; i < count; i++)
2035 change += cseBBlock (ebbs[i], computeOnly, ebbs, count);