1 /*-------------------------------------------------------------------------
2 SDCCopt.c - calls all the optimizations routines and does some of the
3 hackier transformations, these include translating iCodes
4 to function calls and replacing local variables with their
5 register equivalents etc. Also contains the driver routine
6 for dead code elimination
8 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 In other words, you are welcome to use, share and improve this program.
25 You are forbidden to forbid anyone else to use, share and improve
26 what you give them. Help stamp out software-hoarding!
27 -------------------------------------------------------------------------*/
31 /*-----------------------------------------------------------------*/
32 /* global variables */
38 /*-----------------------------------------------------------------*/
39 /* printSymName - prints the symbol names */
40 /*-----------------------------------------------------------------*/
42 printSymName (void *vsym)
45 fprintf (stdout, " %s ", sym->name);
49 /*-----------------------------------------------------------------*/
50 /* cnvToFcall - does the actual conversion to function call */
51 /*-----------------------------------------------------------------*/
53 cnvToFcall (iCode * ic, eBBlock * ebp)
60 int lineno = ic->lineno;
63 ip = ic->next; /* insertion point */
64 /* remove it from the iCode */
65 remiCodeFromeBBlock (ebp, ic);
68 right = IC_RIGHT (ic);
104 /* if float support routines NOT compiled as reentrant */
105 if (!options.float_rent)
109 if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
111 newic = newiCode (SEND, IC_LEFT (ic), NULL);
112 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
116 newic = newiCode ('=', NULL, IC_LEFT (ic));
117 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type));
120 addiCodeToeBBlock (ebp, newic, ip);
121 newic->lineno = lineno;
124 if (IS_REGPARM (FUNC_ARGS(func->type)->next->etype))
126 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
127 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->next->etype);
131 newic = newiCode ('=', NULL, IC_RIGHT (ic));
132 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type)->next);
134 addiCodeToeBBlock (ebp, newic, ip);
135 newic->lineno = lineno;
142 if (IS_REGPARM (FUNC_ARGS(func->type)->next->etype))
144 newic = newiCode (SEND, right, NULL);
145 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->next->etype);
149 newic = newiCode (IPUSH, right, NULL);
152 bytesPushed += getSize(operandType(right));
155 addiCodeToeBBlock (ebp, newic, ip);
156 newic->lineno = lineno;
158 /* insert push left */
159 if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
161 newic = newiCode (SEND, left, NULL);
162 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
166 newic = newiCode (IPUSH, left, NULL);
169 bytesPushed += getSize(operandType(left));
171 addiCodeToeBBlock (ebp, newic, ip);
172 newic->lineno = lineno;
174 /* insert the call */
175 newic = newiCode (CALL, operandFromSymbol (func), NULL);
176 IC_RESULT (newic) = IC_RESULT (ic);
177 newic->lineno = lineno;
178 newic->parmBytes+=bytesPushed;
181 FUNC_HASFCALL (currFunc->type) = 1;
183 if(TARGET_IS_PIC16) {
184 /* normally these functions aren't marked external, so we can use their
185 * _extern field to marked as already added to symbol table */
187 if(!SPEC_EXTR(func->etype)) {
188 memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
190 SPEC_EXTR(func->etype) = 1;
191 seg = SPEC_OCLS( func->etype );
192 addSet(&seg->syms, func);
196 addiCodeToeBBlock (ebp, newic, ip);
199 /*-----------------------------------------------------------------*/
200 /* cnvToFloatCast - converts casts to floats to function calls */
201 /*-----------------------------------------------------------------*/
203 cnvToFloatCast (iCode * ic, eBBlock * ebp)
207 sym_link *type = operandType (IC_RIGHT (ic));
208 int linenno = ic->lineno;
213 /* remove it from the iCode */
214 remiCodeFromeBBlock (ebp, ic);
215 /* depending on the type */
216 for (bwd = 0; bwd < 3; bwd++)
218 for (su = 0; su < 2; su++)
220 if (compareType (type, __multypes[bwd][su]) == 1)
222 func = __conv[0][bwd][su];
230 /* if float support routines NOT compiled as reentrant */
231 if (!options.float_rent)
234 if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
236 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
237 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
241 newic = newiCode ('=', NULL, IC_RIGHT (ic));
242 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type));
244 addiCodeToeBBlock (ebp, newic, ip);
245 newic->lineno = linenno;
251 if (IS_REGPARM (FUNC_ARGS(func->type)->etype)) {
252 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
253 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
257 newic = newiCode (IPUSH, IC_RIGHT (ic), NULL);
259 bytesPushed += getSize(operandType(IC_RIGHT(ic)));
261 addiCodeToeBBlock (ebp, newic, ip);
262 newic->lineno = linenno;
267 newic = newiCode (CALL, operandFromSymbol (func), NULL);
268 IC_RESULT (newic) = IC_RESULT (ic);
269 newic->parmBytes+=bytesPushed;
272 FUNC_HASFCALL (currFunc->type) = 1;
274 if(TARGET_IS_PIC16) {
275 /* normally these functions aren't marked external, so we can use their
276 * _extern field to marked as already added to symbol table */
278 if(!SPEC_EXTR(func->etype)) {
279 memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
281 SPEC_EXTR(func->etype) = 1;
282 seg = SPEC_OCLS( func->etype );
283 addSet(&seg->syms, func);
287 addiCodeToeBBlock (ebp, newic, ip);
288 newic->lineno = linenno;
291 /*-----------------------------------------------------------------*/
292 /* cnvFromFloatCast - converts casts From floats to function calls */
293 /*-----------------------------------------------------------------*/
295 cnvFromFloatCast (iCode * ic, eBBlock * ebp)
299 sym_link *type = operandType (IC_LEFT (ic));
300 int lineno = ic->lineno;
305 /* remove it from the iCode */
306 remiCodeFromeBBlock (ebp, ic);
308 /* depending on the type */
309 for (bwd = 0; bwd < 3; bwd++)
311 for (su = 0; su < 2; su++)
313 if (compareType (type, __multypes[bwd][su]) == 1)
315 func = __conv[1][bwd][su];
323 /* if float support routines NOT compiled as reentrant */
324 if (!options.float_rent)
327 if (IS_REGPARM (FUNC_ARGS(func->type)->etype)) {
328 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
329 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
333 newic = newiCode ('=', NULL, IC_RIGHT (ic));
334 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type));
336 addiCodeToeBBlock (ebp, newic, ip);
337 newic->lineno = lineno;
344 if (IS_REGPARM (FUNC_ARGS(func->type)->etype)) {
345 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
346 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
350 newic = newiCode (IPUSH, IC_RIGHT (ic), NULL);
352 bytesPushed += getSize(operandType(IC_RIGHT(ic)));
354 addiCodeToeBBlock (ebp, newic, ip);
355 newic->lineno = lineno;
360 newic = newiCode (CALL, operandFromSymbol (func), NULL);
361 IC_RESULT (newic) = IC_RESULT (ic);
362 newic->parmBytes+=bytesPushed;
365 FUNC_HASFCALL (currFunc->type) = 1;
367 if(TARGET_IS_PIC16) {
368 /* normally these functions aren't marked external, so we can use their
369 * _extern field to marked as already added to symbol table */
371 if(!SPEC_EXTR(func->etype)) {
372 memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
374 SPEC_EXTR(func->etype) = 1;
375 seg = SPEC_OCLS( func->etype );
376 addSet(&seg->syms, func);
380 addiCodeToeBBlock (ebp, newic, ip);
381 newic->lineno = lineno;
384 extern operand *geniCodeRValue (operand *, bool);
386 /*-----------------------------------------------------------------*/
387 /* convilong - converts int or long mults or divs to fcalls */
388 /*-----------------------------------------------------------------*/
390 convilong (iCode * ic, eBBlock * ebp, sym_link * type, int op)
393 iCode *ip = ic->next;
395 int lineno = ic->lineno;
400 // Easy special case which avoids function call: modulo by a literal power
401 // of two can be replaced by a bitwise AND.
402 if (op == '%' && isOperandLiteral(IC_RIGHT(ic)))
404 unsigned litVal = (unsigned)(operandLitValue(IC_RIGHT(ic)));
406 // See if literal value is a power of 2.
407 while (litVal && !(litVal & 1))
413 // discard first high bit set.
420 IC_RIGHT(ic) = operandFromLit(operandLitValue(IC_RIGHT(ic)) - 1);
425 remiCodeFromeBBlock (ebp, ic);
428 /* depending on the type */
429 for (bwd = 0; bwd < 3; bwd++)
431 for (su = 0; su < 2; su++)
433 if (compareType (type, __multypes[bwd][su]) == 1)
436 func = __muldiv[0][bwd][su];
438 func = __muldiv[1][bwd][su];
440 func = __muldiv[2][bwd][su];
442 func = __rlrr[1][bwd][su];
444 func = __rlrr[0][bwd][su];
445 else if (op == RIGHT_OP)
446 func = __rlrr[1][bwd][su];
447 else if (op == LEFT_OP)
448 func = __rlrr[0][bwd][su];
457 /* if int & long support routines NOT compiled as reentrant */
458 if (!options.intlong_rent)
461 if (IS_REGPARM (FUNC_ARGS(func->type)->etype)) {
462 newic = newiCode (SEND, IC_LEFT (ic), NULL);
463 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
467 newic = newiCode ('=', NULL, IC_LEFT (ic));
468 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type));
470 addiCodeToeBBlock (ebp, newic, ip);
471 newic->lineno = lineno;
474 if (IS_REGPARM (FUNC_ARGS(func->type)->next->etype)) {
475 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
476 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->next->etype);
480 newic = newiCode ('=', NULL, IC_RIGHT (ic));
481 IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type)->next);
483 addiCodeToeBBlock (ebp, newic, ip);
484 newic->lineno = lineno;
489 /* compiled as reentrant then push */
491 if (IS_REGPARM (FUNC_ARGS(func->type)->next->etype))
493 newic = newiCode (SEND, IC_RIGHT (ic), NULL);
494 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->next->etype);
498 newic = newiCode (IPUSH, IC_RIGHT (ic), NULL);
501 bytesPushed += getSize(operandType(IC_RIGHT(ic)));
503 addiCodeToeBBlock (ebp, newic, ip);
504 newic->lineno = lineno;
506 /* insert push left */
507 if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
509 newic = newiCode (SEND, IC_LEFT (ic), NULL);
510 newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
514 newic = newiCode (IPUSH, IC_LEFT (ic), NULL);
517 bytesPushed += getSize(operandType(IC_LEFT(ic)));
519 addiCodeToeBBlock (ebp, newic, ip);
520 newic->lineno = lineno;
525 newic = newiCode (CALL, operandFromSymbol (func), NULL);
526 IC_RESULT (newic) = IC_RESULT (ic);
527 newic->lineno = lineno;
528 newic->parmBytes+=bytesPushed; // to clear the stack after the call
531 FUNC_HASFCALL (currFunc->type) = 1;
533 if(TARGET_IS_PIC16) {
534 /* normally these functions aren't marked external, so we can use their
535 * _extern field to marked as already added to symbol table */
537 if(!SPEC_EXTR(func->etype)) {
538 memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
540 SPEC_EXTR(func->etype) = 1;
541 seg = SPEC_OCLS( func->etype );
542 addSet(&seg->syms, func);
546 addiCodeToeBBlock (ebp, newic, ip);
549 /*-----------------------------------------------------------------*/
550 /* convertToFcall - converts some operations to fcalls */
551 /*-----------------------------------------------------------------*/
553 convertToFcall (eBBlock ** ebbs, int count)
557 /* for all blocks do */
558 for (i = 0; i < count; i++)
562 /* for all instructions in the block do */
563 for (ic = ebbs[i]->sch; ic; ic = ic->next)
566 /* floating point operations are
567 converted to function calls */
568 if ((IS_CONDITIONAL (ic) ||
569 IS_ARITHMETIC_OP (ic)) &&
570 (IS_FLOAT (operandType (IC_RIGHT (ic)))))
573 cnvToFcall (ic, ebbs[i]);
576 /* casting is a little different */
579 if (IS_FLOAT (operandType (IC_RIGHT (ic))))
580 cnvFromFloatCast (ic, ebbs[i]);
581 else if (IS_FLOAT (operandType (IC_LEFT (ic))))
582 cnvToFloatCast (ic, ebbs[i]);
585 /* if long / int mult or divide or mod */
586 if (ic->op == '*' || ic->op == '/' || ic->op == '%')
588 sym_link *leftType = operandType (IC_LEFT (ic));
590 if (IS_INTEGRAL (leftType) && getSize (leftType) > port->support.muldiv)
592 sym_link *rightType = operandType (IC_RIGHT (ic));
594 if (port->hasNativeMulFor != NULL &&
595 port->hasNativeMulFor (ic, leftType, rightType))
597 /* Leave as native */
601 convilong (ic, ebbs[i], leftType, ic->op);
606 if (ic->op == RRC || ic->op == RLC || ic->op == LEFT_OP || ic->op == RIGHT_OP)
608 sym_link *type = operandType (IC_LEFT (ic));
610 if (IS_INTEGRAL (type) && getSize (type) > port->support.shift && port->support.shift >= 0)
612 convilong (ic, ebbs[i], type, ic->op);
619 /*-----------------------------------------------------------------*/
620 /* isLocalWithoutDef - return 1 if sym might be used without a */
622 /*-----------------------------------------------------------------*/
624 isLocalWithoutDef (symbol * sym)
629 if (IS_VOLATILE (sym->type))
635 if (IS_AGGREGATE (sym->type))
644 /*-----------------------------------------------------------------*/
645 /* replaceRegEqv - replace all local variables with their reqv */
646 /*-----------------------------------------------------------------*/
648 replaceRegEqv (eBBlock ** ebbs, int count)
652 /* Update the symbols' def bitvector so we know if there is */
653 /* a defining iCode or not. Only replace a local variable */
654 /* with its register equivalent if there is a defining iCode; */
655 /* otherwise, the port's register allocater may choke. */
656 cseAllBlocks (ebbs, count, TRUE);
658 for (i = 0; i < count; i++)
663 for (ic = ebbs[i]->sch; ic; ic = ic->next)
672 IS_TRUE_SYMOP (IC_COND (ic)) &&
673 isLocalWithoutDef (OP_SYMBOL (IC_COND (ic))))
675 werrorfl (ic->filename, ic->lineno,
677 OP_SYMBOL (IC_COND (ic))->name);
678 OP_REQV (IC_COND (ic)) = NULL;
679 OP_SYMBOL (IC_COND (ic))->allocreq = 1;
682 if (IS_TRUE_SYMOP (IC_COND (ic)) &&
683 OP_REQV (IC_COND (ic)))
684 IC_COND (ic) = opFromOpWithDU (OP_REQV (IC_COND (ic)),
685 OP_SYMBOL (IC_COND (ic))->defs,
686 OP_SYMBOL (IC_COND (ic))->uses);
692 if (ic->op == JUMPTABLE)
694 if (IC_JTCOND (ic) &&
695 IS_TRUE_SYMOP (IC_JTCOND (ic)) &&
696 isLocalWithoutDef (OP_SYMBOL (IC_JTCOND (ic))))
698 werrorfl (ic->filename, ic->lineno,
700 OP_SYMBOL (IC_JTCOND (ic))->name);
701 OP_REQV (IC_JTCOND (ic)) = NULL;
702 OP_SYMBOL (IC_JTCOND (ic))->allocreq = 1;
705 if (IS_TRUE_SYMOP (IC_JTCOND (ic)) &&
706 OP_REQV (IC_JTCOND (ic)))
707 IC_JTCOND (ic) = opFromOpWithDU (OP_REQV (IC_JTCOND (ic)),
708 OP_SYMBOL (IC_JTCOND (ic))->defs,
709 OP_SYMBOL (IC_JTCOND (ic))->uses);
713 if (ic->op == RECEIVE)
715 if (OP_SYMBOL (IC_RESULT (ic))->addrtaken)
716 OP_SYMBOL (IC_RESULT (ic))->isspilt = 1;
720 if (IC_RESULT (ic) &&
721 IS_TRUE_SYMOP (IC_RESULT (ic)) &&
722 OP_REQV (IC_RESULT (ic)))
724 if (POINTER_SET (ic))
726 IC_RESULT (ic) = opFromOpWithDU (OP_REQV (IC_RESULT (ic)),
727 OP_SYMBOL (IC_RESULT (ic))->defs,
728 OP_SYMBOL (IC_RESULT (ic))->uses);
729 IC_RESULT (ic)->isaddr = 1;
732 IC_RESULT (ic) = opFromOpWithDU (OP_REQV (IC_RESULT (ic)),
733 OP_SYMBOL (IC_RESULT (ic))->defs,
734 OP_SYMBOL (IC_RESULT (ic))->uses);
738 IS_TRUE_SYMOP (IC_RIGHT (ic)) &&
739 isLocalWithoutDef (OP_SYMBOL (IC_RIGHT (ic))))
741 werrorfl (ic->filename, ic->lineno,
743 OP_SYMBOL (IC_RIGHT (ic))->name);
744 OP_REQV (IC_RIGHT (ic)) = NULL;
745 OP_SYMBOL (IC_RIGHT (ic))->allocreq = 1;
749 IS_TRUE_SYMOP (IC_RIGHT (ic)) &&
750 OP_REQV (IC_RIGHT (ic)))
752 IC_RIGHT (ic) = opFromOpWithDU (OP_REQV (IC_RIGHT (ic)),
753 OP_SYMBOL (IC_RIGHT (ic))->defs,
754 OP_SYMBOL (IC_RIGHT (ic))->uses);
755 IC_RIGHT (ic)->isaddr = 0;
759 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
760 isLocalWithoutDef (OP_SYMBOL (IC_LEFT (ic))))
762 werrorfl (ic->filename, ic->lineno,
764 OP_SYMBOL (IC_LEFT (ic))->name);
765 OP_REQV (IC_LEFT (ic)) = NULL;
766 OP_SYMBOL (IC_LEFT (ic))->allocreq = 1;
770 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
771 OP_REQV (IC_LEFT (ic)))
773 IC_LEFT (ic) = opFromOpWithDU (OP_REQV (IC_LEFT (ic)),
774 OP_SYMBOL (IC_LEFT (ic))->defs,
775 OP_SYMBOL (IC_LEFT (ic))->uses);
776 IC_LEFT (ic)->isaddr = 0;
782 /*-----------------------------------------------------------------*/
783 /* findReqv - search for a register equivalent */
784 /*-----------------------------------------------------------------*/
786 findReqv (symbol * prereqv, eBBlock ** ebbs, int count)
791 /* for all blocks do */
792 for (i=0; i<count; i++)
794 /* for all instructions in the block do */
795 for (ic = ebbs[i]->sch; ic; ic = ic->next)
799 if (IS_ITEMP (IC_COND (ic))
800 && OP_SYMBOL (IC_COND (ic))->prereqv == prereqv)
803 else if (ic->op == JUMPTABLE)
805 if (IS_ITEMP (IC_JTCOND (ic))
806 && OP_SYMBOL (IC_JTCOND (ic))->prereqv == prereqv)
807 return IC_JTCOND (ic);
811 if (IS_ITEMP (IC_LEFT (ic))
812 && OP_SYMBOL (IC_LEFT (ic))->prereqv == prereqv)
814 if (IS_ITEMP (IC_RIGHT (ic))
815 && OP_SYMBOL (IC_RIGHT (ic))->prereqv == prereqv)
816 return IC_RIGHT (ic);
817 if (IS_ITEMP (IC_RESULT (ic))
818 && OP_SYMBOL (IC_RESULT (ic))->prereqv == prereqv)
819 return IC_RESULT (ic);
827 /*-----------------------------------------------------------------*/
828 /* killDeadCode - eliminates dead assignments */
829 /*-----------------------------------------------------------------*/
831 killDeadCode (eBBlock ** ebbs, int count)
838 /* basic algorithm :- */
839 /* first the exclusion rules :- */
840 /* 1. if result is a global or volatile then skip */
841 /* 2. if assignment and result is a temp & isaddr then skip */
842 /* since this means array & pointer access, will be taken */
843 /* care of by alias analysis. */
844 /* 3. if the result is used in the remainder of the block skip */
845 /* 4. if this definition does not reach the end of the block */
846 /* i.e. the result is not present in the outExprs then KILL */
847 /* 5. if it reaches the end of block & is used by some success */
850 /* this whole process is carried on iteratively till no change */
855 /* for all blocks do */
856 for (i = 0; i < count; i++)
860 /* for all instructions in the block do */
861 for (ic = ebbs[i]->sch; ic; ic = ic->next)
869 ic->op == DUMMY_READ_VOLATILE ||
870 ic->op == CRITICAL ||
871 ic->op == ENDCRITICAL)
874 /* Since both IFX & JUMPTABLE (in SKIP_IC) have been tested for */
875 /* it is now safe to assume IC_LEFT, IC_RIGHT, & IC_RESULT are */
878 /* if the result is volatile then continue */
879 if (IC_RESULT (ic) && isOperandVolatile (IC_RESULT (ic), FALSE))
882 /* if the result is a temp & isaddr then skip */
883 if (IC_RESULT (ic) && POINTER_SET (ic))
886 if (POINTER_GET (ic) && IS_VOLATILE (operandType (IC_LEFT (ic))->next)
887 && !SPIL_LOC (IC_RESULT (ic)))
890 /* if the result is used in the remainder of the */
891 /* block then skip */
892 if (usedInRemaining (IC_RESULT (ic), ic->next))
895 /* does this definition reach the end of the block
896 or the usage is zero then we can kill */
897 if (!bitVectBitValue (ebbs[i]->outDefs, ic->key))
898 kill = 1; /* if not we can kill it */
901 /* if this is a global variable or function parameter */
902 /* we cannot kill anyway */
903 if (isOperandGlobal (IC_RESULT (ic)) ||
904 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
905 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
908 /* if we are sure there are no usages */
909 if (bitVectIsZero (OP_USES (IC_RESULT (ic))))
915 /* reset visited flag */
916 for (j = 0; j < count; ebbs[j++]->visited = 0);
918 /* find out if this definition is alive */
919 if (applyToSet (ebbs[i]->succList,
928 /* kill this one if required */
931 bool volLeft = IS_SYMOP (IC_LEFT (ic))
932 && isOperandVolatile (IC_LEFT (ic), FALSE);
933 bool volRight = IS_SYMOP (IC_RIGHT (ic))
934 && isOperandVolatile (IC_RIGHT (ic), FALSE);
936 /* a dead address-of operation should die, even if volatile */
937 if (ic->op == ADDRESS_OF)
940 if (ic->next && ic->seqPoint == ic->next->seqPoint
941 && (ic->next->op == '+' || ic->next->op == '-'))
943 if (isOperandEqual (IC_LEFT(ic), IC_LEFT(ic->next))
944 || isOperandEqual (IC_LEFT(ic), IC_RIGHT(ic->next)))
946 if (isOperandEqual (IC_RIGHT(ic), IC_LEFT(ic->next))
947 || isOperandEqual (IC_RIGHT(ic), IC_RIGHT(ic->next)))
951 if (POINTER_GET (ic) && IS_VOLATILE (operandType (IC_LEFT (ic))->next))
953 if (SPIL_LOC (IC_RESULT (ic)))
955 IC_RESULT (ic) = newiTempFromOp (IC_RESULT (ic));
956 SPIL_LOC (IC_RESULT (ic)) = NULL;
964 /* now delete from defUseSet */
965 deleteItemIf (&ebbs[i]->outExprs, ifDiCodeIsX, ic);
966 bitVectUnSetBit (ebbs[i]->outDefs, ic->key);
968 /* and defset of the block */
969 bitVectUnSetBit (ebbs[i]->defSet, ic->key);
971 /* If this is the last of a register equivalent, */
972 /* look for a successor register equivalent. */
973 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
974 if (IS_ITEMP (IC_RESULT (ic))
975 && OP_SYMBOL (IC_RESULT (ic))->isreqv
976 && bitVectIsZero (OP_DEFS (IC_RESULT (ic))))
978 symbol * resultsym = OP_SYMBOL (IC_RESULT (ic));
979 symbol * prereqv = resultsym->prereqv;
981 if (OP_SYMBOL (prereqv->reqv) == resultsym)
985 IC_RESULT (ic) = NULL;
986 newreqv = findReqv (prereqv, ebbs, count);
989 prereqv->reqv = newreqv;
994 /* delete the result */
995 IC_RESULT (ic) = NULL;
997 if (volLeft || volRight)
999 /* something is volatile, so keep the iCode */
1000 /* and change the operator instead */
1001 ic->op = DUMMY_READ_VOLATILE;
1003 /* keep only the volatile operands */
1005 IC_LEFT (ic) = NULL;
1007 IC_RIGHT (ic) = NULL;
1011 /* nothing is volatile, eliminate the iCode */
1012 remiCodeFromeBBlock (ebbs[i], ic);
1014 /* for the left & right remove the usage */
1015 if (IS_SYMOP (IC_LEFT (ic)))
1016 bitVectUnSetBit (OP_USES (IC_LEFT (ic)), ic->key);
1018 if (IS_SYMOP (IC_RIGHT (ic)))
1019 bitVectUnSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
1023 } /* end of all instructions */
1025 if (!ebbs[i]->sch && !ebbs[i]->noPath)
1026 disconBBlock (ebbs[i], ebbs, count);
1028 } /* end of for all blocks */
1032 } /* end of while(1) */
1037 /*-----------------------------------------------------------------*/
1038 /* printCyclomatic - prints the cyclomatic information */
1039 /*-----------------------------------------------------------------*/
1041 printCyclomatic (eBBlock ** ebbs, int count)
1043 int nEdges = elementsInSet (graphEdges);
1046 for (i = 0; i < count; i++)
1047 nNodes += (!ebbs[i]->noPath);
1049 /* print the information */
1050 werror (I_CYCLOMATIC, currFunc->name, nEdges, nNodes, nEdges - nNodes + 2);
1053 /*-----------------------------------------------------------------*/
1054 /* discardDeadParamReceives - remove any RECEIVE opcodes which */
1055 /* refer to dead variables. */
1056 /*-----------------------------------------------------------------*/
1058 discardDeadParamReceives (eBBlock ** ebbs, int count)
1064 for (i = 0; i < count; i++)
1066 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1068 if (ic->op == RECEIVE)
1070 if (IC_RESULT (ic) && OP_SYMBOL (IC_RESULT (ic))
1071 && !OP_SYMBOL (IC_RESULT (ic))->used)
1074 fprintf (stderr, "discarding dead receive for %s\n",
1075 OP_SYMBOL (IC_RESULT (ic))->name);
1077 dummyIcode.next = ic->next;
1078 remiCodeFromeBBlock (ebbs[i], ic);
1086 /*-----------------------------------------------------------------*/
1087 /* optimizeCastCast - remove unneeded intermediate casts. */
1088 /* Integer promotion may cast (un)signed char to int and then */
1089 /* recast the int to (un)signed long. If the signedness of the */
1090 /* char and long are the same, the cast can be safely performed in */
1091 /* a single step. */
1092 /*-----------------------------------------------------------------*/
1094 optimizeCastCast (eBBlock ** ebbs, int count)
1104 for (i = 0; i < count; i++)
1106 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1109 if (ic->op == CAST && IC_RESULT (ic) && IS_ITEMP (IC_RESULT (ic)))
1111 type1 = operandType (IC_RIGHT (ic));
1112 type2 = operandType (IC_RESULT (ic));
1114 /* Look only for a cast from an integer type to an */
1115 /* integer type that has no loss of bits */
1116 if (!IS_INTEGRAL (type1) || !IS_INTEGRAL (type2))
1118 if (getSize (type2) < getSize (type1))
1121 /* There must be only one use of this first result */
1122 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) != 1)
1125 /* This use must be a second cast */
1126 uic = hTabItemWithKey (iCodehTab,
1127 bitVectFirstBit (OP_USES (IC_RESULT (ic))));
1128 if (!uic || uic->op != CAST)
1131 /* It must be a cast to another integer type that */
1132 /* has no loss of bits */
1133 type3 = operandType (IC_RESULT (uic));
1134 if (!IS_INTEGRAL (type3))
1136 if (getSize (type3) < getSize (type2))
1139 /* The signedness between the first and last types */
1141 if (SPEC_USIGN (type3) != SPEC_USIGN (type1))
1144 /* Change the first cast to a simple assignment and */
1145 /* let the second cast do all the work */
1147 IC_LEFT (ic) = NULL;
1148 sym = OP_SYMBOL (IC_RESULT (ic));
1149 sym->type = copyLinkChain (type1);
1150 sym->etype = getSpec (sym->type);
1156 /*-----------------------------------------------------------------*/
1157 /* eBBlockFromiCode - creates extended basic blocks from iCode */
1158 /* will return an array of eBBlock pointers */
1159 /*-----------------------------------------------------------------*/
1161 eBBlockFromiCode (iCode * ic)
1163 eBBlock **ebbs = NULL;
1171 /* if nothing passed then return nothing */
1178 /* optimize the chain for labels & gotos
1179 this will eliminate redundant labels and
1180 will change jump to jumps by jumps */
1181 ic = iCodeLabelOptimize (ic);
1183 /* break it down into basic blocks */
1184 ebbs = iCodeBreakDown (ic, &count);
1187 /* hash the iCode keys so that we can quickly index */
1188 /* them in the rest of the optimization steps */
1189 setToNull ((void *) &iCodehTab);
1190 iCodehTab = newHashTable (iCodeKey);
1191 hashiCodeKeys (ebbs, count);
1193 /* compute the control flow */
1194 computeControlFlow (ebbs, count, 0);
1196 /* dumpraw if asked for */
1197 if (options.dump_raw)
1198 dumpEbbsToFileExt (DUMP_RAW0, ebbs, count);
1200 /* replace the local variables with their
1201 register equivalents : the liveRange computation
1202 along with the register allocation will determine
1203 if it finally stays in the registers */
1204 replaceRegEqv (ebbs, count);
1206 /* create loop regions */
1207 loops = createLoopRegions (ebbs, count);
1209 /* dumpraw if asked for */
1210 if (options.dump_raw)
1211 dumpEbbsToFileExt (DUMP_RAW1, ebbs, count);
1213 optimizeCastCast (ebbs, saveCount);
1215 /* do common subexpression elimination for each block */
1216 change = cseAllBlocks (ebbs, saveCount, FALSE);
1218 /* dumpraw if asked for */
1219 if (options.dump_raw)
1220 dumpEbbsToFileExt (DUMP_CSE, ebbs, count);
1222 /* compute the data flow */
1223 computeDataFlow (ebbs, saveCount);
1225 /* dumpraw if asked for */
1226 if (options.dump_raw)
1227 dumpEbbsToFileExt (DUMP_DFLOW, ebbs, count);
1229 /* global common subexpression elimination */
1230 if (optimize.global_cse)
1232 change += cseAllBlocks (ebbs, saveCount, FALSE);
1233 if (options.dump_gcse)
1234 dumpEbbsToFileExt (DUMP_GCSE, ebbs, saveCount);
1238 // compute the dataflow only
1239 assert(cseAllBlocks (ebbs, saveCount, TRUE)==0);
1241 /* kill dead code */
1242 kchange = killDeadCode (ebbs, saveCount);
1244 if (options.dump_kill)
1245 dumpEbbsToFileExt (DUMP_DEADCODE, ebbs, count);
1247 /* do loop optimizations */
1248 change += (lchange = loopOptimizations (loops, ebbs, count));
1249 if (options.dump_loop)
1250 dumpEbbsToFileExt (DUMP_LOOP, ebbs, count);
1252 /* recompute the data flow and apply global cse again
1253 if loops optimizations or dead code caused a change:
1254 loops will brings out of the loop which then may be
1255 available for use in the later blocks: dead code
1256 elimination could potentially disconnect some blocks
1257 conditional flow may be efected so we need to apply
1258 subexpression once more */
1259 if (lchange || kchange)
1261 computeDataFlow (ebbs, saveCount);
1262 change += cseAllBlocks (ebbs, saveCount, FALSE);
1263 if (options.dump_loop)
1264 dumpEbbsToFileExt (DUMP_LOOPG, ebbs, count);
1266 /* if loop optimizations caused a change then do
1267 dead code elimination once more : this will
1268 get rid of the extra assignments to the induction
1269 variables created during loop optimizations */
1270 killDeadCode (ebbs, saveCount);
1272 if (options.dump_loop)
1273 dumpEbbsToFileExt (DUMP_LOOPD, ebbs, count);
1277 /* sort it back by block number */
1278 qsort (ebbs, saveCount, sizeof (eBBlock *), bbNumCompare);
1280 if (!options.lessPedantic) {
1281 // this is a good place to check missing return values
1283 // the user is on his own with naked functions...
1284 if (!IS_VOID(currFunc->etype)
1285 && !FUNC_ISNAKED(currFunc->type)) {
1287 // make sure all predecessors of the last block end in a return
1288 for (bp=setFirstItem(ebbs[saveCount-1]->predList);
1290 bp=setNextItem(ebbs[saveCount-1]->predList)) {
1291 if (bp->ech->op != RETURN) {
1292 werrorfl (bp->ech->filename, bp->ech->lineno,
1293 W_VOID_FUNC, currFunc->name);
1300 /* if cyclomatic info requested then print it */
1301 if (options.cyclomatic)
1302 printCyclomatic (ebbs, saveCount);
1304 /* convert operations with support routines
1305 written in C to function calls : Iam doing
1306 this at this point since I want all the
1307 operations to be as they are for optimzations */
1308 convertToFcall (ebbs, count);
1310 /* compute the live ranges */
1311 computeLiveRanges (ebbs, count);
1313 if (options.dump_range)
1314 dumpEbbsToFileExt (DUMP_RANGE, ebbs, count);
1316 /* Now that we have the live ranges, discard parameter
1317 * receives for unused parameters.
1319 discardDeadParamReceives (ebbs, count);
1321 /* allocate registers & generate code */
1322 port->assignRegisters (ebbs, count);
1324 /* throw away blocks */
1325 setToNull ((void *) &graphEdges);
1332 /* (add-hook 'c-mode-hook (lambda () (setq c-basic-offset 4))) */