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
9#include "llvm/Bitcode/BitcodeReader.h"
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"
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/Bitcode/BitcodeCommon.h"
22#include "llvm/Bitcode/LLVMBitCodes.h"
23#include "llvm/Bitstream/BitstreamReader.h"
24#include "llvm/Config/llvm-config.h"
25#include "llvm/IR/Argument.h"
26#include "llvm/IR/AttributeMask.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/ConstantRangeList.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/DebugInfo.h"
37#include "llvm/IR/DebugInfoMetadata.h"
38#include "llvm/IR/DebugLoc.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GVMaterializer.h"
42#include "llvm/IR/GetElementPtrTypeIterator.h"
43#include "llvm/IR/GlobalAlias.h"
44#include "llvm/IR/GlobalIFunc.h"
45#include "llvm/IR/GlobalObject.h"
46#include "llvm/IR/GlobalValue.h"
47#include "llvm/IR/GlobalVariable.h"
48#include "llvm/IR/InlineAsm.h"
49#include "llvm/IR/InstIterator.h"
50#include "llvm/IR/InstrTypes.h"
51#include "llvm/IR/Instruction.h"
52#include "llvm/IR/Instructions.h"
53#include "llvm/IR/Intrinsics.h"
54#include "llvm/IR/IntrinsicsAArch64.h"
55#include "llvm/IR/IntrinsicsARM.h"
56#include "llvm/IR/LLVMContext.h"
57#include "llvm/IR/Metadata.h"
58#include "llvm/IR/Module.h"
59#include "llvm/IR/ModuleSummaryIndex.h"
60#include "llvm/IR/Operator.h"
61#include "llvm/IR/ProfDataUtils.h"
62#include "llvm/IR/Type.h"
63#include "llvm/IR/Value.h"
64#include "llvm/IR/Verifier.h"
65#include "llvm/Support/AtomicOrdering.h"
66#include "llvm/Support/Casting.h"
67#include "llvm/Support/CommandLine.h"
68#include "llvm/Support/Compiler.h"
69#include "llvm/Support/Debug.h"
70#include "llvm/Support/Error.h"
71#include "llvm/Support/ErrorHandling.h"
72#include "llvm/Support/ErrorOr.h"
73#include "llvm/Support/MathExtras.h"
74#include "llvm/Support/MemoryBuffer.h"
75#include "llvm/Support/ModRef.h"
76#include "llvm/Support/raw_ostream.h"
77#include "llvm/TargetParser/Triple.h"
78#include <algorithm>
79#include <cassert>
80#include <cstddef>
81#include <cstdint>
82#include <deque>
83#include <map>
84#include <memory>
85#include <optional>
86#include <string>
87#include <system_error>
88#include <tuple>
89#include <utility>
90#include <vector>
91
92using namespace llvm;
93
94static cl::opt<bool> PrintSummaryGUIDs(
95 "print-summary-global-ids", cl::init(Val: false), cl::Hidden,
96 cl::desc(
97 "Print the global id for each value when reading the module summary"));
98
99static cl::opt<bool> ExpandConstantExprs(
100 "expand-constant-exprs", cl::Hidden,
101 cl::desc(
102 "Expand constant expressions to instructions for testing purposes"));
103
104namespace {
105
106enum {
107 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
108};
109
110} // end anonymous namespace
111
112static Error error(const Twine &Message) {
113 return make_error<StringError>(
114 Args: Message, Args: make_error_code(E: BitcodeError::CorruptedBitcode));
115}
116
117static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) {
118 if (!Stream.canSkipToPos(pos: 4))
119 return createStringError(EC: std::errc::illegal_byte_sequence,
120 Fmt: "file too small to contain bitcode header");
121 for (unsigned C : {'B', 'C'})
122 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
123 if (Res.get() != C)
124 return createStringError(EC: std::errc::illegal_byte_sequence,
125 Fmt: "file doesn't start with bitcode header");
126 } else
127 return Res.takeError();
128 for (unsigned C : {0x0, 0xC, 0xE, 0xD})
129 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 4)) {
130 if (Res.get() != C)
131 return createStringError(EC: std::errc::illegal_byte_sequence,
132 Fmt: "file doesn't start with bitcode header");
133 } else
134 return Res.takeError();
135 return Error::success();
136}
137
138static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) {
139 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
140 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
141
142 if (Buffer.getBufferSize() & 3)
143 return error(Message: "Invalid bitcode signature");
144
145 // If we have a wrapper header, parse it and ignore the non-bc file contents.
146 // The magic number is 0x0B17C0DE stored in little endian.
147 if (isBitcodeWrapper(BufPtr, BufEnd))
148 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, VerifyBufferSize: true))
149 return error(Message: "Invalid bitcode wrapper header");
150
151 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
152 if (Error Err = hasInvalidBitcodeHeader(Stream))
153 return std::move(Err);
154
155 return std::move(Stream);
156}
157
158/// Convert a string from a record into an std::string, return true on failure.
159template <typename StrTy>
160static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
161 StrTy &Result) {
162 if (Idx > Record.size())
163 return true;
164
165 Result.append(Record.begin() + Idx, Record.end());
166 return false;
167}
168
169// Strip all the TBAA attachment for the module.
170static void stripTBAA(Module *M) {
171 for (auto &F : *M) {
172 if (F.isMaterializable())
173 continue;
174 for (auto &I : instructions(F))
175 I.setMetadata(KindID: LLVMContext::MD_tbaa, Node: nullptr);
176 }
177}
178
179/// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
180/// "epoch" encoded in the bitcode, and return the producer name if any.
181static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
182 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::IDENTIFICATION_BLOCK_ID))
183 return std::move(Err);
184
185 // Read all the records.
186 SmallVector<uint64_t, 64> Record;
187
188 std::string ProducerIdentification;
189
190 while (true) {
191 BitstreamEntry Entry;
192 if (Error E = Stream.advance().moveInto(Value&: Entry))
193 return std::move(E);
194
195 switch (Entry.Kind) {
196 default:
197 case BitstreamEntry::Error:
198 return error(Message: "Malformed block");
199 case BitstreamEntry::EndBlock:
200 return ProducerIdentification;
201 case BitstreamEntry::Record:
202 // The interesting case.
203 break;
204 }
205
206 // Read a record.
207 Record.clear();
208 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
209 if (!MaybeBitCode)
210 return MaybeBitCode.takeError();
211 switch (MaybeBitCode.get()) {
212 default: // Default behavior: reject
213 return error(Message: "Invalid value");
214 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
215 convertToString(Record, Idx: 0, Result&: ProducerIdentification);
216 break;
217 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
218 unsigned epoch = (unsigned)Record[0];
219 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
220 return error(
221 Message: Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
222 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
223 }
224 }
225 }
226 }
227}
228
229static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
230 // We expect a number of well-defined blocks, though we don't necessarily
231 // need to understand them all.
232 while (true) {
233 if (Stream.AtEndOfStream())
234 return "";
235
236 BitstreamEntry Entry;
237 if (Error E = Stream.advance().moveInto(Value&: Entry))
238 return std::move(E);
239
240 switch (Entry.Kind) {
241 case BitstreamEntry::EndBlock:
242 case BitstreamEntry::Error:
243 return error(Message: "Malformed block");
244
245 case BitstreamEntry::SubBlock:
246 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
247 return readIdentificationBlock(Stream);
248
249 // Ignore other sub-blocks.
250 if (Error Err = Stream.SkipBlock())
251 return std::move(Err);
252 continue;
253 case BitstreamEntry::Record:
254 if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError())
255 return std::move(E);
256 continue;
257 }
258 }
259}
260
261static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
262 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID))
263 return std::move(Err);
264
265 SmallVector<uint64_t, 64> Record;
266 // Read all the records for this module.
267
268 while (true) {
269 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
270 if (!MaybeEntry)
271 return MaybeEntry.takeError();
272 BitstreamEntry Entry = MaybeEntry.get();
273
274 switch (Entry.Kind) {
275 case BitstreamEntry::SubBlock: // Handled for us already.
276 case BitstreamEntry::Error:
277 return error(Message: "Malformed block");
278 case BitstreamEntry::EndBlock:
279 return false;
280 case BitstreamEntry::Record:
281 // The interesting case.
282 break;
283 }
284
285 // Read a record.
286 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
287 if (!MaybeRecord)
288 return MaybeRecord.takeError();
289 switch (MaybeRecord.get()) {
290 default:
291 break; // Default behavior, ignore unknown content.
292 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
293 std::string S;
294 if (convertToString(Record, Idx: 0, Result&: S))
295 return error(Message: "Invalid section name record");
296
297 // Check for the i386 and other (x86_64, ARM) conventions
298
299 auto [Segment, Section] = StringRef(S).split(Separator: ",");
300 Segment = Segment.trim();
301 Section = Section.trim();
302
303 if (Segment == "__DATA" && Section.starts_with(Prefix: "__objc_catlist"))
304 return true;
305 if (Segment == "__OBJC" && Section.starts_with(Prefix: "__category"))
306 return true;
307 if (Segment == "__TEXT" && Section.starts_with(Prefix: "__swift"))
308 return true;
309 break;
310 }
311 }
312 Record.clear();
313 }
314 llvm_unreachable("Exit infinite loop");
315}
316
317static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
318 // We expect a number of well-defined blocks, though we don't necessarily
319 // need to understand them all.
320 while (true) {
321 BitstreamEntry Entry;
322 if (Error E = Stream.advance().moveInto(Value&: Entry))
323 return std::move(E);
324
325 switch (Entry.Kind) {
326 case BitstreamEntry::Error:
327 return error(Message: "Malformed block");
328 case BitstreamEntry::EndBlock:
329 return false;
330
331 case BitstreamEntry::SubBlock:
332 if (Entry.ID == bitc::MODULE_BLOCK_ID)
333 return hasObjCCategoryInModule(Stream);
334
335 // Ignore other sub-blocks.
336 if (Error Err = Stream.SkipBlock())
337 return std::move(Err);
338 continue;
339
340 case BitstreamEntry::Record:
341 if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError())
342 return std::move(E);
343 continue;
344 }
345 }
346}
347
348static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) {
349 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID))
350 return std::move(Err);
351
352 SmallVector<uint64_t, 64> Record;
353
354 std::string Triple;
355
356 // Read all the records for this module.
357 while (true) {
358 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
359 if (!MaybeEntry)
360 return MaybeEntry.takeError();
361 BitstreamEntry Entry = MaybeEntry.get();
362
363 switch (Entry.Kind) {
364 case BitstreamEntry::SubBlock: // Handled for us already.
365 case BitstreamEntry::Error:
366 return error(Message: "Malformed block");
367 case BitstreamEntry::EndBlock:
368 return Triple;
369 case BitstreamEntry::Record:
370 // The interesting case.
371 break;
372 }
373
374 // Read a record.
375 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
376 if (!MaybeRecord)
377 return MaybeRecord.takeError();
378 switch (MaybeRecord.get()) {
379 default: break; // Default behavior, ignore unknown content.
380 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
381 std::string S;
382 if (convertToString(Record, Idx: 0, Result&: S))
383 return error(Message: "Invalid triple record");
384 Triple = S;
385 break;
386 }
387 }
388 Record.clear();
389 }
390 llvm_unreachable("Exit infinite loop");
391}
392
393static Expected<std::string> readTriple(BitstreamCursor &Stream) {
394 // We expect a number of well-defined blocks, though we don't necessarily
395 // need to understand them all.
396 while (true) {
397 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
398 if (!MaybeEntry)
399 return MaybeEntry.takeError();
400 BitstreamEntry Entry = MaybeEntry.get();
401
402 switch (Entry.Kind) {
403 case BitstreamEntry::Error:
404 return error(Message: "Malformed block");
405 case BitstreamEntry::EndBlock:
406 return "";
407
408 case BitstreamEntry::SubBlock:
409 if (Entry.ID == bitc::MODULE_BLOCK_ID)
410 return readModuleTriple(Stream);
411
412 // Ignore other sub-blocks.
413 if (Error Err = Stream.SkipBlock())
414 return std::move(Err);
415 continue;
416
417 case BitstreamEntry::Record:
418 if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(AbbrevID: Entry.ID))
419 continue;
420 else
421 return Skipped.takeError();
422 }
423 }
424}
425
426namespace {
427
428class BitcodeReaderBase {
429protected:
430 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
431 : Stream(std::move(Stream)), Strtab(Strtab) {
432 this->Stream.setBlockInfo(&BlockInfo);
433 }
434
435 BitstreamBlockInfo BlockInfo;
436 BitstreamCursor Stream;
437 StringRef Strtab;
438
439 /// In version 2 of the bitcode we store names of global values and comdats in
440 /// a string table rather than in the VST.
441 bool UseStrtab = false;
442
443 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
444
445 /// If this module uses a string table, pop the reference to the string table
446 /// and return the referenced string and the rest of the record. Otherwise
447 /// just return the record itself.
448 std::pair<StringRef, ArrayRef<uint64_t>>
449 readNameFromStrtab(ArrayRef<uint64_t> Record);
450
451 Error readBlockInfo();
452
453 // Contains an arbitrary and optional string identifying the bitcode producer
454 std::string ProducerIdentification;
455
456 Error error(const Twine &Message);
457};
458
459} // end anonymous namespace
460
461Error BitcodeReaderBase::error(const Twine &Message) {
462 std::string FullMsg = Message.str();
463 if (!ProducerIdentification.empty())
464 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
465 LLVM_VERSION_STRING "')";
466 return ::error(Message: FullMsg);
467}
468
469Expected<unsigned>
470BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
471 if (Record.empty())
472 return error(Message: "Invalid version record");
473 unsigned ModuleVersion = Record[0];
474 if (ModuleVersion > 2)
475 return error(Message: "Invalid value");
476 UseStrtab = ModuleVersion >= 2;
477 return ModuleVersion;
478}
479
480std::pair<StringRef, ArrayRef<uint64_t>>
481BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
482 if (!UseStrtab)
483 return {"", Record};
484 // Invalid reference. Let the caller complain about the record being empty.
485 if (Record[0] + Record[1] > Strtab.size())
486 return {"", {}};
487 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(N: 2)};
488}
489
490namespace {
491
492/// This represents a constant expression or constant aggregate using a custom
493/// structure internal to the bitcode reader. Later, this structure will be
494/// expanded by materializeValue() either into a constant expression/aggregate,
495/// or into an instruction sequence at the point of use. This allows us to
496/// upgrade bitcode using constant expressions even if this kind of constant
497/// expression is no longer supported.
498class BitcodeConstant final : public Value,
499 TrailingObjects<BitcodeConstant, unsigned> {
500 friend TrailingObjects;
501
502 // Value subclass ID: Pick largest possible value to avoid any clashes.
503 static constexpr uint8_t SubclassID = 255;
504
505public:
506 // Opcodes used for non-expressions. This includes constant aggregates
507 // (struct, array, vector) that might need expansion, as well as non-leaf
508 // constants that don't need expansion (no_cfi, dso_local, blockaddress),
509 // but still go through BitcodeConstant to avoid different uselist orders
510 // between the two cases.
511 static constexpr uint8_t ConstantStructOpcode = 255;
512 static constexpr uint8_t ConstantArrayOpcode = 254;
513 static constexpr uint8_t ConstantVectorOpcode = 253;
514 static constexpr uint8_t NoCFIOpcode = 252;
515 static constexpr uint8_t DSOLocalEquivalentOpcode = 251;
516 static constexpr uint8_t BlockAddressOpcode = 250;
517 static constexpr uint8_t ConstantPtrAuthOpcode = 249;
518 static constexpr uint8_t FirstSpecialOpcode = ConstantPtrAuthOpcode;
519
520 // Separate struct to make passing different number of parameters to
521 // BitcodeConstant::create() more convenient.
522 struct ExtraInfo {
523 uint8_t Opcode;
524 uint8_t Flags;
525 unsigned BlockAddressBB = 0;
526 Type *SrcElemTy = nullptr;
527 std::optional<ConstantRange> InRange;
528
529 ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, Type *SrcElemTy = nullptr,
530 std::optional<ConstantRange> InRange = std::nullopt)
531 : Opcode(Opcode), Flags(Flags), SrcElemTy(SrcElemTy),
532 InRange(std::move(InRange)) {}
533
534 ExtraInfo(uint8_t Opcode, uint8_t Flags, unsigned BlockAddressBB)
535 : Opcode(Opcode), Flags(Flags), BlockAddressBB(BlockAddressBB) {}
536 };
537
538 uint8_t Opcode;
539 uint8_t Flags;
540 unsigned NumOperands;
541 unsigned BlockAddressBB;
542 Type *SrcElemTy; // GEP source element type.
543 std::optional<ConstantRange> InRange; // GEP inrange attribute.
544
545private:
546 BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs)
547 : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags),
548 NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB),
549 SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) {
550 llvm::uninitialized_copy(Src&: OpIDs, Dst: getTrailingObjects());
551 }
552
553 BitcodeConstant &operator=(const BitcodeConstant &) = delete;
554
555public:
556 static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty,
557 const ExtraInfo &Info,
558 ArrayRef<unsigned> OpIDs) {
559 void *Mem = A.Allocate(Size: totalSizeToAlloc<unsigned>(Counts: OpIDs.size()),
560 Alignment: alignof(BitcodeConstant));
561 return new (Mem) BitcodeConstant(Ty, Info, OpIDs);
562 }
563
564 static bool classof(const Value *V) { return V->getValueID() == SubclassID; }
565
566 ArrayRef<unsigned> getOperandIDs() const {
567 return ArrayRef(getTrailingObjects(), NumOperands);
568 }
569
570 std::optional<ConstantRange> getInRange() const {
571 assert(Opcode == Instruction::GetElementPtr);
572 return InRange;
573 }
574
575 const char *getOpcodeName() const {
576 return Instruction::getOpcodeName(Opcode);
577 }
578};
579
580class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
581 LLVMContext &Context;
582 Module *TheModule = nullptr;
583 // Next offset to start scanning for lazy parsing of function bodies.
584 uint64_t NextUnreadBit = 0;
585 // Last function offset found in the VST.
586 uint64_t LastFunctionBlockBit = 0;
587 bool SeenValueSymbolTable = false;
588 uint64_t VSTOffset = 0;
589
590 std::vector<std::string> SectionTable;
591 std::vector<std::string> GCTable;
592
593 std::vector<Type *> TypeList;
594 /// Track type IDs of contained types. Order is the same as the contained
595 /// types of a Type*. This is used during upgrades of typed pointer IR in
596 /// opaque pointer mode.
597 DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs;
598 /// In some cases, we need to create a type ID for a type that was not
599 /// explicitly encoded in the bitcode, or we don't know about at the current
600 /// point. For example, a global may explicitly encode the value type ID, but
601 /// not have a type ID for the pointer to value type, for which we create a
602 /// virtual type ID instead. This map stores the new type ID that was created
603 /// for the given pair of Type and contained type ID.
604 DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs;
605 DenseMap<Function *, unsigned> FunctionTypeIDs;
606 /// Allocator for BitcodeConstants. This should come before ValueList,
607 /// because the ValueList might hold ValueHandles to these constants, so
608 /// ValueList must be destroyed before Alloc.
609 BumpPtrAllocator Alloc;
610 BitcodeReaderValueList ValueList;
611 std::optional<MetadataLoader> MDLoader;
612 std::vector<Comdat *> ComdatList;
613 DenseSet<GlobalObject *> ImplicitComdatObjects;
614 SmallVector<Instruction *, 64> InstructionList;
615
616 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
617 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits;
618
619 struct FunctionOperandInfo {
620 Function *F;
621 unsigned PersonalityFn;
622 unsigned Prefix;
623 unsigned Prologue;
624 };
625 std::vector<FunctionOperandInfo> FunctionOperands;
626
627 /// The set of attributes by index. Index zero in the file is for null, and
628 /// is thus not represented here. As such all indices are off by one.
629 std::vector<AttributeList> MAttributes;
630
631 /// The set of attribute groups.
632 std::map<unsigned, AttributeList> MAttributeGroups;
633
634 /// While parsing a function body, this is a list of the basic blocks for the
635 /// function.
636 std::vector<BasicBlock*> FunctionBBs;
637
638 // When reading the module header, this list is populated with functions that
639 // have bodies later in the file.
640 std::vector<Function*> FunctionsWithBodies;
641
642 // When intrinsic functions are encountered which require upgrading they are
643 // stored here with their replacement function.
644 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
645 UpdatedIntrinsicMap UpgradedIntrinsics;
646
647 // Several operations happen after the module header has been read, but
648 // before function bodies are processed. This keeps track of whether
649 // we've done this yet.
650 bool SeenFirstFunctionBody = false;
651
652 /// When function bodies are initially scanned, this map contains info about
653 /// where to find deferred function body in the stream.
654 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
655
656 /// When Metadata block is initially scanned when parsing the module, we may
657 /// choose to defer parsing of the metadata. This vector contains info about
658 /// which Metadata blocks are deferred.
659 std::vector<uint64_t> DeferredMetadataInfo;
660
661 /// These are basic blocks forward-referenced by block addresses. They are
662 /// inserted lazily into functions when they're loaded. The basic block ID is
663 /// its index into the vector.
664 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
665 std::deque<Function *> BasicBlockFwdRefQueue;
666
667 /// These are Functions that contain BlockAddresses which refer a different
668 /// Function. When parsing the different Function, queue Functions that refer
669 /// to the different Function. Those Functions must be materialized in order
670 /// to resolve their BlockAddress constants before the different Function
671 /// gets moved into another Module.
672 std::vector<Function *> BackwardRefFunctions;
673
674 /// Indicates that we are using a new encoding for instruction operands where
675 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
676 /// instruction number, for a more compact encoding. Some instruction
677 /// operands are not relative to the instruction ID: basic block numbers, and
678 /// types. Once the old style function blocks have been phased out, we would
679 /// not need this flag.
680 bool UseRelativeIDs = false;
681
682 /// True if all functions will be materialized, negating the need to process
683 /// (e.g.) blockaddress forward references.
684 bool WillMaterializeAllForwardRefs = false;
685
686 /// Tracks whether we have seen debug intrinsics or records in this bitcode;
687 /// seeing both in a single module is currently a fatal error.
688 bool SeenDebugIntrinsic = false;
689 bool SeenDebugRecord = false;
690
691 bool StripDebugInfo = false;
692 TBAAVerifier TBAAVerifyHelper;
693
694 std::vector<std::string> BundleTags;
695 SmallVector<SyncScope::ID, 8> SSIDs;
696
697 std::optional<ValueTypeCallbackTy> ValueTypeCallback;
698
699public:
700 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
701 StringRef ProducerIdentification, LLVMContext &Context);
702
703 Error materializeForwardReferencedFunctions();
704
705 Error materialize(GlobalValue *GV) override;
706 Error materializeModule() override;
707 std::vector<StructType *> getIdentifiedStructTypes() const override;
708
709 /// Main interface to parsing a bitcode buffer.
710 /// \returns true if an error occurred.
711 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
712 bool IsImporting, ParserCallbacks Callbacks = {});
713
714 static uint64_t decodeSignRotatedValue(uint64_t V);
715
716 /// Materialize any deferred Metadata block.
717 Error materializeMetadata() override;
718
719 void setStripDebugInfo() override;
720
721private:
722 std::vector<StructType *> IdentifiedStructTypes;
723 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
724 StructType *createIdentifiedStructType(LLVMContext &Context);
725
726 static constexpr unsigned InvalidTypeID = ~0u;
727
728 Type *getTypeByID(unsigned ID);
729 Type *getPtrElementTypeByID(unsigned ID);
730 unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0);
731 unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {});
732
733 void callValueTypeCallback(Value *F, unsigned TypeID);
734 Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB);
735 Expected<Constant *> getValueForInitializer(unsigned ID);
736
737 Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID,
738 BasicBlock *ConstExprInsertBB) {
739 if (Ty && Ty->isMetadataTy())
740 return MetadataAsValue::get(Context&: Ty->getContext(), MD: getFnMetadataByID(ID));
741 return ValueList.getValueFwdRef(Idx: ID, Ty, TyID, ConstExprInsertBB);
742 }
743
744 Metadata *getFnMetadataByID(unsigned ID) {
745 return MDLoader->getMetadataFwdRefOrLoad(Idx: ID);
746 }
747
748 BasicBlock *getBasicBlock(unsigned ID) const {
749 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
750 return FunctionBBs[ID];
751 }
752
753 AttributeList getAttributes(unsigned i) const {
754 if (i-1 < MAttributes.size())
755 return MAttributes[i-1];
756 return AttributeList();
757 }
758
759 /// Read a value/type pair out of the specified record from slot 'Slot'.
760 /// Increment Slot past the number of slots used in the record. Return true on
761 /// failure.
762 bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
763 unsigned InstNum, Value *&ResVal, unsigned &TypeID,
764 BasicBlock *ConstExprInsertBB) {
765 if (Slot == Record.size()) return true;
766 unsigned ValNo = (unsigned)Record[Slot++];
767 // Adjust the ValNo, if it was encoded relative to the InstNum.
768 if (UseRelativeIDs)
769 ValNo = InstNum - ValNo;
770 if (ValNo < InstNum) {
771 // If this is not a forward reference, just return the value we already
772 // have.
773 TypeID = ValueList.getTypeID(ValNo);
774 ResVal = getFnValueByID(ID: ValNo, Ty: nullptr, TyID: TypeID, ConstExprInsertBB);
775 assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) &&
776 "Incorrect type ID stored for value");
777 return ResVal == nullptr;
778 }
779 if (Slot == Record.size())
780 return true;
781
782 TypeID = (unsigned)Record[Slot++];
783 ResVal = getFnValueByID(ID: ValNo, Ty: getTypeByID(ID: TypeID), TyID: TypeID,
784 ConstExprInsertBB);
785 return ResVal == nullptr;
786 }
787
788 bool getValueOrMetadata(const SmallVectorImpl<uint64_t> &Record,
789 unsigned &Slot, unsigned InstNum, Value *&ResVal,
790 BasicBlock *ConstExprInsertBB) {
791 if (Slot == Record.size())
792 return true;
793 unsigned ValID = Record[Slot++];
794 if (ValID != static_cast<unsigned>(bitc::OB_METADATA)) {
795 unsigned TypeId;
796 return getValueTypePair(Record, Slot&: --Slot, InstNum, ResVal, TypeID&: TypeId,
797 ConstExprInsertBB);
798 }
799 if (Slot == Record.size())
800 return true;
801 unsigned ValNo = InstNum - (unsigned)Record[Slot++];
802 ResVal = MetadataAsValue::get(Context, MD: getFnMetadataByID(ID: ValNo));
803 return false;
804 }
805
806 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
807 /// past the number of slots used by the value in the record. Return true if
808 /// there is an error.
809 bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
810 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,
811 BasicBlock *ConstExprInsertBB) {
812 if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB))
813 return true;
814 // All values currently take a single record slot.
815 ++Slot;
816 return false;
817 }
818
819 /// Like popValue, but does not increment the Slot number.
820 bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
821 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal,
822 BasicBlock *ConstExprInsertBB) {
823 ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB);
824 return ResVal == nullptr;
825 }
826
827 /// Version of getValue that returns ResVal directly, or 0 if there is an
828 /// error.
829 Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
830 unsigned InstNum, Type *Ty, unsigned TyID,
831 BasicBlock *ConstExprInsertBB) {
832 if (Slot == Record.size()) return nullptr;
833 unsigned ValNo = (unsigned)Record[Slot];
834 // Adjust the ValNo, if it was encoded relative to the InstNum.
835 if (UseRelativeIDs)
836 ValNo = InstNum - ValNo;
837 return getFnValueByID(ID: ValNo, Ty, TyID, ConstExprInsertBB);
838 }
839
840 /// Like getValue, but decodes signed VBRs.
841 Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
842 unsigned InstNum, Type *Ty, unsigned TyID,
843 BasicBlock *ConstExprInsertBB) {
844 if (Slot == Record.size()) return nullptr;
845 unsigned ValNo = (unsigned)decodeSignRotatedValue(V: Record[Slot]);
846 // Adjust the ValNo, if it was encoded relative to the InstNum.
847 if (UseRelativeIDs)
848 ValNo = InstNum - ValNo;
849 return getFnValueByID(ID: ValNo, Ty, TyID, ConstExprInsertBB);
850 }
851
852 Expected<ConstantRange> readConstantRange(ArrayRef<uint64_t> Record,
853 unsigned &OpNum,
854 unsigned BitWidth) {
855 if (Record.size() - OpNum < 2)
856 return error(Message: "Too few records for range");
857 if (BitWidth > 64) {
858 unsigned LowerActiveWords = Record[OpNum];
859 unsigned UpperActiveWords = Record[OpNum++] >> 32;
860 if (Record.size() - OpNum < LowerActiveWords + UpperActiveWords)
861 return error(Message: "Too few records for range");
862 APInt Lower =
863 readWideAPInt(Vals: ArrayRef(&Record[OpNum], LowerActiveWords), TypeBits: BitWidth);
864 OpNum += LowerActiveWords;
865 APInt Upper =
866 readWideAPInt(Vals: ArrayRef(&Record[OpNum], UpperActiveWords), TypeBits: BitWidth);
867 OpNum += UpperActiveWords;
868 return ConstantRange(Lower, Upper);
869 } else {
870 int64_t Start = BitcodeReader::decodeSignRotatedValue(V: Record[OpNum++]);
871 int64_t End = BitcodeReader::decodeSignRotatedValue(V: Record[OpNum++]);
872 return ConstantRange(APInt(BitWidth, Start, true),
873 APInt(BitWidth, End, true));
874 }
875 }
876
877 Expected<ConstantRange>
878 readBitWidthAndConstantRange(ArrayRef<uint64_t> Record, unsigned &OpNum) {
879 if (Record.size() - OpNum < 1)
880 return error(Message: "Too few records for range");
881 unsigned BitWidth = Record[OpNum++];
882 return readConstantRange(Record, OpNum, BitWidth);
883 }
884
885 /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the
886 /// corresponding argument's pointee type. Also upgrades intrinsics that now
887 /// require an elementtype attribute.
888 Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys);
889
890 /// Converts alignment exponent (i.e. power of two (or zero)) to the
891 /// corresponding alignment to use. If alignment is too large, returns
892 /// a corresponding error code.
893 Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment);
894 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
895 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false,
896 ParserCallbacks Callbacks = {});
897
898 Error parseComdatRecord(ArrayRef<uint64_t> Record);
899 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
900 Error parseFunctionRecord(ArrayRef<uint64_t> Record);
901 Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
902 ArrayRef<uint64_t> Record);
903
904 Error parseAttributeBlock();
905 Error parseAttributeGroupBlock();
906 Error parseTypeTable();
907 Error parseTypeTableBody();
908 Error parseOperandBundleTags();
909 Error parseSyncScopeNames();
910
911 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
912 unsigned NameIndex, Triple &TT);
913 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
914 ArrayRef<uint64_t> Record);
915 Error parseValueSymbolTable(uint64_t Offset = 0);
916 Error parseGlobalValueSymbolTable();
917 Error parseConstants();
918 Error rememberAndSkipFunctionBodies();
919 Error rememberAndSkipFunctionBody();
920 /// Save the positions of the Metadata blocks and skip parsing the blocks.
921 Error rememberAndSkipMetadata();
922 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
923 Error parseFunctionBody(Function *F);
924 Error globalCleanup();
925 Error resolveGlobalAndIndirectSymbolInits();
926 Error parseUseLists();
927 Error findFunctionInStream(
928 Function *F,
929 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
930
931 SyncScope::ID getDecodedSyncScopeID(unsigned Val);
932};
933
934/// Class to manage reading and parsing function summary index bitcode
935/// files/sections.
936class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
937 /// The module index built during parsing.
938 ModuleSummaryIndex &TheIndex;
939
940 /// Indicates whether we have encountered a global value summary section
941 /// yet during parsing.
942 bool SeenGlobalValSummary = false;
943
944 /// Indicates whether we have already parsed the VST, used for error checking.
945 bool SeenValueSymbolTable = false;
946
947 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
948 /// Used to enable on-demand parsing of the VST.
949 uint64_t VSTOffset = 0;
950
951 // Map to save ValueId to ValueInfo association that was recorded in the
952 // ValueSymbolTable. It is used after the VST is parsed to convert
953 // call graph edges read from the function summary from referencing
954 // callees by their ValueId to using the ValueInfo instead, which is how
955 // they are recorded in the summary index being built.
956 // We save a GUID which refers to the same global as the ValueInfo, but
957 // ignoring the linkage, i.e. for values other than local linkage they are
958 // identical (this is the second member). ValueInfo has the real GUID.
959 DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>>
960 ValueIdToValueInfoMap;
961
962 /// Map populated during module path string table parsing, from the
963 /// module ID to a string reference owned by the index's module
964 /// path string table, used to correlate with combined index
965 /// summary records.
966 DenseMap<uint64_t, StringRef> ModuleIdMap;
967
968 /// Original source file name recorded in a bitcode record.
969 std::string SourceFileName;
970
971 /// The string identifier given to this module by the client, normally the
972 /// path to the bitcode file.
973 StringRef ModulePath;
974
975 /// Callback to ask whether a symbol is the prevailing copy when invoked
976 /// during combined index building.
977 std::function<bool(GlobalValue::GUID)> IsPrevailing;
978
979 /// Saves the stack ids from the STACK_IDS record to consult when adding stack
980 /// ids from the lists in the callsite and alloc entries to the index.
981 std::vector<uint64_t> StackIds;
982
983 /// Linearized radix tree of allocation contexts. See the description above
984 /// the CallStackRadixTreeBuilder class in ProfileData/MemProf.h for format.
985 std::vector<uint64_t> RadixArray;
986
987public:
988 ModuleSummaryIndexBitcodeReader(
989 BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex,
990 StringRef ModulePath,
991 std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr);
992
993 Error parseModule();
994
995private:
996 void setValueGUID(uint64_t ValueID, StringRef ValueName,
997 GlobalValue::LinkageTypes Linkage,
998 StringRef SourceFileName);
999 Error parseValueSymbolTable(
1000 uint64_t Offset,
1001 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
1002 SmallVector<ValueInfo, 0> makeRefList(ArrayRef<uint64_t> Record);
1003 SmallVector<FunctionSummary::EdgeTy, 0>
1004 makeCallList(ArrayRef<uint64_t> Record, bool IsOldProfileFormat,
1005 bool HasProfile, bool HasRelBF);
1006 Error parseEntireSummary(unsigned ID);
1007 Error parseModuleStringTable();
1008 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
1009 void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,
1010 TypeIdCompatibleVtableInfo &TypeId);
1011 std::vector<FunctionSummary::ParamAccess>
1012 parseParamAccesses(ArrayRef<uint64_t> Record);
1013 SmallVector<unsigned> parseAllocInfoContext(ArrayRef<uint64_t> Record,
1014 unsigned &I);
1015
1016 template <bool AllowNullValueInfo = false>
1017 std::pair<ValueInfo, GlobalValue::GUID>
1018 getValueInfoFromValueId(unsigned ValueId);
1019
1020 void addThisModule();
1021 ModuleSummaryIndex::ModuleInfo *getThisModule();
1022};
1023
1024} // end anonymous namespace
1025
1026std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx,
1027 Error Err) {
1028 if (Err) {
1029 std::error_code EC;
1030 handleAllErrors(E: std::move(Err), Handlers: [&](ErrorInfoBase &EIB) {
1031 EC = EIB.convertToErrorCode();
1032 Ctx.emitError(ErrorStr: EIB.message());
1033 });
1034 return EC;
1035 }
1036 return std::error_code();
1037}
1038
1039BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
1040 StringRef ProducerIdentification,
1041 LLVMContext &Context)
1042 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
1043 ValueList(this->Stream.SizeInBytes(),
1044 [this](unsigned ValID, BasicBlock *InsertBB) {
1045 return materializeValue(ValID, InsertBB);
1046 }) {
1047 this->ProducerIdentification = std::string(ProducerIdentification);
1048}
1049
1050Error BitcodeReader::materializeForwardReferencedFunctions() {
1051 if (WillMaterializeAllForwardRefs)
1052 return Error::success();
1053
1054 // Prevent recursion.
1055 WillMaterializeAllForwardRefs = true;
1056
1057 while (!BasicBlockFwdRefQueue.empty()) {
1058 Function *F = BasicBlockFwdRefQueue.front();
1059 BasicBlockFwdRefQueue.pop_front();
1060 assert(F && "Expected valid function");
1061 if (!BasicBlockFwdRefs.count(Val: F))
1062 // Already materialized.
1063 continue;
1064
1065 // Check for a function that isn't materializable to prevent an infinite
1066 // loop. When parsing a blockaddress stored in a global variable, there
1067 // isn't a trivial way to check if a function will have a body without a
1068 // linear search through FunctionsWithBodies, so just check it here.
1069 if (!F->isMaterializable())
1070 return error(Message: "Never resolved function from blockaddress");
1071
1072 // Try to materialize F.
1073 if (Error Err = materialize(GV: F))
1074 return Err;
1075 }
1076 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
1077
1078 for (Function *F : BackwardRefFunctions)
1079 if (Error Err = materialize(GV: F))
1080 return Err;
1081 BackwardRefFunctions.clear();
1082
1083 // Reset state.
1084 WillMaterializeAllForwardRefs = false;
1085 return Error::success();
1086}
1087
1088//===----------------------------------------------------------------------===//
1089// Helper functions to implement forward reference resolution, etc.
1090//===----------------------------------------------------------------------===//
1091
1092static bool hasImplicitComdat(size_t Val) {
1093 switch (Val) {
1094 default:
1095 return false;
1096 case 1: // Old WeakAnyLinkage
1097 case 4: // Old LinkOnceAnyLinkage
1098 case 10: // Old WeakODRLinkage
1099 case 11: // Old LinkOnceODRLinkage
1100 return true;
1101 }
1102}
1103
1104static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
1105 switch (Val) {
1106 default: // Map unknown/new linkages to external
1107 case 0:
1108 return GlobalValue::ExternalLinkage;
1109 case 2:
1110 return GlobalValue::AppendingLinkage;
1111 case 3:
1112 return GlobalValue::InternalLinkage;
1113 case 5:
1114 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
1115 case 6:
1116 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
1117 case 7:
1118 return GlobalValue::ExternalWeakLinkage;
1119 case 8:
1120 return GlobalValue::CommonLinkage;
1121 case 9:
1122 return GlobalValue::PrivateLinkage;
1123 case 12:
1124 return GlobalValue::AvailableExternallyLinkage;
1125 case 13:
1126 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
1127 case 14:
1128 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
1129 case 15:
1130 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
1131 case 1: // Old value with implicit comdat.
1132 case 16:
1133 return GlobalValue::WeakAnyLinkage;
1134 case 10: // Old value with implicit comdat.
1135 case 17:
1136 return GlobalValue::WeakODRLinkage;
1137 case 4: // Old value with implicit comdat.
1138 case 18:
1139 return GlobalValue::LinkOnceAnyLinkage;
1140 case 11: // Old value with implicit comdat.
1141 case 19:
1142 return GlobalValue::LinkOnceODRLinkage;
1143 }
1144}
1145
1146static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) {
1147 FunctionSummary::FFlags Flags;
1148 Flags.ReadNone = RawFlags & 0x1;
1149 Flags.ReadOnly = (RawFlags >> 1) & 0x1;
1150 Flags.NoRecurse = (RawFlags >> 2) & 0x1;
1151 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
1152 Flags.NoInline = (RawFlags >> 4) & 0x1;
1153 Flags.AlwaysInline = (RawFlags >> 5) & 0x1;
1154 Flags.NoUnwind = (RawFlags >> 6) & 0x1;
1155 Flags.MayThrow = (RawFlags >> 7) & 0x1;
1156 Flags.HasUnknownCall = (RawFlags >> 8) & 0x1;
1157 Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1;
1158 return Flags;
1159}
1160
1161// Decode the flags for GlobalValue in the summary. The bits for each attribute:
1162//
1163// linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7,
1164// visibility: [8, 10).
1165static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
1166 uint64_t Version) {
1167 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
1168 // like getDecodedLinkage() above. Any future change to the linkage enum and
1169 // to getDecodedLinkage() will need to be taken into account here as above.
1170 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
1171 auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits
1172 auto IK = GlobalValueSummary::ImportKind((RawFlags >> 10) & 1); // 1 bit
1173 RawFlags = RawFlags >> 4;
1174 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
1175 // The Live flag wasn't introduced until version 3. For dead stripping
1176 // to work correctly on earlier versions, we must conservatively treat all
1177 // values as live.
1178 bool Live = (RawFlags & 0x2) || Version < 3;
1179 bool Local = (RawFlags & 0x4);
1180 bool AutoHide = (RawFlags & 0x8);
1181
1182 return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport,
1183 Live, Local, AutoHide, IK);
1184}
1185
1186// Decode the flags for GlobalVariable in the summary
1187static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) {
1188 return GlobalVarSummary::GVarFlags(
1189 (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false,
1190 (RawFlags & 0x4) ? true : false,
1191 (GlobalObject::VCallVisibility)(RawFlags >> 3));
1192}
1193
1194static std::pair<CalleeInfo::HotnessType, bool>
1195getDecodedHotnessCallEdgeInfo(uint64_t RawFlags) {
1196 CalleeInfo::HotnessType Hotness =
1197 static_cast<CalleeInfo::HotnessType>(RawFlags & 0x7); // 3 bits
1198 bool HasTailCall = (RawFlags & 0x8); // 1 bit
1199 return {Hotness, HasTailCall};
1200}
1201
1202// Deprecated, but still needed to read old bitcode files.
1203static void getDecodedRelBFCallEdgeInfo(uint64_t RawFlags, uint64_t &RelBF,
1204 bool &HasTailCall) {
1205 static constexpr unsigned RelBlockFreqBits = 28;
1206 static constexpr uint64_t RelBlockFreqMask = (1 << RelBlockFreqBits) - 1;
1207 RelBF = RawFlags & RelBlockFreqMask; // RelBlockFreqBits bits
1208 HasTailCall = (RawFlags & (1 << RelBlockFreqBits)); // 1 bit
1209}
1210
1211static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
1212 switch (Val) {
1213 default: // Map unknown visibilities to default.
1214 case 0: return GlobalValue::DefaultVisibility;
1215 case 1: return GlobalValue::HiddenVisibility;
1216 case 2: return GlobalValue::ProtectedVisibility;
1217 }
1218}
1219
1220static GlobalValue::DLLStorageClassTypes
1221getDecodedDLLStorageClass(unsigned Val) {
1222 switch (Val) {
1223 default: // Map unknown values to default.
1224 case 0: return GlobalValue::DefaultStorageClass;
1225 case 1: return GlobalValue::DLLImportStorageClass;
1226 case 2: return GlobalValue::DLLExportStorageClass;
1227 }
1228}
1229
1230static bool getDecodedDSOLocal(unsigned Val) {
1231 switch(Val) {
1232 default: // Map unknown values to preemptable.
1233 case 0: return false;
1234 case 1: return true;
1235 }
1236}
1237
1238static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) {
1239 switch (Val) {
1240 case 1:
1241 return CodeModel::Tiny;
1242 case 2:
1243 return CodeModel::Small;
1244 case 3:
1245 return CodeModel::Kernel;
1246 case 4:
1247 return CodeModel::Medium;
1248 case 5:
1249 return CodeModel::Large;
1250 }
1251
1252 return {};
1253}
1254
1255static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
1256 switch (Val) {
1257 case 0: return GlobalVariable::NotThreadLocal;
1258 default: // Map unknown non-zero value to general dynamic.
1259 case 1: return GlobalVariable::GeneralDynamicTLSModel;
1260 case 2: return GlobalVariable::LocalDynamicTLSModel;
1261 case 3: return GlobalVariable::InitialExecTLSModel;
1262 case 4: return GlobalVariable::LocalExecTLSModel;
1263 }
1264}
1265
1266static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
1267 switch (Val) {
1268 default: // Map unknown to UnnamedAddr::None.
1269 case 0: return GlobalVariable::UnnamedAddr::None;
1270 case 1: return GlobalVariable::UnnamedAddr::Global;
1271 case 2: return GlobalVariable::UnnamedAddr::Local;
1272 }
1273}
1274
1275static int getDecodedCastOpcode(unsigned Val) {
1276 switch (Val) {
1277 default: return -1;
1278 case bitc::CAST_TRUNC : return Instruction::Trunc;
1279 case bitc::CAST_ZEXT : return Instruction::ZExt;
1280 case bitc::CAST_SEXT : return Instruction::SExt;
1281 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
1282 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
1283 case bitc::CAST_UITOFP : return Instruction::UIToFP;
1284 case bitc::CAST_SITOFP : return Instruction::SIToFP;
1285 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
1286 case bitc::CAST_FPEXT : return Instruction::FPExt;
1287 case bitc::CAST_PTRTOADDR: return Instruction::PtrToAddr;
1288 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
1289 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
1290 case bitc::CAST_BITCAST : return Instruction::BitCast;
1291 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
1292 }
1293}
1294
1295static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
1296 bool IsFP = Ty->isFPOrFPVectorTy();
1297 // UnOps are only valid for int/fp or vector of int/fp types
1298 if (!IsFP && !Ty->isIntOrIntVectorTy())
1299 return -1;
1300
1301 switch (Val) {
1302 default:
1303 return -1;
1304 case bitc::UNOP_FNEG:
1305 return IsFP ? Instruction::FNeg : -1;
1306 }
1307}
1308
1309static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
1310 bool IsFP = Ty->isFPOrFPVectorTy();
1311 // BinOps are only valid for int/fp or vector of int/fp types
1312 if (!IsFP && !Ty->isIntOrIntVectorTy())
1313 return -1;
1314
1315 switch (Val) {
1316 default:
1317 return -1;
1318 case bitc::BINOP_ADD:
1319 return IsFP ? Instruction::FAdd : Instruction::Add;
1320 case bitc::BINOP_SUB:
1321 return IsFP ? Instruction::FSub : Instruction::Sub;
1322 case bitc::BINOP_MUL:
1323 return IsFP ? Instruction::FMul : Instruction::Mul;
1324 case bitc::BINOP_UDIV:
1325 return IsFP ? -1 : Instruction::UDiv;
1326 case bitc::BINOP_SDIV:
1327 return IsFP ? Instruction::FDiv : Instruction::SDiv;
1328 case bitc::BINOP_UREM:
1329 return IsFP ? -1 : Instruction::URem;
1330 case bitc::BINOP_SREM:
1331 return IsFP ? Instruction::FRem : Instruction::SRem;
1332 case bitc::BINOP_SHL:
1333 return IsFP ? -1 : Instruction::Shl;
1334 case bitc::BINOP_LSHR:
1335 return IsFP ? -1 : Instruction::LShr;
1336 case bitc::BINOP_ASHR:
1337 return IsFP ? -1 : Instruction::AShr;
1338 case bitc::BINOP_AND:
1339 return IsFP ? -1 : Instruction::And;
1340 case bitc::BINOP_OR:
1341 return IsFP ? -1 : Instruction::Or;
1342 case bitc::BINOP_XOR:
1343 return IsFP ? -1 : Instruction::Xor;
1344 }
1345}
1346
1347static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
1348 switch (Val) {
1349 default: return AtomicRMWInst::BAD_BINOP;
1350 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
1351 case bitc::RMW_ADD: return AtomicRMWInst::Add;
1352 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
1353 case bitc::RMW_AND: return AtomicRMWInst::And;
1354 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
1355 case bitc::RMW_OR: return AtomicRMWInst::Or;
1356 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
1357 case bitc::RMW_MAX: return AtomicRMWInst::Max;
1358 case bitc::RMW_MIN: return AtomicRMWInst::Min;
1359 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
1360 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
1361 case bitc::RMW_FADD: return AtomicRMWInst::FAdd;
1362 case bitc::RMW_FSUB: return AtomicRMWInst::FSub;
1363 case bitc::RMW_FMAX: return AtomicRMWInst::FMax;
1364 case bitc::RMW_FMIN: return AtomicRMWInst::FMin;
1365 case bitc::RMW_FMAXIMUM:
1366 return AtomicRMWInst::FMaximum;
1367 case bitc::RMW_FMINIMUM:
1368 return AtomicRMWInst::FMinimum;
1369 case bitc::RMW_UINC_WRAP:
1370 return AtomicRMWInst::UIncWrap;
1371 case bitc::RMW_UDEC_WRAP:
1372 return AtomicRMWInst::UDecWrap;
1373 case bitc::RMW_USUB_COND:
1374 return AtomicRMWInst::USubCond;
1375 case bitc::RMW_USUB_SAT:
1376 return AtomicRMWInst::USubSat;
1377 }
1378}
1379
1380static AtomicOrdering getDecodedOrdering(unsigned Val) {
1381 switch (Val) {
1382 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
1383 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
1384 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
1385 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
1386 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
1387 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
1388 default: // Map unknown orderings to sequentially-consistent.
1389 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1390 }
1391}
1392
1393static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
1394 switch (Val) {
1395 default: // Map unknown selection kinds to any.
1396 case bitc::COMDAT_SELECTION_KIND_ANY:
1397 return Comdat::Any;
1398 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
1399 return Comdat::ExactMatch;
1400 case bitc::COMDAT_SELECTION_KIND_LARGEST:
1401 return Comdat::Largest;
1402 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
1403 return Comdat::NoDeduplicate;
1404 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
1405 return Comdat::SameSize;
1406 }
1407}
1408
1409static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
1410 FastMathFlags FMF;
1411 if (0 != (Val & bitc::UnsafeAlgebra))
1412 FMF.setFast();
1413 if (0 != (Val & bitc::AllowReassoc))
1414 FMF.setAllowReassoc();
1415 if (0 != (Val & bitc::NoNaNs))
1416 FMF.setNoNaNs();
1417 if (0 != (Val & bitc::NoInfs))
1418 FMF.setNoInfs();
1419 if (0 != (Val & bitc::NoSignedZeros))
1420 FMF.setNoSignedZeros();
1421 if (0 != (Val & bitc::AllowReciprocal))
1422 FMF.setAllowReciprocal();
1423 if (0 != (Val & bitc::AllowContract))
1424 FMF.setAllowContract(true);
1425 if (0 != (Val & bitc::ApproxFunc))
1426 FMF.setApproxFunc();
1427 return FMF;
1428}
1429
1430static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1431 // A GlobalValue with local linkage cannot have a DLL storage class.
1432 if (GV->hasLocalLinkage())
1433 return;
1434 switch (Val) {
1435 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
1436 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
1437 }
1438}
1439
1440Type *BitcodeReader::getTypeByID(unsigned ID) {
1441 // The type table size is always specified correctly.
1442 if (ID >= TypeList.size())
1443 return nullptr;
1444
1445 if (Type *Ty = TypeList[ID])
1446 return Ty;
1447
1448 // If we have a forward reference, the only possible case is when it is to a
1449 // named struct. Just create a placeholder for now.
1450 return TypeList[ID] = createIdentifiedStructType(Context);
1451}
1452
1453unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) {
1454 auto It = ContainedTypeIDs.find(Val: ID);
1455 if (It == ContainedTypeIDs.end())
1456 return InvalidTypeID;
1457
1458 if (Idx >= It->second.size())
1459 return InvalidTypeID;
1460
1461 return It->second[Idx];
1462}
1463
1464Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) {
1465 if (ID >= TypeList.size())
1466 return nullptr;
1467
1468 Type *Ty = TypeList[ID];
1469 if (!Ty->isPointerTy())
1470 return nullptr;
1471
1472 return getTypeByID(ID: getContainedTypeID(ID, Idx: 0));
1473}
1474
1475unsigned BitcodeReader::getVirtualTypeID(Type *Ty,
1476 ArrayRef<unsigned> ChildTypeIDs) {
1477 unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0];
1478 auto CacheKey = std::make_pair(x&: Ty, y&: ChildTypeID);
1479 auto It = VirtualTypeIDs.find(Val: CacheKey);
1480 if (It != VirtualTypeIDs.end()) {
1481 // The cmpxchg return value is the only place we need more than one
1482 // contained type ID, however the second one will always be the same (i1),
1483 // so we don't need to include it in the cache key. This asserts that the
1484 // contained types are indeed as expected and there are no collisions.
1485 assert((ChildTypeIDs.empty() ||
1486 ContainedTypeIDs[It->second] == ChildTypeIDs) &&
1487 "Incorrect cached contained type IDs");
1488 return It->second;
1489 }
1490
1491 unsigned TypeID = TypeList.size();
1492 TypeList.push_back(x: Ty);
1493 if (!ChildTypeIDs.empty())
1494 append_range(C&: ContainedTypeIDs[TypeID], R&: ChildTypeIDs);
1495 VirtualTypeIDs.insert(KV: {CacheKey, TypeID});
1496 return TypeID;
1497}
1498
1499static GEPNoWrapFlags toGEPNoWrapFlags(uint64_t Flags) {
1500 GEPNoWrapFlags NW;
1501 if (Flags & (1 << bitc::GEP_INBOUNDS))
1502 NW |= GEPNoWrapFlags::inBounds();
1503 if (Flags & (1 << bitc::GEP_NUSW))
1504 NW |= GEPNoWrapFlags::noUnsignedSignedWrap();
1505 if (Flags & (1 << bitc::GEP_NUW))
1506 NW |= GEPNoWrapFlags::noUnsignedWrap();
1507 return NW;
1508}
1509
1510static bool isConstExprSupported(const BitcodeConstant *BC) {
1511 uint8_t Opcode = BC->Opcode;
1512
1513 // These are not real constant expressions, always consider them supported.
1514 if (Opcode >= BitcodeConstant::FirstSpecialOpcode)
1515 return true;
1516
1517 // If -expand-constant-exprs is set, we want to consider all expressions
1518 // as unsupported.
1519 if (ExpandConstantExprs)
1520 return false;
1521
1522 if (Instruction::isBinaryOp(Opcode))
1523 return ConstantExpr::isSupportedBinOp(Opcode);
1524
1525 if (Instruction::isCast(Opcode))
1526 return ConstantExpr::isSupportedCastOp(Opcode);
1527
1528 if (Opcode == Instruction::GetElementPtr)
1529 return ConstantExpr::isSupportedGetElementPtr(SrcElemTy: BC->SrcElemTy);
1530
1531 switch (Opcode) {
1532 case Instruction::FNeg:
1533 case Instruction::Select:
1534 case Instruction::ICmp:
1535 case Instruction::FCmp:
1536 return false;
1537 default:
1538 return true;
1539 }
1540}
1541
1542Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
1543 BasicBlock *InsertBB) {
1544 // Quickly handle the case where there is no BitcodeConstant to resolve.
1545 if (StartValID < ValueList.size() && ValueList[StartValID] &&
1546 !isa<BitcodeConstant>(Val: ValueList[StartValID]))
1547 return ValueList[StartValID];
1548
1549 SmallDenseMap<unsigned, Value *> MaterializedValues;
1550 SmallVector<unsigned> Worklist;
1551 Worklist.push_back(Elt: StartValID);
1552 while (!Worklist.empty()) {
1553 unsigned ValID = Worklist.back();
1554 if (MaterializedValues.count(Val: ValID)) {
1555 // Duplicate expression that was already handled.
1556 Worklist.pop_back();
1557 continue;
1558 }
1559
1560 if (ValID >= ValueList.size() || !ValueList[ValID])
1561 return error(Message: "Invalid value ID");
1562
1563 Value *V = ValueList[ValID];
1564 auto *BC = dyn_cast<BitcodeConstant>(Val: V);
1565 if (!BC) {
1566 MaterializedValues.insert(KV: {ValID, V});
1567 Worklist.pop_back();
1568 continue;
1569 }
1570
1571 // Iterate in reverse, so values will get popped from the worklist in
1572 // expected order.
1573 SmallVector<Value *> Ops;
1574 for (unsigned OpID : reverse(C: BC->getOperandIDs())) {
1575 auto It = MaterializedValues.find(Val: OpID);
1576 if (It != MaterializedValues.end())
1577 Ops.push_back(Elt: It->second);
1578 else
1579 Worklist.push_back(Elt: OpID);
1580 }
1581
1582 // Some expressions have not been resolved yet, handle them first and then
1583 // revisit this one.
1584 if (Ops.size() != BC->getOperandIDs().size())
1585 continue;
1586 std::reverse(first: Ops.begin(), last: Ops.end());
1587
1588 SmallVector<Constant *> ConstOps;
1589 for (Value *Op : Ops)
1590 if (auto *C = dyn_cast<Constant>(Val: Op))
1591 ConstOps.push_back(Elt: C);
1592
1593 // Materialize as constant expression if possible.
1594 if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) {
1595 Constant *C;
1596 if (Instruction::isCast(Opcode: BC->Opcode)) {
1597 C = UpgradeBitCastExpr(Opc: BC->Opcode, C: ConstOps[0], DestTy: BC->getType());
1598 if (!C)
1599 C = ConstantExpr::getCast(ops: BC->Opcode, C: ConstOps[0], Ty: BC->getType());
1600 } else if (Instruction::isBinaryOp(Opcode: BC->Opcode)) {
1601 C = ConstantExpr::get(Opcode: BC->Opcode, C1: ConstOps[0], C2: ConstOps[1], Flags: BC->Flags);
1602 } else {
1603 switch (BC->Opcode) {
1604 case BitcodeConstant::ConstantPtrAuthOpcode: {
1605 auto *Key = dyn_cast<ConstantInt>(Val: ConstOps[1]);
1606 if (!Key)
1607 return error(Message: "ptrauth key operand must be ConstantInt");
1608
1609 auto *Disc = dyn_cast<ConstantInt>(Val: ConstOps[2]);
1610 if (!Disc)
1611 return error(Message: "ptrauth disc operand must be ConstantInt");
1612
1613 Constant *DeactivationSymbol =
1614 ConstOps.size() > 4 ? ConstOps[4]
1615 : ConstantPointerNull::get(T: cast<PointerType>(
1616 Val: ConstOps[3]->getType()));
1617 if (!DeactivationSymbol->getType()->isPointerTy())
1618 return error(
1619 Message: "ptrauth deactivation symbol operand must be a pointer");
1620
1621 C = ConstantPtrAuth::get(Ptr: ConstOps[0], Key, Disc, AddrDisc: ConstOps[3],
1622 DeactivationSymbol);
1623 break;
1624 }
1625 case BitcodeConstant::NoCFIOpcode: {
1626 auto *GV = dyn_cast<GlobalValue>(Val: ConstOps[0]);
1627 if (!GV)
1628 return error(Message: "no_cfi operand must be GlobalValue");
1629 C = NoCFIValue::get(GV);
1630 break;
1631 }
1632 case BitcodeConstant::DSOLocalEquivalentOpcode: {
1633 auto *GV = dyn_cast<GlobalValue>(Val: ConstOps[0]);
1634 if (!GV)
1635 return error(Message: "dso_local operand must be GlobalValue");
1636 C = DSOLocalEquivalent::get(GV);
1637 break;
1638 }
1639 case BitcodeConstant::BlockAddressOpcode: {
1640 Function *Fn = dyn_cast<Function>(Val: ConstOps[0]);
1641 if (!Fn)
1642 return error(Message: "blockaddress operand must be a function");
1643
1644 // If the function is already parsed we can insert the block address
1645 // right away.
1646 BasicBlock *BB;
1647 unsigned BBID = BC->BlockAddressBB;
1648 if (!BBID)
1649 // Invalid reference to entry block.
1650 return error(Message: "Invalid ID");
1651 if (!Fn->empty()) {
1652 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
1653 for (size_t I = 0, E = BBID; I != E; ++I) {
1654 if (BBI == BBE)
1655 return error(Message: "Invalid ID");
1656 ++BBI;
1657 }
1658 BB = &*BBI;
1659 } else {
1660 // Otherwise insert a placeholder and remember it so it can be
1661 // inserted when the function is parsed.
1662 auto &FwdBBs = BasicBlockFwdRefs[Fn];
1663 if (FwdBBs.empty())
1664 BasicBlockFwdRefQueue.push_back(x: Fn);
1665 if (FwdBBs.size() < BBID + 1)
1666 FwdBBs.resize(new_size: BBID + 1);
1667 if (!FwdBBs[BBID])
1668 FwdBBs[BBID] = BasicBlock::Create(Context);
1669 BB = FwdBBs[BBID];
1670 }
1671 C = BlockAddress::get(Ty: Fn->getType(), BB);
1672 break;
1673 }
1674 case BitcodeConstant::ConstantStructOpcode: {
1675 auto *ST = cast<StructType>(Val: BC->getType());
1676 if (ST->getNumElements() != ConstOps.size())
1677 return error(Message: "Invalid number of elements in struct initializer");
1678
1679 for (const auto [Ty, Op] : zip(t: ST->elements(), u&: ConstOps))
1680 if (Op->getType() != Ty)
1681 return error(Message: "Incorrect type in struct initializer");
1682
1683 C = ConstantStruct::get(T: ST, V: ConstOps);
1684 break;
1685 }
1686 case BitcodeConstant::ConstantArrayOpcode: {
1687 auto *AT = cast<ArrayType>(Val: BC->getType());
1688 if (AT->getNumElements() != ConstOps.size())
1689 return error(Message: "Invalid number of elements in array initializer");
1690
1691 for (Constant *Op : ConstOps)
1692 if (Op->getType() != AT->getElementType())
1693 return error(Message: "Incorrect type in array initializer");
1694
1695 C = ConstantArray::get(T: AT, V: ConstOps);
1696 break;
1697 }
1698 case BitcodeConstant::ConstantVectorOpcode: {
1699 auto *VT = cast<FixedVectorType>(Val: BC->getType());
1700 if (VT->getNumElements() != ConstOps.size())
1701 return error(Message: "Invalid number of elements in vector initializer");
1702
1703 for (Constant *Op : ConstOps)
1704 if (Op->getType() != VT->getElementType())
1705 return error(Message: "Incorrect type in vector initializer");
1706
1707 C = ConstantVector::get(V: ConstOps);
1708 break;
1709 }
1710 case Instruction::GetElementPtr:
1711 C = ConstantExpr::getGetElementPtr(
1712 Ty: BC->SrcElemTy, C: ConstOps[0], IdxList: ArrayRef(ConstOps).drop_front(),
1713 NW: toGEPNoWrapFlags(Flags: BC->Flags), InRange: BC->getInRange());
1714 break;
1715 case Instruction::ExtractElement:
1716 C = ConstantExpr::getExtractElement(Vec: ConstOps[0], Idx: ConstOps[1]);
1717 break;
1718 case Instruction::InsertElement:
1719 C = ConstantExpr::getInsertElement(Vec: ConstOps[0], Elt: ConstOps[1],
1720 Idx: ConstOps[2]);
1721 break;
1722 case Instruction::ShuffleVector: {
1723 SmallVector<int, 16> Mask;
1724 ShuffleVectorInst::getShuffleMask(Mask: ConstOps[2], Result&: Mask);
1725 C = ConstantExpr::getShuffleVector(V1: ConstOps[0], V2: ConstOps[1], Mask);
1726 break;
1727 }
1728 default:
1729 llvm_unreachable("Unhandled bitcode constant");
1730 }
1731 }
1732
1733 // Cache resolved constant.
1734 ValueList.replaceValueWithoutRAUW(ValNo: ValID, NewV: C);
1735 MaterializedValues.insert(KV: {ValID, C});
1736 Worklist.pop_back();
1737 continue;
1738 }
1739
1740 if (!InsertBB)
1741 return error(Message: Twine("Value referenced by initializer is an unsupported "
1742 "constant expression of type ") +
1743 BC->getOpcodeName());
1744
1745 // Materialize as instructions if necessary.
1746 Instruction *I;
1747 if (Instruction::isCast(Opcode: BC->Opcode)) {
1748 I = CastInst::Create((Instruction::CastOps)BC->Opcode, S: Ops[0],
1749 Ty: BC->getType(), Name: "constexpr", InsertBefore: InsertBB);
1750 } else if (Instruction::isUnaryOp(Opcode: BC->Opcode)) {
1751 I = UnaryOperator::Create(Op: (Instruction::UnaryOps)BC->Opcode, S: Ops[0],
1752 Name: "constexpr", InsertBefore: InsertBB);
1753 } else if (Instruction::isBinaryOp(Opcode: BC->Opcode)) {
1754 I = BinaryOperator::Create(Op: (Instruction::BinaryOps)BC->Opcode, S1: Ops[0],
1755 S2: Ops[1], Name: "constexpr", InsertBefore: InsertBB);
1756 if (isa<OverflowingBinaryOperator>(Val: I)) {
1757 if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap)
1758 I->setHasNoSignedWrap();
1759 if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap)
1760 I->setHasNoUnsignedWrap();
1761 }
1762 if (isa<PossiblyExactOperator>(Val: I) &&
1763 (BC->Flags & PossiblyExactOperator::IsExact))
1764 I->setIsExact();
1765 } else {
1766 switch (BC->Opcode) {
1767 case BitcodeConstant::ConstantVectorOpcode: {
1768 Type *IdxTy = Type::getInt32Ty(C&: BC->getContext());
1769 Value *V = PoisonValue::get(T: BC->getType());
1770 for (auto Pair : enumerate(First&: Ops)) {
1771 Value *Idx = ConstantInt::get(Ty: IdxTy, V: Pair.index());
1772 V = InsertElementInst::Create(Vec: V, NewElt: Pair.value(), Idx, NameStr: "constexpr.ins",
1773 InsertBefore: InsertBB);
1774 }
1775 I = cast<Instruction>(Val: V);
1776 break;
1777 }
1778 case BitcodeConstant::ConstantStructOpcode:
1779 case BitcodeConstant::ConstantArrayOpcode: {
1780 Value *V = PoisonValue::get(T: BC->getType());
1781 for (auto Pair : enumerate(First&: Ops))
1782 V = InsertValueInst::Create(Agg: V, Val: Pair.value(), Idxs: Pair.index(),
1783 NameStr: "constexpr.ins", InsertBefore: InsertBB);
1784 I = cast<Instruction>(Val: V);
1785 break;
1786 }
1787 case Instruction::ICmp:
1788 case Instruction::FCmp:
1789 I = CmpInst::Create(Op: (Instruction::OtherOps)BC->Opcode,
1790 Pred: (CmpInst::Predicate)BC->Flags, S1: Ops[0], S2: Ops[1],
1791 Name: "constexpr", InsertBefore: InsertBB);
1792 break;
1793 case Instruction::GetElementPtr:
1794 I = GetElementPtrInst::Create(PointeeType: BC->SrcElemTy, Ptr: Ops[0],
1795 IdxList: ArrayRef(Ops).drop_front(), NameStr: "constexpr",
1796 InsertBefore: InsertBB);
1797 cast<GetElementPtrInst>(Val: I)->setNoWrapFlags(toGEPNoWrapFlags(Flags: BC->Flags));
1798 break;
1799 case Instruction::Select:
1800 I = SelectInst::Create(C: Ops[0], S1: Ops[1], S2: Ops[2], NameStr: "constexpr", InsertBefore: InsertBB);
1801 break;
1802 case Instruction::ExtractElement:
1803 I = ExtractElementInst::Create(Vec: Ops[0], Idx: Ops[1], NameStr: "constexpr", InsertBefore: InsertBB);
1804 break;
1805 case Instruction::InsertElement:
1806 I = InsertElementInst::Create(Vec: Ops[0], NewElt: Ops[1], Idx: Ops[2], NameStr: "constexpr",
1807 InsertBefore: InsertBB);
1808 break;
1809 case Instruction::ShuffleVector:
1810 I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr",
1811 InsertBB);
1812 break;
1813 default:
1814 llvm_unreachable("Unhandled bitcode constant");
1815 }
1816 }
1817
1818 MaterializedValues.insert(KV: {ValID, I});
1819 Worklist.pop_back();
1820 }
1821
1822 return MaterializedValues[StartValID];
1823}
1824
1825Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) {
1826 Expected<Value *> MaybeV = materializeValue(StartValID: ID, /* InsertBB */ nullptr);
1827 if (!MaybeV)
1828 return MaybeV.takeError();
1829
1830 // Result must be Constant if InsertBB is nullptr.
1831 return cast<Constant>(Val: MaybeV.get());
1832}
1833
1834StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1835 StringRef Name) {
1836 auto *Ret = StructType::create(Context, Name);
1837 IdentifiedStructTypes.push_back(x: Ret);
1838 return Ret;
1839}
1840
1841StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1842 auto *Ret = StructType::create(Context);
1843 IdentifiedStructTypes.push_back(x: Ret);
1844 return Ret;
1845}
1846
1847//===----------------------------------------------------------------------===//
1848// Functions for parsing blocks from the bitcode file
1849//===----------------------------------------------------------------------===//
1850
1851static uint64_t getRawAttributeMask(Attribute::AttrKind Val) {
1852 switch (Val) {
1853 case Attribute::EndAttrKinds:
1854 case Attribute::EmptyKey:
1855 case Attribute::TombstoneKey:
1856 llvm_unreachable("Synthetic enumerators which should never get here");
1857
1858 case Attribute::None: return 0;
1859 case Attribute::ZExt: return 1 << 0;
1860 case Attribute::SExt: return 1 << 1;
1861 case Attribute::NoReturn: return 1 << 2;
1862 case Attribute::InReg: return 1 << 3;
1863 case Attribute::StructRet: return 1 << 4;
1864 case Attribute::NoUnwind: return 1 << 5;
1865 case Attribute::NoAlias: return 1 << 6;
1866 case Attribute::ByVal: return 1 << 7;
1867 case Attribute::Nest: return 1 << 8;
1868 case Attribute::ReadNone: return 1 << 9;
1869 case Attribute::ReadOnly: return 1 << 10;
1870 case Attribute::NoInline: return 1 << 11;
1871 case Attribute::AlwaysInline: return 1 << 12;
1872 case Attribute::OptimizeForSize: return 1 << 13;
1873 case Attribute::StackProtect: return 1 << 14;
1874 case Attribute::StackProtectReq: return 1 << 15;
1875 case Attribute::Alignment: return 31 << 16;
1876 // 1ULL << 21 is NoCapture, which is upgraded separately.
1877 case Attribute::NoRedZone: return 1 << 22;
1878 case Attribute::NoImplicitFloat: return 1 << 23;
1879 case Attribute::Naked: return 1 << 24;
1880 case Attribute::InlineHint: return 1 << 25;
1881 case Attribute::StackAlignment: return 7 << 26;
1882 case Attribute::ReturnsTwice: return 1 << 29;
1883 case Attribute::UWTable: return 1 << 30;
1884 case Attribute::NonLazyBind: return 1U << 31;
1885 case Attribute::SanitizeAddress: return 1ULL << 32;
1886 case Attribute::MinSize: return 1ULL << 33;
1887 case Attribute::NoDuplicate: return 1ULL << 34;
1888 case Attribute::StackProtectStrong: return 1ULL << 35;
1889 case Attribute::SanitizeThread: return 1ULL << 36;
1890 case Attribute::SanitizeMemory: return 1ULL << 37;
1891 case Attribute::NoBuiltin: return 1ULL << 38;
1892 case Attribute::Returned: return 1ULL << 39;
1893 case Attribute::Cold: return 1ULL << 40;
1894 case Attribute::Builtin: return 1ULL << 41;
1895 case Attribute::OptimizeNone: return 1ULL << 42;
1896 case Attribute::InAlloca: return 1ULL << 43;
1897 case Attribute::NonNull: return 1ULL << 44;
1898 case Attribute::JumpTable: return 1ULL << 45;
1899 case Attribute::Convergent: return 1ULL << 46;
1900 case Attribute::SafeStack: return 1ULL << 47;
1901 case Attribute::NoRecurse: return 1ULL << 48;
1902 // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately.
1903 // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately.
1904 case Attribute::SwiftSelf: return 1ULL << 51;
1905 case Attribute::SwiftError: return 1ULL << 52;
1906 case Attribute::WriteOnly: return 1ULL << 53;
1907 case Attribute::Speculatable: return 1ULL << 54;
1908 case Attribute::StrictFP: return 1ULL << 55;
1909 case Attribute::SanitizeHWAddress: return 1ULL << 56;
1910 case Attribute::NoCfCheck: return 1ULL << 57;
1911 case Attribute::OptForFuzzing: return 1ULL << 58;
1912 case Attribute::ShadowCallStack: return 1ULL << 59;
1913 case Attribute::SpeculativeLoadHardening:
1914 return 1ULL << 60;
1915 case Attribute::ImmArg:
1916 return 1ULL << 61;
1917 case Attribute::WillReturn:
1918 return 1ULL << 62;
1919 case Attribute::NoFree:
1920 return 1ULL << 63;
1921 default:
1922 // Other attributes are not supported in the raw format,
1923 // as we ran out of space.
1924 return 0;
1925 }
1926 llvm_unreachable("Unsupported attribute type");
1927}
1928
1929static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) {
1930 if (!Val) return;
1931
1932 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1933 I = Attribute::AttrKind(I + 1)) {
1934 if (uint64_t A = (Val & getRawAttributeMask(Val: I))) {
1935 if (I == Attribute::Alignment)
1936 B.addAlignmentAttr(Align: 1ULL << ((A >> 16) - 1));
1937 else if (I == Attribute::StackAlignment)
1938 B.addStackAlignmentAttr(Align: 1ULL << ((A >> 26)-1));
1939 else if (Attribute::isTypeAttrKind(Kind: I))
1940 B.addTypeAttr(Kind: I, Ty: nullptr); // Type will be auto-upgraded.
1941 else
1942 B.addAttribute(Val: I);
1943 }
1944 }
1945}
1946
1947/// This fills an AttrBuilder object with the LLVM attributes that have
1948/// been decoded from the given integer.
1949static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1950 uint64_t EncodedAttrs,
1951 uint64_t AttrIdx) {
1952 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1953 // the bits above 31 down by 11 bits.
1954 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1955 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1956 "Alignment must be a power of two.");
1957
1958 if (Alignment)
1959 B.addAlignmentAttr(Align: Alignment);
1960
1961 uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1962 (EncodedAttrs & 0xffff);
1963
1964 if (AttrIdx == AttributeList::FunctionIndex) {
1965 // Upgrade old memory attributes.
1966 MemoryEffects ME = MemoryEffects::unknown();
1967 if (Attrs & (1ULL << 9)) {
1968 // ReadNone
1969 Attrs &= ~(1ULL << 9);
1970 ME &= MemoryEffects::none();
1971 }
1972 if (Attrs & (1ULL << 10)) {
1973 // ReadOnly
1974 Attrs &= ~(1ULL << 10);
1975 ME &= MemoryEffects::readOnly();
1976 }
1977 if (Attrs & (1ULL << 49)) {
1978 // InaccessibleMemOnly
1979 Attrs &= ~(1ULL << 49);
1980 ME &= MemoryEffects::inaccessibleMemOnly();
1981 }
1982 if (Attrs & (1ULL << 50)) {
1983 // InaccessibleMemOrArgMemOnly
1984 Attrs &= ~(1ULL << 50);
1985 ME &= MemoryEffects::inaccessibleOrArgMemOnly();
1986 }
1987 if (Attrs & (1ULL << 53)) {
1988 // WriteOnly
1989 Attrs &= ~(1ULL << 53);
1990 ME &= MemoryEffects::writeOnly();
1991 }
1992 if (ME != MemoryEffects::unknown())
1993 B.addMemoryAttr(ME);
1994 }
1995
1996 // Upgrade nocapture to captures(none).
1997 if (Attrs & (1ULL << 21)) {
1998 Attrs &= ~(1ULL << 21);
1999 B.addCapturesAttr(CI: CaptureInfo::none());
2000 }
2001
2002 addRawAttributeValue(B, Val: Attrs);
2003}
2004
2005Error BitcodeReader::parseAttributeBlock() {
2006 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::PARAMATTR_BLOCK_ID))
2007 return Err;
2008
2009 if (!MAttributes.empty())
2010 return error(Message: "Invalid multiple blocks");
2011
2012 SmallVector<uint64_t, 64> Record;
2013
2014 SmallVector<AttributeList, 8> Attrs;
2015
2016 // Read all the records.
2017 while (true) {
2018 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2019 if (!MaybeEntry)
2020 return MaybeEntry.takeError();
2021 BitstreamEntry Entry = MaybeEntry.get();
2022
2023 switch (Entry.Kind) {
2024 case BitstreamEntry::SubBlock: // Handled for us already.
2025 case BitstreamEntry::Error:
2026 return error(Message: "Malformed block");
2027 case BitstreamEntry::EndBlock:
2028 return Error::success();
2029 case BitstreamEntry::Record:
2030 // The interesting case.
2031 break;
2032 }
2033
2034 // Read a record.
2035 Record.clear();
2036 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2037 if (!MaybeRecord)
2038 return MaybeRecord.takeError();
2039 switch (MaybeRecord.get()) {
2040 default: // Default behavior: ignore.
2041 break;
2042 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
2043 // Deprecated, but still needed to read old bitcode files.
2044 if (Record.size() & 1)
2045 return error(Message: "Invalid parameter attribute record");
2046
2047 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
2048 AttrBuilder B(Context);
2049 decodeLLVMAttributesForBitcode(B, EncodedAttrs: Record[i+1], AttrIdx: Record[i]);
2050 Attrs.push_back(Elt: AttributeList::get(C&: Context, Index: Record[i], B));
2051 }
2052
2053 MAttributes.push_back(x: AttributeList::get(C&: Context, Attrs));
2054 Attrs.clear();
2055 break;
2056 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
2057 for (uint64_t Val : Record)
2058 Attrs.push_back(Elt: MAttributeGroups[Val]);
2059
2060 MAttributes.push_back(x: AttributeList::get(C&: Context, Attrs));
2061 Attrs.clear();
2062 break;
2063 }
2064 }
2065}
2066
2067// Returns Attribute::None on unrecognized codes.
2068static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
2069 switch (Code) {
2070 default:
2071 return Attribute::None;
2072 case bitc::ATTR_KIND_ALIGNMENT:
2073 return Attribute::Alignment;
2074 case bitc::ATTR_KIND_ALWAYS_INLINE:
2075 return Attribute::AlwaysInline;
2076 case bitc::ATTR_KIND_BUILTIN:
2077 return Attribute::Builtin;
2078 case bitc::ATTR_KIND_BY_VAL:
2079 return Attribute::ByVal;
2080 case bitc::ATTR_KIND_IN_ALLOCA:
2081 return Attribute::InAlloca;
2082 case bitc::ATTR_KIND_COLD:
2083 return Attribute::Cold;
2084 case bitc::ATTR_KIND_CONVERGENT:
2085 return Attribute::Convergent;
2086 case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION:
2087 return Attribute::DisableSanitizerInstrumentation;
2088 case bitc::ATTR_KIND_ELEMENTTYPE:
2089 return Attribute::ElementType;
2090 case bitc::ATTR_KIND_FNRETTHUNK_EXTERN:
2091 return Attribute::FnRetThunkExtern;
2092 case bitc::ATTR_KIND_INLINE_HINT:
2093 return Attribute::InlineHint;
2094 case bitc::ATTR_KIND_IN_REG:
2095 return Attribute::InReg;
2096 case bitc::ATTR_KIND_JUMP_TABLE:
2097 return Attribute::JumpTable;
2098 case bitc::ATTR_KIND_MEMORY:
2099 return Attribute::Memory;
2100 case bitc::ATTR_KIND_NOFPCLASS:
2101 return Attribute::NoFPClass;
2102 case bitc::ATTR_KIND_MIN_SIZE:
2103 return Attribute::MinSize;
2104 case bitc::ATTR_KIND_NAKED:
2105 return Attribute::Naked;
2106 case bitc::ATTR_KIND_NEST:
2107 return Attribute::Nest;
2108 case bitc::ATTR_KIND_NO_ALIAS:
2109 return Attribute::NoAlias;
2110 case bitc::ATTR_KIND_NO_BUILTIN:
2111 return Attribute::NoBuiltin;
2112 case bitc::ATTR_KIND_NO_CALLBACK:
2113 return Attribute::NoCallback;
2114 case bitc::ATTR_KIND_NO_DIVERGENCE_SOURCE:
2115 return Attribute::NoDivergenceSource;
2116 case bitc::ATTR_KIND_NO_DUPLICATE:
2117 return Attribute::NoDuplicate;
2118 case bitc::ATTR_KIND_NOFREE:
2119 return Attribute::NoFree;
2120 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
2121 return Attribute::NoImplicitFloat;
2122 case bitc::ATTR_KIND_NO_INLINE:
2123 return Attribute::NoInline;
2124 case bitc::ATTR_KIND_NO_RECURSE:
2125 return Attribute::NoRecurse;
2126 case bitc::ATTR_KIND_NO_MERGE:
2127 return Attribute::NoMerge;
2128 case bitc::ATTR_KIND_NON_LAZY_BIND:
2129 return Attribute::NonLazyBind;
2130 case bitc::ATTR_KIND_NON_NULL:
2131 return Attribute::NonNull;
2132 case bitc::ATTR_KIND_DEREFERENCEABLE:
2133 return Attribute::Dereferenceable;
2134 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
2135 return Attribute::DereferenceableOrNull;
2136 case bitc::ATTR_KIND_ALLOC_ALIGN:
2137 return Attribute::AllocAlign;
2138 case bitc::ATTR_KIND_ALLOC_KIND:
2139 return Attribute::AllocKind;
2140 case bitc::ATTR_KIND_ALLOC_SIZE:
2141 return Attribute::AllocSize;
2142 case bitc::ATTR_KIND_ALLOCATED_POINTER:
2143 return Attribute::AllocatedPointer;
2144 case bitc::ATTR_KIND_NO_RED_ZONE:
2145 return Attribute::NoRedZone;
2146 case bitc::ATTR_KIND_NO_RETURN:
2147 return Attribute::NoReturn;
2148 case bitc::ATTR_KIND_NOSYNC:
2149 return Attribute::NoSync;
2150 case bitc::ATTR_KIND_NOCF_CHECK:
2151 return Attribute::NoCfCheck;
2152 case bitc::ATTR_KIND_NO_PROFILE:
2153 return Attribute::NoProfile;
2154 case bitc::ATTR_KIND_SKIP_PROFILE:
2155 return Attribute::SkipProfile;
2156 case bitc::ATTR_KIND_NO_UNWIND:
2157 return Attribute::NoUnwind;
2158 case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS:
2159 return Attribute::NoSanitizeBounds;
2160 case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE:
2161 return Attribute::NoSanitizeCoverage;
2162 case bitc::ATTR_KIND_NULL_POINTER_IS_VALID:
2163 return Attribute::NullPointerIsValid;
2164 case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING:
2165 return Attribute::OptimizeForDebugging;
2166 case bitc::ATTR_KIND_OPT_FOR_FUZZING:
2167 return Attribute::OptForFuzzing;
2168 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
2169 return Attribute::OptimizeForSize;
2170 case bitc::ATTR_KIND_OPTIMIZE_NONE:
2171 return Attribute::OptimizeNone;
2172 case bitc::ATTR_KIND_READ_NONE:
2173 return Attribute::ReadNone;
2174 case bitc::ATTR_KIND_READ_ONLY:
2175 return Attribute::ReadOnly;
2176 case bitc::ATTR_KIND_RETURNED:
2177 return Attribute::Returned;
2178 case bitc::ATTR_KIND_RETURNS_TWICE:
2179 return Attribute::ReturnsTwice;
2180 case bitc::ATTR_KIND_S_EXT:
2181 return Attribute::SExt;
2182 case bitc::ATTR_KIND_SPECULATABLE:
2183 return Attribute::Speculatable;
2184 case bitc::ATTR_KIND_STACK_ALIGNMENT:
2185 return Attribute::StackAlignment;
2186 case bitc::ATTR_KIND_STACK_PROTECT:
2187 return Attribute::StackProtect;
2188 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
2189 return Attribute::StackProtectReq;
2190 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
2191 return Attribute::StackProtectStrong;
2192 case bitc::ATTR_KIND_SAFESTACK:
2193 return Attribute::SafeStack;
2194 case bitc::ATTR_KIND_SHADOWCALLSTACK:
2195 return Attribute::ShadowCallStack;
2196 case bitc::ATTR_KIND_STRICT_FP:
2197 return Attribute::StrictFP;
2198 case bitc::ATTR_KIND_STRUCT_RET:
2199 return Attribute::StructRet;
2200 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
2201 return Attribute::SanitizeAddress;
2202 case bitc::ATTR_KIND_SANITIZE_HWADDRESS:
2203 return Attribute::SanitizeHWAddress;
2204 case bitc::ATTR_KIND_SANITIZE_THREAD:
2205 return Attribute::SanitizeThread;
2206 case bitc::ATTR_KIND_SANITIZE_TYPE:
2207 return Attribute::SanitizeType;
2208 case bitc::ATTR_KIND_SANITIZE_MEMORY:
2209 return Attribute::SanitizeMemory;
2210 case bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY:
2211 return Attribute::SanitizeNumericalStability;
2212 case bitc::ATTR_KIND_SANITIZE_REALTIME:
2213 return Attribute::SanitizeRealtime;
2214 case bitc::ATTR_KIND_SANITIZE_REALTIME_BLOCKING:
2215 return Attribute::SanitizeRealtimeBlocking;
2216 case bitc::ATTR_KIND_SANITIZE_ALLOC_TOKEN:
2217 return Attribute::SanitizeAllocToken;
2218 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
2219 return Attribute::SpeculativeLoadHardening;
2220 case bitc::ATTR_KIND_SWIFT_ERROR:
2221 return Attribute::SwiftError;
2222 case bitc::ATTR_KIND_SWIFT_SELF:
2223 return Attribute::SwiftSelf;
2224 case bitc::ATTR_KIND_SWIFT_ASYNC:
2225 return Attribute::SwiftAsync;
2226 case bitc::ATTR_KIND_UW_TABLE:
2227 return Attribute::UWTable;
2228 case bitc::ATTR_KIND_VSCALE_RANGE:
2229 return Attribute::VScaleRange;
2230 case bitc::ATTR_KIND_WILLRETURN:
2231 return Attribute::WillReturn;
2232 case bitc::ATTR_KIND_WRITEONLY:
2233 return Attribute::WriteOnly;
2234 case bitc::ATTR_KIND_Z_EXT:
2235 return Attribute::ZExt;
2236 case bitc::ATTR_KIND_IMMARG:
2237 return Attribute::ImmArg;
2238 case bitc::ATTR_KIND_SANITIZE_MEMTAG:
2239 return Attribute::SanitizeMemTag;
2240 case bitc::ATTR_KIND_PREALLOCATED:
2241 return Attribute::Preallocated;
2242 case bitc::ATTR_KIND_NOUNDEF:
2243 return Attribute::NoUndef;
2244 case bitc::ATTR_KIND_BYREF:
2245 return Attribute::ByRef;
2246 case bitc::ATTR_KIND_MUSTPROGRESS:
2247 return Attribute::MustProgress;
2248 case bitc::ATTR_KIND_HOT:
2249 return Attribute::Hot;
2250 case bitc::ATTR_KIND_PRESPLIT_COROUTINE:
2251 return Attribute::PresplitCoroutine;
2252 case bitc::ATTR_KIND_WRITABLE:
2253 return Attribute::Writable;
2254 case bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE:
2255 return Attribute::CoroDestroyOnlyWhenComplete;
2256 case bitc::ATTR_KIND_DEAD_ON_UNWIND:
2257 return Attribute::DeadOnUnwind;
2258 case bitc::ATTR_KIND_RANGE:
2259 return Attribute::Range;
2260 case bitc::ATTR_KIND_INITIALIZES:
2261 return Attribute::Initializes;
2262 case bitc::ATTR_KIND_CORO_ELIDE_SAFE:
2263 return Attribute::CoroElideSafe;
2264 case bitc::ATTR_KIND_NO_EXT:
2265 return Attribute::NoExt;
2266 case bitc::ATTR_KIND_CAPTURES:
2267 return Attribute::Captures;
2268 case bitc::ATTR_KIND_DEAD_ON_RETURN:
2269 return Attribute::DeadOnReturn;
2270 case bitc::ATTR_KIND_NO_CREATE_UNDEF_OR_POISON:
2271 return Attribute::NoCreateUndefOrPoison;
2272 }
2273}
2274
2275Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
2276 MaybeAlign &Alignment) {
2277 // Note: Alignment in bitcode files is incremented by 1, so that zero
2278 // can be used for default alignment.
2279 if (Exponent > Value::MaxAlignmentExponent + 1)
2280 return error(Message: "Invalid alignment value");
2281 Alignment = decodeMaybeAlign(Value: Exponent);
2282 return Error::success();
2283}
2284
2285Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
2286 *Kind = getAttrFromCode(Code);
2287 if (*Kind == Attribute::None)
2288 return error(Message: "Unknown attribute kind (" + Twine(Code) + ")");
2289 return Error::success();
2290}
2291
2292static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) {
2293 switch (EncodedKind) {
2294 case bitc::ATTR_KIND_READ_NONE:
2295 ME &= MemoryEffects::none();
2296 return true;
2297 case bitc::ATTR_KIND_READ_ONLY:
2298 ME &= MemoryEffects::readOnly();
2299 return true;
2300 case bitc::ATTR_KIND_WRITEONLY:
2301 ME &= MemoryEffects::writeOnly();
2302 return true;
2303 case bitc::ATTR_KIND_ARGMEMONLY:
2304 ME &= MemoryEffects::argMemOnly();
2305 return true;
2306 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
2307 ME &= MemoryEffects::inaccessibleMemOnly();
2308 return true;
2309 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
2310 ME &= MemoryEffects::inaccessibleOrArgMemOnly();
2311 return true;
2312 default:
2313 return false;
2314 }
2315}
2316
2317Error BitcodeReader::parseAttributeGroupBlock() {
2318 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::PARAMATTR_GROUP_BLOCK_ID))
2319 return Err;
2320
2321 if (!MAttributeGroups.empty())
2322 return error(Message: "Invalid multiple blocks");
2323
2324 SmallVector<uint64_t, 64> Record;
2325
2326 // Read all the records.
2327 while (true) {
2328 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2329 if (!MaybeEntry)
2330 return MaybeEntry.takeError();
2331 BitstreamEntry Entry = MaybeEntry.get();
2332
2333 switch (Entry.Kind) {
2334 case BitstreamEntry::SubBlock: // Handled for us already.
2335 case BitstreamEntry::Error:
2336 return error(Message: "Malformed block");
2337 case BitstreamEntry::EndBlock:
2338 return Error::success();
2339 case BitstreamEntry::Record:
2340 // The interesting case.
2341 break;
2342 }
2343
2344 // Read a record.
2345 Record.clear();
2346 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2347 if (!MaybeRecord)
2348 return MaybeRecord.takeError();
2349 switch (MaybeRecord.get()) {
2350 default: // Default behavior: ignore.
2351 break;
2352 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
2353 if (Record.size() < 3)
2354 return error(Message: "Invalid grp record");
2355
2356 uint64_t GrpID = Record[0];
2357 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
2358
2359 AttrBuilder B(Context);
2360 MemoryEffects ME = MemoryEffects::unknown();
2361 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2362 if (Record[i] == 0) { // Enum attribute
2363 Attribute::AttrKind Kind;
2364 uint64_t EncodedKind = Record[++i];
2365 if (Idx == AttributeList::FunctionIndex &&
2366 upgradeOldMemoryAttribute(ME, EncodedKind))
2367 continue;
2368
2369 if (EncodedKind == bitc::ATTR_KIND_NO_CAPTURE) {
2370 B.addCapturesAttr(CI: CaptureInfo::none());
2371 continue;
2372 }
2373
2374 if (Error Err = parseAttrKind(Code: EncodedKind, Kind: &Kind))
2375 return Err;
2376
2377 // Upgrade old-style byval attribute to one with a type, even if it's
2378 // nullptr. We will have to insert the real type when we associate
2379 // this AttributeList with a function.
2380 if (Kind == Attribute::ByVal)
2381 B.addByValAttr(Ty: nullptr);
2382 else if (Kind == Attribute::StructRet)
2383 B.addStructRetAttr(Ty: nullptr);
2384 else if (Kind == Attribute::InAlloca)
2385 B.addInAllocaAttr(Ty: nullptr);
2386 else if (Kind == Attribute::UWTable)
2387 B.addUWTableAttr(Kind: UWTableKind::Default);
2388 else if (Kind == Attribute::DeadOnReturn)
2389 B.addDeadOnReturnAttr(Info: DeadOnReturnInfo());
2390 else if (Attribute::isEnumAttrKind(Kind))
2391 B.addAttribute(Val: Kind);
2392 else
2393 return error(Message: "Not an enum attribute");
2394 } else if (Record[i] == 1) { // Integer attribute
2395 Attribute::AttrKind Kind;
2396 if (Error Err = parseAttrKind(Code: Record[++i], Kind: &Kind))
2397 return Err;
2398 if (!Attribute::isIntAttrKind(Kind))
2399 return error(Message: "Not an int attribute");
2400 if (Kind == Attribute::Alignment)
2401 B.addAlignmentAttr(Align: Record[++i]);
2402 else if (Kind == Attribute::StackAlignment)
2403 B.addStackAlignmentAttr(Align: Record[++i]);
2404 else if (Kind == Attribute::Dereferenceable)
2405 B.addDereferenceableAttr(Bytes: Record[++i]);
2406 else if (Kind == Attribute::DereferenceableOrNull)
2407 B.addDereferenceableOrNullAttr(Bytes: Record[++i]);
2408 else if (Kind == Attribute::DeadOnReturn)
2409 B.addDeadOnReturnAttr(
2410 Info: DeadOnReturnInfo::createFromIntValue(Data: Record[++i]));
2411 else if (Kind == Attribute::AllocSize)
2412 B.addAllocSizeAttrFromRawRepr(RawAllocSizeRepr: Record[++i]);
2413 else if (Kind == Attribute::VScaleRange)
2414 B.addVScaleRangeAttrFromRawRepr(RawVScaleRangeRepr: Record[++i]);
2415 else if (Kind == Attribute::UWTable)
2416 B.addUWTableAttr(Kind: UWTableKind(Record[++i]));
2417 else if (Kind == Attribute::AllocKind)
2418 B.addAllocKindAttr(Kind: static_cast<AllocFnKind>(Record[++i]));
2419 else if (Kind == Attribute::Memory) {
2420 uint64_t EncodedME = Record[++i];
2421 const uint8_t Version = (EncodedME >> 56);
2422 if (Version == 0) {
2423 // Errno memory location was previously encompassed into default
2424 // memory. Ensure this is taken into account while reconstructing
2425 // the memory attribute prior to its introduction.
2426 ModRefInfo ArgMem = ModRefInfo((EncodedME >> 0) & 3);
2427 ModRefInfo InaccessibleMem = ModRefInfo((EncodedME >> 2) & 3);
2428 ModRefInfo OtherMem = ModRefInfo((EncodedME >> 4) & 3);
2429 auto ME = MemoryEffects::inaccessibleMemOnly(MR: InaccessibleMem) |
2430 MemoryEffects::argMemOnly(MR: ArgMem) |
2431 MemoryEffects::errnoMemOnly(MR: OtherMem) |
2432 MemoryEffects::otherMemOnly(MR: OtherMem);
2433 B.addMemoryAttr(ME);
2434 } else {
2435 // Construct the memory attribute directly from the encoded base
2436 // on newer versions.
2437 B.addMemoryAttr(ME: MemoryEffects::createFromIntValue(
2438 Data: EncodedME & 0x00FFFFFFFFFFFFFFULL));
2439 }
2440 } else if (Kind == Attribute::Captures)
2441 B.addCapturesAttr(CI: CaptureInfo::createFromIntValue(Data: Record[++i]));
2442 else if (Kind == Attribute::NoFPClass)
2443 B.addNoFPClassAttr(
2444 NoFPClassMask: static_cast<FPClassTest>(Record[++i] & fcAllFlags));
2445 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute
2446 bool HasValue = (Record[i++] == 4);
2447 SmallString<64> KindStr;
2448 SmallString<64> ValStr;
2449
2450 while (Record[i] != 0 && i != e)
2451 KindStr += Record[i++];
2452 assert(Record[i] == 0 && "Kind string not null terminated");
2453
2454 if (HasValue) {
2455 // Has a value associated with it.
2456 ++i; // Skip the '0' that terminates the "kind" string.
2457 while (Record[i] != 0 && i != e)
2458 ValStr += Record[i++];
2459 assert(Record[i] == 0 && "Value string not null terminated");
2460 }
2461
2462 B.addAttribute(A: KindStr.str(), V: ValStr.str());
2463 } else if (Record[i] == 5 || Record[i] == 6) {
2464 bool HasType = Record[i] == 6;
2465 Attribute::AttrKind Kind;
2466 if (Error Err = parseAttrKind(Code: Record[++i], Kind: &Kind))
2467 return Err;
2468 if (!Attribute::isTypeAttrKind(Kind))
2469 return error(Message: "Not a type attribute");
2470
2471 B.addTypeAttr(Kind, Ty: HasType ? getTypeByID(ID: Record[++i]) : nullptr);
2472 } else if (Record[i] == 7) {
2473 Attribute::AttrKind Kind;
2474
2475 i++;
2476 if (Error Err = parseAttrKind(Code: Record[i++], Kind: &Kind))
2477 return Err;
2478 if (!Attribute::isConstantRangeAttrKind(Kind))
2479 return error(Message: "Not a ConstantRange attribute");
2480
2481 Expected<ConstantRange> MaybeCR =
2482 readBitWidthAndConstantRange(Record, OpNum&: i);
2483 if (!MaybeCR)
2484 return MaybeCR.takeError();
2485 i--;
2486
2487 B.addConstantRangeAttr(Kind, CR: MaybeCR.get());
2488 } else if (Record[i] == 8) {
2489 Attribute::AttrKind Kind;
2490
2491 i++;
2492 if (Error Err = parseAttrKind(Code: Record[i++], Kind: &Kind))
2493 return Err;
2494 if (!Attribute::isConstantRangeListAttrKind(Kind))
2495 return error(Message: "Not a constant range list attribute");
2496
2497 SmallVector<ConstantRange, 2> Val;
2498 if (i + 2 > e)
2499 return error(Message: "Too few records for constant range list");
2500 unsigned RangeSize = Record[i++];
2501 unsigned BitWidth = Record[i++];
2502 for (unsigned Idx = 0; Idx < RangeSize; ++Idx) {
2503 Expected<ConstantRange> MaybeCR =
2504 readConstantRange(Record, OpNum&: i, BitWidth);
2505 if (!MaybeCR)
2506 return MaybeCR.takeError();
2507 Val.push_back(Elt: MaybeCR.get());
2508 }
2509 i--;
2510
2511 if (!ConstantRangeList::isOrderedRanges(RangesRef: Val))
2512 return error(Message: "Invalid (unordered or overlapping) range list");
2513 B.addConstantRangeListAttr(Kind, Val);
2514 } else {
2515 return error(Message: "Invalid attribute group entry");
2516 }
2517 }
2518
2519 if (ME != MemoryEffects::unknown())
2520 B.addMemoryAttr(ME);
2521
2522 UpgradeAttributes(B);
2523 MAttributeGroups[GrpID] = AttributeList::get(C&: Context, Index: Idx, B);
2524 break;
2525 }
2526 }
2527 }
2528}
2529
2530Error BitcodeReader::parseTypeTable() {
2531 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::TYPE_BLOCK_ID_NEW))
2532 return Err;
2533
2534 return parseTypeTableBody();
2535}
2536
2537Error BitcodeReader::parseTypeTableBody() {
2538 if (!TypeList.empty())
2539 return error(Message: "Invalid multiple blocks");
2540
2541 SmallVector<uint64_t, 64> Record;
2542 unsigned NumRecords = 0;
2543
2544 SmallString<64> TypeName;
2545
2546 // Read all the records for this type table.
2547 while (true) {
2548 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2549 if (!MaybeEntry)
2550 return MaybeEntry.takeError();
2551 BitstreamEntry Entry = MaybeEntry.get();
2552
2553 switch (Entry.Kind) {
2554 case BitstreamEntry::SubBlock: // Handled for us already.
2555 case BitstreamEntry::Error:
2556 return error(Message: "Malformed block");
2557 case BitstreamEntry::EndBlock:
2558 if (NumRecords != TypeList.size())
2559 return error(Message: "Malformed block");
2560 return Error::success();
2561 case BitstreamEntry::Record:
2562 // The interesting case.
2563 break;
2564 }
2565
2566 // Read a record.
2567 Record.clear();
2568 Type *ResultTy = nullptr;
2569 SmallVector<unsigned> ContainedIDs;
2570 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2571 if (!MaybeRecord)
2572 return MaybeRecord.takeError();
2573 switch (MaybeRecord.get()) {
2574 default:
2575 return error(Message: "Invalid value");
2576 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
2577 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
2578 // type list. This allows us to reserve space.
2579 if (Record.empty())
2580 return error(Message: "Invalid numentry record");
2581 TypeList.resize(new_size: Record[0]);
2582 continue;
2583 case bitc::TYPE_CODE_VOID: // VOID
2584 ResultTy = Type::getVoidTy(C&: Context);
2585 break;
2586 case bitc::TYPE_CODE_HALF: // HALF
2587 ResultTy = Type::getHalfTy(C&: Context);
2588 break;
2589 case bitc::TYPE_CODE_BFLOAT: // BFLOAT
2590 ResultTy = Type::getBFloatTy(C&: Context);
2591 break;
2592 case bitc::TYPE_CODE_FLOAT: // FLOAT
2593 ResultTy = Type::getFloatTy(C&: Context);
2594 break;
2595 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
2596 ResultTy = Type::getDoubleTy(C&: Context);
2597 break;
2598 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
2599 ResultTy = Type::getX86_FP80Ty(C&: Context);
2600 break;
2601 case bitc::TYPE_CODE_FP128: // FP128
2602 ResultTy = Type::getFP128Ty(C&: Context);
2603 break;
2604 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
2605 ResultTy = Type::getPPC_FP128Ty(C&: Context);
2606 break;
2607 case bitc::TYPE_CODE_LABEL: // LABEL
2608 ResultTy = Type::getLabelTy(C&: Context);
2609 break;
2610 case bitc::TYPE_CODE_METADATA: // METADATA
2611 ResultTy = Type::getMetadataTy(C&: Context);
2612 break;
2613 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
2614 // Deprecated: decodes as <1 x i64>
2615 ResultTy =
2616 llvm::FixedVectorType::get(ElementType: llvm::IntegerType::get(C&: Context, NumBits: 64), NumElts: 1);
2617 break;
2618 case bitc::TYPE_CODE_X86_AMX: // X86_AMX
2619 ResultTy = Type::getX86_AMXTy(C&: Context);
2620 break;
2621 case bitc::TYPE_CODE_TOKEN: // TOKEN
2622 ResultTy = Type::getTokenTy(C&: Context);
2623 break;
2624 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
2625 if (Record.empty())
2626 return error(Message: "Invalid integer record");
2627
2628 uint64_t NumBits = Record[0];
2629 if (NumBits < IntegerType::MIN_INT_BITS ||
2630 NumBits > IntegerType::MAX_INT_BITS)
2631 return error(Message: "Bitwidth for integer type out of range");
2632 ResultTy = IntegerType::get(C&: Context, NumBits);
2633 break;
2634 }
2635 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
2636 // [pointee type, address space]
2637 if (Record.empty())
2638 return error(Message: "Invalid pointer record");
2639 unsigned AddressSpace = 0;
2640 if (Record.size() == 2)
2641 AddressSpace = Record[1];
2642 ResultTy = getTypeByID(ID: Record[0]);
2643 if (!ResultTy ||
2644 !PointerType::isValidElementType(ElemTy: ResultTy))
2645 return error(Message: "Invalid type");
2646 ContainedIDs.push_back(Elt: Record[0]);
2647 ResultTy = PointerType::get(C&: ResultTy->getContext(), AddressSpace);
2648 break;
2649 }
2650 case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace]
2651 if (Record.size() != 1)
2652 return error(Message: "Invalid opaque pointer record");
2653 unsigned AddressSpace = Record[0];
2654 ResultTy = PointerType::get(C&: Context, AddressSpace);
2655 break;
2656 }
2657 case bitc::TYPE_CODE_FUNCTION_OLD: {
2658 // Deprecated, but still needed to read old bitcode files.
2659 // FUNCTION: [vararg, attrid, retty, paramty x N]
2660 if (Record.size() < 3)
2661 return error(Message: "Invalid function record");
2662 SmallVector<Type*, 8> ArgTys;
2663 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
2664 if (Type *T = getTypeByID(ID: Record[i]))
2665 ArgTys.push_back(Elt: T);
2666 else
2667 break;
2668 }
2669
2670 ResultTy = getTypeByID(ID: Record[2]);
2671 if (!ResultTy || ArgTys.size() < Record.size()-3)
2672 return error(Message: "Invalid type");
2673
2674 ContainedIDs.append(in_start: Record.begin() + 2, in_end: Record.end());
2675 ResultTy = FunctionType::get(Result: ResultTy, Params: ArgTys, isVarArg: Record[0]);
2676 break;
2677 }
2678 case bitc::TYPE_CODE_FUNCTION: {
2679 // FUNCTION: [vararg, retty, paramty x N]
2680 if (Record.size() < 2)
2681 return error(Message: "Invalid function record");
2682 SmallVector<Type*, 8> ArgTys;
2683 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
2684 if (Type *T = getTypeByID(ID: Record[i])) {
2685 if (!FunctionType::isValidArgumentType(ArgTy: T))
2686 return error(Message: "Invalid function argument type");
2687 ArgTys.push_back(Elt: T);
2688 }
2689 else
2690 break;
2691 }
2692
2693 ResultTy = getTypeByID(ID: Record[1]);
2694 if (!ResultTy || ArgTys.size() < Record.size()-2)
2695 return error(Message: "Invalid type");
2696
2697 ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end());
2698 ResultTy = FunctionType::get(Result: ResultTy, Params: ArgTys, isVarArg: Record[0]);
2699 break;
2700 }
2701 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
2702 if (Record.empty())
2703 return error(Message: "Invalid anon struct record");
2704 SmallVector<Type*, 8> EltTys;
2705 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2706 if (Type *T = getTypeByID(ID: Record[i]))
2707 EltTys.push_back(Elt: T);
2708 else
2709 break;
2710 }
2711 if (EltTys.size() != Record.size()-1)
2712 return error(Message: "Invalid type");
2713 ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end());
2714 ResultTy = StructType::get(Context, Elements: EltTys, isPacked: Record[0]);
2715 break;
2716 }
2717 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
2718 if (convertToString(Record, Idx: 0, Result&: TypeName))
2719 return error(Message: "Invalid struct name record");
2720 continue;
2721
2722 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
2723 if (Record.empty())
2724 return error(Message: "Invalid named struct record");
2725
2726 if (NumRecords >= TypeList.size())
2727 return error(Message: "Invalid TYPE table");
2728
2729 // Check to see if this was forward referenced, if so fill in the temp.
2730 StructType *Res = cast_or_null<StructType>(Val: TypeList[NumRecords]);
2731 if (Res) {
2732 Res->setName(TypeName);
2733 TypeList[NumRecords] = nullptr;
2734 } else // Otherwise, create a new struct.
2735 Res = createIdentifiedStructType(Context, Name: TypeName);
2736 TypeName.clear();
2737
2738 SmallVector<Type*, 8> EltTys;
2739 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
2740 if (Type *T = getTypeByID(ID: Record[i]))
2741 EltTys.push_back(Elt: T);
2742 else
2743 break;
2744 }
2745 if (EltTys.size() != Record.size()-1)
2746 return error(Message: "Invalid named struct record");
2747 if (auto E = Res->setBodyOrError(Elements: EltTys, isPacked: Record[0]))
2748 return E;
2749 ContainedIDs.append(in_start: Record.begin() + 1, in_end: Record.end());
2750 ResultTy = Res;
2751 break;
2752 }
2753 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
2754 if (Record.size() != 1)
2755 return error(Message: "Invalid opaque type record");
2756
2757 if (NumRecords >= TypeList.size())
2758 return error(Message: "Invalid TYPE table");
2759
2760 // Check to see if this was forward referenced, if so fill in the temp.
2761 StructType *Res = cast_or_null<StructType>(Val: TypeList[NumRecords]);
2762 if (Res) {
2763 Res->setName(TypeName);
2764 TypeList[NumRecords] = nullptr;
2765 } else // Otherwise, create a new struct with no body.
2766 Res = createIdentifiedStructType(Context, Name: TypeName);
2767 TypeName.clear();
2768 ResultTy = Res;
2769 break;
2770 }
2771 case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...]
2772 if (Record.size() < 1)
2773 return error(Message: "Invalid target extension type record");
2774
2775 if (NumRecords >= TypeList.size())
2776 return error(Message: "Invalid TYPE table");
2777
2778 if (Record[0] >= Record.size())
2779 return error(Message: "Too many type parameters");
2780
2781 unsigned NumTys = Record[0];
2782 SmallVector<Type *, 4> TypeParams;
2783 SmallVector<unsigned, 8> IntParams;
2784 for (unsigned i = 0; i < NumTys; i++) {
2785 if (Type *T = getTypeByID(ID: Record[i + 1]))
2786 TypeParams.push_back(Elt: T);
2787 else
2788 return error(Message: "Invalid type");
2789 }
2790
2791 for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) {
2792 if (Record[i] > UINT_MAX)
2793 return error(Message: "Integer parameter too large");
2794 IntParams.push_back(Elt: Record[i]);
2795 }
2796 auto TTy =
2797 TargetExtType::getOrError(Context, Name: TypeName, Types: TypeParams, Ints: IntParams);
2798 if (auto E = TTy.takeError())
2799 return E;
2800 ResultTy = *TTy;
2801 TypeName.clear();
2802 break;
2803 }
2804 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
2805 if (Record.size() < 2)
2806 return error(Message: "Invalid array type record");
2807 ResultTy = getTypeByID(ID: Record[1]);
2808 if (!ResultTy || !ArrayType::isValidElementType(ElemTy: ResultTy))
2809 return error(Message: "Invalid type");
2810 ContainedIDs.push_back(Elt: Record[1]);
2811 ResultTy = ArrayType::get(ElementType: ResultTy, NumElements: Record[0]);
2812 break;
2813 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
2814 // [numelts, eltty, scalable]
2815 if (Record.size() < 2)
2816 return error(Message: "Invalid vector type record");
2817 if (Record[0] == 0)
2818 return error(Message: "Invalid vector length");
2819 ResultTy = getTypeByID(ID: Record[1]);
2820 if (!ResultTy || !VectorType::isValidElementType(ElemTy: ResultTy))
2821 return error(Message: "Invalid type");
2822 bool Scalable = Record.size() > 2 ? Record[2] : false;
2823 ContainedIDs.push_back(Elt: Record[1]);
2824 ResultTy = VectorType::get(ElementType: ResultTy, NumElements: Record[0], Scalable);
2825 break;
2826 }
2827
2828 if (NumRecords >= TypeList.size())
2829 return error(Message: "Invalid TYPE table");
2830 if (TypeList[NumRecords])
2831 return error(
2832 Message: "Invalid TYPE table: Only named structs can be forward referenced");
2833 assert(ResultTy && "Didn't read a type?");
2834 TypeList[NumRecords] = ResultTy;
2835 if (!ContainedIDs.empty())
2836 ContainedTypeIDs[NumRecords] = std::move(ContainedIDs);
2837 ++NumRecords;
2838 }
2839}
2840
2841Error BitcodeReader::parseOperandBundleTags() {
2842 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
2843 return Err;
2844
2845 if (!BundleTags.empty())
2846 return error(Message: "Invalid multiple blocks");
2847
2848 SmallVector<uint64_t, 64> Record;
2849
2850 while (true) {
2851 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2852 if (!MaybeEntry)
2853 return MaybeEntry.takeError();
2854 BitstreamEntry Entry = MaybeEntry.get();
2855
2856 switch (Entry.Kind) {
2857 case BitstreamEntry::SubBlock: // Handled for us already.
2858 case BitstreamEntry::Error:
2859 return error(Message: "Malformed block");
2860 case BitstreamEntry::EndBlock:
2861 return Error::success();
2862 case BitstreamEntry::Record:
2863 // The interesting case.
2864 break;
2865 }
2866
2867 // Tags are implicitly mapped to integers by their order.
2868
2869 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2870 if (!MaybeRecord)
2871 return MaybeRecord.takeError();
2872 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
2873 return error(Message: "Invalid operand bundle record");
2874
2875 // OPERAND_BUNDLE_TAG: [strchr x N]
2876 BundleTags.emplace_back();
2877 if (convertToString(Record, Idx: 0, Result&: BundleTags.back()))
2878 return error(Message: "Invalid operand bundle record");
2879 Record.clear();
2880 }
2881}
2882
2883Error BitcodeReader::parseSyncScopeNames() {
2884 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
2885 return Err;
2886
2887 if (!SSIDs.empty())
2888 return error(Message: "Invalid multiple synchronization scope names blocks");
2889
2890 SmallVector<uint64_t, 64> Record;
2891 while (true) {
2892 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2893 if (!MaybeEntry)
2894 return MaybeEntry.takeError();
2895 BitstreamEntry Entry = MaybeEntry.get();
2896
2897 switch (Entry.Kind) {
2898 case BitstreamEntry::SubBlock: // Handled for us already.
2899 case BitstreamEntry::Error:
2900 return error(Message: "Malformed block");
2901 case BitstreamEntry::EndBlock:
2902 if (SSIDs.empty())
2903 return error(Message: "Invalid empty synchronization scope names block");
2904 return Error::success();
2905 case BitstreamEntry::Record:
2906 // The interesting case.
2907 break;
2908 }
2909
2910 // Synchronization scope names are implicitly mapped to synchronization
2911 // scope IDs by their order.
2912
2913 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2914 if (!MaybeRecord)
2915 return MaybeRecord.takeError();
2916 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
2917 return error(Message: "Invalid sync scope record");
2918
2919 SmallString<16> SSN;
2920 if (convertToString(Record, Idx: 0, Result&: SSN))
2921 return error(Message: "Invalid sync scope record");
2922
2923 SSIDs.push_back(Elt: Context.getOrInsertSyncScopeID(SSN));
2924 Record.clear();
2925 }
2926}
2927
2928/// Associate a value with its name from the given index in the provided record.
2929Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2930 unsigned NameIndex, Triple &TT) {
2931 SmallString<128> ValueName;
2932 if (convertToString(Record, Idx: NameIndex, Result&: ValueName))
2933 return error(Message: "Invalid record");
2934 unsigned ValueID = Record[0];
2935 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2936 return error(Message: "Invalid record");
2937 Value *V = ValueList[ValueID];
2938
2939 StringRef NameStr(ValueName.data(), ValueName.size());
2940 if (NameStr.contains(C: 0))
2941 return error(Message: "Invalid value name");
2942 V->setName(NameStr);
2943 auto *GO = dyn_cast<GlobalObject>(Val: V);
2944 if (GO && ImplicitComdatObjects.contains(V: GO) && TT.supportsCOMDAT())
2945 GO->setComdat(TheModule->getOrInsertComdat(Name: V->getName()));
2946 return V;
2947}
2948
2949/// Helper to note and return the current location, and jump to the given
2950/// offset.
2951static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset,
2952 BitstreamCursor &Stream) {
2953 // Save the current parsing location so we can jump back at the end
2954 // of the VST read.
2955 uint64_t CurrentBit = Stream.GetCurrentBitNo();
2956 if (Error JumpFailed = Stream.JumpToBit(BitNo: Offset * 32))
2957 return std::move(JumpFailed);
2958 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2959 if (!MaybeEntry)
2960 return MaybeEntry.takeError();
2961 if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock ||
2962 MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID)
2963 return error(Message: "Expected value symbol table subblock");
2964 return CurrentBit;
2965}
2966
2967void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2968 Function *F,
2969 ArrayRef<uint64_t> Record) {
2970 // Note that we subtract 1 here because the offset is relative to one word
2971 // before the start of the identification or module block, which was
2972 // historically always the start of the regular bitcode header.
2973 uint64_t FuncWordOffset = Record[1] - 1;
2974 uint64_t FuncBitOffset = FuncWordOffset * 32;
2975 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2976 // Set the LastFunctionBlockBit to point to the last function block.
2977 // Later when parsing is resumed after function materialization,
2978 // we can simply skip that last function block.
2979 if (FuncBitOffset > LastFunctionBlockBit)
2980 LastFunctionBlockBit = FuncBitOffset;
2981}
2982
2983/// Read a new-style GlobalValue symbol table.
2984Error BitcodeReader::parseGlobalValueSymbolTable() {
2985 unsigned FuncBitcodeOffsetDelta =
2986 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2987
2988 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID))
2989 return Err;
2990
2991 SmallVector<uint64_t, 64> Record;
2992 while (true) {
2993 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2994 if (!MaybeEntry)
2995 return MaybeEntry.takeError();
2996 BitstreamEntry Entry = MaybeEntry.get();
2997
2998 switch (Entry.Kind) {
2999 case BitstreamEntry::SubBlock:
3000 case BitstreamEntry::Error:
3001 return error(Message: "Malformed block");
3002 case BitstreamEntry::EndBlock:
3003 return Error::success();
3004 case BitstreamEntry::Record:
3005 break;
3006 }
3007
3008 Record.clear();
3009 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3010 if (!MaybeRecord)
3011 return MaybeRecord.takeError();
3012 switch (MaybeRecord.get()) {
3013 case bitc::VST_CODE_FNENTRY: { // [valueid, offset]
3014 unsigned ValueID = Record[0];
3015 if (ValueID >= ValueList.size() || !ValueList[ValueID])
3016 return error(Message: "Invalid value reference in symbol table");
3017 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
3018 F: cast<Function>(Val: ValueList[ValueID]), Record);
3019 break;
3020 }
3021 }
3022 }
3023}
3024
3025/// Parse the value symbol table at either the current parsing location or
3026/// at the given bit offset if provided.
3027Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
3028 uint64_t CurrentBit;
3029 // Pass in the Offset to distinguish between calling for the module-level
3030 // VST (where we want to jump to the VST offset) and the function-level
3031 // VST (where we don't).
3032 if (Offset > 0) {
3033 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
3034 if (!MaybeCurrentBit)
3035 return MaybeCurrentBit.takeError();
3036 CurrentBit = MaybeCurrentBit.get();
3037 // If this module uses a string table, read this as a module-level VST.
3038 if (UseStrtab) {
3039 if (Error Err = parseGlobalValueSymbolTable())
3040 return Err;
3041 if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit))
3042 return JumpFailed;
3043 return Error::success();
3044 }
3045 // Otherwise, the VST will be in a similar format to a function-level VST,
3046 // and will contain symbol names.
3047 }
3048
3049 // Compute the delta between the bitcode indices in the VST (the word offset
3050 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
3051 // expected by the lazy reader. The reader's EnterSubBlock expects to have
3052 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
3053 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
3054 // just before entering the VST subblock because: 1) the EnterSubBlock
3055 // changes the AbbrevID width; 2) the VST block is nested within the same
3056 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
3057 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
3058 // jump to the FUNCTION_BLOCK using this offset later, we don't want
3059 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
3060 unsigned FuncBitcodeOffsetDelta =
3061 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
3062
3063 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID))
3064 return Err;
3065
3066 SmallVector<uint64_t, 64> Record;
3067
3068 Triple TT(TheModule->getTargetTriple());
3069
3070 // Read all the records for this value table.
3071 SmallString<128> ValueName;
3072
3073 while (true) {
3074 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3075 if (!MaybeEntry)
3076 return MaybeEntry.takeError();
3077 BitstreamEntry Entry = MaybeEntry.get();
3078
3079 switch (Entry.Kind) {
3080 case BitstreamEntry::SubBlock: // Handled for us already.
3081 case BitstreamEntry::Error:
3082 return error(Message: "Malformed block");
3083 case BitstreamEntry::EndBlock:
3084 if (Offset > 0)
3085 if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit))
3086 return JumpFailed;
3087 return Error::success();
3088 case BitstreamEntry::Record:
3089 // The interesting case.
3090 break;
3091 }
3092
3093 // Read a record.
3094 Record.clear();
3095 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3096 if (!MaybeRecord)
3097 return MaybeRecord.takeError();
3098 switch (MaybeRecord.get()) {
3099 default: // Default behavior: unknown type.
3100 break;
3101 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
3102 Expected<Value *> ValOrErr = recordValue(Record, NameIndex: 1, TT);
3103 if (Error Err = ValOrErr.takeError())
3104 return Err;
3105 ValOrErr.get();
3106 break;
3107 }
3108 case bitc::VST_CODE_FNENTRY: {
3109 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
3110 Expected<Value *> ValOrErr = recordValue(Record, NameIndex: 2, TT);
3111 if (Error Err = ValOrErr.takeError())
3112 return Err;
3113 Value *V = ValOrErr.get();
3114
3115 // Ignore function offsets emitted for aliases of functions in older
3116 // versions of LLVM.
3117 if (auto *F = dyn_cast<Function>(Val: V))
3118 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
3119 break;
3120 }
3121 case bitc::VST_CODE_BBENTRY: {
3122 if (convertToString(Record, Idx: 1, Result&: ValueName))
3123 return error(Message: "Invalid bbentry record");
3124 BasicBlock *BB = getBasicBlock(ID: Record[0]);
3125 if (!BB)
3126 return error(Message: "Invalid bbentry record");
3127
3128 BB->setName(ValueName.str());
3129 ValueName.clear();
3130 break;
3131 }
3132 }
3133 }
3134}
3135
3136/// Decode a signed value stored with the sign bit in the LSB for dense VBR
3137/// encoding.
3138uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
3139 if ((V & 1) == 0)
3140 return V >> 1;
3141 if (V != 1)
3142 return -(V >> 1);
3143 // There is no such thing as -0 with integers. "-0" really means MININT.
3144 return 1ULL << 63;
3145}
3146
3147/// Resolve all of the initializers for global values and aliases that we can.
3148Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
3149 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
3150 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist;
3151 std::vector<FunctionOperandInfo> FunctionOperandWorklist;
3152
3153 GlobalInitWorklist.swap(x&: GlobalInits);
3154 IndirectSymbolInitWorklist.swap(x&: IndirectSymbolInits);
3155 FunctionOperandWorklist.swap(x&: FunctionOperands);
3156
3157 while (!GlobalInitWorklist.empty()) {
3158 unsigned ValID = GlobalInitWorklist.back().second;
3159 if (ValID >= ValueList.size()) {
3160 // Not ready to resolve this yet, it requires something later in the file.
3161 GlobalInits.push_back(x: GlobalInitWorklist.back());
3162 } else {
3163 Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID);
3164 if (!MaybeC)
3165 return MaybeC.takeError();
3166 GlobalInitWorklist.back().first->setInitializer(MaybeC.get());
3167 }
3168 GlobalInitWorklist.pop_back();
3169 }
3170
3171 while (!IndirectSymbolInitWorklist.empty()) {
3172 unsigned ValID = IndirectSymbolInitWorklist.back().second;
3173 if (ValID >= ValueList.size()) {
3174 IndirectSymbolInits.push_back(x: IndirectSymbolInitWorklist.back());
3175 } else {
3176 Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID);
3177 if (!MaybeC)
3178 return MaybeC.takeError();
3179 Constant *C = MaybeC.get();
3180 GlobalValue *GV = IndirectSymbolInitWorklist.back().first;
3181 if (auto *GA = dyn_cast<GlobalAlias>(Val: GV)) {
3182 if (C->getType() != GV->getType())
3183 return error(Message: "Alias and aliasee types don't match");
3184 GA->setAliasee(C);
3185 } else if (auto *GI = dyn_cast<GlobalIFunc>(Val: GV)) {
3186 GI->setResolver(C);
3187 } else {
3188 return error(Message: "Expected an alias or an ifunc");
3189 }
3190 }
3191 IndirectSymbolInitWorklist.pop_back();
3192 }
3193
3194 while (!FunctionOperandWorklist.empty()) {
3195 FunctionOperandInfo &Info = FunctionOperandWorklist.back();
3196 if (Info.PersonalityFn) {
3197 unsigned ValID = Info.PersonalityFn - 1;
3198 if (ValID < ValueList.size()) {
3199 Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID);
3200 if (!MaybeC)
3201 return MaybeC.takeError();
3202 Info.F->setPersonalityFn(MaybeC.get());
3203 Info.PersonalityFn = 0;
3204 }
3205 }
3206 if (Info.Prefix) {
3207 unsigned ValID = Info.Prefix - 1;
3208 if (ValID < ValueList.size()) {
3209 Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID);
3210 if (!MaybeC)
3211 return MaybeC.takeError();
3212 Info.F->setPrefixData(MaybeC.get());
3213 Info.Prefix = 0;
3214 }
3215 }
3216 if (Info.Prologue) {
3217 unsigned ValID = Info.Prologue - 1;
3218 if (ValID < ValueList.size()) {
3219 Expected<Constant *> MaybeC = getValueForInitializer(ID: ValID);
3220 if (!MaybeC)
3221 return MaybeC.takeError();
3222 Info.F->setPrologueData(MaybeC.get());
3223 Info.Prologue = 0;
3224 }
3225 }
3226 if (Info.PersonalityFn || Info.Prefix || Info.Prologue)
3227 FunctionOperands.push_back(x: Info);
3228 FunctionOperandWorklist.pop_back();
3229 }
3230
3231 return Error::success();
3232}
3233
3234APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
3235 SmallVector<uint64_t, 8> Words(Vals.size());
3236 transform(Range&: Vals, d_first: Words.begin(),
3237 F: BitcodeReader::decodeSignRotatedValue);
3238
3239 return APInt(TypeBits, Words);
3240}
3241
3242Error BitcodeReader::parseConstants() {
3243 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::CONSTANTS_BLOCK_ID))
3244 return Err;
3245
3246 SmallVector<uint64_t, 64> Record;
3247
3248 // Read all the records for this value table.
3249 Type *CurTy = Type::getInt32Ty(C&: Context);
3250 unsigned Int32TyID = getVirtualTypeID(Ty: CurTy);
3251 unsigned CurTyID = Int32TyID;
3252 Type *CurElemTy = nullptr;
3253 unsigned NextCstNo = ValueList.size();
3254
3255 while (true) {
3256 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3257 if (!MaybeEntry)
3258 return MaybeEntry.takeError();
3259 BitstreamEntry Entry = MaybeEntry.get();
3260
3261 switch (Entry.Kind) {
3262 case BitstreamEntry::SubBlock: // Handled for us already.
3263 case BitstreamEntry::Error:
3264 return error(Message: "Malformed block");
3265 case BitstreamEntry::EndBlock:
3266 if (NextCstNo != ValueList.size())
3267 return error(Message: "Invalid constant reference");
3268 return Error::success();
3269 case BitstreamEntry::Record:
3270 // The interesting case.
3271 break;
3272 }
3273
3274 // Read a record.
3275 Record.clear();
3276 Type *VoidType = Type::getVoidTy(C&: Context);
3277 Value *V = nullptr;
3278 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3279 if (!MaybeBitCode)
3280 return MaybeBitCode.takeError();
3281 switch (unsigned BitCode = MaybeBitCode.get()) {
3282 default: // Default behavior: unknown constant
3283 case bitc::CST_CODE_UNDEF: // UNDEF
3284 V = UndefValue::get(T: CurTy);
3285 break;
3286 case bitc::CST_CODE_POISON: // POISON
3287 V = PoisonValue::get(T: CurTy);
3288 break;
3289 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
3290 if (Record.empty())
3291 return error(Message: "Invalid settype record");
3292 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
3293 return error(Message: "Invalid settype record");
3294 if (TypeList[Record[0]] == VoidType)
3295 return error(Message: "Invalid constant type");
3296 CurTyID = Record[0];
3297 CurTy = TypeList[CurTyID];
3298 CurElemTy = getPtrElementTypeByID(ID: CurTyID);
3299 continue; // Skip the ValueList manipulation.
3300 case bitc::CST_CODE_NULL: // NULL
3301 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
3302 return error(Message: "Invalid type for a constant null value");
3303 if (auto *TETy = dyn_cast<TargetExtType>(Val: CurTy))
3304 if (!TETy->hasProperty(Prop: TargetExtType::HasZeroInit))
3305 return error(Message: "Invalid type for a constant null value");
3306 V = Constant::getNullValue(Ty: CurTy);
3307 break;
3308 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
3309 if (!CurTy->isIntOrIntVectorTy() || Record.empty())
3310 return error(Message: "Invalid integer const record");
3311 V = ConstantInt::getSigned(Ty: CurTy, V: decodeSignRotatedValue(V: Record[0]));
3312 break;
3313 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
3314 if (!CurTy->isIntOrIntVectorTy() || Record.empty())
3315 return error(Message: "Invalid wide integer const record");
3316
3317 auto *ScalarTy = cast<IntegerType>(Val: CurTy->getScalarType());
3318 APInt VInt = readWideAPInt(Vals: Record, TypeBits: ScalarTy->getBitWidth());
3319 V = ConstantInt::get(Ty: CurTy, V: VInt);
3320 break;
3321 }
3322 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
3323 if (Record.empty())
3324 return error(Message: "Invalid float const record");
3325
3326 auto *ScalarTy = CurTy->getScalarType();
3327 if (ScalarTy->isHalfTy())
3328 V = ConstantFP::get(Ty: CurTy, V: APFloat(APFloat::IEEEhalf(),
3329 APInt(16, (uint16_t)Record[0])));
3330 else if (ScalarTy->isBFloatTy())
3331 V = ConstantFP::get(
3332 Ty: CurTy, V: APFloat(APFloat::BFloat(), APInt(16, (uint32_t)Record[0])));
3333 else if (ScalarTy->isFloatTy())
3334 V = ConstantFP::get(Ty: CurTy, V: APFloat(APFloat::IEEEsingle(),
3335 APInt(32, (uint32_t)Record[0])));
3336 else if (ScalarTy->isDoubleTy())
3337 V = ConstantFP::get(
3338 Ty: CurTy, V: APFloat(APFloat::IEEEdouble(), APInt(64, Record[0])));
3339 else if (ScalarTy->isX86_FP80Ty()) {
3340 // Bits are not stored the same way as a normal i80 APInt, compensate.
3341 uint64_t Rearrange[2];
3342 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
3343 Rearrange[1] = Record[0] >> 48;
3344 V = ConstantFP::get(
3345 Ty: CurTy, V: APFloat(APFloat::x87DoubleExtended(), APInt(80, Rearrange)));
3346 } else if (ScalarTy->isFP128Ty())
3347 V = ConstantFP::get(Ty: CurTy,
3348 V: APFloat(APFloat::IEEEquad(), APInt(128, Record)));
3349 else if (ScalarTy->isPPC_FP128Ty())
3350 V = ConstantFP::get(
3351 Ty: CurTy, V: APFloat(APFloat::PPCDoubleDouble(), APInt(128, Record)));
3352 else
3353 V = PoisonValue::get(T: CurTy);
3354 break;
3355 }
3356
3357 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
3358 if (Record.empty())
3359 return error(Message: "Invalid aggregate record");
3360
3361 SmallVector<unsigned, 16> Elts;
3362 llvm::append_range(C&: Elts, R&: Record);
3363
3364 if (isa<StructType>(Val: CurTy)) {
3365 V = BitcodeConstant::create(
3366 A&: Alloc, Ty: CurTy, Info: BitcodeConstant::ConstantStructOpcode, OpIDs: Elts);
3367 } else if (isa<ArrayType>(Val: CurTy)) {
3368 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy,
3369 Info: BitcodeConstant::ConstantArrayOpcode, OpIDs: Elts);
3370 } else if (isa<VectorType>(Val: CurTy)) {
3371 V = BitcodeConstant::create(
3372 A&: Alloc, Ty: CurTy, Info: BitcodeConstant::ConstantVectorOpcode, OpIDs: Elts);
3373 } else {
3374 V = PoisonValue::get(T: CurTy);
3375 }
3376 break;
3377 }
3378 case bitc::CST_CODE_STRING: // STRING: [values]
3379 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
3380 if (Record.empty())
3381 return error(Message: "Invalid string record");
3382
3383 SmallString<16> Elts(Record.begin(), Record.end());
3384 V = ConstantDataArray::getString(Context, Initializer: Elts,
3385 AddNull: BitCode == bitc::CST_CODE_CSTRING);
3386 break;
3387 }
3388 case bitc::CST_CODE_DATA: {// DATA: [n x value]
3389 if (Record.empty())
3390 return error(Message: "Invalid data record");
3391
3392 Type *EltTy;
3393 if (auto *Array = dyn_cast<ArrayType>(Val: CurTy))
3394 EltTy = Array->getElementType();
3395 else
3396 EltTy = cast<VectorType>(Val: CurTy)->getElementType();
3397 if (EltTy->isIntegerTy(Bitwidth: 8)) {
3398 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
3399 if (isa<VectorType>(Val: CurTy))
3400 V = ConstantDataVector::get(Context, Elts);
3401 else
3402 V = ConstantDataArray::get(Context, Elts);
3403 } else if (EltTy->isIntegerTy(Bitwidth: 16)) {
3404 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3405 if (isa<VectorType>(Val: CurTy))
3406 V = ConstantDataVector::get(Context, Elts);
3407 else
3408 V = ConstantDataArray::get(Context, Elts);
3409 } else if (EltTy->isIntegerTy(Bitwidth: 32)) {
3410 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3411 if (isa<VectorType>(Val: CurTy))
3412 V = ConstantDataVector::get(Context, Elts);
3413 else
3414 V = ConstantDataArray::get(Context, Elts);
3415 } else if (EltTy->isIntegerTy(Bitwidth: 64)) {
3416 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3417 if (isa<VectorType>(Val: CurTy))
3418 V = ConstantDataVector::get(Context, Elts);
3419 else
3420 V = ConstantDataArray::get(Context, Elts);
3421 } else if (EltTy->isHalfTy()) {
3422 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3423 if (isa<VectorType>(Val: CurTy))
3424 V = ConstantDataVector::getFP(ElementType: EltTy, Elts);
3425 else
3426 V = ConstantDataArray::getFP(ElementType: EltTy, Elts);
3427 } else if (EltTy->isBFloatTy()) {
3428 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3429 if (isa<VectorType>(Val: CurTy))
3430 V = ConstantDataVector::getFP(ElementType: EltTy, Elts);
3431 else
3432 V = ConstantDataArray::getFP(ElementType: EltTy, Elts);
3433 } else if (EltTy->isFloatTy()) {
3434 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3435 if (isa<VectorType>(Val: CurTy))
3436 V = ConstantDataVector::getFP(ElementType: EltTy, Elts);
3437 else
3438 V = ConstantDataArray::getFP(ElementType: EltTy, Elts);
3439 } else if (EltTy->isDoubleTy()) {
3440 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3441 if (isa<VectorType>(Val: CurTy))
3442 V = ConstantDataVector::getFP(ElementType: EltTy, Elts);
3443 else
3444 V = ConstantDataArray::getFP(ElementType: EltTy, Elts);
3445 } else {
3446 return error(Message: "Invalid type for value");
3447 }
3448 break;
3449 }
3450 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
3451 if (Record.size() < 2)
3452 return error(Message: "Invalid unary op constexpr record");
3453 int Opc = getDecodedUnaryOpcode(Val: Record[0], Ty: CurTy);
3454 if (Opc < 0) {
3455 V = PoisonValue::get(T: CurTy); // Unknown unop.
3456 } else {
3457 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Opc, OpIDs: (unsigned)Record[1]);
3458 }
3459 break;
3460 }
3461 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
3462 if (Record.size() < 3)
3463 return error(Message: "Invalid binary op constexpr record");
3464 int Opc = getDecodedBinaryOpcode(Val: Record[0], Ty: CurTy);
3465 if (Opc < 0) {
3466 V = PoisonValue::get(T: CurTy); // Unknown binop.
3467 } else {
3468 uint8_t Flags = 0;
3469 if (Record.size() >= 4) {
3470 if (Opc == Instruction::Add ||
3471 Opc == Instruction::Sub ||
3472 Opc == Instruction::Mul ||
3473 Opc == Instruction::Shl) {
3474 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3475 Flags |= OverflowingBinaryOperator::NoSignedWrap;
3476 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3477 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
3478 } else if (Opc == Instruction::SDiv ||
3479 Opc == Instruction::UDiv ||
3480 Opc == Instruction::LShr ||
3481 Opc == Instruction::AShr) {
3482 if (Record[3] & (1 << bitc::PEO_EXACT))
3483 Flags |= PossiblyExactOperator::IsExact;
3484 }
3485 }
3486 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: {(uint8_t)Opc, Flags},
3487 OpIDs: {(unsigned)Record[1], (unsigned)Record[2]});
3488 }
3489 break;
3490 }
3491 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
3492 if (Record.size() < 3)
3493 return error(Message: "Invalid cast constexpr record");
3494 int Opc = getDecodedCastOpcode(Val: Record[0]);
3495 if (Opc < 0) {
3496 V = PoisonValue::get(T: CurTy); // Unknown cast.
3497 } else {
3498 unsigned OpTyID = Record[1];
3499 Type *OpTy = getTypeByID(ID: OpTyID);
3500 if (!OpTy)
3501 return error(Message: "Invalid cast constexpr record");
3502 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Opc, OpIDs: (unsigned)Record[2]);
3503 }
3504 break;
3505 }
3506 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
3507 case bitc::CST_CODE_CE_GEP_OLD: // [ty, n x operands]
3508 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD: // [ty, flags, n x
3509 // operands]
3510 case bitc::CST_CODE_CE_GEP: // [ty, flags, n x operands]
3511 case bitc::CST_CODE_CE_GEP_WITH_INRANGE: { // [ty, flags, start, end, n x
3512 // operands]
3513 if (Record.size() < 2)
3514 return error(Message: "Constant GEP record must have at least two elements");
3515 unsigned OpNum = 0;
3516 Type *PointeeType = nullptr;
3517 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD ||
3518 BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE ||
3519 BitCode == bitc::CST_CODE_CE_GEP || Record.size() % 2)
3520 PointeeType = getTypeByID(ID: Record[OpNum++]);
3521
3522 uint64_t Flags = 0;
3523 std::optional<ConstantRange> InRange;
3524 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX_OLD) {
3525 uint64_t Op = Record[OpNum++];
3526 Flags = Op & 1; // inbounds
3527 unsigned InRangeIndex = Op >> 1;
3528 // "Upgrade" inrange by dropping it. The feature is too niche to
3529 // bother.
3530 (void)InRangeIndex;
3531 } else if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE) {
3532 Flags = Record[OpNum++];
3533 Expected<ConstantRange> MaybeInRange =
3534 readBitWidthAndConstantRange(Record, OpNum);
3535 if (!MaybeInRange)
3536 return MaybeInRange.takeError();
3537 InRange = MaybeInRange.get();
3538 } else if (BitCode == bitc::CST_CODE_CE_GEP) {
3539 Flags = Record[OpNum++];
3540 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
3541 Flags = (1 << bitc::GEP_INBOUNDS);
3542
3543 SmallVector<unsigned, 16> Elts;
3544 unsigned BaseTypeID = Record[OpNum];
3545 while (OpNum != Record.size()) {
3546 unsigned ElTyID = Record[OpNum++];
3547 Type *ElTy = getTypeByID(ID: ElTyID);
3548 if (!ElTy)
3549 return error(Message: "Invalid getelementptr constexpr record");
3550 Elts.push_back(Elt: Record[OpNum++]);
3551 }
3552
3553 if (Elts.size() < 1)
3554 return error(Message: "Invalid gep with no operands");
3555
3556 Type *BaseType = getTypeByID(ID: BaseTypeID);
3557 if (isa<VectorType>(Val: BaseType)) {
3558 BaseTypeID = getContainedTypeID(ID: BaseTypeID, Idx: 0);
3559 BaseType = getTypeByID(ID: BaseTypeID);
3560 }
3561
3562 PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(Val: BaseType);
3563 if (!OrigPtrTy)
3564 return error(Message: "GEP base operand must be pointer or vector of pointer");
3565
3566 if (!PointeeType) {
3567 PointeeType = getPtrElementTypeByID(ID: BaseTypeID);
3568 if (!PointeeType)
3569 return error(Message: "Missing element type for old-style constant GEP");
3570 }
3571
3572 V = BitcodeConstant::create(
3573 A&: Alloc, Ty: CurTy,
3574 Info: {Instruction::GetElementPtr, uint8_t(Flags), PointeeType, InRange},
3575 OpIDs: Elts);
3576 break;
3577 }
3578 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
3579 if (Record.size() < 3)
3580 return error(Message: "Invalid select constexpr record");
3581
3582 V = BitcodeConstant::create(
3583 A&: Alloc, Ty: CurTy, Info: Instruction::Select,
3584 OpIDs: {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3585 break;
3586 }
3587 case bitc::CST_CODE_CE_EXTRACTELT
3588 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
3589 if (Record.size() < 3)
3590 return error(Message: "Invalid extractelement constexpr record");
3591 unsigned OpTyID = Record[0];
3592 VectorType *OpTy =
3593 dyn_cast_or_null<VectorType>(Val: getTypeByID(ID: OpTyID));
3594 if (!OpTy)
3595 return error(Message: "Invalid extractelement constexpr record");
3596 unsigned IdxRecord;
3597 if (Record.size() == 4) {
3598 unsigned IdxTyID = Record[2];
3599 Type *IdxTy = getTypeByID(ID: IdxTyID);
3600 if (!IdxTy)
3601 return error(Message: "Invalid extractelement constexpr record");
3602 IdxRecord = Record[3];
3603 } else {
3604 // Deprecated, but still needed to read old bitcode files.
3605 IdxRecord = Record[2];
3606 }
3607 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: Instruction::ExtractElement,
3608 OpIDs: {(unsigned)Record[1], IdxRecord});
3609 break;
3610 }
3611 case bitc::CST_CODE_CE_INSERTELT
3612 : { // CE_INSERTELT: [opval, opval, opty, opval]
3613 VectorType *OpTy = dyn_cast<VectorType>(Val: CurTy);
3614 if (Record.size() < 3 || !OpTy)
3615 return error(Message: "Invalid insertelement constexpr record");
3616 unsigned IdxRecord;
3617 if (Record.size() == 4) {
3618 unsigned IdxTyID = Record[2];
3619 Type *IdxTy = getTypeByID(ID: IdxTyID);
3620 if (!IdxTy)
3621 return error(Message: "Invalid insertelement constexpr record");
3622 IdxRecord = Record[3];
3623 } else {
3624 // Deprecated, but still needed to read old bitcode files.
3625 IdxRecord = Record[2];
3626 }
3627 V = BitcodeConstant::create(
3628 A&: Alloc, Ty: CurTy, Info: Instruction::InsertElement,
3629 OpIDs: {(unsigned)Record[0], (unsigned)Record[1], IdxRecord});
3630 break;
3631 }
3632 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
3633 VectorType *OpTy = dyn_cast<VectorType>(Val: CurTy);
3634 if (Record.size() < 3 || !OpTy)
3635 return error(Message: "Invalid shufflevector constexpr record");
3636 V = BitcodeConstant::create(
3637 A&: Alloc, Ty: CurTy, Info: Instruction::ShuffleVector,
3638 OpIDs: {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]});
3639 break;
3640 }
3641 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
3642 VectorType *RTy = dyn_cast<VectorType>(Val: CurTy);
3643 VectorType *OpTy =
3644 dyn_cast_or_null<VectorType>(Val: getTypeByID(ID: Record[0]));
3645 if (Record.size() < 4 || !RTy || !OpTy)
3646 return error(Message: "Invalid shufflevector constexpr record");
3647 V = BitcodeConstant::create(
3648 A&: Alloc, Ty: CurTy, Info: Instruction::ShuffleVector,
3649 OpIDs: {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]});
3650 break;
3651 }
3652 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
3653 if (Record.size() < 4)
3654 return error(Message: "Invalid cmp constexpt record");
3655 unsigned OpTyID = Record[0];
3656 Type *OpTy = getTypeByID(ID: OpTyID);
3657 if (!OpTy)
3658 return error(Message: "Invalid cmp constexpr record");
3659 V = BitcodeConstant::create(
3660 A&: Alloc, Ty: CurTy,
3661 Info: {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp
3662 : Instruction::ICmp),
3663 (uint8_t)Record[3]},
3664 OpIDs: {(unsigned)Record[1], (unsigned)Record[2]});
3665 break;
3666 }
3667 // This maintains backward compatibility, pre-asm dialect keywords.
3668 // Deprecated, but still needed to read old bitcode files.
3669 case bitc::CST_CODE_INLINEASM_OLD: {
3670 if (Record.size() < 2)
3671 return error(Message: "Invalid inlineasm record");
3672 std::string AsmStr, ConstrStr;
3673 bool HasSideEffects = Record[0] & 1;
3674 bool IsAlignStack = Record[0] >> 1;
3675 unsigned AsmStrSize = Record[1];
3676 if (2+AsmStrSize >= Record.size())
3677 return error(Message: "Invalid inlineasm record");
3678 unsigned ConstStrSize = Record[2+AsmStrSize];
3679 if (3+AsmStrSize+ConstStrSize > Record.size())
3680 return error(Message: "Invalid inlineasm record");
3681
3682 for (unsigned i = 0; i != AsmStrSize; ++i)
3683 AsmStr += (char)Record[2+i];
3684 for (unsigned i = 0; i != ConstStrSize; ++i)
3685 ConstrStr += (char)Record[3+AsmStrSize+i];
3686 UpgradeInlineAsmString(AsmStr: &AsmStr);
3687 if (!CurElemTy)
3688 return error(Message: "Missing element type for old-style inlineasm");
3689 V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr,
3690 hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack);
3691 break;
3692 }
3693 // This version adds support for the asm dialect keywords (e.g.,
3694 // inteldialect).
3695 case bitc::CST_CODE_INLINEASM_OLD2: {
3696 if (Record.size() < 2)
3697 return error(Message: "Invalid inlineasm record");
3698 std::string AsmStr, ConstrStr;
3699 bool HasSideEffects = Record[0] & 1;
3700 bool IsAlignStack = (Record[0] >> 1) & 1;
3701 unsigned AsmDialect = Record[0] >> 2;
3702 unsigned AsmStrSize = Record[1];
3703 if (2+AsmStrSize >= Record.size())
3704 return error(Message: "Invalid inlineasm record");
3705 unsigned ConstStrSize = Record[2+AsmStrSize];
3706 if (3+AsmStrSize+ConstStrSize > Record.size())
3707 return error(Message: "Invalid inlineasm record");
3708
3709 for (unsigned i = 0; i != AsmStrSize; ++i)
3710 AsmStr += (char)Record[2+i];
3711 for (unsigned i = 0; i != ConstStrSize; ++i)
3712 ConstrStr += (char)Record[3+AsmStrSize+i];
3713 UpgradeInlineAsmString(AsmStr: &AsmStr);
3714 if (!CurElemTy)
3715 return error(Message: "Missing element type for old-style inlineasm");
3716 V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr,
3717 hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack,
3718 asmDialect: InlineAsm::AsmDialect(AsmDialect));
3719 break;
3720 }
3721 // This version adds support for the unwind keyword.
3722 case bitc::CST_CODE_INLINEASM_OLD3: {
3723 if (Record.size() < 2)
3724 return error(Message: "Invalid inlineasm record");
3725 unsigned OpNum = 0;
3726 std::string AsmStr, ConstrStr;
3727 bool HasSideEffects = Record[OpNum] & 1;
3728 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3729 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3730 bool CanThrow = (Record[OpNum] >> 3) & 1;
3731 ++OpNum;
3732 unsigned AsmStrSize = Record[OpNum];
3733 ++OpNum;
3734 if (OpNum + AsmStrSize >= Record.size())
3735 return error(Message: "Invalid inlineasm record");
3736 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3737 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3738 return error(Message: "Invalid inlineasm record");
3739
3740 for (unsigned i = 0; i != AsmStrSize; ++i)
3741 AsmStr += (char)Record[OpNum + i];
3742 ++OpNum;
3743 for (unsigned i = 0; i != ConstStrSize; ++i)
3744 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3745 UpgradeInlineAsmString(AsmStr: &AsmStr);
3746 if (!CurElemTy)
3747 return error(Message: "Missing element type for old-style inlineasm");
3748 V = InlineAsm::get(Ty: cast<FunctionType>(Val: CurElemTy), AsmString: AsmStr, Constraints: ConstrStr,
3749 hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack,
3750 asmDialect: InlineAsm::AsmDialect(AsmDialect), canThrow: CanThrow);
3751 break;
3752 }
3753 // This version adds explicit function type.
3754 case bitc::CST_CODE_INLINEASM: {
3755 if (Record.size() < 3)
3756 return error(Message: "Invalid inlineasm record");
3757 unsigned OpNum = 0;
3758 auto *FnTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: Record[OpNum]));
3759 ++OpNum;
3760 if (!FnTy)
3761 return error(Message: "Invalid inlineasm record");
3762 std::string AsmStr, ConstrStr;
3763 bool HasSideEffects = Record[OpNum] & 1;
3764 bool IsAlignStack = (Record[OpNum] >> 1) & 1;
3765 unsigned AsmDialect = (Record[OpNum] >> 2) & 1;
3766 bool CanThrow = (Record[OpNum] >> 3) & 1;
3767 ++OpNum;
3768 unsigned AsmStrSize = Record[OpNum];
3769 ++OpNum;
3770 if (OpNum + AsmStrSize >= Record.size())
3771 return error(Message: "Invalid inlineasm record");
3772 unsigned ConstStrSize = Record[OpNum + AsmStrSize];
3773 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size())
3774 return error(Message: "Invalid inlineasm record");
3775
3776 for (unsigned i = 0; i != AsmStrSize; ++i)
3777 AsmStr += (char)Record[OpNum + i];
3778 ++OpNum;
3779 for (unsigned i = 0; i != ConstStrSize; ++i)
3780 ConstrStr += (char)Record[OpNum + AsmStrSize + i];
3781 UpgradeInlineAsmString(AsmStr: &AsmStr);
3782 V = InlineAsm::get(Ty: FnTy, AsmString: AsmStr, Constraints: ConstrStr, hasSideEffects: HasSideEffects, isAlignStack: IsAlignStack,
3783 asmDialect: InlineAsm::AsmDialect(AsmDialect), canThrow: CanThrow);
3784 break;
3785 }
3786 case bitc::CST_CODE_BLOCKADDRESS:{
3787 if (Record.size() < 3)
3788 return error(Message: "Invalid blockaddress record");
3789 unsigned FnTyID = Record[0];
3790 Type *FnTy = getTypeByID(ID: FnTyID);
3791 if (!FnTy)
3792 return error(Message: "Invalid blockaddress record");
3793 V = BitcodeConstant::create(
3794 A&: Alloc, Ty: CurTy,
3795 Info: {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]},
3796 OpIDs: Record[1]);
3797 break;
3798 }
3799 case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: {
3800 if (Record.size() < 2)
3801 return error(Message: "Invalid dso_local record");
3802 unsigned GVTyID = Record[0];
3803 Type *GVTy = getTypeByID(ID: GVTyID);
3804 if (!GVTy)
3805 return error(Message: "Invalid dso_local record");
3806 V = BitcodeConstant::create(
3807 A&: Alloc, Ty: CurTy, Info: BitcodeConstant::DSOLocalEquivalentOpcode, OpIDs: Record[1]);
3808 break;
3809 }
3810 case bitc::CST_CODE_NO_CFI_VALUE: {
3811 if (Record.size() < 2)
3812 return error(Message: "Invalid no_cfi record");
3813 unsigned GVTyID = Record[0];
3814 Type *GVTy = getTypeByID(ID: GVTyID);
3815 if (!GVTy)
3816 return error(Message: "Invalid no_cfi record");
3817 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy, Info: BitcodeConstant::NoCFIOpcode,
3818 OpIDs: Record[1]);
3819 break;
3820 }
3821 case bitc::CST_CODE_PTRAUTH: {
3822 if (Record.size() < 4)
3823 return error(Message: "Invalid ptrauth record");
3824 // Ptr, Key, Disc, AddrDisc
3825 V = BitcodeConstant::create(A&: Alloc, Ty: CurTy,
3826 Info: BitcodeConstant::ConstantPtrAuthOpcode,
3827 OpIDs: {(unsigned)Record[0], (unsigned)Record[1],
3828 (unsigned)Record[2], (unsigned)Record[3]});
3829 break;
3830 }
3831 case bitc::CST_CODE_PTRAUTH2: {
3832 if (Record.size() < 5)
3833 return error(Message: "Invalid ptrauth record");
3834 // Ptr, Key, Disc, AddrDisc, DeactivationSymbol
3835 V = BitcodeConstant::create(
3836 A&: Alloc, Ty: CurTy, Info: BitcodeConstant::ConstantPtrAuthOpcode,
3837 OpIDs: {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],
3838 (unsigned)Record[3], (unsigned)Record[4]});
3839 break;
3840 }
3841 }
3842
3843 assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");
3844 if (Error Err = ValueList.assignValue(Idx: NextCstNo, V, TypeID: CurTyID))
3845 return Err;
3846 ++NextCstNo;
3847 }
3848}
3849
3850Error BitcodeReader::parseUseLists() {
3851 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::USELIST_BLOCK_ID))
3852 return Err;
3853
3854 // Read all the records.
3855 SmallVector<uint64_t, 64> Record;
3856
3857 while (true) {
3858 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
3859 if (!MaybeEntry)
3860 return MaybeEntry.takeError();
3861 BitstreamEntry Entry = MaybeEntry.get();
3862
3863 switch (Entry.Kind) {
3864 case BitstreamEntry::SubBlock: // Handled for us already.
3865 case BitstreamEntry::Error:
3866 return error(Message: "Malformed block");
3867 case BitstreamEntry::EndBlock:
3868 return Error::success();
3869 case BitstreamEntry::Record:
3870 // The interesting case.
3871 break;
3872 }
3873
3874 // Read a use list record.
3875 Record.clear();
3876 bool IsBB = false;
3877 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3878 if (!MaybeRecord)
3879 return MaybeRecord.takeError();
3880 switch (MaybeRecord.get()) {
3881 default: // Default behavior: unknown type.
3882 break;
3883 case bitc::USELIST_CODE_BB:
3884 IsBB = true;
3885 [[fallthrough]];
3886 case bitc::USELIST_CODE_DEFAULT: {
3887 unsigned RecordLength = Record.size();
3888 if (RecordLength < 3)
3889 // Records should have at least an ID and two indexes.
3890 return error(Message: "Invalid uselist record");
3891 unsigned ID = Record.pop_back_val();
3892
3893 Value *V;
3894 if (IsBB) {
3895 assert(ID < FunctionBBs.size() && "Basic block not found");
3896 V = FunctionBBs[ID];
3897 } else
3898 V = ValueList[ID];
3899
3900 if (!V->hasUseList())
3901 break;
3902
3903 unsigned NumUses = 0;
3904 SmallDenseMap<const Use *, unsigned, 16> Order;
3905 for (const Use &U : V->materialized_uses()) {
3906 if (++NumUses > Record.size())
3907 break;
3908 Order[&U] = Record[NumUses - 1];
3909 }
3910 if (Order.size() != Record.size() || NumUses > Record.size())
3911 // Mismatches can happen if the functions are being materialized lazily
3912 // (out-of-order), or a value has been upgraded.
3913 break;
3914
3915 V->sortUseList(Cmp: [&](const Use &L, const Use &R) {
3916 return Order.lookup(Val: &L) < Order.lookup(Val: &R);
3917 });
3918 break;
3919 }
3920 }
3921 }
3922}
3923
3924/// When we see the block for metadata, remember where it is and then skip it.
3925/// This lets us lazily deserialize the metadata.
3926Error BitcodeReader::rememberAndSkipMetadata() {
3927 // Save the current stream state.
3928 uint64_t CurBit = Stream.GetCurrentBitNo();
3929 DeferredMetadataInfo.push_back(x: CurBit);
3930
3931 // Skip over the block for now.
3932 if (Error Err = Stream.SkipBlock())
3933 return Err;
3934 return Error::success();
3935}
3936
3937Error BitcodeReader::materializeMetadata() {
3938 for (uint64_t BitPos : DeferredMetadataInfo) {
3939 // Move the bit stream to the saved position.
3940 if (Error JumpFailed = Stream.JumpToBit(BitNo: BitPos))
3941 return JumpFailed;
3942 if (Error Err = MDLoader->parseModuleMetadata())
3943 return Err;
3944 }
3945
3946 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
3947 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
3948 // multiple times.
3949 if (!TheModule->getNamedMetadata(Name: "llvm.linker.options")) {
3950 if (Metadata *Val = TheModule->getModuleFlag(Key: "Linker Options")) {
3951 NamedMDNode *LinkerOpts =
3952 TheModule->getOrInsertNamedMetadata(Name: "llvm.linker.options");
3953 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
3954 LinkerOpts->addOperand(M: cast<MDNode>(Val: MDOptions));
3955 }
3956 }
3957
3958 DeferredMetadataInfo.clear();
3959 return Error::success();
3960}
3961
3962void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3963
3964/// When we see the block for a function body, remember where it is and then
3965/// skip it. This lets us lazily deserialize the functions.
3966Error BitcodeReader::rememberAndSkipFunctionBody() {
3967 // Get the function we are talking about.
3968 if (FunctionsWithBodies.empty())
3969 return error(Message: "Insufficient function protos");
3970
3971 Function *Fn = FunctionsWithBodies.back();
3972 FunctionsWithBodies.pop_back();
3973
3974 // Save the current stream state.
3975 uint64_t CurBit = Stream.GetCurrentBitNo();
3976 assert(
3977 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3978 "Mismatch between VST and scanned function offsets");
3979 DeferredFunctionInfo[Fn] = CurBit;
3980
3981 // Skip over the function block for now.
3982 if (Error Err = Stream.SkipBlock())
3983 return Err;
3984 return Error::success();
3985}
3986
3987Error BitcodeReader::globalCleanup() {
3988 // Patch the initializers for globals and aliases up.
3989 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3990 return Err;
3991 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3992 return error(Message: "Malformed global initializer set");
3993
3994 // Look for intrinsic functions which need to be upgraded at some point
3995 // and functions that need to have their function attributes upgraded.
3996 for (Function &F : *TheModule) {
3997 MDLoader->upgradeDebugIntrinsics(F);
3998 Function *NewFn;
3999 if (UpgradeIntrinsicFunction(F: &F, NewFn))
4000 UpgradedIntrinsics[&F] = NewFn;
4001 // Look for functions that rely on old function attribute behavior.
4002 UpgradeFunctionAttributes(F);
4003 }
4004
4005 // Look for global variables which need to be renamed.
4006 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
4007 for (GlobalVariable &GV : TheModule->globals())
4008 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(GV: &GV))
4009 UpgradedVariables.emplace_back(args: &GV, args&: Upgraded);
4010 for (auto &Pair : UpgradedVariables) {
4011 Pair.first->eraseFromParent();
4012 TheModule->insertGlobalVariable(GV: Pair.second);
4013 }
4014
4015 // Force deallocation of memory for these vectors to favor the client that
4016 // want lazy deserialization.
4017 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(x&: GlobalInits);
4018 std::vector<std::pair<GlobalValue *, unsigned>>().swap(x&: IndirectSymbolInits);
4019 return Error::success();
4020}
4021
4022/// Support for lazy parsing of function bodies. This is required if we
4023/// either have an old bitcode file without a VST forward declaration record,
4024/// or if we have an anonymous function being materialized, since anonymous
4025/// functions do not have a name and are therefore not in the VST.
4026Error BitcodeReader::rememberAndSkipFunctionBodies() {
4027 if (Error JumpFailed = Stream.JumpToBit(BitNo: NextUnreadBit))
4028 return JumpFailed;
4029
4030 if (Stream.AtEndOfStream())
4031 return error(Message: "Could not find function in stream");
4032
4033 if (!SeenFirstFunctionBody)
4034 return error(Message: "Trying to materialize functions before seeing function blocks");
4035
4036 // An old bitcode file with the symbol table at the end would have
4037 // finished the parse greedily.
4038 assert(SeenValueSymbolTable);
4039
4040 while (true) {
4041 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4042 if (!MaybeEntry)
4043 return MaybeEntry.takeError();
4044 llvm::BitstreamEntry Entry = MaybeEntry.get();
4045
4046 switch (Entry.Kind) {
4047 default:
4048 return error(Message: "Expect SubBlock");
4049 case BitstreamEntry::SubBlock:
4050 switch (Entry.ID) {
4051 default:
4052 return error(Message: "Expect function block");
4053 case bitc::FUNCTION_BLOCK_ID:
4054 if (Error Err = rememberAndSkipFunctionBody())
4055 return Err;
4056 NextUnreadBit = Stream.GetCurrentBitNo();
4057 return Error::success();
4058 }
4059 }
4060 }
4061}
4062
4063Error BitcodeReaderBase::readBlockInfo() {
4064 Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
4065 Stream.ReadBlockInfoBlock();
4066 if (!MaybeNewBlockInfo)
4067 return MaybeNewBlockInfo.takeError();
4068 std::optional<BitstreamBlockInfo> NewBlockInfo =
4069 std::move(MaybeNewBlockInfo.get());
4070 if (!NewBlockInfo)
4071 return error(Message: "Malformed block");
4072 BlockInfo = std::move(*NewBlockInfo);
4073 return Error::success();
4074}
4075
4076Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
4077 // v1: [selection_kind, name]
4078 // v2: [strtab_offset, strtab_size, selection_kind]
4079 StringRef Name;
4080 std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record);
4081
4082 if (Record.empty())
4083 return error(Message: "Invalid comdat record");
4084 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Val: Record[0]);
4085 std::string OldFormatName;
4086 if (!UseStrtab) {
4087 if (Record.size() < 2)
4088 return error(Message: "Invalid comdat record");
4089 unsigned ComdatNameSize = Record[1];
4090 if (ComdatNameSize > Record.size() - 2)
4091 return error(Message: "Comdat name size too large");
4092 OldFormatName.reserve(res_arg: ComdatNameSize);
4093 for (unsigned i = 0; i != ComdatNameSize; ++i)
4094 OldFormatName += (char)Record[2 + i];
4095 Name = OldFormatName;
4096 }
4097 Comdat *C = TheModule->getOrInsertComdat(Name);
4098 C->setSelectionKind(SK);
4099 ComdatList.push_back(x: C);
4100 return Error::success();
4101}
4102
4103static void inferDSOLocal(GlobalValue *GV) {
4104 // infer dso_local from linkage and visibility if it is not encoded.
4105 if (GV->hasLocalLinkage() ||
4106 (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()))
4107 GV->setDSOLocal(true);
4108}
4109
4110GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) {
4111 GlobalValue::SanitizerMetadata Meta;
4112 if (V & (1 << 0))
4113 Meta.NoAddress = true;
4114 if (V & (1 << 1))
4115 Meta.NoHWAddress = true;
4116 if (V & (1 << 2))
4117 Meta.Memtag = true;
4118 if (V & (1 << 3))
4119 Meta.IsDynInit = true;
4120 return Meta;
4121}
4122
4123Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
4124 // v1: [pointer type, isconst, initid, linkage, alignment, section,
4125 // visibility, threadlocal, unnamed_addr, externally_initialized,
4126 // dllstorageclass, comdat, attributes, preemption specifier,
4127 // partition strtab offset, partition strtab size] (name in VST)
4128 // v2: [strtab_offset, strtab_size, v1]
4129 // v3: [v2, code_model]
4130 StringRef Name;
4131 std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record);
4132
4133 if (Record.size() < 6)
4134 return error(Message: "Invalid global variable record");
4135 unsigned TyID = Record[0];
4136 Type *Ty = getTypeByID(ID: TyID);
4137 if (!Ty)
4138 return error(Message: "Invalid global variable record");
4139 bool isConstant = Record[1] & 1;
4140 bool explicitType = Record[1] & 2;
4141 unsigned AddressSpace;
4142 if (explicitType) {
4143 AddressSpace = Record[1] >> 2;
4144 } else {
4145 if (!Ty->isPointerTy())
4146 return error(Message: "Invalid type for value");
4147 AddressSpace = cast<PointerType>(Val: Ty)->getAddressSpace();
4148 TyID = getContainedTypeID(ID: TyID);
4149 Ty = getTypeByID(ID: TyID);
4150 if (!Ty)
4151 return error(Message: "Missing element type for old-style global");
4152 }
4153
4154 uint64_t RawLinkage = Record[3];
4155 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Val: RawLinkage);
4156 MaybeAlign Alignment;
4157 if (Error Err = parseAlignmentValue(Exponent: Record[4], Alignment))
4158 return Err;
4159 std::string Section;
4160 if (Record[5]) {
4161 if (Record[5] - 1 >= SectionTable.size())
4162 return error(Message: "Invalid ID");
4163 Section = SectionTable[Record[5] - 1];
4164 }
4165 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
4166 // Local linkage must have default visibility.
4167 // auto-upgrade `hidden` and `protected` for old bitcode.
4168 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
4169 Visibility = getDecodedVisibility(Val: Record[6]);
4170
4171 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
4172 if (Record.size() > 7)
4173 TLM = getDecodedThreadLocalMode(Val: Record[7]);
4174
4175 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
4176 if (Record.size() > 8)
4177 UnnamedAddr = getDecodedUnnamedAddrType(Val: Record[8]);
4178
4179 bool ExternallyInitialized = false;
4180 if (Record.size() > 9)
4181 ExternallyInitialized = Record[9];
4182
4183 GlobalVariable *NewGV =
4184 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
4185 nullptr, TLM, AddressSpace, ExternallyInitialized);
4186 if (Alignment)
4187 NewGV->setAlignment(*Alignment);
4188 if (!Section.empty())
4189 NewGV->setSection(Section);
4190 NewGV->setVisibility(Visibility);
4191 NewGV->setUnnamedAddr(UnnamedAddr);
4192
4193 if (Record.size() > 10) {
4194 // A GlobalValue with local linkage cannot have a DLL storage class.
4195 if (!NewGV->hasLocalLinkage()) {
4196 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Val: Record[10]));
4197 }
4198 } else {
4199 upgradeDLLImportExportLinkage(GV: NewGV, Val: RawLinkage);
4200 }
4201
4202 ValueList.push_back(V: NewGV, TypeID: getVirtualTypeID(Ty: NewGV->getType(), ChildTypeIDs: TyID));
4203
4204 // Remember which value to use for the global initializer.
4205 if (unsigned InitID = Record[2])
4206 GlobalInits.push_back(x: std::make_pair(x&: NewGV, y: InitID - 1));
4207
4208 if (Record.size() > 11) {
4209 if (unsigned ComdatID = Record[11]) {
4210 if (ComdatID > ComdatList.size())
4211 return error(Message: "Invalid global variable comdat ID");
4212 NewGV->setComdat(ComdatList[ComdatID - 1]);
4213 }
4214 } else if (hasImplicitComdat(Val: RawLinkage)) {
4215 ImplicitComdatObjects.insert(V: NewGV);
4216 }
4217
4218 if (Record.size() > 12) {
4219 auto AS = getAttributes(i: Record[12]).getFnAttrs();
4220 NewGV->setAttributes(AS);
4221 }
4222
4223 if (Record.size() > 13) {
4224 NewGV->setDSOLocal(getDecodedDSOLocal(Val: Record[13]));
4225 }
4226 inferDSOLocal(GV: NewGV);
4227
4228 // Check whether we have enough values to read a partition name.
4229 if (Record.size() > 15)
4230 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
4231
4232 if (Record.size() > 16 && Record[16]) {
4233 llvm::GlobalValue::SanitizerMetadata Meta =
4234 deserializeSanitizerMetadata(V: Record[16]);
4235 NewGV->setSanitizerMetadata(Meta);
4236 }
4237
4238 if (Record.size() > 17 && Record[17]) {
4239 if (auto CM = getDecodedCodeModel(Val: Record[17]))
4240 NewGV->setCodeModel(*CM);
4241 else
4242 return error(Message: "Invalid global variable code model");
4243 }
4244
4245 return Error::success();
4246}
4247
4248void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) {
4249 if (ValueTypeCallback) {
4250 (*ValueTypeCallback)(
4251 F, TypeID, [this](unsigned I) { return getTypeByID(ID: I); },
4252 [this](unsigned I, unsigned J) { return getContainedTypeID(ID: I, Idx: J); });
4253 }
4254}
4255
4256Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
4257 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
4258 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
4259 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
4260 // v2: [strtab_offset, strtab_size, v1]
4261 StringRef Name;
4262 std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record);
4263
4264 if (Record.size() < 8)
4265 return error(Message: "Invalid function record");
4266 unsigned FTyID = Record[0];
4267 Type *FTy = getTypeByID(ID: FTyID);
4268 if (!FTy)
4269 return error(Message: "Invalid function record");
4270 if (isa<PointerType>(Val: FTy)) {
4271 FTyID = getContainedTypeID(ID: FTyID, Idx: 0);
4272 FTy = getTypeByID(ID: FTyID);
4273 if (!FTy)
4274 return error(Message: "Missing element type for old-style function");
4275 }
4276
4277 if (!isa<FunctionType>(Val: FTy))
4278 return error(Message: "Invalid type for value");
4279 auto CC = static_cast<CallingConv::ID>(Record[1]);
4280 if (CC & ~CallingConv::MaxID)
4281 return error(Message: "Invalid calling convention ID");
4282
4283 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
4284 if (Record.size() > 16)
4285 AddrSpace = Record[16];
4286
4287 Function *Func =
4288 Function::Create(Ty: cast<FunctionType>(Val: FTy), Linkage: GlobalValue::ExternalLinkage,
4289 AddrSpace, N: Name, M: TheModule);
4290
4291 assert(Func->getFunctionType() == FTy &&
4292 "Incorrect fully specified type provided for function");
4293 FunctionTypeIDs[Func] = FTyID;
4294
4295 Func->setCallingConv(CC);
4296 bool isProto = Record[2];
4297 uint64_t RawLinkage = Record[3];
4298 Func->setLinkage(getDecodedLinkage(Val: RawLinkage));
4299 Func->setAttributes(getAttributes(i: Record[4]));
4300 callValueTypeCallback(F: Func, TypeID: FTyID);
4301
4302 // Upgrade any old-style byval or sret without a type by propagating the
4303 // argument's pointee type. There should be no opaque pointers where the byval
4304 // type is implicit.
4305 for (unsigned i = 0; i != Func->arg_size(); ++i) {
4306 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4307 Attribute::InAlloca}) {
4308 if (!Func->hasParamAttribute(ArgNo: i, Kind))
4309 continue;
4310
4311 if (Func->getParamAttribute(ArgNo: i, Kind).getValueAsType())
4312 continue;
4313
4314 Func->removeParamAttr(ArgNo: i, Kind);
4315
4316 unsigned ParamTypeID = getContainedTypeID(ID: FTyID, Idx: i + 1);
4317 Type *PtrEltTy = getPtrElementTypeByID(ID: ParamTypeID);
4318 if (!PtrEltTy)
4319 return error(Message: "Missing param element type for attribute upgrade");
4320
4321 Attribute NewAttr;
4322 switch (Kind) {
4323 case Attribute::ByVal:
4324 NewAttr = Attribute::getWithByValType(Context, Ty: PtrEltTy);
4325 break;
4326 case Attribute::StructRet:
4327 NewAttr = Attribute::getWithStructRetType(Context, Ty: PtrEltTy);
4328 break;
4329 case Attribute::InAlloca:
4330 NewAttr = Attribute::getWithInAllocaType(Context, Ty: PtrEltTy);
4331 break;
4332 default:
4333 llvm_unreachable("not an upgraded type attribute");
4334 }
4335
4336 Func->addParamAttr(ArgNo: i, Attr: NewAttr);
4337 }
4338 }
4339
4340 if (Func->getCallingConv() == CallingConv::X86_INTR &&
4341 !Func->arg_empty() && !Func->hasParamAttribute(ArgNo: 0, Kind: Attribute::ByVal)) {
4342 unsigned ParamTypeID = getContainedTypeID(ID: FTyID, Idx: 1);
4343 Type *ByValTy = getPtrElementTypeByID(ID: ParamTypeID);
4344 if (!ByValTy)
4345 return error(Message: "Missing param element type for x86_intrcc upgrade");
4346 Attribute NewAttr = Attribute::getWithByValType(Context, Ty: ByValTy);
4347 Func->addParamAttr(ArgNo: 0, Attr: NewAttr);
4348 }
4349
4350 MaybeAlign Alignment;
4351 if (Error Err = parseAlignmentValue(Exponent: Record[5], Alignment))
4352 return Err;
4353 if (Alignment)
4354 Func->setAlignment(*Alignment);
4355 if (Record[6]) {
4356 if (Record[6] - 1 >= SectionTable.size())
4357 return error(Message: "Invalid ID");
4358 Func->setSection(SectionTable[Record[6] - 1]);
4359 }
4360 // Local linkage must have default visibility.
4361 // auto-upgrade `hidden` and `protected` for old bitcode.
4362 if (!Func->hasLocalLinkage())
4363 Func->setVisibility(getDecodedVisibility(Val: Record[7]));
4364 if (Record.size() > 8 && Record[8]) {
4365 if (Record[8] - 1 >= GCTable.size())
4366 return error(Message: "Invalid ID");
4367 Func->setGC(GCTable[Record[8] - 1]);
4368 }
4369 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
4370 if (Record.size() > 9)
4371 UnnamedAddr = getDecodedUnnamedAddrType(Val: Record[9]);
4372 Func->setUnnamedAddr(UnnamedAddr);
4373
4374 FunctionOperandInfo OperandInfo = {.F: Func, .PersonalityFn: 0, .Prefix: 0, .Prologue: 0};
4375 if (Record.size() > 10)
4376 OperandInfo.Prologue = Record[10];
4377
4378 if (Record.size() > 11) {
4379 // A GlobalValue with local linkage cannot have a DLL storage class.
4380 if (!Func->hasLocalLinkage()) {
4381 Func->setDLLStorageClass(getDecodedDLLStorageClass(Val: Record[11]));
4382 }
4383 } else {
4384 upgradeDLLImportExportLinkage(GV: Func, Val: RawLinkage);
4385 }
4386
4387 if (Record.size() > 12) {
4388 if (unsigned ComdatID = Record[12]) {
4389 if (ComdatID > ComdatList.size())
4390 return error(Message: "Invalid function comdat ID");
4391 Func->setComdat(ComdatList[ComdatID - 1]);
4392 }
4393 } else if (hasImplicitComdat(Val: RawLinkage)) {
4394 ImplicitComdatObjects.insert(V: Func);
4395 }
4396
4397 if (Record.size() > 13)
4398 OperandInfo.Prefix = Record[13];
4399
4400 if (Record.size() > 14)
4401 OperandInfo.PersonalityFn = Record[14];
4402
4403 if (Record.size() > 15) {
4404 Func->setDSOLocal(getDecodedDSOLocal(Val: Record[15]));
4405 }
4406 inferDSOLocal(GV: Func);
4407
4408 // Record[16] is the address space number.
4409
4410 // Check whether we have enough values to read a partition name. Also make
4411 // sure Strtab has enough values.
4412 if (Record.size() > 18 && Strtab.data() &&
4413 Record[17] + Record[18] <= Strtab.size()) {
4414 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
4415 }
4416
4417 ValueList.push_back(V: Func, TypeID: getVirtualTypeID(Ty: Func->getType(), ChildTypeIDs: FTyID));
4418
4419 if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)
4420 FunctionOperands.push_back(x: OperandInfo);
4421
4422 // If this is a function with a body, remember the prototype we are
4423 // creating now, so that we can match up the body with them later.
4424 if (!isProto) {
4425 Func->setIsMaterializable(true);
4426 FunctionsWithBodies.push_back(x: Func);
4427 DeferredFunctionInfo[Func] = 0;
4428 }
4429 return Error::success();
4430}
4431
4432Error BitcodeReader::parseGlobalIndirectSymbolRecord(
4433 unsigned BitCode, ArrayRef<uint64_t> Record) {
4434 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
4435 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
4436 // dllstorageclass, threadlocal, unnamed_addr,
4437 // preemption specifier] (name in VST)
4438 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
4439 // visibility, dllstorageclass, threadlocal, unnamed_addr,
4440 // preemption specifier] (name in VST)
4441 // v2: [strtab_offset, strtab_size, v1]
4442 StringRef Name;
4443 std::tie(args&: Name, args&: Record) = readNameFromStrtab(Record);
4444
4445 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
4446 if (Record.size() < (3 + (unsigned)NewRecord))
4447 return error(Message: "Invalid global indirect symbol record");
4448 unsigned OpNum = 0;
4449 unsigned TypeID = Record[OpNum++];
4450 Type *Ty = getTypeByID(ID: TypeID);
4451 if (!Ty)
4452 return error(Message: "Invalid global indirect symbol record");
4453
4454 unsigned AddrSpace;
4455 if (!NewRecord) {
4456 auto *PTy = dyn_cast<PointerType>(Val: Ty);
4457 if (!PTy)
4458 return error(Message: "Invalid type for value");
4459 AddrSpace = PTy->getAddressSpace();
4460 TypeID = getContainedTypeID(ID: TypeID);
4461 Ty = getTypeByID(ID: TypeID);
4462 if (!Ty)
4463 return error(Message: "Missing element type for old-style indirect symbol");
4464 } else {
4465 AddrSpace = Record[OpNum++];
4466 }
4467
4468 auto Val = Record[OpNum++];
4469 auto Linkage = Record[OpNum++];
4470 GlobalValue *NewGA;
4471 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4472 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
4473 NewGA = GlobalAlias::create(Ty, AddressSpace: AddrSpace, Linkage: getDecodedLinkage(Val: Linkage), Name,
4474 Parent: TheModule);
4475 else
4476 NewGA = GlobalIFunc::create(Ty, AddressSpace: AddrSpace, Linkage: getDecodedLinkage(Val: Linkage), Name,
4477 Resolver: nullptr, Parent: TheModule);
4478
4479 // Local linkage must have default visibility.
4480 // auto-upgrade `hidden` and `protected` for old bitcode.
4481 if (OpNum != Record.size()) {
4482 auto VisInd = OpNum++;
4483 if (!NewGA->hasLocalLinkage())
4484 NewGA->setVisibility(getDecodedVisibility(Val: Record[VisInd]));
4485 }
4486 if (BitCode == bitc::MODULE_CODE_ALIAS ||
4487 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
4488 if (OpNum != Record.size()) {
4489 auto S = Record[OpNum++];
4490 // A GlobalValue with local linkage cannot have a DLL storage class.
4491 if (!NewGA->hasLocalLinkage())
4492 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Val: S));
4493 }
4494 else
4495 upgradeDLLImportExportLinkage(GV: NewGA, Val: Linkage);
4496 if (OpNum != Record.size())
4497 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Val: Record[OpNum++]));
4498 if (OpNum != Record.size())
4499 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Val: Record[OpNum++]));
4500 }
4501 if (OpNum != Record.size())
4502 NewGA->setDSOLocal(getDecodedDSOLocal(Val: Record[OpNum++]));
4503 inferDSOLocal(GV: NewGA);
4504
4505 // Check whether we have enough values to read a partition name.
4506 if (OpNum + 1 < Record.size()) {
4507 // Check Strtab has enough values for the partition.
4508 if (Record[OpNum] + Record[OpNum + 1] > Strtab.size())
4509 return error(Message: "Malformed partition, too large.");
4510 NewGA->setPartition(
4511 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
4512 }
4513
4514 ValueList.push_back(V: NewGA, TypeID: getVirtualTypeID(Ty: NewGA->getType(), ChildTypeIDs: TypeID));
4515 IndirectSymbolInits.push_back(x: std::make_pair(x&: NewGA, y&: Val));
4516 return Error::success();
4517}
4518
4519Error BitcodeReader::parseModule(uint64_t ResumeBit,
4520 bool ShouldLazyLoadMetadata,
4521 ParserCallbacks Callbacks) {
4522 this->ValueTypeCallback = std::move(Callbacks.ValueType);
4523 if (ResumeBit) {
4524 if (Error JumpFailed = Stream.JumpToBit(BitNo: ResumeBit))
4525 return JumpFailed;
4526 } else if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID))
4527 return Err;
4528
4529 SmallVector<uint64_t, 64> Record;
4530
4531 // Parts of bitcode parsing depend on the datalayout. Make sure we
4532 // finalize the datalayout before we run any of that code.
4533 bool ResolvedDataLayout = false;
4534 // In order to support importing modules with illegal data layout strings,
4535 // delay parsing the data layout string until after upgrades and overrides
4536 // have been applied, allowing to fix illegal data layout strings.
4537 // Initialize to the current module's layout string in case none is specified.
4538 std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr();
4539
4540 auto ResolveDataLayout = [&]() -> Error {
4541 if (ResolvedDataLayout)
4542 return Error::success();
4543
4544 // Datalayout and triple can't be parsed after this point.
4545 ResolvedDataLayout = true;
4546
4547 // Auto-upgrade the layout string
4548 TentativeDataLayoutStr = llvm::UpgradeDataLayoutString(
4549 DL: TentativeDataLayoutStr, Triple: TheModule->getTargetTriple().str());
4550
4551 // Apply override
4552 if (Callbacks.DataLayout) {
4553 if (auto LayoutOverride = (*Callbacks.DataLayout)(
4554 TheModule->getTargetTriple().str(), TentativeDataLayoutStr))
4555 TentativeDataLayoutStr = *LayoutOverride;
4556 }
4557
4558 // Now the layout string is finalized in TentativeDataLayoutStr. Parse it.
4559 Expected<DataLayout> MaybeDL = DataLayout::parse(LayoutString: TentativeDataLayoutStr);
4560 if (!MaybeDL)
4561 return MaybeDL.takeError();
4562
4563 TheModule->setDataLayout(MaybeDL.get());
4564 return Error::success();
4565 };
4566
4567 // Read all the records for this module.
4568 while (true) {
4569 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4570 if (!MaybeEntry)
4571 return MaybeEntry.takeError();
4572 llvm::BitstreamEntry Entry = MaybeEntry.get();
4573
4574 switch (Entry.Kind) {
4575 case BitstreamEntry::Error:
4576 return error(Message: "Malformed block");
4577 case BitstreamEntry::EndBlock:
4578 if (Error Err = ResolveDataLayout())
4579 return Err;
4580 return globalCleanup();
4581
4582 case BitstreamEntry::SubBlock:
4583 switch (Entry.ID) {
4584 default: // Skip unknown content.
4585 if (Error Err = Stream.SkipBlock())
4586 return Err;
4587 break;
4588 case bitc::BLOCKINFO_BLOCK_ID:
4589 if (Error Err = readBlockInfo())
4590 return Err;
4591 break;
4592 case bitc::PARAMATTR_BLOCK_ID:
4593 if (Error Err = parseAttributeBlock())
4594 return Err;
4595 break;
4596 case bitc::PARAMATTR_GROUP_BLOCK_ID:
4597 if (Error Err = parseAttributeGroupBlock())
4598 return Err;
4599 break;
4600 case bitc::TYPE_BLOCK_ID_NEW:
4601 if (Error Err = parseTypeTable())
4602 return Err;
4603 break;
4604 case bitc::VALUE_SYMTAB_BLOCK_ID:
4605 if (!SeenValueSymbolTable) {
4606 // Either this is an old form VST without function index and an
4607 // associated VST forward declaration record (which would have caused
4608 // the VST to be jumped to and parsed before it was encountered
4609 // normally in the stream), or there were no function blocks to
4610 // trigger an earlier parsing of the VST.
4611 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
4612 if (Error Err = parseValueSymbolTable())
4613 return Err;
4614 SeenValueSymbolTable = true;
4615 } else {
4616 // We must have had a VST forward declaration record, which caused
4617 // the parser to jump to and parse the VST earlier.
4618 assert(VSTOffset > 0);
4619 if (Error Err = Stream.SkipBlock())
4620 return Err;
4621 }
4622 break;
4623 case bitc::CONSTANTS_BLOCK_ID:
4624 if (Error Err = parseConstants())
4625 return Err;
4626 if (Error Err = resolveGlobalAndIndirectSymbolInits())
4627 return Err;
4628 break;
4629 case bitc::METADATA_BLOCK_ID:
4630 if (ShouldLazyLoadMetadata) {
4631 if (Error Err = rememberAndSkipMetadata())
4632 return Err;
4633 break;
4634 }
4635 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
4636 if (Error Err = MDLoader->parseModuleMetadata())
4637 return Err;
4638 break;
4639 case bitc::METADATA_KIND_BLOCK_ID:
4640 if (Error Err = MDLoader->parseMetadataKinds())
4641 return Err;
4642 break;
4643 case bitc::FUNCTION_BLOCK_ID:
4644 if (Error Err = ResolveDataLayout())
4645 return Err;
4646
4647 // If this is the first function body we've seen, reverse the
4648 // FunctionsWithBodies list.
4649 if (!SeenFirstFunctionBody) {
4650 std::reverse(first: FunctionsWithBodies.begin(), last: FunctionsWithBodies.end());
4651 if (Error Err = globalCleanup())
4652 return Err;
4653 SeenFirstFunctionBody = true;
4654 }
4655
4656 if (VSTOffset > 0) {
4657 // If we have a VST forward declaration record, make sure we
4658 // parse the VST now if we haven't already. It is needed to
4659 // set up the DeferredFunctionInfo vector for lazy reading.
4660 if (!SeenValueSymbolTable) {
4661 if (Error Err = BitcodeReader::parseValueSymbolTable(Offset: VSTOffset))
4662 return Err;
4663 SeenValueSymbolTable = true;
4664 // Fall through so that we record the NextUnreadBit below.
4665 // This is necessary in case we have an anonymous function that
4666 // is later materialized. Since it will not have a VST entry we
4667 // need to fall back to the lazy parse to find its offset.
4668 } else {
4669 // If we have a VST forward declaration record, but have already
4670 // parsed the VST (just above, when the first function body was
4671 // encountered here), then we are resuming the parse after
4672 // materializing functions. The ResumeBit points to the
4673 // start of the last function block recorded in the
4674 // DeferredFunctionInfo map. Skip it.
4675 if (Error Err = Stream.SkipBlock())
4676 return Err;
4677 continue;
4678 }
4679 }
4680
4681 // Support older bitcode files that did not have the function
4682 // index in the VST, nor a VST forward declaration record, as
4683 // well as anonymous functions that do not have VST entries.
4684 // Build the DeferredFunctionInfo vector on the fly.
4685 if (Error Err = rememberAndSkipFunctionBody())
4686 return Err;
4687
4688 // Suspend parsing when we reach the function bodies. Subsequent
4689 // materialization calls will resume it when necessary. If the bitcode
4690 // file is old, the symbol table will be at the end instead and will not
4691 // have been seen yet. In this case, just finish the parse now.
4692 if (SeenValueSymbolTable) {
4693 NextUnreadBit = Stream.GetCurrentBitNo();
4694 // After the VST has been parsed, we need to make sure intrinsic name
4695 // are auto-upgraded.
4696 return globalCleanup();
4697 }
4698 break;
4699 case bitc::USELIST_BLOCK_ID:
4700 if (Error Err = parseUseLists())
4701 return Err;
4702 break;
4703 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
4704 if (Error Err = parseOperandBundleTags())
4705 return Err;
4706 break;
4707 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID:
4708 if (Error Err = parseSyncScopeNames())
4709 return Err;
4710 break;
4711 }
4712 continue;
4713
4714 case BitstreamEntry::Record:
4715 // The interesting case.
4716 break;
4717 }
4718
4719 // Read a record.
4720 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
4721 if (!MaybeBitCode)
4722 return MaybeBitCode.takeError();
4723 switch (unsigned BitCode = MaybeBitCode.get()) {
4724 default: break; // Default behavior, ignore unknown content.
4725 case bitc::MODULE_CODE_VERSION: {
4726 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
4727 if (!VersionOrErr)
4728 return VersionOrErr.takeError();
4729 UseRelativeIDs = *VersionOrErr >= 1;
4730 break;
4731 }
4732 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
4733 if (ResolvedDataLayout)
4734 return error(Message: "target triple too late in module");
4735 std::string S;
4736 if (convertToString(Record, Idx: 0, Result&: S))
4737 return error(Message: "Invalid triple record");
4738 TheModule->setTargetTriple(Triple(std::move(S)));
4739 break;
4740 }
4741 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
4742 if (ResolvedDataLayout)
4743 return error(Message: "datalayout too late in module");
4744 if (convertToString(Record, Idx: 0, Result&: TentativeDataLayoutStr))
4745 return error(Message: "Invalid data layout record");
4746 break;
4747 }
4748 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
4749 std::string S;
4750 if (convertToString(Record, Idx: 0, Result&: S))
4751 return error(Message: "Invalid asm record");
4752 TheModule->setModuleInlineAsm(S);
4753 break;
4754 }
4755 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
4756 // Deprecated, but still needed to read old bitcode files.
4757 std::string S;
4758 if (convertToString(Record, Idx: 0, Result&: S))
4759 return error(Message: "Invalid deplib record");
4760 // Ignore value.
4761 break;
4762 }
4763 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
4764 std::string S;
4765 if (convertToString(Record, Idx: 0, Result&: S))
4766 return error(Message: "Invalid section name record");
4767 SectionTable.push_back(x: S);
4768 break;
4769 }
4770 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
4771 std::string S;
4772 if (convertToString(Record, Idx: 0, Result&: S))
4773 return error(Message: "Invalid gcname record");
4774 GCTable.push_back(x: S);
4775 break;
4776 }
4777 case bitc::MODULE_CODE_COMDAT:
4778 if (Error Err = parseComdatRecord(Record))
4779 return Err;
4780 break;
4781 // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC}
4782 // written by ThinLinkBitcodeWriter. See
4783 // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each
4784 // record
4785 // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714)
4786 case bitc::MODULE_CODE_GLOBALVAR:
4787 if (Error Err = parseGlobalVarRecord(Record))
4788 return Err;
4789 break;
4790 case bitc::MODULE_CODE_FUNCTION:
4791 if (Error Err = ResolveDataLayout())
4792 return Err;
4793 if (Error Err = parseFunctionRecord(Record))
4794 return Err;
4795 break;
4796 case bitc::MODULE_CODE_IFUNC:
4797 case bitc::MODULE_CODE_ALIAS:
4798 case bitc::MODULE_CODE_ALIAS_OLD:
4799 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
4800 return Err;
4801 break;
4802 /// MODULE_CODE_VSTOFFSET: [offset]
4803 case bitc::MODULE_CODE_VSTOFFSET:
4804 if (Record.empty())
4805 return error(Message: "Invalid vstoffset record");
4806 // Note that we subtract 1 here because the offset is relative to one word
4807 // before the start of the identification or module block, which was
4808 // historically always the start of the regular bitcode header.
4809 VSTOffset = Record[0] - 1;
4810 break;
4811 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4812 case bitc::MODULE_CODE_SOURCE_FILENAME:
4813 SmallString<128> ValueName;
4814 if (convertToString(Record, Idx: 0, Result&: ValueName))
4815 return error(Message: "Invalid source filename record");
4816 TheModule->setSourceFileName(ValueName);
4817 break;
4818 }
4819 Record.clear();
4820 }
4821 this->ValueTypeCallback = std::nullopt;
4822 return Error::success();
4823}
4824
4825Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
4826 bool IsImporting,
4827 ParserCallbacks Callbacks) {
4828 TheModule = M;
4829 MetadataLoaderCallbacks MDCallbacks;
4830 MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); };
4831 MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) {
4832 return getContainedTypeID(ID: I, Idx: J);
4833 };
4834 MDCallbacks.MDType = Callbacks.MDType;
4835 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks);
4836 return parseModule(ResumeBit: 0, ShouldLazyLoadMetadata, Callbacks);
4837}
4838
4839Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
4840 if (!isa<PointerType>(Val: PtrType))
4841 return error(Message: "Load/Store operand is not a pointer type");
4842 if (!PointerType::isLoadableOrStorableType(ElemTy: ValType))
4843 return error(Message: "Cannot load/store from pointer");
4844 return Error::success();
4845}
4846
4847Error BitcodeReader::propagateAttributeTypes(CallBase *CB,
4848 ArrayRef<unsigned> ArgTyIDs) {
4849 AttributeList Attrs = CB->getAttributes();
4850 for (unsigned i = 0; i != CB->arg_size(); ++i) {
4851 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet,
4852 Attribute::InAlloca}) {
4853 if (!Attrs.hasParamAttr(ArgNo: i, Kind) ||
4854 Attrs.getParamAttr(ArgNo: i, Kind).getValueAsType())
4855 continue;
4856
4857 Type *PtrEltTy = getPtrElementTypeByID(ID: ArgTyIDs[i]);
4858 if (!PtrEltTy)
4859 return error(Message: "Missing element type for typed attribute upgrade");
4860
4861 Attribute NewAttr;
4862 switch (Kind) {
4863 case Attribute::ByVal:
4864 NewAttr = Attribute::getWithByValType(Context, Ty: PtrEltTy);
4865 break;
4866 case Attribute::StructRet:
4867 NewAttr = Attribute::getWithStructRetType(Context, Ty: PtrEltTy);
4868 break;
4869 case Attribute::InAlloca:
4870 NewAttr = Attribute::getWithInAllocaType(Context, Ty: PtrEltTy);
4871 break;
4872 default:
4873 llvm_unreachable("not an upgraded type attribute");
4874 }
4875
4876 Attrs = Attrs.addParamAttribute(C&: Context, ArgNos: i, A: NewAttr);
4877 }
4878 }
4879
4880 if (CB->isInlineAsm()) {
4881 const InlineAsm *IA = cast<InlineAsm>(Val: CB->getCalledOperand());
4882 unsigned ArgNo = 0;
4883 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
4884 if (!CI.hasArg())
4885 continue;
4886
4887 if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) {
4888 Type *ElemTy = getPtrElementTypeByID(ID: ArgTyIDs[ArgNo]);
4889 if (!ElemTy)
4890 return error(Message: "Missing element type for inline asm upgrade");
4891 Attrs = Attrs.addParamAttribute(
4892 C&: Context, ArgNos: ArgNo,
4893 A: Attribute::get(Context, Kind: Attribute::ElementType, Ty: ElemTy));
4894 }
4895
4896 ArgNo++;
4897 }
4898 }
4899
4900 switch (CB->getIntrinsicID()) {
4901 case Intrinsic::preserve_array_access_index:
4902 case Intrinsic::preserve_struct_access_index:
4903 case Intrinsic::aarch64_ldaxr:
4904 case Intrinsic::aarch64_ldxr:
4905 case Intrinsic::aarch64_stlxr:
4906 case Intrinsic::aarch64_stxr:
4907 case Intrinsic::arm_ldaex:
4908 case Intrinsic::arm_ldrex:
4909 case Intrinsic::arm_stlex:
4910 case Intrinsic::arm_strex: {
4911 unsigned ArgNo;
4912 switch (CB->getIntrinsicID()) {
4913 case Intrinsic::aarch64_stlxr:
4914 case Intrinsic::aarch64_stxr:
4915 case Intrinsic::arm_stlex:
4916 case Intrinsic::arm_strex:
4917 ArgNo = 1;
4918 break;
4919 default:
4920 ArgNo = 0;
4921 break;
4922 }
4923 if (!Attrs.getParamElementType(ArgNo)) {
4924 Type *ElTy = getPtrElementTypeByID(ID: ArgTyIDs[ArgNo]);
4925 if (!ElTy)
4926 return error(Message: "Missing element type for elementtype upgrade");
4927 Attribute NewAttr = Attribute::get(Context, Kind: Attribute::ElementType, Ty: ElTy);
4928 Attrs = Attrs.addParamAttribute(C&: Context, ArgNos: ArgNo, A: NewAttr);
4929 }
4930 break;
4931 }
4932 default:
4933 break;
4934 }
4935
4936 CB->setAttributes(Attrs);
4937 return Error::success();
4938}
4939
4940/// Lazily parse the specified function body block.
4941Error BitcodeReader::parseFunctionBody(Function *F) {
4942 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::FUNCTION_BLOCK_ID))
4943 return Err;
4944
4945 // Unexpected unresolved metadata when parsing function.
4946 if (MDLoader->hasFwdRefs())
4947 return error(Message: "Invalid function metadata: incoming forward references");
4948
4949 InstructionList.clear();
4950 unsigned ModuleValueListSize = ValueList.size();
4951 unsigned ModuleMDLoaderSize = MDLoader->size();
4952
4953 // Add all the function arguments to the value table.
4954 unsigned ArgNo = 0;
4955 unsigned FTyID = FunctionTypeIDs[F];
4956 for (Argument &I : F->args()) {
4957 unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: ArgNo + 1);
4958 assert(I.getType() == getTypeByID(ArgTyID) &&
4959 "Incorrect fully specified type for Function Argument");
4960 ValueList.push_back(V: &I, TypeID: ArgTyID);
4961 ++ArgNo;
4962 }
4963 unsigned NextValueNo = ValueList.size();
4964 BasicBlock *CurBB = nullptr;
4965 unsigned CurBBNo = 0;
4966 // Block into which constant expressions from phi nodes are materialized.
4967 BasicBlock *PhiConstExprBB = nullptr;
4968 // Edge blocks for phi nodes into which constant expressions have been
4969 // expanded.
4970 SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4>
4971 ConstExprEdgeBBs;
4972
4973 DebugLoc LastLoc;
4974 auto getLastInstruction = [&]() -> Instruction * {
4975 if (CurBB && !CurBB->empty())
4976 return &CurBB->back();
4977 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4978 !FunctionBBs[CurBBNo - 1]->empty())
4979 return &FunctionBBs[CurBBNo - 1]->back();
4980 return nullptr;
4981 };
4982
4983 std::vector<OperandBundleDef> OperandBundles;
4984
4985 // Read all the records.
4986 SmallVector<uint64_t, 64> Record;
4987
4988 while (true) {
4989 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4990 if (!MaybeEntry)
4991 return MaybeEntry.takeError();
4992 llvm::BitstreamEntry Entry = MaybeEntry.get();
4993
4994 switch (Entry.Kind) {
4995 case BitstreamEntry::Error:
4996 return error(Message: "Malformed block");
4997 case BitstreamEntry::EndBlock:
4998 goto OutOfRecordLoop;
4999
5000 case BitstreamEntry::SubBlock:
5001 switch (Entry.ID) {
5002 default: // Skip unknown content.
5003 if (Error Err = Stream.SkipBlock())
5004 return Err;
5005 break;
5006 case bitc::CONSTANTS_BLOCK_ID:
5007 if (Error Err = parseConstants())
5008 return Err;
5009 NextValueNo = ValueList.size();
5010 break;
5011 case bitc::VALUE_SYMTAB_BLOCK_ID:
5012 if (Error Err = parseValueSymbolTable())
5013 return Err;
5014 break;
5015 case bitc::METADATA_ATTACHMENT_ID:
5016 if (Error Err = MDLoader->parseMetadataAttachment(F&: *F, InstructionList))
5017 return Err;
5018 break;
5019 case bitc::METADATA_BLOCK_ID:
5020 assert(DeferredMetadataInfo.empty() &&
5021 "Must read all module-level metadata before function-level");
5022 if (Error Err = MDLoader->parseFunctionMetadata())
5023 return Err;
5024 break;
5025 case bitc::USELIST_BLOCK_ID:
5026 if (Error Err = parseUseLists())
5027 return Err;
5028 break;
5029 }
5030 continue;
5031
5032 case BitstreamEntry::Record:
5033 // The interesting case.
5034 break;
5035 }
5036
5037 // Read a record.
5038 Record.clear();
5039 Instruction *I = nullptr;
5040 unsigned ResTypeID = InvalidTypeID;
5041 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
5042 if (!MaybeBitCode)
5043 return MaybeBitCode.takeError();
5044 switch (unsigned BitCode = MaybeBitCode.get()) {
5045 default: // Default behavior: reject
5046 return error(Message: "Invalid value");
5047 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
5048 if (Record.empty() || Record[0] == 0)
5049 return error(Message: "Invalid declareblocks record");
5050 // Create all the basic blocks for the function.
5051 FunctionBBs.resize(new_size: Record[0]);
5052
5053 // See if anything took the address of blocks in this function.
5054 auto BBFRI = BasicBlockFwdRefs.find(Val: F);
5055 if (BBFRI == BasicBlockFwdRefs.end()) {
5056 for (BasicBlock *&BB : FunctionBBs)
5057 BB = BasicBlock::Create(Context, Name: "", Parent: F);
5058 } else {
5059 auto &BBRefs = BBFRI->second;
5060 // Check for invalid basic block references.
5061 if (BBRefs.size() > FunctionBBs.size())
5062 return error(Message: "Invalid ID");
5063 assert(!BBRefs.empty() && "Unexpected empty array");
5064 assert(!BBRefs.front() && "Invalid reference to entry block");
5065 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
5066 ++I)
5067 if (I < RE && BBRefs[I]) {
5068 BBRefs[I]->insertInto(Parent: F);
5069 FunctionBBs[I] = BBRefs[I];
5070 } else {
5071 FunctionBBs[I] = BasicBlock::Create(Context, Name: "", Parent: F);
5072 }
5073
5074 // Erase from the table.
5075 BasicBlockFwdRefs.erase(I: BBFRI);
5076 }
5077
5078 CurBB = FunctionBBs[0];
5079 continue;
5080 }
5081
5082 case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...]
5083 // The record should not be emitted if it's an empty list.
5084 if (Record.empty())
5085 return error(Message: "Invalid blockaddr users record");
5086 // When we have the RARE case of a BlockAddress Constant that is not
5087 // scoped to the Function it refers to, we need to conservatively
5088 // materialize the referred to Function, regardless of whether or not
5089 // that Function will ultimately be linked, otherwise users of
5090 // BitcodeReader might start splicing out Function bodies such that we
5091 // might no longer be able to materialize the BlockAddress since the
5092 // BasicBlock (and entire body of the Function) the BlockAddress refers
5093 // to may have been moved. In the case that the user of BitcodeReader
5094 // decides ultimately not to link the Function body, materializing here
5095 // could be considered wasteful, but it's better than a deserialization
5096 // failure as described. This keeps BitcodeReader unaware of complex
5097 // linkage policy decisions such as those use by LTO, leaving those
5098 // decisions "one layer up."
5099 for (uint64_t ValID : Record)
5100 if (auto *F = dyn_cast<Function>(Val: ValueList[ValID]))
5101 BackwardRefFunctions.push_back(x: F);
5102 else
5103 return error(Message: "Invalid blockaddr users record");
5104
5105 continue;
5106
5107 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
5108 // This record indicates that the last instruction is at the same
5109 // location as the previous instruction with a location.
5110 I = getLastInstruction();
5111
5112 if (!I)
5113 return error(Message: "Invalid debug_loc_again record");
5114 I->setDebugLoc(LastLoc);
5115 I = nullptr;
5116 continue;
5117
5118 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
5119 I = getLastInstruction();
5120 if (!I || Record.size() < 4)
5121 return error(Message: "Invalid debug loc record");
5122
5123 unsigned Line = Record[0], Col = Record[1];
5124 unsigned ScopeID = Record[2], IAID = Record[3];
5125 bool isImplicitCode = Record.size() >= 5 && Record[4];
5126 uint64_t AtomGroup = Record.size() == 7 ? Record[5] : 0;
5127 uint8_t AtomRank = Record.size() == 7 ? Record[6] : 0;
5128
5129 MDNode *Scope = nullptr, *IA = nullptr;
5130 if (ScopeID) {
5131 Scope = dyn_cast_or_null<MDNode>(
5132 Val: MDLoader->getMetadataFwdRefOrLoad(Idx: ScopeID - 1));
5133 if (!Scope)
5134 return error(Message: "Invalid debug loc record");
5135 }
5136 if (IAID) {
5137 IA = dyn_cast_or_null<MDNode>(
5138 Val: MDLoader->getMetadataFwdRefOrLoad(Idx: IAID - 1));
5139 if (!IA)
5140 return error(Message: "Invalid debug loc record");
5141 }
5142
5143 LastLoc = DILocation::get(Context&: Scope->getContext(), Line, Column: Col, Scope, InlinedAt: IA,
5144 ImplicitCode: isImplicitCode, AtomGroup, AtomRank);
5145 I->setDebugLoc(LastLoc);
5146 I = nullptr;
5147 continue;
5148 }
5149 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
5150 unsigned OpNum = 0;
5151 Value *LHS;
5152 unsigned TypeID;
5153 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID, ConstExprInsertBB: CurBB) ||
5154 OpNum+1 > Record.size())
5155 return error(Message: "Invalid unary operator record");
5156
5157 int Opc = getDecodedUnaryOpcode(Val: Record[OpNum++], Ty: LHS->getType());
5158 if (Opc == -1)
5159 return error(Message: "Invalid unary operator record");
5160 I = UnaryOperator::Create(Op: (Instruction::UnaryOps)Opc, S: LHS);
5161 ResTypeID = TypeID;
5162 InstructionList.push_back(Elt: I);
5163 if (OpNum < Record.size()) {
5164 if (isa<FPMathOperator>(Val: I)) {
5165 FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]);
5166 if (FMF.any())
5167 I->setFastMathFlags(FMF);
5168 }
5169 }
5170 break;
5171 }
5172 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
5173 unsigned OpNum = 0;
5174 Value *LHS, *RHS;
5175 unsigned TypeID;
5176 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID, ConstExprInsertBB: CurBB) ||
5177 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: LHS->getType(), TyID: TypeID, ResVal&: RHS,
5178 ConstExprInsertBB: CurBB) ||
5179 OpNum+1 > Record.size())
5180 return error(Message: "Invalid binary operator record");
5181
5182 int Opc = getDecodedBinaryOpcode(Val: Record[OpNum++], Ty: LHS->getType());
5183 if (Opc == -1)
5184 return error(Message: "Invalid binary operator record");
5185 I = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS);
5186 ResTypeID = TypeID;
5187 InstructionList.push_back(Elt: I);
5188 if (OpNum < Record.size()) {
5189 if (Opc == Instruction::Add ||
5190 Opc == Instruction::Sub ||
5191 Opc == Instruction::Mul ||
5192 Opc == Instruction::Shl) {
5193 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
5194 cast<BinaryOperator>(Val: I)->setHasNoSignedWrap(true);
5195 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
5196 cast<BinaryOperator>(Val: I)->setHasNoUnsignedWrap(true);
5197 } else if (Opc == Instruction::SDiv ||
5198 Opc == Instruction::UDiv ||
5199 Opc == Instruction::LShr ||
5200 Opc == Instruction::AShr) {
5201 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
5202 cast<BinaryOperator>(Val: I)->setIsExact(true);
5203 } else if (Opc == Instruction::Or) {
5204 if (Record[OpNum] & (1 << bitc::PDI_DISJOINT))
5205 cast<PossiblyDisjointInst>(Val: I)->setIsDisjoint(true);
5206 } else if (isa<FPMathOperator>(Val: I)) {
5207 FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]);
5208 if (FMF.any())
5209 I->setFastMathFlags(FMF);
5210 }
5211 }
5212 break;
5213 }
5214 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
5215 unsigned OpNum = 0;
5216 Value *Op;
5217 unsigned OpTypeID;
5218 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) ||
5219 OpNum + 1 > Record.size())
5220 return error(Message: "Invalid cast record");
5221
5222 ResTypeID = Record[OpNum++];
5223 Type *ResTy = getTypeByID(ID: ResTypeID);
5224 int Opc = getDecodedCastOpcode(Val: Record[OpNum++]);
5225
5226 if (Opc == -1 || !ResTy)
5227 return error(Message: "Invalid cast record");
5228 Instruction *Temp = nullptr;
5229 if ((I = UpgradeBitCastInst(Opc, V: Op, DestTy: ResTy, Temp))) {
5230 if (Temp) {
5231 InstructionList.push_back(Elt: Temp);
5232 assert(CurBB && "No current BB?");
5233 Temp->insertInto(ParentBB: CurBB, It: CurBB->end());
5234 }
5235 } else {
5236 auto CastOp = (Instruction::CastOps)Opc;
5237 if (!CastInst::castIsValid(op: CastOp, S: Op, DstTy: ResTy))
5238 return error(Message: "Invalid cast");
5239 I = CastInst::Create(CastOp, S: Op, Ty: ResTy);
5240 }
5241
5242 if (OpNum < Record.size()) {
5243 if (Opc == Instruction::ZExt || Opc == Instruction::UIToFP) {
5244 if (Record[OpNum] & (1 << bitc::PNNI_NON_NEG))
5245 cast<PossiblyNonNegInst>(Val: I)->setNonNeg(true);
5246 } else if (Opc == Instruction::Trunc) {
5247 if (Record[OpNum] & (1 << bitc::TIO_NO_UNSIGNED_WRAP))
5248 cast<TruncInst>(Val: I)->setHasNoUnsignedWrap(true);
5249 if (Record[OpNum] & (1 << bitc::TIO_NO_SIGNED_WRAP))
5250 cast<TruncInst>(Val: I)->setHasNoSignedWrap(true);
5251 }
5252 if (isa<FPMathOperator>(Val: I)) {
5253 FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]);
5254 if (FMF.any())
5255 I->setFastMathFlags(FMF);
5256 }
5257 }
5258
5259 InstructionList.push_back(Elt: I);
5260 break;
5261 }
5262 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
5263 case bitc::FUNC_CODE_INST_GEP_OLD:
5264 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
5265 unsigned OpNum = 0;
5266
5267 unsigned TyID;
5268 Type *Ty;
5269 GEPNoWrapFlags NW;
5270
5271 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
5272 NW = toGEPNoWrapFlags(Flags: Record[OpNum++]);
5273 TyID = Record[OpNum++];
5274 Ty = getTypeByID(ID: TyID);
5275 } else {
5276 if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD)
5277 NW = GEPNoWrapFlags::inBounds();
5278 TyID = InvalidTypeID;
5279 Ty = nullptr;
5280 }
5281
5282 Value *BasePtr;
5283 unsigned BasePtrTypeID;
5284 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: BasePtr, TypeID&: BasePtrTypeID,
5285 ConstExprInsertBB: CurBB))
5286 return error(Message: "Invalid gep record");
5287
5288 if (!Ty) {
5289 TyID = getContainedTypeID(ID: BasePtrTypeID);
5290 if (BasePtr->getType()->isVectorTy())
5291 TyID = getContainedTypeID(ID: TyID);
5292 Ty = getTypeByID(ID: TyID);
5293 }
5294
5295 SmallVector<Value*, 16> GEPIdx;
5296 while (OpNum != Record.size()) {
5297 Value *Op;
5298 unsigned OpTypeID;
5299 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
5300 return error(Message: "Invalid gep record");
5301 GEPIdx.push_back(Elt: Op);
5302 }
5303
5304 auto *GEP = GetElementPtrInst::Create(PointeeType: Ty, Ptr: BasePtr, IdxList: GEPIdx);
5305 I = GEP;
5306
5307 ResTypeID = TyID;
5308 if (cast<GEPOperator>(Val: I)->getNumIndices() != 0) {
5309 auto GTI = std::next(x: gep_type_begin(GEP: I));
5310 for (Value *Idx : drop_begin(RangeOrContainer: cast<GEPOperator>(Val: I)->indices())) {
5311 unsigned SubType = 0;
5312 if (GTI.isStruct()) {
5313 ConstantInt *IdxC =
5314 Idx->getType()->isVectorTy()
5315 ? cast<ConstantInt>(Val: cast<Constant>(Val: Idx)->getSplatValue())
5316 : cast<ConstantInt>(Val: Idx);
5317 SubType = IdxC->getZExtValue();
5318 }
5319 ResTypeID = getContainedTypeID(ID: ResTypeID, Idx: SubType);
5320 ++GTI;
5321 }
5322 }
5323
5324 // At this point ResTypeID is the result element type. We need a pointer
5325 // or vector of pointer to it.
5326 ResTypeID = getVirtualTypeID(Ty: I->getType()->getScalarType(), ChildTypeIDs: ResTypeID);
5327 if (I->getType()->isVectorTy())
5328 ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: ResTypeID);
5329
5330 InstructionList.push_back(Elt: I);
5331 GEP->setNoWrapFlags(NW);
5332 break;
5333 }
5334
5335 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
5336 // EXTRACTVAL: [opty, opval, n x indices]
5337 unsigned OpNum = 0;
5338 Value *Agg;
5339 unsigned AggTypeID;
5340 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Agg, TypeID&: AggTypeID, ConstExprInsertBB: CurBB))
5341 return error(Message: "Invalid extractvalue record");
5342 Type *Ty = Agg->getType();
5343
5344 unsigned RecSize = Record.size();
5345 if (OpNum == RecSize)
5346 return error(Message: "EXTRACTVAL: Invalid instruction with 0 indices");
5347
5348 SmallVector<unsigned, 4> EXTRACTVALIdx;
5349 ResTypeID = AggTypeID;
5350 for (; OpNum != RecSize; ++OpNum) {
5351 bool IsArray = Ty->isArrayTy();
5352 bool IsStruct = Ty->isStructTy();
5353 uint64_t Index = Record[OpNum];
5354
5355 if (!IsStruct && !IsArray)
5356 return error(Message: "EXTRACTVAL: Invalid type");
5357 if ((unsigned)Index != Index)
5358 return error(Message: "Invalid value");
5359 if (IsStruct && Index >= Ty->getStructNumElements())
5360 return error(Message: "EXTRACTVAL: Invalid struct index");
5361 if (IsArray && Index >= Ty->getArrayNumElements())
5362 return error(Message: "EXTRACTVAL: Invalid array index");
5363 EXTRACTVALIdx.push_back(Elt: (unsigned)Index);
5364
5365 if (IsStruct) {
5366 Ty = Ty->getStructElementType(N: Index);
5367 ResTypeID = getContainedTypeID(ID: ResTypeID, Idx: Index);
5368 } else {
5369 Ty = Ty->getArrayElementType();
5370 ResTypeID = getContainedTypeID(ID: ResTypeID);
5371 }
5372 }
5373
5374 I = ExtractValueInst::Create(Agg, Idxs: EXTRACTVALIdx);
5375 InstructionList.push_back(Elt: I);
5376 break;
5377 }
5378
5379 case bitc::FUNC_CODE_INST_INSERTVAL: {
5380 // INSERTVAL: [opty, opval, opty, opval, n x indices]
5381 unsigned OpNum = 0;
5382 Value *Agg;
5383 unsigned AggTypeID;
5384 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Agg, TypeID&: AggTypeID, ConstExprInsertBB: CurBB))
5385 return error(Message: "Invalid insertvalue record");
5386 Value *Val;
5387 unsigned ValTypeID;
5388 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB))
5389 return error(Message: "Invalid insertvalue record");
5390
5391 unsigned RecSize = Record.size();
5392 if (OpNum == RecSize)
5393 return error(Message: "INSERTVAL: Invalid instruction with 0 indices");
5394
5395 SmallVector<unsigned, 4> INSERTVALIdx;
5396 Type *CurTy = Agg->getType();
5397 for (; OpNum != RecSize; ++OpNum) {
5398 bool IsArray = CurTy->isArrayTy();
5399 bool IsStruct = CurTy->isStructTy();
5400 uint64_t Index = Record[OpNum];
5401
5402 if (!IsStruct && !IsArray)
5403 return error(Message: "INSERTVAL: Invalid type");
5404 if ((unsigned)Index != Index)
5405 return error(Message: "Invalid value");
5406 if (IsStruct && Index >= CurTy->getStructNumElements())
5407 return error(Message: "INSERTVAL: Invalid struct index");
5408 if (IsArray && Index >= CurTy->getArrayNumElements())
5409 return error(Message: "INSERTVAL: Invalid array index");
5410
5411 INSERTVALIdx.push_back(Elt: (unsigned)Index);
5412 if (IsStruct)
5413 CurTy = CurTy->getStructElementType(N: Index);
5414 else
5415 CurTy = CurTy->getArrayElementType();
5416 }
5417
5418 if (CurTy != Val->getType())
5419 return error(Message: "Inserted value type doesn't match aggregate type");
5420
5421 I = InsertValueInst::Create(Agg, Val, Idxs: INSERTVALIdx);
5422 ResTypeID = AggTypeID;
5423 InstructionList.push_back(Elt: I);
5424 break;
5425 }
5426
5427 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
5428 // obsolete form of select
5429 // handles select i1 ... in old bitcode
5430 unsigned OpNum = 0;
5431 Value *TrueVal, *FalseVal, *Cond;
5432 unsigned TypeID;
5433 Type *CondType = Type::getInt1Ty(C&: Context);
5434 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: TrueVal, TypeID,
5435 ConstExprInsertBB: CurBB) ||
5436 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: TrueVal->getType(), TyID: TypeID,
5437 ResVal&: FalseVal, ConstExprInsertBB: CurBB) ||
5438 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: CondType,
5439 TyID: getVirtualTypeID(Ty: CondType), ResVal&: Cond, ConstExprInsertBB: CurBB))
5440 return error(Message: "Invalid select record");
5441
5442 I = SelectInst::Create(C: Cond, S1: TrueVal, S2: FalseVal);
5443 ResTypeID = TypeID;
5444 InstructionList.push_back(Elt: I);
5445 break;
5446 }
5447
5448 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
5449 // new form of select
5450 // handles select i1 or select [N x i1]
5451 unsigned OpNum = 0;
5452 Value *TrueVal, *FalseVal, *Cond;
5453 unsigned ValTypeID, CondTypeID;
5454 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: TrueVal, TypeID&: ValTypeID,
5455 ConstExprInsertBB: CurBB) ||
5456 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: TrueVal->getType(), TyID: ValTypeID,
5457 ResVal&: FalseVal, ConstExprInsertBB: CurBB) ||
5458 getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Cond, TypeID&: CondTypeID, ConstExprInsertBB: CurBB))
5459 return error(Message: "Invalid vector select record");
5460
5461 // select condition can be either i1 or [N x i1]
5462 if (VectorType* vector_type =
5463 dyn_cast<VectorType>(Val: Cond->getType())) {
5464 // expect <n x i1>
5465 if (vector_type->getElementType() != Type::getInt1Ty(C&: Context))
5466 return error(Message: "Invalid type for value");
5467 } else {
5468 // expect i1
5469 if (Cond->getType() != Type::getInt1Ty(C&: Context))
5470 return error(Message: "Invalid type for value");
5471 }
5472
5473 I = SelectInst::Create(C: Cond, S1: TrueVal, S2: FalseVal);
5474 ResTypeID = ValTypeID;
5475 InstructionList.push_back(Elt: I);
5476 if (OpNum < Record.size() && isa<FPMathOperator>(Val: I)) {
5477 FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[OpNum]);
5478 if (FMF.any())
5479 I->setFastMathFlags(FMF);
5480 }
5481 break;
5482 }
5483
5484 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
5485 unsigned OpNum = 0;
5486 Value *Vec, *Idx;
5487 unsigned VecTypeID, IdxTypeID;
5488 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec, TypeID&: VecTypeID, ConstExprInsertBB: CurBB) ||
5489 getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Idx, TypeID&: IdxTypeID, ConstExprInsertBB: CurBB))
5490 return error(Message: "Invalid extractelement record");
5491 if (!Vec->getType()->isVectorTy())
5492 return error(Message: "Invalid type for value");
5493 I = ExtractElementInst::Create(Vec, Idx);
5494 ResTypeID = getContainedTypeID(ID: VecTypeID);
5495 InstructionList.push_back(Elt: I);
5496 break;
5497 }
5498
5499 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
5500 unsigned OpNum = 0;
5501 Value *Vec, *Elt, *Idx;
5502 unsigned VecTypeID, IdxTypeID;
5503 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec, TypeID&: VecTypeID, ConstExprInsertBB: CurBB))
5504 return error(Message: "Invalid insertelement record");
5505 if (!Vec->getType()->isVectorTy())
5506 return error(Message: "Invalid type for value");
5507 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo,
5508 Ty: cast<VectorType>(Val: Vec->getType())->getElementType(),
5509 TyID: getContainedTypeID(ID: VecTypeID), ResVal&: Elt, ConstExprInsertBB: CurBB) ||
5510 getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Idx, TypeID&: IdxTypeID, ConstExprInsertBB: CurBB))
5511 return error(Message: "Invalid insert element record");
5512 I = InsertElementInst::Create(Vec, NewElt: Elt, Idx);
5513 ResTypeID = VecTypeID;
5514 InstructionList.push_back(Elt: I);
5515 break;
5516 }
5517
5518 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
5519 unsigned OpNum = 0;
5520 Value *Vec1, *Vec2, *Mask;
5521 unsigned Vec1TypeID;
5522 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Vec1, TypeID&: Vec1TypeID,
5523 ConstExprInsertBB: CurBB) ||
5524 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Vec1->getType(), TyID: Vec1TypeID,
5525 ResVal&: Vec2, ConstExprInsertBB: CurBB))
5526 return error(Message: "Invalid shufflevector record");
5527
5528 unsigned MaskTypeID;
5529 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Mask, TypeID&: MaskTypeID, ConstExprInsertBB: CurBB))
5530 return error(Message: "Invalid shufflevector record");
5531 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
5532 return error(Message: "Invalid type for value");
5533
5534 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
5535 ResTypeID =
5536 getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: getContainedTypeID(ID: Vec1TypeID));
5537 InstructionList.push_back(Elt: I);
5538 break;
5539 }
5540
5541 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
5542 // Old form of ICmp/FCmp returning bool
5543 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
5544 // both legal on vectors but had different behaviour.
5545 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
5546 // FCmp/ICmp returning bool or vector of bool
5547
5548 unsigned OpNum = 0;
5549 Value *LHS, *RHS;
5550 unsigned LHSTypeID;
5551 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: LHS, TypeID&: LHSTypeID, ConstExprInsertBB: CurBB) ||
5552 popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: LHS->getType(), TyID: LHSTypeID, ResVal&: RHS,
5553 ConstExprInsertBB: CurBB))
5554 return error(Message: "Invalid comparison record");
5555
5556 if (OpNum >= Record.size())
5557 return error(
5558 Message: "Invalid record: operand number exceeded available operands");
5559
5560 CmpInst::Predicate PredVal = CmpInst::Predicate(Record[OpNum]);
5561 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
5562 FastMathFlags FMF;
5563 if (IsFP && Record.size() > OpNum+1)
5564 FMF = getDecodedFastMathFlags(Val: Record[++OpNum]);
5565
5566 if (IsFP) {
5567 if (!CmpInst::isFPPredicate(P: PredVal))
5568 return error(Message: "Invalid fcmp predicate");
5569 I = new FCmpInst(PredVal, LHS, RHS);
5570 } else {
5571 if (!CmpInst::isIntPredicate(P: PredVal))
5572 return error(Message: "Invalid icmp predicate");
5573 I = new ICmpInst(PredVal, LHS, RHS);
5574 if (Record.size() > OpNum + 1 &&
5575 (Record[++OpNum] & (1 << bitc::ICMP_SAME_SIGN)))
5576 cast<ICmpInst>(Val: I)->setSameSign();
5577 }
5578
5579 if (OpNum + 1 != Record.size())
5580 return error(Message: "Invalid comparison record");
5581
5582 ResTypeID = getVirtualTypeID(Ty: I->getType()->getScalarType());
5583 if (LHS->getType()->isVectorTy())
5584 ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: ResTypeID);
5585
5586 if (FMF.any())
5587 I->setFastMathFlags(FMF);
5588 InstructionList.push_back(Elt: I);
5589 break;
5590 }
5591
5592 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
5593 {
5594 unsigned Size = Record.size();
5595 if (Size == 0) {
5596 I = ReturnInst::Create(C&: Context);
5597 InstructionList.push_back(Elt: I);
5598 break;
5599 }
5600
5601 unsigned OpNum = 0;
5602 Value *Op = nullptr;
5603 unsigned OpTypeID;
5604 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
5605 return error(Message: "Invalid ret record");
5606 if (OpNum != Record.size())
5607 return error(Message: "Invalid ret record");
5608
5609 I = ReturnInst::Create(C&: Context, retVal: Op);
5610 InstructionList.push_back(Elt: I);
5611 break;
5612 }
5613 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
5614 if (Record.size() != 1 && Record.size() != 3)
5615 return error(Message: "Invalid br record");
5616 BasicBlock *TrueDest = getBasicBlock(ID: Record[0]);
5617 if (!TrueDest)
5618 return error(Message: "Invalid br record");
5619
5620 if (Record.size() == 1) {
5621 I = BranchInst::Create(IfTrue: TrueDest);
5622 InstructionList.push_back(Elt: I);
5623 }
5624 else {
5625 BasicBlock *FalseDest = getBasicBlock(ID: Record[1]);
5626 Type *CondType = Type::getInt1Ty(C&: Context);
5627 Value *Cond = getValue(Record, Slot: 2, InstNum: NextValueNo, Ty: CondType,
5628 TyID: getVirtualTypeID(Ty: CondType), ConstExprInsertBB: CurBB);
5629 if (!FalseDest || !Cond)
5630 return error(Message: "Invalid br record");
5631 I = BranchInst::Create(IfTrue: TrueDest, IfFalse: FalseDest, Cond);
5632 InstructionList.push_back(Elt: I);
5633 }
5634 break;
5635 }
5636 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
5637 if (Record.size() != 1 && Record.size() != 2)
5638 return error(Message: "Invalid cleanupret record");
5639 unsigned Idx = 0;
5640 Type *TokenTy = Type::getTokenTy(C&: Context);
5641 Value *CleanupPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy,
5642 TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB);
5643 if (!CleanupPad)
5644 return error(Message: "Invalid cleanupret record");
5645 BasicBlock *UnwindDest = nullptr;
5646 if (Record.size() == 2) {
5647 UnwindDest = getBasicBlock(ID: Record[Idx++]);
5648 if (!UnwindDest)
5649 return error(Message: "Invalid cleanupret record");
5650 }
5651
5652 I = CleanupReturnInst::Create(CleanupPad, UnwindBB: UnwindDest);
5653 InstructionList.push_back(Elt: I);
5654 break;
5655 }
5656 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
5657 if (Record.size() != 2)
5658 return error(Message: "Invalid catchret record");
5659 unsigned Idx = 0;
5660 Type *TokenTy = Type::getTokenTy(C&: Context);
5661 Value *CatchPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy,
5662 TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB);
5663 if (!CatchPad)
5664 return error(Message: "Invalid catchret record");
5665 BasicBlock *BB = getBasicBlock(ID: Record[Idx++]);
5666 if (!BB)
5667 return error(Message: "Invalid catchret record");
5668
5669 I = CatchReturnInst::Create(CatchPad, BB);
5670 InstructionList.push_back(Elt: I);
5671 break;
5672 }
5673 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
5674 // We must have, at minimum, the outer scope and the number of arguments.
5675 if (Record.size() < 2)
5676 return error(Message: "Invalid catchswitch record");
5677
5678 unsigned Idx = 0;
5679
5680 Type *TokenTy = Type::getTokenTy(C&: Context);
5681 Value *ParentPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy,
5682 TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB);
5683 if (!ParentPad)
5684 return error(Message: "Invalid catchswitch record");
5685
5686 unsigned NumHandlers = Record[Idx++];
5687
5688 SmallVector<BasicBlock *, 2> Handlers;
5689 for (unsigned Op = 0; Op != NumHandlers; ++Op) {
5690 BasicBlock *BB = getBasicBlock(ID: Record[Idx++]);
5691 if (!BB)
5692 return error(Message: "Invalid catchswitch record");
5693 Handlers.push_back(Elt: BB);
5694 }
5695
5696 BasicBlock *UnwindDest = nullptr;
5697 if (Idx + 1 == Record.size()) {
5698 UnwindDest = getBasicBlock(ID: Record[Idx++]);
5699 if (!UnwindDest)
5700 return error(Message: "Invalid catchswitch record");
5701 }
5702
5703 if (Record.size() != Idx)
5704 return error(Message: "Invalid catchswitch record");
5705
5706 auto *CatchSwitch =
5707 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
5708 for (BasicBlock *Handler : Handlers)
5709 CatchSwitch->addHandler(Dest: Handler);
5710 I = CatchSwitch;
5711 ResTypeID = getVirtualTypeID(Ty: I->getType());
5712 InstructionList.push_back(Elt: I);
5713 break;
5714 }
5715 case bitc::FUNC_CODE_INST_CATCHPAD:
5716 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
5717 // We must have, at minimum, the outer scope and the number of arguments.
5718 if (Record.size() < 2)
5719 return error(Message: "Invalid catchpad/cleanuppad record");
5720
5721 unsigned Idx = 0;
5722
5723 Type *TokenTy = Type::getTokenTy(C&: Context);
5724 Value *ParentPad = getValue(Record, Slot: Idx++, InstNum: NextValueNo, Ty: TokenTy,
5725 TyID: getVirtualTypeID(Ty: TokenTy), ConstExprInsertBB: CurBB);
5726 if (!ParentPad)
5727 return error(Message: "Invalid catchpad/cleanuppad record");
5728
5729 unsigned NumArgOperands = Record[Idx++];
5730
5731 SmallVector<Value *, 2> Args;
5732 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
5733 Value *Val;
5734 unsigned ValTypeID;
5735 if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: nullptr))
5736 return error(Message: "Invalid catchpad/cleanuppad record");
5737 Args.push_back(Elt: Val);
5738 }
5739
5740 if (Record.size() != Idx)
5741 return error(Message: "Invalid catchpad/cleanuppad record");
5742
5743 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
5744 I = CleanupPadInst::Create(ParentPad, Args);
5745 else
5746 I = CatchPadInst::Create(CatchSwitch: ParentPad, Args);
5747 ResTypeID = getVirtualTypeID(Ty: I->getType());
5748 InstructionList.push_back(Elt: I);
5749 break;
5750 }
5751 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
5752 // Check magic
5753 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
5754 // "New" SwitchInst format with case ranges. The changes to write this
5755 // format were reverted but we still recognize bitcode that uses it.
5756 // Hopefully someday we will have support for case ranges and can use
5757 // this format again.
5758
5759 unsigned OpTyID = Record[1];
5760 Type *OpTy = getTypeByID(ID: OpTyID);
5761 unsigned ValueBitWidth = cast<IntegerType>(Val: OpTy)->getBitWidth();
5762
5763 Value *Cond = getValue(Record, Slot: 2, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB);
5764 BasicBlock *Default = getBasicBlock(ID: Record[3]);
5765 if (!OpTy || !Cond || !Default)
5766 return error(Message: "Invalid switch record");
5767
5768 unsigned NumCases = Record[4];
5769
5770 SwitchInst *SI = SwitchInst::Create(Value: Cond, Default, NumCases);
5771 InstructionList.push_back(Elt: SI);
5772
5773 unsigned CurIdx = 5;
5774 for (unsigned i = 0; i != NumCases; ++i) {
5775 SmallVector<ConstantInt*, 1> CaseVals;
5776 unsigned NumItems = Record[CurIdx++];
5777 for (unsigned ci = 0; ci != NumItems; ++ci) {
5778 bool isSingleNumber = Record[CurIdx++];
5779
5780 APInt Low;
5781 unsigned ActiveWords = 1;
5782 if (ValueBitWidth > 64)
5783 ActiveWords = Record[CurIdx++];
5784 Low = readWideAPInt(Vals: ArrayRef(&Record[CurIdx], ActiveWords),
5785 TypeBits: ValueBitWidth);
5786 CurIdx += ActiveWords;
5787
5788 if (!isSingleNumber) {
5789 ActiveWords = 1;
5790 if (ValueBitWidth > 64)
5791 ActiveWords = Record[CurIdx++];
5792 APInt High = readWideAPInt(Vals: ArrayRef(&Record[CurIdx], ActiveWords),
5793 TypeBits: ValueBitWidth);
5794 CurIdx += ActiveWords;
5795
5796 // FIXME: It is not clear whether values in the range should be
5797 // compared as signed or unsigned values. The partially
5798 // implemented changes that used this format in the past used
5799 // unsigned comparisons.
5800 for ( ; Low.ule(RHS: High); ++Low)
5801 CaseVals.push_back(Elt: ConstantInt::get(Context, V: Low));
5802 } else
5803 CaseVals.push_back(Elt: ConstantInt::get(Context, V: Low));
5804 }
5805 BasicBlock *DestBB = getBasicBlock(ID: Record[CurIdx++]);
5806 for (ConstantInt *Cst : CaseVals)
5807 SI->addCase(OnVal: Cst, Dest: DestBB);
5808 }
5809 I = SI;
5810 break;
5811 }
5812
5813 // Old SwitchInst format without case ranges.
5814
5815 if (Record.size() < 3 || (Record.size() & 1) == 0)
5816 return error(Message: "Invalid switch record");
5817 unsigned OpTyID = Record[0];
5818 Type *OpTy = getTypeByID(ID: OpTyID);
5819 Value *Cond = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB);
5820 BasicBlock *Default = getBasicBlock(ID: Record[2]);
5821 if (!OpTy || !Cond || !Default)
5822 return error(Message: "Invalid switch record");
5823 unsigned NumCases = (Record.size()-3)/2;
5824 SwitchInst *SI = SwitchInst::Create(Value: Cond, Default, NumCases);
5825 InstructionList.push_back(Elt: SI);
5826 for (unsigned i = 0, e = NumCases; i != e; ++i) {
5827 ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>(
5828 Val: getFnValueByID(ID: Record[3+i*2], Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: nullptr));
5829 BasicBlock *DestBB = getBasicBlock(ID: Record[1+3+i*2]);
5830 if (!CaseVal || !DestBB) {
5831 delete SI;
5832 return error(Message: "Invalid switch record");
5833 }
5834 SI->addCase(OnVal: CaseVal, Dest: DestBB);
5835 }
5836 I = SI;
5837 break;
5838 }
5839 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
5840 if (Record.size() < 2)
5841 return error(Message: "Invalid indirectbr record");
5842 unsigned OpTyID = Record[0];
5843 Type *OpTy = getTypeByID(ID: OpTyID);
5844 Value *Address = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB);
5845 if (!OpTy || !Address)
5846 return error(Message: "Invalid indirectbr record");
5847 unsigned NumDests = Record.size()-2;
5848 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
5849 InstructionList.push_back(Elt: IBI);
5850 for (unsigned i = 0, e = NumDests; i != e; ++i) {
5851 if (BasicBlock *DestBB = getBasicBlock(ID: Record[2+i])) {
5852 IBI->addDestination(Dest: DestBB);
5853 } else {
5854 delete IBI;
5855 return error(Message: "Invalid indirectbr record");
5856 }
5857 }
5858 I = IBI;
5859 break;
5860 }
5861
5862 case bitc::FUNC_CODE_INST_INVOKE: {
5863 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
5864 if (Record.size() < 4)
5865 return error(Message: "Invalid invoke record");
5866 unsigned OpNum = 0;
5867 AttributeList PAL = getAttributes(i: Record[OpNum++]);
5868 unsigned CCInfo = Record[OpNum++];
5869 BasicBlock *NormalBB = getBasicBlock(ID: Record[OpNum++]);
5870 BasicBlock *UnwindBB = getBasicBlock(ID: Record[OpNum++]);
5871
5872 unsigned FTyID = InvalidTypeID;
5873 FunctionType *FTy = nullptr;
5874 if ((CCInfo >> 13) & 1) {
5875 FTyID = Record[OpNum++];
5876 FTy = dyn_cast<FunctionType>(Val: getTypeByID(ID: FTyID));
5877 if (!FTy)
5878 return error(Message: "Explicit invoke type is not a function type");
5879 }
5880
5881 Value *Callee;
5882 unsigned CalleeTypeID;
5883 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID,
5884 ConstExprInsertBB: CurBB))
5885 return error(Message: "Invalid invoke record");
5886
5887 PointerType *CalleeTy = dyn_cast<PointerType>(Val: Callee->getType());
5888 if (!CalleeTy)
5889 return error(Message: "Callee is not a pointer");
5890 if (!FTy) {
5891 FTyID = getContainedTypeID(ID: CalleeTypeID);
5892 FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID));
5893 if (!FTy)
5894 return error(Message: "Callee is not of pointer to function type");
5895 }
5896 if (Record.size() < FTy->getNumParams() + OpNum)
5897 return error(Message: "Insufficient operands to call");
5898
5899 SmallVector<Value*, 16> Ops;
5900 SmallVector<unsigned, 16> ArgTyIDs;
5901 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5902 unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1);
5903 Ops.push_back(Elt: getValue(Record, Slot: OpNum, InstNum: NextValueNo, Ty: FTy->getParamType(i),
5904 TyID: ArgTyID, ConstExprInsertBB: CurBB));
5905 ArgTyIDs.push_back(Elt: ArgTyID);
5906 if (!Ops.back())
5907 return error(Message: "Invalid invoke record");
5908 }
5909
5910 if (!FTy->isVarArg()) {
5911 if (Record.size() != OpNum)
5912 return error(Message: "Invalid invoke record");
5913 } else {
5914 // Read type/value pairs for varargs params.
5915 while (OpNum != Record.size()) {
5916 Value *Op;
5917 unsigned OpTypeID;
5918 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
5919 return error(Message: "Invalid invoke record");
5920 Ops.push_back(Elt: Op);
5921 ArgTyIDs.push_back(Elt: OpTypeID);
5922 }
5923 }
5924
5925 // Upgrade the bundles if needed.
5926 if (!OperandBundles.empty())
5927 UpgradeOperandBundles(OperandBundles);
5928
5929 I = InvokeInst::Create(Ty: FTy, Func: Callee, IfNormal: NormalBB, IfException: UnwindBB, Args: Ops,
5930 Bundles: OperandBundles);
5931 ResTypeID = getContainedTypeID(ID: FTyID);
5932 OperandBundles.clear();
5933 InstructionList.push_back(Elt: I);
5934 cast<InvokeInst>(Val: I)->setCallingConv(
5935 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
5936 cast<InvokeInst>(Val: I)->setAttributes(PAL);
5937 if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) {
5938 I->deleteValue();
5939 return Err;
5940 }
5941
5942 break;
5943 }
5944 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
5945 unsigned Idx = 0;
5946 Value *Val = nullptr;
5947 unsigned ValTypeID;
5948 if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB))
5949 return error(Message: "Invalid resume record");
5950 I = ResumeInst::Create(Exn: Val);
5951 InstructionList.push_back(Elt: I);
5952 break;
5953 }
5954 case bitc::FUNC_CODE_INST_CALLBR: {
5955 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]
5956 unsigned OpNum = 0;
5957 AttributeList PAL = getAttributes(i: Record[OpNum++]);
5958 unsigned CCInfo = Record[OpNum++];
5959
5960 BasicBlock *DefaultDest = getBasicBlock(ID: Record[OpNum++]);
5961 unsigned NumIndirectDests = Record[OpNum++];
5962 SmallVector<BasicBlock *, 16> IndirectDests;
5963 for (unsigned i = 0, e = NumIndirectDests; i != e; ++i)
5964 IndirectDests.push_back(Elt: getBasicBlock(ID: Record[OpNum++]));
5965
5966 unsigned FTyID = InvalidTypeID;
5967 FunctionType *FTy = nullptr;
5968 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
5969 FTyID = Record[OpNum++];
5970 FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID));
5971 if (!FTy)
5972 return error(Message: "Explicit call type is not a function type");
5973 }
5974
5975 Value *Callee;
5976 unsigned CalleeTypeID;
5977 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID,
5978 ConstExprInsertBB: CurBB))
5979 return error(Message: "Invalid callbr record");
5980
5981 PointerType *OpTy = dyn_cast<PointerType>(Val: Callee->getType());
5982 if (!OpTy)
5983 return error(Message: "Callee is not a pointer type");
5984 if (!FTy) {
5985 FTyID = getContainedTypeID(ID: CalleeTypeID);
5986 FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID));
5987 if (!FTy)
5988 return error(Message: "Callee is not of pointer to function type");
5989 }
5990 if (Record.size() < FTy->getNumParams() + OpNum)
5991 return error(Message: "Insufficient operands to call");
5992
5993 SmallVector<Value*, 16> Args;
5994 SmallVector<unsigned, 16> ArgTyIDs;
5995 // Read the fixed params.
5996 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5997 Value *Arg;
5998 unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1);
5999 if (FTy->getParamType(i)->isLabelTy())
6000 Arg = getBasicBlock(ID: Record[OpNum]);
6001 else
6002 Arg = getValue(Record, Slot: OpNum, InstNum: NextValueNo, Ty: FTy->getParamType(i),
6003 TyID: ArgTyID, ConstExprInsertBB: CurBB);
6004 if (!Arg)
6005 return error(Message: "Invalid callbr record");
6006 Args.push_back(Elt: Arg);
6007 ArgTyIDs.push_back(Elt: ArgTyID);
6008 }
6009
6010 // Read type/value pairs for varargs params.
6011 if (!FTy->isVarArg()) {
6012 if (OpNum != Record.size())
6013 return error(Message: "Invalid callbr record");
6014 } else {
6015 while (OpNum != Record.size()) {
6016 Value *Op;
6017 unsigned OpTypeID;
6018 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
6019 return error(Message: "Invalid callbr record");
6020 Args.push_back(Elt: Op);
6021 ArgTyIDs.push_back(Elt: OpTypeID);
6022 }
6023 }
6024
6025 // Upgrade the bundles if needed.
6026 if (!OperandBundles.empty())
6027 UpgradeOperandBundles(OperandBundles);
6028
6029 if (auto *IA = dyn_cast<InlineAsm>(Val: Callee)) {
6030 InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints();
6031 auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) {
6032 return CI.Type == InlineAsm::isLabel;
6033 };
6034 if (none_of(Range&: ConstraintInfo, P: IsLabelConstraint)) {
6035 // Upgrade explicit blockaddress arguments to label constraints.
6036 // Verify that the last arguments are blockaddress arguments that
6037 // match the indirect destinations. Clang always generates callbr
6038 // in this form. We could support reordering with more effort.
6039 unsigned FirstBlockArg = Args.size() - IndirectDests.size();
6040 for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) {
6041 unsigned LabelNo = ArgNo - FirstBlockArg;
6042 auto *BA = dyn_cast<BlockAddress>(Val: Args[ArgNo]);
6043 if (!BA || BA->getFunction() != F ||
6044 LabelNo > IndirectDests.size() ||
6045 BA->getBasicBlock() != IndirectDests[LabelNo])
6046 return error(Message: "callbr argument does not match indirect dest");
6047 }
6048
6049 // Remove blockaddress arguments.
6050 Args.erase(CS: Args.begin() + FirstBlockArg, CE: Args.end());
6051 ArgTyIDs.erase(CS: ArgTyIDs.begin() + FirstBlockArg, CE: ArgTyIDs.end());
6052
6053 // Recreate the function type with less arguments.
6054 SmallVector<Type *> ArgTys;
6055 for (Value *Arg : Args)
6056 ArgTys.push_back(Elt: Arg->getType());
6057 FTy =
6058 FunctionType::get(Result: FTy->getReturnType(), Params: ArgTys, isVarArg: FTy->isVarArg());
6059
6060 // Update constraint string to use label constraints.
6061 std::string Constraints = IA->getConstraintString().str();
6062 unsigned ArgNo = 0;
6063 size_t Pos = 0;
6064 for (const auto &CI : ConstraintInfo) {
6065 if (CI.hasArg()) {
6066 if (ArgNo >= FirstBlockArg)
6067 Constraints.insert(pos: Pos, s: "!");
6068 ++ArgNo;
6069 }
6070
6071 // Go to next constraint in string.
6072 Pos = Constraints.find(c: ',', pos: Pos);
6073 if (Pos == std::string::npos)
6074 break;
6075 ++Pos;
6076 }
6077
6078 Callee = InlineAsm::get(Ty: FTy, AsmString: IA->getAsmString(), Constraints,
6079 hasSideEffects: IA->hasSideEffects(), isAlignStack: IA->isAlignStack(),
6080 asmDialect: IA->getDialect(), canThrow: IA->canThrow());
6081 }
6082 }
6083
6084 I = CallBrInst::Create(Ty: FTy, Func: Callee, DefaultDest, IndirectDests, Args,
6085 Bundles: OperandBundles);
6086 ResTypeID = getContainedTypeID(ID: FTyID);
6087 OperandBundles.clear();
6088 InstructionList.push_back(Elt: I);
6089 cast<CallBrInst>(Val: I)->setCallingConv(
6090 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
6091 cast<CallBrInst>(Val: I)->setAttributes(PAL);
6092 if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) {
6093 I->deleteValue();
6094 return Err;
6095 }
6096 break;
6097 }
6098 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
6099 I = new UnreachableInst(Context);
6100 InstructionList.push_back(Elt: I);
6101 break;
6102 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
6103 if (Record.empty())
6104 return error(Message: "Invalid phi record");
6105 // The first record specifies the type.
6106 unsigned TyID = Record[0];
6107 Type *Ty = getTypeByID(ID: TyID);
6108 if (!Ty)
6109 return error(Message: "Invalid phi record");
6110
6111 // Phi arguments are pairs of records of [value, basic block].
6112 // There is an optional final record for fast-math-flags if this phi has a
6113 // floating-point type.
6114 size_t NumArgs = (Record.size() - 1) / 2;
6115 PHINode *PN = PHINode::Create(Ty, NumReservedValues: NumArgs);
6116 if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(Val: PN)) {
6117 PN->deleteValue();
6118 return error(Message: "Invalid phi record");
6119 }
6120 InstructionList.push_back(Elt: PN);
6121
6122 SmallDenseMap<BasicBlock *, Value *> Args;
6123 for (unsigned i = 0; i != NumArgs; i++) {
6124 BasicBlock *BB = getBasicBlock(ID: Record[i * 2 + 2]);
6125 if (!BB) {
6126 PN->deleteValue();
6127 return error(Message: "Invalid phi BB");
6128 }
6129
6130 // Phi nodes may contain the same predecessor multiple times, in which
6131 // case the incoming value must be identical. Directly reuse the already
6132 // seen value here, to avoid expanding a constant expression multiple
6133 // times.
6134 auto It = Args.find(Val: BB);
6135 BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup(Key: {BB, CurBB});
6136 if (It != Args.end()) {
6137 // If this predecessor was also replaced with a constexpr basic
6138 // block, it must be de-duplicated.
6139 if (!EdgeBB) {
6140 PN->addIncoming(V: It->second, BB);
6141 }
6142 continue;
6143 }
6144
6145 // If there already is a block for this edge (from a different phi),
6146 // use it.
6147 if (!EdgeBB) {
6148 // Otherwise, use a temporary block (that we will discard if it
6149 // turns out to be unnecessary).
6150 if (!PhiConstExprBB)
6151 PhiConstExprBB = BasicBlock::Create(Context, Name: "phi.constexpr", Parent: F);
6152 EdgeBB = PhiConstExprBB;
6153 }
6154
6155 // With the new function encoding, it is possible that operands have
6156 // negative IDs (for forward references). Use a signed VBR
6157 // representation to keep the encoding small.
6158 Value *V;
6159 if (UseRelativeIDs)
6160 V = getValueSigned(Record, Slot: i * 2 + 1, InstNum: NextValueNo, Ty, TyID, ConstExprInsertBB: EdgeBB);
6161 else
6162 V = getValue(Record, Slot: i * 2 + 1, InstNum: NextValueNo, Ty, TyID, ConstExprInsertBB: EdgeBB);
6163 if (!V) {
6164 PN->deleteValue();
6165 PhiConstExprBB->eraseFromParent();
6166 return error(Message: "Invalid phi record");
6167 }
6168
6169 if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) {
6170 ConstExprEdgeBBs.insert(KV: {{BB, CurBB}, EdgeBB});
6171 PhiConstExprBB = nullptr;
6172 }
6173 PN->addIncoming(V, BB);
6174 Args.insert(KV: {BB, V});
6175 }
6176 I = PN;
6177 ResTypeID = TyID;
6178
6179 // If there are an even number of records, the final record must be FMF.
6180 if (Record.size() % 2 == 0) {
6181 assert(isa<FPMathOperator>(I) && "Unexpected phi type");
6182 FastMathFlags FMF = getDecodedFastMathFlags(Val: Record[Record.size() - 1]);
6183 if (FMF.any())
6184 I->setFastMathFlags(FMF);
6185 }
6186
6187 break;
6188 }
6189
6190 case bitc::FUNC_CODE_INST_LANDINGPAD:
6191 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
6192 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
6193 unsigned Idx = 0;
6194 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
6195 if (Record.size() < 3)
6196 return error(Message: "Invalid landingpad record");
6197 } else {
6198 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
6199 if (Record.size() < 4)
6200 return error(Message: "Invalid landingpad record");
6201 }
6202 ResTypeID = Record[Idx++];
6203 Type *Ty = getTypeByID(ID: ResTypeID);
6204 if (!Ty)
6205 return error(Message: "Invalid landingpad record");
6206 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
6207 Value *PersFn = nullptr;
6208 unsigned PersFnTypeID;
6209 if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: PersFn, TypeID&: PersFnTypeID,
6210 ConstExprInsertBB: nullptr))
6211 return error(Message: "Invalid landingpad record");
6212
6213 if (!F->hasPersonalityFn())
6214 F->setPersonalityFn(cast<Constant>(Val: PersFn));
6215 else if (F->getPersonalityFn() != cast<Constant>(Val: PersFn))
6216 return error(Message: "Personality function mismatch");
6217 }
6218
6219 bool IsCleanup = !!Record[Idx++];
6220 unsigned NumClauses = Record[Idx++];
6221 LandingPadInst *LP = LandingPadInst::Create(RetTy: Ty, NumReservedClauses: NumClauses);
6222 LP->setCleanup(IsCleanup);
6223 for (unsigned J = 0; J != NumClauses; ++J) {
6224 LandingPadInst::ClauseType CT =
6225 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
6226 Value *Val;
6227 unsigned ValTypeID;
6228
6229 if (getValueTypePair(Record, Slot&: Idx, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID,
6230 ConstExprInsertBB: nullptr)) {
6231 delete LP;
6232 return error(Message: "Invalid landingpad record");
6233 }
6234
6235 assert((CT != LandingPadInst::Catch ||
6236 !isa<ArrayType>(Val->getType())) &&
6237 "Catch clause has a invalid type!");
6238 assert((CT != LandingPadInst::Filter ||
6239 isa<ArrayType>(Val->getType())) &&
6240 "Filter clause has invalid type!");
6241 LP->addClause(ClauseVal: cast<Constant>(Val));
6242 }
6243
6244 I = LP;
6245 InstructionList.push_back(Elt: I);
6246 break;
6247 }
6248
6249 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
6250 if (Record.size() != 4 && Record.size() != 5)
6251 return error(Message: "Invalid alloca record");
6252 using APV = AllocaPackedValues;
6253 const uint64_t Rec = Record[3];
6254 const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Packed: Rec);
6255 const bool SwiftError = Bitfield::get<APV::SwiftError>(Packed: Rec);
6256 unsigned TyID = Record[0];
6257 Type *Ty = getTypeByID(ID: TyID);
6258 if (!Bitfield::get<APV::ExplicitType>(Packed: Rec)) {
6259 TyID = getContainedTypeID(ID: TyID);
6260 Ty = getTypeByID(ID: TyID);
6261 if (!Ty)
6262 return error(Message: "Missing element type for old-style alloca");
6263 }
6264 unsigned OpTyID = Record[1];
6265 Type *OpTy = getTypeByID(ID: OpTyID);
6266 Value *Size = getFnValueByID(ID: Record[2], Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB);
6267 MaybeAlign Align;
6268 uint64_t AlignExp =
6269 Bitfield::get<APV::AlignLower>(Packed: Rec) |
6270 (Bitfield::get<APV::AlignUpper>(Packed: Rec) << APV::AlignLower::Bits);
6271 if (Error Err = parseAlignmentValue(Exponent: AlignExp, Alignment&: Align)) {
6272 return Err;
6273 }
6274 if (!Ty || !Size)
6275 return error(Message: "Invalid alloca record");
6276
6277 const DataLayout &DL = TheModule->getDataLayout();
6278 unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace();
6279
6280 SmallPtrSet<Type *, 4> Visited;
6281 if (!Align && !Ty->isSized(Visited: &Visited))
6282 return error(Message: "alloca of unsized type");
6283 if (!Align)
6284 Align = DL.getPrefTypeAlign(Ty);
6285
6286 if (!Size->getType()->isIntegerTy())
6287 return error(Message: "alloca element count must have integer type");
6288
6289 AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align);
6290 AI->setUsedWithInAlloca(InAlloca);
6291 AI->setSwiftError(SwiftError);
6292 I = AI;
6293 ResTypeID = getVirtualTypeID(Ty: AI->getType(), ChildTypeIDs: TyID);
6294 InstructionList.push_back(Elt: I);
6295 break;
6296 }
6297 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
6298 unsigned OpNum = 0;
6299 Value *Op;
6300 unsigned OpTypeID;
6301 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) ||
6302 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
6303 return error(Message: "Invalid load record");
6304
6305 if (!isa<PointerType>(Val: Op->getType()))
6306 return error(Message: "Load operand is not a pointer type");
6307
6308 Type *Ty = nullptr;
6309 if (OpNum + 3 == Record.size()) {
6310 ResTypeID = Record[OpNum++];
6311 Ty = getTypeByID(ID: ResTypeID);
6312 } else {
6313 ResTypeID = getContainedTypeID(ID: OpTypeID);
6314 Ty = getTypeByID(ID: ResTypeID);
6315 }
6316
6317 if (!Ty)
6318 return error(Message: "Missing load type");
6319
6320 if (Error Err = typeCheckLoadStoreInst(ValType: Ty, PtrType: Op->getType()))
6321 return Err;
6322
6323 MaybeAlign Align;
6324 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align))
6325 return Err;
6326 SmallPtrSet<Type *, 4> Visited;
6327 if (!Align && !Ty->isSized(Visited: &Visited))
6328 return error(Message: "load of unsized type");
6329 if (!Align)
6330 Align = TheModule->getDataLayout().getABITypeAlign(Ty);
6331 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align);
6332 InstructionList.push_back(Elt: I);
6333 break;
6334 }
6335 case bitc::FUNC_CODE_INST_LOADATOMIC: {
6336 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
6337 unsigned OpNum = 0;
6338 Value *Op;
6339 unsigned OpTypeID;
6340 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB) ||
6341 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
6342 return error(Message: "Invalid load atomic record");
6343
6344 if (!isa<PointerType>(Val: Op->getType()))
6345 return error(Message: "Load operand is not a pointer type");
6346
6347 Type *Ty = nullptr;
6348 if (OpNum + 5 == Record.size()) {
6349 ResTypeID = Record[OpNum++];
6350 Ty = getTypeByID(ID: ResTypeID);
6351 } else {
6352 ResTypeID = getContainedTypeID(ID: OpTypeID);
6353 Ty = getTypeByID(ID: ResTypeID);
6354 }
6355
6356 if (!Ty)
6357 return error(Message: "Missing atomic load type");
6358
6359 if (Error Err = typeCheckLoadStoreInst(ValType: Ty, PtrType: Op->getType()))
6360 return Err;
6361
6362 AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]);
6363 if (Ordering == AtomicOrdering::NotAtomic ||
6364 Ordering == AtomicOrdering::Release ||
6365 Ordering == AtomicOrdering::AcquireRelease)
6366 return error(Message: "Invalid load atomic record");
6367 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
6368 return error(Message: "Invalid load atomic record");
6369 SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]);
6370
6371 MaybeAlign Align;
6372 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align))
6373 return Err;
6374 if (!Align)
6375 return error(Message: "Alignment missing from atomic load");
6376 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID);
6377 InstructionList.push_back(Elt: I);
6378 break;
6379 }
6380 case bitc::FUNC_CODE_INST_STORE:
6381 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
6382 unsigned OpNum = 0;
6383 Value *Val, *Ptr;
6384 unsigned PtrTypeID, ValTypeID;
6385 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB))
6386 return error(Message: "Invalid store record");
6387
6388 if (BitCode == bitc::FUNC_CODE_INST_STORE) {
6389 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB))
6390 return error(Message: "Invalid store record");
6391 } else {
6392 ValTypeID = getContainedTypeID(ID: PtrTypeID);
6393 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: ValTypeID),
6394 TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB))
6395 return error(Message: "Invalid store record");
6396 }
6397
6398 if (OpNum + 2 != Record.size())
6399 return error(Message: "Invalid store record");
6400
6401 if (Error Err = typeCheckLoadStoreInst(ValType: Val->getType(), PtrType: Ptr->getType()))
6402 return Err;
6403 MaybeAlign Align;
6404 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align))
6405 return Err;
6406 SmallPtrSet<Type *, 4> Visited;
6407 if (!Align && !Val->getType()->isSized(Visited: &Visited))
6408 return error(Message: "store of unsized type");
6409 if (!Align)
6410 Align = TheModule->getDataLayout().getABITypeAlign(Ty: Val->getType());
6411 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align);
6412 InstructionList.push_back(Elt: I);
6413 break;
6414 }
6415 case bitc::FUNC_CODE_INST_STOREATOMIC:
6416 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
6417 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
6418 unsigned OpNum = 0;
6419 Value *Val, *Ptr;
6420 unsigned PtrTypeID, ValTypeID;
6421 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB) ||
6422 !isa<PointerType>(Val: Ptr->getType()))
6423 return error(Message: "Invalid store atomic record");
6424 if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) {
6425 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB))
6426 return error(Message: "Invalid store atomic record");
6427 } else {
6428 ValTypeID = getContainedTypeID(ID: PtrTypeID);
6429 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: ValTypeID),
6430 TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB))
6431 return error(Message: "Invalid store atomic record");
6432 }
6433
6434 if (OpNum + 4 != Record.size())
6435 return error(Message: "Invalid store atomic record");
6436
6437 if (Error Err = typeCheckLoadStoreInst(ValType: Val->getType(), PtrType: Ptr->getType()))
6438 return Err;
6439 AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]);
6440 if (Ordering == AtomicOrdering::NotAtomic ||
6441 Ordering == AtomicOrdering::Acquire ||
6442 Ordering == AtomicOrdering::AcquireRelease)
6443 return error(Message: "Invalid store atomic record");
6444 SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]);
6445 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
6446 return error(Message: "Invalid store atomic record");
6447
6448 MaybeAlign Align;
6449 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum], Alignment&: Align))
6450 return Err;
6451 if (!Align)
6452 return error(Message: "Alignment missing from atomic store");
6453 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID);
6454 InstructionList.push_back(Elt: I);
6455 break;
6456 }
6457 case bitc::FUNC_CODE_INST_CMPXCHG_OLD: {
6458 // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, syncscope,
6459 // failure_ordering?, weak?]
6460 const size_t NumRecords = Record.size();
6461 unsigned OpNum = 0;
6462 Value *Ptr = nullptr;
6463 unsigned PtrTypeID;
6464 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB))
6465 return error(Message: "Invalid cmpxchg record");
6466
6467 if (!isa<PointerType>(Val: Ptr->getType()))
6468 return error(Message: "Cmpxchg operand is not a pointer type");
6469
6470 Value *Cmp = nullptr;
6471 unsigned CmpTypeID = getContainedTypeID(ID: PtrTypeID);
6472 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: getTypeByID(ID: CmpTypeID),
6473 TyID: CmpTypeID, ResVal&: Cmp, ConstExprInsertBB: CurBB))
6474 return error(Message: "Invalid cmpxchg record");
6475
6476 Value *New = nullptr;
6477 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Cmp->getType(), TyID: CmpTypeID,
6478 ResVal&: New, ConstExprInsertBB: CurBB) ||
6479 NumRecords < OpNum + 3 || NumRecords > OpNum + 5)
6480 return error(Message: "Invalid cmpxchg record");
6481
6482 const AtomicOrdering SuccessOrdering =
6483 getDecodedOrdering(Val: Record[OpNum + 1]);
6484 if (SuccessOrdering == AtomicOrdering::NotAtomic ||
6485 SuccessOrdering == AtomicOrdering::Unordered)
6486 return error(Message: "Invalid cmpxchg record");
6487
6488 const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 2]);
6489
6490 if (Error Err = typeCheckLoadStoreInst(ValType: Cmp->getType(), PtrType: Ptr->getType()))
6491 return Err;
6492
6493 const AtomicOrdering FailureOrdering =
6494 NumRecords < 7
6495 ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering)
6496 : getDecodedOrdering(Val: Record[OpNum + 3]);
6497
6498 if (FailureOrdering == AtomicOrdering::NotAtomic ||
6499 FailureOrdering == AtomicOrdering::Unordered)
6500 return error(Message: "Invalid cmpxchg record");
6501
6502 const Align Alignment(
6503 TheModule->getDataLayout().getTypeStoreSize(Ty: Cmp->getType()));
6504
6505 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering,
6506 FailureOrdering, SSID);
6507 cast<AtomicCmpXchgInst>(Val: I)->setVolatile(Record[OpNum]);
6508
6509 if (NumRecords < 8) {
6510 // Before weak cmpxchgs existed, the instruction simply returned the
6511 // value loaded from memory, so bitcode files from that era will be
6512 // expecting the first component of a modern cmpxchg.
6513 I->insertInto(ParentBB: CurBB, It: CurBB->end());
6514 I = ExtractValueInst::Create(Agg: I, Idxs: 0);
6515 ResTypeID = CmpTypeID;
6516 } else {
6517 cast<AtomicCmpXchgInst>(Val: I)->setWeak(Record[OpNum + 4]);
6518 unsigned I1TypeID = getVirtualTypeID(Ty: Type::getInt1Ty(C&: Context));
6519 ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: {CmpTypeID, I1TypeID});
6520 }
6521
6522 InstructionList.push_back(Elt: I);
6523 break;
6524 }
6525 case bitc::FUNC_CODE_INST_CMPXCHG: {
6526 // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, syncscope,
6527 // failure_ordering, weak, align?]
6528 const size_t NumRecords = Record.size();
6529 unsigned OpNum = 0;
6530 Value *Ptr = nullptr;
6531 unsigned PtrTypeID;
6532 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB))
6533 return error(Message: "Invalid cmpxchg record");
6534
6535 if (!isa<PointerType>(Val: Ptr->getType()))
6536 return error(Message: "Cmpxchg operand is not a pointer type");
6537
6538 Value *Cmp = nullptr;
6539 unsigned CmpTypeID;
6540 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Cmp, TypeID&: CmpTypeID, ConstExprInsertBB: CurBB))
6541 return error(Message: "Invalid cmpxchg record");
6542
6543 Value *Val = nullptr;
6544 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo, Ty: Cmp->getType(), TyID: CmpTypeID, ResVal&: Val,
6545 ConstExprInsertBB: CurBB))
6546 return error(Message: "Invalid cmpxchg record");
6547
6548 if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6)
6549 return error(Message: "Invalid cmpxchg record");
6550
6551 const bool IsVol = Record[OpNum];
6552
6553 const AtomicOrdering SuccessOrdering =
6554 getDecodedOrdering(Val: Record[OpNum + 1]);
6555 if (!AtomicCmpXchgInst::isValidSuccessOrdering(Ordering: SuccessOrdering))
6556 return error(Message: "Invalid cmpxchg success ordering");
6557
6558 const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 2]);
6559
6560 if (Error Err = typeCheckLoadStoreInst(ValType: Cmp->getType(), PtrType: Ptr->getType()))
6561 return Err;
6562
6563 const AtomicOrdering FailureOrdering =
6564 getDecodedOrdering(Val: Record[OpNum + 3]);
6565 if (!AtomicCmpXchgInst::isValidFailureOrdering(Ordering: FailureOrdering))
6566 return error(Message: "Invalid cmpxchg failure ordering");
6567
6568 const bool IsWeak = Record[OpNum + 4];
6569
6570 MaybeAlign Alignment;
6571
6572 if (NumRecords == (OpNum + 6)) {
6573 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum + 5], Alignment))
6574 return Err;
6575 }
6576 if (!Alignment)
6577 Alignment =
6578 Align(TheModule->getDataLayout().getTypeStoreSize(Ty: Cmp->getType()));
6579
6580 I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering,
6581 FailureOrdering, SSID);
6582 cast<AtomicCmpXchgInst>(Val: I)->setVolatile(IsVol);
6583 cast<AtomicCmpXchgInst>(Val: I)->setWeak(IsWeak);
6584
6585 unsigned I1TypeID = getVirtualTypeID(Ty: Type::getInt1Ty(C&: Context));
6586 ResTypeID = getVirtualTypeID(Ty: I->getType(), ChildTypeIDs: {CmpTypeID, I1TypeID});
6587
6588 InstructionList.push_back(Elt: I);
6589 break;
6590 }
6591 case bitc::FUNC_CODE_INST_ATOMICRMW_OLD:
6592 case bitc::FUNC_CODE_INST_ATOMICRMW: {
6593 // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?]
6594 // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?]
6595 const size_t NumRecords = Record.size();
6596 unsigned OpNum = 0;
6597
6598 Value *Ptr = nullptr;
6599 unsigned PtrTypeID;
6600 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Ptr, TypeID&: PtrTypeID, ConstExprInsertBB: CurBB))
6601 return error(Message: "Invalid atomicrmw record");
6602
6603 if (!isa<PointerType>(Val: Ptr->getType()))
6604 return error(Message: "Invalid atomicrmw record");
6605
6606 Value *Val = nullptr;
6607 unsigned ValTypeID = InvalidTypeID;
6608 if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) {
6609 ValTypeID = getContainedTypeID(ID: PtrTypeID);
6610 if (popValue(Record, Slot&: OpNum, InstNum: NextValueNo,
6611 Ty: getTypeByID(ID: ValTypeID), TyID: ValTypeID, ResVal&: Val, ConstExprInsertBB: CurBB))
6612 return error(Message: "Invalid atomicrmw record");
6613 } else {
6614 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Val, TypeID&: ValTypeID, ConstExprInsertBB: CurBB))
6615 return error(Message: "Invalid atomicrmw record");
6616 }
6617
6618 if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5)))
6619 return error(Message: "Invalid atomicrmw record");
6620
6621 const AtomicRMWInst::BinOp Operation =
6622 getDecodedRMWOperation(Val: Record[OpNum]);
6623 if (Operation < AtomicRMWInst::FIRST_BINOP ||
6624 Operation > AtomicRMWInst::LAST_BINOP)
6625 return error(Message: "Invalid atomicrmw record");
6626
6627 const bool IsVol = Record[OpNum + 1];
6628
6629 const AtomicOrdering Ordering = getDecodedOrdering(Val: Record[OpNum + 2]);
6630 if (Ordering == AtomicOrdering::NotAtomic ||
6631 Ordering == AtomicOrdering::Unordered)
6632 return error(Message: "Invalid atomicrmw record");
6633
6634 const SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[OpNum + 3]);
6635
6636 MaybeAlign Alignment;
6637
6638 if (NumRecords == (OpNum + 5)) {
6639 if (Error Err = parseAlignmentValue(Exponent: Record[OpNum + 4], Alignment))
6640 return Err;
6641 }
6642
6643 if (!Alignment)
6644 Alignment =
6645 Align(TheModule->getDataLayout().getTypeStoreSize(Ty: Val->getType()));
6646
6647 I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID);
6648 ResTypeID = ValTypeID;
6649 cast<AtomicRMWInst>(Val: I)->setVolatile(IsVol);
6650
6651 InstructionList.push_back(Elt: I);
6652 break;
6653 }
6654 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid]
6655 if (2 != Record.size())
6656 return error(Message: "Invalid fence record");
6657 AtomicOrdering Ordering = getDecodedOrdering(Val: Record[0]);
6658 if (Ordering == AtomicOrdering::NotAtomic ||
6659 Ordering == AtomicOrdering::Unordered ||
6660 Ordering == AtomicOrdering::Monotonic)
6661 return error(Message: "Invalid fence record");
6662 SyncScope::ID SSID = getDecodedSyncScopeID(Val: Record[1]);
6663 I = new FenceInst(Context, Ordering, SSID);
6664 InstructionList.push_back(Elt: I);
6665 break;
6666 }
6667 case bitc::FUNC_CODE_DEBUG_RECORD_LABEL: {
6668 // DbgLabelRecords are placed after the Instructions that they are
6669 // attached to.
6670 SeenDebugRecord = true;
6671 Instruction *Inst = getLastInstruction();
6672 if (!Inst)
6673 return error(Message: "Invalid dbg record: missing instruction");
6674 DILocation *DIL = cast<DILocation>(Val: getFnMetadataByID(ID: Record[0]));
6675 DILabel *Label = cast<DILabel>(Val: getFnMetadataByID(ID: Record[1]));
6676 Inst->getParent()->insertDbgRecordBefore(
6677 DR: new DbgLabelRecord(Label, DebugLoc(DIL)), Here: Inst->getIterator());
6678 continue; // This isn't an instruction.
6679 }
6680 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE:
6681 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE:
6682 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE:
6683 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:
6684 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
6685 // DbgVariableRecords are placed after the Instructions that they are
6686 // attached to.
6687 SeenDebugRecord = true;
6688 Instruction *Inst = getLastInstruction();
6689 if (!Inst)
6690 return error(Message: "Invalid dbg record: missing instruction");
6691
6692 // First 3 fields are common to all kinds:
6693 // DILocation, DILocalVariable, DIExpression
6694 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)
6695 // ..., LocationMetadata
6696 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)
6697 // ..., Value
6698 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
6699 // ..., LocationMetadata
6700 // dbg_declare_value (FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE)
6701 // ..., LocationMetadata
6702 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
6703 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
6704 unsigned Slot = 0;
6705 // Common fields (0-2).
6706 DILocation *DIL = cast<DILocation>(Val: getFnMetadataByID(ID: Record[Slot++]));
6707 DILocalVariable *Var =
6708 cast<DILocalVariable>(Val: getFnMetadataByID(ID: Record[Slot++]));
6709 DIExpression *Expr =
6710 cast<DIExpression>(Val: getFnMetadataByID(ID: Record[Slot++]));
6711
6712 // Union field (3: LocationMetadata | Value).
6713 Metadata *RawLocation = nullptr;
6714 if (BitCode == bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE) {
6715 Value *V = nullptr;
6716 unsigned TyID = 0;
6717 // We never expect to see a fwd reference value here because
6718 // use-before-defs are encoded with the standard non-abbrev record
6719 // type (they'd require encoding the type too, and they're rare). As a
6720 // result, getValueTypePair only ever increments Slot by one here (once
6721 // for the value, never twice for value and type).
6722 unsigned SlotBefore = Slot;
6723 if (getValueTypePair(Record, Slot, InstNum: NextValueNo, ResVal&: V, TypeID&: TyID, ConstExprInsertBB: CurBB))
6724 return error(Message: "Invalid dbg record: invalid value");
6725 (void)SlotBefore;
6726 assert((SlotBefore == Slot - 1) && "unexpected fwd ref");
6727 RawLocation = ValueAsMetadata::get(V);
6728 } else {
6729 RawLocation = getFnMetadataByID(ID: Record[Slot++]);
6730 }
6731
6732 DbgVariableRecord *DVR = nullptr;
6733 switch (BitCode) {
6734 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE:
6735 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE:
6736 DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL,
6737 DbgVariableRecord::LocationType::Value);
6738 break;
6739 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE:
6740 DVR = new DbgVariableRecord(RawLocation, Var, Expr, DIL,
6741 DbgVariableRecord::LocationType::Declare);
6742 break;
6743 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE:
6744 DVR = new DbgVariableRecord(
6745 RawLocation, Var, Expr, DIL,
6746 DbgVariableRecord::LocationType::DeclareValue);
6747 break;
6748 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
6749 DIAssignID *ID = cast<DIAssignID>(Val: getFnMetadataByID(ID: Record[Slot++]));
6750 DIExpression *AddrExpr =
6751 cast<DIExpression>(Val: getFnMetadataByID(ID: Record[Slot++]));
6752 Metadata *Addr = getFnMetadataByID(ID: Record[Slot++]);
6753 DVR = new DbgVariableRecord(RawLocation, Var, Expr, ID, Addr, AddrExpr,
6754 DIL);
6755 break;
6756 }
6757 default:
6758 llvm_unreachable("Unknown DbgVariableRecord bitcode");
6759 }
6760 Inst->getParent()->insertDbgRecordBefore(DR: DVR, Here: Inst->getIterator());
6761 continue; // This isn't an instruction.
6762 }
6763 case bitc::FUNC_CODE_INST_CALL: {
6764 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
6765 if (Record.size() < 3)
6766 return error(Message: "Invalid call record");
6767
6768 unsigned OpNum = 0;
6769 AttributeList PAL = getAttributes(i: Record[OpNum++]);
6770 unsigned CCInfo = Record[OpNum++];
6771
6772 FastMathFlags FMF;
6773 if ((CCInfo >> bitc::CALL_FMF) & 1) {
6774 FMF = getDecodedFastMathFlags(Val: Record[OpNum++]);
6775 if (!FMF.any())
6776 return error(Message: "Fast math flags indicator set for call with no FMF");
6777 }
6778
6779 unsigned FTyID = InvalidTypeID;
6780 FunctionType *FTy = nullptr;
6781 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
6782 FTyID = Record[OpNum++];
6783 FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID));
6784 if (!FTy)
6785 return error(Message: "Explicit call type is not a function type");
6786 }
6787
6788 Value *Callee;
6789 unsigned CalleeTypeID;
6790 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Callee, TypeID&: CalleeTypeID,
6791 ConstExprInsertBB: CurBB))
6792 return error(Message: "Invalid call record");
6793
6794 PointerType *OpTy = dyn_cast<PointerType>(Val: Callee->getType());
6795 if (!OpTy)
6796 return error(Message: "Callee is not a pointer type");
6797 if (!FTy) {
6798 FTyID = getContainedTypeID(ID: CalleeTypeID);
6799 FTy = dyn_cast_or_null<FunctionType>(Val: getTypeByID(ID: FTyID));
6800 if (!FTy)
6801 return error(Message: "Callee is not of pointer to function type");
6802 }
6803 if (Record.size() < FTy->getNumParams() + OpNum)
6804 return error(Message: "Insufficient operands to call");
6805
6806 SmallVector<Value*, 16> Args;
6807 SmallVector<unsigned, 16> ArgTyIDs;
6808 // Read the fixed params.
6809 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
6810 unsigned ArgTyID = getContainedTypeID(ID: FTyID, Idx: i + 1);
6811 if (FTy->getParamType(i)->isLabelTy())
6812 Args.push_back(Elt: getBasicBlock(ID: Record[OpNum]));
6813 else
6814 Args.push_back(Elt: getValue(Record, Slot: OpNum, InstNum: NextValueNo,
6815 Ty: FTy->getParamType(i), TyID: ArgTyID, ConstExprInsertBB: CurBB));
6816 ArgTyIDs.push_back(Elt: ArgTyID);
6817 if (!Args.back())
6818 return error(Message: "Invalid call record");
6819 }
6820
6821 // Read type/value pairs for varargs params.
6822 if (!FTy->isVarArg()) {
6823 if (OpNum != Record.size())
6824 return error(Message: "Invalid call record");
6825 } else {
6826 while (OpNum != Record.size()) {
6827 Value *Op;
6828 unsigned OpTypeID;
6829 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
6830 return error(Message: "Invalid call record");
6831 Args.push_back(Elt: Op);
6832 ArgTyIDs.push_back(Elt: OpTypeID);
6833 }
6834 }
6835
6836 // Upgrade the bundles if needed.
6837 if (!OperandBundles.empty())
6838 UpgradeOperandBundles(OperandBundles);
6839
6840 I = CallInst::Create(Ty: FTy, Func: Callee, Args, Bundles: OperandBundles);
6841 ResTypeID = getContainedTypeID(ID: FTyID);
6842 OperandBundles.clear();
6843 InstructionList.push_back(Elt: I);
6844 cast<CallInst>(Val: I)->setCallingConv(
6845 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
6846 CallInst::TailCallKind TCK = CallInst::TCK_None;
6847 if (CCInfo & (1 << bitc::CALL_TAIL))
6848 TCK = CallInst::TCK_Tail;
6849 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
6850 TCK = CallInst::TCK_MustTail;
6851 if (CCInfo & (1 << bitc::CALL_NOTAIL))
6852 TCK = CallInst::TCK_NoTail;
6853 cast<CallInst>(Val: I)->setTailCallKind(TCK);
6854 cast<CallInst>(Val: I)->setAttributes(PAL);
6855 if (isa<DbgInfoIntrinsic>(Val: I))
6856 SeenDebugIntrinsic = true;
6857 if (Error Err = propagateAttributeTypes(CB: cast<CallBase>(Val: I), ArgTyIDs)) {
6858 I->deleteValue();
6859 return Err;
6860 }
6861 if (FMF.any()) {
6862 if (!isa<FPMathOperator>(Val: I))
6863 return error(Message: "Fast-math-flags specified for call without "
6864 "floating-point scalar or vector return type");
6865 I->setFastMathFlags(FMF);
6866 }
6867 break;
6868 }
6869 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
6870 if (Record.size() < 3)
6871 return error(Message: "Invalid va_arg record");
6872 unsigned OpTyID = Record[0];
6873 Type *OpTy = getTypeByID(ID: OpTyID);
6874 Value *Op = getValue(Record, Slot: 1, InstNum: NextValueNo, Ty: OpTy, TyID: OpTyID, ConstExprInsertBB: CurBB);
6875 ResTypeID = Record[2];
6876 Type *ResTy = getTypeByID(ID: ResTypeID);
6877 if (!OpTy || !Op || !ResTy)
6878 return error(Message: "Invalid va_arg record");
6879 I = new VAArgInst(Op, ResTy);
6880 InstructionList.push_back(Elt: I);
6881 break;
6882 }
6883
6884 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
6885 // A call or an invoke can be optionally prefixed with some variable
6886 // number of operand bundle blocks. These blocks are read into
6887 // OperandBundles and consumed at the next call or invoke instruction.
6888
6889 if (Record.empty() || Record[0] >= BundleTags.size())
6890 return error(Message: "Invalid operand bundle record");
6891
6892 std::vector<Value *> Inputs;
6893
6894 unsigned OpNum = 1;
6895 while (OpNum != Record.size()) {
6896 Value *Op;
6897 if (getValueOrMetadata(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, ConstExprInsertBB: CurBB))
6898 return error(Message: "Invalid operand bundle record");
6899 Inputs.push_back(x: Op);
6900 }
6901
6902 OperandBundles.emplace_back(args&: BundleTags[Record[0]], args: std::move(Inputs));
6903 continue;
6904 }
6905
6906 case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval]
6907 unsigned OpNum = 0;
6908 Value *Op = nullptr;
6909 unsigned OpTypeID;
6910 if (getValueTypePair(Record, Slot&: OpNum, InstNum: NextValueNo, ResVal&: Op, TypeID&: OpTypeID, ConstExprInsertBB: CurBB))
6911 return error(Message: "Invalid freeze record");
6912 if (OpNum != Record.size())
6913 return error(Message: "Invalid freeze record");
6914
6915 I = new FreezeInst(Op);
6916 ResTypeID = OpTypeID;
6917 InstructionList.push_back(Elt: I);
6918 break;
6919 }
6920 }
6921
6922 // Add instruction to end of current BB. If there is no current BB, reject
6923 // this file.
6924 if (!CurBB) {
6925 I->deleteValue();
6926 return error(Message: "Invalid instruction with no BB");
6927 }
6928 if (!OperandBundles.empty()) {
6929 I->deleteValue();
6930 return error(Message: "Operand bundles found with no consumer");
6931 }
6932 I->insertInto(ParentBB: CurBB, It: CurBB->end());
6933
6934 // If this was a terminator instruction, move to the next block.
6935 if (I->isTerminator()) {
6936 ++CurBBNo;
6937 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
6938 }
6939
6940 // Non-void values get registered in the value table for future use.
6941 if (!I->getType()->isVoidTy()) {
6942 assert(I->getType() == getTypeByID(ResTypeID) &&
6943 "Incorrect result type ID");
6944 if (Error Err = ValueList.assignValue(Idx: NextValueNo++, V: I, TypeID: ResTypeID))
6945 return Err;
6946 }
6947 }
6948
6949OutOfRecordLoop:
6950
6951 if (!OperandBundles.empty())
6952 return error(Message: "Operand bundles found with no consumer");
6953
6954 // Check the function list for unresolved values.
6955 if (Argument *A = dyn_cast<Argument>(Val: ValueList.back())) {
6956 if (!A->getParent()) {
6957 // We found at least one unresolved value. Nuke them all to avoid leaks.
6958 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
6959 if ((A = dyn_cast_or_null<Argument>(Val: ValueList[i])) && !A->getParent()) {
6960 A->replaceAllUsesWith(V: PoisonValue::get(T: A->getType()));
6961 delete A;
6962 }
6963 }
6964 return error(Message: "Never resolved value found in function");
6965 }
6966 }
6967
6968 // Unexpected unresolved metadata about to be dropped.
6969 if (MDLoader->hasFwdRefs())
6970 return error(Message: "Invalid function metadata: outgoing forward refs");
6971
6972 if (PhiConstExprBB)
6973 PhiConstExprBB->eraseFromParent();
6974
6975 for (const auto &Pair : ConstExprEdgeBBs) {
6976 BasicBlock *From = Pair.first.first;
6977 BasicBlock *To = Pair.first.second;
6978 BasicBlock *EdgeBB = Pair.second;
6979 BranchInst::Create(IfTrue: To, InsertBefore: EdgeBB);
6980 From->getTerminator()->replaceSuccessorWith(OldBB: To, NewBB: EdgeBB);
6981 To->replacePhiUsesWith(Old: From, New: EdgeBB);
6982 EdgeBB->moveBefore(MovePos: To);
6983 }
6984
6985 // Trim the value list down to the size it was before we parsed this function.
6986 ValueList.shrinkTo(N: ModuleValueListSize);
6987 MDLoader->shrinkTo(N: ModuleMDLoaderSize);
6988 std::vector<BasicBlock*>().swap(x&: FunctionBBs);
6989 return Error::success();
6990}
6991
6992/// Find the function body in the bitcode stream
6993Error BitcodeReader::findFunctionInStream(
6994 Function *F,
6995 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
6996 while (DeferredFunctionInfoIterator->second == 0) {
6997 // This is the fallback handling for the old format bitcode that
6998 // didn't contain the function index in the VST, or when we have
6999 // an anonymous function which would not have a VST entry.
7000 // Assert that we have one of those two cases.
7001 assert(VSTOffset == 0 || !F->hasName());
7002 // Parse the next body in the stream and set its position in the
7003 // DeferredFunctionInfo map.
7004 if (Error Err = rememberAndSkipFunctionBodies())
7005 return Err;
7006 }
7007 return Error::success();
7008}
7009
7010SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) {
7011 if (Val == SyncScope::SingleThread || Val == SyncScope::System)
7012 return SyncScope::ID(Val);
7013 if (Val >= SSIDs.size())
7014 return SyncScope::System; // Map unknown synchronization scopes to system.
7015 return SSIDs[Val];
7016}
7017
7018//===----------------------------------------------------------------------===//
7019// GVMaterializer implementation
7020//===----------------------------------------------------------------------===//
7021
7022Error BitcodeReader::materialize(GlobalValue *GV) {
7023 Function *F = dyn_cast<Function>(Val: GV);
7024 // If it's not a function or is already material, ignore the request.
7025 if (!F || !F->isMaterializable())
7026 return Error::success();
7027
7028 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(Val: F);
7029 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
7030 // If its position is recorded as 0, its body is somewhere in the stream
7031 // but we haven't seen it yet.
7032 if (DFII->second == 0)
7033 if (Error Err = findFunctionInStream(F, DeferredFunctionInfoIterator: DFII))
7034 return Err;
7035
7036 // Materialize metadata before parsing any function bodies.
7037 if (Error Err = materializeMetadata())
7038 return Err;
7039
7040 // Move the bit stream to the saved position of the deferred function body.
7041 if (Error JumpFailed = Stream.JumpToBit(BitNo: DFII->second))
7042 return JumpFailed;
7043
7044 if (Error Err = parseFunctionBody(F))
7045 return Err;
7046 F->setIsMaterializable(false);
7047
7048 // All parsed Functions should load into the debug info format dictated by the
7049 // Module.
7050 if (SeenDebugIntrinsic && SeenDebugRecord)
7051 return error(Message: "Mixed debug intrinsics and debug records in bitcode module!");
7052
7053 if (StripDebugInfo)
7054 stripDebugInfo(F&: *F);
7055
7056 // Finish fn->subprogram upgrade for materialized functions.
7057 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
7058 F->setSubprogram(SP);
7059
7060 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
7061 if (!MDLoader->isStrippingTBAA()) {
7062 for (auto &I : instructions(F)) {
7063 MDNode *TBAA = I.getMetadata(KindID: LLVMContext::MD_tbaa);
7064 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I: &I, MD: TBAA))
7065 continue;
7066 MDLoader->setStripTBAA(true);
7067 stripTBAA(M: F->getParent());
7068 }
7069 }
7070
7071 for (auto &I : make_early_inc_range(Range: instructions(F))) {
7072 // "Upgrade" older incorrect branch weights by dropping them.
7073 if (auto *MD = I.getMetadata(KindID: LLVMContext::MD_prof)) {
7074 if (MD->getOperand(I: 0) != nullptr && isa<MDString>(Val: MD->getOperand(I: 0))) {
7075 MDString *MDS = cast<MDString>(Val: MD->getOperand(I: 0));
7076 StringRef ProfName = MDS->getString();
7077 // Check consistency of !prof branch_weights metadata.
7078 if (ProfName != MDProfLabels::BranchWeights)
7079 continue;
7080 unsigned ExpectedNumOperands = 0;
7081 if (BranchInst *BI = dyn_cast<BranchInst>(Val: &I))
7082 ExpectedNumOperands = BI->getNumSuccessors();
7083 else if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: &I))
7084 ExpectedNumOperands = SI->getNumSuccessors();
7085 else if (isa<CallInst>(Val: &I))
7086 ExpectedNumOperands = 1;
7087 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(Val: &I))
7088 ExpectedNumOperands = IBI->getNumDestinations();
7089 else if (isa<SelectInst>(Val: &I))
7090 ExpectedNumOperands = 2;
7091 else
7092 continue; // ignore and continue.
7093
7094 unsigned Offset = getBranchWeightOffset(ProfileData: MD);
7095
7096 // If branch weight doesn't match, just strip branch weight.
7097 if (MD->getNumOperands() != Offset + ExpectedNumOperands)
7098 I.setMetadata(KindID: LLVMContext::MD_prof, Node: nullptr);
7099 }
7100 }
7101
7102 if (auto *CI = dyn_cast<CallBase>(Val: &I)) {
7103 // Remove incompatible attributes on function calls.
7104 CI->removeRetAttrs(AttrsToRemove: AttributeFuncs::typeIncompatible(
7105 Ty: CI->getFunctionType()->getReturnType(), AS: CI->getRetAttributes()));
7106
7107 for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo)
7108 CI->removeParamAttrs(ArgNo, AttrsToRemove: AttributeFuncs::typeIncompatible(
7109 Ty: CI->getArgOperand(i: ArgNo)->getType(),
7110 AS: CI->getParamAttributes(ArgNo)));
7111
7112 // Upgrade intrinsics.
7113 if (Function *OldFn = CI->getCalledFunction()) {
7114 auto It = UpgradedIntrinsics.find(Val: OldFn);
7115 if (It != UpgradedIntrinsics.end())
7116 UpgradeIntrinsicCall(CB: CI, NewFn: It->second);
7117 }
7118 }
7119 }
7120
7121 // Look for functions that rely on old function attribute behavior.
7122 UpgradeFunctionAttributes(F&: *F);
7123
7124 // Bring in any functions that this function forward-referenced via
7125 // blockaddresses.
7126 return materializeForwardReferencedFunctions();
7127}
7128
7129Error BitcodeReader::materializeModule() {
7130 if (Error Err = materializeMetadata())
7131 return Err;
7132
7133 // Promise to materialize all forward references.
7134 WillMaterializeAllForwardRefs = true;
7135
7136 // Iterate over the module, deserializing any functions that are still on
7137 // disk.
7138 for (Function &F : *TheModule) {
7139 if (Error Err = materialize(GV: &F))
7140 return Err;
7141 }
7142 // At this point, if there are any function bodies, parse the rest of
7143 // the bits in the module past the last function block we have recorded
7144 // through either lazy scanning or the VST.
7145 if (LastFunctionBlockBit || NextUnreadBit)
7146 if (Error Err = parseModule(ResumeBit: LastFunctionBlockBit > NextUnreadBit
7147 ? LastFunctionBlockBit
7148 : NextUnreadBit))
7149 return Err;
7150
7151 // Check that all block address forward references got resolved (as we
7152 // promised above).
7153 if (!BasicBlockFwdRefs.empty())
7154 return error(Message: "Never resolved function from blockaddress");
7155
7156 // Upgrade any intrinsic calls that slipped through (should not happen!) and
7157 // delete the old functions to clean up. We can't do this unless the entire
7158 // module is materialized because there could always be another function body
7159 // with calls to the old function.
7160 for (auto &I : UpgradedIntrinsics) {
7161 for (auto *U : I.first->users()) {
7162 if (CallInst *CI = dyn_cast<CallInst>(Val: U))
7163 UpgradeIntrinsicCall(CB: CI, NewFn: I.second);
7164 }
7165 if (I.first != I.second) {
7166 if (!I.first->use_empty())
7167 I.first->replaceAllUsesWith(V: I.second);
7168 I.first->eraseFromParent();
7169 }
7170 }
7171 UpgradedIntrinsics.clear();
7172
7173 UpgradeDebugInfo(M&: *TheModule);
7174
7175 UpgradeModuleFlags(M&: *TheModule);
7176
7177 UpgradeNVVMAnnotations(M&: *TheModule);
7178
7179 UpgradeARCRuntime(M&: *TheModule);
7180
7181 copyModuleAttrToFunctions(M&: *TheModule);
7182
7183 return Error::success();
7184}
7185
7186std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
7187 return IdentifiedStructTypes;
7188}
7189
7190ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
7191 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,
7192 StringRef ModulePath, std::function<bool(GlobalValue::GUID)> IsPrevailing)
7193 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),
7194 ModulePath(ModulePath), IsPrevailing(IsPrevailing) {}
7195
7196void ModuleSummaryIndexBitcodeReader::addThisModule() {
7197 TheIndex.addModule(ModPath: ModulePath);
7198}
7199
7200ModuleSummaryIndex::ModuleInfo *
7201ModuleSummaryIndexBitcodeReader::getThisModule() {
7202 return TheIndex.getModule(ModPath: ModulePath);
7203}
7204
7205template <bool AllowNullValueInfo>
7206std::pair<ValueInfo, GlobalValue::GUID>
7207ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) {
7208 auto VGI = ValueIdToValueInfoMap[ValueId];
7209 // We can have a null value info in distributed ThinLTO index files:
7210 // - For memprof callsite info records when the callee function summary is not
7211 // included in the index.
7212 // - For alias summary when its aliasee summary is not included in the index.
7213 // The bitcode writer records 0 in these cases,
7214 // and the caller of this helper will set AllowNullValueInfo to true.
7215 assert(AllowNullValueInfo || std::get<0>(VGI));
7216 return VGI;
7217}
7218
7219void ModuleSummaryIndexBitcodeReader::setValueGUID(
7220 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage,
7221 StringRef SourceFileName) {
7222 std::string GlobalId =
7223 GlobalValue::getGlobalIdentifier(Name: ValueName, Linkage, FileName: SourceFileName);
7224 auto ValueGUID = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: GlobalId);
7225 auto OriginalNameID = ValueGUID;
7226 if (GlobalValue::isLocalLinkage(Linkage))
7227 OriginalNameID = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: ValueName);
7228 if (PrintSummaryGUIDs)
7229 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
7230 << ValueName << "\n";
7231
7232 // UseStrtab is false for legacy summary formats and value names are
7233 // created on stack. In that case we save the name in a string saver in
7234 // the index so that the value name can be recorded.
7235 ValueIdToValueInfoMap[ValueID] = std::make_pair(
7236 x: TheIndex.getOrInsertValueInfo(
7237 GUID: ValueGUID, Name: UseStrtab ? ValueName : TheIndex.saveString(String: ValueName)),
7238 y&: OriginalNameID);
7239}
7240
7241// Specialized value symbol table parser used when reading module index
7242// blocks where we don't actually create global values. The parsed information
7243// is saved in the bitcode reader for use when later parsing summaries.
7244Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
7245 uint64_t Offset,
7246 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {
7247 // With a strtab the VST is not required to parse the summary.
7248 if (UseStrtab)
7249 return Error::success();
7250
7251 assert(Offset > 0 && "Expected non-zero VST offset");
7252 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
7253 if (!MaybeCurrentBit)
7254 return MaybeCurrentBit.takeError();
7255 uint64_t CurrentBit = MaybeCurrentBit.get();
7256
7257 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::VALUE_SYMTAB_BLOCK_ID))
7258 return Err;
7259
7260 SmallVector<uint64_t, 64> Record;
7261
7262 // Read all the records for this value table.
7263 SmallString<128> ValueName;
7264
7265 while (true) {
7266 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
7267 if (!MaybeEntry)
7268 return MaybeEntry.takeError();
7269 BitstreamEntry Entry = MaybeEntry.get();
7270
7271 switch (Entry.Kind) {
7272 case BitstreamEntry::SubBlock: // Handled for us already.
7273 case BitstreamEntry::Error:
7274 return error(Message: "Malformed block");
7275 case BitstreamEntry::EndBlock:
7276 // Done parsing VST, jump back to wherever we came from.
7277 if (Error JumpFailed = Stream.JumpToBit(BitNo: CurrentBit))
7278 return JumpFailed;
7279 return Error::success();
7280 case BitstreamEntry::Record:
7281 // The interesting case.
7282 break;
7283 }
7284
7285 // Read a record.
7286 Record.clear();
7287 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
7288 if (!MaybeRecord)
7289 return MaybeRecord.takeError();
7290 switch (MaybeRecord.get()) {
7291 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
7292 break;
7293 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
7294 if (convertToString(Record, Idx: 1, Result&: ValueName))
7295 return error(Message: "Invalid vst_code_entry record");
7296 unsigned ValueID = Record[0];
7297 assert(!SourceFileName.empty());
7298 auto VLI = ValueIdToLinkageMap.find(Val: ValueID);
7299 assert(VLI != ValueIdToLinkageMap.end() &&
7300 "No linkage found for VST entry?");
7301 auto Linkage = VLI->second;
7302 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
7303 ValueName.clear();
7304 break;
7305 }
7306 case bitc::VST_CODE_FNENTRY: {
7307 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
7308 if (convertToString(Record, Idx: 2, Result&: ValueName))
7309 return error(Message: "Invalid vst_code_fnentry record");
7310 unsigned ValueID = Record[0];
7311 assert(!SourceFileName.empty());
7312 auto VLI = ValueIdToLinkageMap.find(Val: ValueID);
7313 assert(VLI != ValueIdToLinkageMap.end() &&
7314 "No linkage found for VST entry?");
7315 auto Linkage = VLI->second;
7316 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
7317 ValueName.clear();
7318 break;
7319 }
7320 case bitc::VST_CODE_COMBINED_ENTRY: {
7321 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
7322 unsigned ValueID = Record[0];
7323 GlobalValue::GUID RefGUID = Record[1];
7324 // The "original name", which is the second value of the pair will be
7325 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
7326 ValueIdToValueInfoMap[ValueID] =
7327 std::make_pair(x: TheIndex.getOrInsertValueInfo(GUID: RefGUID), y&: RefGUID);
7328 break;
7329 }
7330 }
7331 }
7332}
7333
7334// Parse just the blocks needed for building the index out of the module.
7335// At the end of this routine the module Index is populated with a map
7336// from global value id to GlobalValueSummary objects.
7337Error ModuleSummaryIndexBitcodeReader::parseModule() {
7338 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID))
7339 return Err;
7340
7341 SmallVector<uint64_t, 64> Record;
7342 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;
7343 unsigned ValueId = 0;
7344
7345 // Read the index for this module.
7346 while (true) {
7347 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
7348 if (!MaybeEntry)
7349 return MaybeEntry.takeError();
7350 llvm::BitstreamEntry Entry = MaybeEntry.get();
7351
7352 switch (Entry.Kind) {
7353 case BitstreamEntry::Error:
7354 return error(Message: "Malformed block");
7355 case BitstreamEntry::EndBlock:
7356 return Error::success();
7357
7358 case BitstreamEntry::SubBlock:
7359 switch (Entry.ID) {
7360 default: // Skip unknown content.
7361 if (Error Err = Stream.SkipBlock())
7362 return Err;
7363 break;
7364 case bitc::BLOCKINFO_BLOCK_ID:
7365 // Need to parse these to get abbrev ids (e.g. for VST)
7366 if (Error Err = readBlockInfo())
7367 return Err;
7368 break;
7369 case bitc::VALUE_SYMTAB_BLOCK_ID:
7370 // Should have been parsed earlier via VSTOffset, unless there
7371 // is no summary section.
7372 assert(((SeenValueSymbolTable && VSTOffset > 0) ||
7373 !SeenGlobalValSummary) &&
7374 "Expected early VST parse via VSTOffset record");
7375 if (Error Err = Stream.SkipBlock())
7376 return Err;
7377 break;
7378 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
7379 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
7380 // Add the module if it is a per-module index (has a source file name).
7381 if (!SourceFileName.empty())
7382 addThisModule();
7383 assert(!SeenValueSymbolTable &&
7384 "Already read VST when parsing summary block?");
7385 // We might not have a VST if there were no values in the
7386 // summary. An empty summary block generated when we are
7387 // performing ThinLTO compiles so we don't later invoke
7388 // the regular LTO process on them.
7389 if (VSTOffset > 0) {
7390 if (Error Err = parseValueSymbolTable(Offset: VSTOffset, ValueIdToLinkageMap))
7391 return Err;
7392 SeenValueSymbolTable = true;
7393 }
7394 SeenGlobalValSummary = true;
7395 if (Error Err = parseEntireSummary(ID: Entry.ID))
7396 return Err;
7397 break;
7398 case bitc::MODULE_STRTAB_BLOCK_ID:
7399 if (Error Err = parseModuleStringTable())
7400 return Err;
7401 break;
7402 }
7403 continue;
7404
7405 case BitstreamEntry::Record: {
7406 Record.clear();
7407 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
7408 if (!MaybeBitCode)
7409 return MaybeBitCode.takeError();
7410 switch (MaybeBitCode.get()) {
7411 default:
7412 break; // Default behavior, ignore unknown content.
7413 case bitc::MODULE_CODE_VERSION: {
7414 if (Error Err = parseVersionRecord(Record).takeError())
7415 return Err;
7416 break;
7417 }
7418 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
7419 case bitc::MODULE_CODE_SOURCE_FILENAME: {
7420 SmallString<128> ValueName;
7421 if (convertToString(Record, Idx: 0, Result&: ValueName))
7422 return error(Message: "Invalid source filename record");
7423 SourceFileName = ValueName.c_str();
7424 break;
7425 }
7426 /// MODULE_CODE_HASH: [5*i32]
7427 case bitc::MODULE_CODE_HASH: {
7428 if (Record.size() != 5)
7429 return error(Message: "Invalid hash length " + Twine(Record.size()).str());
7430 auto &Hash = getThisModule()->second;
7431 int Pos = 0;
7432 for (auto &Val : Record) {
7433 assert(!(Val >> 32) && "Unexpected high bits set");
7434 Hash[Pos++] = Val;
7435 }
7436 break;
7437 }
7438 /// MODULE_CODE_VSTOFFSET: [offset]
7439 case bitc::MODULE_CODE_VSTOFFSET:
7440 if (Record.empty())
7441 return error(Message: "Invalid vstoffset record");
7442 // Note that we subtract 1 here because the offset is relative to one
7443 // word before the start of the identification or module block, which
7444 // was historically always the start of the regular bitcode header.
7445 VSTOffset = Record[0] - 1;
7446 break;
7447 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
7448 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
7449 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
7450 // v2: [strtab offset, strtab size, v1]
7451 case bitc::MODULE_CODE_GLOBALVAR:
7452 case bitc::MODULE_CODE_FUNCTION:
7453 case bitc::MODULE_CODE_ALIAS: {
7454 StringRef Name;
7455 ArrayRef<uint64_t> GVRecord;
7456 std::tie(args&: Name, args&: GVRecord) = readNameFromStrtab(Record);
7457 if (GVRecord.size() <= 3)
7458 return error(Message: "Invalid global record");
7459 uint64_t RawLinkage = GVRecord[3];
7460 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(Val: RawLinkage);
7461 if (!UseStrtab) {
7462 ValueIdToLinkageMap[ValueId++] = Linkage;
7463 break;
7464 }
7465
7466 setValueGUID(ValueID: ValueId++, ValueName: Name, Linkage, SourceFileName);
7467 break;
7468 }
7469 }
7470 }
7471 continue;
7472 }
7473 }
7474}
7475
7476SmallVector<ValueInfo, 0>
7477ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {
7478 SmallVector<ValueInfo, 0> Ret;
7479 Ret.reserve(N: Record.size());
7480 for (uint64_t RefValueId : Record)
7481 Ret.push_back(Elt: std::get<0>(in: getValueInfoFromValueId(ValueId: RefValueId)));
7482 return Ret;
7483}
7484
7485SmallVector<FunctionSummary::EdgeTy, 0>
7486ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
7487 bool IsOldProfileFormat,
7488 bool HasProfile, bool HasRelBF) {
7489 SmallVector<FunctionSummary::EdgeTy, 0> Ret;
7490 // In the case of new profile formats, there are two Record entries per
7491 // Edge. Otherwise, conservatively reserve up to Record.size.
7492 if (!IsOldProfileFormat && (HasProfile || HasRelBF))
7493 Ret.reserve(N: Record.size() / 2);
7494 else
7495 Ret.reserve(N: Record.size());
7496
7497 for (unsigned I = 0, E = Record.size(); I != E; ++I) {
7498 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
7499 bool HasTailCall = false;
7500 uint64_t RelBF = 0;
7501 ValueInfo Callee = std::get<0>(in: getValueInfoFromValueId(ValueId: Record[I]));
7502 if (IsOldProfileFormat) {
7503 I += 1; // Skip old callsitecount field
7504 if (HasProfile)
7505 I += 1; // Skip old profilecount field
7506 } else if (HasProfile)
7507 std::tie(args&: Hotness, args&: HasTailCall) =
7508 getDecodedHotnessCallEdgeInfo(RawFlags: Record[++I]);
7509 // Deprecated, but still needed to read old bitcode files.
7510 else if (HasRelBF)
7511 getDecodedRelBFCallEdgeInfo(RawFlags: Record[++I], RelBF, HasTailCall);
7512 Ret.push_back(
7513 Elt: FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, HasTailCall)});
7514 }
7515 return Ret;
7516}
7517
7518static void
7519parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot,
7520 WholeProgramDevirtResolution &Wpd) {
7521 uint64_t ArgNum = Record[Slot++];
7522 WholeProgramDevirtResolution::ByArg &B =
7523 Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}];
7524 Slot += ArgNum;
7525
7526 B.TheKind =
7527 static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]);
7528 B.Info = Record[Slot++];
7529 B.Byte = Record[Slot++];
7530 B.Bit = Record[Slot++];
7531}
7532
7533static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record,
7534 StringRef Strtab, size_t &Slot,
7535 TypeIdSummary &TypeId) {
7536 uint64_t Id = Record[Slot++];
7537 WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];
7538
7539 Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]);
7540 Wpd.SingleImplName = {Strtab.data() + Record[Slot],
7541 static_cast<size_t>(Record[Slot + 1])};
7542 Slot += 2;
7543
7544 uint64_t ResByArgNum = Record[Slot++];
7545 for (uint64_t I = 0; I != ResByArgNum; ++I)
7546 parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd);
7547}
7548
7549static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record,
7550 StringRef Strtab,
7551 ModuleSummaryIndex &TheIndex) {
7552 size_t Slot = 0;
7553 TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(
7554 TypeId: {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])});
7555 Slot += 2;
7556
7557 TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]);
7558 TypeId.TTRes.SizeM1BitWidth = Record[Slot++];
7559 TypeId.TTRes.AlignLog2 = Record[Slot++];
7560 TypeId.TTRes.SizeM1 = Record[Slot++];
7561 TypeId.TTRes.BitMask = Record[Slot++];
7562 TypeId.TTRes.InlineBits = Record[Slot++];
7563
7564 while (Slot < Record.size())
7565 parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId);
7566}
7567
7568std::vector<FunctionSummary::ParamAccess>
7569ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) {
7570 auto ReadRange = [&]() {
7571 APInt Lower(FunctionSummary::ParamAccess::RangeWidth,
7572 BitcodeReader::decodeSignRotatedValue(V: Record.consume_front()));
7573 APInt Upper(FunctionSummary::ParamAccess::RangeWidth,
7574 BitcodeReader::decodeSignRotatedValue(V: Record.consume_front()));
7575 ConstantRange Range{Lower, Upper};
7576 assert(!Range.isFullSet());
7577 assert(!Range.isUpperSignWrapped());
7578 return Range;
7579 };
7580
7581 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses;
7582 while (!Record.empty()) {
7583 PendingParamAccesses.emplace_back();
7584 FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back();
7585 ParamAccess.ParamNo = Record.consume_front();
7586 ParamAccess.Use = ReadRange();
7587 ParamAccess.Calls.resize(new_size: Record.consume_front());
7588 for (auto &Call : ParamAccess.Calls) {
7589 Call.ParamNo = Record.consume_front();
7590 Call.Callee =
7591 std::get<0>(in: getValueInfoFromValueId(ValueId: Record.consume_front()));
7592 Call.Offsets = ReadRange();
7593 }
7594 }
7595 return PendingParamAccesses;
7596}
7597
7598void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo(
7599 ArrayRef<uint64_t> Record, size_t &Slot,
7600 TypeIdCompatibleVtableInfo &TypeId) {
7601 uint64_t Offset = Record[Slot++];
7602 ValueInfo Callee = std::get<0>(in: getValueInfoFromValueId(ValueId: Record[Slot++]));
7603 TypeId.push_back(x: {Offset, Callee});
7604}
7605
7606void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(
7607 ArrayRef<uint64_t> Record) {
7608 size_t Slot = 0;
7609 TypeIdCompatibleVtableInfo &TypeId =
7610 TheIndex.getOrInsertTypeIdCompatibleVtableSummary(
7611 TypeId: {Strtab.data() + Record[Slot],
7612 static_cast<size_t>(Record[Slot + 1])});
7613 Slot += 2;
7614
7615 while (Slot < Record.size())
7616 parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId);
7617}
7618
7619SmallVector<unsigned> ModuleSummaryIndexBitcodeReader::parseAllocInfoContext(
7620 ArrayRef<uint64_t> Record, unsigned &I) {
7621 SmallVector<unsigned> StackIdList;
7622 // For backwards compatibility with old format before radix tree was
7623 // used, simply see if we found a radix tree array record (and thus if
7624 // the RadixArray is non-empty).
7625 if (RadixArray.empty()) {
7626 unsigned NumStackEntries = Record[I++];
7627 assert(Record.size() - I >= NumStackEntries);
7628 StackIdList.reserve(N: NumStackEntries);
7629 for (unsigned J = 0; J < NumStackEntries; J++) {
7630 assert(Record[I] < StackIds.size());
7631 StackIdList.push_back(
7632 Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[Record[I++]]));
7633 }
7634 } else {
7635 unsigned RadixIndex = Record[I++];
7636 // See the comments above CallStackRadixTreeBuilder in ProfileData/MemProf.h
7637 // for a detailed description of the radix tree array format. Briefly, the
7638 // first entry will be the number of frames, any negative values are the
7639 // negative of the offset of the next frame, and otherwise the frames are in
7640 // increasing linear order.
7641 assert(RadixIndex < RadixArray.size());
7642 unsigned NumStackIds = RadixArray[RadixIndex++];
7643 StackIdList.reserve(N: NumStackIds);
7644 while (NumStackIds--) {
7645 assert(RadixIndex < RadixArray.size());
7646 unsigned Elem = RadixArray[RadixIndex];
7647 if (static_cast<std::make_signed_t<unsigned>>(Elem) < 0) {
7648 RadixIndex = RadixIndex - Elem;
7649 assert(RadixIndex < RadixArray.size());
7650 Elem = RadixArray[RadixIndex];
7651 // We shouldn't encounter a second offset in a row.
7652 assert(static_cast<std::make_signed_t<unsigned>>(Elem) >= 0);
7653 }
7654 RadixIndex++;
7655 StackIdList.push_back(Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[Elem]));
7656 }
7657 }
7658 return StackIdList;
7659}
7660
7661static void setSpecialRefs(SmallVectorImpl<ValueInfo> &Refs, unsigned ROCnt,
7662 unsigned WOCnt) {
7663 // Readonly and writeonly refs are in the end of the refs list.
7664 assert(ROCnt + WOCnt <= Refs.size());
7665 unsigned FirstWORef = Refs.size() - WOCnt;
7666 unsigned RefNo = FirstWORef - ROCnt;
7667 for (; RefNo < FirstWORef; ++RefNo)
7668 Refs[RefNo].setReadOnly();
7669 for (; RefNo < Refs.size(); ++RefNo)
7670 Refs[RefNo].setWriteOnly();
7671}
7672
7673// Eagerly parse the entire summary block. This populates the GlobalValueSummary
7674// objects in the index.
7675Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
7676 if (Error Err = Stream.EnterSubBlock(BlockID: ID))
7677 return Err;
7678 SmallVector<uint64_t, 64> Record;
7679
7680 // Parse version
7681 {
7682 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
7683 if (!MaybeEntry)
7684 return MaybeEntry.takeError();
7685 BitstreamEntry Entry = MaybeEntry.get();
7686
7687 if (Entry.Kind != BitstreamEntry::Record)
7688 return error(Message: "Invalid Summary Block: record for version expected");
7689 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
7690 if (!MaybeRecord)
7691 return MaybeRecord.takeError();
7692 if (MaybeRecord.get() != bitc::FS_VERSION)
7693 return error(Message: "Invalid Summary Block: version expected");
7694 }
7695 const uint64_t Version = Record[0];
7696 const bool IsOldProfileFormat = Version == 1;
7697 if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion)
7698 return error(Message: "Invalid summary version " + Twine(Version) +
7699 ". Version should be in the range [1-" +
7700 Twine(ModuleSummaryIndex::BitcodeSummaryVersion) +
7701 "].");
7702 Record.clear();
7703
7704 // Keep around the last seen summary to be used when we see an optional
7705 // "OriginalName" attachement.
7706 GlobalValueSummary *LastSeenSummary = nullptr;
7707 GlobalValue::GUID LastSeenGUID = 0;
7708
7709 // We can expect to see any number of type ID information records before
7710 // each function summary records; these variables store the information
7711 // collected so far so that it can be used to create the summary object.
7712 std::vector<GlobalValue::GUID> PendingTypeTests;
7713 std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls,
7714 PendingTypeCheckedLoadVCalls;
7715 std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls,
7716 PendingTypeCheckedLoadConstVCalls;
7717 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses;
7718
7719 std::vector<CallsiteInfo> PendingCallsites;
7720 std::vector<AllocInfo> PendingAllocs;
7721 std::vector<uint64_t> PendingContextIds;
7722
7723 while (true) {
7724 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
7725 if (!MaybeEntry)
7726 return MaybeEntry.takeError();
7727 BitstreamEntry Entry = MaybeEntry.get();
7728
7729 switch (Entry.Kind) {
7730 case BitstreamEntry::SubBlock: // Handled for us already.
7731 case BitstreamEntry::Error:
7732 return error(Message: "Malformed block");
7733 case BitstreamEntry::EndBlock:
7734 return Error::success();
7735 case BitstreamEntry::Record:
7736 // The interesting case.
7737 break;
7738 }
7739
7740 // Read a record. The record format depends on whether this
7741 // is a per-module index or a combined index file. In the per-module
7742 // case the records contain the associated value's ID for correlation
7743 // with VST entries. In the combined index the correlation is done
7744 // via the bitcode offset of the summary records (which were saved
7745 // in the combined index VST entries). The records also contain
7746 // information used for ThinLTO renaming and importing.
7747 Record.clear();
7748 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
7749 if (!MaybeBitCode)
7750 return MaybeBitCode.takeError();
7751 switch (unsigned BitCode = MaybeBitCode.get()) {
7752 default: // Default behavior: ignore.
7753 break;
7754 case bitc::FS_FLAGS: { // [flags]
7755 TheIndex.setFlags(Record[0]);
7756 break;
7757 }
7758 case bitc::FS_VALUE_GUID: { // [valueid, refguid_upper32, refguid_lower32]
7759 uint64_t ValueID = Record[0];
7760 GlobalValue::GUID RefGUID;
7761 if (Version >= 11) {
7762 RefGUID = Record[1] << 32 | Record[2];
7763 } else {
7764 RefGUID = Record[1];
7765 }
7766 ValueIdToValueInfoMap[ValueID] =
7767 std::make_pair(x: TheIndex.getOrInsertValueInfo(GUID: RefGUID), y&: RefGUID);
7768 break;
7769 }
7770 // FS_PERMODULE is legacy and does not have support for the tail call flag.
7771 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,
7772 // numrefs x valueid, n x (valueid)]
7773 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,
7774 // numrefs x valueid,
7775 // n x (valueid, hotness+tailcall flags)]
7776 // Deprecated, but still needed to read old bitcode files.
7777 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,
7778 // numrefs x valueid,
7779 // n x (valueid, relblockfreq+tailcall)]
7780 case bitc::FS_PERMODULE:
7781 case bitc::FS_PERMODULE_PROFILE:
7782 // Deprecated, but still needed to read old bitcode files.
7783 case bitc::FS_PERMODULE_RELBF: {
7784 unsigned ValueID = Record[0];
7785 uint64_t RawFlags = Record[1];
7786 unsigned InstCount = Record[2];
7787 uint64_t RawFunFlags = 0;
7788 unsigned NumRefs = Record[3];
7789 unsigned NumRORefs = 0, NumWORefs = 0;
7790 int RefListStartIndex = 4;
7791 if (Version >= 4) {
7792 RawFunFlags = Record[3];
7793 NumRefs = Record[4];
7794 RefListStartIndex = 5;
7795 if (Version >= 5) {
7796 NumRORefs = Record[5];
7797 RefListStartIndex = 6;
7798 if (Version >= 7) {
7799 NumWORefs = Record[6];
7800 RefListStartIndex = 7;
7801 }
7802 }
7803 }
7804
7805 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
7806 // The module path string ref set in the summary must be owned by the
7807 // index's module string table. Since we don't have a module path
7808 // string table section in the per-module index, we create a single
7809 // module path string table entry with an empty (0) ID to take
7810 // ownership.
7811 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
7812 assert(Record.size() >= RefListStartIndex + NumRefs &&
7813 "Record size inconsistent with number of references");
7814 SmallVector<ValueInfo, 0> Refs = makeRefList(
7815 Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs));
7816 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
7817 // Deprecated, but still needed to read old bitcode files.
7818 bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF);
7819 SmallVector<FunctionSummary::EdgeTy, 0> Calls = makeCallList(
7820 Record: ArrayRef<uint64_t>(Record).slice(N: CallGraphEdgeStartIndex),
7821 IsOldProfileFormat, HasProfile, HasRelBF);
7822 setSpecialRefs(Refs, ROCnt: NumRORefs, WOCnt: NumWORefs);
7823 auto VIAndOriginalGUID = getValueInfoFromValueId(ValueId: ValueID);
7824 // In order to save memory, only record the memprof summaries if this is
7825 // the prevailing copy of a symbol. The linker doesn't resolve local
7826 // linkage values so don't check whether those are prevailing.
7827 auto LT = (GlobalValue::LinkageTypes)Flags.Linkage;
7828 if (IsPrevailing && !GlobalValue::isLocalLinkage(Linkage: LT) &&
7829 !IsPrevailing(VIAndOriginalGUID.first.getGUID())) {
7830 PendingCallsites.clear();
7831 PendingAllocs.clear();
7832 }
7833 auto FS = std::make_unique<FunctionSummary>(
7834 args&: Flags, args&: InstCount, args: getDecodedFFlags(RawFlags: RawFunFlags), args: std::move(Refs),
7835 args: std::move(Calls), args: std::move(PendingTypeTests),
7836 args: std::move(PendingTypeTestAssumeVCalls),
7837 args: std::move(PendingTypeCheckedLoadVCalls),
7838 args: std::move(PendingTypeTestAssumeConstVCalls),
7839 args: std::move(PendingTypeCheckedLoadConstVCalls),
7840 args: std::move(PendingParamAccesses), args: std::move(PendingCallsites),
7841 args: std::move(PendingAllocs));
7842 FS->setModulePath(getThisModule()->first());
7843 FS->setOriginalName(std::get<1>(in&: VIAndOriginalGUID));
7844 TheIndex.addGlobalValueSummary(VI: std::get<0>(in&: VIAndOriginalGUID),
7845 Summary: std::move(FS));
7846 break;
7847 }
7848 // FS_ALIAS: [valueid, flags, valueid]
7849 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
7850 // they expect all aliasee summaries to be available.
7851 case bitc::FS_ALIAS: {
7852 unsigned ValueID = Record[0];
7853 uint64_t RawFlags = Record[1];
7854 unsigned AliaseeID = Record[2];
7855 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
7856 auto AS = std::make_unique<AliasSummary>(args&: Flags);
7857 // The module path string ref set in the summary must be owned by the
7858 // index's module string table. Since we don't have a module path
7859 // string table section in the per-module index, we create a single
7860 // module path string table entry with an empty (0) ID to take
7861 // ownership.
7862 AS->setModulePath(getThisModule()->first());
7863
7864 auto AliaseeVI = std::get<0>(in: getValueInfoFromValueId(ValueId: AliaseeID));
7865 auto AliaseeInModule = TheIndex.findSummaryInModule(VI: AliaseeVI, ModuleId: ModulePath);
7866 if (!AliaseeInModule)
7867 return error(Message: "Alias expects aliasee summary to be parsed");
7868 AS->setAliasee(AliaseeVI, Aliasee: AliaseeInModule);
7869
7870 auto GUID = getValueInfoFromValueId(ValueId: ValueID);
7871 AS->setOriginalName(std::get<1>(in&: GUID));
7872 TheIndex.addGlobalValueSummary(VI: std::get<0>(in&: GUID), Summary: std::move(AS));
7873 break;
7874 }
7875 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid]
7876 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: {
7877 unsigned ValueID = Record[0];
7878 uint64_t RawFlags = Record[1];
7879 unsigned RefArrayStart = 2;
7880 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false,
7881 /* WriteOnly */ false,
7882 /* Constant */ false,
7883 GlobalObject::VCallVisibilityPublic);
7884 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
7885 if (Version >= 5) {
7886 GVF = getDecodedGVarFlags(RawFlags: Record[2]);
7887 RefArrayStart = 3;
7888 }
7889 SmallVector<ValueInfo, 0> Refs =
7890 makeRefList(Record: ArrayRef<uint64_t>(Record).slice(N: RefArrayStart));
7891 auto FS =
7892 std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs));
7893 FS->setModulePath(getThisModule()->first());
7894 auto GUID = getValueInfoFromValueId(ValueId: ValueID);
7895 FS->setOriginalName(std::get<1>(in&: GUID));
7896 TheIndex.addGlobalValueSummary(VI: std::get<0>(in&: GUID), Summary: std::move(FS));
7897 break;
7898 }
7899 // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags,
7900 // numrefs, numrefs x valueid,
7901 // n x (valueid, offset)]
7902 case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: {
7903 unsigned ValueID = Record[0];
7904 uint64_t RawFlags = Record[1];
7905 GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(RawFlags: Record[2]);
7906 unsigned NumRefs = Record[3];
7907 unsigned RefListStartIndex = 4;
7908 unsigned VTableListStartIndex = RefListStartIndex + NumRefs;
7909 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
7910 SmallVector<ValueInfo, 0> Refs = makeRefList(
7911 Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs));
7912 VTableFuncList VTableFuncs;
7913 for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) {
7914 ValueInfo Callee = std::get<0>(in: getValueInfoFromValueId(ValueId: Record[I]));
7915 uint64_t Offset = Record[++I];
7916 VTableFuncs.push_back(x: {Callee, Offset});
7917 }
7918 auto VS =
7919 std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs));
7920 VS->setModulePath(getThisModule()->first());
7921 VS->setVTableFuncs(VTableFuncs);
7922 auto GUID = getValueInfoFromValueId(ValueId: ValueID);
7923 VS->setOriginalName(std::get<1>(in&: GUID));
7924 TheIndex.addGlobalValueSummary(VI: std::get<0>(in&: GUID), Summary: std::move(VS));
7925 break;
7926 }
7927 // FS_COMBINED is legacy and does not have support for the tail call flag.
7928 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,
7929 // numrefs x valueid, n x (valueid)]
7930 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,
7931 // numrefs x valueid,
7932 // n x (valueid, hotness+tailcall flags)]
7933 case bitc::FS_COMBINED:
7934 case bitc::FS_COMBINED_PROFILE: {
7935 unsigned ValueID = Record[0];
7936 uint64_t ModuleId = Record[1];
7937 uint64_t RawFlags = Record[2];
7938 unsigned InstCount = Record[3];
7939 uint64_t RawFunFlags = 0;
7940 unsigned NumRefs = Record[4];
7941 unsigned NumRORefs = 0, NumWORefs = 0;
7942 int RefListStartIndex = 5;
7943
7944 if (Version >= 4) {
7945 RawFunFlags = Record[4];
7946 RefListStartIndex = 6;
7947 size_t NumRefsIndex = 5;
7948 if (Version >= 5) {
7949 unsigned NumRORefsOffset = 1;
7950 RefListStartIndex = 7;
7951 if (Version >= 6) {
7952 NumRefsIndex = 6;
7953 RefListStartIndex = 8;
7954 if (Version >= 7) {
7955 RefListStartIndex = 9;
7956 NumWORefs = Record[8];
7957 NumRORefsOffset = 2;
7958 }
7959 }
7960 NumRORefs = Record[RefListStartIndex - NumRORefsOffset];
7961 }
7962 NumRefs = Record[NumRefsIndex];
7963 }
7964
7965 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
7966 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
7967 assert(Record.size() >= RefListStartIndex + NumRefs &&
7968 "Record size inconsistent with number of references");
7969 SmallVector<ValueInfo, 0> Refs = makeRefList(
7970 Record: ArrayRef<uint64_t>(Record).slice(N: RefListStartIndex, M: NumRefs));
7971 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
7972 SmallVector<FunctionSummary::EdgeTy, 0> Edges = makeCallList(
7973 Record: ArrayRef<uint64_t>(Record).slice(N: CallGraphEdgeStartIndex),
7974 IsOldProfileFormat, HasProfile, HasRelBF: false);
7975 ValueInfo VI = std::get<0>(in: getValueInfoFromValueId(ValueId: ValueID));
7976 setSpecialRefs(Refs, ROCnt: NumRORefs, WOCnt: NumWORefs);
7977 auto FS = std::make_unique<FunctionSummary>(
7978 args&: Flags, args&: InstCount, args: getDecodedFFlags(RawFlags: RawFunFlags), args: std::move(Refs),
7979 args: std::move(Edges), args: std::move(PendingTypeTests),
7980 args: std::move(PendingTypeTestAssumeVCalls),
7981 args: std::move(PendingTypeCheckedLoadVCalls),
7982 args: std::move(PendingTypeTestAssumeConstVCalls),
7983 args: std::move(PendingTypeCheckedLoadConstVCalls),
7984 args: std::move(PendingParamAccesses), args: std::move(PendingCallsites),
7985 args: std::move(PendingAllocs));
7986 LastSeenSummary = FS.get();
7987 LastSeenGUID = VI.getGUID();
7988 FS->setModulePath(ModuleIdMap[ModuleId]);
7989 TheIndex.addGlobalValueSummary(VI, Summary: std::move(FS));
7990 break;
7991 }
7992 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]
7993 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
7994 // they expect all aliasee summaries to be available.
7995 case bitc::FS_COMBINED_ALIAS: {
7996 unsigned ValueID = Record[0];
7997 uint64_t ModuleId = Record[1];
7998 uint64_t RawFlags = Record[2];
7999 unsigned AliaseeValueId = Record[3];
8000 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
8001 auto AS = std::make_unique<AliasSummary>(args&: Flags);
8002 LastSeenSummary = AS.get();
8003 AS->setModulePath(ModuleIdMap[ModuleId]);
8004
8005 auto AliaseeVI = std::get<0>(
8006 in: getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueId: AliaseeValueId));
8007 if (AliaseeVI) {
8008 auto AliaseeInModule =
8009 TheIndex.findSummaryInModule(VI: AliaseeVI, ModuleId: AS->modulePath());
8010 AS->setAliasee(AliaseeVI, Aliasee: AliaseeInModule);
8011 }
8012 ValueInfo VI = std::get<0>(in: getValueInfoFromValueId(ValueId: ValueID));
8013 LastSeenGUID = VI.getGUID();
8014 TheIndex.addGlobalValueSummary(VI, Summary: std::move(AS));
8015 break;
8016 }
8017 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]
8018 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: {
8019 unsigned ValueID = Record[0];
8020 uint64_t ModuleId = Record[1];
8021 uint64_t RawFlags = Record[2];
8022 unsigned RefArrayStart = 3;
8023 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false,
8024 /* WriteOnly */ false,
8025 /* Constant */ false,
8026 GlobalObject::VCallVisibilityPublic);
8027 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
8028 if (Version >= 5) {
8029 GVF = getDecodedGVarFlags(RawFlags: Record[3]);
8030 RefArrayStart = 4;
8031 }
8032 SmallVector<ValueInfo, 0> Refs =
8033 makeRefList(Record: ArrayRef<uint64_t>(Record).slice(N: RefArrayStart));
8034 auto FS =
8035 std::make_unique<GlobalVarSummary>(args&: Flags, args&: GVF, args: std::move(Refs));
8036 LastSeenSummary = FS.get();
8037 FS->setModulePath(ModuleIdMap[ModuleId]);
8038 ValueInfo VI = std::get<0>(in: getValueInfoFromValueId(ValueId: ValueID));
8039 LastSeenGUID = VI.getGUID();
8040 TheIndex.addGlobalValueSummary(VI, Summary: std::move(FS));
8041 break;
8042 }
8043 // FS_COMBINED_ORIGINAL_NAME: [original_name]
8044 case bitc::FS_COMBINED_ORIGINAL_NAME: {
8045 uint64_t OriginalName = Record[0];
8046 if (!LastSeenSummary)
8047 return error(Message: "Name attachment that does not follow a combined record");
8048 LastSeenSummary->setOriginalName(OriginalName);
8049 TheIndex.addOriginalName(ValueGUID: LastSeenGUID, OrigGUID: OriginalName);
8050 // Reset the LastSeenSummary
8051 LastSeenSummary = nullptr;
8052 LastSeenGUID = 0;
8053 break;
8054 }
8055 case bitc::FS_TYPE_TESTS:
8056 assert(PendingTypeTests.empty());
8057 llvm::append_range(C&: PendingTypeTests, R&: Record);
8058 break;
8059
8060 case bitc::FS_TYPE_TEST_ASSUME_VCALLS:
8061 assert(PendingTypeTestAssumeVCalls.empty());
8062 for (unsigned I = 0; I != Record.size(); I += 2)
8063 PendingTypeTestAssumeVCalls.push_back(x: {.GUID: Record[I], .Offset: Record[I+1]});
8064 break;
8065
8066 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS:
8067 assert(PendingTypeCheckedLoadVCalls.empty());
8068 for (unsigned I = 0; I != Record.size(); I += 2)
8069 PendingTypeCheckedLoadVCalls.push_back(x: {.GUID: Record[I], .Offset: Record[I+1]});
8070 break;
8071
8072 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL:
8073 PendingTypeTestAssumeConstVCalls.push_back(
8074 x: {.VFunc: {.GUID: Record[0], .Offset: Record[1]}, .Args: {Record.begin() + 2, Record.end()}});
8075 break;
8076
8077 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL:
8078 PendingTypeCheckedLoadConstVCalls.push_back(
8079 x: {.VFunc: {.GUID: Record[0], .Offset: Record[1]}, .Args: {Record.begin() + 2, Record.end()}});
8080 break;
8081
8082 case bitc::FS_CFI_FUNCTION_DEFS: {
8083 auto &CfiFunctionDefs = TheIndex.cfiFunctionDefs();
8084 for (unsigned I = 0; I != Record.size(); I += 2)
8085 CfiFunctionDefs.emplace(A: Strtab.data() + Record[I],
8086 A: static_cast<size_t>(Record[I + 1]));
8087 break;
8088 }
8089
8090 case bitc::FS_CFI_FUNCTION_DECLS: {
8091 auto &CfiFunctionDecls = TheIndex.cfiFunctionDecls();
8092 for (unsigned I = 0; I != Record.size(); I += 2)
8093 CfiFunctionDecls.emplace(A: Strtab.data() + Record[I],
8094 A: static_cast<size_t>(Record[I + 1]));
8095 break;
8096 }
8097
8098 case bitc::FS_TYPE_ID:
8099 parseTypeIdSummaryRecord(Record, Strtab, TheIndex);
8100 break;
8101
8102 case bitc::FS_TYPE_ID_METADATA:
8103 parseTypeIdCompatibleVtableSummaryRecord(Record);
8104 break;
8105
8106 case bitc::FS_BLOCK_COUNT:
8107 TheIndex.addBlockCount(C: Record[0]);
8108 break;
8109
8110 case bitc::FS_PARAM_ACCESS: {
8111 PendingParamAccesses = parseParamAccesses(Record);
8112 break;
8113 }
8114
8115 case bitc::FS_STACK_IDS: { // [n x stackid]
8116 // Save stack ids in the reader to consult when adding stack ids from the
8117 // lists in the stack node and alloc node entries.
8118 if (Version <= 11) {
8119 StackIds = ArrayRef<uint64_t>(Record);
8120 break;
8121 }
8122 // This is an array of 32-bit fixed-width values, holding each 64-bit
8123 // context id as a pair of adjacent (most significant first) 32-bit words.
8124 assert(Record.size() % 2 == 0);
8125 StackIds.reserve(n: Record.size() / 2);
8126 for (auto R = Record.begin(); R != Record.end(); R += 2)
8127 StackIds.push_back(x: *R << 32 | *(R + 1));
8128 break;
8129 }
8130
8131 case bitc::FS_CONTEXT_RADIX_TREE_ARRAY: { // [n x entry]
8132 RadixArray = ArrayRef<uint64_t>(Record);
8133 break;
8134 }
8135
8136 case bitc::FS_PERMODULE_CALLSITE_INFO: {
8137 unsigned ValueID = Record[0];
8138 SmallVector<unsigned> StackIdList;
8139 for (uint64_t R : drop_begin(RangeOrContainer&: Record)) {
8140 assert(R < StackIds.size());
8141 StackIdList.push_back(Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[R]));
8142 }
8143 ValueInfo VI = std::get<0>(in: getValueInfoFromValueId(ValueId: ValueID));
8144 PendingCallsites.push_back(x: CallsiteInfo({VI, std::move(StackIdList)}));
8145 break;
8146 }
8147
8148 case bitc::FS_COMBINED_CALLSITE_INFO: {
8149 auto RecordIter = Record.begin();
8150 unsigned ValueID = *RecordIter++;
8151 unsigned NumStackIds = *RecordIter++;
8152 unsigned NumVersions = *RecordIter++;
8153 assert(Record.size() == 3 + NumStackIds + NumVersions);
8154 SmallVector<unsigned> StackIdList;
8155 for (unsigned J = 0; J < NumStackIds; J++) {
8156 assert(*RecordIter < StackIds.size());
8157 StackIdList.push_back(
8158 Elt: TheIndex.addOrGetStackIdIndex(StackId: StackIds[*RecordIter++]));
8159 }
8160 SmallVector<unsigned> Versions;
8161 for (unsigned J = 0; J < NumVersions; J++)
8162 Versions.push_back(Elt: *RecordIter++);
8163 ValueInfo VI = std::get<0>(
8164 in: getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueId: ValueID));
8165 PendingCallsites.push_back(
8166 x: CallsiteInfo({VI, std::move(Versions), std::move(StackIdList)}));
8167 break;
8168 }
8169
8170 case bitc::FS_ALLOC_CONTEXT_IDS: {
8171 // This is an array of 32-bit fixed-width values, holding each 64-bit
8172 // context id as a pair of adjacent (most significant first) 32-bit words.
8173 assert(Record.size() % 2 == 0);
8174 PendingContextIds.reserve(n: Record.size() / 2);
8175 for (auto R = Record.begin(); R != Record.end(); R += 2)
8176 PendingContextIds.push_back(x: *R << 32 | *(R + 1));
8177 break;
8178 }
8179
8180 case bitc::FS_PERMODULE_ALLOC_INFO: {
8181 unsigned I = 0;
8182 std::vector<MIBInfo> MIBs;
8183 unsigned NumMIBs = 0;
8184 if (Version >= 10)
8185 NumMIBs = Record[I++];
8186 unsigned MIBsRead = 0;
8187 while ((Version >= 10 && MIBsRead++ < NumMIBs) ||
8188 (Version < 10 && I < Record.size())) {
8189 assert(Record.size() - I >= 2);
8190 AllocationType AllocType = (AllocationType)Record[I++];
8191 auto StackIdList = parseAllocInfoContext(Record, I);
8192 MIBs.push_back(x: MIBInfo(AllocType, std::move(StackIdList)));
8193 }
8194 // We either have nothing left or at least NumMIBs context size info
8195 // indices left (for the total sizes included when reporting of hinted
8196 // bytes is enabled).
8197 assert(I == Record.size() || Record.size() - I >= NumMIBs);
8198 std::vector<std::vector<ContextTotalSize>> AllContextSizes;
8199 if (I < Record.size()) {
8200 assert(!PendingContextIds.empty() &&
8201 "Missing context ids for alloc sizes");
8202 unsigned ContextIdIndex = 0;
8203 MIBsRead = 0;
8204 // The sizes are a linearized array of sizes, where for each MIB there
8205 // is 1 or more sizes (due to context trimming, each MIB in the metadata
8206 // and summarized here can correspond to more than one original context
8207 // from the profile).
8208 while (MIBsRead++ < NumMIBs) {
8209 // First read the number of contexts recorded for this MIB.
8210 unsigned NumContextSizeInfoEntries = Record[I++];
8211 assert(Record.size() - I >= NumContextSizeInfoEntries);
8212 std::vector<ContextTotalSize> ContextSizes;
8213 ContextSizes.reserve(n: NumContextSizeInfoEntries);
8214 for (unsigned J = 0; J < NumContextSizeInfoEntries; J++) {
8215 assert(ContextIdIndex < PendingContextIds.size());
8216 // Skip any 0 entries for MIBs without the context size info.
8217 if (PendingContextIds[ContextIdIndex] == 0) {
8218 // The size should also be 0 if the context was 0.
8219 assert(!Record[I]);
8220 ContextIdIndex++;
8221 I++;
8222 continue;
8223 }
8224 // PendingContextIds read from the preceding FS_ALLOC_CONTEXT_IDS
8225 // should be in the same order as the total sizes.
8226 ContextSizes.push_back(
8227 x: {.FullStackId: PendingContextIds[ContextIdIndex++], .TotalSize: Record[I++]});
8228 }
8229 AllContextSizes.push_back(x: std::move(ContextSizes));
8230 }
8231 PendingContextIds.clear();
8232 }
8233 PendingAllocs.push_back(x: AllocInfo(std::move(MIBs)));
8234 if (!AllContextSizes.empty()) {
8235 assert(PendingAllocs.back().MIBs.size() == AllContextSizes.size());
8236 PendingAllocs.back().ContextSizeInfos = std::move(AllContextSizes);
8237 }
8238 break;
8239 }
8240
8241 case bitc::FS_COMBINED_ALLOC_INFO:
8242 case bitc::FS_COMBINED_ALLOC_INFO_NO_CONTEXT: {
8243 unsigned I = 0;
8244 std::vector<MIBInfo> MIBs;
8245 unsigned NumMIBs = Record[I++];
8246 unsigned NumVersions = Record[I++];
8247 unsigned MIBsRead = 0;
8248 while (MIBsRead++ < NumMIBs) {
8249 assert(Record.size() - I >= 2);
8250 AllocationType AllocType = (AllocationType)Record[I++];
8251 SmallVector<unsigned> StackIdList;
8252 if (BitCode == bitc::FS_COMBINED_ALLOC_INFO)
8253 StackIdList = parseAllocInfoContext(Record, I);
8254 MIBs.push_back(x: MIBInfo(AllocType, std::move(StackIdList)));
8255 }
8256 assert(Record.size() - I >= NumVersions);
8257 SmallVector<uint8_t> Versions;
8258 for (unsigned J = 0; J < NumVersions; J++)
8259 Versions.push_back(Elt: Record[I++]);
8260 assert(I == Record.size());
8261 PendingAllocs.push_back(x: AllocInfo(std::move(Versions), std::move(MIBs)));
8262 break;
8263 }
8264 }
8265 }
8266 llvm_unreachable("Exit infinite loop");
8267}
8268
8269// Parse the module string table block into the Index.
8270// This populates the ModulePathStringTable map in the index.
8271Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
8272 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_STRTAB_BLOCK_ID))
8273 return Err;
8274
8275 SmallVector<uint64_t, 64> Record;
8276
8277 SmallString<128> ModulePath;
8278 ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr;
8279
8280 while (true) {
8281 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
8282 if (!MaybeEntry)
8283 return MaybeEntry.takeError();
8284 BitstreamEntry Entry = MaybeEntry.get();
8285
8286 switch (Entry.Kind) {
8287 case BitstreamEntry::SubBlock: // Handled for us already.
8288 case BitstreamEntry::Error:
8289 return error(Message: "Malformed block");
8290 case BitstreamEntry::EndBlock:
8291 return Error::success();
8292 case BitstreamEntry::Record:
8293 // The interesting case.
8294 break;
8295 }
8296
8297 Record.clear();
8298 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
8299 if (!MaybeRecord)
8300 return MaybeRecord.takeError();
8301 switch (MaybeRecord.get()) {
8302 default: // Default behavior: ignore.
8303 break;
8304 case bitc::MST_CODE_ENTRY: {
8305 // MST_ENTRY: [modid, namechar x N]
8306 uint64_t ModuleId = Record[0];
8307
8308 if (convertToString(Record, Idx: 1, Result&: ModulePath))
8309 return error(Message: "Invalid code_entry record");
8310
8311 LastSeenModule = TheIndex.addModule(ModPath: ModulePath);
8312 ModuleIdMap[ModuleId] = LastSeenModule->first();
8313
8314 ModulePath.clear();
8315 break;
8316 }
8317 /// MST_CODE_HASH: [5*i32]
8318 case bitc::MST_CODE_HASH: {
8319 if (Record.size() != 5)
8320 return error(Message: "Invalid hash length " + Twine(Record.size()).str());
8321 if (!LastSeenModule)
8322 return error(Message: "Invalid hash that does not follow a module path");
8323 int Pos = 0;
8324 for (auto &Val : Record) {
8325 assert(!(Val >> 32) && "Unexpected high bits set");
8326 LastSeenModule->second[Pos++] = Val;
8327 }
8328 // Reset LastSeenModule to avoid overriding the hash unexpectedly.
8329 LastSeenModule = nullptr;
8330 break;
8331 }
8332 }
8333 }
8334 llvm_unreachable("Exit infinite loop");
8335}
8336
8337namespace {
8338
8339// FIXME: This class is only here to support the transition to llvm::Error. It
8340// will be removed once this transition is complete. Clients should prefer to
8341// deal with the Error value directly, rather than converting to error_code.
8342class BitcodeErrorCategoryType : public std::error_category {
8343 const char *name() const noexcept override {
8344 return "llvm.bitcode";
8345 }
8346
8347 std::string message(int IE) const override {
8348 BitcodeError E = static_cast<BitcodeError>(IE);
8349 switch (E) {
8350 case BitcodeError::CorruptedBitcode:
8351 return "Corrupted bitcode";
8352 }
8353 llvm_unreachable("Unknown error type!");
8354 }
8355};
8356
8357} // end anonymous namespace
8358
8359const std::error_category &llvm::BitcodeErrorCategory() {
8360 static BitcodeErrorCategoryType ErrorCategory;
8361 return ErrorCategory;
8362}
8363
8364static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream,
8365 unsigned Block, unsigned RecordID) {
8366 if (Error Err = Stream.EnterSubBlock(BlockID: Block))
8367 return std::move(Err);
8368
8369 StringRef Strtab;
8370 while (true) {
8371 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
8372 if (!MaybeEntry)
8373 return MaybeEntry.takeError();
8374 llvm::BitstreamEntry Entry = MaybeEntry.get();
8375
8376 switch (Entry.Kind) {
8377 case BitstreamEntry::EndBlock:
8378 return Strtab;
8379
8380 case BitstreamEntry::Error:
8381 return error(Message: "Malformed block");
8382
8383 case BitstreamEntry::SubBlock:
8384 if (Error Err = Stream.SkipBlock())
8385 return std::move(Err);
8386 break;
8387
8388 case BitstreamEntry::Record:
8389 StringRef Blob;
8390 SmallVector<uint64_t, 1> Record;
8391 Expected<unsigned> MaybeRecord =
8392 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
8393 if (!MaybeRecord)
8394 return MaybeRecord.takeError();
8395 if (MaybeRecord.get() == RecordID)
8396 Strtab = Blob;
8397 break;
8398 }
8399 }
8400}
8401
8402//===----------------------------------------------------------------------===//
8403// External interface
8404//===----------------------------------------------------------------------===//
8405
8406Expected<std::vector<BitcodeModule>>
8407llvm::getBitcodeModuleList(MemoryBufferRef Buffer) {
8408 auto FOrErr = getBitcodeFileContents(Buffer);
8409 if (!FOrErr)
8410 return FOrErr.takeError();
8411 return std::move(FOrErr->Mods);
8412}
8413
8414Expected<BitcodeFileContents>
8415llvm::getBitcodeFileContents(MemoryBufferRef Buffer) {
8416 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
8417 if (!StreamOrErr)
8418 return StreamOrErr.takeError();
8419 BitstreamCursor &Stream = *StreamOrErr;
8420
8421 BitcodeFileContents F;
8422 while (true) {
8423 uint64_t BCBegin = Stream.getCurrentByteNo();
8424
8425 // We may be consuming bitcode from a client that leaves garbage at the end
8426 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
8427 // the end that there cannot possibly be another module, stop looking.
8428 if (BCBegin + 8 >= Stream.getBitcodeBytes().size())
8429 return F;
8430
8431 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
8432 if (!MaybeEntry)
8433 return MaybeEntry.takeError();
8434 llvm::BitstreamEntry Entry = MaybeEntry.get();
8435
8436 switch (Entry.Kind) {
8437 case BitstreamEntry::EndBlock:
8438 case BitstreamEntry::Error:
8439 return error(Message: "Malformed block");
8440
8441 case BitstreamEntry::SubBlock: {
8442 uint64_t IdentificationBit = -1ull;
8443 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
8444 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8;
8445 if (Error Err = Stream.SkipBlock())
8446 return std::move(Err);
8447
8448 {
8449 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
8450 if (!MaybeEntry)
8451 return MaybeEntry.takeError();
8452 Entry = MaybeEntry.get();
8453 }
8454
8455 if (Entry.Kind != BitstreamEntry::SubBlock ||
8456 Entry.ID != bitc::MODULE_BLOCK_ID)
8457 return error(Message: "Malformed block");
8458 }
8459
8460 if (Entry.ID == bitc::MODULE_BLOCK_ID) {
8461 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8;
8462 if (Error Err = Stream.SkipBlock())
8463 return std::move(Err);
8464
8465 F.Mods.push_back(x: {Stream.getBitcodeBytes().slice(
8466 N: BCBegin, M: Stream.getCurrentByteNo() - BCBegin),
8467 Buffer.getBufferIdentifier(), IdentificationBit,
8468 ModuleBit});
8469 continue;
8470 }
8471
8472 if (Entry.ID == bitc::STRTAB_BLOCK_ID) {
8473 Expected<StringRef> Strtab =
8474 readBlobInRecord(Stream, Block: bitc::STRTAB_BLOCK_ID, RecordID: bitc::STRTAB_BLOB);
8475 if (!Strtab)
8476 return Strtab.takeError();
8477 // This string table is used by every preceding bitcode module that does
8478 // not have its own string table. A bitcode file may have multiple
8479 // string tables if it was created by binary concatenation, for example
8480 // with "llvm-cat -b".
8481 for (BitcodeModule &I : llvm::reverse(C&: F.Mods)) {
8482 if (!I.Strtab.empty())
8483 break;
8484 I.Strtab = *Strtab;
8485 }
8486 // Similarly, the string table is used by every preceding symbol table;
8487 // normally there will be just one unless the bitcode file was created
8488 // by binary concatenation.
8489 if (!F.Symtab.empty() && F.StrtabForSymtab.empty())
8490 F.StrtabForSymtab = *Strtab;
8491 continue;
8492 }
8493
8494 if (Entry.ID == bitc::SYMTAB_BLOCK_ID) {
8495 Expected<StringRef> SymtabOrErr =
8496 readBlobInRecord(Stream, Block: bitc::SYMTAB_BLOCK_ID, RecordID: bitc::SYMTAB_BLOB);
8497 if (!SymtabOrErr)
8498 return SymtabOrErr.takeError();
8499
8500 // We can expect the bitcode file to have multiple symbol tables if it
8501 // was created by binary concatenation. In that case we silently
8502 // ignore any subsequent symbol tables, which is fine because this is a
8503 // low level function. The client is expected to notice that the number
8504 // of modules in the symbol table does not match the number of modules
8505 // in the input file and regenerate the symbol table.
8506 if (F.Symtab.empty())
8507 F.Symtab = *SymtabOrErr;
8508 continue;
8509 }
8510
8511 if (Error Err = Stream.SkipBlock())
8512 return std::move(Err);
8513 continue;
8514 }
8515 case BitstreamEntry::Record:
8516 if (Error E = Stream.skipRecord(AbbrevID: Entry.ID).takeError())
8517 return std::move(E);
8518 continue;
8519 }
8520 }
8521}
8522
8523/// Get a lazy one-at-time loading module from bitcode.
8524///
8525/// This isn't always used in a lazy context. In particular, it's also used by
8526/// \a parseModule(). If this is truly lazy, then we need to eagerly pull
8527/// in forward-referenced functions from block address references.
8528///
8529/// \param[in] MaterializeAll Set to \c true if we should materialize
8530/// everything.
8531Expected<std::unique_ptr<Module>>
8532BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll,
8533 bool ShouldLazyLoadMetadata, bool IsImporting,
8534 ParserCallbacks Callbacks) {
8535 BitstreamCursor Stream(Buffer);
8536
8537 std::string ProducerIdentification;
8538 if (IdentificationBit != -1ull) {
8539 if (Error JumpFailed = Stream.JumpToBit(BitNo: IdentificationBit))
8540 return std::move(JumpFailed);
8541 if (Error E =
8542 readIdentificationBlock(Stream).moveInto(Value&: ProducerIdentification))
8543 return std::move(E);
8544 }
8545
8546 if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit))
8547 return std::move(JumpFailed);
8548 auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification,
8549 Context);
8550
8551 std::unique_ptr<Module> M =
8552 std::make_unique<Module>(args&: ModuleIdentifier, args&: Context);
8553 M->setMaterializer(R);
8554
8555 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
8556 if (Error Err = R->parseBitcodeInto(M: M.get(), ShouldLazyLoadMetadata,
8557 IsImporting, Callbacks))
8558 return std::move(Err);
8559
8560 if (MaterializeAll) {
8561 // Read in the entire module, and destroy the BitcodeReader.
8562 if (Error Err = M->materializeAll())
8563 return std::move(Err);
8564 } else {
8565 // Resolve forward references from blockaddresses.
8566 if (Error Err = R->materializeForwardReferencedFunctions())
8567 return std::move(Err);
8568 }
8569
8570 return std::move(M);
8571}
8572
8573Expected<std::unique_ptr<Module>>
8574BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,
8575 bool IsImporting, ParserCallbacks Callbacks) {
8576 return getModuleImpl(Context, MaterializeAll: false, ShouldLazyLoadMetadata, IsImporting,
8577 Callbacks);
8578}
8579
8580// Parse the specified bitcode buffer and merge the index into CombinedIndex.
8581// We don't use ModuleIdentifier here because the client may need to control the
8582// module path used in the combined summary (e.g. when reading summaries for
8583// regular LTO modules).
8584Error BitcodeModule::readSummary(
8585 ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
8586 std::function<bool(GlobalValue::GUID)> IsPrevailing) {
8587 BitstreamCursor Stream(Buffer);
8588 if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit))
8589 return JumpFailed;
8590
8591 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex,
8592 ModulePath, IsPrevailing);
8593 return R.parseModule();
8594}
8595
8596// Parse the specified bitcode buffer, returning the function info index.
8597Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() {
8598 BitstreamCursor Stream(Buffer);
8599 if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit))
8600 return std::move(JumpFailed);
8601
8602 auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/args: false);
8603 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index,
8604 ModuleIdentifier, 0);
8605
8606 if (Error Err = R.parseModule())
8607 return std::move(Err);
8608
8609 return std::move(Index);
8610}
8611
8612static Expected<std::pair<bool, bool>>
8613getEnableSplitLTOUnitAndUnifiedFlag(BitstreamCursor &Stream, unsigned ID) {
8614 if (Error Err = Stream.EnterSubBlock(BlockID: ID))
8615 return std::move(Err);
8616
8617 SmallVector<uint64_t, 64> Record;
8618 while (true) {
8619 BitstreamEntry Entry;
8620 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Value&: Entry))
8621 return std::move(E);
8622
8623 switch (Entry.Kind) {
8624 case BitstreamEntry::SubBlock: // Handled for us already.
8625 case BitstreamEntry::Error:
8626 return error(Message: "Malformed block");
8627 case BitstreamEntry::EndBlock: {
8628 // If no flags record found, return both flags as false.
8629 return std::make_pair(x: false, y: false);
8630 }
8631 case BitstreamEntry::Record:
8632 // The interesting case.
8633 break;
8634 }
8635
8636 // Look for the FS_FLAGS record.
8637 Record.clear();
8638 Expected<unsigned> MaybeBitCode = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
8639 if (!MaybeBitCode)
8640 return MaybeBitCode.takeError();
8641 switch (MaybeBitCode.get()) {
8642 default: // Default behavior: ignore.
8643 break;
8644 case bitc::FS_FLAGS: { // [flags]
8645 uint64_t Flags = Record[0];
8646 // Scan flags.
8647 assert(Flags <= 0x7ff && "Unexpected bits in flag");
8648
8649 bool EnableSplitLTOUnit = Flags & 0x8;
8650 bool UnifiedLTO = Flags & 0x200;
8651 return std::make_pair(x&: EnableSplitLTOUnit, y&: UnifiedLTO);
8652 }
8653 }
8654 }
8655 llvm_unreachable("Exit infinite loop");
8656}
8657
8658// Check if the given bitcode buffer contains a global value summary block.
8659Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() {
8660 BitstreamCursor Stream(Buffer);
8661 if (Error JumpFailed = Stream.JumpToBit(BitNo: ModuleBit))
8662 return std::move(JumpFailed);
8663
8664 if (Error Err = Stream.EnterSubBlock(BlockID: bitc::MODULE_BLOCK_ID))
8665 return std::move(Err);
8666
8667 while (true) {
8668 llvm::BitstreamEntry Entry;
8669 if (Error E = Stream.advance().moveInto(Value&: Entry))
8670 return std::move(E);
8671
8672 switch (Entry.Kind) {
8673 case BitstreamEntry::Error:
8674 return error(Message: "Malformed block");
8675 case BitstreamEntry::EndBlock:
8676 return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false,
8677 /*EnableSplitLTOUnit=*/false, /*UnifiedLTO=*/false};
8678
8679 case BitstreamEntry::SubBlock:
8680 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID ||
8681 Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) {
8682 Expected<std::pair<bool, bool>> Flags =
8683 getEnableSplitLTOUnitAndUnifiedFlag(Stream, ID: Entry.ID);
8684 if (!Flags)
8685 return Flags.takeError();
8686 BitcodeLTOInfo LTOInfo;
8687 std::tie(args&: LTOInfo.EnableSplitLTOUnit, args&: LTOInfo.UnifiedLTO) = Flags.get();
8688 LTOInfo.IsThinLTO = (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID);
8689 LTOInfo.HasSummary = true;
8690 return LTOInfo;
8691 }
8692
8693 // Ignore other sub-blocks.
8694 if (Error Err = Stream.SkipBlock())
8695 return std::move(Err);
8696 continue;
8697
8698 case BitstreamEntry::Record:
8699 if (Expected<unsigned> StreamFailed = Stream.skipRecord(AbbrevID: Entry.ID))
8700 continue;
8701 else
8702 return StreamFailed.takeError();
8703 }
8704 }
8705}
8706
8707static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) {
8708 Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer);
8709 if (!MsOrErr)
8710 return MsOrErr.takeError();
8711
8712 if (MsOrErr->size() != 1)
8713 return error(Message: "Expected a single module");
8714
8715 return (*MsOrErr)[0];
8716}
8717
8718Expected<std::unique_ptr<Module>>
8719llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
8720 bool ShouldLazyLoadMetadata, bool IsImporting,
8721 ParserCallbacks Callbacks) {
8722 Expected<BitcodeModule> BM = getSingleModule(Buffer);
8723 if (!BM)
8724 return BM.takeError();
8725
8726 return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting,
8727 Callbacks);
8728}
8729
8730Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule(
8731 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
8732 bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks) {
8733 auto MOrErr = getLazyBitcodeModule(Buffer: *Buffer, Context, ShouldLazyLoadMetadata,
8734 IsImporting, Callbacks);
8735 if (MOrErr)
8736 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer));
8737 return MOrErr;
8738}
8739
8740Expected<std::unique_ptr<Module>>
8741BitcodeModule::parseModule(LLVMContext &Context, ParserCallbacks Callbacks) {
8742 return getModuleImpl(Context, MaterializeAll: true, ShouldLazyLoadMetadata: false, IsImporting: false, Callbacks);
8743 // TODO: Restore the use-lists to the in-memory state when the bitcode was
8744 // written. We must defer until the Module has been fully materialized.
8745}
8746
8747Expected<std::unique_ptr<Module>>
8748llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
8749 ParserCallbacks Callbacks) {
8750 Expected<BitcodeModule> BM = getSingleModule(Buffer);
8751 if (!BM)
8752 return BM.takeError();
8753
8754 return BM->parseModule(Context, Callbacks);
8755}
8756
8757Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) {
8758 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
8759 if (!StreamOrErr)
8760 return StreamOrErr.takeError();
8761
8762 return readTriple(Stream&: *StreamOrErr);
8763}
8764
8765Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) {
8766 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
8767 if (!StreamOrErr)
8768 return StreamOrErr.takeError();
8769
8770 return hasObjCCategory(Stream&: *StreamOrErr);
8771}
8772
8773Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) {
8774 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
8775 if (!StreamOrErr)
8776 return StreamOrErr.takeError();
8777
8778 return readIdentificationCode(Stream&: *StreamOrErr);
8779}
8780
8781Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer,
8782 ModuleSummaryIndex &CombinedIndex) {
8783 Expected<BitcodeModule> BM = getSingleModule(Buffer);
8784 if (!BM)
8785 return BM.takeError();
8786
8787 return BM->readSummary(CombinedIndex, ModulePath: BM->getModuleIdentifier());
8788}
8789
8790Expected<std::unique_ptr<ModuleSummaryIndex>>
8791llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {
8792 Expected<BitcodeModule> BM = getSingleModule(Buffer);
8793 if (!BM)
8794 return BM.takeError();
8795
8796 return BM->getSummary();
8797}
8798
8799Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) {
8800 Expected<BitcodeModule> BM = getSingleModule(Buffer);
8801 if (!BM)
8802 return BM.takeError();
8803
8804 return BM->getLTOInfo();
8805}
8806
8807Expected<std::unique_ptr<ModuleSummaryIndex>>
8808llvm::getModuleSummaryIndexForFile(StringRef Path,
8809 bool IgnoreEmptyThinLTOIndexFile) {
8810 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
8811 MemoryBuffer::getFileOrSTDIN(Filename: Path);
8812 if (!FileOrErr)
8813 return errorCodeToError(EC: FileOrErr.getError());
8814 if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize())
8815 return nullptr;
8816 return getModuleSummaryIndex(Buffer: **FileOrErr);
8817}
8818