* src/SDCCloop.c (assignmentsToSym, loopInduction): cast argument of setToNull()...
[fw/sdcc] / src / SDCClrange.c
index 96287420938c673a72f8b208fad8b4c28330ba7d..3a86ced93309c75d4e8607d191bb75da67062120 100644 (file)
@@ -29,6 +29,7 @@
 int iCodeSeq = 0;
 hTab *liveRanges = NULL;
 hTab *iCodehTab = NULL;
+hTab *iCodeSeqhTab = NULL;
 
 /*-----------------------------------------------------------------*/
 /* sequenceiCode - creates a sequence number for the iCode & add   */
@@ -48,6 +49,7 @@ sequenceiCode (eBBlock ** ebbs, int count)
          ic->seq = ++iCodeSeq;
          ic->depth = ebbs[i]->depth;
          hTabAddItem (&iCodehTab, ic->key, ic);
+         hTabAddItem (&iCodeSeqhTab, ic->seq, ic);
        }
       ebbs[i]->lSeq = iCodeSeq;
     }
@@ -122,10 +124,25 @@ isLastUse (operand * op, eBBlock * ebp, iCode * ic,
   if (usedInRemaining (op, ic))
     return 0;
 
-  /* if not then check any of the successor blocks use it */
-  for (i = 0; i < count; ebbs[i++]->visited = 0);
-  if (applyToSet (ebp->succList, isOpAlive, op, ebp, ic))
-    return 0;
+  if (getenv ("LRKLAUS"))
+    {
+      /* if not then check any of the following blocks use it */
+      for (i = 0; i < count; i++)
+        {
+          if (ebbs[i]->fSeq <= ebp->fSeq)
+           continue;
+          if (usedInRemaining (op, ebbs[i]->sch))
+           return 0;
+        }
+    }
+  else
+    {
+      /* if not then check any of the successor blocks use it */
+      for (i = 0; i < count;)
+        ebbs[i++]->visited = 0;
+      if (applyToSet (ebp->succList, isOpAlive, op, ebp, ic))
+        return 0;
+    }
 
   /* this is the last use */
   return 1;
@@ -278,7 +295,9 @@ useDefLoopCheck (operand * op, iCode * ic)
                  OP_SYMBOL (op)->name,
                  ic->filename, ic->lineno);
        }
+#if 0 // this will create a segfault: bug #498971
       OP_SYMBOL (op)->isspilt = 1;
+#endif
     }
 }
 
