LLVM 23.0.0git
MachineLICM.cpp
Go to the documentation of this file.
1//===- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ----------===//
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 pass performs loop invariant code motion on machine instructions. We
10// attempt to remove as much code from the body of a loop as possible.
11//
12// This pass is not intended to be a replacement or a complete alternative
13// for the LLVM-IR-level LICM pass. It is only designed to hoist simple
14// constructs that are not exposed before lowering and instruction selection.
15//
16//===----------------------------------------------------------------------===//
17
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/Statistic.h"
43#include "llvm/IR/DebugLoc.h"
45#include "llvm/MC/MCInstrDesc.h"
46#include "llvm/MC/MCRegister.h"
47#include "llvm/Pass.h"
50#include "llvm/Support/Debug.h"
52#include <cassert>
53#include <limits>
54#include <vector>
55
56using namespace llvm;
57
58#define DEBUG_TYPE "machinelicm"
59
60static cl::opt<bool>
61AvoidSpeculation("avoid-speculation",
62 cl::desc("MachineLICM should avoid speculation"),
63 cl::init(true), cl::Hidden);
64
65static cl::opt<bool>
66HoistCheapInsts("hoist-cheap-insts",
67 cl::desc("MachineLICM should hoist even cheap instructions"),
68 cl::init(false), cl::Hidden);
69
70static cl::opt<bool>
71HoistConstStores("hoist-const-stores",
72 cl::desc("Hoist invariant stores"),
73 cl::init(true), cl::Hidden);
74
75static cl::opt<bool> HoistConstLoads("hoist-const-loads",
76 cl::desc("Hoist invariant loads"),
77 cl::init(true), cl::Hidden);
78
79// The default threshold of 100 (i.e. if target block is 100 times hotter)
80// is based on empirical data on a single target and is subject to tuning.
82BlockFrequencyRatioThreshold("block-freq-ratio-threshold",
83 cl::desc("Do not hoist instructions if target"
84 "block is N times hotter than the source."),
85 cl::init(100), cl::Hidden);
86
87enum class UseBFI { None, PGO, All };
88
89static cl::opt<UseBFI>
90DisableHoistingToHotterBlocks("disable-hoisting-to-hotter-blocks",
91 cl::desc("Disable hoisting instructions to"
92 " hotter blocks"),
95 "disable the feature"),
97 "enable the feature when using profile data"),
99 "enable the feature with/wo profile data")));
100
101STATISTIC(NumHoisted,
102 "Number of machine instructions hoisted out of loops");
103STATISTIC(NumLowRP,
104 "Number of instructions hoisted in low reg pressure situation");
105STATISTIC(NumHighLatency,
106 "Number of high latency instructions hoisted");
107STATISTIC(NumCSEed,
108 "Number of hoisted machine instructions CSEed");
109STATISTIC(NumPostRAHoisted,
110 "Number of machine instructions hoisted out of loops post regalloc");
111STATISTIC(NumStoreConst,
112 "Number of stores of const phys reg hoisted out of loops");
113STATISTIC(NumNotHoistedDueToHotness,
114 "Number of instructions not hoisted due to block frequency");
115
116namespace {
117 enum HoistResult { NotHoisted = 1, Hoisted = 2, ErasedMI = 4 };
118
119 class MachineLICMImpl {
120 const TargetInstrInfo *TII = nullptr;
121 const TargetLoweringBase *TLI = nullptr;
122 const TargetRegisterInfo *TRI = nullptr;
123 const MachineFrameInfo *MFI = nullptr;
124 MachineRegisterInfo *MRI = nullptr;
125 TargetSchedModel SchedModel;
126 bool PreRegAlloc = false;
127 bool HasProfileData = false;
128 Pass *LegacyPass;
130
131 // Various analyses that we use...
132 AliasAnalysis *AA = nullptr; // Alias analysis info.
133 MachineBlockFrequencyInfo *MBFI = nullptr; // Machine block frequncy info
134 MachineLoopInfo *MLI = nullptr; // Current MachineLoopInfo
135 MachineDomTreeUpdater *MDTU = nullptr; // Wraps current dominator tree
136
137 // State that is updated as we process loops
138 bool Changed = false; // True if a loop is changed.
139 bool FirstInLoop = false; // True if it's the first LICM in the loop.
140
141 // Holds information about whether it is allowed to move load instructions
142 // out of the loop
143 SmallDenseMap<MachineLoop *, bool> AllowedToHoistLoads;
144
145 // Exit blocks of each Loop.
146 DenseMap<MachineLoop *, SmallVector<MachineBasicBlock *, 8>> ExitBlockMap;
147
148 bool isExitBlock(MachineLoop *CurLoop, const MachineBasicBlock *MBB) {
149 auto [It, Inserted] = ExitBlockMap.try_emplace(CurLoop);
150 if (Inserted) {
151 SmallVector<MachineBasicBlock *, 8> ExitBlocks;
152 CurLoop->getExitBlocks(ExitBlocks);
153 It->second = std::move(ExitBlocks);
154 }
155 return is_contained(It->second, MBB);
156 }
157
158 // Track 'estimated' register pressure.
159 SmallDenseSet<Register> RegSeen;
160 SmallVector<unsigned, 8> RegPressure;
161
162 // Register pressure "limit" per register pressure set. If the pressure
163 // is higher than the limit, then it's considered high.
164 SmallVector<unsigned, 8> RegLimit;
165
166 // Register pressure on path leading from loop preheader to current BB.
168
169 // For each opcode per preheader, keep a list of potential CSE instructions.
170 DenseMap<MachineBasicBlock *,
171 DenseMap<unsigned, std::vector<MachineInstr *>>>
172 CSEMap;
173
174 enum {
175 SpeculateFalse = 0,
176 SpeculateTrue = 1,
177 SpeculateUnknown = 2
178 };
179
180 // If a MBB does not dominate loop exiting blocks then it may not safe
181 // to hoist loads from this block.
182 // Tri-state: 0 - false, 1 - true, 2 - unknown
183 unsigned SpeculationState = SpeculateUnknown;
184
185 public:
186 MachineLICMImpl(bool PreRegAlloc, Pass *LegacyPass,
188 : PreRegAlloc(PreRegAlloc), LegacyPass(LegacyPass), MFAM(MFAM) {
189 assert((LegacyPass || MFAM) && "LegacyPass or MFAM must be provided");
190 assert(!(LegacyPass && MFAM) &&
191 "LegacyPass and MFAM cannot be provided at the same time");
192 }
193
194 bool run(MachineFunction &MF);
195
196 void releaseMemory() {
197 RegSeen.clear();
198 RegPressure.clear();
199 RegLimit.clear();
200 BackTrace.clear();
201 CSEMap.clear();
202 ExitBlockMap.clear();
203 }
204
205 private:
206 /// Keep track of information about hoisting candidates.
207 struct CandidateInfo {
208 MachineInstr *MI;
209 Register Def;
210 int FI;
211
212 CandidateInfo(MachineInstr *mi, Register def, int fi)
213 : MI(mi), Def(def), FI(fi) {}
214 };
215
216 void HoistRegionPostRA(MachineLoop *CurLoop);
217
218 void HoistPostRA(MachineInstr *MI, Register Def, MachineLoop *CurLoop);
219
220 void ProcessMI(MachineInstr *MI, BitVector &RUDefs, BitVector &RUClobbers,
221 SmallDenseSet<int> &StoredFIs,
222 SmallVectorImpl<CandidateInfo> &Candidates,
223 MachineLoop *CurLoop);
224
225 void AddToLiveIns(MCRegister Reg, MachineLoop *CurLoop);
226
227 bool IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop);
228
229 bool IsLoopInvariantInst(MachineInstr &I, MachineLoop *CurLoop);
230
231 bool HasLoopPHIUse(const MachineInstr *MI, MachineLoop *CurLoop);
232
233 bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, Register Reg,
234 MachineLoop *CurLoop) const;
235
236 bool IsCheapInstruction(MachineInstr &MI) const;
237
238 bool CanCauseHighRegPressure(const SmallDenseMap<unsigned, int> &Cost,
239 bool Cheap);
240
241 void UpdateBackTraceRegPressure(const MachineInstr *MI);
242
243 bool IsProfitableToHoist(MachineInstr &MI, MachineLoop *CurLoop);
244
245 bool IsGuaranteedToExecute(MachineBasicBlock *BB, MachineLoop *CurLoop);
246
247 void EnterScope(MachineBasicBlock *MBB);
248
249 void ExitScope(MachineBasicBlock *MBB);
250
251 void ExitScopeIfDone(
252 MachineDomTreeNode *Node,
253 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
254 const DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
255
256 void HoistOutOfLoop(MachineDomTreeNode *HeaderN, MachineLoop *CurLoop);
257
258 void InitRegPressure(MachineBasicBlock *BB);
259
260 SmallDenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
261 bool ConsiderSeen,
262 bool ConsiderUnseenAsDef);
263
264 void UpdateRegPressure(const MachineInstr *MI,
265 bool ConsiderUnseenAsDef = false);
266
267 MachineInstr *ExtractHoistableLoad(MachineInstr *MI, MachineLoop *CurLoop);
268
269 MachineInstr *LookForDuplicate(const MachineInstr *MI,
270 std::vector<MachineInstr *> &PrevMIs);
271
272 bool
273 EliminateCSE(MachineInstr *MI,
274 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI);
275
276 bool MayCSE(MachineInstr *MI);
277
278 unsigned Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
279 MachineLoop *CurLoop);
280
281 void InitCSEMap(MachineBasicBlock *BB);
282
283 void InitializeLoadsHoistableLoops();
284
285 bool isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
286 MachineBasicBlock *TgtBlock);
287 MachineBasicBlock *getOrCreatePreheader(MachineLoop *CurLoop);
288 };
289
290 class MachineLICMBase : public MachineFunctionPass {
291 bool PreRegAlloc;
292
293 public:
294 MachineLICMBase(char &ID, bool PreRegAlloc)
295 : MachineFunctionPass(ID), PreRegAlloc(PreRegAlloc) {}
296
297 bool runOnMachineFunction(MachineFunction &MF) override;
298
299 void getAnalysisUsage(AnalysisUsage &AU) const override {
300 AU.addRequired<MachineLoopInfoWrapperPass>();
302 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
303 AU.addRequired<MachineDominatorTreeWrapperPass>();
304 AU.addRequired<AAResultsWrapperPass>();
305 AU.addPreserved<MachineLoopInfoWrapperPass>();
307 }
308 };
309
310 class MachineLICM : public MachineLICMBase {
311 public:
312 static char ID;
313 MachineLICM() : MachineLICMBase(ID, false) {
315 }
316 };
317
318 class EarlyMachineLICM : public MachineLICMBase {
319 public:
320 static char ID;
321 EarlyMachineLICM() : MachineLICMBase(ID, true) {
323 }
324 };
325
326} // end anonymous namespace
327
328char MachineLICM::ID;
329char EarlyMachineLICM::ID;
330
331char &llvm::MachineLICMID = MachineLICM::ID;
332char &llvm::EarlyMachineLICMID = EarlyMachineLICM::ID;
333
335 "Machine Loop Invariant Code Motion", false, false)
341 "Machine Loop Invariant Code Motion", false, false)
342
343INITIALIZE_PASS_BEGIN(EarlyMachineLICM, "early-machinelicm",
344 "Early Machine Loop Invariant Code Motion", false, false)
349INITIALIZE_PASS_END(EarlyMachineLICM, "early-machinelicm",
350 "Early Machine Loop Invariant Code Motion", false, false)
351
352bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
353 if (skipFunction(MF.getFunction()))
354 return false;
355
356 MachineLICMImpl Impl(PreRegAlloc, this, nullptr);
357 return Impl.run(MF);
358}
359
360#define GET_RESULT(RESULT, GETTER, INFIX) \
361 ((LegacyPass) \
362 ? &LegacyPass->getAnalysis<RESULT##INFIX##WrapperPass>().GETTER() \
363 : &MFAM->getResult<RESULT##Analysis>(MF))
364
365bool MachineLICMImpl::run(MachineFunction &MF) {
366 AA = MFAM != nullptr
368 .getManager()
369 .getResult<AAManager>(MF.getFunction())
370 : &LegacyPass->getAnalysis<AAResultsWrapperPass>().getAAResults();
372 MachineDomTreeUpdater::UpdateStrategy::Lazy);
373 MDTU = &DTU;
374 MLI = GET_RESULT(MachineLoop, getLI, Info);
376 ? GET_RESULT(MachineBlockFrequency, getMBFI, Info)
377 : nullptr;
378
379 Changed = FirstInLoop = false;
380 const TargetSubtargetInfo &ST = MF.getSubtarget();
381 TII = ST.getInstrInfo();
382 TLI = ST.getTargetLowering();
383 TRI = ST.getRegisterInfo();
384 MFI = &MF.getFrameInfo();
385 MRI = &MF.getRegInfo();
386 SchedModel.init(&ST);
387
388 HasProfileData = MF.getFunction().hasProfileData();
389
390 if (PreRegAlloc)
391 LLVM_DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
392 else
393 LLVM_DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
394 LLVM_DEBUG(dbgs() << MF.getName() << " ********\n");
395
396 if (PreRegAlloc) {
397 // Estimate register pressure during pre-regalloc pass.
398 unsigned NumRPS = TRI->getNumRegPressureSets();
399 RegPressure.resize(NumRPS);
400 llvm::fill(RegPressure, 0);
401 RegLimit.resize(NumRPS);
402 for (unsigned i = 0, e = NumRPS; i != e; ++i)
403 RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
404 }
405
406 if (HoistConstLoads)
407 InitializeLoadsHoistableLoops();
408
409 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
410 while (!Worklist.empty()) {
411 MachineLoop *CurLoop = Worklist.pop_back_val();
412
413 if (!PreRegAlloc) {
414 HoistRegionPostRA(CurLoop);
415 } else {
416 // CSEMap is initialized for loop header when the first instruction is
417 // being hoisted.
418 MachineDomTreeNode *N = MDTU->getDomTree().getNode(CurLoop->getHeader());
419 FirstInLoop = true;
420 HoistOutOfLoop(N, CurLoop);
421 CSEMap.clear();
422 }
423 }
424 releaseMemory();
425 return Changed;
426}
427
428/// Return true if instruction stores to the specified frame.
429static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
430 // Check mayStore before memory operands so that e.g. DBG_VALUEs will return
431 // true since they have no memory operands.
432 if (!MI->mayStore())
433 return false;
434 // If we lost memory operands, conservatively assume that the instruction
435 // writes to all slots.
436 if (MI->memoperands_empty())
437 return true;
438 for (const MachineMemOperand *MemOp : MI->memoperands()) {
439 if (!MemOp->isStore() || !MemOp->getPseudoValue())
440 continue;
442 dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
443 if (Value->getFrameIndex() == FI)
444 return true;
445 }
446 }
447 return false;
448}
449
451 BitVector &RUs,
452 const uint32_t *Mask) {
453 // FIXME: This intentionally works in reverse due to some issues with the
454 // Register Units infrastructure.
455 //
456 // This is used to apply callee-saved-register masks to the clobbered regunits
457 // mask.
458 //
459 // The right way to approach this is to start with a BitVector full of ones,
460 // then reset all the bits of the regunits of each register that is set in the
461 // mask (registers preserved), then OR the resulting bits with the Clobbers
462 // mask. This correctly prioritizes the saved registers, so if a RU is shared
463 // between a register that is preserved, and one that is NOT preserved, that
464 // RU will not be set in the output vector (the clobbers).
465 //
466 // What we have to do for now is the opposite: we have to assume that the
467 // regunits of all registers that are NOT preserved are clobbered, even if
468 // those regunits are preserved by another register. So if a RU is shared
469 // like described previously, that RU will be set.
470 //
471 // This is to work around an issue which appears in AArch64, but isn't
472 // exclusive to that target: AArch64's Qn registers (128 bits) have Dn
473 // register (lower 64 bits). A few Dn registers are preserved by some calling
474 // conventions, but Qn and Dn share exactly the same reg units.
475 //
476 // If we do this the right way, Qn will be marked as NOT clobbered even though
477 // its upper 64 bits are NOT preserved. The conservative approach handles this
478 // correctly at the cost of some missed optimizations on other targets.
479 //
480 // This is caused by how RegUnits are handled within TableGen. Ideally, Qn
481 // should have an extra RegUnit to model the "unknown" bits not covered by the
482 // subregs.
483 BitVector RUsFromRegsNotInMask(TRI.getNumRegUnits());
484 const unsigned NumRegs = TRI.getNumRegs();
485 const unsigned MaskWords = (NumRegs + 31) / 32;
486 for (unsigned K = 0; K < MaskWords; ++K) {
487 const uint32_t Word = Mask[K];
488 for (unsigned Bit = 0; Bit < 32; ++Bit) {
489 const unsigned PhysReg = (K * 32) + Bit;
490 if (PhysReg == NumRegs)
491 break;
492
493 if (PhysReg && !((Word >> Bit) & 1)) {
494 for (MCRegUnit Unit : TRI.regunits(PhysReg))
495 RUsFromRegsNotInMask.set(static_cast<unsigned>(Unit));
496 }
497 }
498 }
499
500 RUs |= RUsFromRegsNotInMask;
501}
502
503/// Examine the instruction for potential LICM candidate. Also
504/// gather register def and frame object update information.
505void MachineLICMImpl::ProcessMI(MachineInstr *MI, BitVector &RUDefs,
506 BitVector &RUClobbers,
507 SmallDenseSet<int> &StoredFIs,
508 SmallVectorImpl<CandidateInfo> &Candidates,
509 MachineLoop *CurLoop) {
510 bool RuledOut = false;
511 bool HasNonInvariantUse = false;
513 for (const MachineOperand &MO : MI->operands()) {
514 if (MO.isFI()) {
515 // Remember if the instruction stores to the frame index.
516 int FI = MO.getIndex();
517 if (!StoredFIs.count(FI) &&
518 MFI->isSpillSlotObjectIndex(FI) &&
520 StoredFIs.insert(FI);
521 HasNonInvariantUse = true;
522 continue;
523 }
524
525 // We can't hoist an instruction defining a physreg that is clobbered in
526 // the loop.
527 if (MO.isRegMask()) {
528 applyBitsNotInRegMaskToRegUnitsMask(*TRI, RUClobbers, MO.getRegMask());
529 continue;
530 }
531
532 if (!MO.isReg())
533 continue;
534 Register Reg = MO.getReg();
535 if (!Reg)
536 continue;
537 assert(Reg.isPhysical() && "Not expecting virtual register!");
538
539 if (!MO.isDef()) {
540 if (!HasNonInvariantUse) {
541 for (MCRegUnit Unit : TRI->regunits(Reg)) {
542 // If it's using a non-loop-invariant register, then it's obviously
543 // not safe to hoist.
544 if (RUDefs.test(static_cast<unsigned>(Unit)) ||
545 RUClobbers.test(static_cast<unsigned>(Unit))) {
546 HasNonInvariantUse = true;
547 break;
548 }
549 }
550 }
551 continue;
552 }
553
554 // FIXME: For now, avoid instructions with multiple defs, unless it's dead.
555 if (!MO.isDead()) {
556 if (Def)
557 RuledOut = true;
558 else
559 Def = Reg;
560 }
561
562 // If we have already seen another instruction that defines the same
563 // register, then this is not safe. Two defs is indicated by setting a
564 // PhysRegClobbers bit.
565 for (MCRegUnit Unit : TRI->regunits(Reg)) {
566 if (RUDefs.test(static_cast<unsigned>(Unit))) {
567 RUClobbers.set(static_cast<unsigned>(Unit));
568 RuledOut = true;
569 } else if (RUClobbers.test(static_cast<unsigned>(Unit))) {
570 // MI defined register is seen defined by another instruction in
571 // the loop, it cannot be a LICM candidate.
572 RuledOut = true;
573 }
574
575 RUDefs.set(static_cast<unsigned>(Unit));
576 }
577 }
578
579 // Only consider reloads for now and remats which do not have register
580 // operands. FIXME: Consider unfold load folding instructions.
581 if (Def && !RuledOut) {
582 int FI = std::numeric_limits<int>::min();
583 if ((!HasNonInvariantUse && IsLICMCandidate(*MI, CurLoop)) ||
585 Candidates.push_back(CandidateInfo(MI, Def, FI));
586 }
587}
588
589/// Walk the specified region of the CFG and hoist loop invariants out to the
590/// preheader.
591void MachineLICMImpl::HoistRegionPostRA(MachineLoop *CurLoop) {
592 MachineBasicBlock *Preheader = getOrCreatePreheader(CurLoop);
593 if (!Preheader)
594 return;
595
596 unsigned NumRegUnits = TRI->getNumRegUnits();
597 BitVector RUDefs(NumRegUnits); // RUs defined once in the loop.
598 BitVector RUClobbers(NumRegUnits); // RUs defined more than once.
599
601 SmallDenseSet<int> StoredFIs;
602
603 // Walk the entire region, count number of defs for each register, and
604 // collect potential LICM candidates.
605 for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
606 // If the header of the loop containing this basic block is a landing pad,
607 // then don't try to hoist instructions out of this loop.
608 const MachineLoop *ML = MLI->getLoopFor(BB);
609 if (ML && ML->getHeader()->isEHPad()) continue;
610
611 // Conservatively treat live-in's as an external def.
612 // FIXME: That means a reload that're reused in successor block(s) will not
613 // be LICM'ed.
614 for (const auto &LI : BB->liveins()) {
615 for (MCRegUnit Unit : TRI->regunits(LI.PhysReg))
616 RUDefs.set(static_cast<unsigned>(Unit));
617 }
618
619 // Funclet entry blocks will clobber all registers
620 if (const uint32_t *Mask = BB->getBeginClobberMask(TRI))
621 applyBitsNotInRegMaskToRegUnitsMask(*TRI, RUClobbers, Mask);
622
623 // EH landing pads clobber exception pointer/selector registers.
624 if (BB->isEHPad()) {
625 const MachineFunction &MF = *BB->getParent();
626 const Constant *PersonalityFn = MF.getFunction().getPersonalityFn();
627 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
628 if (MCRegister Reg = TLI.getExceptionPointerRegister(PersonalityFn))
629 for (MCRegUnit Unit : TRI->regunits(Reg))
630 RUClobbers.set(static_cast<unsigned>(Unit));
631 if (MCRegister Reg = TLI.getExceptionSelectorRegister(PersonalityFn))
632 for (MCRegUnit Unit : TRI->regunits(Reg))
633 RUClobbers.set(static_cast<unsigned>(Unit));
634 }
635
636 SpeculationState = SpeculateUnknown;
637 for (MachineInstr &MI : *BB)
638 ProcessMI(&MI, RUDefs, RUClobbers, StoredFIs, Candidates, CurLoop);
639 }
640
641 // Gather the registers read / clobbered by the terminator.
642 BitVector TermRUs(NumRegUnits);
644 if (TI != Preheader->end()) {
645 for (const MachineOperand &MO : TI->operands()) {
646 if (!MO.isReg())
647 continue;
648 Register Reg = MO.getReg();
649 if (!Reg)
650 continue;
651 for (MCRegUnit Unit : TRI->regunits(Reg))
652 TermRUs.set(static_cast<unsigned>(Unit));
653 }
654 }
655
656 // Now evaluate whether the potential candidates qualify.
657 // 1. Check if the candidate defined register is defined by another
658 // instruction in the loop.
659 // 2. If the candidate is a load from stack slot (always true for now),
660 // check if the slot is stored anywhere in the loop.
661 // 3. Make sure candidate def should not clobber
662 // registers read by the terminator. Similarly its def should not be
663 // clobbered by the terminator.
664 for (CandidateInfo &Candidate : Candidates) {
665 if (Candidate.FI != std::numeric_limits<int>::min() &&
666 StoredFIs.count(Candidate.FI))
667 continue;
668
669 Register Def = Candidate.Def;
670 bool Safe = true;
671 for (MCRegUnit Unit : TRI->regunits(Def)) {
672 if (RUClobbers.test(static_cast<unsigned>(Unit)) ||
673 TermRUs.test(static_cast<unsigned>(Unit))) {
674 Safe = false;
675 break;
676 }
677 }
678
679 if (!Safe)
680 continue;
681
682 MachineInstr *MI = Candidate.MI;
683 for (const MachineOperand &MO : MI->all_uses()) {
684 if (!MO.getReg())
685 continue;
686 for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
687 if (RUDefs.test(static_cast<unsigned>(Unit)) ||
688 RUClobbers.test(static_cast<unsigned>(Unit))) {
689 // If it's using a non-loop-invariant register, then it's obviously
690 // not safe to hoist.
691 Safe = false;
692 break;
693 }
694 }
695
696 if (!Safe)
697 break;
698 }
699
700 if (Safe)
701 HoistPostRA(MI, Candidate.Def, CurLoop);
702 }
703}
704
705/// Add register 'Reg' to the livein sets of BBs in the current loop, and make
706/// sure it is not killed by any instructions in the loop.
707void MachineLICMImpl::AddToLiveIns(MCRegister Reg, MachineLoop *CurLoop) {
708 for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
709 if (!BB->isLiveIn(Reg))
710 BB->addLiveIn(Reg);
711 for (MachineInstr &MI : *BB) {
712 for (MachineOperand &MO : MI.all_uses()) {
713 if (!MO.getReg())
714 continue;
715 if (TRI->regsOverlap(Reg, MO.getReg()))
716 MO.setIsKill(false);
717 }
718 }
719 }
720}
721
722/// When an instruction is found to only use loop invariant operands that is
723/// safe to hoist, this instruction is called to do the dirty work.
724void MachineLICMImpl::HoistPostRA(MachineInstr *MI, Register Def,
725 MachineLoop *CurLoop) {
726 MachineBasicBlock *Preheader = CurLoop->getLoopPreheader();
727
728 // Now move the instructions to the predecessor, inserting it before any
729 // terminator instructions.
730 LLVM_DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader)
731 << " from " << printMBBReference(*MI->getParent()) << ": "
732 << *MI);
733
734 // Splice the instruction to the preheader.
735 MachineBasicBlock *MBB = MI->getParent();
736 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
737
738 // Since we are moving the instruction out of its basic block, we do not
739 // retain its debug location. Doing so would degrade the debugging
740 // experience and adversely affect the accuracy of profiling information.
741 assert(!MI->isDebugInstr() && "Should not hoist debug inst");
742 MI->setDebugLoc(DebugLoc());
743
744 // Add register to livein list to all the BBs in the current loop since a
745 // loop invariant must be kept live throughout the whole loop. This is
746 // important to ensure later passes do not scavenge the def register.
747 AddToLiveIns(Def, CurLoop);
748
749 ++NumPostRAHoisted;
750 Changed = true;
751}
752
753/// Check if this mbb is guaranteed to execute. If not then a load from this mbb
754/// may not be safe to hoist.
755bool MachineLICMImpl::IsGuaranteedToExecute(MachineBasicBlock *BB,
756 MachineLoop *CurLoop) {
757 if (SpeculationState != SpeculateUnknown)
758 return SpeculationState == SpeculateFalse;
759
760 if (BB != CurLoop->getHeader()) {
761 // Check loop exiting blocks.
762 SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
763 CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
764 for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
765 if (!MDTU->getDomTree().dominates(BB, CurrentLoopExitingBlock)) {
766 SpeculationState = SpeculateTrue;
767 return false;
768 }
769 }
770
771 SpeculationState = SpeculateFalse;
772 return true;
773}
774
775void MachineLICMImpl::EnterScope(MachineBasicBlock *MBB) {
776 LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n');
777
778 // Remember livein register pressure.
779 BackTrace.push_back(RegPressure);
780}
781
782void MachineLICMImpl::ExitScope(MachineBasicBlock *MBB) {
783 LLVM_DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n');
784 BackTrace.pop_back();
785}
786
787/// Destroy scope for the MBB that corresponds to the given dominator tree node
788/// if its a leaf or all of its children are done. Walk up the dominator tree to
789/// destroy ancestors which are now done.
790void MachineLICMImpl::ExitScopeIfDone(
791 MachineDomTreeNode *Node,
792 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
793 const DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap) {
794 if (OpenChildren[Node])
795 return;
796
797 for(;;) {
798 ExitScope(Node->getBlock());
799 // Now traverse upwards to pop ancestors whose offsprings are all done.
800 MachineDomTreeNode *Parent = ParentMap.lookup(Node);
801 if (!Parent || --OpenChildren[Parent] != 0)
802 break;
803 Node = Parent;
804 }
805}
806
807/// Walk the specified loop in the CFG (defined by all blocks dominated by the
808/// specified header block, and that are in the current loop) in depth first
809/// order w.r.t the DominatorTree. This allows us to visit definitions before
810/// uses, allowing us to hoist a loop body in one pass without iteration.
811void MachineLICMImpl::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
812 MachineLoop *CurLoop) {
813 MachineBasicBlock *Preheader = getOrCreatePreheader(CurLoop);
814 if (!Preheader)
815 return;
816
819 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
820 DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
821
822 // Perform a DFS walk to determine the order of visit.
823 WorkList.push_back(HeaderN);
824 while (!WorkList.empty()) {
826 assert(Node && "Null dominator tree node?");
827 MachineBasicBlock *BB = Node->getBlock();
828
829 // If the header of the loop containing this basic block is a landing pad,
830 // then don't try to hoist instructions out of this loop.
831 const MachineLoop *ML = MLI->getLoopFor(BB);
832 if (ML && ML->getHeader()->isEHPad())
833 continue;
834
835 // If this subregion is not in the top level loop at all, exit.
836 if (!CurLoop->contains(BB))
837 continue;
838
839 Scopes.push_back(Node);
840
841 // Don't hoist things out of a large switch statement. This often causes
842 // code to be hoisted that wasn't going to be executed, and increases
843 // register pressure in a situation where it's likely to matter.
844 if (BB->succ_size() >= 25) {
845 OpenChildren[Node] = 0;
846 continue;
847 }
848
849 // Add children in reverse order as then the next popped worklist node is
850 // the first child of this node. This means we ultimately traverse the
851 // DOM tree in exactly the same order as if we'd recursed.
852 size_t WorkListStart = WorkList.size();
853 for (MachineDomTreeNode *Child : Node->children()) {
854 ParentMap[Child] = Node;
855 WorkList.push_back(Child);
856 }
857 std::reverse(WorkList.begin() + WorkListStart, WorkList.end());
858 OpenChildren[Node] = WorkList.size() - WorkListStart;
859 }
860
861 if (Scopes.size() == 0)
862 return;
863
864 // Compute registers which are livein into the loop headers.
865 RegSeen.clear();
866 BackTrace.clear();
867 InitRegPressure(Preheader);
868
869 // Now perform LICM.
870 for (MachineDomTreeNode *Node : Scopes) {
871 MachineBasicBlock *MBB = Node->getBlock();
872
873 EnterScope(MBB);
874
875 // Process the block
876 SpeculationState = SpeculateUnknown;
877 for (MachineInstr &MI : llvm::make_early_inc_range(*MBB)) {
878 unsigned HoistRes = HoistResult::NotHoisted;
879 HoistRes = Hoist(&MI, Preheader, CurLoop);
880 if (HoistRes & HoistResult::NotHoisted) {
881 // We have failed to hoist MI to outermost loop's preheader. If MI is in
882 // a subloop, try to hoist it to subloop's preheader.
883 SmallVector<MachineLoop *> InnerLoopWorkList;
884 for (MachineLoop *L = MLI->getLoopFor(MI.getParent()); L != CurLoop;
885 L = L->getParentLoop())
886 InnerLoopWorkList.push_back(L);
887
888 while (!InnerLoopWorkList.empty()) {
889 MachineLoop *InnerLoop = InnerLoopWorkList.pop_back_val();
890 MachineBasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader();
891 if (InnerLoopPreheader) {
892 HoistRes = Hoist(&MI, InnerLoopPreheader, InnerLoop);
893 if (HoistRes & HoistResult::Hoisted)
894 break;
895 }
896 }
897 }
898
899 if (HoistRes & HoistResult::ErasedMI)
900 continue;
901
902 UpdateRegPressure(&MI);
903 }
904
905 // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
906 ExitScopeIfDone(Node, OpenChildren, ParentMap);
907 }
908}
909
911 return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
912}
913
914/// Find all virtual register references that are liveout of the preheader to
915/// initialize the starting "register pressure". Note this does not count live
916/// through (livein but not used) registers.
917void MachineLICMImpl::InitRegPressure(MachineBasicBlock *BB) {
918 llvm::fill(RegPressure, 0);
919
920 // If the preheader has only a single predecessor and it ends with a
921 // fallthrough or an unconditional branch, then scan its predecessor for live
922 // defs as well. This happens whenever the preheader is created by splitting
923 // the critical edge from the loop predecessor to the loop header.
924 if (BB->pred_size() == 1) {
925 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
927 if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
928 InitRegPressure(*BB->pred_begin());
929 }
930
931 for (const MachineInstr &MI : *BB)
932 UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
933}
934
935/// Update estimate of register pressure after the specified instruction.
936void MachineLICMImpl::UpdateRegPressure(const MachineInstr *MI,
937 bool ConsiderUnseenAsDef) {
938 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
939 for (const auto &[Class, Weight] : Cost) {
940 if (static_cast<int>(RegPressure[Class]) < -Weight)
941 RegPressure[Class] = 0;
942 else
943 RegPressure[Class] += Weight;
944 }
945}
946
947/// Calculate the additional register pressure that the registers used in MI
948/// cause.
949///
950/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
951/// figure out which usages are live-ins.
952/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
953SmallDenseMap<unsigned, int>
954MachineLICMImpl::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
955 bool ConsiderUnseenAsDef) {
956 SmallDenseMap<unsigned, int> Cost;
957 if (MI->isImplicitDef())
958 return Cost;
959 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
960 const MachineOperand &MO = MI->getOperand(i);
961 if (!MO.isReg() || MO.isImplicit())
962 continue;
963 Register Reg = MO.getReg();
964 if (!Reg.isVirtual())
965 continue;
966
967 // FIXME: It seems bad to use RegSeen only for some of these calculations.
968 bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
969 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
970
971 RegClassWeight W = TRI->getRegClassWeight(RC);
972 int RCCost = 0;
973 if (MO.isDef())
974 RCCost = W.RegWeight;
975 else {
976 bool isKill = isOperandKill(MO, MRI);
977 if (isNew && !isKill && ConsiderUnseenAsDef)
978 // Haven't seen this, it must be a livein.
979 RCCost = W.RegWeight;
980 else if (!isNew && isKill)
981 RCCost = -W.RegWeight;
982 }
983 if (RCCost == 0)
984 continue;
985 const int *PS = TRI->getRegClassPressureSets(RC);
986 for (; *PS != -1; ++PS)
987 Cost[*PS] += RCCost;
988 }
989 return Cost;
990}
991
992/// Return true if this machine instruction loads from global offset table or
993/// constant pool.
995 assert(MI.mayLoad() && "Expected MI that loads!");
996
997 // If we lost memory operands, conservatively assume that the instruction
998 // reads from everything..
999 if (MI.memoperands_empty())
1000 return true;
1001
1002 for (MachineMemOperand *MemOp : MI.memoperands())
1003 if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
1004 if (PSV->isGOT() || PSV->isConstantPool())
1005 return true;
1006
1007 return false;
1008}
1009
1010// This function iterates through all the operands of the input store MI and
1011// checks that each register operand statisfies isCallerPreservedPhysReg.
1012// This means, the value being stored and the address where it is being stored
1013// is constant throughout the body of the function (not including prologue and
1014// epilogue). When called with an MI that isn't a store, it returns false.
1015// A future improvement can be to check if the store registers are constant
1016// throughout the loop rather than throughout the funtion.
1018 const TargetRegisterInfo *TRI,
1019 const MachineRegisterInfo *MRI) {
1020
1021 bool FoundCallerPresReg = false;
1022 if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
1023 (MI.getNumOperands() == 0))
1024 return false;
1025
1026 // Check that all register operands are caller-preserved physical registers.
1027 for (const MachineOperand &MO : MI.operands()) {
1028 if (MO.isReg()) {
1029 Register Reg = MO.getReg();
1030 // If operand is a virtual register, check if it comes from a copy of a
1031 // physical register.
1032 if (Reg.isVirtual())
1033 Reg = TRI->lookThruCopyLike(MO.getReg(), MRI);
1034 if (Reg.isVirtual())
1035 return false;
1036 if (!TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *MI.getMF()))
1037 return false;
1038 else
1039 FoundCallerPresReg = true;
1040 } else if (!MO.isImm()) {
1041 return false;
1042 }
1043 }
1044 return FoundCallerPresReg;
1045}
1046
1047// Return true if the input MI is a copy instruction that feeds an invariant
1048// store instruction. This means that the src of the copy has to satisfy
1049// isCallerPreservedPhysReg and atleast one of it's users should satisfy
1050// isInvariantStore.
1052 const MachineRegisterInfo *MRI,
1053 const TargetRegisterInfo *TRI) {
1054
1055 // FIXME: If targets would like to look through instructions that aren't
1056 // pure copies, this can be updated to a query.
1057 if (!MI.isCopy())
1058 return false;
1059
1060 const MachineFunction *MF = MI.getMF();
1061 // Check that we are copying a constant physical register.
1062 Register CopySrcReg = MI.getOperand(1).getReg();
1063 if (CopySrcReg.isVirtual())
1064 return false;
1065
1066 if (!TRI->isCallerPreservedPhysReg(CopySrcReg.asMCReg(), *MF))
1067 return false;
1068
1069 Register CopyDstReg = MI.getOperand(0).getReg();
1070 // Check if any of the uses of the copy are invariant stores.
1071 assert(CopyDstReg.isVirtual() && "copy dst is not a virtual reg");
1072
1073 for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) {
1074 if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI))
1075 return true;
1076 }
1077 return false;
1078}
1079
1080/// Returns true if the instruction may be a suitable candidate for LICM.
1081/// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
1082bool MachineLICMImpl::IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop) {
1083 // Check if it's safe to move the instruction.
1084 bool DontMoveAcrossStore = !HoistConstLoads || !AllowedToHoistLoads[CurLoop];
1085 if ((!I.isSafeToMove(DontMoveAcrossStore)) &&
1087 LLVM_DEBUG(dbgs() << "LICM: Instruction not safe to move.\n");
1088 return false;
1089 }
1090
1091 // If it is a load then check if it is guaranteed to execute by making sure
1092 // that it dominates all exiting blocks. If it doesn't, then there is a path
1093 // out of the loop which does not execute this load, so we can't hoist it.
1094 // Loads from constant memory are safe to speculate, for example indexed load
1095 // from a jump table.
1096 // Stores and side effects are already checked by isSafeToMove.
1097 if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
1098 !IsGuaranteedToExecute(I.getParent(), CurLoop)) {
1099 LLVM_DEBUG(dbgs() << "LICM: Load not guaranteed to execute.\n");
1100 return false;
1101 }
1102
1103 // Convergent attribute has been used on operations that involve inter-thread
1104 // communication which results are implicitly affected by the enclosing
1105 // control flows. It is not safe to hoist or sink such operations across
1106 // control flow.
1107 if (I.isConvergent())
1108 return false;
1109
1110 if (!TII->shouldHoist(I, CurLoop))
1111 return false;
1112
1113 return true;
1114}
1115
1116/// Returns true if the instruction is loop invariant.
1117bool MachineLICMImpl::IsLoopInvariantInst(MachineInstr &I,
1118 MachineLoop *CurLoop) {
1119 if (!IsLICMCandidate(I, CurLoop)) {
1120 LLVM_DEBUG(dbgs() << "LICM: Instruction not a LICM candidate\n");
1121 return false;
1122 }
1123 return CurLoop->isLoopInvariant(I);
1124}
1125
1126/// Return true if the specified instruction is used by a phi node and hoisting
1127/// it could cause a copy to be inserted.
1128bool MachineLICMImpl::HasLoopPHIUse(const MachineInstr *MI,
1129 MachineLoop *CurLoop) {
1131 do {
1132 MI = Work.pop_back_val();
1133 for (const MachineOperand &MO : MI->all_defs()) {
1134 Register Reg = MO.getReg();
1135 if (!Reg.isVirtual())
1136 continue;
1137 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
1138 // A PHI may cause a copy to be inserted.
1139 if (UseMI.isPHI()) {
1140 // A PHI inside the loop causes a copy because the live range of Reg is
1141 // extended across the PHI.
1142 if (CurLoop->contains(&UseMI))
1143 return true;
1144 // A PHI in an exit block can cause a copy to be inserted if the PHI
1145 // has multiple predecessors in the loop with different values.
1146 // For now, approximate by rejecting all exit blocks.
1147 if (isExitBlock(CurLoop, UseMI.getParent()))
1148 return true;
1149 continue;
1150 }
1151 // Look past copies as well.
1152 if (UseMI.isCopy() && CurLoop->contains(&UseMI))
1153 Work.push_back(&UseMI);
1154 }
1155 }
1156 } while (!Work.empty());
1157 return false;
1158}
1159
1160/// Compute operand latency between a def of 'Reg' and an use in the current
1161/// loop, return true if the target considered it high.
1162bool MachineLICMImpl::HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
1163 Register Reg,
1164 MachineLoop *CurLoop) const {
1165 if (MRI->use_nodbg_empty(Reg))
1166 return false;
1167
1168 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
1169 if (UseMI.isCopyLike())
1170 continue;
1171 if (!CurLoop->contains(UseMI.getParent()))
1172 continue;
1173 for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
1174 const MachineOperand &MO = UseMI.getOperand(i);
1175 if (!MO.isReg() || !MO.isUse())
1176 continue;
1177 Register MOReg = MO.getReg();
1178 if (MOReg != Reg)
1179 continue;
1180
1181 if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
1182 return true;
1183 }
1184
1185 // Only look at the first in loop use.
1186 break;
1187 }
1188
1189 return false;
1190}
1191
1192/// Return true if the instruction is marked "cheap" or the operand latency
1193/// between its def and a use is one or less.
1194bool MachineLICMImpl::IsCheapInstruction(MachineInstr &MI) const {
1195 if (TII->isAsCheapAsAMove(MI) || MI.isSubregToReg())
1196 return true;
1197
1198 bool isCheap = false;
1199 unsigned NumDefs = MI.getDesc().getNumDefs();
1200 for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1201 MachineOperand &DefMO = MI.getOperand(i);
1202 if (!DefMO.isReg() || !DefMO.isDef())
1203 continue;
1204 --NumDefs;
1205 Register Reg = DefMO.getReg();
1206 if (Reg.isPhysical())
1207 continue;
1208
1209 if (!TII->hasLowDefLatency(SchedModel, MI, i))
1210 return false;
1211 isCheap = true;
1212 }
1213
1214 return isCheap;
1215}
1216
1217/// Visit BBs from header to current BB, check if hoisting an instruction of the
1218/// given cost matrix can cause high register pressure.
1219bool MachineLICMImpl::CanCauseHighRegPressure(
1220 const SmallDenseMap<unsigned, int> &Cost, bool CheapInstr) {
1221 for (const auto &[Class, Weight] : Cost) {
1222 if (Weight <= 0)
1223 continue;
1224
1225 int Limit = RegLimit[Class];
1226
1227 // Don't hoist cheap instructions if they would increase register pressure,
1228 // even if we're under the limit.
1229 if (CheapInstr && !HoistCheapInsts)
1230 return true;
1231
1232 for (const auto &RP : BackTrace)
1233 if (static_cast<int>(RP[Class]) + Weight >= Limit)
1234 return true;
1235 }
1236
1237 return false;
1238}
1239
1240/// Traverse the back trace from header to the current block and update their
1241/// register pressures to reflect the effect of hoisting MI from the current
1242/// block to the preheader.
1243void MachineLICMImpl::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1244 // First compute the 'cost' of the instruction, i.e. its contribution
1245 // to register pressure.
1246 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1247 /*ConsiderUnseenAsDef=*/false);
1248
1249 // Update register pressure of blocks from loop header to current block.
1250 for (auto &RP : BackTrace)
1251 for (const auto &[Class, Weight] : Cost)
1252 RP[Class] += Weight;
1253}
1254
1255/// Return true if it is potentially profitable to hoist the given loop
1256/// invariant.
1257bool MachineLICMImpl::IsProfitableToHoist(MachineInstr &MI,
1258 MachineLoop *CurLoop) {
1259 if (MI.isImplicitDef())
1260 return true;
1261
1262 // Besides removing computation from the loop, hoisting an instruction has
1263 // these effects:
1264 //
1265 // - The value defined by the instruction becomes live across the entire
1266 // loop. This increases register pressure in the loop.
1267 //
1268 // - If the value is used by a PHI in the loop, a copy will be required for
1269 // lowering the PHI after extending the live range.
1270 //
1271 // - When hoisting the last use of a value in the loop, that value no longer
1272 // needs to be live in the loop. This lowers register pressure in the loop.
1273
1275 return true;
1276
1277 bool CheapInstr = IsCheapInstruction(MI);
1278 bool CreatesCopy = HasLoopPHIUse(&MI, CurLoop);
1279
1280 // Don't hoist a cheap instruction if it would create a copy in the loop.
1281 if (CheapInstr && CreatesCopy) {
1282 LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1283 return false;
1284 }
1285
1286 // Trivially rematerializable instructions should always be hoisted
1287 // providing the register allocator can just pull them down again when needed.
1288 if (TII->isTriviallyReMaterializable(MI))
1289 return true;
1290
1291 // FIXME: If there are long latency loop-invariant instructions inside the
1292 // loop at this point, why didn't the optimizer's LICM hoist them?
1293 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1294 const MachineOperand &MO = MI.getOperand(i);
1295 if (!MO.isReg() || MO.isImplicit())
1296 continue;
1297 Register Reg = MO.getReg();
1298 if (!Reg.isVirtual())
1299 continue;
1300 if (MO.isDef() && HasHighOperandLatency(MI, i, Reg, CurLoop)) {
1301 LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI);
1302 ++NumHighLatency;
1303 return true;
1304 }
1305 }
1306
1307 // Estimate register pressure to determine whether to LICM the instruction.
1308 // In low register pressure situation, we can be more aggressive about
1309 // hoisting. Also, favors hoisting long latency instructions even in
1310 // moderately high pressure situation.
1311 // Cheap instructions will only be hoisted if they don't increase register
1312 // pressure at all.
1313 auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1314 /*ConsiderUnseenAsDef=*/false);
1315
1316 // Visit BBs from header to current BB, if hoisting this doesn't cause
1317 // high register pressure, then it's safe to proceed.
1318 if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1319 LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1320 ++NumLowRP;
1321 return true;
1322 }
1323
1324 // Don't risk increasing register pressure if it would create copies.
1325 if (CreatesCopy) {
1326 LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1327 return false;
1328 }
1329
1330 // Do not "speculate" in high register pressure situation. If an
1331 // instruction is not guaranteed to be executed in the loop, it's best to be
1332 // conservative.
1333 if (AvoidSpeculation &&
1334 (!IsGuaranteedToExecute(MI.getParent(), CurLoop) && !MayCSE(&MI))) {
1335 LLVM_DEBUG(dbgs() << "Won't speculate: " << MI);
1336 return false;
1337 }
1338
1339 // If we have a COPY with other uses in the loop, hoist to allow the users to
1340 // also be hoisted.
1341 // TODO: Handle all isCopyLike?
1342 if (MI.isCopy() || MI.isRegSequence()) {
1343 Register DefReg = MI.getOperand(0).getReg();
1344 if (DefReg.isVirtual() &&
1345 all_of(MI.uses(),
1346 [this](const MachineOperand &UseOp) {
1347 return !UseOp.isReg() || UseOp.getReg().isVirtual() ||
1348 MRI->isConstantPhysReg(UseOp.getReg());
1349 }) &&
1350 IsLoopInvariantInst(MI, CurLoop) &&
1351 any_of(MRI->use_nodbg_instructions(DefReg),
1352 [&CurLoop, this, DefReg,
1353 Cost = std::move(Cost)](MachineInstr &UseMI) {
1354 if (!CurLoop->contains(&UseMI))
1355 return false;
1356
1357 // COPY is a cheap instruction, but if moving it won't cause
1358 // high RP we're fine to hoist it even if the user can't be
1359 // hoisted later Otherwise we want to check the user if it's
1360 // hoistable
1361 if (CanCauseHighRegPressure(Cost, false) &&
1362 !CurLoop->isLoopInvariant(UseMI, DefReg))
1363 return false;
1364
1365 return true;
1366 }))
1367 return true;
1368 }
1369
1370 // High register pressure situation, only hoist if the instruction is going
1371 // to be remat'ed.
1372 if (!TII->isTriviallyReMaterializable(MI) &&
1373 !MI.isDereferenceableInvariantLoad()) {
1374 LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1375 return false;
1376 }
1377
1378 return true;
1379}
1380
1381/// Unfold a load from the given machineinstr if the load itself could be
1382/// hoisted. Return the unfolded and hoistable load, or null if the load
1383/// couldn't be unfolded or if it wouldn't be hoistable.
1384MachineInstr *MachineLICMImpl::ExtractHoistableLoad(MachineInstr *MI,
1385 MachineLoop *CurLoop) {
1386 // Don't unfold simple loads.
1387 if (MI->canFoldAsLoad())
1388 return nullptr;
1389
1390 // If not, we may be able to unfold a load and hoist that.
1391 // First test whether the instruction is loading from an amenable
1392 // memory location.
1393 if (!MI->isDereferenceableInvariantLoad())
1394 return nullptr;
1395
1396 // Next determine the register class for a temporary register.
1397 unsigned LoadRegIndex;
1398 unsigned NewOpc =
1399 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1400 /*UnfoldLoad=*/true,
1401 /*UnfoldStore=*/false,
1402 &LoadRegIndex);
1403 if (NewOpc == 0) return nullptr;
1404 const MCInstrDesc &MID = TII->get(NewOpc);
1405 MachineFunction &MF = *MI->getMF();
1406 const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex);
1407 // Ok, we're unfolding. Create a temporary register and do the unfold.
1408 Register Reg = MRI->createVirtualRegister(RC);
1409
1410 SmallVector<MachineInstr *, 2> NewMIs;
1411 bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
1412 /*UnfoldLoad=*/true,
1413 /*UnfoldStore=*/false, NewMIs);
1414 (void)Success;
1415 assert(Success &&
1416 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1417 "succeeded!");
1418 assert(NewMIs.size() == 2 &&
1419 "Unfolded a load into multiple instructions!");
1420 MachineBasicBlock *MBB = MI->getParent();
1422 MBB->insert(Pos, NewMIs[0]);
1423 MBB->insert(Pos, NewMIs[1]);
1424 // If unfolding produced a load that wasn't loop-invariant or profitable to
1425 // hoist, discard the new instructions and bail.
1426 if (!IsLoopInvariantInst(*NewMIs[0], CurLoop) ||
1427 !IsProfitableToHoist(*NewMIs[0], CurLoop)) {
1428 NewMIs[0]->eraseFromParent();
1429 NewMIs[1]->eraseFromParent();
1430 return nullptr;
1431 }
1432
1433 // Update register pressure for the unfolded instruction.
1434 UpdateRegPressure(NewMIs[1]);
1435
1436 // Otherwise we successfully unfolded a load that we can hoist.
1437
1438 // Update the call info.
1439 if (MI->shouldUpdateAdditionalCallInfo())
1441
1442 MI->eraseFromParent();
1443 return NewMIs[0];
1444}
1445
1446/// Initialize the CSE map with instructions that are in the current loop
1447/// preheader that may become duplicates of instructions that are hoisted
1448/// out of the loop.
1449void MachineLICMImpl::InitCSEMap(MachineBasicBlock *BB) {
1450 for (MachineInstr &MI : *BB)
1451 CSEMap[BB][MI.getOpcode()].push_back(&MI);
1452}
1453
1454/// Initialize AllowedToHoistLoads with information about whether invariant
1455/// loads can be moved outside a given loop
1456void MachineLICMImpl::InitializeLoadsHoistableLoops() {
1457 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
1458 SmallVector<MachineLoop *, 8> LoopsInPreOrder;
1459
1460 // Mark all loops as hoistable initially and prepare a list of loops in
1461 // pre-order DFS.
1462 while (!Worklist.empty()) {
1463 auto *L = Worklist.pop_back_val();
1464 AllowedToHoistLoads[L] = true;
1465 LoopsInPreOrder.push_back(L);
1466 llvm::append_range(Worklist, L->getSubLoops());
1467 }
1468
1469 // Going from the innermost to outermost loops, check if a loop has
1470 // instructions preventing invariant load hoisting. If such instruction is
1471 // found, mark this loop and its parent as non-hoistable and continue
1472 // investigating the next loop.
1473 // Visiting in a reversed pre-ordered DFS manner
1474 // allows us to not process all the instructions of the outer loop if the
1475 // inner loop is proved to be non-load-hoistable.
1476 for (auto *Loop : reverse(LoopsInPreOrder)) {
1477 for (auto *MBB : Loop->blocks()) {
1478 // If this loop has already been marked as non-hoistable, skip it.
1479 if (!AllowedToHoistLoads[Loop])
1480 continue;
1481 for (auto &MI : *MBB) {
1482 if (!MI.isLoadFoldBarrier() && !MI.mayStore() && !MI.isCall() &&
1483 !(MI.mayLoad() && MI.hasOrderedMemoryRef()))
1484 continue;
1485 for (MachineLoop *L = Loop; L != nullptr; L = L->getParentLoop())
1486 AllowedToHoistLoads[L] = false;
1487 break;
1488 }
1489 }
1490 }
1491}
1492
1493/// Find an instruction amount PrevMIs that is a duplicate of MI.
1494/// Return this instruction if it's found.
1495MachineInstr *
1496MachineLICMImpl::LookForDuplicate(const MachineInstr *MI,
1497 std::vector<MachineInstr *> &PrevMIs) {
1498 for (MachineInstr *PrevMI : PrevMIs)
1499 if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
1500 return PrevMI;
1501
1502 return nullptr;
1503}
1504
1505/// Given a LICM'ed instruction, look for an instruction on the preheader that
1506/// computes the same value. If it's found, do a RAU on with the definition of
1507/// the existing instruction rather than hoisting the instruction to the
1508/// preheader.
1509bool MachineLICMImpl::EliminateCSE(
1510 MachineInstr *MI,
1511 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI) {
1512 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1513 // the undef property onto uses.
1514 if (MI->isImplicitDef())
1515 return false;
1516
1517 // Do not CSE normal loads because between them could be store instructions
1518 // that change the loaded value
1519 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1520 return false;
1521
1522 if (MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1523 LLVM_DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1524
1525 // Replace virtual registers defined by MI by their counterparts defined
1526 // by Dup.
1527 SmallVector<unsigned, 2> Defs;
1528 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1529 const MachineOperand &MO = MI->getOperand(i);
1530
1531 // Physical registers may not differ here.
1532 assert((!MO.isReg() || MO.getReg() == 0 || !MO.getReg().isPhysical() ||
1533 MO.getReg() == Dup->getOperand(i).getReg()) &&
1534 "Instructions with different phys regs are not identical!");
1535
1536 if (MO.isReg() && MO.isDef() && !MO.getReg().isPhysical())
1537 Defs.push_back(i);
1538 }
1539
1541 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1542 unsigned Idx = Defs[i];
1543 Register Reg = MI->getOperand(Idx).getReg();
1544 Register DupReg = Dup->getOperand(Idx).getReg();
1545 OrigRCs.push_back(MRI->getRegClass(DupReg));
1546
1547 if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1548 // Restore old RCs if more than one defs.
1549 for (unsigned j = 0; j != i; ++j)
1550 MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1551 return false;
1552 }
1553 }
1554
1555 for (unsigned Idx : Defs) {
1556 Register Reg = MI->getOperand(Idx).getReg();
1557 Register DupReg = Dup->getOperand(Idx).getReg();
1558 MRI->replaceRegWith(Reg, DupReg);
1559 MRI->clearKillFlags(DupReg);
1560 // Clear Dup dead flag if any, we reuse it for Reg.
1561 if (!MRI->use_nodbg_empty(DupReg))
1562 Dup->getOperand(Idx).setIsDead(false);
1563 }
1564
1565 MI->eraseFromParent();
1566 ++NumCSEed;
1567 return true;
1568 }
1569 return false;
1570}
1571
1572/// Return true if the given instruction will be CSE'd if it's hoisted out of
1573/// the loop.
1574bool MachineLICMImpl::MayCSE(MachineInstr *MI) {
1575 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1576 return false;
1577
1578 unsigned Opcode = MI->getOpcode();
1579 for (auto &Map : CSEMap) {
1580 // Check this CSEMap's preheader dominates MI's basic block.
1581 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1582 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1583 Map.second.find(Opcode);
1584 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1585 // the undef property onto uses.
1586 if (CI == Map.second.end() || MI->isImplicitDef())
1587 continue;
1588 if (LookForDuplicate(MI, CI->second) != nullptr)
1589 return true;
1590 }
1591 }
1592
1593 return false;
1594}
1595
1596/// When an instruction is found to use only loop invariant operands
1597/// that are safe to hoist, this instruction is called to do the dirty work.
1598/// It returns true if the instruction is hoisted.
1599unsigned MachineLICMImpl::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
1600 MachineLoop *CurLoop) {
1601 MachineBasicBlock *SrcBlock = MI->getParent();
1602
1603 // Disable the instruction hoisting due to block hotness
1605 (DisableHoistingToHotterBlocks == UseBFI::PGO && HasProfileData)) &&
1606 isTgtHotterThanSrc(SrcBlock, Preheader)) {
1607 ++NumNotHoistedDueToHotness;
1608 return HoistResult::NotHoisted;
1609 }
1610 // First check whether we should hoist this instruction.
1611 bool HasExtractHoistableLoad = false;
1612 if (!IsLoopInvariantInst(*MI, CurLoop) ||
1613 !IsProfitableToHoist(*MI, CurLoop)) {
1614 // If not, try unfolding a hoistable load.
1615 MI = ExtractHoistableLoad(MI, CurLoop);
1616 if (!MI)
1617 return HoistResult::NotHoisted;
1618 HasExtractHoistableLoad = true;
1619 }
1620
1621 // If we have hoisted an instruction that may store, it can only be a constant
1622 // store.
1623 if (MI->mayStore())
1624 NumStoreConst++;
1625
1626 // Now move the instructions to the predecessor, inserting it before any
1627 // terminator instructions.
1628 LLVM_DEBUG({
1629 dbgs() << "Hoisting " << *MI;
1630 if (MI->getParent()->getBasicBlock())
1631 dbgs() << " from " << printMBBReference(*MI->getParent());
1632 if (Preheader->getBasicBlock())
1633 dbgs() << " to " << printMBBReference(*Preheader);
1634 dbgs() << "\n";
1635 });
1636
1637 // If this is the first instruction being hoisted to the preheader,
1638 // initialize the CSE map with potential common expressions.
1639 if (FirstInLoop) {
1640 InitCSEMap(Preheader);
1641 FirstInLoop = false;
1642 }
1643
1644 // Look for opportunity to CSE the hoisted instruction.
1645 unsigned Opcode = MI->getOpcode();
1646 bool HasCSEDone = false;
1647 for (auto &Map : CSEMap) {
1648 // Check this CSEMap's preheader dominates MI's basic block.
1649 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1650 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1651 Map.second.find(Opcode);
1652 if (CI != Map.second.end()) {
1653 if (EliminateCSE(MI, CI)) {
1654 HasCSEDone = true;
1655 break;
1656 }
1657 }
1658 }
1659 }
1660
1661 if (!HasCSEDone) {
1662 // Otherwise, splice the instruction to the preheader.
1663 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1664
1665 // Since we are moving the instruction out of its basic block, we do not
1666 // retain its debug location. Doing so would degrade the debugging
1667 // experience and adversely affect the accuracy of profiling information.
1668 assert(!MI->isDebugInstr() && "Should not hoist debug inst");
1669 MI->setDebugLoc(DebugLoc());
1670
1671 // Update register pressure for BBs from header to this block.
1672 UpdateBackTraceRegPressure(MI);
1673
1674 // Clear the kill flags of any register this instruction defines,
1675 // since they may need to be live throughout the entire loop
1676 // rather than just live for part of it.
1677 for (MachineOperand &MO : MI->all_defs())
1678 if (!MO.isDead())
1679 MRI->clearKillFlags(MO.getReg());
1680
1681 CSEMap[Preheader][Opcode].push_back(MI);
1682 }
1683
1684 ++NumHoisted;
1685 Changed = true;
1686
1687 if (HasCSEDone || HasExtractHoistableLoad)
1688 return HoistResult::Hoisted | HoistResult::ErasedMI;
1689 return HoistResult::Hoisted;
1690}
1691
1692/// Get the preheader for the current loop, splitting a critical edge if needed.
1693MachineBasicBlock *MachineLICMImpl::getOrCreatePreheader(MachineLoop *CurLoop) {
1694 // Determine the block to which to hoist instructions. If we can't find a
1695 // suitable loop predecessor, we can't do any hoisting.
1696 if (MachineBasicBlock *Preheader = CurLoop->getLoopPreheader())
1697 return Preheader;
1698
1699 // Try forming a preheader by splitting the critical edge between the single
1700 // predecessor and the loop header.
1701 if (MachineBasicBlock *Pred = CurLoop->getLoopPredecessor()) {
1702 MachineBasicBlock *NewPreheader = Pred->SplitCriticalEdge(
1703 CurLoop->getHeader(), LegacyPass, MFAM, nullptr, MDTU);
1704 if (NewPreheader)
1705 Changed = true;
1706 return NewPreheader;
1707 }
1708
1709 return nullptr;
1710}
1711
1712/// Is the target basic block at least "BlockFrequencyRatioThreshold"
1713/// times hotter than the source basic block.
1714bool MachineLICMImpl::isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
1715 MachineBasicBlock *TgtBlock) {
1716 // Parse source and target basic block frequency from MBFI
1717 uint64_t SrcBF = MBFI->getBlockFreq(SrcBlock).getFrequency();
1718 uint64_t DstBF = MBFI->getBlockFreq(TgtBlock).getFrequency();
1719
1720 // Disable the hoisting if source block frequency is zero
1721 if (!SrcBF)
1722 return true;
1723
1724 double Ratio = (double)DstBF / SrcBF;
1725
1726 // Compare the block frequency ratio with the threshold
1727 return Ratio > BlockFrequencyRatioThreshold;
1728}
1729
1730template <typename DerivedT, bool PreRegAlloc>
1733 bool Changed = MachineLICMImpl(PreRegAlloc, nullptr, &MFAM).run(MF);
1734 if (!Changed)
1735 return PreservedAnalyses::all();
1737 PA.preserve<MachineLoopAnalysis>();
1738 return PA;
1739}
1740
unsigned const MachineRegisterInfo * MRI
#define Success
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
basic Basic Alias true
This file implements the BitVector class.
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file defines the DenseMap class.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
static bool isExitBlock(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &ExitBlocks)
Return true if the specified block is in the list.
Definition LCSSA.cpp:68
#define I(x, y, z)
Definition MD5.cpp:57
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:593
#define GET_RESULT(RESULT, GETTER, INFIX)
static cl::opt< bool > HoistConstStores("hoist-const-stores", cl::desc("Hoist invariant stores"), cl::init(true), cl::Hidden)
static cl::opt< UseBFI > DisableHoistingToHotterBlocks("disable-hoisting-to-hotter-blocks", cl::desc("Disable hoisting instructions to" " hotter blocks"), cl::init(UseBFI::PGO), cl::Hidden, cl::values(clEnumValN(UseBFI::None, "none", "disable the feature"), clEnumValN(UseBFI::PGO, "pgo", "enable the feature when using profile data"), clEnumValN(UseBFI::All, "all", "enable the feature with/wo profile data")))
static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI)
Return true if this machine instruction loads from global offset table or constant pool.
static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI)
static cl::opt< bool > HoistConstLoads("hoist-const-loads", cl::desc("Hoist invariant loads"), cl::init(true), cl::Hidden)
UseBFI
Machine Loop Invariant Code false
static cl::opt< bool > AvoidSpeculation("avoid-speculation", cl::desc("MachineLICM should avoid speculation"), cl::init(true), cl::Hidden)
static bool InstructionStoresToFI(const MachineInstr *MI, int FI)
Return true if instruction stores to the specified frame.
static bool isCopyFeedingInvariantStore(const MachineInstr &MI, const MachineRegisterInfo *MRI, const TargetRegisterInfo *TRI)
static void applyBitsNotInRegMaskToRegUnitsMask(const TargetRegisterInfo &TRI, BitVector &RUs, const uint32_t *Mask)
static cl::opt< bool > HoistCheapInsts("hoist-cheap-insts", cl::desc("MachineLICM should hoist even cheap instructions"), cl::init(false), cl::Hidden)
static bool isInvariantStore(const MachineInstr &MI, const TargetRegisterInfo *TRI, const MachineRegisterInfo *MRI)
static cl::opt< unsigned > BlockFrequencyRatioThreshold("block-freq-ratio-threshold", cl::desc("Do not hoist instructions if target" "block is N times hotter than the source."), cl::init(100), cl::Hidden)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
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:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file describes how to lower LLVM code to machine code.
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool test(unsigned Idx) const
Definition BitVector.h:480
BitVector & set()
Definition BitVector.h:370
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
A specialized PseudoSourceValue for holding FixedStack values, which must include a frame index.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
bool hasProfileData(bool IncludeSynthetic=false) const
Return true if the function is annotated with profile data.
Definition Function.h:334
DomTreeT & getDomTree()
Flush DomTree updates and return DomTree.
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
TargetInstrInfo overrides.
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....
bool isAsCheapAsAMove(const MachineInstr &MI) const override
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
BlockT * getHeader() const
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
iterator end() const
iterator begin() const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic 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 '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
void eraseAdditionalCallInfo(const MachineInstr *MI)
Following functions update call site info.
Representation of each machine instruction.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Analysis pass that exposes the MachineLoopInfo for a machine function.
LLVM_ABI bool isLoopInvariant(MachineInstr &I, const Register ExcludeReg=0) const
Returns true if the instruction is loop invariant.
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Special value supplied for machine level alias analysis.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetLowering * getTargetLowering() const
LLVM Value Representation.
Definition Value.h:75
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
Changed
Abstract Attribute helper functions.
Definition Attributor.h:165
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI char & EarlyMachineLICMID
This pass performs loop invariant code motion on machine instructions.
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI void initializeMachineLICMPass(PassRegistry &)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void initializeEarlyMachineLICMPass(PassRegistry &)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1945
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
LLVM_ABI char & MachineLICMID
This pass performs loop invariant code motion on machine instructions.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
#define N