IC_RESULT needs to be a symbol
[fw/sdcc] / src / SDCCcse.c
index 43c469a25e5b6298bda9034263fbc8e29cca0404..af3d2ce94a0372abbfc4b8bcac47fcb002eecb51 100644 (file)
@@ -1,3 +1,9 @@
+//#define LIVERANGEHUNT
+#ifdef LIVERANGEHUNT
+  #define LRH(x) x
+#else
+  #define LRH(x)
+#endif
 /*-------------------------------------------------------------------------
   SDCCcse.c - source file for Common Subexpressions and other utility
 
@@ -34,7 +40,7 @@ newCseDef (operand * sym, iCode * ic)
   cseDef *cdp;
 
   assert (sym);
-  cdp = Safe_calloc (1, sizeof (cseDef));
+  cdp = Safe_alloc (sizeof (cseDef));
 
   cdp->sym = sym;
   cdp->diCode = ic;
@@ -81,6 +87,28 @@ pcseDef (void *item, va_list ap)
   return 1;
 }
 
+void ReplaceOpWithCheaperOp(operand **op, operand *cop) {
+#ifdef RANGEHUNT
+  printf ("ReplaceOpWithCheaperOp (%s:%d with %s:%d): ", 
+         OP_SYMBOL((*op))->name, OP_SYMBOL((*op))->isreqv,
+         OP_SYMBOL(cop)->name, OP_SYMBOL(cop)->isreqv);
+  // if op is a register equivalent
+  if (IS_ITEMP(cop) && OP_SYMBOL((*op))->isreqv) {
+    operand **rop = &OP_SYMBOL((*op))->usl.spillLoc->reqv;
+    if (isOperandEqual(*rop, *op)) {
+      printf ("true");
+      *rop=cop;
+      OP_SYMBOL((*op))->isreqv=0;
+      OP_SYMBOL(cop)->isreqv=1;
+    } else {
+      printf ("false");
+    }
+  }
+  printf ("\n");
+#endif
+  *op=cop;
+}
+
 /*-----------------------------------------------------------------*/
 /* replaceAllSymBySym - replaces all operands by operand in an     */
 /*                      instruction chain                          */
