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