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