blob: d31a69e49abe4275124f376979a2d250e9547eb5 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
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 Lattner2f7c9632001-06-06 20:29:01 +0000107 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000110 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111}
112
113// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000114void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000116 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000117 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000118 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000120 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121}
122
Chris Lattner4e8c4872002-03-23 22:51:58 +0000123void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000124 if (getParent())
125 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000127 if (getParent())
128 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129}
130
Chris Lattner91db5822002-03-29 03:44:36 +0000131const FunctionType *Function::getFunctionType() const {
132 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000133}
134
Chris Lattner4e8c4872002-03-23 22:51:58 +0000135const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000136 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137}
138
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139// dropAllReferences() - This function causes all the subinstructions to "let
140// go" of all references that they are maintaining. This allows one to
141// 'delete' a whole class at a time, even though there may be circular
142// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000143// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144// valid on an object that has "dropped all references", except operator
145// delete.
146//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000147void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000148 for (iterator I = begin(), E = end(); I != E; ++I)
149 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000150 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151}
Chris Lattnerda975502001-09-10 07:58:01 +0000152
Chris Lattnerbb346d02003-05-08 03:47:33 +0000153/// getIntrinsicID - This method returns the ID number of the specified
154/// function, or LLVMIntrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000155/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000156/// zero to allow easy checking for whether a function is intrinsic or not. The
157/// particular intrinsic functions which correspond to this value are defined in
158/// llvm/Intrinsics.h.
159///
160unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000161 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000162 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
163 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000164
165 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000166
Chris Lattner4159fdae2003-08-06 20:08:25 +0000167 // a table of all Alpha intrinsic functions
168 struct {
169 std::string name; // The name of the intrinsic
170 unsigned id; // Its ID number
171 } alpha_intrinsics[] = {
172 { "llvm.alpha.ctlz", LLVMIntrinsic::alpha_ctlz },
173 { "llvm.alpha.cttz", LLVMIntrinsic::alpha_cttz },
174 { "llvm.alpha.ctpop", LLVMIntrinsic::alpha_ctpop },
175 { "llvm.alpha.umulh", LLVMIntrinsic::alpha_umulh },
176 { "llvm.alpha.vecop", LLVMIntrinsic::alpha_vecop },
177 { "llvm.alpha.pup", LLVMIntrinsic::alpha_pup },
178 { "llvm.alpha.bytezap", LLVMIntrinsic::alpha_bytezap },
179 { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
180 { "llvm.alpha.dfp_bop", LLVMIntrinsic::alpha_dfpbop },
181 { "llvm.alpha.dfp_uop", LLVMIntrinsic::alpha_dfpuop },
182 { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
183 { "llvm.alpha.uqtodfp", LLVMIntrinsic::alpha_uqtodfp },
184 { "llvm.alpha.uqtosfp", LLVMIntrinsic::alpha_uqtosfp },
185 { "llvm.alpha.dfptosq", LLVMIntrinsic::alpha_dfptosq },
186 { "llvm.alpha.sfptosq", LLVMIntrinsic::alpha_sfptosq },
187 };
188 const unsigned num_alpha_intrinsics =
189 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
190
Chris Lattnerbb346d02003-05-08 03:47:33 +0000191 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000192 case 'a':
Chris Lattnerade94102003-08-24 05:30:29 +0000193 if (getName().size() > 11 &&
194 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
195 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
196 if (getName() == alpha_intrinsics[i].name)
197 return alpha_intrinsics[i].id;
198 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000199 case 'l':
200 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
201 break;
202 case 's':
Chris Lattner748e9e12003-08-18 15:41:24 +0000203 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
204 if (getName() == "llvm.sigsetjmp") return LLVMIntrinsic::sigsetjmp;
205 if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000206 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000207 case 'v':
Chris Lattner192623e2003-05-17 22:26:33 +0000208 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
209 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
210 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000211 break;
212 }
213 // The "llvm." namespace is reserved!
214 assert(0 && "Unknown LLVM intrinsic function!");
215 return 0;
216}
217
218
Chris Lattnerda975502001-09-10 07:58:01 +0000219//===----------------------------------------------------------------------===//
220// GlobalVariable Implementation
221//===----------------------------------------------------------------------===//
222
Chris Lattner379a8d22003-04-16 20:28:45 +0000223GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000224 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000225 const std::string &Name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000226 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000227 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000228 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000229
Chris Lattner184b2982002-09-08 18:59:35 +0000230 LeakDetector::addGarbageObject(this);
231
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000232 if (ParentModule)
233 ParentModule->getGlobalList().push_back(this);
234}
235
236void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000237 if (getParent())
238 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000239 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000240 if (getParent())
241 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000242}
243
244// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000245void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000246 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000247 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000248 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000249 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000250 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000251 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000252}