]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptind.c
Better code for compares
[cc65] / src / cc65 / coptind.c
index 33b8f660d32b45ea7f7ff84aaf4e59f2191fcdc4..605c70317ca0e78511477e76aa906cba5dd65e89 100644 (file)
 
 
 
-#include <string.h>
-
 /* cc65 */
 #include "codeent.h"
 #include "codeinfo.h"
 #include "codeopt.h"
+#include "cpu.h"
 #include "error.h"
 #include "coptind.h"
 
 
 
 /*****************************************************************************/
-/*                                 Macros                                   */
+/*                            Helper functions                              */
 /*****************************************************************************/
 
 
 
-/* Macro to increment and decrement register contents if they're valid */
-#define INC(reg,val)    if ((reg) >= 0) (reg) = ((reg) + val) & 0xFF
-#define DEC(reg,val)           if ((reg) >= 0) (reg) = ((reg) - val) & 0xFF
+static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
+/* Get the branch distance between the two entries and return it. The distance
+ * will be negative for backward jumps and positive for forward jumps.
+ */
+{
+    /* Get the index of the branch target */
+    unsigned TI = CS_GetEntryIndex (S, To);
+
+    /* Determine the branch distance */
+    int Distance = 0;
+    if (TI >= From) {
+       /* Forward branch, do not count the current insn */
+       unsigned J = From+1;
+       while (J < TI) {
+           CodeEntry* N = CS_GetEntry (S, J++);
+           Distance += N->Size;
+       }
+    } else {
+       /* Backward branch */
+       unsigned J = TI;
+       while (J < From) {
+           CodeEntry* N = CS_GetEntry (S, J++);
+                   Distance -= N->Size;
+       }
+    }
+
+    /* Return the calculated distance */
+    return Distance;
+}
+
+
+
+static int IsShortDist (int Distance)
+/* Return true if the given distance is a short branch distance */
+{
+    return (Distance >= -125 && Distance <= 125);
+}
 
 
 
 /*****************************************************************************/
-/*                       Replace jumps to RTS by RTS                        */
+/*                       Replace jumps to RTS by RTS                        */
 /*****************************************************************************/
 
 
 
