* device/lib/z80/printf.c: Changed emitter to volatile to work
[fw/sdcc] / src / SDCCcse.c
index 01f5ed656b84234a2a93b2b647646bde9aac4b92..a5ea44f393830ed470c338d37be4d7b054051f5a 100644 (file)
@@ -240,6 +240,7 @@ DEFSETFUNC (findCheaperOp)
   cseDef *cdp = item;
   V_ARG (operand *, cop);
   V_ARG (operand **, opp);
+  V_ARG (int, checkSign);
 
   /* if we have already found it */
   if (*opp)
@@ -302,15 +303,11 @@ DEFSETFUNC (findCheaperOp)
     *opp = IC_RESULT (cdp->diCode);
 
   if ((*opp) && 
-      (isOperandLiteral(*opp) || 
-#if 0 // jwk: because of bug #480645, this is clumsy anyway. E.g.
-       getSize(operandType(*opp)) == getSize(operandType(cop)) ||
-       (SPEC_USIGN(operandType (cop))==SPEC_USIGN(operandType (*opp)) &&
-       (SPEC_LONG(operandType (cop))==SPEC_LONG(operandType (*opp))))))
-#else
-       compareType(operandType(*opp), operandType(cop))==1))
-#endif
-    {
+      (isOperandLiteral(*opp) || !checkSign || 
+       (checkSign &&
+       (SPEC_USIGN(operandType (cop))==SPEC_USIGN(operandType (*opp)) &&
+        (SPEC_LONG(operandType (cop))==SPEC_LONG(operandType (*opp)))))))
+      {
 
       if ((isGlobalInNearSpace (cop) &&
           !isOperandLiteral (*opp)) ||
@@ -574,6 +571,20 @@ DEFSETFUNC (ifDiCodeIsX)
 
 }
 
+/*-----------------------------------------------------------------*/
+/* findBackwardDef - scan backwards to find deinition of operand   */
+/*-----------------------------------------------------------------*/
+iCode *findBackwardDef(operand *op,iCode *ic)
+{
+    iCode *lic;
+
+    for (lic = ic; lic ; lic = lic->prev) {
+       if (IC_RESULT(lic) && isOperandEqual(op,IC_RESULT(lic))) 
+           return lic;
+    }
+    return NULL;
+}
+
 /*-----------------------------------------------------------------*/
 /* algebraicOpts - does some algebraic optimizations               */
 /*-----------------------------------------------------------------*/
@@ -793,6 +804,7 @@ algebraicOpts (iCode * ic)
          IC_LEFT (ic) = NULL;
          IC_RESULT (ic) = operandFromOperand (IC_RESULT (ic));
          IC_RESULT (ic)->isaddr = 0;
+         break;
        }
       /* if this is a division then check if right */
       /* is one then change it to an assignment    */
@@ -898,6 +910,14 @@ updateSpillLocation (iCode * ic, int induction)
                                IC_RESULT (ic)->operand.symOperand;
        }
 
