fixed the
[fw/sdcc] / src / mcs51 / ralloc.c
index aafd4dcd9314cec8f706e998877855d0ce42e13c..447c9ff11ce6be4341154476860fcaa7f87e2c0a 100644 (file)
@@ -455,8 +455,7 @@ spillLRWithPtrReg (symbol * forSym)
     {
       int j;
 
-      /* if no registers assigned to it or
-         spilt */
+      /* if no registers assigned to it or spilt */
       /* if it does not overlap with this then 
          not need to spill it */
 
@@ -1074,146 +1073,138 @@ xchgPositions:
 static void
 serialRegAssign (eBBlock ** ebbs, int count)
 {
-  int i;
+    int i;
 
-  /* for all blocks */
-  for (i = 0; i < count; i++)
-    {
+    /* for all blocks */
+    for (i = 0; i < count; i++) {
 
-      iCode *ic;
+       iCode *ic;
 
-      if (ebbs[i]->noPath &&
-         (ebbs[i]->entryLabel != entryLabel &&
-          ebbs[i]->entryLabel != returnLabel))
-       continue;
+       if (ebbs[i]->noPath &&
+           (ebbs[i]->entryLabel != entryLabel &&
+            ebbs[i]->entryLabel != returnLabel))
+           continue;
 
-      /* of all instructions do */
-      for (ic = ebbs[i]->sch; ic; ic = ic->next)
-       {
+       /* of all instructions do */
+       for (ic = ebbs[i]->sch; ic; ic = ic->next) {
 
-         /* if this is an ipop that means some live
-            range will have to be assigned again */
-         if (ic->op == IPOP)
-           reassignLR (IC_LEFT (ic));
-
-         /* if result is present && is a true symbol */
-         if (IC_RESULT (ic) && ic->op != IFX &&
-             IS_TRUE_SYMOP (IC_RESULT (ic)))
-           OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
-
-         /* take away registers from live
-            ranges that end at this instruction */
-         deassignLRs (ic, ebbs[i]);
-
-         /* some don't need registers */
-         if (SKIP_IC2 (ic) ||
-             ic->op == JUMPTABLE ||
-             ic->op == IFX ||
-             ic->op == IPUSH ||
-             ic->op == IPOP ||
-             (IC_RESULT (ic) && POINTER_SET (ic)))
-           continue;
+           /* if this is an ipop that means some live
+              range will have to be assigned again */
+           if (ic->op == IPOP)
+               reassignLR (IC_LEFT (ic));
 
-         /* now we need to allocate registers
-            only for the result */
-         if (IC_RESULT (ic))
-           {
-             symbol *sym = OP_SYMBOL (IC_RESULT (ic));
-             bitVect *spillable;
-             int willCS;
-             int j;
-             int ptrRegSet = 0;
-
-             /* if it does not need or is spilt 
-                or is already assigned to registers
-                or will not live beyond this instructions */
-             if (!sym->nRegs ||
-                 sym->isspilt ||
-                 bitVectBitValue (_G.regAssigned, sym->key) ||
-                 sym->liveTo <= ic->seq)
-               continue;
+           /* if result is present && is a true symbol */
+           if (IC_RESULT (ic) && ic->op != IFX &&
+               IS_TRUE_SYMOP (IC_RESULT (ic)))
+               OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
 
-             /* if some liverange has been spilt at the block level
-                and this one live beyond this block then spil this
-                to be safe */
-             if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
-               {
-                 spillThis (sym);
-                 continue;
-               }
-             /* if trying to allocate this will cause
-                a spill and there is nothing to spill 
-                or this one is rematerializable then
-                spill this one */
-             willCS = willCauseSpill (sym->nRegs, sym->regType);
-             spillable = computeSpillable (ic);
-             if (sym->remat ||
-                 (willCS && bitVectIsZero (spillable)))
-               {
+           /* take away registers from live
+              ranges that end at this instruction */
+           deassignLRs (ic, ebbs[i]);
 
-                 spillThis (sym);
-                 continue;
+           /* some don't need registers */
+           if (SKIP_IC2 (ic) ||
+               ic->op == JUMPTABLE ||
+               ic->op == IFX ||
+               ic->op == IPUSH ||
+               ic->op == IPOP ||
+               (IC_RESULT (ic) && POINTER_SET (ic)))
+               continue;
 
+           /* now we need to allocate registers
+              only for the result */
+           if (IC_RESULT (ic)) {
+               symbol *sym = OP_SYMBOL (IC_RESULT (ic));
+               bitVect *spillable;
+               int willCS;
+               int j;
+               int ptrRegSet = 0;
+
+               /* if it does not need or is spilt 
+                  or is already assigned to registers
+                  or will not live beyond this instructions */
+               if (!sym->nRegs ||
+                   sym->isspilt ||
+                   bitVectBitValue (_G.regAssigned, sym->key) ||
+                   sym->liveTo <= ic->seq)
+                   continue;
+
+               /* if some liverange has been spilt at the block level
+                  and this one live beyond this block then spil this
+                  to be safe */
+               if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
+                   spillThis (sym);
+                   continue;
+               }
+               /* if trying to allocate this will cause
+                  a spill and there is nothing to spill 
+                  or this one is rematerializable then
+                  spill this one */
+               willCS = willCauseSpill (sym->nRegs, sym->regType);
+               spillable = computeSpillable (ic);
+               if (sym->remat || (willCS && bitVectIsZero (spillable))) {                    
+                   spillThis (sym);
+                   continue;                 
                }
 
-             /* if it has a spillocation & is used less than
-                all other live ranges then spill this */
-             if (willCS && sym->usl.spillLoc)
-               {
-
-                 symbol *leastUsed =
-                 leastUsedLR (liveRangesWith (spillable,
-                                              allLRs,
-                                              ebbs[i],
-                                              ic));
-                 if (leastUsed &&
-                     leastUsed->used > sym->used)
-                   {
-                     spillThis (sym);
-                     continue;
+               /* if it has a spillocation & is used less than
+                  all other live ranges then spill this */
+               if (willCS) {
+                   if (sym->usl.spillLoc) {
+                       symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
+                                                                        allLRs, ebbs[i], ic));
+                       if (leastUsed && leastUsed->used > sym->used) {
+                           spillThis (sym);
+                           continue;
+                       }
+                   } else {
+                       /* if none of the liveRanges have a spillLocation then better
+                          to spill this one than anything else already assigned to registers */
+                       if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
+                           spillThis (sym);
+                           continue;
+                       }
                    }
                }
-
-             /* if we need ptr regs for the right side
-                then mark it */
-             if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
-                 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
-                 <= PTRSIZE)
-               {
-                 mcs51_ptrRegReq++;
-                 ptrRegSet = 1;
+               /* if we need ptr regs for the right side
+                  then mark it */
+               if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
+                   && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
+                   mcs51_ptrRegReq++;
+                   ptrRegSet = 1;
+               }
+               /* else we assign registers to it */
+               _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
+
+               for (j = 0; j < sym->nRegs; j++) {
+                   if (sym->regType == REG_PTR)
+                       sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
+                   else
+                       sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
+
+                   /* if the allocation falied which means
+                      this was spilt then break */
+                   if (!sym->regs[j])
+                       break;
                }
-             /* else we assign registers to it */
-             _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
-
-             for (j = 0; j < sym->nRegs; j++)
-               {
-                 if (sym->regType == REG_PTR)
-                   sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
-                 else
-                   sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
 
-                 /* if the allocation falied which means
-                    this was spilt then break */
-                 if (!sym->regs[j])
-                   break;
+               /* if it shares registers with operands make sure
+                  that they are in the same position */
+               if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
+                   OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
+                   positionRegs (OP_SYMBOL (IC_RESULT (ic)),
+                                 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
                }
-             /* if it shares registers with operands make sure
-                that they are in the same position */
-             if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
-                 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
-               positionRegs (OP_SYMBOL (IC_RESULT (ic)),
-                             OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
-             /* do the same for the right operand */
-             if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
-                 OP_SYMBOL (IC_RIGHT (ic))->nRegs)
-               positionRegs (OP_SYMBOL (IC_RESULT (ic)),
-                             OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
-
-             if (ptrRegSet)
-               {
-                 mcs51_ptrRegReq--;
-                 ptrRegSet = 0;
+               /* do the same for the right operand */
+               if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
+                   OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
+                   positionRegs (OP_SYMBOL (IC_RESULT (ic)),
+                                 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
+               }
+
+               if (ptrRegSet) {
+                   mcs51_ptrRegReq--;
+                   ptrRegSet = 0;
                }
 
            }
@@ -1531,7 +1522,6 @@ farSpacePackable (iCode * ic)
      symbol on the right */
   for (dic = ic->prev; dic; dic = dic->prev)
     {
-
       /* if the definition is a call then no */
       if ((dic->op == CALL || dic->op == PCALL) &&
          IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
@@ -1605,20 +1595,26 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
 
   /* if the true symbol is defined in far space or on stack
      then we should not since this will increase register pressure */
+#if 0
   if (isOperandInFarSpace (IC_RESULT (ic)))
     {
       if ((dic = farSpacePackable (ic)))
        goto pack;
       else
        return 0;
-
     }
+#else
+  if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
+    return 0;
+  }
+#endif
+
   /* find the definition of iTempNN scanning backwards if we find a 
      a use of the true symbol in before we find the definition then 
      we cannot */
   for (dic = ic->prev; dic; dic = dic->prev)
     {
-
+#if 0 // jwk 20010410
       /* if there is a function call and this is
          a parameter & not my parameter then don't pack it */
       if ((dic->op == CALL || dic->op == PCALL) &&
@@ -1628,6 +1624,14 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
          dic = NULL;
          break;
        }
+#else
+      /* if there is a function call then don't pack it */
+      if ((dic->op == CALL || dic->op == PCALL))
+       {
+         dic = NULL;
+         break;
+       }
+#endif
 
       if (SKIP_IC2 (dic))
        continue;
@@ -1814,6 +1818,7 @@ packRegsForSupport (iCode * ic, eBBlock * ebp)
   /* for the left & right operand :- look to see if the
      left was assigned a true symbol in far space in that
      case replace them */
+
   if (IS_ITEMP (IC_LEFT (ic)) &&
       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
     {
@@ -1837,7 +1842,7 @@ packRegsForSupport (iCode * ic, eBBlock * ebp)
     }
 
   /* do the same for the right operand */
-right:
+ right:
   if (!change &&
       IS_ITEMP (IC_RIGHT (ic)) &&
       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
@@ -1913,7 +1918,7 @@ packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
   if (bitVectnBitsOn (OP_DEFS (op)) > 1)
     return NULL;               /* has more than one definition */
 
-  /* get the that definition */
+  /* get that definition */
   if (!(dic =
        hTabItemWithKey (iCodehTab,
                         bitVectFirstBit (OP_DEFS (op)))))
@@ -1999,6 +2004,12 @@ packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
        {
          return NULL;
        }
+      /* if left or right or result is on stack */
+      if (isOperandOnStack(IC_LEFT(dic)) ||
+         isOperandOnStack(IC_RIGHT(dic)) ||
+         isOperandOnStack(IC_RESULT(dic))) {
+       return NULL;
+      }
     }
 
   OP_SYMBOL (op)->ruonly = 1;
@@ -2013,22 +2024,22 @@ static bool
 isBitwiseOptimizable (iCode * ic)
 {
   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
-  // jwk sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
+  sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
 
   /* bitwise operations are considered optimizable
      under the following conditions (Jean-Louis VERN) 
 
-     x & lit   <== jwk: should be x && lit
+     x & lit
      bit & bit
      bit & x
      bit ^ bit
      bit ^ x
-     x   ^ lit <== jwk: should be x ^^ lit
-     x   | lit <== jwk: should be x || lit
+     x   ^ lit
+     x   | lit
      bit | bit
      bit | x
-   */
-  if ( /* jwk IS_LITERAL (rtype) || */
+  */
+  if (IS_LITERAL(rtype) ||
       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
     return TRUE;
   else
@@ -2172,6 +2183,8 @@ accuse:
 /*-----------------------------------------------------------------*/
 /* packForPush - hueristics to reduce iCode for pushing            */
 /*-----------------------------------------------------------------*/
+catchMe() {}
+
 static void
 packForPush (iCode * ic, eBBlock * ebp)
 {
@@ -2197,13 +2210,28 @@ packForPush (iCode * ic, eBBlock * ebp)
   /* make sure the right side does not have any definitions
      inbetween */
   dbv = OP_DEFS(IC_RIGHT(dic));
-  for (lic = ic; lic != dic ; lic = lic->prev) {
-         if (bitVectBitValue(dbv,lic->key)) return ;
+  for (lic = ic; lic && lic != dic ; lic = lic->prev) {
+    if (bitVectBitValue(dbv,lic->key)) 
+      return ;
+  }
+  /* make sure they have the same type */
+  {
+    sym_link *itype=operandType(IC_LEFT(ic));
+    sym_link *ditype=operandType(IC_RIGHT(dic));
+
+    if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
+       SPEC_SHORT(itype)!=SPEC_SHORT(ditype) ||
+       SPEC_USIGN(itype)!=SPEC_USIGN(ditype))
+      return;
+  }
+  /* extend the live range of replaced operand if needed */
+  if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
+         OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
   }
   /* we now we know that it has one & only one def & use
      and the that the definition is an assignment */
   IC_LEFT (ic) = IC_RIGHT (dic);
-
+   
   remiCodeFromeBBlock (ebp, dic);
   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
 }
@@ -2240,7 +2268,6 @@ packRegisters (eBBlock * ebp)
 
   for (ic = ebp->sch; ic; ic = ic->next)
     {
-
       /* if this is an itemp & result of a address of a true sym 
          then mark this as rematerialisable   */
       if (ic->op == ADDRESS_OF &&
@@ -2280,8 +2307,6 @@ packRegisters (eBBlock * ebp)
           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
           IS_OP_LITERAL (IC_RIGHT (ic))))
        {
-
-         //int i = operandLitValue (IC_RIGHT (ic));
          OP_SYMBOL (IC_RESULT (ic))->remat = 1;
          OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
          OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
@@ -2318,6 +2343,7 @@ packRegisters (eBBlock * ebp)
            }
        }
 
+#if 0
       /* if the condition of an if instruction
          is defined in the previous instruction then
          mark the itemp as a conditional */
@@ -2330,6 +2356,22 @@ packRegisters (eBBlock * ebp)
          OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
          continue;
        }
+#else
+      /* if the condition of an if instruction
+         is defined in the previous instruction and
+        this is the only usage then
+         mark the itemp as a conditional */
+      if ((IS_CONDITIONAL (ic) ||
+          (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
+         ic->next && ic->next->op == IFX &&
+         bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
+         isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
+         OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
+       {
+         OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
+         continue;
+       }
+#endif
 
       /* reduce for support function calls */
       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
@@ -2339,8 +2381,14 @@ packRegisters (eBBlock * ebp)
          can be eliminated for return statements */
       if ((ic->op == RETURN || ic->op == SEND) &&
          !isOperandInFarSpace (IC_LEFT (ic)) &&
-         options.model == MODEL_SMALL)
-       packRegsForOneuse (ic, IC_LEFT (ic), ebp);
+         options.model == MODEL_SMALL) {
+       if (0 && options.stackAuto) {
+         /* we should check here if acc will be clobbered for stack
+            offset calculations */
+       } else {
+         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
+       }
+      }
 
       /* if pointer set & left has a size more than
          one and right is not in far space */
@@ -2381,7 +2429,7 @@ packRegisters (eBBlock * ebp)
              if (dic)
                {
                  if (IS_ARITHMETIC_OP (dic))
-                   {
+                   {                  
                      IC_RESULT (dic) = IC_RESULT (ic);
                      remiCodeFromeBBlock (ebp, ic);
                      hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
@@ -2442,7 +2490,6 @@ packRegisters (eBBlock * ebp)
          getSize (operandType (IC_RESULT (ic))) <= 2)
 
        packRegsForAccUse (ic);
-
     }
 }