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/CallSite.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include "llvm/IR/InstIterator.h"
00024 #include "llvm/IR/IntrinsicInst.h"
00025 #include "llvm/IR/LLVMContext.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/Support/ManagedStatic.h"
00028 #include "llvm/Support/RWMutex.h"
00029 #include "llvm/Support/StringPool.h"
00030 #include "llvm/Support/Threading.h"
00031 using namespace llvm;
00032 
00033 // Explicit instantiations of SymbolTableListTraits since some of the methods
00034 // are not in the public header file...
00035 template class llvm::SymbolTableListTraits<Argument, Function>;
00036 template class llvm::SymbolTableListTraits<BasicBlock, Function>;
00037 
00038 //===----------------------------------------------------------------------===//
00039 // Argument Implementation
00040 //===----------------------------------------------------------------------===//
00041 
00042 void Argument::anchor() { }
00043 
00044 Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
00045   : Value(Ty, Value::ArgumentVal) {
00046   Parent = nullptr;
00047 
00048   if (Par)
00049     Par->getArgumentList().push_back(this);
00050   setName(Name);
00051 }
00052 
00053 void Argument::setParent(Function *parent) {
00054   Parent = parent;
00055 }
00056 
00057 /// getArgNo - Return the index of this formal argument in its containing
00058 /// function.  For example in "void foo(int a, float b)" a is 0 and b is 1.
00059 unsigned Argument::getArgNo() const {
00060   const Function *F = getParent();
00061   assert(F && "Argument is not in a function");
00062 
00063   Function::const_arg_iterator AI = F->arg_begin();
00064   unsigned ArgIdx = 0;
00065   for (; &*AI != this; ++AI)
00066     ++ArgIdx;
00067 
00068   return ArgIdx;
00069 }
00070 
00071 /// hasNonNullAttr - Return true if this argument has the nonnull attribute on
00072 /// it in its containing function. Also returns true if at least one byte is
00073 /// known to be dereferenceable and the pointer is in addrspace(0).
00074 bool Argument::hasNonNullAttr() const {
00075   if (!getType()->isPointerTy()) return false;
00076   if (getParent()->getAttributes().
00077         hasAttribute(getArgNo()+1, Attribute::NonNull))
00078     return true;
00079   else if (getDereferenceableBytes() > 0 &&
00080            getType()->getPointerAddressSpace() == 0)
00081     return true;
00082   return false;
00083 }
00084 
00085 /// hasByValAttr - Return true if this argument has the byval attribute on it
00086 /// in its containing function.
00087 bool Argument::hasByValAttr() const {
00088   if (!getType()->isPointerTy()) return false;
00089   return getParent()->getAttributes().
00090     hasAttribute(getArgNo()+1, Attribute::ByVal);
00091 }
00092 
00093 /// \brief Return true if this argument has the inalloca attribute on it in
00094 /// its containing function.
00095 bool Argument::hasInAllocaAttr() const {
00096   if (!getType()->isPointerTy()) return false;
00097   return getParent()->getAttributes().
00098     hasAttribute(getArgNo()+1, Attribute::InAlloca);
00099 }
00100 
00101 bool Argument::hasByValOrInAllocaAttr() const {
00102   if (!getType()->isPointerTy()) return false;
00103   AttributeSet Attrs = getParent()->getAttributes();
00104   return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
00105          Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
00106 }
00107 
00108 unsigned Argument::getParamAlignment() const {
00109   assert(getType()->isPointerTy() && "Only pointers have alignments");
00110   return getParent()->getParamAlignment(getArgNo()+1);
00111 
00112 }
00113 
00114 uint64_t Argument::getDereferenceableBytes() const {
00115   assert(getType()->isPointerTy() &&
00116          "Only pointers have dereferenceable bytes");
00117   return getParent()->getDereferenceableBytes(getArgNo()+1);
00118 }
00119 
00120 /// hasNestAttr - Return true if this argument has the nest attribute on
00121 /// it in its containing function.
00122 bool Argument::hasNestAttr() const {
00123   if (!getType()->isPointerTy()) return false;
00124   return getParent()->getAttributes().
00125     hasAttribute(getArgNo()+1, Attribute::Nest);
00126 }
00127 
00128 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
00129 /// it in its containing function.
00130 bool Argument::hasNoAliasAttr() const {
00131   if (!getType()->isPointerTy()) return false;
00132   return getParent()->getAttributes().
00133     hasAttribute(getArgNo()+1, Attribute::NoAlias);
00134 }
00135 
00136 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
00137 /// on it in its containing function.
00138 bool Argument::hasNoCaptureAttr() const {
00139   if (!getType()->isPointerTy()) return false;
00140   return getParent()->getAttributes().
00141     hasAttribute(getArgNo()+1, Attribute::NoCapture);
00142 }
00143 
00144 /// hasSRetAttr - Return true if this argument has the sret attribute on
00145 /// it in its containing function.
00146 bool Argument::hasStructRetAttr() const {
00147   if (!getType()->isPointerTy()) return false;
00148   if (this != getParent()->arg_begin())
00149     return false; // StructRet param must be first param
00150   return getParent()->getAttributes().
00151     hasAttribute(1, Attribute::StructRet);
00152 }
00153 
00154 /// hasReturnedAttr - Return true if this argument has the returned attribute on
00155 /// it in its containing function.
00156 bool Argument::hasReturnedAttr() const {
00157   return getParent()->getAttributes().
00158     hasAttribute(getArgNo()+1, Attribute::Returned);
00159 }
00160 
00161 /// hasZExtAttr - Return true if this argument has the zext attribute on it in
00162 /// its containing function.
00163 bool Argument::hasZExtAttr() const {
00164   return getParent()->getAttributes().
00165     hasAttribute(getArgNo()+1, Attribute::ZExt);
00166 }
00167 
00168 /// hasSExtAttr Return true if this argument has the sext attribute on it in its
00169 /// containing function.
00170 bool Argument::hasSExtAttr() const {
00171   return getParent()->getAttributes().
00172     hasAttribute(getArgNo()+1, Attribute::SExt);
00173 }
00174 
00175 /// Return true if this argument has the readonly or readnone attribute on it
00176 /// in its containing function.
00177 bool Argument::onlyReadsMemory() const {
00178   return getParent()->getAttributes().
00179       hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
00180       getParent()->getAttributes().
00181       hasAttribute(getArgNo()+1, Attribute::ReadNone);
00182 }
00183 
00184 /// addAttr - Add attributes to an argument.
00185 void Argument::addAttr(AttributeSet AS) {
00186   assert(AS.getNumSlots() <= 1 &&
00187          "Trying to add more than one attribute set to an argument!");
00188   AttrBuilder B(AS, AS.getSlotIndex(0));
00189   getParent()->addAttributes(getArgNo() + 1,
00190                              AttributeSet::get(Parent->getContext(),
00191                                                getArgNo() + 1, B));
00192 }
00193 
00194 /// removeAttr - Remove attributes from an argument.
00195 void Argument::removeAttr(AttributeSet AS) {
00196   assert(AS.getNumSlots() <= 1 &&
00197          "Trying to remove more than one attribute set from an argument!");
00198   AttrBuilder B(AS, AS.getSlotIndex(0));
00199   getParent()->removeAttributes(getArgNo() + 1,
00200                                 AttributeSet::get(Parent->getContext(),
00201                                                   getArgNo() + 1, B));
00202 }
00203 
00204 //===----------------------------------------------------------------------===//
00205 // Helper Methods in Function
00206 //===----------------------------------------------------------------------===//
00207 
00208 bool Function::isMaterializable() const {
00209   return getGlobalObjectSubClassData();
00210 }
00211 
00212 void Function::setIsMaterializable(bool V) { setGlobalObjectSubClassData(V); }
00213 
00214 LLVMContext &Function::getContext() const {
00215   return getType()->getContext();
00216 }
00217 
00218 FunctionType *Function::getFunctionType() const {
00219   return cast<FunctionType>(getType()->getElementType());
00220 }
00221 
00222 bool Function::isVarArg() const {
00223   return getFunctionType()->isVarArg();
00224 }
00225 
00226 Type *Function::getReturnType() const {
00227   return getFunctionType()->getReturnType();
00228 }
00229 
00230 void Function::removeFromParent() {
00231   getParent()->getFunctionList().remove(this);
00232 }
00233 
00234 void Function::eraseFromParent() {
00235   getParent()->getFunctionList().erase(this);
00236 }
00237 
00238 //===----------------------------------------------------------------------===//
00239 // Function Implementation
00240 //===----------------------------------------------------------------------===//
00241 
00242 Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name,
00243                    Module *ParentModule)
00244     : GlobalObject(PointerType::getUnqual(Ty), Value::FunctionVal, nullptr, 0,
00245                    Linkage, name) {
00246   assert(FunctionType::isValidReturnType(getReturnType()) &&
00247          "invalid return type");
00248   setIsMaterializable(false);
00249   SymTab = new ValueSymbolTable();
00250 
00251   // If the function has arguments, mark them as lazily built.
00252   if (Ty->getNumParams())
00253     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
00254 
00255   if (ParentModule)
00256     ParentModule->getFunctionList().push_back(this);
00257 
00258   // Ensure intrinsics have the right parameter attributes.
00259   if (unsigned IID = getIntrinsicID())
00260     setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
00261 
00262 }
00263 
00264 Function::~Function() {
00265   dropAllReferences();    // After this it is safe to delete instructions.
00266 
00267   // Delete all of the method arguments and unlink from symbol table...
00268   ArgumentList.clear();
00269   delete SymTab;
00270 
00271   // Remove the function from the on-the-side GC table.
00272   clearGC();
00273 
00274   // Remove the intrinsicID from the Cache.
00275   if (getValueName() && isIntrinsic())
00276     getContext().pImpl->IntrinsicIDCache.erase(this);
00277 }
00278 
00279 void Function::BuildLazyArguments() const {
00280   // Create the arguments vector, all arguments start out unnamed.
00281   FunctionType *FT = getFunctionType();
00282   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
00283     assert(!FT->getParamType(i)->isVoidTy() &&
00284            "Cannot have void typed arguments!");
00285     ArgumentList.push_back(new Argument(FT->getParamType(i)));
00286   }
00287 
00288   // Clear the lazy arguments bit.
00289   unsigned SDC = getSubclassDataFromValue();
00290   const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
00291 }
00292 
00293 size_t Function::arg_size() const {
00294   return getFunctionType()->getNumParams();
00295 }
00296 bool Function::arg_empty() const {
00297   return getFunctionType()->getNumParams() == 0;
00298 }
00299 
00300 void Function::setParent(Module *parent) {
00301   Parent = parent;
00302 }
00303 
00304 // dropAllReferences() - This function causes all the subinstructions to "let
00305 // go" of all references that they are maintaining.  This allows one to
00306 // 'delete' a whole class at a time, even though there may be circular
00307 // references... first all references are dropped, and all use counts go to
00308 // zero.  Then everything is deleted for real.  Note that no operations are
00309 // valid on an object that has "dropped all references", except operator
00310 // delete.
00311 //
00312 void Function::dropAllReferences() {
00313   setIsMaterializable(false);
00314 
00315   for (iterator I = begin(), E = end(); I != E; ++I)
00316     I->dropAllReferences();
00317 
00318   // Delete all basic blocks. They are now unused, except possibly by
00319   // blockaddresses, but BasicBlock's destructor takes care of those.
00320   while (!BasicBlocks.empty())
00321     BasicBlocks.begin()->eraseFromParent();
00322 
00323   // Prefix and prologue data are stored in a side table.
00324   setPrefixData(nullptr);
00325   setPrologueData(nullptr);
00326 }
00327 
00328 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
00329   AttributeSet PAL = getAttributes();
00330   PAL = PAL.addAttribute(getContext(), i, attr);
00331   setAttributes(PAL);
00332 }
00333 
00334 void Function::addAttributes(unsigned i, AttributeSet attrs) {
00335   AttributeSet PAL = getAttributes();
00336   PAL = PAL.addAttributes(getContext(), i, attrs);
00337   setAttributes(PAL);
00338 }
00339 
00340 void Function::removeAttributes(unsigned i, AttributeSet attrs) {
00341   AttributeSet PAL = getAttributes();
00342   PAL = PAL.removeAttributes(getContext(), i, attrs);
00343   setAttributes(PAL);
00344 }
00345 
00346 void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
00347   AttributeSet PAL = getAttributes();
00348   PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
00349   setAttributes(PAL);
00350 }
00351 
00352 // Maintain the GC name for each function in an on-the-side table. This saves
00353 // allocating an additional word in Function for programs which do not use GC
00354 // (i.e., most programs) at the cost of increased overhead for clients which do
00355 // use GC.
00356 static DenseMap<const Function*,PooledStringPtr> *GCNames;
00357 static StringPool *GCNamePool;
00358 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
00359 
00360 bool Function::hasGC() const {
00361   sys::SmartScopedReader<true> Reader(*GCLock);
00362   return GCNames && GCNames->count(this);
00363 }
00364 
00365 const char *Function::getGC() const {
00366   assert(hasGC() && "Function has no collector");
00367   sys::SmartScopedReader<true> Reader(*GCLock);
00368   return *(*GCNames)[this];
00369 }
00370 
00371 void Function::setGC(const char *Str) {
00372   sys::SmartScopedWriter<true> Writer(*GCLock);
00373   if (!GCNamePool)
00374     GCNamePool = new StringPool();
00375   if (!GCNames)
00376     GCNames = new DenseMap<const Function*,PooledStringPtr>();
00377   (*GCNames)[this] = GCNamePool->intern(Str);
00378 }
00379 
00380 void Function::clearGC() {
00381   sys::SmartScopedWriter<true> Writer(*GCLock);
00382   if (GCNames) {
00383     GCNames->erase(this);
00384     if (GCNames->empty()) {
00385       delete GCNames;
00386       GCNames = nullptr;
00387       if (GCNamePool->empty()) {
00388         delete GCNamePool;
00389         GCNamePool = nullptr;
00390       }
00391     }
00392   }
00393 }
00394 
00395 /// copyAttributesFrom - copy all additional attributes (those not needed to
00396 /// create a Function) from the Function Src to this one.
00397 void Function::copyAttributesFrom(const GlobalValue *Src) {
00398   assert(isa<Function>(Src) && "Expected a Function!");
00399   GlobalObject::copyAttributesFrom(Src);
00400   const Function *SrcF = cast<Function>(Src);
00401   setCallingConv(SrcF->getCallingConv());
00402   setAttributes(SrcF->getAttributes());
00403   if (SrcF->hasGC())
00404     setGC(SrcF->getGC());
00405   else
00406     clearGC();
00407   if (SrcF->hasPrefixData())
00408     setPrefixData(SrcF->getPrefixData());
00409   else
00410     setPrefixData(nullptr);
00411   if (SrcF->hasPrologueData())
00412     setPrologueData(SrcF->getPrologueData());
00413   else
00414     setPrologueData(nullptr);
00415 }
00416 
00417 /// getIntrinsicID - This method returns the ID number of the specified
00418 /// function, or Intrinsic::not_intrinsic if the function is not an
00419 /// intrinsic, or if the pointer is null.  This value is always defined to be
00420 /// zero to allow easy checking for whether a function is intrinsic or not.  The
00421 /// particular intrinsic functions which correspond to this value are defined in
00422 /// llvm/Intrinsics.h.  Results are cached in the LLVM context, subsequent
00423 /// requests for the same ID return results much faster from the cache.
00424 ///
00425 unsigned Function::getIntrinsicID() const {
00426   const ValueName *ValName = this->getValueName();
00427   if (!ValName || !isIntrinsic())
00428     return 0;
00429 
00430   LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
00431     getContext().pImpl->IntrinsicIDCache;
00432   if (!IntrinsicIDCache.count(this)) {
00433     unsigned Id = lookupIntrinsicID();
00434     IntrinsicIDCache[this]=Id;
00435     return Id;
00436   }
00437   return IntrinsicIDCache[this];
00438 }
00439 
00440 /// This private method does the actual lookup of an intrinsic ID when the query
00441 /// could not be answered from the cache.
00442 unsigned Function::lookupIntrinsicID() const {
00443   const ValueName *ValName = this->getValueName();
00444   unsigned Len = ValName->getKeyLength();
00445   const char *Name = ValName->getKeyData();
00446 
00447 #define GET_FUNCTION_RECOGNIZER
00448 #include "llvm/IR/Intrinsics.gen"
00449 #undef GET_FUNCTION_RECOGNIZER
00450 
00451   return 0;
00452 }
00453 
00454 /// Returns a stable mangling for the type specified for use in the name
00455 /// mangling scheme used by 'any' types in intrinsic signatures.  The mangling
00456 /// of named types is simply their name.  Manglings for unnamed types consist
00457 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
00458 /// combined with the mangling of their component types.  A vararg function
00459 /// type will have a suffix of 'vararg'.  Since function types can contain
00460 /// other function types, we close a function type mangling with suffix 'f'
00461 /// which can't be confused with it's prefix.  This ensures we don't have
00462 /// collisions between two unrelated function types. Otherwise, you might
00463 /// parse ffXX as f(fXX) or f(fX)X.  (X is a placeholder for any other type.)
00464 /// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most
00465 /// cases) fall back to the MVT codepath, where they could be mangled to
00466 /// 'x86mmx', for example; matching on derived types is not sufficient to mangle
00467 /// everything.
00468 static std::string getMangledTypeStr(Type* Ty) {
00469   std::string Result;
00470   if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
00471     Result += "p" + llvm::utostr(PTyp->getAddressSpace()) +
00472       getMangledTypeStr(PTyp->getElementType());
00473   } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
00474     Result += "a" + llvm::utostr(ATyp->getNumElements()) +
00475       getMangledTypeStr(ATyp->getElementType());
00476   } else if (StructType* STyp = dyn_cast<StructType>(Ty)) {
00477     if (!STyp->isLiteral())
00478       Result += STyp->getName();
00479     else
00480       llvm_unreachable("TODO: implement literal types");
00481   } else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) {
00482     Result += "f_" + getMangledTypeStr(FT->getReturnType());
00483     for (size_t i = 0; i < FT->getNumParams(); i++)
00484       Result += getMangledTypeStr(FT->getParamType(i));
00485     if (FT->isVarArg())
00486       Result += "vararg";
00487     // Ensure nested function types are distinguishable.
00488     Result += "f"; 
00489   } else if (Ty)
00490     Result += EVT::getEVT(Ty).getEVTString();
00491   return Result;
00492 }
00493 
00494 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
00495   assert(id < num_intrinsics && "Invalid intrinsic ID!");
00496   static const char * const Table[] = {
00497     "not_intrinsic",
00498 #define GET_INTRINSIC_NAME_TABLE
00499 #include "llvm/IR/Intrinsics.gen"
00500 #undef GET_INTRINSIC_NAME_TABLE
00501   };
00502   if (Tys.empty())
00503     return Table[id];
00504   std::string Result(Table[id]);
00505   for (unsigned i = 0; i < Tys.size(); ++i) {
00506     Result += "." + getMangledTypeStr(Tys[i]);
00507   }
00508   return Result;
00509 }
00510 
00511 
00512 /// IIT_Info - These are enumerators that describe the entries returned by the
00513 /// getIntrinsicInfoTableEntries function.
00514 ///
00515 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
00516 enum IIT_Info {
00517   // Common values should be encoded with 0-15.
00518   IIT_Done = 0,
00519   IIT_I1   = 1,
00520   IIT_I8   = 2,
00521   IIT_I16  = 3,
00522   IIT_I32  = 4,
00523   IIT_I64  = 5,
00524   IIT_F16  = 6,
00525   IIT_F32  = 7,
00526   IIT_F64  = 8,
00527   IIT_V2   = 9,
00528   IIT_V4   = 10,
00529   IIT_V8   = 11,
00530   IIT_V16  = 12,
00531   IIT_V32  = 13,
00532   IIT_PTR  = 14,
00533   IIT_ARG  = 15,
00534 
00535   // Values from 16+ are only encodable with the inefficient encoding.
00536   IIT_V64  = 16,
00537   IIT_MMX  = 17,
00538   IIT_METADATA = 18,
00539   IIT_EMPTYSTRUCT = 19,
00540   IIT_STRUCT2 = 20,
00541   IIT_STRUCT3 = 21,
00542   IIT_STRUCT4 = 22,
00543   IIT_STRUCT5 = 23,
00544   IIT_EXTEND_ARG = 24,
00545   IIT_TRUNC_ARG = 25,
00546   IIT_ANYPTR = 26,
00547   IIT_V1   = 27,
00548   IIT_VARARG = 28,
00549   IIT_HALF_VEC_ARG = 29,
00550   IIT_SAME_VEC_WIDTH_ARG = 30,
00551   IIT_PTR_TO_ARG = 31,
00552   IIT_VEC_OF_PTRS_TO_ELT = 32
00553 };
00554 
00555 
00556 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
00557                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
00558   IIT_Info Info = IIT_Info(Infos[NextElt++]);
00559   unsigned StructElts = 2;
00560   using namespace Intrinsic;
00561 
00562   switch (Info) {
00563   case IIT_Done:
00564     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
00565     return;
00566   case IIT_VARARG:
00567     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
00568     return;
00569   case IIT_MMX:
00570     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
00571     return;
00572   case IIT_METADATA:
00573     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
00574     return;
00575   case IIT_F16:
00576     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
00577     return;
00578   case IIT_F32:
00579     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
00580     return;
00581   case IIT_F64:
00582     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
00583     return;
00584   case IIT_I1:
00585     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
00586     return;
00587   case IIT_I8:
00588     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
00589     return;
00590   case IIT_I16:
00591     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
00592     return;
00593   case IIT_I32:
00594     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
00595     return;
00596   case IIT_I64:
00597     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
00598     return;
00599   case IIT_V1:
00600     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
00601     DecodeIITType(NextElt, Infos, OutputTable);
00602     return;
00603   case IIT_V2:
00604     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
00605     DecodeIITType(NextElt, Infos, OutputTable);
00606     return;
00607   case IIT_V4:
00608     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
00609     DecodeIITType(NextElt, Infos, OutputTable);
00610     return;
00611   case IIT_V8:
00612     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
00613     DecodeIITType(NextElt, Infos, OutputTable);
00614     return;
00615   case IIT_V16:
00616     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
00617     DecodeIITType(NextElt, Infos, OutputTable);
00618     return;
00619   case IIT_V32:
00620     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
00621     DecodeIITType(NextElt, Infos, OutputTable);
00622     return;
00623   case IIT_V64:
00624     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
00625     DecodeIITType(NextElt, Infos, OutputTable);
00626     return;
00627   case IIT_PTR:
00628     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
00629     DecodeIITType(NextElt, Infos, OutputTable);
00630     return;
00631   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
00632     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
00633                                              Infos[NextElt++]));
00634     DecodeIITType(NextElt, Infos, OutputTable);
00635     return;
00636   }
00637   case IIT_ARG: {
00638     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00639     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
00640     return;
00641   }
00642   case IIT_EXTEND_ARG: {
00643     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00644     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
00645                                              ArgInfo));
00646     return;
00647   }
00648   case IIT_TRUNC_ARG: {
00649     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00650     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
00651                                              ArgInfo));
00652     return;
00653   }
00654   case IIT_HALF_VEC_ARG: {
00655     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00656     OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
00657                                              ArgInfo));
00658     return;
00659   }
00660   case IIT_SAME_VEC_WIDTH_ARG: {
00661     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00662     OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
00663                                              ArgInfo));
00664     return;
00665   }
00666   case IIT_PTR_TO_ARG: {
00667     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00668     OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
00669                                              ArgInfo));
00670     return;
00671   }
00672   case IIT_VEC_OF_PTRS_TO_ELT: {
00673     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00674     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt,
00675                                              ArgInfo));
00676     return;
00677   }
00678   case IIT_EMPTYSTRUCT:
00679     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
00680     return;
00681   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
00682   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
00683   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
00684   case IIT_STRUCT2: {
00685     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
00686 
00687     for (unsigned i = 0; i != StructElts; ++i)
00688       DecodeIITType(NextElt, Infos, OutputTable);
00689     return;
00690   }
00691   }
00692   llvm_unreachable("unhandled");
00693 }
00694 
00695 
00696 #define GET_INTRINSIC_GENERATOR_GLOBAL
00697 #include "llvm/IR/Intrinsics.gen"
00698 #undef GET_INTRINSIC_GENERATOR_GLOBAL
00699 
00700 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
00701                                              SmallVectorImpl<IITDescriptor> &T){
00702   // Check to see if the intrinsic's type was expressible by the table.
00703   unsigned TableVal = IIT_Table[id-1];
00704 
00705   // Decode the TableVal into an array of IITValues.
00706   SmallVector<unsigned char, 8> IITValues;
00707   ArrayRef<unsigned char> IITEntries;
00708   unsigned NextElt = 0;
00709   if ((TableVal >> 31) != 0) {
00710     // This is an offset into the IIT_LongEncodingTable.
00711     IITEntries = IIT_LongEncodingTable;
00712 
00713     // Strip sentinel bit.
00714     NextElt = (TableVal << 1) >> 1;
00715   } else {
00716     // Decode the TableVal into an array of IITValues.  If the entry was encoded
00717     // into a single word in the table itself, decode it now.
00718     do {
00719       IITValues.push_back(TableVal & 0xF);
00720       TableVal >>= 4;
00721     } while (TableVal);
00722 
00723     IITEntries = IITValues;
00724     NextElt = 0;
00725   }
00726 
00727   // Okay, decode the table into the output vector of IITDescriptors.
00728   DecodeIITType(NextElt, IITEntries, T);
00729   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
00730     DecodeIITType(NextElt, IITEntries, T);
00731 }
00732 
00733 
00734 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
00735                              ArrayRef<Type*> Tys, LLVMContext &Context) {
00736   using namespace Intrinsic;
00737   IITDescriptor D = Infos.front();
00738   Infos = Infos.slice(1);
00739 
00740   switch (D.Kind) {
00741   case IITDescriptor::Void: return Type::getVoidTy(Context);
00742   case IITDescriptor::VarArg: return Type::getVoidTy(Context);
00743   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
00744   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
00745   case IITDescriptor::Half: return Type::getHalfTy(Context);
00746   case IITDescriptor::Float: return Type::getFloatTy(Context);
00747   case IITDescriptor::Double: return Type::getDoubleTy(Context);
00748 
00749   case IITDescriptor::Integer:
00750     return IntegerType::get(Context, D.Integer_Width);
00751   case IITDescriptor::Vector:
00752     return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
00753   case IITDescriptor::Pointer:
00754     return PointerType::get(DecodeFixedType(Infos, Tys, Context),
00755                             D.Pointer_AddressSpace);
00756   case IITDescriptor::Struct: {
00757     Type *Elts[5];
00758     assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
00759     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
00760       Elts[i] = DecodeFixedType(Infos, Tys, Context);
00761     return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
00762   }
00763 
00764   case IITDescriptor::Argument:
00765     return Tys[D.getArgumentNumber()];
00766   case IITDescriptor::ExtendArgument: {
00767     Type *Ty = Tys[D.getArgumentNumber()];
00768     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
00769       return VectorType::getExtendedElementVectorType(VTy);
00770 
00771     return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
00772   }
00773   case IITDescriptor::TruncArgument: {
00774     Type *Ty = Tys[D.getArgumentNumber()];
00775     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
00776       return VectorType::getTruncatedElementVectorType(VTy);
00777 
00778     IntegerType *ITy = cast<IntegerType>(Ty);
00779     assert(ITy->getBitWidth() % 2 == 0);
00780     return IntegerType::get(Context, ITy->getBitWidth() / 2);
00781   }
00782   case IITDescriptor::HalfVecArgument:
00783     return VectorType::getHalfElementsVectorType(cast<VectorType>(
00784                                                   Tys[D.getArgumentNumber()]));
00785   case IITDescriptor::SameVecWidthArgument: {
00786     Type *EltTy = DecodeFixedType(Infos, Tys, Context);
00787     Type *Ty = Tys[D.getArgumentNumber()];
00788     if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
00789       return VectorType::get(EltTy, VTy->getNumElements());
00790     }
00791     llvm_unreachable("unhandled");
00792   }
00793   case IITDescriptor::PtrToArgument: {
00794     Type *Ty = Tys[D.getArgumentNumber()];
00795     return PointerType::getUnqual(Ty);
00796   }
00797   case IITDescriptor::VecOfPtrsToElt: {
00798     Type *Ty = Tys[D.getArgumentNumber()];
00799     VectorType *VTy = dyn_cast<VectorType>(Ty);
00800     if (!VTy)
00801       llvm_unreachable("Expected an argument of Vector Type");
00802     Type *EltTy = VTy->getVectorElementType();
00803     return VectorType::get(PointerType::getUnqual(EltTy),
00804                            VTy->getNumElements());
00805   }
00806  }
00807   llvm_unreachable("unhandled");
00808 }
00809 
00810 
00811 
00812 FunctionType *Intrinsic::getType(LLVMContext &Context,
00813                                  ID id, ArrayRef<Type*> Tys) {
00814   SmallVector<IITDescriptor, 8> Table;
00815   getIntrinsicInfoTableEntries(id, Table);
00816 
00817   ArrayRef<IITDescriptor> TableRef = Table;
00818   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
00819 
00820   SmallVector<Type*, 8> ArgTys;
00821   while (!TableRef.empty())
00822     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
00823 
00824   // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
00825   // If we see void type as the type of the last argument, it is vararg intrinsic
00826   if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
00827     ArgTys.pop_back();
00828     return FunctionType::get(ResultTy, ArgTys, true);
00829   }
00830   return FunctionType::get(ResultTy, ArgTys, false);
00831 }
00832 
00833 bool Intrinsic::isOverloaded(ID id) {
00834 #define GET_INTRINSIC_OVERLOAD_TABLE
00835 #include "llvm/IR/Intrinsics.gen"
00836 #undef GET_INTRINSIC_OVERLOAD_TABLE
00837 }
00838 
00839 /// This defines the "Intrinsic::getAttributes(ID id)" method.
00840 #define GET_INTRINSIC_ATTRIBUTES
00841 #include "llvm/IR/Intrinsics.gen"
00842 #undef GET_INTRINSIC_ATTRIBUTES
00843 
00844 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
00845   // There can never be multiple globals with the same name of different types,
00846   // because intrinsics must be a specific type.
00847   return
00848     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
00849                                           getType(M->getContext(), id, Tys)));
00850 }
00851 
00852 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
00853 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00854 #include "llvm/IR/Intrinsics.gen"
00855 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00856 
00857 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
00858 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
00859 #include "llvm/IR/Intrinsics.gen"
00860 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
00861 
00862 /// hasAddressTaken - returns true if there are any uses of this function
00863 /// other than direct calls or invokes to it.
00864 bool Function::hasAddressTaken(const User* *PutOffender) const {
00865   for (const Use &U : uses()) {
00866     const User *FU = U.getUser();
00867     if (isa<BlockAddress>(FU))
00868       continue;
00869     if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
00870       return PutOffender ? (*PutOffender = FU, true) : true;
00871     ImmutableCallSite CS(cast<Instruction>(FU));
00872     if (!CS.isCallee(&U))
00873       return PutOffender ? (*PutOffender = FU, true) : true;
00874   }
00875   return false;
00876 }
00877 
00878 bool Function::isDefTriviallyDead() const {
00879   // Check the linkage
00880   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
00881       !hasAvailableExternallyLinkage())
00882     return false;
00883 
00884   // Check if the function is used by anything other than a blockaddress.
00885   for (const User *U : users())
00886     if (!isa<BlockAddress>(U))
00887       return false;
00888 
00889   return true;
00890 }
00891 
00892 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
00893 /// setjmp or other function that gcc recognizes as "returning twice".
00894 bool Function::callsFunctionThatReturnsTwice() const {
00895   for (const_inst_iterator
00896          I = inst_begin(this), E = inst_end(this); I != E; ++I) {
00897     ImmutableCallSite CS(&*I);
00898     if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
00899       return true;
00900   }
00901 
00902   return false;
00903 }
00904 
00905 Constant *Function::getPrefixData() const {
00906   assert(hasPrefixData());
00907   const LLVMContextImpl::PrefixDataMapTy &PDMap =
00908       getContext().pImpl->PrefixDataMap;
00909   assert(PDMap.find(this) != PDMap.end());
00910   return cast<Constant>(PDMap.find(this)->second->getReturnValue());
00911 }
00912 
00913 void Function::setPrefixData(Constant *PrefixData) {
00914   if (!PrefixData && !hasPrefixData())
00915     return;
00916 
00917   unsigned SCData = getSubclassDataFromValue();
00918   LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
00919   ReturnInst *&PDHolder = PDMap[this];
00920   if (PrefixData) {
00921     if (PDHolder)
00922       PDHolder->setOperand(0, PrefixData);
00923     else
00924       PDHolder = ReturnInst::Create(getContext(), PrefixData);
00925     SCData |= (1<<1);
00926   } else {
00927     delete PDHolder;
00928     PDMap.erase(this);
00929     SCData &= ~(1<<1);
00930   }
00931   setValueSubclassData(SCData);
00932 }
00933 
00934 Constant *Function::getPrologueData() const {
00935   assert(hasPrologueData());
00936   const LLVMContextImpl::PrologueDataMapTy &SOMap =
00937       getContext().pImpl->PrologueDataMap;
00938   assert(SOMap.find(this) != SOMap.end());
00939   return cast<Constant>(SOMap.find(this)->second->getReturnValue());
00940 }
00941 
00942 void Function::setPrologueData(Constant *PrologueData) {
00943   if (!PrologueData && !hasPrologueData())
00944     return;
00945 
00946   unsigned PDData = getSubclassDataFromValue();
00947   LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap;
00948   ReturnInst *&PDHolder = PDMap[this];
00949   if (PrologueData) {
00950     if (PDHolder)
00951       PDHolder->setOperand(0, PrologueData);
00952     else
00953       PDHolder = ReturnInst::Create(getContext(), PrologueData);
00954     PDData |= (1<<2);
00955   } else {
00956     delete PDHolder;
00957     PDMap.erase(this);
00958     PDData &= ~(1<<2);
00959   }
00960   setValueSubclassData(PDData);
00961 }