LLVM 20.0.0git
ShrinkWrap.cpp
Go to the documentation of this file.
1//===- ShrinkWrap.cpp - Compute safe point for prolog/epilog insertion ----===//
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 looks for safe point where the prologue and epilogue can be
10// inserted.
11// The safe point for the prologue (resp. epilogue) is called Save
12// (resp. Restore).
13// A point is safe for prologue (resp. epilogue) if and only if
14// it 1) dominates (resp. post-dominates) all the frame related operations and
15// between 2) two executions of the Save (resp. Restore) point there is an
16// execution of the Restore (resp. Save) point.
17//
18// For instance, the following points are safe:
19// for (int i = 0; i < 10; ++i) {
20// Save
21// ...
22// Restore
23// }
24// Indeed, the execution looks like Save -> Restore -> Save -> Restore ...
25// And the following points are not:
26// for (int i = 0; i < 10; ++i) {
27// Save
28// ...
29// }
30// for (int i = 0; i < 10; ++i) {
31// ...
32// Restore
33// }
34// Indeed, the execution looks like Save -> Save -> ... -> Restore -> Restore.
35//
36// This pass also ensures that the safe points are 3) cheaper than the regular
37// entry and exits blocks.
38//
39// Property #1 is ensured via the use of MachineDominatorTree and
40// MachinePostDominatorTree.
41// Property #2 is ensured via property #1 and MachineLoopInfo, i.e., both
42// points must be in the same loop.
43// Property #3 is ensured via the MachineBlockFrequencyInfo.
44//
45// If this pass found points matching all these properties, then
46// MachineFrameInfo is updated with this information.
47//
48//===----------------------------------------------------------------------===//
49
50#include "llvm/ADT/BitVector.h"
52#include "llvm/ADT/SetVector.h"
54#include "llvm/ADT/Statistic.h"
55#include "llvm/Analysis/CFG.h"
75#include "llvm/IR/Attributes.h"
76#include "llvm/IR/Function.h"
78#include "llvm/MC/MCAsmInfo.h"
79#include "llvm/Pass.h"
81#include "llvm/Support/Debug.h"
85#include <cassert>
86#include <cstdint>
87#include <memory>
88
89using namespace llvm;
90
91#define DEBUG_TYPE "shrink-wrap"
92
93STATISTIC(NumFunc, "Number of functions");
94STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
95STATISTIC(NumCandidatesDropped,
96 "Number of shrink-wrapping candidates dropped because of frequency");
97
99EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
100 cl::desc("enable the shrink-wrapping pass"));
102 "enable-shrink-wrap-region-split", cl::init(true), cl::Hidden,
103 cl::desc("enable splitting of the restore block if possible"));
104
105namespace {
106
107/// Class to determine where the safe point to insert the
108/// prologue and epilogue are.
109/// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
110/// shrink-wrapping term for prologue/epilogue placement, this pass
111/// does not rely on expensive data-flow analysis. Instead we use the
112/// dominance properties and loop information to decide which point
113/// are safe for such insertion.
114class ShrinkWrap : public MachineFunctionPass {
115 /// Hold callee-saved information.
117 MachineDominatorTree *MDT = nullptr;
118 MachinePostDominatorTree *MPDT = nullptr;
119
120 /// Current safe point found for the prologue.
121 /// The prologue will be inserted before the first instruction
122 /// in this basic block.
123 MachineBasicBlock *Save = nullptr;
124
125 /// Current safe point found for the epilogue.
126 /// The epilogue will be inserted before the first terminator instruction
127 /// in this basic block.
128 MachineBasicBlock *Restore = nullptr;
129
130 /// Hold the information of the basic block frequency.
131 /// Use to check the profitability of the new points.
132 MachineBlockFrequencyInfo *MBFI = nullptr;
133
134 /// Hold the loop information. Used to determine if Save and Restore
135 /// are in the same loop.
136 MachineLoopInfo *MLI = nullptr;
137
138 // Emit remarks.
140
141 /// Frequency of the Entry block.
142 BlockFrequency EntryFreq;
143
144 /// Current opcode for frame setup.
145 unsigned FrameSetupOpcode = ~0u;
146
147 /// Current opcode for frame destroy.
148 unsigned FrameDestroyOpcode = ~0u;
149
150 /// Stack pointer register, used by llvm.{savestack,restorestack}
151 Register SP;
152
153 /// Entry block.
154 const MachineBasicBlock *Entry = nullptr;
155
156 using SetOfRegs = SmallSetVector<unsigned, 16>;
157
158 /// Registers that need to be saved for the current function.
159 mutable SetOfRegs CurrentCSRs;
160
161 /// Current MachineFunction.
162 MachineFunction *MachineFunc = nullptr;
163
164 /// Is `true` for the block numbers where we assume possible stack accesses
165 /// or computation of stack-relative addresses on any CFG path including the
166 /// block itself. Is `false` for basic blocks where we can guarantee the
167 /// opposite. False positives won't lead to incorrect analysis results,
168 /// therefore this approach is fair.
169 BitVector StackAddressUsedBlockInfo;
170
171 /// Check if \p MI uses or defines a callee-saved register or
172 /// a frame index. If this is the case, this means \p MI must happen
173 /// after Save and before Restore.
174 bool useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
175 bool StackAddressUsed) const;
176
177 const SetOfRegs &getCurrentCSRs(RegScavenger *RS) const {
178 if (CurrentCSRs.empty()) {
179 BitVector SavedRegs;
180 const TargetFrameLowering *TFI =
181 MachineFunc->getSubtarget().getFrameLowering();
182
183 TFI->determineCalleeSaves(*MachineFunc, SavedRegs, RS);
184
185 for (int Reg = SavedRegs.find_first(); Reg != -1;
186 Reg = SavedRegs.find_next(Reg))
187 CurrentCSRs.insert((unsigned)Reg);
188 }
189 return CurrentCSRs;
190 }
191
192 /// Update the Save and Restore points such that \p MBB is in
193 /// the region that is dominated by Save and post-dominated by Restore
194 /// and Save and Restore still match the safe point definition.
195 /// Such point may not exist and Save and/or Restore may be null after
196 /// this call.
197 void updateSaveRestorePoints(MachineBasicBlock &MBB, RegScavenger *RS);
198
199 // Try to find safe point based on dominance and block frequency without
200 // any change in IR.
201 bool performShrinkWrapping(
203 RegScavenger *RS);
204
205 /// This function tries to split the restore point if doing so can shrink the
206 /// save point further. \return True if restore point is split.
207 bool postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
208 RegScavenger *RS);
209
210 /// This function analyzes if the restore point can split to create a new
211 /// restore point. This function collects
212 /// 1. Any preds of current restore that are reachable by callee save/FI
213 /// blocks
214 /// - indicated by DirtyPreds
215 /// 2. Any preds of current restore that are not DirtyPreds - indicated by
216 /// CleanPreds
217 /// Both sets should be non-empty for considering restore point split.
218 bool checkIfRestoreSplittable(
219 const MachineBasicBlock *CurRestore,
220 const DenseSet<const MachineBasicBlock *> &ReachableByDirty,
223 const TargetInstrInfo *TII, RegScavenger *RS);
224
225 /// Initialize the pass for \p MF.
226 void init(MachineFunction &MF) {
227 RCI.runOnMachineFunction(MF);
228 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
229 MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
230 Save = nullptr;
231 Restore = nullptr;
232 MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
233 MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
234 ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
235 EntryFreq = MBFI->getEntryFreq();
236 const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
237 const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
238 FrameSetupOpcode = TII.getCallFrameSetupOpcode();
239 FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
241 Entry = &MF.front();
242 CurrentCSRs.clear();
243 MachineFunc = &MF;
244
245 ++NumFunc;
246 }
247
248 /// Check whether or not Save and Restore points are still interesting for
249 /// shrink-wrapping.
250 bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
251
252 /// Check if shrink wrapping is enabled for this target and function.
253 static bool isShrinkWrapEnabled(const MachineFunction &MF);
254
255public:
256 static char ID;
257
258 ShrinkWrap() : MachineFunctionPass(ID) {
260 }
261
262 void getAnalysisUsage(AnalysisUsage &AU) const override {
263 AU.setPreservesAll();
270 }
271
274 MachineFunctionProperties::Property::NoVRegs);
275 }
276
277 StringRef getPassName() const override { return "Shrink Wrapping analysis"; }
278
279 /// Perform the shrink-wrapping analysis and update
280 /// the MachineFrameInfo attached to \p MF with the results.
281 bool runOnMachineFunction(MachineFunction &MF) override;
282};
283
284} // end anonymous namespace
285
286char ShrinkWrap::ID = 0;
287
288char &llvm::ShrinkWrapID = ShrinkWrap::ID;
289
290INITIALIZE_PASS_BEGIN(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
296INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
297
298bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
299 bool StackAddressUsed) const {
300 /// Check if \p Op is known to access an address not on the function's stack .
301 /// At the moment, accesses where the underlying object is a global, function
302 /// argument, or jump table are considered non-stack accesses. Note that the
303 /// caller's stack may get accessed when passing an argument via the stack,
304 /// but not the stack of the current function.
305 ///
306 auto IsKnownNonStackPtr = [](MachineMemOperand *Op) {
307 if (Op->getValue()) {
308 const Value *UO = getUnderlyingObject(Op->getValue());
309 if (!UO)
310 return false;
311 if (auto *Arg = dyn_cast<Argument>(UO))
312 return !Arg->hasPassPointeeByValueCopyAttr();
313 return isa<GlobalValue>(UO);
314 }
315 if (const PseudoSourceValue *PSV = Op->getPseudoValue())
316 return PSV->isJumpTable();
317 return false;
318 };
319 // Load/store operations may access the stack indirectly when we previously
320 // computed an address to a stack location.
321 if (StackAddressUsed && MI.mayLoadOrStore() &&
322 (MI.isCall() || MI.hasUnmodeledSideEffects() || MI.memoperands_empty() ||
323 !all_of(MI.memoperands(), IsKnownNonStackPtr)))
324 return true;
325
326 if (MI.getOpcode() == FrameSetupOpcode ||
327 MI.getOpcode() == FrameDestroyOpcode) {
328 LLVM_DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
329 return true;
330 }
331 const MachineFunction *MF = MI.getParent()->getParent();
333 for (const MachineOperand &MO : MI.operands()) {
334 bool UseOrDefCSR = false;
335 if (MO.isReg()) {
336 // Ignore instructions like DBG_VALUE which don't read/def the register.
337 if (!MO.isDef() && !MO.readsReg())
338 continue;
339 Register PhysReg = MO.getReg();
340 if (!PhysReg)
341 continue;
342 assert(PhysReg.isPhysical() && "Unallocated register?!");
343 // The stack pointer is not normally described as a callee-saved register
344 // in calling convention definitions, so we need to watch for it
345 // separately. An SP mentioned by a call instruction, we can ignore,
346 // though, as it's harmless and we do not want to effectively disable tail
347 // calls by forcing the restore point to post-dominate them.
348 // PPC's LR is also not normally described as a callee-saved register in
349 // calling convention definitions, so we need to watch for it, too. An LR
350 // mentioned implicitly by a return (or "branch to link register")
351 // instruction we can ignore, otherwise we may pessimize shrinkwrapping.
352 UseOrDefCSR =
353 (!MI.isCall() && PhysReg == SP) ||
354 RCI.getLastCalleeSavedAlias(PhysReg) ||
355 (!MI.isReturn() && TRI->isNonallocatableRegisterCalleeSave(PhysReg));
356 } else if (MO.isRegMask()) {
357 // Check if this regmask clobbers any of the CSRs.
358 for (unsigned Reg : getCurrentCSRs(RS)) {
359 if (MO.clobbersPhysReg(Reg)) {
360 UseOrDefCSR = true;
361 break;
362 }
363 }
364 }
365 // Skip FrameIndex operands in DBG_VALUE instructions.
366 if (UseOrDefCSR || (MO.isFI() && !MI.isDebugValue())) {
367 LLVM_DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
368 << MO.isFI() << "): " << MI << '\n');
369 return true;
370 }
371 }
372 return false;
373}
374
375/// Helper function to find the immediate (post) dominator.
376template <typename ListOfBBs, typename DominanceAnalysis>
378 DominanceAnalysis &Dom, bool Strict = true) {
379 MachineBasicBlock *IDom = &Block;
380 for (MachineBasicBlock *BB : BBs) {
381 IDom = Dom.findNearestCommonDominator(IDom, BB);
382 if (!IDom)
383 break;
384 }
385 if (Strict && IDom == &Block)
386 return nullptr;
387 return IDom;
388}
389
391 MachineBasicBlock &Entry) {
392 // Check if the block is analyzable.
393 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
395 return !TII.analyzeBranch(Entry, TBB, FBB, Cond);
396}
397
398/// Determines if any predecessor of MBB is on the path from block that has use
399/// or def of CSRs/FI to MBB.
400/// ReachableByDirty: All blocks reachable from block that has use or def of
401/// CSR/FI.
402static bool
404 const MachineBasicBlock &MBB) {
405 for (const MachineBasicBlock *PredBB : MBB.predecessors())
406 if (ReachableByDirty.count(PredBB))
407 return true;
408 return false;
409}
410
411/// Derives the list of all the basic blocks reachable from MBB.
413 const MachineBasicBlock &MBB) {
415 MBB.succ_end());
416 Visited.insert(&MBB);
417 while (!Worklist.empty()) {
418 MachineBasicBlock *SuccMBB = Worklist.pop_back_val();
419 if (!Visited.insert(SuccMBB).second)
420 continue;
421 Worklist.append(SuccMBB->succ_begin(), SuccMBB->succ_end());
422 }
423}
424
425/// Collect blocks reachable by use or def of CSRs/FI.
428 DenseSet<const MachineBasicBlock *> &ReachableByDirty) {
429 for (const MachineBasicBlock *MBB : DirtyBBs) {
430 if (ReachableByDirty.count(MBB))
431 continue;
432 // Mark all offsprings as reachable.
433 markAllReachable(ReachableByDirty, *MBB);
434 }
435}
436
437/// \return true if there is a clean path from SavePoint to the original
438/// Restore.
439static bool
443 SmallVector<MachineBasicBlock *, 4> Worklist(CleanPreds);
444 while (!Worklist.empty()) {
445 MachineBasicBlock *CleanBB = Worklist.pop_back_val();
446 if (CleanBB == SavePoint)
447 return true;
448 if (!Visited.insert(CleanBB).second || !CleanBB->pred_size())
449 continue;
450 Worklist.append(CleanBB->pred_begin(), CleanBB->pred_end());
451 }
452 return false;
453}
454
455/// This function updates the branches post restore point split.
456///
457/// Restore point has been split.
458/// Old restore point: MBB
459/// New restore point: NMBB
460/// Any basic block(say BBToUpdate) which had a fallthrough to MBB
461/// previously should
462/// 1. Fallthrough to NMBB iff NMBB is inserted immediately above MBB in the
463/// block layout OR
464/// 2. Branch unconditionally to NMBB iff NMBB is inserted at any other place.
465static void updateTerminator(MachineBasicBlock *BBToUpdate,
466 MachineBasicBlock *NMBB,
467 const TargetInstrInfo *TII) {
468 DebugLoc DL = BBToUpdate->findBranchDebugLoc();
469 // if NMBB isn't the new layout successor for BBToUpdate, insert unconditional
470 // branch to it
471 if (!BBToUpdate->isLayoutSuccessor(NMBB))
472 TII->insertUnconditionalBranch(*BBToUpdate, NMBB, DL);
473}
474
475/// This function splits the restore point and returns new restore point/BB.
476///
477/// DirtyPreds: Predessors of \p MBB that are ReachableByDirty
478///
479/// Decision has been made to split the restore point.
480/// old restore point: \p MBB
481/// new restore point: \p NMBB
482/// This function makes the necessary block layout changes so that
483/// 1. \p NMBB points to \p MBB unconditionally
484/// 2. All dirtyPreds that previously pointed to \p MBB point to \p NMBB
485static MachineBasicBlock *
488 const TargetInstrInfo *TII) {
490
491 // get the list of DirtyPreds who have a fallthrough to MBB
492 // before the block layout change. This is just to ensure that if the NMBB is
493 // inserted after MBB, then we create unconditional branch from
494 // DirtyPred/CleanPred to NMBB
496 for (MachineBasicBlock *BB : DirtyPreds)
497 if (BB->getFallThrough(false) == MBB)
498 MBBFallthrough.insert(BB);
499
501 // Insert this block at the end of the function. Inserting in between may
502 // interfere with control flow optimizer decisions.
503 MF->insert(MF->end(), NMBB);
504
506 NMBB->addLiveIn(LI.PhysReg);
507
508 TII->insertUnconditionalBranch(*NMBB, MBB, DebugLoc());
509
510 // After splitting, all predecessors of the restore point should be dirty
511 // blocks.
512 for (MachineBasicBlock *SuccBB : DirtyPreds)
513 SuccBB->ReplaceUsesOfBlockWith(MBB, NMBB);
514
515 NMBB->addSuccessor(MBB);
516
517 for (MachineBasicBlock *BBToUpdate : MBBFallthrough)
518 updateTerminator(BBToUpdate, NMBB, TII);
519
520 return NMBB;
521}
522
523/// This function undoes the restore point split done earlier.
524///
525/// DirtyPreds: All predecessors of \p NMBB that are ReachableByDirty.
526///
527/// Restore point was split and the change needs to be unrolled. Make necessary
528/// changes to reset restore point from \p NMBB to \p MBB.
532 const TargetInstrInfo *TII) {
533 // For a BB, if NMBB is fallthrough in the current layout, then in the new
534 // layout a. BB should fallthrough to MBB OR b. BB should undconditionally
535 // branch to MBB
537 for (MachineBasicBlock *BB : DirtyPreds)
538 if (BB->getFallThrough(false) == NMBB)
539 NMBBFallthrough.insert(BB);
540
541 NMBB->removeSuccessor(MBB);
542 for (MachineBasicBlock *SuccBB : DirtyPreds)
543 SuccBB->ReplaceUsesOfBlockWith(NMBB, MBB);
544
545 NMBB->erase(NMBB->begin(), NMBB->end());
546 NMBB->eraseFromParent();
547
548 for (MachineBasicBlock *BBToUpdate : NMBBFallthrough)
549 updateTerminator(BBToUpdate, MBB, TII);
550}
551
552// A block is deemed fit for restore point split iff there exist
553// 1. DirtyPreds - preds of CurRestore reachable from use or def of CSR/FI
554// 2. CleanPreds - preds of CurRestore that arent DirtyPreds
555bool ShrinkWrap::checkIfRestoreSplittable(
556 const MachineBasicBlock *CurRestore,
557 const DenseSet<const MachineBasicBlock *> &ReachableByDirty,
560 const TargetInstrInfo *TII, RegScavenger *RS) {
561 for (const MachineInstr &MI : *CurRestore)
562 if (useOrDefCSROrFI(MI, RS, /*StackAddressUsed=*/true))
563 return false;
564
565 for (MachineBasicBlock *PredBB : CurRestore->predecessors()) {
566 if (!isAnalyzableBB(*TII, *PredBB))
567 return false;
568
569 if (ReachableByDirty.count(PredBB))
570 DirtyPreds.push_back(PredBB);
571 else
572 CleanPreds.push_back(PredBB);
573 }
574
575 return !(CleanPreds.empty() || DirtyPreds.empty());
576}
577
578bool ShrinkWrap::postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
579 RegScavenger *RS) {
581 return false;
582
583 MachineBasicBlock *InitSave = nullptr;
584 MachineBasicBlock *InitRestore = nullptr;
585
586 if (HasCandidate) {
587 InitSave = Save;
588 InitRestore = Restore;
589 } else {
590 InitRestore = nullptr;
591 InitSave = &MF.front();
592 for (MachineBasicBlock &MBB : MF) {
593 if (MBB.isEHFuncletEntry())
594 return false;
595 if (MBB.isReturnBlock()) {
596 // Do not support multiple restore points.
597 if (InitRestore)
598 return false;
599 InitRestore = &MBB;
600 }
601 }
602 }
603
604 if (!InitSave || !InitRestore || InitRestore == InitSave ||
605 !MDT->dominates(InitSave, InitRestore) ||
606 !MPDT->dominates(InitRestore, InitSave))
607 return false;
608
609 // Bail out of the optimization if any of the basic block is target of
610 // INLINEASM_BR instruction
611 for (MachineBasicBlock &MBB : MF)
613 return false;
614
616 for (MachineBasicBlock &MBB : MF) {
617 if (MBB.isEHPad()) {
618 DirtyBBs.insert(&MBB);
619 continue;
620 }
621 for (const MachineInstr &MI : MBB)
622 if (useOrDefCSROrFI(MI, RS, /*StackAddressUsed=*/true)) {
623 DirtyBBs.insert(&MBB);
624 break;
625 }
626 }
627
628 // Find blocks reachable from the use or def of CSRs/FI.
630 collectBlocksReachableByDirty(DirtyBBs, ReachableByDirty);
631
632 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
635 if (!checkIfRestoreSplittable(InitRestore, ReachableByDirty, DirtyPreds,
636 CleanPreds, TII, RS))
637 return false;
638
639 // Trying to reach out to the new save point which dominates all dirty blocks.
640 MachineBasicBlock *NewSave =
641 FindIDom<>(**DirtyPreds.begin(), DirtyPreds, *MDT, false);
642
643 while (NewSave && (hasDirtyPred(ReachableByDirty, *NewSave) ||
644 EntryFreq < MBFI->getBlockFreq(NewSave) ||
645 /*Entry freq has been observed more than a loop block in
646 some cases*/
647 MLI->getLoopFor(NewSave)))
648 NewSave = FindIDom<>(**NewSave->pred_begin(), NewSave->predecessors(), *MDT,
649 false);
650
651 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
652 if (!NewSave || NewSave == InitSave ||
653 isSaveReachableThroughClean(NewSave, CleanPreds) ||
654 !TFI->canUseAsPrologue(*NewSave))
655 return false;
656
657 // Now we know that splitting a restore point can isolate the restore point
658 // from clean blocks and doing so can shrink the save point.
659 MachineBasicBlock *NewRestore =
660 tryToSplitRestore(InitRestore, DirtyPreds, TII);
661
662 // Make sure if the new restore point is valid as an epilogue, depending on
663 // targets.
664 if (!TFI->canUseAsEpilogue(*NewRestore)) {
665 rollbackRestoreSplit(MF, NewRestore, InitRestore, DirtyPreds, TII);
666 return false;
667 }
668
669 Save = NewSave;
670 Restore = NewRestore;
671
672 MDT->recalculate(MF);
673 MPDT->recalculate(MF);
674
675 assert((MDT->dominates(Save, Restore) && MPDT->dominates(Restore, Save)) &&
676 "Incorrect save or restore point due to dominance relations");
677 assert((!MLI->getLoopFor(Save) && !MLI->getLoopFor(Restore)) &&
678 "Unexpected save or restore point in a loop");
679 assert((EntryFreq >= MBFI->getBlockFreq(Save) &&
680 EntryFreq >= MBFI->getBlockFreq(Restore)) &&
681 "Incorrect save or restore point based on block frequency");
682 return true;
683}
684
685void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB,
686 RegScavenger *RS) {
687 // Get rid of the easy cases first.
688 if (!Save)
689 Save = &MBB;
690 else
691 Save = MDT->findNearestCommonDominator(Save, &MBB);
692 assert(Save);
693
694 if (!Restore)
695 Restore = &MBB;
696 else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it
697 // means the block never returns. If that's the
698 // case, we don't want to call
699 // `findNearestCommonDominator`, which will
700 // return `Restore`.
701 Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
702 else
703 Restore = nullptr; // Abort, we can't find a restore point in this case.
704
705 // Make sure we would be able to insert the restore code before the
706 // terminator.
707 if (Restore == &MBB) {
708 for (const MachineInstr &Terminator : MBB.terminators()) {
709 if (!useOrDefCSROrFI(Terminator, RS, /*StackAddressUsed=*/true))
710 continue;
711 // One of the terminator needs to happen before the restore point.
712 if (MBB.succ_empty()) {
713 Restore = nullptr; // Abort, we can't find a restore point in this case.
714 break;
715 }
716 // Look for a restore point that post-dominates all the successors.
717 // The immediate post-dominator is what we are looking for.
718 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
719 break;
720 }
721 }
722
723 if (!Restore) {
725 dbgs() << "Restore point needs to be spanned on several blocks\n");
726 return;
727 }
728
729 // Make sure Save and Restore are suitable for shrink-wrapping:
730 // 1. all path from Save needs to lead to Restore before exiting.
731 // 2. all path to Restore needs to go through Save from Entry.
732 // We achieve that by making sure that:
733 // A. Save dominates Restore.
734 // B. Restore post-dominates Save.
735 // C. Save and Restore are in the same loop.
736 bool SaveDominatesRestore = false;
737 bool RestorePostDominatesSave = false;
738 while (Restore &&
739 (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
740 !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
741 // Post-dominance is not enough in loops to ensure that all uses/defs
742 // are after the prologue and before the epilogue at runtime.
743 // E.g.,
744 // while(1) {
745 // Save
746 // Restore
747 // if (...)
748 // break;
749 // use/def CSRs
750 // }
751 // All the uses/defs of CSRs are dominated by Save and post-dominated
752 // by Restore. However, the CSRs uses are still reachable after
753 // Restore and before Save are executed.
754 //
755 // For now, just push the restore/save points outside of loops.
756 // FIXME: Refine the criteria to still find interesting cases
757 // for loops.
758 MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
759 // Fix (A).
760 if (!SaveDominatesRestore) {
761 Save = MDT->findNearestCommonDominator(Save, Restore);
762 continue;
763 }
764 // Fix (B).
765 if (!RestorePostDominatesSave)
766 Restore = MPDT->findNearestCommonDominator(Restore, Save);
767
768 // Fix (C).
769 if (Restore && (MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
770 if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
771 // Push Save outside of this loop if immediate dominator is different
772 // from save block. If immediate dominator is not different, bail out.
773 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
774 if (!Save)
775 break;
776 } else {
777 // If the loop does not exit, there is no point in looking
778 // for a post-dominator outside the loop.
780 MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
781 // Push Restore outside of this loop.
782 // Look for the immediate post-dominator of the loop exits.
783 MachineBasicBlock *IPdom = Restore;
784 for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
785 IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
786 if (!IPdom)
787 break;
788 }
789 // If the immediate post-dominator is not in a less nested loop,
790 // then we are stuck in a program with an infinite loop.
791 // In that case, we will not find a safe point, hence, bail out.
792 if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
793 Restore = IPdom;
794 else {
795 Restore = nullptr;
796 break;
797 }
798 }
799 }
800 }
801}
802
804 StringRef RemarkName, StringRef RemarkMessage,
805 const DiagnosticLocation &Loc,
806 const MachineBasicBlock *MBB) {
807 ORE->emit([&]() {
808 return MachineOptimizationRemarkMissed(DEBUG_TYPE, RemarkName, Loc, MBB)
809 << RemarkMessage;
810 });
811
812 LLVM_DEBUG(dbgs() << RemarkMessage << '\n');
813 return false;
814}
815
816bool ShrinkWrap::performShrinkWrapping(
818 RegScavenger *RS) {
819 for (MachineBasicBlock *MBB : RPOT) {
820 LLVM_DEBUG(dbgs() << "Look into: " << printMBBReference(*MBB) << '\n');
821
822 if (MBB->isEHFuncletEntry())
823 return giveUpWithRemarks(ORE, "UnsupportedEHFunclets",
824 "EH Funclets are not supported yet.",
825 MBB->front().getDebugLoc(), MBB);
826
828 // Push the prologue and epilogue outside of the region that may throw (or
829 // jump out via inlineasm_br), by making sure that all the landing pads
830 // are at least at the boundary of the save and restore points. The
831 // problem is that a basic block can jump out from the middle in these
832 // cases, which we do not handle.
833 updateSaveRestorePoints(*MBB, RS);
834 if (!ArePointsInteresting()) {
835 LLVM_DEBUG(dbgs() << "EHPad/inlineasm_br prevents shrink-wrapping\n");
836 return false;
837 }
838 continue;
839 }
840
841 bool StackAddressUsed = false;
842 // Check if we found any stack accesses in the predecessors. We are not
843 // doing a full dataflow analysis here to keep things simple but just
844 // rely on a reverse portorder traversal (RPOT) to guarantee predecessors
845 // are already processed except for loops (and accept the conservative
846 // result for loops).
847 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
848 if (StackAddressUsedBlockInfo.test(Pred->getNumber())) {
849 StackAddressUsed = true;
850 break;
851 }
852 }
853
854 for (const MachineInstr &MI : *MBB) {
855 if (useOrDefCSROrFI(MI, RS, StackAddressUsed)) {
856 // Save (resp. restore) point must dominate (resp. post dominate)
857 // MI. Look for the proper basic block for those.
858 updateSaveRestorePoints(*MBB, RS);
859 // If we are at a point where we cannot improve the placement of
860 // save/restore instructions, just give up.
861 if (!ArePointsInteresting()) {
862 LLVM_DEBUG(dbgs() << "No Shrink wrap candidate found\n");
863 return false;
864 }
865 // No need to look for other instructions, this basic block
866 // will already be part of the handled region.
867 StackAddressUsed = true;
868 break;
869 }
870 }
871 StackAddressUsedBlockInfo[MBB->getNumber()] = StackAddressUsed;
872 }
873 if (!ArePointsInteresting()) {
874 // If the points are not interesting at this point, then they must be null
875 // because it means we did not encounter any frame/CSR related code.
876 // Otherwise, we would have returned from the previous loop.
877 assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
878 LLVM_DEBUG(dbgs() << "Nothing to shrink-wrap\n");
879 return false;
880 }
881
882 LLVM_DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: "
883 << EntryFreq.getFrequency() << '\n');
884
885 const TargetFrameLowering *TFI =
886 MachineFunc->getSubtarget().getFrameLowering();
887 do {
888 LLVM_DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
889 << printMBBReference(*Save) << ' '
890 << printBlockFreq(*MBFI, *Save)
891 << "\nRestore: " << printMBBReference(*Restore) << ' '
892 << printBlockFreq(*MBFI, *Restore) << '\n');
893
894 bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
895 if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save)) &&
896 EntryFreq >= MBFI->getBlockFreq(Restore)) &&
897 ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
898 TFI->canUseAsEpilogue(*Restore)))
899 break;
901 dbgs() << "New points are too expensive or invalid for the target\n");
902 MachineBasicBlock *NewBB;
903 if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
904 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
905 if (!Save)
906 break;
907 NewBB = Save;
908 } else {
909 // Restore is expensive.
910 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
911 if (!Restore)
912 break;
913 NewBB = Restore;
914 }
915 updateSaveRestorePoints(*NewBB, RS);
916 } while (Save && Restore);
917
918 if (!ArePointsInteresting()) {
919 ++NumCandidatesDropped;
920 return false;
921 }
922 return true;
923}
924
925bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
926 if (skipFunction(MF.getFunction()) || MF.empty() || !isShrinkWrapEnabled(MF))
927 return false;
928
929 LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
930
931 init(MF);
932
934 if (containsIrreducibleCFG<MachineBasicBlock *>(RPOT, *MLI)) {
935 // If MF is irreducible, a block may be in a loop without
936 // MachineLoopInfo reporting it. I.e., we may use the
937 // post-dominance property in loops, which lead to incorrect
938 // results. Moreover, we may miss that the prologue and
939 // epilogue are not in the same loop, leading to unbalanced
940 // construction/deconstruction of the stack frame.
941 return giveUpWithRemarks(ORE, "UnsupportedIrreducibleCFG",
942 "Irreducible CFGs are not supported yet.",
943 MF.getFunction().getSubprogram(), &MF.front());
944 }
945
947 std::unique_ptr<RegScavenger> RS(
948 TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr);
949
950 bool Changed = false;
951
952 // Initially, conservatively assume that stack addresses can be used in each
953 // basic block and change the state only for those basic blocks for which we
954 // were able to prove the opposite.
955 StackAddressUsedBlockInfo.resize(MF.getNumBlockIDs(), true);
956 bool HasCandidate = performShrinkWrapping(RPOT, RS.get());
957 StackAddressUsedBlockInfo.clear();
958 Changed = postShrinkWrapping(HasCandidate, MF, RS.get());
959 if (!HasCandidate && !Changed)
960 return false;
961 if (!ArePointsInteresting())
962 return Changed;
963
964 LLVM_DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: "
965 << printMBBReference(*Save) << ' '
966 << "\nRestore: " << printMBBReference(*Restore) << '\n');
967
968 MachineFrameInfo &MFI = MF.getFrameInfo();
969 MFI.setSavePoint(Save);
970 MFI.setRestorePoint(Restore);
971 ++NumCandidates;
972 return Changed;
973}
974
975bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
977
978 switch (EnableShrinkWrapOpt) {
979 case cl::BOU_UNSET:
980 return TFI->enableShrinkWrapping(MF) &&
981 // Windows with CFI has some limitations that make it impossible
982 // to use shrink-wrapping.
984 // Sanitizers look at the value of the stack at the location
985 // of the crash. Since a crash can happen anywhere, the
986 // frame must be lowered before anything else happen for the
987 // sanitizers to be able to get a correct stack frame.
988 !(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
989 MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
990 MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
991 MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
992 // If EnableShrinkWrap is set, it takes precedence on whatever the
993 // target sets. The rational is that we assume we want to test
994 // something related to shrink-wrapping.
995 case cl::BOU_TRUE:
996 return true;
997 case cl::BOU_FALSE:
998 return false;
999 }
1000 llvm_unreachable("Invalid shrink-wrapping state");
1001}
aarch64 promote const
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
This file implements the BitVector class.
#define LLVM_DEBUG(X)
Definition: Debug.h:101
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
static void markAllReachable(DenseSet< const MachineBasicBlock * > &Visited, const MachineBasicBlock &MBB)
Derives the list of all the basic blocks reachable from MBB.
Definition: ShrinkWrap.cpp:412
static void updateTerminator(MachineBasicBlock *BBToUpdate, MachineBasicBlock *NMBB, const TargetInstrInfo *TII)
This function updates the branches post restore point split.
Definition: ShrinkWrap.cpp:465
static MachineBasicBlock * tryToSplitRestore(MachineBasicBlock *MBB, ArrayRef< MachineBasicBlock * > DirtyPreds, const TargetInstrInfo *TII)
This function splits the restore point and returns new restore point/BB.
Definition: ShrinkWrap.cpp:486
static bool hasDirtyPred(const DenseSet< const MachineBasicBlock * > &ReachableByDirty, const MachineBasicBlock &MBB)
Determines if any predecessor of MBB is on the path from block that has use or def of CSRs/FI to MBB.
Definition: ShrinkWrap.cpp:403
static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE, StringRef RemarkName, StringRef RemarkMessage, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
Definition: ShrinkWrap.cpp:803
static cl::opt< bool > EnablePostShrinkWrapOpt("enable-shrink-wrap-region-split", cl::init(true), cl::Hidden, cl::desc("enable splitting of the restore block if possible"))
static void rollbackRestoreSplit(MachineFunction &MF, MachineBasicBlock *NMBB, MachineBasicBlock *MBB, ArrayRef< MachineBasicBlock * > DirtyPreds, const TargetInstrInfo *TII)
This function undoes the restore point split done earlier.
Definition: ShrinkWrap.cpp:529
static bool isAnalyzableBB(const TargetInstrInfo &TII, MachineBasicBlock &Entry)
Definition: ShrinkWrap.cpp:390
static bool isSaveReachableThroughClean(const MachineBasicBlock *SavePoint, ArrayRef< MachineBasicBlock * > CleanPreds)
Definition: ShrinkWrap.cpp:440
static cl::opt< cl::boolOrDefault > EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden, cl::desc("enable the shrink-wrapping pass"))
#define DEBUG_TYPE
Definition: ShrinkWrap.cpp:91
static void collectBlocksReachableByDirty(const DenseSet< const MachineBasicBlock * > &DirtyBBs, DenseSet< const MachineBasicBlock * > &ReachableByDirty)
Collect blocks reachable by use or def of CSRs/FI.
Definition: ShrinkWrap.cpp:426
static MachineBasicBlock * FindIDom(MachineBasicBlock &Block, ListOfBBs BBs, DominanceAnalysis &Dom, bool Strict=true)
Helper function to find the immediate (post) dominator.
Definition: ShrinkWrap.cpp:377
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:166
This file describes how to lower LLVM code to machine code.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool test(unsigned Idx) const
Definition: BitVector.h:461
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
Definition: BitVector.h:300
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
Definition: BitVector.h:308
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1837
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:743
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....
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool usesWindowsCFI() const
Definition: MCAsmInfo.h:759
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
unsigned pred_size() const
bool isEHPad() const
Returns true if the block is a landing pad.
iterator_range< livein_iterator > liveins() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
iterator_range< iterator > terminators()
DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
BlockFrequency getEntryFreq() const
Divide a block's BlockFrequency::getFrequency() value by this value to obtain the entry block - relat...
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setRestorePoint(MachineBasicBlock *NewRestore)
void setSavePoint(MachineBasicBlock *NewSave)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
Representation of each machine instruction.
Definition: MachineInstr.h:69
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:498
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
Diagnostic information for missed-optimization remarks.
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
MachineBasicBlock * findNearestCommonDominator(ArrayRef< MachineBasicBlock * > Blocks) const
Returns the nearest common dominator of the given blocks.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Special value supplied for machine level alias analysis.
MCRegister getLastCalleeSavedAlias(MCRegister PhysReg) const
getLastCalleeSavedAlias - Returns the last callee saved register that overlaps PhysReg,...
void runOnMachineFunction(const MachineFunction &MF)
runOnFunction - Prepare to answer questions about MF.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:367
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:502
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Information about stack frame layout on the target.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual bool enableShrinkWrapping(const MachineFunction &MF) const
Returns true if the target will correctly handle shrink wrapping.
virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a epilogue for the target.
virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const
Check whether or not the given MBB can be used as a prologue for the target.
TargetInstrInfo - Interface to description of machine instruction set.
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetLowering * getTargetLowering() const
LLVM Value Representation.
Definition: Value.h:74
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
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:97
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition: COFF.h:826
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1722
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
char & ShrinkWrapID
ShrinkWrap pass. Look for the best place to insert save and restore.
Definition: ShrinkWrap.cpp:288
void initializeShrinkWrapPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
DWARFExpression::Operation Op
Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Pair of physical register and lane mask.