blob: 6339c6791d4d35ddd40d24612e943b3344debd5d [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// 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
David Blaikiea379b1812011-12-20 02:50:00 +000041void Argument::anchor() { }
42
Chris Lattner229907c2011-07-18 04:54:35 +000043Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000044 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000045 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000046
47 // Make sure that we get added to a function
48 LeakDetector::addGarbageObject(this);
49
Chris Lattner9ed7aef2002-09-06 21:33:15 +000050 if (Par)
51 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000052 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000053}
54
Chris Lattner9ed7aef2002-09-06 21:33:15 +000055void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000056 if (getParent())
57 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000058 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000059 if (getParent())
60 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000061}
62
Chris Lattnere30f09d2008-01-24 17:47:11 +000063/// getArgNo - Return the index of this formal argument in its containing
64/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
65unsigned Argument::getArgNo() const {
66 const Function *F = getParent();
67 assert(F && "Argument is not in a function");
68
69 Function::const_arg_iterator AI = F->arg_begin();
70 unsigned ArgIdx = 0;
71 for (; &*AI != this; ++AI)
72 ++ArgIdx;
73
74 return ArgIdx;
75}
76
77/// hasByValAttr - Return true if this argument has the byval attribute on it
78/// in its containing function.
79bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000080 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +000081 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000082}
83
Chris Lattner4d37d992011-05-22 23:57:23 +000084unsigned Argument::getParamAlignment() const {
85 assert(getType()->isPointerTy() && "Only pointers have alignments");
86 return getParent()->getParamAlignment(getArgNo()+1);
87
88}
89
Duncan Sands5d96f3f2009-12-11 08:36:17 +000090/// hasNestAttr - Return true if this argument has the nest attribute on
91/// it in its containing function.
92bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000093 if (!getType()->isPointerTy()) return false;
Duncan Sands5d96f3f2009-12-11 08:36:17 +000094 return getParent()->paramHasAttr(getArgNo()+1, Attribute::Nest);
95}
96
Chris Lattnere30f09d2008-01-24 17:47:11 +000097/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
98/// it in its containing function.
99bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000100 if (!getType()->isPointerTy()) return false;
Devang Patel4c758ea2008-09-25 21:00:45 +0000101 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000102}
103
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000104/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
105/// on it in its containing function.
106bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000107 if (!getType()->isPointerTy()) return false;
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000108 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
109}
110
Owen Andersonc64dfb42008-02-17 23:22:28 +0000111/// hasSRetAttr - Return true if this argument has the sret attribute on
112/// it in its containing function.
113bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000114 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000115 if (this != getParent()->arg_begin())
116 return false; // StructRet param must be first param
Devang Patel4c758ea2008-09-25 21:00:45 +0000117 return getParent()->paramHasAttr(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000118}
119
Devang Patel4c758ea2008-09-25 21:00:45 +0000120/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000121void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000122 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000123}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000124
Devang Patel4c758ea2008-09-25 21:00:45 +0000125/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000126void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000127 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000128}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000129
130
Chris Lattnerd255ae22002-04-09 19:39:35 +0000131//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000133//===----------------------------------------------------------------------===//
134
Owen Anderson47db9412009-07-22 00:24:57 +0000135LLVMContext &Function::getContext() const {
136 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000137}
138
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000139FunctionType *Function::getFunctionType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000141}
142
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000143bool Function::isVarArg() const {
144 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000145}
146
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000147Type *Function::getReturnType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000149}
150
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151void Function::removeFromParent() {
152 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000153}
154
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000155void Function::eraseFromParent() {
156 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000157}
158
Reid Spencer019c8862007-04-09 15:01:12 +0000159//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000160// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000161//===----------------------------------------------------------------------===//
162
Chris Lattner229907c2011-07-18 04:54:35 +0000163Function::Function(FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000164 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000165 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000166 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000167 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000168 "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000169 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000170
Chris Lattnere2de9082007-08-18 06:14:52 +0000171 // If the function has arguments, mark them as lazily built.
172 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000173 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000174
Chris Lattner184b2982002-09-08 18:59:35 +0000175 // Make sure that we get added to a function
176 LeakDetector::addGarbageObject(this);
177
Chris Lattner6213ae02002-09-06 20:46:32 +0000178 if (ParentModule)
179 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000180
181 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000182 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000183 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000184
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185}
186
Gordon Henriksen14a55692007-12-10 02:14:30 +0000187Function::~Function() {
188 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000191 ArgumentList.clear();
192 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000193
Gordon Henriksend930f912008-08-17 18:44:35 +0000194 // Remove the function from the on-the-side GC table.
195 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196}
197
Chris Lattnere2de9082007-08-18 06:14:52 +0000198void Function::BuildLazyArguments() const {
199 // Create the arguments vector, all arguments start out unnamed.
Chris Lattner229907c2011-07-18 04:54:35 +0000200 FunctionType *FT = getFunctionType();
Chris Lattnere2de9082007-08-18 06:14:52 +0000201 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000202 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000203 "Cannot have void typed arguments!");
204 ArgumentList.push_back(new Argument(FT->getParamType(i)));
205 }
206
207 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000208 unsigned SDC = getSubclassDataFromValue();
209 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000210}
211
212size_t Function::arg_size() const {
213 return getFunctionType()->getNumParams();
214}
215bool Function::arg_empty() const {
216 return getFunctionType()->getNumParams() == 0;
217}
218
Chris Lattner4e8c4872002-03-23 22:51:58 +0000219void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000220 if (getParent())
221 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000223 if (getParent())
224 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225}
226
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227// dropAllReferences() - This function causes all the subinstructions to "let
228// go" of all references that they are maintaining. This allows one to
229// 'delete' a whole class at a time, even though there may be circular
230// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000231// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000232// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233// delete.
234//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000235void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000236 for (iterator I = begin(), E = end(); I != E; ++I)
237 I->dropAllReferences();
Chris Lattner3a395302009-10-28 03:37:35 +0000238
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000239 // Delete all basic blocks. They are now unused, except possibly by
240 // blockaddresses, but BasicBlock's destructor takes care of those.
241 while (!BasicBlocks.empty())
242 BasicBlocks.begin()->eraseFromParent();
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
Benjamin Kramere6e19332011-07-14 17:45:39 +0000338std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000339 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000340 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000341 "not_intrinsic",
342#define GET_INTRINSIC_NAME_TABLE
343#include "llvm/Intrinsics.gen"
344#undef GET_INTRINSIC_NAME_TABLE
345 };
Benjamin Kramere6e19332011-07-14 17:45:39 +0000346 if (Tys.empty())
Reid Spencer2a2117c2007-04-01 07:25:33 +0000347 return Table[id];
348 std::string Result(Table[id]);
Benjamin Kramere6e19332011-07-14 17:45:39 +0000349 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000350 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Mon P Wang2c839d42008-07-30 04:36:53 +0000351 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
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000360#define GET_INTRINSTIC_GENERATOR_GLOBAL
361#include "llvm/Intrinsics.gen"
362#undef GET_INTRINSTIC_GENERATOR_GLOBAL
363
Chris Lattnera57c7972012-05-17 05:13:57 +0000364static Type *DecodeFixedType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
365 ArrayRef<Type*> Tys, LLVMContext &Context) {
366 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000367 unsigned StructElts = 2;
368
Chris Lattnera57c7972012-05-17 05:13:57 +0000369 switch (Info) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000370 case IIT_Done: return Type::getVoidTy(Context);
371 case IIT_I1: return Type::getInt1Ty(Context);
372 case IIT_I8: return Type::getInt8Ty(Context);
373 case IIT_I16: return Type::getInt16Ty(Context);
374 case IIT_I32: return Type::getInt32Ty(Context);
375 case IIT_I64: return Type::getInt64Ty(Context);
376 case IIT_F32: return Type::getFloatTy(Context);
377 case IIT_F64: return Type::getDoubleTy(Context);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000378 case IIT_MMX: return Type::getX86_MMXTy(Context);
Manuel Klimek0fc33af2012-05-17 09:32:05 +0000379 case IIT_METADATA: return Type::getMetadataTy(Context);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000380 case IIT_EMPTYSTRUCT: return StructType::get(Context);
Chris Lattner827b2532012-05-17 04:30:58 +0000381 case IIT_V2:
Chris Lattnera57c7972012-05-17 05:13:57 +0000382 return VectorType::get(DecodeFixedType(NextElt, Infos, Tys, Context), 2);
Chris Lattner827b2532012-05-17 04:30:58 +0000383 case IIT_V4:
Chris Lattnera57c7972012-05-17 05:13:57 +0000384 return VectorType::get(DecodeFixedType(NextElt, Infos, Tys, Context), 4);
Chris Lattner827b2532012-05-17 04:30:58 +0000385 case IIT_V8:
Chris Lattnera57c7972012-05-17 05:13:57 +0000386 return VectorType::get(DecodeFixedType(NextElt, Infos, Tys, Context), 8);
Chris Lattner827b2532012-05-17 04:30:58 +0000387 case IIT_V16:
Chris Lattnera57c7972012-05-17 05:13:57 +0000388 return VectorType::get(DecodeFixedType(NextElt, Infos, Tys, Context), 16);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000389 case IIT_V32:
Chris Lattnera57c7972012-05-17 05:13:57 +0000390 return VectorType::get(DecodeFixedType(NextElt, Infos, Tys, Context), 32);
Pete Cooper243efd72012-05-21 23:21:28 +0000391 case IIT_PTR: {
392 unsigned AddrSpace = Infos[NextElt++];
393 Type *PtrTy = DecodeFixedType(NextElt, Infos, Tys,Context);
394 return PointerType::get(PtrTy, AddrSpace);
395 }
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000396 case IIT_ARG:
397 case IIT_EXTEND_VEC_ARG:
398 case IIT_TRUNC_VEC_ARG: {
Chris Lattnera57c7972012-05-17 05:13:57 +0000399 unsigned ArgNo = NextElt == Infos.size() ? 0 : Infos[NextElt++];
Chris Lattner827b2532012-05-17 04:30:58 +0000400 assert(ArgNo < Tys.size() && "Not enough types specified!");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000401 Type *T = Tys[ArgNo];
402
Chris Lattnera57c7972012-05-17 05:13:57 +0000403 if (Info == IIT_EXTEND_VEC_ARG)
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000404 T = VectorType::getExtendedElementVectorType(cast<VectorType>(T));
Chris Lattnera57c7972012-05-17 05:13:57 +0000405 if (Info == IIT_TRUNC_VEC_ARG)
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000406 T = VectorType::getTruncatedElementVectorType(cast<VectorType>(T));
407 return T;
408 }
409 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
410 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
411 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
412 case IIT_STRUCT2: {
413 Type *Elts[5];
414 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnera57c7972012-05-17 05:13:57 +0000415 Elts[i] = DecodeFixedType(NextElt, Infos, Tys, Context);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000416 return StructType::get(Context, ArrayRef<Type*>(Elts, StructElts));
Chris Lattner827b2532012-05-17 04:30:58 +0000417 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000418 }
419 llvm_unreachable("unhandled");
420}
421
422
Chris Lattner229907c2011-07-18 04:54:35 +0000423FunctionType *Intrinsic::getType(LLVMContext &Context,
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000424 ID id, ArrayRef<Type*> Tys) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000425 // Check to see if the intrinsic's type was expressible by the table.
426 unsigned TableVal = IIT_Table[id-1];
Chris Lattnera3b0f522012-05-17 15:55:41 +0000427
428 // Decode the TableVal into an array of IITValues.
429 SmallVector<unsigned char, 8> IITValues;
430 ArrayRef<unsigned char> IITEntries;
431 unsigned NextElt = 0;
432 if ((TableVal >> 31) != 0) {
433 // This is an offset into the IIT_LongEncodingTable.
434 IITEntries = IIT_LongEncodingTable;
435
436 // Strip sentinel bit.
437 NextElt = (TableVal << 1) >> 1;
438 } else {
Chris Lattnera57c7972012-05-17 05:13:57 +0000439 do {
440 IITValues.push_back(TableVal & 0xF);
441 TableVal >>= 4;
442 } while (TableVal);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000443
Chris Lattnera3b0f522012-05-17 15:55:41 +0000444 IITEntries = IITValues;
445 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000446 }
Chris Lattnera3b0f522012-05-17 15:55:41 +0000447
448 Type *ResultTy = DecodeFixedType(NextElt, IITEntries, Tys, Context);
449
450 SmallVector<Type*, 8> ArgTys;
451 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
452 ArgTys.push_back(DecodeFixedType(NextElt, IITEntries, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000453
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000454 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000455}
456
Mon P Wangb4024932009-02-24 23:17:49 +0000457bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000458#define GET_INTRINSIC_OVERLOAD_TABLE
459#include "llvm/Intrinsics.gen"
460#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +0000461}
462
Chris Lattner49b7ee12009-01-12 01:18:58 +0000463/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000464#define GET_INTRINSIC_ATTRIBUTES
465#include "llvm/Intrinsics.gen"
466#undef GET_INTRINSIC_ATTRIBUTES
467
Benjamin Kramere6e19332011-07-14 17:45:39 +0000468Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000469 // There can never be multiple globals with the same name of different types,
470 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000471 return
Benjamin Kramere6e19332011-07-14 17:45:39 +0000472 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
473 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000474}
475
Dale Johannesenb842d522009-02-05 01:49:45 +0000476// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
477#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
478#include "llvm/Intrinsics.gen"
479#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
480
Gabor Greif161cb042010-03-23 14:40:20 +0000481/// hasAddressTaken - returns true if there are any uses of this function
482/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000483bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000484 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000485 const User *U = *I;
Jay Foadca0c4992012-05-12 08:30:16 +0000486 if (isa<BlockAddress>(U))
487 continue;
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000488 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
489 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000490 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000491 if (!CS.isCallee(I))
492 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000493 }
494 return false;
495}
496
Eli Friedman1923a332011-10-20 05:23:42 +0000497bool Function::isDefTriviallyDead() const {
498 // Check the linkage
499 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
500 !hasAvailableExternallyLinkage())
501 return false;
502
503 // Check if the function is used by anything other than a blockaddress.
504 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
505 if (!isa<BlockAddress>(*I))
506 return false;
507
508 return true;
509}
510
Bill Wendling63a4ea12011-10-17 18:43:40 +0000511/// callsFunctionThatReturnsTwice - Return true if the function has a call to
512/// setjmp or other function that gcc recognizes as "returning twice".
513bool Function::callsFunctionThatReturnsTwice() const {
514 for (const_inst_iterator
515 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
516 const CallInst* callInst = dyn_cast<CallInst>(&*I);
517 if (!callInst)
518 continue;
519 if (callInst->canReturnTwice())
520 return true;
521 }
522
523 return false;
524}
525