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