blob: 5b42a68774b8b434715888cb292bd0a8549aa083 [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 {
25
26/// Each basic block in a CFG function contains a list of basic block arguments,
27/// normal instructions, and a terminator instruction.
28///
29/// Basic blocks form a graph (the CFG) which can be traversed through
30/// predecessor and successor edges.
Chris Lattner3a467cc2018-07-01 20:28:00 -070031class BasicBlock
32 : public llvm::ilist_node_with_parent<BasicBlock, CFGFunction> {
Chris Lattner4c95a502018-06-23 16:03:42 -070033public:
Chris Lattner3a467cc2018-07-01 20:28:00 -070034 explicit BasicBlock();
35 ~BasicBlock();
Chris Lattner4c95a502018-06-23 16:03:42 -070036
37 /// Return the function that a BasicBlock is part of.
38 CFGFunction *getFunction() const {
39 return function;
40 }
41
Chris Lattnered65a732018-06-28 20:45:33 -070042 // TODO: bb arguments
43
Chris Lattner3a467cc2018-07-01 20:28:00 -070044 /// Unlink this BasicBlock from its CFGFunction and delete it.
45 void eraseFromFunction();
Chris Lattner4c95a502018-06-23 16:03:42 -070046
Chris Lattner3a467cc2018-07-01 20:28:00 -070047 //===--------------------------------------------------------------------===//
48 // Operation list management
49 //===--------------------------------------------------------------------===//
50
51 /// This is the list of operations in the block.
52 typedef llvm::iplist<OperationInst> OperationListType;
53 OperationListType &getOperations() { return operations; }
54 const OperationListType &getOperations() const { return operations; }
55
56 // Iteration over the operations in the block.
57 using iterator = OperationListType::iterator;
58 using const_iterator = OperationListType::const_iterator;
59 using reverse_iterator = OperationListType::reverse_iterator;
60 using const_reverse_iterator = OperationListType::const_reverse_iterator;
61
62 iterator begin() { return operations.begin(); }
63 iterator end() { return operations.end(); }
64 const_iterator begin() const { return operations.begin(); }
65 const_iterator end() const { return operations.end(); }
66 reverse_iterator rbegin() { return operations.rbegin(); }
67 reverse_iterator rend() { return operations.rend(); }
68 const_reverse_iterator rbegin() const { return operations.rbegin(); }
69 const_reverse_iterator rend() const { return operations.rend(); }
70
71 bool empty() const { return operations.empty(); }
72 void push_back(OperationInst *inst) { operations.push_back(inst); }
73 void push_front(OperationInst *inst) { operations.push_front(inst); }
74
75 OperationInst &back() { return operations.back(); }
76 const OperationInst &back() const {
77 return const_cast<BasicBlock *>(this)->back();
Chris Lattner4c95a502018-06-23 16:03:42 -070078 }
Chris Lattner3a467cc2018-07-01 20:28:00 -070079
80 OperationInst &front() { return operations.front(); }
81 const OperationInst &front() const {
82 return const_cast<BasicBlock*>(this)->front();
83 }
84
85 //===--------------------------------------------------------------------===//
86 // Terminator management
87 //===--------------------------------------------------------------------===//
88
89 /// Change the terminator of this block to the specified instruction.
90 void setTerminator(TerminatorInst *inst);
91
Chris Lattner4c95a502018-06-23 16:03:42 -070092 TerminatorInst *getTerminator() const { return terminator; }
93
94 void print(raw_ostream &os) const;
95 void dump() const;
96
Chris Lattner3a467cc2018-07-01 20:28:00 -070097 /// getSublistAccess() - Returns pointer to member of operation list
98 static OperationListType BasicBlock::*getSublistAccess(OperationInst*) {
99 return &BasicBlock::operations;
100 }
101
Chris Lattner4c95a502018-06-23 16:03:42 -0700102private:
Chris Lattner3a467cc2018-07-01 20:28:00 -0700103 CFGFunction *function = nullptr;
104
105 /// This is the list of operations in the block.
106 OperationListType operations;
107
108 /// This is the owning reference to the terminator of the block.
Chris Lattner4c95a502018-06-23 16:03:42 -0700109 TerminatorInst *terminator = nullptr;
Chris Lattner3a467cc2018-07-01 20:28:00 -0700110
111 BasicBlock(const BasicBlock&) = delete;
112 void operator=(const BasicBlock&) = delete;
113
114 friend struct llvm::ilist_traits<BasicBlock>;
Chris Lattner4c95a502018-06-23 16:03:42 -0700115};
116
117} // end namespace mlir
118
Chris Lattner3a467cc2018-07-01 20:28:00 -0700119//===----------------------------------------------------------------------===//
120// ilist_traits for OperationInst
121//===----------------------------------------------------------------------===//
122
123namespace llvm {
124
125template <>
126struct ilist_traits<::mlir::BasicBlock>
127 : public ilist_alloc_traits<::mlir::BasicBlock> {
128 using BasicBlock = ::mlir::BasicBlock;
129 using block_iterator = simple_ilist<BasicBlock>::iterator;
130
131 void addNodeToList(BasicBlock *block);
132 void removeNodeFromList(BasicBlock *block);
133 void transferNodesFromList(ilist_traits<BasicBlock> &otherList,
134 block_iterator first, block_iterator last);
135private:
136 mlir::CFGFunction *getContainingFunction();
137};
138} // end namespace llvm
139
140
Chris Lattner4c95a502018-06-23 16:03:42 -0700141#endif // MLIR_IR_BASICBLOCK_H