Add testing for mismatched explicit type on a load instruction when loading from...
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.cpp
1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Bitcode/ReaderWriter.h"
11 #include "BitcodeReader.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "llvm/IR/AutoUpgrade.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DebugInfoMetadata.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/OperandTraits.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/Support/DataStream.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 using namespace llvm;
35
36 enum {
37   SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
38 };
39
40 BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
41                                              DiagnosticSeverity Severity,
42                                              const Twine &Msg)
43     : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
44
45 void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
46
47 static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
48                              std::error_code EC, const Twine &Message) {
49   BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
50   DiagnosticHandler(DI);
51   return EC;
52 }
53
54 static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
55                              std::error_code EC) {
56   return Error(DiagnosticHandler, EC, EC.message());
57 }
58
59 std::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) {
60   return ::Error(DiagnosticHandler, make_error_code(E), Message);
61 }
62
63 std::error_code BitcodeReader::Error(const Twine &Message) {
64   return ::Error(DiagnosticHandler,
65                  make_error_code(BitcodeError::CorruptedBitcode), Message);
66 }
67
68 std::error_code BitcodeReader::Error(BitcodeError E) {
69   return ::Error(DiagnosticHandler, make_error_code(E));
70 }
71
72 static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
73                                                 LLVMContext &C) {
74   if (F)
75     return F;
76   return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
77 }
78
79 BitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
80                              DiagnosticHandlerFunction DiagnosticHandler)
81     : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
82       TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
83       NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
84       MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
85       WillMaterializeAllForwardRefs(false), IsMetadataMaterialized(false) {}
86
87 BitcodeReader::BitcodeReader(DataStreamer *streamer, LLVMContext &C,
88                              DiagnosticHandlerFunction DiagnosticHandler)
89     : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
90       TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer),
91       NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
92       MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
93       WillMaterializeAllForwardRefs(false), IsMetadataMaterialized(false) {}
94
95 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
96   if (WillMaterializeAllForwardRefs)
97     return std::error_code();
98
99   // Prevent recursion.
100   WillMaterializeAllForwardRefs = true;
101
102   while (!BasicBlockFwdRefQueue.empty()) {
103     Function *F = BasicBlockFwdRefQueue.front();
104     BasicBlockFwdRefQueue.pop_front();
105     assert(F && "Expected valid function");
106     if (!BasicBlockFwdRefs.count(F))
107       // Already materialized.
108       continue;
109
110     // Check for a function that isn't materializable to prevent an infinite
111     // loop.  When parsing a blockaddress stored in a global variable, there
112     // isn't a trivial way to check if a function will have a body without a
113     // linear search through FunctionsWithBodies, so just check it here.
114     if (!F->isMaterializable())
115       return Error("Never resolved function from blockaddress");
116
117     // Try to materialize F.
118     if (std::error_code EC = materialize(F))
119       return EC;
120   }
121   assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
122
123   // Reset state.
124   WillMaterializeAllForwardRefs = false;
125   return std::error_code();
126 }
127
128 void BitcodeReader::FreeState() {
129   Buffer = nullptr;
130   std::vector<Type*>().swap(TypeList);
131   ValueList.clear();
132   MDValueList.clear();
133   std::vector<Comdat *>().swap(ComdatList);
134
135   std::vector<AttributeSet>().swap(MAttributes);
136   std::vector<BasicBlock*>().swap(FunctionBBs);
137   std::vector<Function*>().swap(FunctionsWithBodies);
138   DeferredFunctionInfo.clear();
139   DeferredMetadataInfo.clear();
140   MDKindMap.clear();
141
142   assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
143   BasicBlockFwdRefQueue.clear();
144 }
145
146 //===----------------------------------------------------------------------===//
147 //  Helper functions to implement forward reference resolution, etc.
148 //===----------------------------------------------------------------------===//
149
150 /// ConvertToString - Convert a string from a record into an std::string, return
151 /// true on failure.
152 template<typename StrTy>
153 static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
154                             StrTy &Result) {
155   if (Idx > Record.size())
156     return true;
157
158   for (unsigned i = Idx, e = Record.size(); i != e; ++i)
159     Result += (char)Record[i];
160   return false;
161 }
162
163 static bool hasImplicitComdat(size_t Val) {
164   switch (Val) {
165   default:
166     return false;
167   case 1:  // Old WeakAnyLinkage
168   case 4:  // Old LinkOnceAnyLinkage
169   case 10: // Old WeakODRLinkage
170   case 11: // Old LinkOnceODRLinkage
171     return true;
172   }
173 }
174
175 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
176   switch (Val) {
177   default: // Map unknown/new linkages to external
178   case 0:
179     return GlobalValue::ExternalLinkage;
180   case 2:
181     return GlobalValue::AppendingLinkage;
182   case 3:
183     return GlobalValue::InternalLinkage;
184   case 5:
185     return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
186   case 6:
187     return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
188   case 7:
189     return GlobalValue::ExternalWeakLinkage;
190   case 8:
191     return GlobalValue::CommonLinkage;
192   case 9:
193     return GlobalValue::PrivateLinkage;
194   case 12:
195     return GlobalValue::AvailableExternallyLinkage;
196   case 13:
197     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
198   case 14:
199     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
200   case 15:
201     return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
202   case 1: // Old value with implicit comdat.
203   case 16:
204     return GlobalValue::WeakAnyLinkage;
205   case 10: // Old value with implicit comdat.
206   case 17:
207     return GlobalValue::WeakODRLinkage;
208   case 4: // Old value with implicit comdat.
209   case 18:
210     return GlobalValue::LinkOnceAnyLinkage;
211   case 11: // Old value with implicit comdat.
212   case 19:
213     return GlobalValue::LinkOnceODRLinkage;
214   }
215 }
216
217 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
218   switch (Val) {
219   default: // Map unknown visibilities to default.
220   case 0: return GlobalValue::DefaultVisibility;
221   case 1: return GlobalValue::HiddenVisibility;
222   case 2: return GlobalValue::ProtectedVisibility;
223   }
224 }
225
226 static GlobalValue::DLLStorageClassTypes
227 GetDecodedDLLStorageClass(unsigned Val) {
228   switch (Val) {
229   default: // Map unknown values to default.
230   case 0: return GlobalValue::DefaultStorageClass;
231   case 1: return GlobalValue::DLLImportStorageClass;
232   case 2: return GlobalValue::DLLExportStorageClass;
233   }
234 }
235
236 static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
237   switch (Val) {
238     case 0: return GlobalVariable::NotThreadLocal;
239     default: // Map unknown non-zero value to general dynamic.
240     case 1: return GlobalVariable::GeneralDynamicTLSModel;
241     case 2: return GlobalVariable::LocalDynamicTLSModel;
242     case 3: return GlobalVariable::InitialExecTLSModel;
243     case 4: return GlobalVariable::LocalExecTLSModel;
244   }
245 }
246
247 static int GetDecodedCastOpcode(unsigned Val) {
248   switch (Val) {
249   default: return -1;
250   case bitc::CAST_TRUNC   : return Instruction::Trunc;
251   case bitc::CAST_ZEXT    : return Instruction::ZExt;
252   case bitc::CAST_SEXT    : return Instruction::SExt;
253   case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
254   case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
255   case bitc::CAST_UITOFP  : return Instruction::UIToFP;
256   case bitc::CAST_SITOFP  : return Instruction::SIToFP;
257   case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
258   case bitc::CAST_FPEXT   : return Instruction::FPExt;
259   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
260   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
261   case bitc::CAST_BITCAST : return Instruction::BitCast;
262   case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
263   }
264 }
265 static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
266   switch (Val) {
267   default: return -1;
268   case bitc::BINOP_ADD:
269     return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
270   case bitc::BINOP_SUB:
271     return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
272   case bitc::BINOP_MUL:
273     return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
274   case bitc::BINOP_UDIV: return Instruction::UDiv;
275   case bitc::BINOP_SDIV:
276     return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
277   case bitc::BINOP_UREM: return Instruction::URem;
278   case bitc::BINOP_SREM:
279     return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
280   case bitc::BINOP_SHL:  return Instruction::Shl;
281   case bitc::BINOP_LSHR: return Instruction::LShr;
282   case bitc::BINOP_ASHR: return Instruction::AShr;
283   case bitc::BINOP_AND:  return Instruction::And;
284   case bitc::BINOP_OR:   return Instruction::Or;
285   case bitc::BINOP_XOR:  return Instruction::Xor;
286   }
287 }
288
289 static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
290   switch (Val) {
291   default: return AtomicRMWInst::BAD_BINOP;
292   case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
293   case bitc::RMW_ADD: return AtomicRMWInst::Add;
294   case bitc::RMW_SUB: return AtomicRMWInst::Sub;
295   case bitc::RMW_AND: return AtomicRMWInst::And;
296   case bitc::RMW_NAND: return AtomicRMWInst::Nand;
297   case bitc::RMW_OR: return AtomicRMWInst::Or;
298   case bitc::RMW_XOR: return AtomicRMWInst::Xor;
299   case bitc::RMW_MAX: return AtomicRMWInst::Max;
300   case bitc::RMW_MIN: return AtomicRMWInst::Min;
301   case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
302   case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
303   }
304 }
305
306 static AtomicOrdering GetDecodedOrdering(unsigned Val) {
307   switch (Val) {
308   case bitc::ORDERING_NOTATOMIC: return NotAtomic;
309   case bitc::ORDERING_UNORDERED: return Unordered;
310   case bitc::ORDERING_MONOTONIC: return Monotonic;
311   case bitc::ORDERING_ACQUIRE: return Acquire;
312   case bitc::ORDERING_RELEASE: return Release;
313   case bitc::ORDERING_ACQREL: return AcquireRelease;
314   default: // Map unknown orderings to sequentially-consistent.
315   case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
316   }
317 }
318
319 static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
320   switch (Val) {
321   case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
322   default: // Map unknown scopes to cross-thread.
323   case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
324   }
325 }
326
327 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
328   switch (Val) {
329   default: // Map unknown selection kinds to any.
330   case bitc::COMDAT_SELECTION_KIND_ANY:
331     return Comdat::Any;
332   case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
333     return Comdat::ExactMatch;
334   case bitc::COMDAT_SELECTION_KIND_LARGEST:
335     return Comdat::Largest;
336   case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
337     return Comdat::NoDuplicates;
338   case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
339     return Comdat::SameSize;
340   }
341 }
342
343 static void UpgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
344   switch (Val) {
345   case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
346   case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
347   }
348 }
349
350 namespace llvm {
351 namespace {
352   /// @brief A class for maintaining the slot number definition
353   /// as a placeholder for the actual definition for forward constants defs.
354   class ConstantPlaceHolder : public ConstantExpr {
355     void operator=(const ConstantPlaceHolder &) = delete;
356   public:
357     // allocate space for exactly one operand
358     void *operator new(size_t s) {
359       return User::operator new(s, 1);
360     }
361     explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
362       : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
363       Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
364     }
365
366     /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
367     static bool classof(const Value *V) {
368       return isa<ConstantExpr>(V) &&
369              cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
370     }
371
372
373     /// Provide fast operand accessors
374     DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
375   };
376 }
377
378 // FIXME: can we inherit this from ConstantExpr?
379 template <>
380 struct OperandTraits<ConstantPlaceHolder> :
381   public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
382 };
383 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
384 }
385
386
387 void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
388   if (Idx == size()) {
389     push_back(V);
390     return;
391   }
392
393   if (Idx >= size())
394     resize(Idx+1);
395
396   WeakVH &OldV = ValuePtrs[Idx];
397   if (!OldV) {
398     OldV = V;
399     return;
400   }
401
402   // Handle constants and non-constants (e.g. instrs) differently for
403   // efficiency.
404   if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
405     ResolveConstants.push_back(std::make_pair(PHC, Idx));
406     OldV = V;
407   } else {
408     // If there was a forward reference to this value, replace it.
409     Value *PrevVal = OldV;
410     OldV->replaceAllUsesWith(V);
411     delete PrevVal;
412   }
413 }
414
415
416 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
417                                                     Type *Ty) {
418   if (Idx >= size())
419     resize(Idx + 1);
420
421   if (Value *V = ValuePtrs[Idx]) {
422     assert(Ty == V->getType() && "Type mismatch in constant table!");
423     return cast<Constant>(V);
424   }
425
426   // Create and return a placeholder, which will later be RAUW'd.
427   Constant *C = new ConstantPlaceHolder(Ty, Context);
428   ValuePtrs[Idx] = C;
429   return C;
430 }
431
432 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
433   if (Idx >= size())
434     resize(Idx + 1);
435
436   if (Value *V = ValuePtrs[Idx]) {
437     assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
438     return V;
439   }
440
441   // No type specified, must be invalid reference.
442   if (!Ty) return nullptr;
443
444   // Create and return a placeholder, which will later be RAUW'd.
445   Value *V = new Argument(Ty);
446   ValuePtrs[Idx] = V;
447   return V;
448 }
449
450 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
451 /// resolves any forward references.  The idea behind this is that we sometimes
452 /// get constants (such as large arrays) which reference *many* forward ref
453 /// constants.  Replacing each of these causes a lot of thrashing when
454 /// building/reuniquing the constant.  Instead of doing this, we look at all the
455 /// uses and rewrite all the place holders at once for any constant that uses
456 /// a placeholder.
457 void BitcodeReaderValueList::ResolveConstantForwardRefs() {
458   // Sort the values by-pointer so that they are efficient to look up with a
459   // binary search.
460   std::sort(ResolveConstants.begin(), ResolveConstants.end());
461
462   SmallVector<Constant*, 64> NewOps;
463
464   while (!ResolveConstants.empty()) {
465     Value *RealVal = operator[](ResolveConstants.back().second);
466     Constant *Placeholder = ResolveConstants.back().first;
467     ResolveConstants.pop_back();
468
469     // Loop over all users of the placeholder, updating them to reference the
470     // new value.  If they reference more than one placeholder, update them all
471     // at once.
472     while (!Placeholder->use_empty()) {
473       auto UI = Placeholder->user_begin();
474       User *U = *UI;
475
476       // If the using object isn't uniqued, just update the operands.  This
477       // handles instructions and initializers for global variables.
478       if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
479         UI.getUse().set(RealVal);
480         continue;
481       }
482
483       // Otherwise, we have a constant that uses the placeholder.  Replace that
484       // constant with a new constant that has *all* placeholder uses updated.
485       Constant *UserC = cast<Constant>(U);
486       for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
487            I != E; ++I) {
488         Value *NewOp;
489         if (!isa<ConstantPlaceHolder>(*I)) {
490           // Not a placeholder reference.
491           NewOp = *I;
492         } else if (*I == Placeholder) {
493           // Common case is that it just references this one placeholder.
494           NewOp = RealVal;
495         } else {
496           // Otherwise, look up the placeholder in ResolveConstants.
497           ResolveConstantsTy::iterator It =
498             std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
499                              std::pair<Constant*, unsigned>(cast<Constant>(*I),
500                                                             0));
501           assert(It != ResolveConstants.end() && It->first == *I);
502           NewOp = operator[](It->second);
503         }
504
505         NewOps.push_back(cast<Constant>(NewOp));
506       }
507
508       // Make the new constant.
509       Constant *NewC;
510       if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
511         NewC = ConstantArray::get(UserCA->getType(), NewOps);
512       } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
513         NewC = ConstantStruct::get(UserCS->getType(), NewOps);
514       } else if (isa<ConstantVector>(UserC)) {
515         NewC = ConstantVector::get(NewOps);
516       } else {
517         assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
518         NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
519       }
520
521       UserC->replaceAllUsesWith(NewC);
522       UserC->destroyConstant();
523       NewOps.clear();
524     }
525
526     // Update all ValueHandles, they should be the only users at this point.
527     Placeholder->replaceAllUsesWith(RealVal);
528     delete Placeholder;
529   }
530 }
531
532 void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
533   if (Idx == size()) {
534     push_back(MD);
535     return;
536   }
537
538   if (Idx >= size())
539     resize(Idx+1);
540
541   TrackingMDRef &OldMD = MDValuePtrs[Idx];
542   if (!OldMD) {
543     OldMD.reset(MD);
544     return;
545   }
546
547   // If there was a forward reference to this value, replace it.
548   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
549   PrevMD->replaceAllUsesWith(MD);
550   --NumFwdRefs;
551 }
552
553 Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
554   if (Idx >= size())
555     resize(Idx + 1);
556
557   if (Metadata *MD = MDValuePtrs[Idx])
558     return MD;
559
560   // Track forward refs to be resolved later.
561   if (AnyFwdRefs) {
562     MinFwdRef = std::min(MinFwdRef, Idx);
563     MaxFwdRef = std::max(MaxFwdRef, Idx);
564   } else {
565     AnyFwdRefs = true;
566     MinFwdRef = MaxFwdRef = Idx;
567   }
568   ++NumFwdRefs;
569
570   // Create and return a placeholder, which will later be RAUW'd.
571   Metadata *MD = MDNode::getTemporary(Context, None).release();
572   MDValuePtrs[Idx].reset(MD);
573   return MD;
574 }
575
576 void BitcodeReaderMDValueList::tryToResolveCycles() {
577   if (!AnyFwdRefs)
578     // Nothing to do.
579     return;
580
581   if (NumFwdRefs)
582     // Still forward references... can't resolve cycles.
583     return;
584
585   // Resolve any cycles.
586   for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
587     auto &MD = MDValuePtrs[I];
588     auto *N = dyn_cast_or_null<MDNode>(MD);
589     if (!N)
590       continue;
591
592     assert(!N->isTemporary() && "Unexpected forward reference");
593     N->resolveCycles();
594   }
595
596   // Make sure we return early again until there's another forward ref.
597   AnyFwdRefs = false;
598 }
599
600 Type *BitcodeReader::getTypeByID(unsigned ID) {
601   // The type table size is always specified correctly.
602   if (ID >= TypeList.size())
603     return nullptr;
604
605   if (Type *Ty = TypeList[ID])
606     return Ty;
607
608   // If we have a forward reference, the only possible case is when it is to a
609   // named struct.  Just create a placeholder for now.
610   return TypeList[ID] = createIdentifiedStructType(Context);
611 }
612
613 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
614                                                       StringRef Name) {
615   auto *Ret = StructType::create(Context, Name);
616   IdentifiedStructTypes.push_back(Ret);
617   return Ret;
618 }
619
620 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
621   auto *Ret = StructType::create(Context);
622   IdentifiedStructTypes.push_back(Ret);
623   return Ret;
624 }
625
626
627 //===----------------------------------------------------------------------===//
628 //  Functions for parsing blocks from the bitcode file
629 //===----------------------------------------------------------------------===//
630
631
632 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
633 /// been decoded from the given integer. This function must stay in sync with
634 /// 'encodeLLVMAttributesForBitcode'.
635 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
636                                            uint64_t EncodedAttrs) {
637   // FIXME: Remove in 4.0.
638
639   // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
640   // the bits above 31 down by 11 bits.
641   unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
642   assert((!Alignment || isPowerOf2_32(Alignment)) &&
643          "Alignment must be a power of two.");
644
645   if (Alignment)
646     B.addAlignmentAttr(Alignment);
647   B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
648                 (EncodedAttrs & 0xffff));
649 }
650
651 std::error_code BitcodeReader::ParseAttributeBlock() {
652   if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
653     return Error("Invalid record");
654
655   if (!MAttributes.empty())
656     return Error("Invalid multiple blocks");
657
658   SmallVector<uint64_t, 64> Record;
659
660   SmallVector<AttributeSet, 8> Attrs;
661
662   // Read all the records.
663   while (1) {
664     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
665
666     switch (Entry.Kind) {
667     case BitstreamEntry::SubBlock: // Handled for us already.
668     case BitstreamEntry::Error:
669       return Error("Malformed block");
670     case BitstreamEntry::EndBlock:
671       return std::error_code();
672     case BitstreamEntry::Record:
673       // The interesting case.
674       break;
675     }
676
677     // Read a record.
678     Record.clear();
679     switch (Stream.readRecord(Entry.ID, Record)) {
680     default:  // Default behavior: ignore.
681       break;
682     case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
683       // FIXME: Remove in 4.0.
684       if (Record.size() & 1)
685         return Error("Invalid record");
686
687       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
688         AttrBuilder B;
689         decodeLLVMAttributesForBitcode(B, Record[i+1]);
690         Attrs.push_back(AttributeSet::get(Context, Record[i], B));
691       }
692
693       MAttributes.push_back(AttributeSet::get(Context, Attrs));
694       Attrs.clear();
695       break;
696     }
697     case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
698       for (unsigned i = 0, e = Record.size(); i != e; ++i)
699         Attrs.push_back(MAttributeGroups[Record[i]]);
700
701       MAttributes.push_back(AttributeSet::get(Context, Attrs));
702       Attrs.clear();
703       break;
704     }
705     }
706   }
707 }
708
709 // Returns Attribute::None on unrecognized codes.
710 static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
711   switch (Code) {
712   default:
713     return Attribute::None;
714   case bitc::ATTR_KIND_ALIGNMENT:
715     return Attribute::Alignment;
716   case bitc::ATTR_KIND_ALWAYS_INLINE:
717     return Attribute::AlwaysInline;
718   case bitc::ATTR_KIND_BUILTIN:
719     return Attribute::Builtin;
720   case bitc::ATTR_KIND_BY_VAL:
721     return Attribute::ByVal;
722   case bitc::ATTR_KIND_IN_ALLOCA:
723     return Attribute::InAlloca;
724   case bitc::ATTR_KIND_COLD:
725     return Attribute::Cold;
726   case bitc::ATTR_KIND_INLINE_HINT:
727     return Attribute::InlineHint;
728   case bitc::ATTR_KIND_IN_REG:
729     return Attribute::InReg;
730   case bitc::ATTR_KIND_JUMP_TABLE:
731     return Attribute::JumpTable;
732   case bitc::ATTR_KIND_MIN_SIZE:
733     return Attribute::MinSize;
734   case bitc::ATTR_KIND_NAKED:
735     return Attribute::Naked;
736   case bitc::ATTR_KIND_NEST:
737     return Attribute::Nest;
738   case bitc::ATTR_KIND_NO_ALIAS:
739     return Attribute::NoAlias;
740   case bitc::ATTR_KIND_NO_BUILTIN:
741     return Attribute::NoBuiltin;
742   case bitc::ATTR_KIND_NO_CAPTURE:
743     return Attribute::NoCapture;
744   case bitc::ATTR_KIND_NO_DUPLICATE:
745     return Attribute::NoDuplicate;
746   case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
747     return Attribute::NoImplicitFloat;
748   case bitc::ATTR_KIND_NO_INLINE:
749     return Attribute::NoInline;
750   case bitc::ATTR_KIND_NON_LAZY_BIND:
751     return Attribute::NonLazyBind;
752   case bitc::ATTR_KIND_NON_NULL:
753     return Attribute::NonNull;
754   case bitc::ATTR_KIND_DEREFERENCEABLE:
755     return Attribute::Dereferenceable;
756   case bitc::ATTR_KIND_NO_RED_ZONE:
757     return Attribute::NoRedZone;
758   case bitc::ATTR_KIND_NO_RETURN:
759     return Attribute::NoReturn;
760   case bitc::ATTR_KIND_NO_UNWIND:
761     return Attribute::NoUnwind;
762   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
763     return Attribute::OptimizeForSize;
764   case bitc::ATTR_KIND_OPTIMIZE_NONE:
765     return Attribute::OptimizeNone;
766   case bitc::ATTR_KIND_READ_NONE:
767     return Attribute::ReadNone;
768   case bitc::ATTR_KIND_READ_ONLY:
769     return Attribute::ReadOnly;
770   case bitc::ATTR_KIND_RETURNED:
771     return Attribute::Returned;
772   case bitc::ATTR_KIND_RETURNS_TWICE:
773     return Attribute::ReturnsTwice;
774   case bitc::ATTR_KIND_S_EXT:
775     return Attribute::SExt;
776   case bitc::ATTR_KIND_STACK_ALIGNMENT:
777     return Attribute::StackAlignment;
778   case bitc::ATTR_KIND_STACK_PROTECT:
779     return Attribute::StackProtect;
780   case bitc::ATTR_KIND_STACK_PROTECT_REQ:
781     return Attribute::StackProtectReq;
782   case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
783     return Attribute::StackProtectStrong;
784   case bitc::ATTR_KIND_STRUCT_RET:
785     return Attribute::StructRet;
786   case bitc::ATTR_KIND_SANITIZE_ADDRESS:
787     return Attribute::SanitizeAddress;
788   case bitc::ATTR_KIND_SANITIZE_THREAD:
789     return Attribute::SanitizeThread;
790   case bitc::ATTR_KIND_SANITIZE_MEMORY:
791     return Attribute::SanitizeMemory;
792   case bitc::ATTR_KIND_UW_TABLE:
793     return Attribute::UWTable;
794   case bitc::ATTR_KIND_Z_EXT:
795     return Attribute::ZExt;
796   }
797 }
798
799 std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
800                                                    unsigned &Alignment) {
801   // Note: Alignment in bitcode files is incremented by 1, so that zero
802   // can be used for default alignment.
803   if (Exponent > Value::MaxAlignmentExponent + 1)
804     return Error("Invalid alignment value");
805   Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
806   return std::error_code();
807 }
808
809 std::error_code BitcodeReader::ParseAttrKind(uint64_t Code,
810                                              Attribute::AttrKind *Kind) {
811   *Kind = GetAttrFromCode(Code);
812   if (*Kind == Attribute::None)
813     return Error(BitcodeError::CorruptedBitcode,
814                  "Unknown attribute kind (" + Twine(Code) + ")");
815   return std::error_code();
816 }
817
818 std::error_code BitcodeReader::ParseAttributeGroupBlock() {
819   if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
820     return Error("Invalid record");
821
822   if (!MAttributeGroups.empty())
823     return Error("Invalid multiple blocks");
824
825   SmallVector<uint64_t, 64> Record;
826
827   // Read all the records.
828   while (1) {
829     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
830
831     switch (Entry.Kind) {
832     case BitstreamEntry::SubBlock: // Handled for us already.
833     case BitstreamEntry::Error:
834       return Error("Malformed block");
835     case BitstreamEntry::EndBlock:
836       return std::error_code();
837     case BitstreamEntry::Record:
838       // The interesting case.
839       break;
840     }
841
842     // Read a record.
843     Record.clear();
844     switch (Stream.readRecord(Entry.ID, Record)) {
845     default:  // Default behavior: ignore.
846       break;
847     case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
848       if (Record.size() < 3)
849         return Error("Invalid record");
850
851       uint64_t GrpID = Record[0];
852       uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
853
854       AttrBuilder B;
855       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
856         if (Record[i] == 0) {        // Enum attribute
857           Attribute::AttrKind Kind;
858           if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
859             return EC;
860
861           B.addAttribute(Kind);
862         } else if (Record[i] == 1) { // Integer attribute
863           Attribute::AttrKind Kind;
864           if (std::error_code EC = ParseAttrKind(Record[++i], &Kind))
865             return EC;
866           if (Kind == Attribute::Alignment)
867             B.addAlignmentAttr(Record[++i]);
868           else if (Kind == Attribute::StackAlignment)
869             B.addStackAlignmentAttr(Record[++i]);
870           else if (Kind == Attribute::Dereferenceable)
871             B.addDereferenceableAttr(Record[++i]);
872         } else {                     // String attribute
873           assert((Record[i] == 3 || Record[i] == 4) &&
874                  "Invalid attribute group entry");
875           bool HasValue = (Record[i++] == 4);
876           SmallString<64> KindStr;
877           SmallString<64> ValStr;
878
879           while (Record[i] != 0 && i != e)
880             KindStr += Record[i++];
881           assert(Record[i] == 0 && "Kind string not null terminated");
882
883           if (HasValue) {
884             // Has a value associated with it.
885             ++i; // Skip the '0' that terminates the "kind" string.
886             while (Record[i] != 0 && i != e)
887               ValStr += Record[i++];
888             assert(Record[i] == 0 && "Value string not null terminated");
889           }
890
891           B.addAttribute(KindStr.str(), ValStr.str());
892         }
893       }
894
895       MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
896       break;
897     }
898     }
899   }
900 }
901
902 std::error_code BitcodeReader::ParseTypeTable() {
903   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
904     return Error("Invalid record");
905
906   return ParseTypeTableBody();
907 }
908
909 std::error_code BitcodeReader::ParseTypeTableBody() {
910   if (!TypeList.empty())
911     return Error("Invalid multiple blocks");
912
913   SmallVector<uint64_t, 64> Record;
914   unsigned NumRecords = 0;
915
916   SmallString<64> TypeName;
917
918   // Read all the records for this type table.
919   while (1) {
920     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
921
922     switch (Entry.Kind) {
923     case BitstreamEntry::SubBlock: // Handled for us already.
924     case BitstreamEntry::Error:
925       return Error("Malformed block");
926     case BitstreamEntry::EndBlock:
927       if (NumRecords != TypeList.size())
928         return Error("Malformed block");
929       return std::error_code();
930     case BitstreamEntry::Record:
931       // The interesting case.
932       break;
933     }
934
935     // Read a record.
936     Record.clear();
937     Type *ResultTy = nullptr;
938     switch (Stream.readRecord(Entry.ID, Record)) {
939     default:
940       return Error("Invalid value");
941     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
942       // TYPE_CODE_NUMENTRY contains a count of the number of types in the
943       // type list.  This allows us to reserve space.
944       if (Record.size() < 1)
945         return Error("Invalid record");
946       TypeList.resize(Record[0]);
947       continue;
948     case bitc::TYPE_CODE_VOID:      // VOID
949       ResultTy = Type::getVoidTy(Context);
950       break;
951     case bitc::TYPE_CODE_HALF:     // HALF
952       ResultTy = Type::getHalfTy(Context);
953       break;
954     case bitc::TYPE_CODE_FLOAT:     // FLOAT
955       ResultTy = Type::getFloatTy(Context);
956       break;
957     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
958       ResultTy = Type::getDoubleTy(Context);
959       break;
960     case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
961       ResultTy = Type::getX86_FP80Ty(Context);
962       break;
963     case bitc::TYPE_CODE_FP128:     // FP128
964       ResultTy = Type::getFP128Ty(Context);
965       break;
966     case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
967       ResultTy = Type::getPPC_FP128Ty(Context);
968       break;
969     case bitc::TYPE_CODE_LABEL:     // LABEL
970       ResultTy = Type::getLabelTy(Context);
971       break;
972     case bitc::TYPE_CODE_METADATA:  // METADATA
973       ResultTy = Type::getMetadataTy(Context);
974       break;
975     case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
976       ResultTy = Type::getX86_MMXTy(Context);
977       break;
978     case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
979       if (Record.size() < 1)
980         return Error("Invalid record");
981
982       uint64_t NumBits = Record[0];
983       if (NumBits < IntegerType::MIN_INT_BITS ||
984           NumBits > IntegerType::MAX_INT_BITS)
985         return Error("Bitwidth for integer type out of range");
986       ResultTy = IntegerType::get(Context, NumBits);
987       break;
988     }
989     case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
990                                     //          [pointee type, address space]
991       if (Record.size() < 1)
992         return Error("Invalid record");
993       unsigned AddressSpace = 0;
994       if (Record.size() == 2)
995         AddressSpace = Record[1];
996       ResultTy = getTypeByID(Record[0]);
997       if (!ResultTy)
998         return Error("Invalid type");
999       ResultTy = PointerType::get(ResultTy, AddressSpace);
1000       break;
1001     }
1002     case bitc::TYPE_CODE_FUNCTION_OLD: {
1003       // FIXME: attrid is dead, remove it in LLVM 4.0
1004       // FUNCTION: [vararg, attrid, retty, paramty x N]
1005       if (Record.size() < 3)
1006         return Error("Invalid record");
1007       SmallVector<Type*, 8> ArgTys;
1008       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1009         if (Type *T = getTypeByID(Record[i]))
1010           ArgTys.push_back(T);
1011         else
1012           break;
1013       }
1014
1015       ResultTy = getTypeByID(Record[2]);
1016       if (!ResultTy || ArgTys.size() < Record.size()-3)
1017         return Error("Invalid type");
1018
1019       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1020       break;
1021     }
1022     case bitc::TYPE_CODE_FUNCTION: {
1023       // FUNCTION: [vararg, retty, paramty x N]
1024       if (Record.size() < 2)
1025         return Error("Invalid record");
1026       SmallVector<Type*, 8> ArgTys;
1027       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1028         if (Type *T = getTypeByID(Record[i]))
1029           ArgTys.push_back(T);
1030         else
1031           break;
1032       }
1033
1034       ResultTy = getTypeByID(Record[1]);
1035       if (!ResultTy || ArgTys.size() < Record.size()-2)
1036         return Error("Invalid type");
1037
1038       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1039       break;
1040     }
1041     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
1042       if (Record.size() < 1)
1043         return Error("Invalid record");
1044       SmallVector<Type*, 8> EltTys;
1045       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1046         if (Type *T = getTypeByID(Record[i]))
1047           EltTys.push_back(T);
1048         else
1049           break;
1050       }
1051       if (EltTys.size() != Record.size()-1)
1052         return Error("Invalid type");
1053       ResultTy = StructType::get(Context, EltTys, Record[0]);
1054       break;
1055     }
1056     case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
1057       if (ConvertToString(Record, 0, TypeName))
1058         return Error("Invalid record");
1059       continue;
1060
1061     case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1062       if (Record.size() < 1)
1063         return Error("Invalid record");
1064
1065       if (NumRecords >= TypeList.size())
1066         return Error("Invalid TYPE table");
1067
1068       // Check to see if this was forward referenced, if so fill in the temp.
1069       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1070       if (Res) {
1071         Res->setName(TypeName);
1072         TypeList[NumRecords] = nullptr;
1073       } else  // Otherwise, create a new struct.
1074         Res = createIdentifiedStructType(Context, TypeName);
1075       TypeName.clear();
1076
1077       SmallVector<Type*, 8> EltTys;
1078       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1079         if (Type *T = getTypeByID(Record[i]))
1080           EltTys.push_back(T);
1081         else
1082           break;
1083       }
1084       if (EltTys.size() != Record.size()-1)
1085         return Error("Invalid record");
1086       Res->setBody(EltTys, Record[0]);
1087       ResultTy = Res;
1088       break;
1089     }
1090     case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
1091       if (Record.size() != 1)
1092         return Error("Invalid record");
1093
1094       if (NumRecords >= TypeList.size())
1095         return Error("Invalid TYPE table");
1096
1097       // Check to see if this was forward referenced, if so fill in the temp.
1098       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1099       if (Res) {
1100         Res->setName(TypeName);
1101         TypeList[NumRecords] = nullptr;
1102       } else  // Otherwise, create a new struct with no body.
1103         Res = createIdentifiedStructType(Context, TypeName);
1104       TypeName.clear();
1105       ResultTy = Res;
1106       break;
1107     }
1108     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
1109       if (Record.size() < 2)
1110         return Error("Invalid record");
1111       if ((ResultTy = getTypeByID(Record[1])))
1112         ResultTy = ArrayType::get(ResultTy, Record[0]);
1113       else
1114         return Error("Invalid type");
1115       break;
1116     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
1117       if (Record.size() < 2)
1118         return Error("Invalid record");
1119       if ((ResultTy = getTypeByID(Record[1])))
1120         ResultTy = VectorType::get(ResultTy, Record[0]);
1121       else
1122         return Error("Invalid type");
1123       break;
1124     }
1125
1126     if (NumRecords >= TypeList.size())
1127       return Error("Invalid TYPE table");
1128     if (TypeList[NumRecords])
1129       return Error(
1130           "Invalid TYPE table: Only named structs can be forward referenced");
1131     assert(ResultTy && "Didn't read a type?");
1132     TypeList[NumRecords++] = ResultTy;
1133   }
1134 }
1135
1136 std::error_code BitcodeReader::ParseValueSymbolTable() {
1137   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1138     return Error("Invalid record");
1139
1140   SmallVector<uint64_t, 64> Record;
1141
1142   Triple TT(TheModule->getTargetTriple());
1143
1144   // Read all the records for this value table.
1145   SmallString<128> ValueName;
1146   while (1) {
1147     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1148
1149     switch (Entry.Kind) {
1150     case BitstreamEntry::SubBlock: // Handled for us already.
1151     case BitstreamEntry::Error:
1152       return Error("Malformed block");
1153     case BitstreamEntry::EndBlock:
1154       return std::error_code();
1155     case BitstreamEntry::Record:
1156       // The interesting case.
1157       break;
1158     }
1159
1160     // Read a record.
1161     Record.clear();
1162     switch (Stream.readRecord(Entry.ID, Record)) {
1163     default:  // Default behavior: unknown type.
1164       break;
1165     case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N]
1166       if (ConvertToString(Record, 1, ValueName))
1167         return Error("Invalid record");
1168       unsigned ValueID = Record[0];
1169       if (ValueID >= ValueList.size() || !ValueList[ValueID])
1170         return Error("Invalid record");
1171       Value *V = ValueList[ValueID];
1172
1173       V->setName(StringRef(ValueName.data(), ValueName.size()));
1174       if (auto *GO = dyn_cast<GlobalObject>(V)) {
1175         if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1176           if (TT.isOSBinFormatMachO())
1177             GO->setComdat(nullptr);
1178           else
1179             GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1180         }
1181       }
1182       ValueName.clear();
1183       break;
1184     }
1185     case bitc::VST_CODE_BBENTRY: {
1186       if (ConvertToString(Record, 1, ValueName))
1187         return Error("Invalid record");
1188       BasicBlock *BB = getBasicBlock(Record[0]);
1189       if (!BB)
1190         return Error("Invalid record");
1191
1192       BB->setName(StringRef(ValueName.data(), ValueName.size()));
1193       ValueName.clear();
1194       break;
1195     }
1196     }
1197   }
1198 }
1199
1200 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
1201
1202 std::error_code BitcodeReader::ParseMetadata() {
1203   IsMetadataMaterialized = true;
1204   unsigned NextMDValueNo = MDValueList.size();
1205
1206   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
1207     return Error("Invalid record");
1208
1209   SmallVector<uint64_t, 64> Record;
1210
1211   auto getMD =
1212       [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); };
1213   auto getMDOrNull = [&](unsigned ID) -> Metadata *{
1214     if (ID)
1215       return getMD(ID - 1);
1216     return nullptr;
1217   };
1218   auto getMDString = [&](unsigned ID) -> MDString *{
1219     // This requires that the ID is not really a forward reference.  In
1220     // particular, the MDString must already have been resolved.
1221     return cast_or_null<MDString>(getMDOrNull(ID));
1222   };
1223
1224 #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS)                                 \
1225   (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1226
1227   // Read all the records.
1228   while (1) {
1229     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1230
1231     switch (Entry.Kind) {
1232     case BitstreamEntry::SubBlock: // Handled for us already.
1233     case BitstreamEntry::Error:
1234       return Error("Malformed block");
1235     case BitstreamEntry::EndBlock:
1236       MDValueList.tryToResolveCycles();
1237       return std::error_code();
1238     case BitstreamEntry::Record:
1239       // The interesting case.
1240       break;
1241     }
1242
1243     // Read a record.
1244     Record.clear();
1245     unsigned Code = Stream.readRecord(Entry.ID, Record);
1246     bool IsDistinct = false;
1247     switch (Code) {
1248     default:  // Default behavior: ignore.
1249       break;
1250     case bitc::METADATA_NAME: {
1251       // Read name of the named metadata.
1252       SmallString<8> Name(Record.begin(), Record.end());
1253       Record.clear();
1254       Code = Stream.ReadCode();
1255
1256       // METADATA_NAME is always followed by METADATA_NAMED_NODE.
1257       unsigned NextBitCode = Stream.readRecord(Code, Record);
1258       assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
1259
1260       // Read named metadata elements.
1261       unsigned Size = Record.size();
1262       NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
1263       for (unsigned i = 0; i != Size; ++i) {
1264         MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1265         if (!MD)
1266           return Error("Invalid record");
1267         NMD->addOperand(MD);
1268       }
1269       break;
1270     }
1271     case bitc::METADATA_OLD_FN_NODE: {
1272       // FIXME: Remove in 4.0.
1273       // This is a LocalAsMetadata record, the only type of function-local
1274       // metadata.
1275       if (Record.size() % 2 == 1)
1276         return Error("Invalid record");
1277
1278       // If this isn't a LocalAsMetadata record, we're dropping it.  This used
1279       // to be legal, but there's no upgrade path.
1280       auto dropRecord = [&] {
1281         MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++);
1282       };
1283       if (Record.size() != 2) {
1284         dropRecord();
1285         break;
1286       }
1287
1288       Type *Ty = getTypeByID(Record[0]);
1289       if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1290         dropRecord();
1291         break;
1292       }
1293
1294       MDValueList.AssignValue(
1295           LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1296           NextMDValueNo++);
1297       break;
1298     }
1299     case bitc::METADATA_OLD_NODE: {
1300       // FIXME: Remove in 4.0.
1301       if (Record.size() % 2 == 1)
1302         return Error("Invalid record");
1303
1304       unsigned Size = Record.size();
1305       SmallVector<Metadata *, 8> Elts;
1306       for (unsigned i = 0; i != Size; i += 2) {
1307         Type *Ty = getTypeByID(Record[i]);
1308         if (!Ty)
1309           return Error("Invalid record");
1310         if (Ty->isMetadataTy())
1311           Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
1312         else if (!Ty->isVoidTy()) {
1313           auto *MD =
1314               ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
1315           assert(isa<ConstantAsMetadata>(MD) &&
1316                  "Expected non-function-local metadata");
1317           Elts.push_back(MD);
1318         } else
1319           Elts.push_back(nullptr);
1320       }
1321       MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
1322       break;
1323     }
1324     case bitc::METADATA_VALUE: {
1325       if (Record.size() != 2)
1326         return Error("Invalid record");
1327
1328       Type *Ty = getTypeByID(Record[0]);
1329       if (Ty->isMetadataTy() || Ty->isVoidTy())
1330         return Error("Invalid record");
1331
1332       MDValueList.AssignValue(
1333           ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
1334           NextMDValueNo++);
1335       break;
1336     }
1337     case bitc::METADATA_DISTINCT_NODE:
1338       IsDistinct = true;
1339       // fallthrough...
1340     case bitc::METADATA_NODE: {
1341       SmallVector<Metadata *, 8> Elts;
1342       Elts.reserve(Record.size());
1343       for (unsigned ID : Record)
1344         Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
1345       MDValueList.AssignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
1346                                          : MDNode::get(Context, Elts),
1347                               NextMDValueNo++);
1348       break;
1349     }
1350     case bitc::METADATA_LOCATION: {
1351       if (Record.size() != 5)
1352         return Error("Invalid record");
1353
1354       auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get;
1355       unsigned Line = Record[1];
1356       unsigned Column = Record[2];
1357       MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
1358       Metadata *InlinedAt =
1359           Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
1360       MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt),
1361                               NextMDValueNo++);
1362       break;
1363     }
1364     case bitc::METADATA_GENERIC_DEBUG: {
1365       if (Record.size() < 4)
1366         return Error("Invalid record");
1367
1368       unsigned Tag = Record[1];
1369       unsigned Version = Record[2];
1370
1371       if (Tag >= 1u << 16 || Version != 0)
1372         return Error("Invalid record");
1373
1374       auto *Header = getMDString(Record[3]);
1375       SmallVector<Metadata *, 8> DwarfOps;
1376       for (unsigned I = 4, E = Record.size(); I != E; ++I)
1377         DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1)
1378                                      : nullptr);
1379       MDValueList.AssignValue(GET_OR_DISTINCT(GenericDebugNode, Record[0],
1380                                               (Context, Tag, Header, DwarfOps)),
1381                               NextMDValueNo++);
1382       break;
1383     }
1384     case bitc::METADATA_SUBRANGE: {
1385       if (Record.size() != 3)
1386         return Error("Invalid record");
1387
1388       MDValueList.AssignValue(
1389           GET_OR_DISTINCT(MDSubrange, Record[0],
1390                           (Context, Record[1], unrotateSign(Record[2]))),
1391           NextMDValueNo++);
1392       break;
1393     }
1394     case bitc::METADATA_ENUMERATOR: {
1395       if (Record.size() != 3)
1396         return Error("Invalid record");
1397
1398       MDValueList.AssignValue(GET_OR_DISTINCT(MDEnumerator, Record[0],
1399                                               (Context, unrotateSign(Record[1]),
1400                                                getMDString(Record[2]))),
1401                               NextMDValueNo++);
1402       break;
1403     }
1404     case bitc::METADATA_BASIC_TYPE: {
1405       if (Record.size() != 6)
1406         return Error("Invalid record");
1407
1408       MDValueList.AssignValue(
1409           GET_OR_DISTINCT(MDBasicType, Record[0],
1410                           (Context, Record[1], getMDString(Record[2]),
1411                            Record[3], Record[4], Record[5])),
1412           NextMDValueNo++);
1413       break;
1414     }
1415     case bitc::METADATA_DERIVED_TYPE: {
1416       if (Record.size() != 12)
1417         return Error("Invalid record");
1418
1419       MDValueList.AssignValue(
1420           GET_OR_DISTINCT(MDDerivedType, Record[0],
1421                           (Context, Record[1], getMDString(Record[2]),
1422                            getMDOrNull(Record[3]), Record[4],
1423                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
1424                            Record[7], Record[8], Record[9], Record[10],
1425                            getMDOrNull(Record[11]))),
1426           NextMDValueNo++);
1427       break;
1428     }
1429     case bitc::METADATA_COMPOSITE_TYPE: {
1430       if (Record.size() != 16)
1431         return Error("Invalid record");
1432
1433       MDValueList.AssignValue(
1434           GET_OR_DISTINCT(MDCompositeType, Record[0],
1435                           (Context, Record[1], getMDString(Record[2]),
1436                            getMDOrNull(Record[3]), Record[4],
1437                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
1438                            Record[7], Record[8], Record[9], Record[10],
1439                            getMDOrNull(Record[11]), Record[12],
1440                            getMDOrNull(Record[13]), getMDOrNull(Record[14]),
1441                            getMDString(Record[15]))),
1442           NextMDValueNo++);
1443       break;
1444     }
1445     case bitc::METADATA_SUBROUTINE_TYPE: {
1446       if (Record.size() != 3)
1447         return Error("Invalid record");
1448
1449       MDValueList.AssignValue(
1450           GET_OR_DISTINCT(MDSubroutineType, Record[0],
1451                           (Context, Record[1], getMDOrNull(Record[2]))),
1452           NextMDValueNo++);
1453       break;
1454     }
1455     case bitc::METADATA_FILE: {
1456       if (Record.size() != 3)
1457         return Error("Invalid record");
1458
1459       MDValueList.AssignValue(
1460           GET_OR_DISTINCT(MDFile, Record[0], (Context, getMDString(Record[1]),
1461                                               getMDString(Record[2]))),
1462           NextMDValueNo++);
1463       break;
1464     }
1465     case bitc::METADATA_COMPILE_UNIT: {
1466       if (Record.size() != 14)
1467         return Error("Invalid record");
1468
1469       MDValueList.AssignValue(
1470           GET_OR_DISTINCT(MDCompileUnit, Record[0],
1471                           (Context, Record[1], getMDOrNull(Record[2]),
1472                            getMDString(Record[3]), Record[4],
1473                            getMDString(Record[5]), Record[6],
1474                            getMDString(Record[7]), Record[8],
1475                            getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1476                            getMDOrNull(Record[11]), getMDOrNull(Record[12]),
1477                            getMDOrNull(Record[13]))),
1478           NextMDValueNo++);
1479       break;
1480     }
1481     case bitc::METADATA_SUBPROGRAM: {
1482       if (Record.size() != 19)
1483         return Error("Invalid record");
1484
1485       MDValueList.AssignValue(
1486           GET_OR_DISTINCT(
1487               MDSubprogram, Record[0],
1488               (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1489                getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1490                getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
1491                getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
1492                Record[14], getMDOrNull(Record[15]), getMDOrNull(Record[16]),
1493                getMDOrNull(Record[17]), getMDOrNull(Record[18]))),
1494           NextMDValueNo++);
1495       break;
1496     }
1497     case bitc::METADATA_LEXICAL_BLOCK: {
1498       if (Record.size() != 5)
1499         return Error("Invalid record");
1500
1501       MDValueList.AssignValue(
1502           GET_OR_DISTINCT(MDLexicalBlock, Record[0],
1503                           (Context, getMDOrNull(Record[1]),
1504                            getMDOrNull(Record[2]), Record[3], Record[4])),
1505           NextMDValueNo++);
1506       break;
1507     }
1508     case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1509       if (Record.size() != 4)
1510         return Error("Invalid record");
1511
1512       MDValueList.AssignValue(
1513           GET_OR_DISTINCT(MDLexicalBlockFile, Record[0],
1514                           (Context, getMDOrNull(Record[1]),
1515                            getMDOrNull(Record[2]), Record[3])),
1516           NextMDValueNo++);
1517       break;
1518     }
1519     case bitc::METADATA_NAMESPACE: {
1520       if (Record.size() != 5)
1521         return Error("Invalid record");
1522
1523       MDValueList.AssignValue(
1524           GET_OR_DISTINCT(MDNamespace, Record[0],
1525                           (Context, getMDOrNull(Record[1]),
1526                            getMDOrNull(Record[2]), getMDString(Record[3]),
1527                            Record[4])),
1528           NextMDValueNo++);
1529       break;
1530     }
1531     case bitc::METADATA_TEMPLATE_TYPE: {
1532       if (Record.size() != 3)
1533         return Error("Invalid record");
1534
1535       MDValueList.AssignValue(GET_OR_DISTINCT(MDTemplateTypeParameter,
1536                                               Record[0],
1537                                               (Context, getMDString(Record[1]),
1538                                                getMDOrNull(Record[2]))),
1539                               NextMDValueNo++);
1540       break;
1541     }
1542     case bitc::METADATA_TEMPLATE_VALUE: {
1543       if (Record.size() != 5)
1544         return Error("Invalid record");
1545
1546       MDValueList.AssignValue(
1547           GET_OR_DISTINCT(MDTemplateValueParameter, Record[0],
1548                           (Context, Record[1], getMDString(Record[2]),
1549                            getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
1550           NextMDValueNo++);
1551       break;
1552     }
1553     case bitc::METADATA_GLOBAL_VAR: {
1554       if (Record.size() != 11)
1555         return Error("Invalid record");
1556
1557       MDValueList.AssignValue(
1558           GET_OR_DISTINCT(MDGlobalVariable, Record[0],
1559                           (Context, getMDOrNull(Record[1]),
1560                            getMDString(Record[2]), getMDString(Record[3]),
1561                            getMDOrNull(Record[4]), Record[5],
1562                            getMDOrNull(Record[6]), Record[7], Record[8],
1563                            getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
1564           NextMDValueNo++);
1565       break;
1566     }
1567     case bitc::METADATA_LOCAL_VAR: {
1568       if (Record.size() != 10)
1569         return Error("Invalid record");
1570
1571       MDValueList.AssignValue(
1572           GET_OR_DISTINCT(MDLocalVariable, Record[0],
1573                           (Context, Record[1], getMDOrNull(Record[2]),
1574                            getMDString(Record[3]), getMDOrNull(Record[4]),
1575                            Record[5], getMDOrNull(Record[6]), Record[7],
1576                            Record[8], getMDOrNull(Record[9]))),
1577           NextMDValueNo++);
1578       break;
1579     }
1580     case bitc::METADATA_EXPRESSION: {
1581       if (Record.size() < 1)
1582         return Error("Invalid record");
1583
1584       MDValueList.AssignValue(
1585           GET_OR_DISTINCT(MDExpression, Record[0],
1586                           (Context, makeArrayRef(Record).slice(1))),
1587           NextMDValueNo++);
1588       break;
1589     }
1590     case bitc::METADATA_OBJC_PROPERTY: {
1591       if (Record.size() != 8)
1592         return Error("Invalid record");
1593
1594       MDValueList.AssignValue(
1595           GET_OR_DISTINCT(MDObjCProperty, Record[0],
1596                           (Context, getMDString(Record[1]),
1597                            getMDOrNull(Record[2]), Record[3],
1598                            getMDString(Record[4]), getMDString(Record[5]),
1599                            Record[6], getMDOrNull(Record[7]))),
1600           NextMDValueNo++);
1601       break;
1602     }
1603     case bitc::METADATA_IMPORTED_ENTITY: {
1604       if (Record.size() != 6)
1605         return Error("Invalid record");
1606
1607       MDValueList.AssignValue(
1608           GET_OR_DISTINCT(MDImportedEntity, Record[0],
1609                           (Context, Record[1], getMDOrNull(Record[2]),
1610                            getMDOrNull(Record[3]), Record[4],
1611                            getMDString(Record[5]))),
1612           NextMDValueNo++);
1613       break;
1614     }
1615     case bitc::METADATA_STRING: {
1616       std::string String(Record.begin(), Record.end());
1617       llvm::UpgradeMDStringConstant(String);
1618       Metadata *MD = MDString::get(Context, String);
1619       MDValueList.AssignValue(MD, NextMDValueNo++);
1620       break;
1621     }
1622     case bitc::METADATA_KIND: {
1623       if (Record.size() < 2)
1624         return Error("Invalid record");
1625
1626       unsigned Kind = Record[0];
1627       SmallString<8> Name(Record.begin()+1, Record.end());
1628
1629       unsigned NewKind = TheModule->getMDKindID(Name.str());
1630       if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1631         return Error("Conflicting METADATA_KIND records");
1632       break;
1633     }
1634     }
1635   }
1636 #undef GET_OR_DISTINCT
1637 }
1638
1639 /// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
1640 /// the LSB for dense VBR encoding.
1641 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
1642   if ((V & 1) == 0)
1643     return V >> 1;
1644   if (V != 1)
1645     return -(V >> 1);
1646   // There is no such thing as -0 with integers.  "-0" really means MININT.
1647   return 1ULL << 63;
1648 }
1649
1650 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1651 /// values and aliases that we can.
1652 std::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
1653   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1654   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
1655   std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
1656   std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
1657
1658   GlobalInitWorklist.swap(GlobalInits);
1659   AliasInitWorklist.swap(AliasInits);
1660   FunctionPrefixWorklist.swap(FunctionPrefixes);
1661   FunctionPrologueWorklist.swap(FunctionPrologues);
1662
1663   while (!GlobalInitWorklist.empty()) {
1664     unsigned ValID = GlobalInitWorklist.back().second;
1665     if (ValID >= ValueList.size()) {
1666       // Not ready to resolve this yet, it requires something later in the file.
1667       GlobalInits.push_back(GlobalInitWorklist.back());
1668     } else {
1669       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1670         GlobalInitWorklist.back().first->setInitializer(C);
1671       else
1672         return Error("Expected a constant");
1673     }
1674     GlobalInitWorklist.pop_back();
1675   }
1676
1677   while (!AliasInitWorklist.empty()) {
1678     unsigned ValID = AliasInitWorklist.back().second;
1679     if (ValID >= ValueList.size()) {
1680       AliasInits.push_back(AliasInitWorklist.back());
1681     } else {
1682       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1683         AliasInitWorklist.back().first->setAliasee(C);
1684       else
1685         return Error("Expected a constant");
1686     }
1687     AliasInitWorklist.pop_back();
1688   }
1689
1690   while (!FunctionPrefixWorklist.empty()) {
1691     unsigned ValID = FunctionPrefixWorklist.back().second;
1692     if (ValID >= ValueList.size()) {
1693       FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
1694     } else {
1695       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1696         FunctionPrefixWorklist.back().first->setPrefixData(C);
1697       else
1698         return Error("Expected a constant");
1699     }
1700     FunctionPrefixWorklist.pop_back();
1701   }
1702
1703   while (!FunctionPrologueWorklist.empty()) {
1704     unsigned ValID = FunctionPrologueWorklist.back().second;
1705     if (ValID >= ValueList.size()) {
1706       FunctionPrologues.push_back(FunctionPrologueWorklist.back());
1707     } else {
1708       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1709         FunctionPrologueWorklist.back().first->setPrologueData(C);
1710       else
1711         return Error("Expected a constant");
1712     }
1713     FunctionPrologueWorklist.pop_back();
1714   }
1715
1716   return std::error_code();
1717 }
1718
1719 static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1720   SmallVector<uint64_t, 8> Words(Vals.size());
1721   std::transform(Vals.begin(), Vals.end(), Words.begin(),
1722                  BitcodeReader::decodeSignRotatedValue);
1723
1724   return APInt(TypeBits, Words);
1725 }
1726
1727 std::error_code BitcodeReader::ParseConstants() {
1728   if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
1729     return Error("Invalid record");
1730
1731   SmallVector<uint64_t, 64> Record;
1732
1733   // Read all the records for this value table.
1734   Type *CurTy = Type::getInt32Ty(Context);
1735   unsigned NextCstNo = ValueList.size();
1736   while (1) {
1737     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1738
1739     switch (Entry.Kind) {
1740     case BitstreamEntry::SubBlock: // Handled for us already.
1741     case BitstreamEntry::Error:
1742       return Error("Malformed block");
1743     case BitstreamEntry::EndBlock:
1744       if (NextCstNo != ValueList.size())
1745         return Error("Invalid ronstant reference");
1746
1747       // Once all the constants have been read, go through and resolve forward
1748       // references.
1749       ValueList.ResolveConstantForwardRefs();
1750       return std::error_code();
1751     case BitstreamEntry::Record:
1752       // The interesting case.
1753       break;
1754     }
1755
1756     // Read a record.
1757     Record.clear();
1758     Value *V = nullptr;
1759     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
1760     switch (BitCode) {
1761     default:  // Default behavior: unknown constant
1762     case bitc::CST_CODE_UNDEF:     // UNDEF
1763       V = UndefValue::get(CurTy);
1764       break;
1765     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
1766       if (Record.empty())
1767         return Error("Invalid record");
1768       if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
1769         return Error("Invalid record");
1770       CurTy = TypeList[Record[0]];
1771       continue;  // Skip the ValueList manipulation.
1772     case bitc::CST_CODE_NULL:      // NULL
1773       V = Constant::getNullValue(CurTy);
1774       break;
1775     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
1776       if (!CurTy->isIntegerTy() || Record.empty())
1777         return Error("Invalid record");
1778       V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
1779       break;
1780     case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
1781       if (!CurTy->isIntegerTy() || Record.empty())
1782         return Error("Invalid record");
1783
1784       APInt VInt = ReadWideAPInt(Record,
1785                                  cast<IntegerType>(CurTy)->getBitWidth());
1786       V = ConstantInt::get(Context, VInt);
1787
1788       break;
1789     }
1790     case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
1791       if (Record.empty())
1792         return Error("Invalid record");
1793       if (CurTy->isHalfTy())
1794         V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1795                                              APInt(16, (uint16_t)Record[0])));
1796       else if (CurTy->isFloatTy())
1797         V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1798                                              APInt(32, (uint32_t)Record[0])));
1799       else if (CurTy->isDoubleTy())
1800         V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1801                                              APInt(64, Record[0])));
1802       else if (CurTy->isX86_FP80Ty()) {
1803         // Bits are not stored the same way as a normal i80 APInt, compensate.
1804         uint64_t Rearrange[2];
1805         Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1806         Rearrange[1] = Record[0] >> 48;
1807         V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1808                                              APInt(80, Rearrange)));
1809       } else if (CurTy->isFP128Ty())
1810         V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1811                                              APInt(128, Record)));
1812       else if (CurTy->isPPC_FP128Ty())
1813         V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1814                                              APInt(128, Record)));
1815       else
1816         V = UndefValue::get(CurTy);
1817       break;
1818     }
1819
1820     case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1821       if (Record.empty())
1822         return Error("Invalid record");
1823
1824       unsigned Size = Record.size();
1825       SmallVector<Constant*, 16> Elts;
1826
1827       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
1828         for (unsigned i = 0; i != Size; ++i)
1829           Elts.push_back(ValueList.getConstantFwdRef(Record[i],
1830                                                      STy->getElementType(i)));
1831         V = ConstantStruct::get(STy, Elts);
1832       } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1833         Type *EltTy = ATy->getElementType();
1834         for (unsigned i = 0; i != Size; ++i)
1835           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1836         V = ConstantArray::get(ATy, Elts);
1837       } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1838         Type *EltTy = VTy->getElementType();
1839         for (unsigned i = 0; i != Size; ++i)
1840           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1841         V = ConstantVector::get(Elts);
1842       } else {
1843         V = UndefValue::get(CurTy);
1844       }
1845       break;
1846     }
1847     case bitc::CST_CODE_STRING:    // STRING: [values]
1848     case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1849       if (Record.empty())
1850         return Error("Invalid record");
1851
1852       SmallString<16> Elts(Record.begin(), Record.end());
1853       V = ConstantDataArray::getString(Context, Elts,
1854                                        BitCode == bitc::CST_CODE_CSTRING);
1855       break;
1856     }
1857     case bitc::CST_CODE_DATA: {// DATA: [n x value]
1858       if (Record.empty())
1859         return Error("Invalid record");
1860
1861       Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1862       unsigned Size = Record.size();
1863
1864       if (EltTy->isIntegerTy(8)) {
1865         SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1866         if (isa<VectorType>(CurTy))
1867           V = ConstantDataVector::get(Context, Elts);
1868         else
1869           V = ConstantDataArray::get(Context, Elts);
1870       } else if (EltTy->isIntegerTy(16)) {
1871         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1872         if (isa<VectorType>(CurTy))
1873           V = ConstantDataVector::get(Context, Elts);
1874         else
1875           V = ConstantDataArray::get(Context, Elts);
1876       } else if (EltTy->isIntegerTy(32)) {
1877         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1878         if (isa<VectorType>(CurTy))
1879           V = ConstantDataVector::get(Context, Elts);
1880         else
1881           V = ConstantDataArray::get(Context, Elts);
1882       } else if (EltTy->isIntegerTy(64)) {
1883         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1884         if (isa<VectorType>(CurTy))
1885           V = ConstantDataVector::get(Context, Elts);
1886         else
1887           V = ConstantDataArray::get(Context, Elts);
1888       } else if (EltTy->isFloatTy()) {
1889         SmallVector<float, 16> Elts(Size);
1890         std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
1891         if (isa<VectorType>(CurTy))
1892           V = ConstantDataVector::get(Context, Elts);
1893         else
1894           V = ConstantDataArray::get(Context, Elts);
1895       } else if (EltTy->isDoubleTy()) {
1896         SmallVector<double, 16> Elts(Size);
1897         std::transform(Record.begin(), Record.end(), Elts.begin(),
1898                        BitsToDouble);
1899         if (isa<VectorType>(CurTy))
1900           V = ConstantDataVector::get(Context, Elts);
1901         else
1902           V = ConstantDataArray::get(Context, Elts);
1903       } else {
1904         return Error("Invalid type for value");
1905       }
1906       break;
1907     }
1908
1909     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
1910       if (Record.size() < 3)
1911         return Error("Invalid record");
1912       int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
1913       if (Opc < 0) {
1914         V = UndefValue::get(CurTy);  // Unknown binop.
1915       } else {
1916         Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1917         Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
1918         unsigned Flags = 0;
1919         if (Record.size() >= 4) {
1920           if (Opc == Instruction::Add ||
1921               Opc == Instruction::Sub ||
1922               Opc == Instruction::Mul ||
1923               Opc == Instruction::Shl) {
1924             if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1925               Flags |= OverflowingBinaryOperator::NoSignedWrap;
1926             if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1927               Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
1928           } else if (Opc == Instruction::SDiv ||
1929                      Opc == Instruction::UDiv ||
1930                      Opc == Instruction::LShr ||
1931                      Opc == Instruction::AShr) {
1932             if (Record[3] & (1 << bitc::PEO_EXACT))
1933               Flags |= SDivOperator::IsExact;
1934           }
1935         }
1936         V = ConstantExpr::get(Opc, LHS, RHS, Flags);
1937       }
1938       break;
1939     }
1940     case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
1941       if (Record.size() < 3)
1942         return Error("Invalid record");
1943       int Opc = GetDecodedCastOpcode(Record[0]);
1944       if (Opc < 0) {
1945         V = UndefValue::get(CurTy);  // Unknown cast.
1946       } else {
1947         Type *OpTy = getTypeByID(Record[1]);
1948         if (!OpTy)
1949           return Error("Invalid record");
1950         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
1951         V = UpgradeBitCastExpr(Opc, Op, CurTy);
1952         if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
1953       }
1954       break;
1955     }
1956     case bitc::CST_CODE_CE_INBOUNDS_GEP:
1957     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
1958       unsigned OpNum = 0;
1959       Type *PointeeType = nullptr;
1960       if (Record.size() % 2)
1961         PointeeType = getTypeByID(Record[OpNum++]);
1962       SmallVector<Constant*, 16> Elts;
1963       while (OpNum != Record.size()) {
1964         Type *ElTy = getTypeByID(Record[OpNum++]);
1965         if (!ElTy)
1966           return Error("Invalid record");
1967         Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
1968       }
1969
1970       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
1971       V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1972                                          BitCode ==
1973                                            bitc::CST_CODE_CE_INBOUNDS_GEP);
1974       if (PointeeType &&
1975           PointeeType != cast<GEPOperator>(V)->getSourceElementType())
1976         return Error("Invalid record");
1977       break;
1978     }
1979     case bitc::CST_CODE_CE_SELECT: {  // CE_SELECT: [opval#, opval#, opval#]
1980       if (Record.size() < 3)
1981         return Error("Invalid record");
1982
1983       Type *SelectorTy = Type::getInt1Ty(Context);
1984
1985       // If CurTy is a vector of length n, then Record[0] must be a <n x i1>
1986       // vector. Otherwise, it must be a single bit.
1987       if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
1988         SelectorTy = VectorType::get(Type::getInt1Ty(Context),
1989                                      VTy->getNumElements());
1990
1991       V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1992                                                               SelectorTy),
1993                                   ValueList.getConstantFwdRef(Record[1],CurTy),
1994                                   ValueList.getConstantFwdRef(Record[2],CurTy));
1995       break;
1996     }
1997     case bitc::CST_CODE_CE_EXTRACTELT
1998         : { // CE_EXTRACTELT: [opty, opval, opty, opval]
1999       if (Record.size() < 3)
2000         return Error("Invalid record");
2001       VectorType *OpTy =
2002         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2003       if (!OpTy)
2004         return Error("Invalid record");
2005       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2006       Constant *Op1 = nullptr;
2007       if (Record.size() == 4) {
2008         Type *IdxTy = getTypeByID(Record[2]);
2009         if (!IdxTy)
2010           return Error("Invalid record");
2011         Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2012       } else // TODO: Remove with llvm 4.0
2013         Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2014       if (!Op1)
2015         return Error("Invalid record");
2016       V = ConstantExpr::getExtractElement(Op0, Op1);
2017       break;
2018     }
2019     case bitc::CST_CODE_CE_INSERTELT
2020         : { // CE_INSERTELT: [opval, opval, opty, opval]
2021       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2022       if (Record.size() < 3 || !OpTy)
2023         return Error("Invalid record");
2024       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2025       Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2026                                                   OpTy->getElementType());
2027       Constant *Op2 = nullptr;
2028       if (Record.size() == 4) {
2029         Type *IdxTy = getTypeByID(Record[2]);
2030         if (!IdxTy)
2031           return Error("Invalid record");
2032         Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2033       } else // TODO: Remove with llvm 4.0
2034         Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2035       if (!Op2)
2036         return Error("Invalid record");
2037       V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2038       break;
2039     }
2040     case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2041       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2042       if (Record.size() < 3 || !OpTy)
2043         return Error("Invalid record");
2044       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2045       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
2046       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2047                                                  OpTy->getNumElements());
2048       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
2049       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2050       break;
2051     }
2052     case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2053       VectorType *RTy = dyn_cast<VectorType>(CurTy);
2054       VectorType *OpTy =
2055         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2056       if (Record.size() < 4 || !RTy || !OpTy)
2057         return Error("Invalid record");
2058       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2059       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2060       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2061                                                  RTy->getNumElements());
2062       Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
2063       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2064       break;
2065     }
2066     case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
2067       if (Record.size() < 4)
2068         return Error("Invalid record");
2069       Type *OpTy = getTypeByID(Record[0]);
2070       if (!OpTy)
2071         return Error("Invalid record");
2072       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2073       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2074
2075       if (OpTy->isFPOrFPVectorTy())
2076         V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2077       else
2078         V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2079       break;
2080     }
2081     // This maintains backward compatibility, pre-asm dialect keywords.
2082     // FIXME: Remove with the 4.0 release.
2083     case bitc::CST_CODE_INLINEASM_OLD: {
2084       if (Record.size() < 2)
2085         return Error("Invalid record");
2086       std::string AsmStr, ConstrStr;
2087       bool HasSideEffects = Record[0] & 1;
2088       bool IsAlignStack = Record[0] >> 1;
2089       unsigned AsmStrSize = Record[1];
2090       if (2+AsmStrSize >= Record.size())
2091         return Error("Invalid record");
2092       unsigned ConstStrSize = Record[2+AsmStrSize];
2093       if (3+AsmStrSize+ConstStrSize > Record.size())
2094         return Error("Invalid record");
2095
2096       for (unsigned i = 0; i != AsmStrSize; ++i)
2097         AsmStr += (char)Record[2+i];
2098       for (unsigned i = 0; i != ConstStrSize; ++i)
2099         ConstrStr += (char)Record[3+AsmStrSize+i];
2100       PointerType *PTy = cast<PointerType>(CurTy);
2101       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2102                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
2103       break;
2104     }
2105     // This version adds support for the asm dialect keywords (e.g.,
2106     // inteldialect).
2107     case bitc::CST_CODE_INLINEASM: {
2108       if (Record.size() < 2)
2109         return Error("Invalid record");
2110       std::string AsmStr, ConstrStr;
2111       bool HasSideEffects = Record[0] & 1;
2112       bool IsAlignStack = (Record[0] >> 1) & 1;
2113       unsigned AsmDialect = Record[0] >> 2;
2114       unsigned AsmStrSize = Record[1];
2115       if (2+AsmStrSize >= Record.size())
2116         return Error("Invalid record");
2117       unsigned ConstStrSize = Record[2+AsmStrSize];
2118       if (3+AsmStrSize+ConstStrSize > Record.size())
2119         return Error("Invalid record");
2120
2121       for (unsigned i = 0; i != AsmStrSize; ++i)
2122         AsmStr += (char)Record[2+i];
2123       for (unsigned i = 0; i != ConstStrSize; ++i)
2124         ConstrStr += (char)Record[3+AsmStrSize+i];
2125       PointerType *PTy = cast<PointerType>(CurTy);
2126       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2127                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
2128                          InlineAsm::AsmDialect(AsmDialect));
2129       break;
2130     }
2131     case bitc::CST_CODE_BLOCKADDRESS:{
2132       if (Record.size() < 3)
2133         return Error("Invalid record");
2134       Type *FnTy = getTypeByID(Record[0]);
2135       if (!FnTy)
2136         return Error("Invalid record");
2137       Function *Fn =
2138         dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
2139       if (!Fn)
2140         return Error("Invalid record");
2141
2142       // Don't let Fn get dematerialized.
2143       BlockAddressesTaken.insert(Fn);
2144
2145       // If the function is already parsed we can insert the block address right
2146       // away.
2147       BasicBlock *BB;
2148       unsigned BBID = Record[2];
2149       if (!BBID)
2150         // Invalid reference to entry block.
2151         return Error("Invalid ID");
2152       if (!Fn->empty()) {
2153         Function::iterator BBI = Fn->begin(), BBE = Fn->end();
2154         for (size_t I = 0, E = BBID; I != E; ++I) {
2155           if (BBI == BBE)
2156             return Error("Invalid ID");
2157           ++BBI;
2158         }
2159         BB = BBI;
2160       } else {
2161         // Otherwise insert a placeholder and remember it so it can be inserted
2162         // when the function is parsed.
2163         auto &FwdBBs = BasicBlockFwdRefs[Fn];
2164         if (FwdBBs.empty())
2165           BasicBlockFwdRefQueue.push_back(Fn);
2166         if (FwdBBs.size() < BBID + 1)
2167           FwdBBs.resize(BBID + 1);
2168         if (!FwdBBs[BBID])
2169           FwdBBs[BBID] = BasicBlock::Create(Context);
2170         BB = FwdBBs[BBID];
2171       }
2172       V = BlockAddress::get(Fn, BB);
2173       break;
2174     }
2175     }
2176
2177     ValueList.AssignValue(V, NextCstNo);
2178     ++NextCstNo;
2179   }
2180 }
2181
2182 std::error_code BitcodeReader::ParseUseLists() {
2183   if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
2184     return Error("Invalid record");
2185
2186   // Read all the records.
2187   SmallVector<uint64_t, 64> Record;
2188   while (1) {
2189     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2190
2191     switch (Entry.Kind) {
2192     case BitstreamEntry::SubBlock: // Handled for us already.
2193     case BitstreamEntry::Error:
2194       return Error("Malformed block");
2195     case BitstreamEntry::EndBlock:
2196       return std::error_code();
2197     case BitstreamEntry::Record:
2198       // The interesting case.
2199       break;
2200     }
2201
2202     // Read a use list record.
2203     Record.clear();
2204     bool IsBB = false;
2205     switch (Stream.readRecord(Entry.ID, Record)) {
2206     default:  // Default behavior: unknown type.
2207       break;
2208     case bitc::USELIST_CODE_BB:
2209       IsBB = true;
2210       // fallthrough
2211     case bitc::USELIST_CODE_DEFAULT: {
2212       unsigned RecordLength = Record.size();
2213       if (RecordLength < 3)
2214         // Records should have at least an ID and two indexes.
2215         return Error("Invalid record");
2216       unsigned ID = Record.back();
2217       Record.pop_back();
2218
2219       Value *V;
2220       if (IsBB) {
2221         assert(ID < FunctionBBs.size() && "Basic block not found");
2222         V = FunctionBBs[ID];
2223       } else
2224         V = ValueList[ID];
2225       unsigned NumUses = 0;
2226       SmallDenseMap<const Use *, unsigned, 16> Order;
2227       for (const Use &U : V->uses()) {
2228         if (++NumUses > Record.size())
2229           break;
2230         Order[&U] = Record[NumUses - 1];
2231       }
2232       if (Order.size() != Record.size() || NumUses > Record.size())
2233         // Mismatches can happen if the functions are being materialized lazily
2234         // (out-of-order), or a value has been upgraded.
2235         break;
2236
2237       V->sortUseList([&](const Use &L, const Use &R) {
2238         return Order.lookup(&L) < Order.lookup(&R);
2239       });
2240       break;
2241     }
2242     }
2243   }
2244 }
2245
2246 /// When we see the block for metadata, remember where it is and then skip it.
2247 /// This lets us lazily deserialize the metadata.
2248 std::error_code BitcodeReader::rememberAndSkipMetadata() {
2249   // Save the current stream state.
2250   uint64_t CurBit = Stream.GetCurrentBitNo();
2251   DeferredMetadataInfo.push_back(CurBit);
2252
2253   // Skip over the block for now.
2254   if (Stream.SkipBlock())
2255     return Error("Invalid record");
2256   return std::error_code();
2257 }
2258
2259 std::error_code BitcodeReader::materializeMetadata() {
2260   for (uint64_t BitPos : DeferredMetadataInfo) {
2261     // Move the bit stream to the saved position.
2262     Stream.JumpToBit(BitPos);
2263     if (std::error_code EC = ParseMetadata())
2264       return EC;
2265   }
2266   DeferredMetadataInfo.clear();
2267   return std::error_code();
2268 }
2269
2270 /// RememberAndSkipFunctionBody - When we see the block for a function body,
2271 /// remember where it is and then skip it.  This lets us lazily deserialize the
2272 /// functions.
2273 std::error_code BitcodeReader::RememberAndSkipFunctionBody() {
2274   // Get the function we are talking about.
2275   if (FunctionsWithBodies.empty())
2276     return Error("Insufficient function protos");
2277
2278   Function *Fn = FunctionsWithBodies.back();
2279   FunctionsWithBodies.pop_back();
2280
2281   // Save the current stream state.
2282   uint64_t CurBit = Stream.GetCurrentBitNo();
2283   DeferredFunctionInfo[Fn] = CurBit;
2284
2285   // Skip over the function block for now.
2286   if (Stream.SkipBlock())
2287     return Error("Invalid record");
2288   return std::error_code();
2289 }
2290
2291 std::error_code BitcodeReader::GlobalCleanup() {
2292   // Patch the initializers for globals and aliases up.
2293   ResolveGlobalAndAliasInits();
2294   if (!GlobalInits.empty() || !AliasInits.empty())
2295     return Error("Malformed global initializer set");
2296
2297   // Look for intrinsic functions which need to be upgraded at some point
2298   for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
2299        FI != FE; ++FI) {
2300     Function *NewFn;
2301     if (UpgradeIntrinsicFunction(FI, NewFn))
2302       UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
2303   }
2304
2305   // Look for global variables which need to be renamed.
2306   for (Module::global_iterator
2307          GI = TheModule->global_begin(), GE = TheModule->global_end();
2308        GI != GE;) {
2309     GlobalVariable *GV = GI++;
2310     UpgradeGlobalVariable(GV);
2311   }
2312
2313   // Force deallocation of memory for these vectors to favor the client that
2314   // want lazy deserialization.
2315   std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
2316   std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
2317   return std::error_code();
2318 }
2319
2320 std::error_code BitcodeReader::ParseModule(bool Resume,
2321                                            bool ShouldLazyLoadMetadata) {
2322   if (Resume)
2323     Stream.JumpToBit(NextUnreadBit);
2324   else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
2325     return Error("Invalid record");
2326
2327   SmallVector<uint64_t, 64> Record;
2328   std::vector<std::string> SectionTable;
2329   std::vector<std::string> GCTable;
2330
2331   // Read all the records for this module.
2332   while (1) {
2333     BitstreamEntry Entry = Stream.advance();
2334
2335     switch (Entry.Kind) {
2336     case BitstreamEntry::Error:
2337       return Error("Malformed block");
2338     case BitstreamEntry::EndBlock:
2339       return GlobalCleanup();
2340
2341     case BitstreamEntry::SubBlock:
2342       switch (Entry.ID) {
2343       default:  // Skip unknown content.
2344         if (Stream.SkipBlock())
2345           return Error("Invalid record");
2346         break;
2347       case bitc::BLOCKINFO_BLOCK_ID:
2348         if (Stream.ReadBlockInfoBlock())
2349           return Error("Malformed block");
2350         break;
2351       case bitc::PARAMATTR_BLOCK_ID:
2352         if (std::error_code EC = ParseAttributeBlock())
2353           return EC;
2354         break;
2355       case bitc::PARAMATTR_GROUP_BLOCK_ID:
2356         if (std::error_code EC = ParseAttributeGroupBlock())
2357           return EC;
2358         break;
2359       case bitc::TYPE_BLOCK_ID_NEW:
2360         if (std::error_code EC = ParseTypeTable())
2361           return EC;
2362         break;
2363       case bitc::VALUE_SYMTAB_BLOCK_ID:
2364         if (std::error_code EC = ParseValueSymbolTable())
2365           return EC;
2366         SeenValueSymbolTable = true;
2367         break;
2368       case bitc::CONSTANTS_BLOCK_ID:
2369         if (std::error_code EC = ParseConstants())
2370           return EC;
2371         if (std::error_code EC = ResolveGlobalAndAliasInits())
2372           return EC;
2373         break;
2374       case bitc::METADATA_BLOCK_ID:
2375         if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
2376           if (std::error_code EC = rememberAndSkipMetadata())
2377             return EC;
2378           break;
2379         }
2380         assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
2381         if (std::error_code EC = ParseMetadata())
2382           return EC;
2383         break;
2384       case bitc::FUNCTION_BLOCK_ID:
2385         // If this is the first function body we've seen, reverse the
2386         // FunctionsWithBodies list.
2387         if (!SeenFirstFunctionBody) {
2388           std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
2389           if (std::error_code EC = GlobalCleanup())
2390             return EC;
2391           SeenFirstFunctionBody = true;
2392         }
2393
2394         if (std::error_code EC = RememberAndSkipFunctionBody())
2395           return EC;
2396         // For streaming bitcode, suspend parsing when we reach the function
2397         // bodies. Subsequent materialization calls will resume it when
2398         // necessary. For streaming, the function bodies must be at the end of
2399         // the bitcode. If the bitcode file is old, the symbol table will be
2400         // at the end instead and will not have been seen yet. In this case,
2401         // just finish the parse now.
2402         if (LazyStreamer && SeenValueSymbolTable) {
2403           NextUnreadBit = Stream.GetCurrentBitNo();
2404           return std::error_code();
2405         }
2406         break;
2407       case bitc::USELIST_BLOCK_ID:
2408         if (std::error_code EC = ParseUseLists())
2409           return EC;
2410         break;
2411       }
2412       continue;
2413
2414     case BitstreamEntry::Record:
2415       // The interesting case.
2416       break;
2417     }
2418
2419
2420     // Read a record.
2421     switch (Stream.readRecord(Entry.ID, Record)) {
2422     default: break;  // Default behavior, ignore unknown content.
2423     case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
2424       if (Record.size() < 1)
2425         return Error("Invalid record");
2426       // Only version #0 and #1 are supported so far.
2427       unsigned module_version = Record[0];
2428       switch (module_version) {
2429         default:
2430           return Error("Invalid value");
2431         case 0:
2432           UseRelativeIDs = false;
2433           break;
2434         case 1:
2435           UseRelativeIDs = true;
2436           break;
2437       }
2438       break;
2439     }
2440     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
2441       std::string S;
2442       if (ConvertToString(Record, 0, S))
2443         return Error("Invalid record");
2444       TheModule->setTargetTriple(S);
2445       break;
2446     }
2447     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
2448       std::string S;
2449       if (ConvertToString(Record, 0, S))
2450         return Error("Invalid record");
2451       TheModule->setDataLayout(S);
2452       break;
2453     }
2454     case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
2455       std::string S;
2456       if (ConvertToString(Record, 0, S))
2457         return Error("Invalid record");
2458       TheModule->setModuleInlineAsm(S);
2459       break;
2460     }
2461     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
2462       // FIXME: Remove in 4.0.
2463       std::string S;
2464       if (ConvertToString(Record, 0, S))
2465         return Error("Invalid record");
2466       // Ignore value.
2467       break;
2468     }
2469     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
2470       std::string S;
2471       if (ConvertToString(Record, 0, S))
2472         return Error("Invalid record");
2473       SectionTable.push_back(S);
2474       break;
2475     }
2476     case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
2477       std::string S;
2478       if (ConvertToString(Record, 0, S))
2479         return Error("Invalid record");
2480       GCTable.push_back(S);
2481       break;
2482     }
2483     case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
2484       if (Record.size() < 2)
2485         return Error("Invalid record");
2486       Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
2487       unsigned ComdatNameSize = Record[1];
2488       std::string ComdatName;
2489       ComdatName.reserve(ComdatNameSize);
2490       for (unsigned i = 0; i != ComdatNameSize; ++i)
2491         ComdatName += (char)Record[2 + i];
2492       Comdat *C = TheModule->getOrInsertComdat(ComdatName);
2493       C->setSelectionKind(SK);
2494       ComdatList.push_back(C);
2495       break;
2496     }
2497     // GLOBALVAR: [pointer type, isconst, initid,
2498     //             linkage, alignment, section, visibility, threadlocal,
2499     //             unnamed_addr, externally_initialized, dllstorageclass,
2500     //             comdat]
2501     case bitc::MODULE_CODE_GLOBALVAR: {
2502       if (Record.size() < 6)
2503         return Error("Invalid record");
2504       Type *Ty = getTypeByID(Record[0]);
2505       if (!Ty)
2506         return Error("Invalid record");
2507       if (!Ty->isPointerTy())
2508         return Error("Invalid type for value");
2509       unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
2510       Ty = cast<PointerType>(Ty)->getElementType();
2511
2512       bool isConstant = Record[1];
2513       uint64_t RawLinkage = Record[3];
2514       GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
2515       unsigned Alignment;
2516       if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
2517         return EC;
2518       std::string Section;
2519       if (Record[5]) {
2520         if (Record[5]-1 >= SectionTable.size())
2521           return Error("Invalid ID");
2522         Section = SectionTable[Record[5]-1];
2523       }
2524       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
2525       // Local linkage must have default visibility.
2526       if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
2527         // FIXME: Change to an error if non-default in 4.0.
2528         Visibility = GetDecodedVisibility(Record[6]);
2529
2530       GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
2531       if (Record.size() > 7)
2532         TLM = GetDecodedThreadLocalMode(Record[7]);
2533
2534       bool UnnamedAddr = false;
2535       if (Record.size() > 8)
2536         UnnamedAddr = Record[8];
2537
2538       bool ExternallyInitialized = false;
2539       if (Record.size() > 9)
2540         ExternallyInitialized = Record[9];
2541
2542       GlobalVariable *NewGV =
2543         new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
2544                            TLM, AddressSpace, ExternallyInitialized);
2545       NewGV->setAlignment(Alignment);
2546       if (!Section.empty())
2547         NewGV->setSection(Section);
2548       NewGV->setVisibility(Visibility);
2549       NewGV->setUnnamedAddr(UnnamedAddr);
2550
2551       if (Record.size() > 10)
2552         NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10]));
2553       else
2554         UpgradeDLLImportExportLinkage(NewGV, RawLinkage);
2555
2556       ValueList.push_back(NewGV);
2557
2558       // Remember which value to use for the global initializer.
2559       if (unsigned InitID = Record[2])
2560         GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
2561
2562       if (Record.size() > 11) {
2563         if (unsigned ComdatID = Record[11]) {
2564           assert(ComdatID <= ComdatList.size());
2565           NewGV->setComdat(ComdatList[ComdatID - 1]);
2566         }
2567       } else if (hasImplicitComdat(RawLinkage)) {
2568         NewGV->setComdat(reinterpret_cast<Comdat *>(1));
2569       }
2570       break;
2571     }
2572     // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
2573     //             alignment, section, visibility, gc, unnamed_addr,
2574     //             prologuedata, dllstorageclass, comdat, prefixdata]
2575     case bitc::MODULE_CODE_FUNCTION: {
2576       if (Record.size() < 8)
2577         return Error("Invalid record");
2578       Type *Ty = getTypeByID(Record[0]);
2579       if (!Ty)
2580         return Error("Invalid record");
2581       if (!Ty->isPointerTy())
2582         return Error("Invalid type for value");
2583       FunctionType *FTy =
2584         dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
2585       if (!FTy)
2586         return Error("Invalid type for value");
2587
2588       Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
2589                                         "", TheModule);
2590
2591       Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
2592       bool isProto = Record[2];
2593       uint64_t RawLinkage = Record[3];
2594       Func->setLinkage(getDecodedLinkage(RawLinkage));
2595       Func->setAttributes(getAttributes(Record[4]));
2596
2597       unsigned Alignment;
2598       if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
2599         return EC;
2600       Func->setAlignment(Alignment);
2601       if (Record[6]) {
2602         if (Record[6]-1 >= SectionTable.size())
2603           return Error("Invalid ID");
2604         Func->setSection(SectionTable[Record[6]-1]);
2605       }
2606       // Local linkage must have default visibility.
2607       if (!Func->hasLocalLinkage())
2608         // FIXME: Change to an error if non-default in 4.0.
2609         Func->setVisibility(GetDecodedVisibility(Record[7]));
2610       if (Record.size() > 8 && Record[8]) {
2611         if (Record[8]-1 > GCTable.size())
2612           return Error("Invalid ID");
2613         Func->setGC(GCTable[Record[8]-1].c_str());
2614       }
2615       bool UnnamedAddr = false;
2616       if (Record.size() > 9)
2617         UnnamedAddr = Record[9];
2618       Func->setUnnamedAddr(UnnamedAddr);
2619       if (Record.size() > 10 && Record[10] != 0)
2620         FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
2621
2622       if (Record.size() > 11)
2623         Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11]));
2624       else
2625         UpgradeDLLImportExportLinkage(Func, RawLinkage);
2626
2627       if (Record.size() > 12) {
2628         if (unsigned ComdatID = Record[12]) {
2629           assert(ComdatID <= ComdatList.size());
2630           Func->setComdat(ComdatList[ComdatID - 1]);
2631         }
2632       } else if (hasImplicitComdat(RawLinkage)) {
2633         Func->setComdat(reinterpret_cast<Comdat *>(1));
2634       }
2635
2636       if (Record.size() > 13 && Record[13] != 0)
2637         FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
2638
2639       ValueList.push_back(Func);
2640
2641       // If this is a function with a body, remember the prototype we are
2642       // creating now, so that we can match up the body with them later.
2643       if (!isProto) {
2644         Func->setIsMaterializable(true);
2645         FunctionsWithBodies.push_back(Func);
2646         if (LazyStreamer)
2647           DeferredFunctionInfo[Func] = 0;
2648       }
2649       break;
2650     }
2651     // ALIAS: [alias type, aliasee val#, linkage]
2652     // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
2653     case bitc::MODULE_CODE_ALIAS: {
2654       if (Record.size() < 3)
2655         return Error("Invalid record");
2656       Type *Ty = getTypeByID(Record[0]);
2657       if (!Ty)
2658         return Error("Invalid record");
2659       auto *PTy = dyn_cast<PointerType>(Ty);
2660       if (!PTy)
2661         return Error("Invalid type for value");
2662
2663       auto *NewGA =
2664           GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2665                               getDecodedLinkage(Record[2]), "", TheModule);
2666       // Old bitcode files didn't have visibility field.
2667       // Local linkage must have default visibility.
2668       if (Record.size() > 3 && !NewGA->hasLocalLinkage())
2669         // FIXME: Change to an error if non-default in 4.0.
2670         NewGA->setVisibility(GetDecodedVisibility(Record[3]));
2671       if (Record.size() > 4)
2672         NewGA->setDLLStorageClass(GetDecodedDLLStorageClass(Record[4]));
2673       else
2674         UpgradeDLLImportExportLinkage(NewGA, Record[2]);
2675       if (Record.size() > 5)
2676         NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
2677       if (Record.size() > 6)
2678         NewGA->setUnnamedAddr(Record[6]);
2679       ValueList.push_back(NewGA);
2680       AliasInits.push_back(std::make_pair(NewGA, Record[1]));
2681       break;
2682     }
2683     /// MODULE_CODE_PURGEVALS: [numvals]
2684     case bitc::MODULE_CODE_PURGEVALS:
2685       // Trim down the value list to the specified size.
2686       if (Record.size() < 1 || Record[0] > ValueList.size())
2687         return Error("Invalid record");
2688       ValueList.shrinkTo(Record[0]);
2689       break;
2690     }
2691     Record.clear();
2692   }
2693 }
2694
2695 std::error_code BitcodeReader::ParseBitcodeInto(Module *M,
2696                                                 bool ShouldLazyLoadMetadata) {
2697   TheModule = nullptr;
2698
2699   if (std::error_code EC = InitStream())
2700     return EC;
2701
2702   // Sniff for the signature.
2703   if (Stream.Read(8) != 'B' ||
2704       Stream.Read(8) != 'C' ||
2705       Stream.Read(4) != 0x0 ||
2706       Stream.Read(4) != 0xC ||
2707       Stream.Read(4) != 0xE ||
2708       Stream.Read(4) != 0xD)
2709     return Error("Invalid bitcode signature");
2710
2711   // We expect a number of well-defined blocks, though we don't necessarily
2712   // need to understand them all.
2713   while (1) {
2714     if (Stream.AtEndOfStream())
2715       return std::error_code();
2716
2717     BitstreamEntry Entry =
2718       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
2719
2720     switch (Entry.Kind) {
2721     case BitstreamEntry::Error:
2722       return Error("Malformed block");
2723     case BitstreamEntry::EndBlock:
2724       return std::error_code();
2725
2726     case BitstreamEntry::SubBlock:
2727       switch (Entry.ID) {
2728       case bitc::BLOCKINFO_BLOCK_ID:
2729         if (Stream.ReadBlockInfoBlock())
2730           return Error("Malformed block");
2731         break;
2732       case bitc::MODULE_BLOCK_ID:
2733         // Reject multiple MODULE_BLOCK's in a single bitstream.
2734         if (TheModule)
2735           return Error("Invalid multiple blocks");
2736         TheModule = M;
2737         if (std::error_code EC = ParseModule(false, ShouldLazyLoadMetadata))
2738           return EC;
2739         if (LazyStreamer)
2740           return std::error_code();
2741         break;
2742       default:
2743         if (Stream.SkipBlock())
2744           return Error("Invalid record");
2745         break;
2746       }
2747       continue;
2748     case BitstreamEntry::Record:
2749       // There should be no records in the top-level of blocks.
2750
2751       // The ranlib in Xcode 4 will align archive members by appending newlines
2752       // to the end of them. If this file size is a multiple of 4 but not 8, we
2753       // have to read and ignore these final 4 bytes :-(
2754       if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
2755           Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
2756           Stream.AtEndOfStream())
2757         return std::error_code();
2758
2759       return Error("Invalid record");
2760     }
2761   }
2762 }
2763
2764 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
2765   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
2766     return Error("Invalid record");
2767
2768   SmallVector<uint64_t, 64> Record;
2769
2770   std::string Triple;
2771   // Read all the records for this module.
2772   while (1) {
2773     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2774
2775     switch (Entry.Kind) {
2776     case BitstreamEntry::SubBlock: // Handled for us already.
2777     case BitstreamEntry::Error:
2778       return Error("Malformed block");
2779     case BitstreamEntry::EndBlock:
2780       return Triple;
2781     case BitstreamEntry::Record:
2782       // The interesting case.
2783       break;
2784     }
2785
2786     // Read a record.
2787     switch (Stream.readRecord(Entry.ID, Record)) {
2788     default: break;  // Default behavior, ignore unknown content.
2789     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
2790       std::string S;
2791       if (ConvertToString(Record, 0, S))
2792         return Error("Invalid record");
2793       Triple = S;
2794       break;
2795     }
2796     }
2797     Record.clear();
2798   }
2799   llvm_unreachable("Exit infinite loop");
2800 }
2801
2802 ErrorOr<std::string> BitcodeReader::parseTriple() {
2803   if (std::error_code EC = InitStream())
2804     return EC;
2805
2806   // Sniff for the signature.
2807   if (Stream.Read(8) != 'B' ||
2808       Stream.Read(8) != 'C' ||
2809       Stream.Read(4) != 0x0 ||
2810       Stream.Read(4) != 0xC ||
2811       Stream.Read(4) != 0xE ||
2812       Stream.Read(4) != 0xD)
2813     return Error("Invalid bitcode signature");
2814
2815   // We expect a number of well-defined blocks, though we don't necessarily
2816   // need to understand them all.
2817   while (1) {
2818     BitstreamEntry Entry = Stream.advance();
2819
2820     switch (Entry.Kind) {
2821     case BitstreamEntry::Error:
2822       return Error("Malformed block");
2823     case BitstreamEntry::EndBlock:
2824       return std::error_code();
2825
2826     case BitstreamEntry::SubBlock:
2827       if (Entry.ID == bitc::MODULE_BLOCK_ID)
2828         return parseModuleTriple();
2829
2830       // Ignore other sub-blocks.
2831       if (Stream.SkipBlock())
2832         return Error("Malformed block");
2833       continue;
2834
2835     case BitstreamEntry::Record:
2836       Stream.skipRecord(Entry.ID);
2837       continue;
2838     }
2839   }
2840 }
2841
2842 /// ParseMetadataAttachment - Parse metadata attachments.
2843 std::error_code BitcodeReader::ParseMetadataAttachment() {
2844   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
2845     return Error("Invalid record");
2846
2847   SmallVector<uint64_t, 64> Record;
2848   while (1) {
2849     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2850
2851     switch (Entry.Kind) {
2852     case BitstreamEntry::SubBlock: // Handled for us already.
2853     case BitstreamEntry::Error:
2854       return Error("Malformed block");
2855     case BitstreamEntry::EndBlock:
2856       return std::error_code();
2857     case BitstreamEntry::Record:
2858       // The interesting case.
2859       break;
2860     }
2861
2862     // Read a metadata attachment record.
2863     Record.clear();
2864     switch (Stream.readRecord(Entry.ID, Record)) {
2865     default:  // Default behavior: ignore.
2866       break;
2867     case bitc::METADATA_ATTACHMENT: {
2868       unsigned RecordLength = Record.size();
2869       if (Record.empty() || (RecordLength - 1) % 2 == 1)
2870         return Error("Invalid record");
2871       Instruction *Inst = InstructionList[Record[0]];
2872       for (unsigned i = 1; i != RecordLength; i = i+2) {
2873         unsigned Kind = Record[i];
2874         DenseMap<unsigned, unsigned>::iterator I =
2875           MDKindMap.find(Kind);
2876         if (I == MDKindMap.end())
2877           return Error("Invalid ID");
2878         Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
2879         if (isa<LocalAsMetadata>(Node))
2880           // Drop the attachment.  This used to be legal, but there's no
2881           // upgrade path.
2882           break;
2883         Inst->setMetadata(I->second, cast<MDNode>(Node));
2884         if (I->second == LLVMContext::MD_tbaa)
2885           InstsWithTBAATag.push_back(Inst);
2886       }
2887       break;
2888     }
2889     }
2890   }
2891 }
2892
2893 /// ParseFunctionBody - Lazily parse the specified function body block.
2894 std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
2895   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
2896     return Error("Invalid record");
2897
2898   InstructionList.clear();
2899   unsigned ModuleValueListSize = ValueList.size();
2900   unsigned ModuleMDValueListSize = MDValueList.size();
2901
2902   // Add all the function arguments to the value table.
2903   for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2904     ValueList.push_back(I);
2905
2906   unsigned NextValueNo = ValueList.size();
2907   BasicBlock *CurBB = nullptr;
2908   unsigned CurBBNo = 0;
2909
2910   DebugLoc LastLoc;
2911   auto getLastInstruction = [&]() -> Instruction * {
2912     if (CurBB && !CurBB->empty())
2913       return &CurBB->back();
2914     else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
2915              !FunctionBBs[CurBBNo - 1]->empty())
2916       return &FunctionBBs[CurBBNo - 1]->back();
2917     return nullptr;
2918   };
2919
2920   // Read all the records.
2921   SmallVector<uint64_t, 64> Record;
2922   while (1) {
2923     BitstreamEntry Entry = Stream.advance();
2924
2925     switch (Entry.Kind) {
2926     case BitstreamEntry::Error:
2927       return Error("Malformed block");
2928     case BitstreamEntry::EndBlock:
2929       goto OutOfRecordLoop;
2930
2931     case BitstreamEntry::SubBlock:
2932       switch (Entry.ID) {
2933       default:  // Skip unknown content.
2934         if (Stream.SkipBlock())
2935           return Error("Invalid record");
2936         break;
2937       case bitc::CONSTANTS_BLOCK_ID:
2938         if (std::error_code EC = ParseConstants())
2939           return EC;
2940         NextValueNo = ValueList.size();
2941         break;
2942       case bitc::VALUE_SYMTAB_BLOCK_ID:
2943         if (std::error_code EC = ParseValueSymbolTable())
2944           return EC;
2945         break;
2946       case bitc::METADATA_ATTACHMENT_ID:
2947         if (std::error_code EC = ParseMetadataAttachment())
2948           return EC;
2949         break;
2950       case bitc::METADATA_BLOCK_ID:
2951         if (std::error_code EC = ParseMetadata())
2952           return EC;
2953         break;
2954       case bitc::USELIST_BLOCK_ID:
2955         if (std::error_code EC = ParseUseLists())
2956           return EC;
2957         break;
2958       }
2959       continue;
2960
2961     case BitstreamEntry::Record:
2962       // The interesting case.
2963       break;
2964     }
2965
2966     // Read a record.
2967     Record.clear();
2968     Instruction *I = nullptr;
2969     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
2970     switch (BitCode) {
2971     default: // Default behavior: reject
2972       return Error("Invalid value");
2973     case bitc::FUNC_CODE_DECLAREBLOCKS: {   // DECLAREBLOCKS: [nblocks]
2974       if (Record.size() < 1 || Record[0] == 0)
2975         return Error("Invalid record");
2976       // Create all the basic blocks for the function.
2977       FunctionBBs.resize(Record[0]);
2978
2979       // See if anything took the address of blocks in this function.
2980       auto BBFRI = BasicBlockFwdRefs.find(F);
2981       if (BBFRI == BasicBlockFwdRefs.end()) {
2982         for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
2983           FunctionBBs[i] = BasicBlock::Create(Context, "", F);
2984       } else {
2985         auto &BBRefs = BBFRI->second;
2986         // Check for invalid basic block references.
2987         if (BBRefs.size() > FunctionBBs.size())
2988           return Error("Invalid ID");
2989         assert(!BBRefs.empty() && "Unexpected empty array");
2990         assert(!BBRefs.front() && "Invalid reference to entry block");
2991         for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
2992              ++I)
2993           if (I < RE && BBRefs[I]) {
2994             BBRefs[I]->insertInto(F);
2995             FunctionBBs[I] = BBRefs[I];
2996           } else {
2997             FunctionBBs[I] = BasicBlock::Create(Context, "", F);
2998           }
2999
3000         // Erase from the table.
3001         BasicBlockFwdRefs.erase(BBFRI);
3002       }
3003
3004       CurBB = FunctionBBs[0];
3005       continue;
3006     }
3007
3008     case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
3009       // This record indicates that the last instruction is at the same
3010       // location as the previous instruction with a location.
3011       I = getLastInstruction();
3012
3013       if (!I)
3014         return Error("Invalid record");
3015       I->setDebugLoc(LastLoc);
3016       I = nullptr;
3017       continue;
3018
3019     case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
3020       I = getLastInstruction();
3021       if (!I || Record.size() < 4)
3022         return Error("Invalid record");
3023
3024       unsigned Line = Record[0], Col = Record[1];
3025       unsigned ScopeID = Record[2], IAID = Record[3];
3026
3027       MDNode *Scope = nullptr, *IA = nullptr;
3028       if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
3029       if (IAID)    IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
3030       LastLoc = DebugLoc::get(Line, Col, Scope, IA);
3031       I->setDebugLoc(LastLoc);
3032       I = nullptr;
3033       continue;
3034     }
3035
3036     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
3037       unsigned OpNum = 0;
3038       Value *LHS, *RHS;
3039       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3040           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
3041           OpNum+1 > Record.size())
3042         return Error("Invalid record");
3043
3044       int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
3045       if (Opc == -1)
3046         return Error("Invalid record");
3047       I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3048       InstructionList.push_back(I);
3049       if (OpNum < Record.size()) {
3050         if (Opc == Instruction::Add ||
3051             Opc == Instruction::Sub ||
3052             Opc == Instruction::Mul ||
3053             Opc == Instruction::Shl) {
3054           if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3055             cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
3056           if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3057             cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
3058         } else if (Opc == Instruction::SDiv ||
3059                    Opc == Instruction::UDiv ||
3060                    Opc == Instruction::LShr ||
3061                    Opc == Instruction::AShr) {
3062           if (Record[OpNum] & (1 << bitc::PEO_EXACT))
3063             cast<BinaryOperator>(I)->setIsExact(true);
3064         } else if (isa<FPMathOperator>(I)) {
3065           FastMathFlags FMF;
3066           if (0 != (Record[OpNum] & FastMathFlags::UnsafeAlgebra))
3067             FMF.setUnsafeAlgebra();
3068           if (0 != (Record[OpNum] & FastMathFlags::NoNaNs))
3069             FMF.setNoNaNs();
3070           if (0 != (Record[OpNum] & FastMathFlags::NoInfs))
3071             FMF.setNoInfs();
3072           if (0 != (Record[OpNum] & FastMathFlags::NoSignedZeros))
3073             FMF.setNoSignedZeros();
3074           if (0 != (Record[OpNum] & FastMathFlags::AllowReciprocal))
3075             FMF.setAllowReciprocal();
3076           if (FMF.any())
3077             I->setFastMathFlags(FMF);
3078         }
3079
3080       }
3081       break;
3082     }
3083     case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
3084       unsigned OpNum = 0;
3085       Value *Op;
3086       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3087           OpNum+2 != Record.size())
3088         return Error("Invalid record");
3089
3090       Type *ResTy = getTypeByID(Record[OpNum]);
3091       int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
3092       if (Opc == -1 || !ResTy)
3093         return Error("Invalid record");
3094       Instruction *Temp = nullptr;
3095       if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
3096         if (Temp) {
3097           InstructionList.push_back(Temp);
3098           CurBB->getInstList().push_back(Temp);
3099         }
3100       } else {
3101         I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
3102       }
3103       InstructionList.push_back(I);
3104       break;
3105     }
3106     case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
3107     case bitc::FUNC_CODE_INST_GEP_OLD:
3108     case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
3109       unsigned OpNum = 0;
3110
3111       Type *Ty;
3112       bool InBounds;
3113
3114       if (BitCode == bitc::FUNC_CODE_INST_GEP) {
3115         InBounds = Record[OpNum++];
3116         Ty = getTypeByID(Record[OpNum++]);
3117       } else {
3118         InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
3119         Ty = nullptr;
3120       }
3121
3122       Value *BasePtr;
3123       if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
3124         return Error("Invalid record");
3125
3126       if (Ty &&
3127           Ty !=
3128               cast<SequentialType>(BasePtr->getType()->getScalarType())
3129                   ->getElementType())
3130         return Error(
3131             "Explicit gep type does not match pointee type of pointer operand");
3132
3133       SmallVector<Value*, 16> GEPIdx;
3134       while (OpNum != Record.size()) {
3135         Value *Op;
3136         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3137           return Error("Invalid record");
3138         GEPIdx.push_back(Op);
3139       }
3140
3141       I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
3142
3143       InstructionList.push_back(I);
3144       if (InBounds)
3145         cast<GetElementPtrInst>(I)->setIsInBounds(true);
3146       break;
3147     }
3148
3149     case bitc::FUNC_CODE_INST_EXTRACTVAL: {
3150                                        // EXTRACTVAL: [opty, opval, n x indices]
3151       unsigned OpNum = 0;
3152       Value *Agg;
3153       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
3154         return Error("Invalid record");
3155
3156       SmallVector<unsigned, 4> EXTRACTVALIdx;
3157       Type *CurTy = Agg->getType();
3158       for (unsigned RecSize = Record.size();
3159            OpNum != RecSize; ++OpNum) {
3160         bool IsArray = CurTy->isArrayTy();
3161         bool IsStruct = CurTy->isStructTy();
3162         uint64_t Index = Record[OpNum];
3163
3164         if (!IsStruct && !IsArray)
3165           return Error("EXTRACTVAL: Invalid type");
3166         if ((unsigned)Index != Index)
3167           return Error("Invalid value");
3168         if (IsStruct && Index >= CurTy->subtypes().size())
3169           return Error("EXTRACTVAL: Invalid struct index");
3170         if (IsArray && Index >= CurTy->getArrayNumElements())
3171           return Error("EXTRACTVAL: Invalid array index");
3172         EXTRACTVALIdx.push_back((unsigned)Index);
3173
3174         if (IsStruct)
3175           CurTy = CurTy->subtypes()[Index];
3176         else
3177           CurTy = CurTy->subtypes()[0];
3178       }
3179
3180       I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
3181       InstructionList.push_back(I);
3182       break;
3183     }
3184
3185     case bitc::FUNC_CODE_INST_INSERTVAL: {
3186                            // INSERTVAL: [opty, opval, opty, opval, n x indices]
3187       unsigned OpNum = 0;
3188       Value *Agg;
3189       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
3190         return Error("Invalid record");
3191       Value *Val;
3192       if (getValueTypePair(Record, OpNum, NextValueNo, Val))
3193         return Error("Invalid record");
3194
3195       SmallVector<unsigned, 4> INSERTVALIdx;
3196       Type *CurTy = Agg->getType();
3197       for (unsigned RecSize = Record.size();
3198            OpNum != RecSize; ++OpNum) {
3199         bool IsArray = CurTy->isArrayTy();
3200         bool IsStruct = CurTy->isStructTy();
3201         uint64_t Index = Record[OpNum];
3202
3203         if (!IsStruct && !IsArray)
3204           return Error("INSERTVAL: Invalid type");
3205         if (!CurTy->isStructTy() && !CurTy->isArrayTy())
3206           return Error("Invalid type");
3207         if ((unsigned)Index != Index)
3208           return Error("Invalid value");
3209         if (IsStruct && Index >= CurTy->subtypes().size())
3210           return Error("INSERTVAL: Invalid struct index");
3211         if (IsArray && Index >= CurTy->getArrayNumElements())
3212           return Error("INSERTVAL: Invalid array index");
3213
3214         INSERTVALIdx.push_back((unsigned)Index);
3215         if (IsStruct)
3216           CurTy = CurTy->subtypes()[Index];
3217         else
3218           CurTy = CurTy->subtypes()[0];
3219       }
3220
3221       I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
3222       InstructionList.push_back(I);
3223       break;
3224     }
3225
3226     case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
3227       // obsolete form of select
3228       // handles select i1 ... in old bitcode
3229       unsigned OpNum = 0;
3230       Value *TrueVal, *FalseVal, *Cond;
3231       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
3232           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
3233           popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
3234         return Error("Invalid record");
3235
3236       I = SelectInst::Create(Cond, TrueVal, FalseVal);
3237       InstructionList.push_back(I);
3238       break;
3239     }
3240
3241     case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
3242       // new form of select
3243       // handles select i1 or select [N x i1]
3244       unsigned OpNum = 0;
3245       Value *TrueVal, *FalseVal, *Cond;
3246       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
3247           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
3248           getValueTypePair(Record, OpNum, NextValueNo, Cond))
3249         return Error("Invalid record");
3250
3251       // select condition can be either i1 or [N x i1]
3252       if (VectorType* vector_type =
3253           dyn_cast<VectorType>(Cond->getType())) {
3254         // expect <n x i1>
3255         if (vector_type->getElementType() != Type::getInt1Ty(Context))
3256           return Error("Invalid type for value");
3257       } else {
3258         // expect i1
3259         if (Cond->getType() != Type::getInt1Ty(Context))
3260           return Error("Invalid type for value");
3261       }
3262
3263       I = SelectInst::Create(Cond, TrueVal, FalseVal);
3264       InstructionList.push_back(I);
3265       break;
3266     }
3267
3268     case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
3269       unsigned OpNum = 0;
3270       Value *Vec, *Idx;
3271       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
3272           getValueTypePair(Record, OpNum, NextValueNo, Idx))
3273         return Error("Invalid record");
3274       I = ExtractElementInst::Create(Vec, Idx);
3275       InstructionList.push_back(I);
3276       break;
3277     }
3278
3279     case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
3280       unsigned OpNum = 0;
3281       Value *Vec, *Elt, *Idx;
3282       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
3283           popValue(Record, OpNum, NextValueNo,
3284                    cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
3285           getValueTypePair(Record, OpNum, NextValueNo, Idx))
3286         return Error("Invalid record");
3287       I = InsertElementInst::Create(Vec, Elt, Idx);
3288       InstructionList.push_back(I);
3289       break;
3290     }
3291
3292     case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
3293       unsigned OpNum = 0;
3294       Value *Vec1, *Vec2, *Mask;
3295       if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
3296           popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
3297         return Error("Invalid record");
3298
3299       if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
3300         return Error("Invalid record");
3301       I = new ShuffleVectorInst(Vec1, Vec2, Mask);
3302       InstructionList.push_back(I);
3303       break;
3304     }
3305
3306     case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
3307       // Old form of ICmp/FCmp returning bool
3308       // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
3309       // both legal on vectors but had different behaviour.
3310     case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
3311       // FCmp/ICmp returning bool or vector of bool
3312
3313       unsigned OpNum = 0;
3314       Value *LHS, *RHS;
3315       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3316           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
3317           OpNum+1 != Record.size())
3318         return Error("Invalid record");
3319
3320       if (LHS->getType()->isFPOrFPVectorTy())
3321         I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
3322       else
3323         I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
3324       InstructionList.push_back(I);
3325       break;
3326     }
3327
3328     case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
3329       {
3330         unsigned Size = Record.size();
3331         if (Size == 0) {
3332           I = ReturnInst::Create(Context);
3333           InstructionList.push_back(I);
3334           break;
3335         }
3336
3337         unsigned OpNum = 0;
3338         Value *Op = nullptr;
3339         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3340           return Error("Invalid record");
3341         if (OpNum != Record.size())
3342           return Error("Invalid record");
3343
3344         I = ReturnInst::Create(Context, Op);
3345         InstructionList.push_back(I);
3346         break;
3347       }
3348     case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
3349       if (Record.size() != 1 && Record.size() != 3)
3350         return Error("Invalid record");
3351       BasicBlock *TrueDest = getBasicBlock(Record[0]);
3352       if (!TrueDest)
3353         return Error("Invalid record");
3354
3355       if (Record.size() == 1) {
3356         I = BranchInst::Create(TrueDest);
3357         InstructionList.push_back(I);
3358       }
3359       else {
3360         BasicBlock *FalseDest = getBasicBlock(Record[1]);
3361         Value *Cond = getValue(Record, 2, NextValueNo,
3362                                Type::getInt1Ty(Context));
3363         if (!FalseDest || !Cond)
3364           return Error("Invalid record");
3365         I = BranchInst::Create(TrueDest, FalseDest, Cond);
3366         InstructionList.push_back(I);
3367       }
3368       break;
3369     }
3370     case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
3371       // Check magic
3372       if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
3373         // "New" SwitchInst format with case ranges. The changes to write this
3374         // format were reverted but we still recognize bitcode that uses it.
3375         // Hopefully someday we will have support for case ranges and can use
3376         // this format again.
3377
3378         Type *OpTy = getTypeByID(Record[1]);
3379         unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
3380
3381         Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
3382         BasicBlock *Default = getBasicBlock(Record[3]);
3383         if (!OpTy || !Cond || !Default)
3384           return Error("Invalid record");
3385
3386         unsigned NumCases = Record[4];
3387
3388         SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
3389         InstructionList.push_back(SI);
3390
3391         unsigned CurIdx = 5;
3392         for (unsigned i = 0; i != NumCases; ++i) {
3393           SmallVector<ConstantInt*, 1> CaseVals;
3394           unsigned NumItems = Record[CurIdx++];
3395           for (unsigned ci = 0; ci != NumItems; ++ci) {
3396             bool isSingleNumber = Record[CurIdx++];
3397
3398             APInt Low;
3399             unsigned ActiveWords = 1;
3400             if (ValueBitWidth > 64)
3401               ActiveWords = Record[CurIdx++];
3402             Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
3403                                 ValueBitWidth);
3404             CurIdx += ActiveWords;
3405
3406             if (!isSingleNumber) {
3407               ActiveWords = 1;
3408               if (ValueBitWidth > 64)
3409                 ActiveWords = Record[CurIdx++];
3410               APInt High =
3411                   ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
3412                                 ValueBitWidth);
3413               CurIdx += ActiveWords;
3414
3415               // FIXME: It is not clear whether values in the range should be
3416               // compared as signed or unsigned values. The partially
3417               // implemented changes that used this format in the past used
3418               // unsigned comparisons.
3419               for ( ; Low.ule(High); ++Low)
3420                 CaseVals.push_back(ConstantInt::get(Context, Low));
3421             } else
3422               CaseVals.push_back(ConstantInt::get(Context, Low));
3423           }
3424           BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
3425           for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
3426                  cve = CaseVals.end(); cvi != cve; ++cvi)
3427             SI->addCase(*cvi, DestBB);
3428         }
3429         I = SI;
3430         break;
3431       }
3432
3433       // Old SwitchInst format without case ranges.
3434
3435       if (Record.size() < 3 || (Record.size() & 1) == 0)
3436         return Error("Invalid record");
3437       Type *OpTy = getTypeByID(Record[0]);
3438       Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
3439       BasicBlock *Default = getBasicBlock(Record[2]);
3440       if (!OpTy || !Cond || !Default)
3441         return Error("Invalid record");
3442       unsigned NumCases = (Record.size()-3)/2;
3443       SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
3444       InstructionList.push_back(SI);
3445       for (unsigned i = 0, e = NumCases; i != e; ++i) {
3446         ConstantInt *CaseVal =
3447           dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
3448         BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
3449         if (!CaseVal || !DestBB) {
3450           delete SI;
3451           return Error("Invalid record");
3452         }
3453         SI->addCase(CaseVal, DestBB);
3454       }
3455       I = SI;
3456       break;
3457     }
3458     case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
3459       if (Record.size() < 2)
3460         return Error("Invalid record");
3461       Type *OpTy = getTypeByID(Record[0]);
3462       Value *Address = getValue(Record, 1, NextValueNo, OpTy);
3463       if (!OpTy || !Address)
3464         return Error("Invalid record");
3465       unsigned NumDests = Record.size()-2;
3466       IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
3467       InstructionList.push_back(IBI);
3468       for (unsigned i = 0, e = NumDests; i != e; ++i) {
3469         if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
3470           IBI->addDestination(DestBB);
3471         } else {
3472           delete IBI;
3473           return Error("Invalid record");
3474         }
3475       }
3476       I = IBI;
3477       break;
3478     }
3479
3480     case bitc::FUNC_CODE_INST_INVOKE: {
3481       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
3482       if (Record.size() < 4)
3483         return Error("Invalid record");
3484       AttributeSet PAL = getAttributes(Record[0]);
3485       unsigned CCInfo = Record[1];
3486       BasicBlock *NormalBB = getBasicBlock(Record[2]);
3487       BasicBlock *UnwindBB = getBasicBlock(Record[3]);
3488
3489       unsigned OpNum = 4;
3490       Value *Callee;
3491       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
3492         return Error("Invalid record");
3493
3494       PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
3495       FunctionType *FTy = !CalleeTy ? nullptr :
3496         dyn_cast<FunctionType>(CalleeTy->getElementType());
3497
3498       // Check that the right number of fixed parameters are here.
3499       if (!FTy || !NormalBB || !UnwindBB ||
3500           Record.size() < OpNum+FTy->getNumParams())
3501         return Error("Invalid record");
3502
3503       SmallVector<Value*, 16> Ops;
3504       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
3505         Ops.push_back(getValue(Record, OpNum, NextValueNo,
3506                                FTy->getParamType(i)));
3507         if (!Ops.back())
3508           return Error("Invalid record");
3509       }
3510
3511       if (!FTy->isVarArg()) {
3512         if (Record.size() != OpNum)
3513           return Error("Invalid record");
3514       } else {
3515         // Read type/value pairs for varargs params.
3516         while (OpNum != Record.size()) {
3517           Value *Op;
3518           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3519             return Error("Invalid record");
3520           Ops.push_back(Op);
3521         }
3522       }
3523
3524       I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
3525       InstructionList.push_back(I);
3526       cast<InvokeInst>(I)->setCallingConv(
3527         static_cast<CallingConv::ID>(CCInfo));
3528       cast<InvokeInst>(I)->setAttributes(PAL);
3529       break;
3530     }
3531     case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
3532       unsigned Idx = 0;
3533       Value *Val = nullptr;
3534       if (getValueTypePair(Record, Idx, NextValueNo, Val))
3535         return Error("Invalid record");
3536       I = ResumeInst::Create(Val);
3537       InstructionList.push_back(I);
3538       break;
3539     }
3540     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
3541       I = new UnreachableInst(Context);
3542       InstructionList.push_back(I);
3543       break;
3544     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
3545       if (Record.size() < 1 || ((Record.size()-1)&1))
3546         return Error("Invalid record");
3547       Type *Ty = getTypeByID(Record[0]);
3548       if (!Ty)
3549         return Error("Invalid record");
3550
3551       PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
3552       InstructionList.push_back(PN);
3553
3554       for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
3555         Value *V;
3556         // With the new function encoding, it is possible that operands have
3557         // negative IDs (for forward references).  Use a signed VBR
3558         // representation to keep the encoding small.
3559         if (UseRelativeIDs)
3560           V = getValueSigned(Record, 1+i, NextValueNo, Ty);
3561         else
3562           V = getValue(Record, 1+i, NextValueNo, Ty);
3563         BasicBlock *BB = getBasicBlock(Record[2+i]);
3564         if (!V || !BB)
3565           return Error("Invalid record");
3566         PN->addIncoming(V, BB);
3567       }
3568       I = PN;
3569       break;
3570     }
3571
3572     case bitc::FUNC_CODE_INST_LANDINGPAD: {
3573       // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
3574       unsigned Idx = 0;
3575       if (Record.size() < 4)
3576         return Error("Invalid record");
3577       Type *Ty = getTypeByID(Record[Idx++]);
3578       if (!Ty)
3579         return Error("Invalid record");
3580       Value *PersFn = nullptr;
3581       if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
3582         return Error("Invalid record");
3583
3584       bool IsCleanup = !!Record[Idx++];
3585       unsigned NumClauses = Record[Idx++];
3586       LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
3587       LP->setCleanup(IsCleanup);
3588       for (unsigned J = 0; J != NumClauses; ++J) {
3589         LandingPadInst::ClauseType CT =
3590           LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
3591         Value *Val;
3592
3593         if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
3594           delete LP;
3595           return Error("Invalid record");
3596         }
3597
3598         assert((CT != LandingPadInst::Catch ||
3599                 !isa<ArrayType>(Val->getType())) &&
3600                "Catch clause has a invalid type!");
3601         assert((CT != LandingPadInst::Filter ||
3602                 isa<ArrayType>(Val->getType())) &&
3603                "Filter clause has invalid type!");
3604         LP->addClause(cast<Constant>(Val));
3605       }
3606
3607       I = LP;
3608       InstructionList.push_back(I);
3609       break;
3610     }
3611
3612     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
3613       if (Record.size() != 4)
3614         return Error("Invalid record");
3615       PointerType *Ty =
3616         dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
3617       Type *OpTy = getTypeByID(Record[1]);
3618       Value *Size = getFnValueByID(Record[2], OpTy);
3619       uint64_t AlignRecord = Record[3];
3620       const uint64_t InAllocaMask = uint64_t(1) << 5;
3621       bool InAlloca = AlignRecord & InAllocaMask;
3622       unsigned Align;
3623       if (std::error_code EC =
3624           parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) {
3625         return EC;
3626       }
3627       if (!Ty || !Size)
3628         return Error("Invalid record");
3629       AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align);
3630       AI->setUsedWithInAlloca(InAlloca);
3631       I = AI;
3632       InstructionList.push_back(I);
3633       break;
3634     }
3635     case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
3636       unsigned OpNum = 0;
3637       Value *Op;
3638       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3639           (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
3640         return Error("Invalid record");
3641
3642       Type *Ty = nullptr;
3643       if (OpNum + 3 == Record.size())
3644         Ty = getTypeByID(Record[OpNum++]);
3645
3646       unsigned Align;
3647       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
3648         return EC;
3649       I = new LoadInst(Op, "", Record[OpNum+1], Align);
3650
3651       if (Ty && Ty != I->getType())
3652         return Error("Explicit load type does not match pointee type of "
3653                      "pointer operand");
3654
3655       InstructionList.push_back(I);
3656       break;
3657     }
3658     case bitc::FUNC_CODE_INST_LOADATOMIC: {
3659        // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
3660       unsigned OpNum = 0;
3661       Value *Op;
3662       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3663           (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
3664         return Error("Invalid record");
3665
3666       Type *Ty = nullptr;
3667       if (OpNum + 5 == Record.size())
3668         Ty = getTypeByID(Record[OpNum++]);
3669
3670       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
3671       if (Ordering == NotAtomic || Ordering == Release ||
3672           Ordering == AcquireRelease)
3673         return Error("Invalid record");
3674       if (Ordering != NotAtomic && Record[OpNum] == 0)
3675         return Error("Invalid record");
3676       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3677
3678       unsigned Align;
3679       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
3680         return EC;
3681       I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
3682
3683       (void)Ty;
3684       assert((!Ty || Ty == I->getType()) &&
3685              "Explicit type doesn't match pointee type of the first operand");
3686
3687       InstructionList.push_back(I);
3688       break;
3689     }
3690     case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
3691       unsigned OpNum = 0;
3692       Value *Val, *Ptr;
3693       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
3694           popValue(Record, OpNum, NextValueNo,
3695                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3696           OpNum+2 != Record.size())
3697         return Error("Invalid record");
3698       unsigned Align;
3699       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
3700         return EC;
3701       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
3702       InstructionList.push_back(I);
3703       break;
3704     }
3705     case bitc::FUNC_CODE_INST_STOREATOMIC: {
3706       // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
3707       unsigned OpNum = 0;
3708       Value *Val, *Ptr;
3709       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
3710           popValue(Record, OpNum, NextValueNo,
3711                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3712           OpNum+4 != Record.size())
3713         return Error("Invalid record");
3714
3715       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
3716       if (Ordering == NotAtomic || Ordering == Acquire ||
3717           Ordering == AcquireRelease)
3718         return Error("Invalid record");
3719       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3720       if (Ordering != NotAtomic && Record[OpNum] == 0)
3721         return Error("Invalid record");
3722
3723       unsigned Align;
3724       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
3725         return EC;
3726       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
3727       InstructionList.push_back(I);
3728       break;
3729     }
3730     case bitc::FUNC_CODE_INST_CMPXCHG: {
3731       // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
3732       //          failureordering?, isweak?]
3733       unsigned OpNum = 0;
3734       Value *Ptr, *Cmp, *New;
3735       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
3736           popValue(Record, OpNum, NextValueNo,
3737                     cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
3738           popValue(Record, OpNum, NextValueNo,
3739                     cast<PointerType>(Ptr->getType())->getElementType(), New) ||
3740           (Record.size() < OpNum + 3 || Record.size() > OpNum + 5))
3741         return Error("Invalid record");
3742       AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]);
3743       if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
3744         return Error("Invalid record");
3745       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
3746
3747       AtomicOrdering FailureOrdering;
3748       if (Record.size() < 7)
3749         FailureOrdering =
3750             AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
3751       else
3752         FailureOrdering = GetDecodedOrdering(Record[OpNum+3]);
3753
3754       I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
3755                                 SynchScope);
3756       cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
3757
3758       if (Record.size() < 8) {
3759         // Before weak cmpxchgs existed, the instruction simply returned the
3760         // value loaded from memory, so bitcode files from that era will be
3761         // expecting the first component of a modern cmpxchg.
3762         CurBB->getInstList().push_back(I);
3763         I = ExtractValueInst::Create(I, 0);
3764       } else {
3765         cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
3766       }
3767
3768       InstructionList.push_back(I);
3769       break;
3770     }
3771     case bitc::FUNC_CODE_INST_ATOMICRMW: {
3772       // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
3773       unsigned OpNum = 0;
3774       Value *Ptr, *Val;
3775       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
3776           popValue(Record, OpNum, NextValueNo,
3777                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3778           OpNum+4 != Record.size())
3779         return Error("Invalid record");
3780       AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
3781       if (Operation < AtomicRMWInst::FIRST_BINOP ||
3782           Operation > AtomicRMWInst::LAST_BINOP)
3783         return Error("Invalid record");
3784       AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
3785       if (Ordering == NotAtomic || Ordering == Unordered)
3786         return Error("Invalid record");
3787       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
3788       I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
3789       cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
3790       InstructionList.push_back(I);
3791       break;
3792     }
3793     case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
3794       if (2 != Record.size())
3795         return Error("Invalid record");
3796       AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
3797       if (Ordering == NotAtomic || Ordering == Unordered ||
3798           Ordering == Monotonic)
3799         return Error("Invalid record");
3800       SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
3801       I = new FenceInst(Context, Ordering, SynchScope);
3802       InstructionList.push_back(I);
3803       break;
3804     }
3805     case bitc::FUNC_CODE_INST_CALL: {
3806       // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
3807       if (Record.size() < 3)
3808         return Error("Invalid record");
3809
3810       AttributeSet PAL = getAttributes(Record[0]);
3811       unsigned CCInfo = Record[1];
3812
3813       unsigned OpNum = 2;
3814       Value *Callee;
3815       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
3816         return Error("Invalid record");
3817
3818       PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
3819       FunctionType *FTy = nullptr;
3820       if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
3821       if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
3822         return Error("Invalid record");
3823
3824       SmallVector<Value*, 16> Args;
3825       // Read the fixed params.
3826       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
3827         if (FTy->getParamType(i)->isLabelTy())
3828           Args.push_back(getBasicBlock(Record[OpNum]));
3829         else
3830           Args.push_back(getValue(Record, OpNum, NextValueNo,
3831                                   FTy->getParamType(i)));
3832         if (!Args.back())
3833           return Error("Invalid record");
3834       }
3835
3836       // Read type/value pairs for varargs params.
3837       if (!FTy->isVarArg()) {
3838         if (OpNum != Record.size())
3839           return Error("Invalid record");
3840       } else {
3841         while (OpNum != Record.size()) {
3842           Value *Op;
3843           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3844             return Error("Invalid record");
3845           Args.push_back(Op);
3846         }
3847       }
3848
3849       I = CallInst::Create(Callee, Args);
3850       InstructionList.push_back(I);
3851       cast<CallInst>(I)->setCallingConv(
3852           static_cast<CallingConv::ID>((~(1U << 14) & CCInfo) >> 1));
3853       CallInst::TailCallKind TCK = CallInst::TCK_None;
3854       if (CCInfo & 1)
3855         TCK = CallInst::TCK_Tail;
3856       if (CCInfo & (1 << 14))
3857         TCK = CallInst::TCK_MustTail;
3858       cast<CallInst>(I)->setTailCallKind(TCK);
3859       cast<CallInst>(I)->setAttributes(PAL);
3860       break;
3861     }
3862     case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3863       if (Record.size() < 3)
3864         return Error("Invalid record");
3865       Type *OpTy = getTypeByID(Record[0]);
3866       Value *Op = getValue(Record, 1, NextValueNo, OpTy);
3867       Type *ResTy = getTypeByID(Record[2]);
3868       if (!OpTy || !Op || !ResTy)
3869         return Error("Invalid record");
3870       I = new VAArgInst(Op, ResTy);
3871       InstructionList.push_back(I);
3872       break;
3873     }
3874     }
3875
3876     // Add instruction to end of current BB.  If there is no current BB, reject
3877     // this file.
3878     if (!CurBB) {
3879       delete I;
3880       return Error("Invalid instruction with no BB");
3881     }
3882     CurBB->getInstList().push_back(I);
3883
3884     // If this was a terminator instruction, move to the next block.
3885     if (isa<TerminatorInst>(I)) {
3886       ++CurBBNo;
3887       CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
3888     }
3889
3890     // Non-void values get registered in the value table for future use.
3891     if (I && !I->getType()->isVoidTy())
3892       ValueList.AssignValue(I, NextValueNo++);
3893   }
3894
3895 OutOfRecordLoop:
3896
3897   // Check the function list for unresolved values.
3898   if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
3899     if (!A->getParent()) {
3900       // We found at least one unresolved value.  Nuke them all to avoid leaks.
3901       for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
3902         if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
3903           A->replaceAllUsesWith(UndefValue::get(A->getType()));
3904           delete A;
3905         }
3906       }
3907       return Error("Never resolved value found in function");
3908     }
3909   }
3910
3911   // FIXME: Check for unresolved forward-declared metadata references
3912   // and clean up leaks.
3913
3914   // Trim the value list down to the size it was before we parsed this function.
3915   ValueList.shrinkTo(ModuleValueListSize);
3916   MDValueList.shrinkTo(ModuleMDValueListSize);
3917   std::vector<BasicBlock*>().swap(FunctionBBs);
3918   return std::error_code();
3919 }
3920
3921 /// Find the function body in the bitcode stream
3922 std::error_code BitcodeReader::FindFunctionInStream(
3923     Function *F,
3924     DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
3925   while (DeferredFunctionInfoIterator->second == 0) {
3926     if (Stream.AtEndOfStream())
3927       return Error("Could not find function in stream");
3928     // ParseModule will parse the next body in the stream and set its
3929     // position in the DeferredFunctionInfo map.
3930     if (std::error_code EC = ParseModule(true))
3931       return EC;
3932   }
3933   return std::error_code();
3934 }
3935
3936 //===----------------------------------------------------------------------===//
3937 // GVMaterializer implementation
3938 //===----------------------------------------------------------------------===//
3939
3940 void BitcodeReader::releaseBuffer() { Buffer.release(); }
3941
3942 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
3943   if (std::error_code EC = materializeMetadata())
3944     return EC;
3945
3946   Function *F = dyn_cast<Function>(GV);
3947   // If it's not a function or is already material, ignore the request.
3948   if (!F || !F->isMaterializable())
3949     return std::error_code();
3950
3951   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
3952   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
3953   // If its position is recorded as 0, its body is somewhere in the stream
3954   // but we haven't seen it yet.
3955   if (DFII->second == 0 && LazyStreamer)
3956     if (std::error_code EC = FindFunctionInStream(F, DFII))
3957       return EC;
3958
3959   // Move the bit stream to the saved position of the deferred function body.
3960   Stream.JumpToBit(DFII->second);
3961
3962   if (std::error_code EC = ParseFunctionBody(F))
3963     return EC;
3964   F->setIsMaterializable(false);
3965
3966   // Upgrade any old intrinsic calls in the function.
3967   for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3968        E = UpgradedIntrinsics.end(); I != E; ++I) {
3969     if (I->first != I->second) {
3970       for (auto UI = I->first->user_begin(), UE = I->first->user_end();
3971            UI != UE;) {
3972         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3973           UpgradeIntrinsicCall(CI, I->second);
3974       }
3975     }
3976   }
3977
3978   // Bring in any functions that this function forward-referenced via
3979   // blockaddresses.
3980   return materializeForwardReferencedFunctions();
3981 }
3982
3983 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3984   const Function *F = dyn_cast<Function>(GV);
3985   if (!F || F->isDeclaration())
3986     return false;
3987
3988   // Dematerializing F would leave dangling references that wouldn't be
3989   // reconnected on re-materialization.
3990   if (BlockAddressesTaken.count(F))
3991     return false;
3992
3993   return DeferredFunctionInfo.count(const_cast<Function*>(F));
3994 }
3995
3996 void BitcodeReader::Dematerialize(GlobalValue *GV) {
3997   Function *F = dyn_cast<Function>(GV);
3998   // If this function isn't dematerializable, this is a noop.
3999   if (!F || !isDematerializable(F))
4000     return;
4001
4002   assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
4003
4004   // Just forget the function body, we can remat it later.
4005   F->dropAllReferences();
4006   F->setIsMaterializable(true);
4007 }
4008
4009 std::error_code BitcodeReader::MaterializeModule(Module *M) {
4010   assert(M == TheModule &&
4011          "Can only Materialize the Module this BitcodeReader is attached to.");
4012
4013   if (std::error_code EC = materializeMetadata())
4014     return EC;
4015
4016   // Promise to materialize all forward references.
4017   WillMaterializeAllForwardRefs = true;
4018
4019   // Iterate over the module, deserializing any functions that are still on
4020   // disk.
4021   for (Module::iterator F = TheModule->begin(), E = TheModule->end();
4022        F != E; ++F) {
4023     if (std::error_code EC = materialize(F))
4024       return EC;
4025   }
4026   // At this point, if there are any function bodies, the current bit is
4027   // pointing to the END_BLOCK record after them. Now make sure the rest
4028   // of the bits in the module have been read.
4029   if (NextUnreadBit)
4030     ParseModule(true);
4031
4032   // Check that all block address forward references got resolved (as we
4033   // promised above).
4034   if (!BasicBlockFwdRefs.empty())
4035     return Error("Never resolved function from blockaddress");
4036
4037   // Upgrade any intrinsic calls that slipped through (should not happen!) and
4038   // delete the old functions to clean up. We can't do this unless the entire
4039   // module is materialized because there could always be another function body
4040   // with calls to the old function.
4041   for (std::vector<std::pair<Function*, Function*> >::iterator I =
4042        UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
4043     if (I->first != I->second) {
4044       for (auto UI = I->first->user_begin(), UE = I->first->user_end();
4045            UI != UE;) {
4046         if (CallInst* CI = dyn_cast<CallInst>(*UI++))
4047           UpgradeIntrinsicCall(CI, I->second);
4048       }
4049       if (!I->first->use_empty())
4050         I->first->replaceAllUsesWith(I->second);
4051       I->first->eraseFromParent();
4052     }
4053   }
4054   std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
4055
4056   for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
4057     UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
4058
4059   UpgradeDebugInfo(*M);
4060   return std::error_code();
4061 }
4062
4063 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
4064   return IdentifiedStructTypes;
4065 }
4066
4067 std::error_code BitcodeReader::InitStream() {
4068   if (LazyStreamer)
4069     return InitLazyStream();
4070   return InitStreamFromBuffer();
4071 }
4072
4073 std::error_code BitcodeReader::InitStreamFromBuffer() {
4074   const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
4075   const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
4076
4077   if (Buffer->getBufferSize() & 3)
4078     return Error("Invalid bitcode signature");
4079
4080   // If we have a wrapper header, parse it and ignore the non-bc file contents.
4081   // The magic number is 0x0B17C0DE stored in little endian.
4082   if (isBitcodeWrapper(BufPtr, BufEnd))
4083     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
4084       return Error("Invalid bitcode wrapper header");
4085
4086   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
4087   Stream.init(&*StreamFile);
4088
4089   return std::error_code();
4090 }
4091
4092 std::error_code BitcodeReader::InitLazyStream() {
4093   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
4094   // see it.
4095   auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer);
4096   StreamingMemoryObject &Bytes = *OwnedBytes;
4097   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
4098   Stream.init(&*StreamFile);
4099
4100   unsigned char buf[16];
4101   if (Bytes.readBytes(buf, 16, 0) != 16)
4102     return Error("Invalid bitcode signature");
4103
4104   if (!isBitcode(buf, buf + 16))
4105     return Error("Invalid bitcode signature");
4106
4107   if (isBitcodeWrapper(buf, buf + 4)) {
4108     const unsigned char *bitcodeStart = buf;
4109     const unsigned char *bitcodeEnd = buf + 16;
4110     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
4111     Bytes.dropLeadingBytes(bitcodeStart - buf);
4112     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
4113   }
4114   return std::error_code();
4115 }
4116
4117 namespace {
4118 class BitcodeErrorCategoryType : public std::error_category {
4119   const char *name() const LLVM_NOEXCEPT override {
4120     return "llvm.bitcode";
4121   }
4122   std::string message(int IE) const override {
4123     BitcodeError E = static_cast<BitcodeError>(IE);
4124     switch (E) {
4125     case BitcodeError::InvalidBitcodeSignature:
4126       return "Invalid bitcode signature";
4127     case BitcodeError::CorruptedBitcode:
4128       return "Corrupted bitcode";
4129     }
4130     llvm_unreachable("Unknown error type!");
4131   }
4132 };
4133 }
4134
4135 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
4136
4137 const std::error_category &llvm::BitcodeErrorCategory() {
4138   return *ErrorCategory;
4139 }
4140
4141 //===----------------------------------------------------------------------===//
4142 // External interface
4143 //===----------------------------------------------------------------------===//
4144
4145 /// \brief Get a lazy one-at-time loading module from bitcode.
4146 ///
4147 /// This isn't always used in a lazy context.  In particular, it's also used by
4148 /// \a parseBitcodeFile().  If this is truly lazy, then we need to eagerly pull
4149 /// in forward-referenced functions from block address references.
4150 ///
4151 /// \param[in] WillMaterializeAll Set to \c true if the caller promises to
4152 /// materialize everything -- in particular, if this isn't truly lazy.
4153 static ErrorOr<Module *>
4154 getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
4155                          LLVMContext &Context, bool WillMaterializeAll,
4156                          DiagnosticHandlerFunction DiagnosticHandler,
4157                          bool ShouldLazyLoadMetadata = false) {
4158   Module *M = new Module(Buffer->getBufferIdentifier(), Context);
4159   BitcodeReader *R =
4160       new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
4161   M->setMaterializer(R);
4162
4163   auto cleanupOnError = [&](std::error_code EC) {
4164     R->releaseBuffer(); // Never take ownership on error.
4165     delete M;  // Also deletes R.
4166     return EC;
4167   };
4168
4169   // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
4170   if (std::error_code EC = R->ParseBitcodeInto(M, ShouldLazyLoadMetadata))
4171     return cleanupOnError(EC);
4172
4173   if (!WillMaterializeAll)
4174     // Resolve forward references from blockaddresses.
4175     if (std::error_code EC = R->materializeForwardReferencedFunctions())
4176       return cleanupOnError(EC);
4177
4178   Buffer.release(); // The BitcodeReader owns it now.
4179   return M;
4180 }
4181
4182 ErrorOr<Module *>
4183 llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
4184                            LLVMContext &Context,
4185                            DiagnosticHandlerFunction DiagnosticHandler,
4186                            bool ShouldLazyLoadMetadata) {
4187   return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
4188                                   DiagnosticHandler, ShouldLazyLoadMetadata);
4189 }
4190
4191 ErrorOr<std::unique_ptr<Module>>
4192 llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
4193                                LLVMContext &Context,
4194                                DiagnosticHandlerFunction DiagnosticHandler) {
4195   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
4196   BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler);
4197   M->setMaterializer(R);
4198   if (std::error_code EC = R->ParseBitcodeInto(M.get()))
4199     return EC;
4200   return std::move(M);
4201 }
4202
4203 ErrorOr<Module *>
4204 llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
4205                        DiagnosticHandlerFunction DiagnosticHandler) {
4206   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
4207   ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl(
4208       std::move(Buf), Context, true, DiagnosticHandler);
4209   if (!ModuleOrErr)
4210     return ModuleOrErr;
4211   Module *M = ModuleOrErr.get();
4212   // Read in the entire module, and destroy the BitcodeReader.
4213   if (std::error_code EC = M->materializeAllPermanently()) {
4214     delete M;
4215     return EC;
4216   }
4217
4218   // TODO: Restore the use-lists to the in-memory state when the bitcode was
4219   // written.  We must defer until the Module has been fully materialized.
4220
4221   return M;
4222 }
4223
4224 std::string
4225 llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
4226                              DiagnosticHandlerFunction DiagnosticHandler) {
4227   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
4228   auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context,
4229                                             DiagnosticHandler);
4230   ErrorOr<std::string> Triple = R->parseTriple();
4231   if (Triple.getError())
4232     return "";
4233   return Triple.get();
4234 }