blob: 9a4ac6711ccb3f299d0c4faf21183a3781ed74fc [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"
Chris Lattner189d19f2003-11-21 20:23:48 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner9ed7aef2002-09-06 21:33:15 +000023BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000024 BasicBlock *Ret = new BasicBlock();
25 // This should not be garbage monitored.
26 LeakDetector::removeGarbageObject(Ret);
27 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000028}
29
Chris Lattner113f4f42002-06-25 16:13:24 +000030iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
31 return F->getBasicBlockList();
32}
33
34Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000035 Argument *Ret = new Argument(Type::IntTy);
36 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000039}
40
41iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
42 return F->getArgumentList();
43}
44
45// Explicit instantiations of SymbolTableListTraits since some of the methods
46// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000047template class SymbolTableListTraits<Argument, Function, Function>;
48template class SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
Chris Lattnerda975502001-09-10 07:58:01 +000050//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000051// Argument Implementation
52//===----------------------------------------------------------------------===//
53
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000054Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000055 : Value(Ty, Value::ArgumentVal, Name) {
56 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000057
58 // Make sure that we get added to a function
59 LeakDetector::addGarbageObject(this);
60
Chris Lattner9ed7aef2002-09-06 21:33:15 +000061 if (Par)
62 Par->getArgumentList().push_back(this);
63}
64
65
Chris Lattnerd255ae22002-04-09 19:39:35 +000066// Specialize setName to take care of symbol table majik
67void Argument::setName(const std::string &name, SymbolTable *ST) {
68 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000069 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerd255ae22002-04-09 19:39:35 +000070 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000071 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000072 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000073 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000074}
75
Chris Lattner9ed7aef2002-09-06 21:33:15 +000076void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000077 if (getParent())
78 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000079 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000080 if (getParent())
81 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000082}
83
84
Chris Lattnerd255ae22002-04-09 19:39:35 +000085//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000086// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000087//===----------------------------------------------------------------------===//
88
Chris Lattner379a8d22003-04-16 20:28:45 +000089Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000090 const std::string &name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +000091 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000092 BasicBlocks.setItemParent(this);
93 BasicBlocks.setParent(this);
94 ArgumentList.setItemParent(this);
95 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000096 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000097
Chris Lattner149376d2002-10-13 20:57:00 +000098 // Create the arguments vector, all arguments start out unnamed.
99 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
100 assert(Ty->getParamType(i) != Type::VoidTy &&
101 "Cannot have void typed arguments!");
102 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
103 }
104
Chris Lattner184b2982002-09-08 18:59:35 +0000105 // Make sure that we get added to a function
106 LeakDetector::addGarbageObject(this);
107
Chris Lattner6213ae02002-09-06 20:46:32 +0000108 if (ParentModule)
109 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110}
111
Chris Lattner4e8c4872002-03-23 22:51:58 +0000112Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 dropAllReferences(); // After this it is safe to delete instructions.
114
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000118 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119}
120
121// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000122void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000124 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000125 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000126 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000128 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129}
130
Chris Lattner4e8c4872002-03-23 22:51:58 +0000131void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000132 if (getParent())
133 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000135 if (getParent())
136 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137}
138
Chris Lattner91db5822002-03-29 03:44:36 +0000139const FunctionType *Function::getFunctionType() const {
140 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000141}
142
Chris Lattner4e8c4872002-03-23 22:51:58 +0000143const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000144 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145}
146
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147// dropAllReferences() - This function causes all the subinstructions to "let
148// go" of all references that they are maintaining. This allows one to
149// 'delete' a whole class at a time, even though there may be circular
150// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000151// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152// valid on an object that has "dropped all references", except operator
153// delete.
154//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000155void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000156 for (iterator I = begin(), E = end(); I != E; ++I)
157 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000158 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159}
Chris Lattnerda975502001-09-10 07:58:01 +0000160
Chris Lattnerbb346d02003-05-08 03:47:33 +0000161/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000162/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000163/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000164/// zero to allow easy checking for whether a function is intrinsic or not. The
165/// particular intrinsic functions which correspond to this value are defined in
166/// llvm/Intrinsics.h.
167///
168unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000169 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000170 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
171 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000172
173 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000174
Chris Lattner4159fdae2003-08-06 20:08:25 +0000175 // a table of all Alpha intrinsic functions
176 struct {
177 std::string name; // The name of the intrinsic
178 unsigned id; // Its ID number
179 } alpha_intrinsics[] = {
Brian Gaeke960707c2003-11-11 22:41:34 +0000180 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
181 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
182 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
183 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
184 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
185 { "llvm.alpha.pup", Intrinsic::alpha_pup },
186 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
187 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
188 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
189 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
190 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
191 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
192 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
193 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
194 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
Chris Lattner4159fdae2003-08-06 20:08:25 +0000195 };
196 const unsigned num_alpha_intrinsics =
197 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
198
Chris Lattnerbb346d02003-05-08 03:47:33 +0000199 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000200 case 'a':
Chris Lattnerade94102003-08-24 05:30:29 +0000201 if (getName().size() > 11 &&
202 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
203 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
204 if (getName() == alpha_intrinsics[i].name)
205 return alpha_intrinsics[i].id;
206 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000207 case 'l':
Brian Gaeke960707c2003-11-11 22:41:34 +0000208 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000209 break;
210 case 's':
Brian Gaeke960707c2003-11-11 22:41:34 +0000211 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
212 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
213 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000214 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000215 case 'v':
Brian Gaeke960707c2003-11-11 22:41:34 +0000216 if (getName() == "llvm.va_copy") return Intrinsic::va_copy;
217 if (getName() == "llvm.va_end") return Intrinsic::va_end;
218 if (getName() == "llvm.va_start") return Intrinsic::va_start;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000219 break;
220 }
221 // The "llvm." namespace is reserved!
222 assert(0 && "Unknown LLVM intrinsic function!");
223 return 0;
224}
225
226
Chris Lattnerda975502001-09-10 07:58:01 +0000227//===----------------------------------------------------------------------===//
228// GlobalVariable Implementation
229//===----------------------------------------------------------------------===//
230
Chris Lattner379a8d22003-04-16 20:28:45 +0000231GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000232 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000233 const std::string &Name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000234 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000235 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000236 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000237
Chris Lattner184b2982002-09-08 18:59:35 +0000238 LeakDetector::addGarbageObject(this);
239
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000240 if (ParentModule)
241 ParentModule->getGlobalList().push_back(this);
242}
243
244void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000245 if (getParent())
246 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000247 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000248 if (getParent())
249 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000250}
251
252// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000253void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000254 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000255 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000256 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000257 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000258 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000259 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000260}