blob: 847907b753c8e18eef09501497f6303513dcf588 [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
Uday Bondhugula15984952018-08-01 22:36:12 -0700148OperationInst *OperationInst::clone() const {
149 SmallVector<CFGValue *, 8> operands;
150 SmallVector<Type *, 8> resultTypes;
151
152 // TODO(clattner): switch to iterator logic.
153 // Put together the operands and results.
154 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
155 operands.push_back(getInstOperand(i).get());
156
157 for (unsigned i = 0, e = getNumResults(); i != e; ++i)
158 resultTypes.push_back(getInstResult(i).getType());
159
160 return create(getName(), operands, resultTypes, getAttrs(), getContext());
161}
162
Chris Lattner3b2ef762018-07-18 15:31:25 -0700163OperationInst::OperationInst(Identifier name, unsigned numOperands,
164 unsigned numResults,
165 ArrayRef<NamedAttribute> attributes,
166 MLIRContext *context)
James Molloy4f788372018-07-24 15:01:27 -0700167 : Operation(name, /*isInstruction=*/true, attributes, context),
Chris Lattner55315d52018-07-18 19:06:45 -0700168 Instruction(Kind::Operation), numOperands(numOperands),
169 numResults(numResults) {}
Chris Lattner3b2ef762018-07-18 15:31:25 -0700170
171OperationInst::~OperationInst() {
172 // Explicitly run the destructors for the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700173 for (auto &operand : getInstOperands())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700174 operand.~InstOperand();
175
Chris Lattnerf8cce872018-07-20 09:28:54 -0700176 for (auto &result : getInstResults())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700177 result.~InstResult();
178}
179
Chris Lattner3a467cc2018-07-01 20:28:00 -0700180mlir::BasicBlock *
181llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() {
182 size_t Offset(
183 size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr))));
184 iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this));
185 return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) -
James Molloy4f788372018-07-24 15:01:27 -0700186 Offset);
Chris Lattnered65a732018-06-28 20:45:33 -0700187}
188
Chris Lattner3a467cc2018-07-01 20:28:00 -0700189/// This is a trait method invoked when an instruction is added to a block. We
190/// keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700191void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList(
192 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700193 assert(!inst->getBlock() && "already in a basic block!");
194 inst->block = getContainingBlock();
195}
196
197/// This is a trait method invoked when an instruction is removed from a block.
198/// We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700199void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList(
200 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700201 assert(inst->block && "not already in a basic block!");
202 inst->block = nullptr;
203}
204
205/// This is a trait method invoked when an instruction is moved from one block
206/// to another. We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700207void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList(
208 ilist_traits<OperationInst> &otherList, instr_iterator first,
209 instr_iterator last) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700210 // If we are transferring instructions within the same basic block, the block
211 // pointer doesn't need to be updated.
212 BasicBlock *curParent = getContainingBlock();
213 if (curParent == otherList.getContainingBlock())
214 return;
215
216 // Update the 'block' member of each instruction.
217 for (; first != last; ++first)
218 first->block = curParent;
219}
220
221/// Unlink this instruction from its BasicBlock and delete it.
222void OperationInst::eraseFromBlock() {
223 assert(getBlock() && "Instruction has no parent");
224 getBlock()->getOperations().erase(this);
225}
226
Chris Lattnered65a732018-06-28 20:45:33 -0700227//===----------------------------------------------------------------------===//
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700228// TerminatorInst
Chris Lattnered65a732018-06-28 20:45:33 -0700229//===----------------------------------------------------------------------===//
230
Chris Lattner3a467cc2018-07-01 20:28:00 -0700231/// Remove this terminator from its BasicBlock and delete it.
232void TerminatorInst::eraseFromBlock() {
233 assert(getBlock() && "Instruction has no parent");
234 getBlock()->setTerminator(nullptr);
Chris Lattner6119d382018-07-20 18:41:34 -0700235 destroy();
Chris Lattner4c95a502018-06-23 16:03:42 -0700236}
237
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700238/// Return the list of destination entries that this terminator branches to.
Chris Lattner548cd7f2018-07-26 11:30:28 -0700239MutableArrayRef<BasicBlockOperand> TerminatorInst::getBasicBlockOperands() {
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700240 switch (getKind()) {
241 case Kind::Operation:
MLIR Team6f8692f2018-07-26 09:23:10 -0700242 llvm_unreachable("not a terminator");
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700243 case Kind::Branch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700244 return cast<BranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700245 case Kind::CondBranch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700246 return cast<CondBranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700247 case Kind::Return:
248 // Return has no basic block successors.
249 return {};
250 }
251}
252
253//===----------------------------------------------------------------------===//
254// ReturnInst
255//===----------------------------------------------------------------------===//
256
Chris Lattner40746442018-07-21 14:32:09 -0700257/// Create a new OperationInst with the specific fields.
258ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) {
259 auto byteSize = totalSizeToAlloc<InstOperand>(operands.size());
260 void *rawMem = malloc(byteSize);
Chris Lattner3a467cc2018-07-01 20:28:00 -0700261
Chris Lattner40746442018-07-21 14:32:09 -0700262 // Initialize the ReturnInst part of the instruction.
263 auto inst = ::new (rawMem) ReturnInst(operands.size());
264
265 // Initialize the operands and results.
266 auto instOperands = inst->getInstOperands();
267 for (unsigned i = 0, e = operands.size(); i != e; ++i)
268 new (&instOperands[i]) InstOperand(inst, operands[i]);
269 return inst;
270}
271
272void ReturnInst::destroy() {
273 this->~ReturnInst();
274 free(this);
275}
276
277ReturnInst::~ReturnInst() {
278 // Explicitly run the destructors for the operands.
279 for (auto &operand : getInstOperands())
280 operand.~InstOperand();
281}
Chris Lattner1604e472018-07-23 08:42:19 -0700282
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700283//===----------------------------------------------------------------------===//
284// BranchInst
285//===----------------------------------------------------------------------===//
286
287BranchInst::BranchInst(BasicBlock *dest)
288 : TerminatorInst(Kind::Branch), dest(this, dest) {}
289
290void BranchInst::setDest(BasicBlock *block) { dest.set(block); }
291
Chris Lattner1604e472018-07-23 08:42:19 -0700292/// Add one value to the operand list.
293void BranchInst::addOperand(CFGValue *value) {
294 operands.emplace_back(InstOperand(this, value));
295}
296
297/// Add a list of values to the operand list.
298void BranchInst::addOperands(ArrayRef<CFGValue *> values) {
299 operands.reserve(operands.size() + values.size());
300 for (auto *value : values)
301 addOperand(value);
302}
James Molloy4f788372018-07-24 15:01:27 -0700303
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700304//===----------------------------------------------------------------------===//
305// CondBranchInst
306//===----------------------------------------------------------------------===//
307
308CondBranchInst::CondBranchInst(CFGValue *condition, BasicBlock *trueDest,
309 BasicBlock *falseDest)
310 : TerminatorInst(Kind::CondBranch),
311 condition(condition), dests{{this}, {this}}, numTrueOperands(0) {
312 dests[falseIndex].set(falseDest);
313 dests[trueIndex].set(trueDest);
314}
315
James Molloy4f788372018-07-24 15:01:27 -0700316/// Add one value to the true operand list.
317void CondBranchInst::addTrueOperand(CFGValue *value) {
318 assert(getNumFalseOperands() == 0 &&
319 "Must insert all true operands before false operands!");
320 operands.emplace_back(InstOperand(this, value));
321 ++numTrueOperands;
322}
323
324/// Add a list of values to the true operand list.
325void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) {
326 operands.reserve(operands.size() + values.size());
327 for (auto *value : values)
328 addTrueOperand(value);
329}
330
331/// Add one value to the false operand list.
332void CondBranchInst::addFalseOperand(CFGValue *value) {
333 operands.emplace_back(InstOperand(this, value));
334}
335
336/// Add a list of values to the false operand list.
337void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) {
338 operands.reserve(operands.size() + values.size());
339 for (auto *value : values)
340 addFalseOperand(value);
341}