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