blob: a64a4fa64faf882efc0a1e818e48804e78b7023e [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"
Michael Ilseman4c8e74f2013-03-01 18:48:54 +000015#include "LLVMContextImpl.h"
Chris Lattner7e708292002-06-25 16:13:24 +000016#include "SymbolTableListTraitsImpl.h"
Gordon Henriksen80a75bf2007-12-10 03:18:06 +000017#include "llvm/ADT/DenseMap.h"
Rafael Espindola0e00c6c2011-05-16 03:05:33 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattnercaa4ae72004-12-05 06:43:27 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/IntrinsicInst.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/CallSite.h"
26#include "llvm/Support/InstIterator.h"
27#include "llvm/Support/LeakDetector.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/RWMutex.h"
30#include "llvm/Support/StringPool.h"
31#include "llvm/Support/Threading.h"
Chris Lattner31f84992003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner7e708292002-06-25 16:13:24 +000034// Explicit instantiations of SymbolTableListTraits since some of the methods
35// are not in the public header file...
John McCallc63ca0a2009-12-19 00:55:12 +000036template class llvm::SymbolTableListTraits<Argument, Function>;
37template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner70cc3392001-09-10 07:58:01 +000039//===----------------------------------------------------------------------===//
Chris Lattner80dd50b2002-04-09 19:39:35 +000040// Argument Implementation
41//===----------------------------------------------------------------------===//
42
David Blaikie2d24e2a2011-12-20 02:50:00 +000043void Argument::anchor() { }
44
Chris Lattnerdb125cf2011-07-18 04:54:35 +000045Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
Chris Lattnerdec628e2007-02-12 05:18:08 +000046 : Value(Ty, Value::ArgumentVal) {
Chris Lattnerbded1322002-09-06 21:33:15 +000047 Parent = 0;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000048
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
51
Chris Lattnerbded1322002-09-06 21:33:15 +000052 if (Par)
53 Par->getArgumentList().push_back(this);
Chris Lattnerdec628e2007-02-12 05:18:08 +000054 setName(Name);
Chris Lattnerbded1322002-09-06 21:33:15 +000055}
56
Chris Lattnerbded1322002-09-06 21:33:15 +000057void Argument::setParent(Function *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000058 if (getParent())
59 LeakDetector::addGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000060 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +000061 if (getParent())
62 LeakDetector::removeGarbageObject(this);
Chris Lattnerbded1322002-09-06 21:33:15 +000063}
64
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000065/// getArgNo - Return the index of this formal argument in its containing
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000066/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000067unsigned Argument::getArgNo() const {
68 const Function *F = getParent();
69 assert(F && "Argument is not in a function");
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000070
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000071 Function::const_arg_iterator AI = F->arg_begin();
72 unsigned ArgIdx = 0;
73 for (; &*AI != this; ++AI)
74 ++ArgIdx;
75
76 return ArgIdx;
77}
78
79/// hasByValAttr - Return true if this argument has the byval attribute on it
80/// in its containing function.
81bool Argument::hasByValAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +000082 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +000083 return getParent()->getAttributes().
84 hasAttribute(getArgNo()+1, Attribute::ByVal);
Chris Lattnerde6fa5f2008-01-24 17:47:11 +000085}
86
Chris Lattnerae441cc2011-05-22 23:57:23 +000087unsigned Argument::getParamAlignment() const {
88 assert(getType()->isPointerTy() && "Only pointers have alignments");
89 return getParent()->getParamAlignment(getArgNo()+1);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +000090
Chris Lattnerae441cc2011-05-22 23:57:23 +000091}
92
Duncan Sandsbe1ce0a2009-12-11 08:36:17 +000093/// hasNestAttr - Return true if this argument has the nest attribute on
94/// it in its containing function.
95bool Argument::hasNestAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +000096 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +000097 return getParent()->getAttributes().
98 hasAttribute(getArgNo()+1, Attribute::Nest);
Duncan Sandsbe1ce0a2009-12-11 08:36:17 +000099}
100
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000101/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
102/// it in its containing function.
103bool Argument::hasNoAliasAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000104 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +0000105 return getParent()->getAttributes().
106 hasAttribute(getArgNo()+1, Attribute::NoAlias);
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000107}
108
Duncan Sands17da06f2008-12-31 18:08:59 +0000109/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
110/// on it in its containing function.
111bool Argument::hasNoCaptureAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000112 if (!getType()->isPointerTy()) return false;
Bill Wendling39cd0c82012-12-30 12:45:13 +0000113 return getParent()->getAttributes().
114 hasAttribute(getArgNo()+1, Attribute::NoCapture);
Duncan Sands17da06f2008-12-31 18:08:59 +0000115}
116
Owen Anderson7d542542008-02-17 23:22:28 +0000117/// hasSRetAttr - Return true if this argument has the sret attribute on
118/// it in its containing function.
119bool Argument::hasStructRetAttr() const {
Duncan Sands1df98592010-02-16 11:11:14 +0000120 if (!getType()->isPointerTy()) return false;
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000121 if (this != getParent()->arg_begin())
122 return false; // StructRet param must be first param
Bill Wendling39cd0c82012-12-30 12:45:13 +0000123 return getParent()->getAttributes().
124 hasAttribute(1, Attribute::StructRet);
Owen Anderson7d542542008-02-17 23:22:28 +0000125}
126
Stephen Lin456ca042013-04-20 05:14:40 +0000127/// hasReturnedAttr - Return true if this argument has the returned attribute on
128/// it in its containing function.
129bool Argument::hasReturnedAttr() const {
130 return getParent()->getAttributes().
131 hasAttribute(getArgNo()+1, Attribute::Returned);
132}
133
Nick Lewyckydc897372013-07-06 00:29:58 +0000134/// Return true if this argument has the readonly or readnone attribute on it
135/// in its containing function.
136bool Argument::onlyReadsMemory() const {
137 return getParent()->getAttributes().
138 hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
139 getParent()->getAttributes().
140 hasAttribute(getArgNo()+1, Attribute::ReadNone);
141}
142
Bill Wendling28d65722013-01-23 06:14:59 +0000143/// addAttr - Add attributes to an argument.
144void Argument::addAttr(AttributeSet AS) {
Bill Wendlingfece4422013-02-21 19:46:51 +0000145 assert(AS.getNumSlots() <= 1 &&
Bill Wendlingb7a1dda2013-02-20 23:04:11 +0000146 "Trying to add more than one attribute set to an argument!");
147 AttrBuilder B(AS, AS.getSlotIndex(0));
148 getParent()->addAttributes(getArgNo() + 1,
149 AttributeSet::get(Parent->getContext(),
150 getArgNo() + 1, B));
Gordon Henriksene2435da2008-04-28 17:37:06 +0000151}
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000152
Bill Wendling28d65722013-01-23 06:14:59 +0000153/// removeAttr - Remove attributes from an argument.
154void Argument::removeAttr(AttributeSet AS) {
Bill Wendlingfece4422013-02-21 19:46:51 +0000155 assert(AS.getNumSlots() <= 1 &&
Bill Wendlingb7a1dda2013-02-20 23:04:11 +0000156 "Trying to remove more than one attribute set from an argument!");
157 AttrBuilder B(AS, AS.getSlotIndex(0));
158 getParent()->removeAttributes(getArgNo() + 1,
159 AttributeSet::get(Parent->getContext(),
160 getArgNo() + 1, B));
Duncan Sandscfa3c232008-07-08 09:41:30 +0000161}
Chris Lattnerde6fa5f2008-01-24 17:47:11 +0000162
Chris Lattner80dd50b2002-04-09 19:39:35 +0000163//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164// Helper Methods in Function
Reid Spencer4746ecf2007-04-09 15:01:12 +0000165//===----------------------------------------------------------------------===//
166
Owen Andersone922c022009-07-22 00:24:57 +0000167LLVMContext &Function::getContext() const {
168 return getType()->getContext();
Owen Anderson62fabf52009-07-02 18:03:58 +0000169}
170
Chris Lattner1afcace2011-07-09 17:41:24 +0000171FunctionType *Function::getFunctionType() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000172 return cast<FunctionType>(getType()->getElementType());
Reid Spencer4746ecf2007-04-09 15:01:12 +0000173}
174
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000175bool Function::isVarArg() const {
176 return getFunctionType()->isVarArg();
Reid Spencer4746ecf2007-04-09 15:01:12 +0000177}
178
Chris Lattner1afcace2011-07-09 17:41:24 +0000179Type *Function::getReturnType() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000180 return getFunctionType()->getReturnType();
Duncan Sands827cde12007-11-25 14:10:56 +0000181}
182
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000183void Function::removeFromParent() {
184 getParent()->getFunctionList().remove(this);
Duncan Sands827cde12007-11-25 14:10:56 +0000185}
186
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000187void Function::eraseFromParent() {
188 getParent()->getFunctionList().erase(this);
Reid Spencer4746ecf2007-04-09 15:01:12 +0000189}
190
Reid Spencer4746ecf2007-04-09 15:01:12 +0000191//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +0000192// Function Implementation
Chris Lattner70cc3392001-09-10 07:58:01 +0000193//===----------------------------------------------------------------------===//
194
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000195Function::Function(FunctionType *Ty, LinkageTypes Linkage,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000196 const Twine &name, Module *ParentModule)
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000197 : GlobalValue(PointerType::getUnqual(Ty),
Chris Lattner58d74912008-03-12 17:45:29 +0000198 Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattnerc8e222b2009-01-05 07:58:59 +0000199 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattner1afcace2011-07-09 17:41:24 +0000200 "invalid return type");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000201 SymTab = new ValueSymbolTable();
Chris Lattner48270152002-09-06 20:46:32 +0000202
Chris Lattner0162c182007-08-18 06:14:52 +0000203 // If the function has arguments, mark them as lazily built.
204 if (Ty->getNumParams())
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000205 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000206
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000207 // Make sure that we get added to a function
208 LeakDetector::addGarbageObject(this);
209
Chris Lattner48270152002-09-06 20:46:32 +0000210 if (ParentModule)
211 ParentModule->getFunctionList().push_back(this);
Duncan Sands79ab3e82008-04-07 13:39:11 +0000212
213 // Ensure intrinsics have the right parameter attributes.
Dale Johannesen49de9822009-02-05 01:49:45 +0000214 if (unsigned IID = getIntrinsicID())
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000215 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
Devang Patel81b2ab82008-09-02 20:51:15 +0000216
Chris Lattner00950542001-06-06 20:29:01 +0000217}
218
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000219Function::~Function() {
220 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner00950542001-06-06 20:29:01 +0000221
Chris Lattner00950542001-06-06 20:29:01 +0000222 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksenafba8fe2007-12-10 02:14:30 +0000223 ArgumentList.clear();
224 delete SymTab;
Reid Spencerb90909e2007-04-22 17:28:03 +0000225
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000226 // Remove the function from the on-the-side GC table.
227 clearGC();
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000228
229 // Remove the intrinsicID from the Cache.
Chris Lattner1ac186e2013-03-20 21:04:53 +0000230 if (getValueName() && isIntrinsic())
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000231 getContext().pImpl->IntrinsicIDCache.erase(this);
Chris Lattner00950542001-06-06 20:29:01 +0000232}
233
Chris Lattner0162c182007-08-18 06:14:52 +0000234void Function::BuildLazyArguments() const {
235 // Create the arguments vector, all arguments start out unnamed.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000236 FunctionType *FT = getFunctionType();
Chris Lattner0162c182007-08-18 06:14:52 +0000237 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerf0127052010-01-05 13:12:22 +0000238 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattner0162c182007-08-18 06:14:52 +0000239 "Cannot have void typed arguments!");
240 ArgumentList.push_back(new Argument(FT->getParamType(i)));
241 }
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000242
Chris Lattner0162c182007-08-18 06:14:52 +0000243 // Clear the lazy arguments bit.
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000244 unsigned SDC = getSubclassDataFromValue();
245 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattner0162c182007-08-18 06:14:52 +0000246}
247
248size_t Function::arg_size() const {
249 return getFunctionType()->getNumParams();
250}
251bool Function::arg_empty() const {
252 return getFunctionType()->getNumParams() == 0;
253}
254
Chris Lattnere7506a32002-03-23 22:51:58 +0000255void Function::setParent(Module *parent) {
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000256 if (getParent())
257 LeakDetector::addGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000258 Parent = parent;
Chris Lattnerd1e693f2002-09-08 18:59:35 +0000259 if (getParent())
260 LeakDetector::removeGarbageObject(this);
Chris Lattner00950542001-06-06 20:29:01 +0000261}
262
Chris Lattner00950542001-06-06 20:29:01 +0000263// dropAllReferences() - This function causes all the subinstructions to "let
264// go" of all references that they are maintaining. This allows one to
265// 'delete' a whole class at a time, even though there may be circular
266// references... first all references are dropped, and all use counts go to
Misha Brukman6b634522003-10-10 17:54:14 +0000267// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanfd939082005-04-21 23:48:37 +0000268// valid on an object that has "dropped all references", except operator
Chris Lattner00950542001-06-06 20:29:01 +0000269// delete.
270//
Chris Lattnere7506a32002-03-23 22:51:58 +0000271void Function::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000272 for (iterator I = begin(), E = end(); I != E; ++I)
273 I->dropAllReferences();
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000274
Dan Gohmana6399ae2010-12-07 19:56:51 +0000275 // Delete all basic blocks. They are now unused, except possibly by
276 // blockaddresses, but BasicBlock's destructor takes care of those.
277 while (!BasicBlocks.empty())
278 BasicBlocks.begin()->eraseFromParent();
Peter Collingbourne1e3037f2013-09-16 01:08:15 +0000279
280 // Prefix data is stored in a side table.
281 setPrefixData(0);
Chris Lattner00950542001-06-06 20:29:01 +0000282}
Chris Lattner70cc3392001-09-10 07:58:01 +0000283
Bill Wendling70d2ca02013-01-23 00:20:53 +0000284void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000285 AttributeSet PAL = getAttributes();
Bill Wendling70d2ca02013-01-23 00:20:53 +0000286 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel05988662008-09-25 21:00:45 +0000287 setAttributes(PAL);
Eric Christopher0bf7b412008-05-16 20:39:43 +0000288}
289
Bill Wendling70d2ca02013-01-23 00:20:53 +0000290void Function::addAttributes(unsigned i, AttributeSet attrs) {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000291 AttributeSet PAL = getAttributes();
Bill Wendling70d2ca02013-01-23 00:20:53 +0000292 PAL = PAL.addAttributes(getContext(), i, attrs);
293 setAttributes(PAL);
294}
295
Bill Wendling8246df62013-01-23 00:45:55 +0000296void Function::removeAttributes(unsigned i, AttributeSet attrs) {
Bill Wendling70d2ca02013-01-23 00:20:53 +0000297 AttributeSet PAL = getAttributes();
Bill Wendling8246df62013-01-23 00:45:55 +0000298 PAL = PAL.removeAttributes(getContext(), i, attrs);
Devang Patel05988662008-09-25 21:00:45 +0000299 setAttributes(PAL);
Duncan Sandscfa3c232008-07-08 09:41:30 +0000300}
301
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000302// Maintain the GC name for each function in an on-the-side table. This saves
303// allocating an additional word in Function for programs which do not use GC
304// (i.e., most programs) at the cost of increased overhead for clients which do
305// use GC.
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000306static DenseMap<const Function*,PooledStringPtr> *GCNames;
307static StringPool *GCNamePool;
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000308static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000309
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000310bool Function::hasGC() const {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000311 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000312 return GCNames && GCNames->count(this);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000313}
314
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000315const char *Function::getGC() const {
316 assert(hasGC() && "Function has no collector");
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000317 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Andersonb2c0fe42009-06-18 20:56:48 +0000318 return *(*GCNames)[this];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000319}
320
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000321void Function::setGC(const char *Str) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000322 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000323 if (!GCNamePool)
324 GCNamePool = new StringPool();
325 if (!GCNames)
326 GCNames = new DenseMap<const Function*,PooledStringPtr>();
327 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000328}
329
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000330void Function::clearGC() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000331 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000332 if (GCNames) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000333 GCNames->erase(this);
Owen Andersonbf5ec1b2009-06-17 23:49:06 +0000334 if (GCNames->empty()) {
335 delete GCNames;
336 GCNames = 0;
337 if (GCNamePool->empty()) {
338 delete GCNamePool;
339 GCNamePool = 0;
340 }
341 }
342 }
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000343}
344
Duncan Sands28c3cff2008-05-26 19:58:59 +0000345/// copyAttributesFrom - copy all additional attributes (those not needed to
346/// create a Function) from the Function Src to this one.
347void Function::copyAttributesFrom(const GlobalValue *Src) {
348 assert(isa<Function>(Src) && "Expected a Function!");
349 GlobalValue::copyAttributesFrom(Src);
350 const Function *SrcF = cast<Function>(Src);
351 setCallingConv(SrcF->getCallingConv());
Devang Patel05988662008-09-25 21:00:45 +0000352 setAttributes(SrcF->getAttributes());
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000353 if (SrcF->hasGC())
354 setGC(SrcF->getGC());
355 else
356 clearGC();
Peter Collingbourne1e3037f2013-09-16 01:08:15 +0000357 if (SrcF->hasPrefixData())
358 setPrefixData(SrcF->getPrefixData());
359 else
360 setPrefixData(0);
Duncan Sands28c3cff2008-05-26 19:58:59 +0000361}
362
Chris Lattnerdd035d12003-05-08 03:47:33 +0000363/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeked0fde302003-11-11 22:41:34 +0000364/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukman6b634522003-10-10 17:54:14 +0000365/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerdd035d12003-05-08 03:47:33 +0000366/// zero to allow easy checking for whether a function is intrinsic or not. The
367/// particular intrinsic functions which correspond to this value are defined in
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000368/// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent
369/// requests for the same ID return results much faster from the cache.
Chris Lattnerdd035d12003-05-08 03:47:33 +0000370///
Dale Johannesen49de9822009-02-05 01:49:45 +0000371unsigned Function::getIntrinsicID() const {
Chris Lattner3b515802007-02-15 19:17:16 +0000372 const ValueName *ValName = this->getValueName();
Michael Ilsemanf846f162012-12-19 23:17:20 +0000373 if (!ValName || !isIntrinsic())
Reid Spencer085659f2007-04-16 07:08:44 +0000374 return 0;
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000375
376 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
377 getContext().pImpl->IntrinsicIDCache;
Chris Lattner1ac186e2013-03-20 21:04:53 +0000378 if (!IntrinsicIDCache.count(this)) {
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000379 unsigned Id = lookupIntrinsicID();
380 IntrinsicIDCache[this]=Id;
381 return Id;
382 }
383 return IntrinsicIDCache[this];
384}
385
386/// This private method does the actual lookup of an intrinsic ID when the query
387/// could not be answered from the cache.
388unsigned Function::lookupIntrinsicID() const {
389 const ValueName *ValName = this->getValueName();
Chris Lattner3b515802007-02-15 19:17:16 +0000390 unsigned Len = ValName->getKeyLength();
391 const char *Name = ValName->getKeyData();
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000392
Chris Lattner9a016ff2006-03-09 20:35:01 +0000393#define GET_FUNCTION_RECOGNIZER
Chandler Carruth351ba142013-01-02 12:09:16 +0000394#include "llvm/IR/Intrinsics.gen"
Chris Lattner9a016ff2006-03-09 20:35:01 +0000395#undef GET_FUNCTION_RECOGNIZER
Michael Ilseman4c8e74f2013-03-01 18:48:54 +0000396
Chris Lattnerdd035d12003-05-08 03:47:33 +0000397 return 0;
398}
399
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000400std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattnerb8474232006-03-25 06:32:47 +0000401 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattner607b8eb2011-04-25 22:14:33 +0000402 static const char * const Table[] = {
Chris Lattnerb8474232006-03-25 06:32:47 +0000403 "not_intrinsic",
404#define GET_INTRINSIC_NAME_TABLE
Chandler Carruth351ba142013-01-02 12:09:16 +0000405#include "llvm/IR/Intrinsics.gen"
Chris Lattnerb8474232006-03-25 06:32:47 +0000406#undef GET_INTRINSIC_NAME_TABLE
407 };
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000408 if (Tys.empty())
Reid Spencer1437a092007-04-01 07:25:33 +0000409 return Table[id];
410 std::string Result(Table[id]);
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000411 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000412 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000413 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Andersone50ed302009-08-10 22:56:29 +0000414 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wange3b3a722008-07-30 04:36:53 +0000415 }
416 else if (Tys[i])
Owen Andersone50ed302009-08-10 22:56:29 +0000417 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wange3b3a722008-07-30 04:36:53 +0000418 }
Reid Spencer1437a092007-04-01 07:25:33 +0000419 return Result;
Chris Lattnerb8474232006-03-25 06:32:47 +0000420}
421
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000422
Chris Lattner908a8312012-05-27 18:28:35 +0000423/// IIT_Info - These are enumerators that describe the entries returned by the
424/// getIntrinsicInfoTableEntries function.
425///
426/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
427enum IIT_Info {
428 // Common values should be encoded with 0-15.
429 IIT_Done = 0,
430 IIT_I1 = 1,
431 IIT_I8 = 2,
432 IIT_I16 = 3,
433 IIT_I32 = 4,
434 IIT_I64 = 5,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000435 IIT_F16 = 6,
436 IIT_F32 = 7,
437 IIT_F64 = 8,
438 IIT_V2 = 9,
439 IIT_V4 = 10,
440 IIT_V8 = 11,
441 IIT_V16 = 12,
442 IIT_V32 = 13,
Chris Lattner908a8312012-05-27 18:28:35 +0000443 IIT_PTR = 14,
444 IIT_ARG = 15,
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000445
Chris Lattner908a8312012-05-27 18:28:35 +0000446 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000447 IIT_MMX = 16,
448 IIT_METADATA = 17,
449 IIT_EMPTYSTRUCT = 18,
450 IIT_STRUCT2 = 19,
451 IIT_STRUCT3 = 20,
452 IIT_STRUCT4 = 21,
453 IIT_STRUCT5 = 22,
454 IIT_EXTEND_VEC_ARG = 23,
455 IIT_TRUNC_VEC_ARG = 24,
456 IIT_ANYPTR = 25
Chris Lattner908a8312012-05-27 18:28:35 +0000457};
458
459
460static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
461 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Chris Lattner626b1082012-05-17 05:13:57 +0000462 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000463 unsigned StructElts = 2;
Chris Lattner908a8312012-05-27 18:28:35 +0000464 using namespace Intrinsic;
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000465
Chris Lattner626b1082012-05-17 05:13:57 +0000466 switch (Info) {
Chris Lattner908a8312012-05-27 18:28:35 +0000467 case IIT_Done:
468 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
469 return;
470 case IIT_MMX:
471 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
472 return;
473 case IIT_METADATA:
474 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
475 return;
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000476 case IIT_F16:
477 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
478 return;
Chris Lattner908a8312012-05-27 18:28:35 +0000479 case IIT_F32:
480 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
481 return;
482 case IIT_F64:
483 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
484 return;
485 case IIT_I1:
486 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
487 return;
488 case IIT_I8:
489 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
490 return;
491 case IIT_I16:
492 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
493 return;
494 case IIT_I32:
495 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
496 return;
497 case IIT_I64:
498 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
499 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000500 case IIT_V2:
Chris Lattner908a8312012-05-27 18:28:35 +0000501 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
502 DecodeIITType(NextElt, Infos, OutputTable);
503 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000504 case IIT_V4:
Chris Lattner908a8312012-05-27 18:28:35 +0000505 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
506 DecodeIITType(NextElt, Infos, OutputTable);
507 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000508 case IIT_V8:
Chris Lattner908a8312012-05-27 18:28:35 +0000509 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
510 DecodeIITType(NextElt, Infos, OutputTable);
511 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000512 case IIT_V16:
Chris Lattner908a8312012-05-27 18:28:35 +0000513 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
514 DecodeIITType(NextElt, Infos, OutputTable);
515 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000516 case IIT_V32:
Chris Lattner908a8312012-05-27 18:28:35 +0000517 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
518 DecodeIITType(NextElt, Infos, OutputTable);
519 return;
Chris Lattnera48289a2012-05-23 05:19:18 +0000520 case IIT_PTR:
Chris Lattner908a8312012-05-27 18:28:35 +0000521 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
522 DecodeIITType(NextElt, Infos, OutputTable);
523 return;
Chris Lattnera48289a2012-05-23 05:19:18 +0000524 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000525 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattner908a8312012-05-27 18:28:35 +0000526 Infos[NextElt++]));
527 DecodeIITType(NextElt, Infos, OutputTable);
528 return;
Pete Cooperb4285112012-05-21 23:21:28 +0000529 }
Chris Lattner908a8312012-05-27 18:28:35 +0000530 case IIT_ARG: {
531 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
532 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
533 return;
534 }
535 case IIT_EXTEND_VEC_ARG: {
536 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
537 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
538 ArgInfo));
539 return;
540 }
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000541 case IIT_TRUNC_VEC_ARG: {
Chris Lattner908a8312012-05-27 18:28:35 +0000542 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
543 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
544 ArgInfo));
545 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000546 }
Chris Lattner908a8312012-05-27 18:28:35 +0000547 case IIT_EMPTYSTRUCT:
548 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
549 return;
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000550 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
551 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
552 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
553 case IIT_STRUCT2: {
Chris Lattner908a8312012-05-27 18:28:35 +0000554 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
555
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000556 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattner908a8312012-05-27 18:28:35 +0000557 DecodeIITType(NextElt, Infos, OutputTable);
558 return;
Chris Lattner46aaf692012-05-17 04:30:58 +0000559 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000560 }
561 llvm_unreachable("unhandled");
562}
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000563
Chris Lattner908a8312012-05-27 18:28:35 +0000564
565#define GET_INTRINSIC_GENERATOR_GLOBAL
Chandler Carruth351ba142013-01-02 12:09:16 +0000566#include "llvm/IR/Intrinsics.gen"
Chris Lattner908a8312012-05-27 18:28:35 +0000567#undef GET_INTRINSIC_GENERATOR_GLOBAL
568
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000569void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattner908a8312012-05-27 18:28:35 +0000570 SmallVectorImpl<IITDescriptor> &T){
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000571 // Check to see if the intrinsic's type was expressible by the table.
572 unsigned TableVal = IIT_Table[id-1];
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000573
Chris Lattner387c9dc2012-05-17 15:55:41 +0000574 // Decode the TableVal into an array of IITValues.
575 SmallVector<unsigned char, 8> IITValues;
576 ArrayRef<unsigned char> IITEntries;
577 unsigned NextElt = 0;
578 if ((TableVal >> 31) != 0) {
579 // This is an offset into the IIT_LongEncodingTable.
580 IITEntries = IIT_LongEncodingTable;
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000581
Chris Lattner387c9dc2012-05-17 15:55:41 +0000582 // Strip sentinel bit.
583 NextElt = (TableVal << 1) >> 1;
584 } else {
Chris Lattner908a8312012-05-27 18:28:35 +0000585 // Decode the TableVal into an array of IITValues. If the entry was encoded
586 // into a single word in the table itself, decode it now.
Chris Lattner626b1082012-05-17 05:13:57 +0000587 do {
588 IITValues.push_back(TableVal & 0xF);
589 TableVal >>= 4;
590 } while (TableVal);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000591
Chris Lattner387c9dc2012-05-17 15:55:41 +0000592 IITEntries = IITValues;
593 NextElt = 0;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000594 }
Chris Lattner908a8312012-05-27 18:28:35 +0000595
596 // Okay, decode the table into the output vector of IITDescriptors.
597 DecodeIITType(NextElt, IITEntries, T);
598 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
599 DecodeIITType(NextElt, IITEntries, T);
600}
601
602
603static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
604 ArrayRef<Type*> Tys, LLVMContext &Context) {
605 using namespace Intrinsic;
606 IITDescriptor D = Infos.front();
607 Infos = Infos.slice(1);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000608
Chris Lattner908a8312012-05-27 18:28:35 +0000609 switch (D.Kind) {
610 case IITDescriptor::Void: return Type::getVoidTy(Context);
611 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
612 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000613 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattner908a8312012-05-27 18:28:35 +0000614 case IITDescriptor::Float: return Type::getFloatTy(Context);
615 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000616
Chris Lattner908a8312012-05-27 18:28:35 +0000617 case IITDescriptor::Integer:
618 return IntegerType::get(Context, D.Integer_Width);
619 case IITDescriptor::Vector:
620 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
621 case IITDescriptor::Pointer:
622 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
623 D.Pointer_AddressSpace);
624 case IITDescriptor::Struct: {
625 Type *Elts[5];
626 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
627 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
628 Elts[i] = DecodeFixedType(Infos, Tys, Context);
629 return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
630 }
631
632 case IITDescriptor::Argument:
633 return Tys[D.getArgumentNumber()];
634 case IITDescriptor::ExtendVecArgument:
635 return VectorType::getExtendedElementVectorType(cast<VectorType>(
636 Tys[D.getArgumentNumber()]));
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000637
Chris Lattner908a8312012-05-27 18:28:35 +0000638 case IITDescriptor::TruncVecArgument:
639 return VectorType::getTruncatedElementVectorType(cast<VectorType>(
640 Tys[D.getArgumentNumber()]));
641 }
642 llvm_unreachable("unhandled");
643}
644
645
646
647FunctionType *Intrinsic::getType(LLVMContext &Context,
648 ID id, ArrayRef<Type*> Tys) {
649 SmallVector<IITDescriptor, 8> Table;
650 getIntrinsicInfoTableEntries(id, Table);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000651
Chris Lattner908a8312012-05-27 18:28:35 +0000652 ArrayRef<IITDescriptor> TableRef = Table;
653 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000654
Chris Lattner387c9dc2012-05-17 15:55:41 +0000655 SmallVector<Type*, 8> ArgTys;
Chris Lattner908a8312012-05-27 18:28:35 +0000656 while (!TableRef.empty())
657 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey95af5922007-02-07 20:38:26 +0000658
Michael Ilseman0f47e7e2012-12-17 20:37:55 +0000659 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey95af5922007-02-07 20:38:26 +0000660}
661
Mon P Wang0d52ff12009-02-24 23:17:49 +0000662bool Intrinsic::isOverloaded(ID id) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000663#define GET_INTRINSIC_OVERLOAD_TABLE
Chandler Carruth351ba142013-01-02 12:09:16 +0000664#include "llvm/IR/Intrinsics.gen"
Mon P Wang0d52ff12009-02-24 23:17:49 +0000665#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wang0d52ff12009-02-24 23:17:49 +0000666}
667
Chris Lattner048ffb22009-01-12 01:18:58 +0000668/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000669#define GET_INTRINSIC_ATTRIBUTES
Chandler Carruth351ba142013-01-02 12:09:16 +0000670#include "llvm/IR/Intrinsics.gen"
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000671#undef GET_INTRINSIC_ATTRIBUTES
672
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000673Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000674 // There can never be multiple globals with the same name of different types,
675 // because intrinsics must be a specific type.
Duncan Sands79ab3e82008-04-07 13:39:11 +0000676 return
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000677 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
678 getType(M->getContext(), id, Tys)));
Jim Laskey95af5922007-02-07 20:38:26 +0000679}
680
Dale Johannesen49de9822009-02-05 01:49:45 +0000681// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
682#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Chandler Carruth351ba142013-01-02 12:09:16 +0000683#include "llvm/IR/Intrinsics.gen"
Dale Johannesen49de9822009-02-05 01:49:45 +0000684#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
685
Gabor Greif0054c7a2010-03-23 14:40:20 +0000686/// hasAddressTaken - returns true if there are any uses of this function
687/// other than direct calls or invokes to it.
Gabor Greifc9f75002010-03-24 13:21:49 +0000688bool Function::hasAddressTaken(const User* *PutOffender) const {
Gabor Greif60ad7812010-03-25 23:06:16 +0000689 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
Gabor Greifc9f75002010-03-24 13:21:49 +0000690 const User *U = *I;
Jay Foadb7454fd2012-05-12 08:30:16 +0000691 if (isa<BlockAddress>(U))
692 continue;
Gabor Greifc9f75002010-03-24 13:21:49 +0000693 if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
694 return PutOffender ? (*PutOffender = U, true) : true;
Gabor Greifc8b82cc2010-04-01 08:21:08 +0000695 ImmutableCallSite CS(cast<Instruction>(U));
Gabor Greifc9f75002010-03-24 13:21:49 +0000696 if (!CS.isCallee(I))
697 return PutOffender ? (*PutOffender = U, true) : true;
Jay Foad757068f2009-06-10 08:41:11 +0000698 }
699 return false;
700}
701
Eli Friedmanc6633052011-10-20 05:23:42 +0000702bool Function::isDefTriviallyDead() const {
703 // Check the linkage
704 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
705 !hasAvailableExternallyLinkage())
706 return false;
707
708 // Check if the function is used by anything other than a blockaddress.
709 for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
710 if (!isa<BlockAddress>(*I))
711 return false;
712
713 return true;
714}
715
Bill Wendling3c5e6092011-10-17 18:43:40 +0000716/// callsFunctionThatReturnsTwice - Return true if the function has a call to
717/// setjmp or other function that gcc recognizes as "returning twice".
718bool Function::callsFunctionThatReturnsTwice() const {
719 for (const_inst_iterator
720 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
721 const CallInst* callInst = dyn_cast<CallInst>(&*I);
722 if (!callInst)
723 continue;
724 if (callInst->canReturnTwice())
725 return true;
726 }
727
728 return false;
729}
Peter Collingbourne1e3037f2013-09-16 01:08:15 +0000730
731Constant *Function::getPrefixData() const {
732 assert(hasPrefixData());
733 const LLVMContextImpl::PrefixDataMapTy &PDMap =
734 getContext().pImpl->PrefixDataMap;
735 assert(PDMap.find(this) != PDMap.end());
736 return cast<Constant>(PDMap.find(this)->second->getReturnValue());
737}
738
739void Function::setPrefixData(Constant *PrefixData) {
740 if (!PrefixData && !hasPrefixData())
741 return;
742
743 unsigned SCData = getSubclassDataFromValue();
744 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
745 ReturnInst *&PDHolder = PDMap[this];
746 if (PrefixData) {
747 if (PDHolder)
748 PDHolder->setOperand(0, PrefixData);
749 else
750 PDHolder = ReturnInst::Create(getContext(), PrefixData);
751 SCData |= 2;
752 } else {
753 delete PDHolder;
754 PDMap.erase(this);
755 SCData &= ~2;
756 }
757 setValueSubclassData(SCData);
758}