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" |
| 19 | #include "mlir/IR/BasicBlock.h" |
| 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'. |
| 25 | void 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. |
| 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 | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 72 | CFGFunction *Instruction::getFunction() const { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 73 | return getBlock()->getFunction(); |
| 74 | } |
| 75 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 76 | unsigned 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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 82 | case Kind::CondBranch: |
| 83 | return cast<CondBranchInst>(this)->getNumOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 84 | case Kind::Return: |
| 85 | return cast<ReturnInst>(this)->getNumOperands(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | MutableArrayRef<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 Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 95 | case Kind::CondBranch: |
| 96 | return cast<CondBranchInst>(this)->getInstOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 97 | case Kind::Return: |
| 98 | return cast<ReturnInst>(this)->getInstOperands(); |
| 99 | } |
| 100 | } |
| 101 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 102 | /// 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. |
| 105 | void Instruction::dropAllReferences() { |
| 106 | for (auto &op : getInstOperands()) |
| 107 | op.drop(); |
| 108 | } |
| 109 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
| 111 | // OperationInst |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 114 | /// Create a new OperationInst with the specific fields. |
| 115 | OperationInst *OperationInst::create(Identifier name, |
| 116 | ArrayRef<CFGValue *> operands, |
| 117 | ArrayRef<Type *> resultTypes, |
| 118 | ArrayRef<NamedAttribute> attributes, |
| 119 | MLIRContext *context) { |
| 120 | auto byteSize = totalSizeToAlloc<InstOperand, InstResult>(operands.size(), |
| 121 | resultTypes.size()); |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 122 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 123 | |
| 124 | // Initialize the OperationInst part of the instruction. |
| 125 | auto inst = ::new (rawMem) OperationInst( |
| 126 | name, operands.size(), resultTypes.size(), attributes, context); |
| 127 | |
| 128 | // Initialize the operands and results. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 129 | auto instOperands = inst->getInstOperands(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 130 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 131 | new (&instOperands[i]) InstOperand(inst, operands[i]); |
| 132 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 133 | auto instResults = inst->getInstResults(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 134 | for (unsigned i = 0, e = resultTypes.size(); i != e; ++i) |
| 135 | new (&instResults[i]) InstResult(resultTypes[i], inst); |
| 136 | return inst; |
| 137 | } |
| 138 | |
| 139 | OperationInst::OperationInst(Identifier name, unsigned numOperands, |
| 140 | unsigned numResults, |
| 141 | ArrayRef<NamedAttribute> attributes, |
| 142 | MLIRContext *context) |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 143 | : Operation(name, /*isInstruction=*/true, attributes, context), |
Chris Lattner | 55315d5 | 2018-07-18 19:06:45 -0700 | [diff] [blame] | 144 | Instruction(Kind::Operation), numOperands(numOperands), |
| 145 | numResults(numResults) {} |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 146 | |
| 147 | OperationInst::~OperationInst() { |
| 148 | // Explicitly run the destructors for the operands and results. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 149 | for (auto &operand : getInstOperands()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 150 | operand.~InstOperand(); |
| 151 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 152 | for (auto &result : getInstResults()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 153 | result.~InstResult(); |
| 154 | } |
| 155 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 156 | mlir::BasicBlock * |
| 157 | llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() { |
| 158 | size_t Offset( |
| 159 | size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr)))); |
| 160 | iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this)); |
| 161 | return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) - |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 162 | Offset); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 165 | /// This is a trait method invoked when an instruction is added to a block. We |
| 166 | /// keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 167 | void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList( |
| 168 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 169 | assert(!inst->getBlock() && "already in a basic block!"); |
| 170 | inst->block = getContainingBlock(); |
| 171 | } |
| 172 | |
| 173 | /// This is a trait method invoked when an instruction is removed from a block. |
| 174 | /// We keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 175 | void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList( |
| 176 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 177 | assert(inst->block && "not already in a basic block!"); |
| 178 | inst->block = nullptr; |
| 179 | } |
| 180 | |
| 181 | /// This is a trait method invoked when an instruction is moved from one block |
| 182 | /// to another. We keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 183 | void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList( |
| 184 | ilist_traits<OperationInst> &otherList, instr_iterator first, |
| 185 | instr_iterator last) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 186 | // If we are transferring instructions within the same basic block, the block |
| 187 | // pointer doesn't need to be updated. |
| 188 | BasicBlock *curParent = getContainingBlock(); |
| 189 | if (curParent == otherList.getContainingBlock()) |
| 190 | return; |
| 191 | |
| 192 | // Update the 'block' member of each instruction. |
| 193 | for (; first != last; ++first) |
| 194 | first->block = curParent; |
| 195 | } |
| 196 | |
| 197 | /// Unlink this instruction from its BasicBlock and delete it. |
| 198 | void OperationInst::eraseFromBlock() { |
| 199 | assert(getBlock() && "Instruction has no parent"); |
| 200 | getBlock()->getOperations().erase(this); |
| 201 | } |
| 202 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 203 | /// If this value is the result of an OperationInst, return the instruction |
| 204 | /// that defines it. |
| 205 | OperationInst *SSAValue::getDefiningInst() { |
| 206 | if (auto *result = dyn_cast<InstResult>(this)) |
| 207 | return result->getOwner(); |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | // Terminators |
| 213 | //===----------------------------------------------------------------------===// |
| 214 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 215 | /// Remove this terminator from its BasicBlock and delete it. |
| 216 | void TerminatorInst::eraseFromBlock() { |
| 217 | assert(getBlock() && "Instruction has no parent"); |
| 218 | getBlock()->setTerminator(nullptr); |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 219 | destroy(); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 222 | /// Create a new OperationInst with the specific fields. |
| 223 | ReturnInst *ReturnInst::create(ArrayRef<CFGValue *> operands) { |
| 224 | auto byteSize = totalSizeToAlloc<InstOperand>(operands.size()); |
| 225 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 226 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 227 | // Initialize the ReturnInst part of the instruction. |
| 228 | auto inst = ::new (rawMem) ReturnInst(operands.size()); |
| 229 | |
| 230 | // Initialize the operands and results. |
| 231 | auto instOperands = inst->getInstOperands(); |
| 232 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 233 | new (&instOperands[i]) InstOperand(inst, operands[i]); |
| 234 | return inst; |
| 235 | } |
| 236 | |
| 237 | void ReturnInst::destroy() { |
| 238 | this->~ReturnInst(); |
| 239 | free(this); |
| 240 | } |
| 241 | |
| 242 | ReturnInst::~ReturnInst() { |
| 243 | // Explicitly run the destructors for the operands. |
| 244 | for (auto &operand : getInstOperands()) |
| 245 | operand.~InstOperand(); |
| 246 | } |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 247 | |
| 248 | /// Add one value to the operand list. |
| 249 | void BranchInst::addOperand(CFGValue *value) { |
| 250 | operands.emplace_back(InstOperand(this, value)); |
| 251 | } |
| 252 | |
| 253 | /// Add a list of values to the operand list. |
| 254 | void BranchInst::addOperands(ArrayRef<CFGValue *> values) { |
| 255 | operands.reserve(operands.size() + values.size()); |
| 256 | for (auto *value : values) |
| 257 | addOperand(value); |
| 258 | } |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 259 | |
| 260 | /// Add one value to the true operand list. |
| 261 | void CondBranchInst::addTrueOperand(CFGValue *value) { |
| 262 | assert(getNumFalseOperands() == 0 && |
| 263 | "Must insert all true operands before false operands!"); |
| 264 | operands.emplace_back(InstOperand(this, value)); |
| 265 | ++numTrueOperands; |
| 266 | } |
| 267 | |
| 268 | /// Add a list of values to the true operand list. |
| 269 | void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) { |
| 270 | operands.reserve(operands.size() + values.size()); |
| 271 | for (auto *value : values) |
| 272 | addTrueOperand(value); |
| 273 | } |
| 274 | |
| 275 | /// Add one value to the false operand list. |
| 276 | void CondBranchInst::addFalseOperand(CFGValue *value) { |
| 277 | operands.emplace_back(InstOperand(this, value)); |
| 278 | } |
| 279 | |
| 280 | /// Add a list of values to the false operand list. |
| 281 | void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) { |
| 282 | operands.reserve(operands.size() + values.size()); |
| 283 | for (auto *value : values) |
| 284 | addFalseOperand(value); |
| 285 | } |