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