14 #ifndef LLVM_XRAY_GRAPH_T_H 15 #define LLVM_XRAY_GRAPH_T_H 17 #include <initializer_list> 19 #include <type_traits> 73 template <
typename VertexAttribute,
typename EdgeAttribute,
74 typename VI = int32_t>
128 template <
bool IsConst,
bool IsOut,
132 class NeighborEdgeIteratorT
134 NeighborEdgeIteratorT<IsConst, IsOut>, BaseIt,
135 typename std::iterator_traits<BaseIt>::iterator_category, T> {
136 using InternalEdgeMapT =
137 typename std::conditional<IsConst, const EdgeMapT, EdgeMapT>::type;
139 friend class NeighborEdgeIteratorT<false, IsOut, BaseIt, EdgeValueType>;
140 friend class NeighborEdgeIteratorT<true, IsOut, BaseIt,
141 const EdgeValueType>;
143 InternalEdgeMapT *MP;
147 template <
bool IsConstDest,
148 typename =
typename std::enable_if<IsConstDest && !IsConst>::type>
149 operator NeighborEdgeIteratorT<IsConstDest, IsOut, BaseIt,
150 const EdgeValueType>()
const {
151 return NeighborEdgeIteratorT<IsConstDest, IsOut, BaseIt,
152 const EdgeValueType>(this->
I, MP,
SI);
155 NeighborEdgeIteratorT() =
default;
156 NeighborEdgeIteratorT(BaseIt _I, InternalEdgeMapT *_MP,
157 VertexIdentifier _SI)
159 NeighborEdgeIteratorT<IsConst, IsOut>, BaseIt,
160 typename std::iterator_traits<BaseIt>::iterator_category, T>(_I),
165 return *(MP->find({*(this->
I), SI}));
167 return *(MP->find({
SI, *(this->
I)}));
201 using iterator = NeighborEdgeIteratorT<isConst, isOut>;
203 using GraphT =
typename std::conditional<isConst, const Graph, Graph>::type;
205 typename std::conditional<isConst, const EdgeMapT, EdgeMapT>::type;
209 const VertexIdentifier A;
214 auto It = NL.
find(A);
217 return iterator(It->second.begin(), &M, A);
221 auto It = NL.
find(A);
230 auto It = NL.
find(A);
233 return iterator(It->second.end(), &M, A);
236 auto It = NL.
find(A);
249 return I->second.size();
255 : M(G.Edges), A(A), NL(isOut ? G.OutNeighbors : G.InNeighbors) {}
279 using GraphT =
typename std::conditional<isConst, const Graph, Graph>::type;
292 bool empty()
const {
return G.Vertices.empty(); }
316 using GraphT =
typename std::conditional<isConst, const Graph, Graph>::type;
329 bool empty()
const {
return G.Edges.empty(); }
345 OutNeighbors.
clear();
393 InNeighbors[I.second].
insert(I.first);
394 OutNeighbors[I.first].
insert(I.second);
400 auto It = Vertices.
find(I);
401 if (It == Vertices.
end())
402 return make_error<StringError>(
403 "Vertex Identifier Does Not Exist",
409 auto It = Vertices.
find(I);
410 if (It == Vertices.
end())
411 return make_error<StringError>(
412 "Vertex Identifier Does Not Exist",
419 auto It = Edges.
find(I);
420 if (It == Edges.
end())
421 return make_error<StringError>(
422 "Edge Identifier Does Not Exist",
428 auto It = Edges.
find(I);
429 if (It == Edges.
end())
430 return make_error<StringError>(
431 "Edge Identifier Does Not Exist",
439 return Vertices.
count(I);
448 std::pair<VertexIterator, bool>
449 insert(
const std::pair<VertexIdentifier, VertexAttribute> &Val) {
450 return Vertices.
insert(Val);
453 std::pair<VertexIterator, bool>
454 insert(std::pair<VertexIdentifier, VertexAttribute> &&Val) {
455 return Vertices.
insert(std::move(Val));
461 std::pair<EdgeIterator, bool>
462 insert(
const std::pair<EdgeIdentifier, EdgeAttribute> &Val) {
463 const auto &p = Edges.
insert(Val);
465 const auto &EI = Val.first;
468 InNeighbors[EI.second].
insert(EI.first);
469 OutNeighbors[EI.first].
insert(EI.second);
478 std::pair<EdgeIterator, bool>
479 insert(std::pair<EdgeIdentifier, EdgeAttribute> &&Val) {
481 const auto &p = Edges.
insert(std::move(Val));
485 InNeighbors[EI.second].
insert(EI.first);
486 OutNeighbors[EI.first].
insert(EI.second);
typename EdgeMapT::iterator EdgeIterator
An iterator for iterating through the entire edge set of the graph.
const_iterator cbegin() const
This class represents lattice values for constants.
Implements a dense probed hash-table based set.
InOutEdgeView< true, false > inEdges(const VertexIdentifier I) const
VI VertexIdentifier
These objects are used to name edges and vertices in the graph.
void clear()
Empty the Graph.
Expected< VertexAttribute & > at(const VertexIdentifier &I)
Looks up a vertex with Identifier I, or an error if it does not exist.
const_iterator cend() const
typename VertexMapT::iterator VertexIterator
An iterator type for iterating through the whole vertex set of the graph.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
DenseMapIterator< VertexIdentifier, VertexAttribute, DenseMapInfo< VertexIdentifier >, llvm::detail::DenseMapPair< VertexIdentifier, VertexAttribute >, true > const_iterator
NeighborEdgeIteratorT< false, true > OutEdgeIterator
An iterator type for iterating through the set of edges leaving a vertex.
std::pair< EdgeIterator, bool > insert(const std::pair< EdgeIdentifier, EdgeAttribute > &Val)
Inserts an edge into the graph with Identifier Val.first, and Attribute Val.second.
const_iterator begin() const
std::error_code make_error_code(BitcodeError E)
Tagged union holding either a T or a Error.
APInt operator*(APInt a, uint64_t RHS)
typename std::conditional< isConst, const Graph, Graph >::type GraphT
ConstIterator const_iterator
NeighborEdgeIteratorT< true, false > ConstInEdgeIterator
A const iterator type for iterating through the set of edges entering a vertex.
const_iterator cbegin() const
const_iterator begin() const
value_type & FindAndConstruct(const KeyT &Key)
iterator find(const_arg_type_t< KeyT > Val)
std::pair< VI, VI > EdgeIdentifier
const_iterator end() const
const_iterator cend() const
CRTP base class for adapting an iterator to a different type.
A Graph object represents a Directed Graph and is used in XRay to compute and store function call gra...
const_iterator end() const
NeighborEdgeIteratorT< true, true > ConstOutEdgeIterator
A const iterator type for iterating through the set of edges entering a vertex.
typename VertexMapT::const_iterator ConstVertexIterator
A const iterator type for iterating through the whole vertex set of the graph.
A class for ranging over the incoming edges incident to a vertex.
EdgeView< true > edges() const
VertexAttribute & operator[](const VertexIdentifier &I)
Looks up the vertex with identifier I, if it does not exist it default constructs it...
typename std::conditional< isConst, const EdgeMapT, EdgeMapT >::type InternalEdgeMapT
ConstEdgeIterator const_iterator
A class for ranging over all the edges in the graph.
VertexView< false > vertices()
Returns a view object allowing iteration over the vertices of the graph.
EdgeView< false > edges()
Returns a view object allowing iteration over the edges of the graph.
typename std::conditional< isConst, ConstVertexIterator, VertexIterator >::type iterator
size_type count(const VertexIdentifier &I) const
Looks for a vertex with identifier I, returns 1 if one exists, and 0 otherwise.
const_iterator cend() const
NeighborEdgeIteratorT< true, isOut > const_iterator
Expected< const EdgeAttribute & > at(const EdgeIdentifier &I) const
NeighborEdgeIteratorT< isConst, isOut > iterator
NeighborEdgeIteratorT< false, false > InEdgeIterator
An iterator type for iterating through the set of edges leaving a vertex.
const_iterator cbegin() const
typename EdgeMapT::const_iterator ConstEdgeIterator
A const iterator for iterating through the entire edge set of the graph.
VertexView< true > vertices() const
Expected< const VertexAttribute & > at(const VertexIdentifier &I) const
A class for ranging over the vertices in the graph.
detail::DenseMapPair< EdgeIdentifier, EdgeAttribute > EdgeValueType
This type is the value_type of all iterators which range over edges, Determined by the Edges DenseMap...
DenseMapIterator< VertexIdentifier, VertexAttribute, DenseMapInfo< VertexIdentifier >, llvm::detail::DenseMapPair< VertexIdentifier, VertexAttribute > > iterator
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
typename std::conditional< isConst, const Graph, Graph >::type GraphT
typename std::conditional< isConst, const Graph, Graph >::type GraphT
InOutEdgeView(GraphT &G, VertexIdentifier A)
ConstVertexIterator const_iterator
typename std::conditional< isConst, ConstEdgeIterator, EdgeIterator >::type iterator
EdgeAttribute & operator[](const EdgeIdentifier &I)
Looks up the edge with identifier I, if it does not exist it default constructs it, if it's endpoints do not exist it also default constructs them.
InOutEdgeView< false, false > inEdges(const VertexIdentifier I)
Returns a view object allowing iteration over the edges which point to a vertex I.
const_iterator end() const
std::pair< EdgeIterator, bool > insert(std::pair< EdgeIdentifier, EdgeAttribute > &&Val)
Inserts an edge into the graph with Identifier Val.first, and Attribute Val.second.
InOutEdgeView< false, true > outEdges(const VertexIdentifier I)
Returns a view object allowing iteration over the edges which start at a vertex I.
size_type count(const EdgeIdentifier &I) const
Looks for an edge with Identifier I, returns 1 if one exists and 0 otherwise.
std::pair< VertexIterator, bool > insert(std::pair< VertexIdentifier, VertexAttribute > &&Val)
const_iterator begin() const
std::pair< VertexIterator, bool > insert(const std::pair< VertexIdentifier, VertexAttribute > &Val)
Inserts a vertex into the graph with Identifier Val.first, and Attribute Val.second.
InOutEdgeView< true, true > outEdges(const VertexIdentifier I) const
Expected< EdgeAttribute & > at(const EdgeIdentifier &I)
Looks up an edge with Identifier I, or an error if it does not exist.