blob: 8476b0601a5307e7213393e3180bf141c4ea826d [file] [log] [blame]
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -07001//===- Function.cpp - MLIR Function 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// =============================================================================
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070017
Chris Lattner4c95a502018-06-23 16:03:42 -070018#include "mlir/IR/CFGFunction.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070019#include "mlir/IR/MLFunction.h"
Chris Lattnera8e47672018-07-25 14:08:16 -070020#include "mlir/IR/Module.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070021#include "mlir/IR/Types.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070022#include "llvm/ADT/StringRef.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070023using namespace mlir;
24
Chris Lattner4c95a502018-06-23 16:03:42 -070025Function::Function(StringRef name, FunctionType *type, Kind kind)
26 : kind(kind), name(name.str()), type(type) {
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070027}
28
Chris Lattnerff0d5902018-07-05 09:12:11 -070029MLIRContext *Function::getContext() const { return getType()->getContext(); }
30
Chris Lattnera8e47672018-07-25 14:08:16 -070031/// Delete this object.
32void Function::destroy() {
33 switch (getKind()) {
34 case Kind::ExtFunc:
35 delete cast<ExtFunction>(this);
36 break;
37 case Kind::MLFunc:
38 delete cast<MLFunction>(this);
39 break;
40 case Kind::CFGFunc:
41 delete cast<CFGFunction>(this);
42 break;
43 }
44}
45
46Module *llvm::ilist_traits<Function>::getContainingModule() {
47 size_t Offset(
48 size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr))));
49 iplist<Function> *Anchor(static_cast<iplist<Function> *>(this));
50 return reinterpret_cast<Module *>(reinterpret_cast<char *>(Anchor) - Offset);
51}
52
53/// This is a trait method invoked when a Function is added to a Module. We
54/// keep the module pointer up to date.
55void llvm::ilist_traits<Function>::addNodeToList(Function *function) {
56 assert(!function->getModule() && "already in a module!");
57 function->module = getContainingModule();
58}
59
60/// This is a trait method invoked when a Function is removed from a Module.
61/// We keep the module pointer up to date.
62void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) {
63 assert(function->module && "not already in a module!");
64 function->module = nullptr;
65}
66
67/// This is a trait method invoked when an instruction is moved from one block
68/// to another. We keep the block pointer up to date.
69void llvm::ilist_traits<Function>::transferNodesFromList(
70 ilist_traits<Function> &otherList, function_iterator first,
71 function_iterator last) {
72 // If we are transferring functions within the same module, the Module
73 // pointer doesn't need to be updated.
74 Module *curParent = getContainingModule();
75 if (curParent == otherList.getContainingModule())
76 return;
77
78 // Update the 'module' member of each function.
79 for (; first != last; ++first)
80 first->module = curParent;
81}
82
83/// Unlink this function from its Module and delete it.
84void Function::eraseFromModule() {
85 assert(getModule() && "Function has no parent");
86 getModule()->getFunctions().erase(this);
87}
88
Chris Lattner4c95a502018-06-23 16:03:42 -070089//===----------------------------------------------------------------------===//
90// ExtFunction implementation.
91//===----------------------------------------------------------------------===//
Chris Lattnerf7e22732018-06-22 22:03:48 -070092
Chris Lattner4c95a502018-06-23 16:03:42 -070093ExtFunction::ExtFunction(StringRef name, FunctionType *type)
94 : Function(name, type, Kind::ExtFunc) {
Chris Lattnere2259872018-06-21 15:22:42 -070095}
96
Chris Lattner4c95a502018-06-23 16:03:42 -070097//===----------------------------------------------------------------------===//
98// CFGFunction implementation.
99//===----------------------------------------------------------------------===//
100
101CFGFunction::CFGFunction(StringRef name, FunctionType *type)
102 : Function(name, type, Kind::CFGFunc) {
Chris Lattnere2259872018-06-21 15:22:42 -0700103}
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700104
Chris Lattnera8e47672018-07-25 14:08:16 -0700105CFGFunction::~CFGFunction() {
106 // Instructions may have cyclic references, which need to be dropped before we
107 // can start deleting them.
108 for (auto &bb : *this) {
109 for (auto &inst : bb)
110 inst.dropAllReferences();
111 }
112}
113
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700114//===----------------------------------------------------------------------===//
115// MLFunction implementation.
116//===----------------------------------------------------------------------===//
117
118MLFunction::MLFunction(StringRef name, FunctionType *type)
Tatiana Shpeismanc2d88e92018-07-14 16:44:22 -0700119 : Function(name, type, Kind::MLFunc), StmtBlock(StmtBlockKind::MLFunc) {}
Chris Lattnera8e47672018-07-25 14:08:16 -0700120
121MLFunction::~MLFunction() {
122 // TODO: When move SSA stuff is supported.
123 // dropAllReferences();
124}