Remove the bitwise assignment OR operator from the Attributes class. Replace it with...
[oota-llvm.git] / lib / VMCore / 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 VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Module.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/CodeGen/ValueTypes.h"
19 #include "llvm/Support/CallSite.h"
20 #include "llvm/Support/InstIterator.h"
21 #include "llvm/Support/LeakDetector.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/StringPool.h"
24 #include "llvm/Support/RWMutex.h"
25 #include "llvm/Support/Threading.h"
26 #include "SymbolTableListTraitsImpl.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/StringExtras.h"
30 using namespace llvm;
31
32 // Explicit instantiations of SymbolTableListTraits since some of the methods
33 // are not in the public header file...
34 template class llvm::SymbolTableListTraits<Argument, Function>;
35 template class llvm::SymbolTableListTraits<BasicBlock, Function>;
36
37 //===----------------------------------------------------------------------===//
38 // Argument Implementation
39 //===----------------------------------------------------------------------===//
40
41 void Argument::anchor() { }
42
43 Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
44   : Value(Ty, Value::ArgumentVal) {
45   Parent = 0;
46
47   // Make sure that we get added to a function
48   LeakDetector::addGarbageObject(this);
49
50   if (Par)
51     Par->getArgumentList().push_back(this);
52   setName(Name);
53 }
54
55 void Argument::setParent(Function *parent) {
56   if (getParent())
57     LeakDetector::addGarbageObject(this);
58   Parent = parent;
59   if (getParent())
60     LeakDetector::removeGarbageObject(this);
61 }
62
63 /// 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. 
65 unsigned 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.
79 bool Argument::hasByValAttr() const {
80   if (!getType()->isPointerTy()) return false;
81   return getParent()->getParamAttributes(getArgNo()+1).
82     hasAttribute(Attributes::ByVal);
83 }
84
85 unsigned Argument::getParamAlignment() const {
86   assert(getType()->isPointerTy() && "Only pointers have alignments");
87   return getParent()->getParamAlignment(getArgNo()+1);
88   
89 }
90
91 /// hasNestAttr - Return true if this argument has the nest attribute on
92 /// it in its containing function.
93 bool Argument::hasNestAttr() const {
94   if (!getType()->isPointerTy()) return false;
95   return getParent()->getParamAttributes(getArgNo()+1).
96     hasAttribute(Attributes::Nest);
97 }
98
99 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
100 /// it in its containing function.
101 bool Argument::hasNoAliasAttr() const {
102   if (!getType()->isPointerTy()) return false;
103   return getParent()->getParamAttributes(getArgNo()+1).
104     hasAttribute(Attributes::NoAlias);
105 }
106
107 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
108 /// on it in its containing function.
109 bool Argument::hasNoCaptureAttr() const {
110   if (!getType()->isPointerTy()) return false;
111   return getParent()->getParamAttributes(getArgNo()+1).
112     hasAttribute(Attributes::NoCapture);
113 }
114
115 /// hasSRetAttr - Return true if this argument has the sret attribute on
116 /// it in its containing function.
117 bool Argument::hasStructRetAttr() const {
118   if (!getType()->isPointerTy()) return false;
119   if (this != getParent()->arg_begin())
120     return false; // StructRet param must be first param
121   return getParent()->getParamAttributes(1).
122     hasAttribute(Attributes::StructRet);
123 }
124
125 /// addAttr - Add a Attribute to an argument
126 void Argument::addAttr(Attributes attr) {
127   getParent()->addAttribute(getArgNo() + 1, attr);
128 }
129
130 /// removeAttr - Remove a Attribute from an argument
131 void Argument::removeAttr(Attributes attr) {
132   getParent()->removeAttribute(getArgNo() + 1, attr);
133 }
134
135
136 //===----------------------------------------------------------------------===//
137 // Helper Methods in Function
138 //===----------------------------------------------------------------------===//
139
140 LLVMContext &Function::getContext() const {
141   return getType()->getContext();
142 }
143
144 FunctionType *Function::getFunctionType() const {
145   return cast<FunctionType>(getType()->getElementType());
146 }
147
148 bool Function::isVarArg() const {
149   return getFunctionType()->isVarArg();
150 }
151
152 Type *Function::getReturnType() const {
153   return getFunctionType()->getReturnType();
154 }
155
156 void Function::removeFromParent() {
157   getParent()->getFunctionList().remove(this);
158 }
159
160 void Function::eraseFromParent() {
161   getParent()->getFunctionList().erase(this);
162 }
163
164 //===----------------------------------------------------------------------===//
165 // Function Implementation
166 //===----------------------------------------------------------------------===//
167
168 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
169                    const Twine &name, Module *ParentModule)
170   : GlobalValue(PointerType::getUnqual(Ty), 
171                 Value::FunctionVal, 0, 0, Linkage, name) {
172   assert(FunctionType::isValidReturnType(getReturnType()) &&
173          "invalid return type");
174   SymTab = new ValueSymbolTable();
175
176   // If the function has arguments, mark them as lazily built.
177   if (Ty->getNumParams())
178     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
179   
180   // Make sure that we get added to a function
181   LeakDetector::addGarbageObject(this);
182
183   if (ParentModule)
184     ParentModule->getFunctionList().push_back(this);
185
186   // Ensure intrinsics have the right parameter attributes.
187   if (unsigned IID = getIntrinsicID())
188     setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
189
190 }
191
192 Function::~Function() {
193   dropAllReferences();    // After this it is safe to delete instructions.
194
195   // Delete all of the method arguments and unlink from symbol table...
196   ArgumentList.clear();
197   delete SymTab;
198
199   // Remove the function from the on-the-side GC table.
200   clearGC();
201 }
202
203 void Function::BuildLazyArguments() const {
204   // Create the arguments vector, all arguments start out unnamed.
205   FunctionType *FT = getFunctionType();
206   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
207     assert(!FT->getParamType(i)->isVoidTy() &&
208            "Cannot have void typed arguments!");
209     ArgumentList.push_back(new Argument(FT->getParamType(i)));
210   }
211   
212   // Clear the lazy arguments bit.
213   unsigned SDC = getSubclassDataFromValue();
214   const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
215 }
216
217 size_t Function::arg_size() const {
218   return getFunctionType()->getNumParams();
219 }
220 bool Function::arg_empty() const {
221   return getFunctionType()->getNumParams() == 0;
222 }
223
224 void Function::setParent(Module *parent) {
225   if (getParent())
226     LeakDetector::addGarbageObject(this);
227   Parent = parent;
228   if (getParent())
229     LeakDetector::removeGarbageObject(this);
230 }
231
232 // 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
236 // zero.  Then everything is deleted for real.  Note that no operations are
237 // valid on an object that has "dropped all references", except operator
238 // delete.
239 //
240 void Function::dropAllReferences() {
241   for (iterator I = begin(), E = end(); I != E; ++I)
242     I->dropAllReferences();
243   
244   // 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();
248 }
249
250 void Function::addAttribute(unsigned i, Attributes attr) {
251   AttrListPtr PAL = getAttributes();
252   PAL = PAL.addAttr(getContext(), i, attr);
253   setAttributes(PAL);
254 }
255
256 void Function::removeAttribute(unsigned i, Attributes attr) {
257   AttrListPtr PAL = getAttributes();
258   PAL = PAL.removeAttr(getContext(), i, attr);
259   setAttributes(PAL);
260 }
261
262 // 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.
266 static DenseMap<const Function*,PooledStringPtr> *GCNames;
267 static StringPool *GCNamePool;
268 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
269
270 bool Function::hasGC() const {
271   sys::SmartScopedReader<true> Reader(*GCLock);
272   return GCNames && GCNames->count(this);
273 }
274
275 const char *Function::getGC() const {
276   assert(hasGC() && "Function has no collector");
277   sys::SmartScopedReader<true> Reader(*GCLock);
278   return *(*GCNames)[this];
279 }
280
281 void Function::setGC(const char *Str) {
282   sys::SmartScopedWriter<true> Writer(*GCLock);
283   if (!GCNamePool)
284     GCNamePool = new StringPool();
285   if (!GCNames)
286     GCNames = new DenseMap<const Function*,PooledStringPtr>();
287   (*GCNames)[this] = GCNamePool->intern(Str);
288 }
289
290 void Function::clearGC() {
291   sys::SmartScopedWriter<true> Writer(*GCLock);
292   if (GCNames) {
293     GCNames->erase(this);
294     if (GCNames->empty()) {
295       delete GCNames;
296       GCNames = 0;
297       if (GCNamePool->empty()) {
298         delete GCNamePool;
299         GCNamePool = 0;
300       }
301     }
302   }
303 }
304
305 /// copyAttributesFrom - copy all additional attributes (those not needed to
306 /// create a Function) from the Function Src to this one.
307 void 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());
312   setAttributes(SrcF->getAttributes());
313   if (SrcF->hasGC())
314     setGC(SrcF->getGC());
315   else
316     clearGC();
317 }
318
319 /// getIntrinsicID - This method returns the ID number of the specified
320 /// function, or Intrinsic::not_intrinsic if the function is not an
321 /// intrinsic, or if the pointer is null.  This value is always defined to be
322 /// 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 ///
326 unsigned Function::getIntrinsicID() const {
327   const ValueName *ValName = this->getValueName();
328   if (!ValName)
329     return 0;
330   unsigned Len = ValName->getKeyLength();
331   const char *Name = ValName->getKeyData();
332   
333   if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
334       || Name[2] != 'v' || Name[3] != 'm')
335     return 0;  // All intrinsics start with 'llvm.'
336
337 #define GET_FUNCTION_RECOGNIZER
338 #include "llvm/Intrinsics.gen"
339 #undef GET_FUNCTION_RECOGNIZER
340   return 0;
341 }
342
343 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
344   assert(id < num_intrinsics && "Invalid intrinsic ID!");
345   static const char * const Table[] = {
346     "not_intrinsic",
347 #define GET_INTRINSIC_NAME_TABLE
348 #include "llvm/Intrinsics.gen"
349 #undef GET_INTRINSIC_NAME_TABLE
350   };
351   if (Tys.empty())
352     return Table[id];
353   std::string Result(Table[id]);
354   for (unsigned i = 0; i < Tys.size(); ++i) {
355     if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
356       Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + 
357                 EVT::getEVT(PTyp->getElementType()).getEVTString();
358     }
359     else if (Tys[i])
360       Result += "." + EVT::getEVT(Tys[i]).getEVTString();
361   }
362   return Result;
363 }
364
365
366 /// 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!
370 enum 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
402 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
403                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
404   IIT_Info Info = IIT_Info(Infos[NextElt++]);
405   unsigned StructElts = 2;
406   using namespace Intrinsic;
407   
408   switch (Info) {
409   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;
439   case IIT_V2:
440     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
441     DecodeIITType(NextElt, Infos, OutputTable);
442     return;
443   case IIT_V4:
444     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
445     DecodeIITType(NextElt, Infos, OutputTable);
446     return;
447   case IIT_V8:
448     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
449     DecodeIITType(NextElt, Infos, OutputTable);
450     return;
451   case IIT_V16:
452     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
453     DecodeIITType(NextElt, Infos, OutputTable);
454     return;
455   case IIT_V32:
456     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
457     DecodeIITType(NextElt, Infos, OutputTable);
458     return;
459   case IIT_PTR:
460     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
461     DecodeIITType(NextElt, Infos, OutputTable);
462     return;
463   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
464     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 
465                                              Infos[NextElt++]));
466     DecodeIITType(NextElt, Infos, OutputTable);
467     return;
468   }
469   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   }
480   case IIT_TRUNC_VEC_ARG: {
481     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
482     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
483                                              ArgInfo));
484     return;
485   }
486   case IIT_EMPTYSTRUCT:
487     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
488     return;
489   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
490   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
491   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
492   case IIT_STRUCT2: {
493     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
494
495     for (unsigned i = 0; i != StructElts; ++i)
496       DecodeIITType(NextElt, Infos, OutputTable);
497     return;
498   }
499   }
500   llvm_unreachable("unhandled");
501 }
502
503
504 #define GET_INTRINSIC_GENERATOR_GLOBAL
505 #include "llvm/Intrinsics.gen"
506 #undef GET_INTRINSIC_GENERATOR_GLOBAL
507
508 void Intrinsic::getIntrinsicInfoTableEntries(ID id, 
509                                              SmallVectorImpl<IITDescriptor> &T){
510   // Check to see if the intrinsic's type was expressible by the table.
511   unsigned TableVal = IIT_Table[id-1];
512   
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 {
524     // 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.
526     do {
527       IITValues.push_back(TableVal & 0xF);
528       TableVal >>= 4;
529     } while (TableVal);
530     
531     IITEntries = IITValues;
532     NextElt = 0;
533   }
534
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
542 static 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
585 FunctionType *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);
592     
593   SmallVector<Type*, 8> ArgTys;
594   while (!TableRef.empty())
595     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
596
597   return FunctionType::get(ResultTy, ArgTys, false); 
598 }
599
600 bool Intrinsic::isOverloaded(ID id) {
601 #define GET_INTRINSIC_OVERLOAD_TABLE
602 #include "llvm/Intrinsics.gen"
603 #undef GET_INTRINSIC_OVERLOAD_TABLE
604 }
605
606 /// This defines the "Intrinsic::getAttributes(ID id)" method.
607 #define GET_INTRINSIC_ATTRIBUTES
608 #include "llvm/Intrinsics.gen"
609 #undef GET_INTRINSIC_ATTRIBUTES
610
611 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
612   // There can never be multiple globals with the same name of different types,
613   // because intrinsics must be a specific type.
614   return
615     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
616                                           getType(M->getContext(), id, Tys)));
617 }
618
619 // 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
624 /// hasAddressTaken - returns true if there are any uses of this function
625 /// other than direct calls or invokes to it.
626 bool Function::hasAddressTaken(const User* *PutOffender) const {
627   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
628     const User *U = *I;
629     if (isa<BlockAddress>(U))
630       continue;
631     if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
632       return PutOffender ? (*PutOffender = U, true) : true;
633     ImmutableCallSite CS(cast<Instruction>(U));
634     if (!CS.isCallee(I))
635       return PutOffender ? (*PutOffender = U, true) : true;
636   }
637   return false;
638 }
639
640 bool 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
654 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
655 /// setjmp or other function that gcc recognizes as "returning twice".
656 bool 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