MLIR  20.0.0git
RegionGraphTraits.h
Go to the documentation of this file.
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 <>
23 struct GraphTraits<mlir::Block *> {
25  using Node = mlir::Block;
26  using NodeRef = Node *;
27 
28  static NodeRef getEntryNode(NodeRef bb) { return bb; }
29 
31  return node->succ_begin();
32  }
33  static ChildIteratorType child_end(NodeRef node) { return node->succ_end(); }
34 };
35 
36 template <>
37 struct GraphTraits<Inverse<mlir::Block *>> {
39  using Node = mlir::Block;
40  using NodeRef = Node *;
41  static NodeRef getEntryNode(Inverse<NodeRef> inverseGraph) {
42  return inverseGraph.Graph;
43  }
44  static inline ChildIteratorType child_begin(NodeRef node) {
45  return node->pred_begin();
46  }
47  static inline ChildIteratorType child_end(NodeRef node) {
48  return node->pred_end();
49  }
50 };
51 
52 template <>
53 struct GraphTraits<const mlir::Block *> {
55  using Node = const mlir::Block;
56  using NodeRef = Node *;
57 
58  static NodeRef getEntryNode(NodeRef node) { return node; }
59 
61  return const_cast<mlir::Block *>(node)->succ_begin();
62  }
64  return const_cast<mlir::Block *>(node)->succ_end();
65  }
66 };
67 
68 template <>
69 struct GraphTraits<Inverse<const mlir::Block *>> {
71  using Node = const mlir::Block;
72  using NodeRef = Node *;
73 
74  static NodeRef getEntryNode(Inverse<NodeRef> inverseGraph) {
75  return inverseGraph.Graph;
76  }
77 
79  return const_cast<mlir::Block *>(node)->pred_begin();
80  }
82  return const_cast<mlir::Block *>(node)->pred_end();
83  }
84 };
85 
86 template <>
87 struct GraphTraits<mlir::Region *> : public GraphTraits<mlir::Block *> {
89  using NodeRef = mlir::Block *;
90 
91  static NodeRef getEntryNode(GraphType fn) { return &fn->front(); }
92 
93  using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
95  return nodes_iterator(fn->begin());
96  }
98  return nodes_iterator(fn->end());
99  }
100 };
101 
102 template <>
103 struct GraphTraits<Inverse<mlir::Region *>>
104  : public GraphTraits<Inverse<mlir::Block *>> {
105  using GraphType = Inverse<mlir::Region *>;
106  using NodeRef = NodeRef;
107 
108  static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); }
109 
110  using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
112  return nodes_iterator(fn.Graph->begin());
113  }
115  return nodes_iterator(fn.Graph->end());
116  }
117 };
118 
119 } // namespace llvm
120 
121 #endif
Block represents an ordered list of Operations.
Definition: Block.h:31
SuccessorRange::iterator succ_iterator
Definition: Block.h:262
succ_iterator succ_end()
Definition: Block.h:264
pred_iterator pred_begin()
Definition: Block.h:231
succ_iterator succ_begin()
Definition: Block.h:263
PredecessorIterator pred_iterator
Definition: Block.h:230
Operation & front()
Definition: Block.h:151
pred_iterator pred_end()
Definition: Block.h:234
Implement a predecessor iterator for blocks.
Definition: BlockSupport.h:51
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
iterator end()
Definition: Region.h:56
iterator begin()
Definition: Region.h:55
Block & front()
Definition: Region.h:65
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.
static ChildIteratorType child_begin(NodeRef node)
static NodeRef getEntryNode(Inverse< NodeRef > inverseGraph)
static ChildIteratorType child_end(NodeRef node)
static ChildIteratorType child_end(NodeRef node)
static NodeRef getEntryNode(Inverse< NodeRef > inverseGraph)
static ChildIteratorType child_begin(NodeRef node)
static nodes_iterator nodes_begin(GraphType fn)
static nodes_iterator nodes_end(GraphType fn)
pointer_iterator< mlir::Region::iterator > nodes_iterator
static ChildIteratorType child_begin(NodeRef node)
mlir::Block::succ_iterator ChildIteratorType
static NodeRef getEntryNode(NodeRef node)
static ChildIteratorType child_end(NodeRef node)
static ChildIteratorType child_end(NodeRef node)
mlir::Block::succ_iterator ChildIteratorType
static ChildIteratorType child_begin(NodeRef node)
static NodeRef getEntryNode(NodeRef bb)
static NodeRef getEntryNode(GraphType fn)
static nodes_iterator nodes_begin(GraphType fn)
pointer_iterator< mlir::Region::iterator > nodes_iterator
static nodes_iterator nodes_end(GraphType fn)