1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "BitcodeReader.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Module.h"
21 #include "llvm/Operator.h"
22 #include "llvm/AutoUpgrade.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/DataStream.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/OperandTraits.h"
32 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
35 void BitcodeReader::materializeForwardReferencedFunctions() {
36 while (!BlockAddrFwdRefs.empty()) {
37 Function *F = BlockAddrFwdRefs.begin()->first;
42 void BitcodeReader::FreeState() {
46 std::vector<Type*>().swap(TypeList);
50 std::vector<AttrListPtr>().swap(MAttributes);
51 std::vector<BasicBlock*>().swap(FunctionBBs);
52 std::vector<Function*>().swap(FunctionsWithBodies);
53 DeferredFunctionInfo.clear();
57 //===----------------------------------------------------------------------===//
58 // Helper functions to implement forward reference resolution, etc.
59 //===----------------------------------------------------------------------===//
61 /// ConvertToString - Convert a string from a record into an std::string, return
63 template<typename StrTy>
64 static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
66 if (Idx > Record.size())
69 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
70 Result += (char)Record[i];
74 static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) {
76 default: // Map unknown/new linkages to external
77 case 0: return GlobalValue::ExternalLinkage;
78 case 1: return GlobalValue::WeakAnyLinkage;
79 case 2: return GlobalValue::AppendingLinkage;
80 case 3: return GlobalValue::InternalLinkage;
81 case 4: return GlobalValue::LinkOnceAnyLinkage;
82 case 5: return GlobalValue::DLLImportLinkage;
83 case 6: return GlobalValue::DLLExportLinkage;
84 case 7: return GlobalValue::ExternalWeakLinkage;
85 case 8: return GlobalValue::CommonLinkage;
86 case 9: return GlobalValue::PrivateLinkage;
87 case 10: return GlobalValue::WeakODRLinkage;
88 case 11: return GlobalValue::LinkOnceODRLinkage;
89 case 12: return GlobalValue::AvailableExternallyLinkage;
90 case 13: return GlobalValue::LinkerPrivateLinkage;
91 case 14: return GlobalValue::LinkerPrivateWeakLinkage;
92 case 15: return GlobalValue::LinkOnceODRAutoHideLinkage;
96 static GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
98 default: // Map unknown visibilities to default.
99 case 0: return GlobalValue::DefaultVisibility;
100 case 1: return GlobalValue::HiddenVisibility;
101 case 2: return GlobalValue::ProtectedVisibility;
105 static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
107 case 0: return GlobalVariable::NotThreadLocal;
108 default: // Map unknown non-zero value to general dynamic.
109 case 1: return GlobalVariable::GeneralDynamicTLSModel;
110 case 2: return GlobalVariable::LocalDynamicTLSModel;
111 case 3: return GlobalVariable::InitialExecTLSModel;
112 case 4: return GlobalVariable::LocalExecTLSModel;
116 static int GetDecodedCastOpcode(unsigned Val) {
119 case bitc::CAST_TRUNC : return Instruction::Trunc;
120 case bitc::CAST_ZEXT : return Instruction::ZExt;
121 case bitc::CAST_SEXT : return Instruction::SExt;
122 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
123 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
124 case bitc::CAST_UITOFP : return Instruction::UIToFP;
125 case bitc::CAST_SITOFP : return Instruction::SIToFP;
126 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
127 case bitc::CAST_FPEXT : return Instruction::FPExt;
128 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
129 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
130 case bitc::CAST_BITCAST : return Instruction::BitCast;
133 static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
136 case bitc::BINOP_ADD:
137 return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
138 case bitc::BINOP_SUB:
139 return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
140 case bitc::BINOP_MUL:
141 return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
142 case bitc::BINOP_UDIV: return Instruction::UDiv;
143 case bitc::BINOP_SDIV:
144 return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
145 case bitc::BINOP_UREM: return Instruction::URem;
146 case bitc::BINOP_SREM:
147 return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
148 case bitc::BINOP_SHL: return Instruction::Shl;
149 case bitc::BINOP_LSHR: return Instruction::LShr;
150 case bitc::BINOP_ASHR: return Instruction::AShr;
151 case bitc::BINOP_AND: return Instruction::And;
152 case bitc::BINOP_OR: return Instruction::Or;
153 case bitc::BINOP_XOR: return Instruction::Xor;
157 static AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
159 default: return AtomicRMWInst::BAD_BINOP;
160 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
161 case bitc::RMW_ADD: return AtomicRMWInst::Add;
162 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
163 case bitc::RMW_AND: return AtomicRMWInst::And;
164 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
165 case bitc::RMW_OR: return AtomicRMWInst::Or;
166 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
167 case bitc::RMW_MAX: return AtomicRMWInst::Max;
168 case bitc::RMW_MIN: return AtomicRMWInst::Min;
169 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
170 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
174 static AtomicOrdering GetDecodedOrdering(unsigned Val) {
176 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
177 case bitc::ORDERING_UNORDERED: return Unordered;
178 case bitc::ORDERING_MONOTONIC: return Monotonic;
179 case bitc::ORDERING_ACQUIRE: return Acquire;
180 case bitc::ORDERING_RELEASE: return Release;
181 case bitc::ORDERING_ACQREL: return AcquireRelease;
182 default: // Map unknown orderings to sequentially-consistent.
183 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
187 static SynchronizationScope GetDecodedSynchScope(unsigned Val) {
189 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
190 default: // Map unknown scopes to cross-thread.
191 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
197 /// @brief A class for maintaining the slot number definition
198 /// as a placeholder for the actual definition for forward constants defs.
199 class ConstantPlaceHolder : public ConstantExpr {
200 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
202 // allocate space for exactly one operand
203 void *operator new(size_t s) {
204 return User::operator new(s, 1);
206 explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
207 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
208 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
211 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
212 //static inline bool classof(const ConstantPlaceHolder *) { return true; }
213 static bool classof(const Value *V) {
214 return isa<ConstantExpr>(V) &&
215 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
219 /// Provide fast operand accessors
220 //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
224 // FIXME: can we inherit this from ConstantExpr?
226 struct OperandTraits<ConstantPlaceHolder> :
227 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
232 void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
241 WeakVH &OldV = ValuePtrs[Idx];
247 // Handle constants and non-constants (e.g. instrs) differently for
249 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
250 ResolveConstants.push_back(std::make_pair(PHC, Idx));
253 // If there was a forward reference to this value, replace it.
254 Value *PrevVal = OldV;
255 OldV->replaceAllUsesWith(V);
261 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
266 if (Value *V = ValuePtrs[Idx]) {
267 assert(Ty == V->getType() && "Type mismatch in constant table!");
268 return cast<Constant>(V);
271 // Create and return a placeholder, which will later be RAUW'd.
272 Constant *C = new ConstantPlaceHolder(Ty, Context);
277 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
281 if (Value *V = ValuePtrs[Idx]) {
282 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
286 // No type specified, must be invalid reference.
287 if (Ty == 0) return 0;
289 // Create and return a placeholder, which will later be RAUW'd.
290 Value *V = new Argument(Ty);
295 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
296 /// resolves any forward references. The idea behind this is that we sometimes
297 /// get constants (such as large arrays) which reference *many* forward ref
298 /// constants. Replacing each of these causes a lot of thrashing when
299 /// building/reuniquing the constant. Instead of doing this, we look at all the
300 /// uses and rewrite all the place holders at once for any constant that uses
302 void BitcodeReaderValueList::ResolveConstantForwardRefs() {
303 // Sort the values by-pointer so that they are efficient to look up with a
305 std::sort(ResolveConstants.begin(), ResolveConstants.end());
307 SmallVector<Constant*, 64> NewOps;
309 while (!ResolveConstants.empty()) {
310 Value *RealVal = operator[](ResolveConstants.back().second);
311 Constant *Placeholder = ResolveConstants.back().first;
312 ResolveConstants.pop_back();
314 // Loop over all users of the placeholder, updating them to reference the
315 // new value. If they reference more than one placeholder, update them all
317 while (!Placeholder->use_empty()) {
318 Value::use_iterator UI = Placeholder->use_begin();
321 // If the using object isn't uniqued, just update the operands. This
322 // handles instructions and initializers for global variables.
323 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
324 UI.getUse().set(RealVal);
328 // Otherwise, we have a constant that uses the placeholder. Replace that
329 // constant with a new constant that has *all* placeholder uses updated.
330 Constant *UserC = cast<Constant>(U);
331 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
334 if (!isa<ConstantPlaceHolder>(*I)) {
335 // Not a placeholder reference.
337 } else if (*I == Placeholder) {
338 // Common case is that it just references this one placeholder.
341 // Otherwise, look up the placeholder in ResolveConstants.
342 ResolveConstantsTy::iterator It =
343 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
344 std::pair<Constant*, unsigned>(cast<Constant>(*I),
346 assert(It != ResolveConstants.end() && It->first == *I);
347 NewOp = operator[](It->second);
350 NewOps.push_back(cast<Constant>(NewOp));
353 // Make the new constant.
355 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
356 NewC = ConstantArray::get(UserCA->getType(), NewOps);
357 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
358 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
359 } else if (isa<ConstantVector>(UserC)) {
360 NewC = ConstantVector::get(NewOps);
362 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
363 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
366 UserC->replaceAllUsesWith(NewC);
367 UserC->destroyConstant();
371 // Update all ValueHandles, they should be the only users at this point.
372 Placeholder->replaceAllUsesWith(RealVal);
377 void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
386 WeakVH &OldV = MDValuePtrs[Idx];
392 // If there was a forward reference to this value, replace it.
393 MDNode *PrevVal = cast<MDNode>(OldV);
394 OldV->replaceAllUsesWith(V);
395 MDNode::deleteTemporary(PrevVal);
396 // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
398 MDValuePtrs[Idx] = V;
401 Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
405 if (Value *V = MDValuePtrs[Idx]) {
406 assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
410 // Create and return a placeholder, which will later be RAUW'd.
411 Value *V = MDNode::getTemporary(Context, ArrayRef<Value*>());
412 MDValuePtrs[Idx] = V;
416 Type *BitcodeReader::getTypeByID(unsigned ID) {
417 // The type table size is always specified correctly.
418 if (ID >= TypeList.size())
421 if (Type *Ty = TypeList[ID])
424 // If we have a forward reference, the only possible case is when it is to a
425 // named struct. Just create a placeholder for now.
426 return TypeList[ID] = StructType::create(Context);
430 //===----------------------------------------------------------------------===//
431 // Functions for parsing blocks from the bitcode file
432 //===----------------------------------------------------------------------===//
434 bool BitcodeReader::ParseAttributeBlock() {
435 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
436 return Error("Malformed block record");
438 if (!MAttributes.empty())
439 return Error("Multiple PARAMATTR blocks found!");
441 SmallVector<uint64_t, 64> Record;
443 SmallVector<AttributeWithIndex, 8> Attrs;
445 // Read all the records.
447 unsigned Code = Stream.ReadCode();
448 if (Code == bitc::END_BLOCK) {
449 if (Stream.ReadBlockEnd())
450 return Error("Error at end of PARAMATTR block");
454 if (Code == bitc::ENTER_SUBBLOCK) {
455 // No known subblocks, always skip them.
456 Stream.ReadSubBlockID();
457 if (Stream.SkipBlock())
458 return Error("Malformed block record");
462 if (Code == bitc::DEFINE_ABBREV) {
463 Stream.ReadAbbrevRecord();
469 switch (Stream.ReadRecord(Code, Record)) {
470 default: // Default behavior: ignore.
472 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [paramidx0, attr0, ...]
473 if (Record.size() & 1)
474 return Error("Invalid ENTRY record");
476 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
477 Attributes ReconstitutedAttr =
478 Attribute::decodeLLVMAttributesForBitcode(Record[i+1]);
479 Record[i+1] = ReconstitutedAttr.Raw();
482 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
483 if (Attributes(Record[i+1]) != Attribute::None)
484 Attrs.push_back(AttributeWithIndex::get(Record[i],
485 Attributes(Record[i+1])));
488 MAttributes.push_back(AttrListPtr::get(Attrs));
496 bool BitcodeReader::ParseTypeTable() {
497 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
498 return Error("Malformed block record");
500 return ParseTypeTableBody();
503 bool BitcodeReader::ParseTypeTableBody() {
504 if (!TypeList.empty())
505 return Error("Multiple TYPE_BLOCKs found!");
507 SmallVector<uint64_t, 64> Record;
508 unsigned NumRecords = 0;
510 SmallString<64> TypeName;
512 // Read all the records for this type table.
514 unsigned Code = Stream.ReadCode();
515 if (Code == bitc::END_BLOCK) {
516 if (NumRecords != TypeList.size())
517 return Error("Invalid type forward reference in TYPE_BLOCK");
518 if (Stream.ReadBlockEnd())
519 return Error("Error at end of type table block");
523 if (Code == bitc::ENTER_SUBBLOCK) {
524 // No known subblocks, always skip them.
525 Stream.ReadSubBlockID();
526 if (Stream.SkipBlock())
527 return Error("Malformed block record");
531 if (Code == bitc::DEFINE_ABBREV) {
532 Stream.ReadAbbrevRecord();
539 switch (Stream.ReadRecord(Code, Record)) {
540 default: return Error("unknown type in type table");
541 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
542 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
543 // type list. This allows us to reserve space.
544 if (Record.size() < 1)
545 return Error("Invalid TYPE_CODE_NUMENTRY record");
546 TypeList.resize(Record[0]);
548 case bitc::TYPE_CODE_VOID: // VOID
549 ResultTy = Type::getVoidTy(Context);
551 case bitc::TYPE_CODE_HALF: // HALF
552 ResultTy = Type::getHalfTy(Context);
554 case bitc::TYPE_CODE_FLOAT: // FLOAT
555 ResultTy = Type::getFloatTy(Context);
557 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
558 ResultTy = Type::getDoubleTy(Context);
560 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
561 ResultTy = Type::getX86_FP80Ty(Context);
563 case bitc::TYPE_CODE_FP128: // FP128
564 ResultTy = Type::getFP128Ty(Context);
566 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
567 ResultTy = Type::getPPC_FP128Ty(Context);
569 case bitc::TYPE_CODE_LABEL: // LABEL
570 ResultTy = Type::getLabelTy(Context);
572 case bitc::TYPE_CODE_METADATA: // METADATA
573 ResultTy = Type::getMetadataTy(Context);
575 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
576 ResultTy = Type::getX86_MMXTy(Context);
578 case bitc::TYPE_CODE_INTEGER: // INTEGER: [width]
579 if (Record.size() < 1)
580 return Error("Invalid Integer type record");
582 ResultTy = IntegerType::get(Context, Record[0]);
584 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
585 // [pointee type, address space]
586 if (Record.size() < 1)
587 return Error("Invalid POINTER type record");
588 unsigned AddressSpace = 0;
589 if (Record.size() == 2)
590 AddressSpace = Record[1];
591 ResultTy = getTypeByID(Record[0]);
592 if (ResultTy == 0) return Error("invalid element type in pointer type");
593 ResultTy = PointerType::get(ResultTy, AddressSpace);
596 case bitc::TYPE_CODE_FUNCTION_OLD: {
597 // FIXME: attrid is dead, remove it in LLVM 4.0
598 // FUNCTION: [vararg, attrid, retty, paramty x N]
599 if (Record.size() < 3)
600 return Error("Invalid FUNCTION type record");
601 SmallVector<Type*, 8> ArgTys;
602 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
603 if (Type *T = getTypeByID(Record[i]))
609 ResultTy = getTypeByID(Record[2]);
610 if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
611 return Error("invalid type in function type");
613 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
616 case bitc::TYPE_CODE_FUNCTION: {
617 // FUNCTION: [vararg, retty, paramty x N]
618 if (Record.size() < 2)
619 return Error("Invalid FUNCTION type record");
620 SmallVector<Type*, 8> ArgTys;
621 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
622 if (Type *T = getTypeByID(Record[i]))
628 ResultTy = getTypeByID(Record[1]);
629 if (ResultTy == 0 || ArgTys.size() < Record.size()-2)
630 return Error("invalid type in function type");
632 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
635 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
636 if (Record.size() < 1)
637 return Error("Invalid STRUCT type record");
638 SmallVector<Type*, 8> EltTys;
639 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
640 if (Type *T = getTypeByID(Record[i]))
645 if (EltTys.size() != Record.size()-1)
646 return Error("invalid type in struct type");
647 ResultTy = StructType::get(Context, EltTys, Record[0]);
650 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
651 if (ConvertToString(Record, 0, TypeName))
652 return Error("Invalid STRUCT_NAME record");
655 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
656 if (Record.size() < 1)
657 return Error("Invalid STRUCT type record");
659 if (NumRecords >= TypeList.size())
660 return Error("invalid TYPE table");
662 // Check to see if this was forward referenced, if so fill in the temp.
663 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
665 Res->setName(TypeName);
666 TypeList[NumRecords] = 0;
667 } else // Otherwise, create a new struct.
668 Res = StructType::create(Context, TypeName);
671 SmallVector<Type*, 8> EltTys;
672 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
673 if (Type *T = getTypeByID(Record[i]))
678 if (EltTys.size() != Record.size()-1)
679 return Error("invalid STRUCT type record");
680 Res->setBody(EltTys, Record[0]);
684 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
685 if (Record.size() != 1)
686 return Error("Invalid OPAQUE type record");
688 if (NumRecords >= TypeList.size())
689 return Error("invalid TYPE table");
691 // Check to see if this was forward referenced, if so fill in the temp.
692 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
694 Res->setName(TypeName);
695 TypeList[NumRecords] = 0;
696 } else // Otherwise, create a new struct with no body.
697 Res = StructType::create(Context, TypeName);
702 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
703 if (Record.size() < 2)
704 return Error("Invalid ARRAY type record");
705 if ((ResultTy = getTypeByID(Record[1])))
706 ResultTy = ArrayType::get(ResultTy, Record[0]);
708 return Error("Invalid ARRAY type element");
710 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
711 if (Record.size() < 2)
712 return Error("Invalid VECTOR type record");
713 if ((ResultTy = getTypeByID(Record[1])))
714 ResultTy = VectorType::get(ResultTy, Record[0]);
716 return Error("Invalid ARRAY type element");
720 if (NumRecords >= TypeList.size())
721 return Error("invalid TYPE table");
722 assert(ResultTy && "Didn't read a type?");
723 assert(TypeList[NumRecords] == 0 && "Already read type?");
724 TypeList[NumRecords++] = ResultTy;
728 bool BitcodeReader::ParseValueSymbolTable() {
729 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
730 return Error("Malformed block record");
732 SmallVector<uint64_t, 64> Record;
734 // Read all the records for this value table.
735 SmallString<128> ValueName;
737 unsigned Code = Stream.ReadCode();
738 if (Code == bitc::END_BLOCK) {
739 if (Stream.ReadBlockEnd())
740 return Error("Error at end of value symbol table block");
743 if (Code == bitc::ENTER_SUBBLOCK) {
744 // No known subblocks, always skip them.
745 Stream.ReadSubBlockID();
746 if (Stream.SkipBlock())
747 return Error("Malformed block record");
751 if (Code == bitc::DEFINE_ABBREV) {
752 Stream.ReadAbbrevRecord();
758 switch (Stream.ReadRecord(Code, Record)) {
759 default: // Default behavior: unknown type.
761 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
762 if (ConvertToString(Record, 1, ValueName))
763 return Error("Invalid VST_ENTRY record");
764 unsigned ValueID = Record[0];
765 if (ValueID >= ValueList.size())
766 return Error("Invalid Value ID in VST_ENTRY record");
767 Value *V = ValueList[ValueID];
769 V->setName(StringRef(ValueName.data(), ValueName.size()));
773 case bitc::VST_CODE_BBENTRY: {
774 if (ConvertToString(Record, 1, ValueName))
775 return Error("Invalid VST_BBENTRY record");
776 BasicBlock *BB = getBasicBlock(Record[0]);
778 return Error("Invalid BB ID in VST_BBENTRY record");
780 BB->setName(StringRef(ValueName.data(), ValueName.size()));
788 bool BitcodeReader::ParseMetadata() {
789 unsigned NextMDValueNo = MDValueList.size();
791 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
792 return Error("Malformed block record");
794 SmallVector<uint64_t, 64> Record;
796 // Read all the records.
798 unsigned Code = Stream.ReadCode();
799 if (Code == bitc::END_BLOCK) {
800 if (Stream.ReadBlockEnd())
801 return Error("Error at end of PARAMATTR block");
805 if (Code == bitc::ENTER_SUBBLOCK) {
806 // No known subblocks, always skip them.
807 Stream.ReadSubBlockID();
808 if (Stream.SkipBlock())
809 return Error("Malformed block record");
813 if (Code == bitc::DEFINE_ABBREV) {
814 Stream.ReadAbbrevRecord();
818 bool IsFunctionLocal = false;
821 Code = Stream.ReadRecord(Code, Record);
823 default: // Default behavior: ignore.
825 case bitc::METADATA_NAME: {
826 // Read named of the named metadata.
827 SmallString<8> Name(Record.begin(), Record.end());
829 Code = Stream.ReadCode();
831 // METADATA_NAME is always followed by METADATA_NAMED_NODE.
832 unsigned NextBitCode = Stream.ReadRecord(Code, Record);
833 assert(NextBitCode == bitc::METADATA_NAMED_NODE); (void)NextBitCode;
835 // Read named metadata elements.
836 unsigned Size = Record.size();
837 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
838 for (unsigned i = 0; i != Size; ++i) {
839 MDNode *MD = dyn_cast<MDNode>(MDValueList.getValueFwdRef(Record[i]));
841 return Error("Malformed metadata record");
846 case bitc::METADATA_FN_NODE:
847 IsFunctionLocal = true;
849 case bitc::METADATA_NODE: {
850 if (Record.size() % 2 == 1)
851 return Error("Invalid METADATA_NODE record");
853 unsigned Size = Record.size();
854 SmallVector<Value*, 8> Elts;
855 for (unsigned i = 0; i != Size; i += 2) {
856 Type *Ty = getTypeByID(Record[i]);
857 if (!Ty) return Error("Invalid METADATA_NODE record");
858 if (Ty->isMetadataTy())
859 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
860 else if (!Ty->isVoidTy())
861 Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
863 Elts.push_back(NULL);
865 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
866 IsFunctionLocal = false;
867 MDValueList.AssignValue(V, NextMDValueNo++);
870 case bitc::METADATA_STRING: {
871 SmallString<8> String(Record.begin(), Record.end());
872 Value *V = MDString::get(Context, String);
873 MDValueList.AssignValue(V, NextMDValueNo++);
876 case bitc::METADATA_KIND: {
877 if (Record.size() < 2)
878 return Error("Invalid METADATA_KIND record");
880 unsigned Kind = Record[0];
881 SmallString<8> Name(Record.begin()+1, Record.end());
883 unsigned NewKind = TheModule->getMDKindID(Name.str());
884 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
885 return Error("Conflicting METADATA_KIND records");
892 /// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
893 /// the LSB for dense VBR encoding.
894 static uint64_t DecodeSignRotatedValue(uint64_t V) {
899 // There is no such thing as -0 with integers. "-0" really means MININT.
903 /// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
904 /// values and aliases that we can.
905 bool BitcodeReader::ResolveGlobalAndAliasInits() {
906 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
907 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
909 GlobalInitWorklist.swap(GlobalInits);
910 AliasInitWorklist.swap(AliasInits);
912 while (!GlobalInitWorklist.empty()) {
913 unsigned ValID = GlobalInitWorklist.back().second;
914 if (ValID >= ValueList.size()) {
915 // Not ready to resolve this yet, it requires something later in the file.
916 GlobalInits.push_back(GlobalInitWorklist.back());
918 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
919 GlobalInitWorklist.back().first->setInitializer(C);
921 return Error("Global variable initializer is not a constant!");
923 GlobalInitWorklist.pop_back();
926 while (!AliasInitWorklist.empty()) {
927 unsigned ValID = AliasInitWorklist.back().second;
928 if (ValID >= ValueList.size()) {
929 AliasInits.push_back(AliasInitWorklist.back());
931 if (Constant *C = dyn_cast<Constant>(ValueList[ValID]))
932 AliasInitWorklist.back().first->setAliasee(C);
934 return Error("Alias initializer is not a constant!");
936 AliasInitWorklist.pop_back();
941 static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
942 SmallVector<uint64_t, 8> Words(Vals.size());
943 std::transform(Vals.begin(), Vals.end(), Words.begin(),
944 DecodeSignRotatedValue);
946 return APInt(TypeBits, Words);
949 bool BitcodeReader::ParseConstants() {
950 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
951 return Error("Malformed block record");
953 SmallVector<uint64_t, 64> Record;
955 // Read all the records for this value table.
956 Type *CurTy = Type::getInt32Ty(Context);
957 unsigned NextCstNo = ValueList.size();
959 unsigned Code = Stream.ReadCode();
960 if (Code == bitc::END_BLOCK)
963 if (Code == bitc::ENTER_SUBBLOCK) {
964 // No known subblocks, always skip them.
965 Stream.ReadSubBlockID();
966 if (Stream.SkipBlock())
967 return Error("Malformed block record");
971 if (Code == bitc::DEFINE_ABBREV) {
972 Stream.ReadAbbrevRecord();
979 unsigned BitCode = Stream.ReadRecord(Code, Record);
981 default: // Default behavior: unknown constant
982 case bitc::CST_CODE_UNDEF: // UNDEF
983 V = UndefValue::get(CurTy);
985 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
987 return Error("Malformed CST_SETTYPE record");
988 if (Record[0] >= TypeList.size())
989 return Error("Invalid Type ID in CST_SETTYPE record");
990 CurTy = TypeList[Record[0]];
991 continue; // Skip the ValueList manipulation.
992 case bitc::CST_CODE_NULL: // NULL
993 V = Constant::getNullValue(CurTy);
995 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
996 if (!CurTy->isIntegerTy() || Record.empty())
997 return Error("Invalid CST_INTEGER record");
998 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
1000 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
1001 if (!CurTy->isIntegerTy() || Record.empty())
1002 return Error("Invalid WIDE_INTEGER record");
1004 APInt VInt = ReadWideAPInt(Record,
1005 cast<IntegerType>(CurTy)->getBitWidth());
1006 V = ConstantInt::get(Context, VInt);
1010 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
1012 return Error("Invalid FLOAT record");
1013 if (CurTy->isHalfTy())
1014 V = ConstantFP::get(Context, APFloat(APInt(16, (uint16_t)Record[0])));
1015 else if (CurTy->isFloatTy())
1016 V = ConstantFP::get(Context, APFloat(APInt(32, (uint32_t)Record[0])));
1017 else if (CurTy->isDoubleTy())
1018 V = ConstantFP::get(Context, APFloat(APInt(64, Record[0])));
1019 else if (CurTy->isX86_FP80Ty()) {
1020 // Bits are not stored the same way as a normal i80 APInt, compensate.
1021 uint64_t Rearrange[2];
1022 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1023 Rearrange[1] = Record[0] >> 48;
1024 V = ConstantFP::get(Context, APFloat(APInt(80, Rearrange)));
1025 } else if (CurTy->isFP128Ty())
1026 V = ConstantFP::get(Context, APFloat(APInt(128, Record), true));
1027 else if (CurTy->isPPC_FP128Ty())
1028 V = ConstantFP::get(Context, APFloat(APInt(128, Record)));
1030 V = UndefValue::get(CurTy);
1034 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1036 return Error("Invalid CST_AGGREGATE record");
1038 unsigned Size = Record.size();
1039 SmallVector<Constant*, 16> Elts;
1041 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
1042 for (unsigned i = 0; i != Size; ++i)
1043 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
1044 STy->getElementType(i)));
1045 V = ConstantStruct::get(STy, Elts);
1046 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1047 Type *EltTy = ATy->getElementType();
1048 for (unsigned i = 0; i != Size; ++i)
1049 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1050 V = ConstantArray::get(ATy, Elts);
1051 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1052 Type *EltTy = VTy->getElementType();
1053 for (unsigned i = 0; i != Size; ++i)
1054 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1055 V = ConstantVector::get(Elts);
1057 V = UndefValue::get(CurTy);
1061 case bitc::CST_CODE_STRING: // STRING: [values]
1062 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1064 return Error("Invalid CST_STRING record");
1066 SmallString<16> Elts(Record.begin(), Record.end());
1067 V = ConstantDataArray::getString(Context, Elts,
1068 BitCode == bitc::CST_CODE_CSTRING);
1071 case bitc::CST_CODE_DATA: {// DATA: [n x value]
1073 return Error("Invalid CST_DATA record");
1075 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
1076 unsigned Size = Record.size();
1078 if (EltTy->isIntegerTy(8)) {
1079 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
1080 if (isa<VectorType>(CurTy))
1081 V = ConstantDataVector::get(Context, Elts);
1083 V = ConstantDataArray::get(Context, Elts);
1084 } else if (EltTy->isIntegerTy(16)) {
1085 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
1086 if (isa<VectorType>(CurTy))
1087 V = ConstantDataVector::get(Context, Elts);
1089 V = ConstantDataArray::get(Context, Elts);
1090 } else if (EltTy->isIntegerTy(32)) {
1091 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
1092 if (isa<VectorType>(CurTy))
1093 V = ConstantDataVector::get(Context, Elts);
1095 V = ConstantDataArray::get(Context, Elts);
1096 } else if (EltTy->isIntegerTy(64)) {
1097 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
1098 if (isa<VectorType>(CurTy))
1099 V = ConstantDataVector::get(Context, Elts);
1101 V = ConstantDataArray::get(Context, Elts);
1102 } else if (EltTy->isFloatTy()) {
1103 SmallVector<float, 16> Elts(Size);
1104 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
1105 if (isa<VectorType>(CurTy))
1106 V = ConstantDataVector::get(Context, Elts);
1108 V = ConstantDataArray::get(Context, Elts);
1109 } else if (EltTy->isDoubleTy()) {
1110 SmallVector<double, 16> Elts(Size);
1111 std::transform(Record.begin(), Record.end(), Elts.begin(),
1113 if (isa<VectorType>(CurTy))
1114 V = ConstantDataVector::get(Context, Elts);
1116 V = ConstantDataArray::get(Context, Elts);
1118 return Error("Unknown element type in CE_DATA");
1123 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
1124 if (Record.size() < 3) return Error("Invalid CE_BINOP record");
1125 int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
1127 V = UndefValue::get(CurTy); // Unknown binop.
1129 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1130 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
1132 if (Record.size() >= 4) {
1133 if (Opc == Instruction::Add ||
1134 Opc == Instruction::Sub ||
1135 Opc == Instruction::Mul ||
1136 Opc == Instruction::Shl) {
1137 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1138 Flags |= OverflowingBinaryOperator::NoSignedWrap;
1139 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1140 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
1141 } else if (Opc == Instruction::SDiv ||
1142 Opc == Instruction::UDiv ||
1143 Opc == Instruction::LShr ||
1144 Opc == Instruction::AShr) {
1145 if (Record[3] & (1 << bitc::PEO_EXACT))
1146 Flags |= SDivOperator::IsExact;
1149 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
1153 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
1154 if (Record.size() < 3) return Error("Invalid CE_CAST record");
1155 int Opc = GetDecodedCastOpcode(Record[0]);
1157 V = UndefValue::get(CurTy); // Unknown cast.
1159 Type *OpTy = getTypeByID(Record[1]);
1160 if (!OpTy) return Error("Invalid CE_CAST record");
1161 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
1162 V = ConstantExpr::getCast(Opc, Op, CurTy);
1166 case bitc::CST_CODE_CE_INBOUNDS_GEP:
1167 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
1168 if (Record.size() & 1) return Error("Invalid CE_GEP record");
1169 SmallVector<Constant*, 16> Elts;
1170 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1171 Type *ElTy = getTypeByID(Record[i]);
1172 if (!ElTy) return Error("Invalid CE_GEP record");
1173 Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1175 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
1176 V = ConstantExpr::getGetElementPtr(Elts[0], Indices,
1178 bitc::CST_CODE_CE_INBOUNDS_GEP);
1181 case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]
1182 if (Record.size() < 3) return Error("Invalid CE_SELECT record");
1183 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1184 Type::getInt1Ty(Context)),
1185 ValueList.getConstantFwdRef(Record[1],CurTy),
1186 ValueList.getConstantFwdRef(Record[2],CurTy));
1188 case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
1189 if (Record.size() < 3) return Error("Invalid CE_EXTRACTELT record");
1191 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
1192 if (OpTy == 0) return Error("Invalid CE_EXTRACTELT record");
1193 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1194 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1195 V = ConstantExpr::getExtractElement(Op0, Op1);
1198 case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
1199 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
1200 if (Record.size() < 3 || OpTy == 0)
1201 return Error("Invalid CE_INSERTELT record");
1202 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1203 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1204 OpTy->getElementType());
1205 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1206 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
1209 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
1210 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
1211 if (Record.size() < 3 || OpTy == 0)
1212 return Error("Invalid CE_SHUFFLEVEC record");
1213 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1214 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
1215 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
1216 OpTy->getNumElements());
1217 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
1218 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
1221 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
1222 VectorType *RTy = dyn_cast<VectorType>(CurTy);
1224 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
1225 if (Record.size() < 4 || RTy == 0 || OpTy == 0)
1226 return Error("Invalid CE_SHUFVEC_EX record");
1227 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1228 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1229 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
1230 RTy->getNumElements());
1231 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
1232 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
1235 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
1236 if (Record.size() < 4) return Error("Invalid CE_CMP record");
1237 Type *OpTy = getTypeByID(Record[0]);
1238 if (OpTy == 0) return Error("Invalid CE_CMP record");
1239 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1240 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1242 if (OpTy->isFPOrFPVectorTy())
1243 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
1245 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
1248 case bitc::CST_CODE_INLINEASM: {
1249 if (Record.size() < 2) return Error("Invalid INLINEASM record");
1250 std::string AsmStr, ConstrStr;
1251 bool HasSideEffects = Record[0] & 1;
1252 bool IsAlignStack = Record[0] >> 1;
1253 unsigned AsmStrSize = Record[1];
1254 if (2+AsmStrSize >= Record.size())
1255 return Error("Invalid INLINEASM record");
1256 unsigned ConstStrSize = Record[2+AsmStrSize];
1257 if (3+AsmStrSize+ConstStrSize > Record.size())
1258 return Error("Invalid INLINEASM record");
1260 for (unsigned i = 0; i != AsmStrSize; ++i)
1261 AsmStr += (char)Record[2+i];
1262 for (unsigned i = 0; i != ConstStrSize; ++i)
1263 ConstrStr += (char)Record[3+AsmStrSize+i];
1264 PointerType *PTy = cast<PointerType>(CurTy);
1265 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1266 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
1269 case bitc::CST_CODE_BLOCKADDRESS:{
1270 if (Record.size() < 3) return Error("Invalid CE_BLOCKADDRESS record");
1271 Type *FnTy = getTypeByID(Record[0]);
1272 if (FnTy == 0) return Error("Invalid CE_BLOCKADDRESS record");
1274 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
1275 if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record");
1277 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1278 Type::getInt8Ty(Context),
1279 false, GlobalValue::InternalLinkage,
1281 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1287 ValueList.AssignValue(V, NextCstNo);
1291 if (NextCstNo != ValueList.size())
1292 return Error("Invalid constant reference!");
1294 if (Stream.ReadBlockEnd())
1295 return Error("Error at end of constants block");
1297 // Once all the constants have been read, go through and resolve forward
1299 ValueList.ResolveConstantForwardRefs();
1303 bool BitcodeReader::ParseUseLists() {
1304 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
1305 return Error("Malformed block record");
1307 SmallVector<uint64_t, 64> Record;
1309 // Read all the records.
1311 unsigned Code = Stream.ReadCode();
1312 if (Code == bitc::END_BLOCK) {
1313 if (Stream.ReadBlockEnd())
1314 return Error("Error at end of use-list table block");
1318 if (Code == bitc::ENTER_SUBBLOCK) {
1319 // No known subblocks, always skip them.
1320 Stream.ReadSubBlockID();
1321 if (Stream.SkipBlock())
1322 return Error("Malformed block record");
1326 if (Code == bitc::DEFINE_ABBREV) {
1327 Stream.ReadAbbrevRecord();
1331 // Read a use list record.
1333 switch (Stream.ReadRecord(Code, Record)) {
1334 default: // Default behavior: unknown type.
1336 case bitc::USELIST_CODE_ENTRY: { // USELIST_CODE_ENTRY: TBD.
1337 unsigned RecordLength = Record.size();
1338 if (RecordLength < 1)
1339 return Error ("Invalid UseList reader!");
1340 UseListRecords.push_back(Record);
1347 /// RememberAndSkipFunctionBody - When we see the block for a function body,
1348 /// remember where it is and then skip it. This lets us lazily deserialize the
1350 bool BitcodeReader::RememberAndSkipFunctionBody() {
1351 // Get the function we are talking about.
1352 if (FunctionsWithBodies.empty())
1353 return Error("Insufficient function protos");
1355 Function *Fn = FunctionsWithBodies.back();
1356 FunctionsWithBodies.pop_back();
1358 // Save the current stream state.
1359 uint64_t CurBit = Stream.GetCurrentBitNo();
1360 DeferredFunctionInfo[Fn] = CurBit;
1362 // Skip over the function block for now.
1363 if (Stream.SkipBlock())
1364 return Error("Malformed block record");
1368 bool BitcodeReader::GlobalCleanup() {
1369 // Patch the initializers for globals and aliases up.
1370 ResolveGlobalAndAliasInits();
1371 if (!GlobalInits.empty() || !AliasInits.empty())
1372 return Error("Malformed global initializer set");
1374 // Look for intrinsic functions which need to be upgraded at some point
1375 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1378 if (UpgradeIntrinsicFunction(FI, NewFn))
1379 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1382 // Look for global variables which need to be renamed.
1383 for (Module::global_iterator
1384 GI = TheModule->global_begin(), GE = TheModule->global_end();
1386 UpgradeGlobalVariable(GI);
1387 // Force deallocation of memory for these vectors to favor the client that
1388 // want lazy deserialization.
1389 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1390 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
1394 bool BitcodeReader::ParseModule(bool Resume) {
1396 Stream.JumpToBit(NextUnreadBit);
1397 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1398 return Error("Malformed block record");
1400 SmallVector<uint64_t, 64> Record;
1401 std::vector<std::string> SectionTable;
1402 std::vector<std::string> GCTable;
1404 // Read all the records for this module.
1405 while (!Stream.AtEndOfStream()) {
1406 unsigned Code = Stream.ReadCode();
1407 if (Code == bitc::END_BLOCK) {
1408 if (Stream.ReadBlockEnd())
1409 return Error("Error at end of module block");
1411 return GlobalCleanup();
1414 if (Code == bitc::ENTER_SUBBLOCK) {
1415 switch (Stream.ReadSubBlockID()) {
1416 default: // Skip unknown content.
1417 if (Stream.SkipBlock())
1418 return Error("Malformed block record");
1420 case bitc::BLOCKINFO_BLOCK_ID:
1421 if (Stream.ReadBlockInfoBlock())
1422 return Error("Malformed BlockInfoBlock");
1424 case bitc::PARAMATTR_BLOCK_ID:
1425 if (ParseAttributeBlock())
1428 case bitc::TYPE_BLOCK_ID_NEW:
1429 if (ParseTypeTable())
1432 case bitc::VALUE_SYMTAB_BLOCK_ID:
1433 if (ParseValueSymbolTable())
1435 SeenValueSymbolTable = true;
1437 case bitc::CONSTANTS_BLOCK_ID:
1438 if (ParseConstants() || ResolveGlobalAndAliasInits())
1441 case bitc::METADATA_BLOCK_ID:
1442 if (ParseMetadata())
1445 case bitc::FUNCTION_BLOCK_ID:
1446 // If this is the first function body we've seen, reverse the
1447 // FunctionsWithBodies list.
1448 if (!SeenFirstFunctionBody) {
1449 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1450 if (GlobalCleanup())
1452 SeenFirstFunctionBody = true;
1455 if (RememberAndSkipFunctionBody())
1457 // For streaming bitcode, suspend parsing when we reach the function
1458 // bodies. Subsequent materialization calls will resume it when
1459 // necessary. For streaming, the function bodies must be at the end of
1460 // the bitcode. If the bitcode file is old, the symbol table will be
1461 // at the end instead and will not have been seen yet. In this case,
1462 // just finish the parse now.
1463 if (LazyStreamer && SeenValueSymbolTable) {
1464 NextUnreadBit = Stream.GetCurrentBitNo();
1468 case bitc::USELIST_BLOCK_ID:
1469 if (ParseUseLists())
1476 if (Code == bitc::DEFINE_ABBREV) {
1477 Stream.ReadAbbrevRecord();
1482 switch (Stream.ReadRecord(Code, Record)) {
1483 default: break; // Default behavior, ignore unknown content.
1484 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
1485 if (Record.size() < 1)
1486 return Error("Malformed MODULE_CODE_VERSION");
1487 // Only version #0 is supported so far.
1489 return Error("Unknown bitstream version!");
1491 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
1493 if (ConvertToString(Record, 0, S))
1494 return Error("Invalid MODULE_CODE_TRIPLE record");
1495 TheModule->setTargetTriple(S);
1498 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
1500 if (ConvertToString(Record, 0, S))
1501 return Error("Invalid MODULE_CODE_DATALAYOUT record");
1502 TheModule->setDataLayout(S);
1505 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
1507 if (ConvertToString(Record, 0, S))
1508 return Error("Invalid MODULE_CODE_ASM record");
1509 TheModule->setModuleInlineAsm(S);
1512 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
1514 if (ConvertToString(Record, 0, S))
1515 return Error("Invalid MODULE_CODE_DEPLIB record");
1516 TheModule->addLibrary(S);
1519 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
1521 if (ConvertToString(Record, 0, S))
1522 return Error("Invalid MODULE_CODE_SECTIONNAME record");
1523 SectionTable.push_back(S);
1526 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
1528 if (ConvertToString(Record, 0, S))
1529 return Error("Invalid MODULE_CODE_GCNAME record");
1530 GCTable.push_back(S);
1533 // GLOBALVAR: [pointer type, isconst, initid,
1534 // linkage, alignment, section, visibility, threadlocal,
1536 case bitc::MODULE_CODE_GLOBALVAR: {
1537 if (Record.size() < 6)
1538 return Error("Invalid MODULE_CODE_GLOBALVAR record");
1539 Type *Ty = getTypeByID(Record[0]);
1540 if (!Ty) return Error("Invalid MODULE_CODE_GLOBALVAR record");
1541 if (!Ty->isPointerTy())
1542 return Error("Global not a pointer type!");
1543 unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
1544 Ty = cast<PointerType>(Ty)->getElementType();
1546 bool isConstant = Record[1];
1547 GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]);
1548 unsigned Alignment = (1 << Record[4]) >> 1;
1549 std::string Section;
1551 if (Record[5]-1 >= SectionTable.size())
1552 return Error("Invalid section ID");
1553 Section = SectionTable[Record[5]-1];
1555 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
1556 if (Record.size() > 6)
1557 Visibility = GetDecodedVisibility(Record[6]);
1559 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
1560 if (Record.size() > 7)
1561 TLM = GetDecodedThreadLocalMode(Record[7]);
1563 bool UnnamedAddr = false;
1564 if (Record.size() > 8)
1565 UnnamedAddr = Record[8];
1567 GlobalVariable *NewGV =
1568 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
1570 NewGV->setAlignment(Alignment);
1571 if (!Section.empty())
1572 NewGV->setSection(Section);
1573 NewGV->setVisibility(Visibility);
1574 NewGV->setUnnamedAddr(UnnamedAddr);
1576 ValueList.push_back(NewGV);
1578 // Remember which value to use for the global initializer.
1579 if (unsigned InitID = Record[2])
1580 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
1583 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
1584 // alignment, section, visibility, gc, unnamed_addr]
1585 case bitc::MODULE_CODE_FUNCTION: {
1586 if (Record.size() < 8)
1587 return Error("Invalid MODULE_CODE_FUNCTION record");
1588 Type *Ty = getTypeByID(Record[0]);
1589 if (!Ty) return Error("Invalid MODULE_CODE_FUNCTION record");
1590 if (!Ty->isPointerTy())
1591 return Error("Function not a pointer type!");
1593 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1595 return Error("Function not a pointer to function type!");
1597 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1600 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
1601 bool isProto = Record[2];
1602 Func->setLinkage(GetDecodedLinkage(Record[3]));
1603 Func->setAttributes(getAttributes(Record[4]));
1605 Func->setAlignment((1 << Record[5]) >> 1);
1607 if (Record[6]-1 >= SectionTable.size())
1608 return Error("Invalid section ID");
1609 Func->setSection(SectionTable[Record[6]-1]);
1611 Func->setVisibility(GetDecodedVisibility(Record[7]));
1612 if (Record.size() > 8 && Record[8]) {
1613 if (Record[8]-1 > GCTable.size())
1614 return Error("Invalid GC ID");
1615 Func->setGC(GCTable[Record[8]-1].c_str());
1617 bool UnnamedAddr = false;
1618 if (Record.size() > 9)
1619 UnnamedAddr = Record[9];
1620 Func->setUnnamedAddr(UnnamedAddr);
1621 ValueList.push_back(Func);
1623 // If this is a function with a body, remember the prototype we are
1624 // creating now, so that we can match up the body with them later.
1626 FunctionsWithBodies.push_back(Func);
1627 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1631 // ALIAS: [alias type, aliasee val#, linkage]
1632 // ALIAS: [alias type, aliasee val#, linkage, visibility]
1633 case bitc::MODULE_CODE_ALIAS: {
1634 if (Record.size() < 3)
1635 return Error("Invalid MODULE_ALIAS record");
1636 Type *Ty = getTypeByID(Record[0]);
1637 if (!Ty) return Error("Invalid MODULE_ALIAS record");
1638 if (!Ty->isPointerTy())
1639 return Error("Function not a pointer type!");
1641 GlobalAlias *NewGA = new GlobalAlias(Ty, GetDecodedLinkage(Record[2]),
1643 // Old bitcode files didn't have visibility field.
1644 if (Record.size() > 3)
1645 NewGA->setVisibility(GetDecodedVisibility(Record[3]));
1646 ValueList.push_back(NewGA);
1647 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
1650 /// MODULE_CODE_PURGEVALS: [numvals]
1651 case bitc::MODULE_CODE_PURGEVALS:
1652 // Trim down the value list to the specified size.
1653 if (Record.size() < 1 || Record[0] > ValueList.size())
1654 return Error("Invalid MODULE_PURGEVALS record");
1655 ValueList.shrinkTo(Record[0]);
1661 return Error("Premature end of bitstream");
1664 bool BitcodeReader::ParseBitcodeInto(Module *M) {
1667 if (InitStream()) return true;
1669 // Sniff for the signature.
1670 if (Stream.Read(8) != 'B' ||
1671 Stream.Read(8) != 'C' ||
1672 Stream.Read(4) != 0x0 ||
1673 Stream.Read(4) != 0xC ||
1674 Stream.Read(4) != 0xE ||
1675 Stream.Read(4) != 0xD)
1676 return Error("Invalid bitcode signature");
1678 // We expect a number of well-defined blocks, though we don't necessarily
1679 // need to understand them all.
1680 while (!Stream.AtEndOfStream()) {
1681 unsigned Code = Stream.ReadCode();
1683 if (Code != bitc::ENTER_SUBBLOCK) {
1685 // The ranlib in xcode 4 will align archive members by appending newlines
1686 // to the end of them. If this file size is a multiple of 4 but not 8, we
1687 // have to read and ignore these final 4 bytes :-(
1688 if (Stream.GetAbbrevIDWidth() == 2 && Code == 2 &&
1689 Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
1690 Stream.AtEndOfStream())
1693 return Error("Invalid record at top-level");
1696 unsigned BlockID = Stream.ReadSubBlockID();
1698 // We only know the MODULE subblock ID.
1700 case bitc::BLOCKINFO_BLOCK_ID:
1701 if (Stream.ReadBlockInfoBlock())
1702 return Error("Malformed BlockInfoBlock");
1704 case bitc::MODULE_BLOCK_ID:
1705 // Reject multiple MODULE_BLOCK's in a single bitstream.
1707 return Error("Multiple MODULE_BLOCKs in same stream");
1709 if (ParseModule(false))
1711 if (LazyStreamer) return false;
1714 if (Stream.SkipBlock())
1715 return Error("Malformed block record");
1723 bool BitcodeReader::ParseModuleTriple(std::string &Triple) {
1724 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1725 return Error("Malformed block record");
1727 SmallVector<uint64_t, 64> Record;
1729 // Read all the records for this module.
1730 while (!Stream.AtEndOfStream()) {
1731 unsigned Code = Stream.ReadCode();
1732 if (Code == bitc::END_BLOCK) {
1733 if (Stream.ReadBlockEnd())
1734 return Error("Error at end of module block");
1739 if (Code == bitc::ENTER_SUBBLOCK) {
1740 switch (Stream.ReadSubBlockID()) {
1741 default: // Skip unknown content.
1742 if (Stream.SkipBlock())
1743 return Error("Malformed block record");
1749 if (Code == bitc::DEFINE_ABBREV) {
1750 Stream.ReadAbbrevRecord();
1755 switch (Stream.ReadRecord(Code, Record)) {
1756 default: break; // Default behavior, ignore unknown content.
1757 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
1758 if (Record.size() < 1)
1759 return Error("Malformed MODULE_CODE_VERSION");
1760 // Only version #0 is supported so far.
1762 return Error("Unknown bitstream version!");
1764 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
1766 if (ConvertToString(Record, 0, S))
1767 return Error("Invalid MODULE_CODE_TRIPLE record");
1775 return Error("Premature end of bitstream");
1778 bool BitcodeReader::ParseTriple(std::string &Triple) {
1779 if (InitStream()) return true;
1781 // Sniff for the signature.
1782 if (Stream.Read(8) != 'B' ||
1783 Stream.Read(8) != 'C' ||
1784 Stream.Read(4) != 0x0 ||
1785 Stream.Read(4) != 0xC ||
1786 Stream.Read(4) != 0xE ||
1787 Stream.Read(4) != 0xD)
1788 return Error("Invalid bitcode signature");
1790 // We expect a number of well-defined blocks, though we don't necessarily
1791 // need to understand them all.
1792 while (!Stream.AtEndOfStream()) {
1793 unsigned Code = Stream.ReadCode();
1795 if (Code != bitc::ENTER_SUBBLOCK)
1796 return Error("Invalid record at top-level");
1798 unsigned BlockID = Stream.ReadSubBlockID();
1800 // We only know the MODULE subblock ID.
1802 case bitc::MODULE_BLOCK_ID:
1803 if (ParseModuleTriple(Triple))
1807 if (Stream.SkipBlock())
1808 return Error("Malformed block record");
1816 /// ParseMetadataAttachment - Parse metadata attachments.
1817 bool BitcodeReader::ParseMetadataAttachment() {
1818 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1819 return Error("Malformed block record");
1821 SmallVector<uint64_t, 64> Record;
1823 unsigned Code = Stream.ReadCode();
1824 if (Code == bitc::END_BLOCK) {
1825 if (Stream.ReadBlockEnd())
1826 return Error("Error at end of PARAMATTR block");
1829 if (Code == bitc::DEFINE_ABBREV) {
1830 Stream.ReadAbbrevRecord();
1833 // Read a metadata attachment record.
1835 switch (Stream.ReadRecord(Code, Record)) {
1836 default: // Default behavior: ignore.
1838 case bitc::METADATA_ATTACHMENT: {
1839 unsigned RecordLength = Record.size();
1840 if (Record.empty() || (RecordLength - 1) % 2 == 1)
1841 return Error ("Invalid METADATA_ATTACHMENT reader!");
1842 Instruction *Inst = InstructionList[Record[0]];
1843 for (unsigned i = 1; i != RecordLength; i = i+2) {
1844 unsigned Kind = Record[i];
1845 DenseMap<unsigned, unsigned>::iterator I =
1846 MDKindMap.find(Kind);
1847 if (I == MDKindMap.end())
1848 return Error("Invalid metadata kind ID");
1849 Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
1850 Inst->setMetadata(I->second, cast<MDNode>(Node));
1859 /// ParseFunctionBody - Lazily parse the specified function body block.
1860 bool BitcodeReader::ParseFunctionBody(Function *F) {
1861 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
1862 return Error("Malformed block record");
1864 InstructionList.clear();
1865 unsigned ModuleValueListSize = ValueList.size();
1866 unsigned ModuleMDValueListSize = MDValueList.size();
1868 // Add all the function arguments to the value table.
1869 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
1870 ValueList.push_back(I);
1872 unsigned NextValueNo = ValueList.size();
1873 BasicBlock *CurBB = 0;
1874 unsigned CurBBNo = 0;
1878 // Read all the records.
1879 SmallVector<uint64_t, 64> Record;
1881 unsigned Code = Stream.ReadCode();
1882 if (Code == bitc::END_BLOCK) {
1883 if (Stream.ReadBlockEnd())
1884 return Error("Error at end of function block");
1888 if (Code == bitc::ENTER_SUBBLOCK) {
1889 switch (Stream.ReadSubBlockID()) {
1890 default: // Skip unknown content.
1891 if (Stream.SkipBlock())
1892 return Error("Malformed block record");
1894 case bitc::CONSTANTS_BLOCK_ID:
1895 if (ParseConstants()) return true;
1896 NextValueNo = ValueList.size();
1898 case bitc::VALUE_SYMTAB_BLOCK_ID:
1899 if (ParseValueSymbolTable()) return true;
1901 case bitc::METADATA_ATTACHMENT_ID:
1902 if (ParseMetadataAttachment()) return true;
1904 case bitc::METADATA_BLOCK_ID:
1905 if (ParseMetadata()) return true;
1911 if (Code == bitc::DEFINE_ABBREV) {
1912 Stream.ReadAbbrevRecord();
1919 unsigned BitCode = Stream.ReadRecord(Code, Record);
1921 default: // Default behavior: reject
1922 return Error("Unknown instruction");
1923 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
1924 if (Record.size() < 1 || Record[0] == 0)
1925 return Error("Invalid DECLAREBLOCKS record");
1926 // Create all the basic blocks for the function.
1927 FunctionBBs.resize(Record[0]);
1928 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
1929 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
1930 CurBB = FunctionBBs[0];
1933 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
1934 // This record indicates that the last instruction is at the same
1935 // location as the previous instruction with a location.
1938 // Get the last instruction emitted.
1939 if (CurBB && !CurBB->empty())
1941 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
1942 !FunctionBBs[CurBBNo-1]->empty())
1943 I = &FunctionBBs[CurBBNo-1]->back();
1945 if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record");
1946 I->setDebugLoc(LastLoc);
1950 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
1951 I = 0; // Get the last instruction emitted.
1952 if (CurBB && !CurBB->empty())
1954 else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
1955 !FunctionBBs[CurBBNo-1]->empty())
1956 I = &FunctionBBs[CurBBNo-1]->back();
1957 if (I == 0 || Record.size() < 4)
1958 return Error("Invalid FUNC_CODE_DEBUG_LOC record");
1960 unsigned Line = Record[0], Col = Record[1];
1961 unsigned ScopeID = Record[2], IAID = Record[3];
1963 MDNode *Scope = 0, *IA = 0;
1964 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
1965 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
1966 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
1967 I->setDebugLoc(LastLoc);
1972 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
1975 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
1976 getValue(Record, OpNum, LHS->getType(), RHS) ||
1977 OpNum+1 > Record.size())
1978 return Error("Invalid BINOP record");
1980 int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
1981 if (Opc == -1) return Error("Invalid BINOP record");
1982 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
1983 InstructionList.push_back(I);
1984 if (OpNum < Record.size()) {
1985 if (Opc == Instruction::Add ||
1986 Opc == Instruction::Sub ||
1987 Opc == Instruction::Mul ||
1988 Opc == Instruction::Shl) {
1989 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1990 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
1991 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1992 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
1993 } else if (Opc == Instruction::SDiv ||
1994 Opc == Instruction::UDiv ||
1995 Opc == Instruction::LShr ||
1996 Opc == Instruction::AShr) {
1997 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
1998 cast<BinaryOperator>(I)->setIsExact(true);
2003 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
2006 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2007 OpNum+2 != Record.size())
2008 return Error("Invalid CAST record");
2010 Type *ResTy = getTypeByID(Record[OpNum]);
2011 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2012 if (Opc == -1 || ResTy == 0)
2013 return Error("Invalid CAST record");
2014 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2015 InstructionList.push_back(I);
2018 case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
2019 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
2022 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
2023 return Error("Invalid GEP record");
2025 SmallVector<Value*, 16> GEPIdx;
2026 while (OpNum != Record.size()) {
2028 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2029 return Error("Invalid GEP record");
2030 GEPIdx.push_back(Op);
2033 I = GetElementPtrInst::Create(BasePtr, GEPIdx);
2034 InstructionList.push_back(I);
2035 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
2036 cast<GetElementPtrInst>(I)->setIsInBounds(true);
2040 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2041 // EXTRACTVAL: [opty, opval, n x indices]
2044 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
2045 return Error("Invalid EXTRACTVAL record");
2047 SmallVector<unsigned, 4> EXTRACTVALIdx;
2048 for (unsigned RecSize = Record.size();
2049 OpNum != RecSize; ++OpNum) {
2050 uint64_t Index = Record[OpNum];
2051 if ((unsigned)Index != Index)
2052 return Error("Invalid EXTRACTVAL index");
2053 EXTRACTVALIdx.push_back((unsigned)Index);
2056 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
2057 InstructionList.push_back(I);
2061 case bitc::FUNC_CODE_INST_INSERTVAL: {
2062 // INSERTVAL: [opty, opval, opty, opval, n x indices]
2065 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
2066 return Error("Invalid INSERTVAL record");
2068 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
2069 return Error("Invalid INSERTVAL record");
2071 SmallVector<unsigned, 4> INSERTVALIdx;
2072 for (unsigned RecSize = Record.size();
2073 OpNum != RecSize; ++OpNum) {
2074 uint64_t Index = Record[OpNum];
2075 if ((unsigned)Index != Index)
2076 return Error("Invalid INSERTVAL index");
2077 INSERTVALIdx.push_back((unsigned)Index);
2080 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
2081 InstructionList.push_back(I);
2085 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
2086 // obsolete form of select
2087 // handles select i1 ... in old bitcode
2089 Value *TrueVal, *FalseVal, *Cond;
2090 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2091 getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
2092 getValue(Record, OpNum, Type::getInt1Ty(Context), Cond))
2093 return Error("Invalid SELECT record");
2095 I = SelectInst::Create(Cond, TrueVal, FalseVal);
2096 InstructionList.push_back(I);
2100 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2101 // new form of select
2102 // handles select i1 or select [N x i1]
2104 Value *TrueVal, *FalseVal, *Cond;
2105 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2106 getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
2107 getValueTypePair(Record, OpNum, NextValueNo, Cond))
2108 return Error("Invalid SELECT record");
2110 // select condition can be either i1 or [N x i1]
2111 if (VectorType* vector_type =
2112 dyn_cast<VectorType>(Cond->getType())) {
2114 if (vector_type->getElementType() != Type::getInt1Ty(Context))
2115 return Error("Invalid SELECT condition type");
2118 if (Cond->getType() != Type::getInt1Ty(Context))
2119 return Error("Invalid SELECT condition type");
2122 I = SelectInst::Create(Cond, TrueVal, FalseVal);
2123 InstructionList.push_back(I);
2127 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
2130 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
2131 getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
2132 return Error("Invalid EXTRACTELT record");
2133 I = ExtractElementInst::Create(Vec, Idx);
2134 InstructionList.push_back(I);
2138 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
2140 Value *Vec, *Elt, *Idx;
2141 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
2142 getValue(Record, OpNum,
2143 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
2144 getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
2145 return Error("Invalid INSERTELT record");
2146 I = InsertElementInst::Create(Vec, Elt, Idx);
2147 InstructionList.push_back(I);
2151 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2153 Value *Vec1, *Vec2, *Mask;
2154 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
2155 getValue(Record, OpNum, Vec1->getType(), Vec2))
2156 return Error("Invalid SHUFFLEVEC record");
2158 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
2159 return Error("Invalid SHUFFLEVEC record");
2160 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
2161 InstructionList.push_back(I);
2165 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
2166 // Old form of ICmp/FCmp returning bool
2167 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2168 // both legal on vectors but had different behaviour.
2169 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2170 // FCmp/ICmp returning bool or vector of bool
2174 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
2175 getValue(Record, OpNum, LHS->getType(), RHS) ||
2176 OpNum+1 != Record.size())
2177 return Error("Invalid CMP record");
2179 if (LHS->getType()->isFPOrFPVectorTy())
2180 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
2182 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
2183 InstructionList.push_back(I);
2187 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
2189 unsigned Size = Record.size();
2191 I = ReturnInst::Create(Context);
2192 InstructionList.push_back(I);
2198 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2199 return Error("Invalid RET record");
2200 if (OpNum != Record.size())
2201 return Error("Invalid RET record");
2203 I = ReturnInst::Create(Context, Op);
2204 InstructionList.push_back(I);
2207 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
2208 if (Record.size() != 1 && Record.size() != 3)
2209 return Error("Invalid BR record");
2210 BasicBlock *TrueDest = getBasicBlock(Record[0]);
2212 return Error("Invalid BR record");
2214 if (Record.size() == 1) {
2215 I = BranchInst::Create(TrueDest);
2216 InstructionList.push_back(I);
2219 BasicBlock *FalseDest = getBasicBlock(Record[1]);
2220 Value *Cond = getFnValueByID(Record[2], Type::getInt1Ty(Context));
2221 if (FalseDest == 0 || Cond == 0)
2222 return Error("Invalid BR record");
2223 I = BranchInst::Create(TrueDest, FalseDest, Cond);
2224 InstructionList.push_back(I);
2228 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
2230 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
2231 // New SwitchInst format with case ranges.
2233 Type *OpTy = getTypeByID(Record[1]);
2234 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
2236 Value *Cond = getFnValueByID(Record[2], OpTy);
2237 BasicBlock *Default = getBasicBlock(Record[3]);
2238 if (OpTy == 0 || Cond == 0 || Default == 0)
2239 return Error("Invalid SWITCH record");
2241 unsigned NumCases = Record[4];
2243 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2244 InstructionList.push_back(SI);
2246 unsigned CurIdx = 5;
2247 for (unsigned i = 0; i != NumCases; ++i) {
2248 IntegersSubsetToBB CaseBuilder;
2249 unsigned NumItems = Record[CurIdx++];
2250 for (unsigned ci = 0; ci != NumItems; ++ci) {
2251 bool isSingleNumber = Record[CurIdx++];
2254 unsigned ActiveWords = 1;
2255 if (ValueBitWidth > 64)
2256 ActiveWords = Record[CurIdx++];
2257 Low = ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2259 CurIdx += ActiveWords;
2261 if (!isSingleNumber) {
2263 if (ValueBitWidth > 64)
2264 ActiveWords = Record[CurIdx++];
2266 ReadWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
2269 CaseBuilder.add(IntItem::fromType(OpTy, Low),
2270 IntItem::fromType(OpTy, High));
2271 CurIdx += ActiveWords;
2273 CaseBuilder.add(IntItem::fromType(OpTy, Low));
2275 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
2276 IntegersSubset Case = CaseBuilder.getCase();
2277 SI->addCase(Case, DestBB);
2279 uint16_t Hash = SI->hash();
2280 if (Hash != (Record[0] & 0xFFFF))
2281 return Error("Invalid SWITCH record");
2286 // Old SwitchInst format without case ranges.
2288 if (Record.size() < 3 || (Record.size() & 1) == 0)
2289 return Error("Invalid SWITCH record");
2290 Type *OpTy = getTypeByID(Record[0]);
2291 Value *Cond = getFnValueByID(Record[1], OpTy);
2292 BasicBlock *Default = getBasicBlock(Record[2]);
2293 if (OpTy == 0 || Cond == 0 || Default == 0)
2294 return Error("Invalid SWITCH record");
2295 unsigned NumCases = (Record.size()-3)/2;
2296 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2297 InstructionList.push_back(SI);
2298 for (unsigned i = 0, e = NumCases; i != e; ++i) {
2299 ConstantInt *CaseVal =
2300 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2301 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2302 if (CaseVal == 0 || DestBB == 0) {
2304 return Error("Invalid SWITCH record!");
2306 SI->addCase(CaseVal, DestBB);
2311 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
2312 if (Record.size() < 2)
2313 return Error("Invalid INDIRECTBR record");
2314 Type *OpTy = getTypeByID(Record[0]);
2315 Value *Address = getFnValueByID(Record[1], OpTy);
2316 if (OpTy == 0 || Address == 0)
2317 return Error("Invalid INDIRECTBR record");
2318 unsigned NumDests = Record.size()-2;
2319 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
2320 InstructionList.push_back(IBI);
2321 for (unsigned i = 0, e = NumDests; i != e; ++i) {
2322 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2323 IBI->addDestination(DestBB);
2326 return Error("Invalid INDIRECTBR record!");
2333 case bitc::FUNC_CODE_INST_INVOKE: {
2334 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
2335 if (Record.size() < 4) return Error("Invalid INVOKE record");
2336 AttrListPtr PAL = getAttributes(Record[0]);
2337 unsigned CCInfo = Record[1];
2338 BasicBlock *NormalBB = getBasicBlock(Record[2]);
2339 BasicBlock *UnwindBB = getBasicBlock(Record[3]);
2343 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
2344 return Error("Invalid INVOKE record");
2346 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
2347 FunctionType *FTy = !CalleeTy ? 0 :
2348 dyn_cast<FunctionType>(CalleeTy->getElementType());
2350 // Check that the right number of fixed parameters are here.
2351 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
2352 Record.size() < OpNum+FTy->getNumParams())
2353 return Error("Invalid INVOKE record");
2355 SmallVector<Value*, 16> Ops;
2356 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
2357 Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
2358 if (Ops.back() == 0) return Error("Invalid INVOKE record");
2361 if (!FTy->isVarArg()) {
2362 if (Record.size() != OpNum)
2363 return Error("Invalid INVOKE record");
2365 // Read type/value pairs for varargs params.
2366 while (OpNum != Record.size()) {
2368 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2369 return Error("Invalid INVOKE record");
2374 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
2375 InstructionList.push_back(I);
2376 cast<InvokeInst>(I)->setCallingConv(
2377 static_cast<CallingConv::ID>(CCInfo));
2378 cast<InvokeInst>(I)->setAttributes(PAL);
2381 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
2384 if (getValueTypePair(Record, Idx, NextValueNo, Val))
2385 return Error("Invalid RESUME record");
2386 I = ResumeInst::Create(Val);
2387 InstructionList.push_back(I);
2390 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
2391 I = new UnreachableInst(Context);
2392 InstructionList.push_back(I);
2394 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
2395 if (Record.size() < 1 || ((Record.size()-1)&1))
2396 return Error("Invalid PHI record");
2397 Type *Ty = getTypeByID(Record[0]);
2398 if (!Ty) return Error("Invalid PHI record");
2400 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
2401 InstructionList.push_back(PN);
2403 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
2404 Value *V = getFnValueByID(Record[1+i], Ty);
2405 BasicBlock *BB = getBasicBlock(Record[2+i]);
2406 if (!V || !BB) return Error("Invalid PHI record");
2407 PN->addIncoming(V, BB);
2413 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2414 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2416 if (Record.size() < 4)
2417 return Error("Invalid LANDINGPAD record");
2418 Type *Ty = getTypeByID(Record[Idx++]);
2419 if (!Ty) return Error("Invalid LANDINGPAD record");
2421 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
2422 return Error("Invalid LANDINGPAD record");
2424 bool IsCleanup = !!Record[Idx++];
2425 unsigned NumClauses = Record[Idx++];
2426 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2427 LP->setCleanup(IsCleanup);
2428 for (unsigned J = 0; J != NumClauses; ++J) {
2429 LandingPadInst::ClauseType CT =
2430 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2433 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2435 return Error("Invalid LANDINGPAD record");
2438 assert((CT != LandingPadInst::Catch ||
2439 !isa<ArrayType>(Val->getType())) &&
2440 "Catch clause has a invalid type!");
2441 assert((CT != LandingPadInst::Filter ||
2442 isa<ArrayType>(Val->getType())) &&
2443 "Filter clause has invalid type!");
2448 InstructionList.push_back(I);
2452 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
2453 if (Record.size() != 4)
2454 return Error("Invalid ALLOCA record");
2456 dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
2457 Type *OpTy = getTypeByID(Record[1]);
2458 Value *Size = getFnValueByID(Record[2], OpTy);
2459 unsigned Align = Record[3];
2460 if (!Ty || !Size) return Error("Invalid ALLOCA record");
2461 I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
2462 InstructionList.push_back(I);
2465 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
2468 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2469 OpNum+2 != Record.size())
2470 return Error("Invalid LOAD record");
2472 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
2473 InstructionList.push_back(I);
2476 case bitc::FUNC_CODE_INST_LOADATOMIC: {
2477 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
2480 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2481 OpNum+4 != Record.size())
2482 return Error("Invalid LOADATOMIC record");
2485 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2486 if (Ordering == NotAtomic || Ordering == Release ||
2487 Ordering == AcquireRelease)
2488 return Error("Invalid LOADATOMIC record");
2489 if (Ordering != NotAtomic && Record[OpNum] == 0)
2490 return Error("Invalid LOADATOMIC record");
2491 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2493 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2494 Ordering, SynchScope);
2495 InstructionList.push_back(I);
2498 case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
2501 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2502 getValue(Record, OpNum,
2503 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2504 OpNum+2 != Record.size())
2505 return Error("Invalid STORE record");
2507 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
2508 InstructionList.push_back(I);
2511 case bitc::FUNC_CODE_INST_STOREATOMIC: {
2512 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
2515 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2516 getValue(Record, OpNum,
2517 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2518 OpNum+4 != Record.size())
2519 return Error("Invalid STOREATOMIC record");
2521 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2522 if (Ordering == NotAtomic || Ordering == Acquire ||
2523 Ordering == AcquireRelease)
2524 return Error("Invalid STOREATOMIC record");
2525 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2526 if (Ordering != NotAtomic && Record[OpNum] == 0)
2527 return Error("Invalid STOREATOMIC record");
2529 I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1,
2530 Ordering, SynchScope);
2531 InstructionList.push_back(I);
2534 case bitc::FUNC_CODE_INST_CMPXCHG: {
2535 // CMPXCHG:[ptrty, ptr, cmp, new, vol, ordering, synchscope]
2537 Value *Ptr, *Cmp, *New;
2538 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2539 getValue(Record, OpNum,
2540 cast<PointerType>(Ptr->getType())->getElementType(), Cmp) ||
2541 getValue(Record, OpNum,
2542 cast<PointerType>(Ptr->getType())->getElementType(), New) ||
2543 OpNum+3 != Record.size())
2544 return Error("Invalid CMPXCHG record");
2545 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+1]);
2546 if (Ordering == NotAtomic || Ordering == Unordered)
2547 return Error("Invalid CMPXCHG record");
2548 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]);
2549 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope);
2550 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
2551 InstructionList.push_back(I);
2554 case bitc::FUNC_CODE_INST_ATOMICRMW: {
2555 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
2558 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
2559 getValue(Record, OpNum,
2560 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
2561 OpNum+4 != Record.size())
2562 return Error("Invalid ATOMICRMW record");
2563 AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]);
2564 if (Operation < AtomicRMWInst::FIRST_BINOP ||
2565 Operation > AtomicRMWInst::LAST_BINOP)
2566 return Error("Invalid ATOMICRMW record");
2567 AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]);
2568 if (Ordering == NotAtomic || Ordering == Unordered)
2569 return Error("Invalid ATOMICRMW record");
2570 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]);
2571 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
2572 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
2573 InstructionList.push_back(I);
2576 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
2577 if (2 != Record.size())
2578 return Error("Invalid FENCE record");
2579 AtomicOrdering Ordering = GetDecodedOrdering(Record[0]);
2580 if (Ordering == NotAtomic || Ordering == Unordered ||
2581 Ordering == Monotonic)
2582 return Error("Invalid FENCE record");
2583 SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]);
2584 I = new FenceInst(Context, Ordering, SynchScope);
2585 InstructionList.push_back(I);
2588 case bitc::FUNC_CODE_INST_CALL: {
2589 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
2590 if (Record.size() < 3)
2591 return Error("Invalid CALL record");
2593 AttrListPtr PAL = getAttributes(Record[0]);
2594 unsigned CCInfo = Record[1];
2598 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
2599 return Error("Invalid CALL record");
2601 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
2602 FunctionType *FTy = 0;
2603 if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
2604 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
2605 return Error("Invalid CALL record");
2607 SmallVector<Value*, 16> Args;
2608 // Read the fixed params.
2609 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
2610 if (FTy->getParamType(i)->isLabelTy())
2611 Args.push_back(getBasicBlock(Record[OpNum]));
2613 Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
2614 if (Args.back() == 0) return Error("Invalid CALL record");
2617 // Read type/value pairs for varargs params.
2618 if (!FTy->isVarArg()) {
2619 if (OpNum != Record.size())
2620 return Error("Invalid CALL record");
2622 while (OpNum != Record.size()) {
2624 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
2625 return Error("Invalid CALL record");
2630 I = CallInst::Create(Callee, Args);
2631 InstructionList.push_back(I);
2632 cast<CallInst>(I)->setCallingConv(
2633 static_cast<CallingConv::ID>(CCInfo>>1));
2634 cast<CallInst>(I)->setTailCall(CCInfo & 1);
2635 cast<CallInst>(I)->setAttributes(PAL);
2638 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
2639 if (Record.size() < 3)
2640 return Error("Invalid VAARG record");
2641 Type *OpTy = getTypeByID(Record[0]);
2642 Value *Op = getFnValueByID(Record[1], OpTy);
2643 Type *ResTy = getTypeByID(Record[2]);
2644 if (!OpTy || !Op || !ResTy)
2645 return Error("Invalid VAARG record");
2646 I = new VAArgInst(Op, ResTy);
2647 InstructionList.push_back(I);
2652 // Add instruction to end of current BB. If there is no current BB, reject
2656 return Error("Invalid instruction with no BB");
2658 CurBB->getInstList().push_back(I);
2660 // If this was a terminator instruction, move to the next block.
2661 if (isa<TerminatorInst>(I)) {
2663 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
2666 // Non-void values get registered in the value table for future use.
2667 if (I && !I->getType()->isVoidTy())
2668 ValueList.AssignValue(I, NextValueNo++);
2671 // Check the function list for unresolved values.
2672 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
2673 if (A->getParent() == 0) {
2674 // We found at least one unresolved value. Nuke them all to avoid leaks.
2675 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
2676 if ((A = dyn_cast<Argument>(ValueList[i])) && A->getParent() == 0) {
2677 A->replaceAllUsesWith(UndefValue::get(A->getType()));
2681 return Error("Never resolved value found in function!");
2685 // FIXME: Check for unresolved forward-declared metadata references
2686 // and clean up leaks.
2688 // See if anything took the address of blocks in this function. If so,
2689 // resolve them now.
2690 DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
2691 BlockAddrFwdRefs.find(F);
2692 if (BAFRI != BlockAddrFwdRefs.end()) {
2693 std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
2694 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
2695 unsigned BlockIdx = RefList[i].first;
2696 if (BlockIdx >= FunctionBBs.size())
2697 return Error("Invalid blockaddress block #");
2699 GlobalVariable *FwdRef = RefList[i].second;
2700 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
2701 FwdRef->eraseFromParent();
2704 BlockAddrFwdRefs.erase(BAFRI);
2707 // Trim the value list down to the size it was before we parsed this function.
2708 ValueList.shrinkTo(ModuleValueListSize);
2709 MDValueList.shrinkTo(ModuleMDValueListSize);
2710 std::vector<BasicBlock*>().swap(FunctionBBs);
2714 /// FindFunctionInStream - Find the function body in the bitcode stream
2715 bool BitcodeReader::FindFunctionInStream(Function *F,
2716 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
2717 while (DeferredFunctionInfoIterator->second == 0) {
2718 if (Stream.AtEndOfStream())
2719 return Error("Could not find Function in stream");
2720 // ParseModule will parse the next body in the stream and set its
2721 // position in the DeferredFunctionInfo map.
2722 if (ParseModule(true)) return true;
2727 //===----------------------------------------------------------------------===//
2728 // GVMaterializer implementation
2729 //===----------------------------------------------------------------------===//
2732 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
2733 if (const Function *F = dyn_cast<Function>(GV)) {
2734 return F->isDeclaration() &&
2735 DeferredFunctionInfo.count(const_cast<Function*>(F));
2740 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
2741 Function *F = dyn_cast<Function>(GV);
2742 // If it's not a function or is already material, ignore the request.
2743 if (!F || !F->isMaterializable()) return false;
2745 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
2746 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
2747 // If its position is recorded as 0, its body is somewhere in the stream
2748 // but we haven't seen it yet.
2749 if (DFII->second == 0)
2750 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
2752 // Move the bit stream to the saved position of the deferred function body.
2753 Stream.JumpToBit(DFII->second);
2755 if (ParseFunctionBody(F)) {
2756 if (ErrInfo) *ErrInfo = ErrorString;
2760 // Upgrade any old intrinsic calls in the function.
2761 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
2762 E = UpgradedIntrinsics.end(); I != E; ++I) {
2763 if (I->first != I->second) {
2764 for (Value::use_iterator UI = I->first->use_begin(),
2765 UE = I->first->use_end(); UI != UE; ) {
2766 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
2767 UpgradeIntrinsicCall(CI, I->second);
2775 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
2776 const Function *F = dyn_cast<Function>(GV);
2777 if (!F || F->isDeclaration())
2779 return DeferredFunctionInfo.count(const_cast<Function*>(F));
2782 void BitcodeReader::Dematerialize(GlobalValue *GV) {
2783 Function *F = dyn_cast<Function>(GV);
2784 // If this function isn't dematerializable, this is a noop.
2785 if (!F || !isDematerializable(F))
2788 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
2790 // Just forget the function body, we can remat it later.
2795 bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
2796 assert(M == TheModule &&
2797 "Can only Materialize the Module this BitcodeReader is attached to.");
2798 // Iterate over the module, deserializing any functions that are still on
2800 for (Module::iterator F = TheModule->begin(), E = TheModule->end();
2802 if (F->isMaterializable() &&
2803 Materialize(F, ErrInfo))
2806 // At this point, if there are any function bodies, the current bit is
2807 // pointing to the END_BLOCK record after them. Now make sure the rest
2808 // of the bits in the module have been read.
2812 // Upgrade any intrinsic calls that slipped through (should not happen!) and
2813 // delete the old functions to clean up. We can't do this unless the entire
2814 // module is materialized because there could always be another function body
2815 // with calls to the old function.
2816 for (std::vector<std::pair<Function*, Function*> >::iterator I =
2817 UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
2818 if (I->first != I->second) {
2819 for (Value::use_iterator UI = I->first->use_begin(),
2820 UE = I->first->use_end(); UI != UE; ) {
2821 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
2822 UpgradeIntrinsicCall(CI, I->second);
2824 if (!I->first->use_empty())
2825 I->first->replaceAllUsesWith(I->second);
2826 I->first->eraseFromParent();
2829 std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
2834 bool BitcodeReader::InitStream() {
2835 if (LazyStreamer) return InitLazyStream();
2836 return InitStreamFromBuffer();
2839 bool BitcodeReader::InitStreamFromBuffer() {
2840 const unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
2841 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
2843 if (Buffer->getBufferSize() & 3) {
2844 if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
2845 return Error("Invalid bitcode signature");
2847 return Error("Bitcode stream should be a multiple of 4 bytes in length");
2850 // If we have a wrapper header, parse it and ignore the non-bc file contents.
2851 // The magic number is 0x0B17C0DE stored in little endian.
2852 if (isBitcodeWrapper(BufPtr, BufEnd))
2853 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
2854 return Error("Invalid bitcode wrapper header");
2856 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
2857 Stream.init(*StreamFile);
2862 bool BitcodeReader::InitLazyStream() {
2863 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
2865 StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
2866 StreamFile.reset(new BitstreamReader(Bytes));
2867 Stream.init(*StreamFile);
2869 unsigned char buf[16];
2870 if (Bytes->readBytes(0, 16, buf, NULL) == -1)
2871 return Error("Bitcode stream must be at least 16 bytes in length");
2873 if (!isBitcode(buf, buf + 16))
2874 return Error("Invalid bitcode signature");
2876 if (isBitcodeWrapper(buf, buf + 4)) {
2877 const unsigned char *bitcodeStart = buf;
2878 const unsigned char *bitcodeEnd = buf + 16;
2879 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
2880 Bytes->dropLeadingBytes(bitcodeStart - buf);
2881 Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
2886 //===----------------------------------------------------------------------===//
2887 // External interface
2888 //===----------------------------------------------------------------------===//
2890 /// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
2892 Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
2893 LLVMContext& Context,
2894 std::string *ErrMsg) {
2895 Module *M = new Module(Buffer->getBufferIdentifier(), Context);
2896 BitcodeReader *R = new BitcodeReader(Buffer, Context);
2897 M->setMaterializer(R);
2898 if (R->ParseBitcodeInto(M)) {
2900 *ErrMsg = R->getErrorString();
2902 delete M; // Also deletes R.
2905 // Have the BitcodeReader dtor delete 'Buffer'.
2906 R->setBufferOwned(true);
2908 R->materializeForwardReferencedFunctions();
2914 Module *llvm::getStreamedBitcodeModule(const std::string &name,
2915 DataStreamer *streamer,
2916 LLVMContext &Context,
2917 std::string *ErrMsg) {
2918 Module *M = new Module(name, Context);
2919 BitcodeReader *R = new BitcodeReader(streamer, Context);
2920 M->setMaterializer(R);
2921 if (R->ParseBitcodeInto(M)) {
2923 *ErrMsg = R->getErrorString();
2924 delete M; // Also deletes R.
2927 R->setBufferOwned(false); // no buffer to delete
2931 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
2932 /// If an error occurs, return null and fill in *ErrMsg if non-null.
2933 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
2934 std::string *ErrMsg){
2935 Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
2938 // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
2939 // there was an error.
2940 static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
2942 // Read in the entire module, and destroy the BitcodeReader.
2943 if (M->MaterializeAllPermanently(ErrMsg)) {
2948 // TODO: Restore the use-lists to the in-memory state when the bitcode was
2949 // written. We must defer until the Module has been fully materialized.
2954 std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
2955 LLVMContext& Context,
2956 std::string *ErrMsg) {
2957 BitcodeReader *R = new BitcodeReader(Buffer, Context);
2958 // Don't let the BitcodeReader dtor delete 'Buffer'.
2959 R->setBufferOwned(false);
2961 std::string Triple("");
2962 if (R->ParseTriple(Triple))
2964 *ErrMsg = R->getErrorString();