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