LLVM API Documentation

BitcodeReader.cpp
Go to the documentation of this file.
00001 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
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 #include "llvm/Bitcode/ReaderWriter.h"
00011 #include "BitcodeReader.h"
00012 #include "llvm/ADT/STLExtras.h"
00013 #include "llvm/ADT/SmallString.h"
00014 #include "llvm/ADT/SmallVector.h"
00015 #include "llvm/ADT/Triple.h"
00016 #include "llvm/Bitcode/LLVMBitCodes.h"
00017 #include "llvm/IR/AutoUpgrade.h"
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DebugInfoMetadata.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/DiagnosticPrinter.h"
00022 #include "llvm/IR/InlineAsm.h"
00023 #include "llvm/IR/IntrinsicInst.h"
00024 #include "llvm/IR/LLVMContext.h"
00025 #include "llvm/IR/Module.h"
00026 #include "llvm/IR/OperandTraits.h"
00027 #include "llvm/IR/Operator.h"
00028 #include "llvm/Support/DataStream.h"
00029 #include "llvm/Support/ManagedStatic.h"
00030 #include "llvm/Support/MathExtras.h"
00031 #include "llvm/Support/MemoryBuffer.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 
00034 using namespace llvm;
00035 
00036 enum {
00037   SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
00038 };
00039 
00040 BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
00041                                              DiagnosticSeverity Severity,
00042                                              const Twine &Msg)
00043     : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
00044 
00045 void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
00046 
00047 static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
00048                              std::error_code EC, const Twine &Message) {
00049   BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
00050   DiagnosticHandler(DI);
00051   return EC;
00052 }
00053 
00054 static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
00055                              std::error_code EC) {
00056   return Error(DiagnosticHandler, EC, EC.message());
00057 }
00058 
00059 std::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) {
00060   return ::Error(DiagnosticHandler, make_error_code(E), Message);
00061 }
00062 
00063 std::error_code BitcodeReader::Error(const Twine &Message) {
00064   return ::Error(DiagnosticHandler,
00065                  make_error_code(BitcodeError::CorruptedBitcode), Message);
00066 }
00067 
00068 std::error_code BitcodeReader::Error(BitcodeError E) {
00069   return ::Error(DiagnosticHandler, make_error_code(E));
00070 }
00071 
00072 static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
00073                                                 LLVMContext &C) {
00074   if (F)
00075     return F;
00076   return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
00077 }
00078 
00079 BitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
00080                              DiagnosticHandlerFunction DiagnosticHandler)
00081     : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
00082       TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
00083       NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
00084       MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
00085       WillMaterializeAllForwardRefs(false) {}
00086 
00087 BitcodeReader::BitcodeReader(DataStreamer *streamer, LLVMContext &C,
00088                              DiagnosticHandlerFunction DiagnosticHandler)
00089     : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
00090       TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer),
00091       NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
00092       MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
00093       WillMaterializeAllForwardRefs(false) {}
00094 
00095 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
00096   if (WillMaterializeAllForwardRefs)
00097     return std::error_code();
00098 
00099   // Prevent recursion.
00100   WillMaterializeAllForwardRefs = true;
00101 
00102   while (!BasicBlockFwdRefQueue.empty()) {
00103     Function *F = BasicBlockFwdRefQueue.front();
00104     BasicBlockFwdRefQueue.pop_front();
00105     assert(F && "Expected valid function");
00106     if (!BasicBlockFwdRefs.count(F))
00107       // Already materialized.
00108       continue;
00109 
00110     // Check for a function that isn't materializable to prevent an infinite
00111     // loop.  When parsing a blockaddress stored in a global variable, there
00112     // isn't a trivial way to check if a function will have a body without a
00113     // linear search through FunctionsWithBodies, so just check it here.
00114     if (!F->isMaterializable())
00115       return Error("Never resolved function from blockaddress");
00116 
00117     // Try to materialize F.
00118     if (std::error_code EC = materialize(F))
00119       return EC;
00120   }
00121   assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
00122 
00123   // Reset state.
00124   WillMaterializeAllForwardRefs = false;
00125   return std::error_code();
00126 }
00127 
00128 void BitcodeReader::FreeState() {
00129   Buffer = nullptr;
00130   std::vector<Type*>().swap(TypeList);
00131   ValueList.clear();
00132   MDValueList.clear();
00133   std::vector<Comdat *>().swap(ComdatList);
00134 
00135   std::vector<AttributeSet>().swap(MAttributes);
00136   std::vector<BasicBlock*>().swap(FunctionBBs);
00137   std::vector<Function*>().swap(FunctionsWithBodies);
00138   DeferredFunctionInfo.clear();
00139   MDKindMap.clear();
00140 
00141   assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
00142   BasicBlockFwdRefQueue.clear();
00143 }
00144 
00145 //===----------------------------------------------------------------------===//
00146 //  Helper functions to implement forward reference resolution, etc.
00147 //===----------------------------------------------------------------------===//
00148 
00149 /// ConvertToString - Convert a string from a record into an std::string, return
00150 /// true on failure.
00151 template<typename StrTy>
00152 static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
00153                             StrTy &Result) {
00154   if (Idx > Record.size())
00155     return true;
00156 
00157   for (unsigned i = Idx, e = Record.size(); i != e; ++i)
00158     Result += (char)Record[i];
00159   return false;
00160 }
00161 
00162 static bool hasImplicitComdat(size_t Val) {
00163   switch (Val) {
00164   default:
00165     return false;
00166   case 1:  // Old WeakAnyLinkage
00167   case 4:  // Old LinkOnceAnyLinkage
00168   case 10: // Old WeakODRLinkage
00169   case 11: // Old LinkOnceODRLinkage
00170     return true;
00171   }
00172 }
00173 
00174 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
00175   switch (Val) {
00176   default: // Map unknown/new linkages to external
00177   case 0:
00178     return GlobalValue::ExternalLinkage;
00179   case 2:
00180     return GlobalValue::AppendingLinkage;
00181   case 3:
00182     return GlobalValue::InternalLinkage;
00183   case 5:
00184     return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
00185   case 6:
00186     return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
00187   case 7:
00188     return GlobalValue::ExternalWeakLinkage;
00189   case 8:
00190     return GlobalValue::CommonLinkage;
00191   case 9:
00192     return GlobalValue::PrivateLinkage;
00193   case 12:
00194     return GlobalValue::AvailableExternallyLinkage;
00195   case 13:
00196     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
00197   case 14:
00198     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
00199   case 15:
00200     return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
00201   case 1: // Old value with implicit comdat.
00202   case 16:
00203     return GlobalValue::WeakAnyLinkage;
00204   case 10: // Old value with implicit comdat.
00205   case 17:
00206     return GlobalValue::WeakODRLinkage;
00207   case 4: // Old value with implicit comdat.
00208   case 18:
00209     return GlobalValue::LinkOnceAnyLinkage;
00210   case 11: // Old value with implicit comdat.
00211   case 19:
00212     return GlobalValue::LinkOnceODRLinkage;
00213   }
00214 }
00215 
00216 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
00217   switch (Val) {
00218   default: // Map unknown visibilities to default.
00219   case 0: return GlobalValue::DefaultVisibility;
00220   case 1: return GlobalValue::HiddenVisibility;
00221   case 2: return GlobalValue::ProtectedVisibility;
00222   }
00223 }
00224 
00225 static GlobalValue::DLLStorageClassTypes
00226 GetDecodedDLLStorageClass(unsigned Val) {
00227   switch (Val) {
00228   default: // Map unknown values to default.
00229   case 0: return GlobalValue::DefaultStorageClass;
00230   case 1: return GlobalValue::DLLImportStorageClass;
00231   case 2: return GlobalValue::DLLExportStorageClass;
00232   }
00233 }
00234 
00235 static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
00236   switch (Val) {
00237     case 0: return GlobalVariable::NotThreadLocal;
00238     default: // Map unknown non-zero value to general dynamic.
00239     case 1: return GlobalVariable::GeneralDynamicTLSModel;
00240     case 2: return GlobalVariable::LocalDynamicTLSModel;
00241     case 3: return GlobalVariable::InitialExecTLSModel;
00242     case 4: return GlobalVariable::LocalExecTLSModel;
00243   }
00244 }
00245 
00246 static int GetDecodedCastOpcode(unsigned Val) {
00247   switch (Val) {
00248   default: return -1;
00249   case bitc::CAST_TRUNC   : return Instruction::Trunc;
00250   case bitc::CAST_ZEXT    : return Instruction::ZExt;
00251   case bitc::CAST_SEXT    : return Instruction::SExt;
00252   case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
00253   case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
00254   case bitc::CAST_UITOFP  : return Instruction::UIToFP;
00255   case bitc::CAST_SITOFP  : return Instruction::SIToFP;
00256   case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
00257   case bitc::CAST_FPEXT   : return Instruction::FPExt;
00258   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
00259   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
00260   case bitc::CAST_BITCAST : return Instruction::BitCast;
00261   case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
00262   }
00263 }
00264 static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
00265   switch (Val) {
00266   default: return -1;
00267   case bitc::BINOP_ADD:
00268     return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
00269   case bitc::BINOP_SUB:
00270     return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
00271   case bitc::BINOP_MUL:
00272     return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
00273   case bitc::BINOP_UDIV: return Instruction::UDiv;
00274   case bitc::BINOP_SDIV:
00275     return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
00276   case bitc::BINOP_UREM: return Instruction::URem;
00277   case bitc::BINOP_SREM:
00278     return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
00279   case bitc::BINOP_SHL:  return Instruction::Shl;
00280   case bitc::BINOP_LSHR: return Instruction::LShr;
00281   case bitc::BINOP_ASHR: return Instruction::AShr;
00282   case bitc::BINOP_AND:  return Instruction::And;
00283   case bitc::BINOP_OR:   return Instruction::Or;
00284   case bitc::BINOP_XOR:  return Instruction::Xor;
00285   }
00286 }
00287 
00288 static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
00289   switch (Val) {
00290   default: return AtomicRMWInst::BAD_BINOP;
00291   case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
00292   case bitc::RMW_ADD: return AtomicRMWInst::Add;
00293   case bitc::RMW_SUB: return AtomicRMWInst::Sub;
00294   case bitc::RMW_AND: return AtomicRMWInst::And;
00295   case bitc::RMW_NAND: return AtomicRMWInst::Nand;
00296   case bitc::RMW_OR: return AtomicRMWInst::Or;
00297   case bitc::RMW_XOR: return AtomicRMWInst::Xor;
00298   case bitc::RMW_MAX: return AtomicRMWInst::Max;
00299   case bitc::RMW_MIN: return AtomicRMWInst::Min;
00300   case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
00301   case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
00302   }
00303 }
00304 
00305 static AtomicOrdering GetDecodedOrdering(unsigned Val) {
00306   switch (Val) {
00307   case bitc::ORDERING_NOTATOMIC: return NotAtomic;
00308   case bitc::ORDERING_UNORDERED: return Unordered;
00309   case bitc::ORDERING_MONOTONIC: return Monotonic;
00310   case bitc::ORDERING_ACQUIRE: return Acquire;
00311   case bitc::ORDERING_RELEASE: return Release;
00312   case bitc::ORDERING_ACQREL: return AcquireRelease;
00313   default: // Map unknown orderings to sequentially-consistent.
00314   case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
00315   }
00316 }
00317 
00318 static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
00319   switch (Val) {
00320   case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
00321   default: // Map unknown scopes to cross-thread.
00322   case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
00323   }
00324 }
00325 
00326 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
00327   switch (Val) {
00328   default: // Map unknown selection kinds to any.
00329   case bitc::COMDAT_SELECTION_KIND_ANY:
00330     return Comdat::Any;
00331   case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
00332     return Comdat::ExactMatch;
00333   case bitc::COMDAT_SELECTION_KIND_LARGEST:
00334     return Comdat::Largest;
00335   case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
00336     return Comdat::NoDuplicates;
00337   case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
00338     return Comdat::SameSize;
00339   }
00340 }
00341 
00342 static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
00343   switch (Val) {
00344   case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
00345   case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
00346   }
00347 }
00348 
00349 namespace llvm {
00350 namespace {
00351   /// @brief A class for maintaining the slot number definition
00352   /// as a placeholder for the actual definition for forward constants defs.
00353   class ConstantPlaceHolder : public ConstantExpr {
00354     void operator=(const ConstantPlaceHolder &) = delete;
00355   public:
00356     // allocate space for exactly one operand
00357     void *operator new(size_t s) {
00358       return User::operator new(s, 1);
00359     }
00360     explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
00361       : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
00362       Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
00363     }
00364 
00365     /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
00366     static bool classof(const Value *V) {
00367       return isa<ConstantExpr>(V) &&
00368              cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
00369     }
00370 
00371 
00372     /// Provide fast operand accessors
00373     DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00374   };
00375 }
00376 
00377 // FIXME: can we inherit this from ConstantExpr?
00378 template <>
00379 struct OperandTraits<ConstantPlaceHolder> :
00380   public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
00381 };
00382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
00383 }
00384 
00385 
00386 void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
00387   if (Idx == size()) {
00388     push_back(V);
00389     return;
00390   }
00391 
00392   if (Idx >= size())
00393     resize(Idx+1);
00394 
00395   WeakVH &OldV = ValuePtrs[Idx];
00396   if (!OldV) {
00397     OldV = V;
00398     return;
00399   }
00400 
00401   // Handle constants and non-constants (e.g. instrs) differently for
00402   // efficiency.
00403   if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
00404     ResolveConstants.push_back(std::make_pair(PHC, Idx));
00405     OldV = V;
00406   } else {
00407     // If there was a forward reference to this value, replace it.
00408     Value *PrevVal = OldV;
00409     OldV->replaceAllUsesWith(V);
00410     delete PrevVal;
00411   }
00412 }
00413 
00414 
00415 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
00416                                                     Type *Ty) {
00417   if (Idx >= size())
00418     resize(Idx + 1);
00419 
00420   if (Value *V = ValuePtrs[Idx]) {
00421     assert(Ty == V->getType() && "Type mismatch in constant table!");
00422     return cast<Constant>(V);
00423   }
00424 
00425   // Create and return a placeholder, which will later be RAUW'd.
00426   Constant *C = new ConstantPlaceHolder(Ty, Context);
00427   ValuePtrs[Idx] = C;
00428   return C;
00429 }
00430 
00431 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
00432   if (Idx >= size())
00433     resize(Idx + 1);
00434 
00435   if (Value *V = ValuePtrs[Idx]) {
00436     assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
00437     return V;
00438   }
00439 
00440   // No type specified, must be invalid reference.
00441   if (!Ty) return nullptr;
00442 
00443   // Create and return a placeholder, which will later be RAUW'd.
00444   Value *V = new Argument(Ty);
00445   ValuePtrs[Idx] = V;
00446   return V;
00447 }
00448 
00449 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
00450 /// resolves any forward references.  The idea behind this is that we sometimes
00451 /// get constants (such as large arrays) which reference *many* forward ref
00452 /// constants.  Replacing each of these causes a lot of thrashing when
00453 /// building/reuniquing the constant.  Instead of doing this, we look at all the
00454 /// uses and rewrite all the place holders at once for any constant that uses
00455 /// a placeholder.
00456 void BitcodeReaderValueList::ResolveConstantForwardRefs() {
00457   // Sort the values by-pointer so that they are efficient to look up with a
00458   // binary search.
00459   std::sort(ResolveConstants.begin(), ResolveConstants.end());
00460 
00461   SmallVector<Constant*, 64> NewOps;
00462 
00463   while (!ResolveConstants.empty()) {
00464     Value *RealVal = operator[](ResolveConstants.back().second);
00465     Constant *Placeholder = ResolveConstants.back().first;
00466     ResolveConstants.pop_back();
00467 
00468     // Loop over all users of the placeholder, updating them to reference the
00469     // new value.  If they reference more than one placeholder, update them all
00470     // at once.
00471     while (!Placeholder->use_empty()) {
00472       auto UI = Placeholder->user_begin();
00473       User *U = *UI;
00474 
00475       // If the using object isn't uniqued, just update the operands.  This
00476       // handles instructions and initializers for global variables.
00477       if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
00478         UI.getUse().set(RealVal);
00479         continue;
00480       }
00481 
00482       // Otherwise, we have a constant that uses the placeholder.  Replace that
00483       // constant with a new constant that has *all* placeholder uses updated.
00484       Constant *UserC = cast<Constant>(U);
00485       for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
00486            I != E; ++I) {
00487         Value *NewOp;
00488         if (!isa<ConstantPlaceHolder>(*I)) {
00489           // Not a placeholder reference.
00490           NewOp = *I;
00491         } else if (*I == Placeholder) {
00492           // Common case is that it just references this one placeholder.
00493           NewOp = RealVal;
00494         } else {
00495           // Otherwise, look up the placeholder in ResolveConstants.
00496           ResolveConstantsTy::iterator It =
00497             std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
00498                              std::pair<Constant*, unsigned>(cast<Constant>(*I),
00499                                                             0));
00500           assert(It != ResolveConstants.end() && It->first == *I);
00501           NewOp = operator[](It->second);
00502         }
00503 
00504         NewOps.push_back(cast<Constant>(NewOp));
00505       }
00506 
00507       // Make the new constant.
00508       Constant *NewC;
00509       if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
00510         NewC = ConstantArray::get(UserCA->getType(), NewOps);
00511       } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
00512         NewC = ConstantStruct::get(UserCS->getType(), NewOps);
00513       } else if (isa<ConstantVector>(UserC)) {
00514         NewC = ConstantVector::get(NewOps);
00515       } else {
00516         assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
00517         NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
00518       }
00519 
00520       UserC->replaceAllUsesWith(NewC);
00521       UserC->destroyConstant();
00522       NewOps.clear();
00523     }
00524 
00525     // Update all ValueHandles, they should be the only users at this point.
00526     Placeholder->replaceAllUsesWith(RealVal);
00527     delete Placeholder;
00528   }
00529 }
00530 
00531 void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
00532   if (Idx == size()) {
00533     push_back(MD);
00534     return;
00535   }
00536 
00537   if (Idx >= size())
00538     resize(Idx+1);
00539 
00540   TrackingMDRef &OldMD = MDValuePtrs[Idx];
00541   if (!OldMD) {
00542     OldMD.reset(MD);
00543     return;
00544   }
00545 
00546   // If there was a forward reference to this value, replace it.
00547   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
00548   PrevMD->replaceAllUsesWith(MD);
00549   --NumFwdRefs;
00550 }
00551 
00552 Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
00553   if (Idx >= size())
00554     resize(Idx + 1);
00555 
00556   if (Metadata *MD = MDValuePtrs[Idx])
00557     return MD;
00558 
00559   // Track forward refs to be resolved later.
00560   if (AnyFwdRefs) {
00561     MinFwdRef = std::min(MinFwdRef, Idx);
00562     MaxFwdRef = std::max(MaxFwdRef, Idx);
00563   } else {
00564     AnyFwdRefs = true;
00565     MinFwdRef = MaxFwdRef = Idx;
00566   }
00567   ++NumFwdRefs;
00568 
00569   // Create and return a placeholder, which will later be RAUW'd.
00570   Metadata *MD = MDNode::getTemporary(Context, None).release();
00571   MDValuePtrs[Idx].reset(MD);
00572   return MD;
00573 }
00574 
00575 void BitcodeReaderMDValueList::tryToResolveCycles() {
00576   if (!AnyFwdRefs)
00577     // Nothing to do.
00578     return;
00579 
00580   if (NumFwdRefs)
00581     // Still forward references... can't resolve cycles.
00582     return;
00583 
00584   // Resolve any cycles.
00585   for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
00586     auto &MD = MDValuePtrs[I];
00587     auto *N = dyn_cast_or_null<MDNode>(MD);
00588     if (!N)
00589       continue;
00590 
00591     assert(!N->isTemporary() && "Unexpected forward reference");
00592     N->resolveCycles();
00593   }
00594 
00595   // Make sure we return early again until there's another forward ref.
00596   AnyFwdRefs = false;
00597 }
00598 
00599 Type *BitcodeReader::getTypeByID(unsigned ID) {
00600   // The type table size is always specified correctly.
00601   if (ID >= TypeList.size())
00602     return nullptr;
00603 
00604   if (Type *Ty = TypeList[ID])
00605     return Ty;
00606 
00607   // If we have a forward reference, the only possible case is when it is to a
00608   // named struct.  Just create a placeholder for now.
00609   return TypeList[ID] = createIdentifiedStructType(Context);
00610 }
00611 
00612 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
00613                                                       StringRef Name) {
00614   auto *Ret = StructType::create(Context, Name);
00615   IdentifiedStructTypes.push_back(Ret);
00616   return Ret;
00617 }
00618 
00619 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
00620   auto *Ret = StructType::create(Context);
00621   IdentifiedStructTypes.push_back(Ret);
00622   return Ret;
00623 }
00624 
00625 
00626 //===----------------------------------------------------------------------===//
00627 //  Functions for parsing blocks from the bitcode file
00628 //===----------------------------------------------------------------------===//
00629 
00630 
00631 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
00632 /// been decoded from the given integer. This function must stay in sync with
00633 /// 'encodeLLVMAttributesForBitcode'.
00634 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
00635                                            uint64_t EncodedAttrs) {
00636   // FIXME: Remove in 4.0.
00637 
00638   // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
00639   // the bits above 31 down by 11 bits.
00640   unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
00641   assert((!Alignment || isPowerOf2_32(Alignment)) &&
00642          "Alignment must be a power of two.");
00643 
00644   if (Alignment)
00645     B.addAlignmentAttr(Alignment);
00646   B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
00647                 (EncodedAttrs & 0xffff));
00648 }
00649 
00650 std::error_code BitcodeReader::ParseAttributeBlock() {
00651   if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
00652     return Error("Invalid record");
00653 
00654   if (!MAttributes.empty())
00655     return Error("Invalid multiple blocks");
00656 
00657   SmallVector<uint64_t, 64> Record;
00658 
00659   SmallVector<AttributeSet, 8> Attrs;
00660 
00661   // Read all the records.
00662   while (1) {
00663     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
00664 
00665     switch (Entry.Kind) {
00666     case BitstreamEntry::SubBlock: // Handled for us already.
00667     case BitstreamEntry::Error:
00668       return Error("Malformed block");
00669     case BitstreamEntry::EndBlock:
00670       return std::error_code();
00671     case BitstreamEntry::Record:
00672       // The interesting case.
00673       break;
00674     }
00675 
00676     // Read a record.
00677     Record.clear();
00678     switch (Stream.readRecord(Entry.ID, Record)) {
00679     default:  // Default behavior: ignore.
00680       break;
00681     case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
00682       // FIXME: Remove in 4.0.
00683       if (Record.size() & 1)
00684         return Error("Invalid record");
00685 
00686       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
00687         AttrBuilder B;
00688         decodeLLVMAttributesForBitcode(B, Record[i+1]);
00689         Attrs.push_back(AttributeSet::get(Context, Record[i], B));
00690       }
00691 
00692       MAttributes.push_back(AttributeSet::get(Context, Attrs));
00693       Attrs.clear();
00694       break;
00695     }
00696     case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
00697       for (unsigned i = 0, e = Record.size(); i != e; ++i)
00698         Attrs.push_back(MAttributeGroups[Record[i]]);
00699 
00700       MAttributes.push_back(AttributeSet::get(Context, Attrs));
00701       Attrs.clear();
00702       break;
00703     }
00704     }
00705   }
00706 }
00707 
00708 // Returns Attribute::None on unrecognized codes.
00709 static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
00710   switch (Code) {
00711   default:
00712     return Attribute::None;
00713   case bitc::ATTR_KIND_ALIGNMENT:
00714     return Attribute::Alignment;
00715   case bitc::ATTR_KIND_ALWAYS_INLINE:
00716     return Attribute::AlwaysInline;
00717   case bitc::ATTR_KIND_BUILTIN:
00718     return Attribute::Builtin;
00719   case bitc::ATTR_KIND_BY_VAL:
00720     return Attribute::ByVal;
00721   case bitc::ATTR_KIND_IN_ALLOCA:
00722     return Attribute::InAlloca;
00723   case bitc::ATTR_KIND_COLD:
00724     return Attribute::Cold;
00725   case bitc::ATTR_KIND_INLINE_HINT:
00726     return Attribute::InlineHint;
00727   case bitc::ATTR_KIND_IN_REG:
00728     return Attribute::InReg;
00729   case bitc::ATTR_KIND_JUMP_TABLE:
00730     return Attribute::JumpTable;
00731   case bitc::ATTR_KIND_MIN_SIZE:
00732     return Attribute::MinSize;
00733   case bitc::ATTR_KIND_NAKED:
00734     return Attribute::Naked;
00735   case bitc::ATTR_KIND_NEST:
00736     return Attribute::Nest;
00737   case bitc::ATTR_KIND_NO_ALIAS:
00738     return Attribute::NoAlias;
00739   case bitc::ATTR_KIND_NO_BUILTIN:
00740     return Attribute::NoBuiltin;
00741   case bitc::ATTR_KIND_NO_CAPTURE:
00742     return Attribute::NoCapture;
00743   case bitc::ATTR_KIND_NO_DUPLICATE:
00744     return Attribute::NoDuplicate;
00745   case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
00746     return Attribute::NoImplicitFloat;
00747   case bitc::ATTR_KIND_NO_INLINE:
00748     return Attribute::NoInline;
00749   case bitc::ATTR_KIND_NON_LAZY_BIND:
00750     return Attribute::NonLazyBind;
00751   case bitc::ATTR_KIND_NON_NULL:
00752     return Attribute::NonNull;
00753   case bitc::ATTR_KIND_DEREFERENCEABLE:
00754     return Attribute::Dereferenceable;
00755   case bitc::ATTR_KIND_NO_RED_ZONE:
00756     return Attribute::NoRedZone;
00757   case bitc::ATTR_KIND_NO_RETURN:
00758     return Attribute::NoReturn;
00759   case bitc::ATTR_KIND_NO_UNWIND:
00760     return Attribute::NoUnwind;
00761   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
00762     return Attribute::OptimizeForSize;
00763   case bitc::ATTR_KIND_OPTIMIZE_NONE:
00764     return Attribute::OptimizeNone;
00765   case bitc::ATTR_KIND_READ_NONE:
00766     return Attribute::ReadNone;
00767   case bitc::ATTR_KIND_READ_ONLY:
00768     return Attribute::ReadOnly;
00769   case bitc::ATTR_KIND_RETURNED:
00770     return Attribute::Returned;
00771   case bitc::ATTR_KIND_RETURNS_TWICE:
00772     return Attribute::ReturnsTwice;
00773   case bitc::ATTR_KIND_S_EXT:
00774     return Attribute::SExt;
00775   case bitc::ATTR_KIND_STACK_ALIGNMENT:
00776     return Attribute::StackAlignment;
00777   case bitc::ATTR_KIND_STACK_PROTECT:
00778     return Attribute::StackProtect;
00779   case bitc::ATTR_KIND_STACK_PROTECT_REQ:
00780     return Attribute::StackProtectReq;
00781   case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
00782     return Attribute::StackProtectStrong;
00783   case bitc::ATTR_KIND_STRUCT_RET:
00784     return Attribute::StructRet;
00785   case bitc::ATTR_KIND_SANITIZE_ADDRESS:
00786     return Attribute::SanitizeAddress;
00787   case bitc::ATTR_KIND_SANITIZE_THREAD:
00788     return Attribute::SanitizeThread;
00789   case bitc::ATTR_KIND_SANITIZE_MEMORY:
00790     return Attribute::SanitizeMemory;
00791   case bitc::ATTR_KIND_UW_TABLE:
00792     return Attribute::UWTable;
00793   case bitc::ATTR_KIND_Z_EXT:
00794     return Attribute::ZExt;
00795   }
00796 }
00797 
00798 std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
00799                                                    unsigned &Alignment) {
00800   // Note: Alignment in bitcode files is incremented by 1, so that zero
00801   // can be used for default alignment.
00802   if (Exponent > Value::MaxAlignmentExponent + 1)
00803     return Error("Invalid alignment value");
00804   Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
00805   return std::error_code();
00806 }
00807 
00808 std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
00809                                              Attribute::AttrKind *Kind) {
00810   *Kind = GetAttrFromCode(Code);
00811   if (*Kind == Attribute::None)
00812     return Error(BitcodeError::CorruptedBitcode,
00813                  "Unknown attribute kind (" + Twine(Code) + ")");
00814   return std::error_code();
00815 }
00816 
00817 std::error_code BitcodeReader::ParseAttributeGroupBlock() {
00818   if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
00819     return Error("Invalid record");
00820 
00821   if (!MAttributeGroups.empty())
00822     return Error("Invalid multiple blocks");
00823 
00824   SmallVector<uint64_t, 64> Record;
00825 
00826   // Read all the records.
00827   while (1) {
00828     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
00829 
00830     switch (Entry.Kind) {
00831     case BitstreamEntry::SubBlock: // Handled for us already.
00832     case BitstreamEntry::Error:
00833       return Error("Malformed block");
00834     case BitstreamEntry::EndBlock:
00835       return std::error_code();
00836     case BitstreamEntry::Record:
00837       // The interesting case.
00838       break;
00839     }
00840 
00841     // Read a record.
00842     Record.clear();
00843     switch (Stream.readRecord(Entry.ID, Record)) {
00844     default:  // Default behavior: ignore.
00845       break;
00846     case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
00847       if (Record.size() < 3)
00848         return Error("Invalid record");
00849 
00850       uint64_t GrpID = Record[0];
00851       uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
00852 
00853       AttrBuilder B;
00854       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
00855         if (Record[i] == 0) {        // Enum attribute
00856           Attribute::AttrKind Kind;
00857           if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
00858             return EC;
00859 
00860           B.addAttribute(Kind);
00861         } else if (Record[i] == 1) { // Integer attribute
00862           Attribute::AttrKind Kind;
00863           if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
00864             return EC;
00865           if (Kind == Attribute::Alignment)
00866             B.addAlignmentAttr(Record[++i]);
00867           else if (Kind == Attribute::StackAlignment)
00868             B.addStackAlignmentAttr(Record[++i]);
00869           else if (Kind == Attribute::Dereferenceable)
00870             B.addDereferenceableAttr(Record[++i]);
00871         } else {                     // String attribute
00872           assert((Record[i] == 3 || Record[i] == 4) &&
00873                  "Invalid attribute group entry");
00874           bool HasValue = (Record[i++] == 4);
00875           SmallString<64> KindStr;
00876           SmallString<64> ValStr;
00877 
00878           while (Record[i] != 0 && i != e)
00879             KindStr += Record[i++];
00880           assert(Record[i] == 0 && "Kind string not null terminated");
00881 
00882           if (HasValue) {
00883             // Has a value associated with it.
00884             ++i; // Skip the '0' that terminates the "kind" string.
00885             while (Record[i] != 0 && i != e)
00886               ValStr += Record[i++];
00887             assert(Record[i] == 0 && "Value string not null terminated");
00888           }
00889 
00890           B.addAttribute(KindStr.str(), ValStr.str());
00891         }
00892       }
00893 
00894       MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
00895       break;
00896     }
00897     }
00898   }
00899 }
00900 
00901 std::error_code BitcodeReader::ParseTypeTable() {
00902   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
00903     return Error("Invalid record");
00904 
00905   return ParseTypeTableBody();
00906 }
00907 
00908 std::error_code BitcodeReader::ParseTypeTableBody() {
00909   if (!TypeList.empty())
00910     return Error("Invalid multiple blocks");
00911 
00912   SmallVector<uint64_t, 64> Record;
00913   unsigned NumRecords = 0;
00914 
00915   SmallString<64> TypeName;
00916 
00917   // Read all the records for this type table.
00918   while (1) {
00919     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
00920 
00921     switch (Entry.Kind) {
00922     case BitstreamEntry::SubBlock: // Handled for us already.
00923     case BitstreamEntry::Error:
00924       return Error("Malformed block");
00925     case BitstreamEntry::EndBlock:
00926       if (NumRecords != TypeList.size())
00927         return Error("Malformed block");
00928       return std::error_code();
00929     case BitstreamEntry::Record:
00930       // The interesting case.
00931       break;
00932     }
00933 
00934     // Read a record.
00935     Record.clear();
00936     Type *ResultTy = nullptr;
00937     switch (Stream.readRecord(Entry.ID, Record)) {
00938     default:
00939       return Error("Invalid value");
00940     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
00941       // TYPE_CODE_NUMENTRY contains a count of the number of types in the
00942       // type list.  This allows us to reserve space.
00943       if (Record.size() < 1)
00944         return Error("Invalid record");
00945       TypeList.resize(Record[0]);
00946       continue;
00947     case bitc::TYPE_CODE_VOID:      // VOID
00948       ResultTy = Type::getVoidTy(Context);
00949       break;
00950     case bitc::TYPE_CODE_HALF:     // HALF
00951       ResultTy = Type::getHalfTy(Context);
00952       break;
00953     case bitc::TYPE_CODE_FLOAT:     // FLOAT
00954       ResultTy = Type::getFloatTy(Context);
00955       break;
00956     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
00957       ResultTy = Type::getDoubleTy(Context);
00958       break;
00959     case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
00960       ResultTy = Type::getX86_FP80Ty(Context);
00961       break;
00962     case bitc::TYPE_CODE_FP128:     // FP128
00963       ResultTy = Type::getFP128Ty(Context);
00964       break;
00965     case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
00966       ResultTy = Type::getPPC_FP128Ty(Context);
00967       break;
00968     case bitc::TYPE_CODE_LABEL:     // LABEL
00969       ResultTy = Type::getLabelTy(Context);
00970       break;
00971     case bitc::TYPE_CODE_METADATA:  // METADATA
00972       ResultTy = Type::getMetadataTy(Context);
00973       break;
00974     case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
00975       ResultTy = Type::getX86_MMXTy(Context);
00976       break;
00977     case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
00978       if (Record.size() < 1)
00979         return Error("Invalid record");
00980 
00981       uint64_t NumBits = Record[0];
00982       if (NumBits < IntegerType::MIN_INT_BITS ||
00983           NumBits > IntegerType::MAX_INT_BITS)
00984         return Error("Bitwidth for integer type out of range");
00985       ResultTy = IntegerType::get(Context, NumBits);
00986       break;
00987     }
00988     case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
00989                                     //          [pointee type, address space]
00990       if (Record.size() < 1)
00991         return Error("Invalid record");
00992       unsigned AddressSpace = 0;
00993       if (Record.size() == 2)
00994         AddressSpace = Record[1];
00995       ResultTy = getTypeByID(Record[0]);
00996       if (!ResultTy)
00997         return Error("Invalid type");
00998       ResultTy = PointerType::get(ResultTy, AddressSpace);
00999       break;
01000     }
01001     case bitc::TYPE_CODE_FUNCTION_OLD: {
01002       // FIXME: attrid is dead, remove it in LLVM 4.0
01003       // FUNCTION: [vararg, attrid, retty, paramty x N]
01004       if (Record.size() < 3)
01005         return Error("Invalid record");
01006       SmallVector<Type*, 8> ArgTys;
01007       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
01008         if (Type *T = getTypeByID(Record[i]))
01009           ArgTys.push_back(T);
01010         else
01011           break;
01012       }
01013 
01014       ResultTy = getTypeByID(Record[2]);
01015       if (!ResultTy || ArgTys.size() < Record.size()-3)
01016         return Error("Invalid type");
01017 
01018       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
01019       break;
01020     }
01021     case bitc::TYPE_CODE_FUNCTION: {
01022       // FUNCTION: [vararg, retty, paramty x N]
01023       if (Record.size() < 2)
01024         return Error("Invalid record");
01025       SmallVector<Type*, 8> ArgTys;
01026       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
01027         if (Type *T = getTypeByID(Record[i]))
01028           ArgTys.push_back(T);
01029         else
01030           break;
01031       }
01032 
01033       ResultTy = getTypeByID(Record[1]);
01034       if (!ResultTy || ArgTys.size() < Record.size()-2)
01035         return Error("Invalid type");
01036 
01037       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
01038       break;
01039     }
01040     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
01041       if (Record.size() < 1)
01042         return Error("Invalid record");
01043       SmallVector<Type*, 8> EltTys;
01044       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
01045         if (Type *T = getTypeByID(Record[i]))
01046           EltTys.push_back(T);
01047         else
01048           break;
01049       }
01050       if (EltTys.size() != Record.size()-1)
01051         return Error("Invalid type");
01052       ResultTy = StructType::get(Context, EltTys, Record[0]);
01053       break;
01054     }
01055     case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
01056       if (ConvertToString(Record, 0, TypeName))
01057         return Error("Invalid record");
01058       continue;
01059 
01060     case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
01061       if (Record.size() < 1)
01062         return Error("Invalid record");
01063 
01064       if (NumRecords >= TypeList.size())
01065         return Error("Invalid TYPE table");
01066 
01067       // Check to see if this was forward referenced, if so fill in the temp.
01068       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
01069       if (Res) {
01070         Res->setName(TypeName);
01071         TypeList[NumRecords] = nullptr;
01072       } else  // Otherwise, create a new struct.
01073         Res = createIdentifiedStructType(Context, TypeName);
01074       TypeName.clear();
01075 
01076       SmallVector<Type*, 8> EltTys;
01077       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
01078         if (Type *T = getTypeByID(Record[i]))
01079           EltTys.push_back(T);
01080         else
01081           break;
01082       }
01083       if (EltTys.size() != Record.size()-1)
01084         return Error("Invalid record");
01085       Res->setBody(EltTys, Record[0]);
01086       ResultTy = Res;
01087       break;
01088     }
01089     case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
01090       if (Record.size() != 1)
01091         return Error("Invalid record");
01092 
01093       if (NumRecords >= TypeList.size())
01094         return Error("Invalid TYPE table");
01095 
01096       // Check to see if this was forward referenced, if so fill in the temp.
01097       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
01098       if (Res) {
01099         Res->setName(TypeName);
01100         TypeList[NumRecords] = nullptr;
01101       } else  // Otherwise, create a new struct with no body.
01102         Res = createIdentifiedStructType(Context, TypeName);
01103       TypeName.clear();
01104       ResultTy = Res;
01105       break;
01106     }
01107     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
01108       if (Record.size() < 2)
01109         return Error("Invalid record");
01110       if ((ResultTy = getTypeByID(Record[1])))
01111         ResultTy = ArrayType::get(ResultTy, Record[0]);
01112       else
01113         return Error("Invalid type");
01114       break;
01115     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
01116       if (Record.size() < 2)
01117         return Error("Invalid record");
01118       if ((ResultTy = getTypeByID(Record[1])))
01119         ResultTy = VectorType::get(ResultTy, Record[0]);
01120       else
01121         return Error("Invalid type");
01122       break;
01123     }
01124 
01125     if (NumRecords >= TypeList.size())
01126       return Error("Invalid TYPE table");
01127     if (TypeList[NumRecords])
01128       return Error(
01129           "Invalid TYPE table: Only named structs can be forward referenced");
01130     assert(ResultTy && "Didn't read a type?");
01131     TypeList[NumRecords++] = ResultTy;
01132   }
01133 }
01134 
01135 std::error_code BitcodeReader::ParseValueSymbolTable() {
01136   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
01137     return Error("Invalid record");
01138 
01139   SmallVector<uint64_t, 64> Record;
01140 
01141   Triple TT(TheModule->getTargetTriple());
01142 
01143   // Read all the records for this value table.
01144   SmallString<128> ValueName;
01145   while (1) {
01146     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01147 
01148     switch (Entry.Kind) {
01149     case BitstreamEntry::SubBlock: // Handled for us already.
01150     case BitstreamEntry::Error:
01151       return Error("Malformed block");
01152     case BitstreamEntry::EndBlock:
01153       return std::error_code();
01154     case BitstreamEntry::Record:
01155       // The interesting case.
01156       break;
01157     }
01158 
01159     // Read a record.
01160     Record.clear();
01161     switch (Stream.readRecord(Entry.ID, Record)) {
01162     default:  // Default behavior: unknown type.
01163       break;
01164     case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N]
01165       if (ConvertToString(Record, 1, ValueName))
01166         return Error("Invalid record");
01167       unsigned ValueID = Record[0];
01168       if (ValueID >= ValueList.size() || !ValueList[ValueID])
01169         return Error("Invalid record");
01170       Value *V = ValueList[ValueID];
01171 
01172       V->setName(StringRef(ValueName.data(), ValueName.size()));
01173       if (auto *GO = dyn_cast<GlobalObject>(V)) {
01174         if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
01175           if (TT.isOSBinFormatMachO())
01176             GO->setComdat(nullptr);
01177           else
01178             GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
01179         }
01180       }
01181       ValueName.clear();
01182       break;
01183     }
01184     case bitc::VST_CODE_BBENTRY: {
01185       if (ConvertToString(Record, 1, ValueName))
01186         return Error("Invalid record");
01187       BasicBlock *BB = getBasicBlock(Record[0]);
01188       if (!BB)
01189         return Error("Invalid record");
01190 
01191       BB->setName(StringRef(ValueName.data(), ValueName.size()));
01192       ValueName.clear();
01193       break;
01194     }
01195     }
01196   }
01197 }
01198 
01199 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
01200 
01201 std::error_code BitcodeReader::ParseMetadata() {
01202   unsigned NextMDValueNo = MDValueList.size();
01203 
01204   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
01205     return Error("Invalid record");
01206 
01207   SmallVector<uint64_t, 64> Record;
01208 
01209   auto getMD =
01210       [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); };
01211   auto getMDOrNull = [&](unsigned ID) -> Metadata *{
01212     if (ID)
01213       return getMD(ID - 1);
01214     return nullptr;
01215   };
01216   auto getMDString = [&](unsigned ID) -> MDString *{
01217     // This requires that the ID is not really a forward reference.  In
01218     // particular, the MDString must already have been resolved.
01219     return cast_or_null<MDString>(getMDOrNull(ID));
01220   };
01221 
01222 #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS)                                 \
01223   (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
01224 
01225   // Read all the records.
01226   while (1) {
01227     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01228 
01229     switch (Entry.Kind) {
01230     case BitstreamEntry::SubBlock: // Handled for us already.
01231     case BitstreamEntry::Error:
01232       return Error("Malformed block");
01233     case BitstreamEntry::EndBlock:
01234       MDValueList.tryToResolveCycles();
01235       return std::error_code();
01236     case BitstreamEntry::Record:
01237       // The interesting case.
01238       break;
01239     }
01240 
01241     // Read a record.
01242     Record.clear();
01243     unsigned Code = Stream.readRecord(Entry.ID, Record);
01244     bool IsDistinct = false;
01245     switch (Code) {
01246     default:  // Default behavior: ignore.
01247       break;
01248     case bitc::METADATA_NAME: {
01249       // Read name of the named metadata.
01250       SmallString<8> Name(Record.begin(), Record.end());
01251       Record.clear();
01252       Code = Stream.ReadCode();
01253 
01254       // METADATA_NAME is always followed by METADATA_NAMED_NODE.
01255       unsigned NextBitCode = Stream.readRecord(Code, Record);
01256       assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
01257 
01258       // Read named metadata elements.
01259       unsigned Size = Record.size();
01260       NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
01261       for (unsigned i = 0; i != Size; ++i) {
01262         MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
01263         if (!MD)
01264           return Error("Invalid record");
01265         NMD->addOperand(MD);
01266       }
01267       break;
01268     }
01269     case bitc::METADATA_OLD_FN_NODE: {
01270       // FIXME: Remove in 4.0.
01271       // This is a LocalAsMetadata record, the only type of function-local
01272       // metadata.
01273       if (Record.size() % 2 == 1)
01274         return Error("Invalid record");
01275 
01276       // If this isn't a LocalAsMetadata record, we're dropping it.  This used
01277       // to be legal, but there's no upgrade path.
01278       auto dropRecord = [&] {
01279         MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++);
01280       };
01281       if (Record.size() != 2) {
01282         dropRecord();
01283         break;
01284       }
01285 
01286       Type *Ty = getTypeByID(Record[0]);
01287       if (Ty->isMetadataTy() || Ty->isVoidTy()) {
01288         dropRecord();
01289         break;
01290       }
01291 
01292       MDValueList.AssignValue(
01293           LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
01294           NextMDValueNo++);
01295       break;
01296     }
01297     case bitc::METADATA_OLD_NODE: {
01298       // FIXME: Remove in 4.0.
01299       if (Record.size() % 2 == 1)
01300         return Error("Invalid record");
01301 
01302       unsigned Size = Record.size();
01303       SmallVector<Metadata *, 8> Elts;
01304       for (unsigned i = 0; i != Size; i += 2) {
01305         Type *Ty = getTypeByID(Record[i]);
01306         if (!Ty)
01307           return Error("Invalid record");
01308         if (Ty->isMetadataTy())
01309           Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
01310         else if (!Ty->isVoidTy()) {
01311           auto *MD =
01312               ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
01313           assert(isa<ConstantAsMetadata>(MD) &&
01314                  "Expected non-function-local metadata");
01315           Elts.push_back(MD);
01316         } else
01317           Elts.push_back(nullptr);
01318       }
01319       MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
01320       break;
01321     }
01322     case bitc::METADATA_VALUE: {
01323       if (Record.size() != 2)
01324         return Error("Invalid record");
01325 
01326       Type *Ty = getTypeByID(Record[0]);
01327       if (Ty->isMetadataTy() || Ty->isVoidTy())
01328         return Error("Invalid record");
01329 
01330       MDValueList.AssignValue(
01331           ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
01332           NextMDValueNo++);
01333       break;
01334     }
01335     case bitc::METADATA_DISTINCT_NODE:
01336       IsDistinct = true;
01337       // fallthrough...
01338     case bitc::METADATA_NODE: {
01339       SmallVector<Metadata *, 8> Elts;
01340       Elts.reserve(Record.size());
01341       for (unsigned ID : Record)
01342         Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
01343       MDValueList.AssignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
01344                                          : MDNode::get(Context, Elts),
01345                               NextMDValueNo++);
01346       break;
01347     }
01348     case bitc::METADATA_LOCATION: {
01349       if (Record.size() != 5)
01350         return Error("Invalid record");
01351 
01352       auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get;
01353       unsigned Line = Record[1];
01354       unsigned Column = Record[2];
01355       MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
01356       Metadata *InlinedAt =
01357           Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
01358       MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt),
01359                               NextMDValueNo++);
01360       break;
01361     }
01362     case bitc::METADATA_GENERIC_DEBUG: {
01363       if (Record.size() < 4)
01364         return Error("Invalid record");
01365 
01366       unsigned Tag = Record[1];
01367       unsigned Version = Record[2];
01368 
01369       if (Tag >= 1u << 16 || Version != 0)
01370         return Error("Invalid record");
01371 
01372       auto *Header = getMDString(Record[3]);
01373       SmallVector<Metadata *, 8> DwarfOps;
01374       for (unsigned I = 4, E = Record.size(); I != E; ++I)
01375         DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1)
01376                                      : nullptr);
01377       MDValueList.AssignValue(GET_OR_DISTINCT(GenericDebugNode, Record[0],
01378                                               (Context, Tag, Header, DwarfOps)),
01379                               NextMDValueNo++);
01380       break;
01381     }
01382     case bitc::METADATA_SUBRANGE: {
01383       if (Record.size() != 3)
01384         return Error("Invalid record");
01385 
01386       MDValueList.AssignValue(
01387           GET_OR_DISTINCT(MDSubrange, Record[0],
01388                           (Context, Record[1], unrotateSign(Record[2]))),
01389           NextMDValueNo++);
01390       break;
01391     }
01392     case bitc::METADATA_ENUMERATOR: {
01393       if (Record.size() != 3)
01394         return Error("Invalid record");
01395 
01396       MDValueList.AssignValue(GET_OR_DISTINCT(MDEnumerator, Record[0],
01397                                               (Context, unrotateSign(Record[1]),
01398                                                getMDString(Record[2]))),
01399                               NextMDValueNo++);
01400       break;
01401     }
01402     case bitc::METADATA_BASIC_TYPE: {
01403       if (Record.size() != 6)
01404         return Error("Invalid record");
01405 
01406       MDValueList.AssignValue(
01407           GET_OR_DISTINCT(MDBasicType, Record[0],
01408                           (Context, Record[1], getMDString(Record[2]),
01409                            Record[3], Record[4], Record[5])),
01410           NextMDValueNo++);
01411       break;
01412     }
01413     case bitc::METADATA_DERIVED_TYPE: {
01414       if (Record.size() != 12)
01415         return Error("Invalid record");
01416 
01417       MDValueList.AssignValue(
01418           GET_OR_DISTINCT(MDDerivedType, Record[0],
01419                           (Context, Record[1], getMDString(Record[2]),
01420                            getMDOrNull(Record[3]), Record[4],
01421                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
01422                            Record[7], Record[8], Record[9], Record[10],
01423                            getMDOrNull(Record[11]))),
01424           NextMDValueNo++);
01425       break;
01426     }
01427     case bitc::METADATA_COMPOSITE_TYPE: {
01428       if (Record.size() != 16)
01429         return Error("Invalid record");
01430 
01431       MDValueList.AssignValue(
01432           GET_OR_DISTINCT(MDCompositeType, Record[0],
01433                           (Context, Record[1], getMDString(Record[2]),
01434                            getMDOrNull(Record[3]), Record[4],
01435                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
01436                            Record[7], Record[8], Record[9], Record[10],
01437                            getMDOrNull(Record[11]), Record[12],
01438                            getMDOrNull(Record[13]), getMDOrNull(Record[14]),
01439                            getMDString(Record[15]))),
01440           NextMDValueNo++);
01441       break;
01442     }
01443     case bitc::METADATA_SUBROUTINE_TYPE: {
01444       if (Record.size() != 3)
01445         return Error("Invalid record");
01446 
01447       MDValueList.AssignValue(
01448           GET_OR_DISTINCT(MDSubroutineType, Record[0],
01449                           (Context, Record[1], getMDOrNull(Record[2]))),
01450           NextMDValueNo++);
01451       break;
01452     }
01453     case bitc::METADATA_FILE: {
01454       if (Record.size() != 3)
01455         return Error("Invalid record");
01456 
01457       MDValueList.AssignValue(
01458           GET_OR_DISTINCT(MDFile, Record[0], (Context, getMDString(Record[1]),
01459                                               getMDString(Record[2]))),
01460           NextMDValueNo++);
01461       break;
01462     }
01463     case bitc::METADATA_COMPILE_UNIT: {
01464       if (Record.size() != 14)
01465         return Error("Invalid record");
01466 
01467       MDValueList.AssignValue(
01468           GET_OR_DISTINCT(MDCompileUnit, Record[0],
01469                           (Context, Record[1], getMDOrNull(Record[2]),
01470                            getMDString(Record[3]), Record[4],
01471                            getMDString(Record[5]), Record[6],
01472                            getMDString(Record[7]), Record[8],
01473                            getMDOrNull(Record[9]), getMDOrNull(Record[10]),
01474                            getMDOrNull(Record[11]), getMDOrNull(Record[12]),
01475                            getMDOrNull(Record[13]))),
01476           NextMDValueNo++);
01477       break;
01478     }
01479     case bitc::METADATA_SUBPROGRAM: {
01480       if (Record.size() != 19)
01481         return Error("Invalid record");
01482 
01483       MDValueList.AssignValue(
01484           GET_OR_DISTINCT(
01485               MDSubprogram, Record[0],
01486               (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
01487                getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
01488                getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
01489                getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
01490                Record[14], getMDOrNull(Record[15]), getMDOrNull(Record[16]),
01491                getMDOrNull(Record[17]), getMDOrNull(Record[18]))),
01492           NextMDValueNo++);
01493       break;
01494     }
01495     case bitc::METADATA_LEXICAL_BLOCK: {
01496       if (Record.size() != 5)
01497         return Error("Invalid record");
01498 
01499       MDValueList.AssignValue(
01500           GET_OR_DISTINCT(MDLexicalBlock, Record[0],
01501                           (Context, getMDOrNull(Record[1]),
01502                            getMDOrNull(Record[2]), Record[3], Record[4])),
01503           NextMDValueNo++);
01504       break;
01505     }
01506     case bitc::METADATA_LEXICAL_BLOCK_FILE: {
01507       if (Record.size() != 4)
01508         return Error("Invalid record");
01509 
01510       MDValueList.AssignValue(
01511           GET_OR_DISTINCT(MDLexicalBlockFile, Record[0],
01512                           (Context, getMDOrNull(Record[1]),
01513                            getMDOrNull(Record[2]), Record[3])),
01514           NextMDValueNo++);
01515       break;
01516     }
01517     case bitc::METADATA_NAMESPACE: {
01518       if (Record.size() != 5)
01519         return Error("Invalid record");
01520 
01521       MDValueList.AssignValue(
01522           GET_OR_DISTINCT(MDNamespace, Record[0],
01523                           (Context, getMDOrNull(Record[1]),
01524                            getMDOrNull(Record[2]), getMDString(Record[3]),
01525                            Record[4])),
01526           NextMDValueNo++);
01527       break;
01528     }
01529     case bitc::METADATA_TEMPLATE_TYPE: {
01530       if (Record.size() != 3)
01531         return Error("Invalid record");
01532 
01533       MDValueList.AssignValue(GET_OR_DISTINCT(MDTemplateTypeParameter,
01534                                               Record[0],
01535                                               (Context, getMDString(Record[1]),
01536                                                getMDOrNull(Record[2]))),
01537                               NextMDValueNo++);
01538       break;
01539     }
01540     case bitc::METADATA_TEMPLATE_VALUE: {
01541       if (Record.size() != 5)
01542         return Error("Invalid record");
01543 
01544       MDValueList.AssignValue(
01545           GET_OR_DISTINCT(MDTemplateValueParameter, Record[0],
01546                           (Context, Record[1], getMDString(Record[2]),
01547                            getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
01548           NextMDValueNo++);
01549       break;
01550     }
01551     case bitc::METADATA_GLOBAL_VAR: {
01552       if (Record.size() != 11)
01553         return Error("Invalid record");
01554 
01555       MDValueList.AssignValue(
01556           GET_OR_DISTINCT(MDGlobalVariable, Record[0],
01557                           (Context, getMDOrNull(Record[1]),
01558                            getMDString(Record[2]), getMDString(Record[3]),
01559                            getMDOrNull(Record[4]), Record[5],
01560                            getMDOrNull(Record[6]), Record[7], Record[8],
01561                            getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
01562           NextMDValueNo++);
01563       break;
01564     }
01565     case bitc::METADATA_LOCAL_VAR: {
01566       if (Record.size() != 10)
01567         return Error("Invalid record");
01568 
01569       MDValueList.AssignValue(
01570           GET_OR_DISTINCT(MDLocalVariable, Record[0],
01571                           (Context, Record[1], getMDOrNull(Record[2]),
01572                            getMDString(Record[3]), getMDOrNull(Record[4]),
01573                            Record[5], getMDOrNull(Record[6]), Record[7],
01574                            Record[8], getMDOrNull(Record[9]))),
01575           NextMDValueNo++);
01576       break;
01577     }
01578     case bitc::METADATA_EXPRESSION: {
01579       if (Record.size() < 1)
01580         return Error("Invalid record");
01581 
01582       MDValueList.AssignValue(
01583           GET_OR_DISTINCT(MDExpression, Record[0],
01584                           (Context, makeArrayRef(Record).slice(1))),
01585           NextMDValueNo++);
01586       break;
01587     }
01588     case bitc::METADATA_OBJC_PROPERTY: {
01589       if (Record.size() != 8)
01590         return Error("Invalid record");
01591 
01592       MDValueList.AssignValue(
01593           GET_OR_DISTINCT(MDObjCProperty, Record[0],
01594                           (Context, getMDString(Record[1]),
01595                            getMDOrNull(Record[2]), Record[3],
01596                            getMDString(Record[4]), getMDString(Record[5]),
01597                            Record[6], getMDOrNull(Record[7]))),
01598           NextMDValueNo++);
01599       break;
01600     }
01601     case bitc::METADATA_IMPORTED_ENTITY: {
01602       if (Record.size() != 6)
01603         return Error("Invalid record");
01604 
01605       MDValueList.AssignValue(
01606           GET_OR_DISTINCT(MDImportedEntity, Record[0],
01607                           (Context, Record[1], getMDOrNull(Record[2]),
01608                            getMDOrNull(Record[3]), Record[4],
01609                            getMDString(Record[5]))),
01610           NextMDValueNo++);
01611       break;
01612     }
01613     case bitc::METADATA_STRING: {
01614       std::string String(Record.begin(), Record.end());
01615       llvm::UpgradeMDStringConstant(String);
01616       Metadata *MD = MDString::get(Context, String);
01617       MDValueList.AssignValue(MD, NextMDValueNo++);
01618       break;
01619     }
01620     case bitc::METADATA_KIND: {
01621       if (Record.size() < 2)
01622         return Error("Invalid record");
01623 
01624       unsigned Kind = Record[0];
01625       SmallString<8> Name(Record.begin()+1, Record.end());
01626 
01627       unsigned NewKind = TheModule->getMDKindID(Name.str());
01628       if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
01629         return Error("Conflicting METADATA_KIND records");
01630       break;
01631     }
01632     }
01633   }
01634 #undef GET_OR_DISTINCT
01635 }
01636 
01637 /// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
01638 /// the LSB for dense VBR encoding.
01639 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
01640   if ((V & 1) == 0)
01641     return V >> 1;
01642   if (V != 1)
01643     return -(V >> 1);
01644   // There is no such thing as -0 with integers.  "-0" really means MININT.
01645   return 1ULL << 63;
01646 }
01647 
01648 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
01649 /// values and aliases that we can.
01650 std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
01651   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
01652   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
01653   std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
01654   std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
01655 
01656   GlobalInitWorklist.swap(GlobalInits);
01657   AliasInitWorklist.swap(AliasInits);
01658   FunctionPrefixWorklist.swap(FunctionPrefixes);
01659   FunctionPrologueWorklist.swap(FunctionPrologues);
01660 
01661   while (!GlobalInitWorklist.empty()) {
01662     unsigned ValID = GlobalInitWorklist.back().second;
01663     if (ValID >= ValueList.size()) {
01664       // Not ready to resolve this yet, it requires something later in the file.
01665       GlobalInits.push_back(GlobalInitWorklist.back());
01666     } else {
01667       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
01668         GlobalInitWorklist.back().first->setInitializer(C);
01669       else
01670         return Error("Expected a constant");
01671     }
01672     GlobalInitWorklist.pop_back();
01673   }
01674 
01675   while (!AliasInitWorklist.empty()) {
01676     unsigned ValID = AliasInitWorklist.back().second;
01677     if (ValID >= ValueList.size()) {
01678       AliasInits.push_back(AliasInitWorklist.back());
01679     } else {
01680       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
01681         AliasInitWorklist.back().first->setAliasee(C);
01682       else
01683         return Error("Expected a constant");
01684     }
01685     AliasInitWorklist.pop_back();
01686   }
01687 
01688   while (!FunctionPrefixWorklist.empty()) {
01689     unsigned ValID = FunctionPrefixWorklist.back().second;
01690     if (ValID >= ValueList.size()) {
01691       FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
01692     } else {
01693       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
01694         FunctionPrefixWorklist.back().first->setPrefixData(C);
01695       else
01696         return Error("Expected a constant");
01697     }
01698     FunctionPrefixWorklist.pop_back();
01699   }
01700 
01701   while (!FunctionPrologueWorklist.empty()) {
01702     unsigned ValID = FunctionPrologueWorklist.back().second;
01703     if (ValID >= ValueList.size()) {
01704       FunctionPrologues.push_back(FunctionPrologueWorklist.back());
01705     } else {
01706       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
01707         FunctionPrologueWorklist.back().first->setPrologueData(C);
01708       else
01709         return Error("Expected a constant");
01710     }
01711     FunctionPrologueWorklist.pop_back();
01712   }
01713 
01714   return std::error_code();
01715 }
01716 
01717 static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
01718   SmallVector<uint64_t, 8> Words(Vals.size());
01719   std::transform(Vals.begin(), Vals.end(), Words.begin(),
01720                  BitcodeReader::decodeSignRotatedValue);
01721 
01722   return APInt(TypeBits, Words);
01723 }
01724 
01725 std::error_code BitcodeReader::ParseConstants() {
01726   if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
01727     return Error("Invalid record");
01728 
01729   SmallVector<uint64_t, 64> Record;
01730 
01731   // Read all the records for this value table.
01732   Type *CurTy = Type::getInt32Ty(Context);
01733   unsigned NextCstNo = ValueList.size();
01734   while (1) {
01735     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01736 
01737     switch (Entry.Kind) {
01738     case BitstreamEntry::SubBlock: // Handled for us already.
01739     case BitstreamEntry::Error:
01740       return Error("Malformed block");
01741     case BitstreamEntry::EndBlock:
01742       if (NextCstNo != ValueList.size())
01743         return Error("Invalid ronstant reference");
01744 
01745       // Once all the constants have been read, go through and resolve forward
01746       // references.
01747       ValueList.ResolveConstantForwardRefs();
01748       return std::error_code();
01749     case BitstreamEntry::Record:
01750       // The interesting case.
01751       break;
01752     }
01753 
01754     // Read a record.
01755     Record.clear();
01756     Value *V = nullptr;
01757     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
01758     switch (BitCode) {
01759     default:  // Default behavior: unknown constant
01760     case bitc::CST_CODE_UNDEF:     // UNDEF
01761       V = UndefValue::get(CurTy);
01762       break;
01763     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
01764       if (Record.empty())
01765         return Error("Invalid record");
01766       if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
01767         return Error("Invalid record");
01768       CurTy = TypeList[Record[0]];
01769       continue;  // Skip the ValueList manipulation.
01770     case bitc::CST_CODE_NULL:      // NULL
01771       V = Constant::getNullValue(CurTy);
01772       break;
01773     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
01774       if (!CurTy->isIntegerTy() || Record.empty())
01775         return Error("Invalid record");
01776       V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
01777       break;
01778     case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
01779       if (!CurTy->isIntegerTy() || Record.empty())
01780         return Error("Invalid record");
01781 
01782       APInt VInt = ReadWideAPInt(Record,
01783                                  cast<IntegerType>(CurTy)->getBitWidth());
01784       V = ConstantInt::get(Context, VInt);
01785 
01786       break;
01787     }
01788     case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
01789       if (Record.empty())
01790         return Error("Invalid record");
01791       if (CurTy->isHalfTy())
01792         V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
01793                                              APInt(16, (uint16_t)Record[0])));
01794       else if (CurTy->isFloatTy())
01795         V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
01796                                              APInt(32, (uint32_t)Record[0])));
01797       else if (CurTy->isDoubleTy())
01798         V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
01799                                              APInt(64, Record[0])));
01800       else if (CurTy->isX86_FP80Ty()) {
01801         // Bits are not stored the same way as a normal i80 APInt, compensate.
01802         uint64_t Rearrange[2];
01803         Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
01804         Rearrange[1] = Record[0] >> 48;
01805         V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
01806                                              APInt(80, Rearrange)));
01807       } else if (CurTy->isFP128Ty())
01808         V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
01809                                              APInt(128, Record)));
01810       else if (CurTy->isPPC_FP128Ty())
01811         V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
01812                                              APInt(128, Record)));
01813       else
01814         V = UndefValue::get(CurTy);
01815       break;
01816     }
01817 
01818     case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
01819       if (Record.empty())
01820         return Error("Invalid record");
01821 
01822       unsigned Size = Record.size();
01823       SmallVector<Constant*, 16> Elts;
01824 
01825       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
01826         for (unsigned i = 0; i != Size; ++i)
01827           Elts.push_back(ValueList.getConstantFwdRef(Record[i],
01828                                                      STy->getElementType(i)));
01829         V = ConstantStruct::get(STy, Elts);
01830       } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
01831         Type *EltTy = ATy->getElementType();
01832         for (unsigned i = 0; i != Size; ++i)
01833           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
01834         V = ConstantArray::get(ATy, Elts);
01835       } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
01836         Type *EltTy = VTy->getElementType();
01837         for (unsigned i = 0; i != Size; ++i)
01838           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
01839         V = ConstantVector::get(Elts);
01840       } else {
01841         V = UndefValue::get(CurTy);
01842       }
01843       break;
01844     }
01845     case bitc::CST_CODE_STRING:    // STRING: [values]
01846     case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
01847       if (Record.empty())
01848         return Error("Invalid record");
01849 
01850       SmallString<16> Elts(Record.begin(), Record.end());
01851       V = ConstantDataArray::getString(Context, Elts,
01852                                        BitCode == bitc::CST_CODE_CSTRING);
01853       break;
01854     }
01855     case bitc::CST_CODE_DATA: {// DATA: [n x value]
01856       if (Record.empty())
01857         return Error("Invalid record");
01858 
01859       Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
01860       unsigned Size = Record.size();
01861 
01862       if (EltTy->isIntegerTy(8)) {
01863         SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
01864         if (isa<VectorType>(CurTy))
01865           V = ConstantDataVector::get(Context, Elts);
01866         else
01867           V = ConstantDataArray::get(Context, Elts);
01868       } else if (EltTy->isIntegerTy(16)) {
01869         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
01870         if (isa<VectorType>(CurTy))
01871           V = ConstantDataVector::get(Context, Elts);
01872         else
01873           V = ConstantDataArray::get(Context, Elts);
01874       } else if (EltTy->isIntegerTy(32)) {
01875         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
01876         if (isa<VectorType>(CurTy))
01877           V = ConstantDataVector::get(Context, Elts);
01878         else
01879           V = ConstantDataArray::get(Context, Elts);
01880       } else if (EltTy->isIntegerTy(64)) {
01881         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
01882         if (isa<VectorType>(CurTy))
01883           V = ConstantDataVector::get(Context, Elts);
01884         else
01885           V = ConstantDataArray::get(Context, Elts);
01886       } else if (EltTy->isFloatTy()) {
01887         SmallVector<float, 16> Elts(Size);
01888         std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
01889         if (isa<VectorType>(CurTy))
01890           V = ConstantDataVector::get(Context, Elts);
01891         else
01892           V = ConstantDataArray::get(Context, Elts);
01893       } else if (EltTy->isDoubleTy()) {
01894         SmallVector<double, 16> Elts(Size);
01895         std::transform(Record.begin(), Record.end(), Elts.begin(),
01896                        BitsToDouble);
01897         if (isa<VectorType>(CurTy))
01898           V = ConstantDataVector::get(Context, Elts);
01899         else
01900           V = ConstantDataArray::get(Context, Elts);
01901       } else {
01902         return Error("Invalid type for value");
01903       }
01904       break;
01905     }
01906 
01907     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
01908       if (Record.size() < 3)
01909         return Error("Invalid record");
01910       int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
01911       if (Opc < 0) {
01912         V = UndefValue::get(CurTy);  // Unknown binop.
01913       } else {
01914         Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
01915         Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
01916         unsigned Flags = 0;
01917         if (Record.size() >= 4) {
01918           if (Opc == Instruction::Add ||
01919               Opc == Instruction::Sub ||
01920               Opc == Instruction::Mul ||
01921               Opc == Instruction::Shl) {
01922             if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
01923               Flags |= OverflowingBinaryOperator::NoSignedWrap;
01924             if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
01925               Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
01926           } else if (Opc == Instruction::SDiv ||
01927                      Opc == Instruction::UDiv ||
01928                      Opc == Instruction::LShr ||
01929                      Opc == Instruction::AShr) {
01930             if (Record[3] & (1 << bitc::PEO_EXACT))
01931               Flags |= SDivOperator::IsExact;
01932           }
01933         }
01934         V = ConstantExpr::get(Opc, LHS, RHS, Flags);
01935       }
01936       break;
01937     }
01938     case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
01939       if (Record.size() < 3)
01940         return Error("Invalid record");
01941       int Opc = GetDecodedCastOpcode(Record[0]);
01942       if (Opc < 0) {
01943         V = UndefValue::get(CurTy);  // Unknown cast.
01944       } else {
01945         Type *OpTy = getTypeByID(Record[1]);
01946         if (!OpTy)
01947           return Error("Invalid record");
01948         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
01949         V = UpgradeBitCastExpr(Opc, Op, CurTy);
01950         if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
01951       }
01952       break;
01953     }
01954     case bitc::CST_CODE_CE_INBOUNDS_GEP:
01955     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
01956       if (Record.size() & 1)
01957         return Error("Invalid record");
01958       SmallVector<Constant*, 16> Elts;
01959       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
01960         Type *ElTy = getTypeByID(Record[i]);
01961         if (!ElTy)
01962           return Error("Invalid record");
01963         Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
01964       }
01965       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
01966       V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
01967                                          BitCode ==
01968                                            bitc::CST_CODE_CE_INBOUNDS_GEP);
01969       break;
01970     }
01971     case bitc::CST_CODE_CE_SELECT: {  // CE_SELECT: [opval#, opval#, opval#]
01972       if (Record.size() < 3)
01973         return Error("Invalid record");
01974 
01975       Type *SelectorTy = Type::getInt1Ty(Context);
01976 
01977       // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
01978       // vector. Otherwise, it must be a single bit.
01979       if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
01980         SelectorTy = VectorType::get(Type::getInt1Ty(Context),
01981                                      VTy->getNumElements());
01982 
01983       V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
01984                                                               SelectorTy),
01985                                   ValueList.getConstantFwdRef(Record[1],CurTy),
01986                                   ValueList.getConstantFwdRef(Record[2],CurTy));
01987       break;
01988     }
01989     case bitc::CST_CODE_CE_EXTRACTELT
01990         : { // CE_EXTRACTELT: [opty, opval, opty, opval]
01991       if (Record.size() < 3)
01992         return Error("Invalid record");
01993       VectorType *OpTy =
01994         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
01995       if (!OpTy)
01996         return Error("Invalid record");
01997       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
01998       Constant *Op1 = nullptr;
01999       if (Record.size() == 4) {
02000         Type *IdxTy = getTypeByID(Record[2]);
02001         if (!IdxTy)
02002           return Error("Invalid record");
02003         Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
02004       } else // TODO: Remove with llvm 4.0
02005         Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
02006       if (!Op1)
02007         return Error("Invalid record");
02008       V = ConstantExpr::getExtractElement(Op0, Op1);
02009       break;
02010     }
02011     case bitc::CST_CODE_CE_INSERTELT
02012         : { // CE_INSERTELT: [opval, opval, opty, opval]
02013       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
02014       if (Record.size() < 3 || !OpTy)
02015         return Error("Invalid record");
02016       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
02017       Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
02018                                                   OpTy->getElementType());
02019       Constant *Op2 = nullptr;
02020       if (Record.size() == 4) {
02021         Type *IdxTy = getTypeByID(Record[2]);
02022         if (!IdxTy)
02023           return Error("Invalid record");
02024         Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
02025       } else // TODO: Remove with llvm 4.0
02026         Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
02027       if (!Op2)
02028         return Error("Invalid record");
02029       V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
02030       break;
02031     }
02032     case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
02033       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
02034       if (Record.size() < 3 || !OpTy)
02035         return Error("Invalid record");
02036       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
02037       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
02038       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
02039                                                  OpTy->getNumElements());
02040       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
02041       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
02042       break;
02043     }
02044     case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
02045       VectorType *RTy = dyn_cast<VectorType>(CurTy);
02046       VectorType *OpTy =
02047         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
02048       if (Record.size() < 4 || !RTy || !OpTy)
02049         return Error("Invalid record");
02050       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
02051       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
02052       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
02053                                                  RTy->getNumElements());
02054       Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
02055       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
02056       break;
02057     }
02058     case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
02059       if (Record.size() < 4)
02060         return Error("Invalid record");
02061       Type *OpTy = getTypeByID(Record[0]);
02062       if (!OpTy)
02063         return Error("Invalid record");
02064       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
02065       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
02066 
02067       if (OpTy->isFPOrFPVectorTy())
02068         V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
02069       else
02070         V = ConstantExpr::getICmp(Record[3], Op0, Op1);
02071       break;
02072     }
02073     // This maintains backward compatibility, pre-asm dialect keywords.
02074     // FIXME: Remove with the 4.0 release.
02075     case bitc::CST_CODE_INLINEASM_OLD: {
02076       if (Record.size() < 2)
02077         return Error("Invalid record");
02078       std::string AsmStr, ConstrStr;
02079       bool HasSideEffects = Record[0] & 1;
02080       bool IsAlignStack = Record[0] >> 1;
02081       unsigned AsmStrSize = Record[1];
02082       if (2+AsmStrSize >= Record.size())
02083         return Error("Invalid record");
02084       unsigned ConstStrSize = Record[2+AsmStrSize];
02085       if (3+AsmStrSize+ConstStrSize > Record.size())
02086         return Error("Invalid record");
02087 
02088       for (unsigned i = 0; i != AsmStrSize; ++i)
02089         AsmStr += (char)Record[2+i];
02090       for (unsigned i = 0; i != ConstStrSize; ++i)
02091         ConstrStr += (char)Record[3+AsmStrSize+i];
02092       PointerType *PTy = cast<PointerType>(CurTy);
02093       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
02094                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
02095       break;
02096     }
02097     // This version adds support for the asm dialect keywords (e.g.,
02098     // inteldialect).
02099     case bitc::CST_CODE_INLINEASM: {
02100       if (Record.size() < 2)
02101         return Error("Invalid record");
02102       std::string AsmStr, ConstrStr;
02103       bool HasSideEffects = Record[0] & 1;
02104       bool IsAlignStack = (Record[0] >> 1) & 1;
02105       unsigned AsmDialect = Record[0] >> 2;
02106       unsigned AsmStrSize = Record[1];
02107       if (2+AsmStrSize >= Record.size())
02108         return Error("Invalid record");
02109       unsigned ConstStrSize = Record[2+AsmStrSize];
02110       if (3+AsmStrSize+ConstStrSize > Record.size())
02111         return Error("Invalid record");
02112 
02113       for (unsigned i = 0; i != AsmStrSize; ++i)
02114         AsmStr += (char)Record[2+i];
02115       for (unsigned i = 0; i != ConstStrSize; ++i)
02116         ConstrStr += (char)Record[3+AsmStrSize+i];
02117       PointerType *PTy = cast<PointerType>(CurTy);
02118       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
02119                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
02120                          InlineAsm::AsmDialect(AsmDialect));
02121       break;
02122     }
02123     case bitc::CST_CODE_BLOCKADDRESS:{
02124       if (Record.size() < 3)
02125         return Error("Invalid record");
02126       Type *FnTy = getTypeByID(Record[0]);
02127       if (!FnTy)
02128         return Error("Invalid record");
02129       Function *Fn =
02130         dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
02131       if (!Fn)
02132         return Error("Invalid record");
02133 
02134       // Don't let Fn get dematerialized.
02135       BlockAddressesTaken.insert(Fn);
02136 
02137       // If the function is already parsed we can insert the block address right
02138       // away.
02139       BasicBlock *BB;
02140       unsigned BBID = Record[2];
02141       if (!BBID)
02142         // Invalid reference to entry block.
02143         return Error("Invalid ID");
02144       if (!Fn->empty()) {
02145         Function::iterator BBI = Fn->begin(), BBE = Fn->end();
02146         for (size_t I = 0, E = BBID; I != E; ++I) {
02147           if (BBI == BBE)
02148             return Error("Invalid ID");
02149           ++BBI;
02150         }
02151         BB = BBI;
02152       } else {
02153         // Otherwise insert a placeholder and remember it so it can be inserted
02154         // when the function is parsed.
02155         auto &FwdBBs = BasicBlockFwdRefs[Fn];
02156         if (FwdBBs.empty())
02157           BasicBlockFwdRefQueue.push_back(Fn);
02158         if (FwdBBs.size() < BBID + 1)
02159           FwdBBs.resize(BBID + 1);
02160         if (!FwdBBs[BBID])
02161           FwdBBs[BBID] = BasicBlock::Create(Context);
02162         BB = FwdBBs[BBID];
02163       }
02164       V = BlockAddress::get(Fn, BB);
02165       break;
02166     }
02167     }
02168 
02169     ValueList.AssignValue(V, NextCstNo);
02170     ++NextCstNo;
02171   }
02172 }
02173 
02174 std::error_code BitcodeReader::ParseUseLists() {
02175   if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
02176     return Error("Invalid record");
02177 
02178   // Read all the records.
02179   SmallVector<uint64_t, 64> Record;
02180   while (1) {
02181     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02182 
02183     switch (Entry.Kind) {
02184     case BitstreamEntry::SubBlock: // Handled for us already.
02185     case BitstreamEntry::Error:
02186       return Error("Malformed block");
02187     case BitstreamEntry::EndBlock:
02188       return std::error_code();
02189     case BitstreamEntry::Record:
02190       // The interesting case.
02191       break;
02192     }
02193 
02194     // Read a use list record.
02195     Record.clear();
02196     bool IsBB = false;
02197     switch (Stream.readRecord(Entry.ID, Record)) {
02198     default:  // Default behavior: unknown type.
02199       break;
02200     case bitc::USELIST_CODE_BB:
02201       IsBB = true;
02202       // fallthrough
02203     case bitc::USELIST_CODE_DEFAULT: {
02204       unsigned RecordLength = Record.size();
02205       if (RecordLength < 3)
02206         // Records should have at least an ID and two indexes.
02207         return Error("Invalid record");
02208       unsigned ID = Record.back();
02209       Record.pop_back();
02210 
02211       Value *V;
02212       if (IsBB) {
02213         assert(ID < FunctionBBs.size() && "Basic block not found");
02214         V = FunctionBBs[ID];
02215       } else
02216         V = ValueList[ID];
02217       unsigned NumUses = 0;
02218       SmallDenseMap<const Use *, unsigned, 16> Order;
02219       for (const Use &U : V->uses()) {
02220         if (++NumUses > Record.size())
02221           break;
02222         Order[&U] = Record[NumUses - 1];
02223       }
02224       if (Order.size() != Record.size() || NumUses > Record.size())
02225         // Mismatches can happen if the functions are being materialized lazily
02226         // (out-of-order), or a value has been upgraded.
02227         break;
02228 
02229       V->sortUseList([&](const Use &L, const Use &R) {
02230         return Order.lookup(&L) < Order.lookup(&R);
02231       });
02232       break;
02233     }
02234     }
02235   }
02236 }
02237 
02238 /// RememberAndSkipFunctionBody - When we see the block for a function body,
02239 /// remember where it is and then skip it.  This lets us lazily deserialize the
02240 /// functions.
02241 std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
02242   // Get the function we are talking about.
02243   if (FunctionsWithBodies.empty())
02244     return Error("Insufficient function protos");
02245 
02246   Function *Fn = FunctionsWithBodies.back();
02247   FunctionsWithBodies.pop_back();
02248 
02249   // Save the current stream state.
02250   uint64_t CurBit = Stream.GetCurrentBitNo();
02251   DeferredFunctionInfo[Fn] = CurBit;
02252 
02253   // Skip over the function block for now.
02254   if (Stream.SkipBlock())
02255     return Error("Invalid record");
02256   return std::error_code();
02257 }
02258 
02259 std::error_code BitcodeReader::GlobalCleanup() {
02260   // Patch the initializers for globals and aliases up.
02261   ResolveGlobalAndAliasInits();
02262   if (!GlobalInits.empty() || !AliasInits.empty())
02263     return Error("Malformed global initializer set");
02264 
02265   // Look for intrinsic functions which need to be upgraded at some point
02266   for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
02267        FI != FE; ++FI) {
02268     Function *NewFn;
02269     if (UpgradeIntrinsicFunction(FI, NewFn))
02270       UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
02271   }
02272 
02273   // Look for global variables which need to be renamed.
02274   for (Module::global_iterator
02275          GI = TheModule->global_begin(), GE = TheModule->global_end();
02276        GI != GE;) {
02277     GlobalVariable *GV = GI++;
02278     UpgradeGlobalVariable(GV);
02279   }
02280 
02281   // Force deallocation of memory for these vectors to favor the client that
02282   // want lazy deserialization.
02283   std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
02284   std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
02285   return std::error_code();
02286 }
02287 
02288 std::error_code BitcodeReader::ParseModule(bool Resume) {
02289   if (Resume)
02290     Stream.JumpToBit(NextUnreadBit);
02291   else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
02292     return Error("Invalid record");
02293 
02294   SmallVector<uint64_t, 64> Record;
02295   std::vector<std::string> SectionTable;
02296   std::vector<std::string> GCTable;
02297 
02298   // Read all the records for this module.
02299   while (1) {
02300     BitstreamEntry Entry = Stream.advance();
02301 
02302     switch (Entry.Kind) {
02303     case BitstreamEntry::Error:
02304       return Error("Malformed block");
02305     case BitstreamEntry::EndBlock:
02306       return GlobalCleanup();
02307 
02308     case BitstreamEntry::SubBlock:
02309       switch (Entry.ID) {
02310       default:  // Skip unknown content.
02311         if (Stream.SkipBlock())
02312           return Error("Invalid record");
02313         break;
02314       case bitc::BLOCKINFO_BLOCK_ID:
02315         if (Stream.ReadBlockInfoBlock())
02316           return Error("Malformed block");
02317         break;
02318       case bitc::PARAMATTR_BLOCK_ID:
02319         if (std::error_code EC = ParseAttributeBlock())
02320           return EC;
02321         break;
02322       case bitc::PARAMATTR_GROUP_BLOCK_ID:
02323         if (std::error_code EC = ParseAttributeGroupBlock())
02324           return EC;
02325         break;
02326       case bitc::TYPE_BLOCK_ID_NEW:
02327         if (std::error_code EC = ParseTypeTable())
02328           return EC;
02329         break;
02330       case bitc::VALUE_SYMTAB_BLOCK_ID:
02331         if (std::error_code EC = ParseValueSymbolTable())
02332           return EC;
02333         SeenValueSymbolTable = true;
02334         break;
02335       case bitc::CONSTANTS_BLOCK_ID:
02336         if (std::error_code EC = ParseConstants())
02337           return EC;
02338         if (std::error_code EC = ResolveGlobalAndAliasInits())
02339           return EC;
02340         break;
02341       case bitc::METADATA_BLOCK_ID:
02342         if (std::error_code EC = ParseMetadata())
02343           return EC;
02344         break;
02345       case bitc::FUNCTION_BLOCK_ID:
02346         // If this is the first function body we've seen, reverse the
02347         // FunctionsWithBodies list.
02348         if (!SeenFirstFunctionBody) {
02349           std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
02350           if (std::error_code EC = GlobalCleanup())
02351             return EC;
02352           SeenFirstFunctionBody = true;
02353         }
02354 
02355         if (std::error_code EC = RememberAndSkipFunctionBody())
02356           return EC;
02357         // For streaming bitcode, suspend parsing when we reach the function
02358         // bodies. Subsequent materialization calls will resume it when
02359         // necessary. For streaming, the function bodies must be at the end of
02360         // the bitcode. If the bitcode file is old, the symbol table will be
02361         // at the end instead and will not have been seen yet. In this case,
02362         // just finish the parse now.
02363         if (LazyStreamer && SeenValueSymbolTable) {
02364           NextUnreadBit = Stream.GetCurrentBitNo();
02365           return std::error_code();
02366         }
02367         break;
02368       case bitc::USELIST_BLOCK_ID:
02369         if (std::error_code EC = ParseUseLists())
02370           return EC;
02371         break;
02372       }
02373       continue;
02374 
02375     case BitstreamEntry::Record:
02376       // The interesting case.
02377       break;
02378     }
02379 
02380 
02381     // Read a record.
02382     switch (Stream.readRecord(Entry.ID, Record)) {
02383     default: break;  // Default behavior, ignore unknown content.
02384     case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
02385       if (Record.size() < 1)
02386         return Error("Invalid record");
02387       // Only version #0 and #1 are supported so far.
02388       unsigned module_version = Record[0];
02389       switch (module_version) {
02390         default:
02391           return Error("Invalid value");
02392         case 0:
02393           UseRelativeIDs = false;
02394           break;
02395         case 1:
02396           UseRelativeIDs = true;
02397           break;
02398       }
02399       break;
02400     }
02401     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
02402       std::string S;
02403       if (ConvertToString(Record, 0, S))
02404         return Error("Invalid record");
02405       TheModule->setTargetTriple(S);
02406       break;
02407     }
02408     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
02409       std::string S;
02410       if (ConvertToString(Record, 0, S))
02411         return Error("Invalid record");
02412       TheModule->setDataLayout(S);
02413       break;
02414     }
02415     case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
02416       std::string S;
02417       if (ConvertToString(Record, 0, S))
02418         return Error("Invalid record");
02419       TheModule->setModuleInlineAsm(S);
02420       break;
02421     }
02422     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
02423       // FIXME: Remove in 4.0.
02424       std::string S;
02425       if (ConvertToString(Record, 0, S))
02426         return Error("Invalid record");
02427       // Ignore value.
02428       break;
02429     }
02430     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
02431       std::string S;
02432       if (ConvertToString(Record, 0, S))
02433         return Error("Invalid record");
02434       SectionTable.push_back(S);
02435       break;
02436     }
02437     case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
02438       std::string S;
02439       if (ConvertToString(Record, 0, S))
02440         return Error("Invalid record");
02441       GCTable.push_back(S);
02442       break;
02443     }
02444     case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
02445       if (Record.size() < 2)
02446         return Error("Invalid record");
02447       Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
02448       unsigned ComdatNameSize = Record[1];
02449       std::string ComdatName;
02450       ComdatName.reserve(ComdatNameSize);
02451       for (unsigned i = 0; i != ComdatNameSize; ++i)
02452         ComdatName += (char)Record[2 + i];
02453       Comdat *C = TheModule->getOrInsertComdat(ComdatName);
02454       C->setSelectionKind(SK);
02455       ComdatList.push_back(C);
02456       break;
02457     }
02458     // GLOBALVAR: [pointer type, isconst, initid,
02459     //             linkage, alignment, section, visibility, threadlocal,
02460     //             unnamed_addr, externally_initialized, dllstorageclass,
02461     //             comdat]
02462     case bitc::MODULE_CODE_GLOBALVAR: {
02463       if (Record.size() < 6)
02464         return Error("Invalid record");
02465       Type *Ty = getTypeByID(Record[0]);
02466       if (!Ty)
02467         return Error("Invalid record");
02468       if (!Ty->isPointerTy())
02469         return Error("Invalid type for value");
02470       unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
02471       Ty = cast<PointerType>(Ty)->getElementType();
02472 
02473       bool isConstant = Record[1];
02474       uint64_t RawLinkage = Record[3];
02475       GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
02476       unsigned Alignment;
02477       if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
02478         return EC;
02479       std::string Section;
02480       if (Record[5]) {
02481         if (Record[5]-1 >= SectionTable.size())
02482           return Error("Invalid ID");
02483         Section = SectionTable[Record[5]-1];
02484       }
02485       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
02486       // Local linkage must have default visibility.
02487       if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
02488         // FIXME: Change to an error if non-default in 4.0.
02489         Visibility = GetDecodedVisibility(Record[6]);
02490 
02491       GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
02492       if (Record.size() > 7)
02493         TLM = GetDecodedThreadLocalMode(Record[7]);
02494 
02495       bool UnnamedAddr = false;
02496       if (Record.size() > 8)
02497         UnnamedAddr = Record[8];
02498 
02499       bool ExternallyInitialized = false;
02500       if (Record.size() > 9)
02501         ExternallyInitialized = Record[9];
02502 
02503       GlobalVariable *NewGV =
02504         new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
02505                            TLM, AddressSpace, ExternallyInitialized);
02506       NewGV->setAlignment(Alignment);
02507       if (!Section.empty())
02508         NewGV->setSection(Section);
02509       NewGV->setVisibility(Visibility);
02510       NewGV->setUnnamedAddr(UnnamedAddr);
02511 
02512       if (Record.size() > 10)
02513         NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
02514       else
02515         UpgradeDLLImportExportLinkage(NewGV, RawLinkage);
02516 
02517       ValueList.push_back(NewGV);
02518 
02519       // Remember which value to use for the global initializer.
02520       if (unsigned InitID = Record[2])
02521         GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
02522 
02523       if (Record.size() > 11) {
02524         if (unsigned ComdatID = Record[11]) {
02525           assert(ComdatID <= ComdatList.size());
02526           NewGV->setComdat(ComdatList[ComdatID - 1]);
02527         }
02528       } else if (hasImplicitComdat(RawLinkage)) {
02529         NewGV->setComdat(reinterpret_cast<Comdat *>(1));
02530       }
02531       break;
02532     }
02533     // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
02534     //             alignment, section, visibility, gc, unnamed_addr,
02535     //             prologuedata, dllstorageclass, comdat, prefixdata]
02536     case bitc::MODULE_CODE_FUNCTION: {
02537       if (Record.size() < 8)
02538         return Error("Invalid record");
02539       Type *Ty = getTypeByID(Record[0]);
02540       if (!Ty)
02541         return Error("Invalid record");
02542       if (!Ty->isPointerTy())
02543         return Error("Invalid type for value");
02544       FunctionType *FTy =
02545         dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
02546       if (!FTy)
02547         return Error("Invalid type for value");
02548 
02549       Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
02550                                         "", TheModule);
02551 
02552       Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
02553       bool isProto = Record[2];
02554       uint64_t RawLinkage = Record[3];
02555       Func->setLinkage(getDecodedLinkage(RawLinkage));
02556       Func->setAttributes(getAttributes(Record[4]));
02557 
02558       unsigned Alignment;
02559       if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
02560         return EC;
02561       Func->setAlignment(Alignment);
02562       if (Record[6]) {
02563         if (Record[6]-1 >= SectionTable.size())
02564           return Error("Invalid ID");
02565         Func->setSection(SectionTable[Record[6]-1]);
02566       }
02567       // Local linkage must have default visibility.
02568       if (!Func->hasLocalLinkage())
02569         // FIXME: Change to an error if non-default in 4.0.
02570         Func->setVisibility(GetDecodedVisibility(Record[7]));
02571       if (Record.size() > 8 && Record[8]) {
02572         if (Record[8]-1 > GCTable.size())
02573           return Error("Invalid ID");
02574         Func->setGC(GCTable[Record[8]-1].c_str());
02575       }
02576       bool UnnamedAddr = false;
02577       if (Record.size() > 9)
02578         UnnamedAddr = Record[9];
02579       Func->setUnnamedAddr(UnnamedAddr);
02580       if (Record.size() > 10 && Record[10] != 0)
02581         FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
02582 
02583       if (Record.size() > 11)
02584         Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
02585       else
02586         UpgradeDLLImportExportLinkage(Func, RawLinkage);
02587 
02588       if (Record.size() > 12) {
02589         if (unsigned ComdatID = Record[12]) {
02590           assert(ComdatID <= ComdatList.size());
02591           Func->setComdat(ComdatList[ComdatID - 1]);
02592         }
02593       } else if (hasImplicitComdat(RawLinkage)) {
02594         Func->setComdat(reinterpret_cast<Comdat *>(1));
02595       }
02596 
02597       if (Record.size() > 13 && Record[13] != 0)
02598         FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
02599 
02600       ValueList.push_back(Func);
02601 
02602       // If this is a function with a body, remember the prototype we are
02603       // creating now, so that we can match up the body with them later.
02604       if (!isProto) {
02605         Func->setIsMaterializable(true);
02606         FunctionsWithBodies.push_back(Func);
02607         if (LazyStreamer)
02608           DeferredFunctionInfo[Func] = 0;
02609       }
02610       break;
02611     }
02612     // ALIAS: [alias type, aliasee val#, linkage]
02613     // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
02614     case bitc::MODULE_CODE_ALIAS: {
02615       if (Record.size() < 3)
02616         return Error("Invalid record");
02617       Type *Ty = getTypeByID(Record[0]);
02618       if (!Ty)
02619         return Error("Invalid record");
02620       auto *PTy = dyn_cast<PointerType>(Ty);
02621       if (!PTy)
02622         return Error("Invalid type for value");
02623 
02624       auto *NewGA =
02625           GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
02626                               getDecodedLinkage(Record[2]), "", TheModule);
02627       // Old bitcode files didn't have visibility field.
02628       // Local linkage must have default visibility.
02629       if (Record.size() > 3 && !NewGA->hasLocalLinkage())
02630         // FIXME: Change to an error if non-default in 4.0.
02631         NewGA->setVisibility(GetDecodedVisibility(Record[3]));
02632       if (Record.size() > 4)
02633         NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
02634       else
02635         UpgradeDLLImportExportLinkage(NewGA, Record[2]);
02636       if (Record.size() > 5)
02637         NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
02638       if (Record.size() > 6)
02639         NewGA->setUnnamedAddr(Record[6]);
02640       ValueList.push_back(NewGA);
02641       AliasInits.push_back(std::make_pair(NewGA, Record[1]));
02642       break;
02643     }
02644     /// MODULE_CODE_PURGEVALS: [numvals]
02645     case bitc::MODULE_CODE_PURGEVALS:
02646       // Trim down the value list to the specified size.
02647       if (Record.size() < 1 || Record[0] > ValueList.size())
02648         return Error("Invalid record");
02649       ValueList.shrinkTo(Record[0]);
02650       break;
02651     }
02652     Record.clear();
02653   }
02654 }
02655 
02656 std::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
02657   TheModule = nullptr;
02658 
02659   if (std::error_code EC = InitStream())
02660     return EC;
02661 
02662   // Sniff for the signature.
02663   if (Stream.Read(8) != 'B' ||
02664       Stream.Read(8) != 'C' ||
02665       Stream.Read(4) != 0x0 ||
02666       Stream.Read(4) != 0xC ||
02667       Stream.Read(4) != 0xE ||
02668       Stream.Read(4) != 0xD)
02669     return Error("Invalid bitcode signature");
02670 
02671   // We expect a number of well-defined blocks, though we don't necessarily
02672   // need to understand them all.
02673   while (1) {
02674     if (Stream.AtEndOfStream())
02675       return std::error_code();
02676 
02677     BitstreamEntry Entry =
02678       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
02679 
02680     switch (Entry.Kind) {
02681     case BitstreamEntry::Error:
02682       return Error("Malformed block");
02683     case BitstreamEntry::EndBlock:
02684       return std::error_code();
02685 
02686     case BitstreamEntry::SubBlock:
02687       switch (Entry.ID) {
02688       case bitc::BLOCKINFO_BLOCK_ID:
02689         if (Stream.ReadBlockInfoBlock())
02690           return Error("Malformed block");
02691         break;
02692       case bitc::MODULE_BLOCK_ID:
02693         // Reject multiple MODULE_BLOCK's in a single bitstream.
02694         if (TheModule)
02695           return Error("Invalid multiple blocks");
02696         TheModule = M;
02697         if (std::error_code EC = ParseModule(false))
02698           return EC;
02699         if (LazyStreamer)
02700           return std::error_code();
02701         break;
02702       default:
02703         if (Stream.SkipBlock())
02704           return Error("Invalid record");
02705         break;
02706       }
02707       continue;
02708     case BitstreamEntry::Record:
02709       // There should be no records in the top-level of blocks.
02710 
02711       // The ranlib in Xcode 4 will align archive members by appending newlines
02712       // to the end of them. If this file size is a multiple of 4 but not 8, we
02713       // have to read and ignore these final 4 bytes :-(
02714       if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
02715           Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
02716           Stream.AtEndOfStream())
02717         return std::error_code();
02718 
02719       return Error("Invalid record");
02720     }
02721   }
02722 }
02723 
02724 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
02725   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
02726     return Error("Invalid record");
02727 
02728   SmallVector<uint64_t, 64> Record;
02729 
02730   std::string Triple;
02731   // Read all the records for this module.
02732   while (1) {
02733     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02734 
02735     switch (Entry.Kind) {
02736     case BitstreamEntry::SubBlock: // Handled for us already.
02737     case BitstreamEntry::Error:
02738       return Error("Malformed block");
02739     case BitstreamEntry::EndBlock:
02740       return Triple;
02741     case BitstreamEntry::Record:
02742       // The interesting case.
02743       break;
02744     }
02745 
02746     // Read a record.
02747     switch (Stream.readRecord(Entry.ID, Record)) {
02748     default: break;  // Default behavior, ignore unknown content.
02749     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
02750       std::string S;
02751       if (ConvertToString(Record, 0, S))
02752         return Error("Invalid record");
02753       Triple = S;
02754       break;
02755     }
02756     }
02757     Record.clear();
02758   }
02759   llvm_unreachable("Exit infinite loop");
02760 }
02761 
02762 ErrorOr<std::string> BitcodeReader::parseTriple() {
02763   if (std::error_code EC = InitStream())
02764     return EC;
02765 
02766   // Sniff for the signature.
02767   if (Stream.Read(8) != 'B' ||
02768       Stream.Read(8) != 'C' ||
02769       Stream.Read(4) != 0x0 ||
02770       Stream.Read(4) != 0xC ||
02771       Stream.Read(4) != 0xE ||
02772       Stream.Read(4) != 0xD)
02773     return Error("Invalid bitcode signature");
02774 
02775   // We expect a number of well-defined blocks, though we don't necessarily
02776   // need to understand them all.
02777   while (1) {
02778     BitstreamEntry Entry = Stream.advance();
02779 
02780     switch (Entry.Kind) {
02781     case BitstreamEntry::Error:
02782       return Error("Malformed block");
02783     case BitstreamEntry::EndBlock:
02784       return std::error_code();
02785 
02786     case BitstreamEntry::SubBlock:
02787       if (Entry.ID == bitc::MODULE_BLOCK_ID)
02788         return parseModuleTriple();
02789 
02790       // Ignore other sub-blocks.
02791       if (Stream.SkipBlock())
02792         return Error("Malformed block");
02793       continue;
02794 
02795     case BitstreamEntry::Record:
02796       Stream.skipRecord(Entry.ID);
02797       continue;
02798     }
02799   }
02800 }
02801 
02802 /// ParseMetadataAttachment - Parse metadata attachments.
02803 std::error_code BitcodeReader::ParseMetadataAttachment() {
02804   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
02805     return Error("Invalid record");
02806 
02807   SmallVector<uint64_t, 64> Record;
02808   while (1) {
02809     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02810 
02811     switch (Entry.Kind) {
02812     case BitstreamEntry::SubBlock: // Handled for us already.
02813     case BitstreamEntry::Error:
02814       return Error("Malformed block");
02815     case BitstreamEntry::EndBlock:
02816       return std::error_code();
02817     case BitstreamEntry::Record:
02818       // The interesting case.
02819       break;
02820     }
02821 
02822     // Read a metadata attachment record.
02823     Record.clear();
02824     switch (Stream.readRecord(Entry.ID, Record)) {
02825     default:  // Default behavior: ignore.
02826       break;
02827     case bitc::METADATA_ATTACHMENT: {
02828       unsigned RecordLength = Record.size();
02829       if (Record.empty() || (RecordLength - 1) % 2 == 1)
02830         return Error("Invalid record");
02831       Instruction *Inst = InstructionList[Record[0]];
02832       for (unsigned i = 1; i != RecordLength; i = i+2) {
02833         unsigned Kind = Record[i];
02834         DenseMap<unsigned, unsigned>::iterator I =
02835           MDKindMap.find(Kind);
02836         if (I == MDKindMap.end())
02837           return Error("Invalid ID");
02838         Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
02839         if (isa<LocalAsMetadata>(Node))
02840           // Drop the attachment.  This used to be legal, but there's no
02841           // upgrade path.
02842           break;
02843         Inst->setMetadata(I->second, cast<MDNode>(Node));
02844         if (I->second == LLVMContext::MD_tbaa)
02845           InstsWithTBAATag.push_back(Inst);
02846       }
02847       break;
02848     }
02849     }
02850   }
02851 }
02852 
02853 /// ParseFunctionBody - Lazily parse the specified function body block.
02854 std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
02855   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
02856     return Error("Invalid record");
02857 
02858   InstructionList.clear();
02859   unsigned ModuleValueListSize = ValueList.size();
02860   unsigned ModuleMDValueListSize = MDValueList.size();
02861 
02862   // Add all the function arguments to the value table.
02863   for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
02864     ValueList.push_back(I);
02865 
02866   unsigned NextValueNo = ValueList.size();
02867   BasicBlock *CurBB = nullptr;
02868   unsigned CurBBNo = 0;
02869 
02870   DebugLoc LastLoc;
02871   auto getLastInstruction = [&]() -> Instruction * {
02872     if (CurBB && !CurBB->empty())
02873       return &CurBB->back();
02874     else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
02875              !FunctionBBs[CurBBNo - 1]->empty())
02876       return &FunctionBBs[CurBBNo - 1]->back();
02877     return nullptr;
02878   };
02879 
02880   // Read all the records.
02881   SmallVector<uint64_t, 64> Record;
02882   while (1) {
02883     BitstreamEntry Entry = Stream.advance();
02884 
02885     switch (Entry.Kind) {
02886     case BitstreamEntry::Error:
02887       return Error("Malformed block");
02888     case BitstreamEntry::EndBlock:
02889       goto OutOfRecordLoop;
02890 
02891     case BitstreamEntry::SubBlock:
02892       switch (Entry.ID) {
02893       default:  // Skip unknown content.
02894         if (Stream.SkipBlock())
02895           return Error("Invalid record");
02896         break;
02897       case bitc::CONSTANTS_BLOCK_ID:
02898         if (std::error_code EC = ParseConstants())
02899           return EC;
02900         NextValueNo = ValueList.size();
02901         break;
02902       case bitc::VALUE_SYMTAB_BLOCK_ID:
02903         if (std::error_code EC = ParseValueSymbolTable())
02904           return EC;
02905         break;
02906       case bitc::METADATA_ATTACHMENT_ID:
02907         if (std::error_code EC = ParseMetadataAttachment())
02908           return EC;
02909         break;
02910       case bitc::METADATA_BLOCK_ID:
02911         if (std::error_code EC = ParseMetadata())
02912           return EC;
02913         break;
02914       case bitc::USELIST_BLOCK_ID:
02915         if (std::error_code EC = ParseUseLists())
02916           return EC;
02917         break;
02918       }
02919       continue;
02920 
02921     case BitstreamEntry::Record:
02922       // The interesting case.
02923       break;
02924     }
02925 
02926     // Read a record.
02927     Record.clear();
02928     Instruction *I = nullptr;
02929     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
02930     switch (BitCode) {
02931     default: // Default behavior: reject
02932       return Error("Invalid value");
02933     case bitc::FUNC_CODE_DECLAREBLOCKS: {   // DECLAREBLOCKS: [nblocks]
02934       if (Record.size() < 1 || Record[0] == 0)
02935         return Error("Invalid record");
02936       // Create all the basic blocks for the function.
02937       FunctionBBs.resize(Record[0]);
02938 
02939       // See if anything took the address of blocks in this function.
02940       auto BBFRI = BasicBlockFwdRefs.find(F);
02941       if (BBFRI == BasicBlockFwdRefs.end()) {
02942         for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
02943           FunctionBBs[i] = BasicBlock::Create(Context, "", F);
02944       } else {
02945         auto &BBRefs = BBFRI->second;
02946         // Check for invalid basic block references.
02947         if (BBRefs.size() > FunctionBBs.size())
02948           return Error("Invalid ID");
02949         assert(!BBRefs.empty() && "Unexpected empty array");
02950         assert(!BBRefs.front() && "Invalid reference to entry block");
02951         for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
02952              ++I)
02953           if (I < RE && BBRefs[I]) {
02954             BBRefs[I]->insertInto(F);
02955             FunctionBBs[I] = BBRefs[I];
02956           } else {
02957             FunctionBBs[I] = BasicBlock::Create(Context, "", F);
02958           }
02959 
02960         // Erase from the table.
02961         BasicBlockFwdRefs.erase(BBFRI);
02962       }
02963 
02964       CurBB = FunctionBBs[0];
02965       continue;
02966     }
02967 
02968     case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
02969       // This record indicates that the last instruction is at the same
02970       // location as the previous instruction with a location.
02971       I = getLastInstruction();
02972 
02973       if (!I)
02974         return Error("Invalid record");
02975       I->setDebugLoc(LastLoc);
02976       I = nullptr;
02977       continue;
02978 
02979     case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
02980       I = getLastInstruction();
02981       if (!I || Record.size() < 4)
02982         return Error("Invalid record");
02983 
02984       unsigned Line = Record[0], Col = Record[1];
02985       unsigned ScopeID = Record[2], IAID = Record[3];
02986 
02987       MDNode *Scope = nullptr, *IA = nullptr;
02988       if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
02989       if (IAID)    IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
02990       LastLoc = DebugLoc::get(Line, Col, Scope, IA);
02991       I->setDebugLoc(LastLoc);
02992       I = nullptr;
02993       continue;
02994     }
02995 
02996     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
02997       unsigned OpNum = 0;
02998       Value *LHS, *RHS;
02999       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
03000           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
03001           OpNum+1 > Record.size())
03002         return Error("Invalid record");
03003 
03004       int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
03005       if (Opc == -1)
03006         return Error("Invalid record");
03007       I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
03008       InstructionList.push_back(I);
03009       if (OpNum < Record.size()) {
03010         if (Opc == Instruction::Add ||
03011             Opc == Instruction::Sub ||
03012             Opc == Instruction::Mul ||
03013             Opc == Instruction::Shl) {
03014           if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
03015             cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
03016           if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
03017             cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
03018         } else if (Opc == Instruction::SDiv ||
03019                    Opc == Instruction::UDiv ||
03020                    Opc == Instruction::LShr ||
03021                    Opc == Instruction::AShr) {
03022           if (Record[OpNum] & (1 << bitc::PEO_EXACT))
03023             cast<BinaryOperator>(I)->setIsExact(true);
03024         } else if (isa<FPMathOperator>(I)) {
03025           FastMathFlags FMF;
03026           if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
03027             FMF.setUnsafeAlgebra();
03028           if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
03029             FMF.setNoNaNs();
03030           if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
03031             FMF.setNoInfs();
03032           if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
03033             FMF.setNoSignedZeros();
03034           if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
03035             FMF.setAllowReciprocal();
03036           if (FMF.any())
03037             I->setFastMathFlags(FMF);
03038         }
03039 
03040       }
03041       break;
03042     }
03043     case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
03044       unsigned OpNum = 0;
03045       Value *Op;
03046       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
03047           OpNum+2 != Record.size())
03048         return Error("Invalid record");
03049 
03050       Type *ResTy = getTypeByID(Record[OpNum]);
03051       int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
03052       if (Opc == -1 || !ResTy)
03053         return Error("Invalid record");
03054       Instruction *Temp = nullptr;
03055       if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
03056         if (Temp) {
03057           InstructionList.push_back(Temp);
03058           CurBB->getInstList().push_back(Temp);
03059         }
03060       } else {
03061         I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
03062       }
03063       InstructionList.push_back(I);
03064       break;
03065     }
03066     case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
03067     case bitc::FUNC_CODE_INST_GEP_OLD:
03068     case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
03069       unsigned OpNum = 0;
03070 
03071       Type *Ty;
03072       bool InBounds;
03073 
03074       if (BitCode == bitc::FUNC_CODE_INST_GEP) {
03075         InBounds = Record[OpNum++];
03076         Ty = getTypeByID(Record[OpNum++]);
03077       } else {
03078         InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
03079         Ty = nullptr;
03080       }
03081 
03082       Value *BasePtr;
03083       if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
03084         return Error("Invalid record");
03085 
03086       SmallVector<Value*, 16> GEPIdx;
03087       while (OpNum != Record.size()) {
03088         Value *Op;
03089         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
03090           return Error("Invalid record");
03091         GEPIdx.push_back(Op);
03092       }
03093 
03094       I = GetElementPtrInst::Create(BasePtr, GEPIdx);
03095       (void)Ty;
03096       assert(!Ty || Ty == cast<GetElementPtrInst>(I)->getSourceElementType());
03097       InstructionList.push_back(I);
03098       if (InBounds)
03099         cast<GetElementPtrInst>(I)->setIsInBounds(true);
03100       break;
03101     }
03102 
03103     case bitc::FUNC_CODE_INST_EXTRACTVAL: {
03104                                        // EXTRACTVAL: [opty, opval, n x indices]
03105       unsigned OpNum = 0;
03106       Value *Agg;
03107       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
03108         return Error("Invalid record");
03109 
03110       SmallVector<unsigned, 4> EXTRACTVALIdx;
03111       Type *CurTy = Agg->getType();
03112       for (unsigned RecSize = Record.size();
03113            OpNum != RecSize; ++OpNum) {
03114         bool IsArray = CurTy->isArrayTy();
03115         bool IsStruct = CurTy->isStructTy();
03116         uint64_t Index = Record[OpNum];
03117 
03118         if (!IsStruct && !IsArray)
03119           return Error("EXTRACTVAL: Invalid type");
03120         if ((unsigned)Index != Index)
03121           return Error("Invalid value");
03122         if (IsStruct && Index >= CurTy->subtypes().size())
03123           return Error("EXTRACTVAL: Invalid struct index");
03124         if (IsArray && Index >= CurTy->getArrayNumElements())
03125           return Error("EXTRACTVAL: Invalid array index");
03126         EXTRACTVALIdx.push_back((unsigned)Index);
03127 
03128         if (IsStruct)
03129           CurTy = CurTy->subtypes()[Index];
03130         else
03131           CurTy = CurTy->subtypes()[0];
03132       }
03133 
03134       I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
03135       InstructionList.push_back(I);
03136       break;
03137     }
03138 
03139     case bitc::FUNC_CODE_INST_INSERTVAL: {
03140                            // INSERTVAL: [opty, opval, opty, opval, n x indices]
03141       unsigned OpNum = 0;
03142       Value *Agg;
03143       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
03144         return Error("Invalid record");
03145       Value *Val;
03146       if (getValueTypePair(Record, OpNum, NextValueNo, Val))
03147         return Error("Invalid record");
03148 
03149       SmallVector<unsigned, 4> INSERTVALIdx;
03150       Type *CurTy = Agg->getType();
03151       for (unsigned RecSize = Record.size();
03152            OpNum != RecSize; ++OpNum) {
03153         bool IsArray = CurTy->isArrayTy();
03154         bool IsStruct = CurTy->isStructTy();
03155         uint64_t Index = Record[OpNum];
03156 
03157         if (!IsStruct && !IsArray)
03158           return Error("INSERTVAL: Invalid type");
03159         if (!CurTy->isStructTy() && !CurTy->isArrayTy())
03160           return Error("Invalid type");
03161         if ((unsigned)Index != Index)
03162           return Error("Invalid value");
03163         if (IsStruct && Index >= CurTy->subtypes().size())
03164           return Error("INSERTVAL: Invalid struct index");
03165         if (IsArray && Index >= CurTy->getArrayNumElements())
03166           return Error("INSERTVAL: Invalid array index");
03167 
03168         INSERTVALIdx.push_back((unsigned)Index);
03169         if (IsStruct)
03170           CurTy = CurTy->subtypes()[Index];
03171         else
03172           CurTy = CurTy->subtypes()[0];
03173       }
03174 
03175       I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
03176       InstructionList.push_back(I);
03177       break;
03178     }
03179 
03180     case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
03181       // obsolete form of select
03182       // handles select i1 ... in old bitcode
03183       unsigned OpNum = 0;
03184       Value *TrueVal, *FalseVal, *Cond;
03185       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
03186           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
03187           popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
03188         return Error("Invalid record");
03189 
03190       I = SelectInst::Create(Cond, TrueVal, FalseVal);
03191       InstructionList.push_back(I);
03192       break;
03193     }
03194 
03195     case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
03196       // new form of select
03197       // handles select i1 or select [N x i1]
03198       unsigned OpNum = 0;
03199       Value *TrueVal, *FalseVal, *Cond;
03200       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
03201           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
03202           getValueTypePair(Record, OpNum, NextValueNo, Cond))
03203         return Error("Invalid record");
03204 
03205       // select condition can be either i1 or [N x i1]
03206       if (VectorType* vector_type =
03207           dyn_cast<VectorType>(Cond->getType())) {
03208         // expect <n x i1>
03209         if (vector_type->getElementType() != Type::getInt1Ty(Context))
03210           return Error("Invalid type for value");
03211       } else {
03212         // expect i1
03213         if (Cond->getType() != Type::getInt1Ty(Context))
03214           return Error("Invalid type for value");
03215       }
03216 
03217       I = SelectInst::Create(Cond, TrueVal, FalseVal);
03218       InstructionList.push_back(I);
03219       break;
03220     }
03221 
03222     case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
03223       unsigned OpNum = 0;
03224       Value *Vec, *Idx;
03225       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
03226           getValueTypePair(Record, OpNum, NextValueNo, Idx))
03227         return Error("Invalid record");
03228       I = ExtractElementInst::Create(Vec, Idx);
03229       InstructionList.push_back(I);
03230       break;
03231     }
03232 
03233     case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
03234       unsigned OpNum = 0;
03235       Value *Vec, *Elt, *Idx;
03236       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
03237           popValue(Record, OpNum, NextValueNo,
03238                    cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
03239           getValueTypePair(Record, OpNum, NextValueNo, Idx))
03240         return Error("Invalid record");
03241       I = InsertElementInst::Create(Vec, Elt, Idx);
03242       InstructionList.push_back(I);
03243       break;
03244     }
03245 
03246     case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
03247       unsigned OpNum = 0;
03248       Value *Vec1, *Vec2, *Mask;
03249       if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
03250           popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
03251         return Error("Invalid record");
03252 
03253       if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
03254         return Error("Invalid record");
03255       I = new ShuffleVectorInst(Vec1, Vec2, Mask);
03256       InstructionList.push_back(I);
03257       break;
03258     }
03259 
03260     case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
03261       // Old form of ICmp/FCmp returning bool
03262       // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
03263       // both legal on vectors but had different behaviour.
03264     case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
03265       // FCmp/ICmp returning bool or vector of bool
03266 
03267       unsigned OpNum = 0;
03268       Value *LHS, *RHS;
03269       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
03270           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
03271           OpNum+1 != Record.size())
03272         return Error("Invalid record");
03273 
03274       if (LHS->getType()->isFPOrFPVectorTy())
03275         I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
03276       else
03277         I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
03278       InstructionList.push_back(I);
03279       break;
03280     }
03281 
03282     case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
03283       {
03284         unsigned Size = Record.size();
03285         if (Size == 0) {
03286           I = ReturnInst::Create(Context);
03287           InstructionList.push_back(I);
03288           break;
03289         }
03290 
03291         unsigned OpNum = 0;
03292         Value *Op = nullptr;
03293         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
03294           return Error("Invalid record");
03295         if (OpNum != Record.size())
03296           return Error("Invalid record");
03297 
03298         I = ReturnInst::Create(Context, Op);
03299         InstructionList.push_back(I);
03300         break;
03301       }
03302     case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
03303       if (Record.size() != 1 && Record.size() != 3)
03304         return Error("Invalid record");
03305       BasicBlock *TrueDest = getBasicBlock(Record[0]);
03306       if (!TrueDest)
03307         return Error("Invalid record");
03308 
03309       if (Record.size() == 1) {
03310         I = BranchInst::Create(TrueDest);
03311         InstructionList.push_back(I);
03312       }
03313       else {
03314         BasicBlock *FalseDest = getBasicBlock(Record[1]);
03315         Value *Cond = getValue(Record, 2, NextValueNo,
03316                                Type::getInt1Ty(Context));
03317         if (!FalseDest || !Cond)
03318           return Error("Invalid record");
03319         I = BranchInst::Create(TrueDest, FalseDest, Cond);
03320         InstructionList.push_back(I);
03321       }
03322       break;
03323     }
03324     case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
03325       // Check magic
03326       if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
03327         // "New" SwitchInst format with case ranges. The changes to write this
03328         // format were reverted but we still recognize bitcode that uses it.
03329         // Hopefully someday we will have support for case ranges and can use
03330         // this format again.
03331 
03332         Type *OpTy = getTypeByID(Record[1]);
03333         unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
03334 
03335         Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
03336         BasicBlock *Default = getBasicBlock(Record[3]);
03337         if (!OpTy || !Cond || !Default)
03338           return Error("Invalid record");
03339 
03340         unsigned NumCases = Record[4];
03341 
03342         SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
03343         InstructionList.push_back(SI);
03344 
03345         unsigned CurIdx = 5;
03346         for (unsigned i = 0; i != NumCases; ++i) {
03347           SmallVector<ConstantInt*, 1> CaseVals;
03348           unsigned NumItems = Record[CurIdx++];
03349           for (unsigned ci = 0; ci != NumItems; ++ci) {
03350             bool isSingleNumber = Record[CurIdx++];
03351 
03352             APInt Low;
03353             unsigned ActiveWords = 1;
03354             if (ValueBitWidth > 64)
03355               ActiveWords = Record[CurIdx++];
03356             Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
03357                                 ValueBitWidth);
03358             CurIdx += ActiveWords;
03359 
03360             if (!isSingleNumber) {
03361               ActiveWords = 1;
03362               if (ValueBitWidth > 64)
03363                 ActiveWords = Record[CurIdx++];
03364               APInt High =
03365                   ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
03366                                 ValueBitWidth);
03367               CurIdx += ActiveWords;
03368 
03369               // FIXME: It is not clear whether values in the range should be
03370               // compared as signed or unsigned values. The partially
03371               // implemented changes that used this format in the past used
03372               // unsigned comparisons.
03373               for ( ; Low.ule(High); ++Low)
03374                 CaseVals.push_back(ConstantInt::get(Context, Low));
03375             } else
03376               CaseVals.push_back(ConstantInt::get(Context, Low));
03377           }
03378           BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
03379           for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
03380                  cve = CaseVals.end(); cvi != cve; ++cvi)
03381             SI->addCase(*cvi, DestBB);
03382         }
03383         I = SI;
03384         break;
03385       }
03386 
03387       // Old SwitchInst format without case ranges.
03388 
03389       if (Record.size() < 3 || (Record.size() & 1) == 0)
03390         return Error("Invalid record");
03391       Type *OpTy = getTypeByID(Record[0]);
03392       Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
03393       BasicBlock *Default = getBasicBlock(Record[2]);
03394       if (!OpTy || !Cond || !Default)
03395         return Error("Invalid record");
03396       unsigned NumCases = (Record.size()-3)/2;
03397       SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
03398       InstructionList.push_back(SI);
03399       for (unsigned i = 0, e = NumCases; i != e; ++i) {
03400         ConstantInt *CaseVal =
03401           dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
03402         BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
03403         if (!CaseVal || !DestBB) {
03404           delete SI;
03405           return Error("Invalid record");
03406         }
03407         SI->addCase(CaseVal, DestBB);
03408       }
03409       I = SI;
03410       break;
03411     }
03412     case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
03413       if (Record.size() < 2)
03414         return Error("Invalid record");
03415       Type *OpTy = getTypeByID(Record[0]);
03416       Value *Address = getValue(Record, 1, NextValueNo, OpTy);
03417       if (!OpTy || !Address)
03418         return Error("Invalid record");
03419       unsigned NumDests = Record.size()-2;
03420       IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
03421       InstructionList.push_back(IBI);
03422       for (unsigned i = 0, e = NumDests; i != e; ++i) {
03423         if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
03424           IBI->addDestination(DestBB);
03425         } else {
03426           delete IBI;
03427           return Error("Invalid record");
03428         }
03429       }
03430       I = IBI;
03431       break;
03432     }
03433 
03434     case bitc::FUNC_CODE_INST_INVOKE: {
03435       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
03436       if (Record.size() < 4)
03437         return Error("Invalid record");
03438       AttributeSet PAL = getAttributes(Record[0]);
03439       unsigned CCInfo = Record[1];
03440       BasicBlock *NormalBB = getBasicBlock(Record[2]);
03441       BasicBlock *UnwindBB = getBasicBlock(Record[3]);
03442 
03443       unsigned OpNum = 4;
03444       Value *Callee;
03445       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
03446         return Error("Invalid record");
03447 
03448       PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
03449       FunctionType *FTy = !CalleeTy ? nullptr :
03450         dyn_cast<FunctionType>(CalleeTy->getElementType());
03451 
03452       // Check that the right number of fixed parameters are here.
03453       if (!FTy || !NormalBB || !UnwindBB ||
03454           Record.size() < OpNum+FTy->getNumParams())
03455         return Error("Invalid record");
03456 
03457       SmallVector<Value*, 16> Ops;
03458       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
03459         Ops.push_back(getValue(Record, OpNum, NextValueNo,
03460                                FTy->getParamType(i)));
03461         if (!Ops.back())
03462           return Error("Invalid record");
03463       }
03464 
03465       if (!FTy->isVarArg()) {
03466         if (Record.size() != OpNum)
03467           return Error("Invalid record");
03468       } else {
03469         // Read type/value pairs for varargs params.
03470         while (OpNum != Record.size()) {
03471           Value *Op;
03472           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
03473             return Error("Invalid record");
03474           Ops.push_back(Op);
03475         }
03476       }
03477 
03478       I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
03479       InstructionList.push_back(I);
03480       cast<InvokeInst>(I)->setCallingConv(
03481         static_cast<CallingConv::ID>(CCInfo));
03482       cast<InvokeInst>(I)->setAttributes(PAL);
03483       break;
03484     }
03485     case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
03486       unsigned Idx = 0;
03487       Value *Val = nullptr;
03488       if (getValueTypePair(Record, Idx, NextValueNo, Val))
03489         return Error("Invalid record");
03490       I = ResumeInst::Create(Val);
03491       InstructionList.push_back(I);
03492       break;
03493     }
03494     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
03495       I = new UnreachableInst(Context);
03496       InstructionList.push_back(I);
03497       break;
03498     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
03499       if (Record.size() < 1 || ((Record.size()-1)&1))
03500         return Error("Invalid record");
03501       Type *Ty = getTypeByID(Record[0]);
03502       if (!Ty)
03503         return Error("Invalid record");
03504 
03505       PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
03506       InstructionList.push_back(PN);
03507 
03508       for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
03509         Value *V;
03510         // With the new function encoding, it is possible that operands have
03511         // negative IDs (for forward references).  Use a signed VBR
03512         // representation to keep the encoding small.
03513         if (UseRelativeIDs)
03514           V = getValueSigned(Record, 1+i, NextValueNo, Ty);
03515         else
03516           V = getValue(Record, 1+i, NextValueNo, Ty);
03517         BasicBlock *BB = getBasicBlock(Record[2+i]);
03518         if (!V || !BB)
03519           return Error("Invalid record");
03520         PN->addIncoming(V, BB);
03521       }
03522       I = PN;
03523       break;
03524     }
03525 
03526     case bitc::FUNC_CODE_INST_LANDINGPAD: {
03527       // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
03528       unsigned Idx = 0;
03529       if (Record.size() < 4)
03530         return Error("Invalid record");
03531       Type *Ty = getTypeByID(Record[Idx++]);
03532       if (!Ty)
03533         return Error("Invalid record");
03534       Value *PersFn = nullptr;
03535       if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
03536         return Error("Invalid record");
03537 
03538       bool IsCleanup = !!Record[Idx++];
03539       unsigned NumClauses = Record[Idx++];
03540       LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
03541       LP->setCleanup(IsCleanup);
03542       for (unsigned J = 0; J != NumClauses; ++J) {
03543         LandingPadInst::ClauseType CT =
03544           LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
03545         Value *Val;
03546 
03547         if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
03548           delete LP;
03549           return Error("Invalid record");
03550         }
03551 
03552         assert((CT != LandingPadInst::Catch ||
03553                 !isa<ArrayType>(Val->getType())) &&
03554                "Catch clause has a invalid type!");
03555         assert((CT != LandingPadInst::Filter ||
03556                 isa<ArrayType>(Val->getType())) &&
03557                "Filter clause has invalid type!");
03558         LP->addClause(cast<Constant>(Val));
03559       }
03560 
03561       I = LP;
03562       InstructionList.push_back(I);
03563       break;
03564     }
03565 
03566     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
03567       if (Record.size() != 4)
03568         return Error("Invalid record");
03569       PointerType *Ty =
03570         dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
03571       Type *OpTy = getTypeByID(Record[1]);
03572       Value *Size = getFnValueByID(Record[2], OpTy);
03573       uint64_t AlignRecord = Record[3];
03574       const uint64_t InAllocaMask = uint64_t(1) << 5;
03575       bool InAlloca = AlignRecord & InAllocaMask;
03576       unsigned Align;
03577       if (std::error_code EC =
03578           parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) {
03579         return EC;
03580       }
03581       if (!Ty || !Size)
03582         return Error("Invalid record");
03583       AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align);
03584       AI->setUsedWithInAlloca(InAlloca);
03585       I = AI;
03586       InstructionList.push_back(I);
03587       break;
03588     }
03589     case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
03590       unsigned OpNum = 0;
03591       Value *Op;
03592       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
03593           (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
03594         return Error("Invalid record");
03595 
03596       Type *Ty = nullptr;
03597       if (OpNum + 3 == Record.size())
03598         Ty = getTypeByID(Record[OpNum++]);
03599 
03600       unsigned Align;
03601       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
03602         return EC;
03603       I = new LoadInst(Op, "", Record[OpNum+1], Align);
03604 
03605       (void)Ty;
03606       assert((!Ty || Ty == I->getType()) &&
03607              "Explicit type doesn't match pointee type of the first operand");
03608 
03609       InstructionList.push_back(I);
03610       break;
03611     }
03612     case bitc::FUNC_CODE_INST_LOADATOMIC: {
03613        // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
03614       unsigned OpNum = 0;
03615       Value *Op;
03616       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
03617           (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
03618         return Error("Invalid record");
03619 
03620       Type *Ty = nullptr;
03621       if (OpNum + 5 == Record.size())
03622         Ty = getTypeByID(Record[OpNum++]);
03623 
03624       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
03625       if (Ordering == NotAtomic || Ordering == Release ||
03626           Ordering == AcquireRelease)
03627         return Error("Invalid record");
03628       if (Ordering != NotAtomic && Record[OpNum] == 0)
03629         return Error("Invalid record");
03630       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
03631 
03632       unsigned Align;
03633       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
03634         return EC;
03635       I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
03636 
03637       (void)Ty;
03638       assert((!Ty || Ty == I->getType()) &&
03639              "Explicit type doesn't match pointee type of the first operand");
03640 
03641       InstructionList.push_back(I);
03642       break;
03643     }
03644     case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
03645       unsigned OpNum = 0;
03646       Value *Val, *Ptr;
03647       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
03648           popValue(Record, OpNum, NextValueNo,
03649                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
03650           OpNum+2 != Record.size())
03651         return Error("Invalid record");
03652       unsigned Align;
03653       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
03654         return EC;
03655       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
03656       InstructionList.push_back(I);
03657       break;
03658     }
03659     case bitc::FUNC_CODE_INST_STOREATOMIC: {
03660       // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
03661       unsigned OpNum = 0;
03662       Value *Val, *Ptr;
03663       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
03664           popValue(Record, OpNum, NextValueNo,
03665                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
03666           OpNum+4 != Record.size())
03667         return Error("Invalid record");
03668 
03669       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
03670       if (Ordering == NotAtomic || Ordering == Acquire ||
03671           Ordering == AcquireRelease)
03672         return Error("Invalid record");
03673       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
03674       if (Ordering != NotAtomic && Record[OpNum] == 0)
03675         return Error("Invalid record");
03676 
03677       unsigned Align;
03678       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
03679         return EC;
03680       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
03681       InstructionList.push_back(I);
03682       break;
03683     }
03684     case bitc::FUNC_CODE_INST_CMPXCHG: {
03685       // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
03686       //          failureordering?, isweak?]
03687       unsigned OpNum = 0;
03688       Value *Ptr, *Cmp, *New;
03689       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
03690           popValue(Record, OpNum, NextValueNo,
03691                     cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
03692           popValue(Record, OpNum, NextValueNo,
03693                     cast<PointerType>(Ptr->getType())->getElementType(), New) ||
03694           (Record.size() < OpNum + 3 || Record.size() > OpNum + 5))
03695         return Error("Invalid record");
03696       AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
03697       if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
03698         return Error("Invalid record");
03699       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
03700 
03701       AtomicOrdering FailureOrdering;
03702       if (Record.size() < 7)
03703         FailureOrdering =
03704             AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
03705       else
03706         FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
03707 
03708       I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
03709                                 SynchScope);
03710       cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
03711 
03712       if (Record.size() < 8) {
03713         // Before weak cmpxchgs existed, the instruction simply returned the
03714         // value loaded from memory, so bitcode files from that era will be
03715         // expecting the first component of a modern cmpxchg.
03716         CurBB->getInstList().push_back(I);
03717         I = ExtractValueInst::Create(I, 0);
03718       } else {
03719         cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
03720       }
03721 
03722       InstructionList.push_back(I);
03723       break;
03724     }
03725     case bitc::FUNC_CODE_INST_ATOMICRMW: {
03726       // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
03727       unsigned OpNum = 0;
03728       Value *Ptr, *Val;
03729       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
03730           popValue(Record, OpNum, NextValueNo,
03731                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
03732           OpNum+4 != Record.size())
03733         return Error("Invalid record");
03734       AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
03735       if (Operation < AtomicRMWInst::FIRST_BINOP ||
03736           Operation > AtomicRMWInst::LAST_BINOP)
03737         return Error("Invalid record");
03738       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
03739       if (Ordering == NotAtomic || Ordering == Unordered)
03740         return Error("Invalid record");
03741       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
03742       I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
03743       cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
03744       InstructionList.push_back(I);
03745       break;
03746     }
03747     case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
03748       if (2 != Record.size())
03749         return Error("Invalid record");
03750       AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
03751       if (Ordering == NotAtomic || Ordering == Unordered ||
03752           Ordering == Monotonic)
03753         return Error("Invalid record");
03754       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
03755       I = new FenceInst(Context, Ordering, SynchScope);
03756       InstructionList.push_back(I);
03757       break;
03758     }
03759     case bitc::FUNC_CODE_INST_CALL: {
03760       // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
03761       if (Record.size() < 3)
03762         return Error("Invalid record");
03763 
03764       AttributeSet PAL = getAttributes(Record[0]);
03765       unsigned CCInfo = Record[1];
03766 
03767       unsigned OpNum = 2;
03768       Value *Callee;
03769       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
03770         return Error("Invalid record");
03771 
03772       PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
03773       FunctionType *FTy = nullptr;
03774       if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
03775       if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
03776         return Error("Invalid record");
03777 
03778       SmallVector<Value*, 16> Args;
03779       // Read the fixed params.
03780       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
03781         if (FTy->getParamType(i)->isLabelTy())
03782           Args.push_back(getBasicBlock(Record[OpNum]));
03783         else
03784           Args.push_back(getValue(Record, OpNum, NextValueNo,
03785                                   FTy->getParamType(i)));
03786         if (!Args.back())
03787           return Error("Invalid record");
03788       }
03789 
03790       // Read type/value pairs for varargs params.
03791       if (!FTy->isVarArg()) {
03792         if (OpNum != Record.size())
03793           return Error("Invalid record");
03794       } else {
03795         while (OpNum != Record.size()) {
03796           Value *Op;
03797           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
03798             return Error("Invalid record");
03799           Args.push_back(Op);
03800         }
03801       }
03802 
03803       I = CallInst::Create(Callee, Args);
03804       InstructionList.push_back(I);
03805       cast<CallInst>(I)->setCallingConv(
03806           static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1));
03807       CallInst::TailCallKind TCK = CallInst::TCK_None;
03808       if (CCInfo & 1)
03809         TCK = CallInst::TCK_Tail;
03810       if (CCInfo & (1 << 14))
03811         TCK = CallInst::TCK_MustTail;
03812       cast<CallInst>(I)->setTailCallKind(TCK);
03813       cast<CallInst>(I)->setAttributes(PAL);
03814       break;
03815     }
03816     case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
03817       if (Record.size() < 3)
03818         return Error("Invalid record");
03819       Type *OpTy = getTypeByID(Record[0]);
03820       Value *Op = getValue(Record, 1, NextValueNo, OpTy);
03821       Type *ResTy = getTypeByID(Record[2]);
03822       if (!OpTy || !Op || !ResTy)
03823         return Error("Invalid record");
03824       I = new VAArgInst(Op, ResTy);
03825       InstructionList.push_back(I);
03826       break;
03827     }
03828     }
03829 
03830     // Add instruction to end of current BB.  If there is no current BB, reject
03831     // this file.
03832     if (!CurBB) {
03833       delete I;
03834       return Error("Invalid instruction with no BB");
03835     }
03836     CurBB->getInstList().push_back(I);
03837 
03838     // If this was a terminator instruction, move to the next block.
03839     if (isa<TerminatorInst>(I)) {
03840       ++CurBBNo;
03841       CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
03842     }
03843 
03844     // Non-void values get registered in the value table for future use.
03845     if (I && !I->getType()->isVoidTy())
03846       ValueList.AssignValue(I, NextValueNo++);
03847   }
03848 
03849 OutOfRecordLoop:
03850 
03851   // Check the function list for unresolved values.
03852   if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
03853     if (!A->getParent()) {
03854       // We found at least one unresolved value.  Nuke them all to avoid leaks.
03855       for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
03856         if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
03857           A->replaceAllUsesWith(UndefValue::get(A->getType()));
03858           delete A;
03859         }
03860       }
03861       return Error("Never resolved value found in function");
03862     }
03863   }
03864 
03865   // FIXME: Check for unresolved forward-declared metadata references
03866   // and clean up leaks.
03867 
03868   // Trim the value list down to the size it was before we parsed this function.
03869   ValueList.shrinkTo(ModuleValueListSize);
03870   MDValueList.shrinkTo(ModuleMDValueListSize);
03871   std::vector<BasicBlock*>().swap(FunctionBBs);
03872   return std::error_code();
03873 }
03874 
03875 /// Find the function body in the bitcode stream
03876 std::error_code BitcodeReader::FindFunctionInStream(
03877     Function *F,
03878     DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
03879   while (DeferredFunctionInfoIterator->second == 0) {
03880     if (Stream.AtEndOfStream())
03881       return Error("Could not find function in stream");
03882     // ParseModule will parse the next body in the stream and set its
03883     // position in the DeferredFunctionInfo map.
03884     if (std::error_code EC = ParseModule(true))
03885       return EC;
03886   }
03887   return std::error_code();
03888 }
03889 
03890 //===----------------------------------------------------------------------===//
03891 // GVMaterializer implementation
03892 //===----------------------------------------------------------------------===//
03893 
03894 void BitcodeReader::releaseBuffer() { Buffer.release(); }
03895 
03896 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
03897   Function *F = dyn_cast<Function>(GV);
03898   // If it's not a function or is already material, ignore the request.
03899   if (!F || !F->isMaterializable())
03900     return std::error_code();
03901 
03902   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
03903   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
03904   // If its position is recorded as 0, its body is somewhere in the stream
03905   // but we haven't seen it yet.
03906   if (DFII->second == 0 && LazyStreamer)
03907     if (std::error_code EC = FindFunctionInStream(F, DFII))
03908       return EC;
03909 
03910   // Move the bit stream to the saved position of the deferred function body.
03911   Stream.JumpToBit(DFII->second);
03912 
03913   if (std::error_code EC = ParseFunctionBody(F))
03914     return EC;
03915   F->setIsMaterializable(false);
03916 
03917   // Upgrade any old intrinsic calls in the function.
03918   for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
03919        E = UpgradedIntrinsics.end(); I != E; ++I) {
03920     if (I->first != I->second) {
03921       for (auto UI = I->first->user_begin(), UE = I->first->user_end();
03922            UI != UE;) {
03923         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
03924           UpgradeIntrinsicCall(CI, I->second);
03925       }
03926     }
03927   }
03928 
03929   // Bring in any functions that this function forward-referenced via
03930   // blockaddresses.
03931   return materializeForwardReferencedFunctions();
03932 }
03933 
03934 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
03935   const Function *F = dyn_cast<Function>(GV);
03936   if (!F || F->isDeclaration())
03937     return false;
03938 
03939   // Dematerializing F would leave dangling references that wouldn't be
03940   // reconnected on re-materialization.
03941   if (BlockAddressesTaken.count(F))
03942     return false;
03943 
03944   return DeferredFunctionInfo.count(const_cast<Function*>(F));
03945 }
03946 
03947 void BitcodeReader::Dematerialize(GlobalValue *GV) {
03948   Function *F = dyn_cast<Function>(GV);
03949   // If this function isn't dematerializable, this is a noop.
03950   if (!F || !isDematerializable(F))
03951     return;
03952 
03953   assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
03954 
03955   // Just forget the function body, we can remat it later.
03956   F->dropAllReferences();
03957   F->setIsMaterializable(true);
03958 }
03959 
03960 std::error_code BitcodeReader::MaterializeModule(Module *M) {
03961   assert(M == TheModule &&
03962          "Can only Materialize the Module this BitcodeReader is attached to.");
03963 
03964   // Promise to materialize all forward references.
03965   WillMaterializeAllForwardRefs = true;
03966 
03967   // Iterate over the module, deserializing any functions that are still on
03968   // disk.
03969   for (Module::iterator F = TheModule->begin(), E = TheModule->end();
03970        F != E; ++F) {
03971     if (std::error_code EC = materialize(F))
03972       return EC;
03973   }
03974   // At this point, if there are any function bodies, the current bit is
03975   // pointing to the END_BLOCK record after them. Now make sure the rest
03976   // of the bits in the module have been read.
03977   if (NextUnreadBit)
03978     ParseModule(true);
03979 
03980   // Check that all block address forward references got resolved (as we
03981   // promised above).
03982   if (!BasicBlockFwdRefs.empty())
03983     return Error("Never resolved function from blockaddress");
03984 
03985   // Upgrade any intrinsic calls that slipped through (should not happen!) and
03986   // delete the old functions to clean up. We can't do this unless the entire
03987   // module is materialized because there could always be another function body
03988   // with calls to the old function.
03989   for (std::vector<std::pair<Function*, Function*> >::iterator I =
03990        UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
03991     if (I->first != I->second) {
03992       for (auto UI = I->first->user_begin(), UE = I->first->user_end();
03993            UI != UE;) {
03994         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
03995           UpgradeIntrinsicCall(CI, I->second);
03996       }
03997       if (!I->first->use_empty())
03998         I->first->replaceAllUsesWith(I->second);
03999       I->first->eraseFromParent();
04000     }
04001   }
04002   std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
04003 
04004   for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
04005     UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
04006 
04007   UpgradeDebugInfo(*M);
04008   return std::error_code();
04009 }
04010 
04011 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
04012   return IdentifiedStructTypes;
04013 }
04014 
04015 std::error_code BitcodeReader::InitStream() {
04016   if (LazyStreamer)
04017     return InitLazyStream();
04018   return InitStreamFromBuffer();
04019 }
04020 
04021 std::error_code BitcodeReader::InitStreamFromBuffer() {
04022   const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
04023   const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
04024 
04025   if (Buffer->getBufferSize() & 3)
04026     return Error("Invalid bitcode signature");
04027 
04028   // If we have a wrapper header, parse it and ignore the non-bc file contents.
04029   // The magic number is 0x0B17C0DE stored in little endian.
04030   if (isBitcodeWrapper(BufPtr, BufEnd))
04031     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
04032       return Error("Invalid bitcode wrapper header");
04033 
04034   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
04035   Stream.init(&*StreamFile);
04036 
04037   return std::error_code();
04038 }
04039 
04040 std::error_code BitcodeReader::InitLazyStream() {
04041   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
04042   // see it.
04043   auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer);
04044   StreamingMemoryObject &Bytes = *OwnedBytes;
04045   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
04046   Stream.init(&*StreamFile);
04047 
04048   unsigned char buf[16];
04049   if (Bytes.readBytes(buf, 16, 0) != 16)
04050     return Error("Invalid bitcode signature");
04051 
04052   if (!isBitcode(buf, buf + 16))
04053     return Error("Invalid bitcode signature");
04054 
04055   if (isBitcodeWrapper(buf, buf + 4)) {
04056     const unsigned char *bitcodeStart = buf;
04057     const unsigned char *bitcodeEnd = buf + 16;
04058     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
04059     Bytes.dropLeadingBytes(bitcodeStart - buf);
04060     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
04061   }
04062   return std::error_code();
04063 }
04064 
04065 namespace {
04066 class BitcodeErrorCategoryType : public std::error_category {
04067   const char *name() const LLVM_NOEXCEPT override {
04068     return "llvm.bitcode";
04069   }
04070   std::string message(int IE) const override {
04071     BitcodeError E = static_cast<BitcodeError>(IE);
04072     switch (E) {
04073     case BitcodeError::InvalidBitcodeSignature:
04074       return "Invalid bitcode signature";
04075     case BitcodeError::CorruptedBitcode:
04076       return "Corrupted bitcode";
04077     }
04078     llvm_unreachable("Unknown error type!");
04079   }
04080 };
04081 }
04082 
04083 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
04084 
04085 const std::error_category &llvm::BitcodeErrorCategory() {
04086   return *ErrorCategory;
04087 }
04088 
04089 //===----------------------------------------------------------------------===//
04090 // External interface
04091 //===----------------------------------------------------------------------===//
04092 
04093 /// \brief Get a lazy one-at-time loading module from bitcode.
04094 ///
04095 /// This isn't always used in a lazy context.  In particular, it's also used by
04096 /// \a parseBitcodeFile().  If this is truly lazy, then we need to eagerly pull
04097 /// in forward-referenced functions from block address references.
04098 ///
04099 /// \param[in] WillMaterializeAll Set to \c true if the caller promises to
04100 /// materialize everything -- in particular, if this isn't truly lazy.
04101 static ErrorOr<Module *>
04102 getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
04103                          LLVMContext &Context, bool WillMaterializeAll,
04104                          DiagnosticHandlerFunction DiagnosticHandler) {
04105   Module *M = new Module(Buffer->getBufferIdentifier(), Context);
04106   BitcodeReader *R =
04107       new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
04108   M->setMaterializer(R);
04109 
04110   auto cleanupOnError = [&](std::error_code EC) {
04111     R->releaseBuffer(); // Never take ownership on error.
04112     delete M;  // Also deletes R.
04113     return EC;
04114   };
04115 
04116   if (std::error_code EC = R->ParseBitcodeInto(M))
04117     return cleanupOnError(EC);
04118 
04119   if (!WillMaterializeAll)
04120     // Resolve forward references from blockaddresses.
04121     if (std::error_code EC = R->materializeForwardReferencedFunctions())
04122       return cleanupOnError(EC);
04123 
04124   Buffer.release(); // The BitcodeReader owns it now.
04125   return M;
04126 }
04127 
04128 ErrorOr<Module *>
04129 llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
04130                            LLVMContext &Context,
04131                            DiagnosticHandlerFunction DiagnosticHandler) {
04132   return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
04133                                   DiagnosticHandler);
04134 }
04135 
04136 ErrorOr<std::unique_ptr<Module>>
04137 llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
04138                                LLVMContext &Context,
04139                                DiagnosticHandlerFunction DiagnosticHandler) {
04140   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
04141   BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler);
04142   M->setMaterializer(R);
04143   if (std::error_code EC = R->ParseBitcodeInto(M.get()))
04144     return EC;
04145   return std::move(M);
04146 }
04147 
04148 ErrorOr<Module *>
04149 llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
04150                        DiagnosticHandlerFunction DiagnosticHandler) {
04151   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
04152   ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl(
04153       std::move(Buf), Context, true, DiagnosticHandler);
04154   if (!ModuleOrErr)
04155     return ModuleOrErr;
04156   Module *M = ModuleOrErr.get();
04157   // Read in the entire module, and destroy the BitcodeReader.
04158   if (std::error_code EC = M->materializeAllPermanently()) {
04159     delete M;
04160     return EC;
04161   }
04162 
04163   // TODO: Restore the use-lists to the in-memory state when the bitcode was
04164   // written.  We must defer until the Module has been fully materialized.
04165 
04166   return M;
04167 }
04168 
04169 std::string
04170 llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
04171                              DiagnosticHandlerFunction DiagnosticHandler) {
04172   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
04173   auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context,
04174                                             DiagnosticHandler);
04175   ErrorOr<std::string> Triple = R->parseTriple();
04176   if (Triple.getError())
04177     return "";
04178   return Triple.get();
04179 }