blob: 6d78711411bef9a83623b3df3ad46b15e251281c [file] [log] [blame]
Chris Lattner57698e22002-03-26 18:01:55 +00001//===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner57698e22002-03-26 18:01:55 +00003// This file implements the Function & GlobalVariable classes for the VMCore
Chris Lattnerda975502001-09-10 07:58:01 +00004// library.
Chris Lattner2f7c9632001-06-06 20:29:01 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +00009#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/iOther.h"
Chris Lattnerbb346d02003-05-08 03:47:33 +000011#include "llvm/Intrinsics.h"
Chris Lattner184b2982002-09-08 18:59:35 +000012#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000013#include "SymbolTableListTraitsImpl.h"
14
Chris Lattner9ed7aef2002-09-06 21:33:15 +000015BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000016 BasicBlock *Ret = new BasicBlock();
17 // This should not be garbage monitored.
18 LeakDetector::removeGarbageObject(Ret);
19 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000020}
21
Chris Lattner113f4f42002-06-25 16:13:24 +000022iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
23 return F->getBasicBlockList();
24}
25
26Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000027 Argument *Ret = new Argument(Type::IntTy);
28 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
30 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000031}
32
33iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
34 return F->getArgumentList();
35}
36
37// Explicit instantiations of SymbolTableListTraits since some of the methods
38// are not in the public header file...
39template SymbolTableListTraits<Argument, Function, Function>;
40template SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000041
Chris Lattnerda975502001-09-10 07:58:01 +000042//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000043// Argument Implementation
44//===----------------------------------------------------------------------===//
45
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000046Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000047 : Value(Ty, Value::ArgumentVal, Name) {
48 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000049
50 // Make sure that we get added to a function
51 LeakDetector::addGarbageObject(this);
52
Chris Lattner9ed7aef2002-09-06 21:33:15 +000053 if (Par)
54 Par->getArgumentList().push_back(this);
55}
56
57
Chris Lattnerd255ae22002-04-09 19:39:35 +000058// Specialize setName to take care of symbol table majik
59void Argument::setName(const std::string &name, SymbolTable *ST) {
60 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000061 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerd255ae22002-04-09 19:39:35 +000062 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000063 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000064 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000065 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000066}
67
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000069 if (getParent())
70 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000071 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000072 if (getParent())
73 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000074}
75
76
Chris Lattnerd255ae22002-04-09 19:39:35 +000077//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000078// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000079//===----------------------------------------------------------------------===//
80
Chris Lattner379a8d22003-04-16 20:28:45 +000081Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000082 const std::string &name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +000083 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000084 BasicBlocks.setItemParent(this);
85 BasicBlocks.setParent(this);
86 ArgumentList.setItemParent(this);
87 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000088 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000089
Chris Lattner149376d2002-10-13 20:57:00 +000090 // Create the arguments vector, all arguments start out unnamed.
91 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
92 assert(Ty->getParamType(i) != Type::VoidTy &&
93 "Cannot have void typed arguments!");
94 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
95 }
96
Chris Lattner184b2982002-09-08 18:59:35 +000097 // Make sure that we get added to a function
98 LeakDetector::addGarbageObject(this);
99
Chris Lattner6213ae02002-09-06 20:46:32 +0000100 if (ParentModule)
101 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102}
103
Chris Lattner4e8c4872002-03-23 22:51:58 +0000104Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105 dropAllReferences(); // After this it is safe to delete instructions.
106
Chris Lattner113f4f42002-06-25 16:13:24 +0000107 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108
109 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000110 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000112 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113}
114
115// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000116void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000118 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000119 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000120 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000122 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123}
124
Chris Lattner4e8c4872002-03-23 22:51:58 +0000125void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000126 if (getParent())
127 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000129 if (getParent())
130 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131}
132
Chris Lattner91db5822002-03-29 03:44:36 +0000133const FunctionType *Function::getFunctionType() const {
134 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000135}
136
Chris Lattner4e8c4872002-03-23 22:51:58 +0000137const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000138 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139}
140
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141// dropAllReferences() - This function causes all the subinstructions to "let
142// go" of all references that they are maintaining. This allows one to
143// 'delete' a whole class at a time, even though there may be circular
144// references... first all references are dropped, and all use counts go to
145// zero. Then everything is delete'd for real. Note that no operations are
146// valid on an object that has "dropped all references", except operator
147// delete.
148//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000149void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000150 for (iterator I = begin(), E = end(); I != E; ++I)
151 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152}
Chris Lattnerda975502001-09-10 07:58:01 +0000153
Chris Lattnerbb346d02003-05-08 03:47:33 +0000154/// getIntrinsicID - This method returns the ID number of the specified
155/// function, or LLVMIntrinsic::not_intrinsic if the function is not an
156/// instrinsic, or if the pointer is null. This value is always defined to be
157/// zero to allow easy checking for whether a function is intrinsic or not. The
158/// particular intrinsic functions which correspond to this value are defined in
159/// llvm/Intrinsics.h.
160///
161unsigned Function::getIntrinsicID() const {
162 if (getName().size() <= 5 || getName()[4] != '.' || getName()[0] != 'l' ||
163 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
164 return 0; // All intrinsics start with 'llvm.'
165
166 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000167 case 'a':
168 if (getName() == "llvm.alpha.ctlz") return LLVMIntrinsic::alpha_ctlz;
169 if (getName() == "llvm.alpha.cttz") return LLVMIntrinsic::alpha_cttz;
170 if (getName() == "llvm.alpha.ctpop") return LLVMIntrinsic::alpha_ctpop;
171 if (getName() == "llvm.alpha.umulh") return LLVMIntrinsic::alpha_umulh;
172 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000173 case 'l':
174 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
175 break;
176 case 's':
177 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
178 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000179 case 'v':
Chris Lattner192623e2003-05-17 22:26:33 +0000180 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
181 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
182 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000183 break;
184 }
185 // The "llvm." namespace is reserved!
186 assert(0 && "Unknown LLVM intrinsic function!");
187 return 0;
188}
189
190
Chris Lattnerda975502001-09-10 07:58:01 +0000191//===----------------------------------------------------------------------===//
192// GlobalVariable Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattner379a8d22003-04-16 20:28:45 +0000195GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000196 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000197 const std::string &Name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000198 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000199 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000200 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000201
Chris Lattner184b2982002-09-08 18:59:35 +0000202 LeakDetector::addGarbageObject(this);
203
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000204 if (ParentModule)
205 ParentModule->getGlobalList().push_back(this);
206}
207
208void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000209 if (getParent())
210 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000211 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000212 if (getParent())
213 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000214}
215
216// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000217void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000218 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000219 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000220 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000221 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000222 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000223 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000224}