blob: 302eff30ff3f2cb01ee86132925ad2d0163da5cc [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Reid Spenceref9b9a72007-02-05 20:47:22 +000010// This file implements the Function class for the VMCore library.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner48270152002-09-06 20:46:32 +000015#include "llvm/DerivedTypes.h"
Chris Lattner4ec82eb2004-10-12 04:20:25 +000016#include "llvm/IntrinsicInst.h"
Dan Gohman7abff312007-08-20 19:23:34 +000017#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Gordon Henriksen80a75bf2007-12-10 03:18:06 +000019#include "llvm/Support/StringPool.h"
Chris Lattner7e708292002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
Duncan Sandsa3355ff2007-12-03 20:06:50 +000021#include "llvm/ADT/BitVector.h"
Gordon Henriksen80a75bf2007-12-10 03:18:06 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattnercaa4ae72004-12-05 06:43:27 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner31f84992003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerbca81442005-01-30 00:09:23 +000026BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Gabor Greif051a9502008-04-06 20:25:17 +000027 BasicBlock *Ret = BasicBlock::Create();
Chris Lattnerd1e693f2002-09-08 18:59:35 +000028 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
30 return Ret;
Chris Lattnerbded1322002-09-06 21:33:15 +000031}
32
Chris Lattner7e708292002-06-25 16:13:24 +000033iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
34 return F->getBasicBlockList();
35}
36
Chris Lattnerbca81442005-01-30 00:09:23 +000037Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer79e21d32006-12-31 05:26:44 +000038 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattnerd1e693f2002-09-08 18:59:35 +000039 // This should not be garbage monitored.
40 LeakDetector::removeGarbageObject(Ret);
41 return Ret;
Chris Lattner7e708292002-06-25 16:13:24 +000042}
43
44iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
45 return F->getArgumentList();
46}
47
48// Explicit instantiations of SymbolTableListTraits since some of the methods
49// are not in the public header file...
Chris Lattner17fcdd52007-04-17 03:26:42 +000050template class SymbolTableListTraits<Argument, Function>;
51template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattner70cc3392001-09-10 07:58:01 +000053//===----------------------------------------------------------------------===//
Chris Lattner80dd50b2002-04-09 19:39:35 +000054// Argument Implementation
55//===----------------------------------------------------------------------===//
56
Misha Brukmanfd939082005-04-21 23:48:37 +000057Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattnerdec628e2007-02-12 05:18:08 +000058 : Value(Ty, Value::ArgumentVal) {
Chris Lattnerbded1322002-09-06 21:33:15 +000059 Parent = 0;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000060
61 // Make sure that we get added to a function
62 LeakDetector::addGarbageObject(this);
63
Chris Lattnerbded1322002-09-06 21:33:15 +000064 if (Par)
65 Par->getArgumentList().push_back(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +000066 setName(Name);
Chris Lattnerbded1322002-09-06 21:33:15 +000067}
68
Chris Lattnerbded1322002-09-06 21:33:15 +000069void Argument::setParent(Function *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000070 if (getParent())
71 LeakDetector::addGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000072 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000073 if (getParent())
74 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000075}
76
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000077/// getArgNo - Return the index of this formal argument in its containing
78/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
79unsigned Argument::getArgNo() const {
80 const Function *F = getParent();
81 assert(F && "Argument is not in a function");
82
83 Function::const_arg_iterator AI = F->arg_begin();
84 unsigned ArgIdx = 0;
85 for (; &*AI != this; ++AI)
86 ++ArgIdx;
87
88 return ArgIdx;
89}
90
91/// hasByValAttr - Return true if this argument has the byval attribute on it
92/// in its containing function.
93bool Argument::hasByValAttr() const {
94 if (!isa<PointerType>(getType())) return false;
95 return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::ByVal);
96}
97
98/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
99/// it in its containing function.
100bool Argument::hasNoAliasAttr() const {
101 if (!isa<PointerType>(getType())) return false;
102 return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::NoAlias);
103}
104
Owen Anderson7d542542008-02-17 23:22:28 +0000105/// hasSRetAttr - Return true if this argument has the sret attribute on
106/// it in its containing function.
107bool Argument::hasStructRetAttr() const {
108 if (!isa<PointerType>(getType())) return false;
Owen Anderson874a8922008-02-18 09:22:21 +0000109 if (this != getParent()->arg_begin()) return false; // StructRet param must be first param
Owen Anderson9eb948b2008-02-18 04:06:26 +0000110 return getParent()->paramHasAttr(1, ParamAttr::StructRet);
Owen Anderson7d542542008-02-17 23:22:28 +0000111}
112
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000113
114
115
Chris Lattner80dd50b2002-04-09 19:39:35 +0000116//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000117// Helper Methods in Function
Reid Spencer4746ecf2007-04-09 15:01:12 +0000118//===----------------------------------------------------------------------===//
119
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000120const FunctionType *Function::getFunctionType() const {
121 return cast<FunctionType>(getType()->getElementType());
Reid Spencer4746ecf2007-04-09 15:01:12 +0000122}
123
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000124bool Function::isVarArg() const {
125 return getFunctionType()->isVarArg();
Reid Spencer4746ecf2007-04-09 15:01:12 +0000126}
127
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000128const Type *Function::getReturnType() const {
129 return getFunctionType()->getReturnType();
Duncan Sands827cde12007-11-25 14:10:56 +0000130}
131
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000132void Function::removeFromParent() {
133 getParent()->getFunctionList().remove(this);
Duncan Sands827cde12007-11-25 14:10:56 +0000134}
135
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000136void Function::eraseFromParent() {
137 getParent()->getFunctionList().erase(this);
Reid Spencer4746ecf2007-04-09 15:01:12 +0000138}
139
Reid Spencer4746ecf2007-04-09 15:01:12 +0000140//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +0000141// Function Implementation
Chris Lattner70cc3392001-09-10 07:58:01 +0000142//===----------------------------------------------------------------------===//
143
Chris Lattner4ad02e72003-04-16 20:28:45 +0000144Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner48270152002-09-06 20:46:32 +0000145 const std::string &name, Module *ParentModule)
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000146 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner58d74912008-03-12 17:45:29 +0000147 Value::FunctionVal, 0, 0, Linkage, name) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000148 SymTab = new ValueSymbolTable();
Chris Lattner48270152002-09-06 20:46:32 +0000149
Devang Patel93f9d572008-02-20 22:36:03 +0000150 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy
Devang Patel6c94b702008-02-21 22:24:17 +0000151 || isa<StructType>(getReturnType()))
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000152 && "LLVM functions cannot return aggregate values!");
153
Chris Lattner0162c182007-08-18 06:14:52 +0000154 // If the function has arguments, mark them as lazily built.
155 if (Ty->getNumParams())
156 SubclassData = 1; // Set the "has lazy arguments" bit.
157
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000158 // Make sure that we get added to a function
159 LeakDetector::addGarbageObject(this);
160
Chris Lattner48270152002-09-06 20:46:32 +0000161 if (ParentModule)
162 ParentModule->getFunctionList().push_back(this);
Chris Lattner00950542001-06-06 20:29:01 +0000163}
164
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000165Function::~Function() {
166 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000169 ArgumentList.clear();
170 delete SymTab;
Reid Spencerb90909e2007-04-22 17:28:03 +0000171
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000172 // Remove the function from the on-the-side collector table.
173 clearCollector();
Chris Lattner00950542001-06-06 20:29:01 +0000174}
175
Chris Lattner0162c182007-08-18 06:14:52 +0000176void Function::BuildLazyArguments() const {
177 // Create the arguments vector, all arguments start out unnamed.
178 const FunctionType *FT = getFunctionType();
179 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
180 assert(FT->getParamType(i) != Type::VoidTy &&
181 "Cannot have void typed arguments!");
182 ArgumentList.push_back(new Argument(FT->getParamType(i)));
183 }
184
185 // Clear the lazy arguments bit.
186 const_cast<Function*>(this)->SubclassData &= ~1;
187}
188
189size_t Function::arg_size() const {
190 return getFunctionType()->getNumParams();
191}
192bool Function::arg_empty() const {
193 return getFunctionType()->getNumParams() == 0;
194}
195
Chris Lattnere7506a32002-03-23 22:51:58 +0000196void Function::setParent(Module *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000197 if (getParent())
198 LeakDetector::addGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000199 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000200 if (getParent())
201 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000202}
203
Chris Lattner00950542001-06-06 20:29:01 +0000204// dropAllReferences() - This function causes all the subinstructions to "let
205// go" of all references that they are maintaining. This allows one to
206// 'delete' a whole class at a time, even though there may be circular
207// references... first all references are dropped, and all use counts go to
Misha Brukman6b634522003-10-10 17:54:14 +0000208// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanfd939082005-04-21 23:48:37 +0000209// valid on an object that has "dropped all references", except operator
Chris Lattner00950542001-06-06 20:29:01 +0000210// delete.
211//
Chris Lattnere7506a32002-03-23 22:51:58 +0000212void Function::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000213 for (iterator I = begin(), E = end(); I != E; ++I)
214 I->dropAllReferences();
Chris Lattner0981b622003-09-17 04:58:59 +0000215 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner00950542001-06-06 20:29:01 +0000216}
Chris Lattner70cc3392001-09-10 07:58:01 +0000217
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000218// Maintain the collector name for each function in an on-the-side table. This
219// saves allocating an additional word in Function for programs which do not use
220// GC (i.e., most programs) at the cost of increased overhead for clients which
221// do use GC.
222static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
223static StringPool *CollectorNamePool;
224
225bool Function::hasCollector() const {
226 return CollectorNames && CollectorNames->count(this);
227}
228
229const char *Function::getCollector() const {
230 assert(hasCollector() && "Function has no collector");
231 return *(*CollectorNames)[this];
232}
233
234void Function::setCollector(const char *Str) {
235 if (!CollectorNamePool)
236 CollectorNamePool = new StringPool();
237 if (!CollectorNames)
238 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
239 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
240}
241
242void Function::clearCollector() {
243 if (CollectorNames) {
244 CollectorNames->erase(this);
245 if (CollectorNames->empty()) {
246 delete CollectorNames;
247 CollectorNames = 0;
Gordon Henriksen53c34b12007-12-10 03:35:18 +0000248 if (CollectorNamePool->empty()) {
249 delete CollectorNamePool;
250 CollectorNamePool = 0;
251 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000252 }
253 }
254}
255
Chris Lattnerdd035d12003-05-08 03:47:33 +0000256/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeked0fde302003-11-11 22:41:34 +0000257/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukman6b634522003-10-10 17:54:14 +0000258/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerdd035d12003-05-08 03:47:33 +0000259/// zero to allow easy checking for whether a function is intrinsic or not. The
260/// particular intrinsic functions which correspond to this value are defined in
261/// llvm/Intrinsics.h.
262///
Reid Spencer2db15e22007-04-16 06:54:34 +0000263unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner3b515802007-02-15 19:17:16 +0000264 const ValueName *ValName = this->getValueName();
Reid Spencer085659f2007-04-16 07:08:44 +0000265 if (!ValName)
266 return 0;
Chris Lattner3b515802007-02-15 19:17:16 +0000267 unsigned Len = ValName->getKeyLength();
268 const char *Name = ValName->getKeyData();
269
Reid Spencer9c15de12007-04-16 16:56:54 +0000270 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencer0b118202006-01-16 21:12:35 +0000271 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerdd035d12003-05-08 03:47:33 +0000272 return 0; // All intrinsics start with 'llvm.'
Chris Lattner095e9072003-09-19 19:31:41 +0000273
Reid Spencer2db15e22007-04-16 06:54:34 +0000274 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanfd939082005-04-21 23:48:37 +0000275
Chris Lattner9a016ff2006-03-09 20:35:01 +0000276#define GET_FUNCTION_RECOGNIZER
277#include "llvm/Intrinsics.gen"
278#undef GET_FUNCTION_RECOGNIZER
Reid Spencer2db15e22007-04-16 06:54:34 +0000279 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerdd035d12003-05-08 03:47:33 +0000280 return 0;
281}
282
Reid Spencer1437a092007-04-01 07:25:33 +0000283std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattnerb8474232006-03-25 06:32:47 +0000284 assert(id < num_intrinsics && "Invalid intrinsic ID!");
285 const char * const Table[] = {
286 "not_intrinsic",
287#define GET_INTRINSIC_NAME_TABLE
288#include "llvm/Intrinsics.gen"
289#undef GET_INTRINSIC_NAME_TABLE
290 };
Reid Spencer1437a092007-04-01 07:25:33 +0000291 if (numTys == 0)
292 return Table[id];
293 std::string Result(Table[id]);
294 for (unsigned i = 0; i < numTys; ++i)
295 if (Tys[i])
Dan Gohman7abff312007-08-20 19:23:34 +0000296 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer1437a092007-04-01 07:25:33 +0000297 return Result;
Chris Lattnerb8474232006-03-25 06:32:47 +0000298}
299
Reid Spencer1437a092007-04-01 07:25:33 +0000300const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner1405d002007-06-05 23:49:06 +0000301 unsigned numTys) {
Jim Laskey95af5922007-02-07 20:38:26 +0000302 const Type *ResultTy = NULL;
303 std::vector<const Type*> ArgTys;
Jim Laskey95af5922007-02-07 20:38:26 +0000304 bool IsVarArg = false;
305
306#define GET_INTRINSIC_GENERATOR
307#include "llvm/Intrinsics.gen"
308#undef GET_INTRINSIC_GENERATOR
309
Reid Spencerac66f422007-04-09 06:11:23 +0000310 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey95af5922007-02-07 20:38:26 +0000311}
312
Chris Lattner58d74912008-03-12 17:45:29 +0000313PAListPtr Intrinsic::getParamAttrs(ID id) {
Dale Johannesen0d51e7e2008-02-19 21:38:47 +0000314 ParameterAttributes Attr = ParamAttr::None;
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000315
316#define GET_INTRINSIC_ATTRIBUTES
317#include "llvm/Intrinsics.gen"
318#undef GET_INTRINSIC_ATTRIBUTES
319
320 // Intrinsics cannot throw exceptions.
321 Attr |= ParamAttr::NoUnwind;
322
Chris Lattner58d74912008-03-12 17:45:29 +0000323 ParamAttrsWithIndex PAWI = ParamAttrsWithIndex::get(0, Attr);
324 return PAListPtr::get(&PAWI, 1);
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000325}
326
Reid Spencer1437a092007-04-01 07:25:33 +0000327Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
328 unsigned numTys) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000329 // There can never be multiple globals with the same name of different types,
330 // because intrinsics must be a specific type.
331 Function *F =
332 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
333 getType(id, Tys, numTys)));
334 F->setParamAttrs(getParamAttrs(id));
335 return F;
Jim Laskey95af5922007-02-07 20:38:26 +0000336}
337
Chris Lattnera28809d2004-10-12 04:32:37 +0000338Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner4ec82eb2004-10-12 04:20:25 +0000339 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000340 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner4ec82eb2004-10-12 04:20:25 +0000341 if (isa<PointerType>(CE->getOperand(0)->getType()))
342 return StripPointerCasts(CE->getOperand(0));
343 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
344 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
345 if (!CE->getOperand(i)->isNullValue())
346 return Ptr;
347 return StripPointerCasts(CE->getOperand(0));
348 }
349 return Ptr;
350 }
351
Reid Spencer3da59db2006-11-27 01:05:10 +0000352 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner4ec82eb2004-10-12 04:20:25 +0000353 if (isa<PointerType>(CI->getOperand(0)->getType()))
354 return StripPointerCasts(CI->getOperand(0));
355 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattner1a34b7b2007-04-25 05:49:09 +0000356 if (GEP->hasAllZeroIndices())
357 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner4ec82eb2004-10-12 04:20:25 +0000358 }
359 return Ptr;
360}
Chris Lattnerdd035d12003-05-08 03:47:33 +0000361
Reid Spencerbb905152004-07-17 23:50:19 +0000362// vim: sw=2 ai