blob: dd73a8745462b6891349d682f89fd8489b15eda7 [file] [log] [blame]
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001//===- 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"
20using 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.
28Statement::~Statement() {
29 assert(block == nullptr && "statement destroyed but still in a block");
30}
31
32/// Destroy this statement or one of its subclasses.
33void 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
47MLFunction *Statement::getFunction() const {
48 return this->getBlock()->getFunction();
49}
50
51//===----------------------------------------------------------------------===//
52// ilist_traits for Statement
53//===----------------------------------------------------------------------===//
54
55StmtBlock *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.
65void 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.
72void 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.
80void 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.
95void Statement::eraseFromBlock() {
96 assert(getBlock() && "Statement has no block");
97 getBlock()->getStatements().erase(this);
98}
99
100//===----------------------------------------------------------------------===//
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -0700101// IfStmt
102//===----------------------------------------------------------------------===//
103
104IfStmt::~IfStmt() {
105 // TODO: correctly delete StmtBlocks under then and else clauses
106 delete thenClause;
107 if (elseClause != nullptr)
108 delete elseClause;
109}