blob: 1aad430e4aabd412b39798efbe18df0354a2fc62 [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
James Molloy61a656c2018-07-22 15:45:24 -070022BasicBlock::BasicBlock() {}
Chris Lattner3a467cc2018-07-01 20:28:00 -070023
24BasicBlock::~BasicBlock() {
25 if (terminator)
26 terminator->eraseFromBlock();
James Molloy61a656c2018-07-22 15:45:24 -070027 for (BBArgument *arg : arguments)
28 delete arg;
29 arguments.clear();
Chris Lattner3a467cc2018-07-01 20:28:00 -070030}
31
32/// Unlink this BasicBlock from its CFGFunction and delete it.
33void BasicBlock::eraseFromFunction() {
34 assert(getFunction() && "BasicBlock has no parent");
35 getFunction()->getBlocks().erase(this);
36}
37
38void BasicBlock::setTerminator(TerminatorInst *inst) {
39 // If we already had a terminator, abandon it.
40 if (terminator)
41 terminator->block = nullptr;
42
43 // Reset our terminator to the new instruction.
44 terminator = inst;
45 if (inst)
46 inst->block = this;
47}
48
49mlir::CFGFunction *
50llvm::ilist_traits<::mlir::BasicBlock>::getContainingFunction() {
51 size_t Offset(
52 size_t(&((CFGFunction *)nullptr->*CFGFunction::getSublistAccess(nullptr))));
53 iplist<BasicBlock> *Anchor(static_cast<iplist<BasicBlock> *>(this));
54 return reinterpret_cast<CFGFunction *>(reinterpret_cast<char *>(Anchor) -
55 Offset);
56}
57
58/// This is a trait method invoked when a basic block is added to a function.
59/// We keep the function pointer up to date.
60void llvm::ilist_traits<::mlir::BasicBlock>::
61addNodeToList(BasicBlock *block) {
62 assert(!block->function && "already in a function!");
63 block->function = getContainingFunction();
64}
65
66/// This is a trait method invoked when an instruction is removed from a
67/// function. We keep the function pointer up to date.
68void llvm::ilist_traits<::mlir::BasicBlock>::
69removeNodeFromList(BasicBlock *block) {
70 assert(block->function && "not already in a function!");
71 block->function = nullptr;
72}
73
74/// This is a trait method invoked when an instruction is moved from one block
75/// to another. We keep the block pointer up to date.
76void llvm::ilist_traits<::mlir::BasicBlock>::
77transferNodesFromList(ilist_traits<BasicBlock> &otherList,
78 block_iterator first, block_iterator last) {
79 // If we are transferring instructions within the same function, the parent
80 // pointer doesn't need to be updated.
81 CFGFunction *curParent = getContainingFunction();
82 if (curParent == otherList.getContainingFunction())
83 return;
84
85 // Update the 'function' member of each BasicBlock.
86 for (; first != last; ++first)
87 first->function = curParent;
Chris Lattner4c95a502018-06-23 16:03:42 -070088}
James Molloy61a656c2018-07-22 15:45:24 -070089
90BBArgument *BasicBlock::addArgument(Type *type) {
91 arguments.push_back(new BBArgument(type, this));
92 return arguments.back();
93}
94
95llvm::iterator_range<BasicBlock::BBArgListType::iterator>
96BasicBlock::addArguments(ArrayRef<Type *> types) {
97 auto initial_size = arguments.size();
98 for (auto *type : types) {
99 addArgument(type);
100 }
101 return {arguments.data() + initial_size, arguments.data() + arguments.size()};
102}