blob: 22f0096e538bf802ccdf874531a3204f9e4b41db [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"
Chris Lattnerb392d302004-12-05 06:43:27 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner113f4f42002-06-25 16:13:24 +000030
31// Explicit instantiations of SymbolTableListTraits since some of the methods
32// are not in the public header file...
John McCall086bb4e2009-12-19 00:55:12 +000033template class llvm::SymbolTableListTraits<Argument, Function>;
34template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000035
Chris Lattnerda975502001-09-10 07:58:01 +000036//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000037// Argument Implementation
38//===----------------------------------------------------------------------===//
39
Daniel Dunbar4975db62009-07-25 04:41:11 +000040Argument::Argument(const Type *Ty, const Twine &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000041 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000042 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000043
44 // Make sure that we get added to a function
45 LeakDetector::addGarbageObject(this);
46
Chris Lattner9ed7aef2002-09-06 21:33:15 +000047 if (Par)
48 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000049 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000050}
51
Chris Lattner9ed7aef2002-09-06 21:33:15 +000052void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000053 if (getParent())
54 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000055 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000056 if (getParent())
57 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000058}
59
Chris Lattnere30f09d2008-01-24 17:47:11 +000060/// getArgNo - Return the index of this formal argument in its containing
61/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
62unsigned Argument::getArgNo() const {
63 const Function *F = getParent();
64 assert(F && "Argument is not in a function");
65
66 Function::const_arg_iterator AI = F->arg_begin();
67 unsigned ArgIdx = 0;
68 for (; &*AI != this; ++AI)
69 ++ArgIdx;
70
71 return ArgIdx;
72}
73
74/// hasByValAttr - Return true if this argument has the byval attribute on it
75/// in its containing function.
76bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000077 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000078 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000079}
80
Duncan Sands5d96f3f2009-12-11 08:36:17 +000081/// hasNestAttr - Return true if this argument has the nest attribute on
82/// it in its containing function.
83bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000084 if (!getType()->isPointerTy()) return false;
Duncan Sands5d96f3f2009-12-11 08:36:17 +000085 return getParent()->paramHasAttr(getArgNo()+1, Attribute::Nest);
86}
87
Chris Lattnere30f09d2008-01-24 17:47:11 +000088/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
89/// it in its containing function.
90bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000091 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000092 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +000093}
94
Duncan Sandsdf128eb2008-12-31 18:08:59 +000095/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
96/// on it in its containing function.
97bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000098 if (!getType()->isPointerTy()) return false;
Duncan Sandsdf128eb2008-12-31 18:08:59 +000099 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
100}
101
Owen Andersonc64dfb42008-02-17 23:22:28 +0000102/// hasSRetAttr - Return true if this argument has the sret attribute on
103/// it in its containing function.
104bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000105 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000106 if (this != getParent()->arg_begin())
107 return false; // StructRet param must be first param
Devang Patel4c758ea2008-09-25 21:00:45 +0000108 return getParent()->paramHasAttr(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000109}
110
Devang Patel4c758ea2008-09-25 21:00:45 +0000111/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000112void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000113 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000114}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000115
Devang Patel4c758ea2008-09-25 21:00:45 +0000116/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000117void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000118 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000119}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000120
121
Chris Lattnerd255ae22002-04-09 19:39:35 +0000122//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000123// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000124//===----------------------------------------------------------------------===//
125
Owen Anderson47db9412009-07-22 00:24:57 +0000126LLVMContext &Function::getContext() const {
127 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000128}
129
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130const FunctionType *Function::getFunctionType() const {
131 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000132}
133
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134bool Function::isVarArg() const {
135 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000136}
137
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000138const Type *Function::getReturnType() const {
139 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000140}
141
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142void Function::removeFromParent() {
143 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000144}
145
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146void Function::eraseFromParent() {
147 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000148}
149
Reid Spencer019c8862007-04-09 15:01:12 +0000150//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000151// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000152//===----------------------------------------------------------------------===//
153
Chris Lattner379a8d22003-04-16 20:28:45 +0000154Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000155 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000156 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000157 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000158 assert(FunctionType::isValidReturnType(getReturnType()) &&
Duncan Sandscbd43f82010-02-16 14:50:09 +0000159 !getReturnType()->isOpaqueTy() && "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000160 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000161
Chris Lattnere2de9082007-08-18 06:14:52 +0000162 // If the function has arguments, mark them as lazily built.
163 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000164 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000165
Chris Lattner184b2982002-09-08 18:59:35 +0000166 // Make sure that we get added to a function
167 LeakDetector::addGarbageObject(this);
168
Chris Lattner6213ae02002-09-06 20:46:32 +0000169 if (ParentModule)
170 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000171
172 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000173 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000174 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000175
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176}
177
Gordon Henriksen14a55692007-12-10 02:14:30 +0000178Function::~Function() {
179 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000182 ArgumentList.clear();
183 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000184
Gordon Henriksend930f912008-08-17 18:44:35 +0000185 // Remove the function from the on-the-side GC table.
186 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187}
188
Chris Lattnere2de9082007-08-18 06:14:52 +0000189void Function::BuildLazyArguments() const {
190 // Create the arguments vector, all arguments start out unnamed.
191 const FunctionType *FT = getFunctionType();
192 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000193 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000194 "Cannot have void typed arguments!");
195 ArgumentList.push_back(new Argument(FT->getParamType(i)));
196 }
197
198 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000199 unsigned SDC = getSubclassDataFromValue();
200 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000201}
202
203size_t Function::arg_size() const {
204 return getFunctionType()->getNumParams();
205}
206bool Function::arg_empty() const {
207 return getFunctionType()->getNumParams() == 0;
208}
209
Chris Lattner4e8c4872002-03-23 22:51:58 +0000210void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000211 if (getParent())
212 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000214 if (getParent())
215 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216}
217
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218// dropAllReferences() - This function causes all the subinstructions to "let
219// go" of all references that they are maintaining. This allows one to
220// 'delete' a whole class at a time, even though there may be circular
221// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000222// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000223// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224// delete.
225//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000226void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000227 for (iterator I = begin(), E = end(); I != E; ++I)
228 I->dropAllReferences();
Chris Lattner3a395302009-10-28 03:37:35 +0000229
230 // Delete all basic blocks.
231 while (!BasicBlocks.empty()) {
232 // If there is still a reference to the block, it must be a 'blockaddress'
233 // constant pointing to it. Just replace the BlockAddress with undef.
234 BasicBlock *BB = BasicBlocks.begin();
235 if (!BB->use_empty()) {
236 BlockAddress *BA = cast<BlockAddress>(BB->use_back());
237 BA->replaceAllUsesWith(UndefValue::get(BA->getType()));
238 BA->destroyConstant();
239 }
240
241 BB->eraseFromParent();
242 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243}
Chris Lattnerda975502001-09-10 07:58:01 +0000244
Devang Patel4c758ea2008-09-25 21:00:45 +0000245void Function::addAttribute(unsigned i, Attributes attr) {
246 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000247 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000248 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000249}
250
Devang Patel4c758ea2008-09-25 21:00:45 +0000251void Function::removeAttribute(unsigned i, Attributes attr) {
252 AttrListPtr PAL = getAttributes();
Duncan Sands66336db2008-07-08 09:41:30 +0000253 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000254 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000255}
256
Gordon Henriksend930f912008-08-17 18:44:35 +0000257// Maintain the GC name for each function in an on-the-side table. This saves
258// allocating an additional word in Function for programs which do not use GC
259// (i.e., most programs) at the cost of increased overhead for clients which do
260// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000261static DenseMap<const Function*,PooledStringPtr> *GCNames;
262static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000263static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000264
Gordon Henriksend930f912008-08-17 18:44:35 +0000265bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000266 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000267 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000268}
269
Gordon Henriksend930f912008-08-17 18:44:35 +0000270const char *Function::getGC() const {
271 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000272 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000273 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000274}
275
Gordon Henriksend930f912008-08-17 18:44:35 +0000276void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000277 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000278 if (!GCNamePool)
279 GCNamePool = new StringPool();
280 if (!GCNames)
281 GCNames = new DenseMap<const Function*,PooledStringPtr>();
282 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000283}
284
Gordon Henriksend930f912008-08-17 18:44:35 +0000285void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000286 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000287 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000288 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000289 if (GCNames->empty()) {
290 delete GCNames;
291 GCNames = 0;
292 if (GCNamePool->empty()) {
293 delete GCNamePool;
294 GCNamePool = 0;
295 }
296 }
297 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000298}
299
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000300/// copyAttributesFrom - copy all additional attributes (those not needed to
301/// create a Function) from the Function Src to this one.
302void Function::copyAttributesFrom(const GlobalValue *Src) {
303 assert(isa<Function>(Src) && "Expected a Function!");
304 GlobalValue::copyAttributesFrom(Src);
305 const Function *SrcF = cast<Function>(Src);
306 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000307 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000308 if (SrcF->hasGC())
309 setGC(SrcF->getGC());
310 else
311 clearGC();
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000312}
313
Chris Lattnerbb346d02003-05-08 03:47:33 +0000314/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000315/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000316/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000317/// zero to allow easy checking for whether a function is intrinsic or not. The
318/// particular intrinsic functions which correspond to this value are defined in
319/// llvm/Intrinsics.h.
320///
Dale Johannesenb842d522009-02-05 01:49:45 +0000321unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000322 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000323 if (!ValName)
324 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000325 unsigned Len = ValName->getKeyLength();
326 const char *Name = ValName->getKeyData();
327
Reid Spencer78d71f12007-04-16 16:56:54 +0000328 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000329 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000330 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000331
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000332#define GET_FUNCTION_RECOGNIZER
333#include "llvm/Intrinsics.gen"
334#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000335 return 0;
336}
337
Reid Spencer2a2117c2007-04-01 07:25:33 +0000338std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000339 assert(id < num_intrinsics && "Invalid intrinsic ID!");
340 const char * const Table[] = {
341 "not_intrinsic",
342#define GET_INTRINSIC_NAME_TABLE
343#include "llvm/Intrinsics.gen"
344#undef GET_INTRINSIC_NAME_TABLE
345 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000346 if (numTys == 0)
347 return Table[id];
348 std::string Result(Table[id]);
Mon P Wang2c839d42008-07-30 04:36:53 +0000349 for (unsigned i = 0; i < numTys; ++i) {
350 if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
351 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000352 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000353 }
354 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000355 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000356 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000357 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000358}
359
Owen Anderson155dccd82009-07-07 23:43:39 +0000360const FunctionType *Intrinsic::getType(LLVMContext &Context,
361 ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000362 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000363 const Type *ResultTy = NULL;
364 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000365 bool IsVarArg = false;
366
367#define GET_INTRINSIC_GENERATOR
368#include "llvm/Intrinsics.gen"
369#undef GET_INTRINSIC_GENERATOR
370
Owen Anderson4056ca92009-07-29 22:17:13 +0000371 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000372}
373
Mon P Wangb4024932009-02-24 23:17:49 +0000374bool Intrinsic::isOverloaded(ID id) {
375 const bool OTable[] = {
376 false,
377#define GET_INTRINSIC_OVERLOAD_TABLE
378#include "llvm/Intrinsics.gen"
379#undef GET_INTRINSIC_OVERLOAD_TABLE
380 };
381 return OTable[id];
382}
383
Chris Lattner49b7ee12009-01-12 01:18:58 +0000384/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000385#define GET_INTRINSIC_ATTRIBUTES
386#include "llvm/Intrinsics.gen"
387#undef GET_INTRINSIC_ATTRIBUTES
388
Reid Spencer2a2117c2007-04-01 07:25:33 +0000389Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
390 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000391 // There can never be multiple globals with the same name of different types,
392 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000393 return
Duncan Sands38ef3a82007-12-03 20:06:50 +0000394 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
Owen Anderson155dccd82009-07-07 23:43:39 +0000395 getType(M->getContext(),
396 id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000397}
398
Dale Johannesenb842d522009-02-05 01:49:45 +0000399// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
400#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
401#include "llvm/Intrinsics.gen"
402#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
403
Gabor Greif161cb042010-03-23 14:40:20 +0000404/// hasAddressTaken - returns true if there are any uses of this function
405/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000406bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000407 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000408 const User *U = *I;
409 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
410 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000411 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000412 if (!CS.isCallee(I))
413 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000414 }
415 return false;
416}
417
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000418// vim: sw=2 ai