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