ShrinkWrap.cpp 23 KB

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