CFG.h 7.9 KB

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