Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 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 | |
Philip Reames | e4b728e | 2018-03-29 19:22:12 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/MustExecute.h" |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/InstructionSimplify.h" |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/LoopInfo.h" |
| 13 | #include "llvm/Analysis/Passes.h" |
| 14 | #include "llvm/Analysis/ValueTracking.h" |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 15 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 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" |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FormattedStream.h" |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 25 | bool LoopSafetyInfo::headerMayThrow() const { |
| 26 | return HeaderMayThrow; |
| 27 | } |
| 28 | |
| 29 | bool LoopSafetyInfo::anyBlockMayThrow() const { |
| 30 | return MayThrow; |
| 31 | } |
| 32 | |
| 33 | void LoopSafetyInfo::computeLoopSafetyInfo(Loop *CurLoop) { |
Shoaib Meenai | a57d713 | 2018-07-19 19:11:29 +0000 | [diff] [blame] | 34 | assert(CurLoop != nullptr && "CurLoop can't be null"); |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 35 | BasicBlock *Header = CurLoop->getHeader(); |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 36 | // Iterate over header and compute safety info. |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 37 | HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header); |
| 38 | MayThrow = HeaderMayThrow; |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 39 | // Iterate over loop instructions and compute safety info. |
| 40 | // Skip header as it has been computed and stored in HeaderMayThrow. |
| 41 | // The first block in loopinfo.Blocks is guaranteed to be the header. |
| 42 | assert(Header == *CurLoop->getBlocks().begin() && |
| 43 | "First block must be header"); |
| 44 | for (Loop::block_iterator BB = std::next(CurLoop->block_begin()), |
| 45 | BBE = CurLoop->block_end(); |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 46 | (BB != BBE) && !MayThrow; ++BB) |
| 47 | MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB); |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 48 | |
| 49 | // Compute funclet colors if we might sink/hoist in a function with a funclet |
| 50 | // personality routine. |
| 51 | Function *Fn = CurLoop->getHeader()->getParent(); |
| 52 | if (Fn->hasPersonalityFn()) |
| 53 | if (Constant *PersonalityFn = Fn->getPersonalityFn()) |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 54 | if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn))) |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 55 | BlockColors = colorEHFunclets(*Fn); |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /// Return true if we can prove that the given ExitBlock is not reached on the |
| 59 | /// first iteration of the given loop. That is, the backedge of the loop must |
| 60 | /// be executed before the ExitBlock is executed in any dynamic execution trace. |
| 61 | static bool CanProveNotTakenFirstIteration(BasicBlock *ExitBlock, |
| 62 | const DominatorTree *DT, |
| 63 | const Loop *CurLoop) { |
| 64 | auto *CondExitBlock = ExitBlock->getSinglePredecessor(); |
| 65 | if (!CondExitBlock) |
| 66 | // expect unique exits |
| 67 | return false; |
| 68 | assert(CurLoop->contains(CondExitBlock) && "meaning of exit block"); |
| 69 | auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator()); |
| 70 | if (!BI || !BI->isConditional()) |
| 71 | return false; |
Serguei Katkov | 5095883 | 2018-05-18 04:56:28 +0000 | [diff] [blame] | 72 | // If condition is constant and false leads to ExitBlock then we always |
| 73 | // execute the true branch. |
| 74 | if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) |
| 75 | return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock; |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 76 | auto *Cond = dyn_cast<CmpInst>(BI->getCondition()); |
| 77 | if (!Cond) |
| 78 | return false; |
| 79 | // todo: this would be a lot more powerful if we used scev, but all the |
| 80 | // plumbing is currently missing to pass a pointer in from the pass |
| 81 | // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known |
| 82 | auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0)); |
| 83 | auto *RHS = Cond->getOperand(1); |
| 84 | if (!LHS || LHS->getParent() != CurLoop->getHeader()) |
| 85 | return false; |
| 86 | auto DL = ExitBlock->getModule()->getDataLayout(); |
| 87 | auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader()); |
| 88 | auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(), |
| 89 | IVStart, RHS, |
| 90 | {DL, /*TLI*/ nullptr, |
| 91 | DT, /*AC*/ nullptr, BI}); |
| 92 | auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull); |
| 93 | if (!SimpleCst) |
| 94 | return false; |
| 95 | if (ExitBlock == BI->getSuccessor(0)) |
| 96 | return SimpleCst->isZeroValue(); |
| 97 | assert(ExitBlock == BI->getSuccessor(1) && "implied by above"); |
| 98 | return SimpleCst->isAllOnesValue(); |
| 99 | } |
| 100 | |
| 101 | /// Returns true if the instruction in a loop is guaranteed to execute at least |
| 102 | /// once. |
| 103 | bool llvm::isGuaranteedToExecute(const Instruction &Inst, |
| 104 | const DominatorTree *DT, const Loop *CurLoop, |
| 105 | const LoopSafetyInfo *SafetyInfo) { |
| 106 | // We have to check to make sure that the instruction dominates all |
| 107 | // of the exit blocks. If it doesn't, then there is a path out of the loop |
| 108 | // which does not execute this instruction, so we can't hoist it. |
| 109 | |
| 110 | // If the instruction is in the header block for the loop (which is very |
| 111 | // common), it is always guaranteed to dominate the exit blocks. Since this |
| 112 | // is a common case, and can save some work, check it now. |
| 113 | if (Inst.getParent() == CurLoop->getHeader()) |
| 114 | // If there's a throw in the header block, we can't guarantee we'll reach |
Philip Reames | e4ec473 | 2018-04-27 20:44:01 +0000 | [diff] [blame] | 115 | // Inst unless we can prove that Inst comes before the potential implicit |
| 116 | // exit. At the moment, we use a (cheap) hack for the common case where |
| 117 | // the instruction of interest is the first one in the block. |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 118 | return !SafetyInfo->headerMayThrow() || |
David Stenberg | 05b6a53 | 2018-05-25 13:02:59 +0000 | [diff] [blame] | 119 | Inst.getParent()->getFirstNonPHIOrDbg() == &Inst; |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 120 | |
| 121 | // Somewhere in this loop there is an instruction which may throw and make us |
| 122 | // exit the loop. |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 123 | if (SafetyInfo->anyBlockMayThrow()) |
Philip Reames | 23aed5e | 2018-03-20 22:45:23 +0000 | [diff] [blame] | 124 | return false; |
| 125 | |
| 126 | // Note: There are two styles of reasoning intermixed below for |
| 127 | // implementation efficiency reasons. They are: |
| 128 | // 1) If we can prove that the instruction dominates all exit blocks, then we |
| 129 | // know the instruction must have executed on *some* iteration before we |
| 130 | // exit. We do not prove *which* iteration the instruction must execute on. |
| 131 | // 2) If we can prove that the instruction dominates the latch and all exits |
| 132 | // which might be taken on the first iteration, we know the instruction must |
| 133 | // execute on the first iteration. This second style allows a conditional |
| 134 | // exit before the instruction of interest which is provably not taken on the |
| 135 | // first iteration. This is a quite common case for range check like |
| 136 | // patterns. TODO: support loops with multiple latches. |
| 137 | |
| 138 | const bool InstDominatesLatch = |
| 139 | CurLoop->getLoopLatch() != nullptr && |
| 140 | DT->dominates(Inst.getParent(), CurLoop->getLoopLatch()); |
| 141 | |
| 142 | // Get the exit blocks for the current loop. |
| 143 | SmallVector<BasicBlock *, 8> ExitBlocks; |
| 144 | CurLoop->getExitBlocks(ExitBlocks); |
| 145 | |
| 146 | // Verify that the block dominates each of the exit blocks of the loop. |
| 147 | for (BasicBlock *ExitBlock : ExitBlocks) |
| 148 | if (!DT->dominates(Inst.getParent(), ExitBlock)) |
| 149 | if (!InstDominatesLatch || |
| 150 | !CanProveNotTakenFirstIteration(ExitBlock, DT, CurLoop)) |
| 151 | return false; |
| 152 | |
| 153 | // As a degenerate case, if the loop is statically infinite then we haven't |
| 154 | // proven anything since there are no exit blocks. |
| 155 | if (ExitBlocks.empty()) |
| 156 | return false; |
| 157 | |
| 158 | // FIXME: In general, we have to prove that the loop isn't an infinite loop. |
| 159 | // See http::llvm.org/PR24078 . (The "ExitBlocks.empty()" check above is |
| 160 | // just a special case of this.) |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 165 | namespace { |
| 166 | struct MustExecutePrinter : public FunctionPass { |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 167 | |
| 168 | static char ID; // Pass identification, replacement for typeid |
| 169 | MustExecutePrinter() : FunctionPass(ID) { |
| 170 | initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry()); |
| 171 | } |
| 172 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 173 | AU.setPreservesAll(); |
| 174 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 175 | AU.addRequired<LoopInfoWrapperPass>(); |
| 176 | } |
| 177 | bool runOnFunction(Function &F) override; |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 178 | }; |
| 179 | } |
| 180 | |
| 181 | char MustExecutePrinter::ID = 0; |
| 182 | INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute", |
| 183 | "Instructions which execute on loop entry", false, true) |
| 184 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 185 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 186 | INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute", |
| 187 | "Instructions which execute on loop entry", false, true) |
| 188 | |
| 189 | FunctionPass *llvm::createMustExecutePrinter() { |
| 190 | return new MustExecutePrinter(); |
| 191 | } |
| 192 | |
Benjamin Kramer | 1fc0da4 | 2018-04-04 11:45:11 +0000 | [diff] [blame] | 193 | static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) { |
Philip Reames | 37a1a29 | 2018-03-20 23:00:54 +0000 | [diff] [blame] | 194 | // TODO: merge these two routines. For the moment, we display the best |
| 195 | // result obtained by *either* implementation. This is a bit unfair since no |
| 196 | // caller actually gets the full power at the moment. |
| 197 | LoopSafetyInfo LSI; |
Max Kazantsev | 530b8d1 | 2018-08-15 05:55:43 +0000 | [diff] [blame^] | 198 | LSI.computeLoopSafetyInfo(L); |
Philip Reames | 37a1a29 | 2018-03-20 23:00:54 +0000 | [diff] [blame] | 199 | return isGuaranteedToExecute(I, DT, L, &LSI) || |
| 200 | isGuaranteedToExecuteForEveryIteration(&I, L); |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Benjamin Kramer | 1fc0da4 | 2018-04-04 11:45:11 +0000 | [diff] [blame] | 203 | namespace { |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 204 | /// An assembly annotator class to print must execute information in |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 205 | /// comments. |
| 206 | class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter { |
| 207 | DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec; |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 208 | |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 209 | public: |
| 210 | MustExecuteAnnotatedWriter(const Function &F, |
| 211 | DominatorTree &DT, LoopInfo &LI) { |
| 212 | for (auto &I: instructions(F)) { |
| 213 | Loop *L = LI.getLoopFor(I.getParent()); |
| 214 | while (L) { |
| 215 | if (isMustExecuteIn(I, L, &DT)) { |
| 216 | MustExec[&I].push_back(L); |
| 217 | } |
| 218 | L = L->getParentLoop(); |
| 219 | }; |
| 220 | } |
| 221 | } |
| 222 | MustExecuteAnnotatedWriter(const Module &M, |
| 223 | DominatorTree &DT, LoopInfo &LI) { |
| 224 | for (auto &F : M) |
| 225 | for (auto &I: instructions(F)) { |
| 226 | Loop *L = LI.getLoopFor(I.getParent()); |
| 227 | while (L) { |
| 228 | if (isMustExecuteIn(I, L, &DT)) { |
| 229 | MustExec[&I].push_back(L); |
| 230 | } |
| 231 | L = L->getParentLoop(); |
| 232 | }; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 237 | void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 238 | if (!MustExec.count(&V)) |
| 239 | return; |
| 240 | |
| 241 | const auto &Loops = MustExec.lookup(&V); |
| 242 | const auto NumLoops = Loops.size(); |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 243 | if (NumLoops > 1) |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 244 | OS << " ; (mustexec in " << NumLoops << " loops: "; |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 245 | else |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 246 | OS << " ; (mustexec in: "; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 247 | |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 248 | bool first = true; |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 249 | for (const Loop *L : Loops) { |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 250 | if (!first) |
| 251 | OS << ", "; |
| 252 | first = false; |
| 253 | OS << L->getHeader()->getName(); |
| 254 | } |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 255 | OS << ")"; |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 256 | } |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 257 | }; |
Benjamin Kramer | 1fc0da4 | 2018-04-04 11:45:11 +0000 | [diff] [blame] | 258 | } // namespace |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 259 | |
| 260 | bool MustExecutePrinter::runOnFunction(Function &F) { |
| 261 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 262 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 263 | |
| 264 | MustExecuteAnnotatedWriter Writer(F, DT, LI); |
| 265 | F.print(dbgs(), &Writer); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 266 | |
Philip Reames | ce998ad | 2018-03-20 18:43:44 +0000 | [diff] [blame] | 267 | return false; |
Philip Reames | 89f2241 | 2018-03-20 17:09:21 +0000 | [diff] [blame] | 268 | } |