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