blob: 1374d55e7d9c755bd5d8d3bf75a8e787b834f458 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000010// This file implements the Function class for the VMCore library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000015#include "llvm/DerivedTypes.h"
Reid Spencer26d9ff62007-04-09 06:11:23 +000016#include "llvm/ParameterAttributes.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"
Reid Spencer4388f0b2007-04-22 05:46:44 +000019#include "llvm/Support/ManagedStatic.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattnerf6c93e32005-01-30 00:09:23 +000024BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
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
Chris Lattnerf6c93e32005-01-30 00:09:23 +000035Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000036 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000037 // 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 Lattnerb47aa542007-04-17 03:26:42 +000048template class SymbolTableListTraits<Argument, Function>;
49template class SymbolTableListTraits<BasicBlock, 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
Misha Brukmanb1c93172005-04-21 23:48:37 +000055Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000056 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000057 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);
Chris Lattner32ab6432007-02-12 05:18:08 +000064 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000065}
66
Chris Lattner9ed7aef2002-09-06 21:33:15 +000067void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000068 if (getParent())
69 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000071 if (getParent())
72 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073}
74
Chris Lattnerd255ae22002-04-09 19:39:35 +000075//===----------------------------------------------------------------------===//
Reid Spencer019c8862007-04-09 15:01:12 +000076// ParamAttrsList Implementation
77//===----------------------------------------------------------------------===//
78
79uint16_t
80ParamAttrsList::getParamAttrs(uint16_t Index) const {
81 unsigned limit = attrs.size();
82 for (unsigned i = 0; i < limit; ++i)
83 if (attrs[i].index == Index)
84 return attrs[i].attrs;
Reid Spencera472f662007-04-11 02:44:20 +000085 return ParamAttr::None;
Reid Spencer019c8862007-04-09 15:01:12 +000086}
87
88
89std::string
90ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
91 std::string Result;
Reid Spencera472f662007-04-11 02:44:20 +000092 if (Attrs & ParamAttr::ZExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000093 Result += "zeroext ";
Reid Spencera472f662007-04-11 02:44:20 +000094 if (Attrs & ParamAttr::SExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000095 Result += "signext ";
Reid Spencera472f662007-04-11 02:44:20 +000096 if (Attrs & ParamAttr::NoReturn)
Reid Spencer019c8862007-04-09 15:01:12 +000097 Result += "noreturn ";
Reid Spencera472f662007-04-11 02:44:20 +000098 if (Attrs & ParamAttr::NoUnwind)
Reid Spencer019c8862007-04-09 15:01:12 +000099 Result += "nounwind ";
Reid Spencera472f662007-04-11 02:44:20 +0000100 if (Attrs & ParamAttr::InReg)
Reid Spencer019c8862007-04-09 15:01:12 +0000101 Result += "inreg ";
Zhou Sheng2444a9a2007-06-05 05:28:26 +0000102 if (Attrs & ParamAttr::NoAlias)
103 Result += "noalias ";
Reid Spencera472f662007-04-11 02:44:20 +0000104 if (Attrs & ParamAttr::StructRet)
Reid Spencer019c8862007-04-09 15:01:12 +0000105 Result += "sret ";
Rafael Espindolab567e3f2007-07-06 10:57:03 +0000106 if (Attrs & ParamAttr::ByVal)
107 Result += "byval ";
Duncan Sands644f9172007-07-27 12:58:54 +0000108 if (Attrs & ParamAttr::Nest)
109 Result += "nest ";
Reid Spencer019c8862007-04-09 15:01:12 +0000110 return Result;
111}
112
Reid Spencer4388f0b2007-04-22 05:46:44 +0000113void
114ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
115 for (unsigned i = 0; i < attrs.size(); ++i) {
116 unsigned val = attrs[i].attrs << 16 | attrs[i].index;
117 ID.AddInteger(val);
118 }
Reid Spencer019c8862007-04-09 15:01:12 +0000119}
120
Reid Spencer4388f0b2007-04-22 05:46:44 +0000121static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
Reid Spencer019c8862007-04-09 15:01:12 +0000122
Reid Spencer4388f0b2007-04-22 05:46:44 +0000123ParamAttrsList *
124ParamAttrsList::get(const ParamAttrsVector &attrVec) {
125 assert(!attrVec.empty() && "Illegal to create empty ParamAttrsList");
126 ParamAttrsList key(attrVec);
127 FoldingSetNodeID ID;
128 key.Profile(ID);
129 void *InsertPos;
130 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
131 if (!PAL) {
132 PAL = new ParamAttrsList(attrVec);
133 ParamAttrsLists->InsertNode(PAL, InsertPos);
134 }
135 return PAL;
Reid Spencer019c8862007-04-09 15:01:12 +0000136}
137
Reid Spencerc6a83842007-04-22 17:28:03 +0000138ParamAttrsList::~ParamAttrsList() {
139 ParamAttrsLists->RemoveNode(this);
140}
141
Reid Spencer019c8862007-04-09 15:01:12 +0000142//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000143// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000144//===----------------------------------------------------------------------===//
145
Chris Lattner379a8d22003-04-16 20:28:45 +0000146Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000147 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000148 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Reid Spencer019c8862007-04-09 15:01:12 +0000149 ParamAttrs = 0;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000150 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000151
Chris Lattner3ae303c2003-11-21 22:32:23 +0000152 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
153 && "LLVM functions cannot return aggregate values!");
154
Chris Lattner149376d2002-10-13 20:57:00 +0000155 // Create the arguments vector, all arguments start out unnamed.
156 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
157 assert(Ty->getParamType(i) != Type::VoidTy &&
158 "Cannot have void typed arguments!");
159 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
160 }
161
Chris Lattner184b2982002-09-08 18:59:35 +0000162 // Make sure that we get added to a function
163 LeakDetector::addGarbageObject(this);
164
Chris Lattner6213ae02002-09-06 20:46:32 +0000165 if (ParentModule)
166 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167}
168
Chris Lattner4e8c4872002-03-23 22:51:58 +0000169Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170 dropAllReferences(); // After this it is safe to delete instructions.
171
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000173 ArgumentList.clear();
Chris Lattner2c8ff632002-04-28 04:51:51 +0000174 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000175
176 // Drop our reference to the parameter attributes, if any.
177 if (ParamAttrs)
178 ParamAttrs->dropRef();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179}
180
Chris Lattner4e8c4872002-03-23 22:51:58 +0000181void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000182 if (getParent())
183 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000185 if (getParent())
186 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187}
188
Reid Spencerc6a83842007-04-22 17:28:03 +0000189void Function::setParamAttrs(ParamAttrsList *attrs) {
190 if (ParamAttrs)
191 ParamAttrs->dropRef();
192
193 if (attrs)
194 attrs->addRef();
195
196 ParamAttrs = attrs;
197}
198
Chris Lattner91db5822002-03-29 03:44:36 +0000199const FunctionType *Function::getFunctionType() const {
200 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000201}
202
Chris Lattner07b522d2005-01-07 07:40:32 +0000203bool Function::isVarArg() const {
204 return getFunctionType()->isVarArg();
205}
206
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000208 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209}
210
Chris Lattner02a71e72004-10-11 22:21:39 +0000211void Function::removeFromParent() {
212 getParent()->getFunctionList().remove(this);
213}
214
215void Function::eraseFromParent() {
216 getParent()->getFunctionList().erase(this);
217}
218
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219// dropAllReferences() - This function causes all the subinstructions to "let
220// go" of all references that they are maintaining. This allows one to
221// 'delete' a whole class at a time, even though there may be circular
222// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000223// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000224// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225// delete.
226//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000227void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000228 for (iterator I = begin(), E = end(); I != E; ++I)
229 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000230 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
Chris Lattnerda975502001-09-10 07:58:01 +0000232
Chris Lattnerbb346d02003-05-08 03:47:33 +0000233/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000234/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000235/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000236/// zero to allow easy checking for whether a function is intrinsic or not. The
237/// particular intrinsic functions which correspond to this value are defined in
238/// llvm/Intrinsics.h.
239///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000240unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000241 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000242 if (!ValName)
243 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000244 unsigned Len = ValName->getKeyLength();
245 const char *Name = ValName->getKeyData();
246
Reid Spencer78d71f12007-04-16 16:56:54 +0000247 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000248 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000249 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000250
Reid Spencer9c2eec32007-04-16 06:54:34 +0000251 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000252
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000253#define GET_FUNCTION_RECOGNIZER
254#include "llvm/Intrinsics.gen"
255#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000256 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000257 return 0;
258}
259
Reid Spencer2a2117c2007-04-01 07:25:33 +0000260std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000261 assert(id < num_intrinsics && "Invalid intrinsic ID!");
262 const char * const Table[] = {
263 "not_intrinsic",
264#define GET_INTRINSIC_NAME_TABLE
265#include "llvm/Intrinsics.gen"
266#undef GET_INTRINSIC_NAME_TABLE
267 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000268 if (numTys == 0)
269 return Table[id];
270 std::string Result(Table[id]);
271 for (unsigned i = 0; i < numTys; ++i)
272 if (Tys[i])
273 Result += "." + Tys[i]->getDescription();
274 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000275}
276
Reid Spencer2a2117c2007-04-01 07:25:33 +0000277const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000278 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000279 const Type *ResultTy = NULL;
280 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000281 bool IsVarArg = false;
282
283#define GET_INTRINSIC_GENERATOR
284#include "llvm/Intrinsics.gen"
285#undef GET_INTRINSIC_GENERATOR
286
Reid Spencer26d9ff62007-04-09 06:11:23 +0000287 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000288}
289
Reid Spencer2a2117c2007-04-01 07:25:33 +0000290Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
291 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000292// There can never be multiple globals with the same name of different types,
293// because intrinsics must be a specific type.
Reid Spencer2a2117c2007-04-01 07:25:33 +0000294 return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
295 getType(id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000296}
297
Chris Lattner1ccab842004-10-12 04:32:37 +0000298Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000299 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000300 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000301 if (isa<PointerType>(CE->getOperand(0)->getType()))
302 return StripPointerCasts(CE->getOperand(0));
303 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
304 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
305 if (!CE->getOperand(i)->isNullValue())
306 return Ptr;
307 return StripPointerCasts(CE->getOperand(0));
308 }
309 return Ptr;
310 }
311
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000312 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000313 if (isa<PointerType>(CI->getOperand(0)->getType()))
314 return StripPointerCasts(CI->getOperand(0));
315 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000316 if (GEP->hasAllZeroIndices())
317 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000318 }
319 return Ptr;
320}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000321
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000322// vim: sw=2 ai