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