blob: 4d047f6443cabc8702240497ee5216a466673665 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the Function class for the IR library.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/Function.h"
Chris Lattner7e708292002-06-25 16:13:24 +000015#include "SymbolTableListTraitsImpl.h"
Gordon Henriksen80a75bf2007-12-10 03:18:06 +000016#include "llvm/ADT/DenseMap.h"
Rafael Espindola0e00c6c2011-05-16 03:05:33 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattnercaa4ae72004-12-05 06:43:27 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/CallSite.h"
25#include "llvm/Support/InstIterator.h"
26#include "llvm/Support/LeakDetector.h"
27#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/RWMutex.h"
29#include "llvm/Support/StringPool.h"
30#include "llvm/Support/Threading.h"
Chris Lattner31f84992003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner7e708292002-06-25 16:13:24 +000033// Explicit instantiations of SymbolTableListTraits since some of the methods
34// are not in the public header file...
John McCallc63ca0a2009-12-19 00:55:12 +000035template class llvm::SymbolTableListTraits<Argument, Function>;
36template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner70cc3392001-09-10 07:58:01 +000038//===----------------------------------------------------------------------===//
Chris Lattner80dd50b2002-04-09 19:39:35 +000039// Argument Implementation
40//===----------------------------------------------------------------------===//
41
David Blaikie2d24e2a2011-12-20 02:50:00 +000042void Argument::anchor() { }
43
Chris Lattnerdb125cf2011-07-18 04:54:35 +000044Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
Chris Lattnerdec628e2007-02-12 05:18:08 +000045 : Value(Ty, Value::ArgumentVal) {
Chris Lattnerbded1322002-09-06 21:33:15 +000046 Parent = 0;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000047
48 // Make sure that we get added to a function
49 LeakDetector::addGarbageObject(this);
50
Chris Lattnerbded1322002-09-06 21:33:15 +000051 if (Par)
52 Par->getArgumentList().push_back(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +000053 setName(Name);
Chris Lattnerbded1322002-09-06 21:33:15 +000054}
55
Chris Lattnerbded1322002-09-06 21:33:15 +000056void Argument::setParent(Function *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000057 if (getParent())
58 LeakDetector::addGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000059 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000060 if (getParent())
61 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000062}
63
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000064/// getArgNo - Return the index of this formal argument in its containing
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000065/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000066unsigned Argument::getArgNo() const {
67 const Function *F = getParent();
68 assert(F && "Argument is not in a function");
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000069
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000070 Function::const_arg_iterator AI = F->arg_begin();
71 unsigned ArgIdx = 0;
72 for (; &*AI != this; ++AI)
73 ++ArgIdx;
74
75 return ArgIdx;
76}
77
78/// hasByValAttr - Return true if this argument has the byval attribute on it
79/// in its containing function.
80bool Argument::hasByValAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +000081 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +000082 return getParent()->getAttributes().
83 hasAttribute(getArgNo()+1, Attribute::ByVal);
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000084}
85
Chris Lattnerae441cc2011-05-22 23:57:23 +000086unsigned Argument::getParamAlignment() const {
87 assert(getType()->isPointerTy() && "Only pointers have alignments");
88 return getParent()->getParamAlignment(getArgNo()+1);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000089
Chris Lattnerae441cc2011-05-22 23:57:23 +000090}
91
Duncan Sandsbe1ce0a2009-12-11 08:36:17 +000092/// hasNestAttr - Return true if this argument has the nest attribute on
93/// it in its containing function.
94bool Argument::hasNestAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +000095 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +000096 return getParent()->getAttributes().
97 hasAttribute(getArgNo()+1, Attribute::Nest);
Duncan Sandsbe1ce0a2009-12-11 08:36:17 +000098}
99
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000100/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
101/// it in its containing function.
102bool Argument::hasNoAliasAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000103 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +0000104 return getParent()->getAttributes().
105 hasAttribute(getArgNo()+1, Attribute::NoAlias);
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000106}
107
Duncan Sands17da06f2008-12-31 18:08:59 +0000108/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
109/// on it in its containing function.
110bool Argument::hasNoCaptureAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000111 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +0000112 return getParent()->getAttributes().
113 hasAttribute(getArgNo()+1, Attribute::NoCapture);
Duncan Sands17da06f2008-12-31 18:08:59 +0000114}
115
Owen Anderson7d542542008-02-17 23:22:28 +0000116/// hasSRetAttr - Return true if this argument has the sret attribute on
117/// it in its containing function.
118bool Argument::hasStructRetAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000119 if (!getType()->isPointerTy()) return false;
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000120 if (this != getParent()->arg_begin())
121 return false; // StructRet param must be first param
Bill Wendling39cd0c82012-12-30 12:45:13 +0000122 return getParent()->getAttributes().
123 hasAttribute(1, Attribute::StructRet);
Owen Anderson7d542542008-02-17 23:22:28 +0000124}
125
Devang Patel05988662008-09-25 21:00:45 +0000126/// addAttr - Add a Attribute to an argument
Bill Wendling034b94b2012-12-19 07:18:57 +0000127void Argument::addAttr(Attribute attr) {
Bill Wendling70d2ca02013-01-23 00:20:53 +0000128 AttrBuilder B(attr);
129 getParent()->addAttributes(getArgNo() + 1,
130 AttributeSet::get(getParent()->getContext(),
131 getArgNo() + 1, B));
Gordon Henriksene2435da2008-04-28 17:37:06 +0000132}
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000133
Devang Patel05988662008-09-25 21:00:45 +0000134/// removeAttr - Remove a Attribute from an argument
Bill Wendling034b94b2012-12-19 07:18:57 +0000135void Argument::removeAttr(Attribute attr) {
Devang Patel05988662008-09-25 21:00:45 +0000136 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sandscfa3c232008-07-08 09:41:30 +0000137}
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000138
139
Chris Lattner80dd50b2002-04-09 19:39:35 +0000140//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000141// Helper Methods in Function
Reid Spencer4746ecf2007-04-09 15:01:12 +0000142//===----------------------------------------------------------------------===//
143
Owen Andersone922c022009-07-22 00:24:57 +0000144LLVMContext &Function::getContext() const {
145 return getType()->getContext();
Owen Anderson62fabf52009-07-02 18:03:58 +0000146}
147
Chris Lattner1afcace2011-07-09 17:41:24 +0000148FunctionType *Function::getFunctionType() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000149 return cast<FunctionType>(getType()->getElementType());
Reid Spencer4746ecf2007-04-09 15:01:12 +0000150}
151
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000152bool Function::isVarArg() const {
153 return getFunctionType()->isVarArg();
Reid Spencer4746ecf2007-04-09 15:01:12 +0000154}
155
Chris Lattner1afcace2011-07-09 17:41:24 +0000156Type *Function::getReturnType() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000157 return getFunctionType()->getReturnType();
Duncan Sands827cde12007-11-25 14:10:56 +0000158}
159
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000160void Function::removeFromParent() {
161 getParent()->getFunctionList().remove(this);
Duncan Sands827cde12007-11-25 14:10:56 +0000162}
163
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164void Function::eraseFromParent() {
165 getParent()->getFunctionList().erase(this);
Reid Spencer4746ecf2007-04-09 15:01:12 +0000166}
167
Reid Spencer4746ecf2007-04-09 15:01:12 +0000168//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +0000169// Function Implementation
Chris Lattner70cc3392001-09-10 07:58:01 +0000170//===----------------------------------------------------------------------===//
171
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000172Function::Function(FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000173 const Twine &name, Module *ParentModule)
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000174 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner58d74912008-03-12 17:45:29 +0000175 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattnerc8e222b2009-01-05 07:58:59 +0000176 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattner1afcace2011-07-09 17:41:24 +0000177 "invalid return type");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000178 SymTab = new ValueSymbolTable();
Chris Lattner48270152002-09-06 20:46:32 +0000179
Chris Lattner0162c182007-08-18 06:14:52 +0000180 // If the function has arguments, mark them as lazily built.
181 if (Ty->getNumParams())
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000182 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000183
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000184 // Make sure that we get added to a function
185 LeakDetector::addGarbageObject(this);
186
Chris Lattner48270152002-09-06 20:46:32 +0000187 if (ParentModule)
188 ParentModule->getFunctionList().push_back(this);
Duncan Sands79ab3e82008-04-07 13:39:11 +0000189
190 // Ensure intrinsics have the right parameter attributes.
Dale Johannesen49de9822009-02-05 01:49:45 +0000191 if (unsigned IID = getIntrinsicID())
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000192 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
Devang Patel81b2ab82008-09-02 20:51:15 +0000193
Chris Lattner00950542001-06-06 20:29:01 +0000194}
195
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000196Function::~Function() {
197 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner00950542001-06-06 20:29:01 +0000198
Chris Lattner00950542001-06-06 20:29:01 +0000199 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000200 ArgumentList.clear();
201 delete SymTab;
Reid Spencerb90909e2007-04-22 17:28:03 +0000202
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000203 // Remove the function from the on-the-side GC table.
204 clearGC();
Chris Lattner00950542001-06-06 20:29:01 +0000205}
206
Chris Lattner0162c182007-08-18 06:14:52 +0000207void Function::BuildLazyArguments() const {
208 // Create the arguments vector, all arguments start out unnamed.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000209 FunctionType *FT = getFunctionType();
Chris Lattner0162c182007-08-18 06:14:52 +0000210 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerf0127052010-01-05 13:12:22 +0000211 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattner0162c182007-08-18 06:14:52 +0000212 "Cannot have void typed arguments!");
213 ArgumentList.push_back(new Argument(FT->getParamType(i)));
214 }
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000215
Chris Lattner0162c182007-08-18 06:14:52 +0000216 // Clear the lazy arguments bit.
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000217 unsigned SDC = getSubclassDataFromValue();
218 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattner0162c182007-08-18 06:14:52 +0000219}
220
221size_t Function::arg_size() const {
222 return getFunctionType()->getNumParams();
223}
224bool Function::arg_empty() const {
225 return getFunctionType()->getNumParams() == 0;
226}
227
Chris Lattnere7506a32002-03-23 22:51:58 +0000228void Function::setParent(Module *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000229 if (getParent())
230 LeakDetector::addGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000231 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000232 if (getParent())
233 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000234}
235
Chris Lattner00950542001-06-06 20:29:01 +0000236// dropAllReferences() - This function causes all the subinstructions to "let
237// go" of all references that they are maintaining. This allows one to
238// 'delete' a whole class at a time, even though there may be circular
239// references... first all references are dropped, and all use counts go to
Misha Brukman6b634522003-10-10 17:54:14 +0000240// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanfd939082005-04-21 23:48:37 +0000241// valid on an object that has "dropped all references", except operator
Chris Lattner00950542001-06-06 20:29:01 +0000242// delete.
243//
Chris Lattnere7506a32002-03-23 22:51:58 +0000244void Function::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000245 for (iterator I = begin(), E = end(); I != E; ++I)
246 I->dropAllReferences();
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000247
Dan Gohmana6399ae2010-12-07 19:56:51 +0000248 // Delete all basic blocks. They are now unused, except possibly by
249 // blockaddresses, but BasicBlock's destructor takes care of those.
250 while (!BasicBlocks.empty())
251 BasicBlocks.begin()->eraseFromParent();
Chris Lattner00950542001-06-06 20:29:01 +0000252}
Chris Lattner70cc3392001-09-10 07:58:01 +0000253
Bill Wendling70d2ca02013-01-23 00:20:53 +0000254void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000255 AttributeSet PAL = getAttributes();
Bill Wendling70d2ca02013-01-23 00:20:53 +0000256 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000257 setAttributes(PAL);
Eric Christopher0bf7b412008-05-16 20:39:43 +0000258}
259
Bill Wendling70d2ca02013-01-23 00:20:53 +0000260void Function::addAttributes(unsigned i, AttributeSet attrs) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000261 AttributeSet PAL = getAttributes();
Bill Wendling70d2ca02013-01-23 00:20:53 +0000262 PAL = PAL.addAttributes(getContext(), i, attrs);
263 setAttributes(PAL);
264}
265
266void Function::removeAttribute(unsigned i, Attribute attrs) {
267 AttributeSet PAL = getAttributes();
268 PAL = PAL.removeAttr(getContext(), i, attrs);
Devang Patel05988662008-09-25 21:00:45 +0000269 setAttributes(PAL);
Duncan Sandscfa3c232008-07-08 09:41:30 +0000270}
271
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000272// Maintain the GC name for each function in an on-the-side table. This saves
273// allocating an additional word in Function for programs which do not use GC
274// (i.e., most programs) at the cost of increased overhead for clients which do
275// use GC.
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000276static DenseMap<const Function*,PooledStringPtr> *GCNames;
277static StringPool *GCNamePool;
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000278static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000279
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000280bool Function::hasGC() const {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000281 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000282 return GCNames && GCNames->count(this);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000283}
284
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000285const char *Function::getGC() const {
286 assert(hasGC() && "Function has no collector");
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000287 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000288 return *(*GCNames)[this];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000289}
290
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000291void Function::setGC(const char *Str) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000292 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000293 if (!GCNamePool)
294 GCNamePool = new StringPool();
295 if (!GCNames)
296 GCNames = new DenseMap<const Function*,PooledStringPtr>();
297 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000298}
299
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000300void Function::clearGC() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000301 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000302 if (GCNames) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000303 GCNames->erase(this);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000304 if (GCNames->empty()) {
305 delete GCNames;
306 GCNames = 0;
307 if (GCNamePool->empty()) {
308 delete GCNamePool;
309 GCNamePool = 0;
310 }
311 }
312 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000313}
314
Duncan Sands28c3cff2008-05-26 19:58:59 +0000315/// copyAttributesFrom - copy all additional attributes (those not needed to
316/// create a Function) from the Function Src to this one.
317void Function::copyAttributesFrom(const GlobalValue *Src) {
318 assert(isa<Function>(Src) && "Expected a Function!");
319 GlobalValue::copyAttributesFrom(Src);
320 const Function *SrcF = cast<Function>(Src);
321 setCallingConv(SrcF->getCallingConv());
Devang Patel05988662008-09-25 21:00:45 +0000322 setAttributes(SrcF->getAttributes());
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000323 if (SrcF->hasGC())
324 setGC(SrcF->getGC());
325 else
326 clearGC();
Duncan Sands28c3cff2008-05-26 19:58:59 +0000327}
328
Chris Lattnerdd035d12003-05-08 03:47:33 +0000329/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeked0fde302003-11-11 22:41:34 +0000330/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukman6b634522003-10-10 17:54:14 +0000331/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerdd035d12003-05-08 03:47:33 +0000332/// zero to allow easy checking for whether a function is intrinsic or not. The
333/// particular intrinsic functions which correspond to this value are defined in
334/// llvm/Intrinsics.h.
335///
Dale Johannesen49de9822009-02-05 01:49:45 +0000336unsigned Function::getIntrinsicID() const {
Chris Lattner3b515802007-02-15 19:17:16 +0000337 const ValueName *ValName = this->getValueName();
Michael Ilsemanf846f162012-12-19 23:17:20 +0000338 if (!ValName || !isIntrinsic())
Reid Spencer085659f2007-04-16 07:08:44 +0000339 return 0;
Chris Lattner3b515802007-02-15 19:17:16 +0000340 unsigned Len = ValName->getKeyLength();
341 const char *Name = ValName->getKeyData();
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000342
Chris Lattner9a016ff2006-03-09 20:35:01 +0000343#define GET_FUNCTION_RECOGNIZER
Chandler Carruth351ba142013-01-02 12:09:16 +0000344#include "llvm/IR/Intrinsics.gen"
Chris Lattner9a016ff2006-03-09 20:35:01 +0000345#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerdd035d12003-05-08 03:47:33 +0000346 return 0;
347}
348
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000349std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattnerb8474232006-03-25 06:32:47 +0000350 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattner607b8eb2011-04-25 22:14:33 +0000351 static const char * const Table[] = {
Chris Lattnerb8474232006-03-25 06:32:47 +0000352 "not_intrinsic",
353#define GET_INTRINSIC_NAME_TABLE
Chandler Carruth351ba142013-01-02 12:09:16 +0000354#include "llvm/IR/Intrinsics.gen"
Chris Lattnerb8474232006-03-25 06:32:47 +0000355#undef GET_INTRINSIC_NAME_TABLE
356 };
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000357 if (Tys.empty())
Reid Spencer1437a092007-04-01 07:25:33 +0000358 return Table[id];
359 std::string Result(Table[id]);
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000360 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000361 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000362 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Andersone50ed302009-08-10 22:56:29 +0000363 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wange3b3a722008-07-30 04:36:53 +0000364 }
365 else if (Tys[i])
Owen Andersone50ed302009-08-10 22:56:29 +0000366 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wange3b3a722008-07-30 04:36:53 +0000367 }
Reid Spencer1437a092007-04-01 07:25:33 +0000368 return Result;
Chris Lattnerb8474232006-03-25 06:32:47 +0000369}
370
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000371
Chris Lattner908a8312012-05-27 18:28:35 +0000372/// IIT_Info - These are enumerators that describe the entries returned by the
373/// getIntrinsicInfoTableEntries function.
374///
375/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
376enum IIT_Info {
377 // Common values should be encoded with 0-15.
378 IIT_Done = 0,
379 IIT_I1 = 1,
380 IIT_I8 = 2,
381 IIT_I16 = 3,
382 IIT_I32 = 4,
383 IIT_I64 = 5,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000384 IIT_F16 = 6,
385 IIT_F32 = 7,
386 IIT_F64 = 8,
387 IIT_V2 = 9,
388 IIT_V4 = 10,
389 IIT_V8 = 11,
390 IIT_V16 = 12,
391 IIT_V32 = 13,
Chris Lattner908a8312012-05-27 18:28:35 +0000392 IIT_PTR = 14,
393 IIT_ARG = 15,
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000394
Chris Lattner908a8312012-05-27 18:28:35 +0000395 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000396 IIT_MMX = 16,
397 IIT_METADATA = 17,
398 IIT_EMPTYSTRUCT = 18,
399 IIT_STRUCT2 = 19,
400 IIT_STRUCT3 = 20,
401 IIT_STRUCT4 = 21,
402 IIT_STRUCT5 = 22,
403 IIT_EXTEND_VEC_ARG = 23,
404 IIT_TRUNC_VEC_ARG = 24,
405 IIT_ANYPTR = 25
Chris Lattner908a8312012-05-27 18:28:35 +0000406};
407
408
409static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
410 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Chris Lattner626b1082012-05-17 05:13:57 +0000411 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000412 unsigned StructElts = 2;
Chris Lattner908a8312012-05-27 18:28:35 +0000413 using namespace Intrinsic;
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000414
Chris Lattner626b1082012-05-17 05:13:57 +0000415 switch (Info) {
Chris Lattner908a8312012-05-27 18:28:35 +0000416 case IIT_Done:
417 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
418 return;
419 case IIT_MMX:
420 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
421 return;
422 case IIT_METADATA:
423 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
424 return;
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000425 case IIT_F16:
426 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
427 return;
Chris Lattner908a8312012-05-27 18:28:35 +0000428 case IIT_F32:
429 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
430 return;
431 case IIT_F64:
432 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
433 return;
434 case IIT_I1:
435 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
436 return;
437 case IIT_I8:
438 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
439 return;
440 case IIT_I16:
441 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
442 return;
443 case IIT_I32:
444 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
445 return;
446 case IIT_I64:
447 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
448 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000449 case IIT_V2:
Chris Lattner908a8312012-05-27 18:28:35 +0000450 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
451 DecodeIITType(NextElt, Infos, OutputTable);
452 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000453 case IIT_V4:
Chris Lattner908a8312012-05-27 18:28:35 +0000454 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
455 DecodeIITType(NextElt, Infos, OutputTable);
456 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000457 case IIT_V8:
Chris Lattner908a8312012-05-27 18:28:35 +0000458 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
459 DecodeIITType(NextElt, Infos, OutputTable);
460 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000461 case IIT_V16:
Chris Lattner908a8312012-05-27 18:28:35 +0000462 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
463 DecodeIITType(NextElt, Infos, OutputTable);
464 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000465 case IIT_V32:
Chris Lattner908a8312012-05-27 18:28:35 +0000466 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
467 DecodeIITType(NextElt, Infos, OutputTable);
468 return;
Chris Lattnera48289a2012-05-23 05:19:18 +0000469 case IIT_PTR:
Chris Lattner908a8312012-05-27 18:28:35 +0000470 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
471 DecodeIITType(NextElt, Infos, OutputTable);
472 return;
Chris Lattnera48289a2012-05-23 05:19:18 +0000473 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000474 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattner908a8312012-05-27 18:28:35 +0000475 Infos[NextElt++]));
476 DecodeIITType(NextElt, Infos, OutputTable);
477 return;
Pete Cooperb4285112012-05-21 23:21:28 +0000478 }
Chris Lattner908a8312012-05-27 18:28:35 +0000479 case IIT_ARG: {
480 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
481 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
482 return;
483 }
484 case IIT_EXTEND_VEC_ARG: {
485 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
486 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
487 ArgInfo));
488 return;
489 }
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000490 case IIT_TRUNC_VEC_ARG: {
Chris Lattner908a8312012-05-27 18:28:35 +0000491 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
492 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
493 ArgInfo));
494 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000495 }
Chris Lattner908a8312012-05-27 18:28:35 +0000496 case IIT_EMPTYSTRUCT:
497 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
498 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000499 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
500 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
501 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
502 case IIT_STRUCT2: {
Chris Lattner908a8312012-05-27 18:28:35 +0000503 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
504
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000505 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattner908a8312012-05-27 18:28:35 +0000506 DecodeIITType(NextElt, Infos, OutputTable);
507 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000508 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000509 }
510 llvm_unreachable("unhandled");
511}
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000512
Chris Lattner908a8312012-05-27 18:28:35 +0000513
514#define GET_INTRINSIC_GENERATOR_GLOBAL
Chandler Carruth351ba142013-01-02 12:09:16 +0000515#include "llvm/IR/Intrinsics.gen"
Chris Lattner908a8312012-05-27 18:28:35 +0000516#undef GET_INTRINSIC_GENERATOR_GLOBAL
517
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000518void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattner908a8312012-05-27 18:28:35 +0000519 SmallVectorImpl<IITDescriptor> &T){
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000520 // Check to see if the intrinsic's type was expressible by the table.
521 unsigned TableVal = IIT_Table[id-1];
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000522
Chris Lattner387c9dc2012-05-17 15:55:41 +0000523 // Decode the TableVal into an array of IITValues.
524 SmallVector<unsigned char, 8> IITValues;
525 ArrayRef<unsigned char> IITEntries;
526 unsigned NextElt = 0;
527 if ((TableVal >> 31) != 0) {
528 // This is an offset into the IIT_LongEncodingTable.
529 IITEntries = IIT_LongEncodingTable;
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000530
Chris Lattner387c9dc2012-05-17 15:55:41 +0000531 // Strip sentinel bit.
532 NextElt = (TableVal << 1) >> 1;
533 } else {
Chris Lattner908a8312012-05-27 18:28:35 +0000534 // Decode the TableVal into an array of IITValues. If the entry was encoded
535 // into a single word in the table itself, decode it now.
Chris Lattner626b1082012-05-17 05:13:57 +0000536 do {
537 IITValues.push_back(TableVal & 0xF);
538 TableVal >>= 4;
539 } while (TableVal);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000540
Chris Lattner387c9dc2012-05-17 15:55:41 +0000541 IITEntries = IITValues;
542 NextElt = 0;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000543 }
Chris Lattner908a8312012-05-27 18:28:35 +0000544
545 // Okay, decode the table into the output vector of IITDescriptors.
546 DecodeIITType(NextElt, IITEntries, T);
547 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
548 DecodeIITType(NextElt, IITEntries, T);
549}
550
551
552static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
553 ArrayRef<Type*> Tys, LLVMContext &Context) {
554 using namespace Intrinsic;
555 IITDescriptor D = Infos.front();
556 Infos = Infos.slice(1);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000557
Chris Lattner908a8312012-05-27 18:28:35 +0000558 switch (D.Kind) {
559 case IITDescriptor::Void: return Type::getVoidTy(Context);
560 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
561 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000562 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattner908a8312012-05-27 18:28:35 +0000563 case IITDescriptor::Float: return Type::getFloatTy(Context);
564 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000565
Chris Lattner908a8312012-05-27 18:28:35 +0000566 case IITDescriptor::Integer:
567 return IntegerType::get(Context, D.Integer_Width);
568 case IITDescriptor::Vector:
569 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
570 case IITDescriptor::Pointer:
571 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
572 D.Pointer_AddressSpace);
573 case IITDescriptor::Struct: {
574 Type *Elts[5];
575 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
576 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
577 Elts[i] = DecodeFixedType(Infos, Tys, Context);
578 return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
579 }
580
581 case IITDescriptor::Argument:
582 return Tys[D.getArgumentNumber()];
583 case IITDescriptor::ExtendVecArgument:
584 return VectorType::getExtendedElementVectorType(cast<VectorType>(
585 Tys[D.getArgumentNumber()]));
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000586
Chris Lattner908a8312012-05-27 18:28:35 +0000587 case IITDescriptor::TruncVecArgument:
588 return VectorType::getTruncatedElementVectorType(cast<VectorType>(
589 Tys[D.getArgumentNumber()]));
590 }
591 llvm_unreachable("unhandled");
592}
593
594
595
596FunctionType *Intrinsic::getType(LLVMContext &Context,
597 ID id, ArrayRef<Type*> Tys) {
598 SmallVector<IITDescriptor, 8> Table;
599 getIntrinsicInfoTableEntries(id, Table);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000600
Chris Lattner908a8312012-05-27 18:28:35 +0000601 ArrayRef<IITDescriptor> TableRef = Table;
602 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000603
Chris Lattner387c9dc2012-05-17 15:55:41 +0000604 SmallVector<Type*, 8> ArgTys;
Chris Lattner908a8312012-05-27 18:28:35 +0000605 while (!TableRef.empty())
606 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey95af5922007-02-07 20:38:26 +0000607
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000608 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey95af5922007-02-07 20:38:26 +0000609}
610
Mon P Wang0d52ff12009-02-24 23:17:49 +0000611bool Intrinsic::isOverloaded(ID id) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000612#define GET_INTRINSIC_OVERLOAD_TABLE
Chandler Carruth351ba142013-01-02 12:09:16 +0000613#include "llvm/IR/Intrinsics.gen"
Mon P Wang0d52ff12009-02-24 23:17:49 +0000614#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wang0d52ff12009-02-24 23:17:49 +0000615}
616
Chris Lattner048ffb22009-01-12 01:18:58 +0000617/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000618#define GET_INTRINSIC_ATTRIBUTES
Chandler Carruth351ba142013-01-02 12:09:16 +0000619#include "llvm/IR/Intrinsics.gen"
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000620#undef GET_INTRINSIC_ATTRIBUTES
621
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000622Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000623 // There can never be multiple globals with the same name of different types,
624 // because intrinsics must be a specific type.
Duncan Sands79ab3e82008-04-07 13:39:11 +0000625 return
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000626 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
627 getType(M->getContext(), id, Tys)));
Jim Laskey95af5922007-02-07 20:38:26 +0000628}
629
Dale Johannesen49de9822009-02-05 01:49:45 +0000630// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
631#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Chandler Carruth351ba142013-01-02 12:09:16 +0000632#include "llvm/IR/Intrinsics.gen"
Dale Johannesen49de9822009-02-05 01:49:45 +0000633#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
634
Gabor Greif0054c7a2010-03-23 14:40:20 +0000635/// hasAddressTaken - returns true if there are any uses of this function
636/// other than direct calls or invokes to it.
Gabor Greifc9f75002010-03-24 13:21:49 +0000637bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000638 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifc9f75002010-03-24 13:21:49 +0000639 const User *U = *I;
Jay Foadb7454fd2012-05-12 08:30:16 +0000640 if (isa<BlockAddress>(U))
641 continue;
Gabor Greifc9f75002010-03-24 13:21:49 +0000642 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
643 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greifc8b82cc2010-04-01 08:21:08 +0000644 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifc9f75002010-03-24 13:21:49 +0000645 if (!CS.isCallee(I))
646 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad757068f2009-06-10 08:41:11 +0000647 }
648 return false;
649}
650
Eli Friedmanc6633052011-10-20 05:23:42 +0000651bool Function::isDefTriviallyDead() const {
652 // Check the linkage
653 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
654 !hasAvailableExternallyLinkage())
655 return false;
656
657 // Check if the function is used by anything other than a blockaddress.
658 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
659 if (!isa<BlockAddress>(*I))
660 return false;
661
662 return true;
663}
664
Bill Wendling3c5e6092011-10-17 18:43:40 +0000665/// callsFunctionThatReturnsTwice - Return true if the function has a call to
666/// setjmp or other function that gcc recognizes as "returning twice".
667bool Function::callsFunctionThatReturnsTwice() const {
668 for (const_inst_iterator
669 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
670 const CallInst* callInst = dyn_cast<CallInst>(&*I);
671 if (!callInst)
672 continue;
673 if (callInst->canReturnTwice())
674 return true;
675 }
676
677 return false;
678}
679