blob: 318f75e6d791437bb6c707d063b0c602a707faa0 [file] [log] [blame]
Bill Wendling18581a42010-12-21 01:54:40 +00001//===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===//
Evan Cheng10043e22007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Cheng10043e22007-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 Cheng22c7cf52007-01-25 03:12:46 +000018#include "ARMMachineFunctionInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000019#include "ARMInstrInfo.h"
Evan Cheng44a320d2010-08-12 20:30:05 +000020#include "Thumb2InstrInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Evan Cheng10043e22007-01-19 07:51:42 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chengc6d70ae2009-07-29 02:18:14 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetMachine.h"
Evan Cheng10043e22007-01-19 07:51:42 +000027#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnera6f074f2009-08-23 03:41:05 +000029#include "llvm/Support/raw_ostream.h"
Bob Wilson2f9be502009-10-15 20:49:47 +000030#include "llvm/ADT/SmallSet.h"
Evan Cheng8b7700f2007-02-09 20:54:44 +000031#include "llvm/ADT/SmallVector.h"
Evan Cheng10043e22007-01-19 07:51:42 +000032#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/Statistic.h"
Jim Grosbach8d92ec42009-11-11 02:47:19 +000034#include "llvm/Support/CommandLine.h"
Bob Wilson2f9be502009-10-15 20:49:47 +000035#include <algorithm>
Evan Cheng10043e22007-01-19 07:51:42 +000036using namespace llvm;
37
Evan Chengdb73d682009-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 Chenge41903b2009-08-14 18:31:44 +000044STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
Evan Cheng6f29ad92009-10-31 23:46:45 +000045STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
Jim Grosbach8d92ec42009-11-11 02:47:19 +000046STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
Jim Grosbach5d577142009-11-12 17:25:07 +000047STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
Jim Grosbach8d92ec42009-11-11 02:47:19 +000048
49
50static cl::opt<bool>
Jim Grosbachcdde77c2009-11-17 21:24:11 +000051AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
Jim Grosbach8d92ec42009-11-11 02:47:19 +000052 cl::desc("Adjust basic block layout to better use TB[BH]"));
Evan Cheng10043e22007-01-19 07:51:42 +000053
54namespace {
Dale Johannesene18b13b2007-02-23 05:02:36 +000055 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
Evan Cheng10043e22007-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 Johannesene18b13b2007-02-23 05:02:36 +000058 /// constant pool; instead, it places constants wherever it feels like with
Evan Cheng10043e22007-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 Lewycky02d5f772009-10-25 06:33:48 +000066 class ARMConstantIslands : public MachineFunctionPass {
Jakob Stoklund Olesene2b3ff22011-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 Wilson2f4e56f2009-05-12 17:09:30 +000076
Jakob Stoklund Olesene2b3ff22011-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
85 BasicBlockInfo() : Offset(0), Size(0) {}
86 BasicBlockInfo(unsigned o, unsigned s) : Offset(o), Size(s) {}
87 };
88
89 std::vector<BasicBlockInfo> BBInfo;
Dale Johannesen01ee5752007-02-25 00:47:03 +000090
Evan Cheng10043e22007-01-19 07:51:42 +000091 /// WaterList - A sorted list of basic blocks where islands could be placed
92 /// (i.e. blocks that don't fall through to the following block, due
93 /// to a return, unreachable, or unconditional branch).
Evan Cheng540f5e02007-02-09 23:59:14 +000094 std::vector<MachineBasicBlock*> WaterList;
Evan Cheng8b7700f2007-02-09 20:54:44 +000095
Bob Wilson2f9be502009-10-15 20:49:47 +000096 /// NewWaterList - The subset of WaterList that was created since the
97 /// previous iteration by inserting unconditional branches.
98 SmallSet<MachineBasicBlock*, 4> NewWaterList;
99
Bob Wilsonc7a3cf42009-10-12 18:52:13 +0000100 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
101
Evan Cheng10043e22007-01-19 07:51:42 +0000102 /// CPUser - One user of a constant pool, keeping the machine instruction
103 /// pointer, the constant pool being referenced, and the max displacement
Bob Wilson68ead6c2009-10-15 05:52:29 +0000104 /// allowed from the instruction to the CP. The HighWaterMark records the
105 /// highest basic block where a new CPEntry can be placed. To ensure this
106 /// pass terminates, the CP entries are initially placed at the end of the
107 /// function and then move monotonically to lower addresses. The
108 /// exception to this rule is when the current CP entry for a particular
109 /// CPUser is out of range, but there is another CP entry for the same
110 /// constant value in range. We want to use the existing in-range CP
111 /// entry, but if it later moves out of range, the search for new water
112 /// should resume where it left off. The HighWaterMark is used to record
113 /// that point.
Evan Cheng10043e22007-01-19 07:51:42 +0000114 struct CPUser {
115 MachineInstr *MI;
116 MachineInstr *CPEMI;
Bob Wilson68ead6c2009-10-15 05:52:29 +0000117 MachineBasicBlock *HighWaterMark;
Evan Cheng10043e22007-01-19 07:51:42 +0000118 unsigned MaxDisp;
Evan Cheng87aaa192009-07-21 23:56:01 +0000119 bool NegOk;
Evan Chengd2919a12009-07-23 18:27:47 +0000120 bool IsSoImm;
121 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
122 bool neg, bool soimm)
Bob Wilson68ead6c2009-10-15 05:52:29 +0000123 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {
124 HighWaterMark = CPEMI->getParent();
125 }
Evan Cheng10043e22007-01-19 07:51:42 +0000126 };
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000127
Evan Cheng10043e22007-01-19 07:51:42 +0000128 /// CPUsers - Keep track of all of the machine instructions that use various
129 /// constant pools and their max displacement.
Evan Cheng540f5e02007-02-09 23:59:14 +0000130 std::vector<CPUser> CPUsers;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000131
Evan Cheng8b7700f2007-02-09 20:54:44 +0000132 /// CPEntry - One per constant pool entry, keeping the machine instruction
133 /// pointer, the constpool index, and the number of CPUser's which
134 /// reference this entry.
135 struct CPEntry {
136 MachineInstr *CPEMI;
137 unsigned CPI;
138 unsigned RefCount;
139 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
140 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
141 };
142
143 /// CPEntries - Keep track of all of the constant pool entry machine
Dale Johannesene18b13b2007-02-23 05:02:36 +0000144 /// instructions. For each original constpool index (i.e. those that
145 /// existed upon entry to this pass), it keeps a vector of entries.
146 /// Original elements are cloned as we go along; the clones are
147 /// put in the vector of the original element, but have distinct CPIs.
Evan Cheng8b7700f2007-02-09 20:54:44 +0000148 std::vector<std::vector<CPEntry> > CPEntries;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000149
Evan Cheng22c7cf52007-01-25 03:12:46 +0000150 /// ImmBranch - One per immediate branch, keeping the machine instruction
151 /// pointer, conditional or unconditional, the max displacement,
152 /// and (if isCond is true) the corresponding unconditional branch
153 /// opcode.
154 struct ImmBranch {
155 MachineInstr *MI;
Evan Cheng010ae382007-01-25 23:18:59 +0000156 unsigned MaxDisp : 31;
157 bool isCond : 1;
Evan Cheng22c7cf52007-01-25 03:12:46 +0000158 int UncondBr;
Evan Cheng010ae382007-01-25 23:18:59 +0000159 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
160 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Cheng22c7cf52007-01-25 03:12:46 +0000161 };
162
Evan Chengc95f95b2007-05-16 05:14:06 +0000163 /// ImmBranches - Keep track of all the immediate branch instructions.
Evan Cheng22c7cf52007-01-25 03:12:46 +0000164 ///
Evan Cheng540f5e02007-02-09 23:59:14 +0000165 std::vector<ImmBranch> ImmBranches;
Evan Cheng22c7cf52007-01-25 03:12:46 +0000166
Evan Cheng7fa69642007-01-30 01:18:38 +0000167 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
168 ///
Evan Cheng8b7700f2007-02-09 20:54:44 +0000169 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Cheng7fa69642007-01-30 01:18:38 +0000170
Evan Chengc6d70ae2009-07-29 02:18:14 +0000171 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
172 SmallVector<MachineInstr*, 4> T2JumpTables;
173
Evan Cheng7fa69642007-01-30 01:18:38 +0000174 /// HasFarJump - True if any far jump instruction has been emitted during
175 /// the branch fix up pass.
176 bool HasFarJump;
177
Jim Grosbachb73918c2009-11-19 23:10:28 +0000178 /// HasInlineAsm - True if the function contains inline assembly.
179 bool HasInlineAsm;
180
Chris Lattner749ca322010-07-22 21:27:00 +0000181 const ARMInstrInfo *TII;
Evan Chenge64f48b2009-08-01 06:13:52 +0000182 const ARMSubtarget *STI;
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000183 ARMFunctionInfo *AFI;
Dale Johannesen962fa8e2007-02-28 23:20:38 +0000184 bool isThumb;
Evan Chengd2919a12009-07-23 18:27:47 +0000185 bool isThumb1;
David Goodwin27303cd2009-06-30 18:04:13 +0000186 bool isThumb2;
Evan Cheng10043e22007-01-19 07:51:42 +0000187 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000188 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +0000189 ARMConstantIslands() : MachineFunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +0000190
Evan Chengc6d70ae2009-07-29 02:18:14 +0000191 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Cheng10043e22007-01-19 07:51:42 +0000192
193 virtual const char *getPassName() const {
Evan Cheng22c7cf52007-01-25 03:12:46 +0000194 return "ARM constant island placement and branch shortening pass";
Evan Cheng10043e22007-01-19 07:51:42 +0000195 }
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000196
Evan Cheng10043e22007-01-19 07:51:42 +0000197 private:
Evan Chengc6d70ae2009-07-29 02:18:14 +0000198 void DoInitialPlacement(MachineFunction &MF,
Evan Cheng540f5e02007-02-09 23:59:14 +0000199 std::vector<MachineInstr*> &CPEMIs);
Evan Cheng8b7700f2007-02-09 20:54:44 +0000200 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Jim Grosbach5d577142009-11-12 17:25:07 +0000201 void JumpTableFunctionScan(MachineFunction &MF);
Evan Chengc6d70ae2009-07-29 02:18:14 +0000202 void InitialFunctionScan(MachineFunction &MF,
Evan Cheng540f5e02007-02-09 23:59:14 +0000203 const std::vector<MachineInstr*> &CPEMIs);
Evan Cheng345877e2007-01-31 02:22:22 +0000204 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Cheng10043e22007-01-19 07:51:42 +0000205 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Dale Johannesen01ee5752007-02-25 00:47:03 +0000206 void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta);
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000207 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
Dale Johannesene18b13b2007-02-23 05:02:36 +0000208 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
Bob Wilson2f9be502009-10-15 20:49:47 +0000209 bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter);
Dale Johannesen962fa8e2007-02-28 23:20:38 +0000210 void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
Bob Wilson3250e772009-10-12 21:39:43 +0000211 MachineBasicBlock *&NewMBB);
Evan Chengc6d70ae2009-07-29 02:18:14 +0000212 bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex);
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000213 void RemoveDeadCPEMI(MachineInstr *CPEMI);
214 bool RemoveUnusedCPEntries();
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000215 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng87aaa192009-07-21 23:56:01 +0000216 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
217 bool DoDump = false);
Dale Johannesen01ee5752007-02-25 00:47:03 +0000218 bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
Dale Johannesene59411d2007-07-11 18:32:38 +0000219 CPUser &U);
Dale Johannesen01ee5752007-02-25 00:47:03 +0000220 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
Evan Chengd2919a12009-07-23 18:27:47 +0000221 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
Evan Cheng1f3fc4b2007-01-31 19:57:44 +0000222 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Chengc6d70ae2009-07-29 02:18:14 +0000223 bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br);
224 bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br);
225 bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br);
Evan Cheng7fa69642007-01-30 01:18:38 +0000226 bool UndoLRSpillRestore();
Evan Chengdb73d682009-08-14 00:32:16 +0000227 bool OptimizeThumb2Instructions(MachineFunction &MF);
228 bool OptimizeThumb2Branches(MachineFunction &MF);
Jim Grosbach5d577142009-11-12 17:25:07 +0000229 bool ReorderThumb2JumpTables(MachineFunction &MF);
Evan Chengc6d70ae2009-07-29 02:18:14 +0000230 bool OptimizeThumb2JumpTables(MachineFunction &MF);
Jim Grosbach8d92ec42009-11-11 02:47:19 +0000231 MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB,
232 MachineBasicBlock *JTBB);
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Evan Cheng10043e22007-01-19 07:51:42 +0000234 unsigned GetOffsetOf(MachineInstr *MI) const;
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000235 void dumpBBs();
Evan Chengc6d70ae2009-07-29 02:18:14 +0000236 void verify(MachineFunction &MF);
Evan Cheng10043e22007-01-19 07:51:42 +0000237 };
Devang Patel8c78a0b2007-05-03 01:11:54 +0000238 char ARMConstantIslands::ID = 0;
Evan Cheng10043e22007-01-19 07:51:42 +0000239}
240
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000241/// verify - check BBOffsets, BBSizes, alignment of islands
Evan Chengc6d70ae2009-07-29 02:18:14 +0000242void ARMConstantIslands::verify(MachineFunction &MF) {
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000243 for (unsigned i = 1, e = BBInfo.size(); i != e; ++i)
244 assert(BBInfo[i-1].Offset + BBInfo[i-1].Size == BBInfo[i].Offset);
Evan Chengd2919a12009-07-23 18:27:47 +0000245 if (!isThumb)
246 return;
247#ifndef NDEBUG
Evan Chengc6d70ae2009-07-29 02:18:14 +0000248 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chengd2919a12009-07-23 18:27:47 +0000249 MBBI != E; ++MBBI) {
250 MachineBasicBlock *MBB = MBBI;
251 if (!MBB->empty() &&
252 MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
253 unsigned MBBId = MBB->getNumber();
Jim Grosbachb73918c2009-11-19 23:10:28 +0000254 assert(HasInlineAsm ||
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000255 (BBInfo[MBBId].Offset%4 == 0 && BBInfo[MBBId].Size%4 == 0) ||
256 (BBInfo[MBBId].Offset%4 != 0 && BBInfo[MBBId].Size%4 != 0));
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000257 }
258 }
Jim Grosbachb73918c2009-11-19 23:10:28 +0000259 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
260 CPUser &U = CPUsers[i];
261 unsigned UserOffset = GetOffsetOf(U.MI) + (isThumb ? 4 : 8);
Jim Grosbach6c3b7112009-11-20 19:37:38 +0000262 unsigned CPEOffset = GetOffsetOf(U.CPEMI);
263 unsigned Disp = UserOffset < CPEOffset ? CPEOffset - UserOffset :
264 UserOffset - CPEOffset;
265 assert(Disp <= U.MaxDisp || "Constant pool entry out of range!");
Jim Grosbachb73918c2009-11-19 23:10:28 +0000266 }
Jim Grosbach6c3b7112009-11-20 19:37:38 +0000267#endif
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000268}
269
270/// print block size and offset information - debugging
271void ARMConstantIslands::dumpBBs() {
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000272 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
273 DEBUG(errs() << "block " << J << " offset " << BBInfo[J].Offset
274 << " size " << BBInfo[J].Size << "\n");
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000275 }
276}
277
Evan Cheng22c7cf52007-01-25 03:12:46 +0000278/// createARMConstantIslandPass - returns an instance of the constpool
279/// island pass.
Evan Cheng10043e22007-01-19 07:51:42 +0000280FunctionPass *llvm::createARMConstantIslandPass() {
281 return new ARMConstantIslands();
282}
283
Evan Chengc6d70ae2009-07-29 02:18:14 +0000284bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) {
285 MachineConstantPool &MCP = *MF.getConstantPool();
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000286
Chris Lattner749ca322010-07-22 21:27:00 +0000287 TII = (const ARMInstrInfo*)MF.getTarget().getInstrInfo();
Evan Chengc6d70ae2009-07-29 02:18:14 +0000288 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chenge64f48b2009-08-01 06:13:52 +0000289 STI = &MF.getTarget().getSubtarget<ARMSubtarget>();
290
Dale Johannesen962fa8e2007-02-28 23:20:38 +0000291 isThumb = AFI->isThumbFunction();
Evan Chengd2919a12009-07-23 18:27:47 +0000292 isThumb1 = AFI->isThumb1OnlyFunction();
David Goodwin27303cd2009-06-30 18:04:13 +0000293 isThumb2 = AFI->isThumb2Function();
Evan Cheng7fa69642007-01-30 01:18:38 +0000294
295 HasFarJump = false;
Jim Grosbachb73918c2009-11-19 23:10:28 +0000296 HasInlineAsm = false;
Evan Cheng7fa69642007-01-30 01:18:38 +0000297
Evan Cheng10043e22007-01-19 07:51:42 +0000298 // Renumber all of the machine basic blocks in the function, guaranteeing that
299 // the numbers agree with the position of the block in the function.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000300 MF.RenumberBlocks();
Evan Cheng10043e22007-01-19 07:51:42 +0000301
Jim Grosbach5d577142009-11-12 17:25:07 +0000302 // Try to reorder and otherwise adjust the block layout to make good use
303 // of the TB[BH] instructions.
304 bool MadeChange = false;
305 if (isThumb2 && AdjustJumpTableBlocks) {
306 JumpTableFunctionScan(MF);
307 MadeChange |= ReorderThumb2JumpTables(MF);
308 // Data is out of date, so clear it. It'll be re-computed later.
Jim Grosbach5d577142009-11-12 17:25:07 +0000309 T2JumpTables.clear();
310 // Blocks may have shifted around. Keep the numbering up to date.
311 MF.RenumberBlocks();
312 }
313
Evan Chengf6d0fa32009-07-31 18:28:05 +0000314 // Thumb1 functions containing constant pools get 4-byte alignment.
Evan Chengd2919a12009-07-23 18:27:47 +0000315 // This is so we can keep exact track of where the alignment padding goes.
316
Chris Lattnerfeba1e22010-01-27 23:37:36 +0000317 // ARM and Thumb2 functions need to be 4-byte aligned.
318 if (!isThumb1)
319 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesenfdfb7572007-04-23 20:09:04 +0000320
Evan Cheng10043e22007-01-19 07:51:42 +0000321 // Perform the initial placement of the constant pool entries. To start with,
322 // we put them all at the end of the function.
Evan Cheng540f5e02007-02-09 23:59:14 +0000323 std::vector<MachineInstr*> CPEMIs;
Dale Johannesenfdfb7572007-04-23 20:09:04 +0000324 if (!MCP.isEmpty()) {
Evan Chengc6d70ae2009-07-29 02:18:14 +0000325 DoInitialPlacement(MF, CPEMIs);
Evan Chengd2919a12009-07-23 18:27:47 +0000326 if (isThumb1)
Chris Lattnerfeba1e22010-01-27 23:37:36 +0000327 MF.EnsureAlignment(2); // 2 = log2(4)
Dale Johannesenfdfb7572007-04-23 20:09:04 +0000328 }
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000329
Evan Cheng10043e22007-01-19 07:51:42 +0000330 /// The next UID to take is the first unused one.
Evan Chengdfce83c2011-01-17 08:03:18 +0000331 AFI->initPICLabelUId(CPEMIs.size());
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000332
Evan Cheng10043e22007-01-19 07:51:42 +0000333 // Do the initial scan of the function, building up information about the
334 // sizes of each block, the location of all the water, and finding all of the
335 // constant pool users.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000336 InitialFunctionScan(MF, CPEMIs);
Evan Cheng10043e22007-01-19 07:51:42 +0000337 CPEMIs.clear();
Dale Johannesenc17dd572010-07-23 22:50:23 +0000338 DEBUG(dumpBBs());
339
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000340
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000341 /// Remove dead constant pool entries.
Bill Wendlinga4dda532010-12-18 01:53:06 +0000342 MadeChange |= RemoveUnusedCPEntries();
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000343
Evan Cheng7fa69642007-01-30 01:18:38 +0000344 // Iteratively place constant pool entries and fix up branches until there
345 // is no change.
Evan Cheng82ff0222009-08-07 07:35:21 +0000346 unsigned NoCPIters = 0, NoBRIters = 0;
Evan Cheng7fa69642007-01-30 01:18:38 +0000347 while (true) {
Evan Cheng82ff0222009-08-07 07:35:21 +0000348 bool CPChange = false;
Evan Cheng10043e22007-01-19 07:51:42 +0000349 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Evan Cheng82ff0222009-08-07 07:35:21 +0000350 CPChange |= HandleConstantPoolUser(MF, i);
351 if (CPChange && ++NoCPIters > 30)
352 llvm_unreachable("Constant Island pass failed to converge!");
Evan Cheng94579db2007-07-10 22:00:16 +0000353 DEBUG(dumpBBs());
Jim Grosbache4ba2aa2010-07-07 21:06:51 +0000354
Bob Wilson2f9be502009-10-15 20:49:47 +0000355 // Clear NewWaterList now. If we split a block for branches, it should
356 // appear as "new water" for the next iteration of constant pool placement.
357 NewWaterList.clear();
Evan Cheng82ff0222009-08-07 07:35:21 +0000358
359 bool BRChange = false;
Evan Cheng22c7cf52007-01-25 03:12:46 +0000360 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Cheng82ff0222009-08-07 07:35:21 +0000361 BRChange |= FixUpImmediateBr(MF, ImmBranches[i]);
362 if (BRChange && ++NoBRIters > 30)
363 llvm_unreachable("Branch Fix Up pass failed to converge!");
Evan Cheng94579db2007-07-10 22:00:16 +0000364 DEBUG(dumpBBs());
Evan Cheng82ff0222009-08-07 07:35:21 +0000365
366 if (!CPChange && !BRChange)
Evan Cheng7fa69642007-01-30 01:18:38 +0000367 break;
368 MadeChange = true;
369 }
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000370
Evan Chengdb73d682009-08-14 00:32:16 +0000371 // Shrink 32-bit Thumb2 branch, load, and store instructions.
Evan Chengce8fb682010-08-09 18:35:19 +0000372 if (isThumb2 && !STI->prefers32BitThumb())
Evan Chengdb73d682009-08-14 00:32:16 +0000373 MadeChange |= OptimizeThumb2Instructions(MF);
Evan Chenge64f48b2009-08-01 06:13:52 +0000374
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000375 // After a while, this might be made debug-only, but it is not expensive.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000376 verify(MF);
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000377
Jim Grosbache4ba2aa2010-07-07 21:06:51 +0000378 // If LR has been forced spilled and no far jump (i.e. BL) has been issued,
379 // undo the spill / restore of LR if possible.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000380 if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
Evan Cheng7fa69642007-01-30 01:18:38 +0000381 MadeChange |= UndoLRSpillRestore();
382
Anton Korobeynikov221f4fa2011-01-30 22:07:39 +0000383 // Save the mapping between original and cloned constpool entries.
384 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
385 for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) {
386 const CPEntry & CPE = CPEntries[i][j];
387 AFI->recordCPEClone(i, CPE.CPI);
388 }
389 }
390
Evan Cheng3fabe072010-07-22 02:09:47 +0000391 DEBUG(errs() << '\n'; dumpBBs());
392
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000393 BBInfo.clear();
Evan Cheng10043e22007-01-19 07:51:42 +0000394 WaterList.clear();
395 CPUsers.clear();
Evan Cheng8b7700f2007-02-09 20:54:44 +0000396 CPEntries.clear();
Evan Cheng22c7cf52007-01-25 03:12:46 +0000397 ImmBranches.clear();
Evan Cheng8b7700f2007-02-09 20:54:44 +0000398 PushPopMIs.clear();
Evan Chengc6d70ae2009-07-29 02:18:14 +0000399 T2JumpTables.clear();
Evan Cheng7fa69642007-01-30 01:18:38 +0000400
401 return MadeChange;
Evan Cheng10043e22007-01-19 07:51:42 +0000402}
403
404/// DoInitialPlacement - Perform the initial placement of the constant pool
405/// entries. To start with, we put them all at the end of the function.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000406void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF,
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000407 std::vector<MachineInstr*> &CPEMIs) {
Evan Cheng10043e22007-01-19 07:51:42 +0000408 // Create the basic block to hold the CPE's.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000409 MachineBasicBlock *BB = MF.CreateMachineBasicBlock();
410 MF.push_back(BB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000411
Jakob Stoklund Olesen2e05db22011-12-06 01:43:02 +0000412 // Mark the basic block as 4-byte aligned as required by the const-pool.
413 BB->setAlignment(2);
414
Evan Cheng10043e22007-01-19 07:51:42 +0000415 // Add all of the constants from the constant pool to the end block, use an
416 // identity mapping of CPI's to CPE's.
417 const std::vector<MachineConstantPoolEntry> &CPs =
Evan Chengc6d70ae2009-07-29 02:18:14 +0000418 MF.getConstantPool()->getConstants();
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000419
Evan Chengc6d70ae2009-07-29 02:18:14 +0000420 const TargetData &TD = *MF.getTarget().getTargetData();
Evan Cheng10043e22007-01-19 07:51:42 +0000421 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000422 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
Evan Cheng10043e22007-01-19 07:51:42 +0000423 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
424 // we would have to pad them out or something so that instructions stay
425 // aligned.
426 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
427 MachineInstr *CPEMI =
Chris Lattner6f306d72010-04-02 20:16:16 +0000428 BuildMI(BB, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
429 .addImm(i).addConstantPoolIndex(i).addImm(Size);
Evan Cheng10043e22007-01-19 07:51:42 +0000430 CPEMIs.push_back(CPEMI);
Evan Cheng8b7700f2007-02-09 20:54:44 +0000431
432 // Add a new CPEntry, but no corresponding CPUser yet.
433 std::vector<CPEntry> CPEs;
434 CPEs.push_back(CPEntry(CPEMI, i));
435 CPEntries.push_back(CPEs);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000436 ++NumCPEs;
Chris Lattneraf29ea62009-08-23 06:49:22 +0000437 DEBUG(errs() << "Moved CPI#" << i << " to end of function as #" << i
438 << "\n");
Evan Cheng10043e22007-01-19 07:51:42 +0000439 }
440}
441
Dale Johannesene18b13b2007-02-23 05:02:36 +0000442/// BBHasFallthrough - Return true if the specified basic block can fallthrough
Evan Cheng10043e22007-01-19 07:51:42 +0000443/// into the block immediately after it.
444static bool BBHasFallthrough(MachineBasicBlock *MBB) {
445 // Get the next machine basic block in the function.
446 MachineFunction::iterator MBBI = MBB;
Jim Grosbach84511e12010-06-02 21:53:11 +0000447 // Can't fall off end of function.
448 if (llvm::next(MBBI) == MBB->getParent()->end())
Evan Cheng10043e22007-01-19 07:51:42 +0000449 return false;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000450
Chris Lattnera48f44d2009-12-03 00:50:42 +0000451 MachineBasicBlock *NextBB = llvm::next(MBBI);
Evan Cheng10043e22007-01-19 07:51:42 +0000452 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
453 E = MBB->succ_end(); I != E; ++I)
454 if (*I == NextBB)
455 return true;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000456
Evan Cheng10043e22007-01-19 07:51:42 +0000457 return false;
458}
459
Evan Cheng8b7700f2007-02-09 20:54:44 +0000460/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
461/// look up the corresponding CPEntry.
462ARMConstantIslands::CPEntry
463*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
464 const MachineInstr *CPEMI) {
465 std::vector<CPEntry> &CPEs = CPEntries[CPI];
466 // Number of entries per constpool index should be small, just do a
467 // linear search.
468 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
469 if (CPEs[i].CPEMI == CPEMI)
470 return &CPEs[i];
471 }
472 return NULL;
473}
474
Jim Grosbach5d577142009-11-12 17:25:07 +0000475/// JumpTableFunctionScan - Do a scan of the function, building up
476/// information about the sizes of each block and the locations of all
477/// the jump tables.
478void ARMConstantIslands::JumpTableFunctionScan(MachineFunction &MF) {
Jim Grosbach5d577142009-11-12 17:25:07 +0000479 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
480 MBBI != E; ++MBBI) {
481 MachineBasicBlock &MBB = *MBBI;
482
Jim Grosbach5d577142009-11-12 17:25:07 +0000483 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Jim Grosbach9785e592009-11-16 18:58:52 +0000484 I != E; ++I)
485 if (I->getDesc().isBranch() && I->getOpcode() == ARM::t2BR_JT)
486 T2JumpTables.push_back(I);
Jim Grosbach5d577142009-11-12 17:25:07 +0000487 }
488}
489
Evan Cheng10043e22007-01-19 07:51:42 +0000490/// InitialFunctionScan - Do the initial scan of the function, building up
491/// information about the sizes of each block, the location of all the water,
492/// and finding all of the constant pool users.
Evan Chengc6d70ae2009-07-29 02:18:14 +0000493void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF,
Evan Cheng540f5e02007-02-09 23:59:14 +0000494 const std::vector<MachineInstr*> &CPEMIs) {
Jim Grosbachb73918c2009-11-19 23:10:28 +0000495 // First thing, see if the function has any inline assembly in it. If so,
496 // we have to be conservative about alignment assumptions, as we don't
497 // know for sure the size of any instructions in the inline assembly.
498 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
499 MBBI != E; ++MBBI) {
500 MachineBasicBlock &MBB = *MBBI;
501 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
502 I != E; ++I)
503 if (I->getOpcode() == ARM::INLINEASM)
504 HasInlineAsm = true;
505 }
506
Bill Wendling18581a42010-12-21 01:54:40 +0000507 // Now go back through the instructions and build up our data structures.
Dale Johannesen01ee5752007-02-25 00:47:03 +0000508 unsigned Offset = 0;
Evan Chengc6d70ae2009-07-29 02:18:14 +0000509 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Cheng10043e22007-01-19 07:51:42 +0000510 MBBI != E; ++MBBI) {
511 MachineBasicBlock &MBB = *MBBI;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000512
Evan Cheng10043e22007-01-19 07:51:42 +0000513 // If this block doesn't fall through into the next MBB, then this is
514 // 'water' that a constant pool island could be placed.
515 if (!BBHasFallthrough(&MBB))
516 WaterList.push_back(&MBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000517
Evan Cheng10043e22007-01-19 07:51:42 +0000518 unsigned MBBSize = 0;
519 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
520 I != E; ++I) {
Jim Grosbach97c8a6a2010-06-21 17:49:23 +0000521 if (I->isDebugValue())
522 continue;
Evan Cheng10043e22007-01-19 07:51:42 +0000523 // Add instruction size to MBBSize.
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000524 MBBSize += TII->GetInstSizeInBytes(I);
Evan Cheng10043e22007-01-19 07:51:42 +0000525
Evan Cheng22c7cf52007-01-25 03:12:46 +0000526 int Opc = I->getOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +0000527 if (I->getDesc().isBranch()) {
Evan Cheng22c7cf52007-01-25 03:12:46 +0000528 bool isCond = false;
529 unsigned Bits = 0;
530 unsigned Scale = 1;
531 int UOpc = Opc;
532 switch (Opc) {
Evan Chengc6d70ae2009-07-29 02:18:14 +0000533 default:
534 continue; // Ignore other JT branches
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000535 case ARM::tBR_JTr:
Evan Chengf3a1fce2009-07-25 00:33:29 +0000536 // A Thumb1 table jump may involve padding; for the offsets to
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000537 // be right, functions containing these must be 4-byte aligned.
Evan Cheng3fabe072010-07-22 02:09:47 +0000538 // tBR_JTr expands to a mov pc followed by .align 2 and then the jump
Jakob Stoklund Olesencc6bfa82011-12-06 22:41:31 +0000539 // table entries. So this code checks whether offset of tBR_JTr + 2
540 // is aligned. That is held in Offset+MBBSize, which already has
541 // 2 added in for the size of the mov pc instruction.
Chris Lattnerfeba1e22010-01-27 23:37:36 +0000542 MF.EnsureAlignment(2U);
Jakob Stoklund Olesencc6bfa82011-12-06 22:41:31 +0000543 if ((Offset+MBBSize)%4 != 0 || HasInlineAsm)
544 // FIXME: Add a pseudo ALIGN instruction instead.
545 MBBSize += 2; // padding
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000546 continue; // Does not get an entry in ImmBranches
Evan Chengc6d70ae2009-07-29 02:18:14 +0000547 case ARM::t2BR_JT:
548 T2JumpTables.push_back(I);
549 continue; // Does not get an entry in ImmBranches
Evan Cheng22c7cf52007-01-25 03:12:46 +0000550 case ARM::Bcc:
551 isCond = true;
552 UOpc = ARM::B;
553 // Fallthrough
554 case ARM::B:
555 Bits = 24;
556 Scale = 4;
557 break;
558 case ARM::tBcc:
559 isCond = true;
560 UOpc = ARM::tB;
561 Bits = 8;
562 Scale = 2;
563 break;
564 case ARM::tB:
565 Bits = 11;
566 Scale = 2;
567 break;
David Goodwin27303cd2009-06-30 18:04:13 +0000568 case ARM::t2Bcc:
569 isCond = true;
570 UOpc = ARM::t2B;
571 Bits = 20;
572 Scale = 2;
573 break;
574 case ARM::t2B:
575 Bits = 24;
576 Scale = 2;
577 break;
Evan Cheng22c7cf52007-01-25 03:12:46 +0000578 }
Evan Chengf9a4c692007-02-01 10:16:15 +0000579
580 // Record this immediate branch.
Evan Cheng36d559d2007-02-03 02:08:34 +0000581 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengf9a4c692007-02-01 10:16:15 +0000582 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Cheng22c7cf52007-01-25 03:12:46 +0000583 }
584
Evan Cheng7fa69642007-01-30 01:18:38 +0000585 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
586 PushPopMIs.push_back(I);
587
Evan Chengd2919a12009-07-23 18:27:47 +0000588 if (Opc == ARM::CONSTPOOL_ENTRY)
589 continue;
590
Evan Cheng10043e22007-01-19 07:51:42 +0000591 // Scan the instructions for constant pool operands.
592 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000593 if (I->getOperand(op).isCPI()) {
Evan Cheng10043e22007-01-19 07:51:42 +0000594 // We found one. The addressing mode tells us the max displacement
595 // from the PC that this instruction permits.
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000596
Evan Cheng10043e22007-01-19 07:51:42 +0000597 // Basic size info comes from the TSFlags field.
Evan Chengf9a4c692007-02-01 10:16:15 +0000598 unsigned Bits = 0;
599 unsigned Scale = 1;
Evan Cheng87aaa192009-07-21 23:56:01 +0000600 bool NegOk = false;
Evan Chengd2919a12009-07-23 18:27:47 +0000601 bool IsSoImm = false;
602
603 switch (Opc) {
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000604 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000605 llvm_unreachable("Unknown addressing mode for CP reference!");
Evan Chengd2919a12009-07-23 18:27:47 +0000606 break;
607
608 // Taking the address of a CP entry.
609 case ARM::LEApcrel:
610 // This takes a SoImm, which is 8 bit immediate rotated. We'll
611 // pretend the maximum offset is 255 * 4. Since each instruction
Jim Grosbach36a5bf82009-11-19 18:23:19 +0000612 // 4 byte wide, this is always correct. We'll check for other
Evan Chengd2919a12009-07-23 18:27:47 +0000613 // displacements that fits in a SoImm as well.
Evan Chengf9a4c692007-02-01 10:16:15 +0000614 Bits = 8;
Evan Chengd2919a12009-07-23 18:27:47 +0000615 Scale = 4;
616 NegOk = true;
617 IsSoImm = true;
618 break;
Owen Anderson9a4d4282010-12-13 22:51:08 +0000619 case ARM::t2LEApcrel:
Evan Chengd2919a12009-07-23 18:27:47 +0000620 Bits = 12;
Evan Cheng87aaa192009-07-21 23:56:01 +0000621 NegOk = true;
Evan Cheng10043e22007-01-19 07:51:42 +0000622 break;
Evan Chengd2919a12009-07-23 18:27:47 +0000623 case ARM::tLEApcrel:
624 Bits = 8;
625 Scale = 4;
626 break;
627
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000628 case ARM::LDRi12:
Evan Chengd2919a12009-07-23 18:27:47 +0000629 case ARM::LDRcp:
Owen Anderson4ebf4712011-02-08 22:39:40 +0000630 case ARM::t2LDRpci:
Evan Chengfd522992007-02-01 20:44:52 +0000631 Bits = 12; // +-offset_12
Evan Cheng87aaa192009-07-21 23:56:01 +0000632 NegOk = true;
Evan Cheng10043e22007-01-19 07:51:42 +0000633 break;
Evan Chengd2919a12009-07-23 18:27:47 +0000634
635 case ARM::tLDRpci:
Evan Chengf9a4c692007-02-01 10:16:15 +0000636 Bits = 8;
637 Scale = 4; // +(offset_8*4)
Evan Cheng1526ba52007-01-24 08:53:17 +0000638 break;
Evan Chengd2919a12009-07-23 18:27:47 +0000639
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000640 case ARM::VLDRD:
641 case ARM::VLDRS:
Evan Chengd2919a12009-07-23 18:27:47 +0000642 Bits = 8;
643 Scale = 4; // +-(offset_8*4)
644 NegOk = true;
Evan Chengb23b50d2009-06-29 07:51:04 +0000645 break;
Evan Cheng10043e22007-01-19 07:51:42 +0000646 }
Evan Chengf9a4c692007-02-01 10:16:15 +0000647
Evan Cheng10043e22007-01-19 07:51:42 +0000648 // Remember that this is a user of a CP entry.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000649 unsigned CPI = I->getOperand(op).getIndex();
Evan Cheng8b7700f2007-02-09 20:54:44 +0000650 MachineInstr *CPEMI = CPEMIs[CPI];
Evan Chenge41903b2009-08-14 18:31:44 +0000651 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
Evan Chengd2919a12009-07-23 18:27:47 +0000652 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
Evan Cheng8b7700f2007-02-09 20:54:44 +0000653
654 // Increment corresponding CPEntry reference count.
655 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
656 assert(CPE && "Cannot find a corresponding CPEntry!");
657 CPE->RefCount++;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000658
Evan Cheng10043e22007-01-19 07:51:42 +0000659 // Instructions can only use one CP entry, don't bother scanning the
660 // rest of the operands.
661 break;
662 }
663 }
Evan Cheng234e0312007-02-01 01:09:47 +0000664
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000665 // In thumb mode, if this block is a constpool island, we may need padding
666 // so it's aligned on 4 byte boundary.
Dale Johannesen962fa8e2007-02-28 23:20:38 +0000667 if (isThumb &&
Evan Chengd9d94702007-02-02 19:09:19 +0000668 !MBB.empty() &&
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000669 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Jim Grosbachb73918c2009-11-19 23:10:28 +0000670 ((Offset%4) != 0 || HasInlineAsm))
Evan Cheng234e0312007-02-01 01:09:47 +0000671 MBBSize += 2;
672
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000673 BBInfo.push_back(BasicBlockInfo(Offset, MBBSize));
Dale Johannesen01ee5752007-02-25 00:47:03 +0000674 Offset += MBBSize;
Evan Cheng10043e22007-01-19 07:51:42 +0000675 }
676}
677
Evan Cheng10043e22007-01-19 07:51:42 +0000678/// GetOffsetOf - Return the current offset of the specified machine instruction
679/// from the start of the function. This offset changes as stuff is moved
680/// around inside the function.
681unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
682 MachineBasicBlock *MBB = MI->getParent();
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000683
Evan Cheng10043e22007-01-19 07:51:42 +0000684 // The offset is composed of two things: the sum of the sizes of all MBB's
685 // before this instruction's block, and the offset from the start of the block
686 // it is in.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000687 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
Evan Cheng10043e22007-01-19 07:51:42 +0000688
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000689 // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
690 // alignment padding, and compensate if so.
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000691 if (isThumb &&
692 MI->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Jim Grosbachb73918c2009-11-19 23:10:28 +0000693 (Offset%4 != 0 || HasInlineAsm))
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000694 Offset += 2;
695
Evan Cheng10043e22007-01-19 07:51:42 +0000696 // Sum instructions before MI in MBB.
697 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
698 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
699 if (&*I == MI) return Offset;
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000700 Offset += TII->GetInstSizeInBytes(I);
Evan Cheng10043e22007-01-19 07:51:42 +0000701 }
702}
703
704/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
705/// ID.
706static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
707 const MachineBasicBlock *RHS) {
708 return LHS->getNumber() < RHS->getNumber();
709}
710
711/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
712/// machine function, it upsets all of the block numbers. Renumber the blocks
713/// and update the arrays that parallel this numbering.
714void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
Duncan Sands75b5d272011-02-15 09:23:02 +0000715 // Renumber the MBB's to keep them consecutive.
Evan Cheng10043e22007-01-19 07:51:42 +0000716 NewBB->getParent()->RenumberBlocks(NewBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000717
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000718 // Insert an entry into BBInfo to align it properly with the (newly
Evan Cheng10043e22007-01-19 07:51:42 +0000719 // renumbered) block numbers.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000720 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000721
722 // Next, update WaterList. Specifically, we need to add NewMBB as having
Evan Cheng10043e22007-01-19 07:51:42 +0000723 // available water after it.
Bob Wilsonc7a3cf42009-10-12 18:52:13 +0000724 water_iterator IP =
Evan Cheng10043e22007-01-19 07:51:42 +0000725 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
726 CompareMBBNumbers);
727 WaterList.insert(IP, NewBB);
728}
729
730
731/// Split the basic block containing MI into two blocks, which are joined by
Bob Wilson2f9be502009-10-15 20:49:47 +0000732/// an unconditional branch. Update data structures and renumber blocks to
Evan Cheng345877e2007-01-31 02:22:22 +0000733/// account for this change and returns the newly created block.
734MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Cheng10043e22007-01-19 07:51:42 +0000735 MachineBasicBlock *OrigBB = MI->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +0000736 MachineFunction &MF = *OrigBB->getParent();
Evan Cheng10043e22007-01-19 07:51:42 +0000737
738 // Create a new MBB for the code after the OrigBB.
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000739 MachineBasicBlock *NewBB =
740 MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
Evan Cheng10043e22007-01-19 07:51:42 +0000741 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
Dan Gohman3b460302008-07-07 23:14:23 +0000742 MF.insert(MBBI, NewBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000743
Evan Cheng10043e22007-01-19 07:51:42 +0000744 // Splice the instructions starting with MI over to NewBB.
745 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000746
Evan Cheng10043e22007-01-19 07:51:42 +0000747 // Add an unconditional branch from OrigBB to NewBB.
Evan Cheng7169bd82007-01-31 18:29:27 +0000748 // Note the new unconditional branch is not being recorded.
Dale Johannesen7647da62009-02-13 02:25:56 +0000749 // There doesn't seem to be meaningful DebugInfo available; this doesn't
750 // correspond to anything in the source.
Evan Cheng7c943432009-07-07 01:16:41 +0000751 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000752 if (!isThumb)
753 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
754 else
755 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
756 .addImm(ARMCC::AL).addReg(0);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000757 ++NumSplit;
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000758
Evan Cheng10043e22007-01-19 07:51:42 +0000759 // Update the CFG. All succs of OrigBB are now succs of NewBB.
Jakob Stoklund Olesen26081572011-12-06 00:51:12 +0000760 NewBB->transferSuccessors(OrigBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000761
Evan Cheng10043e22007-01-19 07:51:42 +0000762 // OrigBB branches to NewBB.
763 OrigBB->addSuccessor(NewBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000764
Evan Cheng10043e22007-01-19 07:51:42 +0000765 // Update internal data structures to account for the newly inserted MBB.
Dale Johannesene18b13b2007-02-23 05:02:36 +0000766 // This is almost the same as UpdateForInsertedWaterBlock, except that
767 // the Water goes after OrigBB, not NewBB.
Dan Gohman3b460302008-07-07 23:14:23 +0000768 MF.RenumberBlocks(NewBB);
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000769
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000770 // Insert an entry into BBInfo to align it properly with the (newly
Dale Johannesene18b13b2007-02-23 05:02:36 +0000771 // renumbered) block numbers.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000772 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
Dale Johannesen01ee5752007-02-25 00:47:03 +0000773
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000774 // Next, update WaterList. Specifically, we need to add OrigMBB as having
Dale Johannesene18b13b2007-02-23 05:02:36 +0000775 // available water after it (but not if it's already there, which happens
776 // when splitting before a conditional branch that is followed by an
777 // unconditional branch - in that case we want to insert NewBB).
Bob Wilsonc7a3cf42009-10-12 18:52:13 +0000778 water_iterator IP =
Dale Johannesene18b13b2007-02-23 05:02:36 +0000779 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
780 CompareMBBNumbers);
781 MachineBasicBlock* WaterBB = *IP;
782 if (WaterBB == OrigBB)
Chris Lattnera48f44d2009-12-03 00:50:42 +0000783 WaterList.insert(llvm::next(IP), NewBB);
Dale Johannesene18b13b2007-02-23 05:02:36 +0000784 else
785 WaterList.insert(IP, OrigBB);
Bob Wilson2f9be502009-10-15 20:49:47 +0000786 NewWaterList.insert(OrigBB);
Dale Johannesene18b13b2007-02-23 05:02:36 +0000787
Dale Johannesen01ee5752007-02-25 00:47:03 +0000788 unsigned OrigBBI = OrigBB->getNumber();
789 unsigned NewBBI = NewBB->getNumber();
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000790
Evan Chenge64f48b2009-08-01 06:13:52 +0000791 int delta = isThumb1 ? 2 : 4;
Dale Johannesenc17dd572010-07-23 22:50:23 +0000792
793 // Figure out how large the OrigBB is. As the first half of the original
794 // block, it cannot contain a tablejump. The size includes
795 // the new jump we added. (It should be possible to do this without
796 // recounting everything, but it's very confusing, and this is rarely
797 // executed.)
798 unsigned OrigBBSize = 0;
799 for (MachineBasicBlock::iterator I = OrigBB->begin(), E = OrigBB->end();
800 I != E; ++I)
801 OrigBBSize += TII->GetInstSizeInBytes(I);
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000802 BBInfo[OrigBBI].Size = OrigBBSize;
Dale Johannesen01ee5752007-02-25 00:47:03 +0000803
804 // ...and adjust BBOffsets for NewBB accordingly.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000805 BBInfo[NewBBI].Offset = BBInfo[OrigBBI].Offset + BBInfo[OrigBBI].Size;
Dale Johannesen01ee5752007-02-25 00:47:03 +0000806
Dale Johannesenc17dd572010-07-23 22:50:23 +0000807 // Figure out how large the NewMBB is. As the second half of the original
808 // block, it may contain a tablejump.
809 unsigned NewBBSize = 0;
810 for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
811 I != E; ++I)
812 NewBBSize += TII->GetInstSizeInBytes(I);
813 // Set the size of NewBB in BBSizes. It does not include any padding now.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000814 BBInfo[NewBBI].Size = NewBBSize;
Dale Johannesenc17dd572010-07-23 22:50:23 +0000815
Jakob Stoklund Olesencc6bfa82011-12-06 22:41:31 +0000816 MachineInstr* ThumbJTMI = prior(NewBB->end());
817 if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) {
818 // We've added another 2-byte instruction before this tablejump, which
819 // means we will always need padding if we didn't before, and vice versa.
820
821 // The original offset of the jump instruction was:
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000822 unsigned OrigOffset = BBInfo[OrigBBI].Offset + BBInfo[OrigBBI].Size - delta;
Jakob Stoklund Olesencc6bfa82011-12-06 22:41:31 +0000823 if (OrigOffset%4 == 0) {
824 // We had padding before and now we don't. No net change in code size.
825 delta = 0;
826 } else {
827 // We didn't have padding before and now we do.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000828 BBInfo[NewBBI].Size += 2;
Jakob Stoklund Olesencc6bfa82011-12-06 22:41:31 +0000829 delta = 4;
830 }
831 }
832
Dale Johannesen01ee5752007-02-25 00:47:03 +0000833 // All BBOffsets following these blocks must be modified.
Dale Johannesenc17dd572010-07-23 22:50:23 +0000834 if (delta)
835 AdjustBBOffsetsAfter(NewBB, delta);
Evan Cheng345877e2007-01-31 02:22:22 +0000836
837 return NewBB;
Evan Cheng10043e22007-01-19 07:51:42 +0000838}
839
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000840/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000841/// reference) is within MaxDisp of TrialOffset (a proposed location of a
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000842/// constant pool entry).
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000843bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
Evan Chengd2919a12009-07-23 18:27:47 +0000844 unsigned TrialOffset, unsigned MaxDisp,
845 bool NegativeOK, bool IsSoImm) {
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000846 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
847 // purposes of the displacement computation; compensate for that here.
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000848 // Effectively, the valid range of displacements is 2 bytes smaller for such
849 // references.
Evan Chenge41903b2009-08-14 18:31:44 +0000850 unsigned TotalAdj = 0;
851 if (isThumb && UserOffset%4 !=0) {
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000852 UserOffset -= 2;
Evan Chenge41903b2009-08-14 18:31:44 +0000853 TotalAdj = 2;
854 }
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000855 // CPEs will be rounded up to a multiple of 4.
Evan Chenge41903b2009-08-14 18:31:44 +0000856 if (isThumb && TrialOffset%4 != 0) {
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000857 TrialOffset += 2;
Evan Chenge41903b2009-08-14 18:31:44 +0000858 TotalAdj += 2;
859 }
860
861 // In Thumb2 mode, later branch adjustments can shift instructions up and
862 // cause alignment change. In the worst case scenario this can cause the
863 // user's effective address to be subtracted by 2 and the CPE's address to
864 // be plus 2.
865 if (isThumb2 && TotalAdj != 4)
866 MaxDisp -= (4 - TotalAdj);
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000867
Dale Johannesen01ee5752007-02-25 00:47:03 +0000868 if (UserOffset <= TrialOffset) {
869 // User before the Trial.
Evan Chengd2919a12009-07-23 18:27:47 +0000870 if (TrialOffset - UserOffset <= MaxDisp)
871 return true;
Evan Chengc26c76e2009-07-24 19:31:03 +0000872 // FIXME: Make use full range of soimm values.
Dale Johannesen01ee5752007-02-25 00:47:03 +0000873 } else if (NegativeOK) {
Evan Chengd2919a12009-07-23 18:27:47 +0000874 if (UserOffset - TrialOffset <= MaxDisp)
875 return true;
Evan Chengc26c76e2009-07-24 19:31:03 +0000876 // FIXME: Make use full range of soimm values.
Dale Johannesen01ee5752007-02-25 00:47:03 +0000877 }
878 return false;
879}
880
Dale Johannesene18b13b2007-02-23 05:02:36 +0000881/// WaterIsInRange - Returns true if a CPE placed after the specified
882/// Water (a basic block) will be in range for the specific MI.
883
884bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Evan Cheng87aaa192009-07-21 23:56:01 +0000885 MachineBasicBlock* Water, CPUser &U) {
Dale Johannesene59411d2007-07-11 18:32:38 +0000886 unsigned MaxDisp = U.MaxDisp;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000887 unsigned CPEOffset = BBInfo[Water->getNumber()].Offset +
888 BBInfo[Water->getNumber()].Size;
Dale Johannesene18b13b2007-02-23 05:02:36 +0000889
Dale Johannesend13786d2007-04-02 20:31:06 +0000890 // If the CPE is to be inserted before the instruction, that will raise
Bob Wilson5b07a902009-10-12 22:49:05 +0000891 // the offset of the instruction.
Dale Johannesend13786d2007-04-02 20:31:06 +0000892 if (CPEOffset < UserOffset)
Dale Johannesene59411d2007-07-11 18:32:38 +0000893 UserOffset += U.CPEMI->getOperand(2).getImm();
Dale Johannesend13786d2007-04-02 20:31:06 +0000894
Evan Chengd2919a12009-07-23 18:27:47 +0000895 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm);
Dale Johannesene18b13b2007-02-23 05:02:36 +0000896}
897
898/// CPEIsInRange - Returns true if the distance between specific MI and
Evan Cheng1f3fc4b2007-01-31 19:57:44 +0000899/// specific ConstPool entry instruction can fit in MI's displacement field.
Dale Johannesene18b13b2007-02-23 05:02:36 +0000900bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng87aaa192009-07-21 23:56:01 +0000901 MachineInstr *CPEMI, unsigned MaxDisp,
902 bool NegOk, bool DoDump) {
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000903 unsigned CPEOffset = GetOffsetOf(CPEMI);
Jim Grosbachb73918c2009-11-19 23:10:28 +0000904 assert((CPEOffset%4 == 0 || HasInlineAsm) && "Misaligned CPE");
Evan Cheng234e0312007-02-01 01:09:47 +0000905
Dale Johannesene18b13b2007-02-23 05:02:36 +0000906 if (DoDump) {
Chris Lattnera6f074f2009-08-23 03:41:05 +0000907 DEBUG(errs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
908 << " max delta=" << MaxDisp
909 << " insn address=" << UserOffset
910 << " CPE address=" << CPEOffset
911 << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI);
Dale Johannesene18b13b2007-02-23 05:02:36 +0000912 }
Evan Cheng1f3fc4b2007-01-31 19:57:44 +0000913
Evan Cheng87aaa192009-07-21 23:56:01 +0000914 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
Evan Cheng1f3fc4b2007-01-31 19:57:44 +0000915}
916
Evan Chenge4510972009-01-28 00:53:34 +0000917#ifndef NDEBUG
Evan Cheng8b7700f2007-02-09 20:54:44 +0000918/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
919/// unconditionally branches to its only successor.
920static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
921 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
922 return false;
923
924 MachineBasicBlock *Succ = *MBB->succ_begin();
925 MachineBasicBlock *Pred = *MBB->pred_begin();
926 MachineInstr *PredMI = &Pred->back();
David Goodwin27303cd2009-06-30 18:04:13 +0000927 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
928 || PredMI->getOpcode() == ARM::t2B)
Evan Cheng8b7700f2007-02-09 20:54:44 +0000929 return PredMI->getOperand(0).getMBB() == Succ;
930 return false;
931}
Evan Chenge4510972009-01-28 00:53:34 +0000932#endif // NDEBUG
Evan Cheng8b7700f2007-02-09 20:54:44 +0000933
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000934void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB,
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000935 int delta) {
Chris Lattnera48f44d2009-12-03 00:50:42 +0000936 MachineFunction::iterator MBBI = BB; MBBI = llvm::next(MBBI);
Evan Chengd2919a12009-07-23 18:27:47 +0000937 for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs();
938 i < e; ++i) {
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000939 BBInfo[i].Offset += delta;
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000940 // If some existing blocks have padding, adjust the padding as needed, a
941 // bit tricky. delta can be negative so don't use % on that.
Evan Chengd2919a12009-07-23 18:27:47 +0000942 if (!isThumb)
943 continue;
944 MachineBasicBlock *MBB = MBBI;
Jim Grosbachb73918c2009-11-19 23:10:28 +0000945 if (!MBB->empty() && !HasInlineAsm) {
Evan Chengd2919a12009-07-23 18:27:47 +0000946 // Constant pool entries require padding.
947 if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000948 unsigned OldOffset = BBInfo[i].Offset - delta;
949 if ((OldOffset%4) == 0 && (BBInfo[i].Offset%4) != 0) {
Evan Chengd2919a12009-07-23 18:27:47 +0000950 // add new padding
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000951 BBInfo[i].Size += 2;
Evan Chengd2919a12009-07-23 18:27:47 +0000952 delta += 2;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000953 } else if ((OldOffset%4) != 0 && (BBInfo[i].Offset%4) == 0) {
Evan Chengd2919a12009-07-23 18:27:47 +0000954 // remove existing padding
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000955 BBInfo[i].Size -= 2;
Evan Chengd2919a12009-07-23 18:27:47 +0000956 delta -= 2;
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000957 }
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000958 }
Evan Chengd2919a12009-07-23 18:27:47 +0000959 // Thumb1 jump tables require padding. They should be at the end;
960 // following unconditional branches are removed by AnalyzeBranch.
Evan Cheng3fabe072010-07-22 02:09:47 +0000961 // tBR_JTr expands to a mov pc followed by .align 2 and then the jump
Dale Johannesenc17dd572010-07-23 22:50:23 +0000962 // table entries. So this code checks whether offset of tBR_JTr
963 // is aligned; if it is, the offset of the jump table following the
964 // instruction will not be aligned, and we need padding.
Evan Cheng666c9122009-07-24 18:20:44 +0000965 MachineInstr *ThumbJTMI = prior(MBB->end());
Evan Chengf3a1fce2009-07-25 00:33:29 +0000966 if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) {
Dale Johannesenc17dd572010-07-23 22:50:23 +0000967 unsigned NewMIOffset = GetOffsetOf(ThumbJTMI);
Evan Cheng192d7c02009-08-11 07:36:14 +0000968 unsigned OldMIOffset = NewMIOffset - delta;
969 if ((OldMIOffset%4) == 0 && (NewMIOffset%4) != 0) {
Evan Chengd2919a12009-07-23 18:27:47 +0000970 // remove existing padding
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000971 BBInfo[i].Size -= 2;
Evan Chengd2919a12009-07-23 18:27:47 +0000972 delta -= 2;
Evan Cheng192d7c02009-08-11 07:36:14 +0000973 } else if ((OldMIOffset%4) != 0 && (NewMIOffset%4) == 0) {
Evan Chengd2919a12009-07-23 18:27:47 +0000974 // add new padding
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +0000975 BBInfo[i].Size += 2;
Evan Chengd2919a12009-07-23 18:27:47 +0000976 delta += 2;
977 }
978 }
979 if (delta==0)
980 return;
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000981 }
Chris Lattnera48f44d2009-12-03 00:50:42 +0000982 MBBI = llvm::next(MBBI);
Dale Johannesen4a00cf32007-04-29 19:19:30 +0000983 }
Dale Johannesen01ee5752007-02-25 00:47:03 +0000984}
985
Dale Johannesene18b13b2007-02-23 05:02:36 +0000986/// DecrementOldEntry - find the constant pool entry with index CPI
987/// and instruction CPEMI, and decrement its refcount. If the refcount
Bob Wilson2f4e56f2009-05-12 17:09:30 +0000988/// becomes 0 remove the entry and instruction. Returns true if we removed
Dale Johannesene18b13b2007-02-23 05:02:36 +0000989/// the entry, false if we didn't.
Evan Cheng10043e22007-01-19 07:51:42 +0000990
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000991bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
Evan Cheng8b7700f2007-02-09 20:54:44 +0000992 // Find the old entry. Eliminate it if it is no longer used.
Evan Cheng3c68d4e2007-04-03 23:39:48 +0000993 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
994 assert(CPE && "Unexpected!");
995 if (--CPE->RefCount == 0) {
996 RemoveDeadCPEMI(CPEMI);
997 CPE->CPEMI = NULL;
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000998 --NumCPEs;
Dale Johannesene18b13b2007-02-23 05:02:36 +0000999 return true;
1000 }
1001 return false;
1002}
1003
Dale Johannesene18b13b2007-02-23 05:02:36 +00001004/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1005/// if not, see if an in-range clone of the CPE is in range, and if so,
1006/// change the data structures so the user references the clone. Returns:
1007/// 0 = no existing entry found
1008/// 1 = entry found, and there were no code insertions or deletions
1009/// 2 = entry found, and there were code insertions or deletions
1010int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
1011{
1012 MachineInstr *UserMI = U.MI;
1013 MachineInstr *CPEMI = U.CPEMI;
1014
1015 // Check to see if the CPE is already in-range.
Evan Cheng87aaa192009-07-21 23:56:01 +00001016 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
Chris Lattneraf29ea62009-08-23 06:49:22 +00001017 DEBUG(errs() << "In range\n");
Dale Johannesene18b13b2007-02-23 05:02:36 +00001018 return 1;
Evan Cheng8b7700f2007-02-09 20:54:44 +00001019 }
1020
Dale Johannesene18b13b2007-02-23 05:02:36 +00001021 // No. Look for previously created clones of the CPE that are in range.
Chris Lattnera5bb3702007-12-30 23:10:15 +00001022 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesene18b13b2007-02-23 05:02:36 +00001023 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1024 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1025 // We already tried this one
1026 if (CPEs[i].CPEMI == CPEMI)
1027 continue;
1028 // Removing CPEs can leave empty entries, skip
1029 if (CPEs[i].CPEMI == NULL)
1030 continue;
Evan Cheng87aaa192009-07-21 23:56:01 +00001031 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
Chris Lattneraf29ea62009-08-23 06:49:22 +00001032 DEBUG(errs() << "Replacing CPE#" << CPI << " with CPE#"
1033 << CPEs[i].CPI << "\n");
Dale Johannesene18b13b2007-02-23 05:02:36 +00001034 // Point the CPUser node to the replacement
1035 U.CPEMI = CPEs[i].CPEMI;
1036 // Change the CPI in the instruction operand to refer to the clone.
1037 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001038 if (UserMI->getOperand(j).isCPI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +00001039 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
Dale Johannesene18b13b2007-02-23 05:02:36 +00001040 break;
1041 }
1042 // Adjust the refcount of the clone...
1043 CPEs[i].RefCount++;
1044 // ...and the original. If we didn't remove the old entry, none of the
1045 // addresses changed, so we don't need another pass.
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001046 return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
Dale Johannesene18b13b2007-02-23 05:02:36 +00001047 }
1048 }
1049 return 0;
1050}
1051
Dale Johannesen440995b2007-02-28 18:41:23 +00001052/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1053/// the specific unconditional branch instruction.
1054static inline unsigned getUnconditionalBrDisp(int Opc) {
David Goodwin27303cd2009-06-30 18:04:13 +00001055 switch (Opc) {
1056 case ARM::tB:
1057 return ((1<<10)-1)*2;
1058 case ARM::t2B:
1059 return ((1<<23)-1)*2;
1060 default:
1061 break;
1062 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001063
David Goodwin27303cd2009-06-30 18:04:13 +00001064 return ((1<<23)-1)*4;
Dale Johannesen440995b2007-02-28 18:41:23 +00001065}
1066
Bob Wilson2f9be502009-10-15 20:49:47 +00001067/// LookForWater - Look for an existing entry in the WaterList in which
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001068/// we can place the CPE referenced from U so it's within range of U's MI.
Bob Wilson2f9be502009-10-15 20:49:47 +00001069/// Returns true if found, false if not. If it returns true, WaterIter
Bob Wilsoncc121aa2009-10-12 21:23:15 +00001070/// is set to the WaterList entry. For Thumb, prefer water that will not
1071/// introduce padding to water that will. To ensure that this pass
1072/// terminates, the CPE location for a particular CPUser is only allowed to
1073/// move to a lower address, so search backward from the end of the list and
1074/// prefer the first water that is in range.
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001075bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
Bob Wilson2f9be502009-10-15 20:49:47 +00001076 water_iterator &WaterIter) {
Bob Wilson3a7326e2009-10-12 19:04:03 +00001077 if (WaterList.empty())
1078 return false;
1079
Bob Wilsone4adae22009-10-12 20:45:53 +00001080 bool FoundWaterThatWouldPad = false;
1081 water_iterator IPThatWouldPad;
Bob Wilson3a7326e2009-10-12 19:04:03 +00001082 for (water_iterator IP = prior(WaterList.end()),
1083 B = WaterList.begin();; --IP) {
1084 MachineBasicBlock* WaterBB = *IP;
Bob Wilson2f9be502009-10-15 20:49:47 +00001085 // Check if water is in range and is either at a lower address than the
1086 // current "high water mark" or a new water block that was created since
1087 // the previous iteration by inserting an unconditional branch. In the
1088 // latter case, we want to allow resetting the high water mark back to
1089 // this new water since we haven't seen it before. Inserting branches
1090 // should be relatively uncommon and when it does happen, we want to be
1091 // sure to take advantage of it for all the CPEs near that block, so that
1092 // we don't insert more branches than necessary.
1093 if (WaterIsInRange(UserOffset, WaterBB, U) &&
1094 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1095 NewWaterList.count(WaterBB))) {
Bob Wilson3a7326e2009-10-12 19:04:03 +00001096 unsigned WBBId = WaterBB->getNumber();
1097 if (isThumb &&
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001098 (BBInfo[WBBId].Offset + BBInfo[WBBId].Size)%4 != 0) {
Bob Wilson3a7326e2009-10-12 19:04:03 +00001099 // This is valid Water, but would introduce padding. Remember
1100 // it in case we don't find any Water that doesn't do this.
Bob Wilsone4adae22009-10-12 20:45:53 +00001101 if (!FoundWaterThatWouldPad) {
1102 FoundWaterThatWouldPad = true;
Bob Wilson3a7326e2009-10-12 19:04:03 +00001103 IPThatWouldPad = IP;
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001104 }
Bob Wilson3a7326e2009-10-12 19:04:03 +00001105 } else {
Bob Wilson2f9be502009-10-15 20:49:47 +00001106 WaterIter = IP;
Bob Wilson3a7326e2009-10-12 19:04:03 +00001107 return true;
Evan Chengd2919a12009-07-23 18:27:47 +00001108 }
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001109 }
Bob Wilson3a7326e2009-10-12 19:04:03 +00001110 if (IP == B)
1111 break;
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001112 }
Bob Wilsone4adae22009-10-12 20:45:53 +00001113 if (FoundWaterThatWouldPad) {
Bob Wilson2f9be502009-10-15 20:49:47 +00001114 WaterIter = IPThatWouldPad;
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001115 return true;
1116 }
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001117 return false;
1118}
1119
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001120/// CreateNewWater - No existing WaterList entry will work for
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001121/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1122/// block is used if in range, and the conditional branch munged so control
1123/// flow is correct. Otherwise the block is split to create a hole with an
Bob Wilson3250e772009-10-12 21:39:43 +00001124/// unconditional branch around it. In either case NewMBB is set to a
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001125/// block following which the new island can be inserted (the WaterList
1126/// is not adjusted).
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001127void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
Bob Wilson3250e772009-10-12 21:39:43 +00001128 unsigned UserOffset,
1129 MachineBasicBlock *&NewMBB) {
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001130 CPUser &U = CPUsers[CPUserIndex];
1131 MachineInstr *UserMI = U.MI;
1132 MachineInstr *CPEMI = U.CPEMI;
1133 MachineBasicBlock *UserMBB = UserMI->getParent();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001134 unsigned OffsetOfNextBlock = BBInfo[UserMBB->getNumber()].Offset +
1135 BBInfo[UserMBB->getNumber()].Size;
1136 assert(OffsetOfNextBlock== BBInfo[UserMBB->getNumber()+1].Offset);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001137
Bob Wilsonb4f2a852009-10-15 05:10:36 +00001138 // If the block does not end in an unconditional branch already, and if the
1139 // end of the block is within range, make new water there. (The addition
1140 // below is for the unconditional branch we will be adding: 4 bytes on ARM +
1141 // Thumb2, 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001142 // inside OffsetIsInRange.
Bob Wilsonb4f2a852009-10-15 05:10:36 +00001143 if (BBHasFallthrough(UserMBB) &&
Evan Chengd2919a12009-07-23 18:27:47 +00001144 OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4),
1145 U.MaxDisp, U.NegOk, U.IsSoImm)) {
Chris Lattneraf29ea62009-08-23 06:49:22 +00001146 DEBUG(errs() << "Split at end of block\n");
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001147 if (&UserMBB->back() == UserMI)
1148 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
Chris Lattnera48f44d2009-12-03 00:50:42 +00001149 NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001150 // Add an unconditional branch from UserMBB to fallthrough block.
1151 // Record it for branch lengthening; this new branch will not get out of
1152 // range, but if the preceding conditional branch is out of range, the
1153 // targets will be exchanged, and the altered branch may be out of
1154 // range, so the machinery has to know about it.
David Goodwin27303cd2009-06-30 18:04:13 +00001155 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
Owen Anderson29cfe6c2011-09-09 21:48:23 +00001156 if (!isThumb)
1157 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1158 else
1159 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
1160 .addImm(ARMCC::AL).addReg(0);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001161 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001162 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001163 MaxDisp, false, UncondBr));
Evan Chengd2919a12009-07-23 18:27:47 +00001164 int delta = isThumb1 ? 2 : 4;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001165 BBInfo[UserMBB->getNumber()].Size += delta;
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001166 AdjustBBOffsetsAfter(UserMBB, delta);
1167 } else {
1168 // What a big block. Find a place within the block to split it.
Evan Chengd2919a12009-07-23 18:27:47 +00001169 // This is a little tricky on Thumb1 since instructions are 2 bytes
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001170 // and constant pool entries are 4 bytes: if instruction I references
1171 // island CPE, and instruction I+1 references CPE', it will
1172 // not work well to put CPE as far forward as possible, since then
1173 // CPE' cannot immediately follow it (that location is 2 bytes
1174 // farther away from I+1 than CPE was from I) and we'd need to create
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001175 // a new island. So, we make a first guess, then walk through the
1176 // instructions between the one currently being looked at and the
1177 // possible insertion point, and make sure any other instructions
1178 // that reference CPEs will be able to use the same island area;
1179 // if not, we back up the insertion point.
1180
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001181 // The 4 in the following is for the unconditional branch we'll be
Evan Chengd2919a12009-07-23 18:27:47 +00001182 // inserting (allows for long branch on Thumb1). Alignment of the
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001183 // island is handled inside OffsetIsInRange.
1184 unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4;
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001185 // This could point off the end of the block if we've already got
1186 // constant pool entries following this block; only the last one is
1187 // in the water list. Back past any possible branches (allow for a
1188 // conditional and a maximally long unconditional).
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001189 if (BaseInsertOffset >= BBInfo[UserMBB->getNumber()+1].Offset)
1190 BaseInsertOffset = BBInfo[UserMBB->getNumber()+1].Offset -
Evan Chengd2919a12009-07-23 18:27:47 +00001191 (isThumb1 ? 6 : 8);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001192 unsigned EndInsertOffset = BaseInsertOffset +
1193 CPEMI->getOperand(2).getImm();
1194 MachineBasicBlock::iterator MI = UserMI;
1195 ++MI;
1196 unsigned CPUIndex = CPUserIndex+1;
Evan Cheng44a320d2010-08-12 20:30:05 +00001197 unsigned NumCPUsers = CPUsers.size();
1198 MachineInstr *LastIT = 0;
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001199 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001200 Offset < BaseInsertOffset;
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001201 Offset += TII->GetInstSizeInBytes(MI),
Evan Cheng44a320d2010-08-12 20:30:05 +00001202 MI = llvm::next(MI)) {
1203 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
Evan Chengd2919a12009-07-23 18:27:47 +00001204 CPUser &U = CPUsers[CPUIndex];
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001205 if (!OffsetIsInRange(Offset, EndInsertOffset,
Evan Chengd2919a12009-07-23 18:27:47 +00001206 U.MaxDisp, U.NegOk, U.IsSoImm)) {
1207 BaseInsertOffset -= (isThumb1 ? 2 : 4);
1208 EndInsertOffset -= (isThumb1 ? 2 : 4);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001209 }
1210 // This is overly conservative, as we don't account for CPEMIs
1211 // being reused within the block, but it doesn't matter much.
1212 EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1213 CPUIndex++;
1214 }
Evan Cheng44a320d2010-08-12 20:30:05 +00001215
1216 // Remember the last IT instruction.
1217 if (MI->getOpcode() == ARM::t2IT)
1218 LastIT = MI;
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001219 }
Evan Cheng44a320d2010-08-12 20:30:05 +00001220
Chris Lattneraf29ea62009-08-23 06:49:22 +00001221 DEBUG(errs() << "Split in middle of big block\n");
Evan Cheng44a320d2010-08-12 20:30:05 +00001222 --MI;
1223
1224 // Avoid splitting an IT block.
1225 if (LastIT) {
1226 unsigned PredReg = 0;
1227 ARMCC::CondCodes CC = llvm::getITInstrPredicate(MI, PredReg);
1228 if (CC != ARMCC::AL)
1229 MI = LastIT;
1230 }
1231 NewMBB = SplitBlockBeforeInstr(MI);
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001232 }
1233}
1234
Dale Johannesene18b13b2007-02-23 05:02:36 +00001235/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
Bob Wilsonce8cfb42009-05-12 17:35:29 +00001236/// is out-of-range. If so, pick up the constant pool value and move it some
Dale Johannesene18b13b2007-02-23 05:02:36 +00001237/// place in-range. Return true if we changed any addresses (thus must run
1238/// another pass of branch lengthening), false otherwise.
Evan Chengc6d70ae2009-07-29 02:18:14 +00001239bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF,
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001240 unsigned CPUserIndex) {
Dale Johannesen440995b2007-02-28 18:41:23 +00001241 CPUser &U = CPUsers[CPUserIndex];
Dale Johannesene18b13b2007-02-23 05:02:36 +00001242 MachineInstr *UserMI = U.MI;
1243 MachineInstr *CPEMI = U.CPEMI;
Chris Lattnera5bb3702007-12-30 23:10:15 +00001244 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesene18b13b2007-02-23 05:02:36 +00001245 unsigned Size = CPEMI->getOperand(2).getImm();
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001246 // Compute this only once, it's expensive. The 4 or 8 is the value the
Evan Chengdb73d682009-08-14 00:32:16 +00001247 // hardware keeps in the PC.
Dale Johannesene18b13b2007-02-23 05:02:36 +00001248 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
Evan Chengd9990f02007-04-27 08:14:15 +00001249
Dale Johannesene18b13b2007-02-23 05:02:36 +00001250 // See if the current entry is within range, or there is a clone of it
1251 // in range.
1252 int result = LookForExistingCPEntry(U, UserOffset);
1253 if (result==1) return false;
1254 else if (result==2) return true;
1255
1256 // No existing clone of this CPE is within range.
1257 // We will be generating a new clone. Get a UID for it.
Evan Chengdfce83c2011-01-17 08:03:18 +00001258 unsigned ID = AFI->createPICLabelUId();
Dale Johannesene18b13b2007-02-23 05:02:36 +00001259
Bob Wilsoncc121aa2009-10-12 21:23:15 +00001260 // Look for water where we can place this CPE.
Bob Wilson2f9be502009-10-15 20:49:47 +00001261 MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock();
1262 MachineBasicBlock *NewMBB;
1263 water_iterator IP;
1264 if (LookForWater(U, UserOffset, IP)) {
1265 DEBUG(errs() << "found water in range\n");
1266 MachineBasicBlock *WaterBB = *IP;
1267
1268 // If the original WaterList entry was "new water" on this iteration,
1269 // propagate that to the new island. This is just keeping NewWaterList
1270 // updated to match the WaterList, which will be updated below.
1271 if (NewWaterList.count(WaterBB)) {
1272 NewWaterList.erase(WaterBB);
1273 NewWaterList.insert(NewIsland);
1274 }
1275 // The new CPE goes before the following block (NewMBB).
Chris Lattnera48f44d2009-12-03 00:50:42 +00001276 NewMBB = llvm::next(MachineFunction::iterator(WaterBB));
Bob Wilson2f9be502009-10-15 20:49:47 +00001277
1278 } else {
Dale Johannesene18b13b2007-02-23 05:02:36 +00001279 // No water found.
Chris Lattneraf29ea62009-08-23 06:49:22 +00001280 DEBUG(errs() << "No water found\n");
Bob Wilson3250e772009-10-12 21:39:43 +00001281 CreateNewWater(CPUserIndex, UserOffset, NewMBB);
Bob Wilson2f9be502009-10-15 20:49:47 +00001282
1283 // SplitBlockBeforeInstr adds to WaterList, which is important when it is
1284 // called while handling branches so that the water will be seen on the
1285 // next iteration for constant pools, but in this context, we don't want
1286 // it. Check for this so it will be removed from the WaterList.
1287 // Also remove any entry from NewWaterList.
1288 MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB));
1289 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1290 if (IP != WaterList.end())
1291 NewWaterList.erase(WaterBB);
1292
1293 // We are adding new water. Update NewWaterList.
1294 NewWaterList.insert(NewIsland);
Dale Johannesene18b13b2007-02-23 05:02:36 +00001295 }
1296
Bob Wilson2f9be502009-10-15 20:49:47 +00001297 // Remove the original WaterList entry; we want subsequent insertions in
1298 // this vicinity to go after the one we're about to insert. This
1299 // considerably reduces the number of times we have to move the same CPE
1300 // more than once and is also important to ensure the algorithm terminates.
1301 if (IP != WaterList.end())
1302 WaterList.erase(IP);
1303
Dale Johannesene18b13b2007-02-23 05:02:36 +00001304 // Okay, we know we can put an island before NewMBB now, do it!
Evan Chengc6d70ae2009-07-29 02:18:14 +00001305 MF.insert(NewMBB, NewIsland);
Dale Johannesene18b13b2007-02-23 05:02:36 +00001306
1307 // Update internal data structures to account for the newly inserted MBB.
1308 UpdateForInsertedWaterBlock(NewIsland);
1309
1310 // Decrement the old entry, and remove it if refcount becomes 0.
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001311 DecrementOldEntry(CPI, CPEMI);
Dale Johannesene18b13b2007-02-23 05:02:36 +00001312
1313 // Now that we have an island to add the CPE to, clone the original CPE and
1314 // add it to the island.
Bob Wilson68ead6c2009-10-15 05:52:29 +00001315 U.HighWaterMark = NewIsland;
Chris Lattner6f306d72010-04-02 20:16:16 +00001316 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Evan Cheng10043e22007-01-19 07:51:42 +00001317 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Dale Johannesene18b13b2007-02-23 05:02:36 +00001318 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
Dan Gohmand2d1ae12010-06-22 15:08:57 +00001319 ++NumCPEs;
Evan Cheng8b7700f2007-02-09 20:54:44 +00001320
Jakob Stoklund Olesen2e05db22011-12-06 01:43:02 +00001321 // Mark the basic block as 4-byte aligned as required by the const-pool entry.
1322 NewIsland->setAlignment(2);
1323
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001324 BBInfo[NewIsland->getNumber()].Offset = BBInfo[NewMBB->getNumber()].Offset;
Evan Chengf9a4c692007-02-01 10:16:15 +00001325 // Compensate for .align 2 in thumb mode.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001326 if (isThumb && (BBInfo[NewIsland->getNumber()].Offset%4 != 0 || HasInlineAsm))
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001327 Size += 2;
Evan Cheng10043e22007-01-19 07:51:42 +00001328 // Increase the size of the island block to account for the new entry.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001329 BBInfo[NewIsland->getNumber()].Size += Size;
Dale Johannesen01ee5752007-02-25 00:47:03 +00001330 AdjustBBOffsetsAfter(NewIsland, Size);
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001331
Evan Cheng10043e22007-01-19 07:51:42 +00001332 // Finally, change the CPI in the instruction operand to be ID.
1333 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001334 if (UserMI->getOperand(i).isCPI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +00001335 UserMI->getOperand(i).setIndex(ID);
Evan Cheng10043e22007-01-19 07:51:42 +00001336 break;
1337 }
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001338
Chris Lattnera6f074f2009-08-23 03:41:05 +00001339 DEBUG(errs() << " Moved CPE to #" << ID << " CPI=" << CPI
1340 << '\t' << *UserMI);
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001341
Evan Cheng10043e22007-01-19 07:51:42 +00001342 return true;
1343}
1344
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001345/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1346/// sizes and offsets of impacted basic blocks.
1347void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1348 MachineBasicBlock *CPEBB = CPEMI->getParent();
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001349 unsigned Size = CPEMI->getOperand(2).getImm();
1350 CPEMI->eraseFromParent();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001351 BBInfo[CPEBB->getNumber()].Size -= Size;
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001352 // All succeeding offsets have the current size value added in, fix this.
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001353 if (CPEBB->empty()) {
Evan Chengd2919a12009-07-23 18:27:47 +00001354 // In thumb1 mode, the size of island may be padded by two to compensate for
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001355 // the alignment requirement. Then it will now be 2 when the block is
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001356 // empty, so fix this.
1357 // All succeeding offsets have the current size value added in, fix this.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001358 if (BBInfo[CPEBB->getNumber()].Size != 0) {
1359 Size += BBInfo[CPEBB->getNumber()].Size;
1360 BBInfo[CPEBB->getNumber()].Size = 0;
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001361 }
Jakob Stoklund Olesen2fa74482011-12-06 21:55:35 +00001362
1363 // This block no longer needs to be aligned. <rdar://problem/10534709>.
1364 CPEBB->setAlignment(0);
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001365 }
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001366 AdjustBBOffsetsAfter(CPEBB, -Size);
1367 // An island has only one predecessor BB and one successor BB. Check if
1368 // this BB's predecessor jumps directly to this BB's successor. This
1369 // shouldn't happen currently.
1370 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1371 // FIXME: remove the empty blocks after all the work is done?
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001372}
1373
1374/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1375/// are zero.
1376bool ARMConstantIslands::RemoveUnusedCPEntries() {
1377 unsigned MadeChange = false;
1378 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1379 std::vector<CPEntry> &CPEs = CPEntries[i];
1380 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1381 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1382 RemoveDeadCPEMI(CPEs[j].CPEMI);
1383 CPEs[j].CPEMI = NULL;
1384 MadeChange = true;
1385 }
1386 }
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001387 }
Evan Cheng3c68d4e2007-04-03 23:39:48 +00001388 return MadeChange;
1389}
1390
Dale Johannesene18b13b2007-02-23 05:02:36 +00001391/// BBIsInRange - Returns true if the distance between specific MI and
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001392/// specific BB can fit in MI's displacement field.
Evan Cheng1f3fc4b2007-01-31 19:57:44 +00001393bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1394 unsigned MaxDisp) {
Dale Johannesen962fa8e2007-02-28 23:20:38 +00001395 unsigned PCAdj = isThumb ? 4 : 8;
Evan Cheng1f3fc4b2007-01-31 19:57:44 +00001396 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001397 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001398
Chris Lattnera6f074f2009-08-23 03:41:05 +00001399 DEBUG(errs() << "Branch of destination BB#" << DestBB->getNumber()
1400 << " from BB#" << MI->getParent()->getNumber()
1401 << " max delta=" << MaxDisp
1402 << " from " << GetOffsetOf(MI) << " to " << DestOffset
1403 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
Evan Cheng1f3fc4b2007-01-31 19:57:44 +00001404
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001405 if (BrOffset <= DestOffset) {
1406 // Branch before the Dest.
1407 if (DestOffset-BrOffset <= MaxDisp)
1408 return true;
1409 } else {
1410 if (BrOffset-DestOffset <= MaxDisp)
1411 return true;
1412 }
1413 return false;
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001414}
1415
Evan Cheng7fa69642007-01-30 01:18:38 +00001416/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1417/// away to fit in its displacement field.
Evan Chengc6d70ae2009-07-29 02:18:14 +00001418bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) {
Evan Cheng22c7cf52007-01-25 03:12:46 +00001419 MachineInstr *MI = Br.MI;
Chris Lattnera5bb3702007-12-30 23:10:15 +00001420 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Cheng22c7cf52007-01-25 03:12:46 +00001421
Evan Cheng1f3fc4b2007-01-31 19:57:44 +00001422 // Check to see if the DestBB is already in-range.
1423 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001424 return false;
Evan Cheng22c7cf52007-01-25 03:12:46 +00001425
Evan Cheng7fa69642007-01-30 01:18:38 +00001426 if (!Br.isCond)
Evan Chengc6d70ae2009-07-29 02:18:14 +00001427 return FixUpUnconditionalBr(MF, Br);
1428 return FixUpConditionalBr(MF, Br);
Evan Cheng7fa69642007-01-30 01:18:38 +00001429}
Evan Cheng22c7cf52007-01-25 03:12:46 +00001430
Dale Johannesene18b13b2007-02-23 05:02:36 +00001431/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1432/// too far away to fit in its displacement field. If the LR register has been
Evan Cheng7fa69642007-01-30 01:18:38 +00001433/// spilled in the epilogue, then we can use BL to implement a far jump.
Bob Wilsonce8cfb42009-05-12 17:35:29 +00001434/// Otherwise, add an intermediate branch instruction to a branch.
Evan Cheng7fa69642007-01-30 01:18:38 +00001435bool
Evan Chengc6d70ae2009-07-29 02:18:14 +00001436ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Cheng7fa69642007-01-30 01:18:38 +00001437 MachineInstr *MI = Br.MI;
1438 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng317bd7a2009-08-07 05:45:07 +00001439 if (!isThumb1)
1440 llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!");
Evan Cheng7fa69642007-01-30 01:18:38 +00001441
1442 // Use BL to implement far jump.
1443 Br.MaxDisp = (1 << 21) * 2;
Chris Lattner59687512008-01-11 18:10:50 +00001444 MI->setDesc(TII->get(ARM::tBfar));
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001445 BBInfo[MBB->getNumber()].Size += 2;
Dale Johannesen01ee5752007-02-25 00:47:03 +00001446 AdjustBBOffsetsAfter(MBB, 2);
Evan Cheng7fa69642007-01-30 01:18:38 +00001447 HasFarJump = true;
Dan Gohmand2d1ae12010-06-22 15:08:57 +00001448 ++NumUBrFixed;
Evan Cheng36d559d2007-02-03 02:08:34 +00001449
Chris Lattnera6f074f2009-08-23 03:41:05 +00001450 DEBUG(errs() << " Changed B to long jump " << *MI);
Evan Cheng36d559d2007-02-03 02:08:34 +00001451
Evan Cheng7fa69642007-01-30 01:18:38 +00001452 return true;
1453}
1454
Dale Johannesene18b13b2007-02-23 05:02:36 +00001455/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
Evan Cheng7fa69642007-01-30 01:18:38 +00001456/// far away to fit in its displacement field. It is converted to an inverse
1457/// conditional branch + an unconditional branch to the destination.
1458bool
Evan Chengc6d70ae2009-07-29 02:18:14 +00001459ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Cheng7fa69642007-01-30 01:18:38 +00001460 MachineInstr *MI = Br.MI;
Chris Lattnera5bb3702007-12-30 23:10:15 +00001461 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Cheng7fa69642007-01-30 01:18:38 +00001462
Bob Wilsonce8cfb42009-05-12 17:35:29 +00001463 // Add an unconditional branch to the destination and invert the branch
Evan Cheng7fa69642007-01-30 01:18:38 +00001464 // condition to jump over it:
Evan Cheng22c7cf52007-01-25 03:12:46 +00001465 // blt L1
1466 // =>
1467 // bge L2
1468 // b L1
1469 // L2:
Chris Lattner5c463782007-12-30 20:49:49 +00001470 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
Evan Cheng22c7cf52007-01-25 03:12:46 +00001471 CC = ARMCC::getOppositeCondition(CC);
Evan Cheng94f04c62007-07-05 07:18:20 +00001472 unsigned CCReg = MI->getOperand(2).getReg();
Evan Cheng22c7cf52007-01-25 03:12:46 +00001473
1474 // If the branch is at the end of its MBB and that has a fall-through block,
1475 // direct the updated conditional branch to the fall-through block. Otherwise,
1476 // split the MBB before the next instruction.
1477 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng36d559d2007-02-03 02:08:34 +00001478 MachineInstr *BMI = &MBB->back();
1479 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001480
Dan Gohmand2d1ae12010-06-22 15:08:57 +00001481 ++NumCBrFixed;
Evan Cheng36d559d2007-02-03 02:08:34 +00001482 if (BMI != MI) {
Chris Lattnera48f44d2009-12-03 00:50:42 +00001483 if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
Evan Cheng36d559d2007-02-03 02:08:34 +00001484 BMI->getOpcode() == Br.UncondBr) {
Bob Wilsonce8cfb42009-05-12 17:35:29 +00001485 // Last MI in the BB is an unconditional branch. Can we simply invert the
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001486 // condition and swap destinations:
1487 // beq L1
1488 // b L2
1489 // =>
1490 // bne L2
1491 // b L1
Chris Lattnera5bb3702007-12-30 23:10:15 +00001492 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
Evan Cheng1f3fc4b2007-01-31 19:57:44 +00001493 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Chris Lattnera6f074f2009-08-23 03:41:05 +00001494 DEBUG(errs() << " Invert Bcc condition and swap its destination with "
1495 << *BMI);
Chris Lattnera5bb3702007-12-30 23:10:15 +00001496 BMI->getOperand(0).setMBB(DestBB);
1497 MI->getOperand(0).setMBB(NewDest);
Evan Cheng3c9dc6b2007-01-26 20:38:26 +00001498 MI->getOperand(1).setImm(CC);
1499 return true;
1500 }
1501 }
1502 }
1503
1504 if (NeedSplit) {
Evan Cheng22c7cf52007-01-25 03:12:46 +00001505 SplitBlockBeforeInstr(MI);
Bob Wilsonce8cfb42009-05-12 17:35:29 +00001506 // No need for the branch to the next block. We're adding an unconditional
Evan Cheng1e270b62007-01-26 02:02:39 +00001507 // branch to the destination.
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001508 int delta = TII->GetInstSizeInBytes(&MBB->back());
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001509 BBInfo[MBB->getNumber()].Size -= delta;
Chris Lattnera48f44d2009-12-03 00:50:42 +00001510 MachineBasicBlock* SplitBB = llvm::next(MachineFunction::iterator(MBB));
Dale Johannesen4a00cf32007-04-29 19:19:30 +00001511 AdjustBBOffsetsAfter(SplitBB, -delta);
Evan Cheng1e270b62007-01-26 02:02:39 +00001512 MBB->back().eraseFromParent();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001513 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
Evan Cheng1e270b62007-01-26 02:02:39 +00001514 }
Chris Lattnera48f44d2009-12-03 00:50:42 +00001515 MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB));
Bob Wilson2f4e56f2009-05-12 17:09:30 +00001516
Chris Lattneraf29ea62009-08-23 06:49:22 +00001517 DEBUG(errs() << " Insert B to BB#" << DestBB->getNumber()
1518 << " also invert condition and change dest. to BB#"
1519 << NextBB->getNumber() << "\n");
Evan Cheng22c7cf52007-01-25 03:12:46 +00001520
Dale Johannesenfdfb7572007-04-23 20:09:04 +00001521 // Insert a new conditional branch and a new unconditional branch.
Evan Cheng22c7cf52007-01-25 03:12:46 +00001522 // Also update the ImmBranch as well as adding a new entry for the new branch.
Chris Lattner6f306d72010-04-02 20:16:16 +00001523 BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
Dale Johannesen7647da62009-02-13 02:25:56 +00001524 .addMBB(NextBB).addImm(CC).addReg(CCReg);
Evan Cheng22c7cf52007-01-25 03:12:46 +00001525 Br.MI = &MBB->back();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001526 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Owen Anderson93cd3182011-09-09 23:05:14 +00001527 if (isThumb)
1528 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
1529 .addImm(ARMCC::AL).addReg(0);
1530 else
1531 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001532 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
Evan Cheng7169bd82007-01-31 18:29:27 +00001533 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Cheng1d138982007-01-25 23:31:04 +00001534 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Dale Johannesenfdfb7572007-04-23 20:09:04 +00001535
1536 // Remove the old conditional branch. It may or may not still be in MBB.
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001537 BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
Evan Cheng22c7cf52007-01-25 03:12:46 +00001538 MI->eraseFromParent();
1539
Dale Johannesenfdfb7572007-04-23 20:09:04 +00001540 // The net size change is an addition of one unconditional branch.
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001541 int delta = TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesen01ee5752007-02-25 00:47:03 +00001542 AdjustBBOffsetsAfter(MBB, delta);
Evan Cheng22c7cf52007-01-25 03:12:46 +00001543 return true;
1544}
Evan Cheng7fa69642007-01-30 01:18:38 +00001545
Evan Cheng7fa69642007-01-30 01:18:38 +00001546/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
Evan Chengcc9ca352009-08-11 21:11:32 +00001547/// LR / restores LR to pc. FIXME: This is done here because it's only possible
1548/// to do this if tBfar is not used.
Evan Cheng7fa69642007-01-30 01:18:38 +00001549bool ARMConstantIslands::UndoLRSpillRestore() {
1550 bool MadeChange = false;
1551 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1552 MachineInstr *MI = PushPopMIs[i];
Bob Wilson947f04b2010-03-13 01:08:20 +00001553 // First two operands are predicates.
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001554 if (MI->getOpcode() == ARM::tPOP_RET &&
Bob Wilson947f04b2010-03-13 01:08:20 +00001555 MI->getOperand(2).getReg() == ARM::PC &&
1556 MI->getNumExplicitOperands() == 3) {
Jim Grosbach74719372011-07-08 21:50:04 +00001557 // Create the new insn and copy the predicate from the old.
1558 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET))
1559 .addOperand(MI->getOperand(0))
1560 .addOperand(MI->getOperand(1));
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001561 MI->eraseFromParent();
1562 MadeChange = true;
Evan Cheng7fa69642007-01-30 01:18:38 +00001563 }
1564 }
1565 return MadeChange;
1566}
Evan Chengc6d70ae2009-07-29 02:18:14 +00001567
Evan Chengdb73d682009-08-14 00:32:16 +00001568bool ARMConstantIslands::OptimizeThumb2Instructions(MachineFunction &MF) {
1569 bool MadeChange = false;
1570
1571 // Shrink ADR and LDR from constantpool.
1572 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
1573 CPUser &U = CPUsers[i];
1574 unsigned Opcode = U.MI->getOpcode();
1575 unsigned NewOpc = 0;
1576 unsigned Scale = 1;
1577 unsigned Bits = 0;
1578 switch (Opcode) {
1579 default: break;
Owen Anderson9a4d4282010-12-13 22:51:08 +00001580 case ARM::t2LEApcrel:
Evan Chengdb73d682009-08-14 00:32:16 +00001581 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1582 NewOpc = ARM::tLEApcrel;
1583 Bits = 8;
1584 Scale = 4;
1585 }
1586 break;
1587 case ARM::t2LDRpci:
1588 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1589 NewOpc = ARM::tLDRpci;
1590 Bits = 8;
1591 Scale = 4;
1592 }
1593 break;
1594 }
1595
1596 if (!NewOpc)
1597 continue;
1598
1599 unsigned UserOffset = GetOffsetOf(U.MI) + 4;
1600 unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
1601 // FIXME: Check if offset is multiple of scale if scale is not 4.
1602 if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
1603 U.MI->setDesc(TII->get(NewOpc));
1604 MachineBasicBlock *MBB = U.MI->getParent();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001605 BBInfo[MBB->getNumber()].Size -= 2;
Evan Chengdb73d682009-08-14 00:32:16 +00001606 AdjustBBOffsetsAfter(MBB, -2);
1607 ++NumT2CPShrunk;
1608 MadeChange = true;
1609 }
1610 }
1611
Evan Chengdb73d682009-08-14 00:32:16 +00001612 MadeChange |= OptimizeThumb2Branches(MF);
Jim Grosbach6385ea72009-11-12 03:28:35 +00001613 MadeChange |= OptimizeThumb2JumpTables(MF);
Evan Chengdb73d682009-08-14 00:32:16 +00001614 return MadeChange;
1615}
1616
1617bool ARMConstantIslands::OptimizeThumb2Branches(MachineFunction &MF) {
Evan Chenge41903b2009-08-14 18:31:44 +00001618 bool MadeChange = false;
1619
1620 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
1621 ImmBranch &Br = ImmBranches[i];
1622 unsigned Opcode = Br.MI->getOpcode();
1623 unsigned NewOpc = 0;
1624 unsigned Scale = 1;
1625 unsigned Bits = 0;
1626 switch (Opcode) {
1627 default: break;
1628 case ARM::t2B:
1629 NewOpc = ARM::tB;
1630 Bits = 11;
1631 Scale = 2;
1632 break;
Evan Cheng6f29ad92009-10-31 23:46:45 +00001633 case ARM::t2Bcc: {
Evan Chenge41903b2009-08-14 18:31:44 +00001634 NewOpc = ARM::tBcc;
1635 Bits = 8;
Evan Cheng6f29ad92009-10-31 23:46:45 +00001636 Scale = 2;
Evan Chenge41903b2009-08-14 18:31:44 +00001637 break;
1638 }
Evan Cheng6f29ad92009-10-31 23:46:45 +00001639 }
1640 if (NewOpc) {
1641 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
1642 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
1643 if (BBIsInRange(Br.MI, DestBB, MaxOffs)) {
1644 Br.MI->setDesc(TII->get(NewOpc));
1645 MachineBasicBlock *MBB = Br.MI->getParent();
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001646 BBInfo[MBB->getNumber()].Size -= 2;
Evan Cheng6f29ad92009-10-31 23:46:45 +00001647 AdjustBBOffsetsAfter(MBB, -2);
1648 ++NumT2BrShrunk;
1649 MadeChange = true;
1650 }
1651 }
1652
1653 Opcode = Br.MI->getOpcode();
1654 if (Opcode != ARM::tBcc)
Evan Chenge41903b2009-08-14 18:31:44 +00001655 continue;
1656
Evan Cheng6f29ad92009-10-31 23:46:45 +00001657 NewOpc = 0;
1658 unsigned PredReg = 0;
1659 ARMCC::CondCodes Pred = llvm::getInstrPredicate(Br.MI, PredReg);
1660 if (Pred == ARMCC::EQ)
1661 NewOpc = ARM::tCBZ;
1662 else if (Pred == ARMCC::NE)
1663 NewOpc = ARM::tCBNZ;
1664 if (!NewOpc)
1665 continue;
Evan Chenge41903b2009-08-14 18:31:44 +00001666 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
Evan Cheng6f29ad92009-10-31 23:46:45 +00001667 // Check if the distance is within 126. Subtract starting offset by 2
1668 // because the cmp will be eliminated.
1669 unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001670 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
Evan Cheng6f29ad92009-10-31 23:46:45 +00001671 if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
Evan Cheng88530e62011-04-01 22:09:28 +00001672 MachineBasicBlock::iterator CmpMI = Br.MI;
1673 if (CmpMI != Br.MI->getParent()->begin()) {
1674 --CmpMI;
1675 if (CmpMI->getOpcode() == ARM::tCMPi8) {
1676 unsigned Reg = CmpMI->getOperand(0).getReg();
1677 Pred = llvm::getInstrPredicate(CmpMI, PredReg);
1678 if (Pred == ARMCC::AL &&
1679 CmpMI->getOperand(1).getImm() == 0 &&
1680 isARMLowRegister(Reg)) {
1681 MachineBasicBlock *MBB = Br.MI->getParent();
1682 MachineInstr *NewBR =
1683 BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc))
1684 .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags());
1685 CmpMI->eraseFromParent();
1686 Br.MI->eraseFromParent();
1687 Br.MI = NewBR;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001688 BBInfo[MBB->getNumber()].Size -= 2;
Evan Cheng88530e62011-04-01 22:09:28 +00001689 AdjustBBOffsetsAfter(MBB, -2);
1690 ++NumCBZ;
1691 MadeChange = true;
1692 }
Evan Cheng6f29ad92009-10-31 23:46:45 +00001693 }
1694 }
Evan Chenge41903b2009-08-14 18:31:44 +00001695 }
1696 }
1697
1698 return MadeChange;
Evan Chengdb73d682009-08-14 00:32:16 +00001699}
1700
Evan Chengdb73d682009-08-14 00:32:16 +00001701/// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
1702/// jumptables when it's possible.
Evan Chengc6d70ae2009-07-29 02:18:14 +00001703bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) {
1704 bool MadeChange = false;
1705
1706 // FIXME: After the tables are shrunk, can we get rid some of the
1707 // constantpool tables?
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001708 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnera14ac3fd2010-01-25 23:22:00 +00001709 if (MJTI == 0) return false;
Jim Grosbache4ba2aa2010-07-07 21:06:51 +00001710
Evan Chengc6d70ae2009-07-29 02:18:14 +00001711 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1712 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1713 MachineInstr *MI = T2JumpTables[i];
Evan Cheng6cc775f2011-06-28 19:10:37 +00001714 const MCInstrDesc &MCID = MI->getDesc();
1715 unsigned NumOps = MCID.getNumOperands();
1716 unsigned JTOpIdx = NumOps - (MCID.isPredicable() ? 3 : 2);
Evan Chengc6d70ae2009-07-29 02:18:14 +00001717 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1718 unsigned JTI = JTOP.getIndex();
1719 assert(JTI < JT.size());
1720
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001721 bool ByteOk = true;
1722 bool HalfWordOk = true;
Jim Grosbach5d577142009-11-12 17:25:07 +00001723 unsigned JTOffset = GetOffsetOf(MI) + 4;
1724 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Evan Chengc6d70ae2009-07-29 02:18:14 +00001725 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1726 MachineBasicBlock *MBB = JTBBs[j];
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001727 unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
Evan Chenge3493a92009-07-29 23:20:20 +00001728 // Negative offset is not ok. FIXME: We should change BB layout to make
1729 // sure all the branches are forward.
Evan Chengf6d0fa32009-07-31 18:28:05 +00001730 if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
Evan Chengc6d70ae2009-07-29 02:18:14 +00001731 ByteOk = false;
Evan Chenge64f48b2009-08-01 06:13:52 +00001732 unsigned TBHLimit = ((1<<16)-1)*2;
Evan Chenge64f48b2009-08-01 06:13:52 +00001733 if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
Evan Chengc6d70ae2009-07-29 02:18:14 +00001734 HalfWordOk = false;
1735 if (!ByteOk && !HalfWordOk)
1736 break;
1737 }
1738
1739 if (ByteOk || HalfWordOk) {
1740 MachineBasicBlock *MBB = MI->getParent();
1741 unsigned BaseReg = MI->getOperand(0).getReg();
1742 bool BaseRegKill = MI->getOperand(0).isKill();
1743 if (!BaseRegKill)
1744 continue;
1745 unsigned IdxReg = MI->getOperand(1).getReg();
1746 bool IdxRegKill = MI->getOperand(1).isKill();
Jim Grosbach40eda102010-07-07 22:51:22 +00001747
1748 // Scan backwards to find the instruction that defines the base
1749 // register. Due to post-RA scheduling, we can't count on it
1750 // immediately preceding the branch instruction.
Evan Chengc6d70ae2009-07-29 02:18:14 +00001751 MachineBasicBlock::iterator PrevI = MI;
Jim Grosbach40eda102010-07-07 22:51:22 +00001752 MachineBasicBlock::iterator B = MBB->begin();
1753 while (PrevI != B && !PrevI->definesRegister(BaseReg))
1754 --PrevI;
1755
1756 // If for some reason we didn't find it, we can't do anything, so
1757 // just skip this one.
1758 if (!PrevI->definesRegister(BaseReg))
Evan Chengc6d70ae2009-07-29 02:18:14 +00001759 continue;
1760
Jim Grosbach40eda102010-07-07 22:51:22 +00001761 MachineInstr *AddrMI = PrevI;
Evan Chengc6d70ae2009-07-29 02:18:14 +00001762 bool OptOk = true;
Jim Grosbache4ba2aa2010-07-07 21:06:51 +00001763 // Examine the instruction that calculates the jumptable entry address.
Jim Grosbach40eda102010-07-07 22:51:22 +00001764 // Make sure it only defines the base register and kills any uses
1765 // other than the index register.
Evan Chengc6d70ae2009-07-29 02:18:14 +00001766 for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
1767 const MachineOperand &MO = AddrMI->getOperand(k);
1768 if (!MO.isReg() || !MO.getReg())
1769 continue;
1770 if (MO.isDef() && MO.getReg() != BaseReg) {
1771 OptOk = false;
1772 break;
1773 }
1774 if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
1775 OptOk = false;
1776 break;
1777 }
1778 }
1779 if (!OptOk)
1780 continue;
1781
Owen Anderson9a4d4282010-12-13 22:51:08 +00001782 // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction
Jim Grosbach40eda102010-07-07 22:51:22 +00001783 // that gave us the initial base register definition.
1784 for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI)
1785 ;
1786
Owen Anderson9a4d4282010-12-13 22:51:08 +00001787 // The instruction should be a tLEApcrel or t2LEApcrelJT; we want
Evan Chengdb73d682009-08-14 00:32:16 +00001788 // to delete it as well.
Jim Grosbach40eda102010-07-07 22:51:22 +00001789 MachineInstr *LeaMI = PrevI;
Evan Chengdb73d682009-08-14 00:32:16 +00001790 if ((LeaMI->getOpcode() != ARM::tLEApcrelJT &&
Owen Anderson9a4d4282010-12-13 22:51:08 +00001791 LeaMI->getOpcode() != ARM::t2LEApcrelJT) ||
Evan Chengc6d70ae2009-07-29 02:18:14 +00001792 LeaMI->getOperand(0).getReg() != BaseReg)
Evan Chenge64f48b2009-08-01 06:13:52 +00001793 OptOk = false;
Evan Chengc6d70ae2009-07-29 02:18:14 +00001794
Evan Chenge64f48b2009-08-01 06:13:52 +00001795 if (!OptOk)
1796 continue;
1797
Jim Grosbach81af4f92010-11-29 21:28:32 +00001798 unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
Evan Chenge64f48b2009-08-01 06:13:52 +00001799 MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
1800 .addReg(IdxReg, getKillRegState(IdxRegKill))
1801 .addJumpTableIndex(JTI, JTOP.getTargetFlags())
1802 .addImm(MI->getOperand(JTOpIdx+1).getImm());
1803 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1804 // is 2-byte aligned. For now, asm printer will fix it up.
1805 unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
1806 unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
1807 OrigSize += TII->GetInstSizeInBytes(LeaMI);
1808 OrigSize += TII->GetInstSizeInBytes(MI);
1809
1810 AddrMI->eraseFromParent();
1811 LeaMI->eraseFromParent();
1812 MI->eraseFromParent();
1813
1814 int delta = OrigSize - NewSize;
Jakob Stoklund Olesene2b3ff22011-12-07 01:08:25 +00001815 BBInfo[MBB->getNumber()].Size -= delta;
Evan Chenge64f48b2009-08-01 06:13:52 +00001816 AdjustBBOffsetsAfter(MBB, -delta);
1817
1818 ++NumTBs;
1819 MadeChange = true;
Evan Chengc6d70ae2009-07-29 02:18:14 +00001820 }
1821 }
1822
1823 return MadeChange;
1824}
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001825
Jim Grosbach87b0f0d2009-11-16 18:55:47 +00001826/// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that
1827/// jump tables always branch forwards, since that's what tbb and tbh need.
Jim Grosbach5d577142009-11-12 17:25:07 +00001828bool ARMConstantIslands::ReorderThumb2JumpTables(MachineFunction &MF) {
1829 bool MadeChange = false;
1830
1831 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
Chris Lattnera14ac3fd2010-01-25 23:22:00 +00001832 if (MJTI == 0) return false;
Jim Grosbache4ba2aa2010-07-07 21:06:51 +00001833
Jim Grosbach5d577142009-11-12 17:25:07 +00001834 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1835 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1836 MachineInstr *MI = T2JumpTables[i];
Evan Cheng6cc775f2011-06-28 19:10:37 +00001837 const MCInstrDesc &MCID = MI->getDesc();
1838 unsigned NumOps = MCID.getNumOperands();
1839 unsigned JTOpIdx = NumOps - (MCID.isPredicable() ? 3 : 2);
Jim Grosbach5d577142009-11-12 17:25:07 +00001840 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1841 unsigned JTI = JTOP.getIndex();
1842 assert(JTI < JT.size());
1843
1844 // We prefer if target blocks for the jump table come after the jump
1845 // instruction so we can use TB[BH]. Loop through the target blocks
1846 // and try to adjust them such that that's true.
Jim Grosbach9785e592009-11-16 18:58:52 +00001847 int JTNumber = MI->getParent()->getNumber();
Jim Grosbach5d577142009-11-12 17:25:07 +00001848 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1849 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1850 MachineBasicBlock *MBB = JTBBs[j];
Jim Grosbach9785e592009-11-16 18:58:52 +00001851 int DTNumber = MBB->getNumber();
Jim Grosbach5d577142009-11-12 17:25:07 +00001852
Jim Grosbach9785e592009-11-16 18:58:52 +00001853 if (DTNumber < JTNumber) {
Jim Grosbach5d577142009-11-12 17:25:07 +00001854 // The destination precedes the switch. Try to move the block forward
1855 // so we have a positive offset.
1856 MachineBasicBlock *NewBB =
1857 AdjustJTTargetBlockForward(MBB, MI->getParent());
1858 if (NewBB)
Jim Grosbach43d21082009-11-14 20:10:18 +00001859 MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
Jim Grosbach5d577142009-11-12 17:25:07 +00001860 MadeChange = true;
1861 }
1862 }
1863 }
1864
1865 return MadeChange;
1866}
1867
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001868MachineBasicBlock *ARMConstantIslands::
1869AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB)
1870{
1871 MachineFunction &MF = *BB->getParent();
1872
Jim Grosbach73ef80f2010-07-07 22:53:35 +00001873 // If the destination block is terminated by an unconditional branch,
Jim Grosbach5d577142009-11-12 17:25:07 +00001874 // try to move it; otherwise, create a new block following the jump
Jim Grosbach9785e592009-11-16 18:58:52 +00001875 // table that branches back to the actual target. This is a very simple
1876 // heuristic. FIXME: We can definitely improve it.
Jim Grosbach5d577142009-11-12 17:25:07 +00001877 MachineBasicBlock *TBB = 0, *FBB = 0;
1878 SmallVector<MachineOperand, 4> Cond;
Jim Grosbachaf1ad302009-11-17 01:21:04 +00001879 SmallVector<MachineOperand, 4> CondPrior;
1880 MachineFunction::iterator BBi = BB;
1881 MachineFunction::iterator OldPrior = prior(BBi);
Jim Grosbach43d21082009-11-14 20:10:18 +00001882
Jim Grosbach47d5e332009-11-16 17:10:56 +00001883 // If the block terminator isn't analyzable, don't try to move the block
Jim Grosbachaf1ad302009-11-17 01:21:04 +00001884 bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond);
Jim Grosbach47d5e332009-11-16 17:10:56 +00001885
Jim Grosbachaf1ad302009-11-17 01:21:04 +00001886 // If the block ends in an unconditional branch, move it. The prior block
1887 // has to have an analyzable terminator for us to move this one. Be paranoid
Jim Grosbach9785e592009-11-16 18:58:52 +00001888 // and make sure we're not trying to move the entry block of the function.
Jim Grosbachaf1ad302009-11-17 01:21:04 +00001889 if (!B && Cond.empty() && BB != MF.begin() &&
1890 !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
Jim Grosbach5d577142009-11-12 17:25:07 +00001891 BB->moveAfter(JTBB);
1892 OldPrior->updateTerminator();
Jim Grosbach43d21082009-11-14 20:10:18 +00001893 BB->updateTerminator();
Jim Grosbach9785e592009-11-16 18:58:52 +00001894 // Update numbering to account for the block being moved.
Jim Grosbachaf1ad302009-11-17 01:21:04 +00001895 MF.RenumberBlocks();
Jim Grosbach5d577142009-11-12 17:25:07 +00001896 ++NumJTMoved;
1897 return NULL;
1898 }
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001899
1900 // Create a new MBB for the code after the jump BB.
1901 MachineBasicBlock *NewBB =
1902 MF.CreateMachineBasicBlock(JTBB->getBasicBlock());
1903 MachineFunction::iterator MBBI = JTBB; ++MBBI;
1904 MF.insert(MBBI, NewBB);
1905
1906 // Add an unconditional branch from NewBB to BB.
1907 // There doesn't seem to be meaningful DebugInfo available; this doesn't
1908 // correspond directly to anything in the source.
1909 assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?");
Owen Anderson29cfe6c2011-09-09 21:48:23 +00001910 BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
1911 .addImm(ARMCC::AL).addReg(0);
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001912
Jim Grosbach43d21082009-11-14 20:10:18 +00001913 // Update internal data structures to account for the newly inserted MBB.
1914 MF.RenumberBlocks(NewBB);
1915
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001916 // Update the CFG.
1917 NewBB->addSuccessor(BB);
1918 JTBB->removeSuccessor(BB);
1919 JTBB->addSuccessor(NewBB);
1920
Jim Grosbach5d577142009-11-12 17:25:07 +00001921 ++NumJTInserted;
Jim Grosbach8d92ec42009-11-11 02:47:19 +00001922 return NewBB;
1923}