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