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