@@ -90,6 +118,9 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
 {
   iCode *lic;
 
+  LRH(printf ("replaceAllSymBySym: from %s to %s\n", 
+             OP_SYMBOL(from)->name, 
+             IS_SYMOP(to) ? OP_SYMBOL(to)->name) : "!SYM");
   for (lic = ic; lic; lic = lic->next)
     {
       int siaddr;
@@ -102,7 +133,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
            {
 
              bitVectUnSetBit (OP_USES (from), lic->key);
-             OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
+             OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
              siaddr = IC_COND (lic)->isaddr;
              IC_COND (lic) = operandFromOperand (to);
              IC_COND (lic)->isaddr = siaddr;
@@ -118,7 +149,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
            {
 
              bitVectUnSetBit (OP_USES (from), lic->key);
-             OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
+             OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
              siaddr = IC_COND (lic)->isaddr;
              IC_JTCOND (lic) = operandFromOperand (to);
              IC_JTCOND (lic)->isaddr = siaddr;
@@ -127,13 +158,14 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
          continue;
        }
 
-      if (IC_RESULT (lic) && IC_RESULT (lic)->key == from->key)
+      if (IS_SYMOP(to) && 
+         IC_RESULT (lic) && IC_RESULT (lic)->key == from->key)
        {
          /* maintain du chains */
          if (POINTER_SET (lic))
            {
              bitVectUnSetBit (OP_USES (from), lic->key);
-             OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
+             OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
 
              /* also check if the "from" was in the non-dominating
                 pointer sets and replace it with "to" in the bitVector */
@@ -147,7 +179,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
          else
            {
              bitVectUnSetBit (OP_DEFS (from), lic->key);
-             OP_DEFS (to) = bitVectSetBit (OP_DEFS (to), lic->key);
+             OP_DEFS(to)=bitVectSetBit (OP_DEFS (to), lic->key);
            }
          siaddr = IC_RESULT (lic)->isaddr;
          IC_RESULT (lic) = operandFromOperand (to);
@@ -158,7 +190,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
          IC_RIGHT (lic) && IC_RIGHT (lic)->key == from->key)
        {
          bitVectUnSetBit (OP_USES (from), lic->key);
-         OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
+         OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
          siaddr = IC_RIGHT (lic)->isaddr;
          IC_RIGHT (lic) = operandFromOperand (to);
          IC_RIGHT (lic)->isaddr = siaddr;
@@ -168,7 +200,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
          IC_LEFT (lic) && IC_LEFT (lic)->key == from->key)
        {
          bitVectUnSetBit (OP_USES (from), lic->key);
-         OP_USES (to) = bitVectSetBit (OP_USES (to), lic->key);
+         OP_USES(to)=bitVectSetBit (OP_USES (to), lic->key);
          siaddr = IC_LEFT (lic)->isaddr;
          IC_LEFT (lic) = operandFromOperand (to);
          IC_LEFT (lic)->isaddr = siaddr;
@@ -240,6 +272,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)
@@ -253,40 +286,40 @@ DEFSETFUNC (findCheaperOp)
       /* do a special check this will help in */
       /* constant propagation & dead code elim */
       /* for assignments only                 */
-      if (cdp->diCode->op == '=')
-       {
-         /* if the result is volatile then return result */
-         if (IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
-           *opp = IC_RESULT (cdp->diCode);
-         else
-           /* if this is a straight assignment and
-              left is a temp then prefer the temporary to the
-              true symbol */
-           if (!POINTER_SET (cdp->diCode) &&
-               IS_ITEMP (IC_RESULT (cdp->diCode)) &&
-               IS_TRUE_SYMOP (IC_RIGHT (cdp->diCode)))
+      if (cdp->diCode->op == '=') {
+       /* if the result is volatile then return result */
+       if (IS_OP_VOLATILE (IC_RESULT (cdp->diCode)))
+         *opp = IC_RESULT (cdp->diCode);
+       else 
+         /* if this is a straight assignment and
+            left is a temp then prefer the temporary to the
+            true symbol */
+         if (!POINTER_SET (cdp->diCode) &&
+             IS_ITEMP (IC_RESULT (cdp->diCode)) &&
+             IS_TRUE_SYMOP (IC_RIGHT (cdp->diCode)))
            *opp = IC_RESULT (cdp->diCode);
-         else
+         else {
            /* if straight assignement && and both
               are temps then prefer the one that
               will not need extra space to spil, also
               take into consideration if right side
               an induction variable
-            */
+           */
            if (!POINTER_SET (cdp->diCode) &&
                IS_ITEMP (IC_RESULT (cdp->diCode)) &&
                IS_ITEMP (IC_RIGHT (cdp->diCode)) &&
                !OP_SYMBOL (IC_RIGHT (cdp->diCode))->isind &&
+               !OP_SYMBOL(IC_RIGHT (cdp->diCode))->isreqv &&
                ((!SPIL_LOC (IC_RIGHT (cdp->diCode)) &&
                  SPIL_LOC (IC_RESULT (cdp->diCode))) ||
                 (SPIL_LOC (IC_RESULT (cdp->diCode)) &&
                  SPIL_LOC (IC_RESULT (cdp->diCode)) ==
-                 SPIL_LOC (IC_RIGHT (cdp->diCode))))
-           )
-           *opp = IC_RESULT (cdp->diCode);
-         else
-           *opp = IC_RIGHT (cdp->diCode);
-       }
+                 SPIL_LOC (IC_RIGHT (cdp->diCode)))))
+             *opp = IC_RESULT (cdp->diCode);
+           else
+             *opp = IC_RIGHT (cdp->diCode);
+         }
+      }
       else
        *opp = IC_RESULT (cdp->diCode);
     }
@@ -301,8 +334,13 @@ DEFSETFUNC (findCheaperOp)
       IS_ITEMP (IC_RESULT (cdp->diCode)))
     *opp = IC_RESULT (cdp->diCode);
 
