xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/MustExecute.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/Analysis/MustExecute.h"
108bcb0991SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
11fe6060f1SDimitry Andric #include "llvm/ADT/StringExtras.h"
128bcb0991SDimitry Andric #include "llvm/Analysis/CFG.h"
130b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
140b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/Passes.h"
16480093f4SDimitry Andric #include "llvm/Analysis/PostDominators.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
180b57cec5SDimitry Andric #include "llvm/IR/AssemblyAnnotationWriter.h"
19e8d8bef9SDimitry Andric #include "llvm/IR/Dominators.h"
200b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
210b57cec5SDimitry Andric #include "llvm/IR/Module.h"
22e8d8bef9SDimitry Andric #include "llvm/IR/PassManager.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
268bcb0991SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric 
298bcb0991SDimitry Andric #define DEBUG_TYPE "must-execute"
308bcb0991SDimitry Andric 
310b57cec5SDimitry Andric const DenseMap<BasicBlock *, ColorVector> &
320b57cec5SDimitry Andric LoopSafetyInfo::getBlockColors() const {
330b57cec5SDimitry Andric   return BlockColors;
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) {
370b57cec5SDimitry Andric   ColorVector &ColorsForNewBlock = BlockColors[New];
380b57cec5SDimitry Andric   ColorVector &ColorsForOldBlock = BlockColors[Old];
390b57cec5SDimitry Andric   ColorsForNewBlock = ColorsForOldBlock;
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
430b57cec5SDimitry Andric   (void)BB;
440b57cec5SDimitry Andric   return anyBlockMayThrow();
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric bool SimpleLoopSafetyInfo::anyBlockMayThrow() const {
480b57cec5SDimitry Andric   return MayThrow;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
520b57cec5SDimitry Andric   assert(CurLoop != nullptr && "CurLoop can't be null");
530b57cec5SDimitry Andric   BasicBlock *Header = CurLoop->getHeader();
540b57cec5SDimitry Andric   // Iterate over header and compute safety info.
550b57cec5SDimitry Andric   HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
560b57cec5SDimitry Andric   MayThrow = HeaderMayThrow;
570b57cec5SDimitry Andric   // Iterate over loop instructions and compute safety info.
580b57cec5SDimitry Andric   // Skip header as it has been computed and stored in HeaderMayThrow.
590b57cec5SDimitry Andric   // The first block in loopinfo.Blocks is guaranteed to be the header.
600b57cec5SDimitry Andric   assert(Header == *CurLoop->getBlocks().begin() &&
610b57cec5SDimitry Andric          "First block must be header");
62bdd1243dSDimitry Andric   for (const BasicBlock *BB : llvm::drop_begin(CurLoop->blocks())) {
63bdd1243dSDimitry Andric     MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(BB);
64bdd1243dSDimitry Andric     if (MayThrow)
65bdd1243dSDimitry Andric       break;
66bdd1243dSDimitry Andric   }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   computeBlockColors(CurLoop);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
720b57cec5SDimitry Andric   return ICF.hasICF(BB);
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric bool ICFLoopSafetyInfo::anyBlockMayThrow() const {
760b57cec5SDimitry Andric   return MayThrow;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
800b57cec5SDimitry Andric   assert(CurLoop != nullptr && "CurLoop can't be null");
810b57cec5SDimitry Andric   ICF.clear();
820b57cec5SDimitry Andric   MW.clear();
830b57cec5SDimitry Andric   MayThrow = false;
840b57cec5SDimitry Andric   // Figure out the fact that at least one block may throw.
85fcaf7f86SDimitry Andric   for (const auto &BB : CurLoop->blocks())
860b57cec5SDimitry Andric     if (ICF.hasICF(&*BB)) {
870b57cec5SDimitry Andric       MayThrow = true;
880b57cec5SDimitry Andric       break;
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric   computeBlockColors(CurLoop);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst,
940b57cec5SDimitry Andric                                             const BasicBlock *BB) {
950b57cec5SDimitry Andric   ICF.insertInstructionTo(Inst, BB);
960b57cec5SDimitry Andric   MW.insertInstructionTo(Inst, BB);
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
1000b57cec5SDimitry Andric   ICF.removeInstruction(Inst);
1010b57cec5SDimitry Andric   MW.removeInstruction(Inst);
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
1050b57cec5SDimitry Andric   // Compute funclet colors if we might sink/hoist in a function with a funclet
1060b57cec5SDimitry Andric   // personality routine.
1070b57cec5SDimitry Andric   Function *Fn = CurLoop->getHeader()->getParent();
1080b57cec5SDimitry Andric   if (Fn->hasPersonalityFn())
1090b57cec5SDimitry Andric     if (Constant *PersonalityFn = Fn->getPersonalityFn())
1100b57cec5SDimitry Andric       if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
1110b57cec5SDimitry Andric         BlockColors = colorEHFunclets(*Fn);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /// Return true if we can prove that the given ExitBlock is not reached on the
1150b57cec5SDimitry Andric /// first iteration of the given loop.  That is, the backedge of the loop must
1160b57cec5SDimitry Andric /// be executed before the ExitBlock is executed in any dynamic execution trace.
1170b57cec5SDimitry Andric static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
1180b57cec5SDimitry Andric                                            const DominatorTree *DT,
1190b57cec5SDimitry Andric                                            const Loop *CurLoop) {
1200b57cec5SDimitry Andric   auto *CondExitBlock = ExitBlock->getSinglePredecessor();
1210b57cec5SDimitry Andric   if (!CondExitBlock)
1220b57cec5SDimitry Andric     // expect unique exits
1230b57cec5SDimitry Andric     return false;
1240b57cec5SDimitry Andric   assert(CurLoop->contains(CondExitBlock) && "meaning of exit block");
1250b57cec5SDimitry Andric   auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator());
1260b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
1270b57cec5SDimitry Andric     return false;
1280b57cec5SDimitry Andric   // If condition is constant and false leads to ExitBlock then we always
1290b57cec5SDimitry Andric   // execute the true branch.
1300b57cec5SDimitry Andric   if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition()))
1310b57cec5SDimitry Andric     return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock;
1320b57cec5SDimitry Andric   auto *Cond = dyn_cast<CmpInst>(BI->getCondition());
1330b57cec5SDimitry Andric   if (!Cond)
1340b57cec5SDimitry Andric     return false;
1350b57cec5SDimitry Andric   // todo: this would be a lot more powerful if we used scev, but all the
1360b57cec5SDimitry Andric   // plumbing is currently missing to pass a pointer in from the pass
1370b57cec5SDimitry Andric   // Check for cmp (phi [x, preheader] ...), y where (pred x, y is known
1380b57cec5SDimitry Andric   auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0));
1390b57cec5SDimitry Andric   auto *RHS = Cond->getOperand(1);
1400b57cec5SDimitry Andric   if (!LHS || LHS->getParent() != CurLoop->getHeader())
1410b57cec5SDimitry Andric     return false;
142*0fca6ea1SDimitry Andric   auto DL = ExitBlock->getDataLayout();
1430b57cec5SDimitry Andric   auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
14481ad6265SDimitry Andric   auto *SimpleValOrNull = simplifyCmpInst(Cond->getPredicate(),
1450b57cec5SDimitry Andric                                           IVStart, RHS,
1460b57cec5SDimitry Andric                                           {DL, /*TLI*/ nullptr,
1470b57cec5SDimitry Andric                                               DT, /*AC*/ nullptr, BI});
1480b57cec5SDimitry Andric   auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
1490b57cec5SDimitry Andric   if (!SimpleCst)
1500b57cec5SDimitry Andric     return false;
1510b57cec5SDimitry Andric   if (ExitBlock == BI->getSuccessor(0))
1520b57cec5SDimitry Andric     return SimpleCst->isZeroValue();
1530b57cec5SDimitry Andric   assert(ExitBlock == BI->getSuccessor(1) && "implied by above");
1540b57cec5SDimitry Andric   return SimpleCst->isAllOnesValue();
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric /// Collect all blocks from \p CurLoop which lie on all possible paths from
1580b57cec5SDimitry Andric /// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
1590b57cec5SDimitry Andric /// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
1600b57cec5SDimitry Andric static void collectTransitivePredecessors(
1610b57cec5SDimitry Andric     const Loop *CurLoop, const BasicBlock *BB,
1620b57cec5SDimitry Andric     SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
1630b57cec5SDimitry Andric   assert(Predecessors.empty() && "Garbage in predecessors set?");
1640b57cec5SDimitry Andric   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
1650b57cec5SDimitry Andric   if (BB == CurLoop->getHeader())
1660b57cec5SDimitry Andric     return;
1670b57cec5SDimitry Andric   SmallVector<const BasicBlock *, 4> WorkList;
168fcaf7f86SDimitry Andric   for (const auto *Pred : predecessors(BB)) {
1690b57cec5SDimitry Andric     Predecessors.insert(Pred);
1700b57cec5SDimitry Andric     WorkList.push_back(Pred);
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric   while (!WorkList.empty()) {
1730b57cec5SDimitry Andric     auto *Pred = WorkList.pop_back_val();
1740b57cec5SDimitry Andric     assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
1750b57cec5SDimitry Andric     // We are not interested in backedges and we don't want to leave loop.
1760b57cec5SDimitry Andric     if (Pred == CurLoop->getHeader())
1770b57cec5SDimitry Andric       continue;
1780b57cec5SDimitry Andric     // TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
1790b57cec5SDimitry Andric     // blocks of this inner loop, even those that are always executed AFTER the
1800b57cec5SDimitry Andric     // BB. It may make our analysis more conservative than it could be, see test
1810b57cec5SDimitry Andric     // @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
1820b57cec5SDimitry Andric     // We can ignore backedge of all loops containing BB to get a sligtly more
1830b57cec5SDimitry Andric     // optimistic result.
184fcaf7f86SDimitry Andric     for (const auto *PredPred : predecessors(Pred))
1850b57cec5SDimitry Andric       if (Predecessors.insert(PredPred).second)
1860b57cec5SDimitry Andric         WorkList.push_back(PredPred);
1870b57cec5SDimitry Andric   }
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
1910b57cec5SDimitry Andric                                              const BasicBlock *BB,
1920b57cec5SDimitry Andric                                              const DominatorTree *DT) const {
1930b57cec5SDimitry Andric   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   // Fast path: header is always reached once the loop is entered.
1960b57cec5SDimitry Andric   if (BB == CurLoop->getHeader())
1970b57cec5SDimitry Andric     return true;
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   // Collect all transitive predecessors of BB in the same loop. This set will
2000b57cec5SDimitry Andric   // be a subset of the blocks within the loop.
2010b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 4> Predecessors;
2020b57cec5SDimitry Andric   collectTransitivePredecessors(CurLoop, BB, Predecessors);
2030b57cec5SDimitry Andric 
204bdd1243dSDimitry Andric   // Bail out if a latch block is part of the predecessor set. In this case
205bdd1243dSDimitry Andric   // we may take the backedge to the header and not execute other latch
206bdd1243dSDimitry Andric   // successors.
207bdd1243dSDimitry Andric   for (const BasicBlock *Pred : predecessors(CurLoop->getHeader()))
208bdd1243dSDimitry Andric     // Predecessors only contains loop blocks, so we don't have to worry about
209bdd1243dSDimitry Andric     // preheader predecessors here.
210bdd1243dSDimitry Andric     if (Predecessors.contains(Pred))
211bdd1243dSDimitry Andric       return false;
212bdd1243dSDimitry Andric 
2130b57cec5SDimitry Andric   // Make sure that all successors of, all predecessors of BB which are not
2140b57cec5SDimitry Andric   // dominated by BB, are either:
2150b57cec5SDimitry Andric   // 1) BB,
2160b57cec5SDimitry Andric   // 2) Also predecessors of BB,
2170b57cec5SDimitry Andric   // 3) Exit blocks which are not taken on 1st iteration.
2180b57cec5SDimitry Andric   // Memoize blocks we've already checked.
2190b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
220fcaf7f86SDimitry Andric   for (const auto *Pred : Predecessors) {
2210b57cec5SDimitry Andric     // Predecessor block may throw, so it has a side exit.
2220b57cec5SDimitry Andric     if (blockMayThrow(Pred))
2230b57cec5SDimitry Andric       return false;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     // BB dominates Pred, so if Pred runs, BB must run.
2260b57cec5SDimitry Andric     // This is true when Pred is a loop latch.
2270b57cec5SDimitry Andric     if (DT->dominates(BB, Pred))
2280b57cec5SDimitry Andric       continue;
2290b57cec5SDimitry Andric 
230fcaf7f86SDimitry Andric     for (const auto *Succ : successors(Pred))
2310b57cec5SDimitry Andric       if (CheckedSuccessors.insert(Succ).second &&
2320b57cec5SDimitry Andric           Succ != BB && !Predecessors.count(Succ))
2330b57cec5SDimitry Andric         // By discharging conditions that are not executed on the 1st iteration,
2340b57cec5SDimitry Andric         // we guarantee that *at least* on the first iteration all paths from
2350b57cec5SDimitry Andric         // header that *may* execute will lead us to the block of interest. So
2360b57cec5SDimitry Andric         // that if we had virtually peeled one iteration away, in this peeled
2370b57cec5SDimitry Andric         // iteration the set of predecessors would contain only paths from
2380b57cec5SDimitry Andric         // header to BB without any exiting edges that may execute.
2390b57cec5SDimitry Andric         //
2400b57cec5SDimitry Andric         // TODO: We only do it for exiting edges currently. We could use the
2410b57cec5SDimitry Andric         // same function to skip some of the edges within the loop if we know
2420b57cec5SDimitry Andric         // that they will not be taken on the 1st iteration.
2430b57cec5SDimitry Andric         //
2440b57cec5SDimitry Andric         // TODO: If we somehow know the number of iterations in loop, the same
2450b57cec5SDimitry Andric         // check may be done for any arbitrary N-th iteration as long as N is
2460b57cec5SDimitry Andric         // not greater than minimum number of iterations in this loop.
2470b57cec5SDimitry Andric         if (CurLoop->contains(Succ) ||
2480b57cec5SDimitry Andric             !CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
2490b57cec5SDimitry Andric           return false;
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // All predecessors can only lead us to BB.
2530b57cec5SDimitry Andric   return true;
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric /// Returns true if the instruction in a loop is guaranteed to execute at least
2570b57cec5SDimitry Andric /// once.
2580b57cec5SDimitry Andric bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
2590b57cec5SDimitry Andric                                                  const DominatorTree *DT,
2600b57cec5SDimitry Andric                                                  const Loop *CurLoop) const {
2610b57cec5SDimitry Andric   // If the instruction is in the header block for the loop (which is very
2620b57cec5SDimitry Andric   // common), it is always guaranteed to dominate the exit blocks.  Since this
2630b57cec5SDimitry Andric   // is a common case, and can save some work, check it now.
2640b57cec5SDimitry Andric   if (Inst.getParent() == CurLoop->getHeader())
2650b57cec5SDimitry Andric     // If there's a throw in the header block, we can't guarantee we'll reach
2660b57cec5SDimitry Andric     // Inst unless we can prove that Inst comes before the potential implicit
2670b57cec5SDimitry Andric     // exit.  At the moment, we use a (cheap) hack for the common case where
2680b57cec5SDimitry Andric     // the instruction of interest is the first one in the block.
2690b57cec5SDimitry Andric     return !HeaderMayThrow ||
2700b57cec5SDimitry Andric            Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   // If there is a path from header to exit or latch that doesn't lead to our
2730b57cec5SDimitry Andric   // instruction's block, return false.
2740b57cec5SDimitry Andric   return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
2780b57cec5SDimitry Andric                                               const DominatorTree *DT,
2790b57cec5SDimitry Andric                                               const Loop *CurLoop) const {
2800b57cec5SDimitry Andric   return !ICF.isDominatedByICFIFromSameBlock(&Inst) &&
2810b57cec5SDimitry Andric          allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB,
2850b57cec5SDimitry Andric                                                  const Loop *CurLoop) const {
2860b57cec5SDimitry Andric   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // Fast path: there are no instructions before header.
2890b57cec5SDimitry Andric   if (BB == CurLoop->getHeader())
2900b57cec5SDimitry Andric     return true;
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   // Collect all transitive predecessors of BB in the same loop. This set will
2930b57cec5SDimitry Andric   // be a subset of the blocks within the loop.
2940b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 4> Predecessors;
2950b57cec5SDimitry Andric   collectTransitivePredecessors(CurLoop, BB, Predecessors);
2960b57cec5SDimitry Andric   // Find if there any instruction in either predecessor that could write
2970b57cec5SDimitry Andric   // to memory.
298fcaf7f86SDimitry Andric   for (const auto *Pred : Predecessors)
2990b57cec5SDimitry Andric     if (MW.mayWriteToMemory(Pred))
3000b57cec5SDimitry Andric       return false;
3010b57cec5SDimitry Andric   return true;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I,
3050b57cec5SDimitry Andric                                                  const Loop *CurLoop) const {
3060b57cec5SDimitry Andric   auto *BB = I.getParent();
3070b57cec5SDimitry Andric   assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
3080b57cec5SDimitry Andric   return !MW.isDominatedByMemoryWriteFromSameBlock(&I) &&
3090b57cec5SDimitry Andric          doesNotWriteMemoryBefore(BB, CurLoop);
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
3130b57cec5SDimitry Andric   // TODO: merge these two routines.  For the moment, we display the best
3140b57cec5SDimitry Andric   // result obtained by *either* implementation.  This is a bit unfair since no
3150b57cec5SDimitry Andric   // caller actually gets the full power at the moment.
3160b57cec5SDimitry Andric   SimpleLoopSafetyInfo LSI;
3170b57cec5SDimitry Andric   LSI.computeLoopSafetyInfo(L);
3180b57cec5SDimitry Andric   return LSI.isGuaranteedToExecute(I, DT, L) ||
3190b57cec5SDimitry Andric     isGuaranteedToExecuteForEveryIteration(&I, L);
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric namespace {
3230b57cec5SDimitry Andric /// An assembly annotator class to print must execute information in
3240b57cec5SDimitry Andric /// comments.
3250b57cec5SDimitry Andric class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
3260b57cec5SDimitry Andric   DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric public:
3290b57cec5SDimitry Andric   MustExecuteAnnotatedWriter(const Function &F,
3300b57cec5SDimitry Andric                              DominatorTree &DT, LoopInfo &LI) {
331fcaf7f86SDimitry Andric     for (const auto &I: instructions(F)) {
3320b57cec5SDimitry Andric       Loop *L = LI.getLoopFor(I.getParent());
3330b57cec5SDimitry Andric       while (L) {
3340b57cec5SDimitry Andric         if (isMustExecuteIn(I, L, &DT)) {
3350b57cec5SDimitry Andric           MustExec[&I].push_back(L);
3360b57cec5SDimitry Andric         }
3370b57cec5SDimitry Andric         L = L->getParentLoop();
3380b57cec5SDimitry Andric       };
3390b57cec5SDimitry Andric     }
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric   MustExecuteAnnotatedWriter(const Module &M,
3420b57cec5SDimitry Andric                              DominatorTree &DT, LoopInfo &LI) {
343fcaf7f86SDimitry Andric     for (const auto &F : M)
344fcaf7f86SDimitry Andric     for (const auto &I: instructions(F)) {
3450b57cec5SDimitry Andric       Loop *L = LI.getLoopFor(I.getParent());
3460b57cec5SDimitry Andric       while (L) {
3470b57cec5SDimitry Andric         if (isMustExecuteIn(I, L, &DT)) {
3480b57cec5SDimitry Andric           MustExec[&I].push_back(L);
3490b57cec5SDimitry Andric         }
3500b57cec5SDimitry Andric         L = L->getParentLoop();
3510b57cec5SDimitry Andric       };
3520b57cec5SDimitry Andric     }
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
3570b57cec5SDimitry Andric     if (!MustExec.count(&V))
3580b57cec5SDimitry Andric       return;
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric     const auto &Loops = MustExec.lookup(&V);
3610b57cec5SDimitry Andric     const auto NumLoops = Loops.size();
3620b57cec5SDimitry Andric     if (NumLoops > 1)
3630b57cec5SDimitry Andric       OS << " ; (mustexec in " << NumLoops << " loops: ";
3640b57cec5SDimitry Andric     else
3650b57cec5SDimitry Andric       OS << " ; (mustexec in: ";
3660b57cec5SDimitry Andric 
367fe6060f1SDimitry Andric     ListSeparator LS;
368fe6060f1SDimitry Andric     for (const Loop *L : Loops)
369fe6060f1SDimitry Andric       OS << LS << L->getHeader()->getName();
3700b57cec5SDimitry Andric     OS << ")";
3710b57cec5SDimitry Andric   }
3720b57cec5SDimitry Andric };
3730b57cec5SDimitry Andric } // namespace
3740b57cec5SDimitry Andric 
375480093f4SDimitry Andric /// Return true if \p L might be an endless loop.
376480093f4SDimitry Andric static bool maybeEndlessLoop(const Loop &L) {
377480093f4SDimitry Andric   if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn))
378480093f4SDimitry Andric     return false;
379480093f4SDimitry Andric   // TODO: Actually try to prove it is not.
380480093f4SDimitry Andric   // TODO: If maybeEndlessLoop is going to be expensive, cache it.
381480093f4SDimitry Andric   return true;
382480093f4SDimitry Andric }
383480093f4SDimitry Andric 
3845ffd83dbSDimitry Andric bool llvm::mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) {
385480093f4SDimitry Andric   if (!LI)
386480093f4SDimitry Andric     return false;
387480093f4SDimitry Andric   using RPOTraversal = ReversePostOrderTraversal<const Function *>;
388480093f4SDimitry Andric   RPOTraversal FuncRPOT(&F);
3895ffd83dbSDimitry Andric   return containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
390480093f4SDimitry Andric                                 const LoopInfo>(FuncRPOT, *LI);
391480093f4SDimitry Andric }
392480093f4SDimitry Andric 
393480093f4SDimitry Andric /// Lookup \p Key in \p Map and return the result, potentially after
394480093f4SDimitry Andric /// initializing the optional through \p Fn(\p args).
395480093f4SDimitry Andric template <typename K, typename V, typename FnTy, typename... ArgsTy>
396bdd1243dSDimitry Andric static V getOrCreateCachedOptional(K Key, DenseMap<K, std::optional<V>> &Map,
397480093f4SDimitry Andric                                    FnTy &&Fn, ArgsTy &&...args) {
398bdd1243dSDimitry Andric   std::optional<V> &OptVal = Map[Key];
39981ad6265SDimitry Andric   if (!OptVal)
400480093f4SDimitry Andric     OptVal = Fn(std::forward<ArgsTy>(args)...);
401bdd1243dSDimitry Andric   return *OptVal;
402480093f4SDimitry Andric }
403480093f4SDimitry Andric 
404480093f4SDimitry Andric const BasicBlock *
405480093f4SDimitry Andric MustBeExecutedContextExplorer::findForwardJoinPoint(const BasicBlock *InitBB) {
406480093f4SDimitry Andric   const LoopInfo *LI = LIGetter(*InitBB->getParent());
407480093f4SDimitry Andric   const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent());
408480093f4SDimitry Andric 
409480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName()
410480093f4SDimitry Andric                     << (LI ? " [LI]" : "") << (PDT ? " [PDT]" : ""));
411480093f4SDimitry Andric 
412480093f4SDimitry Andric   const Function &F = *InitBB->getParent();
413480093f4SDimitry Andric   const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
414480093f4SDimitry Andric   const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB;
415480093f4SDimitry Andric   bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) ||
416480093f4SDimitry Andric                                (L && !maybeEndlessLoop(*L))) &&
417480093f4SDimitry Andric                               F.doesNotThrow();
418480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "")
419480093f4SDimitry Andric                     << (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "")
420480093f4SDimitry Andric                     << "\n");
421480093f4SDimitry Andric 
422480093f4SDimitry Andric   // Determine the adjacent blocks in the given direction but exclude (self)
423480093f4SDimitry Andric   // loops under certain circumstances.
424480093f4SDimitry Andric   SmallVector<const BasicBlock *, 8> Worklist;
425480093f4SDimitry Andric   for (const BasicBlock *SuccBB : successors(InitBB)) {
426480093f4SDimitry Andric     bool IsLatch = SuccBB == HeaderBB;
427480093f4SDimitry Andric     // Loop latches are ignored in forward propagation if the loop cannot be
428480093f4SDimitry Andric     // endless and may not throw: control has to go somewhere.
429480093f4SDimitry Andric     if (!WillReturnAndNoThrow || !IsLatch)
430480093f4SDimitry Andric       Worklist.push_back(SuccBB);
431480093f4SDimitry Andric   }
432480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n");
433480093f4SDimitry Andric 
434480093f4SDimitry Andric   // If there are no other adjacent blocks, there is no join point.
435480093f4SDimitry Andric   if (Worklist.empty())
436480093f4SDimitry Andric     return nullptr;
437480093f4SDimitry Andric 
438480093f4SDimitry Andric   // If there is one adjacent block, it is the join point.
439480093f4SDimitry Andric   if (Worklist.size() == 1)
440480093f4SDimitry Andric     return Worklist[0];
441480093f4SDimitry Andric 
442480093f4SDimitry Andric   // Try to determine a join block through the help of the post-dominance
443480093f4SDimitry Andric   // tree. If no tree was provided, we perform simple pattern matching for one
444480093f4SDimitry Andric   // block conditionals and one block loops only.
445480093f4SDimitry Andric   const BasicBlock *JoinBB = nullptr;
446480093f4SDimitry Andric   if (PDT)
447480093f4SDimitry Andric     if (const auto *InitNode = PDT->getNode(InitBB))
448480093f4SDimitry Andric       if (const auto *IDomNode = InitNode->getIDom())
449480093f4SDimitry Andric         JoinBB = IDomNode->getBlock();
450480093f4SDimitry Andric 
451480093f4SDimitry Andric   if (!JoinBB && Worklist.size() == 2) {
452480093f4SDimitry Andric     const BasicBlock *Succ0 = Worklist[0];
453480093f4SDimitry Andric     const BasicBlock *Succ1 = Worklist[1];
454480093f4SDimitry Andric     const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor();
455480093f4SDimitry Andric     const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor();
456480093f4SDimitry Andric     if (Succ0UniqueSucc == InitBB) {
457480093f4SDimitry Andric       // InitBB -> Succ0 -> InitBB
458480093f4SDimitry Andric       // InitBB -> Succ1  = JoinBB
459480093f4SDimitry Andric       JoinBB = Succ1;
460480093f4SDimitry Andric     } else if (Succ1UniqueSucc == InitBB) {
461480093f4SDimitry Andric       // InitBB -> Succ1 -> InitBB
462480093f4SDimitry Andric       // InitBB -> Succ0  = JoinBB
463480093f4SDimitry Andric       JoinBB = Succ0;
464480093f4SDimitry Andric     } else if (Succ0 == Succ1UniqueSucc) {
465480093f4SDimitry Andric       // InitBB ->          Succ0 = JoinBB
466480093f4SDimitry Andric       // InitBB -> Succ1 -> Succ0 = JoinBB
467480093f4SDimitry Andric       JoinBB = Succ0;
468480093f4SDimitry Andric     } else if (Succ1 == Succ0UniqueSucc) {
469480093f4SDimitry Andric       // InitBB -> Succ0 -> Succ1 = JoinBB
470480093f4SDimitry Andric       // InitBB ->          Succ1 = JoinBB
471480093f4SDimitry Andric       JoinBB = Succ1;
472480093f4SDimitry Andric     } else if (Succ0UniqueSucc == Succ1UniqueSucc) {
473480093f4SDimitry Andric       // InitBB -> Succ0 -> JoinBB
474480093f4SDimitry Andric       // InitBB -> Succ1 -> JoinBB
475480093f4SDimitry Andric       JoinBB = Succ0UniqueSucc;
476480093f4SDimitry Andric     }
477480093f4SDimitry Andric   }
478480093f4SDimitry Andric 
479480093f4SDimitry Andric   if (!JoinBB && L)
480480093f4SDimitry Andric     JoinBB = L->getUniqueExitBlock();
481480093f4SDimitry Andric 
482480093f4SDimitry Andric   if (!JoinBB)
483480093f4SDimitry Andric     return nullptr;
484480093f4SDimitry Andric 
485480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n");
486480093f4SDimitry Andric 
487480093f4SDimitry Andric   // In forward direction we check if control will for sure reach JoinBB from
488480093f4SDimitry Andric   // InitBB, thus it can not be "stopped" along the way. Ways to "stop" control
489480093f4SDimitry Andric   // are: infinite loops and instructions that do not necessarily transfer
490480093f4SDimitry Andric   // execution to their successor. To check for them we traverse the CFG from
491480093f4SDimitry Andric   // the adjacent blocks to the JoinBB, looking at all intermediate blocks.
492480093f4SDimitry Andric 
493480093f4SDimitry Andric   // If we know the function is "will-return" and "no-throw" there is no need
494480093f4SDimitry Andric   // for futher checks.
495480093f4SDimitry Andric   if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) {
496480093f4SDimitry Andric 
497480093f4SDimitry Andric     auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) {
498480093f4SDimitry Andric       return isGuaranteedToTransferExecutionToSuccessor(BB);
499480093f4SDimitry Andric     };
500480093f4SDimitry Andric 
501480093f4SDimitry Andric     SmallPtrSet<const BasicBlock *, 16> Visited;
502480093f4SDimitry Andric     while (!Worklist.empty()) {
503480093f4SDimitry Andric       const BasicBlock *ToBB = Worklist.pop_back_val();
504480093f4SDimitry Andric       if (ToBB == JoinBB)
505480093f4SDimitry Andric         continue;
506480093f4SDimitry Andric 
507480093f4SDimitry Andric       // Make sure all loops in-between are finite.
508480093f4SDimitry Andric       if (!Visited.insert(ToBB).second) {
509480093f4SDimitry Andric         if (!F.hasFnAttribute(Attribute::WillReturn)) {
510480093f4SDimitry Andric           if (!LI)
511480093f4SDimitry Andric             return nullptr;
512480093f4SDimitry Andric 
513480093f4SDimitry Andric           bool MayContainIrreducibleControl = getOrCreateCachedOptional(
514480093f4SDimitry Andric               &F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI);
515480093f4SDimitry Andric           if (MayContainIrreducibleControl)
516480093f4SDimitry Andric             return nullptr;
517480093f4SDimitry Andric 
518480093f4SDimitry Andric           const Loop *L = LI->getLoopFor(ToBB);
519480093f4SDimitry Andric           if (L && maybeEndlessLoop(*L))
520480093f4SDimitry Andric             return nullptr;
521480093f4SDimitry Andric         }
522480093f4SDimitry Andric 
523480093f4SDimitry Andric         continue;
524480093f4SDimitry Andric       }
525480093f4SDimitry Andric 
526480093f4SDimitry Andric       // Make sure the block has no instructions that could stop control
527480093f4SDimitry Andric       // transfer.
528480093f4SDimitry Andric       bool TransfersExecution = getOrCreateCachedOptional(
529480093f4SDimitry Andric           ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB);
530480093f4SDimitry Andric       if (!TransfersExecution)
531480093f4SDimitry Andric         return nullptr;
532480093f4SDimitry Andric 
533e8d8bef9SDimitry Andric       append_range(Worklist, successors(ToBB));
534480093f4SDimitry Andric     }
535480093f4SDimitry Andric   }
536480093f4SDimitry Andric 
537480093f4SDimitry Andric   LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n");
538480093f4SDimitry Andric   return JoinBB;
539480093f4SDimitry Andric }
5405ffd83dbSDimitry Andric const BasicBlock *
5415ffd83dbSDimitry Andric MustBeExecutedContextExplorer::findBackwardJoinPoint(const BasicBlock *InitBB) {
5425ffd83dbSDimitry Andric   const LoopInfo *LI = LIGetter(*InitBB->getParent());
5435ffd83dbSDimitry Andric   const DominatorTree *DT = DTGetter(*InitBB->getParent());
5445ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "\tFind backward join point for " << InitBB->getName()
5455ffd83dbSDimitry Andric                     << (LI ? " [LI]" : "") << (DT ? " [DT]" : ""));
5465ffd83dbSDimitry Andric 
5475ffd83dbSDimitry Andric   // Try to determine a join block through the help of the dominance tree. If no
5485ffd83dbSDimitry Andric   // tree was provided, we perform simple pattern matching for one block
5495ffd83dbSDimitry Andric   // conditionals only.
5505ffd83dbSDimitry Andric   if (DT)
5515ffd83dbSDimitry Andric     if (const auto *InitNode = DT->getNode(InitBB))
5525ffd83dbSDimitry Andric       if (const auto *IDomNode = InitNode->getIDom())
5535ffd83dbSDimitry Andric         return IDomNode->getBlock();
5545ffd83dbSDimitry Andric 
5555ffd83dbSDimitry Andric   const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
5565ffd83dbSDimitry Andric   const BasicBlock *HeaderBB = L ? L->getHeader() : nullptr;
5575ffd83dbSDimitry Andric 
5585ffd83dbSDimitry Andric   // Determine the predecessor blocks but ignore backedges.
5595ffd83dbSDimitry Andric   SmallVector<const BasicBlock *, 8> Worklist;
5605ffd83dbSDimitry Andric   for (const BasicBlock *PredBB : predecessors(InitBB)) {
5615ffd83dbSDimitry Andric     bool IsBackedge =
5625ffd83dbSDimitry Andric         (PredBB == InitBB) || (HeaderBB == InitBB && L->contains(PredBB));
5635ffd83dbSDimitry Andric     // Loop backedges are ignored in backwards propagation: control has to come
5645ffd83dbSDimitry Andric     // from somewhere.
5655ffd83dbSDimitry Andric     if (!IsBackedge)
5665ffd83dbSDimitry Andric       Worklist.push_back(PredBB);
5675ffd83dbSDimitry Andric   }
5685ffd83dbSDimitry Andric 
5695ffd83dbSDimitry Andric   // If there are no other predecessor blocks, there is no join point.
5705ffd83dbSDimitry Andric   if (Worklist.empty())
5715ffd83dbSDimitry Andric     return nullptr;
5725ffd83dbSDimitry Andric 
5735ffd83dbSDimitry Andric   // If there is one predecessor block, it is the join point.
5745ffd83dbSDimitry Andric   if (Worklist.size() == 1)
5755ffd83dbSDimitry Andric     return Worklist[0];
5765ffd83dbSDimitry Andric 
5775ffd83dbSDimitry Andric   const BasicBlock *JoinBB = nullptr;
5785ffd83dbSDimitry Andric   if (Worklist.size() == 2) {
5795ffd83dbSDimitry Andric     const BasicBlock *Pred0 = Worklist[0];
5805ffd83dbSDimitry Andric     const BasicBlock *Pred1 = Worklist[1];
5815ffd83dbSDimitry Andric     const BasicBlock *Pred0UniquePred = Pred0->getUniquePredecessor();
5825ffd83dbSDimitry Andric     const BasicBlock *Pred1UniquePred = Pred1->getUniquePredecessor();
5835ffd83dbSDimitry Andric     if (Pred0 == Pred1UniquePred) {
5845ffd83dbSDimitry Andric       // InitBB <-          Pred0 = JoinBB
5855ffd83dbSDimitry Andric       // InitBB <- Pred1 <- Pred0 = JoinBB
5865ffd83dbSDimitry Andric       JoinBB = Pred0;
5875ffd83dbSDimitry Andric     } else if (Pred1 == Pred0UniquePred) {
5885ffd83dbSDimitry Andric       // InitBB <- Pred0 <- Pred1 = JoinBB
5895ffd83dbSDimitry Andric       // InitBB <-          Pred1 = JoinBB
5905ffd83dbSDimitry Andric       JoinBB = Pred1;
5915ffd83dbSDimitry Andric     } else if (Pred0UniquePred == Pred1UniquePred) {
5925ffd83dbSDimitry Andric       // InitBB <- Pred0 <- JoinBB
5935ffd83dbSDimitry Andric       // InitBB <- Pred1 <- JoinBB
5945ffd83dbSDimitry Andric       JoinBB = Pred0UniquePred;
5955ffd83dbSDimitry Andric     }
5965ffd83dbSDimitry Andric   }
5975ffd83dbSDimitry Andric 
5985ffd83dbSDimitry Andric   if (!JoinBB && L)
5995ffd83dbSDimitry Andric     JoinBB = L->getHeader();
6005ffd83dbSDimitry Andric 
6015ffd83dbSDimitry Andric   // In backwards direction there is no need to show termination of previous
6025ffd83dbSDimitry Andric   // instructions. If they do not terminate, the code afterward is dead, making
6035ffd83dbSDimitry Andric   // any information/transformation correct anyway.
6045ffd83dbSDimitry Andric   return JoinBB;
6055ffd83dbSDimitry Andric }
606480093f4SDimitry Andric 
6078bcb0991SDimitry Andric const Instruction *
6088bcb0991SDimitry Andric MustBeExecutedContextExplorer::getMustBeExecutedNextInstruction(
6098bcb0991SDimitry Andric     MustBeExecutedIterator &It, const Instruction *PP) {
6108bcb0991SDimitry Andric   if (!PP)
6118bcb0991SDimitry Andric     return PP;
6128bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n");
6138bcb0991SDimitry Andric 
6148bcb0991SDimitry Andric   // If we explore only inside a given basic block we stop at terminators.
6158bcb0991SDimitry Andric   if (!ExploreInterBlock && PP->isTerminator()) {
6168bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n");
6178bcb0991SDimitry Andric     return nullptr;
6188bcb0991SDimitry Andric   }
6198bcb0991SDimitry Andric 
6208bcb0991SDimitry Andric   // If we do not traverse the call graph we check if we can make progress in
6218bcb0991SDimitry Andric   // the current function. First, check if the instruction is guaranteed to
6228bcb0991SDimitry Andric   // transfer execution to the successor.
6238bcb0991SDimitry Andric   bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP);
6248bcb0991SDimitry Andric   if (!TransfersExecution)
6258bcb0991SDimitry Andric     return nullptr;
6268bcb0991SDimitry Andric 
6278bcb0991SDimitry Andric   // If this is not a terminator we know that there is a single instruction
6288bcb0991SDimitry Andric   // after this one that is executed next if control is transfered. If not,
6298bcb0991SDimitry Andric   // we can try to go back to a call site we entered earlier. If none exists, we
6308bcb0991SDimitry Andric   // do not know any instruction that has to be executd next.
6318bcb0991SDimitry Andric   if (!PP->isTerminator()) {
6328bcb0991SDimitry Andric     const Instruction *NextPP = PP->getNextNode();
6338bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n");
6348bcb0991SDimitry Andric     return NextPP;
6358bcb0991SDimitry Andric   }
6368bcb0991SDimitry Andric 
6378bcb0991SDimitry Andric   // Finally, we have to handle terminators, trivial ones first.
6388bcb0991SDimitry Andric   assert(PP->isTerminator() && "Expected a terminator!");
6398bcb0991SDimitry Andric 
6408bcb0991SDimitry Andric   // A terminator without a successor is not handled yet.
6418bcb0991SDimitry Andric   if (PP->getNumSuccessors() == 0) {
6428bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n");
6438bcb0991SDimitry Andric     return nullptr;
6448bcb0991SDimitry Andric   }
6458bcb0991SDimitry Andric 
6468bcb0991SDimitry Andric   // A terminator with a single successor, we will continue at the beginning of
6478bcb0991SDimitry Andric   // that one.
6488bcb0991SDimitry Andric   if (PP->getNumSuccessors() == 1) {
6498bcb0991SDimitry Andric     LLVM_DEBUG(
6508bcb0991SDimitry Andric         dbgs() << "\tUnconditional terminator, continue with successor\n");
6518bcb0991SDimitry Andric     return &PP->getSuccessor(0)->front();
6528bcb0991SDimitry Andric   }
6538bcb0991SDimitry Andric 
654480093f4SDimitry Andric   // Multiple successors mean we need to find the join point where control flow
655480093f4SDimitry Andric   // converges again. We use the findForwardJoinPoint helper function with
656480093f4SDimitry Andric   // information about the function and helper analyses, if available.
657480093f4SDimitry Andric   if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent()))
658480093f4SDimitry Andric     return &JoinBB->front();
659480093f4SDimitry Andric 
6608bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << "\tNo join point found\n");
6618bcb0991SDimitry Andric   return nullptr;
6628bcb0991SDimitry Andric }
6638bcb0991SDimitry Andric 
6645ffd83dbSDimitry Andric const Instruction *
6655ffd83dbSDimitry Andric MustBeExecutedContextExplorer::getMustBeExecutedPrevInstruction(
6665ffd83dbSDimitry Andric     MustBeExecutedIterator &It, const Instruction *PP) {
6675ffd83dbSDimitry Andric   if (!PP)
6685ffd83dbSDimitry Andric     return PP;
6695ffd83dbSDimitry Andric 
6705ffd83dbSDimitry Andric   bool IsFirst = !(PP->getPrevNode());
6715ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP
6725ffd83dbSDimitry Andric                     << (IsFirst ? " [IsFirst]" : "") << "\n");
6735ffd83dbSDimitry Andric 
6745ffd83dbSDimitry Andric   // If we explore only inside a given basic block we stop at the first
6755ffd83dbSDimitry Andric   // instruction.
6765ffd83dbSDimitry Andric   if (!ExploreInterBlock && IsFirst) {
6775ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "\tReached block front in intra-block mode, done\n");
6785ffd83dbSDimitry Andric     return nullptr;
6795ffd83dbSDimitry Andric   }
6805ffd83dbSDimitry Andric 
6815ffd83dbSDimitry Andric   // The block and function that contains the current position.
6825ffd83dbSDimitry Andric   const BasicBlock *PPBlock = PP->getParent();
6835ffd83dbSDimitry Andric 
6845ffd83dbSDimitry Andric   // If we are inside a block we know what instruction was executed before, the
6855ffd83dbSDimitry Andric   // previous one.
6865ffd83dbSDimitry Andric   if (!IsFirst) {
6875ffd83dbSDimitry Andric     const Instruction *PrevPP = PP->getPrevNode();
6885ffd83dbSDimitry Andric     LLVM_DEBUG(
6895ffd83dbSDimitry Andric         dbgs() << "\tIntermediate instruction, continue with previous\n");
6905ffd83dbSDimitry Andric     // We did not enter a callee so we simply return the previous instruction.
6915ffd83dbSDimitry Andric     return PrevPP;
6925ffd83dbSDimitry Andric   }
6935ffd83dbSDimitry Andric 
6945ffd83dbSDimitry Andric   // Finally, we have to handle the case where the program point is the first in
6955ffd83dbSDimitry Andric   // a block but not in the function. We use the findBackwardJoinPoint helper
6965ffd83dbSDimitry Andric   // function with information about the function and helper analyses, if
6975ffd83dbSDimitry Andric   // available.
6985ffd83dbSDimitry Andric   if (const BasicBlock *JoinBB = findBackwardJoinPoint(PPBlock))
6995ffd83dbSDimitry Andric     return &JoinBB->back();
7005ffd83dbSDimitry Andric 
7015ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "\tNo join point found\n");
7025ffd83dbSDimitry Andric   return nullptr;
7035ffd83dbSDimitry Andric }
7045ffd83dbSDimitry Andric 
7058bcb0991SDimitry Andric MustBeExecutedIterator::MustBeExecutedIterator(
7068bcb0991SDimitry Andric     MustBeExecutedContextExplorer &Explorer, const Instruction *I)
7078bcb0991SDimitry Andric     : Explorer(Explorer), CurInst(I) {
7088bcb0991SDimitry Andric   reset(I);
7098bcb0991SDimitry Andric }
7108bcb0991SDimitry Andric 
7118bcb0991SDimitry Andric void MustBeExecutedIterator::reset(const Instruction *I) {
7128bcb0991SDimitry Andric   Visited.clear();
7135ffd83dbSDimitry Andric   resetInstruction(I);
7145ffd83dbSDimitry Andric }
7155ffd83dbSDimitry Andric 
7165ffd83dbSDimitry Andric void MustBeExecutedIterator::resetInstruction(const Instruction *I) {
7175ffd83dbSDimitry Andric   CurInst = I;
7185ffd83dbSDimitry Andric   Head = Tail = nullptr;
7195ffd83dbSDimitry Andric   Visited.insert({I, ExplorationDirection::FORWARD});
7205ffd83dbSDimitry Andric   Visited.insert({I, ExplorationDirection::BACKWARD});
7215ffd83dbSDimitry Andric   if (Explorer.ExploreCFGForward)
7225ffd83dbSDimitry Andric     Head = I;
7235ffd83dbSDimitry Andric   if (Explorer.ExploreCFGBackward)
7245ffd83dbSDimitry Andric     Tail = I;
7258bcb0991SDimitry Andric }
7268bcb0991SDimitry Andric 
7278bcb0991SDimitry Andric const Instruction *MustBeExecutedIterator::advance() {
7288bcb0991SDimitry Andric   assert(CurInst && "Cannot advance an end iterator!");
7295ffd83dbSDimitry Andric   Head = Explorer.getMustBeExecutedNextInstruction(*this, Head);
7305ffd83dbSDimitry Andric   if (Head && Visited.insert({Head, ExplorationDirection ::FORWARD}).second)
7315ffd83dbSDimitry Andric     return Head;
7325ffd83dbSDimitry Andric   Head = nullptr;
7335ffd83dbSDimitry Andric 
7345ffd83dbSDimitry Andric   Tail = Explorer.getMustBeExecutedPrevInstruction(*this, Tail);
7355ffd83dbSDimitry Andric   if (Tail && Visited.insert({Tail, ExplorationDirection ::BACKWARD}).second)
7365ffd83dbSDimitry Andric     return Tail;
7375ffd83dbSDimitry Andric   Tail = nullptr;
7385ffd83dbSDimitry Andric   return nullptr;
7398bcb0991SDimitry Andric }
740e8d8bef9SDimitry Andric 
741e8d8bef9SDimitry Andric PreservedAnalyses MustExecutePrinterPass::run(Function &F,
742e8d8bef9SDimitry Andric                                               FunctionAnalysisManager &AM) {
743e8d8bef9SDimitry Andric   auto &LI = AM.getResult<LoopAnalysis>(F);
744e8d8bef9SDimitry Andric   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
745e8d8bef9SDimitry Andric 
746e8d8bef9SDimitry Andric   MustExecuteAnnotatedWriter Writer(F, DT, LI);
747e8d8bef9SDimitry Andric   F.print(OS, &Writer);
748e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
749e8d8bef9SDimitry Andric }
750e8d8bef9SDimitry Andric 
751e8d8bef9SDimitry Andric PreservedAnalyses
752e8d8bef9SDimitry Andric MustBeExecutedContextPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
753e8d8bef9SDimitry Andric   FunctionAnalysisManager &FAM =
754e8d8bef9SDimitry Andric       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
755e8d8bef9SDimitry Andric   GetterTy<const LoopInfo> LIGetter = [&](const Function &F) {
756e8d8bef9SDimitry Andric     return &FAM.getResult<LoopAnalysis>(const_cast<Function &>(F));
757e8d8bef9SDimitry Andric   };
758e8d8bef9SDimitry Andric   GetterTy<const DominatorTree> DTGetter = [&](const Function &F) {
759e8d8bef9SDimitry Andric     return &FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(F));
760e8d8bef9SDimitry Andric   };
761e8d8bef9SDimitry Andric   GetterTy<const PostDominatorTree> PDTGetter = [&](const Function &F) {
762e8d8bef9SDimitry Andric     return &FAM.getResult<PostDominatorTreeAnalysis>(const_cast<Function &>(F));
763e8d8bef9SDimitry Andric   };
764e8d8bef9SDimitry Andric 
765e8d8bef9SDimitry Andric   MustBeExecutedContextExplorer Explorer(
766e8d8bef9SDimitry Andric       /* ExploreInterBlock */ true,
767e8d8bef9SDimitry Andric       /* ExploreCFGForward */ true,
768e8d8bef9SDimitry Andric       /* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter);
769e8d8bef9SDimitry Andric 
770e8d8bef9SDimitry Andric   for (Function &F : M) {
771e8d8bef9SDimitry Andric     for (Instruction &I : instructions(F)) {
772e8d8bef9SDimitry Andric       OS << "-- Explore context of: " << I << "\n";
773e8d8bef9SDimitry Andric       for (const Instruction *CI : Explorer.range(&I))
774e8d8bef9SDimitry Andric         OS << "  [F: " << CI->getFunction()->getName() << "] " << *CI << "\n";
775e8d8bef9SDimitry Andric     }
776e8d8bef9SDimitry Andric   }
777e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
778e8d8bef9SDimitry Andric }
779