Coverage Report

Created: 2018-07-19 03:59

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