]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptind.c
ValidSegName now defined in segnames.h
[cc65] / src / cc65 / coptind.c
index 982c744d4b0c42f3144abc71dbe671db8fc2521a..0ffbf9e2f8a58b962fe75b3e354248dac558e46e 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001      Ullrich von Bassewitz                                       */
+/* (C) 2001-2002 Ullrich von Bassewitz                                       */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
 /* EMail:        uz@cc65.org                                                 */
@@ -89,6 +89,28 @@ static int IsShortDist (int Distance)
 
 
 
+static short RegVal (unsigned short Use, const RegContents* RC)
+/* Return the contents of the given register */
+{
+    if ((Use & REG_A) != 0) {
+               return RC->RegA;
+    } else if ((Use & REG_X) != 0) {
+       return RC->RegX;
+    } else if ((Use & REG_Y) != 0) {
+       return RC->RegY;
+    } else if ((Use & REG_TMP1) != 0) {
+       return RC->Tmp1;
+    } else if ((Use & REG_SREG_LO) != 0) {
+       return RC->SRegLo;
+    } else if ((Use & REG_SREG_HI) != 0) {
+       return RC->SRegHi;
+    } else {
+       return UNKNOWN_REGVAL;
+    }
+}
+
+
+
 /*****************************************************************************/
 /*                       Replace jumps to RTS by RTS                        */
 /*****************************************************************************/
@@ -214,7 +236,9 @@ unsigned OptDeadJumps (CodeSeg* S)
        /* Check if it's a branch, if it has a local target, and if the target
         * is the next instruction.
         */
