added post increment optimization
authorsandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Mar 2001 21:27:50 +0000 (21:27 +0000)
committersandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Mar 2001 21:27:50 +0000 (21:27 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@721 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/mcs51/gen.c

index 1e3801225077f425ab8519522f17aaa5dbcfed27..4b97304d9f4e7cbf295ddd96e186bfd63b21f69d 100644 (file)
@@ -4153,6 +4153,37 @@ ifxForOp (operand * op, iCode * ic)
 
   return NULL;
 }
+
+/*-----------------------------------------------------------------*/
+/* hasInc - operand is incremented before any other use            */
+/*-----------------------------------------------------------------*/
+static iCode *
+hasInc (operand *op, iCode *ic)
+{
+  sym_link *type = operandType(op);
+  sym_link *retype = getSpec (type);
+  iCode *lic = ic->next;
+  int isize ;
+  
+  if (IS_BITVAR(retype)||!IS_PTR(type)) return NULL;
+  isize = getSize(type->next);
+  while (lic) {
+    /* if operand of the form op = op + <sizeof *op> */
+    if (lic->op == '+' && isOperandEqual(IC_LEFT(lic),op) &&
+       isOperandEqual(IC_RESULT(lic),op) && 
+       isOperandLiteral(IC_RIGHT(lic)) &&
+       operandLitValue(IC_RIGHT(lic)) == isize) {
+      return lic;
+    }
+    /* if the operand used or deffed */
+    if (bitVectBitValue(ic->uses,op->key) || ic->defKey == op->key) {
+      return NULL;
+    }
+    lic = lic->next;
+  }
+  return NULL;
+}
+
 /*-----------------------------------------------------------------*/
 /* genAndOp - for && operation                                     */
 /*-----------------------------------------------------------------*/
