blob: c3f0b1ea717d5afcf4d0239a42f39ba1fa9d4f51 [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'.
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 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 Lattnera8e47672018-07-25 14:08:16 -0700102/// This drops all operand uses from this instruction, which is an essential
103/// step in breaking cyclic dependences between references when they are to
104/// be deleted.
105void Instruction::dropAllReferences() {
106 for (auto &op : getInstOperands())
107 op.drop();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700108
109 if (auto *term = dyn_cast<TerminatorInst>(this))
Chris Lattner548cd7f2018-07-26 11:30:28 -0700110 for (auto &dest : term->getBasicBlockOperands())
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700111 dest.drop();
Chris Lattnera8e47672018-07-25 14:08:16 -0700112}
113
Chris Lattnered65a732018-06-28 20:45:33 -0700114//===----------------------------------------------------------------------===//
115// OperationInst
116//===----------------------------------------------------------------------===//
117
Chris Lattner3b2ef762018-07-18 15:31:25 -0700118/// Create a new OperationInst with the specific fields.
119OperationInst *OperationInst::create(Identifier name,
120 ArrayRef<CFGValue *> operands,
121 ArrayRef<Type *> resultTypes,
122 ArrayRef<NamedAttribute> attributes,
123 MLIRContext *context) {
124 auto byteSize = totalSizeToAlloc<InstOperand, InstResult>(operands.size(),
125 resultTypes.size());
Chris Lattner6119d382018-07-20 18:41:34 -0700126 void *rawMem = malloc(byteSize);
Chris Lattner3b2ef762018-07-18 15:31:25 -0700127
128 // Initialize the OperationInst part of the instruction.
129 auto inst = ::new (rawMem) OperationInst(
130 name, operands.size(), resultTypes.size(), attributes, context);
131
132 // Initialize the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700133 auto instOperands = inst->getInstOperands();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700134 for (unsigned i = 0, e = operands.size(); i != e; ++i)
135 new (&instOperands[i]) InstOperand(inst, operands[i]);
136
Chris Lattnerf8cce872018-07-20 09:28:54 -0700137 auto instResults = inst->getInstResults();
Chris Lattner3b2ef762018-07-18 15:31:25 -0700138 for (unsigned i = 0, e = resultTypes.size(); i != e; ++i)
139 new (&instResults[i]) InstResult(resultTypes[i], inst);
140 return inst;
141}
142
143OperationInst::OperationInst(Identifier name, unsigned numOperands,
144 unsigned numResults,
145 ArrayRef<NamedAttribute> attributes,
146 MLIRContext *context)
James Molloy4f788372018-07-24 15:01:27 -0700147 : Operation(name, /*isInstruction=*/true, attributes, context),
Chris Lattner55315d52018-07-18 19:06:45 -0700148 Instruction(Kind::Operation), numOperands(numOperands),
149 numResults(numResults) {}
Chris Lattner3b2ef762018-07-18 15:31:25 -0700150
151OperationInst::~OperationInst() {
152 // Explicitly run the destructors for the operands and results.
Chris Lattnerf8cce872018-07-20 09:28:54 -0700153 for (auto &operand : getInstOperands())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700154 operand.~InstOperand();
155
Chris Lattnerf8cce872018-07-20 09:28:54 -0700156 for (auto &result : getInstResults())
Chris Lattner3b2ef762018-07-18 15:31:25 -0700157 result.~InstResult();
158}
159
Chris Lattner3a467cc2018-07-01 20:28:00 -0700160mlir::BasicBlock *
161llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() {
162 size_t Offset(
163 size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr))));
164 iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this));
165 return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) -
James Molloy4f788372018-07-24 15:01:27 -0700166 Offset);
Chris Lattnered65a732018-06-28 20:45:33 -0700167}
168
Chris Lattner3a467cc2018-07-01 20:28:00 -0700169/// This is a trait method invoked when an instruction is added to a block. We
170/// keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700171void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList(
172 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700173 assert(!inst->getBlock() && "already in a basic block!");
174 inst->block = getContainingBlock();
175}
176
177/// This is a trait method invoked when an instruction is removed from a block.
178/// We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700179void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList(
180 OperationInst *inst) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700181 assert(inst->block && "not already in a basic block!");
182 inst->block = nullptr;
183}
184
185/// This is a trait method invoked when an instruction is moved from one block
186/// to another. We keep the block pointer up to date.
James Molloy4f788372018-07-24 15:01:27 -0700187void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList(
188 ilist_traits<OperationInst> &otherList, instr_iterator first,
189 instr_iterator last) {
Chris Lattner3a467cc2018-07-01 20:28:00 -0700190 // If we are transferring instructions within the same basic block, the block
191 // pointer doesn't need to be updated.
192 BasicBlock *curParent = getContainingBlock();
193 if (curParent == otherList.getContainingBlock())
194 return;
195
196 // Update the 'block' member of each instruction.
197 for (; first != last; ++first)
198 first->block = curParent;
199}
200
201/// Unlink this instruction from its BasicBlock and delete it.
202void OperationInst::eraseFromBlock() {
203 assert(getBlock() && "Instruction has no parent");
204 getBlock()->getOperations().erase(this);
205}
206
Chris Lattner6119d382018-07-20 18:41:34 -0700207/// If this value is the result of an OperationInst, return the instruction
208/// that defines it.
209OperationInst *SSAValue::getDefiningInst() {
210 if (auto *result = dyn_cast<InstResult>(this))
211 return result->getOwner();
212 return nullptr;
213}
214
Chris Lattnered65a732018-06-28 20:45:33 -0700215//===----------------------------------------------------------------------===//
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700216// TerminatorInst
Chris Lattnered65a732018-06-28 20:45:33 -0700217//===----------------------------------------------------------------------===//
218
Chris Lattner3a467cc2018-07-01 20:28:00 -0700219/// Remove this terminator from its BasicBlock and delete it.
220void TerminatorInst::eraseFromBlock() {
221 assert(getBlock() && "Instruction has no parent");
222 getBlock()->setTerminator(nullptr);
Chris Lattner6119d382018-07-20 18:41:34 -0700223 destroy();
Chris Lattner4c95a502018-06-23 16:03:42 -0700224}
225
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700226/// Return the list of destination entries that this terminator branches to.
Chris Lattner548cd7f2018-07-26 11:30:28 -0700227MutableArrayRef<BasicBlockOperand> TerminatorInst::getBasicBlockOperands() {
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700228 switch (getKind()) {
229 case Kind::Operation:
MLIR Team6f8692f2018-07-26 09:23:10 -0700230 llvm_unreachable("not a terminator");
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700231 case Kind::Branch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700232 return cast<BranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700233 case Kind::CondBranch:
Chris Lattner548cd7f2018-07-26 11:30:28 -0700234 return cast<CondBranchInst>(this)->getBasicBlockOperands();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700235 case Kind::Return:
236 // Return has no basic block successors.
237 return {};
238 }
239}
240
241//===----------------------------------------------------------------------===//
242// ReturnInst
243//===----------------------------------------------------------------------===//
244
Chris Lattner40746442018-07-21 14:32:09 -0700245/// Create a new OperationInst with the specific fields.
246ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) {
247 auto byteSize = totalSizeToAlloc<InstOperand>(operands.size());
248 void *rawMem = malloc(byteSize);
Chris Lattner3a467cc2018-07-01 20:28:00 -0700249
Chris Lattner40746442018-07-21 14:32:09 -0700250 // Initialize the ReturnInst part of the instruction.
251 auto inst = ::new (rawMem) ReturnInst(operands.size());
252
253 // Initialize the operands and results.
254 auto instOperands = inst->getInstOperands();
255 for (unsigned i = 0, e = operands.size(); i != e; ++i)
256 new (&instOperands[i]) InstOperand(inst, operands[i]);
257 return inst;
258}
259
260void ReturnInst::destroy() {
261 this->~ReturnInst();
262 free(this);
263}
264
265ReturnInst::~ReturnInst() {
266 // Explicitly run the destructors for the operands.
267 for (auto &operand : getInstOperands())
268 operand.~InstOperand();
269}
Chris Lattner1604e472018-07-23 08:42:19 -0700270
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700271//===----------------------------------------------------------------------===//
272// BranchInst
273//===----------------------------------------------------------------------===//
274
275BranchInst::BranchInst(BasicBlock *dest)
276 : TerminatorInst(Kind::Branch), dest(this, dest) {}
277
278void BranchInst::setDest(BasicBlock *block) { dest.set(block); }
279
Chris Lattner1604e472018-07-23 08:42:19 -0700280/// Add one value to the operand list.
281void BranchInst::addOperand(CFGValue *value) {
282 operands.emplace_back(InstOperand(this, value));
283}
284
285/// Add a list of values to the operand list.
286void BranchInst::addOperands(ArrayRef<CFGValue *> values) {
287 operands.reserve(operands.size() + values.size());
288 for (auto *value : values)
289 addOperand(value);
290}
James Molloy4f788372018-07-24 15:01:27 -0700291
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700292//===----------------------------------------------------------------------===//
293// CondBranchInst
294//===----------------------------------------------------------------------===//
295
296CondBranchInst::CondBranchInst(CFGValue *condition, BasicBlock *trueDest,
297 BasicBlock *falseDest)
298 : TerminatorInst(Kind::CondBranch),
299 condition(condition), dests{{this}, {this}}, numTrueOperands(0) {
300 dests[falseIndex].set(falseDest);
301 dests[trueIndex].set(trueDest);
302}
303
James Molloy4f788372018-07-24 15:01:27 -0700304/// Add one value to the true operand list.
305void CondBranchInst::addTrueOperand(CFGValue *value) {
306 assert(getNumFalseOperands() == 0 &&
307 "Must insert all true operands before false operands!");
308 operands.emplace_back(InstOperand(this, value));
309 ++numTrueOperands;
310}
311
312/// Add a list of values to the true operand list.
313void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) {
314 operands.reserve(operands.size() + values.size());
315 for (auto *value : values)
316 addTrueOperand(value);
317}
318
319/// Add one value to the false operand list.
320void CondBranchInst::addFalseOperand(CFGValue *value) {
321 operands.emplace_back(InstOperand(this, value));
322}
323
324/// Add a list of values to the false operand list.
325void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) {
326 operands.reserve(operands.size() + values.size());
327 for (auto *value : values)
328 addFalseOperand(value);
329}