LLVM 22.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(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(Unit) || RUClobbers.test(Unit)) {
545 HasNonInvariantUse = true;
546 break;
547 }
548 }
549 }
550 continue;
551 }
552
553 // FIXME: For now, avoid instructions with multiple defs, unless it's dead.
554 if (!MO.isDead()) {
555 if (Def)
556 RuledOut = true;
557 else
558 Def = Reg;
559 }
560
561 // If we have already seen another instruction that defines the same
562 // register, then this is not safe. Two defs is indicated by setting a
563 // PhysRegClobbers bit.
564 for (MCRegUnit Unit : TRI->regunits(Reg)) {
565 if (RUDefs.test(Unit)) {
566 RUClobbers.set(Unit);
567 RuledOut = true;
568 } else if (RUClobbers.test(Unit)) {
569 // MI defined register is seen defined by another instruction in
570 // the loop, it cannot be a LICM candidate.
571 RuledOut = true;
572 }
573
574 RUDefs.set(Unit);
575 }
576 }
577
578 // Only consider reloads for now and remats which do not have register
579 // operands. FIXME: Consider unfold load folding instructions.
580 if (Def && !RuledOut) {
581 int FI = std::numeric_limits<int>::min();
582 if ((!HasNonInvariantUse && IsLICMCandidate(*MI, CurLoop)) ||
584 Candidates.push_back(CandidateInfo(MI, Def, FI));
585 }
586}
587
588/// Walk the specified region of the CFG and hoist loop invariants out to the
589/// preheader.
590void MachineLICMImpl::HoistRegionPostRA(MachineLoop *CurLoop) {
591 MachineBasicBlock *Preheader = getOrCreatePreheader(CurLoop);
592 if (!Preheader)
593 return;
594
595 unsigned NumRegUnits = TRI->getNumRegUnits();
596 BitVector RUDefs(NumRegUnits); // RUs defined once in the loop.
597 BitVector RUClobbers(NumRegUnits); // RUs defined more than once.
598
600 SmallDenseSet<int> StoredFIs;
601
602 // Walk the entire region, count number of defs for each register, and
603 // collect potential LICM candidates.
604 for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
605 // If the header of the loop containing this basic block is a landing pad,
606 // then don't try to hoist instructions out of this loop.
607 const MachineLoop *ML = MLI->getLoopFor(BB);
608 if (ML && ML->getHeader()->isEHPad()) continue;
609
610 // Conservatively treat live-in's as an external def.
611 // FIXME: That means a reload that're reused in successor block(s) will not
612 // be LICM'ed.
613 for (const auto &LI : BB->liveins()) {
614 for (MCRegUnit Unit : TRI->regunits(LI.PhysReg))
615 RUDefs.set(Unit);
616 }
617
618 // Funclet entry blocks will clobber all registers
619 if (const uint32_t *Mask = BB->getBeginClobberMask(TRI))
620 applyBitsNotInRegMaskToRegUnitsMask(*TRI, RUClobbers, Mask);
621
622 // EH landing pads clobber exception pointer/selector registers.
623 if (BB->isEHPad()) {
624 const MachineFunction &MF = *BB->getParent();
625 const Constant *PersonalityFn = MF.getFunction().getPersonalityFn();
626 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
627 if (MCRegister Reg = TLI.getExceptionPointerRegister(PersonalityFn))
628 for (MCRegUnit Unit : TRI->regunits(Reg))
629 RUClobbers.set(Unit);
630 if (MCRegister Reg = TLI.getExceptionSelectorRegister(PersonalityFn))
631 for (MCRegUnit Unit : TRI->regunits(Reg))
632 RUClobbers.set(Unit);
633 }
634
635 SpeculationState = SpeculateUnknown;
636 for (MachineInstr &MI : *BB)
637 ProcessMI(&MI, RUDefs, RUClobbers, StoredFIs, Candidates, CurLoop);
638 }
639
640 // Gather the registers read / clobbered by the terminator.
641 BitVector TermRUs(NumRegUnits);
643 if (TI != Preheader->end()) {
644 for (const MachineOperand &MO : TI->operands()) {
645 if (!MO.isReg())
646 continue;
647 Register Reg = MO.getReg();
648 if (!Reg)
649 continue;
650 for (MCRegUnit Unit : TRI->regunits(Reg))
651 TermRUs.set(Unit);
652 }
653 }
654
655 // Now evaluate whether the potential candidates qualify.
656 // 1. Check if the candidate defined register is defined by another
657 // instruction in the loop.
658 // 2. If the candidate is a load from stack slot (always true for now),
659 // check if the slot is stored anywhere in the loop.
660 // 3. Make sure candidate def should not clobber
661 // registers read by the terminator. Similarly its def should not be
662 // clobbered by the terminator.
663 for (CandidateInfo &Candidate : Candidates) {
664 if (Candidate.FI != std::numeric_limits<int>::min() &&
665 StoredFIs.count(Candidate.FI))
666 continue;
667
668 Register Def = Candidate.Def;
669 bool Safe = true;
670 for (MCRegUnit Unit : TRI->regunits(Def)) {
671 if (RUClobbers.test(Unit) || TermRUs.test(Unit)) {
672 Safe = false;
673 break;
674 }
675 }
676
677 if (!Safe)
678 continue;
679
680 MachineInstr *MI = Candidate.MI;
681 for (const MachineOperand &MO : MI->all_uses()) {
682 if (!MO.getReg())
683 continue;
684 for (MCRegUnit Unit : TRI->regunits(MO.getReg())) {
685 if (RUDefs.test(Unit) || RUClobbers.test(Unit)) {
686 // If it's using a non-loop-invariant register, then it's obviously
687 // not safe to hoist.
688 Safe = false;
689 break;
690 }
691 }
692
693 if (!Safe)
694 break;
695 }
696
697 if (Safe)
698 HoistPostRA(MI, Candidate.Def, CurLoop);
699 }
700}
701
702/// Add register 'Reg' to the livein sets of BBs in the current loop, and make
703/// sure it is not killed by any instructions in the loop.
704void MachineLICMImpl::AddToLiveIns(MCRegister Reg, MachineLoop *CurLoop) {
705 for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
706 if (!BB->isLiveIn(Reg))
707 BB->addLiveIn(Reg);
708 for (MachineInstr &MI : *BB) {
709 for (MachineOperand &MO : MI.all_uses()) {
710 if (!MO.getReg())
711 continue;
712 if (TRI->regsOverlap(Reg, MO.getReg()))
713 MO.setIsKill(false);
714 }
715 }
716 }
717}
718
719/// When an instruction is found to only use loop invariant operands that is
720/// safe to hoist, this instruction is called to do the dirty work.
721void MachineLICMImpl::HoistPostRA(MachineInstr *MI, Register Def,
722 MachineLoop *CurLoop) {
723 MachineBasicBlock *Preheader = CurLoop->getLoopPreheader();
724
725 // Now move the instructions to the predecessor, inserting it before any
726 // terminator instructions.
727 LLVM_DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader)
728 << " from " << printMBBReference(*MI->getParent()) << ": "
729 << *MI);
730
731 // Splice the instruction to the preheader.
732 MachineBasicBlock *MBB = MI->getParent();
733 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
734
735 // Since we are moving the instruction out of its basic block, we do not
736 // retain its debug location. Doing so would degrade the debugging
737 // experience and adversely affect the accuracy of profiling information.
738 assert(!MI->isDebugInstr() && "Should not hoist debug inst");
739 MI->setDebugLoc(DebugLoc());
740
741 // Add register to livein list to all the BBs in the current loop since a
742 // loop invariant must be kept live throughout the whole loop. This is
743 // important to ensure later passes do not scavenge the def register.
744 AddToLiveIns(Def, CurLoop);
745
746 ++NumPostRAHoisted;
747 Changed = true;
748}
749
750/// Check if this mbb is guaranteed to execute. If not then a load from this mbb
751/// may not be safe to hoist.
752bool MachineLICMImpl::IsGuaranteedToExecute(MachineBasicBlock *BB,
753 MachineLoop *CurLoop) {
754 if (SpeculationState != SpeculateUnknown)
755 return SpeculationState == SpeculateFalse;
756
757 if (BB != CurLoop->getHeader()) {
758 // Check loop exiting blocks.
759 SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
760 CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
761 for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
762 if (!MDTU->getDomTree().dominates(BB, CurrentLoopExitingBlock)) {
763 SpeculationState = SpeculateTrue;
764 return false;
765 }
766 }
767
768 SpeculationState = SpeculateFalse;
769 return true;
770}
771
772void MachineLICMImpl::EnterScope(MachineBasicBlock *MBB) {
773 LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n');
774
775 // Remember livein register pressure.
776 BackTrace.push_back(RegPressure);
777}
778
779void MachineLICMImpl::ExitScope(MachineBasicBlock *MBB) {
780 LLVM_DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n');
781 BackTrace.pop_back();
782}
783
784/// Destroy scope for the MBB that corresponds to the given dominator tree node
785/// if its a leaf or all of its children are done. Walk up the dominator tree to
786/// destroy ancestors which are now done.
787void MachineLICMImpl::ExitScopeIfDone(
788 MachineDomTreeNode *Node,
789 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
790 const DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap) {
791 if (OpenChildren[Node])
792 return;
793
794 for(;;) {
795 ExitScope(Node->getBlock());
796 // Now traverse upwards to pop ancestors whose offsprings are all done.
797 MachineDomTreeNode *Parent = ParentMap.lookup(Node);
798 if (!Parent || --OpenChildren[Parent] != 0)
799 break;
800 Node = Parent;
801 }
802}
803
804/// Walk the specified loop in the CFG (defined by all blocks dominated by the
805/// specified header block, and that are in the current loop) in depth first
806/// order w.r.t the DominatorTree. This allows us to visit definitions before
807/// uses, allowing us to hoist a loop body in one pass without iteration.
808void MachineLICMImpl::HoistOutOfLoop(MachineDomTreeNode *HeaderN,
809 MachineLoop *CurLoop) {
810 MachineBasicBlock *Preheader = getOrCreatePreheader(CurLoop);
811 if (!Preheader)
812 return;
813
816 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
817 DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
818
819 // Perform a DFS walk to determine the order of visit.
820 WorkList.push_back(HeaderN);
821 while (!WorkList.empty()) {
823 assert(Node && "Null dominator tree node?");
824 MachineBasicBlock *BB = Node->getBlock();
825
826 // If the header of the loop containing this basic block is a landing pad,
827 // then don't try to hoist instructions out of this loop.
828 const MachineLoop *ML = MLI->getLoopFor(BB);
829 if (ML && ML->getHeader()->isEHPad())
830 continue;
831
832 // If this subregion is not in the top level loop at all, exit.
833 if (!CurLoop->contains(BB))
834 continue;
835
836 Scopes.push_back(Node);
837 unsigned NumChildren = Node->getNumChildren();
838
839 // Don't hoist things out of a large switch statement. This often causes
840 // code to be hoisted that wasn't going to be executed, and increases
841 // register pressure in a situation where it's likely to matter.
842 if (BB->succ_size() >= 25)
843 NumChildren = 0;
844
845 OpenChildren[Node] = NumChildren;
846 if (NumChildren) {
847 // Add children in reverse order as then the next popped worklist node is
848 // the first child of this node. This means we ultimately traverse the
849 // DOM tree in exactly the same order as if we'd recursed.
850 for (MachineDomTreeNode *Child : reverse(Node->children())) {
851 ParentMap[Child] = Node;
852 WorkList.push_back(Child);
853 }
854 }
855 }
856
857 if (Scopes.size() == 0)
858 return;
859
860 // Compute registers which are livein into the loop headers.
861 RegSeen.clear();
862 BackTrace.clear();
863 InitRegPressure(Preheader);
864
865 // Now perform LICM.
866 for (MachineDomTreeNode *Node : Scopes) {
867 MachineBasicBlock *MBB = Node->getBlock();
868
869 EnterScope(MBB);
870
871 // Process the block
872 SpeculationState = SpeculateUnknown;
873 for (MachineInstr &MI : llvm::make_early_inc_range(*MBB)) {
874 unsigned HoistRes = HoistResult::NotHoisted;
875 HoistRes = Hoist(&MI, Preheader, CurLoop);
876 if (HoistRes & HoistResult::NotHoisted) {
877 // We have failed to hoist MI to outermost loop's preheader. If MI is in
878 // a subloop, try to hoist it to subloop's preheader.
879 SmallVector<MachineLoop *> InnerLoopWorkList;
880 for (MachineLoop *L = MLI->getLoopFor(MI.getParent()); L != CurLoop;
881 L = L->getParentLoop())
882 InnerLoopWorkList.push_back(L);
883
884 while (!InnerLoopWorkList.empty()) {
885 MachineLoop *InnerLoop = InnerLoopWorkList.pop_back_val();
886 MachineBasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader();
887 if (InnerLoopPreheader) {
888 HoistRes = Hoist(&MI, InnerLoopPreheader, InnerLoop);
889 if (HoistRes & HoistResult::Hoisted)
890 break;
891 }
892 }
893 }
894
895 if (HoistRes & HoistResult::ErasedMI)
896 continue;
897
898 UpdateRegPressure(&MI);
899 }
900
901 // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
902 ExitScopeIfDone(Node, OpenChildren, ParentMap);
903 }
904}
905
907 return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
908}
909
910/// Find all virtual register references that are liveout of the preheader to
911/// initialize the starting "register pressure". Note this does not count live
912/// through (livein but not used) registers.
913void MachineLICMImpl::InitRegPressure(MachineBasicBlock *BB) {
914 llvm::fill(RegPressure, 0);
915
916 // If the preheader has only a single predecessor and it ends with a
917 // fallthrough or an unconditional branch, then scan its predecessor for live
918 // defs as well. This happens whenever the preheader is created by splitting
919 // the critical edge from the loop predecessor to the loop header.
920 if (BB->pred_size() == 1) {
921 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
923 if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
924 InitRegPressure(*BB->pred_begin());
925 }
926
927 for (const MachineInstr &MI : *BB)
928 UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
929}
930
931/// Update estimate of register pressure after the specified instruction.
932void MachineLICMImpl::UpdateRegPressure(const MachineInstr *MI,
933 bool ConsiderUnseenAsDef) {
934 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
935 for (const auto &[Class, Weight] : Cost) {
936 if (static_cast<int>(RegPressure[Class]) < -Weight)
937 RegPressure[Class] = 0;
938 else
939 RegPressure[Class] += Weight;
940 }
941}
942
943/// Calculate the additional register pressure that the registers used in MI
944/// cause.
945///
946/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
947/// figure out which usages are live-ins.
948/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
949SmallDenseMap<unsigned, int>
950MachineLICMImpl::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
951 bool ConsiderUnseenAsDef) {
952 SmallDenseMap<unsigned, int> Cost;
953 if (MI->isImplicitDef())
954 return Cost;
955 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
956 const MachineOperand &MO = MI->getOperand(i);
957 if (!MO.isReg() || MO.isImplicit())
958 continue;
959 Register Reg = MO.getReg();
960 if (!Reg.isVirtual())
961 continue;
962
963 // FIXME: It seems bad to use RegSeen only for some of these calculations.
964 bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
965 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
966
967 RegClassWeight W = TRI->getRegClassWeight(RC);
968 int RCCost = 0;
969 if (MO.isDef())
970 RCCost = W.RegWeight;
971 else {
972 bool isKill = isOperandKill(MO, MRI);
973 if (isNew && !isKill && ConsiderUnseenAsDef)
974 // Haven't seen this, it must be a livein.
975 RCCost = W.RegWeight;
976 else if (!isNew && isKill)
977 RCCost = -W.RegWeight;
978 }
979 if (RCCost == 0)
980 continue;
981 const int *PS = TRI->getRegClassPressureSets(RC);
982 for (; *PS != -1; ++PS)
983 Cost[*PS] += RCCost;
984 }
985 return Cost;
986}
987
988/// Return true if this machine instruction loads from global offset table or
989/// constant pool.
991 assert(MI.mayLoad() && "Expected MI that loads!");
992
993 // If we lost memory operands, conservatively assume that the instruction
994 // reads from everything..
995 if (MI.memoperands_empty())
996 return true;
997
998 for (MachineMemOperand *MemOp : MI.memoperands())
999 if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
1000 if (PSV->isGOT() || PSV->isConstantPool())
1001 return true;
1002
1003 return false;
1004}
1005
1006// This function iterates through all the operands of the input store MI and
1007// checks that each register operand statisfies isCallerPreservedPhysReg.
1008// This means, the value being stored and the address where it is being stored
1009// is constant throughout the body of the function (not including prologue and
1010// epilogue). When called with an MI that isn't a store, it returns false.
1011// A future improvement can be to check if the store registers are constant
1012// throughout the loop rather than throughout the funtion.
1014 const TargetRegisterInfo *TRI,
1015 const MachineRegisterInfo *MRI) {
1016
1017 bool FoundCallerPresReg = false;
1018 if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
1019 (MI.getNumOperands() == 0))
1020 return false;
1021
1022 // Check that all register operands are caller-preserved physical registers.
1023 for (const MachineOperand &MO : MI.operands()) {
1024 if (MO.isReg()) {
1025 Register Reg = MO.getReg();
1026 // If operand is a virtual register, check if it comes from a copy of a
1027 // physical register.
1028 if (Reg.isVirtual())
1029 Reg = TRI->lookThruCopyLike(MO.getReg(), MRI);
1030 if (Reg.isVirtual())
1031 return false;
1032 if (!TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *MI.getMF()))
1033 return false;
1034 else
1035 FoundCallerPresReg = true;
1036 } else if (!MO.isImm()) {
1037 return false;
1038 }
1039 }
1040 return FoundCallerPresReg;
1041}
1042
1043// Return true if the input MI is a copy instruction that feeds an invariant
1044// store instruction. This means that the src of the copy has to satisfy
1045// isCallerPreservedPhysReg and atleast one of it's users should satisfy
1046// isInvariantStore.
1048 const MachineRegisterInfo *MRI,
1049 const TargetRegisterInfo *TRI) {
1050
1051 // FIXME: If targets would like to look through instructions that aren't
1052 // pure copies, this can be updated to a query.
1053 if (!MI.isCopy())
1054 return false;
1055
1056 const MachineFunction *MF = MI.getMF();
1057 // Check that we are copying a constant physical register.
1058 Register CopySrcReg = MI.getOperand(1).getReg();
1059 if (CopySrcReg.isVirtual())
1060 return false;
1061
1062 if (!TRI->isCallerPreservedPhysReg(CopySrcReg.asMCReg(), *MF))
1063 return false;
1064
1065 Register CopyDstReg = MI.getOperand(0).getReg();
1066 // Check if any of the uses of the copy are invariant stores.
1067 assert(CopyDstReg.isVirtual() && "copy dst is not a virtual reg");
1068
1069 for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) {
1070 if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI))
1071 return true;
1072 }
1073 return false;
1074}
1075
1076/// Returns true if the instruction may be a suitable candidate for LICM.
1077/// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
1078bool MachineLICMImpl::IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop) {
1079 // Check if it's safe to move the instruction.
1080 bool DontMoveAcrossStore = !HoistConstLoads || !AllowedToHoistLoads[CurLoop];
1081 if ((!I.isSafeToMove(DontMoveAcrossStore)) &&
1083 LLVM_DEBUG(dbgs() << "LICM: Instruction not safe to move.\n");
1084 return false;
1085 }
1086
1087 // If it is a load then check if it is guaranteed to execute by making sure
1088 // that it dominates all exiting blocks. If it doesn't, then there is a path
1089 // out of the loop which does not execute this load, so we can't hoist it.
1090 // Loads from constant memory are safe to speculate, for example indexed load
1091 // from a jump table.
1092 // Stores and side effects are already checked by isSafeToMove.
1093 if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
1094 !IsGuaranteedToExecute(I.getParent(), CurLoop)) {
1095 LLVM_DEBUG(dbgs() << "LICM: Load not guaranteed to execute.\n");
1096 return false;
1097 }
1098
1099 // Convergent attribute has been used on operations that involve inter-thread
1100 // communication which results are implicitly affected by the enclosing
1101 // control flows. It is not safe to hoist or sink such operations across
1102 // control flow.
1103 if (I.isConvergent())
1104 return false;
1105
1106 if (!TII->shouldHoist(I, CurLoop))
1107 return false;
1108
1109 return true;
1110}
1111
1112/// Returns true if the instruction is loop invariant.
1113bool MachineLICMImpl::IsLoopInvariantInst(MachineInstr &I,
1114 MachineLoop *CurLoop) {
1115 if (!IsLICMCandidate(I, CurLoop)) {
1116 LLVM_DEBUG(dbgs() << "LICM: Instruction not a LICM candidate\n");
1117 return false;
1118 }
1119 return CurLoop->isLoopInvariant(I);
1120}
1121
1122/// Return true if the specified instruction is used by a phi node and hoisting
1123/// it could cause a copy to be inserted.
1124bool MachineLICMImpl::HasLoopPHIUse(const MachineInstr *MI,
1125 MachineLoop *CurLoop) {
1127 do {
1128 MI = Work.pop_back_val();
1129 for (const MachineOperand &MO : MI->all_defs()) {
1130 Register Reg = MO.getReg();
1131 if (!Reg.isVirtual())
1132 continue;
1133 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
1134 // A PHI may cause a copy to be inserted.
1135 if (UseMI.isPHI()) {
1136 // A PHI inside the loop causes a copy because the live range of Reg is
1137 // extended across the PHI.
1138 if (CurLoop->contains(&UseMI))
1139 return true;
1140 // A PHI in an exit block can cause a copy to be inserted if the PHI
1141 // has multiple predecessors in the loop with different values.
1142 // For now, approximate by rejecting all exit blocks.
1143 if (isExitBlock(CurLoop, UseMI.getParent()))
1144 return true;
1145 continue;
1146 }
1147 // Look past copies as well.
1148 if (UseMI.isCopy() && CurLoop->contains(&UseMI))
1149 Work.push_back(&UseMI);
1150 }
1151 }
1152 } while (!Work.empty());
1153 return false;
1154}
1155
1156/// Compute operand latency between a def of 'Reg' and an use in the current
1157/// loop, return true if the target considered it high.
1158bool MachineLICMImpl::HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
1159 Register Reg,
1160 MachineLoop *CurLoop) const {
1161 if (MRI->use_nodbg_empty(Reg))
1162 return false;
1163
1164 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
1165 if (UseMI.isCopyLike())
1166 continue;
1167 if (!CurLoop->contains(UseMI.getParent()))
1168 continue;
1169 for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
1170 const MachineOperand &MO = UseMI.getOperand(i);
1171 if (!MO.isReg() || !MO.isUse())
1172 continue;
1173 Register MOReg = MO.getReg();
1174 if (MOReg != Reg)
1175 continue;
1176
1177 if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
1178 return true;
1179 }
1180
1181 // Only look at the first in loop use.
1182 break;
1183 }
1184
1185 return false;
1186}
1187
1188/// Return true if the instruction is marked "cheap" or the operand latency
1189/// between its def and a use is one or less.
1190bool MachineLICMImpl::IsCheapInstruction(MachineInstr &MI) const {
1191 if (TII->isAsCheapAsAMove(MI) || MI.isSubregToReg())
1192 return true;
1193
1194 bool isCheap = false;
1195 unsigned NumDefs = MI.getDesc().getNumDefs();
1196 for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1197 MachineOperand &DefMO = MI.getOperand(i);
1198 if (!DefMO.isReg() || !DefMO.isDef())
1199 continue;
1200 --NumDefs;
1201 Register Reg = DefMO.getReg();
1202 if (Reg.isPhysical())
1203 continue;
1204
1205 if (!TII->hasLowDefLatency(SchedModel, MI, i))
1206 return false;
1207 isCheap = true;
1208 }
1209
1210 return isCheap;
1211}
1212
1213/// Visit BBs from header to current BB, check if hoisting an instruction of the
1214/// given cost matrix can cause high register pressure.
1215bool MachineLICMImpl::CanCauseHighRegPressure(
1216 const SmallDenseMap<unsigned, int> &Cost, bool CheapInstr) {
1217 for (const auto &[Class, Weight] : Cost) {
1218 if (Weight <= 0)
1219 continue;
1220
1221 int Limit = RegLimit[Class];
1222
1223 // Don't hoist cheap instructions if they would increase register pressure,
1224 // even if we're under the limit.
1225 if (CheapInstr && !HoistCheapInsts)
1226 return true;
1227
1228 for (const auto &RP : BackTrace)
1229 if (static_cast<int>(RP[Class]) + Weight >= Limit)
1230 return true;
1231 }
1232
1233 return false;
1234}
1235
1236/// Traverse the back trace from header to the current block and update their
1237/// register pressures to reflect the effect of hoisting MI from the current
1238/// block to the preheader.
1239void MachineLICMImpl::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1240 // First compute the 'cost' of the instruction, i.e. its contribution
1241 // to register pressure.
1242 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1243 /*ConsiderUnseenAsDef=*/false);
1244
1245 // Update register pressure of blocks from loop header to current block.
1246 for (auto &RP : BackTrace)
1247 for (const auto &[Class, Weight] : Cost)
1248 RP[Class] += Weight;
1249}
1250
1251/// Return true if it is potentially profitable to hoist the given loop
1252/// invariant.
1253bool MachineLICMImpl::IsProfitableToHoist(MachineInstr &MI,
1254 MachineLoop *CurLoop) {
1255 if (MI.isImplicitDef())
1256 return true;
1257
1258 // Besides removing computation from the loop, hoisting an instruction has
1259 // these effects:
1260 //
1261 // - The value defined by the instruction becomes live across the entire
1262 // loop. This increases register pressure in the loop.
1263 //
1264 // - If the value is used by a PHI in the loop, a copy will be required for
1265 // lowering the PHI after extending the live range.
1266 //
1267 // - When hoisting the last use of a value in the loop, that value no longer
1268 // needs to be live in the loop. This lowers register pressure in the loop.
1269
1271 return true;
1272
1273 bool CheapInstr = IsCheapInstruction(MI);
1274 bool CreatesCopy = HasLoopPHIUse(&MI, CurLoop);
1275
1276 // Don't hoist a cheap instruction if it would create a copy in the loop.
1277 if (CheapInstr && CreatesCopy) {
1278 LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1279 return false;
1280 }
1281
1282 // Trivially rematerializable instructions should always be hoisted
1283 // providing the register allocator can just pull them down again when needed.
1284 if (TII->isTriviallyReMaterializable(MI))
1285 return true;
1286
1287 // FIXME: If there are long latency loop-invariant instructions inside the
1288 // loop at this point, why didn't the optimizer's LICM hoist them?
1289 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1290 const MachineOperand &MO = MI.getOperand(i);
1291 if (!MO.isReg() || MO.isImplicit())
1292 continue;
1293 Register Reg = MO.getReg();
1294 if (!Reg.isVirtual())
1295 continue;
1296 if (MO.isDef() && HasHighOperandLatency(MI, i, Reg, CurLoop)) {
1297 LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI);
1298 ++NumHighLatency;
1299 return true;
1300 }
1301 }
1302
1303 // Estimate register pressure to determine whether to LICM the instruction.
1304 // In low register pressure situation, we can be more aggressive about
1305 // hoisting. Also, favors hoisting long latency instructions even in
1306 // moderately high pressure situation.
1307 // Cheap instructions will only be hoisted if they don't increase register
1308 // pressure at all.
1309 auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1310 /*ConsiderUnseenAsDef=*/false);
1311
1312 // Visit BBs from header to current BB, if hoisting this doesn't cause
1313 // high register pressure, then it's safe to proceed.
1314 if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1315 LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1316 ++NumLowRP;
1317 return true;
1318 }
1319
1320 // Don't risk increasing register pressure if it would create copies.
1321 if (CreatesCopy) {
1322 LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1323 return false;
1324 }
1325
1326 // Do not "speculate" in high register pressure situation. If an
1327 // instruction is not guaranteed to be executed in the loop, it's best to be
1328 // conservative.
1329 if (AvoidSpeculation &&
1330 (!IsGuaranteedToExecute(MI.getParent(), CurLoop) && !MayCSE(&MI))) {
1331 LLVM_DEBUG(dbgs() << "Won't speculate: " << MI);
1332 return false;
1333 }
1334
1335 // If we have a COPY with other uses in the loop, hoist to allow the users to
1336 // also be hoisted.
1337 // TODO: Handle all isCopyLike?
1338 if (MI.isCopy() || MI.isRegSequence()) {
1339 Register DefReg = MI.getOperand(0).getReg();
1340 if (DefReg.isVirtual() &&
1341 all_of(MI.uses(),
1342 [this](const MachineOperand &UseOp) {
1343 return !UseOp.isReg() || UseOp.getReg().isVirtual() ||
1344 MRI->isConstantPhysReg(UseOp.getReg());
1345 }) &&
1346 IsLoopInvariantInst(MI, CurLoop) &&
1347 any_of(MRI->use_nodbg_instructions(DefReg),
1348 [&CurLoop, this, DefReg,
1349 Cost = std::move(Cost)](MachineInstr &UseMI) {
1350 if (!CurLoop->contains(&UseMI))
1351 return false;
1352
1353 // COPY is a cheap instruction, but if moving it won't cause
1354 // high RP we're fine to hoist it even if the user can't be
1355 // hoisted later Otherwise we want to check the user if it's
1356 // hoistable
1357 if (CanCauseHighRegPressure(Cost, false) &&
1358 !CurLoop->isLoopInvariant(UseMI, DefReg))
1359 return false;
1360
1361 return true;
1362 }))
1363 return true;
1364 }
1365
1366 // High register pressure situation, only hoist if the instruction is going
1367 // to be remat'ed.
1368 if (!TII->isTriviallyReMaterializable(MI) &&
1369 !MI.isDereferenceableInvariantLoad()) {
1370 LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1371 return false;
1372 }
1373
1374 return true;
1375}
1376
1377/// Unfold a load from the given machineinstr if the load itself could be
1378/// hoisted. Return the unfolded and hoistable load, or null if the load
1379/// couldn't be unfolded or if it wouldn't be hoistable.
1380MachineInstr *MachineLICMImpl::ExtractHoistableLoad(MachineInstr *MI,
1381 MachineLoop *CurLoop) {
1382 // Don't unfold simple loads.
1383 if (MI->canFoldAsLoad())
1384 return nullptr;
1385
1386 // If not, we may be able to unfold a load and hoist that.
1387 // First test whether the instruction is loading from an amenable
1388 // memory location.
1389 if (!MI->isDereferenceableInvariantLoad())
1390 return nullptr;
1391
1392 // Next determine the register class for a temporary register.
1393 unsigned LoadRegIndex;
1394 unsigned NewOpc =
1395 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1396 /*UnfoldLoad=*/true,
1397 /*UnfoldStore=*/false,
1398 &LoadRegIndex);
1399 if (NewOpc == 0) return nullptr;
1400 const MCInstrDesc &MID = TII->get(NewOpc);
1401 MachineFunction &MF = *MI->getMF();
1402 const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI);
1403 // Ok, we're unfolding. Create a temporary register and do the unfold.
1404 Register Reg = MRI->createVirtualRegister(RC);
1405
1406 SmallVector<MachineInstr *, 2> NewMIs;
1407 bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
1408 /*UnfoldLoad=*/true,
1409 /*UnfoldStore=*/false, NewMIs);
1410 (void)Success;
1411 assert(Success &&
1412 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1413 "succeeded!");
1414 assert(NewMIs.size() == 2 &&
1415 "Unfolded a load into multiple instructions!");
1416 MachineBasicBlock *MBB = MI->getParent();
1418 MBB->insert(Pos, NewMIs[0]);
1419 MBB->insert(Pos, NewMIs[1]);
1420 // If unfolding produced a load that wasn't loop-invariant or profitable to
1421 // hoist, discard the new instructions and bail.
1422 if (!IsLoopInvariantInst(*NewMIs[0], CurLoop) ||
1423 !IsProfitableToHoist(*NewMIs[0], CurLoop)) {
1424 NewMIs[0]->eraseFromParent();
1425 NewMIs[1]->eraseFromParent();
1426 return nullptr;
1427 }
1428
1429 // Update register pressure for the unfolded instruction.
1430 UpdateRegPressure(NewMIs[1]);
1431
1432 // Otherwise we successfully unfolded a load that we can hoist.
1433
1434 // Update the call info.
1435 if (MI->shouldUpdateAdditionalCallInfo())
1437
1438 MI->eraseFromParent();
1439 return NewMIs[0];
1440}
1441
1442/// Initialize the CSE map with instructions that are in the current loop
1443/// preheader that may become duplicates of instructions that are hoisted
1444/// out of the loop.
1445void MachineLICMImpl::InitCSEMap(MachineBasicBlock *BB) {
1446 for (MachineInstr &MI : *BB)
1447 CSEMap[BB][MI.getOpcode()].push_back(&MI);
1448}
1449
1450/// Initialize AllowedToHoistLoads with information about whether invariant
1451/// loads can be moved outside a given loop
1452void MachineLICMImpl::InitializeLoadsHoistableLoops() {
1453 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
1454 SmallVector<MachineLoop *, 8> LoopsInPreOrder;
1455
1456 // Mark all loops as hoistable initially and prepare a list of loops in
1457 // pre-order DFS.
1458 while (!Worklist.empty()) {
1459 auto *L = Worklist.pop_back_val();
1460 AllowedToHoistLoads[L] = true;
1461 LoopsInPreOrder.push_back(L);
1462 llvm::append_range(Worklist, L->getSubLoops());
1463 }
1464
1465 // Going from the innermost to outermost loops, check if a loop has
1466 // instructions preventing invariant load hoisting. If such instruction is
1467 // found, mark this loop and its parent as non-hoistable and continue
1468 // investigating the next loop.
1469 // Visiting in a reversed pre-ordered DFS manner
1470 // allows us to not process all the instructions of the outer loop if the
1471 // inner loop is proved to be non-load-hoistable.
1472 for (auto *Loop : reverse(LoopsInPreOrder)) {
1473 for (auto *MBB : Loop->blocks()) {
1474 // If this loop has already been marked as non-hoistable, skip it.
1475 if (!AllowedToHoistLoads[Loop])
1476 continue;
1477 for (auto &MI : *MBB) {
1478 if (!MI.isLoadFoldBarrier() && !MI.mayStore() && !MI.isCall() &&
1479 !(MI.mayLoad() && MI.hasOrderedMemoryRef()))
1480 continue;
1481 for (MachineLoop *L = Loop; L != nullptr; L = L->getParentLoop())
1482 AllowedToHoistLoads[L] = false;
1483 break;
1484 }
1485 }
1486 }
1487}
1488
1489/// Find an instruction amount PrevMIs that is a duplicate of MI.
1490/// Return this instruction if it's found.
1491MachineInstr *
1492MachineLICMImpl::LookForDuplicate(const MachineInstr *MI,
1493 std::vector<MachineInstr *> &PrevMIs) {
1494 for (MachineInstr *PrevMI : PrevMIs)
1495 if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
1496 return PrevMI;
1497
1498 return nullptr;
1499}
1500
1501/// Given a LICM'ed instruction, look for an instruction on the preheader that
1502/// computes the same value. If it's found, do a RAU on with the definition of
1503/// the existing instruction rather than hoisting the instruction to the
1504/// preheader.
1505bool MachineLICMImpl::EliminateCSE(
1506 MachineInstr *MI,
1507 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI) {
1508 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1509 // the undef property onto uses.
1510 if (MI->isImplicitDef())
1511 return false;
1512
1513 // Do not CSE normal loads because between them could be store instructions
1514 // that change the loaded value
1515 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1516 return false;
1517
1518 if (MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1519 LLVM_DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1520
1521 // Replace virtual registers defined by MI by their counterparts defined
1522 // by Dup.
1523 SmallVector<unsigned, 2> Defs;
1524 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1525 const MachineOperand &MO = MI->getOperand(i);
1526
1527 // Physical registers may not differ here.
1528 assert((!MO.isReg() || MO.getReg() == 0 || !MO.getReg().isPhysical() ||
1529 MO.getReg() == Dup->getOperand(i).getReg()) &&
1530 "Instructions with different phys regs are not identical!");
1531
1532 if (MO.isReg() && MO.isDef() && !MO.getReg().isPhysical())
1533 Defs.push_back(i);
1534 }
1535
1537 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1538 unsigned Idx = Defs[i];
1539 Register Reg = MI->getOperand(Idx).getReg();
1540 Register DupReg = Dup->getOperand(Idx).getReg();
1541 OrigRCs.push_back(MRI->getRegClass(DupReg));
1542
1543 if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1544 // Restore old RCs if more than one defs.
1545 for (unsigned j = 0; j != i; ++j)
1546 MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1547 return false;
1548 }
1549 }
1550
1551 for (unsigned Idx : Defs) {
1552 Register Reg = MI->getOperand(Idx).getReg();
1553 Register DupReg = Dup->getOperand(Idx).getReg();
1554 MRI->replaceRegWith(Reg, DupReg);
1555 MRI->clearKillFlags(DupReg);
1556 // Clear Dup dead flag if any, we reuse it for Reg.
1557 if (!MRI->use_nodbg_empty(DupReg))
1558 Dup->getOperand(Idx).setIsDead(false);
1559 }
1560
1561 MI->eraseFromParent();
1562 ++NumCSEed;
1563 return true;
1564 }
1565 return false;
1566}
1567
1568/// Return true if the given instruction will be CSE'd if it's hoisted out of
1569/// the loop.
1570bool MachineLICMImpl::MayCSE(MachineInstr *MI) {
1571 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1572 return false;
1573
1574 unsigned Opcode = MI->getOpcode();
1575 for (auto &Map : CSEMap) {
1576 // Check this CSEMap's preheader dominates MI's basic block.
1577 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1578 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1579 Map.second.find(Opcode);
1580 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1581 // the undef property onto uses.
1582 if (CI == Map.second.end() || MI->isImplicitDef())
1583 continue;
1584 if (LookForDuplicate(MI, CI->second) != nullptr)
1585 return true;
1586 }
1587 }
1588
1589 return false;
1590}
1591
1592/// When an instruction is found to use only loop invariant operands
1593/// that are safe to hoist, this instruction is called to do the dirty work.
1594/// It returns true if the instruction is hoisted.
1595unsigned MachineLICMImpl::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
1596 MachineLoop *CurLoop) {
1597 MachineBasicBlock *SrcBlock = MI->getParent();
1598
1599 // Disable the instruction hoisting due to block hotness
1601 (DisableHoistingToHotterBlocks == UseBFI::PGO && HasProfileData)) &&
1602 isTgtHotterThanSrc(SrcBlock, Preheader)) {
1603 ++NumNotHoistedDueToHotness;
1604 return HoistResult::NotHoisted;
1605 }
1606 // First check whether we should hoist this instruction.
1607 bool HasExtractHoistableLoad = false;
1608 if (!IsLoopInvariantInst(*MI, CurLoop) ||
1609 !IsProfitableToHoist(*MI, CurLoop)) {
1610 // If not, try unfolding a hoistable load.
1611 MI = ExtractHoistableLoad(MI, CurLoop);
1612 if (!MI)
1613 return HoistResult::NotHoisted;
1614 HasExtractHoistableLoad = true;
1615 }
1616
1617 // If we have hoisted an instruction that may store, it can only be a constant
1618 // store.
1619 if (MI->mayStore())
1620 NumStoreConst++;
1621
1622 // Now move the instructions to the predecessor, inserting it before any
1623 // terminator instructions.
1624 LLVM_DEBUG({
1625 dbgs() << "Hoisting " << *MI;
1626 if (MI->getParent()->getBasicBlock())
1627 dbgs() << " from " << printMBBReference(*MI->getParent());
1628 if (Preheader->getBasicBlock())
1629 dbgs() << " to " << printMBBReference(*Preheader);
1630 dbgs() << "\n";
1631 });
1632
1633 // If this is the first instruction being hoisted to the preheader,
1634 // initialize the CSE map with potential common expressions.
1635 if (FirstInLoop) {
1636 InitCSEMap(Preheader);
1637 FirstInLoop = false;
1638 }
1639
1640 // Look for opportunity to CSE the hoisted instruction.
1641 unsigned Opcode = MI->getOpcode();
1642 bool HasCSEDone = false;
1643 for (auto &Map : CSEMap) {
1644 // Check this CSEMap's preheader dominates MI's basic block.
1645 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1646 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1647 Map.second.find(Opcode);
1648 if (CI != Map.second.end()) {
1649 if (EliminateCSE(MI, CI)) {
1650 HasCSEDone = true;
1651 break;
1652 }
1653 }
1654 }
1655 }
1656
1657 if (!HasCSEDone) {
1658 // Otherwise, splice the instruction to the preheader.
1659 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1660
1661 // Since we are moving the instruction out of its basic block, we do not
1662 // retain its debug location. Doing so would degrade the debugging
1663 // experience and adversely affect the accuracy of profiling information.
1664 assert(!MI->isDebugInstr() && "Should not hoist debug inst");
1665 MI->setDebugLoc(DebugLoc());
1666
1667 // Update register pressure for BBs from header to this block.
1668 UpdateBackTraceRegPressure(MI);
1669
1670 // Clear the kill flags of any register this instruction defines,
1671 // since they may need to be live throughout the entire loop
1672 // rather than just live for part of it.
1673 for (MachineOperand &MO : MI->all_defs())
1674 if (!MO.isDead())
1675 MRI->clearKillFlags(MO.getReg());
1676
1677 CSEMap[Preheader][Opcode].push_back(MI);
1678 }
1679
1680 ++NumHoisted;
1681 Changed = true;
1682
1683 if (HasCSEDone || HasExtractHoistableLoad)
1684 return HoistResult::Hoisted | HoistResult::ErasedMI;
1685 return HoistResult::Hoisted;
1686}
1687
1688/// Get the preheader for the current loop, splitting a critical edge if needed.
1689MachineBasicBlock *MachineLICMImpl::getOrCreatePreheader(MachineLoop *CurLoop) {
1690 // Determine the block to which to hoist instructions. If we can't find a
1691 // suitable loop predecessor, we can't do any hoisting.
1692 if (MachineBasicBlock *Preheader = CurLoop->getLoopPreheader())
1693 return Preheader;
1694
1695 // Try forming a preheader by splitting the critical edge between the single
1696 // predecessor and the loop header.
1697 if (MachineBasicBlock *Pred = CurLoop->getLoopPredecessor()) {
1698 MachineBasicBlock *NewPreheader = Pred->SplitCriticalEdge(
1699 CurLoop->getHeader(), LegacyPass, MFAM, nullptr, MDTU);
1700 if (NewPreheader)
1701 Changed = true;
1702 return NewPreheader;
1703 }
1704
1705 return nullptr;
1706}
1707
1708/// Is the target basic block at least "BlockFrequencyRatioThreshold"
1709/// times hotter than the source basic block.
1710bool MachineLICMImpl::isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
1711 MachineBasicBlock *TgtBlock) {
1712 // Parse source and target basic block frequency from MBFI
1713 uint64_t SrcBF = MBFI->getBlockFreq(SrcBlock).getFrequency();
1714 uint64_t DstBF = MBFI->getBlockFreq(TgtBlock).getFrequency();
1715
1716 // Disable the hoisting if source block frequency is zero
1717 if (!SrcBF)
1718 return true;
1719
1720 double Ratio = (double)DstBF / SrcBF;
1721
1722 // Compare the block frequency ratio with the threshold
1723 return Ratio > BlockFrequencyRatioThreshold;
1724}
1725
1726template <typename DerivedT, bool PreRegAlloc>
1729 bool Changed = MachineLICMImpl(PreRegAlloc, nullptr, &MFAM).run(MF);
1730 if (!Changed)
1731 return PreservedAnalyses::all();
1733 PA.preserve<MachineLoopAnalysis>();
1734 return PA;
1735}
1736
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:58
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:369
#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:194
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:19
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:102
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
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.
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:1745
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:1725
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:2136
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:1732
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
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
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:1897
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