blob: 79384f98049865e6e95bf16b7d0c209558438ed1 [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 Lattner3df3e052004-07-12 01:17:34 +000016#include "llvm/Constant.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000017#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/iOther.h"
Chris Lattnerbb346d02003-05-08 03:47:33 +000019#include "llvm/Intrinsics.h"
Chris Lattner184b2982002-09-08 18:59:35 +000020#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner9ed7aef2002-09-06 21:33:15 +000024BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000025 BasicBlock *Ret = new BasicBlock();
26 // This should not be garbage monitored.
27 LeakDetector::removeGarbageObject(Ret);
28 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000029}
30
Chris Lattner113f4f42002-06-25 16:13:24 +000031iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
32 return F->getBasicBlockList();
33}
34
35Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000036 Argument *Ret = new Argument(Type::IntTy);
37 // This should not be garbage monitored.
38 LeakDetector::removeGarbageObject(Ret);
39 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000040}
41
42iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
43 return F->getArgumentList();
44}
45
46// Explicit instantiations of SymbolTableListTraits since some of the methods
47// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000048template class SymbolTableListTraits<Argument, Function, Function>;
49template class SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000050
Chris Lattnerda975502001-09-10 07:58:01 +000051//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000052// Argument Implementation
53//===----------------------------------------------------------------------===//
54
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000055Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000056 : Value(Ty, Value::ArgumentVal, Name) {
57 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000058
59 // Make sure that we get added to a function
60 LeakDetector::addGarbageObject(this);
61
Chris Lattner9ed7aef2002-09-06 21:33:15 +000062 if (Par)
63 Par->getArgumentList().push_back(this);
64}
65
66
Chris Lattnerd255ae22002-04-09 19:39:35 +000067// Specialize setName to take care of symbol table majik
68void Argument::setName(const std::string &name, SymbolTable *ST) {
69 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000070 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerd255ae22002-04-09 19:39:35 +000071 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000072 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000073 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000074 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000075}
76
Chris Lattner9ed7aef2002-09-06 21:33:15 +000077void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000078 if (getParent())
79 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000080 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000081 if (getParent())
82 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000083}
84
Chris Lattner3df3e052004-07-12 01:17:34 +000085static bool removeDeadConstantUsers(Constant *C) {
86 while (!C->use_empty()) {
87 if (Constant *C = dyn_cast<Constant>(C->use_back())) {
88 if (!removeDeadConstantUsers(C))
89 return false; // Constant wasn't dead.
90 } else {
91 return false; // Nonconstant user of the global.
92 }
93 }
94
95 C->destroyConstant();
96 return true;
97}
98
99
100/// removeDeadConstantUsers - If there are any dead constant users dangling
101/// off of this global value, remove them. This method is useful for clients
102/// that want to check to see if a global is unused, but don't want to deal
103/// with potentially dead constants hanging off of the globals.
104///
105/// This function returns true if the global value is now dead. If all
106/// users of this global are not dead, this method may return false and
107/// leave some of them around.
108bool GlobalValue::removeDeadConstantUsers() {
109 while (!use_empty()) {
110 if (Constant *C = dyn_cast<Constant>(use_back())) {
111 if (!::removeDeadConstantUsers(C))
112 return false; // Constant wasn't dead.
113 } else {
114 return false; // Nonconstant user of the global.
115 }
116 }
117 return true;
118}
119
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000120
Chris Lattnerd255ae22002-04-09 19:39:35 +0000121//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000122// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000123//===----------------------------------------------------------------------===//
124
Chris Lattner379a8d22003-04-16 20:28:45 +0000125Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000126 const std::string &name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000127 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000128 BasicBlocks.setItemParent(this);
129 BasicBlocks.setParent(this);
130 ArgumentList.setItemParent(this);
131 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +0000132 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000133
Chris Lattner3ae303c2003-11-21 22:32:23 +0000134 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
135 && "LLVM functions cannot return aggregate values!");
136
Chris Lattner149376d2002-10-13 20:57:00 +0000137 // Create the arguments vector, all arguments start out unnamed.
138 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
139 assert(Ty->getParamType(i) != Type::VoidTy &&
140 "Cannot have void typed arguments!");
141 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
142 }
143
Chris Lattner184b2982002-09-08 18:59:35 +0000144 // Make sure that we get added to a function
145 LeakDetector::addGarbageObject(this);
146
Chris Lattner6213ae02002-09-06 20:46:32 +0000147 if (ParentModule)
148 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
Chris Lattner4e8c4872002-03-23 22:51:58 +0000151Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152 dropAllReferences(); // After this it is safe to delete instructions.
153
Chris Lattner2f7c9632001-06-06 20:29:01 +0000154 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000155 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000157 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158}
159
160// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000161void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000163 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000164 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000165 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166 Value::setName(name);
Chris Lattner570c69c2004-01-10 21:42:24 +0000167 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168}
169
Chris Lattner4e8c4872002-03-23 22:51:58 +0000170void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000171 if (getParent())
172 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000174 if (getParent())
175 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176}
177
Chris Lattner91db5822002-03-29 03:44:36 +0000178const FunctionType *Function::getFunctionType() const {
179 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000180}
181
Chris Lattner4e8c4872002-03-23 22:51:58 +0000182const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000183 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184}
185
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186// dropAllReferences() - This function causes all the subinstructions to "let
187// go" of all references that they are maintaining. This allows one to
188// 'delete' a whole class at a time, even though there may be circular
189// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000190// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191// valid on an object that has "dropped all references", except operator
192// delete.
193//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000194void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000195 for (iterator I = begin(), E = end(); I != E; ++I)
196 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000197 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198}
Chris Lattnerda975502001-09-10 07:58:01 +0000199
Chris Lattnerbb346d02003-05-08 03:47:33 +0000200/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000201/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000202/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000203/// zero to allow easy checking for whether a function is intrinsic or not. The
204/// particular intrinsic functions which correspond to this value are defined in
205/// llvm/Intrinsics.h.
206///
207unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000208 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000209 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
210 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000211
212 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000213
Chris Lattner4159fdae2003-08-06 20:08:25 +0000214 // a table of all Alpha intrinsic functions
215 struct {
216 std::string name; // The name of the intrinsic
217 unsigned id; // Its ID number
218 } alpha_intrinsics[] = {
Brian Gaeke960707c2003-11-11 22:41:34 +0000219 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
220 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
221 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
222 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
223 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
224 { "llvm.alpha.pup", Intrinsic::alpha_pup },
225 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
226 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
227 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
228 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
229 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
230 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
231 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
232 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
233 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
Chris Lattner4159fdae2003-08-06 20:08:25 +0000234 };
235 const unsigned num_alpha_intrinsics =
236 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
237
Chris Lattnerbb346d02003-05-08 03:47:33 +0000238 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000239 case 'a':
Chris Lattnerade94102003-08-24 05:30:29 +0000240 if (getName().size() > 11 &&
241 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
242 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
243 if (getName() == alpha_intrinsics[i].name)
244 return alpha_intrinsics[i].id;
245 break;
Chris Lattner3d903f02004-01-05 05:36:30 +0000246 case 'd':
247 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
248 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
249 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
250 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
Chris Lattner59f1ef42004-01-06 05:33:02 +0000251 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
Chris Lattner3d903f02004-01-05 05:36:30 +0000252 break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000253 case 'f':
254 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
255 break;
Chris Lattner9c251eb2004-05-23 21:16:51 +0000256 case 'g':
257 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
258 if (getName() == "llvm.gcread") return Intrinsic::gcread;
259 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
260 break;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000261 case 'i':
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000262 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000263 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000264 case 'l':
Brian Gaeke960707c2003-11-11 22:41:34 +0000265 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000266 break;
Chris Lattner17d028d2004-02-12 17:01:09 +0000267 case 'm':
268 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
Chris Lattner5ed171e2004-02-12 18:11:20 +0000269 if (getName() == "llvm.memmove") return Intrinsic::memmove;
Chris Lattnerdc632112004-02-14 02:47:17 +0000270 if (getName() == "llvm.memset") return Intrinsic::memset;
271 break;
272 case 'r':
273 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
John Criswell52010042004-04-08 20:27:38 +0000274 if (getName() == "llvm.readport") return Intrinsic::readport;
John Criswell23c48d62004-04-14 13:46:52 +0000275 if (getName() == "llvm.readio") return Intrinsic::readio;
Chris Lattner17d028d2004-02-12 17:01:09 +0000276 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000277 case 's':
Brian Gaeke960707c2003-11-11 22:41:34 +0000278 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
279 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
280 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000281 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000282 case 'v':
Chris Lattner071a5e52004-03-13 00:24:00 +0000283 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
284 if (getName() == "llvm.va_end") return Intrinsic::vaend;
285 if (getName() == "llvm.va_start") return Intrinsic::vastart;
John Criswell52010042004-04-08 20:27:38 +0000286 case 'w':
287 if (getName() == "llvm.writeport") return Intrinsic::writeport;
John Criswell23c48d62004-04-14 13:46:52 +0000288 if (getName() == "llvm.writeio") return Intrinsic::writeio;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000289 break;
290 }
291 // The "llvm." namespace is reserved!
292 assert(0 && "Unknown LLVM intrinsic function!");
293 return 0;
294}
295
296
Chris Lattnerda975502001-09-10 07:58:01 +0000297//===----------------------------------------------------------------------===//
298// GlobalVariable Implementation
299//===----------------------------------------------------------------------===//
300
Chris Lattner379a8d22003-04-16 20:28:45 +0000301GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000302 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000303 const std::string &Name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +0000304 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000305 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000306 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000307
Chris Lattner184b2982002-09-08 18:59:35 +0000308 LeakDetector::addGarbageObject(this);
309
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000310 if (ParentModule)
311 ParentModule->getGlobalList().push_back(this);
312}
313
314void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000315 if (getParent())
316 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000317 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000318 if (getParent())
319 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000320}
321
322// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000323void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000324 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000325 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000326 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000327 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000328 Value::setName(name);
Chris Lattner03c71fa2004-01-10 21:40:29 +0000329 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000330}