MustExecute.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MustExecute.h - Is an instruction known to execute--------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// Contains a collection of routines for determining if a given instruction is
  15. /// guaranteed to execute if a given point in control flow is reached. The most
  16. /// common example is an instruction within a loop being provably executed if we
  17. /// branch to the header of it's containing loop.
  18. ///
  19. /// There are two interfaces available to determine if an instruction is
  20. /// executed once a given point in the control flow is reached:
  21. /// 1) A loop-centric one derived from LoopSafetyInfo.
  22. /// 2) A "must be executed context"-based one implemented in the
  23. /// MustBeExecutedContextExplorer.
  24. /// Please refer to the class comments for more information.
  25. ///
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_ANALYSIS_MUSTEXECUTE_H
  28. #define LLVM_ANALYSIS_MUSTEXECUTE_H
  29. #include "llvm/ADT/DenseMap.h"
  30. #include "llvm/ADT/DenseSet.h"
  31. #include "llvm/Analysis/EHPersonalities.h"
  32. #include "llvm/Analysis/InstructionPrecedenceTracking.h"
  33. #include "llvm/IR/PassManager.h"
  34. namespace llvm {
  35. namespace {
  36. template <typename T> using GetterTy = std::function<T *(const Function &F)>;
  37. }
  38. class BasicBlock;
  39. class DominatorTree;
  40. class Instruction;
  41. class Loop;
  42. class LoopInfo;
  43. class PostDominatorTree;
  44. class raw_ostream;
  45. /// Captures loop safety information.
  46. /// It keep information for loop blocks may throw exception or otherwise
  47. /// exit abnormally on any iteration of the loop which might actually execute
  48. /// at runtime. The primary way to consume this information is via
  49. /// isGuaranteedToExecute below, but some callers bailout or fallback to
  50. /// alternate reasoning if a loop contains any implicit control flow.
  51. /// NOTE: LoopSafetyInfo contains cached information regarding loops and their
  52. /// particular blocks. This information is only dropped on invocation of
  53. /// computeLoopSafetyInfo. If the loop or any of its block is deleted, or if
  54. /// any thrower instructions have been added or removed from them, or if the
  55. /// control flow has changed, or in case of other meaningful modifications, the
  56. /// LoopSafetyInfo needs to be recomputed. If a meaningful modifications to the
  57. /// loop were made and the info wasn't recomputed properly, the behavior of all
  58. /// methods except for computeLoopSafetyInfo is undefined.
  59. class LoopSafetyInfo {
  60. // Used to update funclet bundle operands.
  61. DenseMap<BasicBlock *, ColorVector> BlockColors;
  62. protected:
  63. /// Computes block colors.
  64. void computeBlockColors(const Loop *CurLoop);
  65. public:
  66. /// Returns block colors map that is used to update funclet operand bundles.
  67. const DenseMap<BasicBlock *, ColorVector> &getBlockColors() const;
  68. /// Copy colors of block \p Old into the block \p New.
  69. void copyColors(BasicBlock *New, BasicBlock *Old);
  70. /// Returns true iff the block \p BB potentially may throw exception. It can
  71. /// be false-positive in cases when we want to avoid complex analysis.
  72. virtual bool blockMayThrow(const BasicBlock *BB) const = 0;
  73. /// Returns true iff any block of the loop for which this info is contains an
  74. /// instruction that may throw or otherwise exit abnormally.
  75. virtual bool anyBlockMayThrow() const = 0;
  76. /// Return true if we must reach the block \p BB under assumption that the
  77. /// loop \p CurLoop is entered.
  78. bool allLoopPathsLeadToBlock(const Loop *CurLoop, const BasicBlock *BB,
  79. const DominatorTree *DT) const;
  80. /// Computes safety information for a loop checks loop body & header for
  81. /// the possibility of may throw exception, it takes LoopSafetyInfo and loop
  82. /// as argument. Updates safety information in LoopSafetyInfo argument.
  83. /// Note: This is defined to clear and reinitialize an already initialized
  84. /// LoopSafetyInfo. Some callers rely on this fact.
  85. virtual void computeLoopSafetyInfo(const Loop *CurLoop) = 0;
  86. /// Returns true if the instruction in a loop is guaranteed to execute at
  87. /// least once (under the assumption that the loop is entered).
  88. virtual bool isGuaranteedToExecute(const Instruction &Inst,
  89. const DominatorTree *DT,
  90. const Loop *CurLoop) const = 0;
  91. LoopSafetyInfo() = default;
  92. virtual ~LoopSafetyInfo() = default;
  93. };
  94. /// Simple and conservative implementation of LoopSafetyInfo that can give
  95. /// false-positive answers to its queries in order to avoid complicated
  96. /// analysis.
  97. class SimpleLoopSafetyInfo: public LoopSafetyInfo {
  98. bool MayThrow = false; // The current loop contains an instruction which
  99. // may throw.
  100. bool HeaderMayThrow = false; // Same as previous, but specific to loop header
  101. public:
  102. bool blockMayThrow(const BasicBlock *BB) const override;
  103. bool anyBlockMayThrow() const override;
  104. void computeLoopSafetyInfo(const Loop *CurLoop) override;
  105. bool isGuaranteedToExecute(const Instruction &Inst,
  106. const DominatorTree *DT,
  107. const Loop *CurLoop) const override;
  108. };
  109. /// This implementation of LoopSafetyInfo use ImplicitControlFlowTracking to
  110. /// give precise answers on "may throw" queries. This implementation uses cache
  111. /// that should be invalidated by calling the methods insertInstructionTo and
  112. /// removeInstruction whenever we modify a basic block's contents by adding or
  113. /// removing instructions.
  114. class ICFLoopSafetyInfo: public LoopSafetyInfo {
  115. bool MayThrow = false; // The current loop contains an instruction which
  116. // may throw.
  117. // Contains information about implicit control flow in this loop's blocks.
  118. mutable ImplicitControlFlowTracking ICF;
  119. // Contains information about instruction that may possibly write memory.
  120. mutable MemoryWriteTracking MW;
  121. public:
  122. bool blockMayThrow(const BasicBlock *BB) const override;
  123. bool anyBlockMayThrow() const override;
  124. void computeLoopSafetyInfo(const Loop *CurLoop) override;
  125. bool isGuaranteedToExecute(const Instruction &Inst,
  126. const DominatorTree *DT,
  127. const Loop *CurLoop) const override;
  128. /// Returns true if we could not execute a memory-modifying instruction before
  129. /// we enter \p BB under assumption that \p CurLoop is entered.
  130. bool doesNotWriteMemoryBefore(const BasicBlock *BB, const Loop *CurLoop)
  131. const;
  132. /// Returns true if we could not execute a memory-modifying instruction before
  133. /// we execute \p I under assumption that \p CurLoop is entered.
  134. bool doesNotWriteMemoryBefore(const Instruction &I, const Loop *CurLoop)
  135. const;
  136. /// Inform the safety info that we are planning to insert a new instruction
  137. /// \p Inst into the basic block \p BB. It will make all cache updates to keep
  138. /// it correct after this insertion.
  139. void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB);
  140. /// Inform safety info that we are planning to remove the instruction \p Inst
  141. /// from its block. It will make all cache updates to keep it correct after
  142. /// this removal.
  143. void removeInstruction(const Instruction *Inst);
  144. };
  145. bool mayContainIrreducibleControl(const Function &F, const LoopInfo *LI);
  146. struct MustBeExecutedContextExplorer;
  147. /// Enum that allows us to spell out the direction.
  148. enum class ExplorationDirection {
  149. BACKWARD = 0,
  150. FORWARD = 1,
  151. };
  152. /// Must be executed iterators visit stretches of instructions that are
  153. /// guaranteed to be executed together, potentially with other instruction
  154. /// executed in-between.
  155. ///
  156. /// Given the following code, and assuming all statements are single
  157. /// instructions which transfer execution to the successor (see
  158. /// isGuaranteedToTransferExecutionToSuccessor), there are two possible
  159. /// outcomes. If we start the iterator at A, B, or E, we will visit only A, B,
  160. /// and E. If we start at C or D, we will visit all instructions A-E.
  161. ///
  162. /// \code
  163. /// A;
  164. /// B;
  165. /// if (...) {
  166. /// C;
  167. /// D;
  168. /// }
  169. /// E;
  170. /// \endcode
  171. ///
  172. ///
  173. /// Below is the example extneded with instructions F and G. Now we assume F
  174. /// might not transfer execution to it's successor G. As a result we get the
  175. /// following visit sets:
  176. ///
  177. /// Start Instruction | Visit Set
  178. /// A | A, B, E, F
  179. /// B | A, B, E, F
  180. /// C | A, B, C, D, E, F
  181. /// D | A, B, C, D, E, F
  182. /// E | A, B, E, F
  183. /// F | A, B, E, F
  184. /// G | A, B, E, F, G
  185. ///
  186. ///
  187. /// \code
  188. /// A;
  189. /// B;
  190. /// if (...) {
  191. /// C;
  192. /// D;
  193. /// }
  194. /// E;
  195. /// F; // Might not transfer execution to its successor G.
  196. /// G;
  197. /// \endcode
  198. ///
  199. ///
  200. /// A more complex example involving conditionals, loops, break, and continue
  201. /// is shown below. We again assume all instructions will transmit control to
  202. /// the successor and we assume we can prove the inner loop to be finite. We
  203. /// omit non-trivial branch conditions as the exploration is oblivious to them.
  204. /// Constant branches are assumed to be unconditional in the CFG. The resulting
  205. /// visist sets are shown in the table below.
  206. ///
  207. /// \code
  208. /// A;
  209. /// while (true) {
  210. /// B;
  211. /// if (...)
  212. /// C;
  213. /// if (...)
  214. /// continue;
  215. /// D;
  216. /// if (...)
  217. /// break;
  218. /// do {
  219. /// if (...)
  220. /// continue;
  221. /// E;
  222. /// } while (...);
  223. /// F;
  224. /// }
  225. /// G;
  226. /// \endcode
  227. ///
  228. /// Start Instruction | Visit Set
  229. /// A | A, B
  230. /// B | A, B
  231. /// C | A, B, C
  232. /// D | A, B, D
  233. /// E | A, B, D, E, F
  234. /// F | A, B, D, F
  235. /// G | A, B, D, G
  236. ///
  237. ///
  238. /// Note that the examples show optimal visist sets but not necessarily the ones
  239. /// derived by the explorer depending on the available CFG analyses (see
  240. /// MustBeExecutedContextExplorer). Also note that we, depending on the options,
  241. /// the visit set can contain instructions from other functions.
  242. struct MustBeExecutedIterator {
  243. /// Type declarations that make his class an input iterator.
  244. ///{
  245. typedef const Instruction *value_type;
  246. typedef std::ptrdiff_t difference_type;
  247. typedef const Instruction **pointer;
  248. typedef const Instruction *&reference;
  249. typedef std::input_iterator_tag iterator_category;
  250. ///}
  251. using ExplorerTy = MustBeExecutedContextExplorer;
  252. MustBeExecutedIterator(const MustBeExecutedIterator &Other) = default;
  253. MustBeExecutedIterator(MustBeExecutedIterator &&Other)
  254. : Visited(std::move(Other.Visited)), Explorer(Other.Explorer),
  255. CurInst(Other.CurInst), Head(Other.Head), Tail(Other.Tail) {}
  256. MustBeExecutedIterator &operator=(MustBeExecutedIterator &&Other) {
  257. if (this != &Other) {
  258. std::swap(Visited, Other.Visited);
  259. std::swap(CurInst, Other.CurInst);
  260. std::swap(Head, Other.Head);
  261. std::swap(Tail, Other.Tail);
  262. }
  263. return *this;
  264. }
  265. ~MustBeExecutedIterator() = default;
  266. /// Pre- and post-increment operators.
  267. ///{
  268. MustBeExecutedIterator &operator++() {
  269. CurInst = advance();
  270. return *this;
  271. }
  272. MustBeExecutedIterator operator++(int) {
  273. MustBeExecutedIterator tmp(*this);
  274. operator++();
  275. return tmp;
  276. }
  277. ///}
  278. /// Equality and inequality operators. Note that we ignore the history here.
  279. ///{
  280. bool operator==(const MustBeExecutedIterator &Other) const {
  281. return CurInst == Other.CurInst && Head == Other.Head && Tail == Other.Tail;
  282. }
  283. bool operator!=(const MustBeExecutedIterator &Other) const {
  284. return !(*this == Other);
  285. }
  286. ///}
  287. /// Return the underlying instruction.
  288. const Instruction *&operator*() { return CurInst; }
  289. const Instruction *getCurrentInst() const { return CurInst; }
  290. /// Return true if \p I was encountered by this iterator already.
  291. bool count(const Instruction *I) const {
  292. return Visited.count({I, ExplorationDirection::FORWARD}) ||
  293. Visited.count({I, ExplorationDirection::BACKWARD});
  294. }
  295. private:
  296. using VisitedSetTy =
  297. DenseSet<PointerIntPair<const Instruction *, 1, ExplorationDirection>>;
  298. /// Private constructors.
  299. MustBeExecutedIterator(ExplorerTy &Explorer, const Instruction *I);
  300. /// Reset the iterator to its initial state pointing at \p I.
  301. void reset(const Instruction *I);
  302. /// Reset the iterator to point at \p I, keep cached state.
  303. void resetInstruction(const Instruction *I);
  304. /// Try to advance one of the underlying positions (Head or Tail).
  305. ///
  306. /// \return The next instruction in the must be executed context, or nullptr
  307. /// if none was found.
  308. const Instruction *advance();
  309. /// A set to track the visited instructions in order to deal with endless
  310. /// loops and recursion.
  311. VisitedSetTy Visited;
  312. /// A reference to the explorer that created this iterator.
  313. ExplorerTy &Explorer;
  314. /// The instruction we are currently exposing to the user. There is always an
  315. /// instruction that we know is executed with the given program point,
  316. /// initially the program point itself.
  317. const Instruction *CurInst;
  318. /// Two positions that mark the program points where this iterator will look
  319. /// for the next instruction. Note that the current instruction is either the
  320. /// one pointed to by Head, Tail, or both.
  321. const Instruction *Head, *Tail;
  322. friend struct MustBeExecutedContextExplorer;
  323. };
  324. /// A "must be executed context" for a given program point PP is the set of
  325. /// instructions, potentially before and after PP, that are executed always when
  326. /// PP is reached. The MustBeExecutedContextExplorer an interface to explore
  327. /// "must be executed contexts" in a module through the use of
  328. /// MustBeExecutedIterator.
  329. ///
  330. /// The explorer exposes "must be executed iterators" that traverse the must be
  331. /// executed context. There is little information sharing between iterators as
  332. /// the expected use case involves few iterators for "far apart" instructions.
  333. /// If that changes, we should consider caching more intermediate results.
  334. struct MustBeExecutedContextExplorer {
  335. /// In the description of the parameters we use PP to denote a program point
  336. /// for which the must be executed context is explored, or put differently,
  337. /// for which the MustBeExecutedIterator is created.
  338. ///
  339. /// \param ExploreInterBlock Flag to indicate if instructions in blocks
  340. /// other than the parent of PP should be
  341. /// explored.
  342. /// \param ExploreCFGForward Flag to indicate if instructions located after
  343. /// PP in the CFG, e.g., post-dominating PP,
  344. /// should be explored.
  345. /// \param ExploreCFGBackward Flag to indicate if instructions located
  346. /// before PP in the CFG, e.g., dominating PP,
  347. /// should be explored.
  348. MustBeExecutedContextExplorer(
  349. bool ExploreInterBlock, bool ExploreCFGForward, bool ExploreCFGBackward,
  350. GetterTy<const LoopInfo> LIGetter =
  351. [](const Function &) { return nullptr; },
  352. GetterTy<const DominatorTree> DTGetter =
  353. [](const Function &) { return nullptr; },
  354. GetterTy<const PostDominatorTree> PDTGetter =
  355. [](const Function &) { return nullptr; })
  356. : ExploreInterBlock(ExploreInterBlock),
  357. ExploreCFGForward(ExploreCFGForward),
  358. ExploreCFGBackward(ExploreCFGBackward), LIGetter(LIGetter),
  359. DTGetter(DTGetter), PDTGetter(PDTGetter), EndIterator(*this, nullptr) {}
  360. /// Iterator-based interface. \see MustBeExecutedIterator.
  361. ///{
  362. using iterator = MustBeExecutedIterator;
  363. using const_iterator = const MustBeExecutedIterator;
  364. /// Return an iterator to explore the context around \p PP.
  365. iterator &begin(const Instruction *PP) {
  366. auto &It = InstructionIteratorMap[PP];
  367. if (!It)
  368. It.reset(new iterator(*this, PP));
  369. return *It;
  370. }
  371. /// Return an iterator to explore the cached context around \p PP.
  372. const_iterator &begin(const Instruction *PP) const {
  373. return *InstructionIteratorMap.find(PP)->second;
  374. }
  375. /// Return an universal end iterator.
  376. ///{
  377. iterator &end() { return EndIterator; }
  378. iterator &end(const Instruction *) { return EndIterator; }
  379. const_iterator &end() const { return EndIterator; }
  380. const_iterator &end(const Instruction *) const { return EndIterator; }
  381. ///}
  382. /// Return an iterator range to explore the context around \p PP.
  383. llvm::iterator_range<iterator> range(const Instruction *PP) {
  384. return llvm::make_range(begin(PP), end(PP));
  385. }
  386. /// Return an iterator range to explore the cached context around \p PP.
  387. llvm::iterator_range<const_iterator> range(const Instruction *PP) const {
  388. return llvm::make_range(begin(PP), end(PP));
  389. }
  390. ///}
  391. /// Check \p Pred on all instructions in the context.
  392. ///
  393. /// This method will evaluate \p Pred and return
  394. /// true if \p Pred holds in every instruction.
  395. bool checkForAllContext(const Instruction *PP,
  396. function_ref<bool(const Instruction *)> Pred) {
  397. for (auto EIt = begin(PP), EEnd = end(PP); EIt != EEnd; ++EIt)
  398. if (!Pred(*EIt))
  399. return false;
  400. return true;
  401. }
  402. /// Helper to look for \p I in the context of \p PP.
  403. ///
  404. /// The context is expanded until \p I was found or no more expansion is
  405. /// possible.
  406. ///
  407. /// \returns True, iff \p I was found.
  408. bool findInContextOf(const Instruction *I, const Instruction *PP) {
  409. auto EIt = begin(PP), EEnd = end(PP);
  410. return findInContextOf(I, EIt, EEnd);
  411. }
  412. /// Helper to look for \p I in the context defined by \p EIt and \p EEnd.
  413. ///
  414. /// The context is expanded until \p I was found or no more expansion is
  415. /// possible.
  416. ///
  417. /// \returns True, iff \p I was found.
  418. bool findInContextOf(const Instruction *I, iterator &EIt, iterator &EEnd) {
  419. bool Found = EIt.count(I);
  420. while (!Found && EIt != EEnd)
  421. Found = (++EIt).getCurrentInst() == I;
  422. return Found;
  423. }
  424. /// Return the next instruction that is guaranteed to be executed after \p PP.
  425. ///
  426. /// \param It The iterator that is used to traverse the must be
  427. /// executed context.
  428. /// \param PP The program point for which the next instruction
  429. /// that is guaranteed to execute is determined.
  430. const Instruction *
  431. getMustBeExecutedNextInstruction(MustBeExecutedIterator &It,
  432. const Instruction *PP);
  433. /// Return the previous instr. that is guaranteed to be executed before \p PP.
  434. ///
  435. /// \param It The iterator that is used to traverse the must be
  436. /// executed context.
  437. /// \param PP The program point for which the previous instr.
  438. /// that is guaranteed to execute is determined.
  439. const Instruction *
  440. getMustBeExecutedPrevInstruction(MustBeExecutedIterator &It,
  441. const Instruction *PP);
  442. /// Find the next join point from \p InitBB in forward direction.
  443. const BasicBlock *findForwardJoinPoint(const BasicBlock *InitBB);
  444. /// Find the next join point from \p InitBB in backward direction.
  445. const BasicBlock *findBackwardJoinPoint(const BasicBlock *InitBB);
  446. /// Parameter that limit the performed exploration. See the constructor for
  447. /// their meaning.
  448. ///{
  449. const bool ExploreInterBlock;
  450. const bool ExploreCFGForward;
  451. const bool ExploreCFGBackward;
  452. ///}
  453. private:
  454. /// Getters for common CFG analyses: LoopInfo, DominatorTree, and
  455. /// PostDominatorTree.
  456. ///{
  457. GetterTy<const LoopInfo> LIGetter;
  458. GetterTy<const DominatorTree> DTGetter;
  459. GetterTy<const PostDominatorTree> PDTGetter;
  460. ///}
  461. /// Map to cache isGuaranteedToTransferExecutionToSuccessor results.
  462. DenseMap<const BasicBlock *, std::optional<bool>> BlockTransferMap;
  463. /// Map to cache containsIrreducibleCFG results.
  464. DenseMap<const Function *, std::optional<bool>> IrreducibleControlMap;
  465. /// Map from instructions to associated must be executed iterators.
  466. DenseMap<const Instruction *, std::unique_ptr<MustBeExecutedIterator>>
  467. InstructionIteratorMap;
  468. /// A unique end iterator.
  469. MustBeExecutedIterator EndIterator;
  470. };
  471. class MustExecutePrinterPass : public PassInfoMixin<MustExecutePrinterPass> {
  472. raw_ostream &OS;
  473. public:
  474. MustExecutePrinterPass(raw_ostream &OS) : OS(OS) {}
  475. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  476. };
  477. class MustBeExecutedContextPrinterPass
  478. : public PassInfoMixin<MustBeExecutedContextPrinterPass> {
  479. raw_ostream &OS;
  480. public:
  481. MustBeExecutedContextPrinterPass(raw_ostream &OS) : OS(OS) {}
  482. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  483. };
  484. } // namespace llvm
  485. #endif
  486. #ifdef __GNUC__
  487. #pragma GCC diagnostic pop
  488. #endif