blob: c2c865cba2cde1befaf5ae88c722692c9b58c639 [file] [log] [blame]
Chris Lattner4c95a502018-06-23 16:03:42 -07001//===- 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 Lattnerf6d80a02018-06-24 11:18:29 -070019#include "mlir/IR/CFGFunction.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070020using namespace mlir;
21
Chris Lattner3a467cc2018-07-01 20:28:00 -070022BasicBlock::BasicBlock() {
23}
24
25BasicBlock::~BasicBlock() {
26 if (terminator)
27 terminator->eraseFromBlock();
28}
29
30/// Unlink this BasicBlock from its CFGFunction and delete it.
31void BasicBlock::eraseFromFunction() {
32 assert(getFunction() && "BasicBlock has no parent");
33 getFunction()->getBlocks().erase(this);
34}
35
36void BasicBlock::setTerminator(TerminatorInst *inst) {
37 // If we already had a terminator, abandon it.
38 if (terminator)
39 terminator->block = nullptr;
40
41 // Reset our terminator to the new instruction.
42 terminator = inst;
43 if (inst)
44 inst->block = this;
45}
46
47mlir::CFGFunction *
48llvm::ilist_traits<::mlir::BasicBlock>::getContainingFunction() {
49 size_t Offset(
50 size_t(&((CFGFunction *)nullptr->*CFGFunction::getSublistAccess(nullptr))));
51 iplist<BasicBlock> *Anchor(static_cast<iplist<BasicBlock> *>(this));
52 return reinterpret_cast<CFGFunction *>(reinterpret_cast<char *>(Anchor) -
53 Offset);
54}
55
56/// This is a trait method invoked when a basic block is added to a function.
57/// We keep the function pointer up to date.
58void llvm::ilist_traits<::mlir::BasicBlock>::
59addNodeToList(BasicBlock *block) {
60 assert(!block->function && "already in a function!");
61 block->function = getContainingFunction();
62}
63
64/// This is a trait method invoked when an instruction is removed from a
65/// function. We keep the function pointer up to date.
66void llvm::ilist_traits<::mlir::BasicBlock>::
67removeNodeFromList(BasicBlock *block) {
68 assert(block->function && "not already in a function!");
69 block->function = nullptr;
70}
71
72/// This is a trait method invoked when an instruction is moved from one block
73/// to another. We keep the block pointer up to date.
74void llvm::ilist_traits<::mlir::BasicBlock>::
75transferNodesFromList(ilist_traits<BasicBlock> &otherList,
76 block_iterator first, block_iterator last) {
77 // If we are transferring instructions within the same function, the parent
78 // pointer doesn't need to be updated.
79 CFGFunction *curParent = getContainingFunction();
80 if (curParent == otherList.getContainingFunction())
81 return;
82
83 // Update the 'function' member of each BasicBlock.
84 for (; first != last; ++first)
85 first->function = curParent;
Chris Lattner4c95a502018-06-23 16:03:42 -070086}