blob: 6571f10be5dadd053c91050f756091d3d53bc64b [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//
Chris Lattnerf3ebc3f2007-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 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"
Chris Lattner89046ca2004-10-12 04:20:25 +000016#include "llvm/IntrinsicInst.h"
Dan Gohman3a071482007-08-20 19:23:34 +000017#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000019#include "llvm/Support/StringPool.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000021#include "llvm/ADT/BitVector.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattnerf6c93e32005-01-30 00:09:23 +000026BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Gabor Greife9ecc682008-04-06 20:25:17 +000027 BasicBlock *Ret = BasicBlock::Create();
Chris Lattner184b2982002-09-08 18:59:35 +000028 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
30 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000031}
32
Chris Lattner113f4f42002-06-25 16:13:24 +000033iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
34 return F->getBasicBlockList();
35}
36
Chris Lattnerf6c93e32005-01-30 00:09:23 +000037Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000038 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000039 // This should not be garbage monitored.
40 LeakDetector::removeGarbageObject(Ret);
41 return Ret;
Chris Lattner113f4f42002-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 Lattnerb47aa542007-04-17 03:26:42 +000050template class SymbolTableListTraits<Argument, Function>;
51template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000052
Chris Lattnerda975502001-09-10 07:58:01 +000053//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000054// Argument Implementation
55//===----------------------------------------------------------------------===//
56
Misha Brukmanb1c93172005-04-21 23:48:37 +000057Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000058 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000059 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000060
61 // Make sure that we get added to a function
62 LeakDetector::addGarbageObject(this);
63
Chris Lattner9ed7aef2002-09-06 21:33:15 +000064 if (Par)
65 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000066 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000067}
68
Chris Lattner9ed7aef2002-09-06 21:33:15 +000069void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000070 if (getParent())
71 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000072 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000073 if (getParent())
74 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000075}
76
Chris Lattnere30f09d2008-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 Andersonc64dfb42008-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 Andersona54570d2008-02-18 09:22:21 +0000109 if (this != getParent()->arg_begin()) return false; // StructRet param must be first param
Owen Andersonc66655e2008-02-18 04:06:26 +0000110 return getParent()->paramHasAttr(1, ParamAttr::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000111}
112
Chris Lattnere30f09d2008-01-24 17:47:11 +0000113
114
115
Chris Lattnerd255ae22002-04-09 19:39:35 +0000116//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000117// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000118//===----------------------------------------------------------------------===//
119
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000120const FunctionType *Function::getFunctionType() const {
121 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000122}
123
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124bool Function::isVarArg() const {
125 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000126}
127
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000128const Type *Function::getReturnType() const {
129 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000130}
131
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132void Function::removeFromParent() {
133 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000134}
135
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136void Function::eraseFromParent() {
137 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000138}
139
Reid Spencer019c8862007-04-09 15:01:12 +0000140//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000141// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000142//===----------------------------------------------------------------------===//
143
Chris Lattner379a8d22003-04-16 20:28:45 +0000144Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000145 const std::string &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000146 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000147 Value::FunctionVal, 0, 0, Linkage, name) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000148 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000149
Devang Patel616f0e02008-02-20 22:36:03 +0000150 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy
Devang Patel9fea0192008-02-21 22:24:17 +0000151 || isa<StructType>(getReturnType()))
Chris Lattner3ae303c2003-11-21 22:32:23 +0000152 && "LLVM functions cannot return aggregate values!");
153
Chris Lattnere2de9082007-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 Lattner184b2982002-09-08 18:59:35 +0000158 // Make sure that we get added to a function
159 LeakDetector::addGarbageObject(this);
160
Chris Lattner6213ae02002-09-06 20:46:32 +0000161 if (ParentModule)
162 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000163
164 // Ensure intrinsics have the right parameter attributes.
165 if (unsigned IID = getIntrinsicID(true))
166 setParamAttrs(Intrinsic::getParamAttrs(Intrinsic::ID(IID)));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167}
168
Gordon Henriksen14a55692007-12-10 02:14:30 +0000169Function::~Function() {
170 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000173 ArgumentList.clear();
174 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000175
Gordon Henriksen71183b62007-12-10 03:18:06 +0000176 // Remove the function from the on-the-side collector table.
177 clearCollector();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178}
179
Chris Lattnere2de9082007-08-18 06:14:52 +0000180void Function::BuildLazyArguments() const {
181 // Create the arguments vector, all arguments start out unnamed.
182 const FunctionType *FT = getFunctionType();
183 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
184 assert(FT->getParamType(i) != Type::VoidTy &&
185 "Cannot have void typed arguments!");
186 ArgumentList.push_back(new Argument(FT->getParamType(i)));
187 }
188
189 // Clear the lazy arguments bit.
190 const_cast<Function*>(this)->SubclassData &= ~1;
191}
192
193size_t Function::arg_size() const {
194 return getFunctionType()->getNumParams();
195}
196bool Function::arg_empty() const {
197 return getFunctionType()->getNumParams() == 0;
198}
199
Chris Lattner4e8c4872002-03-23 22:51:58 +0000200void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000201 if (getParent())
202 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000204 if (getParent())
205 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206}
207
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208// dropAllReferences() - This function causes all the subinstructions to "let
209// go" of all references that they are maintaining. This allows one to
210// 'delete' a whole class at a time, even though there may be circular
211// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000212// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000213// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214// delete.
215//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000216void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000217 for (iterator I = begin(), E = end(); I != E; ++I)
218 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000219 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220}
Chris Lattnerda975502001-09-10 07:58:01 +0000221
Gordon Henriksen71183b62007-12-10 03:18:06 +0000222// Maintain the collector name for each function in an on-the-side table. This
223// saves allocating an additional word in Function for programs which do not use
224// GC (i.e., most programs) at the cost of increased overhead for clients which
225// do use GC.
226static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
227static StringPool *CollectorNamePool;
228
229bool Function::hasCollector() const {
230 return CollectorNames && CollectorNames->count(this);
231}
232
233const char *Function::getCollector() const {
234 assert(hasCollector() && "Function has no collector");
235 return *(*CollectorNames)[this];
236}
237
238void Function::setCollector(const char *Str) {
239 if (!CollectorNamePool)
240 CollectorNamePool = new StringPool();
241 if (!CollectorNames)
242 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
243 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
244}
245
246void Function::clearCollector() {
247 if (CollectorNames) {
248 CollectorNames->erase(this);
249 if (CollectorNames->empty()) {
250 delete CollectorNames;
251 CollectorNames = 0;
Gordon Henriksen4b904b92007-12-10 03:35:18 +0000252 if (CollectorNamePool->empty()) {
253 delete CollectorNamePool;
254 CollectorNamePool = 0;
255 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000256 }
257 }
258}
259
Chris Lattnerbb346d02003-05-08 03:47:33 +0000260/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000261/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000262/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000263/// zero to allow easy checking for whether a function is intrinsic or not. The
264/// particular intrinsic functions which correspond to this value are defined in
265/// llvm/Intrinsics.h.
266///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000267unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000268 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000269 if (!ValName)
270 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000271 unsigned Len = ValName->getKeyLength();
272 const char *Name = ValName->getKeyData();
273
Reid Spencer78d71f12007-04-16 16:56:54 +0000274 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000275 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000276 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000277
Reid Spencer9c2eec32007-04-16 06:54:34 +0000278 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000280#define GET_FUNCTION_RECOGNIZER
281#include "llvm/Intrinsics.gen"
282#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000283 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000284 return 0;
285}
286
Reid Spencer2a2117c2007-04-01 07:25:33 +0000287std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000288 assert(id < num_intrinsics && "Invalid intrinsic ID!");
289 const char * const Table[] = {
290 "not_intrinsic",
291#define GET_INTRINSIC_NAME_TABLE
292#include "llvm/Intrinsics.gen"
293#undef GET_INTRINSIC_NAME_TABLE
294 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000295 if (numTys == 0)
296 return Table[id];
297 std::string Result(Table[id]);
298 for (unsigned i = 0; i < numTys; ++i)
299 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000300 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000301 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000302}
303
Reid Spencer2a2117c2007-04-01 07:25:33 +0000304const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000305 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000306 const Type *ResultTy = NULL;
307 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000308 bool IsVarArg = false;
309
310#define GET_INTRINSIC_GENERATOR
311#include "llvm/Intrinsics.gen"
312#undef GET_INTRINSIC_GENERATOR
313
Reid Spencer26d9ff62007-04-09 06:11:23 +0000314 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000315}
316
Chris Lattner8a923e72008-03-12 17:45:29 +0000317PAListPtr Intrinsic::getParamAttrs(ID id) {
Dale Johannesen89268bc2008-02-19 21:38:47 +0000318 ParameterAttributes Attr = ParamAttr::None;
Duncan Sands38ef3a82007-12-03 20:06:50 +0000319
320#define GET_INTRINSIC_ATTRIBUTES
321#include "llvm/Intrinsics.gen"
322#undef GET_INTRINSIC_ATTRIBUTES
323
324 // Intrinsics cannot throw exceptions.
325 Attr |= ParamAttr::NoUnwind;
326
Chris Lattner8a923e72008-03-12 17:45:29 +0000327 ParamAttrsWithIndex PAWI = ParamAttrsWithIndex::get(0, Attr);
328 return PAListPtr::get(&PAWI, 1);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000329}
330
Reid Spencer2a2117c2007-04-01 07:25:33 +0000331Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
332 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000333 // There can never be multiple globals with the same name of different types,
334 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000335 return
Duncan Sands38ef3a82007-12-03 20:06:50 +0000336 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
337 getType(id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000338}
339
Chris Lattner1ccab842004-10-12 04:32:37 +0000340Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000341 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000342 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000343 if (isa<PointerType>(CE->getOperand(0)->getType()))
344 return StripPointerCasts(CE->getOperand(0));
345 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
346 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
347 if (!CE->getOperand(i)->isNullValue())
348 return Ptr;
349 return StripPointerCasts(CE->getOperand(0));
350 }
351 return Ptr;
352 }
353
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000354 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000355 if (isa<PointerType>(CI->getOperand(0)->getType()))
356 return StripPointerCasts(CI->getOperand(0));
357 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000358 if (GEP->hasAllZeroIndices())
359 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000360 }
361 return Ptr;
362}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000363
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000364// vim: sw=2 ai