Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame^] | 1 | //===- Statement.cpp - MLIR Statement 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/MLFunction.h" |
| 19 | #include "mlir/IR/Statements.h" |
| 20 | using namespace mlir; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Statement |
| 24 | //===------------------------------------------------------------------===// |
| 25 | |
| 26 | // Statements are deleted through the destroy() member because we don't have |
| 27 | // a virtual destructor. |
| 28 | Statement::~Statement() { |
| 29 | assert(block == nullptr && "statement destroyed but still in a block"); |
| 30 | } |
| 31 | |
| 32 | /// Destroy this statement or one of its subclasses. |
| 33 | void Statement::destroy(Statement *stmt) { |
| 34 | switch (stmt->getKind()) { |
| 35 | case Kind::Operation: |
| 36 | delete cast<OperationStmt>(stmt); |
| 37 | break; |
| 38 | case Kind::For: |
| 39 | delete cast<ForStmt>(stmt); |
| 40 | break; |
| 41 | case Kind::If: |
| 42 | delete cast<IfStmt>(stmt); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | MLFunction *Statement::getFunction() const { |
| 48 | return this->getBlock()->getFunction(); |
| 49 | } |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | // ilist_traits for Statement |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | StmtBlock *llvm::ilist_traits<::mlir::Statement>::getContainingBlock() { |
| 56 | size_t Offset( |
| 57 | size_t(&((StmtBlock *)nullptr->*StmtBlock::getSublistAccess(nullptr)))); |
| 58 | iplist<Statement> *Anchor(static_cast<iplist<Statement> *>(this)); |
| 59 | return reinterpret_cast<StmtBlock *>(reinterpret_cast<char *>(Anchor) - |
| 60 | Offset); |
| 61 | } |
| 62 | |
| 63 | /// This is a trait method invoked when a statement is added to a block. We |
| 64 | /// keep the block pointer up to date. |
| 65 | void llvm::ilist_traits<::mlir::Statement>::addNodeToList(Statement *stmt) { |
| 66 | assert(!stmt->getBlock() && "already in a statement block!"); |
| 67 | stmt->block = getContainingBlock(); |
| 68 | } |
| 69 | |
| 70 | /// This is a trait method invoked when a statement is removed from a block. |
| 71 | /// We keep the block pointer up to date. |
| 72 | void llvm::ilist_traits<::mlir::Statement>::removeNodeFromList( |
| 73 | Statement *stmt) { |
| 74 | assert(stmt->block && "not already in a statement block!"); |
| 75 | stmt->block = nullptr; |
| 76 | } |
| 77 | |
| 78 | /// This is a trait method invoked when a statement is moved from one block |
| 79 | /// to another. We keep the block pointer up to date. |
| 80 | void llvm::ilist_traits<::mlir::Statement>::transferNodesFromList( |
| 81 | ilist_traits<Statement> &otherList, stmt_iterator first, |
| 82 | stmt_iterator last) { |
| 83 | // If we are transferring statements within the same block, the block |
| 84 | // pointer doesn't need to be updated. |
| 85 | StmtBlock *curParent = getContainingBlock(); |
| 86 | if (curParent == otherList.getContainingBlock()) |
| 87 | return; |
| 88 | |
| 89 | // Update the 'block' member of each statement. |
| 90 | for (; first != last; ++first) |
| 91 | first->block = curParent; |
| 92 | } |
| 93 | |
| 94 | /// Remove this statement from its StmtBlock and delete it. |
| 95 | void Statement::eraseFromBlock() { |
| 96 | assert(getBlock() && "Statement has no block"); |
| 97 | getBlock()->getStatements().erase(this); |
| 98 | } |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // IfClause |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
| 104 | IfClause::IfClause(IfStmt *stmt) : StmtBlock(stmt) { |
| 105 | assert(stmt != nullptr && "If clause must have non-null parent"); |
| 106 | } |
| 107 | |
| 108 | IfStmt *IfClause::getIf() const { return static_cast<IfStmt *>(parent); } |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // IfStmt |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
| 114 | IfStmt::~IfStmt() { |
| 115 | // TODO: correctly delete StmtBlocks under then and else clauses |
| 116 | delete thenClause; |
| 117 | if (elseClause != nullptr) |
| 118 | delete elseClause; |
| 119 | } |