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/Twine.h"
24#include "llvm/Config/llvm-config.h"
25#include "llvm/IR/Argument.h"
26#include "llvm/IR/Attributes.h"
27#include "llvm/IR/AutoUpgrade.h"
28#include "llvm/IR/BasicBlock.h"
29#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/Comdat.h"
31#include "llvm/IR/Constant.h"
32#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DebugInfo.h"
36#include "llvm/IR/DebugLoc.h"
38#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalAlias.h"
42#include "llvm/IR/GlobalIFunc.h"
44#include "llvm/IR/GlobalValue.h"
46#include "llvm/IR/InlineAsm.h"
48#include "llvm/IR/InstrTypes.h"
49#include "llvm/IR/Instruction.h"
51#include "llvm/IR/Intrinsics.h"
52#include "llvm/IR/IntrinsicsAArch64.h"
53#include "llvm/IR/IntrinsicsARM.h"
54#include "llvm/IR/LLVMContext.h"
55#include "llvm/IR/Metadata.h"
56#include "llvm/IR/Module.h"
58#include "llvm/IR/Operator.h"
59#include "llvm/IR/Type.h"
60#include "llvm/IR/Value.h"
61#include "llvm/IR/Verifier.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/Error.h"
72#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(const BitcodeConstant *BC) {
1403 uint8_t Opcode = BC->Opcode;
1404
1405 // These are not real constant expressions, always consider them supported.
1406 if (Opcode >= BitcodeConstant::FirstSpecialOpcode)
1407 return true;
1408
1409 // If -expand-constant-exprs is set, we want to consider all expressions
1410 // as unsupported.
1412 return false;
1413
1414 if (Instruction::isBinaryOp(Opcode))
1415 return ConstantExpr::isSupportedBinOp(Opcode);
1416
1417 if (Opcode == Instruction::GetElementPtr)
1418 return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy);
1419
1420 switch (Opcode) {
1421 case Instruction::FNeg:
1422 case Instruction::Select:
1423 return false;
1424 default:
1425 return true;
1426 }
1427}
1428
1429Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
1430 BasicBlock *InsertBB) {
1431 // Quickly handle the case where there is no BitcodeConstant to resolve.
1432 if (StartValID < ValueList.size() && ValueList[StartValID] &&
1433 !isa<BitcodeConstant>(ValueList[StartValID]))
1434 return ValueList[StartValID];
1435
1436 SmallDenseMap<unsigned, Value *> MaterializedValues;
1437 SmallVector<unsigned> Worklist;
1438 Worklist.push_back(StartValID);
1439 while (!Worklist.empty()) {
1440 unsigned ValID = Worklist.back();
1441 if (MaterializedValues.count(ValID)) {
1442 // Duplicate expression that was already handled.
1443 Worklist.pop_back();
1444 continue;
1445 }
1446
1447 if (ValID >= ValueList.size() || !ValueList[ValID])
1448 return error("Invalid value ID");
1449
1450 Value *V = ValueList[ValID];
1451 auto *BC = dyn_cast<BitcodeConstant>(V);
1452 if (!BC) {
1453 MaterializedValues.insert({ValID, V});
1454 Worklist.pop_back();
1455 continue;
1456 }
1457
1458 // Iterate in reverse, so values will get popped from the worklist in
1459 // expected order.
1461 for (unsigned OpID : reverse(BC->getOperandIDs())) {
1462 auto It = MaterializedValues.find(OpID);
1463 if (It != MaterializedValues.end())
1464 Ops.push_back(It->second);
1465 else
1466 Worklist.push_back(OpID);
1467 }
1468
1469 // Some expressions have not been resolved yet, handle them first and then
1470 // revisit this one.
1471 if (Ops.size() != BC->getOperandIDs().size())
1472 continue;
1473 std::reverse(Ops.begin(), Ops.end());
1474
1475 SmallVector<Constant *> ConstOps;
1476 for (Value *Op : Ops)
1477 if (auto *C = dyn_cast<Constant>(Op))
1478 ConstOps.push_back(C);
1479
1480 // Materialize as constant expression if possible.
1481 if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) {
1482 Constant *C;
1483 if (Instruction::isCast(BC->Opcode)) {
1484 C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType());
1485 if (!C)
1486 C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType());
1487 } else if (Instruction::isBinaryOp(BC->Opcode)) {
1488 C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags);
1489 } else {
1490 switch (BC->Opcode) {
1491 case BitcodeConstant::NoCFIOpcode: {
1492 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);
1493 if (!GV)
1494 return error("no_cfi operand must be GlobalValue");
1495 C = NoCFIValue::get(GV);
1496 break;
1497 }
1498 case BitcodeConstant::DSOLocalEquivalentOpcode: {
1499 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]);
1500 if (!GV)
1501 return error("dso_local operand must be GlobalValue");
1503 break;
1504 }
1505 case BitcodeConstant::BlockAddressOpcode: {
1506 Function *Fn = dyn_cast<Function>(ConstOps[0]);
1507 if (!Fn)
1508 return error("blockaddress operand must be a function");
1509
1510 // If the function is already parsed we can insert the block address
1511 // right away.
1512 BasicBlock *BB;
1513 unsigned BBID = BC->Extra;
1514 if (!BBID)
1515 // Invalid reference to entry block.
1516 return error("Invalid ID");
1517 if (!Fn->empty()) {
1518 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1519 for (size_t I = 0, E = BBID; I != E; ++I) {
1520 if (BBI == BBE)
1521 return error("Invalid ID");
1522 ++BBI;
1523 }
1524 BB = &*BBI;
1525 } else {
1526 // Otherwise insert a placeholder and remember it so it can be
1527 // inserted when the function is parsed.
1528 auto &FwdBBs = BasicBlockFwdRefs[Fn];
1529 if (FwdBBs.empty())
1530 BasicBlockFwdRefQueue.push_back(Fn);
1531 if (FwdBBs.size() < BBID + 1)
1532 FwdBBs.resize(BBID + 1);
1533 if (!FwdBBs[BBID])
1534 FwdBBs[BBID] = BasicBlock::Create(Context);
1535 BB = FwdBBs[BBID];
1536 }
1537 C = BlockAddress::get(Fn, BB);
1538 break;
1539 }
1540 case BitcodeConstant::ConstantStructOpcode:
1541 C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps);
1542 break;
1543 case BitcodeConstant::ConstantArrayOpcode:
1544 C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps);
1545 break;
1546 case BitcodeConstant::ConstantVectorOpcode:
1547 C = ConstantVector::get(ConstOps);
1548 break;
1549 case Instruction::ICmp:
1550 case Instruction::FCmp:
1551 C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]);
1552 break;
1553 case Instruction::GetElementPtr:
1554 C = ConstantExpr::getGetElementPtr(BC->SrcElemTy, ConstOps[0],
1555 ArrayRef(ConstOps).drop_front(),
1556 BC->Flags, BC->getInRangeIndex());
1557 break;
1558 case Instruction::ExtractElement:
1559 C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]);
1560 break;
1561 case Instruction::InsertElement:
1562 C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1],
1563 ConstOps[2]);
1564 break;
1565 case Instruction::ShuffleVector: {
1567 ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask);
1568 C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask);
1569 break;
1570 }
1571 default:
1572 llvm_unreachable("Unhandled bitcode constant");
1573 }
1574 }
1575
1576 // Cache resolved constant.
1577 ValueList.replaceValueWithoutRAUW(ValID, C);
1578 MaterializedValues.insert({ValID, C});
1579 Worklist.pop_back();
1580 continue;
1581 }
1582
1583 if (!InsertBB)
1584 return error(Twine("Value referenced by initializer is an unsupported "
1585 "constant expression of type ") +
1586 BC->getOpcodeName());
1587
1588 // Materialize as instructions if necessary.
1589 Instruction *I;
1590 if (Instruction::isCast(BC->Opcode)) {
1591 I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0],
1592 BC->getType(), "constexpr", InsertBB);
1593 } else if (Instruction::isUnaryOp(BC->Opcode)) {
1594 I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0],
1595 "constexpr", InsertBB);
1596 } else if (Instruction::isBinaryOp(BC->Opcode)) {
1597 I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0],
1598 Ops[1], "constexpr", InsertBB);
1599 if (isa<OverflowingBinaryOperator>(I)) {
1601 I->setHasNoSignedWrap();
1603 I->setHasNoUnsignedWrap();
1604 }
1605 if (isa<PossiblyExactOperator>(I) &&
1606 (BC->Flags & PossiblyExactOperator::IsExact))
1607 I->setIsExact();
1608 } else {
1609 switch (BC->Opcode) {
1610 case BitcodeConstant::ConstantVectorOpcode: {
1611 Type *IdxTy = Type::getInt32Ty(BC->getContext());
1612 Value *V = PoisonValue::get(BC->getType());
1613 for (auto Pair : enumerate(Ops)) {
1614 Value *Idx = ConstantInt::get(IdxTy, Pair.index());
1615 V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins",
1616 InsertBB);
1617 }
1618 I = cast<Instruction>(V);
1619 break;
1620 }
1621 case BitcodeConstant::ConstantStructOpcode:
1622 case BitcodeConstant::ConstantArrayOpcode: {
1623 Value *V = PoisonValue::get(BC->getType());
1624 for (auto Pair : enumerate(Ops))
1625 V = InsertValueInst::Create(V, Pair.value(), Pair.index(),
1626 "constexpr.ins", InsertBB);
1627 I = cast<Instruction>(V);
1628 break;
1629 }
1630 case Instruction::ICmp:
1631 case Instruction::FCmp:
1633 (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1],
1634 "constexpr", InsertBB);
1635 break;
1636 case Instruction::GetElementPtr:
1637 I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0],
1638 ArrayRef(Ops).drop_front(), "constexpr",
1639 InsertBB);
1640 if (BC->Flags)
1641 cast<GetElementPtrInst>(I)->setIsInBounds();
1642 break;
1643 case Instruction::Select:
1644 I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB);
1645 break;
1646 case Instruction::ExtractElement:
1647 I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB);
1648 break;
1649 case Instruction::InsertElement:
1650 I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr",
1651 InsertBB);
1652 break;
1653 case Instruction::ShuffleVector:
1654 I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr",
1655 InsertBB);
1656 break;
1657 default:
1658 llvm_unreachable("Unhandled bitcode constant");
1659 }
1660 }
1661
1662 MaterializedValues.insert({ValID, I});
1663 Worklist.pop_back();
1664 }
1665
1666 return MaterializedValues[StartValID];
1667}
1668
1669Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) {
1670 Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr);
1671 if (!MaybeV)
1672 return MaybeV.takeError();
1673
1674 // Result must be Constant if InsertBB is nullptr.
1675 return cast<Constant>(MaybeV.get());
1676}
1677
1678StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1679 StringRef Name) {
1680 auto *Ret = StructType::create(Context, Name);
1681 IdentifiedStructTypes.push_back(Ret);
1682 return Ret;
1683}
1684
1685StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1686 auto *Ret = StructType::create(Context);
1687 IdentifiedStructTypes.push_back(Ret);
1688 return Ret;
1689}
1690
1691//===----------------------------------------------------------------------===//
1692// Functions for parsing blocks from the bitcode file
1693//===----------------------------------------------------------------------===//
1694
1696 switch (Val) {
1700 llvm_unreachable("Synthetic enumerators which should never get here");
1701
1702 case Attribute::None: return 0;
1703 case Attribute::ZExt: return 1 << 0;
1704 case Attribute::SExt: return 1 << 1;
1705 case Attribute::NoReturn: return 1 << 2;
1706 case Attribute::InReg: return 1 << 3;
1707 case Attribute::StructRet: return 1 << 4;
1708 case Attribute::NoUnwind: return 1 << 5;
1709 case Attribute::NoAlias: return 1 << 6;
1710 case Attribute::ByVal: return 1 << 7;
1711 case Attribute::Nest: return 1 << 8;
1712 case Attribute::ReadNone: return 1 << 9;
1713 case Attribute::ReadOnly: return 1 << 10;
1714 case Attribute::NoInline: return 1 << 11;
1715 case Attribute::AlwaysInline: return 1 << 12;
1716 case Attribute::OptimizeForSize: return 1 << 13;
1717 case Attribute::StackProtect: return 1 << 14;
1718 case Attribute::StackProtectReq: return 1 << 15;
1719 case Attribute::Alignment: return 31 << 16;
1720 case Attribute::NoCapture: return 1 << 21;
1721 case Attribute::NoRedZone: return 1 << 22;
1722 case Attribute::NoImplicitFloat: return 1 << 23;
1723 case Attribute::Naked: return 1 << 24;
1724 case Attribute::InlineHint: return 1 << 25;
1725 case Attribute::StackAlignment: return 7 << 26;
1726 case Attribute::ReturnsTwice: return 1 << 29;
1727 case Attribute::UWTable: return 1 << 30;
1728 case Attribute::NonLazyBind: return 1U << 31;
1729 case Attribute::SanitizeAddress: return 1ULL << 32;
1730 case Attribute::MinSize: return 1ULL << 33;
1731 case Attribute::NoDuplicate: return 1ULL << 34;
1732 case Attribute::StackProtectStrong: return 1ULL << 35;
1733 case Attribute::SanitizeThread: return 1ULL << 36;
1734 case Attribute::SanitizeMemory: return 1ULL << 37;
1735 case Attribute::NoBuiltin: return 1ULL << 38;
1736 case Attribute::Returned: return 1ULL << 39;
1737 case Attribute::Cold: return 1ULL << 40;
1738 case Attribute::Builtin: return 1ULL << 41;
1739 case Attribute::OptimizeNone: return 1ULL << 42;
1740 case Attribute::InAlloca: return 1ULL << 43;
1741 case Attribute::NonNull: return 1ULL << 44;
1742 case Attribute::JumpTable: return 1ULL << 45;
1743 case Attribute::Convergent: return 1ULL << 46;
1744 case Attribute::SafeStack: return 1ULL << 47;
1745 case Attribute::NoRecurse: return 1ULL << 48;
1746 // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately.
1747 // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately.
1748 case Attribute::SwiftSelf: return 1ULL << 51;
1749 case Attribute::SwiftError: return 1ULL << 52;
1750 case Attribute::WriteOnly: return 1ULL << 53;
1751 case Attribute::Speculatable: return 1ULL << 54;
1752 case Attribute::StrictFP: return 1ULL << 55;
1753 case Attribute::SanitizeHWAddress: return 1ULL << 56;
1754 case Attribute::NoCfCheck: return 1ULL << 57;
1755 case Attribute::OptForFuzzing: return 1ULL << 58;
1756 case Attribute::ShadowCallStack: return 1ULL << 59;
1757 case Attribute::SpeculativeLoadHardening:
1758 return 1ULL << 60;
1759 case Attribute::ImmArg:
1760 return 1ULL << 61;
1761 case Attribute::WillReturn:
1762 return 1ULL << 62;
1763 case Attribute::NoFree:
1764 return 1ULL << 63;
1765 default:
1766 // Other attributes are not supported in the raw format,
1767 // as we ran out of space.
1768 return 0;
1769 }
1770 llvm_unreachable("Unsupported attribute type");
1771}
1772
1774 if (!Val) return;
1775
1777 I = Attribute::AttrKind(I + 1)) {
1778 if (uint64_t A = (Val & getRawAttributeMask(I))) {
1779 if (I == Attribute::Alignment)
1780 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1781 else if (I == Attribute::StackAlignment)
1782 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1783 else if (Attribute::isTypeAttrKind(I))
1784 B.addTypeAttr(I, nullptr); // Type will be auto-upgraded.
1785 else
1786 B.addAttribute(I);
1787 }
1788 }
1789}
1790
1791/// This fills an AttrBuilder object with the LLVM attributes that have
1792/// been decoded from the given integer. This function must stay in sync with
1793/// 'encodeLLVMAttributesForBitcode'.
1795 uint64_t EncodedAttrs,
1796 uint64_t AttrIdx) {
1797 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1798 // the bits above 31 down by 11 bits.
1799 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1800 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1801 "Alignment must be a power of two.");
1802
1803 if (Alignment)
1804 B.addAlignmentAttr(Alignment);
1805
1806 uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1807 (EncodedAttrs & 0xffff);
1808
1809 if (AttrIdx == AttributeList::FunctionIndex) {
1810 // Upgrade old memory attributes.
1812 if (Attrs & (1ULL << 9)) {
1813 // ReadNone
1814 Attrs &= ~(1ULL << 9);
1815 ME &= MemoryEffects::none();
1816 }
1817 if (Attrs & (1ULL << 10)) {
1818 // ReadOnly
1819 Attrs &= ~(1ULL << 10);
1821 }
1822 if (Attrs & (1ULL << 49)) {
1823 // InaccessibleMemOnly
1824 Attrs &= ~(1ULL << 49);
1826 }
1827 if (Attrs & (1ULL << 50)) {
1828 // InaccessibleMemOrArgMemOnly
1829 Attrs &= ~(1ULL << 50);
1831 }
1832 if (Attrs & (1ULL << 53)) {
1833 // WriteOnly
1834 Attrs &= ~(1ULL << 53);
1836 }
1837 if (ME != MemoryEffects::unknown())
1838 B.addMemoryAttr(ME);
1839 }
1840
1841 addRawAttributeValue(B, Attrs);
1842}
1843
1844Error BitcodeReader::parseAttributeBlock() {
1845 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1846 return Err;
1847
1848 if (!MAttributes.empty())
1849 return error("Invalid multiple blocks");
1850
1852
1854
1855 // Read all the records.
1856 while (true) {
1857 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1858 if (!MaybeEntry)
1859 return MaybeEntry.takeError();
1860 BitstreamEntry Entry = MaybeEntry.get();
1861
1862 switch (Entry.Kind) {
1863 case BitstreamEntry::SubBlock: // Handled for us already.
1865 return error("Malformed block");
1867 return Error::success();
1869 // The interesting case.
1870 break;
1871 }
1872
1873 // Read a record.
1874 Record.clear();
1875 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1876 if (!MaybeRecord)
1877 return MaybeRecord.takeError();
1878 switch (MaybeRecord.get()) {
1879 default: // Default behavior: ignore.
1880 break;
1881 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1882 // Deprecated, but still needed to read old bitcode files.
1883 if (Record.size() & 1)
1884 return error("Invalid parameter attribute record");
1885
1886 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1887 AttrBuilder B(Context);
1889 Attrs.push_back(AttributeList::get(Context, Record[i], B));
1890 }
1891
1892 MAttributes.push_back(AttributeList::get(Context, Attrs));
1893 Attrs.clear();
1894 break;
1895 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1896 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1897 Attrs.push_back(MAttributeGroups[Record[i]]);
1898
1899 MAttributes.push_back(AttributeList::get(Context, Attrs));
1900 Attrs.clear();
1901 break;
1902 }
1903 }
1904}
1905
1906// Returns Attribute::None on unrecognized codes.
1908 switch (Code) {
1909 default:
1910 return Attribute::None;
1912 return Attribute::Alignment;
1914 return Attribute::AlwaysInline;
1916 return Attribute::Builtin;
1918 return Attribute::ByVal;
1920 return Attribute::InAlloca;
1922 return Attribute::Cold;
1924 return Attribute::Convergent;
1926 return Attribute::DisableSanitizerInstrumentation;
1928 return Attribute::ElementType;
1930 return Attribute::FnRetThunkExtern;
1932 return Attribute::InlineHint;
1934 return Attribute::InReg;
1936 return Attribute::JumpTable;
1938 return Attribute::Memory;
1940 return Attribute::NoFPClass;
1942 return Attribute::MinSize;
1944 return Attribute::Naked;
1946 return Attribute::Nest;
1948 return Attribute::NoAlias;
1950 return Attribute::NoBuiltin;
1952 return Attribute::NoCallback;
1954 return Attribute::NoCapture;
1956 return Attribute::NoDuplicate;
1958 return Attribute::NoFree;
1960 return Attribute::NoImplicitFloat;
1962 return Attribute::NoInline;
1964 return Attribute::NoRecurse;
1966 return Attribute::NoMerge;
1968 return Attribute::NonLazyBind;
1970 return Attribute::NonNull;
1972 return Attribute::Dereferenceable;
1974 return Attribute::DereferenceableOrNull;
1976 return Attribute::AllocAlign;
1978 return Attribute::AllocKind;
1980 return Attribute::AllocSize;
1982 return Attribute::AllocatedPointer;
1984 return Attribute::NoRedZone;
1986 return Attribute::NoReturn;
1988 return Attribute::NoSync;
1990 return Attribute::NoCfCheck;
1992 return Attribute::NoProfile;
1994 return Attribute::SkipProfile;
1996 return Attribute::NoUnwind;
1998 return Attribute::NoSanitizeBounds;
2000 return Attribute::NoSanitizeCoverage;
2002 return Attribute::NullPointerIsValid;
2004 return Attribute::OptForFuzzing;
2006 return Attribute::OptimizeForSize;
2008 return Attribute::OptimizeNone;
2010 return Attribute::ReadNone;
2012 return Attribute::ReadOnly;
2014 return Attribute::Returned;
2016 return Attribute::ReturnsTwice;
2018 return Attribute::SExt;
2020 return Attribute::Speculatable;
2022 return Attribute::StackAlignment;
2024 return Attribute::StackProtect;
2026 return Attribute::StackProtectReq;
2028 return Attribute::StackProtectStrong;
2030 return Attribute::SafeStack;
2032 return Attribute::ShadowCallStack;
2034 return Attribute::StrictFP;
2036 return Attribute::StructRet;
2038 return Attribute::SanitizeAddress;
2040 return Attribute::SanitizeHWAddress;
2042 return Attribute::SanitizeThread;
2044 return Attribute::SanitizeMemory;
2046 return Attribute::SpeculativeLoadHardening;
2048 return Attribute::SwiftError;
2050 return Attribute::SwiftSelf;
2052 return Attribute::SwiftAsync;
2054 return Attribute::UWTable;
2056 return Attribute::VScaleRange;
2058 return Attribute::WillReturn;
2060 return Attribute::WriteOnly;
2062 return Attribute::ZExt;
2064 return Attribute::ImmArg;
2066 return Attribute::SanitizeMemTag;
2068 return Attribute::Preallocated;
2070 return Attribute::NoUndef;
2072 return Attribute::ByRef;
2074 return Attribute::MustProgress;
2076 return Attribute::Hot;
2078 return Attribute::PresplitCoroutine;
2079 }
2080}
2081
2082Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
2083 MaybeAlign &Alignment) {
2084 // Note: Alignment in bitcode files is incremented by 1, so that zero
2085 // can be used for default alignment.
2087 return error("Invalid alignment value");
2088 Alignment = decodeMaybeAlign(Exponent);
2089 return Error::success();
2090}
2091
2092Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
2093 *Kind = getAttrFromCode(Code);
2094 if (*Kind == Attribute::None)
2095 return error("Unknown attribute kind (" + Twine(Code) + ")");
2096 return Error::success();
2097}
2098
2099static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) {
2100 switch (EncodedKind) {
2102 ME &= MemoryEffects::none();
2103 return true;
2106 return true;
2109 return true;
2112 return true;
2115 return true;
2118 return true;
2119 default:
2120 return false;
2121 }
2122}
2123
2124Error BitcodeReader::parseAttributeGroupBlock() {
2125 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
2126 return Err;
2127
2128 if (!MAttributeGroups.empty())
2129 return error("Invalid multiple blocks");
2130
2132
2133 // Read all the records.
2134 while (true) {
2135 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2136 if (!MaybeEntry)
2137 return MaybeEntry.takeError();
2138 BitstreamEntry Entry = MaybeEntry.get();
2139
2140 switch (Entry.Kind) {
2141 case BitstreamEntry::SubBlock: // Handled for us already.
2143 return error("Malformed block");
2145 return Error::success();
2147 // The interesting case.
2148 break;
2149 }
2150
2151 // Read a record.
2152 Record.clear();
2153 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2154 if (!MaybeRecord)
2155 return MaybeRecord.takeError();
2156 switch (MaybeRecord.get()) {
2157 default: // Default behavior: ignore.
2158 break;
2159 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
2160 if (Record.size() < 3)
2161 return error("Invalid grp record");
2162
2163 uint64_t GrpID = Record[0];
2164 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
2165
2166 AttrBuilder B(Context);
2168 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2169 if (Record[i] == 0) { // Enum attribute
2171 uint64_t EncodedKind = Record[++i];
2173 upgradeOldMemoryAttribute(ME, EncodedKind))
2174 continue;
2175
2176 if (Error Err = parseAttrKind(EncodedKind, &Kind))
2177 return Err;
2178
2179 // Upgrade old-style byval attribute to one with a type, even if it's
2180 // nullptr. We will have to insert the real type when we associate
2181 // this AttributeList with a function.
2182 if (Kind == Attribute::ByVal)
2183 B.addByValAttr(nullptr);
2184 else if (Kind == Attribute::StructRet)
2185 B.addStructRetAttr(nullptr);
2186 else if (Kind == Attribute::InAlloca)
2187 B.addInAllocaAttr(nullptr);
2188 else if (Kind == Attribute::UWTable)
2189 B.addUWTableAttr(UWTableKind::Default);
2190 else if (Attribute::isEnumAttrKind(Kind))
2191 B.addAttribute(Kind);
2192 else
2193 return error("Not an enum attribute");
2194 } else if (Record[i] == 1) { // Integer attribute
2196 if (Error Err = parseAttrKind(Record[++i], &Kind))
2197 return Err;
2198 if (!Attribute::isIntAttrKind(Kind))
2199 return error("Not an int attribute");
2200 if (Kind == Attribute::Alignment)
2201 B.addAlignmentAttr(Record[++i]);
2202 else if (Kind == Attribute::StackAlignment)
2203 B.addStackAlignmentAttr(Record[++i]);
2204 else if (Kind == Attribute::Dereferenceable)
2205 B.addDereferenceableAttr(Record[++i]);
2206 else if (Kind == Attribute::DereferenceableOrNull)
2207 B.addDereferenceableOrNullAttr(Record[++i]);
2208 else if (Kind == Attribute::AllocSize)
2209 B.addAllocSizeAttrFromRawRepr(Record[++i]);
2210 else if (Kind == Attribute::VScaleRange)
2211 B.addVScaleRangeAttrFromRawRepr(Record[++i]);
2212 else if (Kind == Attribute::UWTable)
2213 B.addUWTableAttr(UWTableKind(Record[++i]));
2214 else if (Kind == Attribute::AllocKind)
2215 B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i]));
2216 else if (Kind == Attribute::Memory)
2217 B.addMemoryAttr(MemoryEffects::createFromIntValue(Record[++i]));
2218 else if (Kind == Attribute::NoFPClass)
2219 B.addNoFPClassAttr(
2220 static_cast<FPClassTest>(Record[++i] & fcAllFlags));
2221 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute
2222 bool HasValue = (Record[i++] == 4);
2223 SmallString<64> KindStr;
2224 SmallString<64> ValStr;
2225
2226 while (Record[i] != 0 && i != e)
2227 KindStr += Record[i++];
2228 assert(Record[i] == 0 && "Kind string not null terminated");
2229
2230 if (HasValue) {
2231 // Has a value associated with it.
2232 ++i; // Skip the '0' that terminates the "kind" string.
2233 while (Record[i] != 0 && i != e)
2234 ValStr += Record[i++];
2235 assert(Record[i] == 0 && "Value string not null terminated");
2236 }
2237
2238 B.addAttribute(KindStr.str(), ValStr.str());
2239 } else if (Record[i] == 5 || Record[i] == 6) {
2240 bool HasType = Record[i] == 6;
2242 if (Error Err = parseAttrKind(Record[++i], &Kind))
2243 return Err;
2244 if (!Attribute::isTypeAttrKind(Kind))
2245 return error("Not a type attribute");
2246
2247 B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr);
2248 } else {
2249 return error("Invalid attribute group entry");
2250 }
2251 }
2252
2253 if (ME != MemoryEffects::unknown())
2254 B.addMemoryAttr(ME);
2255
2257 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
2258 break;
2259 }
2260 }
2261 }
2262}
2263
2264Error BitcodeReader::parseTypeTable() {
2265 if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
2266 return Err;
2267
2268 return parseTypeTableBody();
2269}
2270
2271Error BitcodeReader::parseTypeTableBody() {
2272 if (!TypeList.empty())
2273 return error("Invalid multiple blocks");
2274
2276 unsigned NumRecords = 0;
2277
2279
2280 // Read all the records for this type table.
2281 while (true) {
2282 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2283 if (!MaybeEntry)
2284 return MaybeEntry.takeError();
2285 BitstreamEntry Entry = MaybeEntry.get();
2286
2287 switch (Entry.Kind) {
2288 case BitstreamEntry::SubBlock: // Handled for us already.
2290 return error("Malformed block");
2292 if (NumRecords != TypeList.size())
2293 return error("Malformed block");
2294 return Error::success();
2296 // The interesting case.
2297 break;
2298 }
2299
2300 // Read a record.
2301 Record.clear();
2302 Type *ResultTy = nullptr;
2303 SmallVector<unsigned> ContainedIDs;
2304 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2305 if (!MaybeRecord)
2306 return MaybeRecord.takeError();
2307 switch (MaybeRecord.get()) {
2308 default:
2309 return error("Invalid value");
2310 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
2311 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
2312 // type list. This allows us to reserve space.
2313 if (Record.empty())
2314 return error("Invalid numentry record");
2315 TypeList.resize(Record[0]);
2316 continue;
2317 case bitc::TYPE_CODE_VOID: // VOID
2318 ResultTy = Type::getVoidTy(Context);
2319 break;
2320 case bitc::TYPE_CODE_HALF: // HALF
2321 ResultTy = Type::getHalfTy(Context);
2322 break;
2323 case bitc::TYPE_CODE_BFLOAT: // BFLOAT
2324 ResultTy = Type::getBFloatTy(Context);
2325 break;
2326 case bitc::TYPE_CODE_FLOAT: // FLOAT
2327 ResultTy = Type::getFloatTy(Context);
2328 break;
2329 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
2330 ResultTy = Type::getDoubleTy(Context);
2331 break;
2332 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
2333 ResultTy = Type::getX86_FP80Ty(Context);
2334 break;
2335 case bitc::TYPE_CODE_FP128: // FP128
2336 ResultTy = Type::getFP128Ty(Context);
2337 break;
2338 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
2339 ResultTy = Type::getPPC_FP128Ty(Context);
2340 break;
2341 case bitc::TYPE_CODE_LABEL: // LABEL
2342 ResultTy = Type::getLabelTy(Context);
2343 break;
2344 case bitc::TYPE_CODE_METADATA: // METADATA
2345 ResultTy = Type::getMetadataTy(Context);
2346 break;
2347 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
2348 ResultTy = Type::getX86_MMXTy(Context);
2349 break;
2350 case bitc::TYPE_CODE_X86_AMX: // X86_AMX
2351 ResultTy = Type::getX86_AMXTy(Context);
2352 break;
2353 case bitc::TYPE_CODE_TOKEN: // TOKEN
2354 ResultTy = Type::getTokenTy(Context);
2355 break;
2356 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
2357 if (Record.empty())
2358 return error("Invalid integer record");
2359
2360 uint64_t NumBits = Record[0];
2361 if (NumBits < IntegerType::MIN_INT_BITS ||
2362 NumBits > IntegerType::MAX_INT_BITS)
2363 return error("Bitwidth for integer type out of range");
2364 ResultTy = IntegerType::get(Context, NumBits);
2365 break;
2366 }
2367 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
2368 // [pointee type, address space]
2369 if (Record.empty())
2370 return error("Invalid pointer record");
2371 unsigned AddressSpace = 0;
2372 if (Record.size() == 2)
2373 AddressSpace = Record[1];
2374 ResultTy = getTypeByID(Record[0]);
2375 if (!ResultTy ||
2376 !PointerType::isValidElementType(ResultTy))
2377 return error("Invalid type");
2378 ContainedIDs.push_back(Record[0]);
2379 ResultTy = PointerType::get(ResultTy, AddressSpace);
2380 break;
2381 }
2382 case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace]
2383 if (Record.size() != 1)
2384 return error("Invalid opaque pointer record");
2386 return error(
2387 "Opaque pointers are only supported in -opaque-pointers mode");
2388 unsigned AddressSpace = Record[0];
2389 ResultTy = PointerType::get(Context, AddressSpace);
2390 break;
2391 }
2393 // Deprecated, but still needed to read old bitcode files.
2394 // FUNCTION: [vararg, attrid, retty, paramty x N]
2395 if (Record.size() < 3)
2396 return error("Invalid function record");
2397 SmallVector<Type*, 8> ArgTys;
2398 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
2399 if (Type *T = getTypeByID(Record[i]))
2400 ArgTys.push_back(T);
2401 else
2402 break;
2403 }
2404
2405 ResultTy = getTypeByID(Record[2]);
2406 if (!ResultTy || ArgTys.size() < Record.size()-3)
2407 return error("Invalid type");
2408
2409 ContainedIDs.append(Record.begin() + 2, Record.end());
2410 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
2411 break;
2412 }
2414 // FUNCTION: [vararg, retty, paramty x N]
2415 if (Record.size() < 2)
2416 return error("Invalid function record");
2417 SmallVector<Type*, 8> ArgTys;
2418 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2419 if (Type *T = getTypeByID(Record[i])) {
2420 if (!FunctionType::isValidArgumentType(T))
2421 return error("Invalid function argument type");
2422 ArgTys.push_back(T);
2423 }
2424 else
2425 break;
2426 }
2427
2428 ResultTy = getTypeByID(Record[1]);
2429 if (!ResultTy || ArgTys.size() < Record.size()-2)
2430 return error("Invalid type");
2431
2432 ContainedIDs.append(Record.begin() + 1, Record.end());
2433 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
2434 break;
2435 }
2436 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
2437 if (Record.empty())
2438 return error("Invalid anon struct record");
2439 SmallVector<Type*, 8> EltTys;
2440 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2441 if (Type *T = getTypeByID(Record[i]))
2442 EltTys.push_back(T);
2443 else
2444 break;
2445 }
2446 if (EltTys.size() != Record.size()-1)
2447 return error("Invalid type");
2448 ContainedIDs.append(Record.begin() + 1, Record.end());
2449 ResultTy = StructType::get(Context, EltTys, Record[0]);
2450 break;
2451 }
2452 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
2453 if (convertToString(Record, 0, TypeName))
2454 return error("Invalid struct name record");
2455 continue;
2456
2457 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
2458 if (Record.empty())
2459 return error("Invalid named struct record");
2460
2461 if (NumRecords >= TypeList.size())
2462 return error("Invalid TYPE table");
2463
2464 // Check to see if this was forward referenced, if so fill in the temp.
2465 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
2466 if (Res) {
2467 Res->setName(TypeName);
2468 TypeList[NumRecords] = nullptr;
2469 } else // Otherwise, create a new struct.
2470 Res = createIdentifiedStructType(Context, TypeName);
2471 TypeName.clear();
2472
2473 SmallVector<Type*, 8> EltTys;
2474 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2475 if (Type *T = getTypeByID(Record[i]))
2476 EltTys.push_back(T);
2477 else
2478 break;
2479 }
2480 if (EltTys.size() != Record.size()-1)
2481 return error("Invalid named struct record");
2482 Res->setBody(EltTys, Record[0]);
2483 ContainedIDs.append(Record.begin() + 1, Record.end());
2484 ResultTy = Res;
2485 break;
2486 }
2487 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
2488 if (Record.size() != 1)
2489 return error("Invalid opaque type record");
2490
2491 if (NumRecords >= TypeList.size())
2492 return error("Invalid TYPE table");
2493
2494 // Check to see if this was forward referenced, if so fill in the temp.
2495 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
2496 if (Res) {
2497 Res->setName(TypeName);
2498 TypeList[NumRecords] = nullptr;
2499 } else // Otherwise, create a new struct with no body.
2500 Res = createIdentifiedStructType(Context, TypeName);
2501 TypeName.clear();
2502 ResultTy = Res;
2503 break;
2504 }
2505 case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...]
2506 if (Record.size() < 1)
2507 return error("Invalid target extension type record");
2508
2509 if (NumRecords >= TypeList.size())
2510 return error("Invalid TYPE table");
2511
2512 if (Record[0] >= Record.size())
2513 return error("Too many type parameters");
2514
2515 unsigned NumTys = Record[0];
2516 SmallVector<Type *, 4> TypeParams;
2517 SmallVector<unsigned, 8> IntParams;
2518 for (unsigned i = 0; i < NumTys; i++) {
2519 if (Type *T = getTypeByID(Record[i + 1]))
2520 TypeParams.push_back(T);
2521 else
2522 return error("Invalid type");
2523 }
2524
2525 for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) {
2526 if (Record[i] > UINT_MAX)
2527 return error("Integer parameter too large");
2528 IntParams.push_back(Record[i]);
2529 }
2530 ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams);
2531 TypeName.clear();
2532 break;
2533 }
2534 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
2535 if (Record.size() < 2)
2536 return error("Invalid array type record");
2537 ResultTy = getTypeByID(Record[1]);
2538 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
2539 return error("Invalid type");
2540 ContainedIDs.push_back(Record[1]);
2541 ResultTy = ArrayType::get(ResultTy, Record[0]);
2542 break;
2543 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
2544 // [numelts, eltty, scalable]
2545 if (Record.size() < 2)
2546 return error("Invalid vector type record");
2547 if (Record[0] == 0)
2548 return error("Invalid vector length");
2549 ResultTy = getTypeByID(Record[1]);
2550 if (!ResultTy || !VectorType::isValidElementType(ResultTy))
2551 return error("Invalid type");
2552 bool Scalable = Record.size() > 2 ? Record[2] : false;
2553 ContainedIDs.push_back(Record[1]);
2554 ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
2555 break;
2556 }
2557
2558 if (NumRecords >= TypeList.size())
2559 return error("Invalid TYPE table");
2560 if (TypeList[NumRecords])
2561 return error(
2562 "Invalid TYPE table: Only named structs can be forward referenced");
2563 assert(ResultTy && "Didn't read a type?");
2564 TypeList[NumRecords] = ResultTy;
2565 if (!ContainedIDs.empty())
2566 ContainedTypeIDs[NumRecords] = std::move(ContainedIDs);
2567 ++NumRecords;
2568 }
2569}
2570
2571Error BitcodeReader::parseOperandBundleTags() {
2572 if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
2573 return Err;
2574
2575 if (!BundleTags.empty())
2576 return error("Invalid multiple blocks");
2577
2579
2580 while (true) {
2581 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2582 if (!MaybeEntry)
2583 return MaybeEntry.takeError();
2584 BitstreamEntry Entry = MaybeEntry.get();
2585
2586 switch (Entry.Kind) {
2587 case BitstreamEntry::SubBlock: // Handled for us already.
2589 return error("Malformed block");
2591 return Error::success();
2593 // The interesting case.
2594 break;
2595 }
2596
2597 // Tags are implicitly mapped to integers by their order.
2598
2599 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2600 if (!MaybeRecord)
2601 return MaybeRecord.takeError();
2602 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
2603 return error("Invalid operand bundle record");
2604
2605 // OPERAND_BUNDLE_TAG: [strchr x N]
2606 BundleTags.emplace_back();
2607 if (convertToString(Record, 0, BundleTags.back()))
2608 return error("Invalid operand bundle record");
2609 Record.clear();
2610 }
2611}
2612
2613Error BitcodeReader::parseSyncScopeNames() {
2614 if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
2615 return Err;
2616
2617 if (!SSIDs.empty())
2618 return error("Invalid multiple synchronization scope names blocks");
2619
2621 while (true) {
2622 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2623 if (!MaybeEntry)
2624 return MaybeEntry.takeError();
2625 BitstreamEntry Entry = MaybeEntry.get();
2626
2627 switch (Entry.Kind) {
2628 case BitstreamEntry::SubBlock: // Handled for us already.
2630 return error("Malformed block");
2632 if (SSIDs.empty())
2633 return error("Invalid empty synchronization scope names block");
2634 return Error::success();
2636 // The interesting case.
2637 break;
2638 }
2639
2640 // Synchronization scope names are implicitly mapped to synchronization
2641 // scope IDs by their order.
2642
2643 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2644 if (!MaybeRecord)
2645 return MaybeRecord.takeError();
2646 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
2647 return error("Invalid sync scope record");
2648
2649 SmallString<16> SSN;
2650 if (convertToString(Record, 0, SSN))
2651 return error("Invalid sync scope record");
2652
2654 Record.clear();
2655 }
2656}
2657
2658/// Associate a value with its name from the given index in the provided record.
2659Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2660 unsigned NameIndex, Triple &TT) {
2662 if (convertToString(Record, NameIndex, ValueName))
2663 return error("Invalid record");
2664 unsigned ValueID = Record[0];
2665 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2666 return error("Invalid record");
2667 Value *V = ValueList[ValueID];
2668
2669 StringRef NameStr(ValueName.data(), ValueName.size());
2670 if (NameStr.find_first_of(0) != StringRef::npos)
2671 return error("Invalid value name");
2672 V->setName(NameStr);
2673 auto *GO = dyn_cast<GlobalObject>(V);
2674 if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT())
2675 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
2676 return V;
2677}
2678
2679/// Helper to note and return the current location, and jump to the given
2680/// offset.
2682 BitstreamCursor &Stream) {
2683 // Save the current parsing location so we can jump back at the end
2684 // of the VST read.
2685 uint64_t CurrentBit = Stream.GetCurrentBitNo();
2686 if (Error JumpFailed = Stream.JumpToBit(Offset * 32))
2687 return std::move(JumpFailed);
2688 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2689 if (!MaybeEntry)
2690 return MaybeEntry.takeError();
2691 if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock ||
2692 MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID)
2693 return error("Expected value symbol table subblock");
2694 return CurrentBit;
2695}
2696
2697void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2698 Function *F,
2700 // Note that we subtract 1 here because the offset is relative to one word
2701 // before the start of the identification or module block, which was
2702 // historically always the start of the regular bitcode header.
2703 uint64_t FuncWordOffset = Record[1] - 1;
2704 uint64_t FuncBitOffset = FuncWordOffset * 32;
2705 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2706 // Set the LastFunctionBlockBit to point to the last function block.
2707 // Later when parsing is resumed after function materialization,
2708 // we can simply skip that last function block.
2709 if (FuncBitOffset > LastFunctionBlockBit)
2710 LastFunctionBlockBit = FuncBitOffset;
2711}
2712
2713/// Read a new-style GlobalValue symbol table.
2714Error BitcodeReader::parseGlobalValueSymbolTable() {
2715 unsigned FuncBitcodeOffsetDelta =
2716 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2717
2718 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2719 return Err;
2720
2722 while (true) {
2723 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2724 if (!MaybeEntry)
2725 return MaybeEntry.takeError();
2726 BitstreamEntry Entry = MaybeEntry.get();
2727
2728 switch (Entry.Kind) {
2731 return error("Malformed block");
2733 return Error::success();
2735 break;
2736 }
2737
2738 Record.clear();
2739 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2740 if (!MaybeRecord)
2741 return MaybeRecord.takeError();
2742 switch (MaybeRecord.get()) {
2743 case bitc::VST_CODE_FNENTRY: { // [valueid, offset]
2744 unsigned ValueID = Record[0];
2745 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2746 return error("Invalid value reference in symbol table");
2747 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
2748 cast<Function>(ValueList[ValueID]), Record);
2749 break;
2750 }
2751 }
2752 }
2753}
2754
2755/// Parse the value symbol table at either the current parsing location or
2756/// at the given bit offset if provided.
2757Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
2758 uint64_t CurrentBit;
2759 // Pass in the Offset to distinguish between calling for the module-level
2760 // VST (where we want to jump to the VST offset) and the function-level
2761 // VST (where we don't).
2762 if (Offset > 0) {
2763 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
2764 if (!MaybeCurrentBit)
2765 return MaybeCurrentBit.takeError();
2766 CurrentBit = MaybeCurrentBit.get();
2767 // If this module uses a string table, read this as a module-level VST.
2768 if (UseStrtab) {
2769 if (Error Err = parseGlobalValueSymbolTable())
2770 return Err;
2771 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2772 return JumpFailed;
2773 return Error::success();
2774 }
2775 // Otherwise, the VST will be in a similar format to a function-level VST,
2776 // and will contain symbol names.
2777 }
2778
2779 // Compute the delta between the bitcode indices in the VST (the word offset
2780 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2781 // expected by the lazy reader. The reader's EnterSubBlock expects to have
2782 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2783 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2784 // just before entering the VST subblock because: 1) the EnterSubBlock
2785 // changes the AbbrevID width; 2) the VST block is nested within the same
2786 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2787 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2788 // jump to the FUNCTION_BLOCK using this offset later, we don't want
2789 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2790 unsigned FuncBitcodeOffsetDelta =
2791 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2792
2793 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2794 return Err;
2795
2797
2798 Triple TT(TheModule->getTargetTriple());
2799
2800 // Read all the records for this value table.
2802
2803 while (true) {
2804 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2805 if (!MaybeEntry)
2806 return MaybeEntry.takeError();
2807 BitstreamEntry Entry = MaybeEntry.get();
2808
2809 switch (Entry.Kind) {
2810 case BitstreamEntry::SubBlock: // Handled for us already.
2812 return error("Malformed block");
2814 if (Offset > 0)
2815 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2816 return JumpFailed;
2817 return Error::success();
2819 // The interesting case.
2820 break;
2821 }
2822
2823 // Read a record.
2824 Record.clear();
2825 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2826 if (!MaybeRecord)
2827 return MaybeRecord.takeError();
2828 switch (MaybeRecord.get()) {
2829 default: // Default behavior: unknown type.
2830 break;
2831 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
2832 Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2833 if (Error Err = ValOrErr.takeError())
2834 return Err;
2835 ValOrErr.get();
2836 break;
2837 }
2839 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2840 Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2841 if (Error Err = ValOrErr.takeError())
2842 return Err;
2843 Value *V = ValOrErr.get();
2844
2845 // Ignore function offsets emitted for aliases of functions in older
2846 // versions of LLVM.
2847 if (auto *F = dyn_cast<Function>(V))
2848 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2849 break;
2850 }
2853 return error("Invalid bbentry record");
2854 BasicBlock *BB = getBasicBlock(Record[0]);
2855 if (!BB)
2856 return error("Invalid bbentry record");
2857
2858 BB->setName(StringRef(ValueName.data(), ValueName.size()));
2859 ValueName.clear();
2860 break;
2861 }
2862 }
2863 }
2864}
2865
2866/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2867/// encoding.
2868uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2869 if ((V & 1) == 0)
2870 return V >> 1;
2871 if (V != 1)
2872 return -(V >> 1);
2873 // There is no such thing as -0 with integers. "-0" really means MININT.
2874 return 1ULL << 63;
2875}
2876
2877/// Resolve all of the initializers for global values and aliases that we can.
2878Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2879 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2880 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist;
2881 std::vector<FunctionOperandInfo> FunctionOperandWorklist;
2882
2883 GlobalInitWorklist.swap(GlobalInits);
2884 IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2885 FunctionOperandWorklist.swap(FunctionOperands);
2886
2887 while (!GlobalInitWorklist.empty()) {
2888 unsigned ValID = GlobalInitWorklist.back().second;
2889 if (ValID >= ValueList.size()) {
2890 // Not ready to resolve this yet, it requires something later in the file.
2891 GlobalInits.push_back(GlobalInitWorklist.back());
2892 } else {
2893 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2894 if (!MaybeC)
2895 return MaybeC.takeError();
2896 GlobalInitWorklist.back().first->setInitializer(MaybeC.get());
2897 }
2898 GlobalInitWorklist.pop_back();
2899 }
2900
2901 while (!IndirectSymbolInitWorklist.empty()) {
2902 unsigned ValID = IndirectSymbolInitWorklist.back().second;
2903 if (ValID >= ValueList.size()) {
2904 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2905 } else {
2906 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2907 if (!MaybeC)
2908 return MaybeC.takeError();
2909 Constant *C = MaybeC.get();
2910 GlobalValue *GV = IndirectSymbolInitWorklist.back().first;
2911 if (auto *GA = dyn_cast<GlobalAlias>(GV)) {
2912 if (C->getType() != GV->getType())
2913 return error("Alias and aliasee types don't match");
2914 GA->setAliasee(C);
2915 } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) {
2916 Type *ResolverFTy =
2917 GlobalIFunc::getResolverFunctionType(GI->getValueType());
2918 // Transparently fix up the type for compatibility with older bitcode
2919 GI->setResolver(ConstantExpr::getBitCast(
2920 C, ResolverFTy->getPointerTo(GI->getAddressSpace())));
2921 } else {
2922 return error("Expected an alias or an ifunc");
2923 }
2924 }
2925 IndirectSymbolInitWorklist.pop_back();
2926 }
2927
2928 while (!FunctionOperandWorklist.empty()) {
2929 FunctionOperandInfo &Info = FunctionOperandWorklist.back();
2930 if (Info.PersonalityFn) {
2931 unsigned ValID = Info.PersonalityFn - 1;
2932 if (ValID < ValueList.size()) {
2933 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2934 if (!MaybeC)
2935 return MaybeC.takeError();
2936 Info.F->setPersonalityFn(MaybeC.get());
2937 Info.PersonalityFn = 0;
2938 }
2939 }
2940 if (Info.Prefix) {
2941 unsigned ValID = Info.Prefix - 1;
2942 if (ValID < ValueList.size()) {
2943 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2944 if (!MaybeC)
2945 return MaybeC.takeError();
2946 Info.F->setPrefixData(MaybeC.get());
2947 Info.Prefix = 0;
2948 }
2949 }
2950 if (Info.Prologue) {
2951 unsigned ValID = Info.Prologue - 1;
2952 if (ValID < ValueList.size()) {
2953 Expected<Constant *> MaybeC = getValueForInitializer(ValID);
2954 if (!MaybeC)
2955 return MaybeC.takeError();
2956 Info.F->setPrologueData(MaybeC.get());
2957 Info.Prologue = 0;
2958 }
2959 }
2960 if (Info.PersonalityFn || Info.Prefix || Info.Prologue)
2961 FunctionOperands.push_back(Info);
2962 FunctionOperandWorklist.pop_back();
2963 }
2964
2965 return Error::success();
2966}
2967
2969 SmallVector<uint64_t, 8> Words(Vals.size());
2970 transform(Vals, Words.begin(),
2971 BitcodeReader::decodeSignRotatedValue);
2972
2973 return APInt(TypeBits, Words);
2974}
2975
2976Error BitcodeReader::parseConstants() {
2977 if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2978 return Err;
2979
2981
2982 // Read all the records for this value table.
2983 Type *CurTy = Type::getInt32Ty(Context);
2984 unsigned Int32TyID = getVirtualTypeID(CurTy);
2985 unsigned CurTyID = Int32TyID;
2986 Type *CurElemTy = nullptr;
2987 unsigned NextCstNo = ValueList.size();
2988
2989 while (true) {
2990 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2991 if (!MaybeEntry)
2992 return MaybeEntry.takeError();
2993 BitstreamEntry Entry = MaybeEntry.get();
2994
2995 switch (Entry.Kind) {
2996 case BitstreamEntry::SubBlock: // Handled for us already.
2998 return error("Malformed block");
3000 if (NextCstNo != ValueList.size())
3001 return error("Invalid constant reference");
3002 return Error::success();
3004 // The interesting case.
3005 break;
3006 }
3007
3008 // Read a record.
3009 Record.clear();
3010 Type *VoidType = Type::getVoidTy(Context);
3011 Value *V = nullptr;
3012 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3013 if (!MaybeBitCode)
3014 return MaybeBitCode.takeError();
3015 switch (unsigned BitCode = MaybeBitCode.get()) {
3016 default: // Default behavior: unknown constant
3017 case bitc::CST_CODE_UNDEF: // UNDEF
3018 V = UndefValue::get(CurTy);
3019 break;
3020 case bitc::CST_CODE_POISON: // POISON
3021 V = PoisonValue::get(CurTy);
3022 break;
3023 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
3024 if (Record.empty())
3025 return error("Invalid settype record");
3026 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
3027 return error("Invalid settype record");
3028 if (TypeList[Record[0]] == VoidType)
3029 return error("Invalid constant type");
3030 CurTyID = Record[0];
3031 CurTy = TypeList[CurTyID];
3032 CurElemTy = getPtrElementTypeByID(CurTyID);
3033 continue; // Skip the ValueList manipulation.
3034 case bitc::CST_CODE_NULL: // NULL
3035 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
3036 return error("Invalid type for a constant null value");
3037 if (auto *TETy = dyn_cast<TargetExtType>(CurTy))
3038 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
3039 return error("Invalid type for a constant null value");
3040 V = Constant::getNullValue(CurTy);
3041 break;
3042 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
3043 if (!CurTy->isIntegerTy() || Record.empty())
3044 return error("Invalid integer const record");
3046 break;
3047 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
3048 if (!CurTy->isIntegerTy() || Record.empty())
3049 return error("Invalid wide integer const record");
3050
3051 APInt VInt =
3052 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
3053 V = ConstantInt::get(Context, VInt);
3054
3055 break;
3056 }
3057 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
3058 if (Record.empty())
3059 return error("Invalid float const record");
3060 if (CurTy->isHalfTy())
3061 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(),
3062 APInt(16, (uint16_t)Record[0])));
3063 else if (CurTy->isBFloatTy())
3064 V = ConstantFP::get(Context, APFloat(APFloat::BFloat(),
3065 APInt(16, (uint32_t)Record[0])));
3066 else if (CurTy->isFloatTy())
3067 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(),
3068 APInt(32, (uint32_t)Record[0])));
3069 else if (CurTy->isDoubleTy())
3070 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(),
3071 APInt(64, Record[0])));
3072 else if (CurTy->isX86_FP80Ty()) {
3073 // Bits are not stored the same way as a normal i80 APInt, compensate.
3074 uint64_t Rearrange[2];
3075 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
3076 Rearrange[1] = Record[0] >> 48;
3077 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(),
3078 APInt(80, Rearrange)));
3079 } else if (CurTy->isFP128Ty())
3080 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(),
3081 APInt(128, Record)));
3082 else if (CurTy->isPPC_FP128Ty())
3083 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(),
3084 APInt(128, Record)));
3085 else
3086 V = UndefValue::get(CurTy);
3087 break;
3088 }
3089
3090 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
3091 if (Record.empty())
3092 return error("Invalid aggregate record");
3093
3094 unsigned Size = Record.size();
3096 for (unsigned i = 0; i != Size; ++i)
3097 Elts.push_back(Record[i]);
3098
3099 if (isa<StructType>(CurTy)) {
3100 V = BitcodeConstant::create(
3101 Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts);
3102 } else if (isa<ArrayType>(CurTy)) {
3103 V = BitcodeConstant::create(Alloc, CurTy,
3104 BitcodeConstant::ConstantArrayOpcode, Elts);
3105 } else if (isa<VectorType>(CurTy)) {
3106 V = BitcodeConstant::create(
3107 Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts);
3108 } else {
3109 V = UndefValue::get(CurTy);
3110 }
3111 break;
3112 }
3113 case bitc::CST_CODE_STRING: // STRING: [values]
3114 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
3115 if (Record.empty())
3116 return error("Invalid string record");
3117
3118 SmallString<16> Elts(Record.begin(), Record.end());
3119 V = ConstantDataArray::getString(Context, Elts,
3120 BitCode == bitc::CST_CODE_CSTRING);
3121 break;
3122 }
3123 case bitc::CST_CODE_DATA: {// DATA: [n x value]
3124 if (Record.empty())
3125 return error("Invalid data record");
3126
3127 Type *EltTy;
3128 if (auto *Array = dyn_cast<ArrayType>(CurTy))
3129 EltTy = Array->getElementType();
3130 else
3131 EltTy = cast<VectorType>(CurTy)->getElementType();
3132 if (EltTy->isIntegerTy(8)) {
3133 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
3134 if (isa<VectorType>(CurTy))
3135 V = ConstantDataVector::get(Context, Elts);
3136 else
3137 V = ConstantDataArray::get(Context, Elts);
3138 } else if (EltTy->isIntegerTy(16)) {
3139 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3140 if (isa<VectorType>(CurTy))
3141 V = ConstantDataVector::get(Context, Elts);
3142 else
3143 V = ConstantDataArray::get(Context, Elts);
3144 } else if (EltTy->isIntegerTy(32)) {
3145 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3146 if (isa<VectorType>(CurTy))
3147 V = ConstantDataVector::get(Context, Elts);
3148 else
3149 V = ConstantDataArray::get(Context, Elts);
3150 } else if (EltTy->isIntegerTy(64)) {
3151 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3152 if (isa<VectorType>(CurTy))
3153 V = ConstantDataVector::get(Context, Elts);
3154 else
3155 V = ConstantDataArray::get(Context, Elts);
3156 } else if (EltTy->isHalfTy()) {
3157 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3158 if (isa<VectorType>(CurTy))
3159 V = ConstantDataVector::getFP(EltTy, Elts);
3160 else
3161 V = ConstantDataArray::getFP(EltTy, Elts);
3162 } else if (EltTy->isBFloatTy()) {
3163 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3164 if (isa<VectorType>(CurTy))
3165 V = ConstantDataVector::getFP(EltTy, Elts);
3166 else
3167 V = ConstantDataArray::getFP(EltTy, Elts);
3168 } else if (EltTy->isFloatTy()) {
3169 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3170 if (isa<VectorType>(CurTy))
3171 V = ConstantDataVector::getFP(EltTy, Elts);
3172 else
3173 V = ConstantDataArray::getFP(EltTy, Elts);
3174 } else if (EltTy->isDoubleTy()) {
3175 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3176 if (isa<VectorType>(CurTy))
3177 V = ConstantDataVector::getFP(EltTy, Elts);
3178 else
3179 V = ConstantDataArray::getFP(EltTy, Elts);
3180 } else {
3181 return error("Invalid type for value");
3182 }
3183 break;
3184 }
3185 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
3186 if (Record.size() < 2)
3187 return error("Invalid unary op constexpr record");
3188 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
3189 if (Opc < 0) {
3190 V = UndefValue::get(CurTy); // Unknown unop.
3191 } else {
3192 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]);
3193 }
3194 break;
3195 }
3196 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
3197 if (Record.size() < 3)
3198 return error("Invalid binary op constexpr record");
3199 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
3200 if (Opc < 0) {
3201 V = UndefValue::get(CurTy); // Unknown binop.
3202 } else {
3203 uint8_t Flags = 0;
3204 if (Record.size() >= 4) {
3205 if (Opc == Instruction::Add ||
3206 Opc == Instruction::Sub ||
3207 Opc == Instruction::Mul ||
3208 Opc == Instruction::Shl) {
3209 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3211 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3213 } else if (Opc == Instruction::SDiv ||
3214 Opc == Instruction::UDiv ||
3215 Opc == Instruction::LShr ||
3216 Opc == Instruction::AShr) {
3217 if (Record[3] & (1 << bitc::PEO_EXACT))
3218 Flags |= SDivOperator::IsExact;
3219 }
3220 }
3221 V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags},
3222 {(unsigned)Record[1], (unsigned)Record[2]});
3223 }
3224 break;
3225 }
3226 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
3227 if (Record.size() < 3)
3228 return error("Invalid cast constexpr record");
3229 int Opc = getDecodedCastOpcode(Record[0]);
3230 if (Opc < 0) {
3231 V = UndefValue::get(CurTy); // Unknown cast.
3232 } else {
3233 unsigned OpTyID = Record[1];
3234 Type *OpTy = getTypeByID(OpTyID);
3235 if (!OpTy)
3236 return error("Invalid cast constexpr record");
3237 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]);
3238 }
3239 break;
3240 }
3241 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
3242 case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
3243 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
3244 // operands]
3245 if (Record.size() < 2)
3246 return error("Constant GEP record must have at least two elements");
3247 unsigned OpNum = 0;
3248 Type *PointeeType = nullptr;
3250 Record.size() % 2)
3251 PointeeType = getTypeByID(Record[OpNum++]);
3252
3253 bool InBounds = false;
3254 std::optional<unsigned> InRangeIndex;
3256 uint64_t Op = Record[OpNum++];
3257 InBounds = Op & 1;
3258 InRangeIndex = Op >> 1;
3259 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
3260 InBounds = true;
3261
3263 unsigned BaseTypeID = Record[OpNum];
3264 while (OpNum != Record.size()) {
3265 unsigned ElTyID = Record[OpNum++];
3266 Type *ElTy = getTypeByID(ElTyID);
3267 if (!ElTy)
3268 return error("Invalid getelementptr constexpr record");
3269 Elts.push_back(Record[OpNum++]);
3270 }
3271
3272 if (Elts.size() < 1)
3273 return error("Invalid gep with no operands");
3274
3275 Type *BaseType = getTypeByID(BaseTypeID);
3276 if (isa<VectorType>(BaseType)) {
3277 BaseTypeID = getContainedTypeID(BaseTypeID, 0);
3278 BaseType = getTypeByID(BaseTypeID);
3279 }
3280
3281 PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType);
3282 if (!OrigPtrTy)
3283 return error("GEP base operand must be pointer or vector of pointer");
3284
3285 if (!PointeeType) {
3286 PointeeType = getPtrElementTypeByID(BaseTypeID);
3287 if (!PointeeType)
3288 return error("Missing element type for old-style constant GEP");
3289 } else if (!OrigPtrTy->isOpaqueOrPointeeTypeMatches(PointeeType))
3290 return error("Explicit gep operator type does not match pointee type "
3291 "of pointer operand");
3292
3293 V = BitcodeConstant::create(Alloc, CurTy,
3294 {Instruction::GetElementPtr, InBounds,
3295 InRangeIndex.value_or(-1), PointeeType},
3296 Elts);
3297 break;
3298 }
3299 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
3300 if (Record.size() < 3)
3301 return error("Invalid select constexpr record");
3302
3303 V = BitcodeConstant::create(
3304 Alloc, CurTy, Instruction::Select,
3305 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3306 break;
3307 }
3309 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
3310 if (Record.size() < 3)
3311 return error("Invalid extractelement constexpr record");
3312 unsigned OpTyID = Record[0];
3313 VectorType *OpTy =
3314 dyn_cast_or_null<VectorType>(getTypeByID(OpTyID));
3315 if (!OpTy)
3316 return error("Invalid extractelement constexpr record");
3317 unsigned IdxRecord;
3318 if (Record.size() == 4) {
3319 unsigned IdxTyID = Record[2];
3320 Type *IdxTy = getTypeByID(IdxTyID);
3321 if (!IdxTy)
3322 return error("Invalid extractelement constexpr record");
3323 IdxRecord = Record[3];
3324 } else {
3325 // Deprecated, but still needed to read old bitcode files.
3326 IdxRecord = Record[2];
3327 }
3328 V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement,
3329 {(unsigned)Record[1], IdxRecord});
3330 break;
3331 }
3333 : { // CE_INSERTELT: [opval, opval, opty, opval]
3334 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3335 if (Record.size() < 3 || !OpTy)
3336 return error("Invalid insertelement constexpr record");
3337 unsigned IdxRecord;
3338 if (Record.size() == 4) {
3339 unsigned IdxTyID = Record[2];
3340 Type *IdxTy = getTypeByID(IdxTyID);
3341 if (!IdxTy)
3342 return error("Invalid insertelement constexpr record");
3343 IdxRecord = Record[3];
3344 } else {
3345 // Deprecated, but still needed to read old bitcode files.
3346 IdxRecord = Record[2];
3347 }
3348 V = BitcodeConstant::create(
3349 Alloc, CurTy, Instruction::InsertElement,
3350 {(unsigned)Record[0], (unsigned)Record[1], IdxRecord});
3351 break;
3352 }
3353 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
3354 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3355 if (Record.size() < 3 || !OpTy)
3356 return error("Invalid shufflevector constexpr record");
3357 V = BitcodeConstant::create(
3358 Alloc, CurTy, Instruction::ShuffleVector,
3359 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3360 break;
3361 }
3362 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
3363 VectorType *RTy = dyn_cast<VectorType>(CurTy);
3364 VectorType *OpTy =
3365 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
3366 if (Record.size() < 4 || !RTy || !OpTy)
3367 return error("Invalid shufflevector constexpr record");
3368 V = BitcodeConstant::create(
3369 Alloc, CurTy, Instruction::ShuffleVector,
3370 {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]});
3371 break;
3372 }
3373 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
3374 if (Record.size() < 4)
3375 return error("Invalid cmp constexpt record");
3376 unsigned OpTyID = Record[0];
3377 Type *OpTy = getTypeByID(OpTyID);
3378 if (!OpTy)
3379 return error("Invalid cmp constexpr record");
3380 V = BitcodeConstant::create(
3381 Alloc, CurTy,
3382 {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp
3383 : Instruction::ICmp),
3384 (uint8_t)Record[3]},
3385 {(unsigned)Record[1], (unsigned)Record[2]});
3386 break;
3387 }
3388 // This maintains backward compatibility, pre-asm dialect keywords.
3389 // Deprecated, but still needed to read old bitcode files.
3391 if (Record.size() < 2)
3392 return error("Invalid inlineasm record");
3393 std::string AsmStr, ConstrStr;
3394 bool HasSideEffects = Record[0] & 1;
3395 bool IsAlignStack = Record[0] >> 1;
3396 unsigned AsmStrSize = Record[1];
3397 if (2+AsmStrSize >= Record.size())
3398 return error("Invalid inlineasm record");
3399 unsigned ConstStrSize = Record[2+AsmStrSize];
3400 if (3+AsmStrSize+ConstStrSize > Record.size())
3401 return error("Invalid inlineasm record");
3402
3403 for (unsigned i = 0; i != AsmStrSize; ++i)
3404 AsmStr += (char)Record[2+i];
3405 for (unsigned i = 0; i != ConstStrSize; ++i)
3406 ConstrStr += (char)Record[3+AsmStrSize+i];
3407 UpgradeInlineAsmString(&AsmStr);
3408 if (!CurElemTy)
3409 return error("Missing element type for old-style inlineasm");
3410 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3411 HasSideEffects, IsAlignStack);
3412 break;
3413 }
3414 // This version adds support for the asm dialect keywords (e.g.,
3415 // inteldialect).
3417 if (Record.size() < 2)
3418 return error("Invalid inlineasm record");
3419 std::string AsmStr, ConstrStr;
3420 bool HasSideEffects = Record[0] & 1;
3421 bool IsAlignStack = (Record[0] >> 1) & 1;
3422 unsigned AsmDialect = Record[0] >> 2;
3423 unsigned AsmStrSize = Record[1];
3424 if (2+AsmStrSize >= Record.size())
3425 return error("Invalid inlineasm record");
3426 unsigned ConstStrSize = Record[2+AsmStrSize];
3427 if (3+AsmStrSize+ConstStrSize > Record.size())
3428 return error("Invalid inlineasm record");
3429
3430 for (unsigned i = 0; i != AsmStrSize; ++i)
3431 AsmStr += (char)Record[2+i];
3432 for (unsigned i = 0; i != ConstStrSize; ++i)
3433 ConstrStr += (char)Record[3+AsmStrSize+i];
3434 UpgradeInlineAsmString(&AsmStr);
3435 if (!CurElemTy)
3436 return error("Missing element type for old-style inlineasm");
3437 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3438 HasSideEffects, IsAlignStack,
3439 InlineAsm::AsmDialect(AsmDialect));
3440 break;
3441 }
3442 // This version adds support for the unwind keyword.
3444 if (Record.size() < 2)
3445 return error("Invalid inlineasm record");
3446 unsigned OpNum = 0;
3447 std::string AsmStr, ConstrStr;
3448 bool HasSideEffects = Record[OpNum] & 1;
3449 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3450 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3451 bool CanThrow = (Record[OpNum] >> 3) & 1;
3452 ++OpNum;
3453 unsigned AsmStrSize = Record[OpNum];
3454 ++OpNum;
3455 if (OpNum + AsmStrSize >= Record.size())
3456 return error("Invalid inlineasm record");
3457 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3458 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3459 return error("Invalid inlineasm record");
3460
3461 for (unsigned i = 0; i != AsmStrSize; ++i)
3462 AsmStr += (char)Record[OpNum + i];
3463 ++OpNum;
3464 for (unsigned i = 0; i != ConstStrSize; ++i)
3465 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3466 UpgradeInlineAsmString(&AsmStr);
3467 if (!CurElemTy)
3468 return error("Missing element type for old-style inlineasm");
3469 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr,
3470 HasSideEffects, IsAlignStack,
3471 InlineAsm::AsmDialect(AsmDialect), CanThrow);
3472 break;
3473 }
3474 // This version adds explicit function type.
3476 if (Record.size() < 3)
3477 return error("Invalid inlineasm record");
3478 unsigned OpNum = 0;
3479 auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum]));
3480 ++OpNum;
3481 if (!FnTy)
3482 return error("Invalid inlineasm record");
3483 std::string AsmStr, ConstrStr;
3484 bool HasSideEffects = Record[OpNum] & 1;
3485 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3486 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3487 bool CanThrow = (Record[OpNum] >> 3) & 1;
3488 ++OpNum;
3489 unsigned AsmStrSize = Record[OpNum];
3490 ++OpNum;
3491 if (OpNum + AsmStrSize >= Record.size())
3492 return error("Invalid inlineasm record");
3493 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3494 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3495 return error("Invalid inlineasm record");
3496
3497 for (unsigned i = 0; i != AsmStrSize; ++i)
3498 AsmStr += (char)Record[OpNum + i];
3499 ++OpNum;
3500 for (unsigned i = 0; i != ConstStrSize; ++i)
3501 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3502 UpgradeInlineAsmString(&AsmStr);
3503 V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
3504 InlineAsm::AsmDialect(AsmDialect), CanThrow);
3505 break;
3506 }
3508 if (Record.size() < 3)
3509 return error("Invalid blockaddress record");
3510 unsigned FnTyID = Record[0];
3511 Type *FnTy = getTypeByID(FnTyID);
3512 if (!FnTy)
3513 return error("Invalid blockaddress record");
3514 V = BitcodeConstant::create(
3515 Alloc, CurTy,
3516 {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]},
3517 Record[1]);
3518 break;
3519 }
3521 if (Record.size() < 2)
3522 return error("Invalid dso_local record");
3523 unsigned GVTyID = Record[0];
3524 Type *GVTy = getTypeByID(GVTyID);
3525 if (!GVTy)
3526 return error("Invalid dso_local record");
3527 V = BitcodeConstant::create(
3528 Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]);
3529 break;
3530 }
3532 if (Record.size() < 2)
3533 return error("Invalid no_cfi record");
3534 unsigned GVTyID = Record[0];
3535 Type *GVTy = getTypeByID(GVTyID);
3536 if (!GVTy)
3537 return error("Invalid no_cfi record");
3538 V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode,
3539 Record[1]);
3540 break;
3541 }
3542 }
3543
3544 assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");
3545 if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID))
3546 return Err;
3547 ++NextCstNo;
3548 }
3549}
3550
3551Error BitcodeReader::parseUseLists() {
3552 if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
3553 return Err;
3554
3555 // Read all the records.
3557
3558 while (true) {
3559 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3560 if (!MaybeEntry)
3561 return MaybeEntry.takeError();
3562 BitstreamEntry Entry = MaybeEntry.get();
3563
3564 switch (Entry.Kind) {
3565 case BitstreamEntry::SubBlock: // Handled for us already.
3567 return error("Malformed block");
3569 return Error::success();
3571 // The interesting case.
3572 break;
3573 }
3574
3575 // Read a use list record.
3576 Record.clear();
3577 bool IsBB = false;
3578 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
3579 if (!MaybeRecord)
3580 return MaybeRecord.takeError();
3581 switch (MaybeRecord.get()) {
3582 default: // Default behavior: unknown type.
3583 break;
3585 IsBB = true;
3586 [[fallthrough]];
3588 unsigned RecordLength = Record.size();
3589 if (RecordLength < 3)
3590 // Records should have at least an ID and two indexes.
3591 return error("Invalid record");
3592 unsigned ID = Record.pop_back_val();
3593
3594 Value *V;
3595 if (IsBB) {
3596 assert(ID < FunctionBBs.size() && "Basic block not found");
3597 V = FunctionBBs[ID];
3598 } else
3599 V = ValueList[ID];
3600 unsigned NumUses = 0;
3602 for (const Use &U : V->materialized_uses()) {
3603 if (++NumUses > Record.size())
3604 break;
3605 Order[&U] = Record[NumUses - 1];
3606 }
3607 if (Order.size() != Record.size() || NumUses > Record.size())
3608 // Mismatches can happen if the functions are being materialized lazily
3609 // (out-of-order), or a value has been upgraded.
3610 break;
3611
3612 V->sortUseList([&](const Use &L, const Use &R) {
3613 return Order.lookup(&L) < Order.lookup(&R);
3614 });
3615 break;
3616 }
3617 }
3618 }
3619}
3620
3621/// When we see the block for metadata, remember where it is and then skip it.
3622/// This lets us lazily deserialize the metadata.
3623Error BitcodeReader::rememberAndSkipMetadata() {
3624 // Save the current stream state.
3625 uint64_t CurBit = Stream.GetCurrentBitNo();
3626 DeferredMetadataInfo.push_back(CurBit);
3627
3628 // Skip over the block for now.
3629 if (Error Err = Stream.SkipBlock())
3630 return Err;
3631 return Error::success();
3632}
3633
3634Error BitcodeReader::materializeMetadata() {
3635 for (uint64_t BitPos : DeferredMetadataInfo) {
3636 // Move the bit stream to the saved position.
3637 if (Error JumpFailed = Stream.JumpToBit(BitPos))
3638 return JumpFailed;
3639 if (Error Err = MDLoader->parseModuleMetadata())
3640 return Err;
3641 }
3642
3643 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
3644 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
3645 // multiple times.
3646 if (!TheModule->getNamedMetadata("llvm.linker.options")) {
3647 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
3648 NamedMDNode *LinkerOpts =
3649 TheModule->getOrInsertNamedMetadata("llvm.linker.options");
3650 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
3651 LinkerOpts->addOperand(cast<MDNode>(MDOptions));
3652 }
3653 }
3654
3655 DeferredMetadataInfo.clear();
3656 return Error::success();
3657}
3658
3659void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3660
3661/// When we see the block for a function body, remember where it is and then
3662/// skip it. This lets us lazily deserialize the functions.
3663Error BitcodeReader::rememberAndSkipFunctionBody() {
3664 // Get the function we are talking about.
3665 if (FunctionsWithBodies.empty())
3666 return error("Insufficient function protos");
3667
3668 Function *Fn = FunctionsWithBodies.back();
3669 FunctionsWithBodies.pop_back();
3670
3671 // Save the current stream state.
3672 uint64_t CurBit = Stream.GetCurrentBitNo();
3673 assert(
3674 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3675 "Mismatch between VST and scanned function offsets");
3676 DeferredFunctionInfo[Fn] = CurBit;
3677
3678 // Skip over the function block for now.
3679 if (Error Err = Stream.SkipBlock())
3680 return Err;
3681 return Error::success();
3682}
3683
3684Error BitcodeReader::globalCleanup() {
3685 // Patch the initializers for globals and aliases up.
3686 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3687 return Err;
3688 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3689 return error("Malformed global initializer set");
3690
3691 // Look for intrinsic functions which need to be upgraded at some point
3692 // and functions that need to have their function attributes upgraded.
3693 for (Function &F : *TheModule) {
3694 MDLoader->upgradeDebugIntrinsics(F);
3695 Function *NewFn;
3696 if (UpgradeIntrinsicFunction(&F, NewFn))
3697 UpgradedIntrinsics[&F] = NewFn;
3698 // Look for functions that rely on old function attribute behavior.
3700 }
3701
3702 // Look for global variables which need to be renamed.
3703 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
3704 for (GlobalVariable &GV : TheModule->globals())
3705 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))
3706 UpgradedVariables.emplace_back(&GV, Upgraded);
3707 for (auto &Pair : UpgradedVariables) {
3708 Pair.first->eraseFromParent();
3709 TheModule->insertGlobalVariable(Pair.second);
3710 }
3711
3712 // Force deallocation of memory for these vectors to favor the client that
3713 // want lazy deserialization.
3714 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
3715 std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits);
3716 return Error::success();
3717}
3718
3719/// Support for lazy parsing of function bodies. This is required if we
3720/// either have an old bitcode file without a VST forward declaration record,
3721/// or if we have an anonymous function being materialized, since anonymous
3722/// functions do not have a name and are therefore not in the VST.
3723Error BitcodeReader::rememberAndSkipFunctionBodies() {
3724 if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))
3725 return JumpFailed;
3726
3727 if (Stream.AtEndOfStream())
3728 return error("Could not find function in stream");
3729
3730 if (!SeenFirstFunctionBody)
3731 return error("Trying to materialize functions before seeing function blocks");
3732
3733 // An old bitcode file with the symbol table at the end would have
3734 // finished the parse greedily.
3735 assert(SeenValueSymbolTable);
3736
3738
3739 while (true) {
3740 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3741 if (!MaybeEntry)
3742 return MaybeEntry.takeError();
3743 llvm::BitstreamEntry Entry = MaybeEntry.get();
3744
3745 switch (Entry.Kind) {
3746 default:
3747 return error("Expect SubBlock");
3749 switch (Entry.ID) {
3750 default:
3751 return error("Expect function block");
3753 if (Error Err = rememberAndSkipFunctionBody())
3754 return Err;
3755 NextUnreadBit = Stream.GetCurrentBitNo();
3756 return Error::success();
3757 }
3758 }
3759 }
3760}
3761
3762Error BitcodeReaderBase::readBlockInfo() {
3764 Stream.ReadBlockInfoBlock();
3765 if (!MaybeNewBlockInfo)
3766 return MaybeNewBlockInfo.takeError();
3767 std::optional<BitstreamBlockInfo> NewBlockInfo =
3768 std::move(MaybeNewBlockInfo.get());
3769 if (!NewBlockInfo)
3770 return error("Malformed block");
3771 BlockInfo = std::move(*NewBlockInfo);
3772 return Error::success();
3773}
3774
3775Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
3776 // v1: [selection_kind, name]
3777 // v2: [strtab_offset, strtab_size, selection_kind]
3779 std::tie(Name, Record) = readNameFromStrtab(Record);
3780
3781 if (Record.empty())
3782 return error("Invalid record");
3784 std::string OldFormatName;
3785 if (!UseStrtab) {
3786 if (Record.size() < 2)
3787 return error("Invalid record");
3788 unsigned ComdatNameSize = Record[1];
3789 if (ComdatNameSize > Record.size() - 2)
3790 return error("Comdat name size too large");
3791 OldFormatName.reserve(ComdatNameSize);
3792 for (unsigned i = 0; i != ComdatNameSize; ++i)
3793 OldFormatName += (char)Record[2 + i];
3794 Name = OldFormatName;
3795 }
3796 Comdat *C = TheModule->getOrInsertComdat(Name);
3797 C->setSelectionKind(SK);
3798 ComdatList.push_back(C);
3799 return Error::success();
3800}
3801
3802static void inferDSOLocal(GlobalValue *GV) {
3803 // infer dso_local from linkage and visibility if it is not encoded.
3804 if (GV->hasLocalLinkage() ||
3806 GV->setDSOLocal(true);
3807}
3808
3811 if (V & (1 << 0))
3812 Meta.NoAddress = true;
3813 if (V & (1 << 1))
3814 Meta.NoHWAddress = true;
3815 if (V & (1 << 2))
3816 Meta.Memtag = true;
3817 if (V & (1 << 3))
3818 Meta.IsDynInit = true;
3819 return Meta;
3820}
3821
3822Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
3823 // v1: [pointer type, isconst, initid, linkage, alignment, section,
3824 // visibility, threadlocal, unnamed_addr, externally_initialized,
3825 // dllstorageclass, comdat, attributes, preemption specifier,
3826 // partition strtab offset, partition strtab size] (name in VST)
3827 // v2: [strtab_offset, strtab_size, v1]
3829 std::tie(Name, Record) = readNameFromStrtab(Record);
3830
3831 if (Record.size() < 6)
3832 return error("Invalid record");
3833 unsigned TyID = Record[0];
3834 Type *Ty = getTypeByID(TyID);
3835 if (!Ty)
3836 return error("Invalid record");
3837 bool isConstant = Record[1] & 1;
3838 bool explicitType = Record[1] & 2;
3839 unsigned AddressSpace;
3840 if (explicitType) {
3841 AddressSpace = Record[1] >> 2;
3842 } else {
3843 if (!Ty->isPointerTy())
3844 return error("Invalid type for value");
3845 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3846 TyID = getContainedTypeID(TyID);
3847 Ty = getTypeByID(TyID);
3848 if (!Ty)
3849 return error("Missing element type for old-style global");
3850 }
3851
3852 uint64_t RawLinkage = Record[3];
3854 MaybeAlign Alignment;
3855 if (Error Err = parseAlignmentValue(Record[4], Alignment))
3856 return Err;
3857 std::string Section;
3858 if (Record[5]) {
3859 if (Record[5] - 1 >= SectionTable.size())
3860 return error("Invalid ID");
3861 Section = SectionTable[Record[5] - 1];
3862 }
3864 // Local linkage must have default visibility.
3865 // auto-upgrade `hidden` and `protected` for old bitcode.
3866 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3867 Visibility = getDecodedVisibility(Record[6]);
3868
3869 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3870 if (Record.size() > 7)
3872
3873 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3874 if (Record.size() > 8)
3875 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
3876
3877 bool ExternallyInitialized = false;
3878 if (Record.size() > 9)
3879 ExternallyInitialized = Record[9];
3880
3881 GlobalVariable *NewGV =
3882 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
3883 nullptr, TLM, AddressSpace, ExternallyInitialized);
3884 if (Alignment)
3885 NewGV->setAlignment(*Alignment);
3886 if (!Section.empty())
3887 NewGV->setSection(Section);
3888 NewGV->setVisibility(Visibility);
3889 NewGV->setUnnamedAddr(UnnamedAddr);
3890
3891 if (Record.size() > 10) {
3892 // A GlobalValue with local linkage cannot have a DLL storage class.
3893 if (!NewGV->hasLocalLinkage()) {
3895 }
3896 } else {
3897 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3898 }
3899
3900 ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID));
3901
3902 // Remember which value to use for the global initializer.
3903 if (unsigned InitID = Record[2])
3904 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
3905
3906 if (Record.size() > 11) {
3907 if (unsigned ComdatID = Record[11]) {
3908 if (ComdatID > ComdatList.size())
3909 return error("Invalid global variable comdat ID");
3910 NewGV->setComdat(ComdatList[ComdatID - 1]);
3911 }
3912 } else if (hasImplicitComdat(RawLinkage)) {
3913 ImplicitComdatObjects.insert(NewGV);
3914 }
3915
3916 if (Record.size() > 12) {
3917 auto AS = getAttributes(Record[12]).getFnAttrs();
3918 NewGV->setAttributes(AS);
3919 }
3920
3921 if (Record.size() > 13) {
3923 }
3924 inferDSOLocal(NewGV);
3925
3926 // Check whether we have enough values to read a partition name.
3927 if (Record.size() > 15)
3928 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
3929
3930 if (Record.size() > 16 && Record[16]) {
3933 NewGV->setSanitizerMetadata(Meta);
3934 }
3935
3936 return Error::success();
3937}
3938
3939void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) {
3940 if (ValueTypeCallback) {
3941 (*ValueTypeCallback)(
3942 F, TypeID, [this](unsigned I) { return getTypeByID(I); },
3943 [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); });
3944 }
3945}
3946
3947Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
3948 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3949 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3950 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
3951 // v2: [strtab_offset, strtab_size, v1]
3953 std::tie(Name, Record) = readNameFromStrtab(Record);
3954
3955 if (Record.size() < 8)
3956 return error("Invalid record");
3957 unsigned FTyID = Record[0];
3958 Type *FTy = getTypeByID(FTyID);
3959 if (!FTy)
3960 return error("Invalid record");
3961 if (isa<PointerType>(FTy)) {
3962 FTyID = getContainedTypeID(FTyID, 0);
3963 FTy = getTypeByID(FTyID);
3964 if (!FTy)
3965 return error("Missing element type for old-style function");
3966 }
3967
3968 if (!isa<FunctionType>(FTy))
3969 return error("Invalid type for value");
3970 auto CC = static_cast<CallingConv::ID>(Record[1]);
3971 if (CC & ~CallingConv::MaxID)
3972 return error("Invalid calling convention ID");
3973
3974 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
3975 if (Record.size() > 16)
3976 AddrSpace = Record[16];
3977
3978 Function *Func =
3979 Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,
3980 AddrSpace, Name, TheModule);
3981
3982 assert(Func->getFunctionType() == FTy &&
3983 "Incorrect fully specified type provided for function");
3984 FunctionTypeIDs[Func] = FTyID;
3985
3986 Func->setCallingConv(CC);
3987 bool isProto = Record[2];
3988 uint64_t RawLinkage = Record[3];
3989 Func->setLinkage(getDecodedLinkage(RawLinkage));
3990 Func->setAttributes(getAttributes(Record[4]));
3991 callValueTypeCallback(Func, FTyID);
3992
3993 // Upgrade any old-style byval or sret without a type by propagating the
3994 // argument's pointee type. There should be no opaque pointers where the byval
3995 // type is implicit.
3996 for (unsigned i = 0; i != Func->arg_size(); ++i) {
3997 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
3998 Attribute::InAlloca}) {
3999 if (!Func->hasParamAttribute(i, Kind))
4000 continue;
4001
4002 if (Func->getParamAttribute(i, Kind).getValueAsType())
4003 continue;
4004
4005 Func->removeParamAttr(i, Kind);
4006
4007 unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1);
4008 Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID);
4009 if (!PtrEltTy)
4010 return error("Missing param element type for attribute upgrade");
4011
4012 Attribute NewAttr;
4013 switch (Kind) {
4014 case Attribute::ByVal:
4015 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
4016 break;
4017 case Attribute::StructRet:
4018 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
4019 break;
4020 case Attribute::InAlloca:
4021 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
4022 break;
4023 default:
4024 llvm_unreachable("not an upgraded type attribute");
4025 }
4026
4027 Func->addParamAttr(i, NewAttr);
4028 }
4029 }
4030
4031 if (Func->getCallingConv() == CallingConv::X86_INTR &&
4032 !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) {
4033 unsigned ParamTypeID = getContainedTypeID(FTyID, 1);
4034 Type *ByValTy = getPtrElementTypeByID(ParamTypeID);
4035 if (!ByValTy)
4036 return error("Missing param element type for x86_intrcc upgrade");
4037 Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy);
4038 Func->addParamAttr(0, NewAttr);
4039 }
4040
4041 MaybeAlign Alignment;
4042 if (Error Err = parseAlignmentValue(Record[5], Alignment))
4043 return Err;
4044 if (Alignment)
4045 Func->setAlignment(*Alignment);
4046 if (Record[6]) {
4047 if (Record[6] - 1 >= SectionTable.size())
4048 return error("Invalid ID");
4049 Func->setSection(SectionTable[Record[6] - 1]);
4050 }
4051 // Local linkage must have default visibility.
4052 // auto-upgrade `hidden` and `protected` for old bitcode.
4053 if (!Func->hasLocalLinkage())
4054 Func->setVisibility(getDecodedVisibility(Record[7]));
4055 if (Record.size() > 8 && Record[8]) {
4056 if (Record[8] - 1 >= GCTable.size())
4057 return error("Invalid ID");
4058 Func->setGC(GCTable[Record[8] - 1]);
4059 }
4060 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
4061 if (Record.size() > 9)
4062 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
4063 Func->setUnnamedAddr(UnnamedAddr);
4064
4065 FunctionOperandInfo OperandInfo = {Func, 0, 0, 0};
4066 if (Record.size() > 10)
4067 OperandInfo.Prologue = Record[10];
4068
4069 if (Record.size() > 11) {
4070 // A GlobalValue with local linkage cannot have a DLL storage class.
4071 if (!Func->hasLocalLinkage()) {
4072 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
4073 }
4074 } else {
4075 upgradeDLLImportExportLinkage(Func, RawLinkage);
4076 }
4077
4078 if (Record.size() > 12) {
4079 if (unsigned ComdatID = Record[12]) {
4080 if (ComdatID > ComdatList.size())
4081 return error("Invalid function comdat ID");
4082 Func->setComdat(ComdatList[ComdatID - 1]);
4083 }
4084 } else if (hasImplicitComdat(RawLinkage)) {
4085 ImplicitComdatObjects.insert(Func);
4086 }
4087
4088 if (Record.size() > 13)
4089 OperandInfo.Prefix = Record[13];
4090
4091 if (Record.size() > 14)
4092 OperandInfo.PersonalityFn = Record[14];
4093
4094 if (Record.size() > 15) {
4095 Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
4096 }
4097 inferDSOLocal(Func);
4098
4099 // Record[16] is the address space number.
4100
4101 // Check whether we have enough values to read a partition name. Also make
4102 // sure Strtab has enough values.
4103 if (Record.size() > 18 && Strtab.data() &&
4104 Record[17] + Record[18] <= Strtab.size()) {
4105 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
4106 }
4107
4108 ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));
4109
4110 if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)
4111 FunctionOperands.push_back(OperandInfo);
4112
4113 // If this is a function with a body, remember the prototype we are
4114 // creating now, so that we can match up the body with them later.
4115 if (!isProto) {
4116 Func->setIsMaterializable(true);
4117 FunctionsWithBodies.push_back(Func);
4118 DeferredFunctionInfo[Func] = 0;
4119 }
4120 return Error::success();
4121}
4122
4123Error BitcodeReader::parseGlobalIndirectSymbolRecord(
4124 unsigned BitCode, ArrayRef<uint64_t> Record) {
4125 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
4126 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
4127 // dllstorageclass, threadlocal, unnamed_addr,
4128 // preemption specifier] (name in VST)
4129 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
4130 // visibility, dllstorageclass, threadlocal, unnamed_addr,
4131 // preemption specifier] (name in VST)
4132 // v2: [strtab_offset, strtab_size, v1]
4134 std::tie(Name, Record) = readNameFromStrtab(Record);
4135
4136 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
4137 if (Record.size() < (3 + (unsigned)NewRecord))
4138 return error("Invalid record");
4139 unsigned OpNum = 0;
4140 unsigned TypeID = Record[OpNum++];
4141 Type *Ty = getTypeByID(TypeID);
4142 if (!Ty)
4143 return error("Invalid record");
4144
4145 unsigned AddrSpace;
4146 if (!NewRecord) {
4147 auto *PTy = dyn_cast<PointerType>(Ty);
4148 if (!PTy)
4149 return error("Invalid type for value");
4150 AddrSpace = PTy->getAddressSpace();
4151 TypeID = getContainedTypeID(TypeID);
4152 Ty = getTypeByID(TypeID);
4153 if (!Ty)
4154 return error("Missing element type for old-style indirect symbol");
4155 } else {
4156 AddrSpace = Record[OpNum++];
4157 }
4158
4159 auto Val = Record[OpNum++];
4160 auto Linkage = Record[OpNum++];
4161 GlobalValue *NewGA;
4162 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4163 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
4164 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
4165 TheModule);
4166 else
4167 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
4168 nullptr, TheModule);
4169
4170 // Local linkage must have default visibility.
4171 // auto-upgrade `hidden` and `protected` for old bitcode.
4172 if (OpNum != Record.size()) {
4173 auto VisInd = OpNum++;
4174 if (!NewGA->hasLocalLinkage())
4175 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
4176 }
4177 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4178 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
4179 if (OpNum != Record.size()) {
4180 auto S = Record[OpNum++];
4181 // A GlobalValue with local linkage cannot have a DLL storage class.
4182 if (!NewGA->hasLocalLinkage())
4184 }
4185 else
4186 upgradeDLLImportExportLinkage(NewGA, Linkage);
4187 if (OpNum != Record.size())
4189 if (OpNum != Record.size())
4191 }
4192 if (OpNum != Record.size())
4193 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
4194 inferDSOLocal(NewGA);
4195
4196 // Check whether we have enough values to read a partition name.
4197 if (OpNum + 1 < Record.size()) {
4198 NewGA->setPartition(
4199 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
4200 OpNum += 2;
4201 }
4202
4203 ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID));
4204 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
4205 return Error::success();
4206}
4207
4208Error BitcodeReader::parseModule(uint64_t ResumeBit,
4209 bool ShouldLazyLoadMetadata,
4210 ParserCallbacks Callbacks) {
4211 this->ValueTypeCallback = std::move(Callbacks.ValueType);
4212 if (ResumeBit) {
4213 if (Error JumpFailed = Stream.JumpToBit(ResumeBit))
4214 return JumpFailed;
4215 } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
4216 return Err;
4217
4219
4220 // Parts of bitcode parsing depend on the datalayout. Make sure we
4221 // finalize the datalayout before we run any of that code.
4222 bool ResolvedDataLayout = false;
4223 // In order to support importing modules with illegal data layout strings,
4224 // delay parsing the data layout string until after upgrades and overrides
4225 // have been applied, allowing to fix illegal data layout strings.
4226 // Initialize to the current module's layout string in case none is specified.
4227 std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr();
4228
4229 auto ResolveDataLayout = [&]() -> Error {
4230 if (ResolvedDataLayout)
4231 return Error::success();
4232
4233 // Datalayout and triple can't be parsed after this point.
4234 ResolvedDataLayout = true;
4235
4236 // Auto-upgrade the layout string
4237 TentativeDataLayoutStr = llvm::UpgradeDataLayoutString(
4238 TentativeDataLayoutStr, TheModule->getTargetTriple());
4239
4240 // Apply override
4241 if (Callbacks.DataLayout) {
4242 if (auto LayoutOverride = (*Callbacks.DataLayout)(
4243 TheModule->getTargetTriple(), TentativeDataLayoutStr))
4244 TentativeDataLayoutStr = *LayoutOverride;
4245 }
4246
4247 // Now the layout string is finalized in TentativeDataLayoutStr. Parse it.
4248 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr);
4249 if (!MaybeDL)
4250 return MaybeDL.takeError();
4251
4252 TheModule->setDataLayout(MaybeDL.get());
4253 return Error::success();
4254 };
4255
4256 // Read all the records for this module.
4257 while (true) {
4258 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4259 if (!MaybeEntry)
4260 return MaybeEntry.takeError();
4261 llvm::BitstreamEntry Entry = MaybeEntry.get();
4262
4263 switch (Entry.Kind) {
4265 return error("Malformed block");
4267 if (Error Err = ResolveDataLayout())
4268 return Err;
4269 return globalCleanup();
4270
4272 switch (Entry.ID) {
4273 default: // Skip unknown content.
4274 if (Error Err = Stream.SkipBlock())
4275 return Err;
4276 break;
4278 if (Error Err = readBlockInfo())
4279 return Err;
4280 break;
4282 if (Error Err = parseAttributeBlock())
4283 return Err;
4284 break;
4286 if (Error Err = parseAttributeGroupBlock())
4287 return Err;
4288 break;
4290 if (Error Err = parseTypeTable())
4291 return Err;
4292 break;
4294 if (!SeenValueSymbolTable) {
4295 // Either this is an old form VST without function index and an
4296 // associated VST forward declaration record (which would have caused
4297 // the VST to be jumped to and parsed before it was encountered
4298 // normally in the stream), or there were no function blocks to
4299 // trigger an earlier parsing of the VST.
4300 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
4301 if (Error Err = parseValueSymbolTable())
4302 return Err;
4303 SeenValueSymbolTable = true;
4304 } else {
4305 // We must have had a VST forward declaration record, which caused
4306 // the parser to jump to and parse the VST earlier.
4307 assert(VSTOffset > 0);
4308 if (Error Err = Stream.SkipBlock())
4309 return Err;
4310 }
4311 break;
4313 if (Error Err = parseConstants())
4314 return Err;
4315 if (Error Err = resolveGlobalAndIndirectSymbolInits())
4316 return Err;
4317 break;
4319 if (ShouldLazyLoadMetadata) {
4320 if (Error Err = rememberAndSkipMetadata())
4321 return Err;
4322 break;
4323 }
4324 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
4325 if (Error Err = MDLoader->parseModuleMetadata())
4326 return Err;
4327 break;
4329 if (Error Err = MDLoader->parseMetadataKinds())
4330 return Err;
4331 break;
4333 if (Error Err = ResolveDataLayout())
4334 return Err;
4335
4336 // If this is the first function body we've seen, reverse the
4337 // FunctionsWithBodies list.
4338 if (!SeenFirstFunctionBody) {
4339 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
4340 if (Error Err = globalCleanup())
4341 return Err;
4342 SeenFirstFunctionBody = true;
4343 }
4344
4345 if (VSTOffset > 0) {
4346 // If we have a VST forward declaration record, make sure we
4347 // parse the VST now if we haven't already. It is needed to
4348 // set up the DeferredFunctionInfo vector for lazy reading.
4349 if (!SeenValueSymbolTable) {
4350 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
4351 return Err;
4352 SeenValueSymbolTable = true;
4353 // Fall through so that we record the NextUnreadBit below.
4354 // This is necessary in case we have an anonymous function that
4355 // is later materialized. Since it will not have a VST entry we
4356 // need to fall back to the lazy parse to find its offset.
4357 } else {
4358 // If we have a VST forward declaration record, but have already
4359 // parsed the VST (just above, when the first function body was
4360 // encountered here), then we are resuming the parse after
4361 // materializing functions. The ResumeBit points to the
4362 // start of the last function block recorded in the
4363 // DeferredFunctionInfo map. Skip it.
4364 if (Error Err = Stream.SkipBlock())
4365 return Err;
4366 continue;
4367 }
4368 }
4369
4370 // Support older bitcode files that did not have the function
4371 // index in the VST, nor a VST forward declaration record, as
4372 // well as anonymous functions that do not have VST entries.
4373 // Build the DeferredFunctionInfo vector on the fly.
4374 if (Error Err = rememberAndSkipFunctionBody())
4375 return Err;
4376
4377 // Suspend parsing when we reach the function bodies. Subsequent
4378 // materialization calls will resume it when necessary. If the bitcode
4379 // file is old, the symbol table will be at the end instead and will not
4380 // have been seen yet. In this case, just finish the parse now.
4381 if (SeenValueSymbolTable) {
4382 NextUnreadBit = Stream.GetCurrentBitNo();
4383 // After the VST has been parsed, we need to make sure intrinsic name
4384 // are auto-upgraded.
4385 return globalCleanup();
4386 }
4387 break;
4389 if (Error Err = parseUseLists())
4390 return Err;
4391 break;
4393 if (Error Err = parseOperandBundleTags())
4394 return Err;
4395 break;
4397 if (Error Err = parseSyncScopeNames())
4398 return Err;
4399 break;
4400 }
4401 continue;
4402
4404 // The interesting case.
4405 break;
4406 }
4407
4408 // Read a record.
4409 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
4410 if (!MaybeBitCode)
4411 return MaybeBitCode.takeError();
4412 switch (unsigned BitCode = MaybeBitCode.get()) {
4413 default: break; // Default behavior, ignore unknown content.
4415 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
4416 if (!VersionOrErr)
4417 return VersionOrErr.takeError();
4418 UseRelativeIDs = *VersionOrErr >= 1;
4419 break;
4420 }
4421 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
4422 if (ResolvedDataLayout)
4423 return error("target triple too late in module");
4424 std::string S;
4425 if (convertToString(Record, 0, S))
4426 return error("Invalid record");
4427 TheModule->setTargetTriple(S);
4428 break;
4429 }
4430 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
4431 if (ResolvedDataLayout)
4432 return error("datalayout too late in module");
4433 if (convertToString(Record, 0, TentativeDataLayoutStr))
4434 return error("Invalid record");
4435 break;
4436 }
4437 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
4438 std::string S;
4439 if (convertToString(Record, 0, S))
4440 return error("Invalid record");
4441 TheModule->setModuleInlineAsm(S);
4442 break;
4443 }
4444 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
4445 // Deprecated, but still needed to read old bitcode files.
4446 std::string S;
4447 if (convertToString(Record, 0, S))
4448 return error("Invalid record");
4449 // Ignore value.
4450 break;
4451 }
4452 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
4453 std::string S;
4454 if (convertToString(Record, 0, S))
4455 return error("Invalid record");
4456 SectionTable.push_back(S);
4457 break;
4458 }
4459 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
4460 std::string S;
4461 if (convertToString(Record, 0, S))
4462 return error("Invalid record");
4463 GCTable.push_back(S);
4464 break;
4465 }
4467 if (Error Err = parseComdatRecord(Record))
4468 return Err;
4469 break;
4470 // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC}
4471 // written by ThinLinkBitcodeWriter. See
4472 // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each
4473 // record
4474 // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714)
4476 if (Error Err = parseGlobalVarRecord(Record))
4477 return Err;
4478 break;
4480 if (Error Err = ResolveDataLayout())
4481 return Err;
4482 if (Error Err = parseFunctionRecord(Record))
4483 return Err;
4484 break;
4488 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
4489 return Err;
4490 break;
4491 /// MODULE_CODE_VSTOFFSET: [offset]
4493 if (Record.empty())
4494 return error("Invalid record");
4495 // Note that we subtract 1 here because the offset is relative to one word
4496 // before the start of the identification or module block, which was
4497 // historically always the start of the regular bitcode header.
4498 VSTOffset = Record[0] - 1;
4499 break;
4500 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4504 return error("Invalid record");
4505 TheModule->setSourceFileName(ValueName);
4506 break;
4507 }
4508 Record.clear();
4509 }
4510 this->ValueTypeCallback = std::nullopt;
4511 return Error::success();
4512}
4513
4514Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
4515 bool IsImporting,
4516 ParserCallbacks Callbacks) {
4517 TheModule = M;
4518 MetadataLoaderCallbacks MDCallbacks;
4519 MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); };
4520 MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) {
4521 return getContainedTypeID(I, J);
4522 };
4523 MDCallbacks.MDType = Callbacks.MDType;
4524 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks);
4525 return parseModule(0, ShouldLazyLoadMetadata, Callbacks);
4526}
4527
4528Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
4529 if (!isa<PointerType>(PtrType))
4530 return error("Load/Store operand is not a pointer type");
4531
4532 if (!cast<PointerType>(PtrType)->isOpaqueOrPointeeTypeMatches(ValType))
4533 return error("Explicit load/store type does not match pointee "
4534 "type of pointer operand");
4535 if (!PointerType::isLoadableOrStorableType(ValType))
4536 return error("Cannot load/store from pointer");
4537 return Error::success();
4538}
4539
4540Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
4541 ArrayRef<unsigned> ArgTyIDs) {
4543 for (unsigned i = 0; i != CB->arg_size(); ++i) {
4544 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4545 Attribute::InAlloca}) {
4546 if (!Attrs.hasParamAttr(i, Kind) ||
4547 Attrs.getParamAttr(i, Kind).getValueAsType())
4548 continue;
4549
4550 Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]);
4551 if (!PtrEltTy)
4552 return error("Missing element type for typed attribute upgrade");
4553
4554 Attribute NewAttr;
4555 switch (Kind) {
4556 case Attribute::ByVal:
4557 NewAttr = Attribute::getWithByValType(Context, PtrEltTy);
4558 break;
4559 case Attribute::StructRet:
4560 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy);
4561 break;
4562 case Attribute::InAlloca:
4563 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy);
4564 break;
4565 default:
4566 llvm_unreachable("not an upgraded type attribute");
4567 }
4568
4569 Attrs = Attrs.addParamAttribute(Context, i, NewAttr);
4570 }
4571 }
4572
4573 if (CB->isInlineAsm()) {
4574 const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
4575 unsigned ArgNo = 0;
4576 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
4577 if (!CI.hasArg())
4578 continue;
4579
4580 if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) {
4581 Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4582 if (!ElemTy)
4583 return error("Missing element type for inline asm upgrade");
4584 Attrs = Attrs.addParamAttribute(
4585 Context, ArgNo,
4586 Attribute::get(Context, Attribute::ElementType, ElemTy));
4587 }
4588
4589 ArgNo++;
4590 }
4591 }
4592
4593 switch (CB->getIntrinsicID()) {
4594 case Intrinsic::preserve_array_access_index:
4595 case Intrinsic::preserve_struct_access_index:
4596 case Intrinsic::aarch64_ldaxr:
4597 case Intrinsic::aarch64_ldxr:
4598 case Intrinsic::aarch64_stlxr:
4599 case Intrinsic::aarch64_stxr:
4600 case Intrinsic::arm_ldaex:
4601 case Intrinsic::arm_ldrex:
4602 case Intrinsic::arm_stlex:
4603 case Intrinsic::arm_strex: {
4604 unsigned ArgNo;
4605 switch (CB->getIntrinsicID()) {
4606 case Intrinsic::aarch64_stlxr:
4607 case Intrinsic::aarch64_stxr:
4608 case Intrinsic::arm_stlex:
4609 case Intrinsic::arm_strex:
4610 ArgNo = 1;
4611 break;
4612 default:
4613 ArgNo = 0;
4614 break;
4615 }
4616 if (!Attrs.getParamElementType(ArgNo)) {
4617 Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]);
4618 if (!ElTy)
4619 return error("Missing element type for elementtype upgrade");
4620 Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy);
4621 Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr);
4622 }
4623 break;
4624 }
4625 default:
4626 break;
4627 }
4628
4629 CB->setAttributes(Attrs);
4630 return Error::success();
4631}
4632
4633/// Lazily parse the specified function body block.
4634Error BitcodeReader::parseFunctionBody(Function *F) {
4635 if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
4636 return Err;
4637
4638 // Unexpected unresolved metadata when parsing function.
4639 if (MDLoader->hasFwdRefs())
4640 return error("Invalid function metadata: incoming forward references");
4641
4642 InstructionList.clear();
4643 unsigned ModuleValueListSize = ValueList.size();
4644 unsigned ModuleMDLoaderSize = MDLoader->size();
4645
4646 // Add all the function arguments to the value table.
4647 unsigned ArgNo = 0;
4648 unsigned FTyID = FunctionTypeIDs[F];
4649 for (Argument &I : F->args()) {
4650 unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1);
4651 assert(I.getType() == getTypeByID(ArgTyID) &&
4652 "Incorrect fully specified type for Function Argument");
4653 ValueList.push_back(&I, ArgTyID);
4654 ++ArgNo;
4655 }
4656 unsigned NextValueNo = ValueList.size();
4657 BasicBlock *CurBB = nullptr;
4658 unsigned CurBBNo = 0;
4659 // Block into which constant expressions from phi nodes are materialized.
4660 BasicBlock *PhiConstExprBB = nullptr;
4661 // Edge blocks for phi nodes into which constant expressions have been
4662 // expanded.
4664 ConstExprEdgeBBs;
4665
4666 DebugLoc LastLoc;
4667 auto getLastInstruction = [&]() -> Instruction * {
4668 if (CurBB && !CurBB->empty())
4669 return &CurBB->back();
4670 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4671 !FunctionBBs[CurBBNo - 1]->empty())
4672 return &FunctionBBs[CurBBNo - 1]->back();
4673 return nullptr;
4674 };
4675
4676 std::vector<OperandBundleDef> OperandBundles;
4677
4678 // Read all the records.
4680
4681 while (true) {
4682 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4683 if (!MaybeEntry)
4684 return MaybeEntry.takeError();
4685 llvm::BitstreamEntry Entry = MaybeEntry.get();
4686
4687 switch (Entry.Kind) {
4689 return error("Malformed block");
4691 goto OutOfRecordLoop;
4692
4694 switch (Entry.ID) {
4695 default: // Skip unknown content.
4696 if (Error Err = Stream.SkipBlock())
4697 return Err;
4698 break;
4700 if (Error Err = parseConstants())
4701 return Err;
4702 NextValueNo = ValueList.size();
4703 break;
4705 if (Error Err = parseValueSymbolTable())
4706 return Err;
4707 break;
4709 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
4710 return Err;
4711 break;
4713 assert(DeferredMetadataInfo.empty() &&
4714 "Must read all module-level metadata before function-level");
4715 if (Error Err = MDLoader->parseFunctionMetadata())
4716 return Err;
4717 break;
4719 if (Error Err = parseUseLists())
4720 return Err;
4721 break;
4722 }
4723 continue;
4724
4726 // The interesting case.
4727 break;
4728 }
4729
4730 // Read a record.
4731 Record.clear();
4732 Instruction *I = nullptr;
4733 unsigned ResTypeID = InvalidTypeID;
4734 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
4735 if (!MaybeBitCode)
4736 return MaybeBitCode.takeError();
4737 switch (unsigned BitCode = MaybeBitCode.get()) {
4738 default: // Default behavior: reject
4739 return error("Invalid value");
4740 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
4741 if (Record.empty() || Record[0] == 0)
4742 return error("Invalid record");
4743 // Create all the basic blocks for the function.
4744 FunctionBBs.resize(Record[0]);
4745
4746 // See if anything took the address of blocks in this function.
4747 auto BBFRI = BasicBlockFwdRefs.find(F);
4748 if (BBFRI == BasicBlockFwdRefs.end()) {
4749 for (BasicBlock *&BB : FunctionBBs)
4750 BB = BasicBlock::Create(Context, "", F);
4751 } else {
4752 auto &BBRefs = BBFRI->second;
4753 // Check for invalid basic block references.
4754 if (BBRefs.size() > FunctionBBs.size())
4755 return error("Invalid ID");
4756 assert(!BBRefs.empty() && "Unexpected empty array");
4757 assert(!BBRefs.front() && "Invalid reference to entry block");
4758 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4759 ++I)
4760 if (I < RE && BBRefs[I]) {
4761 BBRefs[I]->insertInto(F);
4762 FunctionBBs[I] = BBRefs[I];
4763 } else {
4764 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4765 }
4766
4767 // Erase from the table.
4768 BasicBlockFwdRefs.erase(BBFRI);
4769 }
4770
4771 CurBB = FunctionBBs[0];
4772 continue;
4773 }
4774
4775 case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...]
4776 // The record should not be emitted if it's an empty list.
4777 if (Record.empty())
4778 return error("Invalid record");
4779 // When we have the RARE case of a BlockAddress Constant that is not
4780 // scoped to the Function it refers to, we need to conservatively
4781 // materialize the referred to Function, regardless of whether or not
4782 // that Function will ultimately be linked, otherwise users of
4783 // BitcodeReader might start splicing out Function bodies such that we
4784 // might no longer be able to materialize the BlockAddress since the
4785 // BasicBlock (and entire body of the Function) the BlockAddress refers
4786 // to may have been moved. In the case that the user of BitcodeReader
4787 // decides ultimately not to link the Function body, materializing here
4788 // could be considered wasteful, but it's better than a deserialization
4789 // failure as described. This keeps BitcodeReader unaware of complex
4790 // linkage policy decisions such as those use by LTO, leaving those
4791 // decisions "one layer up."
4792 for (uint64_t ValID : Record)
4793 if (auto *F = dyn_cast<Function>(ValueList[ValID]))
4794 BackwardRefFunctions.push_back(F);
4795 else
4796 return error("Invalid record");
4797
4798 continue;
4799
4800 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
4801 // This record indicates that the last instruction is at the same
4802 // location as the previous instruction with a location.
4803 I = getLastInstruction();
4804
4805 if (!I)
4806 return error("Invalid record");
4807 I->setDebugLoc(LastLoc);
4808 I = nullptr;
4809 continue;
4810
4811 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
4812 I = getLastInstruction();
4813 if (!I || Record.size() < 4)
4814 return error("Invalid record");
4815
4816 unsigned Line = Record[0], Col = Record[1];
4817 unsigned ScopeID = Record[2], IAID = Record[3];
4818 bool isImplicitCode = Record.size() == 5 && Record[4];
4819
4820 MDNode *Scope = nullptr, *IA = nullptr;
4821 if (ScopeID) {
4822 Scope = dyn_cast_or_null<MDNode>(
4823 MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));
4824 if (!Scope)
4825 return error("Invalid record");
4826 }
4827 if (IAID) {
4828 IA = dyn_cast_or_null<MDNode>(
4829 MDLoader->getMetadataFwdRefOrLoad(IAID - 1));
4830 if (!IA)
4831 return error("Invalid record");
4832 }
4833 LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA,
4834 isImplicitCode);
4835 I->setDebugLoc(LastLoc);
4836 I = nullptr;
4837 continue;
4838 }
4839 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
4840 unsigned OpNum = 0;
4841 Value *LHS;
4842 unsigned TypeID;
4843 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) ||
4844 OpNum+1 > Record.size())
4845 return error("Invalid record");
4846
4847 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
4848 if (Opc == -1)
4849 return error("Invalid record");
4851 ResTypeID = TypeID;
4852 InstructionList.push_back(I);
4853 if (OpNum < Record.size()) {
4854 if (isa<FPMathOperator>(I)) {
4856 if (FMF.any())
4857 I->setFastMathFlags(FMF);
4858 }
4859 }
4860 break;
4861 }
4862 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
4863 unsigned OpNum = 0;
4864 Value *LHS, *RHS;
4865 unsigned TypeID;
4866 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) ||
4867 popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS,
4868 CurBB) ||
4869 OpNum+1 > Record.size())
4870 return error("Invalid record");
4871
4872 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
4873 if (Opc == -1)
4874 return error("Invalid record");
4876 ResTypeID = TypeID;
4877 InstructionList.push_back(I);
4878 if (OpNum < Record.size()) {
4879 if (Opc == Instruction::Add ||
4880 Opc == Instruction::Sub ||
4881 Opc == Instruction::Mul ||
4882 Opc == Instruction::Shl) {
4883 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
4884 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
4885 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
4886 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
4887 } else if (Opc == Instruction::SDiv ||
4888 Opc == Instruction::UDiv ||
4889 Opc == Instruction::LShr ||
4890 Opc == Instruction::AShr) {
4891 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
4892 cas