ShrinkWrap.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. const MachineFunction *MF = MI.getParent()->getParent();
  236. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  237. for (const MachineOperand &MO : MI.operands()) {
  238. bool UseOrDefCSR = false;
  239. if (MO.isReg()) {
  240. // Ignore instructions like DBG_VALUE which don't read/def the register.
  241. if (!MO.isDef() && !MO.readsReg())
  242. continue;
  243. Register PhysReg = MO.getReg();
  244. if (!PhysReg)
  245. continue;
  246. assert(Register::isPhysicalRegister(PhysReg) && "Unallocated register?!");
  247. // The stack pointer is not normally described as a callee-saved register
  248. // in calling convention definitions, so we need to watch for it
  249. // separately. An SP mentioned by a call instruction, we can ignore,
  250. // though, as it's harmless and we do not want to effectively disable tail
  251. // calls by forcing the restore point to post-dominate them.
  252. // PPC's LR is also not normally described as a callee-saved register in
  253. // calling convention definitions, so we need to watch for it, too. An LR
  254. // mentioned implicitly by a return (or "branch to link register")
  255. // instruction we can ignore, otherwise we may pessimize shrinkwrapping.
  256. UseOrDefCSR =
  257. (!MI.isCall() && PhysReg == SP) ||
  258. RCI.getLastCalleeSavedAlias(PhysReg) ||
  259. (!MI.isReturn() && TRI->isNonallocatableRegisterCalleeSave(PhysReg));
  260. } else if (MO.isRegMask()) {
  261. // Check if this regmask clobbers any of the CSRs.
  262. for (unsigned Reg : getCurrentCSRs(RS)) {
  263. if (MO.clobbersPhysReg(Reg)) {
  264. UseOrDefCSR = true;
  265. break;
  266. }
  267. }
  268. }
  269. // Skip FrameIndex operands in DBG_VALUE instructions.
  270. if (UseOrDefCSR || (MO.isFI() && !MI.isDebugValue())) {
  271. LLVM_DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
  272. << MO.isFI() << "): " << MI << '\n');
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. /// Helper function to find the immediate (post) dominator.
  279. template <typename ListOfBBs, typename DominanceAnalysis>
  280. static MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
  281. DominanceAnalysis &Dom) {
  282. MachineBasicBlock *IDom = &Block;
  283. for (MachineBasicBlock *BB : BBs) {
  284. IDom = Dom.findNearestCommonDominator(IDom, BB);
  285. if (!IDom)
  286. break;
  287. }
  288. if (IDom == &Block)
  289. return nullptr;
  290. return IDom;
  291. }
  292. void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB,
  293. RegScavenger *RS) {
  294. // Get rid of the easy cases first.
  295. if (!Save)
  296. Save = &MBB;
  297. else
  298. Save = MDT->findNearestCommonDominator(Save, &MBB);
  299. assert(Save);
  300. if (!Restore)
  301. Restore = &MBB;
  302. else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it
  303. // means the block never returns. If that's the
  304. // case, we don't want to call
  305. // `findNearestCommonDominator`, which will
  306. // return `Restore`.
  307. Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
  308. else
  309. Restore = nullptr; // Abort, we can't find a restore point in this case.
  310. // Make sure we would be able to insert the restore code before the
  311. // terminator.
  312. if (Restore == &MBB) {
  313. for (const MachineInstr &Terminator : MBB.terminators()) {
  314. if (!useOrDefCSROrFI(Terminator, RS))
  315. continue;
  316. // One of the terminator needs to happen before the restore point.
  317. if (MBB.succ_empty()) {
  318. Restore = nullptr; // Abort, we can't find a restore point in this case.
  319. break;
  320. }
  321. // Look for a restore point that post-dominates all the successors.
  322. // The immediate post-dominator is what we are looking for.
  323. Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
  324. break;
  325. }
  326. }
  327. if (!Restore) {
  328. LLVM_DEBUG(
  329. dbgs() << "Restore point needs to be spanned on several blocks\n");
  330. return;
  331. }
  332. // Make sure Save and Restore are suitable for shrink-wrapping:
  333. // 1. all path from Save needs to lead to Restore before exiting.
  334. // 2. all path to Restore needs to go through Save from Entry.
  335. // We achieve that by making sure that:
  336. // A. Save dominates Restore.
  337. // B. Restore post-dominates Save.
  338. // C. Save and Restore are in the same loop.
  339. bool SaveDominatesRestore = false;
  340. bool RestorePostDominatesSave = false;
  341. while (Restore &&
  342. (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
  343. !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
  344. // Post-dominance is not enough in loops to ensure that all uses/defs
  345. // are after the prologue and before the epilogue at runtime.
  346. // E.g.,
  347. // while(1) {
  348. // Save
  349. // Restore
  350. // if (...)
  351. // break;
  352. // use/def CSRs
  353. // }
  354. // All the uses/defs of CSRs are dominated by Save and post-dominated
  355. // by Restore. However, the CSRs uses are still reachable after
  356. // Restore and before Save are executed.
  357. //
  358. // For now, just push the restore/save points outside of loops.
  359. // FIXME: Refine the criteria to still find interesting cases
  360. // for loops.
  361. MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
  362. // Fix (A).
  363. if (!SaveDominatesRestore) {
  364. Save = MDT->findNearestCommonDominator(Save, Restore);
  365. continue;
  366. }
  367. // Fix (B).
  368. if (!RestorePostDominatesSave)
  369. Restore = MPDT->findNearestCommonDominator(Restore, Save);
  370. // Fix (C).
  371. if (Restore && (MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) {
  372. if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
  373. // Push Save outside of this loop if immediate dominator is different
  374. // from save block. If immediate dominator is not different, bail out.
  375. Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
  376. if (!Save)
  377. break;
  378. } else {
  379. // If the loop does not exit, there is no point in looking
  380. // for a post-dominator outside the loop.
  381. SmallVector<MachineBasicBlock*, 4> ExitBlocks;
  382. MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
  383. // Push Restore outside of this loop.
  384. // Look for the immediate post-dominator of the loop exits.
  385. MachineBasicBlock *IPdom = Restore;
  386. for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
  387. IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
  388. if (!IPdom)
  389. break;
  390. }
  391. // If the immediate post-dominator is not in a less nested loop,
  392. // then we are stuck in a program with an infinite loop.
  393. // In that case, we will not find a safe point, hence, bail out.
  394. if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
  395. Restore = IPdom;
  396. else {
  397. Restore = nullptr;
  398. break;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE,
  405. StringRef RemarkName, StringRef RemarkMessage,
  406. const DiagnosticLocation &Loc,
  407. const MachineBasicBlock *MBB) {
  408. ORE->emit([&]() {
  409. return MachineOptimizationRemarkMissed(DEBUG_TYPE, RemarkName, Loc, MBB)
  410. << RemarkMessage;
  411. });
  412. LLVM_DEBUG(dbgs() << RemarkMessage << '\n');
  413. return false;
  414. }
  415. bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
  416. if (skipFunction(MF.getFunction()) || MF.empty() || !isShrinkWrapEnabled(MF))
  417. return false;
  418. LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
  419. init(MF);
  420. ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
  421. if (containsIrreducibleCFG<MachineBasicBlock *>(RPOT, *MLI)) {
  422. // If MF is irreducible, a block may be in a loop without
  423. // MachineLoopInfo reporting it. I.e., we may use the
  424. // post-dominance property in loops, which lead to incorrect
  425. // results. Moreover, we may miss that the prologue and
  426. // epilogue are not in the same loop, leading to unbalanced
  427. // construction/deconstruction of the stack frame.
  428. return giveUpWithRemarks(ORE, "UnsupportedIrreducibleCFG",
  429. "Irreducible CFGs are not supported yet.",
  430. MF.getFunction().getSubprogram(), &MF.front());
  431. }
  432. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  433. std::unique_ptr<RegScavenger> RS(
  434. TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr);
  435. for (MachineBasicBlock &MBB : MF) {
  436. LLVM_DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' '
  437. << MBB.getName() << '\n');
  438. if (MBB.isEHFuncletEntry())
  439. return giveUpWithRemarks(ORE, "UnsupportedEHFunclets",
  440. "EH Funclets are not supported yet.",
  441. MBB.front().getDebugLoc(), &MBB);
  442. if (MBB.isEHPad() || MBB.isInlineAsmBrIndirectTarget()) {
  443. // Push the prologue and epilogue outside of the region that may throw (or
  444. // jump out via inlineasm_br), by making sure that all the landing pads
  445. // are at least at the boundary of the save and restore points. The
  446. // problem is that a basic block can jump out from the middle in these
  447. // cases, which we do not handle.
  448. updateSaveRestorePoints(MBB, RS.get());
  449. if (!ArePointsInteresting()) {
  450. LLVM_DEBUG(dbgs() << "EHPad/inlineasm_br prevents shrink-wrapping\n");
  451. return false;
  452. }
  453. continue;
  454. }
  455. for (const MachineInstr &MI : MBB) {
  456. if (!useOrDefCSROrFI(MI, RS.get()))
  457. continue;
  458. // Save (resp. restore) point must dominate (resp. post dominate)
  459. // MI. Look for the proper basic block for those.
  460. updateSaveRestorePoints(MBB, RS.get());
  461. // If we are at a point where we cannot improve the placement of
  462. // save/restore instructions, just give up.
  463. if (!ArePointsInteresting()) {
  464. LLVM_DEBUG(dbgs() << "No Shrink wrap candidate found\n");
  465. return false;
  466. }
  467. // No need to look for other instructions, this basic block
  468. // will already be part of the handled region.
  469. break;
  470. }
  471. }
  472. if (!ArePointsInteresting()) {
  473. // If the points are not interesting at this point, then they must be null
  474. // because it means we did not encounter any frame/CSR related code.
  475. // Otherwise, we would have returned from the previous loop.
  476. assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
  477. LLVM_DEBUG(dbgs() << "Nothing to shrink-wrap\n");
  478. return false;
  479. }
  480. LLVM_DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
  481. << '\n');
  482. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  483. do {
  484. LLVM_DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
  485. << Save->getNumber() << ' ' << Save->getName() << ' '
  486. << MBFI->getBlockFreq(Save).getFrequency()
  487. << "\nRestore: " << Restore->getNumber() << ' '
  488. << Restore->getName() << ' '
  489. << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
  490. bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
  491. if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
  492. EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
  493. ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
  494. TFI->canUseAsEpilogue(*Restore)))
  495. break;
  496. LLVM_DEBUG(
  497. dbgs() << "New points are too expensive or invalid for the target\n");
  498. MachineBasicBlock *NewBB;
  499. if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
  500. Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
  501. if (!Save)
  502. break;
  503. NewBB = Save;
  504. } else {
  505. // Restore is expensive.
  506. Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
  507. if (!Restore)
  508. break;
  509. NewBB = Restore;
  510. }
  511. updateSaveRestorePoints(*NewBB, RS.get());
  512. } while (Save && Restore);
  513. if (!ArePointsInteresting()) {
  514. ++NumCandidatesDropped;
  515. return false;
  516. }
  517. LLVM_DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: "
  518. << Save->getNumber() << ' ' << Save->getName()
  519. << "\nRestore: " << Restore->getNumber() << ' '
  520. << Restore->getName() << '\n');
  521. MachineFrameInfo &MFI = MF.getFrameInfo();
  522. MFI.setSavePoint(Save);
  523. MFI.setRestorePoint(Restore);
  524. ++NumCandidates;
  525. return false;
  526. }
  527. bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
  528. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  529. switch (EnableShrinkWrapOpt) {
  530. case cl::BOU_UNSET:
  531. return TFI->enableShrinkWrapping(MF) &&
  532. // Windows with CFI has some limitations that make it impossible
  533. // to use shrink-wrapping.
  534. !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() &&
  535. // Sanitizers look at the value of the stack at the location
  536. // of the crash. Since a crash can happen anywhere, the
  537. // frame must be lowered before anything else happen for the
  538. // sanitizers to be able to get a correct stack frame.
  539. !(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) ||
  540. MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) ||
  541. MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) ||
  542. MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress));
  543. // If EnableShrinkWrap is set, it takes precedence on whatever the
  544. // target sets. The rational is that we assume we want to test
  545. // something related to shrink-wrapping.
  546. case cl::BOU_TRUE:
  547. return true;
  548. case cl::BOU_FALSE:
  549. return false;
  550. }
  551. llvm_unreachable("Invalid shrink-wrapping state");
  552. }