GraphTraits.h 5.9 KB

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