xref: /llvm-project/llvm/lib/Analysis/MustExecute.cpp (revision e253cdda35eb24f29170aaaf102caed94a85201c)
1 //===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/MustExecute.h"
10 #include "llvm/ADT/PostOrderIterator.h"
11 #include "llvm/Analysis/CFG.h"
12 #include "llvm/Analysis/InstructionSimplify.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Analysis/PostDominators.h"
16 #include "llvm/Analysis/ValueTracking.h"
17 #include "llvm/IR/AssemblyAnnotationWriter.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "must-execute"
30 
31 const DenseMap<BasicBlock *, ColorVector> &
32 LoopSafetyInfo::getBlockColors() const {
33   return BlockColors;
34 }
35 
36 void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) {
37   ColorVector &ColorsForNewBlock = BlockColors[New];
38   ColorVector &ColorsForOldBlock = BlockColors[Old];
39   ColorsForNewBlock = ColorsForOldBlock;
40 }
41 
42 bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
43   (void)BB;
44   return anyBlockMayThrow();
45 }
46 
47 bool SimpleLoopSafetyInfo::anyBlockMayThrow() const {
48   return MayThrow;
49 }
50 
51 void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
52   assert(CurLoop != nullptr && "CurLoop can't be null");
53   BasicBlock *Header = CurLoop->getHeader();
54   // Iterate over header and compute safety info.
55   HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
56   MayThrow = HeaderMayThrow;
57   // Iterate over loop instructions and compute safety info.
58   // Skip header as it has been computed and stored in HeaderMayThrow.
59   // The first block in loopinfo.Blocks is guaranteed to be the header.
60   assert(Header == *CurLoop->getBlocks().begin() &&
61          "First block must be header");
62   for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
63                             BBE = CurLoop->block_end();
64        (BB != BBE) && !MayThrow; ++BB)
65     MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
66 
67   computeBlockColors(CurLoop);
68 }
69 
70 bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
71   return ICF.hasICF(BB);
72 }
73 
74 bool ICFLoopSafetyInfo::anyBlockMayThrow() const {
75   return MayThrow;
76 }
77 
78 void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
79   assert(CurLoop != nullptr && "CurLoop can't be null");
80   ICF.clear();
81   MW.clear();
82   MayThrow = false;
83   // Figure out the fact that at least one block may throw.
84   for (auto &BB : CurLoop->blocks())
85     if (ICF.hasICF(&*BB)) {
86       MayThrow = true;
87       break;
88     }
89   computeBlockColors(CurLoop);
90 }
91 
92 void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst,
93                                             const BasicBlock *BB) {
94   ICF.insertInstructionTo(Inst, BB);
95   MW.insertInstructionTo(Inst, BB);
96 }
97 
98 void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
99   ICF.removeInstruction(Inst);
100   MW.removeInstruction(Inst);
101 }
102 
103 void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
104   // Compute funclet colors if we might sink/hoist in a function with a funclet
105   // personality routine.
106   Function *Fn = CurLoop->getHeader()->getParent();
107   if (Fn->hasPersonalityFn())
108     if (Constant *PersonalityFn = Fn->getPersonalityFn())
109       if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
110         BlockColors = colorEHFunclets(*Fn);
111 }
112 
113 /// Return true if we can prove that the given ExitBlock is not reached on the
114 /// first iteration of the given loop.  That is, the backedge of the loop must
115 /// be executed before the ExitBlock is executed in any dynamic execution trace.
116 static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
117                                            const DominatorTree *DT,
118                                            const Loop *CurLoop) {
119   auto *CondExitBlock = ExitBlock->getSinglePredecessor();
120   if (!CondExitBlock)
121     // expect unique exits
122     return false;
123   assert(CurLoop->contains(CondExitBlock) && "meaning of exit block");
124   auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator());
125   if (!BI || !BI->isConditional())
126     return false;
127   // If condition is constant and false leads to ExitBlock then we always
128   // execute the true branch.
129   if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition()))
130     return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock;
131   auto *Cond = dyn_cast<CmpInst>(BI->getCondition());
132   if (!Cond)
133     return false;
134   // todo: this would be a lot more powerful if we used scev, but all the
135   // plumbing is currently missing to pass a pointer in from the pass
136   // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known
137   auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0));
138   auto *RHS = Cond->getOperand(1);
139   if (!LHS || LHS->getParent() != CurLoop->getHeader())
140     return false;
141   auto DL = ExitBlock->getModule()->getDataLayout();
142   auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
143   auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(),
144                                           IVStart, RHS,
145                                           {DL, /*TLI*/ nullptr,
146                                               DT, /*AC*/ nullptr, BI});
147   auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
148   if (!SimpleCst)
149     return false;
150   if (ExitBlock == BI->getSuccessor(0))
151     return SimpleCst->isZeroValue();
152   assert(ExitBlock == BI->getSuccessor(1) && "implied by above");
153   return SimpleCst->isAllOnesValue();
154 }
155 
156 /// Collect all blocks from \p CurLoop which lie on all possible paths from
157 /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
158 /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
159 static void collectTransitivePredecessors(
160     const Loop *CurLoop, const BasicBlock *BB,
161     SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
162   assert(Predecessors.empty() && "Garbage in predecessors set?");
163   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
164   if (BB == CurLoop->getHeader())
165     return;
166   SmallVector<const BasicBlock *, 4> WorkList;
167   for (auto *Pred : predecessors(BB)) {
168     Predecessors.insert(Pred);
169     WorkList.push_back(Pred);
170   }
171   while (!WorkList.empty()) {
172     auto *Pred = WorkList.pop_back_val();
173     assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
174     // We are not interested in backedges and we don't want to leave loop.
175     if (Pred == CurLoop->getHeader())
176       continue;
177     // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
178     // blocks of this inner loop, even those that are always executed AFTER the
179     // BB. It may make our analysis more conservative than it could be, see test
180     // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
181     // We can ignore backedge of all loops containing BB to get a sligtly more
182     // optimistic result.
183     for (auto *PredPred : predecessors(Pred))
184       if (Predecessors.insert(PredPred).second)
185         WorkList.push_back(PredPred);
186   }
187 }
188 
189 bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
190                                              const BasicBlock *BB,
191                                              const DominatorTree *DT) const {
192   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
193 
194   // Fast path: header is always reached once the loop is entered.
195   if (BB == CurLoop->getHeader())
196     return true;
197 
198   // Collect all transitive predecessors of BB in the same loop. This set will
199   // be a subset of the blocks within the loop.
200   SmallPtrSet<const BasicBlock *, 4> Predecessors;
201   collectTransitivePredecessors(CurLoop, BB, Predecessors);
202 
203   // Make sure that all successors of, all predecessors of BB which are not
204   // dominated by BB, are either:
205   // 1) BB,
206   // 2) Also predecessors of BB,
207   // 3) Exit blocks which are not taken on 1st iteration.
208   // Memoize blocks we've already checked.
209   SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
210   for (auto *Pred : Predecessors) {
211     // Predecessor block may throw, so it has a side exit.
212     if (blockMayThrow(Pred))
213       return false;
214 
215     // BB dominates Pred, so if Pred runs, BB must run.
216     // This is true when Pred is a loop latch.
217     if (DT->dominates(BB, Pred))
218       continue;
219 
220     for (auto *Succ : successors(Pred))
221       if (CheckedSuccessors.insert(Succ).second &&
222           Succ != BB && !Predecessors.count(Succ))
223         // By discharging conditions that are not executed on the 1st iteration,
224         // we guarantee that *at least* on the first iteration all paths from
225         // header that *may* execute will lead us to the block of interest. So
226         // that if we had virtually peeled one iteration away, in this peeled
227         // iteration the set of predecessors would contain only paths from
228         // header to BB without any exiting edges that may execute.
229         //
230         // TODO: We only do it for exiting edges currently. We could use the
231         // same function to skip some of the edges within the loop if we know
232         // that they will not be taken on the 1st iteration.
233         //
234         // TODO: If we somehow know the number of iterations in loop, the same
235         // check may be done for any arbitrary N-th iteration as long as N is
236         // not greater than minimum number of iterations in this loop.
237         if (CurLoop->contains(Succ) ||
238             !CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
239           return false;
240   }
241 
242   // All predecessors can only lead us to BB.
243   return true;
244 }
245 
246 /// Returns true if the instruction in a loop is guaranteed to execute at least
247 /// once.
248 bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
249                                                  const DominatorTree *DT,
250                                                  const Loop *CurLoop) const {
251   // If the instruction is in the header block for the loop (which is very
252   // common), it is always guaranteed to dominate the exit blocks.  Since this
253   // is a common case, and can save some work, check it now.
254   if (Inst.getParent() == CurLoop->getHeader())
255     // If there's a throw in the header block, we can't guarantee we'll reach
256     // Inst unless we can prove that Inst comes before the potential implicit
257     // exit.  At the moment, we use a (cheap) hack for the common case where
258     // the instruction of interest is the first one in the block.
259     return !HeaderMayThrow ||
260            Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
261 
262   // If there is a path from header to exit or latch that doesn't lead to our
263   // instruction's block, return false.
264   return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
265 }
266 
267 bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
268                                               const DominatorTree *DT,
269                                               const Loop *CurLoop) const {
270   return !ICF.isDominatedByICFIFromSameBlock(&Inst) &&
271          allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
272 }
273 
274 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB,
275                                                  const Loop *CurLoop) const {
276   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
277 
278   // Fast path: there are no instructions before header.
279   if (BB == CurLoop->getHeader())
280     return true;
281 
282   // Collect all transitive predecessors of BB in the same loop. This set will
283   // be a subset of the blocks within the loop.
284   SmallPtrSet<const BasicBlock *, 4> Predecessors;
285   collectTransitivePredecessors(CurLoop, BB, Predecessors);
286   // Find if there any instruction in either predecessor that could write
287   // to memory.
288   for (auto *Pred : Predecessors)
289     if (MW.mayWriteToMemory(Pred))
290       return false;
291   return true;
292 }
293 
294 bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I,
295                                                  const Loop *CurLoop) const {
296   auto *BB = I.getParent();
297   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
298   return !MW.isDominatedByMemoryWriteFromSameBlock(&I) &&
299          doesNotWriteMemoryBefore(BB, CurLoop);
300 }
301 
302 namespace {
303   struct MustExecutePrinter : public FunctionPass {
304 
305     static char ID; // Pass identification, replacement for typeid
306     MustExecutePrinter() : FunctionPass(ID) {
307       initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry());
308     }
309     void getAnalysisUsage(AnalysisUsage &AU) const override {
310       AU.setPreservesAll();
311       AU.addRequired<DominatorTreeWrapperPass>();
312       AU.addRequired<LoopInfoWrapperPass>();
313     }
314     bool runOnFunction(Function &F) override;
315   };
316   struct MustBeExecutedContextPrinter : public ModulePass {
317     static char ID;
318 
319     MustBeExecutedContextPrinter() : ModulePass(ID) {
320       initializeMustBeExecutedContextPrinterPass(*PassRegistry::getPassRegistry());
321     }
322     void getAnalysisUsage(AnalysisUsage &AU) const override {
323       AU.setPreservesAll();
324     }
325     bool runOnModule(Module &M) override;
326   };
327 }
328 
329 char MustExecutePrinter::ID = 0;
330 INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
331                       "Instructions which execute on loop entry", false, true)
332 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
333 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
334 INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
335                     "Instructions which execute on loop entry", false, true)
336 
337 FunctionPass *llvm::createMustExecutePrinter() {
338   return new MustExecutePrinter();
339 }
340 
341 char MustBeExecutedContextPrinter::ID = 0;
342 INITIALIZE_PASS_BEGIN(
343     MustBeExecutedContextPrinter, "print-must-be-executed-contexts",
344     "print the must-be-executed-contexed for all instructions", false, true)
345 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
346 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
347 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
348 INITIALIZE_PASS_END(MustBeExecutedContextPrinter,
349                     "print-must-be-executed-contexts",
350                     "print the must-be-executed-contexed for all instructions",
351                     false, true)
352 
353 ModulePass *llvm::createMustBeExecutedContextPrinter() {
354   return new MustBeExecutedContextPrinter();
355 }
356 
357 bool MustBeExecutedContextPrinter::runOnModule(Module &M) {
358   // We provide non-PM analysis here because the old PM doesn't like to query
359   // function passes from a module pass.
360   SmallVector<PostDominatorTree *, 8> PDTs;
361   SmallVector<DominatorTree *, 8> DTs;
362   SmallVector<LoopInfo *, 8> LIs;
363 
364   GetterTy<LoopInfo> LIGetter = [&](const Function &F) {
365     DominatorTree *DT = new DominatorTree(const_cast<Function &>(F));
366     LoopInfo *LI = new LoopInfo(*DT);
367     DTs.push_back(DT);
368     LIs.push_back(LI);
369     return LI;
370   };
371   GetterTy<DominatorTree> DTGetter = [&](const Function &F) {
372     DominatorTree *DT = new DominatorTree(const_cast<Function &>(F));
373     DTs.push_back(DT);
374     return DT;
375   };
376   GetterTy<PostDominatorTree> PDTGetter = [&](const Function &F) {
377     PostDominatorTree *PDT = new PostDominatorTree(const_cast<Function &>(F));
378     PDTs.push_back(PDT);
379     return PDT;
380   };
381   MustBeExecutedContextExplorer Explorer(
382       /* ExploreInterBlock */ true,
383       /* ExploreCFGForward */ true,
384       /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter);
385 
386   for (Function &F : M) {
387     for (Instruction &I : instructions(F)) {
388       dbgs() << "-- Explore context of: " << I << "\n";
389       for (const Instruction *CI : Explorer.range(&I))
390         dbgs() << "  [F: " << CI->getFunction()->getName() << "] " << *CI
391                << "\n";
392     }
393   }
394 
395   DeleteContainerPointers(PDTs);
396   DeleteContainerPointers(LIs);
397   DeleteContainerPointers(DTs);
398   return false;
399 }
400 
401 static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
402   // TODO: merge these two routines.  For the moment, we display the best
403   // result obtained by *either* implementation.  This is a bit unfair since no
404   // caller actually gets the full power at the moment.
405   SimpleLoopSafetyInfo LSI;
406   LSI.computeLoopSafetyInfo(L);
407   return LSI.isGuaranteedToExecute(I, DT, L) ||
408     isGuaranteedToExecuteForEveryIteration(&I, L);
409 }
410 
411 namespace {
412 /// An assembly annotator class to print must execute information in
413 /// comments.
414 class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
415   DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
416 
417 public:
418   MustExecuteAnnotatedWriter(const Function &F,
419                              DominatorTree &DT, LoopInfo &LI) {
420     for (auto &I: instructions(F)) {
421       Loop *L = LI.getLoopFor(I.getParent());
422       while (L) {
423         if (isMustExecuteIn(I, L, &DT)) {
424           MustExec[&I].push_back(L);
425         }
426         L = L->getParentLoop();
427       };
428     }
429   }
430   MustExecuteAnnotatedWriter(const Module &M,
431                              DominatorTree &DT, LoopInfo &LI) {
432     for (auto &F : M)
433     for (auto &I: instructions(F)) {
434       Loop *L = LI.getLoopFor(I.getParent());
435       while (L) {
436         if (isMustExecuteIn(I, L, &DT)) {
437           MustExec[&I].push_back(L);
438         }
439         L = L->getParentLoop();
440       };
441     }
442   }
443 
444 
445   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
446     if (!MustExec.count(&V))
447       return;
448 
449     const auto &Loops = MustExec.lookup(&V);
450     const auto NumLoops = Loops.size();
451     if (NumLoops > 1)
452       OS << " ; (mustexec in " << NumLoops << " loops: ";
453     else
454       OS << " ; (mustexec in: ";
455 
456     bool first = true;
457     for (const Loop *L : Loops) {
458       if (!first)
459         OS << ", ";
460       first = false;
461       OS << L->getHeader()->getName();
462     }
463     OS << ")";
464   }
465 };
466 } // namespace
467 
468 bool MustExecutePrinter::runOnFunction(Function &F) {
469   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
470   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
471 
472   MustExecuteAnnotatedWriter Writer(F, DT, LI);
473   F.print(dbgs(), &Writer);
474 
475   return false;
476 }
477 
478 /// Return true if \p L might be an endless loop.
479 static bool maybeEndlessLoop(const Loop &L) {
480   if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn))
481     return false;
482   // TODO: Actually try to prove it is not.
483   // TODO: If maybeEndlessLoop is going to be expensive, cache it.
484   return true;
485 }
486 
487 static bool mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) {
488   if (!LI)
489     return false;
490   using RPOTraversal = ReversePostOrderTraversal<const Function *>;
491   RPOTraversal FuncRPOT(&F);
492   return !containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
493                                  const LoopInfo>(FuncRPOT, *LI);
494 }
495 
496 /// Lookup \p Key in \p Map and return the result, potentially after
497 /// initializing the optional through \p Fn(\p args).
498 template <typename K, typename V, typename FnTy, typename... ArgsTy>
499 static V getOrCreateCachedOptional(K Key, DenseMap<K, Optional<V>> &Map,
500                                    FnTy &&Fn, ArgsTy&&... args) {
501   Optional<V> &OptVal = Map[Key];
502   if (!OptVal.hasValue())
503     OptVal = Fn(std::forward<ArgsTy>(args)...);
504   return OptVal.getValue();
505 }
506 
507 const BasicBlock *
508 MustBeExecutedContextExplorer::findForwardJoinPoint(const BasicBlock *InitBB) {
509   const LoopInfo *LI = LIGetter(*InitBB->getParent());
510   const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent());
511 
512   LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName()
513                     << (LI ? " [LI]" : "") << (PDT ? " [PDT]" : ""));
514 
515   const Function &F = *InitBB->getParent();
516   const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
517   const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB;
518   bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) ||
519                                (L && !maybeEndlessLoop(*L))) &&
520                               F.doesNotThrow();
521   LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "")
522                     << (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "")
523                     << "\n");
524 
525   // Determine the adjacent blocks in the given direction but exclude (self)
526   // loops under certain circumstances.
527   SmallVector<const BasicBlock *, 8> Worklist;
528   for (const BasicBlock *SuccBB : successors(InitBB)) {
529     bool IsLatch = SuccBB == HeaderBB;
530     // Loop latches are ignored in forward propagation if the loop cannot be
531     // endless and may not throw: control has to go somewhere.
532     if (!WillReturnAndNoThrow || !IsLatch)
533       Worklist.push_back(SuccBB);
534   }
535   LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n");
536 
537   // If there are no other adjacent blocks, there is no join point.
538   if (Worklist.empty())
539     return nullptr;
540 
541   // If there is one adjacent block, it is the join point.
542   if (Worklist.size() == 1)
543     return Worklist[0];
544 
545   // Try to determine a join block through the help of the post-dominance
546   // tree. If no tree was provided, we perform simple pattern matching for one
547   // block conditionals and one block loops only.
548   const BasicBlock *JoinBB = nullptr;
549   if (PDT)
550     if (const auto *InitNode = PDT->getNode(InitBB))
551       if (const auto *IDomNode = InitNode->getIDom())
552         JoinBB = IDomNode->getBlock();
553 
554   if (!JoinBB && Worklist.size() == 2) {
555     const BasicBlock *Succ0 = Worklist[0];
556     const BasicBlock *Succ1 = Worklist[1];
557     const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor();
558     const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor();
559     if (Succ0UniqueSucc == InitBB) {
560       // InitBB -> Succ0 -> InitBB
561       // InitBB -> Succ1  = JoinBB
562       JoinBB = Succ1;
563     } else if (Succ1UniqueSucc == InitBB) {
564       // InitBB -> Succ1 -> InitBB
565       // InitBB -> Succ0  = JoinBB
566       JoinBB = Succ0;
567     } else if (Succ0 == Succ1UniqueSucc) {
568       // InitBB ->          Succ0 = JoinBB
569       // InitBB -> Succ1 -> Succ0 = JoinBB
570       JoinBB = Succ0;
571     } else if (Succ1 == Succ0UniqueSucc) {
572       // InitBB -> Succ0 -> Succ1 = JoinBB
573       // InitBB ->          Succ1 = JoinBB
574       JoinBB = Succ1;
575     } else if (Succ0UniqueSucc == Succ1UniqueSucc) {
576       // InitBB -> Succ0 -> JoinBB
577       // InitBB -> Succ1 -> JoinBB
578       JoinBB = Succ0UniqueSucc;
579     }
580   }
581 
582   if (!JoinBB && L)
583     JoinBB = L->getUniqueExitBlock();
584 
585   if (!JoinBB)
586     return nullptr;
587 
588   LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n");
589 
590   // In forward direction we check if control will for sure reach JoinBB from
591   // InitBB, thus it can not be "stopped" along the way. Ways to "stop" control
592   // are: infinite loops and instructions that do not necessarily transfer
593   // execution to their successor. To check for them we traverse the CFG from
594   // the adjacent blocks to the JoinBB, looking at all intermediate blocks.
595 
596   // If we know the function is "will-return" and "no-throw" there is no need
597   // for futher checks.
598   if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) {
599 
600     auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) {
601       return isGuaranteedToTransferExecutionToSuccessor(BB);
602     };
603 
604     SmallPtrSet<const BasicBlock *, 16> Visited;
605     while (!Worklist.empty()) {
606       const BasicBlock *ToBB = Worklist.pop_back_val();
607       if (ToBB == JoinBB)
608         continue;
609 
610       // Make sure all loops in-between are finite.
611       if (!Visited.insert(ToBB).second) {
612         if (!F.hasFnAttribute(Attribute::WillReturn)) {
613           if (!LI)
614             return nullptr;
615 
616           bool MayContainIrreducibleControl = getOrCreateCachedOptional(
617               &F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI);
618           if (MayContainIrreducibleControl)
619             return nullptr;
620 
621           const Loop *L = LI->getLoopFor(ToBB);
622           if (L && maybeEndlessLoop(*L))
623             return nullptr;
624         }
625 
626         continue;
627       }
628 
629       // Make sure the block has no instructions that could stop control
630       // transfer.
631       bool TransfersExecution = getOrCreateCachedOptional(
632           ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB);
633       if (!TransfersExecution)
634         return nullptr;
635 
636       for (const BasicBlock *AdjacentBB : successors(ToBB))
637         Worklist.push_back(AdjacentBB);
638     }
639   }
640 
641   LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n");
642   return JoinBB;
643 }
644 const BasicBlock *
645 MustBeExecutedContextExplorer::findBackwardJoinPoint(const BasicBlock *InitBB) {
646   const LoopInfo *LI = LIGetter(*InitBB->getParent());
647   const DominatorTree *DT = DTGetter(*InitBB->getParent());
648   LLVM_DEBUG(dbgs() << "\tFind backward join point for " << InitBB->getName()
649                     << (LI ? " [LI]" : "") << (DT ? " [DT]" : ""));
650 
651   // Try to determine a join block through the help of the dominance tree. If no
652   // tree was provided, we perform simple pattern matching for one block
653   // conditionals only.
654   if (DT)
655     if (const auto *InitNode = DT->getNode(InitBB))
656       if (const auto *IDomNode = InitNode->getIDom())
657         return IDomNode->getBlock();
658 
659   const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
660   const BasicBlock *HeaderBB = L ? L->getHeader() : nullptr;
661 
662   // Determine the predecessor blocks but ignore backedges.
663   SmallVector<const BasicBlock *, 8> Worklist;
664   for (const BasicBlock *PredBB : predecessors(InitBB)) {
665     bool IsBackedge =
666         (PredBB == InitBB) || (HeaderBB == InitBB && L->contains(PredBB));
667     // Loop backedges are ignored in backwards propagation: control has to come
668     // from somewhere.
669     if (!IsBackedge)
670       Worklist.push_back(PredBB);
671   }
672 
673   // If there are no other predecessor blocks, there is no join point.
674   if (Worklist.empty())
675     return nullptr;
676 
677   // If there is one predecessor block, it is the join point.
678   if (Worklist.size() == 1)
679     return Worklist[0];
680 
681   const BasicBlock *JoinBB = nullptr;
682   if (Worklist.size() == 2) {
683     const BasicBlock *Pred0 = Worklist[0];
684     const BasicBlock *Pred1 = Worklist[1];
685     const BasicBlock *Pred0UniquePred = Pred0->getUniquePredecessor();
686     const BasicBlock *Pred1UniquePred = Pred1->getUniquePredecessor();
687     if (Pred0 == Pred1UniquePred) {
688       // InitBB <-          Pred0 = JoinBB
689       // InitBB <- Pred1 <- Pred0 = JoinBB
690       JoinBB = Pred0;
691     } else if (Pred1 == Pred0UniquePred) {
692       // InitBB <- Pred0 <- Pred1 = JoinBB
693       // InitBB <-          Pred1 = JoinBB
694       JoinBB = Pred1;
695     } else if (Pred0UniquePred == Pred1UniquePred) {
696       // InitBB <- Pred0 <- JoinBB
697       // InitBB <- Pred1 <- JoinBB
698       JoinBB = Pred0UniquePred;
699     }
700   }
701 
702   if (!JoinBB && L)
703     JoinBB = L->getHeader();
704 
705   // In backwards direction there is no need to show termination of previous
706   // instructions. If they do not terminate, the code afterward is dead, making
707   // any information/transformation correct anyway.
708   return JoinBB;
709 }
710 
711 const Instruction *
712 MustBeExecutedContextExplorer::getMustBeExecutedNextInstruction(
713     MustBeExecutedIterator &It, const Instruction *PP) {
714   if (!PP)
715     return PP;
716   LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n");
717 
718   // If we explore only inside a given basic block we stop at terminators.
719   if (!ExploreInterBlock && PP->isTerminator()) {
720     LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n");
721     return nullptr;
722   }
723 
724   // If we do not traverse the call graph we check if we can make progress in
725   // the current function. First, check if the instruction is guaranteed to
726   // transfer execution to the successor.
727   bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP);
728   if (!TransfersExecution)
729     return nullptr;
730 
731   // If this is not a terminator we know that there is a single instruction
732   // after this one that is executed next if control is transfered. If not,
733   // we can try to go back to a call site we entered earlier. If none exists, we
734   // do not know any instruction that has to be executd next.
735   if (!PP->isTerminator()) {
736     const Instruction *NextPP = PP->getNextNode();
737     LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n");
738     return NextPP;
739   }
740 
741   // Finally, we have to handle terminators, trivial ones first.
742   assert(PP->isTerminator() && "Expected a terminator!");
743 
744   // A terminator without a successor is not handled yet.
745   if (PP->getNumSuccessors() == 0) {
746     LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n");
747     return nullptr;
748   }
749 
750   // A terminator with a single successor, we will continue at the beginning of
751   // that one.
752   if (PP->getNumSuccessors() == 1) {
753     LLVM_DEBUG(
754         dbgs() << "\tUnconditional terminator, continue with successor\n");
755     return &PP->getSuccessor(0)->front();
756   }
757 
758   // Multiple successors mean we need to find the join point where control flow
759   // converges again. We use the findForwardJoinPoint helper function with
760   // information about the function and helper analyses, if available.
761   if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent()))
762     return &JoinBB->front();
763 
764   LLVM_DEBUG(dbgs() << "\tNo join point found\n");
765   return nullptr;
766 }
767 
768 const Instruction *
769 MustBeExecutedContextExplorer::getMustBeExecutedPrevInstruction(
770     MustBeExecutedIterator &It, const Instruction *PP) {
771   if (!PP)
772     return PP;
773 
774   bool IsFirst = !(PP->getPrevNode());
775   LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP
776                     << (IsFirst ? " [IsFirst]" : "") << "\n");
777 
778   // If we explore only inside a given basic block we stop at the first
779   // instruction.
780   if (!ExploreInterBlock && IsFirst) {
781     LLVM_DEBUG(dbgs() << "\tReached block front in intra-block mode, done\n");
782     return nullptr;
783   }
784 
785   // The block and function that contains the current position.
786   const BasicBlock *PPBlock = PP->getParent();
787 
788   // If we are inside a block we know what instruction was executed before, the
789   // previous one.
790   if (!IsFirst) {
791     const Instruction *PrevPP = PP->getPrevNode();
792     LLVM_DEBUG(
793         dbgs() << "\tIntermediate instruction, continue with previous\n");
794     // We did not enter a callee so we simply return the previous instruction.
795     return PrevPP;
796   }
797 
798   // Finally, we have to handle the case where the program point is the first in
799   // a block but not in the function. We use the findBackwardJoinPoint helper
800   // function with information about the function and helper analyses, if
801   // available.
802   if (const BasicBlock *JoinBB = findBackwardJoinPoint(PPBlock))
803     return &JoinBB->back();
804 
805   LLVM_DEBUG(dbgs() << "\tNo join point found\n");
806   return nullptr;
807 }
808 
809 MustBeExecutedIterator::MustBeExecutedIterator(
810     MustBeExecutedContextExplorer &Explorer, const Instruction *I)
811     : Explorer(Explorer), CurInst(I) {
812   reset(I);
813 }
814 
815 void MustBeExecutedIterator::reset(const Instruction *I) {
816   Visited.clear();
817   resetInstruction(I);
818 }
819 
820 void MustBeExecutedIterator::resetInstruction(const Instruction *I) {
821   CurInst = I;
822   Head = Tail = nullptr;
823   Visited.insert({I, ExplorationDirection::FORWARD});
824   Visited.insert({I, ExplorationDirection::BACKWARD});
825   if (Explorer.ExploreCFGForward)
826     Head = I;
827   if (Explorer.ExploreCFGBackward)
828     Tail = I;
829 }
830 
831 const Instruction *MustBeExecutedIterator::advance() {
832   assert(CurInst && "Cannot advance an end iterator!");
833   Head = Explorer.getMustBeExecutedNextInstruction(*this, Head);
834   if (Head && Visited.insert({Head, ExplorationDirection ::FORWARD}).second)
835     return Head;
836   Head = nullptr;
837 
838   Tail = Explorer.getMustBeExecutedPrevInstruction(*this, Tail);
839   if (Tail && Visited.insert({Tail, ExplorationDirection ::BACKWARD}).second)
840     return Tail;
841   Tail = nullptr;
842   return nullptr;
843 }
844