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