blob: 8cbdabccc06166e47f39bc357415e249cfedbbc0 [file] [log] [blame]
Chris Lattner4c95a502018-06-23 16:03:42 -07001//===- Instructions.cpp - MLIR CFGFunction Instruction Classes ------------===//
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#include "mlir/IR/Instructions.h"
19#include "mlir/IR/BasicBlock.h"
20using namespace mlir;
21
Chris Lattner6119d382018-07-20 18:41:34 -070022/// Replace all uses of 'this' value with the new value, updating anything in
23/// the IR that uses 'this' to use the other value instead. When this returns
24/// there are zero uses of 'this'.
25void SSAValue::replaceAllUsesWith(SSAValue *newValue) {
26 assert(this != newValue && "cannot RAUW a value with itself");
27 while (!use_empty()) {
28 use_begin()->set(newValue);
29 }
30}
31
32/// Return the result number of this result.
33unsigned InstResult::getResultNumber() const {
34 // Results are always stored consecutively, so use pointer subtraction to
35 // figure out what number this is.
36 return this - &getOwner()->getInstResults()[0];
37}
38
Chris Lattnered65a732018-06-28 20:45:33 -070039//===----------------------------------------------------------------------===//
40// Instruction
41//===----------------------------------------------------------------------===//
42
Chris Lattner3a467cc2018-07-01 20:28:00 -070043// Instructions are deleted through the destroy() member because we don't have
44// a virtual destructor.
45Instruction::~Instruction() {
46 assert(block == nullptr && "instruction destroyed but still in a block");
47}
48
49/// Destroy this instruction or one of its subclasses.
Chris Lattner6119d382018-07-20 18:41:34 -070050void Instruction::destroy() {
51 switch (getKind()) {
Chris Lattner3a467cc2018-07-01 20:28:00 -070052 case Kind::Operation:
Chris Lattner6119d382018-07-20 18:41:34 -070053 cast<OperationInst>(this)->destroy();
Chris Lattner3a467cc2018-07-01 20:28:00 -070054 break;
55 case Kind::Branch:
Chris Lattner6119d382018-07-20 18:41:34 -070056 delete cast<BranchInst>(this);
Chris Lattner3a467cc2018-07-01 20:28:00 -070057 break;
James Molloy4f788372018-07-24 15:01:27 -070058 case Kind::CondBranch:
59 delete cast<CondBranchInst>(this);
60 break;
Chris Lattner3a467cc2018-07-01 20:28:00 -070061 case Kind::Return:
Chris Lattner40746442018-07-21 14:32:09 -070062 cast<ReturnInst>(this)->destroy();
Chris Lattner3a467cc2018-07-01 20:28:00 -070063 break;
64 }
65}
66
Chris Lattner6119d382018-07-20 18:41:34 -070067void OperationInst::destroy() {
68 this->~OperationInst();
69 free(this);
70}
71
Chris Lattnered65a732018-06-28 20:45:33 -070072CFGFunction *Instruction::getFunction() const {
Chris Lattner4c95a502018-06-23 16:03:42 -070073 return getBlock()->getFunction();
74}
75
Chris Lattner68a3fd02018-07-23 10:08:00 -070076unsigned Instruction::getNumOperands() const {
77 switch (getKind()) {
78 case Kind::Operation:
79 return cast<OperationInst>(this)->getNumOperands();
80 case Kind::Branch:
81 return cast<BranchInst>(this)->getNumOperands();
James Molloy4f788372018-07-24 15:01:27 -070082 case Kind::CondBranch:
83 return cast<CondBranchInst>(this)->getNumOperands();
Chris Lattner68a3fd02018-07-23 10:08:00 -070084 case Kind::Return:
85 return cast<ReturnInst>(this)->getNumOperands();
86 }
87}
88
89MutableArrayRef<InstOperand> Instruction::getInstOperands() {
90 switch (getKind()) {
91 case Kind::Operation:
92 return cast<OperationInst>(this)->getInstOperands();
93 case Kind::Branch:
94 return cast<BranchInst>(this)->getInstOperands();
James Molloy4f788372018-07-24 15:01:27 -070095 case Kind::CondBranch:
96 return cast<CondBranchInst>(this)->getInstOperands();
Chris Lattner68a3fd02018-07-23 10:08:00 -070097 case Kind::Return:
98 return cast<ReturnInst>(this)->getInstOperands();
99 }
100}
101
Chris Lattnered65a732018-06-28 20:45:33 -0700102//===----------------------------------------------------------------------===//
103// OperationInst
104//===----------------------------------------------------------------------===//
105
Chris Lattner3b2ef762018-07-18 15:31:25 -0700106/// Create a new OperationInst with the specific fields.
107OperationInst *OperationInst::create(Identifier name,
108 ArrayRef<CFGValue *> operands,
109 ArrayRef<Type *> resultTypes,
110 ArrayRef<NamedAttribute> attributes,
111 MLIRContext *context) {
112 auto byteSize = totalSizeToAlloc<InstOperand, InstResult>(operands.size(),
113 resultTypes.size());
Chris Lattner6119d382018-07-20 18:41:34 -0700114 void *rawMem = malloc(byteSize);
Chris Lattner3b2ef762018-07-18 15:31:25 -0700115
116 // Initialize the OperationInst part of the instruction.
117 auto inst = ::new (rawMem) OperationInst(
118 name, operands.size(), resultTypes.size(), attributes, context);
119
120 // Initialize the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700121 auto instOperands = inst->getInstOperands();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700122 for (unsigned i = 0, e = operands.size(); i != e; ++i)
123 new (&instOperands[i]) InstOperand(inst, operands[i]);
124
Chris Lattnerf8cce872018-07-20 09:28:54 -0700125 auto instResults = inst->getInstResults();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700126 for (unsigned i = 0, e = resultTypes.size(); i != e; ++i)
127 new (&instResults[i]) InstResult(resultTypes[i], inst);
128 return inst;
129}
130
131OperationInst::OperationInst(Identifier name, unsigned numOperands,
132 unsigned numResults,
133 ArrayRef<NamedAttribute> attributes,
134 MLIRContext *context)
James Molloy4f788372018-07-24 15:01:27 -0700135 : Operation(name, /*isInstruction=*/true, attributes, context),
Chris Lattner55315d52018-07-18 19:06:45 -0700136 Instruction(Kind::Operation), numOperands(numOperands),
137 numResults(numResults) {}
Chris Lattner3b2ef762018-07-18 15:31:25 -0700138
139OperationInst::~OperationInst() {
140 // Explicitly run the destructors for the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700141 for (auto &operand : getInstOperands())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700142 operand.~InstOperand();
143
Chris Lattnerf8cce872018-07-20 09:28:54 -0700144 for (auto &result : getInstResults())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700145 result.~InstResult();
146}
147
Chris Lattner3a467cc2018-07-01 20:28:00 -0700148mlir::BasicBlock *
149llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() {
150 size_t Offset(
151 size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr))));
152 iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this));
153 return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) -
James Molloy4f788372018-07-24 15:01:27 -0700154 Offset);
Chris Lattnered65a732018-06-28 20:45:33 -0700155}
156
Chris Lattner3a467cc2018-07-01 20:28:00 -0700157/// This is a trait method invoked when an instruction is added to a block. We
158/// keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700159void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList(
160 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700161 assert(!inst->getBlock() && "already in a basic block!");
162 inst->block = getContainingBlock();
163}
164
165/// This is a trait method invoked when an instruction is removed from a block.
166/// We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700167void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList(
168 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700169 assert(inst->block && "not already in a basic block!");
170 inst->block = nullptr;
171}
172
173/// This is a trait method invoked when an instruction is moved from one block
174/// to another. We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700175void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList(
176 ilist_traits<OperationInst> &otherList, instr_iterator first,
177 instr_iterator last) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700178 // If we are transferring instructions within the same basic block, the block
179 // pointer doesn't need to be updated.
180 BasicBlock *curParent = getContainingBlock();
181 if (curParent == otherList.getContainingBlock())
182 return;
183
184 // Update the 'block' member of each instruction.
185 for (; first != last; ++first)
186 first->block = curParent;
187}
188
189/// Unlink this instruction from its BasicBlock and delete it.
190void OperationInst::eraseFromBlock() {
191 assert(getBlock() && "Instruction has no parent");
192 getBlock()->getOperations().erase(this);
193}
194
Chris Lattner6119d382018-07-20 18:41:34 -0700195/// If this value is the result of an OperationInst, return the instruction
196/// that defines it.
197OperationInst *SSAValue::getDefiningInst() {
198 if (auto *result = dyn_cast<InstResult>(this))
199 return result->getOwner();
200 return nullptr;
201}
202
Chris Lattnered65a732018-06-28 20:45:33 -0700203//===----------------------------------------------------------------------===//
204// Terminators
205//===----------------------------------------------------------------------===//
206
Chris Lattner3a467cc2018-07-01 20:28:00 -0700207/// Remove this terminator from its BasicBlock and delete it.
208void TerminatorInst::eraseFromBlock() {
209 assert(getBlock() && "Instruction has no parent");
210 getBlock()->setTerminator(nullptr);
Chris Lattner6119d382018-07-20 18:41:34 -0700211 destroy();
Chris Lattner4c95a502018-06-23 16:03:42 -0700212}
213
Chris Lattner40746442018-07-21 14:32:09 -0700214/// Create a new OperationInst with the specific fields.
215ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) {
216 auto byteSize = totalSizeToAlloc<InstOperand>(operands.size());
217 void *rawMem = malloc(byteSize);
Chris Lattner3a467cc2018-07-01 20:28:00 -0700218
Chris Lattner40746442018-07-21 14:32:09 -0700219 // Initialize the ReturnInst part of the instruction.
220 auto inst = ::new (rawMem) ReturnInst(operands.size());
221
222 // Initialize the operands and results.
223 auto instOperands = inst->getInstOperands();
224 for (unsigned i = 0, e = operands.size(); i != e; ++i)
225 new (&instOperands[i]) InstOperand(inst, operands[i]);
226 return inst;
227}
228
229void ReturnInst::destroy() {
230 this->~ReturnInst();
231 free(this);
232}
233
234ReturnInst::~ReturnInst() {
235 // Explicitly run the destructors for the operands.
236 for (auto &operand : getInstOperands())
237 operand.~InstOperand();
238}
Chris Lattner1604e472018-07-23 08:42:19 -0700239
240/// Add one value to the operand list.
241void BranchInst::addOperand(CFGValue *value) {
242 operands.emplace_back(InstOperand(this, value));
243}
244
245/// Add a list of values to the operand list.
246void BranchInst::addOperands(ArrayRef<CFGValue *> values) {
247 operands.reserve(operands.size() + values.size());
248 for (auto *value : values)
249 addOperand(value);
250}
James Molloy4f788372018-07-24 15:01:27 -0700251
252/// Add one value to the true operand list.
253void CondBranchInst::addTrueOperand(CFGValue *value) {
254 assert(getNumFalseOperands() == 0 &&
255 "Must insert all true operands before false operands!");
256 operands.emplace_back(InstOperand(this, value));
257 ++numTrueOperands;
258}
259
260/// Add a list of values to the true operand list.
261void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) {
262 operands.reserve(operands.size() + values.size());
263 for (auto *value : values)
264 addTrueOperand(value);
265}
266
267/// Add one value to the false operand list.
268void CondBranchInst::addFalseOperand(CFGValue *value) {
269 operands.emplace_back(InstOperand(this, value));
270}
271
272/// Add a list of values to the false operand list.
273void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) {
274 operands.reserve(operands.size() + values.size());
275 for (auto *value : values)
276 addFalseOperand(value);
277}