1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Bitcode/BitcodeReader.h"
10 #include "MetadataLoader.h"
11 #include "ValueList.h"
12 #include "llvm/ADT/APFloat.h"
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Bitstream/BitstreamReader.h"
24 #include "llvm/Bitcode/LLVMBitCodes.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/Argument.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/AutoUpgrade.h"
29 #include "llvm/IR/BasicBlock.h"
30 #include "llvm/IR/CallSite.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Comdat.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/DebugInfo.h"
37 #include "llvm/IR/DebugInfoMetadata.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/DerivedTypes.h"
40 #include "llvm/IR/Function.h"
41 #include "llvm/IR/GVMaterializer.h"
42 #include "llvm/IR/GlobalAlias.h"
43 #include "llvm/IR/GlobalIFunc.h"
44 #include "llvm/IR/GlobalIndirectSymbol.h"
45 #include "llvm/IR/GlobalObject.h"
46 #include "llvm/IR/GlobalValue.h"
47 #include "llvm/IR/GlobalVariable.h"
48 #include "llvm/IR/InlineAsm.h"
49 #include "llvm/IR/InstIterator.h"
50 #include "llvm/IR/InstrTypes.h"
51 #include "llvm/IR/Instruction.h"
52 #include "llvm/IR/Instructions.h"
53 #include "llvm/IR/Intrinsics.h"
54 #include "llvm/IR/LLVMContext.h"
55 #include "llvm/IR/Metadata.h"
56 #include "llvm/IR/Module.h"
57 #include "llvm/IR/ModuleSummaryIndex.h"
58 #include "llvm/IR/Operator.h"
59 #include "llvm/IR/Type.h"
60 #include "llvm/IR/Value.h"
61 #include "llvm/IR/Verifier.h"
62 #include "llvm/Support/AtomicOrdering.h"
63 #include "llvm/Support/Casting.h"
64 #include "llvm/Support/CommandLine.h"
65 #include "llvm/Support/Compiler.h"
66 #include "llvm/Support/Debug.h"
67 #include "llvm/Support/Error.h"
68 #include "llvm/Support/ErrorHandling.h"
69 #include "llvm/Support/ErrorOr.h"
70 #include "llvm/Support/ManagedStatic.h"
71 #include "llvm/Support/MathExtras.h"
72 #include "llvm/Support/MemoryBuffer.h"
73 #include "llvm/Support/raw_ostream.h"
83 #include <system_error>
90 static cl::opt
<bool> PrintSummaryGUIDs(
91 "print-summary-global-ids", cl::init(false), cl::Hidden
,
93 "Print the global id for each value when reading the module summary"));
98 SWITCH_INST_MAGIC
= 0x4B5 // May 2012 => 1205 => Hex
101 } // end anonymous namespace
103 static Error
error(const Twine
&Message
) {
104 return make_error
<StringError
>(
105 Message
, make_error_code(BitcodeError::CorruptedBitcode
));
108 static Error
hasInvalidBitcodeHeader(BitstreamCursor
&Stream
) {
109 if (!Stream
.canSkipToPos(4))
110 return createStringError(std::errc::illegal_byte_sequence
,
111 "file too small to contain bitcode header");
112 for (unsigned C
: {'B', 'C'})
113 if (Expected
<SimpleBitstreamCursor::word_t
> Res
= Stream
.Read(8)) {
115 return createStringError(std::errc::illegal_byte_sequence
,
116 "file doesn't start with bitcode header");
118 return Res
.takeError();
119 for (unsigned C
: {0x0, 0xC, 0xE, 0xD})
120 if (Expected
<SimpleBitstreamCursor::word_t
> Res
= Stream
.Read(4)) {
122 return createStringError(std::errc::illegal_byte_sequence
,
123 "file doesn't start with bitcode header");
125 return Res
.takeError();
126 return Error::success();
129 static Expected
<BitstreamCursor
> initStream(MemoryBufferRef Buffer
) {
130 const unsigned char *BufPtr
= (const unsigned char *)Buffer
.getBufferStart();
131 const unsigned char *BufEnd
= BufPtr
+ Buffer
.getBufferSize();
133 if (Buffer
.getBufferSize() & 3)
134 return error("Invalid bitcode signature");
136 // If we have a wrapper header, parse it and ignore the non-bc file contents.
137 // The magic number is 0x0B17C0DE stored in little endian.
138 if (isBitcodeWrapper(BufPtr
, BufEnd
))
139 if (SkipBitcodeWrapperHeader(BufPtr
, BufEnd
, true))
140 return error("Invalid bitcode wrapper header");
142 BitstreamCursor
Stream(ArrayRef
<uint8_t>(BufPtr
, BufEnd
));
143 if (Error Err
= hasInvalidBitcodeHeader(Stream
))
144 return std::move(Err
);
146 return std::move(Stream
);
149 /// Convert a string from a record into an std::string, return true on failure.
150 template <typename StrTy
>
151 static bool convertToString(ArrayRef
<uint64_t> Record
, unsigned Idx
,
153 if (Idx
> Record
.size())
156 for (unsigned i
= Idx
, e
= Record
.size(); i
!= e
; ++i
)
157 Result
+= (char)Record
[i
];
161 // Strip all the TBAA attachment for the module.
162 static void stripTBAA(Module
*M
) {
164 if (F
.isMaterializable())
166 for (auto &I
: instructions(F
))
167 I
.setMetadata(LLVMContext::MD_tbaa
, nullptr);
171 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
172 /// "epoch" encoded in the bitcode, and return the producer name if any.
173 static Expected
<std::string
> readIdentificationBlock(BitstreamCursor
&Stream
) {
174 if (Error Err
= Stream
.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID
))
175 return std::move(Err
);
177 // Read all the records.
178 SmallVector
<uint64_t, 64> Record
;
180 std::string ProducerIdentification
;
183 BitstreamEntry Entry
;
184 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
187 return Res
.takeError();
189 switch (Entry
.Kind
) {
191 case BitstreamEntry::Error
:
192 return error("Malformed block");
193 case BitstreamEntry::EndBlock
:
194 return ProducerIdentification
;
195 case BitstreamEntry::Record
:
196 // The interesting case.
202 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
204 return MaybeBitCode
.takeError();
205 switch (MaybeBitCode
.get()) {
206 default: // Default behavior: reject
207 return error("Invalid value");
208 case bitc::IDENTIFICATION_CODE_STRING
: // IDENTIFICATION: [strchr x N]
209 convertToString(Record
, 0, ProducerIdentification
);
211 case bitc::IDENTIFICATION_CODE_EPOCH
: { // EPOCH: [epoch#]
212 unsigned epoch
= (unsigned)Record
[0];
213 if (epoch
!= bitc::BITCODE_CURRENT_EPOCH
) {
215 Twine("Incompatible epoch: Bitcode '") + Twine(epoch
) +
216 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH
) + "'");
223 static Expected
<std::string
> readIdentificationCode(BitstreamCursor
&Stream
) {
224 // We expect a number of well-defined blocks, though we don't necessarily
225 // need to understand them all.
227 if (Stream
.AtEndOfStream())
230 BitstreamEntry Entry
;
231 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
232 Entry
= std::move(Res
.get());
234 return Res
.takeError();
236 switch (Entry
.Kind
) {
237 case BitstreamEntry::EndBlock
:
238 case BitstreamEntry::Error
:
239 return error("Malformed block");
241 case BitstreamEntry::SubBlock
:
242 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
)
243 return readIdentificationBlock(Stream
);
245 // Ignore other sub-blocks.
246 if (Error Err
= Stream
.SkipBlock())
247 return std::move(Err
);
249 case BitstreamEntry::Record
:
250 if (Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
253 return Skipped
.takeError();
258 static Expected
<bool> hasObjCCategoryInModule(BitstreamCursor
&Stream
) {
259 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
260 return std::move(Err
);
262 SmallVector
<uint64_t, 64> Record
;
263 // Read all the records for this module.
266 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
268 return MaybeEntry
.takeError();
269 BitstreamEntry Entry
= MaybeEntry
.get();
271 switch (Entry
.Kind
) {
272 case BitstreamEntry::SubBlock
: // Handled for us already.
273 case BitstreamEntry::Error
:
274 return error("Malformed block");
275 case BitstreamEntry::EndBlock
:
277 case BitstreamEntry::Record
:
278 // The interesting case.
283 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
285 return MaybeRecord
.takeError();
286 switch (MaybeRecord
.get()) {
288 break; // Default behavior, ignore unknown content.
289 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
291 if (convertToString(Record
, 0, S
))
292 return error("Invalid record");
293 // Check for the i386 and other (x86_64, ARM) conventions
294 if (S
.find("__DATA,__objc_catlist") != std::string::npos
||
295 S
.find("__OBJC,__category") != std::string::npos
)
302 llvm_unreachable("Exit infinite loop");
305 static Expected
<bool> hasObjCCategory(BitstreamCursor
&Stream
) {
306 // We expect a number of well-defined blocks, though we don't necessarily
307 // need to understand them all.
309 BitstreamEntry Entry
;
310 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
311 Entry
= std::move(Res
.get());
313 return Res
.takeError();
315 switch (Entry
.Kind
) {
316 case BitstreamEntry::Error
:
317 return error("Malformed block");
318 case BitstreamEntry::EndBlock
:
321 case BitstreamEntry::SubBlock
:
322 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
323 return hasObjCCategoryInModule(Stream
);
325 // Ignore other sub-blocks.
326 if (Error Err
= Stream
.SkipBlock())
327 return std::move(Err
);
330 case BitstreamEntry::Record
:
331 if (Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
334 return Skipped
.takeError();
339 static Expected
<std::string
> readModuleTriple(BitstreamCursor
&Stream
) {
340 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
341 return std::move(Err
);
343 SmallVector
<uint64_t, 64> Record
;
347 // Read all the records for this module.
349 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
351 return MaybeEntry
.takeError();
352 BitstreamEntry Entry
= MaybeEntry
.get();
354 switch (Entry
.Kind
) {
355 case BitstreamEntry::SubBlock
: // Handled for us already.
356 case BitstreamEntry::Error
:
357 return error("Malformed block");
358 case BitstreamEntry::EndBlock
:
360 case BitstreamEntry::Record
:
361 // The interesting case.
366 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
368 return MaybeRecord
.takeError();
369 switch (MaybeRecord
.get()) {
370 default: break; // Default behavior, ignore unknown content.
371 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
373 if (convertToString(Record
, 0, S
))
374 return error("Invalid record");
381 llvm_unreachable("Exit infinite loop");
384 static Expected
<std::string
> readTriple(BitstreamCursor
&Stream
) {
385 // We expect a number of well-defined blocks, though we don't necessarily
386 // need to understand them all.
388 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advance();
390 return MaybeEntry
.takeError();
391 BitstreamEntry Entry
= MaybeEntry
.get();
393 switch (Entry
.Kind
) {
394 case BitstreamEntry::Error
:
395 return error("Malformed block");
396 case BitstreamEntry::EndBlock
:
399 case BitstreamEntry::SubBlock
:
400 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
401 return readModuleTriple(Stream
);
403 // Ignore other sub-blocks.
404 if (Error Err
= Stream
.SkipBlock())
405 return std::move(Err
);
408 case BitstreamEntry::Record
:
409 if (llvm::Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
412 return Skipped
.takeError();
419 class BitcodeReaderBase
{
421 BitcodeReaderBase(BitstreamCursor Stream
, StringRef Strtab
)
422 : Stream(std::move(Stream
)), Strtab(Strtab
) {
423 this->Stream
.setBlockInfo(&BlockInfo
);
426 BitstreamBlockInfo BlockInfo
;
427 BitstreamCursor Stream
;
430 /// In version 2 of the bitcode we store names of global values and comdats in
431 /// a string table rather than in the VST.
432 bool UseStrtab
= false;
434 Expected
<unsigned> parseVersionRecord(ArrayRef
<uint64_t> Record
);
436 /// If this module uses a string table, pop the reference to the string table
437 /// and return the referenced string and the rest of the record. Otherwise
438 /// just return the record itself.
439 std::pair
<StringRef
, ArrayRef
<uint64_t>>
440 readNameFromStrtab(ArrayRef
<uint64_t> Record
);
442 bool readBlockInfo();
444 // Contains an arbitrary and optional string identifying the bitcode producer
445 std::string ProducerIdentification
;
447 Error
error(const Twine
&Message
);
450 } // end anonymous namespace
452 Error
BitcodeReaderBase::error(const Twine
&Message
) {
453 std::string FullMsg
= Message
.str();
454 if (!ProducerIdentification
.empty())
455 FullMsg
+= " (Producer: '" + ProducerIdentification
+ "' Reader: 'LLVM " +
456 LLVM_VERSION_STRING
"')";
457 return ::error(FullMsg
);
461 BitcodeReaderBase::parseVersionRecord(ArrayRef
<uint64_t> Record
) {
463 return error("Invalid record");
464 unsigned ModuleVersion
= Record
[0];
465 if (ModuleVersion
> 2)
466 return error("Invalid value");
467 UseStrtab
= ModuleVersion
>= 2;
468 return ModuleVersion
;
471 std::pair
<StringRef
, ArrayRef
<uint64_t>>
472 BitcodeReaderBase::readNameFromStrtab(ArrayRef
<uint64_t> Record
) {
475 // Invalid reference. Let the caller complain about the record being empty.
476 if (Record
[0] + Record
[1] > Strtab
.size())
478 return {StringRef(Strtab
.data() + Record
[0], Record
[1]), Record
.slice(2)};
483 class BitcodeReader
: public BitcodeReaderBase
, public GVMaterializer
{
484 LLVMContext
&Context
;
485 Module
*TheModule
= nullptr;
486 // Next offset to start scanning for lazy parsing of function bodies.
487 uint64_t NextUnreadBit
= 0;
488 // Last function offset found in the VST.
489 uint64_t LastFunctionBlockBit
= 0;
490 bool SeenValueSymbolTable
= false;
491 uint64_t VSTOffset
= 0;
493 std::vector
<std::string
> SectionTable
;
494 std::vector
<std::string
> GCTable
;
496 std::vector
<Type
*> TypeList
;
497 DenseMap
<Function
*, FunctionType
*> FunctionTypes
;
498 BitcodeReaderValueList ValueList
;
499 Optional
<MetadataLoader
> MDLoader
;
500 std::vector
<Comdat
*> ComdatList
;
501 SmallVector
<Instruction
*, 64> InstructionList
;
503 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInits
;
504 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>> IndirectSymbolInits
;
505 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixes
;
506 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologues
;
507 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFns
;
509 /// The set of attributes by index. Index zero in the file is for null, and
510 /// is thus not represented here. As such all indices are off by one.
511 std::vector
<AttributeList
> MAttributes
;
513 /// The set of attribute groups.
514 std::map
<unsigned, AttributeList
> MAttributeGroups
;
516 /// While parsing a function body, this is a list of the basic blocks for the
518 std::vector
<BasicBlock
*> FunctionBBs
;
520 // When reading the module header, this list is populated with functions that
521 // have bodies later in the file.
522 std::vector
<Function
*> FunctionsWithBodies
;
524 // When intrinsic functions are encountered which require upgrading they are
525 // stored here with their replacement function.
526 using UpdatedIntrinsicMap
= DenseMap
<Function
*, Function
*>;
527 UpdatedIntrinsicMap UpgradedIntrinsics
;
528 // Intrinsics which were remangled because of types rename
529 UpdatedIntrinsicMap RemangledIntrinsics
;
531 // Several operations happen after the module header has been read, but
532 // before function bodies are processed. This keeps track of whether
533 // we've done this yet.
534 bool SeenFirstFunctionBody
= false;
536 /// When function bodies are initially scanned, this map contains info about
537 /// where to find deferred function body in the stream.
538 DenseMap
<Function
*, uint64_t> DeferredFunctionInfo
;
540 /// When Metadata block is initially scanned when parsing the module, we may
541 /// choose to defer parsing of the metadata. This vector contains info about
542 /// which Metadata blocks are deferred.
543 std::vector
<uint64_t> DeferredMetadataInfo
;
545 /// These are basic blocks forward-referenced by block addresses. They are
546 /// inserted lazily into functions when they're loaded. The basic block ID is
547 /// its index into the vector.
548 DenseMap
<Function
*, std::vector
<BasicBlock
*>> BasicBlockFwdRefs
;
549 std::deque
<Function
*> BasicBlockFwdRefQueue
;
551 /// Indicates that we are using a new encoding for instruction operands where
552 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
553 /// instruction number, for a more compact encoding. Some instruction
554 /// operands are not relative to the instruction ID: basic block numbers, and
555 /// types. Once the old style function blocks have been phased out, we would
556 /// not need this flag.
557 bool UseRelativeIDs
= false;
559 /// True if all functions will be materialized, negating the need to process
560 /// (e.g.) blockaddress forward references.
561 bool WillMaterializeAllForwardRefs
= false;
563 bool StripDebugInfo
= false;
564 TBAAVerifier TBAAVerifyHelper
;
566 std::vector
<std::string
> BundleTags
;
567 SmallVector
<SyncScope::ID
, 8> SSIDs
;
570 BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
571 StringRef ProducerIdentification
, LLVMContext
&Context
);
573 Error
materializeForwardReferencedFunctions();
575 Error
materialize(GlobalValue
*GV
) override
;
576 Error
materializeModule() override
;
577 std::vector
<StructType
*> getIdentifiedStructTypes() const override
;
579 /// Main interface to parsing a bitcode buffer.
580 /// \returns true if an error occurred.
581 Error
parseBitcodeInto(Module
*M
, bool ShouldLazyLoadMetadata
= false,
582 bool IsImporting
= false);
584 static uint64_t decodeSignRotatedValue(uint64_t V
);
586 /// Materialize any deferred Metadata block.
587 Error
materializeMetadata() override
;
589 void setStripDebugInfo() override
;
592 std::vector
<StructType
*> IdentifiedStructTypes
;
593 StructType
*createIdentifiedStructType(LLVMContext
&Context
, StringRef Name
);
594 StructType
*createIdentifiedStructType(LLVMContext
&Context
);
596 /// Map all pointer types within \param Ty to the opaque pointer
597 /// type in the same address space if opaque pointers are being
598 /// used, otherwise nop. This converts a bitcode-reader internal
599 /// type into one suitable for use in a Value.
600 Type
*flattenPointerTypes(Type
*Ty
) {
604 /// Given a fully structured pointer type (i.e. not opaque), return
605 /// the flattened form of its element, suitable for use in a Value.
606 Type
*getPointerElementFlatType(Type
*Ty
) {
607 return flattenPointerTypes(cast
<PointerType
>(Ty
)->getElementType());
610 /// Given a fully structured pointer type, get its element type in
611 /// both fully structured form, and flattened form suitable for use
613 std::pair
<Type
*, Type
*> getPointerElementTypes(Type
*FullTy
) {
614 Type
*ElTy
= cast
<PointerType
>(FullTy
)->getElementType();
615 return std::make_pair(ElTy
, flattenPointerTypes(ElTy
));
618 /// Return the flattened type (suitable for use in a Value)
619 /// specified by the given \param ID .
620 Type
*getTypeByID(unsigned ID
) {
621 return flattenPointerTypes(getFullyStructuredTypeByID(ID
));
624 /// Return the fully structured (bitcode-reader internal) type
625 /// corresponding to the given \param ID .
626 Type
*getFullyStructuredTypeByID(unsigned ID
);
628 Value
*getFnValueByID(unsigned ID
, Type
*Ty
, Type
**FullTy
= nullptr) {
629 if (Ty
&& Ty
->isMetadataTy())
630 return MetadataAsValue::get(Ty
->getContext(), getFnMetadataByID(ID
));
631 return ValueList
.getValueFwdRef(ID
, Ty
, FullTy
);
634 Metadata
*getFnMetadataByID(unsigned ID
) {
635 return MDLoader
->getMetadataFwdRefOrLoad(ID
);
638 BasicBlock
*getBasicBlock(unsigned ID
) const {
639 if (ID
>= FunctionBBs
.size()) return nullptr; // Invalid ID
640 return FunctionBBs
[ID
];
643 AttributeList
getAttributes(unsigned i
) const {
644 if (i
-1 < MAttributes
.size())
645 return MAttributes
[i
-1];
646 return AttributeList();
649 /// Read a value/type pair out of the specified record from slot 'Slot'.
650 /// Increment Slot past the number of slots used in the record. Return true on
652 bool getValueTypePair(SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
653 unsigned InstNum
, Value
*&ResVal
,
654 Type
**FullTy
= nullptr) {
655 if (Slot
== Record
.size()) return true;
656 unsigned ValNo
= (unsigned)Record
[Slot
++];
657 // Adjust the ValNo, if it was encoded relative to the InstNum.
659 ValNo
= InstNum
- ValNo
;
660 if (ValNo
< InstNum
) {
661 // If this is not a forward reference, just return the value we already
663 ResVal
= getFnValueByID(ValNo
, nullptr, FullTy
);
664 return ResVal
== nullptr;
666 if (Slot
== Record
.size())
669 unsigned TypeNo
= (unsigned)Record
[Slot
++];
670 ResVal
= getFnValueByID(ValNo
, getTypeByID(TypeNo
));
672 *FullTy
= getFullyStructuredTypeByID(TypeNo
);
673 return ResVal
== nullptr;
676 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
677 /// past the number of slots used by the value in the record. Return true if
678 /// there is an error.
679 bool popValue(SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
680 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
681 if (getValue(Record
, Slot
, InstNum
, Ty
, ResVal
))
683 // All values currently take a single record slot.
688 /// Like popValue, but does not increment the Slot number.
689 bool getValue(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
690 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
691 ResVal
= getValue(Record
, Slot
, InstNum
, Ty
);
692 return ResVal
== nullptr;
695 /// Version of getValue that returns ResVal directly, or 0 if there is an
697 Value
*getValue(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
698 unsigned InstNum
, Type
*Ty
) {
699 if (Slot
== Record
.size()) return nullptr;
700 unsigned ValNo
= (unsigned)Record
[Slot
];
701 // Adjust the ValNo, if it was encoded relative to the InstNum.
703 ValNo
= InstNum
- ValNo
;
704 return getFnValueByID(ValNo
, Ty
);
707 /// Like getValue, but decodes signed VBRs.
708 Value
*getValueSigned(SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
709 unsigned InstNum
, Type
*Ty
) {
710 if (Slot
== Record
.size()) return nullptr;
711 unsigned ValNo
= (unsigned)decodeSignRotatedValue(Record
[Slot
]);
712 // Adjust the ValNo, if it was encoded relative to the InstNum.
714 ValNo
= InstNum
- ValNo
;
715 return getFnValueByID(ValNo
, Ty
);
718 /// Upgrades old-style typeless byval attributes by adding the corresponding
719 /// argument's pointee type.
720 void propagateByValTypes(CallBase
*CB
, ArrayRef
<Type
*> ArgsFullTys
);
722 /// Converts alignment exponent (i.e. power of two (or zero)) to the
723 /// corresponding alignment to use. If alignment is too large, returns
724 /// a corresponding error code.
725 Error
parseAlignmentValue(uint64_t Exponent
, unsigned &Alignment
);
726 Error
parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
);
727 Error
parseModule(uint64_t ResumeBit
, bool ShouldLazyLoadMetadata
= false);
729 Error
parseComdatRecord(ArrayRef
<uint64_t> Record
);
730 Error
parseGlobalVarRecord(ArrayRef
<uint64_t> Record
);
731 Error
parseFunctionRecord(ArrayRef
<uint64_t> Record
);
732 Error
parseGlobalIndirectSymbolRecord(unsigned BitCode
,
733 ArrayRef
<uint64_t> Record
);
735 Error
parseAttributeBlock();
736 Error
parseAttributeGroupBlock();
737 Error
parseTypeTable();
738 Error
parseTypeTableBody();
739 Error
parseOperandBundleTags();
740 Error
parseSyncScopeNames();
742 Expected
<Value
*> recordValue(SmallVectorImpl
<uint64_t> &Record
,
743 unsigned NameIndex
, Triple
&TT
);
744 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
, Function
*F
,
745 ArrayRef
<uint64_t> Record
);
746 Error
parseValueSymbolTable(uint64_t Offset
= 0);
747 Error
parseGlobalValueSymbolTable();
748 Error
parseConstants();
749 Error
rememberAndSkipFunctionBodies();
750 Error
rememberAndSkipFunctionBody();
751 /// Save the positions of the Metadata blocks and skip parsing the blocks.
752 Error
rememberAndSkipMetadata();
753 Error
typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
);
754 Error
parseFunctionBody(Function
*F
);
755 Error
globalCleanup();
756 Error
resolveGlobalAndIndirectSymbolInits();
757 Error
parseUseLists();
758 Error
findFunctionInStream(
760 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
);
762 SyncScope::ID
getDecodedSyncScopeID(unsigned Val
);
765 /// Class to manage reading and parsing function summary index bitcode
767 class ModuleSummaryIndexBitcodeReader
: public BitcodeReaderBase
{
768 /// The module index built during parsing.
769 ModuleSummaryIndex
&TheIndex
;
771 /// Indicates whether we have encountered a global value summary section
772 /// yet during parsing.
773 bool SeenGlobalValSummary
= false;
775 /// Indicates whether we have already parsed the VST, used for error checking.
776 bool SeenValueSymbolTable
= false;
778 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
779 /// Used to enable on-demand parsing of the VST.
780 uint64_t VSTOffset
= 0;
782 // Map to save ValueId to ValueInfo association that was recorded in the
783 // ValueSymbolTable. It is used after the VST is parsed to convert
784 // call graph edges read from the function summary from referencing
785 // callees by their ValueId to using the ValueInfo instead, which is how
786 // they are recorded in the summary index being built.
787 // We save a GUID which refers to the same global as the ValueInfo, but
788 // ignoring the linkage, i.e. for values other than local linkage they are
790 DenseMap
<unsigned, std::pair
<ValueInfo
, GlobalValue::GUID
>>
791 ValueIdToValueInfoMap
;
793 /// Map populated during module path string table parsing, from the
794 /// module ID to a string reference owned by the index's module
795 /// path string table, used to correlate with combined index
797 DenseMap
<uint64_t, StringRef
> ModuleIdMap
;
799 /// Original source file name recorded in a bitcode record.
800 std::string SourceFileName
;
802 /// The string identifier given to this module by the client, normally the
803 /// path to the bitcode file.
804 StringRef ModulePath
;
806 /// For per-module summary indexes, the unique numerical identifier given to
807 /// this module by the client.
811 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
812 ModuleSummaryIndex
&TheIndex
,
813 StringRef ModulePath
, unsigned ModuleId
);
818 void setValueGUID(uint64_t ValueID
, StringRef ValueName
,
819 GlobalValue::LinkageTypes Linkage
,
820 StringRef SourceFileName
);
821 Error
parseValueSymbolTable(
823 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
);
824 std::vector
<ValueInfo
> makeRefList(ArrayRef
<uint64_t> Record
);
825 std::vector
<FunctionSummary::EdgeTy
> makeCallList(ArrayRef
<uint64_t> Record
,
826 bool IsOldProfileFormat
,
829 Error
parseEntireSummary(unsigned ID
);
830 Error
parseModuleStringTable();
831 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef
<uint64_t> Record
);
832 void parseTypeIdCompatibleVtableInfo(ArrayRef
<uint64_t> Record
, size_t &Slot
,
833 TypeIdCompatibleVtableInfo
&TypeId
);
835 std::pair
<ValueInfo
, GlobalValue::GUID
>
836 getValueInfoFromValueId(unsigned ValueId
);
838 void addThisModule();
839 ModuleSummaryIndex::ModuleInfo
*getThisModule();
842 } // end anonymous namespace
844 std::error_code
llvm::errorToErrorCodeAndEmitErrors(LLVMContext
&Ctx
,
848 handleAllErrors(std::move(Err
), [&](ErrorInfoBase
&EIB
) {
849 EC
= EIB
.convertToErrorCode();
850 Ctx
.emitError(EIB
.message());
854 return std::error_code();
857 BitcodeReader::BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
858 StringRef ProducerIdentification
,
859 LLVMContext
&Context
)
860 : BitcodeReaderBase(std::move(Stream
), Strtab
), Context(Context
),
861 ValueList(Context
, Stream
.SizeInBytes()) {
862 this->ProducerIdentification
= ProducerIdentification
;
865 Error
BitcodeReader::materializeForwardReferencedFunctions() {
866 if (WillMaterializeAllForwardRefs
)
867 return Error::success();
869 // Prevent recursion.
870 WillMaterializeAllForwardRefs
= true;
872 while (!BasicBlockFwdRefQueue
.empty()) {
873 Function
*F
= BasicBlockFwdRefQueue
.front();
874 BasicBlockFwdRefQueue
.pop_front();
875 assert(F
&& "Expected valid function");
876 if (!BasicBlockFwdRefs
.count(F
))
877 // Already materialized.
880 // Check for a function that isn't materializable to prevent an infinite
881 // loop. When parsing a blockaddress stored in a global variable, there
882 // isn't a trivial way to check if a function will have a body without a
883 // linear search through FunctionsWithBodies, so just check it here.
884 if (!F
->isMaterializable())
885 return error("Never resolved function from blockaddress");
887 // Try to materialize F.
888 if (Error Err
= materialize(F
))
891 assert(BasicBlockFwdRefs
.empty() && "Function missing from queue");
894 WillMaterializeAllForwardRefs
= false;
895 return Error::success();
898 //===----------------------------------------------------------------------===//
899 // Helper functions to implement forward reference resolution, etc.
900 //===----------------------------------------------------------------------===//
902 static bool hasImplicitComdat(size_t Val
) {
906 case 1: // Old WeakAnyLinkage
907 case 4: // Old LinkOnceAnyLinkage
908 case 10: // Old WeakODRLinkage
909 case 11: // Old LinkOnceODRLinkage
914 static GlobalValue::LinkageTypes
getDecodedLinkage(unsigned Val
) {
916 default: // Map unknown/new linkages to external
918 return GlobalValue::ExternalLinkage
;
920 return GlobalValue::AppendingLinkage
;
922 return GlobalValue::InternalLinkage
;
924 return GlobalValue::ExternalLinkage
; // Obsolete DLLImportLinkage
926 return GlobalValue::ExternalLinkage
; // Obsolete DLLExportLinkage
928 return GlobalValue::ExternalWeakLinkage
;
930 return GlobalValue::CommonLinkage
;
932 return GlobalValue::PrivateLinkage
;
934 return GlobalValue::AvailableExternallyLinkage
;
936 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateLinkage
938 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateWeakLinkage
940 return GlobalValue::ExternalLinkage
; // Obsolete LinkOnceODRAutoHideLinkage
941 case 1: // Old value with implicit comdat.
943 return GlobalValue::WeakAnyLinkage
;
944 case 10: // Old value with implicit comdat.
946 return GlobalValue::WeakODRLinkage
;
947 case 4: // Old value with implicit comdat.
949 return GlobalValue::LinkOnceAnyLinkage
;
950 case 11: // Old value with implicit comdat.
952 return GlobalValue::LinkOnceODRLinkage
;
956 static FunctionSummary::FFlags
getDecodedFFlags(uint64_t RawFlags
) {
957 FunctionSummary::FFlags Flags
;
958 Flags
.ReadNone
= RawFlags
& 0x1;
959 Flags
.ReadOnly
= (RawFlags
>> 1) & 0x1;
960 Flags
.NoRecurse
= (RawFlags
>> 2) & 0x1;
961 Flags
.ReturnDoesNotAlias
= (RawFlags
>> 3) & 0x1;
962 Flags
.NoInline
= (RawFlags
>> 4) & 0x1;
966 /// Decode the flags for GlobalValue in the summary.
967 static GlobalValueSummary::GVFlags
getDecodedGVSummaryFlags(uint64_t RawFlags
,
969 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
970 // like getDecodedLinkage() above. Any future change to the linkage enum and
971 // to getDecodedLinkage() will need to be taken into account here as above.
972 auto Linkage
= GlobalValue::LinkageTypes(RawFlags
& 0xF); // 4 bits
973 RawFlags
= RawFlags
>> 4;
974 bool NotEligibleToImport
= (RawFlags
& 0x1) || Version
< 3;
975 // The Live flag wasn't introduced until version 3. For dead stripping
976 // to work correctly on earlier versions, we must conservatively treat all
978 bool Live
= (RawFlags
& 0x2) || Version
< 3;
979 bool Local
= (RawFlags
& 0x4);
980 bool AutoHide
= (RawFlags
& 0x8);
982 return GlobalValueSummary::GVFlags(Linkage
, NotEligibleToImport
, Live
, Local
, AutoHide
);
985 // Decode the flags for GlobalVariable in the summary
986 static GlobalVarSummary::GVarFlags
getDecodedGVarFlags(uint64_t RawFlags
) {
987 return GlobalVarSummary::GVarFlags((RawFlags
& 0x1) ? true : false,
988 (RawFlags
& 0x2) ? true : false);
991 static GlobalValue::VisibilityTypes
getDecodedVisibility(unsigned Val
) {
993 default: // Map unknown visibilities to default.
994 case 0: return GlobalValue::DefaultVisibility
;
995 case 1: return GlobalValue::HiddenVisibility
;
996 case 2: return GlobalValue::ProtectedVisibility
;
1000 static GlobalValue::DLLStorageClassTypes
1001 getDecodedDLLStorageClass(unsigned Val
) {
1003 default: // Map unknown values to default.
1004 case 0: return GlobalValue::DefaultStorageClass
;
1005 case 1: return GlobalValue::DLLImportStorageClass
;
1006 case 2: return GlobalValue::DLLExportStorageClass
;
1010 static bool getDecodedDSOLocal(unsigned Val
) {
1012 default: // Map unknown values to preemptable.
1013 case 0: return false;
1014 case 1: return true;
1018 static GlobalVariable::ThreadLocalMode
getDecodedThreadLocalMode(unsigned Val
) {
1020 case 0: return GlobalVariable::NotThreadLocal
;
1021 default: // Map unknown non-zero value to general dynamic.
1022 case 1: return GlobalVariable::GeneralDynamicTLSModel
;
1023 case 2: return GlobalVariable::LocalDynamicTLSModel
;
1024 case 3: return GlobalVariable::InitialExecTLSModel
;
1025 case 4: return GlobalVariable::LocalExecTLSModel
;
1029 static GlobalVariable::UnnamedAddr
getDecodedUnnamedAddrType(unsigned Val
) {
1031 default: // Map unknown to UnnamedAddr::None.
1032 case 0: return GlobalVariable::UnnamedAddr::None
;
1033 case 1: return GlobalVariable::UnnamedAddr::Global
;
1034 case 2: return GlobalVariable::UnnamedAddr::Local
;
1038 static int getDecodedCastOpcode(unsigned Val
) {
1041 case bitc::CAST_TRUNC
: return Instruction::Trunc
;
1042 case bitc::CAST_ZEXT
: return Instruction::ZExt
;
1043 case bitc::CAST_SEXT
: return Instruction::SExt
;
1044 case bitc::CAST_FPTOUI
: return Instruction::FPToUI
;
1045 case bitc::CAST_FPTOSI
: return Instruction::FPToSI
;
1046 case bitc::CAST_UITOFP
: return Instruction::UIToFP
;
1047 case bitc::CAST_SITOFP
: return Instruction::SIToFP
;
1048 case bitc::CAST_FPTRUNC
: return Instruction::FPTrunc
;
1049 case bitc::CAST_FPEXT
: return Instruction::FPExt
;
1050 case bitc::CAST_PTRTOINT
: return Instruction::PtrToInt
;
1051 case bitc::CAST_INTTOPTR
: return Instruction::IntToPtr
;
1052 case bitc::CAST_BITCAST
: return Instruction::BitCast
;
1053 case bitc::CAST_ADDRSPACECAST
: return Instruction::AddrSpaceCast
;
1057 static int getDecodedUnaryOpcode(unsigned Val
, Type
*Ty
) {
1058 bool IsFP
= Ty
->isFPOrFPVectorTy();
1059 // UnOps are only valid for int/fp or vector of int/fp types
1060 if (!IsFP
&& !Ty
->isIntOrIntVectorTy())
1066 case bitc::UNOP_NEG
:
1067 return IsFP
? Instruction::FNeg
: -1;
1071 static int getDecodedBinaryOpcode(unsigned Val
, Type
*Ty
) {
1072 bool IsFP
= Ty
->isFPOrFPVectorTy();
1073 // BinOps are only valid for int/fp or vector of int/fp types
1074 if (!IsFP
&& !Ty
->isIntOrIntVectorTy())
1080 case bitc::BINOP_ADD
:
1081 return IsFP
? Instruction::FAdd
: Instruction::Add
;
1082 case bitc::BINOP_SUB
:
1083 return IsFP
? Instruction::FSub
: Instruction::Sub
;
1084 case bitc::BINOP_MUL
:
1085 return IsFP
? Instruction::FMul
: Instruction::Mul
;
1086 case bitc::BINOP_UDIV
:
1087 return IsFP
? -1 : Instruction::UDiv
;
1088 case bitc::BINOP_SDIV
:
1089 return IsFP
? Instruction::FDiv
: Instruction::SDiv
;
1090 case bitc::BINOP_UREM
:
1091 return IsFP
? -1 : Instruction::URem
;
1092 case bitc::BINOP_SREM
:
1093 return IsFP
? Instruction::FRem
: Instruction::SRem
;
1094 case bitc::BINOP_SHL
:
1095 return IsFP
? -1 : Instruction::Shl
;
1096 case bitc::BINOP_LSHR
:
1097 return IsFP
? -1 : Instruction::LShr
;
1098 case bitc::BINOP_ASHR
:
1099 return IsFP
? -1 : Instruction::AShr
;
1100 case bitc::BINOP_AND
:
1101 return IsFP
? -1 : Instruction::And
;
1102 case bitc::BINOP_OR
:
1103 return IsFP
? -1 : Instruction::Or
;
1104 case bitc::BINOP_XOR
:
1105 return IsFP
? -1 : Instruction::Xor
;
1109 static AtomicRMWInst::BinOp
getDecodedRMWOperation(unsigned Val
) {
1111 default: return AtomicRMWInst::BAD_BINOP
;
1112 case bitc::RMW_XCHG
: return AtomicRMWInst::Xchg
;
1113 case bitc::RMW_ADD
: return AtomicRMWInst::Add
;
1114 case bitc::RMW_SUB
: return AtomicRMWInst::Sub
;
1115 case bitc::RMW_AND
: return AtomicRMWInst::And
;
1116 case bitc::RMW_NAND
: return AtomicRMWInst::Nand
;
1117 case bitc::RMW_OR
: return AtomicRMWInst::Or
;
1118 case bitc::RMW_XOR
: return AtomicRMWInst::Xor
;
1119 case bitc::RMW_MAX
: return AtomicRMWInst::Max
;
1120 case bitc::RMW_MIN
: return AtomicRMWInst::Min
;
1121 case bitc::RMW_UMAX
: return AtomicRMWInst::UMax
;
1122 case bitc::RMW_UMIN
: return AtomicRMWInst::UMin
;
1123 case bitc::RMW_FADD
: return AtomicRMWInst::FAdd
;
1124 case bitc::RMW_FSUB
: return AtomicRMWInst::FSub
;
1128 static AtomicOrdering
getDecodedOrdering(unsigned Val
) {
1130 case bitc::ORDERING_NOTATOMIC
: return AtomicOrdering::NotAtomic
;
1131 case bitc::ORDERING_UNORDERED
: return AtomicOrdering::Unordered
;
1132 case bitc::ORDERING_MONOTONIC
: return AtomicOrdering::Monotonic
;
1133 case bitc::ORDERING_ACQUIRE
: return AtomicOrdering::Acquire
;
1134 case bitc::ORDERING_RELEASE
: return AtomicOrdering::Release
;
1135 case bitc::ORDERING_ACQREL
: return AtomicOrdering::AcquireRelease
;
1136 default: // Map unknown orderings to sequentially-consistent.
1137 case bitc::ORDERING_SEQCST
: return AtomicOrdering::SequentiallyConsistent
;
1141 static Comdat::SelectionKind
getDecodedComdatSelectionKind(unsigned Val
) {
1143 default: // Map unknown selection kinds to any.
1144 case bitc::COMDAT_SELECTION_KIND_ANY
:
1146 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH
:
1147 return Comdat::ExactMatch
;
1148 case bitc::COMDAT_SELECTION_KIND_LARGEST
:
1149 return Comdat::Largest
;
1150 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES
:
1151 return Comdat::NoDuplicates
;
1152 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE
:
1153 return Comdat::SameSize
;
1157 static FastMathFlags
getDecodedFastMathFlags(unsigned Val
) {
1159 if (0 != (Val
& bitc::UnsafeAlgebra
))
1161 if (0 != (Val
& bitc::AllowReassoc
))
1162 FMF
.setAllowReassoc();
1163 if (0 != (Val
& bitc::NoNaNs
))
1165 if (0 != (Val
& bitc::NoInfs
))
1167 if (0 != (Val
& bitc::NoSignedZeros
))
1168 FMF
.setNoSignedZeros();
1169 if (0 != (Val
& bitc::AllowReciprocal
))
1170 FMF
.setAllowReciprocal();
1171 if (0 != (Val
& bitc::AllowContract
))
1172 FMF
.setAllowContract(true);
1173 if (0 != (Val
& bitc::ApproxFunc
))
1174 FMF
.setApproxFunc();
1178 static void upgradeDLLImportExportLinkage(GlobalValue
*GV
, unsigned Val
) {
1180 case 5: GV
->setDLLStorageClass(GlobalValue::DLLImportStorageClass
); break;
1181 case 6: GV
->setDLLStorageClass(GlobalValue::DLLExportStorageClass
); break;
1185 Type
*BitcodeReader::getFullyStructuredTypeByID(unsigned ID
) {
1186 // The type table size is always specified correctly.
1187 if (ID
>= TypeList
.size())
1190 if (Type
*Ty
= TypeList
[ID
])
1193 // If we have a forward reference, the only possible case is when it is to a
1194 // named struct. Just create a placeholder for now.
1195 return TypeList
[ID
] = createIdentifiedStructType(Context
);
1198 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
,
1200 auto *Ret
= StructType::create(Context
, Name
);
1201 IdentifiedStructTypes
.push_back(Ret
);
1205 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
) {
1206 auto *Ret
= StructType::create(Context
);
1207 IdentifiedStructTypes
.push_back(Ret
);
1211 //===----------------------------------------------------------------------===//
1212 // Functions for parsing blocks from the bitcode file
1213 //===----------------------------------------------------------------------===//
1215 static uint64_t getRawAttributeMask(Attribute::AttrKind Val
) {
1217 case Attribute::EndAttrKinds
:
1218 llvm_unreachable("Synthetic enumerators which should never get here");
1220 case Attribute::None
: return 0;
1221 case Attribute::ZExt
: return 1 << 0;
1222 case Attribute::SExt
: return 1 << 1;
1223 case Attribute::NoReturn
: return 1 << 2;
1224 case Attribute::InReg
: return 1 << 3;
1225 case Attribute::StructRet
: return 1 << 4;
1226 case Attribute::NoUnwind
: return 1 << 5;
1227 case Attribute::NoAlias
: return 1 << 6;
1228 case Attribute::ByVal
: return 1 << 7;
1229 case Attribute::Nest
: return 1 << 8;
1230 case Attribute::ReadNone
: return 1 << 9;
1231 case Attribute::ReadOnly
: return 1 << 10;
1232 case Attribute::NoInline
: return 1 << 11;
1233 case Attribute::AlwaysInline
: return 1 << 12;
1234 case Attribute::OptimizeForSize
: return 1 << 13;
1235 case Attribute::StackProtect
: return 1 << 14;
1236 case Attribute::StackProtectReq
: return 1 << 15;
1237 case Attribute::Alignment
: return 31 << 16;
1238 case Attribute::NoCapture
: return 1 << 21;
1239 case Attribute::NoRedZone
: return 1 << 22;
1240 case Attribute::NoImplicitFloat
: return 1 << 23;
1241 case Attribute::Naked
: return 1 << 24;
1242 case Attribute::InlineHint
: return 1 << 25;
1243 case Attribute::StackAlignment
: return 7 << 26;
1244 case Attribute::ReturnsTwice
: return 1 << 29;
1245 case Attribute::UWTable
: return 1 << 30;
1246 case Attribute::NonLazyBind
: return 1U << 31;
1247 case Attribute::SanitizeAddress
: return 1ULL << 32;
1248 case Attribute::MinSize
: return 1ULL << 33;
1249 case Attribute::NoDuplicate
: return 1ULL << 34;
1250 case Attribute::StackProtectStrong
: return 1ULL << 35;
1251 case Attribute::SanitizeThread
: return 1ULL << 36;
1252 case Attribute::SanitizeMemory
: return 1ULL << 37;
1253 case Attribute::NoBuiltin
: return 1ULL << 38;
1254 case Attribute::Returned
: return 1ULL << 39;
1255 case Attribute::Cold
: return 1ULL << 40;
1256 case Attribute::Builtin
: return 1ULL << 41;
1257 case Attribute::OptimizeNone
: return 1ULL << 42;
1258 case Attribute::InAlloca
: return 1ULL << 43;
1259 case Attribute::NonNull
: return 1ULL << 44;
1260 case Attribute::JumpTable
: return 1ULL << 45;
1261 case Attribute::Convergent
: return 1ULL << 46;
1262 case Attribute::SafeStack
: return 1ULL << 47;
1263 case Attribute::NoRecurse
: return 1ULL << 48;
1264 case Attribute::InaccessibleMemOnly
: return 1ULL << 49;
1265 case Attribute::InaccessibleMemOrArgMemOnly
: return 1ULL << 50;
1266 case Attribute::SwiftSelf
: return 1ULL << 51;
1267 case Attribute::SwiftError
: return 1ULL << 52;
1268 case Attribute::WriteOnly
: return 1ULL << 53;
1269 case Attribute::Speculatable
: return 1ULL << 54;
1270 case Attribute::StrictFP
: return 1ULL << 55;
1271 case Attribute::SanitizeHWAddress
: return 1ULL << 56;
1272 case Attribute::NoCfCheck
: return 1ULL << 57;
1273 case Attribute::OptForFuzzing
: return 1ULL << 58;
1274 case Attribute::ShadowCallStack
: return 1ULL << 59;
1275 case Attribute::SpeculativeLoadHardening
:
1277 case Attribute::ImmArg
:
1279 case Attribute::WillReturn
:
1281 case Attribute::NoFree
:
1283 case Attribute::NoSync
:
1284 llvm_unreachable("nosync attribute not supported in raw format");
1286 case Attribute::Dereferenceable
:
1287 llvm_unreachable("dereferenceable attribute not supported in raw format");
1289 case Attribute::DereferenceableOrNull
:
1290 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
1293 case Attribute::ArgMemOnly
:
1294 llvm_unreachable("argmemonly attribute not supported in raw format");
1296 case Attribute::AllocSize
:
1297 llvm_unreachable("allocsize not supported in raw format");
1299 case Attribute::SanitizeMemTag
:
1300 llvm_unreachable("sanitize_memtag attribute not supported in raw format");
1303 llvm_unreachable("Unsupported attribute type");
1306 static void addRawAttributeValue(AttrBuilder
&B
, uint64_t Val
) {
1309 for (Attribute::AttrKind I
= Attribute::None
; I
!= Attribute::EndAttrKinds
;
1310 I
= Attribute::AttrKind(I
+ 1)) {
1311 if (I
== Attribute::SanitizeMemTag
||
1312 I
== Attribute::Dereferenceable
||
1313 I
== Attribute::DereferenceableOrNull
||
1314 I
== Attribute::ArgMemOnly
||
1315 I
== Attribute::AllocSize
||
1316 I
== Attribute::NoSync
)
1318 if (uint64_t A
= (Val
& getRawAttributeMask(I
))) {
1319 if (I
== Attribute::Alignment
)
1320 B
.addAlignmentAttr(1ULL << ((A
>> 16) - 1));
1321 else if (I
== Attribute::StackAlignment
)
1322 B
.addStackAlignmentAttr(1ULL << ((A
>> 26)-1));
1329 /// This fills an AttrBuilder object with the LLVM attributes that have
1330 /// been decoded from the given integer. This function must stay in sync with
1331 /// 'encodeLLVMAttributesForBitcode'.
1332 static void decodeLLVMAttributesForBitcode(AttrBuilder
&B
,
1333 uint64_t EncodedAttrs
) {
1334 // FIXME: Remove in 4.0.
1336 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1337 // the bits above 31 down by 11 bits.
1338 unsigned Alignment
= (EncodedAttrs
& (0xffffULL
<< 16)) >> 16;
1339 assert((!Alignment
|| isPowerOf2_32(Alignment
)) &&
1340 "Alignment must be a power of two.");
1343 B
.addAlignmentAttr(Alignment
);
1344 addRawAttributeValue(B
, ((EncodedAttrs
& (0xfffffULL
<< 32)) >> 11) |
1345 (EncodedAttrs
& 0xffff));
1348 Error
BitcodeReader::parseAttributeBlock() {
1349 if (Error Err
= Stream
.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID
))
1352 if (!MAttributes
.empty())
1353 return error("Invalid multiple blocks");
1355 SmallVector
<uint64_t, 64> Record
;
1357 SmallVector
<AttributeList
, 8> Attrs
;
1359 // Read all the records.
1361 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1363 return MaybeEntry
.takeError();
1364 BitstreamEntry Entry
= MaybeEntry
.get();
1366 switch (Entry
.Kind
) {
1367 case BitstreamEntry::SubBlock
: // Handled for us already.
1368 case BitstreamEntry::Error
:
1369 return error("Malformed block");
1370 case BitstreamEntry::EndBlock
:
1371 return Error::success();
1372 case BitstreamEntry::Record
:
1373 // The interesting case.
1379 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1381 return MaybeRecord
.takeError();
1382 switch (MaybeRecord
.get()) {
1383 default: // Default behavior: ignore.
1385 case bitc::PARAMATTR_CODE_ENTRY_OLD
: // ENTRY: [paramidx0, attr0, ...]
1386 // FIXME: Remove in 4.0.
1387 if (Record
.size() & 1)
1388 return error("Invalid record");
1390 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; i
+= 2) {
1392 decodeLLVMAttributesForBitcode(B
, Record
[i
+1]);
1393 Attrs
.push_back(AttributeList::get(Context
, Record
[i
], B
));
1396 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1399 case bitc::PARAMATTR_CODE_ENTRY
: // ENTRY: [attrgrp0, attrgrp1, ...]
1400 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; ++i
)
1401 Attrs
.push_back(MAttributeGroups
[Record
[i
]]);
1403 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1410 // Returns Attribute::None on unrecognized codes.
1411 static Attribute::AttrKind
getAttrFromCode(uint64_t Code
) {
1414 return Attribute::None
;
1415 case bitc::ATTR_KIND_ALIGNMENT
:
1416 return Attribute::Alignment
;
1417 case bitc::ATTR_KIND_ALWAYS_INLINE
:
1418 return Attribute::AlwaysInline
;
1419 case bitc::ATTR_KIND_ARGMEMONLY
:
1420 return Attribute::ArgMemOnly
;
1421 case bitc::ATTR_KIND_BUILTIN
:
1422 return Attribute::Builtin
;
1423 case bitc::ATTR_KIND_BY_VAL
:
1424 return Attribute::ByVal
;
1425 case bitc::ATTR_KIND_IN_ALLOCA
:
1426 return Attribute::InAlloca
;
1427 case bitc::ATTR_KIND_COLD
:
1428 return Attribute::Cold
;
1429 case bitc::ATTR_KIND_CONVERGENT
:
1430 return Attribute::Convergent
;
1431 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY
:
1432 return Attribute::InaccessibleMemOnly
;
1433 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY
:
1434 return Attribute::InaccessibleMemOrArgMemOnly
;
1435 case bitc::ATTR_KIND_INLINE_HINT
:
1436 return Attribute::InlineHint
;
1437 case bitc::ATTR_KIND_IN_REG
:
1438 return Attribute::InReg
;
1439 case bitc::ATTR_KIND_JUMP_TABLE
:
1440 return Attribute::JumpTable
;
1441 case bitc::ATTR_KIND_MIN_SIZE
:
1442 return Attribute::MinSize
;
1443 case bitc::ATTR_KIND_NAKED
:
1444 return Attribute::Naked
;
1445 case bitc::ATTR_KIND_NEST
:
1446 return Attribute::Nest
;
1447 case bitc::ATTR_KIND_NO_ALIAS
:
1448 return Attribute::NoAlias
;
1449 case bitc::ATTR_KIND_NO_BUILTIN
:
1450 return Attribute::NoBuiltin
;
1451 case bitc::ATTR_KIND_NO_CAPTURE
:
1452 return Attribute::NoCapture
;
1453 case bitc::ATTR_KIND_NO_DUPLICATE
:
1454 return Attribute::NoDuplicate
;
1455 case bitc::ATTR_KIND_NOFREE
:
1456 return Attribute::NoFree
;
1457 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT
:
1458 return Attribute::NoImplicitFloat
;
1459 case bitc::ATTR_KIND_NO_INLINE
:
1460 return Attribute::NoInline
;
1461 case bitc::ATTR_KIND_NO_RECURSE
:
1462 return Attribute::NoRecurse
;
1463 case bitc::ATTR_KIND_NON_LAZY_BIND
:
1464 return Attribute::NonLazyBind
;
1465 case bitc::ATTR_KIND_NON_NULL
:
1466 return Attribute::NonNull
;
1467 case bitc::ATTR_KIND_DEREFERENCEABLE
:
1468 return Attribute::Dereferenceable
;
1469 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL
:
1470 return Attribute::DereferenceableOrNull
;
1471 case bitc::ATTR_KIND_ALLOC_SIZE
:
1472 return Attribute::AllocSize
;
1473 case bitc::ATTR_KIND_NO_RED_ZONE
:
1474 return Attribute::NoRedZone
;
1475 case bitc::ATTR_KIND_NO_RETURN
:
1476 return Attribute::NoReturn
;
1477 case bitc::ATTR_KIND_NOSYNC
:
1478 return Attribute::NoSync
;
1479 case bitc::ATTR_KIND_NOCF_CHECK
:
1480 return Attribute::NoCfCheck
;
1481 case bitc::ATTR_KIND_NO_UNWIND
:
1482 return Attribute::NoUnwind
;
1483 case bitc::ATTR_KIND_OPT_FOR_FUZZING
:
1484 return Attribute::OptForFuzzing
;
1485 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE
:
1486 return Attribute::OptimizeForSize
;
1487 case bitc::ATTR_KIND_OPTIMIZE_NONE
:
1488 return Attribute::OptimizeNone
;
1489 case bitc::ATTR_KIND_READ_NONE
:
1490 return Attribute::ReadNone
;
1491 case bitc::ATTR_KIND_READ_ONLY
:
1492 return Attribute::ReadOnly
;
1493 case bitc::ATTR_KIND_RETURNED
:
1494 return Attribute::Returned
;
1495 case bitc::ATTR_KIND_RETURNS_TWICE
:
1496 return Attribute::ReturnsTwice
;
1497 case bitc::ATTR_KIND_S_EXT
:
1498 return Attribute::SExt
;
1499 case bitc::ATTR_KIND_SPECULATABLE
:
1500 return Attribute::Speculatable
;
1501 case bitc::ATTR_KIND_STACK_ALIGNMENT
:
1502 return Attribute::StackAlignment
;
1503 case bitc::ATTR_KIND_STACK_PROTECT
:
1504 return Attribute::StackProtect
;
1505 case bitc::ATTR_KIND_STACK_PROTECT_REQ
:
1506 return Attribute::StackProtectReq
;
1507 case bitc::ATTR_KIND_STACK_PROTECT_STRONG
:
1508 return Attribute::StackProtectStrong
;
1509 case bitc::ATTR_KIND_SAFESTACK
:
1510 return Attribute::SafeStack
;
1511 case bitc::ATTR_KIND_SHADOWCALLSTACK
:
1512 return Attribute::ShadowCallStack
;
1513 case bitc::ATTR_KIND_STRICT_FP
:
1514 return Attribute::StrictFP
;
1515 case bitc::ATTR_KIND_STRUCT_RET
:
1516 return Attribute::StructRet
;
1517 case bitc::ATTR_KIND_SANITIZE_ADDRESS
:
1518 return Attribute::SanitizeAddress
;
1519 case bitc::ATTR_KIND_SANITIZE_HWADDRESS
:
1520 return Attribute::SanitizeHWAddress
;
1521 case bitc::ATTR_KIND_SANITIZE_THREAD
:
1522 return Attribute::SanitizeThread
;
1523 case bitc::ATTR_KIND_SANITIZE_MEMORY
:
1524 return Attribute::SanitizeMemory
;
1525 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING
:
1526 return Attribute::SpeculativeLoadHardening
;
1527 case bitc::ATTR_KIND_SWIFT_ERROR
:
1528 return Attribute::SwiftError
;
1529 case bitc::ATTR_KIND_SWIFT_SELF
:
1530 return Attribute::SwiftSelf
;
1531 case bitc::ATTR_KIND_UW_TABLE
:
1532 return Attribute::UWTable
;
1533 case bitc::ATTR_KIND_WILLRETURN
:
1534 return Attribute::WillReturn
;
1535 case bitc::ATTR_KIND_WRITEONLY
:
1536 return Attribute::WriteOnly
;
1537 case bitc::ATTR_KIND_Z_EXT
:
1538 return Attribute::ZExt
;
1539 case bitc::ATTR_KIND_IMMARG
:
1540 return Attribute::ImmArg
;
1541 case bitc::ATTR_KIND_SANITIZE_MEMTAG
:
1542 return Attribute::SanitizeMemTag
;
1546 Error
BitcodeReader::parseAlignmentValue(uint64_t Exponent
,
1547 unsigned &Alignment
) {
1548 // Note: Alignment in bitcode files is incremented by 1, so that zero
1549 // can be used for default alignment.
1550 if (Exponent
> Value::MaxAlignmentExponent
+ 1)
1551 return error("Invalid alignment value");
1552 Alignment
= (1 << static_cast<unsigned>(Exponent
)) >> 1;
1553 return Error::success();
1556 Error
BitcodeReader::parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
) {
1557 *Kind
= getAttrFromCode(Code
);
1558 if (*Kind
== Attribute::None
)
1559 return error("Unknown attribute kind (" + Twine(Code
) + ")");
1560 return Error::success();
1563 Error
BitcodeReader::parseAttributeGroupBlock() {
1564 if (Error Err
= Stream
.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID
))
1567 if (!MAttributeGroups
.empty())
1568 return error("Invalid multiple blocks");
1570 SmallVector
<uint64_t, 64> Record
;
1572 // Read all the records.
1574 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1576 return MaybeEntry
.takeError();
1577 BitstreamEntry Entry
= MaybeEntry
.get();
1579 switch (Entry
.Kind
) {
1580 case BitstreamEntry::SubBlock
: // Handled for us already.
1581 case BitstreamEntry::Error
:
1582 return error("Malformed block");
1583 case BitstreamEntry::EndBlock
:
1584 return Error::success();
1585 case BitstreamEntry::Record
:
1586 // The interesting case.
1592 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1594 return MaybeRecord
.takeError();
1595 switch (MaybeRecord
.get()) {
1596 default: // Default behavior: ignore.
1598 case bitc::PARAMATTR_GRP_CODE_ENTRY
: { // ENTRY: [grpid, idx, a0, a1, ...]
1599 if (Record
.size() < 3)
1600 return error("Invalid record");
1602 uint64_t GrpID
= Record
[0];
1603 uint64_t Idx
= Record
[1]; // Index of the object this attribute refers to.
1606 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1607 if (Record
[i
] == 0) { // Enum attribute
1608 Attribute::AttrKind Kind
;
1609 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1612 // Upgrade old-style byval attribute to one with a type, even if it's
1613 // nullptr. We will have to insert the real type when we associate
1614 // this AttributeList with a function.
1615 if (Kind
== Attribute::ByVal
)
1616 B
.addByValAttr(nullptr);
1618 B
.addAttribute(Kind
);
1619 } else if (Record
[i
] == 1) { // Integer attribute
1620 Attribute::AttrKind Kind
;
1621 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1623 if (Kind
== Attribute::Alignment
)
1624 B
.addAlignmentAttr(Record
[++i
]);
1625 else if (Kind
== Attribute::StackAlignment
)
1626 B
.addStackAlignmentAttr(Record
[++i
]);
1627 else if (Kind
== Attribute::Dereferenceable
)
1628 B
.addDereferenceableAttr(Record
[++i
]);
1629 else if (Kind
== Attribute::DereferenceableOrNull
)
1630 B
.addDereferenceableOrNullAttr(Record
[++i
]);
1631 else if (Kind
== Attribute::AllocSize
)
1632 B
.addAllocSizeAttrFromRawRepr(Record
[++i
]);
1633 } else if (Record
[i
] == 3 || Record
[i
] == 4) { // String attribute
1634 bool HasValue
= (Record
[i
++] == 4);
1635 SmallString
<64> KindStr
;
1636 SmallString
<64> ValStr
;
1638 while (Record
[i
] != 0 && i
!= e
)
1639 KindStr
+= Record
[i
++];
1640 assert(Record
[i
] == 0 && "Kind string not null terminated");
1643 // Has a value associated with it.
1644 ++i
; // Skip the '0' that terminates the "kind" string.
1645 while (Record
[i
] != 0 && i
!= e
)
1646 ValStr
+= Record
[i
++];
1647 assert(Record
[i
] == 0 && "Value string not null terminated");
1650 B
.addAttribute(KindStr
.str(), ValStr
.str());
1652 assert((Record
[i
] == 5 || Record
[i
] == 6) &&
1653 "Invalid attribute group entry");
1654 bool HasType
= Record
[i
] == 6;
1655 Attribute::AttrKind Kind
;
1656 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1658 if (Kind
== Attribute::ByVal
)
1659 B
.addByValAttr(HasType
? getTypeByID(Record
[++i
]) : nullptr);
1663 MAttributeGroups
[GrpID
] = AttributeList::get(Context
, Idx
, B
);
1670 Error
BitcodeReader::parseTypeTable() {
1671 if (Error Err
= Stream
.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW
))
1674 return parseTypeTableBody();
1677 Error
BitcodeReader::parseTypeTableBody() {
1678 if (!TypeList
.empty())
1679 return error("Invalid multiple blocks");
1681 SmallVector
<uint64_t, 64> Record
;
1682 unsigned NumRecords
= 0;
1684 SmallString
<64> TypeName
;
1686 // Read all the records for this type table.
1688 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1690 return MaybeEntry
.takeError();
1691 BitstreamEntry Entry
= MaybeEntry
.get();
1693 switch (Entry
.Kind
) {
1694 case BitstreamEntry::SubBlock
: // Handled for us already.
1695 case BitstreamEntry::Error
:
1696 return error("Malformed block");
1697 case BitstreamEntry::EndBlock
:
1698 if (NumRecords
!= TypeList
.size())
1699 return error("Malformed block");
1700 return Error::success();
1701 case BitstreamEntry::Record
:
1702 // The interesting case.
1708 Type
*ResultTy
= nullptr;
1709 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1711 return MaybeRecord
.takeError();
1712 switch (MaybeRecord
.get()) {
1714 return error("Invalid value");
1715 case bitc::TYPE_CODE_NUMENTRY
: // TYPE_CODE_NUMENTRY: [numentries]
1716 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1717 // type list. This allows us to reserve space.
1718 if (Record
.size() < 1)
1719 return error("Invalid record");
1720 TypeList
.resize(Record
[0]);
1722 case bitc::TYPE_CODE_VOID
: // VOID
1723 ResultTy
= Type::getVoidTy(Context
);
1725 case bitc::TYPE_CODE_HALF
: // HALF
1726 ResultTy
= Type::getHalfTy(Context
);
1728 case bitc::TYPE_CODE_FLOAT
: // FLOAT
1729 ResultTy
= Type::getFloatTy(Context
);
1731 case bitc::TYPE_CODE_DOUBLE
: // DOUBLE
1732 ResultTy
= Type::getDoubleTy(Context
);
1734 case bitc::TYPE_CODE_X86_FP80
: // X86_FP80
1735 ResultTy
= Type::getX86_FP80Ty(Context
);
1737 case bitc::TYPE_CODE_FP128
: // FP128
1738 ResultTy
= Type::getFP128Ty(Context
);
1740 case bitc::TYPE_CODE_PPC_FP128
: // PPC_FP128
1741 ResultTy
= Type::getPPC_FP128Ty(Context
);
1743 case bitc::TYPE_CODE_LABEL
: // LABEL
1744 ResultTy
= Type::getLabelTy(Context
);
1746 case bitc::TYPE_CODE_METADATA
: // METADATA
1747 ResultTy
= Type::getMetadataTy(Context
);
1749 case bitc::TYPE_CODE_X86_MMX
: // X86_MMX
1750 ResultTy
= Type::getX86_MMXTy(Context
);
1752 case bitc::TYPE_CODE_TOKEN
: // TOKEN
1753 ResultTy
= Type::getTokenTy(Context
);
1755 case bitc::TYPE_CODE_INTEGER
: { // INTEGER: [width]
1756 if (Record
.size() < 1)
1757 return error("Invalid record");
1759 uint64_t NumBits
= Record
[0];
1760 if (NumBits
< IntegerType::MIN_INT_BITS
||
1761 NumBits
> IntegerType::MAX_INT_BITS
)
1762 return error("Bitwidth for integer type out of range");
1763 ResultTy
= IntegerType::get(Context
, NumBits
);
1766 case bitc::TYPE_CODE_POINTER
: { // POINTER: [pointee type] or
1767 // [pointee type, address space]
1768 if (Record
.size() < 1)
1769 return error("Invalid record");
1770 unsigned AddressSpace
= 0;
1771 if (Record
.size() == 2)
1772 AddressSpace
= Record
[1];
1773 ResultTy
= getTypeByID(Record
[0]);
1775 !PointerType::isValidElementType(ResultTy
))
1776 return error("Invalid type");
1777 ResultTy
= PointerType::get(ResultTy
, AddressSpace
);
1780 case bitc::TYPE_CODE_FUNCTION_OLD
: {
1781 // FIXME: attrid is dead, remove it in LLVM 4.0
1782 // FUNCTION: [vararg, attrid, retty, paramty x N]
1783 if (Record
.size() < 3)
1784 return error("Invalid record");
1785 SmallVector
<Type
*, 8> ArgTys
;
1786 for (unsigned i
= 3, e
= Record
.size(); i
!= e
; ++i
) {
1787 if (Type
*T
= getTypeByID(Record
[i
]))
1788 ArgTys
.push_back(T
);
1793 ResultTy
= getTypeByID(Record
[2]);
1794 if (!ResultTy
|| ArgTys
.size() < Record
.size()-3)
1795 return error("Invalid type");
1797 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1800 case bitc::TYPE_CODE_FUNCTION
: {
1801 // FUNCTION: [vararg, retty, paramty x N]
1802 if (Record
.size() < 2)
1803 return error("Invalid record");
1804 SmallVector
<Type
*, 8> ArgTys
;
1805 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1806 if (Type
*T
= getTypeByID(Record
[i
])) {
1807 if (!FunctionType::isValidArgumentType(T
))
1808 return error("Invalid function argument type");
1809 ArgTys
.push_back(T
);
1815 ResultTy
= getTypeByID(Record
[1]);
1816 if (!ResultTy
|| ArgTys
.size() < Record
.size()-2)
1817 return error("Invalid type");
1819 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1822 case bitc::TYPE_CODE_STRUCT_ANON
: { // STRUCT: [ispacked, eltty x N]
1823 if (Record
.size() < 1)
1824 return error("Invalid record");
1825 SmallVector
<Type
*, 8> EltTys
;
1826 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1827 if (Type
*T
= getTypeByID(Record
[i
]))
1828 EltTys
.push_back(T
);
1832 if (EltTys
.size() != Record
.size()-1)
1833 return error("Invalid type");
1834 ResultTy
= StructType::get(Context
, EltTys
, Record
[0]);
1837 case bitc::TYPE_CODE_STRUCT_NAME
: // STRUCT_NAME: [strchr x N]
1838 if (convertToString(Record
, 0, TypeName
))
1839 return error("Invalid record");
1842 case bitc::TYPE_CODE_STRUCT_NAMED
: { // STRUCT: [ispacked, eltty x N]
1843 if (Record
.size() < 1)
1844 return error("Invalid record");
1846 if (NumRecords
>= TypeList
.size())
1847 return error("Invalid TYPE table");
1849 // Check to see if this was forward referenced, if so fill in the temp.
1850 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1852 Res
->setName(TypeName
);
1853 TypeList
[NumRecords
] = nullptr;
1854 } else // Otherwise, create a new struct.
1855 Res
= createIdentifiedStructType(Context
, TypeName
);
1858 SmallVector
<Type
*, 8> EltTys
;
1859 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1860 if (Type
*T
= getTypeByID(Record
[i
]))
1861 EltTys
.push_back(T
);
1865 if (EltTys
.size() != Record
.size()-1)
1866 return error("Invalid record");
1867 Res
->setBody(EltTys
, Record
[0]);
1871 case bitc::TYPE_CODE_OPAQUE
: { // OPAQUE: []
1872 if (Record
.size() != 1)
1873 return error("Invalid record");
1875 if (NumRecords
>= TypeList
.size())
1876 return error("Invalid TYPE table");
1878 // Check to see if this was forward referenced, if so fill in the temp.
1879 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1881 Res
->setName(TypeName
);
1882 TypeList
[NumRecords
] = nullptr;
1883 } else // Otherwise, create a new struct with no body.
1884 Res
= createIdentifiedStructType(Context
, TypeName
);
1889 case bitc::TYPE_CODE_ARRAY
: // ARRAY: [numelts, eltty]
1890 if (Record
.size() < 2)
1891 return error("Invalid record");
1892 ResultTy
= getTypeByID(Record
[1]);
1893 if (!ResultTy
|| !ArrayType::isValidElementType(ResultTy
))
1894 return error("Invalid type");
1895 ResultTy
= ArrayType::get(ResultTy
, Record
[0]);
1897 case bitc::TYPE_CODE_VECTOR
: // VECTOR: [numelts, eltty] or
1898 // [numelts, eltty, scalable]
1899 if (Record
.size() < 2)
1900 return error("Invalid record");
1902 return error("Invalid vector length");
1903 ResultTy
= getTypeByID(Record
[1]);
1904 if (!ResultTy
|| !StructType::isValidElementType(ResultTy
))
1905 return error("Invalid type");
1906 bool Scalable
= Record
.size() > 2 ? Record
[2] : false;
1907 ResultTy
= VectorType::get(ResultTy
, Record
[0], Scalable
);
1911 if (NumRecords
>= TypeList
.size())
1912 return error("Invalid TYPE table");
1913 if (TypeList
[NumRecords
])
1915 "Invalid TYPE table: Only named structs can be forward referenced");
1916 assert(ResultTy
&& "Didn't read a type?");
1917 TypeList
[NumRecords
++] = ResultTy
;
1921 Error
BitcodeReader::parseOperandBundleTags() {
1922 if (Error Err
= Stream
.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
))
1925 if (!BundleTags
.empty())
1926 return error("Invalid multiple blocks");
1928 SmallVector
<uint64_t, 64> Record
;
1931 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1933 return MaybeEntry
.takeError();
1934 BitstreamEntry Entry
= MaybeEntry
.get();
1936 switch (Entry
.Kind
) {
1937 case BitstreamEntry::SubBlock
: // Handled for us already.
1938 case BitstreamEntry::Error
:
1939 return error("Malformed block");
1940 case BitstreamEntry::EndBlock
:
1941 return Error::success();
1942 case BitstreamEntry::Record
:
1943 // The interesting case.
1947 // Tags are implicitly mapped to integers by their order.
1949 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1951 return MaybeRecord
.takeError();
1952 if (MaybeRecord
.get() != bitc::OPERAND_BUNDLE_TAG
)
1953 return error("Invalid record");
1955 // OPERAND_BUNDLE_TAG: [strchr x N]
1956 BundleTags
.emplace_back();
1957 if (convertToString(Record
, 0, BundleTags
.back()))
1958 return error("Invalid record");
1963 Error
BitcodeReader::parseSyncScopeNames() {
1964 if (Error Err
= Stream
.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID
))
1968 return error("Invalid multiple synchronization scope names blocks");
1970 SmallVector
<uint64_t, 64> Record
;
1972 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1974 return MaybeEntry
.takeError();
1975 BitstreamEntry Entry
= MaybeEntry
.get();
1977 switch (Entry
.Kind
) {
1978 case BitstreamEntry::SubBlock
: // Handled for us already.
1979 case BitstreamEntry::Error
:
1980 return error("Malformed block");
1981 case BitstreamEntry::EndBlock
:
1983 return error("Invalid empty synchronization scope names block");
1984 return Error::success();
1985 case BitstreamEntry::Record
:
1986 // The interesting case.
1990 // Synchronization scope names are implicitly mapped to synchronization
1991 // scope IDs by their order.
1993 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1995 return MaybeRecord
.takeError();
1996 if (MaybeRecord
.get() != bitc::SYNC_SCOPE_NAME
)
1997 return error("Invalid record");
1999 SmallString
<16> SSN
;
2000 if (convertToString(Record
, 0, SSN
))
2001 return error("Invalid record");
2003 SSIDs
.push_back(Context
.getOrInsertSyncScopeID(SSN
));
2008 /// Associate a value with its name from the given index in the provided record.
2009 Expected
<Value
*> BitcodeReader::recordValue(SmallVectorImpl
<uint64_t> &Record
,
2010 unsigned NameIndex
, Triple
&TT
) {
2011 SmallString
<128> ValueName
;
2012 if (convertToString(Record
, NameIndex
, ValueName
))
2013 return error("Invalid record");
2014 unsigned ValueID
= Record
[0];
2015 if (ValueID
>= ValueList
.size() || !ValueList
[ValueID
])
2016 return error("Invalid record");
2017 Value
*V
= ValueList
[ValueID
];
2019 StringRef
NameStr(ValueName
.data(), ValueName
.size());
2020 if (NameStr
.find_first_of(0) != StringRef::npos
)
2021 return error("Invalid value name");
2022 V
->setName(NameStr
);
2023 auto *GO
= dyn_cast
<GlobalObject
>(V
);
2025 if (GO
->getComdat() == reinterpret_cast<Comdat
*>(1)) {
2026 if (TT
.supportsCOMDAT())
2027 GO
->setComdat(TheModule
->getOrInsertComdat(V
->getName()));
2029 GO
->setComdat(nullptr);
2035 /// Helper to note and return the current location, and jump to the given
2037 static Expected
<uint64_t> jumpToValueSymbolTable(uint64_t Offset
,
2038 BitstreamCursor
&Stream
) {
2039 // Save the current parsing location so we can jump back at the end
2041 uint64_t CurrentBit
= Stream
.GetCurrentBitNo();
2042 if (Error JumpFailed
= Stream
.JumpToBit(Offset
* 32))
2043 return std::move(JumpFailed
);
2044 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advance();
2046 return MaybeEntry
.takeError();
2047 assert(MaybeEntry
.get().Kind
== BitstreamEntry::SubBlock
);
2048 assert(MaybeEntry
.get().ID
== bitc::VALUE_SYMTAB_BLOCK_ID
);
2052 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
,
2054 ArrayRef
<uint64_t> Record
) {
2055 // Note that we subtract 1 here because the offset is relative to one word
2056 // before the start of the identification or module block, which was
2057 // historically always the start of the regular bitcode header.
2058 uint64_t FuncWordOffset
= Record
[1] - 1;
2059 uint64_t FuncBitOffset
= FuncWordOffset
* 32;
2060 DeferredFunctionInfo
[F
] = FuncBitOffset
+ FuncBitcodeOffsetDelta
;
2061 // Set the LastFunctionBlockBit to point to the last function block.
2062 // Later when parsing is resumed after function materialization,
2063 // we can simply skip that last function block.
2064 if (FuncBitOffset
> LastFunctionBlockBit
)
2065 LastFunctionBlockBit
= FuncBitOffset
;
2068 /// Read a new-style GlobalValue symbol table.
2069 Error
BitcodeReader::parseGlobalValueSymbolTable() {
2070 unsigned FuncBitcodeOffsetDelta
=
2071 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
2073 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
2076 SmallVector
<uint64_t, 64> Record
;
2078 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2080 return MaybeEntry
.takeError();
2081 BitstreamEntry Entry
= MaybeEntry
.get();
2083 switch (Entry
.Kind
) {
2084 case BitstreamEntry::SubBlock
:
2085 case BitstreamEntry::Error
:
2086 return error("Malformed block");
2087 case BitstreamEntry::EndBlock
:
2088 return Error::success();
2089 case BitstreamEntry::Record
:
2094 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2096 return MaybeRecord
.takeError();
2097 switch (MaybeRecord
.get()) {
2098 case bitc::VST_CODE_FNENTRY
: // [valueid, offset]
2099 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
,
2100 cast
<Function
>(ValueList
[Record
[0]]), Record
);
2106 /// Parse the value symbol table at either the current parsing location or
2107 /// at the given bit offset if provided.
2108 Error
BitcodeReader::parseValueSymbolTable(uint64_t Offset
) {
2109 uint64_t CurrentBit
;
2110 // Pass in the Offset to distinguish between calling for the module-level
2111 // VST (where we want to jump to the VST offset) and the function-level
2112 // VST (where we don't).
2114 Expected
<uint64_t> MaybeCurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
2115 if (!MaybeCurrentBit
)
2116 return MaybeCurrentBit
.takeError();
2117 CurrentBit
= MaybeCurrentBit
.get();
2118 // If this module uses a string table, read this as a module-level VST.
2120 if (Error Err
= parseGlobalValueSymbolTable())
2122 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
2124 return Error::success();
2126 // Otherwise, the VST will be in a similar format to a function-level VST,
2127 // and will contain symbol names.
2130 // Compute the delta between the bitcode indices in the VST (the word offset
2131 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2132 // expected by the lazy reader. The reader's EnterSubBlock expects to have
2133 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2134 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2135 // just before entering the VST subblock because: 1) the EnterSubBlock
2136 // changes the AbbrevID width; 2) the VST block is nested within the same
2137 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2138 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2139 // jump to the FUNCTION_BLOCK using this offset later, we don't want
2140 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2141 unsigned FuncBitcodeOffsetDelta
=
2142 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
2144 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
2147 SmallVector
<uint64_t, 64> Record
;
2149 Triple
TT(TheModule
->getTargetTriple());
2151 // Read all the records for this value table.
2152 SmallString
<128> ValueName
;
2155 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2157 return MaybeEntry
.takeError();
2158 BitstreamEntry Entry
= MaybeEntry
.get();
2160 switch (Entry
.Kind
) {
2161 case BitstreamEntry::SubBlock
: // Handled for us already.
2162 case BitstreamEntry::Error
:
2163 return error("Malformed block");
2164 case BitstreamEntry::EndBlock
:
2166 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
2168 return Error::success();
2169 case BitstreamEntry::Record
:
2170 // The interesting case.
2176 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2178 return MaybeRecord
.takeError();
2179 switch (MaybeRecord
.get()) {
2180 default: // Default behavior: unknown type.
2182 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
2183 Expected
<Value
*> ValOrErr
= recordValue(Record
, 1, TT
);
2184 if (Error Err
= ValOrErr
.takeError())
2189 case bitc::VST_CODE_FNENTRY
: {
2190 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2191 Expected
<Value
*> ValOrErr
= recordValue(Record
, 2, TT
);
2192 if (Error Err
= ValOrErr
.takeError())
2194 Value
*V
= ValOrErr
.get();
2196 // Ignore function offsets emitted for aliases of functions in older
2197 // versions of LLVM.
2198 if (auto *F
= dyn_cast
<Function
>(V
))
2199 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
, F
, Record
);
2202 case bitc::VST_CODE_BBENTRY
: {
2203 if (convertToString(Record
, 1, ValueName
))
2204 return error("Invalid record");
2205 BasicBlock
*BB
= getBasicBlock(Record
[0]);
2207 return error("Invalid record");
2209 BB
->setName(StringRef(ValueName
.data(), ValueName
.size()));
2217 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2219 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V
) {
2224 // There is no such thing as -0 with integers. "-0" really means MININT.
2228 /// Resolve all of the initializers for global values and aliases that we can.
2229 Error
BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2230 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInitWorklist
;
2231 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>
2232 IndirectSymbolInitWorklist
;
2233 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixWorklist
;
2234 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologueWorklist
;
2235 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFnWorklist
;
2237 GlobalInitWorklist
.swap(GlobalInits
);
2238 IndirectSymbolInitWorklist
.swap(IndirectSymbolInits
);
2239 FunctionPrefixWorklist
.swap(FunctionPrefixes
);
2240 FunctionPrologueWorklist
.swap(FunctionPrologues
);
2241 FunctionPersonalityFnWorklist
.swap(FunctionPersonalityFns
);
2243 while (!GlobalInitWorklist
.empty()) {
2244 unsigned ValID
= GlobalInitWorklist
.back().second
;
2245 if (ValID
>= ValueList
.size()) {
2246 // Not ready to resolve this yet, it requires something later in the file.
2247 GlobalInits
.push_back(GlobalInitWorklist
.back());
2249 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2250 GlobalInitWorklist
.back().first
->setInitializer(C
);
2252 return error("Expected a constant");
2254 GlobalInitWorklist
.pop_back();
2257 while (!IndirectSymbolInitWorklist
.empty()) {
2258 unsigned ValID
= IndirectSymbolInitWorklist
.back().second
;
2259 if (ValID
>= ValueList
.size()) {
2260 IndirectSymbolInits
.push_back(IndirectSymbolInitWorklist
.back());
2262 Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]);
2264 return error("Expected a constant");
2265 GlobalIndirectSymbol
*GIS
= IndirectSymbolInitWorklist
.back().first
;
2266 if (isa
<GlobalAlias
>(GIS
) && C
->getType() != GIS
->getType())
2267 return error("Alias and aliasee types don't match");
2268 GIS
->setIndirectSymbol(C
);
2270 IndirectSymbolInitWorklist
.pop_back();
2273 while (!FunctionPrefixWorklist
.empty()) {
2274 unsigned ValID
= FunctionPrefixWorklist
.back().second
;
2275 if (ValID
>= ValueList
.size()) {
2276 FunctionPrefixes
.push_back(FunctionPrefixWorklist
.back());
2278 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2279 FunctionPrefixWorklist
.back().first
->setPrefixData(C
);
2281 return error("Expected a constant");
2283 FunctionPrefixWorklist
.pop_back();
2286 while (!FunctionPrologueWorklist
.empty()) {
2287 unsigned ValID
= FunctionPrologueWorklist
.back().second
;
2288 if (ValID
>= ValueList
.size()) {
2289 FunctionPrologues
.push_back(FunctionPrologueWorklist
.back());
2291 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2292 FunctionPrologueWorklist
.back().first
->setPrologueData(C
);
2294 return error("Expected a constant");
2296 FunctionPrologueWorklist
.pop_back();
2299 while (!FunctionPersonalityFnWorklist
.empty()) {
2300 unsigned ValID
= FunctionPersonalityFnWorklist
.back().second
;
2301 if (ValID
>= ValueList
.size()) {
2302 FunctionPersonalityFns
.push_back(FunctionPersonalityFnWorklist
.back());
2304 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2305 FunctionPersonalityFnWorklist
.back().first
->setPersonalityFn(C
);
2307 return error("Expected a constant");
2309 FunctionPersonalityFnWorklist
.pop_back();
2312 return Error::success();
2315 static APInt
readWideAPInt(ArrayRef
<uint64_t> Vals
, unsigned TypeBits
) {
2316 SmallVector
<uint64_t, 8> Words(Vals
.size());
2317 transform(Vals
, Words
.begin(),
2318 BitcodeReader::decodeSignRotatedValue
);
2320 return APInt(TypeBits
, Words
);
2323 Error
BitcodeReader::parseConstants() {
2324 if (Error Err
= Stream
.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID
))
2327 SmallVector
<uint64_t, 64> Record
;
2329 // Read all the records for this value table.
2330 Type
*CurTy
= Type::getInt32Ty(Context
);
2331 Type
*CurFullTy
= Type::getInt32Ty(Context
);
2332 unsigned NextCstNo
= ValueList
.size();
2335 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2337 return MaybeEntry
.takeError();
2338 BitstreamEntry Entry
= MaybeEntry
.get();
2340 switch (Entry
.Kind
) {
2341 case BitstreamEntry::SubBlock
: // Handled for us already.
2342 case BitstreamEntry::Error
:
2343 return error("Malformed block");
2344 case BitstreamEntry::EndBlock
:
2345 if (NextCstNo
!= ValueList
.size())
2346 return error("Invalid constant reference");
2348 // Once all the constants have been read, go through and resolve forward
2350 ValueList
.resolveConstantForwardRefs();
2351 return Error::success();
2352 case BitstreamEntry::Record
:
2353 // The interesting case.
2359 Type
*VoidType
= Type::getVoidTy(Context
);
2361 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
2363 return MaybeBitCode
.takeError();
2364 switch (unsigned BitCode
= MaybeBitCode
.get()) {
2365 default: // Default behavior: unknown constant
2366 case bitc::CST_CODE_UNDEF
: // UNDEF
2367 V
= UndefValue::get(CurTy
);
2369 case bitc::CST_CODE_SETTYPE
: // SETTYPE: [typeid]
2371 return error("Invalid record");
2372 if (Record
[0] >= TypeList
.size() || !TypeList
[Record
[0]])
2373 return error("Invalid record");
2374 if (TypeList
[Record
[0]] == VoidType
)
2375 return error("Invalid constant type");
2376 CurFullTy
= TypeList
[Record
[0]];
2377 CurTy
= flattenPointerTypes(CurFullTy
);
2378 continue; // Skip the ValueList manipulation.
2379 case bitc::CST_CODE_NULL
: // NULL
2380 V
= Constant::getNullValue(CurTy
);
2382 case bitc::CST_CODE_INTEGER
: // INTEGER: [intval]
2383 if (!CurTy
->isIntegerTy() || Record
.empty())
2384 return error("Invalid record");
2385 V
= ConstantInt::get(CurTy
, decodeSignRotatedValue(Record
[0]));
2387 case bitc::CST_CODE_WIDE_INTEGER
: {// WIDE_INTEGER: [n x intval]
2388 if (!CurTy
->isIntegerTy() || Record
.empty())
2389 return error("Invalid record");
2392 readWideAPInt(Record
, cast
<IntegerType
>(CurTy
)->getBitWidth());
2393 V
= ConstantInt::get(Context
, VInt
);
2397 case bitc::CST_CODE_FLOAT
: { // FLOAT: [fpval]
2399 return error("Invalid record");
2400 if (CurTy
->isHalfTy())
2401 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEhalf(),
2402 APInt(16, (uint16_t)Record
[0])));
2403 else if (CurTy
->isFloatTy())
2404 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEsingle(),
2405 APInt(32, (uint32_t)Record
[0])));
2406 else if (CurTy
->isDoubleTy())
2407 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEdouble(),
2408 APInt(64, Record
[0])));
2409 else if (CurTy
->isX86_FP80Ty()) {
2410 // Bits are not stored the same way as a normal i80 APInt, compensate.
2411 uint64_t Rearrange
[2];
2412 Rearrange
[0] = (Record
[1] & 0xffffLL
) | (Record
[0] << 16);
2413 Rearrange
[1] = Record
[0] >> 48;
2414 V
= ConstantFP::get(Context
, APFloat(APFloat::x87DoubleExtended(),
2415 APInt(80, Rearrange
)));
2416 } else if (CurTy
->isFP128Ty())
2417 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEquad(),
2418 APInt(128, Record
)));
2419 else if (CurTy
->isPPC_FP128Ty())
2420 V
= ConstantFP::get(Context
, APFloat(APFloat::PPCDoubleDouble(),
2421 APInt(128, Record
)));
2423 V
= UndefValue::get(CurTy
);
2427 case bitc::CST_CODE_AGGREGATE
: {// AGGREGATE: [n x value number]
2429 return error("Invalid record");
2431 unsigned Size
= Record
.size();
2432 SmallVector
<Constant
*, 16> Elts
;
2434 if (StructType
*STy
= dyn_cast
<StructType
>(CurTy
)) {
2435 for (unsigned i
= 0; i
!= Size
; ++i
)
2436 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
],
2437 STy
->getElementType(i
)));
2438 V
= ConstantStruct::get(STy
, Elts
);
2439 } else if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(CurTy
)) {
2440 Type
*EltTy
= ATy
->getElementType();
2441 for (unsigned i
= 0; i
!= Size
; ++i
)
2442 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2443 V
= ConstantArray::get(ATy
, Elts
);
2444 } else if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
)) {
2445 Type
*EltTy
= VTy
->getElementType();
2446 for (unsigned i
= 0; i
!= Size
; ++i
)
2447 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2448 V
= ConstantVector::get(Elts
);
2450 V
= UndefValue::get(CurTy
);
2454 case bitc::CST_CODE_STRING
: // STRING: [values]
2455 case bitc::CST_CODE_CSTRING
: { // CSTRING: [values]
2457 return error("Invalid record");
2459 SmallString
<16> Elts(Record
.begin(), Record
.end());
2460 V
= ConstantDataArray::getString(Context
, Elts
,
2461 BitCode
== bitc::CST_CODE_CSTRING
);
2464 case bitc::CST_CODE_DATA
: {// DATA: [n x value]
2466 return error("Invalid record");
2468 Type
*EltTy
= cast
<SequentialType
>(CurTy
)->getElementType();
2469 if (EltTy
->isIntegerTy(8)) {
2470 SmallVector
<uint8_t, 16> Elts(Record
.begin(), Record
.end());
2471 if (isa
<VectorType
>(CurTy
))
2472 V
= ConstantDataVector::get(Context
, Elts
);
2474 V
= ConstantDataArray::get(Context
, Elts
);
2475 } else if (EltTy
->isIntegerTy(16)) {
2476 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2477 if (isa
<VectorType
>(CurTy
))
2478 V
= ConstantDataVector::get(Context
, Elts
);
2480 V
= ConstantDataArray::get(Context
, Elts
);
2481 } else if (EltTy
->isIntegerTy(32)) {
2482 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2483 if (isa
<VectorType
>(CurTy
))
2484 V
= ConstantDataVector::get(Context
, Elts
);
2486 V
= ConstantDataArray::get(Context
, Elts
);
2487 } else if (EltTy
->isIntegerTy(64)) {
2488 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2489 if (isa
<VectorType
>(CurTy
))
2490 V
= ConstantDataVector::get(Context
, Elts
);
2492 V
= ConstantDataArray::get(Context
, Elts
);
2493 } else if (EltTy
->isHalfTy()) {
2494 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2495 if (isa
<VectorType
>(CurTy
))
2496 V
= ConstantDataVector::getFP(Context
, Elts
);
2498 V
= ConstantDataArray::getFP(Context
, Elts
);
2499 } else if (EltTy
->isFloatTy()) {
2500 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2501 if (isa
<VectorType
>(CurTy
))
2502 V
= ConstantDataVector::getFP(Context
, Elts
);
2504 V
= ConstantDataArray::getFP(Context
, Elts
);
2505 } else if (EltTy
->isDoubleTy()) {
2506 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2507 if (isa
<VectorType
>(CurTy
))
2508 V
= ConstantDataVector::getFP(Context
, Elts
);
2510 V
= ConstantDataArray::getFP(Context
, Elts
);
2512 return error("Invalid type for value");
2516 case bitc::CST_CODE_CE_UNOP
: { // CE_UNOP: [opcode, opval]
2517 if (Record
.size() < 2)
2518 return error("Invalid record");
2519 int Opc
= getDecodedUnaryOpcode(Record
[0], CurTy
);
2521 V
= UndefValue::get(CurTy
); // Unknown unop.
2523 Constant
*LHS
= ValueList
.getConstantFwdRef(Record
[1], CurTy
);
2525 V
= ConstantExpr::get(Opc
, LHS
, Flags
);
2529 case bitc::CST_CODE_CE_BINOP
: { // CE_BINOP: [opcode, opval, opval]
2530 if (Record
.size() < 3)
2531 return error("Invalid record");
2532 int Opc
= getDecodedBinaryOpcode(Record
[0], CurTy
);
2534 V
= UndefValue::get(CurTy
); // Unknown binop.
2536 Constant
*LHS
= ValueList
.getConstantFwdRef(Record
[1], CurTy
);
2537 Constant
*RHS
= ValueList
.getConstantFwdRef(Record
[2], CurTy
);
2539 if (Record
.size() >= 4) {
2540 if (Opc
== Instruction::Add
||
2541 Opc
== Instruction::Sub
||
2542 Opc
== Instruction::Mul
||
2543 Opc
== Instruction::Shl
) {
2544 if (Record
[3] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
2545 Flags
|= OverflowingBinaryOperator::NoSignedWrap
;
2546 if (Record
[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
2547 Flags
|= OverflowingBinaryOperator::NoUnsignedWrap
;
2548 } else if (Opc
== Instruction::SDiv
||
2549 Opc
== Instruction::UDiv
||
2550 Opc
== Instruction::LShr
||
2551 Opc
== Instruction::AShr
) {
2552 if (Record
[3] & (1 << bitc::PEO_EXACT
))
2553 Flags
|= SDivOperator::IsExact
;
2556 V
= ConstantExpr::get(Opc
, LHS
, RHS
, Flags
);
2560 case bitc::CST_CODE_CE_CAST
: { // CE_CAST: [opcode, opty, opval]
2561 if (Record
.size() < 3)
2562 return error("Invalid record");
2563 int Opc
= getDecodedCastOpcode(Record
[0]);
2565 V
= UndefValue::get(CurTy
); // Unknown cast.
2567 Type
*OpTy
= getTypeByID(Record
[1]);
2569 return error("Invalid record");
2570 Constant
*Op
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2571 V
= UpgradeBitCastExpr(Opc
, Op
, CurTy
);
2572 if (!V
) V
= ConstantExpr::getCast(Opc
, Op
, CurTy
);
2576 case bitc::CST_CODE_CE_INBOUNDS_GEP
: // [ty, n x operands]
2577 case bitc::CST_CODE_CE_GEP
: // [ty, n x operands]
2578 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
: { // [ty, flags, n x
2581 Type
*PointeeType
= nullptr;
2582 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
||
2584 PointeeType
= getTypeByID(Record
[OpNum
++]);
2586 bool InBounds
= false;
2587 Optional
<unsigned> InRangeIndex
;
2588 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
) {
2589 uint64_t Op
= Record
[OpNum
++];
2591 InRangeIndex
= Op
>> 1;
2592 } else if (BitCode
== bitc::CST_CODE_CE_INBOUNDS_GEP
)
2595 SmallVector
<Constant
*, 16> Elts
;
2596 Type
*Elt0FullTy
= nullptr;
2597 while (OpNum
!= Record
.size()) {
2599 Elt0FullTy
= getFullyStructuredTypeByID(Record
[OpNum
]);
2600 Type
*ElTy
= getTypeByID(Record
[OpNum
++]);
2602 return error("Invalid record");
2603 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[OpNum
++], ElTy
));
2606 if (Elts
.size() < 1)
2607 return error("Invalid gep with no operands");
2609 Type
*ImplicitPointeeType
=
2610 getPointerElementFlatType(Elt0FullTy
->getScalarType());
2612 PointeeType
= ImplicitPointeeType
;
2613 else if (PointeeType
!= ImplicitPointeeType
)
2614 return error("Explicit gep operator type does not match pointee type "
2615 "of pointer operand");
2617 ArrayRef
<Constant
*> Indices(Elts
.begin() + 1, Elts
.end());
2618 V
= ConstantExpr::getGetElementPtr(PointeeType
, Elts
[0], Indices
,
2619 InBounds
, InRangeIndex
);
2622 case bitc::CST_CODE_CE_SELECT
: { // CE_SELECT: [opval#, opval#, opval#]
2623 if (Record
.size() < 3)
2624 return error("Invalid record");
2626 Type
*SelectorTy
= Type::getInt1Ty(Context
);
2628 // The selector might be an i1 or an <n x i1>
2629 // Get the type from the ValueList before getting a forward ref.
2630 if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
))
2631 if (Value
*V
= ValueList
[Record
[0]])
2632 if (SelectorTy
!= V
->getType())
2633 SelectorTy
= VectorType::get(SelectorTy
, VTy
->getNumElements());
2635 V
= ConstantExpr::getSelect(ValueList
.getConstantFwdRef(Record
[0],
2637 ValueList
.getConstantFwdRef(Record
[1],CurTy
),
2638 ValueList
.getConstantFwdRef(Record
[2],CurTy
));
2641 case bitc::CST_CODE_CE_EXTRACTELT
2642 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2643 if (Record
.size() < 3)
2644 return error("Invalid record");
2646 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2648 return error("Invalid record");
2649 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2650 Constant
*Op1
= nullptr;
2651 if (Record
.size() == 4) {
2652 Type
*IdxTy
= getTypeByID(Record
[2]);
2654 return error("Invalid record");
2655 Op1
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2656 } else // TODO: Remove with llvm 4.0
2657 Op1
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2659 return error("Invalid record");
2660 V
= ConstantExpr::getExtractElement(Op0
, Op1
);
2663 case bitc::CST_CODE_CE_INSERTELT
2664 : { // CE_INSERTELT: [opval, opval, opty, opval]
2665 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2666 if (Record
.size() < 3 || !OpTy
)
2667 return error("Invalid record");
2668 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[0], OpTy
);
2669 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[1],
2670 OpTy
->getElementType());
2671 Constant
*Op2
= nullptr;
2672 if (Record
.size() == 4) {
2673 Type
*IdxTy
= getTypeByID(Record
[2]);
2675 return error("Invalid record");
2676 Op2
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2677 } else // TODO: Remove with llvm 4.0
2678 Op2
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2680 return error("Invalid record");
2681 V
= ConstantExpr::getInsertElement(Op0
, Op1
, Op2
);
2684 case bitc::CST_CODE_CE_SHUFFLEVEC
: { // CE_SHUFFLEVEC: [opval, opval, opval]
2685 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2686 if (Record
.size() < 3 || !OpTy
)
2687 return error("Invalid record");
2688 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[0], OpTy
);
2689 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2690 Type
*ShufTy
= VectorType::get(Type::getInt32Ty(Context
),
2691 OpTy
->getNumElements());
2692 Constant
*Op2
= ValueList
.getConstantFwdRef(Record
[2], ShufTy
);
2693 V
= ConstantExpr::getShuffleVector(Op0
, Op1
, Op2
);
2696 case bitc::CST_CODE_CE_SHUFVEC_EX
: { // [opty, opval, opval, opval]
2697 VectorType
*RTy
= dyn_cast
<VectorType
>(CurTy
);
2699 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2700 if (Record
.size() < 4 || !RTy
|| !OpTy
)
2701 return error("Invalid record");
2702 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2703 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2704 Type
*ShufTy
= VectorType::get(Type::getInt32Ty(Context
),
2705 RTy
->getNumElements());
2706 Constant
*Op2
= ValueList
.getConstantFwdRef(Record
[3], ShufTy
);
2707 V
= ConstantExpr::getShuffleVector(Op0
, Op1
, Op2
);
2710 case bitc::CST_CODE_CE_CMP
: { // CE_CMP: [opty, opval, opval, pred]
2711 if (Record
.size() < 4)
2712 return error("Invalid record");
2713 Type
*OpTy
= getTypeByID(Record
[0]);
2715 return error("Invalid record");
2716 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2717 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2719 if (OpTy
->isFPOrFPVectorTy())
2720 V
= ConstantExpr::getFCmp(Record
[3], Op0
, Op1
);
2722 V
= ConstantExpr::getICmp(Record
[3], Op0
, Op1
);
2725 // This maintains backward compatibility, pre-asm dialect keywords.
2726 // FIXME: Remove with the 4.0 release.
2727 case bitc::CST_CODE_INLINEASM_OLD
: {
2728 if (Record
.size() < 2)
2729 return error("Invalid record");
2730 std::string AsmStr
, ConstrStr
;
2731 bool HasSideEffects
= Record
[0] & 1;
2732 bool IsAlignStack
= Record
[0] >> 1;
2733 unsigned AsmStrSize
= Record
[1];
2734 if (2+AsmStrSize
>= Record
.size())
2735 return error("Invalid record");
2736 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2737 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2738 return error("Invalid record");
2740 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2741 AsmStr
+= (char)Record
[2+i
];
2742 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2743 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2744 UpgradeInlineAsmString(&AsmStr
);
2746 cast
<FunctionType
>(getPointerElementFlatType(CurFullTy
)), AsmStr
,
2747 ConstrStr
, HasSideEffects
, IsAlignStack
);
2750 // This version adds support for the asm dialect keywords (e.g.,
2752 case bitc::CST_CODE_INLINEASM
: {
2753 if (Record
.size() < 2)
2754 return error("Invalid record");
2755 std::string AsmStr
, ConstrStr
;
2756 bool HasSideEffects
= Record
[0] & 1;
2757 bool IsAlignStack
= (Record
[0] >> 1) & 1;
2758 unsigned AsmDialect
= Record
[0] >> 2;
2759 unsigned AsmStrSize
= Record
[1];
2760 if (2+AsmStrSize
>= Record
.size())
2761 return error("Invalid record");
2762 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2763 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2764 return error("Invalid record");
2766 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2767 AsmStr
+= (char)Record
[2+i
];
2768 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2769 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2770 UpgradeInlineAsmString(&AsmStr
);
2772 cast
<FunctionType
>(getPointerElementFlatType(CurFullTy
)), AsmStr
,
2773 ConstrStr
, HasSideEffects
, IsAlignStack
,
2774 InlineAsm::AsmDialect(AsmDialect
));
2777 case bitc::CST_CODE_BLOCKADDRESS
:{
2778 if (Record
.size() < 3)
2779 return error("Invalid record");
2780 Type
*FnTy
= getTypeByID(Record
[0]);
2782 return error("Invalid record");
2784 dyn_cast_or_null
<Function
>(ValueList
.getConstantFwdRef(Record
[1],FnTy
));
2786 return error("Invalid record");
2788 // If the function is already parsed we can insert the block address right
2791 unsigned BBID
= Record
[2];
2793 // Invalid reference to entry block.
2794 return error("Invalid ID");
2796 Function::iterator BBI
= Fn
->begin(), BBE
= Fn
->end();
2797 for (size_t I
= 0, E
= BBID
; I
!= E
; ++I
) {
2799 return error("Invalid ID");
2804 // Otherwise insert a placeholder and remember it so it can be inserted
2805 // when the function is parsed.
2806 auto &FwdBBs
= BasicBlockFwdRefs
[Fn
];
2808 BasicBlockFwdRefQueue
.push_back(Fn
);
2809 if (FwdBBs
.size() < BBID
+ 1)
2810 FwdBBs
.resize(BBID
+ 1);
2812 FwdBBs
[BBID
] = BasicBlock::Create(Context
);
2815 V
= BlockAddress::get(Fn
, BB
);
2820 assert(V
->getType() == flattenPointerTypes(CurFullTy
) &&
2821 "Incorrect fully structured type provided for Constant");
2822 ValueList
.assignValue(V
, NextCstNo
, CurFullTy
);
2827 Error
BitcodeReader::parseUseLists() {
2828 if (Error Err
= Stream
.EnterSubBlock(bitc::USELIST_BLOCK_ID
))
2831 // Read all the records.
2832 SmallVector
<uint64_t, 64> Record
;
2835 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2837 return MaybeEntry
.takeError();
2838 BitstreamEntry Entry
= MaybeEntry
.get();
2840 switch (Entry
.Kind
) {
2841 case BitstreamEntry::SubBlock
: // Handled for us already.
2842 case BitstreamEntry::Error
:
2843 return error("Malformed block");
2844 case BitstreamEntry::EndBlock
:
2845 return Error::success();
2846 case BitstreamEntry::Record
:
2847 // The interesting case.
2851 // Read a use list record.
2854 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2856 return MaybeRecord
.takeError();
2857 switch (MaybeRecord
.get()) {
2858 default: // Default behavior: unknown type.
2860 case bitc::USELIST_CODE_BB
:
2863 case bitc::USELIST_CODE_DEFAULT
: {
2864 unsigned RecordLength
= Record
.size();
2865 if (RecordLength
< 3)
2866 // Records should have at least an ID and two indexes.
2867 return error("Invalid record");
2868 unsigned ID
= Record
.back();
2873 assert(ID
< FunctionBBs
.size() && "Basic block not found");
2874 V
= FunctionBBs
[ID
];
2877 unsigned NumUses
= 0;
2878 SmallDenseMap
<const Use
*, unsigned, 16> Order
;
2879 for (const Use
&U
: V
->materialized_uses()) {
2880 if (++NumUses
> Record
.size())
2882 Order
[&U
] = Record
[NumUses
- 1];
2884 if (Order
.size() != Record
.size() || NumUses
> Record
.size())
2885 // Mismatches can happen if the functions are being materialized lazily
2886 // (out-of-order), or a value has been upgraded.
2889 V
->sortUseList([&](const Use
&L
, const Use
&R
) {
2890 return Order
.lookup(&L
) < Order
.lookup(&R
);
2898 /// When we see the block for metadata, remember where it is and then skip it.
2899 /// This lets us lazily deserialize the metadata.
2900 Error
BitcodeReader::rememberAndSkipMetadata() {
2901 // Save the current stream state.
2902 uint64_t CurBit
= Stream
.GetCurrentBitNo();
2903 DeferredMetadataInfo
.push_back(CurBit
);
2905 // Skip over the block for now.
2906 if (Error Err
= Stream
.SkipBlock())
2908 return Error::success();
2911 Error
BitcodeReader::materializeMetadata() {
2912 for (uint64_t BitPos
: DeferredMetadataInfo
) {
2913 // Move the bit stream to the saved position.
2914 if (Error JumpFailed
= Stream
.JumpToBit(BitPos
))
2916 if (Error Err
= MDLoader
->parseModuleMetadata())
2920 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2922 if (Metadata
*Val
= TheModule
->getModuleFlag("Linker Options")) {
2923 NamedMDNode
*LinkerOpts
=
2924 TheModule
->getOrInsertNamedMetadata("llvm.linker.options");
2925 for (const MDOperand
&MDOptions
: cast
<MDNode
>(Val
)->operands())
2926 LinkerOpts
->addOperand(cast
<MDNode
>(MDOptions
));
2929 DeferredMetadataInfo
.clear();
2930 return Error::success();
2933 void BitcodeReader::setStripDebugInfo() { StripDebugInfo
= true; }
2935 /// When we see the block for a function body, remember where it is and then
2936 /// skip it. This lets us lazily deserialize the functions.
2937 Error
BitcodeReader::rememberAndSkipFunctionBody() {
2938 // Get the function we are talking about.
2939 if (FunctionsWithBodies
.empty())
2940 return error("Insufficient function protos");
2942 Function
*Fn
= FunctionsWithBodies
.back();
2943 FunctionsWithBodies
.pop_back();
2945 // Save the current stream state.
2946 uint64_t CurBit
= Stream
.GetCurrentBitNo();
2948 (DeferredFunctionInfo
[Fn
] == 0 || DeferredFunctionInfo
[Fn
] == CurBit
) &&
2949 "Mismatch between VST and scanned function offsets");
2950 DeferredFunctionInfo
[Fn
] = CurBit
;
2952 // Skip over the function block for now.
2953 if (Error Err
= Stream
.SkipBlock())
2955 return Error::success();
2958 Error
BitcodeReader::globalCleanup() {
2959 // Patch the initializers for globals and aliases up.
2960 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
2962 if (!GlobalInits
.empty() || !IndirectSymbolInits
.empty())
2963 return error("Malformed global initializer set");
2965 // Look for intrinsic functions which need to be upgraded at some point
2966 for (Function
&F
: *TheModule
) {
2967 MDLoader
->upgradeDebugIntrinsics(F
);
2969 if (UpgradeIntrinsicFunction(&F
, NewFn
))
2970 UpgradedIntrinsics
[&F
] = NewFn
;
2971 else if (auto Remangled
= Intrinsic::remangleIntrinsicFunction(&F
))
2972 // Some types could be renamed during loading if several modules are
2973 // loaded in the same LLVMContext (LTO scenario). In this case we should
2974 // remangle intrinsics names as well.
2975 RemangledIntrinsics
[&F
] = Remangled
.getValue();
2978 // Look for global variables which need to be renamed.
2979 std::vector
<std::pair
<GlobalVariable
*, GlobalVariable
*>> UpgradedVariables
;
2980 for (GlobalVariable
&GV
: TheModule
->globals())
2981 if (GlobalVariable
*Upgraded
= UpgradeGlobalVariable(&GV
))
2982 UpgradedVariables
.emplace_back(&GV
, Upgraded
);
2983 for (auto &Pair
: UpgradedVariables
) {
2984 Pair
.first
->eraseFromParent();
2985 TheModule
->getGlobalList().push_back(Pair
.second
);
2988 // Force deallocation of memory for these vectors to favor the client that
2989 // want lazy deserialization.
2990 std::vector
<std::pair
<GlobalVariable
*, unsigned>>().swap(GlobalInits
);
2991 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>().swap(
2992 IndirectSymbolInits
);
2993 return Error::success();
2996 /// Support for lazy parsing of function bodies. This is required if we
2997 /// either have an old bitcode file without a VST forward declaration record,
2998 /// or if we have an anonymous function being materialized, since anonymous
2999 /// functions do not have a name and are therefore not in the VST.
3000 Error
BitcodeReader::rememberAndSkipFunctionBodies() {
3001 if (Error JumpFailed
= Stream
.JumpToBit(NextUnreadBit
))
3004 if (Stream
.AtEndOfStream())
3005 return error("Could not find function in stream");
3007 if (!SeenFirstFunctionBody
)
3008 return error("Trying to materialize functions before seeing function blocks");
3010 // An old bitcode file with the symbol table at the end would have
3011 // finished the parse greedily.
3012 assert(SeenValueSymbolTable
);
3014 SmallVector
<uint64_t, 64> Record
;
3017 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3019 return MaybeEntry
.takeError();
3020 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3022 switch (Entry
.Kind
) {
3024 return error("Expect SubBlock");
3025 case BitstreamEntry::SubBlock
:
3028 return error("Expect function block");
3029 case bitc::FUNCTION_BLOCK_ID
:
3030 if (Error Err
= rememberAndSkipFunctionBody())
3032 NextUnreadBit
= Stream
.GetCurrentBitNo();
3033 return Error::success();
3039 bool BitcodeReaderBase::readBlockInfo() {
3040 Expected
<Optional
<BitstreamBlockInfo
>> MaybeNewBlockInfo
=
3041 Stream
.ReadBlockInfoBlock();
3042 if (!MaybeNewBlockInfo
)
3043 return true; // FIXME Handle the error.
3044 Optional
<BitstreamBlockInfo
> NewBlockInfo
=
3045 std::move(MaybeNewBlockInfo
.get());
3048 BlockInfo
= std::move(*NewBlockInfo
);
3052 Error
BitcodeReader::parseComdatRecord(ArrayRef
<uint64_t> Record
) {
3053 // v1: [selection_kind, name]
3054 // v2: [strtab_offset, strtab_size, selection_kind]
3056 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3059 return error("Invalid record");
3060 Comdat::SelectionKind SK
= getDecodedComdatSelectionKind(Record
[0]);
3061 std::string OldFormatName
;
3063 if (Record
.size() < 2)
3064 return error("Invalid record");
3065 unsigned ComdatNameSize
= Record
[1];
3066 OldFormatName
.reserve(ComdatNameSize
);
3067 for (unsigned i
= 0; i
!= ComdatNameSize
; ++i
)
3068 OldFormatName
+= (char)Record
[2 + i
];
3069 Name
= OldFormatName
;
3071 Comdat
*C
= TheModule
->getOrInsertComdat(Name
);
3072 C
->setSelectionKind(SK
);
3073 ComdatList
.push_back(C
);
3074 return Error::success();
3077 static void inferDSOLocal(GlobalValue
*GV
) {
3078 // infer dso_local from linkage and visibility if it is not encoded.
3079 if (GV
->hasLocalLinkage() ||
3080 (!GV
->hasDefaultVisibility() && !GV
->hasExternalWeakLinkage()))
3081 GV
->setDSOLocal(true);
3084 Error
BitcodeReader::parseGlobalVarRecord(ArrayRef
<uint64_t> Record
) {
3085 // v1: [pointer type, isconst, initid, linkage, alignment, section,
3086 // visibility, threadlocal, unnamed_addr, externally_initialized,
3087 // dllstorageclass, comdat, attributes, preemption specifier,
3088 // partition strtab offset, partition strtab size] (name in VST)
3089 // v2: [strtab_offset, strtab_size, v1]
3091 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3093 if (Record
.size() < 6)
3094 return error("Invalid record");
3095 Type
*FullTy
= getFullyStructuredTypeByID(Record
[0]);
3096 Type
*Ty
= flattenPointerTypes(FullTy
);
3098 return error("Invalid record");
3099 bool isConstant
= Record
[1] & 1;
3100 bool explicitType
= Record
[1] & 2;
3101 unsigned AddressSpace
;
3103 AddressSpace
= Record
[1] >> 2;
3105 if (!Ty
->isPointerTy())
3106 return error("Invalid type for value");
3107 AddressSpace
= cast
<PointerType
>(Ty
)->getAddressSpace();
3108 std::tie(FullTy
, Ty
) = getPointerElementTypes(FullTy
);
3111 uint64_t RawLinkage
= Record
[3];
3112 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
3114 if (Error Err
= parseAlignmentValue(Record
[4], Alignment
))
3116 std::string Section
;
3118 if (Record
[5] - 1 >= SectionTable
.size())
3119 return error("Invalid ID");
3120 Section
= SectionTable
[Record
[5] - 1];
3122 GlobalValue::VisibilityTypes Visibility
= GlobalValue::DefaultVisibility
;
3123 // Local linkage must have default visibility.
3124 if (Record
.size() > 6 && !GlobalValue::isLocalLinkage(Linkage
))
3125 // FIXME: Change to an error if non-default in 4.0.
3126 Visibility
= getDecodedVisibility(Record
[6]);
3128 GlobalVariable::ThreadLocalMode TLM
= GlobalVariable::NotThreadLocal
;
3129 if (Record
.size() > 7)
3130 TLM
= getDecodedThreadLocalMode(Record
[7]);
3132 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
3133 if (Record
.size() > 8)
3134 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[8]);
3136 bool ExternallyInitialized
= false;
3137 if (Record
.size() > 9)
3138 ExternallyInitialized
= Record
[9];
3140 GlobalVariable
*NewGV
=
3141 new GlobalVariable(*TheModule
, Ty
, isConstant
, Linkage
, nullptr, Name
,
3142 nullptr, TLM
, AddressSpace
, ExternallyInitialized
);
3143 NewGV
->setAlignment(Alignment
);
3144 if (!Section
.empty())
3145 NewGV
->setSection(Section
);
3146 NewGV
->setVisibility(Visibility
);
3147 NewGV
->setUnnamedAddr(UnnamedAddr
);
3149 if (Record
.size() > 10)
3150 NewGV
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[10]));
3152 upgradeDLLImportExportLinkage(NewGV
, RawLinkage
);
3154 FullTy
= PointerType::get(FullTy
, AddressSpace
);
3155 assert(NewGV
->getType() == flattenPointerTypes(FullTy
) &&
3156 "Incorrect fully specified type for GlobalVariable");
3157 ValueList
.push_back(NewGV
, FullTy
);
3159 // Remember which value to use for the global initializer.
3160 if (unsigned InitID
= Record
[2])
3161 GlobalInits
.push_back(std::make_pair(NewGV
, InitID
- 1));
3163 if (Record
.size() > 11) {
3164 if (unsigned ComdatID
= Record
[11]) {
3165 if (ComdatID
> ComdatList
.size())
3166 return error("Invalid global variable comdat ID");
3167 NewGV
->setComdat(ComdatList
[ComdatID
- 1]);
3169 } else if (hasImplicitComdat(RawLinkage
)) {
3170 NewGV
->setComdat(reinterpret_cast<Comdat
*>(1));
3173 if (Record
.size() > 12) {
3174 auto AS
= getAttributes(Record
[12]).getFnAttributes();
3175 NewGV
->setAttributes(AS
);
3178 if (Record
.size() > 13) {
3179 NewGV
->setDSOLocal(getDecodedDSOLocal(Record
[13]));
3181 inferDSOLocal(NewGV
);
3183 // Check whether we have enough values to read a partition name.
3184 if (Record
.size() > 15)
3185 NewGV
->setPartition(StringRef(Strtab
.data() + Record
[14], Record
[15]));
3187 return Error::success();
3190 Error
BitcodeReader::parseFunctionRecord(ArrayRef
<uint64_t> Record
) {
3191 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3192 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3193 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
3194 // v2: [strtab_offset, strtab_size, v1]
3196 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3198 if (Record
.size() < 8)
3199 return error("Invalid record");
3200 Type
*FullFTy
= getFullyStructuredTypeByID(Record
[0]);
3201 Type
*FTy
= flattenPointerTypes(FullFTy
);
3203 return error("Invalid record");
3204 if (isa
<PointerType
>(FTy
))
3205 std::tie(FullFTy
, FTy
) = getPointerElementTypes(FullFTy
);
3207 if (!isa
<FunctionType
>(FTy
))
3208 return error("Invalid type for value");
3209 auto CC
= static_cast<CallingConv::ID
>(Record
[1]);
3210 if (CC
& ~CallingConv::MaxID
)
3211 return error("Invalid calling convention ID");
3213 unsigned AddrSpace
= TheModule
->getDataLayout().getProgramAddressSpace();
3214 if (Record
.size() > 16)
3215 AddrSpace
= Record
[16];
3218 Function::Create(cast
<FunctionType
>(FTy
), GlobalValue::ExternalLinkage
,
3219 AddrSpace
, Name
, TheModule
);
3221 assert(Func
->getFunctionType() == flattenPointerTypes(FullFTy
) &&
3222 "Incorrect fully specified type provided for function");
3223 FunctionTypes
[Func
] = cast
<FunctionType
>(FullFTy
);
3225 Func
->setCallingConv(CC
);
3226 bool isProto
= Record
[2];
3227 uint64_t RawLinkage
= Record
[3];
3228 Func
->setLinkage(getDecodedLinkage(RawLinkage
));
3229 Func
->setAttributes(getAttributes(Record
[4]));
3231 // Upgrade any old-style byval without a type by propagating the argument's
3232 // pointee type. There should be no opaque pointers where the byval type is
3234 for (unsigned i
= 0; i
!= Func
->arg_size(); ++i
) {
3235 if (!Func
->hasParamAttribute(i
, Attribute::ByVal
))
3238 Type
*PTy
= cast
<FunctionType
>(FullFTy
)->getParamType(i
);
3239 Func
->removeParamAttr(i
, Attribute::ByVal
);
3240 Func
->addParamAttr(i
, Attribute::getWithByValType(
3241 Context
, getPointerElementFlatType(PTy
)));
3245 if (Error Err
= parseAlignmentValue(Record
[5], Alignment
))
3247 Func
->setAlignment(Alignment
);
3249 if (Record
[6] - 1 >= SectionTable
.size())
3250 return error("Invalid ID");
3251 Func
->setSection(SectionTable
[Record
[6] - 1]);
3253 // Local linkage must have default visibility.
3254 if (!Func
->hasLocalLinkage())
3255 // FIXME: Change to an error if non-default in 4.0.
3256 Func
->setVisibility(getDecodedVisibility(Record
[7]));
3257 if (Record
.size() > 8 && Record
[8]) {
3258 if (Record
[8] - 1 >= GCTable
.size())
3259 return error("Invalid ID");
3260 Func
->setGC(GCTable
[Record
[8] - 1]);
3262 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
3263 if (Record
.size() > 9)
3264 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[9]);
3265 Func
->setUnnamedAddr(UnnamedAddr
);
3266 if (Record
.size() > 10 && Record
[10] != 0)
3267 FunctionPrologues
.push_back(std::make_pair(Func
, Record
[10] - 1));
3269 if (Record
.size() > 11)
3270 Func
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[11]));
3272 upgradeDLLImportExportLinkage(Func
, RawLinkage
);
3274 if (Record
.size() > 12) {
3275 if (unsigned ComdatID
= Record
[12]) {
3276 if (ComdatID
> ComdatList
.size())
3277 return error("Invalid function comdat ID");
3278 Func
->setComdat(ComdatList
[ComdatID
- 1]);
3280 } else if (hasImplicitComdat(RawLinkage
)) {
3281 Func
->setComdat(reinterpret_cast<Comdat
*>(1));
3284 if (Record
.size() > 13 && Record
[13] != 0)
3285 FunctionPrefixes
.push_back(std::make_pair(Func
, Record
[13] - 1));
3287 if (Record
.size() > 14 && Record
[14] != 0)
3288 FunctionPersonalityFns
.push_back(std::make_pair(Func
, Record
[14] - 1));
3290 if (Record
.size() > 15) {
3291 Func
->setDSOLocal(getDecodedDSOLocal(Record
[15]));
3293 inferDSOLocal(Func
);
3295 // Record[16] is the address space number.
3297 // Check whether we have enough values to read a partition name.
3298 if (Record
.size() > 18)
3299 Func
->setPartition(StringRef(Strtab
.data() + Record
[17], Record
[18]));
3301 Type
*FullTy
= PointerType::get(FullFTy
, AddrSpace
);
3302 assert(Func
->getType() == flattenPointerTypes(FullTy
) &&
3303 "Incorrect fully specified type provided for Function");
3304 ValueList
.push_back(Func
, FullTy
);
3306 // If this is a function with a body, remember the prototype we are
3307 // creating now, so that we can match up the body with them later.
3309 Func
->setIsMaterializable(true);
3310 FunctionsWithBodies
.push_back(Func
);
3311 DeferredFunctionInfo
[Func
] = 0;
3313 return Error::success();
3316 Error
BitcodeReader::parseGlobalIndirectSymbolRecord(
3317 unsigned BitCode
, ArrayRef
<uint64_t> Record
) {
3318 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3319 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3320 // dllstorageclass, threadlocal, unnamed_addr,
3321 // preemption specifier] (name in VST)
3322 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3323 // visibility, dllstorageclass, threadlocal, unnamed_addr,
3324 // preemption specifier] (name in VST)
3325 // v2: [strtab_offset, strtab_size, v1]
3327 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3329 bool NewRecord
= BitCode
!= bitc::MODULE_CODE_ALIAS_OLD
;
3330 if (Record
.size() < (3 + (unsigned)NewRecord
))
3331 return error("Invalid record");
3333 Type
*FullTy
= getFullyStructuredTypeByID(Record
[OpNum
++]);
3334 Type
*Ty
= flattenPointerTypes(FullTy
);
3336 return error("Invalid record");
3340 auto *PTy
= dyn_cast
<PointerType
>(Ty
);
3342 return error("Invalid type for value");
3343 std::tie(FullTy
, Ty
) = getPointerElementTypes(FullTy
);
3344 AddrSpace
= PTy
->getAddressSpace();
3346 AddrSpace
= Record
[OpNum
++];
3349 auto Val
= Record
[OpNum
++];
3350 auto Linkage
= Record
[OpNum
++];
3351 GlobalIndirectSymbol
*NewGA
;
3352 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3353 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
)
3354 NewGA
= GlobalAlias::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3357 NewGA
= GlobalIFunc::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3358 nullptr, TheModule
);
3360 assert(NewGA
->getValueType() == flattenPointerTypes(FullTy
) &&
3361 "Incorrect fully structured type provided for GlobalIndirectSymbol");
3362 // Old bitcode files didn't have visibility field.
3363 // Local linkage must have default visibility.
3364 if (OpNum
!= Record
.size()) {
3365 auto VisInd
= OpNum
++;
3366 if (!NewGA
->hasLocalLinkage())
3367 // FIXME: Change to an error if non-default in 4.0.
3368 NewGA
->setVisibility(getDecodedVisibility(Record
[VisInd
]));
3370 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3371 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
) {
3372 if (OpNum
!= Record
.size())
3373 NewGA
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[OpNum
++]));
3375 upgradeDLLImportExportLinkage(NewGA
, Linkage
);
3376 if (OpNum
!= Record
.size())
3377 NewGA
->setThreadLocalMode(getDecodedThreadLocalMode(Record
[OpNum
++]));
3378 if (OpNum
!= Record
.size())
3379 NewGA
->setUnnamedAddr(getDecodedUnnamedAddrType(Record
[OpNum
++]));
3381 if (OpNum
!= Record
.size())
3382 NewGA
->setDSOLocal(getDecodedDSOLocal(Record
[OpNum
++]));
3383 inferDSOLocal(NewGA
);
3385 // Check whether we have enough values to read a partition name.
3386 if (OpNum
+ 1 < Record
.size()) {
3387 NewGA
->setPartition(
3388 StringRef(Strtab
.data() + Record
[OpNum
], Record
[OpNum
+ 1]));
3392 FullTy
= PointerType::get(FullTy
, AddrSpace
);
3393 assert(NewGA
->getType() == flattenPointerTypes(FullTy
) &&
3394 "Incorrect fully structured type provided for GlobalIndirectSymbol");
3395 ValueList
.push_back(NewGA
, FullTy
);
3396 IndirectSymbolInits
.push_back(std::make_pair(NewGA
, Val
));
3397 return Error::success();
3400 Error
BitcodeReader::parseModule(uint64_t ResumeBit
,
3401 bool ShouldLazyLoadMetadata
) {
3403 if (Error JumpFailed
= Stream
.JumpToBit(ResumeBit
))
3405 } else if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
3408 SmallVector
<uint64_t, 64> Record
;
3410 // Read all the records for this module.
3412 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3414 return MaybeEntry
.takeError();
3415 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3417 switch (Entry
.Kind
) {
3418 case BitstreamEntry::Error
:
3419 return error("Malformed block");
3420 case BitstreamEntry::EndBlock
:
3421 return globalCleanup();
3423 case BitstreamEntry::SubBlock
:
3425 default: // Skip unknown content.
3426 if (Error Err
= Stream
.SkipBlock())
3429 case bitc::BLOCKINFO_BLOCK_ID
:
3430 if (readBlockInfo())
3431 return error("Malformed block");
3433 case bitc::PARAMATTR_BLOCK_ID
:
3434 if (Error Err
= parseAttributeBlock())
3437 case bitc::PARAMATTR_GROUP_BLOCK_ID
:
3438 if (Error Err
= parseAttributeGroupBlock())
3441 case bitc::TYPE_BLOCK_ID_NEW
:
3442 if (Error Err
= parseTypeTable())
3445 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3446 if (!SeenValueSymbolTable
) {
3447 // Either this is an old form VST without function index and an
3448 // associated VST forward declaration record (which would have caused
3449 // the VST to be jumped to and parsed before it was encountered
3450 // normally in the stream), or there were no function blocks to
3451 // trigger an earlier parsing of the VST.
3452 assert(VSTOffset
== 0 || FunctionsWithBodies
.empty());
3453 if (Error Err
= parseValueSymbolTable())
3455 SeenValueSymbolTable
= true;
3457 // We must have had a VST forward declaration record, which caused
3458 // the parser to jump to and parse the VST earlier.
3459 assert(VSTOffset
> 0);
3460 if (Error Err
= Stream
.SkipBlock())
3464 case bitc::CONSTANTS_BLOCK_ID
:
3465 if (Error Err
= parseConstants())
3467 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
3470 case bitc::METADATA_BLOCK_ID
:
3471 if (ShouldLazyLoadMetadata
) {
3472 if (Error Err
= rememberAndSkipMetadata())
3476 assert(DeferredMetadataInfo
.empty() && "Unexpected deferred metadata");
3477 if (Error Err
= MDLoader
->parseModuleMetadata())
3480 case bitc::METADATA_KIND_BLOCK_ID
:
3481 if (Error Err
= MDLoader
->parseMetadataKinds())
3484 case bitc::FUNCTION_BLOCK_ID
:
3485 // If this is the first function body we've seen, reverse the
3486 // FunctionsWithBodies list.
3487 if (!SeenFirstFunctionBody
) {
3488 std::reverse(FunctionsWithBodies
.begin(), FunctionsWithBodies
.end());
3489 if (Error Err
= globalCleanup())
3491 SeenFirstFunctionBody
= true;
3494 if (VSTOffset
> 0) {
3495 // If we have a VST forward declaration record, make sure we
3496 // parse the VST now if we haven't already. It is needed to
3497 // set up the DeferredFunctionInfo vector for lazy reading.
3498 if (!SeenValueSymbolTable
) {
3499 if (Error Err
= BitcodeReader::parseValueSymbolTable(VSTOffset
))
3501 SeenValueSymbolTable
= true;
3502 // Fall through so that we record the NextUnreadBit below.
3503 // This is necessary in case we have an anonymous function that
3504 // is later materialized. Since it will not have a VST entry we
3505 // need to fall back to the lazy parse to find its offset.
3507 // If we have a VST forward declaration record, but have already
3508 // parsed the VST (just above, when the first function body was
3509 // encountered here), then we are resuming the parse after
3510 // materializing functions. The ResumeBit points to the
3511 // start of the last function block recorded in the
3512 // DeferredFunctionInfo map. Skip it.
3513 if (Error Err
= Stream
.SkipBlock())
3519 // Support older bitcode files that did not have the function
3520 // index in the VST, nor a VST forward declaration record, as
3521 // well as anonymous functions that do not have VST entries.
3522 // Build the DeferredFunctionInfo vector on the fly.
3523 if (Error Err
= rememberAndSkipFunctionBody())
3526 // Suspend parsing when we reach the function bodies. Subsequent
3527 // materialization calls will resume it when necessary. If the bitcode
3528 // file is old, the symbol table will be at the end instead and will not
3529 // have been seen yet. In this case, just finish the parse now.
3530 if (SeenValueSymbolTable
) {
3531 NextUnreadBit
= Stream
.GetCurrentBitNo();
3532 // After the VST has been parsed, we need to make sure intrinsic name
3533 // are auto-upgraded.
3534 return globalCleanup();
3537 case bitc::USELIST_BLOCK_ID
:
3538 if (Error Err
= parseUseLists())
3541 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
:
3542 if (Error Err
= parseOperandBundleTags())
3545 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID
:
3546 if (Error Err
= parseSyncScopeNames())
3552 case BitstreamEntry::Record
:
3553 // The interesting case.
3558 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3560 return MaybeBitCode
.takeError();
3561 switch (unsigned BitCode
= MaybeBitCode
.get()) {
3562 default: break; // Default behavior, ignore unknown content.
3563 case bitc::MODULE_CODE_VERSION
: {
3564 Expected
<unsigned> VersionOrErr
= parseVersionRecord(Record
);
3566 return VersionOrErr
.takeError();
3567 UseRelativeIDs
= *VersionOrErr
>= 1;
3570 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
3572 if (convertToString(Record
, 0, S
))
3573 return error("Invalid record");
3574 TheModule
->setTargetTriple(S
);
3577 case bitc::MODULE_CODE_DATALAYOUT
: { // DATALAYOUT: [strchr x N]
3579 if (convertToString(Record
, 0, S
))
3580 return error("Invalid record");
3581 TheModule
->setDataLayout(S
);
3584 case bitc::MODULE_CODE_ASM
: { // ASM: [strchr x N]
3586 if (convertToString(Record
, 0, S
))
3587 return error("Invalid record");
3588 TheModule
->setModuleInlineAsm(S
);
3591 case bitc::MODULE_CODE_DEPLIB
: { // DEPLIB: [strchr x N]
3592 // FIXME: Remove in 4.0.
3594 if (convertToString(Record
, 0, S
))
3595 return error("Invalid record");
3599 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
3601 if (convertToString(Record
, 0, S
))
3602 return error("Invalid record");
3603 SectionTable
.push_back(S
);
3606 case bitc::MODULE_CODE_GCNAME
: { // SECTIONNAME: [strchr x N]
3608 if (convertToString(Record
, 0, S
))
3609 return error("Invalid record");
3610 GCTable
.push_back(S
);
3613 case bitc::MODULE_CODE_COMDAT
:
3614 if (Error Err
= parseComdatRecord(Record
))
3617 case bitc::MODULE_CODE_GLOBALVAR
:
3618 if (Error Err
= parseGlobalVarRecord(Record
))
3621 case bitc::MODULE_CODE_FUNCTION
:
3622 if (Error Err
= parseFunctionRecord(Record
))
3625 case bitc::MODULE_CODE_IFUNC
:
3626 case bitc::MODULE_CODE_ALIAS
:
3627 case bitc::MODULE_CODE_ALIAS_OLD
:
3628 if (Error Err
= parseGlobalIndirectSymbolRecord(BitCode
, Record
))
3631 /// MODULE_CODE_VSTOFFSET: [offset]
3632 case bitc::MODULE_CODE_VSTOFFSET
:
3633 if (Record
.size() < 1)
3634 return error("Invalid record");
3635 // Note that we subtract 1 here because the offset is relative to one word
3636 // before the start of the identification or module block, which was
3637 // historically always the start of the regular bitcode header.
3638 VSTOffset
= Record
[0] - 1;
3640 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3641 case bitc::MODULE_CODE_SOURCE_FILENAME
:
3642 SmallString
<128> ValueName
;
3643 if (convertToString(Record
, 0, ValueName
))
3644 return error("Invalid record");
3645 TheModule
->setSourceFileName(ValueName
);
3652 Error
BitcodeReader::parseBitcodeInto(Module
*M
, bool ShouldLazyLoadMetadata
,
3655 MDLoader
= MetadataLoader(Stream
, *M
, ValueList
, IsImporting
,
3656 [&](unsigned ID
) { return getTypeByID(ID
); });
3657 return parseModule(0, ShouldLazyLoadMetadata
);
3660 Error
BitcodeReader::typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
) {
3661 if (!isa
<PointerType
>(PtrType
))
3662 return error("Load/Store operand is not a pointer type");
3663 Type
*ElemType
= cast
<PointerType
>(PtrType
)->getElementType();
3665 if (ValType
&& ValType
!= ElemType
)
3666 return error("Explicit load/store type does not match pointee "
3667 "type of pointer operand");
3668 if (!PointerType::isLoadableOrStorableType(ElemType
))
3669 return error("Cannot load/store from pointer");
3670 return Error::success();
3673 void BitcodeReader::propagateByValTypes(CallBase
*CB
,
3674 ArrayRef
<Type
*> ArgsFullTys
) {
3675 for (unsigned i
= 0; i
!= CB
->arg_size(); ++i
) {
3676 if (!CB
->paramHasAttr(i
, Attribute::ByVal
))
3679 CB
->removeParamAttr(i
, Attribute::ByVal
);
3681 i
, Attribute::getWithByValType(
3682 Context
, getPointerElementFlatType(ArgsFullTys
[i
])));
3686 /// Lazily parse the specified function body block.
3687 Error
BitcodeReader::parseFunctionBody(Function
*F
) {
3688 if (Error Err
= Stream
.EnterSubBlock(bitc::FUNCTION_BLOCK_ID
))
3691 // Unexpected unresolved metadata when parsing function.
3692 if (MDLoader
->hasFwdRefs())
3693 return error("Invalid function metadata: incoming forward references");
3695 InstructionList
.clear();
3696 unsigned ModuleValueListSize
= ValueList
.size();
3697 unsigned ModuleMDLoaderSize
= MDLoader
->size();
3699 // Add all the function arguments to the value table.
3701 FunctionType
*FullFTy
= FunctionTypes
[F
];
3702 for (Argument
&I
: F
->args()) {
3703 assert(I
.getType() == flattenPointerTypes(FullFTy
->getParamType(ArgNo
)) &&
3704 "Incorrect fully specified type for Function Argument");
3705 ValueList
.push_back(&I
, FullFTy
->getParamType(ArgNo
++));
3707 unsigned NextValueNo
= ValueList
.size();
3708 BasicBlock
*CurBB
= nullptr;
3709 unsigned CurBBNo
= 0;
3712 auto getLastInstruction
= [&]() -> Instruction
* {
3713 if (CurBB
&& !CurBB
->empty())
3714 return &CurBB
->back();
3715 else if (CurBBNo
&& FunctionBBs
[CurBBNo
- 1] &&
3716 !FunctionBBs
[CurBBNo
- 1]->empty())
3717 return &FunctionBBs
[CurBBNo
- 1]->back();
3721 std::vector
<OperandBundleDef
> OperandBundles
;
3723 // Read all the records.
3724 SmallVector
<uint64_t, 64> Record
;
3727 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3729 return MaybeEntry
.takeError();
3730 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3732 switch (Entry
.Kind
) {
3733 case BitstreamEntry::Error
:
3734 return error("Malformed block");
3735 case BitstreamEntry::EndBlock
:
3736 goto OutOfRecordLoop
;
3738 case BitstreamEntry::SubBlock
:
3740 default: // Skip unknown content.
3741 if (Error Err
= Stream
.SkipBlock())
3744 case bitc::CONSTANTS_BLOCK_ID
:
3745 if (Error Err
= parseConstants())
3747 NextValueNo
= ValueList
.size();
3749 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3750 if (Error Err
= parseValueSymbolTable())
3753 case bitc::METADATA_ATTACHMENT_ID
:
3754 if (Error Err
= MDLoader
->parseMetadataAttachment(*F
, InstructionList
))
3757 case bitc::METADATA_BLOCK_ID
:
3758 assert(DeferredMetadataInfo
.empty() &&
3759 "Must read all module-level metadata before function-level");
3760 if (Error Err
= MDLoader
->parseFunctionMetadata())
3763 case bitc::USELIST_BLOCK_ID
:
3764 if (Error Err
= parseUseLists())
3770 case BitstreamEntry::Record
:
3771 // The interesting case.
3777 Instruction
*I
= nullptr;
3778 Type
*FullTy
= nullptr;
3779 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3781 return MaybeBitCode
.takeError();
3782 switch (unsigned BitCode
= MaybeBitCode
.get()) {
3783 default: // Default behavior: reject
3784 return error("Invalid value");
3785 case bitc::FUNC_CODE_DECLAREBLOCKS
: { // DECLAREBLOCKS: [nblocks]
3786 if (Record
.size() < 1 || Record
[0] == 0)
3787 return error("Invalid record");
3788 // Create all the basic blocks for the function.
3789 FunctionBBs
.resize(Record
[0]);
3791 // See if anything took the address of blocks in this function.
3792 auto BBFRI
= BasicBlockFwdRefs
.find(F
);
3793 if (BBFRI
== BasicBlockFwdRefs
.end()) {
3794 for (unsigned i
= 0, e
= FunctionBBs
.size(); i
!= e
; ++i
)
3795 FunctionBBs
[i
] = BasicBlock::Create(Context
, "", F
);
3797 auto &BBRefs
= BBFRI
->second
;
3798 // Check for invalid basic block references.
3799 if (BBRefs
.size() > FunctionBBs
.size())
3800 return error("Invalid ID");
3801 assert(!BBRefs
.empty() && "Unexpected empty array");
3802 assert(!BBRefs
.front() && "Invalid reference to entry block");
3803 for (unsigned I
= 0, E
= FunctionBBs
.size(), RE
= BBRefs
.size(); I
!= E
;
3805 if (I
< RE
&& BBRefs
[I
]) {
3806 BBRefs
[I
]->insertInto(F
);
3807 FunctionBBs
[I
] = BBRefs
[I
];
3809 FunctionBBs
[I
] = BasicBlock::Create(Context
, "", F
);
3812 // Erase from the table.
3813 BasicBlockFwdRefs
.erase(BBFRI
);
3816 CurBB
= FunctionBBs
[0];
3820 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN
: // DEBUG_LOC_AGAIN
3821 // This record indicates that the last instruction is at the same
3822 // location as the previous instruction with a location.
3823 I
= getLastInstruction();
3826 return error("Invalid record");
3827 I
->setDebugLoc(LastLoc
);
3831 case bitc::FUNC_CODE_DEBUG_LOC
: { // DEBUG_LOC: [line, col, scope, ia]
3832 I
= getLastInstruction();
3833 if (!I
|| Record
.size() < 4)
3834 return error("Invalid record");
3836 unsigned Line
= Record
[0], Col
= Record
[1];
3837 unsigned ScopeID
= Record
[2], IAID
= Record
[3];
3838 bool isImplicitCode
= Record
.size() == 5 && Record
[4];
3840 MDNode
*Scope
= nullptr, *IA
= nullptr;
3842 Scope
= dyn_cast_or_null
<MDNode
>(
3843 MDLoader
->getMetadataFwdRefOrLoad(ScopeID
- 1));
3845 return error("Invalid record");
3848 IA
= dyn_cast_or_null
<MDNode
>(
3849 MDLoader
->getMetadataFwdRefOrLoad(IAID
- 1));
3851 return error("Invalid record");
3853 LastLoc
= DebugLoc::get(Line
, Col
, Scope
, IA
, isImplicitCode
);
3854 I
->setDebugLoc(LastLoc
);
3858 case bitc::FUNC_CODE_INST_UNOP
: { // UNOP: [opval, ty, opcode]
3861 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
3862 OpNum
+1 > Record
.size())
3863 return error("Invalid record");
3865 int Opc
= getDecodedUnaryOpcode(Record
[OpNum
++], LHS
->getType());
3867 return error("Invalid record");
3868 I
= UnaryOperator::Create((Instruction::UnaryOps
)Opc
, LHS
);
3869 InstructionList
.push_back(I
);
3870 if (OpNum
< Record
.size()) {
3871 if (isa
<FPMathOperator
>(I
)) {
3872 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
3874 I
->setFastMathFlags(FMF
);
3879 case bitc::FUNC_CODE_INST_BINOP
: { // BINOP: [opval, ty, opval, opcode]
3882 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
3883 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
) ||
3884 OpNum
+1 > Record
.size())
3885 return error("Invalid record");
3887 int Opc
= getDecodedBinaryOpcode(Record
[OpNum
++], LHS
->getType());
3889 return error("Invalid record");
3890 I
= BinaryOperator::Create((Instruction::BinaryOps
)Opc
, LHS
, RHS
);
3891 InstructionList
.push_back(I
);
3892 if (OpNum
< Record
.size()) {
3893 if (Opc
== Instruction::Add
||
3894 Opc
== Instruction::Sub
||
3895 Opc
== Instruction::Mul
||
3896 Opc
== Instruction::Shl
) {
3897 if (Record
[OpNum
] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
3898 cast
<BinaryOperator
>(I
)->setHasNoSignedWrap(true);
3899 if (Record
[OpNum
] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
3900 cast
<BinaryOperator
>(I
)->setHasNoUnsignedWrap(true);
3901 } else if (Opc
== Instruction::SDiv
||
3902 Opc
== Instruction::UDiv
||
3903 Opc
== Instruction::LShr
||
3904 Opc
== Instruction::AShr
) {
3905 if (Record
[OpNum
] & (1 << bitc::PEO_EXACT
))
3906 cast
<BinaryOperator
>(I
)->setIsExact(true);
3907 } else if (isa
<FPMathOperator
>(I
)) {
3908 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
3910 I
->setFastMathFlags(FMF
);
3916 case bitc::FUNC_CODE_INST_CAST
: { // CAST: [opval, opty, destty, castopc]
3919 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
3920 OpNum
+2 != Record
.size())
3921 return error("Invalid record");
3923 FullTy
= getFullyStructuredTypeByID(Record
[OpNum
]);
3924 Type
*ResTy
= flattenPointerTypes(FullTy
);
3925 int Opc
= getDecodedCastOpcode(Record
[OpNum
+ 1]);
3926 if (Opc
== -1 || !ResTy
)
3927 return error("Invalid record");
3928 Instruction
*Temp
= nullptr;
3929 if ((I
= UpgradeBitCastInst(Opc
, Op
, ResTy
, Temp
))) {
3931 InstructionList
.push_back(Temp
);
3932 CurBB
->getInstList().push_back(Temp
);
3935 auto CastOp
= (Instruction::CastOps
)Opc
;
3936 if (!CastInst::castIsValid(CastOp
, Op
, ResTy
))
3937 return error("Invalid cast");
3938 I
= CastInst::Create(CastOp
, Op
, ResTy
);
3940 InstructionList
.push_back(I
);
3943 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
:
3944 case bitc::FUNC_CODE_INST_GEP_OLD
:
3945 case bitc::FUNC_CODE_INST_GEP
: { // GEP: type, [n x operands]
3951 if (BitCode
== bitc::FUNC_CODE_INST_GEP
) {
3952 InBounds
= Record
[OpNum
++];
3953 FullTy
= getFullyStructuredTypeByID(Record
[OpNum
++]);
3954 Ty
= flattenPointerTypes(FullTy
);
3956 InBounds
= BitCode
== bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
;
3961 Type
*FullBaseTy
= nullptr;
3962 if (getValueTypePair(Record
, OpNum
, NextValueNo
, BasePtr
, &FullBaseTy
))
3963 return error("Invalid record");
3966 std::tie(FullTy
, Ty
) =
3967 getPointerElementTypes(FullBaseTy
->getScalarType());
3968 } else if (Ty
!= getPointerElementFlatType(FullBaseTy
->getScalarType()))
3970 "Explicit gep type does not match pointee type of pointer operand");
3972 SmallVector
<Value
*, 16> GEPIdx
;
3973 while (OpNum
!= Record
.size()) {
3975 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
3976 return error("Invalid record");
3977 GEPIdx
.push_back(Op
);
3980 I
= GetElementPtrInst::Create(Ty
, BasePtr
, GEPIdx
);
3981 FullTy
= GetElementPtrInst::getGEPReturnType(FullTy
, I
, GEPIdx
);
3983 InstructionList
.push_back(I
);
3985 cast
<GetElementPtrInst
>(I
)->setIsInBounds(true);
3989 case bitc::FUNC_CODE_INST_EXTRACTVAL
: {
3990 // EXTRACTVAL: [opty, opval, n x indices]
3993 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
, &FullTy
))
3994 return error("Invalid record");
3996 unsigned RecSize
= Record
.size();
3997 if (OpNum
== RecSize
)
3998 return error("EXTRACTVAL: Invalid instruction with 0 indices");
4000 SmallVector
<unsigned, 4> EXTRACTVALIdx
;
4001 for (; OpNum
!= RecSize
; ++OpNum
) {
4002 bool IsArray
= FullTy
->isArrayTy();
4003 bool IsStruct
= FullTy
->isStructTy();
4004 uint64_t Index
= Record
[OpNum
];
4006 if (!IsStruct
&& !IsArray
)
4007 return error("EXTRACTVAL: Invalid type");
4008 if ((unsigned)Index
!= Index
)
4009 return error("Invalid value");
4010 if (IsStruct
&& Index
>= FullTy
->getStructNumElements())
4011 return error("EXTRACTVAL: Invalid struct index");
4012 if (IsArray
&& Index
>= FullTy
->getArrayNumElements())
4013 return error("EXTRACTVAL: Invalid array index");
4014 EXTRACTVALIdx
.push_back((unsigned)Index
);
4017 FullTy
= FullTy
->getStructElementType(Index
);
4019 FullTy
= FullTy
->getArrayElementType();
4022 I
= ExtractValueInst::Create(Agg
, EXTRACTVALIdx
);
4023 InstructionList
.push_back(I
);
4027 case bitc::FUNC_CODE_INST_INSERTVAL
: {
4028 // INSERTVAL: [opty, opval, opty, opval, n x indices]
4031 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
, &FullTy
))
4032 return error("Invalid record");
4034 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Val
))
4035 return error("Invalid record");
4037 unsigned RecSize
= Record
.size();
4038 if (OpNum
== RecSize
)
4039 return error("INSERTVAL: Invalid instruction with 0 indices");
4041 SmallVector
<unsigned, 4> INSERTVALIdx
;
4042 Type
*CurTy
= Agg
->getType();
4043 for (; OpNum
!= RecSize
; ++OpNum
) {
4044 bool IsArray
= CurTy
->isArrayTy();
4045 bool IsStruct
= CurTy
->isStructTy();
4046 uint64_t Index
= Record
[OpNum
];
4048 if (!IsStruct
&& !IsArray
)
4049 return error("INSERTVAL: Invalid type");
4050 if ((unsigned)Index
!= Index
)
4051 return error("Invalid value");
4052 if (IsStruct
&& Index
>= CurTy
->getStructNumElements())
4053 return error("INSERTVAL: Invalid struct index");
4054 if (IsArray
&& Index
>= CurTy
->getArrayNumElements())
4055 return error("INSERTVAL: Invalid array index");
4057 INSERTVALIdx
.push_back((unsigned)Index
);
4059 CurTy
= CurTy
->getStructElementType(Index
);
4061 CurTy
= CurTy
->getArrayElementType();
4064 if (CurTy
!= Val
->getType())
4065 return error("Inserted value type doesn't match aggregate type");
4067 I
= InsertValueInst::Create(Agg
, Val
, INSERTVALIdx
);
4068 InstructionList
.push_back(I
);
4072 case bitc::FUNC_CODE_INST_SELECT
: { // SELECT: [opval, ty, opval, opval]
4073 // obsolete form of select
4074 // handles select i1 ... in old bitcode
4076 Value
*TrueVal
, *FalseVal
, *Cond
;
4077 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
, &FullTy
) ||
4078 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
4079 popValue(Record
, OpNum
, NextValueNo
, Type::getInt1Ty(Context
), Cond
))
4080 return error("Invalid record");
4082 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
4083 InstructionList
.push_back(I
);
4087 case bitc::FUNC_CODE_INST_VSELECT
: {// VSELECT: [ty,opval,opval,predty,pred]
4088 // new form of select
4089 // handles select i1 or select [N x i1]
4091 Value
*TrueVal
, *FalseVal
, *Cond
;
4092 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
, &FullTy
) ||
4093 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
4094 getValueTypePair(Record
, OpNum
, NextValueNo
, Cond
))
4095 return error("Invalid record");
4097 // select condition can be either i1 or [N x i1]
4098 if (VectorType
* vector_type
=
4099 dyn_cast
<VectorType
>(Cond
->getType())) {
4101 if (vector_type
->getElementType() != Type::getInt1Ty(Context
))
4102 return error("Invalid type for value");
4105 if (Cond
->getType() != Type::getInt1Ty(Context
))
4106 return error("Invalid type for value");
4109 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
4110 InstructionList
.push_back(I
);
4111 if (OpNum
< Record
.size() && isa
<FPMathOperator
>(I
)) {
4112 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
4114 I
->setFastMathFlags(FMF
);
4119 case bitc::FUNC_CODE_INST_EXTRACTELT
: { // EXTRACTELT: [opty, opval, opval]
4122 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
, &FullTy
) ||
4123 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
4124 return error("Invalid record");
4125 if (!Vec
->getType()->isVectorTy())
4126 return error("Invalid type for value");
4127 I
= ExtractElementInst::Create(Vec
, Idx
);
4128 FullTy
= FullTy
->getVectorElementType();
4129 InstructionList
.push_back(I
);
4133 case bitc::FUNC_CODE_INST_INSERTELT
: { // INSERTELT: [ty, opval,opval,opval]
4135 Value
*Vec
, *Elt
, *Idx
;
4136 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
, &FullTy
))
4137 return error("Invalid record");
4138 if (!Vec
->getType()->isVectorTy())
4139 return error("Invalid type for value");
4140 if (popValue(Record
, OpNum
, NextValueNo
,
4141 cast
<VectorType
>(Vec
->getType())->getElementType(), Elt
) ||
4142 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
4143 return error("Invalid record");
4144 I
= InsertElementInst::Create(Vec
, Elt
, Idx
);
4145 InstructionList
.push_back(I
);
4149 case bitc::FUNC_CODE_INST_SHUFFLEVEC
: {// SHUFFLEVEC: [opval,ty,opval,opval]
4151 Value
*Vec1
, *Vec2
, *Mask
;
4152 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec1
, &FullTy
) ||
4153 popValue(Record
, OpNum
, NextValueNo
, Vec1
->getType(), Vec2
))
4154 return error("Invalid record");
4156 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Mask
))
4157 return error("Invalid record");
4158 if (!Vec1
->getType()->isVectorTy() || !Vec2
->getType()->isVectorTy())
4159 return error("Invalid type for value");
4160 I
= new ShuffleVectorInst(Vec1
, Vec2
, Mask
);
4161 FullTy
= VectorType::get(FullTy
->getVectorElementType(),
4162 Mask
->getType()->getVectorNumElements());
4163 InstructionList
.push_back(I
);
4167 case bitc::FUNC_CODE_INST_CMP
: // CMP: [opty, opval, opval, pred]
4168 // Old form of ICmp/FCmp returning bool
4169 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4170 // both legal on vectors but had different behaviour.
4171 case bitc::FUNC_CODE_INST_CMP2
: { // CMP2: [opty, opval, opval, pred]
4172 // FCmp/ICmp returning bool or vector of bool
4176 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
4177 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
))
4178 return error("Invalid record");
4180 if (OpNum
>= Record
.size())
4182 "Invalid record: operand number exceeded available operands");
4184 unsigned PredVal
= Record
[OpNum
];
4185 bool IsFP
= LHS
->getType()->isFPOrFPVectorTy();
4187 if (IsFP
&& Record
.size() > OpNum
+1)
4188 FMF
= getDecodedFastMathFlags(Record
[++OpNum
]);
4190 if (OpNum
+1 != Record
.size())
4191 return error("Invalid record");
4193 if (LHS
->getType()->isFPOrFPVectorTy())
4194 I
= new FCmpInst((FCmpInst::Predicate
)PredVal
, LHS
, RHS
);
4196 I
= new ICmpInst((ICmpInst::Predicate
)PredVal
, LHS
, RHS
);
4199 I
->setFastMathFlags(FMF
);
4200 InstructionList
.push_back(I
);
4204 case bitc::FUNC_CODE_INST_RET
: // RET: [opty,opval<optional>]
4206 unsigned Size
= Record
.size();
4208 I
= ReturnInst::Create(Context
);
4209 InstructionList
.push_back(I
);
4214 Value
*Op
= nullptr;
4215 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4216 return error("Invalid record");
4217 if (OpNum
!= Record
.size())
4218 return error("Invalid record");
4220 I
= ReturnInst::Create(Context
, Op
);
4221 InstructionList
.push_back(I
);
4224 case bitc::FUNC_CODE_INST_BR
: { // BR: [bb#, bb#, opval] or [bb#]
4225 if (Record
.size() != 1 && Record
.size() != 3)
4226 return error("Invalid record");
4227 BasicBlock
*TrueDest
= getBasicBlock(Record
[0]);
4229 return error("Invalid record");
4231 if (Record
.size() == 1) {
4232 I
= BranchInst::Create(TrueDest
);
4233 InstructionList
.push_back(I
);
4236 BasicBlock
*FalseDest
= getBasicBlock(Record
[1]);
4237 Value
*Cond
= getValue(Record
, 2, NextValueNo
,
4238 Type::getInt1Ty(Context
));
4239 if (!FalseDest
|| !Cond
)
4240 return error("Invalid record");
4241 I
= BranchInst::Create(TrueDest
, FalseDest
, Cond
);
4242 InstructionList
.push_back(I
);
4246 case bitc::FUNC_CODE_INST_CLEANUPRET
: { // CLEANUPRET: [val] or [val,bb#]
4247 if (Record
.size() != 1 && Record
.size() != 2)
4248 return error("Invalid record");
4251 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4253 return error("Invalid record");
4254 BasicBlock
*UnwindDest
= nullptr;
4255 if (Record
.size() == 2) {
4256 UnwindDest
= getBasicBlock(Record
[Idx
++]);
4258 return error("Invalid record");
4261 I
= CleanupReturnInst::Create(CleanupPad
, UnwindDest
);
4262 InstructionList
.push_back(I
);
4265 case bitc::FUNC_CODE_INST_CATCHRET
: { // CATCHRET: [val,bb#]
4266 if (Record
.size() != 2)
4267 return error("Invalid record");
4270 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4272 return error("Invalid record");
4273 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
4275 return error("Invalid record");
4277 I
= CatchReturnInst::Create(CatchPad
, BB
);
4278 InstructionList
.push_back(I
);
4281 case bitc::FUNC_CODE_INST_CATCHSWITCH
: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4282 // We must have, at minimum, the outer scope and the number of arguments.
4283 if (Record
.size() < 2)
4284 return error("Invalid record");
4289 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4291 unsigned NumHandlers
= Record
[Idx
++];
4293 SmallVector
<BasicBlock
*, 2> Handlers
;
4294 for (unsigned Op
= 0; Op
!= NumHandlers
; ++Op
) {
4295 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
4297 return error("Invalid record");
4298 Handlers
.push_back(BB
);
4301 BasicBlock
*UnwindDest
= nullptr;
4302 if (Idx
+ 1 == Record
.size()) {
4303 UnwindDest
= getBasicBlock(Record
[Idx
++]);
4305 return error("Invalid record");
4308 if (Record
.size() != Idx
)
4309 return error("Invalid record");
4312 CatchSwitchInst::Create(ParentPad
, UnwindDest
, NumHandlers
);
4313 for (BasicBlock
*Handler
: Handlers
)
4314 CatchSwitch
->addHandler(Handler
);
4316 InstructionList
.push_back(I
);
4319 case bitc::FUNC_CODE_INST_CATCHPAD
:
4320 case bitc::FUNC_CODE_INST_CLEANUPPAD
: { // [tok,num,(ty,val)*]
4321 // We must have, at minimum, the outer scope and the number of arguments.
4322 if (Record
.size() < 2)
4323 return error("Invalid record");
4328 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4330 unsigned NumArgOperands
= Record
[Idx
++];
4332 SmallVector
<Value
*, 2> Args
;
4333 for (unsigned Op
= 0; Op
!= NumArgOperands
; ++Op
) {
4335 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
4336 return error("Invalid record");
4337 Args
.push_back(Val
);
4340 if (Record
.size() != Idx
)
4341 return error("Invalid record");
4343 if (BitCode
== bitc::FUNC_CODE_INST_CLEANUPPAD
)
4344 I
= CleanupPadInst::Create(ParentPad
, Args
);
4346 I
= CatchPadInst::Create(ParentPad
, Args
);
4347 InstructionList
.push_back(I
);
4350 case bitc::FUNC_CODE_INST_SWITCH
: { // SWITCH: [opty, op0, op1, ...]
4352 if ((Record
[0] >> 16) == SWITCH_INST_MAGIC
) {
4353 // "New" SwitchInst format with case ranges. The changes to write this
4354 // format were reverted but we still recognize bitcode that uses it.
4355 // Hopefully someday we will have support for case ranges and can use
4356 // this format again.
4358 Type
*OpTy
= getTypeByID(Record
[1]);
4359 unsigned ValueBitWidth
= cast
<IntegerType
>(OpTy
)->getBitWidth();
4361 Value
*Cond
= getValue(Record
, 2, NextValueNo
, OpTy
);
4362 BasicBlock
*Default
= getBasicBlock(Record
[3]);
4363 if (!OpTy
|| !Cond
|| !Default
)
4364 return error("Invalid record");
4366 unsigned NumCases
= Record
[4];
4368 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4369 InstructionList
.push_back(SI
);
4371 unsigned CurIdx
= 5;
4372 for (unsigned i
= 0; i
!= NumCases
; ++i
) {
4373 SmallVector
<ConstantInt
*, 1> CaseVals
;
4374 unsigned NumItems
= Record
[CurIdx
++];
4375 for (unsigned ci
= 0; ci
!= NumItems
; ++ci
) {
4376 bool isSingleNumber
= Record
[CurIdx
++];
4379 unsigned ActiveWords
= 1;
4380 if (ValueBitWidth
> 64)
4381 ActiveWords
= Record
[CurIdx
++];
4382 Low
= readWideAPInt(makeArrayRef(&Record
[CurIdx
], ActiveWords
),
4384 CurIdx
+= ActiveWords
;
4386 if (!isSingleNumber
) {
4388 if (ValueBitWidth
> 64)
4389 ActiveWords
= Record
[CurIdx
++];
4390 APInt High
= readWideAPInt(
4391 makeArrayRef(&Record
[CurIdx
], ActiveWords
), ValueBitWidth
);
4392 CurIdx
+= ActiveWords
;
4394 // FIXME: It is not clear whether values in the range should be
4395 // compared as signed or unsigned values. The partially
4396 // implemented changes that used this format in the past used
4397 // unsigned comparisons.
4398 for ( ; Low
.ule(High
); ++Low
)
4399 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4401 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4403 BasicBlock
*DestBB
= getBasicBlock(Record
[CurIdx
++]);
4404 for (SmallVector
<ConstantInt
*, 1>::iterator cvi
= CaseVals
.begin(),
4405 cve
= CaseVals
.end(); cvi
!= cve
; ++cvi
)
4406 SI
->addCase(*cvi
, DestBB
);
4412 // Old SwitchInst format without case ranges.
4414 if (Record
.size() < 3 || (Record
.size() & 1) == 0)
4415 return error("Invalid record");
4416 Type
*OpTy
= getTypeByID(Record
[0]);
4417 Value
*Cond
= getValue(Record
, 1, NextValueNo
, OpTy
);
4418 BasicBlock
*Default
= getBasicBlock(Record
[2]);
4419 if (!OpTy
|| !Cond
|| !Default
)
4420 return error("Invalid record");
4421 unsigned NumCases
= (Record
.size()-3)/2;
4422 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4423 InstructionList
.push_back(SI
);
4424 for (unsigned i
= 0, e
= NumCases
; i
!= e
; ++i
) {
4425 ConstantInt
*CaseVal
=
4426 dyn_cast_or_null
<ConstantInt
>(getFnValueByID(Record
[3+i
*2], OpTy
));
4427 BasicBlock
*DestBB
= getBasicBlock(Record
[1+3+i
*2]);
4428 if (!CaseVal
|| !DestBB
) {
4430 return error("Invalid record");
4432 SI
->addCase(CaseVal
, DestBB
);
4437 case bitc::FUNC_CODE_INST_INDIRECTBR
: { // INDIRECTBR: [opty, op0, op1, ...]
4438 if (Record
.size() < 2)
4439 return error("Invalid record");
4440 Type
*OpTy
= getTypeByID(Record
[0]);
4441 Value
*Address
= getValue(Record
, 1, NextValueNo
, OpTy
);
4442 if (!OpTy
|| !Address
)
4443 return error("Invalid record");
4444 unsigned NumDests
= Record
.size()-2;
4445 IndirectBrInst
*IBI
= IndirectBrInst::Create(Address
, NumDests
);
4446 InstructionList
.push_back(IBI
);
4447 for (unsigned i
= 0, e
= NumDests
; i
!= e
; ++i
) {
4448 if (BasicBlock
*DestBB
= getBasicBlock(Record
[2+i
])) {
4449 IBI
->addDestination(DestBB
);
4452 return error("Invalid record");
4459 case bitc::FUNC_CODE_INST_INVOKE
: {
4460 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4461 if (Record
.size() < 4)
4462 return error("Invalid record");
4464 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4465 unsigned CCInfo
= Record
[OpNum
++];
4466 BasicBlock
*NormalBB
= getBasicBlock(Record
[OpNum
++]);
4467 BasicBlock
*UnwindBB
= getBasicBlock(Record
[OpNum
++]);
4469 FunctionType
*FTy
= nullptr;
4470 FunctionType
*FullFTy
= nullptr;
4471 if ((CCInfo
>> 13) & 1) {
4473 dyn_cast
<FunctionType
>(getFullyStructuredTypeByID(Record
[OpNum
++]));
4475 return error("Explicit invoke type is not a function type");
4476 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
4480 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
, &FullTy
))
4481 return error("Invalid record");
4483 PointerType
*CalleeTy
= dyn_cast
<PointerType
>(Callee
->getType());
4485 return error("Callee is not a pointer");
4488 dyn_cast
<FunctionType
>(cast
<PointerType
>(FullTy
)->getElementType());
4490 return error("Callee is not of pointer to function type");
4491 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
4492 } else if (getPointerElementFlatType(FullTy
) != FTy
)
4493 return error("Explicit invoke type does not match pointee type of "
4495 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4496 return error("Insufficient operands to call");
4498 SmallVector
<Value
*, 16> Ops
;
4499 SmallVector
<Type
*, 16> ArgsFullTys
;
4500 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4501 Ops
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4502 FTy
->getParamType(i
)));
4503 ArgsFullTys
.push_back(FullFTy
->getParamType(i
));
4505 return error("Invalid record");
4508 if (!FTy
->isVarArg()) {
4509 if (Record
.size() != OpNum
)
4510 return error("Invalid record");
4512 // Read type/value pairs for varargs params.
4513 while (OpNum
!= Record
.size()) {
4516 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
, &FullTy
))
4517 return error("Invalid record");
4519 ArgsFullTys
.push_back(FullTy
);
4523 I
= InvokeInst::Create(FTy
, Callee
, NormalBB
, UnwindBB
, Ops
,
4525 FullTy
= FullFTy
->getReturnType();
4526 OperandBundles
.clear();
4527 InstructionList
.push_back(I
);
4528 cast
<InvokeInst
>(I
)->setCallingConv(
4529 static_cast<CallingConv::ID
>(CallingConv::MaxID
& CCInfo
));
4530 cast
<InvokeInst
>(I
)->setAttributes(PAL
);
4531 propagateByValTypes(cast
<CallBase
>(I
), ArgsFullTys
);
4535 case bitc::FUNC_CODE_INST_RESUME
: { // RESUME: [opval]
4537 Value
*Val
= nullptr;
4538 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
4539 return error("Invalid record");
4540 I
= ResumeInst::Create(Val
);
4541 InstructionList
.push_back(I
);
4544 case bitc::FUNC_CODE_INST_CALLBR
: {
4545 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]
4547 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4548 unsigned CCInfo
= Record
[OpNum
++];
4550 BasicBlock
*DefaultDest
= getBasicBlock(Record
[OpNum
++]);
4551 unsigned NumIndirectDests
= Record
[OpNum
++];
4552 SmallVector
<BasicBlock
*, 16> IndirectDests
;
4553 for (unsigned i
= 0, e
= NumIndirectDests
; i
!= e
; ++i
)
4554 IndirectDests
.push_back(getBasicBlock(Record
[OpNum
++]));
4556 FunctionType
*FTy
= nullptr;
4557 FunctionType
*FullFTy
= nullptr;
4558 if ((CCInfo
>> bitc::CALL_EXPLICIT_TYPE
) & 1) {
4560 dyn_cast
<FunctionType
>(getFullyStructuredTypeByID(Record
[OpNum
++]));
4562 return error("Explicit call type is not a function type");
4563 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
4567 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
, &FullTy
))
4568 return error("Invalid record");
4570 PointerType
*OpTy
= dyn_cast
<PointerType
>(Callee
->getType());
4572 return error("Callee is not a pointer type");
4575 dyn_cast
<FunctionType
>(cast
<PointerType
>(FullTy
)->getElementType());
4577 return error("Callee is not of pointer to function type");
4578 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
4579 } else if (getPointerElementFlatType(FullTy
) != FTy
)
4580 return error("Explicit call type does not match pointee type of "
4582 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4583 return error("Insufficient operands to call");
4585 SmallVector
<Value
*, 16> Args
;
4586 // Read the fixed params.
4587 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4588 if (FTy
->getParamType(i
)->isLabelTy())
4589 Args
.push_back(getBasicBlock(Record
[OpNum
]));
4591 Args
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4592 FTy
->getParamType(i
)));
4594 return error("Invalid record");
4597 // Read type/value pairs for varargs params.
4598 if (!FTy
->isVarArg()) {
4599 if (OpNum
!= Record
.size())
4600 return error("Invalid record");
4602 while (OpNum
!= Record
.size()) {
4604 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4605 return error("Invalid record");
4610 I
= CallBrInst::Create(FTy
, Callee
, DefaultDest
, IndirectDests
, Args
,
4612 FullTy
= FullFTy
->getReturnType();
4613 OperandBundles
.clear();
4614 InstructionList
.push_back(I
);
4615 cast
<CallBrInst
>(I
)->setCallingConv(
4616 static_cast<CallingConv::ID
>((0x7ff & CCInfo
) >> bitc::CALL_CCONV
));
4617 cast
<CallBrInst
>(I
)->setAttributes(PAL
);
4620 case bitc::FUNC_CODE_INST_UNREACHABLE
: // UNREACHABLE
4621 I
= new UnreachableInst(Context
);
4622 InstructionList
.push_back(I
);
4624 case bitc::FUNC_CODE_INST_PHI
: { // PHI: [ty, val0,bb0, ...]
4625 if (Record
.size() < 1 || ((Record
.size()-1)&1))
4626 return error("Invalid record");
4627 FullTy
= getFullyStructuredTypeByID(Record
[0]);
4628 Type
*Ty
= flattenPointerTypes(FullTy
);
4630 return error("Invalid record");
4632 PHINode
*PN
= PHINode::Create(Ty
, (Record
.size()-1)/2);
4633 InstructionList
.push_back(PN
);
4635 for (unsigned i
= 0, e
= Record
.size()-1; i
!= e
; i
+= 2) {
4637 // With the new function encoding, it is possible that operands have
4638 // negative IDs (for forward references). Use a signed VBR
4639 // representation to keep the encoding small.
4641 V
= getValueSigned(Record
, 1+i
, NextValueNo
, Ty
);
4643 V
= getValue(Record
, 1+i
, NextValueNo
, Ty
);
4644 BasicBlock
*BB
= getBasicBlock(Record
[2+i
]);
4646 return error("Invalid record");
4647 PN
->addIncoming(V
, BB
);
4653 case bitc::FUNC_CODE_INST_LANDINGPAD
:
4654 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD
: {
4655 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4657 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD
) {
4658 if (Record
.size() < 3)
4659 return error("Invalid record");
4661 assert(BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
);
4662 if (Record
.size() < 4)
4663 return error("Invalid record");
4665 FullTy
= getFullyStructuredTypeByID(Record
[Idx
++]);
4666 Type
*Ty
= flattenPointerTypes(FullTy
);
4668 return error("Invalid record");
4669 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
) {
4670 Value
*PersFn
= nullptr;
4671 if (getValueTypePair(Record
, Idx
, NextValueNo
, PersFn
))
4672 return error("Invalid record");
4674 if (!F
->hasPersonalityFn())
4675 F
->setPersonalityFn(cast
<Constant
>(PersFn
));
4676 else if (F
->getPersonalityFn() != cast
<Constant
>(PersFn
))
4677 return error("Personality function mismatch");
4680 bool IsCleanup
= !!Record
[Idx
++];
4681 unsigned NumClauses
= Record
[Idx
++];
4682 LandingPadInst
*LP
= LandingPadInst::Create(Ty
, NumClauses
);
4683 LP
->setCleanup(IsCleanup
);
4684 for (unsigned J
= 0; J
!= NumClauses
; ++J
) {
4685 LandingPadInst::ClauseType CT
=
4686 LandingPadInst::ClauseType(Record
[Idx
++]); (void)CT
;
4689 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
)) {
4691 return error("Invalid record");
4694 assert((CT
!= LandingPadInst::Catch
||
4695 !isa
<ArrayType
>(Val
->getType())) &&
4696 "Catch clause has a invalid type!");
4697 assert((CT
!= LandingPadInst::Filter
||
4698 isa
<ArrayType
>(Val
->getType())) &&
4699 "Filter clause has invalid type!");
4700 LP
->addClause(cast
<Constant
>(Val
));
4704 InstructionList
.push_back(I
);
4708 case bitc::FUNC_CODE_INST_ALLOCA
: { // ALLOCA: [instty, opty, op, align]
4709 if (Record
.size() != 4)
4710 return error("Invalid record");
4711 uint64_t AlignRecord
= Record
[3];
4712 const uint64_t InAllocaMask
= uint64_t(1) << 5;
4713 const uint64_t ExplicitTypeMask
= uint64_t(1) << 6;
4714 const uint64_t SwiftErrorMask
= uint64_t(1) << 7;
4715 const uint64_t FlagMask
= InAllocaMask
| ExplicitTypeMask
|
4717 bool InAlloca
= AlignRecord
& InAllocaMask
;
4718 bool SwiftError
= AlignRecord
& SwiftErrorMask
;
4719 FullTy
= getFullyStructuredTypeByID(Record
[0]);
4720 Type
*Ty
= flattenPointerTypes(FullTy
);
4721 if ((AlignRecord
& ExplicitTypeMask
) == 0) {
4722 auto *PTy
= dyn_cast_or_null
<PointerType
>(Ty
);
4724 return error("Old-style alloca with a non-pointer type");
4725 std::tie(FullTy
, Ty
) = getPointerElementTypes(FullTy
);
4727 Type
*OpTy
= getTypeByID(Record
[1]);
4728 Value
*Size
= getFnValueByID(Record
[2], OpTy
);
4730 if (Error Err
= parseAlignmentValue(AlignRecord
& ~FlagMask
, Align
)) {
4734 return error("Invalid record");
4736 // FIXME: Make this an optional field.
4737 const DataLayout
&DL
= TheModule
->getDataLayout();
4738 unsigned AS
= DL
.getAllocaAddrSpace();
4740 AllocaInst
*AI
= new AllocaInst(Ty
, AS
, Size
, Align
);
4741 AI
->setUsedWithInAlloca(InAlloca
);
4742 AI
->setSwiftError(SwiftError
);
4744 FullTy
= PointerType::get(FullTy
, AS
);
4745 InstructionList
.push_back(I
);
4748 case bitc::FUNC_CODE_INST_LOAD
: { // LOAD: [opty, op, align, vol]
4751 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
, &FullTy
) ||
4752 (OpNum
+ 2 != Record
.size() && OpNum
+ 3 != Record
.size()))
4753 return error("Invalid record");
4755 if (!isa
<PointerType
>(Op
->getType()))
4756 return error("Load operand is not a pointer type");
4759 if (OpNum
+ 3 == Record
.size()) {
4760 FullTy
= getFullyStructuredTypeByID(Record
[OpNum
++]);
4761 Ty
= flattenPointerTypes(FullTy
);
4763 std::tie(FullTy
, Ty
) = getPointerElementTypes(FullTy
);
4765 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4769 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4771 I
= new LoadInst(Ty
, Op
, "", Record
[OpNum
+ 1], Align
);
4772 InstructionList
.push_back(I
);
4775 case bitc::FUNC_CODE_INST_LOADATOMIC
: {
4776 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4779 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
, &FullTy
) ||
4780 (OpNum
+ 4 != Record
.size() && OpNum
+ 5 != Record
.size()))
4781 return error("Invalid record");
4783 if (!isa
<PointerType
>(Op
->getType()))
4784 return error("Load operand is not a pointer type");
4787 if (OpNum
+ 5 == Record
.size()) {
4788 FullTy
= getFullyStructuredTypeByID(Record
[OpNum
++]);
4789 Ty
= flattenPointerTypes(FullTy
);
4791 std::tie(FullTy
, Ty
) = getPointerElementTypes(FullTy
);
4793 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4796 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4797 if (Ordering
== AtomicOrdering::NotAtomic
||
4798 Ordering
== AtomicOrdering::Release
||
4799 Ordering
== AtomicOrdering::AcquireRelease
)
4800 return error("Invalid record");
4801 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
4802 return error("Invalid record");
4803 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4806 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4808 I
= new LoadInst(Ty
, Op
, "", Record
[OpNum
+ 1], Align
, Ordering
, SSID
);
4809 InstructionList
.push_back(I
);
4812 case bitc::FUNC_CODE_INST_STORE
:
4813 case bitc::FUNC_CODE_INST_STORE_OLD
: { // STORE2:[ptrty, ptr, val, align, vol]
4817 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
, &FullTy
) ||
4818 (BitCode
== bitc::FUNC_CODE_INST_STORE
4819 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
4820 : popValue(Record
, OpNum
, NextValueNo
,
4821 getPointerElementFlatType(FullTy
), Val
)) ||
4822 OpNum
+ 2 != Record
.size())
4823 return error("Invalid record");
4825 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
4828 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4830 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+1], Align
);
4831 InstructionList
.push_back(I
);
4834 case bitc::FUNC_CODE_INST_STOREATOMIC
:
4835 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD
: {
4836 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
4840 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
, &FullTy
) ||
4841 !isa
<PointerType
>(Ptr
->getType()) ||
4842 (BitCode
== bitc::FUNC_CODE_INST_STOREATOMIC
4843 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
4844 : popValue(Record
, OpNum
, NextValueNo
,
4845 getPointerElementFlatType(FullTy
), Val
)) ||
4846 OpNum
+ 4 != Record
.size())
4847 return error("Invalid record");
4849 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
4851 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4852 if (Ordering
== AtomicOrdering::NotAtomic
||
4853 Ordering
== AtomicOrdering::Acquire
||
4854 Ordering
== AtomicOrdering::AcquireRelease
)
4855 return error("Invalid record");
4856 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4857 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
4858 return error("Invalid record");
4861 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4863 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+1], Align
, Ordering
, SSID
);
4864 InstructionList
.push_back(I
);
4867 case bitc::FUNC_CODE_INST_CMPXCHG_OLD
:
4868 case bitc::FUNC_CODE_INST_CMPXCHG
: {
4869 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, ssid,
4870 // failureordering?, isweak?]
4872 Value
*Ptr
, *Cmp
, *New
;
4873 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
, &FullTy
))
4874 return error("Invalid record");
4876 if (!isa
<PointerType
>(Ptr
->getType()))
4877 return error("Cmpxchg operand is not a pointer type");
4879 if (BitCode
== bitc::FUNC_CODE_INST_CMPXCHG
) {
4880 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Cmp
, &FullTy
))
4881 return error("Invalid record");
4882 } else if (popValue(Record
, OpNum
, NextValueNo
,
4883 getPointerElementFlatType(FullTy
), Cmp
))
4884 return error("Invalid record");
4886 FullTy
= cast
<PointerType
>(FullTy
)->getElementType();
4888 if (popValue(Record
, OpNum
, NextValueNo
, Cmp
->getType(), New
) ||
4889 Record
.size() < OpNum
+ 3 || Record
.size() > OpNum
+ 5)
4890 return error("Invalid record");
4892 AtomicOrdering SuccessOrdering
= getDecodedOrdering(Record
[OpNum
+ 1]);
4893 if (SuccessOrdering
== AtomicOrdering::NotAtomic
||
4894 SuccessOrdering
== AtomicOrdering::Unordered
)
4895 return error("Invalid record");
4896 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 2]);
4898 if (Error Err
= typeCheckLoadStoreInst(Cmp
->getType(), Ptr
->getType()))
4900 AtomicOrdering FailureOrdering
;
4901 if (Record
.size() < 7)
4903 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering
);
4905 FailureOrdering
= getDecodedOrdering(Record
[OpNum
+ 3]);
4907 I
= new AtomicCmpXchgInst(Ptr
, Cmp
, New
, SuccessOrdering
, FailureOrdering
,
4909 FullTy
= StructType::get(Context
, {FullTy
, Type::getInt1Ty(Context
)});
4910 cast
<AtomicCmpXchgInst
>(I
)->setVolatile(Record
[OpNum
]);
4912 if (Record
.size() < 8) {
4913 // Before weak cmpxchgs existed, the instruction simply returned the
4914 // value loaded from memory, so bitcode files from that era will be
4915 // expecting the first component of a modern cmpxchg.
4916 CurBB
->getInstList().push_back(I
);
4917 I
= ExtractValueInst::Create(I
, 0);
4918 FullTy
= cast
<StructType
>(FullTy
)->getElementType(0);
4920 cast
<AtomicCmpXchgInst
>(I
)->setWeak(Record
[OpNum
+4]);
4923 InstructionList
.push_back(I
);
4926 case bitc::FUNC_CODE_INST_ATOMICRMW
: {
4927 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid]
4930 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
, &FullTy
) ||
4931 !isa
<PointerType
>(Ptr
->getType()) ||
4932 popValue(Record
, OpNum
, NextValueNo
,
4933 getPointerElementFlatType(FullTy
), Val
) ||
4934 OpNum
+ 4 != Record
.size())
4935 return error("Invalid record");
4936 AtomicRMWInst::BinOp Operation
= getDecodedRMWOperation(Record
[OpNum
]);
4937 if (Operation
< AtomicRMWInst::FIRST_BINOP
||
4938 Operation
> AtomicRMWInst::LAST_BINOP
)
4939 return error("Invalid record");
4940 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4941 if (Ordering
== AtomicOrdering::NotAtomic
||
4942 Ordering
== AtomicOrdering::Unordered
)
4943 return error("Invalid record");
4944 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4945 I
= new AtomicRMWInst(Operation
, Ptr
, Val
, Ordering
, SSID
);
4946 FullTy
= getPointerElementFlatType(FullTy
);
4947 cast
<AtomicRMWInst
>(I
)->setVolatile(Record
[OpNum
+1]);
4948 InstructionList
.push_back(I
);
4951 case bitc::FUNC_CODE_INST_FENCE
: { // FENCE:[ordering, ssid]
4952 if (2 != Record
.size())
4953 return error("Invalid record");
4954 AtomicOrdering Ordering
= getDecodedOrdering(Record
[0]);
4955 if (Ordering
== AtomicOrdering::NotAtomic
||
4956 Ordering
== AtomicOrdering::Unordered
||
4957 Ordering
== AtomicOrdering::Monotonic
)
4958 return error("Invalid record");
4959 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[1]);
4960 I
= new FenceInst(Context
, Ordering
, SSID
);
4961 InstructionList
.push_back(I
);
4964 case bitc::FUNC_CODE_INST_CALL
: {
4965 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
4966 if (Record
.size() < 3)
4967 return error("Invalid record");
4970 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4971 unsigned CCInfo
= Record
[OpNum
++];
4974 if ((CCInfo
>> bitc::CALL_FMF
) & 1) {
4975 FMF
= getDecodedFastMathFlags(Record
[OpNum
++]);
4977 return error("Fast math flags indicator set for call with no FMF");
4980 FunctionType
*FTy
= nullptr;
4981 FunctionType
*FullFTy
= nullptr;
4982 if ((CCInfo
>> bitc::CALL_EXPLICIT_TYPE
) & 1) {
4984 dyn_cast
<FunctionType
>(getFullyStructuredTypeByID(Record
[OpNum
++]));
4986 return error("Explicit call type is not a function type");
4987 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
4991 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
, &FullTy
))
4992 return error("Invalid record");
4994 PointerType
*OpTy
= dyn_cast
<PointerType
>(Callee
->getType());
4996 return error("Callee is not a pointer type");
4999 dyn_cast
<FunctionType
>(cast
<PointerType
>(FullTy
)->getElementType());
5001 return error("Callee is not of pointer to function type");
5002 FTy
= cast
<FunctionType
>(flattenPointerTypes(FullFTy
));
5003 } else if (getPointerElementFlatType(FullTy
) != FTy
)
5004 return error("Explicit call type does not match pointee type of "
5006 if (Record
.size() < FTy
->getNumParams() + OpNum
)
5007 return error("Insufficient operands to call");
5009 SmallVector
<Value
*, 16> Args
;
5010 SmallVector
<Type
*, 16> ArgsFullTys
;
5011 // Read the fixed params.
5012 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
5013 if (FTy
->getParamType(i
)->isLabelTy())
5014 Args
.push_back(getBasicBlock(Record
[OpNum
]));
5016 Args
.push_back(getValue(Record
, OpNum
, NextValueNo
,
5017 FTy
->getParamType(i
)));
5018 ArgsFullTys
.push_back(FullFTy
->getParamType(i
));
5020 return error("Invalid record");
5023 // Read type/value pairs for varargs params.
5024 if (!FTy
->isVarArg()) {
5025 if (OpNum
!= Record
.size())
5026 return error("Invalid record");
5028 while (OpNum
!= Record
.size()) {
5031 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
, &FullTy
))
5032 return error("Invalid record");
5034 ArgsFullTys
.push_back(FullTy
);
5038 I
= CallInst::Create(FTy
, Callee
, Args
, OperandBundles
);
5039 FullTy
= FullFTy
->getReturnType();
5040 OperandBundles
.clear();
5041 InstructionList
.push_back(I
);
5042 cast
<CallInst
>(I
)->setCallingConv(
5043 static_cast<CallingConv::ID
>((0x7ff & CCInfo
) >> bitc::CALL_CCONV
));
5044 CallInst::TailCallKind TCK
= CallInst::TCK_None
;
5045 if (CCInfo
& 1 << bitc::CALL_TAIL
)
5046 TCK
= CallInst::TCK_Tail
;
5047 if (CCInfo
& (1 << bitc::CALL_MUSTTAIL
))
5048 TCK
= CallInst::TCK_MustTail
;
5049 if (CCInfo
& (1 << bitc::CALL_NOTAIL
))
5050 TCK
= CallInst::TCK_NoTail
;
5051 cast
<CallInst
>(I
)->setTailCallKind(TCK
);
5052 cast
<CallInst
>(I
)->setAttributes(PAL
);
5053 propagateByValTypes(cast
<CallBase
>(I
), ArgsFullTys
);
5055 if (!isa
<FPMathOperator
>(I
))
5056 return error("Fast-math-flags specified for call without "
5057 "floating-point scalar or vector return type");
5058 I
->setFastMathFlags(FMF
);
5062 case bitc::FUNC_CODE_INST_VAARG
: { // VAARG: [valistty, valist, instty]
5063 if (Record
.size() < 3)
5064 return error("Invalid record");
5065 Type
*OpTy
= getTypeByID(Record
[0]);
5066 Value
*Op
= getValue(Record
, 1, NextValueNo
, OpTy
);
5067 FullTy
= getFullyStructuredTypeByID(Record
[2]);
5068 Type
*ResTy
= flattenPointerTypes(FullTy
);
5069 if (!OpTy
|| !Op
|| !ResTy
)
5070 return error("Invalid record");
5071 I
= new VAArgInst(Op
, ResTy
);
5072 InstructionList
.push_back(I
);
5076 case bitc::FUNC_CODE_OPERAND_BUNDLE
: {
5077 // A call or an invoke can be optionally prefixed with some variable
5078 // number of operand bundle blocks. These blocks are read into
5079 // OperandBundles and consumed at the next call or invoke instruction.
5081 if (Record
.size() < 1 || Record
[0] >= BundleTags
.size())
5082 return error("Invalid record");
5084 std::vector
<Value
*> Inputs
;
5087 while (OpNum
!= Record
.size()) {
5089 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
5090 return error("Invalid record");
5091 Inputs
.push_back(Op
);
5094 OperandBundles
.emplace_back(BundleTags
[Record
[0]], std::move(Inputs
));
5099 // Add instruction to end of current BB. If there is no current BB, reject
5103 return error("Invalid instruction with no BB");
5105 if (!OperandBundles
.empty()) {
5107 return error("Operand bundles found with no consumer");
5109 CurBB
->getInstList().push_back(I
);
5111 // If this was a terminator instruction, move to the next block.
5112 if (I
->isTerminator()) {
5114 CurBB
= CurBBNo
< FunctionBBs
.size() ? FunctionBBs
[CurBBNo
] : nullptr;
5117 // Non-void values get registered in the value table for future use.
5118 if (I
&& !I
->getType()->isVoidTy()) {
5120 FullTy
= I
->getType();
5122 !FullTy
->isPointerTy() && !isa
<StructType
>(FullTy
) &&
5123 !isa
<ArrayType
>(FullTy
) &&
5124 (!isa
<VectorType
>(FullTy
) ||
5125 FullTy
->getVectorElementType()->isFloatingPointTy() ||
5126 FullTy
->getVectorElementType()->isIntegerTy()) &&
5127 "Structured types must be assigned with corresponding non-opaque "
5131 assert(I
->getType() == flattenPointerTypes(FullTy
) &&
5132 "Incorrect fully structured type provided for Instruction");
5133 ValueList
.assignValue(I
, NextValueNo
++, FullTy
);
5139 if (!OperandBundles
.empty())
5140 return error("Operand bundles found with no consumer");
5142 // Check the function list for unresolved values.
5143 if (Argument
*A
= dyn_cast
<Argument
>(ValueList
.back())) {
5144 if (!A
->getParent()) {
5145 // We found at least one unresolved value. Nuke them all to avoid leaks.
5146 for (unsigned i
= ModuleValueListSize
, e
= ValueList
.size(); i
!= e
; ++i
){
5147 if ((A
= dyn_cast_or_null
<Argument
>(ValueList
[i
])) && !A
->getParent()) {
5148 A
->replaceAllUsesWith(UndefValue::get(A
->getType()));
5152 return error("Never resolved value found in function");
5156 // Unexpected unresolved metadata about to be dropped.
5157 if (MDLoader
->hasFwdRefs())
5158 return error("Invalid function metadata: outgoing forward refs");
5160 // Trim the value list down to the size it was before we parsed this function.
5161 ValueList
.shrinkTo(ModuleValueListSize
);
5162 MDLoader
->shrinkTo(ModuleMDLoaderSize
);
5163 std::vector
<BasicBlock
*>().swap(FunctionBBs
);
5164 return Error::success();
5167 /// Find the function body in the bitcode stream
5168 Error
BitcodeReader::findFunctionInStream(
5170 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
) {
5171 while (DeferredFunctionInfoIterator
->second
== 0) {
5172 // This is the fallback handling for the old format bitcode that
5173 // didn't contain the function index in the VST, or when we have
5174 // an anonymous function which would not have a VST entry.
5175 // Assert that we have one of those two cases.
5176 assert(VSTOffset
== 0 || !F
->hasName());
5177 // Parse the next body in the stream and set its position in the
5178 // DeferredFunctionInfo map.
5179 if (Error Err
= rememberAndSkipFunctionBodies())
5182 return Error::success();
5185 SyncScope::ID
BitcodeReader::getDecodedSyncScopeID(unsigned Val
) {
5186 if (Val
== SyncScope::SingleThread
|| Val
== SyncScope::System
)
5187 return SyncScope::ID(Val
);
5188 if (Val
>= SSIDs
.size())
5189 return SyncScope::System
; // Map unknown synchronization scopes to system.
5193 //===----------------------------------------------------------------------===//
5194 // GVMaterializer implementation
5195 //===----------------------------------------------------------------------===//
5197 Error
BitcodeReader::materialize(GlobalValue
*GV
) {
5198 Function
*F
= dyn_cast
<Function
>(GV
);
5199 // If it's not a function or is already material, ignore the request.
5200 if (!F
|| !F
->isMaterializable())
5201 return Error::success();
5203 DenseMap
<Function
*, uint64_t>::iterator DFII
= DeferredFunctionInfo
.find(F
);
5204 assert(DFII
!= DeferredFunctionInfo
.end() && "Deferred function not found!");
5205 // If its position is recorded as 0, its body is somewhere in the stream
5206 // but we haven't seen it yet.
5207 if (DFII
->second
== 0)
5208 if (Error Err
= findFunctionInStream(F
, DFII
))
5211 // Materialize metadata before parsing any function bodies.
5212 if (Error Err
= materializeMetadata())
5215 // Move the bit stream to the saved position of the deferred function body.
5216 if (Error JumpFailed
= Stream
.JumpToBit(DFII
->second
))
5218 if (Error Err
= parseFunctionBody(F
))
5220 F
->setIsMaterializable(false);
5225 // Upgrade any old intrinsic calls in the function.
5226 for (auto &I
: UpgradedIntrinsics
) {
5227 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
5231 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
5232 UpgradeIntrinsicCall(CI
, I
.second
);
5236 // Update calls to the remangled intrinsics
5237 for (auto &I
: RemangledIntrinsics
)
5238 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
5240 // Don't expect any other users than call sites
5241 CallSite(*UI
++).setCalledFunction(I
.second
);
5243 // Finish fn->subprogram upgrade for materialized functions.
5244 if (DISubprogram
*SP
= MDLoader
->lookupSubprogramForFunction(F
))
5245 F
->setSubprogram(SP
);
5247 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
5248 if (!MDLoader
->isStrippingTBAA()) {
5249 for (auto &I
: instructions(F
)) {
5250 MDNode
*TBAA
= I
.getMetadata(LLVMContext::MD_tbaa
);
5251 if (!TBAA
|| TBAAVerifyHelper
.visitTBAAMetadata(I
, TBAA
))
5253 MDLoader
->setStripTBAA(true);
5254 stripTBAA(F
->getParent());
5258 // Bring in any functions that this function forward-referenced via
5260 return materializeForwardReferencedFunctions();
5263 Error
BitcodeReader::materializeModule() {
5264 if (Error Err
= materializeMetadata())
5267 // Promise to materialize all forward references.
5268 WillMaterializeAllForwardRefs
= true;
5270 // Iterate over the module, deserializing any functions that are still on
5272 for (Function
&F
: *TheModule
) {
5273 if (Error Err
= materialize(&F
))
5276 // At this point, if there are any function bodies, parse the rest of
5277 // the bits in the module past the last function block we have recorded
5278 // through either lazy scanning or the VST.
5279 if (LastFunctionBlockBit
|| NextUnreadBit
)
5280 if (Error Err
= parseModule(LastFunctionBlockBit
> NextUnreadBit
5281 ? LastFunctionBlockBit
5285 // Check that all block address forward references got resolved (as we
5287 if (!BasicBlockFwdRefs
.empty())
5288 return error("Never resolved function from blockaddress");
5290 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5291 // delete the old functions to clean up. We can't do this unless the entire
5292 // module is materialized because there could always be another function body
5293 // with calls to the old function.
5294 for (auto &I
: UpgradedIntrinsics
) {
5295 for (auto *U
: I
.first
->users()) {
5296 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
5297 UpgradeIntrinsicCall(CI
, I
.second
);
5299 if (!I
.first
->use_empty())
5300 I
.first
->replaceAllUsesWith(I
.second
);
5301 I
.first
->eraseFromParent();
5303 UpgradedIntrinsics
.clear();
5304 // Do the same for remangled intrinsics
5305 for (auto &I
: RemangledIntrinsics
) {
5306 I
.first
->replaceAllUsesWith(I
.second
);
5307 I
.first
->eraseFromParent();
5309 RemangledIntrinsics
.clear();
5311 UpgradeDebugInfo(*TheModule
);
5313 UpgradeModuleFlags(*TheModule
);
5315 UpgradeRetainReleaseMarker(*TheModule
);
5317 return Error::success();
5320 std::vector
<StructType
*> BitcodeReader::getIdentifiedStructTypes() const {
5321 return IdentifiedStructTypes
;
5324 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5325 BitstreamCursor Cursor
, StringRef Strtab
, ModuleSummaryIndex
&TheIndex
,
5326 StringRef ModulePath
, unsigned ModuleId
)
5327 : BitcodeReaderBase(std::move(Cursor
), Strtab
), TheIndex(TheIndex
),
5328 ModulePath(ModulePath
), ModuleId(ModuleId
) {}
5330 void ModuleSummaryIndexBitcodeReader::addThisModule() {
5331 TheIndex
.addModule(ModulePath
, ModuleId
);
5334 ModuleSummaryIndex::ModuleInfo
*
5335 ModuleSummaryIndexBitcodeReader::getThisModule() {
5336 return TheIndex
.getModule(ModulePath
);
5339 std::pair
<ValueInfo
, GlobalValue::GUID
>
5340 ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId
) {
5341 auto VGI
= ValueIdToValueInfoMap
[ValueId
];
5346 void ModuleSummaryIndexBitcodeReader::setValueGUID(
5347 uint64_t ValueID
, StringRef ValueName
, GlobalValue::LinkageTypes Linkage
,
5348 StringRef SourceFileName
) {
5349 std::string GlobalId
=
5350 GlobalValue::getGlobalIdentifier(ValueName
, Linkage
, SourceFileName
);
5351 auto ValueGUID
= GlobalValue::getGUID(GlobalId
);
5352 auto OriginalNameID
= ValueGUID
;
5353 if (GlobalValue::isLocalLinkage(Linkage
))
5354 OriginalNameID
= GlobalValue::getGUID(ValueName
);
5355 if (PrintSummaryGUIDs
)
5356 dbgs() << "GUID " << ValueGUID
<< "(" << OriginalNameID
<< ") is "
5357 << ValueName
<< "\n";
5359 // UseStrtab is false for legacy summary formats and value names are
5360 // created on stack. In that case we save the name in a string saver in
5361 // the index so that the value name can be recorded.
5362 ValueIdToValueInfoMap
[ValueID
] = std::make_pair(
5363 TheIndex
.getOrInsertValueInfo(
5365 UseStrtab
? ValueName
: TheIndex
.saveString(ValueName
)),
5369 // Specialized value symbol table parser used when reading module index
5370 // blocks where we don't actually create global values. The parsed information
5371 // is saved in the bitcode reader for use when later parsing summaries.
5372 Error
ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
5374 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
) {
5375 // With a strtab the VST is not required to parse the summary.
5377 return Error::success();
5379 assert(Offset
> 0 && "Expected non-zero VST offset");
5380 Expected
<uint64_t> MaybeCurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
5381 if (!MaybeCurrentBit
)
5382 return MaybeCurrentBit
.takeError();
5383 uint64_t CurrentBit
= MaybeCurrentBit
.get();
5385 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
5388 SmallVector
<uint64_t, 64> Record
;
5390 // Read all the records for this value table.
5391 SmallString
<128> ValueName
;
5394 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
5396 return MaybeEntry
.takeError();
5397 BitstreamEntry Entry
= MaybeEntry
.get();
5399 switch (Entry
.Kind
) {
5400 case BitstreamEntry::SubBlock
: // Handled for us already.
5401 case BitstreamEntry::Error
:
5402 return error("Malformed block");
5403 case BitstreamEntry::EndBlock
:
5404 // Done parsing VST, jump back to wherever we came from.
5405 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
5407 return Error::success();
5408 case BitstreamEntry::Record
:
5409 // The interesting case.
5415 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
5417 return MaybeRecord
.takeError();
5418 switch (MaybeRecord
.get()) {
5419 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5421 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
5422 if (convertToString(Record
, 1, ValueName
))
5423 return error("Invalid record");
5424 unsigned ValueID
= Record
[0];
5425 assert(!SourceFileName
.empty());
5426 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
5427 assert(VLI
!= ValueIdToLinkageMap
.end() &&
5428 "No linkage found for VST entry?");
5429 auto Linkage
= VLI
->second
;
5430 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
5434 case bitc::VST_CODE_FNENTRY
: {
5435 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
5436 if (convertToString(Record
, 2, ValueName
))
5437 return error("Invalid record");
5438 unsigned ValueID
= Record
[0];
5439 assert(!SourceFileName
.empty());
5440 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
5441 assert(VLI
!= ValueIdToLinkageMap
.end() &&
5442 "No linkage found for VST entry?");
5443 auto Linkage
= VLI
->second
;
5444 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
5448 case bitc::VST_CODE_COMBINED_ENTRY
: {
5449 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
5450 unsigned ValueID
= Record
[0];
5451 GlobalValue::GUID RefGUID
= Record
[1];
5452 // The "original name", which is the second value of the pair will be
5453 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5454 ValueIdToValueInfoMap
[ValueID
] =
5455 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
5462 // Parse just the blocks needed for building the index out of the module.
5463 // At the end of this routine the module Index is populated with a map
5464 // from global value id to GlobalValueSummary objects.
5465 Error
ModuleSummaryIndexBitcodeReader::parseModule() {
5466 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
5469 SmallVector
<uint64_t, 64> Record
;
5470 DenseMap
<unsigned, GlobalValue::LinkageTypes
> ValueIdToLinkageMap
;
5471 unsigned ValueId
= 0;
5473 // Read the index for this module.
5475 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
5477 return MaybeEntry
.takeError();
5478 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
5480 switch (Entry
.Kind
) {
5481 case BitstreamEntry::Error
:
5482 return error("Malformed block");
5483 case BitstreamEntry::EndBlock
:
5484 return Error::success();
5486 case BitstreamEntry::SubBlock
:
5488 default: // Skip unknown content.
5489 if (Error Err
= Stream
.SkipBlock())
5492 case bitc::BLOCKINFO_BLOCK_ID
:
5493 // Need to parse these to get abbrev ids (e.g. for VST)
5494 if (readBlockInfo())
5495 return error("Malformed block");
5497 case bitc::VALUE_SYMTAB_BLOCK_ID
:
5498 // Should have been parsed earlier via VSTOffset, unless there
5499 // is no summary section.
5500 assert(((SeenValueSymbolTable
&& VSTOffset
> 0) ||
5501 !SeenGlobalValSummary
) &&
5502 "Expected early VST parse via VSTOffset record");
5503 if (Error Err
= Stream
.SkipBlock())
5506 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID
:
5507 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
:
5508 // Add the module if it is a per-module index (has a source file name).
5509 if (!SourceFileName
.empty())
5511 assert(!SeenValueSymbolTable
&&
5512 "Already read VST when parsing summary block?");
5513 // We might not have a VST if there were no values in the
5514 // summary. An empty summary block generated when we are
5515 // performing ThinLTO compiles so we don't later invoke
5516 // the regular LTO process on them.
5517 if (VSTOffset
> 0) {
5518 if (Error Err
= parseValueSymbolTable(VSTOffset
, ValueIdToLinkageMap
))
5520 SeenValueSymbolTable
= true;
5522 SeenGlobalValSummary
= true;
5523 if (Error Err
= parseEntireSummary(Entry
.ID
))
5526 case bitc::MODULE_STRTAB_BLOCK_ID
:
5527 if (Error Err
= parseModuleStringTable())
5533 case BitstreamEntry::Record
: {
5535 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
5537 return MaybeBitCode
.takeError();
5538 switch (MaybeBitCode
.get()) {
5540 break; // Default behavior, ignore unknown content.
5541 case bitc::MODULE_CODE_VERSION
: {
5542 if (Error Err
= parseVersionRecord(Record
).takeError())
5546 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5547 case bitc::MODULE_CODE_SOURCE_FILENAME
: {
5548 SmallString
<128> ValueName
;
5549 if (convertToString(Record
, 0, ValueName
))
5550 return error("Invalid record");
5551 SourceFileName
= ValueName
.c_str();
5554 /// MODULE_CODE_HASH: [5*i32]
5555 case bitc::MODULE_CODE_HASH
: {
5556 if (Record
.size() != 5)
5557 return error("Invalid hash length " + Twine(Record
.size()).str());
5558 auto &Hash
= getThisModule()->second
.second
;
5560 for (auto &Val
: Record
) {
5561 assert(!(Val
>> 32) && "Unexpected high bits set");
5566 /// MODULE_CODE_VSTOFFSET: [offset]
5567 case bitc::MODULE_CODE_VSTOFFSET
:
5568 if (Record
.size() < 1)
5569 return error("Invalid record");
5570 // Note that we subtract 1 here because the offset is relative to one
5571 // word before the start of the identification or module block, which
5572 // was historically always the start of the regular bitcode header.
5573 VSTOffset
= Record
[0] - 1;
5575 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
5576 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
5577 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
5578 // v2: [strtab offset, strtab size, v1]
5579 case bitc::MODULE_CODE_GLOBALVAR
:
5580 case bitc::MODULE_CODE_FUNCTION
:
5581 case bitc::MODULE_CODE_ALIAS
: {
5583 ArrayRef
<uint64_t> GVRecord
;
5584 std::tie(Name
, GVRecord
) = readNameFromStrtab(Record
);
5585 if (GVRecord
.size() <= 3)
5586 return error("Invalid record");
5587 uint64_t RawLinkage
= GVRecord
[3];
5588 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
5590 ValueIdToLinkageMap
[ValueId
++] = Linkage
;
5594 setValueGUID(ValueId
++, Name
, Linkage
, SourceFileName
);
5604 std::vector
<ValueInfo
>
5605 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef
<uint64_t> Record
) {
5606 std::vector
<ValueInfo
> Ret
;
5607 Ret
.reserve(Record
.size());
5608 for (uint64_t RefValueId
: Record
)
5609 Ret
.push_back(getValueInfoFromValueId(RefValueId
).first
);
5613 std::vector
<FunctionSummary::EdgeTy
>
5614 ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef
<uint64_t> Record
,
5615 bool IsOldProfileFormat
,
5616 bool HasProfile
, bool HasRelBF
) {
5617 std::vector
<FunctionSummary::EdgeTy
> Ret
;
5618 Ret
.reserve(Record
.size());
5619 for (unsigned I
= 0, E
= Record
.size(); I
!= E
; ++I
) {
5620 CalleeInfo::HotnessType Hotness
= CalleeInfo::HotnessType::Unknown
;
5622 ValueInfo Callee
= getValueInfoFromValueId(Record
[I
]).first
;
5623 if (IsOldProfileFormat
) {
5624 I
+= 1; // Skip old callsitecount field
5626 I
+= 1; // Skip old profilecount field
5627 } else if (HasProfile
)
5628 Hotness
= static_cast<CalleeInfo::HotnessType
>(Record
[++I
]);
5630 RelBF
= Record
[++I
];
5631 Ret
.push_back(FunctionSummary::EdgeTy
{Callee
, CalleeInfo(Hotness
, RelBF
)});
5637 parseWholeProgramDevirtResolutionByArg(ArrayRef
<uint64_t> Record
, size_t &Slot
,
5638 WholeProgramDevirtResolution
&Wpd
) {
5639 uint64_t ArgNum
= Record
[Slot
++];
5640 WholeProgramDevirtResolution::ByArg
&B
=
5641 Wpd
.ResByArg
[{Record
.begin() + Slot
, Record
.begin() + Slot
+ ArgNum
}];
5645 static_cast<WholeProgramDevirtResolution::ByArg::Kind
>(Record
[Slot
++]);
5646 B
.Info
= Record
[Slot
++];
5647 B
.Byte
= Record
[Slot
++];
5648 B
.Bit
= Record
[Slot
++];
5651 static void parseWholeProgramDevirtResolution(ArrayRef
<uint64_t> Record
,
5652 StringRef Strtab
, size_t &Slot
,
5653 TypeIdSummary
&TypeId
) {
5654 uint64_t Id
= Record
[Slot
++];
5655 WholeProgramDevirtResolution
&Wpd
= TypeId
.WPDRes
[Id
];
5657 Wpd
.TheKind
= static_cast<WholeProgramDevirtResolution::Kind
>(Record
[Slot
++]);
5658 Wpd
.SingleImplName
= {Strtab
.data() + Record
[Slot
],
5659 static_cast<size_t>(Record
[Slot
+ 1])};
5662 uint64_t ResByArgNum
= Record
[Slot
++];
5663 for (uint64_t I
= 0; I
!= ResByArgNum
; ++I
)
5664 parseWholeProgramDevirtResolutionByArg(Record
, Slot
, Wpd
);
5667 static void parseTypeIdSummaryRecord(ArrayRef
<uint64_t> Record
,
5669 ModuleSummaryIndex
&TheIndex
) {
5671 TypeIdSummary
&TypeId
= TheIndex
.getOrInsertTypeIdSummary(
5672 {Strtab
.data() + Record
[Slot
], static_cast<size_t>(Record
[Slot
+ 1])});
5675 TypeId
.TTRes
.TheKind
= static_cast<TypeTestResolution::Kind
>(Record
[Slot
++]);
5676 TypeId
.TTRes
.SizeM1BitWidth
= Record
[Slot
++];
5677 TypeId
.TTRes
.AlignLog2
= Record
[Slot
++];
5678 TypeId
.TTRes
.SizeM1
= Record
[Slot
++];
5679 TypeId
.TTRes
.BitMask
= Record
[Slot
++];
5680 TypeId
.TTRes
.InlineBits
= Record
[Slot
++];
5682 while (Slot
< Record
.size())
5683 parseWholeProgramDevirtResolution(Record
, Strtab
, Slot
, TypeId
);
5686 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo(
5687 ArrayRef
<uint64_t> Record
, size_t &Slot
,
5688 TypeIdCompatibleVtableInfo
&TypeId
) {
5689 uint64_t Offset
= Record
[Slot
++];
5690 ValueInfo Callee
= getValueInfoFromValueId(Record
[Slot
++]).first
;
5691 TypeId
.push_back({Offset
, Callee
});
5694 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(
5695 ArrayRef
<uint64_t> Record
) {
5697 TypeIdCompatibleVtableInfo
&TypeId
=
5698 TheIndex
.getOrInsertTypeIdCompatibleVtableSummary(
5699 {Strtab
.data() + Record
[Slot
],
5700 static_cast<size_t>(Record
[Slot
+ 1])});
5703 while (Slot
< Record
.size())
5704 parseTypeIdCompatibleVtableInfo(Record
, Slot
, TypeId
);
5707 static void setSpecialRefs(std::vector
<ValueInfo
> &Refs
, unsigned ROCnt
,
5709 // Readonly and writeonly refs are in the end of the refs list.
5710 assert(ROCnt
+ WOCnt
<= Refs
.size());
5711 unsigned FirstWORef
= Refs
.size() - WOCnt
;
5712 unsigned RefNo
= FirstWORef
- ROCnt
;
5713 for (; RefNo
< FirstWORef
; ++RefNo
)
5714 Refs
[RefNo
].setReadOnly();
5715 for (; RefNo
< Refs
.size(); ++RefNo
)
5716 Refs
[RefNo
].setWriteOnly();
5719 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
5720 // objects in the index.
5721 Error
ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID
) {
5722 if (Error Err
= Stream
.EnterSubBlock(ID
))
5724 SmallVector
<uint64_t, 64> Record
;
5728 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
5730 return MaybeEntry
.takeError();
5731 BitstreamEntry Entry
= MaybeEntry
.get();
5733 if (Entry
.Kind
!= BitstreamEntry::Record
)
5734 return error("Invalid Summary Block: record for version expected");
5735 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
5737 return MaybeRecord
.takeError();
5738 if (MaybeRecord
.get() != bitc::FS_VERSION
)
5739 return error("Invalid Summary Block: version expected");
5741 const uint64_t Version
= Record
[0];
5742 const bool IsOldProfileFormat
= Version
== 1;
5743 if (Version
< 1 || Version
> 7)
5744 return error("Invalid summary version " + Twine(Version
) +
5745 ". Version should be in the range [1-7].");
5748 // Keep around the last seen summary to be used when we see an optional
5749 // "OriginalName" attachement.
5750 GlobalValueSummary
*LastSeenSummary
= nullptr;
5751 GlobalValue::GUID LastSeenGUID
= 0;
5753 // We can expect to see any number of type ID information records before
5754 // each function summary records; these variables store the information
5755 // collected so far so that it can be used to create the summary object.
5756 std::vector
<GlobalValue::GUID
> PendingTypeTests
;
5757 std::vector
<FunctionSummary::VFuncId
> PendingTypeTestAssumeVCalls
,
5758 PendingTypeCheckedLoadVCalls
;
5759 std::vector
<FunctionSummary::ConstVCall
> PendingTypeTestAssumeConstVCalls
,
5760 PendingTypeCheckedLoadConstVCalls
;
5763 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
5765 return MaybeEntry
.takeError();
5766 BitstreamEntry Entry
= MaybeEntry
.get();
5768 switch (Entry
.Kind
) {
5769 case BitstreamEntry::SubBlock
: // Handled for us already.
5770 case BitstreamEntry::Error
:
5771 return error("Malformed block");
5772 case BitstreamEntry::EndBlock
:
5773 return Error::success();
5774 case BitstreamEntry::Record
:
5775 // The interesting case.
5779 // Read a record. The record format depends on whether this
5780 // is a per-module index or a combined index file. In the per-module
5781 // case the records contain the associated value's ID for correlation
5782 // with VST entries. In the combined index the correlation is done
5783 // via the bitcode offset of the summary records (which were saved
5784 // in the combined index VST entries). The records also contain
5785 // information used for ThinLTO renaming and importing.
5787 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
5789 return MaybeBitCode
.takeError();
5790 switch (unsigned BitCode
= MaybeBitCode
.get()) {
5791 default: // Default behavior: ignore.
5793 case bitc::FS_FLAGS
: { // [flags]
5794 uint64_t Flags
= Record
[0];
5796 assert(Flags
<= 0x1f && "Unexpected bits in flag");
5798 // 1 bit: WithGlobalValueDeadStripping flag.
5799 // Set on combined index only.
5801 TheIndex
.setWithGlobalValueDeadStripping();
5802 // 1 bit: SkipModuleByDistributedBackend flag.
5803 // Set on combined index only.
5805 TheIndex
.setSkipModuleByDistributedBackend();
5806 // 1 bit: HasSyntheticEntryCounts flag.
5807 // Set on combined index only.
5809 TheIndex
.setHasSyntheticEntryCounts();
5810 // 1 bit: DisableSplitLTOUnit flag.
5811 // Set on per module indexes. It is up to the client to validate
5812 // the consistency of this flag across modules being linked.
5814 TheIndex
.setEnableSplitLTOUnit();
5815 // 1 bit: PartiallySplitLTOUnits flag.
5816 // Set on combined index only.
5818 TheIndex
.setPartiallySplitLTOUnits();
5821 case bitc::FS_VALUE_GUID
: { // [valueid, refguid]
5822 uint64_t ValueID
= Record
[0];
5823 GlobalValue::GUID RefGUID
= Record
[1];
5824 ValueIdToValueInfoMap
[ValueID
] =
5825 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
5828 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,
5829 // numrefs x valueid, n x (valueid)]
5830 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,
5831 // numrefs x valueid,
5832 // n x (valueid, hotness)]
5833 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,
5834 // numrefs x valueid,
5835 // n x (valueid, relblockfreq)]
5836 case bitc::FS_PERMODULE
:
5837 case bitc::FS_PERMODULE_RELBF
:
5838 case bitc::FS_PERMODULE_PROFILE
: {
5839 unsigned ValueID
= Record
[0];
5840 uint64_t RawFlags
= Record
[1];
5841 unsigned InstCount
= Record
[2];
5842 uint64_t RawFunFlags
= 0;
5843 unsigned NumRefs
= Record
[3];
5844 unsigned NumRORefs
= 0, NumWORefs
= 0;
5845 int RefListStartIndex
= 4;
5847 RawFunFlags
= Record
[3];
5848 NumRefs
= Record
[4];
5849 RefListStartIndex
= 5;
5851 NumRORefs
= Record
[5];
5852 RefListStartIndex
= 6;
5854 NumWORefs
= Record
[6];
5855 RefListStartIndex
= 7;
5860 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5861 // The module path string ref set in the summary must be owned by the
5862 // index's module string table. Since we don't have a module path
5863 // string table section in the per-module index, we create a single
5864 // module path string table entry with an empty (0) ID to take
5866 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
5867 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
5868 "Record size inconsistent with number of references");
5869 std::vector
<ValueInfo
> Refs
= makeRefList(
5870 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
5871 bool HasProfile
= (BitCode
== bitc::FS_PERMODULE_PROFILE
);
5872 bool HasRelBF
= (BitCode
== bitc::FS_PERMODULE_RELBF
);
5873 std::vector
<FunctionSummary::EdgeTy
> Calls
= makeCallList(
5874 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
5875 IsOldProfileFormat
, HasProfile
, HasRelBF
);
5876 setSpecialRefs(Refs
, NumRORefs
, NumWORefs
);
5877 auto FS
= llvm::make_unique
<FunctionSummary
>(
5878 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), /*EntryCount=*/0,
5879 std::move(Refs
), std::move(Calls
), std::move(PendingTypeTests
),
5880 std::move(PendingTypeTestAssumeVCalls
),
5881 std::move(PendingTypeCheckedLoadVCalls
),
5882 std::move(PendingTypeTestAssumeConstVCalls
),
5883 std::move(PendingTypeCheckedLoadConstVCalls
));
5884 PendingTypeTests
.clear();
5885 PendingTypeTestAssumeVCalls
.clear();
5886 PendingTypeCheckedLoadVCalls
.clear();
5887 PendingTypeTestAssumeConstVCalls
.clear();
5888 PendingTypeCheckedLoadConstVCalls
.clear();
5889 auto VIAndOriginalGUID
= getValueInfoFromValueId(ValueID
);
5890 FS
->setModulePath(getThisModule()->first());
5891 FS
->setOriginalName(VIAndOriginalGUID
.second
);
5892 TheIndex
.addGlobalValueSummary(VIAndOriginalGUID
.first
, std::move(FS
));
5895 // FS_ALIAS: [valueid, flags, valueid]
5896 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
5897 // they expect all aliasee summaries to be available.
5898 case bitc::FS_ALIAS
: {
5899 unsigned ValueID
= Record
[0];
5900 uint64_t RawFlags
= Record
[1];
5901 unsigned AliaseeID
= Record
[2];
5902 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5903 auto AS
= llvm::make_unique
<AliasSummary
>(Flags
);
5904 // The module path string ref set in the summary must be owned by the
5905 // index's module string table. Since we don't have a module path
5906 // string table section in the per-module index, we create a single
5907 // module path string table entry with an empty (0) ID to take
5909 AS
->setModulePath(getThisModule()->first());
5911 auto AliaseeVI
= getValueInfoFromValueId(AliaseeID
).first
;
5912 auto AliaseeInModule
= TheIndex
.findSummaryInModule(AliaseeVI
, ModulePath
);
5913 if (!AliaseeInModule
)
5914 return error("Alias expects aliasee summary to be parsed");
5915 AS
->setAliasee(AliaseeVI
, AliaseeInModule
);
5917 auto GUID
= getValueInfoFromValueId(ValueID
);
5918 AS
->setOriginalName(GUID
.second
);
5919 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(AS
));
5922 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid]
5923 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS
: {
5924 unsigned ValueID
= Record
[0];
5925 uint64_t RawFlags
= Record
[1];
5926 unsigned RefArrayStart
= 2;
5927 GlobalVarSummary::GVarFlags
GVF(/* ReadOnly */ false,
5928 /* WriteOnly */ false);
5929 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5931 GVF
= getDecodedGVarFlags(Record
[2]);
5934 std::vector
<ValueInfo
> Refs
=
5935 makeRefList(ArrayRef
<uint64_t>(Record
).slice(RefArrayStart
));
5937 llvm::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
5938 FS
->setModulePath(getThisModule()->first());
5939 auto GUID
= getValueInfoFromValueId(ValueID
);
5940 FS
->setOriginalName(GUID
.second
);
5941 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(FS
));
5944 // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags,
5945 // numrefs, numrefs x valueid,
5946 // n x (valueid, offset)]
5947 case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS
: {
5948 unsigned ValueID
= Record
[0];
5949 uint64_t RawFlags
= Record
[1];
5950 GlobalVarSummary::GVarFlags GVF
= getDecodedGVarFlags(Record
[2]);
5951 unsigned NumRefs
= Record
[3];
5952 unsigned RefListStartIndex
= 4;
5953 unsigned VTableListStartIndex
= RefListStartIndex
+ NumRefs
;
5954 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
5955 std::vector
<ValueInfo
> Refs
= makeRefList(
5956 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
5957 VTableFuncList VTableFuncs
;
5958 for (unsigned I
= VTableListStartIndex
, E
= Record
.size(); I
!= E
; ++I
) {
5959 ValueInfo Callee
= getValueInfoFromValueId(Record
[I
]).first
;
5960 uint64_t Offset
= Record
[++I
];
5961 VTableFuncs
.push_back({Callee
, Offset
});
5964 llvm::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
5965 VS
->setModulePath(getThisModule()->first());
5966 VS
->setVTableFuncs(VTableFuncs
);
5967 auto GUID
= getValueInfoFromValueId(ValueID
);
5968 VS
->setOriginalName(GUID
.second
);
5969 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(VS
));
5972 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,
5973 // numrefs x valueid, n x (valueid)]
5974 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,
5975 // numrefs x valueid, n x (valueid, hotness)]
5976 case bitc::FS_COMBINED
:
5977 case bitc::FS_COMBINED_PROFILE
: {
5978 unsigned ValueID
= Record
[0];
5979 uint64_t ModuleId
= Record
[1];
5980 uint64_t RawFlags
= Record
[2];
5981 unsigned InstCount
= Record
[3];
5982 uint64_t RawFunFlags
= 0;
5983 uint64_t EntryCount
= 0;
5984 unsigned NumRefs
= Record
[4];
5985 unsigned NumRORefs
= 0, NumWORefs
= 0;
5986 int RefListStartIndex
= 5;
5989 RawFunFlags
= Record
[4];
5990 RefListStartIndex
= 6;
5991 size_t NumRefsIndex
= 5;
5993 unsigned NumRORefsOffset
= 1;
5994 RefListStartIndex
= 7;
5997 EntryCount
= Record
[5];
5998 RefListStartIndex
= 8;
6000 RefListStartIndex
= 9;
6001 NumWORefs
= Record
[8];
6002 NumRORefsOffset
= 2;
6005 NumRORefs
= Record
[RefListStartIndex
- NumRORefsOffset
];
6007 NumRefs
= Record
[NumRefsIndex
];
6010 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6011 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
6012 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
6013 "Record size inconsistent with number of references");
6014 std::vector
<ValueInfo
> Refs
= makeRefList(
6015 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
6016 bool HasProfile
= (BitCode
== bitc::FS_COMBINED_PROFILE
);
6017 std::vector
<FunctionSummary::EdgeTy
> Edges
= makeCallList(
6018 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
6019 IsOldProfileFormat
, HasProfile
, false);
6020 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6021 setSpecialRefs(Refs
, NumRORefs
, NumWORefs
);
6022 auto FS
= llvm::make_unique
<FunctionSummary
>(
6023 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), EntryCount
,
6024 std::move(Refs
), std::move(Edges
), std::move(PendingTypeTests
),
6025 std::move(PendingTypeTestAssumeVCalls
),
6026 std::move(PendingTypeCheckedLoadVCalls
),
6027 std::move(PendingTypeTestAssumeConstVCalls
),
6028 std::move(PendingTypeCheckedLoadConstVCalls
));
6029 PendingTypeTests
.clear();
6030 PendingTypeTestAssumeVCalls
.clear();
6031 PendingTypeCheckedLoadVCalls
.clear();
6032 PendingTypeTestAssumeConstVCalls
.clear();
6033 PendingTypeCheckedLoadConstVCalls
.clear();
6034 LastSeenSummary
= FS
.get();
6035 LastSeenGUID
= VI
.getGUID();
6036 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
6037 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
6040 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]
6041 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
6042 // they expect all aliasee summaries to be available.
6043 case bitc::FS_COMBINED_ALIAS
: {
6044 unsigned ValueID
= Record
[0];
6045 uint64_t ModuleId
= Record
[1];
6046 uint64_t RawFlags
= Record
[2];
6047 unsigned AliaseeValueId
= Record
[3];
6048 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6049 auto AS
= llvm::make_unique
<AliasSummary
>(Flags
);
6050 LastSeenSummary
= AS
.get();
6051 AS
->setModulePath(ModuleIdMap
[ModuleId
]);
6053 auto AliaseeVI
= getValueInfoFromValueId(AliaseeValueId
).first
;
6054 auto AliaseeInModule
= TheIndex
.findSummaryInModule(AliaseeVI
, AS
->modulePath());
6055 AS
->setAliasee(AliaseeVI
, AliaseeInModule
);
6057 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6058 LastSeenGUID
= VI
.getGUID();
6059 TheIndex
.addGlobalValueSummary(VI
, std::move(AS
));
6062 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]
6063 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS
: {
6064 unsigned ValueID
= Record
[0];
6065 uint64_t ModuleId
= Record
[1];
6066 uint64_t RawFlags
= Record
[2];
6067 unsigned RefArrayStart
= 3;
6068 GlobalVarSummary::GVarFlags
GVF(/* ReadOnly */ false,
6069 /* WriteOnly */ false);
6070 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6072 GVF
= getDecodedGVarFlags(Record
[3]);
6075 std::vector
<ValueInfo
> Refs
=
6076 makeRefList(ArrayRef
<uint64_t>(Record
).slice(RefArrayStart
));
6078 llvm::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
6079 LastSeenSummary
= FS
.get();
6080 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
6081 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6082 LastSeenGUID
= VI
.getGUID();
6083 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
6086 // FS_COMBINED_ORIGINAL_NAME: [original_name]
6087 case bitc::FS_COMBINED_ORIGINAL_NAME
: {
6088 uint64_t OriginalName
= Record
[0];
6089 if (!LastSeenSummary
)
6090 return error("Name attachment that does not follow a combined record");
6091 LastSeenSummary
->setOriginalName(OriginalName
);
6092 TheIndex
.addOriginalName(LastSeenGUID
, OriginalName
);
6093 // Reset the LastSeenSummary
6094 LastSeenSummary
= nullptr;
6098 case bitc::FS_TYPE_TESTS
:
6099 assert(PendingTypeTests
.empty());
6100 PendingTypeTests
.insert(PendingTypeTests
.end(), Record
.begin(),
6104 case bitc::FS_TYPE_TEST_ASSUME_VCALLS
:
6105 assert(PendingTypeTestAssumeVCalls
.empty());
6106 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6107 PendingTypeTestAssumeVCalls
.push_back({Record
[I
], Record
[I
+1]});
6110 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS
:
6111 assert(PendingTypeCheckedLoadVCalls
.empty());
6112 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6113 PendingTypeCheckedLoadVCalls
.push_back({Record
[I
], Record
[I
+1]});
6116 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL
:
6117 PendingTypeTestAssumeConstVCalls
.push_back(
6118 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
6121 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL
:
6122 PendingTypeCheckedLoadConstVCalls
.push_back(
6123 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
6126 case bitc::FS_CFI_FUNCTION_DEFS
: {
6127 std::set
<std::string
> &CfiFunctionDefs
= TheIndex
.cfiFunctionDefs();
6128 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6129 CfiFunctionDefs
.insert(
6130 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
6134 case bitc::FS_CFI_FUNCTION_DECLS
: {
6135 std::set
<std::string
> &CfiFunctionDecls
= TheIndex
.cfiFunctionDecls();
6136 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6137 CfiFunctionDecls
.insert(
6138 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
6142 case bitc::FS_TYPE_ID
:
6143 parseTypeIdSummaryRecord(Record
, Strtab
, TheIndex
);
6146 case bitc::FS_TYPE_ID_METADATA
:
6147 parseTypeIdCompatibleVtableSummaryRecord(Record
);
6151 llvm_unreachable("Exit infinite loop");
6154 // Parse the module string table block into the Index.
6155 // This populates the ModulePathStringTable map in the index.
6156 Error
ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
6157 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID
))
6160 SmallVector
<uint64_t, 64> Record
;
6162 SmallString
<128> ModulePath
;
6163 ModuleSummaryIndex::ModuleInfo
*LastSeenModule
= nullptr;
6166 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6168 return MaybeEntry
.takeError();
6169 BitstreamEntry Entry
= MaybeEntry
.get();
6171 switch (Entry
.Kind
) {
6172 case BitstreamEntry::SubBlock
: // Handled for us already.
6173 case BitstreamEntry::Error
:
6174 return error("Malformed block");
6175 case BitstreamEntry::EndBlock
:
6176 return Error::success();
6177 case BitstreamEntry::Record
:
6178 // The interesting case.
6183 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
6185 return MaybeRecord
.takeError();
6186 switch (MaybeRecord
.get()) {
6187 default: // Default behavior: ignore.
6189 case bitc::MST_CODE_ENTRY
: {
6190 // MST_ENTRY: [modid, namechar x N]
6191 uint64_t ModuleId
= Record
[0];
6193 if (convertToString(Record
, 1, ModulePath
))
6194 return error("Invalid record");
6196 LastSeenModule
= TheIndex
.addModule(ModulePath
, ModuleId
);
6197 ModuleIdMap
[ModuleId
] = LastSeenModule
->first();
6202 /// MST_CODE_HASH: [5*i32]
6203 case bitc::MST_CODE_HASH
: {
6204 if (Record
.size() != 5)
6205 return error("Invalid hash length " + Twine(Record
.size()).str());
6206 if (!LastSeenModule
)
6207 return error("Invalid hash that does not follow a module path");
6209 for (auto &Val
: Record
) {
6210 assert(!(Val
>> 32) && "Unexpected high bits set");
6211 LastSeenModule
->second
.second
[Pos
++] = Val
;
6213 // Reset LastSeenModule to avoid overriding the hash unexpectedly.
6214 LastSeenModule
= nullptr;
6219 llvm_unreachable("Exit infinite loop");
6224 // FIXME: This class is only here to support the transition to llvm::Error. It
6225 // will be removed once this transition is complete. Clients should prefer to
6226 // deal with the Error value directly, rather than converting to error_code.
6227 class BitcodeErrorCategoryType
: public std::error_category
{
6228 const char *name() const noexcept override
{
6229 return "llvm.bitcode";
6232 std::string
message(int IE
) const override
{
6233 BitcodeError E
= static_cast<BitcodeError
>(IE
);
6235 case BitcodeError::CorruptedBitcode
:
6236 return "Corrupted bitcode";
6238 llvm_unreachable("Unknown error type!");
6242 } // end anonymous namespace
6244 static ManagedStatic
<BitcodeErrorCategoryType
> ErrorCategory
;
6246 const std::error_category
&llvm::BitcodeErrorCategory() {
6247 return *ErrorCategory
;
6250 static Expected
<StringRef
> readBlobInRecord(BitstreamCursor
&Stream
,
6251 unsigned Block
, unsigned RecordID
) {
6252 if (Error Err
= Stream
.EnterSubBlock(Block
))
6253 return std::move(Err
);
6257 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6259 return MaybeEntry
.takeError();
6260 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6262 switch (Entry
.Kind
) {
6263 case BitstreamEntry::EndBlock
:
6266 case BitstreamEntry::Error
:
6267 return error("Malformed block");
6269 case BitstreamEntry::SubBlock
:
6270 if (Error Err
= Stream
.SkipBlock())
6271 return std::move(Err
);
6274 case BitstreamEntry::Record
:
6276 SmallVector
<uint64_t, 1> Record
;
6277 Expected
<unsigned> MaybeRecord
=
6278 Stream
.readRecord(Entry
.ID
, Record
, &Blob
);
6280 return MaybeRecord
.takeError();
6281 if (MaybeRecord
.get() == RecordID
)
6288 //===----------------------------------------------------------------------===//
6289 // External interface
6290 //===----------------------------------------------------------------------===//
6292 Expected
<std::vector
<BitcodeModule
>>
6293 llvm::getBitcodeModuleList(MemoryBufferRef Buffer
) {
6294 auto FOrErr
= getBitcodeFileContents(Buffer
);
6296 return FOrErr
.takeError();
6297 return std::move(FOrErr
->Mods
);
6300 Expected
<BitcodeFileContents
>
6301 llvm::getBitcodeFileContents(MemoryBufferRef Buffer
) {
6302 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6304 return StreamOrErr
.takeError();
6305 BitstreamCursor
&Stream
= *StreamOrErr
;
6307 BitcodeFileContents F
;
6309 uint64_t BCBegin
= Stream
.getCurrentByteNo();
6311 // We may be consuming bitcode from a client that leaves garbage at the end
6312 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
6313 // the end that there cannot possibly be another module, stop looking.
6314 if (BCBegin
+ 8 >= Stream
.getBitcodeBytes().size())
6317 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6319 return MaybeEntry
.takeError();
6320 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6322 switch (Entry
.Kind
) {
6323 case BitstreamEntry::EndBlock
:
6324 case BitstreamEntry::Error
:
6325 return error("Malformed block");
6327 case BitstreamEntry::SubBlock
: {
6328 uint64_t IdentificationBit
= -1ull;
6329 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
) {
6330 IdentificationBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
6331 if (Error Err
= Stream
.SkipBlock())
6332 return std::move(Err
);
6335 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6337 return MaybeEntry
.takeError();
6338 Entry
= MaybeEntry
.get();
6341 if (Entry
.Kind
!= BitstreamEntry::SubBlock
||
6342 Entry
.ID
!= bitc::MODULE_BLOCK_ID
)
6343 return error("Malformed block");
6346 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
) {
6347 uint64_t ModuleBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
6348 if (Error Err
= Stream
.SkipBlock())
6349 return std::move(Err
);
6351 F
.Mods
.push_back({Stream
.getBitcodeBytes().slice(
6352 BCBegin
, Stream
.getCurrentByteNo() - BCBegin
),
6353 Buffer
.getBufferIdentifier(), IdentificationBit
,
6358 if (Entry
.ID
== bitc::STRTAB_BLOCK_ID
) {
6359 Expected
<StringRef
> Strtab
=
6360 readBlobInRecord(Stream
, bitc::STRTAB_BLOCK_ID
, bitc::STRTAB_BLOB
);
6362 return Strtab
.takeError();
6363 // This string table is used by every preceding bitcode module that does
6364 // not have its own string table. A bitcode file may have multiple
6365 // string tables if it was created by binary concatenation, for example
6366 // with "llvm-cat -b".
6367 for (auto I
= F
.Mods
.rbegin(), E
= F
.Mods
.rend(); I
!= E
; ++I
) {
6368 if (!I
->Strtab
.empty())
6370 I
->Strtab
= *Strtab
;
6372 // Similarly, the string table is used by every preceding symbol table;
6373 // normally there will be just one unless the bitcode file was created
6374 // by binary concatenation.
6375 if (!F
.Symtab
.empty() && F
.StrtabForSymtab
.empty())
6376 F
.StrtabForSymtab
= *Strtab
;
6380 if (Entry
.ID
== bitc::SYMTAB_BLOCK_ID
) {
6381 Expected
<StringRef
> SymtabOrErr
=
6382 readBlobInRecord(Stream
, bitc::SYMTAB_BLOCK_ID
, bitc::SYMTAB_BLOB
);
6384 return SymtabOrErr
.takeError();
6386 // We can expect the bitcode file to have multiple symbol tables if it
6387 // was created by binary concatenation. In that case we silently
6388 // ignore any subsequent symbol tables, which is fine because this is a
6389 // low level function. The client is expected to notice that the number
6390 // of modules in the symbol table does not match the number of modules
6391 // in the input file and regenerate the symbol table.
6392 if (F
.Symtab
.empty())
6393 F
.Symtab
= *SymtabOrErr
;
6397 if (Error Err
= Stream
.SkipBlock())
6398 return std::move(Err
);
6401 case BitstreamEntry::Record
:
6402 if (Expected
<unsigned> StreamFailed
= Stream
.skipRecord(Entry
.ID
))
6405 return StreamFailed
.takeError();
6410 /// Get a lazy one-at-time loading module from bitcode.
6412 /// This isn't always used in a lazy context. In particular, it's also used by
6413 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull
6414 /// in forward-referenced functions from block address references.
6416 /// \param[in] MaterializeAll Set to \c true if we should materialize
6418 Expected
<std::unique_ptr
<Module
>>
6419 BitcodeModule::getModuleImpl(LLVMContext
&Context
, bool MaterializeAll
,
6420 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
6421 BitstreamCursor
Stream(Buffer
);
6423 std::string ProducerIdentification
;
6424 if (IdentificationBit
!= -1ull) {
6425 if (Error JumpFailed
= Stream
.JumpToBit(IdentificationBit
))
6426 return std::move(JumpFailed
);
6427 Expected
<std::string
> ProducerIdentificationOrErr
=
6428 readIdentificationBlock(Stream
);
6429 if (!ProducerIdentificationOrErr
)
6430 return ProducerIdentificationOrErr
.takeError();
6432 ProducerIdentification
= *ProducerIdentificationOrErr
;
6435 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6436 return std::move(JumpFailed
);
6437 auto *R
= new BitcodeReader(std::move(Stream
), Strtab
, ProducerIdentification
,
6440 std::unique_ptr
<Module
> M
=
6441 llvm::make_unique
<Module
>(ModuleIdentifier
, Context
);
6442 M
->setMaterializer(R
);
6444 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6446 R
->parseBitcodeInto(M
.get(), ShouldLazyLoadMetadata
, IsImporting
))
6447 return std::move(Err
);
6449 if (MaterializeAll
) {
6450 // Read in the entire module, and destroy the BitcodeReader.
6451 if (Error Err
= M
->materializeAll())
6452 return std::move(Err
);
6454 // Resolve forward references from blockaddresses.
6455 if (Error Err
= R
->materializeForwardReferencedFunctions())
6456 return std::move(Err
);
6458 return std::move(M
);
6461 Expected
<std::unique_ptr
<Module
>>
6462 BitcodeModule::getLazyModule(LLVMContext
&Context
, bool ShouldLazyLoadMetadata
,
6464 return getModuleImpl(Context
, false, ShouldLazyLoadMetadata
, IsImporting
);
6467 // Parse the specified bitcode buffer and merge the index into CombinedIndex.
6468 // We don't use ModuleIdentifier here because the client may need to control the
6469 // module path used in the combined summary (e.g. when reading summaries for
6470 // regular LTO modules).
6471 Error
BitcodeModule::readSummary(ModuleSummaryIndex
&CombinedIndex
,
6472 StringRef ModulePath
, uint64_t ModuleId
) {
6473 BitstreamCursor
Stream(Buffer
);
6474 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6477 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, CombinedIndex
,
6478 ModulePath
, ModuleId
);
6479 return R
.parseModule();
6482 // Parse the specified bitcode buffer, returning the function info index.
6483 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> BitcodeModule::getSummary() {
6484 BitstreamCursor
Stream(Buffer
);
6485 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6486 return std::move(JumpFailed
);
6488 auto Index
= llvm::make_unique
<ModuleSummaryIndex
>(/*HaveGVs=*/false);
6489 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, *Index
,
6490 ModuleIdentifier
, 0);
6492 if (Error Err
= R
.parseModule())
6493 return std::move(Err
);
6495 return std::move(Index
);
6498 static Expected
<bool> getEnableSplitLTOUnitFlag(BitstreamCursor
&Stream
,
6500 if (Error Err
= Stream
.EnterSubBlock(ID
))
6501 return std::move(Err
);
6502 SmallVector
<uint64_t, 64> Record
;
6505 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6507 return MaybeEntry
.takeError();
6508 BitstreamEntry Entry
= MaybeEntry
.get();
6510 switch (Entry
.Kind
) {
6511 case BitstreamEntry::SubBlock
: // Handled for us already.
6512 case BitstreamEntry::Error
:
6513 return error("Malformed block");
6514 case BitstreamEntry::EndBlock
:
6515 // If no flags record found, conservatively return true to mimic
6516 // behavior before this flag was added.
6518 case BitstreamEntry::Record
:
6519 // The interesting case.
6523 // Look for the FS_FLAGS record.
6525 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
6527 return MaybeBitCode
.takeError();
6528 switch (MaybeBitCode
.get()) {
6529 default: // Default behavior: ignore.
6531 case bitc::FS_FLAGS
: { // [flags]
6532 uint64_t Flags
= Record
[0];
6534 assert(Flags
<= 0x1f && "Unexpected bits in flag");
6540 llvm_unreachable("Exit infinite loop");
6543 // Check if the given bitcode buffer contains a global value summary block.
6544 Expected
<BitcodeLTOInfo
> BitcodeModule::getLTOInfo() {
6545 BitstreamCursor
Stream(Buffer
);
6546 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6547 return std::move(JumpFailed
);
6549 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
6550 return std::move(Err
);
6553 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6555 return MaybeEntry
.takeError();
6556 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6558 switch (Entry
.Kind
) {
6559 case BitstreamEntry::Error
:
6560 return error("Malformed block");
6561 case BitstreamEntry::EndBlock
:
6562 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/false,
6563 /*EnableSplitLTOUnit=*/false};
6565 case BitstreamEntry::SubBlock
:
6566 if (Entry
.ID
== bitc::GLOBALVAL_SUMMARY_BLOCK_ID
) {
6567 Expected
<bool> EnableSplitLTOUnit
=
6568 getEnableSplitLTOUnitFlag(Stream
, Entry
.ID
);
6569 if (!EnableSplitLTOUnit
)
6570 return EnableSplitLTOUnit
.takeError();
6571 return BitcodeLTOInfo
{/*IsThinLTO=*/true, /*HasSummary=*/true,
6572 *EnableSplitLTOUnit
};
6575 if (Entry
.ID
== bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
) {
6576 Expected
<bool> EnableSplitLTOUnit
=
6577 getEnableSplitLTOUnitFlag(Stream
, Entry
.ID
);
6578 if (!EnableSplitLTOUnit
)
6579 return EnableSplitLTOUnit
.takeError();
6580 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/true,
6581 *EnableSplitLTOUnit
};
6584 // Ignore other sub-blocks.
6585 if (Error Err
= Stream
.SkipBlock())
6586 return std::move(Err
);
6589 case BitstreamEntry::Record
:
6590 if (Expected
<unsigned> StreamFailed
= Stream
.skipRecord(Entry
.ID
))
6593 return StreamFailed
.takeError();
6598 static Expected
<BitcodeModule
> getSingleModule(MemoryBufferRef Buffer
) {
6599 Expected
<std::vector
<BitcodeModule
>> MsOrErr
= getBitcodeModuleList(Buffer
);
6601 return MsOrErr
.takeError();
6603 if (MsOrErr
->size() != 1)
6604 return error("Expected a single module");
6606 return (*MsOrErr
)[0];
6609 Expected
<std::unique_ptr
<Module
>>
6610 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer
, LLVMContext
&Context
,
6611 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
6612 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6614 return BM
.takeError();
6616 return BM
->getLazyModule(Context
, ShouldLazyLoadMetadata
, IsImporting
);
6619 Expected
<std::unique_ptr
<Module
>> llvm::getOwningLazyBitcodeModule(
6620 std::unique_ptr
<MemoryBuffer
> &&Buffer
, LLVMContext
&Context
,
6621 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
6622 auto MOrErr
= getLazyBitcodeModule(*Buffer
, Context
, ShouldLazyLoadMetadata
,
6625 (*MOrErr
)->setOwnedMemoryBuffer(std::move(Buffer
));
6629 Expected
<std::unique_ptr
<Module
>>
6630 BitcodeModule::parseModule(LLVMContext
&Context
) {
6631 return getModuleImpl(Context
, true, false, false);
6632 // TODO: Restore the use-lists to the in-memory state when the bitcode was
6633 // written. We must defer until the Module has been fully materialized.
6636 Expected
<std::unique_ptr
<Module
>> llvm::parseBitcodeFile(MemoryBufferRef Buffer
,
6637 LLVMContext
&Context
) {
6638 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6640 return BM
.takeError();
6642 return BM
->parseModule(Context
);
6645 Expected
<std::string
> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer
) {
6646 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6648 return StreamOrErr
.takeError();
6650 return readTriple(*StreamOrErr
);
6653 Expected
<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer
) {
6654 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6656 return StreamOrErr
.takeError();
6658 return hasObjCCategory(*StreamOrErr
);
6661 Expected
<std::string
> llvm::getBitcodeProducerString(MemoryBufferRef Buffer
) {
6662 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6664 return StreamOrErr
.takeError();
6666 return readIdentificationCode(*StreamOrErr
);
6669 Error
llvm::readModuleSummaryIndex(MemoryBufferRef Buffer
,
6670 ModuleSummaryIndex
&CombinedIndex
,
6671 uint64_t ModuleId
) {
6672 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6674 return BM
.takeError();
6676 return BM
->readSummary(CombinedIndex
, BM
->getModuleIdentifier(), ModuleId
);
6679 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
6680 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer
) {
6681 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6683 return BM
.takeError();
6685 return BM
->getSummary();
6688 Expected
<BitcodeLTOInfo
> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer
) {
6689 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6691 return BM
.takeError();
6693 return BM
->getLTOInfo();
6696 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
6697 llvm::getModuleSummaryIndexForFile(StringRef Path
,
6698 bool IgnoreEmptyThinLTOIndexFile
) {
6699 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
6700 MemoryBuffer::getFileOrSTDIN(Path
);
6702 return errorCodeToError(FileOrErr
.getError());
6703 if (IgnoreEmptyThinLTOIndexFile
&& !(*FileOrErr
)->getBufferSize())
6705 return getModuleSummaryIndex(**FileOrErr
);