]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptstop.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / cc65 / coptstop.c
index 620a73504e52d7aba8fe303dcb61605846d32af7..4d3fe76e69e70f0846bbf8f89c96fe5d5c38a1a0 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                coptstop.c                                */
+/*                                 coptstop.c                                */
 /*                                                                           */
-/*          Optimize operations that take operands via the stack            */
+/*           Optimize operations that take operands via the stack            */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
@@ -364,7 +364,7 @@ static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
 
 
 /*****************************************************************************/
-/*                                         Helpers                                  */
+/*                                  Helpers                                  */
 /*****************************************************************************/
 
 
@@ -428,14 +428,14 @@ static void AdjustStackOffset (StackOpData* D, unsigned Offs)
     int I = D->PushIndex + 1;
     while (I < D->OpIndex) {
 
-       CodeEntry* E = CS_GetEntry (D->Code, I);
+        CodeEntry* E = CS_GetEntry (D->Code, I);
 
         int NeedCorrection = 0;
-       if ((E->Use & REG_SP) != 0) {
+        if ((E->Use & REG_SP) != 0) {
 
-           /* Check for some things that should not happen */
-           CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
-           CHECK (strcmp (E->Arg, "sp") == 0);
+            /* Check for some things that should not happen */
+            CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
+            CHECK (strcmp (E->Arg, "sp") == 0);
 
             /* We need to correct this one */
             NeedCorrection = 1;
@@ -449,37 +449,37 @@ static void AdjustStackOffset (StackOpData* D, unsigned Offs)
 
         if (NeedCorrection) {
 
-           /* Get the code entry before this one. If it's a LDY, adjust the
-            * value.
-            */
-           CodeEntry* P = CS_GetPrevEntry (D->Code, I);
-           if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
+            /* Get the code entry before this one. If it's a LDY, adjust the
+             * value.
+             */
+            CodeEntry* P = CS_GetPrevEntry (D->Code, I);
+            if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
 
-               /* The Y load is just before the stack access, adjust it */
-               CE_SetNumArg (P, P->Num - Offs);
+                /* The Y load is just before the stack access, adjust it */
+                CE_SetNumArg (P, P->Num - Offs);
 
-           } else {
+            } else {
 
-               /* Insert a new load instruction before the stack access */
-               const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
-               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
-               InsertEntry (D, X, I++);
+                /* Insert a new load instruction before the stack access */
+                const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
+                CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+                InsertEntry (D, X, I++);
 
-           }
+            }
 
             /* If we need the value of Y later, be sure to reload it */
             if (RegYUsed (D->Code, I+1)) {
-               const char* Arg = MakeHexArg (E->RI->In.RegY);
-               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
-               InsertEntry (D, X, I+1);
+                const char* Arg = MakeHexArg (E->RI->In.RegY);
+                CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+                InsertEntry (D, X, I+1);
 
-               /* Skip this instruction in the next round */
-               ++I;
+                /* Skip this instruction in the next round */
+                ++I;
             }
-       }
+        }
 
-       /* Next entry */
-       ++I;
+        /* Next entry */
+        ++I;
     }
 
     /* If we have rhs load insns that load from stack, we'll have to adjust
@@ -542,7 +542,7 @@ static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
     CodeEntry* X;
 
     if ((LI->A.Flags & LI_DIRECT) != 0) {
-               /* Op with a variable location. If the location is on the stack, we
+        /* Op with a variable location. If the location is on the stack, we
          * need to reload the Y register.
          */
         if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
@@ -570,8 +570,8 @@ static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
 
     } else {
 
-       /* Op with temp storage */
-       X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+        /* Op with temp storage */
+        X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
         InsertEntry (D, X, D->IP++);
 
     }
@@ -588,9 +588,9 @@ static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
     CodeEntry* X;
 
     if (KeepResult) {
-       /* pha */
-       X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
-       InsertEntry (D, X, D->IP++);
+        /* pha */
+        X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
     }
 
     /* txa */
@@ -603,7 +603,7 @@ static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
 
             /* opc xxx */
             CodeEntry* LoadX = LI->X.LoadEntry;
-           X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+            X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->IP++);
 
         } else {
@@ -628,13 +628,13 @@ static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
     }
 
     if (KeepResult) {
-       /* tax */
-       X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
-       InsertEntry (D, X, D->IP++);
+        /* tax */
+        X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
 
-       /* pla */
-       X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
-       InsertEntry (D, X, D->IP++);
+        /* pla */
+        X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
     }
 }
 
