xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/ShrinkWrap.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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"
51 #include "llvm/ADT/PostOrderIterator.h"
52 #include "llvm/ADT/SetVector.h"
53 #include "llvm/ADT/SmallVector.h"
54 #include "llvm/ADT/Statistic.h"
55 #include "llvm/Analysis/CFG.h"
56 #include "llvm/CodeGen/MachineBasicBlock.h"
57 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
58 #include "llvm/CodeGen/MachineDominators.h"
59 #include "llvm/CodeGen/MachineFrameInfo.h"
60 #include "llvm/CodeGen/MachineFunction.h"
61 #include "llvm/CodeGen/MachineFunctionPass.h"
62 #include "llvm/CodeGen/MachineInstr.h"
63 #include "llvm/CodeGen/MachineLoopInfo.h"
64 #include "llvm/CodeGen/MachineOperand.h"
65 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
66 #include "llvm/CodeGen/MachinePostDominators.h"
67 #include "llvm/CodeGen/RegisterClassInfo.h"
68 #include "llvm/CodeGen/RegisterScavenging.h"
69 #include "llvm/CodeGen/TargetFrameLowering.h"
70 #include "llvm/CodeGen/TargetInstrInfo.h"
71 #include "llvm/CodeGen/TargetLowering.h"
72 #include "llvm/CodeGen/TargetRegisterInfo.h"
73 #include "llvm/CodeGen/TargetSubtargetInfo.h"
74 #include "llvm/IR/Attributes.h"
75 #include "llvm/IR/Function.h"
76 #include "llvm/InitializePasses.h"
77 #include "llvm/MC/MCAsmInfo.h"
78 #include "llvm/Pass.h"
79 #include "llvm/Support/CommandLine.h"
80 #include "llvm/Support/Debug.h"
81 #include "llvm/Support/ErrorHandling.h"
82 #include "llvm/Support/raw_ostream.h"
83 #include "llvm/Target/TargetMachine.h"
84 #include <cassert>
85 #include <cstdint>
86 #include <memory>
87 
88 using namespace llvm;
89 
90 #define DEBUG_TYPE "shrink-wrap"
91 
92 STATISTIC(NumFunc, "Number of functions");
93 STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
94 STATISTIC(NumCandidatesDropped,
95           "Number of shrink-wrapping candidates dropped because of frequency");
96 
97 static cl::opt<cl::boolOrDefault>
98 EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
99                     cl::desc("enable the shrink-wrapping pass"));
100 
101 namespace {
102 
103 /// Class to determine where the safe point to insert the
104 /// prologue and epilogue are.
105 /// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
106 /// shrink-wrapping term for prologue/epilogue placement, this pass
107 /// does not rely on expensive data-flow analysis. Instead we use the
108 /// dominance properties and loop information to decide which point
109 /// are safe for such insertion.
110 class ShrinkWrap : public MachineFunctionPass {
111   /// Hold callee-saved information.
112   RegisterClassInfo RCI;
113   MachineDominatorTree *MDT;
114   MachinePostDominatorTree *MPDT;
115 
116   /// Current safe point found for the prologue.
117   /// The prologue will be inserted before the first instruction
118   /// in this basic block.
119   MachineBasicBlock *Save;
120 
121   /// Current safe point found for the epilogue.
122   /// The epilogue will be inserted before the first terminator instruction
123   /// in this basic block.
124   MachineBasicBlock *Restore;
125 
126   /// Hold the information of the basic block frequency.
127   /// Use to check the profitability of the new points.
128   MachineBlockFrequencyInfo *MBFI;
129 
130   /// Hold the loop information. Used to determine if Save and Restore
131   /// are in the same loop.
132   MachineLoopInfo *MLI;
133 
134   // Emit remarks.
135   MachineOptimizationRemarkEmitter *ORE = nullptr;
136 
137   /// Frequency of the Entry block.
138   uint64_t EntryFreq;
139 
140   /// Current opcode for frame setup.
141   unsigned FrameSetupOpcode;
142 
143   /// Current opcode for frame destroy.
144   unsigned FrameDestroyOpcode;
145 
146   /// Stack pointer register, used by llvm.{savestack,restorestack}
147   unsigned SP;
148 
149   /// Entry block.
150   const MachineBasicBlock *Entry;
151 
152   using SetOfRegs = SmallSetVector<unsigned, 16>;
153 
154   /// Registers that need to be saved for the current function.
155   mutable SetOfRegs CurrentCSRs;
156 
157   /// Current MachineFunction.
158   MachineFunction *MachineFunc;
159 
160   /// Check if \p MI uses or defines a callee-saved register or
161   /// a frame index. If this is the case, this means \p MI must happen
162   /// after Save and before Restore.
163   bool useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS) const;
164 
165   const SetOfRegs &getCurrentCSRs(RegScavenger *RS) const {
166     if (CurrentCSRs.empty()) {
167       BitVector SavedRegs;
168       const TargetFrameLowering *TFI =
169           MachineFunc->getSubtarget().getFrameLowering();
170 
171       TFI->determineCalleeSaves(*MachineFunc, SavedRegs, RS);
172 
173       for (int Reg = SavedRegs.find_first(); Reg != -1;
174            Reg = SavedRegs.find_next(Reg))
175         CurrentCSRs.insert((unsigned)Reg);
176     }
177     return CurrentCSRs;
178   }
179 
180   /// Update the Save and Restore points such that \p MBB is in
181   /// the region that is dominated by Save and post-dominated by Restore
182   /// and Save and Restore still match the safe point definition.
183   /// Such point may not exist and Save and/or Restore may be null after
184   /// this call.
185   void updateSaveRestorePoints(MachineBasicBlock &MBB, RegScavenger *RS);
186 
187   /// Initialize the pass for \p MF.
188   void init(MachineFunction &MF) {
189     RCI.runOnMachineFunction(MF);
190     MDT = &getAnalysis<MachineDominatorTree>();
191     MPDT = &getAnalysis<MachinePostDominatorTree>();
192     Save = nullptr;
193     Restore = nullptr;
194     MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
195     MLI = &getAnalysis<MachineLoopInfo>();
196     ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
197     EntryFreq = MBFI->getEntryFreq();
198     const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
199     const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
200     FrameSetupOpcode = TII.getCallFrameSetupOpcode();
201     FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
202     SP = Subtarget.getTargetLowering()->getStackPointerRegisterToSaveRestore();
203     Entry = &MF.front();
204     CurrentCSRs.clear();
205     MachineFunc = &MF;
206 
207     ++NumFunc;
208   }
209 
210   /// Check whether or not Save and Restore points are still interesting for
211   /// shrink-wrapping.
212   bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
213 
214   /// Check if shrink wrapping is enabled for this target and function.
215   static bool isShrinkWrapEnabled(const MachineFunction &MF);
216 
217 public:
218   static char ID;
219 
220   ShrinkWrap() : MachineFunctionPass(ID) {
221     initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
222   }
223 
224   void getAnalysisUsage(AnalysisUsage &AU) const override {
225     AU.setPreservesAll();
226     AU.addRequired<MachineBlockFrequencyInfo>();
227     AU.addRequired<MachineDominatorTree>();
228     AU.addRequired<MachinePostDominatorTree>();
229     AU.addRequired<MachineLoopInfo>();
230     AU.addRequired<MachineOptimizationRemarkEmitterPass>();
231     MachineFunctionPass::getAnalysisUsage(AU);
232   }
233 
234   MachineFunctionProperties getRequiredProperties() const override {
235     return MachineFunctionProperties().set(
236       MachineFunctionProperties::Property::NoVRegs);
237   }
238 
239   StringRef getPassName() const override { return "Shrink Wrapping analysis"; }
240 
241   /// Perform the shrink-wrapping analysis and update
242   /// the MachineFrameInfo attached to \p MF with the results.
243   bool runOnMachineFunction(MachineFunction &MF) override;
244 };
245 
246 } // end anonymous namespace
247 
248 char ShrinkWrap::ID = 0;
249 
250 char &llvm::ShrinkWrapID = ShrinkWrap::ID;
251 
252 INITIALIZE_PASS_BEGIN(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
253 INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
254 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
255 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
256 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
257 INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
258 INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
259 
260 bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI,
261                                  RegScavenger *RS) const {
262   // This prevents premature stack popping when occurs a indirect stack
263   // access. It is overly aggressive for the moment.
264   // TODO: - Obvious non-stack loads and store, such as global values,
265   //         are known to not access the stack.
266   //       - Further, data dependency and alias analysis can validate
267   //         that load and stores never derive from the stack pointer.
268   if (MI.mayLoadOrStore())
269     return true;
270 
271   if (MI.getOpcode() == FrameSetupOpcode ||
272       MI.getOpcode() == FrameDestroyOpcode) {
273     LLVM_DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
274     return true;
275   }
276   for (const MachineOperand &MO : MI.operands()) {
277     bool UseOrDefCSR = false;
278     if (MO.isReg()) {
279       // Ignore instructions like DBG_VALUE which don't read/def the register.
280       if (!MO.isDef() && !MO.readsReg())
281         continue;
282       Register PhysReg = MO.getReg();
283       if (!PhysReg)
284         continue;
285       assert(Register::isPhysicalRegister(PhysReg) && "Unallocated register?!");
286       // The stack pointer is not normally described as a callee-saved register
287       // in calling convention definitions, so we need to watch for it
288       // separately. An SP mentioned by a call instruction, we can ignore,
289       // though, as it's harmless and we do not want to effectively disable tail
290       // calls by forcing the restore point to post-dominate them.
291       UseOrDefCSR = (!MI.isCall() && PhysReg == SP) ||
292                     RCI.getLastCalleeSavedAlias(PhysReg);
293     } else if (MO.isRegMask()) {
294       // Check if this regmask clobbers any of the CSRs.
295       for (unsigned Reg : getCurrentCSRs(RS)) {
296         if (MO.clobbersPhysReg(Reg)) {
297           UseOrDefCSR = true;
298           break;
299         }
300       }
301     }
302     // Skip FrameIndex operands in DBG_VALUE instructions.
303     if (UseOrDefCSR || (MO.isFI() && !MI.isDebugValue())) {
304       LLVM_DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
305                         << MO.isFI() << "): " << MI << '\n');
306       return true;
307     }
308   }
309   return false;
310 }
311 
312 /// Helper function to find the immediate (post) dominator.
313 template <typename ListOfBBs, typename DominanceAnalysis>
314 static MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
315                                    DominanceAnalysis &Dom) {
316   MachineBasicBlock *IDom = &Block;
317   for (MachineBasicBlock *BB : BBs) {
318     IDom = Dom.findNearestCommonDominator(IDom, BB);
319     if (!IDom)
320       break;
321   }
322   if (IDom == &Block)
323     return nullptr;
324   return IDom;
325 }
326 
327 void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB,
328                                          RegScavenger *RS) {
329   // Get rid of the easy cases first.
330   if (!Save)
331     Save = &MBB;
332   else
333     Save = MDT->findNearestCommonDominator(Save, &MBB);
334 
335   if (!Save) {
336     LLVM_DEBUG(dbgs() << "Found a block that is not reachable from Entry\n");
337     return;
338   }
339 
340   if (!Restore)
341     Restore = &MBB;
342   else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it
343                                 // means the block never returns. If that's the
344                                 // case, we don't want to call
345                                 // `findNearestCommonDominator`, which will
346                                 // return `Restore`.
347     Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
348   else
349     Restore = nullptr; // Abort, we can't find a restore point in this case.
350 
351   // Make sure we would be able to insert the restore code before the
352   // terminator.
353   if (Restore == &MBB) {
354     for (const MachineInstr &Terminator : MBB.terminators()) {
355       if (!useOrDefCSROrFI(Terminator, RS))
356         continue;
357       // One of the terminator needs to happen before the restore point.
358       if (MBB.succ_empty()) {
359         Restore = nullptr; // Abort, we can't find a restore point in this case.
360         break;
361       }
362       // Look for a restore point that post-dominates all the successors.
363       // The immediate post-dominator is what we are looking for.
364       Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
365       break;
366     }
367   }
368 
369   if (!Restore) {
370     LLVM_DEBUG(
371         dbgs() << "Restore point needs to be spanned on several blocks\n");
372     return;
373   }
374 
375   // Make sure Save and Restore are suitable for shrink-wrapping:
376   // 1. all path from Save needs to lead to Restore before exiting.
377   // 2. all path to Restore needs to go through Save from Entry.
378   // We achieve that by making sure that:
379   // A. Save dominates Restore.
380   // B. Restore post-dominates Save.
381   // C. Save and Restore are in the same loop.
382   bool SaveDominatesRestore = false;
383   bool RestorePostDominatesSave = false;
384   while (Save && Restore &&
385          (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
386           !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
387           // Post-dominance is not enough in loops to ensure that all uses/defs
388           // are after the prologue and before the epilogue at runtime.
389           // E.g.,
390           // while(1) {
391           //  Save
392           //  Restore
393           //   if (...)
394           //     break;
395           //  use/def CSRs
396           // }
397           // All the uses/defs of CSRs are dominated by Save and post-dominated
398           // by Restore. However, the CSRs uses are still reachable after
399           // Restore and before Save are executed.
400           //
401           // For now, just push the restore/save points outside of loops.
402           // FIXME: Refine the criteria to still find interesting cases
403           // for loops.
404           MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
405     // Fix (A).
406     if (!SaveDominatesRestore) {
407       Save = MDT->findNearestCommonDominator(Save, Restore);
408       continue;
409     }
410     // Fix (B).
411     if (!RestorePostDominatesSave)
412       Restore = MPDT->findNearestCommonDominator(Restore, Save);
413 
414     // Fix (C).
415     if (Save && Restore &&
416         (MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
417       if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
418         // Push Save outside of this loop if immediate dominator is different
419         // from save block. If immediate dominator is not different, bail out.
420         Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
421         if (!Save)
422           break;
423       } else {
424         // If the loop does not exit, there is no point in looking
425         // for a post-dominator outside the loop.
426         SmallVector<MachineBasicBlock*, 4> ExitBlocks;
427         MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
428         // Push Restore outside of this loop.
429         // Look for the immediate post-dominator of the loop exits.
430         MachineBasicBlock *IPdom = Restore;
431         for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
432           IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
433           if (!IPdom)
434             break;
435         }
436         // If the immediate post-dominator is not in a less nested loop,
437         // then we are stuck in a program with an infinite loop.
438         // In that case, we will not find a safe point, hence, bail out.
439         if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
440           Restore = IPdom;
441         else {
442           Restore = nullptr;
443           break;
444         }
445       }
446     }
447   }
448 }
449 
450 static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE,
451                               StringRef RemarkName, StringRef RemarkMessage,
452                               const DiagnosticLocation &Loc,
453                               const MachineBasicBlock *MBB) {
454   ORE->emit([&]() {
455     return MachineOptimizationRemarkMissed(DEBUG_TYPE, RemarkName, Loc, MBB)
456            << RemarkMessage;
457   });
458 
459   LLVM_DEBUG(dbgs() << RemarkMessage << '\n');
460   return false;
461 }
462 
463 bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
464   if (skipFunction(MF.getFunction()) || MF.empty() || !isShrinkWrapEnabled(MF))
465     return false;
466 
467   LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
468 
469   init(MF);
470 
471   ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
472   if (containsIrreducibleCFG<MachineBasicBlock *>(RPOT, *MLI)) {
473     // If MF is irreducible, a block may be in a loop without
474     // MachineLoopInfo reporting it. I.e., we may use the
475     // post-dominance property in loops, which lead to incorrect
476     // results. Moreover, we may miss that the prologue and
477     // epilogue are not in the same loop, leading to unbalanced
478     // construction/deconstruction of the stack frame.
479     return giveUpWithRemarks(ORE, "UnsupportedIrreducibleCFG",
480                              "Irreducible CFGs are not supported yet.",
481                              MF.getFunction().getSubprogram(), &MF.front());
482   }
483 
484   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
485   std::unique_ptr<RegScavenger> RS(
486       TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr);
487 
488   for (MachineBasicBlock &MBB : MF) {
489     LLVM_DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' '
490                       << MBB.getName() << '\n');
491 
492     if (MBB.isEHFuncletEntry())
493       return giveUpWithRemarks(ORE, "UnsupportedEHFunclets",
494                                "EH Funclets are not supported yet.",
495                                MBB.front().getDebugLoc(), &MBB);
496 
497     if (MBB.isEHPad() || MBB.isInlineAsmBrIndirectTarget()) {
498       // Push the prologue and epilogue outside of the region that may throw (or
499       // jump out via inlineasm_br), by making sure that all the landing pads
500       // are at least at the boundary of the save and restore points.  The
501       // problem is that a basic block can jump out from the middle in these
502       // cases, which we do not handle.
503       updateSaveRestorePoints(MBB, RS.get());
504       if (!ArePointsInteresting()) {
505         LLVM_DEBUG(dbgs() << "EHPad/inlineasm_br prevents shrink-wrapping\n");
506         return false;
507       }
508       continue;
509     }
510 
511     for (const MachineInstr &MI : MBB) {
512       if (!useOrDefCSROrFI(MI, RS.get()))
513         continue;
514       // Save (resp. restore) point must dominate (resp. post dominate)
515       // MI. Look for the proper basic block for those.
516       updateSaveRestorePoints(MBB, RS.get());
517       // If we are at a point where we cannot improve the placement of
518       // save/restore instructions, just give up.
519       if (!ArePointsInteresting()) {
520         LLVM_DEBUG(dbgs() << "No Shrink wrap candidate found\n");
521         return false;
522       }
523       // No need to look for other instructions, this basic block
524       // will already be part of the handled region.
525       break;
526     }
527   }
528   if (!ArePointsInteresting()) {
529     // If the points are not interesting at this point, then they must be null
530     // because it means we did not encounter any frame/CSR related code.
531     // Otherwise, we would have returned from the previous loop.
532     assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
533     LLVM_DEBUG(dbgs() << "Nothing to shrink-wrap\n");
534     return false;
535   }
536 
537   LLVM_DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
538                     << '\n');
539 
540   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
541   do {
542     LLVM_DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
543                       << Save->getNumber() << ' ' << Save->getName() << ' '
544                       << MBFI->getBlockFreq(Save).getFrequency()
545                       << "\nRestore: " << Restore->getNumber() << ' '
546                       << Restore->getName() << ' '
547                       << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
548 
549     bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
550     if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
551          EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
552         ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
553          TFI->canUseAsEpilogue(*Restore)))
554       break;
555     LLVM_DEBUG(
556         dbgs() << "New points are too expensive or invalid for the target\n");
557     MachineBasicBlock *NewBB;
558     if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
559       Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
560       if (!Save)
561         break;
562       NewBB = Save;
563     } else {
564       // Restore is expensive.
565       Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
566       if (!Restore)
567         break;
568       NewBB = Restore;
569     }
570     updateSaveRestorePoints(*NewBB, RS.get());
571   } while (Save && Restore);
572 
573   if (!ArePointsInteresting()) {
574     ++NumCandidatesDropped;
575     return false;
576   }
577 
578   LLVM_DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: "
579                     << Save->getNumber() << ' ' << Save->getName()
580                     << "\nRestore: " << Restore->getNumber() << ' '
581                     << Restore->getName() << '\n');
582 
583   MachineFrameInfo &MFI = MF.getFrameInfo();
584   MFI.setSavePoint(Save);
585   MFI.setRestorePoint(Restore);
586   ++NumCandidates;
587   return false;
588 }
589 
590 bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
591   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
592 
593   switch (EnableShrinkWrapOpt) {
594   case cl::BOU_UNSET:
595     return TFI->enableShrinkWrapping(MF) &&
596            // Windows with CFI has some limitations that make it impossible
597            // to use shrink-wrapping.
598            !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() &&
599            // Sanitizers look at the value of the stack at the location
600            // of the crash. Since a crash can happen anywhere, the
601            // frame must be lowered before anything else happen for the
602            // sanitizers to be able to get a correct stack frame.
603            !(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
604              MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
605              MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
606              MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
607   // If EnableShrinkWrap is set, it takes precedence on whatever the
608   // target sets. The rational is that we assume we want to test
609   // something related to shrink-wrapping.
610   case cl::BOU_TRUE:
611     return true;
612   case cl::BOU_FALSE:
613     return false;
614   }
615   llvm_unreachable("Invalid shrink-wrapping state");
616 }
617