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