framework for the liverangehunt
authorjohanknol <johanknol@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 24 Feb 2003 12:45:59 +0000 (12:45 +0000)
committerjohanknol <johanknol@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Mon, 24 Feb 2003 12:45:59 +0000 (12:45 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2302 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/SDCCBBlock.c
src/SDCCast.h
src/SDCCcse.c
src/SDCCcse.h
src/SDCCmem.c
src/mcs51/ralloc.c

index 559d7341844401eaf57732d9388f162d6d8444fa..fd50e6a3b00b08df5a1e1715180d402c8b413c2e 100644 (file)
@@ -188,9 +188,10 @@ dumpEbbsToFileExt (int id, eBBlock ** ebbs, int count)
   for (i = 0; i < count; i++)
     {
       fprintf (of, "\n----------------------------------------------------------------\n");
-      fprintf (of, "Basic Block %s : loop Depth = %d noPath = %d , lastinLoop = %d\n",
+      fprintf (of, "Basic Block %s : loop Depth(lSeq) = %d(%d) noPath = %d , lastinLoop = %d\n",
               ebbs[i]->entryLabel->name,
               ebbs[i]->depth,
+              ebbs[i]->depth ? findLoopEndSeq(ebbs[i]->partOfLoop) : 0,
               ebbs[i]->noPath,
               ebbs[i]->isLastInLoop);
       fprintf (of, "\ndefines bitVector :");
index 8b3ba3dbc1b459e9020f0ebd53f2e08e86799bfc..9865bb297e1c9fbe8cfcd3cd273c964460ba1002 100644 (file)
@@ -209,6 +209,7 @@ ast *initAggregates (symbol *, initList *, ast *);
 bool hasSEFcalls (ast *);
 void addSymToBlock (symbol *, ast *);
 void freeStringSymbol(symbol *);
+DEFSETFUNC(resetParmKey);
 
 // exported variables 
 extern set *operKeyReset;
index 4c92f75a7b9efcbeebac1c8f5626188c71c9430e..2854a5bf956afe04e4ee431ea4836b4af29f7016 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
 
@@ -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,7 @@ replaceAllSymBySym (iCode * ic, operand * from, operand * to, bitVect ** ndpset)
 {
   iCode *lic;
 
+  LRH(printf ("replaceAllSymBySym: from %s to %s\n", OP_SYMBOL(from)->name, OP_SYMBOL(to)->name));
   for (lic = ic; lic; lic = lic->next)
     {
       int siaddr;
@@ -329,7 +358,9 @@ DEFSETFUNC (findCheaperOp)
          *opp = operandFromOperand (*opp);
          (*opp)->isaddr = cop->isaddr;
        }
-
+      LRH(printf ("findCheaperOp: %s < %s\n",\
+             IS_SYMOP((*opp)) ? OP_SYMBOL((*opp))->name : "!SYM",\
+             OP_SYMBOL(cop)->name));
       return 1;
 
     }
@@ -379,6 +410,7 @@ DEFSETFUNC (findPrevIc)
   if (isiCodeEqual (ic, cdp->diCode) &&
       isOperandEqual (cdp->sym, IC_RESULT (cdp->diCode)))
     {
+      LRH(printf ("findPrevIc same: %d %d\n", ic->key, cdp->diCode->key));
        *icp = cdp->diCode;
       return 1;
     }
@@ -390,6 +422,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;
     }
@@ -886,7 +919,6 @@ algebraicOpts (iCode * ic)
 void 
 updateSpillLocation (iCode * ic, int induction)
 {
-
        sym_link *setype;
 
        if (POINTER_SET (ic))
@@ -1016,7 +1048,7 @@ ifxOptimize (iCode * ic, set * cseSet,
       applyToSetFTrue (cseSet, findCheaperOp, IC_COND (ic), &pdop, 0);
       if (pdop)
        {
-         IC_COND (ic) = pdop;
+         ReplaceOpWithCheaperOp(&IC_COND (ic), pdop);
          (*change)++;
        }
     }
@@ -1334,6 +1366,7 @@ static int isSignedOp (iCode *ic)
        return 0;
     }
  }
+
 /*-----------------------------------------------------------------*/
 /* cseBBlock - common subexpression elimination for basic blocks   */
 /*             this is the hackiest kludgiest routine in the whole */
@@ -1433,7 +1466,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
              pdop = NULL;
              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)))
