LLVM 19.0.0git
BitcodeReader.cpp
Go to the documentation of this file.
1//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
24#include "llvm/Config/llvm-config.h"
25#include "llvm/IR/Argument.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/AutoUpgrade.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/Comdat.h"
32#include "llvm/IR/Constant.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/DebugInfo.h"
37#include "llvm/IR/DebugLoc.h"
39#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalAlias.h"
43#include "llvm/IR/GlobalIFunc.h"
45#include "llvm/IR/GlobalValue.h"
47#include "llvm/IR/InlineAsm.h"
49#include "llvm/IR/InstrTypes.h"
50#include "llvm/IR/Instruction.h"
52#include "llvm/IR/Intrinsics.h"
53#include "llvm/IR/IntrinsicsAArch64.h"
54#include "llvm/IR/IntrinsicsARM.h"
55#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Metadata.h"
57#include "llvm/IR/Module.h"
59#include "llvm/IR/Operator.h"
60#include "llvm/IR/Type.h"
61#include "llvm/IR/Value.h"
62#include "llvm/IR/Verifier.h"
67#include "llvm/Support/Debug.h"
68#include "llvm/Support/Error.h"
73#include "llvm/Support/ModRef.h"
76#include <algorithm>
77#include <cassert>
78#include <cstddef>
79#include <cstdint>
80#include <deque>
81#include <map>
82#include <memory>
83#include <optional>
84#include <set>
85#include <string>
86#include <system_error>
87#include <tuple>
88#include <utility>
89#include <vector>
90
91using namespace llvm;
92
94 "print-summary-global-ids", cl::init(false), cl::Hidden,
96 "Print the global id for each value when reading the module summary"));
97
99 "expand-constant-exprs", cl::Hidden,
100 cl::desc(
101 "Expand constant expressions to instructions for testing purposes"));
102
103/// Load bitcode directly into RemoveDIs format (use debug records instead
104/// of debug intrinsics). UNSET is treated as FALSE, so the default action
105/// is to do nothing. Individual tools can override this to incrementally add
106/// support for the RemoveDIs format.
108 "load-bitcode-into-experimental-debuginfo-iterators", cl::Hidden,
109 cl::desc("Load bitcode directly into the new debug info format (regardless "
110 "of input format)"));
114
115namespace {
116
117enum {
118 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
119};
120
121} // end anonymous namespace
122
123static Error error(const Twine &Message) {
124 return make_error<StringError>(
125 Message, make_error_code(BitcodeError::CorruptedBitcode));
126}
127
129 if (!Stream.canSkipToPos(4))
130 return createStringError(std::errc::illegal_byte_sequence,
131 "file too small to contain bitcode header");
132 for (unsigned C : {'B', 'C'})
133 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
134 if (Res.get() != C)
135 return createStringError(std::errc::illegal_byte_sequence,
136 "file doesn't start with bitcode header");
137 } else
138 return Res.takeError();
139 for (unsigned C : {0x0, 0xC, 0xE, 0xD})
140 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) {
141 if (Res.get() != C)
142 return createStringError(std::errc::illegal_byte_sequence,
143 "file doesn't start with bitcode header");
144 } else
145 return Res.takeError();
146 return Error::success();
147}
148
150 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
151 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
152
153 if (Buffer.getBufferSize() & 3)
154 return error("Invalid bitcode signature");
155
156 // If we have a wrapper header, parse it and ignore the non-bc file contents.
157 // The magic number is 0x0B17C0DE stored in little endian.
158 if (isBitcodeWrapper(BufPtr, BufEnd))
159 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
160 return error("Invalid bitcode wrapper header");
161
162 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
163 if (Error Err = hasInvalidBitcodeHeader(Stream))
164 return std::move(Err);
165
166 return std::move(Stream);
167}
168
169/// Convert a string from a record into an std::string, return true on failure.
170template <typename StrTy>
172 StrTy &Result) {
173 if (Idx > Record.size())
174 return true;
175
176 Result.append(Record.begin() + Idx, Record.end());
177 return false;
178}
179
180// Strip all the TBAA attachment for the module.
181static void stripTBAA(Module *M) {
182 for (auto &F : *M) {
183 if (F.isMaterializable())
184 continue;
185 for (auto &I : instructions(F))
186 I.setMetadata(LLVMContext::MD_tbaa, nullptr);
187 }
188}
189
190/// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
191/// "epoch" encoded in the bitcode, and return the producer name if any.
194 return std::move(Err);
195
196 // Read all the records.
198
199 std::string ProducerIdentification;
200
201 while (true) {
202 BitstreamEntry Entry;
203 if (Error E = Stream.advance().moveInto(Entry))
204 return std::move(E);
205
206 switch (Entry.Kind) {
207 default:
209 return error("Malformed block");
211 return ProducerIdentification;
213 // The interesting case.
214 break;
215 }
216
217 // Read a record.
218 Record.clear();
219 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
220 if (!MaybeBitCode)
221 return MaybeBitCode.takeError();
222 switch (MaybeBitCode.get()) {
223 default: // Default behavior: reject
224 return error("Invalid value");
225 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
226 convertToString(Record, 0, ProducerIdentification);
227 break;
228 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
229 unsigned epoch = (unsigned)Record[0];
230 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
231 return error(
232 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
233 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
234 }
235 }
236 }
237 }
238}
239
241 // We expect a number of well-defined blocks, though we don't necessarily
242 // need to understand them all.
243 while (true) {
244 if (Stream.AtEndOfStream())
245 return "";
246
247 BitstreamEntry Entry;
248 if (Error E = Stream.advance().moveInto(Entry))
249 return std::move(E);
250
251 switch (Entry.Kind) {
254 return error("Malformed block");
255
257 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
258 return readIdentificationBlock(Stream);
259
260 // Ignore other sub-blocks.
261 if (Error Err = Stream.SkipBlock())
262 return std::move(Err);
263 continue;
265 if (Error E = Stream.skipRecord(Entry.ID).takeError())
266 return std::move(E);
267 continue;
268 }
269 }
270}
271
273 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
274 return std::move(Err);
275
277 // Read all the records for this module.
278
279 while (true) {
281 if (!MaybeEntry)
282 return MaybeEntry.takeError();
283 BitstreamEntry Entry = MaybeEntry.get();
284
285 switch (Entry.Kind) {
286 case BitstreamEntry::SubBlock: // Handled for us already.
288 return error("Malformed block");
290 return false;
292 // The interesting case.
293 break;
294 }
295
296 // Read a record.
297 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
298 if (!MaybeRecord)
299 return MaybeRecord.takeError();
300 switch (MaybeRecord.get()) {
301 default:
302 break; // Default behavior, ignore unknown content.
303 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
304 std::string S;
305 if (convertToString(Record, 0, S))
306 return error("Invalid section name record");
307 // Check for the i386 and other (x86_64, ARM) conventions
308 if (S.find("__DATA,__objc_catlist") != std::string::npos ||
309 S.find("__OBJC,__category") != std::string::npos ||
310 S.find("__TEXT,__swift") != std::string::npos)
311 return true;
312 break;
313 }
314 }
315 Record.clear();
316 }
317 llvm_unreachable("Exit infinite loop");
318}
319
321 // We expect a number of well-defined blocks, though we don't necessarily
322 // need to understand them all.
323 while (true) {
324 BitstreamEntry Entry;
325 if (Error E = Stream.advance().moveInto(Entry))
326 return std::move(E);
327
328 switch (Entry.Kind) {
330 return error("Malformed block");
332 return false;
333
335 if (Entry.ID == bitc::MODULE_BLOCK_ID)
336 return hasObjCCategoryInModule(Stream);
337
338 // Ignore other sub-blocks.
339 if (Error Err = Stream.SkipBlock())
340 return std::move(Err);
341 continue;
342
344 if (Error E = Stream.skipRecord(Entry.ID).takeError())
345 return std::move(E);
346 continue;
347 }
348 }
349}
350
352 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
353 return std::move(Err);
354
356
357 std::string Triple;
358
359 // Read all the records for this module.
360 while (true) {
362 if (!MaybeEntry)
363 return MaybeEntry.takeError();
364 BitstreamEntry Entry = MaybeEntry.get();
365
366 switch (Entry.Kind) {
367 case BitstreamEntry::SubBlock: // Handled for us already.
369 return error("Malformed block");
371 return Triple;
373 // The interesting case.
374 break;
375 }
376
377 // Read a record.
378 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
379 if (!MaybeRecord)
380 return MaybeRecord.takeError();
381 switch (MaybeRecord.get()) {
382 default: break; // Default behavior, ignore unknown content.
383 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
384 std::string S;
385 if (convertToString(Record, 0, S))
386 return error("Invalid triple record");
387 Triple = S;
388 break;
389 }
390 }
391 Record.clear();
392 }
393 llvm_unreachable("Exit infinite loop");
394}
395
397 // We expect a number of well-defined blocks, though we don't necessarily
398 // need to understand them all.
399 while (true) {
400 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
401 if (!MaybeEntry)
402 return MaybeEntry.takeError();
403 BitstreamEntry Entry = MaybeEntry.get();
404
405 switch (Entry.Kind) {
407 return error("Malformed block");
409 return "";
410
412 if (Entry.ID == bitc::MODULE_BLOCK_ID)
413 return readModuleTriple(Stream);
414
415 // Ignore other sub-blocks.
416 if (Error Err = Stream.SkipBlock())
417 return std::move(Err);
418 continue;
419
421 if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
422 continue;
423 else
424 return Skipped.takeError();
425 }
426 }
427}
428
429namespace {
430
431class BitcodeReaderBase {
432protected:
433 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
434 : Stream(std::move(Stream)), Strtab(Strtab) {
435 this->Stream.setBlockInfo(&BlockInfo);
436 }
437
438 BitstreamBlockInfo BlockInfo;
439 BitstreamCursor Stream;
440 StringRef Strtab;
441
442 /// In version 2 of the bitcode we store names of global values and comdats in
443 /// a string table rather than in the VST.
444 bool UseStrtab = false;
445
446 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
447
448 /// If this module uses a string table, pop the reference to the string table
449 /// and return the referenced string and the rest of the record. Otherwise
450 /// just return the record itself.
451 std::pair<StringRef, ArrayRef<uint64_t>>
452 readNameFromStrtab(ArrayRef<uint64_t> Record);
453
454 Error readBlockInfo();
455
456 // Contains an arbitrary and optional string identifying the bitcode producer
457 std::string ProducerIdentification;
458
459 Error error(const Twine &Message);
460};
461
462} // end anonymous namespace
463
464Error BitcodeReaderBase::error(const Twine &Message) {
465 std::string FullMsg = Message.str();
466 if (!ProducerIdentification.empty())
467 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
468 LLVM_VERSION_STRING "')";
469 return ::error(FullMsg);
470}
471
473BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
474 if (Record.empty())
475 return error("Invalid version record");
476 unsigned ModuleVersion = Record[0];
477 if (ModuleVersion > 2)
478 return error("Invalid value");
479 UseStrtab = ModuleVersion >= 2;
480 return ModuleVersion;
481}
482
483std::pair<StringRef, ArrayRef<uint64_t>>
484BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
485 if (!UseStrtab)
486 return {"", Record};
487 // Invalid reference. Let the caller complain about the record being empty.
488 if (Record[0] + Record[1] > Strtab.size())
489 return {"", {}};
490 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
491}
492
493namespace {
494
495/// This represents a constant expression or constant aggregate using a custom
496/// structure internal to the bitcode reader. Later, this structure will be
497/// expanded by materializeValue() either into a constant expression/aggregate,
498/// or into an instruction sequence at the point of use. This allows us to
499/// upgrade bitcode using constant expressions even if this kind of constant
500/// expression is no longer supported.
501class BitcodeConstant final : public Value,
502 TrailingObjects<BitcodeConstant, unsigned> {
503 friend TrailingObjects;
504
505 // Value subclass ID: Pick largest possible value to avoid any clashes.
506 static constexpr uint8_t SubclassID = 255;
507
508public:
509 // Opcodes used for non-expressions. This includes constant aggregates
510 // (struct, array, vector) that might need expansion, as well as non-leaf
511 // constants that don't need expansion (no_cfi, dso_local, blockaddress),
512 // but still go through BitcodeConstant to avoid different uselist orders
513 // between the two cases.
514 static constexpr uint8_t ConstantStructOpcode = 255;
515 static constexpr uint8_t ConstantArrayOpcode = 254;
516 static constexpr uint8_t ConstantVectorOpcode = 253;
517 static constexpr uint8_t NoCFIOpcode = 252;
518 static constexpr uint8_t DSOLocalEquivalentOpcode = 251;
519 static constexpr uint8_t BlockAddressOpcode = 250;
520 static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode;
521
522 // Separate struct to make passing different number of parameters to
523 // BitcodeConstant::create() more convenient.
524 struct ExtraInfo {
525 uint8_t Opcode;
526 uint8_t Flags;
527 unsigned BlockAddressBB = 0;
528 Type *SrcElemTy = nullptr;
529 std::optional<ConstantRange> InRange;
530
531 ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, Type *SrcElemTy = nullptr,
532 std::optional<ConstantRange> InRange = std::nullopt)
533 : Opcode(Opcode), Flags(Flags), SrcElemTy(SrcElemTy),
534 InRange(std::move(InRange)) {}
535
536 ExtraInfo(uint8_t Opcode, uint8_t Flags, unsigned BlockAddressBB)
537 : Opcode(Opcode), Flags(Flags), BlockAddressBB(BlockAddressBB) {}
538 };
539
540 uint8_t Opcode;
541 uint8_t Flags;
542 unsigned NumOperands;
543 unsigned BlockAddressBB;
544 Type *SrcElemTy; // GEP source element type.
545 std::optional<ConstantRange> InRange; // GEP inrange attribute.
546
547private:
548 BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs)
549 : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags),
550 NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB),
551 SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) {
552 std::uninitialized_copy(OpIDs.begin(), OpIDs.end(),
553 getTrailingObjects<unsigned>());
554 }
555
556 BitcodeConstant &operator=(const BitcodeConstant &) = delete;
557
558public:
559 static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty,
560 const ExtraInfo &Info,
561 ArrayRef<unsigned> OpIDs) {
562 void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()),
563 alignof(BitcodeConstant));
564 return new (Mem) BitcodeConstant(Ty, Info, OpIDs);
565 }
566
567 static bool classof(const Value *V) { return V->getValueID() == SubclassID; }
568
569 ArrayRef<unsigned> getOperandIDs() const {
570 return ArrayRef(getTrailingObjects<unsigned>(), NumOperands);
571 }
572
573 std::optional<ConstantRange> getInRange() const {
574 assert(Opcode == Instruction::GetElementPtr);
575 return InRange;
576 }
577
578 const char *getOpcodeName() const {
579 return Instruction::getOpcodeName(Opcode);
580 }
581};
582
583class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
585 Module *TheModule = nullptr;
586 // Next offset to start scanning for lazy parsing of function bodies.
587 uint64_t NextUnreadBit = 0;
588 // Last function offset found in the VST.
589 uint64_t LastFunctionBlockBit = 0;
590 bool SeenValueSymbolTable = false;
591 uint64_t VSTOffset = 0;
592
593 std::vector<std::string> SectionTable;
594 std::vector<std::string> GCTable;
595
596 std::vector<Type *> TypeList;
597 /// Track type IDs of contained types. Order is the same as the contained
598 /// types of a Type*. This is used during upgrades of typed pointer IR in
599 /// opaque pointer mode.
601 /// In some cases, we need to create a type ID for a type that was not
602 /// explicitly encoded in the bitcode, or we don't know about at the current
603 /// point. For example, a global may explicitly encode the value type ID, but
604 /// not have a type ID for the pointer to value type, for which we create a
605 /// virtual type ID instead. This map stores the new type ID that was created
606 /// for the given pair of Type and contained type ID.
607 DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs;
608 DenseMap<Function *, unsigned> FunctionTypeIDs;
609 /// Allocator for BitcodeConstants. This should come before ValueList,
610 /// because the ValueList might hold ValueHandles to these constants, so
611 /// ValueList must be destroyed before Alloc.
613 BitcodeReaderValueList ValueList;
614 std::optional<MetadataLoader> MDLoader;
615 std::vector<Comdat *> ComdatList;
616 DenseSet<GlobalObject *> ImplicitComdatObjects;
617 SmallVector<Instruction *, 64> InstructionList;
618
619 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
620 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits;
621
622 struct FunctionOperandInfo {
623 Function *F;
624 unsigned PersonalityFn;
625 unsigned Prefix;
626 unsigned Prologue;
627 };
628 std::vector<FunctionOperandInfo> FunctionOperands;
629
630 /// The set of attributes by index. Index zero in the file is for null, and
631 /// is thus not represented here. As such all indices are off by one.
632 std::vector<AttributeList> MAttributes;
633
634 /// The set of attribute groups.
635 std::map<unsigned, AttributeList> MAttributeGroups;
636
637 /// While parsing a function body, this is a list of the basic blocks for the
638 /// function.
639 std::vector<BasicBlock*> FunctionBBs;
640
641 // When reading the module header, this list is populated with functions that
642 // have bodies later in the file.
643 std::vector<Function*> FunctionsWithBodies;
644
645 // When intrinsic functions are encountered which require upgrading they are
646 // stored here with their replacement function.
647 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
648 UpdatedIntrinsicMap UpgradedIntrinsics;
649
650 // Several operations happen after the module header has been read, but
651 // before function bodies are processed. This keeps track of whether
652 // we've done this yet.
653 bool SeenFirstFunctionBody = false;
654
655 /// When function bodies are initially scanned, this map contains info about
656 /// where to find deferred function body in the stream.
657 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
658
659 /// When Metadata block is initially scanned when parsing the module, we may
660 /// choose to defer parsing of the metadata. This vector contains info about
661 /// which Metadata blocks are deferred.
662 std::vector<uint64_t> DeferredMetadataInfo;
663
664 /// These are basic blocks forward-referenced by block addresses. They are
665 /// inserted lazily into functions when they're loaded. The basic block ID is
666 /// its index into the vector.
668 std::deque<Function *> BasicBlockFwdRefQueue;
669
670 /// These are Functions that contain BlockAddresses which refer a different
671 /// Function. When parsing the different Function, queue Functions that refer
672 /// to the different Function. Those Functions must be materialized in order
673 /// to resolve their BlockAddress constants before the different Function
674 /// gets moved into another Module.
675 std::vector<Function *> BackwardRefFunctions;
676
677 /// Indicates that we are using a new encoding for instruction operands where
678 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
679 /// instruction number, for a more compact encoding. Some instruction
680 /// operands are not relative to the instruction ID: basic block numbers, and
681 /// types. Once the old style function blocks have been phased out, we would
682 /// not need this flag.
683 bool UseRelativeIDs = false;
684
685 /// True if all functions will be materialized, negating the need to process
686 /// (e.g.) blockaddress forward references.
687 bool WillMaterializeAllForwardRefs = false;
688
689 /// Tracks whether we have seen debug intrinsics or records in this bitcode;
690 /// seeing both in a single module is currently a fatal error.
691 bool SeenDebugIntrinsic = false;
692 bool SeenDebugRecord = false;
693
694 bool StripDebugInfo = false;
695 TBAAVerifier TBAAVerifyHelper;
696
697 std::vector<std::string> BundleTags;
699
700 std::optional<ValueTypeCallbackTy> ValueTypeCallback;
701
702public:
703 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
704 StringRef ProducerIdentification, LLVMContext &Context);
705
706 Error materializeForwardReferencedFunctions();
707
708 Error materialize(GlobalValue *GV) override;
709 Error materializeModule() override;
710 std::vector<StructType *> getIdentifiedStructTypes() const override;
711
712 /// Main interface to parsing a bitcode buffer.
713 /// \returns true if an error occurred.
714 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
715 bool IsImporting, ParserCallbacks Callbacks = {});
716
718
719 /// Materialize any deferred Metadata block.
720 Error materializeMetadata() override;
721
722 void setStripDebugInfo() override;
723
724private:
725 std::vector<StructType *> IdentifiedStructTypes;
726 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
727 StructType *createIdentifiedStructType(LLVMContext &Context);
728
729 static constexpr unsigned InvalidTypeID = ~0u;
730
731 Type *getTypeByID(unsigned ID);
732 Type *getPtrElementTypeByID(unsigned ID);
733 unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0);
734 unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {});
735
736 void callValueTypeCallback(Value *F, unsigned TypeID);
737 Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB);
738 Expected<Constant *> getValueForInitializer(unsigned ID);
739
740 Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID,
741 BasicBlock *ConstExprInsertBB) {
742 if (Ty && Ty->isMetadataTy())
743 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
744 return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB);
745 }
746
747 Metadata *getFnMetadataByID(unsigned ID) {
748 return MDLoader->getMetadataFwdRefOrLoad(ID);
749 }
750
751 BasicBlock *getBasicBlock(unsigned ID) const {
752 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
753 return FunctionBBs[ID];
754 }
755
756 AttributeList getAttributes(unsigned i) const {
757 if (i-1 < MAttributes.size())
758 return MAttributes[i-1];
759 return AttributeList();
760 }
761
762 /// Read a value/type pair out of the specified record from slot 'Slot'.
763 /// Increment Slot past the number of slots used in the record. Return true on
764 /// failure.
765 bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
766 unsigned InstNum, Value *&ResVal, unsigned &TypeID,
767 BasicBlock *ConstExprInsertBB) {
768 if (Slot == Record.size()) return true;
769 unsigned ValNo = (unsigned)Record[Slot++];
770 // Adjust the ValNo, if it was encoded relative to the InstNum.
771 if (UseRelativeIDs)
772 ValNo = InstNum - ValNo;
773 if (ValNo < InstNum) {
774 // If this is not a forward reference, just return the value we already
775 // have.
776 TypeID = ValueList.getTypeID(ValNo);
777 ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB);
778 assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) &&
779 "Incorrect type ID stored for value");
780 return ResVal == nullptr;
781 }
782 if (Slot == Record.size())
783 return true;
784
785 TypeID = (unsigned)Record[Slot++];
786 ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID,
787 ConstExprInsertBB);
788 return ResVal == nullptr;
789 }
790
791 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
792 /// past the number of slots used by the value in the record. Return true if
793 /// there is an error.
794 bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
795 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,
796 BasicBlock *ConstExprInsertBB) {
797 if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB))
798 return true;
799 // All values currently take a single record slot.
800 ++Slot;
801 return false;
802 }
803
804 /// Like popValue, but does not increment the Slot number.
805 bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
806 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,
807 BasicBlock *ConstExprInsertBB) {
808 ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB);
809 return ResVal == nullptr;
810 }
811
812 /// Version of getValue that returns ResVal directly, or 0 if there is an
813 /// error.
814 Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
815 unsigned InstNum, Type *Ty, unsigned TyID,
816 BasicBlock *ConstExprInsertBB) {
817 if (Slot == Record.size()) return nullptr;
818 unsigned ValNo = (unsigned)Record[Slot];
819 // Adjust the ValNo, if it was encoded relative to the InstNum.
820 if (UseRelativeIDs)
821 ValNo = InstNum - ValNo;
822 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB);
823 }
824
825 /// Like getValue, but decodes signed VBRs.
826 Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
827 unsigned InstNum, Type *Ty, unsigned TyID,
828 BasicBlock *ConstExprInsertBB) {
829 if (Slot == Record.size()) return nullptr;
830 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
831 // Adjust the ValNo, if it was encoded relative to the InstNum.
832 if (UseRelativeIDs)
833 ValNo = InstNum - ValNo;
834 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB);
835 }
836
838 unsigned &OpNum) {
839 if (Record.size() - OpNum < 3)
840 return error("Too few records for range");
841 unsigned BitWidth = Record[OpNum++];
842 if (BitWidth > 64) {
843 unsigned LowerActiveWords = Record[OpNum];
844 unsigned UpperActiveWords = Record[OpNum++] >> 32;
845 if (Record.size() - OpNum < LowerActiveWords + UpperActiveWords)
846 return error("Too few records for range");
847 APInt Lower =
848 readWideAPInt(ArrayRef(&Record[OpNum], LowerActiveWords), BitWidth);
849 OpNum += LowerActiveWords;
850 APInt Upper =
851 readWideAPInt(ArrayRef(&Record[OpNum], UpperActiveWords), BitWidth);
852 OpNum += UpperActiveWords;
853 return ConstantRange(Lower, Upper);
854 } else {
855 int64_t Start = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]);
856 int64_t End = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]);
857 return ConstantRange(APInt(BitWidth, Start), APInt(BitWidth, End));
858 }
859 }
860
861 /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
862 /// corresponding argument's pointee type. Also upgrades intrinsics that now
863 /// require an elementtype attribute.
864 Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
865
866 /// Converts alignment exponent (i.e. power of two (or zero)) to the
867 /// corresponding alignment to use. If alignment is too large, returns
868 /// a corresponding error code.
869 Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment);
870 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
871 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false,
872 ParserCallbacks Callbacks = {});
873
874 Error parseComdatRecord(ArrayRef<uint64_t> Record);
875 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
876 Error parseFunctionRecord(ArrayRef<uint64_t> Record);
877 Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
879
880 Error parseAttributeBlock();
881 Error parseAttributeGroupBlock();
882 Error parseTypeTable();
883 Error parseTypeTableBody();
884 Error parseOperandBundleTags();
885 Error parseSyncScopeNames();
886
888 unsigned NameIndex, Triple &TT);
889 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
891 Error parseValueSymbolTable(uint64_t Offset = 0);
892 Error parseGlobalValueSymbolTable();
893 Error parseConstants();
894 Error rememberAndSkipFunctionBodies();
895 Error rememberAndSkipFunctionBody();
896 /// Save the positions of the Metadata blocks and skip parsing the blocks.
897 Error rememberAndSkipMetadata();
898 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
899 Error parseFunctionBody(Function *F);
900 Error globalCleanup();
901 Error resolveGlobalAndIndirectSymbolInits();
902 Error parseUseLists();
903 Error findFunctionInStream(
904 Function *F,
905 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
906
907 SyncScope::ID getDecodedSyncScopeID(unsigned Val);
908};
909
910/// Class to manage reading and parsing function summary index bitcode
911/// files/sections.
912class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
913 /// The module index built during parsing.
914 ModuleSummaryIndex &TheIndex;
915
916 /// Indicates whether we have encountered a global value summary section
917 /// yet during parsing.
918 bool SeenGlobalValSummary = false;
919
920 /// Indicates whether we have already parsed the VST, used for error checking.
921 bool SeenValueSymbolTable = false;
922
923 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
924 /// Used to enable on-demand parsing of the VST.
925 uint64_t VSTOffset = 0;
926
927 // Map to save ValueId to ValueInfo association that was recorded in the
928 // ValueSymbolTable. It is used after the VST is parsed to convert
929 // call graph edges read from the function summary from referencing
930 // callees by their ValueId to using the ValueInfo instead, which is how
931 // they are recorded in the summary index being built.
932 // We save a GUID which refers to the same global as the ValueInfo, but
933 // ignoring the linkage, i.e. for values other than local linkage they are
934 // identical (this is the second tuple member).
935 // The third tuple member is the real GUID of the ValueInfo.
937 std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>>
938 ValueIdToValueInfoMap;
939
940 /// Map populated during module path string table parsing, from the
941 /// module ID to a string reference owned by the index's module
942 /// path string table, used to correlate with combined index
943 /// summary records.
945
946 /// Original source file name recorded in a bitcode record.
947 std::string SourceFileName;
948
949 /// The string identifier given to this module by the client, normally the
950 /// path to the bitcode file.
951 StringRef ModulePath;
952
953 /// Callback to ask whether a symbol is the prevailing copy when invoked
954 /// during combined index building.
955 std::function<bool(GlobalValue::GUID)> IsPrevailing;
956
957 /// Saves the stack ids from the STACK_IDS record to consult when adding stack
958 /// ids from the lists in the callsite and alloc entries to the index.
959 std::vector<uint64_t> StackIds;
960
961public:
962 ModuleSummaryIndexBitcodeReader(
963 BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex,
964 StringRef ModulePath,
965 std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr);
966
968
969private:
970 void setValueGUID(uint64_t ValueID, StringRef ValueName,
972 StringRef SourceFileName);
973 Error parseValueSymbolTable(
976 std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record);
977 std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
978 bool IsOldProfileFormat,
979 bool HasProfile,
980 bool HasRelBF);
981 Error parseEntireSummary(unsigned ID);
982 Error parseModuleStringTable();
983 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
984 void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,
986 std::vector<FunctionSummary::ParamAccess>
987 parseParamAccesses(ArrayRef<uint64_t> Record);
988
989 template <bool AllowNullValueInfo = false>
990 std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>
991 getValueInfoFromValueId(unsigned ValueId);
992
993 void addThisModule();
994 ModuleSummaryIndex::ModuleInfo *getThisModule();
995};
996
997} // end anonymous namespace
998
1000 Error Err) {
1001 if (Err) {
1002 std::error_code EC;
1003 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
1004 EC = EIB.convertToErrorCode();
1005 Ctx.emitError(EIB.message());
1006 });
1007 return EC;
1008 }
1009 return std::error_code();
1010}
1011
1012BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
1013 StringRef ProducerIdentification,
1014 LLVMContext &Context)
1015 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
1016 ValueList(this->Stream.SizeInBytes(),
1017 [this](unsigned ValID, BasicBlock *InsertBB) {
1018 return materializeValue(ValID, InsertBB);
1019 }) {
1020 this->ProducerIdentification = std::string(ProducerIdentification);
1021}
1022
1023Error BitcodeReader::materializeForwardReferencedFunctions() {
1024 if (WillMaterializeAllForwardRefs)
1025 return Error::success();
1026
1027 // Prevent recursion.
1028 WillMaterializeAllForwardRefs = true;
1029
1030 while (!BasicBlockFwdRefQueue.empty()) {
1031 Function *F = BasicBlockFwdRefQueue.front();
1032 BasicBlockFwdRefQueue.pop_front();
1033 assert(F && "Expected valid function");
1034 if (!BasicBlockFwdRefs.count(F))
1035 // Already materialized.
1036 continue;
1037
1038 // Check for a function that isn't materializable to prevent an infinite
1039 // loop. When parsing a blockaddress stored in a global variable, there
1040 // isn't a trivial way to check if a function will have a body without a
1041 // linear search through FunctionsWithBodies, so just check it here.
1042 if (!F->isMaterializable())
1043 return error("Never resolved function from blockaddress");
1044
1045 // Try to materialize F.
1046 if (Error Err = materialize(F))
1047 return Err;
1048 }
1049 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
1050
1051 for (Function *F : BackwardRefFunctions)
1052 if (Error Err = materialize(F))
1053 return Err;
1054 BackwardRefFunctions.clear();
1055
1056 // Reset state.
1057 WillMaterializeAllForwardRefs = false;
1058 return Error::success();
1059}
1060
1061//===----------------------------------------------------------------------===//
1062// Helper functions to implement forward reference resolution, etc.
1063//===----------------------------------------------------------------------===//
1064
1065static bool hasImplicitComdat(size_t Val) {
1066 switch (Val) {
1067 default:
1068 return false;
1069 case 1: // Old WeakAnyLinkage
1070 case 4: // Old LinkOnceAnyLinkage
1071 case 10: // Old WeakODRLinkage
1072 case 11: // Old LinkOnceODRLinkage
1073 return true;
1074 }
1075}
1076
1078 switch (Val) {
1079 default: // Map unknown/new linkages to external
1080 case 0:
1082 case 2:
1084 case 3:
1086 case 5:
1087 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
1088 case 6:
1089 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
1090 case 7:
1092 case 8:
1094 case 9:
1096 case 12:
1098 case 13:
1099 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
1100 case 14:
1101 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
1102 case 15:
1103 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
1104 case 1: // Old value with implicit comdat.
1105 case 16:
1107 case 10: // Old value with implicit comdat.
1108 case 17:
1110 case 4: // Old value with implicit comdat.
1111 case 18:
1113 case 11: // Old value with implicit comdat.
1114 case 19:
1116 }
1117}
1118
1121 Flags.ReadNone = RawFlags & 0x1;
1122 Flags.ReadOnly = (RawFlags >> 1) & 0x1;
1123 Flags.NoRecurse = (RawFlags >> 2) & 0x1;
1124 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
1125 Flags.NoInline = (RawFlags >> 4) & 0x1;
1126 Flags.AlwaysInline = (RawFlags >> 5) & 0x1;
1127 Flags.NoUnwind = (RawFlags >> 6) & 0x1;
1128 Flags.MayThrow = (RawFlags >> 7) & 0x1;
1129 Flags.HasUnknownCall = (RawFlags >> 8) & 0x1;
1130 Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1;
1131 return Flags;
1132}
1133
1134// Decode the flags for GlobalValue in the summary. The bits for each attribute:
1135//
1136// linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7,
1137// visibility: [8, 10).
1139 uint64_t Version) {
1140 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
1141 // like getDecodedLinkage() above. Any future change to the linkage enum and
1142 // to getDecodedLinkage() will need to be taken into account here as above.
1143 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
1144 auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits
1145 auto IK = GlobalValueSummary::ImportKind((RawFlags >> 10) & 1); // 1 bit
1146 RawFlags = RawFlags >> 4;
1147 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
1148 // The Live flag wasn't introduced until version 3. For dead stripping
1149 // to work correctly on earlier versions, we must conservatively treat all
1150 // values as live.
1151 bool Live = (RawFlags & 0x2) || Version < 3;
1152 bool Local = (RawFlags & 0x4);
1153 bool AutoHide = (RawFlags & 0x8);
1154
1155 return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport,
1156 Live, Local, AutoHide, IK);
1157}
1158
1159// Decode the flags for GlobalVariable in the summary
1162 (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false,
1163 (RawFlags & 0x4) ? true : false,
1164 (GlobalObject::VCallVisibility)(RawFlags >> 3));
1165}
1166
1167static std::pair<CalleeInfo::HotnessType, bool>
1169 CalleeInfo::HotnessType Hotness =
1170 static_cast<CalleeInfo::HotnessType>(RawFlags & 0x7); // 3 bits
1171 bool HasTailCall = (RawFlags & 0x8); // 1 bit
1172 return {Hotness, HasTailCall};
1173}
1174
1175static void getDecodedRelBFCallEdgeInfo(uint64_t RawFlags, uint64_t &RelBF,
1176 bool &HasTailCall) {
1177 static constexpr uint64_t RelBlockFreqMask =
1179 RelBF = RawFlags & RelBlockFreqMask; // RelBlockFreqBits bits
1180 HasTailCall = (RawFlags & (1 << CalleeInfo::RelBlockFreqBits)); // 1 bit
1181}
1182
1184 switch (Val) {
1185 default: // Map unknown visibilities to default.
1186 case 0: return GlobalValue::DefaultVisibility;
1187 case 1: return GlobalValue::HiddenVisibility;
1188 case 2: return GlobalValue::ProtectedVisibility;
1189 }
1190}
1191
1194 switch (Val) {
1195 default: // Map unknown values to default.
1196 case 0: return GlobalValue::DefaultStorageClass;
1199 }
1200}
1201
1202static bool getDecodedDSOLocal(unsigned Val) {
1203 switch(Val) {
1204 default: // Map unknown values to preemptable.
1205 case 0: return false;
1206 case 1: return true;
1207 }
1208}
1209
1210static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) {
1211 switch (Val) {
1212 case 1:
1213 return CodeModel::Tiny;
1214 case 2:
1215 return CodeModel::Small;
1216 case 3:
1217 return CodeModel::Kernel;
1218 case 4:
1219 return CodeModel::Medium;
1220 case 5:
1221 return CodeModel::Large;
1222 }
1223
1224 return {};
1225}
1226
1228 switch (Val) {
1229 case 0: return GlobalVariable::NotThreadLocal;
1230 default: // Map unknown non-zero value to general dynamic.
1231 case 1: return GlobalVariable::GeneralDynamicTLSModel;
1232 case 2: return GlobalVariable::LocalDynamicTLSModel;
1233 case 3: return GlobalVariable::InitialExecTLSModel;
1234 case 4: return GlobalVariable::LocalExecTLSModel;
1235 }
1236}
1237
1239 switch (Val) {
1240 default: // Map unknown to UnnamedAddr::None.
1241 case 0: return GlobalVariable::UnnamedAddr::None;
1242 case 1: return GlobalVariable::UnnamedAddr::Global;
1243 case 2: return GlobalVariable::UnnamedAddr::Local;
1244 }
1245}
1246
1247static int getDecodedCastOpcode(unsigned Val) {
1248 switch (Val) {
1249 default: return -1;
1250 case bitc::CAST_TRUNC : return Instruction::Trunc;
1251 case bitc::CAST_ZEXT : return Instruction::ZExt;
1252 case bitc::CAST_SEXT : return Instruction::SExt;
1253 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
1254 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
1255 case bitc::CAST_UITOFP : return Instruction::UIToFP;
1256 case bitc::CAST_SITOFP : return Instruction::SIToFP;
1257 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
1258 case bitc::CAST_FPEXT : return Instruction::FPExt;
1259 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
1260 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
1261 case bitc::CAST_BITCAST : return Instruction::BitCast;
1262 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
1263 }
1264}
1265
1266static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
1267 bool IsFP = Ty->isFPOrFPVectorTy();
1268 // UnOps are only valid for int/fp or vector of int/fp types
1269 if (!IsFP && !Ty->isIntOrIntVectorTy())
1270 return -1;
1271
1272 switch (Val) {
1273 default:
1274 return -1;
1275 case bitc::UNOP_FNEG:
1276 return IsFP ? Instruction::FNeg : -1;
1277 }
1278}
1279
1280static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
1281 bool IsFP = Ty->isFPOrFPVectorTy();
1282 // BinOps are only valid for int/fp or vector of int/fp types
1283 if (!IsFP && !Ty->isIntOrIntVectorTy())
1284 return -1;
1285
1286 switch (Val) {
1287 default:
1288 return -1;
1289 case bitc::BINOP_ADD:
1290 return IsFP ? Instruction::FAdd : Instruction::Add;
1291 case bitc::BINOP_SUB:
1292 return IsFP ? Instruction::FSub : Instruction::Sub;
1293 case bitc::BINOP_MUL:
1294 return IsFP ? Instruction::FMul : Instruction::Mul;
1295 case bitc::BINOP_UDIV:
1296 return IsFP ? -1 : Instruction::UDiv;
1297 case bitc::BINOP_SDIV:
1298 return IsFP ? Instruction::FDiv : Instruction::SDiv;
1299 case bitc::BINOP_UREM:
1300 return IsFP ? -1 : Instruction::URem;
1301 case bitc::BINOP_SREM:
1302 return IsFP ? Instruction::FRem : Instruction::SRem;
1303 case bitc::BINOP_SHL:
1304 return IsFP ? -1 : Instruction::Shl;
1305 case bitc::BINOP_LSHR:
1306 return IsFP ? -1 : Instruction::LShr;
1307 case bitc::BINOP_ASHR:
1308 return IsFP ? -1 : Instruction::AShr;
1309 case bitc::BINOP_AND:
1310 return IsFP ? -1 : Instruction::And;
1311 case bitc::BINOP_OR:
1312 return IsFP ? -1 : Instruction::Or;
1313 case bitc::BINOP_XOR:
1314 return IsFP ? -1 : Instruction::Xor;
1315 }
1316}
1317
1319 switch (Val) {
1320 default: return AtomicRMWInst::BAD_BINOP;
1322 case bitc::RMW_ADD: return AtomicRMWInst::Add;
1323 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
1324 case bitc::RMW_AND: return AtomicRMWInst::And;
1326 case bitc::RMW_OR: return AtomicRMWInst::Or;
1327 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
1328 case bitc::RMW_MAX: return AtomicRMWInst::Max;
1329 case bitc::RMW_MIN: return AtomicRMWInst::Min;
1340 }
1341}
1342
1344 switch (Val) {
1345 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
1346 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
1347 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
1348 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
1349 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
1350 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
1351 default: // Map unknown orderings to sequentially-consistent.
1352 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1353 }
1354}
1355
1357 switch (Val) {
1358 default: // Map unknown selection kinds to any.
1360 return Comdat::Any;
1362 return Comdat::ExactMatch;
1364 return Comdat::Largest;
1366 return Comdat::NoDeduplicate;
1368 return Comdat::SameSize;
1369 }
1370}
1371
1373 FastMathFlags FMF;
1374 if (0 != (Val & bitc::UnsafeAlgebra))
1375 FMF.setFast();
1376 if (0 != (Val & bitc::AllowReassoc))
1377 FMF.setAllowReassoc();
1378 if (0 != (Val & bitc::NoNaNs))
1379 FMF.setNoNaNs();
1380 if (0 != (Val & bitc::NoInfs))
1381 FMF.setNoInfs();
1382 if (0 != (Val & bitc::NoSignedZeros))
1383 FMF.setNoSignedZeros();
1384 if (0 != (Val & bitc::AllowReciprocal))
1385 FMF.setAllowReciprocal();
1386 if (0 != (Val & bitc::AllowContract))
1387 FMF.setAllowContract(true);
1388 if (0 != (Val & bitc::ApproxFunc))
1389 FMF.setApproxFunc();
1390 return FMF;
1391}
1392
1393static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1394 // A GlobalValue with local linkage cannot have a DLL storage class.
1395 if (GV->hasLocalLinkage())
1396 return;
1397 switch (Val) {
1400 }
1401}
1402
1403Type *BitcodeReader::getTypeByID(unsigned ID) {
1404 // The type table size is always specified correctly.
1405 if (ID >= TypeList.size())
1406 return nullptr;
1407
1408 if (Type *Ty = TypeList[ID])
1409 return Ty;
1410
1411 // If we have a forward reference, the only possible case is when it is to a
1412 // named struct. Just create a placeholder for now.
1413 return TypeList[ID] = createIdentifiedStructType(Context);
1414}
1415
1416unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) {
1417 auto It = ContainedTypeIDs.find(ID);
1418 if (It == ContainedTypeIDs.end())
1419 return InvalidTypeID;
1420
1421 if (Idx >= It->second.size())
1422 return InvalidTypeID;
1423
1424 return It->second[Idx];
1425}
1426
1427Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) {
1428 if (ID >= TypeList.size())
1429 return nullptr;
1430
1431 Type *Ty = TypeList[ID];
1432 if (!Ty->isPointerTy())
1433 return nullptr;
1434
1435 return getTypeByID(getContainedTypeID(ID, 0));
1436}
1437
1438unsigned BitcodeReader::getVirtualTypeID(Type *Ty,
1439 ArrayRef<unsigned> ChildTypeIDs) {
1440 unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0];
1441 auto CacheKey = std::make_pair(Ty, ChildTypeID);
1442 auto It = VirtualTypeIDs.find(CacheKey);
1443 if (It != VirtualTypeIDs.end()) {
1444 // The cmpxchg return value is the only place we need more than one
1445 // contained type ID, however the second one will always be the same (i1),
1446 // so we don't need to include it in the cache key. This asserts that the
1447 // contained types are indeed as expected and there are no collisions.
1448 assert((ChildTypeIDs.empty() ||
1449 ContainedTypeIDs[It->second] == ChildTypeIDs) &&
1450 "Incorrect cached contained type IDs");
1451 return It->second;
1452 }
1453
1454 unsigned TypeID = TypeList.size();
1455 TypeList.push_back(Ty);
1456 if (!ChildTypeIDs.empty())
1457 append_range(ContainedTypeIDs[TypeID], ChildTypeIDs);
1458 VirtualTypeIDs.insert({CacheKey, TypeID});
1459 return TypeID;
1460}
1461
1463 GEPNoWrapFlags NW;
1464 if (Flags & (1 << bitc::GEP_INBOUNDS))
1466 if (Flags & (1 << bitc::GEP_NUSW))
1468 if (Flags & (1 << bitc::GEP_NUW))
1470 return NW;
1471}
1472
1473static bool isConstExprSupported(const BitcodeConstant *BC) {
1474 uint8_t Opcode = BC->Opcode;
1475
1476 // These are not real constant expressions, always consider them supported.
1477 if (Opcode >= BitcodeConstant::FirstSpecialOpcode)
1478 return true;
1479
1480 // If -expand-constant-exprs is set, we want to consider all expressions
1481 // as unsupported.
1483 return false;
1484
1485 if (Instruction::isBinaryOp(Opcode))
1486 return ConstantExpr::isSupportedBinOp(Opcode);
1487
1488 if (Instruction::isCast(Opcode))
1489 return ConstantExpr::isSupportedCastOp(Opcode);
1490
1491 if (Opcode == Instruction::GetElementPtr)
1492 return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy);
1493
1494 switch (Opcode) {
1495 case Instruction::FNeg:
1496 case Instruction::Select:
1497 return false;
1498 default:
1499 return true;
1500 }
1501}
1502
1503Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
1504 BasicBlock *InsertBB) {
1505 // Quickly handle the case where there is no BitcodeConstant to resolve.
1506 if (StartValID < ValueList.size() && ValueList[StartValID] &&
1507 !isa<BitcodeConstant>(ValueList[StartValID]))
1508 return ValueList[StartValID];
1509
1510 SmallDenseMap<unsigned, Value *> MaterializedValues;
1511 SmallVector<unsigned> Worklist;
1512 Worklist.push_back(StartValID);
1513 while (!Worklist.empty()) {
1514 unsigned ValID = Worklist.back();
1515 if (MaterializedValues.count(ValID)) {
1516 // Duplicate expression that was already handled.
1517 Worklist.pop_back();
1518 continue;
1519 }
1520
1521 if (ValID >= ValueList.size() || !ValueList[ValID])
1522 return error("Invalid value ID");
1523
1524 Value *V = ValueList[ValID];
1525 auto *BC = dyn_cast<BitcodeConstant>(V);
1526 if (!BC) {
1527 MaterializedValues.insert({ValID, V});
1528 Worklist.pop_back();
1529 continue;
1530 }
1531
1532 // Iterate in reverse, so values will get popped from the worklist in
1533 // expected order.
1535 for (unsigned OpID : reverse(BC->getOperandIDs())) {
1536 auto It = MaterializedValues.find(OpID);
1537 if (It != MaterializedValues.end())
1538 Ops.push_back(It->second);
1539 else
1540 Worklist.push_back(OpID);
1541 }
1542
1543 // Some expressions have not been resolved yet, handle them first and then
1544 // revisit this one.
1545 if (Ops.size() != BC->getOperandIDs().size())
1546 continue;
1547 std::reverse(Ops.begin(), Ops.end());
1548
1549 SmallVector<Constant *> ConstOps;
1550 for (Value *Op : Ops)
1551 if (auto *C = dyn_cast<Constant>(Op))
1552 ConstOps.push_back(C);
1553
1554 // Materialize as constant expression if possible.
1555 if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) {
1556 Constant *C;
1557 if (Instruction::isCast(BC->Opcode)) {
1558 C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType());
1559 if (!C)
1560 C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType());
1561 } else if (Instruction::isBinaryOp(BC->Opcode)) {
1562 C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags);
1563 } else {
1564 switch (BC->Opcode) {
1565 case BitcodeConstant::NoCFIOpcode: {
1566 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);
1567 if (!GV)
1568 return error("no_cfi operand must be GlobalValue");
1569 C = NoCFIValue::get(GV);
1570 break;
1571 }
1572 case BitcodeConstant::DSOLocalEquivalentOpcode: {
1573 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);
1574 if (!GV)
1575 return error("dso_local operand must be GlobalValue");
1577 break;
1578 }
1579 case BitcodeConstant::BlockAddressOpcode: {
1580 Function *Fn = dyn_cast<Function>(ConstOps[0]);
1581 if (!Fn)
1582 return error("blockaddress operand must be a function");
1583
1584 // If the function is already parsed we can insert the block address
1585 // right away.
1586 BasicBlock *BB;
1587 unsigned BBID = BC->BlockAddressBB;
1588 if (!BBID)
1589 // Invalid reference to entry block.
1590 return error("Invalid ID");
1591 if (!Fn->empty()) {
1592 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1593 for (size_t I = 0, E = BBID; I != E; ++I) {
1594 if (BBI == BBE)
1595 return error("Invalid ID");
1596 ++BBI;
1597 }
1598 BB = &*BBI;
1599 } else {
1600 // Otherwise insert a placeholder and remember it so it can be
1601 // inserted when the function is parsed.
1602 auto &FwdBBs = BasicBlockFwdRefs[Fn];
1603 if (FwdBBs.empty())
1604 BasicBlockFwdRefQueue.push_back(Fn);
1605 if (FwdBBs.size() < BBID + 1)
1606 FwdBBs.resize(BBID + 1);
1607 if (!FwdBBs[BBID])
1608 FwdBBs[BBID] = BasicBlock::Create(Context);
1609 BB = FwdBBs[BBID];
1610 }
1611 C = BlockAddress::get(Fn, BB);
1612 break;
1613 }
1614 case BitcodeConstant::ConstantStructOpcode:
1615 C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps);
1616 break;
1617 case BitcodeConstant::ConstantArrayOpcode:
1618 C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps);
1619 break;
1620 case BitcodeConstant::ConstantVectorOpcode:
1621 C = ConstantVector::get(ConstOps);
1622 break;
1623 case Instruction::ICmp:
1624 case Instruction::FCmp:
1625 C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]);
1626 break;
1627 case Instruction::GetElementPtr:
1629 BC->SrcElemTy, ConstOps[0], ArrayRef(ConstOps).drop_front(),
1630 toGEPNoWrapFlags(BC->Flags), BC->getInRange());
1631 break;
1632 case Instruction::ExtractElement:
1633 C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]);
1634 break;
1635 case Instruction::InsertElement:
1636 C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1],
1637 ConstOps[2]);
1638 break;
1639 case Instruction::ShuffleVector: {
1641 ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask);
1642 C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask);
1643 break;
1644 }
1645 default:
1646 llvm_unreachable("Unhandled bitcode constant");
1647 }
1648 }
1649
1650 // Cache resolved constant.
1651 ValueList.replaceValueWithoutRAUW(ValID, C);
1652 MaterializedValues.insert({ValID, C});
1653 Worklist.pop_back();
1654 continue;
1655 }
1656
1657 if (!InsertBB)
1658 return error(Twine("Value referenced by initializer is an unsupported "
1659 "constant expression of type ") +
1660 BC->getOpcodeName());
1661
1662 // Materialize as instructions if necessary.
1663 Instruction *I;
1664 if (Instruction::isCast(BC->Opcode)) {
1665 I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0],
1666 BC->getType(), "constexpr", InsertBB);
1667 } else if (Instruction::isUnaryOp(BC->Opcode)) {
1668 I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0],
1669 "constexpr", InsertBB);
1670 } else if (Instruction::isBinaryOp(BC->Opcode)) {
1671 I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0],
1672 Ops[1], "constexpr", InsertBB);
1673 if (isa<OverflowingBinaryOperator>(I)) {
1675 I->setHasNoSignedWrap();
1677 I->setHasNoUnsignedWrap();
1678 }
1679 if (isa<PossiblyExactOperator>(I) &&
1680 (BC->Flags & PossiblyExactOperator::IsExact))
1681 I->setIsExact();
1682 } else {
1683 switch (BC->Opcode) {
1684 case BitcodeConstant::ConstantVectorOpcode: {
1685 Type *IdxTy = Type::getInt32Ty(BC->getContext());
1686 Value *V = PoisonValue::get(BC->getType());
1687 for (auto Pair : enumerate(Ops)) {
1688 Value *Idx = ConstantInt::get(IdxTy, Pair.index());
1689 V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins",
1690 InsertBB);
1691 }
1692 I = cast<Instruction>(V);
1693 break;
1694 }
1695 case BitcodeConstant::ConstantStructOpcode:
1696 case BitcodeConstant::ConstantArrayOpcode: {
1697 Value *V = PoisonValue::get(BC->getType());
1698 for (auto Pair : enumerate(Ops))
1699 V = InsertValueInst::Create(V, Pair.value(), Pair.index(),
1700 "constexpr.ins", InsertBB);
1701 I = cast<Instruction>(V);
1702 break;
1703 }
1704 case Instruction::ICmp:
1705 case Instruction::FCmp:
1707 (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1],
1708 "constexpr", InsertBB);
1709 break;
1710 case Instruction::GetElementPtr:
1711 I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0],
1712 ArrayRef(Ops).drop_front(), "constexpr",
1713 InsertBB);
1714 cast<GetElementPtrInst>(I)->setNoWrapFlags(toGEPNoWrapFlags(BC->Flags));
1715 break;
1716 case Instruction::Select:
1717 I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB);
1718 break;
1719 case Instruction::ExtractElement:
1720 I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB);
1721 break;
1722 case Instruction::InsertElement:
1723 I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr",
1724 InsertBB);
1725 break;
1726 case Instruction::ShuffleVector:
1727 I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr",
1728 InsertBB);
1729 break;
1730 default:
1731 llvm_unreachable("Unhandled bitcode constant");
1732 }
1733 }
1734
1735 MaterializedValues.insert({ValID, I});
1736 Worklist.pop_back();
1737 }
1738
1739 return MaterializedValues[StartValID];
1740}
1741
1742Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) {
1743 Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr);
1744 if (!MaybeV)
1745 return MaybeV.takeError();
1746
1747 // Result must be Constant if InsertBB is nullptr.
1748 return cast<Constant>(MaybeV.get());
1749}
1750
1751StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1752 StringRef Name) {
1753 auto *Ret = StructType::create(Context, Name);
1754 IdentifiedStructTypes.push_back(Ret);
1755 return Ret;
1756}
1757
1758StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1759 auto *Ret = StructType::create(Context);
1760 IdentifiedStructTypes.push_back(Ret);
1761 return Ret;
1762}
1763
1764//===----------------------------------------------------------------------===//
1765// Functions for parsing blocks from the bitcode file
1766//===----------------------------------------------------------------------===//
1767
1769 switch (Val) {
1773 llvm_unreachable("Synthetic enumerators which should never get here");
1774
1775 case Attribute::None: return 0;
1776 case Attribute::ZExt: return 1 << 0;
1777 case Attribute::SExt: return 1 << 1;
1778 case Attribute::NoReturn: return 1 << 2;
1779 case Attribute::InReg: return 1 << 3;
1780 case Attribute::StructRet: return 1 << 4;
1781 case Attribute::NoUnwind: return 1 << 5;
1782 case Attribute::NoAlias: return 1 << 6;
1783 case Attribute::ByVal: return 1 << 7;
1784 case Attribute::Nest: return 1 << 8;
1785 case Attribute::ReadNone: return 1 << 9;
1786 case Attribute::ReadOnly: return 1 << 10;
1787 case Attribute::NoInline: return 1 << 11;
1788 case Attribute::AlwaysInline: return 1 << 12;
1789 case Attribute::OptimizeForSize: return 1 << 13;
1790 case Attribute::StackProtect: return 1 << 14;
1791 case Attribute::StackProtectReq: return 1 << 15;
1792 case Attribute::Alignment: return 31 << 16;
1793 case Attribute::NoCapture: return 1 << 21;
1794 case Attribute::NoRedZone: return 1 << 22;
1795 case Attribute::NoImplicitFloat: return 1 << 23;
1796 case Attribute::Naked: return 1 << 24;
1797 case Attribute::InlineHint: return 1 << 25;
1798 case Attribute::StackAlignment: return 7 << 26;
1799 case Attribute::ReturnsTwice: return 1 << 29;
1800 case Attribute::UWTable: return 1 << 30;
1801 case Attribute::NonLazyBind: return 1U << 31;
1802 case Attribute::SanitizeAddress: return 1ULL << 32;
1803 case Attribute::MinSize: return 1ULL << 33;
1804 case Attribute::NoDuplicate: return 1ULL << 34;
1805 case Attribute::StackProtectStrong: return 1ULL << 35;
1806 case Attribute::SanitizeThread: return 1ULL << 36;
1807 case Attribute::SanitizeMemory: return 1ULL << 37;
1808 case Attribute::NoBuiltin: return 1ULL << 38;
1809 case Attribute::Returned: return 1ULL << 39;
1810 case Attribute::Cold: return 1ULL << 40;
1811 case Attribute::Builtin: return 1ULL << 41;
1812 case Attribute::OptimizeNone: return 1ULL << 42;
1813 case Attribute::InAlloca: return 1ULL << 43;
1814 case Attribute::NonNull: return 1ULL << 44;
1815 case Attribute::JumpTable: return 1ULL << 45;
1816 case Attribute::Convergent: return 1ULL << 46;
1817 case Attribute::SafeStack: return 1ULL << 47;
1818 case Attribute::NoRecurse: return 1ULL << 48;
1819 // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately.
1820 // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately.
1821 case Attribute::SwiftSelf: return 1ULL << 51;
1822 case Attribute::SwiftError: return 1ULL << 52;
1823 case Attribute::WriteOnly: return 1ULL << 53;
1824 case Attribute::Speculatable: return 1ULL << 54;
1825 case Attribute::StrictFP: return 1ULL << 55;
1826 case Attribute::SanitizeHWAddress: return 1ULL << 56;
1827 case Attribute::NoCfCheck: return 1ULL << 57;
1828 case Attribute::OptForFuzzing: return 1ULL << 58;
1829 case Attribute::ShadowCallStack: return 1ULL << 59;
1830 case Attribute::SpeculativeLoadHardening:
1831 return 1ULL << 60;
1832 case Attribute::ImmArg:
1833 return 1ULL << 61;
1834 case Attribute::WillReturn:
1835 return 1ULL << 62;
1836 case Attribute::NoFree:
1837 return 1ULL << 63;
1838 default:
1839 // Other attributes are not supported in the raw format,
1840 // as we ran out of space.
1841 return 0;
1842 }
1843 llvm_unreachable("Unsupported attribute type");
1844}
1845
1847 if (!Val) return;
1848
1850 I = Attribute::AttrKind(I + 1)) {
1851 if (uint64_t A = (Val & getRawAttributeMask(I))) {
1852 if (I == Attribute::Alignment)
1853 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1854 else if (I == Attribute::StackAlignment)
1855 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1856 else if (Attribute::isTypeAttrKind(I))
1857 B.addTypeAttr(I, nullptr); // Type will be auto-upgraded.
1858 else
1859 B.addAttribute(I);
1860 }
1861 }
1862}
1863
1864/// This fills an AttrBuilder object with the LLVM attributes that have
1865/// been decoded from the given integer. This function must stay in sync with
1866/// 'encodeLLVMAttributesForBitcode'.
1868 uint64_t EncodedAttrs,
1869 uint64_t AttrIdx) {
1870 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1871 // the bits above 31 down by 11 bits.
1872 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1873 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1874 "Alignment must be a power of two.");
1875
1876 if (Alignment)
1877 B.addAlignmentAttr(Alignment);
1878
1879 uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1880 (EncodedAttrs & 0xffff);
1881
1882 if (AttrIdx == AttributeList::FunctionIndex) {
1883 // Upgrade old memory attributes.
1885 if (Attrs & (1ULL << 9)) {
1886 // ReadNone
1887 Attrs &= ~(1ULL << 9);
1888 ME &= MemoryEffects::none();
1889 }
1890 if (Attrs & (1ULL << 10)) {
1891 // ReadOnly
1892 Attrs &= ~(1ULL << 10);
1894 }
1895 if (Attrs & (1ULL << 49)) {
1896 // InaccessibleMemOnly
1897 Attrs &= ~(1ULL << 49);
1899 }
1900 if (Attrs & (1ULL << 50)) {
1901 // InaccessibleMemOrArgMemOnly
1902 Attrs &= ~(1ULL << 50);
1904 }
1905 if (Attrs & (1ULL << 53)) {
1906 // WriteOnly
1907 Attrs &= ~(1ULL << 53);
1909 }
1910 if (ME != MemoryEffects::unknown())
1911 B.addMemoryAttr(ME);
1912 }
1913
1914 addRawAttributeValue(B, Attrs);
1915}
1916
1917Error BitcodeReader::parseAttributeBlock() {
1918 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1919 return Err;
1920
1921 if (!MAttributes.empty())
1922 return error("Invalid multiple blocks");
1923
1925
1927
1928 // Read all the records.
1929 while (true) {
1930 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1931 if (!MaybeEntry)
1932 return MaybeEntry.takeError();
1933 BitstreamEntry Entry = MaybeEntry.get();
1934
1935 switch (Entry.Kind) {
1936 case BitstreamEntry::SubBlock: // Handled for us already.
1938 return error("Malformed block");
1940 return Error::success();
1942 // The interesting case.
1943 break;
1944 }
1945
1946 // Read a record.
1947 Record.clear();
1948 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1949 if (!MaybeRecord)
1950 return MaybeRecord.takeError();
1951 switch (MaybeRecord.get()) {
1952 default: // Default behavior: ignore.
1953 break;
1954 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1955 // Deprecated, but still needed to read old bitcode files.
1956 if (Record.size() & 1)
1957 return error("Invalid parameter attribute record");
1958
1959 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1960 AttrBuilder B(Context);
1962 Attrs.push_back(AttributeList::get(Context, Record[i], B));
1963 }
1964
1965 MAttributes.push_back(AttributeList::get(Context, Attrs));
1966 Attrs.clear();
1967 break;
1968 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1969 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1970 Attrs.push_back(MAttributeGroups[Record[i]]);
1971
1972 MAttributes.push_back(AttributeList::get(Context, Attrs));
1973 Attrs.clear();
1974 break;
1975 }
1976 }
1977}
1978
1979// Returns Attribute::None on unrecognized codes.
1981 switch (Code) {
1982 default:
1983 return Attribute::None;
1985 return Attribute::Alignment;
1987 return Attribute::AlwaysInline;
1989 return Attribute::Builtin;
1991 return Attribute::ByVal;
1993 return Attribute::InAlloca;
1995 return Attribute::Cold;
1997 return Attribute::Convergent;
1999 return Attribute::DisableSanitizerInstrumentation;
2001 return Attribute::ElementType;
2003 return Attribute::FnRetThunkExtern;
2005 return Attribute::InlineHint;
2007 return Attribute::InReg;
2009 return Attribute::JumpTable;
2011 return Attribute::Memory;
2013 return Attribute::NoFPClass;
2015 return Attribute::MinSize;
2017 return Attribute::Naked;
2019 return Attribute::Nest;
2021 return Attribute::NoAlias;
2023 return Attribute::NoBuiltin;
2025 return Attribute::NoCallback;
2027 return Attribute::NoCapture;
2029 return Attribute::NoDuplicate;
2031 return Attribute::NoFree;
2033 return Attribute::NoImplicitFloat;
2035 return Attribute::NoInline;
2037 return Attribute::NoRecurse;
2039 return Attribute::NoMerge;
2041 return Attribute::NonLazyBind;
2043 return Attribute::NonNull;
2045 return Attribute::Dereferenceable;
2047 return Attribute::DereferenceableOrNull;
2049 return Attribute::AllocAlign;
2051 return Attribute::AllocKind;
2053 return Attribute::AllocSize;
2055 return Attribute::AllocatedPointer;
2057 return Attribute::NoRedZone;
2059 return Attribute::NoReturn;
2061 return Attribute::NoSync;
2063 return Attribute::NoCfCheck;
2065 return Attribute::NoProfile;
2067 return Attribute::SkipProfile;
2069 return Attribute::NoUnwind;
2071 return Attribute::NoSanitizeBounds;
2073 return Attribute::NoSanitizeCoverage;
2075 return Attribute::NullPointerIsValid;
2077 return Attribute::OptimizeForDebugging;
2079 return Attribute::OptForFuzzing;
2081 return Attribute::OptimizeForSize;
2083 return Attribute::OptimizeNone;
2085 return Attribute::ReadNone;
2087 return Attribute::ReadOnly;
2089 return Attribute::Returned;
2091 return Attribute::ReturnsTwice;
2093 return Attribute::SExt;
2095 return Attribute::Speculatable;
2097 return Attribute::StackAlignment;
2099 return Attribute::StackProtect;
2101 return Attribute::StackProtectReq;
2103 return Attribute::StackProtectStrong;
2105 return Attribute::SafeStack;
2107 return Attribute::ShadowCallStack;
2109 return Attribute::StrictFP;
2111 return Attribute::StructRet;
2113 return Attribute::SanitizeAddress;
2115 return Attribute::SanitizeHWAddress;
2117 return Attribute::SanitizeThread;
2119 return Attribute::SanitizeMemory;
2121 return Attribute::SpeculativeLoadHardening;
2123 return Attribute::SwiftError;
2125 return Attribute::SwiftSelf;
2127 return Attribute::SwiftAsync;
2129 return Attribute::UWTable;
2131 return Attribute::VScaleRange;
2133 return Attribute::WillReturn;
2135 return Attribute::WriteOnly;
2137 return Attribute::ZExt;
2139 return Attribute::ImmArg;
2141 return Attribute::SanitizeMemTag;
2143 return Attribute::Preallocated;
2145 return Attribute::NoUndef;
2147 return Attribute::ByRef;
2149 return Attribute::MustProgress;
2151 return Attribute::Hot;
2153 return Attribute::PresplitCoroutine;
2155 return Attribute::Writable;
2157 return Attribute::CoroDestroyOnlyWhenComplete;
2159 return Attribute::DeadOnUnwind;
2161 return Attribute::Range;
2162 }
2163}
2164
2165Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
2166 MaybeAlign &Alignment) {
2167 // Note: Alignment in bitcode files is incremented by 1, so that zero
2168 // can be used for default alignment.
2170 return error("Invalid alignment value");
2171 Alignment = decodeMaybeAlign(Exponent);
2172 return Error::success();
2173}
2174
2175Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
2176 *Kind = getAttrFromCode(Code);
2177 if (*Kind == Attribute::None)
2178 return error("Unknown attribute kind (" + Twine(Code) + ")");
2179 return Error::success();
2180}
2181
2182static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) {
2183 switch (EncodedKind) {
2185 ME &= MemoryEffects::none();
2186 return true;
2189 return true;
2192 return true;
2195 return true;
2198 return true;
2201 return true;
2202 default:
2203 return false;
2204 }
2205}
2206
2207Error BitcodeReader::parseAttributeGroupBlock() {
2208 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
2209 return Err;
2210
2211 if (!MAttributeGroups.empty())
2212 return error("Invalid multiple blocks");
2213
2215
2216 // Read all the records.
2217 while (true) {
2218 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2219 if (!MaybeEntry)
2220 return MaybeEntry.takeError();
2221 BitstreamEntry Entry = MaybeEntry.get();
2222
2223 switch (Entry.Kind) {
2224 case BitstreamEntry::SubBlock: // Handled for us already.
2226 return error("Malformed block");
2228 return Error::success();
2230 // The interesting case.
2231 break;
2232 }
2233
2234 // Read a record.
2235 Record.clear();
2236 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2237 if (!MaybeRecord)
2238 return MaybeRecord.takeError();
2239 switch (MaybeRecord.get()) {
2240 default: // Default behavior: ignore.
2241 break;
2242 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
2243 if (Record.size() < 3)
2244 return error("Invalid grp record");
2245
2246 uint64_t GrpID = Record[0];
2247 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
2248
2249 AttrBuilder B(Context);
2251 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2252 if (Record[i] == 0) { // Enum attribute
2254 uint64_t EncodedKind = Record[++i];
2256 upgradeOldMemoryAttribute(ME, EncodedKind))
2257 continue;
2258
2259 if (Error Err = parseAttrKind(EncodedKind, &Kind))
2260 return Err;
2261
2262 // Upgrade old-style byval attribute to one with a type, even if it's
2263 // nullptr. We will have to insert the real type when we associate
2264 // this AttributeList with a function.
2265 if (Kind == Attribute::ByVal)
2266 B.addByValAttr(nullptr);
2267 else if (Kind == Attribute::StructRet)
2268 B.addStructRetAttr(nullptr);
2269 else if (Kind == Attribute::InAlloca)
2270 B.addInAllocaAttr(nullptr);
2271 else if (Kind == Attribute::UWTable)
2272 B.addUWTableAttr(UWTableKind::Default);
2273 else if (Attribute::isEnumAttrKind(Kind))
2274 B.addAttribute(Kind);
2275 else
2276 return error("Not an enum attribute");
2277 } else if (Record[i] == 1) { // Integer attribute
2279 if (Error Err = parseAttrKind(Record[++i], &Kind))
2280 return Err;
2281 if (!Attribute::isIntAttrKind(Kind))
2282 return error("Not an int attribute");
2283 if (Kind == Attribute::Alignment)
2284 B.addAlignmentAttr(Record[++i]);
2285 else if (Kind == Attribute::StackAlignment)
2286 B.addStackAlignmentAttr(Record[++i]);
2287 else if (Kind == Attribute::Dereferenceable)
2288 B.addDereferenceableAttr(Record[++i]);
2289 else if (Kind == Attribute::DereferenceableOrNull)
2290 B.addDereferenceableOrNullAttr(Record[++i]);
2291 else if (Kind == Attribute::AllocSize)
2292 B.addAllocSizeAttrFromRawRepr(Record[++i]);
2293 else if (Kind == Attribute::VScaleRange)
2294 B.addVScaleRangeAttrFromRawRepr(Record[++i]);
2295 else if (Kind == Attribute::UWTable)
2296 B.addUWTableAttr(UWTableKind(Record[++i]));
2297 else if (Kind == Attribute::AllocKind)
2298 B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i]));
2299 else if (Kind == Attribute::Memory)
2300 B.addMemoryAttr(MemoryEffects::createFromIntValue(Record[++i]));
2301 else if (Kind == Attribute::NoFPClass)
2302 B.addNoFPClassAttr(
2303 static_cast<FPClassTest>(Record[++i] & fcAllFlags));
2304 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute
2305 bool HasValue = (Record[i++] == 4);
2306 SmallString<64> KindStr;
2307 SmallString<64> ValStr;
2308
2309 while (Record[i] != 0 && i != e)
2310 KindStr += Record[i++];
2311 assert(Record[i] == 0 && "Kind string not null terminated");
2312
2313 if (HasValue) {
2314 // Has a value associated with it.
2315 ++i; // Skip the '0' that terminates the "kind" string.
2316 while (Record[i] != 0 && i != e)
2317 ValStr += Record[i++];
2318 assert(Record[i] == 0 && "Value string not null terminated");
2319 }
2320
2321 B.addAttribute(KindStr.str(), ValStr.str());
2322 } else if (Record[i] == 5 || Record[i] == 6) {
2323 bool HasType = Record[i] == 6;
2325 if (Error Err = parseAttrKind(Record[++i], &Kind))
2326 return Err;
2327 if (!Attribute::isTypeAttrKind(Kind))
2328 return error("Not a type attribute");
2329
2330 B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr);
2331 } else if (Record[i] == 7) {
2333
2334 i++;
2335 if (Error Err = parseAttrKind(Record[i++], &Kind))
2336 return Err;
2338 return error("Not a ConstantRange attribute");
2339
2340 Expected<ConstantRange> MaybeCR = readConstantRange(Record, i);
2341 if (!MaybeCR)
2342 return MaybeCR.takeError();
2343 i--;
2344
2345 B.addConstantRangeAttr(Kind, MaybeCR.get());
2346 } else {
2347 return error("Invalid attribute group entry");
2348 }
2349 }
2350
2351 if (ME != MemoryEffects::unknown())
2352 B.addMemoryAttr(ME);
2353
2355 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
2356 break;
2357 }
2358 }
2359 }
2360}
2361
2362Error BitcodeReader::parseTypeTable() {
2363 if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
2364 return Err;
2365
2366 return parseTypeTableBody();
2367}
2368
2369Error BitcodeReader::parseTypeTableBody() {
2370 if (!TypeList.empty())
2371 return error("Invalid multiple blocks");
2372
2374 unsigned NumRecords = 0;
2375
2377
2378 // Read all the records for this type table.
2379 while (true) {
2380 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2381 if (!MaybeEntry)
2382 return MaybeEntry.takeError();
2383 BitstreamEntry Entry = MaybeEntry.get();
2384
2385 switch (Entry.Kind) {
2386 case BitstreamEntry::SubBlock: // Handled for us already.
2388 return error("Malformed block");
2390 if (NumRecords != TypeList.size())
2391 return error("Malformed block");
2392 return Error::success();
2394 // The interesting case.
2395 break;
2396 }
2397
2398 // Read a record.
2399 Record.clear();
2400 Type *ResultTy = nullptr;
2401 SmallVector<unsigned> ContainedIDs;
2402 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2403 if (!MaybeRecord)
2404 return MaybeRecord.takeError();
2405 switch (MaybeRecord.get()) {
2406 default:
2407 return error("Invalid value");
2408 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
2409 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
2410 // type list. This allows us to reserve space.
2411 if (Record.empty())
2412 return error("Invalid numentry record");
2413 TypeList.resize(Record[0]);
2414 continue;
2415 case bitc::TYPE_CODE_VOID: // VOID
2416 ResultTy = Type::getVoidTy(Context);
2417 break;
2418 case bitc::TYPE_CODE_HALF: // HALF
2419 ResultTy = Type::getHalfTy(Context);
2420 break;
2421 case bitc::TYPE_CODE_BFLOAT: // BFLOAT
2422 ResultTy = Type::getBFloatTy(Context);
2423 break;
2424 case bitc::TYPE_CODE_FLOAT: // FLOAT
2425 ResultTy = Type::getFloatTy(Context);
2426 break;
2427 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
2428 ResultTy = Type::getDoubleTy(Context);
2429 break;
2430 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
2431 ResultTy = Type::getX86_FP80Ty(Context);
2432 break;
2433 case bitc::TYPE_CODE_FP128: // FP128
2434 ResultTy = Type::getFP128Ty(Context);
2435 break;
2436 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
2437 ResultTy = Type::getPPC_FP128Ty(Context);
2438 break;
2439 case bitc::TYPE_CODE_LABEL: // LABEL
2440 ResultTy = Type::getLabelTy(Context);
2441 break;
2442 case bitc::TYPE_CODE_METADATA: // METADATA
2443 ResultTy = Type::getMetadataTy(Context);
2444 break;
2445 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
2446 ResultTy = Type::getX86_MMXTy(Context);
2447 break;
2448 case bitc::TYPE_CODE_X86_AMX: // X86_AMX
2449 ResultTy = Type::getX86_AMXTy(Context);
2450 break;
2451 case bitc::TYPE_CODE_TOKEN: // TOKEN
2452 ResultTy = Type::getTokenTy(Context);
2453 break;
2454 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
2455 if (Record.empty())
2456 return error("Invalid integer record");
2457
2458 uint64_t NumBits = Record[0];
2459 if (NumBits < IntegerType::MIN_INT_BITS ||
2460 NumBits > IntegerType::MAX_INT_BITS)
2461 return error("Bitwidth for integer type out of range");
2462 ResultTy = IntegerType::get(Context, NumBits);
2463 break;
2464 }
2465 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
2466 // [pointee type, address space]
2467 if (Record.empty())
2468 return error("Invalid pointer record");
2469 unsigned AddressSpace = 0;
2470 if (Record.size() == 2)
2471 AddressSpace = Record[1];
2472 ResultTy = getTypeByID(Record[0]);
2473 if (!ResultTy ||
2474 !PointerType::isValidElementType(ResultTy))
2475 return error("Invalid type");
2476 ContainedIDs.push_back(Record[0]);
2477 ResultTy = PointerType::get(ResultTy, AddressSpace);
2478 break;
2479 }
2480 case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace]
2481 if (Record.size() != 1)
2482 return error("Invalid opaque pointer record");
2483 unsigned AddressSpace = Record[0];
2484 ResultTy = PointerType::get(Context, AddressSpace);
2485 break;
2486 }
2488 // Deprecated, but still needed to read old bitcode files.
2489 // FUNCTION: [vararg, attrid, retty, paramty x N]
2490 if (Record.size() < 3)
2491 return error("Invalid function record");
2492 SmallVector<Type*, 8> ArgTys;
2493 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
2494 if (Type *T = getTypeByID(Record[i]))
2495 ArgTys.push_back(T);
2496 else
2497 break;
2498 }
2499
2500 ResultTy = getTypeByID(Record[2]);
2501 if (!ResultTy || ArgTys.size() < Record.size()-3)
2502 return error("Invalid type");
2503
2504 ContainedIDs.append(Record.begin() + 2, Record.end());
2505 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
2506 break;
2507 }
2509 // FUNCTION: [vararg, retty, paramty x N]
2510 if (Record.size() < 2)
2511 return error("Invalid function record");
2512 SmallVector<Type*, 8> ArgTys;
2513 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2514 if (Type *T = getTypeByID(Record[i])) {
2515 if (!FunctionType::isValidArgumentType(T))
2516 return error("Invalid function argument type");
2517 ArgTys.push_back(T);
2518 }
2519 else
2520 break;
2521 }
2522
2523 ResultTy = getTypeByID(Record[1]);
2524 if (!ResultTy || ArgTys.size() < Record.size()-2)
2525 return error("Invalid type");
2526
2527 ContainedIDs.append(Record.begin() + 1, Record.end());
2528 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
2529 break;
2530 }
2531 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
2532 if (Record.empty())
2533 return error("Invalid anon struct record");
2534 SmallVector<Type*, 8> EltTys;
2535 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2536 if (Type *T = getTypeByID(Record[i]))
2537 EltTys.push_back(T);
2538 else
2539 break;
2540 }
2541 if (EltTys.size() != Record.size()-1)
2542 return error("Invalid type");
2543 ContainedIDs.append(Record.begin() + 1, Record.end());
2544 ResultTy = StructType::get(Context, EltTys, Record[0]);
2545 break;
2546 }
2547 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
2548 if (convertToString(Record, 0, TypeName))
2549 return error("Invalid struct name record");
2550 continue;
2551
2552 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
2553 if (Record.empty())
2554 return error("Invalid named struct record");
2555
2556 if (NumRecords >= TypeList.size())
2557 return error("Invalid TYPE table");
2558
2559 // Check to see if this was forward referenced, if so fill in the temp.
2560 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
2561 if (Res) {
2562 Res->setName(TypeName);
2563 TypeList[NumRecords] = nullptr;
2564 } else // Otherwise, create a new struct.
2565 Res = createIdentifiedStructType(Context, TypeName);
2566 TypeName.clear();
2567
2568 SmallVector<Type*, 8> EltTys;
2569 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2570 if (Type *T = getTypeByID(Record[i]))
2571 EltTys.push_back(T);
2572 else
2573 break;
2574 }
2575 if (EltTys.size() != Record.size()-1)
2576 return error("Invalid named struct record");
2577 Res->setBody(EltTys, Record[0]);
2578 ContainedIDs.append(Record.begin() + 1, Record.end());
2579 ResultTy = Res;
2580 break;
2581 }
2582 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
2583 if (Record.size() != 1)
2584 return error("Invalid opaque type record");
2585
2586 if (NumRecords >= TypeList.size())
2587 return error("Invalid TYPE table");
2588
2589 // Check to see if this was forward referenced, if so fill in the temp.
2590 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
2591 if (Res) {
2592 Res->setName(TypeName);
2593 TypeList[NumRecords] = nullptr;
2594 } else // Otherwise, create a new struct with no body.
2595 Res = createIdentifiedStructType(Context, TypeName);
2596 TypeName.clear();
2597 ResultTy = Res;
2598 break;
2599 }
2600 case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...]
2601 if (Record.size() < 1)
2602 return error("Invalid target extension type record");
2603
2604 if (NumRecords >= TypeList.size())
2605 return error("Invalid TYPE table");
2606
2607 if (Record[0] >= Record.size())
2608 return error("Too many type parameters");
2609
2610 unsigned NumTys = Record[0];
2611 SmallVector<Type *, 4> TypeParams;
2612 SmallVector<unsigned, 8> IntParams;
2613 for (unsigned i = 0; i < NumTys; i++) {
2614 if (Type *T = getTypeByID(Record[i + 1]))
2615 TypeParams.push_back(T);
2616 else
2617 return error("Invalid type");
2618 }
2619
2620 for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) {
2621 if (Record[i] > UINT_MAX)
2622 return error("Integer parameter too large");
2623 IntParams.push_back(Record[i]);
2624 }
2625 ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams);
2626 TypeName.clear();
2627 break;
2628 }
2629 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
2630 if (Record.size() < 2)
2631 return error("Invalid array type record");
2632 ResultTy = getTypeByID(Record[1]);
2633 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
2634 return error("Invalid type");
2635 ContainedIDs.push_back(Record[1]);
2636 ResultTy = ArrayType::get(ResultTy, Record[0]);
2637 break;
2638 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
2639 // [numelts, eltty, scalable]
2640 if (Record.size() < 2)
2641 return error("Invalid vector type record");
2642 if (Record[0] == 0)
2643 return error("Invalid vector length");
2644 ResultTy = getTypeByID(Record[1]);
2645 if (!ResultTy || !VectorType::isValidElementType(ResultTy))
2646 return error("Invalid type");
2647 bool Scalable = Record.size() > 2 ? Record[2] : false;
2648 ContainedIDs.push_back(Record[1]);
2649 ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
2650 break;
2651 }
2652
2653 if (NumRecords >= TypeList.size())
2654 return error("Invalid TYPE table");
2655 if (TypeList[NumRecords])
2656 return error(
2657 "Invalid TYPE table: Only named structs can be forward referenced");
2658 assert(ResultTy && "Didn't read a type?");
2659 TypeList[NumRecords] = ResultTy;
2660 if (!ContainedIDs.empty())
2661 ContainedTypeIDs[NumRecords] = std::move(ContainedIDs);
2662 ++NumRecords;
2663 }
2664}
2665
2666Error BitcodeReader::parseOperandBundleTags() {
2667 if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
2668 return Err;
2669
2670 if (!BundleTags.empty())
2671 return error("Invalid multiple blocks");
2672
2674
2675 while (true) {
2676 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2677 if (!MaybeEntry)
2678 return MaybeEntry.takeError();
2679 BitstreamEntry Entry = MaybeEntry.get();
2680
2681 switch (Entry.Kind) {
2682 case BitstreamEntry::SubBlock: // Handled for us already.
2684 return error("Malformed block");
2686 return Error::success();
2688 // The interesting case.
2689 break;
2690 }
2691
2692 // Tags are implicitly mapped to integers by their order.
2693
2694 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2695 if (!MaybeRecord)
2696 return MaybeRecord.takeError();
2697 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
2698 return error("Invalid operand bundle record");
2699
2700 // OPERAND_BUNDLE_TAG: [strchr x N]
2701 BundleTags.emplace_back();
2702 if (convertToString(Record, 0, BundleTags.back()))
2703 return error("Invalid operand bundle record");
2704 Record.clear();
2705 }
2706}
2707
2708Error BitcodeReader::parseSyncScopeNames() {
2709 if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
2710 return Err;
2711
2712 if (!SSIDs.empty())
2713 return error("Invalid multiple synchronization scope names blocks");
2714
2716 while (true) {
2717 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2718 if (!MaybeEntry)
2719 return MaybeEntry.takeError();
2720 BitstreamEntry Entry = MaybeEntry.get();
2721
2722 switch (Entry.Kind) {
2723 case BitstreamEntry::SubBlock: // Handled for us already.
2725 return error("Malformed block");
2727 if (SSIDs.empty())
2728 return error("Invalid empty synchronization scope names block");
2729 return Error::success();
2731 // The interesting case.
2732 break;
2733 }
2734
2735 // Synchronization scope names are implicitly mapped to synchronization
2736 // scope IDs by their order.
2737
2738 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2739 if (!MaybeRecord)
2740 return MaybeRecord.takeError();
2741 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
2742 return error("Invalid sync scope record");
2743
2744 SmallString<16> SSN;
2745 if (convertToString(Record, 0, SSN))
2746 return error("Invalid sync scope record");
2747
2749 Record.clear();
2750 }
2751}
2752
2753/// Associate a value with its name from the given index in the provided record.
2754Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2755 unsigned NameIndex, Triple &TT) {
2757 if (convertToString(Record, NameIndex, ValueName))
2758 return error("Invalid record");
2759 unsigned ValueID = Record[0];
2760 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2761 return error("Invalid record");
2762 Value *V = ValueList[ValueID];
2763
2764 StringRef NameStr(ValueName.data(), ValueName.size());
2765 if (NameStr.contains(0))
2766 return error("Invalid value name");
2767 V->setName(NameStr);
2768 auto *GO = dyn_cast<GlobalObject>(V);
2769 if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT())
2770 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
2771 return V;
2772}
2773
2774/// Helper to note and return the current location, and jump to the given
2775/// offset.
2777 BitstreamCursor &Stream) {
2778 // Save the current parsing location so we can jump back at the end
2779 // of the VST read.
2780 uint64_t CurrentBit = Stream.GetCurrentBitNo();
2781 if (Error JumpFailed = Stream.JumpToBit(Offset * 32))
2782 return std::move(JumpFailed);
2783 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2784 if (!MaybeEntry)
2785 return MaybeEntry.takeError();
2786 if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock ||
2787 MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID)
2788 return error("Expected value symbol table subblock");
2789 return CurrentBit;
2790}
2791
2792void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2793 Function *F,
2795 // Note that we subtract 1 here because the offset is relative to one word
2796 // before the start of the identification or module block, which was
2797 // historically always the start of the regular bitcode header.
2798 uint64_t FuncWordOffset = Record[1] - 1;
2799 uint64_t FuncBitOffset = FuncWordOffset * 32;
2800 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2801 // Set the LastFunctionBlockBit to point to the last function block.
2802 // Later when parsing is resumed after function materialization,
2803 // we can simply skip that last function block.
2804 if (FuncBitOffset > LastFunctionBlockBit)
2805 LastFunctionBlockBit = FuncBitOffset;
2806}
2807
2808/// Read a new-style GlobalValue symbol table.
2809Error BitcodeReader::parseGlobalValueSymbolTable() {
2810 unsigned FuncBitcodeOffsetDelta =
2811 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2812
2813 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2814 return Err;
2815
2817 while (true) {
2818 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2819 if (!MaybeEntry)
2820 return MaybeEntry.takeError();
2821 BitstreamEntry Entry = MaybeEntry.get();
2822
2823 switch (Entry.Kind) {
2826 return error("Malformed block");
2828 return Error::success();
2830 break;
2831 }
2832
2833 Record.clear();
2834 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2835 if (!MaybeRecord)
2836 return MaybeRecord.takeError();
2837 switch (MaybeRecord.get()) {
2838 case bitc::VST_CODE_FNENTRY: { // [valueid, offset]
2839 unsigned ValueID = Record[0];
2840 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2841 return error("Invalid value reference in symbol table");
2842 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
2843 cast<Function>(ValueList[ValueID]), Record);
2844 break;
2845 }
2846 }
2847 }
2848}
2849
2850/// Parse the value symbol table at either the current parsing location or
2851/// at the given bit offset if provided.
2852Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
2853 uint64_t CurrentBit;
2854 // Pass in the Offset to distinguish between calling for the module-level
2855 // VST (where we want to jump to the VST offset) and the function-level
2856 // VST (where we don't).
2857 if (Offset > 0) {
2858 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
2859 if (!MaybeCurrentBit)
2860 return MaybeCurrentBit.takeError();
2861 CurrentBit = MaybeCurrentBit.get();
2862 // If this module uses a string table, read this as a module-level VST.
2863 if (UseStrtab) {
2864 if (Error Err = parseGlobalValueSymbolTable())
2865 return Err;
2866 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2867 return JumpFailed;
2868 return Error::success();
2869 }
2870 // Otherwise, the VST will be in a similar format to a function-level VST,
2871 // and will contain symbol names.
2872 }
2873
2874 // Compute the delta between the bitcode indices in the VST (the word offset
2875 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2876 // expected by the lazy reader. The reader's EnterSubBlock expects to have
2877 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2878 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2879 // just before entering the VST subblock because: 1) the EnterSubBlock
2880 // changes the AbbrevID width; 2) the VST block is nested within the same
2881 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2882 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2883 // jump to the FUNCTION_BLOCK using this offset later, we don't want
2884 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2885 unsigned FuncBitcodeOffsetDelta =
2886 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2887
2888 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2889 return Err;
2890
2892
2893 Triple TT(TheModule->getTargetTriple());
2894
2895 // Read all the records for this value table.
2897
2898 while (true) {
2899 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2900 if (!MaybeEntry)
2901 return MaybeEntry.takeError();
2902 BitstreamEntry Entry = MaybeEntry.get();
2903
2904 switch (Entry.Kind) {
2905 case BitstreamEntry::SubBlock: // Handled for us already.
2907 return error("Malformed block");
2909 if (Offset > 0)
2910 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2911 return JumpFailed;
2912 return Error::success();
2914 // The interesting case.
2915 break;
2916 }
2917
2918 // Read a record.
2919 Record.clear();
2920 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2921 if (!MaybeRecord)
2922 return MaybeRecord.takeError();
2923 switch (MaybeRecord.get()) {
2924 default: // Default behavior: unknown type.
2925 break;
2926 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
2927 Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2928 if (Error Err = ValOrErr.takeError())
2929 return Err;
2930 ValOrErr.get();
2931 break;
2932 }
2934 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2935 Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2936 if (Error Err = ValOrErr.takeError())
2937 return Err;
2938 Value *V = ValOrErr.get();
2939
2940 // Ignore function offsets emitted for aliases of functions in older
2941 // versions of LLVM.
2942 if (auto *F = dyn_cast<Function>(V))
2943 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2944 break;
2945 }
2948 return error("Invalid bbentry record");
2949 BasicBlock *BB = getBasicBlock(Record[0]);
2950 if (!BB)
2951 return error("Invalid bbentry record");
2952
2953 BB->setName(ValueName.str());
2954 ValueName.clear();
2955 break;
2956 }
2957 }
2958 }
2959}
2960
2961/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2962/// encoding.
2963uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2964 if ((V & 1) == 0)
2965 return V >> 1;
2966 if (V != 1)
2967 return -(V >> 1);
2968 // There is no such thing as -0 with integers. "-0" really means MININT.
2969 return 1ULL << 63;
2970}
2971
2972/// Resolve all of the initializers for global values and aliases that we can.
2973Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2974 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2975 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist;
2976 std::vector<FunctionOperandInfo> FunctionOperandWorklist;
2977
2978 GlobalInitWorklist.swap(GlobalInits);
2979 IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2980 FunctionOperandWorklist.swap(FunctionOperands);
2981
2982 while (!GlobalInitWorklist.empty()) {
2983 unsigned ValID = GlobalInitWorklist.back().second;
2984 if (ValID >= ValueList.size()) {
2985 // Not ready to resolve this yet, it requires something later in the file.
2986 GlobalInits.push_back(GlobalInitWorklist.back());
2987 } else {
2988 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2989 if (!MaybeC)
2990 return MaybeC.takeError();
2991 GlobalInitWorklist.back().first->setInitializer(MaybeC.get());
2992 }
2993 GlobalInitWorklist.pop_back();
2994 }
2995
2996 while (!IndirectSymbolInitWorklist.empty()) {
2997 unsigned ValID = IndirectSymbolInitWorklist.back().second;
2998 if (ValID >= ValueList.size()) {
2999 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
3000 } else {
3001 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
3002 if (!MaybeC)
3003 return MaybeC.takeError();
3004 Constant *C = MaybeC.get();
3005 GlobalValue *GV = IndirectSymbolInitWorklist.back().first;
3006 if (auto *GA = dyn_cast<GlobalAlias>(GV)) {
3007 if (C->getType() != GV->getType())
3008 return error("Alias and aliasee types don't match");
3009 GA->setAliasee(C);
3010 } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) {
3011 GI->setResolver(C);
3012 } else {
3013 return error("Expected an alias or an ifunc");
3014 }
3015 }
3016 IndirectSymbolInitWorklist.pop_back();
3017 }
3018
3019 while (!FunctionOperandWorklist.empty()) {
3020 FunctionOperandInfo &Info = FunctionOperandWorklist.back();
3021 if (Info.PersonalityFn) {
3022 unsigned ValID = Info.PersonalityFn - 1;
3023 if (ValID < ValueList.size()) {
3024 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
3025 if (!MaybeC)
3026 return MaybeC.takeError();
3027 Info.F->setPersonalityFn(MaybeC.get());
3028 Info.PersonalityFn = 0;
3029 }
3030 }
3031 if (Info.Prefix) {
3032 unsigned ValID = Info.Prefix - 1;
3033 if (ValID < ValueList.size()) {
3034 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
3035 if (!MaybeC)
3036 return MaybeC.takeError();
3037 Info.F->setPrefixData(MaybeC.get());
3038 Info.Prefix = 0;
3039 }
3040 }
3041 if (Info.Prologue) {
3042 unsigned ValID = Info.Prologue - 1;
3043 if (ValID < ValueList.size()) {
3044 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
3045 if (!MaybeC)
3046 return MaybeC.takeError();
3047 Info.F->setPrologueData(MaybeC.get());
3048 Info.Prologue = 0;
3049 }
3050 }
3051 if (Info.PersonalityFn || Info.Prefix || Info.Prologue)
3052 FunctionOperands.push_back(Info);
3053 FunctionOperandWorklist.pop_back();
3054 }
3055
3056 return Error::success();
3057}
3058
3060 SmallVector<uint64_t, 8> Words(Vals.size());
3061 transform(Vals, Words.begin(),
3062 BitcodeReader::decodeSignRotatedValue);
3063
3064 return APInt(TypeBits, Words);
3065}
3066
3067Error BitcodeReader::parseConstants() {
3068 if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
3069 return Err;
3070
3072
3073 // Read all the records for this value table.
3074 Type *CurTy = Type::getInt32Ty(Context);
3075 unsigned Int32TyID = getVirtualTypeID(CurTy);
3076 unsigned CurTyID = Int32TyID;
3077 Type *CurElemTy = nullptr;
3078 unsigned NextCstNo = ValueList.size();
3079
3080 while (true) {
3081 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3082 if (!MaybeEntry)
3083 return MaybeEntry.takeError();
3084 BitstreamEntry Entry = MaybeEntry.get();
3085
3086 switch (Entry.Kind) {
3087 case BitstreamEntry::SubBlock: // Handled for us already.
3089 return error("Malformed block");
3091 if (NextCstNo != ValueList.size())
3092 return error("Invalid constant reference");
3093 return Error::success();
3095 // The interesting case.
3096 break;
3097 }
3098
3099 // Read a record.
3100 Record.clear();
3101 Type *VoidType = Type::getVoidTy(Context);
3102 Value *V = nullptr;
3103 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3104 if (!MaybeBitCode)
3105 return MaybeBitCode.takeError();
3106 switch (unsigned BitCode = MaybeBitCode.get()) {
3107 default: // Default behavior: unknown constant
3108 case bitc::CST_CODE_UNDEF: // UNDEF
3109 V = UndefValue::get(CurTy);
3110 break;
3111 case bitc::CST_CODE_POISON: // POISON
3112 V = PoisonValue::get(CurTy);
3113 break;
3114 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
3115 if (Record.empty())
3116 return error("Invalid settype record");
3117 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
3118 return error("Invalid settype record");
3119 if (TypeList[Record[0]] == VoidType)
3120 return error("Invalid constant type");
3121 CurTyID = Record[0];
3122 CurTy = TypeList[CurTyID];
3123 CurElemTy = getPtrElementTypeByID(CurTyID);
3124 continue; // Skip the ValueList manipulation.
3125 case bitc::CST_CODE_NULL: // NULL
3126 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
3127 return error("Invalid type for a constant null value");
3128 if (auto *TETy = dyn_cast<TargetExtType>(CurTy))
3129 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
3130 return error("Invalid type for a constant null value");
3131 V = Constant::getNullValue(CurTy);
3132 break;
3133 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
3134 if (!CurTy->isIntOrIntVectorTy() || Record.empty())
3135 return error("Invalid integer const record");
3136 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
3137 break;
3138 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
3139 if (!CurTy->isIntOrIntVectorTy() || Record.empty())
3140 return error("Invalid wide integer const record");
3141
3142 auto *ScalarTy = cast<IntegerType>(CurTy->getScalarType());
3143 APInt VInt = readWideAPInt(Record, ScalarTy->getBitWidth());
3144 V = ConstantInt::get(CurTy, VInt);
3145 break;
3146 }
3147 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
3148 if (Record.empty())
3149 return error("Invalid float const record");
3150
3151 auto *ScalarTy = CurTy->getScalarType();
3152 if (ScalarTy->isHalfTy())
3153 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEhalf(),
3154 APInt(16, (uint16_t)Record[0])));
3155 else if (ScalarTy->isBFloatTy())
3156 V = ConstantFP::get(
3157 CurTy, APFloat(APFloat::BFloat(), APInt(16, (uint32_t)Record[0])));
3158 else if (ScalarTy->isFloatTy())
3159 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEsingle(),
3160 APInt(32, (uint32_t)Record[0])));
3161 else if (ScalarTy->isDoubleTy())
3162 V = ConstantFP::get(
3163 CurTy, APFloat(APFloat::IEEEdouble(), APInt(64, Record[0])));
3164 else if (ScalarTy->isX86_FP80Ty()) {
3165 // Bits are not stored the same way as a normal i80 APInt, compensate.
3166 uint64_t Rearrange[2];
3167 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
3168 Rearrange[1] = Record[0] >> 48;
3169 V = ConstantFP::get(
3170 CurTy, APFloat(APFloat::x87DoubleExtended(), APInt(80, Rearrange)));
3171 } else if (ScalarTy->isFP128Ty())
3172 V = ConstantFP::get(CurTy,
3173 APFloat(APFloat::IEEEquad(), APInt(128, Record)));
3174 else if (ScalarTy->isPPC_FP128Ty())
3175 V = ConstantFP::get(
3176 CurTy, APFloat(APFloat::PPCDoubleDouble(), APInt(128, Record)));
3177 else
3178 V = UndefValue::get(CurTy);
3179 break;
3180 }
3181
3182 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
3183 if (Record.empty())
3184 return error("Invalid aggregate record");
3185
3186 unsigned Size = Record.size();
3188 for (unsigned i = 0; i != Size; ++i)
3189 Elts.push_back(Record[i]);
3190
3191 if (isa<StructType>(CurTy)) {
3192 V = BitcodeConstant::create(
3193 Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts);
3194 } else if (isa<ArrayType>(CurTy)) {
3195 V = BitcodeConstant::create(Alloc, CurTy,
3196 BitcodeConstant::ConstantArrayOpcode, Elts);
3197 } else if (isa<VectorType>(CurTy)) {
3198 V = BitcodeConstant::create(
3199 Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts);
3200 } else {
3201 V = UndefValue::get(CurTy);
3202 }
3203 break;
3204 }
3205 case bitc::CST_CODE_STRING: // STRING: [values]
3206 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
3207 if (Record.empty())
3208 return error("Invalid string record");
3209
3210 SmallString<16> Elts(Record.begin(), Record.end());
3211 V = ConstantDataArray::getString(Context, Elts,
3212 BitCode == bitc::CST_CODE_CSTRING);
3213 break;
3214 }
3215 case bitc::CST_CODE_DATA: {// DATA: [n x value]
3216 if (Record.empty())
3217 return error("Invalid data record");
3218
3219 Type *EltTy;
3220 if (auto *Array = dyn_cast<ArrayType>(CurTy))
3221 EltTy = Array->getElementType();
3222 else
3223 EltTy = cast<VectorType>(CurTy)->getElementType();
3224 if (EltTy->isIntegerTy(8)) {
3225 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
3226 if (isa<VectorType>(CurTy))
3227 V = ConstantDataVector::get(Context, Elts);
3228 else
3229 V = ConstantDataArray::get(Context, Elts);
3230 } else if (EltTy->isIntegerTy(16)) {
3231 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3232 if (isa<VectorType>(CurTy))
3233 V = ConstantDataVector::get(Context, Elts);
3234 else
3235 V = ConstantDataArray::get(Context, Elts);
3236 } else if (EltTy->isIntegerTy(32)) {
3237 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3238 if (isa<VectorType>(CurTy))
3239 V = ConstantDataVector::get(Context, Elts);
3240 else
3241 V = ConstantDataArray::get(Context, Elts);
3242 } else if (EltTy->isIntegerTy(64)) {
3243 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3244 if (isa<VectorType>(CurTy))
3245 V = ConstantDataVector::get(Context, Elts);
3246 else
3247 V = ConstantDataArray::get(Context, Elts);
3248 } else if (EltTy->isHalfTy()) {
3249 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3250 if (isa<VectorType>(CurTy))
3251 V = ConstantDataVector::getFP(EltTy, Elts);
3252 else
3253 V = ConstantDataArray::getFP(EltTy, Elts);
3254 } else if (EltTy->isBFloatTy()) {
3255 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3256 if (isa<VectorType>(CurTy))
3257 V = ConstantDataVector::getFP(EltTy, Elts);
3258 else
3259 V = ConstantDataArray::getFP(EltTy, Elts);
3260 } else if (EltTy->isFloatTy()) {
3261 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3262 if (isa<VectorType>(CurTy))
3263 V = ConstantDataVector::getFP(EltTy, Elts);
3264 else
3265 V = ConstantDataArray::getFP(EltTy, Elts);
3266 } else if (EltTy->isDoubleTy()) {
3267 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3268 if (isa<VectorType>(CurTy))
3269 V = ConstantDataVector::getFP(EltTy, Elts);
3270 else
3271 V = ConstantDataArray::getFP(EltTy, Elts);
3272 } else {
3273 return error("Invalid type for value");
3274 }
3275 break;
3276 }
3277 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
3278 if (Record.size() < 2)
3279 return error("Invalid unary op constexpr record");
3280 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
3281 if (Opc < 0) {
3282 V = UndefValue::get(CurTy); // Unknown unop.
3283 } else {
3284 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]);
3285 }
3286 break;
3287 }
3288 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
3289 if (Record.size() < 3)
3290 return error("Invalid binary op constexpr record");
3291 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
3292 if (Opc < 0) {
3293 V = UndefValue::get(CurTy); // Unknown binop.
3294 } else {
3295 uint8_t Flags = 0;
3296 if (Record.size() >= 4) {
3297 if (Opc == Instruction::Add ||
3298 Opc == Instruction::Sub ||
3299 Opc == Instruction::Mul ||
3300 Opc == Instruction::Shl) {
3301 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3303 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3305 } else if (Opc == Instruction::SDiv ||
3306 Opc == Instruction::UDiv ||
3307 Opc == Instruction::LShr ||
3308 Opc == Instruction::AShr) {
3309 if (Record[3] & (1 << bitc::PEO_EXACT))
3311 }
3312 }
3313 V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags},
3314 {(unsigned)Record[1], (unsigned)Record[2]});
3315 }
3316 break;
3317 }
3318 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
3319 if (Record.size() < 3)
3320 return error("Invalid cast constexpr record");
3321 int Opc = getDecodedCastOpcode(Record[0]);
3322 if (Opc < 0) {
3323 V = UndefValue::get(CurTy); // Unknown cast.
3324 } else {
3325 unsigned OpTyID = Record[1];
3326 Type *OpTy = getTypeByID(OpTyID);
3327 if (!OpTy)
3328 return error("Invalid cast constexpr record");
3329 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]);
3330 }
3331 break;
3332 }
3333 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
3334 case bitc::CST_CODE_CE_GEP_OLD: // [ty, n x operands]
3335 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD: // [ty, flags, n x
3336 // operands]
3337 case bitc::CST_CODE_CE_GEP: // [ty, flags, n x operands]
3338 case bitc::CST_CODE_CE_GEP_WITH_INRANGE: { // [ty, flags, start, end, n x
3339 // operands]
3340 if (Record.size() < 2)
3341 return error("Constant GEP record must have at least two elements");
3342 unsigned OpNum = 0;
3343 Type *PointeeType = nullptr;
3346 BitCode == bitc::CST_CODE_CE_GEP || Record.size() % 2)
3347 PointeeType = getTypeByID(Record[OpNum++]);
3348
3349 uint64_t Flags = 0;
3350 std::optional<ConstantRange> InRange;
3352 uint64_t Op = Record[OpNum++];
3353 Flags = Op & 1; // inbounds
3354 unsigned InRangeIndex = Op >> 1;
3355 // "Upgrade" inrange by dropping it. The feature is too niche to
3356 // bother.
3357 (void)InRangeIndex;
3358 } else if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE) {
3359 Flags = Record[OpNum++];
3360 Expected<ConstantRange> MaybeInRange = readConstantRange(Record, OpNum);
3361 if (!MaybeInRange)
3362 return MaybeInRange.takeError();
3363 InRange = MaybeInRange.get();
3364 } else if (BitCode == bitc::CST_CODE_CE_GEP) {
3365 Flags = Record[OpNum++];
3366 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
3367 Flags = (1 << bitc::GEP_INBOUNDS);
3368
3370 unsigned BaseTypeID = Record[OpNum];
3371 while (OpNum != Record.size()) {
3372 unsigned ElTyID = Record[OpNum++];
3373 Type *ElTy = getTypeByID(ElTyID);
3374 if (!ElTy)
3375 return error("Invalid getelementptr constexpr record");
3376 Elts.push_back(Record[OpNum++]);
3377 }
3378
3379 if (Elts.size() < 1)
3380 return error("Invalid gep with no operands");
3381
3382 Type *BaseType = getTypeByID(BaseTypeID);
3383 if (isa<VectorType>(BaseType)) {
3384 BaseTypeID = getContainedTypeID(BaseTypeID, 0);
3385 BaseType = getTypeByID(BaseTypeID);
3386 }
3387
3388 PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType);
3389 if (!OrigPtrTy)
3390 return error("GEP base operand must be pointer or vector of pointer");
3391
3392 if (!PointeeType) {
3393 PointeeType = getPtrElementTypeByID(BaseTypeID);
3394 if (!PointeeType)
3395 return error("Missing element type for old-style constant GEP");
3396 }
3397
3398 V = BitcodeConstant::create(
3399 Alloc, CurTy,
3400 {Instruction::GetElementPtr, uint8_t(Flags), PointeeType, InRange},
3401 Elts);
3402 break;
3403 }
3404 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
3405 if (Record.size() < 3)
3406 return error("Invalid select constexpr record");
3407
3408 V = BitcodeConstant::create(
3409 Alloc, CurTy, Instruction::Select,
3410 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3411 break;
3412 }
3414 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
3415 if (Record.size() < 3)
3416 return error("Invalid extractelement constexpr record");
3417 unsigned OpTyID = Record[0];
3418 VectorType *OpTy =
3419 dyn_cast_or_null<VectorType>(getTypeByID(OpTyID));
3420 if (!OpTy)
3421 return error("Invalid extractelement constexpr record");
3422 unsigned IdxRecord;
3423 if (Record.size() == 4) {
3424 unsigned IdxTyID = Record[2];
3425 Type *IdxTy = getTypeByID(IdxTyID);
3426 if (!IdxTy)
3427 return error("Invalid extractelement constexpr record");
3428 IdxRecord = Record[3];
3429 } else {
3430 // Deprecated, but still needed to read old bitcode files.
3431 IdxRecord = Record[2];
3432 }
3433 V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement,
3434 {(unsigned)Record[1], IdxRecord});
3435 break;
3436 }
3438 : { // CE_INSERTELT: [opval, opval, opty, opval]
3439 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3440 if (Record.size() < 3 || !OpTy)
3441 return error("Invalid insertelement constexpr record");
3442 unsigned IdxRecord;
3443 if (Record.size() == 4) {
3444 unsigned IdxTyID = Record[2];
3445 Type *IdxTy = getTypeByID(IdxTyID);
3446 if (!IdxTy)
3447 return error("Invalid insertelement constexpr record");
3448 IdxRecord = Record[3];
3449 } else {
3450 // Deprecated, but still needed to read old bitcode files.
3451 IdxRecord = Record[2];
3452 }
3453 V = BitcodeConstant::create(
3454 Alloc, CurTy, Instruction::InsertElement,
3455 {(unsigned)Record[0], (unsigned)Record[1], IdxRecord});
3456 break;
3457 }
3458 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
3459 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3460 if (Record.size() < 3 || !OpTy)
3461 return error("Invalid shufflevector constexpr record");
3462 V = BitcodeConstant::create(
3463 Alloc, CurTy, Instruction::ShuffleVector,
3464 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3465 break;
3466 }
3467 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
3468 VectorType *RTy = dyn_cast<VectorType>(CurTy);
3469 VectorType *OpTy =
3470 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
3471 if (Record.size() < 4 || !RTy || !OpTy)
3472 return error("Invalid shufflevector constexpr record");
3473 V = BitcodeConstant::create(
3474 Alloc, CurTy, Instruction::ShuffleVector,
3475 {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]});
3476 break;
3477 }
3478 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
3479 if (Record.size() < 4)
3480 return error("Invalid cmp constexpt record");
3481 unsigned OpTyID = Record[0];
3482 Type *OpTy = getTypeByID(OpTyID);
3483 if (!OpTy)
3484 return error("Invalid cmp constexpr record");
3485 V = BitcodeConstant::create(
3486 Alloc, CurTy,
3487 {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp
3488 : Instruction::ICmp),
3489 (uint8_t)Record[3]},
3490 {(unsigned)Record[1], (unsigned)Record[2]});
3491 break;
3492 }
3493 // This maintains backward compatibility, pre-asm dialect keywords.
3494 // Deprecated, but still needed to read old bitcode files.
3496 if (Record.size() < 2)
3497 return error("Invalid inlineasm record");
3498 std::string AsmStr, ConstrStr;
3499 bool HasSideEffects = Record[0] & 1;
3500 bool IsAlignStack = Record[0] >> 1;
3501 unsigned AsmStrSize = Record[1];
3502 if (2+AsmStrSize >= Record.size())
3503 return error("Invalid inlineasm record");
3504 unsigned ConstStrSize = Record[2+AsmStrSize];
3505 if (3+AsmStrSize+ConstStrSize > Record.size())
3506 return error("Invalid inlineasm record");
3507
3508 for (unsigned i = 0; i != AsmStrSize; ++i)
3509 AsmStr += (char)Record[2+i];
3510 for (unsigned i = 0; i != ConstStrSize; ++i)
3511 ConstrStr += (char)Record[3+AsmStrSize+i];
3512 UpgradeInlineAsmString(&AsmStr);
3513 if (!CurElemTy)
3514 return error("Missing element type for old-style inlineasm");
3515 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3516 HasSideEffects, IsAlignStack);
3517 break;
3518 }
3519 // This version adds support for the asm dialect keywords (e.g.,
3520 // inteldialect).
3522 if (Record.size() < 2)
3523 return error("Invalid inlineasm record");
3524 std::string AsmStr, ConstrStr;
3525 bool HasSideEffects = Record[0] & 1;
3526 bool IsAlignStack = (Record[0] >> 1) & 1;
3527 unsigned AsmDialect = Record[0] >> 2;
3528 unsigned AsmStrSize = Record[1];
3529 if (2+AsmStrSize >= Record.size())
3530 return error("Invalid inlineasm record");
3531 unsigned ConstStrSize = Record[2+AsmStrSize];
3532 if (3+AsmStrSize+ConstStrSize > Record.size())
3533 return error("Invalid inlineasm record");
3534
3535 for (unsigned i = 0; i != AsmStrSize; ++i)
3536 AsmStr += (char)Record[2+i];
3537 for (unsigned i = 0; i != ConstStrSize; ++i)
3538 ConstrStr += (char)Record[3+AsmStrSize+i];
3539 UpgradeInlineAsmString(&AsmStr);
3540 if (!CurElemTy)
3541 return error("Missing element type for old-style inlineasm");
3542 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3543 HasSideEffects, IsAlignStack,
3544 InlineAsm::AsmDialect(AsmDialect));
3545 break;
3546 }
3547 // This version adds support for the unwind keyword.
3549 if (Record.size() < 2)
3550 return error("Invalid inlineasm record");
3551 unsigned OpNum = 0;
3552 std::string AsmStr, ConstrStr;
3553 bool HasSideEffects = Record[OpNum] & 1;
3554 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3555 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3556 bool CanThrow = (Record[OpNum] >> 3) & 1;
3557 ++OpNum;
3558 unsigned AsmStrSize = Record[OpNum];
3559 ++OpNum;
3560 if (OpNum + AsmStrSize >= Record.size())
3561 return error("Invalid inlineasm record");
3562 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3563 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3564 return error("Invalid inlineasm record");
3565
3566 for (unsigned i = 0; i != AsmStrSize; ++i)
3567 AsmStr += (char)Record[OpNum + i];
3568 ++OpNum;
3569 for (unsigned i = 0; i != ConstStrSize; ++i)
3570 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3571 UpgradeInlineAsmString(&AsmStr);
3572 if (!CurElemTy)
3573 return error("Missing element type for old-style inlineasm");
3574 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3575 HasSideEffects, IsAlignStack,
3576 InlineAsm::AsmDialect(AsmDialect), CanThrow);
3577 break;
3578 }
3579 // This version adds explicit function type.
3581 if (Record.size() < 3)
3582 return error("Invalid inlineasm record");
3583 unsigned OpNum = 0;
3584 auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum]));
3585 ++OpNum;
3586 if (!FnTy)
3587 return error("Invalid inlineasm record");
3588 std::string AsmStr, ConstrStr;
3589 bool HasSideEffects = Record[OpNum] & 1;
3590 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3591 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3592 bool CanThrow = (Record[OpNum] >> 3) & 1;
3593 ++OpNum;
3594 unsigned AsmStrSize = Record[OpNum];
3595 ++OpNum;
3596 if (OpNum + AsmStrSize >= Record.size())
3597 return error("Invalid inlineasm record");
3598 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3599 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3600 return error("Invalid inlineasm record");
3601
3602 for (unsigned i = 0; i != AsmStrSize; ++i)
3603 AsmStr += (char)Record[OpNum + i];
3604 ++OpNum;
3605 for (unsigned i = 0; i != ConstStrSize; ++i)
3606 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3607 UpgradeInlineAsmString(&AsmStr);
3608 V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
3609 InlineAsm::AsmDialect(AsmDialect), CanThrow);
3610 break;
3611 }
3613 if (Record.size() < 3)
3614 return error("Invalid blockaddress record");
3615 unsigned FnTyID = Record[0];
3616 Type *FnTy = getTypeByID(FnTyID);
3617 if (!FnTy)
3618 return error("Invalid blockaddress record");
3619 V = BitcodeConstant::create(
3620 Alloc, CurTy,
3621 {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]},
3622 Record[1]);
3623 break;
3624 }
3626 if (Record.size() < 2)
3627 return error("Invalid dso_local record");
3628 unsigned GVTyID = Record[0];
3629 Type *GVTy = getTypeByID(GVTyID);
3630 if (!GVTy)
3631 return error("Invalid dso_local record");
3632 V = BitcodeConstant::create(
3633 Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]);
3634 break;
3635 }
3637 if (Record.size() < 2)
3638 return error("Invalid no_cfi record");
3639 unsigned GVTyID = Record[0];
3640 Type *GVTy = getTypeByID(GVTyID);
3641 if (!GVTy)
3642 return error("Invalid no_cfi record");
3643 V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode,
3644 Record[1]);
3645 break;
3646 }
3647 }
3648
3649 assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");
3650 if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID))
3651 return Err;
3652 ++NextCstNo;
3653 }
3654}
3655
3656Error BitcodeReader::parseUseLists() {
3657 if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
3658 return Err;
3659
3660 // Read all the records.
3662
3663 while (true) {
3664 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3665 if (!MaybeEntry)
3666 return MaybeEntry.takeError();
3667 BitstreamEntry Entry = MaybeEntry.get();
3668
3669 switch (Entry.Kind) {
3670 case BitstreamEntry::SubBlock: // Handled for us already.
3672 return error("Malformed block");
3674 return Error::success();
3676 // The interesting case.
3677 break;
3678 }
3679
3680 // Read a use list record.
3681 Record.clear();
3682 bool IsBB = false;
3683 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
3684 if (!MaybeRecord)
3685 return MaybeRecord.takeError();
3686 switch (MaybeRecord.get()) {
3687 default: // Default behavior: unknown type.
3688 break;
3690 IsBB = true;
3691 [[fallthrough]];
3693 unsigned RecordLength = Record.size();
3694 if (RecordLength < 3)
3695 // Records should have at least an ID and two indexes.
3696 return error("Invalid record");
3697 unsigned ID = Record.pop_back_val();
3698
3699 Value *V;
3700 if (IsBB) {
3701 assert(ID < FunctionBBs.size() && "Basic block not found");
3702 V = FunctionBBs[ID];
3703 } else
3704 V = ValueList[ID];
3705 unsigned NumUses = 0;
3707 for (const Use &U : V->materialized_uses()) {
3708 if (++NumUses > Record.size())
3709 break;
3710 Order[&U] = Record[NumUses - 1];
3711 }
3712 if (Order.size() != Record.size() || NumUses > Record.size())
3713 // Mismatches can happen if the functions are being materialized lazily
3714 // (out-of-order), or a value has been upgraded.
3715 break;
3716
3717 V->sortUseList([&](const Use &L, const Use &R) {
3718 return Order.lookup(&L) < Order.lookup(&R);
3719 });
3720 break;
3721 }
3722 }
3723 }
3724}
3725
3726/// When we see the block for metadata, remember where it is and then skip it.
3727/// This lets us lazily deserialize the metadata.
3728Error BitcodeReader::rememberAndSkipMetadata() {
3729 // Save the current stream state.
3730 uint64_t CurBit = Stream.GetCurrentBitNo();
3731 DeferredMetadataInfo.push_back(CurBit);
3732
3733 // Skip over the block for now.
3734 if (Error Err = Stream.SkipBlock())
3735 return Err;
3736 return Error::success();
3737}
3738
3739Error BitcodeReader::materializeMetadata() {
3740 for (uint64_t BitPos : DeferredMetadataInfo) {
3741 // Move the bit stream to the saved position.
3742 if (Error JumpFailed = Stream.JumpToBit(BitPos))
3743 return JumpFailed;
3744 if (Error Err = MDLoader->parseModuleMetadata())
3745 return Err;
3746 }
3747
3748 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
3749 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
3750 // multiple times.
3751 if (!TheModule->getNamedMetadata("llvm.linker.options")) {
3752 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
3753 NamedMDNode *LinkerOpts =
3754 TheModule->getOrInsertNamedMetadata("llvm.linker.options");
3755 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
3756 LinkerOpts->addOperand(cast<MDNode>(MDOptions));
3757 }
3758 }
3759
3760 DeferredMetadataInfo.clear();
3761 return Error::success();
3762}
3763
3764void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3765
3766/// When we see the block for a function body, remember where it is and then
3767/// skip it. This lets us lazily deserialize the functions.
3768Error BitcodeReader::rememberAndSkipFunctionBody() {
3769 // Get the function we are talking about.
3770 if (FunctionsWithBodies.empty())
3771 return error("Insufficient function protos");
3772
3773 Function *Fn = FunctionsWithBodies.back();
3774 FunctionsWithBodies.pop_back();
3775
3776 // Save the current stream state.
3777 uint64_t CurBit = Stream.GetCurrentBitNo();
3778 assert(
3779 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3780 "Mismatch between VST and scanned function offsets");
3781 DeferredFunctionInfo[Fn] = CurBit;
3782
3783 // Skip over the function block for now.
3784 if (Error Err = Stream.SkipBlock())
3785 return Err;
3786 return Error::success();
3787}
3788
3789Error BitcodeReader::globalCleanup() {
3790 // Patch the initializers for globals and aliases up.
3791 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3792 return Err;
3793 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3794 return error("Malformed global initializer set");
3795
3796 // Look for intrinsic functions which need to be upgraded at some point
3797 // and functions that need to have their function attributes upgraded.
3798 for (Function &F : *TheModule) {
3799 MDLoader->upgradeDebugIntrinsics(F);
3800 Function *NewFn;
3801 // If PreserveInputDbgFormat=true, then we don't know whether we want
3802 // intrinsics or records, and we won't perform any conversions in either
3803 // case, so don't upgrade intrinsics to records.
3805 &F, NewFn, PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE))
3806 UpgradedIntrinsics[&F] = NewFn;
3807 // Look for functions that rely on old function attribute behavior.
3809 }
3810
3811 // Look for global variables which need to be renamed.
3812 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
3813 for (GlobalVariable &GV : TheModule->globals())
3814 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))
3815 UpgradedVariables.emplace_back(&GV, Upgraded);
3816 for (auto &Pair : UpgradedVariables) {
3817 Pair.first->eraseFromParent();
3818 TheModule->insertGlobalVariable(Pair.second);
3819 }
3820
3821 // Force deallocation of memory for these vectors to favor the client that
3822 // want lazy deserialization.
3823 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
3824 std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits);
3825 return Error::success();
3826}
3827
3828/// Support for lazy parsing of function bodies. This is required if we
3829/// either have an old bitcode file without a VST forward declaration record,
3830/// or if we have an anonymous function being materialized, since anonymous
3831/// functions do not have a name and are therefore not in the VST.
3832Error BitcodeReader::rememberAndSkipFunctionBodies() {
3833 if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))
3834 return JumpFailed;
3835
3836 if (Stream.AtEndOfStream())
3837 return error("Could not find function in stream");
3838
3839 if (!SeenFirstFunctionBody)
3840 return error("Trying to materialize functions before seeing function blocks");
3841
3842 // An old bitcode file with the symbol table at the end would have
3843 // finished the parse greedily.
3844 assert(SeenValueSymbolTable);
3845
3847
3848 while (true) {
3849 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3850 if (!MaybeEntry)
3851 return MaybeEntry.takeError();
3852 llvm::BitstreamEntry Entry = MaybeEntry.get();
3853
3854 switch (Entry.Kind) {
3855 default:
3856 return error("Expect SubBlock");
3858 switch (Entry.ID) {
3859 default:
3860 return error("Expect function block");
3862 if (Error Err = rememberAndSkipFunctionBody())
3863 return Err;
3864 NextUnreadBit = Stream.GetCurrentBitNo();
3865 return Error::success();
3866 }
3867 }
3868 }
3869}
3870
3871Error BitcodeReaderBase::readBlockInfo() {
3873 Stream.ReadBlockInfoBlock();
3874 if (!MaybeNewBlockInfo)
3875 return MaybeNewBlockInfo.takeError();
3876 std::optional<BitstreamBlockInfo> NewBlockInfo =
3877 std::move(MaybeNewBlockInfo.get());
3878 if (!NewBlockInfo)
3879 return error("Malformed block");
3880 BlockInfo = std::move(*NewBlockInfo);
3881 return Error::success();
3882}
3883
3884Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
3885 // v1: [selection_kind, name]
3886 // v2: [strtab_offset, strtab_size, selection_kind]
3888 std::tie(Name, Record) = readNameFromStrtab(Record);
3889
3890 if (Record.empty())
3891 return error("Invalid record");
3893 std::string OldFormatName;
3894 if (!UseStrtab) {
3895 if (Record.size() < 2)
3896 return error("Invalid record");
3897 unsigned ComdatNameSize = Record[1];
3898 if (ComdatNameSize > Record.size() - 2)
3899 return error("Comdat name size too large");
3900 OldFormatName.reserve(ComdatNameSize);
3901 for (unsigned i = 0; i != ComdatNameSize; ++i)
3902 OldFormatName += (char)Record[2 + i];
3903 Name = OldFormatName;
3904 }
3905 Comdat *C = TheModule->getOrInsertComdat(Name);
3906 C->setSelectionKind(SK);
3907 ComdatList.push_back(C);
3908 return Error::success();
3909}
3910
3911static void inferDSOLocal(GlobalValue *GV) {
3912 // infer dso_local from linkage and visibility if it is not encoded.
3913 if (GV->hasLocalLinkage() ||
3915 GV->setDSOLocal(true);
3916}
3917
3920 if (V & (1 << 0))
3921 Meta.NoAddress = true;
3922 if (V & (1 << 1))
3923 Meta.NoHWAddress = true;
3924 if (V & (1 << 2))
3925 Meta.Memtag = true;
3926 if (V & (1 << 3))
3927 Meta.IsDynInit = true;
3928 return Meta;
3929}
3930
3931Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
3932 // v1: [pointer type, isconst, initid, linkage, alignment, section,
3933 // visibility, threadlocal, unnamed_addr, externally_initialized,
3934 // dllstorageclass, comdat, attributes, preemption specifier,
3935 // partition strtab offset, partition strtab size] (name in VST)
3936 // v2: [strtab_offset, strtab_size, v1]
3937 // v3: [v2, code_model]
3939 std::tie(Name, Record) = readNameFromStrtab(Record);
3940
3941 if (Record.size() < 6)
3942 return error("Invalid record");
3943 unsigned TyID = Record[0];
3944 Type *Ty = getTypeByID(TyID);
3945 if (!Ty)
3946 return error("Invalid record");
3947 bool isConstant = Record[1] & 1;
3948 bool explicitType = Record[1] & 2;
3949 unsigned AddressSpace;
3950 if (explicitType) {
3951 AddressSpace = Record[1] >> 2;
3952 } else {
3953 if (!Ty->isPointerTy())
3954 return error("Invalid type for value");
3955 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3956 TyID = getContainedTypeID(TyID);
3957 Ty = getTypeByID(TyID);
3958 if (!Ty)
3959 return error("Missing element type for old-style global");
3960 }
3961
3962 uint64_t RawLinkage = Record[3];
3964 MaybeAlign Alignment;
3965 if (Error Err = parseAlignmentValue(Record[4], Alignment))
3966 return Err;
3967 std::string Section;
3968 if (Record[5]) {
3969 if (Record[5] - 1 >= SectionTable.size())
3970 return error("Invalid ID");
3971 Section = SectionTable[Record[5] - 1];
3972 }
3974 // Local linkage must have default visibility.
3975 // auto-upgrade `hidden` and `protected` for old bitcode.
3976 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3977 Visibility = getDecodedVisibility(Record[6]);
3978
3979 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3980 if (Record.size() > 7)
3982
3983 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3984 if (Record.size() > 8)
3985 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
3986
3987 bool ExternallyInitialized = false;
3988 if (Record.size() > 9)
3989 ExternallyInitialized = Record[9];
3990
3991 GlobalVariable *NewGV =
3992 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
3993 nullptr, TLM, AddressSpace, ExternallyInitialized);
3994 if (Alignment)
3995 NewGV->setAlignment(*Alignment);
3996 if (!Section.empty())
3997 NewGV->setSection(Section);
3998 NewGV->setVisibility(Visibility);
3999 NewGV->setUnnamedAddr(UnnamedAddr);
4000
4001 if (Record.size() > 10) {
4002 // A GlobalValue with local linkage cannot have a DLL storage class.
4003 if (!NewGV->hasLocalLinkage()) {
4005 }
4006 } else {
4007 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
4008 }
4009
4010 ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID));
4011
4012 // Remember which value to use for the global initializer.
4013 if (unsigned InitID = Record[2])
4014 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
4015
4016 if (Record.size() > 11) {
4017 if (unsigned ComdatID = Record[11]) {
4018 if (ComdatID > ComdatList.size())
4019 return error("Invalid global variable comdat ID");
4020 NewGV->setComdat(ComdatList[ComdatID - 1]);
4021 }
4022 } else if (hasImplicitComdat(RawLinkage)) {
4023 ImplicitComdatObjects.insert(NewGV);
4024 }
4025
4026 if (Record.size() > 12) {
4027 auto AS = getAttributes(Record[12]).getFnAttrs();
4028 NewGV->setAttributes(AS);
4029 }
4030
4031 if (Record.size() > 13) {
4033 }
4034 inferDSOLocal(NewGV);
4035
4036 // Check whether we have enough values to read a partition name.
4037 if (Record.size() > 15)
4038 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
4039
4040 if (Record.size() > 16 && Record[16]) {
4043 NewGV->setSanitizerMetadata(Meta);
4044 }
4045
4046 if (Record.size() > 17 && Record[17]) {
4047 if (auto CM = getDecodedCodeModel(Record[17]))
4048 NewGV->setCodeModel(*CM);
4049 else
4050 return error("Invalid global variable code model");
4051 }
4052
4053 return Error::success();
4054}
4055
4056void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) {
4057 if (ValueTypeCallback) {
4058 (*ValueTypeCallback)(
4059 F, TypeID, [this](unsigned I) { return getTypeByID(I); },
4060 [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); });
4061 }
4062}
4063
4064Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
4065 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
4066 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
4067 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
4068 // v2: [strtab_offset, strtab_size, v1]
4070 std::tie(Name, Record) = readNameFromStrtab(Record);
4071
4072 if (Record.size() < 8)
4073 return error("Invalid record");
4074 unsigned FTyID = Record[0];
4075 Type *FTy = getTypeByID(FTyID);
4076 if (!FTy)
4077 return error("Invalid record");
4078 if (isa<PointerType>(FTy)) {
4079 FTyID = getContainedTypeID(FTyID, 0);
4080 FTy = getTypeByID(FTyID);
4081 if (!FTy)
4082 return error("Missing element type for old-style function");
4083 }
4084
4085 if (!isa<FunctionType>(FTy))
4086 return error("Invalid type for value");
4087 auto CC = static_cast<CallingConv::ID>(Record[1]);
4088 if (CC & ~CallingConv::MaxID)
4089 return error("Invalid calling convention ID");
4090
4091 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
4092 if (Record.size() > 16)
4093 AddrSpace = Record[16];
4094
4095 Function *Func =
4096 Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,
4097 AddrSpace, Name, TheModule);
4098
4099 assert(Func->getFunctionType() == FTy &&
4100 "Incorrect fully specified type provided for function");
4101 FunctionTypeIDs[Func] = FTyID;
4102
4103 Func->setCallingConv(CC);
4104 bool isProto = Record[2];
4105 uint64_t RawLinkage = Record[3];
4106 Func->setLinkage(getDecodedLinkage(RawLinkage));
4107 Func->setAttributes(getAttributes(Record[4]));
4108 callValueTypeCallback(Func, FTyID);
4109
4110 // Upgrade any old-style byval or sret without a type by propagating the
4111 // argument's pointee type. There should be no opaque pointers where the byval
4112 // type is implicit.
4113 for (unsigned i = 0; i != Func->arg_size(); ++i) {
4114 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4115 Attribute::InAlloca}) {
4116 if (!Func->hasParamAttribute(i, Kind))
4117 continue;
4118
4119 if (Func->getParamAttribute(i, Kind).getValueAsType())
4120 continue;
4121
4122 Func->removeParamAttr(i, Kind);
4123
4124 unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1);
4125 Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID);
4126 if (!PtrEltTy)
4127 return error("Missing param element type for attribute upgrade");
4128
4129 Attribute NewAttr;
4130 switch (Kind) {
4131 case Attribute::ByVal:
4132 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
4133 break;
4134 case Attribute::StructRet:
4135 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
4136 break;
4137 case Attribute::InAlloca:
4138 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
4139 break;
4140 default:
4141 llvm_unreachable("not an upgraded type attribute");
4142 }
4143
4144 Func->addParamAttr(i, NewAttr);
4145 }
4146 }
4147
4148 if (Func->getCallingConv() == CallingConv::X86_INTR &&
4149 !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) {
4150 unsigned ParamTypeID = getContainedTypeID(FTyID, 1);
4151 Type *ByValTy = getPtrElementTypeByID(ParamTypeID);
4152 if (!ByValTy)
4153 return error("Missing param element type for x86_intrcc upgrade");
4154 Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy);
4155 Func->addParamAttr(0, NewAttr);
4156 }
4157
4158 MaybeAlign Alignment;
4159 if (Error Err = parseAlignmentValue(Record[5], Alignment))
4160 return Err;
4161 if (Alignment)
4162 Func->setAlignment(*Alignment);
4163 if (Record[6]) {
4164 if (Record[6] - 1 >= SectionTable.size())
4165 return error("Invalid ID");
4166 Func->setSection(SectionTable[Record[6] - 1]);
4167 }
4168 // Local linkage must have default visibility.
4169 // auto-upgrade `hidden` and `protected` for old bitcode.
4170 if (!Func->hasLocalLinkage())
4171 Func->setVisibility(getDecodedVisibility(Record[7]));
4172 if (Record.size() > 8 && Record[8]) {
4173 if (Record[8] - 1 >= GCTable.size())
4174 return error("Invalid ID");
4175 Func->setGC(GCTable[Record[8] - 1]);
4176 }
4177 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
4178 if (Record.size() > 9)
4179 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
4180 Func->setUnnamedAddr(UnnamedAddr);
4181
4182 FunctionOperandInfo OperandInfo = {Func, 0, 0, 0};
4183 if (Record.size() > 10)
4184 OperandInfo.Prologue = Record[10];
4185
4186 if (Record.size() > 11) {
4187 // A GlobalValue with local linkage cannot have a DLL storage class.
4188 if (!Func->hasLocalLinkage()) {
4189 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
4190 }
4191 } else {
4192 upgradeDLLImportExportLinkage(Func, RawLinkage);
4193 }
4194
4195 if (Record.size() > 12) {
4196 if (unsigned ComdatID = Record[12]) {
4197 if (ComdatID > ComdatList.size())
4198 return error("Invalid function comdat ID");
4199 Func->setComdat(ComdatList[ComdatID - 1]);
4200 }
4201 } else if (hasImplicitComdat(RawLinkage)) {
4202 ImplicitComdatObjects.insert(Func);
4203 }
4204
4205 if (Record.size() > 13)
4206 OperandInfo.Prefix = Record[13];
4207
4208 if (Record.size() > 14)
4209 OperandInfo.PersonalityFn = Record[14];
4210
4211 if (Record.size() > 15) {
4212 Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
4213 }
4214 inferDSOLocal(Func);
4215
4216 // Record[16] is the address space number.
4217
4218 // Check whether we have enough values to read a partition name. Also make
4219 // sure Strtab has enough values.
4220 if (Record.size() > 18 && Strtab.data() &&
4221 Record[17] + Record[18] <= Strtab.size()) {
4222 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
4223 }
4224
4225 ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));
4226
4227 if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)
4228 FunctionOperands.push_back(OperandInfo);
4229
4230 // If this is a function with a body, remember the prototype we are
4231 // creating now, so that we can match up the body with them later.
4232 if (!isProto) {
4233 Func->setIsMaterializable(true);
4234 FunctionsWithBodies.push_back(Func);
4235 DeferredFunctionInfo[Func] = 0;
4236 }
4237 return Error::success();
4238}
4239
4240Error BitcodeReader::parseGlobalIndirectSymbolRecord(
4241 unsigned BitCode, ArrayRef<uint64_t> Record) {
4242 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
4243 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
4244 // dllstorageclass, threadlocal, unnamed_addr,
4245 // preemption specifier] (name in VST)
4246 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
4247 // visibility, dllstorageclass, threadlocal, unnamed_addr,
4248 // preemption specifier] (name in VST)
4249 // v2: [strtab_offset, strtab_size, v1]
4251 std::tie(Name, Record) = readNameFromStrtab(Record);
4252
4253 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
4254 if (Record.size() < (3 + (unsigned)NewRecord))
4255 return error("Invalid record");
4256 unsigned OpNum = 0;
4257 unsigned TypeID = Record[OpNum++];
4258 Type *Ty = getTypeByID(TypeID);
4259 if (!Ty)
4260 return error("Invalid record");
4261
4262 unsigned AddrSpace;
4263 if (!NewRecord) {
4264 auto *PTy = dyn_cast<PointerType>(Ty);
4265 if (!PTy)
4266 return error("Invalid type for value");
4267 AddrSpace = PTy->getAddressSpace();
4268 TypeID = getContainedTypeID(TypeID);
4269 Ty = getTypeByID(TypeID);
4270 if (!Ty)
4271 return error("Missing element type for old-style indirect symbol");
4272 } else {
4273 AddrSpace = Record[OpNum++];
4274 }
4275
4276 auto Val = Record[OpNum++];
4277 auto Linkage = Record[OpNum++];
4278 GlobalValue *NewGA;
4279 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4280 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
4281 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
4282 TheModule);
4283 else
4284 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
4285 nullptr, TheModule);
4286
4287 // Local linkage must have default visibility.
4288 // auto-upgrade `hidden` and `protected` for old bitcode.
4289 if (OpNum != Record.size()) {
4290 auto VisInd = OpNum++;
4291 if (!NewGA->hasLocalLinkage())
4292 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
4293 }
4294 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4295 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
4296 if (OpNum != Record.size()) {
4297 auto S = Record[OpNum++];
4298 // A GlobalValue with local linkage cannot have a DLL storage class.
4299 if (!NewGA->hasLocalLinkage())
4301 }
4302 else
4303 upgradeDLLImportExportLinkage(NewGA, Linkage);
4304 if (OpNum != Record.size())
4306 if (OpNum != Record.size())
4308 }
4309 if (OpNum != Record.size())
4310 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
4311 inferDSOLocal(NewGA);
4312
4313 // Check whether we have enough values to read a partition name.
4314 if (OpNum + 1 < Record.size()) {
4315 // Check Strtab has enough values for the partition.
4316 if (Record[OpNum] + Record[OpNum + 1] > Strtab.size())
4317 return error("Malformed partition, too large.");
4318 NewGA->setPartition(
4319 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
4320 OpNum += 2;
4321 }
4322
4323 ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID));
4324 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
4325 return Error::success();
4326}
4327
4328Error BitcodeReader::parseModule(uint64_t ResumeBit,
4329 bool ShouldLazyLoadMetadata,
4330 ParserCallbacks Callbacks) {
4331 // Load directly into RemoveDIs format if LoadBitcodeIntoNewDbgInfoFormat
4332 // has been set to true and we aren't attempting to preserve the existing
4333 // format in the bitcode (default action: load into the old debug format).
4334 if (PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE) {
4335 TheModule->IsNewDbgInfoFormat =
4337 LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE;
4338 }
4339
4340 this->ValueTypeCallback = std::move(Callbacks.ValueType);
4341 if (ResumeBit) {
4342 if (Error JumpFailed = Stream.JumpToBit(ResumeBit))
4343 return JumpFailed;
4344 } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
4345 return Err;
4346
4348
4349 // Parts of bitcode parsing depend on the datalayout. Make sure we
4350 // finalize the datalayout before we run any of that code.
4351 bool ResolvedDataLayout = false;
4352 // In order to support importing modules with illegal data layout strings,
4353 // delay parsing the data layout string until after upgrades and overrides
4354 // have been applied, allowing to fix illegal data layout strings.
4355 // Initialize to the current module's layout string in case none is specified.
4356 std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr();
4357
4358 auto ResolveDataLayout = [&]() -> Error {
4359 if (ResolvedDataLayout)
4360 return Error::success();
4361
4362 // Datalayout and triple can't be parsed after this point.
4363 ResolvedDataLayout = true;
4364
4365 // Auto-upgrade the layout string
4366 TentativeDataLayoutStr = llvm::UpgradeDataLayoutString(
4367 TentativeDataLayoutStr, TheModule->getTargetTriple());
4368
4369 // Apply override
4370 if (Callbacks.DataLayout) {
4371 if (auto LayoutOverride = (*Callbacks.DataLayout)(
4372 TheModule->getTargetTriple(), TentativeDataLayoutStr))
4373 TentativeDataLayoutStr = *LayoutOverride;
4374 }
4375
4376 // Now the layout string is finalized in TentativeDataLayoutStr. Parse it.
4377 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr);
4378 if (!MaybeDL)
4379 return MaybeDL.takeError();
4380
4381 TheModule->setDataLayout(MaybeDL.get());
4382 return Error::success();
4383 };
4384
4385 // Read all the records for this module.
4386 while (true) {
4387 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4388 if (!MaybeEntry)
4389 return MaybeEntry.takeError();
4390 llvm::BitstreamEntry Entry = MaybeEntry.get();
4391
4392 switch (Entry.Kind) {
4394 return error("Malformed block");
4396 if (Error Err = ResolveDataLayout())
4397 return Err;
4398 return globalCleanup();
4399
4401 switch (Entry.ID) {
4402 default: // Skip unknown content.
4403 if (Error Err = Stream.SkipBlock())
4404 return Err;
4405 break;
4407 if (Error Err = readBlockInfo())
4408 return Err;
4409 break;
4411 if (Error Err = parseAttributeBlock())
4412 return Err;
4413 break;
4415 if (Error Err = parseAttributeGroupBlock())
4416 return Err;
4417 break;
4419 if (Error Err = parseTypeTable())
4420 return Err;
4421 break;
4423 if (!SeenValueSymbolTable) {
4424 // Either this is an old form VST without function index and an
4425 // associated VST forward declaration record (which would have caused
4426 // the VST to be jumped to and parsed before it was encountered
4427 // normally in the stream), or there were no function blocks to
4428 // trigger an earlier parsing of the VST.
4429 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
4430 if (Error Err = parseValueSymbolTable())
4431 return Err;
4432 SeenValueSymbolTable = true;
4433 } else {
4434 // We must have had a VST forward declaration record, which caused
4435 // the parser to jump to and parse the VST earlier.
4436 assert(VSTOffset > 0);
4437 if (Error Err = Stream.SkipBlock())
4438 return Err;
4439 }
4440 break;
4442 if (Error Err = parseConstants())
4443 return Err;
4444 if (Error Err = resolveGlobalAndIndirectSymbolInits())
4445 return Err;
4446 break;
4448 if (ShouldLazyLoadMetadata) {
4449 if (Error Err = rememberAndSkipMetadata())
4450 return Err;
4451 break;
4452 }
4453 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
4454 if (Error Err = MDLoader->parseModuleMetadata())
4455 return Err;
4456 break;
4458 if (Error Err = MDLoader->parseMetadataKinds())
4459 return Err;
4460 break;
4462 if (Error Err = ResolveDataLayout())
4463 return Err;
4464
4465 // If this is the first function body we've seen, reverse the
4466 // FunctionsWithBodies list.
4467 if (!SeenFirstFunctionBody) {
4468 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
4469 if (Error Err = globalCleanup())
4470 return Err;
4471 SeenFirstFunctionBody = true;
4472 }
4473
4474 if (VSTOffset > 0) {
4475 // If we have a VST forward declaration record, make sure we
4476 // parse the VST now if we haven't already. It is needed to
4477 // set up the DeferredFunctionInfo vector for lazy reading.
4478 if (!SeenValueSymbolTable) {
4479 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
4480 return Err;
4481 SeenValueSymbolTable = true;
4482 // Fall through so that we record the NextUnreadBit below.
4483 // This is necessary in case we have an anonymous function that
4484 // is later materialized. Since it will not have a VST entry we
4485 // need to fall back to the lazy parse to find its offset.
4486 } else {
4487 // If we have a VST forward declaration record, but have already
4488 // parsed the VST (just above, when the first function body was
4489 // encountered here), then we are resuming the parse after
4490 // materializing functions. The ResumeBit points to the
4491 // start of the last function block recorded in the
4492 // DeferredFunctionInfo map. Skip it.
4493 if (Error Err = Stream.SkipBlock())
4494 return Err;
4495 continue;
4496 }
4497 }
4498
4499 // Support older bitcode files that did not have the function
4500 // index in the VST, nor a VST forward declaration record, as
4501 // well as anonymous functions that do not have VST entries.
4502 // Build the DeferredFunctionInfo vector on the fly.
4503 if (Error Err = rememberAndSkipFunctionBody())
4504 return Err;
4505
4506 // Suspend parsing when we reach the function bodies. Subsequent
4507 // materialization calls will resume it when necessary. If the bitcode
4508 // file is old, the symbol table will be at the end instead and will not
4509 // have been seen yet. In this case, just finish the parse now.
4510 if (SeenValueSymbolTable) {
4511 NextUnreadBit = Stream.GetCurrentBitNo();
4512 // After the VST has been parsed, we need to make sure intrinsic name
4513 // are auto-upgraded.
4514 return globalCleanup();
4515 }
4516 break;
4518 if (Error Err = parseUseLists())
4519 return Err;
4520 break;
4522 if (Error Err = parseOperandBundleTags())
4523 return Err;
4524 break;
4526 if (Error Err = parseSyncScopeNames())
4527 return Err;
4528 break;
4529 }
4530 continue;
4531
4533 // The interesting case.
4534 break;
4535 }
4536
4537 // Read a record.
4538 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
4539 if (!MaybeBitCode)
4540 return MaybeBitCode.takeError();
4541 switch (unsigned BitCode = MaybeBitCode.get()) {
4542 default: break; // Default behavior, ignore unknown content.
4544 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
4545 if (!VersionOrErr)
4546 return VersionOrErr.takeError();
4547 UseRelativeIDs = *VersionOrErr >= 1;
4548 break;
4549 }
4550 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
4551 if (ResolvedDataLayout)
4552 return error("target triple too late in module");
4553 std::string S;
4554 if (convertToString(Record, 0, S))
4555 return error("Invalid record");
4556 TheModule->setTargetTriple(S);
4557 break;
4558 }
4559 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
4560 if (ResolvedDataLayout)
4561 return error("datalayout too late in module");
4562 if (convertToString(Record, 0, TentativeDataLayoutStr))
4563 return error("Invalid record");
4564 break;
4565 }
4566 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
4567 std::string S;
4568 if (convertToString(Record, 0, S))
4569 return error("Invalid record");
4570 TheModule->setModuleInlineAsm(S);
4571 break;
4572 }
4573 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
4574 // Deprecated, but still needed to read old bitcode files.
4575 std::string S;
4576 if (convertToString(Record, 0, S))
4577 return error("Invalid record");
4578 // Ignore value.
4579 break;
4580 }
4581 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
4582 std::string S;
4583 if (convertToString(Record, 0, S))
4584 return error("Invalid record");
4585 SectionTable.push_back(S);
4586 break;
4587 }
4588 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
4589 std::string S;
4590 if (convertToString(Record, 0, S))
4591 return error("Invalid record");
4592 GCTable.push_back(S);
4593 break;
4594 }
4596 if (Error Err = parseComdatRecord(Record))
4597 return Err;
4598 break;
4599 // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC}
4600 // written by ThinLinkBitcodeWriter. See
4601 // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each
4602 // record
4603 // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714)
4605 if (Error Err = parseGlobalVarRecord(Record))
4606 return Err;
4607 break;
4609 if (Error Err = ResolveDataLayout())
4610 return Err;
4611 if (Error Err = parseFunctionRecord(Record))
4612 return Err;
4613 break;
4617 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
4618 return Err;
4619 break;
4620 /// MODULE_CODE_VSTOFFSET: [offset]
4622 if (Record.empty())
4623 return error("Invalid record");
4624 // Note that we subtract 1 here because the offset is relative to one word
4625 // before the start of the identification or module block, which was
4626 // historically always the start of the regular bitcode header.
4627 VSTOffset = Record[0] - 1;
4628 break;
4629 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4633 return error("Invalid record");
4634 TheModule->setSourceFileName(ValueName);
4635 break;
4636 }
4637 Record.clear();
4638 }
4639 this->ValueTypeCallback = std::nullopt;
4640 return Error::success();
4641}
4642
4643Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
4644 bool IsImporting,
4645 ParserCallbacks Callbacks) {
4646 TheModule = M;
4647 MetadataLoaderCallbacks MDCallbacks;
4648 MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); };
4649 MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) {
4650 return getContainedTypeID(I, J);
4651 };
4652 MDCallbacks.MDType = Callbacks.MDType;
4653 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks);
4654 return parseModule(0, ShouldLazyLoadMetadata, Callbacks);
4655}
4656
4657Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
4658 if (!isa<PointerType>(PtrType))
4659 return error("Load/Store operand is not a pointer type");
4660 if (!PointerType::isLoadableOrStorableType(ValType))
4661 return error("Cannot load/store from pointer");
4662 return Error::success();
4663}
4664
4665Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
4666 ArrayRef<unsigned> ArgTyIDs) {
4668 for (unsigned i = 0; i != CB->arg_size(); ++i) {
4669 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4670 Attribute::InAlloca}) {
4671 if (!Attrs.hasParamAttr(i, Kind) ||
4672 Attrs.getParamAttr(i, Kind).getValueAsType())
4673 continue;
4674
4675 Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);
4676 if (!PtrEltTy)
4677 return error("Missing element type for typed attribute upgrade");
4678
4679 Attribute NewAttr;
4680 switch (Kind) {
4681 case Attribute::ByVal:
4682 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
4683 break;
4684 case Attribute::StructRet:
4685 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
4686 break;
4687 case Attribute::InAlloca:
4688 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
4689 break;
4690 default:
4691 llvm_unreachable("not an upgraded type attribute");
4692 }
4693
4694 Attrs = Attrs.addParamAttribute(Context, i, NewAttr);
4695 }
4696 }
4697
4698 if (CB->isInlineAsm()) {
4699 const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
4700 unsigned ArgNo = 0;
4701 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
4702 if (!CI.hasArg())
4703 continue;
4704
4705 if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) {
4706 Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4707 if (!ElemTy)
4708 return error("Missing element type for inline asm upgrade");
4709 Attrs = Attrs.addParamAttribute(
4710 Context, ArgNo,
4711 Attribute::get(Context, Attribute::ElementType, ElemTy));
4712 }
4713
4714 ArgNo++;
4715 }
4716 }
4717
4718 switch (CB->getIntrinsicID()) {
4719 case Intrinsic::preserve_array_access_index:
4720 case Intrinsic::preserve_struct_access_index:
4721 case Intrinsic::aarch64_ldaxr:
4722 case Intrinsic::aarch64_ldxr:
4723 case Intrinsic::aarch64_stlxr:
4724 case Intrinsic::aarch64_stxr:
4725 case Intrinsic::arm_ldaex:
4726 case Intrinsic::arm_ldrex:
4727 case Intrinsic::arm_stlex:
4728 case Intrinsic::arm_strex: {
4729 unsigned ArgNo;
4730 switch (CB->getIntrinsicID()) {
4731 case Intrinsic::aarch64_stlxr:
4732 case Intrinsic::aarch64_stxr:
4733 case Intrinsic::arm_stlex:
4734 case Intrinsic::arm_strex:
4735 ArgNo = 1;
4736 break;
4737 default:
4738 ArgNo = 0;
4739 break;
4740 }
4741 if (!Attrs.getParamElementType(ArgNo)) {
4742 Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4743 if (!ElTy)
4744 return error("Missing element type for elementtype upgrade");
4745 Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);
4746 Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr);
4747 }
4748 break;
4749 }
4750 default:
4751 break;
4752 }
4753
4754 CB->setAttributes(Attrs);
4755 return Error::success();
4756}
4757
4758/// Lazily parse the specified function body block.
4759Error BitcodeReader::parseFunctionBody(Function *F) {
4760 if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
4761 return Err;
4762
4763 // Unexpected unresolved metadata when parsing function.
4764 if (MDLoader->hasFwdRefs())
4765 return error("Invalid function metadata: incoming forward references");
4766
4767 InstructionList.clear();
4768 unsigned ModuleValueListSize = ValueList.size();
4769 unsigned ModuleMDLoaderSize = MDLoader->size();
4770
4771 // Add all the function arguments to the value table.
4772 unsigned ArgNo = 0;
4773 unsigned FTyID = FunctionTypeIDs[F];
4774 for (Argument &I : F->args()) {
4775 unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1);
4776 assert(I.getType() == getTypeByID(ArgTyID) &&
4777 "Incorrect fully specified type for Function Argument");
4778 ValueList.push_back(&I, ArgTyID);
4779 ++ArgNo;
4780 }
4781 unsigned NextValueNo = ValueList.size();
4782 BasicBlock *CurBB = nullptr;
4783 unsigned CurBBNo = 0;
4784 // Block into which constant expressions from phi nodes are materialized.
4785 BasicBlock *PhiConstExprBB = nullptr;
4786 // Edge blocks for phi nodes into which constant expressions have been
4787 // expanded.
4789 ConstExprEdgeBBs;
4790
4791 DebugLoc LastLoc;
4792 auto getLastInstruction = [&]() -> Instruction * {
4793 if (CurBB && !CurBB->empty())
4794 return &CurBB->back();
4795 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4796 !FunctionBBs[CurBBNo - 1]->empty())
4797 return &FunctionBBs[CurBBNo - 1]->back();
4798 return nullptr;
4799 };
4800
4801 std::vector<OperandBundleDef> OperandBundles;
4802
4803 // Read all the records.
4805
4806 while (true) {
4807 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4808 if (!MaybeEntry)
4809 return MaybeEntry.takeError();
4810 llvm::BitstreamEntry Entry = MaybeEntry.get();
4811
4812 switch (Entry.Kind) {
4814 return error("Malformed block");
4816 goto OutOfRecordLoop;
4817
4819 switch (Entry.ID) {
4820 default: // Skip unknown content.
4821 if (Error Err = Stream.SkipBlock())
4822 return Err;
4823 break;
4825 if (Error Err = parseConstants())
4826 return Err;
4827 NextValueNo = ValueList.size();
4828 break;
4830 if (Error Err = parseValueSymbolTable())
4831 return Err;
4832 break;
4834 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
4835 return Err;
4836 break;
4838 assert(DeferredMetadataInfo.empty() &&
4839 "Must read all module-level metadata before function-level");
4840 if (Error Err = MDLoader->parseFunctionMetadata())
4841 return Err;
4842 break;
4844 if (Error Err = parseUseLists())
4845 return Err;
4846 break;
4847 }
4848 continue;
4849
4851 // The interesting case.
4852 break;
4853 }
4854
4855 // Read a record.
4856 Record.clear();
4857 Instruction *I = nullptr;
4858 unsigned ResTypeID = InvalidTypeID;
4859 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
4860 if (!MaybeBitCode)
4861 return MaybeBitCode.takeError();
4862 switch (unsigned BitCode = MaybeBitCode.get()) {
4863 default: // Default behavior: reject
4864 return error("Invalid value");
4865 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
4866 if (Record.empty() || Record[0] == 0)
4867 return error("Invalid record");
4868 // Create all the basic blocks for the function.
4869 FunctionBBs.resize(Record[0]);
4870
4871 // See if anything took the address of blocks in this function.
4872 auto BBFRI = BasicBlockFwdRefs.find(F);
4873 if (BBFRI == BasicBlockFwdRefs.end()) {
4874 for (BasicBlock *&BB : FunctionBBs)
4875 BB = BasicBlock::Create(Context, "", F);
4876 } else {
4877 auto &BBRefs = BBFRI->second;
4878 // Check for invalid basic block references.
4879 if (BBRefs.size() > FunctionBBs.size())
4880 return error("Invalid ID");
4881 assert(!BBRefs.empty() && "Unexpected empty array");
4882 assert(!BBRefs.front() && "Invalid reference to entry block");
4883 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4884 ++I)
4885 if (I < RE && BBRefs[I]) {
4886 BBRefs[I]->insertInto(F);
4887 FunctionBBs[I] = BBRefs[I];
4888 } else {
4889 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4890 }
4891
4892 // Erase from the table.
4893 BasicBlockFwdRefs.erase(BBFRI);
4894 }
4895
4896 CurBB = FunctionBBs[0];
4897 continue;
4898 }
4899
4900 case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...]
4901 // The record should not be emitted if it's an empty list.
4902 if (Record.empty())
4903 return error("Invalid record");
4904 // When we have the RARE case of a BlockAddress Constant that is not
4905 // scoped to the Function it refers to, we need to conservatively
4906 // materialize the referred to Function, regardless of whether or not
4907 // that Function will ultimately be linked, otherwise users of
4908