blob: 6c29371862652585285c8daaea452cbc3a492563 [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"
Dan Gohman3a071482007-08-20 19:23:34 +000018#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.h"
Reid Spencer4388f0b2007-04-22 05:46:44 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattnerf6c93e32005-01-30 00:09:23 +000025BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000026 BasicBlock *Ret = new BasicBlock();
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
29 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000030}
31
Chris Lattner113f4f42002-06-25 16:13:24 +000032iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
33 return F->getBasicBlockList();
34}
35
Chris Lattnerf6c93e32005-01-30 00:09:23 +000036Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000037 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000038 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
40 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000041}
42
43iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
44 return F->getArgumentList();
45}
46
47// Explicit instantiations of SymbolTableListTraits since some of the methods
48// are not in the public header file...
Chris Lattnerb47aa542007-04-17 03:26:42 +000049template class SymbolTableListTraits<Argument, Function>;
50template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000051
Chris Lattnerda975502001-09-10 07:58:01 +000052//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000053// Argument Implementation
54//===----------------------------------------------------------------------===//
55
Misha Brukmanb1c93172005-04-21 23:48:37 +000056Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000057 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000058 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000059
60 // Make sure that we get added to a function
61 LeakDetector::addGarbageObject(this);
62
Chris Lattner9ed7aef2002-09-06 21:33:15 +000063 if (Par)
64 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000065 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000066}
67
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000069 if (getParent())
70 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000071 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000072 if (getParent())
73 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000074}
75
Chris Lattnerd255ae22002-04-09 19:39:35 +000076//===----------------------------------------------------------------------===//
Reid Spencer019c8862007-04-09 15:01:12 +000077// ParamAttrsList Implementation
78//===----------------------------------------------------------------------===//
79
80uint16_t
81ParamAttrsList::getParamAttrs(uint16_t Index) const {
82 unsigned limit = attrs.size();
83 for (unsigned i = 0; i < limit; ++i)
84 if (attrs[i].index == Index)
85 return attrs[i].attrs;
Reid Spencera472f662007-04-11 02:44:20 +000086 return ParamAttr::None;
Reid Spencer019c8862007-04-09 15:01:12 +000087}
88
89
90std::string
91ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
92 std::string Result;
Reid Spencera472f662007-04-11 02:44:20 +000093 if (Attrs & ParamAttr::ZExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000094 Result += "zeroext ";
Reid Spencera472f662007-04-11 02:44:20 +000095 if (Attrs & ParamAttr::SExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000096 Result += "signext ";
Reid Spencera472f662007-04-11 02:44:20 +000097 if (Attrs & ParamAttr::NoReturn)
Reid Spencer019c8862007-04-09 15:01:12 +000098 Result += "noreturn ";
Reid Spencera472f662007-04-11 02:44:20 +000099 if (Attrs & ParamAttr::NoUnwind)
Reid Spencer019c8862007-04-09 15:01:12 +0000100 Result += "nounwind ";
Reid Spencera472f662007-04-11 02:44:20 +0000101 if (Attrs & ParamAttr::InReg)
Reid Spencer019c8862007-04-09 15:01:12 +0000102 Result += "inreg ";
Zhou Sheng2444a9a2007-06-05 05:28:26 +0000103 if (Attrs & ParamAttr::NoAlias)
104 Result += "noalias ";
Reid Spencera472f662007-04-11 02:44:20 +0000105 if (Attrs & ParamAttr::StructRet)
Reid Spencer019c8862007-04-09 15:01:12 +0000106 Result += "sret ";
Rafael Espindolab567e3f2007-07-06 10:57:03 +0000107 if (Attrs & ParamAttr::ByVal)
108 Result += "byval ";
Duncan Sands644f9172007-07-27 12:58:54 +0000109 if (Attrs & ParamAttr::Nest)
110 Result += "nest ";
Duncan Sandsa89a1132007-11-22 20:23:04 +0000111 if (Attrs & ParamAttr::ReadNone)
112 Result += "readnone ";
113 if (Attrs & ParamAttr::ReadOnly)
114 Result += "readonly ";
Reid Spencer019c8862007-04-09 15:01:12 +0000115 return Result;
116}
117
Reid Spencer4388f0b2007-04-22 05:46:44 +0000118void
119ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
120 for (unsigned i = 0; i < attrs.size(); ++i) {
121 unsigned val = attrs[i].attrs << 16 | attrs[i].index;
122 ID.AddInteger(val);
123 }
Reid Spencer019c8862007-04-09 15:01:12 +0000124}
125
Reid Spencer4388f0b2007-04-22 05:46:44 +0000126static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
Reid Spencer019c8862007-04-09 15:01:12 +0000127
Reid Spencer4388f0b2007-04-22 05:46:44 +0000128ParamAttrsList *
129ParamAttrsList::get(const ParamAttrsVector &attrVec) {
130 assert(!attrVec.empty() && "Illegal to create empty ParamAttrsList");
Duncan Sandsd7813352007-09-11 14:40:04 +0000131#ifndef NDEBUG
Duncan Sands04eb67e2007-11-20 14:09:29 +0000132 for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
133 assert(attrVec[i].attrs != ParamAttr::None
134 && "Pointless parameter attribute!");
135 assert((!i || attrVec[i-1].index < attrVec[i].index)
136 && "Misordered ParamAttrsList!");
137 }
Duncan Sandsd7813352007-09-11 14:40:04 +0000138#endif
Reid Spencer4388f0b2007-04-22 05:46:44 +0000139 ParamAttrsList key(attrVec);
140 FoldingSetNodeID ID;
141 key.Profile(ID);
142 void *InsertPos;
143 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
144 if (!PAL) {
145 PAL = new ParamAttrsList(attrVec);
146 ParamAttrsLists->InsertNode(PAL, InsertPos);
147 }
148 return PAL;
Reid Spencer019c8862007-04-09 15:01:12 +0000149}
150
Reid Spencerc6a83842007-04-22 17:28:03 +0000151ParamAttrsList::~ParamAttrsList() {
152 ParamAttrsLists->RemoveNode(this);
153}
154
Reid Spencer019c8862007-04-09 15:01:12 +0000155//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000156// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000157//===----------------------------------------------------------------------===//
158
Chris Lattner379a8d22003-04-16 20:28:45 +0000159Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000160 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000161 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Reid Spencer019c8862007-04-09 15:01:12 +0000162 ParamAttrs = 0;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000163 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000164
Chris Lattner3ae303c2003-11-21 22:32:23 +0000165 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
166 && "LLVM functions cannot return aggregate values!");
167
Chris Lattnere2de9082007-08-18 06:14:52 +0000168 // If the function has arguments, mark them as lazily built.
169 if (Ty->getNumParams())
170 SubclassData = 1; // Set the "has lazy arguments" bit.
171
Chris Lattner184b2982002-09-08 18:59:35 +0000172 // Make sure that we get added to a function
173 LeakDetector::addGarbageObject(this);
174
Chris Lattner6213ae02002-09-06 20:46:32 +0000175 if (ParentModule)
176 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177}
178
Chris Lattner4e8c4872002-03-23 22:51:58 +0000179Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180 dropAllReferences(); // After this it is safe to delete instructions.
181
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000183 ArgumentList.clear();
Chris Lattner2c8ff632002-04-28 04:51:51 +0000184 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000185
186 // Drop our reference to the parameter attributes, if any.
187 if (ParamAttrs)
188 ParamAttrs->dropRef();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189}
190
Chris Lattnere2de9082007-08-18 06:14:52 +0000191void Function::BuildLazyArguments() const {
192 // Create the arguments vector, all arguments start out unnamed.
193 const FunctionType *FT = getFunctionType();
194 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
195 assert(FT->getParamType(i) != Type::VoidTy &&
196 "Cannot have void typed arguments!");
197 ArgumentList.push_back(new Argument(FT->getParamType(i)));
198 }
199
200 // Clear the lazy arguments bit.
201 const_cast<Function*>(this)->SubclassData &= ~1;
202}
203
204size_t Function::arg_size() const {
205 return getFunctionType()->getNumParams();
206}
207bool Function::arg_empty() const {
208 return getFunctionType()->getNumParams() == 0;
209}
210
Chris Lattner4e8c4872002-03-23 22:51:58 +0000211void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000212 if (getParent())
213 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000215 if (getParent())
216 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217}
218
Reid Spencerc6a83842007-04-22 17:28:03 +0000219void Function::setParamAttrs(ParamAttrsList *attrs) {
220 if (ParamAttrs)
221 ParamAttrs->dropRef();
222
223 if (attrs)
224 attrs->addRef();
225
226 ParamAttrs = attrs;
227}
228
Chris Lattner91db5822002-03-29 03:44:36 +0000229const FunctionType *Function::getFunctionType() const {
230 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000231}
232
Chris Lattner07b522d2005-01-07 07:40:32 +0000233bool Function::isVarArg() const {
234 return getFunctionType()->isVarArg();
235}
236
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000238 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattner02a71e72004-10-11 22:21:39 +0000241void Function::removeFromParent() {
242 getParent()->getFunctionList().remove(this);
243}
244
245void Function::eraseFromParent() {
246 getParent()->getFunctionList().erase(this);
247}
248
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249// dropAllReferences() - This function causes all the subinstructions to "let
250// go" of all references that they are maintaining. This allows one to
251// 'delete' a whole class at a time, even though there may be circular
252// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000253// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255// delete.
256//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000257void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000258 for (iterator I = begin(), E = end(); I != E; ++I)
259 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000260 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261}
Chris Lattnerda975502001-09-10 07:58:01 +0000262
Chris Lattnerbb346d02003-05-08 03:47:33 +0000263/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000264/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000265/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000266/// zero to allow easy checking for whether a function is intrinsic or not. The
267/// particular intrinsic functions which correspond to this value are defined in
268/// llvm/Intrinsics.h.
269///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000270unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000271 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000272 if (!ValName)
273 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000274 unsigned Len = ValName->getKeyLength();
275 const char *Name = ValName->getKeyData();
276
Reid Spencer78d71f12007-04-16 16:56:54 +0000277 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000278 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000279 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000280
Reid Spencer9c2eec32007-04-16 06:54:34 +0000281 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000282
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000283#define GET_FUNCTION_RECOGNIZER
284#include "llvm/Intrinsics.gen"
285#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000286 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000287 return 0;
288}
289
Reid Spencer2a2117c2007-04-01 07:25:33 +0000290std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000291 assert(id < num_intrinsics && "Invalid intrinsic ID!");
292 const char * const Table[] = {
293 "not_intrinsic",
294#define GET_INTRINSIC_NAME_TABLE
295#include "llvm/Intrinsics.gen"
296#undef GET_INTRINSIC_NAME_TABLE
297 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000298 if (numTys == 0)
299 return Table[id];
300 std::string Result(Table[id]);
301 for (unsigned i = 0; i < numTys; ++i)
302 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000303 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000304 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000305}
306
Reid Spencer2a2117c2007-04-01 07:25:33 +0000307const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000308 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000309 const Type *ResultTy = NULL;
310 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000311 bool IsVarArg = false;
312
313#define GET_INTRINSIC_GENERATOR
314#include "llvm/Intrinsics.gen"
315#undef GET_INTRINSIC_GENERATOR
316
Reid Spencer26d9ff62007-04-09 06:11:23 +0000317 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000318}
319
Reid Spencer2a2117c2007-04-01 07:25:33 +0000320Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
321 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000322// There can never be multiple globals with the same name of different types,
323// because intrinsics must be a specific type.
Reid Spencer2a2117c2007-04-01 07:25:33 +0000324 return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
325 getType(id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000326}
327
Chris Lattner1ccab842004-10-12 04:32:37 +0000328Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000329 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000330 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000331 if (isa<PointerType>(CE->getOperand(0)->getType()))
332 return StripPointerCasts(CE->getOperand(0));
333 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
334 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
335 if (!CE->getOperand(i)->isNullValue())
336 return Ptr;
337 return StripPointerCasts(CE->getOperand(0));
338 }
339 return Ptr;
340 }
341
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000342 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000343 if (isa<PointerType>(CI->getOperand(0)->getType()))
344 return StripPointerCasts(CI->getOperand(0));
345 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000346 if (GEP->hasAllZeroIndices())
347 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000348 }
349 return Ptr;
350}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000351
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000352// vim: sw=2 ai