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-15.
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_PTR  = 14,
00493   IIT_ARG  = 15,
00494 
00495   // Values from 16+ are only encodable with the inefficient encoding.
00496   IIT_MMX  = 16,
00497   IIT_METADATA = 17,
00498   IIT_EMPTYSTRUCT = 18,
00499   IIT_STRUCT2 = 19,
00500   IIT_STRUCT3 = 20,
00501   IIT_STRUCT4 = 21,
00502   IIT_STRUCT5 = 22,
00503   IIT_EXTEND_ARG = 23,
00504   IIT_TRUNC_ARG = 24,
00505   IIT_ANYPTR = 25,
00506   IIT_V1   = 26,
00507   IIT_VARARG = 27,
00508   IIT_HALF_VEC_ARG = 28
00509 };
00510 
00511 
00512 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
00513                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
00514   IIT_Info Info = IIT_Info(Infos[NextElt++]);
00515   unsigned StructElts = 2;
00516   using namespace Intrinsic;
00517 
00518   switch (Info) {
00519   case IIT_Done:
00520     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
00521     return;
00522   case IIT_VARARG:
00523     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
00524     return;
00525   case IIT_MMX:
00526     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
00527     return;
00528   case IIT_METADATA:
00529     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
00530     return;
00531   case IIT_F16:
00532     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
00533     return;
00534   case IIT_F32:
00535     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
00536     return;
00537   case IIT_F64:
00538     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
00539     return;
00540   case IIT_I1:
00541     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
00542     return;
00543   case IIT_I8:
00544     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
00545     return;
00546   case IIT_I16:
00547     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
00548     return;
00549   case IIT_I32:
00550     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
00551     return;
00552   case IIT_I64:
00553     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
00554     return;
00555   case IIT_V1:
00556     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
00557     DecodeIITType(NextElt, Infos, OutputTable);
00558     return;
00559   case IIT_V2:
00560     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
00561     DecodeIITType(NextElt, Infos, OutputTable);
00562     return;
00563   case IIT_V4:
00564     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
00565     DecodeIITType(NextElt, Infos, OutputTable);
00566     return;
00567   case IIT_V8:
00568     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
00569     DecodeIITType(NextElt, Infos, OutputTable);
00570     return;
00571   case IIT_V16:
00572     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
00573     DecodeIITType(NextElt, Infos, OutputTable);
00574     return;
00575   case IIT_V32:
00576     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
00577     DecodeIITType(NextElt, Infos, OutputTable);
00578     return;
00579   case IIT_PTR:
00580     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
00581     DecodeIITType(NextElt, Infos, OutputTable);
00582     return;
00583   case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
00584     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
00585                                              Infos[NextElt++]));
00586     DecodeIITType(NextElt, Infos, OutputTable);
00587     return;
00588   }
00589   case IIT_ARG: {
00590     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00591     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
00592     return;
00593   }
00594   case IIT_EXTEND_ARG: {
00595     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00596     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
00597                                              ArgInfo));
00598     return;
00599   }
00600   case IIT_TRUNC_ARG: {
00601     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00602     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
00603                                              ArgInfo));
00604     return;
00605   }
00606   case IIT_HALF_VEC_ARG: {
00607     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
00608     OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
00609                                              ArgInfo));
00610     return;
00611   }
00612   case IIT_EMPTYSTRUCT:
00613     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
00614     return;
00615   case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
00616   case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
00617   case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
00618   case IIT_STRUCT2: {
00619     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
00620 
00621     for (unsigned i = 0; i != StructElts; ++i)
00622       DecodeIITType(NextElt, Infos, OutputTable);
00623     return;
00624   }
00625   }
00626   llvm_unreachable("unhandled");
00627 }
00628 
00629 
00630 #define GET_INTRINSIC_GENERATOR_GLOBAL
00631 #include "llvm/IR/Intrinsics.gen"
00632 #undef GET_INTRINSIC_GENERATOR_GLOBAL
00633 
00634 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
00635                                              SmallVectorImpl<IITDescriptor> &T){
00636   // Check to see if the intrinsic's type was expressible by the table.
00637   unsigned TableVal = IIT_Table[id-1];
00638 
00639   // Decode the TableVal into an array of IITValues.
00640   SmallVector<unsigned char, 8> IITValues;
00641   ArrayRef<unsigned char> IITEntries;
00642   unsigned NextElt = 0;
00643   if ((TableVal >> 31) != 0) {
00644     // This is an offset into the IIT_LongEncodingTable.
00645     IITEntries = IIT_LongEncodingTable;
00646 
00647     // Strip sentinel bit.
00648     NextElt = (TableVal << 1) >> 1;
00649   } else {
00650     // Decode the TableVal into an array of IITValues.  If the entry was encoded
00651     // into a single word in the table itself, decode it now.
00652     do {
00653       IITValues.push_back(TableVal & 0xF);
00654       TableVal >>= 4;
00655     } while (TableVal);
00656 
00657     IITEntries = IITValues;
00658     NextElt = 0;
00659   }
00660 
00661   // Okay, decode the table into the output vector of IITDescriptors.
00662   DecodeIITType(NextElt, IITEntries, T);
00663   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
00664     DecodeIITType(NextElt, IITEntries, T);
00665 }
00666 
00667 
00668 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
00669                              ArrayRef<Type*> Tys, LLVMContext &Context) {
00670   using namespace Intrinsic;
00671   IITDescriptor D = Infos.front();
00672   Infos = Infos.slice(1);
00673 
00674   switch (D.Kind) {
00675   case IITDescriptor::Void: return Type::getVoidTy(Context);
00676   case IITDescriptor::VarArg: return Type::getVoidTy(Context);
00677   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
00678   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
00679   case IITDescriptor::Half: return Type::getHalfTy(Context);
00680   case IITDescriptor::Float: return Type::getFloatTy(Context);
00681   case IITDescriptor::Double: return Type::getDoubleTy(Context);
00682 
00683   case IITDescriptor::Integer:
00684     return IntegerType::get(Context, D.Integer_Width);
00685   case IITDescriptor::Vector:
00686     return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
00687   case IITDescriptor::Pointer:
00688     return PointerType::get(DecodeFixedType(Infos, Tys, Context),
00689                             D.Pointer_AddressSpace);
00690   case IITDescriptor::Struct: {
00691     Type *Elts[5];
00692     assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
00693     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
00694       Elts[i] = DecodeFixedType(Infos, Tys, Context);
00695     return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
00696   }
00697 
00698   case IITDescriptor::Argument:
00699     return Tys[D.getArgumentNumber()];
00700   case IITDescriptor::ExtendArgument: {
00701     Type *Ty = Tys[D.getArgumentNumber()];
00702     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
00703       return VectorType::getExtendedElementVectorType(VTy);
00704 
00705     return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
00706   }
00707   case IITDescriptor::TruncArgument: {
00708     Type *Ty = Tys[D.getArgumentNumber()];
00709     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
00710       return VectorType::getTruncatedElementVectorType(VTy);
00711 
00712     IntegerType *ITy = cast<IntegerType>(Ty);
00713     assert(ITy->getBitWidth() % 2 == 0);
00714     return IntegerType::get(Context, ITy->getBitWidth() / 2);
00715   }
00716   case IITDescriptor::HalfVecArgument:
00717     return VectorType::getHalfElementsVectorType(cast<VectorType>(
00718                                                   Tys[D.getArgumentNumber()]));
00719   }
00720   llvm_unreachable("unhandled");
00721 }
00722 
00723 
00724 
00725 FunctionType *Intrinsic::getType(LLVMContext &Context,
00726                                  ID id, ArrayRef<Type*> Tys) {
00727   SmallVector<IITDescriptor, 8> Table;
00728   getIntrinsicInfoTableEntries(id, Table);
00729 
00730   ArrayRef<IITDescriptor> TableRef = Table;
00731   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
00732 
00733   SmallVector<Type*, 8> ArgTys;
00734   while (!TableRef.empty())
00735     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
00736 
00737   return FunctionType::get(ResultTy, ArgTys, false);
00738 }
00739 
00740 bool Intrinsic::isOverloaded(ID id) {
00741 #define GET_INTRINSIC_OVERLOAD_TABLE
00742 #include "llvm/IR/Intrinsics.gen"
00743 #undef GET_INTRINSIC_OVERLOAD_TABLE
00744 }
00745 
00746 /// This defines the "Intrinsic::getAttributes(ID id)" method.
00747 #define GET_INTRINSIC_ATTRIBUTES
00748 #include "llvm/IR/Intrinsics.gen"
00749 #undef GET_INTRINSIC_ATTRIBUTES
00750 
00751 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
00752   // There can never be multiple globals with the same name of different types,
00753   // because intrinsics must be a specific type.
00754   return
00755     cast<Function>(M->getOrInsertFunction(getName(id, Tys),
00756                                           getType(M->getContext(), id, Tys)));
00757 }
00758 
00759 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
00760 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00761 #include "llvm/IR/Intrinsics.gen"
00762 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
00763 
00764 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
00765 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
00766 #include "llvm/IR/Intrinsics.gen"
00767 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
00768 
00769 /// hasAddressTaken - returns true if there are any uses of this function
00770 /// other than direct calls or invokes to it.
00771 bool Function::hasAddressTaken(const User* *PutOffender) const {
00772   for (const Use &U : uses()) {
00773     const User *FU = U.getUser();
00774     if (isa<BlockAddress>(FU))
00775       continue;
00776     if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
00777       return PutOffender ? (*PutOffender = FU, true) : true;
00778     ImmutableCallSite CS(cast<Instruction>(FU));
00779     if (!CS.isCallee(&U))
00780       return PutOffender ? (*PutOffender = FU, true) : true;
00781   }
00782   return false;
00783 }
00784 
00785 bool Function::isDefTriviallyDead() const {
00786   // Check the linkage
00787   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
00788       !hasAvailableExternallyLinkage())
00789     return false;
00790 
00791   // Check if the function is used by anything other than a blockaddress.
00792   for (const User *U : users())
00793     if (!isa<BlockAddress>(U))
00794       return false;
00795 
00796   return true;
00797 }
00798 
00799 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
00800 /// setjmp or other function that gcc recognizes as "returning twice".
00801 bool Function::callsFunctionThatReturnsTwice() const {
00802   for (const_inst_iterator
00803          I = inst_begin(this), E = inst_end(this); I != E; ++I) {
00804     ImmutableCallSite CS(&*I);
00805     if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
00806       return true;
00807   }
00808 
00809   return false;
00810 }
00811 
00812 Constant *Function::getPrefixData() const {
00813   assert(hasPrefixData());
00814   const LLVMContextImpl::PrefixDataMapTy &PDMap =
00815       getContext().pImpl->PrefixDataMap;
00816   assert(PDMap.find(this) != PDMap.end());
00817   return cast<Constant>(PDMap.find(this)->second->getReturnValue());
00818 }
00819 
00820 void Function::setPrefixData(Constant *PrefixData) {
00821   if (!PrefixData && !hasPrefixData())
00822     return;
00823 
00824   unsigned SCData = getSubclassDataFromValue();
00825   LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
00826   ReturnInst *&PDHolder = PDMap[this];
00827   if (PrefixData) {
00828     if (PDHolder)
00829       PDHolder->setOperand(0, PrefixData);
00830     else
00831       PDHolder = ReturnInst::Create(getContext(), PrefixData);
00832     SCData |= 2;
00833   } else {
00834     delete PDHolder;
00835     PDMap.erase(this);
00836     SCData &= ~2;
00837   }
00838   setValueSubclassData(SCData);
00839 }