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