@@ -1515,7 +1548,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
              pdop = NULL;
              applyToSetFTrue (cseSet, findCheaperOp, IC_RESULT (ic), &pdop, 0);
              if (pdop && IS_ITEMP (pdop) && !computeOnly)
-               IC_RESULT (ic) = pdop;
+               ReplaceOpWithCheaperOp (&IC_RESULT(ic), pdop);
            }
        }
 
@@ -1543,7 +1576,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
                           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;
+                       ReplaceOpWithCheaperOp(&IC_LEFT(ic), pdop);
                      change = 1;
                    }
                  /* check if there is a pointer set
@@ -1555,14 +1588,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;
                }
            }
@@ -1574,13 +1607,12 @@ cseBBlock (eBBlock * ebb, int computeOnly,
 
          pdop = NULL;
          applyToSetFTrue (cseSet, findCheaperOp, IC_RIGHT (ic), &pdop, checkSign);
-         if (pdop)
-           {
-             IC_RIGHT (ic) = pdop;
-             change = 1;
-           }
+         if (pdop) {
+           ReplaceOpWithCheaperOp(&IC_RIGHT(ic), pdop);
+           change = 1;
+         }
        }
-
+       
       /* if left or right changed then do algebraic */
       if (change)
        {
@@ -1590,7 +1622,7 @@ cseBBlock (eBBlock * ebb, int computeOnly,
 
       /* if after all this it becomes a assignment to self
          then delete it and continue */
-      if (ASSIGNMENT_TO_SELF (ic) && !OTHERS_PARM(OP_SYMBOL(IC_RESULT(ic))))
+      if (ASSIGNMENT_TO_SELF (ic))
        {
          remiCodeFromeBBlock (ebb, ic);
          continue;
index 023afd1adc105b425efb8e2c14060298cbdf4c79..f2770b7682ef25028a73f11cbde71f23ac5194fa 100644 (file)
@@ -58,4 +58,5 @@ void updateSpillLocation (iCode * ic,int);
 void setUsesDefs (operand *, bitVect *, bitVect *, bitVect **);
 void replaceAllSymBySym (iCode *, operand *, operand *, bitVect **);
 iCode *findBackwardDef(operand *,iCode *);
+void ReplaceOpWithCheaperOp(operand **op, operand *cop);
 #endif
index bacebcfb2ffef64635c1e99b3db5db393ce1a006..1b91e5530c462c151d502605578623f8a4f28d83 100644 (file)
@@ -307,7 +307,9 @@ allocGlobal (symbol * sym)
              "%s%s", port->fun_prefix, sym->name);
 
   /* add it to the operandKey reset */
-  addSet (&operKeyReset, sym);
+  if (!isinSet (operKeyReset, sym)) {
+    addSet(&operKeyReset, sym);
+  }
 
   /* if this is a literal e.g. enumerated type */
   /* put it in the data segment & do nothing   */
@@ -586,7 +588,9 @@ deallocParms (value * val)
          addSym (SymbolTab, lval->sym, lval->sym->name,
                  lval->sym->level, lval->sym->block, 1);
          lval->sym->_isparm = 1;
-         addSet (&operKeyReset, lval->sym);
+         if (!isinSet (operKeyReset, lval->sym)) {
+           addSet(&operKeyReset, lval->sym);
+         }
        }
 
     }
