blob: 5c2a03ce09193a8242e4e7a4ddd6e74edc5c2f37 [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;
Bill Wendlingc9b22d72012-10-09 07:45:08 +000081 return getParent()->getParamAttributes(getArgNo()+1).
82 hasAttribute(Attributes::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000083}
84
Chris Lattner4d37d992011-05-22 23:57:23 +000085unsigned Argument::getParamAlignment() const {
86 assert(getType()->isPointerTy() && "Only pointers have alignments");
87 return getParent()->getParamAlignment(getArgNo()+1);
88
89}
90
Duncan Sands5d96f3f2009-12-11 08:36:17 +000091/// hasNestAttr - Return true if this argument has the nest attribute on
92/// it in its containing function.
93bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000094 if (!getType()->isPointerTy()) return false;
Bill Wendlingc9b22d72012-10-09 07:45:08 +000095 return getParent()->getParamAttributes(getArgNo()+1).
96 hasAttribute(Attributes::Nest);
Duncan Sands5d96f3f2009-12-11 08:36:17 +000097}
98
Chris Lattnere30f09d2008-01-24 17:47:11 +000099/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
100/// it in its containing function.
101bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000102 if (!getType()->isPointerTy()) return false;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000103 return getParent()->getParamAttributes(getArgNo()+1).
104 hasAttribute(Attributes::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000105}
106
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000107/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
108/// on it in its containing function.
109bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000110 if (!getType()->isPointerTy()) return false;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000111 return getParent()->getParamAttributes(getArgNo()+1).
112 hasAttribute(Attributes::NoCapture);
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000113}
114
Owen Andersonc64dfb42008-02-17 23:22:28 +0000115/// hasSRetAttr - Return true if this argument has the sret attribute on
116/// it in its containing function.
117bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000118 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000119 if (this != getParent()->arg_begin())
120 return false; // StructRet param must be first param
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000121 return getParent()->getParamAttributes(1).
122 hasAttribute(Attributes::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000123}
124
Devang Patel4c758ea2008-09-25 21:00:45 +0000125/// addAttr - Add a Attribute to an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000126void Argument::addAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000127 getParent()->addAttribute(getArgNo() + 1, attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000128}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000129
Devang Patel4c758ea2008-09-25 21:00:45 +0000130/// removeAttr - Remove a Attribute from an argument
Devang Patelba3fa6c2008-09-23 23:03:40 +0000131void Argument::removeAttr(Attributes attr) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000132 getParent()->removeAttribute(getArgNo() + 1, attr);
Duncan Sands66336db2008-07-08 09:41:30 +0000133}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000134
135
Chris Lattnerd255ae22002-04-09 19:39:35 +0000136//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000137// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000138//===----------------------------------------------------------------------===//
139
Owen Anderson47db9412009-07-22 00:24:57 +0000140LLVMContext &Function::getContext() const {
141 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000142}
143
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000144FunctionType *Function::getFunctionType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000145 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000146}
147
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148bool Function::isVarArg() const {
149 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000150}
151
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000152Type *Function::getReturnType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000153 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000154}
155
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156void Function::removeFromParent() {
157 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000158}
159
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000160void Function::eraseFromParent() {
161 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000162}
163
Reid Spencer019c8862007-04-09 15:01:12 +0000164//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000165// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000166//===----------------------------------------------------------------------===//
167
Chris Lattner229907c2011-07-18 04:54:35 +0000168Function::Function(FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000169 const Twine &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000170 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner8a923e72008-03-12 17:45:29 +0000171 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000172 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173 "invalid return type");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000174 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000175
Chris Lattnere2de9082007-08-18 06:14:52 +0000176 // If the function has arguments, mark them as lazily built.
177 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000178 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Chris Lattnere2de9082007-08-18 06:14:52 +0000179
Chris Lattner184b2982002-09-08 18:59:35 +0000180 // Make sure that we get added to a function
181 LeakDetector::addGarbageObject(this);
182
Chris Lattner6213ae02002-09-06 20:46:32 +0000183 if (ParentModule)
184 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000185
186 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000187 if (unsigned IID = getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000188 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000189
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190}
191
Gordon Henriksen14a55692007-12-10 02:14:30 +0000192Function::~Function() {
193 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000196 ArgumentList.clear();
197 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000198
Gordon Henriksend930f912008-08-17 18:44:35 +0000199 // Remove the function from the on-the-side GC table.
200 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201}
202
Chris Lattnere2de9082007-08-18 06:14:52 +0000203void Function::BuildLazyArguments() const {
204 // Create the arguments vector, all arguments start out unnamed.
Chris Lattner229907c2011-07-18 04:54:35 +0000205 FunctionType *FT = getFunctionType();
Chris Lattnere2de9082007-08-18 06:14:52 +0000206 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000207 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000208 "Cannot have void typed arguments!");
209 ArgumentList.push_back(new Argument(FT->getParamType(i)));
210 }
211
212 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000213 unsigned SDC = getSubclassDataFromValue();
214 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000215}
216
217size_t Function::arg_size() const {
218 return getFunctionType()->getNumParams();
219}
220bool Function::arg_empty() const {
221 return getFunctionType()->getNumParams() == 0;
222}
223
Chris Lattner4e8c4872002-03-23 22:51:58 +0000224void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000225 if (getParent())
226 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000228 if (getParent())
229 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
231
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232// dropAllReferences() - This function causes all the subinstructions to "let
233// go" of all references that they are maintaining. This allows one to
234// 'delete' a whole class at a time, even though there may be circular
235// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000236// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238// delete.
239//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000240void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000241 for (iterator I = begin(), E = end(); I != E; ++I)
242 I->dropAllReferences();
Chris Lattner3a395302009-10-28 03:37:35 +0000243
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000244 // Delete all basic blocks. They are now unused, except possibly by
245 // blockaddresses, but BasicBlock's destructor takes care of those.
246 while (!BasicBlocks.empty())
247 BasicBlocks.begin()->eraseFromParent();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248}
Chris Lattnerda975502001-09-10 07:58:01 +0000249
Devang Patel4c758ea2008-09-25 21:00:45 +0000250void Function::addAttribute(unsigned i, Attributes attr) {
251 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000252 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000253 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000254}
255
Devang Patel4c758ea2008-09-25 21:00:45 +0000256void Function::removeAttribute(unsigned i, Attributes attr) {
257 AttrListPtr PAL = getAttributes();
Duncan Sands66336db2008-07-08 09:41:30 +0000258 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000259 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000260}
261
Gordon Henriksend930f912008-08-17 18:44:35 +0000262// Maintain the GC name for each function in an on-the-side table. This saves
263// allocating an additional word in Function for programs which do not use GC
264// (i.e., most programs) at the cost of increased overhead for clients which do
265// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000266static DenseMap<const Function*,PooledStringPtr> *GCNames;
267static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000268static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000269
Gordon Henriksend930f912008-08-17 18:44:35 +0000270bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000271 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000272 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000273}
274
Gordon Henriksend930f912008-08-17 18:44:35 +0000275const char *Function::getGC() const {
276 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000277 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000278 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000279}
280
Gordon Henriksend930f912008-08-17 18:44:35 +0000281void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000282 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000283 if (!GCNamePool)
284 GCNamePool = new StringPool();
285 if (!GCNames)
286 GCNames = new DenseMap<const Function*,PooledStringPtr>();
287 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000288}
289
Gordon Henriksend930f912008-08-17 18:44:35 +0000290void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000291 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000292 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000293 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000294 if (GCNames->empty()) {
295 delete GCNames;
296 GCNames = 0;
297 if (GCNamePool->empty()) {
298 delete GCNamePool;
299 GCNamePool = 0;
300 }
301 }
302 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000303}
304
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000305/// copyAttributesFrom - copy all additional attributes (those not needed to
306/// create a Function) from the Function Src to this one.
307void Function::copyAttributesFrom(const GlobalValue *Src) {
308 assert(isa<Function>(Src) && "Expected a Function!");
309 GlobalValue::copyAttributesFrom(Src);
310 const Function *SrcF = cast<Function>(Src);
311 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000312 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000313 if (SrcF->hasGC())
314 setGC(SrcF->getGC());
315 else
316 clearGC();
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000317}
318
Chris Lattnerbb346d02003-05-08 03:47:33 +0000319/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000320/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000321/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000322/// zero to allow easy checking for whether a function is intrinsic or not. The
323/// particular intrinsic functions which correspond to this value are defined in
324/// llvm/Intrinsics.h.
325///
Dale Johannesenb842d522009-02-05 01:49:45 +0000326unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000327 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000328 if (!ValName)
329 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000330 unsigned Len = ValName->getKeyLength();
331 const char *Name = ValName->getKeyData();
332
Reid Spencer78d71f12007-04-16 16:56:54 +0000333 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000334 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000335 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000336
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000337#define GET_FUNCTION_RECOGNIZER
338#include "llvm/Intrinsics.gen"
339#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000340 return 0;
341}
342
Benjamin Kramere6e19332011-07-14 17:45:39 +0000343std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000344 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000345 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000346 "not_intrinsic",
347#define GET_INTRINSIC_NAME_TABLE
348#include "llvm/Intrinsics.gen"
349#undef GET_INTRINSIC_NAME_TABLE
350 };
Benjamin Kramere6e19332011-07-14 17:45:39 +0000351 if (Tys.empty())
Reid Spencer2a2117c2007-04-01 07:25:33 +0000352 return Table[id];
353 std::string Result(Table[id]);
Benjamin Kramere6e19332011-07-14 17:45:39 +0000354 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000355 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Mon P Wang2c839d42008-07-30 04:36:53 +0000356 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000357 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000358 }
359 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000360 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000361 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000362 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000363}
364
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000365
Chris Lattnerf39c2782012-05-27 18:28:35 +0000366/// IIT_Info - These are enumerators that describe the entries returned by the
367/// getIntrinsicInfoTableEntries function.
368///
369/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
370enum IIT_Info {
371 // Common values should be encoded with 0-15.
372 IIT_Done = 0,
373 IIT_I1 = 1,
374 IIT_I8 = 2,
375 IIT_I16 = 3,
376 IIT_I32 = 4,
377 IIT_I64 = 5,
378 IIT_F32 = 6,
379 IIT_F64 = 7,
380 IIT_V2 = 8,
381 IIT_V4 = 9,
382 IIT_V8 = 10,
383 IIT_V16 = 11,
384 IIT_V32 = 12,
385 IIT_MMX = 13,
386 IIT_PTR = 14,
387 IIT_ARG = 15,
388
389 // Values from 16+ are only encodable with the inefficient encoding.
390 IIT_METADATA = 16,
391 IIT_EMPTYSTRUCT = 17,
392 IIT_STRUCT2 = 18,
393 IIT_STRUCT3 = 19,
394 IIT_STRUCT4 = 20,
395 IIT_STRUCT5 = 21,
396 IIT_EXTEND_VEC_ARG = 22,
397 IIT_TRUNC_VEC_ARG = 23,
398 IIT_ANYPTR = 24
399};
400
401
402static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
403 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Chris Lattnera57c7972012-05-17 05:13:57 +0000404 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000405 unsigned StructElts = 2;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000406 using namespace Intrinsic;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000407
Chris Lattnera57c7972012-05-17 05:13:57 +0000408 switch (Info) {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000409 case IIT_Done:
410 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
411 return;
412 case IIT_MMX:
413 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
414 return;
415 case IIT_METADATA:
416 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
417 return;
418 case IIT_F32:
419 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
420 return;
421 case IIT_F64:
422 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
423 return;
424 case IIT_I1:
425 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
426 return;
427 case IIT_I8:
428 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
429 return;
430 case IIT_I16:
431 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
432 return;
433 case IIT_I32:
434 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
435 return;
436 case IIT_I64:
437 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
438 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000439 case IIT_V2:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000440 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
441 DecodeIITType(NextElt, Infos, OutputTable);
442 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000443 case IIT_V4:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000444 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
445 DecodeIITType(NextElt, Infos, OutputTable);
446 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000447 case IIT_V8:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000448 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
449 DecodeIITType(NextElt, Infos, OutputTable);
450 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000451 case IIT_V16:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000452 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
453 DecodeIITType(NextElt, Infos, OutputTable);
454 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000455 case IIT_V32:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000456 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
457 DecodeIITType(NextElt, Infos, OutputTable);
458 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000459 case IIT_PTR:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000460 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
461 DecodeIITType(NextElt, Infos, OutputTable);
462 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000463 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Chris Lattnerf39c2782012-05-27 18:28:35 +0000464 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
465 Infos[NextElt++]));
466 DecodeIITType(NextElt, Infos, OutputTable);
467 return;
Pete Cooper243efd72012-05-21 23:21:28 +0000468 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000469 case IIT_ARG: {
470 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
471 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
472 return;
473 }
474 case IIT_EXTEND_VEC_ARG: {
475 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
476 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
477 ArgInfo));
478 return;
479 }
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000480 case IIT_TRUNC_VEC_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000481 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
482 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
483 ArgInfo));
484 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000485 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000486 case IIT_EMPTYSTRUCT:
487 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
488 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000489 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
490 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
491 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
492 case IIT_STRUCT2: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000493 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
494
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000495 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnerf39c2782012-05-27 18:28:35 +0000496 DecodeIITType(NextElt, Infos, OutputTable);
497 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000498 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000499 }
500 llvm_unreachable("unhandled");
501}
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000502
Chris Lattnerf39c2782012-05-27 18:28:35 +0000503
504#define GET_INTRINSIC_GENERATOR_GLOBAL
505#include "llvm/Intrinsics.gen"
506#undef GET_INTRINSIC_GENERATOR_GLOBAL
507
508void Intrinsic::getIntrinsicInfoTableEntries(ID id,
509 SmallVectorImpl<IITDescriptor> &T){
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000510 // Check to see if the intrinsic's type was expressible by the table.
511 unsigned TableVal = IIT_Table[id-1];
Chris Lattnera3b0f522012-05-17 15:55:41 +0000512
513 // Decode the TableVal into an array of IITValues.
514 SmallVector<unsigned char, 8> IITValues;
515 ArrayRef<unsigned char> IITEntries;
516 unsigned NextElt = 0;
517 if ((TableVal >> 31) != 0) {
518 // This is an offset into the IIT_LongEncodingTable.
519 IITEntries = IIT_LongEncodingTable;
520
521 // Strip sentinel bit.
522 NextElt = (TableVal << 1) >> 1;
523 } else {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000524 // Decode the TableVal into an array of IITValues. If the entry was encoded
525 // into a single word in the table itself, decode it now.
Chris Lattnera57c7972012-05-17 05:13:57 +0000526 do {
527 IITValues.push_back(TableVal & 0xF);
528 TableVal >>= 4;
529 } while (TableVal);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000530
Chris Lattnera3b0f522012-05-17 15:55:41 +0000531 IITEntries = IITValues;
532 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000533 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000534
535 // Okay, decode the table into the output vector of IITDescriptors.
536 DecodeIITType(NextElt, IITEntries, T);
537 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
538 DecodeIITType(NextElt, IITEntries, T);
539}
540
541
542static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
543 ArrayRef<Type*> Tys, LLVMContext &Context) {
544 using namespace Intrinsic;
545 IITDescriptor D = Infos.front();
546 Infos = Infos.slice(1);
547
548 switch (D.Kind) {
549 case IITDescriptor::Void: return Type::getVoidTy(Context);
550 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
551 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
552 case IITDescriptor::Float: return Type::getFloatTy(Context);
553 case IITDescriptor::Double: return Type::getDoubleTy(Context);
554
555 case IITDescriptor::Integer:
556 return IntegerType::get(Context, D.Integer_Width);
557 case IITDescriptor::Vector:
558 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
559 case IITDescriptor::Pointer:
560 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
561 D.Pointer_AddressSpace);
562 case IITDescriptor::Struct: {
563 Type *Elts[5];
564 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
565 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
566 Elts[i] = DecodeFixedType(Infos, Tys, Context);
567 return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
568 }
569
570 case IITDescriptor::Argument:
571 return Tys[D.getArgumentNumber()];
572 case IITDescriptor::ExtendVecArgument:
573 return VectorType::getExtendedElementVectorType(cast<VectorType>(
574 Tys[D.getArgumentNumber()]));
575
576 case IITDescriptor::TruncVecArgument:
577 return VectorType::getTruncatedElementVectorType(cast<VectorType>(
578 Tys[D.getArgumentNumber()]));
579 }
580 llvm_unreachable("unhandled");
581}
582
583
584
585FunctionType *Intrinsic::getType(LLVMContext &Context,
586 ID id, ArrayRef<Type*> Tys) {
587 SmallVector<IITDescriptor, 8> Table;
588 getIntrinsicInfoTableEntries(id, Table);
589
590 ArrayRef<IITDescriptor> TableRef = Table;
591 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000592
593 SmallVector<Type*, 8> ArgTys;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000594 while (!TableRef.empty())
595 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000596
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000597 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000598}
599
Mon P Wangb4024932009-02-24 23:17:49 +0000600bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000601#define GET_INTRINSIC_OVERLOAD_TABLE
602#include "llvm/Intrinsics.gen"
603#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +0000604}
605
Chris Lattner49b7ee12009-01-12 01:18:58 +0000606/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000607#define GET_INTRINSIC_ATTRIBUTES
608#include "llvm/Intrinsics.gen"
609#undef GET_INTRINSIC_ATTRIBUTES
610
Benjamin Kramere6e19332011-07-14 17:45:39 +0000611Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000612 // There can never be multiple globals with the same name of different types,
613 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000614 return
Benjamin Kramere6e19332011-07-14 17:45:39 +0000615 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
616 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000617}
618
Dale Johannesenb842d522009-02-05 01:49:45 +0000619// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
620#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
621#include "llvm/Intrinsics.gen"
622#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
623
Gabor Greif161cb042010-03-23 14:40:20 +0000624/// hasAddressTaken - returns true if there are any uses of this function
625/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000626bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greifc78d7202010-03-25 23:06:16 +0000627 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000628 const User *U = *I;
Jay Foadca0c4992012-05-12 08:30:16 +0000629 if (isa<BlockAddress>(U))
630 continue;
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000631 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
632 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greif5d5db532010-04-01 08:21:08 +0000633 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000634 if (!CS.isCallee(I))
635 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000636 }
637 return false;
638}
639
Eli Friedman1923a332011-10-20 05:23:42 +0000640bool Function::isDefTriviallyDead() const {
641 // Check the linkage
642 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
643 !hasAvailableExternallyLinkage())
644 return false;
645
646 // Check if the function is used by anything other than a blockaddress.
647 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
648 if (!isa<BlockAddress>(*I))
649 return false;
650
651 return true;
652}
653
Bill Wendling63a4ea12011-10-17 18:43:40 +0000654/// callsFunctionThatReturnsTwice - Return true if the function has a call to
655/// setjmp or other function that gcc recognizes as "returning twice".
656bool Function::callsFunctionThatReturnsTwice() const {
657 for (const_inst_iterator
658 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
659 const CallInst* callInst = dyn_cast<CallInst>(&*I);
660 if (!callInst)
661 continue;
662 if (callInst->canReturnTwice())
663 return true;
664 }
665
666 return false;
667}
668