+#if 0 /* this needs furthur investigation can save a lot of code */
+       if (ASSIGN_SYM_TO_ITEMP(ic) &&
+           !SPIL_LOC(IC_RESULT(ic))) {
+           if (!OTHERS_PARM (OP_SYMBOL (IC_RIGHT (ic))))
+               SPIL_LOC (IC_RESULT (ic)) =
+                   IC_RIGHT (ic)->operand.symOperand;
+       }
+#endif
        if (ASSIGN_ITEMP_TO_ITEMP (ic)) {
          
                if (!SPIL_LOC (IC_RIGHT (ic)) &&
@@ -993,7 +1013,7 @@ ifxOptimize (iCode * ic, set * cseSet,
   if (!computeOnly)
     {
       pdop = NULL;
-      applyToSetFTrue (cseSet, findCheaperOp, IC_COND (ic), &pdop);
+      applyToSetFTrue (cseSet, findCheaperOp, IC_COND (ic), &pdop, 0);
       if (pdop)
        {
          IC_COND (ic) = pdop;
@@ -1111,7 +1131,7 @@ constFold (iCode * ic, set * cseSet)
   /* this check is a hueristic to prevent live ranges
      from becoming too long */
   if (IS_PTR (operandType (IC_RESULT (ic))))
-    return 0;
+      return 0;
 
   /* check if operation with a literal */
   if (!IS_OP_LITERAL (IC_RIGHT (ic)))
@@ -1265,6 +1285,55 @@ fixUpTypes (iCode * ic)
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* isSignedOp - will return 1 if sign is important to operation    */
+/*-----------------------------------------------------------------*/
+static int isSignedOp (iCode *ic)
+{
+    switch (ic->op) {
+    case '!':
+    case '~':
+    case UNARYMINUS:
+    case IPUSH:
+    case IPOP:
+    case CALL:
+    case PCALL:
+    case RETURN:
+    case '+':
+    case '-':
+    case EQ_OP:
+    case AND_OP:
+    case OR_OP:
+    case '^':
+    case '|':
+    case BITWISEAND:
+    case INLINEASM:
+    case LEFT_OP:
+    case GET_VALUE_AT_ADDRESS:
+    case '=':
+    case IFX:
+    case RECEIVE:
+    case SEND:
+       return 0;
+    case '*':
+    case '/':
+    case '%':
+    case '>':
+    case '<':
+    case LE_OP:
+    case GE_OP:
+    case NE_OP:
+    case RRC:
+    case RLC:
+    case GETHBIT:
+    case RIGHT_OP:
+    case CAST:
+    case ARRAYINIT:
+       return 1;
+    default:
+       return 0;
+    }
+ }
 /*-----------------------------------------------------------------*/
 /* cseBBlock - common subexpression elimination for basic blocks   */
 /*             this is the hackiest kludgiest routine in the whole */
@@ -1308,7 +1377,8 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       iCode *pdic;
       operand *pdop;
       iCode *defic;
-      
+      int checkSign ;
+
       ic->eBBlockNum = ebb->bbnum;
 
       if (SKIP_IC2 (ic))
@@ -1361,7 +1431,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
          if (!computeOnly)
            {
              pdop = NULL;
-             applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop);
+             applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop, 0);
              if (pdop)
                IC_LEFT (ic) = pdop;
            }
@@ -1443,12 +1513,14 @@ cseBBlock (eBBlock * ebb, int computeOnly,
              !(IS_BITFIELD (OP_SYMBOL (IC_RESULT (ic))->etype)))
            {
              pdop = NULL;
-             applyToSetFTrue (cseSet, findCheaperOp, IC_RESULT (ic), &pdop);
+             applyToSetFTrue (cseSet, findCheaperOp, IC_RESULT (ic), &pdop, 0);
              if (pdop && IS_ITEMP (pdop) && !computeOnly)
                IC_RESULT (ic) = pdop;
            }
        }
 
+      checkSign = isSignedOp(ic);
+
       /* do the operand lookup i.e. for both the */
       /* right & left operand : check the cseSet */
       /* to see if they have been replaced if yes */
@@ -1460,13 +1532,17 @@ cseBBlock (eBBlock * ebb, int computeOnly,
        {
 
          pdop = NULL;
-         applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop);
+         applyToSetFTrue (cseSet, findCheaperOp, IC_LEFT (ic), &pdop, checkSign);
          if (pdop)
            {
              if (POINTER_GET (ic))
                {
                  if (IS_ITEMP (pdop) || IS_OP_LITERAL (pdop))
                    {
+                       /* some non dominating block does POINTER_SET with
+                          this variable .. unsafe to remove any POINTER_GETs */
+                       if (bitVectBitValue(ebb->ndompset,IC_LEFT(ic)->key))
+                           ebb->ptrsSet = bitVectSetBit(ebb->ptrsSet,pdop->key);
                      IC_LEFT (ic) = pdop;
                      change = 1;
                    }
@@ -1497,7 +1573,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
        {
 
          pdop = NULL;
-         applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop);
+         applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop, checkSign);
          if (pdop)
            {
              IC_RIGHT (ic) = pdop;
@@ -1545,12 +1621,18 @@ cseBBlock (eBBlock * ebb, int computeOnly,
 
       /* Alternate code */
       if (pdic && IS_ITEMP(IC_RESULT(ic))) {
+       if (POINTER_GET(ic) && bitVectBitValue(ebb->ptrsSet,IC_LEFT(ic)->key)) {
+         /* Mmm, found an equivalent pointer get at a lower level. 
+            This could be a loop however with the same pointer set 
+            later on */
+       } else {
          /* if previous definition found change this to an assignment */
          ic->op = '=';
          IC_LEFT(ic) = NULL;
          IC_RIGHT(ic) = operandFromOperand(IC_RESULT(pdic));
          SET_ISADDR(IC_RESULT(ic),0);
          SET_ISADDR(IC_RIGHT (ic),0);    
+       }
       }
 
       if (!(POINTER_SET (ic)) && IC_RESULT (ic)) {