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