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 #include "llvm/Bitcode/BitcodeReader.h"
11 #include "MetadataLoader.h"
12 #include "ValueList.h"
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Bitcode/BitstreamReader.h"
25 #include "llvm/Bitcode/LLVMBitCodes.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/AutoUpgrade.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Comdat.h"
34 #include "llvm/IR/Constant.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/DebugInfo.h"
38 #include "llvm/IR/DebugInfoMetadata.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GVMaterializer.h"
43 #include "llvm/IR/GlobalAlias.h"
44 #include "llvm/IR/GlobalIFunc.h"
45 #include "llvm/IR/GlobalIndirectSymbol.h"
46 #include "llvm/IR/GlobalObject.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/InlineAsm.h"
50 #include "llvm/IR/InstIterator.h"
51 #include "llvm/IR/InstrTypes.h"
52 #include "llvm/IR/Instruction.h"
53 #include "llvm/IR/Instructions.h"
54 #include "llvm/IR/Intrinsics.h"
55 #include "llvm/IR/LLVMContext.h"
56 #include "llvm/IR/Metadata.h"
57 #include "llvm/IR/Module.h"
58 #include "llvm/IR/ModuleSummaryIndex.h"
59 #include "llvm/IR/Operator.h"
60 #include "llvm/IR/Type.h"
61 #include "llvm/IR/Value.h"
62 #include "llvm/IR/Verifier.h"
63 #include "llvm/Support/AtomicOrdering.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CommandLine.h"
66 #include "llvm/Support/Compiler.h"
67 #include "llvm/Support/Debug.h"
68 #include "llvm/Support/Error.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/ErrorOr.h"
71 #include "llvm/Support/ManagedStatic.h"
72 #include "llvm/Support/MathExtras.h"
73 #include "llvm/Support/MemoryBuffer.h"
74 #include "llvm/Support/raw_ostream.h"
84 #include <system_error>
91 static cl::opt
<bool> PrintSummaryGUIDs(
92 "print-summary-global-ids", cl::init(false), cl::Hidden
,
94 "Print the global id for each value when reading the module summary"));
99 SWITCH_INST_MAGIC
= 0x4B5 // May 2012 => 1205 => Hex
102 } // end anonymous namespace
104 static Error
error(const Twine
&Message
) {
105 return make_error
<StringError
>(
106 Message
, make_error_code(BitcodeError::CorruptedBitcode
));
109 /// Helper to read the header common to all bitcode files.
110 static bool hasValidBitcodeHeader(BitstreamCursor
&Stream
) {
111 // Sniff for the signature.
112 if (!Stream
.canSkipToPos(4) ||
113 Stream
.Read(8) != 'B' ||
114 Stream
.Read(8) != 'C' ||
115 Stream
.Read(4) != 0x0 ||
116 Stream
.Read(4) != 0xC ||
117 Stream
.Read(4) != 0xE ||
118 Stream
.Read(4) != 0xD)
123 static Expected
<BitstreamCursor
> initStream(MemoryBufferRef Buffer
) {
124 const unsigned char *BufPtr
= (const unsigned char *)Buffer
.getBufferStart();
125 const unsigned char *BufEnd
= BufPtr
+ Buffer
.getBufferSize();
127 if (Buffer
.getBufferSize() & 3)
128 return error("Invalid bitcode signature");
130 // If we have a wrapper header, parse it and ignore the non-bc file contents.
131 // The magic number is 0x0B17C0DE stored in little endian.
132 if (isBitcodeWrapper(BufPtr
, BufEnd
))
133 if (SkipBitcodeWrapperHeader(BufPtr
, BufEnd
, true))
134 return error("Invalid bitcode wrapper header");
136 BitstreamCursor
Stream(ArrayRef
<uint8_t>(BufPtr
, BufEnd
));
137 if (!hasValidBitcodeHeader(Stream
))
138 return error("Invalid bitcode signature");
140 return std::move(Stream
);
143 /// Convert a string from a record into an std::string, return true on failure.
144 template <typename StrTy
>
145 static bool convertToString(ArrayRef
<uint64_t> Record
, unsigned Idx
,
147 if (Idx
> Record
.size())
150 for (unsigned i
= Idx
, e
= Record
.size(); i
!= e
; ++i
)
151 Result
+= (char)Record
[i
];
155 // Strip all the TBAA attachment for the module.
156 static void stripTBAA(Module
*M
) {
158 if (F
.isMaterializable())
160 for (auto &I
: instructions(F
))
161 I
.setMetadata(LLVMContext::MD_tbaa
, nullptr);
165 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
166 /// "epoch" encoded in the bitcode, and return the producer name if any.
167 static Expected
<std::string
> readIdentificationBlock(BitstreamCursor
&Stream
) {
168 if (Stream
.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID
))
169 return error("Invalid record");
171 // Read all the records.
172 SmallVector
<uint64_t, 64> Record
;
174 std::string ProducerIdentification
;
177 BitstreamEntry Entry
= Stream
.advance();
179 switch (Entry
.Kind
) {
181 case BitstreamEntry::Error
:
182 return error("Malformed block");
183 case BitstreamEntry::EndBlock
:
184 return ProducerIdentification
;
185 case BitstreamEntry::Record
:
186 // The interesting case.
192 unsigned BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
194 default: // Default behavior: reject
195 return error("Invalid value");
196 case bitc::IDENTIFICATION_CODE_STRING
: // IDENTIFICATION: [strchr x N]
197 convertToString(Record
, 0, ProducerIdentification
);
199 case bitc::IDENTIFICATION_CODE_EPOCH
: { // EPOCH: [epoch#]
200 unsigned epoch
= (unsigned)Record
[0];
201 if (epoch
!= bitc::BITCODE_CURRENT_EPOCH
) {
203 Twine("Incompatible epoch: Bitcode '") + Twine(epoch
) +
204 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH
) + "'");
211 static Expected
<std::string
> readIdentificationCode(BitstreamCursor
&Stream
) {
212 // We expect a number of well-defined blocks, though we don't necessarily
213 // need to understand them all.
215 if (Stream
.AtEndOfStream())
218 BitstreamEntry Entry
= Stream
.advance();
219 switch (Entry
.Kind
) {
220 case BitstreamEntry::EndBlock
:
221 case BitstreamEntry::Error
:
222 return error("Malformed block");
224 case BitstreamEntry::SubBlock
:
225 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
)
226 return readIdentificationBlock(Stream
);
228 // Ignore other sub-blocks.
229 if (Stream
.SkipBlock())
230 return error("Malformed block");
232 case BitstreamEntry::Record
:
233 Stream
.skipRecord(Entry
.ID
);
239 static Expected
<bool> hasObjCCategoryInModule(BitstreamCursor
&Stream
) {
240 if (Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
241 return error("Invalid record");
243 SmallVector
<uint64_t, 64> Record
;
244 // Read all the records for this module.
247 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
249 switch (Entry
.Kind
) {
250 case BitstreamEntry::SubBlock
: // Handled for us already.
251 case BitstreamEntry::Error
:
252 return error("Malformed block");
253 case BitstreamEntry::EndBlock
:
255 case BitstreamEntry::Record
:
256 // The interesting case.
261 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
263 break; // Default behavior, ignore unknown content.
264 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
266 if (convertToString(Record
, 0, S
))
267 return error("Invalid record");
268 // Check for the i386 and other (x86_64, ARM) conventions
269 if (S
.find("__DATA,__objc_catlist") != std::string::npos
||
270 S
.find("__OBJC,__category") != std::string::npos
)
277 llvm_unreachable("Exit infinite loop");
280 static Expected
<bool> hasObjCCategory(BitstreamCursor
&Stream
) {
281 // We expect a number of well-defined blocks, though we don't necessarily
282 // need to understand them all.
284 BitstreamEntry Entry
= Stream
.advance();
286 switch (Entry
.Kind
) {
287 case BitstreamEntry::Error
:
288 return error("Malformed block");
289 case BitstreamEntry::EndBlock
:
292 case BitstreamEntry::SubBlock
:
293 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
294 return hasObjCCategoryInModule(Stream
);
296 // Ignore other sub-blocks.
297 if (Stream
.SkipBlock())
298 return error("Malformed block");
301 case BitstreamEntry::Record
:
302 Stream
.skipRecord(Entry
.ID
);
308 static Expected
<std::string
> readModuleTriple(BitstreamCursor
&Stream
) {
309 if (Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
310 return error("Invalid record");
312 SmallVector
<uint64_t, 64> Record
;
316 // Read all the records for this module.
318 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
320 switch (Entry
.Kind
) {
321 case BitstreamEntry::SubBlock
: // Handled for us already.
322 case BitstreamEntry::Error
:
323 return error("Malformed block");
324 case BitstreamEntry::EndBlock
:
326 case BitstreamEntry::Record
:
327 // The interesting case.
332 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
333 default: break; // Default behavior, ignore unknown content.
334 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
336 if (convertToString(Record
, 0, S
))
337 return error("Invalid record");
344 llvm_unreachable("Exit infinite loop");
347 static Expected
<std::string
> readTriple(BitstreamCursor
&Stream
) {
348 // We expect a number of well-defined blocks, though we don't necessarily
349 // need to understand them all.
351 BitstreamEntry Entry
= Stream
.advance();
353 switch (Entry
.Kind
) {
354 case BitstreamEntry::Error
:
355 return error("Malformed block");
356 case BitstreamEntry::EndBlock
:
359 case BitstreamEntry::SubBlock
:
360 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
361 return readModuleTriple(Stream
);
363 // Ignore other sub-blocks.
364 if (Stream
.SkipBlock())
365 return error("Malformed block");
368 case BitstreamEntry::Record
:
369 Stream
.skipRecord(Entry
.ID
);
377 class BitcodeReaderBase
{
379 BitcodeReaderBase(BitstreamCursor Stream
, StringRef Strtab
)
380 : Stream(std::move(Stream
)), Strtab(Strtab
) {
381 this->Stream
.setBlockInfo(&BlockInfo
);
384 BitstreamBlockInfo BlockInfo
;
385 BitstreamCursor Stream
;
388 /// In version 2 of the bitcode we store names of global values and comdats in
389 /// a string table rather than in the VST.
390 bool UseStrtab
= false;
392 Expected
<unsigned> parseVersionRecord(ArrayRef
<uint64_t> Record
);
394 /// If this module uses a string table, pop the reference to the string table
395 /// and return the referenced string and the rest of the record. Otherwise
396 /// just return the record itself.
397 std::pair
<StringRef
, ArrayRef
<uint64_t>>
398 readNameFromStrtab(ArrayRef
<uint64_t> Record
);
400 bool readBlockInfo();
402 // Contains an arbitrary and optional string identifying the bitcode producer
403 std::string ProducerIdentification
;
405 Error
error(const Twine
&Message
);
408 } // end anonymous namespace
410 Error
BitcodeReaderBase::error(const Twine
&Message
) {
411 std::string FullMsg
= Message
.str();
412 if (!ProducerIdentification
.empty())
413 FullMsg
+= " (Producer: '" + ProducerIdentification
+ "' Reader: 'LLVM " +
414 LLVM_VERSION_STRING
"')";
415 return ::error(FullMsg
);
419 BitcodeReaderBase::parseVersionRecord(ArrayRef
<uint64_t> Record
) {
421 return error("Invalid record");
422 unsigned ModuleVersion
= Record
[0];
423 if (ModuleVersion
> 2)
424 return error("Invalid value");
425 UseStrtab
= ModuleVersion
>= 2;
426 return ModuleVersion
;
429 std::pair
<StringRef
, ArrayRef
<uint64_t>>
430 BitcodeReaderBase::readNameFromStrtab(ArrayRef
<uint64_t> Record
) {
433 // Invalid reference. Let the caller complain about the record being empty.
434 if (Record
[0] + Record
[1] > Strtab
.size())
436 return {StringRef(Strtab
.data() + Record
[0], Record
[1]), Record
.slice(2)};
441 class BitcodeReader
: public BitcodeReaderBase
, public GVMaterializer
{
442 LLVMContext
&Context
;
443 Module
*TheModule
= nullptr;
444 // Next offset to start scanning for lazy parsing of function bodies.
445 uint64_t NextUnreadBit
= 0;
446 // Last function offset found in the VST.
447 uint64_t LastFunctionBlockBit
= 0;
448 bool SeenValueSymbolTable
= false;
449 uint64_t VSTOffset
= 0;
451 std::vector
<std::string
> SectionTable
;
452 std::vector
<std::string
> GCTable
;
454 std::vector
<Type
*> TypeList
;
455 BitcodeReaderValueList ValueList
;
456 Optional
<MetadataLoader
> MDLoader
;
457 std::vector
<Comdat
*> ComdatList
;
458 SmallVector
<Instruction
*, 64> InstructionList
;
460 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInits
;
461 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>> IndirectSymbolInits
;
462 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixes
;
463 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologues
;
464 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFns
;
466 /// The set of attributes by index. Index zero in the file is for null, and
467 /// is thus not represented here. As such all indices are off by one.
468 std::vector
<AttributeList
> MAttributes
;
470 /// The set of attribute groups.
471 std::map
<unsigned, AttributeList
> MAttributeGroups
;
473 /// While parsing a function body, this is a list of the basic blocks for the
475 std::vector
<BasicBlock
*> FunctionBBs
;
477 // When reading the module header, this list is populated with functions that
478 // have bodies later in the file.
479 std::vector
<Function
*> FunctionsWithBodies
;
481 // When intrinsic functions are encountered which require upgrading they are
482 // stored here with their replacement function.
483 using UpdatedIntrinsicMap
= DenseMap
<Function
*, Function
*>;
484 UpdatedIntrinsicMap UpgradedIntrinsics
;
485 // Intrinsics which were remangled because of types rename
486 UpdatedIntrinsicMap RemangledIntrinsics
;
488 // Several operations happen after the module header has been read, but
489 // before function bodies are processed. This keeps track of whether
490 // we've done this yet.
491 bool SeenFirstFunctionBody
= false;
493 /// When function bodies are initially scanned, this map contains info about
494 /// where to find deferred function body in the stream.
495 DenseMap
<Function
*, uint64_t> DeferredFunctionInfo
;
497 /// When Metadata block is initially scanned when parsing the module, we may
498 /// choose to defer parsing of the metadata. This vector contains info about
499 /// which Metadata blocks are deferred.
500 std::vector
<uint64_t> DeferredMetadataInfo
;
502 /// These are basic blocks forward-referenced by block addresses. They are
503 /// inserted lazily into functions when they're loaded. The basic block ID is
504 /// its index into the vector.
505 DenseMap
<Function
*, std::vector
<BasicBlock
*>> BasicBlockFwdRefs
;
506 std::deque
<Function
*> BasicBlockFwdRefQueue
;
508 /// Indicates that we are using a new encoding for instruction operands where
509 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
510 /// instruction number, for a more compact encoding. Some instruction
511 /// operands are not relative to the instruction ID: basic block numbers, and
512 /// types. Once the old style function blocks have been phased out, we would
513 /// not need this flag.
514 bool UseRelativeIDs
= false;
516 /// True if all functions will be materialized, negating the need to process
517 /// (e.g.) blockaddress forward references.
518 bool WillMaterializeAllForwardRefs
= false;
520 bool StripDebugInfo
= false;
521 TBAAVerifier TBAAVerifyHelper
;
523 std::vector
<std::string
> BundleTags
;
524 SmallVector
<SyncScope::ID
, 8> SSIDs
;
527 BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
528 StringRef ProducerIdentification
, LLVMContext
&Context
);
530 Error
materializeForwardReferencedFunctions();
532 Error
materialize(GlobalValue
*GV
) override
;
533 Error
materializeModule() override
;
534 std::vector
<StructType
*> getIdentifiedStructTypes() const override
;
536 /// Main interface to parsing a bitcode buffer.
537 /// \returns true if an error occurred.
538 Error
parseBitcodeInto(Module
*M
, bool ShouldLazyLoadMetadata
= false,
539 bool IsImporting
= false);
541 static uint64_t decodeSignRotatedValue(uint64_t V
);
543 /// Materialize any deferred Metadata block.
544 Error
materializeMetadata() override
;
546 void setStripDebugInfo() override
;
549 std::vector
<StructType
*> IdentifiedStructTypes
;
550 StructType
*createIdentifiedStructType(LLVMContext
&Context
, StringRef Name
);
551 StructType
*createIdentifiedStructType(LLVMContext
&Context
);
553 Type
*getTypeByID(unsigned ID
);
555 Value
*getFnValueByID(unsigned ID
, Type
*Ty
) {
556 if (Ty
&& Ty
->isMetadataTy())
557 return MetadataAsValue::get(Ty
->getContext(), getFnMetadataByID(ID
));
558 return ValueList
.getValueFwdRef(ID
, Ty
);
561 Metadata
*getFnMetadataByID(unsigned ID
) {
562 return MDLoader
->getMetadataFwdRefOrLoad(ID
);
565 BasicBlock
*getBasicBlock(unsigned ID
) const {
566 if (ID
>= FunctionBBs
.size()) return nullptr; // Invalid ID
567 return FunctionBBs
[ID
];
570 AttributeList
getAttributes(unsigned i
) const {
571 if (i
-1 < MAttributes
.size())
572 return MAttributes
[i
-1];
573 return AttributeList();
576 /// Read a value/type pair out of the specified record from slot 'Slot'.
577 /// Increment Slot past the number of slots used in the record. Return true on
579 bool getValueTypePair(SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
580 unsigned InstNum
, Value
*&ResVal
) {
581 if (Slot
== Record
.size()) return true;
582 unsigned ValNo
= (unsigned)Record
[Slot
++];
583 // Adjust the ValNo, if it was encoded relative to the InstNum.
585 ValNo
= InstNum
- ValNo
;
586 if (ValNo
< InstNum
) {
587 // If this is not a forward reference, just return the value we already
589 ResVal
= getFnValueByID(ValNo
, nullptr);
590 return ResVal
== nullptr;
592 if (Slot
== Record
.size())
595 unsigned TypeNo
= (unsigned)Record
[Slot
++];
596 ResVal
= getFnValueByID(ValNo
, getTypeByID(TypeNo
));
597 return ResVal
== nullptr;
600 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
601 /// past the number of slots used by the value in the record. Return true if
602 /// there is an error.
603 bool popValue(SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
604 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
605 if (getValue(Record
, Slot
, InstNum
, Ty
, ResVal
))
607 // All values currently take a single record slot.
612 /// Like popValue, but does not increment the Slot number.
613 bool getValue(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
614 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
615 ResVal
= getValue(Record
, Slot
, InstNum
, Ty
);
616 return ResVal
== nullptr;
619 /// Version of getValue that returns ResVal directly, or 0 if there is an
621 Value
*getValue(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
622 unsigned InstNum
, Type
*Ty
) {
623 if (Slot
== Record
.size()) return nullptr;
624 unsigned ValNo
= (unsigned)Record
[Slot
];
625 // Adjust the ValNo, if it was encoded relative to the InstNum.
627 ValNo
= InstNum
- ValNo
;
628 return getFnValueByID(ValNo
, Ty
);
631 /// Like getValue, but decodes signed VBRs.
632 Value
*getValueSigned(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
633 unsigned InstNum
, Type
*Ty
) {
634 if (Slot
== Record
.size()) return nullptr;
635 unsigned ValNo
= (unsigned)decodeSignRotatedValue(Record
[Slot
]);
636 // Adjust the ValNo, if it was encoded relative to the InstNum.
638 ValNo
= InstNum
- ValNo
;
639 return getFnValueByID(ValNo
, Ty
);
642 /// Converts alignment exponent (i.e. power of two (or zero)) to the
643 /// corresponding alignment to use. If alignment is too large, returns
644 /// a corresponding error code.
645 Error
parseAlignmentValue(uint64_t Exponent
, unsigned &Alignment
);
646 Error
parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
);
647 Error
parseModule(uint64_t ResumeBit
, bool ShouldLazyLoadMetadata
= false);
649 Error
parseComdatRecord(ArrayRef
<uint64_t> Record
);
650 Error
parseGlobalVarRecord(ArrayRef
<uint64_t> Record
);
651 Error
parseFunctionRecord(ArrayRef
<uint64_t> Record
);
652 Error
parseGlobalIndirectSymbolRecord(unsigned BitCode
,
653 ArrayRef
<uint64_t> Record
);
655 Error
parseAttributeBlock();
656 Error
parseAttributeGroupBlock();
657 Error
parseTypeTable();
658 Error
parseTypeTableBody();
659 Error
parseOperandBundleTags();
660 Error
parseSyncScopeNames();
662 Expected
<Value
*> recordValue(SmallVectorImpl
<uint64_t> &Record
,
663 unsigned NameIndex
, Triple
&TT
);
664 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
, Function
*F
,
665 ArrayRef
<uint64_t> Record
);
666 Error
parseValueSymbolTable(uint64_t Offset
= 0);
667 Error
parseGlobalValueSymbolTable();
668 Error
parseConstants();
669 Error
rememberAndSkipFunctionBodies();
670 Error
rememberAndSkipFunctionBody();
671 /// Save the positions of the Metadata blocks and skip parsing the blocks.
672 Error
rememberAndSkipMetadata();
673 Error
typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
);
674 Error
parseFunctionBody(Function
*F
);
675 Error
globalCleanup();
676 Error
resolveGlobalAndIndirectSymbolInits();
677 Error
parseUseLists();
678 Error
findFunctionInStream(
680 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
);
682 SyncScope::ID
getDecodedSyncScopeID(unsigned Val
);
685 /// Class to manage reading and parsing function summary index bitcode
687 class ModuleSummaryIndexBitcodeReader
: public BitcodeReaderBase
{
688 /// The module index built during parsing.
689 ModuleSummaryIndex
&TheIndex
;
691 /// Indicates whether we have encountered a global value summary section
692 /// yet during parsing.
693 bool SeenGlobalValSummary
= false;
695 /// Indicates whether we have already parsed the VST, used for error checking.
696 bool SeenValueSymbolTable
= false;
698 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
699 /// Used to enable on-demand parsing of the VST.
700 uint64_t VSTOffset
= 0;
702 // Map to save ValueId to ValueInfo association that was recorded in the
703 // ValueSymbolTable. It is used after the VST is parsed to convert
704 // call graph edges read from the function summary from referencing
705 // callees by their ValueId to using the ValueInfo instead, which is how
706 // they are recorded in the summary index being built.
707 // We save a GUID which refers to the same global as the ValueInfo, but
708 // ignoring the linkage, i.e. for values other than local linkage they are
710 DenseMap
<unsigned, std::pair
<ValueInfo
, GlobalValue::GUID
>>
711 ValueIdToValueInfoMap
;
713 /// Map populated during module path string table parsing, from the
714 /// module ID to a string reference owned by the index's module
715 /// path string table, used to correlate with combined index
717 DenseMap
<uint64_t, StringRef
> ModuleIdMap
;
719 /// Original source file name recorded in a bitcode record.
720 std::string SourceFileName
;
722 /// The string identifier given to this module by the client, normally the
723 /// path to the bitcode file.
724 StringRef ModulePath
;
726 /// For per-module summary indexes, the unique numerical identifier given to
727 /// this module by the client.
731 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
732 ModuleSummaryIndex
&TheIndex
,
733 StringRef ModulePath
, unsigned ModuleId
);
738 void setValueGUID(uint64_t ValueID
, StringRef ValueName
,
739 GlobalValue::LinkageTypes Linkage
,
740 StringRef SourceFileName
);
741 Error
parseValueSymbolTable(
743 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
);
744 std::vector
<ValueInfo
> makeRefList(ArrayRef
<uint64_t> Record
);
745 std::vector
<FunctionSummary::EdgeTy
> makeCallList(ArrayRef
<uint64_t> Record
,
746 bool IsOldProfileFormat
,
749 Error
parseEntireSummary(unsigned ID
);
750 Error
parseModuleStringTable();
752 std::pair
<ValueInfo
, GlobalValue::GUID
>
753 getValueInfoFromValueId(unsigned ValueId
);
755 void addThisModule();
756 ModuleSummaryIndex::ModuleInfo
*getThisModule();
759 } // end anonymous namespace
761 std::error_code
llvm::errorToErrorCodeAndEmitErrors(LLVMContext
&Ctx
,
765 handleAllErrors(std::move(Err
), [&](ErrorInfoBase
&EIB
) {
766 EC
= EIB
.convertToErrorCode();
767 Ctx
.emitError(EIB
.message());
771 return std::error_code();
774 BitcodeReader::BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
775 StringRef ProducerIdentification
,
776 LLVMContext
&Context
)
777 : BitcodeReaderBase(std::move(Stream
), Strtab
), Context(Context
),
779 this->ProducerIdentification
= ProducerIdentification
;
782 Error
BitcodeReader::materializeForwardReferencedFunctions() {
783 if (WillMaterializeAllForwardRefs
)
784 return Error::success();
786 // Prevent recursion.
787 WillMaterializeAllForwardRefs
= true;
789 while (!BasicBlockFwdRefQueue
.empty()) {
790 Function
*F
= BasicBlockFwdRefQueue
.front();
791 BasicBlockFwdRefQueue
.pop_front();
792 assert(F
&& "Expected valid function");
793 if (!BasicBlockFwdRefs
.count(F
))
794 // Already materialized.
797 // Check for a function that isn't materializable to prevent an infinite
798 // loop. When parsing a blockaddress stored in a global variable, there
799 // isn't a trivial way to check if a function will have a body without a
800 // linear search through FunctionsWithBodies, so just check it here.
801 if (!F
->isMaterializable())
802 return error("Never resolved function from blockaddress");
804 // Try to materialize F.
805 if (Error Err
= materialize(F
))
808 assert(BasicBlockFwdRefs
.empty() && "Function missing from queue");
811 WillMaterializeAllForwardRefs
= false;
812 return Error::success();
815 //===----------------------------------------------------------------------===//
816 // Helper functions to implement forward reference resolution, etc.
817 //===----------------------------------------------------------------------===//
819 static bool hasImplicitComdat(size_t Val
) {
823 case 1: // Old WeakAnyLinkage
824 case 4: // Old LinkOnceAnyLinkage
825 case 10: // Old WeakODRLinkage
826 case 11: // Old LinkOnceODRLinkage
831 static GlobalValue::LinkageTypes
getDecodedLinkage(unsigned Val
) {
833 default: // Map unknown/new linkages to external
835 return GlobalValue::ExternalLinkage
;
837 return GlobalValue::AppendingLinkage
;
839 return GlobalValue::InternalLinkage
;
841 return GlobalValue::ExternalLinkage
; // Obsolete DLLImportLinkage
843 return GlobalValue::ExternalLinkage
; // Obsolete DLLExportLinkage
845 return GlobalValue::ExternalWeakLinkage
;
847 return GlobalValue::CommonLinkage
;
849 return GlobalValue::PrivateLinkage
;
851 return GlobalValue::AvailableExternallyLinkage
;
853 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateLinkage
855 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateWeakLinkage
857 return GlobalValue::ExternalLinkage
; // Obsolete LinkOnceODRAutoHideLinkage
858 case 1: // Old value with implicit comdat.
860 return GlobalValue::WeakAnyLinkage
;
861 case 10: // Old value with implicit comdat.
863 return GlobalValue::WeakODRLinkage
;
864 case 4: // Old value with implicit comdat.
866 return GlobalValue::LinkOnceAnyLinkage
;
867 case 11: // Old value with implicit comdat.
869 return GlobalValue::LinkOnceODRLinkage
;
873 static FunctionSummary::FFlags
getDecodedFFlags(uint64_t RawFlags
) {
874 FunctionSummary::FFlags Flags
;
875 Flags
.ReadNone
= RawFlags
& 0x1;
876 Flags
.ReadOnly
= (RawFlags
>> 1) & 0x1;
877 Flags
.NoRecurse
= (RawFlags
>> 2) & 0x1;
878 Flags
.ReturnDoesNotAlias
= (RawFlags
>> 3) & 0x1;
882 /// Decode the flags for GlobalValue in the summary.
883 static GlobalValueSummary::GVFlags
getDecodedGVSummaryFlags(uint64_t RawFlags
,
885 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
886 // like getDecodedLinkage() above. Any future change to the linkage enum and
887 // to getDecodedLinkage() will need to be taken into account here as above.
888 auto Linkage
= GlobalValue::LinkageTypes(RawFlags
& 0xF); // 4 bits
889 RawFlags
= RawFlags
>> 4;
890 bool NotEligibleToImport
= (RawFlags
& 0x1) || Version
< 3;
891 // The Live flag wasn't introduced until version 3. For dead stripping
892 // to work correctly on earlier versions, we must conservatively treat all
894 bool Live
= (RawFlags
& 0x2) || Version
< 3;
895 bool Local
= (RawFlags
& 0x4);
897 return GlobalValueSummary::GVFlags(Linkage
, NotEligibleToImport
, Live
, Local
);
900 static GlobalValue::VisibilityTypes
getDecodedVisibility(unsigned Val
) {
902 default: // Map unknown visibilities to default.
903 case 0: return GlobalValue::DefaultVisibility
;
904 case 1: return GlobalValue::HiddenVisibility
;
905 case 2: return GlobalValue::ProtectedVisibility
;
909 static GlobalValue::DLLStorageClassTypes
910 getDecodedDLLStorageClass(unsigned Val
) {
912 default: // Map unknown values to default.
913 case 0: return GlobalValue::DefaultStorageClass
;
914 case 1: return GlobalValue::DLLImportStorageClass
;
915 case 2: return GlobalValue::DLLExportStorageClass
;
919 static bool getDecodedDSOLocal(unsigned Val
) {
921 default: // Map unknown values to preemptable.
922 case 0: return false;
927 static GlobalVariable::ThreadLocalMode
getDecodedThreadLocalMode(unsigned Val
) {
929 case 0: return GlobalVariable::NotThreadLocal
;
930 default: // Map unknown non-zero value to general dynamic.
931 case 1: return GlobalVariable::GeneralDynamicTLSModel
;
932 case 2: return GlobalVariable::LocalDynamicTLSModel
;
933 case 3: return GlobalVariable::InitialExecTLSModel
;
934 case 4: return GlobalVariable::LocalExecTLSModel
;
938 static GlobalVariable::UnnamedAddr
getDecodedUnnamedAddrType(unsigned Val
) {
940 default: // Map unknown to UnnamedAddr::None.
941 case 0: return GlobalVariable::UnnamedAddr::None
;
942 case 1: return GlobalVariable::UnnamedAddr::Global
;
943 case 2: return GlobalVariable::UnnamedAddr::Local
;
947 static int getDecodedCastOpcode(unsigned Val
) {
950 case bitc::CAST_TRUNC
: return Instruction::Trunc
;
951 case bitc::CAST_ZEXT
: return Instruction::ZExt
;
952 case bitc::CAST_SEXT
: return Instruction::SExt
;
953 case bitc::CAST_FPTOUI
: return Instruction::FPToUI
;
954 case bitc::CAST_FPTOSI
: return Instruction::FPToSI
;
955 case bitc::CAST_UITOFP
: return Instruction::UIToFP
;
956 case bitc::CAST_SITOFP
: return Instruction::SIToFP
;
957 case bitc::CAST_FPTRUNC
: return Instruction::FPTrunc
;
958 case bitc::CAST_FPEXT
: return Instruction::FPExt
;
959 case bitc::CAST_PTRTOINT
: return Instruction::PtrToInt
;
960 case bitc::CAST_INTTOPTR
: return Instruction::IntToPtr
;
961 case bitc::CAST_BITCAST
: return Instruction::BitCast
;
962 case bitc::CAST_ADDRSPACECAST
: return Instruction::AddrSpaceCast
;
966 static int getDecodedBinaryOpcode(unsigned Val
, Type
*Ty
) {
967 bool IsFP
= Ty
->isFPOrFPVectorTy();
968 // BinOps are only valid for int/fp or vector of int/fp types
969 if (!IsFP
&& !Ty
->isIntOrIntVectorTy())
975 case bitc::BINOP_ADD
:
976 return IsFP
? Instruction::FAdd
: Instruction::Add
;
977 case bitc::BINOP_SUB
:
978 return IsFP
? Instruction::FSub
: Instruction::Sub
;
979 case bitc::BINOP_MUL
:
980 return IsFP
? Instruction::FMul
: Instruction::Mul
;
981 case bitc::BINOP_UDIV
:
982 return IsFP
? -1 : Instruction::UDiv
;
983 case bitc::BINOP_SDIV
:
984 return IsFP
? Instruction::FDiv
: Instruction::SDiv
;
985 case bitc::BINOP_UREM
:
986 return IsFP
? -1 : Instruction::URem
;
987 case bitc::BINOP_SREM
:
988 return IsFP
? Instruction::FRem
: Instruction::SRem
;
989 case bitc::BINOP_SHL
:
990 return IsFP
? -1 : Instruction::Shl
;
991 case bitc::BINOP_LSHR
:
992 return IsFP
? -1 : Instruction::LShr
;
993 case bitc::BINOP_ASHR
:
994 return IsFP
? -1 : Instruction::AShr
;
995 case bitc::BINOP_AND
:
996 return IsFP
? -1 : Instruction::And
;
998 return IsFP
? -1 : Instruction::Or
;
999 case bitc::BINOP_XOR
:
1000 return IsFP
? -1 : Instruction::Xor
;
1004 static AtomicRMWInst::BinOp
getDecodedRMWOperation(unsigned Val
) {
1006 default: return AtomicRMWInst::BAD_BINOP
;
1007 case bitc::RMW_XCHG
: return AtomicRMWInst::Xchg
;
1008 case bitc::RMW_ADD
: return AtomicRMWInst::Add
;
1009 case bitc::RMW_SUB
: return AtomicRMWInst::Sub
;
1010 case bitc::RMW_AND
: return AtomicRMWInst::And
;
1011 case bitc::RMW_NAND
: return AtomicRMWInst::Nand
;
1012 case bitc::RMW_OR
: return AtomicRMWInst::Or
;
1013 case bitc::RMW_XOR
: return AtomicRMWInst::Xor
;
1014 case bitc::RMW_MAX
: return AtomicRMWInst::Max
;
1015 case bitc::RMW_MIN
: return AtomicRMWInst::Min
;
1016 case bitc::RMW_UMAX
: return AtomicRMWInst::UMax
;
1017 case bitc::RMW_UMIN
: return AtomicRMWInst::UMin
;
1021 static AtomicOrdering
getDecodedOrdering(unsigned Val
) {
1023 case bitc::ORDERING_NOTATOMIC
: return AtomicOrdering::NotAtomic
;
1024 case bitc::ORDERING_UNORDERED
: return AtomicOrdering::Unordered
;
1025 case bitc::ORDERING_MONOTONIC
: return AtomicOrdering::Monotonic
;
1026 case bitc::ORDERING_ACQUIRE
: return AtomicOrdering::Acquire
;
1027 case bitc::ORDERING_RELEASE
: return AtomicOrdering::Release
;
1028 case bitc::ORDERING_ACQREL
: return AtomicOrdering::AcquireRelease
;
1029 default: // Map unknown orderings to sequentially-consistent.
1030 case bitc::ORDERING_SEQCST
: return AtomicOrdering::SequentiallyConsistent
;
1034 static Comdat::SelectionKind
getDecodedComdatSelectionKind(unsigned Val
) {
1036 default: // Map unknown selection kinds to any.
1037 case bitc::COMDAT_SELECTION_KIND_ANY
:
1039 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH
:
1040 return Comdat::ExactMatch
;
1041 case bitc::COMDAT_SELECTION_KIND_LARGEST
:
1042 return Comdat::Largest
;
1043 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES
:
1044 return Comdat::NoDuplicates
;
1045 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE
:
1046 return Comdat::SameSize
;
1050 static FastMathFlags
getDecodedFastMathFlags(unsigned Val
) {
1052 if (0 != (Val
& bitc::UnsafeAlgebra
))
1054 if (0 != (Val
& bitc::AllowReassoc
))
1055 FMF
.setAllowReassoc();
1056 if (0 != (Val
& bitc::NoNaNs
))
1058 if (0 != (Val
& bitc::NoInfs
))
1060 if (0 != (Val
& bitc::NoSignedZeros
))
1061 FMF
.setNoSignedZeros();
1062 if (0 != (Val
& bitc::AllowReciprocal
))
1063 FMF
.setAllowReciprocal();
1064 if (0 != (Val
& bitc::AllowContract
))
1065 FMF
.setAllowContract(true);
1066 if (0 != (Val
& bitc::ApproxFunc
))
1067 FMF
.setApproxFunc();
1071 static void upgradeDLLImportExportLinkage(GlobalValue
*GV
, unsigned Val
) {
1073 case 5: GV
->setDLLStorageClass(GlobalValue::DLLImportStorageClass
); break;
1074 case 6: GV
->setDLLStorageClass(GlobalValue::DLLExportStorageClass
); break;
1078 Type
*BitcodeReader::getTypeByID(unsigned ID
) {
1079 // The type table size is always specified correctly.
1080 if (ID
>= TypeList
.size())
1083 if (Type
*Ty
= TypeList
[ID
])
1086 // If we have a forward reference, the only possible case is when it is to a
1087 // named struct. Just create a placeholder for now.
1088 return TypeList
[ID
] = createIdentifiedStructType(Context
);
1091 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
,
1093 auto *Ret
= StructType::create(Context
, Name
);
1094 IdentifiedStructTypes
.push_back(Ret
);
1098 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
) {
1099 auto *Ret
= StructType::create(Context
);
1100 IdentifiedStructTypes
.push_back(Ret
);
1104 //===----------------------------------------------------------------------===//
1105 // Functions for parsing blocks from the bitcode file
1106 //===----------------------------------------------------------------------===//
1108 static uint64_t getRawAttributeMask(Attribute::AttrKind Val
) {
1110 case Attribute::EndAttrKinds
:
1111 llvm_unreachable("Synthetic enumerators which should never get here");
1113 case Attribute::None
: return 0;
1114 case Attribute::ZExt
: return 1 << 0;
1115 case Attribute::SExt
: return 1 << 1;
1116 case Attribute::NoReturn
: return 1 << 2;
1117 case Attribute::InReg
: return 1 << 3;
1118 case Attribute::StructRet
: return 1 << 4;
1119 case Attribute::NoUnwind
: return 1 << 5;
1120 case Attribute::NoAlias
: return 1 << 6;
1121 case Attribute::ByVal
: return 1 << 7;
1122 case Attribute::Nest
: return 1 << 8;
1123 case Attribute::ReadNone
: return 1 << 9;
1124 case Attribute::ReadOnly
: return 1 << 10;
1125 case Attribute::NoInline
: return 1 << 11;
1126 case Attribute::AlwaysInline
: return 1 << 12;
1127 case Attribute::OptimizeForSize
: return 1 << 13;
1128 case Attribute::StackProtect
: return 1 << 14;
1129 case Attribute::StackProtectReq
: return 1 << 15;
1130 case Attribute::Alignment
: return 31 << 16;
1131 case Attribute::NoCapture
: return 1 << 21;
1132 case Attribute::NoRedZone
: return 1 << 22;
1133 case Attribute::NoImplicitFloat
: return 1 << 23;
1134 case Attribute::Naked
: return 1 << 24;
1135 case Attribute::InlineHint
: return 1 << 25;
1136 case Attribute::StackAlignment
: return 7 << 26;
1137 case Attribute::ReturnsTwice
: return 1 << 29;
1138 case Attribute::UWTable
: return 1 << 30;
1139 case Attribute::NonLazyBind
: return 1U << 31;
1140 case Attribute::SanitizeAddress
: return 1ULL << 32;
1141 case Attribute::MinSize
: return 1ULL << 33;
1142 case Attribute::NoDuplicate
: return 1ULL << 34;
1143 case Attribute::StackProtectStrong
: return 1ULL << 35;
1144 case Attribute::SanitizeThread
: return 1ULL << 36;
1145 case Attribute::SanitizeMemory
: return 1ULL << 37;
1146 case Attribute::NoBuiltin
: return 1ULL << 38;
1147 case Attribute::Returned
: return 1ULL << 39;
1148 case Attribute::Cold
: return 1ULL << 40;
1149 case Attribute::Builtin
: return 1ULL << 41;
1150 case Attribute::OptimizeNone
: return 1ULL << 42;
1151 case Attribute::InAlloca
: return 1ULL << 43;
1152 case Attribute::NonNull
: return 1ULL << 44;
1153 case Attribute::JumpTable
: return 1ULL << 45;
1154 case Attribute::Convergent
: return 1ULL << 46;
1155 case Attribute::SafeStack
: return 1ULL << 47;
1156 case Attribute::NoRecurse
: return 1ULL << 48;
1157 case Attribute::InaccessibleMemOnly
: return 1ULL << 49;
1158 case Attribute::InaccessibleMemOrArgMemOnly
: return 1ULL << 50;
1159 case Attribute::SwiftSelf
: return 1ULL << 51;
1160 case Attribute::SwiftError
: return 1ULL << 52;
1161 case Attribute::WriteOnly
: return 1ULL << 53;
1162 case Attribute::Speculatable
: return 1ULL << 54;
1163 case Attribute::StrictFP
: return 1ULL << 55;
1164 case Attribute::SanitizeHWAddress
: return 1ULL << 56;
1165 case Attribute::NoCfCheck
: return 1ULL << 57;
1166 case Attribute::OptForFuzzing
: return 1ULL << 58;
1167 case Attribute::ShadowCallStack
: return 1ULL << 59;
1168 case Attribute::SpeculativeLoadHardening
:
1170 case Attribute::Dereferenceable
:
1171 llvm_unreachable("dereferenceable attribute not supported in raw format");
1173 case Attribute::DereferenceableOrNull
:
1174 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
1177 case Attribute::ArgMemOnly
:
1178 llvm_unreachable("argmemonly attribute not supported in raw format");
1180 case Attribute::AllocSize
:
1181 llvm_unreachable("allocsize not supported in raw format");
1184 llvm_unreachable("Unsupported attribute type");
1187 static void addRawAttributeValue(AttrBuilder
&B
, uint64_t Val
) {
1190 for (Attribute::AttrKind I
= Attribute::None
; I
!= Attribute::EndAttrKinds
;
1191 I
= Attribute::AttrKind(I
+ 1)) {
1192 if (I
== Attribute::Dereferenceable
||
1193 I
== Attribute::DereferenceableOrNull
||
1194 I
== Attribute::ArgMemOnly
||
1195 I
== Attribute::AllocSize
)
1197 if (uint64_t A
= (Val
& getRawAttributeMask(I
))) {
1198 if (I
== Attribute::Alignment
)
1199 B
.addAlignmentAttr(1ULL << ((A
>> 16) - 1));
1200 else if (I
== Attribute::StackAlignment
)
1201 B
.addStackAlignmentAttr(1ULL << ((A
>> 26)-1));
1208 /// This fills an AttrBuilder object with the LLVM attributes that have
1209 /// been decoded from the given integer. This function must stay in sync with
1210 /// 'encodeLLVMAttributesForBitcode'.
1211 static void decodeLLVMAttributesForBitcode(AttrBuilder
&B
,
1212 uint64_t EncodedAttrs
) {
1213 // FIXME: Remove in 4.0.
1215 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1216 // the bits above 31 down by 11 bits.
1217 unsigned Alignment
= (EncodedAttrs
& (0xffffULL
<< 16)) >> 16;
1218 assert((!Alignment
|| isPowerOf2_32(Alignment
)) &&
1219 "Alignment must be a power of two.");
1222 B
.addAlignmentAttr(Alignment
);
1223 addRawAttributeValue(B
, ((EncodedAttrs
& (0xfffffULL
<< 32)) >> 11) |
1224 (EncodedAttrs
& 0xffff));
1227 Error
BitcodeReader::parseAttributeBlock() {
1228 if (Stream
.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID
))
1229 return error("Invalid record");
1231 if (!MAttributes
.empty())
1232 return error("Invalid multiple blocks");
1234 SmallVector
<uint64_t, 64> Record
;
1236 SmallVector
<AttributeList
, 8> Attrs
;
1238 // Read all the records.
1240 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1242 switch (Entry
.Kind
) {
1243 case BitstreamEntry::SubBlock
: // Handled for us already.
1244 case BitstreamEntry::Error
:
1245 return error("Malformed block");
1246 case BitstreamEntry::EndBlock
:
1247 return Error::success();
1248 case BitstreamEntry::Record
:
1249 // The interesting case.
1255 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
1256 default: // Default behavior: ignore.
1258 case bitc::PARAMATTR_CODE_ENTRY_OLD
: // ENTRY: [paramidx0, attr0, ...]
1259 // FIXME: Remove in 4.0.
1260 if (Record
.size() & 1)
1261 return error("Invalid record");
1263 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; i
+= 2) {
1265 decodeLLVMAttributesForBitcode(B
, Record
[i
+1]);
1266 Attrs
.push_back(AttributeList::get(Context
, Record
[i
], B
));
1269 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1272 case bitc::PARAMATTR_CODE_ENTRY
: // ENTRY: [attrgrp0, attrgrp1, ...]
1273 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; ++i
)
1274 Attrs
.push_back(MAttributeGroups
[Record
[i
]]);
1276 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1283 // Returns Attribute::None on unrecognized codes.
1284 static Attribute::AttrKind
getAttrFromCode(uint64_t Code
) {
1287 return Attribute::None
;
1288 case bitc::ATTR_KIND_ALIGNMENT
:
1289 return Attribute::Alignment
;
1290 case bitc::ATTR_KIND_ALWAYS_INLINE
:
1291 return Attribute::AlwaysInline
;
1292 case bitc::ATTR_KIND_ARGMEMONLY
:
1293 return Attribute::ArgMemOnly
;
1294 case bitc::ATTR_KIND_BUILTIN
:
1295 return Attribute::Builtin
;
1296 case bitc::ATTR_KIND_BY_VAL
:
1297 return Attribute::ByVal
;
1298 case bitc::ATTR_KIND_IN_ALLOCA
:
1299 return Attribute::InAlloca
;
1300 case bitc::ATTR_KIND_COLD
:
1301 return Attribute::Cold
;
1302 case bitc::ATTR_KIND_CONVERGENT
:
1303 return Attribute::Convergent
;
1304 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY
:
1305 return Attribute::InaccessibleMemOnly
;
1306 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY
:
1307 return Attribute::InaccessibleMemOrArgMemOnly
;
1308 case bitc::ATTR_KIND_INLINE_HINT
:
1309 return Attribute::InlineHint
;
1310 case bitc::ATTR_KIND_IN_REG
:
1311 return Attribute::InReg
;
1312 case bitc::ATTR_KIND_JUMP_TABLE
:
1313 return Attribute::JumpTable
;
1314 case bitc::ATTR_KIND_MIN_SIZE
:
1315 return Attribute::MinSize
;
1316 case bitc::ATTR_KIND_NAKED
:
1317 return Attribute::Naked
;
1318 case bitc::ATTR_KIND_NEST
:
1319 return Attribute::Nest
;
1320 case bitc::ATTR_KIND_NO_ALIAS
:
1321 return Attribute::NoAlias
;
1322 case bitc::ATTR_KIND_NO_BUILTIN
:
1323 return Attribute::NoBuiltin
;
1324 case bitc::ATTR_KIND_NO_CAPTURE
:
1325 return Attribute::NoCapture
;
1326 case bitc::ATTR_KIND_NO_DUPLICATE
:
1327 return Attribute::NoDuplicate
;
1328 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT
:
1329 return Attribute::NoImplicitFloat
;
1330 case bitc::ATTR_KIND_NO_INLINE
:
1331 return Attribute::NoInline
;
1332 case bitc::ATTR_KIND_NO_RECURSE
:
1333 return Attribute::NoRecurse
;
1334 case bitc::ATTR_KIND_NON_LAZY_BIND
:
1335 return Attribute::NonLazyBind
;
1336 case bitc::ATTR_KIND_NON_NULL
:
1337 return Attribute::NonNull
;
1338 case bitc::ATTR_KIND_DEREFERENCEABLE
:
1339 return Attribute::Dereferenceable
;
1340 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL
:
1341 return Attribute::DereferenceableOrNull
;
1342 case bitc::ATTR_KIND_ALLOC_SIZE
:
1343 return Attribute::AllocSize
;
1344 case bitc::ATTR_KIND_NO_RED_ZONE
:
1345 return Attribute::NoRedZone
;
1346 case bitc::ATTR_KIND_NO_RETURN
:
1347 return Attribute::NoReturn
;
1348 case bitc::ATTR_KIND_NOCF_CHECK
:
1349 return Attribute::NoCfCheck
;
1350 case bitc::ATTR_KIND_NO_UNWIND
:
1351 return Attribute::NoUnwind
;
1352 case bitc::ATTR_KIND_OPT_FOR_FUZZING
:
1353 return Attribute::OptForFuzzing
;
1354 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE
:
1355 return Attribute::OptimizeForSize
;
1356 case bitc::ATTR_KIND_OPTIMIZE_NONE
:
1357 return Attribute::OptimizeNone
;
1358 case bitc::ATTR_KIND_READ_NONE
:
1359 return Attribute::ReadNone
;
1360 case bitc::ATTR_KIND_READ_ONLY
:
1361 return Attribute::ReadOnly
;
1362 case bitc::ATTR_KIND_RETURNED
:
1363 return Attribute::Returned
;
1364 case bitc::ATTR_KIND_RETURNS_TWICE
:
1365 return Attribute::ReturnsTwice
;
1366 case bitc::ATTR_KIND_S_EXT
:
1367 return Attribute::SExt
;
1368 case bitc::ATTR_KIND_SPECULATABLE
:
1369 return Attribute::Speculatable
;
1370 case bitc::ATTR_KIND_STACK_ALIGNMENT
:
1371 return Attribute::StackAlignment
;
1372 case bitc::ATTR_KIND_STACK_PROTECT
:
1373 return Attribute::StackProtect
;
1374 case bitc::ATTR_KIND_STACK_PROTECT_REQ
:
1375 return Attribute::StackProtectReq
;
1376 case bitc::ATTR_KIND_STACK_PROTECT_STRONG
:
1377 return Attribute::StackProtectStrong
;
1378 case bitc::ATTR_KIND_SAFESTACK
:
1379 return Attribute::SafeStack
;
1380 case bitc::ATTR_KIND_SHADOWCALLSTACK
:
1381 return Attribute::ShadowCallStack
;
1382 case bitc::ATTR_KIND_STRICT_FP
:
1383 return Attribute::StrictFP
;
1384 case bitc::ATTR_KIND_STRUCT_RET
:
1385 return Attribute::StructRet
;
1386 case bitc::ATTR_KIND_SANITIZE_ADDRESS
:
1387 return Attribute::SanitizeAddress
;
1388 case bitc::ATTR_KIND_SANITIZE_HWADDRESS
:
1389 return Attribute::SanitizeHWAddress
;
1390 case bitc::ATTR_KIND_SANITIZE_THREAD
:
1391 return Attribute::SanitizeThread
;
1392 case bitc::ATTR_KIND_SANITIZE_MEMORY
:
1393 return Attribute::SanitizeMemory
;
1394 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING
:
1395 return Attribute::SpeculativeLoadHardening
;
1396 case bitc::ATTR_KIND_SWIFT_ERROR
:
1397 return Attribute::SwiftError
;
1398 case bitc::ATTR_KIND_SWIFT_SELF
:
1399 return Attribute::SwiftSelf
;
1400 case bitc::ATTR_KIND_UW_TABLE
:
1401 return Attribute::UWTable
;
1402 case bitc::ATTR_KIND_WRITEONLY
:
1403 return Attribute::WriteOnly
;
1404 case bitc::ATTR_KIND_Z_EXT
:
1405 return Attribute::ZExt
;
1409 Error
BitcodeReader::parseAlignmentValue(uint64_t Exponent
,
1410 unsigned &Alignment
) {
1411 // Note: Alignment in bitcode files is incremented by 1, so that zero
1412 // can be used for default alignment.
1413 if (Exponent
> Value::MaxAlignmentExponent
+ 1)
1414 return error("Invalid alignment value");
1415 Alignment
= (1 << static_cast<unsigned>(Exponent
)) >> 1;
1416 return Error::success();
1419 Error
BitcodeReader::parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
) {
1420 *Kind
= getAttrFromCode(Code
);
1421 if (*Kind
== Attribute::None
)
1422 return error("Unknown attribute kind (" + Twine(Code
) + ")");
1423 return Error::success();
1426 Error
BitcodeReader::parseAttributeGroupBlock() {
1427 if (Stream
.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID
))
1428 return error("Invalid record");
1430 if (!MAttributeGroups
.empty())
1431 return error("Invalid multiple blocks");
1433 SmallVector
<uint64_t, 64> Record
;
1435 // Read all the records.
1437 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1439 switch (Entry
.Kind
) {
1440 case BitstreamEntry::SubBlock
: // Handled for us already.
1441 case BitstreamEntry::Error
:
1442 return error("Malformed block");
1443 case BitstreamEntry::EndBlock
:
1444 return Error::success();
1445 case BitstreamEntry::Record
:
1446 // The interesting case.
1452 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
1453 default: // Default behavior: ignore.
1455 case bitc::PARAMATTR_GRP_CODE_ENTRY
: { // ENTRY: [grpid, idx, a0, a1, ...]
1456 if (Record
.size() < 3)
1457 return error("Invalid record");
1459 uint64_t GrpID
= Record
[0];
1460 uint64_t Idx
= Record
[1]; // Index of the object this attribute refers to.
1463 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1464 if (Record
[i
] == 0) { // Enum attribute
1465 Attribute::AttrKind Kind
;
1466 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1469 B
.addAttribute(Kind
);
1470 } else if (Record
[i
] == 1) { // Integer attribute
1471 Attribute::AttrKind Kind
;
1472 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1474 if (Kind
== Attribute::Alignment
)
1475 B
.addAlignmentAttr(Record
[++i
]);
1476 else if (Kind
== Attribute::StackAlignment
)
1477 B
.addStackAlignmentAttr(Record
[++i
]);
1478 else if (Kind
== Attribute::Dereferenceable
)
1479 B
.addDereferenceableAttr(Record
[++i
]);
1480 else if (Kind
== Attribute::DereferenceableOrNull
)
1481 B
.addDereferenceableOrNullAttr(Record
[++i
]);
1482 else if (Kind
== Attribute::AllocSize
)
1483 B
.addAllocSizeAttrFromRawRepr(Record
[++i
]);
1484 } else { // String attribute
1485 assert((Record
[i
] == 3 || Record
[i
] == 4) &&
1486 "Invalid attribute group entry");
1487 bool HasValue
= (Record
[i
++] == 4);
1488 SmallString
<64> KindStr
;
1489 SmallString
<64> ValStr
;
1491 while (Record
[i
] != 0 && i
!= e
)
1492 KindStr
+= Record
[i
++];
1493 assert(Record
[i
] == 0 && "Kind string not null terminated");
1496 // Has a value associated with it.
1497 ++i
; // Skip the '0' that terminates the "kind" string.
1498 while (Record
[i
] != 0 && i
!= e
)
1499 ValStr
+= Record
[i
++];
1500 assert(Record
[i
] == 0 && "Value string not null terminated");
1503 B
.addAttribute(KindStr
.str(), ValStr
.str());
1507 MAttributeGroups
[GrpID
] = AttributeList::get(Context
, Idx
, B
);
1514 Error
BitcodeReader::parseTypeTable() {
1515 if (Stream
.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW
))
1516 return error("Invalid record");
1518 return parseTypeTableBody();
1521 Error
BitcodeReader::parseTypeTableBody() {
1522 if (!TypeList
.empty())
1523 return error("Invalid multiple blocks");
1525 SmallVector
<uint64_t, 64> Record
;
1526 unsigned NumRecords
= 0;
1528 SmallString
<64> TypeName
;
1530 // Read all the records for this type table.
1532 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1534 switch (Entry
.Kind
) {
1535 case BitstreamEntry::SubBlock
: // Handled for us already.
1536 case BitstreamEntry::Error
:
1537 return error("Malformed block");
1538 case BitstreamEntry::EndBlock
:
1539 if (NumRecords
!= TypeList
.size())
1540 return error("Malformed block");
1541 return Error::success();
1542 case BitstreamEntry::Record
:
1543 // The interesting case.
1549 Type
*ResultTy
= nullptr;
1550 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
1552 return error("Invalid value");
1553 case bitc::TYPE_CODE_NUMENTRY
: // TYPE_CODE_NUMENTRY: [numentries]
1554 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1555 // type list. This allows us to reserve space.
1556 if (Record
.size() < 1)
1557 return error("Invalid record");
1558 TypeList
.resize(Record
[0]);
1560 case bitc::TYPE_CODE_VOID
: // VOID
1561 ResultTy
= Type::getVoidTy(Context
);
1563 case bitc::TYPE_CODE_HALF
: // HALF
1564 ResultTy
= Type::getHalfTy(Context
);
1566 case bitc::TYPE_CODE_FLOAT
: // FLOAT
1567 ResultTy
= Type::getFloatTy(Context
);
1569 case bitc::TYPE_CODE_DOUBLE
: // DOUBLE
1570 ResultTy
= Type::getDoubleTy(Context
);
1572 case bitc::TYPE_CODE_X86_FP80
: // X86_FP80
1573 ResultTy
= Type::getX86_FP80Ty(Context
);
1575 case bitc::TYPE_CODE_FP128
: // FP128
1576 ResultTy
= Type::getFP128Ty(Context
);
1578 case bitc::TYPE_CODE_PPC_FP128
: // PPC_FP128
1579 ResultTy
= Type::getPPC_FP128Ty(Context
);
1581 case bitc::TYPE_CODE_LABEL
: // LABEL
1582 ResultTy
= Type::getLabelTy(Context
);
1584 case bitc::TYPE_CODE_METADATA
: // METADATA
1585 ResultTy
= Type::getMetadataTy(Context
);
1587 case bitc::TYPE_CODE_X86_MMX
: // X86_MMX
1588 ResultTy
= Type::getX86_MMXTy(Context
);
1590 case bitc::TYPE_CODE_TOKEN
: // TOKEN
1591 ResultTy
= Type::getTokenTy(Context
);
1593 case bitc::TYPE_CODE_INTEGER
: { // INTEGER: [width]
1594 if (Record
.size() < 1)
1595 return error("Invalid record");
1597 uint64_t NumBits
= Record
[0];
1598 if (NumBits
< IntegerType::MIN_INT_BITS
||
1599 NumBits
> IntegerType::MAX_INT_BITS
)
1600 return error("Bitwidth for integer type out of range");
1601 ResultTy
= IntegerType::get(Context
, NumBits
);
1604 case bitc::TYPE_CODE_POINTER
: { // POINTER: [pointee type] or
1605 // [pointee type, address space]
1606 if (Record
.size() < 1)
1607 return error("Invalid record");
1608 unsigned AddressSpace
= 0;
1609 if (Record
.size() == 2)
1610 AddressSpace
= Record
[1];
1611 ResultTy
= getTypeByID(Record
[0]);
1613 !PointerType::isValidElementType(ResultTy
))
1614 return error("Invalid type");
1615 ResultTy
= PointerType::get(ResultTy
, AddressSpace
);
1618 case bitc::TYPE_CODE_FUNCTION_OLD
: {
1619 // FIXME: attrid is dead, remove it in LLVM 4.0
1620 // FUNCTION: [vararg, attrid, retty, paramty x N]
1621 if (Record
.size() < 3)
1622 return error("Invalid record");
1623 SmallVector
<Type
*, 8> ArgTys
;
1624 for (unsigned i
= 3, e
= Record
.size(); i
!= e
; ++i
) {
1625 if (Type
*T
= getTypeByID(Record
[i
]))
1626 ArgTys
.push_back(T
);
1631 ResultTy
= getTypeByID(Record
[2]);
1632 if (!ResultTy
|| ArgTys
.size() < Record
.size()-3)
1633 return error("Invalid type");
1635 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1638 case bitc::TYPE_CODE_FUNCTION
: {
1639 // FUNCTION: [vararg, retty, paramty x N]
1640 if (Record
.size() < 2)
1641 return error("Invalid record");
1642 SmallVector
<Type
*, 8> ArgTys
;
1643 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1644 if (Type
*T
= getTypeByID(Record
[i
])) {
1645 if (!FunctionType::isValidArgumentType(T
))
1646 return error("Invalid function argument type");
1647 ArgTys
.push_back(T
);
1653 ResultTy
= getTypeByID(Record
[1]);
1654 if (!ResultTy
|| ArgTys
.size() < Record
.size()-2)
1655 return error("Invalid type");
1657 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1660 case bitc::TYPE_CODE_STRUCT_ANON
: { // STRUCT: [ispacked, eltty x N]
1661 if (Record
.size() < 1)
1662 return error("Invalid record");
1663 SmallVector
<Type
*, 8> EltTys
;
1664 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1665 if (Type
*T
= getTypeByID(Record
[i
]))
1666 EltTys
.push_back(T
);
1670 if (EltTys
.size() != Record
.size()-1)
1671 return error("Invalid type");
1672 ResultTy
= StructType::get(Context
, EltTys
, Record
[0]);
1675 case bitc::TYPE_CODE_STRUCT_NAME
: // STRUCT_NAME: [strchr x N]
1676 if (convertToString(Record
, 0, TypeName
))
1677 return error("Invalid record");
1680 case bitc::TYPE_CODE_STRUCT_NAMED
: { // STRUCT: [ispacked, eltty x N]
1681 if (Record
.size() < 1)
1682 return error("Invalid record");
1684 if (NumRecords
>= TypeList
.size())
1685 return error("Invalid TYPE table");
1687 // Check to see if this was forward referenced, if so fill in the temp.
1688 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1690 Res
->setName(TypeName
);
1691 TypeList
[NumRecords
] = nullptr;
1692 } else // Otherwise, create a new struct.
1693 Res
= createIdentifiedStructType(Context
, TypeName
);
1696 SmallVector
<Type
*, 8> EltTys
;
1697 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1698 if (Type
*T
= getTypeByID(Record
[i
]))
1699 EltTys
.push_back(T
);
1703 if (EltTys
.size() != Record
.size()-1)
1704 return error("Invalid record");
1705 Res
->setBody(EltTys
, Record
[0]);
1709 case bitc::TYPE_CODE_OPAQUE
: { // OPAQUE: []
1710 if (Record
.size() != 1)
1711 return error("Invalid record");
1713 if (NumRecords
>= TypeList
.size())
1714 return error("Invalid TYPE table");
1716 // Check to see if this was forward referenced, if so fill in the temp.
1717 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1719 Res
->setName(TypeName
);
1720 TypeList
[NumRecords
] = nullptr;
1721 } else // Otherwise, create a new struct with no body.
1722 Res
= createIdentifiedStructType(Context
, TypeName
);
1727 case bitc::TYPE_CODE_ARRAY
: // ARRAY: [numelts, eltty]
1728 if (Record
.size() < 2)
1729 return error("Invalid record");
1730 ResultTy
= getTypeByID(Record
[1]);
1731 if (!ResultTy
|| !ArrayType::isValidElementType(ResultTy
))
1732 return error("Invalid type");
1733 ResultTy
= ArrayType::get(ResultTy
, Record
[0]);
1735 case bitc::TYPE_CODE_VECTOR
: // VECTOR: [numelts, eltty]
1736 if (Record
.size() < 2)
1737 return error("Invalid record");
1739 return error("Invalid vector length");
1740 ResultTy
= getTypeByID(Record
[1]);
1741 if (!ResultTy
|| !StructType::isValidElementType(ResultTy
))
1742 return error("Invalid type");
1743 ResultTy
= VectorType::get(ResultTy
, Record
[0]);
1747 if (NumRecords
>= TypeList
.size())
1748 return error("Invalid TYPE table");
1749 if (TypeList
[NumRecords
])
1751 "Invalid TYPE table: Only named structs can be forward referenced");
1752 assert(ResultTy
&& "Didn't read a type?");
1753 TypeList
[NumRecords
++] = ResultTy
;
1757 Error
BitcodeReader::parseOperandBundleTags() {
1758 if (Stream
.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
))
1759 return error("Invalid record");
1761 if (!BundleTags
.empty())
1762 return error("Invalid multiple blocks");
1764 SmallVector
<uint64_t, 64> Record
;
1767 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1769 switch (Entry
.Kind
) {
1770 case BitstreamEntry::SubBlock
: // Handled for us already.
1771 case BitstreamEntry::Error
:
1772 return error("Malformed block");
1773 case BitstreamEntry::EndBlock
:
1774 return Error::success();
1775 case BitstreamEntry::Record
:
1776 // The interesting case.
1780 // Tags are implicitly mapped to integers by their order.
1782 if (Stream
.readRecord(Entry
.ID
, Record
) != bitc::OPERAND_BUNDLE_TAG
)
1783 return error("Invalid record");
1785 // OPERAND_BUNDLE_TAG: [strchr x N]
1786 BundleTags
.emplace_back();
1787 if (convertToString(Record
, 0, BundleTags
.back()))
1788 return error("Invalid record");
1793 Error
BitcodeReader::parseSyncScopeNames() {
1794 if (Stream
.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID
))
1795 return error("Invalid record");
1798 return error("Invalid multiple synchronization scope names blocks");
1800 SmallVector
<uint64_t, 64> Record
;
1802 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1803 switch (Entry
.Kind
) {
1804 case BitstreamEntry::SubBlock
: // Handled for us already.
1805 case BitstreamEntry::Error
:
1806 return error("Malformed block");
1807 case BitstreamEntry::EndBlock
:
1809 return error("Invalid empty synchronization scope names block");
1810 return Error::success();
1811 case BitstreamEntry::Record
:
1812 // The interesting case.
1816 // Synchronization scope names are implicitly mapped to synchronization
1817 // scope IDs by their order.
1819 if (Stream
.readRecord(Entry
.ID
, Record
) != bitc::SYNC_SCOPE_NAME
)
1820 return error("Invalid record");
1822 SmallString
<16> SSN
;
1823 if (convertToString(Record
, 0, SSN
))
1824 return error("Invalid record");
1826 SSIDs
.push_back(Context
.getOrInsertSyncScopeID(SSN
));
1831 /// Associate a value with its name from the given index in the provided record.
1832 Expected
<Value
*> BitcodeReader::recordValue(SmallVectorImpl
<uint64_t> &Record
,
1833 unsigned NameIndex
, Triple
&TT
) {
1834 SmallString
<128> ValueName
;
1835 if (convertToString(Record
, NameIndex
, ValueName
))
1836 return error("Invalid record");
1837 unsigned ValueID
= Record
[0];
1838 if (ValueID
>= ValueList
.size() || !ValueList
[ValueID
])
1839 return error("Invalid record");
1840 Value
*V
= ValueList
[ValueID
];
1842 StringRef
NameStr(ValueName
.data(), ValueName
.size());
1843 if (NameStr
.find_first_of(0) != StringRef::npos
)
1844 return error("Invalid value name");
1845 V
->setName(NameStr
);
1846 auto *GO
= dyn_cast
<GlobalObject
>(V
);
1848 if (GO
->getComdat() == reinterpret_cast<Comdat
*>(1)) {
1849 if (TT
.supportsCOMDAT())
1850 GO
->setComdat(TheModule
->getOrInsertComdat(V
->getName()));
1852 GO
->setComdat(nullptr);
1858 /// Helper to note and return the current location, and jump to the given
1860 static uint64_t jumpToValueSymbolTable(uint64_t Offset
,
1861 BitstreamCursor
&Stream
) {
1862 // Save the current parsing location so we can jump back at the end
1864 uint64_t CurrentBit
= Stream
.GetCurrentBitNo();
1865 Stream
.JumpToBit(Offset
* 32);
1867 // Do some checking if we are in debug mode.
1868 BitstreamEntry Entry
= Stream
.advance();
1869 assert(Entry
.Kind
== BitstreamEntry::SubBlock
);
1870 assert(Entry
.ID
== bitc::VALUE_SYMTAB_BLOCK_ID
);
1872 // In NDEBUG mode ignore the output so we don't get an unused variable
1879 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
,
1881 ArrayRef
<uint64_t> Record
) {
1882 // Note that we subtract 1 here because the offset is relative to one word
1883 // before the start of the identification or module block, which was
1884 // historically always the start of the regular bitcode header.
1885 uint64_t FuncWordOffset
= Record
[1] - 1;
1886 uint64_t FuncBitOffset
= FuncWordOffset
* 32;
1887 DeferredFunctionInfo
[F
] = FuncBitOffset
+ FuncBitcodeOffsetDelta
;
1888 // Set the LastFunctionBlockBit to point to the last function block.
1889 // Later when parsing is resumed after function materialization,
1890 // we can simply skip that last function block.
1891 if (FuncBitOffset
> LastFunctionBlockBit
)
1892 LastFunctionBlockBit
= FuncBitOffset
;
1895 /// Read a new-style GlobalValue symbol table.
1896 Error
BitcodeReader::parseGlobalValueSymbolTable() {
1897 unsigned FuncBitcodeOffsetDelta
=
1898 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
1900 if (Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
1901 return error("Invalid record");
1903 SmallVector
<uint64_t, 64> Record
;
1905 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1907 switch (Entry
.Kind
) {
1908 case BitstreamEntry::SubBlock
:
1909 case BitstreamEntry::Error
:
1910 return error("Malformed block");
1911 case BitstreamEntry::EndBlock
:
1912 return Error::success();
1913 case BitstreamEntry::Record
:
1918 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
1919 case bitc::VST_CODE_FNENTRY
: // [valueid, offset]
1920 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
,
1921 cast
<Function
>(ValueList
[Record
[0]]), Record
);
1927 /// Parse the value symbol table at either the current parsing location or
1928 /// at the given bit offset if provided.
1929 Error
BitcodeReader::parseValueSymbolTable(uint64_t Offset
) {
1930 uint64_t CurrentBit
;
1931 // Pass in the Offset to distinguish between calling for the module-level
1932 // VST (where we want to jump to the VST offset) and the function-level
1933 // VST (where we don't).
1935 CurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
1936 // If this module uses a string table, read this as a module-level VST.
1938 if (Error Err
= parseGlobalValueSymbolTable())
1940 Stream
.JumpToBit(CurrentBit
);
1941 return Error::success();
1943 // Otherwise, the VST will be in a similar format to a function-level VST,
1944 // and will contain symbol names.
1947 // Compute the delta between the bitcode indices in the VST (the word offset
1948 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1949 // expected by the lazy reader. The reader's EnterSubBlock expects to have
1950 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1951 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1952 // just before entering the VST subblock because: 1) the EnterSubBlock
1953 // changes the AbbrevID width; 2) the VST block is nested within the same
1954 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1955 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1956 // jump to the FUNCTION_BLOCK using this offset later, we don't want
1957 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1958 unsigned FuncBitcodeOffsetDelta
=
1959 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
1961 if (Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
1962 return error("Invalid record");
1964 SmallVector
<uint64_t, 64> Record
;
1966 Triple
TT(TheModule
->getTargetTriple());
1968 // Read all the records for this value table.
1969 SmallString
<128> ValueName
;
1972 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
1974 switch (Entry
.Kind
) {
1975 case BitstreamEntry::SubBlock
: // Handled for us already.
1976 case BitstreamEntry::Error
:
1977 return error("Malformed block");
1978 case BitstreamEntry::EndBlock
:
1980 Stream
.JumpToBit(CurrentBit
);
1981 return Error::success();
1982 case BitstreamEntry::Record
:
1983 // The interesting case.
1989 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
1990 default: // Default behavior: unknown type.
1992 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
1993 Expected
<Value
*> ValOrErr
= recordValue(Record
, 1, TT
);
1994 if (Error Err
= ValOrErr
.takeError())
1999 case bitc::VST_CODE_FNENTRY
: {
2000 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2001 Expected
<Value
*> ValOrErr
= recordValue(Record
, 2, TT
);
2002 if (Error Err
= ValOrErr
.takeError())
2004 Value
*V
= ValOrErr
.get();
2006 // Ignore function offsets emitted for aliases of functions in older
2007 // versions of LLVM.
2008 if (auto *F
= dyn_cast
<Function
>(V
))
2009 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
, F
, Record
);
2012 case bitc::VST_CODE_BBENTRY
: {
2013 if (convertToString(Record
, 1, ValueName
))
2014 return error("Invalid record");
2015 BasicBlock
*BB
= getBasicBlock(Record
[0]);
2017 return error("Invalid record");
2019 BB
->setName(StringRef(ValueName
.data(), ValueName
.size()));
2027 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2029 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V
) {
2034 // There is no such thing as -0 with integers. "-0" really means MININT.
2038 /// Resolve all of the initializers for global values and aliases that we can.
2039 Error
BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2040 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInitWorklist
;
2041 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>
2042 IndirectSymbolInitWorklist
;
2043 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixWorklist
;
2044 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologueWorklist
;
2045 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFnWorklist
;
2047 GlobalInitWorklist
.swap(GlobalInits
);
2048 IndirectSymbolInitWorklist
.swap(IndirectSymbolInits
);
2049 FunctionPrefixWorklist
.swap(FunctionPrefixes
);
2050 FunctionPrologueWorklist
.swap(FunctionPrologues
);
2051 FunctionPersonalityFnWorklist
.swap(FunctionPersonalityFns
);
2053 while (!GlobalInitWorklist
.empty()) {
2054 unsigned ValID
= GlobalInitWorklist
.back().second
;
2055 if (ValID
>= ValueList
.size()) {
2056 // Not ready to resolve this yet, it requires something later in the file.
2057 GlobalInits
.push_back(GlobalInitWorklist
.back());
2059 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2060 GlobalInitWorklist
.back().first
->setInitializer(C
);
2062 return error("Expected a constant");
2064 GlobalInitWorklist
.pop_back();
2067 while (!IndirectSymbolInitWorklist
.empty()) {
2068 unsigned ValID
= IndirectSymbolInitWorklist
.back().second
;
2069 if (ValID
>= ValueList
.size()) {
2070 IndirectSymbolInits
.push_back(IndirectSymbolInitWorklist
.back());
2072 Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]);
2074 return error("Expected a constant");
2075 GlobalIndirectSymbol
*GIS
= IndirectSymbolInitWorklist
.back().first
;
2076 if (isa
<GlobalAlias
>(GIS
) && C
->getType() != GIS
->getType())
2077 return error("Alias and aliasee types don't match");
2078 GIS
->setIndirectSymbol(C
);
2080 IndirectSymbolInitWorklist
.pop_back();
2083 while (!FunctionPrefixWorklist
.empty()) {
2084 unsigned ValID
= FunctionPrefixWorklist
.back().second
;
2085 if (ValID
>= ValueList
.size()) {
2086 FunctionPrefixes
.push_back(FunctionPrefixWorklist
.back());
2088 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2089 FunctionPrefixWorklist
.back().first
->setPrefixData(C
);
2091 return error("Expected a constant");
2093 FunctionPrefixWorklist
.pop_back();
2096 while (!FunctionPrologueWorklist
.empty()) {
2097 unsigned ValID
= FunctionPrologueWorklist
.back().second
;
2098 if (ValID
>= ValueList
.size()) {
2099 FunctionPrologues
.push_back(FunctionPrologueWorklist
.back());
2101 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2102 FunctionPrologueWorklist
.back().first
->setPrologueData(C
);
2104 return error("Expected a constant");
2106 FunctionPrologueWorklist
.pop_back();
2109 while (!FunctionPersonalityFnWorklist
.empty()) {
2110 unsigned ValID
= FunctionPersonalityFnWorklist
.back().second
;
2111 if (ValID
>= ValueList
.size()) {
2112 FunctionPersonalityFns
.push_back(FunctionPersonalityFnWorklist
.back());
2114 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2115 FunctionPersonalityFnWorklist
.back().first
->setPersonalityFn(C
);
2117 return error("Expected a constant");
2119 FunctionPersonalityFnWorklist
.pop_back();
2122 return Error::success();
2125 static APInt
readWideAPInt(ArrayRef
<uint64_t> Vals
, unsigned TypeBits
) {
2126 SmallVector
<uint64_t, 8> Words(Vals
.size());
2127 transform(Vals
, Words
.begin(),
2128 BitcodeReader::decodeSignRotatedValue
);
2130 return APInt(TypeBits
, Words
);
2133 Error
BitcodeReader::parseConstants() {
2134 if (Stream
.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID
))
2135 return error("Invalid record");
2137 SmallVector
<uint64_t, 64> Record
;
2139 // Read all the records for this value table.
2140 Type
*CurTy
= Type::getInt32Ty(Context
);
2141 unsigned NextCstNo
= ValueList
.size();
2144 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
2146 switch (Entry
.Kind
) {
2147 case BitstreamEntry::SubBlock
: // Handled for us already.
2148 case BitstreamEntry::Error
:
2149 return error("Malformed block");
2150 case BitstreamEntry::EndBlock
:
2151 if (NextCstNo
!= ValueList
.size())
2152 return error("Invalid constant reference");
2154 // Once all the constants have been read, go through and resolve forward
2156 ValueList
.resolveConstantForwardRefs();
2157 return Error::success();
2158 case BitstreamEntry::Record
:
2159 // The interesting case.
2165 Type
*VoidType
= Type::getVoidTy(Context
);
2167 unsigned BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
2169 default: // Default behavior: unknown constant
2170 case bitc::CST_CODE_UNDEF
: // UNDEF
2171 V
= UndefValue::get(CurTy
);
2173 case bitc::CST_CODE_SETTYPE
: // SETTYPE: [typeid]
2175 return error("Invalid record");
2176 if (Record
[0] >= TypeList
.size() || !TypeList
[Record
[0]])
2177 return error("Invalid record");
2178 if (TypeList
[Record
[0]] == VoidType
)
2179 return error("Invalid constant type");
2180 CurTy
= TypeList
[Record
[0]];
2181 continue; // Skip the ValueList manipulation.
2182 case bitc::CST_CODE_NULL
: // NULL
2183 V
= Constant::getNullValue(CurTy
);
2185 case bitc::CST_CODE_INTEGER
: // INTEGER: [intval]
2186 if (!CurTy
->isIntegerTy() || Record
.empty())
2187 return error("Invalid record");
2188 V
= ConstantInt::get(CurTy
, decodeSignRotatedValue(Record
[0]));
2190 case bitc::CST_CODE_WIDE_INTEGER
: {// WIDE_INTEGER: [n x intval]
2191 if (!CurTy
->isIntegerTy() || Record
.empty())
2192 return error("Invalid record");
2195 readWideAPInt(Record
, cast
<IntegerType
>(CurTy
)->getBitWidth());
2196 V
= ConstantInt::get(Context
, VInt
);
2200 case bitc::CST_CODE_FLOAT
: { // FLOAT: [fpval]
2202 return error("Invalid record");
2203 if (CurTy
->isHalfTy())
2204 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEhalf(),
2205 APInt(16, (uint16_t)Record
[0])));
2206 else if (CurTy
->isFloatTy())
2207 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEsingle(),
2208 APInt(32, (uint32_t)Record
[0])));
2209 else if (CurTy
->isDoubleTy())
2210 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEdouble(),
2211 APInt(64, Record
[0])));
2212 else if (CurTy
->isX86_FP80Ty()) {
2213 // Bits are not stored the same way as a normal i80 APInt, compensate.
2214 uint64_t Rearrange
[2];
2215 Rearrange
[0] = (Record
[1] & 0xffffLL
) | (Record
[0] << 16);
2216 Rearrange
[1] = Record
[0] >> 48;
2217 V
= ConstantFP::get(Context
, APFloat(APFloat::x87DoubleExtended(),
2218 APInt(80, Rearrange
)));
2219 } else if (CurTy
->isFP128Ty())
2220 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEquad(),
2221 APInt(128, Record
)));
2222 else if (CurTy
->isPPC_FP128Ty())
2223 V
= ConstantFP::get(Context
, APFloat(APFloat::PPCDoubleDouble(),
2224 APInt(128, Record
)));
2226 V
= UndefValue::get(CurTy
);
2230 case bitc::CST_CODE_AGGREGATE
: {// AGGREGATE: [n x value number]
2232 return error("Invalid record");
2234 unsigned Size
= Record
.size();
2235 SmallVector
<Constant
*, 16> Elts
;
2237 if (StructType
*STy
= dyn_cast
<StructType
>(CurTy
)) {
2238 for (unsigned i
= 0; i
!= Size
; ++i
)
2239 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
],
2240 STy
->getElementType(i
)));
2241 V
= ConstantStruct::get(STy
, Elts
);
2242 } else if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(CurTy
)) {
2243 Type
*EltTy
= ATy
->getElementType();
2244 for (unsigned i
= 0; i
!= Size
; ++i
)
2245 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2246 V
= ConstantArray::get(ATy
, Elts
);
2247 } else if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
)) {
2248 Type
*EltTy
= VTy
->getElementType();
2249 for (unsigned i
= 0; i
!= Size
; ++i
)
2250 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2251 V
= ConstantVector::get(Elts
);
2253 V
= UndefValue::get(CurTy
);
2257 case bitc::CST_CODE_STRING
: // STRING: [values]
2258 case bitc::CST_CODE_CSTRING
: { // CSTRING: [values]
2260 return error("Invalid record");
2262 SmallString
<16> Elts(Record
.begin(), Record
.end());
2263 V
= ConstantDataArray::getString(Context
, Elts
,
2264 BitCode
== bitc::CST_CODE_CSTRING
);
2267 case bitc::CST_CODE_DATA
: {// DATA: [n x value]
2269 return error("Invalid record");
2271 Type
*EltTy
= cast
<SequentialType
>(CurTy
)->getElementType();
2272 if (EltTy
->isIntegerTy(8)) {
2273 SmallVector
<uint8_t, 16> Elts(Record
.begin(), Record
.end());
2274 if (isa
<VectorType
>(CurTy
))
2275 V
= ConstantDataVector::get(Context
, Elts
);
2277 V
= ConstantDataArray::get(Context
, Elts
);
2278 } else if (EltTy
->isIntegerTy(16)) {
2279 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2280 if (isa
<VectorType
>(CurTy
))
2281 V
= ConstantDataVector::get(Context
, Elts
);
2283 V
= ConstantDataArray::get(Context
, Elts
);
2284 } else if (EltTy
->isIntegerTy(32)) {
2285 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2286 if (isa
<VectorType
>(CurTy
))
2287 V
= ConstantDataVector::get(Context
, Elts
);
2289 V
= ConstantDataArray::get(Context
, Elts
);
2290 } else if (EltTy
->isIntegerTy(64)) {
2291 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2292 if (isa
<VectorType
>(CurTy
))
2293 V
= ConstantDataVector::get(Context
, Elts
);
2295 V
= ConstantDataArray::get(Context
, Elts
);
2296 } else if (EltTy
->isHalfTy()) {
2297 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2298 if (isa
<VectorType
>(CurTy
))
2299 V
= ConstantDataVector::getFP(Context
, Elts
);
2301 V
= ConstantDataArray::getFP(Context
, Elts
);
2302 } else if (EltTy
->isFloatTy()) {
2303 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2304 if (isa
<VectorType
>(CurTy
))
2305 V
= ConstantDataVector::getFP(Context
, Elts
);
2307 V
= ConstantDataArray::getFP(Context
, Elts
);
2308 } else if (EltTy
->isDoubleTy()) {
2309 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2310 if (isa
<VectorType
>(CurTy
))
2311 V
= ConstantDataVector::getFP(Context
, Elts
);
2313 V
= ConstantDataArray::getFP(Context
, Elts
);
2315 return error("Invalid type for value");
2319 case bitc::CST_CODE_CE_BINOP
: { // CE_BINOP: [opcode, opval, opval]
2320 if (Record
.size() < 3)
2321 return error("Invalid record");
2322 int Opc
= getDecodedBinaryOpcode(Record
[0], CurTy
);
2324 V
= UndefValue::get(CurTy
); // Unknown binop.
2326 Constant
*LHS
= ValueList
.getConstantFwdRef(Record
[1], CurTy
);
2327 Constant
*RHS
= ValueList
.getConstantFwdRef(Record
[2], CurTy
);
2329 if (Record
.size() >= 4) {
2330 if (Opc
== Instruction::Add
||
2331 Opc
== Instruction::Sub
||
2332 Opc
== Instruction::Mul
||
2333 Opc
== Instruction::Shl
) {
2334 if (Record
[3] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
2335 Flags
|= OverflowingBinaryOperator::NoSignedWrap
;
2336 if (Record
[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
2337 Flags
|= OverflowingBinaryOperator::NoUnsignedWrap
;
2338 } else if (Opc
== Instruction::SDiv
||
2339 Opc
== Instruction::UDiv
||
2340 Opc
== Instruction::LShr
||
2341 Opc
== Instruction::AShr
) {
2342 if (Record
[3] & (1 << bitc::PEO_EXACT
))
2343 Flags
|= SDivOperator::IsExact
;
2346 V
= ConstantExpr::get(Opc
, LHS
, RHS
, Flags
);
2350 case bitc::CST_CODE_CE_CAST
: { // CE_CAST: [opcode, opty, opval]
2351 if (Record
.size() < 3)
2352 return error("Invalid record");
2353 int Opc
= getDecodedCastOpcode(Record
[0]);
2355 V
= UndefValue::get(CurTy
); // Unknown cast.
2357 Type
*OpTy
= getTypeByID(Record
[1]);
2359 return error("Invalid record");
2360 Constant
*Op
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2361 V
= UpgradeBitCastExpr(Opc
, Op
, CurTy
);
2362 if (!V
) V
= ConstantExpr::getCast(Opc
, Op
, CurTy
);
2366 case bitc::CST_CODE_CE_INBOUNDS_GEP
: // [ty, n x operands]
2367 case bitc::CST_CODE_CE_GEP
: // [ty, n x operands]
2368 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
: { // [ty, flags, n x
2371 Type
*PointeeType
= nullptr;
2372 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
||
2374 PointeeType
= getTypeByID(Record
[OpNum
++]);
2376 bool InBounds
= false;
2377 Optional
<unsigned> InRangeIndex
;
2378 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
) {
2379 uint64_t Op
= Record
[OpNum
++];
2381 InRangeIndex
= Op
>> 1;
2382 } else if (BitCode
== bitc::CST_CODE_CE_INBOUNDS_GEP
)
2385 SmallVector
<Constant
*, 16> Elts
;
2386 while (OpNum
!= Record
.size()) {
2387 Type
*ElTy
= getTypeByID(Record
[OpNum
++]);
2389 return error("Invalid record");
2390 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[OpNum
++], ElTy
));
2395 cast
<PointerType
>(Elts
[0]->getType()->getScalarType())
2397 return error("Explicit gep operator type does not match pointee type "
2398 "of pointer operand");
2400 if (Elts
.size() < 1)
2401 return error("Invalid gep with no operands");
2403 ArrayRef
<Constant
*> Indices(Elts
.begin() + 1, Elts
.end());
2404 V
= ConstantExpr::getGetElementPtr(PointeeType
, Elts
[0], Indices
,
2405 InBounds
, InRangeIndex
);
2408 case bitc::CST_CODE_CE_SELECT
: { // CE_SELECT: [opval#, opval#, opval#]
2409 if (Record
.size() < 3)
2410 return error("Invalid record");
2412 Type
*SelectorTy
= Type::getInt1Ty(Context
);
2414 // The selector might be an i1 or an <n x i1>
2415 // Get the type from the ValueList before getting a forward ref.
2416 if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
))
2417 if (Value
*V
= ValueList
[Record
[0]])
2418 if (SelectorTy
!= V
->getType())
2419 SelectorTy
= VectorType::get(SelectorTy
, VTy
->getNumElements());
2421 V
= ConstantExpr::getSelect(ValueList
.getConstantFwdRef(Record
[0],
2423 ValueList
.getConstantFwdRef(Record
[1],CurTy
),
2424 ValueList
.getConstantFwdRef(Record
[2],CurTy
));
2427 case bitc::CST_CODE_CE_EXTRACTELT
2428 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2429 if (Record
.size() < 3)
2430 return error("Invalid record");
2432 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2434 return error("Invalid record");
2435 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2436 Constant
*Op1
= nullptr;
2437 if (Record
.size() == 4) {
2438 Type
*IdxTy
= getTypeByID(Record
[2]);
2440 return error("Invalid record");
2441 Op1
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2442 } else // TODO: Remove with llvm 4.0
2443 Op1
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2445 return error("Invalid record");
2446 V
= ConstantExpr::getExtractElement(Op0
, Op1
);
2449 case bitc::CST_CODE_CE_INSERTELT
2450 : { // CE_INSERTELT: [opval, opval, opty, opval]
2451 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2452 if (Record
.size() < 3 || !OpTy
)
2453 return error("Invalid record");
2454 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[0], OpTy
);
2455 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[1],
2456 OpTy
->getElementType());
2457 Constant
*Op2
= nullptr;
2458 if (Record
.size() == 4) {
2459 Type
*IdxTy
= getTypeByID(Record
[2]);
2461 return error("Invalid record");
2462 Op2
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2463 } else // TODO: Remove with llvm 4.0
2464 Op2
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2466 return error("Invalid record");
2467 V
= ConstantExpr::getInsertElement(Op0
, Op1
, Op2
);
2470 case bitc::CST_CODE_CE_SHUFFLEVEC
: { // CE_SHUFFLEVEC: [opval, opval, opval]
2471 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2472 if (Record
.size() < 3 || !OpTy
)
2473 return error("Invalid record");
2474 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[0], OpTy
);
2475 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2476 Type
*ShufTy
= VectorType::get(Type::getInt32Ty(Context
),
2477 OpTy
->getNumElements());
2478 Constant
*Op2
= ValueList
.getConstantFwdRef(Record
[2], ShufTy
);
2479 V
= ConstantExpr::getShuffleVector(Op0
, Op1
, Op2
);
2482 case bitc::CST_CODE_CE_SHUFVEC_EX
: { // [opty, opval, opval, opval]
2483 VectorType
*RTy
= dyn_cast
<VectorType
>(CurTy
);
2485 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2486 if (Record
.size() < 4 || !RTy
|| !OpTy
)
2487 return error("Invalid record");
2488 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2489 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2490 Type
*ShufTy
= VectorType::get(Type::getInt32Ty(Context
),
2491 RTy
->getNumElements());
2492 Constant
*Op2
= ValueList
.getConstantFwdRef(Record
[3], ShufTy
);
2493 V
= ConstantExpr::getShuffleVector(Op0
, Op1
, Op2
);
2496 case bitc::CST_CODE_CE_CMP
: { // CE_CMP: [opty, opval, opval, pred]
2497 if (Record
.size() < 4)
2498 return error("Invalid record");
2499 Type
*OpTy
= getTypeByID(Record
[0]);
2501 return error("Invalid record");
2502 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2503 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2505 if (OpTy
->isFPOrFPVectorTy())
2506 V
= ConstantExpr::getFCmp(Record
[3], Op0
, Op1
);
2508 V
= ConstantExpr::getICmp(Record
[3], Op0
, Op1
);
2511 // This maintains backward compatibility, pre-asm dialect keywords.
2512 // FIXME: Remove with the 4.0 release.
2513 case bitc::CST_CODE_INLINEASM_OLD
: {
2514 if (Record
.size() < 2)
2515 return error("Invalid record");
2516 std::string AsmStr
, ConstrStr
;
2517 bool HasSideEffects
= Record
[0] & 1;
2518 bool IsAlignStack
= Record
[0] >> 1;
2519 unsigned AsmStrSize
= Record
[1];
2520 if (2+AsmStrSize
>= Record
.size())
2521 return error("Invalid record");
2522 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2523 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2524 return error("Invalid record");
2526 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2527 AsmStr
+= (char)Record
[2+i
];
2528 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2529 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2530 PointerType
*PTy
= cast
<PointerType
>(CurTy
);
2531 UpgradeInlineAsmString(&AsmStr
);
2532 V
= InlineAsm::get(cast
<FunctionType
>(PTy
->getElementType()),
2533 AsmStr
, ConstrStr
, HasSideEffects
, IsAlignStack
);
2536 // This version adds support for the asm dialect keywords (e.g.,
2538 case bitc::CST_CODE_INLINEASM
: {
2539 if (Record
.size() < 2)
2540 return error("Invalid record");
2541 std::string AsmStr
, ConstrStr
;
2542 bool HasSideEffects
= Record
[0] & 1;
2543 bool IsAlignStack
= (Record
[0] >> 1) & 1;
2544 unsigned AsmDialect
= Record
[0] >> 2;
2545 unsigned AsmStrSize
= Record
[1];
2546 if (2+AsmStrSize
>= Record
.size())
2547 return error("Invalid record");
2548 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2549 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2550 return error("Invalid record");
2552 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2553 AsmStr
+= (char)Record
[2+i
];
2554 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2555 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2556 PointerType
*PTy
= cast
<PointerType
>(CurTy
);
2557 UpgradeInlineAsmString(&AsmStr
);
2558 V
= InlineAsm::get(cast
<FunctionType
>(PTy
->getElementType()),
2559 AsmStr
, ConstrStr
, HasSideEffects
, IsAlignStack
,
2560 InlineAsm::AsmDialect(AsmDialect
));
2563 case bitc::CST_CODE_BLOCKADDRESS
:{
2564 if (Record
.size() < 3)
2565 return error("Invalid record");
2566 Type
*FnTy
= getTypeByID(Record
[0]);
2568 return error("Invalid record");
2570 dyn_cast_or_null
<Function
>(ValueList
.getConstantFwdRef(Record
[1],FnTy
));
2572 return error("Invalid record");
2574 // If the function is already parsed we can insert the block address right
2577 unsigned BBID
= Record
[2];
2579 // Invalid reference to entry block.
2580 return error("Invalid ID");
2582 Function::iterator BBI
= Fn
->begin(), BBE
= Fn
->end();
2583 for (size_t I
= 0, E
= BBID
; I
!= E
; ++I
) {
2585 return error("Invalid ID");
2590 // Otherwise insert a placeholder and remember it so it can be inserted
2591 // when the function is parsed.
2592 auto &FwdBBs
= BasicBlockFwdRefs
[Fn
];
2594 BasicBlockFwdRefQueue
.push_back(Fn
);
2595 if (FwdBBs
.size() < BBID
+ 1)
2596 FwdBBs
.resize(BBID
+ 1);
2598 FwdBBs
[BBID
] = BasicBlock::Create(Context
);
2601 V
= BlockAddress::get(Fn
, BB
);
2606 ValueList
.assignValue(V
, NextCstNo
);
2611 Error
BitcodeReader::parseUseLists() {
2612 if (Stream
.EnterSubBlock(bitc::USELIST_BLOCK_ID
))
2613 return error("Invalid record");
2615 // Read all the records.
2616 SmallVector
<uint64_t, 64> Record
;
2619 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
2621 switch (Entry
.Kind
) {
2622 case BitstreamEntry::SubBlock
: // Handled for us already.
2623 case BitstreamEntry::Error
:
2624 return error("Malformed block");
2625 case BitstreamEntry::EndBlock
:
2626 return Error::success();
2627 case BitstreamEntry::Record
:
2628 // The interesting case.
2632 // Read a use list record.
2635 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
2636 default: // Default behavior: unknown type.
2638 case bitc::USELIST_CODE_BB
:
2641 case bitc::USELIST_CODE_DEFAULT
: {
2642 unsigned RecordLength
= Record
.size();
2643 if (RecordLength
< 3)
2644 // Records should have at least an ID and two indexes.
2645 return error("Invalid record");
2646 unsigned ID
= Record
.back();
2651 assert(ID
< FunctionBBs
.size() && "Basic block not found");
2652 V
= FunctionBBs
[ID
];
2655 unsigned NumUses
= 0;
2656 SmallDenseMap
<const Use
*, unsigned, 16> Order
;
2657 for (const Use
&U
: V
->materialized_uses()) {
2658 if (++NumUses
> Record
.size())
2660 Order
[&U
] = Record
[NumUses
- 1];
2662 if (Order
.size() != Record
.size() || NumUses
> Record
.size())
2663 // Mismatches can happen if the functions are being materialized lazily
2664 // (out-of-order), or a value has been upgraded.
2667 V
->sortUseList([&](const Use
&L
, const Use
&R
) {
2668 return Order
.lookup(&L
) < Order
.lookup(&R
);
2676 /// When we see the block for metadata, remember where it is and then skip it.
2677 /// This lets us lazily deserialize the metadata.
2678 Error
BitcodeReader::rememberAndSkipMetadata() {
2679 // Save the current stream state.
2680 uint64_t CurBit
= Stream
.GetCurrentBitNo();
2681 DeferredMetadataInfo
.push_back(CurBit
);
2683 // Skip over the block for now.
2684 if (Stream
.SkipBlock())
2685 return error("Invalid record");
2686 return Error::success();
2689 Error
BitcodeReader::materializeMetadata() {
2690 for (uint64_t BitPos
: DeferredMetadataInfo
) {
2691 // Move the bit stream to the saved position.
2692 Stream
.JumpToBit(BitPos
);
2693 if (Error Err
= MDLoader
->parseModuleMetadata())
2697 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2699 if (Metadata
*Val
= TheModule
->getModuleFlag("Linker Options")) {
2700 NamedMDNode
*LinkerOpts
=
2701 TheModule
->getOrInsertNamedMetadata("llvm.linker.options");
2702 for (const MDOperand
&MDOptions
: cast
<MDNode
>(Val
)->operands())
2703 LinkerOpts
->addOperand(cast
<MDNode
>(MDOptions
));
2706 DeferredMetadataInfo
.clear();
2707 return Error::success();
2710 void BitcodeReader::setStripDebugInfo() { StripDebugInfo
= true; }
2712 /// When we see the block for a function body, remember where it is and then
2713 /// skip it. This lets us lazily deserialize the functions.
2714 Error
BitcodeReader::rememberAndSkipFunctionBody() {
2715 // Get the function we are talking about.
2716 if (FunctionsWithBodies
.empty())
2717 return error("Insufficient function protos");
2719 Function
*Fn
= FunctionsWithBodies
.back();
2720 FunctionsWithBodies
.pop_back();
2722 // Save the current stream state.
2723 uint64_t CurBit
= Stream
.GetCurrentBitNo();
2725 (DeferredFunctionInfo
[Fn
] == 0 || DeferredFunctionInfo
[Fn
] == CurBit
) &&
2726 "Mismatch between VST and scanned function offsets");
2727 DeferredFunctionInfo
[Fn
] = CurBit
;
2729 // Skip over the function block for now.
2730 if (Stream
.SkipBlock())
2731 return error("Invalid record");
2732 return Error::success();
2735 Error
BitcodeReader::globalCleanup() {
2736 // Patch the initializers for globals and aliases up.
2737 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
2739 if (!GlobalInits
.empty() || !IndirectSymbolInits
.empty())
2740 return error("Malformed global initializer set");
2742 // Look for intrinsic functions which need to be upgraded at some point
2743 for (Function
&F
: *TheModule
) {
2744 MDLoader
->upgradeDebugIntrinsics(F
);
2746 if (UpgradeIntrinsicFunction(&F
, NewFn
))
2747 UpgradedIntrinsics
[&F
] = NewFn
;
2748 else if (auto Remangled
= Intrinsic::remangleIntrinsicFunction(&F
))
2749 // Some types could be renamed during loading if several modules are
2750 // loaded in the same LLVMContext (LTO scenario). In this case we should
2751 // remangle intrinsics names as well.
2752 RemangledIntrinsics
[&F
] = Remangled
.getValue();
2755 // Look for global variables which need to be renamed.
2756 for (GlobalVariable
&GV
: TheModule
->globals())
2757 UpgradeGlobalVariable(&GV
);
2759 // Force deallocation of memory for these vectors to favor the client that
2760 // want lazy deserialization.
2761 std::vector
<std::pair
<GlobalVariable
*, unsigned>>().swap(GlobalInits
);
2762 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>().swap(
2763 IndirectSymbolInits
);
2764 return Error::success();
2767 /// Support for lazy parsing of function bodies. This is required if we
2768 /// either have an old bitcode file without a VST forward declaration record,
2769 /// or if we have an anonymous function being materialized, since anonymous
2770 /// functions do not have a name and are therefore not in the VST.
2771 Error
BitcodeReader::rememberAndSkipFunctionBodies() {
2772 Stream
.JumpToBit(NextUnreadBit
);
2774 if (Stream
.AtEndOfStream())
2775 return error("Could not find function in stream");
2777 if (!SeenFirstFunctionBody
)
2778 return error("Trying to materialize functions before seeing function blocks");
2780 // An old bitcode file with the symbol table at the end would have
2781 // finished the parse greedily.
2782 assert(SeenValueSymbolTable
);
2784 SmallVector
<uint64_t, 64> Record
;
2787 BitstreamEntry Entry
= Stream
.advance();
2788 switch (Entry
.Kind
) {
2790 return error("Expect SubBlock");
2791 case BitstreamEntry::SubBlock
:
2794 return error("Expect function block");
2795 case bitc::FUNCTION_BLOCK_ID
:
2796 if (Error Err
= rememberAndSkipFunctionBody())
2798 NextUnreadBit
= Stream
.GetCurrentBitNo();
2799 return Error::success();
2805 bool BitcodeReaderBase::readBlockInfo() {
2806 Optional
<BitstreamBlockInfo
> NewBlockInfo
= Stream
.ReadBlockInfoBlock();
2809 BlockInfo
= std::move(*NewBlockInfo
);
2813 Error
BitcodeReader::parseComdatRecord(ArrayRef
<uint64_t> Record
) {
2814 // v1: [selection_kind, name]
2815 // v2: [strtab_offset, strtab_size, selection_kind]
2817 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
2820 return error("Invalid record");
2821 Comdat::SelectionKind SK
= getDecodedComdatSelectionKind(Record
[0]);
2822 std::string OldFormatName
;
2824 if (Record
.size() < 2)
2825 return error("Invalid record");
2826 unsigned ComdatNameSize
= Record
[1];
2827 OldFormatName
.reserve(ComdatNameSize
);
2828 for (unsigned i
= 0; i
!= ComdatNameSize
; ++i
)
2829 OldFormatName
+= (char)Record
[2 + i
];
2830 Name
= OldFormatName
;
2832 Comdat
*C
= TheModule
->getOrInsertComdat(Name
);
2833 C
->setSelectionKind(SK
);
2834 ComdatList
.push_back(C
);
2835 return Error::success();
2838 static void inferDSOLocal(GlobalValue
*GV
) {
2839 // infer dso_local from linkage and visibility if it is not encoded.
2840 if (GV
->hasLocalLinkage() ||
2841 (!GV
->hasDefaultVisibility() && !GV
->hasExternalWeakLinkage()))
2842 GV
->setDSOLocal(true);
2845 Error
BitcodeReader::parseGlobalVarRecord(ArrayRef
<uint64_t> Record
) {
2846 // v1: [pointer type, isconst, initid, linkage, alignment, section,
2847 // visibility, threadlocal, unnamed_addr, externally_initialized,
2848 // dllstorageclass, comdat, attributes, preemption specifier] (name in VST)
2849 // v2: [strtab_offset, strtab_size, v1]
2851 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
2853 if (Record
.size() < 6)
2854 return error("Invalid record");
2855 Type
*Ty
= getTypeByID(Record
[0]);
2857 return error("Invalid record");
2858 bool isConstant
= Record
[1] & 1;
2859 bool explicitType
= Record
[1] & 2;
2860 unsigned AddressSpace
;
2862 AddressSpace
= Record
[1] >> 2;
2864 if (!Ty
->isPointerTy())
2865 return error("Invalid type for value");
2866 AddressSpace
= cast
<PointerType
>(Ty
)->getAddressSpace();
2867 Ty
= cast
<PointerType
>(Ty
)->getElementType();
2870 uint64_t RawLinkage
= Record
[3];
2871 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
2873 if (Error Err
= parseAlignmentValue(Record
[4], Alignment
))
2875 std::string Section
;
2877 if (Record
[5] - 1 >= SectionTable
.size())
2878 return error("Invalid ID");
2879 Section
= SectionTable
[Record
[5] - 1];
2881 GlobalValue::VisibilityTypes Visibility
= GlobalValue::DefaultVisibility
;
2882 // Local linkage must have default visibility.
2883 if (Record
.size() > 6 && !GlobalValue::isLocalLinkage(Linkage
))
2884 // FIXME: Change to an error if non-default in 4.0.
2885 Visibility
= getDecodedVisibility(Record
[6]);
2887 GlobalVariable::ThreadLocalMode TLM
= GlobalVariable::NotThreadLocal
;
2888 if (Record
.size() > 7)
2889 TLM
= getDecodedThreadLocalMode(Record
[7]);
2891 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
2892 if (Record
.size() > 8)
2893 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[8]);
2895 bool ExternallyInitialized
= false;
2896 if (Record
.size() > 9)
2897 ExternallyInitialized
= Record
[9];
2899 GlobalVariable
*NewGV
=
2900 new GlobalVariable(*TheModule
, Ty
, isConstant
, Linkage
, nullptr, Name
,
2901 nullptr, TLM
, AddressSpace
, ExternallyInitialized
);
2902 NewGV
->setAlignment(Alignment
);
2903 if (!Section
.empty())
2904 NewGV
->setSection(Section
);
2905 NewGV
->setVisibility(Visibility
);
2906 NewGV
->setUnnamedAddr(UnnamedAddr
);
2908 if (Record
.size() > 10)
2909 NewGV
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[10]));
2911 upgradeDLLImportExportLinkage(NewGV
, RawLinkage
);
2913 ValueList
.push_back(NewGV
);
2915 // Remember which value to use for the global initializer.
2916 if (unsigned InitID
= Record
[2])
2917 GlobalInits
.push_back(std::make_pair(NewGV
, InitID
- 1));
2919 if (Record
.size() > 11) {
2920 if (unsigned ComdatID
= Record
[11]) {
2921 if (ComdatID
> ComdatList
.size())
2922 return error("Invalid global variable comdat ID");
2923 NewGV
->setComdat(ComdatList
[ComdatID
- 1]);
2925 } else if (hasImplicitComdat(RawLinkage
)) {
2926 NewGV
->setComdat(reinterpret_cast<Comdat
*>(1));
2929 if (Record
.size() > 12) {
2930 auto AS
= getAttributes(Record
[12]).getFnAttributes();
2931 NewGV
->setAttributes(AS
);
2934 if (Record
.size() > 13) {
2935 NewGV
->setDSOLocal(getDecodedDSOLocal(Record
[13]));
2937 inferDSOLocal(NewGV
);
2939 return Error::success();
2942 Error
BitcodeReader::parseFunctionRecord(ArrayRef
<uint64_t> Record
) {
2943 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
2944 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
2945 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
2946 // v2: [strtab_offset, strtab_size, v1]
2948 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
2950 if (Record
.size() < 8)
2951 return error("Invalid record");
2952 Type
*Ty
= getTypeByID(Record
[0]);
2954 return error("Invalid record");
2955 if (auto *PTy
= dyn_cast
<PointerType
>(Ty
))
2956 Ty
= PTy
->getElementType();
2957 auto *FTy
= dyn_cast
<FunctionType
>(Ty
);
2959 return error("Invalid type for value");
2960 auto CC
= static_cast<CallingConv::ID
>(Record
[1]);
2961 if (CC
& ~CallingConv::MaxID
)
2962 return error("Invalid calling convention ID");
2964 unsigned AddrSpace
= TheModule
->getDataLayout().getProgramAddressSpace();
2965 if (Record
.size() > 16)
2966 AddrSpace
= Record
[16];
2968 Function
*Func
= Function::Create(FTy
, GlobalValue::ExternalLinkage
,
2969 AddrSpace
, Name
, TheModule
);
2971 Func
->setCallingConv(CC
);
2972 bool isProto
= Record
[2];
2973 uint64_t RawLinkage
= Record
[3];
2974 Func
->setLinkage(getDecodedLinkage(RawLinkage
));
2975 Func
->setAttributes(getAttributes(Record
[4]));
2978 if (Error Err
= parseAlignmentValue(Record
[5], Alignment
))
2980 Func
->setAlignment(Alignment
);
2982 if (Record
[6] - 1 >= SectionTable
.size())
2983 return error("Invalid ID");
2984 Func
->setSection(SectionTable
[Record
[6] - 1]);
2986 // Local linkage must have default visibility.
2987 if (!Func
->hasLocalLinkage())
2988 // FIXME: Change to an error if non-default in 4.0.
2989 Func
->setVisibility(getDecodedVisibility(Record
[7]));
2990 if (Record
.size() > 8 && Record
[8]) {
2991 if (Record
[8] - 1 >= GCTable
.size())
2992 return error("Invalid ID");
2993 Func
->setGC(GCTable
[Record
[8] - 1]);
2995 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
2996 if (Record
.size() > 9)
2997 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[9]);
2998 Func
->setUnnamedAddr(UnnamedAddr
);
2999 if (Record
.size() > 10 && Record
[10] != 0)
3000 FunctionPrologues
.push_back(std::make_pair(Func
, Record
[10] - 1));
3002 if (Record
.size() > 11)
3003 Func
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[11]));
3005 upgradeDLLImportExportLinkage(Func
, RawLinkage
);
3007 if (Record
.size() > 12) {
3008 if (unsigned ComdatID
= Record
[12]) {
3009 if (ComdatID
> ComdatList
.size())
3010 return error("Invalid function comdat ID");
3011 Func
->setComdat(ComdatList
[ComdatID
- 1]);
3013 } else if (hasImplicitComdat(RawLinkage
)) {
3014 Func
->setComdat(reinterpret_cast<Comdat
*>(1));
3017 if (Record
.size() > 13 && Record
[13] != 0)
3018 FunctionPrefixes
.push_back(std::make_pair(Func
, Record
[13] - 1));
3020 if (Record
.size() > 14 && Record
[14] != 0)
3021 FunctionPersonalityFns
.push_back(std::make_pair(Func
, Record
[14] - 1));
3023 if (Record
.size() > 15) {
3024 Func
->setDSOLocal(getDecodedDSOLocal(Record
[15]));
3026 inferDSOLocal(Func
);
3028 ValueList
.push_back(Func
);
3030 // If this is a function with a body, remember the prototype we are
3031 // creating now, so that we can match up the body with them later.
3033 Func
->setIsMaterializable(true);
3034 FunctionsWithBodies
.push_back(Func
);
3035 DeferredFunctionInfo
[Func
] = 0;
3037 return Error::success();
3040 Error
BitcodeReader::parseGlobalIndirectSymbolRecord(
3041 unsigned BitCode
, ArrayRef
<uint64_t> Record
) {
3042 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3043 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3044 // dllstorageclass, threadlocal, unnamed_addr,
3045 // preemption specifier] (name in VST)
3046 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3047 // visibility, dllstorageclass, threadlocal, unnamed_addr,
3048 // preemption specifier] (name in VST)
3049 // v2: [strtab_offset, strtab_size, v1]
3051 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3053 bool NewRecord
= BitCode
!= bitc::MODULE_CODE_ALIAS_OLD
;
3054 if (Record
.size() < (3 + (unsigned)NewRecord
))
3055 return error("Invalid record");
3057 Type
*Ty
= getTypeByID(Record
[OpNum
++]);
3059 return error("Invalid record");
3063 auto *PTy
= dyn_cast
<PointerType
>(Ty
);
3065 return error("Invalid type for value");
3066 Ty
= PTy
->getElementType();
3067 AddrSpace
= PTy
->getAddressSpace();
3069 AddrSpace
= Record
[OpNum
++];
3072 auto Val
= Record
[OpNum
++];
3073 auto Linkage
= Record
[OpNum
++];
3074 GlobalIndirectSymbol
*NewGA
;
3075 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3076 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
)
3077 NewGA
= GlobalAlias::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3080 NewGA
= GlobalIFunc::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3081 nullptr, TheModule
);
3082 // Old bitcode files didn't have visibility field.
3083 // Local linkage must have default visibility.
3084 if (OpNum
!= Record
.size()) {
3085 auto VisInd
= OpNum
++;
3086 if (!NewGA
->hasLocalLinkage())
3087 // FIXME: Change to an error if non-default in 4.0.
3088 NewGA
->setVisibility(getDecodedVisibility(Record
[VisInd
]));
3090 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3091 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
) {
3092 if (OpNum
!= Record
.size())
3093 NewGA
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[OpNum
++]));
3095 upgradeDLLImportExportLinkage(NewGA
, Linkage
);
3096 if (OpNum
!= Record
.size())
3097 NewGA
->setThreadLocalMode(getDecodedThreadLocalMode(Record
[OpNum
++]));
3098 if (OpNum
!= Record
.size())
3099 NewGA
->setUnnamedAddr(getDecodedUnnamedAddrType(Record
[OpNum
++]));
3101 if (OpNum
!= Record
.size())
3102 NewGA
->setDSOLocal(getDecodedDSOLocal(Record
[OpNum
++]));
3103 inferDSOLocal(NewGA
);
3105 ValueList
.push_back(NewGA
);
3106 IndirectSymbolInits
.push_back(std::make_pair(NewGA
, Val
));
3107 return Error::success();
3110 Error
BitcodeReader::parseModule(uint64_t ResumeBit
,
3111 bool ShouldLazyLoadMetadata
) {
3113 Stream
.JumpToBit(ResumeBit
);
3114 else if (Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
3115 return error("Invalid record");
3117 SmallVector
<uint64_t, 64> Record
;
3119 // Read all the records for this module.
3121 BitstreamEntry Entry
= Stream
.advance();
3123 switch (Entry
.Kind
) {
3124 case BitstreamEntry::Error
:
3125 return error("Malformed block");
3126 case BitstreamEntry::EndBlock
:
3127 return globalCleanup();
3129 case BitstreamEntry::SubBlock
:
3131 default: // Skip unknown content.
3132 if (Stream
.SkipBlock())
3133 return error("Invalid record");
3135 case bitc::BLOCKINFO_BLOCK_ID
:
3136 if (readBlockInfo())
3137 return error("Malformed block");
3139 case bitc::PARAMATTR_BLOCK_ID
:
3140 if (Error Err
= parseAttributeBlock())
3143 case bitc::PARAMATTR_GROUP_BLOCK_ID
:
3144 if (Error Err
= parseAttributeGroupBlock())
3147 case bitc::TYPE_BLOCK_ID_NEW
:
3148 if (Error Err
= parseTypeTable())
3151 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3152 if (!SeenValueSymbolTable
) {
3153 // Either this is an old form VST without function index and an
3154 // associated VST forward declaration record (which would have caused
3155 // the VST to be jumped to and parsed before it was encountered
3156 // normally in the stream), or there were no function blocks to
3157 // trigger an earlier parsing of the VST.
3158 assert(VSTOffset
== 0 || FunctionsWithBodies
.empty());
3159 if (Error Err
= parseValueSymbolTable())
3161 SeenValueSymbolTable
= true;
3163 // We must have had a VST forward declaration record, which caused
3164 // the parser to jump to and parse the VST earlier.
3165 assert(VSTOffset
> 0);
3166 if (Stream
.SkipBlock())
3167 return error("Invalid record");
3170 case bitc::CONSTANTS_BLOCK_ID
:
3171 if (Error Err
= parseConstants())
3173 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
3176 case bitc::METADATA_BLOCK_ID
:
3177 if (ShouldLazyLoadMetadata
) {
3178 if (Error Err
= rememberAndSkipMetadata())
3182 assert(DeferredMetadataInfo
.empty() && "Unexpected deferred metadata");
3183 if (Error Err
= MDLoader
->parseModuleMetadata())
3186 case bitc::METADATA_KIND_BLOCK_ID
:
3187 if (Error Err
= MDLoader
->parseMetadataKinds())
3190 case bitc::FUNCTION_BLOCK_ID
:
3191 // If this is the first function body we've seen, reverse the
3192 // FunctionsWithBodies list.
3193 if (!SeenFirstFunctionBody
) {
3194 std::reverse(FunctionsWithBodies
.begin(), FunctionsWithBodies
.end());
3195 if (Error Err
= globalCleanup())
3197 SeenFirstFunctionBody
= true;
3200 if (VSTOffset
> 0) {
3201 // If we have a VST forward declaration record, make sure we
3202 // parse the VST now if we haven't already. It is needed to
3203 // set up the DeferredFunctionInfo vector for lazy reading.
3204 if (!SeenValueSymbolTable
) {
3205 if (Error Err
= BitcodeReader::parseValueSymbolTable(VSTOffset
))
3207 SeenValueSymbolTable
= true;
3208 // Fall through so that we record the NextUnreadBit below.
3209 // This is necessary in case we have an anonymous function that
3210 // is later materialized. Since it will not have a VST entry we
3211 // need to fall back to the lazy parse to find its offset.
3213 // If we have a VST forward declaration record, but have already
3214 // parsed the VST (just above, when the first function body was
3215 // encountered here), then we are resuming the parse after
3216 // materializing functions. The ResumeBit points to the
3217 // start of the last function block recorded in the
3218 // DeferredFunctionInfo map. Skip it.
3219 if (Stream
.SkipBlock())
3220 return error("Invalid record");
3225 // Support older bitcode files that did not have the function
3226 // index in the VST, nor a VST forward declaration record, as
3227 // well as anonymous functions that do not have VST entries.
3228 // Build the DeferredFunctionInfo vector on the fly.
3229 if (Error Err
= rememberAndSkipFunctionBody())
3232 // Suspend parsing when we reach the function bodies. Subsequent
3233 // materialization calls will resume it when necessary. If the bitcode
3234 // file is old, the symbol table will be at the end instead and will not
3235 // have been seen yet. In this case, just finish the parse now.
3236 if (SeenValueSymbolTable
) {
3237 NextUnreadBit
= Stream
.GetCurrentBitNo();
3238 // After the VST has been parsed, we need to make sure intrinsic name
3239 // are auto-upgraded.
3240 return globalCleanup();
3243 case bitc::USELIST_BLOCK_ID
:
3244 if (Error Err
= parseUseLists())
3247 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
:
3248 if (Error Err
= parseOperandBundleTags())
3251 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID
:
3252 if (Error Err
= parseSyncScopeNames())
3258 case BitstreamEntry::Record
:
3259 // The interesting case.
3264 auto BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3266 default: break; // Default behavior, ignore unknown content.
3267 case bitc::MODULE_CODE_VERSION
: {
3268 Expected
<unsigned> VersionOrErr
= parseVersionRecord(Record
);
3270 return VersionOrErr
.takeError();
3271 UseRelativeIDs
= *VersionOrErr
>= 1;
3274 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
3276 if (convertToString(Record
, 0, S
))
3277 return error("Invalid record");
3278 TheModule
->setTargetTriple(S
);
3281 case bitc::MODULE_CODE_DATALAYOUT
: { // DATALAYOUT: [strchr x N]
3283 if (convertToString(Record
, 0, S
))
3284 return error("Invalid record");
3285 TheModule
->setDataLayout(S
);
3288 case bitc::MODULE_CODE_ASM
: { // ASM: [strchr x N]
3290 if (convertToString(Record
, 0, S
))
3291 return error("Invalid record");
3292 TheModule
->setModuleInlineAsm(S
);
3295 case bitc::MODULE_CODE_DEPLIB
: { // DEPLIB: [strchr x N]
3296 // FIXME: Remove in 4.0.
3298 if (convertToString(Record
, 0, S
))
3299 return error("Invalid record");
3303 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
3305 if (convertToString(Record
, 0, S
))
3306 return error("Invalid record");
3307 SectionTable
.push_back(S
);
3310 case bitc::MODULE_CODE_GCNAME
: { // SECTIONNAME: [strchr x N]
3312 if (convertToString(Record
, 0, S
))
3313 return error("Invalid record");
3314 GCTable
.push_back(S
);
3317 case bitc::MODULE_CODE_COMDAT
:
3318 if (Error Err
= parseComdatRecord(Record
))
3321 case bitc::MODULE_CODE_GLOBALVAR
:
3322 if (Error Err
= parseGlobalVarRecord(Record
))
3325 case bitc::MODULE_CODE_FUNCTION
:
3326 if (Error Err
= parseFunctionRecord(Record
))
3329 case bitc::MODULE_CODE_IFUNC
:
3330 case bitc::MODULE_CODE_ALIAS
:
3331 case bitc::MODULE_CODE_ALIAS_OLD
:
3332 if (Error Err
= parseGlobalIndirectSymbolRecord(BitCode
, Record
))
3335 /// MODULE_CODE_VSTOFFSET: [offset]
3336 case bitc::MODULE_CODE_VSTOFFSET
:
3337 if (Record
.size() < 1)
3338 return error("Invalid record");
3339 // Note that we subtract 1 here because the offset is relative to one word
3340 // before the start of the identification or module block, which was
3341 // historically always the start of the regular bitcode header.
3342 VSTOffset
= Record
[0] - 1;
3344 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3345 case bitc::MODULE_CODE_SOURCE_FILENAME
:
3346 SmallString
<128> ValueName
;
3347 if (convertToString(Record
, 0, ValueName
))
3348 return error("Invalid record");
3349 TheModule
->setSourceFileName(ValueName
);
3356 Error
BitcodeReader::parseBitcodeInto(Module
*M
, bool ShouldLazyLoadMetadata
,
3359 MDLoader
= MetadataLoader(Stream
, *M
, ValueList
, IsImporting
,
3360 [&](unsigned ID
) { return getTypeByID(ID
); });
3361 return parseModule(0, ShouldLazyLoadMetadata
);
3364 Error
BitcodeReader::typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
) {
3365 if (!isa
<PointerType
>(PtrType
))
3366 return error("Load/Store operand is not a pointer type");
3367 Type
*ElemType
= cast
<PointerType
>(PtrType
)->getElementType();
3369 if (ValType
&& ValType
!= ElemType
)
3370 return error("Explicit load/store type does not match pointee "
3371 "type of pointer operand");
3372 if (!PointerType::isLoadableOrStorableType(ElemType
))
3373 return error("Cannot load/store from pointer");
3374 return Error::success();
3377 /// Lazily parse the specified function body block.
3378 Error
BitcodeReader::parseFunctionBody(Function
*F
) {
3379 if (Stream
.EnterSubBlock(bitc::FUNCTION_BLOCK_ID
))
3380 return error("Invalid record");
3382 // Unexpected unresolved metadata when parsing function.
3383 if (MDLoader
->hasFwdRefs())
3384 return error("Invalid function metadata: incoming forward references");
3386 InstructionList
.clear();
3387 unsigned ModuleValueListSize
= ValueList
.size();
3388 unsigned ModuleMDLoaderSize
= MDLoader
->size();
3390 // Add all the function arguments to the value table.
3391 for (Argument
&I
: F
->args())
3392 ValueList
.push_back(&I
);
3394 unsigned NextValueNo
= ValueList
.size();
3395 BasicBlock
*CurBB
= nullptr;
3396 unsigned CurBBNo
= 0;
3399 auto getLastInstruction
= [&]() -> Instruction
* {
3400 if (CurBB
&& !CurBB
->empty())
3401 return &CurBB
->back();
3402 else if (CurBBNo
&& FunctionBBs
[CurBBNo
- 1] &&
3403 !FunctionBBs
[CurBBNo
- 1]->empty())
3404 return &FunctionBBs
[CurBBNo
- 1]->back();
3408 std::vector
<OperandBundleDef
> OperandBundles
;
3410 // Read all the records.
3411 SmallVector
<uint64_t, 64> Record
;
3414 BitstreamEntry Entry
= Stream
.advance();
3416 switch (Entry
.Kind
) {
3417 case BitstreamEntry::Error
:
3418 return error("Malformed block");
3419 case BitstreamEntry::EndBlock
:
3420 goto OutOfRecordLoop
;
3422 case BitstreamEntry::SubBlock
:
3424 default: // Skip unknown content.
3425 if (Stream
.SkipBlock())
3426 return error("Invalid record");
3428 case bitc::CONSTANTS_BLOCK_ID
:
3429 if (Error Err
= parseConstants())
3431 NextValueNo
= ValueList
.size();
3433 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3434 if (Error Err
= parseValueSymbolTable())
3437 case bitc::METADATA_ATTACHMENT_ID
:
3438 if (Error Err
= MDLoader
->parseMetadataAttachment(*F
, InstructionList
))
3441 case bitc::METADATA_BLOCK_ID
:
3442 assert(DeferredMetadataInfo
.empty() &&
3443 "Must read all module-level metadata before function-level");
3444 if (Error Err
= MDLoader
->parseFunctionMetadata())
3447 case bitc::USELIST_BLOCK_ID
:
3448 if (Error Err
= parseUseLists())
3454 case BitstreamEntry::Record
:
3455 // The interesting case.
3461 Instruction
*I
= nullptr;
3462 unsigned BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3464 default: // Default behavior: reject
3465 return error("Invalid value");
3466 case bitc::FUNC_CODE_DECLAREBLOCKS
: { // DECLAREBLOCKS: [nblocks]
3467 if (Record
.size() < 1 || Record
[0] == 0)
3468 return error("Invalid record");
3469 // Create all the basic blocks for the function.
3470 FunctionBBs
.resize(Record
[0]);
3472 // See if anything took the address of blocks in this function.
3473 auto BBFRI
= BasicBlockFwdRefs
.find(F
);
3474 if (BBFRI
== BasicBlockFwdRefs
.end()) {
3475 for (unsigned i
= 0, e
= FunctionBBs
.size(); i
!= e
; ++i
)
3476 FunctionBBs
[i
] = BasicBlock::Create(Context
, "", F
);
3478 auto &BBRefs
= BBFRI
->second
;
3479 // Check for invalid basic block references.
3480 if (BBRefs
.size() > FunctionBBs
.size())
3481 return error("Invalid ID");
3482 assert(!BBRefs
.empty() && "Unexpected empty array");
3483 assert(!BBRefs
.front() && "Invalid reference to entry block");
3484 for (unsigned I
= 0, E
= FunctionBBs
.size(), RE
= BBRefs
.size(); I
!= E
;
3486 if (I
< RE
&& BBRefs
[I
]) {
3487 BBRefs
[I
]->insertInto(F
);
3488 FunctionBBs
[I
] = BBRefs
[I
];
3490 FunctionBBs
[I
] = BasicBlock::Create(Context
, "", F
);
3493 // Erase from the table.
3494 BasicBlockFwdRefs
.erase(BBFRI
);
3497 CurBB
= FunctionBBs
[0];
3501 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN
: // DEBUG_LOC_AGAIN
3502 // This record indicates that the last instruction is at the same
3503 // location as the previous instruction with a location.
3504 I
= getLastInstruction();
3507 return error("Invalid record");
3508 I
->setDebugLoc(LastLoc
);
3512 case bitc::FUNC_CODE_DEBUG_LOC
: { // DEBUG_LOC: [line, col, scope, ia]
3513 I
= getLastInstruction();
3514 if (!I
|| Record
.size() < 4)
3515 return error("Invalid record");
3517 unsigned Line
= Record
[0], Col
= Record
[1];
3518 unsigned ScopeID
= Record
[2], IAID
= Record
[3];
3519 bool isImplicitCode
= Record
.size() == 5 && Record
[4];
3521 MDNode
*Scope
= nullptr, *IA
= nullptr;
3523 Scope
= MDLoader
->getMDNodeFwdRefOrNull(ScopeID
- 1);
3525 return error("Invalid record");
3528 IA
= MDLoader
->getMDNodeFwdRefOrNull(IAID
- 1);
3530 return error("Invalid record");
3532 LastLoc
= DebugLoc::get(Line
, Col
, Scope
, IA
, isImplicitCode
);
3533 I
->setDebugLoc(LastLoc
);
3538 case bitc::FUNC_CODE_INST_BINOP
: { // BINOP: [opval, ty, opval, opcode]
3541 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
3542 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
) ||
3543 OpNum
+1 > Record
.size())
3544 return error("Invalid record");
3546 int Opc
= getDecodedBinaryOpcode(Record
[OpNum
++], LHS
->getType());
3548 return error("Invalid record");
3549 I
= BinaryOperator::Create((Instruction::BinaryOps
)Opc
, LHS
, RHS
);
3550 InstructionList
.push_back(I
);
3551 if (OpNum
< Record
.size()) {
3552 if (Opc
== Instruction::Add
||
3553 Opc
== Instruction::Sub
||
3554 Opc
== Instruction::Mul
||
3555 Opc
== Instruction::Shl
) {
3556 if (Record
[OpNum
] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
3557 cast
<BinaryOperator
>(I
)->setHasNoSignedWrap(true);
3558 if (Record
[OpNum
] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
3559 cast
<BinaryOperator
>(I
)->setHasNoUnsignedWrap(true);
3560 } else if (Opc
== Instruction::SDiv
||
3561 Opc
== Instruction::UDiv
||
3562 Opc
== Instruction::LShr
||
3563 Opc
== Instruction::AShr
) {
3564 if (Record
[OpNum
] & (1 << bitc::PEO_EXACT
))
3565 cast
<BinaryOperator
>(I
)->setIsExact(true);
3566 } else if (isa
<FPMathOperator
>(I
)) {
3567 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
3569 I
->setFastMathFlags(FMF
);
3575 case bitc::FUNC_CODE_INST_CAST
: { // CAST: [opval, opty, destty, castopc]
3578 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
3579 OpNum
+2 != Record
.size())
3580 return error("Invalid record");
3582 Type
*ResTy
= getTypeByID(Record
[OpNum
]);
3583 int Opc
= getDecodedCastOpcode(Record
[OpNum
+ 1]);
3584 if (Opc
== -1 || !ResTy
)
3585 return error("Invalid record");
3586 Instruction
*Temp
= nullptr;
3587 if ((I
= UpgradeBitCastInst(Opc
, Op
, ResTy
, Temp
))) {
3589 InstructionList
.push_back(Temp
);
3590 CurBB
->getInstList().push_back(Temp
);
3593 auto CastOp
= (Instruction::CastOps
)Opc
;
3594 if (!CastInst::castIsValid(CastOp
, Op
, ResTy
))
3595 return error("Invalid cast");
3596 I
= CastInst::Create(CastOp
, Op
, ResTy
);
3598 InstructionList
.push_back(I
);
3601 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
:
3602 case bitc::FUNC_CODE_INST_GEP_OLD
:
3603 case bitc::FUNC_CODE_INST_GEP
: { // GEP: type, [n x operands]
3609 if (BitCode
== bitc::FUNC_CODE_INST_GEP
) {
3610 InBounds
= Record
[OpNum
++];
3611 Ty
= getTypeByID(Record
[OpNum
++]);
3613 InBounds
= BitCode
== bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
;
3618 if (getValueTypePair(Record
, OpNum
, NextValueNo
, BasePtr
))
3619 return error("Invalid record");
3622 Ty
= cast
<PointerType
>(BasePtr
->getType()->getScalarType())
3625 cast
<PointerType
>(BasePtr
->getType()->getScalarType())
3628 "Explicit gep type does not match pointee type of pointer operand");
3630 SmallVector
<Value
*, 16> GEPIdx
;
3631 while (OpNum
!= Record
.size()) {
3633 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
3634 return error("Invalid record");
3635 GEPIdx
.push_back(Op
);
3638 I
= GetElementPtrInst::Create(Ty
, BasePtr
, GEPIdx
);
3640 InstructionList
.push_back(I
);
3642 cast
<GetElementPtrInst
>(I
)->setIsInBounds(true);
3646 case bitc::FUNC_CODE_INST_EXTRACTVAL
: {
3647 // EXTRACTVAL: [opty, opval, n x indices]
3650 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
))
3651 return error("Invalid record");
3653 unsigned RecSize
= Record
.size();
3654 if (OpNum
== RecSize
)
3655 return error("EXTRACTVAL: Invalid instruction with 0 indices");
3657 SmallVector
<unsigned, 4> EXTRACTVALIdx
;
3658 Type
*CurTy
= Agg
->getType();
3659 for (; OpNum
!= RecSize
; ++OpNum
) {
3660 bool IsArray
= CurTy
->isArrayTy();
3661 bool IsStruct
= CurTy
->isStructTy();
3662 uint64_t Index
= Record
[OpNum
];
3664 if (!IsStruct
&& !IsArray
)
3665 return error("EXTRACTVAL: Invalid type");
3666 if ((unsigned)Index
!= Index
)
3667 return error("Invalid value");
3668 if (IsStruct
&& Index
>= CurTy
->subtypes().size())
3669 return error("EXTRACTVAL: Invalid struct index");
3670 if (IsArray
&& Index
>= CurTy
->getArrayNumElements())
3671 return error("EXTRACTVAL: Invalid array index");
3672 EXTRACTVALIdx
.push_back((unsigned)Index
);
3675 CurTy
= CurTy
->subtypes()[Index
];
3677 CurTy
= CurTy
->subtypes()[0];
3680 I
= ExtractValueInst::Create(Agg
, EXTRACTVALIdx
);
3681 InstructionList
.push_back(I
);
3685 case bitc::FUNC_CODE_INST_INSERTVAL
: {
3686 // INSERTVAL: [opty, opval, opty, opval, n x indices]
3689 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
))
3690 return error("Invalid record");
3692 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Val
))
3693 return error("Invalid record");
3695 unsigned RecSize
= Record
.size();
3696 if (OpNum
== RecSize
)
3697 return error("INSERTVAL: Invalid instruction with 0 indices");
3699 SmallVector
<unsigned, 4> INSERTVALIdx
;
3700 Type
*CurTy
= Agg
->getType();
3701 for (; OpNum
!= RecSize
; ++OpNum
) {
3702 bool IsArray
= CurTy
->isArrayTy();
3703 bool IsStruct
= CurTy
->isStructTy();
3704 uint64_t Index
= Record
[OpNum
];
3706 if (!IsStruct
&& !IsArray
)
3707 return error("INSERTVAL: Invalid type");
3708 if ((unsigned)Index
!= Index
)
3709 return error("Invalid value");
3710 if (IsStruct
&& Index
>= CurTy
->subtypes().size())
3711 return error("INSERTVAL: Invalid struct index");
3712 if (IsArray
&& Index
>= CurTy
->getArrayNumElements())
3713 return error("INSERTVAL: Invalid array index");
3715 INSERTVALIdx
.push_back((unsigned)Index
);
3717 CurTy
= CurTy
->subtypes()[Index
];
3719 CurTy
= CurTy
->subtypes()[0];
3722 if (CurTy
!= Val
->getType())
3723 return error("Inserted value type doesn't match aggregate type");
3725 I
= InsertValueInst::Create(Agg
, Val
, INSERTVALIdx
);
3726 InstructionList
.push_back(I
);
3730 case bitc::FUNC_CODE_INST_SELECT
: { // SELECT: [opval, ty, opval, opval]
3731 // obsolete form of select
3732 // handles select i1 ... in old bitcode
3734 Value
*TrueVal
, *FalseVal
, *Cond
;
3735 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
) ||
3736 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
3737 popValue(Record
, OpNum
, NextValueNo
, Type::getInt1Ty(Context
), Cond
))
3738 return error("Invalid record");
3740 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
3741 InstructionList
.push_back(I
);
3745 case bitc::FUNC_CODE_INST_VSELECT
: {// VSELECT: [ty,opval,opval,predty,pred]
3746 // new form of select
3747 // handles select i1 or select [N x i1]
3749 Value
*TrueVal
, *FalseVal
, *Cond
;
3750 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
) ||
3751 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
3752 getValueTypePair(Record
, OpNum
, NextValueNo
, Cond
))
3753 return error("Invalid record");
3755 // select condition can be either i1 or [N x i1]
3756 if (VectorType
* vector_type
=
3757 dyn_cast
<VectorType
>(Cond
->getType())) {
3759 if (vector_type
->getElementType() != Type::getInt1Ty(Context
))
3760 return error("Invalid type for value");
3763 if (Cond
->getType() != Type::getInt1Ty(Context
))
3764 return error("Invalid type for value");
3767 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
3768 InstructionList
.push_back(I
);
3772 case bitc::FUNC_CODE_INST_EXTRACTELT
: { // EXTRACTELT: [opty, opval, opval]
3775 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
) ||
3776 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
3777 return error("Invalid record");
3778 if (!Vec
->getType()->isVectorTy())
3779 return error("Invalid type for value");
3780 I
= ExtractElementInst::Create(Vec
, Idx
);
3781 InstructionList
.push_back(I
);
3785 case bitc::FUNC_CODE_INST_INSERTELT
: { // INSERTELT: [ty, opval,opval,opval]
3787 Value
*Vec
, *Elt
, *Idx
;
3788 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
))
3789 return error("Invalid record");
3790 if (!Vec
->getType()->isVectorTy())
3791 return error("Invalid type for value");
3792 if (popValue(Record
, OpNum
, NextValueNo
,
3793 cast
<VectorType
>(Vec
->getType())->getElementType(), Elt
) ||
3794 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
3795 return error("Invalid record");
3796 I
= InsertElementInst::Create(Vec
, Elt
, Idx
);
3797 InstructionList
.push_back(I
);
3801 case bitc::FUNC_CODE_INST_SHUFFLEVEC
: {// SHUFFLEVEC: [opval,ty,opval,opval]
3803 Value
*Vec1
, *Vec2
, *Mask
;
3804 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec1
) ||
3805 popValue(Record
, OpNum
, NextValueNo
, Vec1
->getType(), Vec2
))
3806 return error("Invalid record");
3808 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Mask
))
3809 return error("Invalid record");
3810 if (!Vec1
->getType()->isVectorTy() || !Vec2
->getType()->isVectorTy())
3811 return error("Invalid type for value");
3812 I
= new ShuffleVectorInst(Vec1
, Vec2
, Mask
);
3813 InstructionList
.push_back(I
);
3817 case bitc::FUNC_CODE_INST_CMP
: // CMP: [opty, opval, opval, pred]
3818 // Old form of ICmp/FCmp returning bool
3819 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
3820 // both legal on vectors but had different behaviour.
3821 case bitc::FUNC_CODE_INST_CMP2
: { // CMP2: [opty, opval, opval, pred]
3822 // FCmp/ICmp returning bool or vector of bool
3826 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
3827 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
))
3828 return error("Invalid record");
3830 unsigned PredVal
= Record
[OpNum
];
3831 bool IsFP
= LHS
->getType()->isFPOrFPVectorTy();
3833 if (IsFP
&& Record
.size() > OpNum
+1)
3834 FMF
= getDecodedFastMathFlags(Record
[++OpNum
]);
3836 if (OpNum
+1 != Record
.size())
3837 return error("Invalid record");
3839 if (LHS
->getType()->isFPOrFPVectorTy())
3840 I
= new FCmpInst((FCmpInst::Predicate
)PredVal
, LHS
, RHS
);
3842 I
= new ICmpInst((ICmpInst::Predicate
)PredVal
, LHS
, RHS
);
3845 I
->setFastMathFlags(FMF
);
3846 InstructionList
.push_back(I
);
3850 case bitc::FUNC_CODE_INST_RET
: // RET: [opty,opval<optional>]
3852 unsigned Size
= Record
.size();
3854 I
= ReturnInst::Create(Context
);
3855 InstructionList
.push_back(I
);
3860 Value
*Op
= nullptr;
3861 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
3862 return error("Invalid record");
3863 if (OpNum
!= Record
.size())
3864 return error("Invalid record");
3866 I
= ReturnInst::Create(Context
, Op
);
3867 InstructionList
.push_back(I
);
3870 case bitc::FUNC_CODE_INST_BR
: { // BR: [bb#, bb#, opval] or [bb#]
3871 if (Record
.size() != 1 && Record
.size() != 3)
3872 return error("Invalid record");
3873 BasicBlock
*TrueDest
= getBasicBlock(Record
[0]);
3875 return error("Invalid record");
3877 if (Record
.size() == 1) {
3878 I
= BranchInst::Create(TrueDest
);
3879 InstructionList
.push_back(I
);
3882 BasicBlock
*FalseDest
= getBasicBlock(Record
[1]);
3883 Value
*Cond
= getValue(Record
, 2, NextValueNo
,
3884 Type::getInt1Ty(Context
));
3885 if (!FalseDest
|| !Cond
)
3886 return error("Invalid record");
3887 I
= BranchInst::Create(TrueDest
, FalseDest
, Cond
);
3888 InstructionList
.push_back(I
);
3892 case bitc::FUNC_CODE_INST_CLEANUPRET
: { // CLEANUPRET: [val] or [val,bb#]
3893 if (Record
.size() != 1 && Record
.size() != 2)
3894 return error("Invalid record");
3897 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
3899 return error("Invalid record");
3900 BasicBlock
*UnwindDest
= nullptr;
3901 if (Record
.size() == 2) {
3902 UnwindDest
= getBasicBlock(Record
[Idx
++]);
3904 return error("Invalid record");
3907 I
= CleanupReturnInst::Create(CleanupPad
, UnwindDest
);
3908 InstructionList
.push_back(I
);
3911 case bitc::FUNC_CODE_INST_CATCHRET
: { // CATCHRET: [val,bb#]
3912 if (Record
.size() != 2)
3913 return error("Invalid record");
3916 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
3918 return error("Invalid record");
3919 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
3921 return error("Invalid record");
3923 I
= CatchReturnInst::Create(CatchPad
, BB
);
3924 InstructionList
.push_back(I
);
3927 case bitc::FUNC_CODE_INST_CATCHSWITCH
: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
3928 // We must have, at minimum, the outer scope and the number of arguments.
3929 if (Record
.size() < 2)
3930 return error("Invalid record");
3935 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
3937 unsigned NumHandlers
= Record
[Idx
++];
3939 SmallVector
<BasicBlock
*, 2> Handlers
;
3940 for (unsigned Op
= 0; Op
!= NumHandlers
; ++Op
) {
3941 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
3943 return error("Invalid record");
3944 Handlers
.push_back(BB
);
3947 BasicBlock
*UnwindDest
= nullptr;
3948 if (Idx
+ 1 == Record
.size()) {
3949 UnwindDest
= getBasicBlock(Record
[Idx
++]);
3951 return error("Invalid record");
3954 if (Record
.size() != Idx
)
3955 return error("Invalid record");
3958 CatchSwitchInst::Create(ParentPad
, UnwindDest
, NumHandlers
);
3959 for (BasicBlock
*Handler
: Handlers
)
3960 CatchSwitch
->addHandler(Handler
);
3962 InstructionList
.push_back(I
);
3965 case bitc::FUNC_CODE_INST_CATCHPAD
:
3966 case bitc::FUNC_CODE_INST_CLEANUPPAD
: { // [tok,num,(ty,val)*]
3967 // We must have, at minimum, the outer scope and the number of arguments.
3968 if (Record
.size() < 2)
3969 return error("Invalid record");
3974 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
3976 unsigned NumArgOperands
= Record
[Idx
++];
3978 SmallVector
<Value
*, 2> Args
;
3979 for (unsigned Op
= 0; Op
!= NumArgOperands
; ++Op
) {
3981 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
3982 return error("Invalid record");
3983 Args
.push_back(Val
);
3986 if (Record
.size() != Idx
)
3987 return error("Invalid record");
3989 if (BitCode
== bitc::FUNC_CODE_INST_CLEANUPPAD
)
3990 I
= CleanupPadInst::Create(ParentPad
, Args
);
3992 I
= CatchPadInst::Create(ParentPad
, Args
);
3993 InstructionList
.push_back(I
);
3996 case bitc::FUNC_CODE_INST_SWITCH
: { // SWITCH: [opty, op0, op1, ...]
3998 if ((Record
[0] >> 16) == SWITCH_INST_MAGIC
) {
3999 // "New" SwitchInst format with case ranges. The changes to write this
4000 // format were reverted but we still recognize bitcode that uses it.
4001 // Hopefully someday we will have support for case ranges and can use
4002 // this format again.
4004 Type
*OpTy
= getTypeByID(Record
[1]);
4005 unsigned ValueBitWidth
= cast
<IntegerType
>(OpTy
)->getBitWidth();
4007 Value
*Cond
= getValue(Record
, 2, NextValueNo
, OpTy
);
4008 BasicBlock
*Default
= getBasicBlock(Record
[3]);
4009 if (!OpTy
|| !Cond
|| !Default
)
4010 return error("Invalid record");
4012 unsigned NumCases
= Record
[4];
4014 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4015 InstructionList
.push_back(SI
);
4017 unsigned CurIdx
= 5;
4018 for (unsigned i
= 0; i
!= NumCases
; ++i
) {
4019 SmallVector
<ConstantInt
*, 1> CaseVals
;
4020 unsigned NumItems
= Record
[CurIdx
++];
4021 for (unsigned ci
= 0; ci
!= NumItems
; ++ci
) {
4022 bool isSingleNumber
= Record
[CurIdx
++];
4025 unsigned ActiveWords
= 1;
4026 if (ValueBitWidth
> 64)
4027 ActiveWords
= Record
[CurIdx
++];
4028 Low
= readWideAPInt(makeArrayRef(&Record
[CurIdx
], ActiveWords
),
4030 CurIdx
+= ActiveWords
;
4032 if (!isSingleNumber
) {
4034 if (ValueBitWidth
> 64)
4035 ActiveWords
= Record
[CurIdx
++];
4036 APInt High
= readWideAPInt(
4037 makeArrayRef(&Record
[CurIdx
], ActiveWords
), ValueBitWidth
);
4038 CurIdx
+= ActiveWords
;
4040 // FIXME: It is not clear whether values in the range should be
4041 // compared as signed or unsigned values. The partially
4042 // implemented changes that used this format in the past used
4043 // unsigned comparisons.
4044 for ( ; Low
.ule(High
); ++Low
)
4045 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4047 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4049 BasicBlock
*DestBB
= getBasicBlock(Record
[CurIdx
++]);
4050 for (SmallVector
<ConstantInt
*, 1>::iterator cvi
= CaseVals
.begin(),
4051 cve
= CaseVals
.end(); cvi
!= cve
; ++cvi
)
4052 SI
->addCase(*cvi
, DestBB
);
4058 // Old SwitchInst format without case ranges.
4060 if (Record
.size() < 3 || (Record
.size() & 1) == 0)
4061 return error("Invalid record");
4062 Type
*OpTy
= getTypeByID(Record
[0]);
4063 Value
*Cond
= getValue(Record
, 1, NextValueNo
, OpTy
);
4064 BasicBlock
*Default
= getBasicBlock(Record
[2]);
4065 if (!OpTy
|| !Cond
|| !Default
)
4066 return error("Invalid record");
4067 unsigned NumCases
= (Record
.size()-3)/2;
4068 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4069 InstructionList
.push_back(SI
);
4070 for (unsigned i
= 0, e
= NumCases
; i
!= e
; ++i
) {
4071 ConstantInt
*CaseVal
=
4072 dyn_cast_or_null
<ConstantInt
>(getFnValueByID(Record
[3+i
*2], OpTy
));
4073 BasicBlock
*DestBB
= getBasicBlock(Record
[1+3+i
*2]);
4074 if (!CaseVal
|| !DestBB
) {
4076 return error("Invalid record");
4078 SI
->addCase(CaseVal
, DestBB
);
4083 case bitc::FUNC_CODE_INST_INDIRECTBR
: { // INDIRECTBR: [opty, op0, op1, ...]
4084 if (Record
.size() < 2)
4085 return error("Invalid record");
4086 Type
*OpTy
= getTypeByID(Record
[0]);
4087 Value
*Address
= getValue(Record
, 1, NextValueNo
, OpTy
);
4088 if (!OpTy
|| !Address
)
4089 return error("Invalid record");
4090 unsigned NumDests
= Record
.size()-2;
4091 IndirectBrInst
*IBI
= IndirectBrInst::Create(Address
, NumDests
);
4092 InstructionList
.push_back(IBI
);
4093 for (unsigned i
= 0, e
= NumDests
; i
!= e
; ++i
) {
4094 if (BasicBlock
*DestBB
= getBasicBlock(Record
[2+i
])) {
4095 IBI
->addDestination(DestBB
);
4098 return error("Invalid record");
4105 case bitc::FUNC_CODE_INST_INVOKE
: {
4106 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4107 if (Record
.size() < 4)
4108 return error("Invalid record");
4110 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4111 unsigned CCInfo
= Record
[OpNum
++];
4112 BasicBlock
*NormalBB
= getBasicBlock(Record
[OpNum
++]);
4113 BasicBlock
*UnwindBB
= getBasicBlock(Record
[OpNum
++]);
4115 FunctionType
*FTy
= nullptr;
4116 if (CCInfo
>> 13 & 1 &&
4117 !(FTy
= dyn_cast
<FunctionType
>(getTypeByID(Record
[OpNum
++]))))
4118 return error("Explicit invoke type is not a function type");
4121 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
))
4122 return error("Invalid record");
4124 PointerType
*CalleeTy
= dyn_cast
<PointerType
>(Callee
->getType());
4126 return error("Callee is not a pointer");
4128 FTy
= dyn_cast
<FunctionType
>(CalleeTy
->getElementType());
4130 return error("Callee is not of pointer to function type");
4131 } else if (CalleeTy
->getElementType() != FTy
)
4132 return error("Explicit invoke type does not match pointee type of "
4134 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4135 return error("Insufficient operands to call");
4137 SmallVector
<Value
*, 16> Ops
;
4138 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4139 Ops
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4140 FTy
->getParamType(i
)));
4142 return error("Invalid record");
4145 if (!FTy
->isVarArg()) {
4146 if (Record
.size() != OpNum
)
4147 return error("Invalid record");
4149 // Read type/value pairs for varargs params.
4150 while (OpNum
!= Record
.size()) {
4152 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4153 return error("Invalid record");
4158 I
= InvokeInst::Create(Callee
, NormalBB
, UnwindBB
, Ops
, OperandBundles
);
4159 OperandBundles
.clear();
4160 InstructionList
.push_back(I
);
4161 cast
<InvokeInst
>(I
)->setCallingConv(
4162 static_cast<CallingConv::ID
>(CallingConv::MaxID
& CCInfo
));
4163 cast
<InvokeInst
>(I
)->setAttributes(PAL
);
4166 case bitc::FUNC_CODE_INST_RESUME
: { // RESUME: [opval]
4168 Value
*Val
= nullptr;
4169 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
4170 return error("Invalid record");
4171 I
= ResumeInst::Create(Val
);
4172 InstructionList
.push_back(I
);
4175 case bitc::FUNC_CODE_INST_UNREACHABLE
: // UNREACHABLE
4176 I
= new UnreachableInst(Context
);
4177 InstructionList
.push_back(I
);
4179 case bitc::FUNC_CODE_INST_PHI
: { // PHI: [ty, val0,bb0, ...]
4180 if (Record
.size() < 1 || ((Record
.size()-1)&1))
4181 return error("Invalid record");
4182 Type
*Ty
= getTypeByID(Record
[0]);
4184 return error("Invalid record");
4186 PHINode
*PN
= PHINode::Create(Ty
, (Record
.size()-1)/2);
4187 InstructionList
.push_back(PN
);
4189 for (unsigned i
= 0, e
= Record
.size()-1; i
!= e
; i
+= 2) {
4191 // With the new function encoding, it is possible that operands have
4192 // negative IDs (for forward references). Use a signed VBR
4193 // representation to keep the encoding small.
4195 V
= getValueSigned(Record
, 1+i
, NextValueNo
, Ty
);
4197 V
= getValue(Record
, 1+i
, NextValueNo
, Ty
);
4198 BasicBlock
*BB
= getBasicBlock(Record
[2+i
]);
4200 return error("Invalid record");
4201 PN
->addIncoming(V
, BB
);
4207 case bitc::FUNC_CODE_INST_LANDINGPAD
:
4208 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD
: {
4209 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4211 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD
) {
4212 if (Record
.size() < 3)
4213 return error("Invalid record");
4215 assert(BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
);
4216 if (Record
.size() < 4)
4217 return error("Invalid record");
4219 Type
*Ty
= getTypeByID(Record
[Idx
++]);
4221 return error("Invalid record");
4222 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
) {
4223 Value
*PersFn
= nullptr;
4224 if (getValueTypePair(Record
, Idx
, NextValueNo
, PersFn
))
4225 return error("Invalid record");
4227 if (!F
->hasPersonalityFn())
4228 F
->setPersonalityFn(cast
<Constant
>(PersFn
));
4229 else if (F
->getPersonalityFn() != cast
<Constant
>(PersFn
))
4230 return error("Personality function mismatch");
4233 bool IsCleanup
= !!Record
[Idx
++];
4234 unsigned NumClauses
= Record
[Idx
++];
4235 LandingPadInst
*LP
= LandingPadInst::Create(Ty
, NumClauses
);
4236 LP
->setCleanup(IsCleanup
);
4237 for (unsigned J
= 0; J
!= NumClauses
; ++J
) {
4238 LandingPadInst::ClauseType CT
=
4239 LandingPadInst::ClauseType(Record
[Idx
++]); (void)CT
;
4242 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
)) {
4244 return error("Invalid record");
4247 assert((CT
!= LandingPadInst::Catch
||
4248 !isa
<ArrayType
>(Val
->getType())) &&
4249 "Catch clause has a invalid type!");
4250 assert((CT
!= LandingPadInst::Filter
||
4251 isa
<ArrayType
>(Val
->getType())) &&
4252 "Filter clause has invalid type!");
4253 LP
->addClause(cast
<Constant
>(Val
));
4257 InstructionList
.push_back(I
);
4261 case bitc::FUNC_CODE_INST_ALLOCA
: { // ALLOCA: [instty, opty, op, align]
4262 if (Record
.size() != 4)
4263 return error("Invalid record");
4264 uint64_t AlignRecord
= Record
[3];
4265 const uint64_t InAllocaMask
= uint64_t(1) << 5;
4266 const uint64_t ExplicitTypeMask
= uint64_t(1) << 6;
4267 const uint64_t SwiftErrorMask
= uint64_t(1) << 7;
4268 const uint64_t FlagMask
= InAllocaMask
| ExplicitTypeMask
|
4270 bool InAlloca
= AlignRecord
& InAllocaMask
;
4271 bool SwiftError
= AlignRecord
& SwiftErrorMask
;
4272 Type
*Ty
= getTypeByID(Record
[0]);
4273 if ((AlignRecord
& ExplicitTypeMask
) == 0) {
4274 auto *PTy
= dyn_cast_or_null
<PointerType
>(Ty
);
4276 return error("Old-style alloca with a non-pointer type");
4277 Ty
= PTy
->getElementType();
4279 Type
*OpTy
= getTypeByID(Record
[1]);
4280 Value
*Size
= getFnValueByID(Record
[2], OpTy
);
4282 if (Error Err
= parseAlignmentValue(AlignRecord
& ~FlagMask
, Align
)) {
4286 return error("Invalid record");
4288 // FIXME: Make this an optional field.
4289 const DataLayout
&DL
= TheModule
->getDataLayout();
4290 unsigned AS
= DL
.getAllocaAddrSpace();
4292 AllocaInst
*AI
= new AllocaInst(Ty
, AS
, Size
, Align
);
4293 AI
->setUsedWithInAlloca(InAlloca
);
4294 AI
->setSwiftError(SwiftError
);
4296 InstructionList
.push_back(I
);
4299 case bitc::FUNC_CODE_INST_LOAD
: { // LOAD: [opty, op, align, vol]
4302 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
4303 (OpNum
+ 2 != Record
.size() && OpNum
+ 3 != Record
.size()))
4304 return error("Invalid record");
4307 if (OpNum
+ 3 == Record
.size())
4308 Ty
= getTypeByID(Record
[OpNum
++]);
4309 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4312 Ty
= cast
<PointerType
>(Op
->getType())->getElementType();
4315 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4317 I
= new LoadInst(Ty
, Op
, "", Record
[OpNum
+ 1], Align
);
4319 InstructionList
.push_back(I
);
4322 case bitc::FUNC_CODE_INST_LOADATOMIC
: {
4323 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4326 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
4327 (OpNum
+ 4 != Record
.size() && OpNum
+ 5 != Record
.size()))
4328 return error("Invalid record");
4331 if (OpNum
+ 5 == Record
.size())
4332 Ty
= getTypeByID(Record
[OpNum
++]);
4333 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4336 Ty
= cast
<PointerType
>(Op
->getType())->getElementType();
4338 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4339 if (Ordering
== AtomicOrdering::NotAtomic
||
4340 Ordering
== AtomicOrdering::Release
||
4341 Ordering
== AtomicOrdering::AcquireRelease
)
4342 return error("Invalid record");
4343 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
4344 return error("Invalid record");
4345 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4348 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4350 I
= new LoadInst(Op
, "", Record
[OpNum
+1], Align
, Ordering
, SSID
);
4352 InstructionList
.push_back(I
);
4355 case bitc::FUNC_CODE_INST_STORE
:
4356 case bitc::FUNC_CODE_INST_STORE_OLD
: { // STORE2:[ptrty, ptr, val, align, vol]
4359 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
4360 (BitCode
== bitc::FUNC_CODE_INST_STORE
4361 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
4362 : popValue(Record
, OpNum
, NextValueNo
,
4363 cast
<PointerType
>(Ptr
->getType())->getElementType(),
4365 OpNum
+ 2 != Record
.size())
4366 return error("Invalid record");
4368 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
4371 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4373 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+1], Align
);
4374 InstructionList
.push_back(I
);
4377 case bitc::FUNC_CODE_INST_STOREATOMIC
:
4378 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD
: {
4379 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
4382 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
4383 !isa
<PointerType
>(Ptr
->getType()) ||
4384 (BitCode
== bitc::FUNC_CODE_INST_STOREATOMIC
4385 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
4386 : popValue(Record
, OpNum
, NextValueNo
,
4387 cast
<PointerType
>(Ptr
->getType())->getElementType(),
4389 OpNum
+ 4 != Record
.size())
4390 return error("Invalid record");
4392 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
4394 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4395 if (Ordering
== AtomicOrdering::NotAtomic
||
4396 Ordering
== AtomicOrdering::Acquire
||
4397 Ordering
== AtomicOrdering::AcquireRelease
)
4398 return error("Invalid record");
4399 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4400 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
4401 return error("Invalid record");
4404 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4406 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+1], Align
, Ordering
, SSID
);
4407 InstructionList
.push_back(I
);
4410 case bitc::FUNC_CODE_INST_CMPXCHG_OLD
:
4411 case bitc::FUNC_CODE_INST_CMPXCHG
: {
4412 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, ssid,
4413 // failureordering?, isweak?]
4415 Value
*Ptr
, *Cmp
, *New
;
4416 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
4417 (BitCode
== bitc::FUNC_CODE_INST_CMPXCHG
4418 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Cmp
)
4419 : popValue(Record
, OpNum
, NextValueNo
,
4420 cast
<PointerType
>(Ptr
->getType())->getElementType(),
4422 popValue(Record
, OpNum
, NextValueNo
, Cmp
->getType(), New
) ||
4423 Record
.size() < OpNum
+ 3 || Record
.size() > OpNum
+ 5)
4424 return error("Invalid record");
4425 AtomicOrdering SuccessOrdering
= getDecodedOrdering(Record
[OpNum
+ 1]);
4426 if (SuccessOrdering
== AtomicOrdering::NotAtomic
||
4427 SuccessOrdering
== AtomicOrdering::Unordered
)
4428 return error("Invalid record");
4429 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 2]);
4431 if (Error Err
= typeCheckLoadStoreInst(Cmp
->getType(), Ptr
->getType()))
4433 AtomicOrdering FailureOrdering
;
4434 if (Record
.size() < 7)
4436 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering
);
4438 FailureOrdering
= getDecodedOrdering(Record
[OpNum
+ 3]);
4440 I
= new AtomicCmpXchgInst(Ptr
, Cmp
, New
, SuccessOrdering
, FailureOrdering
,
4442 cast
<AtomicCmpXchgInst
>(I
)->setVolatile(Record
[OpNum
]);
4444 if (Record
.size() < 8) {
4445 // Before weak cmpxchgs existed, the instruction simply returned the
4446 // value loaded from memory, so bitcode files from that era will be
4447 // expecting the first component of a modern cmpxchg.
4448 CurBB
->getInstList().push_back(I
);
4449 I
= ExtractValueInst::Create(I
, 0);
4451 cast
<AtomicCmpXchgInst
>(I
)->setWeak(Record
[OpNum
+4]);
4454 InstructionList
.push_back(I
);
4457 case bitc::FUNC_CODE_INST_ATOMICRMW
: {
4458 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid]
4461 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
4462 !isa
<PointerType
>(Ptr
->getType()) ||
4463 popValue(Record
, OpNum
, NextValueNo
,
4464 cast
<PointerType
>(Ptr
->getType())->getElementType(), Val
) ||
4465 OpNum
+4 != Record
.size())
4466 return error("Invalid record");
4467 AtomicRMWInst::BinOp Operation
= getDecodedRMWOperation(Record
[OpNum
]);
4468 if (Operation
< AtomicRMWInst::FIRST_BINOP
||
4469 Operation
> AtomicRMWInst::LAST_BINOP
)
4470 return error("Invalid record");
4471 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4472 if (Ordering
== AtomicOrdering::NotAtomic
||
4473 Ordering
== AtomicOrdering::Unordered
)
4474 return error("Invalid record");
4475 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4476 I
= new AtomicRMWInst(Operation
, Ptr
, Val
, Ordering
, SSID
);
4477 cast
<AtomicRMWInst
>(I
)->setVolatile(Record
[OpNum
+1]);
4478 InstructionList
.push_back(I
);
4481 case bitc::FUNC_CODE_INST_FENCE
: { // FENCE:[ordering, ssid]
4482 if (2 != Record
.size())
4483 return error("Invalid record");
4484 AtomicOrdering Ordering
= getDecodedOrdering(Record
[0]);
4485 if (Ordering
== AtomicOrdering::NotAtomic
||
4486 Ordering
== AtomicOrdering::Unordered
||
4487 Ordering
== AtomicOrdering::Monotonic
)
4488 return error("Invalid record");
4489 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[1]);
4490 I
= new FenceInst(Context
, Ordering
, SSID
);
4491 InstructionList
.push_back(I
);
4494 case bitc::FUNC_CODE_INST_CALL
: {
4495 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
4496 if (Record
.size() < 3)
4497 return error("Invalid record");
4500 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4501 unsigned CCInfo
= Record
[OpNum
++];
4504 if ((CCInfo
>> bitc::CALL_FMF
) & 1) {
4505 FMF
= getDecodedFastMathFlags(Record
[OpNum
++]);
4507 return error("Fast math flags indicator set for call with no FMF");
4510 FunctionType
*FTy
= nullptr;
4511 if (CCInfo
>> bitc::CALL_EXPLICIT_TYPE
& 1 &&
4512 !(FTy
= dyn_cast
<FunctionType
>(getTypeByID(Record
[OpNum
++]))))
4513 return error("Explicit call type is not a function type");
4516 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
))
4517 return error("Invalid record");
4519 PointerType
*OpTy
= dyn_cast
<PointerType
>(Callee
->getType());
4521 return error("Callee is not a pointer type");
4523 FTy
= dyn_cast
<FunctionType
>(OpTy
->getElementType());
4525 return error("Callee is not of pointer to function type");
4526 } else if (OpTy
->getElementType() != FTy
)
4527 return error("Explicit call type does not match pointee type of "
4529 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4530 return error("Insufficient operands to call");
4532 SmallVector
<Value
*, 16> Args
;
4533 // Read the fixed params.
4534 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4535 if (FTy
->getParamType(i
)->isLabelTy())
4536 Args
.push_back(getBasicBlock(Record
[OpNum
]));
4538 Args
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4539 FTy
->getParamType(i
)));
4541 return error("Invalid record");
4544 // Read type/value pairs for varargs params.
4545 if (!FTy
->isVarArg()) {
4546 if (OpNum
!= Record
.size())
4547 return error("Invalid record");
4549 while (OpNum
!= Record
.size()) {
4551 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4552 return error("Invalid record");
4557 I
= CallInst::Create(FTy
, Callee
, Args
, OperandBundles
);
4558 OperandBundles
.clear();
4559 InstructionList
.push_back(I
);
4560 cast
<CallInst
>(I
)->setCallingConv(
4561 static_cast<CallingConv::ID
>((0x7ff & CCInfo
) >> bitc::CALL_CCONV
));
4562 CallInst::TailCallKind TCK
= CallInst::TCK_None
;
4563 if (CCInfo
& 1 << bitc::CALL_TAIL
)
4564 TCK
= CallInst::TCK_Tail
;
4565 if (CCInfo
& (1 << bitc::CALL_MUSTTAIL
))
4566 TCK
= CallInst::TCK_MustTail
;
4567 if (CCInfo
& (1 << bitc::CALL_NOTAIL
))
4568 TCK
= CallInst::TCK_NoTail
;
4569 cast
<CallInst
>(I
)->setTailCallKind(TCK
);
4570 cast
<CallInst
>(I
)->setAttributes(PAL
);
4572 if (!isa
<FPMathOperator
>(I
))
4573 return error("Fast-math-flags specified for call without "
4574 "floating-point scalar or vector return type");
4575 I
->setFastMathFlags(FMF
);
4579 case bitc::FUNC_CODE_INST_VAARG
: { // VAARG: [valistty, valist, instty]
4580 if (Record
.size() < 3)
4581 return error("Invalid record");
4582 Type
*OpTy
= getTypeByID(Record
[0]);
4583 Value
*Op
= getValue(Record
, 1, NextValueNo
, OpTy
);
4584 Type
*ResTy
= getTypeByID(Record
[2]);
4585 if (!OpTy
|| !Op
|| !ResTy
)
4586 return error("Invalid record");
4587 I
= new VAArgInst(Op
, ResTy
);
4588 InstructionList
.push_back(I
);
4592 case bitc::FUNC_CODE_OPERAND_BUNDLE
: {
4593 // A call or an invoke can be optionally prefixed with some variable
4594 // number of operand bundle blocks. These blocks are read into
4595 // OperandBundles and consumed at the next call or invoke instruction.
4597 if (Record
.size() < 1 || Record
[0] >= BundleTags
.size())
4598 return error("Invalid record");
4600 std::vector
<Value
*> Inputs
;
4603 while (OpNum
!= Record
.size()) {
4605 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4606 return error("Invalid record");
4607 Inputs
.push_back(Op
);
4610 OperandBundles
.emplace_back(BundleTags
[Record
[0]], std::move(Inputs
));
4615 // Add instruction to end of current BB. If there is no current BB, reject
4619 return error("Invalid instruction with no BB");
4621 if (!OperandBundles
.empty()) {
4623 return error("Operand bundles found with no consumer");
4625 CurBB
->getInstList().push_back(I
);
4627 // If this was a terminator instruction, move to the next block.
4628 if (I
->isTerminator()) {
4630 CurBB
= CurBBNo
< FunctionBBs
.size() ? FunctionBBs
[CurBBNo
] : nullptr;
4633 // Non-void values get registered in the value table for future use.
4634 if (I
&& !I
->getType()->isVoidTy())
4635 ValueList
.assignValue(I
, NextValueNo
++);
4640 if (!OperandBundles
.empty())
4641 return error("Operand bundles found with no consumer");
4643 // Check the function list for unresolved values.
4644 if (Argument
*A
= dyn_cast
<Argument
>(ValueList
.back())) {
4645 if (!A
->getParent()) {
4646 // We found at least one unresolved value. Nuke them all to avoid leaks.
4647 for (unsigned i
= ModuleValueListSize
, e
= ValueList
.size(); i
!= e
; ++i
){
4648 if ((A
= dyn_cast_or_null
<Argument
>(ValueList
[i
])) && !A
->getParent()) {
4649 A
->replaceAllUsesWith(UndefValue::get(A
->getType()));
4653 return error("Never resolved value found in function");
4657 // Unexpected unresolved metadata about to be dropped.
4658 if (MDLoader
->hasFwdRefs())
4659 return error("Invalid function metadata: outgoing forward refs");
4661 // Trim the value list down to the size it was before we parsed this function.
4662 ValueList
.shrinkTo(ModuleValueListSize
);
4663 MDLoader
->shrinkTo(ModuleMDLoaderSize
);
4664 std::vector
<BasicBlock
*>().swap(FunctionBBs
);
4665 return Error::success();
4668 /// Find the function body in the bitcode stream
4669 Error
BitcodeReader::findFunctionInStream(
4671 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
) {
4672 while (DeferredFunctionInfoIterator
->second
== 0) {
4673 // This is the fallback handling for the old format bitcode that
4674 // didn't contain the function index in the VST, or when we have
4675 // an anonymous function which would not have a VST entry.
4676 // Assert that we have one of those two cases.
4677 assert(VSTOffset
== 0 || !F
->hasName());
4678 // Parse the next body in the stream and set its position in the
4679 // DeferredFunctionInfo map.
4680 if (Error Err
= rememberAndSkipFunctionBodies())
4683 return Error::success();
4686 SyncScope::ID
BitcodeReader::getDecodedSyncScopeID(unsigned Val
) {
4687 if (Val
== SyncScope::SingleThread
|| Val
== SyncScope::System
)
4688 return SyncScope::ID(Val
);
4689 if (Val
>= SSIDs
.size())
4690 return SyncScope::System
; // Map unknown synchronization scopes to system.
4694 //===----------------------------------------------------------------------===//
4695 // GVMaterializer implementation
4696 //===----------------------------------------------------------------------===//
4698 Error
BitcodeReader::materialize(GlobalValue
*GV
) {
4699 Function
*F
= dyn_cast
<Function
>(GV
);
4700 // If it's not a function or is already material, ignore the request.
4701 if (!F
|| !F
->isMaterializable())
4702 return Error::success();
4704 DenseMap
<Function
*, uint64_t>::iterator DFII
= DeferredFunctionInfo
.find(F
);
4705 assert(DFII
!= DeferredFunctionInfo
.end() && "Deferred function not found!");
4706 // If its position is recorded as 0, its body is somewhere in the stream
4707 // but we haven't seen it yet.
4708 if (DFII
->second
== 0)
4709 if (Error Err
= findFunctionInStream(F
, DFII
))
4712 // Materialize metadata before parsing any function bodies.
4713 if (Error Err
= materializeMetadata())
4716 // Move the bit stream to the saved position of the deferred function body.
4717 Stream
.JumpToBit(DFII
->second
);
4719 if (Error Err
= parseFunctionBody(F
))
4721 F
->setIsMaterializable(false);
4726 // Upgrade any old intrinsic calls in the function.
4727 for (auto &I
: UpgradedIntrinsics
) {
4728 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
4732 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
4733 UpgradeIntrinsicCall(CI
, I
.second
);
4737 // Update calls to the remangled intrinsics
4738 for (auto &I
: RemangledIntrinsics
)
4739 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
4741 // Don't expect any other users than call sites
4742 CallSite(*UI
++).setCalledFunction(I
.second
);
4744 // Finish fn->subprogram upgrade for materialized functions.
4745 if (DISubprogram
*SP
= MDLoader
->lookupSubprogramForFunction(F
))
4746 F
->setSubprogram(SP
);
4748 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
4749 if (!MDLoader
->isStrippingTBAA()) {
4750 for (auto &I
: instructions(F
)) {
4751 MDNode
*TBAA
= I
.getMetadata(LLVMContext::MD_tbaa
);
4752 if (!TBAA
|| TBAAVerifyHelper
.visitTBAAMetadata(I
, TBAA
))
4754 MDLoader
->setStripTBAA(true);
4755 stripTBAA(F
->getParent());
4759 // Bring in any functions that this function forward-referenced via
4761 return materializeForwardReferencedFunctions();
4764 Error
BitcodeReader::materializeModule() {
4765 if (Error Err
= materializeMetadata())
4768 // Promise to materialize all forward references.
4769 WillMaterializeAllForwardRefs
= true;
4771 // Iterate over the module, deserializing any functions that are still on
4773 for (Function
&F
: *TheModule
) {
4774 if (Error Err
= materialize(&F
))
4777 // At this point, if there are any function bodies, parse the rest of
4778 // the bits in the module past the last function block we have recorded
4779 // through either lazy scanning or the VST.
4780 if (LastFunctionBlockBit
|| NextUnreadBit
)
4781 if (Error Err
= parseModule(LastFunctionBlockBit
> NextUnreadBit
4782 ? LastFunctionBlockBit
4786 // Check that all block address forward references got resolved (as we
4788 if (!BasicBlockFwdRefs
.empty())
4789 return error("Never resolved function from blockaddress");
4791 // Upgrade any intrinsic calls that slipped through (should not happen!) and
4792 // delete the old functions to clean up. We can't do this unless the entire
4793 // module is materialized because there could always be another function body
4794 // with calls to the old function.
4795 for (auto &I
: UpgradedIntrinsics
) {
4796 for (auto *U
: I
.first
->users()) {
4797 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
4798 UpgradeIntrinsicCall(CI
, I
.second
);
4800 if (!I
.first
->use_empty())
4801 I
.first
->replaceAllUsesWith(I
.second
);
4802 I
.first
->eraseFromParent();
4804 UpgradedIntrinsics
.clear();
4805 // Do the same for remangled intrinsics
4806 for (auto &I
: RemangledIntrinsics
) {
4807 I
.first
->replaceAllUsesWith(I
.second
);
4808 I
.first
->eraseFromParent();
4810 RemangledIntrinsics
.clear();
4812 UpgradeDebugInfo(*TheModule
);
4814 UpgradeModuleFlags(*TheModule
);
4816 UpgradeRetainReleaseMarker(*TheModule
);
4818 return Error::success();
4821 std::vector
<StructType
*> BitcodeReader::getIdentifiedStructTypes() const {
4822 return IdentifiedStructTypes
;
4825 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
4826 BitstreamCursor Cursor
, StringRef Strtab
, ModuleSummaryIndex
&TheIndex
,
4827 StringRef ModulePath
, unsigned ModuleId
)
4828 : BitcodeReaderBase(std::move(Cursor
), Strtab
), TheIndex(TheIndex
),
4829 ModulePath(ModulePath
), ModuleId(ModuleId
) {}
4831 void ModuleSummaryIndexBitcodeReader::addThisModule() {
4832 TheIndex
.addModule(ModulePath
, ModuleId
);
4835 ModuleSummaryIndex::ModuleInfo
*
4836 ModuleSummaryIndexBitcodeReader::getThisModule() {
4837 return TheIndex
.getModule(ModulePath
);
4840 std::pair
<ValueInfo
, GlobalValue::GUID
>
4841 ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId
) {
4842 auto VGI
= ValueIdToValueInfoMap
[ValueId
];
4847 void ModuleSummaryIndexBitcodeReader::setValueGUID(
4848 uint64_t ValueID
, StringRef ValueName
, GlobalValue::LinkageTypes Linkage
,
4849 StringRef SourceFileName
) {
4850 std::string GlobalId
=
4851 GlobalValue::getGlobalIdentifier(ValueName
, Linkage
, SourceFileName
);
4852 auto ValueGUID
= GlobalValue::getGUID(GlobalId
);
4853 auto OriginalNameID
= ValueGUID
;
4854 if (GlobalValue::isLocalLinkage(Linkage
))
4855 OriginalNameID
= GlobalValue::getGUID(ValueName
);
4856 if (PrintSummaryGUIDs
)
4857 dbgs() << "GUID " << ValueGUID
<< "(" << OriginalNameID
<< ") is "
4858 << ValueName
<< "\n";
4860 // UseStrtab is false for legacy summary formats and value names are
4861 // created on stack. In that case we save the name in a string saver in
4862 // the index so that the value name can be recorded.
4863 ValueIdToValueInfoMap
[ValueID
] = std::make_pair(
4864 TheIndex
.getOrInsertValueInfo(
4866 UseStrtab
? ValueName
: TheIndex
.saveString(ValueName
.str())),
4870 // Specialized value symbol table parser used when reading module index
4871 // blocks where we don't actually create global values. The parsed information
4872 // is saved in the bitcode reader for use when later parsing summaries.
4873 Error
ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
4875 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
) {
4876 // With a strtab the VST is not required to parse the summary.
4878 return Error::success();
4880 assert(Offset
> 0 && "Expected non-zero VST offset");
4881 uint64_t CurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
4883 if (Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
4884 return error("Invalid record");
4886 SmallVector
<uint64_t, 64> Record
;
4888 // Read all the records for this value table.
4889 SmallString
<128> ValueName
;
4892 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
4894 switch (Entry
.Kind
) {
4895 case BitstreamEntry::SubBlock
: // Handled for us already.
4896 case BitstreamEntry::Error
:
4897 return error("Malformed block");
4898 case BitstreamEntry::EndBlock
:
4899 // Done parsing VST, jump back to wherever we came from.
4900 Stream
.JumpToBit(CurrentBit
);
4901 return Error::success();
4902 case BitstreamEntry::Record
:
4903 // The interesting case.
4909 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
4910 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
4912 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
4913 if (convertToString(Record
, 1, ValueName
))
4914 return error("Invalid record");
4915 unsigned ValueID
= Record
[0];
4916 assert(!SourceFileName
.empty());
4917 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
4918 assert(VLI
!= ValueIdToLinkageMap
.end() &&
4919 "No linkage found for VST entry?");
4920 auto Linkage
= VLI
->second
;
4921 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
4925 case bitc::VST_CODE_FNENTRY
: {
4926 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
4927 if (convertToString(Record
, 2, ValueName
))
4928 return error("Invalid record");
4929 unsigned ValueID
= Record
[0];
4930 assert(!SourceFileName
.empty());
4931 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
4932 assert(VLI
!= ValueIdToLinkageMap
.end() &&
4933 "No linkage found for VST entry?");
4934 auto Linkage
= VLI
->second
;
4935 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
4939 case bitc::VST_CODE_COMBINED_ENTRY
: {
4940 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
4941 unsigned ValueID
= Record
[0];
4942 GlobalValue::GUID RefGUID
= Record
[1];
4943 // The "original name", which is the second value of the pair will be
4944 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
4945 ValueIdToValueInfoMap
[ValueID
] =
4946 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
4953 // Parse just the blocks needed for building the index out of the module.
4954 // At the end of this routine the module Index is populated with a map
4955 // from global value id to GlobalValueSummary objects.
4956 Error
ModuleSummaryIndexBitcodeReader::parseModule() {
4957 if (Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
4958 return error("Invalid record");
4960 SmallVector
<uint64_t, 64> Record
;
4961 DenseMap
<unsigned, GlobalValue::LinkageTypes
> ValueIdToLinkageMap
;
4962 unsigned ValueId
= 0;
4964 // Read the index for this module.
4966 BitstreamEntry Entry
= Stream
.advance();
4968 switch (Entry
.Kind
) {
4969 case BitstreamEntry::Error
:
4970 return error("Malformed block");
4971 case BitstreamEntry::EndBlock
:
4972 return Error::success();
4974 case BitstreamEntry::SubBlock
:
4976 default: // Skip unknown content.
4977 if (Stream
.SkipBlock())
4978 return error("Invalid record");
4980 case bitc::BLOCKINFO_BLOCK_ID
:
4981 // Need to parse these to get abbrev ids (e.g. for VST)
4982 if (readBlockInfo())
4983 return error("Malformed block");
4985 case bitc::VALUE_SYMTAB_BLOCK_ID
:
4986 // Should have been parsed earlier via VSTOffset, unless there
4987 // is no summary section.
4988 assert(((SeenValueSymbolTable
&& VSTOffset
> 0) ||
4989 !SeenGlobalValSummary
) &&
4990 "Expected early VST parse via VSTOffset record");
4991 if (Stream
.SkipBlock())
4992 return error("Invalid record");
4994 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID
:
4995 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
:
4996 // Add the module if it is a per-module index (has a source file name).
4997 if (!SourceFileName
.empty())
4999 assert(!SeenValueSymbolTable
&&
5000 "Already read VST when parsing summary block?");
5001 // We might not have a VST if there were no values in the
5002 // summary. An empty summary block generated when we are
5003 // performing ThinLTO compiles so we don't later invoke
5004 // the regular LTO process on them.
5005 if (VSTOffset
> 0) {
5006 if (Error Err
= parseValueSymbolTable(VSTOffset
, ValueIdToLinkageMap
))
5008 SeenValueSymbolTable
= true;
5010 SeenGlobalValSummary
= true;
5011 if (Error Err
= parseEntireSummary(Entry
.ID
))
5014 case bitc::MODULE_STRTAB_BLOCK_ID
:
5015 if (Error Err
= parseModuleStringTable())
5021 case BitstreamEntry::Record
: {
5023 auto BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
5026 break; // Default behavior, ignore unknown content.
5027 case bitc::MODULE_CODE_VERSION
: {
5028 if (Error Err
= parseVersionRecord(Record
).takeError())
5032 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5033 case bitc::MODULE_CODE_SOURCE_FILENAME
: {
5034 SmallString
<128> ValueName
;
5035 if (convertToString(Record
, 0, ValueName
))
5036 return error("Invalid record");
5037 SourceFileName
= ValueName
.c_str();
5040 /// MODULE_CODE_HASH: [5*i32]
5041 case bitc::MODULE_CODE_HASH
: {
5042 if (Record
.size() != 5)
5043 return error("Invalid hash length " + Twine(Record
.size()).str());
5044 auto &Hash
= getThisModule()->second
.second
;
5046 for (auto &Val
: Record
) {
5047 assert(!(Val
>> 32) && "Unexpected high bits set");
5052 /// MODULE_CODE_VSTOFFSET: [offset]
5053 case bitc::MODULE_CODE_VSTOFFSET
:
5054 if (Record
.size() < 1)
5055 return error("Invalid record");
5056 // Note that we subtract 1 here because the offset is relative to one
5057 // word before the start of the identification or module block, which
5058 // was historically always the start of the regular bitcode header.
5059 VSTOffset
= Record
[0] - 1;
5061 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
5062 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
5063 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
5064 // v2: [strtab offset, strtab size, v1]
5065 case bitc::MODULE_CODE_GLOBALVAR
:
5066 case bitc::MODULE_CODE_FUNCTION
:
5067 case bitc::MODULE_CODE_ALIAS
: {
5069 ArrayRef
<uint64_t> GVRecord
;
5070 std::tie(Name
, GVRecord
) = readNameFromStrtab(Record
);
5071 if (GVRecord
.size() <= 3)
5072 return error("Invalid record");
5073 uint64_t RawLinkage
= GVRecord
[3];
5074 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
5076 ValueIdToLinkageMap
[ValueId
++] = Linkage
;
5080 setValueGUID(ValueId
++, Name
, Linkage
, SourceFileName
);
5090 std::vector
<ValueInfo
>
5091 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef
<uint64_t> Record
) {
5092 std::vector
<ValueInfo
> Ret
;
5093 Ret
.reserve(Record
.size());
5094 for (uint64_t RefValueId
: Record
)
5095 Ret
.push_back(getValueInfoFromValueId(RefValueId
).first
);
5099 std::vector
<FunctionSummary::EdgeTy
>
5100 ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef
<uint64_t> Record
,
5101 bool IsOldProfileFormat
,
5102 bool HasProfile
, bool HasRelBF
) {
5103 std::vector
<FunctionSummary::EdgeTy
> Ret
;
5104 Ret
.reserve(Record
.size());
5105 for (unsigned I
= 0, E
= Record
.size(); I
!= E
; ++I
) {
5106 CalleeInfo::HotnessType Hotness
= CalleeInfo::HotnessType::Unknown
;
5108 ValueInfo Callee
= getValueInfoFromValueId(Record
[I
]).first
;
5109 if (IsOldProfileFormat
) {
5110 I
+= 1; // Skip old callsitecount field
5112 I
+= 1; // Skip old profilecount field
5113 } else if (HasProfile
)
5114 Hotness
= static_cast<CalleeInfo::HotnessType
>(Record
[++I
]);
5116 RelBF
= Record
[++I
];
5117 Ret
.push_back(FunctionSummary::EdgeTy
{Callee
, CalleeInfo(Hotness
, RelBF
)});
5123 parseWholeProgramDevirtResolutionByArg(ArrayRef
<uint64_t> Record
, size_t &Slot
,
5124 WholeProgramDevirtResolution
&Wpd
) {
5125 uint64_t ArgNum
= Record
[Slot
++];
5126 WholeProgramDevirtResolution::ByArg
&B
=
5127 Wpd
.ResByArg
[{Record
.begin() + Slot
, Record
.begin() + Slot
+ ArgNum
}];
5131 static_cast<WholeProgramDevirtResolution::ByArg::Kind
>(Record
[Slot
++]);
5132 B
.Info
= Record
[Slot
++];
5133 B
.Byte
= Record
[Slot
++];
5134 B
.Bit
= Record
[Slot
++];
5137 static void parseWholeProgramDevirtResolution(ArrayRef
<uint64_t> Record
,
5138 StringRef Strtab
, size_t &Slot
,
5139 TypeIdSummary
&TypeId
) {
5140 uint64_t Id
= Record
[Slot
++];
5141 WholeProgramDevirtResolution
&Wpd
= TypeId
.WPDRes
[Id
];
5143 Wpd
.TheKind
= static_cast<WholeProgramDevirtResolution::Kind
>(Record
[Slot
++]);
5144 Wpd
.SingleImplName
= {Strtab
.data() + Record
[Slot
],
5145 static_cast<size_t>(Record
[Slot
+ 1])};
5148 uint64_t ResByArgNum
= Record
[Slot
++];
5149 for (uint64_t I
= 0; I
!= ResByArgNum
; ++I
)
5150 parseWholeProgramDevirtResolutionByArg(Record
, Slot
, Wpd
);
5153 static void parseTypeIdSummaryRecord(ArrayRef
<uint64_t> Record
,
5155 ModuleSummaryIndex
&TheIndex
) {
5157 TypeIdSummary
&TypeId
= TheIndex
.getOrInsertTypeIdSummary(
5158 {Strtab
.data() + Record
[Slot
], static_cast<size_t>(Record
[Slot
+ 1])});
5161 TypeId
.TTRes
.TheKind
= static_cast<TypeTestResolution::Kind
>(Record
[Slot
++]);
5162 TypeId
.TTRes
.SizeM1BitWidth
= Record
[Slot
++];
5163 TypeId
.TTRes
.AlignLog2
= Record
[Slot
++];
5164 TypeId
.TTRes
.SizeM1
= Record
[Slot
++];
5165 TypeId
.TTRes
.BitMask
= Record
[Slot
++];
5166 TypeId
.TTRes
.InlineBits
= Record
[Slot
++];
5168 while (Slot
< Record
.size())
5169 parseWholeProgramDevirtResolution(Record
, Strtab
, Slot
, TypeId
);
5172 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
5173 // objects in the index.
5174 Error
ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID
) {
5175 if (Stream
.EnterSubBlock(ID
))
5176 return error("Invalid record");
5177 SmallVector
<uint64_t, 64> Record
;
5181 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
5182 if (Entry
.Kind
!= BitstreamEntry::Record
)
5183 return error("Invalid Summary Block: record for version expected");
5184 if (Stream
.readRecord(Entry
.ID
, Record
) != bitc::FS_VERSION
)
5185 return error("Invalid Summary Block: version expected");
5187 const uint64_t Version
= Record
[0];
5188 const bool IsOldProfileFormat
= Version
== 1;
5189 if (Version
< 1 || Version
> 4)
5190 return error("Invalid summary version " + Twine(Version
) +
5191 ", 1, 2, 3 or 4 expected");
5194 // Keep around the last seen summary to be used when we see an optional
5195 // "OriginalName" attachement.
5196 GlobalValueSummary
*LastSeenSummary
= nullptr;
5197 GlobalValue::GUID LastSeenGUID
= 0;
5199 // We can expect to see any number of type ID information records before
5200 // each function summary records; these variables store the information
5201 // collected so far so that it can be used to create the summary object.
5202 std::vector
<GlobalValue::GUID
> PendingTypeTests
;
5203 std::vector
<FunctionSummary::VFuncId
> PendingTypeTestAssumeVCalls
,
5204 PendingTypeCheckedLoadVCalls
;
5205 std::vector
<FunctionSummary::ConstVCall
> PendingTypeTestAssumeConstVCalls
,
5206 PendingTypeCheckedLoadConstVCalls
;
5209 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
5211 switch (Entry
.Kind
) {
5212 case BitstreamEntry::SubBlock
: // Handled for us already.
5213 case BitstreamEntry::Error
:
5214 return error("Malformed block");
5215 case BitstreamEntry::EndBlock
:
5216 return Error::success();
5217 case BitstreamEntry::Record
:
5218 // The interesting case.
5222 // Read a record. The record format depends on whether this
5223 // is a per-module index or a combined index file. In the per-module
5224 // case the records contain the associated value's ID for correlation
5225 // with VST entries. In the combined index the correlation is done
5226 // via the bitcode offset of the summary records (which were saved
5227 // in the combined index VST entries). The records also contain
5228 // information used for ThinLTO renaming and importing.
5230 auto BitCode
= Stream
.readRecord(Entry
.ID
, Record
);
5232 default: // Default behavior: ignore.
5234 case bitc::FS_FLAGS
: { // [flags]
5235 uint64_t Flags
= Record
[0];
5236 // Scan flags (set only on the combined index).
5237 assert(Flags
<= 0x3 && "Unexpected bits in flag");
5239 // 1 bit: WithGlobalValueDeadStripping flag.
5241 TheIndex
.setWithGlobalValueDeadStripping();
5242 // 1 bit: SkipModuleByDistributedBackend flag.
5244 TheIndex
.setSkipModuleByDistributedBackend();
5247 case bitc::FS_VALUE_GUID
: { // [valueid, refguid]
5248 uint64_t ValueID
= Record
[0];
5249 GlobalValue::GUID RefGUID
= Record
[1];
5250 ValueIdToValueInfoMap
[ValueID
] =
5251 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
5254 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,
5255 // numrefs x valueid, n x (valueid)]
5256 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,
5257 // numrefs x valueid,
5258 // n x (valueid, hotness)]
5259 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,
5260 // numrefs x valueid,
5261 // n x (valueid, relblockfreq)]
5262 case bitc::FS_PERMODULE
:
5263 case bitc::FS_PERMODULE_RELBF
:
5264 case bitc::FS_PERMODULE_PROFILE
: {
5265 unsigned ValueID
= Record
[0];
5266 uint64_t RawFlags
= Record
[1];
5267 unsigned InstCount
= Record
[2];
5268 uint64_t RawFunFlags
= 0;
5269 unsigned NumRefs
= Record
[3];
5270 int RefListStartIndex
= 4;
5272 RawFunFlags
= Record
[3];
5273 NumRefs
= Record
[4];
5274 RefListStartIndex
= 5;
5277 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5278 // The module path string ref set in the summary must be owned by the
5279 // index's module string table. Since we don't have a module path
5280 // string table section in the per-module index, we create a single
5281 // module path string table entry with an empty (0) ID to take
5283 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
5284 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
5285 "Record size inconsistent with number of references");
5286 std::vector
<ValueInfo
> Refs
= makeRefList(
5287 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
5288 bool HasProfile
= (BitCode
== bitc::FS_PERMODULE_PROFILE
);
5289 bool HasRelBF
= (BitCode
== bitc::FS_PERMODULE_RELBF
);
5290 std::vector
<FunctionSummary::EdgeTy
> Calls
= makeCallList(
5291 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
5292 IsOldProfileFormat
, HasProfile
, HasRelBF
);
5293 auto FS
= llvm::make_unique
<FunctionSummary
>(
5294 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), std::move(Refs
),
5295 std::move(Calls
), std::move(PendingTypeTests
),
5296 std::move(PendingTypeTestAssumeVCalls
),
5297 std::move(PendingTypeCheckedLoadVCalls
),
5298 std::move(PendingTypeTestAssumeConstVCalls
),
5299 std::move(PendingTypeCheckedLoadConstVCalls
));
5300 PendingTypeTests
.clear();
5301 PendingTypeTestAssumeVCalls
.clear();
5302 PendingTypeCheckedLoadVCalls
.clear();
5303 PendingTypeTestAssumeConstVCalls
.clear();
5304 PendingTypeCheckedLoadConstVCalls
.clear();
5305 auto VIAndOriginalGUID
= getValueInfoFromValueId(ValueID
);
5306 FS
->setModulePath(getThisModule()->first());
5307 FS
->setOriginalName(VIAndOriginalGUID
.second
);
5308 TheIndex
.addGlobalValueSummary(VIAndOriginalGUID
.first
, std::move(FS
));
5311 // FS_ALIAS: [valueid, flags, valueid]
5312 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
5313 // they expect all aliasee summaries to be available.
5314 case bitc::FS_ALIAS
: {
5315 unsigned ValueID
= Record
[0];
5316 uint64_t RawFlags
= Record
[1];
5317 unsigned AliaseeID
= Record
[2];
5318 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5319 auto AS
= llvm::make_unique
<AliasSummary
>(Flags
);
5320 // The module path string ref set in the summary must be owned by the
5321 // index's module string table. Since we don't have a module path
5322 // string table section in the per-module index, we create a single
5323 // module path string table entry with an empty (0) ID to take
5325 AS
->setModulePath(getThisModule()->first());
5327 GlobalValue::GUID AliaseeGUID
=
5328 getValueInfoFromValueId(AliaseeID
).first
.getGUID();
5329 auto AliaseeInModule
=
5330 TheIndex
.findSummaryInModule(AliaseeGUID
, ModulePath
);
5331 if (!AliaseeInModule
)
5332 return error("Alias expects aliasee summary to be parsed");
5333 AS
->setAliasee(AliaseeInModule
);
5334 AS
->setAliaseeGUID(AliaseeGUID
);
5336 auto GUID
= getValueInfoFromValueId(ValueID
);
5337 AS
->setOriginalName(GUID
.second
);
5338 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(AS
));
5341 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid]
5342 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS
: {
5343 unsigned ValueID
= Record
[0];
5344 uint64_t RawFlags
= Record
[1];
5345 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5346 std::vector
<ValueInfo
> Refs
=
5347 makeRefList(ArrayRef
<uint64_t>(Record
).slice(2));
5348 auto FS
= llvm::make_unique
<GlobalVarSummary
>(Flags
, std::move(Refs
));
5349 FS
->setModulePath(getThisModule()->first());
5350 auto GUID
= getValueInfoFromValueId(ValueID
);
5351 FS
->setOriginalName(GUID
.second
);
5352 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(FS
));
5355 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,
5356 // numrefs x valueid, n x (valueid)]
5357 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,
5358 // numrefs x valueid, n x (valueid, hotness)]
5359 case bitc::FS_COMBINED
:
5360 case bitc::FS_COMBINED_PROFILE
: {
5361 unsigned ValueID
= Record
[0];
5362 uint64_t ModuleId
= Record
[1];
5363 uint64_t RawFlags
= Record
[2];
5364 unsigned InstCount
= Record
[3];
5365 uint64_t RawFunFlags
= 0;
5366 unsigned NumRefs
= Record
[4];
5367 int RefListStartIndex
= 5;
5370 RawFunFlags
= Record
[4];
5371 NumRefs
= Record
[5];
5372 RefListStartIndex
= 6;
5375 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5376 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
5377 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
5378 "Record size inconsistent with number of references");
5379 std::vector
<ValueInfo
> Refs
= makeRefList(
5380 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
5381 bool HasProfile
= (BitCode
== bitc::FS_COMBINED_PROFILE
);
5382 std::vector
<FunctionSummary::EdgeTy
> Edges
= makeCallList(
5383 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
5384 IsOldProfileFormat
, HasProfile
, false);
5385 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
5386 auto FS
= llvm::make_unique
<FunctionSummary
>(
5387 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), std::move(Refs
),
5388 std::move(Edges
), std::move(PendingTypeTests
),
5389 std::move(PendingTypeTestAssumeVCalls
),
5390 std::move(PendingTypeCheckedLoadVCalls
),
5391 std::move(PendingTypeTestAssumeConstVCalls
),
5392 std::move(PendingTypeCheckedLoadConstVCalls
));
5393 PendingTypeTests
.clear();
5394 PendingTypeTestAssumeVCalls
.clear();
5395 PendingTypeCheckedLoadVCalls
.clear();
5396 PendingTypeTestAssumeConstVCalls
.clear();
5397 PendingTypeCheckedLoadConstVCalls
.clear();
5398 LastSeenSummary
= FS
.get();
5399 LastSeenGUID
= VI
.getGUID();
5400 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
5401 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
5404 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]
5405 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
5406 // they expect all aliasee summaries to be available.
5407 case bitc::FS_COMBINED_ALIAS
: {
5408 unsigned ValueID
= Record
[0];
5409 uint64_t ModuleId
= Record
[1];
5410 uint64_t RawFlags
= Record
[2];
5411 unsigned AliaseeValueId
= Record
[3];
5412 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5413 auto AS
= llvm::make_unique
<AliasSummary
>(Flags
);
5414 LastSeenSummary
= AS
.get();
5415 AS
->setModulePath(ModuleIdMap
[ModuleId
]);
5418 getValueInfoFromValueId(AliaseeValueId
).first
.getGUID();
5419 auto AliaseeInModule
=
5420 TheIndex
.findSummaryInModule(AliaseeGUID
, AS
->modulePath());
5421 AS
->setAliasee(AliaseeInModule
);
5422 AS
->setAliaseeGUID(AliaseeGUID
);
5424 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
5425 LastSeenGUID
= VI
.getGUID();
5426 TheIndex
.addGlobalValueSummary(VI
, std::move(AS
));
5429 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]
5430 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS
: {
5431 unsigned ValueID
= Record
[0];
5432 uint64_t ModuleId
= Record
[1];
5433 uint64_t RawFlags
= Record
[2];
5434 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5435 std::vector
<ValueInfo
> Refs
=
5436 makeRefList(ArrayRef
<uint64_t>(Record
).slice(3));
5437 auto FS
= llvm::make_unique
<GlobalVarSummary
>(Flags
, std::move(Refs
));
5438 LastSeenSummary
= FS
.get();
5439 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
5440 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
5441 LastSeenGUID
= VI
.getGUID();
5442 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
5445 // FS_COMBINED_ORIGINAL_NAME: [original_name]
5446 case bitc::FS_COMBINED_ORIGINAL_NAME
: {
5447 uint64_t OriginalName
= Record
[0];
5448 if (!LastSeenSummary
)
5449 return error("Name attachment that does not follow a combined record");
5450 LastSeenSummary
->setOriginalName(OriginalName
);
5451 TheIndex
.addOriginalName(LastSeenGUID
, OriginalName
);
5452 // Reset the LastSeenSummary
5453 LastSeenSummary
= nullptr;
5457 case bitc::FS_TYPE_TESTS
:
5458 assert(PendingTypeTests
.empty());
5459 PendingTypeTests
.insert(PendingTypeTests
.end(), Record
.begin(),
5463 case bitc::FS_TYPE_TEST_ASSUME_VCALLS
:
5464 assert(PendingTypeTestAssumeVCalls
.empty());
5465 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
5466 PendingTypeTestAssumeVCalls
.push_back({Record
[I
], Record
[I
+1]});
5469 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS
:
5470 assert(PendingTypeCheckedLoadVCalls
.empty());
5471 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
5472 PendingTypeCheckedLoadVCalls
.push_back({Record
[I
], Record
[I
+1]});
5475 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL
:
5476 PendingTypeTestAssumeConstVCalls
.push_back(
5477 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
5480 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL
:
5481 PendingTypeCheckedLoadConstVCalls
.push_back(
5482 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
5485 case bitc::FS_CFI_FUNCTION_DEFS
: {
5486 std::set
<std::string
> &CfiFunctionDefs
= TheIndex
.cfiFunctionDefs();
5487 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
5488 CfiFunctionDefs
.insert(
5489 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
5493 case bitc::FS_CFI_FUNCTION_DECLS
: {
5494 std::set
<std::string
> &CfiFunctionDecls
= TheIndex
.cfiFunctionDecls();
5495 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
5496 CfiFunctionDecls
.insert(
5497 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
5501 case bitc::FS_TYPE_ID
:
5502 parseTypeIdSummaryRecord(Record
, Strtab
, TheIndex
);
5506 llvm_unreachable("Exit infinite loop");
5509 // Parse the module string table block into the Index.
5510 // This populates the ModulePathStringTable map in the index.
5511 Error
ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
5512 if (Stream
.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID
))
5513 return error("Invalid record");
5515 SmallVector
<uint64_t, 64> Record
;
5517 SmallString
<128> ModulePath
;
5518 ModuleSummaryIndex::ModuleInfo
*LastSeenModule
= nullptr;
5521 BitstreamEntry Entry
= Stream
.advanceSkippingSubblocks();
5523 switch (Entry
.Kind
) {
5524 case BitstreamEntry::SubBlock
: // Handled for us already.
5525 case BitstreamEntry::Error
:
5526 return error("Malformed block");
5527 case BitstreamEntry::EndBlock
:
5528 return Error::success();
5529 case BitstreamEntry::Record
:
5530 // The interesting case.
5535 switch (Stream
.readRecord(Entry
.ID
, Record
)) {
5536 default: // Default behavior: ignore.
5538 case bitc::MST_CODE_ENTRY
: {
5539 // MST_ENTRY: [modid, namechar x N]
5540 uint64_t ModuleId
= Record
[0];
5542 if (convertToString(Record
, 1, ModulePath
))
5543 return error("Invalid record");
5545 LastSeenModule
= TheIndex
.addModule(ModulePath
, ModuleId
);
5546 ModuleIdMap
[ModuleId
] = LastSeenModule
->first();
5551 /// MST_CODE_HASH: [5*i32]
5552 case bitc::MST_CODE_HASH
: {
5553 if (Record
.size() != 5)
5554 return error("Invalid hash length " + Twine(Record
.size()).str());
5555 if (!LastSeenModule
)
5556 return error("Invalid hash that does not follow a module path");
5558 for (auto &Val
: Record
) {
5559 assert(!(Val
>> 32) && "Unexpected high bits set");
5560 LastSeenModule
->second
.second
[Pos
++] = Val
;
5562 // Reset LastSeenModule to avoid overriding the hash unexpectedly.
5563 LastSeenModule
= nullptr;
5568 llvm_unreachable("Exit infinite loop");
5573 // FIXME: This class is only here to support the transition to llvm::Error. It
5574 // will be removed once this transition is complete. Clients should prefer to
5575 // deal with the Error value directly, rather than converting to error_code.
5576 class BitcodeErrorCategoryType
: public std::error_category
{
5577 const char *name() const noexcept override
{
5578 return "llvm.bitcode";
5581 std::string
message(int IE
) const override
{
5582 BitcodeError E
= static_cast<BitcodeError
>(IE
);
5584 case BitcodeError::CorruptedBitcode
:
5585 return "Corrupted bitcode";
5587 llvm_unreachable("Unknown error type!");
5591 } // end anonymous namespace
5593 static ManagedStatic
<BitcodeErrorCategoryType
> ErrorCategory
;
5595 const std::error_category
&llvm::BitcodeErrorCategory() {
5596 return *ErrorCategory
;
5599 static Expected
<StringRef
> readBlobInRecord(BitstreamCursor
&Stream
,
5600 unsigned Block
, unsigned RecordID
) {
5601 if (Stream
.EnterSubBlock(Block
))
5602 return error("Invalid record");
5606 BitstreamEntry Entry
= Stream
.advance();
5607 switch (Entry
.Kind
) {
5608 case BitstreamEntry::EndBlock
:
5611 case BitstreamEntry::Error
:
5612 return error("Malformed block");
5614 case BitstreamEntry::SubBlock
:
5615 if (Stream
.SkipBlock())
5616 return error("Malformed block");
5619 case BitstreamEntry::Record
:
5621 SmallVector
<uint64_t, 1> Record
;
5622 if (Stream
.readRecord(Entry
.ID
, Record
, &Blob
) == RecordID
)
5629 //===----------------------------------------------------------------------===//
5630 // External interface
5631 //===----------------------------------------------------------------------===//
5633 Expected
<std::vector
<BitcodeModule
>>
5634 llvm::getBitcodeModuleList(MemoryBufferRef Buffer
) {
5635 auto FOrErr
= getBitcodeFileContents(Buffer
);
5637 return FOrErr
.takeError();
5638 return std::move(FOrErr
->Mods
);
5641 Expected
<BitcodeFileContents
>
5642 llvm::getBitcodeFileContents(MemoryBufferRef Buffer
) {
5643 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
5645 return StreamOrErr
.takeError();
5646 BitstreamCursor
&Stream
= *StreamOrErr
;
5648 BitcodeFileContents F
;
5650 uint64_t BCBegin
= Stream
.getCurrentByteNo();
5652 // We may be consuming bitcode from a client that leaves garbage at the end
5653 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
5654 // the end that there cannot possibly be another module, stop looking.
5655 if (BCBegin
+ 8 >= Stream
.getBitcodeBytes().size())
5658 BitstreamEntry Entry
= Stream
.advance();
5659 switch (Entry
.Kind
) {
5660 case BitstreamEntry::EndBlock
:
5661 case BitstreamEntry::Error
:
5662 return error("Malformed block");
5664 case BitstreamEntry::SubBlock
: {
5665 uint64_t IdentificationBit
= -1ull;
5666 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
) {
5667 IdentificationBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
5668 if (Stream
.SkipBlock())
5669 return error("Malformed block");
5671 Entry
= Stream
.advance();
5672 if (Entry
.Kind
!= BitstreamEntry::SubBlock
||
5673 Entry
.ID
!= bitc::MODULE_BLOCK_ID
)
5674 return error("Malformed block");
5677 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
) {
5678 uint64_t ModuleBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
5679 if (Stream
.SkipBlock())
5680 return error("Malformed block");
5682 F
.Mods
.push_back({Stream
.getBitcodeBytes().slice(
5683 BCBegin
, Stream
.getCurrentByteNo() - BCBegin
),
5684 Buffer
.getBufferIdentifier(), IdentificationBit
,
5689 if (Entry
.ID
== bitc::STRTAB_BLOCK_ID
) {
5690 Expected
<StringRef
> Strtab
=
5691 readBlobInRecord(Stream
, bitc::STRTAB_BLOCK_ID
, bitc::STRTAB_BLOB
);
5693 return Strtab
.takeError();
5694 // This string table is used by every preceding bitcode module that does
5695 // not have its own string table. A bitcode file may have multiple
5696 // string tables if it was created by binary concatenation, for example
5697 // with "llvm-cat -b".
5698 for (auto I
= F
.Mods
.rbegin(), E
= F
.Mods
.rend(); I
!= E
; ++I
) {
5699 if (!I
->Strtab
.empty())
5701 I
->Strtab
= *Strtab
;
5703 // Similarly, the string table is used by every preceding symbol table;
5704 // normally there will be just one unless the bitcode file was created
5705 // by binary concatenation.
5706 if (!F
.Symtab
.empty() && F
.StrtabForSymtab
.empty())
5707 F
.StrtabForSymtab
= *Strtab
;
5711 if (Entry
.ID
== bitc::SYMTAB_BLOCK_ID
) {
5712 Expected
<StringRef
> SymtabOrErr
=
5713 readBlobInRecord(Stream
, bitc::SYMTAB_BLOCK_ID
, bitc::SYMTAB_BLOB
);
5715 return SymtabOrErr
.takeError();
5717 // We can expect the bitcode file to have multiple symbol tables if it
5718 // was created by binary concatenation. In that case we silently
5719 // ignore any subsequent symbol tables, which is fine because this is a
5720 // low level function. The client is expected to notice that the number
5721 // of modules in the symbol table does not match the number of modules
5722 // in the input file and regenerate the symbol table.
5723 if (F
.Symtab
.empty())
5724 F
.Symtab
= *SymtabOrErr
;
5728 if (Stream
.SkipBlock())
5729 return error("Malformed block");
5732 case BitstreamEntry::Record
:
5733 Stream
.skipRecord(Entry
.ID
);
5739 /// Get a lazy one-at-time loading module from bitcode.
5741 /// This isn't always used in a lazy context. In particular, it's also used by
5742 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull
5743 /// in forward-referenced functions from block address references.
5745 /// \param[in] MaterializeAll Set to \c true if we should materialize
5747 Expected
<std::unique_ptr
<Module
>>
5748 BitcodeModule::getModuleImpl(LLVMContext
&Context
, bool MaterializeAll
,
5749 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
5750 BitstreamCursor
Stream(Buffer
);
5752 std::string ProducerIdentification
;
5753 if (IdentificationBit
!= -1ull) {
5754 Stream
.JumpToBit(IdentificationBit
);
5755 Expected
<std::string
> ProducerIdentificationOrErr
=
5756 readIdentificationBlock(Stream
);
5757 if (!ProducerIdentificationOrErr
)
5758 return ProducerIdentificationOrErr
.takeError();
5760 ProducerIdentification
= *ProducerIdentificationOrErr
;
5763 Stream
.JumpToBit(ModuleBit
);
5764 auto *R
= new BitcodeReader(std::move(Stream
), Strtab
, ProducerIdentification
,
5767 std::unique_ptr
<Module
> M
=
5768 llvm::make_unique
<Module
>(ModuleIdentifier
, Context
);
5769 M
->setMaterializer(R
);
5771 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
5773 R
->parseBitcodeInto(M
.get(), ShouldLazyLoadMetadata
, IsImporting
))
5774 return std::move(Err
);
5776 if (MaterializeAll
) {
5777 // Read in the entire module, and destroy the BitcodeReader.
5778 if (Error Err
= M
->materializeAll())
5779 return std::move(Err
);
5781 // Resolve forward references from blockaddresses.
5782 if (Error Err
= R
->materializeForwardReferencedFunctions())
5783 return std::move(Err
);
5785 return std::move(M
);
5788 Expected
<std::unique_ptr
<Module
>>
5789 BitcodeModule::getLazyModule(LLVMContext
&Context
, bool ShouldLazyLoadMetadata
,
5791 return getModuleImpl(Context
, false, ShouldLazyLoadMetadata
, IsImporting
);
5794 // Parse the specified bitcode buffer and merge the index into CombinedIndex.
5795 // We don't use ModuleIdentifier here because the client may need to control the
5796 // module path used in the combined summary (e.g. when reading summaries for
5797 // regular LTO modules).
5798 Error
BitcodeModule::readSummary(ModuleSummaryIndex
&CombinedIndex
,
5799 StringRef ModulePath
, uint64_t ModuleId
) {
5800 BitstreamCursor
Stream(Buffer
);
5801 Stream
.JumpToBit(ModuleBit
);
5803 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, CombinedIndex
,
5804 ModulePath
, ModuleId
);
5805 return R
.parseModule();
5808 // Parse the specified bitcode buffer, returning the function info index.
5809 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> BitcodeModule::getSummary() {
5810 BitstreamCursor
Stream(Buffer
);
5811 Stream
.JumpToBit(ModuleBit
);
5813 auto Index
= llvm::make_unique
<ModuleSummaryIndex
>(/*HaveGVs=*/false);
5814 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, *Index
,
5815 ModuleIdentifier
, 0);
5817 if (Error Err
= R
.parseModule())
5818 return std::move(Err
);
5820 return std::move(Index
);
5823 // Check if the given bitcode buffer contains a global value summary block.
5824 Expected
<BitcodeLTOInfo
> BitcodeModule::getLTOInfo() {
5825 BitstreamCursor
Stream(Buffer
);
5826 Stream
.JumpToBit(ModuleBit
);
5828 if (Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
5829 return error("Invalid record");
5832 BitstreamEntry Entry
= Stream
.advance();
5834 switch (Entry
.Kind
) {
5835 case BitstreamEntry::Error
:
5836 return error("Malformed block");
5837 case BitstreamEntry::EndBlock
:
5838 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/false};
5840 case BitstreamEntry::SubBlock
:
5841 if (Entry
.ID
== bitc::GLOBALVAL_SUMMARY_BLOCK_ID
)
5842 return BitcodeLTOInfo
{/*IsThinLTO=*/true, /*HasSummary=*/true};
5844 if (Entry
.ID
== bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
)
5845 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/true};
5847 // Ignore other sub-blocks.
5848 if (Stream
.SkipBlock())
5849 return error("Malformed block");
5852 case BitstreamEntry::Record
:
5853 Stream
.skipRecord(Entry
.ID
);
5859 static Expected
<BitcodeModule
> getSingleModule(MemoryBufferRef Buffer
) {
5860 Expected
<std::vector
<BitcodeModule
>> MsOrErr
= getBitcodeModuleList(Buffer
);
5862 return MsOrErr
.takeError();
5864 if (MsOrErr
->size() != 1)
5865 return error("Expected a single module");
5867 return (*MsOrErr
)[0];
5870 Expected
<std::unique_ptr
<Module
>>
5871 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer
, LLVMContext
&Context
,
5872 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
5873 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
5875 return BM
.takeError();
5877 return BM
->getLazyModule(Context
, ShouldLazyLoadMetadata
, IsImporting
);
5880 Expected
<std::unique_ptr
<Module
>> llvm::getOwningLazyBitcodeModule(
5881 std::unique_ptr
<MemoryBuffer
> &&Buffer
, LLVMContext
&Context
,
5882 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
5883 auto MOrErr
= getLazyBitcodeModule(*Buffer
, Context
, ShouldLazyLoadMetadata
,
5886 (*MOrErr
)->setOwnedMemoryBuffer(std::move(Buffer
));
5890 Expected
<std::unique_ptr
<Module
>>
5891 BitcodeModule::parseModule(LLVMContext
&Context
) {
5892 return getModuleImpl(Context
, true, false, false);
5893 // TODO: Restore the use-lists to the in-memory state when the bitcode was
5894 // written. We must defer until the Module has been fully materialized.
5897 Expected
<std::unique_ptr
<Module
>> llvm::parseBitcodeFile(MemoryBufferRef Buffer
,
5898 LLVMContext
&Context
) {
5899 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
5901 return BM
.takeError();
5903 return BM
->parseModule(Context
);
5906 Expected
<std::string
> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer
) {
5907 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
5909 return StreamOrErr
.takeError();
5911 return readTriple(*StreamOrErr
);
5914 Expected
<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer
) {
5915 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
5917 return StreamOrErr
.takeError();
5919 return hasObjCCategory(*StreamOrErr
);
5922 Expected
<std::string
> llvm::getBitcodeProducerString(MemoryBufferRef Buffer
) {
5923 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
5925 return StreamOrErr
.takeError();
5927 return readIdentificationCode(*StreamOrErr
);
5930 Error
llvm::readModuleSummaryIndex(MemoryBufferRef Buffer
,
5931 ModuleSummaryIndex
&CombinedIndex
,
5932 uint64_t ModuleId
) {
5933 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
5935 return BM
.takeError();
5937 return BM
->readSummary(CombinedIndex
, BM
->getModuleIdentifier(), ModuleId
);
5940 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
5941 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer
) {
5942 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
5944 return BM
.takeError();
5946 return BM
->getSummary();
5949 Expected
<BitcodeLTOInfo
> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer
) {
5950 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
5952 return BM
.takeError();
5954 return BM
->getLTOInfo();
5957 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
5958 llvm::getModuleSummaryIndexForFile(StringRef Path
,
5959 bool IgnoreEmptyThinLTOIndexFile
) {
5960 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
5961 MemoryBuffer::getFileOrSTDIN(Path
);
5963 return errorCodeToError(FileOrErr
.getError());
5964 if (IgnoreEmptyThinLTOIndexFile
&& !(*FileOrErr
)->getBufferSize())
5966 return getModuleSummaryIndex(**FileOrErr
);