blob: ce2186f5cd094ae7569651f3ec1f10ed7c0383d6 [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
Duncan Sands5d96f3f2009-12-11 08:36:17 +000082/// hasNestAttr - Return true if this argument has the nest attribute on
83/// it in its containing function.
84bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000085 if (!getType()->isPointerTy()) return false;
Duncan Sands5d96f3f2009-12-11 08:36:17 +000086 return getParent()->paramHasAttr(getArgNo()+1, Attribute::Nest);
87}
88
Chris Lattnere30f09d2008-01-24 17:47:11 +000089/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
90/// it in its containing function.
91bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000092 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000093 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +000094}
95
Duncan Sandsdf128eb2008-12-31 18:08:59 +000096/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
97/// on it in its containing function.
98bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000099 if (!getType()->isPointerTy()) return false;
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000100 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
101}
102
Owen Andersonc64dfb42008-02-17 23:22:28 +0000103/// hasSRetAttr - Return true if this argument has the sret attribute on
104/// it in its containing function.
105bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000106 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000107 if (this != getParent()->arg_begin())
108 return false; // StructRet param must be first param
Devang Patel4c758ea2008-09-25 21:00:45 +0000109 return getParent()->paramHasAttr(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000110}
111
Devang Patel4c758ea2008-09-25 21:00:45 +0000112/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000113void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000114 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000115}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000116
Devang Patel4c758ea2008-09-25 21:00:45 +0000117/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000118void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000119 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000120}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000121
122
Chris Lattnerd255ae22002-04-09 19:39:35 +0000123//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000125//===----------------------------------------------------------------------===//
126
Owen Anderson47db9412009-07-22 00:24:57 +0000127LLVMContext &Function::getContext() const {
128 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000129}
130
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000131const FunctionType *Function::getFunctionType() const {
132 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000133}
134
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000135bool Function::isVarArg() const {
136 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000137}
138
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000139const Type *Function::getReturnType() const {
140 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000141}
142
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000143void Function::removeFromParent() {
144 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000145}
146
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147void Function::eraseFromParent() {
148 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000149}
150
Reid Spencer019c8862007-04-09 15:01:12 +0000151//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000152// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000153//===----------------------------------------------------------------------===//
154
Chris Lattner379a8d22003-04-16 20:28:45 +0000155Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000156 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000157 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000158 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000159 assert(FunctionType::isValidReturnType(getReturnType()) &&
Duncan Sandscbd43f82010-02-16 14:50:09 +0000160 !getReturnType()->isOpaqueTy() && "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000161 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000162
Chris Lattnere2de9082007-08-18 06:14:52 +0000163 // If the function has arguments, mark them as lazily built.
164 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000165 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000166
Chris Lattner184b2982002-09-08 18:59:35 +0000167 // Make sure that we get added to a function
168 LeakDetector::addGarbageObject(this);
169
Chris Lattner6213ae02002-09-06 20:46:32 +0000170 if (ParentModule)
171 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000172
173 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000174 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000175 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000176
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177}
178
Gordon Henriksen14a55692007-12-10 02:14:30 +0000179Function::~Function() {
180 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000183 ArgumentList.clear();
184 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000185
Gordon Henriksend930f912008-08-17 18:44:35 +0000186 // Remove the function from the on-the-side GC table.
187 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188}
189
Chris Lattnere2de9082007-08-18 06:14:52 +0000190void Function::BuildLazyArguments() const {
191 // Create the arguments vector, all arguments start out unnamed.
192 const FunctionType *FT = getFunctionType();
193 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000194 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000195 "Cannot have void typed arguments!");
196 ArgumentList.push_back(new Argument(FT->getParamType(i)));
197 }
198
199 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000200 unsigned SDC = getSubclassDataFromValue();
201 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000202}
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
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 Lattner3a395302009-10-28 03:37:35 +0000230
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000231 // Delete all basic blocks. They are now unused, except possibly by
232 // blockaddresses, but BasicBlock's destructor takes care of those.
233 while (!BasicBlocks.empty())
234 BasicBlocks.begin()->eraseFromParent();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235}
Chris Lattnerda975502001-09-10 07:58:01 +0000236
Devang Patel4c758ea2008-09-25 21:00:45 +0000237void Function::addAttribute(unsigned i, Attributes attr) {
238 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000239 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000240 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000241}
242
Devang Patel4c758ea2008-09-25 21:00:45 +0000243void Function::removeAttribute(unsigned i, Attributes attr) {
244 AttrListPtr PAL = getAttributes();
Duncan Sands66336db2008-07-08 09:41:30 +0000245 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000246 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000247}
248
Gordon Henriksend930f912008-08-17 18:44:35 +0000249// Maintain the GC name for each function in an on-the-side table. This saves
250// allocating an additional word in Function for programs which do not use GC
251// (i.e., most programs) at the cost of increased overhead for clients which do
252// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000253static DenseMap<const Function*,PooledStringPtr> *GCNames;
254static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000255static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000256
Gordon Henriksend930f912008-08-17 18:44:35 +0000257bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000258 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000259 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000260}
261
Gordon Henriksend930f912008-08-17 18:44:35 +0000262const char *Function::getGC() const {
263 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000264 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000265 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000266}
267
Gordon Henriksend930f912008-08-17 18:44:35 +0000268void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000269 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000270 if (!GCNamePool)
271 GCNamePool = new StringPool();
272 if (!GCNames)
273 GCNames = new DenseMap<const Function*,PooledStringPtr>();
274 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000275}
276
Gordon Henriksend930f912008-08-17 18:44:35 +0000277void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000278 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000279 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000280 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000281 if (GCNames->empty()) {
282 delete GCNames;
283 GCNames = 0;
284 if (GCNamePool->empty()) {
285 delete GCNamePool;
286 GCNamePool = 0;
287 }
288 }
289 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000290}
291
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000292/// copyAttributesFrom - copy all additional attributes (those not needed to
293/// create a Function) from the Function Src to this one.
294void Function::copyAttributesFrom(const GlobalValue *Src) {
295 assert(isa<Function>(Src) && "Expected a Function!");
296 GlobalValue::copyAttributesFrom(Src);
297 const Function *SrcF = cast<Function>(Src);
298 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000299 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000300 if (SrcF->hasGC())
301 setGC(SrcF->getGC());
302 else
303 clearGC();
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000304}
305
Chris Lattnerbb346d02003-05-08 03:47:33 +0000306/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000307/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000308/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000309/// zero to allow easy checking for whether a function is intrinsic or not. The
310/// particular intrinsic functions which correspond to this value are defined in
311/// llvm/Intrinsics.h.
312///
Dale Johannesenb842d522009-02-05 01:49:45 +0000313unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000314 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000315 if (!ValName)
316 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000317 unsigned Len = ValName->getKeyLength();
318 const char *Name = ValName->getKeyData();
319
Reid Spencer78d71f12007-04-16 16:56:54 +0000320 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000321 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000322 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000323
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000324#define GET_FUNCTION_RECOGNIZER
325#include "llvm/Intrinsics.gen"
326#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000327 return 0;
328}
329
Reid Spencer2a2117c2007-04-01 07:25:33 +0000330std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000331 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000332 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000333 "not_intrinsic",
334#define GET_INTRINSIC_NAME_TABLE
335#include "llvm/Intrinsics.gen"
336#undef GET_INTRINSIC_NAME_TABLE
337 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000338 if (numTys == 0)
339 return Table[id];
340 std::string Result(Table[id]);
Mon P Wang2c839d42008-07-30 04:36:53 +0000341 for (unsigned i = 0; i < numTys; ++i) {
342 if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
343 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000344 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000345 }
346 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000347 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000348 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000349 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000350}
351
Owen Anderson155dccd82009-07-07 23:43:39 +0000352const FunctionType *Intrinsic::getType(LLVMContext &Context,
353 ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000354 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000355 const Type *ResultTy = NULL;
356 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000357 bool IsVarArg = false;
358
359#define GET_INTRINSIC_GENERATOR
360#include "llvm/Intrinsics.gen"
361#undef GET_INTRINSIC_GENERATOR
362
Owen Anderson4056ca92009-07-29 22:17:13 +0000363 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000364}
365
Mon P Wangb4024932009-02-24 23:17:49 +0000366bool Intrinsic::isOverloaded(ID id) {
Duncan Sandsd4ea3ec2011-04-26 07:30:10 +0000367 static const bool OTable[] = {
Mon P Wangb4024932009-02-24 23:17:49 +0000368 false,
369#define GET_INTRINSIC_OVERLOAD_TABLE
370#include "llvm/Intrinsics.gen"
371#undef GET_INTRINSIC_OVERLOAD_TABLE
372 };
373 return OTable[id];
374}
375
Chris Lattner49b7ee12009-01-12 01:18:58 +0000376/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000377#define GET_INTRINSIC_ATTRIBUTES
378#include "llvm/Intrinsics.gen"
379#undef GET_INTRINSIC_ATTRIBUTES
380
Reid Spencer2a2117c2007-04-01 07:25:33 +0000381Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
382 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000383 // There can never be multiple globals with the same name of different types,
384 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000385 return
Duncan Sands38ef3a82007-12-03 20:06:50 +0000386 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
Owen Anderson155dccd82009-07-07 23:43:39 +0000387 getType(M->getContext(),
388 id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000389}
390
Dale Johannesenb842d522009-02-05 01:49:45 +0000391// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
392#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
393#include "llvm/Intrinsics.gen"
394#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
395
Gabor Greif161cb042010-03-23 14:40:20 +0000396/// hasAddressTaken - returns true if there are any uses of this function
397/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000398bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000399 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000400 const User *U = *I;
401 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
402 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000403 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000404 if (!CS.isCallee(I))
405 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000406 }
407 return false;
408}
409
Rafael Espindola2050af82011-05-16 03:05:33 +0000410/// callsFunctionThatReturnsTwice - Return true if the function has a call to
411/// setjmp or other function that gcc recognizes as "returning twice".
412///
413/// FIXME: Remove after <rdar://problem/8031714> is fixed.
414/// FIXME: Is the obove FIXME valid?
415bool Function::callsFunctionThatReturnsTwice() const {
416 const Module *M = this->getParent();
417 static const char *ReturnsTwiceFns[] = {
418 "_setjmp",
419 "setjmp",
420 "sigsetjmp",
421 "setjmp_syscall",
422 "savectx",
423 "qsetjmp",
424 "vfork",
425 "getcontext"
426 };
427
428 for (unsigned I = 0; I < array_lengthof(ReturnsTwiceFns); ++I)
429 if (const Function *Callee = M->getFunction(ReturnsTwiceFns[I])) {
430 if (!Callee->use_empty())
431 for (Value::const_use_iterator
432 I = Callee->use_begin(), E = Callee->use_end();
433 I != E; ++I)
434 if (const CallInst *CI = dyn_cast<CallInst>(*I))
435 if (CI->getParent()->getParent() == this)
436 return true;
437 }
438
439 return false;
440}
441
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000442// vim: sw=2 ai