Removed trailing whitespace
[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/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/DerivedTypes.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/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()->getParamAttributes(getArgNo()+1).
83     hasAttribute(Attributes::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()->getParamAttributes(getArgNo()+1).
97     hasAttribute(Attributes::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()->getParamAttributes(getArgNo()+1).
105     hasAttribute(Attributes::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()->getParamAttributes(getArgNo()+1).
113     hasAttribute(Attributes::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()->getParamAttributes(1).
123     hasAttribute(Attributes::StructRet);
124 }
125
126 /// addAttr - Add a Attribute to an argument
127 void Argument::addAttr(Attributes attr) {
128   getParent()->addAttribute(getArgNo() + 1, attr);
129 }
130
131 /// removeAttr - Remove a Attribute from an argument
132 void Argument::removeAttr(Attributes attr) {
133   getParent()->removeAttribute(getArgNo() + 1, attr);
134 }
135
136
137 //===----------------------------------------------------------------------===//
138 // Helper Methods in Function
139 //===----------------------------------------------------------------------===//
140
141 LLVMContext &Function::getContext() const {
142   return getType()->getContext();
143 }
144
145 FunctionType *Function::getFunctionType() const {
146   return cast<FunctionType>(getType()->getElementType());
147 }
148
149 bool Function::isVarArg() const {
150   return getFunctionType()->isVarArg();
151 }
152
153 Type *Function::getReturnType() const {
154   return getFunctionType()->getReturnType();
155 }
156
157 void Function::removeFromParent() {
158   getParent()->getFunctionList().remove(this);
159 }
160
161 void Function::eraseFromParent() {
162   getParent()->getFunctionList().erase(this);
163 }
164
165 //===----------------------------------------------------------------------===//
166 // Function Implementation
167 //===----------------------------------------------------------------------===//
168
169 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
170                    const Twine &name, Module *ParentModule)
171   : GlobalValue(PointerType::getUnqual(Ty),
172                 Value::FunctionVal, 0, 0, Linkage, name) {
173   assert(FunctionType::isValidReturnType(getReturnType()) &&
174          "invalid return type");
175   SymTab = new ValueSymbolTable();
176
177   // If the function has arguments, mark them as lazily built.
178   if (Ty->getNumParams())
179     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
180
181   // Make sure that we get added to a function
182   LeakDetector::addGarbageObject(this);
183
184   if (ParentModule)
185     ParentModule->getFunctionList().push_back(this);
186
187   // Ensure intrinsics have the right parameter attributes.
188   if (unsigned IID = getIntrinsicID())
189     setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
190
191 }
192
193 Function::~Function() {
194   dropAllReferences();    // After this it is safe to delete instructions.
195
196   // Delete all of the method arguments and unlink from symbol table...
197   ArgumentList.clear();
198   delete SymTab;
199
200   // Remove the function from the on-the-side GC table.
201   clearGC();
202 }
203
204 void Function::BuildLazyArguments() const {
205   // Create the arguments vector, all arguments start out unnamed.
206   FunctionType *FT = getFunctionType();
207   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
208     assert(!FT->getParamType(i)->isVoidTy() &&
209            "Cannot have void typed arguments!");
210     ArgumentList.push_back(new Argument(FT->getParamType(i)));
211   }
212
213   // Clear the lazy arguments bit.
214   unsigned SDC = getSubclassDataFromValue();
215   const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
216 }
217
218 size_t Function::arg_size() const {
219   return getFunctionType()->getNumParams();
220 }
221 bool Function::arg_empty() const {
222   return getFunctionType()->getNumParams() == 0;
223 }
224
225 void Function::setParent(Module *parent) {
226   if (getParent())
227     LeakDetector::addGarbageObject(this);
228   Parent = parent;
229   if (getParent())
230     LeakDetector::removeGarbageObject(this);
231 }
232
233 // dropAllReferences() - This function causes all the subinstructions to "let
234 // go" of all references that they are maintaining.  This allows one to
235 // 'delete' a whole class at a time, even though there may be circular
236 // references... first all references are dropped, and all use counts go to
237 // zero.  Then everything is deleted for real.  Note that no operations are
238 // valid on an object that has "dropped all references", except operator
239 // delete.
240 //
241 void Function::dropAllReferences() {
242   for (iterator I = begin(), E = end(); I != E; ++I)
243     I->dropAllReferences();
244
245   // Delete all basic blocks. They are now unused, except possibly by
246   // blockaddresses, but BasicBlock's destructor takes care of those.
247   while (!BasicBlocks.empty())
248     BasicBlocks.begin()->eraseFromParent();
249 }
250
251 void Function::addAttribute(unsigned i, Attributes attr) {
252   AttributeSet PAL = getAttributes();
253   PAL = PAL.addAttr(getContext(), i, attr);
254   setAttributes(PAL);
255 }
256
257 void Function::removeAttribute(unsigned i, Attributes attr) {
258   AttributeSet PAL = getAttributes();
259   PAL = PAL.removeAttr(getContext(), i, attr);
260   setAttributes(PAL);
261 }
262
263 // Maintain the GC name for each function in an on-the-side table. This saves
264 // allocating an additional word in Function for programs which do not use GC
265 // (i.e., most programs) at the cost of increased overhead for clients which do
266 // use GC.
267 static DenseMap<const Function*,PooledStringPtr> *GCNames;
268 static StringPool *GCNamePool;
269 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
270
271 bool Function::hasGC() const {
272   sys::SmartScopedReader<true> Reader(*GCLock);
273   return GCNames && GCNames->count(this);
274 }
275
276 const char *Function::getGC() const {
277   assert(hasGC() && "Function has no collector");
278   sys::SmartScopedReader<true> Reader(*GCLock);
279   return *(*GCNames)[this];
280 }
281
282 void Function::setGC(const char *Str) {
283   sys::SmartScopedWriter<true> Writer(*GCLock);
284   if (!GCNamePool)
285     GCNamePool = new StringPool();
286   if (!GCNames)
287     GCNames = new DenseMap<const Function*,PooledStringPtr>();
288   (*GCNames)[this] = GCNamePool->intern(Str);
289 }
290
291 void Function::clearGC() {
292   sys::SmartScopedWriter<true> Writer(*GCLock);
293   if (GCNames) {
294     GCNames->erase(this);
295     if (GCNames->empty()) {
296       delete GCNames;
297       GCNames = 0;
298       if (GCNamePool->empty()) {
299         delete GCNamePool;
300         GCNamePool = 0;
301       }
302     }
303   }
304 }
305
306 /// copyAttributesFrom - copy all additional attributes (those not needed to
307 /// create a Function) from the Function Src to this one.
308 void Function::copyAttributesFrom(const GlobalValue *Src) {
309   assert(isa<Function>(Src) && "Expected a Function!");
310   GlobalValue::copyAttributesFrom(Src);
311   const Function *SrcF = cast<Function>(Src);
312   setCallingConv(SrcF->getCallingConv());
313   setAttributes(SrcF->getAttributes());
314   if (SrcF->hasGC())
315     setGC(SrcF->getGC());
316   else
317     clearGC();
318 }
319
320 /// getIntrinsicID - This method returns the ID number of the specified
321 /// function, or Intrinsic::not_intrinsic if the function is not an
322 /// intrinsic, or if the pointer is null.  This value is always defined to be
323 /// zero to allow easy checking for whether a function is intrinsic or not.  The
324 /// particular intrinsic functions which correspond to this value are defined in
325 /// llvm/Intrinsics.h.
326 ///
327 unsigned Function::getIntrinsicID() const {
328   const ValueName *ValName = this->getValueName();
329   if (!ValName)
330     return 0;
331   unsigned Len = ValName->getKeyLength();
332   const char *Name = ValName->getKeyData();
333
334   if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
335       || Name[2] != 'v' || Name[3] != 'm')
336     return 0;  // All intrinsics start with 'llvm.'
337
338 #define GET_FUNCTION_RECOGNIZER
339 #include "llvm/Intrinsics.gen"
340 #undef GET_FUNCTION_RECOGNIZER
341   return 0;
342 }
343
344 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
345   assert(id < num_intrinsics && "Invalid intrinsic ID!");
346   static const char * const Table[] = {
347     "not_intrinsic",
348 #define GET_INTRINSIC_NAME_TABLE
349 #include "llvm/Intrinsics.gen"
350 #undef GET_INTRINSIC_NAME_TABLE
351   };
352   if (Tys.empty())
353     return Table[id];
354   std::string Result(Table[id]);
355   for (unsigned i = 0; i < Tys.size(); ++i) {
356     if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
357       Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
358                 EVT::getEVT(PTyp->getElementType()).getEVTString();
359     }
360     else if (Tys[i])
361       Result += "." + EVT::getEVT(Tys[i]).getEVTString();
362   }
363   return Result;
364 }
365
366
367 /// IIT_Info - These are enumerators that describe the entries returned by the
368 /// getIntrinsicInfoTableEntries function.
369 ///
370 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
371 enum IIT_Info {
372   // Common values should be encoded with 0-15.
373   IIT_Done = 0,
374   IIT_I1   = 1,
375   IIT_I8   = 2,
376   IIT_I16  = 3,
377   IIT_I32  = 4,
378   IIT_I64  = 5,
379   IIT_F32  = 6,
380   IIT_F64  = 7,
381   IIT_V2   = 8,
382   IIT_V4   = 9,
383   IIT_V8   = 10,
384   IIT_V16  = 11,
385   IIT_V32  = 12,
386   IIT_MMX  = 13,
387   IIT_PTR  = 14,
388   IIT_ARG  = 15,
389
390   // Values from 16+ are only encodable with the inefficient encoding.
391   IIT_METADATA = 16,
392   IIT_EMPTYSTRUCT = 17,
393   IIT_STRUCT2 = 18,
394   IIT_STRUCT3 = 19,
395   IIT_STRUCT4 = 20,
396   IIT_STRUCT5 = 21,
397   IIT_EXTEND_VEC_ARG = 22,
398   IIT_TRUNC_VEC_ARG = 23,
399   IIT_ANYPTR = 24
400 };
401
402
403 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
404                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
405   IIT_Info Info = IIT_Info(Infos[NextElt++]);
406   unsigned StructElts = 2;
407   using namespace Intrinsic;
408
409   switch (Info) {
410   case IIT_Done:
411     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
412     return;
413   case IIT_MMX:
414     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
415     return;
416   case IIT_METADATA:
417     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
418     return;
419   case IIT_F32:
420     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
421     return;
422   case IIT_F64:
423     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
424     return;
425   case IIT_I1:
426     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
427     return;
428   case IIT_I8:
429     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
430     return;
431   case IIT_I16:
432     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
433     return;
434   case IIT_I32:
435     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
436     return;
437   case IIT_I64:
438     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
439     return;
440   case IIT_V2:
441     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
442     DecodeIITType(NextElt, Infos, OutputTable);
443     return;
444   case IIT_V4:
445     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
446     DecodeIITType(NextElt, Infos, OutputTable);
447     return;
448   case IIT_V8:
449     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
450     DecodeIITType(NextElt, Infos, OutputTable);
451     return;
452   case IIT_V16:
453     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
454     DecodeIITType(NextElt, Infos, OutputTable);
455     return;
456   case IIT_V32:
457     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
458     DecodeIITType(NextElt, Infos, OutputTable);
459     return;
460   case IIT_PTR:
461     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
462     DecodeIITType(NextElt, Infos, OutputTable);
463     return;
464   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
465     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
466                                              Infos[NextElt++]));
467     DecodeIITType(NextElt, Infos, OutputTable);
468     return;
469   }
470   case IIT_ARG: {
471     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
472     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
473     return;
474   }
475   case IIT_EXTEND_VEC_ARG: {
476     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
477     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
478                                              ArgInfo));
479     return;
480   }
481   case IIT_TRUNC_VEC_ARG: {
482     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
483     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
484                                              ArgInfo));
485     return;
486   }
487   case IIT_EMPTYSTRUCT:
488     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
489     return;
490   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
491   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
492   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
493   case IIT_STRUCT2: {
494     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
495
496     for (unsigned i = 0; i != StructElts; ++i)
497       DecodeIITType(NextElt, Infos, OutputTable);
498     return;
499   }
500   }
501   llvm_unreachable("unhandled");
502 }
503
504
505 #define GET_INTRINSIC_GENERATOR_GLOBAL
506 #include "llvm/Intrinsics.gen"
507 #undef GET_INTRINSIC_GENERATOR_GLOBAL
508
509 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
510                                              SmallVectorImpl<IITDescriptor> &T){
511   // Check to see if the intrinsic's type was expressible by the table.
512   unsigned TableVal = IIT_Table[id-1];
513
514   // Decode the TableVal into an array of IITValues.
515   SmallVector<unsigned char, 8> IITValues;
516   ArrayRef<unsigned char> IITEntries;
517   unsigned NextElt = 0;
518   if ((TableVal >> 31) != 0) {
519     // This is an offset into the IIT_LongEncodingTable.
520     IITEntries = IIT_LongEncodingTable;
521
522     // Strip sentinel bit.
523     NextElt = (TableVal << 1) >> 1;
524   } else {
525     // Decode the TableVal into an array of IITValues.  If the entry was encoded
526     // into a single word in the table itself, decode it now.
527     do {
528       IITValues.push_back(TableVal & 0xF);
529       TableVal >>= 4;
530     } while (TableVal);
531
532     IITEntries = IITValues;
533     NextElt = 0;
534   }
535
536   // Okay, decode the table into the output vector of IITDescriptors.
537   DecodeIITType(NextElt, IITEntries, T);
538   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
539     DecodeIITType(NextElt, IITEntries, T);
540 }
541
542
543 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
544                              ArrayRef<Type*> Tys, LLVMContext &Context) {
545   using namespace Intrinsic;
546   IITDescriptor D = Infos.front();
547   Infos = Infos.slice(1);
548
549   switch (D.Kind) {
550   case IITDescriptor::Void: return Type::getVoidTy(Context);
551   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
552   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
553   case IITDescriptor::Float: return Type::getFloatTy(Context);
554   case IITDescriptor::Double: return Type::getDoubleTy(Context);
555
556   case IITDescriptor::Integer:
557     return IntegerType::get(Context, D.Integer_Width);
558   case IITDescriptor::Vector:
559     return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
560   case IITDescriptor::Pointer:
561     return PointerType::get(DecodeFixedType(Infos, Tys, Context),
562                             D.Pointer_AddressSpace);
563   case IITDescriptor::Struct: {
564     Type *Elts[5];
565     assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
566     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
567       Elts[i] = DecodeFixedType(Infos, Tys, Context);
568     return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
569   }
570
571   case IITDescriptor::Argument:
572     return Tys[D.getArgumentNumber()];
573   case IITDescriptor::ExtendVecArgument:
574     return VectorType::getExtendedElementVectorType(cast<VectorType>(
575                                                   Tys[D.getArgumentNumber()]));
576
577   case IITDescriptor::TruncVecArgument:
578     return VectorType::getTruncatedElementVectorType(cast<VectorType>(
579                                                   Tys[D.getArgumentNumber()]));
580   }
581   llvm_unreachable("unhandled");
582 }
583
584
585
586 FunctionType *Intrinsic::getType(LLVMContext &Context,
587                                  ID id, ArrayRef<Type*> Tys) {
588   SmallVector<IITDescriptor, 8> Table;
589   getIntrinsicInfoTableEntries(id, Table);
590
591   ArrayRef<IITDescriptor> TableRef = Table;
592   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
593
594   SmallVector<Type*, 8> ArgTys;
595   while (!TableRef.empty())
596     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
597
598   return FunctionType::get(ResultTy, ArgTys, false);
599 }
600
601 bool Intrinsic::isOverloaded(ID id) {
602 #define GET_INTRINSIC_OVERLOAD_TABLE
603 #include "llvm/Intrinsics.gen"
604 #undef GET_INTRINSIC_OVERLOAD_TABLE
605 }
606
607 /// This defines the "Intrinsic::getAttributes(ID id)" method.
608 #define GET_INTRINSIC_ATTRIBUTES
609 #include "llvm/Intrinsics.gen"
610 #undef GET_INTRINSIC_ATTRIBUTES
611
612 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
613   // There can never be multiple globals with the same name of different types,
614   // because intrinsics must be a specific type.
615   return
616     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
617                                           getType(M->getContext(), id, Tys)));
618 }
619
620 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
621 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
622 #include "llvm/Intrinsics.gen"
623 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
624
625 /// hasAddressTaken - returns true if there are any uses of this function
626 /// other than direct calls or invokes to it.
627 bool Function::hasAddressTaken(const User* *PutOffender) const {
628   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
629     const User *U = *I;
630     if (isa<BlockAddress>(U))
631       continue;
632     if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
633       return PutOffender ? (*PutOffender = U, true) : true;
634     ImmutableCallSite CS(cast<Instruction>(U));
635     if (!CS.isCallee(I))
636       return PutOffender ? (*PutOffender = U, true) : true;
637   }
638   return false;
639 }
640
641 bool Function::isDefTriviallyDead() const {
642   // Check the linkage
643   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
644       !hasAvailableExternallyLinkage())
645     return false;
646
647   // Check if the function is used by anything other than a blockaddress.
648   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
649     if (!isa<BlockAddress>(*I))
650       return false;
651
652   return true;
653 }
654
655 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
656 /// setjmp or other function that gcc recognizes as "returning twice".
657 bool Function::callsFunctionThatReturnsTwice() const {
658   for (const_inst_iterator
659          I = inst_begin(this), E = inst_end(this); I != E; ++I) {
660     const CallInst* callInst = dyn_cast<CallInst>(&*I);
661     if (!callInst)
662       continue;
663     if (callInst->canReturnTwice())
664       return true;
665   }
666
667   return false;
668 }
669