Use the AttributeSet when adding multiple attributes and an Attribute::AttrKind
[oota-llvm.git] / lib / IR / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Function class for the IR library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/Function.h"
15 #include "SymbolTableListTraitsImpl.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #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"
31 using namespace llvm;
32
33 // Explicit instantiations of SymbolTableListTraits since some of the methods
34 // are not in the public header file...
35 template class llvm::SymbolTableListTraits<Argument, Function>;
36 template class llvm::SymbolTableListTraits<BasicBlock, Function>;
37
38 //===----------------------------------------------------------------------===//
39 // Argument Implementation
40 //===----------------------------------------------------------------------===//
41
42 void Argument::anchor() { }
43
44 Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
45   : Value(Ty, Value::ArgumentVal) {
46   Parent = 0;
47
48   // Make sure that we get added to a function
49   LeakDetector::addGarbageObject(this);
50
51   if (Par)
52     Par->getArgumentList().push_back(this);
53   setName(Name);
54 }
55
56 void Argument::setParent(Function *parent) {
57   if (getParent())
58     LeakDetector::addGarbageObject(this);
59   Parent = parent;
60   if (getParent())
61     LeakDetector::removeGarbageObject(this);
62 }
63
64 /// getArgNo - Return the index of this formal argument in its containing
65 /// function.  For example in "void foo(int a, float b)" a is 0 and b is 1.
66 unsigned Argument::getArgNo() const {
67   const Function *F = getParent();
68   assert(F && "Argument is not in a function");
69
70   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.
80 bool Argument::hasByValAttr() const {
81   if (!getType()->isPointerTy()) return false;
82   return getParent()->getAttributes().
83     hasAttribute(getArgNo()+1, Attribute::ByVal);
84 }
85
86 unsigned Argument::getParamAlignment() const {
87   assert(getType()->isPointerTy() && "Only pointers have alignments");
88   return getParent()->getParamAlignment(getArgNo()+1);
89
90 }
91
92 /// hasNestAttr - Return true if this argument has the nest attribute on
93 /// it in its containing function.
94 bool Argument::hasNestAttr() const {
95   if (!getType()->isPointerTy()) return false;
96   return getParent()->getAttributes().
97     hasAttribute(getArgNo()+1, Attribute::Nest);
98 }
99
100 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
101 /// it in its containing function.
102 bool Argument::hasNoAliasAttr() const {
103   if (!getType()->isPointerTy()) return false;
104   return getParent()->getAttributes().
105     hasAttribute(getArgNo()+1, Attribute::NoAlias);
106 }
107
108 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
109 /// on it in its containing function.
110 bool Argument::hasNoCaptureAttr() const {
111   if (!getType()->isPointerTy()) return false;
112   return getParent()->getAttributes().
113     hasAttribute(getArgNo()+1, Attribute::NoCapture);
114 }
115
116 /// hasSRetAttr - Return true if this argument has the sret attribute on
117 /// it in its containing function.
118 bool Argument::hasStructRetAttr() const {
119   if (!getType()->isPointerTy()) return false;
120   if (this != getParent()->arg_begin())
121     return false; // StructRet param must be first param
122   return getParent()->getAttributes().
123     hasAttribute(1, Attribute::StructRet);
124 }
125
126 /// addAttr - Add a Attribute to an argument
127 void Argument::addAttr(Attribute attr) {
128   AttrBuilder B(attr);
129   getParent()->addAttributes(getArgNo() + 1,
130                              AttributeSet::get(getParent()->getContext(),
131                                                getArgNo() + 1, B));
132 }
133
134 /// removeAttr - Remove a Attribute from an argument
135 void Argument::removeAttr(Attribute attr) {
136   getParent()->removeAttribute(getArgNo() + 1, attr);
137 }
138
139
140 //===----------------------------------------------------------------------===//
141 // Helper Methods in Function
142 //===----------------------------------------------------------------------===//
143
144 LLVMContext &Function::getContext() const {
145   return getType()->getContext();
146 }
147
148 FunctionType *Function::getFunctionType() const {
149   return cast<FunctionType>(getType()->getElementType());
150 }
151
152 bool Function::isVarArg() const {
153   return getFunctionType()->isVarArg();
154 }
155
156 Type *Function::getReturnType() const {
157   return getFunctionType()->getReturnType();
158 }
159
160 void Function::removeFromParent() {
161   getParent()->getFunctionList().remove(this);
162 }
163
164 void Function::eraseFromParent() {
165   getParent()->getFunctionList().erase(this);
166 }
167
168 //===----------------------------------------------------------------------===//
169 // Function Implementation
170 //===----------------------------------------------------------------------===//
171
172 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
173                    const Twine &name, Module *ParentModule)
174   : GlobalValue(PointerType::getUnqual(Ty),
175                 Value::FunctionVal, 0, 0, Linkage, name) {
176   assert(FunctionType::isValidReturnType(getReturnType()) &&
177          "invalid return type");
178   SymTab = new ValueSymbolTable();
179
180   // If the function has arguments, mark them as lazily built.
181   if (Ty->getNumParams())
182     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
183
184   // Make sure that we get added to a function
185   LeakDetector::addGarbageObject(this);
186
187   if (ParentModule)
188     ParentModule->getFunctionList().push_back(this);
189
190   // Ensure intrinsics have the right parameter attributes.
191   if (unsigned IID = getIntrinsicID())
192     setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
193
194 }
195
196 Function::~Function() {
197   dropAllReferences();    // After this it is safe to delete instructions.
198
199   // Delete all of the method arguments and unlink from symbol table...
200   ArgumentList.clear();
201   delete SymTab;
202
203   // Remove the function from the on-the-side GC table.
204   clearGC();
205 }
206
207 void Function::BuildLazyArguments() const {
208   // Create the arguments vector, all arguments start out unnamed.
209   FunctionType *FT = getFunctionType();
210   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
211     assert(!FT->getParamType(i)->isVoidTy() &&
212            "Cannot have void typed arguments!");
213     ArgumentList.push_back(new Argument(FT->getParamType(i)));
214   }
215
216   // Clear the lazy arguments bit.
217   unsigned SDC = getSubclassDataFromValue();
218   const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
219 }
220
221 size_t Function::arg_size() const {
222   return getFunctionType()->getNumParams();
223 }
224 bool Function::arg_empty() const {
225   return getFunctionType()->getNumParams() == 0;
226 }
227
228 void Function::setParent(Module *parent) {
229   if (getParent())
230     LeakDetector::addGarbageObject(this);
231   Parent = parent;
232   if (getParent())
233     LeakDetector::removeGarbageObject(this);
234 }
235
236 // 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
240 // zero.  Then everything is deleted for real.  Note that no operations are
241 // valid on an object that has "dropped all references", except operator
242 // delete.
243 //
244 void Function::dropAllReferences() {
245   for (iterator I = begin(), E = end(); I != E; ++I)
246     I->dropAllReferences();
247
248   // 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();
252 }
253
254 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
255   AttributeSet PAL = getAttributes();
256   PAL = PAL.addAttribute(getContext(), i, attr);
257   setAttributes(PAL);
258 }
259
260 void Function::addAttributes(unsigned i, AttributeSet attrs) {
261   AttributeSet PAL = getAttributes();
262   PAL = PAL.addAttributes(getContext(), i, attrs);
263   setAttributes(PAL);
264 }
265
266 void Function::removeAttribute(unsigned i, Attribute attrs) {
267   AttributeSet PAL = getAttributes();
268   PAL = PAL.removeAttr(getContext(), i, attrs);
269   setAttributes(PAL);
270 }
271
272 // 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.
276 static DenseMap<const Function*,PooledStringPtr> *GCNames;
277 static StringPool *GCNamePool;
278 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
279
280 bool Function::hasGC() const {
281   sys::SmartScopedReader<true> Reader(*GCLock);
282   return GCNames && GCNames->count(this);
283 }
284
285 const char *Function::getGC() const {
286   assert(hasGC() && "Function has no collector");
287   sys::SmartScopedReader<true> Reader(*GCLock);
288   return *(*GCNames)[this];
289 }
290
291 void Function::setGC(const char *Str) {
292   sys::SmartScopedWriter<true> Writer(*GCLock);
293   if (!GCNamePool)
294     GCNamePool = new StringPool();
295   if (!GCNames)
296     GCNames = new DenseMap<const Function*,PooledStringPtr>();
297   (*GCNames)[this] = GCNamePool->intern(Str);
298 }
299
300 void Function::clearGC() {
301   sys::SmartScopedWriter<true> Writer(*GCLock);
302   if (GCNames) {
303     GCNames->erase(this);
304     if (GCNames->empty()) {
305       delete GCNames;
306       GCNames = 0;
307       if (GCNamePool->empty()) {
308         delete GCNamePool;
309         GCNamePool = 0;
310       }
311     }
312   }
313 }
314
315 /// copyAttributesFrom - copy all additional attributes (those not needed to
316 /// create a Function) from the Function Src to this one.
317 void 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());
322   setAttributes(SrcF->getAttributes());
323   if (SrcF->hasGC())
324     setGC(SrcF->getGC());
325   else
326     clearGC();
327 }
328
329 /// getIntrinsicID - This method returns the ID number of the specified
330 /// function, or Intrinsic::not_intrinsic if the function is not an
331 /// intrinsic, or if the pointer is null.  This value is always defined to be
332 /// 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 ///
336 unsigned Function::getIntrinsicID() const {
337   const ValueName *ValName = this->getValueName();
338   if (!ValName || !isIntrinsic())
339     return 0;
340   unsigned Len = ValName->getKeyLength();
341   const char *Name = ValName->getKeyData();
342
343 #define GET_FUNCTION_RECOGNIZER
344 #include "llvm/IR/Intrinsics.gen"
345 #undef GET_FUNCTION_RECOGNIZER
346   return 0;
347 }
348
349 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
350   assert(id < num_intrinsics && "Invalid intrinsic ID!");
351   static const char * const Table[] = {
352     "not_intrinsic",
353 #define GET_INTRINSIC_NAME_TABLE
354 #include "llvm/IR/Intrinsics.gen"
355 #undef GET_INTRINSIC_NAME_TABLE
356   };
357   if (Tys.empty())
358     return Table[id];
359   std::string Result(Table[id]);
360   for (unsigned i = 0; i < Tys.size(); ++i) {
361     if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
362       Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
363                 EVT::getEVT(PTyp->getElementType()).getEVTString();
364     }
365     else if (Tys[i])
366       Result += "." + EVT::getEVT(Tys[i]).getEVTString();
367   }
368   return Result;
369 }
370
371
372 /// 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!
376 enum 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,
384   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,
392   IIT_PTR  = 14,
393   IIT_ARG  = 15,
394
395   // Values from 16+ are only encodable with the inefficient encoding.
396   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
406 };
407
408
409 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
410                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
411   IIT_Info Info = IIT_Info(Infos[NextElt++]);
412   unsigned StructElts = 2;
413   using namespace Intrinsic;
414
415   switch (Info) {
416   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;
425   case IIT_F16:
426     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
427     return;
428   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;
449   case IIT_V2:
450     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
451     DecodeIITType(NextElt, Infos, OutputTable);
452     return;
453   case IIT_V4:
454     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
455     DecodeIITType(NextElt, Infos, OutputTable);
456     return;
457   case IIT_V8:
458     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
459     DecodeIITType(NextElt, Infos, OutputTable);
460     return;
461   case IIT_V16:
462     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
463     DecodeIITType(NextElt, Infos, OutputTable);
464     return;
465   case IIT_V32:
466     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
467     DecodeIITType(NextElt, Infos, OutputTable);
468     return;
469   case IIT_PTR:
470     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
471     DecodeIITType(NextElt, Infos, OutputTable);
472     return;
473   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
474     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
475                                              Infos[NextElt++]));
476     DecodeIITType(NextElt, Infos, OutputTable);
477     return;
478   }
479   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   }
490   case IIT_TRUNC_VEC_ARG: {
491     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
492     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
493                                              ArgInfo));
494     return;
495   }
496   case IIT_EMPTYSTRUCT:
497     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
498     return;
499   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
500   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
501   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
502   case IIT_STRUCT2: {
503     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
504
505     for (unsigned i = 0; i != StructElts; ++i)
506       DecodeIITType(NextElt, Infos, OutputTable);
507     return;
508   }
509   }
510   llvm_unreachable("unhandled");
511 }
512
513
514 #define GET_INTRINSIC_GENERATOR_GLOBAL
515 #include "llvm/IR/Intrinsics.gen"
516 #undef GET_INTRINSIC_GENERATOR_GLOBAL
517
518 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
519                                              SmallVectorImpl<IITDescriptor> &T){
520   // Check to see if the intrinsic's type was expressible by the table.
521   unsigned TableVal = IIT_Table[id-1];
522
523   // 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;
530
531     // Strip sentinel bit.
532     NextElt = (TableVal << 1) >> 1;
533   } else {
534     // 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.
536     do {
537       IITValues.push_back(TableVal & 0xF);
538       TableVal >>= 4;
539     } while (TableVal);
540
541     IITEntries = IITValues;
542     NextElt = 0;
543   }
544
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
552 static 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);
557
558   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);
562   case IITDescriptor::Half: return Type::getHalfTy(Context);
563   case IITDescriptor::Float: return Type::getFloatTy(Context);
564   case IITDescriptor::Double: return Type::getDoubleTy(Context);
565
566   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()]));
586
587   case IITDescriptor::TruncVecArgument:
588     return VectorType::getTruncatedElementVectorType(cast<VectorType>(
589                                                   Tys[D.getArgumentNumber()]));
590   }
591   llvm_unreachable("unhandled");
592 }
593
594
595
596 FunctionType *Intrinsic::getType(LLVMContext &Context,
597                                  ID id, ArrayRef<Type*> Tys) {
598   SmallVector<IITDescriptor, 8> Table;
599   getIntrinsicInfoTableEntries(id, Table);
600
601   ArrayRef<IITDescriptor> TableRef = Table;
602   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
603
604   SmallVector<Type*, 8> ArgTys;
605   while (!TableRef.empty())
606     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
607
608   return FunctionType::get(ResultTy, ArgTys, false);
609 }
610
611 bool Intrinsic::isOverloaded(ID id) {
612 #define GET_INTRINSIC_OVERLOAD_TABLE
613 #include "llvm/IR/Intrinsics.gen"
614 #undef GET_INTRINSIC_OVERLOAD_TABLE
615 }
616
617 /// This defines the "Intrinsic::getAttributes(ID id)" method.
618 #define GET_INTRINSIC_ATTRIBUTES
619 #include "llvm/IR/Intrinsics.gen"
620 #undef GET_INTRINSIC_ATTRIBUTES
621
622 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
623   // There can never be multiple globals with the same name of different types,
624   // because intrinsics must be a specific type.
625   return
626     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
627                                           getType(M->getContext(), id, Tys)));
628 }
629
630 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
631 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
632 #include "llvm/IR/Intrinsics.gen"
633 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
634
635 /// hasAddressTaken - returns true if there are any uses of this function
636 /// other than direct calls or invokes to it.
637 bool Function::hasAddressTaken(const User* *PutOffender) const {
638   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
639     const User *U = *I;
640     if (isa<BlockAddress>(U))
641       continue;
642     if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
643       return PutOffender ? (*PutOffender = U, true) : true;
644     ImmutableCallSite CS(cast<Instruction>(U));
645     if (!CS.isCallee(I))
646       return PutOffender ? (*PutOffender = U, true) : true;
647   }
648   return false;
649 }
650
651 bool 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
665 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
666 /// setjmp or other function that gcc recognizes as "returning twice".
667 bool 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