-  if (*opp)
-    {
+  if ((*opp) && 
+      (isOperandLiteral(*opp) || !checkSign || 
+       (checkSign &&
+       IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) &&
+       (SPEC_USIGN(operandType (cop))==SPEC_USIGN(operandType (*opp)) &&
+        (SPEC_LONG(operandType (cop))==SPEC_LONG(operandType (*opp)))))))
+      {
 
       if ((isGlobalInNearSpace (cop) &&
           !isOperandLiteral (*opp)) ||
@@ -324,11 +362,35 @@ DEFSETFUNC (findCheaperOp)
          *opp = operandFromOperand (*opp);
          (*opp)->isaddr = cop->isaddr;
        }
-
+         
+      if (IS_SPEC(operandType (cop)) && IS_SPEC(operandType (*opp)) &&
+         SPEC_NOUN(operandType(cop)) != SPEC_NOUN(operandType(*opp)))
+       {
+           // special case: we can make an unsigned char literal 
+           // into an int literal with no cost.
+           if (isOperandLiteral(*opp)
+            && SPEC_NOUN(operandType(*opp)) == V_CHAR
+            && SPEC_NOUN(operandType(cop)) == V_INT)
+           {
+               *opp = operandFromOperand (*opp);
+               SPEC_NOUN(operandType(*opp)) = V_INT;
+           }
+           else
+           {
+               // No clue...
+               *opp = NULL;
+               return 0;
+           }
+           
+        }
+         
+      LRH(printf ("findCheaperOp: %s < %s\n",\
+             IS_SYMOP((*opp)) ? OP_SYMBOL((*opp))->name : "!SYM",\
+             OP_SYMBOL(cop)->name));
       return 1;
 
     }
-
+  *opp=NULL;
   return 0;
 }
 
@@ -374,7 +436,8 @@ DEFSETFUNC (findPrevIc)
   if (isiCodeEqual (ic, cdp->diCode) &&
       isOperandEqual (cdp->sym, IC_RESULT (cdp->diCode)))
     {
-      *icp = cdp->diCode;
+      LRH(printf ("findPrevIc same: %d %d\n", ic->key, cdp->diCode->key));
+       *icp = cdp->diCode;
       return 1;
     }
 
@@ -385,6 +448,7 @@ DEFSETFUNC (findPrevIc)
       isOperandEqual (IC_LEFT (ic), IC_RIGHT (cdp->diCode)) &&
       isOperandEqual (IC_RIGHT (ic), IC_LEFT (cdp->diCode)))
     {
+      LRH(printf ("findPrevIc inter: %d %d\n", ic->key, cdp->diCode->key));
       *icp = cdp->diCode;
       return 1;
     }
@@ -392,6 +456,20 @@ DEFSETFUNC (findPrevIc)
   return 0;
 }
 
+/*-------------------------------------------------------------------*/
+/* ifAssignedFromGlobal - if definition is an assignment from global */
+/*-------------------------------------------------------------------*/
+DEFSETFUNC (ifAssignedFromGlobal)
+{
+  cseDef *cdp = item;
+  iCode *dic=cdp->diCode;
+
+  if (dic->op=='=' && isOperandGlobal(IC_RIGHT(dic))) {
+    return 1;
+  }
+  return 0;
+}
+
 /*-----------------------------------------------------------------*/
 /* ifDefGlobal - if definition is global                           */
 /*-----------------------------------------------------------------*/
@@ -552,6 +630,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               */
 /*-----------------------------------------------------------------*/
@@ -771,6 +863,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    */
@@ -809,25 +902,28 @@ algebraicOpts (iCode * ic)
        }
       break;
     case CAST:
