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