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