Static Value-Flow Analysis
GraphTraits.h
Go to the documentation of this file.
1 //===- llvm/ADT/GenericGraphTraits.h - Graph traits template -----------*- C++ -*-===//
2 //
3 // From the LLVM Project with some modifications, under the Apache License v2.0
4 // with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the little GenericGraphTraits<X> template class that should be
10 // specialized by classes that want to be iterable by generic graph iterators.
11 //
12 // This file also defines the marker class Inverse that is used to iterate over
13 // graphs in a graph defined, inverse ordering...
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef GRAPHS_GRAPHTRAITS_H
18 #define GRAPHS_GRAPHTRAITS_H
19 
20 #include "Util/iterator_range.h"
21 
22 namespace SVF
23 {
24 
25 // GenericGraphTraits - This class should be specialized by different graph types...
26 // which is why the default version is empty.
27 //
28 // This template evolved from supporting `BasicBlock` to also later supporting
29 // more complex types (e.g. CFG and DomTree).
30 //
31 // GenericGraphTraits can be used to create a view over a graph interpreting it
32 // differently without requiring a copy of the original graph. This could
33 // be achieved by carrying more data in NodeRef. See LoopBodyTraits for one
34 // example.
35 template<class GraphType>
37 {
38  // Elements to provide:
39 
40  // typedef NodeRef - Type of Node token in the graph, which should
41  // be cheap to copy.
42  // typedef ChildIteratorType - Type used to iterate over children in graph,
43  // dereference to a NodeRef.
44 
45  // static NodeRef getEntryNode(const GraphType &)
46  // Return the entry node of the graph
47 
48  // static ChildIteratorType child_begin(NodeRef)
49  // static ChildIteratorType child_end (NodeRef)
50  // Return iterators that point to the beginning and ending of the child
51  // node list for the specified node.
52 
53  // typedef ...iterator nodes_iterator; - dereference to a NodeRef
54  // static nodes_iterator nodes_begin(GraphType *G)
55  // static nodes_iterator nodes_end (GraphType *G)
56  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
57 
58  // typedef EdgeRef - Type of Edge token in the graph, which should
59  // be cheap to copy.
60  // typedef ChildEdgeIteratorType - Type used to iterate over children edges in
61  // graph, dereference to a EdgeRef.
62 
63  // static ChildEdgeIteratorType child_edge_begin(NodeRef)
64  // static ChildEdgeIteratorType child_edge_end(NodeRef)
65  // Return iterators that point to the beginning and ending of the
66  // edge list for the given callgraph node.
67  //
68  // static NodeRef edge_dest(EdgeRef)
69  // Return the destination node of an edge.
70 
71  // static unsigned size (GraphType *G)
72  // Return total number of nodes in the graph
73 
74  // If anyone tries to use this class without having an appropriate
75  // specialization, make an error. If you get this error, it's because you
76  // need to include the appropriate specialization of GenericGraphTraits<> for your
77  // graph, or you need to define it for a new graph type. Either that or
78  // your argument to XXX_begin(...) is unknown or needs to have the proper .h
79  // file #include'd.
80  using NodeRef = typename GraphType::UnknownGraphTypeError;
81 };
82 
83 // Inverse - This class is used as a little marker class to tell the graph
84 // iterator to iterate over the graph in a graph defined "Inverse" ordering.
85 // Not all graphs define an inverse ordering, and if they do, it depends on
86 // the graph exactly what that is. Here's an example of usage with the
87 // df_iterator:
88 //
89 // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
90 // for (; I != E; ++I) { ... }
91 //
92 // Which is equivalent to:
93 // df_iterator<Inverse<Method*>> I = idf_begin(M), E = idf_end(M);
94 // for (; I != E; ++I) { ... }
95 //
96 template <class GraphType>
97 struct Inverse
98 {
99  const GraphType &Graph;
100 
101  inline Inverse(const GraphType &G) : Graph(G) {}
102 };
103 
104 // Provide a partial specialization of GenericGraphTraits so that the inverse of an
105 // inverse falls back to the original graph.
106 template <class T> struct GenericGraphTraits<Inverse<Inverse<T>>> : GenericGraphTraits<T> {};
107 
108 // Provide iterator ranges for the graph traits nodes and children
109 template <class GraphType>
111 nodes(const GraphType &G)
112 {
115 }
116 template <class GraphType>
117 iter_range<typename GenericGraphTraits<Inverse<GraphType>>::nodes_iterator>
118  inverse_nodes(const GraphType &G)
119 {
120  return make_range(GenericGraphTraits<Inverse<GraphType>>::nodes_begin(G),
121  GenericGraphTraits<Inverse<GraphType>>::nodes_end(G));
122 }
123 
124 template <class GraphType>
125 iter_range<typename GenericGraphTraits<GraphType>::ChildIteratorType>
127 {
130 }
131 
132 template <class GraphType>
133 iter_range<typename GenericGraphTraits<Inverse<GraphType>>::ChildIteratorType>
135 {
136  return make_range(GenericGraphTraits<Inverse<GraphType>>::child_begin(G),
137  GenericGraphTraits<Inverse<GraphType>>::child_end(G));
138 }
139 
140 template <class GraphType>
141 iter_range<typename GenericGraphTraits<GraphType>::ChildEdgeIteratorType>
143 {
146 }
147 
148 } // end namespace llvm
149 
150 #endif // LLVM_ADT_GRAPHTRAITS_H
for isBitcode
Definition: BasicTypes.h:68
iter_range< typename GenericGraphTraits< Inverse< GraphType > >::ChildIteratorType > inverse_children(const typename GenericGraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:134
iter_range< T > make_range(T x, T y)
iter_range< typename GenericGraphTraits< Inverse< GraphType > >::nodes_iterator > inverse_nodes(const GraphType &G)
Definition: GraphTraits.h:118
iter_range< typename GenericGraphTraits< GraphType >::ChildIteratorType > children(const typename GenericGraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:126
iter_range< typename GenericGraphTraits< GraphType >::ChildEdgeIteratorType > children_edges(const typename GenericGraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:142
iter_range< typename GenericGraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
Definition: GraphTraits.h:111
typename GraphType::UnknownGraphTypeError NodeRef
Definition: GraphTraits.h:80
const GraphType & Graph
Definition: GraphTraits.h:99
Inverse(const GraphType &G)
Definition: GraphTraits.h:101