blob: be64e4f24616a3732875c705346743bfe0c3c066 [file] [log] [blame]
Chris Lattner4c95a502018-06-23 16:03:42 -07001//===- BasicBlock.h - MLIR BasicBlock Class ---------------------*- C++ -*-===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#ifndef MLIR_IR_BASICBLOCK_H
19#define MLIR_IR_BASICBLOCK_H
20
21#include "mlir/IR/Instructions.h"
Chris Lattner3a467cc2018-07-01 20:28:00 -070022#include <memory>
Chris Lattner4c95a502018-06-23 16:03:42 -070023
24namespace mlir {
James Molloy61a656c2018-07-22 15:45:24 -070025class BBArgument;
Chris Lattner4c95a502018-06-23 16:03:42 -070026
27/// Each basic block in a CFG function contains a list of basic block arguments,
28/// normal instructions, and a terminator instruction.
29///
30/// Basic blocks form a graph (the CFG) which can be traversed through
31/// predecessor and successor edges.
Chris Lattner3a467cc2018-07-01 20:28:00 -070032class BasicBlock
33 : public llvm::ilist_node_with_parent<BasicBlock, CFGFunction> {
Chris Lattner4c95a502018-06-23 16:03:42 -070034public:
Chris Lattner3a467cc2018-07-01 20:28:00 -070035 explicit BasicBlock();
36 ~BasicBlock();
Chris Lattner4c95a502018-06-23 16:03:42 -070037
38 /// Return the function that a BasicBlock is part of.
39 CFGFunction *getFunction() const {
40 return function;
41 }
42
Chris Lattner3a467cc2018-07-01 20:28:00 -070043 /// Unlink this BasicBlock from its CFGFunction and delete it.
44 void eraseFromFunction();
Chris Lattner4c95a502018-06-23 16:03:42 -070045
Chris Lattner3a467cc2018-07-01 20:28:00 -070046 //===--------------------------------------------------------------------===//
James Molloy61a656c2018-07-22 15:45:24 -070047 // Block arguments management
48 //===--------------------------------------------------------------------===//
49
50 // This is the list of arguments to the block.
51 typedef ArrayRef<BBArgument *> BBArgListType;
52 BBArgListType getArguments() const { return arguments; }
53
54 using args_iterator = BBArgListType::iterator;
55 using reverse_args_iterator = BBArgListType::reverse_iterator;
56 args_iterator args_begin() const { return getArguments().begin(); }
57 args_iterator args_end() const { return getArguments().end(); }
58 reverse_args_iterator args_rbegin() const { return getArguments().rbegin(); }
59 reverse_args_iterator args_rend() const { return getArguments().rend(); }
60
61 bool args_empty() const { return arguments.empty(); }
62 BBArgument *addArgument(Type *type);
63 llvm::iterator_range<BBArgListType::iterator>
64 addArguments(ArrayRef<Type *> types);
65
66 unsigned getNumArguments() const { return arguments.size(); }
67 BBArgument *getArgument(unsigned i) { return arguments[i]; }
68 const BBArgument *getArgument(unsigned i) const { return arguments[i]; }
69
70 //===--------------------------------------------------------------------===//
Chris Lattner3a467cc2018-07-01 20:28:00 -070071 // Operation list management
72 //===--------------------------------------------------------------------===//
73
74 /// This is the list of operations in the block.
75 typedef llvm::iplist<OperationInst> OperationListType;
76 OperationListType &getOperations() { return operations; }
77 const OperationListType &getOperations() const { return operations; }
78
79 // Iteration over the operations in the block.
80 using iterator = OperationListType::iterator;
81 using const_iterator = OperationListType::const_iterator;
82 using reverse_iterator = OperationListType::reverse_iterator;
83 using const_reverse_iterator = OperationListType::const_reverse_iterator;
84
85 iterator begin() { return operations.begin(); }
86 iterator end() { return operations.end(); }
87 const_iterator begin() const { return operations.begin(); }
88 const_iterator end() const { return operations.end(); }
89 reverse_iterator rbegin() { return operations.rbegin(); }
90 reverse_iterator rend() { return operations.rend(); }
91 const_reverse_iterator rbegin() const { return operations.rbegin(); }
92 const_reverse_iterator rend() const { return operations.rend(); }
93
94 bool empty() const { return operations.empty(); }
95 void push_back(OperationInst *inst) { operations.push_back(inst); }
96 void push_front(OperationInst *inst) { operations.push_front(inst); }
97
98 OperationInst &back() { return operations.back(); }
99 const OperationInst &back() const {
100 return const_cast<BasicBlock *>(this)->back();
Chris Lattner4c95a502018-06-23 16:03:42 -0700101 }
Chris Lattner3a467cc2018-07-01 20:28:00 -0700102
103 OperationInst &front() { return operations.front(); }
104 const OperationInst &front() const {
105 return const_cast<BasicBlock*>(this)->front();
106 }
107
108 //===--------------------------------------------------------------------===//
109 // Terminator management
110 //===--------------------------------------------------------------------===//
111
112 /// Change the terminator of this block to the specified instruction.
113 void setTerminator(TerminatorInst *inst);
114
Chris Lattner4c95a502018-06-23 16:03:42 -0700115 TerminatorInst *getTerminator() const { return terminator; }
116
117 void print(raw_ostream &os) const;
118 void dump() const;
119
Chris Lattner3a467cc2018-07-01 20:28:00 -0700120 /// getSublistAccess() - Returns pointer to member of operation list
121 static OperationListType BasicBlock::*getSublistAccess(OperationInst*) {
122 return &BasicBlock::operations;
123 }
124
Chris Lattner4c95a502018-06-23 16:03:42 -0700125private:
Chris Lattner3a467cc2018-07-01 20:28:00 -0700126 CFGFunction *function = nullptr;
127
128 /// This is the list of operations in the block.
129 OperationListType operations;
130
James Molloy61a656c2018-07-22 15:45:24 -0700131 /// This is the list of arguments to the block.
132 std::vector<BBArgument *> arguments;
133
Chris Lattner3a467cc2018-07-01 20:28:00 -0700134 /// This is the owning reference to the terminator of the block.
Chris Lattner4c95a502018-06-23 16:03:42 -0700135 TerminatorInst *terminator = nullptr;
Chris Lattner3a467cc2018-07-01 20:28:00 -0700136
137 BasicBlock(const BasicBlock&) = delete;
138 void operator=(const BasicBlock&) = delete;
139
140 friend struct llvm::ilist_traits<BasicBlock>;
Chris Lattner4c95a502018-06-23 16:03:42 -0700141};
142
143} // end namespace mlir
144
Chris Lattner3a467cc2018-07-01 20:28:00 -0700145//===----------------------------------------------------------------------===//
146// ilist_traits for OperationInst
147//===----------------------------------------------------------------------===//
148
149namespace llvm {
150
151template <>
152struct ilist_traits<::mlir::BasicBlock>
153 : public ilist_alloc_traits<::mlir::BasicBlock> {
154 using BasicBlock = ::mlir::BasicBlock;
155 using block_iterator = simple_ilist<BasicBlock>::iterator;
156
157 void addNodeToList(BasicBlock *block);
158 void removeNodeFromList(BasicBlock *block);
159 void transferNodesFromList(ilist_traits<BasicBlock> &otherList,
160 block_iterator first, block_iterator last);
161private:
162 mlir::CFGFunction *getContainingFunction();
163};
164} // end namespace llvm
165
166
Chris Lattner4c95a502018-06-23 16:03:42 -0700167#endif // MLIR_IR_BASICBLOCK_H