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