-       if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
+               if (E->AM == AM65_BRA                               &&
+           E->JumpTo                                       &&
+           E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
 
            /* Delete the dead jump */
            CS_DelEntry (S, I);
@@ -237,7 +261,7 @@ unsigned OptDeadJumps (CodeSeg* S)
 
 
 /*****************************************************************************/
-/*                            Remove dead code                              */
+/*                            Remove dead code                              */
 /*****************************************************************************/
 
 
@@ -254,16 +278,22 @@ unsigned OptDeadCode (CodeSeg* S)
     while (I < CS_GetEntryCount (S)) {
 
        CodeEntry* N;
+        CodeLabel* LN;
 
        /* Get this entry */
        CodeEntry* E = CS_GetEntry (S, I);
 
                /* Check if it's an unconditional branch, and if the next entry has
-        * no labels attached
+        * no labels attached, or if the label is just used so that the insn
+         * can jump to itself.
         */
-               if ((E->Info & OF_DEAD) != 0           &&
-           (N = CS_GetNextEntry (S, I)) != 0  &&
-           !CE_HasLabel (N)) {
+               if ((E->Info & OF_DEAD) != 0                     &&     /* Dead code follows */
+           (N = CS_GetNextEntry (S, I)) != 0            &&     /* Has next entry */
+                   (!CE_HasLabel (N)                        ||         /* Don't has a label */
+             ((N->Info & OF_UBRA) != 0          &&              /* Uncond branch */
+              (LN = N->JumpTo) != 0             &&              /* Jumps to known label */
+              LN->Owner == N                    &&              /* Attached to insn */
+              CL_GetRefCount (LN) == 1))) {                     /* Only reference */
 
            /* Delete the next entry */
            CS_DelEntry (S, I+1);
@@ -328,10 +358,28 @@ unsigned OptJumpCascades (CodeSeg* S)
                        ((E->Info & OF_CBRA) != 0 &&
                 GetBranchCond (E->OPC)  == GetBranchCond (N->OPC))) {
 
-               /* This is a jump cascade and we may jump to the final target.
-                * Insert a new instruction, then remove the old one
+               /* This is a jump cascade and we may jump to the final target,
+                 * provided that the other insn does not jump to itself. If
+                 * this is the case, we can also jump to ourselves, otherwise
+                 * insert a jump to the new instruction and remove the old one.
                 */
-               CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
+                       CodeEntry* X;
+                CodeLabel* LN = N->JumpTo;
+
+                if (LN != 0 && LN->Owner == N) {
+
+                    /* We found a jump to a jump to itself. Replace our jump
+                     * by a jump to itself.
+                     */
+                    CodeLabel* LE = CS_GenLabel (S, E);
+                    X = NewCodeEntry (E->OPC, E->AM, LE->Name, LE, E->LI);
+
+                } else {
+
+                    /* Jump to the final jump target */
+                    X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
+
+                }
 
                /* Insert it behind E */
                CS_InsertEntry (S, X, I+1);
@@ -467,30 +515,29 @@ unsigned OptJumpTarget (CodeSeg* S)
     CodeEntry* E1;                     /* Entry 1 */
     CodeEntry* E2;             /* Entry 2 */
     CodeEntry* T1;             /* Jump target entry 1 */
-    CodeEntry* T2;             /* Jump target entry 2 */
     CodeLabel* TL1;            /* Target label 1 */
-    unsigned TI;               /* Target index */
 
     /* Walk over the entries */
     unsigned I = 0;
     while (I < CS_GetEntryCount (S)) {
 
        /* Get next entry */
-               E2 = CS_GetEntry (S, I+1);
+               E2 = CS_GetNextEntry (S, I);
 
-       /* Check if we have a jump or branch, and a matching label */
-               if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
-
-           /* Get the target instruction for the label */
-           T2 = E2->JumpTo->Owner;
-
-           /* Get the entry preceeding this one (if possible) */
-           TI = CS_GetEntryIndex (S, T2);
-           if (TI == 0) {
-               /* There is no entry before this one */
+       /* Check if we have a jump or branch, and a matching label, which
+        * is not attached to the jump itself
+        */
+               if (E2 != 0                     &&
+           (E2->Info & OF_UBRA) != 0   &&
+           E2->JumpTo                  &&
+           E2->JumpTo->Owner != E2) {
+
+           /* Get the entry preceeding the branch target */
+           T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
+                   if (T1 == 0) {
+               /* There is no such entry */
                goto NextEntry;
            }
-           T1 = CS_GetEntry (S, TI-1);
 
            /* Get the entry preceeding the jump */
            E1 = CS_GetEntry (S, I);
@@ -637,7 +684,7 @@ unsigned OptCondBranches (CodeSeg* S)
 
 
 /*****************************************************************************/
-/*                     Remove unused loads and stores                       */
+/*                     Remove unused loads and stores                       */
 /*****************************************************************************/
 
 
@@ -657,24 +704,24 @@ unsigned OptUnusedLoads (CodeSeg* S)
                CodeEntry* E = CS_GetEntry (S, I);
 
        /* Check if it's a register load or transfer insn */
-       if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0  &&
-           (N = CS_GetNextEntry (S, I)) != 0                    &&
-           (N->Info & OF_FBRA) == 0) {
+       if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0         &&
+           (N = CS_GetNextEntry (S, I)) != 0                           &&
+           !CE_UseLoadFlags (N)) {
 
            /* Check which sort of load or transfer it is */
            unsigned R;
            switch (E->OPC) {
-               case OP65_DEA:
-               case OP65_INA:
-               case OP65_LDA:
+               case OP65_DEA:
+               case OP65_INA:
+               case OP65_LDA:
                case OP65_TXA:
                case OP65_TYA:  R = REG_A;      break;
-               case OP65_DEX:
-               case OP65_INX:
-               case OP65_LDX:
+               case OP65_DEX:
+               case OP65_INX:
+               case OP65_LDX:
                case OP65_TAX:  R = REG_X;      break;
-               case OP65_DEY:
-               case OP65_INY:
+               case OP65_DEY:
+               case OP65_INY:
                        case OP65_LDY:
                case OP65_TAY:  R = REG_Y;      break;
                default:        goto NextEntry;         /* OOPS */
@@ -730,10 +777,10 @@ unsigned OptUnusedStores (CodeSeg* S)
            if ((GetRegInfo (S, I+1, R) & R) == 0) {
 
                /* Register value is not used, remove the load */
-               CS_DelEntry (S, I);
+               CS_DelEntry (S, I);
 
-               /* Remember, we had changes */
-               ++Changes;
+               /* Remember, we had changes */
+               ++Changes;
 
            }
        }
@@ -777,31 +824,31 @@ unsigned OptDupLoads (CodeSeg* S)
        switch (E->OPC) {
 
            case OP65_LDA:
-                       if (In->RegA >= 0                     && /* Value of A is known */
+                       if (RegValIsKnown (In->RegA)          && /* Value of A is known */
                            CE_KnownImm (E)                   && /* Value to be loaded is known */
                            In->RegA == (long) E->Num         && /* Both are equal */
                            (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
-                   (N->Info & OF_FBRA) == 0) {          /* Which is not a cond branch */
+                   !CE_UseLoadFlags (N)) {              /* Which does not use the flags */
                    Delete = 1;
                }
                break;
 
            case OP65_LDX:
-                       if (In->RegX >= 0                     && /* Value of X is known */
+                       if (RegValIsKnown (In->RegX)          && /* Value of X is known */
                    CE_KnownImm (E)                   && /* Value to be loaded is known */
                    In->RegX == (long) E->Num         && /* Both are equal */
                            (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
-                   (N->Info & OF_FBRA) == 0) {          /* Which is not a cond branch */
+                   !CE_UseLoadFlags (N)) {              /* Which does not use the flags */
                    Delete = 1;
                }
                break;
 
            case OP65_LDY:
-                       if (In->RegY >= 0                     && /* Value of Y is known */
+                       if (RegValIsKnown (In->RegY)          && /* Value of Y is known */
                    CE_KnownImm (E)                   && /* Value to be loaded is known */
                    In->RegY == (long) E->Num         && /* Both are equal */
                            (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
-                   (N->Info & OF_FBRA) == 0) {          /* Which is not a cond branch */
+                   !CE_UseLoadFlags (N)) {              /* Which does not use the flags */
                    Delete = 1;
                }
                break;
@@ -811,12 +858,10 @@ unsigned OptDupLoads (CodeSeg* S)
                 * location does already contain the value to be stored,
                 * remove the store.
                 */
-               if (In->RegA >= 0                     && /* Value of A is known */
+               if (RegValIsKnown (In->RegA)          && /* Value of A is known */
                    E->AM == AM65_ZP                  && /* Store into zp */
-                   (((E->Chg & REG_SREG_LO) != 0 &&     /* Store into sreg */
-                     In->RegA == In->SRegLo)       ||   /* Value identical */
-                            ((E->Chg & REG_SREG_HI) != 0 &&     /* Store into sreg+1 */
-                             In->RegA == In->SRegHi))) {        /* Value identical */
+                   In->RegA == RegVal (E->Chg, In)) {   /* Value identical */
+
                    Delete = 1;
                }
                break;
@@ -826,12 +871,10 @@ unsigned OptDupLoads (CodeSeg* S)
                 * location does already contain the value to be stored,
                 * remove the store.
                 */
-               if (In->RegX >= 0                     && /* Value of A is known */
+               if (RegValIsKnown (In->RegX)          && /* Value of A is known */
                    E->AM == AM65_ZP                  && /* Store into zp */
-                   (((E->Chg & REG_SREG_LO) != 0 &&     /* Store into sreg */
-                     In->RegX == In->SRegLo)       ||   /* Value identical */
-                            ((E->Chg & REG_SREG_HI) != 0 &&     /* Store into sreg+1 */
-                             In->RegX == In->SRegHi))) {        /* Value identical */
+                           In->RegX == RegVal (E->Chg, In)) {   /* Value identical */
+
                    Delete = 1;
 
                /* If the value in the X register is known and the same as
@@ -840,7 +883,7 @@ unsigned OptDupLoads (CodeSeg* S)
                 * later. STX does support the zeropage,y addressing mode,
                 * so be sure to check for that.
                 */
-                       } else if (In->RegX >= 0              &&
+                       } else if (RegValIsKnown (In->RegX)   &&
                           In->RegX == In->RegA       &&
                           E->AM != AM65_ABSY         &&
                           E->AM != AM65_ZPY) {
@@ -854,66 +897,77 @@ unsigned OptDupLoads (CodeSeg* S)
                 * location does already contain the value to be stored,
                 * remove the store.
                 */
-               if (In->RegX >= 0                     && /* Value of A is known */
+               if (RegValIsKnown (In->RegY)          && /* Value of Y is known */
                    E->AM == AM65_ZP                  && /* Store into zp */
-                   (((E->Chg & REG_SREG_LO) != 0 &&     /* Store into sreg */
-                     In->RegX == In->SRegLo)       ||   /* Value identical */
-                            ((E->Chg & REG_SREG_HI) != 0 &&     /* Store into sreg+1 */
-                             In->RegX == In->SRegHi))) {        /* Value identical */
+                           In->RegY == RegVal (E->Chg, In)) {   /* Value identical */
+
                    Delete = 1;
+
                /* If the value in the Y register is known and the same as
                 * that in the A register, replace the store by a STA. The
                 * optimizer will then remove the load instruction for Y
                 * later. If replacement by A is not possible try a
-                * replacement by X, but check for invalid addressing modes
+                * replacement by X, but check for invalid addressing modes
                 * in this case.
                 */
-                       } else if (In->RegY >= 0) {
+                       } else if (RegValIsKnown (In->RegY)) {
                    if (In->RegY == In->RegA) {
-                       CE_ReplaceOPC (E, OP65_STA);
+                       CE_ReplaceOPC (E, OP65_STA);
                    } else if (In->RegY == In->RegX   &&
-                              E->AM != AM65_ABSX     &&
-                              E->AM != AM65_ZPX) {
+                              E->AM != AM65_ABSX     &&
+                              E->AM != AM65_ZPX) {
                        CE_ReplaceOPC (E, OP65_STX);
                    }
                }
                break;
 
+           case OP65_STZ:
+               /* If we store into a known zero page location, and this
+                * location does already contain the value to be stored,
+                * remove the store.
+                */
+                       if (CPU >= CPU_65C02 && E->AM == AM65_ZP) {
+                   if (RegVal (E->Chg, In) == 0) {
+                        Delete = 1;
+                    }
+               }
+               break;
+
            case OP65_TAX:
-                if (In->RegA >= 0                     &&
+                if (RegValIsKnown (In->RegA)          &&
                    In->RegA == In->RegX              &&
                    (N = CS_GetNextEntry (S, I)) != 0 &&
-                   (N->Info & OF_FBRA) == 0) {
+                           !CE_UseLoadFlags (N)) {
                    /* Value is identical and not followed by a branch */
                    Delete = 1;
                }
                break;
 
            case OP65_TAY:
-                if (In->RegA >= 0                 &&
-                   In->RegA == In->RegY    &&
+                if (RegValIsKnown (In->RegA)            &&
+                   In->RegA == In->RegY                &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
-                   (N->Info & OF_FBRA) == 0) {
+                           !CE_UseLoadFlags (N)) {
                    /* Value is identical and not followed by a branch */
                    Delete = 1;
                }
                break;
 
                    case OP65_TXA:
-                if (In->RegX >= 0                 &&
-                   In->RegX == In->RegA    &&
+                if (RegValIsKnown (In->RegX)            &&
+                   In->RegX == In->RegA                &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
-                   (N->Info & OF_FBRA) == 0) {
+                           !CE_UseLoadFlags (N)) {
                    /* Value is identical and not followed by a branch */
                    Delete = 1;
                }
                break;
 
            case OP65_TYA:
-                if (In->RegY >= 0                 &&
-                   In->RegY == In->RegA    &&
+                if (RegValIsKnown (In->RegY)            &&
+                   In->RegY == In->RegA                &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
-                   (N->Info & OF_FBRA) == 0) {
+                           !CE_UseLoadFlags (N)) {
                    /* Value is identical and not followed by a branch */
                    Delete = 1;
                }
@@ -978,7 +1032,7 @@ unsigned OptStoreLoad (CodeSeg* S)
             (E->OPC == OP65_STY && N->OPC == OP65_LDY))    &&
            strcmp (E->Arg, N->Arg) == 0                    &&
            (X = CS_GetNextEntry (S, I+1)) != 0             &&
-                   (X->Info & OF_FBRA) == 0) {
+            !CE_UseLoadFlags (X)) {
 
                    /* Register has already the correct value, remove the load */
            CS_DelEntry (S, I+1);
@@ -1027,7 +1081,7 @@ unsigned OptTransfers (CodeSeg* S)
                    if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
                        (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
                        (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
-                       (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
+                       (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
 
                /* If the next insn is a conditional branch, check if the insn
                 * preceeding the first xfr will set the flags right, otherwise we
@@ -1036,24 +1090,24 @@ unsigned OptTransfers (CodeSeg* S)
                if ((X = CS_GetNextEntry (S, I+1)) == 0) {
                    goto NextEntry;
                }
-               if ((X->Info & OF_FBRA) != 0) {
-                   if (I == 0) {
+                if (CE_UseLoadFlags (X)) {
+                   if (I == 0) {
                        /* No preceeding entry */
                        goto NextEntry;
                    }
                    P = CS_GetEntry (S, I-1);
                    if ((P->Info & OF_SETF) == 0) {
                        /* Does not set the flags */
-                       goto NextEntry;
-                   }
-               }
+                       goto NextEntry;
+                   }
+               }
 
-               /* Remove both transfers */
-               CS_DelEntry (S, I+1);
-               CS_DelEntry (S, I);
+               /* Remove both transfers */
+               CS_DelEntry (S, I+1);
+               CS_DelEntry (S, I);
 
-               /* Remember, we had changes */
-               ++Changes;
+               /* Remember, we had changes */
+               ++Changes;
            }
        }
 
@@ -1069,6 +1123,87 @@ NextEntry:
 
 
 
+unsigned OptPushPop (CodeSeg* S)
+/* Remove a PHA/PLA sequence were A is not used later */
+{
+    unsigned Changes = 0;
+    unsigned Push    = 0;       /* Index of push insn */
+    unsigned Pop     = 0;       /* Index of pop insn */
+    enum {
+        Searching,
+        FoundPush,
+        FoundPop
+    } State = Searching;
+
+    /* Walk over the entries. Look for a push instruction that is followed by
+     * a pop later, where the pop is not followed by an conditional branch,
+     * and where the value of the A register is not used later on.
+     * Look out for the following problems:
+     *
+     *  - There may be another PHA/PLA inside the sequence: Restart it.
+     *  - If the PLA has a label, all jumps to this label must be inside
+     *    the sequence, otherwise we cannot remove the PHA/PLA.
+     */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       /* Get next entry */
+               CodeEntry* E = CS_GetEntry (S, I);
+
+        switch (State) {
+
+            case Searching:
+                if (E->OPC == OP65_PHA) {
+                    /* Found start of sequence */
+                    Push  = I;
+                    State = FoundPush;
+                }
+                break;
+
+            case FoundPush:
+                if (E->OPC == OP65_PHA) {
+                    /* Inner push/pop, restart */
+                    Push = I;
+                } else if (E->OPC == OP65_PLA) {
+                    /* Found a matching pop */
+                    Pop = I;
+                    State = FoundPop;
+                }
+                break;
+
+            case FoundPop:
+                /* Next insn, just check if it is no conditional branch and
+                 * that A is not used later. Check also that the range we have
+                 * found now is a basic block, which means that the PHA is the
+                 * only entrance and the PLA the only exit.
+                 */
+                if ((E->Info & OF_CBRA) == 0    &&
+                    !RegAUsed (S, I)            &&
+                    CS_IsBasicBlock (S, Push, Pop)) {
+                    /* We can remove the PHA and PLA instructions */
+                    CS_DelEntry (S, Pop);
+                    CS_DelEntry (S, Push);
+                    /* Correct I so we continue with the next insn */
+                    I -= 2;
+                    /* Remember we had changes */
+                    ++Changes;
+                }
+                /* Go into search mode again */
+                State = Searching;
+                break;
+
+        }
+
+       /* Next entry */
+       ++I;
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
 /*****************************************************************************/
 /*                          Optimize branch types                           */
 /*****************************************************************************/
@@ -1079,14 +1214,10 @@ unsigned OptBranchDist (CodeSeg* S)
 /* Change branches for the distance needed. */
 {
     unsigned Changes = 0;
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have not enough */
-    unsigned Count = CS_GetEntryCount (S);
 
     /* Walk over the entries */
-    I = 0;
-    while (I < Count) {
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
 
        /* Get next entry */
                CodeEntry* E = CS_GetEntry (S, I);