blob: ab091005a20d10d285395ce30f212b47ec6b6454 [file] [log] [blame]
Philip Reames89f22412018-03-20 17:09:21 +00001//===- 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 Reamese4b728e2018-03-29 19:22:12 +000010#include "llvm/Analysis/MustExecute.h"
Philip Reames23aed5e2018-03-20 22:45:23 +000011#include "llvm/Analysis/InstructionSimplify.h"
Philip Reames89f22412018-03-20 17:09:21 +000012#include "llvm/Analysis/LoopInfo.h"
13#include "llvm/Analysis/Passes.h"
14#include "llvm/Analysis/ValueTracking.h"
Philip Reamesce998ad2018-03-20 18:43:44 +000015#include "llvm/IR/AssemblyAnnotationWriter.h"
Philip Reames89f22412018-03-20 17:09:21 +000016#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 Reamesce998ad2018-03-20 18:43:44 +000021#include "llvm/Support/FormattedStream.h"
Philip Reames89f22412018-03-20 17:09:21 +000022#include "llvm/Support/raw_ostream.h"
Philip Reames89f22412018-03-20 17:09:21 +000023using namespace llvm;
24
Max Kazantsev530b8d12018-08-15 05:55:43 +000025bool LoopSafetyInfo::headerMayThrow() const {
26 return HeaderMayThrow;
27}
28
29bool LoopSafetyInfo::anyBlockMayThrow() const {
30 return MayThrow;
31}
32
33void LoopSafetyInfo::computeLoopSafetyInfo(Loop *CurLoop) {
Shoaib Meenaia57d7132018-07-19 19:11:29 +000034 assert(CurLoop != nullptr && "CurLoop can't be null");
Philip Reames23aed5e2018-03-20 22:45:23 +000035 BasicBlock *Header = CurLoop->getHeader();
Philip Reames23aed5e2018-03-20 22:45:23 +000036 // Iterate over header and compute safety info.
Max Kazantsev530b8d12018-08-15 05:55:43 +000037 HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
38 MayThrow = HeaderMayThrow;
Philip Reames23aed5e2018-03-20 22:45:23 +000039 // 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 Kazantsev530b8d12018-08-15 05:55:43 +000046 (BB != BBE) && !MayThrow; ++BB)
47 MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
Philip Reames23aed5e2018-03-20 22:45:23 +000048
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 Ahnb4be38f2018-05-17 20:52:03 +000054 if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
Max Kazantsev530b8d12018-08-15 05:55:43 +000055 BlockColors = colorEHFunclets(*Fn);
Philip Reames23aed5e2018-03-20 22:45:23 +000056}
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.
Max Kazantseva7415872018-08-16 06:28:04 +000061static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
Philip Reames23aed5e2018-03-20 22:45:23 +000062 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 Katkov50958832018-05-18 04:56:28 +000072 // 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 Reames23aed5e2018-03-20 22:45:23 +000076 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
Max Kazantsev7b78d392018-08-17 06:19:17 +0000101
102bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
103 const BasicBlock *BB,
104 const DominatorTree *DT) const {
105 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
106
107 // Fast path: header is always reached once the loop is entered.
108 if (BB == CurLoop->getHeader())
109 return true;
110
111 // Collect all transitive predecessors of BB in the same loop. This set will
112 // be a subset of the blocks within the loop.
113 SmallPtrSet<const BasicBlock *, 4> Predecessors;
114 SmallVector<const BasicBlock *, 4> WorkList;
115 for (auto *Pred : predecessors(BB)) {
116 Predecessors.insert(Pred);
117 WorkList.push_back(Pred);
118 }
119 while (!WorkList.empty()) {
120 auto *Pred = WorkList.pop_back_val();
121 assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
122 // We are not interested in backedges and we don't want to leave loop.
123 if (Pred == CurLoop->getHeader())
124 continue;
125 // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
126 // blocks of this inner loop, even those that are always executed AFTER the
127 // BB. It may make our analysis more conservative than it could be, see test
128 // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
129 // We can ignore backedge of all loops containing BB to get a sligtly more
130 // optimistic result.
131 for (auto *PredPred : predecessors(Pred))
132 if (Predecessors.insert(PredPred).second)
133 WorkList.push_back(PredPred);
134 }
135
136 // Make sure that all successors of all predecessors of BB are either:
137 // 1) BB,
138 // 2) Also predecessors of BB,
139 // 3) Exit blocks which are not taken on 1st iteration.
140 // Memoize blocks we've already checked.
141 SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
142 for (auto *Pred : Predecessors)
143 for (auto *Succ : successors(Pred))
144 if (CheckedSuccessors.insert(Succ).second &&
145 Succ != BB && !Predecessors.count(Succ))
146 // By discharging conditions that are not executed on the 1st iteration,
147 // we guarantee that *at least* on the first iteration all paths from
148 // header that *may* execute will lead us to the block of interest. So
149 // that if we had virtually peeled one iteration away, in this peeled
150 // iteration the set of predecessors would contain only paths from
151 // header to BB without any exiting edges that may execute.
152 //
153 // TODO: We only do it for exiting edges currently. We could use the
154 // same function to skip some of the edges within the loop if we know
155 // that they will not be taken on the 1st iteration.
156 //
157 // TODO: If we somehow know the number of iterations in loop, the same
158 // check may be done for any arbitrary N-th iteration as long as N is
159 // not greater than minimum number of iterations in this loop.
160 if (CurLoop->contains(Succ) ||
161 !CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
162 return false;
163
164 // All predecessors can only lead us to BB.
165 return true;
166}
167
Philip Reames23aed5e2018-03-20 22:45:23 +0000168/// Returns true if the instruction in a loop is guaranteed to execute at least
169/// once.
170bool llvm::isGuaranteedToExecute(const Instruction &Inst,
171 const DominatorTree *DT, const Loop *CurLoop,
172 const LoopSafetyInfo *SafetyInfo) {
173 // We have to check to make sure that the instruction dominates all
174 // of the exit blocks. If it doesn't, then there is a path out of the loop
175 // which does not execute this instruction, so we can't hoist it.
176
177 // If the instruction is in the header block for the loop (which is very
178 // common), it is always guaranteed to dominate the exit blocks. Since this
179 // is a common case, and can save some work, check it now.
180 if (Inst.getParent() == CurLoop->getHeader())
181 // If there's a throw in the header block, we can't guarantee we'll reach
Philip Reamese4ec4732018-04-27 20:44:01 +0000182 // Inst unless we can prove that Inst comes before the potential implicit
183 // exit. At the moment, we use a (cheap) hack for the common case where
184 // the instruction of interest is the first one in the block.
Max Kazantsev530b8d12018-08-15 05:55:43 +0000185 return !SafetyInfo->headerMayThrow() ||
David Stenberg05b6a532018-05-25 13:02:59 +0000186 Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
Philip Reames23aed5e2018-03-20 22:45:23 +0000187
188 // Somewhere in this loop there is an instruction which may throw and make us
189 // exit the loop.
Max Kazantsev530b8d12018-08-15 05:55:43 +0000190 if (SafetyInfo->anyBlockMayThrow())
Philip Reames23aed5e2018-03-20 22:45:23 +0000191 return false;
192
Max Kazantsev7b78d392018-08-17 06:19:17 +0000193 // If there is a path from header to exit or latch that doesn't lead to our
194 // instruction's block, return false.
195 if (!SafetyInfo->allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT))
Philip Reames23aed5e2018-03-20 22:45:23 +0000196 return false;
197
Philip Reames23aed5e2018-03-20 22:45:23 +0000198 return true;
199}
200
201
Philip Reames89f22412018-03-20 17:09:21 +0000202namespace {
203 struct MustExecutePrinter : public FunctionPass {
Philip Reames89f22412018-03-20 17:09:21 +0000204
205 static char ID; // Pass identification, replacement for typeid
206 MustExecutePrinter() : FunctionPass(ID) {
207 initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry());
208 }
209 void getAnalysisUsage(AnalysisUsage &AU) const override {
210 AU.setPreservesAll();
211 AU.addRequired<DominatorTreeWrapperPass>();
212 AU.addRequired<LoopInfoWrapperPass>();
213 }
214 bool runOnFunction(Function &F) override;
Philip Reames89f22412018-03-20 17:09:21 +0000215 };
216}
217
218char MustExecutePrinter::ID = 0;
219INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
220 "Instructions which execute on loop entry", false, true)
221INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
222INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
223INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
224 "Instructions which execute on loop entry", false, true)
225
226FunctionPass *llvm::createMustExecutePrinter() {
227 return new MustExecutePrinter();
228}
229
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000230static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
Philip Reames37a1a292018-03-20 23:00:54 +0000231 // TODO: merge these two routines. For the moment, we display the best
232 // result obtained by *either* implementation. This is a bit unfair since no
233 // caller actually gets the full power at the moment.
234 LoopSafetyInfo LSI;
Max Kazantsev530b8d12018-08-15 05:55:43 +0000235 LSI.computeLoopSafetyInfo(L);
Philip Reames37a1a292018-03-20 23:00:54 +0000236 return isGuaranteedToExecute(I, DT, L, &LSI) ||
237 isGuaranteedToExecuteForEveryIteration(&I, L);
Philip Reames89f22412018-03-20 17:09:21 +0000238}
239
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000240namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000241/// An assembly annotator class to print must execute information in
Philip Reamesce998ad2018-03-20 18:43:44 +0000242/// comments.
243class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
244 DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
Philip Reames89f22412018-03-20 17:09:21 +0000245
Philip Reamesce998ad2018-03-20 18:43:44 +0000246public:
247 MustExecuteAnnotatedWriter(const Function &F,
248 DominatorTree &DT, LoopInfo &LI) {
249 for (auto &I: instructions(F)) {
250 Loop *L = LI.getLoopFor(I.getParent());
251 while (L) {
252 if (isMustExecuteIn(I, L, &DT)) {
253 MustExec[&I].push_back(L);
254 }
255 L = L->getParentLoop();
256 };
257 }
258 }
259 MustExecuteAnnotatedWriter(const Module &M,
260 DominatorTree &DT, LoopInfo &LI) {
261 for (auto &F : M)
262 for (auto &I: instructions(F)) {
263 Loop *L = LI.getLoopFor(I.getParent());
264 while (L) {
265 if (isMustExecuteIn(I, L, &DT)) {
266 MustExec[&I].push_back(L);
267 }
268 L = L->getParentLoop();
269 };
270 }
271 }
272
273
Fangrui Songf78650a2018-07-30 19:41:25 +0000274 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Philip Reamesce998ad2018-03-20 18:43:44 +0000275 if (!MustExec.count(&V))
276 return;
277
278 const auto &Loops = MustExec.lookup(&V);
279 const auto NumLoops = Loops.size();
Philip Reames89f22412018-03-20 17:09:21 +0000280 if (NumLoops > 1)
Philip Reamesce998ad2018-03-20 18:43:44 +0000281 OS << " ; (mustexec in " << NumLoops << " loops: ";
Philip Reames89f22412018-03-20 17:09:21 +0000282 else
Philip Reamesce998ad2018-03-20 18:43:44 +0000283 OS << " ; (mustexec in: ";
Fangrui Songf78650a2018-07-30 19:41:25 +0000284
Philip Reames89f22412018-03-20 17:09:21 +0000285 bool first = true;
Philip Reamesce998ad2018-03-20 18:43:44 +0000286 for (const Loop *L : Loops) {
Philip Reames89f22412018-03-20 17:09:21 +0000287 if (!first)
288 OS << ", ";
289 first = false;
290 OS << L->getHeader()->getName();
291 }
Philip Reamesce998ad2018-03-20 18:43:44 +0000292 OS << ")";
Philip Reames89f22412018-03-20 17:09:21 +0000293 }
Philip Reamesce998ad2018-03-20 18:43:44 +0000294};
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000295} // namespace
Philip Reamesce998ad2018-03-20 18:43:44 +0000296
297bool MustExecutePrinter::runOnFunction(Function &F) {
298 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
299 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
300
301 MustExecuteAnnotatedWriter Writer(F, DT, LI);
302 F.print(dbgs(), &Writer);
Fangrui Songf78650a2018-07-30 19:41:25 +0000303
Philip Reamesce998ad2018-03-20 18:43:44 +0000304 return false;
Philip Reames89f22412018-03-20 17:09:21 +0000305}