a (void *) can be assigned to any pointer
[fw/sdcc] / src / SDCClrange.c
index 495d68072ec2435b97d4aed0681778d042c79f40..33dddbad685b58a07415ef32de55c4a457d7ce7a 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;
     }
@@ -278,7 +280,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
     }
 }
 
@@ -300,6 +304,17 @@ operandLUse (operand * op, eBBlock ** ebbs,
       (OP_LIVETO (op) && OP_LIVETO (op) < ic->seq))
     {
       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->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 */
@@ -308,6 +323,7 @@ operandLUse (operand * op, eBBlock ** ebbs,
        {
          torange = findLoopEndSeq (ebp->partOfLoop);
        }
+      
       op = operandFromOperand (op);
       setToRange (op, torange, FALSE);
     }
@@ -328,7 +344,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;
 
@@ -564,10 +580,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 */
@@ -578,6 +597,7 @@ rlivePoint (eBBlock ** ebbs, int count)
                    SPIL_LOC(IC_RIGHT(ic)) = NULL; /* then cannot share */
                }
            }
+#endif
        }
     }
 }
@@ -624,19 +644,58 @@ static void computeClash ()
            /* so they overlap : set both their clashes */
            inner->clashes = bitVectSetBit(inner->clashes,outer->key);
            outer->clashes = bitVectSetBit(outer->clashes,inner->key);
-#if 0      
+
            /* check if they share the same spillocation */
-           if (SYM_SPIL_LOC(inner) && SYM_SPIL_LOC(outer)) {
+           if (SYM_SPIL_LOC(inner) && SYM_SPIL_LOC(outer) && 
+               SYM_SPIL_LOC(inner) == SYM_SPIL_LOC(outer)) {
                if (inner->reqv && !outer->reqv) SYM_SPIL_LOC(outer)=NULL;
                else if (outer->reqv && !inner->reqv) SYM_SPIL_LOC(inner)=NULL;
                else if (inner->used > outer->used) SYM_SPIL_LOC(outer)=NULL;
                else SYM_SPIL_LOC(inner)=NULL;
            }
-#endif
+
        }
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* 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      */
 /*-----------------------------------------------------------------*/
@@ -650,6 +709,8 @@ computeLiveRanges (eBBlock ** ebbs, int count)
   iCodeSeq = 0;
   setToNull ((void **) &iCodehTab);
   iCodehTab = newHashTable (iCodeKey);
+  setToNull ((void **) &iCodeSeqhTab);
+  iCodeSeqhTab = newHashTable (iCodeKey);
   sequenceiCode (ebbs, count);
 
   /* call routine to mark the from & to live ranges for
@@ -664,3 +725,4 @@ computeLiveRanges (eBBlock ** ebbs, int count)
   /* compute which overlaps with what */
   computeClash();
 }
+