1 //===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
17 #include "ARMMachineFunctionInfo.h"
18 #include "MCTargetDesc/ARMAddressingModes.h"
19 #include "Thumb2InstrInfo.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineJumpTableInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetMachine.h"
38 #define DEBUG_TYPE "arm-cp-islands"
40 STATISTIC(NumCPEs, "Number of constpool entries");
41 STATISTIC(NumSplit, "Number of uncond branches inserted");
42 STATISTIC(NumCBrFixed, "Number of cond branches fixed");
43 STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
44 STATISTIC(NumTBs, "Number of table branches generated");
45 STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk");
46 STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk");
47 STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed");
48 STATISTIC(NumJTMoved, "Number of jump table destination blocks moved");
49 STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted");
53 AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true),
54 cl::desc("Adjust basic block layout to better use TB[BH]"));
56 // FIXME: This option should be removed once it has received sufficient testing.
58 AlignConstantIslands("arm-align-constant-islands", cl::Hidden, cl::init(true),
59 cl::desc("Align constant islands in code"));
61 /// UnknownPadding - Return the worst case padding that could result from
62 /// unknown offset bits. This does not include alignment padding caused by
63 /// known offset bits.
65 /// @param LogAlign log2(alignment)
66 /// @param KnownBits Number of known low offset bits.
67 static inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) {
68 if (KnownBits < LogAlign)
69 return (1u << LogAlign) - (1u << KnownBits);
74 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
75 /// requires constant pool entries to be scattered among the instructions
76 /// inside a function. To do this, it completely ignores the normal LLVM
77 /// constant pool; instead, it places constants wherever it feels like with
78 /// special instructions.
80 /// The terminology used in this pass includes:
81 /// Islands - Clumps of constants placed in the function.
82 /// Water - Potential places where an island could be formed.
83 /// CPE - A constant pool entry that has been placed somewhere, which
84 /// tracks a list of users.
85 class ARMConstantIslands : public MachineFunctionPass {
86 /// BasicBlockInfo - Information about the offset and size of a single
88 struct BasicBlockInfo {
89 /// Offset - Distance from the beginning of the function to the beginning
90 /// of this basic block.
92 /// Offsets are computed assuming worst case padding before an aligned
93 /// block. This means that subtracting basic block offsets always gives a
94 /// conservative estimate of the real distance which may be smaller.
96 /// Because worst case padding is used, the computed offset of an aligned
97 /// block may not actually be aligned.
100 /// Size - Size of the basic block in bytes. If the block contains
101 /// inline assembly, this is a worst case estimate.
103 /// The size does not include any alignment padding whether from the
104 /// beginning of the block, or from an aligned jump table at the end.
107 /// KnownBits - The number of low bits in Offset that are known to be
108 /// exact. The remaining bits of Offset are an upper bound.
111 /// Unalign - When non-zero, the block contains instructions (inline asm)
112 /// of unknown size. The real size may be smaller than Size bytes by a
113 /// multiple of 1 << Unalign.
116 /// PostAlign - When non-zero, the block terminator contains a .align
117 /// directive, so the end of the block is aligned to 1 << PostAlign
121 BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0),
124 /// Compute the number of known offset bits internally to this block.
125 /// This number should be used to predict worst case padding when
126 /// splitting the block.
127 unsigned internalKnownBits() const {
128 unsigned Bits = Unalign ? Unalign : KnownBits;
129 // If the block size isn't a multiple of the known bits, assume the
130 // worst case padding.
131 if (Size & ((1u << Bits) - 1))
132 Bits = countTrailingZeros(Size);
136 /// Compute the offset immediately following this block. If LogAlign is
137 /// specified, return the offset the successor block will get if it has
139 unsigned postOffset(unsigned LogAlign = 0) const {
140 unsigned PO = Offset + Size;
141 unsigned LA = std::max(unsigned(PostAlign), LogAlign);
144 // Add alignment padding from the terminator.
145 return PO + UnknownPadding(LA, internalKnownBits());
148 /// Compute the number of known low bits of postOffset. If this block
149 /// contains inline asm, the number of known bits drops to the
150 /// instruction alignment. An aligned terminator may increase the number
152 /// If LogAlign is given, also consider the alignment of the next block.
153 unsigned postKnownBits(unsigned LogAlign = 0) const {
154 return std::max(std::max(unsigned(PostAlign), LogAlign),
155 internalKnownBits());
159 std::vector<BasicBlockInfo> BBInfo;
161 /// WaterList - A sorted list of basic blocks where islands could be placed
162 /// (i.e. blocks that don't fall through to the following block, due
163 /// to a return, unreachable, or unconditional branch).
164 std::vector<MachineBasicBlock*> WaterList;
166 /// NewWaterList - The subset of WaterList that was created since the
167 /// previous iteration by inserting unconditional branches.
168 SmallSet<MachineBasicBlock*, 4> NewWaterList;
170 typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
172 /// CPUser - One user of a constant pool, keeping the machine instruction
173 /// pointer, the constant pool being referenced, and the max displacement
174 /// allowed from the instruction to the CP. The HighWaterMark records the
175 /// highest basic block where a new CPEntry can be placed. To ensure this
176 /// pass terminates, the CP entries are initially placed at the end of the
177 /// function and then move monotonically to lower addresses. The
178 /// exception to this rule is when the current CP entry for a particular
179 /// CPUser is out of range, but there is another CP entry for the same
180 /// constant value in range. We want to use the existing in-range CP
181 /// entry, but if it later moves out of range, the search for new water
182 /// should resume where it left off. The HighWaterMark is used to record
187 MachineBasicBlock *HighWaterMark;
194 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
195 bool neg, bool soimm)
196 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm),
197 KnownAlignment(false) {
198 HighWaterMark = CPEMI->getParent();
200 /// getMaxDisp - Returns the maximum displacement supported by MI.
201 /// Correct for unknown alignment.
202 /// Conservatively subtract 2 bytes to handle weird alignment effects.
203 unsigned getMaxDisp() const {
204 return (KnownAlignment ? MaxDisp : MaxDisp - 2) - 2;
208 /// CPUsers - Keep track of all of the machine instructions that use various
209 /// constant pools and their max displacement.
210 std::vector<CPUser> CPUsers;
212 /// CPEntry - One per constant pool entry, keeping the machine instruction
213 /// pointer, the constpool index, and the number of CPUser's which
214 /// reference this entry.
219 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
220 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
223 /// CPEntries - Keep track of all of the constant pool entry machine
224 /// instructions. For each original constpool index (i.e. those that
225 /// existed upon entry to this pass), it keeps a vector of entries.
226 /// Original elements are cloned as we go along; the clones are
227 /// put in the vector of the original element, but have distinct CPIs.
228 std::vector<std::vector<CPEntry> > CPEntries;
230 /// ImmBranch - One per immediate branch, keeping the machine instruction
231 /// pointer, conditional or unconditional, the max displacement,
232 /// and (if isCond is true) the corresponding unconditional branch
236 unsigned MaxDisp : 31;
239 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
240 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
243 /// ImmBranches - Keep track of all the immediate branch instructions.
245 std::vector<ImmBranch> ImmBranches;
247 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
249 SmallVector<MachineInstr*, 4> PushPopMIs;
251 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
252 SmallVector<MachineInstr*, 4> T2JumpTables;
254 /// HasFarJump - True if any far jump instruction has been emitted during
255 /// the branch fix up pass.
259 MachineConstantPool *MCP;
260 const ARMBaseInstrInfo *TII;
261 const ARMSubtarget *STI;
262 ARMFunctionInfo *AFI;
268 ARMConstantIslands() : MachineFunctionPass(ID) {}
270 bool runOnMachineFunction(MachineFunction &MF) override;
272 const char *getPassName() const override {
273 return "ARM constant island placement and branch shortening pass";
277 void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
278 bool BBHasFallthrough(MachineBasicBlock *MBB);
279 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
280 unsigned getCPELogAlign(const MachineInstr *CPEMI);
281 void scanFunctionJumpTables();
282 void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
283 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI);
284 void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
285 void adjustBBOffsetsAfter(MachineBasicBlock *BB);
286 bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
287 int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
288 bool findAvailableWater(CPUser&U, unsigned UserOffset,
289 water_iterator &WaterIter);
290 void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
291 MachineBasicBlock *&NewMBB);
292 bool handleConstantPoolUser(unsigned CPUserIndex);
293 void removeDeadCPEMI(MachineInstr *CPEMI);
294 bool removeUnusedCPEntries();
295 bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
296 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
297 bool DoDump = false);
298 bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
299 CPUser &U, unsigned &Growth);
300 bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
301 bool fixupImmediateBr(ImmBranch &Br);
302 bool fixupConditionalBr(ImmBranch &Br);
303 bool fixupUnconditionalBr(ImmBranch &Br);
304 bool undoLRSpillRestore();
305 bool mayOptimizeThumb2Instruction(const MachineInstr *MI) const;
306 bool optimizeThumb2Instructions();
307 bool optimizeThumb2Branches();
308 bool reorderThumb2JumpTables();
309 bool optimizeThumb2JumpTables();
310 MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB,
311 MachineBasicBlock *JTBB);
313 void computeBlockSize(MachineBasicBlock *MBB);
314 unsigned getOffsetOf(MachineInstr *MI) const;
315 unsigned getUserOffset(CPUser&) const;
319 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
320 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
321 bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
323 return isOffsetInRange(UserOffset, TrialOffset,
324 U.getMaxDisp(), U.NegOk, U.IsSoImm);
327 char ARMConstantIslands::ID = 0;
330 /// verify - check BBOffsets, BBSizes, alignment of islands
331 void ARMConstantIslands::verify() {
333 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
335 MachineBasicBlock *MBB = MBBI;
336 unsigned MBBId = MBB->getNumber();
337 assert(!MBBId || BBInfo[MBBId - 1].postOffset() <= BBInfo[MBBId].Offset);
339 DEBUG(dbgs() << "Verifying " << CPUsers.size() << " CP users.\n");
340 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
341 CPUser &U = CPUsers[i];
342 unsigned UserOffset = getUserOffset(U);
343 // Verify offset using the real max displacement without the safety
345 if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp()+2, U.NegOk,
346 /* DoDump = */ true)) {
347 DEBUG(dbgs() << "OK\n");
350 DEBUG(dbgs() << "Out of range.\n");
353 llvm_unreachable("Constant pool entry out of range!");
358 /// print block size and offset information - debugging
359 void ARMConstantIslands::dumpBBs() {
361 for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
362 const BasicBlockInfo &BBI = BBInfo[J];
363 dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
364 << " kb=" << unsigned(BBI.KnownBits)
365 << " ua=" << unsigned(BBI.Unalign)
366 << " pa=" << unsigned(BBI.PostAlign)
367 << format(" size=%#x\n", BBInfo[J].Size);
372 /// createARMConstantIslandPass - returns an instance of the constpool
374 FunctionPass *llvm::createARMConstantIslandPass() {
375 return new ARMConstantIslands();
378 bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
380 MCP = mf.getConstantPool();
382 DEBUG(dbgs() << "***** ARMConstantIslands: "
383 << MCP->getConstants().size() << " CP entries, aligned to "
384 << MCP->getConstantPoolAlignment() << " bytes *****\n");
386 TII = (const ARMBaseInstrInfo *)MF->getTarget()
389 AFI = MF->getInfo<ARMFunctionInfo>();
390 STI = &MF->getTarget().getSubtarget<ARMSubtarget>();
392 isThumb = AFI->isThumbFunction();
393 isThumb1 = AFI->isThumb1OnlyFunction();
394 isThumb2 = AFI->isThumb2Function();
398 // This pass invalidates liveness information when it splits basic blocks.
399 MF->getRegInfo().invalidateLiveness();
401 // Renumber all of the machine basic blocks in the function, guaranteeing that
402 // the numbers agree with the position of the block in the function.
403 MF->RenumberBlocks();
405 // Try to reorder and otherwise adjust the block layout to make good use
406 // of the TB[BH] instructions.
407 bool MadeChange = false;
408 if (isThumb2 && AdjustJumpTableBlocks) {
409 scanFunctionJumpTables();
410 MadeChange |= reorderThumb2JumpTables();
411 // Data is out of date, so clear it. It'll be re-computed later.
412 T2JumpTables.clear();
413 // Blocks may have shifted around. Keep the numbering up to date.
414 MF->RenumberBlocks();
417 // Thumb1 functions containing constant pools get 4-byte alignment.
418 // This is so we can keep exact track of where the alignment padding goes.
420 // ARM and Thumb2 functions need to be 4-byte aligned.
422 MF->ensureAlignment(2); // 2 = log2(4)
424 // Perform the initial placement of the constant pool entries. To start with,
425 // we put them all at the end of the function.
426 std::vector<MachineInstr*> CPEMIs;
428 doInitialPlacement(CPEMIs);
430 /// The next UID to take is the first unused one.
431 AFI->initPICLabelUId(CPEMIs.size());
433 // Do the initial scan of the function, building up information about the
434 // sizes of each block, the location of all the water, and finding all of the
435 // constant pool users.
436 initializeFunctionInfo(CPEMIs);
441 /// Remove dead constant pool entries.
442 MadeChange |= removeUnusedCPEntries();
444 // Iteratively place constant pool entries and fix up branches until there
446 unsigned NoCPIters = 0, NoBRIters = 0;
448 DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
449 bool CPChange = false;
450 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
451 CPChange |= handleConstantPoolUser(i);
452 if (CPChange && ++NoCPIters > 30)
453 report_fatal_error("Constant Island pass failed to converge!");
456 // Clear NewWaterList now. If we split a block for branches, it should
457 // appear as "new water" for the next iteration of constant pool placement.
458 NewWaterList.clear();
460 DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
461 bool BRChange = false;
462 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
463 BRChange |= fixupImmediateBr(ImmBranches[i]);
464 if (BRChange && ++NoBRIters > 30)
465 report_fatal_error("Branch Fix Up pass failed to converge!");
468 if (!CPChange && !BRChange)
473 // Shrink 32-bit Thumb2 branch, load, and store instructions.
474 if (isThumb2 && !STI->prefers32BitThumb())
475 MadeChange |= optimizeThumb2Instructions();
477 // After a while, this might be made debug-only, but it is not expensive.
480 // If LR has been forced spilled and no far jump (i.e. BL) has been issued,
481 // undo the spill / restore of LR if possible.
482 if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
483 MadeChange |= undoLRSpillRestore();
485 // Save the mapping between original and cloned constpool entries.
486 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
487 for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) {
488 const CPEntry & CPE = CPEntries[i][j];
489 AFI->recordCPEClone(i, CPE.CPI);
493 DEBUG(dbgs() << '\n'; dumpBBs());
501 T2JumpTables.clear();
506 /// doInitialPlacement - Perform the initial placement of the constant pool
507 /// entries. To start with, we put them all at the end of the function.
509 ARMConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
510 // Create the basic block to hold the CPE's.
511 MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
514 // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
515 unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
517 // Mark the basic block as required by the const-pool.
518 // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
519 BB->setAlignment(AlignConstantIslands ? MaxAlign : 2);
521 // The function needs to be as aligned as the basic blocks. The linker may
522 // move functions around based on their alignment.
523 MF->ensureAlignment(BB->getAlignment());
525 // Order the entries in BB by descending alignment. That ensures correct
526 // alignment of all entries as long as BB is sufficiently aligned. Keep
527 // track of the insertion point for each alignment. We are going to bucket
528 // sort the entries as they are created.
529 SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end());
531 // Add all of the constants from the constant pool to the end block, use an
532 // identity mapping of CPI's to CPE's.
533 const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
535 const DataLayout &TD = *MF->getSubtarget().getDataLayout();
536 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
537 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
538 assert(Size >= 4 && "Too small constant pool entry");
539 unsigned Align = CPs[i].getAlignment();
540 assert(isPowerOf2_32(Align) && "Invalid alignment");
541 // Verify that all constant pool entries are a multiple of their alignment.
542 // If not, we would have to pad them out so that instructions stay aligned.
543 assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
545 // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
546 unsigned LogAlign = Log2_32(Align);
547 MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
548 MachineInstr *CPEMI =
549 BuildMI(*BB, InsAt, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
550 .addImm(i).addConstantPoolIndex(i).addImm(Size);
551 CPEMIs.push_back(CPEMI);
553 // Ensure that future entries with higher alignment get inserted before
554 // CPEMI. This is bucket sort with iterators.
555 for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a)
556 if (InsPoint[a] == InsAt)
559 // Add a new CPEntry, but no corresponding CPUser yet.
560 CPEntries.emplace_back(1, CPEntry(CPEMI, i));
562 DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
563 << Size << ", align = " << Align <<'\n');
568 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
569 /// into the block immediately after it.
570 bool ARMConstantIslands::BBHasFallthrough(MachineBasicBlock *MBB) {
571 // Get the next machine basic block in the function.
572 MachineFunction::iterator MBBI = MBB;
573 // Can't fall off end of function.
574 if (std::next(MBBI) == MBB->getParent()->end())
577 MachineBasicBlock *NextBB = std::next(MBBI);
578 if (std::find(MBB->succ_begin(), MBB->succ_end(), NextBB) == MBB->succ_end())
581 // Try to analyze the end of the block. A potential fallthrough may already
582 // have an unconditional branch for whatever reason.
583 MachineBasicBlock *TBB, *FBB;
584 SmallVector<MachineOperand, 4> Cond;
585 bool TooDifficult = TII->AnalyzeBranch(*MBB, TBB, FBB, Cond);
586 return TooDifficult || FBB == nullptr;
589 /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
590 /// look up the corresponding CPEntry.
591 ARMConstantIslands::CPEntry
592 *ARMConstantIslands::findConstPoolEntry(unsigned CPI,
593 const MachineInstr *CPEMI) {
594 std::vector<CPEntry> &CPEs = CPEntries[CPI];
595 // Number of entries per constpool index should be small, just do a
597 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
598 if (CPEs[i].CPEMI == CPEMI)
604 /// getCPELogAlign - Returns the required alignment of the constant pool entry
605 /// represented by CPEMI. Alignment is measured in log2(bytes) units.
606 unsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
607 assert(CPEMI && CPEMI->getOpcode() == ARM::CONSTPOOL_ENTRY);
609 // Everything is 4-byte aligned unless AlignConstantIslands is set.
610 if (!AlignConstantIslands)
613 unsigned CPI = CPEMI->getOperand(1).getIndex();
614 assert(CPI < MCP->getConstants().size() && "Invalid constant pool index.");
615 unsigned Align = MCP->getConstants()[CPI].getAlignment();
616 assert(isPowerOf2_32(Align) && "Invalid CPE alignment");
617 return Log2_32(Align);
620 /// scanFunctionJumpTables - Do a scan of the function, building up
621 /// information about the sizes of each block and the locations of all
623 void ARMConstantIslands::scanFunctionJumpTables() {
624 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
626 MachineBasicBlock &MBB = *MBBI;
628 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
630 if (I->isBranch() && I->getOpcode() == ARM::t2BR_JT)
631 T2JumpTables.push_back(I);
635 /// initializeFunctionInfo - Do the initial scan of the function, building up
636 /// information about the sizes of each block, the location of all the water,
637 /// and finding all of the constant pool users.
638 void ARMConstantIslands::
639 initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
641 BBInfo.resize(MF->getNumBlockIDs());
643 // First thing, compute the size of all basic blocks, and see if the function
644 // has any inline assembly in it. If so, we have to be conservative about
645 // alignment assumptions, as we don't know for sure the size of any
646 // instructions in the inline assembly.
647 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
650 // The known bits of the entry block offset are determined by the function
652 BBInfo.front().KnownBits = MF->getAlignment();
654 // Compute block offsets and known bits.
655 adjustBBOffsetsAfter(MF->begin());
657 // Now go back through the instructions and build up our data structures.
658 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
660 MachineBasicBlock &MBB = *MBBI;
662 // If this block doesn't fall through into the next MBB, then this is
663 // 'water' that a constant pool island could be placed.
664 if (!BBHasFallthrough(&MBB))
665 WaterList.push_back(&MBB);
667 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
669 if (I->isDebugValue())
672 int Opc = I->getOpcode();
680 continue; // Ignore other JT branches
682 T2JumpTables.push_back(I);
683 continue; // Does not get an entry in ImmBranches
714 // Record this immediate branch.
715 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
716 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
719 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
720 PushPopMIs.push_back(I);
722 if (Opc == ARM::CONSTPOOL_ENTRY)
725 // Scan the instructions for constant pool operands.
726 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
727 if (I->getOperand(op).isCPI()) {
728 // We found one. The addressing mode tells us the max displacement
729 // from the PC that this instruction permits.
731 // Basic size info comes from the TSFlags field.
735 bool IsSoImm = false;
739 llvm_unreachable("Unknown addressing mode for CP reference!");
741 // Taking the address of a CP entry.
743 // This takes a SoImm, which is 8 bit immediate rotated. We'll
744 // pretend the maximum offset is 255 * 4. Since each instruction
745 // 4 byte wide, this is always correct. We'll check for other
746 // displacements that fits in a SoImm as well.
752 case ARM::t2LEApcrel:
765 Bits = 12; // +-offset_12
771 Scale = 4; // +(offset_8*4)
777 Scale = 4; // +-(offset_8*4)
782 // Remember that this is a user of a CP entry.
783 unsigned CPI = I->getOperand(op).getIndex();
784 MachineInstr *CPEMI = CPEMIs[CPI];
785 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
786 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
788 // Increment corresponding CPEntry reference count.
789 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
790 assert(CPE && "Cannot find a corresponding CPEntry!");
793 // Instructions can only use one CP entry, don't bother scanning the
794 // rest of the operands.
801 /// computeBlockSize - Compute the size and some alignment information for MBB.
802 /// This function updates BBInfo directly.
803 void ARMConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
804 BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
809 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
811 BBI.Size += TII->GetInstSizeInBytes(I);
812 // For inline asm, GetInstSizeInBytes returns a conservative estimate.
813 // The actual size may be smaller, but still a multiple of the instr size.
814 if (I->isInlineAsm())
815 BBI.Unalign = isThumb ? 1 : 2;
816 // Also consider instructions that may be shrunk later.
817 else if (isThumb && mayOptimizeThumb2Instruction(I))
821 // tBR_JTr contains a .align 2 directive.
822 if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) {
824 MBB->getParent()->ensureAlignment(2);
828 /// getOffsetOf - Return the current offset of the specified machine instruction
829 /// from the start of the function. This offset changes as stuff is moved
830 /// around inside the function.
831 unsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const {
832 MachineBasicBlock *MBB = MI->getParent();
834 // The offset is composed of two things: the sum of the sizes of all MBB's
835 // before this instruction's block, and the offset from the start of the block
837 unsigned Offset = BBInfo[MBB->getNumber()].Offset;
839 // Sum instructions before MI in MBB.
840 for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) {
841 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
842 Offset += TII->GetInstSizeInBytes(I);
847 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
849 static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
850 const MachineBasicBlock *RHS) {
851 return LHS->getNumber() < RHS->getNumber();
854 /// updateForInsertedWaterBlock - When a block is newly inserted into the
855 /// machine function, it upsets all of the block numbers. Renumber the blocks
856 /// and update the arrays that parallel this numbering.
857 void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
858 // Renumber the MBB's to keep them consecutive.
859 NewBB->getParent()->RenumberBlocks(NewBB);
861 // Insert an entry into BBInfo to align it properly with the (newly
862 // renumbered) block numbers.
863 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
865 // Next, update WaterList. Specifically, we need to add NewMBB as having
866 // available water after it.
868 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
870 WaterList.insert(IP, NewBB);
874 /// Split the basic block containing MI into two blocks, which are joined by
875 /// an unconditional branch. Update data structures and renumber blocks to
876 /// account for this change and returns the newly created block.
877 MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
878 MachineBasicBlock *OrigBB = MI->getParent();
880 // Create a new MBB for the code after the OrigBB.
881 MachineBasicBlock *NewBB =
882 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
883 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
884 MF->insert(MBBI, NewBB);
886 // Splice the instructions starting with MI over to NewBB.
887 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
889 // Add an unconditional branch from OrigBB to NewBB.
890 // Note the new unconditional branch is not being recorded.
891 // There doesn't seem to be meaningful DebugInfo available; this doesn't
892 // correspond to anything in the source.
893 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
895 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB);
897 BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB)
898 .addImm(ARMCC::AL).addReg(0);
901 // Update the CFG. All succs of OrigBB are now succs of NewBB.
902 NewBB->transferSuccessors(OrigBB);
904 // OrigBB branches to NewBB.
905 OrigBB->addSuccessor(NewBB);
907 // Update internal data structures to account for the newly inserted MBB.
908 // This is almost the same as updateForInsertedWaterBlock, except that
909 // the Water goes after OrigBB, not NewBB.
910 MF->RenumberBlocks(NewBB);
912 // Insert an entry into BBInfo to align it properly with the (newly
913 // renumbered) block numbers.
914 BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
916 // Next, update WaterList. Specifically, we need to add OrigMBB as having
917 // available water after it (but not if it's already there, which happens
918 // when splitting before a conditional branch that is followed by an
919 // unconditional branch - in that case we want to insert NewBB).
921 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
923 MachineBasicBlock* WaterBB = *IP;
924 if (WaterBB == OrigBB)
925 WaterList.insert(std::next(IP), NewBB);
927 WaterList.insert(IP, OrigBB);
928 NewWaterList.insert(OrigBB);
930 // Figure out how large the OrigBB is. As the first half of the original
931 // block, it cannot contain a tablejump. The size includes
932 // the new jump we added. (It should be possible to do this without
933 // recounting everything, but it's very confusing, and this is rarely
935 computeBlockSize(OrigBB);
937 // Figure out how large the NewMBB is. As the second half of the original
938 // block, it may contain a tablejump.
939 computeBlockSize(NewBB);
941 // All BBOffsets following these blocks must be modified.
942 adjustBBOffsetsAfter(OrigBB);
947 /// getUserOffset - Compute the offset of U.MI as seen by the hardware
948 /// displacement computation. Update U.KnownAlignment to match its current
949 /// basic block location.
950 unsigned ARMConstantIslands::getUserOffset(CPUser &U) const {
951 unsigned UserOffset = getOffsetOf(U.MI);
952 const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()];
953 unsigned KnownBits = BBI.internalKnownBits();
955 // The value read from PC is offset from the actual instruction address.
956 UserOffset += (isThumb ? 4 : 8);
958 // Because of inline assembly, we may not know the alignment (mod 4) of U.MI.
959 // Make sure U.getMaxDisp() returns a constrained range.
960 U.KnownAlignment = (KnownBits >= 2);
962 // On Thumb, offsets==2 mod 4 are rounded down by the hardware for
963 // purposes of the displacement computation; compensate for that here.
964 // For unknown alignments, getMaxDisp() constrains the range instead.
965 if (isThumb && U.KnownAlignment)
971 /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
972 /// reference) is within MaxDisp of TrialOffset (a proposed location of a
973 /// constant pool entry).
974 /// UserOffset is computed by getUserOffset above to include PC adjustments. If
975 /// the mod 4 alignment of UserOffset is not known, the uncertainty must be
976 /// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
977 bool ARMConstantIslands::isOffsetInRange(unsigned UserOffset,
978 unsigned TrialOffset, unsigned MaxDisp,
979 bool NegativeOK, bool IsSoImm) {
980 if (UserOffset <= TrialOffset) {
981 // User before the Trial.
982 if (TrialOffset - UserOffset <= MaxDisp)
984 // FIXME: Make use full range of soimm values.
985 } else if (NegativeOK) {
986 if (UserOffset - TrialOffset <= MaxDisp)
988 // FIXME: Make use full range of soimm values.
993 /// isWaterInRange - Returns true if a CPE placed after the specified
994 /// Water (a basic block) will be in range for the specific MI.
996 /// Compute how much the function will grow by inserting a CPE after Water.
997 bool ARMConstantIslands::isWaterInRange(unsigned UserOffset,
998 MachineBasicBlock* Water, CPUser &U,
1000 unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
1001 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
1002 unsigned NextBlockOffset, NextBlockAlignment;
1003 MachineFunction::const_iterator NextBlock = Water;
1004 if (++NextBlock == MF->end()) {
1005 NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
1006 NextBlockAlignment = 0;
1008 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
1009 NextBlockAlignment = NextBlock->getAlignment();
1011 unsigned Size = U.CPEMI->getOperand(2).getImm();
1012 unsigned CPEEnd = CPEOffset + Size;
1014 // The CPE may be able to hide in the alignment padding before the next
1015 // block. It may also cause more padding to be required if it is more aligned
1016 // that the next block.
1017 if (CPEEnd > NextBlockOffset) {
1018 Growth = CPEEnd - NextBlockOffset;
1019 // Compute the padding that would go at the end of the CPE to align the next
1021 Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment);
1023 // If the CPE is to be inserted before the instruction, that will raise
1024 // the offset of the instruction. Also account for unknown alignment padding
1025 // in blocks between CPE and the user.
1026 if (CPEOffset < UserOffset)
1027 UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign);
1029 // CPE fits in existing padding.
1032 return isOffsetInRange(UserOffset, CPEOffset, U);
1035 /// isCPEntryInRange - Returns true if the distance between specific MI and
1036 /// specific ConstPool entry instruction can fit in MI's displacement field.
1037 bool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
1038 MachineInstr *CPEMI, unsigned MaxDisp,
1039 bool NegOk, bool DoDump) {
1040 unsigned CPEOffset = getOffsetOf(CPEMI);
1044 unsigned Block = MI->getParent()->getNumber();
1045 const BasicBlockInfo &BBI = BBInfo[Block];
1046 dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
1047 << " max delta=" << MaxDisp
1048 << format(" insn address=%#x", UserOffset)
1049 << " in BB#" << Block << ": "
1050 << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
1051 << format("CPE address=%#x offset=%+d: ", CPEOffset,
1052 int(CPEOffset-UserOffset));
1056 return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
1060 /// BBIsJumpedOver - Return true of the specified basic block's only predecessor
1061 /// unconditionally branches to its only successor.
1062 static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
1063 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
1066 MachineBasicBlock *Succ = *MBB->succ_begin();
1067 MachineBasicBlock *Pred = *MBB->pred_begin();
1068 MachineInstr *PredMI = &Pred->back();
1069 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
1070 || PredMI->getOpcode() == ARM::t2B)
1071 return PredMI->getOperand(0).getMBB() == Succ;
1076 void ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
1077 unsigned BBNum = BB->getNumber();
1078 for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
1079 // Get the offset and known bits at the end of the layout predecessor.
1080 // Include the alignment of the current block.
1081 unsigned LogAlign = MF->getBlockNumbered(i)->getAlignment();
1082 unsigned Offset = BBInfo[i - 1].postOffset(LogAlign);
1083 unsigned KnownBits = BBInfo[i - 1].postKnownBits(LogAlign);
1085 // This is where block i begins. Stop if the offset is already correct,
1086 // and we have updated 2 blocks. This is the maximum number of blocks
1087 // changed before calling this function.
1088 if (i > BBNum + 2 &&
1089 BBInfo[i].Offset == Offset &&
1090 BBInfo[i].KnownBits == KnownBits)
1093 BBInfo[i].Offset = Offset;
1094 BBInfo[i].KnownBits = KnownBits;
1098 /// decrementCPEReferenceCount - find the constant pool entry with index CPI
1099 /// and instruction CPEMI, and decrement its refcount. If the refcount
1100 /// becomes 0 remove the entry and instruction. Returns true if we removed
1101 /// the entry, false if we didn't.
1103 bool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI,
1104 MachineInstr *CPEMI) {
1105 // Find the old entry. Eliminate it if it is no longer used.
1106 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1107 assert(CPE && "Unexpected!");
1108 if (--CPE->RefCount == 0) {
1109 removeDeadCPEMI(CPEMI);
1110 CPE->CPEMI = nullptr;
1117 /// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1118 /// if not, see if an in-range clone of the CPE is in range, and if so,
1119 /// change the data structures so the user references the clone. Returns:
1120 /// 0 = no existing entry found
1121 /// 1 = entry found, and there were no code insertions or deletions
1122 /// 2 = entry found, and there were code insertions or deletions
1123 int ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
1125 MachineInstr *UserMI = U.MI;
1126 MachineInstr *CPEMI = U.CPEMI;
1128 // Check to see if the CPE is already in-range.
1129 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
1131 DEBUG(dbgs() << "In range\n");
1135 // No. Look for previously created clones of the CPE that are in range.
1136 unsigned CPI = CPEMI->getOperand(1).getIndex();
1137 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1138 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1139 // We already tried this one
1140 if (CPEs[i].CPEMI == CPEMI)
1142 // Removing CPEs can leave empty entries, skip
1143 if (CPEs[i].CPEMI == nullptr)
1145 if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
1147 DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1148 << CPEs[i].CPI << "\n");
1149 // Point the CPUser node to the replacement
1150 U.CPEMI = CPEs[i].CPEMI;
1151 // Change the CPI in the instruction operand to refer to the clone.
1152 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1153 if (UserMI->getOperand(j).isCPI()) {
1154 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1157 // Adjust the refcount of the clone...
1159 // ...and the original. If we didn't remove the old entry, none of the
1160 // addresses changed, so we don't need another pass.
1161 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1167 /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1168 /// the specific unconditional branch instruction.
1169 static inline unsigned getUnconditionalBrDisp(int Opc) {
1172 return ((1<<10)-1)*2;
1174 return ((1<<23)-1)*2;
1179 return ((1<<23)-1)*4;
1182 /// findAvailableWater - Look for an existing entry in the WaterList in which
1183 /// we can place the CPE referenced from U so it's within range of U's MI.
1184 /// Returns true if found, false if not. If it returns true, WaterIter
1185 /// is set to the WaterList entry. For Thumb, prefer water that will not
1186 /// introduce padding to water that will. To ensure that this pass
1187 /// terminates, the CPE location for a particular CPUser is only allowed to
1188 /// move to a lower address, so search backward from the end of the list and
1189 /// prefer the first water that is in range.
1190 bool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
1191 water_iterator &WaterIter) {
1192 if (WaterList.empty())
1195 unsigned BestGrowth = ~0u;
1196 for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();;
1198 MachineBasicBlock* WaterBB = *IP;
1199 // Check if water is in range and is either at a lower address than the
1200 // current "high water mark" or a new water block that was created since
1201 // the previous iteration by inserting an unconditional branch. In the
1202 // latter case, we want to allow resetting the high water mark back to
1203 // this new water since we haven't seen it before. Inserting branches
1204 // should be relatively uncommon and when it does happen, we want to be
1205 // sure to take advantage of it for all the CPEs near that block, so that
1206 // we don't insert more branches than necessary.
1208 if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1209 (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1210 NewWaterList.count(WaterBB) || WaterBB == U.MI->getParent()) &&
1211 Growth < BestGrowth) {
1212 // This is the least amount of required padding seen so far.
1213 BestGrowth = Growth;
1215 DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber()
1216 << " Growth=" << Growth << '\n');
1218 // Keep looking unless it is perfect.
1219 if (BestGrowth == 0)
1225 return BestGrowth != ~0u;
1228 /// createNewWater - No existing WaterList entry will work for
1229 /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
1230 /// block is used if in range, and the conditional branch munged so control
1231 /// flow is correct. Otherwise the block is split to create a hole with an
1232 /// unconditional branch around it. In either case NewMBB is set to a
1233 /// block following which the new island can be inserted (the WaterList
1234 /// is not adjusted).
1235 void ARMConstantIslands::createNewWater(unsigned CPUserIndex,
1236 unsigned UserOffset,
1237 MachineBasicBlock *&NewMBB) {
1238 CPUser &U = CPUsers[CPUserIndex];
1239 MachineInstr *UserMI = U.MI;
1240 MachineInstr *CPEMI = U.CPEMI;
1241 unsigned CPELogAlign = getCPELogAlign(CPEMI);
1242 MachineBasicBlock *UserMBB = UserMI->getParent();
1243 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
1245 // If the block does not end in an unconditional branch already, and if the
1246 // end of the block is within range, make new water there. (The addition
1247 // below is for the unconditional branch we will be adding: 4 bytes on ARM +
1248 // Thumb2, 2 on Thumb1.
1249 if (BBHasFallthrough(UserMBB)) {
1250 // Size of branch to insert.
1251 unsigned Delta = isThumb1 ? 2 : 4;
1252 // Compute the offset where the CPE will begin.
1253 unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
1255 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1256 DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
1257 << format(", expected CPE offset %#x\n", CPEOffset));
1258 NewMBB = std::next(MachineFunction::iterator(UserMBB));
1259 // Add an unconditional branch from UserMBB to fallthrough block. Record
1260 // it for branch lengthening; this new branch will not get out of range,
1261 // but if the preceding conditional branch is out of range, the targets
1262 // will be exchanged, and the altered branch may be out of range, so the
1263 // machinery has to know about it.
1264 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
1266 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1268 BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB)
1269 .addImm(ARMCC::AL).addReg(0);
1270 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
1271 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
1272 MaxDisp, false, UncondBr));
1273 BBInfo[UserMBB->getNumber()].Size += Delta;
1274 adjustBBOffsetsAfter(UserMBB);
1279 // What a big block. Find a place within the block to split it. This is a
1280 // little tricky on Thumb1 since instructions are 2 bytes and constant pool
1281 // entries are 4 bytes: if instruction I references island CPE, and
1282 // instruction I+1 references CPE', it will not work well to put CPE as far
1283 // forward as possible, since then CPE' cannot immediately follow it (that
1284 // location is 2 bytes farther away from I+1 than CPE was from I) and we'd
1285 // need to create a new island. So, we make a first guess, then walk through
1286 // the instructions between the one currently being looked at and the
1287 // possible insertion point, and make sure any other instructions that
1288 // reference CPEs will be able to use the same island area; if not, we back
1289 // up the insertion point.
1291 // Try to split the block so it's fully aligned. Compute the latest split
1292 // point where we can add a 4-byte branch instruction, and then align to
1293 // LogAlign which is the largest possible alignment in the function.
1294 unsigned LogAlign = MF->getAlignment();
1295 assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
1296 unsigned KnownBits = UserBBI.internalKnownBits();
1297 unsigned UPad = UnknownPadding(LogAlign, KnownBits);
1298 unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad;
1299 DEBUG(dbgs() << format("Split in middle of big block before %#x",
1302 // The 4 in the following is for the unconditional branch we'll be inserting
1303 // (allows for long branch on Thumb1). Alignment of the island is handled
1304 // inside isOffsetInRange.
1305 BaseInsertOffset -= 4;
1307 DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
1308 << " la=" << LogAlign
1309 << " kb=" << KnownBits
1310 << " up=" << UPad << '\n');
1312 // This could point off the end of the block if we've already got constant
1313 // pool entries following this block; only the last one is in the water list.
1314 // Back past any possible branches (allow for a conditional and a maximally
1315 // long unconditional).
1316 if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
1317 // Ensure BaseInsertOffset is larger than the offset of the instruction
1318 // following UserMI so that the loop which searches for the split point
1319 // iterates at least once.
1321 std::max(UserBBI.postOffset() - UPad - 8,
1322 UserOffset + TII->GetInstSizeInBytes(UserMI) + 1);
1323 DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
1325 unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad +
1326 CPEMI->getOperand(2).getImm();
1327 MachineBasicBlock::iterator MI = UserMI;
1329 unsigned CPUIndex = CPUserIndex+1;
1330 unsigned NumCPUsers = CPUsers.size();
1331 MachineInstr *LastIT = nullptr;
1332 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
1333 Offset < BaseInsertOffset;
1334 Offset += TII->GetInstSizeInBytes(MI), MI = std::next(MI)) {
1335 assert(MI != UserMBB->end() && "Fell off end of block");
1336 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
1337 CPUser &U = CPUsers[CPUIndex];
1338 if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
1339 // Shift intertion point by one unit of alignment so it is within reach.
1340 BaseInsertOffset -= 1u << LogAlign;
1341 EndInsertOffset -= 1u << LogAlign;
1343 // This is overly conservative, as we don't account for CPEMIs being
1344 // reused within the block, but it doesn't matter much. Also assume CPEs
1345 // are added in order with alignment padding. We may eventually be able
1346 // to pack the aligned CPEs better.
1347 EndInsertOffset += U.CPEMI->getOperand(2).getImm();
1351 // Remember the last IT instruction.
1352 if (MI->getOpcode() == ARM::t2IT)
1358 // Avoid splitting an IT block.
1360 unsigned PredReg = 0;
1361 ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg);
1362 if (CC != ARMCC::AL)
1366 // We really must not split an IT block.
1367 DEBUG(unsigned PredReg;
1368 assert(!isThumb || getITInstrPredicate(MI, PredReg) == ARMCC::AL));
1370 NewMBB = splitBlockBeforeInstr(MI);
1373 /// handleConstantPoolUser - Analyze the specified user, checking to see if it
1374 /// is out-of-range. If so, pick up the constant pool value and move it some
1375 /// place in-range. Return true if we changed any addresses (thus must run
1376 /// another pass of branch lengthening), false otherwise.
1377 bool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
1378 CPUser &U = CPUsers[CPUserIndex];
1379 MachineInstr *UserMI = U.MI;
1380 MachineInstr *CPEMI = U.CPEMI;
1381 unsigned CPI = CPEMI->getOperand(1).getIndex();
1382 unsigned Size = CPEMI->getOperand(2).getImm();
1383 // Compute this only once, it's expensive.
1384 unsigned UserOffset = getUserOffset(U);
1386 // See if the current entry is within range, or there is a clone of it
1388 int result = findInRangeCPEntry(U, UserOffset);
1389 if (result==1) return false;
1390 else if (result==2) return true;
1392 // No existing clone of this CPE is within range.
1393 // We will be generating a new clone. Get a UID for it.
1394 unsigned ID = AFI->createPICLabelUId();
1396 // Look for water where we can place this CPE.
1397 MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1398 MachineBasicBlock *NewMBB;
1400 if (findAvailableWater(U, UserOffset, IP)) {
1401 DEBUG(dbgs() << "Found water in range\n");
1402 MachineBasicBlock *WaterBB = *IP;
1404 // If the original WaterList entry was "new water" on this iteration,
1405 // propagate that to the new island. This is just keeping NewWaterList
1406 // updated to match the WaterList, which will be updated below.
1407 if (NewWaterList.erase(WaterBB))
1408 NewWaterList.insert(NewIsland);
1410 // The new CPE goes before the following block (NewMBB).
1411 NewMBB = std::next(MachineFunction::iterator(WaterBB));
1415 DEBUG(dbgs() << "No water found\n");
1416 createNewWater(CPUserIndex, UserOffset, NewMBB);
1418 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1419 // called while handling branches so that the water will be seen on the
1420 // next iteration for constant pools, but in this context, we don't want
1421 // it. Check for this so it will be removed from the WaterList.
1422 // Also remove any entry from NewWaterList.
1423 MachineBasicBlock *WaterBB = std::prev(MachineFunction::iterator(NewMBB));
1424 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1425 if (IP != WaterList.end())
1426 NewWaterList.erase(WaterBB);
1428 // We are adding new water. Update NewWaterList.
1429 NewWaterList.insert(NewIsland);
1432 // Remove the original WaterList entry; we want subsequent insertions in
1433 // this vicinity to go after the one we're about to insert. This
1434 // considerably reduces the number of times we have to move the same CPE
1435 // more than once and is also important to ensure the algorithm terminates.
1436 if (IP != WaterList.end())
1437 WaterList.erase(IP);
1439 // Okay, we know we can put an island before NewMBB now, do it!
1440 MF->insert(NewMBB, NewIsland);
1442 // Update internal data structures to account for the newly inserted MBB.
1443 updateForInsertedWaterBlock(NewIsland);
1445 // Decrement the old entry, and remove it if refcount becomes 0.
1446 decrementCPEReferenceCount(CPI, CPEMI);
1448 // Now that we have an island to add the CPE to, clone the original CPE and
1449 // add it to the island.
1450 U.HighWaterMark = NewIsland;
1451 U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
1452 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
1453 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
1456 // Mark the basic block as aligned as required by the const-pool entry.
1457 NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
1459 // Increase the size of the island block to account for the new entry.
1460 BBInfo[NewIsland->getNumber()].Size += Size;
1461 adjustBBOffsetsAfter(std::prev(MachineFunction::iterator(NewIsland)));
1463 // Finally, change the CPI in the instruction operand to be ID.
1464 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1465 if (UserMI->getOperand(i).isCPI()) {
1466 UserMI->getOperand(i).setIndex(ID);
1470 DEBUG(dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI
1471 << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
1476 /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1477 /// sizes and offsets of impacted basic blocks.
1478 void ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
1479 MachineBasicBlock *CPEBB = CPEMI->getParent();
1480 unsigned Size = CPEMI->getOperand(2).getImm();
1481 CPEMI->eraseFromParent();
1482 BBInfo[CPEBB->getNumber()].Size -= Size;
1483 // All succeeding offsets have the current size value added in, fix this.
1484 if (CPEBB->empty()) {
1485 BBInfo[CPEBB->getNumber()].Size = 0;
1487 // This block no longer needs to be aligned.
1488 CPEBB->setAlignment(0);
1490 // Entries are sorted by descending alignment, so realign from the front.
1491 CPEBB->setAlignment(getCPELogAlign(CPEBB->begin()));
1493 adjustBBOffsetsAfter(CPEBB);
1494 // An island has only one predecessor BB and one successor BB. Check if
1495 // this BB's predecessor jumps directly to this BB's successor. This
1496 // shouldn't happen currently.
1497 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1498 // FIXME: remove the empty blocks after all the work is done?
1501 /// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1503 bool ARMConstantIslands::removeUnusedCPEntries() {
1504 unsigned MadeChange = false;
1505 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1506 std::vector<CPEntry> &CPEs = CPEntries[i];
1507 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1508 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1509 removeDeadCPEMI(CPEs[j].CPEMI);
1510 CPEs[j].CPEMI = nullptr;
1518 /// isBBInRange - Returns true if the distance between specific MI and
1519 /// specific BB can fit in MI's displacement field.
1520 bool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1522 unsigned PCAdj = isThumb ? 4 : 8;
1523 unsigned BrOffset = getOffsetOf(MI) + PCAdj;
1524 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
1526 DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
1527 << " from BB#" << MI->getParent()->getNumber()
1528 << " max delta=" << MaxDisp
1529 << " from " << getOffsetOf(MI) << " to " << DestOffset
1530 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
1532 if (BrOffset <= DestOffset) {
1533 // Branch before the Dest.
1534 if (DestOffset-BrOffset <= MaxDisp)
1537 if (BrOffset-DestOffset <= MaxDisp)
1543 /// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1544 /// away to fit in its displacement field.
1545 bool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) {
1546 MachineInstr *MI = Br.MI;
1547 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1549 // Check to see if the DestBB is already in-range.
1550 if (isBBInRange(MI, DestBB, Br.MaxDisp))
1554 return fixupUnconditionalBr(Br);
1555 return fixupConditionalBr(Br);
1558 /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1559 /// too far away to fit in its displacement field. If the LR register has been
1560 /// spilled in the epilogue, then we can use BL to implement a far jump.
1561 /// Otherwise, add an intermediate branch instruction to a branch.
1563 ARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
1564 MachineInstr *MI = Br.MI;
1565 MachineBasicBlock *MBB = MI->getParent();
1567 llvm_unreachable("fixupUnconditionalBr is Thumb1 only!");
1569 // Use BL to implement far jump.
1570 Br.MaxDisp = (1 << 21) * 2;
1571 MI->setDesc(TII->get(ARM::tBfar));
1572 BBInfo[MBB->getNumber()].Size += 2;
1573 adjustBBOffsetsAfter(MBB);
1577 DEBUG(dbgs() << " Changed B to long jump " << *MI);
1582 /// fixupConditionalBr - Fix up a conditional branch whose destination is too
1583 /// far away to fit in its displacement field. It is converted to an inverse
1584 /// conditional branch + an unconditional branch to the destination.
1586 ARMConstantIslands::fixupConditionalBr(ImmBranch &Br) {
1587 MachineInstr *MI = Br.MI;
1588 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1590 // Add an unconditional branch to the destination and invert the branch
1591 // condition to jump over it:
1597 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
1598 CC = ARMCC::getOppositeCondition(CC);
1599 unsigned CCReg = MI->getOperand(2).getReg();
1601 // If the branch is at the end of its MBB and that has a fall-through block,
1602 // direct the updated conditional branch to the fall-through block. Otherwise,
1603 // split the MBB before the next instruction.
1604 MachineBasicBlock *MBB = MI->getParent();
1605 MachineInstr *BMI = &MBB->back();
1606 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
1610 if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) &&
1611 BMI->getOpcode() == Br.UncondBr) {
1612 // Last MI in the BB is an unconditional branch. Can we simply invert the
1613 // condition and swap destinations:
1619 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
1620 if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
1621 DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
1623 BMI->getOperand(0).setMBB(DestBB);
1624 MI->getOperand(0).setMBB(NewDest);
1625 MI->getOperand(1).setImm(CC);
1632 splitBlockBeforeInstr(MI);
1633 // No need for the branch to the next block. We're adding an unconditional
1634 // branch to the destination.
1635 int delta = TII->GetInstSizeInBytes(&MBB->back());
1636 BBInfo[MBB->getNumber()].Size -= delta;
1637 MBB->back().eraseFromParent();
1638 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1640 MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(MBB));
1642 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
1643 << " also invert condition and change dest. to BB#"
1644 << NextBB->getNumber() << "\n");
1646 // Insert a new conditional branch and a new unconditional branch.
1647 // Also update the ImmBranch as well as adding a new entry for the new branch.
1648 BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
1649 .addMBB(NextBB).addImm(CC).addReg(CCReg);
1650 Br.MI = &MBB->back();
1651 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1653 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB)
1654 .addImm(ARMCC::AL).addReg(0);
1656 BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
1657 BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1658 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
1659 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
1661 // Remove the old conditional branch. It may or may not still be in MBB.
1662 BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
1663 MI->eraseFromParent();
1664 adjustBBOffsetsAfter(MBB);
1668 /// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills
1669 /// LR / restores LR to pc. FIXME: This is done here because it's only possible
1670 /// to do this if tBfar is not used.
1671 bool ARMConstantIslands::undoLRSpillRestore() {
1672 bool MadeChange = false;
1673 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1674 MachineInstr *MI = PushPopMIs[i];
1675 // First two operands are predicates.
1676 if (MI->getOpcode() == ARM::tPOP_RET &&
1677 MI->getOperand(2).getReg() == ARM::PC &&
1678 MI->getNumExplicitOperands() == 3) {
1679 // Create the new insn and copy the predicate from the old.
1680 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET))
1681 .addOperand(MI->getOperand(0))
1682 .addOperand(MI->getOperand(1));
1683 MI->eraseFromParent();
1690 // mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
1691 // below may shrink MI.
1693 ARMConstantIslands::mayOptimizeThumb2Instruction(const MachineInstr *MI) const {
1694 switch(MI->getOpcode()) {
1695 // optimizeThumb2Instructions.
1696 case ARM::t2LEApcrel:
1698 // optimizeThumb2Branches.
1702 // optimizeThumb2JumpTables.
1709 bool ARMConstantIslands::optimizeThumb2Instructions() {
1710 bool MadeChange = false;
1712 // Shrink ADR and LDR from constantpool.
1713 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
1714 CPUser &U = CPUsers[i];
1715 unsigned Opcode = U.MI->getOpcode();
1716 unsigned NewOpc = 0;
1721 case ARM::t2LEApcrel:
1722 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1723 NewOpc = ARM::tLEApcrel;
1729 if (isARMLowRegister(U.MI->getOperand(0).getReg())) {
1730 NewOpc = ARM::tLDRpci;
1740 unsigned UserOffset = getUserOffset(U);
1741 unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
1743 // Be conservative with inline asm.
1744 if (!U.KnownAlignment)
1747 // FIXME: Check if offset is multiple of scale if scale is not 4.
1748 if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
1749 DEBUG(dbgs() << "Shrink: " << *U.MI);
1750 U.MI->setDesc(TII->get(NewOpc));
1751 MachineBasicBlock *MBB = U.MI->getParent();
1752 BBInfo[MBB->getNumber()].Size -= 2;
1753 adjustBBOffsetsAfter(MBB);
1759 MadeChange |= optimizeThumb2Branches();
1760 MadeChange |= optimizeThumb2JumpTables();
1764 bool ARMConstantIslands::optimizeThumb2Branches() {
1765 bool MadeChange = false;
1767 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
1768 ImmBranch &Br = ImmBranches[i];
1769 unsigned Opcode = Br.MI->getOpcode();
1770 unsigned NewOpc = 0;
1788 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
1789 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
1790 if (isBBInRange(Br.MI, DestBB, MaxOffs)) {
1791 DEBUG(dbgs() << "Shrink branch: " << *Br.MI);
1792 Br.MI->setDesc(TII->get(NewOpc));
1793 MachineBasicBlock *MBB = Br.MI->getParent();
1794 BBInfo[MBB->getNumber()].Size -= 2;
1795 adjustBBOffsetsAfter(MBB);
1801 Opcode = Br.MI->getOpcode();
1802 if (Opcode != ARM::tBcc)
1805 // If the conditional branch doesn't kill CPSR, then CPSR can be liveout
1806 // so this transformation is not safe.
1807 if (!Br.MI->killsRegister(ARM::CPSR))
1811 unsigned PredReg = 0;
1812 ARMCC::CondCodes Pred = getInstrPredicate(Br.MI, PredReg);
1813 if (Pred == ARMCC::EQ)
1815 else if (Pred == ARMCC::NE)
1816 NewOpc = ARM::tCBNZ;
1819 MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
1820 // Check if the distance is within 126. Subtract starting offset by 2
1821 // because the cmp will be eliminated.
1822 unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2;
1823 unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
1824 if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
1825 MachineBasicBlock::iterator CmpMI = Br.MI;
1826 if (CmpMI != Br.MI->getParent()->begin()) {
1828 if (CmpMI->getOpcode() == ARM::tCMPi8) {
1829 unsigned Reg = CmpMI->getOperand(0).getReg();
1830 Pred = getInstrPredicate(CmpMI, PredReg);
1831 if (Pred == ARMCC::AL &&
1832 CmpMI->getOperand(1).getImm() == 0 &&
1833 isARMLowRegister(Reg)) {
1834 MachineBasicBlock *MBB = Br.MI->getParent();
1835 DEBUG(dbgs() << "Fold: " << *CmpMI << " and: " << *Br.MI);
1836 MachineInstr *NewBR =
1837 BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc))
1838 .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags());
1839 CmpMI->eraseFromParent();
1840 Br.MI->eraseFromParent();
1842 BBInfo[MBB->getNumber()].Size -= 2;
1843 adjustBBOffsetsAfter(MBB);
1855 /// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
1856 /// jumptables when it's possible.
1857 bool ARMConstantIslands::optimizeThumb2JumpTables() {
1858 bool MadeChange = false;
1860 // FIXME: After the tables are shrunk, can we get rid some of the
1861 // constantpool tables?
1862 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1863 if (!MJTI) return false;
1865 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1866 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1867 MachineInstr *MI = T2JumpTables[i];
1868 const MCInstrDesc &MCID = MI->getDesc();
1869 unsigned NumOps = MCID.getNumOperands();
1870 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
1871 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1872 unsigned JTI = JTOP.getIndex();
1873 assert(JTI < JT.size());
1876 bool HalfWordOk = true;
1877 unsigned JTOffset = getOffsetOf(MI) + 4;
1878 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1879 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1880 MachineBasicBlock *MBB = JTBBs[j];
1881 unsigned DstOffset = BBInfo[MBB->getNumber()].Offset;
1882 // Negative offset is not ok. FIXME: We should change BB layout to make
1883 // sure all the branches are forward.
1884 if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
1886 unsigned TBHLimit = ((1<<16)-1)*2;
1887 if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
1889 if (!ByteOk && !HalfWordOk)
1893 if (ByteOk || HalfWordOk) {
1894 MachineBasicBlock *MBB = MI->getParent();
1895 unsigned BaseReg = MI->getOperand(0).getReg();
1896 bool BaseRegKill = MI->getOperand(0).isKill();
1899 unsigned IdxReg = MI->getOperand(1).getReg();
1900 bool IdxRegKill = MI->getOperand(1).isKill();
1902 // Scan backwards to find the instruction that defines the base
1903 // register. Due to post-RA scheduling, we can't count on it
1904 // immediately preceding the branch instruction.
1905 MachineBasicBlock::iterator PrevI = MI;
1906 MachineBasicBlock::iterator B = MBB->begin();
1907 while (PrevI != B && !PrevI->definesRegister(BaseReg))
1910 // If for some reason we didn't find it, we can't do anything, so
1911 // just skip this one.
1912 if (!PrevI->definesRegister(BaseReg))
1915 MachineInstr *AddrMI = PrevI;
1917 // Examine the instruction that calculates the jumptable entry address.
1918 // Make sure it only defines the base register and kills any uses
1919 // other than the index register.
1920 for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
1921 const MachineOperand &MO = AddrMI->getOperand(k);
1922 if (!MO.isReg() || !MO.getReg())
1924 if (MO.isDef() && MO.getReg() != BaseReg) {
1928 if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
1936 // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction
1937 // that gave us the initial base register definition.
1938 for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI)
1941 // The instruction should be a tLEApcrel or t2LEApcrelJT; we want
1942 // to delete it as well.
1943 MachineInstr *LeaMI = PrevI;
1944 if ((LeaMI->getOpcode() != ARM::tLEApcrelJT &&
1945 LeaMI->getOpcode() != ARM::t2LEApcrelJT) ||
1946 LeaMI->getOperand(0).getReg() != BaseReg)
1952 DEBUG(dbgs() << "Shrink JT: " << *MI << " addr: " << *AddrMI
1953 << " lea: " << *LeaMI);
1954 unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
1955 MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
1956 .addReg(IdxReg, getKillRegState(IdxRegKill))
1957 .addJumpTableIndex(JTI, JTOP.getTargetFlags())
1958 .addImm(MI->getOperand(JTOpIdx+1).getImm());
1959 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": " << *NewJTMI);
1960 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1961 // is 2-byte aligned. For now, asm printer will fix it up.
1962 unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
1963 unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
1964 OrigSize += TII->GetInstSizeInBytes(LeaMI);
1965 OrigSize += TII->GetInstSizeInBytes(MI);
1967 AddrMI->eraseFromParent();
1968 LeaMI->eraseFromParent();
1969 MI->eraseFromParent();
1971 int delta = OrigSize - NewSize;
1972 BBInfo[MBB->getNumber()].Size -= delta;
1973 adjustBBOffsetsAfter(MBB);
1983 /// reorderThumb2JumpTables - Adjust the function's block layout to ensure that
1984 /// jump tables always branch forwards, since that's what tbb and tbh need.
1985 bool ARMConstantIslands::reorderThumb2JumpTables() {
1986 bool MadeChange = false;
1988 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1989 if (!MJTI) return false;
1991 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1992 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1993 MachineInstr *MI = T2JumpTables[i];
1994 const MCInstrDesc &MCID = MI->getDesc();
1995 unsigned NumOps = MCID.getNumOperands();
1996 unsigned JTOpIdx = NumOps - (MI->isPredicable() ? 3 : 2);
1997 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1998 unsigned JTI = JTOP.getIndex();
1999 assert(JTI < JT.size());
2001 // We prefer if target blocks for the jump table come after the jump
2002 // instruction so we can use TB[BH]. Loop through the target blocks
2003 // and try to adjust them such that that's true.
2004 int JTNumber = MI->getParent()->getNumber();
2005 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
2006 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
2007 MachineBasicBlock *MBB = JTBBs[j];
2008 int DTNumber = MBB->getNumber();
2010 if (DTNumber < JTNumber) {
2011 // The destination precedes the switch. Try to move the block forward
2012 // so we have a positive offset.
2013 MachineBasicBlock *NewBB =
2014 adjustJTTargetBlockForward(MBB, MI->getParent());
2016 MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
2025 MachineBasicBlock *ARMConstantIslands::
2026 adjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) {
2027 // If the destination block is terminated by an unconditional branch,
2028 // try to move it; otherwise, create a new block following the jump
2029 // table that branches back to the actual target. This is a very simple
2030 // heuristic. FIXME: We can definitely improve it.
2031 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
2032 SmallVector<MachineOperand, 4> Cond;
2033 SmallVector<MachineOperand, 4> CondPrior;
2034 MachineFunction::iterator BBi = BB;
2035 MachineFunction::iterator OldPrior = std::prev(BBi);
2037 // If the block terminator isn't analyzable, don't try to move the block
2038 bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond);
2040 // If the block ends in an unconditional branch, move it. The prior block
2041 // has to have an analyzable terminator for us to move this one. Be paranoid
2042 // and make sure we're not trying to move the entry block of the function.
2043 if (!B && Cond.empty() && BB != MF->begin() &&
2044 !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) {
2045 BB->moveAfter(JTBB);
2046 OldPrior->updateTerminator();
2047 BB->updateTerminator();
2048 // Update numbering to account for the block being moved.
2049 MF->RenumberBlocks();
2054 // Create a new MBB for the code after the jump BB.
2055 MachineBasicBlock *NewBB =
2056 MF->CreateMachineBasicBlock(JTBB->getBasicBlock());
2057 MachineFunction::iterator MBBI = JTBB; ++MBBI;
2058 MF->insert(MBBI, NewBB);
2060 // Add an unconditional branch from NewBB to BB.
2061 // There doesn't seem to be meaningful DebugInfo available; this doesn't
2062 // correspond directly to anything in the source.
2063 assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?");
2064 BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB)
2065 .addImm(ARMCC::AL).addReg(0);
2067 // Update internal data structures to account for the newly inserted MBB.
2068 MF->RenumberBlocks(NewBB);
2071 NewBB->addSuccessor(BB);
2072 JTBB->removeSuccessor(BB);
2073 JTBB->addSuccessor(NewBB);