blob: f47ecea7af3db6a2868c4a714f5c94a991e27bcb [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner57698e22002-03-26 18:01:55 +000010// This file implements the Function & GlobalVariable classes for the VMCore
Chris Lattnerda975502001-09-10 07:58:01 +000011// library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000016#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/iOther.h"
Chris Lattnerbb346d02003-05-08 03:47:33 +000018#include "llvm/Intrinsics.h"
Chris Lattner184b2982002-09-08 18:59:35 +000019#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
21
Chris Lattner9ed7aef2002-09-06 21:33:15 +000022BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000023 BasicBlock *Ret = new BasicBlock();
24 // This should not be garbage monitored.
25 LeakDetector::removeGarbageObject(Ret);
26 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000027}
28
Chris Lattner113f4f42002-06-25 16:13:24 +000029iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
30 return F->getBasicBlockList();
31}
32
33Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000034 Argument *Ret = new Argument(Type::IntTy);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
37 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000038}
39
40iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
41 return F->getArgumentList();
42}
43
44// Explicit instantiations of SymbolTableListTraits since some of the methods
45// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000046template class SymbolTableListTraits<Argument, Function, Function>;
47template class SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000048
Chris Lattnerda975502001-09-10 07:58:01 +000049//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000050// Argument Implementation
51//===----------------------------------------------------------------------===//
52
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000053Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000054 : Value(Ty, Value::ArgumentVal, Name) {
55 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000056
57 // Make sure that we get added to a function
58 LeakDetector::addGarbageObject(this);
59
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 if (Par)
61 Par->getArgumentList().push_back(this);
62}
63
64
Chris Lattnerd255ae22002-04-09 19:39:35 +000065// Specialize setName to take care of symbol table majik
66void Argument::setName(const std::string &name, SymbolTable *ST) {
67 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000068 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerd255ae22002-04-09 19:39:35 +000069 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000070 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000071 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000072 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000073}
74
Chris Lattner9ed7aef2002-09-06 21:33:15 +000075void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000076 if (getParent())
77 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000078 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000079 if (getParent())
80 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000081}
82
83
Chris Lattnerd255ae22002-04-09 19:39:35 +000084//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000085// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000086//===----------------------------------------------------------------------===//
87
Chris Lattner379a8d22003-04-16 20:28:45 +000088Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000089 const std::string &name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +000090 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000091 BasicBlocks.setItemParent(this);
92 BasicBlocks.setParent(this);
93 ArgumentList.setItemParent(this);
94 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000095 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000096
Chris Lattner149376d2002-10-13 20:57:00 +000097 // Create the arguments vector, all arguments start out unnamed.
98 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
99 assert(Ty->getParamType(i) != Type::VoidTy &&
100 "Cannot have void typed arguments!");
101 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
102 }
103
Chris Lattner184b2982002-09-08 18:59:35 +0000104 // Make sure that we get added to a function
105 LeakDetector::addGarbageObject(this);
106
Chris Lattner6213ae02002-09-06 20:46:32 +0000107 if (ParentModule)
108 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109}
110
Chris Lattner4e8c4872002-03-23 22:51:58 +0000111Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112 dropAllReferences(); // After this it is safe to delete instructions.
113
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000115 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000117 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118}
119
120// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000121void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000123 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000124 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000125 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000127 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128}
129
Chris Lattner4e8c4872002-03-23 22:51:58 +0000130void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000131 if (getParent())
132 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000134 if (getParent())
135 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136}
137
Chris Lattner91db5822002-03-29 03:44:36 +0000138const FunctionType *Function::getFunctionType() const {
139 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000140}
141
Chris Lattner4e8c4872002-03-23 22:51:58 +0000142const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000143 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000144}
145
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146// dropAllReferences() - This function causes all the subinstructions to "let
147// go" of all references that they are maintaining. This allows one to
148// 'delete' a whole class at a time, even though there may be circular
149// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000150// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151// valid on an object that has "dropped all references", except operator
152// delete.
153//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000154void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000155 for (iterator I = begin(), E = end(); I != E; ++I)
156 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000157 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158}
Chris Lattnerda975502001-09-10 07:58:01 +0000159
Chris Lattnerbb346d02003-05-08 03:47:33 +0000160/// getIntrinsicID - This method returns the ID number of the specified
161/// function, or LLVMIntrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000162/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000163/// zero to allow easy checking for whether a function is intrinsic or not. The
164/// particular intrinsic functions which correspond to this value are defined in
165/// llvm/Intrinsics.h.
166///
167unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000168 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000169 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
170 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000171
172 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000173
Chris Lattner4159fdae2003-08-06 20:08:25 +0000174 // a table of all Alpha intrinsic functions
175 struct {
176 std::string name; // The name of the intrinsic
177 unsigned id; // Its ID number
178 } alpha_intrinsics[] = {
179 { "llvm.alpha.ctlz", LLVMIntrinsic::alpha_ctlz },
180 { "llvm.alpha.cttz", LLVMIntrinsic::alpha_cttz },
181 { "llvm.alpha.ctpop", LLVMIntrinsic::alpha_ctpop },
182 { "llvm.alpha.umulh", LLVMIntrinsic::alpha_umulh },
183 { "llvm.alpha.vecop", LLVMIntrinsic::alpha_vecop },
184 { "llvm.alpha.pup", LLVMIntrinsic::alpha_pup },
185 { "llvm.alpha.bytezap", LLVMIntrinsic::alpha_bytezap },
186 { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
187 { "llvm.alpha.dfp_bop", LLVMIntrinsic::alpha_dfpbop },
188 { "llvm.alpha.dfp_uop", LLVMIntrinsic::alpha_dfpuop },
189 { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
190 { "llvm.alpha.uqtodfp", LLVMIntrinsic::alpha_uqtodfp },
191 { "llvm.alpha.uqtosfp", LLVMIntrinsic::alpha_uqtosfp },
192 { "llvm.alpha.dfptosq", LLVMIntrinsic::alpha_dfptosq },
193 { "llvm.alpha.sfptosq", LLVMIntrinsic::alpha_sfptosq },
194 };
195 const unsigned num_alpha_intrinsics =
196 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
197
Chris Lattnerbb346d02003-05-08 03:47:33 +0000198 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000199 case 'a':
Chris Lattnerade94102003-08-24 05:30:29 +0000200 if (getName().size() > 11 &&
201 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
202 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
203 if (getName() == alpha_intrinsics[i].name)
204 return alpha_intrinsics[i].id;
205 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000206 case 'l':
207 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
208 break;
209 case 's':
Chris Lattner748e9e12003-08-18 15:41:24 +0000210 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
211 if (getName() == "llvm.sigsetjmp") return LLVMIntrinsic::sigsetjmp;
212 if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000213 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000214 case 'v':
Chris Lattner192623e2003-05-17 22:26:33 +0000215 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
216 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
217 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000218 break;
219 }
220 // The "llvm." namespace is reserved!
221 assert(0 && "Unknown LLVM intrinsic function!");
222 return 0;
223}
224
225
Chris Lattnerda975502001-09-10 07:58:01 +0000226//===----------------------------------------------------------------------===//
227// GlobalVariable Implementation
228//===----------------------------------------------------------------------===//
229
Chris Lattner379a8d22003-04-16 20:28:45 +0000230GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000231 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000232 const std::string &Name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000233 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000234 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000235 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000236
Chris Lattner184b2982002-09-08 18:59:35 +0000237 LeakDetector::addGarbageObject(this);
238
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000239 if (ParentModule)
240 ParentModule->getGlobalList().push_back(this);
241}
242
243void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000244 if (getParent())
245 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000246 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000247 if (getParent())
248 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000249}
250
251// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000252void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000253 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000254 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000255 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000256 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000257 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000258 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000259}