@@ -716,7 +716,7 @@ static int IsRegVar (StackOpData* D)
 
 
 /*****************************************************************************/
-/*                      Actual optimization functions                       */
+/*                       Actual optimization functions                       */
 /*****************************************************************************/
 
 
@@ -791,15 +791,15 @@ static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer
 
         D->IP = D->OpIndex+1;
 
-       /* Add operand for low byte */
-       AddOpLow (D, OP65_CMP, &D->Rhs);
+        /* Add operand for low byte */
+        AddOpLow (D, OP65_CMP, &D->Rhs);
 
         /* bne L */
         X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
         InsertEntry (D, X, D->IP++);
 
-       /* Add operand for high byte */
-       AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
+        /* Add operand for high byte */
+        AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
 
     } else {
 
@@ -832,6 +832,80 @@ static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer
 
 
 
+static unsigned Opt_tosshift (StackOpData* D, const char* Name)
+/* Optimize shift sequences. */
+{
+    CodeEntry*  X;
+
+    /* Store the value into the zeropage instead of pushing it */
+    ReplacePushByStore (D);
+
+    /* If the lhs is direct (but not stack relative), we can just reload the
+     * data later.
+     */
+    if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
+        (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
+
+        CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+        CodeEntry* LoadA = D->Lhs.A.LoadEntry;
+
+        /* Inline the shift */
+        D->IP = D->OpIndex+1;
+
+        /* tay */
+        X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+        /* lda */
+        X = NewCodeEntry (OP65_LDA, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+        /* ldx */
+        X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+        /* Lhs load entries can be removed */
+        D->Lhs.X.Flags |= LI_REMOVE;
+        D->Lhs.A.Flags |= LI_REMOVE;
+
+    } else {
+
+        /* Save lhs into zeropage and reload later */
+        AddStoreX (D);
+        AddStoreA (D);
+
+        /* Be sure to setup IP after adding the stores, otherwise it will get
+         * messed up.   
+         */
+        D->IP = D->OpIndex+1;
+
+        /* tay */
+        X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+        /* lda zp */
+        X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+        /* ldx zp+1 */
+        X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
+
+    }
+
+    /* jsr shlaxy/aslaxy/whatever */
+    X = NewCodeEntry (OP65_JSR, AM65_ABS, Name, 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
+
+    /* Remove the push and the call to the shift function */
+    RemoveRemainders (D);
+
+    /* We changed the sequence */
+    return 1;
+}
+
+
+
 static unsigned Opt___bzero (StackOpData* D)
 /* Optimize the __bzero sequence */
 {
@@ -871,7 +945,7 @@ static unsigned Opt___bzero (StackOpData* D)
             /* Loop using the sign bit */
 
             /* ldy #count-1 */
-           Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
+            Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->OpIndex+2);
 
@@ -906,7 +980,7 @@ static unsigned Opt___bzero (StackOpData* D)
             InsertEntry (D, X, D->OpIndex+4);
 
             /* cpy #count */
-           Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
+            Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
             X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->OpIndex+5);
 
@@ -971,20 +1045,20 @@ static unsigned Opt_staxspidx (StackOpData* D)
 
     if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
         /* Value of Y is known */
-               const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
-               X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+        const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
+        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
     } else {
         X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
     }
     InsertEntry (D, X, D->OpIndex+2);
 
     if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
-               /* Value of X is known */
-               const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
-               X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
+        /* Value of X is known */
+        const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
+        X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
     } else {
-               /* Value unknown */
-               X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+        /* Value unknown */
+        X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
     }
     InsertEntry (D, X, D->OpIndex+3);
 
@@ -1101,9 +1175,9 @@ static unsigned Opt_tosaddax (StackOpData* D)
             X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->IP++);
 
