• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- RegionGraphTraits.h - llvm::GraphTraits for CFGs ---------*- 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 implements specializations of llvm::GraphTraits for various MLIR
10 // CFG data types.  This allows the generic LLVM graph algorithms to be applied
11 // to CFGs.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_IR_REGIONGRAPHTRAITS_H
16 #define MLIR_IR_REGIONGRAPHTRAITS_H
17 
18 #include "mlir/IR/Region.h"
19 #include "llvm/ADT/GraphTraits.h"
20 
21 namespace llvm {
22 template <> struct GraphTraits<mlir::Block *> {
23   using ChildIteratorType = mlir::Block::succ_iterator;
24   using Node = mlir::Block;
25   using NodeRef = Node *;
26 
27   static NodeRef getEntryNode(NodeRef bb) { return bb; }
28 
29   static ChildIteratorType child_begin(NodeRef node) {
30     return node->succ_begin();
31   }
32   static ChildIteratorType child_end(NodeRef node) { return node->succ_end(); }
33 };
34 
35 template <> struct GraphTraits<Inverse<mlir::Block *>> {
36   using ChildIteratorType = mlir::Block::pred_iterator;
37   using Node = mlir::Block;
38   using NodeRef = Node *;
39   static NodeRef getEntryNode(Inverse<NodeRef> inverseGraph) {
40     return inverseGraph.Graph;
41   }
42   static inline ChildIteratorType child_begin(NodeRef node) {
43     return node->pred_begin();
44   }
45   static inline ChildIteratorType child_end(NodeRef node) {
46     return node->pred_end();
47   }
48 };
49 
50 template <>
51 struct GraphTraits<mlir::Region *> : public GraphTraits<mlir::Block *> {
52   using GraphType = mlir::Region *;
53   using NodeRef = mlir::Block *;
54 
55   static NodeRef getEntryNode(GraphType fn) { return &fn->front(); }
56 
57   using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
58   static nodes_iterator nodes_begin(GraphType fn) {
59     return nodes_iterator(fn->begin());
60   }
61   static nodes_iterator nodes_end(GraphType fn) {
62     return nodes_iterator(fn->end());
63   }
64 };
65 
66 template <>
67 struct GraphTraits<Inverse<mlir::Region *>>
68     : public GraphTraits<Inverse<mlir::Block *>> {
69   using GraphType = Inverse<mlir::Region *>;
70   using NodeRef = NodeRef;
71 
72   static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); }
73 
74   using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
75   static nodes_iterator nodes_begin(GraphType fn) {
76     return nodes_iterator(fn.Graph->begin());
77   }
78   static nodes_iterator nodes_end(GraphType fn) {
79     return nodes_iterator(fn.Graph->end());
80   }
81 };
82 
83 } // namespace llvm
84 
85 #endif
86