@@ -1015,6 +1019,10 @@ printAllocInfoSeg (memmap * map, symbol * func, FILE * of)
       if (!sym->allocreq && sym->reqv)
        {
          int i;
+         if (!OP_SYMBOL(sym->reqv)->nRegs) {
+           printf ("*** warning: %s -> %s\n", sym->name, 
+                   OP_SYMBOL(sym->reqv)->name);
+         }
          sym = OP_SYMBOL (sym->reqv);
          fprintf (of, "registers ");
          for (i = 0; i < 4 && sym->regs[i]; i++)
index a426dbab114c3bcbf733cfba3217ef890ee7c3ae..e6dbda5d701ecb1736334b20222b7dce592adfd1 100644 (file)
@@ -1,3 +1,9 @@
+//#define LIVERANGEHUNT
+#ifdef LIVERANGEHUNT
+  #define LRH(x) x
+#else
+  #define LRH(x)
+#endif
 /*------------------------------------------------------------------------
 
   SDCCralloc.c - source file for register allocation. (8051) specific
@@ -571,7 +577,7 @@ spillThis (symbol * sym)
   if (!(sym->remat || sym->usl.spillLoc))
     createStackSpil (sym);
 
-
+  LRH(printf("spillThis: %s\n", sym->name));
   /* mark it has spilt & put it in the spilt set */
   sym->isspilt = sym->spillA = 1;
   _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
@@ -719,6 +725,7 @@ spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
 
   /* get something we can spil */
   ssym = selectSpil (ic, ebp, forSym);
+  LRH(printf("spilSomething: spilled %s for %s\n", ssym->name, forSym->name));
 
   /* mark it as spilt */
   ssym->isspilt = ssym->spillA = 1;
@@ -1168,6 +1175,9 @@ serialRegAssign (eBBlock ** ebbs, int count)
                    continue;                 
                }
 
+               if (strcmp(sym->name,"iTemp121")==0) {
+                 printf ("Oops\n");
+               }
                /* if it has a spillocation & is used less than
                   all other live ranges then spill this */
                if (willCS) {
@@ -1563,6 +1573,30 @@ regTypeNum (eBBlock *ebbs)
              continue;
            }
 
+#ifdef RANGEHUNT
+         /* if this symbol has only one usage and that is an assignment
+            to a ruonly, we don't need registers */
+         // if this symbol has only one def
+         if (bitVectnBitsOn (sym->defs)==1) {
+           printf ("sym: %s has only one usage", sym->name);
+           // find that usage
+           if ((ic = hTabItemWithKey (iCodehTab, bitVectFirstBit (sym->defs)))) {
+             if (ic->op==CALL) {
+               printf (" for a call ");
+               // if this is only assigned to a ruonly
+               if ((ic = hTabItemWithKey (iCodehTab, bitVectFirstBit (sym->defs)))) {
+                 if (ic->op=='=') {
+                   if (OP_SYMBOL(IC_RESULT(ic))->ruonly) {
+                     printf("regTypeNum: %s assigned to %s\n", \
+                            sym->name, OP_SYMBOL(IC_RESULT(ic))->name); 
+                   }
+                 }
+               }
+             }
+           }
+         }
+#endif
+
          /* if the symbol has only one definition &
             that definition is a get_pointer */
          if (bitVectnBitsOn (sym->defs) == 1 &&
@@ -1593,7 +1627,7 @@ regTypeNum (eBBlock *ebbs)
                  /* now this really is an assignment to itself, make it so;
                     it will be optimized out later */
                  ic->op='=';
-                 IC_RIGHT(ic)=IC_RESULT(ic);
+                 ReplaceOpWithCheaperOp(&IC_RIGHT(ic), IC_RESULT(ic));
                  IC_LEFT(ic)=NULL;
 #endif
                  continue;
@@ -1630,7 +1664,7 @@ regTypeNum (eBBlock *ebbs)
        /* registers for true symbols we will */
        /* see how things go                  */
        sym->nRegs = 0;
-    }
+         }
 
 }
 
