1 //===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===// 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 #include "llvm/Analysis/MustExecute.h" 11 #include "llvm/Analysis/InstructionSimplify.h" 12 #include "llvm/Analysis/LoopInfo.h" 13 #include "llvm/Analysis/Passes.h" 14 #include "llvm/Analysis/ValueTracking.h" 15 #include "llvm/IR/AssemblyAnnotationWriter.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/InstIterator.h" 18 #include "llvm/IR/LLVMContext.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/FormattedStream.h" 22 #include "llvm/Support/raw_ostream.h" 23 using namespace llvm; 24 25 const DenseMap<BasicBlock *, ColorVector> & 26 LoopSafetyInfo::getBlockColors() const { 27 return BlockColors; 28 } 29 30 void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) { 31 ColorVector &ColorsForNewBlock = BlockColors[New]; 32 ColorVector &ColorsForOldBlock = BlockColors[Old]; 33 ColorsForNewBlock = ColorsForOldBlock; 34 } 35 36 bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const { 37 (void)BB; 38 return anyBlockMayThrow(); 39 } 40 41 bool SimpleLoopSafetyInfo::anyBlockMayThrow() const { 42 return MayThrow; 43 } 44 45 void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) { 46 assert(CurLoop != nullptr && "CurLoop can't be null"); 47 BasicBlock *Header = CurLoop->getHeader(); 48 // Iterate over header and compute safety info. 49 HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header); 50 MayThrow = HeaderMayThrow; 51 // Iterate over loop instructions and compute safety info. 52 // Skip header as it has been computed and stored in HeaderMayThrow. 53 // The first block in loopinfo.Blocks is guaranteed to be the header. 54 assert(Header == *CurLoop->getBlocks().begin() && 55 "First block must be header"); 56 for (Loop::block_iterator BB = std::next(CurLoop->block_begin()), 57 BBE = CurLoop->block_end(); 58 (BB != BBE) && !MayThrow; ++BB) 59 MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB); 60 61 computeBlockColors(CurLoop); 62 } 63 64 bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const { 65 return ICF.hasICF(BB); 66 } 67 68 bool ICFLoopSafetyInfo::anyBlockMayThrow() const { 69 return MayThrow; 70 } 71 72 void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) { 73 assert(CurLoop != nullptr && "CurLoop can't be null"); 74 ICF.clear(); 75 MW.clear(); 76 MayThrow = false; 77 // Figure out the fact that at least one block may throw. 78 for (auto &BB : CurLoop->blocks()) 79 if (ICF.hasICF(&*BB)) { 80 MayThrow = true; 81 break; 82 } 83 computeBlockColors(CurLoop); 84 } 85 86 void ICFLoopSafetyInfo::insertInstructionTo(const BasicBlock *BB) { 87 ICF.invalidateBlock(BB); 88 MW.invalidateBlock(BB); 89 } 90 91 void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) { 92 // TODO: So far we just conservatively drop cache, but maybe we can not do it 93 // when Inst is not an ICF instruction. Follow-up on that. 94 ICF.invalidateBlock(Inst->getParent()); 95 MW.invalidateBlock(Inst->getParent()); 96 } 97 98 void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) { 99 // Compute funclet colors if we might sink/hoist in a function with a funclet 100 // personality routine. 101 Function *Fn = CurLoop->getHeader()->getParent(); 102 if (Fn->hasPersonalityFn()) 103 if (Constant *PersonalityFn = Fn->getPersonalityFn()) 104 if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn))) 105 BlockColors = colorEHFunclets(*Fn); 106 } 107 108 /// Return true if we can prove that the given ExitBlock is not reached on the 109 /// first iteration of the given loop. That is, the backedge of the loop must 110 /// be executed before the ExitBlock is executed in any dynamic execution trace. 111 static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock, 112 const DominatorTree *DT, 113 const Loop *CurLoop) { 114 auto *CondExitBlock = ExitBlock->getSinglePredecessor(); 115 if (!CondExitBlock) 116 // expect unique exits 117 return false; 118 assert(CurLoop->contains(CondExitBlock) && "meaning of exit block"); 119 auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator()); 120 if (!BI || !BI->isConditional()) 121 return false; 122 // If condition is constant and false leads to ExitBlock then we always 123 // execute the true branch. 124 if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) 125 return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock; 126 auto *Cond = dyn_cast<CmpInst>(BI->getCondition()); 127 if (!Cond) 128 return false; 129 // todo: this would be a lot more powerful if we used scev, but all the 130 // plumbing is currently missing to pass a pointer in from the pass 131 // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known 132 auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0)); 133 auto *RHS = Cond->getOperand(1); 134 if (!LHS || LHS->getParent() != CurLoop->getHeader()) 135 return false; 136 auto DL = ExitBlock->getModule()->getDataLayout(); 137 auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader()); 138 auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(), 139 IVStart, RHS, 140 {DL, /*TLI*/ nullptr, 141 DT, /*AC*/ nullptr, BI}); 142 auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull); 143 if (!SimpleCst) 144 return false; 145 if (ExitBlock == BI->getSuccessor(0)) 146 return SimpleCst->isZeroValue(); 147 assert(ExitBlock == BI->getSuccessor(1) && "implied by above"); 148 return SimpleCst->isAllOnesValue(); 149 } 150 151 /// Collect all blocks from \p CurLoop which lie on all possible paths from 152 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set 153 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty. 154 static void collectTransitivePredecessors( 155 const Loop *CurLoop, const BasicBlock *BB, 156 SmallPtrSetImpl<const BasicBlock *> &Predecessors) { 157 assert(Predecessors.empty() && "Garbage in predecessors set?"); 158 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 159 if (BB == CurLoop->getHeader()) 160 return; 161 SmallVector<const BasicBlock *, 4> WorkList; 162 for (auto *Pred : predecessors(BB)) { 163 Predecessors.insert(Pred); 164 WorkList.push_back(Pred); 165 } 166 while (!WorkList.empty()) { 167 auto *Pred = WorkList.pop_back_val(); 168 assert(CurLoop->contains(Pred) && "Should only reach loop blocks!"); 169 // We are not interested in backedges and we don't want to leave loop. 170 if (Pred == CurLoop->getHeader()) 171 continue; 172 // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all 173 // blocks of this inner loop, even those that are always executed AFTER the 174 // BB. It may make our analysis more conservative than it could be, see test 175 // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll. 176 // We can ignore backedge of all loops containing BB to get a sligtly more 177 // optimistic result. 178 for (auto *PredPred : predecessors(Pred)) 179 if (Predecessors.insert(PredPred).second) 180 WorkList.push_back(PredPred); 181 } 182 } 183 184 bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop, 185 const BasicBlock *BB, 186 const DominatorTree *DT) const { 187 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 188 189 // Fast path: header is always reached once the loop is entered. 190 if (BB == CurLoop->getHeader()) 191 return true; 192 193 // Collect all transitive predecessors of BB in the same loop. This set will 194 // be a subset of the blocks within the loop. 195 SmallPtrSet<const BasicBlock *, 4> Predecessors; 196 collectTransitivePredecessors(CurLoop, BB, Predecessors); 197 198 // Make sure that all successors of all predecessors of BB are either: 199 // 1) BB, 200 // 2) Also predecessors of BB, 201 // 3) Exit blocks which are not taken on 1st iteration. 202 // Memoize blocks we've already checked. 203 SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors; 204 for (auto *Pred : Predecessors) { 205 // Predecessor block may throw, so it has a side exit. 206 if (blockMayThrow(Pred)) 207 return false; 208 for (auto *Succ : successors(Pred)) 209 if (CheckedSuccessors.insert(Succ).second && 210 Succ != BB && !Predecessors.count(Succ)) 211 // By discharging conditions that are not executed on the 1st iteration, 212 // we guarantee that *at least* on the first iteration all paths from 213 // header that *may* execute will lead us to the block of interest. So 214 // that if we had virtually peeled one iteration away, in this peeled 215 // iteration the set of predecessors would contain only paths from 216 // header to BB without any exiting edges that may execute. 217 // 218 // TODO: We only do it for exiting edges currently. We could use the 219 // same function to skip some of the edges within the loop if we know 220 // that they will not be taken on the 1st iteration. 221 // 222 // TODO: If we somehow know the number of iterations in loop, the same 223 // check may be done for any arbitrary N-th iteration as long as N is 224 // not greater than minimum number of iterations in this loop. 225 if (CurLoop->contains(Succ) || 226 !CanProveNotTakenFirstIteration(Succ, DT, CurLoop)) 227 return false; 228 } 229 230 // All predecessors can only lead us to BB. 231 return true; 232 } 233 234 /// Returns true if the instruction in a loop is guaranteed to execute at least 235 /// once. 236 bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst, 237 const DominatorTree *DT, 238 const Loop *CurLoop) const { 239 // If the instruction is in the header block for the loop (which is very 240 // common), it is always guaranteed to dominate the exit blocks. Since this 241 // is a common case, and can save some work, check it now. 242 if (Inst.getParent() == CurLoop->getHeader()) 243 // If there's a throw in the header block, we can't guarantee we'll reach 244 // Inst unless we can prove that Inst comes before the potential implicit 245 // exit. At the moment, we use a (cheap) hack for the common case where 246 // the instruction of interest is the first one in the block. 247 return !HeaderMayThrow || 248 Inst.getParent()->getFirstNonPHIOrDbg() == &Inst; 249 250 // If there is a path from header to exit or latch that doesn't lead to our 251 // instruction's block, return false. 252 return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT); 253 } 254 255 bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst, 256 const DominatorTree *DT, 257 const Loop *CurLoop) const { 258 return !ICF.isDominatedByICFIFromSameBlock(&Inst) && 259 allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT); 260 } 261 262 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB, 263 const Loop *CurLoop) const { 264 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 265 266 // Fast path: there are no instructions before header. 267 if (BB == CurLoop->getHeader()) 268 return true; 269 270 // Collect all transitive predecessors of BB in the same loop. This set will 271 // be a subset of the blocks within the loop. 272 SmallPtrSet<const BasicBlock *, 4> Predecessors; 273 collectTransitivePredecessors(CurLoop, BB, Predecessors); 274 // Find if there any instruction in either predecessor that could write 275 // to memory. 276 for (auto *Pred : Predecessors) 277 if (MW.mayWriteToMemory(Pred)) 278 return false; 279 return true; 280 } 281 282 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I, 283 const Loop *CurLoop) const { 284 auto *BB = I.getParent(); 285 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!"); 286 return !MW.isDominatedByMemoryWriteFromSameBlock(&I) && 287 doesNotWriteMemoryBefore(BB, CurLoop); 288 } 289 290 namespace { 291 struct MustExecutePrinter : public FunctionPass { 292 293 static char ID; // Pass identification, replacement for typeid 294 MustExecutePrinter() : FunctionPass(ID) { 295 initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry()); 296 } 297 void getAnalysisUsage(AnalysisUsage &AU) const override { 298 AU.setPreservesAll(); 299 AU.addRequired<DominatorTreeWrapperPass>(); 300 AU.addRequired<LoopInfoWrapperPass>(); 301 } 302 bool runOnFunction(Function &F) override; 303 }; 304 } 305 306 char MustExecutePrinter::ID = 0; 307 INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute", 308 "Instructions which execute on loop entry", false, true) 309 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 310 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 311 INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute", 312 "Instructions which execute on loop entry", false, true) 313 314 FunctionPass *llvm::createMustExecutePrinter() { 315 return new MustExecutePrinter(); 316 } 317 318 static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) { 319 // TODO: merge these two routines. For the moment, we display the best 320 // result obtained by *either* implementation. This is a bit unfair since no 321 // caller actually gets the full power at the moment. 322 SimpleLoopSafetyInfo LSI; 323 LSI.computeLoopSafetyInfo(L); 324 return LSI.isGuaranteedToExecute(I, DT, L) || 325 isGuaranteedToExecuteForEveryIteration(&I, L); 326 } 327 328 namespace { 329 /// An assembly annotator class to print must execute information in 330 /// comments. 331 class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter { 332 DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec; 333 334 public: 335 MustExecuteAnnotatedWriter(const Function &F, 336 DominatorTree &DT, LoopInfo &LI) { 337 for (auto &I: instructions(F)) { 338 Loop *L = LI.getLoopFor(I.getParent()); 339 while (L) { 340 if (isMustExecuteIn(I, L, &DT)) { 341 MustExec[&I].push_back(L); 342 } 343 L = L->getParentLoop(); 344 }; 345 } 346 } 347 MustExecuteAnnotatedWriter(const Module &M, 348 DominatorTree &DT, LoopInfo &LI) { 349 for (auto &F : M) 350 for (auto &I: instructions(F)) { 351 Loop *L = LI.getLoopFor(I.getParent()); 352 while (L) { 353 if (isMustExecuteIn(I, L, &DT)) { 354 MustExec[&I].push_back(L); 355 } 356 L = L->getParentLoop(); 357 }; 358 } 359 } 360 361 362 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { 363 if (!MustExec.count(&V)) 364 return; 365 366 const auto &Loops = MustExec.lookup(&V); 367 const auto NumLoops = Loops.size(); 368 if (NumLoops > 1) 369 OS << " ; (mustexec in " << NumLoops << " loops: "; 370 else 371 OS << " ; (mustexec in: "; 372 373 bool first = true; 374 for (const Loop *L : Loops) { 375 if (!first) 376 OS << ", "; 377 first = false; 378 OS << L->getHeader()->getName(); 379 } 380 OS << ")"; 381 } 382 }; 383 } // namespace 384 385 bool MustExecutePrinter::runOnFunction(Function &F) { 386 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 387 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 388 389 MustExecuteAnnotatedWriter Writer(F, DT, LI); 390 F.print(dbgs(), &Writer); 391 392 return false; 393 } 394