@@ -302,24 +321,45 @@ operandLUse (operand * op, eBBlock ** ebbs,
       int torange = ic->seq;
 
       /* if this is a SEND then the toRange should be extended
-        to the call */
-      if (ic->op == SEND) {
-         iCode *lic ;
-         for (lic = ic->next ; lic ; lic = lic->next) {
-             if (lic->op == CALL || lic->op == PCALL) break;
-         }
-         /* found it : mark */
-         if (lic) torange = lic->seq;
-      }
-      /* if this is the last use then if this block belongs 
-         to a  loop &  some definition  comes into the loop 
+         to the call */
+      if (ic->op == SEND)
+        {
+          iCode *lic ;
+          for (lic = ic->next ; lic ; lic = lic->next)
+            {
+              if (lic->op == CALL || lic->op == PCALL)
+                break;
+            }
+          /* found it : mark */
+          if (lic)
+            torange = lic->prev->seq;
+        }
+      /* if this is the last use then if this block belongs
+         to a  loop &  some definition  comes into the loop
          then extend the live range to  the end of the loop */
-      if (ebp->partOfLoop 
-         && hasIncomingDefs (ebp->partOfLoop, op))
-       {
-         torange = findLoopEndSeq (ebp->partOfLoop);
+      if (getenv ("LRKLAUS"))
+        {
+         if (ebp->KpartOfLoop)
+            {
+              region *aloop;
+
+              aloop = setFirstItem (ebp->KpartOfLoop);
+              for (; aloop; aloop = setNextItem (ebp->KpartOfLoop))
+                {
+                  if (hasIncomingDefs (aloop, op))
+                    torange = findLoopEndSeq (aloop);
+                }
+            }
+       }
+      else
+        {
+          if (ebp->partOfLoop
+             && hasIncomingDefs (ebp->partOfLoop, op))
+           {
+             torange = findLoopEndSeq (ebp->partOfLoop);
+           }
        }
-      
+
       op = operandFromOperand (op);
       setToRange (op, torange, FALSE);
     }
@@ -340,7 +380,7 @@ operandLUse (operand * op, eBBlock ** ebbs,
          !IS_STATIC (etype))
        {
 
-         if (bitVectIsZero (op->usesDefs))
+         if (bitVectIsZero (op->usesDefs) && OP_SYMBOL(op)->ival==NULL)
            {
              OP_SYMBOL (op)->isspilt = 1;
 
@@ -423,10 +463,10 @@ markLiveRanges (eBBlock * ebp, eBBlock ** ebbs, int count)
   bitVect *defsUsed = NULL;
   bitVect *defsNotUsed = NULL;
   int i;
+
   /* for all the instructions */
   for (ic = ebp->sch; ic; ic = ic->next)
     {
-
       if (ic->op == CALL || ic->op == PCALL)
        {
          setFromRange (IC_RESULT (ic), ic->seq);
@@ -537,7 +577,6 @@ markLiveRanges (eBBlock * ebp, eBBlock ** ebbs, int count)
          if (bitVectBitValue (defsNotUsed, i) &&
              (dic = hTabItemWithKey (iCodehTab, i)))
            {
-
              setToRange (IC_RESULT (dic), (ebp->fSeq - 1), TRUE);
            }
        }
@@ -576,10 +615,13 @@ rlivePoint (eBBlock ** ebbs, int count)
                /* if it is live then add the lrange to ic->rlive */
                if (lrange->liveFrom <= ic->seq &&
                    lrange->liveTo >= ic->seq) {
-                   lrange->isLiveFcall |= (ic->op == CALL || ic->op == PCALL || ic->op == SEND);
+                   lrange->isLiveFcall |= ((lrange->liveFrom < ic->seq) && 
+                                           (ic->op == CALL || ic->op == PCALL || ic->op == SEND));
+                   if (ic->op == CALL && lrange->liveFrom == ic->seq) continue;
                    ic->rlive = bitVectSetBit (ic->rlive, lrange->key);
                }
            }
+#if 0
            /* overlapping live ranges should be eliminated */
            if (ASSIGN_ITEMP_TO_ITEMP (ic)) {
                if (SPIL_LOC(IC_RIGHT(ic)) == SPIL_LOC(IC_RESULT(ic))   && /* left & right share the same spil location */
@@ -590,6 +632,7 @@ rlivePoint (eBBlock ** ebbs, int count)
                    SPIL_LOC(IC_RIGHT(ic)) = NULL; /* then cannot share */
                }
            }
+#endif
        }
     }
 }
@@ -650,21 +693,67 @@ static void computeClash ()
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* allDefsOutOfRange - all definitions are out of a range          */
+/*-----------------------------------------------------------------*/
+bool
+allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
+{
+  int i;
+
+  if (!defs)
+    return TRUE;
+
+  for (i = 0; i < defs->size; i++)
+    {
+      iCode *ic;
+
+      if (bitVectBitValue (defs, i) &&
+         (ic = hTabItemWithKey (iCodehTab, i)) &&
+         (ic->seq >= fseq && ic->seq <= toseq))
+
+       return FALSE;
+
+    }
+
+  return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* notUsedInBlock - not used in this block                         */
+/*-----------------------------------------------------------------*/
+int
+notUsedInBlock (symbol * sym, eBBlock * ebp, iCode *ic)
+{
+  return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
+         allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq) &&
+         allDefsOutOfRange (sym->uses, ebp->fSeq, ebp->lSeq));
+}
+
+
 /*-----------------------------------------------------------------*/
 /* computeLiveRanges - computes the live ranges for variables      */
 /*-----------------------------------------------------------------*/
-void 
+void
 computeLiveRanges (eBBlock ** ebbs, int count)
 {
   int i = 0;
-  /* sequence the code the live ranges are computed 
-     in terms of this sequence additionally the   
+  /* sequence the code the live ranges are computed
+     in terms of this sequence additionally the
      routine will also create a hashtable of instructions */
   iCodeSeq = 0;
   setToNull ((void **) &iCodehTab);
   iCodehTab = newHashTable (iCodeKey);
+  setToNull ((void **) &iCodeSeqhTab);
+  iCodeSeqhTab = newHashTable (iCodeKey);
   sequenceiCode (ebbs, count);
 
+  if (getenv ("LRKLAUS"))
+    {
+      /* add blocks between loop blocks as part of that loop */
+      addLoopBlocks (ebbs, count);
+    }
+
   /* call routine to mark the from & to live ranges for
      variables used */
   setToNull ((void **) &liveRanges);
@@ -677,3 +766,4 @@ computeLiveRanges (eBBlock ** ebbs, int count)
   /* compute which overlaps with what */
   computeClash();
 }
+