blob: 65abff0b950139a7dbf36cd0351777548f08d12c [file] [log] [blame]
Bill Wendling9a4d2e42010-12-21 01:54:40 +00001//===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===//
Evan Chenga8e29892007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that splits the constant pool up into 'islands'
11// which are scattered through-out the function. This is required due to the
12// limited pc-relative displacements that ARM has.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "arm-cp-islands"
17#include "ARM.h"
Evan Chengaf5cbcb2007-01-25 03:12:46 +000018#include "ARMMachineFunctionInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMInstrInfo.h"
Evan Cheng719510a2010-08-12 20:30:05 +000020#include "Thumb2InstrInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Cheng5657c012009-07-29 02:18:14 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetMachine.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +000029#include "llvm/Support/Format.h"
Chris Lattner705e07f2009-08-23 03:41:05 +000030#include "llvm/Support/raw_ostream.h"
Bob Wilsonb9239532009-10-15 20:49:47 +000031#include "llvm/ADT/SmallSet.h"
Evan Chengc99ef082007-02-09 20:54:44 +000032#include "llvm/ADT/SmallVector.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/ADT/STLExtras.h"
34#include "llvm/ADT/Statistic.h"
Jim Grosbach1fc7d712009-11-11 02:47:19 +000035#include "llvm/Support/CommandLine.h"
Bob Wilsonb9239532009-10-15 20:49:47 +000036#include <algorithm>
Evan Chenga8e29892007-01-19 07:51:42 +000037using namespace llvm;
38
Evan Chenga1efbbd2009-08-14 00:32:16 +000039STATISTIC(NumCPEs, "Number of constpool entries");
40STATISTIC(NumSplit, "Number of uncond branches inserted");
41STATISTIC(NumCBrFixed, "Number of cond branches fixed");
42STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
43STATISTIC(NumTBs, "Number of table branches generated");
44STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk");
Evan Cheng31b99dd2009-08-14 18:31:44 +000045STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
Evan Chengde17fb62009-10-31 23:46:45 +000046STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
Jim Grosbach1fc7d712009-11-11 02:47:19 +000047STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
Jim Grosbach80697d12009-11-12 17:25:07 +000048STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
Jim Grosbach1fc7d712009-11-11 02:47:19 +000049
50
51static cl::opt<bool>
Jim Grosbachf04777b2009-11-17 21:24:11 +000052AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
Jim Grosbach1fc7d712009-11-11 02:47:19 +000053 cl::desc("Adjust basic block layout to better use TB[BH]"));
Evan Chenga8e29892007-01-19 07:51:42 +000054
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +000055static cl::opt<bool>
56AlignConstantIslands("arm-align-constant-island", cl::Hidden,
57 cl::desc("Align constant islands in code"));
58
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +000059/// UnknownPadding - Return the worst case padding that could result from
60/// unknown offset bits. This does not include alignment padding caused by
61/// known offset bits.
62///
63/// @param LogAlign log2(alignment)
64/// @param KnownBits Number of known low offset bits.
65static inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) {
66 if (KnownBits < LogAlign)
67 return (1u << LogAlign) - (1u << KnownBits);
68 return 0;
69}
70
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +000071/// WorstCaseAlign - Assuming only the low KnownBits bits in Offset are exact,
72/// add padding such that:
73///
74/// 1. The result is aligned to 1 << LogAlign.
75///
76/// 2. No other value of the unknown bits would require more padding.
77///
78/// This may add more padding than is required to satisfy just one of the
79/// constraints. It is necessary to compute alignment this way to guarantee
80/// that we don't underestimate the padding before an aligned block. If the
81/// real padding before a block is larger than we think, constant pool entries
82/// may go out of range.
83static inline unsigned WorstCaseAlign(unsigned Offset, unsigned LogAlign,
84 unsigned KnownBits) {
85 // Add the worst possible padding that the unknown bits could cause.
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +000086 Offset += UnknownPadding(LogAlign, KnownBits);
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +000087
88 // Then align the result.
89 return RoundUpToAlignment(Offset, 1u << LogAlign);
90}
91
Evan Chenga8e29892007-01-19 07:51:42 +000092namespace {
Dale Johannesen88e37ae2007-02-23 05:02:36 +000093 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
Evan Chenga8e29892007-01-19 07:51:42 +000094 /// requires constant pool entries to be scattered among the instructions
95 /// inside a function. To do this, it completely ignores the normal LLVM
Dale Johannesen88e37ae2007-02-23 05:02:36 +000096 /// constant pool; instead, it places constants wherever it feels like with
Evan Chenga8e29892007-01-19 07:51:42 +000097 /// special instructions.
98 ///
99 /// The terminology used in this pass includes:
100 /// Islands - Clumps of constants placed in the function.
101 /// Water - Potential places where an island could be formed.
102 /// CPE - A constant pool entry that has been placed somewhere, which
103 /// tracks a list of users.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000104 class ARMConstantIslands : public MachineFunctionPass {
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000105 /// BasicBlockInfo - Information about the offset and size of a single
106 /// basic block.
107 struct BasicBlockInfo {
108 /// Offset - Distance from the beginning of the function to the beginning
109 /// of this basic block.
110 ///
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000111 /// The offset is always aligned as required by the basic block.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000112 unsigned Offset;
Bob Wilson84945262009-05-12 17:09:30 +0000113
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000114 /// Size - Size of the basic block in bytes. If the block contains
115 /// inline assembly, this is a worst case estimate.
116 ///
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000117 /// The size does not include any alignment padding whether from the
118 /// beginning of the block, or from an aligned jump table at the end.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000119 unsigned Size;
120
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000121 /// KnownBits - The number of low bits in Offset that are known to be
122 /// exact. The remaining bits of Offset are an upper bound.
123 uint8_t KnownBits;
124
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000125 /// Unalign - When non-zero, the block contains instructions (inline asm)
126 /// of unknown size. The real size may be smaller than Size bytes by a
127 /// multiple of 1 << Unalign.
128 uint8_t Unalign;
129
130 /// PostAlign - When non-zero, the block terminator contains a .align
131 /// directive, so the end of the block is aligned to 1 << PostAlign
132 /// bytes.
133 uint8_t PostAlign;
134
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000135 BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0),
136 PostAlign(0) {}
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000137
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +0000138 /// Compute the number of known offset bits internally to this block.
139 /// This number should be used to predict worst case padding when
140 /// splitting the block.
141 unsigned internalKnownBits() const {
142 return Unalign ? Unalign : KnownBits;
143 }
144
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000145 /// Compute the offset immediately following this block.
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000146 unsigned postOffset() const {
147 unsigned PO = Offset + Size;
148 if (!PostAlign)
149 return PO;
150 // Add alignment padding from the terminator.
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +0000151 return WorstCaseAlign(PO, PostAlign, internalKnownBits());
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000152 }
153
154 /// Compute the number of known low bits of postOffset. If this block
155 /// contains inline asm, the number of known bits drops to the
156 /// instruction alignment. An aligned terminator may increase the number
157 /// of know bits.
158 unsigned postKnownBits() const {
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +0000159 return std::max(unsigned(PostAlign), internalKnownBits());
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000160 }
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000161 };
162
163 std::vector<BasicBlockInfo> BBInfo;
Dale Johannesen99c49a42007-02-25 00:47:03 +0000164
Evan Chenga8e29892007-01-19 07:51:42 +0000165 /// WaterList - A sorted list of basic blocks where islands could be placed
166 /// (i.e. blocks that don't fall through to the following block, due
167 /// to a return, unreachable, or unconditional branch).
Evan Chenge03cff62007-02-09 23:59:14 +0000168 std::vector<MachineBasicBlock*> WaterList;
Evan Chengc99ef082007-02-09 20:54:44 +0000169
Bob Wilsonb9239532009-10-15 20:49:47 +0000170 /// NewWaterList - The subset of WaterList that was created since the
171 /// previous iteration by inserting unconditional branches.
172 SmallSet<MachineBasicBlock*, 4> NewWaterList;
173
Bob Wilson034de5f2009-10-12 18:52:13 +0000174 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
175
Evan Chenga8e29892007-01-19 07:51:42 +0000176 /// CPUser - One user of a constant pool, keeping the machine instruction
177 /// pointer, the constant pool being referenced, and the max displacement
Bob Wilson549dda92009-10-15 05:52:29 +0000178 /// allowed from the instruction to the CP. The HighWaterMark records the
179 /// highest basic block where a new CPEntry can be placed. To ensure this
180 /// pass terminates, the CP entries are initially placed at the end of the
181 /// function and then move monotonically to lower addresses. The
182 /// exception to this rule is when the current CP entry for a particular
183 /// CPUser is out of range, but there is another CP entry for the same
184 /// constant value in range. We want to use the existing in-range CP
185 /// entry, but if it later moves out of range, the search for new water
186 /// should resume where it left off. The HighWaterMark is used to record
187 /// that point.
Evan Chenga8e29892007-01-19 07:51:42 +0000188 struct CPUser {
189 MachineInstr *MI;
190 MachineInstr *CPEMI;
Bob Wilson549dda92009-10-15 05:52:29 +0000191 MachineBasicBlock *HighWaterMark;
Evan Chenga8e29892007-01-19 07:51:42 +0000192 unsigned MaxDisp;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000193 bool NegOk;
Evan Chengd3d9d662009-07-23 18:27:47 +0000194 bool IsSoImm;
195 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
196 bool neg, bool soimm)
Bob Wilson549dda92009-10-15 05:52:29 +0000197 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {
198 HighWaterMark = CPEMI->getParent();
199 }
Evan Chenga8e29892007-01-19 07:51:42 +0000200 };
Bob Wilson84945262009-05-12 17:09:30 +0000201
Evan Chenga8e29892007-01-19 07:51:42 +0000202 /// CPUsers - Keep track of all of the machine instructions that use various
203 /// constant pools and their max displacement.
Evan Chenge03cff62007-02-09 23:59:14 +0000204 std::vector<CPUser> CPUsers;
Bob Wilson84945262009-05-12 17:09:30 +0000205
Evan Chengc99ef082007-02-09 20:54:44 +0000206 /// CPEntry - One per constant pool entry, keeping the machine instruction
207 /// pointer, the constpool index, and the number of CPUser's which
208 /// reference this entry.
209 struct CPEntry {
210 MachineInstr *CPEMI;
211 unsigned CPI;
212 unsigned RefCount;
213 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
214 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
215 };
216
217 /// CPEntries - Keep track of all of the constant pool entry machine
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000218 /// instructions. For each original constpool index (i.e. those that
219 /// existed upon entry to this pass), it keeps a vector of entries.
220 /// Original elements are cloned as we go along; the clones are
221 /// put in the vector of the original element, but have distinct CPIs.
Evan Chengc99ef082007-02-09 20:54:44 +0000222 std::vector<std::vector<CPEntry> > CPEntries;
Bob Wilson84945262009-05-12 17:09:30 +0000223
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000224 /// ImmBranch - One per immediate branch, keeping the machine instruction
225 /// pointer, conditional or unconditional, the max displacement,
226 /// and (if isCond is true) the corresponding unconditional branch
227 /// opcode.
228 struct ImmBranch {
229 MachineInstr *MI;
Evan Chengc2854142007-01-25 23:18:59 +0000230 unsigned MaxDisp : 31;
231 bool isCond : 1;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000232 int UncondBr;
Evan Chengc2854142007-01-25 23:18:59 +0000233 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
234 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000235 };
236
Evan Cheng2706f972007-05-16 05:14:06 +0000237 /// ImmBranches - Keep track of all the immediate branch instructions.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000238 ///
Evan Chenge03cff62007-02-09 23:59:14 +0000239 std::vector<ImmBranch> ImmBranches;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000240
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000241 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
242 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000243 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000244
Evan Cheng5657c012009-07-29 02:18:14 +0000245 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
246 SmallVector<MachineInstr*, 4> T2JumpTables;
247
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000248 /// HasFarJump - True if any far jump instruction has been emitted during
249 /// the branch fix up pass.
250 bool HasFarJump;
251
Chris Lattner20628752010-07-22 21:27:00 +0000252 const ARMInstrInfo *TII;
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000253 const ARMSubtarget *STI;
Dale Johannesen8593e412007-04-29 19:19:30 +0000254 ARMFunctionInfo *AFI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000255 bool isThumb;
Evan Chengd3d9d662009-07-23 18:27:47 +0000256 bool isThumb1;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000257 bool isThumb2;
Evan Chenga8e29892007-01-19 07:51:42 +0000258 public:
Devang Patel19974732007-05-03 01:11:54 +0000259 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +0000260 ARMConstantIslands() : MachineFunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000261
Evan Cheng5657c012009-07-29 02:18:14 +0000262 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000263
264 virtual const char *getPassName() const {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000265 return "ARM constant island placement and branch shortening pass";
Evan Chenga8e29892007-01-19 07:51:42 +0000266 }
Bob Wilson84945262009-05-12 17:09:30 +0000267
Evan Chenga8e29892007-01-19 07:51:42 +0000268 private:
Evan Cheng5657c012009-07-29 02:18:14 +0000269 void DoInitialPlacement(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000270 std::vector<MachineInstr*> &CPEMIs);
Evan Chengc99ef082007-02-09 20:54:44 +0000271 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Jim Grosbach80697d12009-11-12 17:25:07 +0000272 void JumpTableFunctionScan(MachineFunction &MF);
Evan Cheng5657c012009-07-29 02:18:14 +0000273 void InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000274 const std::vector<MachineInstr*> &CPEMIs);
Evan Cheng0c615842007-01-31 02:22:22 +0000275 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Chenga8e29892007-01-19 07:51:42 +0000276 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +0000277 void AdjustBBOffsetsAfter(MachineBasicBlock *BB);
Evan Chenged884f32007-04-03 23:39:48 +0000278 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000279 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
Bob Wilsonb9239532009-10-15 20:49:47 +0000280 bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000281 void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
Bob Wilson757652c2009-10-12 21:39:43 +0000282 MachineBasicBlock *&NewMBB);
Evan Cheng5657c012009-07-29 02:18:14 +0000283 bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex);
Evan Chenged884f32007-04-03 23:39:48 +0000284 void RemoveDeadCPEMI(MachineInstr *CPEMI);
285 bool RemoveUnusedCPEntries();
Bob Wilson84945262009-05-12 17:09:30 +0000286 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000287 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
288 bool DoDump = false);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000289 bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000290 CPUser &U);
Evan Chengc0dbec72007-01-31 19:57:44 +0000291 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Cheng5657c012009-07-29 02:18:14 +0000292 bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br);
293 bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br);
294 bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000295 bool UndoLRSpillRestore();
Evan Chenga1efbbd2009-08-14 00:32:16 +0000296 bool OptimizeThumb2Instructions(MachineFunction &MF);
297 bool OptimizeThumb2Branches(MachineFunction &MF);
Jim Grosbach80697d12009-11-12 17:25:07 +0000298 bool ReorderThumb2JumpTables(MachineFunction &MF);
Evan Cheng5657c012009-07-29 02:18:14 +0000299 bool OptimizeThumb2JumpTables(MachineFunction &MF);
Jim Grosbach1fc7d712009-11-11 02:47:19 +0000300 MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB,
301 MachineBasicBlock *JTBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000302
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000303 void ComputeBlockSize(MachineBasicBlock *MBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000304 unsigned GetOffsetOf(MachineInstr *MI) const;
Dale Johannesen8593e412007-04-29 19:19:30 +0000305 void dumpBBs();
Evan Cheng5657c012009-07-29 02:18:14 +0000306 void verify(MachineFunction &MF);
Jakob Stoklund Olesen493ad6b2011-12-09 19:44:39 +0000307
308 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
309 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
310 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
311 const CPUser &U) {
312 return OffsetIsInRange(UserOffset, TrialOffset,
313 U.MaxDisp, U.NegOk, U.IsSoImm);
314 }
Evan Chenga8e29892007-01-19 07:51:42 +0000315 };
Devang Patel19974732007-05-03 01:11:54 +0000316 char ARMConstantIslands::ID = 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000317}
318
Dale Johannesen8593e412007-04-29 19:19:30 +0000319/// verify - check BBOffsets, BBSizes, alignment of islands
Evan Cheng5657c012009-07-29 02:18:14 +0000320void ARMConstantIslands::verify(MachineFunction &MF) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000321#ifndef NDEBUG
Evan Cheng5657c012009-07-29 02:18:14 +0000322 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chengd3d9d662009-07-23 18:27:47 +0000323 MBBI != E; ++MBBI) {
324 MachineBasicBlock *MBB = MBBI;
Jakob Stoklund Olesen99486be2011-12-08 01:10:05 +0000325 unsigned Align = MBB->getAlignment();
326 unsigned MBBId = MBB->getNumber();
327 assert(BBInfo[MBBId].Offset % (1u << Align) == 0);
328 assert(!MBBId || BBInfo[MBBId - 1].postOffset() <= BBInfo[MBBId].Offset);
Dale Johannesen8593e412007-04-29 19:19:30 +0000329 }
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000330 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
331 CPUser &U = CPUsers[i];
332 unsigned UserOffset = GetOffsetOf(U.MI) + (isThumb ? 4 : 8);
Jim Grosbacha9562562009-11-20 19:37:38 +0000333 unsigned CPEOffset = GetOffsetOf(U.CPEMI);
334 unsigned Disp = UserOffset < CPEOffset ? CPEOffset - UserOffset :
335 UserOffset - CPEOffset;
336 assert(Disp <= U.MaxDisp || "Constant pool entry out of range!");
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000337 }
Jim Grosbacha9562562009-11-20 19:37:38 +0000338#endif
Dale Johannesen8593e412007-04-29 19:19:30 +0000339}
340
341/// print block size and offset information - debugging
342void ARMConstantIslands::dumpBBs() {
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +0000343 DEBUG({
344 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
345 const BasicBlockInfo &BBI = BBInfo[J];
346 dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
347 << " kb=" << unsigned(BBI.KnownBits)
348 << " ua=" << unsigned(BBI.Unalign)
349 << " pa=" << unsigned(BBI.PostAlign)
350 << format(" size=%#x\n", BBInfo[J].Size);
351 }
352 });
Dale Johannesen8593e412007-04-29 19:19:30 +0000353}
354
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000355/// createARMConstantIslandPass - returns an instance of the constpool
356/// island pass.
Evan Chenga8e29892007-01-19 07:51:42 +0000357FunctionPass *llvm::createARMConstantIslandPass() {
358 return new ARMConstantIslands();
359}
360
Evan Cheng5657c012009-07-29 02:18:14 +0000361bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) {
362 MachineConstantPool &MCP = *MF.getConstantPool();
Bob Wilson84945262009-05-12 17:09:30 +0000363
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +0000364 DEBUG(dbgs() << "***** ARMConstantIslands: "
365 << MCP.getConstants().size() << " CP entries, aligned to "
366 << MCP.getConstantPoolAlignment() << " bytes *****\n");
367
Chris Lattner20628752010-07-22 21:27:00 +0000368 TII = (const ARMInstrInfo*)MF.getTarget().getInstrInfo();
Evan Cheng5657c012009-07-29 02:18:14 +0000369 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000370 STI = &MF.getTarget().getSubtarget<ARMSubtarget>();
371
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000372 isThumb = AFI->isThumbFunction();
Evan Chengd3d9d662009-07-23 18:27:47 +0000373 isThumb1 = AFI->isThumb1OnlyFunction();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000374 isThumb2 = AFI->isThumb2Function();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000375
376 HasFarJump = false;
377
Evan Chenga8e29892007-01-19 07:51:42 +0000378 // Renumber all of the machine basic blocks in the function, guaranteeing that
379 // the numbers agree with the position of the block in the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000380 MF.RenumberBlocks();
Evan Chenga8e29892007-01-19 07:51:42 +0000381
Jim Grosbach80697d12009-11-12 17:25:07 +0000382 // Try to reorder and otherwise adjust the block layout to make good use
383 // of the TB[BH] instructions.
384 bool MadeChange = false;
385 if (isThumb2 && AdjustJumpTableBlocks) {
386 JumpTableFunctionScan(MF);
387 MadeChange |= ReorderThumb2JumpTables(MF);
388 // Data is out of date, so clear it. It'll be re-computed later.
Jim Grosbach80697d12009-11-12 17:25:07 +0000389 T2JumpTables.clear();
390 // Blocks may have shifted around. Keep the numbering up to date.
391 MF.RenumberBlocks();
392 }
393
Evan Chengd26b14c2009-07-31 18:28:05 +0000394 // Thumb1 functions containing constant pools get 4-byte alignment.
Evan Chengd3d9d662009-07-23 18:27:47 +0000395 // This is so we can keep exact track of where the alignment padding goes.
396
Chris Lattner7d7dab02010-01-27 23:37:36 +0000397 // ARM and Thumb2 functions need to be 4-byte aligned.
398 if (!isThumb1)
399 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000400
Evan Chenga8e29892007-01-19 07:51:42 +0000401 // Perform the initial placement of the constant pool entries. To start with,
402 // we put them all at the end of the function.
Evan Chenge03cff62007-02-09 23:59:14 +0000403 std::vector<MachineInstr*> CPEMIs;
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000404 if (!MCP.isEmpty()) {
Evan Cheng5657c012009-07-29 02:18:14 +0000405 DoInitialPlacement(MF, CPEMIs);
Evan Chengd3d9d662009-07-23 18:27:47 +0000406 if (isThumb1)
Chris Lattner7d7dab02010-01-27 23:37:36 +0000407 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000408 }
Bob Wilson84945262009-05-12 17:09:30 +0000409
Evan Chenga8e29892007-01-19 07:51:42 +0000410 /// The next UID to take is the first unused one.
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000411 AFI->initPICLabelUId(CPEMIs.size());
Bob Wilson84945262009-05-12 17:09:30 +0000412
Evan Chenga8e29892007-01-19 07:51:42 +0000413 // Do the initial scan of the function, building up information about the
414 // sizes of each block, the location of all the water, and finding all of the
415 // constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000416 InitialFunctionScan(MF, CPEMIs);
Evan Chenga8e29892007-01-19 07:51:42 +0000417 CPEMIs.clear();
Dale Johannesen8086d582010-07-23 22:50:23 +0000418 DEBUG(dumpBBs());
419
Bob Wilson84945262009-05-12 17:09:30 +0000420
Evan Chenged884f32007-04-03 23:39:48 +0000421 /// Remove dead constant pool entries.
Bill Wendlingcd080242010-12-18 01:53:06 +0000422 MadeChange |= RemoveUnusedCPEntries();
Evan Chenged884f32007-04-03 23:39:48 +0000423
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000424 // Iteratively place constant pool entries and fix up branches until there
425 // is no change.
Evan Chengb6879b22009-08-07 07:35:21 +0000426 unsigned NoCPIters = 0, NoBRIters = 0;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000427 while (true) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000428 DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
Evan Chengb6879b22009-08-07 07:35:21 +0000429 bool CPChange = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000430 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Evan Chengb6879b22009-08-07 07:35:21 +0000431 CPChange |= HandleConstantPoolUser(MF, i);
432 if (CPChange && ++NoCPIters > 30)
433 llvm_unreachable("Constant Island pass failed to converge!");
Evan Cheng82020102007-07-10 22:00:16 +0000434 DEBUG(dumpBBs());
Jim Grosbach26b8ef52010-07-07 21:06:51 +0000435
Bob Wilsonb9239532009-10-15 20:49:47 +0000436 // Clear NewWaterList now. If we split a block for branches, it should
437 // appear as "new water" for the next iteration of constant pool placement.
438 NewWaterList.clear();
Evan Chengb6879b22009-08-07 07:35:21 +0000439
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000440 DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
Evan Chengb6879b22009-08-07 07:35:21 +0000441 bool BRChange = false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000442 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Chengb6879b22009-08-07 07:35:21 +0000443 BRChange |= FixUpImmediateBr(MF, ImmBranches[i]);
444 if (BRChange && ++NoBRIters > 30)
445 llvm_unreachable("Branch Fix Up pass failed to converge!");
Evan Cheng82020102007-07-10 22:00:16 +0000446 DEBUG(dumpBBs());
Evan Chengb6879b22009-08-07 07:35:21 +0000447
448 if (!CPChange && !BRChange)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000449 break;
450 MadeChange = true;
451 }
Evan Chenged884f32007-04-03 23:39:48 +0000452
Evan Chenga1efbbd2009-08-14 00:32:16 +0000453 // Shrink 32-bit Thumb2 branch, load, and store instructions.
Evan Chenge44be632010-08-09 18:35:19 +0000454 if (isThumb2 && !STI->prefers32BitThumb())
Evan Chenga1efbbd2009-08-14 00:32:16 +0000455 MadeChange |= OptimizeThumb2Instructions(MF);
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000456
Dale Johannesen8593e412007-04-29 19:19:30 +0000457 // After a while, this might be made debug-only, but it is not expensive.
Evan Cheng5657c012009-07-29 02:18:14 +0000458 verify(MF);
Dale Johannesen8593e412007-04-29 19:19:30 +0000459
Jim Grosbach26b8ef52010-07-07 21:06:51 +0000460 // If LR has been forced spilled and no far jump (i.e. BL) has been issued,
461 // undo the spill / restore of LR if possible.
Evan Cheng5657c012009-07-29 02:18:14 +0000462 if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000463 MadeChange |= UndoLRSpillRestore();
464
Anton Korobeynikov98b928e2011-01-30 22:07:39 +0000465 // Save the mapping between original and cloned constpool entries.
466 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
467 for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) {
468 const CPEntry & CPE = CPEntries[i][j];
469 AFI->recordCPEClone(i, CPE.CPI);
470 }
471 }
472
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000473 DEBUG(dbgs() << '\n'; dumpBBs());
Evan Chengb1c857b2010-07-22 02:09:47 +0000474
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000475 BBInfo.clear();
Evan Chenga8e29892007-01-19 07:51:42 +0000476 WaterList.clear();
477 CPUsers.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000478 CPEntries.clear();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000479 ImmBranches.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000480 PushPopMIs.clear();
Evan Cheng5657c012009-07-29 02:18:14 +0000481 T2JumpTables.clear();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000482
483 return MadeChange;
Evan Chenga8e29892007-01-19 07:51:42 +0000484}
485
486/// DoInitialPlacement - Perform the initial placement of the constant pool
487/// entries. To start with, we put them all at the end of the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000488void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +0000489 std::vector<MachineInstr*> &CPEMIs) {
Evan Chenga8e29892007-01-19 07:51:42 +0000490 // Create the basic block to hold the CPE's.
Evan Cheng5657c012009-07-29 02:18:14 +0000491 MachineBasicBlock *BB = MF.CreateMachineBasicBlock();
492 MF.push_back(BB);
Bob Wilson84945262009-05-12 17:09:30 +0000493
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000494 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
495 unsigned MaxAlign = Log2_32(MF.getConstantPool()->getConstantPoolAlignment());
496
497 // Mark the basic block as required by the const-pool.
498 // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
499 BB->setAlignment(AlignConstantIslands ? MaxAlign : 2);
500
501 // Order the entries in BB by descending alignment. That ensures correct
502 // alignment of all entries as long as BB is sufficiently aligned. Keep
503 // track of the insertion point for each alignment. We are going to bucket
504 // sort the entries as they are created.
505 SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end());
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +0000506
Evan Chenga8e29892007-01-19 07:51:42 +0000507 // Add all of the constants from the constant pool to the end block, use an
508 // identity mapping of CPI's to CPE's.
509 const std::vector<MachineConstantPoolEntry> &CPs =
Evan Cheng5657c012009-07-29 02:18:14 +0000510 MF.getConstantPool()->getConstants();
Bob Wilson84945262009-05-12 17:09:30 +0000511
Evan Cheng5657c012009-07-29 02:18:14 +0000512 const TargetData &TD = *MF.getTarget().getTargetData();
Evan Chenga8e29892007-01-19 07:51:42 +0000513 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
Duncan Sands777d2302009-05-09 07:06:46 +0000514 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000515 assert(Size >= 4 && "Too small constant pool entry");
516 unsigned Align = CPs[i].getAlignment();
517 assert(isPowerOf2_32(Align) && "Invalid alignment");
518 // Verify that all constant pool entries are a multiple of their alignment.
519 // If not, we would have to pad them out so that instructions stay aligned.
520 assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
521
522 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
523 unsigned LogAlign = Log2_32(Align);
524 MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
Evan Chenga8e29892007-01-19 07:51:42 +0000525 MachineInstr *CPEMI =
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000526 BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000527 .addImm(i).addConstantPoolIndex(i).addImm(Size);
Evan Chenga8e29892007-01-19 07:51:42 +0000528 CPEMIs.push_back(CPEMI);
Evan Chengc99ef082007-02-09 20:54:44 +0000529
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000530 // Ensure that future entries with higher alignment get inserted before
531 // CPEMI. This is bucket sort with iterators.
532 for (unsigned a = LogAlign + 1; a < MaxAlign; ++a)
533 if (InsPoint[a] == InsAt)
534 InsPoint[a] = CPEMI;
535
Evan Chengc99ef082007-02-09 20:54:44 +0000536 // Add a new CPEntry, but no corresponding CPUser yet.
537 std::vector<CPEntry> CPEs;
538 CPEs.push_back(CPEntry(CPEMI, i));
539 CPEntries.push_back(CPEs);
Dan Gohmanfe601042010-06-22 15:08:57 +0000540 ++NumCPEs;
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000541 DEBUG(dbgs() << "Moved CPI#" << i << " to end of function\n");
Evan Chenga8e29892007-01-19 07:51:42 +0000542 }
Jakob Stoklund Olesenb813f922011-12-12 16:49:37 +0000543 DEBUG(BB->dump());
Evan Chenga8e29892007-01-19 07:51:42 +0000544}
545
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000546/// BBHasFallthrough - Return true if the specified basic block can fallthrough
Evan Chenga8e29892007-01-19 07:51:42 +0000547/// into the block immediately after it.
548static bool BBHasFallthrough(MachineBasicBlock *MBB) {
549 // Get the next machine basic block in the function.
550 MachineFunction::iterator MBBI = MBB;
Jim Grosbach18f30e62010-06-02 21:53:11 +0000551 // Can't fall off end of function.
552 if (llvm::next(MBBI) == MBB->getParent()->end())
Evan Chenga8e29892007-01-19 07:51:42 +0000553 return false;
Bob Wilson84945262009-05-12 17:09:30 +0000554
Chris Lattner7896c9f2009-12-03 00:50:42 +0000555 MachineBasicBlock *NextBB = llvm::next(MBBI);
Evan Chenga8e29892007-01-19 07:51:42 +0000556 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
557 E = MBB->succ_end(); I != E; ++I)
558 if (*I == NextBB)
559 return true;
Bob Wilson84945262009-05-12 17:09:30 +0000560
Evan Chenga8e29892007-01-19 07:51:42 +0000561 return false;
562}
563
Evan Chengc99ef082007-02-09 20:54:44 +0000564/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
565/// look up the corresponding CPEntry.
566ARMConstantIslands::CPEntry
567*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
568 const MachineInstr *CPEMI) {
569 std::vector<CPEntry> &CPEs = CPEntries[CPI];
570 // Number of entries per constpool index should be small, just do a
571 // linear search.
572 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
573 if (CPEs[i].CPEMI == CPEMI)
574 return &CPEs[i];
575 }
576 return NULL;
577}
578
Jim Grosbach80697d12009-11-12 17:25:07 +0000579/// JumpTableFunctionScan - Do a scan of the function, building up
580/// information about the sizes of each block and the locations of all
581/// the jump tables.
582void ARMConstantIslands::JumpTableFunctionScan(MachineFunction &MF) {
Jim Grosbach80697d12009-11-12 17:25:07 +0000583 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
584 MBBI != E; ++MBBI) {
585 MachineBasicBlock &MBB = *MBBI;
586
Jim Grosbach80697d12009-11-12 17:25:07 +0000587 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Jim Grosbach08cbda52009-11-16 18:58:52 +0000588 I != E; ++I)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000589 if (I->isBranch() && I->getOpcode() == ARM::t2BR_JT)
Jim Grosbach08cbda52009-11-16 18:58:52 +0000590 T2JumpTables.push_back(I);
Jim Grosbach80697d12009-11-12 17:25:07 +0000591 }
592}
593
Evan Chenga8e29892007-01-19 07:51:42 +0000594/// InitialFunctionScan - Do the initial scan of the function, building up
595/// information about the sizes of each block, the location of all the water,
596/// and finding all of the constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000597void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000598 const std::vector<MachineInstr*> &CPEMIs) {
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000599 BBInfo.clear();
600 BBInfo.resize(MF.getNumBlockIDs());
601
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000602 // First thing, compute the size of all basic blocks, and see if the function
603 // has any inline assembly in it. If so, we have to be conservative about
604 // alignment assumptions, as we don't know for sure the size of any
605 // instructions in the inline assembly.
606 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
607 ComputeBlockSize(I);
608
609 // The known bits of the entry block offset are determined by the function
610 // alignment.
611 BBInfo.front().KnownBits = MF.getAlignment();
612
613 // Compute block offsets and known bits.
614 AdjustBBOffsetsAfter(MF.begin());
615
Bill Wendling9a4d2e42010-12-21 01:54:40 +0000616 // Now go back through the instructions and build up our data structures.
Evan Cheng5657c012009-07-29 02:18:14 +0000617 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chenga8e29892007-01-19 07:51:42 +0000618 MBBI != E; ++MBBI) {
619 MachineBasicBlock &MBB = *MBBI;
Bob Wilson84945262009-05-12 17:09:30 +0000620
Evan Chenga8e29892007-01-19 07:51:42 +0000621 // If this block doesn't fall through into the next MBB, then this is
622 // 'water' that a constant pool island could be placed.
623 if (!BBHasFallthrough(&MBB))
624 WaterList.push_back(&MBB);
Bob Wilson84945262009-05-12 17:09:30 +0000625
Evan Chenga8e29892007-01-19 07:51:42 +0000626 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
627 I != E; ++I) {
Jim Grosbach9cfcfeb2010-06-21 17:49:23 +0000628 if (I->isDebugValue())
629 continue;
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000630
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000631 int Opc = I->getOpcode();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000632 if (I->isBranch()) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000633 bool isCond = false;
634 unsigned Bits = 0;
635 unsigned Scale = 1;
636 int UOpc = Opc;
637 switch (Opc) {
Evan Cheng5657c012009-07-29 02:18:14 +0000638 default:
639 continue; // Ignore other JT branches
Evan Cheng5657c012009-07-29 02:18:14 +0000640 case ARM::t2BR_JT:
641 T2JumpTables.push_back(I);
642 continue; // Does not get an entry in ImmBranches
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000643 case ARM::Bcc:
644 isCond = true;
645 UOpc = ARM::B;
646 // Fallthrough
647 case ARM::B:
648 Bits = 24;
649 Scale = 4;
650 break;
651 case ARM::tBcc:
652 isCond = true;
653 UOpc = ARM::tB;
654 Bits = 8;
655 Scale = 2;
656 break;
657 case ARM::tB:
658 Bits = 11;
659 Scale = 2;
660 break;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000661 case ARM::t2Bcc:
662 isCond = true;
663 UOpc = ARM::t2B;
664 Bits = 20;
665 Scale = 2;
666 break;
667 case ARM::t2B:
668 Bits = 24;
669 Scale = 2;
670 break;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000671 }
Evan Chengb43216e2007-02-01 10:16:15 +0000672
673 // Record this immediate branch.
Evan Chengbd5d3db2007-02-03 02:08:34 +0000674 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengb43216e2007-02-01 10:16:15 +0000675 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000676 }
677
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000678 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
679 PushPopMIs.push_back(I);
680
Evan Chengd3d9d662009-07-23 18:27:47 +0000681 if (Opc == ARM::CONSTPOOL_ENTRY)
682 continue;
683
Evan Chenga8e29892007-01-19 07:51:42 +0000684 // Scan the instructions for constant pool operands.
685 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Dan Gohmand735b802008-10-03 15:45:36 +0000686 if (I->getOperand(op).isCPI()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000687 // We found one. The addressing mode tells us the max displacement
688 // from the PC that this instruction permits.
Bob Wilson84945262009-05-12 17:09:30 +0000689
Evan Chenga8e29892007-01-19 07:51:42 +0000690 // Basic size info comes from the TSFlags field.
Evan Chengb43216e2007-02-01 10:16:15 +0000691 unsigned Bits = 0;
692 unsigned Scale = 1;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000693 bool NegOk = false;
Evan Chengd3d9d662009-07-23 18:27:47 +0000694 bool IsSoImm = false;
695
696 switch (Opc) {
Bob Wilson84945262009-05-12 17:09:30 +0000697 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000698 llvm_unreachable("Unknown addressing mode for CP reference!");
Evan Chengd3d9d662009-07-23 18:27:47 +0000699 break;
700
701 // Taking the address of a CP entry.
702 case ARM::LEApcrel:
703 // This takes a SoImm, which is 8 bit immediate rotated. We'll
704 // pretend the maximum offset is 255 * 4. Since each instruction
Jim Grosbachdec6de92009-11-19 18:23:19 +0000705 // 4 byte wide, this is always correct. We'll check for other
Evan Chengd3d9d662009-07-23 18:27:47 +0000706 // displacements that fits in a SoImm as well.
Evan Chengb43216e2007-02-01 10:16:15 +0000707 Bits = 8;
Evan Chengd3d9d662009-07-23 18:27:47 +0000708 Scale = 4;
709 NegOk = true;
710 IsSoImm = true;
711 break;
Owen Anderson6b8719f2010-12-13 22:51:08 +0000712 case ARM::t2LEApcrel:
Evan Chengd3d9d662009-07-23 18:27:47 +0000713 Bits = 12;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000714 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000715 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000716 case ARM::tLEApcrel:
717 Bits = 8;
718 Scale = 4;
719 break;
720
Jim Grosbach3e556122010-10-26 22:37:02 +0000721 case ARM::LDRi12:
Evan Chengd3d9d662009-07-23 18:27:47 +0000722 case ARM::LDRcp:
Owen Anderson971b83b2011-02-08 22:39:40 +0000723 case ARM::t2LDRpci:
Evan Cheng556f33c2007-02-01 20:44:52 +0000724 Bits = 12; // +-offset_12
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000725 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000726 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000727
728 case ARM::tLDRpci:
Evan Chengb43216e2007-02-01 10:16:15 +0000729 Bits = 8;
730 Scale = 4; // +(offset_8*4)
Evan Cheng012f2d92007-01-24 08:53:17 +0000731 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000732
Jim Grosbache5165492009-11-09 00:11:35 +0000733 case ARM::VLDRD:
734 case ARM::VLDRS:
Evan Chengd3d9d662009-07-23 18:27:47 +0000735 Bits = 8;
736 Scale = 4; // +-(offset_8*4)
737 NegOk = true;
Evan Cheng055b0312009-06-29 07:51:04 +0000738 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000739 }
Evan Chengb43216e2007-02-01 10:16:15 +0000740
Evan Chenga8e29892007-01-19 07:51:42 +0000741 // Remember that this is a user of a CP entry.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000742 unsigned CPI = I->getOperand(op).getIndex();
Evan Chengc99ef082007-02-09 20:54:44 +0000743 MachineInstr *CPEMI = CPEMIs[CPI];
Evan Cheng31b99dd2009-08-14 18:31:44 +0000744 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
Evan Chengd3d9d662009-07-23 18:27:47 +0000745 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
Evan Chengc99ef082007-02-09 20:54:44 +0000746
747 // Increment corresponding CPEntry reference count.
748 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
749 assert(CPE && "Cannot find a corresponding CPEntry!");
750 CPE->RefCount++;
Bob Wilson84945262009-05-12 17:09:30 +0000751
Evan Chenga8e29892007-01-19 07:51:42 +0000752 // Instructions can only use one CP entry, don't bother scanning the
753 // rest of the operands.
754 break;
755 }
756 }
Evan Chenga8e29892007-01-19 07:51:42 +0000757 }
758}
759
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000760/// ComputeBlockSize - Compute the size and some alignment information for MBB.
761/// This function updates BBInfo directly.
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000762void ARMConstantIslands::ComputeBlockSize(MachineBasicBlock *MBB) {
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000763 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
764 BBI.Size = 0;
765 BBI.Unalign = 0;
766 BBI.PostAlign = 0;
767
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000768 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
769 ++I) {
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000770 BBI.Size += TII->GetInstSizeInBytes(I);
771 // For inline asm, GetInstSizeInBytes returns a conservative estimate.
772 // The actual size may be smaller, but still a multiple of the instr size.
Jakob Stoklund Olesene6f9e9d2011-12-08 01:22:39 +0000773 if (I->isInlineAsm())
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000774 BBI.Unalign = isThumb ? 1 : 2;
775 }
776
777 // tBR_JTr contains a .align 2 directive.
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000778 if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) {
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000779 BBI.PostAlign = 2;
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000780 MBB->getParent()->EnsureAlignment(2);
781 }
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000782}
783
Evan Chenga8e29892007-01-19 07:51:42 +0000784/// GetOffsetOf - Return the current offset of the specified machine instruction
785/// from the start of the function. This offset changes as stuff is moved
786/// around inside the function.
787unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
788 MachineBasicBlock *MBB = MI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000789
Evan Chenga8e29892007-01-19 07:51:42 +0000790 // The offset is composed of two things: the sum of the sizes of all MBB's
791 // before this instruction's block, and the offset from the start of the block
792 // it is in.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000793 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
Evan Chenga8e29892007-01-19 07:51:42 +0000794
795 // Sum instructions before MI in MBB.
796 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
797 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
798 if (&*I == MI) return Offset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000799 Offset += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000800 }
801}
802
803/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
804/// ID.
805static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
806 const MachineBasicBlock *RHS) {
807 return LHS->getNumber() < RHS->getNumber();
808}
809
810/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
811/// machine function, it upsets all of the block numbers. Renumber the blocks
812/// and update the arrays that parallel this numbering.
813void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
Duncan Sandsab4c3662011-02-15 09:23:02 +0000814 // Renumber the MBB's to keep them consecutive.
Evan Chenga8e29892007-01-19 07:51:42 +0000815 NewBB->getParent()->RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000816
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000817 // Insert an entry into BBInfo to align it properly with the (newly
Evan Chenga8e29892007-01-19 07:51:42 +0000818 // renumbered) block numbers.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000819 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Bob Wilson84945262009-05-12 17:09:30 +0000820
821 // Next, update WaterList. Specifically, we need to add NewMBB as having
Evan Chenga8e29892007-01-19 07:51:42 +0000822 // available water after it.
Bob Wilson034de5f2009-10-12 18:52:13 +0000823 water_iterator IP =
Evan Chenga8e29892007-01-19 07:51:42 +0000824 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
825 CompareMBBNumbers);
826 WaterList.insert(IP, NewBB);
827}
828
829
830/// Split the basic block containing MI into two blocks, which are joined by
Bob Wilsonb9239532009-10-15 20:49:47 +0000831/// an unconditional branch. Update data structures and renumber blocks to
Evan Cheng0c615842007-01-31 02:22:22 +0000832/// account for this change and returns the newly created block.
833MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Chenga8e29892007-01-19 07:51:42 +0000834 MachineBasicBlock *OrigBB = MI->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000835 MachineFunction &MF = *OrigBB->getParent();
Evan Chenga8e29892007-01-19 07:51:42 +0000836
837 // Create a new MBB for the code after the OrigBB.
Bob Wilson84945262009-05-12 17:09:30 +0000838 MachineBasicBlock *NewBB =
839 MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
Evan Chenga8e29892007-01-19 07:51:42 +0000840 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000841 MF.insert(MBBI, NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000842
Evan Chenga8e29892007-01-19 07:51:42 +0000843 // Splice the instructions starting with MI over to NewBB.
844 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
Bob Wilson84945262009-05-12 17:09:30 +0000845
Evan Chenga8e29892007-01-19 07:51:42 +0000846 // Add an unconditional branch from OrigBB to NewBB.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000847 // Note the new unconditional branch is not being recorded.
Dale Johannesenb6728402009-02-13 02:25:56 +0000848 // There doesn't seem to be meaningful DebugInfo available; this doesn't
849 // correspond to anything in the source.
Evan Cheng58541fd2009-07-07 01:16:41 +0000850 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000851 if (!isThumb)
852 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
853 else
854 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
855 .addImm(ARMCC::AL).addReg(0);
Dan Gohmanfe601042010-06-22 15:08:57 +0000856 ++NumSplit;
Bob Wilson84945262009-05-12 17:09:30 +0000857
Evan Chenga8e29892007-01-19 07:51:42 +0000858 // Update the CFG. All succs of OrigBB are now succs of NewBB.
Jakob Stoklund Olesene80fba02011-12-06 00:51:12 +0000859 NewBB->transferSuccessors(OrigBB);
Bob Wilson84945262009-05-12 17:09:30 +0000860
Evan Chenga8e29892007-01-19 07:51:42 +0000861 // OrigBB branches to NewBB.
862 OrigBB->addSuccessor(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000863
Evan Chenga8e29892007-01-19 07:51:42 +0000864 // Update internal data structures to account for the newly inserted MBB.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000865 // This is almost the same as UpdateForInsertedWaterBlock, except that
866 // the Water goes after OrigBB, not NewBB.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000867 MF.RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000868
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000869 // Insert an entry into BBInfo to align it properly with the (newly
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000870 // renumbered) block numbers.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000871 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Dale Johannesen99c49a42007-02-25 00:47:03 +0000872
Bob Wilson84945262009-05-12 17:09:30 +0000873 // Next, update WaterList. Specifically, we need to add OrigMBB as having
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000874 // available water after it (but not if it's already there, which happens
875 // when splitting before a conditional branch that is followed by an
876 // unconditional branch - in that case we want to insert NewBB).
Bob Wilson034de5f2009-10-12 18:52:13 +0000877 water_iterator IP =
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000878 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
879 CompareMBBNumbers);
880 MachineBasicBlock* WaterBB = *IP;
881 if (WaterBB == OrigBB)
Chris Lattner7896c9f2009-12-03 00:50:42 +0000882 WaterList.insert(llvm::next(IP), NewBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000883 else
884 WaterList.insert(IP, OrigBB);
Bob Wilsonb9239532009-10-15 20:49:47 +0000885 NewWaterList.insert(OrigBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000886
Dale Johannesen8086d582010-07-23 22:50:23 +0000887 // Figure out how large the OrigBB is. As the first half of the original
888 // block, it cannot contain a tablejump. The size includes
889 // the new jump we added. (It should be possible to do this without
890 // recounting everything, but it's very confusing, and this is rarely
891 // executed.)
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000892 ComputeBlockSize(OrigBB);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000893
Dale Johannesen8086d582010-07-23 22:50:23 +0000894 // Figure out how large the NewMBB is. As the second half of the original
895 // block, it may contain a tablejump.
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000896 ComputeBlockSize(NewBB);
Dale Johannesen8086d582010-07-23 22:50:23 +0000897
Dale Johannesen99c49a42007-02-25 00:47:03 +0000898 // All BBOffsets following these blocks must be modified.
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +0000899 AdjustBBOffsetsAfter(OrigBB);
Evan Cheng0c615842007-01-31 02:22:22 +0000900
901 return NewBB;
Evan Chenga8e29892007-01-19 07:51:42 +0000902}
903
Dale Johannesen8593e412007-04-29 19:19:30 +0000904/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
Bob Wilson84945262009-05-12 17:09:30 +0000905/// reference) is within MaxDisp of TrialOffset (a proposed location of a
Dale Johannesen8593e412007-04-29 19:19:30 +0000906/// constant pool entry).
Bob Wilson84945262009-05-12 17:09:30 +0000907bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000908 unsigned TrialOffset, unsigned MaxDisp,
909 bool NegativeOK, bool IsSoImm) {
Bob Wilson84945262009-05-12 17:09:30 +0000910 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
911 // purposes of the displacement computation; compensate for that here.
Dale Johannesen8593e412007-04-29 19:19:30 +0000912 // Effectively, the valid range of displacements is 2 bytes smaller for such
913 // references.
Evan Cheng31b99dd2009-08-14 18:31:44 +0000914 unsigned TotalAdj = 0;
915 if (isThumb && UserOffset%4 !=0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000916 UserOffset -= 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +0000917 TotalAdj = 2;
918 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000919 // CPEs will be rounded up to a multiple of 4.
Evan Cheng31b99dd2009-08-14 18:31:44 +0000920 if (isThumb && TrialOffset%4 != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000921 TrialOffset += 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +0000922 TotalAdj += 2;
923 }
924
925 // In Thumb2 mode, later branch adjustments can shift instructions up and
926 // cause alignment change. In the worst case scenario this can cause the
927 // user's effective address to be subtracted by 2 and the CPE's address to
928 // be plus 2.
929 if (isThumb2 && TotalAdj != 4)
930 MaxDisp -= (4 - TotalAdj);
Dale Johannesen8593e412007-04-29 19:19:30 +0000931
Dale Johannesen99c49a42007-02-25 00:47:03 +0000932 if (UserOffset <= TrialOffset) {
933 // User before the Trial.
Evan Chengd3d9d662009-07-23 18:27:47 +0000934 if (TrialOffset - UserOffset <= MaxDisp)
935 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000936 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000937 } else if (NegativeOK) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000938 if (UserOffset - TrialOffset <= MaxDisp)
939 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000940 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000941 }
942 return false;
943}
944
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000945/// WaterIsInRange - Returns true if a CPE placed after the specified
946/// Water (a basic block) will be in range for the specific MI.
947
948bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000949 MachineBasicBlock* Water, CPUser &U) {
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000950 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset();
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000951
Dale Johannesend959aa42007-04-02 20:31:06 +0000952 // If the CPE is to be inserted before the instruction, that will raise
Bob Wilsonaf4b7352009-10-12 22:49:05 +0000953 // the offset of the instruction.
Dale Johannesend959aa42007-04-02 20:31:06 +0000954 if (CPEOffset < UserOffset)
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000955 UserOffset += U.CPEMI->getOperand(2).getImm();
Dale Johannesend959aa42007-04-02 20:31:06 +0000956
Jakob Stoklund Olesen493ad6b2011-12-09 19:44:39 +0000957 return OffsetIsInRange(UserOffset, CPEOffset, U);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000958}
959
960/// CPEIsInRange - Returns true if the distance between specific MI and
Evan Chengc0dbec72007-01-31 19:57:44 +0000961/// specific ConstPool entry instruction can fit in MI's displacement field.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000962bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000963 MachineInstr *CPEMI, unsigned MaxDisp,
964 bool NegOk, bool DoDump) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000965 unsigned CPEOffset = GetOffsetOf(CPEMI);
Jakob Stoklund Olesene6f9e9d2011-12-08 01:22:39 +0000966 assert(CPEOffset % 4 == 0 && "Misaligned CPE");
Evan Cheng2021abe2007-02-01 01:09:47 +0000967
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000968 if (DoDump) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000969 DEBUG({
970 unsigned Block = MI->getParent()->getNumber();
971 const BasicBlockInfo &BBI = BBInfo[Block];
972 dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
973 << " max delta=" << MaxDisp
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +0000974 << format(" insn address=%#x", UserOffset)
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000975 << " in BB#" << Block << ": "
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +0000976 << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
977 << format("CPE address=%#x offset=%+d: ", CPEOffset,
978 int(CPEOffset-UserOffset));
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +0000979 });
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000980 }
Evan Chengc0dbec72007-01-31 19:57:44 +0000981
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000982 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
Evan Chengc0dbec72007-01-31 19:57:44 +0000983}
984
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000985#ifndef NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000986/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
987/// unconditionally branches to its only successor.
988static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
989 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
990 return false;
991
992 MachineBasicBlock *Succ = *MBB->succ_begin();
993 MachineBasicBlock *Pred = *MBB->pred_begin();
994 MachineInstr *PredMI = &Pred->back();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000995 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
996 || PredMI->getOpcode() == ARM::t2B)
Evan Chengc99ef082007-02-09 20:54:44 +0000997 return PredMI->getOperand(0).getMBB() == Succ;
998 return false;
999}
Evan Chengd1e7d9a2009-01-28 00:53:34 +00001000#endif // NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +00001001
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001002void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB) {
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +00001003 MachineFunction *MF = BB->getParent();
1004 for(unsigned i = BB->getNumber() + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
1005 // Get the offset and known bits at the end of the layout predecessor.
1006 unsigned Offset = BBInfo[i - 1].postOffset();
1007 unsigned KnownBits = BBInfo[i - 1].postKnownBits();
1008
1009 // Add padding before an aligned block. This may teach us more bits.
1010 if (unsigned Align = MF->getBlockNumbered(i)->getAlignment()) {
1011 Offset = WorstCaseAlign(Offset, Align, KnownBits);
1012 KnownBits = std::max(KnownBits, Align);
Dale Johannesen8593e412007-04-29 19:19:30 +00001013 }
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +00001014
1015 // This is where block i begins.
1016 BBInfo[i].Offset = Offset;
1017 BBInfo[i].KnownBits = KnownBits;
Dale Johannesen8593e412007-04-29 19:19:30 +00001018 }
Dale Johannesen99c49a42007-02-25 00:47:03 +00001019}
1020
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001021/// DecrementOldEntry - find the constant pool entry with index CPI
1022/// and instruction CPEMI, and decrement its refcount. If the refcount
Bob Wilson84945262009-05-12 17:09:30 +00001023/// becomes 0 remove the entry and instruction. Returns true if we removed
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001024/// the entry, false if we didn't.
Evan Chenga8e29892007-01-19 07:51:42 +00001025
Evan Chenged884f32007-04-03 23:39:48 +00001026bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
Evan Chengc99ef082007-02-09 20:54:44 +00001027 // Find the old entry. Eliminate it if it is no longer used.
Evan Chenged884f32007-04-03 23:39:48 +00001028 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1029 assert(CPE && "Unexpected!");
1030 if (--CPE->RefCount == 0) {
1031 RemoveDeadCPEMI(CPEMI);
1032 CPE->CPEMI = NULL;
Dan Gohmanfe601042010-06-22 15:08:57 +00001033 --NumCPEs;
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001034 return true;
1035 }
1036 return false;
1037}
1038
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001039/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1040/// if not, see if an in-range clone of the CPE is in range, and if so,
1041/// change the data structures so the user references the clone. Returns:
1042/// 0 = no existing entry found
1043/// 1 = entry found, and there were no code insertions or deletions
1044/// 2 = entry found, and there were code insertions or deletions
1045int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
1046{
1047 MachineInstr *UserMI = U.MI;
1048 MachineInstr *CPEMI = U.CPEMI;
1049
1050 // Check to see if the CPE is already in-range.
Evan Cheng5d8f1ca2009-07-21 23:56:01 +00001051 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001052 DEBUG(dbgs() << "In range\n");
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001053 return 1;
Evan Chengc99ef082007-02-09 20:54:44 +00001054 }
1055
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001056 // No. Look for previously created clones of the CPE that are in range.
Chris Lattner8aa797a2007-12-30 23:10:15 +00001057 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001058 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1059 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1060 // We already tried this one
1061 if (CPEs[i].CPEMI == CPEMI)
1062 continue;
1063 // Removing CPEs can leave empty entries, skip
1064 if (CPEs[i].CPEMI == NULL)
1065 continue;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +00001066 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001067 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
Chris Lattner893e1c92009-08-23 06:49:22 +00001068 << CPEs[i].CPI << "\n");
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001069 // Point the CPUser node to the replacement
1070 U.CPEMI = CPEs[i].CPEMI;
1071 // Change the CPI in the instruction operand to refer to the clone.
1072 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
Dan Gohmand735b802008-10-03 15:45:36 +00001073 if (UserMI->getOperand(j).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001074 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001075 break;
1076 }
1077 // Adjust the refcount of the clone...
1078 CPEs[i].RefCount++;
1079 // ...and the original. If we didn't remove the old entry, none of the
1080 // addresses changed, so we don't need another pass.
Evan Chenged884f32007-04-03 23:39:48 +00001081 return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001082 }
1083 }
1084 return 0;
1085}
1086
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001087/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1088/// the specific unconditional branch instruction.
1089static inline unsigned getUnconditionalBrDisp(int Opc) {
David Goodwin5e47a9a2009-06-30 18:04:13 +00001090 switch (Opc) {
1091 case ARM::tB:
1092 return ((1<<10)-1)*2;
1093 case ARM::t2B:
1094 return ((1<<23)-1)*2;
1095 default:
1096 break;
1097 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001098
David Goodwin5e47a9a2009-06-30 18:04:13 +00001099 return ((1<<23)-1)*4;
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001100}
1101
Bob Wilsonb9239532009-10-15 20:49:47 +00001102/// LookForWater - Look for an existing entry in the WaterList in which
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001103/// we can place the CPE referenced from U so it's within range of U's MI.
Bob Wilsonb9239532009-10-15 20:49:47 +00001104/// Returns true if found, false if not. If it returns true, WaterIter
Bob Wilsonf98032e2009-10-12 21:23:15 +00001105/// is set to the WaterList entry. For Thumb, prefer water that will not
1106/// introduce padding to water that will. To ensure that this pass
1107/// terminates, the CPE location for a particular CPUser is only allowed to
1108/// move to a lower address, so search backward from the end of the list and
1109/// prefer the first water that is in range.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001110bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
Bob Wilsonb9239532009-10-15 20:49:47 +00001111 water_iterator &WaterIter) {
Bob Wilson3b757352009-10-12 19:04:03 +00001112 if (WaterList.empty())
1113 return false;
1114
Bob Wilson32c50e82009-10-12 20:45:53 +00001115 bool FoundWaterThatWouldPad = false;
1116 water_iterator IPThatWouldPad;
Bob Wilson3b757352009-10-12 19:04:03 +00001117 for (water_iterator IP = prior(WaterList.end()),
1118 B = WaterList.begin();; --IP) {
1119 MachineBasicBlock* WaterBB = *IP;
Bob Wilsonb9239532009-10-15 20:49:47 +00001120 // Check if water is in range and is either at a lower address than the
1121 // current "high water mark" or a new water block that was created since
1122 // the previous iteration by inserting an unconditional branch. In the
1123 // latter case, we want to allow resetting the high water mark back to
1124 // this new water since we haven't seen it before. Inserting branches
1125 // should be relatively uncommon and when it does happen, we want to be
1126 // sure to take advantage of it for all the CPEs near that block, so that
1127 // we don't insert more branches than necessary.
1128 if (WaterIsInRange(UserOffset, WaterBB, U) &&
1129 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1130 NewWaterList.count(WaterBB))) {
Bob Wilson3b757352009-10-12 19:04:03 +00001131 unsigned WBBId = WaterBB->getNumber();
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +00001132 if (isThumb && BBInfo[WBBId].postOffset()%4 != 0) {
Bob Wilson3b757352009-10-12 19:04:03 +00001133 // This is valid Water, but would introduce padding. Remember
1134 // it in case we don't find any Water that doesn't do this.
Bob Wilson32c50e82009-10-12 20:45:53 +00001135 if (!FoundWaterThatWouldPad) {
1136 FoundWaterThatWouldPad = true;
Bob Wilson3b757352009-10-12 19:04:03 +00001137 IPThatWouldPad = IP;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001138 }
Bob Wilson3b757352009-10-12 19:04:03 +00001139 } else {
Bob Wilsonb9239532009-10-15 20:49:47 +00001140 WaterIter = IP;
Bob Wilson3b757352009-10-12 19:04:03 +00001141 return true;
Evan Chengd3d9d662009-07-23 18:27:47 +00001142 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001143 }
Bob Wilson3b757352009-10-12 19:04:03 +00001144 if (IP == B)
1145 break;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001146 }
Bob Wilson32c50e82009-10-12 20:45:53 +00001147 if (FoundWaterThatWouldPad) {
Bob Wilsonb9239532009-10-15 20:49:47 +00001148 WaterIter = IPThatWouldPad;
Dale Johannesen8593e412007-04-29 19:19:30 +00001149 return true;
1150 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001151 return false;
1152}
1153
Bob Wilson84945262009-05-12 17:09:30 +00001154/// CreateNewWater - No existing WaterList entry will work for
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001155/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1156/// block is used if in range, and the conditional branch munged so control
1157/// flow is correct. Otherwise the block is split to create a hole with an
Bob Wilson757652c2009-10-12 21:39:43 +00001158/// unconditional branch around it. In either case NewMBB is set to a
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001159/// block following which the new island can be inserted (the WaterList
1160/// is not adjusted).
Bob Wilson84945262009-05-12 17:09:30 +00001161void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
Bob Wilson757652c2009-10-12 21:39:43 +00001162 unsigned UserOffset,
1163 MachineBasicBlock *&NewMBB) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001164 CPUser &U = CPUsers[CPUserIndex];
1165 MachineInstr *UserMI = U.MI;
1166 MachineInstr *CPEMI = U.CPEMI;
1167 MachineBasicBlock *UserMBB = UserMI->getParent();
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +00001168 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
1169 unsigned OffsetOfNextBlock = UserBBI.postOffset();
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001170
Bob Wilson36fa5322009-10-15 05:10:36 +00001171 // If the block does not end in an unconditional branch already, and if the
1172 // end of the block is within range, make new water there. (The addition
1173 // below is for the unconditional branch we will be adding: 4 bytes on ARM +
1174 // Thumb2, 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
Dale Johannesen8593e412007-04-29 19:19:30 +00001175 // inside OffsetIsInRange.
Bob Wilson36fa5322009-10-15 05:10:36 +00001176 if (BBHasFallthrough(UserMBB) &&
Jakob Stoklund Olesen493ad6b2011-12-09 19:44:39 +00001177 OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4), U)) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001178 DEBUG(dbgs() << "Split at end of block\n");
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001179 if (&UserMBB->back() == UserMI)
1180 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
Chris Lattner7896c9f2009-12-03 00:50:42 +00001181 NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001182 // Add an unconditional branch from UserMBB to fallthrough block.
1183 // Record it for branch lengthening; this new branch will not get out of
1184 // range, but if the preceding conditional branch is out of range, the
1185 // targets will be exchanged, and the altered branch may be out of
1186 // range, so the machinery has to know about it.
David Goodwin5e47a9a2009-06-30 18:04:13 +00001187 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001188 if (!isThumb)
1189 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1190 else
1191 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
1192 .addImm(ARMCC::AL).addReg(0);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001193 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
Bob Wilson84945262009-05-12 17:09:30 +00001194 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001195 MaxDisp, false, UncondBr));
Evan Chengd3d9d662009-07-23 18:27:47 +00001196 int delta = isThumb1 ? 2 : 4;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001197 BBInfo[UserMBB->getNumber()].Size += delta;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001198 AdjustBBOffsetsAfter(UserMBB);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001199 } else {
1200 // What a big block. Find a place within the block to split it.
Evan Chengd3d9d662009-07-23 18:27:47 +00001201 // This is a little tricky on Thumb1 since instructions are 2 bytes
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001202 // and constant pool entries are 4 bytes: if instruction I references
1203 // island CPE, and instruction I+1 references CPE', it will
1204 // not work well to put CPE as far forward as possible, since then
1205 // CPE' cannot immediately follow it (that location is 2 bytes
1206 // farther away from I+1 than CPE was from I) and we'd need to create
Dale Johannesen8593e412007-04-29 19:19:30 +00001207 // a new island. So, we make a first guess, then walk through the
1208 // instructions between the one currently being looked at and the
1209 // possible insertion point, and make sure any other instructions
1210 // that reference CPEs will be able to use the same island area;
1211 // if not, we back up the insertion point.
1212
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +00001213 // Try to split the block so it's fully aligned. Compute the latest split
1214 // point where we can add a 4-byte branch instruction, and then
1215 // WorstCaseAlign to LogAlign.
1216 unsigned LogAlign = UserMBB->getParent()->getAlignment();
1217 unsigned KnownBits = UserBBI.internalKnownBits();
1218 unsigned UPad = UnknownPadding(LogAlign, KnownBits);
1219 unsigned BaseInsertOffset = UserOffset + U.MaxDisp;
1220 DEBUG(dbgs() << format("Split in middle of big block before %#x",
1221 BaseInsertOffset));
1222
1223 // Account for alignment and unknown padding.
1224 BaseInsertOffset &= ~((1u << LogAlign) - 1);
1225 BaseInsertOffset -= UPad;
1226
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001227 // The 4 in the following is for the unconditional branch we'll be
Evan Chengd3d9d662009-07-23 18:27:47 +00001228 // inserting (allows for long branch on Thumb1). Alignment of the
Dale Johannesen8593e412007-04-29 19:19:30 +00001229 // island is handled inside OffsetIsInRange.
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +00001230 BaseInsertOffset -= 4;
1231
1232 DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
1233 << " la=" << LogAlign
1234 << " kb=" << KnownBits
1235 << " up=" << UPad << '\n');
1236
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001237 // This could point off the end of the block if we've already got
1238 // constant pool entries following this block; only the last one is
1239 // in the water list. Back past any possible branches (allow for a
1240 // conditional and a maximally long unconditional).
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001241 if (BaseInsertOffset >= BBInfo[UserMBB->getNumber()+1].Offset)
1242 BaseInsertOffset = BBInfo[UserMBB->getNumber()+1].Offset -
Evan Chengd3d9d662009-07-23 18:27:47 +00001243 (isThumb1 ? 6 : 8);
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +00001244 unsigned EndInsertOffset =
1245 WorstCaseAlign(BaseInsertOffset + 4, LogAlign, KnownBits) +
1246 CPEMI->getOperand(2).getImm();
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001247 MachineBasicBlock::iterator MI = UserMI;
1248 ++MI;
1249 unsigned CPUIndex = CPUserIndex+1;
Evan Cheng719510a2010-08-12 20:30:05 +00001250 unsigned NumCPUsers = CPUsers.size();
1251 MachineInstr *LastIT = 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001252 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001253 Offset < BaseInsertOffset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001254 Offset += TII->GetInstSizeInBytes(MI),
Evan Cheng719510a2010-08-12 20:30:05 +00001255 MI = llvm::next(MI)) {
1256 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001257 CPUser &U = CPUsers[CPUIndex];
Jakob Stoklund Olesen493ad6b2011-12-09 19:44:39 +00001258 if (!OffsetIsInRange(Offset, EndInsertOffset, U)) {
Jakob Stoklund Olesen77caaf02011-12-10 02:55:10 +00001259 BaseInsertOffset -= 1u << LogAlign;
1260 EndInsertOffset -= 1u << LogAlign;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001261 }
1262 // This is overly conservative, as we don't account for CPEMIs
1263 // being reused within the block, but it doesn't matter much.
1264 EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1265 CPUIndex++;
1266 }
Evan Cheng719510a2010-08-12 20:30:05 +00001267
1268 // Remember the last IT instruction.
1269 if (MI->getOpcode() == ARM::t2IT)
1270 LastIT = MI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001271 }
Evan Cheng719510a2010-08-12 20:30:05 +00001272
Evan Cheng719510a2010-08-12 20:30:05 +00001273 --MI;
1274
1275 // Avoid splitting an IT block.
1276 if (LastIT) {
1277 unsigned PredReg = 0;
1278 ARMCC::CondCodes CC = llvm::getITInstrPredicate(MI, PredReg);
1279 if (CC != ARMCC::AL)
1280 MI = LastIT;
1281 }
1282 NewMBB = SplitBlockBeforeInstr(MI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001283 }
1284}
1285
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001286/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
Bob Wilson39bf0512009-05-12 17:35:29 +00001287/// is out-of-range. If so, pick up the constant pool value and move it some
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001288/// place in-range. Return true if we changed any addresses (thus must run
1289/// another pass of branch lengthening), false otherwise.
Evan Cheng5657c012009-07-29 02:18:14 +00001290bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +00001291 unsigned CPUserIndex) {
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001292 CPUser &U = CPUsers[CPUserIndex];
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001293 MachineInstr *UserMI = U.MI;
1294 MachineInstr *CPEMI = U.CPEMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001295 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001296 unsigned Size = CPEMI->getOperand(2).getImm();
Dale Johannesen8593e412007-04-29 19:19:30 +00001297 // Compute this only once, it's expensive. The 4 or 8 is the value the
Evan Chenga1efbbd2009-08-14 00:32:16 +00001298 // hardware keeps in the PC.
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001299 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
Evan Cheng768c9f72007-04-27 08:14:15 +00001300
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001301 // See if the current entry is within range, or there is a clone of it
1302 // in range.
1303 int result = LookForExistingCPEntry(U, UserOffset);
1304 if (result==1) return false;
1305 else if (result==2) return true;
1306
1307 // No existing clone of this CPE is within range.
1308 // We will be generating a new clone. Get a UID for it.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001309 unsigned ID = AFI->createPICLabelUId();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001310
Bob Wilsonf98032e2009-10-12 21:23:15 +00001311 // Look for water where we can place this CPE.
Bob Wilsonb9239532009-10-15 20:49:47 +00001312 MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock();
1313 MachineBasicBlock *NewMBB;
1314 water_iterator IP;
1315 if (LookForWater(U, UserOffset, IP)) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001316 DEBUG(dbgs() << "Found water in range\n");
Bob Wilsonb9239532009-10-15 20:49:47 +00001317 MachineBasicBlock *WaterBB = *IP;
1318
1319 // If the original WaterList entry was "new water" on this iteration,
1320 // propagate that to the new island. This is just keeping NewWaterList
1321 // updated to match the WaterList, which will be updated below.
1322 if (NewWaterList.count(WaterBB)) {
1323 NewWaterList.erase(WaterBB);
1324 NewWaterList.insert(NewIsland);
1325 }
1326 // The new CPE goes before the following block (NewMBB).
Chris Lattner7896c9f2009-12-03 00:50:42 +00001327 NewMBB = llvm::next(MachineFunction::iterator(WaterBB));
Bob Wilsonb9239532009-10-15 20:49:47 +00001328
1329 } else {
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001330 // No water found.
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001331 DEBUG(dbgs() << "No water found\n");
Bob Wilson757652c2009-10-12 21:39:43 +00001332 CreateNewWater(CPUserIndex, UserOffset, NewMBB);
Bob Wilsonb9239532009-10-15 20:49:47 +00001333
1334 // SplitBlockBeforeInstr adds to WaterList, which is important when it is
1335 // called while handling branches so that the water will be seen on the
1336 // next iteration for constant pools, but in this context, we don't want
1337 // it. Check for this so it will be removed from the WaterList.
1338 // Also remove any entry from NewWaterList.
1339 MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB));
1340 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1341 if (IP != WaterList.end())
1342 NewWaterList.erase(WaterBB);
1343
1344 // We are adding new water. Update NewWaterList.
1345 NewWaterList.insert(NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001346 }
1347
Bob Wilsonb9239532009-10-15 20:49:47 +00001348 // Remove the original WaterList entry; we want subsequent insertions in
1349 // this vicinity to go after the one we're about to insert. This
1350 // considerably reduces the number of times we have to move the same CPE
1351 // more than once and is also important to ensure the algorithm terminates.
1352 if (IP != WaterList.end())
1353 WaterList.erase(IP);
1354
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001355 // Okay, we know we can put an island before NewMBB now, do it!
Evan Cheng5657c012009-07-29 02:18:14 +00001356 MF.insert(NewMBB, NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001357
1358 // Update internal data structures to account for the newly inserted MBB.
1359 UpdateForInsertedWaterBlock(NewIsland);
1360
1361 // Decrement the old entry, and remove it if refcount becomes 0.
Evan Chenged884f32007-04-03 23:39:48 +00001362 DecrementOldEntry(CPI, CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001363
1364 // Now that we have an island to add the CPE to, clone the original CPE and
1365 // add it to the island.
Bob Wilson549dda92009-10-15 05:52:29 +00001366 U.HighWaterMark = NewIsland;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001367 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +00001368 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001369 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
Dan Gohmanfe601042010-06-22 15:08:57 +00001370 ++NumCPEs;
Evan Chengc99ef082007-02-09 20:54:44 +00001371
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +00001372 // Mark the basic block as 4-byte aligned as required by the const-pool entry.
1373 NewIsland->setAlignment(2);
1374
Evan Chenga8e29892007-01-19 07:51:42 +00001375 // Increase the size of the island block to account for the new entry.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001376 BBInfo[NewIsland->getNumber()].Size += Size;
Jakob Stoklund Olesen540c6d92011-12-08 00:55:02 +00001377 AdjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland)));
Bob Wilson84945262009-05-12 17:09:30 +00001378
Evan Chenga8e29892007-01-19 07:51:42 +00001379 // Finally, change the CPI in the instruction operand to be ID.
1380 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +00001381 if (UserMI->getOperand(i).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001382 UserMI->getOperand(i).setIndex(ID);
Evan Chenga8e29892007-01-19 07:51:42 +00001383 break;
1384 }
Bob Wilson84945262009-05-12 17:09:30 +00001385
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001386 DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
Jakob Stoklund Olesen2d5023b2011-12-10 02:55:06 +00001387 << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
Bob Wilson84945262009-05-12 17:09:30 +00001388
Evan Chenga8e29892007-01-19 07:51:42 +00001389 return true;
1390}
1391
Evan Chenged884f32007-04-03 23:39:48 +00001392/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1393/// sizes and offsets of impacted basic blocks.
1394void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1395 MachineBasicBlock *CPEBB = CPEMI->getParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001396 unsigned Size = CPEMI->getOperand(2).getImm();
1397 CPEMI->eraseFromParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001398 BBInfo[CPEBB->getNumber()].Size -= Size;
Dale Johannesen8593e412007-04-29 19:19:30 +00001399 // All succeeding offsets have the current size value added in, fix this.
Evan Chenged884f32007-04-03 23:39:48 +00001400 if (CPEBB->empty()) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001401 // In thumb1 mode, the size of island may be padded by two to compensate for
Dale Johannesen8593e412007-04-29 19:19:30 +00001402 // the alignment requirement. Then it will now be 2 when the block is
Evan Chenged884f32007-04-03 23:39:48 +00001403 // empty, so fix this.
1404 // All succeeding offsets have the current size value added in, fix this.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001405 if (BBInfo[CPEBB->getNumber()].Size != 0) {
1406 Size += BBInfo[CPEBB->getNumber()].Size;
1407 BBInfo[CPEBB->getNumber()].Size = 0;
Evan Chenged884f32007-04-03 23:39:48 +00001408 }
Jakob Stoklund Olesen305e5fe2011-12-06 21:55:35 +00001409
1410 // This block no longer needs to be aligned. <rdar://problem/10534709>.
1411 CPEBB->setAlignment(0);
Evan Chenged884f32007-04-03 23:39:48 +00001412 }
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001413 AdjustBBOffsetsAfter(CPEBB);
Dale Johannesen8593e412007-04-29 19:19:30 +00001414 // An island has only one predecessor BB and one successor BB. Check if
1415 // this BB's predecessor jumps directly to this BB's successor. This
1416 // shouldn't happen currently.
1417 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1418 // FIXME: remove the empty blocks after all the work is done?
Evan Chenged884f32007-04-03 23:39:48 +00001419}
1420
1421/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1422/// are zero.
1423bool ARMConstantIslands::RemoveUnusedCPEntries() {
1424 unsigned MadeChange = false;
1425 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1426 std::vector<CPEntry> &CPEs = CPEntries[i];
1427 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1428 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1429 RemoveDeadCPEMI(CPEs[j].CPEMI);
1430 CPEs[j].CPEMI = NULL;
1431 MadeChange = true;
1432 }
1433 }
Bob Wilson84945262009-05-12 17:09:30 +00001434 }
Evan Chenged884f32007-04-03 23:39:48 +00001435 return MadeChange;
1436}
1437
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001438/// BBIsInRange - Returns true if the distance between specific MI and
Evan Cheng43aeab62007-01-26 20:38:26 +00001439/// specific BB can fit in MI's displacement field.
Evan Chengc0dbec72007-01-31 19:57:44 +00001440bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1441 unsigned MaxDisp) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001442 unsigned PCAdj = isThumb ? 4 : 8;
Evan Chengc0dbec72007-01-31 19:57:44 +00001443 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001444 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Cheng43aeab62007-01-26 20:38:26 +00001445
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001446 DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
Chris Lattner705e07f2009-08-23 03:41:05 +00001447 << " from BB#" << MI->getParent()->getNumber()
1448 << " max delta=" << MaxDisp
1449 << " from " << GetOffsetOf(MI) << " to " << DestOffset
1450 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
Evan Chengc0dbec72007-01-31 19:57:44 +00001451
Dale Johannesen8593e412007-04-29 19:19:30 +00001452 if (BrOffset <= DestOffset) {
1453 // Branch before the Dest.
1454 if (DestOffset-BrOffset <= MaxDisp)
1455 return true;
1456 } else {
1457 if (BrOffset-DestOffset <= MaxDisp)
1458 return true;
1459 }
1460 return false;
Evan Cheng43aeab62007-01-26 20:38:26 +00001461}
1462
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001463/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1464/// away to fit in its displacement field.
Evan Cheng5657c012009-07-29 02:18:14 +00001465bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001466 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001467 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001468
Evan Chengc0dbec72007-01-31 19:57:44 +00001469 // Check to see if the DestBB is already in-range.
1470 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng43aeab62007-01-26 20:38:26 +00001471 return false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001472
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001473 if (!Br.isCond)
Evan Cheng5657c012009-07-29 02:18:14 +00001474 return FixUpUnconditionalBr(MF, Br);
1475 return FixUpConditionalBr(MF, Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001476}
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001477
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001478/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1479/// too far away to fit in its displacement field. If the LR register has been
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001480/// spilled in the epilogue, then we can use BL to implement a far jump.
Bob Wilson39bf0512009-05-12 17:35:29 +00001481/// Otherwise, add an intermediate branch instruction to a branch.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001482bool
Evan Cheng5657c012009-07-29 02:18:14 +00001483ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001484 MachineInstr *MI = Br.MI;
1485 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng53c67c02009-08-07 05:45:07 +00001486 if (!isThumb1)
1487 llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!");
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001488
1489 // Use BL to implement far jump.
1490 Br.MaxDisp = (1 << 21) * 2;
Chris Lattner5080f4d2008-01-11 18:10:50 +00001491 MI->setDesc(TII->get(ARM::tBfar));
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001492 BBInfo[MBB->getNumber()].Size += 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001493 AdjustBBOffsetsAfter(MBB);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001494 HasFarJump = true;
Dan Gohmanfe601042010-06-22 15:08:57 +00001495 ++NumUBrFixed;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001496
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001497 DEBUG(dbgs() << " Changed B to long jump " << *MI);
Evan Chengbd5d3db2007-02-03 02:08:34 +00001498
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001499 return true;
1500}
1501
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001502/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001503/// far away to fit in its displacement field. It is converted to an inverse
1504/// conditional branch + an unconditional branch to the destination.
1505bool
Evan Cheng5657c012009-07-29 02:18:14 +00001506ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001507 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001508 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001509
Bob Wilson39bf0512009-05-12 17:35:29 +00001510 // Add an unconditional branch to the destination and invert the branch
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001511 // condition to jump over it:
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001512 // blt L1
1513 // =>
1514 // bge L2
1515 // b L1
1516 // L2:
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001517 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001518 CC = ARMCC::getOppositeCondition(CC);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001519 unsigned CCReg = MI->getOperand(2).getReg();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001520
1521 // If the branch is at the end of its MBB and that has a fall-through block,
1522 // direct the updated conditional branch to the fall-through block. Otherwise,
1523 // split the MBB before the next instruction.
1524 MachineBasicBlock *MBB = MI->getParent();
Evan Chengbd5d3db2007-02-03 02:08:34 +00001525 MachineInstr *BMI = &MBB->back();
1526 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng43aeab62007-01-26 20:38:26 +00001527
Dan Gohmanfe601042010-06-22 15:08:57 +00001528 ++NumCBrFixed;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001529 if (BMI != MI) {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001530 if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
Evan Chengbd5d3db2007-02-03 02:08:34 +00001531 BMI->getOpcode() == Br.UncondBr) {
Bob Wilson39bf0512009-05-12 17:35:29 +00001532 // Last MI in the BB is an unconditional branch. Can we simply invert the
Evan Cheng43aeab62007-01-26 20:38:26 +00001533 // condition and swap destinations:
1534 // beq L1
1535 // b L2
1536 // =>
1537 // bne L2
1538 // b L1
Chris Lattner8aa797a2007-12-30 23:10:15 +00001539 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
Evan Chengc0dbec72007-01-31 19:57:44 +00001540 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001541 DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
Chris Lattner705e07f2009-08-23 03:41:05 +00001542 << *BMI);
Chris Lattner8aa797a2007-12-30 23:10:15 +00001543 BMI->getOperand(0).setMBB(DestBB);
1544 MI->getOperand(0).setMBB(NewDest);
Evan Cheng43aeab62007-01-26 20:38:26 +00001545 MI->getOperand(1).setImm(CC);
1546 return true;
1547 }
1548 }
1549 }
1550
1551 if (NeedSplit) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001552 SplitBlockBeforeInstr(MI);
Bob Wilson39bf0512009-05-12 17:35:29 +00001553 // No need for the branch to the next block. We're adding an unconditional
Evan Chengdd353b82007-01-26 02:02:39 +00001554 // branch to the destination.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001555 int delta = TII->GetInstSizeInBytes(&MBB->back());
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001556 BBInfo[MBB->getNumber()].Size -= delta;
Evan Chengdd353b82007-01-26 02:02:39 +00001557 MBB->back().eraseFromParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001558 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
Evan Chengdd353b82007-01-26 02:02:39 +00001559 }
Chris Lattner7896c9f2009-12-03 00:50:42 +00001560 MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB));
Bob Wilson84945262009-05-12 17:09:30 +00001561
Jakob Stoklund Olesen3c4615e2011-12-09 18:20:35 +00001562 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
Chris Lattner893e1c92009-08-23 06:49:22 +00001563 << " also invert condition and change dest. to BB#"
1564 << NextBB->getNumber() << "\n");
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001565
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001566 // Insert a new conditional branch and a new unconditional branch.
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001567 // Also update the ImmBranch as well as adding a new entry for the new branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001568 BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
Dale Johannesenb6728402009-02-13 02:25:56 +00001569 .addMBB(NextBB).addImm(CC).addReg(CCReg);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001570 Br.MI = &MBB->back();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001571 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Owen Andersoncd4338f2011-09-09 23:05:14 +00001572 if (isThumb)
1573 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
1574 .addImm(ARMCC::AL).addReg(0);
1575 else
1576 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001577 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Evan Chenga9b8b8d2007-01-31 18:29:27 +00001578 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Chenga0bf7942007-01-25 23:31:04 +00001579 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001580
1581 // Remove the old conditional branch. It may or may not still be in MBB.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001582 BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001583 MI->eraseFromParent();
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001584 AdjustBBOffsetsAfter(MBB);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001585 return true;
1586}
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001587
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001588/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
Evan Cheng4b322e52009-08-11 21:11:32 +00001589/// LR / restores LR to pc. FIXME: This is done here because it's only possible
1590/// to do this if tBfar is not used.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001591bool ARMConstantIslands::UndoLRSpillRestore() {
1592 bool MadeChange = false;
1593 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1594 MachineInstr *MI = PushPopMIs[i];
Bob Wilson815baeb2010-03-13 01:08:20 +00001595 // First two operands are predicates.
Evan Cheng44bec522007-05-15 01:29:07 +00001596 if (MI->getOpcode() == ARM::tPOP_RET &&
Bob Wilson815baeb2010-03-13 01:08:20 +00001597 MI->getOperand(2).getReg() == ARM::PC &&
1598 MI->getNumExplicitOperands() == 3) {
Jim Grosbach25e6d482011-07-08 21:50:04 +00001599 // Create the new insn and copy the predicate from the old.
1600 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET))
1601 .addOperand(MI->getOperand(0))
1602 .addOperand(MI->getOperand(1));
Evan Cheng44bec522007-05-15 01:29:07 +00001603 MI->eraseFromParent();
1604 MadeChange = true;
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001605 }
1606 }
1607 return MadeChange;
1608}
Evan Cheng5657c012009-07-29 02:18:14 +00001609
Evan Chenga1efbbd2009-08-14 00:32:16 +00001610bool ARMConstantIslands::OptimizeThumb2Instructions(MachineFunction &MF) {
1611 bool MadeChange = false;
1612
1613 // Shrink ADR and LDR from constantpool.
1614 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
1615 CPUser &U = CPUsers[i];
1616 unsigned Opcode = U.MI->getOpcode();
1617 unsigned NewOpc = 0;
1618 unsigned Scale = 1;
1619 unsigned Bits = 0;
1620 switch (Opcode) {
1621 default: break;
Owen Anderson6b8719f2010-12-13 22:51:08 +00001622 case ARM::t2LEApcrel:
Evan Chenga1efbbd2009-08-14 00:32:16 +00001623 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1624 NewOpc = ARM::tLEApcrel;
1625 Bits = 8;
1626 Scale = 4;
1627 }
1628 break;
1629 case ARM::t2LDRpci:
1630 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1631 NewOpc = ARM::tLDRpci;
1632 Bits = 8;
1633 Scale = 4;
1634 }
1635 break;
1636 }
1637
1638 if (!NewOpc)
1639 continue;
1640
1641 unsigned UserOffset = GetOffsetOf(U.MI) + 4;
1642 unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
1643 // FIXME: Check if offset is multiple of scale if scale is not 4.
1644 if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
1645 U.MI->setDesc(TII->get(NewOpc));
1646 MachineBasicBlock *MBB = U.MI->getParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001647 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001648 AdjustBBOffsetsAfter(MBB);
Evan Chenga1efbbd2009-08-14 00:32:16 +00001649 ++NumT2CPShrunk;
1650 MadeChange = true;
1651 }
1652 }
1653
Evan Chenga1efbbd2009-08-14 00:32:16 +00001654 MadeChange |= OptimizeThumb2Branches(MF);
Jim Grosbach01dec0e2009-11-12 03:28:35 +00001655 MadeChange |= OptimizeThumb2JumpTables(MF);
Evan Chenga1efbbd2009-08-14 00:32:16 +00001656 return MadeChange;
1657}
1658
1659bool ARMConstantIslands::OptimizeThumb2Branches(MachineFunction &MF) {
Evan Cheng31b99dd2009-08-14 18:31:44 +00001660 bool MadeChange = false;
1661
1662 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
1663 ImmBranch &Br = ImmBranches[i];
1664 unsigned Opcode = Br.MI->getOpcode();
1665 unsigned NewOpc = 0;
1666 unsigned Scale = 1;
1667 unsigned Bits = 0;
1668 switch (Opcode) {
1669 default: break;
1670 case ARM::t2B:
1671 NewOpc = ARM::tB;
1672 Bits = 11;
1673 Scale = 2;
1674 break;
Evan Chengde17fb62009-10-31 23:46:45 +00001675 case ARM::t2Bcc: {
Evan Cheng31b99dd2009-08-14 18:31:44 +00001676 NewOpc = ARM::tBcc;
1677 Bits = 8;
Evan Chengde17fb62009-10-31 23:46:45 +00001678 Scale = 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +00001679 break;
1680 }
Evan Chengde17fb62009-10-31 23:46:45 +00001681 }
1682 if (NewOpc) {
1683 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
1684 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
1685 if (BBIsInRange(Br.MI, DestBB, MaxOffs)) {
1686 Br.MI->setDesc(TII->get(NewOpc));
1687 MachineBasicBlock *MBB = Br.MI->getParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001688 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001689 AdjustBBOffsetsAfter(MBB);
Evan Chengde17fb62009-10-31 23:46:45 +00001690 ++NumT2BrShrunk;
1691 MadeChange = true;
1692 }
1693 }
1694
1695 Opcode = Br.MI->getOpcode();
1696 if (Opcode != ARM::tBcc)
Evan Cheng31b99dd2009-08-14 18:31:44 +00001697 continue;
1698
Evan Chengde17fb62009-10-31 23:46:45 +00001699 NewOpc = 0;
1700 unsigned PredReg = 0;
1701 ARMCC::CondCodes Pred = llvm::getInstrPredicate(Br.MI, PredReg);
1702 if (Pred == ARMCC::EQ)
1703 NewOpc = ARM::tCBZ;
1704 else if (Pred == ARMCC::NE)
1705 NewOpc = ARM::tCBNZ;
1706 if (!NewOpc)
1707 continue;
Evan Cheng31b99dd2009-08-14 18:31:44 +00001708 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
Evan Chengde17fb62009-10-31 23:46:45 +00001709 // Check if the distance is within 126. Subtract starting offset by 2
1710 // because the cmp will be eliminated.
1711 unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001712 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Chengde17fb62009-10-31 23:46:45 +00001713 if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
Evan Cheng0539c152011-04-01 22:09:28 +00001714 MachineBasicBlock::iterator CmpMI = Br.MI;
1715 if (CmpMI != Br.MI->getParent()->begin()) {
1716 --CmpMI;
1717 if (CmpMI->getOpcode() == ARM::tCMPi8) {
1718 unsigned Reg = CmpMI->getOperand(0).getReg();
1719 Pred = llvm::getInstrPredicate(CmpMI, PredReg);
1720 if (Pred == ARMCC::AL &&
1721 CmpMI->getOperand(1).getImm() == 0 &&
1722 isARMLowRegister(Reg)) {
1723 MachineBasicBlock *MBB = Br.MI->getParent();
1724 MachineInstr *NewBR =
1725 BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc))
1726 .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags());
1727 CmpMI->eraseFromParent();
1728 Br.MI->eraseFromParent();
1729 Br.MI = NewBR;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001730 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001731 AdjustBBOffsetsAfter(MBB);
Evan Cheng0539c152011-04-01 22:09:28 +00001732 ++NumCBZ;
1733 MadeChange = true;
1734 }
Evan Chengde17fb62009-10-31 23:46:45 +00001735 }
1736 }
Evan Cheng31b99dd2009-08-14 18:31:44 +00001737 }
1738 }
1739
1740 return MadeChange;
Evan Chenga1efbbd2009-08-14 00:32:16 +00001741}
1742
Evan Chenga1efbbd2009-08-14 00:32:16 +00001743/// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
1744/// jumptables when it's possible.
Evan Cheng5657c012009-07-29 02:18:14 +00001745bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) {
1746 bool MadeChange = false;
1747
1748 // FIXME: After the tables are shrunk, can we get rid some of the
1749 // constantpool tables?
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001750 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +00001751 if (MJTI == 0) return false;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001752
Evan Cheng5657c012009-07-29 02:18:14 +00001753 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1754 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1755 MachineInstr *MI = T2JumpTables[i];
Evan Chenge837dea2011-06-28 19:10:37 +00001756 const MCInstrDesc &MCID = MI->getDesc();
1757 unsigned NumOps = MCID.getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001758 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
Evan Cheng5657c012009-07-29 02:18:14 +00001759 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1760 unsigned JTI = JTOP.getIndex();
1761 assert(JTI < JT.size());
1762
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001763 bool ByteOk = true;
1764 bool HalfWordOk = true;
Jim Grosbach80697d12009-11-12 17:25:07 +00001765 unsigned JTOffset = GetOffsetOf(MI) + 4;
1766 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Evan Cheng5657c012009-07-29 02:18:14 +00001767 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1768 MachineBasicBlock *MBB = JTBBs[j];
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001769 unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
Evan Cheng8770f742009-07-29 23:20:20 +00001770 // Negative offset is not ok. FIXME: We should change BB layout to make
1771 // sure all the branches are forward.
Evan Chengd26b14c2009-07-31 18:28:05 +00001772 if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
Evan Cheng5657c012009-07-29 02:18:14 +00001773 ByteOk = false;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001774 unsigned TBHLimit = ((1<<16)-1)*2;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001775 if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
Evan Cheng5657c012009-07-29 02:18:14 +00001776 HalfWordOk = false;
1777 if (!ByteOk && !HalfWordOk)
1778 break;
1779 }
1780
1781 if (ByteOk || HalfWordOk) {
1782 MachineBasicBlock *MBB = MI->getParent();
1783 unsigned BaseReg = MI->getOperand(0).getReg();
1784 bool BaseRegKill = MI->getOperand(0).isKill();
1785 if (!BaseRegKill)
1786 continue;
1787 unsigned IdxReg = MI->getOperand(1).getReg();
1788 bool IdxRegKill = MI->getOperand(1).isKill();
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001789
1790 // Scan backwards to find the instruction that defines the base
1791 // register. Due to post-RA scheduling, we can't count on it
1792 // immediately preceding the branch instruction.
Evan Cheng5657c012009-07-29 02:18:14 +00001793 MachineBasicBlock::iterator PrevI = MI;
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001794 MachineBasicBlock::iterator B = MBB->begin();
1795 while (PrevI != B && !PrevI->definesRegister(BaseReg))
1796 --PrevI;
1797
1798 // If for some reason we didn't find it, we can't do anything, so
1799 // just skip this one.
1800 if (!PrevI->definesRegister(BaseReg))
Evan Cheng5657c012009-07-29 02:18:14 +00001801 continue;
1802
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001803 MachineInstr *AddrMI = PrevI;
Evan Cheng5657c012009-07-29 02:18:14 +00001804 bool OptOk = true;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001805 // Examine the instruction that calculates the jumptable entry address.
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001806 // Make sure it only defines the base register and kills any uses
1807 // other than the index register.
Evan Cheng5657c012009-07-29 02:18:14 +00001808 for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
1809 const MachineOperand &MO = AddrMI->getOperand(k);
1810 if (!MO.isReg() || !MO.getReg())
1811 continue;
1812 if (MO.isDef() && MO.getReg() != BaseReg) {
1813 OptOk = false;
1814 break;
1815 }
1816 if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
1817 OptOk = false;
1818 break;
1819 }
1820 }
1821 if (!OptOk)
1822 continue;
1823
Owen Anderson6b8719f2010-12-13 22:51:08 +00001824 // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001825 // that gave us the initial base register definition.
1826 for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI)
1827 ;
1828
Owen Anderson6b8719f2010-12-13 22:51:08 +00001829 // The instruction should be a tLEApcrel or t2LEApcrelJT; we want
Evan Chenga1efbbd2009-08-14 00:32:16 +00001830 // to delete it as well.
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001831 MachineInstr *LeaMI = PrevI;
Evan Chenga1efbbd2009-08-14 00:32:16 +00001832 if ((LeaMI->getOpcode() != ARM::tLEApcrelJT &&
Owen Anderson6b8719f2010-12-13 22:51:08 +00001833 LeaMI->getOpcode() != ARM::t2LEApcrelJT) ||
Evan Cheng5657c012009-07-29 02:18:14 +00001834 LeaMI->getOperand(0).getReg() != BaseReg)
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001835 OptOk = false;
Evan Cheng5657c012009-07-29 02:18:14 +00001836
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001837 if (!OptOk)
1838 continue;
1839
Jim Grosbachd092a872010-11-29 21:28:32 +00001840 unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001841 MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
1842 .addReg(IdxReg, getKillRegState(IdxRegKill))
1843 .addJumpTableIndex(JTI, JTOP.getTargetFlags())
1844 .addImm(MI->getOperand(JTOpIdx+1).getImm());
1845 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1846 // is 2-byte aligned. For now, asm printer will fix it up.
1847 unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
1848 unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
1849 OrigSize += TII->GetInstSizeInBytes(LeaMI);
1850 OrigSize += TII->GetInstSizeInBytes(MI);
1851
1852 AddrMI->eraseFromParent();
1853 LeaMI->eraseFromParent();
1854 MI->eraseFromParent();
1855
1856 int delta = OrigSize - NewSize;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001857 BBInfo[MBB->getNumber()].Size -= delta;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001858 AdjustBBOffsetsAfter(MBB);
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001859
1860 ++NumTBs;
1861 MadeChange = true;
Evan Cheng5657c012009-07-29 02:18:14 +00001862 }
1863 }
1864
1865 return MadeChange;
1866}
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001867
Jim Grosbach9249efe2009-11-16 18:55:47 +00001868/// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that
1869/// jump tables always branch forwards, since that's what tbb and tbh need.
Jim Grosbach80697d12009-11-12 17:25:07 +00001870bool ARMConstantIslands::ReorderThumb2JumpTables(MachineFunction &MF) {
1871 bool MadeChange = false;
1872
1873 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +00001874 if (MJTI == 0) return false;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001875
Jim Grosbach80697d12009-11-12 17:25:07 +00001876 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1877 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1878 MachineInstr *MI = T2JumpTables[i];
Evan Chenge837dea2011-06-28 19:10:37 +00001879 const MCInstrDesc &MCID = MI->getDesc();
1880 unsigned NumOps = MCID.getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001881 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
Jim Grosbach80697d12009-11-12 17:25:07 +00001882 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1883 unsigned JTI = JTOP.getIndex();
1884 assert(JTI < JT.size());
1885
1886 // We prefer if target blocks for the jump table come after the jump
1887 // instruction so we can use TB[BH]. Loop through the target blocks
1888 // and try to adjust them such that that's true.
Jim Grosbach08cbda52009-11-16 18:58:52 +00001889 int JTNumber = MI->getParent()->getNumber();
Jim Grosbach80697d12009-11-12 17:25:07 +00001890 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1891 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1892 MachineBasicBlock *MBB = JTBBs[j];
Jim Grosbach08cbda52009-11-16 18:58:52 +00001893 int DTNumber = MBB->getNumber();
Jim Grosbach80697d12009-11-12 17:25:07 +00001894
Jim Grosbach08cbda52009-11-16 18:58:52 +00001895 if (DTNumber < JTNumber) {
Jim Grosbach80697d12009-11-12 17:25:07 +00001896 // The destination precedes the switch. Try to move the block forward
1897 // so we have a positive offset.
1898 MachineBasicBlock *NewBB =
1899 AdjustJTTargetBlockForward(MBB, MI->getParent());
1900 if (NewBB)
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001901 MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
Jim Grosbach80697d12009-11-12 17:25:07 +00001902 MadeChange = true;
1903 }
1904 }
1905 }
1906
1907 return MadeChange;
1908}
1909
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001910MachineBasicBlock *ARMConstantIslands::
1911AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB)
1912{
1913 MachineFunction &MF = *BB->getParent();
1914
Jim Grosbach03e2d442010-07-07 22:53:35 +00001915 // If the destination block is terminated by an unconditional branch,
Jim Grosbach80697d12009-11-12 17:25:07 +00001916 // try to move it; otherwise, create a new block following the jump
Jim Grosbach08cbda52009-11-16 18:58:52 +00001917 // table that branches back to the actual target. This is a very simple
1918 // heuristic. FIXME: We can definitely improve it.
Jim Grosbach80697d12009-11-12 17:25:07 +00001919 MachineBasicBlock *TBB = 0, *FBB = 0;
1920 SmallVector<MachineOperand, 4> Cond;
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001921 SmallVector<MachineOperand, 4> CondPrior;
1922 MachineFunction::iterator BBi = BB;
1923 MachineFunction::iterator OldPrior = prior(BBi);
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001924
Jim Grosbachca215e72009-11-16 17:10:56 +00001925 // If the block terminator isn't analyzable, don't try to move the block
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001926 bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond);
Jim Grosbachca215e72009-11-16 17:10:56 +00001927
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001928 // If the block ends in an unconditional branch, move it. The prior block
1929 // has to have an analyzable terminator for us to move this one. Be paranoid
Jim Grosbach08cbda52009-11-16 18:58:52 +00001930 // and make sure we're not trying to move the entry block of the function.
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001931 if (!B && Cond.empty() && BB != MF.begin() &&
1932 !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
Jim Grosbach80697d12009-11-12 17:25:07 +00001933 BB->moveAfter(JTBB);
1934 OldPrior->updateTerminator();
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001935 BB->updateTerminator();
Jim Grosbach08cbda52009-11-16 18:58:52 +00001936 // Update numbering to account for the block being moved.
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001937 MF.RenumberBlocks();
Jim Grosbach80697d12009-11-12 17:25:07 +00001938 ++NumJTMoved;
1939 return NULL;
1940 }
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001941
1942 // Create a new MBB for the code after the jump BB.
1943 MachineBasicBlock *NewBB =
1944 MF.CreateMachineBasicBlock(JTBB->getBasicBlock());
1945 MachineFunction::iterator MBBI = JTBB; ++MBBI;
1946 MF.insert(MBBI, NewBB);
1947
1948 // Add an unconditional branch from NewBB to BB.
1949 // There doesn't seem to be meaningful DebugInfo available; this doesn't
1950 // correspond directly to anything in the source.
1951 assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?");
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001952 BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
1953 .addImm(ARMCC::AL).addReg(0);
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001954
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001955 // Update internal data structures to account for the newly inserted MBB.
1956 MF.RenumberBlocks(NewBB);
1957
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001958 // Update the CFG.
1959 NewBB->addSuccessor(BB);
1960 JTBB->removeSuccessor(BB);
1961 JTBB->addSuccessor(NewBB);
1962
Jim Grosbach80697d12009-11-12 17:25:07 +00001963 ++NumJTInserted;
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001964 return NewBB;
1965}