blob: 7cced301e63f566f54d95a90413322baec8220ae [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 Lattner89046ca2004-10-12 04:20:25 +000017#include "llvm/IntrinsicInst.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000019#include "SymbolTableListTraitsImpl.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattnerf6c93e32005-01-30 00:09:23 +000023BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
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
Chris Lattnerf6c93e32005-01-30 00:09:23 +000034Argument *ilist_traits<Argument>::createSentinel() {
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
Chris Lattnered2fb1c2005-03-05 19:01:49 +000067void Argument::setName(const std::string &name) {
Chris Lattnerd255ae22002-04-09 19:39:35 +000068 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000069 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000070 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000071 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000072}
73
Chris Lattner9ed7aef2002-09-06 21:33:15 +000074void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000075 if (getParent())
76 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000077 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000078 if (getParent())
79 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000080}
81
Chris Lattnerd255ae22002-04-09 19:39:35 +000082//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000083// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000084//===----------------------------------------------------------------------===//
85
Chris Lattner379a8d22003-04-16 20:28:45 +000086Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000087 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000088 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000089 BasicBlocks.setItemParent(this);
90 BasicBlocks.setParent(this);
91 ArgumentList.setItemParent(this);
92 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000093 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000094
Chris Lattner3ae303c2003-11-21 22:32:23 +000095 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
96 && "LLVM functions cannot return aggregate values!");
97
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 Lattnered2fb1c2005-03-05 19:01:49 +0000122void Function::setName(const std::string &name) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000124 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125 Value::setName(name);
Chris Lattner570c69c2004-01-10 21:42:24 +0000126 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127}
128
Chris Lattner4e8c4872002-03-23 22:51:58 +0000129void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000130 if (getParent())
131 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000133 if (getParent())
134 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135}
136
Chris Lattner91db5822002-03-29 03:44:36 +0000137const FunctionType *Function::getFunctionType() const {
138 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000139}
140
Chris Lattner07b522d2005-01-07 07:40:32 +0000141bool Function::isVarArg() const {
142 return getFunctionType()->isVarArg();
143}
144
Chris Lattner4e8c4872002-03-23 22:51:58 +0000145const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000146 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147}
148
Chris Lattner02a71e72004-10-11 22:21:39 +0000149void Function::removeFromParent() {
150 getParent()->getFunctionList().remove(this);
151}
152
153void Function::eraseFromParent() {
154 getParent()->getFunctionList().erase(this);
155}
156
Chris Lattnerb392d302004-12-05 06:43:27 +0000157
158/// renameLocalSymbols - This method goes through the Function's symbol table
159/// and renames any symbols that conflict with symbols at global scope. This is
160/// required before printing out to a textual form, to ensure that there is no
161/// ambiguity when parsing.
162void Function::renameLocalSymbols() {
163 SymbolTable &LST = getSymbolTable(); // Local Symtab
164 SymbolTable &GST = getParent()->getSymbolTable(); // Global Symtab
165
166 for (SymbolTable::plane_iterator LPI = LST.plane_begin(), E = LST.plane_end();
167 LPI != E; ++LPI)
168 // All global symbols are of pointer type, ignore any non-pointer planes.
Chris Lattnered2fb1c2005-03-05 19:01:49 +0000169 if (const PointerType *CurTy = dyn_cast<PointerType>(LPI->first)) {
Chris Lattnerb392d302004-12-05 06:43:27 +0000170 // Only check if the global plane has any symbols of this type.
171 SymbolTable::plane_iterator GPI = GST.find(LPI->first);
172 if (GPI != GST.plane_end()) {
173 SymbolTable::ValueMap &LVM = LPI->second;
174 const SymbolTable::ValueMap &GVM = GPI->second;
175
176 // Loop over all local symbols, renaming those that are in the global
177 // symbol table already.
178 for (SymbolTable::value_iterator VI = LVM.begin(), E = LVM.end();
179 VI != E;) {
180 Value *V = VI->second;
181 const std::string &Name = VI->first;
182 ++VI;
183 if (GVM.count(Name)) {
184 static unsigned UniqueNum = 0;
185 // Find a name that does not conflict!
186 while (GVM.count(Name + "_" + utostr(++UniqueNum)) ||
187 LVM.count(Name + "_" + utostr(UniqueNum)))
188 /* scan for UniqueNum that works */;
189 V->setName(Name + "_" + utostr(UniqueNum));
190 }
191 }
192 }
193 }
194}
195
196
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197// dropAllReferences() - This function causes all the subinstructions to "let
198// go" of all references that they are maintaining. This allows one to
199// 'delete' a whole class at a time, even though there may be circular
200// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000201// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202// valid on an object that has "dropped all references", except operator
203// delete.
204//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000205void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000206 for (iterator I = begin(), E = end(); I != E; ++I)
207 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000208 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209}
Chris Lattnerda975502001-09-10 07:58:01 +0000210
Chris Lattnerbb346d02003-05-08 03:47:33 +0000211/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000212/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000213/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000214/// zero to allow easy checking for whether a function is intrinsic or not. The
215/// particular intrinsic functions which correspond to this value are defined in
216/// llvm/Intrinsics.h.
217///
218unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000219 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000220 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
221 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000222
223 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000224
225 switch (getName()[5]) {
Chris Lattner3d903f02004-01-05 05:36:30 +0000226 case 'd':
227 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
228 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
229 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
230 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
Chris Lattner59f1ef42004-01-06 05:33:02 +0000231 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
Chris Lattner3d903f02004-01-05 05:36:30 +0000232 break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000233 case 'f':
234 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
235 break;
Chris Lattner9c251eb2004-05-23 21:16:51 +0000236 case 'g':
237 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
238 if (getName() == "llvm.gcread") return Intrinsic::gcread;
239 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
240 break;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000241 case 'i':
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000242 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000243 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000244 case 'l':
Brian Gaeke960707c2003-11-11 22:41:34 +0000245 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000246 break;
Chris Lattner17d028d2004-02-12 17:01:09 +0000247 case 'm':
248 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
Chris Lattner5ed171e2004-02-12 18:11:20 +0000249 if (getName() == "llvm.memmove") return Intrinsic::memmove;
Chris Lattnerdc632112004-02-14 02:47:17 +0000250 if (getName() == "llvm.memset") return Intrinsic::memset;
251 break;
Chris Lattner1772c1a2005-02-28 19:28:00 +0000252 case 'p':
253 if (getName() == "llvm.prefetch") return Intrinsic::prefetch;
254 break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000255 case 'r':
256 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
John Criswell52010042004-04-08 20:27:38 +0000257 if (getName() == "llvm.readport") return Intrinsic::readport;
John Criswell23c48d62004-04-14 13:46:52 +0000258 if (getName() == "llvm.readio") return Intrinsic::readio;
Chris Lattner17d028d2004-02-12 17:01:09 +0000259 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000260 case 's':
Brian Gaeke960707c2003-11-11 22:41:34 +0000261 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
262 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
263 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000264 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000265 case 'v':
Chris Lattner071a5e52004-03-13 00:24:00 +0000266 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
267 if (getName() == "llvm.va_end") return Intrinsic::vaend;
268 if (getName() == "llvm.va_start") return Intrinsic::vastart;
John Criswell52010042004-04-08 20:27:38 +0000269 case 'w':
270 if (getName() == "llvm.writeport") return Intrinsic::writeport;
John Criswell23c48d62004-04-14 13:46:52 +0000271 if (getName() == "llvm.writeio") return Intrinsic::writeio;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000272 break;
273 }
274 // The "llvm." namespace is reserved!
275 assert(0 && "Unknown LLVM intrinsic function!");
276 return 0;
277}
278
Chris Lattner1ccab842004-10-12 04:32:37 +0000279Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000280 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
281 if (CE->getOpcode() == Instruction::Cast) {
282 if (isa<PointerType>(CE->getOperand(0)->getType()))
283 return StripPointerCasts(CE->getOperand(0));
284 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
285 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
286 if (!CE->getOperand(i)->isNullValue())
287 return Ptr;
288 return StripPointerCasts(CE->getOperand(0));
289 }
290 return Ptr;
291 }
292
293 if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
294 if (isa<PointerType>(CI->getOperand(0)->getType()))
295 return StripPointerCasts(CI->getOperand(0));
296 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattner1ccab842004-10-12 04:32:37 +0000297 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
298 if (!isa<Constant>(GEP->getOperand(i)) ||
299 !cast<Constant>(GEP->getOperand(i))->isNullValue())
Chris Lattner89046ca2004-10-12 04:20:25 +0000300 return Ptr;
Chris Lattner1ccab842004-10-12 04:32:37 +0000301 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000302 }
303 return Ptr;
304}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000305
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000306// vim: sw=2 ai