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