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 this properties, then
47 // MachineFrameInfo is updated this that information.
48 //===----------------------------------------------------------------------===//
49 #include "llvm/ADT/Statistic.h"
50 // To check for profitability.
51 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
52 // For property #1 for Save.
53 #include "llvm/CodeGen/MachineDominators.h"
54 #include "llvm/CodeGen/MachineFunctionPass.h"
55 // To record the result of the analysis.
56 #include "llvm/CodeGen/MachineFrameInfo.h"
58 #include "llvm/CodeGen/MachineLoopInfo.h"
59 // For property #1 for Restore.
60 #include "llvm/CodeGen/MachinePostDominators.h"
61 #include "llvm/CodeGen/Passes.h"
62 // To know about callee-saved.
63 #include "llvm/CodeGen/RegisterClassInfo.h"
64 #include "llvm/Support/Debug.h"
65 // To query the target about frame lowering.
66 #include "llvm/Target/TargetFrameLowering.h"
67 // To know about frame setup operation.
68 #include "llvm/Target/TargetInstrInfo.h"
69 // To access TargetInstrInfo.
70 #include "llvm/Target/TargetSubtargetInfo.h"
72 #define DEBUG_TYPE "shrink-wrap"
76 STATISTIC(NumFunc, "Number of functions");
77 STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
78 STATISTIC(NumCandidatesDropped,
79 "Number of shrink-wrapping candidates dropped because of frequency");
82 /// \brief Class to determine where the safe point to insert the
83 /// prologue and epilogue are.
84 /// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
85 /// shrink-wrapping term for prologue/epilogue placement, this pass
86 /// does not rely on expensive data-flow analysis. Instead we use the
87 /// dominance properties and loop information to decide which point
88 /// are safe for such insertion.
89 class ShrinkWrap : public MachineFunctionPass {
90 /// Hold callee-saved information.
91 RegisterClassInfo RCI;
92 MachineDominatorTree *MDT;
93 MachinePostDominatorTree *MPDT;
94 /// Current safe point found for the prologue.
95 /// The prologue will be inserted before the first instruction
96 /// in this basic block.
97 MachineBasicBlock *Save;
98 /// Current safe point found for the epilogue.
99 /// The epilogue will be inserted before the first terminator instruction
100 /// in this basic block.
101 MachineBasicBlock *Restore;
102 /// Hold the information of the basic block frequency.
103 /// Use to check the profitability of the new points.
104 MachineBlockFrequencyInfo *MBFI;
105 /// Hold the loop information. Used to determine if Save and Restore
106 /// are in the same loop.
107 MachineLoopInfo *MLI;
108 /// Frequency of the Entry block.
110 /// Current opcode for frame setup.
111 unsigned FrameSetupOpcode;
112 /// Current opcode for frame destroy.
113 unsigned FrameDestroyOpcode;
115 const MachineBasicBlock *Entry;
117 /// \brief Check if \p MI uses or defines a callee-saved register or
118 /// a frame index. If this is the case, this means \p MI must happen
119 /// after Save and before Restore.
120 bool useOrDefCSROrFI(const MachineInstr &MI) const;
122 /// \brief Update the Save and Restore points such that \p MBB is in
123 /// the region that is dominated by Save and post-dominated by Restore
124 /// and Save and Restore still match the safe point definition.
125 /// Such point may not exist and Save and/or Restore may be null after
127 void updateSaveRestorePoints(MachineBasicBlock &MBB);
129 /// \brief Initialize the pass for \p MF.
130 void init(MachineFunction &MF) {
131 RCI.runOnMachineFunction(MF);
132 MDT = &getAnalysis<MachineDominatorTree>();
133 MPDT = &getAnalysis<MachinePostDominatorTree>();
136 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
137 MLI = &getAnalysis<MachineLoopInfo>();
138 EntryFreq = MBFI->getEntryFreq();
139 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
140 FrameSetupOpcode = TII.getCallFrameSetupOpcode();
141 FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
147 /// Check whether or not Save and Restore points are still interesting for
149 bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
154 ShrinkWrap() : MachineFunctionPass(ID) {
155 initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
158 void getAnalysisUsage(AnalysisUsage &AU) const override {
159 AU.setPreservesAll();
160 AU.addRequired<MachineBlockFrequencyInfo>();
161 AU.addRequired<MachineDominatorTree>();
162 AU.addRequired<MachinePostDominatorTree>();
163 AU.addRequired<MachineLoopInfo>();
164 MachineFunctionPass::getAnalysisUsage(AU);
167 const char *getPassName() const override {
168 return "Shrink Wrapping analysis";
171 /// \brief Perform the shrink-wrapping analysis and update
172 /// the MachineFrameInfo attached to \p MF with the results.
173 bool runOnMachineFunction(MachineFunction &MF) override;
175 } // End anonymous namespace.
177 char ShrinkWrap::ID = 0;
178 char &llvm::ShrinkWrapID = ShrinkWrap::ID;
180 INITIALIZE_PASS_BEGIN(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false,
182 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
183 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
184 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
185 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
186 INITIALIZE_PASS_END(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false, false)
188 bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI) const {
189 if (MI.getOpcode() == FrameSetupOpcode ||
190 MI.getOpcode() == FrameDestroyOpcode) {
191 DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
194 for (const MachineOperand &MO : MI.operands()) {
197 unsigned PhysReg = MO.getReg();
200 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
201 "Unallocated register?!");
202 UseCSR = RCI.getLastCalleeSavedAlias(PhysReg);
204 // TODO: Handle regmask more accurately.
205 // For now, be conservative about them.
206 if (UseCSR || MO.isFI() || MO.isRegMask()) {
207 DEBUG(dbgs() << "Use or define CSR(" << UseCSR << ") or FI(" << MO.isFI()
208 << "): " << MI << '\n');
215 /// \brief Helper function to find the immediate (post) dominator.
216 template <typename ListOfBBs, typename DominanceAnalysis>
217 MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
218 DominanceAnalysis &Dom) {
219 MachineBasicBlock *IDom = &Block;
220 for (MachineBasicBlock *BB : BBs) {
221 IDom = Dom.findNearestCommonDominator(IDom, BB);
228 void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB) {
229 // Get rid of the easy cases first.
233 Save = MDT->findNearestCommonDominator(Save, &MBB);
236 DEBUG(dbgs() << "Found a block that is not reachable from Entry\n");
243 Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
245 // Make sure we would be able to insert the restore code before the
247 if (Restore == &MBB) {
248 for (const MachineInstr &Terminator : MBB.terminators()) {
249 if (!useOrDefCSROrFI(Terminator))
251 // One of the terminator needs to happen before the restore point.
252 if (MBB.succ_empty()) {
256 // Look for a restore point that post-dominates all the successors.
257 // The immediate post-dominator is what we are looking for.
258 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
264 DEBUG(dbgs() << "Restore point needs to be spanned on several blocks\n");
268 // Make sure Save and Restore are suitable for shrink-wrapping:
269 // 1. all path from Save needs to lead to Restore before exiting.
270 // 2. all path to Restore needs to go through Save from Entry.
271 // We achieve that by making sure that:
272 // A. Save dominates Restore.
273 // B. Restore post-dominates Save.
274 // C. Save and Restore are in the same loop.
275 bool SaveDominatesRestore = false;
276 bool RestorePostDominatesSave = false;
277 while (Save && Restore &&
278 (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
279 !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
280 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore))) {
282 if (!SaveDominatesRestore) {
283 Save = MDT->findNearestCommonDominator(Save, Restore);
287 if (!RestorePostDominatesSave)
288 Restore = MPDT->findNearestCommonDominator(Restore, Save);
291 if (Save && Restore && Save != Restore &&
292 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore)) {
293 if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore))
294 // Push Save outside of this loop.
295 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
297 // Push Restore outside of this loop.
298 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
303 bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
306 DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
310 for (MachineBasicBlock &MBB : MF) {
311 DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' ' << MBB.getName()
314 for (const MachineInstr &MI : MBB) {
315 if (!useOrDefCSROrFI(MI))
317 // Save (resp. restore) point must dominate (resp. post dominate)
318 // MI. Look for the proper basic block for those.
319 updateSaveRestorePoints(MBB);
320 // If we are at a point where we cannot improve the placement of
321 // save/restore instructions, just give up.
322 if (!ArePointsInteresting()) {
323 DEBUG(dbgs() << "No Shrink wrap candidate found\n");
326 // No need to look for other instructions, this basic block
327 // will already be part of the handled region.
331 if (!ArePointsInteresting()) {
332 // If the points are not interesting at this point, then they must be null
333 // because it means we did not encounter any frame/CSR related code.
334 // Otherwise, we would have returned from the previous loop.
335 assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
336 DEBUG(dbgs() << "Nothing to shrink-wrap\n");
340 DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
343 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
345 DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
346 << Save->getNumber() << ' ' << Save->getName() << ' '
347 << MBFI->getBlockFreq(Save).getFrequency() << "\nRestore: "
348 << Restore->getNumber() << ' ' << Restore->getName() << ' '
349 << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
351 bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
352 if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
353 EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
354 ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
355 TFI->canUseAsEpilogue(*Restore)))
357 DEBUG(dbgs() << "New points are too expensive or invalid for the target\n");
358 MachineBasicBlock *NewBB;
359 if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
360 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
365 // Restore is expensive.
366 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
371 updateSaveRestorePoints(*NewBB);
372 } while (Save && Restore);
374 if (!ArePointsInteresting()) {
375 ++NumCandidatesDropped;
379 DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: " << Save->getNumber()
380 << ' ' << Save->getName() << "\nRestore: "
381 << Restore->getNumber() << ' ' << Restore->getName() << '\n');
383 MachineFrameInfo *MFI = MF.getFrameInfo();
384 MFI->setSavePoint(Save);
385 MFI->setRestorePoint(Restore);