blob: fcf87fb65c4bde9be501846cd89b6313e6244a0a [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"
Chris Lattner705e07f2009-08-23 03:41:05 +000029#include "llvm/Support/raw_ostream.h"
Bob Wilsonb9239532009-10-15 20:49:47 +000030#include "llvm/ADT/SmallSet.h"
Evan Chengc99ef082007-02-09 20:54:44 +000031#include "llvm/ADT/SmallVector.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/Statistic.h"
Jim Grosbach1fc7d712009-11-11 02:47:19 +000034#include "llvm/Support/CommandLine.h"
Bob Wilsonb9239532009-10-15 20:49:47 +000035#include <algorithm>
Evan Chenga8e29892007-01-19 07:51:42 +000036using namespace llvm;
37
Evan Chenga1efbbd2009-08-14 00:32:16 +000038STATISTIC(NumCPEs, "Number of constpool entries");
39STATISTIC(NumSplit, "Number of uncond branches inserted");
40STATISTIC(NumCBrFixed, "Number of cond branches fixed");
41STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
42STATISTIC(NumTBs, "Number of table branches generated");
43STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk");
Evan Cheng31b99dd2009-08-14 18:31:44 +000044STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
Evan Chengde17fb62009-10-31 23:46:45 +000045STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
Jim Grosbach1fc7d712009-11-11 02:47:19 +000046STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
Jim Grosbach80697d12009-11-12 17:25:07 +000047STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
Jim Grosbach1fc7d712009-11-11 02:47:19 +000048
49
50static cl::opt<bool>
Jim Grosbachf04777b2009-11-17 21:24:11 +000051AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
Jim Grosbach1fc7d712009-11-11 02:47:19 +000052 cl::desc("Adjust basic block layout to better use TB[BH]"));
Evan Chenga8e29892007-01-19 07:51:42 +000053
54namespace {
Dale Johannesen88e37ae2007-02-23 05:02:36 +000055 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
Evan Chenga8e29892007-01-19 07:51:42 +000056 /// requires constant pool entries to be scattered among the instructions
57 /// inside a function. To do this, it completely ignores the normal LLVM
Dale Johannesen88e37ae2007-02-23 05:02:36 +000058 /// constant pool; instead, it places constants wherever it feels like with
Evan Chenga8e29892007-01-19 07:51:42 +000059 /// special instructions.
60 ///
61 /// The terminology used in this pass includes:
62 /// Islands - Clumps of constants placed in the function.
63 /// Water - Potential places where an island could be formed.
64 /// CPE - A constant pool entry that has been placed somewhere, which
65 /// tracks a list of users.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000066 class ARMConstantIslands : public MachineFunctionPass {
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +000067 /// BasicBlockInfo - Information about the offset and size of a single
68 /// basic block.
69 struct BasicBlockInfo {
70 /// Offset - Distance from the beginning of the function to the beginning
71 /// of this basic block.
72 ///
73 /// The two-byte pads required for Thumb alignment are counted as part of
74 /// the following block.
75 unsigned Offset;
Bob Wilson84945262009-05-12 17:09:30 +000076
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +000077 /// Size - Size of the basic block in bytes. If the block contains
78 /// inline assembly, this is a worst case estimate.
79 ///
80 /// The two-byte pads required for Thumb alignment are counted as part of
81 /// the following block (i.e., the offset and size for a padded block
82 /// will both be ==2 mod 4).
83 unsigned Size;
84
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +000085 /// Unalign - When non-zero, the block contains instructions (inline asm)
86 /// of unknown size. The real size may be smaller than Size bytes by a
87 /// multiple of 1 << Unalign.
88 uint8_t Unalign;
89
90 /// PostAlign - When non-zero, the block terminator contains a .align
91 /// directive, so the end of the block is aligned to 1 << PostAlign
92 /// bytes.
93 uint8_t PostAlign;
94
95 BasicBlockInfo() : Offset(0), Size(0), Unalign(0), PostAlign(0) {}
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +000096
97 /// Compute the offset immediately following this block.
98 unsigned postOffset() const { return Offset + Size; }
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +000099 };
100
101 std::vector<BasicBlockInfo> BBInfo;
Dale Johannesen99c49a42007-02-25 00:47:03 +0000102
Evan Chenga8e29892007-01-19 07:51:42 +0000103 /// WaterList - A sorted list of basic blocks where islands could be placed
104 /// (i.e. blocks that don't fall through to the following block, due
105 /// to a return, unreachable, or unconditional branch).
Evan Chenge03cff62007-02-09 23:59:14 +0000106 std::vector<MachineBasicBlock*> WaterList;
Evan Chengc99ef082007-02-09 20:54:44 +0000107
Bob Wilsonb9239532009-10-15 20:49:47 +0000108 /// NewWaterList - The subset of WaterList that was created since the
109 /// previous iteration by inserting unconditional branches.
110 SmallSet<MachineBasicBlock*, 4> NewWaterList;
111
Bob Wilson034de5f2009-10-12 18:52:13 +0000112 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
113
Evan Chenga8e29892007-01-19 07:51:42 +0000114 /// CPUser - One user of a constant pool, keeping the machine instruction
115 /// pointer, the constant pool being referenced, and the max displacement
Bob Wilson549dda92009-10-15 05:52:29 +0000116 /// allowed from the instruction to the CP. The HighWaterMark records the
117 /// highest basic block where a new CPEntry can be placed. To ensure this
118 /// pass terminates, the CP entries are initially placed at the end of the
119 /// function and then move monotonically to lower addresses. The
120 /// exception to this rule is when the current CP entry for a particular
121 /// CPUser is out of range, but there is another CP entry for the same
122 /// constant value in range. We want to use the existing in-range CP
123 /// entry, but if it later moves out of range, the search for new water
124 /// should resume where it left off. The HighWaterMark is used to record
125 /// that point.
Evan Chenga8e29892007-01-19 07:51:42 +0000126 struct CPUser {
127 MachineInstr *MI;
128 MachineInstr *CPEMI;
Bob Wilson549dda92009-10-15 05:52:29 +0000129 MachineBasicBlock *HighWaterMark;
Evan Chenga8e29892007-01-19 07:51:42 +0000130 unsigned MaxDisp;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000131 bool NegOk;
Evan Chengd3d9d662009-07-23 18:27:47 +0000132 bool IsSoImm;
133 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
134 bool neg, bool soimm)
Bob Wilson549dda92009-10-15 05:52:29 +0000135 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {
136 HighWaterMark = CPEMI->getParent();
137 }
Evan Chenga8e29892007-01-19 07:51:42 +0000138 };
Bob Wilson84945262009-05-12 17:09:30 +0000139
Evan Chenga8e29892007-01-19 07:51:42 +0000140 /// CPUsers - Keep track of all of the machine instructions that use various
141 /// constant pools and their max displacement.
Evan Chenge03cff62007-02-09 23:59:14 +0000142 std::vector<CPUser> CPUsers;
Bob Wilson84945262009-05-12 17:09:30 +0000143
Evan Chengc99ef082007-02-09 20:54:44 +0000144 /// CPEntry - One per constant pool entry, keeping the machine instruction
145 /// pointer, the constpool index, and the number of CPUser's which
146 /// reference this entry.
147 struct CPEntry {
148 MachineInstr *CPEMI;
149 unsigned CPI;
150 unsigned RefCount;
151 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
152 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
153 };
154
155 /// CPEntries - Keep track of all of the constant pool entry machine
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000156 /// instructions. For each original constpool index (i.e. those that
157 /// existed upon entry to this pass), it keeps a vector of entries.
158 /// Original elements are cloned as we go along; the clones are
159 /// put in the vector of the original element, but have distinct CPIs.
Evan Chengc99ef082007-02-09 20:54:44 +0000160 std::vector<std::vector<CPEntry> > CPEntries;
Bob Wilson84945262009-05-12 17:09:30 +0000161
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000162 /// ImmBranch - One per immediate branch, keeping the machine instruction
163 /// pointer, conditional or unconditional, the max displacement,
164 /// and (if isCond is true) the corresponding unconditional branch
165 /// opcode.
166 struct ImmBranch {
167 MachineInstr *MI;
Evan Chengc2854142007-01-25 23:18:59 +0000168 unsigned MaxDisp : 31;
169 bool isCond : 1;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000170 int UncondBr;
Evan Chengc2854142007-01-25 23:18:59 +0000171 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
172 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000173 };
174
Evan Cheng2706f972007-05-16 05:14:06 +0000175 /// ImmBranches - Keep track of all the immediate branch instructions.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000176 ///
Evan Chenge03cff62007-02-09 23:59:14 +0000177 std::vector<ImmBranch> ImmBranches;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000178
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000179 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
180 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000181 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000182
Evan Cheng5657c012009-07-29 02:18:14 +0000183 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
184 SmallVector<MachineInstr*, 4> T2JumpTables;
185
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000186 /// HasFarJump - True if any far jump instruction has been emitted during
187 /// the branch fix up pass.
188 bool HasFarJump;
189
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000190 /// HasInlineAsm - True if the function contains inline assembly.
191 bool HasInlineAsm;
192
Chris Lattner20628752010-07-22 21:27:00 +0000193 const ARMInstrInfo *TII;
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000194 const ARMSubtarget *STI;
Dale Johannesen8593e412007-04-29 19:19:30 +0000195 ARMFunctionInfo *AFI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000196 bool isThumb;
Evan Chengd3d9d662009-07-23 18:27:47 +0000197 bool isThumb1;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000198 bool isThumb2;
Evan Chenga8e29892007-01-19 07:51:42 +0000199 public:
Devang Patel19974732007-05-03 01:11:54 +0000200 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +0000201 ARMConstantIslands() : MachineFunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000202
Evan Cheng5657c012009-07-29 02:18:14 +0000203 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000204
205 virtual const char *getPassName() const {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000206 return "ARM constant island placement and branch shortening pass";
Evan Chenga8e29892007-01-19 07:51:42 +0000207 }
Bob Wilson84945262009-05-12 17:09:30 +0000208
Evan Chenga8e29892007-01-19 07:51:42 +0000209 private:
Evan Cheng5657c012009-07-29 02:18:14 +0000210 void DoInitialPlacement(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000211 std::vector<MachineInstr*> &CPEMIs);
Evan Chengc99ef082007-02-09 20:54:44 +0000212 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Jim Grosbach80697d12009-11-12 17:25:07 +0000213 void JumpTableFunctionScan(MachineFunction &MF);
Evan Cheng5657c012009-07-29 02:18:14 +0000214 void InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000215 const std::vector<MachineInstr*> &CPEMIs);
Evan Cheng0c615842007-01-31 02:22:22 +0000216 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Chenga8e29892007-01-19 07:51:42 +0000217 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +0000218 void AdjustBBOffsetsAfter(MachineBasicBlock *BB);
Evan Chenged884f32007-04-03 23:39:48 +0000219 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000220 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
Bob Wilsonb9239532009-10-15 20:49:47 +0000221 bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000222 void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
Bob Wilson757652c2009-10-12 21:39:43 +0000223 MachineBasicBlock *&NewMBB);
Evan Cheng5657c012009-07-29 02:18:14 +0000224 bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex);
Evan Chenged884f32007-04-03 23:39:48 +0000225 void RemoveDeadCPEMI(MachineInstr *CPEMI);
226 bool RemoveUnusedCPEntries();
Bob Wilson84945262009-05-12 17:09:30 +0000227 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000228 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
229 bool DoDump = false);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000230 bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000231 CPUser &U);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000232 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000233 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
Evan Chengc0dbec72007-01-31 19:57:44 +0000234 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Cheng5657c012009-07-29 02:18:14 +0000235 bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br);
236 bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br);
237 bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000238 bool UndoLRSpillRestore();
Evan Chenga1efbbd2009-08-14 00:32:16 +0000239 bool OptimizeThumb2Instructions(MachineFunction &MF);
240 bool OptimizeThumb2Branches(MachineFunction &MF);
Jim Grosbach80697d12009-11-12 17:25:07 +0000241 bool ReorderThumb2JumpTables(MachineFunction &MF);
Evan Cheng5657c012009-07-29 02:18:14 +0000242 bool OptimizeThumb2JumpTables(MachineFunction &MF);
Jim Grosbach1fc7d712009-11-11 02:47:19 +0000243 MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB,
244 MachineBasicBlock *JTBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000245
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000246 void ComputeBlockSize(const MachineBasicBlock *MBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000247 unsigned GetOffsetOf(MachineInstr *MI) const;
Dale Johannesen8593e412007-04-29 19:19:30 +0000248 void dumpBBs();
Evan Cheng5657c012009-07-29 02:18:14 +0000249 void verify(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000250 };
Devang Patel19974732007-05-03 01:11:54 +0000251 char ARMConstantIslands::ID = 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000252}
253
Dale Johannesen8593e412007-04-29 19:19:30 +0000254/// verify - check BBOffsets, BBSizes, alignment of islands
Evan Cheng5657c012009-07-29 02:18:14 +0000255void ARMConstantIslands::verify(MachineFunction &MF) {
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000256 for (unsigned i = 1, e = BBInfo.size(); i != e; ++i)
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000257 assert(BBInfo[i-1].postOffset() == BBInfo[i].Offset);
Evan Chengd3d9d662009-07-23 18:27:47 +0000258 if (!isThumb)
259 return;
260#ifndef NDEBUG
Evan Cheng5657c012009-07-29 02:18:14 +0000261 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chengd3d9d662009-07-23 18:27:47 +0000262 MBBI != E; ++MBBI) {
263 MachineBasicBlock *MBB = MBBI;
264 if (!MBB->empty() &&
265 MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
266 unsigned MBBId = MBB->getNumber();
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000267 assert(HasInlineAsm ||
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000268 (BBInfo[MBBId].Offset%4 == 0 && BBInfo[MBBId].Size%4 == 0) ||
269 (BBInfo[MBBId].Offset%4 != 0 && BBInfo[MBBId].Size%4 != 0));
Dale Johannesen8593e412007-04-29 19:19:30 +0000270 }
271 }
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000272 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
273 CPUser &U = CPUsers[i];
274 unsigned UserOffset = GetOffsetOf(U.MI) + (isThumb ? 4 : 8);
Jim Grosbacha9562562009-11-20 19:37:38 +0000275 unsigned CPEOffset = GetOffsetOf(U.CPEMI);
276 unsigned Disp = UserOffset < CPEOffset ? CPEOffset - UserOffset :
277 UserOffset - CPEOffset;
278 assert(Disp <= U.MaxDisp || "Constant pool entry out of range!");
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000279 }
Jim Grosbacha9562562009-11-20 19:37:38 +0000280#endif
Dale Johannesen8593e412007-04-29 19:19:30 +0000281}
282
283/// print block size and offset information - debugging
284void ARMConstantIslands::dumpBBs() {
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000285 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
286 DEBUG(errs() << "block " << J << " offset " << BBInfo[J].Offset
287 << " size " << BBInfo[J].Size << "\n");
Dale Johannesen8593e412007-04-29 19:19:30 +0000288 }
289}
290
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000291/// createARMConstantIslandPass - returns an instance of the constpool
292/// island pass.
Evan Chenga8e29892007-01-19 07:51:42 +0000293FunctionPass *llvm::createARMConstantIslandPass() {
294 return new ARMConstantIslands();
295}
296
Evan Cheng5657c012009-07-29 02:18:14 +0000297bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) {
298 MachineConstantPool &MCP = *MF.getConstantPool();
Bob Wilson84945262009-05-12 17:09:30 +0000299
Chris Lattner20628752010-07-22 21:27:00 +0000300 TII = (const ARMInstrInfo*)MF.getTarget().getInstrInfo();
Evan Cheng5657c012009-07-29 02:18:14 +0000301 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000302 STI = &MF.getTarget().getSubtarget<ARMSubtarget>();
303
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000304 isThumb = AFI->isThumbFunction();
Evan Chengd3d9d662009-07-23 18:27:47 +0000305 isThumb1 = AFI->isThumb1OnlyFunction();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000306 isThumb2 = AFI->isThumb2Function();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000307
308 HasFarJump = false;
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000309 HasInlineAsm = false;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000310
Evan Chenga8e29892007-01-19 07:51:42 +0000311 // Renumber all of the machine basic blocks in the function, guaranteeing that
312 // the numbers agree with the position of the block in the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000313 MF.RenumberBlocks();
Evan Chenga8e29892007-01-19 07:51:42 +0000314
Jim Grosbach80697d12009-11-12 17:25:07 +0000315 // Try to reorder and otherwise adjust the block layout to make good use
316 // of the TB[BH] instructions.
317 bool MadeChange = false;
318 if (isThumb2 && AdjustJumpTableBlocks) {
319 JumpTableFunctionScan(MF);
320 MadeChange |= ReorderThumb2JumpTables(MF);
321 // Data is out of date, so clear it. It'll be re-computed later.
Jim Grosbach80697d12009-11-12 17:25:07 +0000322 T2JumpTables.clear();
323 // Blocks may have shifted around. Keep the numbering up to date.
324 MF.RenumberBlocks();
325 }
326
Evan Chengd26b14c2009-07-31 18:28:05 +0000327 // Thumb1 functions containing constant pools get 4-byte alignment.
Evan Chengd3d9d662009-07-23 18:27:47 +0000328 // This is so we can keep exact track of where the alignment padding goes.
329
Chris Lattner7d7dab02010-01-27 23:37:36 +0000330 // ARM and Thumb2 functions need to be 4-byte aligned.
331 if (!isThumb1)
332 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000333
Evan Chenga8e29892007-01-19 07:51:42 +0000334 // Perform the initial placement of the constant pool entries. To start with,
335 // we put them all at the end of the function.
Evan Chenge03cff62007-02-09 23:59:14 +0000336 std::vector<MachineInstr*> CPEMIs;
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000337 if (!MCP.isEmpty()) {
Evan Cheng5657c012009-07-29 02:18:14 +0000338 DoInitialPlacement(MF, CPEMIs);
Evan Chengd3d9d662009-07-23 18:27:47 +0000339 if (isThumb1)
Chris Lattner7d7dab02010-01-27 23:37:36 +0000340 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000341 }
Bob Wilson84945262009-05-12 17:09:30 +0000342
Evan Chenga8e29892007-01-19 07:51:42 +0000343 /// The next UID to take is the first unused one.
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000344 AFI->initPICLabelUId(CPEMIs.size());
Bob Wilson84945262009-05-12 17:09:30 +0000345
Evan Chenga8e29892007-01-19 07:51:42 +0000346 // Do the initial scan of the function, building up information about the
347 // sizes of each block, the location of all the water, and finding all of the
348 // constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000349 InitialFunctionScan(MF, CPEMIs);
Evan Chenga8e29892007-01-19 07:51:42 +0000350 CPEMIs.clear();
Dale Johannesen8086d582010-07-23 22:50:23 +0000351 DEBUG(dumpBBs());
352
Bob Wilson84945262009-05-12 17:09:30 +0000353
Evan Chenged884f32007-04-03 23:39:48 +0000354 /// Remove dead constant pool entries.
Bill Wendlingcd080242010-12-18 01:53:06 +0000355 MadeChange |= RemoveUnusedCPEntries();
Evan Chenged884f32007-04-03 23:39:48 +0000356
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000357 // Iteratively place constant pool entries and fix up branches until there
358 // is no change.
Evan Chengb6879b22009-08-07 07:35:21 +0000359 unsigned NoCPIters = 0, NoBRIters = 0;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000360 while (true) {
Evan Chengb6879b22009-08-07 07:35:21 +0000361 bool CPChange = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000362 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Evan Chengb6879b22009-08-07 07:35:21 +0000363 CPChange |= HandleConstantPoolUser(MF, i);
364 if (CPChange && ++NoCPIters > 30)
365 llvm_unreachable("Constant Island pass failed to converge!");
Evan Cheng82020102007-07-10 22:00:16 +0000366 DEBUG(dumpBBs());
Jim Grosbach26b8ef52010-07-07 21:06:51 +0000367
Bob Wilsonb9239532009-10-15 20:49:47 +0000368 // Clear NewWaterList now. If we split a block for branches, it should
369 // appear as "new water" for the next iteration of constant pool placement.
370 NewWaterList.clear();
Evan Chengb6879b22009-08-07 07:35:21 +0000371
372 bool BRChange = false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000373 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Chengb6879b22009-08-07 07:35:21 +0000374 BRChange |= FixUpImmediateBr(MF, ImmBranches[i]);
375 if (BRChange && ++NoBRIters > 30)
376 llvm_unreachable("Branch Fix Up pass failed to converge!");
Evan Cheng82020102007-07-10 22:00:16 +0000377 DEBUG(dumpBBs());
Evan Chengb6879b22009-08-07 07:35:21 +0000378
379 if (!CPChange && !BRChange)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000380 break;
381 MadeChange = true;
382 }
Evan Chenged884f32007-04-03 23:39:48 +0000383
Evan Chenga1efbbd2009-08-14 00:32:16 +0000384 // Shrink 32-bit Thumb2 branch, load, and store instructions.
Evan Chenge44be632010-08-09 18:35:19 +0000385 if (isThumb2 && !STI->prefers32BitThumb())
Evan Chenga1efbbd2009-08-14 00:32:16 +0000386 MadeChange |= OptimizeThumb2Instructions(MF);
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000387
Dale Johannesen8593e412007-04-29 19:19:30 +0000388 // After a while, this might be made debug-only, but it is not expensive.
Evan Cheng5657c012009-07-29 02:18:14 +0000389 verify(MF);
Dale Johannesen8593e412007-04-29 19:19:30 +0000390
Jim Grosbach26b8ef52010-07-07 21:06:51 +0000391 // If LR has been forced spilled and no far jump (i.e. BL) has been issued,
392 // undo the spill / restore of LR if possible.
Evan Cheng5657c012009-07-29 02:18:14 +0000393 if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000394 MadeChange |= UndoLRSpillRestore();
395
Anton Korobeynikov98b928e2011-01-30 22:07:39 +0000396 // Save the mapping between original and cloned constpool entries.
397 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
398 for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) {
399 const CPEntry & CPE = CPEntries[i][j];
400 AFI->recordCPEClone(i, CPE.CPI);
401 }
402 }
403
Evan Chengb1c857b2010-07-22 02:09:47 +0000404 DEBUG(errs() << '\n'; dumpBBs());
405
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000406 BBInfo.clear();
Evan Chenga8e29892007-01-19 07:51:42 +0000407 WaterList.clear();
408 CPUsers.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000409 CPEntries.clear();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000410 ImmBranches.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000411 PushPopMIs.clear();
Evan Cheng5657c012009-07-29 02:18:14 +0000412 T2JumpTables.clear();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000413
414 return MadeChange;
Evan Chenga8e29892007-01-19 07:51:42 +0000415}
416
417/// DoInitialPlacement - Perform the initial placement of the constant pool
418/// entries. To start with, we put them all at the end of the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000419void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +0000420 std::vector<MachineInstr*> &CPEMIs) {
Evan Chenga8e29892007-01-19 07:51:42 +0000421 // Create the basic block to hold the CPE's.
Evan Cheng5657c012009-07-29 02:18:14 +0000422 MachineBasicBlock *BB = MF.CreateMachineBasicBlock();
423 MF.push_back(BB);
Bob Wilson84945262009-05-12 17:09:30 +0000424
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +0000425 // Mark the basic block as 4-byte aligned as required by the const-pool.
426 BB->setAlignment(2);
427
Evan Chenga8e29892007-01-19 07:51:42 +0000428 // Add all of the constants from the constant pool to the end block, use an
429 // identity mapping of CPI's to CPE's.
430 const std::vector<MachineConstantPoolEntry> &CPs =
Evan Cheng5657c012009-07-29 02:18:14 +0000431 MF.getConstantPool()->getConstants();
Bob Wilson84945262009-05-12 17:09:30 +0000432
Evan Cheng5657c012009-07-29 02:18:14 +0000433 const TargetData &TD = *MF.getTarget().getTargetData();
Evan Chenga8e29892007-01-19 07:51:42 +0000434 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
Duncan Sands777d2302009-05-09 07:06:46 +0000435 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
Evan Chenga8e29892007-01-19 07:51:42 +0000436 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
437 // we would have to pad them out or something so that instructions stay
438 // aligned.
439 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
440 MachineInstr *CPEMI =
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000441 BuildMI(BB, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
442 .addImm(i).addConstantPoolIndex(i).addImm(Size);
Evan Chenga8e29892007-01-19 07:51:42 +0000443 CPEMIs.push_back(CPEMI);
Evan Chengc99ef082007-02-09 20:54:44 +0000444
445 // Add a new CPEntry, but no corresponding CPUser yet.
446 std::vector<CPEntry> CPEs;
447 CPEs.push_back(CPEntry(CPEMI, i));
448 CPEntries.push_back(CPEs);
Dan Gohmanfe601042010-06-22 15:08:57 +0000449 ++NumCPEs;
Chris Lattner893e1c92009-08-23 06:49:22 +0000450 DEBUG(errs() << "Moved CPI#" << i << " to end of function as #" << i
451 << "\n");
Evan Chenga8e29892007-01-19 07:51:42 +0000452 }
453}
454
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000455/// BBHasFallthrough - Return true if the specified basic block can fallthrough
Evan Chenga8e29892007-01-19 07:51:42 +0000456/// into the block immediately after it.
457static bool BBHasFallthrough(MachineBasicBlock *MBB) {
458 // Get the next machine basic block in the function.
459 MachineFunction::iterator MBBI = MBB;
Jim Grosbach18f30e62010-06-02 21:53:11 +0000460 // Can't fall off end of function.
461 if (llvm::next(MBBI) == MBB->getParent()->end())
Evan Chenga8e29892007-01-19 07:51:42 +0000462 return false;
Bob Wilson84945262009-05-12 17:09:30 +0000463
Chris Lattner7896c9f2009-12-03 00:50:42 +0000464 MachineBasicBlock *NextBB = llvm::next(MBBI);
Evan Chenga8e29892007-01-19 07:51:42 +0000465 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
466 E = MBB->succ_end(); I != E; ++I)
467 if (*I == NextBB)
468 return true;
Bob Wilson84945262009-05-12 17:09:30 +0000469
Evan Chenga8e29892007-01-19 07:51:42 +0000470 return false;
471}
472
Evan Chengc99ef082007-02-09 20:54:44 +0000473/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
474/// look up the corresponding CPEntry.
475ARMConstantIslands::CPEntry
476*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
477 const MachineInstr *CPEMI) {
478 std::vector<CPEntry> &CPEs = CPEntries[CPI];
479 // Number of entries per constpool index should be small, just do a
480 // linear search.
481 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
482 if (CPEs[i].CPEMI == CPEMI)
483 return &CPEs[i];
484 }
485 return NULL;
486}
487
Jim Grosbach80697d12009-11-12 17:25:07 +0000488/// JumpTableFunctionScan - Do a scan of the function, building up
489/// information about the sizes of each block and the locations of all
490/// the jump tables.
491void ARMConstantIslands::JumpTableFunctionScan(MachineFunction &MF) {
Jim Grosbach80697d12009-11-12 17:25:07 +0000492 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
493 MBBI != E; ++MBBI) {
494 MachineBasicBlock &MBB = *MBBI;
495
Jim Grosbach80697d12009-11-12 17:25:07 +0000496 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Jim Grosbach08cbda52009-11-16 18:58:52 +0000497 I != E; ++I)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000498 if (I->isBranch() && I->getOpcode() == ARM::t2BR_JT)
Jim Grosbach08cbda52009-11-16 18:58:52 +0000499 T2JumpTables.push_back(I);
Jim Grosbach80697d12009-11-12 17:25:07 +0000500 }
501}
502
Evan Chenga8e29892007-01-19 07:51:42 +0000503/// InitialFunctionScan - Do the initial scan of the function, building up
504/// information about the sizes of each block, the location of all the water,
505/// and finding all of the constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000506void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000507 const std::vector<MachineInstr*> &CPEMIs) {
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000508 // First thing, see if the function has any inline assembly in it. If so,
509 // we have to be conservative about alignment assumptions, as we don't
510 // know for sure the size of any instructions in the inline assembly.
511 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
512 MBBI != E; ++MBBI) {
513 MachineBasicBlock &MBB = *MBBI;
514 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
515 I != E; ++I)
516 if (I->getOpcode() == ARM::INLINEASM)
517 HasInlineAsm = true;
518 }
519
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000520 BBInfo.clear();
521 BBInfo.resize(MF.getNumBlockIDs());
522
Bill Wendling9a4d2e42010-12-21 01:54:40 +0000523 // Now go back through the instructions and build up our data structures.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000524 unsigned Offset = 0;
Evan Cheng5657c012009-07-29 02:18:14 +0000525 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chenga8e29892007-01-19 07:51:42 +0000526 MBBI != E; ++MBBI) {
527 MachineBasicBlock &MBB = *MBBI;
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000528 BasicBlockInfo &BBI = BBInfo[MBB.getNumber()];
529 BBI.Offset = Offset;
Bob Wilson84945262009-05-12 17:09:30 +0000530
Evan Chenga8e29892007-01-19 07:51:42 +0000531 // If this block doesn't fall through into the next MBB, then this is
532 // 'water' that a constant pool island could be placed.
533 if (!BBHasFallthrough(&MBB))
534 WaterList.push_back(&MBB);
Bob Wilson84945262009-05-12 17:09:30 +0000535
Evan Chenga8e29892007-01-19 07:51:42 +0000536 unsigned MBBSize = 0;
537 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
538 I != E; ++I) {
Jim Grosbach9cfcfeb2010-06-21 17:49:23 +0000539 if (I->isDebugValue())
540 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000541 // Add instruction size to MBBSize.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000542 MBBSize += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000543
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000544 // For inline asm, GetInstSizeInBytes returns a conservative estimate.
545 // The actual size may be smaller, but still a multiple of the instr size.
546 if (I->isInlineAsm())
547 BBI.Unalign = isThumb ? 1 : 2;
548
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000549 int Opc = I->getOpcode();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000550 if (I->isBranch()) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000551 bool isCond = false;
552 unsigned Bits = 0;
553 unsigned Scale = 1;
554 int UOpc = Opc;
555 switch (Opc) {
Evan Cheng5657c012009-07-29 02:18:14 +0000556 default:
557 continue; // Ignore other JT branches
Dale Johannesen8593e412007-04-29 19:19:30 +0000558 case ARM::tBR_JTr:
Evan Cheng66ac5312009-07-25 00:33:29 +0000559 // A Thumb1 table jump may involve padding; for the offsets to
Dale Johannesen8593e412007-04-29 19:19:30 +0000560 // be right, functions containing these must be 4-byte aligned.
Evan Chengb1c857b2010-07-22 02:09:47 +0000561 // tBR_JTr expands to a mov pc followed by .align 2 and then the jump
Jakob Stoklund Olesen6fbea432011-12-06 22:41:31 +0000562 // table entries. So this code checks whether offset of tBR_JTr + 2
563 // is aligned. That is held in Offset+MBBSize, which already has
564 // 2 added in for the size of the mov pc instruction.
Chris Lattner7d7dab02010-01-27 23:37:36 +0000565 MF.EnsureAlignment(2U);
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000566 BBI.PostAlign = 2;
Jakob Stoklund Olesen6fbea432011-12-06 22:41:31 +0000567 if ((Offset+MBBSize)%4 != 0 || HasInlineAsm)
568 // FIXME: Add a pseudo ALIGN instruction instead.
569 MBBSize += 2; // padding
Dale Johannesen8593e412007-04-29 19:19:30 +0000570 continue; // Does not get an entry in ImmBranches
Evan Cheng5657c012009-07-29 02:18:14 +0000571 case ARM::t2BR_JT:
572 T2JumpTables.push_back(I);
573 continue; // Does not get an entry in ImmBranches
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000574 case ARM::Bcc:
575 isCond = true;
576 UOpc = ARM::B;
577 // Fallthrough
578 case ARM::B:
579 Bits = 24;
580 Scale = 4;
581 break;
582 case ARM::tBcc:
583 isCond = true;
584 UOpc = ARM::tB;
585 Bits = 8;
586 Scale = 2;
587 break;
588 case ARM::tB:
589 Bits = 11;
590 Scale = 2;
591 break;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000592 case ARM::t2Bcc:
593 isCond = true;
594 UOpc = ARM::t2B;
595 Bits = 20;
596 Scale = 2;
597 break;
598 case ARM::t2B:
599 Bits = 24;
600 Scale = 2;
601 break;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000602 }
Evan Chengb43216e2007-02-01 10:16:15 +0000603
604 // Record this immediate branch.
Evan Chengbd5d3db2007-02-03 02:08:34 +0000605 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengb43216e2007-02-01 10:16:15 +0000606 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000607 }
608
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000609 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
610 PushPopMIs.push_back(I);
611
Evan Chengd3d9d662009-07-23 18:27:47 +0000612 if (Opc == ARM::CONSTPOOL_ENTRY)
613 continue;
614
Evan Chenga8e29892007-01-19 07:51:42 +0000615 // Scan the instructions for constant pool operands.
616 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Dan Gohmand735b802008-10-03 15:45:36 +0000617 if (I->getOperand(op).isCPI()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000618 // We found one. The addressing mode tells us the max displacement
619 // from the PC that this instruction permits.
Bob Wilson84945262009-05-12 17:09:30 +0000620
Evan Chenga8e29892007-01-19 07:51:42 +0000621 // Basic size info comes from the TSFlags field.
Evan Chengb43216e2007-02-01 10:16:15 +0000622 unsigned Bits = 0;
623 unsigned Scale = 1;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000624 bool NegOk = false;
Evan Chengd3d9d662009-07-23 18:27:47 +0000625 bool IsSoImm = false;
626
627 switch (Opc) {
Bob Wilson84945262009-05-12 17:09:30 +0000628 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000629 llvm_unreachable("Unknown addressing mode for CP reference!");
Evan Chengd3d9d662009-07-23 18:27:47 +0000630 break;
631
632 // Taking the address of a CP entry.
633 case ARM::LEApcrel:
634 // This takes a SoImm, which is 8 bit immediate rotated. We'll
635 // pretend the maximum offset is 255 * 4. Since each instruction
Jim Grosbachdec6de92009-11-19 18:23:19 +0000636 // 4 byte wide, this is always correct. We'll check for other
Evan Chengd3d9d662009-07-23 18:27:47 +0000637 // displacements that fits in a SoImm as well.
Evan Chengb43216e2007-02-01 10:16:15 +0000638 Bits = 8;
Evan Chengd3d9d662009-07-23 18:27:47 +0000639 Scale = 4;
640 NegOk = true;
641 IsSoImm = true;
642 break;
Owen Anderson6b8719f2010-12-13 22:51:08 +0000643 case ARM::t2LEApcrel:
Evan Chengd3d9d662009-07-23 18:27:47 +0000644 Bits = 12;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000645 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000646 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000647 case ARM::tLEApcrel:
648 Bits = 8;
649 Scale = 4;
650 break;
651
Jim Grosbach3e556122010-10-26 22:37:02 +0000652 case ARM::LDRi12:
Evan Chengd3d9d662009-07-23 18:27:47 +0000653 case ARM::LDRcp:
Owen Anderson971b83b2011-02-08 22:39:40 +0000654 case ARM::t2LDRpci:
Evan Cheng556f33c2007-02-01 20:44:52 +0000655 Bits = 12; // +-offset_12
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000656 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000657 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000658
659 case ARM::tLDRpci:
Evan Chengb43216e2007-02-01 10:16:15 +0000660 Bits = 8;
661 Scale = 4; // +(offset_8*4)
Evan Cheng012f2d92007-01-24 08:53:17 +0000662 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000663
Jim Grosbache5165492009-11-09 00:11:35 +0000664 case ARM::VLDRD:
665 case ARM::VLDRS:
Evan Chengd3d9d662009-07-23 18:27:47 +0000666 Bits = 8;
667 Scale = 4; // +-(offset_8*4)
668 NegOk = true;
Evan Cheng055b0312009-06-29 07:51:04 +0000669 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000670 }
Evan Chengb43216e2007-02-01 10:16:15 +0000671
Evan Chenga8e29892007-01-19 07:51:42 +0000672 // Remember that this is a user of a CP entry.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000673 unsigned CPI = I->getOperand(op).getIndex();
Evan Chengc99ef082007-02-09 20:54:44 +0000674 MachineInstr *CPEMI = CPEMIs[CPI];
Evan Cheng31b99dd2009-08-14 18:31:44 +0000675 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
Evan Chengd3d9d662009-07-23 18:27:47 +0000676 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
Evan Chengc99ef082007-02-09 20:54:44 +0000677
678 // Increment corresponding CPEntry reference count.
679 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
680 assert(CPE && "Cannot find a corresponding CPEntry!");
681 CPE->RefCount++;
Bob Wilson84945262009-05-12 17:09:30 +0000682
Evan Chenga8e29892007-01-19 07:51:42 +0000683 // Instructions can only use one CP entry, don't bother scanning the
684 // rest of the operands.
685 break;
686 }
687 }
Evan Cheng2021abe2007-02-01 01:09:47 +0000688
Dale Johannesen8593e412007-04-29 19:19:30 +0000689 // In thumb mode, if this block is a constpool island, we may need padding
690 // so it's aligned on 4 byte boundary.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000691 if (isThumb &&
Evan Cheng05cc4242007-02-02 19:09:19 +0000692 !MBB.empty() &&
Dale Johannesen8593e412007-04-29 19:19:30 +0000693 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000694 ((Offset%4) != 0 || HasInlineAsm))
Evan Cheng2021abe2007-02-01 01:09:47 +0000695 MBBSize += 2;
696
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000697 BBI.Size = MBBSize;
Dale Johannesen99c49a42007-02-25 00:47:03 +0000698 Offset += MBBSize;
Evan Chenga8e29892007-01-19 07:51:42 +0000699 }
700}
701
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000702/// ComputeBlockSize - Compute the size and some alignment information for MBB.
703/// This function updates BBInfo directly.
704void ARMConstantIslands::ComputeBlockSize(const MachineBasicBlock *MBB) {
705 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
706 BBI.Size = 0;
707 BBI.Unalign = 0;
708 BBI.PostAlign = 0;
709
710 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
711 I != E; ++I) {
712 BBI.Size += TII->GetInstSizeInBytes(I);
713 // For inline asm, GetInstSizeInBytes returns a conservative estimate.
714 // The actual size may be smaller, but still a multiple of the instr size.
715 if (I->isInlineAsm())
716 BBI.Unalign = isThumb ? 1 : 2;
717 }
718
719 // tBR_JTr contains a .align 2 directive.
720 if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr)
721 BBI.PostAlign = 2;
722}
723
Evan Chenga8e29892007-01-19 07:51:42 +0000724/// GetOffsetOf - Return the current offset of the specified machine instruction
725/// from the start of the function. This offset changes as stuff is moved
726/// around inside the function.
727unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
728 MachineBasicBlock *MBB = MI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000729
Evan Chenga8e29892007-01-19 07:51:42 +0000730 // The offset is composed of two things: the sum of the sizes of all MBB's
731 // before this instruction's block, and the offset from the start of the block
732 // it is in.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000733 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
Evan Chenga8e29892007-01-19 07:51:42 +0000734
Dale Johannesen8593e412007-04-29 19:19:30 +0000735 // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
736 // alignment padding, and compensate if so.
Bob Wilson84945262009-05-12 17:09:30 +0000737 if (isThumb &&
738 MI->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000739 (Offset%4 != 0 || HasInlineAsm))
Dale Johannesen8593e412007-04-29 19:19:30 +0000740 Offset += 2;
741
Evan Chenga8e29892007-01-19 07:51:42 +0000742 // Sum instructions before MI in MBB.
743 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
744 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
745 if (&*I == MI) return Offset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000746 Offset += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000747 }
748}
749
750/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
751/// ID.
752static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
753 const MachineBasicBlock *RHS) {
754 return LHS->getNumber() < RHS->getNumber();
755}
756
757/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
758/// machine function, it upsets all of the block numbers. Renumber the blocks
759/// and update the arrays that parallel this numbering.
760void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
Duncan Sandsab4c3662011-02-15 09:23:02 +0000761 // Renumber the MBB's to keep them consecutive.
Evan Chenga8e29892007-01-19 07:51:42 +0000762 NewBB->getParent()->RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000763
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000764 // Insert an entry into BBInfo to align it properly with the (newly
Evan Chenga8e29892007-01-19 07:51:42 +0000765 // renumbered) block numbers.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000766 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Bob Wilson84945262009-05-12 17:09:30 +0000767
768 // Next, update WaterList. Specifically, we need to add NewMBB as having
Evan Chenga8e29892007-01-19 07:51:42 +0000769 // available water after it.
Bob Wilson034de5f2009-10-12 18:52:13 +0000770 water_iterator IP =
Evan Chenga8e29892007-01-19 07:51:42 +0000771 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
772 CompareMBBNumbers);
773 WaterList.insert(IP, NewBB);
774}
775
776
777/// Split the basic block containing MI into two blocks, which are joined by
Bob Wilsonb9239532009-10-15 20:49:47 +0000778/// an unconditional branch. Update data structures and renumber blocks to
Evan Cheng0c615842007-01-31 02:22:22 +0000779/// account for this change and returns the newly created block.
780MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Chenga8e29892007-01-19 07:51:42 +0000781 MachineBasicBlock *OrigBB = MI->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000782 MachineFunction &MF = *OrigBB->getParent();
Evan Chenga8e29892007-01-19 07:51:42 +0000783
784 // Create a new MBB for the code after the OrigBB.
Bob Wilson84945262009-05-12 17:09:30 +0000785 MachineBasicBlock *NewBB =
786 MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
Evan Chenga8e29892007-01-19 07:51:42 +0000787 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000788 MF.insert(MBBI, NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000789
Evan Chenga8e29892007-01-19 07:51:42 +0000790 // Splice the instructions starting with MI over to NewBB.
791 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
Bob Wilson84945262009-05-12 17:09:30 +0000792
Evan Chenga8e29892007-01-19 07:51:42 +0000793 // Add an unconditional branch from OrigBB to NewBB.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000794 // Note the new unconditional branch is not being recorded.
Dale Johannesenb6728402009-02-13 02:25:56 +0000795 // There doesn't seem to be meaningful DebugInfo available; this doesn't
796 // correspond to anything in the source.
Evan Cheng58541fd2009-07-07 01:16:41 +0000797 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000798 if (!isThumb)
799 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
800 else
801 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
802 .addImm(ARMCC::AL).addReg(0);
Dan Gohmanfe601042010-06-22 15:08:57 +0000803 ++NumSplit;
Bob Wilson84945262009-05-12 17:09:30 +0000804
Evan Chenga8e29892007-01-19 07:51:42 +0000805 // Update the CFG. All succs of OrigBB are now succs of NewBB.
Jakob Stoklund Olesene80fba02011-12-06 00:51:12 +0000806 NewBB->transferSuccessors(OrigBB);
Bob Wilson84945262009-05-12 17:09:30 +0000807
Evan Chenga8e29892007-01-19 07:51:42 +0000808 // OrigBB branches to NewBB.
809 OrigBB->addSuccessor(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000810
Evan Chenga8e29892007-01-19 07:51:42 +0000811 // Update internal data structures to account for the newly inserted MBB.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000812 // This is almost the same as UpdateForInsertedWaterBlock, except that
813 // the Water goes after OrigBB, not NewBB.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000814 MF.RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000815
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000816 // Insert an entry into BBInfo to align it properly with the (newly
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000817 // renumbered) block numbers.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000818 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Dale Johannesen99c49a42007-02-25 00:47:03 +0000819
Bob Wilson84945262009-05-12 17:09:30 +0000820 // Next, update WaterList. Specifically, we need to add OrigMBB as having
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000821 // available water after it (but not if it's already there, which happens
822 // when splitting before a conditional branch that is followed by an
823 // unconditional branch - in that case we want to insert NewBB).
Bob Wilson034de5f2009-10-12 18:52:13 +0000824 water_iterator IP =
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000825 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
826 CompareMBBNumbers);
827 MachineBasicBlock* WaterBB = *IP;
828 if (WaterBB == OrigBB)
Chris Lattner7896c9f2009-12-03 00:50:42 +0000829 WaterList.insert(llvm::next(IP), NewBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000830 else
831 WaterList.insert(IP, OrigBB);
Bob Wilsonb9239532009-10-15 20:49:47 +0000832 NewWaterList.insert(OrigBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000833
Dale Johannesen99c49a42007-02-25 00:47:03 +0000834 unsigned OrigBBI = OrigBB->getNumber();
835 unsigned NewBBI = NewBB->getNumber();
Bob Wilson84945262009-05-12 17:09:30 +0000836
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000837 int delta = isThumb1 ? 2 : 4;
Dale Johannesen8086d582010-07-23 22:50:23 +0000838
839 // Figure out how large the OrigBB is. As the first half of the original
840 // block, it cannot contain a tablejump. The size includes
841 // the new jump we added. (It should be possible to do this without
842 // recounting everything, but it's very confusing, and this is rarely
843 // executed.)
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000844 ComputeBlockSize(OrigBB);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000845
846 // ...and adjust BBOffsets for NewBB accordingly.
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000847 BBInfo[NewBBI].Offset = BBInfo[OrigBBI].postOffset();
Dale Johannesen99c49a42007-02-25 00:47:03 +0000848
Dale Johannesen8086d582010-07-23 22:50:23 +0000849 // Figure out how large the NewMBB is. As the second half of the original
850 // block, it may contain a tablejump.
Jakob Stoklund Olesena26811e2011-12-07 04:17:35 +0000851 ComputeBlockSize(NewBB);
Dale Johannesen8086d582010-07-23 22:50:23 +0000852
Jakob Stoklund Olesen6fbea432011-12-06 22:41:31 +0000853 MachineInstr* ThumbJTMI = prior(NewBB->end());
854 if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) {
855 // We've added another 2-byte instruction before this tablejump, which
856 // means we will always need padding if we didn't before, and vice versa.
857
858 // The original offset of the jump instruction was:
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000859 unsigned OrigOffset = BBInfo[OrigBBI].postOffset() - delta;
Jakob Stoklund Olesen6fbea432011-12-06 22:41:31 +0000860 if (OrigOffset%4 == 0) {
861 // We had padding before and now we don't. No net change in code size.
862 delta = 0;
863 } else {
864 // We didn't have padding before and now we do.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000865 BBInfo[NewBBI].Size += 2;
Jakob Stoklund Olesen6fbea432011-12-06 22:41:31 +0000866 delta = 4;
867 }
868 }
869
Dale Johannesen99c49a42007-02-25 00:47:03 +0000870 // All BBOffsets following these blocks must be modified.
Dale Johannesen8086d582010-07-23 22:50:23 +0000871 if (delta)
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +0000872 AdjustBBOffsetsAfter(NewBB);
Evan Cheng0c615842007-01-31 02:22:22 +0000873
874 return NewBB;
Evan Chenga8e29892007-01-19 07:51:42 +0000875}
876
Dale Johannesen8593e412007-04-29 19:19:30 +0000877/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
Bob Wilson84945262009-05-12 17:09:30 +0000878/// reference) is within MaxDisp of TrialOffset (a proposed location of a
Dale Johannesen8593e412007-04-29 19:19:30 +0000879/// constant pool entry).
Bob Wilson84945262009-05-12 17:09:30 +0000880bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000881 unsigned TrialOffset, unsigned MaxDisp,
882 bool NegativeOK, bool IsSoImm) {
Bob Wilson84945262009-05-12 17:09:30 +0000883 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
884 // purposes of the displacement computation; compensate for that here.
Dale Johannesen8593e412007-04-29 19:19:30 +0000885 // Effectively, the valid range of displacements is 2 bytes smaller for such
886 // references.
Evan Cheng31b99dd2009-08-14 18:31:44 +0000887 unsigned TotalAdj = 0;
888 if (isThumb && UserOffset%4 !=0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000889 UserOffset -= 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +0000890 TotalAdj = 2;
891 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000892 // CPEs will be rounded up to a multiple of 4.
Evan Cheng31b99dd2009-08-14 18:31:44 +0000893 if (isThumb && TrialOffset%4 != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000894 TrialOffset += 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +0000895 TotalAdj += 2;
896 }
897
898 // In Thumb2 mode, later branch adjustments can shift instructions up and
899 // cause alignment change. In the worst case scenario this can cause the
900 // user's effective address to be subtracted by 2 and the CPE's address to
901 // be plus 2.
902 if (isThumb2 && TotalAdj != 4)
903 MaxDisp -= (4 - TotalAdj);
Dale Johannesen8593e412007-04-29 19:19:30 +0000904
Dale Johannesen99c49a42007-02-25 00:47:03 +0000905 if (UserOffset <= TrialOffset) {
906 // User before the Trial.
Evan Chengd3d9d662009-07-23 18:27:47 +0000907 if (TrialOffset - UserOffset <= MaxDisp)
908 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000909 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000910 } else if (NegativeOK) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000911 if (UserOffset - TrialOffset <= MaxDisp)
912 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000913 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000914 }
915 return false;
916}
917
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000918/// WaterIsInRange - Returns true if a CPE placed after the specified
919/// Water (a basic block) will be in range for the specific MI.
920
921bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000922 MachineBasicBlock* Water, CPUser &U) {
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000923 unsigned MaxDisp = U.MaxDisp;
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +0000924 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset();
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000925
Dale Johannesend959aa42007-04-02 20:31:06 +0000926 // If the CPE is to be inserted before the instruction, that will raise
Bob Wilsonaf4b7352009-10-12 22:49:05 +0000927 // the offset of the instruction.
Dale Johannesend959aa42007-04-02 20:31:06 +0000928 if (CPEOffset < UserOffset)
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000929 UserOffset += U.CPEMI->getOperand(2).getImm();
Dale Johannesend959aa42007-04-02 20:31:06 +0000930
Evan Chengd3d9d662009-07-23 18:27:47 +0000931 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000932}
933
934/// CPEIsInRange - Returns true if the distance between specific MI and
Evan Chengc0dbec72007-01-31 19:57:44 +0000935/// specific ConstPool entry instruction can fit in MI's displacement field.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000936bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000937 MachineInstr *CPEMI, unsigned MaxDisp,
938 bool NegOk, bool DoDump) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000939 unsigned CPEOffset = GetOffsetOf(CPEMI);
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000940 assert((CPEOffset%4 == 0 || HasInlineAsm) && "Misaligned CPE");
Evan Cheng2021abe2007-02-01 01:09:47 +0000941
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000942 if (DoDump) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000943 DEBUG(errs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
944 << " max delta=" << MaxDisp
945 << " insn address=" << UserOffset
946 << " CPE address=" << CPEOffset
947 << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000948 }
Evan Chengc0dbec72007-01-31 19:57:44 +0000949
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000950 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
Evan Chengc0dbec72007-01-31 19:57:44 +0000951}
952
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000953#ifndef NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000954/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
955/// unconditionally branches to its only successor.
956static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
957 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
958 return false;
959
960 MachineBasicBlock *Succ = *MBB->succ_begin();
961 MachineBasicBlock *Pred = *MBB->pred_begin();
962 MachineInstr *PredMI = &Pred->back();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000963 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
964 || PredMI->getOpcode() == ARM::t2B)
Evan Chengc99ef082007-02-09 20:54:44 +0000965 return PredMI->getOperand(0).getMBB() == Succ;
966 return false;
967}
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000968#endif // NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000969
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +0000970void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB) {
Chris Lattner7896c9f2009-12-03 00:50:42 +0000971 MachineFunction::iterator MBBI = BB; MBBI = llvm::next(MBBI);
Evan Chengd3d9d662009-07-23 18:27:47 +0000972 for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs();
973 i < e; ++i) {
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +0000974 unsigned OldOffset = BBInfo[i].Offset;
975 BBInfo[i].Offset = BBInfo[i-1].postOffset();
976 int delta = BBInfo[i].Offset - OldOffset;
Dale Johannesen8593e412007-04-29 19:19:30 +0000977 // If some existing blocks have padding, adjust the padding as needed, a
978 // bit tricky. delta can be negative so don't use % on that.
Evan Chengd3d9d662009-07-23 18:27:47 +0000979 if (!isThumb)
980 continue;
981 MachineBasicBlock *MBB = MBBI;
Jim Grosbach4d8e90a2009-11-19 23:10:28 +0000982 if (!MBB->empty() && !HasInlineAsm) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000983 // Constant pool entries require padding.
984 if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000985 if ((OldOffset%4) == 0 && (BBInfo[i].Offset%4) != 0) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000986 // add new padding
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000987 BBInfo[i].Size += 2;
Evan Chengd3d9d662009-07-23 18:27:47 +0000988 delta += 2;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000989 } else if ((OldOffset%4) != 0 && (BBInfo[i].Offset%4) == 0) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000990 // remove existing padding
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +0000991 BBInfo[i].Size -= 2;
Evan Chengd3d9d662009-07-23 18:27:47 +0000992 delta -= 2;
Dale Johannesen8593e412007-04-29 19:19:30 +0000993 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000994 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000995 // Thumb1 jump tables require padding. They should be at the end;
996 // following unconditional branches are removed by AnalyzeBranch.
Evan Chengb1c857b2010-07-22 02:09:47 +0000997 // tBR_JTr expands to a mov pc followed by .align 2 and then the jump
Dale Johannesen8086d582010-07-23 22:50:23 +0000998 // table entries. So this code checks whether offset of tBR_JTr
999 // is aligned; if it is, the offset of the jump table following the
1000 // instruction will not be aligned, and we need padding.
Evan Cheng78947622009-07-24 18:20:44 +00001001 MachineInstr *ThumbJTMI = prior(MBB->end());
Evan Cheng66ac5312009-07-25 00:33:29 +00001002 if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) {
Dale Johannesen8086d582010-07-23 22:50:23 +00001003 unsigned NewMIOffset = GetOffsetOf(ThumbJTMI);
Evan Cheng4a8ea212009-08-11 07:36:14 +00001004 unsigned OldMIOffset = NewMIOffset - delta;
1005 if ((OldMIOffset%4) == 0 && (NewMIOffset%4) != 0) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001006 // remove existing padding
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001007 BBInfo[i].Size -= 2;
Evan Cheng4a8ea212009-08-11 07:36:14 +00001008 } else if ((OldMIOffset%4) != 0 && (NewMIOffset%4) == 0) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001009 // add new padding
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001010 BBInfo[i].Size += 2;
Evan Chengd3d9d662009-07-23 18:27:47 +00001011 }
1012 }
Dale Johannesen8593e412007-04-29 19:19:30 +00001013 }
Chris Lattner7896c9f2009-12-03 00:50:42 +00001014 MBBI = llvm::next(MBBI);
Dale Johannesen8593e412007-04-29 19:19:30 +00001015 }
Dale Johannesen99c49a42007-02-25 00:47:03 +00001016}
1017
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001018/// DecrementOldEntry - find the constant pool entry with index CPI
1019/// and instruction CPEMI, and decrement its refcount. If the refcount
Bob Wilson84945262009-05-12 17:09:30 +00001020/// becomes 0 remove the entry and instruction. Returns true if we removed
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001021/// the entry, false if we didn't.
Evan Chenga8e29892007-01-19 07:51:42 +00001022
Evan Chenged884f32007-04-03 23:39:48 +00001023bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
Evan Chengc99ef082007-02-09 20:54:44 +00001024 // Find the old entry. Eliminate it if it is no longer used.
Evan Chenged884f32007-04-03 23:39:48 +00001025 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1026 assert(CPE && "Unexpected!");
1027 if (--CPE->RefCount == 0) {
1028 RemoveDeadCPEMI(CPEMI);
1029 CPE->CPEMI = NULL;
Dan Gohmanfe601042010-06-22 15:08:57 +00001030 --NumCPEs;
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001031 return true;
1032 }
1033 return false;
1034}
1035
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001036/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1037/// if not, see if an in-range clone of the CPE is in range, and if so,
1038/// change the data structures so the user references the clone. Returns:
1039/// 0 = no existing entry found
1040/// 1 = entry found, and there were no code insertions or deletions
1041/// 2 = entry found, and there were code insertions or deletions
1042int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
1043{
1044 MachineInstr *UserMI = U.MI;
1045 MachineInstr *CPEMI = U.CPEMI;
1046
1047 // Check to see if the CPE is already in-range.
Evan Cheng5d8f1ca2009-07-21 23:56:01 +00001048 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
Chris Lattner893e1c92009-08-23 06:49:22 +00001049 DEBUG(errs() << "In range\n");
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001050 return 1;
Evan Chengc99ef082007-02-09 20:54:44 +00001051 }
1052
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001053 // No. Look for previously created clones of the CPE that are in range.
Chris Lattner8aa797a2007-12-30 23:10:15 +00001054 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001055 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1056 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1057 // We already tried this one
1058 if (CPEs[i].CPEMI == CPEMI)
1059 continue;
1060 // Removing CPEs can leave empty entries, skip
1061 if (CPEs[i].CPEMI == NULL)
1062 continue;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +00001063 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
Chris Lattner893e1c92009-08-23 06:49:22 +00001064 DEBUG(errs() << "Replacing CPE#" << CPI << " with CPE#"
1065 << CPEs[i].CPI << "\n");
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001066 // Point the CPUser node to the replacement
1067 U.CPEMI = CPEs[i].CPEMI;
1068 // Change the CPI in the instruction operand to refer to the clone.
1069 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
Dan Gohmand735b802008-10-03 15:45:36 +00001070 if (UserMI->getOperand(j).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001071 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001072 break;
1073 }
1074 // Adjust the refcount of the clone...
1075 CPEs[i].RefCount++;
1076 // ...and the original. If we didn't remove the old entry, none of the
1077 // addresses changed, so we don't need another pass.
Evan Chenged884f32007-04-03 23:39:48 +00001078 return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001079 }
1080 }
1081 return 0;
1082}
1083
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001084/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1085/// the specific unconditional branch instruction.
1086static inline unsigned getUnconditionalBrDisp(int Opc) {
David Goodwin5e47a9a2009-06-30 18:04:13 +00001087 switch (Opc) {
1088 case ARM::tB:
1089 return ((1<<10)-1)*2;
1090 case ARM::t2B:
1091 return ((1<<23)-1)*2;
1092 default:
1093 break;
1094 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001095
David Goodwin5e47a9a2009-06-30 18:04:13 +00001096 return ((1<<23)-1)*4;
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001097}
1098
Bob Wilsonb9239532009-10-15 20:49:47 +00001099/// LookForWater - Look for an existing entry in the WaterList in which
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001100/// we can place the CPE referenced from U so it's within range of U's MI.
Bob Wilsonb9239532009-10-15 20:49:47 +00001101/// Returns true if found, false if not. If it returns true, WaterIter
Bob Wilsonf98032e2009-10-12 21:23:15 +00001102/// is set to the WaterList entry. For Thumb, prefer water that will not
1103/// introduce padding to water that will. To ensure that this pass
1104/// terminates, the CPE location for a particular CPUser is only allowed to
1105/// move to a lower address, so search backward from the end of the list and
1106/// prefer the first water that is in range.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001107bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
Bob Wilsonb9239532009-10-15 20:49:47 +00001108 water_iterator &WaterIter) {
Bob Wilson3b757352009-10-12 19:04:03 +00001109 if (WaterList.empty())
1110 return false;
1111
Bob Wilson32c50e82009-10-12 20:45:53 +00001112 bool FoundWaterThatWouldPad = false;
1113 water_iterator IPThatWouldPad;
Bob Wilson3b757352009-10-12 19:04:03 +00001114 for (water_iterator IP = prior(WaterList.end()),
1115 B = WaterList.begin();; --IP) {
1116 MachineBasicBlock* WaterBB = *IP;
Bob Wilsonb9239532009-10-15 20:49:47 +00001117 // Check if water is in range and is either at a lower address than the
1118 // current "high water mark" or a new water block that was created since
1119 // the previous iteration by inserting an unconditional branch. In the
1120 // latter case, we want to allow resetting the high water mark back to
1121 // this new water since we haven't seen it before. Inserting branches
1122 // should be relatively uncommon and when it does happen, we want to be
1123 // sure to take advantage of it for all the CPEs near that block, so that
1124 // we don't insert more branches than necessary.
1125 if (WaterIsInRange(UserOffset, WaterBB, U) &&
1126 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1127 NewWaterList.count(WaterBB))) {
Bob Wilson3b757352009-10-12 19:04:03 +00001128 unsigned WBBId = WaterBB->getNumber();
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +00001129 if (isThumb && BBInfo[WBBId].postOffset()%4 != 0) {
Bob Wilson3b757352009-10-12 19:04:03 +00001130 // This is valid Water, but would introduce padding. Remember
1131 // it in case we don't find any Water that doesn't do this.
Bob Wilson32c50e82009-10-12 20:45:53 +00001132 if (!FoundWaterThatWouldPad) {
1133 FoundWaterThatWouldPad = true;
Bob Wilson3b757352009-10-12 19:04:03 +00001134 IPThatWouldPad = IP;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001135 }
Bob Wilson3b757352009-10-12 19:04:03 +00001136 } else {
Bob Wilsonb9239532009-10-15 20:49:47 +00001137 WaterIter = IP;
Bob Wilson3b757352009-10-12 19:04:03 +00001138 return true;
Evan Chengd3d9d662009-07-23 18:27:47 +00001139 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001140 }
Bob Wilson3b757352009-10-12 19:04:03 +00001141 if (IP == B)
1142 break;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001143 }
Bob Wilson32c50e82009-10-12 20:45:53 +00001144 if (FoundWaterThatWouldPad) {
Bob Wilsonb9239532009-10-15 20:49:47 +00001145 WaterIter = IPThatWouldPad;
Dale Johannesen8593e412007-04-29 19:19:30 +00001146 return true;
1147 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001148 return false;
1149}
1150
Bob Wilson84945262009-05-12 17:09:30 +00001151/// CreateNewWater - No existing WaterList entry will work for
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001152/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1153/// block is used if in range, and the conditional branch munged so control
1154/// flow is correct. Otherwise the block is split to create a hole with an
Bob Wilson757652c2009-10-12 21:39:43 +00001155/// unconditional branch around it. In either case NewMBB is set to a
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001156/// block following which the new island can be inserted (the WaterList
1157/// is not adjusted).
Bob Wilson84945262009-05-12 17:09:30 +00001158void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
Bob Wilson757652c2009-10-12 21:39:43 +00001159 unsigned UserOffset,
1160 MachineBasicBlock *&NewMBB) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001161 CPUser &U = CPUsers[CPUserIndex];
1162 MachineInstr *UserMI = U.MI;
1163 MachineInstr *CPEMI = U.CPEMI;
1164 MachineBasicBlock *UserMBB = UserMI->getParent();
Jakob Stoklund Olesen5bb32532011-12-07 01:22:52 +00001165 unsigned OffsetOfNextBlock = BBInfo[UserMBB->getNumber()].postOffset();
1166 assert(OffsetOfNextBlock == BBInfo[UserMBB->getNumber()+1].Offset);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001167
Bob Wilson36fa5322009-10-15 05:10:36 +00001168 // If the block does not end in an unconditional branch already, and if the
1169 // end of the block is within range, make new water there. (The addition
1170 // below is for the unconditional branch we will be adding: 4 bytes on ARM +
1171 // Thumb2, 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
Dale Johannesen8593e412007-04-29 19:19:30 +00001172 // inside OffsetIsInRange.
Bob Wilson36fa5322009-10-15 05:10:36 +00001173 if (BBHasFallthrough(UserMBB) &&
Evan Chengd3d9d662009-07-23 18:27:47 +00001174 OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4),
1175 U.MaxDisp, U.NegOk, U.IsSoImm)) {
Chris Lattner893e1c92009-08-23 06:49:22 +00001176 DEBUG(errs() << "Split at end of block\n");
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001177 if (&UserMBB->back() == UserMI)
1178 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
Chris Lattner7896c9f2009-12-03 00:50:42 +00001179 NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001180 // Add an unconditional branch from UserMBB to fallthrough block.
1181 // Record it for branch lengthening; this new branch will not get out of
1182 // range, but if the preceding conditional branch is out of range, the
1183 // targets will be exchanged, and the altered branch may be out of
1184 // range, so the machinery has to know about it.
David Goodwin5e47a9a2009-06-30 18:04:13 +00001185 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001186 if (!isThumb)
1187 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1188 else
1189 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
1190 .addImm(ARMCC::AL).addReg(0);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001191 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
Bob Wilson84945262009-05-12 17:09:30 +00001192 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001193 MaxDisp, false, UncondBr));
Evan Chengd3d9d662009-07-23 18:27:47 +00001194 int delta = isThumb1 ? 2 : 4;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001195 BBInfo[UserMBB->getNumber()].Size += delta;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001196 AdjustBBOffsetsAfter(UserMBB);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001197 } else {
1198 // What a big block. Find a place within the block to split it.
Evan Chengd3d9d662009-07-23 18:27:47 +00001199 // This is a little tricky on Thumb1 since instructions are 2 bytes
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001200 // and constant pool entries are 4 bytes: if instruction I references
1201 // island CPE, and instruction I+1 references CPE', it will
1202 // not work well to put CPE as far forward as possible, since then
1203 // CPE' cannot immediately follow it (that location is 2 bytes
1204 // farther away from I+1 than CPE was from I) and we'd need to create
Dale Johannesen8593e412007-04-29 19:19:30 +00001205 // a new island. So, we make a first guess, then walk through the
1206 // instructions between the one currently being looked at and the
1207 // possible insertion point, and make sure any other instructions
1208 // that reference CPEs will be able to use the same island area;
1209 // if not, we back up the insertion point.
1210
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001211 // The 4 in the following is for the unconditional branch we'll be
Evan Chengd3d9d662009-07-23 18:27:47 +00001212 // inserting (allows for long branch on Thumb1). Alignment of the
Dale Johannesen8593e412007-04-29 19:19:30 +00001213 // island is handled inside OffsetIsInRange.
1214 unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001215 // This could point off the end of the block if we've already got
1216 // constant pool entries following this block; only the last one is
1217 // in the water list. Back past any possible branches (allow for a
1218 // conditional and a maximally long unconditional).
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001219 if (BaseInsertOffset >= BBInfo[UserMBB->getNumber()+1].Offset)
1220 BaseInsertOffset = BBInfo[UserMBB->getNumber()+1].Offset -
Evan Chengd3d9d662009-07-23 18:27:47 +00001221 (isThumb1 ? 6 : 8);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001222 unsigned EndInsertOffset = BaseInsertOffset +
1223 CPEMI->getOperand(2).getImm();
1224 MachineBasicBlock::iterator MI = UserMI;
1225 ++MI;
1226 unsigned CPUIndex = CPUserIndex+1;
Evan Cheng719510a2010-08-12 20:30:05 +00001227 unsigned NumCPUsers = CPUsers.size();
1228 MachineInstr *LastIT = 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001229 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001230 Offset < BaseInsertOffset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001231 Offset += TII->GetInstSizeInBytes(MI),
Evan Cheng719510a2010-08-12 20:30:05 +00001232 MI = llvm::next(MI)) {
1233 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001234 CPUser &U = CPUsers[CPUIndex];
Bob Wilson84945262009-05-12 17:09:30 +00001235 if (!OffsetIsInRange(Offset, EndInsertOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +00001236 U.MaxDisp, U.NegOk, U.IsSoImm)) {
1237 BaseInsertOffset -= (isThumb1 ? 2 : 4);
1238 EndInsertOffset -= (isThumb1 ? 2 : 4);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001239 }
1240 // This is overly conservative, as we don't account for CPEMIs
1241 // being reused within the block, but it doesn't matter much.
1242 EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1243 CPUIndex++;
1244 }
Evan Cheng719510a2010-08-12 20:30:05 +00001245
1246 // Remember the last IT instruction.
1247 if (MI->getOpcode() == ARM::t2IT)
1248 LastIT = MI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001249 }
Evan Cheng719510a2010-08-12 20:30:05 +00001250
Chris Lattner893e1c92009-08-23 06:49:22 +00001251 DEBUG(errs() << "Split in middle of big block\n");
Evan Cheng719510a2010-08-12 20:30:05 +00001252 --MI;
1253
1254 // Avoid splitting an IT block.
1255 if (LastIT) {
1256 unsigned PredReg = 0;
1257 ARMCC::CondCodes CC = llvm::getITInstrPredicate(MI, PredReg);
1258 if (CC != ARMCC::AL)
1259 MI = LastIT;
1260 }
1261 NewMBB = SplitBlockBeforeInstr(MI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001262 }
1263}
1264
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001265/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
Bob Wilson39bf0512009-05-12 17:35:29 +00001266/// is out-of-range. If so, pick up the constant pool value and move it some
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001267/// place in-range. Return true if we changed any addresses (thus must run
1268/// another pass of branch lengthening), false otherwise.
Evan Cheng5657c012009-07-29 02:18:14 +00001269bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +00001270 unsigned CPUserIndex) {
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001271 CPUser &U = CPUsers[CPUserIndex];
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001272 MachineInstr *UserMI = U.MI;
1273 MachineInstr *CPEMI = U.CPEMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001274 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001275 unsigned Size = CPEMI->getOperand(2).getImm();
Dale Johannesen8593e412007-04-29 19:19:30 +00001276 // Compute this only once, it's expensive. The 4 or 8 is the value the
Evan Chenga1efbbd2009-08-14 00:32:16 +00001277 // hardware keeps in the PC.
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001278 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
Evan Cheng768c9f72007-04-27 08:14:15 +00001279
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001280 // See if the current entry is within range, or there is a clone of it
1281 // in range.
1282 int result = LookForExistingCPEntry(U, UserOffset);
1283 if (result==1) return false;
1284 else if (result==2) return true;
1285
1286 // No existing clone of this CPE is within range.
1287 // We will be generating a new clone. Get a UID for it.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001288 unsigned ID = AFI->createPICLabelUId();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001289
Bob Wilsonf98032e2009-10-12 21:23:15 +00001290 // Look for water where we can place this CPE.
Bob Wilsonb9239532009-10-15 20:49:47 +00001291 MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock();
1292 MachineBasicBlock *NewMBB;
1293 water_iterator IP;
1294 if (LookForWater(U, UserOffset, IP)) {
1295 DEBUG(errs() << "found water in range\n");
1296 MachineBasicBlock *WaterBB = *IP;
1297
1298 // If the original WaterList entry was "new water" on this iteration,
1299 // propagate that to the new island. This is just keeping NewWaterList
1300 // updated to match the WaterList, which will be updated below.
1301 if (NewWaterList.count(WaterBB)) {
1302 NewWaterList.erase(WaterBB);
1303 NewWaterList.insert(NewIsland);
1304 }
1305 // The new CPE goes before the following block (NewMBB).
Chris Lattner7896c9f2009-12-03 00:50:42 +00001306 NewMBB = llvm::next(MachineFunction::iterator(WaterBB));
Bob Wilsonb9239532009-10-15 20:49:47 +00001307
1308 } else {
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001309 // No water found.
Chris Lattner893e1c92009-08-23 06:49:22 +00001310 DEBUG(errs() << "No water found\n");
Bob Wilson757652c2009-10-12 21:39:43 +00001311 CreateNewWater(CPUserIndex, UserOffset, NewMBB);
Bob Wilsonb9239532009-10-15 20:49:47 +00001312
1313 // SplitBlockBeforeInstr adds to WaterList, which is important when it is
1314 // called while handling branches so that the water will be seen on the
1315 // next iteration for constant pools, but in this context, we don't want
1316 // it. Check for this so it will be removed from the WaterList.
1317 // Also remove any entry from NewWaterList.
1318 MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB));
1319 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1320 if (IP != WaterList.end())
1321 NewWaterList.erase(WaterBB);
1322
1323 // We are adding new water. Update NewWaterList.
1324 NewWaterList.insert(NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001325 }
1326
Bob Wilsonb9239532009-10-15 20:49:47 +00001327 // Remove the original WaterList entry; we want subsequent insertions in
1328 // this vicinity to go after the one we're about to insert. This
1329 // considerably reduces the number of times we have to move the same CPE
1330 // more than once and is also important to ensure the algorithm terminates.
1331 if (IP != WaterList.end())
1332 WaterList.erase(IP);
1333
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001334 // Okay, we know we can put an island before NewMBB now, do it!
Evan Cheng5657c012009-07-29 02:18:14 +00001335 MF.insert(NewMBB, NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001336
1337 // Update internal data structures to account for the newly inserted MBB.
1338 UpdateForInsertedWaterBlock(NewIsland);
1339
1340 // Decrement the old entry, and remove it if refcount becomes 0.
Evan Chenged884f32007-04-03 23:39:48 +00001341 DecrementOldEntry(CPI, CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001342
1343 // Now that we have an island to add the CPE to, clone the original CPE and
1344 // add it to the island.
Bob Wilson549dda92009-10-15 05:52:29 +00001345 U.HighWaterMark = NewIsland;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001346 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +00001347 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001348 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
Dan Gohmanfe601042010-06-22 15:08:57 +00001349 ++NumCPEs;
Evan Chengc99ef082007-02-09 20:54:44 +00001350
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +00001351 // Mark the basic block as 4-byte aligned as required by the const-pool entry.
1352 NewIsland->setAlignment(2);
1353
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001354 BBInfo[NewIsland->getNumber()].Offset = BBInfo[NewMBB->getNumber()].Offset;
Evan Chengb43216e2007-02-01 10:16:15 +00001355 // Compensate for .align 2 in thumb mode.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001356 if (isThumb && (BBInfo[NewIsland->getNumber()].Offset%4 != 0 || HasInlineAsm))
Dale Johannesen8593e412007-04-29 19:19:30 +00001357 Size += 2;
Evan Chenga8e29892007-01-19 07:51:42 +00001358 // Increase the size of the island block to account for the new entry.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001359 BBInfo[NewIsland->getNumber()].Size += Size;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001360 AdjustBBOffsetsAfter(NewIsland);
Bob Wilson84945262009-05-12 17:09:30 +00001361
Evan Chenga8e29892007-01-19 07:51:42 +00001362 // Finally, change the CPI in the instruction operand to be ID.
1363 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +00001364 if (UserMI->getOperand(i).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001365 UserMI->getOperand(i).setIndex(ID);
Evan Chenga8e29892007-01-19 07:51:42 +00001366 break;
1367 }
Bob Wilson84945262009-05-12 17:09:30 +00001368
Chris Lattner705e07f2009-08-23 03:41:05 +00001369 DEBUG(errs() << " Moved CPE to #" << ID << " CPI=" << CPI
1370 << '\t' << *UserMI);
Bob Wilson84945262009-05-12 17:09:30 +00001371
Evan Chenga8e29892007-01-19 07:51:42 +00001372 return true;
1373}
1374
Evan Chenged884f32007-04-03 23:39:48 +00001375/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1376/// sizes and offsets of impacted basic blocks.
1377void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1378 MachineBasicBlock *CPEBB = CPEMI->getParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001379 unsigned Size = CPEMI->getOperand(2).getImm();
1380 CPEMI->eraseFromParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001381 BBInfo[CPEBB->getNumber()].Size -= Size;
Dale Johannesen8593e412007-04-29 19:19:30 +00001382 // All succeeding offsets have the current size value added in, fix this.
Evan Chenged884f32007-04-03 23:39:48 +00001383 if (CPEBB->empty()) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001384 // In thumb1 mode, the size of island may be padded by two to compensate for
Dale Johannesen8593e412007-04-29 19:19:30 +00001385 // the alignment requirement. Then it will now be 2 when the block is
Evan Chenged884f32007-04-03 23:39:48 +00001386 // empty, so fix this.
1387 // All succeeding offsets have the current size value added in, fix this.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001388 if (BBInfo[CPEBB->getNumber()].Size != 0) {
1389 Size += BBInfo[CPEBB->getNumber()].Size;
1390 BBInfo[CPEBB->getNumber()].Size = 0;
Evan Chenged884f32007-04-03 23:39:48 +00001391 }
Jakob Stoklund Olesen305e5fe2011-12-06 21:55:35 +00001392
1393 // This block no longer needs to be aligned. <rdar://problem/10534709>.
1394 CPEBB->setAlignment(0);
Evan Chenged884f32007-04-03 23:39:48 +00001395 }
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001396 AdjustBBOffsetsAfter(CPEBB);
Dale Johannesen8593e412007-04-29 19:19:30 +00001397 // An island has only one predecessor BB and one successor BB. Check if
1398 // this BB's predecessor jumps directly to this BB's successor. This
1399 // shouldn't happen currently.
1400 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1401 // FIXME: remove the empty blocks after all the work is done?
Evan Chenged884f32007-04-03 23:39:48 +00001402}
1403
1404/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1405/// are zero.
1406bool ARMConstantIslands::RemoveUnusedCPEntries() {
1407 unsigned MadeChange = false;
1408 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1409 std::vector<CPEntry> &CPEs = CPEntries[i];
1410 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1411 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1412 RemoveDeadCPEMI(CPEs[j].CPEMI);
1413 CPEs[j].CPEMI = NULL;
1414 MadeChange = true;
1415 }
1416 }
Bob Wilson84945262009-05-12 17:09:30 +00001417 }
Evan Chenged884f32007-04-03 23:39:48 +00001418 return MadeChange;
1419}
1420
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001421/// BBIsInRange - Returns true if the distance between specific MI and
Evan Cheng43aeab62007-01-26 20:38:26 +00001422/// specific BB can fit in MI's displacement field.
Evan Chengc0dbec72007-01-31 19:57:44 +00001423bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1424 unsigned MaxDisp) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001425 unsigned PCAdj = isThumb ? 4 : 8;
Evan Chengc0dbec72007-01-31 19:57:44 +00001426 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001427 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Cheng43aeab62007-01-26 20:38:26 +00001428
Chris Lattner705e07f2009-08-23 03:41:05 +00001429 DEBUG(errs() << "Branch of destination BB#" << DestBB->getNumber()
1430 << " from BB#" << MI->getParent()->getNumber()
1431 << " max delta=" << MaxDisp
1432 << " from " << GetOffsetOf(MI) << " to " << DestOffset
1433 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
Evan Chengc0dbec72007-01-31 19:57:44 +00001434
Dale Johannesen8593e412007-04-29 19:19:30 +00001435 if (BrOffset <= DestOffset) {
1436 // Branch before the Dest.
1437 if (DestOffset-BrOffset <= MaxDisp)
1438 return true;
1439 } else {
1440 if (BrOffset-DestOffset <= MaxDisp)
1441 return true;
1442 }
1443 return false;
Evan Cheng43aeab62007-01-26 20:38:26 +00001444}
1445
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001446/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1447/// away to fit in its displacement field.
Evan Cheng5657c012009-07-29 02:18:14 +00001448bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001449 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001450 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001451
Evan Chengc0dbec72007-01-31 19:57:44 +00001452 // Check to see if the DestBB is already in-range.
1453 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng43aeab62007-01-26 20:38:26 +00001454 return false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001455
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001456 if (!Br.isCond)
Evan Cheng5657c012009-07-29 02:18:14 +00001457 return FixUpUnconditionalBr(MF, Br);
1458 return FixUpConditionalBr(MF, Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001459}
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001460
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001461/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1462/// too far away to fit in its displacement field. If the LR register has been
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001463/// spilled in the epilogue, then we can use BL to implement a far jump.
Bob Wilson39bf0512009-05-12 17:35:29 +00001464/// Otherwise, add an intermediate branch instruction to a branch.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001465bool
Evan Cheng5657c012009-07-29 02:18:14 +00001466ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001467 MachineInstr *MI = Br.MI;
1468 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng53c67c02009-08-07 05:45:07 +00001469 if (!isThumb1)
1470 llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!");
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001471
1472 // Use BL to implement far jump.
1473 Br.MaxDisp = (1 << 21) * 2;
Chris Lattner5080f4d2008-01-11 18:10:50 +00001474 MI->setDesc(TII->get(ARM::tBfar));
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001475 BBInfo[MBB->getNumber()].Size += 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001476 AdjustBBOffsetsAfter(MBB);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001477 HasFarJump = true;
Dan Gohmanfe601042010-06-22 15:08:57 +00001478 ++NumUBrFixed;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001479
Chris Lattner705e07f2009-08-23 03:41:05 +00001480 DEBUG(errs() << " Changed B to long jump " << *MI);
Evan Chengbd5d3db2007-02-03 02:08:34 +00001481
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001482 return true;
1483}
1484
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001485/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001486/// far away to fit in its displacement field. It is converted to an inverse
1487/// conditional branch + an unconditional branch to the destination.
1488bool
Evan Cheng5657c012009-07-29 02:18:14 +00001489ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001490 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001491 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001492
Bob Wilson39bf0512009-05-12 17:35:29 +00001493 // Add an unconditional branch to the destination and invert the branch
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001494 // condition to jump over it:
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001495 // blt L1
1496 // =>
1497 // bge L2
1498 // b L1
1499 // L2:
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001500 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001501 CC = ARMCC::getOppositeCondition(CC);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001502 unsigned CCReg = MI->getOperand(2).getReg();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001503
1504 // If the branch is at the end of its MBB and that has a fall-through block,
1505 // direct the updated conditional branch to the fall-through block. Otherwise,
1506 // split the MBB before the next instruction.
1507 MachineBasicBlock *MBB = MI->getParent();
Evan Chengbd5d3db2007-02-03 02:08:34 +00001508 MachineInstr *BMI = &MBB->back();
1509 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng43aeab62007-01-26 20:38:26 +00001510
Dan Gohmanfe601042010-06-22 15:08:57 +00001511 ++NumCBrFixed;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001512 if (BMI != MI) {
Chris Lattner7896c9f2009-12-03 00:50:42 +00001513 if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
Evan Chengbd5d3db2007-02-03 02:08:34 +00001514 BMI->getOpcode() == Br.UncondBr) {
Bob Wilson39bf0512009-05-12 17:35:29 +00001515 // Last MI in the BB is an unconditional branch. Can we simply invert the
Evan Cheng43aeab62007-01-26 20:38:26 +00001516 // condition and swap destinations:
1517 // beq L1
1518 // b L2
1519 // =>
1520 // bne L2
1521 // b L1
Chris Lattner8aa797a2007-12-30 23:10:15 +00001522 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
Evan Chengc0dbec72007-01-31 19:57:44 +00001523 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Chris Lattner705e07f2009-08-23 03:41:05 +00001524 DEBUG(errs() << " Invert Bcc condition and swap its destination with "
1525 << *BMI);
Chris Lattner8aa797a2007-12-30 23:10:15 +00001526 BMI->getOperand(0).setMBB(DestBB);
1527 MI->getOperand(0).setMBB(NewDest);
Evan Cheng43aeab62007-01-26 20:38:26 +00001528 MI->getOperand(1).setImm(CC);
1529 return true;
1530 }
1531 }
1532 }
1533
1534 if (NeedSplit) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001535 SplitBlockBeforeInstr(MI);
Bob Wilson39bf0512009-05-12 17:35:29 +00001536 // No need for the branch to the next block. We're adding an unconditional
Evan Chengdd353b82007-01-26 02:02:39 +00001537 // branch to the destination.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001538 int delta = TII->GetInstSizeInBytes(&MBB->back());
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001539 BBInfo[MBB->getNumber()].Size -= delta;
Evan Chengdd353b82007-01-26 02:02:39 +00001540 MBB->back().eraseFromParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001541 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
Evan Chengdd353b82007-01-26 02:02:39 +00001542 }
Chris Lattner7896c9f2009-12-03 00:50:42 +00001543 MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB));
Bob Wilson84945262009-05-12 17:09:30 +00001544
Chris Lattner893e1c92009-08-23 06:49:22 +00001545 DEBUG(errs() << " Insert B to BB#" << DestBB->getNumber()
1546 << " also invert condition and change dest. to BB#"
1547 << NextBB->getNumber() << "\n");
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001548
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001549 // Insert a new conditional branch and a new unconditional branch.
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001550 // Also update the ImmBranch as well as adding a new entry for the new branch.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001551 BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
Dale Johannesenb6728402009-02-13 02:25:56 +00001552 .addMBB(NextBB).addImm(CC).addReg(CCReg);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001553 Br.MI = &MBB->back();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001554 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Owen Andersoncd4338f2011-09-09 23:05:14 +00001555 if (isThumb)
1556 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
1557 .addImm(ARMCC::AL).addReg(0);
1558 else
1559 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001560 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Evan Chenga9b8b8d2007-01-31 18:29:27 +00001561 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Chenga0bf7942007-01-25 23:31:04 +00001562 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001563
1564 // Remove the old conditional branch. It may or may not still be in MBB.
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001565 BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001566 MI->eraseFromParent();
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001567 AdjustBBOffsetsAfter(MBB);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001568 return true;
1569}
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001570
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001571/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
Evan Cheng4b322e52009-08-11 21:11:32 +00001572/// LR / restores LR to pc. FIXME: This is done here because it's only possible
1573/// to do this if tBfar is not used.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001574bool ARMConstantIslands::UndoLRSpillRestore() {
1575 bool MadeChange = false;
1576 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1577 MachineInstr *MI = PushPopMIs[i];
Bob Wilson815baeb2010-03-13 01:08:20 +00001578 // First two operands are predicates.
Evan Cheng44bec522007-05-15 01:29:07 +00001579 if (MI->getOpcode() == ARM::tPOP_RET &&
Bob Wilson815baeb2010-03-13 01:08:20 +00001580 MI->getOperand(2).getReg() == ARM::PC &&
1581 MI->getNumExplicitOperands() == 3) {
Jim Grosbach25e6d482011-07-08 21:50:04 +00001582 // Create the new insn and copy the predicate from the old.
1583 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET))
1584 .addOperand(MI->getOperand(0))
1585 .addOperand(MI->getOperand(1));
Evan Cheng44bec522007-05-15 01:29:07 +00001586 MI->eraseFromParent();
1587 MadeChange = true;
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001588 }
1589 }
1590 return MadeChange;
1591}
Evan Cheng5657c012009-07-29 02:18:14 +00001592
Evan Chenga1efbbd2009-08-14 00:32:16 +00001593bool ARMConstantIslands::OptimizeThumb2Instructions(MachineFunction &MF) {
1594 bool MadeChange = false;
1595
1596 // Shrink ADR and LDR from constantpool.
1597 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
1598 CPUser &U = CPUsers[i];
1599 unsigned Opcode = U.MI->getOpcode();
1600 unsigned NewOpc = 0;
1601 unsigned Scale = 1;
1602 unsigned Bits = 0;
1603 switch (Opcode) {
1604 default: break;
Owen Anderson6b8719f2010-12-13 22:51:08 +00001605 case ARM::t2LEApcrel:
Evan Chenga1efbbd2009-08-14 00:32:16 +00001606 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1607 NewOpc = ARM::tLEApcrel;
1608 Bits = 8;
1609 Scale = 4;
1610 }
1611 break;
1612 case ARM::t2LDRpci:
1613 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1614 NewOpc = ARM::tLDRpci;
1615 Bits = 8;
1616 Scale = 4;
1617 }
1618 break;
1619 }
1620
1621 if (!NewOpc)
1622 continue;
1623
1624 unsigned UserOffset = GetOffsetOf(U.MI) + 4;
1625 unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
1626 // FIXME: Check if offset is multiple of scale if scale is not 4.
1627 if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
1628 U.MI->setDesc(TII->get(NewOpc));
1629 MachineBasicBlock *MBB = U.MI->getParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001630 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001631 AdjustBBOffsetsAfter(MBB);
Evan Chenga1efbbd2009-08-14 00:32:16 +00001632 ++NumT2CPShrunk;
1633 MadeChange = true;
1634 }
1635 }
1636
Evan Chenga1efbbd2009-08-14 00:32:16 +00001637 MadeChange |= OptimizeThumb2Branches(MF);
Jim Grosbach01dec0e2009-11-12 03:28:35 +00001638 MadeChange |= OptimizeThumb2JumpTables(MF);
Evan Chenga1efbbd2009-08-14 00:32:16 +00001639 return MadeChange;
1640}
1641
1642bool ARMConstantIslands::OptimizeThumb2Branches(MachineFunction &MF) {
Evan Cheng31b99dd2009-08-14 18:31:44 +00001643 bool MadeChange = false;
1644
1645 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
1646 ImmBranch &Br = ImmBranches[i];
1647 unsigned Opcode = Br.MI->getOpcode();
1648 unsigned NewOpc = 0;
1649 unsigned Scale = 1;
1650 unsigned Bits = 0;
1651 switch (Opcode) {
1652 default: break;
1653 case ARM::t2B:
1654 NewOpc = ARM::tB;
1655 Bits = 11;
1656 Scale = 2;
1657 break;
Evan Chengde17fb62009-10-31 23:46:45 +00001658 case ARM::t2Bcc: {
Evan Cheng31b99dd2009-08-14 18:31:44 +00001659 NewOpc = ARM::tBcc;
1660 Bits = 8;
Evan Chengde17fb62009-10-31 23:46:45 +00001661 Scale = 2;
Evan Cheng31b99dd2009-08-14 18:31:44 +00001662 break;
1663 }
Evan Chengde17fb62009-10-31 23:46:45 +00001664 }
1665 if (NewOpc) {
1666 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
1667 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
1668 if (BBIsInRange(Br.MI, DestBB, MaxOffs)) {
1669 Br.MI->setDesc(TII->get(NewOpc));
1670 MachineBasicBlock *MBB = Br.MI->getParent();
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001671 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001672 AdjustBBOffsetsAfter(MBB);
Evan Chengde17fb62009-10-31 23:46:45 +00001673 ++NumT2BrShrunk;
1674 MadeChange = true;
1675 }
1676 }
1677
1678 Opcode = Br.MI->getOpcode();
1679 if (Opcode != ARM::tBcc)
Evan Cheng31b99dd2009-08-14 18:31:44 +00001680 continue;
1681
Evan Chengde17fb62009-10-31 23:46:45 +00001682 NewOpc = 0;
1683 unsigned PredReg = 0;
1684 ARMCC::CondCodes Pred = llvm::getInstrPredicate(Br.MI, PredReg);
1685 if (Pred == ARMCC::EQ)
1686 NewOpc = ARM::tCBZ;
1687 else if (Pred == ARMCC::NE)
1688 NewOpc = ARM::tCBNZ;
1689 if (!NewOpc)
1690 continue;
Evan Cheng31b99dd2009-08-14 18:31:44 +00001691 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
Evan Chengde17fb62009-10-31 23:46:45 +00001692 // Check if the distance is within 126. Subtract starting offset by 2
1693 // because the cmp will be eliminated.
1694 unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001695 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Chengde17fb62009-10-31 23:46:45 +00001696 if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
Evan Cheng0539c152011-04-01 22:09:28 +00001697 MachineBasicBlock::iterator CmpMI = Br.MI;
1698 if (CmpMI != Br.MI->getParent()->begin()) {
1699 --CmpMI;
1700 if (CmpMI->getOpcode() == ARM::tCMPi8) {
1701 unsigned Reg = CmpMI->getOperand(0).getReg();
1702 Pred = llvm::getInstrPredicate(CmpMI, PredReg);
1703 if (Pred == ARMCC::AL &&
1704 CmpMI->getOperand(1).getImm() == 0 &&
1705 isARMLowRegister(Reg)) {
1706 MachineBasicBlock *MBB = Br.MI->getParent();
1707 MachineInstr *NewBR =
1708 BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc))
1709 .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags());
1710 CmpMI->eraseFromParent();
1711 Br.MI->eraseFromParent();
1712 Br.MI = NewBR;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001713 BBInfo[MBB->getNumber()].Size -= 2;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001714 AdjustBBOffsetsAfter(MBB);
Evan Cheng0539c152011-04-01 22:09:28 +00001715 ++NumCBZ;
1716 MadeChange = true;
1717 }
Evan Chengde17fb62009-10-31 23:46:45 +00001718 }
1719 }
Evan Cheng31b99dd2009-08-14 18:31:44 +00001720 }
1721 }
1722
1723 return MadeChange;
Evan Chenga1efbbd2009-08-14 00:32:16 +00001724}
1725
Evan Chenga1efbbd2009-08-14 00:32:16 +00001726/// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
1727/// jumptables when it's possible.
Evan Cheng5657c012009-07-29 02:18:14 +00001728bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) {
1729 bool MadeChange = false;
1730
1731 // FIXME: After the tables are shrunk, can we get rid some of the
1732 // constantpool tables?
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001733 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +00001734 if (MJTI == 0) return false;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001735
Evan Cheng5657c012009-07-29 02:18:14 +00001736 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1737 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1738 MachineInstr *MI = T2JumpTables[i];
Evan Chenge837dea2011-06-28 19:10:37 +00001739 const MCInstrDesc &MCID = MI->getDesc();
1740 unsigned NumOps = MCID.getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001741 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
Evan Cheng5657c012009-07-29 02:18:14 +00001742 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1743 unsigned JTI = JTOP.getIndex();
1744 assert(JTI < JT.size());
1745
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001746 bool ByteOk = true;
1747 bool HalfWordOk = true;
Jim Grosbach80697d12009-11-12 17:25:07 +00001748 unsigned JTOffset = GetOffsetOf(MI) + 4;
1749 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Evan Cheng5657c012009-07-29 02:18:14 +00001750 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1751 MachineBasicBlock *MBB = JTBBs[j];
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001752 unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
Evan Cheng8770f742009-07-29 23:20:20 +00001753 // Negative offset is not ok. FIXME: We should change BB layout to make
1754 // sure all the branches are forward.
Evan Chengd26b14c2009-07-31 18:28:05 +00001755 if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
Evan Cheng5657c012009-07-29 02:18:14 +00001756 ByteOk = false;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001757 unsigned TBHLimit = ((1<<16)-1)*2;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001758 if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
Evan Cheng5657c012009-07-29 02:18:14 +00001759 HalfWordOk = false;
1760 if (!ByteOk && !HalfWordOk)
1761 break;
1762 }
1763
1764 if (ByteOk || HalfWordOk) {
1765 MachineBasicBlock *MBB = MI->getParent();
1766 unsigned BaseReg = MI->getOperand(0).getReg();
1767 bool BaseRegKill = MI->getOperand(0).isKill();
1768 if (!BaseRegKill)
1769 continue;
1770 unsigned IdxReg = MI->getOperand(1).getReg();
1771 bool IdxRegKill = MI->getOperand(1).isKill();
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001772
1773 // Scan backwards to find the instruction that defines the base
1774 // register. Due to post-RA scheduling, we can't count on it
1775 // immediately preceding the branch instruction.
Evan Cheng5657c012009-07-29 02:18:14 +00001776 MachineBasicBlock::iterator PrevI = MI;
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001777 MachineBasicBlock::iterator B = MBB->begin();
1778 while (PrevI != B && !PrevI->definesRegister(BaseReg))
1779 --PrevI;
1780
1781 // If for some reason we didn't find it, we can't do anything, so
1782 // just skip this one.
1783 if (!PrevI->definesRegister(BaseReg))
Evan Cheng5657c012009-07-29 02:18:14 +00001784 continue;
1785
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001786 MachineInstr *AddrMI = PrevI;
Evan Cheng5657c012009-07-29 02:18:14 +00001787 bool OptOk = true;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001788 // Examine the instruction that calculates the jumptable entry address.
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001789 // Make sure it only defines the base register and kills any uses
1790 // other than the index register.
Evan Cheng5657c012009-07-29 02:18:14 +00001791 for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
1792 const MachineOperand &MO = AddrMI->getOperand(k);
1793 if (!MO.isReg() || !MO.getReg())
1794 continue;
1795 if (MO.isDef() && MO.getReg() != BaseReg) {
1796 OptOk = false;
1797 break;
1798 }
1799 if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
1800 OptOk = false;
1801 break;
1802 }
1803 }
1804 if (!OptOk)
1805 continue;
1806
Owen Anderson6b8719f2010-12-13 22:51:08 +00001807 // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001808 // that gave us the initial base register definition.
1809 for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI)
1810 ;
1811
Owen Anderson6b8719f2010-12-13 22:51:08 +00001812 // The instruction should be a tLEApcrel or t2LEApcrelJT; we want
Evan Chenga1efbbd2009-08-14 00:32:16 +00001813 // to delete it as well.
Jim Grosbachc7937ae2010-07-07 22:51:22 +00001814 MachineInstr *LeaMI = PrevI;
Evan Chenga1efbbd2009-08-14 00:32:16 +00001815 if ((LeaMI->getOpcode() != ARM::tLEApcrelJT &&
Owen Anderson6b8719f2010-12-13 22:51:08 +00001816 LeaMI->getOpcode() != ARM::t2LEApcrelJT) ||
Evan Cheng5657c012009-07-29 02:18:14 +00001817 LeaMI->getOperand(0).getReg() != BaseReg)
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001818 OptOk = false;
Evan Cheng5657c012009-07-29 02:18:14 +00001819
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001820 if (!OptOk)
1821 continue;
1822
Jim Grosbachd092a872010-11-29 21:28:32 +00001823 unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001824 MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
1825 .addReg(IdxReg, getKillRegState(IdxRegKill))
1826 .addJumpTableIndex(JTI, JTOP.getTargetFlags())
1827 .addImm(MI->getOperand(JTOpIdx+1).getImm());
1828 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1829 // is 2-byte aligned. For now, asm printer will fix it up.
1830 unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
1831 unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
1832 OrigSize += TII->GetInstSizeInBytes(LeaMI);
1833 OrigSize += TII->GetInstSizeInBytes(MI);
1834
1835 AddrMI->eraseFromParent();
1836 LeaMI->eraseFromParent();
1837 MI->eraseFromParent();
1838
1839 int delta = OrigSize - NewSize;
Jakob Stoklund Olesena3f331b2011-12-07 01:08:25 +00001840 BBInfo[MBB->getNumber()].Size -= delta;
Jakob Stoklund Olesen2fe71c52011-12-07 05:17:30 +00001841 AdjustBBOffsetsAfter(MBB);
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001842
1843 ++NumTBs;
1844 MadeChange = true;
Evan Cheng5657c012009-07-29 02:18:14 +00001845 }
1846 }
1847
1848 return MadeChange;
1849}
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001850
Jim Grosbach9249efe2009-11-16 18:55:47 +00001851/// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that
1852/// jump tables always branch forwards, since that's what tbb and tbh need.
Jim Grosbach80697d12009-11-12 17:25:07 +00001853bool ARMConstantIslands::ReorderThumb2JumpTables(MachineFunction &MF) {
1854 bool MadeChange = false;
1855
1856 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +00001857 if (MJTI == 0) return false;
Jim Grosbach26b8ef52010-07-07 21:06:51 +00001858
Jim Grosbach80697d12009-11-12 17:25:07 +00001859 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1860 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1861 MachineInstr *MI = T2JumpTables[i];
Evan Chenge837dea2011-06-28 19:10:37 +00001862 const MCInstrDesc &MCID = MI->getDesc();
1863 unsigned NumOps = MCID.getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001864 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
Jim Grosbach80697d12009-11-12 17:25:07 +00001865 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1866 unsigned JTI = JTOP.getIndex();
1867 assert(JTI < JT.size());
1868
1869 // We prefer if target blocks for the jump table come after the jump
1870 // instruction so we can use TB[BH]. Loop through the target blocks
1871 // and try to adjust them such that that's true.
Jim Grosbach08cbda52009-11-16 18:58:52 +00001872 int JTNumber = MI->getParent()->getNumber();
Jim Grosbach80697d12009-11-12 17:25:07 +00001873 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1874 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1875 MachineBasicBlock *MBB = JTBBs[j];
Jim Grosbach08cbda52009-11-16 18:58:52 +00001876 int DTNumber = MBB->getNumber();
Jim Grosbach80697d12009-11-12 17:25:07 +00001877
Jim Grosbach08cbda52009-11-16 18:58:52 +00001878 if (DTNumber < JTNumber) {
Jim Grosbach80697d12009-11-12 17:25:07 +00001879 // The destination precedes the switch. Try to move the block forward
1880 // so we have a positive offset.
1881 MachineBasicBlock *NewBB =
1882 AdjustJTTargetBlockForward(MBB, MI->getParent());
1883 if (NewBB)
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001884 MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
Jim Grosbach80697d12009-11-12 17:25:07 +00001885 MadeChange = true;
1886 }
1887 }
1888 }
1889
1890 return MadeChange;
1891}
1892
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001893MachineBasicBlock *ARMConstantIslands::
1894AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB)
1895{
1896 MachineFunction &MF = *BB->getParent();
1897
Jim Grosbach03e2d442010-07-07 22:53:35 +00001898 // If the destination block is terminated by an unconditional branch,
Jim Grosbach80697d12009-11-12 17:25:07 +00001899 // try to move it; otherwise, create a new block following the jump
Jim Grosbach08cbda52009-11-16 18:58:52 +00001900 // table that branches back to the actual target. This is a very simple
1901 // heuristic. FIXME: We can definitely improve it.
Jim Grosbach80697d12009-11-12 17:25:07 +00001902 MachineBasicBlock *TBB = 0, *FBB = 0;
1903 SmallVector<MachineOperand, 4> Cond;
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001904 SmallVector<MachineOperand, 4> CondPrior;
1905 MachineFunction::iterator BBi = BB;
1906 MachineFunction::iterator OldPrior = prior(BBi);
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001907
Jim Grosbachca215e72009-11-16 17:10:56 +00001908 // If the block terminator isn't analyzable, don't try to move the block
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001909 bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond);
Jim Grosbachca215e72009-11-16 17:10:56 +00001910
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001911 // If the block ends in an unconditional branch, move it. The prior block
1912 // has to have an analyzable terminator for us to move this one. Be paranoid
Jim Grosbach08cbda52009-11-16 18:58:52 +00001913 // and make sure we're not trying to move the entry block of the function.
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001914 if (!B && Cond.empty() && BB != MF.begin() &&
1915 !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
Jim Grosbach80697d12009-11-12 17:25:07 +00001916 BB->moveAfter(JTBB);
1917 OldPrior->updateTerminator();
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001918 BB->updateTerminator();
Jim Grosbach08cbda52009-11-16 18:58:52 +00001919 // Update numbering to account for the block being moved.
Jim Grosbacha0a95a32009-11-17 01:21:04 +00001920 MF.RenumberBlocks();
Jim Grosbach80697d12009-11-12 17:25:07 +00001921 ++NumJTMoved;
1922 return NULL;
1923 }
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001924
1925 // Create a new MBB for the code after the jump BB.
1926 MachineBasicBlock *NewBB =
1927 MF.CreateMachineBasicBlock(JTBB->getBasicBlock());
1928 MachineFunction::iterator MBBI = JTBB; ++MBBI;
1929 MF.insert(MBBI, NewBB);
1930
1931 // Add an unconditional branch from NewBB to BB.
1932 // There doesn't seem to be meaningful DebugInfo available; this doesn't
1933 // correspond directly to anything in the source.
1934 assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?");
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001935 BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
1936 .addImm(ARMCC::AL).addReg(0);
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001937
Jim Grosbach00a6a1f2009-11-14 20:10:18 +00001938 // Update internal data structures to account for the newly inserted MBB.
1939 MF.RenumberBlocks(NewBB);
1940
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001941 // Update the CFG.
1942 NewBB->addSuccessor(BB);
1943 JTBB->removeSuccessor(BB);
1944 JTBB->addSuccessor(NewBB);
1945
Jim Grosbach80697d12009-11-12 17:25:07 +00001946 ++NumJTInserted;
Jim Grosbach1fc7d712009-11-11 02:47:19 +00001947 return NewBB;
1948}