git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4986 4a8a32a2...
[fw/sdcc] / src / SDCCopt.c
index a7cf5cbbbaf703fc885387d40265ba4826c7271f..1adb1461301fca7b0cd207fdcd76ca8d114830fb 100644 (file)
@@ -68,7 +68,12 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
 
-  if(IS_FLOAT(operandType( IC_RIGHT( ic ) ))) {
+  if (IS_SYMOP (left))
+      bitVectUnSetBit (OP_USES (left), ic->key);
+  if (IS_SYMOP (right))
+      bitVectUnSetBit (OP_USES (right), ic->key);
+
+  if (IS_FLOAT (operandType (right))) {
     switch (ic->op)
       {
       case '+':
@@ -103,7 +108,7 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
         break;
       }
   } else
-  if(IS_FIXED16X16 (operandType (IC_RIGHT(ic)))) {
+  if (IS_FIXED16X16 (operandType (right))) {
     switch (ic->op)
       {
       case '+':
@@ -138,7 +143,7 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
         break;
       }
   }
-  
+
 
   /* if float support routines NOT compiled as reentrant */
   if (!options.float_rent)
@@ -147,31 +152,35 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
       /* first one */
       if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
         {
-          newic = newiCode (SEND, IC_LEFT (ic), NULL);
+          newic = newiCode (SEND, left, NULL);
           newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
         }
       else
         {
-          newic = newiCode ('=', NULL, IC_LEFT (ic));
+          newic = newiCode ('=', NULL, left);
           IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type));
         }
 
       addiCodeToeBBlock (ebp, newic, ip);
       newic->lineno = lineno;
+      if (IS_SYMOP (left))
+          OP_USES (left) = bitVectSetBit (OP_USES (left), newic->key);
 
       /* second one */
       if (IS_REGPARM (FUNC_ARGS(func->type)->next->etype))
         {
-          newic = newiCode (SEND, IC_RIGHT (ic), NULL);
+          newic = newiCode (SEND, right, NULL);
           newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->next->etype);
         }
       else
         {
-          newic = newiCode ('=', NULL, IC_RIGHT (ic));
+          newic = newiCode ('=', NULL, right);
           IC_RESULT (newic) = operandFromValue (FUNC_ARGS(func->type)->next);
         }
       addiCodeToeBBlock (ebp, newic, ip);
       newic->lineno = lineno;
+      if (IS_SYMOP (right))
+          OP_USES (right) = bitVectSetBit (OP_USES (right), newic->key);
 
     }
   else
@@ -187,12 +196,13 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
         {
           newic = newiCode (IPUSH, right, NULL);
           newic->parmPush = 1;
-          //bytesPushed+=4;
           bytesPushed += getSize(operandType(right));
         }
 
       addiCodeToeBBlock (ebp, newic, ip);
       newic->lineno = lineno;
+      if (IS_SYMOP (right))
+          OP_USES (right) = bitVectSetBit (OP_USES (right), newic->key);
 
       /* insert push left */
       if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
@@ -204,31 +214,35 @@ cnvToFcall (iCode * ic, eBBlock * ebp)
         {
           newic = newiCode (IPUSH, left, NULL);
           newic->parmPush = 1;
-          //bytesPushed+=4;
           bytesPushed += getSize(operandType(left));
         }
       addiCodeToeBBlock (ebp, newic, ip);
       newic->lineno = lineno;
+      if (IS_SYMOP (left))
+          OP_USES (left) = bitVectSetBit (OP_USES (left), newic->key);
+
     }
   /* insert the call */
   newic = newiCode (CALL, operandFromSymbol (func), NULL);
   IC_RESULT (newic) = IC_RESULT (ic);
+  bitVectUnSetBit (OP_DEFS (IC_RESULT (ic)), ic->key);
+  OP_USES (IC_RESULT (newic)) = bitVectSetBit (OP_USES (IC_RESULT (newic)), newic->key);
   newic->lineno = lineno;
