xref: /aosp_15_r20/external/llvm/lib/CodeGen/ShrinkWrap.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- ShrinkWrap.cpp - Compute safe point for prolog/epilog insertion ---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass looks for safe point where the prologue and epilogue can be
11*9880d681SAndroid Build Coastguard Worker // inserted.
12*9880d681SAndroid Build Coastguard Worker // The safe point for the prologue (resp. epilogue) is called Save
13*9880d681SAndroid Build Coastguard Worker // (resp. Restore).
14*9880d681SAndroid Build Coastguard Worker // A point is safe for prologue (resp. epilogue) if and only if
15*9880d681SAndroid Build Coastguard Worker // it 1) dominates (resp. post-dominates) all the frame related operations and
16*9880d681SAndroid Build Coastguard Worker // between 2) two executions of the Save (resp. Restore) point there is an
17*9880d681SAndroid Build Coastguard Worker // execution of the Restore (resp. Save) point.
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // For instance, the following points are safe:
20*9880d681SAndroid Build Coastguard Worker // for (int i = 0; i < 10; ++i) {
21*9880d681SAndroid Build Coastguard Worker //   Save
22*9880d681SAndroid Build Coastguard Worker //   ...
23*9880d681SAndroid Build Coastguard Worker //   Restore
24*9880d681SAndroid Build Coastguard Worker // }
25*9880d681SAndroid Build Coastguard Worker // Indeed, the execution looks like Save -> Restore -> Save -> Restore ...
26*9880d681SAndroid Build Coastguard Worker // And the following points are not:
27*9880d681SAndroid Build Coastguard Worker // for (int i = 0; i < 10; ++i) {
28*9880d681SAndroid Build Coastguard Worker //   Save
29*9880d681SAndroid Build Coastguard Worker //   ...
30*9880d681SAndroid Build Coastguard Worker // }
31*9880d681SAndroid Build Coastguard Worker // for (int i = 0; i < 10; ++i) {
32*9880d681SAndroid Build Coastguard Worker //   ...
33*9880d681SAndroid Build Coastguard Worker //   Restore
34*9880d681SAndroid Build Coastguard Worker // }
35*9880d681SAndroid Build Coastguard Worker // Indeed, the execution looks like Save -> Save -> ... -> Restore -> Restore.
36*9880d681SAndroid Build Coastguard Worker //
37*9880d681SAndroid Build Coastguard Worker // This pass also ensures that the safe points are 3) cheaper than the regular
38*9880d681SAndroid Build Coastguard Worker // entry and exits blocks.
39*9880d681SAndroid Build Coastguard Worker //
40*9880d681SAndroid Build Coastguard Worker // Property #1 is ensured via the use of MachineDominatorTree and
41*9880d681SAndroid Build Coastguard Worker // MachinePostDominatorTree.
42*9880d681SAndroid Build Coastguard Worker // Property #2 is ensured via property #1 and MachineLoopInfo, i.e., both
43*9880d681SAndroid Build Coastguard Worker // points must be in the same loop.
44*9880d681SAndroid Build Coastguard Worker // Property #3 is ensured via the MachineBlockFrequencyInfo.
45*9880d681SAndroid Build Coastguard Worker //
46*9880d681SAndroid Build Coastguard Worker // If this pass found points matching all these properties, then
47*9880d681SAndroid Build Coastguard Worker // MachineFrameInfo is updated with this information.
48*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
49*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/BitVector.h"
50*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/PostOrderIterator.h"
51*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
52*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
53*9880d681SAndroid Build Coastguard Worker // To check for profitability.
54*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
55*9880d681SAndroid Build Coastguard Worker // For property #1 for Save.
56*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
57*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
58*9880d681SAndroid Build Coastguard Worker // To record the result of the analysis.
59*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
60*9880d681SAndroid Build Coastguard Worker // For property #2.
61*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
62*9880d681SAndroid Build Coastguard Worker // For property #1 for Restore.
63*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachinePostDominators.h"
64*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
65*9880d681SAndroid Build Coastguard Worker // To know about callee-saved.
66*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterClassInfo.h"
67*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterScavenging.h"
68*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
69*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
70*9880d681SAndroid Build Coastguard Worker // To query the target about frame lowering.
71*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetFrameLowering.h"
72*9880d681SAndroid Build Coastguard Worker // To know about frame setup operation.
73*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
74*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
75*9880d681SAndroid Build Coastguard Worker // To access TargetInstrInfo.
76*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "shrink-wrap"
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker using namespace llvm;
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker STATISTIC(NumFunc, "Number of functions");
83*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
84*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCandidatesDropped,
85*9880d681SAndroid Build Coastguard Worker           "Number of shrink-wrapping candidates dropped because of frequency");
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker static cl::opt<cl::boolOrDefault>
88*9880d681SAndroid Build Coastguard Worker     EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
89*9880d681SAndroid Build Coastguard Worker                         cl::desc("enable the shrink-wrapping pass"));
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker namespace {
92*9880d681SAndroid Build Coastguard Worker /// \brief Class to determine where the safe point to insert the
93*9880d681SAndroid Build Coastguard Worker /// prologue and epilogue are.
94*9880d681SAndroid Build Coastguard Worker /// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
95*9880d681SAndroid Build Coastguard Worker /// shrink-wrapping term for prologue/epilogue placement, this pass
96*9880d681SAndroid Build Coastguard Worker /// does not rely on expensive data-flow analysis. Instead we use the
97*9880d681SAndroid Build Coastguard Worker /// dominance properties and loop information to decide which point
98*9880d681SAndroid Build Coastguard Worker /// are safe for such insertion.
99*9880d681SAndroid Build Coastguard Worker class ShrinkWrap : public MachineFunctionPass {
100*9880d681SAndroid Build Coastguard Worker   /// Hold callee-saved information.
101*9880d681SAndroid Build Coastguard Worker   RegisterClassInfo RCI;
102*9880d681SAndroid Build Coastguard Worker   MachineDominatorTree *MDT;
103*9880d681SAndroid Build Coastguard Worker   MachinePostDominatorTree *MPDT;
104*9880d681SAndroid Build Coastguard Worker   /// Current safe point found for the prologue.
105*9880d681SAndroid Build Coastguard Worker   /// The prologue will be inserted before the first instruction
106*9880d681SAndroid Build Coastguard Worker   /// in this basic block.
107*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *Save;
108*9880d681SAndroid Build Coastguard Worker   /// Current safe point found for the epilogue.
109*9880d681SAndroid Build Coastguard Worker   /// The epilogue will be inserted before the first terminator instruction
110*9880d681SAndroid Build Coastguard Worker   /// in this basic block.
111*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *Restore;
112*9880d681SAndroid Build Coastguard Worker   /// Hold the information of the basic block frequency.
113*9880d681SAndroid Build Coastguard Worker   /// Use to check the profitability of the new points.
114*9880d681SAndroid Build Coastguard Worker   MachineBlockFrequencyInfo *MBFI;
115*9880d681SAndroid Build Coastguard Worker   /// Hold the loop information. Used to determine if Save and Restore
116*9880d681SAndroid Build Coastguard Worker   /// are in the same loop.
117*9880d681SAndroid Build Coastguard Worker   MachineLoopInfo *MLI;
118*9880d681SAndroid Build Coastguard Worker   /// Frequency of the Entry block.
119*9880d681SAndroid Build Coastguard Worker   uint64_t EntryFreq;
120*9880d681SAndroid Build Coastguard Worker   /// Current opcode for frame setup.
121*9880d681SAndroid Build Coastguard Worker   unsigned FrameSetupOpcode;
122*9880d681SAndroid Build Coastguard Worker   /// Current opcode for frame destroy.
123*9880d681SAndroid Build Coastguard Worker   unsigned FrameDestroyOpcode;
124*9880d681SAndroid Build Coastguard Worker   /// Entry block.
125*9880d681SAndroid Build Coastguard Worker   const MachineBasicBlock *Entry;
126*9880d681SAndroid Build Coastguard Worker   typedef SmallSetVector<unsigned, 16> SetOfRegs;
127*9880d681SAndroid Build Coastguard Worker   /// Registers that need to be saved for the current function.
128*9880d681SAndroid Build Coastguard Worker   mutable SetOfRegs CurrentCSRs;
129*9880d681SAndroid Build Coastguard Worker   /// Current MachineFunction.
130*9880d681SAndroid Build Coastguard Worker   MachineFunction *MachineFunc;
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   /// \brief Check if \p MI uses or defines a callee-saved register or
133*9880d681SAndroid Build Coastguard Worker   /// a frame index. If this is the case, this means \p MI must happen
134*9880d681SAndroid Build Coastguard Worker   /// after Save and before Restore.
135*9880d681SAndroid Build Coastguard Worker   bool useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS) const;
136*9880d681SAndroid Build Coastguard Worker 
getCurrentCSRs(RegScavenger * RS) const137*9880d681SAndroid Build Coastguard Worker   const SetOfRegs &getCurrentCSRs(RegScavenger *RS) const {
138*9880d681SAndroid Build Coastguard Worker     if (CurrentCSRs.empty()) {
139*9880d681SAndroid Build Coastguard Worker       BitVector SavedRegs;
140*9880d681SAndroid Build Coastguard Worker       const TargetFrameLowering *TFI =
141*9880d681SAndroid Build Coastguard Worker           MachineFunc->getSubtarget().getFrameLowering();
142*9880d681SAndroid Build Coastguard Worker 
143*9880d681SAndroid Build Coastguard Worker       TFI->determineCalleeSaves(*MachineFunc, SavedRegs, RS);
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker       for (int Reg = SavedRegs.find_first(); Reg != -1;
146*9880d681SAndroid Build Coastguard Worker            Reg = SavedRegs.find_next(Reg))
147*9880d681SAndroid Build Coastguard Worker         CurrentCSRs.insert((unsigned)Reg);
148*9880d681SAndroid Build Coastguard Worker     }
149*9880d681SAndroid Build Coastguard Worker     return CurrentCSRs;
150*9880d681SAndroid Build Coastguard Worker   }
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker   /// \brief Update the Save and Restore points such that \p MBB is in
153*9880d681SAndroid Build Coastguard Worker   /// the region that is dominated by Save and post-dominated by Restore
154*9880d681SAndroid Build Coastguard Worker   /// and Save and Restore still match the safe point definition.
155*9880d681SAndroid Build Coastguard Worker   /// Such point may not exist and Save and/or Restore may be null after
156*9880d681SAndroid Build Coastguard Worker   /// this call.
157*9880d681SAndroid Build Coastguard Worker   void updateSaveRestorePoints(MachineBasicBlock &MBB, RegScavenger *RS);
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker   /// \brief Initialize the pass for \p MF.
init(MachineFunction & MF)160*9880d681SAndroid Build Coastguard Worker   void init(MachineFunction &MF) {
161*9880d681SAndroid Build Coastguard Worker     RCI.runOnMachineFunction(MF);
162*9880d681SAndroid Build Coastguard Worker     MDT = &getAnalysis<MachineDominatorTree>();
163*9880d681SAndroid Build Coastguard Worker     MPDT = &getAnalysis<MachinePostDominatorTree>();
164*9880d681SAndroid Build Coastguard Worker     Save = nullptr;
165*9880d681SAndroid Build Coastguard Worker     Restore = nullptr;
166*9880d681SAndroid Build Coastguard Worker     MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
167*9880d681SAndroid Build Coastguard Worker     MLI = &getAnalysis<MachineLoopInfo>();
168*9880d681SAndroid Build Coastguard Worker     EntryFreq = MBFI->getEntryFreq();
169*9880d681SAndroid Build Coastguard Worker     const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
170*9880d681SAndroid Build Coastguard Worker     FrameSetupOpcode = TII.getCallFrameSetupOpcode();
171*9880d681SAndroid Build Coastguard Worker     FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
172*9880d681SAndroid Build Coastguard Worker     Entry = &MF.front();
173*9880d681SAndroid Build Coastguard Worker     CurrentCSRs.clear();
174*9880d681SAndroid Build Coastguard Worker     MachineFunc = &MF;
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker     ++NumFunc;
177*9880d681SAndroid Build Coastguard Worker   }
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker   /// Check whether or not Save and Restore points are still interesting for
180*9880d681SAndroid Build Coastguard Worker   /// shrink-wrapping.
ArePointsInteresting() const181*9880d681SAndroid Build Coastguard Worker   bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
182*9880d681SAndroid Build Coastguard Worker 
183*9880d681SAndroid Build Coastguard Worker   /// \brief Check if shrink wrapping is enabled for this target and function.
184*9880d681SAndroid Build Coastguard Worker   static bool isShrinkWrapEnabled(const MachineFunction &MF);
185*9880d681SAndroid Build Coastguard Worker 
186*9880d681SAndroid Build Coastguard Worker public:
187*9880d681SAndroid Build Coastguard Worker   static char ID;
188*9880d681SAndroid Build Coastguard Worker 
ShrinkWrap()189*9880d681SAndroid Build Coastguard Worker   ShrinkWrap() : MachineFunctionPass(ID) {
190*9880d681SAndroid Build Coastguard Worker     initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
191*9880d681SAndroid Build Coastguard Worker   }
192*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const193*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
194*9880d681SAndroid Build Coastguard Worker     AU.setPreservesAll();
195*9880d681SAndroid Build Coastguard Worker     AU.addRequired<MachineBlockFrequencyInfo>();
196*9880d681SAndroid Build Coastguard Worker     AU.addRequired<MachineDominatorTree>();
197*9880d681SAndroid Build Coastguard Worker     AU.addRequired<MachinePostDominatorTree>();
198*9880d681SAndroid Build Coastguard Worker     AU.addRequired<MachineLoopInfo>();
199*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
200*9880d681SAndroid Build Coastguard Worker   }
201*9880d681SAndroid Build Coastguard Worker 
getPassName() const202*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
203*9880d681SAndroid Build Coastguard Worker     return "Shrink Wrapping analysis";
204*9880d681SAndroid Build Coastguard Worker   }
205*9880d681SAndroid Build Coastguard Worker 
206*9880d681SAndroid Build Coastguard Worker   /// \brief Perform the shrink-wrapping analysis and update
207*9880d681SAndroid Build Coastguard Worker   /// the MachineFrameInfo attached to \p MF with the results.
208*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
209*9880d681SAndroid Build Coastguard Worker };
210*9880d681SAndroid Build Coastguard Worker } // End anonymous namespace.
211*9880d681SAndroid Build Coastguard Worker 
212*9880d681SAndroid Build Coastguard Worker char ShrinkWrap::ID = 0;
213*9880d681SAndroid Build Coastguard Worker char &llvm::ShrinkWrapID = ShrinkWrap::ID;
214*9880d681SAndroid Build Coastguard Worker 
215*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false,
216*9880d681SAndroid Build Coastguard Worker                       false)
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)217*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
218*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
219*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
220*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
221*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false, false)
222*9880d681SAndroid Build Coastguard Worker 
223*9880d681SAndroid Build Coastguard Worker bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI,
224*9880d681SAndroid Build Coastguard Worker                                  RegScavenger *RS) const {
225*9880d681SAndroid Build Coastguard Worker   if (MI.getOpcode() == FrameSetupOpcode ||
226*9880d681SAndroid Build Coastguard Worker       MI.getOpcode() == FrameDestroyOpcode) {
227*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
228*9880d681SAndroid Build Coastguard Worker     return true;
229*9880d681SAndroid Build Coastguard Worker   }
230*9880d681SAndroid Build Coastguard Worker   for (const MachineOperand &MO : MI.operands()) {
231*9880d681SAndroid Build Coastguard Worker     bool UseOrDefCSR = false;
232*9880d681SAndroid Build Coastguard Worker     if (MO.isReg()) {
233*9880d681SAndroid Build Coastguard Worker       unsigned PhysReg = MO.getReg();
234*9880d681SAndroid Build Coastguard Worker       if (!PhysReg)
235*9880d681SAndroid Build Coastguard Worker         continue;
236*9880d681SAndroid Build Coastguard Worker       assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
237*9880d681SAndroid Build Coastguard Worker              "Unallocated register?!");
238*9880d681SAndroid Build Coastguard Worker       UseOrDefCSR = RCI.getLastCalleeSavedAlias(PhysReg);
239*9880d681SAndroid Build Coastguard Worker     } else if (MO.isRegMask()) {
240*9880d681SAndroid Build Coastguard Worker       // Check if this regmask clobbers any of the CSRs.
241*9880d681SAndroid Build Coastguard Worker       for (unsigned Reg : getCurrentCSRs(RS)) {
242*9880d681SAndroid Build Coastguard Worker         if (MO.clobbersPhysReg(Reg)) {
243*9880d681SAndroid Build Coastguard Worker           UseOrDefCSR = true;
244*9880d681SAndroid Build Coastguard Worker           break;
245*9880d681SAndroid Build Coastguard Worker         }
246*9880d681SAndroid Build Coastguard Worker       }
247*9880d681SAndroid Build Coastguard Worker     }
248*9880d681SAndroid Build Coastguard Worker     if (UseOrDefCSR || MO.isFI()) {
249*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
250*9880d681SAndroid Build Coastguard Worker                    << MO.isFI() << "): " << MI << '\n');
251*9880d681SAndroid Build Coastguard Worker       return true;
252*9880d681SAndroid Build Coastguard Worker     }
253*9880d681SAndroid Build Coastguard Worker   }
254*9880d681SAndroid Build Coastguard Worker   return false;
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker /// \brief Helper function to find the immediate (post) dominator.
258*9880d681SAndroid Build Coastguard Worker template <typename ListOfBBs, typename DominanceAnalysis>
FindIDom(MachineBasicBlock & Block,ListOfBBs BBs,DominanceAnalysis & Dom)259*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
260*9880d681SAndroid Build Coastguard Worker                             DominanceAnalysis &Dom) {
261*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *IDom = &Block;
262*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock *BB : BBs) {
263*9880d681SAndroid Build Coastguard Worker     IDom = Dom.findNearestCommonDominator(IDom, BB);
264*9880d681SAndroid Build Coastguard Worker     if (!IDom)
265*9880d681SAndroid Build Coastguard Worker       break;
266*9880d681SAndroid Build Coastguard Worker   }
267*9880d681SAndroid Build Coastguard Worker   if (IDom == &Block)
268*9880d681SAndroid Build Coastguard Worker     return nullptr;
269*9880d681SAndroid Build Coastguard Worker   return IDom;
270*9880d681SAndroid Build Coastguard Worker }
271*9880d681SAndroid Build Coastguard Worker 
updateSaveRestorePoints(MachineBasicBlock & MBB,RegScavenger * RS)272*9880d681SAndroid Build Coastguard Worker void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB,
273*9880d681SAndroid Build Coastguard Worker                                          RegScavenger *RS) {
274*9880d681SAndroid Build Coastguard Worker   // Get rid of the easy cases first.
275*9880d681SAndroid Build Coastguard Worker   if (!Save)
276*9880d681SAndroid Build Coastguard Worker     Save = &MBB;
277*9880d681SAndroid Build Coastguard Worker   else
278*9880d681SAndroid Build Coastguard Worker     Save = MDT->findNearestCommonDominator(Save, &MBB);
279*9880d681SAndroid Build Coastguard Worker 
280*9880d681SAndroid Build Coastguard Worker   if (!Save) {
281*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Found a block that is not reachable from Entry\n");
282*9880d681SAndroid Build Coastguard Worker     return;
283*9880d681SAndroid Build Coastguard Worker   }
284*9880d681SAndroid Build Coastguard Worker 
285*9880d681SAndroid Build Coastguard Worker   if (!Restore)
286*9880d681SAndroid Build Coastguard Worker     Restore = &MBB;
287*9880d681SAndroid Build Coastguard Worker   else
288*9880d681SAndroid Build Coastguard Worker     Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
289*9880d681SAndroid Build Coastguard Worker 
290*9880d681SAndroid Build Coastguard Worker   // Make sure we would be able to insert the restore code before the
291*9880d681SAndroid Build Coastguard Worker   // terminator.
292*9880d681SAndroid Build Coastguard Worker   if (Restore == &MBB) {
293*9880d681SAndroid Build Coastguard Worker     for (const MachineInstr &Terminator : MBB.terminators()) {
294*9880d681SAndroid Build Coastguard Worker       if (!useOrDefCSROrFI(Terminator, RS))
295*9880d681SAndroid Build Coastguard Worker         continue;
296*9880d681SAndroid Build Coastguard Worker       // One of the terminator needs to happen before the restore point.
297*9880d681SAndroid Build Coastguard Worker       if (MBB.succ_empty()) {
298*9880d681SAndroid Build Coastguard Worker         Restore = nullptr;
299*9880d681SAndroid Build Coastguard Worker         break;
300*9880d681SAndroid Build Coastguard Worker       }
301*9880d681SAndroid Build Coastguard Worker       // Look for a restore point that post-dominates all the successors.
302*9880d681SAndroid Build Coastguard Worker       // The immediate post-dominator is what we are looking for.
303*9880d681SAndroid Build Coastguard Worker       Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
304*9880d681SAndroid Build Coastguard Worker       break;
305*9880d681SAndroid Build Coastguard Worker     }
306*9880d681SAndroid Build Coastguard Worker   }
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker   if (!Restore) {
309*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Restore point needs to be spanned on several blocks\n");
310*9880d681SAndroid Build Coastguard Worker     return;
311*9880d681SAndroid Build Coastguard Worker   }
312*9880d681SAndroid Build Coastguard Worker 
313*9880d681SAndroid Build Coastguard Worker   // Make sure Save and Restore are suitable for shrink-wrapping:
314*9880d681SAndroid Build Coastguard Worker   // 1. all path from Save needs to lead to Restore before exiting.
315*9880d681SAndroid Build Coastguard Worker   // 2. all path to Restore needs to go through Save from Entry.
316*9880d681SAndroid Build Coastguard Worker   // We achieve that by making sure that:
317*9880d681SAndroid Build Coastguard Worker   // A. Save dominates Restore.
318*9880d681SAndroid Build Coastguard Worker   // B. Restore post-dominates Save.
319*9880d681SAndroid Build Coastguard Worker   // C. Save and Restore are in the same loop.
320*9880d681SAndroid Build Coastguard Worker   bool SaveDominatesRestore = false;
321*9880d681SAndroid Build Coastguard Worker   bool RestorePostDominatesSave = false;
322*9880d681SAndroid Build Coastguard Worker   while (Save && Restore &&
323*9880d681SAndroid Build Coastguard Worker          (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
324*9880d681SAndroid Build Coastguard Worker           !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
325*9880d681SAndroid Build Coastguard Worker           // Post-dominance is not enough in loops to ensure that all uses/defs
326*9880d681SAndroid Build Coastguard Worker           // are after the prologue and before the epilogue at runtime.
327*9880d681SAndroid Build Coastguard Worker           // E.g.,
328*9880d681SAndroid Build Coastguard Worker           // while(1) {
329*9880d681SAndroid Build Coastguard Worker           //  Save
330*9880d681SAndroid Build Coastguard Worker           //  Restore
331*9880d681SAndroid Build Coastguard Worker           //   if (...)
332*9880d681SAndroid Build Coastguard Worker           //     break;
333*9880d681SAndroid Build Coastguard Worker           //  use/def CSRs
334*9880d681SAndroid Build Coastguard Worker           // }
335*9880d681SAndroid Build Coastguard Worker           // All the uses/defs of CSRs are dominated by Save and post-dominated
336*9880d681SAndroid Build Coastguard Worker           // by Restore. However, the CSRs uses are still reachable after
337*9880d681SAndroid Build Coastguard Worker           // Restore and before Save are executed.
338*9880d681SAndroid Build Coastguard Worker           //
339*9880d681SAndroid Build Coastguard Worker           // For now, just push the restore/save points outside of loops.
340*9880d681SAndroid Build Coastguard Worker           // FIXME: Refine the criteria to still find interesting cases
341*9880d681SAndroid Build Coastguard Worker           // for loops.
342*9880d681SAndroid Build Coastguard Worker           MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
343*9880d681SAndroid Build Coastguard Worker     // Fix (A).
344*9880d681SAndroid Build Coastguard Worker     if (!SaveDominatesRestore) {
345*9880d681SAndroid Build Coastguard Worker       Save = MDT->findNearestCommonDominator(Save, Restore);
346*9880d681SAndroid Build Coastguard Worker       continue;
347*9880d681SAndroid Build Coastguard Worker     }
348*9880d681SAndroid Build Coastguard Worker     // Fix (B).
349*9880d681SAndroid Build Coastguard Worker     if (!RestorePostDominatesSave)
350*9880d681SAndroid Build Coastguard Worker       Restore = MPDT->findNearestCommonDominator(Restore, Save);
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker     // Fix (C).
353*9880d681SAndroid Build Coastguard Worker     if (Save && Restore &&
354*9880d681SAndroid Build Coastguard Worker         (MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
355*9880d681SAndroid Build Coastguard Worker       if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
356*9880d681SAndroid Build Coastguard Worker         // Push Save outside of this loop if immediate dominator is different
357*9880d681SAndroid Build Coastguard Worker         // from save block. If immediate dominator is not different, bail out.
358*9880d681SAndroid Build Coastguard Worker         Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
359*9880d681SAndroid Build Coastguard Worker         if (!Save)
360*9880d681SAndroid Build Coastguard Worker           break;
361*9880d681SAndroid Build Coastguard Worker       } else {
362*9880d681SAndroid Build Coastguard Worker         // If the loop does not exit, there is no point in looking
363*9880d681SAndroid Build Coastguard Worker         // for a post-dominator outside the loop.
364*9880d681SAndroid Build Coastguard Worker         SmallVector<MachineBasicBlock*, 4> ExitBlocks;
365*9880d681SAndroid Build Coastguard Worker         MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
366*9880d681SAndroid Build Coastguard Worker         // Push Restore outside of this loop.
367*9880d681SAndroid Build Coastguard Worker         // Look for the immediate post-dominator of the loop exits.
368*9880d681SAndroid Build Coastguard Worker         MachineBasicBlock *IPdom = Restore;
369*9880d681SAndroid Build Coastguard Worker         for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
370*9880d681SAndroid Build Coastguard Worker           IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
371*9880d681SAndroid Build Coastguard Worker           if (!IPdom)
372*9880d681SAndroid Build Coastguard Worker             break;
373*9880d681SAndroid Build Coastguard Worker         }
374*9880d681SAndroid Build Coastguard Worker         // If the immediate post-dominator is not in a less nested loop,
375*9880d681SAndroid Build Coastguard Worker         // then we are stuck in a program with an infinite loop.
376*9880d681SAndroid Build Coastguard Worker         // In that case, we will not find a safe point, hence, bail out.
377*9880d681SAndroid Build Coastguard Worker         if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
378*9880d681SAndroid Build Coastguard Worker           Restore = IPdom;
379*9880d681SAndroid Build Coastguard Worker         else {
380*9880d681SAndroid Build Coastguard Worker           Restore = nullptr;
381*9880d681SAndroid Build Coastguard Worker           break;
382*9880d681SAndroid Build Coastguard Worker         }
383*9880d681SAndroid Build Coastguard Worker       }
384*9880d681SAndroid Build Coastguard Worker     }
385*9880d681SAndroid Build Coastguard Worker   }
386*9880d681SAndroid Build Coastguard Worker }
387*9880d681SAndroid Build Coastguard Worker 
388*9880d681SAndroid Build Coastguard Worker /// Check whether the edge (\p SrcBB, \p DestBB) is a backedge according to MLI.
389*9880d681SAndroid Build Coastguard Worker /// I.e., check if it exists a loop that contains SrcBB and where DestBB is the
390*9880d681SAndroid Build Coastguard Worker /// loop header.
isProperBackedge(const MachineLoopInfo & MLI,const MachineBasicBlock * SrcBB,const MachineBasicBlock * DestBB)391*9880d681SAndroid Build Coastguard Worker static bool isProperBackedge(const MachineLoopInfo &MLI,
392*9880d681SAndroid Build Coastguard Worker                              const MachineBasicBlock *SrcBB,
393*9880d681SAndroid Build Coastguard Worker                              const MachineBasicBlock *DestBB) {
394*9880d681SAndroid Build Coastguard Worker   for (const MachineLoop *Loop = MLI.getLoopFor(SrcBB); Loop;
395*9880d681SAndroid Build Coastguard Worker        Loop = Loop->getParentLoop()) {
396*9880d681SAndroid Build Coastguard Worker     if (Loop->getHeader() == DestBB)
397*9880d681SAndroid Build Coastguard Worker       return true;
398*9880d681SAndroid Build Coastguard Worker   }
399*9880d681SAndroid Build Coastguard Worker   return false;
400*9880d681SAndroid Build Coastguard Worker }
401*9880d681SAndroid Build Coastguard Worker 
402*9880d681SAndroid Build Coastguard Worker /// Check if the CFG of \p MF is irreducible.
isIrreducibleCFG(const MachineFunction & MF,const MachineLoopInfo & MLI)403*9880d681SAndroid Build Coastguard Worker static bool isIrreducibleCFG(const MachineFunction &MF,
404*9880d681SAndroid Build Coastguard Worker                              const MachineLoopInfo &MLI) {
405*9880d681SAndroid Build Coastguard Worker   const MachineBasicBlock *Entry = &*MF.begin();
406*9880d681SAndroid Build Coastguard Worker   ReversePostOrderTraversal<const MachineBasicBlock *> RPOT(Entry);
407*9880d681SAndroid Build Coastguard Worker   BitVector VisitedBB(MF.getNumBlockIDs());
408*9880d681SAndroid Build Coastguard Worker   for (const MachineBasicBlock *MBB : RPOT) {
409*9880d681SAndroid Build Coastguard Worker     VisitedBB.set(MBB->getNumber());
410*9880d681SAndroid Build Coastguard Worker     for (const MachineBasicBlock *SuccBB : MBB->successors()) {
411*9880d681SAndroid Build Coastguard Worker       if (!VisitedBB.test(SuccBB->getNumber()))
412*9880d681SAndroid Build Coastguard Worker         continue;
413*9880d681SAndroid Build Coastguard Worker       // We already visited SuccBB, thus MBB->SuccBB must be a backedge.
414*9880d681SAndroid Build Coastguard Worker       // Check that the head matches what we have in the loop information.
415*9880d681SAndroid Build Coastguard Worker       // Otherwise, we have an irreducible graph.
416*9880d681SAndroid Build Coastguard Worker       if (!isProperBackedge(MLI, MBB, SuccBB))
417*9880d681SAndroid Build Coastguard Worker         return true;
418*9880d681SAndroid Build Coastguard Worker     }
419*9880d681SAndroid Build Coastguard Worker   }
420*9880d681SAndroid Build Coastguard Worker   return false;
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)423*9880d681SAndroid Build Coastguard Worker bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
424*9880d681SAndroid Build Coastguard Worker   if (MF.empty() || !isShrinkWrapEnabled(MF))
425*9880d681SAndroid Build Coastguard Worker     return false;
426*9880d681SAndroid Build Coastguard Worker 
427*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
428*9880d681SAndroid Build Coastguard Worker 
429*9880d681SAndroid Build Coastguard Worker   init(MF);
430*9880d681SAndroid Build Coastguard Worker 
431*9880d681SAndroid Build Coastguard Worker   if (isIrreducibleCFG(MF, *MLI)) {
432*9880d681SAndroid Build Coastguard Worker     // If MF is irreducible, a block may be in a loop without
433*9880d681SAndroid Build Coastguard Worker     // MachineLoopInfo reporting it. I.e., we may use the
434*9880d681SAndroid Build Coastguard Worker     // post-dominance property in loops, which lead to incorrect
435*9880d681SAndroid Build Coastguard Worker     // results. Moreover, we may miss that the prologue and
436*9880d681SAndroid Build Coastguard Worker     // epilogue are not in the same loop, leading to unbalanced
437*9880d681SAndroid Build Coastguard Worker     // construction/deconstruction of the stack frame.
438*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Irreducible CFGs are not supported yet\n");
439*9880d681SAndroid Build Coastguard Worker     return false;
440*9880d681SAndroid Build Coastguard Worker   }
441*9880d681SAndroid Build Coastguard Worker 
442*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
443*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<RegScavenger> RS(
444*9880d681SAndroid Build Coastguard Worker       TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr);
445*9880d681SAndroid Build Coastguard Worker 
446*9880d681SAndroid Build Coastguard Worker   for (MachineBasicBlock &MBB : MF) {
447*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' ' << MBB.getName()
448*9880d681SAndroid Build Coastguard Worker                  << '\n');
449*9880d681SAndroid Build Coastguard Worker 
450*9880d681SAndroid Build Coastguard Worker     if (MBB.isEHFuncletEntry()) {
451*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "EH Funclets are not supported yet.\n");
452*9880d681SAndroid Build Coastguard Worker       return false;
453*9880d681SAndroid Build Coastguard Worker     }
454*9880d681SAndroid Build Coastguard Worker 
455*9880d681SAndroid Build Coastguard Worker     for (const MachineInstr &MI : MBB) {
456*9880d681SAndroid Build Coastguard Worker       if (!useOrDefCSROrFI(MI, RS.get()))
457*9880d681SAndroid Build Coastguard Worker         continue;
458*9880d681SAndroid Build Coastguard Worker       // Save (resp. restore) point must dominate (resp. post dominate)
459*9880d681SAndroid Build Coastguard Worker       // MI. Look for the proper basic block for those.
460*9880d681SAndroid Build Coastguard Worker       updateSaveRestorePoints(MBB, RS.get());
461*9880d681SAndroid Build Coastguard Worker       // If we are at a point where we cannot improve the placement of
462*9880d681SAndroid Build Coastguard Worker       // save/restore instructions, just give up.
463*9880d681SAndroid Build Coastguard Worker       if (!ArePointsInteresting()) {
464*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "No Shrink wrap candidate found\n");
465*9880d681SAndroid Build Coastguard Worker         return false;
466*9880d681SAndroid Build Coastguard Worker       }
467*9880d681SAndroid Build Coastguard Worker       // No need to look for other instructions, this basic block
468*9880d681SAndroid Build Coastguard Worker       // will already be part of the handled region.
469*9880d681SAndroid Build Coastguard Worker       break;
470*9880d681SAndroid Build Coastguard Worker     }
471*9880d681SAndroid Build Coastguard Worker   }
472*9880d681SAndroid Build Coastguard Worker   if (!ArePointsInteresting()) {
473*9880d681SAndroid Build Coastguard Worker     // If the points are not interesting at this point, then they must be null
474*9880d681SAndroid Build Coastguard Worker     // because it means we did not encounter any frame/CSR related code.
475*9880d681SAndroid Build Coastguard Worker     // Otherwise, we would have returned from the previous loop.
476*9880d681SAndroid Build Coastguard Worker     assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
477*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Nothing to shrink-wrap\n");
478*9880d681SAndroid Build Coastguard Worker     return false;
479*9880d681SAndroid Build Coastguard Worker   }
480*9880d681SAndroid Build Coastguard Worker 
481*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
482*9880d681SAndroid Build Coastguard Worker                << '\n');
483*9880d681SAndroid Build Coastguard Worker 
484*9880d681SAndroid Build Coastguard Worker   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
485*9880d681SAndroid Build Coastguard Worker   do {
486*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
487*9880d681SAndroid Build Coastguard Worker                  << Save->getNumber() << ' ' << Save->getName() << ' '
488*9880d681SAndroid Build Coastguard Worker                  << MBFI->getBlockFreq(Save).getFrequency() << "\nRestore: "
489*9880d681SAndroid Build Coastguard Worker                  << Restore->getNumber() << ' ' << Restore->getName() << ' '
490*9880d681SAndroid Build Coastguard Worker                  << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
491*9880d681SAndroid Build Coastguard Worker 
492*9880d681SAndroid Build Coastguard Worker     bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
493*9880d681SAndroid Build Coastguard Worker     if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
494*9880d681SAndroid Build Coastguard Worker          EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
495*9880d681SAndroid Build Coastguard Worker         ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
496*9880d681SAndroid Build Coastguard Worker          TFI->canUseAsEpilogue(*Restore)))
497*9880d681SAndroid Build Coastguard Worker       break;
498*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "New points are too expensive or invalid for the target\n");
499*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *NewBB;
500*9880d681SAndroid Build Coastguard Worker     if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
501*9880d681SAndroid Build Coastguard Worker       Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
502*9880d681SAndroid Build Coastguard Worker       if (!Save)
503*9880d681SAndroid Build Coastguard Worker         break;
504*9880d681SAndroid Build Coastguard Worker       NewBB = Save;
505*9880d681SAndroid Build Coastguard Worker     } else {
506*9880d681SAndroid Build Coastguard Worker       // Restore is expensive.
507*9880d681SAndroid Build Coastguard Worker       Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
508*9880d681SAndroid Build Coastguard Worker       if (!Restore)
509*9880d681SAndroid Build Coastguard Worker         break;
510*9880d681SAndroid Build Coastguard Worker       NewBB = Restore;
511*9880d681SAndroid Build Coastguard Worker     }
512*9880d681SAndroid Build Coastguard Worker     updateSaveRestorePoints(*NewBB, RS.get());
513*9880d681SAndroid Build Coastguard Worker   } while (Save && Restore);
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker   if (!ArePointsInteresting()) {
516*9880d681SAndroid Build Coastguard Worker     ++NumCandidatesDropped;
517*9880d681SAndroid Build Coastguard Worker     return false;
518*9880d681SAndroid Build Coastguard Worker   }
519*9880d681SAndroid Build Coastguard Worker 
520*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: " << Save->getNumber()
521*9880d681SAndroid Build Coastguard Worker                << ' ' << Save->getName() << "\nRestore: "
522*9880d681SAndroid Build Coastguard Worker                << Restore->getNumber() << ' ' << Restore->getName() << '\n');
523*9880d681SAndroid Build Coastguard Worker 
524*9880d681SAndroid Build Coastguard Worker   MachineFrameInfo *MFI = MF.getFrameInfo();
525*9880d681SAndroid Build Coastguard Worker   MFI->setSavePoint(Save);
526*9880d681SAndroid Build Coastguard Worker   MFI->setRestorePoint(Restore);
527*9880d681SAndroid Build Coastguard Worker   ++NumCandidates;
528*9880d681SAndroid Build Coastguard Worker   return false;
529*9880d681SAndroid Build Coastguard Worker }
530*9880d681SAndroid Build Coastguard Worker 
isShrinkWrapEnabled(const MachineFunction & MF)531*9880d681SAndroid Build Coastguard Worker bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
532*9880d681SAndroid Build Coastguard Worker   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
533*9880d681SAndroid Build Coastguard Worker 
534*9880d681SAndroid Build Coastguard Worker   switch (EnableShrinkWrapOpt) {
535*9880d681SAndroid Build Coastguard Worker   case cl::BOU_UNSET:
536*9880d681SAndroid Build Coastguard Worker     return TFI->enableShrinkWrapping(MF) &&
537*9880d681SAndroid Build Coastguard Worker       // Windows with CFI has some limitations that make it impossible
538*9880d681SAndroid Build Coastguard Worker       // to use shrink-wrapping.
539*9880d681SAndroid Build Coastguard Worker       !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() &&
540*9880d681SAndroid Build Coastguard Worker       // Sanitizers look at the value of the stack at the location
541*9880d681SAndroid Build Coastguard Worker       // of the crash. Since a crash can happen anywhere, the
542*9880d681SAndroid Build Coastguard Worker       // frame must be lowered before anything else happen for the
543*9880d681SAndroid Build Coastguard Worker       // sanitizers to be able to get a correct stack frame.
544*9880d681SAndroid Build Coastguard Worker       !(MF.getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
545*9880d681SAndroid Build Coastguard Worker         MF.getFunction()->hasFnAttribute(Attribute::SanitizeThread) ||
546*9880d681SAndroid Build Coastguard Worker         MF.getFunction()->hasFnAttribute(Attribute::SanitizeMemory));
547*9880d681SAndroid Build Coastguard Worker   // If EnableShrinkWrap is set, it takes precedence on whatever the
548*9880d681SAndroid Build Coastguard Worker   // target sets. The rational is that we assume we want to test
549*9880d681SAndroid Build Coastguard Worker   // something related to shrink-wrapping.
550*9880d681SAndroid Build Coastguard Worker   case cl::BOU_TRUE:
551*9880d681SAndroid Build Coastguard Worker     return true;
552*9880d681SAndroid Build Coastguard Worker   case cl::BOU_FALSE:
553*9880d681SAndroid Build Coastguard Worker     return false;
554*9880d681SAndroid Build Coastguard Worker   }
555*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Invalid shrink-wrapping state");
556*9880d681SAndroid Build Coastguard Worker }
557