blob: bce941be26c47e276f01aca4a15db31043974263 [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 Kazantsev8d56be72018-10-16 08:07:14 +000025const DenseMap<BasicBlock *, ColorVector> &
26LoopSafetyInfo::getBlockColors() const {
27 return BlockColors;
28}
29
30void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) {
31 ColorVector &ColorsForNewBlock = BlockColors[New];
32 ColorVector &ColorsForOldBlock = BlockColors[Old];
33 ColorsForNewBlock = ColorsForOldBlock;
34}
35
Max Kazantsev530b8d12018-08-15 05:55:43 +000036bool LoopSafetyInfo::headerMayThrow() const {
37 return HeaderMayThrow;
38}
39
Max Kazantsev6a4f5e22018-10-16 07:50:14 +000040bool LoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
41 (void)BB;
42 return anyBlockMayThrow();
43}
44
Max Kazantsev530b8d12018-08-15 05:55:43 +000045bool LoopSafetyInfo::anyBlockMayThrow() const {
46 return MayThrow;
47}
48
Max Kazantsev8d56be72018-10-16 08:07:14 +000049void LoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
Shoaib Meenaia57d7132018-07-19 19:11:29 +000050 assert(CurLoop != nullptr && "CurLoop can't be null");
Philip Reames23aed5e2018-03-20 22:45:23 +000051 BasicBlock *Header = CurLoop->getHeader();
Philip Reames23aed5e2018-03-20 22:45:23 +000052 // Iterate over header and compute safety info.
Max Kazantsev530b8d12018-08-15 05:55:43 +000053 HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
54 MayThrow = HeaderMayThrow;
Philip Reames23aed5e2018-03-20 22:45:23 +000055 // 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();
Max Kazantsev530b8d12018-08-15 05:55:43 +000062 (BB != BBE) && !MayThrow; ++BB)
63 MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
Philip Reames23aed5e2018-03-20 22:45:23 +000064
Max Kazantsev8d56be72018-10-16 08:07:14 +000065 computeBlockColors(CurLoop);
66}
67
68void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
Philip Reames23aed5e2018-03-20 22:45:23 +000069 // 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())
Heejin Ahnb4be38f2018-05-17 20:52:03 +000074 if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
Max Kazantsev530b8d12018-08-15 05:55:43 +000075 BlockColors = colorEHFunclets(*Fn);
Philip Reames23aed5e2018-03-20 22:45:23 +000076}
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.
Max Kazantseva7415872018-08-16 06:28:04 +000081static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
Philip Reames23aed5e2018-03-20 22:45:23 +000082 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;
Serguei Katkov50958832018-05-18 04:56:28 +000092 // 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;
Philip Reames23aed5e2018-03-20 22:45:23 +000096 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
Max Kazantsevbfbd4d12018-08-21 07:15:06 +0000121void LoopSafetyInfo::collectTransitivePredecessors(
122 const Loop *CurLoop, const BasicBlock *BB,
123 SmallPtrSetImpl<const BasicBlock *> &Predecessors) const {
124 assert(Predecessors.empty() && "Garbage in predecessors set?");
Max Kazantsev7b78d392018-08-17 06:19:17 +0000125 assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
Max Kazantsev7b78d392018-08-17 06:19:17 +0000126 if (BB == CurLoop->getHeader())
Max Kazantsevbfbd4d12018-08-21 07:15:06 +0000127 return;
Max Kazantsev7b78d392018-08-17 06:19:17 +0000128 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 }
Max Kazantsevbfbd4d12018-08-21 07:15:06 +0000149}
150
151bool 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);
Max Kazantsev7b78d392018-08-17 06:19:17 +0000164
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;
Max Kazantsev6a4f5e22018-10-16 07:50:14 +0000171 for (auto *Pred : Predecessors) {
172 // Predecessor block may throw, so it has a side exit.
173 if (blockMayThrow(Pred))
174 return false;
Max Kazantsev7b78d392018-08-17 06:19:17 +0000175 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;
Max Kazantsev6a4f5e22018-10-16 07:50:14 +0000195 }
Max Kazantsev7b78d392018-08-17 06:19:17 +0000196
197 // All predecessors can only lead us to BB.
198 return true;
199}
200
Philip Reames23aed5e2018-03-20 22:45:23 +0000201/// Returns true if the instruction in a loop is guaranteed to execute at least
202/// once.
Max Kazantsevc8466f92018-10-16 06:34:53 +0000203bool LoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
204 const DominatorTree *DT,
205 const Loop *CurLoop) const {
Philip Reames23aed5e2018-03-20 22:45:23 +0000206 // 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
Philip Reamese4ec4732018-04-27 20:44:01 +0000215 // 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.
Max Kazantsevc8466f92018-10-16 06:34:53 +0000218 return !headerMayThrow() ||
219 Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
Philip Reames23aed5e2018-03-20 22:45:23 +0000220
Max Kazantsev7b78d392018-08-17 06:19:17 +0000221 // If there is a path from header to exit or latch that doesn't lead to our
222 // instruction's block, return false.
Max Kazantsevc8466f92018-10-16 06:34:53 +0000223 if (!allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT))
Philip Reames23aed5e2018-03-20 22:45:23 +0000224 return false;
225
Philip Reames23aed5e2018-03-20 22:45:23 +0000226 return true;
227}
228
229
Philip Reames89f22412018-03-20 17:09:21 +0000230namespace {
231 struct MustExecutePrinter : public FunctionPass {
Philip Reames89f22412018-03-20 17:09:21 +0000232
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;
Philip Reames89f22412018-03-20 17:09:21 +0000243 };
244}
245
246char MustExecutePrinter::ID = 0;
247INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
248 "Instructions which execute on loop entry", false, true)
249INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
250INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
251INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
252 "Instructions which execute on loop entry", false, true)
253
254FunctionPass *llvm::createMustExecutePrinter() {
255 return new MustExecutePrinter();
256}
257
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000258static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
Philip Reames37a1a292018-03-20 23:00:54 +0000259 // 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;
Max Kazantsev530b8d12018-08-15 05:55:43 +0000263 LSI.computeLoopSafetyInfo(L);
Max Kazantsevc8466f92018-10-16 06:34:53 +0000264 return LSI.isGuaranteedToExecute(I, DT, L) ||
Philip Reames37a1a292018-03-20 23:00:54 +0000265 isGuaranteedToExecuteForEveryIteration(&I, L);
Philip Reames89f22412018-03-20 17:09:21 +0000266}
267
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000268namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000269/// An assembly annotator class to print must execute information in
Philip Reamesce998ad2018-03-20 18:43:44 +0000270/// comments.
271class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
272 DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
Philip Reames89f22412018-03-20 17:09:21 +0000273
Philip Reamesce998ad2018-03-20 18:43:44 +0000274public:
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
Fangrui Songf78650a2018-07-30 19:41:25 +0000302 void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
Philip Reamesce998ad2018-03-20 18:43:44 +0000303 if (!MustExec.count(&V))
304 return;
305
306 const auto &Loops = MustExec.lookup(&V);
307 const auto NumLoops = Loops.size();
Philip Reames89f22412018-03-20 17:09:21 +0000308 if (NumLoops > 1)
Philip Reamesce998ad2018-03-20 18:43:44 +0000309 OS << " ; (mustexec in " << NumLoops << " loops: ";
Philip Reames89f22412018-03-20 17:09:21 +0000310 else
Philip Reamesce998ad2018-03-20 18:43:44 +0000311 OS << " ; (mustexec in: ";
Fangrui Songf78650a2018-07-30 19:41:25 +0000312
Philip Reames89f22412018-03-20 17:09:21 +0000313 bool first = true;
Philip Reamesce998ad2018-03-20 18:43:44 +0000314 for (const Loop *L : Loops) {
Philip Reames89f22412018-03-20 17:09:21 +0000315 if (!first)
316 OS << ", ";
317 first = false;
318 OS << L->getHeader()->getName();
319 }
Philip Reamesce998ad2018-03-20 18:43:44 +0000320 OS << ")";
Philip Reames89f22412018-03-20 17:09:21 +0000321 }
Philip Reamesce998ad2018-03-20 18:43:44 +0000322};
Benjamin Kramer1fc0da42018-04-04 11:45:11 +0000323} // namespace
Philip Reamesce998ad2018-03-20 18:43:44 +0000324
325bool 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);
Fangrui Songf78650a2018-07-30 19:41:25 +0000331
Philip Reamesce998ad2018-03-20 18:43:44 +0000332 return false;
Philip Reames89f22412018-03-20 17:09:21 +0000333}