-      /* if this is a cast of a literal value */
-      if (IS_OP_LITERAL (IC_RIGHT (ic)))
-       {
-         ic->op = '=';
-         IC_RIGHT (ic) =
-           operandFromValue (valCastLiteral (operandType (IC_LEFT (ic)),
-                                         operandLitValue (IC_RIGHT (ic))));
-         IC_LEFT (ic) = NULL;
-         SET_ISADDR (IC_RESULT (ic), 0);
-       }
-      /* if casting to the same */
-      if (checkType (operandType (IC_RESULT (ic)),
-                    operandType (IC_RIGHT (ic))) == 1)
-       {
-         ic->op = '=';
-         IC_LEFT (ic) = NULL;
-         SET_ISADDR (IC_RESULT (ic), 0);
-       }
-      break;
+           {
+                   sym_link *otype = operandType(IC_RIGHT(ic));
+                   sym_link *ctype = operandType(IC_LEFT(ic));
+                   /* if this is a cast of a literal value */
+                   if (IS_OP_LITERAL (IC_RIGHT (ic)) &&
+                       !(IS_GENPTR(ctype) && (IS_PTR(otype) && !IS_GENPTR(otype)))) {
+                           ic->op = '=';
+                           IC_RIGHT (ic) =
+                                   operandFromValue (valCastLiteral (operandType (IC_LEFT (ic)),
+                                                                     operandLitValue (IC_RIGHT (ic))));
+                           IC_LEFT (ic) = NULL;
+                           SET_ISADDR (IC_RESULT (ic), 0);
+                   }
+                   /* if casting to the same */
+                   if (compareType (operandType (IC_RESULT (ic)),
+                                    operandType (IC_RIGHT (ic))) == 1) {
+                           ic->op = '=';
+                           IC_LEFT (ic) = NULL;
+                           SET_ISADDR (IC_RESULT (ic), 0);
+                   }
+           }
+           break;
     case '!':
       if (IS_OP_LITERAL (IC_LEFT (ic)))
        {
@@ -847,51 +943,68 @@ algebraicOpts (iCode * ic)
 /* updateSpillLocation - keeps track of register spill location    */
 /*-----------------------------------------------------------------*/
 void 
-updateSpillLocation (iCode * ic)
+updateSpillLocation (iCode * ic, int induction)
 {
+       sym_link *setype;
 
-  sym_link *setype;
+       if (POINTER_SET (ic))
+               return;
 
-  if (POINTER_SET (ic))
-    return;
+       if (ic->nosupdate)
+               return;
 
-  if (ic->nosupdate)
-    return;
-
-  /* for the form true_symbol := iTempNN */
-  if (ASSIGN_ITEMP_TO_SYM (ic)
-      && !SPIL_LOC (IC_RIGHT (ic)))
-    {
-
-      setype = getSpec (operandType (IC_RESULT (ic)));
+       /* for the form true_symbol := iTempNN */
+       if (ASSIGN_ITEMP_TO_SYM (ic) && 
+           !SPIL_LOC (IC_RIGHT (ic))) {
 
-      if (!IC_RIGHT (ic)->noSpilLoc &&
-         !IS_VOLATILE (setype) &&
-         !IN_FARSPACE (SPEC_OCLS (setype)) &&
-         !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
+               setype = getSpec (operandType (IC_RESULT (ic)));
 
-       SPIL_LOC (IC_RIGHT (ic)) =
-         IC_RESULT (ic)->operand.symOperand;
-    }
-
-  if (ASSIGN_ITEMP_TO_ITEMP (ic) &&
-      !SPIL_LOC (IC_RIGHT (ic)) &&
-  !bitVectBitsInCommon (OP_DEFS (IC_RIGHT (ic)), OP_USES (IC_RESULT (ic))) &&
-      OP_SYMBOL (IC_RESULT (ic))->isreqv)
-    {
-
-      setype = getSpec (operandType (IC_RESULT (ic)));
-
-      if (!IC_RIGHT (ic)->noSpilLoc &&
-         !IS_VOLATILE (setype) &&
-         !IN_FARSPACE (SPEC_OCLS (setype)) &&
-         !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
+               if (!OP_SYMBOL(IC_RIGHT (ic))->noSpilLoc &&
+                   !IS_VOLATILE (setype) &&
+                   !IN_FARSPACE (SPEC_OCLS (setype)) &&
+                   !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
+               {
+                   wassert(IS_SYMOP(IC_RESULT (ic)));
+                   wassert(IS_SYMOP(IC_RIGHT (ic)));
+                       SPIL_LOC (IC_RIGHT (ic)) =
+                               IC_RESULT (ic)->operand.symOperand;
+               }
+           
+       }
 
-       SPIL_LOC (IC_RIGHT (ic)) =
-         SPIL_LOC (IC_RESULT (ic));
-    }
+#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)) &&
+                   !bitVectBitsInCommon (OP_DEFS (IC_RIGHT (ic)), OP_USES (IC_RESULT (ic))) &&
+                   OP_SYMBOL (IC_RESULT (ic))->isreqv) {
+
+                       setype = getSpec (operandType (IC_RESULT (ic)));
+             
+                       if (!OP_SYMBOL(IC_RIGHT (ic))->noSpilLoc &&
+                           !IS_VOLATILE (setype) &&
+                           !IN_FARSPACE (SPEC_OCLS (setype)) &&
+                           !OTHERS_PARM (OP_SYMBOL (IC_RESULT (ic))))
+
+                               SPIL_LOC (IC_RIGHT (ic)) =
+                                       SPIL_LOC (IC_RESULT (ic));
+               }
+               /* special case for inductions */
+               if (induction && 
+                   OP_SYMBOL(IC_RIGHT(ic))->isreqv && 
+                   !OP_SYMBOL(IC_RESULT (ic))->noSpilLoc &&
+                   !SPIL_LOC(IC_RESULT(ic))) {
+                       SPIL_LOC (IC_RESULT (ic)) = SPIL_LOC (IC_RIGHT (ic));
+               }
+       }
 }
-
 /*-----------------------------------------------------------------*/
 /* setUsesDef - sets the uses def bitvector for a given operand    */
 /*-----------------------------------------------------------------*/
@@ -962,10 +1075,10 @@ 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;
+         ReplaceOpWithCheaperOp(&IC_COND (ic), pdop);
          (*change)++;
        }
     }
