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