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