@@ -1006,7 +1119,9 @@ ifxOptimize (iCode * ic, set * cseSet,
       /* too often, if it does happen then the user pays */
       /* the price */
       computeControlFlow (ebbs, count, 1);
-      werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
+      if (!options.lessPedantic) {
+       werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
+      }
       return;
     }
 
@@ -1020,13 +1135,15 @@ ifxOptimize (iCode * ic, set * cseSet,
 
       remiCodeFromeBBlock (ebb, ic);
       computeControlFlow (ebbs, count, 1);
-      werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
+      if (!options.lessPedantic) {
+       werror (W_CONTROL_FLOW, ic->filename, ic->lineno);
+      }
       return;
     }
 
 
   /* if it remains an IFX the update the use Set */
-  OP_USES (IC_COND (ic)) = bitVectSetBit (OP_USES (IC_COND (ic)), ic->key);
+  OP_USES(IC_COND (ic))=bitVectSetBit (OP_USES (IC_COND (ic)), ic->key);
   setUsesDefs (IC_COND (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
   return;
 }
@@ -1076,7 +1193,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)))
@@ -1118,7 +1235,7 @@ constFold (iCode * ic, set * cseSet)
   if (IS_ITEMP (IC_RESULT (ic)))
     {
       SPIL_LOC (IC_RESULT (ic)) = NULL;
-      IC_RESULT (ic)->noSpilLoc = 1;
+      OP_SYMBOL(IC_RESULT (ic))->noSpilLoc = 1;
     }
 
 
@@ -1160,9 +1277,9 @@ deleteGetPointers (set ** cseSet, set ** pss, operand * op, eBBlock * ebb)
             iTemp1 = iTemp0 + 8;
             iTemp2 = iTemp1 + 8; */
          if (isinSetWith (compItems, (void*)IC_LEFT (cdp->diCode), 
-                          (void*)isOperandEqual) ||
+                          (insetwithFunc)isOperandEqual) ||
              isinSetWith (compItems, (void*)IC_RIGHT (cdp->diCode), 
-                          (void*)isOperandEqual))
+                          (insetwithFunc)isOperandEqual))
            {
              addSet (&compItems, IC_RESULT (cdp->diCode));
            }
