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