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