Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:11

0001 //===-- Analysis/CFG.h - BasicBlock Analyses --------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This family of functions performs analyses on basic blocks, and instructions
0010 // contained within basic blocks.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_ANALYSIS_CFG_H
0015 #define LLVM_ANALYSIS_CFG_H
0016 
0017 #include "llvm/ADT/GraphTraits.h"
0018 #include "llvm/ADT/SmallPtrSet.h"
0019 #include <utility>
0020 
0021 namespace llvm {
0022 
0023 class BasicBlock;
0024 class DominatorTree;
0025 class Function;
0026 class Instruction;
0027 class LoopInfo;
0028 template <typename T> class SmallVectorImpl;
0029 
0030 /// Analyze the specified function to find all of the loop backedges in the
0031 /// function and return them.  This is a relatively cheap (compared to
0032 /// computing dominators and loop info) analysis.
0033 ///
0034 /// The output is added to Result, as pairs of <from,to> edge info.
0035 void FindFunctionBackedges(
0036     const Function &F,
0037     SmallVectorImpl<std::pair<const BasicBlock *, const BasicBlock *> > &
0038         Result);
0039 
0040 /// Search for the specified successor of basic block BB and return its position
0041 /// in the terminator instruction's list of successors.  It is an error to call
0042 /// this with a block that is not a successor.
0043 unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ);
0044 
0045 /// Return true if the specified edge is a critical edge. Critical edges are
0046 /// edges from a block with multiple successors to a block with multiple
0047 /// predecessors.
0048 ///
0049 bool isCriticalEdge(const Instruction *TI, unsigned SuccNum,
0050                     bool AllowIdenticalEdges = false);
0051 bool isCriticalEdge(const Instruction *TI, const BasicBlock *Succ,
0052                     bool AllowIdenticalEdges = false);
0053 
0054 /// Determine whether instruction 'To' is reachable from 'From', without passing
0055 /// through any blocks in ExclusionSet, returning true if uncertain.
0056 ///
0057 /// Determine whether there is a path from From to To within a single function.
0058 /// Returns false only if we can prove that once 'From' has been executed then
0059 /// 'To' can not be executed. Conservatively returns true.
0060 ///
0061 /// This function is linear with respect to the number of blocks in the CFG,
0062 /// walking down successors from From to reach To, with a fixed threshold.
0063 /// Using DT or LI allows us to answer more quickly. LI reduces the cost of
0064 /// an entire loop of any number of blocks to be the same as the cost of a
0065 /// single block. DT reduces the cost by allowing the search to terminate when
0066 /// we find a block that dominates the block containing 'To'. DT is most useful
0067 /// on branchy code but not loops, and LI is most useful on code with loops but
0068 /// does not help on branchy code outside loops.
0069 bool isPotentiallyReachable(
0070     const Instruction *From, const Instruction *To,
0071     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
0072     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
0073 
0074 /// Determine whether block 'To' is reachable from 'From', returning
0075 /// true if uncertain.
0076 ///
0077 /// Determine whether there is a path from From to To within a single function.
0078 /// Returns false only if we can prove that once 'From' has been reached then
0079 /// 'To' can not be executed. Conservatively returns true.
0080 bool isPotentiallyReachable(
0081     const BasicBlock *From, const BasicBlock *To,
0082     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
0083     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
0084 
0085 /// Determine whether there is at least one path from a block in
0086 /// 'Worklist' to 'StopBB' without passing through any blocks in
0087 /// 'ExclusionSet', returning true if uncertain.
0088 ///
0089 /// Determine whether there is a path from at least one block in Worklist to
0090 /// StopBB within a single function without passing through any of the blocks
0091 /// in 'ExclusionSet'. Returns false only if we can prove that once any block
0092 /// in 'Worklist' has been reached then 'StopBB' can not be executed.
0093 /// Conservatively returns true.
0094 bool isPotentiallyReachableFromMany(
0095     SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
0096     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
0097     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
0098 
0099 /// Determine whether there is a potentially a path from at least one block in
0100 /// 'Worklist' to at least one block in 'StopSet' within a single function
0101 /// without passing through any of the blocks in 'ExclusionSet'. Returns false
0102 /// only if we can prove that once any block in 'Worklist' has been reached then
0103 /// no blocks in 'StopSet' can be executed without passing through any blocks in
0104 /// 'ExclusionSet'. Conservatively returns true.
0105 bool isManyPotentiallyReachableFromMany(
0106     SmallVectorImpl<BasicBlock *> &Worklist,
0107     const SmallPtrSetImpl<const BasicBlock *> &StopSet,
0108     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
0109     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
0110 
0111 /// Return true if the control flow in \p RPOTraversal is irreducible.
0112 ///
0113 /// This is a generic implementation to detect CFG irreducibility based on loop
0114 /// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
0115 /// Function, MachineFunction, etc.) by providing an RPO traversal (\p
0116 /// RPOTraversal) and the loop info analysis (\p LI) of the CFG. This utility
0117 /// function is only recommended when loop info analysis is available. If loop
0118 /// info analysis isn't available, please, don't compute it explicitly for this
0119 /// purpose. There are more efficient ways to detect CFG irreducibility that
0120 /// don't require recomputing loop info analysis (e.g., T1/T2 or Tarjan's
0121 /// algorithm).
0122 ///
0123 /// Requirements:
0124 ///   1) GraphTraits must be implemented for NodeT type. It is used to access
0125 ///      NodeT successors.
0126 //    2) \p RPOTraversal must be a valid reverse post-order traversal of the
0127 ///      target CFG with begin()/end() iterator interfaces.
0128 ///   3) \p LI must be a valid LoopInfoBase that contains up-to-date loop
0129 ///      analysis information of the CFG.
0130 ///
0131 /// This algorithm uses the information about reducible loop back-edges already
0132 /// computed in \p LI. When a back-edge is found during the RPO traversal, the
0133 /// algorithm checks whether the back-edge is one of the reducible back-edges in
0134 /// loop info. If it isn't, the CFG is irreducible. For example, for the CFG
0135 /// below (canonical irreducible graph) loop info won't contain any loop, so the
0136 /// algorithm will return that the CFG is irreducible when checking the B <-
0137 /// -> C back-edge.
0138 ///
0139 /// (A->B, A->C, B->C, C->B, C->D)
0140 ///    A
0141 ///  /   \
0142 /// B<- ->C
0143 ///       |
0144 ///       D
0145 ///
0146 template <class NodeT, class RPOTraversalT, class LoopInfoT,
0147           class GT = GraphTraits<NodeT>>
0148 bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
0149   /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
0150   /// according to LI. I.e., check if there exists a loop that contains Src and
0151   /// where Dst is the loop header.
0152   auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
0153     for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
0154       if (Lp->getHeader() == Dst)
0155         return true;
0156     }
0157     return false;
0158   };
0159 
0160   SmallPtrSet<NodeT, 32> Visited;
0161   for (NodeT Node : RPOTraversal) {
0162     Visited.insert(Node);
0163     for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
0164       // Succ hasn't been visited yet
0165       if (!Visited.count(Succ))
0166         continue;
0167       // We already visited Succ, thus Node->Succ must be a backedge. Check that
0168       // the head matches what we have in the loop information. Otherwise, we
0169       // have an irreducible graph.
0170       if (!isProperBackedge(Node, Succ))
0171         return true;
0172     }
0173   }
0174 
0175   return false;
0176 }
0177 } // End llvm namespace
0178 
0179 #endif