X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCcse.c;h=fc45babf2fe0f1a81f940bcc25148a993ba90314;hb=60c7254a378af70bb45e47881bfd0c02fc75c51b;hp=51dc99775f283b7a5f0d043f5f108d10f22dcb9f;hpb=6d5732239ca066c1757ae16f69942fd4b320e612;p=fw%2Fsdcc diff --git a/src/SDCCcse.c b/src/SDCCcse.c index 51dc9977..fc45babf 100644 --- a/src/SDCCcse.c +++ b/src/SDCCcse.c @@ -24,6 +24,7 @@ #include "common.h" #include "newalloc.h" +#include "dbuf_string.h" /*-----------------------------------------------------------------*/ @@ -42,6 +43,7 @@ newCseDef (operand * sym, iCode * ic) cdp->key = sym->key; cdp->ancestors = newBitVect(iCodeKey); cdp->fromGlobal = 0; + cdp->fromAddrTaken = 0; if (ic->op!=IF && ic->op!=JUMPTABLE) { @@ -49,11 +51,13 @@ newCseDef (operand * sym, iCode * ic) { bitVectSetBit (cdp->ancestors, IC_LEFT (ic)->key); cdp->fromGlobal |= isOperandGlobal (IC_LEFT (ic)); + cdp->fromAddrTaken |= OP_SYMBOL (IC_LEFT (ic))->addrtaken; } if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))) { bitVectSetBit (cdp->ancestors, IC_RIGHT (ic)->key); cdp->fromGlobal |= isOperandGlobal (IC_RIGHT (ic)); + cdp->fromAddrTaken |= OP_SYMBOL (IC_RIGHT (ic))->addrtaken; } } @@ -79,6 +83,7 @@ updateCseDefAncestors(cseDef *cdp, set * cseSet) { cdp->ancestors = bitVectUnion (cdp->ancestors, loop->ancestors); cdp->fromGlobal |= loop->fromGlobal; + cdp->fromAddrTaken |= loop->fromAddrTaken; break; } } @@ -93,6 +98,7 @@ updateCseDefAncestors(cseDef *cdp, set * cseSet) { cdp->ancestors = bitVectUnion (cdp->ancestors, loop->ancestors); cdp->fromGlobal |= loop->fromGlobal; + cdp->fromAddrTaken |= loop->fromAddrTaken; break; } } @@ -126,6 +132,7 @@ pcseDef (void *item, va_list ap) { cseDef *cdp = item; iCodeTable *icTab; + struct dbuf_s dbuf; (void) ap; @@ -133,17 +140,21 @@ pcseDef (void *item, va_list ap) fprintf (stdout, "**null op**"); printOperand (cdp->sym, stdout); icTab = getTableEntry (cdp->diCode->op); - icTab->iCodePrint (stdout, cdp->diCode, icTab->printName); + dbuf_init (&dbuf, 1024); + icTab->iCodePrint (&dbuf, cdp->diCode, icTab->printName); + dbuf_write_and_destroy (&dbuf, stdout); return 1; } void ReplaceOpWithCheaperOp(operand **op, operand *cop) { #ifdef RANGEHUNT - printf ("ReplaceOpWithCheaperOp %s with %s: ", - IS_SYMOP((*op)) ? OP_SYMBOL((*op))->name : "!SYM", - IS_SYMOP(cop) ? OP_SYMBOL(cop)->name : "!SYM"); + printf ("ReplaceOpWithCheaperOp\n\t"); + printOperand (*op, stdout); + printf ("\nwith\t"); + printOperand (cop, stdout); + // if op is a register equivalent - if (IS_ITEMP(cop) && OP_SYMBOL((*op))->isreqv) { + if (IS_ITEMP(cop) && IS_SYMOP((*op)) && OP_SYMBOL((*op))->isreqv) { operand **rop = &OP_SYMBOL((*op))->usl.spillLoc->reqv; if (isOperandEqual(*rop, *op)) { printf ("true"); @@ -168,9 +179,16 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) { iCode *lic; +#ifdef RANGEHUNT + printf ("replaceAllSymBySym\n\t"); + printOperand (from, stdout); + printf ("\nwith\t"); + printOperand (to, stdout); + printf ("\n"); +#endif for (lic = ic; lic; lic = lic->next) { - int siaddr; + int isaddr; /* do the special cases first */ if (lic->op == IFX) @@ -181,9 +199,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) bitVectUnSetBit (OP_USES (from), lic->key); OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key); - siaddr = IC_COND (lic)->isaddr; + isaddr = IC_COND (lic)->isaddr; IC_COND (lic) = operandFromOperand (to); - IC_COND (lic)->isaddr = siaddr; + IC_COND (lic)->isaddr = isaddr; } continue; @@ -197,9 +215,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) bitVectUnSetBit (OP_USES (from), lic->key); OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key); - siaddr = IC_COND (lic)->isaddr; + isaddr = IC_COND (lic)->isaddr; IC_JTCOND (lic) = operandFromOperand (to); - IC_JTCOND (lic)->isaddr = siaddr; + IC_JTCOND (lic)->isaddr = isaddr; } continue; @@ -228,9 +246,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) bitVectUnSetBit (OP_DEFS (from), lic->key); OP_DEFS(to)=bitVectSetBit (OP_DEFS (to), lic->key); } - siaddr = IC_RESULT (lic)->isaddr; + isaddr = IC_RESULT (lic)->isaddr; IC_RESULT (lic) = operandFromOperand (to); - IC_RESULT (lic)->isaddr = siaddr; + IC_RESULT (lic)->isaddr = isaddr; } if (IS_SYMOP (to) && @@ -238,9 +256,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) { bitVectUnSetBit (OP_USES (from), lic->key); OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key); - siaddr = IC_RIGHT (lic)->isaddr; + isaddr = IC_RIGHT (lic)->isaddr; IC_RIGHT (lic) = operandFromOperand (to); - IC_RIGHT (lic)->isaddr = siaddr; + IC_RIGHT (lic)->isaddr = isaddr; } if (IS_SYMOP (to) && @@ -248,9 +266,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset) { bitVectUnSetBit (OP_USES (from), lic->key); OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key); - siaddr = IC_LEFT (lic)->isaddr; + isaddr = IC_LEFT (lic)->isaddr; IC_LEFT (lic) = operandFromOperand (to); - IC_LEFT (lic)->isaddr = siaddr; + IC_LEFT (lic)->isaddr = isaddr; } } } @@ -410,6 +428,15 @@ DEFSETFUNC (findCheaperOp) (*opp)->isaddr = cop->isaddr; } + /* copy signedness to literal operands */ + if (IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) + && isOperandLiteral(*opp) + && SPEC_NOUN(operandType(*opp)) == SPEC_NOUN(operandType(cop)) + && SPEC_USIGN(operandType(*opp)) != SPEC_USIGN(operandType(cop))) + { + SPEC_USIGN(operandType(*opp)) = SPEC_USIGN(operandType(cop)); + } + if (IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) && SPEC_NOUN(operandType(cop)) != SPEC_NOUN(operandType(*opp))) { @@ -455,6 +482,16 @@ DEFSETFUNC (findPointerSet) getSize (operandType (IC_RIGHT (cdp->diCode))) == getSize (operandType (rop))) { + if (IS_SPEC (operandType (IC_RIGHT (cdp->diCode))) && + SPEC_USIGN (operandType (IC_RIGHT (cdp->diCode))) != + SPEC_USIGN (operandType (rop))) + { + /* bug #1493710 + Reminder for Bernhard: check of signedness + could be unnecessary together with 'checkSign', if + signedness of operation is stored in ic */ + return 0; + } *opp = IC_RIGHT (cdp->diCode); return 1; } @@ -487,7 +524,7 @@ DEFSETFUNC (findPrevIc) /* if iCodes are not the same */ /* see the operands maybe interchanged */ if (ic->op == cdp->diCode->op && - (ic->op == '+' || ic->op == '*') && + IS_ASSOCIATIVE(ic) && isOperandEqual (IC_LEFT (ic), IC_RIGHT (cdp->diCode)) && isOperandEqual (IC_RIGHT (ic), IC_LEFT (cdp->diCode))) { @@ -532,6 +569,18 @@ DEFSETFUNC (ifDefGlobal) return (isOperandGlobal (cdp->sym)); } +/*-------------------------------------------------------------------*/ +/* ifFromAddrTaken - if definition is derived from a symbol whose */ +/* address was taken */ +/*-------------------------------------------------------------------*/ +DEFSETFUNC (ifFromAddrTaken) +{ + cseDef *cdp = item; + + return cdp->fromAddrTaken; +} + + /*-----------------------------------------------------------------*/ /* ifAnyGetPointer - if get pointer icode */ /*-----------------------------------------------------------------*/ @@ -801,7 +850,8 @@ algebraicOpts (iCode * ic, eBBlock * ebp) case '+': /* if adding the same thing change to left shift by 1 */ if (IC_LEFT (ic)->key == IC_RIGHT (ic)->key && - !IS_FLOAT (operandType (IC_RESULT (ic)))) + !(IS_FLOAT (operandType (IC_RESULT (ic))) + || IS_FIXED(operandType (IC_RESULT (ic))))) { ic->op = LEFT_OP; IC_RIGHT (ic) = operandFromLit (1); @@ -812,8 +862,10 @@ algebraicOpts (iCode * ic, eBBlock * ebp) if (IS_OP_LITERAL (IC_LEFT (ic)) && operandLitValue (IC_LEFT (ic)) == 0.0) { - if (compareType (operandType (IC_RESULT (ic)), - operandType (IC_RIGHT (ic)))<0) + int typematch; + typematch = compareType (operandType (IC_RESULT (ic)), + operandType (IC_RIGHT (ic))); + if (typematch<0) { ic->op = CAST; IC_LEFT (ic) = operandFromLink (operandType (IC_RESULT (ic))); @@ -822,6 +874,12 @@ algebraicOpts (iCode * ic, eBBlock * ebp) { ic->op = '='; IC_LEFT (ic) = NULL; + if (typematch==0) + { + /* for completely different types, preserve the source type */ + IC_RIGHT (ic) = operandFromOperand (IC_RIGHT (ic)); + setOperandType (IC_RIGHT (ic), operandType (IC_RESULT (ic))); + } } SET_ISADDR (IC_RESULT (ic), 0); SET_ISADDR (IC_RIGHT (ic), 0); @@ -830,8 +888,10 @@ algebraicOpts (iCode * ic, eBBlock * ebp) if (IS_OP_LITERAL (IC_RIGHT (ic)) && operandLitValue (IC_RIGHT (ic)) == 0.0) { - if (compareType (operandType (IC_RESULT (ic)), - operandType (IC_LEFT (ic)))<0) + int typematch; + typematch = compareType (operandType (IC_RESULT (ic)), + operandType (IC_LEFT (ic))); + if (typematch<0) { ic->op = CAST; IC_RIGHT (ic) = IC_LEFT (ic); @@ -842,6 +902,12 @@ algebraicOpts (iCode * ic, eBBlock * ebp) ic->op = '='; IC_RIGHT (ic) = IC_LEFT (ic); IC_LEFT (ic) = NULL; + if (typematch==0) + { + /* for completely different types, preserve the source type */ + IC_RIGHT (ic) = operandFromOperand (IC_RIGHT (ic)); + setOperandType (IC_RIGHT (ic), operandType (IC_RESULT (ic))); + } } SET_ISADDR (IC_RIGHT (ic), 0); SET_ISADDR (IC_RESULT (ic), 0); @@ -1106,7 +1172,7 @@ algebraicOpts (iCode * ic, eBBlock * ebp) default: return; } - if (((unsigned) operandLitValue (IC_RIGHT (ic)) & val) == val) + if (((unsigned) double2ul (operandLitValue (IC_RIGHT (ic))) & val) == val) { ic->op = '='; IC_RIGHT (ic) = IC_LEFT (ic); @@ -1174,7 +1240,7 @@ algebraicOpts (iCode * ic, eBBlock * ebp) default: return; } - if (((unsigned) operandLitValue (IC_RIGHT (ic)) & val) == val) + if (((unsigned) double2ul (operandLitValue (IC_RIGHT (ic))) & val) == val) { if (IS_OP_VOLATILE (IC_LEFT (ic))) { @@ -1376,7 +1442,7 @@ void ifxOptimize (iCode * ic, set * cseSet, int computeOnly, eBBlock * ebb, int *change, - eBBlock ** ebbs, int count) + ebbIndex * ebbi) { operand *pdop; symbol *label; @@ -1428,7 +1494,7 @@ ifxOptimize (iCode * ic, set * cseSet, /* this is very expensive but it does not happen */ /* too often, if it does happen then the user pays */ /* the price */ - computeControlFlow (ebbs, count, 1); + computeControlFlow (ebbi); if (!options.lessPedantic) { werrorfl (ic->filename, ic->lineno, W_CONTROL_FLOW); } @@ -1440,7 +1506,7 @@ ifxOptimize (iCode * ic, set * cseSet, we can remove this conditional statement */ label = (IC_TRUE (ic) ? IC_TRUE (ic) : IC_FALSE (ic)); if (elementsInSet (ebb->succList) == 1 && - isinSet (ebb->succList, eBBWithEntryLabel (ebbs, label, count))) + isinSet (ebb->succList, eBBWithEntryLabel (ebbi, label))) { if (!options.lessPedantic) { @@ -1456,7 +1522,7 @@ ifxOptimize (iCode * ic, set * cseSet, else { remiCodeFromeBBlock (ebb, ic); - computeControlFlow (ebbs, count, 1); + computeControlFlow (ebbi); return; } } @@ -1518,7 +1584,7 @@ constFold (iCode * ic, set * cseSet) ic->op != '-') return 0; - /* this check is a hueristic to prevent live ranges + /* this check is a heuristic to prevent live ranges from becoming too long */ if (IS_PTR (operandType (IC_RESULT (ic)))) return 0; @@ -1528,7 +1594,7 @@ constFold (iCode * ic, set * cseSet) return 0; /* check if we can find a definition for the - right hand side */ + left hand side */ if (!(applyToSet (cseSet, diCodeForSym, IC_LEFT (ic), &dic))) return 0; @@ -1718,6 +1784,9 @@ static int isSignedOp (iCode *ic) case RRC: case RLC: case GETHBIT: + case GETABIT: + case GETBYTE: + case GETWORD: case RIGHT_OP: case CAST: case ARRAYINIT: @@ -1751,8 +1820,10 @@ dumpCseSet(set *cseSet) /*-----------------------------------------------------------------*/ int cseBBlock (eBBlock * ebb, int computeOnly, - eBBlock ** ebbs, int count) + ebbIndex * ebbi) { + eBBlock ** ebbs = ebbi->bbOrder; + int count = ebbi->count; set *cseSet; iCode *ic; int change = 0; @@ -1819,9 +1890,13 @@ cseBBlock (eBBlock * ebb, int computeOnly, since they can be modified by the function call */ deleteItemIf (&cseSet, ifDefGlobal); - /* and also itemps derived from globals */ + /* and also iTemps derived from globals */ deleteItemIf (&cseSet, ifFromGlobal); + /* Delete iTemps derived from symbols whose address */ + /* has been taken */ + deleteItemIf (&cseSet, ifFromAddrTaken); + /* delete all getpointer iCodes from cseSet, this should be done only for global arrays & pointers but at this point we don't know if globals, so to be safe do all */ @@ -1905,11 +1980,11 @@ cseBBlock (eBBlock * ebb, int computeOnly, IC_LEFT (ic)->aggr2ptr = 0; fixUpTypes (ic); } - else if (IC_LEFT (ic)->aggr2ptr) + else if (IC_LEFT (ic)->aggr2ptr == 1) {/* band aid for kludge */ setOperandType (IC_LEFT (ic), aggrToPtr (operandType (IC_LEFT (ic)), TRUE)); - IC_LEFT (ic)->aggr2ptr = 0; + IC_LEFT (ic)->aggr2ptr++; fixUpTypes (ic); } } @@ -1922,11 +1997,11 @@ cseBBlock (eBBlock * ebb, int computeOnly, aggrToPtr (operandType (IC_RESULT (ic)), FALSE)); IC_RESULT (ic)->aggr2ptr = 0; } - else if (IC_RESULT (ic)->aggr2ptr) + else if (IC_RESULT (ic)->aggr2ptr == 1) {/* band aid for kludge */ setOperandType (IC_RESULT (ic), aggrToPtr (operandType (IC_RESULT (ic)), TRUE)); - IC_RESULT (ic)->aggr2ptr = 0; + IC_RESULT (ic)->aggr2ptr++; } } @@ -1936,7 +2011,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, { ifxOptimize (ic, cseSet, computeOnly, ebb, &change, - ebbs, count); + ebbi); continue; } @@ -1948,7 +2023,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, /* update the spill location for this */ updateSpillLocation (ic,0); - if (POINTER_SET (ic) && + if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic)) && !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype))) { pdop = NULL; @@ -1974,6 +2049,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, /* left operand */ /* and left is a symbol */ if (IS_SYMOP (IC_LEFT (ic)) && + !IS_BITFIELD (OP_SYM_ETYPE (IC_LEFT (ic))) && !computeOnly && ic->op != ADDRESS_OF) { @@ -2014,7 +2090,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, } } - /*right operand */ + /* right operand */ if (IS_SYMOP (IC_RIGHT (ic)) && !computeOnly) { @@ -2035,7 +2111,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, /* if after all this it becomes an assignment to self then delete it and continue */ - if (ASSIGNMENT_TO_SELF (ic)) + if (ASSIGNMENT_TO_SELF (ic) && !isOperandVolatile (IC_RIGHT(ic), FALSE)) { remiCodeFromeBBlock (ebb, ic); continue; @@ -2056,7 +2132,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, IS_ITEMP (IC_RESULT (ic)) && !computeOnly) { - applyToSet (cseSet, findPrevIc, ic, &pdic); + applyToSet (cseSet, findPrevIc, ic, &pdic); if (pdic && compareType (operandType (IC_RESULT (pdic)), operandType (IC_RESULT (ic))) != 1) pdic = NULL; @@ -2093,6 +2169,7 @@ cseBBlock (eBBlock * ebb, int computeOnly, mine and type is a pointer then delete pointerGets to take care of aliasing */ if (ASSIGNMENT (ic) && + IS_SYMOP (IC_RESULT (ic)) && OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))) && IS_PTR (operandType (IC_RESULT (ic)))) { @@ -2121,12 +2198,11 @@ cseBBlock (eBBlock * ebb, int computeOnly, /* delete from the cseSet anything that has */ /* operands matching the result of this */ /* except in case of pointer access */ - if (!(POINTER_SET (ic)) && IC_RESULT (ic)) + if (!(POINTER_SET (ic)) && IS_SYMOP (IC_RESULT (ic))) { deleteItemIf (&cseSet, ifOperandsHave, IC_RESULT (ic)); /* delete any previous definitions */ ebb->defSet = bitVectCplAnd (ebb->defSet, OP_DEFS (IC_RESULT (ic))); - } /* add the left & right to the defUse set */ @@ -2135,7 +2211,6 @@ cseBBlock (eBBlock * ebb, int computeOnly, OP_USES(IC_LEFT (ic))= bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key); setUsesDefs (IC_LEFT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs); - } if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))) @@ -2143,12 +2218,11 @@ cseBBlock (eBBlock * ebb, int computeOnly, OP_USES(IC_RIGHT (ic))= bitVectSetBit (OP_USES (IC_RIGHT (ic)), ic->key); setUsesDefs (IC_RIGHT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs); - } /* for the result it is special case, put the result */ /* in the defuseSet if it a pointer or array access */ - if (POINTER_SET (defic)) + if (POINTER_SET (defic) && IS_SYMOP (IC_RESULT (ic))) { OP_USES(IC_RESULT (ic))= bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key); @@ -2167,16 +2241,18 @@ cseBBlock (eBBlock * ebb, int computeOnly, addSetHead (&ptrSetSet, newCseDef (IC_RESULT (ic), ic)); } else - /* add the result to defintion set */ if (IC_RESULT (ic)) { - OP_DEFS(IC_RESULT (ic))= - bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key); - ebb->defSet = bitVectSetBit (ebb->defSet, ic->key); - ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic))); - ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key); + /* add the result to definition set */ + if (IS_SYMOP (IC_RESULT (ic))) + { + OP_DEFS(IC_RESULT (ic))= + bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key); + ebb->defSet = bitVectSetBit (ebb->defSet, ic->key); + ebb->outDefs = bitVectCplAnd (ebb->outDefs, OP_DEFS (IC_RESULT (ic))); + ebb->ldefs = bitVectSetBit (ebb->ldefs, ic->key); + } } - /* if this is an addressof instruction then */ /* put the symbol in the address of list & */ /* delete it from the cseSet */ @@ -2203,15 +2279,17 @@ cseBBlock (eBBlock * ebb, int computeOnly, /* cseAllBlocks - will sequentially go thru & do cse for all blocks */ /*-----------------------------------------------------------------*/ int -cseAllBlocks (eBBlock ** ebbs, int count, int computeOnly) +cseAllBlocks (ebbIndex * ebbi, int computeOnly) { + eBBlock ** ebbs = ebbi->dfOrder; + int count = ebbi->count; int i; int change = 0; /* if optimization turned off */ for (i = 0; i < count; i++) - change += cseBBlock (ebbs[i], computeOnly, ebbs, count); + change += cseBBlock (ebbs[i], computeOnly, ebbi); return change; }