CFG.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===-- Analysis/CFG.h - BasicBlock Analyses --------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This family of functions performs analyses on basic blocks, and instructions
  10. // contained within basic blocks.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_CFG_H
  14. #define LLVM_ANALYSIS_CFG_H
  15. #include "llvm/ADT/GraphTraits.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include <utility>
  18. namespace llvm {
  19. class BasicBlock;
  20. class DominatorTree;
  21. class Function;
  22. class Instruction;
  23. class LoopInfo;
  24. template <typename T> class SmallVectorImpl;
  25. /// Analyze the specified function to find all of the loop backedges in the
  26. /// function and return them. This is a relatively cheap (compared to
  27. /// computing dominators and loop info) analysis.
  28. ///
  29. /// The output is added to Result, as pairs of <from,to> edge info.
  30. void FindFunctionBackedges(
  31. const Function &F,
  32. SmallVectorImpl<std::pair<const BasicBlock *, const BasicBlock *> > &
  33. Result);
  34. /// Search for the specified successor of basic block BB and return its position
  35. /// in the terminator instruction's list of successors. It is an error to call
  36. /// this with a block that is not a successor.
  37. unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ);
  38. /// Return true if the specified edge is a critical edge. Critical edges are
  39. /// edges from a block with multiple successors to a block with multiple
  40. /// predecessors.
  41. ///
  42. bool isCriticalEdge(const Instruction *TI, unsigned SuccNum,
  43. bool AllowIdenticalEdges = false);
  44. bool isCriticalEdge(const Instruction *TI, const BasicBlock *Succ,
  45. bool AllowIdenticalEdges = false);
  46. /// Determine whether instruction 'To' is reachable from 'From', without passing
  47. /// through any blocks in ExclusionSet, returning true if uncertain.
  48. ///
  49. /// Determine whether there is a path from From to To within a single function.
  50. /// Returns false only if we can prove that once 'From' has been executed then
  51. /// 'To' can not be executed. Conservatively returns true.
  52. ///
  53. /// This function is linear with respect to the number of blocks in the CFG,
  54. /// walking down successors from From to reach To, with a fixed threshold.
  55. /// Using DT or LI allows us to answer more quickly. LI reduces the cost of
  56. /// an entire loop of any number of blocks to be the same as the cost of a
  57. /// single block. DT reduces the cost by allowing the search to terminate when
  58. /// we find a block that dominates the block containing 'To'. DT is most useful
  59. /// on branchy code but not loops, and LI is most useful on code with loops but
  60. /// does not help on branchy code outside loops.
  61. bool isPotentiallyReachable(
  62. const Instruction *From, const Instruction *To,
  63. const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
  64. const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
  65. /// Determine whether block 'To' is reachable from 'From', returning
  66. /// true if uncertain.
  67. ///
  68. /// Determine whether there is a path from From to To within a single function.
  69. /// Returns false only if we can prove that once 'From' has been reached then
  70. /// 'To' can not be executed. Conservatively returns true.
  71. bool isPotentiallyReachable(
  72. const BasicBlock *From, const BasicBlock *To,
  73. const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
  74. const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
  75. /// Determine whether there is at least one path from a block in
  76. /// 'Worklist' to 'StopBB', returning true if uncertain.
  77. ///
  78. /// Determine whether there is a path from at least one block in Worklist to
  79. /// StopBB within a single function. Returns false only if we can prove that
  80. /// once any block in 'Worklist' has been reached then 'StopBB' can not be
  81. /// executed. Conservatively returns true.
  82. bool isPotentiallyReachableFromMany(SmallVectorImpl<BasicBlock *> &Worklist,
  83. BasicBlock *StopBB,
  84. const DominatorTree *DT = nullptr,
  85. const LoopInfo *LI = nullptr);
  86. /// Determine whether there is at least one path from a block in
  87. /// 'Worklist' to 'StopBB' without passing through any blocks in
  88. /// 'ExclusionSet', returning true if uncertain.
  89. ///
  90. /// Determine whether there is a path from at least one block in Worklist to
  91. /// StopBB within a single function without passing through any of the blocks
  92. /// in 'ExclusionSet'. Returns false only if we can prove that once any block
  93. /// in 'Worklist' has been reached then 'StopBB' can not be executed.
  94. /// Conservatively returns true.
  95. bool isPotentiallyReachableFromMany(
  96. SmallVectorImpl<BasicBlock *> &Worklist, BasicBlock *StopBB,
  97. const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
  98. const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
  99. /// Return true if the control flow in \p RPOTraversal is irreducible.
  100. ///
  101. /// This is a generic implementation to detect CFG irreducibility based on loop
  102. /// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
  103. /// Function, MachineFunction, etc.) by providing an RPO traversal (\p
  104. /// RPOTraversal) and the loop info analysis (\p LI) of the CFG. This utility
  105. /// function is only recommended when loop info analysis is available. If loop
  106. /// info analysis isn't available, please, don't compute it explicitly for this
  107. /// purpose. There are more efficient ways to detect CFG irreducibility that
  108. /// don't require recomputing loop info analysis (e.g., T1/T2 or Tarjan's
  109. /// algorithm).
  110. ///
  111. /// Requirements:
  112. /// 1) GraphTraits must be implemented for NodeT type. It is used to access
  113. /// NodeT successors.
  114. // 2) \p RPOTraversal must be a valid reverse post-order traversal of the
  115. /// target CFG with begin()/end() iterator interfaces.
  116. /// 3) \p LI must be a valid LoopInfoBase that contains up-to-date loop
  117. /// analysis information of the CFG.
  118. ///
  119. /// This algorithm uses the information about reducible loop back-edges already
  120. /// computed in \p LI. When a back-edge is found during the RPO traversal, the
  121. /// algorithm checks whether the back-edge is one of the reducible back-edges in
  122. /// loop info. If it isn't, the CFG is irreducible. For example, for the CFG
  123. /// below (canonical irreducible graph) loop info won't contain any loop, so the
  124. /// algorithm will return that the CFG is irreducible when checking the B <-
  125. /// -> C back-edge.
  126. ///
  127. /// (A->B, A->C, B->C, C->B, C->D)
  128. /// A
  129. /// / \
  130. /// B<- ->C
  131. /// |
  132. /// D
  133. ///
  134. template <class NodeT, class RPOTraversalT, class LoopInfoT,
  135. class GT = GraphTraits<NodeT>>
  136. bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
  137. /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
  138. /// according to LI. I.e., check if there exists a loop that contains Src and
  139. /// where Dst is the loop header.
  140. auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
  141. for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
  142. if (Lp->getHeader() == Dst)
  143. return true;
  144. }
  145. return false;
  146. };
  147. SmallPtrSet<NodeT, 32> Visited;
  148. for (NodeT Node : RPOTraversal) {
  149. Visited.insert(Node);
  150. for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
  151. // Succ hasn't been visited yet
  152. if (!Visited.count(Succ))
  153. continue;
  154. // We already visited Succ, thus Node->Succ must be a backedge. Check that
  155. // the head matches what we have in the loop information. Otherwise, we
  156. // have an irreducible graph.
  157. if (!isProperBackedge(Node, Succ))
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. } // End llvm namespace
  164. #endif