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