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 &RPIdAndCost : Cost) {
936 unsigned Class = RPIdAndCost.first;
937 if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
938 RegPressure[Class] = 0;
939 else
940 RegPressure[Class] += RPIdAndCost.second;
941 }
942}
943
944/// Calculate the additional register pressure that the registers used in MI
945/// cause.
946///
947/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
948/// figure out which usages are live-ins.
949/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
950SmallDenseMap<unsigned, int>
951MachineLICMImpl::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
952 bool ConsiderUnseenAsDef) {
953 SmallDenseMap<unsigned, int> Cost;
954 if (MI->isImplicitDef())
955 return Cost;
956 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
957 const MachineOperand &MO = MI->getOperand(i);
958 if (!MO.isReg() || MO.isImplicit())
959 continue;
960 Register Reg = MO.getReg();
961 if (!Reg.isVirtual())
962 continue;
963
964 // FIXME: It seems bad to use RegSeen only for some of these calculations.
965 bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
966 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
967
968 RegClassWeight W = TRI->getRegClassWeight(RC);
969 int RCCost = 0;
970 if (MO.isDef())
971 RCCost = W.RegWeight;
972 else {
973 bool isKill = isOperandKill(MO, MRI);
974 if (isNew && !isKill && ConsiderUnseenAsDef)
975 // Haven't seen this, it must be a livein.
976 RCCost = W.RegWeight;
977 else if (!isNew && isKill)
978 RCCost = -W.RegWeight;
979 }
980 if (RCCost == 0)
981 continue;
982 const int *PS = TRI->getRegClassPressureSets(RC);
983 for (; *PS != -1; ++PS)
984 Cost[*PS] += RCCost;
985 }
986 return Cost;
987}
988
989/// Return true if this machine instruction loads from global offset table or
990/// constant pool.
992 assert(MI.mayLoad() && "Expected MI that loads!");
993
994 // If we lost memory operands, conservatively assume that the instruction
995 // reads from everything..
996 if (MI.memoperands_empty())
997 return true;
998
999 for (MachineMemOperand *MemOp : MI.memoperands())
1000 if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
1001 if (PSV->isGOT() || PSV->isConstantPool())
1002 return true;
1003
1004 return false;
1005}
1006
1007// This function iterates through all the operands of the input store MI and
1008// checks that each register operand statisfies isCallerPreservedPhysReg.
1009// This means, the value being stored and the address where it is being stored
1010// is constant throughout the body of the function (not including prologue and
1011// epilogue). When called with an MI that isn't a store, it returns false.
1012// A future improvement can be to check if the store registers are constant
1013// throughout the loop rather than throughout the funtion.
1015 const TargetRegisterInfo *TRI,
1016 const MachineRegisterInfo *MRI) {
1017
1018 bool FoundCallerPresReg = false;
1019 if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
1020 (MI.getNumOperands() == 0))
1021 return false;
1022
1023 // Check that all register operands are caller-preserved physical registers.
1024 for (const MachineOperand &MO : MI.operands()) {
1025 if (MO.isReg()) {
1026 Register Reg = MO.getReg();
1027 // If operand is a virtual register, check if it comes from a copy of a
1028 // physical register.
1029 if (Reg.isVirtual())
1030 Reg = TRI->lookThruCopyLike(MO.getReg(), MRI);
1031 if (Reg.isVirtual())
1032 return false;
1033 if (!TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *MI.getMF()))
1034 return false;
1035 else
1036 FoundCallerPresReg = true;
1037 } else if (!MO.isImm()) {
1038 return false;
1039 }
1040 }
1041 return FoundCallerPresReg;
1042}
1043
1044// Return true if the input MI is a copy instruction that feeds an invariant
1045// store instruction. This means that the src of the copy has to satisfy
1046// isCallerPreservedPhysReg and atleast one of it's users should satisfy
1047// isInvariantStore.
1049 const MachineRegisterInfo *MRI,
1050 const TargetRegisterInfo *TRI) {
1051
1052 // FIXME: If targets would like to look through instructions that aren't
1053 // pure copies, this can be updated to a query.
1054 if (!MI.isCopy())
1055 return false;
1056
1057 const MachineFunction *MF = MI.getMF();
1058 // Check that we are copying a constant physical register.
1059 Register CopySrcReg = MI.getOperand(1).getReg();
1060 if (CopySrcReg.isVirtual())
1061 return false;
1062
1063 if (!TRI->isCallerPreservedPhysReg(CopySrcReg.asMCReg(), *MF))
1064 return false;
1065
1066 Register CopyDstReg = MI.getOperand(0).getReg();
1067 // Check if any of the uses of the copy are invariant stores.
1068 assert(CopyDstReg.isVirtual() && "copy dst is not a virtual reg");
1069
1070 for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) {
1071 if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI))
1072 return true;
1073 }
1074 return false;
1075}
1076
1077/// Returns true if the instruction may be a suitable candidate for LICM.
1078/// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
1079bool MachineLICMImpl::IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop) {
1080 // Check if it's safe to move the instruction.
1081 bool DontMoveAcrossStore = !HoistConstLoads || !AllowedToHoistLoads[CurLoop];
1082 if ((!I.isSafeToMove(DontMoveAcrossStore)) &&
1084 LLVM_DEBUG(dbgs() << "LICM: Instruction not safe to move.\n");
1085 return false;
1086 }
1087
1088 // If it is a load then check if it is guaranteed to execute by making sure
1089 // that it dominates all exiting blocks. If it doesn't, then there is a path
1090 // out of the loop which does not execute this load, so we can't hoist it.
1091 // Loads from constant memory are safe to speculate, for example indexed load
1092 // from a jump table.
1093 // Stores and side effects are already checked by isSafeToMove.
1094 if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
1095 !IsGuaranteedToExecute(I.getParent(), CurLoop)) {
1096 LLVM_DEBUG(dbgs() << "LICM: Load not guaranteed to execute.\n");
1097 return false;
1098 }
1099
1100 // Convergent attribute has been used on operations that involve inter-thread
1101 // communication which results are implicitly affected by the enclosing
1102 // control flows. It is not safe to hoist or sink such operations across
1103 // control flow.
1104 if (I.isConvergent())
1105 return false;
1106
1107 if (!TII->shouldHoist(I, CurLoop))
1108 return false;
1109
1110 return true;
1111}
1112
1113/// Returns true if the instruction is loop invariant.
1114bool MachineLICMImpl::IsLoopInvariantInst(MachineInstr &I,
1115 MachineLoop *CurLoop) {
1116 if (!IsLICMCandidate(I, CurLoop)) {
1117 LLVM_DEBUG(dbgs() << "LICM: Instruction not a LICM candidate\n");
1118 return false;
1119 }
1120 return CurLoop->isLoopInvariant(I);
1121}
1122
1123/// Return true if the specified instruction is used by a phi node and hoisting
1124/// it could cause a copy to be inserted.
1125bool MachineLICMImpl::HasLoopPHIUse(const MachineInstr *MI,
1126 MachineLoop *CurLoop) {
1128 do {
1129 MI = Work.pop_back_val();
1130 for (const MachineOperand &MO : MI->all_defs()) {
1131 Register Reg = MO.getReg();
1132 if (!Reg.isVirtual())
1133 continue;
1134 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
1135 // A PHI may cause a copy to be inserted.
1136 if (UseMI.isPHI()) {
1137 // A PHI inside the loop causes a copy because the live range of Reg is
1138 // extended across the PHI.
1139 if (CurLoop->contains(&UseMI))
1140 return true;
1141 // A PHI in an exit block can cause a copy to be inserted if the PHI
1142 // has multiple predecessors in the loop with different values.
1143 // For now, approximate by rejecting all exit blocks.
1144 if (isExitBlock(CurLoop, UseMI.getParent()))
1145 return true;
1146 continue;
1147 }
1148 // Look past copies as well.
1149 if (UseMI.isCopy() && CurLoop->contains(&UseMI))
1150 Work.push_back(&UseMI);
1151 }
1152 }
1153 } while (!Work.empty());
1154 return false;
1155}
1156
1157/// Compute operand latency between a def of 'Reg' and an use in the current
1158/// loop, return true if the target considered it high.
1159bool MachineLICMImpl::HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
1160 Register Reg,
1161 MachineLoop *CurLoop) const {
1162 if (MRI->use_nodbg_empty(Reg))
1163 return false;
1164
1165 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
1166 if (UseMI.isCopyLike())
1167 continue;
1168 if (!CurLoop->contains(UseMI.getParent()))
1169 continue;
1170 for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
1171 const MachineOperand &MO = UseMI.getOperand(i);
1172 if (!MO.isReg() || !MO.isUse())
1173 continue;
1174 Register MOReg = MO.getReg();
1175 if (MOReg != Reg)
1176 continue;
1177
1178 if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
1179 return true;
1180 }
1181
1182 // Only look at the first in loop use.
1183 break;
1184 }
1185
1186 return false;
1187}
1188
1189/// Return true if the instruction is marked "cheap" or the operand latency
1190/// between its def and a use is one or less.
1191bool MachineLICMImpl::IsCheapInstruction(MachineInstr &MI) const {
1192 if (TII->isAsCheapAsAMove(MI) || MI.isSubregToReg())
1193 return true;
1194
1195 bool isCheap = false;
1196 unsigned NumDefs = MI.getDesc().getNumDefs();
1197 for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1198 MachineOperand &DefMO = MI.getOperand(i);
1199 if (!DefMO.isReg() || !DefMO.isDef())
1200 continue;
1201 --NumDefs;
1202 Register Reg = DefMO.getReg();
1203 if (Reg.isPhysical())
1204 continue;
1205
1206 if (!TII->hasLowDefLatency(SchedModel, MI, i))
1207 return false;
1208 isCheap = true;
1209 }
1210
1211 return isCheap;
1212}
1213
1214/// Visit BBs from header to current BB, check if hoisting an instruction of the
1215/// given cost matrix can cause high register pressure.
1216bool MachineLICMImpl::CanCauseHighRegPressure(
1217 const SmallDenseMap<unsigned, int> &Cost, bool CheapInstr) {
1218 for (const auto &RPIdAndCost : Cost) {
1219 if (RPIdAndCost.second <= 0)
1220 continue;
1221
1222 unsigned Class = RPIdAndCost.first;
1223 int Limit = RegLimit[Class];
1224
1225 // Don't hoist cheap instructions if they would increase register pressure,
1226 // even if we're under the limit.
1227 if (CheapInstr && !HoistCheapInsts)
1228 return true;
1229
1230 for (const auto &RP : BackTrace)
1231 if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
1232 return true;
1233 }
1234
1235 return false;
1236}
1237
1238/// Traverse the back trace from header to the current block and update their
1239/// register pressures to reflect the effect of hoisting MI from the current
1240/// block to the preheader.
1241void MachineLICMImpl::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1242 // First compute the 'cost' of the instruction, i.e. its contribution
1243 // to register pressure.
1244 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1245 /*ConsiderUnseenAsDef=*/false);
1246
1247 // Update register pressure of blocks from loop header to current block.
1248 for (auto &RP : BackTrace)
1249 for (const auto &RPIdAndCost : Cost)
1250 RP[RPIdAndCost.first] += RPIdAndCost.second;
1251}
1252
1253/// Return true if it is potentially profitable to hoist the given loop
1254/// invariant.
1255bool MachineLICMImpl::IsProfitableToHoist(MachineInstr &MI,
1256 MachineLoop *CurLoop) {
1257 if (MI.isImplicitDef())
1258 return true;
1259
1260 // Besides removing computation from the loop, hoisting an instruction has
1261 // these effects:
1262 //
1263 // - The value defined by the instruction becomes live across the entire
1264 // loop. This increases register pressure in the loop.
1265 //
1266 // - If the value is used by a PHI in the loop, a copy will be required for
1267 // lowering the PHI after extending the live range.
1268 //
1269 // - When hoisting the last use of a value in the loop, that value no longer
1270 // needs to be live in the loop. This lowers register pressure in the loop.
1271
1273 return true;
1274
1275 bool CheapInstr = IsCheapInstruction(MI);
1276 bool CreatesCopy = HasLoopPHIUse(&MI, CurLoop);
1277
1278 // Don't hoist a cheap instruction if it would create a copy in the loop.
1279 if (CheapInstr && CreatesCopy) {
1280 LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1281 return false;
1282 }
1283
1284 // Trivially rematerializable instructions should always be hoisted
1285 // providing the register allocator can just pull them down again when needed.
1286 if (TII->isTriviallyReMaterializable(MI))
1287 return true;
1288
1289 // FIXME: If there are long latency loop-invariant instructions inside the
1290 // loop at this point, why didn't the optimizer's LICM hoist them?
1291 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1292 const MachineOperand &MO = MI.getOperand(i);
1293 if (!MO.isReg() || MO.isImplicit())
1294 continue;
1295 Register Reg = MO.getReg();
1296 if (!Reg.isVirtual())
1297 continue;
1298 if (MO.isDef() && HasHighOperandLatency(MI, i, Reg, CurLoop)) {
1299 LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI);
1300 ++NumHighLatency;
1301 return true;
1302 }
1303 }
1304
1305 // Estimate register pressure to determine whether to LICM the instruction.
1306 // In low register pressure situation, we can be more aggressive about
1307 // hoisting. Also, favors hoisting long latency instructions even in
1308 // moderately high pressure situation.
1309 // Cheap instructions will only be hoisted if they don't increase register
1310 // pressure at all.
1311 auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1312 /*ConsiderUnseenAsDef=*/false);
1313
1314 // Visit BBs from header to current BB, if hoisting this doesn't cause
1315 // high register pressure, then it's safe to proceed.
1316 if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1317 LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1318 ++NumLowRP;
1319 return true;
1320 }
1321
1322 // Don't risk increasing register pressure if it would create copies.
1323 if (CreatesCopy) {
1324 LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1325 return false;
1326 }
1327
1328 // Do not "speculate" in high register pressure situation. If an
1329 // instruction is not guaranteed to be executed in the loop, it's best to be
1330 // conservative.
1331 if (AvoidSpeculation &&
1332 (!IsGuaranteedToExecute(MI.getParent(), CurLoop) && !MayCSE(&MI))) {
1333 LLVM_DEBUG(dbgs() << "Won't speculate: " << MI);
1334 return false;
1335 }
1336
1337 // If we have a COPY with other uses in the loop, hoist to allow the users to
1338 // also be hoisted.
1339 // TODO: Handle all isCopyLike?
1340 if (MI.isCopy() || MI.isRegSequence()) {
1341 Register DefReg = MI.getOperand(0).getReg();
1342 if (DefReg.isVirtual() &&
1343 all_of(MI.uses(),
1344 [this](const MachineOperand &UseOp) {
1345 return !UseOp.isReg() || UseOp.getReg().isVirtual() ||
1346 MRI->isConstantPhysReg(UseOp.getReg());
1347 }) &&
1348 IsLoopInvariantInst(MI, CurLoop) &&
1349 any_of(MRI->use_nodbg_instructions(DefReg),
1350 [&CurLoop, this, DefReg,
1351 Cost = std::move(Cost)](MachineInstr &UseMI) {
1352 if (!CurLoop->contains(&UseMI))
1353 return false;
1354
1355 // COPY is a cheap instruction, but if moving it won't cause
1356 // high RP we're fine to hoist it even if the user can't be
1357 // hoisted later Otherwise we want to check the user if it's
1358 // hoistable
1359 if (CanCauseHighRegPressure(Cost, false) &&
1360 !CurLoop->isLoopInvariant(UseMI, DefReg))
1361 return false;
1362
1363 return true;
1364 }))
1365 return true;
1366 }
1367
1368 // High register pressure situation, only hoist if the instruction is going
1369 // to be remat'ed.
1370 if (!TII->isTriviallyReMaterializable(MI) &&
1371 !MI.isDereferenceableInvariantLoad()) {
1372 LLVM_DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1373 return false;
1374 }
1375
1376 return true;
1377}
1378
1379/// Unfold a load from the given machineinstr if the load itself could be
1380/// hoisted. Return the unfolded and hoistable load, or null if the load
1381/// couldn't be unfolded or if it wouldn't be hoistable.
1382MachineInstr *MachineLICMImpl::ExtractHoistableLoad(MachineInstr *MI,
1383 MachineLoop *CurLoop) {
1384 // Don't unfold simple loads.
1385 if (MI->canFoldAsLoad())
1386 return nullptr;
1387
1388 // If not, we may be able to unfold a load and hoist that.
1389 // First test whether the instruction is loading from an amenable
1390 // memory location.
1391 if (!MI->isDereferenceableInvariantLoad())
1392 return nullptr;
1393
1394 // Next determine the register class for a temporary register.
1395 unsigned LoadRegIndex;
1396 unsigned NewOpc =
1397 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1398 /*UnfoldLoad=*/true,
1399 /*UnfoldStore=*/false,
1400 &LoadRegIndex);
1401 if (NewOpc == 0) return nullptr;
1402 const MCInstrDesc &MID = TII->get(NewOpc);
1403 MachineFunction &MF = *MI->getMF();
1404 const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI);
1405 // Ok, we're unfolding. Create a temporary register and do the unfold.
1406 Register Reg = MRI->createVirtualRegister(RC);
1407
1408 SmallVector<MachineInstr *, 2> NewMIs;
1409 bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
1410 /*UnfoldLoad=*/true,
1411 /*UnfoldStore=*/false, NewMIs);
1412 (void)Success;
1413 assert(Success &&
1414 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1415 "succeeded!");
1416 assert(NewMIs.size() == 2 &&
1417 "Unfolded a load into multiple instructions!");
1418 MachineBasicBlock *MBB = MI->getParent();
1420 MBB->insert(Pos, NewMIs[0]);
1421 MBB->insert(Pos, NewMIs[1]);
1422 // If unfolding produced a load that wasn't loop-invariant or profitable to
1423 // hoist, discard the new instructions and bail.
1424 if (!IsLoopInvariantInst(*NewMIs[0], CurLoop) ||
1425 !IsProfitableToHoist(*NewMIs[0], CurLoop)) {
1426 NewMIs[0]->eraseFromParent();
1427 NewMIs[1]->eraseFromParent();
1428 return nullptr;
1429 }
1430
1431 // Update register pressure for the unfolded instruction.
1432 UpdateRegPressure(NewMIs[1]);
1433
1434 // Otherwise we successfully unfolded a load that we can hoist.
1435
1436 // Update the call info.
1437 if (MI->shouldUpdateAdditionalCallInfo())
1439
1440 MI->eraseFromParent();
1441 return NewMIs[0];
1442}
1443
1444/// Initialize the CSE map with instructions that are in the current loop
1445/// preheader that may become duplicates of instructions that are hoisted
1446/// out of the loop.
1447void MachineLICMImpl::InitCSEMap(MachineBasicBlock *BB) {
1448 for (MachineInstr &MI : *BB)
1449 CSEMap[BB][MI.getOpcode()].push_back(&MI);
1450}
1451
1452/// Initialize AllowedToHoistLoads with information about whether invariant
1453/// loads can be moved outside a given loop
1454void MachineLICMImpl::InitializeLoadsHoistableLoops() {
1455 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
1456 SmallVector<MachineLoop *, 8> LoopsInPreOrder;
1457
1458 // Mark all loops as hoistable initially and prepare a list of loops in
1459 // pre-order DFS.
1460 while (!Worklist.empty()) {
1461 auto *L = Worklist.pop_back_val();
1462 AllowedToHoistLoads[L] = true;
1463 LoopsInPreOrder.push_back(L);
1464 llvm::append_range(Worklist, L->getSubLoops());
1465 }
1466
1467 // Going from the innermost to outermost loops, check if a loop has
1468 // instructions preventing invariant load hoisting. If such instruction is
1469 // found, mark this loop and its parent as non-hoistable and continue
1470 // investigating the next loop.
1471 // Visiting in a reversed pre-ordered DFS manner
1472 // allows us to not process all the instructions of the outer loop if the
1473 // inner loop is proved to be non-load-hoistable.
1474 for (auto *Loop : reverse(LoopsInPreOrder)) {
1475 for (auto *MBB : Loop->blocks()) {
1476 // If this loop has already been marked as non-hoistable, skip it.
1477 if (!AllowedToHoistLoads[Loop])
1478 continue;
1479 for (auto &MI : *MBB) {
1480 if (!MI.isLoadFoldBarrier() && !MI.mayStore() && !MI.isCall() &&
1481 !(MI.mayLoad() && MI.hasOrderedMemoryRef()))
1482 continue;
1483 for (MachineLoop *L = Loop; L != nullptr; L = L->getParentLoop())
1484 AllowedToHoistLoads[L] = false;
1485 break;
1486 }
1487 }
1488 }
1489}
1490
1491/// Find an instruction amount PrevMIs that is a duplicate of MI.
1492/// Return this instruction if it's found.
1493MachineInstr *
1494MachineLICMImpl::LookForDuplicate(const MachineInstr *MI,
1495 std::vector<MachineInstr *> &PrevMIs) {
1496 for (MachineInstr *PrevMI : PrevMIs)
1497 if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
1498 return PrevMI;
1499
1500 return nullptr;
1501}
1502
1503/// Given a LICM'ed instruction, look for an instruction on the preheader that
1504/// computes the same value. If it's found, do a RAU on with the definition of
1505/// the existing instruction rather than hoisting the instruction to the
1506/// preheader.
1507bool MachineLICMImpl::EliminateCSE(
1508 MachineInstr *MI,
1509 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator &CI) {
1510 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1511 // the undef property onto uses.
1512 if (MI->isImplicitDef())
1513 return false;
1514
1515 // Do not CSE normal loads because between them could be store instructions
1516 // that change the loaded value
1517 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1518 return false;
1519
1520 if (MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1521 LLVM_DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1522
1523 // Replace virtual registers defined by MI by their counterparts defined
1524 // by Dup.
1525 SmallVector<unsigned, 2> Defs;
1526 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1527 const MachineOperand &MO = MI->getOperand(i);
1528
1529 // Physical registers may not differ here.
1530 assert((!MO.isReg() || MO.getReg() == 0 || !MO.getReg().isPhysical() ||
1531 MO.getReg() == Dup->getOperand(i).getReg()) &&
1532 "Instructions with different phys regs are not identical!");
1533
1534 if (MO.isReg() && MO.isDef() && !MO.getReg().isPhysical())
1535 Defs.push_back(i);
1536 }
1537
1539 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1540 unsigned Idx = Defs[i];
1541 Register Reg = MI->getOperand(Idx).getReg();
1542 Register DupReg = Dup->getOperand(Idx).getReg();
1543 OrigRCs.push_back(MRI->getRegClass(DupReg));
1544
1545 if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1546 // Restore old RCs if more than one defs.
1547 for (unsigned j = 0; j != i; ++j)
1548 MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1549 return false;
1550 }
1551 }
1552
1553 for (unsigned Idx : Defs) {
1554 Register Reg = MI->getOperand(Idx).getReg();
1555 Register DupReg = Dup->getOperand(Idx).getReg();
1556 MRI->replaceRegWith(Reg, DupReg);
1557 MRI->clearKillFlags(DupReg);
1558 // Clear Dup dead flag if any, we reuse it for Reg.
1559 if (!MRI->use_nodbg_empty(DupReg))
1560 Dup->getOperand(Idx).setIsDead(false);
1561 }
1562
1563 MI->eraseFromParent();
1564 ++NumCSEed;
1565 return true;
1566 }
1567 return false;
1568}
1569
1570/// Return true if the given instruction will be CSE'd if it's hoisted out of
1571/// the loop.
1572bool MachineLICMImpl::MayCSE(MachineInstr *MI) {
1573 if (MI->mayLoad() && !MI->isDereferenceableInvariantLoad())
1574 return false;
1575
1576 unsigned Opcode = MI->getOpcode();
1577 for (auto &Map : CSEMap) {
1578 // Check this CSEMap's preheader dominates MI's basic block.
1579 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1580 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1581 Map.second.find(Opcode);
1582 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1583 // the undef property onto uses.
1584 if (CI == Map.second.end() || MI->isImplicitDef())
1585 continue;
1586 if (LookForDuplicate(MI, CI->second) != nullptr)
1587 return true;
1588 }
1589 }
1590
1591 return false;
1592}
1593
1594/// When an instruction is found to use only loop invariant operands
1595/// that are safe to hoist, this instruction is called to do the dirty work.
1596/// It returns true if the instruction is hoisted.
1597unsigned MachineLICMImpl::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader,
1598 MachineLoop *CurLoop) {
1599 MachineBasicBlock *SrcBlock = MI->getParent();
1600
1601 // Disable the instruction hoisting due to block hotness
1603 (DisableHoistingToHotterBlocks == UseBFI::PGO && HasProfileData)) &&
1604 isTgtHotterThanSrc(SrcBlock, Preheader)) {
1605 ++NumNotHoistedDueToHotness;
1606 return HoistResult::NotHoisted;
1607 }
1608 // First check whether we should hoist this instruction.
1609 bool HasExtractHoistableLoad = false;
1610 if (!IsLoopInvariantInst(*MI, CurLoop) ||
1611 !IsProfitableToHoist(*MI, CurLoop)) {
1612 // If not, try unfolding a hoistable load.
1613 MI = ExtractHoistableLoad(MI, CurLoop);
1614 if (!MI)
1615 return HoistResult::NotHoisted;
1616 HasExtractHoistableLoad = true;
1617 }
1618
1619 // If we have hoisted an instruction that may store, it can only be a constant
1620 // store.
1621 if (MI->mayStore())
1622 NumStoreConst++;
1623
1624 // Now move the instructions to the predecessor, inserting it before any
1625 // terminator instructions.
1626 LLVM_DEBUG({
1627 dbgs() << "Hoisting " << *MI;
1628 if (MI->getParent()->getBasicBlock())
1629 dbgs() << " from " << printMBBReference(*MI->getParent());
1630 if (Preheader->getBasicBlock())
1631 dbgs() << " to " << printMBBReference(*Preheader);
1632 dbgs() << "\n";
1633 });
1634
1635 // If this is the first instruction being hoisted to the preheader,
1636 // initialize the CSE map with potential common expressions.
1637 if (FirstInLoop) {
1638 InitCSEMap(Preheader);
1639 FirstInLoop = false;
1640 }
1641
1642 // Look for opportunity to CSE the hoisted instruction.
1643 unsigned Opcode = MI->getOpcode();
1644 bool HasCSEDone = false;
1645 for (auto &Map : CSEMap) {
1646 // Check this CSEMap's preheader dominates MI's basic block.
1647 if (MDTU->getDomTree().dominates(Map.first, MI->getParent())) {
1648 DenseMap<unsigned, std::vector<MachineInstr *>>::iterator CI =
1649 Map.second.find(Opcode);
1650 if (CI != Map.second.end()) {
1651 if (EliminateCSE(MI, CI)) {
1652 HasCSEDone = true;
1653 break;
1654 }
1655 }
1656 }
1657 }
1658
1659 if (!HasCSEDone) {
1660 // Otherwise, splice the instruction to the preheader.
1661 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1662
1663 // Since we are moving the instruction out of its basic block, we do not
1664 // retain its debug location. Doing so would degrade the debugging
1665 // experience and adversely affect the accuracy of profiling information.
1666 assert(!MI->isDebugInstr() && "Should not hoist debug inst");
1667 MI->setDebugLoc(DebugLoc());
1668
1669 // Update register pressure for BBs from header to this block.
1670 UpdateBackTraceRegPressure(MI);
1671
1672 // Clear the kill flags of any register this instruction defines,
1673 // since they may need to be live throughout the entire loop
1674 // rather than just live for part of it.
1675 for (MachineOperand &MO : MI->all_defs())
1676 if (!MO.isDead())
1677 MRI->clearKillFlags(MO.getReg());
1678
1679 CSEMap[Preheader][Opcode].push_back(MI);
1680 }
1681
1682 ++NumHoisted;
1683 Changed = true;
1684
1685 if (HasCSEDone || HasExtractHoistableLoad)
1686 return HoistResult::Hoisted | HoistResult::ErasedMI;
1687 return HoistResult::Hoisted;
1688}
1689
1690/// Get the preheader for the current loop, splitting a critical edge if needed.
1691MachineBasicBlock *MachineLICMImpl::getOrCreatePreheader(MachineLoop *CurLoop) {
1692 // Determine the block to which to hoist instructions. If we can't find a
1693 // suitable loop predecessor, we can't do any hoisting.
1694 if (MachineBasicBlock *Preheader = CurLoop->getLoopPreheader())
1695 return Preheader;
1696
1697 // Try forming a preheader by splitting the critical edge between the single
1698 // predecessor and the loop header.
1699 if (MachineBasicBlock *Pred = CurLoop->getLoopPredecessor()) {
1700 MachineBasicBlock *NewPreheader = Pred->SplitCriticalEdge(
1701 CurLoop->getHeader(), LegacyPass, MFAM, nullptr, MDTU);
1702 if (NewPreheader)
1703 Changed = true;
1704 return NewPreheader;
1705 }
1706
1707 return nullptr;
1708}
1709
1710/// Is the target basic block at least "BlockFrequencyRatioThreshold"
1711/// times hotter than the source basic block.
1712bool MachineLICMImpl::isTgtHotterThanSrc(MachineBasicBlock *SrcBlock,
1713 MachineBasicBlock *TgtBlock) {
1714 // Parse source and target basic block frequency from MBFI
1715 uint64_t SrcBF = MBFI->getBlockFreq(SrcBlock).getFrequency();
1716 uint64_t DstBF = MBFI->getBlockFreq(TgtBlock).getFrequency();
1717
1718 // Disable the hoisting if source block frequency is zero
1719 if (!SrcBF)
1720 return true;
1721
1722 double Ratio = (double)DstBF / SrcBF;
1723
1724 // Compare the block frequency ratio with the threshold
1725 return Ratio > BlockFrequencyRatioThreshold;
1726}
1727
1728template <typename DerivedT, bool PreRegAlloc>
1731 bool Changed = MachineLICMImpl(PreRegAlloc, nullptr, &MFAM).run(MF);
1732 if (!Changed)
1733 return PreservedAnalyses::all();
1735 PA.preserve<MachineLoopAnalysis>();
1736 return PA;
1737}
1738
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
#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.
Shrink Wrap Pass
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:1725
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:1705
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
LLVM_ABI void initializeEarlyMachineLICMPass(PassRegistry &)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
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:634
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:1712
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
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:1877
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