-  newic->parmBytes+=bytesPushed;
+  newic->parmBytes += bytesPushed;
   ebp->hasFcall = 1;
   if (currFunc)
     FUNC_HASFCALL (currFunc->type) = 1;
 
-  if(TARGET_IS_PIC16) {
+  if(TARGET_IS_PIC16 || TARGET_IS_PIC) {
         /* normally these functions aren't marked external, so we can use their
          * _extern field to marked as already added to symbol table */
 
         if(!SPEC_EXTR(func->etype)) {
             memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
 
-                SPEC_EXTR(func->etype) = 1;
-                seg = SPEC_OCLS( func->etype );
-                addSet(&seg->syms, func);
+            SPEC_EXTR(func->etype) = 1;
+            seg = SPEC_OCLS( func->etype );
+            addSet(&seg->syms, func);
         }
   }
 
@@ -276,7 +290,7 @@ found:
   if (!options.float_rent)
     {
       /* first one */
-      if (IS_REGPARM (FUNC_ARGS(func->type)->etype)) 
+      if (IS_REGPARM (FUNC_ARGS(func->type)->etype))
         {
           newic = newiCode (SEND, IC_RIGHT (ic), NULL);
           newic->argreg = SPEC_ARGREG(FUNC_ARGS(func->type)->etype);
@@ -316,17 +330,17 @@ found:
   if (currFunc)
     FUNC_HASFCALL (currFunc->type) = 1;
 
-  if(TARGET_IS_PIC16) {
-       /* normally these functions aren't marked external, so we can use their
-        * _extern field to marked as already added to symbol table */
-
-       if(!SPEC_EXTR(func->etype)) {
-           memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
-               
-               SPEC_EXTR(func->etype) = 1;
-               seg = SPEC_OCLS( func->etype );
-               addSet(&seg->syms, func);
-       }
+  if(TARGET_IS_PIC16 || TARGET_IS_PIC) {
+        /* normally these functions aren't marked external, so we can use their
+         * _extern field to marked as already added to symbol table */
+
+        if(!SPEC_EXTR(func->etype)) {
+            memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
+
+            SPEC_EXTR(func->etype) = 1;
+            seg = SPEC_OCLS( func->etype );
+            addSet(&seg->syms, func);
+        }
   }
 
   addiCodeToeBBlock (ebp, newic, ip);
@@ -336,7 +350,7 @@ found:
 /*----------------------------------------------------------------------*/
 /* cnvToFixed16x16Cast - converts casts to fixed16x16 to function calls */
 /*----------------------------------------------------------------------*/
-static void 
+static void
 cnvToFixed16x16Cast (iCode * ic, eBBlock * ebp)
 {
   iCode *ip, *newic;
@@ -353,13 +367,13 @@ cnvToFixed16x16Cast (iCode * ic, eBBlock * ebp)
   for (bwd = 0; bwd < 3; bwd++)
     {
       for (su = 0; su < 2; su++)
-       {
-         if (compareType (type, __multypes[bwd][su]) == 1)
-           {
-             func = __fp16x16conv[0][bwd][su];
-             goto found;
-           }
-       }
+        {
+          if (compareType (type, __multypes[bwd][su]) == 1)
+            {
+              func = __fp16x16conv[0][bwd][su];
+              goto found;
+            }
+        }
     }
   assert (0);
 found:
@@ -408,7 +422,7 @@ found:
   if (currFunc)
     FUNC_HASFCALL (currFunc->type) = 1;
 
-  if(TARGET_IS_PIC16) {
+  if(TARGET_IS_PIC16 || TARGET_IS_PIC) {
         /* normally these functions aren't marked external, so we can use their
          * _extern field to marked as already added to symbol table */
 
@@ -501,7 +515,7 @@ found:
   if (currFunc)
     FUNC_HASFCALL (currFunc->type) = 1;
 
-  if(TARGET_IS_PIC16) {
+  if(TARGET_IS_PIC16 || TARGET_IS_PIC) {
         /* normally these functions aren't marked external, so we can use their
          * _extern field to marked as already added to symbol table */
 
@@ -521,7 +535,7 @@ found:
 /*--------------------------------------------------------------------------*/
 /* cnvFromFixed16x16Cast - converts casts from fixed16x16 to function calls */
 /*--------------------------------------------------------------------------*/
-static void 
+static void
 cnvFromFixed16x16Cast (iCode * ic, eBBlock * ebp)
 {
   iCode *ip, *newic;
@@ -547,13 +561,13 @@ cnvFromFixed16x16Cast (iCode * ic, eBBlock * ebp)
             }
         }
     }
-    
+
   if (compareType (type, floatType) == 1)
     {
       func = __fp16x16conv[1][3][0];
       goto found;
     }
-    
+
   assert (0);
 found:
 
@@ -601,17 +615,17 @@ found:
   if (currFunc)
     FUNC_HASFCALL (currFunc->type) = 1;
 
-  if(TARGET_IS_PIC16) {
-       /* normally these functions aren't marked external, so we can use their
-        * _extern field to marked as already added to symbol table */
-
-       if(!SPEC_EXTR(func->etype)) {
-           memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
-               
-               SPEC_EXTR(func->etype) = 1;
-               seg = SPEC_OCLS( func->etype );
-               addSet(&seg->syms, func);
-       }
+  if(TARGET_IS_PIC16 || TARGET_IS_PIC) {
+        /* normally these functions aren't marked external, so we can use their
+         * _extern field to marked as already added to symbol table */
+
+        if(!SPEC_EXTR(func->etype)) {
+            memmap *seg = SPEC_OCLS(OP_SYMBOL(IC_LEFT(newic))->etype);
+
+                SPEC_EXTR(func->etype) = 1;
+                seg = SPEC_OCLS( func->etype );
+                addSet(&seg->syms, func);
+        }
   }
 
   addiCodeToeBBlock (ebp, newic, ip);
@@ -778,10 +792,9 @@ convertToFcall (eBBlock ** ebbs, int count)
              converted to function calls */
           if ((IS_CONDITIONAL (ic) ||
                IS_ARITHMETIC_OP (ic)) &&
-             (IS_FLOAT (operandType (IC_RIGHT (ic)))
-               || IS_FIXED( operandType (IC_RIGHT (ic)))))
+              (IS_FLOAT (operandType (IC_RIGHT (ic))) ||
+               IS_FIXED( operandType (IC_RIGHT (ic)))))
             {
-
               cnvToFcall (ic, ebbs[i]);
             }
 
@@ -792,10 +805,10 @@ convertToFcall (eBBlock ** ebbs, int count)
                 cnvFromFloatCast (ic, ebbs[i]);
               else if (IS_FLOAT (operandType (IC_LEFT (ic))))
                 cnvToFloatCast (ic, ebbs[i]);
-             if (IS_FIXED16X16 (operandType (IC_RIGHT (ic))))
-               cnvFromFixed16x16Cast (ic, ebbs[i]);
-             else if (IS_FIXED16X16 (operandType (IC_LEFT (ic))))
-               cnvToFixed16x16Cast (ic, ebbs[i]);
+              if (IS_FIXED16X16 (operandType (IC_RIGHT (ic))))
+                cnvFromFixed16x16Cast (ic, ebbs[i]);
+              else if (IS_FIXED16X16 (operandType (IC_LEFT (ic))))
+                cnvToFixed16x16Cast (ic, ebbs[i]);
             }
 
           // Easy special case which avoids function call: modulo by a literal power
@@ -803,7 +816,7 @@ convertToFcall (eBBlock ** ebbs, int count)
           if (ic->op == '%' && isOperandLiteral(IC_RIGHT(ic)) &&
               IS_UNSIGNED(operandType(IC_LEFT(ic))))
             {
-              unsigned litVal = fabs(operandLitValue(IC_RIGHT(ic)));
+              unsigned litVal = abs((unsigned) double2ul (operandLitValue(IC_RIGHT(ic))));
 
               /* modulo by 1: no remainder */
               if (litVal == 1)
@@ -1232,7 +1245,7 @@ killDeadCode (ebbIndex * ebbi)
                       symbol * resultsym = OP_SYMBOL (IC_RESULT (ic));
                       symbol * prereqv = resultsym->prereqv;
 
-                     if (prereqv && prereqv->reqv && (OP_SYMBOL (prereqv->reqv) == resultsym))
+                      if (prereqv && prereqv->reqv && (OP_SYMBOL (prereqv->reqv) == resultsym))
                         {
                           operand * newreqv;
 
@@ -1277,7 +1290,7 @@ killDeadCode (ebbIndex * ebbi)
             }                   /* end of all instructions */
 
           if (!ebbs[i]->sch && !ebbs[i]->noPath)
-           disconBBlock (ebbs[i], ebbi);
+            disconBBlock (ebbs[i], ebbi);
 
         }                       /* end of for all blocks */
 
@@ -1449,7 +1462,7 @@ eBBlockFromiCode (iCode * ic)
 
   /* replace the local variables with their
      register equivalents : the liveRange computation
-     along with the register allocation will determine 
+     along with the register allocation will determine
      if it finally stays in the registers */
   replaceRegEqv (ebbi);
 
@@ -1481,7 +1494,7 @@ eBBlockFromiCode (iCode * ic)
     {
       change += cseAllBlocks (ebbi, FALSE);
       if (options.dump_gcse)
-       dumpEbbsToFileExt (DUMP_GCSE, ebbi);
+        dumpEbbsToFileExt (DUMP_GCSE, ebbi);
     }
   else
     {
@@ -1511,7 +1524,7 @@ eBBlockFromiCode (iCode * ic)
       computeDataFlow (ebbi);
       change += cseAllBlocks (ebbi, FALSE);
       if (options.dump_loop)
-       dumpEbbsToFileExt (DUMP_LOOPG, ebbi);
+        dumpEbbsToFileExt (DUMP_LOOPG, ebbi);
 
       /* if loop optimizations caused a change then do
          dead code elimination once more : this will
@@ -1520,7 +1533,7 @@ eBBlockFromiCode (iCode * ic)
       killDeadCode (ebbi);
 
       if (options.dump_loop)
-       dumpEbbsToFileExt (DUMP_LOOPD, ebbi);
+        dumpEbbsToFileExt (DUMP_LOOPD, ebbi);
 
     }
 
@@ -1535,9 +1548,9 @@ eBBlockFromiCode (iCode * ic)
        && !FUNC_ISNAKED(currFunc->type)) {
         eBBlock *bp;
         // make sure all predecessors of the last block end in a return
-       for (bp=setFirstItem(ebbi->bbOrder[ebbi->count-1]->predList); 
+        for (bp=setFirstItem(ebbi->bbOrder[ebbi->count-1]->predList);
              bp;
-            bp=setNextItem(ebbi->bbOrder[ebbi->count-1]->predList)) {
+             bp=setNextItem(ebbi->bbOrder[ebbi->count-1]->predList)) {
           if (bp->ech->op != RETURN) {
             werrorfl (bp->ech->filename, bp->ech->lineno,
                       W_VOID_FUNC, currFunc->name);
@@ -1554,11 +1567,11 @@ eBBlockFromiCode (iCode * ic)
   /* convert operations with support routines
      written in C to function calls : I am doing
      this at this point since I want all the
-     operations to be as they are for optimzations */
+     operations to be as they are for optimizations */
   convertToFcall (ebbi->bbOrder, ebbi->count);
 
   /* compute the live ranges */
-  computeLiveRanges (ebbi->bbOrder, ebbi->count);
+  computeLiveRanges (ebbi->bbOrder, ebbi->count, TRUE);
 
   if (options.dump_range)
     dumpEbbsToFileExt (DUMP_RANGE, ebbi);