-        } else if (D->OpEntry->RI->In.RegX == 0                        &&
-                  (RegValIsKnown (D->PushEntry->RI->In.RegX)   ||
-                   (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
+        } else if (D->OpEntry->RI->In.RegX == 0                         &&
+                   (RegValIsKnown (D->PushEntry->RI->In.RegX)   ||
+                    (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
 
             /* The high byte is that of the first operand plus carry */
             CodeLabel* L;
@@ -1113,14 +1187,14 @@ static unsigned Opt_tosaddax (StackOpData* D)
                 X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
             } else {
                 /* Value of first op high byte is unknown. Load from ZP or
-                * original storage.
-                */
-               if (D->Lhs.X.Flags & LI_DIRECT) {
-                   CodeEntry* LoadX = D->Lhs.X.LoadEntry;
-                   X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
-               } else {
+                 * original storage.
+                 */
+                if (D->Lhs.X.Flags & LI_DIRECT) {
+                    CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+                    X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+                } else {
                     X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
-               }
+                }
             }
             InsertEntry (D, X, D->IP++);
 
@@ -1169,6 +1243,22 @@ static unsigned Opt_tosandax (StackOpData* D)
 
 
 
+static unsigned Opt_tosaslax (StackOpData* D)
+/* Optimize the tosaslax sequence */
+{
+    return Opt_tosshift (D, "aslaxy");
+}
+
+
+
+static unsigned Opt_tosasrax (StackOpData* D)
+/* Optimize the tosasrax sequence */
+{
+    return Opt_tosshift (D, "asraxy");
+}
+
+
+
 static unsigned Opt_toseqax (StackOpData* D)
 /* Optimize the toseqax sequence */
 {
@@ -1237,7 +1327,7 @@ static unsigned Opt_tosltax (StackOpData* D)
     CodeLabel* L;
 
 
-    /* Inline the sbc */
+    /* Inline the compare */
     D->IP = D->OpIndex+1;
 
     /* Must be true because of OP_RHS_LOAD */
@@ -1314,6 +1404,22 @@ static unsigned Opt_tosorax (StackOpData* D)
 
 
 
+static unsigned Opt_tosshlax (StackOpData* D)
+/* Optimize the tosshlax sequence */
+{
+    return Opt_tosshift (D, "shlaxy");
+}
+
+
+
+static unsigned Opt_tosshrax (StackOpData* D)
+/* Optimize the tosshrax sequence */
+{
+    return Opt_tosshift (D, "shraxy");
+}
+
+
+
 static unsigned Opt_tossubax (StackOpData* D)
 /* Optimize the tossubax sequence. Note: subtraction is not commutative! */
 {
@@ -1517,12 +1623,12 @@ static unsigned Opt_tosxorax (StackOpData* D)
     /* High byte */
     if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
         RegValIsKnown (D->OpEntry->RI->In.RegX)) {
-       /* Both values known, precalculate the result */
-       const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
-               X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
-       InsertEntry (D, X, D->IP++);
+        /* Both values known, precalculate the result */
+        const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
+        X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
+        InsertEntry (D, X, D->IP++);
     } else if (D->PushEntry->RI->In.RegX != 0) {
-       /* High byte is unknown */
+        /* High byte is unknown */
         AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
     }
 
@@ -1536,7 +1642,7 @@ static unsigned Opt_tosxorax (StackOpData* D)
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -1547,11 +1653,15 @@ static const OptFuncDesc FuncTable[] = {
     { "staxspidx",  Opt_staxspidx, REG_AX,   OP_NONE                    },
     { "tosaddax",   Opt_tosaddax,  REG_NONE, OP_NONE                    },
     { "tosandax",   Opt_tosandax,  REG_NONE, OP_NONE                    },
+    { "tosaslax",   Opt_tosaslax,  REG_NONE, OP_NONE                    },
+    { "tosasrax",   Opt_tosasrax,  REG_NONE, OP_NONE                    },
     { "toseqax",    Opt_toseqax,   REG_NONE, OP_NONE                    },
     { "tosgeax",    Opt_tosgeax,   REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosltax",    Opt_tosltax,   REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosneax",    Opt_tosneax,   REG_NONE, OP_NONE                    },
     { "tosorax",    Opt_tosorax,   REG_NONE, OP_NONE                    },
+    { "tosshlax",   Opt_tosshlax,  REG_NONE, OP_NONE                    },
+    { "tosshrax",   Opt_tosshrax,  REG_NONE, OP_NONE                    },
     { "tossubax",   Opt_tossubax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosugeax",   Opt_tosugeax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosugtax",   Opt_tosugtax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
@@ -1566,7 +1676,7 @@ static const OptFuncDesc FuncTable[] = {
 static int CmpFunc (const void* Key, const void* Func)
 /* Compare function for bsearch */
 {
-    return strcmp (Key, ((const        OptFuncDesc*) Func)->Name);
+    return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
 }
 
 
@@ -1599,11 +1709,32 @@ static int HarmlessCall (const char* Name)
         "aslax2",
         "aslax3",
         "aslax4",
+        "aslaxy",
         "asrax1",
         "asrax2",
         "asrax3",
         "asrax4",
+        "asraxy",
         "bnegax",
+        "complax",
+        "decax1",
+        "decax2",
+        "decax3",
+        "decax4",
+        "decax5",
+        "decax6",
+        "decax7",
+        "decax8",
+        "decaxy",
+        "incax1",
+        "incax2",
+        "incax3",
+        "incax4",
+        "incax5",
+        "incax6",
+        "incax7",
+        "incax8",
+        "incaxy",
         "ldaxidx",
         "ldaxysp",
         "negax",
@@ -1611,10 +1742,12 @@ static int HarmlessCall (const char* Name)
         "shlax2",
         "shlax3",
         "shlax4",
+        "shlaxy",
         "shrax1",
         "shrax2",
         "shrax3",
         "shrax4",
+        "shraxy",
     };
 
     void* R = bsearch (Name,
@@ -1714,7 +1847,7 @@ static int PreCondOk (StackOpData* D)
 
 
 /*****************************************************************************/
-/*                                  Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -1724,7 +1857,11 @@ unsigned OptStackOps (CodeSeg* S)
 {
     unsigned            Changes = 0;    /* Number of changes in one run */
     StackOpData         Data;
-    unsigned            I;
+    int                 I;
+    int                 OldEntryCount;  /* Old number of entries */
+    unsigned            UsedRegs = 0;   /* Registers used */
+    unsigned            ChangedRegs = 0;/* Registers changed */
+
 
     enum {
         Initialize,
@@ -1734,9 +1871,6 @@ unsigned OptStackOps (CodeSeg* S)
     } State = Initialize;
 
 
-    /* Generate register info */
-    CS_GenRegInfo (S);
-
     /* Remember the code segment in the info struct */
     Data.Code = S;
 
@@ -1756,16 +1890,17 @@ unsigned OptStackOps (CodeSeg* S)
      * intermediate code for zero page use.
      */
     I = 0;
-    while (I < CS_GetEntryCount (S)) {
+    while (I < (int)CS_GetEntryCount (S)) {
 
-       /* Get the next entry */
-       CodeEntry* E = CS_GetEntry (S, I);
+        /* Get the next entry */
+        CodeEntry* E = CS_GetEntry (S, I);
 
         /* Actions depend on state */
         switch (State) {
 
             case Initialize:
                 ResetStackOpData (&Data);
+                UsedRegs = ChangedRegs = REG_NONE;
                 State = Search;
                 /* FALLTHROUGH */
 
@@ -1852,6 +1987,17 @@ unsigned OptStackOps (CodeSeg* S)
                     Data.UsedRegs |= (E->Use | E->Chg);
                     TrackLoads (&Data.Rhs, E, I);
                 }
+                /* If the registers from the push (A/X) are used before they're
+                 * changed, we cannot change the sequence, because this would
+                 * with a high probability change the register contents.
+                 */
+                UsedRegs |= E->Use;
+                if ((UsedRegs & ~ChangedRegs) & REG_AX) {
+                    I = Data.PushIndex;
+                    State = Initialize;
+                    break;
+                }
+                ChangedRegs |= E->Chg;
                 break;
 
             case FoundOp:
@@ -1890,6 +2036,9 @@ unsigned OptStackOps (CodeSeg* S)
                 Data.OpEntry   = CS_GetEntry (S, Data.OpIndex);
                 Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
 
+                /* Remember the current number of code lines */
+                OldEntryCount = CS_GetEntryCount (S);
+
                 /* Adjust stack offsets to account for the upcoming removal */
                 AdjustStackOffset (&Data, 2);
 
@@ -1901,23 +2050,25 @@ unsigned OptStackOps (CodeSeg* S)
                 /* Call the optimizer function */
                 Changes += Data.OptFunc->Func (&Data);
 
+                /* Since the function may have added or deleted entries,
+                 * correct the index.
+                 */
+                I += CS_GetEntryCount (S) - OldEntryCount;
+
                 /* Regenerate register info */
                 CS_GenRegInfo (S);
 
                 /* Done */
                 State = Initialize;
-                break;
+                continue;
 
-       }
+        }
 
-       /* Next entry */
-       ++I;
+        /* Next entry */
+        ++I;
 
     }
 
-    /* Free the register info */
-    CS_FreeRegInfo (S);
-
     /* Return the number of changes made */
     return Changes;
 }