GraphTraits.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //===- llvm/ADT/GraphTraits.h - Graph traits template -----------*- 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 file defines the little GraphTraits<X> template class that should be
  10. // specialized by classes that want to be iteratable 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. #ifndef LLVM_ADT_GRAPHTRAITS_H
  17. #define LLVM_ADT_GRAPHTRAITS_H
  18. #include "llvm/ADT/iterator_range.h"
  19. namespace llvm {
  20. // GraphTraits - This class should be specialized by different graph types...
  21. // which is why the default version is empty.
  22. //
  23. // This template evolved from supporting `BasicBlock` to also later supporting
  24. // more complex types (e.g. CFG and DomTree).
  25. //
  26. // GraphTraits can be used to create a view over a graph interpreting it
  27. // differently without requiring a copy of the original graph. This could
  28. // be achieved by carrying more data in NodeRef. See LoopBodyTraits for one
  29. // example.
  30. template<class GraphType>
  31. struct GraphTraits {
  32. // Elements to provide:
  33. // typedef NodeRef - Type of Node token in the graph, which should
  34. // be cheap to copy.
  35. // typedef ChildIteratorType - Type used to iterate over children in graph,
  36. // dereference to a NodeRef.
  37. // static NodeRef getEntryNode(const GraphType &)
  38. // Return the entry node of the graph
  39. // static ChildIteratorType child_begin(NodeRef)
  40. // static ChildIteratorType child_end (NodeRef)
  41. // Return iterators that point to the beginning and ending of the child
  42. // node list for the specified node.
  43. // typedef ...iterator nodes_iterator; - dereference to a NodeRef
  44. // static nodes_iterator nodes_begin(GraphType *G)
  45. // static nodes_iterator nodes_end (GraphType *G)
  46. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  47. // typedef EdgeRef - Type of Edge token in the graph, which should
  48. // be cheap to copy.
  49. // typedef ChildEdgeIteratorType - Type used to iterate over children edges in
  50. // graph, dereference to a EdgeRef.
  51. // static ChildEdgeIteratorType child_edge_begin(NodeRef)
  52. // static ChildEdgeIteratorType child_edge_end(NodeRef)
  53. // Return iterators that point to the beginning and ending of the
  54. // edge list for the given callgraph node.
  55. //
  56. // static NodeRef edge_dest(EdgeRef)
  57. // Return the destination node of an edge.
  58. // static unsigned size (GraphType *G)
  59. // Return total number of nodes in the graph
  60. // If anyone tries to use this class without having an appropriate
  61. // specialization, make an error. If you get this error, it's because you
  62. // need to include the appropriate specialization of GraphTraits<> for your
  63. // graph, or you need to define it for a new graph type. Either that or
  64. // your argument to XXX_begin(...) is unknown or needs to have the proper .h
  65. // file #include'd.
  66. using NodeRef = typename GraphType::UnknownGraphTypeError;
  67. };
  68. // Inverse - This class is used as a little marker class to tell the graph
  69. // iterator to iterate over the graph in a graph defined "Inverse" ordering.
  70. // Not all graphs define an inverse ordering, and if they do, it depends on
  71. // the graph exactly what that is. Here's an example of usage with the
  72. // df_iterator:
  73. //
  74. // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
  75. // for (; I != E; ++I) { ... }
  76. //
  77. // Which is equivalent to:
  78. // df_iterator<Inverse<Method*>> I = idf_begin(M), E = idf_end(M);
  79. // for (; I != E; ++I) { ... }
  80. //
  81. template <class GraphType>
  82. struct Inverse {
  83. const GraphType &Graph;
  84. inline Inverse(const GraphType &G) : Graph(G) {}
  85. };
  86. // Provide a partial specialization of GraphTraits so that the inverse of an
  87. // inverse falls back to the original graph.
  88. template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {};
  89. // Provide iterator ranges for the graph traits nodes and children
  90. template <class GraphType>
  91. iterator_range<typename GraphTraits<GraphType>::nodes_iterator>
  92. nodes(const GraphType &G) {
  93. return make_range(GraphTraits<GraphType>::nodes_begin(G),
  94. GraphTraits<GraphType>::nodes_end(G));
  95. }
  96. template <class GraphType>
  97. iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator>
  98. inverse_nodes(const GraphType &G) {
  99. return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G),
  100. GraphTraits<Inverse<GraphType>>::nodes_end(G));
  101. }
  102. template <class GraphType>
  103. iterator_range<typename GraphTraits<GraphType>::ChildIteratorType>
  104. children(const typename GraphTraits<GraphType>::NodeRef &G) {
  105. return make_range(GraphTraits<GraphType>::child_begin(G),
  106. GraphTraits<GraphType>::child_end(G));
  107. }
  108. template <class GraphType>
  109. iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType>
  110. inverse_children(const typename GraphTraits<GraphType>::NodeRef &G) {
  111. return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G),
  112. GraphTraits<Inverse<GraphType>>::child_end(G));
  113. }
  114. template <class GraphType>
  115. iterator_range<typename GraphTraits<GraphType>::ChildEdgeIteratorType>
  116. children_edges(const typename GraphTraits<GraphType>::NodeRef &G) {
  117. return make_range(GraphTraits<GraphType>::child_edge_begin(G),
  118. GraphTraits<GraphType>::child_edge_end(G));
  119. }
  120. } // end namespace llvm
  121. #endif // LLVM_ADT_GRAPHTRAITS_H