Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 1 | //===- 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 Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 19 | #include "mlir/IR/CFGFunction.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 20 | using namespace mlir; |
| 21 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 22 | /// 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 Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 25 | void IRObjectWithUseList::replaceAllUsesWith(IRObjectWithUseList *newValue) { |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 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. |
| 33 | unsigned 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 Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // Instruction |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 43 | // Instructions are deleted through the destroy() member because we don't have |
| 44 | // a virtual destructor. |
| 45 | Instruction::~Instruction() { |
| 46 | assert(block == nullptr && "instruction destroyed but still in a block"); |
| 47 | } |
| 48 | |
| 49 | /// Destroy this instruction or one of its subclasses. |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 50 | void Instruction::destroy() { |
| 51 | switch (getKind()) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 52 | case Kind::Operation: |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 53 | cast<OperationInst>(this)->destroy(); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 54 | break; |
| 55 | case Kind::Branch: |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 56 | delete cast<BranchInst>(this); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 57 | break; |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 58 | case Kind::CondBranch: |
| 59 | delete cast<CondBranchInst>(this); |
| 60 | break; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 61 | case Kind::Return: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 62 | cast<ReturnInst>(this)->destroy(); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 67 | void OperationInst::destroy() { |
| 68 | this->~OperationInst(); |
| 69 | free(this); |
| 70 | } |
| 71 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 72 | /// Return the context this operation is associated with. |
| 73 | MLIRContext *Instruction::getContext() const { |
| 74 | return getFunction()->getContext(); |
| 75 | } |
| 76 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 77 | CFGFunction *Instruction::getFunction() const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 78 | return getBlock()->getFunction(); |
| 79 | } |
| 80 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 81 | unsigned 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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 87 | case Kind::CondBranch: |
| 88 | return cast<CondBranchInst>(this)->getNumOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 89 | case Kind::Return: |
| 90 | return cast<ReturnInst>(this)->getNumOperands(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | MutableArrayRef<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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 100 | case Kind::CondBranch: |
| 101 | return cast<CondBranchInst>(this)->getInstOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 102 | case Kind::Return: |
| 103 | return cast<ReturnInst>(this)->getInstOperands(); |
| 104 | } |
| 105 | } |
| 106 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 107 | /// 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. |
| 110 | void Instruction::dropAllReferences() { |
| 111 | for (auto &op : getInstOperands()) |
| 112 | op.drop(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 113 | |
| 114 | if (auto *term = dyn_cast<TerminatorInst>(this)) |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 115 | for (auto &dest : term->getBasicBlockOperands()) |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 116 | dest.drop(); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
| 120 | // OperationInst |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 123 | /// Create a new OperationInst with the specific fields. |
| 124 | OperationInst *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 Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 131 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 132 | |
| 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 Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 138 | auto instOperands = inst->getInstOperands(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 139 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 140 | new (&instOperands[i]) InstOperand(inst, operands[i]); |
| 141 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 142 | auto instResults = inst->getInstResults(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 143 | for (unsigned i = 0, e = resultTypes.size(); i != e; ++i) |
| 144 | new (&instResults[i]) InstResult(resultTypes[i], inst); |
| 145 | return inst; |
| 146 | } |
| 147 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 148 | OperationInst *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 Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 163 | OperationInst::OperationInst(Identifier name, unsigned numOperands, |
| 164 | unsigned numResults, |
| 165 | ArrayRef<NamedAttribute> attributes, |
| 166 | MLIRContext *context) |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 167 | : Operation(name, /*isInstruction=*/true, attributes, context), |
Chris Lattner | 55315d5 | 2018-07-18 19:06:45 -0700 | [diff] [blame] | 168 | Instruction(Kind::Operation), numOperands(numOperands), |
| 169 | numResults(numResults) {} |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 170 | |
| 171 | OperationInst::~OperationInst() { |
| 172 | // Explicitly run the destructors for the operands and results. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 173 | for (auto &operand : getInstOperands()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 174 | operand.~InstOperand(); |
| 175 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 176 | for (auto &result : getInstResults()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 177 | result.~InstResult(); |
| 178 | } |
| 179 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 180 | mlir::BasicBlock * |
| 181 | llvm::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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 186 | Offset); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 189 | /// This is a trait method invoked when an instruction is added to a block. We |
| 190 | /// keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 191 | void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList( |
| 192 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 193 | 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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 199 | void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList( |
| 200 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 201 | 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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 207 | void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList( |
| 208 | ilist_traits<OperationInst> &otherList, instr_iterator first, |
| 209 | instr_iterator last) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 210 | // 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. |
| 222 | void OperationInst::eraseFromBlock() { |
| 223 | assert(getBlock() && "Instruction has no parent"); |
| 224 | getBlock()->getOperations().erase(this); |
| 225 | } |
| 226 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 227 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 228 | // TerminatorInst |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 231 | /// Remove this terminator from its BasicBlock and delete it. |
| 232 | void TerminatorInst::eraseFromBlock() { |
| 233 | assert(getBlock() && "Instruction has no parent"); |
| 234 | getBlock()->setTerminator(nullptr); |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 235 | destroy(); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 238 | /// Return the list of destination entries that this terminator branches to. |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 239 | MutableArrayRef<BasicBlockOperand> TerminatorInst::getBasicBlockOperands() { |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 240 | switch (getKind()) { |
| 241 | case Kind::Operation: |
MLIR Team | 6f8692f | 2018-07-26 09:23:10 -0700 | [diff] [blame] | 242 | llvm_unreachable("not a terminator"); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 243 | case Kind::Branch: |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 244 | return cast<BranchInst>(this)->getBasicBlockOperands(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 245 | case Kind::CondBranch: |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 246 | return cast<CondBranchInst>(this)->getBasicBlockOperands(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 247 | case Kind::Return: |
| 248 | // Return has no basic block successors. |
| 249 | return {}; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | // ReturnInst |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 257 | /// Create a new OperationInst with the specific fields. |
| 258 | ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) { |
| 259 | auto byteSize = totalSizeToAlloc<InstOperand>(operands.size()); |
| 260 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 261 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 262 | // 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 | |
| 272 | void ReturnInst::destroy() { |
| 273 | this->~ReturnInst(); |
| 274 | free(this); |
| 275 | } |
| 276 | |
| 277 | ReturnInst::~ReturnInst() { |
| 278 | // Explicitly run the destructors for the operands. |
| 279 | for (auto &operand : getInstOperands()) |
| 280 | operand.~InstOperand(); |
| 281 | } |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 282 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 283 | //===----------------------------------------------------------------------===// |
| 284 | // BranchInst |
| 285 | //===----------------------------------------------------------------------===// |
| 286 | |
| 287 | BranchInst::BranchInst(BasicBlock *dest) |
| 288 | : TerminatorInst(Kind::Branch), dest(this, dest) {} |
| 289 | |
| 290 | void BranchInst::setDest(BasicBlock *block) { dest.set(block); } |
| 291 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 292 | /// Add one value to the operand list. |
| 293 | void BranchInst::addOperand(CFGValue *value) { |
| 294 | operands.emplace_back(InstOperand(this, value)); |
| 295 | } |
| 296 | |
| 297 | /// Add a list of values to the operand list. |
| 298 | void BranchInst::addOperands(ArrayRef<CFGValue *> values) { |
| 299 | operands.reserve(operands.size() + values.size()); |
| 300 | for (auto *value : values) |
| 301 | addOperand(value); |
| 302 | } |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 303 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 304 | //===----------------------------------------------------------------------===// |
| 305 | // CondBranchInst |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | CondBranchInst::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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 316 | /// Add one value to the true operand list. |
| 317 | void 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. |
| 325 | void 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. |
| 332 | void CondBranchInst::addFalseOperand(CFGValue *value) { |
| 333 | operands.emplace_back(InstOperand(this, value)); |
| 334 | } |
| 335 | |
| 336 | /// Add a list of values to the false operand list. |
| 337 | void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) { |
| 338 | operands.reserve(operands.size() + values.size()); |
| 339 | for (auto *value : values) |
| 340 | addFalseOperand(value); |
| 341 | } |