blob: bb8f62ac295346ab440f2679201a4b561c219cae [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"
Rafael Espindola79d0c4f2011-10-05 20:05:13 +000020#include "llvm/Support/InstIterator.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Owen Andersonaab59c52009-06-17 22:23:31 +000022#include "llvm/Support/ManagedStatic.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000023#include "llvm/Support/StringPool.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/RWMutex.h"
25#include "llvm/Support/Threading.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000026#include "SymbolTableListTraitsImpl.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000027#include "llvm/ADT/DenseMap.h"
Rafael Espindola2050af82011-05-16 03:05:33 +000028#include "llvm/ADT/STLExtras.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner113f4f42002-06-25 16:13:24 +000032
33// Explicit instantiations of SymbolTableListTraits since some of the methods
34// are not in the public header file...
John McCall086bb4e2009-12-19 00:55:12 +000035template class llvm::SymbolTableListTraits<Argument, Function>;
36template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000037
Chris Lattnerda975502001-09-10 07:58:01 +000038//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000039// Argument Implementation
40//===----------------------------------------------------------------------===//
41
Chris Lattner229907c2011-07-18 04:54:35 +000042Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000043 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000044 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000045
46 // Make sure that we get added to a function
47 LeakDetector::addGarbageObject(this);
48
Chris Lattner9ed7aef2002-09-06 21:33:15 +000049 if (Par)
50 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000051 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000052}
53
Chris Lattner9ed7aef2002-09-06 21:33:15 +000054void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000055 if (getParent())
56 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000057 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000058 if (getParent())
59 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060}
61
Chris Lattnere30f09d2008-01-24 17:47:11 +000062/// getArgNo - Return the index of this formal argument in its containing
63/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
64unsigned Argument::getArgNo() const {
65 const Function *F = getParent();
66 assert(F && "Argument is not in a function");
67
68 Function::const_arg_iterator AI = F->arg_begin();
69 unsigned ArgIdx = 0;
70 for (; &*AI != this; ++AI)
71 ++ArgIdx;
72
73 return ArgIdx;
74}
75
76/// hasByValAttr - Return true if this argument has the byval attribute on it
77/// in its containing function.
78bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000079 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000080 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000081}
82
Chris Lattner4d37d992011-05-22 23:57:23 +000083unsigned Argument::getParamAlignment() const {
84 assert(getType()->isPointerTy() && "Only pointers have alignments");
85 return getParent()->getParamAlignment(getArgNo()+1);
86
87}
88
Duncan Sands5d96f3f2009-12-11 08:36:17 +000089/// hasNestAttr - Return true if this argument has the nest attribute on
90/// it in its containing function.
91bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000092 if (!getType()->isPointerTy()) return false;
Duncan Sands5d96f3f2009-12-11 08:36:17 +000093 return getParent()->paramHasAttr(getArgNo()+1, Attribute::Nest);
94}
95
Chris Lattnere30f09d2008-01-24 17:47:11 +000096/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
97/// it in its containing function.
98bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000099 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +0000100 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000101}
102
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000103/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
104/// on it in its containing function.
105bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000106 if (!getType()->isPointerTy()) return false;
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000107 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
108}
109
Owen Andersonc64dfb42008-02-17 23:22:28 +0000110/// hasSRetAttr - Return true if this argument has the sret attribute on
111/// it in its containing function.
112bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000113 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000114 if (this != getParent()->arg_begin())
115 return false; // StructRet param must be first param
Devang Patel4c758ea2008-09-25 21:00:45 +0000116 return getParent()->paramHasAttr(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000117}
118
Devang Patel4c758ea2008-09-25 21:00:45 +0000119/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000120void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000121 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000122}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000123
Devang Patel4c758ea2008-09-25 21:00:45 +0000124/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000125void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000126 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000127}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000128
129
Chris Lattnerd255ae22002-04-09 19:39:35 +0000130//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000131// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000132//===----------------------------------------------------------------------===//
133
Owen Anderson47db9412009-07-22 00:24:57 +0000134LLVMContext &Function::getContext() const {
135 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000136}
137
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000138FunctionType *Function::getFunctionType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000139 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000140}
141
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142bool Function::isVarArg() const {
143 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000144}
145
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146Type *Function::getReturnType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000148}
149
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000150void Function::removeFromParent() {
151 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000152}
153
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154void Function::eraseFromParent() {
155 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000156}
157
Reid Spencer019c8862007-04-09 15:01:12 +0000158//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000159// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000160//===----------------------------------------------------------------------===//
161
Chris Lattner229907c2011-07-18 04:54:35 +0000162Function::Function(FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000163 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000164 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000165 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000166 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000167 "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000168 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000169
Chris Lattnere2de9082007-08-18 06:14:52 +0000170 // If the function has arguments, mark them as lazily built.
171 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000172 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000173
Chris Lattner184b2982002-09-08 18:59:35 +0000174 // Make sure that we get added to a function
175 LeakDetector::addGarbageObject(this);
176
Chris Lattner6213ae02002-09-06 20:46:32 +0000177 if (ParentModule)
178 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000179
180 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000181 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000182 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000183
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184}
185
Gordon Henriksen14a55692007-12-10 02:14:30 +0000186Function::~Function() {
187 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000190 ArgumentList.clear();
191 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000192
Gordon Henriksend930f912008-08-17 18:44:35 +0000193 // Remove the function from the on-the-side GC table.
194 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195}
196
Chris Lattnere2de9082007-08-18 06:14:52 +0000197void Function::BuildLazyArguments() const {
198 // Create the arguments vector, all arguments start out unnamed.
Chris Lattner229907c2011-07-18 04:54:35 +0000199 FunctionType *FT = getFunctionType();
Chris Lattnere2de9082007-08-18 06:14:52 +0000200 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000201 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000202 "Cannot have void typed arguments!");
203 ArgumentList.push_back(new Argument(FT->getParamType(i)));
204 }
205
206 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000207 unsigned SDC = getSubclassDataFromValue();
208 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000209}
210
211size_t Function::arg_size() const {
212 return getFunctionType()->getNumParams();
213}
214bool Function::arg_empty() const {
215 return getFunctionType()->getNumParams() == 0;
216}
217
Chris Lattner4e8c4872002-03-23 22:51:58 +0000218void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000219 if (getParent())
220 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000222 if (getParent())
223 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224}
225
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226// dropAllReferences() - This function causes all the subinstructions to "let
227// go" of all references that they are maintaining. This allows one to
228// 'delete' a whole class at a time, even though there may be circular
229// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000230// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000231// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232// delete.
233//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000234void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000235 for (iterator I = begin(), E = end(); I != E; ++I)
236 I->dropAllReferences();
Chris Lattner3a395302009-10-28 03:37:35 +0000237
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000238 // Delete all basic blocks. They are now unused, except possibly by
239 // blockaddresses, but BasicBlock's destructor takes care of those.
240 while (!BasicBlocks.empty())
241 BasicBlocks.begin()->eraseFromParent();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242}
Chris Lattnerda975502001-09-10 07:58:01 +0000243
Devang Patel4c758ea2008-09-25 21:00:45 +0000244void Function::addAttribute(unsigned i, Attributes attr) {
245 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000246 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000247 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000248}
249
Devang Patel4c758ea2008-09-25 21:00:45 +0000250void Function::removeAttribute(unsigned i, Attributes attr) {
251 AttrListPtr PAL = getAttributes();
Duncan Sands66336db2008-07-08 09:41:30 +0000252 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000253 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000254}
255
Gordon Henriksend930f912008-08-17 18:44:35 +0000256// Maintain the GC name for each function in an on-the-side table. This saves
257// allocating an additional word in Function for programs which do not use GC
258// (i.e., most programs) at the cost of increased overhead for clients which do
259// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000260static DenseMap<const Function*,PooledStringPtr> *GCNames;
261static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000262static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000263
Gordon Henriksend930f912008-08-17 18:44:35 +0000264bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000265 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000266 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000267}
268
Gordon Henriksend930f912008-08-17 18:44:35 +0000269const char *Function::getGC() const {
270 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000271 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000272 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000273}
274
Gordon Henriksend930f912008-08-17 18:44:35 +0000275void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000276 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000277 if (!GCNamePool)
278 GCNamePool = new StringPool();
279 if (!GCNames)
280 GCNames = new DenseMap<const Function*,PooledStringPtr>();
281 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000282}
283
Gordon Henriksend930f912008-08-17 18:44:35 +0000284void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000285 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000286 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000287 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000288 if (GCNames->empty()) {
289 delete GCNames;
290 GCNames = 0;
291 if (GCNamePool->empty()) {
292 delete GCNamePool;
293 GCNamePool = 0;
294 }
295 }
296 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000297}
298
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000299/// copyAttributesFrom - copy all additional attributes (those not needed to
300/// create a Function) from the Function Src to this one.
301void Function::copyAttributesFrom(const GlobalValue *Src) {
302 assert(isa<Function>(Src) && "Expected a Function!");
303 GlobalValue::copyAttributesFrom(Src);
304 const Function *SrcF = cast<Function>(Src);
305 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000306 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000307 if (SrcF->hasGC())
308 setGC(SrcF->getGC());
309 else
310 clearGC();
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000311}
312
Chris Lattnerbb346d02003-05-08 03:47:33 +0000313/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000314/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000315/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000316/// zero to allow easy checking for whether a function is intrinsic or not. The
317/// particular intrinsic functions which correspond to this value are defined in
318/// llvm/Intrinsics.h.
319///
Dale Johannesenb842d522009-02-05 01:49:45 +0000320unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000321 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000322 if (!ValName)
323 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000324 unsigned Len = ValName->getKeyLength();
325 const char *Name = ValName->getKeyData();
326
Reid Spencer78d71f12007-04-16 16:56:54 +0000327 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000328 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000329 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000330
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000331#define GET_FUNCTION_RECOGNIZER
332#include "llvm/Intrinsics.gen"
333#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000334 return 0;
335}
336
Benjamin Kramere6e19332011-07-14 17:45:39 +0000337std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000338 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000339 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000340 "not_intrinsic",
341#define GET_INTRINSIC_NAME_TABLE
342#include "llvm/Intrinsics.gen"
343#undef GET_INTRINSIC_NAME_TABLE
344 };
Benjamin Kramere6e19332011-07-14 17:45:39 +0000345 if (Tys.empty())
Reid Spencer2a2117c2007-04-01 07:25:33 +0000346 return Table[id];
347 std::string Result(Table[id]);
Benjamin Kramere6e19332011-07-14 17:45:39 +0000348 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000349 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Mon P Wang2c839d42008-07-30 04:36:53 +0000350 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000351 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000352 }
353 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000354 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000355 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000356 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000357}
358
Chris Lattner229907c2011-07-18 04:54:35 +0000359FunctionType *Intrinsic::getType(LLVMContext &Context,
Benjamin Kramere6e19332011-07-14 17:45:39 +0000360 ID id, ArrayRef<Type*> Tys) {
Chris Lattner229907c2011-07-18 04:54:35 +0000361 Type *ResultTy = NULL;
Benjamin Kramer0dfb1592011-10-17 21:33:26 +0000362 SmallVector<Type*, 8> 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
Benjamin Kramere6e19332011-07-14 17:45:39 +0000387Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000388 // There can never be multiple globals with the same name of different types,
389 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000390 return
Benjamin Kramere6e19332011-07-14 17:45:39 +0000391 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
392 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000393}
394
Dale Johannesenb842d522009-02-05 01:49:45 +0000395// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
396#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
397#include "llvm/Intrinsics.gen"
398#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
399
Gabor Greif161cb042010-03-23 14:40:20 +0000400/// hasAddressTaken - returns true if there are any uses of this function
401/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000402bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000403 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000404 const User *U = *I;
Eli Friedman1923a332011-10-20 05:23:42 +0000405 // FIXME: Check for blockaddress, which does not take the address.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000406 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
407 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000408 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000409 if (!CS.isCallee(I))
410 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000411 }
412 return false;
413}
414
Eli Friedman1923a332011-10-20 05:23:42 +0000415bool Function::isDefTriviallyDead() const {
416 // Check the linkage
417 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
418 !hasAvailableExternallyLinkage())
419 return false;
420
421 // Check if the function is used by anything other than a blockaddress.
422 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
423 if (!isa<BlockAddress>(*I))
424 return false;
425
426 return true;
427}
428
Bill Wendling63a4ea12011-10-17 18:43:40 +0000429/// callsFunctionThatReturnsTwice - Return true if the function has a call to
430/// setjmp or other function that gcc recognizes as "returning twice".
431bool Function::callsFunctionThatReturnsTwice() const {
432 for (const_inst_iterator
433 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
434 const CallInst* callInst = dyn_cast<CallInst>(&*I);
435 if (!callInst)
436 continue;
437 if (callInst->canReturnTwice())
438 return true;
439 }
440
441 return false;
442}
443
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000444// vim: sw=2 ai