Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1 | //===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a pass that splits the constant pool up into 'islands' |
| 11 | // which are scattered through-out the function. This is required due to the |
| 12 | // limited pc-relative displacements that ARM has. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "arm-cp-islands" |
| 17 | #include "ARM.h" |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 18 | #include "ARMAddressingModes.h" |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 19 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 20 | #include "ARMInstrInfo.h" |
| 21 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 38 | STATISTIC(NumCPEs, "Number of constpool entries"); |
| 39 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 40 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 41 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
| 42 | STATISTIC(NumTBs, "Number of table branches generated"); |
| 43 | STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk"); |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 44 | STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk"); |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 45 | STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed"); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 46 | STATISTIC(NumJTMoved, "Number of jump table destination blocks moved"); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 47 | STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted"); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | static cl::opt<bool> |
Jim Grosbach | f04777b | 2009-11-17 21:24:11 +0000 | [diff] [blame] | 51 | AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true), |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 52 | cl::desc("Adjust basic block layout to better use TB[BH]")); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 55 | /// ARMConstantIslands - Due to limited PC-relative displacements, ARM |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 56 | /// requires constant pool entries to be scattered among the instructions |
| 57 | /// inside a function. To do this, it completely ignores the normal LLVM |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 58 | /// constant pool; instead, it places constants wherever it feels like with |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 59 | /// special instructions. |
| 60 | /// |
| 61 | /// The terminology used in this pass includes: |
| 62 | /// Islands - Clumps of constants placed in the function. |
| 63 | /// Water - Potential places where an island could be formed. |
| 64 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 65 | /// tracks a list of users. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 66 | class ARMConstantIslands : public MachineFunctionPass { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 67 | /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 68 | /// by MBB Number. The two-byte pads required for Thumb alignment are |
| 69 | /// counted as part of the following block (i.e., the offset and size for |
| 70 | /// a padded block will both be ==2 mod 4). |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 71 | std::vector<unsigned> BBSizes; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 72 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 73 | /// BBOffsets - the offset of each MBB in bytes, starting from 0. |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 74 | /// The two-byte pads required for Thumb alignment are counted as part of |
| 75 | /// the following block. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 76 | std::vector<unsigned> BBOffsets; |
| 77 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 78 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 79 | /// (i.e. blocks that don't fall through to the following block, due |
| 80 | /// to a return, unreachable, or unconditional branch). |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 81 | std::vector<MachineBasicBlock*> WaterList; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 82 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 83 | /// NewWaterList - The subset of WaterList that was created since the |
| 84 | /// previous iteration by inserting unconditional branches. |
| 85 | SmallSet<MachineBasicBlock*, 4> NewWaterList; |
| 86 | |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 87 | typedef std::vector<MachineBasicBlock*>::iterator water_iterator; |
| 88 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 89 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 90 | /// pointer, the constant pool being referenced, and the max displacement |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 91 | /// allowed from the instruction to the CP. The HighWaterMark records the |
| 92 | /// highest basic block where a new CPEntry can be placed. To ensure this |
| 93 | /// pass terminates, the CP entries are initially placed at the end of the |
| 94 | /// function and then move monotonically to lower addresses. The |
| 95 | /// exception to this rule is when the current CP entry for a particular |
| 96 | /// CPUser is out of range, but there is another CP entry for the same |
| 97 | /// constant value in range. We want to use the existing in-range CP |
| 98 | /// entry, but if it later moves out of range, the search for new water |
| 99 | /// should resume where it left off. The HighWaterMark is used to record |
| 100 | /// that point. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 101 | struct CPUser { |
| 102 | MachineInstr *MI; |
| 103 | MachineInstr *CPEMI; |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 104 | MachineBasicBlock *HighWaterMark; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 105 | unsigned MaxDisp; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 106 | bool NegOk; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 107 | bool IsSoImm; |
| 108 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, |
| 109 | bool neg, bool soimm) |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 110 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) { |
| 111 | HighWaterMark = CPEMI->getParent(); |
| 112 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 113 | }; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 114 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 115 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 116 | /// constant pools and their max displacement. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 117 | std::vector<CPUser> CPUsers; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 118 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 119 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 120 | /// pointer, the constpool index, and the number of CPUser's which |
| 121 | /// reference this entry. |
| 122 | struct CPEntry { |
| 123 | MachineInstr *CPEMI; |
| 124 | unsigned CPI; |
| 125 | unsigned RefCount; |
| 126 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 127 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 128 | }; |
| 129 | |
| 130 | /// CPEntries - Keep track of all of the constant pool entry machine |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 131 | /// instructions. For each original constpool index (i.e. those that |
| 132 | /// existed upon entry to this pass), it keeps a vector of entries. |
| 133 | /// Original elements are cloned as we go along; the clones are |
| 134 | /// put in the vector of the original element, but have distinct CPIs. |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 135 | std::vector<std::vector<CPEntry> > CPEntries; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 136 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 137 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 138 | /// pointer, conditional or unconditional, the max displacement, |
| 139 | /// and (if isCond is true) the corresponding unconditional branch |
| 140 | /// opcode. |
| 141 | struct ImmBranch { |
| 142 | MachineInstr *MI; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 143 | unsigned MaxDisp : 31; |
| 144 | bool isCond : 1; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 145 | int UncondBr; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 146 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 147 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Evan Cheng | 2706f97 | 2007-05-16 05:14:06 +0000 | [diff] [blame] | 150 | /// ImmBranches - Keep track of all the immediate branch instructions. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 151 | /// |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 152 | std::vector<ImmBranch> ImmBranches; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 153 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 154 | /// PushPopMIs - Keep track of all the Thumb push / pop instructions. |
| 155 | /// |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 156 | SmallVector<MachineInstr*, 4> PushPopMIs; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 157 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 158 | /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions. |
| 159 | SmallVector<MachineInstr*, 4> T2JumpTables; |
| 160 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 161 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 162 | /// the branch fix up pass. |
| 163 | bool HasFarJump; |
| 164 | |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 165 | /// HasInlineAsm - True if the function contains inline assembly. |
| 166 | bool HasInlineAsm; |
| 167 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 168 | const TargetInstrInfo *TII; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 169 | const ARMSubtarget *STI; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 170 | ARMFunctionInfo *AFI; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 171 | bool isThumb; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 172 | bool isThumb1; |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 173 | bool isThumb2; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 174 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 175 | static char ID; |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 176 | ARMConstantIslands() : MachineFunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 177 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 178 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 179 | |
| 180 | virtual const char *getPassName() const { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 181 | return "ARM constant island placement and branch shortening pass"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 182 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 183 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 184 | private: |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 185 | void DoInitialPlacement(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 186 | std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 187 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 188 | void JumpTableFunctionScan(MachineFunction &MF); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 189 | void InitialFunctionScan(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 190 | const std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 191 | MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 192 | void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 193 | void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 194 | bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 195 | int LookForExistingCPEntry(CPUser& U, unsigned UserOffset); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 196 | bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 197 | void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset, |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 198 | MachineBasicBlock *&NewMBB); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 199 | bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 200 | void RemoveDeadCPEMI(MachineInstr *CPEMI); |
| 201 | bool RemoveUnusedCPEntries(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 202 | bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 203 | MachineInstr *CPEMI, unsigned Disp, bool NegOk, |
| 204 | bool DoDump = false); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 205 | bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water, |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 206 | CPUser &U); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 207 | bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 208 | unsigned Disp, bool NegativeOK, bool IsSoImm = false); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 209 | bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 210 | bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br); |
| 211 | bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br); |
| 212 | bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 213 | bool UndoLRSpillRestore(); |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 214 | bool OptimizeThumb2Instructions(MachineFunction &MF); |
| 215 | bool OptimizeThumb2Branches(MachineFunction &MF); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 216 | bool ReorderThumb2JumpTables(MachineFunction &MF); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 217 | bool OptimizeThumb2JumpTables(MachineFunction &MF); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 218 | MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB, |
| 219 | MachineBasicBlock *JTBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 220 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 221 | unsigned GetOffsetOf(MachineInstr *MI) const; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 222 | void dumpBBs(); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 223 | void verify(MachineFunction &MF); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 224 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 225 | char ARMConstantIslands::ID = 0; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 228 | /// verify - check BBOffsets, BBSizes, alignment of islands |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 229 | void ARMConstantIslands::verify(MachineFunction &MF) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 230 | assert(BBOffsets.size() == BBSizes.size()); |
| 231 | for (unsigned i = 1, e = BBOffsets.size(); i != e; ++i) |
| 232 | assert(BBOffsets[i-1]+BBSizes[i-1] == BBOffsets[i]); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 233 | if (!isThumb) |
| 234 | return; |
| 235 | #ifndef NDEBUG |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 236 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 237 | MBBI != E; ++MBBI) { |
| 238 | MachineBasicBlock *MBB = MBBI; |
| 239 | if (!MBB->empty() && |
| 240 | MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) { |
| 241 | unsigned MBBId = MBB->getNumber(); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 242 | assert(HasInlineAsm || |
| 243 | (BBOffsets[MBBId]%4 == 0 && BBSizes[MBBId]%4 == 0) || |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 244 | (BBOffsets[MBBId]%4 != 0 && BBSizes[MBBId]%4 != 0)); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 247 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 248 | CPUser &U = CPUsers[i]; |
| 249 | unsigned UserOffset = GetOffsetOf(U.MI) + (isThumb ? 4 : 8); |
Jim Grosbach | a956256 | 2009-11-20 19:37:38 +0000 | [diff] [blame] | 250 | unsigned CPEOffset = GetOffsetOf(U.CPEMI); |
| 251 | unsigned Disp = UserOffset < CPEOffset ? CPEOffset - UserOffset : |
| 252 | UserOffset - CPEOffset; |
| 253 | assert(Disp <= U.MaxDisp || "Constant pool entry out of range!"); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 254 | } |
Jim Grosbach | a956256 | 2009-11-20 19:37:38 +0000 | [diff] [blame] | 255 | #endif |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// print block size and offset information - debugging |
| 259 | void ARMConstantIslands::dumpBBs() { |
| 260 | for (unsigned J = 0, E = BBOffsets.size(); J !=E; ++J) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 261 | DEBUG(errs() << "block " << J << " offset " << BBOffsets[J] |
| 262 | << " size " << BBSizes[J] << "\n"); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 266 | /// createARMConstantIslandPass - returns an instance of the constpool |
| 267 | /// island pass. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 268 | FunctionPass *llvm::createARMConstantIslandPass() { |
| 269 | return new ARMConstantIslands(); |
| 270 | } |
| 271 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 272 | bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) { |
| 273 | MachineConstantPool &MCP = *MF.getConstantPool(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 274 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 275 | TII = MF.getTarget().getInstrInfo(); |
| 276 | AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 277 | STI = &MF.getTarget().getSubtarget<ARMSubtarget>(); |
| 278 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 279 | isThumb = AFI->isThumbFunction(); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 280 | isThumb1 = AFI->isThumb1OnlyFunction(); |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 281 | isThumb2 = AFI->isThumb2Function(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 282 | |
| 283 | HasFarJump = false; |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 284 | HasInlineAsm = false; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 285 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 286 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 287 | // the numbers agree with the position of the block in the function. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 288 | MF.RenumberBlocks(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 289 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 290 | // Try to reorder and otherwise adjust the block layout to make good use |
| 291 | // of the TB[BH] instructions. |
| 292 | bool MadeChange = false; |
| 293 | if (isThumb2 && AdjustJumpTableBlocks) { |
| 294 | JumpTableFunctionScan(MF); |
| 295 | MadeChange |= ReorderThumb2JumpTables(MF); |
| 296 | // Data is out of date, so clear it. It'll be re-computed later. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 297 | T2JumpTables.clear(); |
| 298 | // Blocks may have shifted around. Keep the numbering up to date. |
| 299 | MF.RenumberBlocks(); |
| 300 | } |
| 301 | |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 302 | // Thumb1 functions containing constant pools get 4-byte alignment. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 303 | // This is so we can keep exact track of where the alignment padding goes. |
| 304 | |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 305 | // Set default. Thumb1 function is 2-byte aligned, ARM and Thumb2 are 4-byte |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 306 | // aligned. |
| 307 | AFI->setAlign(isThumb1 ? 1U : 2U); |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 308 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 309 | // Perform the initial placement of the constant pool entries. To start with, |
| 310 | // we put them all at the end of the function. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 311 | std::vector<MachineInstr*> CPEMIs; |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 312 | if (!MCP.isEmpty()) { |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 313 | DoInitialPlacement(MF, CPEMIs); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 314 | if (isThumb1) |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 315 | AFI->setAlign(2U); |
| 316 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 317 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 318 | /// The next UID to take is the first unused one. |
Evan Cheng | f1bbb95 | 2008-11-08 00:51:41 +0000 | [diff] [blame] | 319 | AFI->initConstPoolEntryUId(CPEMIs.size()); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 320 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 321 | // Do the initial scan of the function, building up information about the |
| 322 | // sizes of each block, the location of all the water, and finding all of the |
| 323 | // constant pool users. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 324 | InitialFunctionScan(MF, CPEMIs); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 325 | CPEMIs.clear(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 326 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 327 | /// Remove dead constant pool entries. |
| 328 | RemoveUnusedCPEntries(); |
| 329 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 330 | // Iteratively place constant pool entries and fix up branches until there |
| 331 | // is no change. |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 332 | unsigned NoCPIters = 0, NoBRIters = 0; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 333 | while (true) { |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 334 | bool CPChange = false; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 335 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 336 | CPChange |= HandleConstantPoolUser(MF, i); |
| 337 | if (CPChange && ++NoCPIters > 30) |
| 338 | llvm_unreachable("Constant Island pass failed to converge!"); |
Evan Cheng | 8202010 | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 339 | DEBUG(dumpBBs()); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 340 | |
| 341 | // Clear NewWaterList now. If we split a block for branches, it should |
| 342 | // appear as "new water" for the next iteration of constant pool placement. |
| 343 | NewWaterList.clear(); |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 344 | |
| 345 | bool BRChange = false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 346 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 347 | BRChange |= FixUpImmediateBr(MF, ImmBranches[i]); |
| 348 | if (BRChange && ++NoBRIters > 30) |
| 349 | llvm_unreachable("Branch Fix Up pass failed to converge!"); |
Evan Cheng | 8202010 | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 350 | DEBUG(dumpBBs()); |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 351 | |
| 352 | if (!CPChange && !BRChange) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 353 | break; |
| 354 | MadeChange = true; |
| 355 | } |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 356 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 357 | // Shrink 32-bit Thumb2 branch, load, and store instructions. |
| 358 | if (isThumb2) |
| 359 | MadeChange |= OptimizeThumb2Instructions(MF); |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 360 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 361 | // After a while, this might be made debug-only, but it is not expensive. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 362 | verify(MF); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 363 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 364 | // If LR has been forced spilled and no far jumps (i.e. BL) has been issued. |
| 365 | // Undo the spill / restore of LR if possible. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 366 | if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump()) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 367 | MadeChange |= UndoLRSpillRestore(); |
| 368 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 369 | BBSizes.clear(); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 370 | BBOffsets.clear(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 371 | WaterList.clear(); |
| 372 | CPUsers.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 373 | CPEntries.clear(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 374 | ImmBranches.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 375 | PushPopMIs.clear(); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 376 | T2JumpTables.clear(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 377 | |
| 378 | return MadeChange; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /// DoInitialPlacement - Perform the initial placement of the constant pool |
| 382 | /// entries. To start with, we put them all at the end of the function. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 383 | void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF, |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 384 | std::vector<MachineInstr*> &CPEMIs) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 385 | // Create the basic block to hold the CPE's. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 386 | MachineBasicBlock *BB = MF.CreateMachineBasicBlock(); |
| 387 | MF.push_back(BB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 388 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 389 | // Add all of the constants from the constant pool to the end block, use an |
| 390 | // identity mapping of CPI's to CPE's. |
| 391 | const std::vector<MachineConstantPoolEntry> &CPs = |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 392 | MF.getConstantPool()->getConstants(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 393 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 394 | const TargetData &TD = *MF.getTarget().getTargetData(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 395 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 396 | unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 397 | // Verify that all constant pool entries are a multiple of 4 bytes. If not, |
| 398 | // we would have to pad them out or something so that instructions stay |
| 399 | // aligned. |
| 400 | assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 401 | MachineInstr *CPEMI = |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 402 | BuildMI(BB, DebugLoc::getUnknownLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 403 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| 404 | CPEMIs.push_back(CPEMI); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 405 | |
| 406 | // Add a new CPEntry, but no corresponding CPUser yet. |
| 407 | std::vector<CPEntry> CPEs; |
| 408 | CPEs.push_back(CPEntry(CPEMI, i)); |
| 409 | CPEntries.push_back(CPEs); |
| 410 | NumCPEs++; |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 411 | DEBUG(errs() << "Moved CPI#" << i << " to end of function as #" << i |
| 412 | << "\n"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 416 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 417 | /// into the block immediately after it. |
| 418 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 419 | // Get the next machine basic block in the function. |
| 420 | MachineFunction::iterator MBBI = MBB; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 421 | if (llvm::next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 422 | return false; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 424 | MachineBasicBlock *NextBB = llvm::next(MBBI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 425 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 426 | E = MBB->succ_end(); I != E; ++I) |
| 427 | if (*I == NextBB) |
| 428 | return true; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 429 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 433 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 434 | /// look up the corresponding CPEntry. |
| 435 | ARMConstantIslands::CPEntry |
| 436 | *ARMConstantIslands::findConstPoolEntry(unsigned CPI, |
| 437 | const MachineInstr *CPEMI) { |
| 438 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 439 | // Number of entries per constpool index should be small, just do a |
| 440 | // linear search. |
| 441 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 442 | if (CPEs[i].CPEMI == CPEMI) |
| 443 | return &CPEs[i]; |
| 444 | } |
| 445 | return NULL; |
| 446 | } |
| 447 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 448 | /// JumpTableFunctionScan - Do a scan of the function, building up |
| 449 | /// information about the sizes of each block and the locations of all |
| 450 | /// the jump tables. |
| 451 | void ARMConstantIslands::JumpTableFunctionScan(MachineFunction &MF) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 452 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 453 | MBBI != E; ++MBBI) { |
| 454 | MachineBasicBlock &MBB = *MBBI; |
| 455 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 456 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 457 | I != E; ++I) |
| 458 | if (I->getDesc().isBranch() && I->getOpcode() == ARM::t2BR_JT) |
| 459 | T2JumpTables.push_back(I); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 463 | /// InitialFunctionScan - Do the initial scan of the function, building up |
| 464 | /// information about the sizes of each block, the location of all the water, |
| 465 | /// and finding all of the constant pool users. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 466 | void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 467 | const std::vector<MachineInstr*> &CPEMIs) { |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 468 | // First thing, see if the function has any inline assembly in it. If so, |
| 469 | // we have to be conservative about alignment assumptions, as we don't |
| 470 | // know for sure the size of any instructions in the inline assembly. |
| 471 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 472 | MBBI != E; ++MBBI) { |
| 473 | MachineBasicBlock &MBB = *MBBI; |
| 474 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 475 | I != E; ++I) |
| 476 | if (I->getOpcode() == ARM::INLINEASM) |
| 477 | HasInlineAsm = true; |
| 478 | } |
| 479 | |
| 480 | // Now go back through the instructions and build up our data structures |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 481 | unsigned Offset = 0; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 482 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 483 | MBBI != E; ++MBBI) { |
| 484 | MachineBasicBlock &MBB = *MBBI; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 485 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 486 | // If this block doesn't fall through into the next MBB, then this is |
| 487 | // 'water' that a constant pool island could be placed. |
| 488 | if (!BBHasFallthrough(&MBB)) |
| 489 | WaterList.push_back(&MBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 490 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 491 | unsigned MBBSize = 0; |
| 492 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 493 | I != E; ++I) { |
| 494 | // Add instruction size to MBBSize. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 495 | MBBSize += TII->GetInstSizeInBytes(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 496 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 497 | int Opc = I->getOpcode(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 498 | if (I->getDesc().isBranch()) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 499 | bool isCond = false; |
| 500 | unsigned Bits = 0; |
| 501 | unsigned Scale = 1; |
| 502 | int UOpc = Opc; |
| 503 | switch (Opc) { |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 504 | default: |
| 505 | continue; // Ignore other JT branches |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 506 | case ARM::tBR_JTr: |
Evan Cheng | 66ac531 | 2009-07-25 00:33:29 +0000 | [diff] [blame] | 507 | // A Thumb1 table jump may involve padding; for the offsets to |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 508 | // be right, functions containing these must be 4-byte aligned. |
| 509 | AFI->setAlign(2U); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 510 | if ((Offset+MBBSize)%4 != 0 || HasInlineAsm) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 511 | // FIXME: Add a pseudo ALIGN instruction instead. |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 512 | MBBSize += 2; // padding |
| 513 | continue; // Does not get an entry in ImmBranches |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 514 | case ARM::t2BR_JT: |
| 515 | T2JumpTables.push_back(I); |
| 516 | continue; // Does not get an entry in ImmBranches |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 517 | case ARM::Bcc: |
| 518 | isCond = true; |
| 519 | UOpc = ARM::B; |
| 520 | // Fallthrough |
| 521 | case ARM::B: |
| 522 | Bits = 24; |
| 523 | Scale = 4; |
| 524 | break; |
| 525 | case ARM::tBcc: |
| 526 | isCond = true; |
| 527 | UOpc = ARM::tB; |
| 528 | Bits = 8; |
| 529 | Scale = 2; |
| 530 | break; |
| 531 | case ARM::tB: |
| 532 | Bits = 11; |
| 533 | Scale = 2; |
| 534 | break; |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 535 | case ARM::t2Bcc: |
| 536 | isCond = true; |
| 537 | UOpc = ARM::t2B; |
| 538 | Bits = 20; |
| 539 | Scale = 2; |
| 540 | break; |
| 541 | case ARM::t2B: |
| 542 | Bits = 24; |
| 543 | Scale = 2; |
| 544 | break; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 545 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 546 | |
| 547 | // Record this immediate branch. |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 548 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 549 | ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 552 | if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) |
| 553 | PushPopMIs.push_back(I); |
| 554 | |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 555 | if (Opc == ARM::CONSTPOOL_ENTRY) |
| 556 | continue; |
| 557 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 558 | // Scan the instructions for constant pool operands. |
| 559 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 560 | if (I->getOperand(op).isCPI()) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 561 | // We found one. The addressing mode tells us the max displacement |
| 562 | // from the PC that this instruction permits. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 563 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 564 | // Basic size info comes from the TSFlags field. |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 565 | unsigned Bits = 0; |
| 566 | unsigned Scale = 1; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 567 | bool NegOk = false; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 568 | bool IsSoImm = false; |
| 569 | |
| 570 | switch (Opc) { |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 571 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 572 | llvm_unreachable("Unknown addressing mode for CP reference!"); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | // Taking the address of a CP entry. |
| 576 | case ARM::LEApcrel: |
| 577 | // This takes a SoImm, which is 8 bit immediate rotated. We'll |
| 578 | // pretend the maximum offset is 255 * 4. Since each instruction |
Jim Grosbach | dec6de9 | 2009-11-19 18:23:19 +0000 | [diff] [blame] | 579 | // 4 byte wide, this is always correct. We'll check for other |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 580 | // displacements that fits in a SoImm as well. |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 581 | Bits = 8; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 582 | Scale = 4; |
| 583 | NegOk = true; |
| 584 | IsSoImm = true; |
| 585 | break; |
| 586 | case ARM::t2LEApcrel: |
| 587 | Bits = 12; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 588 | NegOk = true; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 589 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 590 | case ARM::tLEApcrel: |
| 591 | Bits = 8; |
| 592 | Scale = 4; |
| 593 | break; |
| 594 | |
| 595 | case ARM::LDR: |
| 596 | case ARM::LDRcp: |
| 597 | case ARM::t2LDRpci: |
Evan Cheng | 556f33c | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 598 | Bits = 12; // +-offset_12 |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 599 | NegOk = true; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 600 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 601 | |
| 602 | case ARM::tLDRpci: |
| 603 | case ARM::tLDRcp: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 604 | Bits = 8; |
| 605 | Scale = 4; // +(offset_8*4) |
Evan Cheng | 012f2d9 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 606 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 607 | |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 608 | case ARM::VLDRD: |
| 609 | case ARM::VLDRS: |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 610 | Bits = 8; |
| 611 | Scale = 4; // +-(offset_8*4) |
| 612 | NegOk = true; |
Evan Cheng | 055b031 | 2009-06-29 07:51:04 +0000 | [diff] [blame] | 613 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 614 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 615 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 616 | // Remember that this is a user of a CP entry. |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 617 | unsigned CPI = I->getOperand(op).getIndex(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 618 | MachineInstr *CPEMI = CPEMIs[CPI]; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 619 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 620 | CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm)); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 621 | |
| 622 | // Increment corresponding CPEntry reference count. |
| 623 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 624 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 625 | CPE->RefCount++; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 626 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 627 | // Instructions can only use one CP entry, don't bother scanning the |
| 628 | // rest of the operands. |
| 629 | break; |
| 630 | } |
| 631 | } |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 632 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 633 | // In thumb mode, if this block is a constpool island, we may need padding |
| 634 | // so it's aligned on 4 byte boundary. |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 635 | if (isThumb && |
Evan Cheng | 05cc424 | 2007-02-02 19:09:19 +0000 | [diff] [blame] | 636 | !MBB.empty() && |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 637 | MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY && |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 638 | ((Offset%4) != 0 || HasInlineAsm)) |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 639 | MBBSize += 2; |
| 640 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 641 | BBSizes.push_back(MBBSize); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 642 | BBOffsets.push_back(Offset); |
| 643 | Offset += MBBSize; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 647 | /// GetOffsetOf - Return the current offset of the specified machine instruction |
| 648 | /// from the start of the function. This offset changes as stuff is moved |
| 649 | /// around inside the function. |
| 650 | unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const { |
| 651 | MachineBasicBlock *MBB = MI->getParent(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 652 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 653 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 654 | // before this instruction's block, and the offset from the start of the block |
| 655 | // it is in. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 656 | unsigned Offset = BBOffsets[MBB->getNumber()]; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 657 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 658 | // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has |
| 659 | // alignment padding, and compensate if so. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 660 | if (isThumb && |
| 661 | MI->getOpcode() == ARM::CONSTPOOL_ENTRY && |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 662 | (Offset%4 != 0 || HasInlineAsm)) |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 663 | Offset += 2; |
| 664 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 665 | // Sum instructions before MI in MBB. |
| 666 | for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) { |
| 667 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| 668 | if (&*I == MI) return Offset; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 669 | Offset += TII->GetInstSizeInBytes(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 674 | /// ID. |
| 675 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 676 | const MachineBasicBlock *RHS) { |
| 677 | return LHS->getNumber() < RHS->getNumber(); |
| 678 | } |
| 679 | |
| 680 | /// UpdateForInsertedWaterBlock - When a block is newly inserted into the |
| 681 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 682 | /// and update the arrays that parallel this numbering. |
| 683 | void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) { |
| 684 | // Renumber the MBB's to keep them consequtive. |
| 685 | NewBB->getParent()->RenumberBlocks(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 686 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 687 | // Insert a size into BBSizes to align it properly with the (newly |
| 688 | // renumbered) block numbers. |
| 689 | BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 690 | |
| 691 | // Likewise for BBOffsets. |
| 692 | BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 693 | |
| 694 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 695 | // available water after it. |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 696 | water_iterator IP = |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 697 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 698 | CompareMBBNumbers); |
| 699 | WaterList.insert(IP, NewBB); |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /// Split the basic block containing MI into two blocks, which are joined by |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 704 | /// an unconditional branch. Update data structures and renumber blocks to |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 705 | /// account for this change and returns the newly created block. |
| 706 | MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 707 | MachineBasicBlock *OrigBB = MI->getParent(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 708 | MachineFunction &MF = *OrigBB->getParent(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 709 | |
| 710 | // Create a new MBB for the code after the OrigBB. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 711 | MachineBasicBlock *NewBB = |
| 712 | MF.CreateMachineBasicBlock(OrigBB->getBasicBlock()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 713 | MachineFunction::iterator MBBI = OrigBB; ++MBBI; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 714 | MF.insert(MBBI, NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 715 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 716 | // Splice the instructions starting with MI over to NewBB. |
| 717 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 718 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 719 | // Add an unconditional branch from OrigBB to NewBB. |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 720 | // Note the new unconditional branch is not being recorded. |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 721 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 722 | // correspond to anything in the source. |
Evan Cheng | 58541fd | 2009-07-07 01:16:41 +0000 | [diff] [blame] | 723 | unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B; |
| 724 | BuildMI(OrigBB, DebugLoc::getUnknownLoc(), TII->get(Opc)).addMBB(NewBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 725 | NumSplit++; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 726 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 727 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| 728 | while (!OrigBB->succ_empty()) { |
| 729 | MachineBasicBlock *Succ = *OrigBB->succ_begin(); |
| 730 | OrigBB->removeSuccessor(Succ); |
| 731 | NewBB->addSuccessor(Succ); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 732 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 733 | // This pass should be run after register allocation, so there should be no |
| 734 | // PHI nodes to update. |
| 735 | assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI) |
| 736 | && "PHI nodes should be eliminated by now!"); |
| 737 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 738 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 739 | // OrigBB branches to NewBB. |
| 740 | OrigBB->addSuccessor(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 741 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 742 | // Update internal data structures to account for the newly inserted MBB. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 743 | // This is almost the same as UpdateForInsertedWaterBlock, except that |
| 744 | // the Water goes after OrigBB, not NewBB. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 745 | MF.RenumberBlocks(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 746 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 747 | // Insert a size into BBSizes to align it properly with the (newly |
| 748 | // renumbered) block numbers. |
| 749 | BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 750 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 751 | // Likewise for BBOffsets. |
| 752 | BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0); |
| 753 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 754 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 755 | // available water after it (but not if it's already there, which happens |
| 756 | // when splitting before a conditional branch that is followed by an |
| 757 | // unconditional branch - in that case we want to insert NewBB). |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 758 | water_iterator IP = |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 759 | std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, |
| 760 | CompareMBBNumbers); |
| 761 | MachineBasicBlock* WaterBB = *IP; |
| 762 | if (WaterBB == OrigBB) |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 763 | WaterList.insert(llvm::next(IP), NewBB); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 764 | else |
| 765 | WaterList.insert(IP, OrigBB); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 766 | NewWaterList.insert(OrigBB); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 767 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 768 | // Figure out how large the first NewMBB is. (It cannot |
| 769 | // contain a constpool_entry or tablejump.) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 770 | unsigned NewBBSize = 0; |
| 771 | for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end(); |
| 772 | I != E; ++I) |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 773 | NewBBSize += TII->GetInstSizeInBytes(I); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 774 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 775 | unsigned OrigBBI = OrigBB->getNumber(); |
| 776 | unsigned NewBBI = NewBB->getNumber(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 777 | // Set the size of NewBB in BBSizes. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 778 | BBSizes[NewBBI] = NewBBSize; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 779 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 780 | // We removed instructions from UserMBB, subtract that off from its size. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 781 | // Add 2 or 4 to the block to count the unconditional branch we added to it. |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 782 | int delta = isThumb1 ? 2 : 4; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 783 | BBSizes[OrigBBI] -= NewBBSize - delta; |
| 784 | |
| 785 | // ...and adjust BBOffsets for NewBB accordingly. |
| 786 | BBOffsets[NewBBI] = BBOffsets[OrigBBI] + BBSizes[OrigBBI]; |
| 787 | |
| 788 | // All BBOffsets following these blocks must be modified. |
| 789 | AdjustBBOffsetsAfter(NewBB, delta); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 790 | |
| 791 | return NewBB; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 794 | /// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 795 | /// reference) is within MaxDisp of TrialOffset (a proposed location of a |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 796 | /// constant pool entry). |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 797 | bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 798 | unsigned TrialOffset, unsigned MaxDisp, |
| 799 | bool NegativeOK, bool IsSoImm) { |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 800 | // On Thumb offsets==2 mod 4 are rounded down by the hardware for |
| 801 | // purposes of the displacement computation; compensate for that here. |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 802 | // Effectively, the valid range of displacements is 2 bytes smaller for such |
| 803 | // references. |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 804 | unsigned TotalAdj = 0; |
| 805 | if (isThumb && UserOffset%4 !=0) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 806 | UserOffset -= 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 807 | TotalAdj = 2; |
| 808 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 809 | // CPEs will be rounded up to a multiple of 4. |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 810 | if (isThumb && TrialOffset%4 != 0) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 811 | TrialOffset += 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 812 | TotalAdj += 2; |
| 813 | } |
| 814 | |
| 815 | // In Thumb2 mode, later branch adjustments can shift instructions up and |
| 816 | // cause alignment change. In the worst case scenario this can cause the |
| 817 | // user's effective address to be subtracted by 2 and the CPE's address to |
| 818 | // be plus 2. |
| 819 | if (isThumb2 && TotalAdj != 4) |
| 820 | MaxDisp -= (4 - TotalAdj); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 821 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 822 | if (UserOffset <= TrialOffset) { |
| 823 | // User before the Trial. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 824 | if (TrialOffset - UserOffset <= MaxDisp) |
| 825 | return true; |
Evan Cheng | 40efc25 | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 826 | // FIXME: Make use full range of soimm values. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 827 | } else if (NegativeOK) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 828 | if (UserOffset - TrialOffset <= MaxDisp) |
| 829 | return true; |
Evan Cheng | 40efc25 | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 830 | // FIXME: Make use full range of soimm values. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 831 | } |
| 832 | return false; |
| 833 | } |
| 834 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 835 | /// WaterIsInRange - Returns true if a CPE placed after the specified |
| 836 | /// Water (a basic block) will be in range for the specific MI. |
| 837 | |
| 838 | bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 839 | MachineBasicBlock* Water, CPUser &U) { |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 840 | unsigned MaxDisp = U.MaxDisp; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 841 | unsigned CPEOffset = BBOffsets[Water->getNumber()] + |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 842 | BBSizes[Water->getNumber()]; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 843 | |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 844 | // If the CPE is to be inserted before the instruction, that will raise |
Bob Wilson | af4b735 | 2009-10-12 22:49:05 +0000 | [diff] [blame] | 845 | // the offset of the instruction. |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 846 | if (CPEOffset < UserOffset) |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 847 | UserOffset += U.CPEMI->getOperand(2).getImm(); |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 848 | |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 849 | return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /// CPEIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 853 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 854 | bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 855 | MachineInstr *CPEMI, unsigned MaxDisp, |
| 856 | bool NegOk, bool DoDump) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 857 | unsigned CPEOffset = GetOffsetOf(CPEMI); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 858 | assert((CPEOffset%4 == 0 || HasInlineAsm) && "Misaligned CPE"); |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 859 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 860 | if (DoDump) { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 861 | DEBUG(errs() << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 862 | << " max delta=" << MaxDisp |
| 863 | << " insn address=" << UserOffset |
| 864 | << " CPE address=" << CPEOffset |
| 865 | << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 866 | } |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 867 | |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 868 | return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Evan Cheng | d1e7d9a | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 871 | #ifndef NDEBUG |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 872 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 873 | /// unconditionally branches to its only successor. |
| 874 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 875 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 876 | return false; |
| 877 | |
| 878 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 879 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 880 | MachineInstr *PredMI = &Pred->back(); |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 881 | if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB |
| 882 | || PredMI->getOpcode() == ARM::t2B) |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 883 | return PredMI->getOperand(0).getMBB() == Succ; |
| 884 | return false; |
| 885 | } |
Evan Cheng | d1e7d9a | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 886 | #endif // NDEBUG |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 887 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 888 | void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB, |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 889 | int delta) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 890 | MachineFunction::iterator MBBI = BB; MBBI = llvm::next(MBBI); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 891 | for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs(); |
| 892 | i < e; ++i) { |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 893 | BBOffsets[i] += delta; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 894 | // If some existing blocks have padding, adjust the padding as needed, a |
| 895 | // bit tricky. delta can be negative so don't use % on that. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 896 | if (!isThumb) |
| 897 | continue; |
| 898 | MachineBasicBlock *MBB = MBBI; |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 899 | if (!MBB->empty() && !HasInlineAsm) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 900 | // Constant pool entries require padding. |
| 901 | if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) { |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 902 | unsigned OldOffset = BBOffsets[i] - delta; |
| 903 | if ((OldOffset%4) == 0 && (BBOffsets[i]%4) != 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 904 | // add new padding |
| 905 | BBSizes[i] += 2; |
| 906 | delta += 2; |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 907 | } else if ((OldOffset%4) != 0 && (BBOffsets[i]%4) == 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 908 | // remove existing padding |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 909 | BBSizes[i] -= 2; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 910 | delta -= 2; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 911 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 912 | } |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 913 | // Thumb1 jump tables require padding. They should be at the end; |
| 914 | // following unconditional branches are removed by AnalyzeBranch. |
Evan Cheng | 7894762 | 2009-07-24 18:20:44 +0000 | [diff] [blame] | 915 | MachineInstr *ThumbJTMI = prior(MBB->end()); |
Evan Cheng | 66ac531 | 2009-07-25 00:33:29 +0000 | [diff] [blame] | 916 | if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) { |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 917 | unsigned NewMIOffset = GetOffsetOf(ThumbJTMI); |
| 918 | unsigned OldMIOffset = NewMIOffset - delta; |
| 919 | if ((OldMIOffset%4) == 0 && (NewMIOffset%4) != 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 920 | // remove existing padding |
| 921 | BBSizes[i] -= 2; |
| 922 | delta -= 2; |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 923 | } else if ((OldMIOffset%4) != 0 && (NewMIOffset%4) == 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 924 | // add new padding |
| 925 | BBSizes[i] += 2; |
| 926 | delta += 2; |
| 927 | } |
| 928 | } |
| 929 | if (delta==0) |
| 930 | return; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 931 | } |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 932 | MBBI = llvm::next(MBBI); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 933 | } |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 936 | /// DecrementOldEntry - find the constant pool entry with index CPI |
| 937 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 938 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 939 | /// the entry, false if we didn't. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 940 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 941 | bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) { |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 942 | // Find the old entry. Eliminate it if it is no longer used. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 943 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 944 | assert(CPE && "Unexpected!"); |
| 945 | if (--CPE->RefCount == 0) { |
| 946 | RemoveDeadCPEMI(CPEMI); |
| 947 | CPE->CPEMI = NULL; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 948 | NumCPEs--; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 949 | return true; |
| 950 | } |
| 951 | return false; |
| 952 | } |
| 953 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 954 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 955 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 956 | /// change the data structures so the user references the clone. Returns: |
| 957 | /// 0 = no existing entry found |
| 958 | /// 1 = entry found, and there were no code insertions or deletions |
| 959 | /// 2 = entry found, and there were code insertions or deletions |
| 960 | int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset) |
| 961 | { |
| 962 | MachineInstr *UserMI = U.MI; |
| 963 | MachineInstr *CPEMI = U.CPEMI; |
| 964 | |
| 965 | // Check to see if the CPE is already in-range. |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 966 | if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 967 | DEBUG(errs() << "In range\n"); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 968 | return 1; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 971 | // No. Look for previously created clones of the CPE that are in range. |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 972 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 973 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 974 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 975 | // We already tried this one |
| 976 | if (CPEs[i].CPEMI == CPEMI) |
| 977 | continue; |
| 978 | // Removing CPEs can leave empty entries, skip |
| 979 | if (CPEs[i].CPEMI == NULL) |
| 980 | continue; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 981 | if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 982 | DEBUG(errs() << "Replacing CPE#" << CPI << " with CPE#" |
| 983 | << CPEs[i].CPI << "\n"); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 984 | // Point the CPUser node to the replacement |
| 985 | U.CPEMI = CPEs[i].CPEMI; |
| 986 | // Change the CPI in the instruction operand to refer to the clone. |
| 987 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 988 | if (UserMI->getOperand(j).isCPI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 989 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 990 | break; |
| 991 | } |
| 992 | // Adjust the refcount of the clone... |
| 993 | CPEs[i].RefCount++; |
| 994 | // ...and the original. If we didn't remove the old entry, none of the |
| 995 | // addresses changed, so we don't need another pass. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 996 | return DecrementOldEntry(CPI, CPEMI) ? 2 : 1; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1002 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 1003 | /// the specific unconditional branch instruction. |
| 1004 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1005 | switch (Opc) { |
| 1006 | case ARM::tB: |
| 1007 | return ((1<<10)-1)*2; |
| 1008 | case ARM::t2B: |
| 1009 | return ((1<<23)-1)*2; |
| 1010 | default: |
| 1011 | break; |
| 1012 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1013 | |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1014 | return ((1<<23)-1)*4; |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1017 | /// LookForWater - Look for an existing entry in the WaterList in which |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1018 | /// we can place the CPE referenced from U so it's within range of U's MI. |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1019 | /// Returns true if found, false if not. If it returns true, WaterIter |
Bob Wilson | f98032e | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1020 | /// is set to the WaterList entry. For Thumb, prefer water that will not |
| 1021 | /// introduce padding to water that will. To ensure that this pass |
| 1022 | /// terminates, the CPE location for a particular CPUser is only allowed to |
| 1023 | /// move to a lower address, so search backward from the end of the list and |
| 1024 | /// prefer the first water that is in range. |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1025 | bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset, |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1026 | water_iterator &WaterIter) { |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1027 | if (WaterList.empty()) |
| 1028 | return false; |
| 1029 | |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1030 | bool FoundWaterThatWouldPad = false; |
| 1031 | water_iterator IPThatWouldPad; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1032 | for (water_iterator IP = prior(WaterList.end()), |
| 1033 | B = WaterList.begin();; --IP) { |
| 1034 | MachineBasicBlock* WaterBB = *IP; |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1035 | // Check if water is in range and is either at a lower address than the |
| 1036 | // current "high water mark" or a new water block that was created since |
| 1037 | // the previous iteration by inserting an unconditional branch. In the |
| 1038 | // latter case, we want to allow resetting the high water mark back to |
| 1039 | // this new water since we haven't seen it before. Inserting branches |
| 1040 | // should be relatively uncommon and when it does happen, we want to be |
| 1041 | // sure to take advantage of it for all the CPEs near that block, so that |
| 1042 | // we don't insert more branches than necessary. |
| 1043 | if (WaterIsInRange(UserOffset, WaterBB, U) && |
| 1044 | (WaterBB->getNumber() < U.HighWaterMark->getNumber() || |
| 1045 | NewWaterList.count(WaterBB))) { |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1046 | unsigned WBBId = WaterBB->getNumber(); |
| 1047 | if (isThumb && |
| 1048 | (BBOffsets[WBBId] + BBSizes[WBBId])%4 != 0) { |
| 1049 | // This is valid Water, but would introduce padding. Remember |
| 1050 | // it in case we don't find any Water that doesn't do this. |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1051 | if (!FoundWaterThatWouldPad) { |
| 1052 | FoundWaterThatWouldPad = true; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1053 | IPThatWouldPad = IP; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1054 | } |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1055 | } else { |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1056 | WaterIter = IP; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1057 | return true; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1058 | } |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1059 | } |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1060 | if (IP == B) |
| 1061 | break; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1062 | } |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1063 | if (FoundWaterThatWouldPad) { |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1064 | WaterIter = IPThatWouldPad; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1065 | return true; |
| 1066 | } |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
| 1069 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1070 | /// CreateNewWater - No existing WaterList entry will work for |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1071 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 1072 | /// block is used if in range, and the conditional branch munged so control |
| 1073 | /// flow is correct. Otherwise the block is split to create a hole with an |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1074 | /// unconditional branch around it. In either case NewMBB is set to a |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1075 | /// block following which the new island can be inserted (the WaterList |
| 1076 | /// is not adjusted). |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1077 | void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex, |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1078 | unsigned UserOffset, |
| 1079 | MachineBasicBlock *&NewMBB) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1080 | CPUser &U = CPUsers[CPUserIndex]; |
| 1081 | MachineInstr *UserMI = U.MI; |
| 1082 | MachineInstr *CPEMI = U.CPEMI; |
| 1083 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1084 | unsigned OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()] + |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1085 | BBSizes[UserMBB->getNumber()]; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1086 | assert(OffsetOfNextBlock== BBOffsets[UserMBB->getNumber()+1]); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1087 | |
Bob Wilson | 36fa532 | 2009-10-15 05:10:36 +0000 | [diff] [blame] | 1088 | // If the block does not end in an unconditional branch already, and if the |
| 1089 | // end of the block is within range, make new water there. (The addition |
| 1090 | // below is for the unconditional branch we will be adding: 4 bytes on ARM + |
| 1091 | // Thumb2, 2 on Thumb1. Possible Thumb1 alignment padding is allowed for |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1092 | // inside OffsetIsInRange. |
Bob Wilson | 36fa532 | 2009-10-15 05:10:36 +0000 | [diff] [blame] | 1093 | if (BBHasFallthrough(UserMBB) && |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1094 | OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4), |
| 1095 | U.MaxDisp, U.NegOk, U.IsSoImm)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1096 | DEBUG(errs() << "Split at end of block\n"); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1097 | if (&UserMBB->back() == UserMI) |
| 1098 | assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!"); |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1099 | NewMBB = llvm::next(MachineFunction::iterator(UserMBB)); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1100 | // Add an unconditional branch from UserMBB to fallthrough block. |
| 1101 | // Record it for branch lengthening; this new branch will not get out of |
| 1102 | // range, but if the preceding conditional branch is out of range, the |
| 1103 | // targets will be exchanged, and the altered branch may be out of |
| 1104 | // range, so the machinery has to know about it. |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1105 | int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B; |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1106 | BuildMI(UserMBB, DebugLoc::getUnknownLoc(), |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1107 | TII->get(UncondBr)).addMBB(NewMBB); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1108 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1109 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1110 | MaxDisp, false, UncondBr)); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1111 | int delta = isThumb1 ? 2 : 4; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1112 | BBSizes[UserMBB->getNumber()] += delta; |
| 1113 | AdjustBBOffsetsAfter(UserMBB, delta); |
| 1114 | } else { |
| 1115 | // What a big block. Find a place within the block to split it. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1116 | // This is a little tricky on Thumb1 since instructions are 2 bytes |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1117 | // and constant pool entries are 4 bytes: if instruction I references |
| 1118 | // island CPE, and instruction I+1 references CPE', it will |
| 1119 | // not work well to put CPE as far forward as possible, since then |
| 1120 | // CPE' cannot immediately follow it (that location is 2 bytes |
| 1121 | // farther away from I+1 than CPE was from I) and we'd need to create |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1122 | // a new island. So, we make a first guess, then walk through the |
| 1123 | // instructions between the one currently being looked at and the |
| 1124 | // possible insertion point, and make sure any other instructions |
| 1125 | // that reference CPEs will be able to use the same island area; |
| 1126 | // if not, we back up the insertion point. |
| 1127 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1128 | // The 4 in the following is for the unconditional branch we'll be |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1129 | // inserting (allows for long branch on Thumb1). Alignment of the |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1130 | // island is handled inside OffsetIsInRange. |
| 1131 | unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1132 | // This could point off the end of the block if we've already got |
| 1133 | // constant pool entries following this block; only the last one is |
| 1134 | // in the water list. Back past any possible branches (allow for a |
| 1135 | // conditional and a maximally long unconditional). |
| 1136 | if (BaseInsertOffset >= BBOffsets[UserMBB->getNumber()+1]) |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1137 | BaseInsertOffset = BBOffsets[UserMBB->getNumber()+1] - |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1138 | (isThumb1 ? 6 : 8); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1139 | unsigned EndInsertOffset = BaseInsertOffset + |
| 1140 | CPEMI->getOperand(2).getImm(); |
| 1141 | MachineBasicBlock::iterator MI = UserMI; |
| 1142 | ++MI; |
| 1143 | unsigned CPUIndex = CPUserIndex+1; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1144 | for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1145 | Offset < BaseInsertOffset; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1146 | Offset += TII->GetInstSizeInBytes(MI), |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1147 | MI = llvm::next(MI)) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1148 | if (CPUIndex < CPUsers.size() && CPUsers[CPUIndex].MI == MI) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1149 | CPUser &U = CPUsers[CPUIndex]; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1150 | if (!OffsetIsInRange(Offset, EndInsertOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1151 | U.MaxDisp, U.NegOk, U.IsSoImm)) { |
| 1152 | BaseInsertOffset -= (isThumb1 ? 2 : 4); |
| 1153 | EndInsertOffset -= (isThumb1 ? 2 : 4); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | // This is overly conservative, as we don't account for CPEMIs |
| 1156 | // being reused within the block, but it doesn't matter much. |
| 1157 | EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm(); |
| 1158 | CPUIndex++; |
| 1159 | } |
| 1160 | } |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1161 | DEBUG(errs() << "Split in middle of big block\n"); |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1162 | NewMBB = SplitBlockBeforeInstr(prior(MI)); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1166 | /// HandleConstantPoolUser - Analyze the specified user, checking to see if it |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1167 | /// is out-of-range. If so, pick up the constant pool value and move it some |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1168 | /// place in-range. Return true if we changed any addresses (thus must run |
| 1169 | /// another pass of branch lengthening), false otherwise. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1170 | bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF, |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1171 | unsigned CPUserIndex) { |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1172 | CPUser &U = CPUsers[CPUserIndex]; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1173 | MachineInstr *UserMI = U.MI; |
| 1174 | MachineInstr *CPEMI = U.CPEMI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1175 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1176 | unsigned Size = CPEMI->getOperand(2).getImm(); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1177 | // Compute this only once, it's expensive. The 4 or 8 is the value the |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1178 | // hardware keeps in the PC. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1179 | unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8); |
Evan Cheng | 768c9f7 | 2007-04-27 08:14:15 +0000 | [diff] [blame] | 1180 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1181 | // See if the current entry is within range, or there is a clone of it |
| 1182 | // in range. |
| 1183 | int result = LookForExistingCPEntry(U, UserOffset); |
| 1184 | if (result==1) return false; |
| 1185 | else if (result==2) return true; |
| 1186 | |
| 1187 | // No existing clone of this CPE is within range. |
| 1188 | // We will be generating a new clone. Get a UID for it. |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1189 | unsigned ID = AFI->createConstPoolEntryUId(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1190 | |
Bob Wilson | f98032e | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1191 | // Look for water where we can place this CPE. |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1192 | MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock(); |
| 1193 | MachineBasicBlock *NewMBB; |
| 1194 | water_iterator IP; |
| 1195 | if (LookForWater(U, UserOffset, IP)) { |
| 1196 | DEBUG(errs() << "found water in range\n"); |
| 1197 | MachineBasicBlock *WaterBB = *IP; |
| 1198 | |
| 1199 | // If the original WaterList entry was "new water" on this iteration, |
| 1200 | // propagate that to the new island. This is just keeping NewWaterList |
| 1201 | // updated to match the WaterList, which will be updated below. |
| 1202 | if (NewWaterList.count(WaterBB)) { |
| 1203 | NewWaterList.erase(WaterBB); |
| 1204 | NewWaterList.insert(NewIsland); |
| 1205 | } |
| 1206 | // The new CPE goes before the following block (NewMBB). |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1207 | NewMBB = llvm::next(MachineFunction::iterator(WaterBB)); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1208 | |
| 1209 | } else { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1210 | // No water found. |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1211 | DEBUG(errs() << "No water found\n"); |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1212 | CreateNewWater(CPUserIndex, UserOffset, NewMBB); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1213 | |
| 1214 | // SplitBlockBeforeInstr adds to WaterList, which is important when it is |
| 1215 | // called while handling branches so that the water will be seen on the |
| 1216 | // next iteration for constant pools, but in this context, we don't want |
| 1217 | // it. Check for this so it will be removed from the WaterList. |
| 1218 | // Also remove any entry from NewWaterList. |
| 1219 | MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB)); |
| 1220 | IP = std::find(WaterList.begin(), WaterList.end(), WaterBB); |
| 1221 | if (IP != WaterList.end()) |
| 1222 | NewWaterList.erase(WaterBB); |
| 1223 | |
| 1224 | // We are adding new water. Update NewWaterList. |
| 1225 | NewWaterList.insert(NewIsland); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1228 | // Remove the original WaterList entry; we want subsequent insertions in |
| 1229 | // this vicinity to go after the one we're about to insert. This |
| 1230 | // considerably reduces the number of times we have to move the same CPE |
| 1231 | // more than once and is also important to ensure the algorithm terminates. |
| 1232 | if (IP != WaterList.end()) |
| 1233 | WaterList.erase(IP); |
| 1234 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1235 | // Okay, we know we can put an island before NewMBB now, do it! |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1236 | MF.insert(NewMBB, NewIsland); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Update internal data structures to account for the newly inserted MBB. |
| 1239 | UpdateForInsertedWaterBlock(NewIsland); |
| 1240 | |
| 1241 | // Decrement the old entry, and remove it if refcount becomes 0. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1242 | DecrementOldEntry(CPI, CPEMI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 1245 | // add it to the island. |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 1246 | U.HighWaterMark = NewIsland; |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1247 | U.CPEMI = BuildMI(NewIsland, DebugLoc::getUnknownLoc(), |
| 1248 | TII->get(ARM::CONSTPOOL_ENTRY)) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1249 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1250 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1251 | NumCPEs++; |
| 1252 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1253 | BBOffsets[NewIsland->getNumber()] = BBOffsets[NewMBB->getNumber()]; |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 1254 | // Compensate for .align 2 in thumb mode. |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 1255 | if (isThumb && (BBOffsets[NewIsland->getNumber()]%4 != 0 || HasInlineAsm)) |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1256 | Size += 2; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1257 | // Increase the size of the island block to account for the new entry. |
| 1258 | BBSizes[NewIsland->getNumber()] += Size; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1259 | AdjustBBOffsetsAfter(NewIsland, Size); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1260 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1261 | // Finally, change the CPI in the instruction operand to be ID. |
| 1262 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1263 | if (UserMI->getOperand(i).isCPI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1264 | UserMI->getOperand(i).setIndex(ID); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1265 | break; |
| 1266 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1268 | DEBUG(errs() << " Moved CPE to #" << ID << " CPI=" << CPI |
| 1269 | << '\t' << *UserMI); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1270 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1274 | /// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update |
| 1275 | /// sizes and offsets of impacted basic blocks. |
| 1276 | void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) { |
| 1277 | MachineBasicBlock *CPEBB = CPEMI->getParent(); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1278 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1279 | CPEMI->eraseFromParent(); |
| 1280 | BBSizes[CPEBB->getNumber()] -= Size; |
| 1281 | // All succeeding offsets have the current size value added in, fix this. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1282 | if (CPEBB->empty()) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1283 | // In thumb1 mode, the size of island may be padded by two to compensate for |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1284 | // the alignment requirement. Then it will now be 2 when the block is |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1285 | // empty, so fix this. |
| 1286 | // All succeeding offsets have the current size value added in, fix this. |
| 1287 | if (BBSizes[CPEBB->getNumber()] != 0) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1288 | Size += BBSizes[CPEBB->getNumber()]; |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1289 | BBSizes[CPEBB->getNumber()] = 0; |
| 1290 | } |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1291 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1292 | AdjustBBOffsetsAfter(CPEBB, -Size); |
| 1293 | // An island has only one predecessor BB and one successor BB. Check if |
| 1294 | // this BB's predecessor jumps directly to this BB's successor. This |
| 1295 | // shouldn't happen currently. |
| 1296 | assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); |
| 1297 | // FIXME: remove the empty blocks after all the work is done? |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts |
| 1301 | /// are zero. |
| 1302 | bool ARMConstantIslands::RemoveUnusedCPEntries() { |
| 1303 | unsigned MadeChange = false; |
| 1304 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 1305 | std::vector<CPEntry> &CPEs = CPEntries[i]; |
| 1306 | for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { |
| 1307 | if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { |
| 1308 | RemoveDeadCPEMI(CPEs[j].CPEMI); |
| 1309 | CPEs[j].CPEMI = NULL; |
| 1310 | MadeChange = true; |
| 1311 | } |
| 1312 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1313 | } |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1314 | return MadeChange; |
| 1315 | } |
| 1316 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1317 | /// BBIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1318 | /// specific BB can fit in MI's displacement field. |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1319 | bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB, |
| 1320 | unsigned MaxDisp) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1321 | unsigned PCAdj = isThumb ? 4 : 8; |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1322 | unsigned BrOffset = GetOffsetOf(MI) + PCAdj; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1323 | unsigned DestOffset = BBOffsets[DestBB->getNumber()]; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1325 | DEBUG(errs() << "Branch of destination BB#" << DestBB->getNumber() |
| 1326 | << " from BB#" << MI->getParent()->getNumber() |
| 1327 | << " max delta=" << MaxDisp |
| 1328 | << " from " << GetOffsetOf(MI) << " to " << DestOffset |
| 1329 | << " offset " << int(DestOffset-BrOffset) << "\t" << *MI); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1330 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1331 | if (BrOffset <= DestOffset) { |
| 1332 | // Branch before the Dest. |
| 1333 | if (DestOffset-BrOffset <= MaxDisp) |
| 1334 | return true; |
| 1335 | } else { |
| 1336 | if (BrOffset-DestOffset <= MaxDisp) |
| 1337 | return true; |
| 1338 | } |
| 1339 | return false; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1342 | /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far |
| 1343 | /// away to fit in its displacement field. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1344 | bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1345 | MachineInstr *MI = Br.MI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1346 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1347 | |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1348 | // Check to see if the DestBB is already in-range. |
| 1349 | if (BBIsInRange(MI, DestBB, Br.MaxDisp)) |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1350 | return false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1351 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1352 | if (!Br.isCond) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1353 | return FixUpUnconditionalBr(MF, Br); |
| 1354 | return FixUpConditionalBr(MF, Br); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1355 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1356 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1357 | /// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is |
| 1358 | /// too far away to fit in its displacement field. If the LR register has been |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1359 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1360 | /// Otherwise, add an intermediate branch instruction to a branch. |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1361 | bool |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1362 | ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1363 | MachineInstr *MI = Br.MI; |
| 1364 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | 53c67c0 | 2009-08-07 05:45:07 +0000 | [diff] [blame] | 1365 | if (!isThumb1) |
| 1366 | llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!"); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Use BL to implement far jump. |
| 1369 | Br.MaxDisp = (1 << 21) * 2; |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1370 | MI->setDesc(TII->get(ARM::tBfar)); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1371 | BBSizes[MBB->getNumber()] += 2; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1372 | AdjustBBOffsetsAfter(MBB, 2); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1373 | HasFarJump = true; |
| 1374 | NumUBrFixed++; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1376 | DEBUG(errs() << " Changed B to long jump " << *MI); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1377 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1378 | return true; |
| 1379 | } |
| 1380 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1381 | /// FixUpConditionalBr - Fix up a conditional branch whose destination is too |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1382 | /// far away to fit in its displacement field. It is converted to an inverse |
| 1383 | /// conditional branch + an unconditional branch to the destination. |
| 1384 | bool |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1385 | ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1386 | MachineInstr *MI = Br.MI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1387 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1388 | |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1389 | // Add an unconditional branch to the destination and invert the branch |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1390 | // condition to jump over it: |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1391 | // blt L1 |
| 1392 | // => |
| 1393 | // bge L2 |
| 1394 | // b L1 |
| 1395 | // L2: |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 1396 | ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1397 | CC = ARMCC::getOppositeCondition(CC); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1398 | unsigned CCReg = MI->getOperand(2).getReg(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1399 | |
| 1400 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1401 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1402 | // split the MBB before the next instruction. |
| 1403 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1404 | MachineInstr *BMI = &MBB->back(); |
| 1405 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1406 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1407 | NumCBrFixed++; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1408 | if (BMI != MI) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1409 | if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) && |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1410 | BMI->getOpcode() == Br.UncondBr) { |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1411 | // Last MI in the BB is an unconditional branch. Can we simply invert the |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1412 | // condition and swap destinations: |
| 1413 | // beq L1 |
| 1414 | // b L2 |
| 1415 | // => |
| 1416 | // bne L2 |
| 1417 | // b L1 |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1418 | MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1419 | if (BBIsInRange(MI, NewDest, Br.MaxDisp)) { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1420 | DEBUG(errs() << " Invert Bcc condition and swap its destination with " |
| 1421 | << *BMI); |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1422 | BMI->getOperand(0).setMBB(DestBB); |
| 1423 | MI->getOperand(0).setMBB(NewDest); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1424 | MI->getOperand(1).setImm(CC); |
| 1425 | return true; |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | if (NeedSplit) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1431 | SplitBlockBeforeInstr(MI); |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1432 | // No need for the branch to the next block. We're adding an unconditional |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1433 | // branch to the destination. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1434 | int delta = TII->GetInstSizeInBytes(&MBB->back()); |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1435 | BBSizes[MBB->getNumber()] -= delta; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1436 | MachineBasicBlock* SplitBB = llvm::next(MachineFunction::iterator(MBB)); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1437 | AdjustBBOffsetsAfter(SplitBB, -delta); |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1438 | MBB->back().eraseFromParent(); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1439 | // BBOffsets[SplitBB] is wrong temporarily, fixed below |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1440 | } |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1441 | MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB)); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1442 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1443 | DEBUG(errs() << " Insert B to BB#" << DestBB->getNumber() |
| 1444 | << " also invert condition and change dest. to BB#" |
| 1445 | << NextBB->getNumber() << "\n"); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1446 | |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1447 | // Insert a new conditional branch and a new unconditional branch. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1448 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1449 | BuildMI(MBB, DebugLoc::getUnknownLoc(), |
| 1450 | TII->get(MI->getOpcode())) |
| 1451 | .addMBB(NextBB).addImm(CC).addReg(CCReg); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1452 | Br.MI = &MBB->back(); |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1453 | BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back()); |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1454 | BuildMI(MBB, DebugLoc::getUnknownLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1455 | BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back()); |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 1456 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
Evan Cheng | a0bf794 | 2007-01-25 23:31:04 +0000 | [diff] [blame] | 1457 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1458 | |
| 1459 | // Remove the old conditional branch. It may or may not still be in MBB. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1460 | BBSizes[MI->getParent()->getNumber()] -= TII->GetInstSizeInBytes(MI); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1461 | MI->eraseFromParent(); |
| 1462 | |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1463 | // The net size change is an addition of one unconditional branch. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1464 | int delta = TII->GetInstSizeInBytes(&MBB->back()); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1465 | AdjustBBOffsetsAfter(MBB, delta); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1466 | return true; |
| 1467 | } |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1468 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1469 | /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 1470 | /// LR / restores LR to pc. FIXME: This is done here because it's only possible |
| 1471 | /// to do this if tBfar is not used. |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1472 | bool ARMConstantIslands::UndoLRSpillRestore() { |
| 1473 | bool MadeChange = false; |
| 1474 | for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { |
| 1475 | MachineInstr *MI = PushPopMIs[i]; |
Evan Cheng | 10469f8 | 2009-10-01 20:54:53 +0000 | [diff] [blame] | 1476 | // First two operands are predicates, the third is a zero since there |
| 1477 | // is no writeback. |
Evan Cheng | 44bec52 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1478 | if (MI->getOpcode() == ARM::tPOP_RET && |
Evan Cheng | 10469f8 | 2009-10-01 20:54:53 +0000 | [diff] [blame] | 1479 | MI->getOperand(3).getReg() == ARM::PC && |
| 1480 | MI->getNumExplicitOperands() == 4) { |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1481 | BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET)); |
Evan Cheng | 44bec52 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1482 | MI->eraseFromParent(); |
| 1483 | MadeChange = true; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | return MadeChange; |
| 1487 | } |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1488 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1489 | bool ARMConstantIslands::OptimizeThumb2Instructions(MachineFunction &MF) { |
| 1490 | bool MadeChange = false; |
| 1491 | |
| 1492 | // Shrink ADR and LDR from constantpool. |
| 1493 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 1494 | CPUser &U = CPUsers[i]; |
| 1495 | unsigned Opcode = U.MI->getOpcode(); |
| 1496 | unsigned NewOpc = 0; |
| 1497 | unsigned Scale = 1; |
| 1498 | unsigned Bits = 0; |
| 1499 | switch (Opcode) { |
| 1500 | default: break; |
| 1501 | case ARM::t2LEApcrel: |
| 1502 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1503 | NewOpc = ARM::tLEApcrel; |
| 1504 | Bits = 8; |
| 1505 | Scale = 4; |
| 1506 | } |
| 1507 | break; |
| 1508 | case ARM::t2LDRpci: |
| 1509 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1510 | NewOpc = ARM::tLDRpci; |
| 1511 | Bits = 8; |
| 1512 | Scale = 4; |
| 1513 | } |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | if (!NewOpc) |
| 1518 | continue; |
| 1519 | |
| 1520 | unsigned UserOffset = GetOffsetOf(U.MI) + 4; |
| 1521 | unsigned MaxOffs = ((1 << Bits) - 1) * Scale; |
| 1522 | // FIXME: Check if offset is multiple of scale if scale is not 4. |
| 1523 | if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) { |
| 1524 | U.MI->setDesc(TII->get(NewOpc)); |
| 1525 | MachineBasicBlock *MBB = U.MI->getParent(); |
| 1526 | BBSizes[MBB->getNumber()] -= 2; |
| 1527 | AdjustBBOffsetsAfter(MBB, -2); |
| 1528 | ++NumT2CPShrunk; |
| 1529 | MadeChange = true; |
| 1530 | } |
| 1531 | } |
| 1532 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1533 | MadeChange |= OptimizeThumb2Branches(MF); |
Jim Grosbach | 01dec0e | 2009-11-12 03:28:35 +0000 | [diff] [blame] | 1534 | MadeChange |= OptimizeThumb2JumpTables(MF); |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1535 | return MadeChange; |
| 1536 | } |
| 1537 | |
| 1538 | bool ARMConstantIslands::OptimizeThumb2Branches(MachineFunction &MF) { |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1539 | bool MadeChange = false; |
| 1540 | |
| 1541 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) { |
| 1542 | ImmBranch &Br = ImmBranches[i]; |
| 1543 | unsigned Opcode = Br.MI->getOpcode(); |
| 1544 | unsigned NewOpc = 0; |
| 1545 | unsigned Scale = 1; |
| 1546 | unsigned Bits = 0; |
| 1547 | switch (Opcode) { |
| 1548 | default: break; |
| 1549 | case ARM::t2B: |
| 1550 | NewOpc = ARM::tB; |
| 1551 | Bits = 11; |
| 1552 | Scale = 2; |
| 1553 | break; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1554 | case ARM::t2Bcc: { |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1555 | NewOpc = ARM::tBcc; |
| 1556 | Bits = 8; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1557 | Scale = 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | } |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1560 | } |
| 1561 | if (NewOpc) { |
| 1562 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 1563 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
| 1564 | if (BBIsInRange(Br.MI, DestBB, MaxOffs)) { |
| 1565 | Br.MI->setDesc(TII->get(NewOpc)); |
| 1566 | MachineBasicBlock *MBB = Br.MI->getParent(); |
| 1567 | BBSizes[MBB->getNumber()] -= 2; |
| 1568 | AdjustBBOffsetsAfter(MBB, -2); |
| 1569 | ++NumT2BrShrunk; |
| 1570 | MadeChange = true; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | Opcode = Br.MI->getOpcode(); |
| 1575 | if (Opcode != ARM::tBcc) |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1576 | continue; |
| 1577 | |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1578 | NewOpc = 0; |
| 1579 | unsigned PredReg = 0; |
| 1580 | ARMCC::CondCodes Pred = llvm::getInstrPredicate(Br.MI, PredReg); |
| 1581 | if (Pred == ARMCC::EQ) |
| 1582 | NewOpc = ARM::tCBZ; |
| 1583 | else if (Pred == ARMCC::NE) |
| 1584 | NewOpc = ARM::tCBNZ; |
| 1585 | if (!NewOpc) |
| 1586 | continue; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1587 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1588 | // Check if the distance is within 126. Subtract starting offset by 2 |
| 1589 | // because the cmp will be eliminated. |
| 1590 | unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2; |
| 1591 | unsigned DestOffset = BBOffsets[DestBB->getNumber()]; |
| 1592 | if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) { |
| 1593 | MachineBasicBlock::iterator CmpMI = Br.MI; --CmpMI; |
| 1594 | if (CmpMI->getOpcode() == ARM::tCMPzi8) { |
| 1595 | unsigned Reg = CmpMI->getOperand(0).getReg(); |
| 1596 | Pred = llvm::getInstrPredicate(CmpMI, PredReg); |
| 1597 | if (Pred == ARMCC::AL && |
| 1598 | CmpMI->getOperand(1).getImm() == 0 && |
| 1599 | isARMLowRegister(Reg)) { |
| 1600 | MachineBasicBlock *MBB = Br.MI->getParent(); |
| 1601 | MachineInstr *NewBR = |
| 1602 | BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc)) |
| 1603 | .addReg(Reg).addMBB(DestBB, Br.MI->getOperand(0).getTargetFlags()); |
| 1604 | CmpMI->eraseFromParent(); |
| 1605 | Br.MI->eraseFromParent(); |
| 1606 | Br.MI = NewBR; |
| 1607 | BBSizes[MBB->getNumber()] -= 2; |
| 1608 | AdjustBBOffsetsAfter(MBB, -2); |
| 1609 | ++NumCBZ; |
| 1610 | MadeChange = true; |
| 1611 | } |
| 1612 | } |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | return MadeChange; |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1619 | /// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller |
| 1620 | /// jumptables when it's possible. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1621 | bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) { |
| 1622 | bool MadeChange = false; |
| 1623 | |
| 1624 | // FIXME: After the tables are shrunk, can we get rid some of the |
| 1625 | // constantpool tables? |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1626 | MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 1627 | if (MJTI == 0) return false; |
| 1628 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1629 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1630 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 1631 | MachineInstr *MI = T2JumpTables[i]; |
| 1632 | const TargetInstrDesc &TID = MI->getDesc(); |
| 1633 | unsigned NumOps = TID.getNumOperands(); |
| 1634 | unsigned JTOpIdx = NumOps - (TID.isPredicable() ? 3 : 2); |
| 1635 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 1636 | unsigned JTI = JTOP.getIndex(); |
| 1637 | assert(JTI < JT.size()); |
| 1638 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1639 | bool ByteOk = true; |
| 1640 | bool HalfWordOk = true; |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1641 | unsigned JTOffset = GetOffsetOf(MI) + 4; |
| 1642 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1643 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 1644 | MachineBasicBlock *MBB = JTBBs[j]; |
| 1645 | unsigned DstOffset = BBOffsets[MBB->getNumber()]; |
Evan Cheng | 8770f74 | 2009-07-29 23:20:20 +0000 | [diff] [blame] | 1646 | // Negative offset is not ok. FIXME: We should change BB layout to make |
| 1647 | // sure all the branches are forward. |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 1648 | if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1649 | ByteOk = false; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1650 | unsigned TBHLimit = ((1<<16)-1)*2; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1651 | if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1652 | HalfWordOk = false; |
| 1653 | if (!ByteOk && !HalfWordOk) |
| 1654 | break; |
| 1655 | } |
| 1656 | |
| 1657 | if (ByteOk || HalfWordOk) { |
| 1658 | MachineBasicBlock *MBB = MI->getParent(); |
| 1659 | unsigned BaseReg = MI->getOperand(0).getReg(); |
| 1660 | bool BaseRegKill = MI->getOperand(0).isKill(); |
| 1661 | if (!BaseRegKill) |
| 1662 | continue; |
| 1663 | unsigned IdxReg = MI->getOperand(1).getReg(); |
| 1664 | bool IdxRegKill = MI->getOperand(1).isKill(); |
| 1665 | MachineBasicBlock::iterator PrevI = MI; |
| 1666 | if (PrevI == MBB->begin()) |
| 1667 | continue; |
| 1668 | |
| 1669 | MachineInstr *AddrMI = --PrevI; |
| 1670 | bool OptOk = true; |
| 1671 | // Examine the instruction that calculate the jumptable entry address. |
| 1672 | // If it's not the one just before the t2BR_JT, we won't delete it, then |
| 1673 | // it's not worth doing the optimization. |
| 1674 | for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) { |
| 1675 | const MachineOperand &MO = AddrMI->getOperand(k); |
| 1676 | if (!MO.isReg() || !MO.getReg()) |
| 1677 | continue; |
| 1678 | if (MO.isDef() && MO.getReg() != BaseReg) { |
| 1679 | OptOk = false; |
| 1680 | break; |
| 1681 | } |
| 1682 | if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) { |
| 1683 | OptOk = false; |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | if (!OptOk) |
| 1688 | continue; |
| 1689 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1690 | // The previous instruction should be a tLEApcrel or t2LEApcrelJT, we want |
| 1691 | // to delete it as well. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1692 | MachineInstr *LeaMI = --PrevI; |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1693 | if ((LeaMI->getOpcode() != ARM::tLEApcrelJT && |
| 1694 | LeaMI->getOpcode() != ARM::t2LEApcrelJT) || |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1695 | LeaMI->getOperand(0).getReg() != BaseReg) |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1696 | OptOk = false; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1697 | |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1698 | if (!OptOk) |
| 1699 | continue; |
| 1700 | |
| 1701 | unsigned Opc = ByteOk ? ARM::t2TBB : ARM::t2TBH; |
| 1702 | MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc)) |
| 1703 | .addReg(IdxReg, getKillRegState(IdxRegKill)) |
| 1704 | .addJumpTableIndex(JTI, JTOP.getTargetFlags()) |
| 1705 | .addImm(MI->getOperand(JTOpIdx+1).getImm()); |
| 1706 | // FIXME: Insert an "ALIGN" instruction to ensure the next instruction |
| 1707 | // is 2-byte aligned. For now, asm printer will fix it up. |
| 1708 | unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI); |
| 1709 | unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI); |
| 1710 | OrigSize += TII->GetInstSizeInBytes(LeaMI); |
| 1711 | OrigSize += TII->GetInstSizeInBytes(MI); |
| 1712 | |
| 1713 | AddrMI->eraseFromParent(); |
| 1714 | LeaMI->eraseFromParent(); |
| 1715 | MI->eraseFromParent(); |
| 1716 | |
| 1717 | int delta = OrigSize - NewSize; |
| 1718 | BBSizes[MBB->getNumber()] -= delta; |
| 1719 | AdjustBBOffsetsAfter(MBB, -delta); |
| 1720 | |
| 1721 | ++NumTBs; |
| 1722 | MadeChange = true; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | return MadeChange; |
| 1727 | } |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1728 | |
Jim Grosbach | 9249efe | 2009-11-16 18:55:47 +0000 | [diff] [blame] | 1729 | /// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that |
| 1730 | /// jump tables always branch forwards, since that's what tbb and tbh need. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1731 | bool ARMConstantIslands::ReorderThumb2JumpTables(MachineFunction &MF) { |
| 1732 | bool MadeChange = false; |
| 1733 | |
| 1734 | MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 1735 | if (MJTI == 0) return false; |
| 1736 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1737 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1738 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 1739 | MachineInstr *MI = T2JumpTables[i]; |
| 1740 | const TargetInstrDesc &TID = MI->getDesc(); |
| 1741 | unsigned NumOps = TID.getNumOperands(); |
| 1742 | unsigned JTOpIdx = NumOps - (TID.isPredicable() ? 3 : 2); |
| 1743 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 1744 | unsigned JTI = JTOP.getIndex(); |
| 1745 | assert(JTI < JT.size()); |
| 1746 | |
| 1747 | // We prefer if target blocks for the jump table come after the jump |
| 1748 | // instruction so we can use TB[BH]. Loop through the target blocks |
| 1749 | // and try to adjust them such that that's true. |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1750 | int JTNumber = MI->getParent()->getNumber(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1751 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
| 1752 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 1753 | MachineBasicBlock *MBB = JTBBs[j]; |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1754 | int DTNumber = MBB->getNumber(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1755 | |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1756 | if (DTNumber < JTNumber) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1757 | // The destination precedes the switch. Try to move the block forward |
| 1758 | // so we have a positive offset. |
| 1759 | MachineBasicBlock *NewBB = |
| 1760 | AdjustJTTargetBlockForward(MBB, MI->getParent()); |
| 1761 | if (NewBB) |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1762 | MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1763 | MadeChange = true; |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | return MadeChange; |
| 1769 | } |
| 1770 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1771 | MachineBasicBlock *ARMConstantIslands:: |
| 1772 | AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) |
| 1773 | { |
| 1774 | MachineFunction &MF = *BB->getParent(); |
| 1775 | |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1776 | // If it's the destination block is terminated by an unconditional branch, |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1777 | // try to move it; otherwise, create a new block following the jump |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1778 | // table that branches back to the actual target. This is a very simple |
| 1779 | // heuristic. FIXME: We can definitely improve it. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1780 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 1781 | SmallVector<MachineOperand, 4> Cond; |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1782 | SmallVector<MachineOperand, 4> CondPrior; |
| 1783 | MachineFunction::iterator BBi = BB; |
| 1784 | MachineFunction::iterator OldPrior = prior(BBi); |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1785 | |
Jim Grosbach | ca215e7 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 1786 | // If the block terminator isn't analyzable, don't try to move the block |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1787 | bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond); |
Jim Grosbach | ca215e7 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 1788 | |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1789 | // If the block ends in an unconditional branch, move it. The prior block |
| 1790 | // has to have an analyzable terminator for us to move this one. Be paranoid |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1791 | // and make sure we're not trying to move the entry block of the function. |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1792 | if (!B && Cond.empty() && BB != MF.begin() && |
| 1793 | !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1794 | BB->moveAfter(JTBB); |
| 1795 | OldPrior->updateTerminator(); |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1796 | BB->updateTerminator(); |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1797 | // Update numbering to account for the block being moved. |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1798 | MF.RenumberBlocks(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1799 | ++NumJTMoved; |
| 1800 | return NULL; |
| 1801 | } |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Create a new MBB for the code after the jump BB. |
| 1804 | MachineBasicBlock *NewBB = |
| 1805 | MF.CreateMachineBasicBlock(JTBB->getBasicBlock()); |
| 1806 | MachineFunction::iterator MBBI = JTBB; ++MBBI; |
| 1807 | MF.insert(MBBI, NewBB); |
| 1808 | |
| 1809 | // Add an unconditional branch from NewBB to BB. |
| 1810 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 1811 | // correspond directly to anything in the source. |
| 1812 | assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?"); |
| 1813 | BuildMI(NewBB, DebugLoc::getUnknownLoc(), TII->get(ARM::t2B)).addMBB(BB); |
| 1814 | |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1815 | // Update internal data structures to account for the newly inserted MBB. |
| 1816 | MF.RenumberBlocks(NewBB); |
| 1817 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1818 | // Update the CFG. |
| 1819 | NewBB->addSuccessor(BB); |
| 1820 | JTBB->removeSuccessor(BB); |
| 1821 | JTBB->addSuccessor(NewBB); |
| 1822 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1823 | ++NumJTInserted; |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1824 | return NewBB; |
| 1825 | } |