Coverage Report

Created: 2018-11-16 02:38

/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
29
/// Analyze the specified function to find all of the loop backedges in the
30
/// function and return them.  This is a relatively cheap (compared to
31
/// computing dominators and loop info) analysis.
32
///
33
/// The output is added to Result, as pairs of <from,to> edge info.
34
void FindFunctionBackedges(
35
    const Function &F,
36
    SmallVectorImpl<std::pair<const BasicBlock *, const BasicBlock *> > &
37
        Result);
38
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
44
/// Return true if the specified edge is a critical edge. Critical edges are
45
/// edges from a block with multiple successors to a block with multiple
46
/// predecessors.
47
///
48
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum,
49
                    bool AllowIdenticalEdges = false);
50
51
/// Determine whether instruction 'To' is reachable from 'From',
52
/// 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(const Instruction *From, const Instruction *To,
67
                            const DominatorTree *DT = nullptr,
68
                            const LoopInfo *LI = nullptr);
69
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
80
/// Determine whether there is at least one path from a block in
81
/// '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.
87
bool isPotentiallyReachableFromMany(SmallVectorImpl<BasicBlock *> &Worklist,
88
                                    BasicBlock *StopBB,
89
                                    const DominatorTree *DT = nullptr,
90
                                    const LoopInfo *LI = nullptr);
91
92
/// Return true if the control flow in \p RPOTraversal is irreducible.
93
///
94
/// This is a generic implementation to detect CFG irreducibility based on loop
95
/// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
96
/// Function, MachineFunction, etc.) by providing an RPO traversal (\p
97
/// RPOTraversal) and the loop info analysis (\p LI) of the CFG. This utility
98
/// function is only recommended when loop info analysis is available. If loop
99
/// info analysis isn't available, please, don't compute it explicitly for this
100
/// purpose. There are more efficient ways to detect CFG irreducibility that
101
/// don't require recomputing loop info analysis (e.g., T1/T2 or Tarjan's
102
/// algorithm).
103
///
104
/// Requirements:
105
///   1) GraphTraits must be implemented for NodeT type. It is used to access
106
///      NodeT successors.
107
//    2) \p RPOTraversal must be a valid reverse post-order traversal of the
108
///      target CFG with begin()/end() iterator interfaces.
109
///   3) \p LI must be a valid LoopInfoBase that contains up-to-date loop
110
///      analysis information of the CFG.
111
///
112
/// This algorithm uses the information about reducible loop back-edges already
113
/// computed in \p LI. When a back-edge is found during the RPO traversal, the
114
/// algorithm checks whether the back-edge is one of the reducible back-edges in
115
/// loop info. If it isn't, the CFG is irreducible. For example, for the CFG
116
/// below (canonical irreducible graph) loop info won't contain any loop, so the
117
/// algorithm will return that the CFG is irreducible when checking the B <-
118
/// -> C back-edge.
119
///
120
/// (A->B, A->C, B->C, C->B, C->D)
121
///    A
122
///  /   \
123
/// B<- ->C
124
///       |
125
///       D
126
///
127
template <class NodeT, class RPOTraversalT, class LoopInfoT,
128
          class GT = GraphTraits<NodeT>>
129
497k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
130
497k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
131
497k
  /// according to LI. I.e., check if there exists a loop that contains Src and
132
497k
  /// where Dst is the loop header.
133
497k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
134
359k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()89
) {
135
359k
      if (Lp->getHeader() == Dst)
136
359k
        return true;
137
359k
    }
138
359k
    
return false48
;
139
359k
  };
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
133
147k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
134
147k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()8
) {
135
147k
      if (Lp->getHeader() == Dst)
136
147k
        return true;
137
147k
    }
138
147k
    
return false4
;
139
147k
  };
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
133
212k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
134
212k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()81
) {
135
212k
      if (Lp->getHeader() == Dst)
136
212k
        return true;
137
212k
    }
138
212k
    
return false44
;
139
212k
  };
140
497k
141
497k
  SmallPtrSet<NodeT, 32> Visited;
142
3.09M
  for (NodeT Node : RPOTraversal) {
143
3.09M
    Visited.insert(Node);
144
4.17M
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
145
4.17M
      // Succ hasn't been visited yet
146
4.17M
      if (!Visited.count(Succ))
147
3.81M
        continue;
148
359k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
149
359k
      // the head matches what we have in the loop information. Otherwise, we
150
359k
      // have an irreducible graph.
151
359k
      if (!isProperBackedge(Node, Succ))
152
48
        return true;
153
359k
    }
154
3.09M
  }
155
497k
156
497k
  
return false497k
;
157
497k
}
bool llvm::containsIrreducibleCFG<llvm::BasicBlock const*, llvm::LoopBlocksRPO, llvm::LoopInfo, llvm::GraphTraits<llvm::BasicBlock const*> >(llvm::LoopBlocksRPO&, llvm::LoopInfo const&)
Line
Count
Source
129
146k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
130
146k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
131
146k
  /// according to LI. I.e., check if there exists a loop that contains Src and
132
146k
  /// where Dst is the loop header.
133
146k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
134
146k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
135
146k
      if (Lp->getHeader() == Dst)
136
146k
        return true;
137
146k
    }
138
146k
    return false;
139
146k
  };
140
146k
141
146k
  SmallPtrSet<NodeT, 32> Visited;
142
352k
  for (NodeT Node : RPOTraversal) {
143
352k
    Visited.insert(Node);
144
643k
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
145
643k
      // Succ hasn't been visited yet
146
643k
      if (!Visited.count(Succ))
147
496k
        continue;
148
147k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
149
147k
      // the head matches what we have in the loop information. Otherwise, we
150
147k
      // have an irreducible graph.
151
147k
      if (!isProperBackedge(Node, Succ))
152
4
        return true;
153
147k
    }
154
352k
  }
155
146k
156
146k
  
return false146k
;
157
146k
}
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
129
350k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
130
350k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
131
350k
  /// according to LI. I.e., check if there exists a loop that contains Src and
132
350k
  /// where Dst is the loop header.
133
350k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
134
350k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
135
350k
      if (Lp->getHeader() == Dst)
136
350k
        return true;
137
350k
    }
138
350k
    return false;
139
350k
  };
140
350k
141
350k
  SmallPtrSet<NodeT, 32> Visited;
142
2.73M
  for (NodeT Node : RPOTraversal) {
143
2.73M
    Visited.insert(Node);
144
3.52M
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
145
3.52M
      // Succ hasn't been visited yet
146
3.52M
      if (!Visited.count(Succ))
147
3.31M
        continue;
148
212k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
149
212k
      // the head matches what we have in the loop information. Otherwise, we
150
212k
      // have an irreducible graph.
151
212k
      if (!isProperBackedge(Node, Succ))
152
44
        return true;
153
212k
    }
154
2.73M
  }
155
350k
156
350k
  
return false350k
;
157
350k
}
158
} // End llvm namespace
159
160
#endif