1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001 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 /*****************************************************************************/
45 /*****************************************************************************/
46 /* Replace jumps to RTS by RTS */
47 /*****************************************************************************/
51 unsigned OptRTSJumps1 (CodeSeg* S)
52 /* Replace jumps to RTS by RTS */
56 /* Walk over all entries minus the last one */
58 while (I < CS_GetEntryCount (S)) {
60 /* Get the next entry */
61 CodeEntry* E = CS_GetEntry (S, I);
63 /* Check if it's an unconditional branch to a local target */
64 if ((E->Info & OF_UBRA) != 0 &&
66 E->JumpTo->Owner->OPC == OP65_RTS) {
68 /* Insert an RTS instruction */
69 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
70 CS_InsertEntry (S, X, I+1);
75 /* Remember, we had changes */
85 /* Return the number of changes made */
91 unsigned OptRTSJumps2 (CodeSeg* S)
92 /* Replace long conditional jumps to RTS */
96 /* Walk over all entries minus the last one */
98 while (I < CS_GetEntryCount (S)) {
102 /* Get the next entry */
103 CodeEntry* E = CS_GetEntry (S, I);
105 /* Check if it's an unconditional branch to a local target */
106 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
107 (E->Info & OF_LBRA) != 0 && /* Long branch */
108 E->JumpTo != 0 && /* Local label */
109 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
110 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
116 /* We will create a jump around an RTS instead of the long branch */
117 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
118 CS_InsertEntry (S, X, I+1);
120 /* Get the new branch opcode */
121 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
123 /* Get the label attached to N, create a new one if needed */
124 LN = CS_GenLabel (S, N);
126 /* Generate the branch */
127 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
128 CS_InsertEntry (S, X, I+1);
130 /* Delete the long branch */
133 /* Remember, we had changes */
143 /* Return the number of changes made */
149 /*****************************************************************************/
150 /* Remove dead jumps */
151 /*****************************************************************************/
155 unsigned OptDeadJumps (CodeSeg* S)
156 /* Remove dead jumps (jumps to the next instruction) */
158 unsigned Changes = 0;
162 /* Get the number of entries, bail out if we have less than two entries */
163 unsigned Count = CS_GetEntryCount (S);
168 /* Walk over all entries minus the last one */
170 while (I < Count-1) {
172 /* Get the next entry */
173 E = CS_GetEntry (S, I);
175 /* Check if it's a branch, if it has a local target, and if the target
176 * is the next instruction.
178 if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
180 /* Delete the dead jump */
183 /* Keep the number of entries updated */
186 /* Remember, we had changes */
197 /* Return the number of changes made */
203 /*****************************************************************************/
204 /* Remove dead code */
205 /*****************************************************************************/
209 unsigned OptDeadCode (CodeSeg* S)
210 /* Remove dead code (code that follows an unconditional jump or an rts/rti
214 unsigned Changes = 0;
217 /* Get the number of entries, bail out if we have less than two entries */
218 unsigned Count = CS_GetEntryCount (S);
223 /* Walk over all entries */
230 CodeEntry* E = CS_GetEntry (S, I);
232 /* Check if it's an unconditional branch, and if the next entry has
235 if ((E->Info & OF_DEAD) != 0 &&
236 (N = CS_GetNextEntry (S, I)) != 0 &&
239 /* Delete the next entry */
240 CS_DelEntry (S, I+1);
242 /* Keep the number of entries updated */
245 /* Remember, we had changes */
256 /* Return the number of changes made */
262 /*****************************************************************************/
263 /* Optimize jump cascades */
264 /*****************************************************************************/
268 unsigned OptJumpCascades (CodeSeg* S)
269 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
270 * replaced by a jump to the final location. This will in some cases produce
271 * worse code, because some jump targets are no longer reachable by short
272 * branches, but this is quite rare, so there are more advantages than
276 unsigned Changes = 0;
278 /* Walk over all entries */
280 while (I < CS_GetEntryCount (S)) {
286 CodeEntry* E = CS_GetEntry (S, I);
288 /* Check if it's a branch, if it has a jump label, if this jump
289 * label is not attached to the instruction itself, and if the
290 * target instruction is itself a branch.
292 if ((E->Info & OF_BRA) != 0 &&
293 (OldLabel = E->JumpTo) != 0 &&
294 (N = OldLabel->Owner) != E &&
295 (N->Info & OF_BRA) != 0) {
297 /* Check if we can use the final target label. This is the case,
298 * if the target branch is an absolut branch, or if it is a
299 * conditional branch checking the same condition as the first one.
301 if ((N->Info & OF_UBRA) != 0 ||
302 ((E->Info & OF_CBRA) != 0 &&
303 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
305 /* This is a jump cascade and we may jump to the final target.
306 * Insert a new instruction, then remove the old one
308 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
310 /* Insert it behind E */
311 CS_InsertEntry (S, X, I+1);
316 /* Remember, we had changes */
324 /* Check if both are conditional branches, and the condition of
325 * the second is the inverse of that of the first. In this case,
326 * the second branch will never be taken, and we may jump directly
327 * to the instruction behind this one.
329 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
331 CodeEntry* X; /* Instruction behind N */
332 CodeLabel* LX; /* Label attached to X */
334 /* Get the branch conditions of both branches */
335 bc_t BC1 = GetBranchCond (E->OPC);
336 bc_t BC2 = GetBranchCond (N->OPC);
338 /* Check the branch conditions */
339 if (BC1 != GetInverseCond (BC2)) {
340 /* Condition not met */
344 /* We may jump behind this conditional branch. Get the
345 * pointer to the next instruction
347 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
348 /* N is the last entry, bail out */
352 /* Get the label attached to X, create a new one if needed */
353 LX = CS_GenLabel (S, X);
355 /* Move the reference from E to the new label */
356 CS_MoveLabelRef (S, E, LX);
358 /* Remember, we had changes */
373 /* Return the number of changes made */
379 /*****************************************************************************/
380 /* Optimize jsr/rts */
381 /*****************************************************************************/
385 unsigned OptRTS (CodeSeg* S)
386 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
387 * replaced by a jump. Don't bother to delete the RTS if it does not have a
388 * label, the dead code elimination should take care of it.
391 unsigned Changes = 0;
394 /* Get the number of entries, bail out if we have less than 2 entries */
395 unsigned Count = CS_GetEntryCount (S);
400 /* Walk over all entries minus the last one */
402 while (I < Count-1) {
407 CodeEntry* E = CS_GetEntry (S, I);
409 /* Check if it's a subroutine call and if the following insn is RTS */
410 if (E->OPC == OP65_JSR &&
411 (N = CS_GetNextEntry (S, I)) != 0 &&
412 N->OPC == OP65_RTS) {
414 /* Change the jsr to a jmp and use the additional info for a jump */
416 CE_ReplaceOPC (E, OP65_JMP);
418 /* Remember, we had changes */
428 /* Return the number of changes made */
434 /*****************************************************************************/
435 /* Optimize jump targets */
436 /*****************************************************************************/
440 unsigned OptJumpTarget (CodeSeg* S)
441 /* If the instruction preceeding an unconditional branch is the same as the
442 * instruction preceeding the jump target, the jump target may be moved
443 * one entry back. This is a size optimization, since the instruction before
444 * the branch gets removed.
447 unsigned Changes = 0;
448 CodeEntry* E1; /* Entry 1 */
449 CodeEntry* E2; /* Entry 2 */
450 CodeEntry* T1; /* Jump target entry 1 */
451 CodeEntry* T2; /* Jump target entry 2 */
452 CodeLabel* TL1; /* Target label 1 */
453 unsigned TI; /* Target index */
456 /* Get the number of entries, bail out if we have not enough */
457 unsigned Count = CS_GetEntryCount (S);
462 /* Walk over the entries */
464 while (I < Count-1) {
467 E2 = CS_GetEntry (S, I+1);
469 /* Check if we have a jump or branch, and a matching label */
470 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
472 /* Get the target instruction for the label */
473 T2 = E2->JumpTo->Owner;
475 /* Get the entry preceeding this one (if possible) */
476 TI = CS_GetEntryIndex (S, T2);
478 /* There is no entry before this one */
481 T1 = CS_GetEntry (S, TI-1);
483 /* Get the entry preceeding the jump */
484 E1 = CS_GetEntry (S, I);
486 /* Check if both preceeding instructions are identical */
487 if (!CodeEntriesAreEqual (E1, T1)) {
488 /* Not equal, try next */
492 /* Get the label for the instruction preceeding the jump target.
493 * This routine will create a new label if the instruction does
494 * not already have one.
496 TL1 = CS_GenLabel (S, T1);
498 /* Change the jump target to point to this new label */
499 CS_MoveLabelRef (S, E2, TL1);
501 /* If the instruction preceeding the jump has labels attached,
502 * move references to this label to the new label.
504 if (CE_HasLabel (E1)) {
505 CS_MoveLabels (S, E1, T1);
508 /* Remove the entry preceeding the jump */
512 /* Remember, we had changes */
523 /* Return the number of changes made */
529 /*****************************************************************************/
530 /* Optimize conditional branches */
531 /*****************************************************************************/
535 unsigned OptCondBranches (CodeSeg* S)
536 /* Performs several optimization steps:
538 * - If an immidiate load of a register is followed by a conditional jump that
539 * is never taken because the load of the register sets the flags in such a
540 * manner, remove the conditional branch.
541 * - If the conditional branch is always taken because of the register load,
542 * replace it by a jmp.
543 * - If a conditional branch jumps around an unconditional branch, remove the
544 * conditional branch and make the jump a conditional branch with the
545 * inverse condition of the first one.
548 unsigned Changes = 0;
551 /* Get the number of entries, bail out if we have not enough */
552 unsigned Count = CS_GetEntryCount (S);
557 /* Walk over the entries */
559 while (I < Count-1) {
565 CodeEntry* E = CS_GetEntry (S, I);
567 /* Check if it's a register load */
568 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
569 E->AM == AM65_IMM && /* ..with immidiate addressing */
570 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
571 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
572 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
573 !CE_HasLabel (N)) { /* ..and does not have a label */
575 /* Get the branch condition */
576 bc_t BC = GetBranchCond (N->OPC);
578 /* Check the argument against the branch condition */
579 if ((BC == BC_EQ && E->Num != 0) ||
580 (BC == BC_NE && E->Num == 0) ||
581 (BC == BC_PL && (E->Num & 0x80) != 0) ||
582 (BC == BC_MI && (E->Num & 0x80) == 0)) {
584 /* Remove the conditional branch */
585 CS_DelEntry (S, I+1);
588 /* Remember, we had changes */
591 } else if ((BC == BC_EQ && E->Num == 0) ||
592 (BC == BC_NE && E->Num != 0) ||
593 (BC == BC_PL && (E->Num & 0x80) == 0) ||
594 (BC == BC_MI && (E->Num & 0x80) != 0)) {
596 /* The branch is always taken, replace it by a jump */
597 CE_ReplaceOPC (N, OP65_JMP);
599 /* Remember, we had changes */
605 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
606 (L = E->JumpTo) != 0 && /* ..referencing a local label */
607 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
608 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
609 !CE_HasLabel (N) && /* ..has no label attached */
610 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
612 /* Replace the jump by a conditional branch with the inverse branch
613 * condition than the branch around it.
615 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
617 /* Remove the conditional branch */
621 /* Remember, we had changes */
631 /* Return the number of changes made */
637 /*****************************************************************************/
638 /* Remove unused loads and stores */
639 /*****************************************************************************/
643 unsigned OptUnusedLoads (CodeSeg* S)
644 /* Remove loads of registers where the value loaded is not used later. */
646 unsigned Changes = 0;
648 /* Walk over the entries */
650 while (I < CS_GetEntryCount (S)) {
655 CodeEntry* E = CS_GetEntry (S, I);
657 /* Check if it's a register load or transfer insn */
658 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
659 (N = CS_GetNextEntry (S, I)) != 0 &&
660 (N->Info & OF_FBRA) == 0) {
662 /* Check which sort of load or transfer it is */
669 case OP65_TYA: R = REG_A; break;
673 case OP65_TAX: R = REG_X; break;
677 case OP65_TAY: R = REG_Y; break;
678 default: goto NextEntry; /* OOPS */
681 /* Get register usage and check if the register value is used later */
682 if ((GetRegInfo (S, I+1, R) & R) == 0) {
684 /* Register value is not used, remove the load */
687 /* Remember, we had changes */
699 /* Return the number of changes made */
705 unsigned OptUnusedStores (CodeSeg* S)
706 /* Remove stores into zero page registers that aren't used later */
708 unsigned Changes = 0;
710 /* Walk over the entries */
712 while (I < CS_GetEntryCount (S)) {
715 CodeEntry* E = CS_GetEntry (S, I);
717 /* Check if it's a register load or transfer insn */
718 if ((E->Info & OF_STORE) != 0 &&
720 (E->Chg & REG_ZP) != 0) {
722 /* Check for the zero page location. We know that there cannot be
723 * more than one zero page location involved in the store.
725 unsigned R = E->Chg & REG_ZP;
727 /* Get register usage and check if the register value is used later */
728 if ((GetRegInfo (S, I+1, R) & R) == 0) {
730 /* Register value is not used, remove the load */
733 /* Remember, we had changes */
744 /* Return the number of changes made */
750 unsigned OptDupLoads (CodeSeg* S)
751 /* Remove loads of registers where the value loaded is already in the register. */
753 unsigned Changes = 0;
756 /* Generate register info for this step */
759 /* Walk over the entries */
761 while (I < CS_GetEntryCount (S)) {
766 CodeEntry* E = CS_GetEntry (S, I);
768 /* Assume we won't delete the entry */
771 /* Get a pointer to the input registers of the insn */
772 const RegContents* In = &E->RI->In;
774 /* Handle the different instructions */
778 if (In->RegA >= 0 && /* Value of A is known */
779 CE_KnownImm (E) && /* Value to be loaded is known */
780 In->RegA == (long) E->Num && /* Both are equal */
781 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
782 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
788 if (In->RegX >= 0 && /* Value of X is known */
789 CE_KnownImm (E) && /* Value to be loaded is known */
790 In->RegX == (long) E->Num && /* Both are equal */
791 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
792 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
798 if (In->RegY >= 0 && /* Value of Y is known */
799 CE_KnownImm (E) && /* Value to be loaded is known */
800 In->RegY == (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 we store into a known zero page location, and this
809 * location does already contain the value to be stored,
812 if (In->RegA >= 0 && /* Value of A is known */
813 E->AM == AM65_ZP && /* Store into zp */
814 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
815 In->RegA == In->SRegLo) || /* Value identical */
816 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
817 In->RegA == In->SRegHi))) { /* Value identical */
823 /* If we store into a known zero page location, and this
824 * location does already contain the value to be stored,
827 if (In->RegX >= 0 && /* Value of A is known */
828 E->AM == AM65_ZP && /* Store into zp */
829 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
830 In->RegX == In->SRegLo) || /* Value identical */
831 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
832 In->RegX == In->SRegHi))) { /* Value identical */
835 /* If the value in the X register is known and the same as
836 * that in the A register, replace the store by a STA. The
837 * optimizer will then remove the load instruction for X
838 * later. STX does support the zeropage,y addressing mode,
839 * so be sure to check for that.
841 } else if (In->RegX >= 0 &&
842 In->RegX == In->RegA &&
843 E->AM != AM65_ABSY &&
845 /* Use the A register instead */
846 CE_ReplaceOPC (E, OP65_STA);
851 /* If we store into a known zero page location, and this
852 * location does already contain the value to be stored,
855 if (In->RegX >= 0 && /* Value of A is known */
856 E->AM == AM65_ZP && /* Store into zp */
857 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
858 In->RegX == In->SRegLo) || /* Value identical */
859 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
860 In->RegX == In->SRegHi))) { /* Value identical */
862 /* If the value in the Y register is known and the same as
863 * that in the A register, replace the store by a STA. The
864 * optimizer will then remove the load instruction for Y
865 * later. If replacement by A is not possible try a
866 * replacement by X, but check for invalid addressing modes
869 } else if (In->RegY >= 0) {
870 if (In->RegY == In->RegA) {
871 CE_ReplaceOPC (E, OP65_STA);
872 } else if (In->RegY == In->RegX &&
873 E->AM != AM65_ABSX &&
875 CE_ReplaceOPC (E, OP65_STX);
882 In->RegA == In->RegX &&
883 (N = CS_GetNextEntry (S, I)) != 0 &&
884 (N->Info & OF_FBRA) == 0) {
885 /* Value is identical and not followed by a branch */
892 In->RegA == In->RegY &&
893 (N = CS_GetNextEntry (S, I)) != 0 &&
894 (N->Info & OF_FBRA) == 0) {
895 /* Value is identical and not followed by a branch */
902 In->RegX == In->RegA &&
903 (N = CS_GetNextEntry (S, I)) != 0 &&
904 (N->Info & OF_FBRA) == 0) {
905 /* Value is identical and not followed by a branch */
912 In->RegY == In->RegA &&
913 (N = CS_GetNextEntry (S, I)) != 0 &&
914 (N->Info & OF_FBRA) == 0) {
915 /* Value is identical and not followed by a branch */
925 /* Delete the entry if requested */
928 /* Register value is not used, remove the load */
931 /* Remember, we had changes */
943 /* Free register info */
946 /* Return the number of changes made */
952 unsigned OptStoreLoad (CodeSeg* S)
953 /* Remove a store followed by a load from the same location. */
955 unsigned Changes = 0;
957 /* Walk over the entries */
959 while (I < CS_GetEntryCount (S)) {
965 CodeEntry* E = CS_GetEntry (S, I);
967 /* Check if it is a store instruction followed by a load from the
968 * same address which is itself not followed by a conditional branch.
970 if ((E->Info & OF_STORE) != 0 &&
971 (N = CS_GetNextEntry (S, I)) != 0 &&
973 (N->Info & OF_LOAD) != 0 &&
975 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
976 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
977 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
978 strcmp (E->Arg, N->Arg) == 0 &&
979 (X = CS_GetNextEntry (S, I+1)) != 0 &&
980 (X->Info & OF_FBRA) == 0) {
982 /* Register value is not used, remove the load */
983 CS_DelEntry (S, I+1);
985 /* Remember, we had changes */
995 /* Return the number of changes made */
1001 unsigned OptTransfers (CodeSeg* S)
1002 /* Remove transfers from one register to another and back */
1004 unsigned Changes = 0;
1006 /* Walk over the entries */
1008 while (I < CS_GetEntryCount (S)) {
1014 /* Get next entry */
1015 CodeEntry* E = CS_GetEntry (S, I);
1017 /* Check if it is a store instruction followed by a load from the
1018 * same address which is itself not followed by a conditional branch.
1020 if ((E->Info & OF_XFR) != 0 &&
1021 (N = CS_GetNextEntry (S, I)) != 0 &&
1023 (N->Info & OF_XFR) != 0) {
1025 /* Check if it's a transfer and back */
1026 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1027 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1028 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1029 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
1031 /* If the next insn is a conditional branch, check if the insn
1032 * preceeding the first xfr will set the flags right, otherwise we
1033 * may not remove the sequence.
1035 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1038 if ((X->Info & OF_FBRA) != 0) {
1040 /* No preceeding entry */
1043 P = CS_GetEntry (S, I-1);
1044 if ((P->Info & OF_SETF) == 0) {
1045 /* Does not set the flags */
1050 /* Remove both transfers */
1051 CS_DelEntry (S, I+1);
1054 /* Remember, we had changes */
1065 /* Return the number of changes made */
1071 /*****************************************************************************/
1072 /* Optimize branch types */
1073 /*****************************************************************************/
1077 unsigned OptBranchDist (CodeSeg* S)
1078 /* Change branches for the distance needed. */
1080 unsigned Changes = 0;
1083 /* Get the number of entries, bail out if we have not enough */
1084 unsigned Count = CS_GetEntryCount (S);
1086 /* Walk over the entries */
1090 /* Get next entry */
1091 CodeEntry* E = CS_GetEntry (S, I);
1093 /* Check if it's a conditional branch to a local label. */
1094 if ((E->Info & OF_CBRA) != 0) {
1096 /* Is this a branch to a local symbol? */
1097 if (E->JumpTo != 0) {
1099 /* Get the index of the branch target */
1100 unsigned TI = CS_GetEntryIndex (S, E->JumpTo->Owner);
1102 /* Determine the branch distance */
1105 /* Forward branch */
1108 CodeEntry* N = CS_GetEntry (S, J++);
1109 Distance += N->Size;
1112 /* Backward branch */
1115 CodeEntry* N = CS_GetEntry (S, J++);
1116 Distance += N->Size;
1120 /* Make the branch short/long according to distance */
1121 if ((E->Info & OF_LBRA) == 0 && Distance > 120) {
1122 /* Short branch but long distance */
1123 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1125 } else if ((E->Info & OF_LBRA) != 0 && Distance < 120) {
1126 /* Long branch but short distance */
1127 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1131 } else if ((E->Info & OF_LBRA) == 0) {
1133 /* Short branch to external symbol - make it long */
1134 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1145 /* Return the number of changes made */