1 //===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===// 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 #include "llvm/Analysis/MustExecute.h" 10 #include "llvm/ADT/PostOrderIterator.h" 11 #include "llvm/Analysis/CFG.h" 12 #include "llvm/Analysis/InstructionSimplify.h" 13 #include "llvm/Analysis/LoopInfo.h" 14 #include "llvm/Analysis/Passes.h" 15 #include "llvm/Analysis/PostDominators.h" 16 #include "llvm/Analysis/ValueTracking.h" 17 #include "llvm/IR/AssemblyAnnotationWriter.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/Dominators.h" 20 #include "llvm/IR/InstIterator.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/IR/PassManager.h" 24 #include "llvm/InitializePasses.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/FormattedStream.h" 27 #include "llvm/Support/raw_ostream.h" 28 29 using namespace llvm; 30 31 #define DEBUG_TYPE "must-execute" 32 33 const DenseMap<BasicBlock *, ColorVector> & 34 LoopSafetyInfo::getBlockColors() const { 35 return BlockColors; 36 } 37 38 void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) { 39 ColorVector &ColorsForNewBlock = BlockColors[New]; 40 ColorVector &ColorsForOldBlock = BlockColors[Old]; 41 ColorsForNewBlock = ColorsForOldBlock; 42 } 43 44 bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const { 45 (void)BB; 46 return anyBlockMayThrow(); 47 } 48 49 bool SimpleLoopSafetyInfo::anyBlockMayThrow() const { 50 return MayThrow; 51 } 52 53 void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) { 54 assert(CurLoop != nullptr && "CurLoop can't be null"); 55 BasicBlock *Header = CurLoop->getHeader(); 56 // Iterate over header and compute safety info. 57 HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header); 58 MayThrow = HeaderMayThrow; 59 // Iterate over loop instructions and compute safety info. 60 // Skip header as it has been computed and stored in HeaderMayThrow. 61 // The first block in loopinfo.Blocks is guaranteed to be the header. 62 assert(Header == *CurLoop->getBlocks().begin() && 63 "First block must be header"); 64 for (Loop::block_iterator BB = std::next(CurLoop->block_begin()), 65 BBE = CurLoop->block_end(); 66 (BB != BBE) && !MayThrow; ++BB) 67 MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB); 68 69 computeBlockColors(CurLoop); 70 } 71 72 bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const { 73 return ICF.hasICF(BB); 74 } 75 76 bool ICFLoopSafetyInfo::anyBlockMayThrow() const { 77 return MayThrow; 78 } 79 80 void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) { 81 assert(CurLoop != nullptr && "CurLoop can't be null"); 82 ICF.clear(); 83 MW.clear(); 84 MayThrow = false; 85 // Figure out the fact that at least one block may throw. 86 for (auto &BB : CurLoop->blocks()) 87 if (ICF.hasICF(&*BB)) { 88 MayThrow = true; 89 break; 90 } 91 computeBlockColors(CurLoop); 92 } 93 94 void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst, 95 const BasicBlock *BB) { 96 ICF.insertInstructionTo(Inst, BB); 97 MW.insertInstructionTo(Inst, BB); 98 } 99 100 void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) { 101 ICF.removeInstruction(Inst); 102 MW.removeInstruction(Inst); 103 } 104 105 void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) { 106 // Compute funclet colors if we might sink/hoist in a function with a funclet 107 // personality routine. 108 Function *Fn = CurLoop->getHeader()->getParent(); 109 if (Fn->hasPersonalityFn()) 110 if (Constant *PersonalityFn = Fn->getPersonalityFn()) 111 if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn))) 112 BlockColors = colorEHFunclets(*Fn); 113 } 114 115 /// Return true if we can prove that the given ExitBlock is not reached on the 116 /// first iteration of the given loop. That is, the backedge of the loop must 117 /// be executed before the ExitBlock is executed in any dynamic execution trace. 118 static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock, 119 const DominatorTree *DT, 120 const Loop *CurLoop) { 121 auto *CondExitBlock = ExitBlock->getSinglePredecessor(); 122 if (!CondExitBlock) 123 // expect unique exits 124 return false; 125 assert(CurLoop->contains(CondExitBlock) && "meaning of exit block"); 126 auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator()); 127 if (!BI || !BI->isConditional()) 128 return false; 129 // If condition is constant and false leads to ExitBlock then we always 130 // execute the true branch. 131 if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) 132 return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock; 133 auto *Cond = dyn_cast<CmpInst>(BI->getCondition()); 134 if (!Cond) 135 return false; 136 // todo: this would be a lot more powerful if we used scev, but all the 137 // plumbing is currently missing to pass a pointer in from the pass 138 // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known 139 auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0)); 140 auto *RHS = Cond->getOperand(1); 141 if (!LHS || LHS->getParent() != CurLoop->getHeader()) 142 return false; 143 auto DL = ExitBlock->getModule()->getDataLayout(); 144 auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader()); 145 auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(), 146 IVStart, RHS, 147 {DL, /*TLI*/ nullptr, 148 DT, /*AC*/ nullptr, BI}); 149 auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull); 150 if (!SimpleCst) 151 return false; 152 if (ExitBlock == BI->getSuccessor(0)) 153 return SimpleCst->isZeroValue(); 154 assert(ExitBlock == BI->getSuccessor(1) && "implied by above"); 155 return SimpleCst->isAllOnesValue(); 156 } 157 158 /// Collect all blocks from \p CurLoop which lie on all possible paths from 159 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set 160 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty. 161 static void collectTransitivePredecessors( 162 const Loop *CurLoop, const BasicBlock *BB, 163 SmallPtrSetImpl<const BasicBlock *> &Predecessors) { 164 assert(Predecessors.empty() && "Garbage in predecessors set?"); 165 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 166 if (BB == CurLoop->getHeader()) 167 return; 168 SmallVector<const BasicBlock *, 4> WorkList; 169 for (auto *Pred : predecessors(BB)) { 170 Predecessors.insert(Pred); 171 WorkList.push_back(Pred); 172 } 173 while (!WorkList.empty()) { 174 auto *Pred = WorkList.pop_back_val(); 175 assert(CurLoop->contains(Pred) && "Should only reach loop blocks!"); 176 // We are not interested in backedges and we don't want to leave loop. 177 if (Pred == CurLoop->getHeader()) 178 continue; 179 // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all 180 // blocks of this inner loop, even those that are always executed AFTER the 181 // BB. It may make our analysis more conservative than it could be, see test 182 // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll. 183 // We can ignore backedge of all loops containing BB to get a sligtly more 184 // optimistic result. 185 for (auto *PredPred : predecessors(Pred)) 186 if (Predecessors.insert(PredPred).second) 187 WorkList.push_back(PredPred); 188 } 189 } 190 191 bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop, 192 const BasicBlock *BB, 193 const DominatorTree *DT) const { 194 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 195 196 // Fast path: header is always reached once the loop is entered. 197 if (BB == CurLoop->getHeader()) 198 return true; 199 200 // Collect all transitive predecessors of BB in the same loop. This set will 201 // be a subset of the blocks within the loop. 202 SmallPtrSet<const BasicBlock *, 4> Predecessors; 203 collectTransitivePredecessors(CurLoop, BB, Predecessors); 204 205 // Make sure that all successors of, all predecessors of BB which are not 206 // dominated by BB, are either: 207 // 1) BB, 208 // 2) Also predecessors of BB, 209 // 3) Exit blocks which are not taken on 1st iteration. 210 // Memoize blocks we've already checked. 211 SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors; 212 for (auto *Pred : Predecessors) { 213 // Predecessor block may throw, so it has a side exit. 214 if (blockMayThrow(Pred)) 215 return false; 216 217 // BB dominates Pred, so if Pred runs, BB must run. 218 // This is true when Pred is a loop latch. 219 if (DT->dominates(BB, Pred)) 220 continue; 221 222 for (auto *Succ : successors(Pred)) 223 if (CheckedSuccessors.insert(Succ).second && 224 Succ != BB && !Predecessors.count(Succ)) 225 // By discharging conditions that are not executed on the 1st iteration, 226 // we guarantee that *at least* on the first iteration all paths from 227 // header that *may* execute will lead us to the block of interest. So 228 // that if we had virtually peeled one iteration away, in this peeled 229 // iteration the set of predecessors would contain only paths from 230 // header to BB without any exiting edges that may execute. 231 // 232 // TODO: We only do it for exiting edges currently. We could use the 233 // same function to skip some of the edges within the loop if we know 234 // that they will not be taken on the 1st iteration. 235 // 236 // TODO: If we somehow know the number of iterations in loop, the same 237 // check may be done for any arbitrary N-th iteration as long as N is 238 // not greater than minimum number of iterations in this loop. 239 if (CurLoop->contains(Succ) || 240 !CanProveNotTakenFirstIteration(Succ, DT, CurLoop)) 241 return false; 242 } 243 244 // All predecessors can only lead us to BB. 245 return true; 246 } 247 248 /// Returns true if the instruction in a loop is guaranteed to execute at least 249 /// once. 250 bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst, 251 const DominatorTree *DT, 252 const Loop *CurLoop) const { 253 // If the instruction is in the header block for the loop (which is very 254 // common), it is always guaranteed to dominate the exit blocks. Since this 255 // is a common case, and can save some work, check it now. 256 if (Inst.getParent() == CurLoop->getHeader()) 257 // If there's a throw in the header block, we can't guarantee we'll reach 258 // Inst unless we can prove that Inst comes before the potential implicit 259 // exit. At the moment, we use a (cheap) hack for the common case where 260 // the instruction of interest is the first one in the block. 261 return !HeaderMayThrow || 262 Inst.getParent()->getFirstNonPHIOrDbg() == &Inst; 263 264 // If there is a path from header to exit or latch that doesn't lead to our 265 // instruction's block, return false. 266 return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT); 267 } 268 269 bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst, 270 const DominatorTree *DT, 271 const Loop *CurLoop) const { 272 return !ICF.isDominatedByICFIFromSameBlock(&Inst) && 273 allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT); 274 } 275 276 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB, 277 const Loop *CurLoop) const { 278 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 279 280 // Fast path: there are no instructions before header. 281 if (BB == CurLoop->getHeader()) 282 return true; 283 284 // Collect all transitive predecessors of BB in the same loop. This set will 285 // be a subset of the blocks within the loop. 286 SmallPtrSet<const BasicBlock *, 4> Predecessors; 287 collectTransitivePredecessors(CurLoop, BB, Predecessors); 288 // Find if there any instruction in either predecessor that could write 289 // to memory. 290 for (auto *Pred : Predecessors) 291 if (MW.mayWriteToMemory(Pred)) 292 return false; 293 return true; 294 } 295 296 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I, 297 const Loop *CurLoop) const { 298 auto *BB = I.getParent(); 299 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 300 return !MW.isDominatedByMemoryWriteFromSameBlock(&I) && 301 doesNotWriteMemoryBefore(BB, CurLoop); 302 } 303 304 namespace { 305 struct MustExecutePrinter : public FunctionPass { 306 307 static char ID; // Pass identification, replacement for typeid 308 MustExecutePrinter() : FunctionPass(ID) { 309 initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry()); 310 } 311 void getAnalysisUsage(AnalysisUsage &AU) const override { 312 AU.setPreservesAll(); 313 AU.addRequired<DominatorTreeWrapperPass>(); 314 AU.addRequired<LoopInfoWrapperPass>(); 315 } 316 bool runOnFunction(Function &F) override; 317 }; 318 struct MustBeExecutedContextPrinter : public ModulePass { 319 static char ID; 320 321 MustBeExecutedContextPrinter() : ModulePass(ID) { 322 initializeMustBeExecutedContextPrinterPass( 323 *PassRegistry::getPassRegistry()); 324 } 325 void getAnalysisUsage(AnalysisUsage &AU) const override { 326 AU.setPreservesAll(); 327 } 328 bool runOnModule(Module &M) override; 329 }; 330 } 331 332 char MustExecutePrinter::ID = 0; 333 INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute", 334 "Instructions which execute on loop entry", false, true) 335 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 336 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 337 INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute", 338 "Instructions which execute on loop entry", false, true) 339 340 FunctionPass *llvm::createMustExecutePrinter() { 341 return new MustExecutePrinter(); 342 } 343 344 char MustBeExecutedContextPrinter::ID = 0; 345 INITIALIZE_PASS_BEGIN(MustBeExecutedContextPrinter, 346 "print-must-be-executed-contexts", 347 "print the must-be-executed-context for all instructions", 348 false, true) 349 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) 350 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 351 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 352 INITIALIZE_PASS_END(MustBeExecutedContextPrinter, 353 "print-must-be-executed-contexts", 354 "print the must-be-executed-context for all instructions", 355 false, true) 356 357 ModulePass *llvm::createMustBeExecutedContextPrinter() { 358 return new MustBeExecutedContextPrinter(); 359 } 360 361 bool MustBeExecutedContextPrinter::runOnModule(Module &M) { 362 // We provide non-PM analysis here because the old PM doesn't like to query 363 // function passes from a module pass. 364 SmallVector<std::unique_ptr<PostDominatorTree>, 8> PDTs; 365 SmallVector<std::unique_ptr<DominatorTree>, 8> DTs; 366 SmallVector<std::unique_ptr<LoopInfo>, 8> LIs; 367 368 GetterTy<LoopInfo> LIGetter = [&](const Function &F) { 369 DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function &>(F))); 370 LIs.push_back(std::make_unique<LoopInfo>(*DTs.back())); 371 return LIs.back().get(); 372 }; 373 GetterTy<DominatorTree> DTGetter = [&](const Function &F) { 374 DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function&>(F))); 375 return DTs.back().get(); 376 }; 377 GetterTy<PostDominatorTree> PDTGetter = [&](const Function &F) { 378 PDTs.push_back( 379 std::make_unique<PostDominatorTree>(const_cast<Function &>(F))); 380 return PDTs.back().get(); 381 }; 382 MustBeExecutedContextExplorer Explorer( 383 /* ExploreInterBlock */ true, 384 /* ExploreCFGForward */ true, 385 /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter); 386 387 for (Function &F : M) { 388 for (Instruction &I : instructions(F)) { 389 dbgs() << "-- Explore context of: " << I << "\n"; 390 for (const Instruction *CI : Explorer.range(&I)) 391 dbgs() << " [F: " << CI->getFunction()->getName() << "] " << *CI 392 << "\n"; 393 } 394 } 395 396 return false; 397 } 398 399 static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) { 400 // TODO: merge these two routines. For the moment, we display the best 401 // result obtained by *either* implementation. This is a bit unfair since no 402 // caller actually gets the full power at the moment. 403 SimpleLoopSafetyInfo LSI; 404 LSI.computeLoopSafetyInfo(L); 405 return LSI.isGuaranteedToExecute(I, DT, L) || 406 isGuaranteedToExecuteForEveryIteration(&I, L); 407 } 408 409 namespace { 410 /// An assembly annotator class to print must execute information in 411 /// comments. 412 class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter { 413 DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec; 414 415 public: 416 MustExecuteAnnotatedWriter(const Function &F, 417 DominatorTree &DT, LoopInfo &LI) { 418 for (auto &I: instructions(F)) { 419 Loop *L = LI.getLoopFor(I.getParent()); 420 while (L) { 421 if (isMustExecuteIn(I, L, &DT)) { 422 MustExec[&I].push_back(L); 423 } 424 L = L->getParentLoop(); 425 }; 426 } 427 } 428 MustExecuteAnnotatedWriter(const Module &M, 429 DominatorTree &DT, LoopInfo &LI) { 430 for (auto &F : M) 431 for (auto &I: instructions(F)) { 432 Loop *L = LI.getLoopFor(I.getParent()); 433 while (L) { 434 if (isMustExecuteIn(I, L, &DT)) { 435 MustExec[&I].push_back(L); 436 } 437 L = L->getParentLoop(); 438 }; 439 } 440 } 441 442 443 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 444 if (!MustExec.count(&V)) 445 return; 446 447 const auto &Loops = MustExec.lookup(&V); 448 const auto NumLoops = Loops.size(); 449 if (NumLoops > 1) 450 OS << " ; (mustexec in " << NumLoops << " loops: "; 451 else 452 OS << " ; (mustexec in: "; 453 454 bool first = true; 455 for (const Loop *L : Loops) { 456 if (!first) 457 OS << ", "; 458 first = false; 459 OS << L->getHeader()->getName(); 460 } 461 OS << ")"; 462 } 463 }; 464 } // namespace 465 466 bool MustExecutePrinter::runOnFunction(Function &F) { 467 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 468 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 469 470 MustExecuteAnnotatedWriter Writer(F, DT, LI); 471 F.print(dbgs(), &Writer); 472 473 return false; 474 } 475 476 /// Return true if \p L might be an endless loop. 477 static bool maybeEndlessLoop(const Loop &L) { 478 if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn)) 479 return false; 480 // TODO: Actually try to prove it is not. 481 // TODO: If maybeEndlessLoop is going to be expensive, cache it. 482 return true; 483 } 484 485 bool llvm::mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) { 486 if (!LI) 487 return false; 488 using RPOTraversal = ReversePostOrderTraversal<const Function *>; 489 RPOTraversal FuncRPOT(&F); 490 return containsIrreducibleCFG<const BasicBlock *, const RPOTraversal, 491 const LoopInfo>(FuncRPOT, *LI); 492 } 493 494 /// Lookup \p Key in \p Map and return the result, potentially after 495 /// initializing the optional through \p Fn(\p args). 496 template <typename K, typename V, typename FnTy, typename... ArgsTy> 497 static V getOrCreateCachedOptional(K Key, DenseMap<K, Optional<V>> &Map, 498 FnTy &&Fn, ArgsTy&&... args) { 499 Optional<V> &OptVal = Map[Key]; 500 if (!OptVal.hasValue()) 501 OptVal = Fn(std::forward<ArgsTy>(args)...); 502 return OptVal.getValue(); 503 } 504 505 const BasicBlock * 506 MustBeExecutedContextExplorer::findForwardJoinPoint(const BasicBlock *InitBB) { 507 const LoopInfo *LI = LIGetter(*InitBB->getParent()); 508 const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent()); 509 510 LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName() 511 << (LI ? " [LI]" : "") << (PDT ? " [PDT]" : "")); 512 513 const Function &F = *InitBB->getParent(); 514 const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr; 515 const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB; 516 bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) || 517 (L && !maybeEndlessLoop(*L))) && 518 F.doesNotThrow(); 519 LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "") 520 << (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "") 521 << "\n"); 522 523 // Determine the adjacent blocks in the given direction but exclude (self) 524 // loops under certain circumstances. 525 SmallVector<const BasicBlock *, 8> Worklist; 526 for (const BasicBlock *SuccBB : successors(InitBB)) { 527 bool IsLatch = SuccBB == HeaderBB; 528 // Loop latches are ignored in forward propagation if the loop cannot be 529 // endless and may not throw: control has to go somewhere. 530 if (!WillReturnAndNoThrow || !IsLatch) 531 Worklist.push_back(SuccBB); 532 } 533 LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n"); 534 535 // If there are no other adjacent blocks, there is no join point. 536 if (Worklist.empty()) 537 return nullptr; 538 539 // If there is one adjacent block, it is the join point. 540 if (Worklist.size() == 1) 541 return Worklist[0]; 542 543 // Try to determine a join block through the help of the post-dominance 544 // tree. If no tree was provided, we perform simple pattern matching for one 545 // block conditionals and one block loops only. 546 const BasicBlock *JoinBB = nullptr; 547 if (PDT) 548 if (const auto *InitNode = PDT->getNode(InitBB)) 549 if (const auto *IDomNode = InitNode->getIDom()) 550 JoinBB = IDomNode->getBlock(); 551 552 if (!JoinBB && Worklist.size() == 2) { 553 const BasicBlock *Succ0 = Worklist[0]; 554 const BasicBlock *Succ1 = Worklist[1]; 555 const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor(); 556 const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor(); 557 if (Succ0UniqueSucc == InitBB) { 558 // InitBB -> Succ0 -> InitBB 559 // InitBB -> Succ1 = JoinBB 560 JoinBB = Succ1; 561 } else if (Succ1UniqueSucc == InitBB) { 562 // InitBB -> Succ1 -> InitBB 563 // InitBB -> Succ0 = JoinBB 564 JoinBB = Succ0; 565 } else if (Succ0 == Succ1UniqueSucc) { 566 // InitBB -> Succ0 = JoinBB 567 // InitBB -> Succ1 -> Succ0 = JoinBB 568 JoinBB = Succ0; 569 } else if (Succ1 == Succ0UniqueSucc) { 570 // InitBB -> Succ0 -> Succ1 = JoinBB 571 // InitBB -> Succ1 = JoinBB 572 JoinBB = Succ1; 573 } else if (Succ0UniqueSucc == Succ1UniqueSucc) { 574 // InitBB -> Succ0 -> JoinBB 575 // InitBB -> Succ1 -> JoinBB 576 JoinBB = Succ0UniqueSucc; 577 } 578 } 579 580 if (!JoinBB && L) 581 JoinBB = L->getUniqueExitBlock(); 582 583 if (!JoinBB) 584 return nullptr; 585 586 LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n"); 587 588 // In forward direction we check if control will for sure reach JoinBB from 589 // InitBB, thus it can not be "stopped" along the way. Ways to "stop" control 590 // are: infinite loops and instructions that do not necessarily transfer 591 // execution to their successor. To check for them we traverse the CFG from 592 // the adjacent blocks to the JoinBB, looking at all intermediate blocks. 593 594 // If we know the function is "will-return" and "no-throw" there is no need 595 // for futher checks. 596 if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) { 597 598 auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) { 599 return isGuaranteedToTransferExecutionToSuccessor(BB); 600 }; 601 602 SmallPtrSet<const BasicBlock *, 16> Visited; 603 while (!Worklist.empty()) { 604 const BasicBlock *ToBB = Worklist.pop_back_val(); 605 if (ToBB == JoinBB) 606 continue; 607 608 // Make sure all loops in-between are finite. 609 if (!Visited.insert(ToBB).second) { 610 if (!F.hasFnAttribute(Attribute::WillReturn)) { 611 if (!LI) 612 return nullptr; 613 614 bool MayContainIrreducibleControl = getOrCreateCachedOptional( 615 &F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI); 616 if (MayContainIrreducibleControl) 617 return nullptr; 618 619 const Loop *L = LI->getLoopFor(ToBB); 620 if (L && maybeEndlessLoop(*L)) 621 return nullptr; 622 } 623 624 continue; 625 } 626 627 // Make sure the block has no instructions that could stop control 628 // transfer. 629 bool TransfersExecution = getOrCreateCachedOptional( 630 ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB); 631 if (!TransfersExecution) 632 return nullptr; 633 634 append_range(Worklist, successors(ToBB)); 635 } 636 } 637 638 LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n"); 639 return JoinBB; 640 } 641 const BasicBlock * 642 MustBeExecutedContextExplorer::findBackwardJoinPoint(const BasicBlock *InitBB) { 643 const LoopInfo *LI = LIGetter(*InitBB->getParent()); 644 const DominatorTree *DT = DTGetter(*InitBB->getParent()); 645 LLVM_DEBUG(dbgs() << "\tFind backward join point for " << InitBB->getName() 646 << (LI ? " [LI]" : "") << (DT ? " [DT]" : "")); 647 648 // Try to determine a join block through the help of the dominance tree. If no 649 // tree was provided, we perform simple pattern matching for one block 650 // conditionals only. 651 if (DT) 652 if (const auto *InitNode = DT->getNode(InitBB)) 653 if (const auto *IDomNode = InitNode->getIDom()) 654 return IDomNode->getBlock(); 655 656 const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr; 657 const BasicBlock *HeaderBB = L ? L->getHeader() : nullptr; 658 659 // Determine the predecessor blocks but ignore backedges. 660 SmallVector<const BasicBlock *, 8> Worklist; 661 for (const BasicBlock *PredBB : predecessors(InitBB)) { 662 bool IsBackedge = 663 (PredBB == InitBB) || (HeaderBB == InitBB && L->contains(PredBB)); 664 // Loop backedges are ignored in backwards propagation: control has to come 665 // from somewhere. 666 if (!IsBackedge) 667 Worklist.push_back(PredBB); 668 } 669 670 // If there are no other predecessor blocks, there is no join point. 671 if (Worklist.empty()) 672 return nullptr; 673 674 // If there is one predecessor block, it is the join point. 675 if (Worklist.size() == 1) 676 return Worklist[0]; 677 678 const BasicBlock *JoinBB = nullptr; 679 if (Worklist.size() == 2) { 680 const BasicBlock *Pred0 = Worklist[0]; 681 const BasicBlock *Pred1 = Worklist[1]; 682 const BasicBlock *Pred0UniquePred = Pred0->getUniquePredecessor(); 683 const BasicBlock *Pred1UniquePred = Pred1->getUniquePredecessor(); 684 if (Pred0 == Pred1UniquePred) { 685 // InitBB <- Pred0 = JoinBB 686 // InitBB <- Pred1 <- Pred0 = JoinBB 687 JoinBB = Pred0; 688 } else if (Pred1 == Pred0UniquePred) { 689 // InitBB <- Pred0 <- Pred1 = JoinBB 690 // InitBB <- Pred1 = JoinBB 691 JoinBB = Pred1; 692 } else if (Pred0UniquePred == Pred1UniquePred) { 693 // InitBB <- Pred0 <- JoinBB 694 // InitBB <- Pred1 <- JoinBB 695 JoinBB = Pred0UniquePred; 696 } 697 } 698 699 if (!JoinBB && L) 700 JoinBB = L->getHeader(); 701 702 // In backwards direction there is no need to show termination of previous 703 // instructions. If they do not terminate, the code afterward is dead, making 704 // any information/transformation correct anyway. 705 return JoinBB; 706 } 707 708 const Instruction * 709 MustBeExecutedContextExplorer::getMustBeExecutedNextInstruction( 710 MustBeExecutedIterator &It, const Instruction *PP) { 711 if (!PP) 712 return PP; 713 LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n"); 714 715 // If we explore only inside a given basic block we stop at terminators. 716 if (!ExploreInterBlock && PP->isTerminator()) { 717 LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n"); 718 return nullptr; 719 } 720 721 // If we do not traverse the call graph we check if we can make progress in 722 // the current function. First, check if the instruction is guaranteed to 723 // transfer execution to the successor. 724 bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP); 725 if (!TransfersExecution) 726 return nullptr; 727 728 // If this is not a terminator we know that there is a single instruction 729 // after this one that is executed next if control is transfered. If not, 730 // we can try to go back to a call site we entered earlier. If none exists, we 731 // do not know any instruction that has to be executd next. 732 if (!PP->isTerminator()) { 733 const Instruction *NextPP = PP->getNextNode(); 734 LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n"); 735 return NextPP; 736 } 737 738 // Finally, we have to handle terminators, trivial ones first. 739 assert(PP->isTerminator() && "Expected a terminator!"); 740 741 // A terminator without a successor is not handled yet. 742 if (PP->getNumSuccessors() == 0) { 743 LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n"); 744 return nullptr; 745 } 746 747 // A terminator with a single successor, we will continue at the beginning of 748 // that one. 749 if (PP->getNumSuccessors() == 1) { 750 LLVM_DEBUG( 751 dbgs() << "\tUnconditional terminator, continue with successor\n"); 752 return &PP->getSuccessor(0)->front(); 753 } 754 755 // Multiple successors mean we need to find the join point where control flow 756 // converges again. We use the findForwardJoinPoint helper function with 757 // information about the function and helper analyses, if available. 758 if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent())) 759 return &JoinBB->front(); 760 761 LLVM_DEBUG(dbgs() << "\tNo join point found\n"); 762 return nullptr; 763 } 764 765 const Instruction * 766 MustBeExecutedContextExplorer::getMustBeExecutedPrevInstruction( 767 MustBeExecutedIterator &It, const Instruction *PP) { 768 if (!PP) 769 return PP; 770 771 bool IsFirst = !(PP->getPrevNode()); 772 LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP 773 << (IsFirst ? " [IsFirst]" : "") << "\n"); 774 775 // If we explore only inside a given basic block we stop at the first 776 // instruction. 777 if (!ExploreInterBlock && IsFirst) { 778 LLVM_DEBUG(dbgs() << "\tReached block front in intra-block mode, done\n"); 779 return nullptr; 780 } 781 782 // The block and function that contains the current position. 783 const BasicBlock *PPBlock = PP->getParent(); 784 785 // If we are inside a block we know what instruction was executed before, the 786 // previous one. 787 if (!IsFirst) { 788 const Instruction *PrevPP = PP->getPrevNode(); 789 LLVM_DEBUG( 790 dbgs() << "\tIntermediate instruction, continue with previous\n"); 791 // We did not enter a callee so we simply return the previous instruction. 792 return PrevPP; 793 } 794 795 // Finally, we have to handle the case where the program point is the first in 796 // a block but not in the function. We use the findBackwardJoinPoint helper 797 // function with information about the function and helper analyses, if 798 // available. 799 if (const BasicBlock *JoinBB = findBackwardJoinPoint(PPBlock)) 800 return &JoinBB->back(); 801 802 LLVM_DEBUG(dbgs() << "\tNo join point found\n"); 803 return nullptr; 804 } 805 806 MustBeExecutedIterator::MustBeExecutedIterator( 807 MustBeExecutedContextExplorer &Explorer, const Instruction *I) 808 : Explorer(Explorer), CurInst(I) { 809 reset(I); 810 } 811 812 void MustBeExecutedIterator::reset(const Instruction *I) { 813 Visited.clear(); 814 resetInstruction(I); 815 } 816 817 void MustBeExecutedIterator::resetInstruction(const Instruction *I) { 818 CurInst = I; 819 Head = Tail = nullptr; 820 Visited.insert({I, ExplorationDirection::FORWARD}); 821 Visited.insert({I, ExplorationDirection::BACKWARD}); 822 if (Explorer.ExploreCFGForward) 823 Head = I; 824 if (Explorer.ExploreCFGBackward) 825 Tail = I; 826 } 827 828 const Instruction *MustBeExecutedIterator::advance() { 829 assert(CurInst && "Cannot advance an end iterator!"); 830 Head = Explorer.getMustBeExecutedNextInstruction(*this, Head); 831 if (Head && Visited.insert({Head, ExplorationDirection ::FORWARD}).second) 832 return Head; 833 Head = nullptr; 834 835 Tail = Explorer.getMustBeExecutedPrevInstruction(*this, Tail); 836 if (Tail && Visited.insert({Tail, ExplorationDirection ::BACKWARD}).second) 837 return Tail; 838 Tail = nullptr; 839 return nullptr; 840 } 841 842 PreservedAnalyses MustExecutePrinterPass::run(Function &F, 843 FunctionAnalysisManager &AM) { 844 auto &LI = AM.getResult<LoopAnalysis>(F); 845 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 846 847 MustExecuteAnnotatedWriter Writer(F, DT, LI); 848 F.print(OS, &Writer); 849 return PreservedAnalyses::all(); 850 } 851 852 PreservedAnalyses 853 MustBeExecutedContextPrinterPass::run(Module &M, ModuleAnalysisManager &AM) { 854 FunctionAnalysisManager &FAM = 855 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 856 GetterTy<const LoopInfo> LIGetter = [&](const Function &F) { 857 return &FAM.getResult<LoopAnalysis>(const_cast<Function &>(F)); 858 }; 859 GetterTy<const DominatorTree> DTGetter = [&](const Function &F) { 860 return &FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(F)); 861 }; 862 GetterTy<const PostDominatorTree> PDTGetter = [&](const Function &F) { 863 return &FAM.getResult<PostDominatorTreeAnalysis>(const_cast<Function &>(F)); 864 }; 865 866 MustBeExecutedContextExplorer Explorer( 867 /* ExploreInterBlock */ true, 868 /* ExploreCFGForward */ true, 869 /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter); 870 871 for (Function &F : M) { 872 for (Instruction &I : instructions(F)) { 873 OS << "-- Explore context of: " << I << "\n"; 874 for (const Instruction *CI : Explorer.range(&I)) 875 OS << " [F: " << CI->getFunction()->getName() << "] " << *CI << "\n"; 876 } 877 } 878 return PreservedAnalyses::all(); 879 } 880