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 | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 20 | #include "mlir/IR/MLIRContext.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 21 | using namespace mlir; |
| 22 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 23 | /// Replace all uses of 'this' value with the new value, updating anything in |
| 24 | /// the IR that uses 'this' to use the other value instead. When this returns |
| 25 | /// there are zero uses of 'this'. |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 26 | void IRObjectWithUseList::replaceAllUsesWith(IRObjectWithUseList *newValue) { |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 27 | assert(this != newValue && "cannot RAUW a value with itself"); |
| 28 | while (!use_empty()) { |
| 29 | use_begin()->set(newValue); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// Return the result number of this result. |
| 34 | unsigned InstResult::getResultNumber() const { |
| 35 | // Results are always stored consecutively, so use pointer subtraction to |
| 36 | // figure out what number this is. |
| 37 | return this - &getOwner()->getInstResults()[0]; |
| 38 | } |
| 39 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Instruction |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 44 | // Instructions are deleted through the destroy() member because we don't have |
| 45 | // a virtual destructor. |
| 46 | Instruction::~Instruction() { |
| 47 | assert(block == nullptr && "instruction destroyed but still in a block"); |
| 48 | } |
| 49 | |
| 50 | /// Destroy this instruction or one of its subclasses. |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 51 | void Instruction::destroy() { |
| 52 | switch (getKind()) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 53 | case Kind::Operation: |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 54 | cast<OperationInst>(this)->destroy(); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 55 | break; |
| 56 | case Kind::Branch: |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 57 | delete cast<BranchInst>(this); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 58 | break; |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 59 | case Kind::CondBranch: |
| 60 | delete cast<CondBranchInst>(this); |
| 61 | break; |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 62 | case Kind::Return: |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 63 | cast<ReturnInst>(this)->destroy(); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 68 | void OperationInst::destroy() { |
| 69 | this->~OperationInst(); |
| 70 | free(this); |
| 71 | } |
| 72 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 73 | /// Return the context this operation is associated with. |
| 74 | MLIRContext *Instruction::getContext() const { |
Tatiana Shpeisman | c335d18 | 2018-08-03 11:12:34 -0700 | [diff] [blame] | 75 | auto *fn = getFunction(); |
| 76 | return fn ? fn->getContext() : nullptr; |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 79 | CFGFunction *Instruction::getFunction() const { |
Tatiana Shpeisman | c335d18 | 2018-08-03 11:12:34 -0700 | [diff] [blame] | 80 | auto *block = getBlock(); |
| 81 | return block ? block->getFunction() : nullptr; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 84 | unsigned Instruction::getNumOperands() const { |
| 85 | switch (getKind()) { |
| 86 | case Kind::Operation: |
| 87 | return cast<OperationInst>(this)->getNumOperands(); |
| 88 | case Kind::Branch: |
| 89 | return cast<BranchInst>(this)->getNumOperands(); |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 90 | case Kind::CondBranch: |
| 91 | return cast<CondBranchInst>(this)->getNumOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 92 | case Kind::Return: |
| 93 | return cast<ReturnInst>(this)->getNumOperands(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | MutableArrayRef<InstOperand> Instruction::getInstOperands() { |
| 98 | switch (getKind()) { |
| 99 | case Kind::Operation: |
| 100 | return cast<OperationInst>(this)->getInstOperands(); |
| 101 | case Kind::Branch: |
| 102 | return cast<BranchInst>(this)->getInstOperands(); |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 103 | case Kind::CondBranch: |
| 104 | return cast<CondBranchInst>(this)->getInstOperands(); |
Chris Lattner | 68a3fd0 | 2018-07-23 10:08:00 -0700 | [diff] [blame] | 105 | case Kind::Return: |
| 106 | return cast<ReturnInst>(this)->getInstOperands(); |
| 107 | } |
| 108 | } |
| 109 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 110 | /// This drops all operand uses from this instruction, which is an essential |
| 111 | /// step in breaking cyclic dependences between references when they are to |
| 112 | /// be deleted. |
| 113 | void Instruction::dropAllReferences() { |
| 114 | for (auto &op : getInstOperands()) |
| 115 | op.drop(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 116 | |
| 117 | if (auto *term = dyn_cast<TerminatorInst>(this)) |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 118 | for (auto &dest : term->getBasicBlockOperands()) |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 119 | dest.drop(); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 122 | /// Emit a note about this instruction, reporting up to any diagnostic |
| 123 | /// handlers that may be listening. |
| 124 | void Instruction::emitNote(const Twine &message) const { |
| 125 | getContext()->emitDiagnostic(getLoc(), message, |
| 126 | MLIRContext::DiagnosticKind::Note); |
| 127 | } |
| 128 | |
| 129 | /// Emit a warning about this operation, reporting up to any diagnostic |
| 130 | /// handlers that may be listening. |
| 131 | void Instruction::emitWarning(const Twine &message) const { |
| 132 | getContext()->emitDiagnostic(getLoc(), message, |
| 133 | MLIRContext::DiagnosticKind::Warning); |
| 134 | } |
| 135 | |
| 136 | /// Emit an error about fatal conditions with this instruction, reporting up to |
| 137 | /// any diagnostic handlers that may be listening. NOTE: This may terminate |
| 138 | /// the containing application, only use when the IR is in an inconsistent |
| 139 | /// state. |
| 140 | void Instruction::emitError(const Twine &message) const { |
| 141 | getContext()->emitDiagnostic(getLoc(), message, |
| 142 | MLIRContext::DiagnosticKind::Error); |
| 143 | } |
| 144 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
| 146 | // OperationInst |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 149 | /// Create a new OperationInst with the specified fields. |
| 150 | OperationInst *OperationInst::create(Location *location, Identifier name, |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 151 | ArrayRef<CFGValue *> operands, |
| 152 | ArrayRef<Type *> resultTypes, |
| 153 | ArrayRef<NamedAttribute> attributes, |
| 154 | MLIRContext *context) { |
| 155 | auto byteSize = totalSizeToAlloc<InstOperand, InstResult>(operands.size(), |
| 156 | resultTypes.size()); |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 157 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 158 | |
| 159 | // Initialize the OperationInst part of the instruction. |
| 160 | auto inst = ::new (rawMem) OperationInst( |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 161 | location, name, operands.size(), resultTypes.size(), attributes, context); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 162 | |
| 163 | // Initialize the operands and results. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 164 | auto instOperands = inst->getInstOperands(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 165 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 166 | new (&instOperands[i]) InstOperand(inst, operands[i]); |
| 167 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 168 | auto instResults = inst->getInstResults(); |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 169 | for (unsigned i = 0, e = resultTypes.size(); i != e; ++i) |
| 170 | new (&instResults[i]) InstResult(resultTypes[i], inst); |
| 171 | return inst; |
| 172 | } |
| 173 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 174 | OperationInst *OperationInst::clone() const { |
| 175 | SmallVector<CFGValue *, 8> operands; |
| 176 | SmallVector<Type *, 8> resultTypes; |
| 177 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 178 | // Put together the operands and results. |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 179 | for (auto *operand : getOperands()) |
| 180 | operands.push_back(const_cast<CFGValue *>(operand)); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 181 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 182 | for (auto *result : getResults()) |
| 183 | resultTypes.push_back(result->getType()); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 184 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 185 | return create(getLoc(), getName(), operands, resultTypes, getAttrs(), |
| 186 | getContext()); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 189 | OperationInst::OperationInst(Location *location, Identifier name, |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 190 | unsigned numOperands, unsigned numResults, |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 191 | ArrayRef<NamedAttribute> attributes, |
| 192 | MLIRContext *context) |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 193 | : Operation(/*isInstruction=*/true, name, attributes, context), |
| 194 | Instruction(Kind::Operation, location), numOperands(numOperands), |
Chris Lattner | 55315d5 | 2018-07-18 19:06:45 -0700 | [diff] [blame] | 195 | numResults(numResults) {} |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 196 | |
| 197 | OperationInst::~OperationInst() { |
| 198 | // Explicitly run the destructors for the operands and results. |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 199 | for (auto &operand : getInstOperands()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 200 | operand.~InstOperand(); |
| 201 | |
Chris Lattner | f8cce87 | 2018-07-20 09:28:54 -0700 | [diff] [blame] | 202 | for (auto &result : getInstResults()) |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 203 | result.~InstResult(); |
| 204 | } |
| 205 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 206 | mlir::BasicBlock * |
| 207 | llvm::ilist_traits<::mlir::OperationInst>::getContainingBlock() { |
| 208 | size_t Offset( |
| 209 | size_t(&((BasicBlock *)nullptr->*BasicBlock::getSublistAccess(nullptr)))); |
| 210 | iplist<OperationInst> *Anchor(static_cast<iplist<OperationInst> *>(this)); |
| 211 | return reinterpret_cast<BasicBlock *>(reinterpret_cast<char *>(Anchor) - |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 212 | Offset); |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 215 | /// This is a trait method invoked when an instruction is added to a block. We |
| 216 | /// keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 217 | void llvm::ilist_traits<::mlir::OperationInst>::addNodeToList( |
| 218 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 219 | assert(!inst->getBlock() && "already in a basic block!"); |
| 220 | inst->block = getContainingBlock(); |
| 221 | } |
| 222 | |
| 223 | /// This is a trait method invoked when an instruction is removed from a block. |
| 224 | /// We keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 225 | void llvm::ilist_traits<::mlir::OperationInst>::removeNodeFromList( |
| 226 | OperationInst *inst) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 227 | assert(inst->block && "not already in a basic block!"); |
| 228 | inst->block = nullptr; |
| 229 | } |
| 230 | |
| 231 | /// This is a trait method invoked when an instruction is moved from one block |
| 232 | /// to another. We keep the block pointer up to date. |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 233 | void llvm::ilist_traits<::mlir::OperationInst>::transferNodesFromList( |
| 234 | ilist_traits<OperationInst> &otherList, instr_iterator first, |
| 235 | instr_iterator last) { |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 236 | // If we are transferring instructions within the same basic block, the block |
| 237 | // pointer doesn't need to be updated. |
| 238 | BasicBlock *curParent = getContainingBlock(); |
| 239 | if (curParent == otherList.getContainingBlock()) |
| 240 | return; |
| 241 | |
| 242 | // Update the 'block' member of each instruction. |
| 243 | for (; first != last; ++first) |
| 244 | first->block = curParent; |
| 245 | } |
| 246 | |
| 247 | /// Unlink this instruction from its BasicBlock and delete it. |
| 248 | void OperationInst::eraseFromBlock() { |
| 249 | assert(getBlock() && "Instruction has no parent"); |
| 250 | getBlock()->getOperations().erase(this); |
| 251 | } |
| 252 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 254 | // TerminatorInst |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
| 256 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 257 | /// Remove this terminator from its BasicBlock and delete it. |
| 258 | void TerminatorInst::eraseFromBlock() { |
| 259 | assert(getBlock() && "Instruction has no parent"); |
| 260 | getBlock()->setTerminator(nullptr); |
Chris Lattner | 6119d38 | 2018-07-20 18:41:34 -0700 | [diff] [blame] | 261 | destroy(); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 264 | /// Return the list of destination entries that this terminator branches to. |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 265 | MutableArrayRef<BasicBlockOperand> TerminatorInst::getBasicBlockOperands() { |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 266 | switch (getKind()) { |
| 267 | case Kind::Operation: |
MLIR Team | 6f8692f | 2018-07-26 09:23:10 -0700 | [diff] [blame] | 268 | llvm_unreachable("not a terminator"); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 269 | case Kind::Branch: |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 270 | return cast<BranchInst>(this)->getBasicBlockOperands(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 271 | case Kind::CondBranch: |
Chris Lattner | 548cd7f | 2018-07-26 11:30:28 -0700 | [diff] [blame] | 272 | return cast<CondBranchInst>(this)->getBasicBlockOperands(); |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 273 | case Kind::Return: |
| 274 | // Return has no basic block successors. |
| 275 | return {}; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | // ReturnInst |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 283 | /// Create a new OperationInst with the specific fields. |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 284 | ReturnInst *ReturnInst::create(Location *location, |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 285 | ArrayRef<CFGValue *> operands) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 286 | auto byteSize = totalSizeToAlloc<InstOperand>(operands.size()); |
| 287 | void *rawMem = malloc(byteSize); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 288 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 289 | // Initialize the ReturnInst part of the instruction. |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 290 | auto inst = ::new (rawMem) ReturnInst(location, operands.size()); |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 291 | |
| 292 | // Initialize the operands and results. |
| 293 | auto instOperands = inst->getInstOperands(); |
| 294 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 295 | new (&instOperands[i]) InstOperand(inst, operands[i]); |
| 296 | return inst; |
| 297 | } |
| 298 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 299 | ReturnInst::ReturnInst(Location *location, unsigned numOperands) |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 300 | : TerminatorInst(Kind::Return, location), numOperands(numOperands) {} |
| 301 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 302 | void ReturnInst::destroy() { |
| 303 | this->~ReturnInst(); |
| 304 | free(this); |
| 305 | } |
| 306 | |
| 307 | ReturnInst::~ReturnInst() { |
| 308 | // Explicitly run the destructors for the operands. |
| 309 | for (auto &operand : getInstOperands()) |
| 310 | operand.~InstOperand(); |
| 311 | } |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 312 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 313 | //===----------------------------------------------------------------------===// |
| 314 | // BranchInst |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 317 | BranchInst::BranchInst(Location *location, BasicBlock *dest, |
Chris Lattner | 8a9310a | 2018-08-24 21:13:19 -0700 | [diff] [blame] | 318 | ArrayRef<CFGValue *> operands) |
| 319 | : TerminatorInst(Kind::Branch, location), dest(this, dest) { |
| 320 | addOperands(operands); |
| 321 | } |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 322 | |
| 323 | void BranchInst::setDest(BasicBlock *block) { dest.set(block); } |
| 324 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 325 | /// Add one value to the operand list. |
| 326 | void BranchInst::addOperand(CFGValue *value) { |
| 327 | operands.emplace_back(InstOperand(this, value)); |
| 328 | } |
| 329 | |
| 330 | /// Add a list of values to the operand list. |
| 331 | void BranchInst::addOperands(ArrayRef<CFGValue *> values) { |
| 332 | operands.reserve(operands.size() + values.size()); |
| 333 | for (auto *value : values) |
| 334 | addOperand(value); |
| 335 | } |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 336 | |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 337 | //===----------------------------------------------------------------------===// |
| 338 | // CondBranchInst |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 341 | CondBranchInst::CondBranchInst(Location *location, CFGValue *condition, |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 342 | BasicBlock *trueDest, BasicBlock *falseDest) |
| 343 | : TerminatorInst(Kind::CondBranch, location), |
Chris Lattner | 1e9fd7f | 2018-07-26 08:56:26 -0700 | [diff] [blame] | 344 | condition(condition), dests{{this}, {this}}, numTrueOperands(0) { |
| 345 | dests[falseIndex].set(falseDest); |
| 346 | dests[trueIndex].set(trueDest); |
| 347 | } |
| 348 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 349 | /// Add one value to the true operand list. |
| 350 | void CondBranchInst::addTrueOperand(CFGValue *value) { |
| 351 | assert(getNumFalseOperands() == 0 && |
| 352 | "Must insert all true operands before false operands!"); |
| 353 | operands.emplace_back(InstOperand(this, value)); |
| 354 | ++numTrueOperands; |
| 355 | } |
| 356 | |
| 357 | /// Add a list of values to the true operand list. |
| 358 | void CondBranchInst::addTrueOperands(ArrayRef<CFGValue *> values) { |
| 359 | operands.reserve(operands.size() + values.size()); |
| 360 | for (auto *value : values) |
| 361 | addTrueOperand(value); |
| 362 | } |
| 363 | |
| 364 | /// Add one value to the false operand list. |
| 365 | void CondBranchInst::addFalseOperand(CFGValue *value) { |
| 366 | operands.emplace_back(InstOperand(this, value)); |
| 367 | } |
| 368 | |
| 369 | /// Add a list of values to the false operand list. |
| 370 | void CondBranchInst::addFalseOperands(ArrayRef<CFGValue *> values) { |
| 371 | operands.reserve(operands.size() + values.size()); |
| 372 | for (auto *value : values) |
| 373 | addFalseOperand(value); |
| 374 | } |