@@ -1731,16 +1765,15 @@ static int
 packRegsForAssign (iCode * ic, eBBlock * ebp)
 {
   iCode *dic, *sic;
-  //sym_link *etype = operandType (IC_RIGHT (ic));
 
   if (!IS_ITEMP (IC_RIGHT (ic)) ||
       OP_SYMBOL (IC_RIGHT (ic))->isind ||
-      OP_LIVETO (IC_RIGHT (ic)) > ic->seq
-      /* why? || IS_BITFIELD (etype) */ )
+      OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
     {
       return 0;
     }
 
+
   /* if the true symbol is defined in far space or on stack
      then we should not since this will increase register pressure */
   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
@@ -1835,12 +1868,14 @@ pack:
   /* replace the result with the result of */
   /* this assignment and remove this assignment */
   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
-  IC_RESULT (dic) = IC_RESULT (ic);
+  ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
 
   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
     {
       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
     }
+  // jwk: and the otherway around?
+
   /* delete from liverange table also 
      delete from all the points inbetween and the new
      one */
@@ -1856,7 +1891,6 @@ pack:
   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
   OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
   return 1;
-
 }
 
 /*------------------------------------------------------------------*/
@@ -1902,6 +1936,7 @@ findAssignToSym (operand * op, iCode * ic)
   if (!dic)
     return NULL;   /* didn't find any assignment to op */
 
+  LRH(printf ("findAssignToSym: %s\n", OP_SYMBOL(IC_RESULT(dic))->name));
   /* we are interested only if defined in far space */
   /* or in stack space in case of + & - */
   
@@ -2089,6 +2124,7 @@ packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
                         bitVectFirstBit (OP_DEFS (op)))))
     return NULL;
 
+  LRH(printf ("packRegsForOneUse: %s\n", OP_SYMBOL(op)->name));
   /* if that only usage is a cast */
   if (dic->op == CAST) {
     /* to a bigger type */
@@ -2190,7 +2226,6 @@ packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
 
   OP_SYMBOL (op)->ruonly = 1;
   return sic;
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -2487,7 +2522,7 @@ packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
 
   /* 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);
+  ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
   remiCodeFromeBBlock (ebp, dic);
   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
 }
@@ -2640,12 +2675,7 @@ packRegisters (eBBlock ** ebpp, int blockno)
       if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
          !isOperandInFarSpace (IC_LEFT (ic)) &&
          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);
-       }
+       packRegsForOneuse (ic, IC_LEFT (ic), ebp);
       }
 
       /* if pointer set & left has a size more than
@@ -2655,7 +2685,6 @@ packRegisters (eBBlock ** ebpp, int blockno)
          !OP_SYMBOL (IC_RESULT (ic))->remat &&
          !IS_OP_RUONLY (IC_RIGHT (ic)) &&
          getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
-
        packRegsForOneuse (ic, IC_RESULT (ic), ebp);
 
       /* if pointer get */
@@ -2664,7 +2693,6 @@ packRegisters (eBBlock ** ebpp, int blockno)
          !OP_SYMBOL (IC_LEFT (ic))->remat &&
          !IS_OP_RUONLY (IC_RESULT (ic)) &&
          getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
-
        packRegsForOneuse (ic, IC_LEFT (ic), ebp);
 
 
@@ -2689,7 +2717,7 @@ packRegisters (eBBlock ** ebpp, int blockno)
                  if (IS_ARITHMETIC_OP (dic))
                    {                  
                      bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
-                     IC_RESULT (dic) = IC_RESULT (ic);
+                     ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
                      remiCodeFromeBBlock (ebp, ic);
                      bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
                      hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
@@ -2712,7 +2740,7 @@ packRegisters (eBBlock ** ebpp, int blockno)
                  if (dic)
                    {
                      bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
-                     IC_RESULT (dic) = IC_RESULT (ic);
+                     ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
                      remiCodeFromeBBlock (ebp, ic);
                      bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
                      hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);