MustExecute.cpp 33 KB

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