Coverage Report

Created: 2019-02-15 18: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
// 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
6
//
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
20
namespace llvm {
21
22
class BasicBlock;
23
class DominatorTree;
24
class Function;
25
class Instruction;
26
class LoopInfo;
27
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.
33
void 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.
41
unsigned 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
///
47
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum,
48
                    bool AllowIdenticalEdges = false);
49
50
/// Determine whether instruction 'To' is reachable from 'From',
51
/// returning true if uncertain.
52
///
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.
65
bool isPotentiallyReachable(const Instruction *From, const Instruction *To,
66
                            const DominatorTree *DT = nullptr,
67
                            const LoopInfo *LI = nullptr);
68
69
/// Determine whether block 'To' is reachable from 'From', returning
70
/// true if uncertain.
71
///
72
/// Determine whether there is a path from From to To within a single function.
73
/// Returns false only if we can prove that once 'From' has been reached then
74
/// 'To' can not be executed. Conservatively returns true.
75
bool isPotentiallyReachable(const BasicBlock *From, const BasicBlock *To,
76
                            const DominatorTree *DT = nullptr,
77
                            const LoopInfo *LI = nullptr);
78
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
91
/// Return true if the control flow in \p RPOTraversal is irreducible.
92
///
93
/// This is a generic implementation to detect CFG irreducibility based on loop
94
/// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
95
/// Function, MachineFunction, etc.) by providing an RPO traversal (\p
96
/// RPOTraversal) and the loop info analysis (\p LI) of the CFG. This utility
97
/// function is only recommended when loop info analysis is available. If loop
98
/// info analysis isn't available, please, don't compute it explicitly for this
99
/// purpose. There are more efficient ways to detect CFG irreducibility that
100
/// don't require recomputing loop info analysis (e.g., T1/T2 or Tarjan's
101
/// algorithm).
102
///
103
/// Requirements:
104
///   1) GraphTraits must be implemented for NodeT type. It is used to access
105
///      NodeT successors.
106
//    2) \p RPOTraversal must be a valid reverse post-order traversal of the
107
///      target CFG with begin()/end() iterator interfaces.
108
///   3) \p LI must be a valid LoopInfoBase that contains up-to-date loop
109
///      analysis information of the CFG.
110
///
111
/// This algorithm uses the information about reducible loop back-edges already
112
/// computed in \p LI. When a back-edge is found during the RPO traversal, the
113
/// algorithm checks whether the back-edge is one of the reducible back-edges in
114
/// loop info. If it isn't, the CFG is irreducible. For example, for the CFG
115
/// below (canonical irreducible graph) loop info won't contain any loop, so the
116
/// algorithm will return that the CFG is irreducible when checking the B <-
117
/// -> C back-edge.
118
///
119
/// (A->B, A->C, B->C, C->B, C->D)
120
///    A
121
///  /   \
122
/// B<- ->C
123
///       |
124
///       D
125
///
126
template <class NodeT, class RPOTraversalT, class LoopInfoT,
127
          class GT = GraphTraits<NodeT>>
128
502k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
129
502k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
130
502k
  /// according to LI. I.e., check if there exists a loop that contains Src and
131
502k
  /// where Dst is the loop header.
132
502k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
360k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()95
) {
134
360k
      if (Lp->getHeader() == Dst)
135
360k
        return true;
136
360k
    }
137
360k
    
return false60
;
138
360k
  };
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
132
149k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
149k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()9
) {
134
149k
      if (Lp->getHeader() == Dst)
135
149k
        return true;
136
149k
    }
137
149k
    
return false5
;
138
149k
  };
bool llvm::containsIrreducibleCFG<llvm::BasicBlock const*, llvm::ReversePostOrderTraversal<llvm::Function const*, llvm::GraphTraits<llvm::Function const*> > const, llvm::LoopInfo const, llvm::GraphTraits<llvm::BasicBlock const*> >(llvm::ReversePostOrderTraversal<llvm::Function const*, llvm::GraphTraits<llvm::Function const*> > const&, llvm::LoopInfo const const&)::'lambda'(llvm::BasicBlock const*, llvm::BasicBlock const*)::operator()(llvm::BasicBlock const*, llvm::BasicBlock const*) const
Line
Count
Source
132
24
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
24
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()0
) {
134
21
      if (Lp->getHeader() == Dst)
135
21
        return true;
136
21
    }
137
24
    
return false3
;
138
24
  };
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
132
211k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
211k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; 
Lp = Lp->getParentLoop()86
) {
134
211k
      if (Lp->getHeader() == Dst)
135
211k
        return true;
136
211k
    }
137
211k
    
