1 //===-- ShrinkWrap.cpp - Compute safe point for prolog/epilog insertion ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass looks for safe point where the prologue and epilogue can be
12 // The safe point for the prologue (resp. epilogue) is called Save
14 // A point is safe for prologue (resp. epilogue) if and only if
15 // it 1) dominates (resp. post-dominates) all the frame related operations and
16 // between 2) two executions of the Save (resp. Restore) point there is an
17 // execution of the Restore (resp. Save) point.
19 // For instance, the following points are safe:
20 // for (int i = 0; i < 10; ++i) {
25 // Indeed, the execution looks like Save -> Restore -> Save -> Restore ...
26 // And the following points are not:
27 // for (int i = 0; i < 10; ++i) {
31 // for (int i = 0; i < 10; ++i) {
35 // Indeed, the execution looks like Save -> Save -> ... -> Restore -> Restore.
37 // This pass also ensures that the safe points are 3) cheaper than the regular
38 // entry and exits blocks.
40 // Property #1 is ensured via the use of MachineDominatorTree and
41 // MachinePostDominatorTree.
42 // Property #2 is ensured via property #1 and MachineLoopInfo, i.e., both
43 // points must be in the same loop.
44 // Property #3 is ensured via the MachineBlockFrequencyInfo.
46 // If this pass found points matching all these properties, then
47 // MachineFrameInfo is updated this that information.
48 //===----------------------------------------------------------------------===//
49 #include "llvm/ADT/BitVector.h"
50 #include "llvm/ADT/SetVector.h"
51 #include "llvm/ADT/Statistic.h"
52 // To check for profitability.
53 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
54 // For property #1 for Save.
55 #include "llvm/CodeGen/MachineDominators.h"
56 #include "llvm/CodeGen/MachineFunctionPass.h"
57 // To record the result of the analysis.
58 #include "llvm/CodeGen/MachineFrameInfo.h"
60 #include "llvm/CodeGen/MachineLoopInfo.h"
61 // For property #1 for Restore.
62 #include "llvm/CodeGen/MachinePostDominators.h"
63 #include "llvm/CodeGen/Passes.h"
64 // To know about callee-saved.
65 #include "llvm/CodeGen/RegisterClassInfo.h"
66 #include "llvm/MC/MCAsmInfo.h"
67 #include "llvm/Support/Debug.h"
68 // To query the target about frame lowering.
69 #include "llvm/Target/TargetFrameLowering.h"
70 // To know about frame setup operation.
71 #include "llvm/Target/TargetInstrInfo.h"
72 #include "llvm/Target/TargetMachine.h"
73 // To access TargetInstrInfo.
74 #include "llvm/Target/TargetSubtargetInfo.h"
76 #define DEBUG_TYPE "shrink-wrap"
80 STATISTIC(NumFunc, "Number of functions");
81 STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
82 STATISTIC(NumCandidatesDropped,
83 "Number of shrink-wrapping candidates dropped because of frequency");
85 static cl::opt<cl::boolOrDefault>
86 EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
87 cl::desc("enable the shrink-wrapping pass"));
90 /// \brief Class to determine where the safe point to insert the
91 /// prologue and epilogue are.
92 /// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
93 /// shrink-wrapping term for prologue/epilogue placement, this pass
94 /// does not rely on expensive data-flow analysis. Instead we use the
95 /// dominance properties and loop information to decide which point
96 /// are safe for such insertion.
97 class ShrinkWrap : public MachineFunctionPass {
98 /// Hold callee-saved information.
99 RegisterClassInfo RCI;
100 MachineDominatorTree *MDT;
101 MachinePostDominatorTree *MPDT;
102 /// Current safe point found for the prologue.
103 /// The prologue will be inserted before the first instruction
104 /// in this basic block.
105 MachineBasicBlock *Save;
106 /// Current safe point found for the epilogue.
107 /// The epilogue will be inserted before the first terminator instruction
108 /// in this basic block.
109 MachineBasicBlock *Restore;
110 /// Hold the information of the basic block frequency.
111 /// Use to check the profitability of the new points.
112 MachineBlockFrequencyInfo *MBFI;
113 /// Hold the loop information. Used to determine if Save and Restore
114 /// are in the same loop.
115 MachineLoopInfo *MLI;
116 /// Frequency of the Entry block.
118 /// Current opcode for frame setup.
119 unsigned FrameSetupOpcode;
120 /// Current opcode for frame destroy.
121 unsigned FrameDestroyOpcode;
123 const MachineBasicBlock *Entry;
124 typedef SmallSetVector<unsigned, 16> SetOfRegs;
125 /// Registers that need to be saved for the current function.
126 mutable SetOfRegs CurrentCSRs;
127 /// Current MachineFunction.
128 MachineFunction *MachineFunc;
130 /// \brief Check if \p MI uses or defines a callee-saved register or
131 /// a frame index. If this is the case, this means \p MI must happen
132 /// after Save and before Restore.
133 bool useOrDefCSROrFI(const MachineInstr &MI) const;
135 const SetOfRegs &getCurrentCSRs() const {
136 if (CurrentCSRs.empty()) {
138 const TargetFrameLowering *TFI =
139 MachineFunc->getSubtarget().getFrameLowering();
141 TFI->determineCalleeSaves(*MachineFunc, SavedRegs, nullptr);
143 for (int Reg = SavedRegs.find_first(); Reg != -1;
144 Reg = SavedRegs.find_next(Reg))
145 CurrentCSRs.insert((unsigned)Reg);
150 /// \brief Update the Save and Restore points such that \p MBB is in
151 /// the region that is dominated by Save and post-dominated by Restore
152 /// and Save and Restore still match the safe point definition.
153 /// Such point may not exist and Save and/or Restore may be null after
155 void updateSaveRestorePoints(MachineBasicBlock &MBB);
157 /// \brief Initialize the pass for \p MF.
158 void init(MachineFunction &MF) {
159 RCI.runOnMachineFunction(MF);
160 MDT = &getAnalysis<MachineDominatorTree>();
161 MPDT = &getAnalysis<MachinePostDominatorTree>();
164 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
165 MLI = &getAnalysis<MachineLoopInfo>();
166 EntryFreq = MBFI->getEntryFreq();
167 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
168 FrameSetupOpcode = TII.getCallFrameSetupOpcode();
169 FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
177 /// Check whether or not Save and Restore points are still interesting for
179 bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
181 /// \brief Check if shrink wrapping is enabled for this target and function.
182 static bool isShrinkWrapEnabled(const MachineFunction &MF);
187 ShrinkWrap() : MachineFunctionPass(ID) {
188 initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
192 AU.setPreservesAll();
193 AU.addRequired<MachineBlockFrequencyInfo>();
194 AU.addRequired<MachineDominatorTree>();
195 AU.addRequired<MachinePostDominatorTree>();
196 AU.addRequired<MachineLoopInfo>();
197 MachineFunctionPass::getAnalysisUsage(AU);
200 const char *getPassName() const override {
201 return "Shrink Wrapping analysis";
204 /// \brief Perform the shrink-wrapping analysis and update
205 /// the MachineFrameInfo attached to \p MF with the results.
206 bool runOnMachineFunction(MachineFunction &MF) override;
208 } // End anonymous namespace.
210 char ShrinkWrap::ID = 0;
211 char &llvm::ShrinkWrapID = ShrinkWrap::ID;
213 INITIALIZE_PASS_BEGIN(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false,
215 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
216 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
217 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
218 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
219 INITIALIZE_PASS_END(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false, false)
221 bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI) const {
222 if (MI.getOpcode() == FrameSetupOpcode ||
223 MI.getOpcode() == FrameDestroyOpcode) {
224 DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
227 for (const MachineOperand &MO : MI.operands()) {
228 bool UseOrDefCSR = false;
230 unsigned PhysReg = MO.getReg();
233 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
234 "Unallocated register?!");
235 UseOrDefCSR = RCI.getLastCalleeSavedAlias(PhysReg);
236 } else if (MO.isRegMask()) {
237 // Check if this regmask clobbers any of the CSRs.
238 for (unsigned Reg : getCurrentCSRs()) {
239 if (MO.clobbersPhysReg(Reg)) {
245 if (UseOrDefCSR || MO.isFI()) {
246 DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
247 << MO.isFI() << "): " << MI << '\n');
254 /// \brief Helper function to find the immediate (post) dominator.
255 template <typename ListOfBBs, typename DominanceAnalysis>
256 MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
257 DominanceAnalysis &Dom) {
258 MachineBasicBlock *IDom = &Block;
259 for (MachineBasicBlock *BB : BBs) {
260 IDom = Dom.findNearestCommonDominator(IDom, BB);
267 void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB) {
268 // Get rid of the easy cases first.
272 Save = MDT->findNearestCommonDominator(Save, &MBB);
275 DEBUG(dbgs() << "Found a block that is not reachable from Entry\n");
282 Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
284 // Make sure we would be able to insert the restore code before the
286 if (Restore == &MBB) {
287 for (const MachineInstr &Terminator : MBB.terminators()) {
288 if (!useOrDefCSROrFI(Terminator))
290 // One of the terminator needs to happen before the restore point.
291 if (MBB.succ_empty()) {
295 // Look for a restore point that post-dominates all the successors.
296 // The immediate post-dominator is what we are looking for.
297 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
303 DEBUG(dbgs() << "Restore point needs to be spanned on several blocks\n");
307 // Make sure Save and Restore are suitable for shrink-wrapping:
308 // 1. all path from Save needs to lead to Restore before exiting.
309 // 2. all path to Restore needs to go through Save from Entry.
310 // We achieve that by making sure that:
311 // A. Save dominates Restore.
312 // B. Restore post-dominates Save.
313 // C. Save and Restore are in the same loop.
314 bool SaveDominatesRestore = false;
315 bool RestorePostDominatesSave = false;
316 while (Save && Restore &&
317 (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
318 !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
319 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore))) {
321 if (!SaveDominatesRestore) {
322 Save = MDT->findNearestCommonDominator(Save, Restore);
326 if (!RestorePostDominatesSave)
327 Restore = MPDT->findNearestCommonDominator(Restore, Save);
330 if (Save && Restore && Save != Restore &&
331 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore)) {
332 if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
333 // Push Save outside of this loop if immediate dominator is different
334 // from save block. If immediate dominator is not different, bail out.
335 MachineBasicBlock *IDom = FindIDom<>(*Save, Save->predecessors(), *MDT);
344 // If the loop does not exit, there is no point in looking
345 // for a post-dominator outside the loop.
346 SmallVector<MachineBasicBlock*, 4> ExitBlocks;
347 MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
348 // Push Restore outside of this loop.
349 // Look for the immediate post-dominator of the loop exits.
350 MachineBasicBlock *IPdom = Restore;
351 for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
352 IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
356 // If the immediate post-dominator is not in a less nested loop,
357 // then we are stuck in a program with an infinite loop.
358 // In that case, we will not find a safe point, hence, bail out.
359 if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
370 bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
371 if (MF.empty() || !isShrinkWrapEnabled(MF))
374 DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
378 for (MachineBasicBlock &MBB : MF) {
379 DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' ' << MBB.getName()
382 if (MBB.isEHFuncletEntry()) {
383 DEBUG(dbgs() << "EH Funclets are not supported yet.\n");
387 for (const MachineInstr &MI : MBB) {
388 if (!useOrDefCSROrFI(MI))
390 // Save (resp. restore) point must dominate (resp. post dominate)
391 // MI. Look for the proper basic block for those.
392 updateSaveRestorePoints(MBB);
393 // If we are at a point where we cannot improve the placement of
394 // save/restore instructions, just give up.
395 if (!ArePointsInteresting()) {
396 DEBUG(dbgs() << "No Shrink wrap candidate found\n");
399 // No need to look for other instructions, this basic block
400 // will already be part of the handled region.
404 if (!ArePointsInteresting()) {
405 // If the points are not interesting at this point, then they must be null
406 // because it means we did not encounter any frame/CSR related code.
407 // Otherwise, we would have returned from the previous loop.
408 assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
409 DEBUG(dbgs() << "Nothing to shrink-wrap\n");
413 DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
416 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
418 DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
419 << Save->getNumber() << ' ' << Save->getName() << ' '
420 << MBFI->getBlockFreq(Save).getFrequency() << "\nRestore: "
421 << Restore->getNumber() << ' ' << Restore->getName() << ' '
422 << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
424 bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
425 if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
426 EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
427 ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
428 TFI->canUseAsEpilogue(*Restore)))
430 DEBUG(dbgs() << "New points are too expensive or invalid for the target\n");
431 MachineBasicBlock *NewBB;
432 if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
433 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
438 // Restore is expensive.
439 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
444 updateSaveRestorePoints(*NewBB);
445 } while (Save && Restore);
447 if (!ArePointsInteresting()) {
448 ++NumCandidatesDropped;
452 DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: " << Save->getNumber()
453 << ' ' << Save->getName() << "\nRestore: "
454 << Restore->getNumber() << ' ' << Restore->getName() << '\n');
456 MachineFrameInfo *MFI = MF.getFrameInfo();
457 MFI->setSavePoint(Save);
458 MFI->setRestorePoint(Restore);
463 bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
464 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
466 switch (EnableShrinkWrapOpt) {
468 return TFI->enableShrinkWrapping(MF) &&
469 // Windows with CFI has some limitations that make it impossible
470 // to use shrink-wrapping.
471 !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
472 // If EnableShrinkWrap is set, it takes precedence on whatever the
473 // target sets. The rational is that we assume we want to test
474 // something related to shrink-wrapping.
480 llvm_unreachable("Invalid shrink-wrapping state");