blob: a10cb3ae5b47bbbe4adb913278c81d867f0854a2 [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"
Chris Lattner95865062018-08-01 10:18:59 -070019#include "mlir/IR/CFGFunction.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070020using 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'.
Chris Lattner1e9fd7f2018-07-26 08:56:26 -070025void IRObjectWithUseList::replaceAllUsesWith(IRObjectWithUseList *newValue) {
Chris Lattner6119d382018-07-20 18:41:34 -070026 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 Lattner95865062018-08-01 10:18:59 -070072/// Return the context this operation is associated with.
73MLIRContext *Instruction::getContext() const {
74 return getFunction()->getContext();
75}
76
Chris Lattnered65a732018-06-28 20:45:33 -070077CFGFunction *Instruction::getFunction() const {
Chris Lattner4c95a502018-06-23 16:03:42 -070078 return getBlock()->getFunction();
79}
80
Chris Lattner68a3fd02018-07-23 10:08:00 -070081unsigned Instruction::getNumOperands() const {
82 switch (getKind()) {
83 case Kind::Operation:
84 return cast<OperationInst>(this)->getNumOperands();
85 case Kind::Branch:
86 return cast<BranchInst>(this)->getNumOperands();
James Molloy4f788372018-07-24 15:01:27 -070087 case Kind::CondBranch:
88 return cast<CondBranchInst>(this)->getNumOperands();
Chris Lattner68a3fd02018-07-23 10:08:00 -070089 case Kind::Return:
90 return cast<ReturnInst>(this)->getNumOperands();
91 }
92}
93
94MutableArrayRef<InstOperand> Instruction::getInstOperands() {
95 switch (getKind()) {
96 case Kind::Operation:
97 return cast<OperationInst>(this)->getInstOperands();
98 case Kind::Branch:
99 return cast<BranchInst>(this)->getInstOperands();
James Molloy4f788372018-07-24 15:01:27 -0700100 case Kind::CondBranch:
101 return cast<CondBranchInst>(this)->getInstOperands();
Chris Lattner68a3fd02018-07-23 10:08:00 -0700102 case Kind::Return:
103 return cast<ReturnInst>(this)->getInstOperands();
104 }
105}
106
Chris Lattnera8e47672018-07-25 14:08:16 -0700107/// This drops all operand uses from this instruction, which is an essential
108/// step in breaking cyclic dependences between references when they are to
109/// be deleted.
110void Instruction::dropAllReferences() {
111 for (auto &op : getInstOperands())
112 op.drop();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700113
114 if (auto *term = dyn_cast<TerminatorInst>(this))
Chris Lattner548cd7f2018-07-26 11:30:28 -0700115 for (auto &dest : term->getBasicBlockOperands())
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700116 dest.drop();
Chris Lattnera8e47672018-07-25 14:08:16 -0700117}
118
Chris Lattnered65a732018-06-28 20:45:33 -0700119//===----------------------------------------------------------------------===//
120// OperationInst
121//===----------------------------------------------------------------------===//
122
Chris Lattner3b2ef762018-07-18 15:31:25 -0700123/// Create a new OperationInst with the specific fields.
124OperationInst *OperationInst::create(Identifier name,
125 ArrayRef<CFGValue *> operands,
126 ArrayRef<Type *> resultTypes,
127 ArrayRef<NamedAttribute> attributes,
128 MLIRContext *context) {
129 auto byteSize = totalSizeToAlloc<InstOperand, InstResult>(operands.size(),
130 resultTypes.size());
Chris Lattner6119d382018-07-20 18:41:34 -0700131 void *rawMem = malloc(byteSize);
Chris Lattner3b2ef762018-07-18 15:31:25 -0700132
133 // Initialize the OperationInst part of the instruction.
134 auto inst = ::new (rawMem) OperationInst(
135 name, operands.size(), resultTypes.size(), attributes, context);
136
137 // Initialize the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700138 auto instOperands = inst->getInstOperands();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700139 for (unsigned i = 0, e = operands.size(); i != e; ++i)
140 new (&instOperands[i]) InstOperand(inst, operands[i]);
141
Chris Lattnerf8cce872018-07-20 09:28:54 -0700142 auto instResults = inst->getInstResults();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700143 for (unsigned i = 0, e = resultTypes.size(); i != e; ++i)
144 new (&instResults[i]) InstResult(resultTypes[i], inst);
145 return inst;
146}
147
148OperationInst::OperationInst(Identifier name, unsigned numOperands,
149 unsigned numResults,
150 ArrayRef<NamedAttribute> attributes,
151 MLIRContext *context)
James Molloy4f788372018-07-24 15:01:27 -0700152 : Operation(name, /*isInstruction=*/true, attributes, context),
Chris Lattner55315d52018-07-18 19:06:45 -0700153 Instruction(Kind::Operation), numOperands(numOperands),
154 numResults(numResults) {}
Chris Lattner3b2ef762018-07-18 15:31:25 -0700155
156OperationInst::~OperationInst() {
157 // Explicitly run the destructors for the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700158 for (auto &operand : getInstOperands())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700159 operand.~InstOperand();
160
Chris Lattnerf8cce872018-07-20 09:28:54 -0700161 for (auto &result : getInstResults())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700162 result.~InstResult();
163}
164
Chris Lattner3a467cc2018-07-01 20:28:00 -0700165mlir::BasicBlock *
166llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() {
167 size_t Offset(
168 size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr))));
169 iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this));
170 return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) -
James Molloy4f788372018-07-24 15:01:27 -0700171 Offset);
Chris Lattnered65a732018-06-28 20:45:33 -0700172}
173
Chris Lattner3a467cc2018-07-01 20:28:00 -0700174/// This is a trait method invoked when an instruction is added to a block. We
175/// keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700176void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList(
177 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700178 assert(!inst->getBlock() && "already in a basic block!");
179 inst->block = getContainingBlock();
180}
181
182/// This is a trait method invoked when an instruction is removed from a block.
183/// We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700184void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList(
185 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700186 assert(inst->block && "not already in a basic block!");
187 inst->block = nullptr;
188}
189
190/// This is a trait method invoked when an instruction is moved from one block
191/// to another. We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700192void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList(
193 ilist_traits<OperationInst> &otherList, instr_iterator first,
194 instr_iterator last) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700195 // If we are transferring instructions within the same basic block, the block
196 // pointer doesn't need to be updated.
197 BasicBlock *curParent = getContainingBlock();
198 if (curParent == otherList.getContainingBlock())
199 return;
200
201 // Update the 'block' member of each instruction.
202 for (; first != last; ++first)
203 first->block = curParent;
204}
205
206/// Unlink this instruction from its BasicBlock and delete it.
207void OperationInst::eraseFromBlock() {
208 assert(getBlock() && "Instruction has no parent");
209 getBlock()->getOperations().erase(this);
210}
211
Chris Lattnered65a732018-06-28 20:45:33 -0700212//===----------------------------------------------------------------------===//
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700213// TerminatorInst
Chris Lattnered65a732018-06-28 20:45:33 -0700214//===----------------------------------------------------------------------===//
215
Chris Lattner3a467cc2018-07-01 20:28:00 -0700216/// Remove this terminator from its BasicBlock and delete it.
217void TerminatorInst::eraseFromBlock() {
218 assert(getBlock() && "Instruction has no parent");
219 getBlock()->setTerminator(nullptr);
Chris Lattner6119d382018-07-20 18:41:34 -0700220 destroy();
Chris Lattner4c95a502018-06-23 16:03:42 -0700221}
222
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700223/// Return the list of destination entries that this terminator branches to.
Chris Lattner548cd7f2018-07-26 11:30:28 -0700224MutableArrayRef<BasicBlockOperand> TerminatorInst::getBasicBlockOperands() {
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700225 switch (getKind()) {
226 case Kind::Operation:
MLIR Team6f8692f2018-07-26 09:23:10 -0700227 llvm_unreachable("not a terminator");
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700228 case Kind::Branch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700229 return cast<BranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700230 case Kind::CondBranch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700231 return cast<CondBranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700232 case Kind::Return:
233 // Return has no basic block successors.
234 return {};
235 }
236}
237
238//===----------------------------------------------------------------------===//
239// ReturnInst
240//===----------------------------------------------------------------------===//
241
Chris Lattner40746442018-07-21 14:32:09 -0700242/// Create a new OperationInst with the specific fields.
243ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) {
244 auto byteSize = totalSizeToAlloc<InstOperand>(operands.size());
245 void *rawMem = malloc(byteSize);
Chris Lattner3a467cc2018-07-01 20:28:00 -0700246
Chris Lattner40746442018-07-21 14:32:09 -0700247 // Initialize the ReturnInst part of the instruction.
248 auto inst = ::new (rawMem) ReturnInst(operands.size());
249
250 // Initialize the operands and results.
251 auto instOperands = inst->getInstOperands();
252 for (unsigned i = 0, e = operands.size(); i != e; ++i)
253 new (&instOperands[i]) InstOperand(inst, operands[i]);
254 return inst;
255}
256
257void ReturnInst::destroy() {
258 this->~ReturnInst();
259 free(this);
260}
261
262ReturnInst::~ReturnInst() {
263 // Explicitly run the destructors for the operands.
264 for (auto &operand : getInstOperands())
265 operand.~InstOperand();
266}
Chris Lattner1604e472018-07-23 08:42:19 -0700267
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700268//===----------------------------------------------------------------------===//
269// BranchInst
270//===----------------------------------------------------------------------===//
271
272BranchInst::BranchInst(BasicBlock *dest)
273 : TerminatorInst(Kind::Branch), dest(this, dest) {}
274
275void BranchInst::setDest(BasicBlock *block) { dest.set(block); }
276
Chris Lattner1604e472018-07-23 08:42:19 -0700277/// Add one value to the operand list.
278void BranchInst::addOperand(CFGValue *value) {
279 operands.emplace_back(InstOperand(this, value));
280}
281
282/// Add a list of values to the operand list.
283void BranchInst::addOperands(ArrayRef<CFGValue *> values) {
284 operands.reserve(operands.size() + values.size());
285 for (auto *value : values)
286 addOperand(value);
287}
James Molloy4f788372018-07-24 15:01:27 -0700288
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700289//===----------------------------------------------------------------------===//
290// CondBranchInst
291//===----------------------------------------------------------------------===//
292
293CondBranchInst::CondBranchInst(CFGValue *condition, BasicBlock *trueDest,
294 BasicBlock *falseDest)
295 : TerminatorInst(Kind::CondBranch),
296 condition(condition), dests{{this}, {this}}, numTrueOperands(0) {
297 dests[falseIndex].set(falseDest);
298 dests[trueIndex].set(trueDest);
299}
300
James Molloy4f788372018-07-24 15:01:27 -0700301/// Add one value to the true operand list.
302void CondBranchInst::addTrueOperand(CFGValue *value) {
303 assert(getNumFalseOperands() == 0 &&
304 "Must insert all true operands before false operands!");
305 operands.emplace_back(InstOperand(this, value));
306 ++numTrueOperands;
307}
308
309/// Add a list of values to the true operand list.
310void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) {
311 operands.reserve(operands.size() + values.size());
312 for (auto *value : values)
313 addTrueOperand(value);
314}
315
316/// Add one value to the false operand list.
317void CondBranchInst::addFalseOperand(CFGValue *value) {
318 operands.emplace_back(InstOperand(this, value));
319}
320
321/// Add a list of values to the false operand list.
322void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) {
323 operands.reserve(operands.size() + values.size());
324 for (auto *value : values)
325 addFalseOperand(value);
326}