blob: bde6a6d6918a9787cf4b510627067ec3262ad54b [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"
Owen Anderson155dccd82009-07-07 23:43:39 +000017#include "llvm/LLVMContext.h"
Dan Gohman3a071482007-08-20 19:23:34 +000018#include "llvm/CodeGen/ValueTypes.h"
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000019#include "llvm/Support/CallSite.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Owen Andersonaab59c52009-06-17 22:23:31 +000021#include "llvm/Support/ManagedStatic.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000022#include "llvm/Support/StringPool.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000023#include "llvm/Support/RWMutex.h"
24#include "llvm/Support/Threading.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000025#include "SymbolTableListTraitsImpl.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000026#include "llvm/ADT/DenseMap.h"
Rafael Espindola2050af82011-05-16 03:05:33 +000027#include "llvm/ADT/STLExtras.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner113f4f42002-06-25 16:13:24 +000031
32// Explicit instantiations of SymbolTableListTraits since some of the methods
33// are not in the public header file...
John McCall086bb4e2009-12-19 00:55:12 +000034template class llvm::SymbolTableListTraits<Argument, Function>;
35template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000036
Chris Lattnerda975502001-09-10 07:58:01 +000037//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000038// Argument Implementation
39//===----------------------------------------------------------------------===//
40
Daniel Dunbar4975db62009-07-25 04:41:11 +000041Argument::Argument(const Type *Ty, const Twine &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000042 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000043 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000044
45 // Make sure that we get added to a function
46 LeakDetector::addGarbageObject(this);
47
Chris Lattner9ed7aef2002-09-06 21:33:15 +000048 if (Par)
49 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000050 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000051}
52
Chris Lattner9ed7aef2002-09-06 21:33:15 +000053void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000054 if (getParent())
55 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000056 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000057 if (getParent())
58 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000059}
60
Chris Lattnere30f09d2008-01-24 17:47:11 +000061/// getArgNo - Return the index of this formal argument in its containing
62/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
63unsigned Argument::getArgNo() const {
64 const Function *F = getParent();
65 assert(F && "Argument is not in a function");
66
67 Function::const_arg_iterator AI = F->arg_begin();
68 unsigned ArgIdx = 0;
69 for (; &*AI != this; ++AI)
70 ++ArgIdx;
71
72 return ArgIdx;
73}
74
75/// hasByValAttr - Return true if this argument has the byval attribute on it
76/// in its containing function.
77bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000078 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000079 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000080}
81
Chris Lattner4d37d992011-05-22 23:57:23 +000082unsigned Argument::getParamAlignment() const {
83 assert(getType()->isPointerTy() && "Only pointers have alignments");
84 return getParent()->getParamAlignment(getArgNo()+1);
85
86}
87
Duncan Sands5d96f3f2009-12-11 08:36:17 +000088/// hasNestAttr - Return true if this argument has the nest attribute on
89/// it in its containing function.
90bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000091 if (!getType()->isPointerTy()) return false;
Duncan Sands5d96f3f2009-12-11 08:36:17 +000092 return getParent()->paramHasAttr(getArgNo()+1, Attribute::Nest);
93}
94
Chris Lattnere30f09d2008-01-24 17:47:11 +000095/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
96/// it in its containing function.
97bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000098 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000099 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000100}
101
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000102/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
103/// on it in its containing function.
104bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000105 if (!getType()->isPointerTy()) return false;
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000106 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
107}
108
Owen Andersonc64dfb42008-02-17 23:22:28 +0000109/// hasSRetAttr - Return true if this argument has the sret attribute on
110/// it in its containing function.
111bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000112 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000113 if (this != getParent()->arg_begin())
114 return false; // StructRet param must be first param
Devang Patel4c758ea2008-09-25 21:00:45 +0000115 return getParent()->paramHasAttr(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000116}
117
Devang Patel4c758ea2008-09-25 21:00:45 +0000118/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000119void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000120 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000121}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000122
Devang Patel4c758ea2008-09-25 21:00:45 +0000123/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000124void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000125 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000126}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000127
128
Chris Lattnerd255ae22002-04-09 19:39:35 +0000129//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000131//===----------------------------------------------------------------------===//
132
Owen Anderson47db9412009-07-22 00:24:57 +0000133LLVMContext &Function::getContext() const {
134 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000135}
136
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000137FunctionType *Function::getFunctionType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000138 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000139}
140
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141bool Function::isVarArg() const {
142 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000143}
144
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000145Type *Function::getReturnType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000147}
148
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000149void Function::removeFromParent() {
150 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000151}
152
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000153void Function::eraseFromParent() {
154 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000155}
156
Reid Spencer019c8862007-04-09 15:01:12 +0000157//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000158// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000159//===----------------------------------------------------------------------===//
160
Chris Lattner379a8d22003-04-16 20:28:45 +0000161Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000162 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000163 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000164 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000165 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000166 "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000167 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000168
Chris Lattnere2de9082007-08-18 06:14:52 +0000169 // If the function has arguments, mark them as lazily built.
170 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000171 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000172
Chris Lattner184b2982002-09-08 18:59:35 +0000173 // Make sure that we get added to a function
174 LeakDetector::addGarbageObject(this);
175
Chris Lattner6213ae02002-09-06 20:46:32 +0000176 if (ParentModule)
177 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000178
179 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000180 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000181 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000182
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183}
184
Gordon Henriksen14a55692007-12-10 02:14:30 +0000185Function::~Function() {
186 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000189 ArgumentList.clear();
190 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000191
Gordon Henriksend930f912008-08-17 18:44:35 +0000192 // Remove the function from the on-the-side GC table.
193 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194}
195
Chris Lattnere2de9082007-08-18 06:14:52 +0000196void Function::BuildLazyArguments() const {
197 // Create the arguments vector, all arguments start out unnamed.
198 const FunctionType *FT = getFunctionType();
199 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000200 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000201 "Cannot have void typed arguments!");
202 ArgumentList.push_back(new Argument(FT->getParamType(i)));
203 }
204
205 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000206 unsigned SDC = getSubclassDataFromValue();
207 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000208}
209
210size_t Function::arg_size() const {
211 return getFunctionType()->getNumParams();
212}
213bool Function::arg_empty() const {
214 return getFunctionType()->getNumParams() == 0;
215}
216
Chris Lattner4e8c4872002-03-23 22:51:58 +0000217void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000218 if (getParent())
219 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000221 if (getParent())
222 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223}
224
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225// dropAllReferences() - This function causes all the subinstructions to "let
226// go" of all references that they are maintaining. This allows one to
227// 'delete' a whole class at a time, even though there may be circular
228// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000229// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000230// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231// delete.
232//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000233void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000234 for (iterator I = begin(), E = end(); I != E; ++I)
235 I->dropAllReferences();
Chris Lattner3a395302009-10-28 03:37:35 +0000236
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000237 // Delete all basic blocks. They are now unused, except possibly by
238 // blockaddresses, but BasicBlock's destructor takes care of those.
239 while (!BasicBlocks.empty())
240 BasicBlocks.begin()->eraseFromParent();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241}
Chris Lattnerda975502001-09-10 07:58:01 +0000242
Devang Patel4c758ea2008-09-25 21:00:45 +0000243void Function::addAttribute(unsigned i, Attributes attr) {
244 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000245 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000246 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000247}
248
Devang Patel4c758ea2008-09-25 21:00:45 +0000249void Function::removeAttribute(unsigned i, Attributes attr) {
250 AttrListPtr PAL = getAttributes();
Duncan Sands66336db2008-07-08 09:41:30 +0000251 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000252 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000253}
254
Gordon Henriksend930f912008-08-17 18:44:35 +0000255// Maintain the GC name for each function in an on-the-side table. This saves
256// allocating an additional word in Function for programs which do not use GC
257// (i.e., most programs) at the cost of increased overhead for clients which do
258// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000259static DenseMap<const Function*,PooledStringPtr> *GCNames;
260static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000261static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000262
Gordon Henriksend930f912008-08-17 18:44:35 +0000263bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000264 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000265 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000266}
267
Gordon Henriksend930f912008-08-17 18:44:35 +0000268const char *Function::getGC() const {
269 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000270 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000271 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000272}
273
Gordon Henriksend930f912008-08-17 18:44:35 +0000274void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000275 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000276 if (!GCNamePool)
277 GCNamePool = new StringPool();
278 if (!GCNames)
279 GCNames = new DenseMap<const Function*,PooledStringPtr>();
280 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000281}
282
Gordon Henriksend930f912008-08-17 18:44:35 +0000283void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000284 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000285 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000286 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000287 if (GCNames->empty()) {
288 delete GCNames;
289 GCNames = 0;
290 if (GCNamePool->empty()) {
291 delete GCNamePool;
292 GCNamePool = 0;
293 }
294 }
295 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000296}
297
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000298/// copyAttributesFrom - copy all additional attributes (those not needed to
299/// create a Function) from the Function Src to this one.
300void Function::copyAttributesFrom(const GlobalValue *Src) {
301 assert(isa<Function>(Src) && "Expected a Function!");
302 GlobalValue::copyAttributesFrom(Src);
303 const Function *SrcF = cast<Function>(Src);
304 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000305 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000306 if (SrcF->hasGC())
307 setGC(SrcF->getGC());
308 else
309 clearGC();
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000310}
311
Chris Lattnerbb346d02003-05-08 03:47:33 +0000312/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000313/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000314/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000315/// zero to allow easy checking for whether a function is intrinsic or not. The
316/// particular intrinsic functions which correspond to this value are defined in
317/// llvm/Intrinsics.h.
318///
Dale Johannesenb842d522009-02-05 01:49:45 +0000319unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000320 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000321 if (!ValName)
322 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000323 unsigned Len = ValName->getKeyLength();
324 const char *Name = ValName->getKeyData();
325
Reid Spencer78d71f12007-04-16 16:56:54 +0000326 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000327 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000328 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000329
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000330#define GET_FUNCTION_RECOGNIZER
331#include "llvm/Intrinsics.gen"
332#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000333 return 0;
334}
335
Jay Foadb804a2b2011-07-12 14:06:48 +0000336std::string Intrinsic::getName(ID id, Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000337 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000338 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000339 "not_intrinsic",
340#define GET_INTRINSIC_NAME_TABLE
341#include "llvm/Intrinsics.gen"
342#undef GET_INTRINSIC_NAME_TABLE
343 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000344 if (numTys == 0)
345 return Table[id];
346 std::string Result(Table[id]);
Mon P Wang2c839d42008-07-30 04:36:53 +0000347 for (unsigned i = 0; i < numTys; ++i) {
348 if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
349 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000350 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000351 }
352 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000353 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000354 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000355 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000356}
357
Owen Anderson155dccd82009-07-07 23:43:39 +0000358const FunctionType *Intrinsic::getType(LLVMContext &Context,
Jay Foadb804a2b2011-07-12 14:06:48 +0000359 ID id, Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000360 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000361 const Type *ResultTy = NULL;
Jay Foadb804a2b2011-07-12 14:06:48 +0000362 std::vector<Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000363 bool IsVarArg = false;
364
365#define GET_INTRINSIC_GENERATOR
366#include "llvm/Intrinsics.gen"
367#undef GET_INTRINSIC_GENERATOR
368
Owen Anderson4056ca92009-07-29 22:17:13 +0000369 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000370}
371
Mon P Wangb4024932009-02-24 23:17:49 +0000372bool Intrinsic::isOverloaded(ID id) {
Duncan Sandsd4ea3ec2011-04-26 07:30:10 +0000373 static const bool OTable[] = {
Mon P Wangb4024932009-02-24 23:17:49 +0000374 false,
375#define GET_INTRINSIC_OVERLOAD_TABLE
376#include "llvm/Intrinsics.gen"
377#undef GET_INTRINSIC_OVERLOAD_TABLE
378 };
379 return OTable[id];
380}
381
Chris Lattner49b7ee12009-01-12 01:18:58 +0000382/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000383#define GET_INTRINSIC_ATTRIBUTES
384#include "llvm/Intrinsics.gen"
385#undef GET_INTRINSIC_ATTRIBUTES
386
Jay Foadb804a2b2011-07-12 14:06:48 +0000387Function *Intrinsic::getDeclaration(Module *M, ID id, Type **Tys,
Reid Spencer2a2117c2007-04-01 07:25:33 +0000388 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000389 // There can never be multiple globals with the same name of different types,
390 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000391 return
Duncan Sands38ef3a82007-12-03 20:06:50 +0000392 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
Owen Anderson155dccd82009-07-07 23:43:39 +0000393 getType(M->getContext(),
394 id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000395}
396
Dale Johannesenb842d522009-02-05 01:49:45 +0000397// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
398#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
399#include "llvm/Intrinsics.gen"
400#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
401
Gabor Greif161cb042010-03-23 14:40:20 +0000402/// hasAddressTaken - returns true if there are any uses of this function
403/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000404bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000405 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000406 const User *U = *I;
407 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
408 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000409 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000410 if (!CS.isCallee(I))
411 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000412 }
413 return false;
414}
415
Rafael Espindola2050af82011-05-16 03:05:33 +0000416/// callsFunctionThatReturnsTwice - Return true if the function has a call to
417/// setjmp or other function that gcc recognizes as "returning twice".
418///
419/// FIXME: Remove after <rdar://problem/8031714> is fixed.
Jay Foadafdfed32011-06-17 13:36:06 +0000420/// FIXME: Is the above FIXME valid?
Rafael Espindola2050af82011-05-16 03:05:33 +0000421bool Function::callsFunctionThatReturnsTwice() const {
422 const Module *M = this->getParent();
423 static const char *ReturnsTwiceFns[] = {
424 "_setjmp",
425 "setjmp",
426 "sigsetjmp",
427 "setjmp_syscall",
428 "savectx",
429 "qsetjmp",
430 "vfork",
431 "getcontext"
432 };
433
434 for (unsigned I = 0; I < array_lengthof(ReturnsTwiceFns); ++I)
435 if (const Function *Callee = M->getFunction(ReturnsTwiceFns[I])) {
436 if (!Callee->use_empty())
437 for (Value::const_use_iterator
438 I = Callee->use_begin(), E = Callee->use_end();
439 I != E; ++I)
440 if (const CallInst *CI = dyn_cast<CallInst>(*I))
441 if (CI->getParent()->getParent() == this)
442 return true;
443 }
444
445 return false;
446}
447
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000448// vim: sw=2 ai