LLVM API Documentation

Function.cpp
Go to the documentation of this file.
00001 //===-- Function.cpp - Implement the Global object classes ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Function class for the IR library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/Function.h"
00015 #include "LLVMContextImpl.h"
00016 #include "SymbolTableListTraitsImpl.h"
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/STLExtras.h"
00019 #include "llvm/ADT/StringExtras.h"
00020 #include "llvm/CodeGen/ValueTypes.h"
00021 #include "llvm/IR/DerivedTypes.h"
00022 #include "llvm/IR/IntrinsicInst.h"
00023 #include "llvm/IR/LLVMContext.h"
00024 #include "llvm/IR/Module.h"
00025 #include "llvm/Support/CallSite.h"
00026 #include "llvm/Support/InstIterator.h"
00027 #include "llvm/Support/LeakDetector.h"
00028 #include "llvm/Support/ManagedStatic.h"
00029 #include "llvm/Support/RWMutex.h"
00030 #include "llvm/Support/StringPool.h"
00031 #include "llvm/Support/Threading.h"
00032 using namespace llvm;
00033 
00034 // Explicit instantiations of SymbolTableListTraits since some of the methods
00035 // are not in the public header file...
00036 template class llvm::SymbolTableListTraits<Argument, Function>;
00037 template class llvm::SymbolTableListTraits<BasicBlock, Function>;
00038 
00039 //===----------------------------------------------------------------------===//
00040 // Argument Implementation
00041 //===----------------------------------------------------------------------===//
00042 
00043 void Argument::anchor() { }
00044 
00045 Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
00046   : Value(Ty, Value::ArgumentVal) {
00047   Parent = 0;
00048 
00049   // Make sure that we get added to a function
00050   LeakDetector::addGarbageObject(this);
00051 
00052   if (Par)
00053     Par->getArgumentList().push_back(this);
00054   setName(Name);
00055 }
00056 
00057 void Argument::setParent(Function *parent) {
00058   if (getParent())
00059     LeakDetector::addGarbageObject(this);
00060   Parent = parent;
00061   if (getParent())
00062     LeakDetector::removeGarbageObject(this);
00063 }
00064 
00065 /// getArgNo - Return the index of this formal argument in its containing
00066 /// function.  For example in "void foo(int a, float b)" a is 0 and b is 1.
00067 unsigned Argument::getArgNo() const {
00068   const Function *F = getParent();
00069   assert(F && "Argument is not in a function");
00070 
00071   Function::const_arg_iterator AI = F->arg_begin();
00072   unsigned ArgIdx = 0;
00073   for (; &*AI != this; ++AI)
00074     ++ArgIdx;
00075 
00076   return ArgIdx;
00077 }
00078 
00079 /// hasByValAttr - Return true if this argument has the byval attribute on it
00080 /// in its containing function.
00081 bool Argument::hasByValAttr() const {
00082   if (!getType()->isPointerTy()) return false;
00083   return getParent()->getAttributes().
00084     hasAttribute(getArgNo()+1, Attribute::ByVal);
00085 }
00086 
00087 unsigned Argument::getParamAlignment() const {
00088   assert(getType()->isPointerTy() && "Only pointers have alignments");
00089   return getParent()->getParamAlignment(getArgNo()+1);
00090 
00091 }
00092 
00093 /// hasNestAttr - Return true if this argument has the nest attribute on
00094 /// it in its containing function.
00095 bool Argument::hasNestAttr() const {
00096   if (!getType()->isPointerTy()) return false;
00097   return getParent()->getAttributes().
00098     hasAttribute(getArgNo()+1, Attribute::Nest);
00099 }
00100 
00101 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
00102 /// it in its containing function.
00103 bool Argument::hasNoAliasAttr() const {
00104   if (!getType()->isPointerTy()) return false;
00105   return getParent()->getAttributes().
00106     hasAttribute(getArgNo()+1, Attribute::NoAlias);
00107 }
00108 
00109 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
00110 /// on it in its containing function.
00111 bool Argument::hasNoCaptureAttr() const {
00112   if (!getType()->isPointerTy()) return false;
00113   return getParent()->getAttributes().
00114     hasAttribute(getArgNo()+1, Attribute::NoCapture);
00115 }
00116 
00117 /// hasSRetAttr - Return true if this argument has the sret attribute on
00118 /// it in its containing function.
00119 bool Argument::hasStructRetAttr() const {
00120   if (!getType()->isPointerTy()) return false;
00121   if (this != getParent()->arg_begin())
00122     return false; // StructRet param must be first param
00123   return getParent()->getAttributes().
00124     hasAttribute(1, Attribute::StructRet);
00125 }
00126 
00127 /// hasReturnedAttr - Return true if this argument has the returned attribute on
00128 /// it in its containing function.
00129 bool Argument::hasReturnedAttr() const {
00130   return getParent()->getAttributes().
00131     hasAttribute(getArgNo()+1, Attribute::Returned);
00132 }
00133 
00134 /// addAttr - Add attributes to an argument.
00135 void Argument::addAttr(AttributeSet AS) {
00136   assert(AS.getNumSlots() <= 1 &&
00137          "Trying to add more than one attribute set to an argument!");
00138   AttrBuilder B(AS, AS.getSlotIndex(0));
00139   getParent()->addAttributes(getArgNo() + 1,
00140                              AttributeSet::get(Parent->getContext(),
00141                                                getArgNo() + 1, B));
00142 }
00143 
00144 /// removeAttr - Remove attributes from an argument.
00145 void Argument::removeAttr(AttributeSet AS) {
00146   assert(AS.getNumSlots() <= 1 &&
00147          "Trying to remove more than one attribute set from an argument!");
00148   AttrBuilder B(AS, AS.getSlotIndex(0));
00149   getParent()->removeAttributes(getArgNo() + 1,
00150                                 AttributeSet::get(Parent->getContext(),
00151                                                   getArgNo() + 1, B));
00152 }
00153 
00154 //===----------------------------------------------------------------------===//
00155 // Helper Methods in Function
00156 //===----------------------------------------------------------------------===//
00157 
00158 LLVMContext &Function::getContext() const {
00159   return getType()->getContext();
00160 }
00161 
00162 FunctionType *Function::getFunctionType() const {
00163   return cast<FunctionType>(getType()->getElementType());
00164 }
00165 
00166 bool Function::isVarArg() const {
00167   return getFunctionType()->isVarArg();
00168 }
00169 
00170 Type *Function::getReturnType() const {
00171   return getFunctionType()->getReturnType();
00172 }
00173 
00174 void Function::removeFromParent() {
00175   getParent()->getFunctionList().remove(this);
00176 }
00177 
00178 void Function::eraseFromParent() {
00179   getParent()->getFunctionList().erase(this);
00180 }
00181 
00182 //===----------------------------------------------------------------------===//
00183 // Function Implementation
00184 //===----------------------------------------------------------------------===//
00185 
00186 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
00187                    const Twine &name, Module *ParentModule)
00188   : GlobalValue(PointerType::getUnqual(Ty),
00189                 Value::FunctionVal, 0, 0, Linkage, name) {
00190   assert(FunctionType::isValidReturnType(getReturnType()) &&
00191          "invalid return type");
00192   SymTab = new ValueSymbolTable();
00193 
00194   // If the function has arguments, mark them as lazily built.
00195   if (Ty->getNumParams())
00196     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
00197 
00198   // Make sure that we get added to a function
00199   LeakDetector::addGarbageObject(this);
00200 
00201   if (ParentModule)
00202     ParentModule->getFunctionList().push_back(this);
00203 
00204   // Ensure intrinsics have the right parameter attributes.
00205   if (unsigned IID = getIntrinsicID())
00206     setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
00207 
00208 }
00209 
00210 Function::~Function() {
00211   dropAllReferences();    // After this it is safe to delete instructions.
00212 
00213   // Delete all of the method arguments and unlink from symbol table...
00214   ArgumentList.clear();
00215   delete SymTab;
00216 
00217   // Remove the function from the on-the-side GC table.
00218   clearGC();
00219 
00220   // Remove the intrinsicID from the Cache.
00221   if (getValueName() && isIntrinsic())
00222     getContext().pImpl->IntrinsicIDCache.erase(this);
00223 }
00224 
00225 void Function::BuildLazyArguments() const {
00226   // Create the arguments vector, all arguments start out unnamed.
00227   FunctionType *FT = getFunctionType();
00228   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
00229     assert(!FT->getParamType(i)->isVoidTy() &&
00230            "Cannot have void typed arguments!");
00231     ArgumentList.push_back(new Argument(FT->getParamType(i)));
00232   }
00233 
00234   // Clear the lazy arguments bit.
00235   unsigned SDC = getSubclassDataFromValue();
00236   const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
00237 }
00238 
00239 size_t Function::arg_size() const {
00240   return getFunctionType()->getNumParams();
00241 }
00242 bool Function::arg_empty() const {
00243   return getFunctionType()->getNumParams() == 0;
00244 }
00245 
00246 void Function::setParent(Module *parent) {
00247   if (getParent())
00248     LeakDetector::addGarbageObject(this);
00249   Parent = parent;
00250   if (getParent())
00251     LeakDetector::removeGarbageObject(this);
00252 }
00253 
00254 // dropAllReferences() - This function causes all the subinstructions to "let
00255 // go" of all references that they are maintaining.  This allows one to
00256 // 'delete' a whole class at a time, even though there may be circular
00257 // references... first all references are dropped, and all use counts go to
00258 // zero.  Then everything is deleted for real.  Note that no operations are
00259 // valid on an object that has "dropped all references", except operator
00260 // delete.
00261 //
00262 void Function::dropAllReferences() {
00263   for (iterator I = begin(), E = end(); I != E; ++I)
00264     I->dropAllReferences();
00265 
00266   // Delete all basic blocks. They are now unused, except possibly by
00267   // blockaddresses, but BasicBlock's destructor takes care of those.
00268   while (!BasicBlocks.empty())
00269     BasicBlocks.begin()->eraseFromParent();
00270 }
00271 
00272 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
00273   AttributeSet PAL = getAttributes();
00274   PAL = PAL.addAttribute(getContext(), i, attr);
00275   setAttributes(PAL);
00276 }
00277 
00278 void Function::addAttributes(unsigned i, AttributeSet attrs) {
00279   AttributeSet PAL = getAttributes();
00280   PAL = PAL.addAttributes(getContext(), i, attrs);
00281   setAttributes(PAL);
00282 }
00283 
00284 void Function::removeAttributes(unsigned i, AttributeSet attrs) {
00285   AttributeSet PAL = getAttributes();
00286   PAL = PAL.removeAttributes(getContext(), i, attrs);
00287   setAttributes(PAL);
00288 }
00289 
00290 // Maintain the GC name for each function in an on-the-side table. This saves
00291 // allocating an additional word in Function for programs which do not use GC
00292 // (i.e., most programs) at the cost of increased overhead for clients which do
00293 // use GC.
00294 static DenseMap<const Function*,PooledStringPtr> *GCNames;
00295 static StringPool *GCNamePool;
00296 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
00297 
00298 bool Function::hasGC() const {
00299   sys::SmartScopedReader<true> Reader(*GCLock);
00300   return GCNames && GCNames->count(this);
00301 }
00302 
00303 const char *Function::getGC() const {
00304   assert(hasGC() && "Function has no collector");
00305   sys::SmartScopedReader<true> Reader(*GCLock);
00306   return *(*GCNames)[this];
00307 }
00308 
00309 void Function::setGC(const char *Str) {
00310   sys::SmartScopedWriter<true> Writer(*GCLock);
00311   if (!GCNamePool)
00312     GCNamePool = new StringPool();
00313   if (!GCNames)
00314     GCNames = new DenseMap<const Function*,PooledStringPtr>();
00315   (*GCNames)[this] = GCNamePool->intern(Str);
00316 }
00317 
00318 void Function::clearGC() {
00319   sys::SmartScopedWriter<true> Writer(*GCLock);
00320   if (GCNames) {
00321     GCNames->erase(this);
00322     if (GCNames->empty()) {
00323       delete GCNames;
00324       GCNames = 0;
00325       if (GCNamePool->empty()) {
00326         delete GCNamePool;
00327         GCNamePool = 0;
00328       }
00329     }
00330   }
00331 }
00332 
00333 /// copyAttributesFrom - copy all additional attributes (those not needed to
00334 /// create a Function) from the Function Src to this one.
00335 void Function::copyAttributesFrom(const GlobalValue *Src) {
00336   assert(isa<Function>(Src) && "Expected a Function!");
00337   GlobalValue::copyAttributesFrom(Src);
00338   const Function *SrcF = cast<Function>(Src);
00339   setCallingConv(SrcF->getCallingConv());
00340   setAttributes(SrcF->getAttributes());
00341   if (SrcF->hasGC())
00342     setGC(SrcF->getGC());
00343   else
00344     clearGC();
00345 }
00346 
00347 /// getIntrinsicID - This method returns the ID number of the specified
00348 /// function, or Intrinsic::not_intrinsic if the function is not an
00349 /// intrinsic, or if the pointer is null.  This value is always defined to be
00350 /// zero to allow easy checking for whether a function is intrinsic or not.  The
00351 /// particular intrinsic functions which correspond to this value are defined in
00352 /// llvm/Intrinsics.h.  Results are cached in the LLVM context, subsequent
00353 /// requests for the same ID return results much faster from the cache.
00354 ///
00355 unsigned Function::getIntrinsicID() const {
00356   const ValueName *ValName = this->getValueName();
00357   if (!ValName || !isIntrinsic())
00358     return 0;
00359 
00360   LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
00361     getContext().pImpl->IntrinsicIDCache;
00362   if (!IntrinsicIDCache.count(this)) {
00363     unsigned Id = lookupIntrinsicID();
00364     IntrinsicIDCache[this]=Id;
00365     return Id;
00366   }
00367   return IntrinsicIDCache[this];
00368 }
00369 
00370 /// This private method does the actual lookup of an intrinsic ID when the query
00371 /// could not be answered from the cache.
00372 unsigned Function::lookupIntrinsicID() const {
00373   const ValueName *ValName = this->getValueName();
00374   unsigned Len = ValName->getKeyLength();
00375   const char *Name = ValName->getKeyData();
00376 
00377 #define GET_FUNCTION_RECOGNIZER
00378 #include "llvm/IR/Intrinsics.gen"
00379 #undef GET_FUNCTION_RECOGNIZER
00380 
00381   return 0;
00382 }
00383 
00384 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
00385   assert(id < num_intrinsics && "Invalid intrinsic ID!");
00386   static const char * const Table[] = {
00387     "not_intrinsic",
00388 #define GET_INTRINSIC_NAME_TABLE
00389 #include "llvm/IR/Intrinsics.gen"
00390 #undef GET_INTRINSIC_NAME_TABLE
00391   };
00392   if (Tys.empty())
00393     return Table[id];
00394   std::string Result(Table[id]);
00395   for (unsigned i = 0; i < Tys.size(); ++i) {
00396     if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
00397       Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
00398                 EVT::getEVT(PTyp->getElementType()).getEVTString();
00399     }
00400     else if (Tys[i])
00401       Result += "." + EVT::getEVT(Tys[i]).getEVTString();
00402   }
00403   return Result;
00404 }
00405 
00406 
00407 /// IIT_Info - These are enumerators that describe the entries returned by the
00408 /// getIntrinsicInfoTableEntries function.
00409 ///
00410 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
00411 enum IIT_Info {
00412   // Common values should be encoded with 0-15.
00413   IIT_Done = 0,
00414   IIT_I1   = 1,
00415   IIT_I8   = 2,
00416   IIT_I16  = 3,
00417   IIT_I32  = 4,
00418   IIT_I64  = 5,
00419   IIT_F16  = 6,
00420   IIT_F32  = 7,
00421   IIT_F64  = 8,
00422   IIT_V2   = 9,
00423   IIT_V4   = 10,
00424   IIT_V8   = 11,
00425   IIT_V16  = 12,
00426   IIT_V32  = 13,
00427   IIT_PTR  = 14,
00428   IIT_ARG  = 15,
00429 
00430   // Values from 16+ are only encodable with the inefficient encoding.
00431   IIT_MMX  = 16,
00432   IIT_METADATA = 17,
00433   IIT_EMPTYSTRUCT = 18,
00434   IIT_STRUCT2 = 19,
00435   IIT_STRUCT3 = 20,
00436   IIT_STRUCT4 = 21,
00437   IIT_STRUCT5 = 22,
00438   IIT_EXTEND_VEC_ARG = 23,
00439   IIT_TRUNC_VEC_ARG = 24,
00440   IIT_ANYPTR = 25
00441 };
00442 
00443 
00444 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
00445                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
00446   IIT_Info Info = IIT_Info(Infos[NextElt++]);
00447   unsigned StructElts = 2;
00448   using namespace Intrinsic;
00449 
00450   switch (Info) {
00451   case IIT_Done:
00452     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
00453     return;
00454   case IIT_MMX:
00455     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
00456     return;
00457   case IIT_METADATA:
00458     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
00459     return;
00460   case IIT_F16:
00461     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
00462     return;
00463   case IIT_F32:
00464     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
00465     return;
00466   case IIT_F64:
00467     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
00468     return;
00469   case IIT_I1:
00470     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
00471     return;
00472   case IIT_I8:
00473     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
00474     return;
00475   case IIT_I16:
00476     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
00477     return;
00478   case IIT_I32:
00479     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
00480     return;
00481   case IIT_I64:
00482     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
00483     return;
00484   case IIT_V2:
00485     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
00486     DecodeIITType(NextElt, Infos, OutputTable);
00487     return;
00488   case IIT_V4:
00489     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
00490     DecodeIITType(NextElt, Infos, OutputTable);
00491     return;
00492   case IIT_V8:
00493     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
00494     DecodeIITType(NextElt, Infos, OutputTable);
00495     return;
00496   case IIT_V16:
00497     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
00498     DecodeIITType(NextElt, Infos, OutputTable);
00499     return;
00500   case IIT_V32:
00501     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
00502     DecodeIITType(NextElt, Infos, OutputTable);
00503     return;
00504   case IIT_PTR:
00505     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
00506     DecodeIITType(NextElt, Infos, OutputTable);
00507     return;
00508   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
00509     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
00510                                              Infos[NextElt++]));
00511     DecodeIITType(NextElt, Infos, OutputTable);
00512     return;
00513   }
00514   case IIT_ARG: {
00515     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00516     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
00517     return;
00518   }
00519   case IIT_EXTEND_VEC_ARG: {
00520     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00521     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
00522                                              ArgInfo));
00523     return;
00524   }
00525   case IIT_TRUNC_VEC_ARG: {
00526     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00527     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
00528                                              ArgInfo));
00529     return;
00530   }
00531   case IIT_EMPTYSTRUCT:
00532     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
00533     return;
00534   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
00535   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
00536   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
00537   case IIT_STRUCT2: {
00538     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
00539 
00540     for (unsigned i = 0; i != StructElts; ++i)
00541       DecodeIITType(NextElt, Infos, OutputTable);
00542     return;
00543   }
00544   }
00545   llvm_unreachable("unhandled");
00546 }
00547 
00548 
00549 #define GET_INTRINSIC_GENERATOR_GLOBAL
00550 #include "llvm/IR/Intrinsics.gen"
00551 #undef GET_INTRINSIC_GENERATOR_GLOBAL
00552 
00553 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
00554                                              SmallVectorImpl<IITDescriptor> &T){
00555   // Check to see if the intrinsic's type was expressible by the table.
00556   unsigned TableVal = IIT_Table[id-1];
00557 
00558   // Decode the TableVal into an array of IITValues.
00559   SmallVector<unsigned char, 8> IITValues;
00560   ArrayRef<unsigned char> IITEntries;
00561   unsigned NextElt = 0;
00562   if ((TableVal >> 31) != 0) {
00563     // This is an offset into the IIT_LongEncodingTable.
00564     IITEntries = IIT_LongEncodingTable;
00565 
00566     // Strip sentinel bit.
00567     NextElt = (TableVal << 1) >> 1;
00568   } else {
00569     // Decode the TableVal into an array of IITValues.  If the entry was encoded
00570     // into a single word in the table itself, decode it now.
00571     do {
00572       IITValues.push_back(TableVal & 0xF);
00573       TableVal >>= 4;
00574     } while (TableVal);
00575 
00576     IITEntries = IITValues;
00577     NextElt = 0;
00578   }
00579 
00580   // Okay, decode the table into the output vector of IITDescriptors.
00581   DecodeIITType(NextElt, IITEntries, T);
00582   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
00583     DecodeIITType(NextElt, IITEntries, T);
00584 }
00585 
00586 
00587 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
00588                              ArrayRef<Type*> Tys, LLVMContext &Context) {
00589   using namespace Intrinsic;
00590   IITDescriptor D = Infos.front();
00591   Infos = Infos.slice(1);
00592 
00593   switch (D.Kind) {
00594   case IITDescriptor::Void: return Type::getVoidTy(Context);
00595   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
00596   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
00597   case IITDescriptor::Half: return Type::getHalfTy(Context);
00598   case IITDescriptor::Float: return Type::getFloatTy(Context);
00599   case IITDescriptor::Double: return Type::getDoubleTy(Context);
00600 
00601   case IITDescriptor::Integer:
00602     return IntegerType::get(Context, D.Integer_Width);
00603   case IITDescriptor::Vector:
00604     return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
00605   case IITDescriptor::Pointer:
00606     return PointerType::get(DecodeFixedType(Infos, Tys, Context),
00607                             D.Pointer_AddressSpace);
00608   case IITDescriptor::Struct: {
00609     Type *Elts[5];
00610     assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
00611     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
00612       Elts[i] = DecodeFixedType(Infos, Tys, Context);
00613     return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
00614   }
00615 
00616   case IITDescriptor::Argument:
00617     return Tys[D.getArgumentNumber()];
00618   case IITDescriptor::ExtendVecArgument:
00619     return VectorType::getExtendedElementVectorType(cast<VectorType>(
00620                                                   Tys[D.getArgumentNumber()]));
00621 
00622   case IITDescriptor::TruncVecArgument:
00623     return VectorType::getTruncatedElementVectorType(cast<VectorType>(
00624                                                   Tys[D.getArgumentNumber()]));
00625   }
00626   llvm_unreachable("unhandled");
00627 }
00628 
00629 
00630 
00631 FunctionType *Intrinsic::getType(LLVMContext &Context,
00632                                  ID id, ArrayRef<Type*> Tys) {
00633   SmallVector<IITDescriptor, 8> Table;
00634   getIntrinsicInfoTableEntries(id, Table);
00635 
00636   ArrayRef<IITDescriptor> TableRef = Table;
00637   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
00638 
00639   SmallVector<Type*, 8> ArgTys;
00640   while (!TableRef.empty())
00641     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
00642 
00643   return FunctionType::get(ResultTy, ArgTys, false);
00644 }
00645 
00646 bool Intrinsic::isOverloaded(ID id) {
00647 #define GET_INTRINSIC_OVERLOAD_TABLE
00648 #include "llvm/IR/Intrinsics.gen"
00649 #undef GET_INTRINSIC_OVERLOAD_TABLE
00650 }
00651 
00652 /// This defines the "Intrinsic::getAttributes(ID id)" method.
00653 #define GET_INTRINSIC_ATTRIBUTES
00654 #include "llvm/IR/Intrinsics.gen"
00655 #undef GET_INTRINSIC_ATTRIBUTES
00656 
00657 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
00658   // There can never be multiple globals with the same name of different types,
00659   // because intrinsics must be a specific type.
00660   return
00661     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
00662                                           getType(M->getContext(), id, Tys)));
00663 }
00664 
00665 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
00666 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00667 #include "llvm/IR/Intrinsics.gen"
00668 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00669 
00670 /// hasAddressTaken - returns true if there are any uses of this function
00671 /// other than direct calls or invokes to it.
00672 bool Function::hasAddressTaken(const User* *PutOffender) const {
00673   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
00674     const User *U = *I;
00675     if (isa<BlockAddress>(U))
00676       continue;
00677     if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
00678       return PutOffender ? (*PutOffender = U, true) : true;
00679     ImmutableCallSite CS(cast<Instruction>(U));
00680     if (!CS.isCallee(I))
00681       return PutOffender ? (*PutOffender = U, true) : true;
00682   }
00683   return false;
00684 }
00685 
00686 bool Function::isDefTriviallyDead() const {
00687   // Check the linkage
00688   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
00689       !hasAvailableExternallyLinkage())
00690     return false;
00691 
00692   // Check if the function is used by anything other than a blockaddress.
00693   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
00694     if (!isa<BlockAddress>(*I))
00695       return false;
00696 
00697   return true;
00698 }
00699 
00700 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
00701 /// setjmp or other function that gcc recognizes as "returning twice".
00702 bool Function::callsFunctionThatReturnsTwice() const {
00703   for (const_inst_iterator
00704          I = inst_begin(this), E = inst_end(this); I != E; ++I) {
00705     const CallInst* callInst = dyn_cast<CallInst>(&*I);
00706     if (!callInst)
00707       continue;
00708     if (callInst->canReturnTwice())
00709       return true;
00710   }
00711 
00712   return false;
00713 }
00714