return false52
;
138
211k
  };
139
502k
140
502k
  SmallPtrSet<NodeT, 32> Visited;
141
3.09M
  for (NodeT Node : RPOTraversal) {
142
3.09M
    Visited.insert(Node);
143
4.16M
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
144
4.16M
      // Succ hasn't been visited yet
145
4.16M
      if (!Visited.count(Succ))
146
3.80M
        continue;
147
360k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
148
360k
      // the head matches what we have in the loop information. Otherwise, we
149
360k
      // have an irreducible graph.
150
360k
      if (!isProperBackedge(Node, Succ))
151
60
        return true;
152
360k
    }
153
3.09M
  }
154
502k
155
502k
  
return false502k
;
156
502k
}
bool llvm::containsIrreducibleCFG<llvm::BasicBlock const*, llvm::LoopBlocksRPO, llvm::LoopInfo, llvm::GraphTraits<llvm::BasicBlock const*> >(llvm::LoopBlocksRPO&, llvm::LoopInfo const&)
Line
Count
Source
128
147k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
129
147k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
130
147k
  /// according to LI. I.e., check if there exists a loop that contains Src and
131
147k
  /// where Dst is the loop header.
132
147k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
147k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
134
147k
      if (Lp->getHeader() == Dst)
135
147k
        return true;
136
147k
    }
137
147k
    return false;
138
147k
  };
139
147k
140
147k
  SmallPtrSet<NodeT, 32> Visited;
141
379k
  for (NodeT Node : RPOTraversal) {
142
379k
    Visited.insert(Node);
143
675k
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
144
675k
      // Succ hasn't been visited yet
145
675k
      if (!Visited.count(Succ))
146
525k
        continue;
147
149k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
148
149k
      // the head matches what we have in the loop information. Otherwise, we
149
149k
      // have an irreducible graph.
150
149k
      if (!isProperBackedge(Node, Succ))
151
5
        return true;
152
149k
    }
153
379k
  }
154
147k
155
147k
  
return false147k
;
156
147k
}
bool llvm::containsIrreducibleCFG<llvm::BasicBlock const*, llvm::ReversePostOrderTraversal<llvm::Function const*, llvm::GraphTraits<llvm::Function const*> > const, llvm::LoopInfo const, llvm::GraphTraits<llvm::BasicBlock const*> >(llvm::ReversePostOrderTraversal<llvm::Function const*, llvm::GraphTraits<llvm::Function const*> > const&, llvm::LoopInfo const const&)
Line
Count
Source
128
69
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
129
69
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
130
69
  /// according to LI. I.e., check if there exists a loop that contains Src and
131
69
  /// where Dst is the loop header.
132
69
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
69
    for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
134
69
      if (Lp->getHeader() == Dst)
135
69
        return true;
136
69
    }
137
69
    return false;
138
69
  };
139
69
140
69
  SmallPtrSet<NodeT, 32> Visited;
141
167
  for (NodeT Node : RPOTraversal) {
142
167
    Visited.insert(Node);
143
167
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
144
155
      // Succ hasn't been visited yet
145
155
      if (!Visited.count(Succ))
146
131
        continue;
147
24
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
148
24
      // the head matches what we have in the loop information. Otherwise, we
149
24
      // have an irreducible graph.
150
24
      if (!isProperBackedge(Node, Succ))
151
3
        return true;
152
24
    }
153
167
  }
154
69
155
69
  
return false66
;
156
69
}
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
128
354k
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
129
354k
  /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
130
354k
  /// according to LI. I.e., check if there exists a loop that contains Src and
131
354k
  /// where Dst is the loop header.
132
354k
  auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
133
354k
    for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
134
354k
      if (Lp->getHeader() == Dst)
135
354k
        return true;
136
354k
    }
137
354k
    return false;
138
354k
  };
139
354k
140
354k
  SmallPtrSet<NodeT, 32> Visited;
141
2.71M
  for (NodeT Node : RPOTraversal) {
142
2.71M
    Visited.insert(Node);
143
3.49M
    for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
144
3.49M
      // Succ hasn't been visited yet
145
3.49M
      if (!Visited.count(Succ))
146
3.28M
        continue;
147
211k
      // We already visited Succ, thus Node->Succ must be a backedge. Check that
148
211k
      // the head matches what we have in the loop information. Otherwise, we
149
211k
      // have an irreducible graph.
150
211k
      if (!isProperBackedge(Node, Succ))
151
52
        return true;
152
211k
    }
153
2.71M
  }
154
354k
155
354k
  
return false354k
;
156
354k
}
157
} // End llvm namespace
158
159
#endif