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