Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 1 | //===- BasicBlock.cpp - MLIR BasicBlock Class -----------------------------===// |
| 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/BasicBlock.h" |
Chris Lattner | f6d80a0 | 2018-06-24 11:18:29 -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 | |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 22 | BasicBlock::BasicBlock() {} |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 23 | |
| 24 | BasicBlock::~BasicBlock() { |
| 25 | if (terminator) |
| 26 | terminator->eraseFromBlock(); |
James Molloy | 61a656c | 2018-07-22 15:45:24 -0700 | [diff] [blame] | 27 | for (BBArgument *arg : arguments) |
| 28 | delete arg; |
| 29 | arguments.clear(); |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /// Unlink this BasicBlock from its CFGFunction and delete it. |
| 33 | void BasicBlock::eraseFromFunction() { |
| 34 | assert(getFunction() && "BasicBlock has no parent"); |
| 35 | getFunction()->getBlocks().erase(this); |
| 36 | } |
| 37 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // Argument list management. |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | BBArgument *BasicBlock::addArgument(Type *type) { |
| 43 | auto *arg = new BBArgument(type, this); |
| 44 | arguments.push_back(arg); |
| 45 | return arg; |
| 46 | } |
| 47 | |
| 48 | /// Add one argument to the argument list for each type specified in the list. |
| 49 | auto BasicBlock::addArguments(ArrayRef<Type *> types) |
| 50 | -> llvm::iterator_range<args_iterator> { |
| 51 | arguments.reserve(arguments.size() + types.size()); |
| 52 | auto initialSize = arguments.size(); |
| 53 | for (auto *type : types) { |
| 54 | addArgument(type); |
| 55 | } |
| 56 | return {arguments.data() + initialSize, arguments.data() + arguments.size()}; |
| 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // Terminator management |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 63 | void BasicBlock::setTerminator(TerminatorInst *inst) { |
| 64 | // If we already had a terminator, abandon it. |
| 65 | if (terminator) |
| 66 | terminator->block = nullptr; |
| 67 | |
| 68 | // Reset our terminator to the new instruction. |
| 69 | terminator = inst; |
| 70 | if (inst) |
| 71 | inst->block = this; |
| 72 | } |
| 73 | |
Chris Lattner | 25ce306 | 2018-07-27 11:10:12 -0700 | [diff] [blame^] | 74 | /// Return true if this block has no predecessors. |
| 75 | bool BasicBlock::hasNoPredecessors() const { |
| 76 | return pred_begin() == pred_end(); |
| 77 | } |
| 78 | |
| 79 | /// If this basic block has exactly one predecessor, return it. Otherwise, |
| 80 | /// return null. |
| 81 | /// |
| 82 | /// Note that multiple edges from a single block (e.g. if you have a cond |
| 83 | /// branch with the same block as the true/false destinations) is not |
| 84 | /// considered to be a single predecessor. |
| 85 | BasicBlock *BasicBlock::getSinglePredecessor() { |
| 86 | auto it = pred_begin(); |
| 87 | if (it == pred_end()) |
| 88 | return nullptr; |
| 89 | auto *firstPred = *it; |
| 90 | ++it; |
| 91 | return it == pred_end() ? firstPred : nullptr; |
| 92 | } |
| 93 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
| 95 | // ilist_traits for BasicBlock |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Chris Lattner | 3a467cc | 2018-07-01 20:28:00 -0700 | [diff] [blame] | 98 | mlir::CFGFunction * |
| 99 | llvm::ilist_traits<::mlir::BasicBlock>::getContainingFunction() { |
| 100 | size_t Offset( |
| 101 | size_t(&((CFGFunction *)nullptr->*CFGFunction::getSublistAccess(nullptr)))); |
| 102 | iplist<BasicBlock> *Anchor(static_cast<iplist<BasicBlock> *>(this)); |
| 103 | return reinterpret_cast<CFGFunction *>(reinterpret_cast<char *>(Anchor) - |
| 104 | Offset); |
| 105 | } |
| 106 | |
| 107 | /// This is a trait method invoked when a basic block is added to a function. |
| 108 | /// We keep the function pointer up to date. |
| 109 | void llvm::ilist_traits<::mlir::BasicBlock>:: |
| 110 | addNodeToList(BasicBlock *block) { |
| 111 | assert(!block->function && "already in a function!"); |
| 112 | block->function = getContainingFunction(); |
| 113 | } |
| 114 | |
| 115 | /// This is a trait method invoked when an instruction is removed from a |
| 116 | /// function. We keep the function pointer up to date. |
| 117 | void llvm::ilist_traits<::mlir::BasicBlock>:: |
| 118 | removeNodeFromList(BasicBlock *block) { |
| 119 | assert(block->function && "not already in a function!"); |
| 120 | block->function = nullptr; |
| 121 | } |
| 122 | |
| 123 | /// This is a trait method invoked when an instruction is moved from one block |
| 124 | /// to another. We keep the block pointer up to date. |
| 125 | void llvm::ilist_traits<::mlir::BasicBlock>:: |
| 126 | transferNodesFromList(ilist_traits<BasicBlock> &otherList, |
| 127 | block_iterator first, block_iterator last) { |
| 128 | // If we are transferring instructions within the same function, the parent |
| 129 | // pointer doesn't need to be updated. |
| 130 | CFGFunction *curParent = getContainingFunction(); |
| 131 | if (curParent == otherList.getContainingFunction()) |
| 132 | return; |
| 133 | |
| 134 | // Update the 'function' member of each BasicBlock. |
| 135 | for (; first != last; ++first) |
| 136 | first->function = curParent; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 137 | } |