@@ -1214,7 +1331,8 @@ fixUpTypes (iCode * ic)
 {
   sym_link *t1 = operandType (IC_LEFT (ic)), *t2;
 
-  if (TARGET_IS_DS390)
+  /* if (TARGET_IS_DS390) */
+  if (options.model == MODEL_FLAT24)
     {
       /* hack-o-matic! */
       return;
@@ -1223,12 +1341,62 @@ fixUpTypes (iCode * ic)
   /* for pointer_gets if the types of result & left r the
      same then change it type of result to next */
   if (IS_PTR (t1) &&
-      checkType (t2 = operandType (IC_RESULT (ic)), t1) == 1)
+      compareType (t2 = operandType (IC_RESULT (ic)), t1) == 1)
     {
       setOperandType (IC_RESULT (ic), t2->next);
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* 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 */
@@ -1268,10 +1436,12 @@ cseBBlock (eBBlock * ebb, int computeOnly,
   /* for all the instructions in this block do */
   for (ic = ebb->sch; ic; ic = ic->next)
     {
-
       iCode *pdic;
       operand *pdop;
       iCode *defic;
+      int checkSign ;
+
+      ic->eBBlockNum = ebb->bbnum;
 
       if (SKIP_IC2 (ic))
        continue;
@@ -1291,7 +1461,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       if (ic->op == PCALL || ic->op == CALL || ic->op == RECEIVE)
        {
          /* add to defSet of the symbol */
-         OP_DEFS (IC_RESULT (ic)) =
+         OP_DEFS(IC_RESULT (ic))=
            bitVectSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
          /* add to the definition set of this block */
          ebb->defSet = bitVectSetBit (ebb->defSet, ic->key);
@@ -1301,6 +1471,10 @@ cseBBlock (eBBlock * ebb, int computeOnly,
          /* delete global variables from the cseSet
             since they can be modified by the function call */
          deleteItemIf (&cseSet, ifDefGlobal);
+
+         /* and also itemps assigned from globals */
+         deleteItemIf (&cseSet, ifAssignedFromGlobal);
+
          /* 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 */
@@ -1319,14 +1493,14 @@ 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;
+               ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
            }
          /* the lookup could have changed it */
          if (IS_SYMOP (IC_LEFT (ic)))
            {
-             OP_USES (IC_LEFT (ic)) =
+             OP_USES(IC_LEFT (ic))=
                bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
              setUsesDefs (IC_LEFT (ic), ebb->defSet,
                           ebb->outDefs, &ebb->usesDefs);
@@ -1351,7 +1525,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       /* if jumptable then mark the usage */
       if (ic->op == JUMPTABLE)
        {
-         OP_USES (IC_JTCOND (ic)) =
+         OP_USES(IC_JTCOND (ic))=
            bitVectSetBit (OP_USES (IC_JTCOND (ic)), ic->key);
          setUsesDefs (IC_JTCOND (ic), ebb->defSet,
                       ebb->outDefs, &ebb->usesDefs);
@@ -1395,18 +1569,20 @@ cseBBlock (eBBlock * ebb, int computeOnly,
        {
 
          /* update the spill location for this */
-         updateSpillLocation (ic);
+         updateSpillLocation (ic,0);
 
          if (POINTER_SET (ic) &&
              !(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;
+               ReplaceOpWithCheaperOp (&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 */
@@ -1418,14 +1594,18 @@ 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))
                    {
-                     IC_LEFT (ic) = 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);
+                       ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
                      change = 1;
                    }
                  /* check if there is a pointer set
@@ -1437,14 +1617,14 @@ cseBBlock (eBBlock * ebb, int computeOnly,
                    {
                      ic->op = '=';
                      IC_LEFT (ic) = NULL;
-                     IC_RIGHT (ic) = pdop;
+                     ReplaceOpWithCheaperOp(&IC_RIGHT(ic), pdop);
                      SET_ISADDR (IC_RESULT (ic), 0);
                    }
 
                }
              else
                {
-                 IC_LEFT (ic) = pdop;
+                 ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
                  change = 1;
                }
            }
@@ -1455,15 +1635,13 @@ cseBBlock (eBBlock * ebb, int computeOnly,
        {
 
          pdop = NULL;
-         applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop);
-         if (pdop)
-           {
-
-             IC_RIGHT (ic) = pdop;
-             change = 1;
-           }
+         applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop, checkSign);
+         if (pdop) {
+           ReplaceOpWithCheaperOp(&IC_RIGHT(ic), pdop);
+           change = 1;
+         }
        }
-
+       
       /* if left or right changed then do algebraic */
       if (change)
        {
@@ -1494,69 +1672,35 @@ cseBBlock (eBBlock * ebb, int computeOnly,
          IS_ITEMP (IC_RESULT (ic)) &&
          !computeOnly)
        {
-         applyToSet (cseSet, findPrevIc, ic, &pdic);
-         if (pdic && checkType (operandType (IC_RESULT (pdic)),
+           applyToSet (cseSet, findPrevIc, ic, &pdic);
+         if (pdic && compareType (operandType (IC_RESULT (pdic)),
                                 operandType (IC_RESULT (ic))) != 1)
            pdic = NULL;
+         if (pdic && port->cseOk && (*port->cseOk)(ic,pdic) == 0) 
+             pdic = NULL;
        }
 
-      /* if found then eliminate this and add to */
-      /* to cseSet an element containing result */
-      /* of this with previous opcode           */
-      if (pdic)
-       {
-
-         if (IS_ITEMP (IC_RESULT (ic)))
-           {
-
-             /* replace in the remaining of this block */
-             replaceAllSymBySym (ic->next, IC_RESULT (ic), IC_RESULT (pdic), &ebb->ndompset);
-             /* remove this iCode from inexpressions of all
-                its successors, it cannot be in the in expressions
-                of any of the predecessors */
-             for (i = 0; i < count; ebbs[i++]->visited = 0);
-             applyToSet (ebb->succList, removeFromInExprs, ic, IC_RESULT (ic),
-                         IC_RESULT (pdic), ebb);
-
-             /* if this was moved from another block */
-             /* then replace in those blocks too     */
-             if (ic->movedFrom)
-               {
-                 eBBlock *owner;
-                 for (owner = setFirstItem (ic->movedFrom); owner;
-                      owner = setNextItem (ic->movedFrom))
-                   replaceAllSymBySym (owner->sch, IC_RESULT (ic), IC_RESULT (pdic), &owner->ndompset);
-               }
-             pdic->movedFrom = unionSets (pdic->movedFrom, ic->movedFrom, THROW_NONE);
-           }
-         else
-           addSetHead (&cseSet, newCseDef (IC_RESULT (ic), pdic));
-
-         if (!computeOnly)
-           /* eliminate this */
-           remiCodeFromeBBlock (ebb, ic);
-
-         defic = pdic;
-         change++;
-
-         if (IS_ITEMP (IC_RESULT (ic)))
-           continue;
-
+      /* 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);    
        }
-      else
-       {
+      }
 
-         /* just add this as a previous expression except in */
-         /* case of a pointer access in which case this is a */
-         /* usage not a definition                           */
-         if (!(POINTER_SET (ic)) && IC_RESULT (ic))
-           {
-             deleteItemIf (&cseSet, ifDefSymIsX, IC_RESULT (ic));
-             addSetHead (&cseSet, newCseDef (IC_RESULT (ic), ic));
-           }
-         defic = ic;
-
-       }
+      if (!(POINTER_SET (ic)) && IC_RESULT (ic)) {
+         deleteItemIf (&cseSet, ifDefSymIsX, IC_RESULT (ic));
+         addSetHead (&cseSet, newCseDef (IC_RESULT (ic), ic));
+      }
+      defic = ic;
 
       /* if assignment to a parameter which is not
          mine and type is a pointer then delete
@@ -1601,7 +1745,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       /* add the left & right to the defUse set */
       if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)))
        {
-         OP_USES (IC_LEFT (ic)) =
+         OP_USES(IC_LEFT (ic))=
            bitVectSetBit (OP_USES (IC_LEFT (ic)), ic->key);
          setUsesDefs (IC_LEFT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
 
@@ -1609,7 +1753,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
 
       if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)))
        {
-         OP_USES (IC_RIGHT (ic)) =
+         OP_USES(IC_RIGHT (ic))=
            bitVectSetBit (OP_USES (IC_RIGHT (ic)), ic->key);
          setUsesDefs (IC_RIGHT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
 
@@ -1619,7 +1763,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       /* in the defuseSet if it a pointer or array access  */
       if (POINTER_SET (defic))
        {
-         OP_USES (IC_RESULT (ic)) =
+         OP_USES(IC_RESULT (ic))=
            bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
          setUsesDefs (IC_RESULT (ic), ebb->defSet, ebb->outDefs, &ebb->usesDefs);
          deleteItemIf (&cseSet, ifPointerGet, IC_RESULT (ic));
@@ -1638,7 +1782,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
       else
        /* add the result to defintion set */ if (IC_RESULT (ic))
        {
-         OP_DEFS (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)));