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 -------------------------------------------------------------------------*/
27 /*-----------------------------------------------------------------*/
28 /* newCseDef - new cseDef */
29 /*-----------------------------------------------------------------*/
30 cseDef *newCseDef (operand *sym, iCode *ic)
35 ALLOC(cdp,sizeof(cseDef));
46 /*-----------------------------------------------------------------*/
47 /* int isCseDefEqual - two definitions are equal */
48 /*-----------------------------------------------------------------*/
49 int isCseDefEqual ( void *vsrc, void *vdest)
57 return (src->key == dest->key &&
58 src->diCode == dest->diCode ) ;
62 /*-----------------------------------------------------------------*/
63 /* pcseDef - in the cseDef */
64 /*-----------------------------------------------------------------*/
65 int pcseDef (void *item, va_list ap)
73 fprintf(stdout,"**null op**");
74 printOperand(cdp->sym,stdout);
75 icTab = getTableEntry(cdp->diCode->op) ;
76 icTab->iCodePrint(stdout,cdp->diCode,icTab->printName);
80 /*-----------------------------------------------------------------*/
81 /* replaceAllSymBySym - replaces all operands by operand in an */
82 /* instruction chain */
83 /*-----------------------------------------------------------------*/
84 void replaceAllSymBySym (iCode *ic, operand *from , operand *to, bitVect **ndpset)
88 for (lic = ic ; lic ; lic = lic->next ) {
91 /* do the special cases first */
94 IC_COND(lic)->key == from->key) {
96 bitVectUnSetBit (OP_USES(from),lic->key);
97 OP_USES(to) = bitVectSetBit(OP_USES(to),lic->key);
98 siaddr = IC_COND(lic)->isaddr ;
99 IC_COND(lic) = operandFromOperand(to);
100 IC_COND(lic)->isaddr = siaddr ;
106 if (lic->op == JUMPTABLE) {
108 IC_JTCOND(lic)->key == from->key) {
110 bitVectUnSetBit (OP_USES(from),lic->key);
111 OP_USES(to) = bitVectSetBit(OP_USES(to),lic->key);
112 siaddr = IC_COND(lic)->isaddr ;
113 IC_JTCOND(lic) = operandFromOperand(to);
114 IC_JTCOND(lic)->isaddr = siaddr ;
120 if (IC_RESULT(lic) && IC_RESULT(lic)->key == from->key ) {
121 /* maintain du chains */
122 if (POINTER_SET(lic)) {
123 bitVectUnSetBit (OP_USES(from),lic->key);
124 OP_USES(to) = bitVectSetBit (OP_USES(to),lic->key);
126 /* also check if the "from" was in the non-dominating
127 pointer sets and replace it with "to" in the bitVector */
128 if (bitVectBitValue(*ndpset,from->key)) {
129 bitVectUnSetBit(*ndpset,from->key);
130 bitVectSetBit(*ndpset,to->key);
135 bitVectUnSetBit (OP_DEFS(from),lic->key);
136 OP_DEFS(to) = bitVectSetBit (OP_DEFS(to),lic->key);
138 siaddr = IC_RESULT(lic)->isaddr ;
139 IC_RESULT(lic) = operandFromOperand(to);
140 IC_RESULT(lic)->isaddr = siaddr ;
144 IC_RIGHT(lic) && IC_RIGHT(lic)->key == from->key ) {
145 bitVectUnSetBit (OP_USES(from),lic->key);
146 OP_USES(to) = bitVectSetBit(OP_USES(to),lic->key);
147 siaddr = IC_RIGHT(lic)->isaddr ;
148 IC_RIGHT(lic) = operandFromOperand(to);
149 IC_RIGHT(lic)->isaddr = siaddr ;
153 IC_LEFT(lic) && IC_LEFT(lic)->key == from->key ) {
154 bitVectUnSetBit (OP_USES(from),lic->key);
155 OP_USES(to) = bitVectSetBit(OP_USES(to),lic->key);
156 siaddr = IC_LEFT(lic)->isaddr ;
157 IC_LEFT(lic) = operandFromOperand(to);
158 IC_LEFT(lic)->isaddr = siaddr ;
163 /*-----------------------------------------------------------------*/
164 /* iCodeKeyIs - if the icode keys match then return 1 */
165 /*-----------------------------------------------------------------*/
166 DEFSETFUNC(iCodeKeyIs)
171 if (cdp->diCode->key == key)
177 /*-----------------------------------------------------------------*/
178 /* removeFromInExprs - removes an icode from inexpressions */
179 /*-----------------------------------------------------------------*/
180 DEFSETFUNC(removeFromInExprs)
184 V_ARG(operand *,from);
186 V_ARG(eBBlock *,cbp);
192 deleteItemIf(&ebp->inExprs,iCodeKeyIs,ic->key);
193 if (ebp != cbp && !bitVectBitValue(cbp->domVect,ebp->bbnum))
194 replaceAllSymBySym(ebp->sch,from,to,&ebp->ndompset);
196 applyToSet(ebp->succList,removeFromInExprs,ic,from,to,cbp);
200 /*-----------------------------------------------------------------*/
201 /* isGlobalInNearSpace - return TRUE if valriable is a globalin data */
202 /*-----------------------------------------------------------------*/
203 static bool isGlobalInNearSpace (operand *op)
205 link *type = getSpec(operandType(op));
206 /* this is 8051 specific: optimization
207 suggested by Jean-Louis VERN, with 8051s we have no
208 advantage of putting variables in near space into
210 if (isOperandGlobal(op) && !IN_FARSPACE(SPEC_OCLS(type)) &&
211 IN_DIRSPACE(SPEC_OCLS(type)))
217 /*-----------------------------------------------------------------*/
218 /* findCheaperOp - cseBBlock support routine, will check to see if */
219 /* we have a operand previously defined */
220 /*-----------------------------------------------------------------*/
221 DEFSETFUNC(findCheaperOp)
224 V_ARG(operand *,cop);
225 V_ARG(operand **,opp);
227 /* if we have already found it */
231 /* not found it yet check if this is the one */
232 /* and this is not the defining one */
233 if (cop->key == cdp->key) {
235 /* do a special check this will help in */
236 /* constant propagation & dead code elim*/
237 /* for assignments only */
238 if (cdp->diCode->op == '=') {
239 /* if the result is volatile then return result */
240 if (IS_OP_VOLATILE (IC_RESULT(cdp->diCode)))
241 *opp = IC_RESULT(cdp->diCode);
243 /* if this is a straight assignment and
244 left is a temp then prefer the temporary to the
246 if (!POINTER_SET(cdp->diCode) &&
247 IS_ITEMP(IC_RESULT(cdp->diCode)) &&
248 IS_TRUE_SYMOP(IC_RIGHT(cdp->diCode)))
249 *opp = IC_RESULT(cdp->diCode);
251 /* if straight assignement && and both
252 are temps then prefer the one that
253 will not need extra space to spil, also
254 take into consideration if right side
255 an induction variable
257 if (!POINTER_SET(cdp->diCode) &&
258 IS_ITEMP(IC_RESULT(cdp->diCode)) &&
259 IS_ITEMP(IC_RIGHT(cdp->diCode)) &&
260 !OP_SYMBOL(IC_RIGHT(cdp->diCode))->isind &&
261 ( (!SPIL_LOC(IC_RIGHT(cdp->diCode)) &&
262 SPIL_LOC(IC_RESULT(cdp->diCode))) ||
263 ( SPIL_LOC(IC_RESULT(cdp->diCode)) &&
264 SPIL_LOC(IC_RESULT(cdp->diCode)) ==
265 SPIL_LOC(IC_RIGHT(cdp->diCode))) )
267 *opp = IC_RESULT(cdp->diCode);
269 *opp = IC_RIGHT(cdp->diCode);
272 *opp = IC_RESULT(cdp->diCode) ;
275 /* if this is an assign to a temp. then check
276 if the right side is this then return this */
277 if (IS_TRUE_SYMOP(cop) &&
278 cdp->diCode->op == '=' &&
279 !POINTER_SET(cdp->diCode) &&
280 cop->key == IC_RIGHT(cdp->diCode)->key &&
281 !isGlobalInNearSpace (IC_RIGHT(cdp->diCode)) &&
282 IS_ITEMP(IC_RESULT(cdp->diCode)))
283 *opp = IC_RESULT(cdp->diCode);
287 if ((isGlobalInNearSpace(cop) &&
288 !isOperandLiteral(*opp)) ||
289 isOperandVolatile(*opp,FALSE)
295 if (cop->key == (*opp)->key ) {
300 if ((*opp)->isaddr != cop->isaddr && IS_ITEMP(cop)) {
301 *opp = operandFromOperand(*opp);
302 (*opp)->isaddr = cop->isaddr;
312 /*-----------------------------------------------------------------*/
313 /* findPointerSet - finds the right side of a pointer set op */
314 /*-----------------------------------------------------------------*/
315 DEFSETFUNC(findPointerSet)
319 V_ARG(operand **,opp);
320 V_ARG(operand *,rop);
322 if (POINTER_SET(cdp->diCode) &&
323 IC_RESULT(cdp->diCode)->key == op->key &&
324 !isOperandVolatile(IC_RESULT(cdp->diCode),TRUE) &&
325 !isOperandVolatile(IC_RIGHT(cdp->diCode),TRUE) &&
326 getSize(operandType(IC_RIGHT(cdp->diCode))) ==
327 getSize(operandType(rop))) {
328 *opp = IC_RIGHT(cdp->diCode);
335 /*-----------------------------------------------------------------*/
336 /* findPrevIc - cseBBlock support function will return the iCode */
337 /* which matches the current one */
338 /*-----------------------------------------------------------------*/
339 DEFSETFUNC(findPrevIc)
345 /* if already found */
349 /* if the iCodes are the same */
350 if (isiCodeEqual(ic,cdp->diCode) &&
351 isOperandEqual(cdp->sym,IC_RESULT(cdp->diCode))) {
356 /* if iCodes are not the same */
357 /* see the operands maybe interchanged */
358 if (ic->op == cdp->diCode->op &&
359 ( ic->op == '+' || ic->op == '*' ) &&
360 isOperandEqual(IC_LEFT(ic),IC_RIGHT(cdp->diCode)) &&
361 isOperandEqual(IC_RIGHT(ic),IC_LEFT(cdp->diCode))) {
369 /*-----------------------------------------------------------------*/
370 /* ifDefGlobal - if definition is global */
371 /*-----------------------------------------------------------------*/
372 DEFSETFUNC(ifDefGlobal)
376 return (isOperandGlobal(cdp->sym));
379 /*-----------------------------------------------------------------*/
380 /* ifAnyGetPointer - if get pointer icode */
381 /*-----------------------------------------------------------------*/
382 DEFSETFUNC(ifAnyGetPointer)
386 if (cdp->diCode && POINTER_GET(cdp->diCode)) return 1;
390 /*-----------------------------------------------------------------*/
391 /* ifOperandsHave - if any of the operand are the same as this */
392 /*-----------------------------------------------------------------*/
393 DEFSETFUNC(ifOperandsHave)
399 if (IC_LEFT(cdp->diCode) &&
400 IS_SYMOP(IC_LEFT(cdp->diCode)) &&
401 IC_LEFT(cdp->diCode)->key == op->key)
404 if (IC_RIGHT(cdp->diCode) &&
405 IS_SYMOP(IC_RIGHT(cdp->diCode)) &&
406 IC_RIGHT(cdp->diCode)->key == op->key)
409 /* or if any of the operands are volatile */
410 if (IC_LEFT(cdp->diCode) &&
411 IS_OP_VOLATILE(IC_LEFT(cdp->diCode)))
414 if (IC_RIGHT(cdp->diCode) &&
415 IS_OP_VOLATILE(IC_RIGHT(cdp->diCode)))
419 if (IC_RESULT(cdp->diCode) &&
420 IS_OP_VOLATILE(IC_RESULT(cdp->diCode)))
426 /*-----------------------------------------------------------------*/
427 /* ifDefSymIs - if a definition is found in the set */
428 /*-----------------------------------------------------------------*/
429 int ifDefSymIs (set *cseSet, operand *sym)
434 if (!sym || !IS_SYMOP(sym))
436 for (sl = cseSet ; sl ; sl = sl->next ) {
438 if (loop->sym->key == sym->key )
445 /*-----------------------------------------------------------------*/
446 /* ifDefSymIsX - will return 1 if the symbols match */
447 /*-----------------------------------------------------------------*/
448 DEFSETFUNC(ifDefSymIsX)
454 return cdp->sym->key == op->key ;
456 return ( isOperandEqual(cdp->sym,op) );
461 /*-----------------------------------------------------------------*/
462 /* ifDiCodeIs - returns truw if diCode is same */
463 /*-----------------------------------------------------------------*/
464 int ifDiCodeIs (set *cseSet, iCode *ic)
472 for (sl = cseSet ; sl ; sl = sl->next ) {
474 if (loop->diCode == ic)
481 /*-----------------------------------------------------------------*/
482 /* ifPointerGet - returns true if the icode is pointer get sym */
483 /*-----------------------------------------------------------------*/
484 DEFSETFUNC(ifPointerGet)
488 iCode *dic = cdp->diCode;
489 operand *left = IC_LEFT(cdp->diCode);
491 if (POINTER_GET(dic) && left->key == op->key)
497 /*-----------------------------------------------------------------*/
498 /* ifPointerSet - returns true if the icode is pointer set sym */
499 /*-----------------------------------------------------------------*/
500 DEFSETFUNC(ifPointerSet)
505 if (POINTER_SET(cdp->diCode) &&
506 IC_RESULT(cdp->diCode)->key == op->key)
512 /*-----------------------------------------------------------------*/
513 /* ifDiCodeIsX - will return 1 if the symbols match */
514 /*-----------------------------------------------------------------*/
515 DEFSETFUNC(ifDiCodeIsX)
520 return cdp->diCode == ic;
524 /*-----------------------------------------------------------------*/
525 /* algebraicOpts - does some algebraic optimizations */
526 /*-----------------------------------------------------------------*/
527 void algebraicOpts (iCode *ic)
529 /* we don't deal with the following iCodes
540 /* if both operands present & ! IFX */
541 /* then if they are both literal we */
542 /* perform the operation right now */
546 IS_OP_LITERAL(IC_LEFT(ic)) &&
547 IS_OP_LITERAL(IC_RIGHT(ic))) {
549 IC_RIGHT(ic) = operandOperation (IC_LEFT(ic),
552 operandType(IC_RESULT(ic)));
555 SET_RESULT_RIGHT(ic);
559 /* if not ifx & only one operand present */
562 IS_OP_LITERAL(IC_LEFT(ic)) &&
565 IC_RIGHT(ic) = operandOperation (IC_LEFT(ic),
568 operandType(IC_RESULT(ic)));
571 SET_RESULT_RIGHT(ic);
576 /* a special case : or in short a kludgy solution will think
577 about a better solution over a glass of wine someday */
578 if ( ic->op == GET_VALUE_AT_ADDRESS ) {
580 if (IS_ITEMP(IC_RESULT(ic)) &&
581 IS_TRUE_SYMOP(IC_LEFT(ic))) {
584 IC_RIGHT(ic) = operandFromOperand(IC_LEFT(ic));
585 IC_RIGHT(ic)->isaddr = 0;
587 IC_RESULT(ic) = operandFromOperand(IC_RESULT(ic));
588 IC_RESULT(ic)->isaddr = 0;
589 setOperandType(IC_RESULT(ic),operandType(IC_RIGHT(ic)));
593 if (IS_ITEMP(IC_LEFT(ic)) &&
594 IS_ITEMP(IC_RESULT(ic)) &&
595 /* !OP_SYMBOL(IC_RESULT(ic))->isreqv && */
596 /* !OP_SYMBOL(IC_LEFT(ic))->isreqv && */
597 !IC_LEFT(ic)->isaddr ) {
599 IC_RIGHT(ic) = operandFromOperand(IC_LEFT(ic));
600 IC_RIGHT(ic)->isaddr = 0;
601 IC_RESULT(ic) = operandFromOperand(IC_RESULT(ic));
602 IC_RESULT(ic)->isaddr = 0;
610 /* depending on the operation */
613 /* if adding the same thing change to left shift by 1*/
614 if (IC_LEFT(ic)->key == IC_RIGHT(ic)->key &&
615 !IS_FLOAT(operandType(IC_RESULT(ic)))) {
617 IC_RIGHT(ic) = operandFromLit(1);
620 /* if addition then check if one of them is a zero */
621 /* if yes turn it into assignmnt*/
622 if (IS_OP_LITERAL(IC_LEFT(ic)) &&
623 operandLitValue(IC_LEFT(ic)) == 0.0 ) {
627 SET_ISADDR(IC_RESULT(ic),0);
628 SET_ISADDR(IC_RIGHT(ic),0);
631 if (IS_OP_LITERAL(IC_RIGHT(ic)) &&
632 operandLitValue(IC_RIGHT(ic)) == 0.0 ) {
635 IC_RIGHT(ic) = IC_LEFT(ic) ;
637 SET_ISADDR(IC_RIGHT(ic),0);
638 SET_ISADDR(IC_RESULT(ic),0);
643 /* if subtracting the the same thing then zero */
644 if ( IC_LEFT(ic)->key == IC_RIGHT(ic)->key ) {
646 IC_RIGHT(ic) = operandFromLit(0);
648 IC_RESULT(ic) = operandFromOperand(IC_RESULT(ic));
649 IC_RESULT(ic)->isaddr = 0;
653 /* if subtraction then check if one of the operand */
654 /* is zero then depending on which operand change */
655 /* to assignment or unary minus */
656 if (IS_OP_LITERAL(IC_RIGHT(ic)) &&
657 operandLitValue(IC_RIGHT(ic)) == 0.0 ) {
658 /* right size zero change to assignment */
660 IC_RIGHT(ic) = IC_LEFT(ic);
662 SET_ISADDR(IC_RIGHT(ic),0);
663 SET_ISADDR(IC_RESULT(ic),0);
666 if (IS_OP_LITERAL(IC_LEFT(ic)) &&
667 operandLitValue (IC_LEFT(ic)) == 0.0) {
668 /* left zero turn into an unary minus */
669 ic->op = UNARYMINUS ;
670 IC_LEFT(ic) = IC_RIGHT(ic);
671 IC_RIGHT(ic) = NULL ;
675 /* if multiplication then check if either of */
676 /* them is zero then the result is zero */
677 /* if either of them is one then result is */
680 if (IS_OP_LITERAL(IC_LEFT(ic))) {
682 if (operandLitValue(IC_LEFT(ic)) == 0.0) {
684 IC_RIGHT(ic) = IC_LEFT(ic);
686 SET_RESULT_RIGHT(ic);
689 if ( operandLitValue(IC_LEFT(ic)) == 1.0) {
692 SET_RESULT_RIGHT(ic);
697 if (IS_OP_LITERAL(IC_RIGHT(ic))) {
699 if (operandLitValue(IC_RIGHT(ic)) == 0.0 ) {
702 SET_RESULT_RIGHT(ic);
706 if (operandLitValue(IC_RIGHT(ic)) == 1.0) {
708 IC_RIGHT(ic) = IC_LEFT(ic);
710 SET_RESULT_RIGHT(ic);
716 /* if division by self then 1 */
717 if (IC_LEFT(ic)->key == IC_RIGHT(ic)->key) {
719 IC_RIGHT(ic) = operandFromLit(1);
721 IC_RESULT(ic) = operandFromOperand(IC_RESULT(ic));
722 IC_RESULT(ic)->isaddr = 0;
724 /* if this is a division then check if right */
725 /* is one then change it to an assignment */
726 if (IS_OP_LITERAL(IC_RIGHT(ic)) &&
727 operandLitValue(IC_RIGHT(ic)) == 1.0 ) {
730 IC_RIGHT(ic) = IC_LEFT(ic);
732 SET_RESULT_RIGHT(ic);
736 /* if both are the same for an comparison operators */
740 if (isOperandEqual(IC_LEFT(ic),IC_RIGHT(ic))) {
742 IC_RIGHT(ic) = operandFromLit(1);
744 SET_RESULT_RIGHT(ic);
750 if (isOperandEqual(IC_LEFT(ic),IC_RIGHT(ic))) {
752 IC_RIGHT(ic) = operandFromLit(0);
754 SET_RESULT_RIGHT(ic);
758 /* if this is a cast of a literal value */
759 if ( IS_OP_LITERAL(IC_RIGHT(ic))) {
762 operandFromValue (valCastLiteral(operandType(IC_LEFT(ic)),
763 operandLitValue(IC_RIGHT(ic))));
765 SET_ISADDR(IC_RESULT(ic),0);
767 /* if casting to the same */
768 if ( checkType(operandType(IC_RESULT(ic)),
769 operandType(IC_RIGHT(ic))) == 1) {
772 SET_ISADDR(IC_RESULT(ic),0);
776 if (IS_OP_LITERAL(IC_LEFT(ic))) {
779 (operandLitValue(IC_LEFT(ic)) == 0 ?
780 operandFromLit(1) : operandFromLit(0));
782 SET_ISADDR(IC_RESULT(ic),0);
788 #define OTHERS_PARM(s) (s->_isparm && !s->ismyparm)
789 /*-----------------------------------------------------------------*/
790 /* updateSpillLocation - keeps track of register spill location */
791 /*-----------------------------------------------------------------*/
792 void updateSpillLocation ( iCode *ic)
803 /* for the form true_symbol := iTempNN */
804 if (ASSIGN_ITEMP_TO_SYM(ic)
805 && !SPIL_LOC(IC_RIGHT(ic))) {
807 setype = getSpec(operandType(IC_RESULT(ic)));
809 if (!IC_RIGHT(ic)->noSpilLoc &&
810 !IS_VOLATILE(setype) &&
811 !IN_FARSPACE(SPEC_OCLS(setype)) &&
812 !OTHERS_PARM(OP_SYMBOL(IC_RESULT(ic))) )
814 SPIL_LOC(IC_RIGHT(ic)) =
815 IC_RESULT(ic)->operand.symOperand;
818 if (ASSIGN_ITEMP_TO_ITEMP(ic) &&
819 !SPIL_LOC(IC_RIGHT(ic)) &&
820 !bitVectBitsInCommon(OP_DEFS(IC_RIGHT(ic)),OP_USES(IC_RESULT(ic))) &&
821 OP_SYMBOL(IC_RESULT(ic))->isreqv) {
823 setype = getSpec(operandType(IC_RESULT(ic)));
825 if (!IC_RIGHT(ic)->noSpilLoc &&
826 !IS_VOLATILE(setype) &&
827 !IN_FARSPACE(SPEC_OCLS(setype)) &&
828 !OTHERS_PARM(OP_SYMBOL(IC_RESULT(ic))) )
830 SPIL_LOC(IC_RIGHT(ic)) =
831 SPIL_LOC(IC_RESULT(ic));
835 /*-----------------------------------------------------------------*/
836 /* setUsesDef - sets the uses def bitvector for a given operand */
837 /*-----------------------------------------------------------------*/
838 void setUsesDefs (operand *op, bitVect *bdefs,
839 bitVect *idefs, bitVect **oud)
841 /* compute the definitions alive at this point */
842 bitVect *adefs = bitVectUnion(bdefs,idefs);
844 /* of these definitions find the ones that are */
845 /* for this operand */
846 adefs = bitVectIntersect(adefs,OP_DEFS(op));
848 /* these are the definitions that this operand can use */
849 op->usesDefs = adefs;
851 /* the out defs is an union */
852 *oud = bitVectUnion(*oud,adefs);
855 /*-----------------------------------------------------------------*/
856 /* unsetDefsAndUses - clear this operation for the operands */
857 /*-----------------------------------------------------------------*/
858 void unsetDefsAndUses ( iCode *ic )
860 if ( ic->op == JUMPTABLE)
863 /* take away this definition from the def chain of the */
864 /* result & take away from use set of the operands */
866 /* turn off def set */
867 if (IS_SYMOP(IC_RESULT(ic))) {
868 if ( !POINTER_SET(ic))
869 bitVectUnSetBit(OP_DEFS(IC_RESULT(ic)),ic->key);
871 bitVectUnSetBit(OP_USES(IC_RESULT(ic)),ic->key);
873 /* turn off the useSet for the operands */
874 if (IS_SYMOP(IC_LEFT(ic)))
875 bitVectUnSetBit (OP_USES(IC_LEFT(ic)),ic->key);
877 if (IS_SYMOP(IC_RIGHT(ic)))
878 bitVectUnSetBit (OP_USES(IC_RIGHT(ic)),ic->key);
880 else /* must be ifx turn off the use */
881 if (IS_SYMOP(IC_COND(ic)))
882 bitVectUnSetBit (OP_USES(IC_COND(ic)),ic->key);
885 /*-----------------------------------------------------------------*/
886 /* ifxOptimize - changes ifx conditions if it can */
887 /*-----------------------------------------------------------------*/
888 void ifxOptimize (iCode *ic, set *cseSet,
890 eBBlock *ebb, int *change,
891 eBBlock **ebbs, int count)
896 /* if the condition can be replaced */
899 applyToSetFTrue (cseSet,findCheaperOp,IC_COND(ic),&pdop);
906 /* if the conditional is a literal then */
907 if (IS_OP_LITERAL(IC_COND(ic))) {
909 if ( (operandLitValue(IC_COND(ic)) != 0.0) && IC_TRUE(ic)) {
911 /* change to a goto */
913 IC_LABEL(ic) = IC_TRUE(ic);
918 if (!operandLitValue(IC_COND(ic)) && IC_FALSE(ic)) {
920 IC_LABEL(ic) = IC_FALSE(ic);
924 /* then kill this if condition */
925 remiCodeFromeBBlock (ebb,ic);
929 /* now we need to recompute the control flow */
930 /* since the control flow has changed */
931 /* this is very expensive but it does not happen */
932 /* too often, if it does happen then the user pays */
934 computeControlFlow (ebbs,count,1);
935 werror (W_CONTROL_FLOW,ic->filename,ic->lineno);
939 /* if there is only one successor and that successor
940 is the same one we are conditionally going to then
941 we can remove this conditional statement */
942 label = (IC_TRUE(ic) ? IC_TRUE(ic) : IC_FALSE(ic));
943 if (elementsInSet(ebb->succList) == 1 &&
944 isinSet(ebb->succList,eBBWithEntryLabel(ebbs,label,count))) {
946 remiCodeFromeBBlock(ebb,ic);
947 computeControlFlow (ebbs,count,1);
948 werror (W_CONTROL_FLOW,ic->filename,ic->lineno);
953 /* if it remains an IFX the update the use Set */
954 OP_USES(IC_COND(ic)) = bitVectSetBit(OP_USES(IC_COND(ic)),ic->key);
955 setUsesDefs(IC_COND(ic),ebb->defSet,ebb->outDefs,&ebb->usesDefs);
959 /*-----------------------------------------------------------------*/
960 /* diCodeForSym - finds the definiting instruction for a symbol */
961 /*-----------------------------------------------------------------*/
962 DEFSETFUNC(diCodeForSym)
965 V_ARG(operand *,sym);
968 /* if already found */
972 /* if not if this is the defining iCode */
973 if (sym->key == cdp->key) {
981 /*-----------------------------------------------------------------*/
982 /* constFold - does some constant folding */
983 /*-----------------------------------------------------------------*/
984 int constFold (iCode *ic, set *cseSet)
988 /* this routine will change
994 /* deal with only + & - */
999 /* this check is a hueristic to prevent live ranges
1000 from becoming too long */
1001 if (IS_PTR(operandType(IC_RESULT(ic))))
1004 /* check if operation with a literal */
1005 if (!IS_OP_LITERAL(IC_RIGHT(ic)))
1008 /* check if we can find a definition for the
1010 if (!(applyToSet(cseSet,diCodeForSym,IC_LEFT(ic),&dic)))
1013 /* check that this is also a +/- */
1014 if (dic->op != '+' && dic->op != '-')
1017 /* with a literal */
1018 if (!IS_OP_LITERAL(IC_RIGHT(dic)))
1021 /* find the definition of the left operand
1022 of dic.then check if this defined with a
1023 get_pointer return 0 if the pointer size is
1024 less than 2 (MCS51 specific) */
1025 if (!(applyToSet(cseSet,diCodeForSym,IC_LEFT(dic),&ldic)))
1028 if (POINTER_GET(ldic) && getSize(operandType(IC_LEFT(ldic))) <= 1)
1031 /* it is if the operations are the same*/
1032 /* the literal parts need to be added */
1033 IC_LEFT(ic) = operandFromOperand(IC_LEFT(dic));
1034 if (ic->op == dic->op )
1035 IC_RIGHT(ic) = operandFromLit(operandLitValue(IC_RIGHT(ic))+
1036 operandLitValue(IC_RIGHT(dic)));
1038 IC_RIGHT(ic) = operandFromLit(operandLitValue(IC_RIGHT(ic)) -
1039 operandLitValue(IC_RIGHT(dic)));
1041 if (IS_ITEMP(IC_RESULT(ic))) {
1042 SPIL_LOC(IC_RESULT(ic)) = NULL;
1043 IC_RESULT(ic)->noSpilLoc = 1;
1050 /*-----------------------------------------------------------------*/
1051 /* deleteGetPointers - called when a pointer is passed as parm */
1052 /* will delete from cseSet all get pointers computed from this */
1053 /* pointer. A simple ifOperandsHave is not good enough here */
1054 /*-----------------------------------------------------------------*/
1055 static void deleteGetPointers (set **cseSet, set **pss, operand *op,eBBlock *ebb)
1057 set *compItems = NULL;
1062 if (!*cseSet && !*pss)
1065 /* first find all items computed from this operand .
1066 This done fairly simply go thru the list and find
1067 those that are computed by arthimetic with this
1069 for (cdp = setFirstItem(*cseSet); cdp ; cdp = setNextItem(*cseSet)) {
1070 if (IS_ARITHMETIC_OP(cdp->diCode)) {
1071 if (isOperandEqual(IC_LEFT(cdp->diCode),op) ||
1072 isOperandEqual(IC_RIGHT(cdp->diCode),op)) {
1073 /* save it in our list of items */
1074 addSet(&compItems,IC_RESULT(cdp->diCode));
1076 /* also check for those computed from our computed
1077 list . This will take care of situations like
1078 iTemp1 = iTemp0 + 8;
1079 iTemp2 = iTemp1 + 8; */
1080 if (isinSetWith(compItems,IC_LEFT(cdp->diCode),isOperandEqual) ||
1081 isinSetWith(compItems,IC_RIGHT(cdp->diCode),isOperandEqual)) {
1082 addSet(&compItems,IC_RESULT(cdp->diCode));
1087 /* now delete all pointer gets with this op */
1088 deleteItemIf(cseSet,ifPointerGet,op);
1089 deleteItemIf(pss,ifPointerSet,op);
1091 /* set the bit vector used by dataFlow computation later */
1092 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,op->key);
1093 /* now for the computed items */
1094 for (cop = setFirstItem(compItems); cop ; cop = setNextItem(compItems)) {
1095 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,cop->key);
1096 deleteItemIf(cseSet,ifPointerGet,cop);
1097 deleteItemIf(pss,ifPointerSet,cop);
1101 /*-----------------------------------------------------------------*/
1102 /* delGetPointerSucc - delete get pointer from inExprs of succ with*/
1103 /* dfnum > supplied */
1104 /*-----------------------------------------------------------------*/
1105 DEFSETFUNC(delGetPointerSucc)
1107 eBBlock *ebp = item;
1108 V_ARG(operand *,op);
1115 if (ebp->dfnum > dfnum) {
1116 deleteItemIf(&ebp->inExprs,ifPointerGet,op);
1119 return applyToSet(ebp->succList,delGetPointerSucc,op,dfnum);
1122 /*-----------------------------------------------------------------*/
1123 /* fixUpTypes - KLUGE HACK fixup a lowering problem */
1124 /*-----------------------------------------------------------------*/
1125 static void fixUpTypes(iCode *ic)
1127 link *t1 = operandType(IC_LEFT(ic)) ,*t2;
1135 /* for pointer_gets if the types of result & left r the
1136 same then change it type of result to next */
1138 checkType(t2=operandType(IC_RESULT(ic)),t1) == 1) {
1139 setOperandType(IC_RESULT(ic),t2->next);
1143 /*-----------------------------------------------------------------*/
1144 /* cseBBlock - common subexpression elimination for basic blocks */
1145 /* this is the hackiest kludgiest routine in the whole */
1146 /* system. also the most important, since almost all */
1147 /* data flow related information is computed by it */
1148 /*-----------------------------------------------------------------*/
1149 int cseBBlock ( eBBlock *ebb, int computeOnly,
1150 eBBlock **ebbs, int count)
1156 set *ptrSetSet = NULL;
1158 /* if this block is not reachable */
1162 /* set of common subexpressions */
1163 cseSet = setFromSet (ebb->inExprs) ;
1165 /* these will be computed by this routine */
1166 setToNull ((void **)&ebb->outDefs);
1167 setToNull ((void **)&ebb->defSet);
1168 setToNull ((void **)&ebb->usesDefs);
1169 setToNull ((void **)&ebb->ptrsSet);
1170 setToNull ((void **)&ebb->addrOf);
1171 setToNull ((void **)&ebb->ldefs);
1173 ebb->outDefs = bitVectCopy (ebb->inDefs);
1174 bitVectDefault = iCodeKey;
1175 ebb->defSet = newBitVect(iCodeKey);
1176 ebb->usesDefs=newBitVect(iCodeKey);
1178 /* for all the instructions in this block do */
1179 for (ic = ebb->sch ; ic ; ic = ic->next ) {
1188 /* if this is an assignment from true symbol
1189 to a temp then do pointer post inc/dec optimzation */
1190 if (ic->op == '=' && !POINTER_SET(ic) &&
1191 IS_PTR(operandType(IC_RESULT(ic)))) {
1192 ptrPostIncDecOpt(ic);
1195 /* clear the def & use chains for the operands involved */
1196 /* in this operation . since it can change due to opts */
1197 unsetDefsAndUses (ic);
1199 if ( ic->op == PCALL || ic->op == CALL || ic->op == RECEIVE) {
1200 /* add to defSet of the symbol */
1201 OP_DEFS(IC_RESULT(ic)) =
1202 bitVectSetBit (OP_DEFS(IC_RESULT(ic)),ic->key);
1203 /* add to the definition set of this block */
1204 ebb->defSet = bitVectSetBit (ebb->defSet,ic->key);
1205 ebb->ldefs = bitVectSetBit (ebb->ldefs,ic->key);
1206 ebb->outDefs= bitVectCplAnd (ebb->outDefs,OP_DEFS(IC_RESULT(ic)));
1207 setUsesDefs(IC_RESULT(ic),ebb->defSet,ebb->outDefs,&ebb->usesDefs);
1208 /* delete global variables from the cseSet
1209 since they can be modified by the function call */
1210 deleteItemIf(&cseSet,ifDefGlobal);
1211 /* delete all getpointer iCodes from cseSet, this should
1212 be done only for global arrays & pointers but at this
1213 point we don't know if globals, so to be safe do all*/
1214 deleteItemIf(&cseSet,ifAnyGetPointer);
1217 /* for pcall & ipush we need to add to the useSet */
1218 if ((ic->op == PCALL ||
1222 IS_SYMOP(IC_LEFT(ic))) {
1224 /* check if they can be replaced */
1225 if ( !computeOnly ) {
1227 applyToSetFTrue(cseSet,findCheaperOp,IC_LEFT(ic),&pdop);
1229 IC_LEFT(ic) = pdop ;
1231 /* the lookup could have changed it*/
1232 if (IS_SYMOP(IC_LEFT(ic))) {
1233 OP_USES(IC_LEFT(ic)) =
1234 bitVectSetBit(OP_USES(IC_LEFT(ic)),ic->key);
1235 setUsesDefs(IC_LEFT(ic),ebb->defSet,
1236 ebb->outDefs,&ebb->usesDefs);
1240 /* if we a sending a pointer as a parameter
1241 then kill all cse since the pointed to item
1242 might be changed in the function being called */
1243 if ((ic->op == IPUSH || ic->op == SEND) &&
1244 IS_PTR(operandType(IC_LEFT(ic)))) {
1245 deleteGetPointers(&cseSet,&ptrSetSet,IC_LEFT(ic),ebb);
1246 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,IC_LEFT(ic)->key);
1247 for (i = 0 ; i < count ;ebbs[i++]->visited = 0);
1248 applyToSet(ebb->succList,delGetPointerSucc,
1249 IC_LEFT(ic),ebb->dfnum);
1254 /* if jumptable then mark the usage */
1255 if (ic->op == JUMPTABLE ) {
1256 OP_USES(IC_JTCOND(ic)) =
1257 bitVectSetBit(OP_USES(IC_JTCOND(ic)),ic->key);
1258 setUsesDefs(IC_JTCOND(ic),ebb->defSet,
1259 ebb->outDefs,&ebb->usesDefs);
1266 /* do some algebraic optimizations if possible */
1268 while (constFold(ic,cseSet));
1271 if (POINTER_GET(ic) && !IS_PTR(operandType(IC_LEFT(ic)))) {
1272 setOperandType(IC_LEFT(ic),
1273 aggrToPtr(operandType(IC_LEFT(ic)),FALSE));
1277 if (POINTER_SET(ic) && !IS_PTR(operandType(IC_RESULT(ic)))) {
1278 setOperandType(IC_RESULT(ic),
1279 aggrToPtr(operandType(IC_RESULT(ic)),FALSE));
1282 /* if this is a condition statment then */
1283 /* check if the condition can be replaced */
1284 if (ic->op == IFX ) {
1285 ifxOptimize (ic, cseSet, computeOnly,
1291 /* if the assignment & result is a temp */
1292 /* see if we can replace it */
1293 if (ic->op == '=') {
1295 /* update the spill location for this */
1296 updateSpillLocation (ic);
1298 if (POINTER_SET(ic)) {
1300 applyToSetFTrue (cseSet,findCheaperOp,IC_RESULT(ic),&pdop);
1301 if (pdop && IS_ITEMP(pdop) && !computeOnly)
1302 IC_RESULT(ic) = pdop;
1306 /* do the operand lookup i.e. for both the */
1307 /* right & left operand : check the cseSet */
1308 /* to see if they have been replaced if yes*/
1309 /* then replace them with those from cseSet*/
1311 /* and left is a symbol */
1312 if (IS_SYMOP(IC_LEFT(ic)) &&
1313 !computeOnly && ic->op != ADDRESS_OF ) {
1316 applyToSetFTrue (cseSet,findCheaperOp,IC_LEFT(ic),&pdop) ;
1318 if (POINTER_GET(ic)) {
1319 if (IS_ITEMP(pdop) || IS_OP_LITERAL(pdop)) {
1323 /* check if there is a pointer set
1324 for the same pointer visible if yes
1325 then change this into an assignment */
1327 if (applyToSetFTrue(cseSet,findPointerSet,IC_LEFT(ic),&pdop,IC_RESULT(ic)) &&
1328 !bitVectBitValue(ebb->ptrsSet,pdop->key)){
1331 IC_RIGHT(ic) = pdop;
1332 SET_ISADDR(IC_RESULT(ic),0);
1337 IC_LEFT(ic) = pdop ;
1344 if (IS_SYMOP(IC_RIGHT(ic)) && !computeOnly) {
1347 applyToSetFTrue (cseSet,findCheaperOp,IC_RIGHT(ic),&pdop);
1350 IC_RIGHT(ic) = pdop;
1355 /* if left or right changed then do algebraic */
1358 while(constFold(ic,cseSet));
1361 /* if after all this it becomes a assignment to self
1362 then delete it and continue */
1363 if (ASSIGNMENT_TO_SELF(ic)) {
1364 remiCodeFromeBBlock(ebb,ic);
1368 /* now we will check to see if the entire */
1369 /* operation has been performed before */
1370 /* and is available */
1371 /* don't do assignments they will be killed */
1372 /* by dead code elimination if required do */
1373 /* it only if result is a temporary */
1375 if (!( POINTER_GET(ic) &&
1376 (IS_BITFIELD(OP_SYMBOL(IC_RESULT(ic))->etype) ||
1377 isOperandVolatile(IC_LEFT(ic),TRUE) ||
1378 bitVectBitValue(ebb->ndompset,IC_LEFT(ic)->key))) &&
1380 IS_ITEMP(IC_RESULT(ic)) &&
1382 applyToSet (cseSet,findPrevIc,ic,&pdic);
1383 if (pdic && checkType(operandType(IC_RESULT(pdic)),
1384 operandType(IC_RESULT(ic))) != 1)
1388 /* if found then eliminate this and add to*/
1389 /* to cseSet an element containing result */
1390 /* of this with previous opcode */
1393 if (IS_ITEMP(IC_RESULT(ic))) {
1395 /* replace in the remaining of this block */
1396 replaceAllSymBySym(ic->next,IC_RESULT(ic),IC_RESULT(pdic),&ebb->ndompset);
1397 /* remove this iCode from inexpressions of all
1398 its successors, it cannot be in the in expressions
1399 of any of the predecessors */
1400 for (i = 0 ; i < count ;ebbs[i++]->visited = 0);
1401 applyToSet(ebb->succList,removeFromInExprs,ic,IC_RESULT(ic),
1402 IC_RESULT(pdic),ebb);
1404 /* if this was moved from another block */
1405 /* then replace in those blocks too */
1406 if ( ic->movedFrom ) {
1408 for (owner = setFirstItem(ic->movedFrom); owner ;
1409 owner = setNextItem(ic->movedFrom))
1410 replaceAllSymBySym(owner->sch,IC_RESULT(ic),IC_RESULT(pdic),&owner->ndompset);
1412 pdic->movedFrom = unionSets(pdic->movedFrom,ic->movedFrom,THROW_NONE);
1415 addSetHead (&cseSet,newCseDef(IC_RESULT(ic),pdic));
1418 /* eliminate this */
1419 remiCodeFromeBBlock (ebb,ic);
1424 if (IS_ITEMP(IC_RESULT(ic)))
1429 /* just add this as a previous expression except in */
1430 /* case of a pointer access in which case this is a */
1431 /* usage not a definition */
1432 if (! (POINTER_SET(ic)) && IC_RESULT(ic)){
1433 deleteItemIf (&cseSet, ifDefSymIsX,IC_RESULT(ic));
1434 addSetHead(&cseSet,newCseDef(IC_RESULT(ic),ic));
1440 /* if assignment to a parameter which is not
1441 mine and type is a pointer then delete
1442 pointerGets to take care of aliasing */
1443 if (ASSIGNMENT(ic) &&
1444 OTHERS_PARM(OP_SYMBOL(IC_RESULT(ic))) &&
1445 IS_PTR(operandType(IC_RESULT(ic)))) {
1446 deleteGetPointers(&cseSet,&ptrSetSet,IC_RIGHT(ic),ebb);
1447 for (i = 0 ; i < count ;ebbs[i++]->visited = 0);
1448 applyToSet(ebb->succList,delGetPointerSucc,IC_RIGHT(ic),ebb->dfnum);
1449 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,IC_RIGHT(ic)->key);
1452 /* if this is a pointerget then see if we can replace
1453 this with a previously assigned pointer value */
1454 if (POINTER_GET(ic) &&
1455 !(IS_BITFIELD(OP_SYMBOL(IC_RESULT(ic))->etype) ||
1456 isOperandVolatile(IC_LEFT(ic),TRUE))) {
1458 applyToSet(ptrSetSet,findPointerSet,IC_LEFT(ic),&pdop,IC_RESULT(ic));
1459 /* if we find it then locally replace all
1460 references to the result with what we assigned */
1462 replaceAllSymBySym(ic->next,IC_RESULT(ic),pdop,&ebb->ndompset);
1466 /* delete from the cseSet anything that has */
1467 /* operands matching the result of this */
1468 /* except in case of pointer access */
1469 if (!(POINTER_SET(ic)) && IC_RESULT(ic)) {
1470 deleteItemIf (&cseSet,ifOperandsHave,IC_RESULT(ic));
1471 /* delete any previous definitions */
1472 ebb->defSet = bitVectCplAnd (ebb->defSet,OP_DEFS(IC_RESULT(ic)));
1476 /* add the left & right to the defUse set */
1477 if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))) {
1478 OP_USES(IC_LEFT(ic)) =
1479 bitVectSetBit (OP_USES(IC_LEFT(ic)),ic->key);
1480 setUsesDefs(IC_LEFT(ic),ebb->defSet,ebb->outDefs,&ebb->usesDefs);
1484 if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))) {
1485 OP_USES(IC_RIGHT(ic)) =
1486 bitVectSetBit (OP_USES(IC_RIGHT(ic)),ic->key);
1487 setUsesDefs(IC_RIGHT(ic),ebb->defSet,ebb->outDefs,&ebb->usesDefs);
1491 /* for the result it is special case, put the result */
1492 /* in the defuseSet if it a pointer or array access */
1493 if ( POINTER_SET(defic) ) {
1494 OP_USES(IC_RESULT(ic)) =
1495 bitVectSetBit (OP_USES(IC_RESULT(ic)),ic->key);
1496 setUsesDefs(IC_RESULT(ic),ebb->defSet,ebb->outDefs,&ebb->usesDefs);
1497 deleteItemIf(&cseSet,ifPointerGet,IC_RESULT(ic));
1498 ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,IC_RESULT(ic)->key);
1499 /* delete from inexpressions of all successors which
1500 have dfNum > than this block */
1501 for (i = 0 ; i < count ;ebbs[i++]->visited = 0);
1502 applyToSet(ebb->succList,delGetPointerSucc,IC_RESULT(ic),ebb->dfnum);
1504 /* delete from cseSet all other pointer sets
1506 deleteItemIf(&ptrSetSet,ifPointerSet,IC_RESULT(ic));
1507 /* add to the local pointerset set */
1508 addSetHead(&ptrSetSet,newCseDef(IC_RESULT(ic),ic));
1510 else /* add the result to defintion set */
1511 if (IC_RESULT(ic)) {
1512 OP_DEFS(IC_RESULT(ic)) =
1513 bitVectSetBit (OP_DEFS(IC_RESULT(ic)),ic->key);
1514 ebb->defSet = bitVectSetBit (ebb->defSet,ic->key);
1515 ebb->outDefs= bitVectCplAnd (ebb->outDefs,OP_DEFS(IC_RESULT(ic)));
1516 ebb->ldefs = bitVectSetBit (ebb->ldefs,ic->key);
1520 /* if this is an addressof instruction then */
1521 /* put the symbol in the address of list & */
1522 /* delete it from the cseSet */
1523 if (defic->op == ADDRESS_OF) {
1524 addSetHead (&ebb->addrOf, IC_LEFT(ic));
1525 deleteItemIf(&cseSet,ifDefSymIsX,IC_LEFT(ic));
1529 setToNull ((void **)&ebb->outExprs);
1530 ebb->outExprs = cseSet;
1531 ebb->outDefs = bitVectUnion (ebb->outDefs,ebb->defSet);
1532 ebb->ptrsSet = bitVectUnion (ebb->ptrsSet,ebb->inPtrsSet);
1536 /*-----------------------------------------------------------------*/
1537 /* cseAllBlocks - will sequentially go thru & do cse for all blocks*/
1538 /*-----------------------------------------------------------------*/
1539 int cseAllBlocks (eBBlock **ebbs,int count)
1544 /* if optimization turned off */
1546 for (i = 0 ; i < count ;i++ )
1547 change += cseBBlock (ebbs[i],FALSE,ebbs,count);