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/Bitcode/BitcodeCommon.h"
24 #include "llvm/Bitcode/LLVMBitCodes.h"
25 #include "llvm/Bitstream/BitstreamReader.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/AutoUpgrade.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/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 Result
.append(Record
.begin() + Idx
, Record
.end());
160 // Strip all the TBAA attachment for the module.
161 static void stripTBAA(Module
*M
) {
163 if (F
.isMaterializable())
165 for (auto &I
: instructions(F
))
166 I
.setMetadata(LLVMContext::MD_tbaa
, nullptr);
170 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
171 /// "epoch" encoded in the bitcode, and return the producer name if any.
172 static Expected
<std::string
> readIdentificationBlock(BitstreamCursor
&Stream
) {
173 if (Error Err
= Stream
.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID
))
174 return std::move(Err
);
176 // Read all the records.
177 SmallVector
<uint64_t, 64> Record
;
179 std::string ProducerIdentification
;
182 BitstreamEntry Entry
;
183 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
186 return Res
.takeError();
188 switch (Entry
.Kind
) {
190 case BitstreamEntry::Error
:
191 return error("Malformed block");
192 case BitstreamEntry::EndBlock
:
193 return ProducerIdentification
;
194 case BitstreamEntry::Record
:
195 // The interesting case.
201 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
203 return MaybeBitCode
.takeError();
204 switch (MaybeBitCode
.get()) {
205 default: // Default behavior: reject
206 return error("Invalid value");
207 case bitc::IDENTIFICATION_CODE_STRING
: // IDENTIFICATION: [strchr x N]
208 convertToString(Record
, 0, ProducerIdentification
);
210 case bitc::IDENTIFICATION_CODE_EPOCH
: { // EPOCH: [epoch#]
211 unsigned epoch
= (unsigned)Record
[0];
212 if (epoch
!= bitc::BITCODE_CURRENT_EPOCH
) {
214 Twine("Incompatible epoch: Bitcode '") + Twine(epoch
) +
215 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH
) + "'");
222 static Expected
<std::string
> readIdentificationCode(BitstreamCursor
&Stream
) {
223 // We expect a number of well-defined blocks, though we don't necessarily
224 // need to understand them all.
226 if (Stream
.AtEndOfStream())
229 BitstreamEntry Entry
;
230 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
231 Entry
= std::move(Res
.get());
233 return Res
.takeError();
235 switch (Entry
.Kind
) {
236 case BitstreamEntry::EndBlock
:
237 case BitstreamEntry::Error
:
238 return error("Malformed block");
240 case BitstreamEntry::SubBlock
:
241 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
)
242 return readIdentificationBlock(Stream
);
244 // Ignore other sub-blocks.
245 if (Error Err
= Stream
.SkipBlock())
246 return std::move(Err
);
248 case BitstreamEntry::Record
:
249 if (Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
252 return Skipped
.takeError();
257 static Expected
<bool> hasObjCCategoryInModule(BitstreamCursor
&Stream
) {
258 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
259 return std::move(Err
);
261 SmallVector
<uint64_t, 64> Record
;
262 // Read all the records for this module.
265 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
267 return MaybeEntry
.takeError();
268 BitstreamEntry Entry
= MaybeEntry
.get();
270 switch (Entry
.Kind
) {
271 case BitstreamEntry::SubBlock
: // Handled for us already.
272 case BitstreamEntry::Error
:
273 return error("Malformed block");
274 case BitstreamEntry::EndBlock
:
276 case BitstreamEntry::Record
:
277 // The interesting case.
282 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
284 return MaybeRecord
.takeError();
285 switch (MaybeRecord
.get()) {
287 break; // Default behavior, ignore unknown content.
288 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
290 if (convertToString(Record
, 0, S
))
291 return error("Invalid record");
292 // Check for the i386 and other (x86_64, ARM) conventions
293 if (S
.find("__DATA,__objc_catlist") != std::string::npos
||
294 S
.find("__OBJC,__category") != std::string::npos
)
301 llvm_unreachable("Exit infinite loop");
304 static Expected
<bool> hasObjCCategory(BitstreamCursor
&Stream
) {
305 // We expect a number of well-defined blocks, though we don't necessarily
306 // need to understand them all.
308 BitstreamEntry Entry
;
309 if (Expected
<BitstreamEntry
> Res
= Stream
.advance())
310 Entry
= std::move(Res
.get());
312 return Res
.takeError();
314 switch (Entry
.Kind
) {
315 case BitstreamEntry::Error
:
316 return error("Malformed block");
317 case BitstreamEntry::EndBlock
:
320 case BitstreamEntry::SubBlock
:
321 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
322 return hasObjCCategoryInModule(Stream
);
324 // Ignore other sub-blocks.
325 if (Error Err
= Stream
.SkipBlock())
326 return std::move(Err
);
329 case BitstreamEntry::Record
:
330 if (Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
333 return Skipped
.takeError();
338 static Expected
<std::string
> readModuleTriple(BitstreamCursor
&Stream
) {
339 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
340 return std::move(Err
);
342 SmallVector
<uint64_t, 64> Record
;
346 // Read all the records for this module.
348 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
350 return MaybeEntry
.takeError();
351 BitstreamEntry Entry
= MaybeEntry
.get();
353 switch (Entry
.Kind
) {
354 case BitstreamEntry::SubBlock
: // Handled for us already.
355 case BitstreamEntry::Error
:
356 return error("Malformed block");
357 case BitstreamEntry::EndBlock
:
359 case BitstreamEntry::Record
:
360 // The interesting case.
365 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
367 return MaybeRecord
.takeError();
368 switch (MaybeRecord
.get()) {
369 default: break; // Default behavior, ignore unknown content.
370 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
372 if (convertToString(Record
, 0, S
))
373 return error("Invalid record");
380 llvm_unreachable("Exit infinite loop");
383 static Expected
<std::string
> readTriple(BitstreamCursor
&Stream
) {
384 // We expect a number of well-defined blocks, though we don't necessarily
385 // need to understand them all.
387 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advance();
389 return MaybeEntry
.takeError();
390 BitstreamEntry Entry
= MaybeEntry
.get();
392 switch (Entry
.Kind
) {
393 case BitstreamEntry::Error
:
394 return error("Malformed block");
395 case BitstreamEntry::EndBlock
:
398 case BitstreamEntry::SubBlock
:
399 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
)
400 return readModuleTriple(Stream
);
402 // Ignore other sub-blocks.
403 if (Error Err
= Stream
.SkipBlock())
404 return std::move(Err
);
407 case BitstreamEntry::Record
:
408 if (llvm::Expected
<unsigned> Skipped
= Stream
.skipRecord(Entry
.ID
))
411 return Skipped
.takeError();
418 class BitcodeReaderBase
{
420 BitcodeReaderBase(BitstreamCursor Stream
, StringRef Strtab
)
421 : Stream(std::move(Stream
)), Strtab(Strtab
) {
422 this->Stream
.setBlockInfo(&BlockInfo
);
425 BitstreamBlockInfo BlockInfo
;
426 BitstreamCursor Stream
;
429 /// In version 2 of the bitcode we store names of global values and comdats in
430 /// a string table rather than in the VST.
431 bool UseStrtab
= false;
433 Expected
<unsigned> parseVersionRecord(ArrayRef
<uint64_t> Record
);
435 /// If this module uses a string table, pop the reference to the string table
436 /// and return the referenced string and the rest of the record. Otherwise
437 /// just return the record itself.
438 std::pair
<StringRef
, ArrayRef
<uint64_t>>
439 readNameFromStrtab(ArrayRef
<uint64_t> Record
);
441 bool readBlockInfo();
443 // Contains an arbitrary and optional string identifying the bitcode producer
444 std::string ProducerIdentification
;
446 Error
error(const Twine
&Message
);
449 } // end anonymous namespace
451 Error
BitcodeReaderBase::error(const Twine
&Message
) {
452 std::string FullMsg
= Message
.str();
453 if (!ProducerIdentification
.empty())
454 FullMsg
+= " (Producer: '" + ProducerIdentification
+ "' Reader: 'LLVM " +
455 LLVM_VERSION_STRING
"')";
456 return ::error(FullMsg
);
460 BitcodeReaderBase::parseVersionRecord(ArrayRef
<uint64_t> Record
) {
462 return error("Invalid record");
463 unsigned ModuleVersion
= Record
[0];
464 if (ModuleVersion
> 2)
465 return error("Invalid value");
466 UseStrtab
= ModuleVersion
>= 2;
467 return ModuleVersion
;
470 std::pair
<StringRef
, ArrayRef
<uint64_t>>
471 BitcodeReaderBase::readNameFromStrtab(ArrayRef
<uint64_t> Record
) {
474 // Invalid reference. Let the caller complain about the record being empty.
475 if (Record
[0] + Record
[1] > Strtab
.size())
477 return {StringRef(Strtab
.data() + Record
[0], Record
[1]), Record
.slice(2)};
482 class BitcodeReader
: public BitcodeReaderBase
, public GVMaterializer
{
483 LLVMContext
&Context
;
484 Module
*TheModule
= nullptr;
485 // Next offset to start scanning for lazy parsing of function bodies.
486 uint64_t NextUnreadBit
= 0;
487 // Last function offset found in the VST.
488 uint64_t LastFunctionBlockBit
= 0;
489 bool SeenValueSymbolTable
= false;
490 uint64_t VSTOffset
= 0;
492 std::vector
<std::string
> SectionTable
;
493 std::vector
<std::string
> GCTable
;
495 std::vector
<Type
*> TypeList
;
496 DenseMap
<Function
*, FunctionType
*> FunctionTypes
;
497 BitcodeReaderValueList ValueList
;
498 Optional
<MetadataLoader
> MDLoader
;
499 std::vector
<Comdat
*> ComdatList
;
500 SmallVector
<Instruction
*, 64> InstructionList
;
502 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInits
;
503 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>> IndirectSymbolInits
;
504 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixes
;
505 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologues
;
506 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFns
;
508 /// The set of attributes by index. Index zero in the file is for null, and
509 /// is thus not represented here. As such all indices are off by one.
510 std::vector
<AttributeList
> MAttributes
;
512 /// The set of attribute groups.
513 std::map
<unsigned, AttributeList
> MAttributeGroups
;
515 /// While parsing a function body, this is a list of the basic blocks for the
517 std::vector
<BasicBlock
*> FunctionBBs
;
519 // When reading the module header, this list is populated with functions that
520 // have bodies later in the file.
521 std::vector
<Function
*> FunctionsWithBodies
;
523 // When intrinsic functions are encountered which require upgrading they are
524 // stored here with their replacement function.
525 using UpdatedIntrinsicMap
= DenseMap
<Function
*, Function
*>;
526 UpdatedIntrinsicMap UpgradedIntrinsics
;
527 // Intrinsics which were remangled because of types rename
528 UpdatedIntrinsicMap RemangledIntrinsics
;
530 // Several operations happen after the module header has been read, but
531 // before function bodies are processed. This keeps track of whether
532 // we've done this yet.
533 bool SeenFirstFunctionBody
= false;
535 /// When function bodies are initially scanned, this map contains info about
536 /// where to find deferred function body in the stream.
537 DenseMap
<Function
*, uint64_t> DeferredFunctionInfo
;
539 /// When Metadata block is initially scanned when parsing the module, we may
540 /// choose to defer parsing of the metadata. This vector contains info about
541 /// which Metadata blocks are deferred.
542 std::vector
<uint64_t> DeferredMetadataInfo
;
544 /// These are basic blocks forward-referenced by block addresses. They are
545 /// inserted lazily into functions when they're loaded. The basic block ID is
546 /// its index into the vector.
547 DenseMap
<Function
*, std::vector
<BasicBlock
*>> BasicBlockFwdRefs
;
548 std::deque
<Function
*> BasicBlockFwdRefQueue
;
550 /// Indicates that we are using a new encoding for instruction operands where
551 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
552 /// instruction number, for a more compact encoding. Some instruction
553 /// operands are not relative to the instruction ID: basic block numbers, and
554 /// types. Once the old style function blocks have been phased out, we would
555 /// not need this flag.
556 bool UseRelativeIDs
= false;
558 /// True if all functions will be materialized, negating the need to process
559 /// (e.g.) blockaddress forward references.
560 bool WillMaterializeAllForwardRefs
= false;
562 bool StripDebugInfo
= false;
563 TBAAVerifier TBAAVerifyHelper
;
565 std::vector
<std::string
> BundleTags
;
566 SmallVector
<SyncScope::ID
, 8> SSIDs
;
569 BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
570 StringRef ProducerIdentification
, LLVMContext
&Context
);
572 Error
materializeForwardReferencedFunctions();
574 Error
materialize(GlobalValue
*GV
) override
;
575 Error
materializeModule() override
;
576 std::vector
<StructType
*> getIdentifiedStructTypes() const override
;
578 /// Main interface to parsing a bitcode buffer.
579 /// \returns true if an error occurred.
580 Error
parseBitcodeInto(
581 Module
*M
, bool ShouldLazyLoadMetadata
= false, bool IsImporting
= false,
582 DataLayoutCallbackTy DataLayoutCallback
= [](StringRef
) { return None
; });
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 Type
*getTypeByID(unsigned ID
);
598 Value
*getFnValueByID(unsigned ID
, Type
*Ty
) {
599 if (Ty
&& Ty
->isMetadataTy())
600 return MetadataAsValue::get(Ty
->getContext(), getFnMetadataByID(ID
));
601 return ValueList
.getValueFwdRef(ID
, Ty
);
604 Metadata
*getFnMetadataByID(unsigned ID
) {
605 return MDLoader
->getMetadataFwdRefOrLoad(ID
);
608 BasicBlock
*getBasicBlock(unsigned ID
) const {
609 if (ID
>= FunctionBBs
.size()) return nullptr; // Invalid ID
610 return FunctionBBs
[ID
];
613 AttributeList
getAttributes(unsigned i
) const {
614 if (i
-1 < MAttributes
.size())
615 return MAttributes
[i
-1];
616 return AttributeList();
619 /// Read a value/type pair out of the specified record from slot 'Slot'.
620 /// Increment Slot past the number of slots used in the record. Return true on
622 bool getValueTypePair(const SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
623 unsigned InstNum
, Value
*&ResVal
) {
624 if (Slot
== Record
.size()) return true;
625 unsigned ValNo
= (unsigned)Record
[Slot
++];
626 // Adjust the ValNo, if it was encoded relative to the InstNum.
628 ValNo
= InstNum
- ValNo
;
629 if (ValNo
< InstNum
) {
630 // If this is not a forward reference, just return the value we already
632 ResVal
= getFnValueByID(ValNo
, nullptr);
633 return ResVal
== nullptr;
635 if (Slot
== Record
.size())
638 unsigned TypeNo
= (unsigned)Record
[Slot
++];
639 ResVal
= getFnValueByID(ValNo
, getTypeByID(TypeNo
));
640 return ResVal
== nullptr;
643 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
644 /// past the number of slots used by the value in the record. Return true if
645 /// there is an error.
646 bool popValue(const SmallVectorImpl
<uint64_t> &Record
, unsigned &Slot
,
647 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
648 if (getValue(Record
, Slot
, InstNum
, Ty
, ResVal
))
650 // All values currently take a single record slot.
655 /// Like popValue, but does not increment the Slot number.
656 bool getValue(const SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
657 unsigned InstNum
, Type
*Ty
, Value
*&ResVal
) {
658 ResVal
= getValue(Record
, Slot
, InstNum
, Ty
);
659 return ResVal
== nullptr;
662 /// Version of getValue that returns ResVal directly, or 0 if there is an
664 Value
*getValue(const SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
665 unsigned InstNum
, Type
*Ty
) {
666 if (Slot
== Record
.size()) return nullptr;
667 unsigned ValNo
= (unsigned)Record
[Slot
];
668 // Adjust the ValNo, if it was encoded relative to the InstNum.
670 ValNo
= InstNum
- ValNo
;
671 return getFnValueByID(ValNo
, Ty
);
674 /// Like getValue, but decodes signed VBRs.
675 Value
*getValueSigned(const SmallVectorImpl
<uint64_t> &Record
, unsigned Slot
,
676 unsigned InstNum
, Type
*Ty
) {
677 if (Slot
== Record
.size()) return nullptr;
678 unsigned ValNo
= (unsigned)decodeSignRotatedValue(Record
[Slot
]);
679 // Adjust the ValNo, if it was encoded relative to the InstNum.
681 ValNo
= InstNum
- ValNo
;
682 return getFnValueByID(ValNo
, Ty
);
685 /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
686 /// corresponding argument's pointee type. Also upgrades intrinsics that now
687 /// require an elementtype attribute.
688 void propagateAttributeTypes(CallBase
*CB
, ArrayRef
<Type
*> ArgsTys
);
690 /// Converts alignment exponent (i.e. power of two (or zero)) to the
691 /// corresponding alignment to use. If alignment is too large, returns
692 /// a corresponding error code.
693 Error
parseAlignmentValue(uint64_t Exponent
, MaybeAlign
&Alignment
);
694 Error
parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
);
696 uint64_t ResumeBit
, bool ShouldLazyLoadMetadata
= false,
697 DataLayoutCallbackTy DataLayoutCallback
= [](StringRef
) { return None
; });
699 Error
parseComdatRecord(ArrayRef
<uint64_t> Record
);
700 Error
parseGlobalVarRecord(ArrayRef
<uint64_t> Record
);
701 Error
parseFunctionRecord(ArrayRef
<uint64_t> Record
);
702 Error
parseGlobalIndirectSymbolRecord(unsigned BitCode
,
703 ArrayRef
<uint64_t> Record
);
705 Error
parseAttributeBlock();
706 Error
parseAttributeGroupBlock();
707 Error
parseTypeTable();
708 Error
parseTypeTableBody();
709 Error
parseOperandBundleTags();
710 Error
parseSyncScopeNames();
712 Expected
<Value
*> recordValue(SmallVectorImpl
<uint64_t> &Record
,
713 unsigned NameIndex
, Triple
&TT
);
714 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
, Function
*F
,
715 ArrayRef
<uint64_t> Record
);
716 Error
parseValueSymbolTable(uint64_t Offset
= 0);
717 Error
parseGlobalValueSymbolTable();
718 Error
parseConstants();
719 Error
rememberAndSkipFunctionBodies();
720 Error
rememberAndSkipFunctionBody();
721 /// Save the positions of the Metadata blocks and skip parsing the blocks.
722 Error
rememberAndSkipMetadata();
723 Error
typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
);
724 Error
parseFunctionBody(Function
*F
);
725 Error
globalCleanup();
726 Error
resolveGlobalAndIndirectSymbolInits();
727 Error
parseUseLists();
728 Error
findFunctionInStream(
730 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
);
732 SyncScope::ID
getDecodedSyncScopeID(unsigned Val
);
735 /// Class to manage reading and parsing function summary index bitcode
737 class ModuleSummaryIndexBitcodeReader
: public BitcodeReaderBase
{
738 /// The module index built during parsing.
739 ModuleSummaryIndex
&TheIndex
;
741 /// Indicates whether we have encountered a global value summary section
742 /// yet during parsing.
743 bool SeenGlobalValSummary
= false;
745 /// Indicates whether we have already parsed the VST, used for error checking.
746 bool SeenValueSymbolTable
= false;
748 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
749 /// Used to enable on-demand parsing of the VST.
750 uint64_t VSTOffset
= 0;
752 // Map to save ValueId to ValueInfo association that was recorded in the
753 // ValueSymbolTable. It is used after the VST is parsed to convert
754 // call graph edges read from the function summary from referencing
755 // callees by their ValueId to using the ValueInfo instead, which is how
756 // they are recorded in the summary index being built.
757 // We save a GUID which refers to the same global as the ValueInfo, but
758 // ignoring the linkage, i.e. for values other than local linkage they are
760 DenseMap
<unsigned, std::pair
<ValueInfo
, GlobalValue::GUID
>>
761 ValueIdToValueInfoMap
;
763 /// Map populated during module path string table parsing, from the
764 /// module ID to a string reference owned by the index's module
765 /// path string table, used to correlate with combined index
767 DenseMap
<uint64_t, StringRef
> ModuleIdMap
;
769 /// Original source file name recorded in a bitcode record.
770 std::string SourceFileName
;
772 /// The string identifier given to this module by the client, normally the
773 /// path to the bitcode file.
774 StringRef ModulePath
;
776 /// For per-module summary indexes, the unique numerical identifier given to
777 /// this module by the client.
781 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
782 ModuleSummaryIndex
&TheIndex
,
783 StringRef ModulePath
, unsigned ModuleId
);
788 void setValueGUID(uint64_t ValueID
, StringRef ValueName
,
789 GlobalValue::LinkageTypes Linkage
,
790 StringRef SourceFileName
);
791 Error
parseValueSymbolTable(
793 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
);
794 std::vector
<ValueInfo
> makeRefList(ArrayRef
<uint64_t> Record
);
795 std::vector
<FunctionSummary::EdgeTy
> makeCallList(ArrayRef
<uint64_t> Record
,
796 bool IsOldProfileFormat
,
799 Error
parseEntireSummary(unsigned ID
);
800 Error
parseModuleStringTable();
801 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef
<uint64_t> Record
);
802 void parseTypeIdCompatibleVtableInfo(ArrayRef
<uint64_t> Record
, size_t &Slot
,
803 TypeIdCompatibleVtableInfo
&TypeId
);
804 std::vector
<FunctionSummary::ParamAccess
>
805 parseParamAccesses(ArrayRef
<uint64_t> Record
);
807 std::pair
<ValueInfo
, GlobalValue::GUID
>
808 getValueInfoFromValueId(unsigned ValueId
);
810 void addThisModule();
811 ModuleSummaryIndex::ModuleInfo
*getThisModule();
814 } // end anonymous namespace
816 std::error_code
llvm::errorToErrorCodeAndEmitErrors(LLVMContext
&Ctx
,
820 handleAllErrors(std::move(Err
), [&](ErrorInfoBase
&EIB
) {
821 EC
= EIB
.convertToErrorCode();
822 Ctx
.emitError(EIB
.message());
826 return std::error_code();
829 BitcodeReader::BitcodeReader(BitstreamCursor Stream
, StringRef Strtab
,
830 StringRef ProducerIdentification
,
831 LLVMContext
&Context
)
832 : BitcodeReaderBase(std::move(Stream
), Strtab
), Context(Context
),
833 ValueList(Context
, Stream
.SizeInBytes()) {
834 this->ProducerIdentification
= std::string(ProducerIdentification
);
837 Error
BitcodeReader::materializeForwardReferencedFunctions() {
838 if (WillMaterializeAllForwardRefs
)
839 return Error::success();
841 // Prevent recursion.
842 WillMaterializeAllForwardRefs
= true;
844 while (!BasicBlockFwdRefQueue
.empty()) {
845 Function
*F
= BasicBlockFwdRefQueue
.front();
846 BasicBlockFwdRefQueue
.pop_front();
847 assert(F
&& "Expected valid function");
848 if (!BasicBlockFwdRefs
.count(F
))
849 // Already materialized.
852 // Check for a function that isn't materializable to prevent an infinite
853 // loop. When parsing a blockaddress stored in a global variable, there
854 // isn't a trivial way to check if a function will have a body without a
855 // linear search through FunctionsWithBodies, so just check it here.
856 if (!F
->isMaterializable())
857 return error("Never resolved function from blockaddress");
859 // Try to materialize F.
860 if (Error Err
= materialize(F
))
863 assert(BasicBlockFwdRefs
.empty() && "Function missing from queue");
866 WillMaterializeAllForwardRefs
= false;
867 return Error::success();
870 //===----------------------------------------------------------------------===//
871 // Helper functions to implement forward reference resolution, etc.
872 //===----------------------------------------------------------------------===//
874 static bool hasImplicitComdat(size_t Val
) {
878 case 1: // Old WeakAnyLinkage
879 case 4: // Old LinkOnceAnyLinkage
880 case 10: // Old WeakODRLinkage
881 case 11: // Old LinkOnceODRLinkage
886 static GlobalValue::LinkageTypes
getDecodedLinkage(unsigned Val
) {
888 default: // Map unknown/new linkages to external
890 return GlobalValue::ExternalLinkage
;
892 return GlobalValue::AppendingLinkage
;
894 return GlobalValue::InternalLinkage
;
896 return GlobalValue::ExternalLinkage
; // Obsolete DLLImportLinkage
898 return GlobalValue::ExternalLinkage
; // Obsolete DLLExportLinkage
900 return GlobalValue::ExternalWeakLinkage
;
902 return GlobalValue::CommonLinkage
;
904 return GlobalValue::PrivateLinkage
;
906 return GlobalValue::AvailableExternallyLinkage
;
908 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateLinkage
910 return GlobalValue::PrivateLinkage
; // Obsolete LinkerPrivateWeakLinkage
912 return GlobalValue::ExternalLinkage
; // Obsolete LinkOnceODRAutoHideLinkage
913 case 1: // Old value with implicit comdat.
915 return GlobalValue::WeakAnyLinkage
;
916 case 10: // Old value with implicit comdat.
918 return GlobalValue::WeakODRLinkage
;
919 case 4: // Old value with implicit comdat.
921 return GlobalValue::LinkOnceAnyLinkage
;
922 case 11: // Old value with implicit comdat.
924 return GlobalValue::LinkOnceODRLinkage
;
928 static FunctionSummary::FFlags
getDecodedFFlags(uint64_t RawFlags
) {
929 FunctionSummary::FFlags Flags
;
930 Flags
.ReadNone
= RawFlags
& 0x1;
931 Flags
.ReadOnly
= (RawFlags
>> 1) & 0x1;
932 Flags
.NoRecurse
= (RawFlags
>> 2) & 0x1;
933 Flags
.ReturnDoesNotAlias
= (RawFlags
>> 3) & 0x1;
934 Flags
.NoInline
= (RawFlags
>> 4) & 0x1;
935 Flags
.AlwaysInline
= (RawFlags
>> 5) & 0x1;
939 // Decode the flags for GlobalValue in the summary. The bits for each attribute:
941 // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7,
942 // visibility: [8, 10).
943 static GlobalValueSummary::GVFlags
getDecodedGVSummaryFlags(uint64_t RawFlags
,
945 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
946 // like getDecodedLinkage() above. Any future change to the linkage enum and
947 // to getDecodedLinkage() will need to be taken into account here as above.
948 auto Linkage
= GlobalValue::LinkageTypes(RawFlags
& 0xF); // 4 bits
949 auto Visibility
= GlobalValue::VisibilityTypes((RawFlags
>> 8) & 3); // 2 bits
950 RawFlags
= RawFlags
>> 4;
951 bool NotEligibleToImport
= (RawFlags
& 0x1) || Version
< 3;
952 // The Live flag wasn't introduced until version 3. For dead stripping
953 // to work correctly on earlier versions, we must conservatively treat all
955 bool Live
= (RawFlags
& 0x2) || Version
< 3;
956 bool Local
= (RawFlags
& 0x4);
957 bool AutoHide
= (RawFlags
& 0x8);
959 return GlobalValueSummary::GVFlags(Linkage
, Visibility
, NotEligibleToImport
,
960 Live
, Local
, AutoHide
);
963 // Decode the flags for GlobalVariable in the summary
964 static GlobalVarSummary::GVarFlags
getDecodedGVarFlags(uint64_t RawFlags
) {
965 return GlobalVarSummary::GVarFlags(
966 (RawFlags
& 0x1) ? true : false, (RawFlags
& 0x2) ? true : false,
967 (RawFlags
& 0x4) ? true : false,
968 (GlobalObject::VCallVisibility
)(RawFlags
>> 3));
971 static GlobalValue::VisibilityTypes
getDecodedVisibility(unsigned Val
) {
973 default: // Map unknown visibilities to default.
974 case 0: return GlobalValue::DefaultVisibility
;
975 case 1: return GlobalValue::HiddenVisibility
;
976 case 2: return GlobalValue::ProtectedVisibility
;
980 static GlobalValue::DLLStorageClassTypes
981 getDecodedDLLStorageClass(unsigned Val
) {
983 default: // Map unknown values to default.
984 case 0: return GlobalValue::DefaultStorageClass
;
985 case 1: return GlobalValue::DLLImportStorageClass
;
986 case 2: return GlobalValue::DLLExportStorageClass
;
990 static bool getDecodedDSOLocal(unsigned Val
) {
992 default: // Map unknown values to preemptable.
993 case 0: return false;
998 static GlobalVariable::ThreadLocalMode
getDecodedThreadLocalMode(unsigned Val
) {
1000 case 0: return GlobalVariable::NotThreadLocal
;
1001 default: // Map unknown non-zero value to general dynamic.
1002 case 1: return GlobalVariable::GeneralDynamicTLSModel
;
1003 case 2: return GlobalVariable::LocalDynamicTLSModel
;
1004 case 3: return GlobalVariable::InitialExecTLSModel
;
1005 case 4: return GlobalVariable::LocalExecTLSModel
;
1009 static GlobalVariable::UnnamedAddr
getDecodedUnnamedAddrType(unsigned Val
) {
1011 default: // Map unknown to UnnamedAddr::None.
1012 case 0: return GlobalVariable::UnnamedAddr::None
;
1013 case 1: return GlobalVariable::UnnamedAddr::Global
;
1014 case 2: return GlobalVariable::UnnamedAddr::Local
;
1018 static int getDecodedCastOpcode(unsigned Val
) {
1021 case bitc::CAST_TRUNC
: return Instruction::Trunc
;
1022 case bitc::CAST_ZEXT
: return Instruction::ZExt
;
1023 case bitc::CAST_SEXT
: return Instruction::SExt
;
1024 case bitc::CAST_FPTOUI
: return Instruction::FPToUI
;
1025 case bitc::CAST_FPTOSI
: return Instruction::FPToSI
;
1026 case bitc::CAST_UITOFP
: return Instruction::UIToFP
;
1027 case bitc::CAST_SITOFP
: return Instruction::SIToFP
;
1028 case bitc::CAST_FPTRUNC
: return Instruction::FPTrunc
;
1029 case bitc::CAST_FPEXT
: return Instruction::FPExt
;
1030 case bitc::CAST_PTRTOINT
: return Instruction::PtrToInt
;
1031 case bitc::CAST_INTTOPTR
: return Instruction::IntToPtr
;
1032 case bitc::CAST_BITCAST
: return Instruction::BitCast
;
1033 case bitc::CAST_ADDRSPACECAST
: return Instruction::AddrSpaceCast
;
1037 static int getDecodedUnaryOpcode(unsigned Val
, Type
*Ty
) {
1038 bool IsFP
= Ty
->isFPOrFPVectorTy();
1039 // UnOps are only valid for int/fp or vector of int/fp types
1040 if (!IsFP
&& !Ty
->isIntOrIntVectorTy())
1046 case bitc::UNOP_FNEG
:
1047 return IsFP
? Instruction::FNeg
: -1;
1051 static int getDecodedBinaryOpcode(unsigned Val
, Type
*Ty
) {
1052 bool IsFP
= Ty
->isFPOrFPVectorTy();
1053 // BinOps are only valid for int/fp or vector of int/fp types
1054 if (!IsFP
&& !Ty
->isIntOrIntVectorTy())
1060 case bitc::BINOP_ADD
:
1061 return IsFP
? Instruction::FAdd
: Instruction::Add
;
1062 case bitc::BINOP_SUB
:
1063 return IsFP
? Instruction::FSub
: Instruction::Sub
;
1064 case bitc::BINOP_MUL
:
1065 return IsFP
? Instruction::FMul
: Instruction::Mul
;
1066 case bitc::BINOP_UDIV
:
1067 return IsFP
? -1 : Instruction::UDiv
;
1068 case bitc::BINOP_SDIV
:
1069 return IsFP
? Instruction::FDiv
: Instruction::SDiv
;
1070 case bitc::BINOP_UREM
:
1071 return IsFP
? -1 : Instruction::URem
;
1072 case bitc::BINOP_SREM
:
1073 return IsFP
? Instruction::FRem
: Instruction::SRem
;
1074 case bitc::BINOP_SHL
:
1075 return IsFP
? -1 : Instruction::Shl
;
1076 case bitc::BINOP_LSHR
:
1077 return IsFP
? -1 : Instruction::LShr
;
1078 case bitc::BINOP_ASHR
:
1079 return IsFP
? -1 : Instruction::AShr
;
1080 case bitc::BINOP_AND
:
1081 return IsFP
? -1 : Instruction::And
;
1082 case bitc::BINOP_OR
:
1083 return IsFP
? -1 : Instruction::Or
;
1084 case bitc::BINOP_XOR
:
1085 return IsFP
? -1 : Instruction::Xor
;
1089 static AtomicRMWInst::BinOp
getDecodedRMWOperation(unsigned Val
) {
1091 default: return AtomicRMWInst::BAD_BINOP
;
1092 case bitc::RMW_XCHG
: return AtomicRMWInst::Xchg
;
1093 case bitc::RMW_ADD
: return AtomicRMWInst::Add
;
1094 case bitc::RMW_SUB
: return AtomicRMWInst::Sub
;
1095 case bitc::RMW_AND
: return AtomicRMWInst::And
;
1096 case bitc::RMW_NAND
: return AtomicRMWInst::Nand
;
1097 case bitc::RMW_OR
: return AtomicRMWInst::Or
;
1098 case bitc::RMW_XOR
: return AtomicRMWInst::Xor
;
1099 case bitc::RMW_MAX
: return AtomicRMWInst::Max
;
1100 case bitc::RMW_MIN
: return AtomicRMWInst::Min
;
1101 case bitc::RMW_UMAX
: return AtomicRMWInst::UMax
;
1102 case bitc::RMW_UMIN
: return AtomicRMWInst::UMin
;
1103 case bitc::RMW_FADD
: return AtomicRMWInst::FAdd
;
1104 case bitc::RMW_FSUB
: return AtomicRMWInst::FSub
;
1108 static AtomicOrdering
getDecodedOrdering(unsigned Val
) {
1110 case bitc::ORDERING_NOTATOMIC
: return AtomicOrdering::NotAtomic
;
1111 case bitc::ORDERING_UNORDERED
: return AtomicOrdering::Unordered
;
1112 case bitc::ORDERING_MONOTONIC
: return AtomicOrdering::Monotonic
;
1113 case bitc::ORDERING_ACQUIRE
: return AtomicOrdering::Acquire
;
1114 case bitc::ORDERING_RELEASE
: return AtomicOrdering::Release
;
1115 case bitc::ORDERING_ACQREL
: return AtomicOrdering::AcquireRelease
;
1116 default: // Map unknown orderings to sequentially-consistent.
1117 case bitc::ORDERING_SEQCST
: return AtomicOrdering::SequentiallyConsistent
;
1121 static Comdat::SelectionKind
getDecodedComdatSelectionKind(unsigned Val
) {
1123 default: // Map unknown selection kinds to any.
1124 case bitc::COMDAT_SELECTION_KIND_ANY
:
1126 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH
:
1127 return Comdat::ExactMatch
;
1128 case bitc::COMDAT_SELECTION_KIND_LARGEST
:
1129 return Comdat::Largest
;
1130 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES
:
1131 return Comdat::NoDeduplicate
;
1132 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE
:
1133 return Comdat::SameSize
;
1137 static FastMathFlags
getDecodedFastMathFlags(unsigned Val
) {
1139 if (0 != (Val
& bitc::UnsafeAlgebra
))
1141 if (0 != (Val
& bitc::AllowReassoc
))
1142 FMF
.setAllowReassoc();
1143 if (0 != (Val
& bitc::NoNaNs
))
1145 if (0 != (Val
& bitc::NoInfs
))
1147 if (0 != (Val
& bitc::NoSignedZeros
))
1148 FMF
.setNoSignedZeros();
1149 if (0 != (Val
& bitc::AllowReciprocal
))
1150 FMF
.setAllowReciprocal();
1151 if (0 != (Val
& bitc::AllowContract
))
1152 FMF
.setAllowContract(true);
1153 if (0 != (Val
& bitc::ApproxFunc
))
1154 FMF
.setApproxFunc();
1158 static void upgradeDLLImportExportLinkage(GlobalValue
*GV
, unsigned Val
) {
1160 case 5: GV
->setDLLStorageClass(GlobalValue::DLLImportStorageClass
); break;
1161 case 6: GV
->setDLLStorageClass(GlobalValue::DLLExportStorageClass
); break;
1165 Type
*BitcodeReader::getTypeByID(unsigned ID
) {
1166 // The type table size is always specified correctly.
1167 if (ID
>= TypeList
.size())
1170 if (Type
*Ty
= TypeList
[ID
])
1173 // If we have a forward reference, the only possible case is when it is to a
1174 // named struct. Just create a placeholder for now.
1175 return TypeList
[ID
] = createIdentifiedStructType(Context
);
1178 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
,
1180 auto *Ret
= StructType::create(Context
, Name
);
1181 IdentifiedStructTypes
.push_back(Ret
);
1185 StructType
*BitcodeReader::createIdentifiedStructType(LLVMContext
&Context
) {
1186 auto *Ret
= StructType::create(Context
);
1187 IdentifiedStructTypes
.push_back(Ret
);
1191 //===----------------------------------------------------------------------===//
1192 // Functions for parsing blocks from the bitcode file
1193 //===----------------------------------------------------------------------===//
1195 static uint64_t getRawAttributeMask(Attribute::AttrKind Val
) {
1197 case Attribute::EndAttrKinds
:
1198 case Attribute::EmptyKey
:
1199 case Attribute::TombstoneKey
:
1200 llvm_unreachable("Synthetic enumerators which should never get here");
1202 case Attribute::None
: return 0;
1203 case Attribute::ZExt
: return 1 << 0;
1204 case Attribute::SExt
: return 1 << 1;
1205 case Attribute::NoReturn
: return 1 << 2;
1206 case Attribute::InReg
: return 1 << 3;
1207 case Attribute::StructRet
: return 1 << 4;
1208 case Attribute::NoUnwind
: return 1 << 5;
1209 case Attribute::NoAlias
: return 1 << 6;
1210 case Attribute::ByVal
: return 1 << 7;
1211 case Attribute::Nest
: return 1 << 8;
1212 case Attribute::ReadNone
: return 1 << 9;
1213 case Attribute::ReadOnly
: return 1 << 10;
1214 case Attribute::NoInline
: return 1 << 11;
1215 case Attribute::AlwaysInline
: return 1 << 12;
1216 case Attribute::OptimizeForSize
: return 1 << 13;
1217 case Attribute::StackProtect
: return 1 << 14;
1218 case Attribute::StackProtectReq
: return 1 << 15;
1219 case Attribute::Alignment
: return 31 << 16;
1220 case Attribute::NoCapture
: return 1 << 21;
1221 case Attribute::NoRedZone
: return 1 << 22;
1222 case Attribute::NoImplicitFloat
: return 1 << 23;
1223 case Attribute::Naked
: return 1 << 24;
1224 case Attribute::InlineHint
: return 1 << 25;
1225 case Attribute::StackAlignment
: return 7 << 26;
1226 case Attribute::ReturnsTwice
: return 1 << 29;
1227 case Attribute::UWTable
: return 1 << 30;
1228 case Attribute::NonLazyBind
: return 1U << 31;
1229 case Attribute::SanitizeAddress
: return 1ULL << 32;
1230 case Attribute::MinSize
: return 1ULL << 33;
1231 case Attribute::NoDuplicate
: return 1ULL << 34;
1232 case Attribute::StackProtectStrong
: return 1ULL << 35;
1233 case Attribute::SanitizeThread
: return 1ULL << 36;
1234 case Attribute::SanitizeMemory
: return 1ULL << 37;
1235 case Attribute::NoBuiltin
: return 1ULL << 38;
1236 case Attribute::Returned
: return 1ULL << 39;
1237 case Attribute::Cold
: return 1ULL << 40;
1238 case Attribute::Builtin
: return 1ULL << 41;
1239 case Attribute::OptimizeNone
: return 1ULL << 42;
1240 case Attribute::InAlloca
: return 1ULL << 43;
1241 case Attribute::NonNull
: return 1ULL << 44;
1242 case Attribute::JumpTable
: return 1ULL << 45;
1243 case Attribute::Convergent
: return 1ULL << 46;
1244 case Attribute::SafeStack
: return 1ULL << 47;
1245 case Attribute::NoRecurse
: return 1ULL << 48;
1246 case Attribute::InaccessibleMemOnly
: return 1ULL << 49;
1247 case Attribute::InaccessibleMemOrArgMemOnly
: return 1ULL << 50;
1248 case Attribute::SwiftSelf
: return 1ULL << 51;
1249 case Attribute::SwiftError
: return 1ULL << 52;
1250 case Attribute::WriteOnly
: return 1ULL << 53;
1251 case Attribute::Speculatable
: return 1ULL << 54;
1252 case Attribute::StrictFP
: return 1ULL << 55;
1253 case Attribute::SanitizeHWAddress
: return 1ULL << 56;
1254 case Attribute::NoCfCheck
: return 1ULL << 57;
1255 case Attribute::OptForFuzzing
: return 1ULL << 58;
1256 case Attribute::ShadowCallStack
: return 1ULL << 59;
1257 case Attribute::SpeculativeLoadHardening
:
1259 case Attribute::ImmArg
:
1261 case Attribute::WillReturn
:
1263 case Attribute::NoFree
:
1266 // Other attributes are not supported in the raw format,
1267 // as we ran out of space.
1270 llvm_unreachable("Unsupported attribute type");
1273 static void addRawAttributeValue(AttrBuilder
&B
, uint64_t Val
) {
1276 for (Attribute::AttrKind I
= Attribute::None
; I
!= Attribute::EndAttrKinds
;
1277 I
= Attribute::AttrKind(I
+ 1)) {
1278 if (uint64_t A
= (Val
& getRawAttributeMask(I
))) {
1279 if (I
== Attribute::Alignment
)
1280 B
.addAlignmentAttr(1ULL << ((A
>> 16) - 1));
1281 else if (I
== Attribute::StackAlignment
)
1282 B
.addStackAlignmentAttr(1ULL << ((A
>> 26)-1));
1283 else if (Attribute::isTypeAttrKind(I
))
1284 B
.addTypeAttr(I
, nullptr); // Type will be auto-upgraded.
1291 /// This fills an AttrBuilder object with the LLVM attributes that have
1292 /// been decoded from the given integer. This function must stay in sync with
1293 /// 'encodeLLVMAttributesForBitcode'.
1294 static void decodeLLVMAttributesForBitcode(AttrBuilder
&B
,
1295 uint64_t EncodedAttrs
) {
1296 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1297 // the bits above 31 down by 11 bits.
1298 unsigned Alignment
= (EncodedAttrs
& (0xffffULL
<< 16)) >> 16;
1299 assert((!Alignment
|| isPowerOf2_32(Alignment
)) &&
1300 "Alignment must be a power of two.");
1303 B
.addAlignmentAttr(Alignment
);
1304 addRawAttributeValue(B
, ((EncodedAttrs
& (0xfffffULL
<< 32)) >> 11) |
1305 (EncodedAttrs
& 0xffff));
1308 Error
BitcodeReader::parseAttributeBlock() {
1309 if (Error Err
= Stream
.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID
))
1312 if (!MAttributes
.empty())
1313 return error("Invalid multiple blocks");
1315 SmallVector
<uint64_t, 64> Record
;
1317 SmallVector
<AttributeList
, 8> Attrs
;
1319 // Read all the records.
1321 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1323 return MaybeEntry
.takeError();
1324 BitstreamEntry Entry
= MaybeEntry
.get();
1326 switch (Entry
.Kind
) {
1327 case BitstreamEntry::SubBlock
: // Handled for us already.
1328 case BitstreamEntry::Error
:
1329 return error("Malformed block");
1330 case BitstreamEntry::EndBlock
:
1331 return Error::success();
1332 case BitstreamEntry::Record
:
1333 // The interesting case.
1339 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1341 return MaybeRecord
.takeError();
1342 switch (MaybeRecord
.get()) {
1343 default: // Default behavior: ignore.
1345 case bitc::PARAMATTR_CODE_ENTRY_OLD
: // ENTRY: [paramidx0, attr0, ...]
1346 // Deprecated, but still needed to read old bitcode files.
1347 if (Record
.size() & 1)
1348 return error("Invalid record");
1350 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; i
+= 2) {
1352 decodeLLVMAttributesForBitcode(B
, Record
[i
+1]);
1353 Attrs
.push_back(AttributeList::get(Context
, Record
[i
], B
));
1356 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1359 case bitc::PARAMATTR_CODE_ENTRY
: // ENTRY: [attrgrp0, attrgrp1, ...]
1360 for (unsigned i
= 0, e
= Record
.size(); i
!= e
; ++i
)
1361 Attrs
.push_back(MAttributeGroups
[Record
[i
]]);
1363 MAttributes
.push_back(AttributeList::get(Context
, Attrs
));
1370 // Returns Attribute::None on unrecognized codes.
1371 static Attribute::AttrKind
getAttrFromCode(uint64_t Code
) {
1374 return Attribute::None
;
1375 case bitc::ATTR_KIND_ALIGNMENT
:
1376 return Attribute::Alignment
;
1377 case bitc::ATTR_KIND_ALWAYS_INLINE
:
1378 return Attribute::AlwaysInline
;
1379 case bitc::ATTR_KIND_ARGMEMONLY
:
1380 return Attribute::ArgMemOnly
;
1381 case bitc::ATTR_KIND_BUILTIN
:
1382 return Attribute::Builtin
;
1383 case bitc::ATTR_KIND_BY_VAL
:
1384 return Attribute::ByVal
;
1385 case bitc::ATTR_KIND_IN_ALLOCA
:
1386 return Attribute::InAlloca
;
1387 case bitc::ATTR_KIND_COLD
:
1388 return Attribute::Cold
;
1389 case bitc::ATTR_KIND_CONVERGENT
:
1390 return Attribute::Convergent
;
1391 case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION
:
1392 return Attribute::DisableSanitizerInstrumentation
;
1393 case bitc::ATTR_KIND_ELEMENTTYPE
:
1394 return Attribute::ElementType
;
1395 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY
:
1396 return Attribute::InaccessibleMemOnly
;
1397 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY
:
1398 return Attribute::InaccessibleMemOrArgMemOnly
;
1399 case bitc::ATTR_KIND_INLINE_HINT
:
1400 return Attribute::InlineHint
;
1401 case bitc::ATTR_KIND_IN_REG
:
1402 return Attribute::InReg
;
1403 case bitc::ATTR_KIND_JUMP_TABLE
:
1404 return Attribute::JumpTable
;
1405 case bitc::ATTR_KIND_MIN_SIZE
:
1406 return Attribute::MinSize
;
1407 case bitc::ATTR_KIND_NAKED
:
1408 return Attribute::Naked
;
1409 case bitc::ATTR_KIND_NEST
:
1410 return Attribute::Nest
;
1411 case bitc::ATTR_KIND_NO_ALIAS
:
1412 return Attribute::NoAlias
;
1413 case bitc::ATTR_KIND_NO_BUILTIN
:
1414 return Attribute::NoBuiltin
;
1415 case bitc::ATTR_KIND_NO_CALLBACK
:
1416 return Attribute::NoCallback
;
1417 case bitc::ATTR_KIND_NO_CAPTURE
:
1418 return Attribute::NoCapture
;
1419 case bitc::ATTR_KIND_NO_DUPLICATE
:
1420 return Attribute::NoDuplicate
;
1421 case bitc::ATTR_KIND_NOFREE
:
1422 return Attribute::NoFree
;
1423 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT
:
1424 return Attribute::NoImplicitFloat
;
1425 case bitc::ATTR_KIND_NO_INLINE
:
1426 return Attribute::NoInline
;
1427 case bitc::ATTR_KIND_NO_RECURSE
:
1428 return Attribute::NoRecurse
;
1429 case bitc::ATTR_KIND_NO_MERGE
:
1430 return Attribute::NoMerge
;
1431 case bitc::ATTR_KIND_NON_LAZY_BIND
:
1432 return Attribute::NonLazyBind
;
1433 case bitc::ATTR_KIND_NON_NULL
:
1434 return Attribute::NonNull
;
1435 case bitc::ATTR_KIND_DEREFERENCEABLE
:
1436 return Attribute::Dereferenceable
;
1437 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL
:
1438 return Attribute::DereferenceableOrNull
;
1439 case bitc::ATTR_KIND_ALLOC_SIZE
:
1440 return Attribute::AllocSize
;
1441 case bitc::ATTR_KIND_NO_RED_ZONE
:
1442 return Attribute::NoRedZone
;
1443 case bitc::ATTR_KIND_NO_RETURN
:
1444 return Attribute::NoReturn
;
1445 case bitc::ATTR_KIND_NOSYNC
:
1446 return Attribute::NoSync
;
1447 case bitc::ATTR_KIND_NOCF_CHECK
:
1448 return Attribute::NoCfCheck
;
1449 case bitc::ATTR_KIND_NO_PROFILE
:
1450 return Attribute::NoProfile
;
1451 case bitc::ATTR_KIND_NO_UNWIND
:
1452 return Attribute::NoUnwind
;
1453 case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE
:
1454 return Attribute::NoSanitizeCoverage
;
1455 case bitc::ATTR_KIND_NULL_POINTER_IS_VALID
:
1456 return Attribute::NullPointerIsValid
;
1457 case bitc::ATTR_KIND_OPT_FOR_FUZZING
:
1458 return Attribute::OptForFuzzing
;
1459 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE
:
1460 return Attribute::OptimizeForSize
;
1461 case bitc::ATTR_KIND_OPTIMIZE_NONE
:
1462 return Attribute::OptimizeNone
;
1463 case bitc::ATTR_KIND_READ_NONE
:
1464 return Attribute::ReadNone
;
1465 case bitc::ATTR_KIND_READ_ONLY
:
1466 return Attribute::ReadOnly
;
1467 case bitc::ATTR_KIND_RETURNED
:
1468 return Attribute::Returned
;
1469 case bitc::ATTR_KIND_RETURNS_TWICE
:
1470 return Attribute::ReturnsTwice
;
1471 case bitc::ATTR_KIND_S_EXT
:
1472 return Attribute::SExt
;
1473 case bitc::ATTR_KIND_SPECULATABLE
:
1474 return Attribute::Speculatable
;
1475 case bitc::ATTR_KIND_STACK_ALIGNMENT
:
1476 return Attribute::StackAlignment
;
1477 case bitc::ATTR_KIND_STACK_PROTECT
:
1478 return Attribute::StackProtect
;
1479 case bitc::ATTR_KIND_STACK_PROTECT_REQ
:
1480 return Attribute::StackProtectReq
;
1481 case bitc::ATTR_KIND_STACK_PROTECT_STRONG
:
1482 return Attribute::StackProtectStrong
;
1483 case bitc::ATTR_KIND_SAFESTACK
:
1484 return Attribute::SafeStack
;
1485 case bitc::ATTR_KIND_SHADOWCALLSTACK
:
1486 return Attribute::ShadowCallStack
;
1487 case bitc::ATTR_KIND_STRICT_FP
:
1488 return Attribute::StrictFP
;
1489 case bitc::ATTR_KIND_STRUCT_RET
:
1490 return Attribute::StructRet
;
1491 case bitc::ATTR_KIND_SANITIZE_ADDRESS
:
1492 return Attribute::SanitizeAddress
;
1493 case bitc::ATTR_KIND_SANITIZE_HWADDRESS
:
1494 return Attribute::SanitizeHWAddress
;
1495 case bitc::ATTR_KIND_SANITIZE_THREAD
:
1496 return Attribute::SanitizeThread
;
1497 case bitc::ATTR_KIND_SANITIZE_MEMORY
:
1498 return Attribute::SanitizeMemory
;
1499 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING
:
1500 return Attribute::SpeculativeLoadHardening
;
1501 case bitc::ATTR_KIND_SWIFT_ERROR
:
1502 return Attribute::SwiftError
;
1503 case bitc::ATTR_KIND_SWIFT_SELF
:
1504 return Attribute::SwiftSelf
;
1505 case bitc::ATTR_KIND_SWIFT_ASYNC
:
1506 return Attribute::SwiftAsync
;
1507 case bitc::ATTR_KIND_UW_TABLE
:
1508 return Attribute::UWTable
;
1509 case bitc::ATTR_KIND_VSCALE_RANGE
:
1510 return Attribute::VScaleRange
;
1511 case bitc::ATTR_KIND_WILLRETURN
:
1512 return Attribute::WillReturn
;
1513 case bitc::ATTR_KIND_WRITEONLY
:
1514 return Attribute::WriteOnly
;
1515 case bitc::ATTR_KIND_Z_EXT
:
1516 return Attribute::ZExt
;
1517 case bitc::ATTR_KIND_IMMARG
:
1518 return Attribute::ImmArg
;
1519 case bitc::ATTR_KIND_SANITIZE_MEMTAG
:
1520 return Attribute::SanitizeMemTag
;
1521 case bitc::ATTR_KIND_PREALLOCATED
:
1522 return Attribute::Preallocated
;
1523 case bitc::ATTR_KIND_NOUNDEF
:
1524 return Attribute::NoUndef
;
1525 case bitc::ATTR_KIND_BYREF
:
1526 return Attribute::ByRef
;
1527 case bitc::ATTR_KIND_MUSTPROGRESS
:
1528 return Attribute::MustProgress
;
1529 case bitc::ATTR_KIND_HOT
:
1530 return Attribute::Hot
;
1534 Error
BitcodeReader::parseAlignmentValue(uint64_t Exponent
,
1535 MaybeAlign
&Alignment
) {
1536 // Note: Alignment in bitcode files is incremented by 1, so that zero
1537 // can be used for default alignment.
1538 if (Exponent
> Value::MaxAlignmentExponent
+ 1)
1539 return error("Invalid alignment value");
1540 Alignment
= decodeMaybeAlign(Exponent
);
1541 return Error::success();
1544 Error
BitcodeReader::parseAttrKind(uint64_t Code
, Attribute::AttrKind
*Kind
) {
1545 *Kind
= getAttrFromCode(Code
);
1546 if (*Kind
== Attribute::None
)
1547 return error("Unknown attribute kind (" + Twine(Code
) + ")");
1548 return Error::success();
1551 Error
BitcodeReader::parseAttributeGroupBlock() {
1552 if (Error Err
= Stream
.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID
))
1555 if (!MAttributeGroups
.empty())
1556 return error("Invalid multiple blocks");
1558 SmallVector
<uint64_t, 64> Record
;
1560 // Read all the records.
1562 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1564 return MaybeEntry
.takeError();
1565 BitstreamEntry Entry
= MaybeEntry
.get();
1567 switch (Entry
.Kind
) {
1568 case BitstreamEntry::SubBlock
: // Handled for us already.
1569 case BitstreamEntry::Error
:
1570 return error("Malformed block");
1571 case BitstreamEntry::EndBlock
:
1572 return Error::success();
1573 case BitstreamEntry::Record
:
1574 // The interesting case.
1580 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1582 return MaybeRecord
.takeError();
1583 switch (MaybeRecord
.get()) {
1584 default: // Default behavior: ignore.
1586 case bitc::PARAMATTR_GRP_CODE_ENTRY
: { // ENTRY: [grpid, idx, a0, a1, ...]
1587 if (Record
.size() < 3)
1588 return error("Invalid record");
1590 uint64_t GrpID
= Record
[0];
1591 uint64_t Idx
= Record
[1]; // Index of the object this attribute refers to.
1594 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1595 if (Record
[i
] == 0) { // Enum attribute
1596 Attribute::AttrKind Kind
;
1597 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1600 // Upgrade old-style byval attribute to one with a type, even if it's
1601 // nullptr. We will have to insert the real type when we associate
1602 // this AttributeList with a function.
1603 if (Kind
== Attribute::ByVal
)
1604 B
.addByValAttr(nullptr);
1605 else if (Kind
== Attribute::StructRet
)
1606 B
.addStructRetAttr(nullptr);
1607 else if (Kind
== Attribute::InAlloca
)
1608 B
.addInAllocaAttr(nullptr);
1609 else if (Attribute::isEnumAttrKind(Kind
))
1610 B
.addAttribute(Kind
);
1612 return error("Not an enum attribute");
1613 } else if (Record
[i
] == 1) { // Integer attribute
1614 Attribute::AttrKind Kind
;
1615 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1617 if (!Attribute::isIntAttrKind(Kind
))
1618 return error("Not an int attribute");
1619 if (Kind
== Attribute::Alignment
)
1620 B
.addAlignmentAttr(Record
[++i
]);
1621 else if (Kind
== Attribute::StackAlignment
)
1622 B
.addStackAlignmentAttr(Record
[++i
]);
1623 else if (Kind
== Attribute::Dereferenceable
)
1624 B
.addDereferenceableAttr(Record
[++i
]);
1625 else if (Kind
== Attribute::DereferenceableOrNull
)
1626 B
.addDereferenceableOrNullAttr(Record
[++i
]);
1627 else if (Kind
== Attribute::AllocSize
)
1628 B
.addAllocSizeAttrFromRawRepr(Record
[++i
]);
1629 else if (Kind
== Attribute::VScaleRange
)
1630 B
.addVScaleRangeAttrFromRawRepr(Record
[++i
]);
1631 } else if (Record
[i
] == 3 || Record
[i
] == 4) { // String attribute
1632 bool HasValue
= (Record
[i
++] == 4);
1633 SmallString
<64> KindStr
;
1634 SmallString
<64> ValStr
;
1636 while (Record
[i
] != 0 && i
!= e
)
1637 KindStr
+= Record
[i
++];
1638 assert(Record
[i
] == 0 && "Kind string not null terminated");
1641 // Has a value associated with it.
1642 ++i
; // Skip the '0' that terminates the "kind" string.
1643 while (Record
[i
] != 0 && i
!= e
)
1644 ValStr
+= Record
[i
++];
1645 assert(Record
[i
] == 0 && "Value string not null terminated");
1648 B
.addAttribute(KindStr
.str(), ValStr
.str());
1650 assert((Record
[i
] == 5 || Record
[i
] == 6) &&
1651 "Invalid attribute group entry");
1652 bool HasType
= Record
[i
] == 6;
1653 Attribute::AttrKind Kind
;
1654 if (Error Err
= parseAttrKind(Record
[++i
], &Kind
))
1656 if (!Attribute::isTypeAttrKind(Kind
))
1657 return error("Not a type attribute");
1659 B
.addTypeAttr(Kind
, HasType
? getTypeByID(Record
[++i
]) : nullptr);
1663 UpgradeAttributes(B
);
1664 MAttributeGroups
[GrpID
] = AttributeList::get(Context
, Idx
, B
);
1671 Error
BitcodeReader::parseTypeTable() {
1672 if (Error Err
= Stream
.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW
))
1675 return parseTypeTableBody();
1678 Error
BitcodeReader::parseTypeTableBody() {
1679 if (!TypeList
.empty())
1680 return error("Invalid multiple blocks");
1682 SmallVector
<uint64_t, 64> Record
;
1683 unsigned NumRecords
= 0;
1685 SmallString
<64> TypeName
;
1687 // Read all the records for this type table.
1689 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1691 return MaybeEntry
.takeError();
1692 BitstreamEntry Entry
= MaybeEntry
.get();
1694 switch (Entry
.Kind
) {
1695 case BitstreamEntry::SubBlock
: // Handled for us already.
1696 case BitstreamEntry::Error
:
1697 return error("Malformed block");
1698 case BitstreamEntry::EndBlock
:
1699 if (NumRecords
!= TypeList
.size())
1700 return error("Malformed block");
1701 return Error::success();
1702 case BitstreamEntry::Record
:
1703 // The interesting case.
1709 Type
*ResultTy
= nullptr;
1710 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1712 return MaybeRecord
.takeError();
1713 switch (MaybeRecord
.get()) {
1715 return error("Invalid value");
1716 case bitc::TYPE_CODE_NUMENTRY
: // TYPE_CODE_NUMENTRY: [numentries]
1717 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1718 // type list. This allows us to reserve space.
1720 return error("Invalid record");
1721 TypeList
.resize(Record
[0]);
1723 case bitc::TYPE_CODE_VOID
: // VOID
1724 ResultTy
= Type::getVoidTy(Context
);
1726 case bitc::TYPE_CODE_HALF
: // HALF
1727 ResultTy
= Type::getHalfTy(Context
);
1729 case bitc::TYPE_CODE_BFLOAT
: // BFLOAT
1730 ResultTy
= Type::getBFloatTy(Context
);
1732 case bitc::TYPE_CODE_FLOAT
: // FLOAT
1733 ResultTy
= Type::getFloatTy(Context
);
1735 case bitc::TYPE_CODE_DOUBLE
: // DOUBLE
1736 ResultTy
= Type::getDoubleTy(Context
);
1738 case bitc::TYPE_CODE_X86_FP80
: // X86_FP80
1739 ResultTy
= Type::getX86_FP80Ty(Context
);
1741 case bitc::TYPE_CODE_FP128
: // FP128
1742 ResultTy
= Type::getFP128Ty(Context
);
1744 case bitc::TYPE_CODE_PPC_FP128
: // PPC_FP128
1745 ResultTy
= Type::getPPC_FP128Ty(Context
);
1747 case bitc::TYPE_CODE_LABEL
: // LABEL
1748 ResultTy
= Type::getLabelTy(Context
);
1750 case bitc::TYPE_CODE_METADATA
: // METADATA
1751 ResultTy
= Type::getMetadataTy(Context
);
1753 case bitc::TYPE_CODE_X86_MMX
: // X86_MMX
1754 ResultTy
= Type::getX86_MMXTy(Context
);
1756 case bitc::TYPE_CODE_X86_AMX
: // X86_AMX
1757 ResultTy
= Type::getX86_AMXTy(Context
);
1759 case bitc::TYPE_CODE_TOKEN
: // TOKEN
1760 ResultTy
= Type::getTokenTy(Context
);
1762 case bitc::TYPE_CODE_INTEGER
: { // INTEGER: [width]
1764 return error("Invalid record");
1766 uint64_t NumBits
= Record
[0];
1767 if (NumBits
< IntegerType::MIN_INT_BITS
||
1768 NumBits
> IntegerType::MAX_INT_BITS
)
1769 return error("Bitwidth for integer type out of range");
1770 ResultTy
= IntegerType::get(Context
, NumBits
);
1773 case bitc::TYPE_CODE_POINTER
: { // POINTER: [pointee type] or
1774 // [pointee type, address space]
1776 return error("Invalid record");
1777 unsigned AddressSpace
= 0;
1778 if (Record
.size() == 2)
1779 AddressSpace
= Record
[1];
1780 ResultTy
= getTypeByID(Record
[0]);
1782 !PointerType::isValidElementType(ResultTy
))
1783 return error("Invalid type");
1784 ResultTy
= PointerType::get(ResultTy
, AddressSpace
);
1787 case bitc::TYPE_CODE_OPAQUE_POINTER
: { // OPAQUE_POINTER: [addrspace]
1788 if (Record
.size() != 1)
1789 return error("Invalid record");
1790 unsigned AddressSpace
= Record
[0];
1791 ResultTy
= PointerType::get(Context
, AddressSpace
);
1794 case bitc::TYPE_CODE_FUNCTION_OLD
: {
1795 // Deprecated, but still needed to read old bitcode files.
1796 // FUNCTION: [vararg, attrid, retty, paramty x N]
1797 if (Record
.size() < 3)
1798 return error("Invalid record");
1799 SmallVector
<Type
*, 8> ArgTys
;
1800 for (unsigned i
= 3, e
= Record
.size(); i
!= e
; ++i
) {
1801 if (Type
*T
= getTypeByID(Record
[i
]))
1802 ArgTys
.push_back(T
);
1807 ResultTy
= getTypeByID(Record
[2]);
1808 if (!ResultTy
|| ArgTys
.size() < Record
.size()-3)
1809 return error("Invalid type");
1811 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1814 case bitc::TYPE_CODE_FUNCTION
: {
1815 // FUNCTION: [vararg, retty, paramty x N]
1816 if (Record
.size() < 2)
1817 return error("Invalid record");
1818 SmallVector
<Type
*, 8> ArgTys
;
1819 for (unsigned i
= 2, e
= Record
.size(); i
!= e
; ++i
) {
1820 if (Type
*T
= getTypeByID(Record
[i
])) {
1821 if (!FunctionType::isValidArgumentType(T
))
1822 return error("Invalid function argument type");
1823 ArgTys
.push_back(T
);
1829 ResultTy
= getTypeByID(Record
[1]);
1830 if (!ResultTy
|| ArgTys
.size() < Record
.size()-2)
1831 return error("Invalid type");
1833 ResultTy
= FunctionType::get(ResultTy
, ArgTys
, Record
[0]);
1836 case bitc::TYPE_CODE_STRUCT_ANON
: { // STRUCT: [ispacked, eltty x N]
1838 return error("Invalid record");
1839 SmallVector
<Type
*, 8> EltTys
;
1840 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1841 if (Type
*T
= getTypeByID(Record
[i
]))
1842 EltTys
.push_back(T
);
1846 if (EltTys
.size() != Record
.size()-1)
1847 return error("Invalid type");
1848 ResultTy
= StructType::get(Context
, EltTys
, Record
[0]);
1851 case bitc::TYPE_CODE_STRUCT_NAME
: // STRUCT_NAME: [strchr x N]
1852 if (convertToString(Record
, 0, TypeName
))
1853 return error("Invalid record");
1856 case bitc::TYPE_CODE_STRUCT_NAMED
: { // STRUCT: [ispacked, eltty x N]
1858 return error("Invalid record");
1860 if (NumRecords
>= TypeList
.size())
1861 return error("Invalid TYPE table");
1863 // Check to see if this was forward referenced, if so fill in the temp.
1864 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1866 Res
->setName(TypeName
);
1867 TypeList
[NumRecords
] = nullptr;
1868 } else // Otherwise, create a new struct.
1869 Res
= createIdentifiedStructType(Context
, TypeName
);
1872 SmallVector
<Type
*, 8> EltTys
;
1873 for (unsigned i
= 1, e
= Record
.size(); i
!= e
; ++i
) {
1874 if (Type
*T
= getTypeByID(Record
[i
]))
1875 EltTys
.push_back(T
);
1879 if (EltTys
.size() != Record
.size()-1)
1880 return error("Invalid record");
1881 Res
->setBody(EltTys
, Record
[0]);
1885 case bitc::TYPE_CODE_OPAQUE
: { // OPAQUE: []
1886 if (Record
.size() != 1)
1887 return error("Invalid record");
1889 if (NumRecords
>= TypeList
.size())
1890 return error("Invalid TYPE table");
1892 // Check to see if this was forward referenced, if so fill in the temp.
1893 StructType
*Res
= cast_or_null
<StructType
>(TypeList
[NumRecords
]);
1895 Res
->setName(TypeName
);
1896 TypeList
[NumRecords
] = nullptr;
1897 } else // Otherwise, create a new struct with no body.
1898 Res
= createIdentifiedStructType(Context
, TypeName
);
1903 case bitc::TYPE_CODE_ARRAY
: // ARRAY: [numelts, eltty]
1904 if (Record
.size() < 2)
1905 return error("Invalid record");
1906 ResultTy
= getTypeByID(Record
[1]);
1907 if (!ResultTy
|| !ArrayType::isValidElementType(ResultTy
))
1908 return error("Invalid type");
1909 ResultTy
= ArrayType::get(ResultTy
, Record
[0]);
1911 case bitc::TYPE_CODE_VECTOR
: // VECTOR: [numelts, eltty] or
1912 // [numelts, eltty, scalable]
1913 if (Record
.size() < 2)
1914 return error("Invalid record");
1916 return error("Invalid vector length");
1917 ResultTy
= getTypeByID(Record
[1]);
1918 if (!ResultTy
|| !StructType::isValidElementType(ResultTy
))
1919 return error("Invalid type");
1920 bool Scalable
= Record
.size() > 2 ? Record
[2] : false;
1921 ResultTy
= VectorType::get(ResultTy
, Record
[0], Scalable
);
1925 if (NumRecords
>= TypeList
.size())
1926 return error("Invalid TYPE table");
1927 if (TypeList
[NumRecords
])
1929 "Invalid TYPE table: Only named structs can be forward referenced");
1930 assert(ResultTy
&& "Didn't read a type?");
1931 TypeList
[NumRecords
++] = ResultTy
;
1935 Error
BitcodeReader::parseOperandBundleTags() {
1936 if (Error Err
= Stream
.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
))
1939 if (!BundleTags
.empty())
1940 return error("Invalid multiple blocks");
1942 SmallVector
<uint64_t, 64> Record
;
1945 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1947 return MaybeEntry
.takeError();
1948 BitstreamEntry Entry
= MaybeEntry
.get();
1950 switch (Entry
.Kind
) {
1951 case BitstreamEntry::SubBlock
: // Handled for us already.
1952 case BitstreamEntry::Error
:
1953 return error("Malformed block");
1954 case BitstreamEntry::EndBlock
:
1955 return Error::success();
1956 case BitstreamEntry::Record
:
1957 // The interesting case.
1961 // Tags are implicitly mapped to integers by their order.
1963 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
1965 return MaybeRecord
.takeError();
1966 if (MaybeRecord
.get() != bitc::OPERAND_BUNDLE_TAG
)
1967 return error("Invalid record");
1969 // OPERAND_BUNDLE_TAG: [strchr x N]
1970 BundleTags
.emplace_back();
1971 if (convertToString(Record
, 0, BundleTags
.back()))
1972 return error("Invalid record");
1977 Error
BitcodeReader::parseSyncScopeNames() {
1978 if (Error Err
= Stream
.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID
))
1982 return error("Invalid multiple synchronization scope names blocks");
1984 SmallVector
<uint64_t, 64> Record
;
1986 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
1988 return MaybeEntry
.takeError();
1989 BitstreamEntry Entry
= MaybeEntry
.get();
1991 switch (Entry
.Kind
) {
1992 case BitstreamEntry::SubBlock
: // Handled for us already.
1993 case BitstreamEntry::Error
:
1994 return error("Malformed block");
1995 case BitstreamEntry::EndBlock
:
1997 return error("Invalid empty synchronization scope names block");
1998 return Error::success();
1999 case BitstreamEntry::Record
:
2000 // The interesting case.
2004 // Synchronization scope names are implicitly mapped to synchronization
2005 // scope IDs by their order.
2007 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2009 return MaybeRecord
.takeError();
2010 if (MaybeRecord
.get() != bitc::SYNC_SCOPE_NAME
)
2011 return error("Invalid record");
2013 SmallString
<16> SSN
;
2014 if (convertToString(Record
, 0, SSN
))
2015 return error("Invalid record");
2017 SSIDs
.push_back(Context
.getOrInsertSyncScopeID(SSN
));
2022 /// Associate a value with its name from the given index in the provided record.
2023 Expected
<Value
*> BitcodeReader::recordValue(SmallVectorImpl
<uint64_t> &Record
,
2024 unsigned NameIndex
, Triple
&TT
) {
2025 SmallString
<128> ValueName
;
2026 if (convertToString(Record
, NameIndex
, ValueName
))
2027 return error("Invalid record");
2028 unsigned ValueID
= Record
[0];
2029 if (ValueID
>= ValueList
.size() || !ValueList
[ValueID
])
2030 return error("Invalid record");
2031 Value
*V
= ValueList
[ValueID
];
2033 StringRef
NameStr(ValueName
.data(), ValueName
.size());
2034 if (NameStr
.find_first_of(0) != StringRef::npos
)
2035 return error("Invalid value name");
2036 V
->setName(NameStr
);
2037 auto *GO
= dyn_cast
<GlobalObject
>(V
);
2039 if (GO
->getComdat() == reinterpret_cast<Comdat
*>(1)) {
2040 if (TT
.supportsCOMDAT())
2041 GO
->setComdat(TheModule
->getOrInsertComdat(V
->getName()));
2043 GO
->setComdat(nullptr);
2049 /// Helper to note and return the current location, and jump to the given
2051 static Expected
<uint64_t> jumpToValueSymbolTable(uint64_t Offset
,
2052 BitstreamCursor
&Stream
) {
2053 // Save the current parsing location so we can jump back at the end
2055 uint64_t CurrentBit
= Stream
.GetCurrentBitNo();
2056 if (Error JumpFailed
= Stream
.JumpToBit(Offset
* 32))
2057 return std::move(JumpFailed
);
2058 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advance();
2060 return MaybeEntry
.takeError();
2061 assert(MaybeEntry
.get().Kind
== BitstreamEntry::SubBlock
);
2062 assert(MaybeEntry
.get().ID
== bitc::VALUE_SYMTAB_BLOCK_ID
);
2066 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta
,
2068 ArrayRef
<uint64_t> Record
) {
2069 // Note that we subtract 1 here because the offset is relative to one word
2070 // before the start of the identification or module block, which was
2071 // historically always the start of the regular bitcode header.
2072 uint64_t FuncWordOffset
= Record
[1] - 1;
2073 uint64_t FuncBitOffset
= FuncWordOffset
* 32;
2074 DeferredFunctionInfo
[F
] = FuncBitOffset
+ FuncBitcodeOffsetDelta
;
2075 // Set the LastFunctionBlockBit to point to the last function block.
2076 // Later when parsing is resumed after function materialization,
2077 // we can simply skip that last function block.
2078 if (FuncBitOffset
> LastFunctionBlockBit
)
2079 LastFunctionBlockBit
= FuncBitOffset
;
2082 /// Read a new-style GlobalValue symbol table.
2083 Error
BitcodeReader::parseGlobalValueSymbolTable() {
2084 unsigned FuncBitcodeOffsetDelta
=
2085 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
2087 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
2090 SmallVector
<uint64_t, 64> Record
;
2092 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2094 return MaybeEntry
.takeError();
2095 BitstreamEntry Entry
= MaybeEntry
.get();
2097 switch (Entry
.Kind
) {
2098 case BitstreamEntry::SubBlock
:
2099 case BitstreamEntry::Error
:
2100 return error("Malformed block");
2101 case BitstreamEntry::EndBlock
:
2102 return Error::success();
2103 case BitstreamEntry::Record
:
2108 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2110 return MaybeRecord
.takeError();
2111 switch (MaybeRecord
.get()) {
2112 case bitc::VST_CODE_FNENTRY
: // [valueid, offset]
2113 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
,
2114 cast
<Function
>(ValueList
[Record
[0]]), Record
);
2120 /// Parse the value symbol table at either the current parsing location or
2121 /// at the given bit offset if provided.
2122 Error
BitcodeReader::parseValueSymbolTable(uint64_t Offset
) {
2123 uint64_t CurrentBit
;
2124 // Pass in the Offset to distinguish between calling for the module-level
2125 // VST (where we want to jump to the VST offset) and the function-level
2126 // VST (where we don't).
2128 Expected
<uint64_t> MaybeCurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
2129 if (!MaybeCurrentBit
)
2130 return MaybeCurrentBit
.takeError();
2131 CurrentBit
= MaybeCurrentBit
.get();
2132 // If this module uses a string table, read this as a module-level VST.
2134 if (Error Err
= parseGlobalValueSymbolTable())
2136 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
2138 return Error::success();
2140 // Otherwise, the VST will be in a similar format to a function-level VST,
2141 // and will contain symbol names.
2144 // Compute the delta between the bitcode indices in the VST (the word offset
2145 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2146 // expected by the lazy reader. The reader's EnterSubBlock expects to have
2147 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2148 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2149 // just before entering the VST subblock because: 1) the EnterSubBlock
2150 // changes the AbbrevID width; 2) the VST block is nested within the same
2151 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2152 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2153 // jump to the FUNCTION_BLOCK using this offset later, we don't want
2154 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2155 unsigned FuncBitcodeOffsetDelta
=
2156 Stream
.getAbbrevIDWidth() + bitc::BlockIDWidth
;
2158 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
2161 SmallVector
<uint64_t, 64> Record
;
2163 Triple
TT(TheModule
->getTargetTriple());
2165 // Read all the records for this value table.
2166 SmallString
<128> ValueName
;
2169 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2171 return MaybeEntry
.takeError();
2172 BitstreamEntry Entry
= MaybeEntry
.get();
2174 switch (Entry
.Kind
) {
2175 case BitstreamEntry::SubBlock
: // Handled for us already.
2176 case BitstreamEntry::Error
:
2177 return error("Malformed block");
2178 case BitstreamEntry::EndBlock
:
2180 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
2182 return Error::success();
2183 case BitstreamEntry::Record
:
2184 // The interesting case.
2190 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2192 return MaybeRecord
.takeError();
2193 switch (MaybeRecord
.get()) {
2194 default: // Default behavior: unknown type.
2196 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
2197 Expected
<Value
*> ValOrErr
= recordValue(Record
, 1, TT
);
2198 if (Error Err
= ValOrErr
.takeError())
2203 case bitc::VST_CODE_FNENTRY
: {
2204 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2205 Expected
<Value
*> ValOrErr
= recordValue(Record
, 2, TT
);
2206 if (Error Err
= ValOrErr
.takeError())
2208 Value
*V
= ValOrErr
.get();
2210 // Ignore function offsets emitted for aliases of functions in older
2211 // versions of LLVM.
2212 if (auto *F
= dyn_cast
<Function
>(V
))
2213 setDeferredFunctionInfo(FuncBitcodeOffsetDelta
, F
, Record
);
2216 case bitc::VST_CODE_BBENTRY
: {
2217 if (convertToString(Record
, 1, ValueName
))
2218 return error("Invalid record");
2219 BasicBlock
*BB
= getBasicBlock(Record
[0]);
2221 return error("Invalid record");
2223 BB
->setName(StringRef(ValueName
.data(), ValueName
.size()));
2231 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2233 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V
) {
2238 // There is no such thing as -0 with integers. "-0" really means MININT.
2242 /// Resolve all of the initializers for global values and aliases that we can.
2243 Error
BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2244 std::vector
<std::pair
<GlobalVariable
*, unsigned>> GlobalInitWorklist
;
2245 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>
2246 IndirectSymbolInitWorklist
;
2247 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrefixWorklist
;
2248 std::vector
<std::pair
<Function
*, unsigned>> FunctionPrologueWorklist
;
2249 std::vector
<std::pair
<Function
*, unsigned>> FunctionPersonalityFnWorklist
;
2251 GlobalInitWorklist
.swap(GlobalInits
);
2252 IndirectSymbolInitWorklist
.swap(IndirectSymbolInits
);
2253 FunctionPrefixWorklist
.swap(FunctionPrefixes
);
2254 FunctionPrologueWorklist
.swap(FunctionPrologues
);
2255 FunctionPersonalityFnWorklist
.swap(FunctionPersonalityFns
);
2257 while (!GlobalInitWorklist
.empty()) {
2258 unsigned ValID
= GlobalInitWorklist
.back().second
;
2259 if (ValID
>= ValueList
.size()) {
2260 // Not ready to resolve this yet, it requires something later in the file.
2261 GlobalInits
.push_back(GlobalInitWorklist
.back());
2263 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2264 GlobalInitWorklist
.back().first
->setInitializer(C
);
2266 return error("Expected a constant");
2268 GlobalInitWorklist
.pop_back();
2271 while (!IndirectSymbolInitWorklist
.empty()) {
2272 unsigned ValID
= IndirectSymbolInitWorklist
.back().second
;
2273 if (ValID
>= ValueList
.size()) {
2274 IndirectSymbolInits
.push_back(IndirectSymbolInitWorklist
.back());
2276 Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]);
2278 return error("Expected a constant");
2279 GlobalIndirectSymbol
*GIS
= IndirectSymbolInitWorklist
.back().first
;
2280 if (isa
<GlobalAlias
>(GIS
) && C
->getType() != GIS
->getType())
2281 return error("Alias and aliasee types don't match");
2282 GIS
->setIndirectSymbol(C
);
2284 IndirectSymbolInitWorklist
.pop_back();
2287 while (!FunctionPrefixWorklist
.empty()) {
2288 unsigned ValID
= FunctionPrefixWorklist
.back().second
;
2289 if (ValID
>= ValueList
.size()) {
2290 FunctionPrefixes
.push_back(FunctionPrefixWorklist
.back());
2292 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2293 FunctionPrefixWorklist
.back().first
->setPrefixData(C
);
2295 return error("Expected a constant");
2297 FunctionPrefixWorklist
.pop_back();
2300 while (!FunctionPrologueWorklist
.empty()) {
2301 unsigned ValID
= FunctionPrologueWorklist
.back().second
;
2302 if (ValID
>= ValueList
.size()) {
2303 FunctionPrologues
.push_back(FunctionPrologueWorklist
.back());
2305 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2306 FunctionPrologueWorklist
.back().first
->setPrologueData(C
);
2308 return error("Expected a constant");
2310 FunctionPrologueWorklist
.pop_back();
2313 while (!FunctionPersonalityFnWorklist
.empty()) {
2314 unsigned ValID
= FunctionPersonalityFnWorklist
.back().second
;
2315 if (ValID
>= ValueList
.size()) {
2316 FunctionPersonalityFns
.push_back(FunctionPersonalityFnWorklist
.back());
2318 if (Constant
*C
= dyn_cast_or_null
<Constant
>(ValueList
[ValID
]))
2319 FunctionPersonalityFnWorklist
.back().first
->setPersonalityFn(C
);
2321 return error("Expected a constant");
2323 FunctionPersonalityFnWorklist
.pop_back();
2326 return Error::success();
2329 APInt
llvm::readWideAPInt(ArrayRef
<uint64_t> Vals
, unsigned TypeBits
) {
2330 SmallVector
<uint64_t, 8> Words(Vals
.size());
2331 transform(Vals
, Words
.begin(),
2332 BitcodeReader::decodeSignRotatedValue
);
2334 return APInt(TypeBits
, Words
);
2337 Error
BitcodeReader::parseConstants() {
2338 if (Error Err
= Stream
.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID
))
2341 SmallVector
<uint64_t, 64> Record
;
2343 // Read all the records for this value table.
2344 Type
*CurTy
= Type::getInt32Ty(Context
);
2345 unsigned NextCstNo
= ValueList
.size();
2347 struct DelayedShufTy
{
2355 std::vector
<DelayedShufTy
> DelayedShuffles
;
2357 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2359 return MaybeEntry
.takeError();
2360 BitstreamEntry Entry
= MaybeEntry
.get();
2362 switch (Entry
.Kind
) {
2363 case BitstreamEntry::SubBlock
: // Handled for us already.
2364 case BitstreamEntry::Error
:
2365 return error("Malformed block");
2366 case BitstreamEntry::EndBlock
:
2367 // Once all the constants have been read, go through and resolve forward
2370 // We have to treat shuffles specially because they don't have three
2371 // operands anymore. We need to convert the shuffle mask into an array,
2372 // and we can't convert a forward reference.
2373 for (auto &DelayedShuffle
: DelayedShuffles
) {
2374 VectorType
*OpTy
= DelayedShuffle
.OpTy
;
2375 VectorType
*RTy
= DelayedShuffle
.RTy
;
2376 uint64_t Op0Idx
= DelayedShuffle
.Op0Idx
;
2377 uint64_t Op1Idx
= DelayedShuffle
.Op1Idx
;
2378 uint64_t Op2Idx
= DelayedShuffle
.Op2Idx
;
2379 uint64_t CstNo
= DelayedShuffle
.CstNo
;
2380 Constant
*Op0
= ValueList
.getConstantFwdRef(Op0Idx
, OpTy
);
2381 Constant
*Op1
= ValueList
.getConstantFwdRef(Op1Idx
, OpTy
);
2383 VectorType::get(Type::getInt32Ty(Context
), RTy
->getElementCount());
2384 Constant
*Op2
= ValueList
.getConstantFwdRef(Op2Idx
, ShufTy
);
2385 if (!ShuffleVectorInst::isValidOperands(Op0
, Op1
, Op2
))
2386 return error("Invalid shufflevector operands");
2387 SmallVector
<int, 16> Mask
;
2388 ShuffleVectorInst::getShuffleMask(Op2
, Mask
);
2389 Value
*V
= ConstantExpr::getShuffleVector(Op0
, Op1
, Mask
);
2390 ValueList
.assignValue(V
, CstNo
);
2393 if (NextCstNo
!= ValueList
.size())
2394 return error("Invalid constant reference");
2396 ValueList
.resolveConstantForwardRefs();
2397 return Error::success();
2398 case BitstreamEntry::Record
:
2399 // The interesting case.
2405 Type
*VoidType
= Type::getVoidTy(Context
);
2407 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
2409 return MaybeBitCode
.takeError();
2410 switch (unsigned BitCode
= MaybeBitCode
.get()) {
2411 default: // Default behavior: unknown constant
2412 case bitc::CST_CODE_UNDEF
: // UNDEF
2413 V
= UndefValue::get(CurTy
);
2415 case bitc::CST_CODE_POISON
: // POISON
2416 V
= PoisonValue::get(CurTy
);
2418 case bitc::CST_CODE_SETTYPE
: // SETTYPE: [typeid]
2420 return error("Invalid record");
2421 if (Record
[0] >= TypeList
.size() || !TypeList
[Record
[0]])
2422 return error("Invalid record");
2423 if (TypeList
[Record
[0]] == VoidType
)
2424 return error("Invalid constant type");
2425 CurTy
= TypeList
[Record
[0]];
2426 continue; // Skip the ValueList manipulation.
2427 case bitc::CST_CODE_NULL
: // NULL
2428 if (CurTy
->isVoidTy() || CurTy
->isFunctionTy() || CurTy
->isLabelTy())
2429 return error("Invalid type for a constant null value");
2430 V
= Constant::getNullValue(CurTy
);
2432 case bitc::CST_CODE_INTEGER
: // INTEGER: [intval]
2433 if (!CurTy
->isIntegerTy() || Record
.empty())
2434 return error("Invalid record");
2435 V
= ConstantInt::get(CurTy
, decodeSignRotatedValue(Record
[0]));
2437 case bitc::CST_CODE_WIDE_INTEGER
: {// WIDE_INTEGER: [n x intval]
2438 if (!CurTy
->isIntegerTy() || Record
.empty())
2439 return error("Invalid record");
2442 readWideAPInt(Record
, cast
<IntegerType
>(CurTy
)->getBitWidth());
2443 V
= ConstantInt::get(Context
, VInt
);
2447 case bitc::CST_CODE_FLOAT
: { // FLOAT: [fpval]
2449 return error("Invalid record");
2450 if (CurTy
->isHalfTy())
2451 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEhalf(),
2452 APInt(16, (uint16_t)Record
[0])));
2453 else if (CurTy
->isBFloatTy())
2454 V
= ConstantFP::get(Context
, APFloat(APFloat::BFloat(),
2455 APInt(16, (uint32_t)Record
[0])));
2456 else if (CurTy
->isFloatTy())
2457 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEsingle(),
2458 APInt(32, (uint32_t)Record
[0])));
2459 else if (CurTy
->isDoubleTy())
2460 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEdouble(),
2461 APInt(64, Record
[0])));
2462 else if (CurTy
->isX86_FP80Ty()) {
2463 // Bits are not stored the same way as a normal i80 APInt, compensate.
2464 uint64_t Rearrange
[2];
2465 Rearrange
[0] = (Record
[1] & 0xffffLL
) | (Record
[0] << 16);
2466 Rearrange
[1] = Record
[0] >> 48;
2467 V
= ConstantFP::get(Context
, APFloat(APFloat::x87DoubleExtended(),
2468 APInt(80, Rearrange
)));
2469 } else if (CurTy
->isFP128Ty())
2470 V
= ConstantFP::get(Context
, APFloat(APFloat::IEEEquad(),
2471 APInt(128, Record
)));
2472 else if (CurTy
->isPPC_FP128Ty())
2473 V
= ConstantFP::get(Context
, APFloat(APFloat::PPCDoubleDouble(),
2474 APInt(128, Record
)));
2476 V
= UndefValue::get(CurTy
);
2480 case bitc::CST_CODE_AGGREGATE
: {// AGGREGATE: [n x value number]
2482 return error("Invalid record");
2484 unsigned Size
= Record
.size();
2485 SmallVector
<Constant
*, 16> Elts
;
2487 if (StructType
*STy
= dyn_cast
<StructType
>(CurTy
)) {
2488 for (unsigned i
= 0; i
!= Size
; ++i
)
2489 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
],
2490 STy
->getElementType(i
)));
2491 V
= ConstantStruct::get(STy
, Elts
);
2492 } else if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(CurTy
)) {
2493 Type
*EltTy
= ATy
->getElementType();
2494 for (unsigned i
= 0; i
!= Size
; ++i
)
2495 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2496 V
= ConstantArray::get(ATy
, Elts
);
2497 } else if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
)) {
2498 Type
*EltTy
= VTy
->getElementType();
2499 for (unsigned i
= 0; i
!= Size
; ++i
)
2500 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[i
], EltTy
));
2501 V
= ConstantVector::get(Elts
);
2503 V
= UndefValue::get(CurTy
);
2507 case bitc::CST_CODE_STRING
: // STRING: [values]
2508 case bitc::CST_CODE_CSTRING
: { // CSTRING: [values]
2510 return error("Invalid record");
2512 SmallString
<16> Elts(Record
.begin(), Record
.end());
2513 V
= ConstantDataArray::getString(Context
, Elts
,
2514 BitCode
== bitc::CST_CODE_CSTRING
);
2517 case bitc::CST_CODE_DATA
: {// DATA: [n x value]
2519 return error("Invalid record");
2522 if (auto *Array
= dyn_cast
<ArrayType
>(CurTy
))
2523 EltTy
= Array
->getElementType();
2525 EltTy
= cast
<VectorType
>(CurTy
)->getElementType();
2526 if (EltTy
->isIntegerTy(8)) {
2527 SmallVector
<uint8_t, 16> Elts(Record
.begin(), Record
.end());
2528 if (isa
<VectorType
>(CurTy
))
2529 V
= ConstantDataVector::get(Context
, Elts
);
2531 V
= ConstantDataArray::get(Context
, Elts
);
2532 } else if (EltTy
->isIntegerTy(16)) {
2533 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2534 if (isa
<VectorType
>(CurTy
))
2535 V
= ConstantDataVector::get(Context
, Elts
);
2537 V
= ConstantDataArray::get(Context
, Elts
);
2538 } else if (EltTy
->isIntegerTy(32)) {
2539 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2540 if (isa
<VectorType
>(CurTy
))
2541 V
= ConstantDataVector::get(Context
, Elts
);
2543 V
= ConstantDataArray::get(Context
, Elts
);
2544 } else if (EltTy
->isIntegerTy(64)) {
2545 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2546 if (isa
<VectorType
>(CurTy
))
2547 V
= ConstantDataVector::get(Context
, Elts
);
2549 V
= ConstantDataArray::get(Context
, Elts
);
2550 } else if (EltTy
->isHalfTy()) {
2551 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2552 if (isa
<VectorType
>(CurTy
))
2553 V
= ConstantDataVector::getFP(EltTy
, Elts
);
2555 V
= ConstantDataArray::getFP(EltTy
, Elts
);
2556 } else if (EltTy
->isBFloatTy()) {
2557 SmallVector
<uint16_t, 16> Elts(Record
.begin(), Record
.end());
2558 if (isa
<VectorType
>(CurTy
))
2559 V
= ConstantDataVector::getFP(EltTy
, Elts
);
2561 V
= ConstantDataArray::getFP(EltTy
, Elts
);
2562 } else if (EltTy
->isFloatTy()) {
2563 SmallVector
<uint32_t, 16> Elts(Record
.begin(), Record
.end());
2564 if (isa
<VectorType
>(CurTy
))
2565 V
= ConstantDataVector::getFP(EltTy
, Elts
);
2567 V
= ConstantDataArray::getFP(EltTy
, Elts
);
2568 } else if (EltTy
->isDoubleTy()) {
2569 SmallVector
<uint64_t, 16> Elts(Record
.begin(), Record
.end());
2570 if (isa
<VectorType
>(CurTy
))
2571 V
= ConstantDataVector::getFP(EltTy
, Elts
);
2573 V
= ConstantDataArray::getFP(EltTy
, Elts
);
2575 return error("Invalid type for value");
2579 case bitc::CST_CODE_CE_UNOP
: { // CE_UNOP: [opcode, opval]
2580 if (Record
.size() < 2)
2581 return error("Invalid record");
2582 int Opc
= getDecodedUnaryOpcode(Record
[0], CurTy
);
2584 V
= UndefValue::get(CurTy
); // Unknown unop.
2586 Constant
*LHS
= ValueList
.getConstantFwdRef(Record
[1], CurTy
);
2588 V
= ConstantExpr::get(Opc
, LHS
, Flags
);
2592 case bitc::CST_CODE_CE_BINOP
: { // CE_BINOP: [opcode, opval, opval]
2593 if (Record
.size() < 3)
2594 return error("Invalid record");
2595 int Opc
= getDecodedBinaryOpcode(Record
[0], CurTy
);
2597 V
= UndefValue::get(CurTy
); // Unknown binop.
2599 Constant
*LHS
= ValueList
.getConstantFwdRef(Record
[1], CurTy
);
2600 Constant
*RHS
= ValueList
.getConstantFwdRef(Record
[2], CurTy
);
2602 if (Record
.size() >= 4) {
2603 if (Opc
== Instruction::Add
||
2604 Opc
== Instruction::Sub
||
2605 Opc
== Instruction::Mul
||
2606 Opc
== Instruction::Shl
) {
2607 if (Record
[3] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
2608 Flags
|= OverflowingBinaryOperator::NoSignedWrap
;
2609 if (Record
[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
2610 Flags
|= OverflowingBinaryOperator::NoUnsignedWrap
;
2611 } else if (Opc
== Instruction::SDiv
||
2612 Opc
== Instruction::UDiv
||
2613 Opc
== Instruction::LShr
||
2614 Opc
== Instruction::AShr
) {
2615 if (Record
[3] & (1 << bitc::PEO_EXACT
))
2616 Flags
|= SDivOperator::IsExact
;
2619 V
= ConstantExpr::get(Opc
, LHS
, RHS
, Flags
);
2623 case bitc::CST_CODE_CE_CAST
: { // CE_CAST: [opcode, opty, opval]
2624 if (Record
.size() < 3)
2625 return error("Invalid record");
2626 int Opc
= getDecodedCastOpcode(Record
[0]);
2628 V
= UndefValue::get(CurTy
); // Unknown cast.
2630 Type
*OpTy
= getTypeByID(Record
[1]);
2632 return error("Invalid record");
2633 Constant
*Op
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2634 V
= UpgradeBitCastExpr(Opc
, Op
, CurTy
);
2635 if (!V
) V
= ConstantExpr::getCast(Opc
, Op
, CurTy
);
2639 case bitc::CST_CODE_CE_INBOUNDS_GEP
: // [ty, n x operands]
2640 case bitc::CST_CODE_CE_GEP
: // [ty, n x operands]
2641 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
: { // [ty, flags, n x
2644 Type
*PointeeType
= nullptr;
2645 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
||
2647 PointeeType
= getTypeByID(Record
[OpNum
++]);
2649 bool InBounds
= false;
2650 Optional
<unsigned> InRangeIndex
;
2651 if (BitCode
== bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX
) {
2652 uint64_t Op
= Record
[OpNum
++];
2654 InRangeIndex
= Op
>> 1;
2655 } else if (BitCode
== bitc::CST_CODE_CE_INBOUNDS_GEP
)
2658 SmallVector
<Constant
*, 16> Elts
;
2659 Type
*Elt0FullTy
= nullptr;
2660 while (OpNum
!= Record
.size()) {
2662 Elt0FullTy
= getTypeByID(Record
[OpNum
]);
2663 Type
*ElTy
= getTypeByID(Record
[OpNum
++]);
2665 return error("Invalid record");
2666 Elts
.push_back(ValueList
.getConstantFwdRef(Record
[OpNum
++], ElTy
));
2669 if (Elts
.size() < 1)
2670 return error("Invalid gep with no operands");
2672 PointerType
*OrigPtrTy
= cast
<PointerType
>(Elt0FullTy
->getScalarType());
2674 PointeeType
= OrigPtrTy
->getElementType();
2675 else if (!OrigPtrTy
->isOpaqueOrPointeeTypeMatches(PointeeType
))
2676 return error("Explicit gep operator type does not match pointee type "
2677 "of pointer operand");
2679 ArrayRef
<Constant
*> Indices(Elts
.begin() + 1, Elts
.end());
2680 V
= ConstantExpr::getGetElementPtr(PointeeType
, Elts
[0], Indices
,
2681 InBounds
, InRangeIndex
);
2684 case bitc::CST_CODE_CE_SELECT
: { // CE_SELECT: [opval#, opval#, opval#]
2685 if (Record
.size() < 3)
2686 return error("Invalid record");
2688 Type
*SelectorTy
= Type::getInt1Ty(Context
);
2690 // The selector might be an i1, an <n x i1>, or a <vscale x n x i1>
2691 // Get the type from the ValueList before getting a forward ref.
2692 if (VectorType
*VTy
= dyn_cast
<VectorType
>(CurTy
))
2693 if (Value
*V
= ValueList
[Record
[0]])
2694 if (SelectorTy
!= V
->getType())
2695 SelectorTy
= VectorType::get(SelectorTy
,
2696 VTy
->getElementCount());
2698 V
= ConstantExpr::getSelect(ValueList
.getConstantFwdRef(Record
[0],
2700 ValueList
.getConstantFwdRef(Record
[1],CurTy
),
2701 ValueList
.getConstantFwdRef(Record
[2],CurTy
));
2704 case bitc::CST_CODE_CE_EXTRACTELT
2705 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2706 if (Record
.size() < 3)
2707 return error("Invalid record");
2709 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2711 return error("Invalid record");
2712 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2713 Constant
*Op1
= nullptr;
2714 if (Record
.size() == 4) {
2715 Type
*IdxTy
= getTypeByID(Record
[2]);
2717 return error("Invalid record");
2718 Op1
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2720 // Deprecated, but still needed to read old bitcode files.
2721 Op1
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2724 return error("Invalid record");
2725 V
= ConstantExpr::getExtractElement(Op0
, Op1
);
2728 case bitc::CST_CODE_CE_INSERTELT
2729 : { // CE_INSERTELT: [opval, opval, opty, opval]
2730 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2731 if (Record
.size() < 3 || !OpTy
)
2732 return error("Invalid record");
2733 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[0], OpTy
);
2734 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[1],
2735 OpTy
->getElementType());
2736 Constant
*Op2
= nullptr;
2737 if (Record
.size() == 4) {
2738 Type
*IdxTy
= getTypeByID(Record
[2]);
2740 return error("Invalid record");
2741 Op2
= ValueList
.getConstantFwdRef(Record
[3], IdxTy
);
2743 // Deprecated, but still needed to read old bitcode files.
2744 Op2
= ValueList
.getConstantFwdRef(Record
[2], Type::getInt32Ty(Context
));
2747 return error("Invalid record");
2748 V
= ConstantExpr::getInsertElement(Op0
, Op1
, Op2
);
2751 case bitc::CST_CODE_CE_SHUFFLEVEC
: { // CE_SHUFFLEVEC: [opval, opval, opval]
2752 VectorType
*OpTy
= dyn_cast
<VectorType
>(CurTy
);
2753 if (Record
.size() < 3 || !OpTy
)
2754 return error("Invalid record");
2755 DelayedShuffles
.push_back(
2756 {OpTy
, OpTy
, Record
[0], Record
[1], Record
[2], NextCstNo
});
2760 case bitc::CST_CODE_CE_SHUFVEC_EX
: { // [opty, opval, opval, opval]
2761 VectorType
*RTy
= dyn_cast
<VectorType
>(CurTy
);
2763 dyn_cast_or_null
<VectorType
>(getTypeByID(Record
[0]));
2764 if (Record
.size() < 4 || !RTy
|| !OpTy
)
2765 return error("Invalid record");
2766 DelayedShuffles
.push_back(
2767 {OpTy
, RTy
, Record
[1], Record
[2], Record
[3], NextCstNo
});
2771 case bitc::CST_CODE_CE_CMP
: { // CE_CMP: [opty, opval, opval, pred]
2772 if (Record
.size() < 4)
2773 return error("Invalid record");
2774 Type
*OpTy
= getTypeByID(Record
[0]);
2776 return error("Invalid record");
2777 Constant
*Op0
= ValueList
.getConstantFwdRef(Record
[1], OpTy
);
2778 Constant
*Op1
= ValueList
.getConstantFwdRef(Record
[2], OpTy
);
2780 if (OpTy
->isFPOrFPVectorTy())
2781 V
= ConstantExpr::getFCmp(Record
[3], Op0
, Op1
);
2783 V
= ConstantExpr::getICmp(Record
[3], Op0
, Op1
);
2786 // This maintains backward compatibility, pre-asm dialect keywords.
2787 // Deprecated, but still needed to read old bitcode files.
2788 case bitc::CST_CODE_INLINEASM_OLD
: {
2789 if (Record
.size() < 2)
2790 return error("Invalid record");
2791 std::string AsmStr
, ConstrStr
;
2792 bool HasSideEffects
= Record
[0] & 1;
2793 bool IsAlignStack
= Record
[0] >> 1;
2794 unsigned AsmStrSize
= Record
[1];
2795 if (2+AsmStrSize
>= Record
.size())
2796 return error("Invalid record");
2797 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2798 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2799 return error("Invalid record");
2801 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2802 AsmStr
+= (char)Record
[2+i
];
2803 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2804 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2805 UpgradeInlineAsmString(&AsmStr
);
2807 cast
<FunctionType
>(cast
<PointerType
>(CurTy
)->getElementType()),
2808 AsmStr
, ConstrStr
, HasSideEffects
, IsAlignStack
);
2811 // This version adds support for the asm dialect keywords (e.g.,
2813 case bitc::CST_CODE_INLINEASM_OLD2
: {
2814 if (Record
.size() < 2)
2815 return error("Invalid record");
2816 std::string AsmStr
, ConstrStr
;
2817 bool HasSideEffects
= Record
[0] & 1;
2818 bool IsAlignStack
= (Record
[0] >> 1) & 1;
2819 unsigned AsmDialect
= Record
[0] >> 2;
2820 unsigned AsmStrSize
= Record
[1];
2821 if (2+AsmStrSize
>= Record
.size())
2822 return error("Invalid record");
2823 unsigned ConstStrSize
= Record
[2+AsmStrSize
];
2824 if (3+AsmStrSize
+ConstStrSize
> Record
.size())
2825 return error("Invalid record");
2827 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2828 AsmStr
+= (char)Record
[2+i
];
2829 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2830 ConstrStr
+= (char)Record
[3+AsmStrSize
+i
];
2831 UpgradeInlineAsmString(&AsmStr
);
2833 cast
<FunctionType
>(cast
<PointerType
>(CurTy
)->getElementType()),
2834 AsmStr
, ConstrStr
, HasSideEffects
, IsAlignStack
,
2835 InlineAsm::AsmDialect(AsmDialect
));
2838 // This version adds support for the unwind keyword.
2839 case bitc::CST_CODE_INLINEASM
: {
2840 if (Record
.size() < 2)
2841 return error("Invalid record");
2842 std::string AsmStr
, ConstrStr
;
2843 bool HasSideEffects
= Record
[0] & 1;
2844 bool IsAlignStack
= (Record
[0] >> 1) & 1;
2845 unsigned AsmDialect
= (Record
[0] >> 2) & 1;
2846 bool CanThrow
= (Record
[0] >> 3) & 1;
2847 unsigned AsmStrSize
= Record
[1];
2848 if (2 + AsmStrSize
>= Record
.size())
2849 return error("Invalid record");
2850 unsigned ConstStrSize
= Record
[2 + AsmStrSize
];
2851 if (3 + AsmStrSize
+ ConstStrSize
> Record
.size())
2852 return error("Invalid record");
2854 for (unsigned i
= 0; i
!= AsmStrSize
; ++i
)
2855 AsmStr
+= (char)Record
[2 + i
];
2856 for (unsigned i
= 0; i
!= ConstStrSize
; ++i
)
2857 ConstrStr
+= (char)Record
[3 + AsmStrSize
+ i
];
2858 UpgradeInlineAsmString(&AsmStr
);
2860 cast
<FunctionType
>(cast
<PointerType
>(CurTy
)->getElementType()),
2861 AsmStr
, ConstrStr
, HasSideEffects
, IsAlignStack
,
2862 InlineAsm::AsmDialect(AsmDialect
), CanThrow
);
2865 case bitc::CST_CODE_BLOCKADDRESS
:{
2866 if (Record
.size() < 3)
2867 return error("Invalid record");
2868 Type
*FnTy
= getTypeByID(Record
[0]);
2870 return error("Invalid record");
2872 dyn_cast_or_null
<Function
>(ValueList
.getConstantFwdRef(Record
[1],FnTy
));
2874 return error("Invalid record");
2876 // If the function is already parsed we can insert the block address right
2879 unsigned BBID
= Record
[2];
2881 // Invalid reference to entry block.
2882 return error("Invalid ID");
2884 Function::iterator BBI
= Fn
->begin(), BBE
= Fn
->end();
2885 for (size_t I
= 0, E
= BBID
; I
!= E
; ++I
) {
2887 return error("Invalid ID");
2892 // Otherwise insert a placeholder and remember it so it can be inserted
2893 // when the function is parsed.
2894 auto &FwdBBs
= BasicBlockFwdRefs
[Fn
];
2896 BasicBlockFwdRefQueue
.push_back(Fn
);
2897 if (FwdBBs
.size() < BBID
+ 1)
2898 FwdBBs
.resize(BBID
+ 1);
2900 FwdBBs
[BBID
] = BasicBlock::Create(Context
);
2903 V
= BlockAddress::get(Fn
, BB
);
2906 case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT
: {
2907 if (Record
.size() < 2)
2908 return error("Invalid record");
2909 Type
*GVTy
= getTypeByID(Record
[0]);
2911 return error("Invalid record");
2912 GlobalValue
*GV
= dyn_cast_or_null
<GlobalValue
>(
2913 ValueList
.getConstantFwdRef(Record
[1], GVTy
));
2915 return error("Invalid record");
2917 V
= DSOLocalEquivalent::get(GV
);
2922 ValueList
.assignValue(V
, NextCstNo
);
2927 Error
BitcodeReader::parseUseLists() {
2928 if (Error Err
= Stream
.EnterSubBlock(bitc::USELIST_BLOCK_ID
))
2931 // Read all the records.
2932 SmallVector
<uint64_t, 64> Record
;
2935 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
2937 return MaybeEntry
.takeError();
2938 BitstreamEntry Entry
= MaybeEntry
.get();
2940 switch (Entry
.Kind
) {
2941 case BitstreamEntry::SubBlock
: // Handled for us already.
2942 case BitstreamEntry::Error
:
2943 return error("Malformed block");
2944 case BitstreamEntry::EndBlock
:
2945 return Error::success();
2946 case BitstreamEntry::Record
:
2947 // The interesting case.
2951 // Read a use list record.
2954 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
2956 return MaybeRecord
.takeError();
2957 switch (MaybeRecord
.get()) {
2958 default: // Default behavior: unknown type.
2960 case bitc::USELIST_CODE_BB
:
2963 case bitc::USELIST_CODE_DEFAULT
: {
2964 unsigned RecordLength
= Record
.size();
2965 if (RecordLength
< 3)
2966 // Records should have at least an ID and two indexes.
2967 return error("Invalid record");
2968 unsigned ID
= Record
.pop_back_val();
2972 assert(ID
< FunctionBBs
.size() && "Basic block not found");
2973 V
= FunctionBBs
[ID
];
2976 unsigned NumUses
= 0;
2977 SmallDenseMap
<const Use
*, unsigned, 16> Order
;
2978 for (const Use
&U
: V
->materialized_uses()) {
2979 if (++NumUses
> Record
.size())
2981 Order
[&U
] = Record
[NumUses
- 1];
2983 if (Order
.size() != Record
.size() || NumUses
> Record
.size())
2984 // Mismatches can happen if the functions are being materialized lazily
2985 // (out-of-order), or a value has been upgraded.
2988 V
->sortUseList([&](const Use
&L
, const Use
&R
) {
2989 return Order
.lookup(&L
) < Order
.lookup(&R
);
2997 /// When we see the block for metadata, remember where it is and then skip it.
2998 /// This lets us lazily deserialize the metadata.
2999 Error
BitcodeReader::rememberAndSkipMetadata() {
3000 // Save the current stream state.
3001 uint64_t CurBit
= Stream
.GetCurrentBitNo();
3002 DeferredMetadataInfo
.push_back(CurBit
);
3004 // Skip over the block for now.
3005 if (Error Err
= Stream
.SkipBlock())
3007 return Error::success();
3010 Error
BitcodeReader::materializeMetadata() {
3011 for (uint64_t BitPos
: DeferredMetadataInfo
) {
3012 // Move the bit stream to the saved position.
3013 if (Error JumpFailed
= Stream
.JumpToBit(BitPos
))
3015 if (Error Err
= MDLoader
->parseModuleMetadata())
3019 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
3020 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
3022 if (!TheModule
->getNamedMetadata("llvm.linker.options")) {
3023 if (Metadata
*Val
= TheModule
->getModuleFlag("Linker Options")) {
3024 NamedMDNode
*LinkerOpts
=
3025 TheModule
->getOrInsertNamedMetadata("llvm.linker.options");
3026 for (const MDOperand
&MDOptions
: cast
<MDNode
>(Val
)->operands())
3027 LinkerOpts
->addOperand(cast
<MDNode
>(MDOptions
));
3031 DeferredMetadataInfo
.clear();
3032 return Error::success();
3035 void BitcodeReader::setStripDebugInfo() { StripDebugInfo
= true; }
3037 /// When we see the block for a function body, remember where it is and then
3038 /// skip it. This lets us lazily deserialize the functions.
3039 Error
BitcodeReader::rememberAndSkipFunctionBody() {
3040 // Get the function we are talking about.
3041 if (FunctionsWithBodies
.empty())
3042 return error("Insufficient function protos");
3044 Function
*Fn
= FunctionsWithBodies
.back();
3045 FunctionsWithBodies
.pop_back();
3047 // Save the current stream state.
3048 uint64_t CurBit
= Stream
.GetCurrentBitNo();
3050 (DeferredFunctionInfo
[Fn
] == 0 || DeferredFunctionInfo
[Fn
] == CurBit
) &&
3051 "Mismatch between VST and scanned function offsets");
3052 DeferredFunctionInfo
[Fn
] = CurBit
;
3054 // Skip over the function block for now.
3055 if (Error Err
= Stream
.SkipBlock())
3057 return Error::success();
3060 Error
BitcodeReader::globalCleanup() {
3061 // Patch the initializers for globals and aliases up.
3062 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
3064 if (!GlobalInits
.empty() || !IndirectSymbolInits
.empty())
3065 return error("Malformed global initializer set");
3067 // Look for intrinsic functions which need to be upgraded at some point
3068 // and functions that need to have their function attributes upgraded.
3069 for (Function
&F
: *TheModule
) {
3070 MDLoader
->upgradeDebugIntrinsics(F
);
3072 if (UpgradeIntrinsicFunction(&F
, NewFn
))
3073 UpgradedIntrinsics
[&F
] = NewFn
;
3074 else if (auto Remangled
= Intrinsic::remangleIntrinsicFunction(&F
))
3075 // Some types could be renamed during loading if several modules are
3076 // loaded in the same LLVMContext (LTO scenario). In this case we should
3077 // remangle intrinsics names as well.
3078 RemangledIntrinsics
[&F
] = Remangled
.getValue();
3079 // Look for functions that rely on old function attribute behavior.
3080 UpgradeFunctionAttributes(F
);
3083 // Look for global variables which need to be renamed.
3084 std::vector
<std::pair
<GlobalVariable
*, GlobalVariable
*>> UpgradedVariables
;
3085 for (GlobalVariable
&GV
: TheModule
->globals())
3086 if (GlobalVariable
*Upgraded
= UpgradeGlobalVariable(&GV
))
3087 UpgradedVariables
.emplace_back(&GV
, Upgraded
);
3088 for (auto &Pair
: UpgradedVariables
) {
3089 Pair
.first
->eraseFromParent();
3090 TheModule
->getGlobalList().push_back(Pair
.second
);
3093 // Force deallocation of memory for these vectors to favor the client that
3094 // want lazy deserialization.
3095 std::vector
<std::pair
<GlobalVariable
*, unsigned>>().swap(GlobalInits
);
3096 std::vector
<std::pair
<GlobalIndirectSymbol
*, unsigned>>().swap(
3097 IndirectSymbolInits
);
3098 return Error::success();
3101 /// Support for lazy parsing of function bodies. This is required if we
3102 /// either have an old bitcode file without a VST forward declaration record,
3103 /// or if we have an anonymous function being materialized, since anonymous
3104 /// functions do not have a name and are therefore not in the VST.
3105 Error
BitcodeReader::rememberAndSkipFunctionBodies() {
3106 if (Error JumpFailed
= Stream
.JumpToBit(NextUnreadBit
))
3109 if (Stream
.AtEndOfStream())
3110 return error("Could not find function in stream");
3112 if (!SeenFirstFunctionBody
)
3113 return error("Trying to materialize functions before seeing function blocks");
3115 // An old bitcode file with the symbol table at the end would have
3116 // finished the parse greedily.
3117 assert(SeenValueSymbolTable
);
3119 SmallVector
<uint64_t, 64> Record
;
3122 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3124 return MaybeEntry
.takeError();
3125 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3127 switch (Entry
.Kind
) {
3129 return error("Expect SubBlock");
3130 case BitstreamEntry::SubBlock
:
3133 return error("Expect function block");
3134 case bitc::FUNCTION_BLOCK_ID
:
3135 if (Error Err
= rememberAndSkipFunctionBody())
3137 NextUnreadBit
= Stream
.GetCurrentBitNo();
3138 return Error::success();
3144 bool BitcodeReaderBase::readBlockInfo() {
3145 Expected
<Optional
<BitstreamBlockInfo
>> MaybeNewBlockInfo
=
3146 Stream
.ReadBlockInfoBlock();
3147 if (!MaybeNewBlockInfo
)
3148 return true; // FIXME Handle the error.
3149 Optional
<BitstreamBlockInfo
> NewBlockInfo
=
3150 std::move(MaybeNewBlockInfo
.get());
3153 BlockInfo
= std::move(*NewBlockInfo
);
3157 Error
BitcodeReader::parseComdatRecord(ArrayRef
<uint64_t> Record
) {
3158 // v1: [selection_kind, name]
3159 // v2: [strtab_offset, strtab_size, selection_kind]
3161 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3164 return error("Invalid record");
3165 Comdat::SelectionKind SK
= getDecodedComdatSelectionKind(Record
[0]);
3166 std::string OldFormatName
;
3168 if (Record
.size() < 2)
3169 return error("Invalid record");
3170 unsigned ComdatNameSize
= Record
[1];
3171 OldFormatName
.reserve(ComdatNameSize
);
3172 for (unsigned i
= 0; i
!= ComdatNameSize
; ++i
)
3173 OldFormatName
+= (char)Record
[2 + i
];
3174 Name
= OldFormatName
;
3176 Comdat
*C
= TheModule
->getOrInsertComdat(Name
);
3177 C
->setSelectionKind(SK
);
3178 ComdatList
.push_back(C
);
3179 return Error::success();
3182 static void inferDSOLocal(GlobalValue
*GV
) {
3183 // infer dso_local from linkage and visibility if it is not encoded.
3184 if (GV
->hasLocalLinkage() ||
3185 (!GV
->hasDefaultVisibility() && !GV
->hasExternalWeakLinkage()))
3186 GV
->setDSOLocal(true);
3189 Error
BitcodeReader::parseGlobalVarRecord(ArrayRef
<uint64_t> Record
) {
3190 // v1: [pointer type, isconst, initid, linkage, alignment, section,
3191 // visibility, threadlocal, unnamed_addr, externally_initialized,
3192 // dllstorageclass, comdat, attributes, preemption specifier,
3193 // partition strtab offset, partition strtab size] (name in VST)
3194 // v2: [strtab_offset, strtab_size, v1]
3196 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3198 if (Record
.size() < 6)
3199 return error("Invalid record");
3200 Type
*Ty
= getTypeByID(Record
[0]);
3202 return error("Invalid record");
3203 bool isConstant
= Record
[1] & 1;
3204 bool explicitType
= Record
[1] & 2;
3205 unsigned AddressSpace
;
3207 AddressSpace
= Record
[1] >> 2;
3209 if (!Ty
->isPointerTy())
3210 return error("Invalid type for value");
3211 AddressSpace
= cast
<PointerType
>(Ty
)->getAddressSpace();
3212 Ty
= cast
<PointerType
>(Ty
)->getElementType();
3215 uint64_t RawLinkage
= Record
[3];
3216 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
3217 MaybeAlign Alignment
;
3218 if (Error Err
= parseAlignmentValue(Record
[4], Alignment
))
3220 std::string Section
;
3222 if (Record
[5] - 1 >= SectionTable
.size())
3223 return error("Invalid ID");
3224 Section
= SectionTable
[Record
[5] - 1];
3226 GlobalValue::VisibilityTypes Visibility
= GlobalValue::DefaultVisibility
;
3227 // Local linkage must have default visibility.
3228 // auto-upgrade `hidden` and `protected` for old bitcode.
3229 if (Record
.size() > 6 && !GlobalValue::isLocalLinkage(Linkage
))
3230 Visibility
= getDecodedVisibility(Record
[6]);
3232 GlobalVariable::ThreadLocalMode TLM
= GlobalVariable::NotThreadLocal
;
3233 if (Record
.size() > 7)
3234 TLM
= getDecodedThreadLocalMode(Record
[7]);
3236 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
3237 if (Record
.size() > 8)
3238 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[8]);
3240 bool ExternallyInitialized
= false;
3241 if (Record
.size() > 9)
3242 ExternallyInitialized
= Record
[9];
3244 GlobalVariable
*NewGV
=
3245 new GlobalVariable(*TheModule
, Ty
, isConstant
, Linkage
, nullptr, Name
,
3246 nullptr, TLM
, AddressSpace
, ExternallyInitialized
);
3247 NewGV
->setAlignment(Alignment
);
3248 if (!Section
.empty())
3249 NewGV
->setSection(Section
);
3250 NewGV
->setVisibility(Visibility
);
3251 NewGV
->setUnnamedAddr(UnnamedAddr
);
3253 if (Record
.size() > 10)
3254 NewGV
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[10]));
3256 upgradeDLLImportExportLinkage(NewGV
, RawLinkage
);
3258 ValueList
.push_back(NewGV
);
3260 // Remember which value to use for the global initializer.
3261 if (unsigned InitID
= Record
[2])
3262 GlobalInits
.push_back(std::make_pair(NewGV
, InitID
- 1));
3264 if (Record
.size() > 11) {
3265 if (unsigned ComdatID
= Record
[11]) {
3266 if (ComdatID
> ComdatList
.size())
3267 return error("Invalid global variable comdat ID");
3268 NewGV
->setComdat(ComdatList
[ComdatID
- 1]);
3270 } else if (hasImplicitComdat(RawLinkage
)) {
3271 NewGV
->setComdat(reinterpret_cast<Comdat
*>(1));
3274 if (Record
.size() > 12) {
3275 auto AS
= getAttributes(Record
[12]).getFnAttrs();
3276 NewGV
->setAttributes(AS
);
3279 if (Record
.size() > 13) {
3280 NewGV
->setDSOLocal(getDecodedDSOLocal(Record
[13]));
3282 inferDSOLocal(NewGV
);
3284 // Check whether we have enough values to read a partition name.
3285 if (Record
.size() > 15)
3286 NewGV
->setPartition(StringRef(Strtab
.data() + Record
[14], Record
[15]));
3288 return Error::success();
3291 Error
BitcodeReader::parseFunctionRecord(ArrayRef
<uint64_t> Record
) {
3292 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3293 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3294 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
3295 // v2: [strtab_offset, strtab_size, v1]
3297 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3299 if (Record
.size() < 8)
3300 return error("Invalid record");
3301 Type
*FTy
= getTypeByID(Record
[0]);
3303 return error("Invalid record");
3304 if (auto *PTy
= dyn_cast
<PointerType
>(FTy
))
3305 FTy
= PTy
->getElementType();
3307 if (!isa
<FunctionType
>(FTy
))
3308 return error("Invalid type for value");
3309 auto CC
= static_cast<CallingConv::ID
>(Record
[1]);
3310 if (CC
& ~CallingConv::MaxID
)
3311 return error("Invalid calling convention ID");
3313 unsigned AddrSpace
= TheModule
->getDataLayout().getProgramAddressSpace();
3314 if (Record
.size() > 16)
3315 AddrSpace
= Record
[16];
3318 Function::Create(cast
<FunctionType
>(FTy
), GlobalValue::ExternalLinkage
,
3319 AddrSpace
, Name
, TheModule
);
3321 assert(Func
->getFunctionType() == FTy
&&
3322 "Incorrect fully specified type provided for function");
3323 FunctionTypes
[Func
] = cast
<FunctionType
>(FTy
);
3325 Func
->setCallingConv(CC
);
3326 bool isProto
= Record
[2];
3327 uint64_t RawLinkage
= Record
[3];
3328 Func
->setLinkage(getDecodedLinkage(RawLinkage
));
3329 Func
->setAttributes(getAttributes(Record
[4]));
3331 // Upgrade any old-style byval or sret without a type by propagating the
3332 // argument's pointee type. There should be no opaque pointers where the byval
3333 // type is implicit.
3334 for (unsigned i
= 0; i
!= Func
->arg_size(); ++i
) {
3335 for (Attribute::AttrKind Kind
: {Attribute::ByVal
, Attribute::StructRet
,
3336 Attribute::InAlloca
}) {
3337 if (!Func
->hasParamAttribute(i
, Kind
))
3340 if (Func
->getParamAttribute(i
, Kind
).getValueAsType())
3343 Func
->removeParamAttr(i
, Kind
);
3345 Type
*PTy
= cast
<FunctionType
>(FTy
)->getParamType(i
);
3346 Type
*PtrEltTy
= cast
<PointerType
>(PTy
)->getElementType();
3349 case Attribute::ByVal
:
3350 NewAttr
= Attribute::getWithByValType(Context
, PtrEltTy
);
3352 case Attribute::StructRet
:
3353 NewAttr
= Attribute::getWithStructRetType(Context
, PtrEltTy
);
3355 case Attribute::InAlloca
:
3356 NewAttr
= Attribute::getWithInAllocaType(Context
, PtrEltTy
);
3359 llvm_unreachable("not an upgraded type attribute");
3362 Func
->addParamAttr(i
, NewAttr
);
3366 MaybeAlign Alignment
;
3367 if (Error Err
= parseAlignmentValue(Record
[5], Alignment
))
3369 Func
->setAlignment(Alignment
);
3371 if (Record
[6] - 1 >= SectionTable
.size())
3372 return error("Invalid ID");
3373 Func
->setSection(SectionTable
[Record
[6] - 1]);
3375 // Local linkage must have default visibility.
3376 // auto-upgrade `hidden` and `protected` for old bitcode.
3377 if (!Func
->hasLocalLinkage())
3378 Func
->setVisibility(getDecodedVisibility(Record
[7]));
3379 if (Record
.size() > 8 && Record
[8]) {
3380 if (Record
[8] - 1 >= GCTable
.size())
3381 return error("Invalid ID");
3382 Func
->setGC(GCTable
[Record
[8] - 1]);
3384 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::UnnamedAddr::None
;
3385 if (Record
.size() > 9)
3386 UnnamedAddr
= getDecodedUnnamedAddrType(Record
[9]);
3387 Func
->setUnnamedAddr(UnnamedAddr
);
3388 if (Record
.size() > 10 && Record
[10] != 0)
3389 FunctionPrologues
.push_back(std::make_pair(Func
, Record
[10] - 1));
3391 if (Record
.size() > 11)
3392 Func
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[11]));
3394 upgradeDLLImportExportLinkage(Func
, RawLinkage
);
3396 if (Record
.size() > 12) {
3397 if (unsigned ComdatID
= Record
[12]) {
3398 if (ComdatID
> ComdatList
.size())
3399 return error("Invalid function comdat ID");
3400 Func
->setComdat(ComdatList
[ComdatID
- 1]);
3402 } else if (hasImplicitComdat(RawLinkage
)) {
3403 Func
->setComdat(reinterpret_cast<Comdat
*>(1));
3406 if (Record
.size() > 13 && Record
[13] != 0)
3407 FunctionPrefixes
.push_back(std::make_pair(Func
, Record
[13] - 1));
3409 if (Record
.size() > 14 && Record
[14] != 0)
3410 FunctionPersonalityFns
.push_back(std::make_pair(Func
, Record
[14] - 1));
3412 if (Record
.size() > 15) {
3413 Func
->setDSOLocal(getDecodedDSOLocal(Record
[15]));
3415 inferDSOLocal(Func
);
3417 // Record[16] is the address space number.
3419 // Check whether we have enough values to read a partition name. Also make
3420 // sure Strtab has enough values.
3421 if (Record
.size() > 18 && Strtab
.data() &&
3422 Record
[17] + Record
[18] <= Strtab
.size()) {
3423 Func
->setPartition(StringRef(Strtab
.data() + Record
[17], Record
[18]));
3426 ValueList
.push_back(Func
);
3428 // If this is a function with a body, remember the prototype we are
3429 // creating now, so that we can match up the body with them later.
3431 Func
->setIsMaterializable(true);
3432 FunctionsWithBodies
.push_back(Func
);
3433 DeferredFunctionInfo
[Func
] = 0;
3435 return Error::success();
3438 Error
BitcodeReader::parseGlobalIndirectSymbolRecord(
3439 unsigned BitCode
, ArrayRef
<uint64_t> Record
) {
3440 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3441 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3442 // dllstorageclass, threadlocal, unnamed_addr,
3443 // preemption specifier] (name in VST)
3444 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3445 // visibility, dllstorageclass, threadlocal, unnamed_addr,
3446 // preemption specifier] (name in VST)
3447 // v2: [strtab_offset, strtab_size, v1]
3449 std::tie(Name
, Record
) = readNameFromStrtab(Record
);
3451 bool NewRecord
= BitCode
!= bitc::MODULE_CODE_ALIAS_OLD
;
3452 if (Record
.size() < (3 + (unsigned)NewRecord
))
3453 return error("Invalid record");
3455 Type
*Ty
= getTypeByID(Record
[OpNum
++]);
3457 return error("Invalid record");
3461 auto *PTy
= dyn_cast
<PointerType
>(Ty
);
3463 return error("Invalid type for value");
3464 Ty
= PTy
->getElementType();
3465 AddrSpace
= PTy
->getAddressSpace();
3467 AddrSpace
= Record
[OpNum
++];
3470 auto Val
= Record
[OpNum
++];
3471 auto Linkage
= Record
[OpNum
++];
3472 GlobalIndirectSymbol
*NewGA
;
3473 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3474 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
)
3475 NewGA
= GlobalAlias::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3478 NewGA
= GlobalIFunc::create(Ty
, AddrSpace
, getDecodedLinkage(Linkage
), Name
,
3479 nullptr, TheModule
);
3481 // Local linkage must have default visibility.
3482 // auto-upgrade `hidden` and `protected` for old bitcode.
3483 if (OpNum
!= Record
.size()) {
3484 auto VisInd
= OpNum
++;
3485 if (!NewGA
->hasLocalLinkage())
3486 NewGA
->setVisibility(getDecodedVisibility(Record
[VisInd
]));
3488 if (BitCode
== bitc::MODULE_CODE_ALIAS
||
3489 BitCode
== bitc::MODULE_CODE_ALIAS_OLD
) {
3490 if (OpNum
!= Record
.size())
3491 NewGA
->setDLLStorageClass(getDecodedDLLStorageClass(Record
[OpNum
++]));
3493 upgradeDLLImportExportLinkage(NewGA
, Linkage
);
3494 if (OpNum
!= Record
.size())
3495 NewGA
->setThreadLocalMode(getDecodedThreadLocalMode(Record
[OpNum
++]));
3496 if (OpNum
!= Record
.size())
3497 NewGA
->setUnnamedAddr(getDecodedUnnamedAddrType(Record
[OpNum
++]));
3499 if (OpNum
!= Record
.size())
3500 NewGA
->setDSOLocal(getDecodedDSOLocal(Record
[OpNum
++]));
3501 inferDSOLocal(NewGA
);
3503 // Check whether we have enough values to read a partition name.
3504 if (OpNum
+ 1 < Record
.size()) {
3505 NewGA
->setPartition(
3506 StringRef(Strtab
.data() + Record
[OpNum
], Record
[OpNum
+ 1]));
3510 ValueList
.push_back(NewGA
);
3511 IndirectSymbolInits
.push_back(std::make_pair(NewGA
, Val
));
3512 return Error::success();
3515 Error
BitcodeReader::parseModule(uint64_t ResumeBit
,
3516 bool ShouldLazyLoadMetadata
,
3517 DataLayoutCallbackTy DataLayoutCallback
) {
3519 if (Error JumpFailed
= Stream
.JumpToBit(ResumeBit
))
3521 } else if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
3524 SmallVector
<uint64_t, 64> Record
;
3526 // Parts of bitcode parsing depend on the datalayout. Make sure we
3527 // finalize the datalayout before we run any of that code.
3528 bool ResolvedDataLayout
= false;
3529 auto ResolveDataLayout
= [&] {
3530 if (ResolvedDataLayout
)
3533 // datalayout and triple can't be parsed after this point.
3534 ResolvedDataLayout
= true;
3536 // Upgrade data layout string.
3537 std::string DL
= llvm::UpgradeDataLayoutString(
3538 TheModule
->getDataLayoutStr(), TheModule
->getTargetTriple());
3539 TheModule
->setDataLayout(DL
);
3541 if (auto LayoutOverride
=
3542 DataLayoutCallback(TheModule
->getTargetTriple()))
3543 TheModule
->setDataLayout(*LayoutOverride
);
3546 // Read all the records for this module.
3548 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3550 return MaybeEntry
.takeError();
3551 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3553 switch (Entry
.Kind
) {
3554 case BitstreamEntry::Error
:
3555 return error("Malformed block");
3556 case BitstreamEntry::EndBlock
:
3557 ResolveDataLayout();
3558 return globalCleanup();
3560 case BitstreamEntry::SubBlock
:
3562 default: // Skip unknown content.
3563 if (Error Err
= Stream
.SkipBlock())
3566 case bitc::BLOCKINFO_BLOCK_ID
:
3567 if (readBlockInfo())
3568 return error("Malformed block");
3570 case bitc::PARAMATTR_BLOCK_ID
:
3571 if (Error Err
= parseAttributeBlock())
3574 case bitc::PARAMATTR_GROUP_BLOCK_ID
:
3575 if (Error Err
= parseAttributeGroupBlock())
3578 case bitc::TYPE_BLOCK_ID_NEW
:
3579 if (Error Err
= parseTypeTable())
3582 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3583 if (!SeenValueSymbolTable
) {
3584 // Either this is an old form VST without function index and an
3585 // associated VST forward declaration record (which would have caused
3586 // the VST to be jumped to and parsed before it was encountered
3587 // normally in the stream), or there were no function blocks to
3588 // trigger an earlier parsing of the VST.
3589 assert(VSTOffset
== 0 || FunctionsWithBodies
.empty());
3590 if (Error Err
= parseValueSymbolTable())
3592 SeenValueSymbolTable
= true;
3594 // We must have had a VST forward declaration record, which caused
3595 // the parser to jump to and parse the VST earlier.
3596 assert(VSTOffset
> 0);
3597 if (Error Err
= Stream
.SkipBlock())
3601 case bitc::CONSTANTS_BLOCK_ID
:
3602 if (Error Err
= parseConstants())
3604 if (Error Err
= resolveGlobalAndIndirectSymbolInits())
3607 case bitc::METADATA_BLOCK_ID
:
3608 if (ShouldLazyLoadMetadata
) {
3609 if (Error Err
= rememberAndSkipMetadata())
3613 assert(DeferredMetadataInfo
.empty() && "Unexpected deferred metadata");
3614 if (Error Err
= MDLoader
->parseModuleMetadata())
3617 case bitc::METADATA_KIND_BLOCK_ID
:
3618 if (Error Err
= MDLoader
->parseMetadataKinds())
3621 case bitc::FUNCTION_BLOCK_ID
:
3622 ResolveDataLayout();
3624 // If this is the first function body we've seen, reverse the
3625 // FunctionsWithBodies list.
3626 if (!SeenFirstFunctionBody
) {
3627 std::reverse(FunctionsWithBodies
.begin(), FunctionsWithBodies
.end());
3628 if (Error Err
= globalCleanup())
3630 SeenFirstFunctionBody
= true;
3633 if (VSTOffset
> 0) {
3634 // If we have a VST forward declaration record, make sure we
3635 // parse the VST now if we haven't already. It is needed to
3636 // set up the DeferredFunctionInfo vector for lazy reading.
3637 if (!SeenValueSymbolTable
) {
3638 if (Error Err
= BitcodeReader::parseValueSymbolTable(VSTOffset
))
3640 SeenValueSymbolTable
= true;
3641 // Fall through so that we record the NextUnreadBit below.
3642 // This is necessary in case we have an anonymous function that
3643 // is later materialized. Since it will not have a VST entry we
3644 // need to fall back to the lazy parse to find its offset.
3646 // If we have a VST forward declaration record, but have already
3647 // parsed the VST (just above, when the first function body was
3648 // encountered here), then we are resuming the parse after
3649 // materializing functions. The ResumeBit points to the
3650 // start of the last function block recorded in the
3651 // DeferredFunctionInfo map. Skip it.
3652 if (Error Err
= Stream
.SkipBlock())
3658 // Support older bitcode files that did not have the function
3659 // index in the VST, nor a VST forward declaration record, as
3660 // well as anonymous functions that do not have VST entries.
3661 // Build the DeferredFunctionInfo vector on the fly.
3662 if (Error Err
= rememberAndSkipFunctionBody())
3665 // Suspend parsing when we reach the function bodies. Subsequent
3666 // materialization calls will resume it when necessary. If the bitcode
3667 // file is old, the symbol table will be at the end instead and will not
3668 // have been seen yet. In this case, just finish the parse now.
3669 if (SeenValueSymbolTable
) {
3670 NextUnreadBit
= Stream
.GetCurrentBitNo();
3671 // After the VST has been parsed, we need to make sure intrinsic name
3672 // are auto-upgraded.
3673 return globalCleanup();
3676 case bitc::USELIST_BLOCK_ID
:
3677 if (Error Err
= parseUseLists())
3680 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID
:
3681 if (Error Err
= parseOperandBundleTags())
3684 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID
:
3685 if (Error Err
= parseSyncScopeNames())
3691 case BitstreamEntry::Record
:
3692 // The interesting case.
3697 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3699 return MaybeBitCode
.takeError();
3700 switch (unsigned BitCode
= MaybeBitCode
.get()) {
3701 default: break; // Default behavior, ignore unknown content.
3702 case bitc::MODULE_CODE_VERSION
: {
3703 Expected
<unsigned> VersionOrErr
= parseVersionRecord(Record
);
3705 return VersionOrErr
.takeError();
3706 UseRelativeIDs
= *VersionOrErr
>= 1;
3709 case bitc::MODULE_CODE_TRIPLE
: { // TRIPLE: [strchr x N]
3710 if (ResolvedDataLayout
)
3711 return error("target triple too late in module");
3713 if (convertToString(Record
, 0, S
))
3714 return error("Invalid record");
3715 TheModule
->setTargetTriple(S
);
3718 case bitc::MODULE_CODE_DATALAYOUT
: { // DATALAYOUT: [strchr x N]
3719 if (ResolvedDataLayout
)
3720 return error("datalayout too late in module");
3722 if (convertToString(Record
, 0, S
))
3723 return error("Invalid record");
3724 TheModule
->setDataLayout(S
);
3727 case bitc::MODULE_CODE_ASM
: { // ASM: [strchr x N]
3729 if (convertToString(Record
, 0, S
))
3730 return error("Invalid record");
3731 TheModule
->setModuleInlineAsm(S
);
3734 case bitc::MODULE_CODE_DEPLIB
: { // DEPLIB: [strchr x N]
3735 // Deprecated, but still needed to read old bitcode files.
3737 if (convertToString(Record
, 0, S
))
3738 return error("Invalid record");
3742 case bitc::MODULE_CODE_SECTIONNAME
: { // SECTIONNAME: [strchr x N]
3744 if (convertToString(Record
, 0, S
))
3745 return error("Invalid record");
3746 SectionTable
.push_back(S
);
3749 case bitc::MODULE_CODE_GCNAME
: { // SECTIONNAME: [strchr x N]
3751 if (convertToString(Record
, 0, S
))
3752 return error("Invalid record");
3753 GCTable
.push_back(S
);
3756 case bitc::MODULE_CODE_COMDAT
:
3757 if (Error Err
= parseComdatRecord(Record
))
3760 case bitc::MODULE_CODE_GLOBALVAR
:
3761 if (Error Err
= parseGlobalVarRecord(Record
))
3764 case bitc::MODULE_CODE_FUNCTION
:
3765 ResolveDataLayout();
3766 if (Error Err
= parseFunctionRecord(Record
))
3769 case bitc::MODULE_CODE_IFUNC
:
3770 case bitc::MODULE_CODE_ALIAS
:
3771 case bitc::MODULE_CODE_ALIAS_OLD
:
3772 if (Error Err
= parseGlobalIndirectSymbolRecord(BitCode
, Record
))
3775 /// MODULE_CODE_VSTOFFSET: [offset]
3776 case bitc::MODULE_CODE_VSTOFFSET
:
3778 return error("Invalid record");
3779 // Note that we subtract 1 here because the offset is relative to one word
3780 // before the start of the identification or module block, which was
3781 // historically always the start of the regular bitcode header.
3782 VSTOffset
= Record
[0] - 1;
3784 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3785 case bitc::MODULE_CODE_SOURCE_FILENAME
:
3786 SmallString
<128> ValueName
;
3787 if (convertToString(Record
, 0, ValueName
))
3788 return error("Invalid record");
3789 TheModule
->setSourceFileName(ValueName
);
3796 Error
BitcodeReader::parseBitcodeInto(Module
*M
, bool ShouldLazyLoadMetadata
,
3798 DataLayoutCallbackTy DataLayoutCallback
) {
3800 MDLoader
= MetadataLoader(Stream
, *M
, ValueList
, IsImporting
,
3801 [&](unsigned ID
) { return getTypeByID(ID
); });
3802 return parseModule(0, ShouldLazyLoadMetadata
, DataLayoutCallback
);
3805 Error
BitcodeReader::typeCheckLoadStoreInst(Type
*ValType
, Type
*PtrType
) {
3806 if (!isa
<PointerType
>(PtrType
))
3807 return error("Load/Store operand is not a pointer type");
3809 if (!cast
<PointerType
>(PtrType
)->isOpaqueOrPointeeTypeMatches(ValType
))
3810 return error("Explicit load/store type does not match pointee "
3811 "type of pointer operand");
3812 if (!PointerType::isLoadableOrStorableType(ValType
))
3813 return error("Cannot load/store from pointer");
3814 return Error::success();
3817 void BitcodeReader::propagateAttributeTypes(CallBase
*CB
,
3818 ArrayRef
<Type
*> ArgsTys
) {
3819 for (unsigned i
= 0; i
!= CB
->arg_size(); ++i
) {
3820 for (Attribute::AttrKind Kind
: {Attribute::ByVal
, Attribute::StructRet
,
3821 Attribute::InAlloca
}) {
3822 if (!CB
->paramHasAttr(i
, Kind
))
3825 CB
->removeParamAttr(i
, Kind
);
3827 Type
*PtrEltTy
= cast
<PointerType
>(ArgsTys
[i
])->getElementType();
3830 case Attribute::ByVal
:
3831 NewAttr
= Attribute::getWithByValType(Context
, PtrEltTy
);
3833 case Attribute::StructRet
:
3834 NewAttr
= Attribute::getWithStructRetType(Context
, PtrEltTy
);
3836 case Attribute::InAlloca
:
3837 NewAttr
= Attribute::getWithInAllocaType(Context
, PtrEltTy
);
3840 llvm_unreachable("not an upgraded type attribute");
3843 CB
->addParamAttr(i
, NewAttr
);
3847 switch (CB
->getIntrinsicID()) {
3848 case Intrinsic::preserve_array_access_index
:
3849 case Intrinsic::preserve_struct_access_index
:
3850 if (!CB
->getAttributes().getParamElementType(0)) {
3851 Type
*ElTy
= cast
<PointerType
>(ArgsTys
[0])->getElementType();
3852 Attribute NewAttr
= Attribute::get(Context
, Attribute::ElementType
, ElTy
);
3853 CB
->addParamAttr(0, NewAttr
);
3861 /// Lazily parse the specified function body block.
3862 Error
BitcodeReader::parseFunctionBody(Function
*F
) {
3863 if (Error Err
= Stream
.EnterSubBlock(bitc::FUNCTION_BLOCK_ID
))
3866 // Unexpected unresolved metadata when parsing function.
3867 if (MDLoader
->hasFwdRefs())
3868 return error("Invalid function metadata: incoming forward references");
3870 InstructionList
.clear();
3871 unsigned ModuleValueListSize
= ValueList
.size();
3872 unsigned ModuleMDLoaderSize
= MDLoader
->size();
3874 // Add all the function arguments to the value table.
3877 FunctionType
*FTy
= FunctionTypes
[F
];
3879 for (Argument
&I
: F
->args()) {
3880 assert(I
.getType() == FTy
->getParamType(ArgNo
++) &&
3881 "Incorrect fully specified type for Function Argument");
3882 ValueList
.push_back(&I
);
3884 unsigned NextValueNo
= ValueList
.size();
3885 BasicBlock
*CurBB
= nullptr;
3886 unsigned CurBBNo
= 0;
3889 auto getLastInstruction
= [&]() -> Instruction
* {
3890 if (CurBB
&& !CurBB
->empty())
3891 return &CurBB
->back();
3892 else if (CurBBNo
&& FunctionBBs
[CurBBNo
- 1] &&
3893 !FunctionBBs
[CurBBNo
- 1]->empty())
3894 return &FunctionBBs
[CurBBNo
- 1]->back();
3898 std::vector
<OperandBundleDef
> OperandBundles
;
3900 // Read all the records.
3901 SmallVector
<uint64_t, 64> Record
;
3904 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
3906 return MaybeEntry
.takeError();
3907 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
3909 switch (Entry
.Kind
) {
3910 case BitstreamEntry::Error
:
3911 return error("Malformed block");
3912 case BitstreamEntry::EndBlock
:
3913 goto OutOfRecordLoop
;
3915 case BitstreamEntry::SubBlock
:
3917 default: // Skip unknown content.
3918 if (Error Err
= Stream
.SkipBlock())
3921 case bitc::CONSTANTS_BLOCK_ID
:
3922 if (Error Err
= parseConstants())
3924 NextValueNo
= ValueList
.size();
3926 case bitc::VALUE_SYMTAB_BLOCK_ID
:
3927 if (Error Err
= parseValueSymbolTable())
3930 case bitc::METADATA_ATTACHMENT_ID
:
3931 if (Error Err
= MDLoader
->parseMetadataAttachment(*F
, InstructionList
))
3934 case bitc::METADATA_BLOCK_ID
:
3935 assert(DeferredMetadataInfo
.empty() &&
3936 "Must read all module-level metadata before function-level");
3937 if (Error Err
= MDLoader
->parseFunctionMetadata())
3940 case bitc::USELIST_BLOCK_ID
:
3941 if (Error Err
= parseUseLists())
3947 case BitstreamEntry::Record
:
3948 // The interesting case.
3954 Instruction
*I
= nullptr;
3955 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
3957 return MaybeBitCode
.takeError();
3958 switch (unsigned BitCode
= MaybeBitCode
.get()) {
3959 default: // Default behavior: reject
3960 return error("Invalid value");
3961 case bitc::FUNC_CODE_DECLAREBLOCKS
: { // DECLAREBLOCKS: [nblocks]
3962 if (Record
.empty() || Record
[0] == 0)
3963 return error("Invalid record");
3964 // Create all the basic blocks for the function.
3965 FunctionBBs
.resize(Record
[0]);
3967 // See if anything took the address of blocks in this function.
3968 auto BBFRI
= BasicBlockFwdRefs
.find(F
);
3969 if (BBFRI
== BasicBlockFwdRefs
.end()) {
3970 for (unsigned i
= 0, e
= FunctionBBs
.size(); i
!= e
; ++i
)
3971 FunctionBBs
[i
] = BasicBlock::Create(Context
, "", F
);
3973 auto &BBRefs
= BBFRI
->second
;
3974 // Check for invalid basic block references.
3975 if (BBRefs
.size() > FunctionBBs
.size())
3976 return error("Invalid ID");
3977 assert(!BBRefs
.empty() && "Unexpected empty array");
3978 assert(!BBRefs
.front() && "Invalid reference to entry block");
3979 for (unsigned I
= 0, E
= FunctionBBs
.size(), RE
= BBRefs
.size(); I
!= E
;
3981 if (I
< RE
&& BBRefs
[I
]) {
3982 BBRefs
[I
]->insertInto(F
);
3983 FunctionBBs
[I
] = BBRefs
[I
];
3985 FunctionBBs
[I
] = BasicBlock::Create(Context
, "", F
);
3988 // Erase from the table.
3989 BasicBlockFwdRefs
.erase(BBFRI
);
3992 CurBB
= FunctionBBs
[0];
3996 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN
: // DEBUG_LOC_AGAIN
3997 // This record indicates that the last instruction is at the same
3998 // location as the previous instruction with a location.
3999 I
= getLastInstruction();
4002 return error("Invalid record");
4003 I
->setDebugLoc(LastLoc
);
4007 case bitc::FUNC_CODE_DEBUG_LOC
: { // DEBUG_LOC: [line, col, scope, ia]
4008 I
= getLastInstruction();
4009 if (!I
|| Record
.size() < 4)
4010 return error("Invalid record");
4012 unsigned Line
= Record
[0], Col
= Record
[1];
4013 unsigned ScopeID
= Record
[2], IAID
= Record
[3];
4014 bool isImplicitCode
= Record
.size() == 5 && Record
[4];
4016 MDNode
*Scope
= nullptr, *IA
= nullptr;
4018 Scope
= dyn_cast_or_null
<MDNode
>(
4019 MDLoader
->getMetadataFwdRefOrLoad(ScopeID
- 1));
4021 return error("Invalid record");
4024 IA
= dyn_cast_or_null
<MDNode
>(
4025 MDLoader
->getMetadataFwdRefOrLoad(IAID
- 1));
4027 return error("Invalid record");
4029 LastLoc
= DILocation::get(Scope
->getContext(), Line
, Col
, Scope
, IA
,
4031 I
->setDebugLoc(LastLoc
);
4035 case bitc::FUNC_CODE_INST_UNOP
: { // UNOP: [opval, ty, opcode]
4038 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
4039 OpNum
+1 > Record
.size())
4040 return error("Invalid record");
4042 int Opc
= getDecodedUnaryOpcode(Record
[OpNum
++], LHS
->getType());
4044 return error("Invalid record");
4045 I
= UnaryOperator::Create((Instruction::UnaryOps
)Opc
, LHS
);
4046 InstructionList
.push_back(I
);
4047 if (OpNum
< Record
.size()) {
4048 if (isa
<FPMathOperator
>(I
)) {
4049 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
4051 I
->setFastMathFlags(FMF
);
4056 case bitc::FUNC_CODE_INST_BINOP
: { // BINOP: [opval, ty, opval, opcode]
4059 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
4060 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
) ||
4061 OpNum
+1 > Record
.size())
4062 return error("Invalid record");
4064 int Opc
= getDecodedBinaryOpcode(Record
[OpNum
++], LHS
->getType());
4066 return error("Invalid record");
4067 I
= BinaryOperator::Create((Instruction::BinaryOps
)Opc
, LHS
, RHS
);
4068 InstructionList
.push_back(I
);
4069 if (OpNum
< Record
.size()) {
4070 if (Opc
== Instruction::Add
||
4071 Opc
== Instruction::Sub
||
4072 Opc
== Instruction::Mul
||
4073 Opc
== Instruction::Shl
) {
4074 if (Record
[OpNum
] & (1 << bitc::OBO_NO_SIGNED_WRAP
))
4075 cast
<BinaryOperator
>(I
)->setHasNoSignedWrap(true);
4076 if (Record
[OpNum
] & (1 << bitc::OBO_NO_UNSIGNED_WRAP
))
4077 cast
<BinaryOperator
>(I
)->setHasNoUnsignedWrap(true);
4078 } else if (Opc
== Instruction::SDiv
||
4079 Opc
== Instruction::UDiv
||
4080 Opc
== Instruction::LShr
||
4081 Opc
== Instruction::AShr
) {
4082 if (Record
[OpNum
] & (1 << bitc::PEO_EXACT
))
4083 cast
<BinaryOperator
>(I
)->setIsExact(true);
4084 } else if (isa
<FPMathOperator
>(I
)) {
4085 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
4087 I
->setFastMathFlags(FMF
);
4093 case bitc::FUNC_CODE_INST_CAST
: { // CAST: [opval, opty, destty, castopc]
4096 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
4097 OpNum
+2 != Record
.size())
4098 return error("Invalid record");
4100 Type
*ResTy
= getTypeByID(Record
[OpNum
]);
4101 int Opc
= getDecodedCastOpcode(Record
[OpNum
+ 1]);
4102 if (Opc
== -1 || !ResTy
)
4103 return error("Invalid record");
4104 Instruction
*Temp
= nullptr;
4105 if ((I
= UpgradeBitCastInst(Opc
, Op
, ResTy
, Temp
))) {
4107 InstructionList
.push_back(Temp
);
4108 assert(CurBB
&& "No current BB?");
4109 CurBB
->getInstList().push_back(Temp
);
4112 auto CastOp
= (Instruction::CastOps
)Opc
;
4113 if (!CastInst::castIsValid(CastOp
, Op
, ResTy
))
4114 return error("Invalid cast");
4115 I
= CastInst::Create(CastOp
, Op
, ResTy
);
4117 InstructionList
.push_back(I
);
4120 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
:
4121 case bitc::FUNC_CODE_INST_GEP_OLD
:
4122 case bitc::FUNC_CODE_INST_GEP
: { // GEP: type, [n x operands]
4128 if (BitCode
== bitc::FUNC_CODE_INST_GEP
) {
4129 InBounds
= Record
[OpNum
++];
4130 Ty
= getTypeByID(Record
[OpNum
++]);
4132 InBounds
= BitCode
== bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD
;
4137 if (getValueTypePair(Record
, OpNum
, NextValueNo
, BasePtr
))
4138 return error("Invalid record");
4141 Ty
= cast
<PointerType
>(BasePtr
->getType()->getScalarType())
4143 } else if (!cast
<PointerType
>(BasePtr
->getType()->getScalarType())
4144 ->isOpaqueOrPointeeTypeMatches(Ty
)) {
4146 "Explicit gep type does not match pointee type of pointer operand");
4149 SmallVector
<Value
*, 16> GEPIdx
;
4150 while (OpNum
!= Record
.size()) {
4152 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4153 return error("Invalid record");
4154 GEPIdx
.push_back(Op
);
4157 I
= GetElementPtrInst::Create(Ty
, BasePtr
, GEPIdx
);
4159 InstructionList
.push_back(I
);
4161 cast
<GetElementPtrInst
>(I
)->setIsInBounds(true);
4165 case bitc::FUNC_CODE_INST_EXTRACTVAL
: {
4166 // EXTRACTVAL: [opty, opval, n x indices]
4169 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
))
4170 return error("Invalid record");
4171 Type
*Ty
= Agg
->getType();
4173 unsigned RecSize
= Record
.size();
4174 if (OpNum
== RecSize
)
4175 return error("EXTRACTVAL: Invalid instruction with 0 indices");
4177 SmallVector
<unsigned, 4> EXTRACTVALIdx
;
4178 for (; OpNum
!= RecSize
; ++OpNum
) {
4179 bool IsArray
= Ty
->isArrayTy();
4180 bool IsStruct
= Ty
->isStructTy();
4181 uint64_t Index
= Record
[OpNum
];
4183 if (!IsStruct
&& !IsArray
)
4184 return error("EXTRACTVAL: Invalid type");
4185 if ((unsigned)Index
!= Index
)
4186 return error("Invalid value");
4187 if (IsStruct
&& Index
>= Ty
->getStructNumElements())
4188 return error("EXTRACTVAL: Invalid struct index");
4189 if (IsArray
&& Index
>= Ty
->getArrayNumElements())
4190 return error("EXTRACTVAL: Invalid array index");
4191 EXTRACTVALIdx
.push_back((unsigned)Index
);
4194 Ty
= Ty
->getStructElementType(Index
);
4196 Ty
= Ty
->getArrayElementType();
4199 I
= ExtractValueInst::Create(Agg
, EXTRACTVALIdx
);
4200 InstructionList
.push_back(I
);
4204 case bitc::FUNC_CODE_INST_INSERTVAL
: {
4205 // INSERTVAL: [opty, opval, opty, opval, n x indices]
4208 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Agg
))
4209 return error("Invalid record");
4211 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Val
))
4212 return error("Invalid record");
4214 unsigned RecSize
= Record
.size();
4215 if (OpNum
== RecSize
)
4216 return error("INSERTVAL: Invalid instruction with 0 indices");
4218 SmallVector
<unsigned, 4> INSERTVALIdx
;
4219 Type
*CurTy
= Agg
->getType();
4220 for (; OpNum
!= RecSize
; ++OpNum
) {
4221 bool IsArray
= CurTy
->isArrayTy();
4222 bool IsStruct
= CurTy
->isStructTy();
4223 uint64_t Index
= Record
[OpNum
];
4225 if (!IsStruct
&& !IsArray
)
4226 return error("INSERTVAL: Invalid type");
4227 if ((unsigned)Index
!= Index
)
4228 return error("Invalid value");
4229 if (IsStruct
&& Index
>= CurTy
->getStructNumElements())
4230 return error("INSERTVAL: Invalid struct index");
4231 if (IsArray
&& Index
>= CurTy
->getArrayNumElements())
4232 return error("INSERTVAL: Invalid array index");
4234 INSERTVALIdx
.push_back((unsigned)Index
);
4236 CurTy
= CurTy
->getStructElementType(Index
);
4238 CurTy
= CurTy
->getArrayElementType();
4241 if (CurTy
!= Val
->getType())
4242 return error("Inserted value type doesn't match aggregate type");
4244 I
= InsertValueInst::Create(Agg
, Val
, INSERTVALIdx
);
4245 InstructionList
.push_back(I
);
4249 case bitc::FUNC_CODE_INST_SELECT
: { // SELECT: [opval, ty, opval, opval]
4250 // obsolete form of select
4251 // handles select i1 ... in old bitcode
4253 Value
*TrueVal
, *FalseVal
, *Cond
;
4254 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
) ||
4255 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
4256 popValue(Record
, OpNum
, NextValueNo
, Type::getInt1Ty(Context
), Cond
))
4257 return error("Invalid record");
4259 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
4260 InstructionList
.push_back(I
);
4264 case bitc::FUNC_CODE_INST_VSELECT
: {// VSELECT: [ty,opval,opval,predty,pred]
4265 // new form of select
4266 // handles select i1 or select [N x i1]
4268 Value
*TrueVal
, *FalseVal
, *Cond
;
4269 if (getValueTypePair(Record
, OpNum
, NextValueNo
, TrueVal
) ||
4270 popValue(Record
, OpNum
, NextValueNo
, TrueVal
->getType(), FalseVal
) ||
4271 getValueTypePair(Record
, OpNum
, NextValueNo
, Cond
))
4272 return error("Invalid record");
4274 // select condition can be either i1 or [N x i1]
4275 if (VectorType
* vector_type
=
4276 dyn_cast
<VectorType
>(Cond
->getType())) {
4278 if (vector_type
->getElementType() != Type::getInt1Ty(Context
))
4279 return error("Invalid type for value");
4282 if (Cond
->getType() != Type::getInt1Ty(Context
))
4283 return error("Invalid type for value");
4286 I
= SelectInst::Create(Cond
, TrueVal
, FalseVal
);
4287 InstructionList
.push_back(I
);
4288 if (OpNum
< Record
.size() && isa
<FPMathOperator
>(I
)) {
4289 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[OpNum
]);
4291 I
->setFastMathFlags(FMF
);
4296 case bitc::FUNC_CODE_INST_EXTRACTELT
: { // EXTRACTELT: [opty, opval, opval]
4299 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
) ||
4300 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
4301 return error("Invalid record");
4302 if (!Vec
->getType()->isVectorTy())
4303 return error("Invalid type for value");
4304 I
= ExtractElementInst::Create(Vec
, Idx
);
4305 InstructionList
.push_back(I
);
4309 case bitc::FUNC_CODE_INST_INSERTELT
: { // INSERTELT: [ty, opval,opval,opval]
4311 Value
*Vec
, *Elt
, *Idx
;
4312 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec
))
4313 return error("Invalid record");
4314 if (!Vec
->getType()->isVectorTy())
4315 return error("Invalid type for value");
4316 if (popValue(Record
, OpNum
, NextValueNo
,
4317 cast
<VectorType
>(Vec
->getType())->getElementType(), Elt
) ||
4318 getValueTypePair(Record
, OpNum
, NextValueNo
, Idx
))
4319 return error("Invalid record");
4320 I
= InsertElementInst::Create(Vec
, Elt
, Idx
);
4321 InstructionList
.push_back(I
);
4325 case bitc::FUNC_CODE_INST_SHUFFLEVEC
: {// SHUFFLEVEC: [opval,ty,opval,opval]
4327 Value
*Vec1
, *Vec2
, *Mask
;
4328 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Vec1
) ||
4329 popValue(Record
, OpNum
, NextValueNo
, Vec1
->getType(), Vec2
))
4330 return error("Invalid record");
4332 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Mask
))
4333 return error("Invalid record");
4334 if (!Vec1
->getType()->isVectorTy() || !Vec2
->getType()->isVectorTy())
4335 return error("Invalid type for value");
4337 I
= new ShuffleVectorInst(Vec1
, Vec2
, Mask
);
4338 InstructionList
.push_back(I
);
4342 case bitc::FUNC_CODE_INST_CMP
: // CMP: [opty, opval, opval, pred]
4343 // Old form of ICmp/FCmp returning bool
4344 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4345 // both legal on vectors but had different behaviour.
4346 case bitc::FUNC_CODE_INST_CMP2
: { // CMP2: [opty, opval, opval, pred]
4347 // FCmp/ICmp returning bool or vector of bool
4351 if (getValueTypePair(Record
, OpNum
, NextValueNo
, LHS
) ||
4352 popValue(Record
, OpNum
, NextValueNo
, LHS
->getType(), RHS
))
4353 return error("Invalid record");
4355 if (OpNum
>= Record
.size())
4357 "Invalid record: operand number exceeded available operands");
4359 unsigned PredVal
= Record
[OpNum
];
4360 bool IsFP
= LHS
->getType()->isFPOrFPVectorTy();
4362 if (IsFP
&& Record
.size() > OpNum
+1)
4363 FMF
= getDecodedFastMathFlags(Record
[++OpNum
]);
4365 if (OpNum
+1 != Record
.size())
4366 return error("Invalid record");
4368 if (LHS
->getType()->isFPOrFPVectorTy())
4369 I
= new FCmpInst((FCmpInst::Predicate
)PredVal
, LHS
, RHS
);
4371 I
= new ICmpInst((ICmpInst::Predicate
)PredVal
, LHS
, RHS
);
4374 I
->setFastMathFlags(FMF
);
4375 InstructionList
.push_back(I
);
4379 case bitc::FUNC_CODE_INST_RET
: // RET: [opty,opval<optional>]
4381 unsigned Size
= Record
.size();
4383 I
= ReturnInst::Create(Context
);
4384 InstructionList
.push_back(I
);
4389 Value
*Op
= nullptr;
4390 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4391 return error("Invalid record");
4392 if (OpNum
!= Record
.size())
4393 return error("Invalid record");
4395 I
= ReturnInst::Create(Context
, Op
);
4396 InstructionList
.push_back(I
);
4399 case bitc::FUNC_CODE_INST_BR
: { // BR: [bb#, bb#, opval] or [bb#]
4400 if (Record
.size() != 1 && Record
.size() != 3)
4401 return error("Invalid record");
4402 BasicBlock
*TrueDest
= getBasicBlock(Record
[0]);
4404 return error("Invalid record");
4406 if (Record
.size() == 1) {
4407 I
= BranchInst::Create(TrueDest
);
4408 InstructionList
.push_back(I
);
4411 BasicBlock
*FalseDest
= getBasicBlock(Record
[1]);
4412 Value
*Cond
= getValue(Record
, 2, NextValueNo
,
4413 Type::getInt1Ty(Context
));
4414 if (!FalseDest
|| !Cond
)
4415 return error("Invalid record");
4416 I
= BranchInst::Create(TrueDest
, FalseDest
, Cond
);
4417 InstructionList
.push_back(I
);
4421 case bitc::FUNC_CODE_INST_CLEANUPRET
: { // CLEANUPRET: [val] or [val,bb#]
4422 if (Record
.size() != 1 && Record
.size() != 2)
4423 return error("Invalid record");
4426 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4428 return error("Invalid record");
4429 BasicBlock
*UnwindDest
= nullptr;
4430 if (Record
.size() == 2) {
4431 UnwindDest
= getBasicBlock(Record
[Idx
++]);
4433 return error("Invalid record");
4436 I
= CleanupReturnInst::Create(CleanupPad
, UnwindDest
);
4437 InstructionList
.push_back(I
);
4440 case bitc::FUNC_CODE_INST_CATCHRET
: { // CATCHRET: [val,bb#]
4441 if (Record
.size() != 2)
4442 return error("Invalid record");
4445 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4447 return error("Invalid record");
4448 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
4450 return error("Invalid record");
4452 I
= CatchReturnInst::Create(CatchPad
, BB
);
4453 InstructionList
.push_back(I
);
4456 case bitc::FUNC_CODE_INST_CATCHSWITCH
: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4457 // We must have, at minimum, the outer scope and the number of arguments.
4458 if (Record
.size() < 2)
4459 return error("Invalid record");
4464 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4466 unsigned NumHandlers
= Record
[Idx
++];
4468 SmallVector
<BasicBlock
*, 2> Handlers
;
4469 for (unsigned Op
= 0; Op
!= NumHandlers
; ++Op
) {
4470 BasicBlock
*BB
= getBasicBlock(Record
[Idx
++]);
4472 return error("Invalid record");
4473 Handlers
.push_back(BB
);
4476 BasicBlock
*UnwindDest
= nullptr;
4477 if (Idx
+ 1 == Record
.size()) {
4478 UnwindDest
= getBasicBlock(Record
[Idx
++]);
4480 return error("Invalid record");
4483 if (Record
.size() != Idx
)
4484 return error("Invalid record");
4487 CatchSwitchInst::Create(ParentPad
, UnwindDest
, NumHandlers
);
4488 for (BasicBlock
*Handler
: Handlers
)
4489 CatchSwitch
->addHandler(Handler
);
4491 InstructionList
.push_back(I
);
4494 case bitc::FUNC_CODE_INST_CATCHPAD
:
4495 case bitc::FUNC_CODE_INST_CLEANUPPAD
: { // [tok,num,(ty,val)*]
4496 // We must have, at minimum, the outer scope and the number of arguments.
4497 if (Record
.size() < 2)
4498 return error("Invalid record");
4503 getValue(Record
, Idx
++, NextValueNo
, Type::getTokenTy(Context
));
4505 unsigned NumArgOperands
= Record
[Idx
++];
4507 SmallVector
<Value
*, 2> Args
;
4508 for (unsigned Op
= 0; Op
!= NumArgOperands
; ++Op
) {
4510 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
4511 return error("Invalid record");
4512 Args
.push_back(Val
);
4515 if (Record
.size() != Idx
)
4516 return error("Invalid record");
4518 if (BitCode
== bitc::FUNC_CODE_INST_CLEANUPPAD
)
4519 I
= CleanupPadInst::Create(ParentPad
, Args
);
4521 I
= CatchPadInst::Create(ParentPad
, Args
);
4522 InstructionList
.push_back(I
);
4525 case bitc::FUNC_CODE_INST_SWITCH
: { // SWITCH: [opty, op0, op1, ...]
4527 if ((Record
[0] >> 16) == SWITCH_INST_MAGIC
) {
4528 // "New" SwitchInst format with case ranges. The changes to write this
4529 // format were reverted but we still recognize bitcode that uses it.
4530 // Hopefully someday we will have support for case ranges and can use
4531 // this format again.
4533 Type
*OpTy
= getTypeByID(Record
[1]);
4534 unsigned ValueBitWidth
= cast
<IntegerType
>(OpTy
)->getBitWidth();
4536 Value
*Cond
= getValue(Record
, 2, NextValueNo
, OpTy
);
4537 BasicBlock
*Default
= getBasicBlock(Record
[3]);
4538 if (!OpTy
|| !Cond
|| !Default
)
4539 return error("Invalid record");
4541 unsigned NumCases
= Record
[4];
4543 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4544 InstructionList
.push_back(SI
);
4546 unsigned CurIdx
= 5;
4547 for (unsigned i
= 0; i
!= NumCases
; ++i
) {
4548 SmallVector
<ConstantInt
*, 1> CaseVals
;
4549 unsigned NumItems
= Record
[CurIdx
++];
4550 for (unsigned ci
= 0; ci
!= NumItems
; ++ci
) {
4551 bool isSingleNumber
= Record
[CurIdx
++];
4554 unsigned ActiveWords
= 1;
4555 if (ValueBitWidth
> 64)
4556 ActiveWords
= Record
[CurIdx
++];
4557 Low
= readWideAPInt(makeArrayRef(&Record
[CurIdx
], ActiveWords
),
4559 CurIdx
+= ActiveWords
;
4561 if (!isSingleNumber
) {
4563 if (ValueBitWidth
> 64)
4564 ActiveWords
= Record
[CurIdx
++];
4565 APInt High
= readWideAPInt(
4566 makeArrayRef(&Record
[CurIdx
], ActiveWords
), ValueBitWidth
);
4567 CurIdx
+= ActiveWords
;
4569 // FIXME: It is not clear whether values in the range should be
4570 // compared as signed or unsigned values. The partially
4571 // implemented changes that used this format in the past used
4572 // unsigned comparisons.
4573 for ( ; Low
.ule(High
); ++Low
)
4574 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4576 CaseVals
.push_back(ConstantInt::get(Context
, Low
));
4578 BasicBlock
*DestBB
= getBasicBlock(Record
[CurIdx
++]);
4579 for (SmallVector
<ConstantInt
*, 1>::iterator cvi
= CaseVals
.begin(),
4580 cve
= CaseVals
.end(); cvi
!= cve
; ++cvi
)
4581 SI
->addCase(*cvi
, DestBB
);
4587 // Old SwitchInst format without case ranges.
4589 if (Record
.size() < 3 || (Record
.size() & 1) == 0)
4590 return error("Invalid record");
4591 Type
*OpTy
= getTypeByID(Record
[0]);
4592 Value
*Cond
= getValue(Record
, 1, NextValueNo
, OpTy
);
4593 BasicBlock
*Default
= getBasicBlock(Record
[2]);
4594 if (!OpTy
|| !Cond
|| !Default
)
4595 return error("Invalid record");
4596 unsigned NumCases
= (Record
.size()-3)/2;
4597 SwitchInst
*SI
= SwitchInst::Create(Cond
, Default
, NumCases
);
4598 InstructionList
.push_back(SI
);
4599 for (unsigned i
= 0, e
= NumCases
; i
!= e
; ++i
) {
4600 ConstantInt
*CaseVal
=
4601 dyn_cast_or_null
<ConstantInt
>(getFnValueByID(Record
[3+i
*2], OpTy
));
4602 BasicBlock
*DestBB
= getBasicBlock(Record
[1+3+i
*2]);
4603 if (!CaseVal
|| !DestBB
) {
4605 return error("Invalid record");
4607 SI
->addCase(CaseVal
, DestBB
);
4612 case bitc::FUNC_CODE_INST_INDIRECTBR
: { // INDIRECTBR: [opty, op0, op1, ...]
4613 if (Record
.size() < 2)
4614 return error("Invalid record");
4615 Type
*OpTy
= getTypeByID(Record
[0]);
4616 Value
*Address
= getValue(Record
, 1, NextValueNo
, OpTy
);
4617 if (!OpTy
|| !Address
)
4618 return error("Invalid record");
4619 unsigned NumDests
= Record
.size()-2;
4620 IndirectBrInst
*IBI
= IndirectBrInst::Create(Address
, NumDests
);
4621 InstructionList
.push_back(IBI
);
4622 for (unsigned i
= 0, e
= NumDests
; i
!= e
; ++i
) {
4623 if (BasicBlock
*DestBB
= getBasicBlock(Record
[2+i
])) {
4624 IBI
->addDestination(DestBB
);
4627 return error("Invalid record");
4634 case bitc::FUNC_CODE_INST_INVOKE
: {
4635 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4636 if (Record
.size() < 4)
4637 return error("Invalid record");
4639 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4640 unsigned CCInfo
= Record
[OpNum
++];
4641 BasicBlock
*NormalBB
= getBasicBlock(Record
[OpNum
++]);
4642 BasicBlock
*UnwindBB
= getBasicBlock(Record
[OpNum
++]);
4644 FunctionType
*FTy
= nullptr;
4645 if ((CCInfo
>> 13) & 1) {
4646 FTy
= dyn_cast
<FunctionType
>(getTypeByID(Record
[OpNum
++]));
4648 return error("Explicit invoke type is not a function type");
4652 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
))
4653 return error("Invalid record");
4655 PointerType
*CalleeTy
= dyn_cast
<PointerType
>(Callee
->getType());
4657 return error("Callee is not a pointer");
4659 FTy
= dyn_cast
<FunctionType
>(
4660 cast
<PointerType
>(Callee
->getType())->getElementType());
4662 return error("Callee is not of pointer to function type");
4663 } else if (!CalleeTy
->isOpaqueOrPointeeTypeMatches(FTy
))
4664 return error("Explicit invoke type does not match pointee type of "
4666 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4667 return error("Insufficient operands to call");
4669 SmallVector
<Value
*, 16> Ops
;
4670 SmallVector
<Type
*, 16> ArgsTys
;
4671 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4672 Ops
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4673 FTy
->getParamType(i
)));
4674 ArgsTys
.push_back(FTy
->getParamType(i
));
4676 return error("Invalid record");
4679 if (!FTy
->isVarArg()) {
4680 if (Record
.size() != OpNum
)
4681 return error("Invalid record");
4683 // Read type/value pairs for varargs params.
4684 while (OpNum
!= Record
.size()) {
4686 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4687 return error("Invalid record");
4689 ArgsTys
.push_back(Op
->getType());
4693 I
= InvokeInst::Create(FTy
, Callee
, NormalBB
, UnwindBB
, Ops
,
4695 OperandBundles
.clear();
4696 InstructionList
.push_back(I
);
4697 cast
<InvokeInst
>(I
)->setCallingConv(
4698 static_cast<CallingConv::ID
>(CallingConv::MaxID
& CCInfo
));
4699 cast
<InvokeInst
>(I
)->setAttributes(PAL
);
4700 propagateAttributeTypes(cast
<CallBase
>(I
), ArgsTys
);
4704 case bitc::FUNC_CODE_INST_RESUME
: { // RESUME: [opval]
4706 Value
*Val
= nullptr;
4707 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
))
4708 return error("Invalid record");
4709 I
= ResumeInst::Create(Val
);
4710 InstructionList
.push_back(I
);
4713 case bitc::FUNC_CODE_INST_CALLBR
: {
4714 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]
4716 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
4717 unsigned CCInfo
= Record
[OpNum
++];
4719 BasicBlock
*DefaultDest
= getBasicBlock(Record
[OpNum
++]);
4720 unsigned NumIndirectDests
= Record
[OpNum
++];
4721 SmallVector
<BasicBlock
*, 16> IndirectDests
;
4722 for (unsigned i
= 0, e
= NumIndirectDests
; i
!= e
; ++i
)
4723 IndirectDests
.push_back(getBasicBlock(Record
[OpNum
++]));
4725 FunctionType
*FTy
= nullptr;
4726 if ((CCInfo
>> bitc::CALL_EXPLICIT_TYPE
) & 1) {
4727 FTy
= dyn_cast
<FunctionType
>(getTypeByID(Record
[OpNum
++]));
4729 return error("Explicit call type is not a function type");
4733 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
))
4734 return error("Invalid record");
4736 PointerType
*OpTy
= dyn_cast
<PointerType
>(Callee
->getType());
4738 return error("Callee is not a pointer type");
4740 FTy
= dyn_cast
<FunctionType
>(
4741 cast
<PointerType
>(Callee
->getType())->getElementType());
4743 return error("Callee is not of pointer to function type");
4744 } else if (cast
<PointerType
>(Callee
->getType())->getElementType() != FTy
)
4745 return error("Explicit call type does not match pointee type of "
4747 if (Record
.size() < FTy
->getNumParams() + OpNum
)
4748 return error("Insufficient operands to call");
4750 SmallVector
<Value
*, 16> Args
;
4751 // Read the fixed params.
4752 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
4753 if (FTy
->getParamType(i
)->isLabelTy())
4754 Args
.push_back(getBasicBlock(Record
[OpNum
]));
4756 Args
.push_back(getValue(Record
, OpNum
, NextValueNo
,
4757 FTy
->getParamType(i
)));
4759 return error("Invalid record");
4762 // Read type/value pairs for varargs params.
4763 if (!FTy
->isVarArg()) {
4764 if (OpNum
!= Record
.size())
4765 return error("Invalid record");
4767 while (OpNum
!= Record
.size()) {
4769 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
4770 return error("Invalid record");
4775 I
= CallBrInst::Create(FTy
, Callee
, DefaultDest
, IndirectDests
, Args
,
4777 OperandBundles
.clear();
4778 InstructionList
.push_back(I
);
4779 cast
<CallBrInst
>(I
)->setCallingConv(
4780 static_cast<CallingConv::ID
>((0x7ff & CCInfo
) >> bitc::CALL_CCONV
));
4781 cast
<CallBrInst
>(I
)->setAttributes(PAL
);
4784 case bitc::FUNC_CODE_INST_UNREACHABLE
: // UNREACHABLE
4785 I
= new UnreachableInst(Context
);
4786 InstructionList
.push_back(I
);
4788 case bitc::FUNC_CODE_INST_PHI
: { // PHI: [ty, val0,bb0, ...]
4790 return error("Invalid record");
4791 // The first record specifies the type.
4792 Type
*Ty
= getTypeByID(Record
[0]);
4794 return error("Invalid record");
4796 // Phi arguments are pairs of records of [value, basic block].
4797 // There is an optional final record for fast-math-flags if this phi has a
4798 // floating-point type.
4799 size_t NumArgs
= (Record
.size() - 1) / 2;
4800 PHINode
*PN
= PHINode::Create(Ty
, NumArgs
);
4801 if ((Record
.size() - 1) % 2 == 1 && !isa
<FPMathOperator
>(PN
))
4802 return error("Invalid record");
4803 InstructionList
.push_back(PN
);
4805 for (unsigned i
= 0; i
!= NumArgs
; i
++) {
4807 // With the new function encoding, it is possible that operands have
4808 // negative IDs (for forward references). Use a signed VBR
4809 // representation to keep the encoding small.
4811 V
= getValueSigned(Record
, i
* 2 + 1, NextValueNo
, Ty
);
4813 V
= getValue(Record
, i
* 2 + 1, NextValueNo
, Ty
);
4814 BasicBlock
*BB
= getBasicBlock(Record
[i
* 2 + 2]);
4816 return error("Invalid record");
4817 PN
->addIncoming(V
, BB
);
4821 // If there are an even number of records, the final record must be FMF.
4822 if (Record
.size() % 2 == 0) {
4823 assert(isa
<FPMathOperator
>(I
) && "Unexpected phi type");
4824 FastMathFlags FMF
= getDecodedFastMathFlags(Record
[Record
.size() - 1]);
4826 I
->setFastMathFlags(FMF
);
4832 case bitc::FUNC_CODE_INST_LANDINGPAD
:
4833 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD
: {
4834 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4836 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD
) {
4837 if (Record
.size() < 3)
4838 return error("Invalid record");
4840 assert(BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
);
4841 if (Record
.size() < 4)
4842 return error("Invalid record");
4844 Type
*Ty
= getTypeByID(Record
[Idx
++]);
4846 return error("Invalid record");
4847 if (BitCode
== bitc::FUNC_CODE_INST_LANDINGPAD_OLD
) {
4848 Value
*PersFn
= nullptr;
4849 if (getValueTypePair(Record
, Idx
, NextValueNo
, PersFn
))
4850 return error("Invalid record");
4852 if (!F
->hasPersonalityFn())
4853 F
->setPersonalityFn(cast
<Constant
>(PersFn
));
4854 else if (F
->getPersonalityFn() != cast
<Constant
>(PersFn
))
4855 return error("Personality function mismatch");
4858 bool IsCleanup
= !!Record
[Idx
++];
4859 unsigned NumClauses
= Record
[Idx
++];
4860 LandingPadInst
*LP
= LandingPadInst::Create(Ty
, NumClauses
);
4861 LP
->setCleanup(IsCleanup
);
4862 for (unsigned J
= 0; J
!= NumClauses
; ++J
) {
4863 LandingPadInst::ClauseType CT
=
4864 LandingPadInst::ClauseType(Record
[Idx
++]); (void)CT
;
4867 if (getValueTypePair(Record
, Idx
, NextValueNo
, Val
)) {
4869 return error("Invalid record");
4872 assert((CT
!= LandingPadInst::Catch
||
4873 !isa
<ArrayType
>(Val
->getType())) &&
4874 "Catch clause has a invalid type!");
4875 assert((CT
!= LandingPadInst::Filter
||
4876 isa
<ArrayType
>(Val
->getType())) &&
4877 "Filter clause has invalid type!");
4878 LP
->addClause(cast
<Constant
>(Val
));
4882 InstructionList
.push_back(I
);
4886 case bitc::FUNC_CODE_INST_ALLOCA
: { // ALLOCA: [instty, opty, op, align]
4887 if (Record
.size() != 4)
4888 return error("Invalid record");
4889 using APV
= AllocaPackedValues
;
4890 const uint64_t Rec
= Record
[3];
4891 const bool InAlloca
= Bitfield::get
<APV::UsedWithInAlloca
>(Rec
);
4892 const bool SwiftError
= Bitfield::get
<APV::SwiftError
>(Rec
);
4893 Type
*Ty
= getTypeByID(Record
[0]);
4894 if (!Bitfield::get
<APV::ExplicitType
>(Rec
)) {
4895 auto *PTy
= dyn_cast_or_null
<PointerType
>(Ty
);
4897 return error("Old-style alloca with a non-pointer type");
4898 Ty
= PTy
->getElementType();
4900 Type
*OpTy
= getTypeByID(Record
[1]);
4901 Value
*Size
= getFnValueByID(Record
[2], OpTy
);
4904 parseAlignmentValue(Bitfield::get
<APV::Align
>(Rec
), Align
)) {
4908 return error("Invalid record");
4910 // FIXME: Make this an optional field.
4911 const DataLayout
&DL
= TheModule
->getDataLayout();
4912 unsigned AS
= DL
.getAllocaAddrSpace();
4914 SmallPtrSet
<Type
*, 4> Visited
;
4915 if (!Align
&& !Ty
->isSized(&Visited
))
4916 return error("alloca of unsized type");
4918 Align
= DL
.getPrefTypeAlign(Ty
);
4920 AllocaInst
*AI
= new AllocaInst(Ty
, AS
, Size
, *Align
);
4921 AI
->setUsedWithInAlloca(InAlloca
);
4922 AI
->setSwiftError(SwiftError
);
4924 InstructionList
.push_back(I
);
4927 case bitc::FUNC_CODE_INST_LOAD
: { // LOAD: [opty, op, align, vol]
4930 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
4931 (OpNum
+ 2 != Record
.size() && OpNum
+ 3 != Record
.size()))
4932 return error("Invalid record");
4934 if (!isa
<PointerType
>(Op
->getType()))
4935 return error("Load operand is not a pointer type");
4938 if (OpNum
+ 3 == Record
.size()) {
4939 Ty
= getTypeByID(Record
[OpNum
++]);
4941 Ty
= cast
<PointerType
>(Op
->getType())->getElementType();
4944 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4948 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4950 SmallPtrSet
<Type
*, 4> Visited
;
4951 if (!Align
&& !Ty
->isSized(&Visited
))
4952 return error("load of unsized type");
4954 Align
= TheModule
->getDataLayout().getABITypeAlign(Ty
);
4955 I
= new LoadInst(Ty
, Op
, "", Record
[OpNum
+ 1], *Align
);
4956 InstructionList
.push_back(I
);
4959 case bitc::FUNC_CODE_INST_LOADATOMIC
: {
4960 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4963 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
) ||
4964 (OpNum
+ 4 != Record
.size() && OpNum
+ 5 != Record
.size()))
4965 return error("Invalid record");
4967 if (!isa
<PointerType
>(Op
->getType()))
4968 return error("Load operand is not a pointer type");
4971 if (OpNum
+ 5 == Record
.size()) {
4972 Ty
= getTypeByID(Record
[OpNum
++]);
4974 Ty
= cast
<PointerType
>(Op
->getType())->getElementType();
4977 if (Error Err
= typeCheckLoadStoreInst(Ty
, Op
->getType()))
4980 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
4981 if (Ordering
== AtomicOrdering::NotAtomic
||
4982 Ordering
== AtomicOrdering::Release
||
4983 Ordering
== AtomicOrdering::AcquireRelease
)
4984 return error("Invalid record");
4985 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
4986 return error("Invalid record");
4987 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
4990 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
4993 return error("Alignment missing from atomic load");
4994 I
= new LoadInst(Ty
, Op
, "", Record
[OpNum
+ 1], *Align
, Ordering
, SSID
);
4995 InstructionList
.push_back(I
);
4998 case bitc::FUNC_CODE_INST_STORE
:
4999 case bitc::FUNC_CODE_INST_STORE_OLD
: { // STORE2:[ptrty, ptr, val, align, vol]
5002 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
5003 (BitCode
== bitc::FUNC_CODE_INST_STORE
5004 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
5005 : popValue(Record
, OpNum
, NextValueNo
,
5006 cast
<PointerType
>(Ptr
->getType())->getElementType(),
5008 OpNum
+ 2 != Record
.size())
5009 return error("Invalid record");
5011 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
5014 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
5016 SmallPtrSet
<Type
*, 4> Visited
;
5017 if (!Align
&& !Val
->getType()->isSized(&Visited
))
5018 return error("store of unsized type");
5020 Align
= TheModule
->getDataLayout().getABITypeAlign(Val
->getType());
5021 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+ 1], *Align
);
5022 InstructionList
.push_back(I
);
5025 case bitc::FUNC_CODE_INST_STOREATOMIC
:
5026 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD
: {
5027 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
5030 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
) ||
5031 !isa
<PointerType
>(Ptr
->getType()) ||
5032 (BitCode
== bitc::FUNC_CODE_INST_STOREATOMIC
5033 ? getValueTypePair(Record
, OpNum
, NextValueNo
, Val
)
5034 : popValue(Record
, OpNum
, NextValueNo
,
5035 cast
<PointerType
>(Ptr
->getType())->getElementType(),
5037 OpNum
+ 4 != Record
.size())
5038 return error("Invalid record");
5040 if (Error Err
= typeCheckLoadStoreInst(Val
->getType(), Ptr
->getType()))
5042 AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
5043 if (Ordering
== AtomicOrdering::NotAtomic
||
5044 Ordering
== AtomicOrdering::Acquire
||
5045 Ordering
== AtomicOrdering::AcquireRelease
)
5046 return error("Invalid record");
5047 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
5048 if (Ordering
!= AtomicOrdering::NotAtomic
&& Record
[OpNum
] == 0)
5049 return error("Invalid record");
5052 if (Error Err
= parseAlignmentValue(Record
[OpNum
], Align
))
5055 return error("Alignment missing from atomic store");
5056 I
= new StoreInst(Val
, Ptr
, Record
[OpNum
+ 1], *Align
, Ordering
, SSID
);
5057 InstructionList
.push_back(I
);
5060 case bitc::FUNC_CODE_INST_CMPXCHG_OLD
: {
5061 // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope,
5062 // failure_ordering?, weak?]
5063 const size_t NumRecords
= Record
.size();
5065 Value
*Ptr
= nullptr;
5066 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
))
5067 return error("Invalid record");
5069 if (!isa
<PointerType
>(Ptr
->getType()))
5070 return error("Cmpxchg operand is not a pointer type");
5072 Value
*Cmp
= nullptr;
5073 if (popValue(Record
, OpNum
, NextValueNo
,
5074 cast
<PointerType
>(Ptr
->getType())->getPointerElementType(),
5076 return error("Invalid record");
5078 Value
*New
= nullptr;
5079 if (popValue(Record
, OpNum
, NextValueNo
, Cmp
->getType(), New
) ||
5080 NumRecords
< OpNum
+ 3 || NumRecords
> OpNum
+ 5)
5081 return error("Invalid record");
5083 const AtomicOrdering SuccessOrdering
=
5084 getDecodedOrdering(Record
[OpNum
+ 1]);
5085 if (SuccessOrdering
== AtomicOrdering::NotAtomic
||
5086 SuccessOrdering
== AtomicOrdering::Unordered
)
5087 return error("Invalid record");
5089 const SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 2]);
5091 if (Error Err
= typeCheckLoadStoreInst(Cmp
->getType(), Ptr
->getType()))
5094 const AtomicOrdering FailureOrdering
=
5096 ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering
)
5097 : getDecodedOrdering(Record
[OpNum
+ 3]);
5099 if (FailureOrdering
== AtomicOrdering::NotAtomic
||
5100 FailureOrdering
== AtomicOrdering::Unordered
)
5101 return error("Invalid record");
5103 const Align
Alignment(
5104 TheModule
->getDataLayout().getTypeStoreSize(Cmp
->getType()));
5106 I
= new AtomicCmpXchgInst(Ptr
, Cmp
, New
, Alignment
, SuccessOrdering
,
5107 FailureOrdering
, SSID
);
5108 cast
<AtomicCmpXchgInst
>(I
)->setVolatile(Record
[OpNum
]);
5110 if (NumRecords
< 8) {
5111 // Before weak cmpxchgs existed, the instruction simply returned the
5112 // value loaded from memory, so bitcode files from that era will be
5113 // expecting the first component of a modern cmpxchg.
5114 CurBB
->getInstList().push_back(I
);
5115 I
= ExtractValueInst::Create(I
, 0);
5117 cast
<AtomicCmpXchgInst
>(I
)->setWeak(Record
[OpNum
+ 4]);
5120 InstructionList
.push_back(I
);
5123 case bitc::FUNC_CODE_INST_CMPXCHG
: {
5124 // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope,
5125 // failure_ordering, weak, align?]
5126 const size_t NumRecords
= Record
.size();
5128 Value
*Ptr
= nullptr;
5129 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
))
5130 return error("Invalid record");
5132 if (!isa
<PointerType
>(Ptr
->getType()))
5133 return error("Cmpxchg operand is not a pointer type");
5135 Value
*Cmp
= nullptr;
5136 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Cmp
))
5137 return error("Invalid record");
5139 Value
*Val
= nullptr;
5140 if (popValue(Record
, OpNum
, NextValueNo
, Cmp
->getType(), Val
))
5141 return error("Invalid record");
5143 if (NumRecords
< OpNum
+ 3 || NumRecords
> OpNum
+ 6)
5144 return error("Invalid record");
5146 const bool IsVol
= Record
[OpNum
];
5148 const AtomicOrdering SuccessOrdering
=
5149 getDecodedOrdering(Record
[OpNum
+ 1]);
5150 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering
))
5151 return error("Invalid cmpxchg success ordering");
5153 const SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 2]);
5155 if (Error Err
= typeCheckLoadStoreInst(Cmp
->getType(), Ptr
->getType()))
5158 const AtomicOrdering FailureOrdering
=
5159 getDecodedOrdering(Record
[OpNum
+ 3]);
5160 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering
))
5161 return error("Invalid cmpxchg failure ordering");
5163 const bool IsWeak
= Record
[OpNum
+ 4];
5165 MaybeAlign Alignment
;
5167 if (NumRecords
== (OpNum
+ 6)) {
5168 if (Error Err
= parseAlignmentValue(Record
[OpNum
+ 5], Alignment
))
5173 Align(TheModule
->getDataLayout().getTypeStoreSize(Cmp
->getType()));
5175 I
= new AtomicCmpXchgInst(Ptr
, Cmp
, Val
, *Alignment
, SuccessOrdering
,
5176 FailureOrdering
, SSID
);
5177 cast
<AtomicCmpXchgInst
>(I
)->setVolatile(IsVol
);
5178 cast
<AtomicCmpXchgInst
>(I
)->setWeak(IsWeak
);
5180 InstructionList
.push_back(I
);
5183 case bitc::FUNC_CODE_INST_ATOMICRMW_OLD
:
5184 case bitc::FUNC_CODE_INST_ATOMICRMW
: {
5185 // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?]
5186 // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?]
5187 const size_t NumRecords
= Record
.size();
5190 Value
*Ptr
= nullptr;
5191 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Ptr
))
5192 return error("Invalid record");
5194 if (!isa
<PointerType
>(Ptr
->getType()))
5195 return error("Invalid record");
5197 Value
*Val
= nullptr;
5198 if (BitCode
== bitc::FUNC_CODE_INST_ATOMICRMW_OLD
) {
5199 if (popValue(Record
, OpNum
, NextValueNo
,
5200 cast
<PointerType
>(Ptr
->getType())->getPointerElementType(),
5202 return error("Invalid record");
5204 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Val
))
5205 return error("Invalid record");
5208 if (!(NumRecords
== (OpNum
+ 4) || NumRecords
== (OpNum
+ 5)))
5209 return error("Invalid record");
5211 const AtomicRMWInst::BinOp Operation
=
5212 getDecodedRMWOperation(Record
[OpNum
]);
5213 if (Operation
< AtomicRMWInst::FIRST_BINOP
||
5214 Operation
> AtomicRMWInst::LAST_BINOP
)
5215 return error("Invalid record");
5217 const bool IsVol
= Record
[OpNum
+ 1];
5219 const AtomicOrdering Ordering
= getDecodedOrdering(Record
[OpNum
+ 2]);
5220 if (Ordering
== AtomicOrdering::NotAtomic
||
5221 Ordering
== AtomicOrdering::Unordered
)
5222 return error("Invalid record");
5224 const SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[OpNum
+ 3]);
5226 MaybeAlign Alignment
;
5228 if (NumRecords
== (OpNum
+ 5)) {
5229 if (Error Err
= parseAlignmentValue(Record
[OpNum
+ 4], Alignment
))
5235 Align(TheModule
->getDataLayout().getTypeStoreSize(Val
->getType()));
5237 I
= new AtomicRMWInst(Operation
, Ptr
, Val
, *Alignment
, Ordering
, SSID
);
5238 cast
<AtomicRMWInst
>(I
)->setVolatile(IsVol
);
5240 InstructionList
.push_back(I
);
5243 case bitc::FUNC_CODE_INST_FENCE
: { // FENCE:[ordering, ssid]
5244 if (2 != Record
.size())
5245 return error("Invalid record");
5246 AtomicOrdering Ordering
= getDecodedOrdering(Record
[0]);
5247 if (Ordering
== AtomicOrdering::NotAtomic
||
5248 Ordering
== AtomicOrdering::Unordered
||
5249 Ordering
== AtomicOrdering::Monotonic
)
5250 return error("Invalid record");
5251 SyncScope::ID SSID
= getDecodedSyncScopeID(Record
[1]);
5252 I
= new FenceInst(Context
, Ordering
, SSID
);
5253 InstructionList
.push_back(I
);
5256 case bitc::FUNC_CODE_INST_CALL
: {
5257 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
5258 if (Record
.size() < 3)
5259 return error("Invalid record");
5262 AttributeList PAL
= getAttributes(Record
[OpNum
++]);
5263 unsigned CCInfo
= Record
[OpNum
++];
5266 if ((CCInfo
>> bitc::CALL_FMF
) & 1) {
5267 FMF
= getDecodedFastMathFlags(Record
[OpNum
++]);
5269 return error("Fast math flags indicator set for call with no FMF");
5272 FunctionType
*FTy
= nullptr;
5273 if ((CCInfo
>> bitc::CALL_EXPLICIT_TYPE
) & 1) {
5274 FTy
= dyn_cast
<FunctionType
>(getTypeByID(Record
[OpNum
++]));
5276 return error("Explicit call type is not a function type");
5280 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Callee
))
5281 return error("Invalid record");
5283 PointerType
*OpTy
= dyn_cast
<PointerType
>(Callee
->getType());
5285 return error("Callee is not a pointer type");
5287 FTy
= dyn_cast
<FunctionType
>(
5288 cast
<PointerType
>(Callee
->getType())->getElementType());
5290 return error("Callee is not of pointer to function type");
5291 } else if (!OpTy
->isOpaqueOrPointeeTypeMatches(FTy
))
5292 return error("Explicit call type does not match pointee type of "
5294 if (Record
.size() < FTy
->getNumParams() + OpNum
)
5295 return error("Insufficient operands to call");
5297 SmallVector
<Value
*, 16> Args
;
5298 SmallVector
<Type
*, 16> ArgsTys
;
5299 // Read the fixed params.
5300 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
, ++OpNum
) {
5301 if (FTy
->getParamType(i
)->isLabelTy())
5302 Args
.push_back(getBasicBlock(Record
[OpNum
]));
5304 Args
.push_back(getValue(Record
, OpNum
, NextValueNo
,
5305 FTy
->getParamType(i
)));
5306 ArgsTys
.push_back(FTy
->getParamType(i
));
5308 return error("Invalid record");
5311 // Read type/value pairs for varargs params.
5312 if (!FTy
->isVarArg()) {
5313 if (OpNum
!= Record
.size())
5314 return error("Invalid record");
5316 while (OpNum
!= Record
.size()) {
5318 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
5319 return error("Invalid record");
5321 ArgsTys
.push_back(Op
->getType());
5325 I
= CallInst::Create(FTy
, Callee
, Args
, OperandBundles
);
5326 OperandBundles
.clear();
5327 InstructionList
.push_back(I
);
5328 cast
<CallInst
>(I
)->setCallingConv(
5329 static_cast<CallingConv::ID
>((0x7ff & CCInfo
) >> bitc::CALL_CCONV
));
5330 CallInst::TailCallKind TCK
= CallInst::TCK_None
;
5331 if (CCInfo
& 1 << bitc::CALL_TAIL
)
5332 TCK
= CallInst::TCK_Tail
;
5333 if (CCInfo
& (1 << bitc::CALL_MUSTTAIL
))
5334 TCK
= CallInst::TCK_MustTail
;
5335 if (CCInfo
& (1 << bitc::CALL_NOTAIL
))
5336 TCK
= CallInst::TCK_NoTail
;
5337 cast
<CallInst
>(I
)->setTailCallKind(TCK
);
5338 cast
<CallInst
>(I
)->setAttributes(PAL
);
5339 propagateAttributeTypes(cast
<CallBase
>(I
), ArgsTys
);
5341 if (!isa
<FPMathOperator
>(I
))
5342 return error("Fast-math-flags specified for call without "
5343 "floating-point scalar or vector return type");
5344 I
->setFastMathFlags(FMF
);
5348 case bitc::FUNC_CODE_INST_VAARG
: { // VAARG: [valistty, valist, instty]
5349 if (Record
.size() < 3)
5350 return error("Invalid record");
5351 Type
*OpTy
= getTypeByID(Record
[0]);
5352 Value
*Op
= getValue(Record
, 1, NextValueNo
, OpTy
);
5353 Type
*ResTy
= getTypeByID(Record
[2]);
5354 if (!OpTy
|| !Op
|| !ResTy
)
5355 return error("Invalid record");
5356 I
= new VAArgInst(Op
, ResTy
);
5357 InstructionList
.push_back(I
);
5361 case bitc::FUNC_CODE_OPERAND_BUNDLE
: {
5362 // A call or an invoke can be optionally prefixed with some variable
5363 // number of operand bundle blocks. These blocks are read into
5364 // OperandBundles and consumed at the next call or invoke instruction.
5366 if (Record
.empty() || Record
[0] >= BundleTags
.size())
5367 return error("Invalid record");
5369 std::vector
<Value
*> Inputs
;
5372 while (OpNum
!= Record
.size()) {
5374 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
5375 return error("Invalid record");
5376 Inputs
.push_back(Op
);
5379 OperandBundles
.emplace_back(BundleTags
[Record
[0]], std::move(Inputs
));
5383 case bitc::FUNC_CODE_INST_FREEZE
: { // FREEZE: [opty,opval]
5385 Value
*Op
= nullptr;
5386 if (getValueTypePair(Record
, OpNum
, NextValueNo
, Op
))
5387 return error("Invalid record");
5388 if (OpNum
!= Record
.size())
5389 return error("Invalid record");
5391 I
= new FreezeInst(Op
);
5392 InstructionList
.push_back(I
);
5397 // Add instruction to end of current BB. If there is no current BB, reject
5401 return error("Invalid instruction with no BB");
5403 if (!OperandBundles
.empty()) {
5405 return error("Operand bundles found with no consumer");
5407 CurBB
->getInstList().push_back(I
);
5409 // If this was a terminator instruction, move to the next block.
5410 if (I
->isTerminator()) {
5412 CurBB
= CurBBNo
< FunctionBBs
.size() ? FunctionBBs
[CurBBNo
] : nullptr;
5415 // Non-void values get registered in the value table for future use.
5416 if (!I
->getType()->isVoidTy())
5417 ValueList
.assignValue(I
, NextValueNo
++);
5422 if (!OperandBundles
.empty())
5423 return error("Operand bundles found with no consumer");
5425 // Check the function list for unresolved values.
5426 if (Argument
*A
= dyn_cast
<Argument
>(ValueList
.back())) {
5427 if (!A
->getParent()) {
5428 // We found at least one unresolved value. Nuke them all to avoid leaks.
5429 for (unsigned i
= ModuleValueListSize
, e
= ValueList
.size(); i
!= e
; ++i
){
5430 if ((A
= dyn_cast_or_null
<Argument
>(ValueList
[i
])) && !A
->getParent()) {
5431 A
->replaceAllUsesWith(UndefValue::get(A
->getType()));
5435 return error("Never resolved value found in function");
5439 // Unexpected unresolved metadata about to be dropped.
5440 if (MDLoader
->hasFwdRefs())
5441 return error("Invalid function metadata: outgoing forward refs");
5443 // Trim the value list down to the size it was before we parsed this function.
5444 ValueList
.shrinkTo(ModuleValueListSize
);
5445 MDLoader
->shrinkTo(ModuleMDLoaderSize
);
5446 std::vector
<BasicBlock
*>().swap(FunctionBBs
);
5447 return Error::success();
5450 /// Find the function body in the bitcode stream
5451 Error
BitcodeReader::findFunctionInStream(
5453 DenseMap
<Function
*, uint64_t>::iterator DeferredFunctionInfoIterator
) {
5454 while (DeferredFunctionInfoIterator
->second
== 0) {
5455 // This is the fallback handling for the old format bitcode that
5456 // didn't contain the function index in the VST, or when we have
5457 // an anonymous function which would not have a VST entry.
5458 // Assert that we have one of those two cases.
5459 assert(VSTOffset
== 0 || !F
->hasName());
5460 // Parse the next body in the stream and set its position in the
5461 // DeferredFunctionInfo map.
5462 if (Error Err
= rememberAndSkipFunctionBodies())
5465 return Error::success();
5468 SyncScope::ID
BitcodeReader::getDecodedSyncScopeID(unsigned Val
) {
5469 if (Val
== SyncScope::SingleThread
|| Val
== SyncScope::System
)
5470 return SyncScope::ID(Val
);
5471 if (Val
>= SSIDs
.size())
5472 return SyncScope::System
; // Map unknown synchronization scopes to system.
5476 //===----------------------------------------------------------------------===//
5477 // GVMaterializer implementation
5478 //===----------------------------------------------------------------------===//
5480 Error
BitcodeReader::materialize(GlobalValue
*GV
) {
5481 Function
*F
= dyn_cast
<Function
>(GV
);
5482 // If it's not a function or is already material, ignore the request.
5483 if (!F
|| !F
->isMaterializable())
5484 return Error::success();
5486 DenseMap
<Function
*, uint64_t>::iterator DFII
= DeferredFunctionInfo
.find(F
);
5487 assert(DFII
!= DeferredFunctionInfo
.end() && "Deferred function not found!");
5488 // If its position is recorded as 0, its body is somewhere in the stream
5489 // but we haven't seen it yet.
5490 if (DFII
->second
== 0)
5491 if (Error Err
= findFunctionInStream(F
, DFII
))
5494 // Materialize metadata before parsing any function bodies.
5495 if (Error Err
= materializeMetadata())
5498 // Move the bit stream to the saved position of the deferred function body.
5499 if (Error JumpFailed
= Stream
.JumpToBit(DFII
->second
))
5501 if (Error Err
= parseFunctionBody(F
))
5503 F
->setIsMaterializable(false);
5508 // Upgrade any old intrinsic calls in the function.
5509 for (auto &I
: UpgradedIntrinsics
) {
5510 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
5514 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
5515 UpgradeIntrinsicCall(CI
, I
.second
);
5519 // Update calls to the remangled intrinsics
5520 for (auto &I
: RemangledIntrinsics
)
5521 for (auto UI
= I
.first
->materialized_user_begin(), UE
= I
.first
->user_end();
5523 // Don't expect any other users than call sites
5524 cast
<CallBase
>(*UI
++)->setCalledFunction(I
.second
);
5526 // Finish fn->subprogram upgrade for materialized functions.
5527 if (DISubprogram
*SP
= MDLoader
->lookupSubprogramForFunction(F
))
5528 F
->setSubprogram(SP
);
5530 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
5531 if (!MDLoader
->isStrippingTBAA()) {
5532 for (auto &I
: instructions(F
)) {
5533 MDNode
*TBAA
= I
.getMetadata(LLVMContext::MD_tbaa
);
5534 if (!TBAA
|| TBAAVerifyHelper
.visitTBAAMetadata(I
, TBAA
))
5536 MDLoader
->setStripTBAA(true);
5537 stripTBAA(F
->getParent());
5541 for (auto &I
: instructions(F
)) {
5542 // "Upgrade" older incorrect branch weights by dropping them.
5543 if (auto *MD
= I
.getMetadata(LLVMContext::MD_prof
)) {
5544 if (MD
->getOperand(0) != nullptr && isa
<MDString
>(MD
->getOperand(0))) {
5545 MDString
*MDS
= cast
<MDString
>(MD
->getOperand(0));
5546 StringRef ProfName
= MDS
->getString();
5547 // Check consistency of !prof branch_weights metadata.
5548 if (!ProfName
.equals("branch_weights"))
5550 unsigned ExpectedNumOperands
= 0;
5551 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(&I
))
5552 ExpectedNumOperands
= BI
->getNumSuccessors();
5553 else if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(&I
))
5554 ExpectedNumOperands
= SI
->getNumSuccessors();
5555 else if (isa
<CallInst
>(&I
))
5556 ExpectedNumOperands
= 1;
5557 else if (IndirectBrInst
*IBI
= dyn_cast
<IndirectBrInst
>(&I
))
5558 ExpectedNumOperands
= IBI
->getNumDestinations();
5559 else if (isa
<SelectInst
>(&I
))
5560 ExpectedNumOperands
= 2;
5562 continue; // ignore and continue.
5564 // If branch weight doesn't match, just strip branch weight.
5565 if (MD
->getNumOperands() != 1 + ExpectedNumOperands
)
5566 I
.setMetadata(LLVMContext::MD_prof
, nullptr);
5570 // Remove incompatible attributes on function calls.
5571 if (auto *CI
= dyn_cast
<CallBase
>(&I
)) {
5572 CI
->removeRetAttrs(AttributeFuncs::typeIncompatible(
5573 CI
->getFunctionType()->getReturnType()));
5575 for (unsigned ArgNo
= 0; ArgNo
< CI
->arg_size(); ++ArgNo
)
5576 CI
->removeParamAttrs(ArgNo
, AttributeFuncs::typeIncompatible(
5577 CI
->getArgOperand(ArgNo
)->getType()));
5581 // Look for functions that rely on old function attribute behavior.
5582 UpgradeFunctionAttributes(*F
);
5584 // Bring in any functions that this function forward-referenced via
5586 return materializeForwardReferencedFunctions();
5589 Error
BitcodeReader::materializeModule() {
5590 if (Error Err
= materializeMetadata())
5593 // Promise to materialize all forward references.
5594 WillMaterializeAllForwardRefs
= true;
5596 // Iterate over the module, deserializing any functions that are still on
5598 for (Function
&F
: *TheModule
) {
5599 if (Error Err
= materialize(&F
))
5602 // At this point, if there are any function bodies, parse the rest of
5603 // the bits in the module past the last function block we have recorded
5604 // through either lazy scanning or the VST.
5605 if (LastFunctionBlockBit
|| NextUnreadBit
)
5606 if (Error Err
= parseModule(LastFunctionBlockBit
> NextUnreadBit
5607 ? LastFunctionBlockBit
5611 // Check that all block address forward references got resolved (as we
5613 if (!BasicBlockFwdRefs
.empty())
5614 return error("Never resolved function from blockaddress");
5616 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5617 // delete the old functions to clean up. We can't do this unless the entire
5618 // module is materialized because there could always be another function body
5619 // with calls to the old function.
5620 for (auto &I
: UpgradedIntrinsics
) {
5621 for (auto *U
: I
.first
->users()) {
5622 if (CallInst
*CI
= dyn_cast
<CallInst
>(U
))
5623 UpgradeIntrinsicCall(CI
, I
.second
);
5625 if (!I
.first
->use_empty())
5626 I
.first
->replaceAllUsesWith(I
.second
);
5627 I
.first
->eraseFromParent();
5629 UpgradedIntrinsics
.clear();
5630 // Do the same for remangled intrinsics
5631 for (auto &I
: RemangledIntrinsics
) {
5632 I
.first
->replaceAllUsesWith(I
.second
);
5633 I
.first
->eraseFromParent();
5635 RemangledIntrinsics
.clear();
5637 UpgradeDebugInfo(*TheModule
);
5639 UpgradeModuleFlags(*TheModule
);
5641 UpgradeARCRuntime(*TheModule
);
5643 return Error::success();
5646 std::vector
<StructType
*> BitcodeReader::getIdentifiedStructTypes() const {
5647 return IdentifiedStructTypes
;
5650 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5651 BitstreamCursor Cursor
, StringRef Strtab
, ModuleSummaryIndex
&TheIndex
,
5652 StringRef ModulePath
, unsigned ModuleId
)
5653 : BitcodeReaderBase(std::move(Cursor
), Strtab
), TheIndex(TheIndex
),
5654 ModulePath(ModulePath
), ModuleId(ModuleId
) {}
5656 void ModuleSummaryIndexBitcodeReader::addThisModule() {
5657 TheIndex
.addModule(ModulePath
, ModuleId
);
5660 ModuleSummaryIndex::ModuleInfo
*
5661 ModuleSummaryIndexBitcodeReader::getThisModule() {
5662 return TheIndex
.getModule(ModulePath
);
5665 std::pair
<ValueInfo
, GlobalValue::GUID
>
5666 ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId
) {
5667 auto VGI
= ValueIdToValueInfoMap
[ValueId
];
5672 void ModuleSummaryIndexBitcodeReader::setValueGUID(
5673 uint64_t ValueID
, StringRef ValueName
, GlobalValue::LinkageTypes Linkage
,
5674 StringRef SourceFileName
) {
5675 std::string GlobalId
=
5676 GlobalValue::getGlobalIdentifier(ValueName
, Linkage
, SourceFileName
);
5677 auto ValueGUID
= GlobalValue::getGUID(GlobalId
);
5678 auto OriginalNameID
= ValueGUID
;
5679 if (GlobalValue::isLocalLinkage(Linkage
))
5680 OriginalNameID
= GlobalValue::getGUID(ValueName
);
5681 if (PrintSummaryGUIDs
)
5682 dbgs() << "GUID " << ValueGUID
<< "(" << OriginalNameID
<< ") is "
5683 << ValueName
<< "\n";
5685 // UseStrtab is false for legacy summary formats and value names are
5686 // created on stack. In that case we save the name in a string saver in
5687 // the index so that the value name can be recorded.
5688 ValueIdToValueInfoMap
[ValueID
] = std::make_pair(
5689 TheIndex
.getOrInsertValueInfo(
5691 UseStrtab
? ValueName
: TheIndex
.saveString(ValueName
)),
5695 // Specialized value symbol table parser used when reading module index
5696 // blocks where we don't actually create global values. The parsed information
5697 // is saved in the bitcode reader for use when later parsing summaries.
5698 Error
ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
5700 DenseMap
<unsigned, GlobalValue::LinkageTypes
> &ValueIdToLinkageMap
) {
5701 // With a strtab the VST is not required to parse the summary.
5703 return Error::success();
5705 assert(Offset
> 0 && "Expected non-zero VST offset");
5706 Expected
<uint64_t> MaybeCurrentBit
= jumpToValueSymbolTable(Offset
, Stream
);
5707 if (!MaybeCurrentBit
)
5708 return MaybeCurrentBit
.takeError();
5709 uint64_t CurrentBit
= MaybeCurrentBit
.get();
5711 if (Error Err
= Stream
.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID
))
5714 SmallVector
<uint64_t, 64> Record
;
5716 // Read all the records for this value table.
5717 SmallString
<128> ValueName
;
5720 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
5722 return MaybeEntry
.takeError();
5723 BitstreamEntry Entry
= MaybeEntry
.get();
5725 switch (Entry
.Kind
) {
5726 case BitstreamEntry::SubBlock
: // Handled for us already.
5727 case BitstreamEntry::Error
:
5728 return error("Malformed block");
5729 case BitstreamEntry::EndBlock
:
5730 // Done parsing VST, jump back to wherever we came from.
5731 if (Error JumpFailed
= Stream
.JumpToBit(CurrentBit
))
5733 return Error::success();
5734 case BitstreamEntry::Record
:
5735 // The interesting case.
5741 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
5743 return MaybeRecord
.takeError();
5744 switch (MaybeRecord
.get()) {
5745 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5747 case bitc::VST_CODE_ENTRY
: { // VST_CODE_ENTRY: [valueid, namechar x N]
5748 if (convertToString(Record
, 1, ValueName
))
5749 return error("Invalid record");
5750 unsigned ValueID
= Record
[0];
5751 assert(!SourceFileName
.empty());
5752 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
5753 assert(VLI
!= ValueIdToLinkageMap
.end() &&
5754 "No linkage found for VST entry?");
5755 auto Linkage
= VLI
->second
;
5756 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
5760 case bitc::VST_CODE_FNENTRY
: {
5761 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
5762 if (convertToString(Record
, 2, ValueName
))
5763 return error("Invalid record");
5764 unsigned ValueID
= Record
[0];
5765 assert(!SourceFileName
.empty());
5766 auto VLI
= ValueIdToLinkageMap
.find(ValueID
);
5767 assert(VLI
!= ValueIdToLinkageMap
.end() &&
5768 "No linkage found for VST entry?");
5769 auto Linkage
= VLI
->second
;
5770 setValueGUID(ValueID
, ValueName
, Linkage
, SourceFileName
);
5774 case bitc::VST_CODE_COMBINED_ENTRY
: {
5775 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
5776 unsigned ValueID
= Record
[0];
5777 GlobalValue::GUID RefGUID
= Record
[1];
5778 // The "original name", which is the second value of the pair will be
5779 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5780 ValueIdToValueInfoMap
[ValueID
] =
5781 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
5788 // Parse just the blocks needed for building the index out of the module.
5789 // At the end of this routine the module Index is populated with a map
5790 // from global value id to GlobalValueSummary objects.
5791 Error
ModuleSummaryIndexBitcodeReader::parseModule() {
5792 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
5795 SmallVector
<uint64_t, 64> Record
;
5796 DenseMap
<unsigned, GlobalValue::LinkageTypes
> ValueIdToLinkageMap
;
5797 unsigned ValueId
= 0;
5799 // Read the index for this module.
5801 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
5803 return MaybeEntry
.takeError();
5804 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
5806 switch (Entry
.Kind
) {
5807 case BitstreamEntry::Error
:
5808 return error("Malformed block");
5809 case BitstreamEntry::EndBlock
:
5810 return Error::success();
5812 case BitstreamEntry::SubBlock
:
5814 default: // Skip unknown content.
5815 if (Error Err
= Stream
.SkipBlock())
5818 case bitc::BLOCKINFO_BLOCK_ID
:
5819 // Need to parse these to get abbrev ids (e.g. for VST)
5820 if (readBlockInfo())
5821 return error("Malformed block");
5823 case bitc::VALUE_SYMTAB_BLOCK_ID
:
5824 // Should have been parsed earlier via VSTOffset, unless there
5825 // is no summary section.
5826 assert(((SeenValueSymbolTable
&& VSTOffset
> 0) ||
5827 !SeenGlobalValSummary
) &&
5828 "Expected early VST parse via VSTOffset record");
5829 if (Error Err
= Stream
.SkipBlock())
5832 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID
:
5833 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
:
5834 // Add the module if it is a per-module index (has a source file name).
5835 if (!SourceFileName
.empty())
5837 assert(!SeenValueSymbolTable
&&
5838 "Already read VST when parsing summary block?");
5839 // We might not have a VST if there were no values in the
5840 // summary. An empty summary block generated when we are
5841 // performing ThinLTO compiles so we don't later invoke
5842 // the regular LTO process on them.
5843 if (VSTOffset
> 0) {
5844 if (Error Err
= parseValueSymbolTable(VSTOffset
, ValueIdToLinkageMap
))
5846 SeenValueSymbolTable
= true;
5848 SeenGlobalValSummary
= true;
5849 if (Error Err
= parseEntireSummary(Entry
.ID
))
5852 case bitc::MODULE_STRTAB_BLOCK_ID
:
5853 if (Error Err
= parseModuleStringTable())
5859 case BitstreamEntry::Record
: {
5861 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
5863 return MaybeBitCode
.takeError();
5864 switch (MaybeBitCode
.get()) {
5866 break; // Default behavior, ignore unknown content.
5867 case bitc::MODULE_CODE_VERSION
: {
5868 if (Error Err
= parseVersionRecord(Record
).takeError())
5872 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5873 case bitc::MODULE_CODE_SOURCE_FILENAME
: {
5874 SmallString
<128> ValueName
;
5875 if (convertToString(Record
, 0, ValueName
))
5876 return error("Invalid record");
5877 SourceFileName
= ValueName
.c_str();
5880 /// MODULE_CODE_HASH: [5*i32]
5881 case bitc::MODULE_CODE_HASH
: {
5882 if (Record
.size() != 5)
5883 return error("Invalid hash length " + Twine(Record
.size()).str());
5884 auto &Hash
= getThisModule()->second
.second
;
5886 for (auto &Val
: Record
) {
5887 assert(!(Val
>> 32) && "Unexpected high bits set");
5892 /// MODULE_CODE_VSTOFFSET: [offset]
5893 case bitc::MODULE_CODE_VSTOFFSET
:
5895 return error("Invalid record");
5896 // Note that we subtract 1 here because the offset is relative to one
5897 // word before the start of the identification or module block, which
5898 // was historically always the start of the regular bitcode header.
5899 VSTOffset
= Record
[0] - 1;
5901 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
5902 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
5903 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
5904 // v2: [strtab offset, strtab size, v1]
5905 case bitc::MODULE_CODE_GLOBALVAR
:
5906 case bitc::MODULE_CODE_FUNCTION
:
5907 case bitc::MODULE_CODE_ALIAS
: {
5909 ArrayRef
<uint64_t> GVRecord
;
5910 std::tie(Name
, GVRecord
) = readNameFromStrtab(Record
);
5911 if (GVRecord
.size() <= 3)
5912 return error("Invalid record");
5913 uint64_t RawLinkage
= GVRecord
[3];
5914 GlobalValue::LinkageTypes Linkage
= getDecodedLinkage(RawLinkage
);
5916 ValueIdToLinkageMap
[ValueId
++] = Linkage
;
5920 setValueGUID(ValueId
++, Name
, Linkage
, SourceFileName
);
5930 std::vector
<ValueInfo
>
5931 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef
<uint64_t> Record
) {
5932 std::vector
<ValueInfo
> Ret
;
5933 Ret
.reserve(Record
.size());
5934 for (uint64_t RefValueId
: Record
)
5935 Ret
.push_back(getValueInfoFromValueId(RefValueId
).first
);
5939 std::vector
<FunctionSummary::EdgeTy
>
5940 ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef
<uint64_t> Record
,
5941 bool IsOldProfileFormat
,
5942 bool HasProfile
, bool HasRelBF
) {
5943 std::vector
<FunctionSummary::EdgeTy
> Ret
;
5944 Ret
.reserve(Record
.size());
5945 for (unsigned I
= 0, E
= Record
.size(); I
!= E
; ++I
) {
5946 CalleeInfo::HotnessType Hotness
= CalleeInfo::HotnessType::Unknown
;
5948 ValueInfo Callee
= getValueInfoFromValueId(Record
[I
]).first
;
5949 if (IsOldProfileFormat
) {
5950 I
+= 1; // Skip old callsitecount field
5952 I
+= 1; // Skip old profilecount field
5953 } else if (HasProfile
)
5954 Hotness
= static_cast<CalleeInfo::HotnessType
>(Record
[++I
]);
5956 RelBF
= Record
[++I
];
5957 Ret
.push_back(FunctionSummary::EdgeTy
{Callee
, CalleeInfo(Hotness
, RelBF
)});
5963 parseWholeProgramDevirtResolutionByArg(ArrayRef
<uint64_t> Record
, size_t &Slot
,
5964 WholeProgramDevirtResolution
&Wpd
) {
5965 uint64_t ArgNum
= Record
[Slot
++];
5966 WholeProgramDevirtResolution::ByArg
&B
=
5967 Wpd
.ResByArg
[{Record
.begin() + Slot
, Record
.begin() + Slot
+ ArgNum
}];
5971 static_cast<WholeProgramDevirtResolution::ByArg::Kind
>(Record
[Slot
++]);
5972 B
.Info
= Record
[Slot
++];
5973 B
.Byte
= Record
[Slot
++];
5974 B
.Bit
= Record
[Slot
++];
5977 static void parseWholeProgramDevirtResolution(ArrayRef
<uint64_t> Record
,
5978 StringRef Strtab
, size_t &Slot
,
5979 TypeIdSummary
&TypeId
) {
5980 uint64_t Id
= Record
[Slot
++];
5981 WholeProgramDevirtResolution
&Wpd
= TypeId
.WPDRes
[Id
];
5983 Wpd
.TheKind
= static_cast<WholeProgramDevirtResolution::Kind
>(Record
[Slot
++]);
5984 Wpd
.SingleImplName
= {Strtab
.data() + Record
[Slot
],
5985 static_cast<size_t>(Record
[Slot
+ 1])};
5988 uint64_t ResByArgNum
= Record
[Slot
++];
5989 for (uint64_t I
= 0; I
!= ResByArgNum
; ++I
)
5990 parseWholeProgramDevirtResolutionByArg(Record
, Slot
, Wpd
);
5993 static void parseTypeIdSummaryRecord(ArrayRef
<uint64_t> Record
,
5995 ModuleSummaryIndex
&TheIndex
) {
5997 TypeIdSummary
&TypeId
= TheIndex
.getOrInsertTypeIdSummary(
5998 {Strtab
.data() + Record
[Slot
], static_cast<size_t>(Record
[Slot
+ 1])});
6001 TypeId
.TTRes
.TheKind
= static_cast<TypeTestResolution::Kind
>(Record
[Slot
++]);
6002 TypeId
.TTRes
.SizeM1BitWidth
= Record
[Slot
++];
6003 TypeId
.TTRes
.AlignLog2
= Record
[Slot
++];
6004 TypeId
.TTRes
.SizeM1
= Record
[Slot
++];
6005 TypeId
.TTRes
.BitMask
= Record
[Slot
++];
6006 TypeId
.TTRes
.InlineBits
= Record
[Slot
++];
6008 while (Slot
< Record
.size())
6009 parseWholeProgramDevirtResolution(Record
, Strtab
, Slot
, TypeId
);
6012 std::vector
<FunctionSummary::ParamAccess
>
6013 ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef
<uint64_t> Record
) {
6014 auto ReadRange
= [&]() {
6015 APInt
Lower(FunctionSummary::ParamAccess::RangeWidth
,
6016 BitcodeReader::decodeSignRotatedValue(Record
.front()));
6017 Record
= Record
.drop_front();
6018 APInt
Upper(FunctionSummary::ParamAccess::RangeWidth
,
6019 BitcodeReader::decodeSignRotatedValue(Record
.front()));
6020 Record
= Record
.drop_front();
6021 ConstantRange Range
{Lower
, Upper
};
6022 assert(!Range
.isFullSet());
6023 assert(!Range
.isUpperSignWrapped());
6027 std::vector
<FunctionSummary::ParamAccess
> PendingParamAccesses
;
6028 while (!Record
.empty()) {
6029 PendingParamAccesses
.emplace_back();
6030 FunctionSummary::ParamAccess
&ParamAccess
= PendingParamAccesses
.back();
6031 ParamAccess
.ParamNo
= Record
.front();
6032 Record
= Record
.drop_front();
6033 ParamAccess
.Use
= ReadRange();
6034 ParamAccess
.Calls
.resize(Record
.front());
6035 Record
= Record
.drop_front();
6036 for (auto &Call
: ParamAccess
.Calls
) {
6037 Call
.ParamNo
= Record
.front();
6038 Record
= Record
.drop_front();
6039 Call
.Callee
= getValueInfoFromValueId(Record
.front()).first
;
6040 Record
= Record
.drop_front();
6041 Call
.Offsets
= ReadRange();
6044 return PendingParamAccesses
;
6047 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo(
6048 ArrayRef
<uint64_t> Record
, size_t &Slot
,
6049 TypeIdCompatibleVtableInfo
&TypeId
) {
6050 uint64_t Offset
= Record
[Slot
++];
6051 ValueInfo Callee
= getValueInfoFromValueId(Record
[Slot
++]).first
;
6052 TypeId
.push_back({Offset
, Callee
});
6055 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(
6056 ArrayRef
<uint64_t> Record
) {
6058 TypeIdCompatibleVtableInfo
&TypeId
=
6059 TheIndex
.getOrInsertTypeIdCompatibleVtableSummary(
6060 {Strtab
.data() + Record
[Slot
],
6061 static_cast<size_t>(Record
[Slot
+ 1])});
6064 while (Slot
< Record
.size())
6065 parseTypeIdCompatibleVtableInfo(Record
, Slot
, TypeId
);
6068 static void setSpecialRefs(std::vector
<ValueInfo
> &Refs
, unsigned ROCnt
,
6070 // Readonly and writeonly refs are in the end of the refs list.
6071 assert(ROCnt
+ WOCnt
<= Refs
.size());
6072 unsigned FirstWORef
= Refs
.size() - WOCnt
;
6073 unsigned RefNo
= FirstWORef
- ROCnt
;
6074 for (; RefNo
< FirstWORef
; ++RefNo
)
6075 Refs
[RefNo
].setReadOnly();
6076 for (; RefNo
< Refs
.size(); ++RefNo
)
6077 Refs
[RefNo
].setWriteOnly();
6080 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
6081 // objects in the index.
6082 Error
ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID
) {
6083 if (Error Err
= Stream
.EnterSubBlock(ID
))
6085 SmallVector
<uint64_t, 64> Record
;
6089 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6091 return MaybeEntry
.takeError();
6092 BitstreamEntry Entry
= MaybeEntry
.get();
6094 if (Entry
.Kind
!= BitstreamEntry::Record
)
6095 return error("Invalid Summary Block: record for version expected");
6096 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
6098 return MaybeRecord
.takeError();
6099 if (MaybeRecord
.get() != bitc::FS_VERSION
)
6100 return error("Invalid Summary Block: version expected");
6102 const uint64_t Version
= Record
[0];
6103 const bool IsOldProfileFormat
= Version
== 1;
6104 if (Version
< 1 || Version
> ModuleSummaryIndex::BitcodeSummaryVersion
)
6105 return error("Invalid summary version " + Twine(Version
) +
6106 ". Version should be in the range [1-" +
6107 Twine(ModuleSummaryIndex::BitcodeSummaryVersion
) +
6111 // Keep around the last seen summary to be used when we see an optional
6112 // "OriginalName" attachement.
6113 GlobalValueSummary
*LastSeenSummary
= nullptr;
6114 GlobalValue::GUID LastSeenGUID
= 0;
6116 // We can expect to see any number of type ID information records before
6117 // each function summary records; these variables store the information
6118 // collected so far so that it can be used to create the summary object.
6119 std::vector
<GlobalValue::GUID
> PendingTypeTests
;
6120 std::vector
<FunctionSummary::VFuncId
> PendingTypeTestAssumeVCalls
,
6121 PendingTypeCheckedLoadVCalls
;
6122 std::vector
<FunctionSummary::ConstVCall
> PendingTypeTestAssumeConstVCalls
,
6123 PendingTypeCheckedLoadConstVCalls
;
6124 std::vector
<FunctionSummary::ParamAccess
> PendingParamAccesses
;
6127 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6129 return MaybeEntry
.takeError();
6130 BitstreamEntry Entry
= MaybeEntry
.get();
6132 switch (Entry
.Kind
) {
6133 case BitstreamEntry::SubBlock
: // Handled for us already.
6134 case BitstreamEntry::Error
:
6135 return error("Malformed block");
6136 case BitstreamEntry::EndBlock
:
6137 return Error::success();
6138 case BitstreamEntry::Record
:
6139 // The interesting case.
6143 // Read a record. The record format depends on whether this
6144 // is a per-module index or a combined index file. In the per-module
6145 // case the records contain the associated value's ID for correlation
6146 // with VST entries. In the combined index the correlation is done
6147 // via the bitcode offset of the summary records (which were saved
6148 // in the combined index VST entries). The records also contain
6149 // information used for ThinLTO renaming and importing.
6151 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
6153 return MaybeBitCode
.takeError();
6154 switch (unsigned BitCode
= MaybeBitCode
.get()) {
6155 default: // Default behavior: ignore.
6157 case bitc::FS_FLAGS
: { // [flags]
6158 TheIndex
.setFlags(Record
[0]);
6161 case bitc::FS_VALUE_GUID
: { // [valueid, refguid]
6162 uint64_t ValueID
= Record
[0];
6163 GlobalValue::GUID RefGUID
= Record
[1];
6164 ValueIdToValueInfoMap
[ValueID
] =
6165 std::make_pair(TheIndex
.getOrInsertValueInfo(RefGUID
), RefGUID
);
6168 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,
6169 // numrefs x valueid, n x (valueid)]
6170 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,
6171 // numrefs x valueid,
6172 // n x (valueid, hotness)]
6173 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,
6174 // numrefs x valueid,
6175 // n x (valueid, relblockfreq)]
6176 case bitc::FS_PERMODULE
:
6177 case bitc::FS_PERMODULE_RELBF
:
6178 case bitc::FS_PERMODULE_PROFILE
: {
6179 unsigned ValueID
= Record
[0];
6180 uint64_t RawFlags
= Record
[1];
6181 unsigned InstCount
= Record
[2];
6182 uint64_t RawFunFlags
= 0;
6183 unsigned NumRefs
= Record
[3];
6184 unsigned NumRORefs
= 0, NumWORefs
= 0;
6185 int RefListStartIndex
= 4;
6187 RawFunFlags
= Record
[3];
6188 NumRefs
= Record
[4];
6189 RefListStartIndex
= 5;
6191 NumRORefs
= Record
[5];
6192 RefListStartIndex
= 6;
6194 NumWORefs
= Record
[6];
6195 RefListStartIndex
= 7;
6200 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6201 // The module path string ref set in the summary must be owned by the
6202 // index's module string table. Since we don't have a module path
6203 // string table section in the per-module index, we create a single
6204 // module path string table entry with an empty (0) ID to take
6206 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
6207 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
6208 "Record size inconsistent with number of references");
6209 std::vector
<ValueInfo
> Refs
= makeRefList(
6210 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
6211 bool HasProfile
= (BitCode
== bitc::FS_PERMODULE_PROFILE
);
6212 bool HasRelBF
= (BitCode
== bitc::FS_PERMODULE_RELBF
);
6213 std::vector
<FunctionSummary::EdgeTy
> Calls
= makeCallList(
6214 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
6215 IsOldProfileFormat
, HasProfile
, HasRelBF
);
6216 setSpecialRefs(Refs
, NumRORefs
, NumWORefs
);
6217 auto FS
= std::make_unique
<FunctionSummary
>(
6218 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), /*EntryCount=*/0,
6219 std::move(Refs
), std::move(Calls
), std::move(PendingTypeTests
),
6220 std::move(PendingTypeTestAssumeVCalls
),
6221 std::move(PendingTypeCheckedLoadVCalls
),
6222 std::move(PendingTypeTestAssumeConstVCalls
),
6223 std::move(PendingTypeCheckedLoadConstVCalls
),
6224 std::move(PendingParamAccesses
));
6225 auto VIAndOriginalGUID
= getValueInfoFromValueId(ValueID
);
6226 FS
->setModulePath(getThisModule()->first());
6227 FS
->setOriginalName(VIAndOriginalGUID
.second
);
6228 TheIndex
.addGlobalValueSummary(VIAndOriginalGUID
.first
, std::move(FS
));
6231 // FS_ALIAS: [valueid, flags, valueid]
6232 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
6233 // they expect all aliasee summaries to be available.
6234 case bitc::FS_ALIAS
: {
6235 unsigned ValueID
= Record
[0];
6236 uint64_t RawFlags
= Record
[1];
6237 unsigned AliaseeID
= Record
[2];
6238 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6239 auto AS
= std::make_unique
<AliasSummary
>(Flags
);
6240 // The module path string ref set in the summary must be owned by the
6241 // index's module string table. Since we don't have a module path
6242 // string table section in the per-module index, we create a single
6243 // module path string table entry with an empty (0) ID to take
6245 AS
->setModulePath(getThisModule()->first());
6247 auto AliaseeVI
= getValueInfoFromValueId(AliaseeID
).first
;
6248 auto AliaseeInModule
= TheIndex
.findSummaryInModule(AliaseeVI
, ModulePath
);
6249 if (!AliaseeInModule
)
6250 return error("Alias expects aliasee summary to be parsed");
6251 AS
->setAliasee(AliaseeVI
, AliaseeInModule
);
6253 auto GUID
= getValueInfoFromValueId(ValueID
);
6254 AS
->setOriginalName(GUID
.second
);
6255 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(AS
));
6258 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid]
6259 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS
: {
6260 unsigned ValueID
= Record
[0];
6261 uint64_t RawFlags
= Record
[1];
6262 unsigned RefArrayStart
= 2;
6263 GlobalVarSummary::GVarFlags
GVF(/* ReadOnly */ false,
6264 /* WriteOnly */ false,
6265 /* Constant */ false,
6266 GlobalObject::VCallVisibilityPublic
);
6267 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6269 GVF
= getDecodedGVarFlags(Record
[2]);
6272 std::vector
<ValueInfo
> Refs
=
6273 makeRefList(ArrayRef
<uint64_t>(Record
).slice(RefArrayStart
));
6275 std::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
6276 FS
->setModulePath(getThisModule()->first());
6277 auto GUID
= getValueInfoFromValueId(ValueID
);
6278 FS
->setOriginalName(GUID
.second
);
6279 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(FS
));
6282 // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags,
6283 // numrefs, numrefs x valueid,
6284 // n x (valueid, offset)]
6285 case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS
: {
6286 unsigned ValueID
= Record
[0];
6287 uint64_t RawFlags
= Record
[1];
6288 GlobalVarSummary::GVarFlags GVF
= getDecodedGVarFlags(Record
[2]);
6289 unsigned NumRefs
= Record
[3];
6290 unsigned RefListStartIndex
= 4;
6291 unsigned VTableListStartIndex
= RefListStartIndex
+ NumRefs
;
6292 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6293 std::vector
<ValueInfo
> Refs
= makeRefList(
6294 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
6295 VTableFuncList VTableFuncs
;
6296 for (unsigned I
= VTableListStartIndex
, E
= Record
.size(); I
!= E
; ++I
) {
6297 ValueInfo Callee
= getValueInfoFromValueId(Record
[I
]).first
;
6298 uint64_t Offset
= Record
[++I
];
6299 VTableFuncs
.push_back({Callee
, Offset
});
6302 std::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
6303 VS
->setModulePath(getThisModule()->first());
6304 VS
->setVTableFuncs(VTableFuncs
);
6305 auto GUID
= getValueInfoFromValueId(ValueID
);
6306 VS
->setOriginalName(GUID
.second
);
6307 TheIndex
.addGlobalValueSummary(GUID
.first
, std::move(VS
));
6310 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,
6311 // numrefs x valueid, n x (valueid)]
6312 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,
6313 // numrefs x valueid, n x (valueid, hotness)]
6314 case bitc::FS_COMBINED
:
6315 case bitc::FS_COMBINED_PROFILE
: {
6316 unsigned ValueID
= Record
[0];
6317 uint64_t ModuleId
= Record
[1];
6318 uint64_t RawFlags
= Record
[2];
6319 unsigned InstCount
= Record
[3];
6320 uint64_t RawFunFlags
= 0;
6321 uint64_t EntryCount
= 0;
6322 unsigned NumRefs
= Record
[4];
6323 unsigned NumRORefs
= 0, NumWORefs
= 0;
6324 int RefListStartIndex
= 5;
6327 RawFunFlags
= Record
[4];
6328 RefListStartIndex
= 6;
6329 size_t NumRefsIndex
= 5;
6331 unsigned NumRORefsOffset
= 1;
6332 RefListStartIndex
= 7;
6335 EntryCount
= Record
[5];
6336 RefListStartIndex
= 8;
6338 RefListStartIndex
= 9;
6339 NumWORefs
= Record
[8];
6340 NumRORefsOffset
= 2;
6343 NumRORefs
= Record
[RefListStartIndex
- NumRORefsOffset
];
6345 NumRefs
= Record
[NumRefsIndex
];
6348 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6349 int CallGraphEdgeStartIndex
= RefListStartIndex
+ NumRefs
;
6350 assert(Record
.size() >= RefListStartIndex
+ NumRefs
&&
6351 "Record size inconsistent with number of references");
6352 std::vector
<ValueInfo
> Refs
= makeRefList(
6353 ArrayRef
<uint64_t>(Record
).slice(RefListStartIndex
, NumRefs
));
6354 bool HasProfile
= (BitCode
== bitc::FS_COMBINED_PROFILE
);
6355 std::vector
<FunctionSummary::EdgeTy
> Edges
= makeCallList(
6356 ArrayRef
<uint64_t>(Record
).slice(CallGraphEdgeStartIndex
),
6357 IsOldProfileFormat
, HasProfile
, false);
6358 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6359 setSpecialRefs(Refs
, NumRORefs
, NumWORefs
);
6360 auto FS
= std::make_unique
<FunctionSummary
>(
6361 Flags
, InstCount
, getDecodedFFlags(RawFunFlags
), EntryCount
,
6362 std::move(Refs
), std::move(Edges
), std::move(PendingTypeTests
),
6363 std::move(PendingTypeTestAssumeVCalls
),
6364 std::move(PendingTypeCheckedLoadVCalls
),
6365 std::move(PendingTypeTestAssumeConstVCalls
),
6366 std::move(PendingTypeCheckedLoadConstVCalls
),
6367 std::move(PendingParamAccesses
));
6368 LastSeenSummary
= FS
.get();
6369 LastSeenGUID
= VI
.getGUID();
6370 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
6371 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
6374 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]
6375 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
6376 // they expect all aliasee summaries to be available.
6377 case bitc::FS_COMBINED_ALIAS
: {
6378 unsigned ValueID
= Record
[0];
6379 uint64_t ModuleId
= Record
[1];
6380 uint64_t RawFlags
= Record
[2];
6381 unsigned AliaseeValueId
= Record
[3];
6382 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6383 auto AS
= std::make_unique
<AliasSummary
>(Flags
);
6384 LastSeenSummary
= AS
.get();
6385 AS
->setModulePath(ModuleIdMap
[ModuleId
]);
6387 auto AliaseeVI
= getValueInfoFromValueId(AliaseeValueId
).first
;
6388 auto AliaseeInModule
= TheIndex
.findSummaryInModule(AliaseeVI
, AS
->modulePath());
6389 AS
->setAliasee(AliaseeVI
, AliaseeInModule
);
6391 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6392 LastSeenGUID
= VI
.getGUID();
6393 TheIndex
.addGlobalValueSummary(VI
, std::move(AS
));
6396 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]
6397 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS
: {
6398 unsigned ValueID
= Record
[0];
6399 uint64_t ModuleId
= Record
[1];
6400 uint64_t RawFlags
= Record
[2];
6401 unsigned RefArrayStart
= 3;
6402 GlobalVarSummary::GVarFlags
GVF(/* ReadOnly */ false,
6403 /* WriteOnly */ false,
6404 /* Constant */ false,
6405 GlobalObject::VCallVisibilityPublic
);
6406 auto Flags
= getDecodedGVSummaryFlags(RawFlags
, Version
);
6408 GVF
= getDecodedGVarFlags(Record
[3]);
6411 std::vector
<ValueInfo
> Refs
=
6412 makeRefList(ArrayRef
<uint64_t>(Record
).slice(RefArrayStart
));
6414 std::make_unique
<GlobalVarSummary
>(Flags
, GVF
, std::move(Refs
));
6415 LastSeenSummary
= FS
.get();
6416 FS
->setModulePath(ModuleIdMap
[ModuleId
]);
6417 ValueInfo VI
= getValueInfoFromValueId(ValueID
).first
;
6418 LastSeenGUID
= VI
.getGUID();
6419 TheIndex
.addGlobalValueSummary(VI
, std::move(FS
));
6422 // FS_COMBINED_ORIGINAL_NAME: [original_name]
6423 case bitc::FS_COMBINED_ORIGINAL_NAME
: {
6424 uint64_t OriginalName
= Record
[0];
6425 if (!LastSeenSummary
)
6426 return error("Name attachment that does not follow a combined record");
6427 LastSeenSummary
->setOriginalName(OriginalName
);
6428 TheIndex
.addOriginalName(LastSeenGUID
, OriginalName
);
6429 // Reset the LastSeenSummary
6430 LastSeenSummary
= nullptr;
6434 case bitc::FS_TYPE_TESTS
:
6435 assert(PendingTypeTests
.empty());
6436 llvm::append_range(PendingTypeTests
, Record
);
6439 case bitc::FS_TYPE_TEST_ASSUME_VCALLS
:
6440 assert(PendingTypeTestAssumeVCalls
.empty());
6441 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6442 PendingTypeTestAssumeVCalls
.push_back({Record
[I
], Record
[I
+1]});
6445 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS
:
6446 assert(PendingTypeCheckedLoadVCalls
.empty());
6447 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6448 PendingTypeCheckedLoadVCalls
.push_back({Record
[I
], Record
[I
+1]});
6451 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL
:
6452 PendingTypeTestAssumeConstVCalls
.push_back(
6453 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
6456 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL
:
6457 PendingTypeCheckedLoadConstVCalls
.push_back(
6458 {{Record
[0], Record
[1]}, {Record
.begin() + 2, Record
.end()}});
6461 case bitc::FS_CFI_FUNCTION_DEFS
: {
6462 std::set
<std::string
> &CfiFunctionDefs
= TheIndex
.cfiFunctionDefs();
6463 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6464 CfiFunctionDefs
.insert(
6465 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
6469 case bitc::FS_CFI_FUNCTION_DECLS
: {
6470 std::set
<std::string
> &CfiFunctionDecls
= TheIndex
.cfiFunctionDecls();
6471 for (unsigned I
= 0; I
!= Record
.size(); I
+= 2)
6472 CfiFunctionDecls
.insert(
6473 {Strtab
.data() + Record
[I
], static_cast<size_t>(Record
[I
+ 1])});
6477 case bitc::FS_TYPE_ID
:
6478 parseTypeIdSummaryRecord(Record
, Strtab
, TheIndex
);
6481 case bitc::FS_TYPE_ID_METADATA
:
6482 parseTypeIdCompatibleVtableSummaryRecord(Record
);
6485 case bitc::FS_BLOCK_COUNT
:
6486 TheIndex
.addBlockCount(Record
[0]);
6489 case bitc::FS_PARAM_ACCESS
: {
6490 PendingParamAccesses
= parseParamAccesses(Record
);
6495 llvm_unreachable("Exit infinite loop");
6498 // Parse the module string table block into the Index.
6499 // This populates the ModulePathStringTable map in the index.
6500 Error
ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
6501 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID
))
6504 SmallVector
<uint64_t, 64> Record
;
6506 SmallString
<128> ModulePath
;
6507 ModuleSummaryIndex::ModuleInfo
*LastSeenModule
= nullptr;
6510 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6512 return MaybeEntry
.takeError();
6513 BitstreamEntry Entry
= MaybeEntry
.get();
6515 switch (Entry
.Kind
) {
6516 case BitstreamEntry::SubBlock
: // Handled for us already.
6517 case BitstreamEntry::Error
:
6518 return error("Malformed block");
6519 case BitstreamEntry::EndBlock
:
6520 return Error::success();
6521 case BitstreamEntry::Record
:
6522 // The interesting case.
6527 Expected
<unsigned> MaybeRecord
= Stream
.readRecord(Entry
.ID
, Record
);
6529 return MaybeRecord
.takeError();
6530 switch (MaybeRecord
.get()) {
6531 default: // Default behavior: ignore.
6533 case bitc::MST_CODE_ENTRY
: {
6534 // MST_ENTRY: [modid, namechar x N]
6535 uint64_t ModuleId
= Record
[0];
6537 if (convertToString(Record
, 1, ModulePath
))
6538 return error("Invalid record");
6540 LastSeenModule
= TheIndex
.addModule(ModulePath
, ModuleId
);
6541 ModuleIdMap
[ModuleId
] = LastSeenModule
->first();
6546 /// MST_CODE_HASH: [5*i32]
6547 case bitc::MST_CODE_HASH
: {
6548 if (Record
.size() != 5)
6549 return error("Invalid hash length " + Twine(Record
.size()).str());
6550 if (!LastSeenModule
)
6551 return error("Invalid hash that does not follow a module path");
6553 for (auto &Val
: Record
) {
6554 assert(!(Val
>> 32) && "Unexpected high bits set");
6555 LastSeenModule
->second
.second
[Pos
++] = Val
;
6557 // Reset LastSeenModule to avoid overriding the hash unexpectedly.
6558 LastSeenModule
= nullptr;
6563 llvm_unreachable("Exit infinite loop");
6568 // FIXME: This class is only here to support the transition to llvm::Error. It
6569 // will be removed once this transition is complete. Clients should prefer to
6570 // deal with the Error value directly, rather than converting to error_code.
6571 class BitcodeErrorCategoryType
: public std::error_category
{
6572 const char *name() const noexcept override
{
6573 return "llvm.bitcode";
6576 std::string
message(int IE
) const override
{
6577 BitcodeError E
= static_cast<BitcodeError
>(IE
);
6579 case BitcodeError::CorruptedBitcode
:
6580 return "Corrupted bitcode";
6582 llvm_unreachable("Unknown error type!");
6586 } // end anonymous namespace
6588 static ManagedStatic
<BitcodeErrorCategoryType
> ErrorCategory
;
6590 const std::error_category
&llvm::BitcodeErrorCategory() {
6591 return *ErrorCategory
;
6594 static Expected
<StringRef
> readBlobInRecord(BitstreamCursor
&Stream
,
6595 unsigned Block
, unsigned RecordID
) {
6596 if (Error Err
= Stream
.EnterSubBlock(Block
))
6597 return std::move(Err
);
6601 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6603 return MaybeEntry
.takeError();
6604 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6606 switch (Entry
.Kind
) {
6607 case BitstreamEntry::EndBlock
:
6610 case BitstreamEntry::Error
:
6611 return error("Malformed block");
6613 case BitstreamEntry::SubBlock
:
6614 if (Error Err
= Stream
.SkipBlock())
6615 return std::move(Err
);
6618 case BitstreamEntry::Record
:
6620 SmallVector
<uint64_t, 1> Record
;
6621 Expected
<unsigned> MaybeRecord
=
6622 Stream
.readRecord(Entry
.ID
, Record
, &Blob
);
6624 return MaybeRecord
.takeError();
6625 if (MaybeRecord
.get() == RecordID
)
6632 //===----------------------------------------------------------------------===//
6633 // External interface
6634 //===----------------------------------------------------------------------===//
6636 Expected
<std::vector
<BitcodeModule
>>
6637 llvm::getBitcodeModuleList(MemoryBufferRef Buffer
) {
6638 auto FOrErr
= getBitcodeFileContents(Buffer
);
6640 return FOrErr
.takeError();
6641 return std::move(FOrErr
->Mods
);
6644 Expected
<BitcodeFileContents
>
6645 llvm::getBitcodeFileContents(MemoryBufferRef Buffer
) {
6646 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6648 return StreamOrErr
.takeError();
6649 BitstreamCursor
&Stream
= *StreamOrErr
;
6651 BitcodeFileContents F
;
6653 uint64_t BCBegin
= Stream
.getCurrentByteNo();
6655 // We may be consuming bitcode from a client that leaves garbage at the end
6656 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
6657 // the end that there cannot possibly be another module, stop looking.
6658 if (BCBegin
+ 8 >= Stream
.getBitcodeBytes().size())
6661 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6663 return MaybeEntry
.takeError();
6664 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6666 switch (Entry
.Kind
) {
6667 case BitstreamEntry::EndBlock
:
6668 case BitstreamEntry::Error
:
6669 return error("Malformed block");
6671 case BitstreamEntry::SubBlock
: {
6672 uint64_t IdentificationBit
= -1ull;
6673 if (Entry
.ID
== bitc::IDENTIFICATION_BLOCK_ID
) {
6674 IdentificationBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
6675 if (Error Err
= Stream
.SkipBlock())
6676 return std::move(Err
);
6679 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6681 return MaybeEntry
.takeError();
6682 Entry
= MaybeEntry
.get();
6685 if (Entry
.Kind
!= BitstreamEntry::SubBlock
||
6686 Entry
.ID
!= bitc::MODULE_BLOCK_ID
)
6687 return error("Malformed block");
6690 if (Entry
.ID
== bitc::MODULE_BLOCK_ID
) {
6691 uint64_t ModuleBit
= Stream
.GetCurrentBitNo() - BCBegin
* 8;
6692 if (Error Err
= Stream
.SkipBlock())
6693 return std::move(Err
);
6695 F
.Mods
.push_back({Stream
.getBitcodeBytes().slice(
6696 BCBegin
, Stream
.getCurrentByteNo() - BCBegin
),
6697 Buffer
.getBufferIdentifier(), IdentificationBit
,
6702 if (Entry
.ID
== bitc::STRTAB_BLOCK_ID
) {
6703 Expected
<StringRef
> Strtab
=
6704 readBlobInRecord(Stream
, bitc::STRTAB_BLOCK_ID
, bitc::STRTAB_BLOB
);
6706 return Strtab
.takeError();
6707 // This string table is used by every preceding bitcode module that does
6708 // not have its own string table. A bitcode file may have multiple
6709 // string tables if it was created by binary concatenation, for example
6710 // with "llvm-cat -b".
6711 for (auto I
= F
.Mods
.rbegin(), E
= F
.Mods
.rend(); I
!= E
; ++I
) {
6712 if (!I
->Strtab
.empty())
6714 I
->Strtab
= *Strtab
;
6716 // Similarly, the string table is used by every preceding symbol table;
6717 // normally there will be just one unless the bitcode file was created
6718 // by binary concatenation.
6719 if (!F
.Symtab
.empty() && F
.StrtabForSymtab
.empty())
6720 F
.StrtabForSymtab
= *Strtab
;
6724 if (Entry
.ID
== bitc::SYMTAB_BLOCK_ID
) {
6725 Expected
<StringRef
> SymtabOrErr
=
6726 readBlobInRecord(Stream
, bitc::SYMTAB_BLOCK_ID
, bitc::SYMTAB_BLOB
);
6728 return SymtabOrErr
.takeError();
6730 // We can expect the bitcode file to have multiple symbol tables if it
6731 // was created by binary concatenation. In that case we silently
6732 // ignore any subsequent symbol tables, which is fine because this is a
6733 // low level function. The client is expected to notice that the number
6734 // of modules in the symbol table does not match the number of modules
6735 // in the input file and regenerate the symbol table.
6736 if (F
.Symtab
.empty())
6737 F
.Symtab
= *SymtabOrErr
;
6741 if (Error Err
= Stream
.SkipBlock())
6742 return std::move(Err
);
6745 case BitstreamEntry::Record
:
6746 if (Expected
<unsigned> StreamFailed
= Stream
.skipRecord(Entry
.ID
))
6749 return StreamFailed
.takeError();
6754 /// Get a lazy one-at-time loading module from bitcode.
6756 /// This isn't always used in a lazy context. In particular, it's also used by
6757 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull
6758 /// in forward-referenced functions from block address references.
6760 /// \param[in] MaterializeAll Set to \c true if we should materialize
6762 Expected
<std::unique_ptr
<Module
>>
6763 BitcodeModule::getModuleImpl(LLVMContext
&Context
, bool MaterializeAll
,
6764 bool ShouldLazyLoadMetadata
, bool IsImporting
,
6765 DataLayoutCallbackTy DataLayoutCallback
) {
6766 BitstreamCursor
Stream(Buffer
);
6768 std::string ProducerIdentification
;
6769 if (IdentificationBit
!= -1ull) {
6770 if (Error JumpFailed
= Stream
.JumpToBit(IdentificationBit
))
6771 return std::move(JumpFailed
);
6772 Expected
<std::string
> ProducerIdentificationOrErr
=
6773 readIdentificationBlock(Stream
);
6774 if (!ProducerIdentificationOrErr
)
6775 return ProducerIdentificationOrErr
.takeError();
6777 ProducerIdentification
= *ProducerIdentificationOrErr
;
6780 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6781 return std::move(JumpFailed
);
6782 auto *R
= new BitcodeReader(std::move(Stream
), Strtab
, ProducerIdentification
,
6785 std::unique_ptr
<Module
> M
=
6786 std::make_unique
<Module
>(ModuleIdentifier
, Context
);
6787 M
->setMaterializer(R
);
6789 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6790 if (Error Err
= R
->parseBitcodeInto(M
.get(), ShouldLazyLoadMetadata
,
6791 IsImporting
, DataLayoutCallback
))
6792 return std::move(Err
);
6794 if (MaterializeAll
) {
6795 // Read in the entire module, and destroy the BitcodeReader.
6796 if (Error Err
= M
->materializeAll())
6797 return std::move(Err
);
6799 // Resolve forward references from blockaddresses.
6800 if (Error Err
= R
->materializeForwardReferencedFunctions())
6801 return std::move(Err
);
6803 return std::move(M
);
6806 Expected
<std::unique_ptr
<Module
>>
6807 BitcodeModule::getLazyModule(LLVMContext
&Context
, bool ShouldLazyLoadMetadata
,
6809 return getModuleImpl(Context
, false, ShouldLazyLoadMetadata
, IsImporting
,
6810 [](StringRef
) { return None
; });
6813 // Parse the specified bitcode buffer and merge the index into CombinedIndex.
6814 // We don't use ModuleIdentifier here because the client may need to control the
6815 // module path used in the combined summary (e.g. when reading summaries for
6816 // regular LTO modules).
6817 Error
BitcodeModule::readSummary(ModuleSummaryIndex
&CombinedIndex
,
6818 StringRef ModulePath
, uint64_t ModuleId
) {
6819 BitstreamCursor
Stream(Buffer
);
6820 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6823 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, CombinedIndex
,
6824 ModulePath
, ModuleId
);
6825 return R
.parseModule();
6828 // Parse the specified bitcode buffer, returning the function info index.
6829 Expected
<std::unique_ptr
<ModuleSummaryIndex
>> BitcodeModule::getSummary() {
6830 BitstreamCursor
Stream(Buffer
);
6831 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6832 return std::move(JumpFailed
);
6834 auto Index
= std::make_unique
<ModuleSummaryIndex
>(/*HaveGVs=*/false);
6835 ModuleSummaryIndexBitcodeReader
R(std::move(Stream
), Strtab
, *Index
,
6836 ModuleIdentifier
, 0);
6838 if (Error Err
= R
.parseModule())
6839 return std::move(Err
);
6841 return std::move(Index
);
6844 static Expected
<bool> getEnableSplitLTOUnitFlag(BitstreamCursor
&Stream
,
6846 if (Error Err
= Stream
.EnterSubBlock(ID
))
6847 return std::move(Err
);
6848 SmallVector
<uint64_t, 64> Record
;
6851 Expected
<BitstreamEntry
> MaybeEntry
= Stream
.advanceSkippingSubblocks();
6853 return MaybeEntry
.takeError();
6854 BitstreamEntry Entry
= MaybeEntry
.get();
6856 switch (Entry
.Kind
) {
6857 case BitstreamEntry::SubBlock
: // Handled for us already.
6858 case BitstreamEntry::Error
:
6859 return error("Malformed block");
6860 case BitstreamEntry::EndBlock
:
6861 // If no flags record found, conservatively return true to mimic
6862 // behavior before this flag was added.
6864 case BitstreamEntry::Record
:
6865 // The interesting case.
6869 // Look for the FS_FLAGS record.
6871 Expected
<unsigned> MaybeBitCode
= Stream
.readRecord(Entry
.ID
, Record
);
6873 return MaybeBitCode
.takeError();
6874 switch (MaybeBitCode
.get()) {
6875 default: // Default behavior: ignore.
6877 case bitc::FS_FLAGS
: { // [flags]
6878 uint64_t Flags
= Record
[0];
6880 assert(Flags
<= 0x7f && "Unexpected bits in flag");
6886 llvm_unreachable("Exit infinite loop");
6889 // Check if the given bitcode buffer contains a global value summary block.
6890 Expected
<BitcodeLTOInfo
> BitcodeModule::getLTOInfo() {
6891 BitstreamCursor
Stream(Buffer
);
6892 if (Error JumpFailed
= Stream
.JumpToBit(ModuleBit
))
6893 return std::move(JumpFailed
);
6895 if (Error Err
= Stream
.EnterSubBlock(bitc::MODULE_BLOCK_ID
))
6896 return std::move(Err
);
6899 Expected
<llvm::BitstreamEntry
> MaybeEntry
= Stream
.advance();
6901 return MaybeEntry
.takeError();
6902 llvm::BitstreamEntry Entry
= MaybeEntry
.get();
6904 switch (Entry
.Kind
) {
6905 case BitstreamEntry::Error
:
6906 return error("Malformed block");
6907 case BitstreamEntry::EndBlock
:
6908 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/false,
6909 /*EnableSplitLTOUnit=*/false};
6911 case BitstreamEntry::SubBlock
:
6912 if (Entry
.ID
== bitc::GLOBALVAL_SUMMARY_BLOCK_ID
) {
6913 Expected
<bool> EnableSplitLTOUnit
=
6914 getEnableSplitLTOUnitFlag(Stream
, Entry
.ID
);
6915 if (!EnableSplitLTOUnit
)
6916 return EnableSplitLTOUnit
.takeError();
6917 return BitcodeLTOInfo
{/*IsThinLTO=*/true, /*HasSummary=*/true,
6918 *EnableSplitLTOUnit
};
6921 if (Entry
.ID
== bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
) {
6922 Expected
<bool> EnableSplitLTOUnit
=
6923 getEnableSplitLTOUnitFlag(Stream
, Entry
.ID
);
6924 if (!EnableSplitLTOUnit
)
6925 return EnableSplitLTOUnit
.takeError();
6926 return BitcodeLTOInfo
{/*IsThinLTO=*/false, /*HasSummary=*/true,
6927 *EnableSplitLTOUnit
};
6930 // Ignore other sub-blocks.
6931 if (Error Err
= Stream
.SkipBlock())
6932 return std::move(Err
);
6935 case BitstreamEntry::Record
:
6936 if (Expected
<unsigned> StreamFailed
= Stream
.skipRecord(Entry
.ID
))
6939 return StreamFailed
.takeError();
6944 static Expected
<BitcodeModule
> getSingleModule(MemoryBufferRef Buffer
) {
6945 Expected
<std::vector
<BitcodeModule
>> MsOrErr
= getBitcodeModuleList(Buffer
);
6947 return MsOrErr
.takeError();
6949 if (MsOrErr
->size() != 1)
6950 return error("Expected a single module");
6952 return (*MsOrErr
)[0];
6955 Expected
<std::unique_ptr
<Module
>>
6956 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer
, LLVMContext
&Context
,
6957 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
6958 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6960 return BM
.takeError();
6962 return BM
->getLazyModule(Context
, ShouldLazyLoadMetadata
, IsImporting
);
6965 Expected
<std::unique_ptr
<Module
>> llvm::getOwningLazyBitcodeModule(
6966 std::unique_ptr
<MemoryBuffer
> &&Buffer
, LLVMContext
&Context
,
6967 bool ShouldLazyLoadMetadata
, bool IsImporting
) {
6968 auto MOrErr
= getLazyBitcodeModule(*Buffer
, Context
, ShouldLazyLoadMetadata
,
6971 (*MOrErr
)->setOwnedMemoryBuffer(std::move(Buffer
));
6975 Expected
<std::unique_ptr
<Module
>>
6976 BitcodeModule::parseModule(LLVMContext
&Context
,
6977 DataLayoutCallbackTy DataLayoutCallback
) {
6978 return getModuleImpl(Context
, true, false, false, DataLayoutCallback
);
6979 // TODO: Restore the use-lists to the in-memory state when the bitcode was
6980 // written. We must defer until the Module has been fully materialized.
6983 Expected
<std::unique_ptr
<Module
>>
6984 llvm::parseBitcodeFile(MemoryBufferRef Buffer
, LLVMContext
&Context
,
6985 DataLayoutCallbackTy DataLayoutCallback
) {
6986 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
6988 return BM
.takeError();
6990 return BM
->parseModule(Context
, DataLayoutCallback
);
6993 Expected
<std::string
> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer
) {
6994 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
6996 return StreamOrErr
.takeError();
6998 return readTriple(*StreamOrErr
);
7001 Expected
<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer
) {
7002 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
7004 return StreamOrErr
.takeError();
7006 return hasObjCCategory(*StreamOrErr
);
7009 Expected
<std::string
> llvm::getBitcodeProducerString(MemoryBufferRef Buffer
) {
7010 Expected
<BitstreamCursor
> StreamOrErr
= initStream(Buffer
);
7012 return StreamOrErr
.takeError();
7014 return readIdentificationCode(*StreamOrErr
);
7017 Error
llvm::readModuleSummaryIndex(MemoryBufferRef Buffer
,
7018 ModuleSummaryIndex
&CombinedIndex
,
7019 uint64_t ModuleId
) {
7020 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
7022 return BM
.takeError();
7024 return BM
->readSummary(CombinedIndex
, BM
->getModuleIdentifier(), ModuleId
);
7027 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
7028 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer
) {
7029 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
7031 return BM
.takeError();
7033 return BM
->getSummary();
7036 Expected
<BitcodeLTOInfo
> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer
) {
7037 Expected
<BitcodeModule
> BM
= getSingleModule(Buffer
);
7039 return BM
.takeError();
7041 return BM
->getLTOInfo();
7044 Expected
<std::unique_ptr
<ModuleSummaryIndex
>>
7045 llvm::getModuleSummaryIndexForFile(StringRef Path
,
7046 bool IgnoreEmptyThinLTOIndexFile
) {
7047 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
7048 MemoryBuffer::getFileOrSTDIN(Path
);
7050 return errorCodeToError(FileOrErr
.getError());
7051 if (IgnoreEmptyThinLTOIndexFile
&& !(*FileOrErr
)->getBufferSize())
7053 return getModuleSummaryIndex(**FileOrErr
);