@@ -6794,7 +6825,8 @@ genDataPointerGet (operand * left,
 static void
 genNearPointerGet (operand * left,
                   operand * result,
-                  iCode * ic)
+                  iCode * ic,
+                  iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -6835,7 +6867,6 @@ genNearPointerGet (operand * left,
   else
     rname = aopGet (AOP (left), 0, FALSE, FALSE);
 
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -6861,15 +6892,17 @@ genNearPointerGet (operand * left,
              aopPut (AOP (result), buffer, offset);
            }
          offset++;
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop)       /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) { /* post increment present */
+       aopPut(AOP ( left ),rname,0);
+      }
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -6879,10 +6912,11 @@ genNearPointerGet (operand * left,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (result) > 1 &&
-         !OP_SYMBOL (left)->remat &&
-         (OP_SYMBOL (left)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (result) > 1 &&
+          !OP_SYMBOL (left)->remat &&
+          (OP_SYMBOL (left)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (result) - 1;
          while (size--)
@@ -6891,8 +6925,9 @@ genNearPointerGet (operand * left,
     }
 
   /* done */
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  if (pi) pi->generated = 1;
 }
 
 /*-----------------------------------------------------------------*/
@@ -6901,7 +6936,8 @@ genNearPointerGet (operand * left,
 static void
 genPagedPointerGet (operand * left,
                    operand * result,
-                   iCode * ic)
+                   iCode * ic,
+                   iCode *pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -6928,7 +6964,6 @@ genPagedPointerGet (operand * left,
   else
     rname = aopGet (AOP (left), 0, FALSE, FALSE);
 
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -6948,15 +6983,15 @@ genPagedPointerGet (operand * left,
 
          offset++;
 
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut ( AOP (left), rname, 0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -6966,10 +7001,11 @@ genPagedPointerGet (operand * left,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (result) > 1 &&
-         !OP_SYMBOL (left)->remat &&
-         (OP_SYMBOL (left)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (result) > 1 &&
+          !OP_SYMBOL (left)->remat &&
+          (OP_SYMBOL (left)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (result) - 1;
          while (size--)
@@ -6978,8 +7014,9 @@ genPagedPointerGet (operand * left,
     }
 
   /* done */
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  if (pi) pi->generated = 1;
 
 }
 
@@ -6988,7 +7025,7 @@ genPagedPointerGet (operand * left,
 /*-----------------------------------------------------------------*/
 static void
 genFarPointerGet (operand * left,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -7009,7 +7046,6 @@ genFarPointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -7024,20 +7060,26 @@ genFarPointerGet (operand * left,
        {
          emitcode ("movx", "a,@dptr");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
-
+  
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
-/* emitcodePointerGet - gget value from code space                  */
+/* genCodePointerGet - gget value from code space                  */
 /*-----------------------------------------------------------------*/
 static void
-emitcodePointerGet (operand * left,
-                   operand * result, iCode * ic)
+genCodePointerGet (operand * left,
+                   operand * result, iCode * ic, iCode *pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -7058,7 +7100,6 @@ emitcodePointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -7074,11 +7115,17 @@ emitcodePointerGet (operand * left,
          emitcode ("clr", "a");
          emitcode ("movc", "a,@a+dptr");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -7087,7 +7134,7 @@ emitcodePointerGet (operand * left,
 /*-----------------------------------------------------------------*/
 static void
 genGenPointerGet (operand * left,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode *pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -7112,7 +7159,6 @@ genGenPointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -7127,11 +7173,17 @@ genGenPointerGet (operand * left,
        {
          emitcode ("lcall", "__gptrget");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -7139,7 +7191,7 @@ genGenPointerGet (operand * left,
 /* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
 static void
-genPointerGet (iCode * ic)
+genPointerGet (iCode * ic, iCode *pi)
 {
   operand *left, *result;
   sym_link *type, *etype;
@@ -7159,21 +7211,6 @@ genPointerGet (iCode * ic)
     {
       /* we have to go by the storage class */
       p_type = PTR_TYPE (SPEC_OCLS (etype));
-
-/*  if (SPEC_OCLS(etype)->codesp ) { */
-/*      p_type = CPOINTER ;  */
-/*  } */
-/*  else */
-/*      if (SPEC_OCLS(etype)->fmap && !SPEC_OCLS(etype)->paged) */
-/*    p_type = FPOINTER ; */
-/*      else */
-/*    if (SPEC_OCLS(etype)->fmap && SPEC_OCLS(etype)->paged) */
-/*        p_type = PPOINTER; */
-/*    else */
-/*        if (SPEC_OCLS(etype) == idata ) */
-/*      p_type = IPOINTER; */
-/*        else */
-/*      p_type = POINTER ; */
     }
 
   /* now that we have the pointer type we assign
@@ -7183,23 +7220,23 @@ genPointerGet (iCode * ic)
 
     case POINTER:
     case IPOINTER:
-      genNearPointerGet (left, result, ic);
+      genNearPointerGet (left, result, ic, pi);
       break;
 
     case PPOINTER:
-      genPagedPointerGet (left, result, ic);
+      genPagedPointerGet (left, result, ic, pi);
       break;
 
     case FPOINTER:
-      genFarPointerGet (left, result, ic);
+      genFarPointerGet (left, result, ic, pi);
       break;
 
     case CPOINTER:
-      emitcodePointerGet (left, result, ic);
+      genCodePointerGet (left, result, ic, pi);
       break;
 
     case GPOINTER:
-      genGenPointerGet (left, result, ic);
+      genGenPointerGet (left, result, ic, pi);
       break;
     }
 
@@ -7408,7 +7445,8 @@ genDataPointerSet (operand * right,
 static void
 genNearPointerSet (operand * right,
                   operand * result,
-                  iCode * ic)
+                  iCode * ic,
+                  iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -7446,7 +7484,6 @@ genNearPointerSet (operand * right,
   else
     rname = aopGet (AOP (result), 0, FALSE, FALSE);
 
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -7468,16 +7505,16 @@ genNearPointerSet (operand * right,
            }
          else
            emitcode ("mov", "@%s,%s", rname, l);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
          offset++;
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut (AOP (result),rname,0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -7487,10 +7524,11 @@ genNearPointerSet (operand * right,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (right) > 1 &&
-         !OP_SYMBOL (result)->remat &&
-         (OP_SYMBOL (result)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (right) > 1 &&
+          !OP_SYMBOL (result)->remat &&
+          (OP_SYMBOL (result)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (right) - 1;
          while (size--)
@@ -7499,9 +7537,9 @@ genNearPointerSet (operand * right,
     }
 
   /* done */
+  if (pi) pi->generated = 1;
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
-
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -7510,7 +7548,8 @@ genNearPointerSet (operand * right,
 static void
 genPagedPointerSet (operand * right,
                    operand * result,
-                   iCode * ic)
+                   iCode * ic,
+                   iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -7537,7 +7576,6 @@ genPagedPointerSet (operand * right,
   else
     rname = aopGet (AOP (result), 0, FALSE, FALSE);
 
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -7556,7 +7594,7 @@ genPagedPointerSet (operand * right,
          MOVA (l);
          emitcode ("movx", "@%s,a", rname);
 
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
 
          offset++;
@@ -7564,9 +7602,9 @@ genPagedPointerSet (operand * right,
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut (AOP (result),rname,0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -7588,6 +7626,8 @@ genPagedPointerSet (operand * right,
     }
 
   /* done */
+  if (pi) pi->generated = 1;
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 
 
@@ -7598,7 +7638,7 @@ genPagedPointerSet (operand * right,
 /*-----------------------------------------------------------------*/
 static void
 genFarPointerSet (operand * right,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (right));
@@ -7619,7 +7659,6 @@ genFarPointerSet (operand * right,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
@@ -7635,11 +7674,16 @@ genFarPointerSet (operand * right,
          char *l = aopGet (AOP (right), offset++, FALSE, FALSE);
          MOVA (l);
          emitcode ("movx", "@dptr,a");
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
-
+  if (pi && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD) {
+    aopPut (AOP(result),"dpl",0);
+    aopPut (AOP(result),"dph",1);
+    pi->generated=1;
+  }
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 }
 
@@ -7648,7 +7692,7 @@ genFarPointerSet (operand * right,
 /*-----------------------------------------------------------------*/
 static void
 genGenPointerSet (operand * right,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (right));
@@ -7674,7 +7718,6 @@ genGenPointerSet (operand * right,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
@@ -7690,11 +7733,17 @@ genGenPointerSet (operand * right,
          char *l = aopGet (AOP (right), offset++, FALSE, FALSE);
          MOVA (l);
          emitcode ("lcall", "__gptrput");
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD) {
+    aopPut (AOP(result),"dpl",0);
+    aopPut (AOP(result),"dph",1);
+    pi->generated=1;
+  }
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 }
 
@@ -7702,7 +7751,7 @@ genGenPointerSet (operand * right,
 /* genPointerSet - stores the value into a pointer location        */
 /*-----------------------------------------------------------------*/
 static void
-genPointerSet (iCode * ic)
+genPointerSet (iCode * ic, iCode *pi)
 {
   operand *right, *result;
   sym_link *type, *etype;
@@ -7733,19 +7782,19 @@ genPointerSet (iCode * ic)
 
     case POINTER:
     case IPOINTER:
-      genNearPointerSet (right, result, ic);
+      genNearPointerSet (right, result, ic, pi);
       break;
 
     case PPOINTER:
-      genPagedPointerSet (right, result, ic);
+      genPagedPointerSet (right, result, ic, pi);
       break;
 
     case FPOINTER:
-      genFarPointerSet (right, result, ic);
+      genFarPointerSet (right, result, ic, pi);
       break;
 
     case GPOINTER:
-      genGenPointerSet (right, result, ic);
+      genGenPointerSet (right, result, ic, pi);
       break;
     }
 
@@ -8496,12 +8545,12 @@ gen51Code (iCode * lic)
          break;
 
        case GET_VALUE_AT_ADDRESS:
-         genPointerGet (ic);
+         genPointerGet (ic, hasInc(IC_LEFT(ic),ic));
          break;
 
        case '=':
          if (POINTER_SET (ic))
-           genPointerSet (ic);
+           genPointerSet (ic, hasInc (IC_RESULT(ic),ic));
          else
            genAssign (ic);
          break;