-unsigned OptRTSJumps (CodeSeg* S)
+unsigned OptRTSJumps1 (CodeSeg* S)
 /* Replace jumps to RTS by RTS */
 {
     unsigned Changes = 0;
@@ -102,6 +135,64 @@ unsigned OptRTSJumps (CodeSeg* S)
 
 
 
+unsigned OptRTSJumps2 (CodeSeg* S)
+/* Replace long conditional jumps to RTS */
+{
+    unsigned Changes = 0;
+
+    /* Walk over all entries minus the last one */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       CodeEntry* N;
+
+       /* Get the next entry */
+       CodeEntry* E = CS_GetEntry (S, I);
+
+               /* Check if it's an unconditional branch to a local target */
+               if ((E->Info & OF_CBRA) != 0            &&   /* Conditional branch */
+           (E->Info & OF_LBRA) != 0            &&   /* Long branch */
+           E->JumpTo != 0                      &&   /* Local label */
+           E->JumpTo->Owner->OPC == OP65_RTS   &&   /* Target is an RTS */
+           (N = CS_GetNextEntry (S, I)) != 0) {     /* There is a next entry */
+
+           CodeEntry* X;
+           CodeLabel* LN;
+           opc_t      NewBranch;
+
+           /* We will create a jump around an RTS instead of the long branch */
+           X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
+           CS_InsertEntry (S, X, I+1);
+
+           /* Get the new branch opcode */
+           NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
+
+           /* Get the label attached to N, create a new one if needed */
+           LN = CS_GenLabel (S, N);
+
+           /* Generate the branch */
+           X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
+           CS_InsertEntry (S, X, I+1);
+
+           /* Delete the long branch */
+           CS_DelEntry (S, I);
+
+           /* Remember, we had changes */
+           ++Changes;
+
+       }
+
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
 /*****************************************************************************/
 /*                            Remove dead jumps                             */
 /*****************************************************************************/
@@ -112,33 +203,24 @@ unsigned OptDeadJumps (CodeSeg* S)
 /* Remove dead jumps (jumps to the next instruction) */
 {
     unsigned Changes = 0;
-    CodeEntry* E;
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have less than two entries */
-    unsigned Count = CS_GetEntryCount (S);
-    if (Count < 2) {
-       return 0;
-    }
 
     /* Walk over all entries minus the last one */
-    I = 0;
-    while (I < Count-1) {
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
 
        /* Get the next entry */
-       E = CS_GetEntry (S, I);
+       CodeEntry* E = CS_GetEntry (S, I);
 
        /* 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);
 
-           /* Keep the number of entries updated */
-           --Count;
-
            /* Remember, we had changes */
            ++Changes;
 
@@ -168,17 +250,10 @@ unsigned OptDeadCode (CodeSeg* S)
  */
 {
     unsigned Changes = 0;
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have less than two entries */
-    unsigned Count = CS_GetEntryCount (S);
-    if (Count < 2) {
-       return 0;
-    }
 
     /* Walk over all entries */
-    I = 0;
-    while (I < Count) {
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
 
        CodeEntry* N;
 
@@ -195,9 +270,6 @@ unsigned OptDeadCode (CodeSeg* S)
            /* Delete the next entry */
            CS_DelEntry (S, I+1);
 
-           /* Keep the number of entries updated */
-           --Count;
-
            /* Remember, we had changes */
            ++Changes;
 
@@ -345,17 +417,10 @@ unsigned OptRTS (CodeSeg* S)
  */
 {
     unsigned Changes = 0;
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have less than 2 entries */
-    unsigned Count = CS_GetEntryCount (S);
-    if (Count < 2) {
-       return 0;
-    }
 
     /* Walk over all entries minus the last one */
-    I = 0;
-    while (I < Count-1) {
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
 
        CodeEntry* N;
 
@@ -404,37 +469,24 @@ 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 */
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have not enough */
-    unsigned Count = CS_GetEntryCount (S);
-    if (Count < 3) {
-       return 0;
-    }
 
     /* Walk over the entries */
-    I = 0;
-    while (I < Count-1) {
+    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;
+               if (E2 && (E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
 
-           /* Get the entry preceeding this one (if possible) */
-           TI = CS_GetEntryIndex (S, T2);
-           if (TI == 0) {
-               /* There is no entry before this one */
+           /* 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);
@@ -463,7 +515,6 @@ unsigned OptJumpTarget (CodeSeg* S)
 
            /* Remove the entry preceeding the jump */
            CS_DelEntry (S, I);
-           --Count;
 
                    /* Remember, we had changes */
            ++Changes;
@@ -502,17 +553,10 @@ unsigned OptCondBranches (CodeSeg* S)
  */
 {
     unsigned Changes = 0;
-    unsigned I;
-
-    /* Get the number of entries, bail out if we have not enough */
-    unsigned Count = CS_GetEntryCount (S);
-    if (Count < 2) {
-       return 0;
-    }
 
     /* Walk over the entries */
-    I = 0;
-    while (I < Count-1) {
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
 
        CodeEntry* N;
        CodeLabel* L;
@@ -539,7 +583,6 @@ unsigned OptCondBranches (CodeSeg* S)
 
                /* Remove the conditional branch */
                CS_DelEntry (S, I+1);
-               --Count;
 
                /* Remember, we had changes */
                ++Changes;
@@ -572,7 +615,6 @@ unsigned OptCondBranches (CodeSeg* S)
 
            /* Remove the conditional branch */
            CS_DelEntry (S, I);
-           --Count;
 
            /* Remember, we had changes */
            ++Changes;
@@ -591,7 +633,7 @@ unsigned OptCondBranches (CodeSeg* S)
 
 
 /*****************************************************************************/
-/*                           Remove unused loads                            */
+/*                     Remove unused loads and stores                       */
 /*****************************************************************************/
 
 
@@ -611,25 +653,31 @@ 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)) != 0    &&
-           (N = CS_GetNextEntry (S, I)) != 0      &&
+       if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0  &&
+           (N = CS_GetNextEntry (S, I)) != 0                    &&
            (N->Info & OF_FBRA) == 0) {
 
            /* 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_TXA:
-               case OP65_TYA:
-               case OP65_LDA:  R = REG_A;      break;
-               case OP65_TAX:
-                       case OP65_LDX:  R = REG_X;      break;
-               case OP65_TAY:
-               case OP65_LDY:  R = REG_Y;      break;
+               case OP65_TYA:  R = REG_A;      break;
+               case OP65_DEX:
+               case OP65_INX:
+               case OP65_LDX:
+               case OP65_TAX:  R = REG_X;      break;
+               case OP65_DEY:
+               case OP65_INY:
+                       case OP65_LDY:
+               case OP65_TAY:  R = REG_Y;      break;
                default:        goto NextEntry;         /* OOPS */
            }
 
            /* Get register usage and check if the register value is used later */
-           if ((GetRegInfo (S, I+1) & R) == 0) {
+           if ((GetRegInfo (S, I+1, R) & R) == 0) {
 
                /* Register value is not used, remove the load */
                CS_DelEntry (S, I);
@@ -652,7 +700,52 @@ NextEntry:
 
 
 
-unsigned OptDuplicateLoads (CodeSeg* S)
+unsigned OptUnusedStores (CodeSeg* S)
+/* Remove stores into zero page registers that aren't used later */
+{
+    unsigned Changes = 0;
+
+    /* Walk over the entries */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       /* Get next entry */
+               CodeEntry* E = CS_GetEntry (S, I);
+
+       /* Check if it's a register load or transfer insn */
+               if ((E->Info & OF_STORE) != 0    &&
+           E->AM == AM65_ZP             &&
+           (E->Chg & REG_ZP) != 0) {
+
+           /* Check for the zero page location. We know that there cannot be
+            * more than one zero page location involved in the store.
+            */
+           unsigned R = E->Chg & REG_ZP;
+
+           /* Get register usage and check if the register value is used later */
+           if ((GetRegInfo (S, I+1, R) & R) == 0) {
+
+               /* Register value is not used, remove the load */
+               CS_DelEntry (S, I);
+
+               /* Remember, we had changes */
+               ++Changes;
+
+           }
+       }
+
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
+unsigned OptDupLoads (CodeSeg* S)
 /* Remove loads of registers where the value loaded is already in the register. */
 {
     unsigned Changes = 0;
@@ -673,13 +766,16 @@ unsigned OptDuplicateLoads (CodeSeg* S)
        /* Assume we won't delete the entry */
        int Delete = 0;
 
+               /* Get a pointer to the input registers of the insn */
+       const RegContents* In  = &E->RI->In;
+
        /* Handle the different instructions */
        switch (E->OPC) {
 
            case OP65_LDA:
-               if (E->RI->In.RegA >= 0               && /* Value of A is known */
+                       if (In->RegA >= 0                     && /* Value of A is known */
                            CE_KnownImm (E)                   && /* Value to be loaded is known */
-                           E->RI->In.RegA == E->Num          && /* Both are equal */
+                           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 */
                    Delete = 1;
@@ -687,9 +783,9 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
            case OP65_LDX:
-                       if (E->RI->In.RegX >= 0               && /* Value of X is known */
+                       if (In->RegX >= 0                     && /* Value of X is known */
                    CE_KnownImm (E)                   && /* Value to be loaded is known */
-                   E->RI->In.RegX == E->Num          && /* Both are equal */
+                   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 */
                    Delete = 1;
@@ -697,44 +793,82 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
            case OP65_LDY:
-                       if (E->RI->In.RegY >= 0               && /* Value of Y is known */
+                       if (In->RegY >= 0                     && /* Value of Y is known */
                    CE_KnownImm (E)                   && /* Value to be loaded is known */
-                   E->RI->In.RegY == E->Num          && /* Both are equal */
+                   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 */
                    Delete = 1;
                }
                break;
 
+           case OP65_STA:
+               /* If we store into a known zero page location, and this
+                * location does already contain the value to be stored,
+                * remove the store.
+                */
+               if (In->RegA >= 0                     && /* 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 */
+                   Delete = 1;
+               }
+               break;
+
            case OP65_STX:
-               /* If the value in the X register is known and the same as
+               /* If we store into a known zero page location, and this
+                * location does already contain the value to be stored,
+                * remove the store.
+                */
+               if (In->RegX >= 0                     && /* 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 */
+                   Delete = 1;
+
+               /* If the value in the X 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 X
                 * later. STX does support the zeropage,y addressing mode,
                 * so be sure to check for that.
                 */
-                       if (E->RI->In.RegX >= 0               &&
-                   E->RI->In.RegX == E->RI->In.RegA  &&
-                   E->AM != AM65_ABSY                &&
-                   E->AM != AM65_ZPY) {
+                       } else if (In->RegX >= 0              &&
+                          In->RegX == In->RegA       &&
+                          E->AM != AM65_ABSY         &&
+                          E->AM != AM65_ZPY) {
                    /* Use the A register instead */
                            CE_ReplaceOPC (E, OP65_STA);
                }
                break;
 
            case OP65_STY:
+               /* If we store into a known zero page location, and this
+                * location does already contain the value to be stored,
+                * remove the store.
+                */
+               if (In->RegX >= 0                     && /* 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 */
+                   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.
                 */
-                       if (E->RI->In.RegY >= 0) {
-                   if (E->RI->In.RegY == E->RI->In.RegA) {
+                       } else if (In->RegY >= 0) {
+                   if (In->RegY == In->RegA) {
                        CE_ReplaceOPC (E, OP65_STA);
-                   } else if (E->RI->In.RegY == E->RI->In.RegX &&
-                              E->AM != AM65_ABSX               &&
+                   } else if (In->RegY == In->RegX   &&
+                              E->AM != AM65_ABSX     &&
                               E->AM != AM65_ZPX) {
                        CE_ReplaceOPC (E, OP65_STX);
                    }
@@ -742,9 +876,9 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
            case OP65_TAX:
-                if (E->RI->In.RegA >= 0                 &&
-                   E->RI->In.RegA == E->RI->In.RegX    &&
-                   (N = CS_GetNextEntry (S, I)) != 0   &&
+                if (In->RegA >= 0                     &&
+                   In->RegA == In->RegX              &&
+                   (N = CS_GetNextEntry (S, I)) != 0 &&
                    (N->Info & OF_FBRA) == 0) {
                    /* Value is identical and not followed by a branch */
                    Delete = 1;
@@ -752,8 +886,8 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
            case OP65_TAY:
-                if (E->RI->In.RegA >= 0                 &&
-                   E->RI->In.RegA == E->RI->In.RegY    &&
+                if (In->RegA >= 0                 &&
+                   In->RegA == In->RegY    &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
                    (N->Info & OF_FBRA) == 0) {
                    /* Value is identical and not followed by a branch */
@@ -762,8 +896,8 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
                    case OP65_TXA:
-                if (E->RI->In.RegX >= 0                 &&
-                   E->RI->In.RegX == E->RI->In.RegA    &&
+                if (In->RegX >= 0                 &&
+                   In->RegX == In->RegA    &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
                    (N->Info & OF_FBRA) == 0) {
                    /* Value is identical and not followed by a branch */
@@ -772,8 +906,8 @@ unsigned OptDuplicateLoads (CodeSeg* S)
                break;
 
            case OP65_TYA:
-                if (E->RI->In.RegY >= 0                 &&
-                   E->RI->In.RegY == E->RI->In.RegA    &&
+                if (In->RegY >= 0                 &&
+                   In->RegY == In->RegA    &&
                    (N = CS_GetNextEntry (S, I)) != 0   &&
                    (N->Info & OF_FBRA) == 0) {
                    /* Value is identical and not followed by a branch */
@@ -831,15 +965,18 @@ unsigned OptStoreLoad (CodeSeg* S)
        /* Check if it is a store instruction followed by a load from the
         * same address which is itself not followed by a conditional branch.
         */
-       if ((E->Info & OF_STORE) != 0                 &&
-           (N = CS_GetNextEntry (S, I)) != 0         &&
-           !CE_HasLabel (N)                          &&
-                   (N->Info & OF_LOAD) != 0                  &&
-           strcmp (E->Arg, N->Arg) == 0              &&
-           (X = CS_GetNextEntry (S, I+1)) != 0       &&
+       if ((E->Info & OF_STORE) != 0                       &&
+           (N = CS_GetNextEntry (S, I)) != 0               &&
+           !CE_HasLabel (N)                                &&
+           E->AM == N->AM                                  &&
+                   ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
+            (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
+            (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) {
 
-           /* Register value is not used, remove the load */
+                   /* Register has already the correct value, remove the load */
            CS_DelEntry (S, I+1);
 
            /* Remember, we had changes */
@@ -858,6 +995,76 @@ unsigned OptStoreLoad (CodeSeg* S)
 
 
 
+unsigned OptTransfers (CodeSeg* S)
+/* Remove transfers from one register to another and back */
+{
+    unsigned Changes = 0;
+
+    /* Walk over the entries */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       CodeEntry* N;
+       CodeEntry* X;
+       CodeEntry* P;
+
+       /* Get next entry */
+               CodeEntry* E = CS_GetEntry (S, I);
+
+       /* Check if it is a store instruction followed by a load from the
+        * same address which is itself not followed by a conditional branch.
+        */
+               if ((E->Info & OF_XFR) != 0                 &&
+           (N = CS_GetNextEntry (S, I)) != 0       &&
+           !CE_HasLabel (N)                        &&
+                   (N->Info & OF_XFR) != 0) {
+
+           /* Check if it's a transfer and back */
+                   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))) {
+
+               /* If the next insn is a conditional branch, check if the insn
+                * preceeding the first xfr will set the flags right, otherwise we
+                * may not remove the sequence.
+                */
+               if ((X = CS_GetNextEntry (S, I+1)) == 0) {
+                   goto NextEntry;
+               }
+               if ((X->Info & OF_FBRA) != 0) {
+                   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;
+                   }
+               }
+
+               /* Remove both transfers */
+               CS_DelEntry (S, I+1);
+               CS_DelEntry (S, I);
+
+               /* Remember, we had changes */
+               ++Changes;
+           }
+       }
+
+NextEntry:
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
 /*****************************************************************************/
 /*                          Optimize branch types                           */
 /*****************************************************************************/
@@ -868,51 +1075,29 @@ 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);
 
        /* Check if it's a conditional branch to a local label. */
-               if ((E->Info & OF_CBRA) != 0) {
+               if (E->Info & OF_CBRA) {
 
            /* Is this a branch to a local symbol? */
            if (E->JumpTo != 0) {
 
-               /* Get the index of the branch target */
-               unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
-
-               /* Determine the branch distance */
-               int Distance = 0;
-               if (TI >= I) {
-                   /* Forward branch */
-                   unsigned J = I;
-                   while (J < TI) {
-                       CodeEntry* N = CS_GetEntry (S, J++);
-                       Distance += N->Size;
-                   }
-               } else {
-                   /* Backward branch */
-                   unsigned J = TI;
-                   while (J < I) {
-                       CodeEntry* N = CS_GetEntry (S, J++);
-                       Distance += N->Size;
-                   }
-               }
+                       /* Check if the branch distance is short */
+                       int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
 
                /* Make the branch short/long according to distance */
-               if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
+               if ((E->Info & OF_LBRA) == 0 && !IsShort) {
                    /* Short branch but long distance */
                    CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
                    ++Changes;
-               } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
+               } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
                    /* Long branch but short distance */
                    CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
                    ++Changes;
@@ -925,6 +1110,15 @@ unsigned OptBranchDist (CodeSeg* S)
                ++Changes;
 
            }
+
+               } else if (CPU == CPU_65C02                                      &&
+                  (E->Info & OF_UBRA) != 0                              &&
+                  E->JumpTo != 0                                        &&
+                  IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
+
+           /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
+           CE_ReplaceOPC (E, OP65_BRA);
+           ++Changes;
        }
 
        /* Next entry */