1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
47 /* Helper functions */
48 /*****************************************************************************/
52 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
53 /* Get the branch distance between the two entries and return it. The distance
54 * will be negative for backward jumps and positive for forward jumps.
57 /* Get the index of the branch target */
58 unsigned TI = CS_GetEntryIndex (S, To);
60 /* Determine the branch distance */
63 /* Forward branch, do not count the current insn */
66 CodeEntry* N = CS_GetEntry (S, J++);
73 CodeEntry* N = CS_GetEntry (S, J++);
78 /* Return the calculated distance */
84 static int IsShortDist (int Distance)
85 /* Return true if the given distance is a short branch distance */
87 return (Distance >= -125 && Distance <= 125);
92 static short RegVal (unsigned short Use, const RegContents* RC)
93 /* Return the contents of the given register */
95 if ((Use & REG_A) != 0) {
97 } else if ((Use & REG_X) != 0) {
99 } else if ((Use & REG_Y) != 0) {
101 } else if ((Use & REG_TMP1) != 0) {
103 } else if ((Use & REG_SREG_LO) != 0) {
105 } else if ((Use & REG_SREG_HI) != 0) {
114 /*****************************************************************************/
115 /* Replace jumps to RTS by RTS */
116 /*****************************************************************************/
120 unsigned OptRTSJumps1 (CodeSeg* S)
121 /* Replace jumps to RTS by RTS */
123 unsigned Changes = 0;
125 /* Walk over all entries minus the last one */
127 while (I < CS_GetEntryCount (S)) {
129 /* Get the next entry */
130 CodeEntry* E = CS_GetEntry (S, I);
132 /* Check if it's an unconditional branch to a local target */
133 if ((E->Info & OF_UBRA) != 0 &&
135 E->JumpTo->Owner->OPC == OP65_RTS) {
137 /* Insert an RTS instruction */
138 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
139 CS_InsertEntry (S, X, I+1);
141 /* Delete the jump */
144 /* Remember, we had changes */
154 /* Return the number of changes made */
160 unsigned OptRTSJumps2 (CodeSeg* S)
161 /* Replace long conditional jumps to RTS */
163 unsigned Changes = 0;
165 /* Walk over all entries minus the last one */
167 while (I < CS_GetEntryCount (S)) {
171 /* Get the next entry */
172 CodeEntry* E = CS_GetEntry (S, I);
174 /* Check if it's an unconditional branch to a local target */
175 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
176 (E->Info & OF_LBRA) != 0 && /* Long branch */
177 E->JumpTo != 0 && /* Local label */
178 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
179 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
185 /* We will create a jump around an RTS instead of the long branch */
186 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
187 CS_InsertEntry (S, X, I+1);
189 /* Get the new branch opcode */
190 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
192 /* Get the label attached to N, create a new one if needed */
193 LN = CS_GenLabel (S, N);
195 /* Generate the branch */
196 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
197 CS_InsertEntry (S, X, I+1);
199 /* Delete the long branch */
202 /* Remember, we had changes */
212 /* Return the number of changes made */
218 /*****************************************************************************/
219 /* Remove dead jumps */
220 /*****************************************************************************/
224 unsigned OptDeadJumps (CodeSeg* S)
225 /* Remove dead jumps (jumps to the next instruction) */
227 unsigned Changes = 0;
229 /* Walk over all entries minus the last one */
231 while (I < CS_GetEntryCount (S)) {
233 /* Get the next entry */
234 CodeEntry* E = CS_GetEntry (S, I);
236 /* Check if it's a branch, if it has a local target, and if the target
237 * is the next instruction.
239 if (E->AM == AM65_BRA &&
241 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
243 /* Delete the dead jump */
246 /* Remember, we had changes */
257 /* Return the number of changes made */
263 /*****************************************************************************/
264 /* Remove dead code */
265 /*****************************************************************************/
269 unsigned OptDeadCode (CodeSeg* S)
270 /* Remove dead code (code that follows an unconditional jump or an rts/rti
274 unsigned Changes = 0;
276 /* Walk over all entries */
278 while (I < CS_GetEntryCount (S)) {
283 CodeEntry* E = CS_GetEntry (S, I);
285 /* Check if it's an unconditional branch, and if the next entry has
288 if ((E->Info & OF_DEAD) != 0 &&
289 (N = CS_GetNextEntry (S, I)) != 0 &&
292 /* Delete the next entry */
293 CS_DelEntry (S, I+1);
295 /* Remember, we had changes */
306 /* Return the number of changes made */
312 /*****************************************************************************/
313 /* Optimize jump cascades */
314 /*****************************************************************************/
318 unsigned OptJumpCascades (CodeSeg* S)
319 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
320 * replaced by a jump to the final location. This will in some cases produce
321 * worse code, because some jump targets are no longer reachable by short
322 * branches, but this is quite rare, so there are more advantages than
326 unsigned Changes = 0;
328 /* Walk over all entries */
330 while (I < CS_GetEntryCount (S)) {
336 CodeEntry* E = CS_GetEntry (S, I);
338 /* Check if it's a branch, if it has a jump label, if this jump
339 * label is not attached to the instruction itself, and if the
340 * target instruction is itself a branch.
342 if ((E->Info & OF_BRA) != 0 &&
343 (OldLabel = E->JumpTo) != 0 &&
344 (N = OldLabel->Owner) != E &&
345 (N->Info & OF_BRA) != 0) {
347 /* Check if we can use the final target label. This is the case,
348 * if the target branch is an absolut branch, or if it is a
349 * conditional branch checking the same condition as the first one.
351 if ((N->Info & OF_UBRA) != 0 ||
352 ((E->Info & OF_CBRA) != 0 &&
353 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
355 /* This is a jump cascade and we may jump to the final target.
356 * Insert a new instruction, then remove the old one
358 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
360 /* Insert it behind E */
361 CS_InsertEntry (S, X, I+1);
366 /* Remember, we had changes */
374 /* Check if both are conditional branches, and the condition of
375 * the second is the inverse of that of the first. In this case,
376 * the second branch will never be taken, and we may jump directly
377 * to the instruction behind this one.
379 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
381 CodeEntry* X; /* Instruction behind N */
382 CodeLabel* LX; /* Label attached to X */
384 /* Get the branch conditions of both branches */
385 bc_t BC1 = GetBranchCond (E->OPC);
386 bc_t BC2 = GetBranchCond (N->OPC);
388 /* Check the branch conditions */
389 if (BC1 != GetInverseCond (BC2)) {
390 /* Condition not met */
394 /* We may jump behind this conditional branch. Get the
395 * pointer to the next instruction
397 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
398 /* N is the last entry, bail out */
402 /* Get the label attached to X, create a new one if needed */
403 LX = CS_GenLabel (S, X);
405 /* Move the reference from E to the new label */
406 CS_MoveLabelRef (S, E, LX);
408 /* Remember, we had changes */
423 /* Return the number of changes made */
429 /*****************************************************************************/
430 /* Optimize jsr/rts */
431 /*****************************************************************************/
435 unsigned OptRTS (CodeSeg* S)
436 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
437 * replaced by a jump. Don't bother to delete the RTS if it does not have a
438 * label, the dead code elimination should take care of it.
441 unsigned Changes = 0;
443 /* Walk over all entries minus the last one */
445 while (I < CS_GetEntryCount (S)) {
450 CodeEntry* E = CS_GetEntry (S, I);
452 /* Check if it's a subroutine call and if the following insn is RTS */
453 if (E->OPC == OP65_JSR &&
454 (N = CS_GetNextEntry (S, I)) != 0 &&
455 N->OPC == OP65_RTS) {
457 /* Change the jsr to a jmp and use the additional info for a jump */
459 CE_ReplaceOPC (E, OP65_JMP);
461 /* Remember, we had changes */
471 /* Return the number of changes made */
477 /*****************************************************************************/
478 /* Optimize jump targets */
479 /*****************************************************************************/
483 unsigned OptJumpTarget (CodeSeg* S)
484 /* If the instruction preceeding an unconditional branch is the same as the
485 * instruction preceeding the jump target, the jump target may be moved
486 * one entry back. This is a size optimization, since the instruction before
487 * the branch gets removed.
490 unsigned Changes = 0;
491 CodeEntry* E1; /* Entry 1 */
492 CodeEntry* E2; /* Entry 2 */
493 CodeEntry* T1; /* Jump target entry 1 */
494 CodeLabel* TL1; /* Target label 1 */
496 /* Walk over the entries */
498 while (I < CS_GetEntryCount (S)) {
501 E2 = CS_GetNextEntry (S, I);
503 /* Check if we have a jump or branch, and a matching label */
504 if (E2 && (E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
506 /* Get the entry preceeding the branch target */
507 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
509 /* There is no such entry */
513 /* Get the entry preceeding the jump */
514 E1 = CS_GetEntry (S, I);
516 /* Check if both preceeding instructions are identical */
517 if (!CodeEntriesAreEqual (E1, T1)) {
518 /* Not equal, try next */
522 /* Get the label for the instruction preceeding the jump target.
523 * This routine will create a new label if the instruction does
524 * not already have one.
526 TL1 = CS_GenLabel (S, T1);
528 /* Change the jump target to point to this new label */
529 CS_MoveLabelRef (S, E2, TL1);
531 /* If the instruction preceeding the jump has labels attached,
532 * move references to this label to the new label.
534 if (CE_HasLabel (E1)) {
535 CS_MoveLabels (S, E1, T1);
538 /* Remove the entry preceeding the jump */
541 /* Remember, we had changes */
552 /* Return the number of changes made */
558 /*****************************************************************************/
559 /* Optimize conditional branches */
560 /*****************************************************************************/
564 unsigned OptCondBranches (CodeSeg* S)
565 /* Performs several optimization steps:
567 * - If an immidiate load of a register is followed by a conditional jump that
568 * is never taken because the load of the register sets the flags in such a
569 * manner, remove the conditional branch.
570 * - If the conditional branch is always taken because of the register load,
571 * replace it by a jmp.
572 * - If a conditional branch jumps around an unconditional branch, remove the
573 * conditional branch and make the jump a conditional branch with the
574 * inverse condition of the first one.
577 unsigned Changes = 0;
579 /* Walk over the entries */
581 while (I < CS_GetEntryCount (S)) {
587 CodeEntry* E = CS_GetEntry (S, I);
589 /* Check if it's a register load */
590 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
591 E->AM == AM65_IMM && /* ..with immidiate addressing */
592 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
593 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
594 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
595 !CE_HasLabel (N)) { /* ..and does not have a label */
597 /* Get the branch condition */
598 bc_t BC = GetBranchCond (N->OPC);
600 /* Check the argument against the branch condition */
601 if ((BC == BC_EQ && E->Num != 0) ||
602 (BC == BC_NE && E->Num == 0) ||
603 (BC == BC_PL && (E->Num & 0x80) != 0) ||
604 (BC == BC_MI && (E->Num & 0x80) == 0)) {
606 /* Remove the conditional branch */
607 CS_DelEntry (S, I+1);
609 /* Remember, we had changes */
612 } else if ((BC == BC_EQ && E->Num == 0) ||
613 (BC == BC_NE && E->Num != 0) ||
614 (BC == BC_PL && (E->Num & 0x80) == 0) ||
615 (BC == BC_MI && (E->Num & 0x80) != 0)) {
617 /* The branch is always taken, replace it by a jump */
618 CE_ReplaceOPC (N, OP65_JMP);
620 /* Remember, we had changes */
626 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
627 (L = E->JumpTo) != 0 && /* ..referencing a local label */
628 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
629 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
630 !CE_HasLabel (N) && /* ..has no label attached */
631 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
633 /* Replace the jump by a conditional branch with the inverse branch
634 * condition than the branch around it.
636 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
638 /* Remove the conditional branch */
641 /* Remember, we had changes */
651 /* Return the number of changes made */
657 /*****************************************************************************/
658 /* Remove unused loads and stores */
659 /*****************************************************************************/
663 unsigned OptUnusedLoads (CodeSeg* S)
664 /* Remove loads of registers where the value loaded is not used later. */
666 unsigned Changes = 0;
668 /* Walk over the entries */
670 while (I < CS_GetEntryCount (S)) {
675 CodeEntry* E = CS_GetEntry (S, I);
677 /* Check if it's a register load or transfer insn */
678 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
679 (N = CS_GetNextEntry (S, I)) != 0 &&
680 (N->Info & OF_FBRA) == 0) {
682 /* Check which sort of load or transfer it is */
689 case OP65_TYA: R = REG_A; break;
693 case OP65_TAX: R = REG_X; break;
697 case OP65_TAY: R = REG_Y; break;
698 default: goto NextEntry; /* OOPS */
701 /* Get register usage and check if the register value is used later */
702 if ((GetRegInfo (S, I+1, R) & R) == 0) {
704 /* Register value is not used, remove the load */
707 /* Remember, we had changes */
719 /* Return the number of changes made */
725 unsigned OptUnusedStores (CodeSeg* S)
726 /* Remove stores into zero page registers that aren't used later */
728 unsigned Changes = 0;
730 /* Walk over the entries */
732 while (I < CS_GetEntryCount (S)) {
735 CodeEntry* E = CS_GetEntry (S, I);
737 /* Check if it's a register load or transfer insn */
738 if ((E->Info & OF_STORE) != 0 &&
740 (E->Chg & REG_ZP) != 0) {
742 /* Check for the zero page location. We know that there cannot be
743 * more than one zero page location involved in the store.
745 unsigned R = E->Chg & REG_ZP;
747 /* Get register usage and check if the register value is used later */
748 if ((GetRegInfo (S, I+1, R) & R) == 0) {
750 /* Register value is not used, remove the load */
753 /* Remember, we had changes */
764 /* Return the number of changes made */
770 unsigned OptDupLoads (CodeSeg* S)
771 /* Remove loads of registers where the value loaded is already in the register. */
773 unsigned Changes = 0;
776 /* Generate register info for this step */
779 /* Walk over the entries */
781 while (I < CS_GetEntryCount (S)) {
786 CodeEntry* E = CS_GetEntry (S, I);
788 /* Assume we won't delete the entry */
791 /* Get a pointer to the input registers of the insn */
792 const RegContents* In = &E->RI->In;
794 /* Handle the different instructions */
798 if (In->RegA >= 0 && /* Value of A is known */
799 CE_KnownImm (E) && /* Value to be loaded is known */
800 In->RegA == (long) E->Num && /* Both are equal */
801 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
802 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
808 if (In->RegX >= 0 && /* Value of X is known */
809 CE_KnownImm (E) && /* Value to be loaded is known */
810 In->RegX == (long) E->Num && /* Both are equal */
811 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
812 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
818 if (In->RegY >= 0 && /* Value of Y is known */
819 CE_KnownImm (E) && /* Value to be loaded is known */
820 In->RegY == (long) E->Num && /* Both are equal */
821 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
822 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
828 /* If we store into a known zero page location, and this
829 * location does already contain the value to be stored,
832 if (In->RegA >= 0 && /* Value of A is known */
833 E->AM == AM65_ZP && /* Store into zp */
834 In->RegA == RegVal (E->Chg, In)) { /* Value identical */
841 /* If we store into a known zero page location, and this
842 * location does already contain the value to be stored,
845 if (In->RegX >= 0 && /* Value of A is known */
846 E->AM == AM65_ZP && /* Store into zp */
847 In->RegX == RegVal (E->Chg, In)) { /* Value identical */
851 /* If the value in the X register is known and the same as
852 * that in the A register, replace the store by a STA. The
853 * optimizer will then remove the load instruction for X
854 * later. STX does support the zeropage,y addressing mode,
855 * so be sure to check for that.
857 } else if (In->RegX >= 0 &&
858 In->RegX == In->RegA &&
859 E->AM != AM65_ABSY &&
861 /* Use the A register instead */
862 CE_ReplaceOPC (E, OP65_STA);
867 /* If we store into a known zero page location, and this
868 * location does already contain the value to be stored,
871 if (In->RegY >= 0 && /* Value of Y is known */
872 E->AM == AM65_ZP && /* Store into zp */
873 In->RegX == RegVal (E->Chg, In)) { /* Value identical */
877 /* If the value in the Y register is known and the same as
878 * that in the A register, replace the store by a STA. The
879 * optimizer will then remove the load instruction for Y
880 * later. If replacement by A is not possible try a
881 * replacement by X, but check for invalid addressing modes
884 } else if (In->RegY >= 0) {
885 if (In->RegY == In->RegA) {
886 CE_ReplaceOPC (E, OP65_STA);
887 } else if (In->RegY == In->RegX &&
888 E->AM != AM65_ABSX &&
890 CE_ReplaceOPC (E, OP65_STX);
896 /* If we store into a known zero page location, and this
897 * location does already contain the value to be stored,
900 if (CPU >= CPU_65C02 && E->AM == AM65_ZP) {
901 if (RegVal (E->Chg, In) == 0) {
909 In->RegA == In->RegX &&
910 (N = CS_GetNextEntry (S, I)) != 0 &&
911 (N->Info & OF_FBRA) == 0) {
912 /* Value is identical and not followed by a branch */
919 In->RegA == In->RegY &&
920 (N = CS_GetNextEntry (S, I)) != 0 &&
921 (N->Info & OF_FBRA) == 0) {
922 /* Value is identical and not followed by a branch */
929 In->RegX == In->RegA &&
930 (N = CS_GetNextEntry (S, I)) != 0 &&
931 (N->Info & OF_FBRA) == 0) {
932 /* Value is identical and not followed by a branch */
939 In->RegY == In->RegA &&
940 (N = CS_GetNextEntry (S, I)) != 0 &&
941 (N->Info & OF_FBRA) == 0) {
942 /* Value is identical and not followed by a branch */
952 /* Delete the entry if requested */
955 /* Register value is not used, remove the load */
958 /* Remember, we had changes */
970 /* Free register info */
973 /* Return the number of changes made */
979 unsigned OptStoreLoad (CodeSeg* S)
980 /* Remove a store followed by a load from the same location. */
982 unsigned Changes = 0;
984 /* Walk over the entries */
986 while (I < CS_GetEntryCount (S)) {
992 CodeEntry* E = CS_GetEntry (S, I);
994 /* Check if it is a store instruction followed by a load from the
995 * same address which is itself not followed by a conditional branch.
997 if ((E->Info & OF_STORE) != 0 &&
998 (N = CS_GetNextEntry (S, I)) != 0 &&
1001 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
1002 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
1003 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
1004 strcmp (E->Arg, N->Arg) == 0 &&
1005 (X = CS_GetNextEntry (S, I+1)) != 0 &&
1006 (X->Info & OF_FBRA) == 0) {
1008 /* Register has already the correct value, remove the load */
1009 CS_DelEntry (S, I+1);
1011 /* Remember, we had changes */
1021 /* Return the number of changes made */
1027 unsigned OptTransfers (CodeSeg* S)
1028 /* Remove transfers from one register to another and back */
1030 unsigned Changes = 0;
1032 /* Walk over the entries */
1034 while (I < CS_GetEntryCount (S)) {
1040 /* Get next entry */
1041 CodeEntry* E = CS_GetEntry (S, I);
1043 /* Check if it is a store instruction followed by a load from the
1044 * same address which is itself not followed by a conditional branch.
1046 if ((E->Info & OF_XFR) != 0 &&
1047 (N = CS_GetNextEntry (S, I)) != 0 &&
1049 (N->Info & OF_XFR) != 0) {
1051 /* Check if it's a transfer and back */
1052 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1053 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1054 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1055 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
1057 /* If the next insn is a conditional branch, check if the insn
1058 * preceeding the first xfr will set the flags right, otherwise we
1059 * may not remove the sequence.
1061 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1064 if ((X->Info & OF_FBRA) != 0) {
1066 /* No preceeding entry */
1069 P = CS_GetEntry (S, I-1);
1070 if ((P->Info & OF_SETF) == 0) {
1071 /* Does not set the flags */
1076 /* Remove both transfers */
1077 CS_DelEntry (S, I+1);
1080 /* Remember, we had changes */
1091 /* Return the number of changes made */
1097 /*****************************************************************************/
1098 /* Optimize branch types */
1099 /*****************************************************************************/
1103 unsigned OptBranchDist (CodeSeg* S)
1104 /* Change branches for the distance needed. */
1106 unsigned Changes = 0;
1108 /* Walk over the entries */
1110 while (I < CS_GetEntryCount (S)) {
1112 /* Get next entry */
1113 CodeEntry* E = CS_GetEntry (S, I);
1115 /* Check if it's a conditional branch to a local label. */
1116 if (E->Info & OF_CBRA) {
1118 /* Is this a branch to a local symbol? */
1119 if (E->JumpTo != 0) {
1121 /* Check if the branch distance is short */
1122 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1124 /* Make the branch short/long according to distance */
1125 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1126 /* Short branch but long distance */
1127 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1129 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1130 /* Long branch but short distance */
1131 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1135 } else if ((E->Info & OF_LBRA) == 0) {
1137 /* Short branch to external symbol - make it long */
1138 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1143 } else if (CPU == CPU_65C02 &&
1144 (E->Info & OF_UBRA) != 0 &&
1146 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1148 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1149 CE_ReplaceOPC (E, OP65_BRA);
1158 /* Return the number of changes made */