//===-- Graph.h - XRay Graph Class ------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // A Graph Datatype for XRay. // //===----------------------------------------------------------------------===// #ifndef LLVM_XRAY_GRAPH_T_H #define LLVM_XRAY_GRAPH_T_H #include #include #include #include #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/iterator.h" #include "llvm/Support/Error.h" namespace llvm { namespace xray { /// A Graph object represents a Directed Graph and is used in XRay to compute /// and store function call graphs and associated statistical information. /// /// The graph takes in four template parameters, these are: /// - VertexAttribute, this is a structure which is stored for each vertex. /// Must be DefaultConstructible, CopyConstructible, CopyAssignable and /// Destructible. /// - EdgeAttribute, this is a structure which is stored for each edge /// Must be DefaultConstructible, CopyConstructible, CopyAssignable and /// Destructible. /// - EdgeAttribute, this is a structure which is stored for each variable /// - VI, this is a type over which DenseMapInfo is defined and is the type /// used look up strings, available as VertexIdentifier. /// - If the built in DenseMapInfo is not defined, provide a specialization /// class type here. /// /// Graph is CopyConstructible, CopyAssignable, MoveConstructible and /// MoveAssignable but is not EqualityComparible or LessThanComparible. /// /// Usage Example Graph with weighted edges and vertices: /// Graph G; /// /// G[1] = 0; /// G[2] = 2; /// G[{1,2}] = 1; /// G[{2,1}] = -1; /// for(const auto &v : G.vertices()){ /// // Do something with the vertices in the graph; /// } /// for(const auto &e : G.edges()){ /// // Do something with the edges in the graph; /// } /// /// Usage Example with StrRef keys. /// Graph StrG; /// char va[] = "Vertex A"; /// char vaa[] = "Vertex A"; /// char vb[] = "Vertex B"; // Vertices are referenced by String Refs. /// G[va] = 0; /// G[vb] = 1; /// G[{va, vb}] = 1.0; /// cout() << G[vaa] << " " << G[{vaa, vb}]; //prints "0 1.0". /// template class Graph { public: /// These objects are used to name edges and vertices in the graph. typedef VI VertexIdentifier; typedef std::pair EdgeIdentifier; /// This type is the value_type of all iterators which range over vertices, /// Determined by the Vertices DenseMap using VertexValueType = detail::DenseMapPair; /// This type is the value_type of all iterators which range over edges, /// Determined by the Edges DenseMap. using EdgeValueType = detail::DenseMapPair; using size_type = std::size_t; private: /// The type used for storing the EdgeAttribute for each edge in the graph using EdgeMapT = DenseMap; /// The type used for storing the VertexAttribute for each vertex in /// the graph. using VertexMapT = DenseMap; /// The type used for storing the edges entering a vertex. Indexed by /// the VertexIdentifier of the start of the edge. Only used to determine /// where the incoming edges are, the EdgeIdentifiers are stored in an /// InnerEdgeMapT. using NeighborSetT = DenseSet; /// The type storing the InnerInvGraphT corresponding to each vertex in /// the graph (When a vertex has an incoming edge incident to it) using NeighborLookupT = DenseMap; private: /// Stores the map from the start and end vertex of an edge to it's /// EdgeAttribute EdgeMapT Edges; /// Stores the map from VertexIdentifier to VertexAttribute VertexMapT Vertices; /// Allows fast lookup for the incoming edge set of any given vertex. NeighborLookupT InNeighbors; /// Allows fast lookup for the outgoing edge set of any given vertex. NeighborLookupT OutNeighbors; /// An Iterator adapter using an InnerInvGraphT::iterator as a base iterator, /// and storing the VertexIdentifier the iterator range comes from. The /// dereference operator is then performed using a pointer to the graph's edge /// set. template > class NeighborEdgeIteratorT : public iterator_adaptor_base< NeighborEdgeIteratorT, BaseIt, typename std::iterator_traits::iterator_category, T> { using InternalEdgeMapT = std::conditional_t; friend class NeighborEdgeIteratorT; friend class NeighborEdgeIteratorT; InternalEdgeMapT *MP; VertexIdentifier SI; public: template > operator NeighborEdgeIteratorT() const { return NeighborEdgeIteratorT(this->I, MP, SI); } NeighborEdgeIteratorT() = default; NeighborEdgeIteratorT(BaseIt _I, InternalEdgeMapT *_MP, VertexIdentifier _SI) : iterator_adaptor_base< NeighborEdgeIteratorT, BaseIt, typename std::iterator_traits::iterator_category, T>(_I), MP(_MP), SI(_SI) {} T &operator*() const { if (!IsOut) return *(MP->find({*(this->I), SI})); else return *(MP->find({SI, *(this->I)})); } }; public: /// A const iterator type for iterating through the set of edges entering a /// vertex. /// /// Has a const EdgeValueType as its value_type using ConstInEdgeIterator = NeighborEdgeIteratorT; /// An iterator type for iterating through the set of edges leaving a vertex. /// /// Has an EdgeValueType as its value_type using InEdgeIterator = NeighborEdgeIteratorT; /// A const iterator type for iterating through the set of edges entering a /// vertex. /// /// Has a const EdgeValueType as its value_type using ConstOutEdgeIterator = NeighborEdgeIteratorT; /// An iterator type for iterating through the set of edges leaving a vertex. /// /// Has an EdgeValueType as its value_type using OutEdgeIterator = NeighborEdgeIteratorT; /// A class for ranging over the incoming edges incident to a vertex. /// /// Like all views in this class it provides methods to get the beginning and /// past the range iterators for the range, as well as methods to determine /// the number of elements in the range and whether the range is empty. template class InOutEdgeView { public: using iterator = NeighborEdgeIteratorT; using const_iterator = NeighborEdgeIteratorT; using GraphT = std::conditional_t; using InternalEdgeMapT = std::conditional_t; private: InternalEdgeMapT &M; const VertexIdentifier A; const NeighborLookupT &NL; public: iterator begin() { auto It = NL.find(A); if (It == NL.end()) return iterator(); return iterator(It->second.begin(), &M, A); } const_iterator cbegin() const { auto It = NL.find(A); if (It == NL.end()) return const_iterator(); return const_iterator(It->second.begin(), &M, A); } const_iterator begin() const { return cbegin(); } iterator end() { auto It = NL.find(A); if (It == NL.end()) return iterator(); return iterator(It->second.end(), &M, A); } const_iterator cend() const { auto It = NL.find(A); if (It == NL.end()) return const_iterator(); return const_iterator(It->second.end(), &M, A); } const_iterator end() const { return cend(); } size_type size() const { auto I = NL.find(A); if (I == NL.end()) return 0; else return I->second.size(); } bool empty() const { return NL.count(A) == 0; }; InOutEdgeView(GraphT &G, VertexIdentifier A) : M(G.Edges), A(A), NL(isOut ? G.OutNeighbors : G.InNeighbors) {} }; /// A const iterator type for iterating through the whole vertex set of the /// graph. /// /// Has a const VertexValueType as its value_type using ConstVertexIterator = typename VertexMapT::const_iterator; /// An iterator type for iterating through the whole vertex set of the graph. /// /// Has a VertexValueType as its value_type using VertexIterator = typename VertexMapT::iterator; /// A class for ranging over the vertices in the graph. /// /// Like all views in this class it provides methods to get the beginning and /// past the range iterators for the range, as well as methods to determine /// the number of elements in the range and whether the range is empty. template class VertexView { public: using iterator = std::conditional_t; using const_iterator = ConstVertexIterator; using GraphT = std::conditional_t; private: GraphT &G; public: iterator begin() { return G.Vertices.begin(); } iterator end() { return G.Vertices.end(); } const_iterator cbegin() const { return G.Vertices.cbegin(); } const_iterator cend() const { return G.Vertices.cend(); } const_iterator begin() const { return G.Vertices.begin(); } const_iterator end() const { return G.Vertices.end(); } size_type size() const { return G.Vertices.size(); } bool empty() const { return G.Vertices.empty(); } VertexView(GraphT &_G) : G(_G) {} }; /// A const iterator for iterating through the entire edge set of the graph. /// /// Has a const EdgeValueType as its value_type using ConstEdgeIterator = typename EdgeMapT::const_iterator; /// An iterator for iterating through the entire edge set of the graph. /// /// Has an EdgeValueType as its value_type using EdgeIterator = typename EdgeMapT::iterator; /// A class for ranging over all the edges in the graph. /// /// Like all views in this class it provides methods to get the beginning and /// past the range iterators for the range, as well as methods to determine /// the number of elements in the range and whether the range is empty. template class EdgeView { public: using iterator = std::conditional_t; using const_iterator = ConstEdgeIterator; using GraphT = std::conditional_t; private: GraphT &G; public: iterator begin() { return G.Edges.begin(); } iterator end() { return G.Edges.end(); } const_iterator cbegin() const { return G.Edges.cbegin(); } const_iterator cend() const { return G.Edges.cend(); } const_iterator begin() const { return G.Edges.begin(); } const_iterator end() const { return G.Edges.end(); } size_type size() const { return G.Edges.size(); } bool empty() const { return G.Edges.empty(); } EdgeView(GraphT &_G) : G(_G) {} }; public: // TODO: implement constructor to enable Graph Initialisation.\ // Something like: // Graph G( // {1, 2, 3, 4, 5}, // {{1, 2}, {2, 3}, {3, 4}}); /// Empty the Graph void clear() { Edges.clear(); Vertices.clear(); InNeighbors.clear(); OutNeighbors.clear(); } /// Returns a view object allowing iteration over the vertices of the graph. /// also allows access to the size of the vertex set. VertexView vertices() { return VertexView(*this); } VertexView vertices() const { return VertexView(*this); } /// Returns a view object allowing iteration over the edges of the graph. /// also allows access to the size of the edge set. EdgeView edges() { return EdgeView(*this); } EdgeView edges() const { return EdgeView(*this); } /// Returns a view object allowing iteration over the edges which start at /// a vertex I. InOutEdgeView outEdges(const VertexIdentifier I) { return InOutEdgeView(*this, I); } InOutEdgeView outEdges(const VertexIdentifier I) const { return InOutEdgeView(*this, I); } /// Returns a view object allowing iteration over the edges which point to /// a vertex I. InOutEdgeView inEdges(const VertexIdentifier I) { return InOutEdgeView(*this, I); } InOutEdgeView inEdges(const VertexIdentifier I) const { return InOutEdgeView(*this, I); } /// Looks up the vertex with identifier I, if it does not exist it default /// constructs it. VertexAttribute &operator[](const VertexIdentifier &I) { return Vertices.FindAndConstruct(I).second; } /// 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. EdgeAttribute &operator[](const EdgeIdentifier &I) { auto &P = Edges.FindAndConstruct(I); Vertices.FindAndConstruct(I.first); Vertices.FindAndConstruct(I.second); InNeighbors[I.second].insert(I.first); OutNeighbors[I.first].insert(I.second); return P.second; } /// Looks up a vertex with Identifier I, or an error if it does not exist. Expected at(const VertexIdentifier &I) { auto It = Vertices.find(I); if (It == Vertices.end()) return make_error( "Vertex Identifier Does Not Exist", std::make_error_code(std::errc::invalid_argument)); return It->second; } Expected at(const VertexIdentifier &I) const { auto It = Vertices.find(I); if (It == Vertices.end()) return make_error( "Vertex Identifier Does Not Exist", std::make_error_code(std::errc::invalid_argument)); return It->second; } /// Looks up an edge with Identifier I, or an error if it does not exist. Expected at(const EdgeIdentifier &I) { auto It = Edges.find(I); if (It == Edges.end()) return make_error( "Edge Identifier Does Not Exist", std::make_error_code(std::errc::invalid_argument)); return It->second; } Expected at(const EdgeIdentifier &I) const { auto It = Edges.find(I); if (It == Edges.end()) return make_error( "Edge Identifier Does Not Exist", std::make_error_code(std::errc::invalid_argument)); return It->second; } /// Looks for a vertex with identifier I, returns 1 if one exists, and /// 0 otherwise size_type count(const VertexIdentifier &I) const { return Vertices.count(I); } /// Looks for an edge with Identifier I, returns 1 if one exists and 0 /// otherwise size_type count(const EdgeIdentifier &I) const { return Edges.count(I); } /// Inserts a vertex into the graph with Identifier Val.first, and /// Attribute Val.second. std::pair insert(const std::pair &Val) { return Vertices.insert(Val); } std::pair insert(std::pair &&Val) { return Vertices.insert(std::move(Val)); } /// Inserts an edge into the graph with Identifier Val.first, and /// Attribute Val.second. If the key is already in the map, it returns false /// and doesn't update the value. std::pair insert(const std::pair &Val) { const auto &p = Edges.insert(Val); if (p.second) { const auto &EI = Val.first; Vertices.FindAndConstruct(EI.first); Vertices.FindAndConstruct(EI.second); InNeighbors[EI.second].insert(EI.first); OutNeighbors[EI.first].insert(EI.second); }; return p; } /// Inserts an edge into the graph with Identifier Val.first, and /// Attribute Val.second. If the key is already in the map, it returns false /// and doesn't update the value. std::pair insert(std::pair &&Val) { auto EI = Val.first; const auto &p = Edges.insert(std::move(Val)); if (p.second) { Vertices.FindAndConstruct(EI.first); Vertices.FindAndConstruct(EI.second); InNeighbors[EI.second].insert(EI.first); OutNeighbors[EI.first].insert(EI.second); }; return p; } }; } } #endif