MLIR 24.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
21namespace llvm {
22template <>
23struct GraphTraits<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 static unsigned getNumber(NodeRef node) { return node->getBlockID(); }
36};
37
38template <>
39struct GraphTraits<Inverse<mlir::Block *>> {
42 using NodeRef = Node *;
43 static NodeRef getEntryNode(Inverse<NodeRef> inverseGraph) {
44 return inverseGraph.Graph;
45 }
47 return node->pred_begin();
48 }
49 static inline ChildIteratorType child_end(NodeRef node) {
50 return node->pred_end();
51 }
52};
53
54template <>
55struct GraphTraits<const mlir::Block *> {
57 using Node = const mlir::Block;
58 using NodeRef = Node *;
59
60 static NodeRef getEntryNode(NodeRef node) { return node; }
61
63 return const_cast<mlir::Block *>(node)->succ_begin();
64 }
66 return const_cast<mlir::Block *>(node)->succ_end();
67 }
68
69 static unsigned getNumber(NodeRef node) { return node->getBlockID(); }
70};
71
72template <>
73struct GraphTraits<Inverse<const mlir::Block *>> {
75 using Node = const mlir::Block;
76 using NodeRef = Node *;
77
78 static NodeRef getEntryNode(Inverse<NodeRef> inverseGraph) {
79 return inverseGraph.Graph;
80 }
81
83 return const_cast<mlir::Block *>(node)->pred_begin();
84 }
86 return const_cast<mlir::Block *>(node)->pred_end();
87 }
88};
89
90template <>
91struct GraphTraits<mlir::Region *> : public GraphTraits<mlir::Block *> {
94
95 static NodeRef getEntryNode(GraphType fn) { return &fn->front(); }
96
97 using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
99 return nodes_iterator(fn->begin());
100 }
102 return nodes_iterator(fn->end());
103 }
104
105 static unsigned getMaxNumber(GraphType region) {
106 return region->getMaxBlockID();
107 }
108 static unsigned getNumberEpoch(GraphType region) {
109 return region->getBlockIDEpoch();
110 }
111};
112
113template <>
114struct GraphTraits<Inverse<mlir::Region *>>
115 : public GraphTraits<Inverse<mlir::Block *>> {
116 using GraphType = Inverse<mlir::Region *>;
118
119 static NodeRef getEntryNode(GraphType fn) { return &fn.Graph->front(); }
120
121 using nodes_iterator = pointer_iterator<mlir::Region::iterator>;
123 return nodes_iterator(fn.Graph->begin());
124 }
126 return nodes_iterator(fn.Graph->end());
127 }
128};
129
130} // namespace llvm
131
132#endif
Block represents an ordered list of Operations.
Definition Block.h:33
SuccessorRange::iterator succ_iterator
Definition Block.h:291
succ_iterator succ_end()
Definition Block.h:293
pred_iterator pred_begin()
Definition Block.h:260
succ_iterator succ_begin()
Definition Block.h:292
PredecessorIterator pred_iterator
Definition Block.h:259
unsigned getBlockID() const
Return an ID uniquely identifying this block within its parent region.
Definition Block.h:64
pred_iterator pred_end()
Definition Block.h:263
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
unsigned getMaxBlockID() const
One past the largest block ID handed out in this region; block IDs lie in [0, getMaxBlockID()).
Definition Region.h:81
Block & front()
Definition Region.h:65
iterator end()
Definition Region.h:56
iterator begin()
Definition Region.h:55
unsigned getBlockIDEpoch() const
The block-ID epoch, part of the generic number-indexed graph contract (LoopInfo, DominatorTree) for d...
Definition Region.h:86
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:227
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 unsigned getNumber(NodeRef node)
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 unsigned getNumber(NodeRef node)
static NodeRef getEntryNode(NodeRef bb)
static unsigned getNumberEpoch(GraphType region)
static NodeRef getEntryNode(GraphType fn)
static nodes_iterator nodes_begin(GraphType fn)
pointer_iterator< mlir::Region::iterator > nodes_iterator
static unsigned getMaxNumber(GraphType region)
static nodes_iterator nodes_end(GraphType fn)