GraphTraits.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/GraphTraits.h - Graph traits template -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the little GraphTraits<X> template class that should be
  15. // specialized by classes that want to be iteratable by generic graph iterators.
  16. //
  17. // This file also defines the marker class Inverse that is used to iterate over
  18. // graphs in a graph defined, inverse ordering...
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_ADT_GRAPHTRAITS_H
  22. #define LLVM_ADT_GRAPHTRAITS_H
  23. #include "llvm/ADT/iterator_range.h"
  24. namespace llvm {
  25. // GraphTraits - 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. // GraphTraits 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>
  36. struct GraphTraits {
  37. // Elements to provide:
  38. // typedef NodeRef - Type of Node token in the graph, which should
  39. // be cheap to copy.
  40. // typedef ChildIteratorType - Type used to iterate over children in graph,
  41. // dereference to a NodeRef.
  42. // static NodeRef getEntryNode(const GraphType &)
  43. // Return the entry node of the graph
  44. // static ChildIteratorType child_begin(NodeRef)
  45. // static ChildIteratorType child_end (NodeRef)
  46. // Return iterators that point to the beginning and ending of the child
  47. // node list for the specified node.
  48. // typedef ...iterator nodes_iterator; - dereference to a NodeRef
  49. // static nodes_iterator nodes_begin(GraphType *G)
  50. // static nodes_iterator nodes_end (GraphType *G)
  51. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  52. // typedef EdgeRef - Type of Edge token in the graph, which should
  53. // be cheap to copy.
  54. // typedef ChildEdgeIteratorType - Type used to iterate over children edges in
  55. // graph, dereference to a EdgeRef.
  56. // static ChildEdgeIteratorType child_edge_begin(NodeRef)
  57. // static ChildEdgeIteratorType child_edge_end(NodeRef)
  58. // Return iterators that point to the beginning and ending of the
  59. // edge list for the given callgraph node.
  60. //
  61. // static NodeRef edge_dest(EdgeRef)
  62. // Return the destination node of an edge.
  63. // static unsigned size (GraphType *G)
  64. // Return total number of nodes in the graph
  65. // If anyone tries to use this class without having an appropriate
  66. // specialization, make an error. If you get this error, it's because you
  67. // need to include the appropriate specialization of GraphTraits<> for your
  68. // graph, or you need to define it for a new graph type. Either that or
  69. // your argument to XXX_begin(...) is unknown or needs to have the proper .h
  70. // file #include'd.
  71. using NodeRef = typename GraphType::UnknownGraphTypeError;
  72. };
  73. // Inverse - This class is used as a little marker class to tell the graph
  74. // iterator to iterate over the graph in a graph defined "Inverse" ordering.
  75. // Not all graphs define an inverse ordering, and if they do, it depends on
  76. // the graph exactly what that is. Here's an example of usage with the
  77. // df_iterator:
  78. //
  79. // idf_iterator<Method*> I = idf_begin(M), E = idf_end(M);
  80. // for (; I != E; ++I) { ... }
  81. //
  82. // Which is equivalent to:
  83. // df_iterator<Inverse<Method*>> I = idf_begin(M), E = idf_end(M);
  84. // for (; I != E; ++I) { ... }
  85. //
  86. template <class GraphType>
  87. struct Inverse {
  88. const GraphType &Graph;
  89. inline Inverse(const GraphType &G) : Graph(G) {}
  90. };
  91. // Provide a partial specialization of GraphTraits so that the inverse of an
  92. // inverse falls back to the original graph.
  93. template <class T> struct GraphTraits<Inverse<Inverse<T>>> : GraphTraits<T> {};
  94. // Provide iterator ranges for the graph traits nodes and children
  95. template <class GraphType>
  96. iterator_range<typename GraphTraits<GraphType>::nodes_iterator>
  97. nodes(const GraphType &G) {
  98. return make_range(GraphTraits<GraphType>::nodes_begin(G),
  99. GraphTraits<GraphType>::nodes_end(G));
  100. }
  101. template <class GraphType>
  102. iterator_range<typename GraphTraits<Inverse<GraphType>>::nodes_iterator>
  103. inverse_nodes(const GraphType &G) {
  104. return make_range(GraphTraits<Inverse<GraphType>>::nodes_begin(G),
  105. GraphTraits<Inverse<GraphType>>::nodes_end(G));
  106. }
  107. template <class GraphType>
  108. iterator_range<typename GraphTraits<GraphType>::ChildIteratorType>
  109. children(const typename GraphTraits<GraphType>::NodeRef &G) {
  110. return make_range(GraphTraits<GraphType>::child_begin(G),
  111. GraphTraits<GraphType>::child_end(G));
  112. }
  113. template <class GraphType>
  114. iterator_range<typename GraphTraits<Inverse<GraphType>>::ChildIteratorType>
  115. inverse_children(const typename GraphTraits<GraphType>::NodeRef &G) {
  116. return make_range(GraphTraits<Inverse<GraphType>>::child_begin(G),
  117. GraphTraits<Inverse<GraphType>>::child_end(G));
  118. }
  119. template <class GraphType>
  120. iterator_range<typename GraphTraits<GraphType>::ChildEdgeIteratorType>
  121. children_edges(const typename GraphTraits<GraphType>::NodeRef &G) {
  122. return make_range(GraphTraits<GraphType>::child_edge_begin(G),
  123. GraphTraits<GraphType>::child_edge_end(G));
  124. }
  125. } // end namespace llvm
  126. #endif // LLVM_ADT_GRAPHTRAITS_H
  127. #ifdef __GNUC__
  128. #pragma GCC diagnostic pop
  129. #endif