blob: d59a83bc69ef84243126b91985db37dea689cd6e [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(); }
Chris Lattner1604e472018-07-23 08:42:19 -070062
63 /// Add one value to the operand list.
James Molloy61a656c2018-07-22 15:45:24 -070064 BBArgument *addArgument(Type *type);
Chris Lattner1604e472018-07-23 08:42:19 -070065
66 /// Add one argument to the argument list for each type specified in the list.
67 llvm::iterator_range<args_iterator> addArguments(ArrayRef<Type *> types);
James Molloy61a656c2018-07-22 15:45:24 -070068
69 unsigned getNumArguments() const { return arguments.size(); }
70 BBArgument *getArgument(unsigned i) { return arguments[i]; }
71 const BBArgument *getArgument(unsigned i) const { return arguments[i]; }
72
73 //===--------------------------------------------------------------------===//
Chris Lattner3a467cc2018-07-01 20:28:00 -070074 // Operation list management
75 //===--------------------------------------------------------------------===//
76
77 /// This is the list of operations in the block.
78 typedef llvm::iplist<OperationInst> OperationListType;
79 OperationListType &getOperations() { return operations; }
80 const OperationListType &getOperations() const { return operations; }
81
82 // Iteration over the operations in the block.
83 using iterator = OperationListType::iterator;
84 using const_iterator = OperationListType::const_iterator;
85 using reverse_iterator = OperationListType::reverse_iterator;
86 using const_reverse_iterator = OperationListType::const_reverse_iterator;
87
88 iterator begin() { return operations.begin(); }
89 iterator end() { return operations.end(); }
90 const_iterator begin() const { return operations.begin(); }
91 const_iterator end() const { return operations.end(); }
92 reverse_iterator rbegin() { return operations.rbegin(); }
93 reverse_iterator rend() { return operations.rend(); }
94 const_reverse_iterator rbegin() const { return operations.rbegin(); }
95 const_reverse_iterator rend() const { return operations.rend(); }
96
97 bool empty() const { return operations.empty(); }
98 void push_back(OperationInst *inst) { operations.push_back(inst); }
99 void push_front(OperationInst *inst) { operations.push_front(inst); }
100
101 OperationInst &back() { return operations.back(); }
102 const OperationInst &back() const {
103 return const_cast<BasicBlock *>(this)->back();
Chris Lattner4c95a502018-06-23 16:03:42 -0700104 }
Chris Lattner3a467cc2018-07-01 20:28:00 -0700105
106 OperationInst &front() { return operations.front(); }
107 const OperationInst &front() const {
108 return const_cast<BasicBlock*>(this)->front();
109 }
110
111 //===--------------------------------------------------------------------===//
112 // Terminator management
113 //===--------------------------------------------------------------------===//
114
115 /// Change the terminator of this block to the specified instruction.
116 void setTerminator(TerminatorInst *inst);
117
Chris Lattner4c95a502018-06-23 16:03:42 -0700118 TerminatorInst *getTerminator() const { return terminator; }
119
120 void print(raw_ostream &os) const;
121 void dump() const;
122
Chris Lattner3a467cc2018-07-01 20:28:00 -0700123 /// getSublistAccess() - Returns pointer to member of operation list
124 static OperationListType BasicBlock::*getSublistAccess(OperationInst*) {
125 return &BasicBlock::operations;
126 }
127
Chris Lattner4c95a502018-06-23 16:03:42 -0700128private:
Chris Lattner3a467cc2018-07-01 20:28:00 -0700129 CFGFunction *function = nullptr;
130
131 /// This is the list of operations in the block.
132 OperationListType operations;
133
James Molloy61a656c2018-07-22 15:45:24 -0700134 /// This is the list of arguments to the block.
135 std::vector<BBArgument *> arguments;
136
Chris Lattner3a467cc2018-07-01 20:28:00 -0700137 /// This is the owning reference to the terminator of the block.
Chris Lattner4c95a502018-06-23 16:03:42 -0700138 TerminatorInst *terminator = nullptr;
Chris Lattner3a467cc2018-07-01 20:28:00 -0700139
140 BasicBlock(const BasicBlock&) = delete;
141 void operator=(const BasicBlock&) = delete;
142
143 friend struct llvm::ilist_traits<BasicBlock>;
Chris Lattner4c95a502018-06-23 16:03:42 -0700144};
145
146} // end namespace mlir
147
Chris Lattner3a467cc2018-07-01 20:28:00 -0700148//===----------------------------------------------------------------------===//
149// ilist_traits for OperationInst
150//===----------------------------------------------------------------------===//
151
152namespace llvm {
153
154template <>
155struct ilist_traits<::mlir::BasicBlock>
156 : public ilist_alloc_traits<::mlir::BasicBlock> {
157 using BasicBlock = ::mlir::BasicBlock;
158 using block_iterator = simple_ilist<BasicBlock>::iterator;
159
160 void addNodeToList(BasicBlock *block);
161 void removeNodeFromList(BasicBlock *block);
162 void transferNodesFromList(ilist_traits<BasicBlock> &otherList,
163 block_iterator first, block_iterator last);
164private:
165 mlir::CFGFunction *getContainingFunction();
166};
167} // end namespace llvm
168
169
Chris Lattner4c95a502018-06-23 16:03:42 -0700170#endif // MLIR_IR_BASICBLOCK_H