* src/SDCC.y, src/SDCCast.c, src/SDCCcse.c, src/SDCCglue.c, src/SDCCicode.c,
[fw/sdcc] / src / SDCCcse.c
index e56c84a581551a5fe36ceee9f9654e18a225f3b7..fc45babf2fe0f1a81f940bcc25148a993ba90314 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "common.h"
 #include "newalloc.h"
+#include "dbuf_string.h"
 
 
 /*-----------------------------------------------------------------*/
@@ -131,6 +132,7 @@ pcseDef (void *item, va_list ap)
 {
   cseDef *cdp = item;
   iCodeTable *icTab;
+  struct dbuf_s dbuf;
 
   (void) ap;
 
@@ -138,15 +140,19 @@ 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) && IS_SYMOP((*op)) && OP_SYMBOL((*op))->isreqv) {
     operand **rop = &OP_SYMBOL((*op))->usl.spillLoc->reqv;
@@ -173,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)
@@ -186,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;
@@ -202,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;
@@ -233,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) &&
@@ -243,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) &&
@@ -253,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;
         }
     }
 }
@@ -415,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)))
         {
@@ -460,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;
     }
@@ -1140,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);
@@ -1208,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)))
                 {
@@ -1860,7 +1892,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
 
           /* and also iTemps derived from globals */
           deleteItemIf (&cseSet, ifFromGlobal);
-          
+
           /* Delete iTemps derived from symbols whose address */
           /* has been taken */
           deleteItemIf (&cseSet, ifFromAddrTaken);
@@ -1948,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);
             }
         }
@@ -1965,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++;
             }
         }
 
@@ -1991,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;
@@ -2058,7 +2090,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
             }
         }
 
-      /*right operand */
+      /* right operand */
       if (IS_SYMOP (IC_RIGHT (ic)) && !computeOnly)
         {
 
@@ -2100,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;
@@ -2137,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))))
         {
@@ -2165,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 */
@@ -2179,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)))
@@ -2187,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);
@@ -2211,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                */