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