Bug Summary

File:llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Warning:line 3459, column 5
Value stored to 'OpNum' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name BitcodeReader.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/build-llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader -I /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/build-llvm/lib/Bitcode/Reader -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-09-26-161721-17566-1 -x c++ /build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
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/GlobalAlias.h"
43#include "llvm/IR/GlobalIFunc.h"
44#include "llvm/IR/GlobalIndirectSymbol.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/LLVMContext.h"
55#include "llvm/IR/Metadata.h"
56#include "llvm/IR/Module.h"
57#include "llvm/IR/ModuleSummaryIndex.h"
58#include "llvm/IR/Operator.h"
59#include "llvm/IR/Type.h"
60#include "llvm/IR/Value.h"
61#include "llvm/IR/Verifier.h"
62#include "llvm/Support/AtomicOrdering.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/CommandLine.h"
65#include "llvm/Support/Compiler.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/Error.h"
68#include "llvm/Support/ErrorHandling.h"
69#include "llvm/Support/ErrorOr.h"
70#include "llvm/Support/ManagedStatic.h"
71#include "llvm/Support/MathExtras.h"
72#include "llvm/Support/MemoryBuffer.h"
73#include "llvm/Support/raw_ostream.h"
74#include <algorithm>
75#include <cassert>
76#include <cstddef>
77#include <cstdint>
78#include <deque>
79#include <map>
80#include <memory>
81#include <set>
82#include <string>
83#include <system_error>
84#include <tuple>
85#include <utility>
86#include <vector>
87
88using namespace llvm;
89
90static cl::opt<bool> PrintSummaryGUIDs(
91 "print-summary-global-ids", cl::init(false), cl::Hidden,
92 cl::desc(
93 "Print the global id for each value when reading the module summary"));
94
95namespace {
96
97enum {
98 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
99};
100
101} // end anonymous namespace
102
103static Error error(const Twine &Message) {
104 return make_error<StringError>(
105 Message, make_error_code(BitcodeError::CorruptedBitcode));
106}
107
108static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) {
109 if (!Stream.canSkipToPos(4))
110 return createStringError(std::errc::illegal_byte_sequence,
111 "file too small to contain bitcode header");
112 for (unsigned C : {'B', 'C'})
113 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
114 if (Res.get() != C)
115 return createStringError(std::errc::illegal_byte_sequence,
116 "file doesn't start with bitcode header");
117 } else
118 return Res.takeError();
119 for (unsigned C : {0x0, 0xC, 0xE, 0xD})
120 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) {
121 if (Res.get() != C)
122 return createStringError(std::errc::illegal_byte_sequence,
123 "file doesn't start with bitcode header");
124 } else
125 return Res.takeError();
126 return Error::success();
127}
128
129static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) {
130 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
131 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
132
133 if (Buffer.getBufferSize() & 3)
134 return error("Invalid bitcode signature");
135
136 // If we have a wrapper header, parse it and ignore the non-bc file contents.
137 // The magic number is 0x0B17C0DE stored in little endian.
138 if (isBitcodeWrapper(BufPtr, BufEnd))
139 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
140 return error("Invalid bitcode wrapper header");
141
142 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
143 if (Error Err = hasInvalidBitcodeHeader(Stream))
144 return std::move(Err);
145
146 return std::move(Stream);
147}
148
149/// Convert a string from a record into an std::string, return true on failure.
150template <typename StrTy>
151static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
152 StrTy &Result) {
153 if (Idx > Record.size())
154 return true;
155
156 Result.append(Record.begin() + Idx, Record.end());
157 return false;
158}
159
160// Strip all the TBAA attachment for the module.
161static void stripTBAA(Module *M) {
162 for (auto &F : *M) {
163 if (F.isMaterializable())
164 continue;
165 for (auto &I : instructions(F))
166 I.setMetadata(LLVMContext::MD_tbaa, nullptr);
167 }
168}
169
170/// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
171/// "epoch" encoded in the bitcode, and return the producer name if any.
172static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
173 if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
174 return std::move(Err);
175
176 // Read all the records.
177 SmallVector<uint64_t, 64> Record;
178
179 std::string ProducerIdentification;
180
181 while (true) {
182 BitstreamEntry Entry;
183 if (Expected<BitstreamEntry> Res = Stream.advance())
184 Entry = Res.get();
185 else
186 return Res.takeError();
187
188 switch (Entry.Kind) {
189 default:
190 case BitstreamEntry::Error:
191 return error("Malformed block");
192 case BitstreamEntry::EndBlock:
193 return ProducerIdentification;
194 case BitstreamEntry::Record:
195 // The interesting case.
196 break;
197 }
198
199 // Read a record.
200 Record.clear();
201 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
202 if (!MaybeBitCode)
203 return MaybeBitCode.takeError();
204 switch (MaybeBitCode.get()) {
205 default: // Default behavior: reject
206 return error("Invalid value");
207 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
208 convertToString(Record, 0, ProducerIdentification);
209 break;
210 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
211 unsigned epoch = (unsigned)Record[0];
212 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
213 return error(
214 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
215 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
216 }
217 }
218 }
219 }
220}
221
222static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
223 // We expect a number of well-defined blocks, though we don't necessarily
224 // need to understand them all.
225 while (true) {
226 if (Stream.AtEndOfStream())
227 return "";
228
229 BitstreamEntry Entry;
230 if (Expected<BitstreamEntry> Res = Stream.advance())
231 Entry = std::move(Res.get());
232 else
233 return Res.takeError();
234
235 switch (Entry.Kind) {
236 case BitstreamEntry::EndBlock:
237 case BitstreamEntry::Error:
238 return error("Malformed block");
239
240 case BitstreamEntry::SubBlock:
241 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
242 return readIdentificationBlock(Stream);
243
244 // Ignore other sub-blocks.
245 if (Error Err = Stream.SkipBlock())
246 return std::move(Err);
247 continue;
248 case BitstreamEntry::Record:
249 if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
250 continue;
251 else
252 return Skipped.takeError();
253 }
254 }
255}
256
257static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
258 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
259 return std::move(Err);
260
261 SmallVector<uint64_t, 64> Record;
262 // Read all the records for this module.
263
264 while (true) {
265 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
266 if (!MaybeEntry)
267 return MaybeEntry.takeError();
268 BitstreamEntry Entry = MaybeEntry.get();
269
270 switch (Entry.Kind) {
271 case BitstreamEntry::SubBlock: // Handled for us already.
272 case BitstreamEntry::Error:
273 return error("Malformed block");
274 case BitstreamEntry::EndBlock:
275 return false;
276 case BitstreamEntry::Record:
277 // The interesting case.
278 break;
279 }
280
281 // Read a record.
282 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
283 if (!MaybeRecord)
284 return MaybeRecord.takeError();
285 switch (MaybeRecord.get()) {
286 default:
287 break; // Default behavior, ignore unknown content.
288 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
289 std::string S;
290 if (convertToString(Record, 0, S))
291 return error("Invalid record");
292 // Check for the i386 and other (x86_64, ARM) conventions
293 if (S.find("__DATA,__objc_catlist") != std::string::npos ||
294 S.find("__OBJC,__category") != std::string::npos)
295 return true;
296 break;
297 }
298 }
299 Record.clear();
300 }
301 llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 301)
;
302}
303
304static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
305 // We expect a number of well-defined blocks, though we don't necessarily
306 // need to understand them all.
307 while (true) {
308 BitstreamEntry Entry;
309 if (Expected<BitstreamEntry> Res = Stream.advance())
310 Entry = std::move(Res.get());
311 else
312 return Res.takeError();
313
314 switch (Entry.Kind) {
315 case BitstreamEntry::Error:
316 return error("Malformed block");
317 case BitstreamEntry::EndBlock:
318 return false;
319
320 case BitstreamEntry::SubBlock:
321 if (Entry.ID == bitc::MODULE_BLOCK_ID)
322 return hasObjCCategoryInModule(Stream);
323
324 // Ignore other sub-blocks.
325 if (Error Err = Stream.SkipBlock())
326 return std::move(Err);
327 continue;
328
329 case BitstreamEntry::Record:
330 if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
331 continue;
332 else
333 return Skipped.takeError();
334 }
335 }
336}
337
338static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) {
339 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
340 return std::move(Err);
341
342 SmallVector<uint64_t, 64> Record;
343
344 std::string Triple;
345
346 // Read all the records for this module.
347 while (true) {
348 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
349 if (!MaybeEntry)
350 return MaybeEntry.takeError();
351 BitstreamEntry Entry = MaybeEntry.get();
352
353 switch (Entry.Kind) {
354 case BitstreamEntry::SubBlock: // Handled for us already.
355 case BitstreamEntry::Error:
356 return error("Malformed block");
357 case BitstreamEntry::EndBlock:
358 return Triple;
359 case BitstreamEntry::Record:
360 // The interesting case.
361 break;
362 }
363
364 // Read a record.
365 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
366 if (!MaybeRecord)
367 return MaybeRecord.takeError();
368 switch (MaybeRecord.get()) {
369 default: break; // Default behavior, ignore unknown content.
370 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
371 std::string S;
372 if (convertToString(Record, 0, S))
373 return error("Invalid record");
374 Triple = S;
375 break;
376 }
377 }
378 Record.clear();
379 }
380 llvm_unreachable("Exit infinite loop")::llvm::llvm_unreachable_internal("Exit infinite loop", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 380)
;
381}
382
383static Expected<std::string> readTriple(BitstreamCursor &Stream) {
384 // We expect a number of well-defined blocks, though we don't necessarily
385 // need to understand them all.
386 while (true) {
387 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
388 if (!MaybeEntry)
389 return MaybeEntry.takeError();
390 BitstreamEntry Entry = MaybeEntry.get();
391
392 switch (Entry.Kind) {
393 case BitstreamEntry::Error:
394 return error("Malformed block");
395 case BitstreamEntry::EndBlock:
396 return "";
397
398 case BitstreamEntry::SubBlock:
399 if (Entry.ID == bitc::MODULE_BLOCK_ID)
400 return readModuleTriple(Stream);
401
402 // Ignore other sub-blocks.
403 if (Error Err = Stream.SkipBlock())
404 return std::move(Err);
405 continue;
406
407 case BitstreamEntry::Record:
408 if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
409 continue;
410 else
411 return Skipped.takeError();
412 }
413 }
414}
415
416namespace {
417
418class BitcodeReaderBase {
419protected:
420 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
421 : Stream(std::move(Stream)), Strtab(Strtab) {
422 this->Stream.setBlockInfo(&BlockInfo);
423 }
424
425 BitstreamBlockInfo BlockInfo;
426 BitstreamCursor Stream;
427 StringRef Strtab;
428
429 /// In version 2 of the bitcode we store names of global values and comdats in
430 /// a string table rather than in the VST.
431 bool UseStrtab = false;
432
433 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
434
435 /// If this module uses a string table, pop the reference to the string table
436 /// and return the referenced string and the rest of the record. Otherwise
437 /// just return the record itself.
438 std::pair<StringRef, ArrayRef<uint64_t>>
439 readNameFromStrtab(ArrayRef<uint64_t> Record);
440
441 bool readBlockInfo();
442
443 // Contains an arbitrary and optional string identifying the bitcode producer
444 std::string ProducerIdentification;
445
446 Error error(const Twine &Message);
447};
448
449} // end anonymous namespace
450
451Error BitcodeReaderBase::error(const Twine &Message) {
452 std::string FullMsg = Message.str();
453 if (!ProducerIdentification.empty())
454 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
455 LLVM_VERSION_STRING"12.0.0" "')";
456 return ::error(FullMsg);
457}
458
459Expected<unsigned>
460BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
461 if (Record.empty())
462 return error("Invalid record");
463 unsigned ModuleVersion = Record[0];
464 if (ModuleVersion > 2)
465 return error("Invalid value");
466 UseStrtab = ModuleVersion >= 2;
467 return ModuleVersion;
468}
469
470std::pair<StringRef, ArrayRef<uint64_t>>
471BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
472 if (!UseStrtab)
473 return {"", Record};
474 // Invalid reference. Let the caller complain about the record being empty.
475 if (Record[0] + Record[1] > Strtab.size())
476 return {"", {}};
477 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
478}
479
480namespace {
481
482class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
483 LLVMContext &Context;
484 Module *TheModule = nullptr;
485 // Next offset to start scanning for lazy parsing of function bodies.
486 uint64_t NextUnreadBit = 0;
487 // Last function offset found in the VST.
488 uint64_t LastFunctionBlockBit = 0;
489 bool SeenValueSymbolTable = false;
490 uint64_t VSTOffset = 0;
491
492 std::vector<std::string> SectionTable;
493 std::vector<std::string> GCTable;
494
495 std::vector<Type*> TypeList;
496 DenseMap<Function *, FunctionType *> FunctionTypes;
497 BitcodeReaderValueList ValueList;
498 Optional<MetadataLoader> MDLoader;
499 std::vector<Comdat *> ComdatList;
500 SmallVector<Instruction *, 64> InstructionList;
501
502 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
503 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> IndirectSymbolInits;
504 std::vector<std::pair<Function *, unsigned>> FunctionPrefixes;
505 std::vector<std::pair<Function *, unsigned>> FunctionPrologues;
506 std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFns;
507
508 /// The set of attributes by index. Index zero in the file is for null, and
509 /// is thus not represented here. As such all indices are off by one.
510 std::vector<AttributeList> MAttributes;
511
512 /// The set of attribute groups.
513 std::map<unsigned, AttributeList> MAttributeGroups;
514
515 /// While parsing a function body, this is a list of the basic blocks for the
516 /// function.
517 std::vector<BasicBlock*> FunctionBBs;
518
519 // When reading the module header, this list is populated with functions that
520 // have bodies later in the file.
521 std::vector<Function*> FunctionsWithBodies;
522
523 // When intrinsic functions are encountered which require upgrading they are
524 // stored here with their replacement function.
525 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
526 UpdatedIntrinsicMap UpgradedIntrinsics;
527 // Intrinsics which were remangled because of types rename
528 UpdatedIntrinsicMap RemangledIntrinsics;
529
530 // Several operations happen after the module header has been read, but
531 // before function bodies are processed. This keeps track of whether
532 // we've done this yet.
533 bool SeenFirstFunctionBody = false;
534
535 /// When function bodies are initially scanned, this map contains info about
536 /// where to find deferred function body in the stream.
537 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
538
539 /// When Metadata block is initially scanned when parsing the module, we may
540 /// choose to defer parsing of the metadata. This vector contains info about
541 /// which Metadata blocks are deferred.
542 std::vector<uint64_t> DeferredMetadataInfo;
543
544 /// These are basic blocks forward-referenced by block addresses. They are
545 /// inserted lazily into functions when they're loaded. The basic block ID is
546 /// its index into the vector.
547 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
548 std::deque<Function *> BasicBlockFwdRefQueue;
549
550 /// Indicates that we are using a new encoding for instruction operands where
551 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
552 /// instruction number, for a more compact encoding. Some instruction
553 /// operands are not relative to the instruction ID: basic block numbers, and
554 /// types. Once the old style function blocks have been phased out, we would
555 /// not need this flag.
556 bool UseRelativeIDs = false;
557
558 /// True if all functions will be materialized, negating the need to process
559 /// (e.g.) blockaddress forward references.
560 bool WillMaterializeAllForwardRefs = false;
561
562 bool StripDebugInfo = false;
563 TBAAVerifier TBAAVerifyHelper;
564
565 std::vector<std::string> BundleTags;
566 SmallVector<SyncScope::ID, 8> SSIDs;
567
568public:
569 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
570 StringRef ProducerIdentification, LLVMContext &Context);
571
572 Error materializeForwardReferencedFunctions();
573
574 Error materialize(GlobalValue *GV) override;
575 Error materializeModule() override;
576 std::vector<StructType *> getIdentifiedStructTypes() const override;
577
578 /// Main interface to parsing a bitcode buffer.
579 /// \returns true if an error occurred.
580 Error parseBitcodeInto(
581 Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false,
582 DataLayoutCallbackTy DataLayoutCallback = [](std::string) {
583 return None;
584 });
585
586 static uint64_t decodeSignRotatedValue(uint64_t V);
587
588 /// Materialize any deferred Metadata block.
589 Error materializeMetadata() override;
590
591 void setStripDebugInfo() override;
592
593private:
594 std::vector<StructType *> IdentifiedStructTypes;
595 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
596 StructType *createIdentifiedStructType(LLVMContext &Context);
597
598 /// Map all pointer types within \param Ty to the opaque pointer
599 /// type in the same address space if opaque pointers are being
600 /// used, otherwise nop. This converts a bitcode-reader internal
601 /// type into one suitable for use in a Value.
602 Type *flattenPointerTypes(Type *Ty) {
603 return Ty;
604 }
605
606 /// Given a fully structured pointer type (i.e. not opaque), return
607 /// the flattened form of its element, suitable for use in a Value.
608 Type *getPointerElementFlatType(Type *Ty) {
609 return flattenPointerTypes(cast<PointerType>(Ty)->getElementType());
610 }
611
612 /// Given a fully structured pointer type, get its element type in
613 /// both fully structured form, and flattened form suitable for use
614 /// in a Value.
615 std::pair<Type *, Type *> getPointerElementTypes(Type *FullTy) {
616 Type *ElTy = cast<PointerType>(FullTy)->getElementType();
617 return std::make_pair(ElTy, flattenPointerTypes(ElTy));
618 }
619
620 /// Return the flattened type (suitable for use in a Value)
621 /// specified by the given \param ID .
622 Type *getTypeByID(unsigned ID) {
623 return flattenPointerTypes(getFullyStructuredTypeByID(ID));
624 }
625
626 /// Return the fully structured (bitcode-reader internal) type
627 /// corresponding to the given \param ID .
628 Type *getFullyStructuredTypeByID(unsigned ID);
629
630 Value *getFnValueByID(unsigned ID, Type *Ty, Type **FullTy = nullptr) {
631 if (Ty && Ty->isMetadataTy())
632 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
633 return ValueList.getValueFwdRef(ID, Ty, FullTy);
634 }
635
636 Metadata *getFnMetadataByID(unsigned ID) {
637 return MDLoader->getMetadataFwdRefOrLoad(ID);
638 }
639
640 BasicBlock *getBasicBlock(unsigned ID) const {
641 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
642 return FunctionBBs[ID];
643 }
644
645 AttributeList getAttributes(unsigned i) const {
646 if (i-1 < MAttributes.size())
647 return MAttributes[i-1];
648 return AttributeList();
649 }
650
651 /// Read a value/type pair out of the specified record from slot 'Slot'.
652 /// Increment Slot past the number of slots used in the record. Return true on
653 /// failure.
654 bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
655 unsigned InstNum, Value *&ResVal,
656 Type **FullTy = nullptr) {
657 if (Slot == Record.size()) return true;
658 unsigned ValNo = (unsigned)Record[Slot++];
659 // Adjust the ValNo, if it was encoded relative to the InstNum.
660 if (UseRelativeIDs)
661 ValNo = InstNum - ValNo;
662 if (ValNo < InstNum) {
663 // If this is not a forward reference, just return the value we already
664 // have.
665 ResVal = getFnValueByID(ValNo, nullptr, FullTy);
666 return ResVal == nullptr;
667 }
668 if (Slot == Record.size())
669 return true;
670
671 unsigned TypeNo = (unsigned)Record[Slot++];
672 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
673 if (FullTy)
674 *FullTy = getFullyStructuredTypeByID(TypeNo);
675 return ResVal == nullptr;
676 }
677
678 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
679 /// past the number of slots used by the value in the record. Return true if
680 /// there is an error.
681 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
682 unsigned InstNum, Type *Ty, Value *&ResVal) {
683 if (getValue(Record, Slot, InstNum, Ty, ResVal))
684 return true;
685 // All values currently take a single record slot.
686 ++Slot;
687 return false;
688 }
689
690 /// Like popValue, but does not increment the Slot number.
691 bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
692 unsigned InstNum, Type *Ty, Value *&ResVal) {
693 ResVal = getValue(Record, Slot, InstNum, Ty);
694 return ResVal == nullptr;
695 }
696
697 /// Version of getValue that returns ResVal directly, or 0 if there is an
698 /// error.
699 Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
700 unsigned InstNum, Type *Ty) {
701 if (Slot == Record.size()) return nullptr;
702 unsigned ValNo = (unsigned)Record[Slot];
703 // Adjust the ValNo, if it was encoded relative to the InstNum.
704 if (UseRelativeIDs)
705 ValNo = InstNum - ValNo;
706 return getFnValueByID(ValNo, Ty);
707 }
708
709 /// Like getValue, but decodes signed VBRs.
710 Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot,
711 unsigned InstNum, Type *Ty) {
712 if (Slot == Record.size()) return nullptr;
713 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
714 // Adjust the ValNo, if it was encoded relative to the InstNum.
715 if (UseRelativeIDs)
716 ValNo = InstNum - ValNo;
717 return getFnValueByID(ValNo, Ty);
718 }
719
720 /// Upgrades old-style typeless byval or sret attributes by adding the
721 /// corresponding argument's pointee type.
722 void propagateByValSRetTypes(CallBase *CB, ArrayRef<Type *> ArgsFullTys);
723
724 /// Converts alignment exponent (i.e. power of two (or zero)) to the
725 /// corresponding alignment to use. If alignment is too large, returns
726 /// a corresponding error code.
727 Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment);
728 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
729 Error parseModule(
730 uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false,
731 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
732
733 Error parseComdatRecord(ArrayRef<uint64_t> Record);
734 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
735 Error parseFunctionRecord(ArrayRef<uint64_t> Record);
736 Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
737 ArrayRef<uint64_t> Record);
738
739 Error parseAttributeBlock();
740 Error parseAttributeGroupBlock();
741 Error parseTypeTable();
742 Error parseTypeTableBody();
743 Error parseOperandBundleTags();
744 Error parseSyncScopeNames();
745
746 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
747 unsigned NameIndex, Triple &TT);
748 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
749 ArrayRef<uint64_t> Record);
750 Error parseValueSymbolTable(uint64_t Offset = 0);
751 Error parseGlobalValueSymbolTable();
752 Error parseConstants();
753 Error rememberAndSkipFunctionBodies();
754 Error rememberAndSkipFunctionBody();
755 /// Save the positions of the Metadata blocks and skip parsing the blocks.
756 Error rememberAndSkipMetadata();
757 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
758 Error parseFunctionBody(Function *F);
759 Error globalCleanup();
760 Error resolveGlobalAndIndirectSymbolInits();
761 Error parseUseLists();
762 Error findFunctionInStream(
763 Function *F,
764 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
765
766 SyncScope::ID getDecodedSyncScopeID(unsigned Val);
767};
768
769/// Class to manage reading and parsing function summary index bitcode
770/// files/sections.
771class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
772 /// The module index built during parsing.
773 ModuleSummaryIndex &TheIndex;
774
775 /// Indicates whether we have encountered a global value summary section
776 /// yet during parsing.
777 bool SeenGlobalValSummary = false;
778
779 /// Indicates whether we have already parsed the VST, used for error checking.
780 bool SeenValueSymbolTable = false;
781
782 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
783 /// Used to enable on-demand parsing of the VST.
784 uint64_t VSTOffset = 0;
785
786 // Map to save ValueId to ValueInfo association that was recorded in the
787 // ValueSymbolTable. It is used after the VST is parsed to convert
788 // call graph edges read from the function summary from referencing
789 // callees by their ValueId to using the ValueInfo instead, which is how
790 // they are recorded in the summary index being built.
791 // We save a GUID which refers to the same global as the ValueInfo, but
792 // ignoring the linkage, i.e. for values other than local linkage they are
793 // identical.
794 DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>>
795 ValueIdToValueInfoMap;
796
797 /// Map populated during module path string table parsing, from the
798 /// module ID to a string reference owned by the index's module
799 /// path string table, used to correlate with combined index
800 /// summary records.
801 DenseMap<uint64_t, StringRef> ModuleIdMap;
802
803 /// Original source file name recorded in a bitcode record.
804 std::string SourceFileName;
805
806 /// The string identifier given to this module by the client, normally the
807 /// path to the bitcode file.
808 StringRef ModulePath;
809
810 /// For per-module summary indexes, the unique numerical identifier given to
811 /// this module by the client.
812 unsigned ModuleId;
813
814public:
815 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
816 ModuleSummaryIndex &TheIndex,
817 StringRef ModulePath, unsigned ModuleId);
818
819 Error parseModule();
820
821private:
822 void setValueGUID(uint64_t ValueID, StringRef ValueName,
823 GlobalValue::LinkageTypes Linkage,
824 StringRef SourceFileName);
825 Error parseValueSymbolTable(
826 uint64_t Offset,
827 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
828 std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record);
829 std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
830 bool IsOldProfileFormat,
831 bool HasProfile,
832 bool HasRelBF);
833 Error parseEntireSummary(unsigned ID);
834 Error parseModuleStringTable();
835 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
836 void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,
837 TypeIdCompatibleVtableInfo &TypeId);
838 std::vector<FunctionSummary::ParamAccess>
839 parseParamAccesses(ArrayRef<uint64_t> Record);
840
841 std::pair<ValueInfo, GlobalValue::GUID>
842 getValueInfoFromValueId(unsigned ValueId);
843
844 void addThisModule();
845 ModuleSummaryIndex::ModuleInfo *getThisModule();
846};
847
848} // end anonymous namespace
849
850std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx,
851 Error Err) {
852 if (Err) {
853 std::error_code EC;
854 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
855 EC = EIB.convertToErrorCode();
856 Ctx.emitError(EIB.message());
857 });
858 return EC;
859 }
860 return std::error_code();
861}
862
863BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
864 StringRef ProducerIdentification,
865 LLVMContext &Context)
866 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
867 ValueList(Context, Stream.SizeInBytes()) {
868 this->ProducerIdentification = std::string(ProducerIdentification);
869}
870
871Error BitcodeReader::materializeForwardReferencedFunctions() {
872 if (WillMaterializeAllForwardRefs)
873 return Error::success();
874
875 // Prevent recursion.
876 WillMaterializeAllForwardRefs = true;
877
878 while (!BasicBlockFwdRefQueue.empty()) {
879 Function *F = BasicBlockFwdRefQueue.front();
880 BasicBlockFwdRefQueue.pop_front();
881 assert(F && "Expected valid function")((F && "Expected valid function") ? static_cast<void
> (0) : __assert_fail ("F && \"Expected valid function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 881, __PRETTY_FUNCTION__))
;
882 if (!BasicBlockFwdRefs.count(F))
883 // Already materialized.
884 continue;
885
886 // Check for a function that isn't materializable to prevent an infinite
887 // loop. When parsing a blockaddress stored in a global variable, there
888 // isn't a trivial way to check if a function will have a body without a
889 // linear search through FunctionsWithBodies, so just check it here.
890 if (!F->isMaterializable())
891 return error("Never resolved function from blockaddress");
892
893 // Try to materialize F.
894 if (Error Err = materialize(F))
895 return Err;
896 }
897 assert(BasicBlockFwdRefs.empty() && "Function missing from queue")((BasicBlockFwdRefs.empty() && "Function missing from queue"
) ? static_cast<void> (0) : __assert_fail ("BasicBlockFwdRefs.empty() && \"Function missing from queue\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 897, __PRETTY_FUNCTION__))
;
898
899 // Reset state.
900 WillMaterializeAllForwardRefs = false;
901 return Error::success();
902}
903
904//===----------------------------------------------------------------------===//
905// Helper functions to implement forward reference resolution, etc.
906//===----------------------------------------------------------------------===//
907
908static bool hasImplicitComdat(size_t Val) {
909 switch (Val) {
910 default:
911 return false;
912 case 1: // Old WeakAnyLinkage
913 case 4: // Old LinkOnceAnyLinkage
914 case 10: // Old WeakODRLinkage
915 case 11: // Old LinkOnceODRLinkage
916 return true;
917 }
918}
919
920static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
921 switch (Val) {
922 default: // Map unknown/new linkages to external
923 case 0:
924 return GlobalValue::ExternalLinkage;
925 case 2:
926 return GlobalValue::AppendingLinkage;
927 case 3:
928 return GlobalValue::InternalLinkage;
929 case 5:
930 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
931 case 6:
932 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
933 case 7:
934 return GlobalValue::ExternalWeakLinkage;
935 case 8:
936 return GlobalValue::CommonLinkage;
937 case 9:
938 return GlobalValue::PrivateLinkage;
939 case 12:
940 return GlobalValue::AvailableExternallyLinkage;
941 case 13:
942 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
943 case 14:
944 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
945 case 15:
946 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
947 case 1: // Old value with implicit comdat.
948 case 16:
949 return GlobalValue::WeakAnyLinkage;
950 case 10: // Old value with implicit comdat.
951 case 17:
952 return GlobalValue::WeakODRLinkage;
953 case 4: // Old value with implicit comdat.
954 case 18:
955 return GlobalValue::LinkOnceAnyLinkage;
956 case 11: // Old value with implicit comdat.
957 case 19:
958 return GlobalValue::LinkOnceODRLinkage;
959 }
960}
961
962static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) {
963 FunctionSummary::FFlags Flags;
964 Flags.ReadNone = RawFlags & 0x1;
965 Flags.ReadOnly = (RawFlags >> 1) & 0x1;
966 Flags.NoRecurse = (RawFlags >> 2) & 0x1;
967 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
968 Flags.NoInline = (RawFlags >> 4) & 0x1;
969 Flags.AlwaysInline = (RawFlags >> 5) & 0x1;
970 return Flags;
971}
972
973/// Decode the flags for GlobalValue in the summary.
974static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
975 uint64_t Version) {
976 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
977 // like getDecodedLinkage() above. Any future change to the linkage enum and
978 // to getDecodedLinkage() will need to be taken into account here as above.
979 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
980 RawFlags = RawFlags >> 4;
981 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
982 // The Live flag wasn't introduced until version 3. For dead stripping
983 // to work correctly on earlier versions, we must conservatively treat all
984 // values as live.
985 bool Live = (RawFlags & 0x2) || Version < 3;
986 bool Local = (RawFlags & 0x4);
987 bool AutoHide = (RawFlags & 0x8);
988
989 return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live, Local, AutoHide);
990}
991
992// Decode the flags for GlobalVariable in the summary
993static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) {
994 return GlobalVarSummary::GVarFlags(
995 (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false,
996 (RawFlags & 0x4) ? true : false,
997 (GlobalObject::VCallVisibility)(RawFlags >> 3));
998}
999
1000static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
1001 switch (Val) {
1002 default: // Map unknown visibilities to default.
1003 case 0: return GlobalValue::DefaultVisibility;
1004 case 1: return GlobalValue::HiddenVisibility;
1005 case 2: return GlobalValue::ProtectedVisibility;
1006 }
1007}
1008
1009static GlobalValue::DLLStorageClassTypes
1010getDecodedDLLStorageClass(unsigned Val) {
1011 switch (Val) {
1012 default: // Map unknown values to default.
1013 case 0: return GlobalValue::DefaultStorageClass;
1014 case 1: return GlobalValue::DLLImportStorageClass;
1015 case 2: return GlobalValue::DLLExportStorageClass;
1016 }
1017}
1018
1019static bool getDecodedDSOLocal(unsigned Val) {
1020 switch(Val) {
1021 default: // Map unknown values to preemptable.
1022 case 0: return false;
1023 case 1: return true;
1024 }
1025}
1026
1027static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
1028 switch (Val) {
1029 case 0: return GlobalVariable::NotThreadLocal;
1030 default: // Map unknown non-zero value to general dynamic.
1031 case 1: return GlobalVariable::GeneralDynamicTLSModel;
1032 case 2: return GlobalVariable::LocalDynamicTLSModel;
1033 case 3: return GlobalVariable::InitialExecTLSModel;
1034 case 4: return GlobalVariable::LocalExecTLSModel;
1035 }
1036}
1037
1038static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
1039 switch (Val) {
1040 default: // Map unknown to UnnamedAddr::None.
1041 case 0: return GlobalVariable::UnnamedAddr::None;
1042 case 1: return GlobalVariable::UnnamedAddr::Global;
1043 case 2: return GlobalVariable::UnnamedAddr::Local;
1044 }
1045}
1046
1047static int getDecodedCastOpcode(unsigned Val) {
1048 switch (Val) {
1049 default: return -1;
1050 case bitc::CAST_TRUNC : return Instruction::Trunc;
1051 case bitc::CAST_ZEXT : return Instruction::ZExt;
1052 case bitc::CAST_SEXT : return Instruction::SExt;
1053 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
1054 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
1055 case bitc::CAST_UITOFP : return Instruction::UIToFP;
1056 case bitc::CAST_SITOFP : return Instruction::SIToFP;
1057 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
1058 case bitc::CAST_FPEXT : return Instruction::FPExt;
1059 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
1060 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
1061 case bitc::CAST_BITCAST : return Instruction::BitCast;
1062 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
1063 }
1064}
1065
1066static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
1067 bool IsFP = Ty->isFPOrFPVectorTy();
1068 // UnOps are only valid for int/fp or vector of int/fp types
1069 if (!IsFP && !Ty->isIntOrIntVectorTy())
1070 return -1;
1071
1072 switch (Val) {
1073 default:
1074 return -1;
1075 case bitc::UNOP_FNEG:
1076 return IsFP ? Instruction::FNeg : -1;
1077 }
1078}
1079
1080static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
1081 bool IsFP = Ty->isFPOrFPVectorTy();
1082 // BinOps are only valid for int/fp or vector of int/fp types
1083 if (!IsFP && !Ty->isIntOrIntVectorTy())
1084 return -1;
1085
1086 switch (Val) {
1087 default:
1088 return -1;
1089 case bitc::BINOP_ADD:
1090 return IsFP ? Instruction::FAdd : Instruction::Add;
1091 case bitc::BINOP_SUB:
1092 return IsFP ? Instruction::FSub : Instruction::Sub;
1093 case bitc::BINOP_MUL:
1094 return IsFP ? Instruction::FMul : Instruction::Mul;
1095 case bitc::BINOP_UDIV:
1096 return IsFP ? -1 : Instruction::UDiv;
1097 case bitc::BINOP_SDIV:
1098 return IsFP ? Instruction::FDiv : Instruction::SDiv;
1099 case bitc::BINOP_UREM:
1100 return IsFP ? -1 : Instruction::URem;
1101 case bitc::BINOP_SREM:
1102 return IsFP ? Instruction::FRem : Instruction::SRem;
1103 case bitc::BINOP_SHL:
1104 return IsFP ? -1 : Instruction::Shl;
1105 case bitc::BINOP_LSHR:
1106 return IsFP ? -1 : Instruction::LShr;
1107 case bitc::BINOP_ASHR:
1108 return IsFP ? -1 : Instruction::AShr;
1109 case bitc::BINOP_AND:
1110 return IsFP ? -1 : Instruction::And;
1111 case bitc::BINOP_OR:
1112 return IsFP ? -1 : Instruction::Or;
1113 case bitc::BINOP_XOR:
1114 return IsFP ? -1 : Instruction::Xor;
1115 }
1116}
1117
1118static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
1119 switch (Val) {
1120 default: return AtomicRMWInst::BAD_BINOP;
1121 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
1122 case bitc::RMW_ADD: return AtomicRMWInst::Add;
1123 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
1124 case bitc::RMW_AND: return AtomicRMWInst::And;
1125 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
1126 case bitc::RMW_OR: return AtomicRMWInst::Or;
1127 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
1128 case bitc::RMW_MAX: return AtomicRMWInst::Max;
1129 case bitc::RMW_MIN: return AtomicRMWInst::Min;
1130 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
1131 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
1132 case bitc::RMW_FADD: return AtomicRMWInst::FAdd;
1133 case bitc::RMW_FSUB: return AtomicRMWInst::FSub;
1134 }
1135}
1136
1137static AtomicOrdering getDecodedOrdering(unsigned Val) {
1138 switch (Val) {
1139 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
1140 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
1141 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
1142 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
1143 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
1144 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
1145 default: // Map unknown orderings to sequentially-consistent.
1146 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1147 }
1148}
1149
1150static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
1151 switch (Val) {
1152 default: // Map unknown selection kinds to any.
1153 case bitc::COMDAT_SELECTION_KIND_ANY:
1154 return Comdat::Any;
1155 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
1156 return Comdat::ExactMatch;
1157 case bitc::COMDAT_SELECTION_KIND_LARGEST:
1158 return Comdat::Largest;
1159 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
1160 return Comdat::NoDuplicates;
1161 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
1162 return Comdat::SameSize;
1163 }
1164}
1165
1166static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
1167 FastMathFlags FMF;
1168 if (0 != (Val & bitc::UnsafeAlgebra))
1169 FMF.setFast();
1170 if (0 != (Val & bitc::AllowReassoc))
1171 FMF.setAllowReassoc();
1172 if (0 != (Val & bitc::NoNaNs))
1173 FMF.setNoNaNs();
1174 if (0 != (Val & bitc::NoInfs))
1175 FMF.setNoInfs();
1176 if (0 != (Val & bitc::NoSignedZeros))
1177 FMF.setNoSignedZeros();
1178 if (0 != (Val & bitc::AllowReciprocal))
1179 FMF.setAllowReciprocal();
1180 if (0 != (Val & bitc::AllowContract))
1181 FMF.setAllowContract(true);
1182 if (0 != (Val & bitc::ApproxFunc))
1183 FMF.setApproxFunc();
1184 return FMF;
1185}
1186
1187static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1188 switch (Val) {
1189 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
1190 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
1191 }
1192}
1193
1194Type *BitcodeReader::getFullyStructuredTypeByID(unsigned ID) {
1195 // The type table size is always specified correctly.
1196 if (ID >= TypeList.size())
1197 return nullptr;
1198
1199 if (Type *Ty = TypeList[ID])
1200 return Ty;
1201
1202 // If we have a forward reference, the only possible case is when it is to a
1203 // named struct. Just create a placeholder for now.
1204 return TypeList[ID] = createIdentifiedStructType(Context);
1205}
1206
1207StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1208 StringRef Name) {
1209 auto *Ret = StructType::create(Context, Name);
1210 IdentifiedStructTypes.push_back(Ret);
1211 return Ret;
1212}
1213
1214StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1215 auto *Ret = StructType::create(Context);
1216 IdentifiedStructTypes.push_back(Ret);
1217 return Ret;
1218}
1219
1220//===----------------------------------------------------------------------===//
1221// Functions for parsing blocks from the bitcode file
1222//===----------------------------------------------------------------------===//
1223
1224static uint64_t getRawAttributeMask(Attribute::AttrKind Val) {
1225 switch (Val) {
1226 case Attribute::EndAttrKinds:
1227 case Attribute::EmptyKey:
1228 case Attribute::TombstoneKey:
1229 llvm_unreachable("Synthetic enumerators which should never get here")::llvm::llvm_unreachable_internal("Synthetic enumerators which should never get here"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1229)
;
1230
1231 case Attribute::None: return 0;
1232 case Attribute::ZExt: return 1 << 0;
1233 case Attribute::SExt: return 1 << 1;
1234 case Attribute::NoReturn: return 1 << 2;
1235 case Attribute::InReg: return 1 << 3;
1236 case Attribute::StructRet: return 1 << 4;
1237 case Attribute::NoUnwind: return 1 << 5;
1238 case Attribute::NoAlias: return 1 << 6;
1239 case Attribute::ByVal: return 1 << 7;
1240 case Attribute::Nest: return 1 << 8;
1241 case Attribute::ReadNone: return 1 << 9;
1242 case Attribute::ReadOnly: return 1 << 10;
1243 case Attribute::NoInline: return 1 << 11;
1244 case Attribute::AlwaysInline: return 1 << 12;
1245 case Attribute::OptimizeForSize: return 1 << 13;
1246 case Attribute::StackProtect: return 1 << 14;
1247 case Attribute::StackProtectReq: return 1 << 15;
1248 case Attribute::Alignment: return 31 << 16;
1249 case Attribute::NoCapture: return 1 << 21;
1250 case Attribute::NoRedZone: return 1 << 22;
1251 case Attribute::NoImplicitFloat: return 1 << 23;
1252 case Attribute::Naked: return 1 << 24;
1253 case Attribute::InlineHint: return 1 << 25;
1254 case Attribute::StackAlignment: return 7 << 26;
1255 case Attribute::ReturnsTwice: return 1 << 29;
1256 case Attribute::UWTable: return 1 << 30;
1257 case Attribute::NonLazyBind: return 1U << 31;
1258 case Attribute::SanitizeAddress: return 1ULL << 32;
1259 case Attribute::MinSize: return 1ULL << 33;
1260 case Attribute::NoDuplicate: return 1ULL << 34;
1261 case Attribute::StackProtectStrong: return 1ULL << 35;
1262 case Attribute::SanitizeThread: return 1ULL << 36;
1263 case Attribute::SanitizeMemory: return 1ULL << 37;
1264 case Attribute::NoBuiltin: return 1ULL << 38;
1265 case Attribute::Returned: return 1ULL << 39;
1266 case Attribute::Cold: return 1ULL << 40;
1267 case Attribute::Builtin: return 1ULL << 41;
1268 case Attribute::OptimizeNone: return 1ULL << 42;
1269 case Attribute::InAlloca: return 1ULL << 43;
1270 case Attribute::NonNull: return 1ULL << 44;
1271 case Attribute::JumpTable: return 1ULL << 45;
1272 case Attribute::Convergent: return 1ULL << 46;
1273 case Attribute::SafeStack: return 1ULL << 47;
1274 case Attribute::NoRecurse: return 1ULL << 48;
1275 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
1276 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
1277 case Attribute::SwiftSelf: return 1ULL << 51;
1278 case Attribute::SwiftError: return 1ULL << 52;
1279 case Attribute::WriteOnly: return 1ULL << 53;
1280 case Attribute::Speculatable: return 1ULL << 54;
1281 case Attribute::StrictFP: return 1ULL << 55;
1282 case Attribute::SanitizeHWAddress: return 1ULL << 56;
1283 case Attribute::NoCfCheck: return 1ULL << 57;
1284 case Attribute::OptForFuzzing: return 1ULL << 58;
1285 case Attribute::ShadowCallStack: return 1ULL << 59;
1286 case Attribute::SpeculativeLoadHardening:
1287 return 1ULL << 60;
1288 case Attribute::ImmArg:
1289 return 1ULL << 61;
1290 case Attribute::WillReturn:
1291 return 1ULL << 62;
1292 case Attribute::NoFree:
1293 return 1ULL << 63;
1294 default:
1295 // Other attributes are not supported in the raw format,
1296 // as we ran out of space.
1297 return 0;
1298 }
1299 llvm_unreachable("Unsupported attribute type")::llvm::llvm_unreachable_internal("Unsupported attribute type"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1299)
;
1300}
1301
1302static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) {
1303 if (!Val) return;
1304
1305 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1306 I = Attribute::AttrKind(I + 1)) {
1307 if (uint64_t A = (Val & getRawAttributeMask(I))) {
1308 if (I == Attribute::Alignment)
1309 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1310 else if (I == Attribute::StackAlignment)
1311 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1312 else
1313 B.addAttribute(I);
1314 }
1315 }
1316}
1317
1318/// This fills an AttrBuilder object with the LLVM attributes that have
1319/// been decoded from the given integer. This function must stay in sync with
1320/// 'encodeLLVMAttributesForBitcode'.
1321static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1322 uint64_t EncodedAttrs) {
1323 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1324 // the bits above 31 down by 11 bits.
1325 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1326 assert((!Alignment || isPowerOf2_32(Alignment)) &&(((!Alignment || isPowerOf2_32(Alignment)) && "Alignment must be a power of two."
) ? static_cast<void> (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1327, __PRETTY_FUNCTION__))
1327 "Alignment must be a power of two.")(((!Alignment || isPowerOf2_32(Alignment)) && "Alignment must be a power of two."
) ? static_cast<void> (0) : __assert_fail ("(!Alignment || isPowerOf2_32(Alignment)) && \"Alignment must be a power of two.\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1327, __PRETTY_FUNCTION__))
;
1328
1329 if (Alignment)
1330 B.addAlignmentAttr(Alignment);
1331 addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1332 (EncodedAttrs & 0xffff));
1333}
1334
1335Error BitcodeReader::parseAttributeBlock() {
1336 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1337 return Err;
1338
1339 if (!MAttributes.empty())
1340 return error("Invalid multiple blocks");
1341
1342 SmallVector<uint64_t, 64> Record;
1343
1344 SmallVector<AttributeList, 8> Attrs;
1345
1346 // Read all the records.
1347 while (true) {
1348 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1349 if (!MaybeEntry)
1350 return MaybeEntry.takeError();
1351 BitstreamEntry Entry = MaybeEntry.get();
1352
1353 switch (Entry.Kind) {
1354 case BitstreamEntry::SubBlock: // Handled for us already.
1355 case BitstreamEntry::Error:
1356 return error("Malformed block");
1357 case BitstreamEntry::EndBlock:
1358 return Error::success();
1359 case BitstreamEntry::Record:
1360 // The interesting case.
1361 break;
1362 }
1363
1364 // Read a record.
1365 Record.clear();
1366 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1367 if (!MaybeRecord)
1368 return MaybeRecord.takeError();
1369 switch (MaybeRecord.get()) {
1370 default: // Default behavior: ignore.
1371 break;
1372 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1373 // Deprecated, but still needed to read old bitcode files.
1374 if (Record.size() & 1)
1375 return error("Invalid record");
1376
1377 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1378 AttrBuilder B;
1379 decodeLLVMAttributesForBitcode(B, Record[i+1]);
1380 Attrs.push_back(AttributeList::get(Context, Record[i], B));
1381 }
1382
1383 MAttributes.push_back(AttributeList::get(Context, Attrs));
1384 Attrs.clear();
1385 break;
1386 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1387 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1388 Attrs.push_back(MAttributeGroups[Record[i]]);
1389
1390 MAttributes.push_back(AttributeList::get(Context, Attrs));
1391 Attrs.clear();
1392 break;
1393 }
1394 }
1395}
1396
1397// Returns Attribute::None on unrecognized codes.
1398static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
1399 switch (Code) {
1400 default:
1401 return Attribute::None;
1402 case bitc::ATTR_KIND_ALIGNMENT:
1403 return Attribute::Alignment;
1404 case bitc::ATTR_KIND_ALWAYS_INLINE:
1405 return Attribute::AlwaysInline;
1406 case bitc::ATTR_KIND_ARGMEMONLY:
1407 return Attribute::ArgMemOnly;
1408 case bitc::ATTR_KIND_BUILTIN:
1409 return Attribute::Builtin;
1410 case bitc::ATTR_KIND_BY_VAL:
1411 return Attribute::ByVal;
1412 case bitc::ATTR_KIND_IN_ALLOCA:
1413 return Attribute::InAlloca;
1414 case bitc::ATTR_KIND_COLD:
1415 return Attribute::Cold;
1416 case bitc::ATTR_KIND_CONVERGENT:
1417 return Attribute::Convergent;
1418 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
1419 return Attribute::InaccessibleMemOnly;
1420 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
1421 return Attribute::InaccessibleMemOrArgMemOnly;
1422 case bitc::ATTR_KIND_INLINE_HINT:
1423 return Attribute::InlineHint;
1424 case bitc::ATTR_KIND_IN_REG:
1425 return Attribute::InReg;
1426 case bitc::ATTR_KIND_JUMP_TABLE:
1427 return Attribute::JumpTable;
1428 case bitc::ATTR_KIND_MIN_SIZE:
1429 return Attribute::MinSize;
1430 case bitc::ATTR_KIND_NAKED:
1431 return Attribute::Naked;
1432 case bitc::ATTR_KIND_NEST:
1433 return Attribute::Nest;
1434 case bitc::ATTR_KIND_NO_ALIAS:
1435 return Attribute::NoAlias;
1436 case bitc::ATTR_KIND_NO_BUILTIN:
1437 return Attribute::NoBuiltin;
1438 case bitc::ATTR_KIND_NO_CAPTURE:
1439 return Attribute::NoCapture;
1440 case bitc::ATTR_KIND_NO_DUPLICATE:
1441 return Attribute::NoDuplicate;
1442 case bitc::ATTR_KIND_NOFREE:
1443 return Attribute::NoFree;
1444 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1445 return Attribute::NoImplicitFloat;
1446 case bitc::ATTR_KIND_NO_INLINE:
1447 return Attribute::NoInline;
1448 case bitc::ATTR_KIND_NO_RECURSE:
1449 return Attribute::NoRecurse;
1450 case bitc::ATTR_KIND_NO_MERGE:
1451 return Attribute::NoMerge;
1452 case bitc::ATTR_KIND_NON_LAZY_BIND:
1453 return Attribute::NonLazyBind;
1454 case bitc::ATTR_KIND_NON_NULL:
1455 return Attribute::NonNull;
1456 case bitc::ATTR_KIND_DEREFERENCEABLE:
1457 return Attribute::Dereferenceable;
1458 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1459 return Attribute::DereferenceableOrNull;
1460 case bitc::ATTR_KIND_ALLOC_SIZE:
1461 return Attribute::AllocSize;
1462 case bitc::ATTR_KIND_NO_RED_ZONE:
1463 return Attribute::NoRedZone;
1464 case bitc::ATTR_KIND_NO_RETURN:
1465 return Attribute::NoReturn;
1466 case bitc::ATTR_KIND_NOSYNC:
1467 return Attribute::NoSync;
1468 case bitc::ATTR_KIND_NOCF_CHECK:
1469 return Attribute::NoCfCheck;
1470 case bitc::ATTR_KIND_NO_UNWIND:
1471 return Attribute::NoUnwind;
1472 case bitc::ATTR_KIND_NULL_POINTER_IS_VALID:
1473 return Attribute::NullPointerIsValid;
1474 case bitc::ATTR_KIND_OPT_FOR_FUZZING:
1475 return Attribute::OptForFuzzing;
1476 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1477 return Attribute::OptimizeForSize;
1478 case bitc::ATTR_KIND_OPTIMIZE_NONE:
1479 return Attribute::OptimizeNone;
1480 case bitc::ATTR_KIND_READ_NONE:
1481 return Attribute::ReadNone;
1482 case bitc::ATTR_KIND_READ_ONLY:
1483 return Attribute::ReadOnly;
1484 case bitc::ATTR_KIND_RETURNED:
1485 return Attribute::Returned;
1486 case bitc::ATTR_KIND_RETURNS_TWICE:
1487 return Attribute::ReturnsTwice;
1488 case bitc::ATTR_KIND_S_EXT:
1489 return Attribute::SExt;
1490 case bitc::ATTR_KIND_SPECULATABLE:
1491 return Attribute::Speculatable;
1492 case bitc::ATTR_KIND_STACK_ALIGNMENT:
1493 return Attribute::StackAlignment;
1494 case bitc::ATTR_KIND_STACK_PROTECT:
1495 return Attribute::StackProtect;
1496 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1497 return Attribute::StackProtectReq;
1498 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1499 return Attribute::StackProtectStrong;
1500 case bitc::ATTR_KIND_SAFESTACK:
1501 return Attribute::SafeStack;
1502 case bitc::ATTR_KIND_SHADOWCALLSTACK:
1503 return Attribute::ShadowCallStack;
1504 case bitc::ATTR_KIND_STRICT_FP:
1505 return Attribute::StrictFP;
1506 case bitc::ATTR_KIND_STRUCT_RET:
1507 return Attribute::StructRet;
1508 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1509 return Attribute::SanitizeAddress;
1510 case bitc::ATTR_KIND_SANITIZE_HWADDRESS:
1511 return Attribute::SanitizeHWAddress;
1512 case bitc::ATTR_KIND_SANITIZE_THREAD:
1513 return Attribute::SanitizeThread;
1514 case bitc::ATTR_KIND_SANITIZE_MEMORY:
1515 return Attribute::SanitizeMemory;
1516 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
1517 return Attribute::SpeculativeLoadHardening;
1518 case bitc::ATTR_KIND_SWIFT_ERROR:
1519 return Attribute::SwiftError;
1520 case bitc::ATTR_KIND_SWIFT_SELF:
1521 return Attribute::SwiftSelf;
1522 case bitc::ATTR_KIND_UW_TABLE:
1523 return Attribute::UWTable;
1524 case bitc::ATTR_KIND_WILLRETURN:
1525 return Attribute::WillReturn;
1526 case bitc::ATTR_KIND_WRITEONLY:
1527 return Attribute::WriteOnly;
1528 case bitc::ATTR_KIND_Z_EXT:
1529 return Attribute::ZExt;
1530 case bitc::ATTR_KIND_IMMARG:
1531 return Attribute::ImmArg;
1532 case bitc::ATTR_KIND_SANITIZE_MEMTAG:
1533 return Attribute::SanitizeMemTag;
1534 case bitc::ATTR_KIND_PREALLOCATED:
1535 return Attribute::Preallocated;
1536 case bitc::ATTR_KIND_NOUNDEF:
1537 return Attribute::NoUndef;
1538 case bitc::ATTR_KIND_BYREF:
1539 return Attribute::ByRef;
1540 }
1541}
1542
1543Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1544 MaybeAlign &Alignment) {
1545 // Note: Alignment in bitcode files is incremented by 1, so that zero
1546 // can be used for default alignment.
1547 if (Exponent > Value::MaxAlignmentExponent + 1)
1548 return error("Invalid alignment value");
1549 Alignment = decodeMaybeAlign(Exponent);
1550 return Error::success();
1551}
1552
1553Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
1554 *Kind = getAttrFromCode(Code);
1555 if (*Kind == Attribute::None)
1556 return error("Unknown attribute kind (" + Twine(Code) + ")");
1557 return Error::success();
1558}
1559
1560Error BitcodeReader::parseAttributeGroupBlock() {
1561 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1562 return Err;
1563
1564 if (!MAttributeGroups.empty())
1565 return error("Invalid multiple blocks");
1566
1567 SmallVector<uint64_t, 64> Record;
1568
1569 // Read all the records.
1570 while (true) {
1571 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1572 if (!MaybeEntry)
1573 return MaybeEntry.takeError();
1574 BitstreamEntry Entry = MaybeEntry.get();
1575
1576 switch (Entry.Kind) {
1577 case BitstreamEntry::SubBlock: // Handled for us already.
1578 case BitstreamEntry::Error:
1579 return error("Malformed block");
1580 case BitstreamEntry::EndBlock:
1581 return Error::success();
1582 case BitstreamEntry::Record:
1583 // The interesting case.
1584 break;
1585 }
1586
1587 // Read a record.
1588 Record.clear();
1589 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1590 if (!MaybeRecord)
1591 return MaybeRecord.takeError();
1592 switch (MaybeRecord.get()) {
1593 default: // Default behavior: ignore.
1594 break;
1595 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1596 if (Record.size() < 3)
1597 return error("Invalid record");
1598
1599 uint64_t GrpID = Record[0];
1600 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1601
1602 AttrBuilder B;
1603 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1604 if (Record[i] == 0) { // Enum attribute
1605 Attribute::AttrKind Kind;
1606 if (Error Err = parseAttrKind(Record[++i], &Kind))
1607 return Err;
1608
1609 // Upgrade old-style byval attribute to one with a type, even if it's
1610 // nullptr. We will have to insert the real type when we associate
1611 // this AttributeList with a function.
1612 if (Kind == Attribute::ByVal)
1613 B.addByValAttr(nullptr);
1614 else if (Kind == Attribute::StructRet)
1615 B.addStructRetAttr(nullptr);
1616
1617 B.addAttribute(Kind);
1618 } else if (Record[i] == 1) { // Integer attribute
1619 Attribute::AttrKind Kind;
1620 if (Error Err = parseAttrKind(Record[++i], &Kind))
1621 return Err;
1622 if (Kind == Attribute::Alignment)
1623 B.addAlignmentAttr(Record[++i]);
1624 else if (Kind == Attribute::StackAlignment)
1625 B.addStackAlignmentAttr(Record[++i]);
1626 else if (Kind == Attribute::Dereferenceable)
1627 B.addDereferenceableAttr(Record[++i]);
1628 else if (Kind == Attribute::DereferenceableOrNull)
1629 B.addDereferenceableOrNullAttr(Record[++i]);
1630 else if (Kind == Attribute::AllocSize)
1631 B.addAllocSizeAttrFromRawRepr(Record[++i]);
1632 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute
1633 bool HasValue = (Record[i++] == 4);
1634 SmallString<64> KindStr;
1635 SmallString<64> ValStr;
1636
1637 while (Record[i] != 0 && i != e)
1638 KindStr += Record[i++];
1639 assert(Record[i] == 0 && "Kind string not null terminated")((Record[i] == 0 && "Kind string not null terminated"
) ? static_cast<void> (0) : __assert_fail ("Record[i] == 0 && \"Kind string not null terminated\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1639, __PRETTY_FUNCTION__))
;
1640
1641 if (HasValue) {
1642 // Has a value associated with it.
1643 ++i; // Skip the '0' that terminates the "kind" string.
1644 while (Record[i] != 0 && i != e)
1645 ValStr += Record[i++];
1646 assert(Record[i] == 0 && "Value string not null terminated")((Record[i] == 0 && "Value string not null terminated"
) ? static_cast<void> (0) : __assert_fail ("Record[i] == 0 && \"Value string not null terminated\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1646, __PRETTY_FUNCTION__))
;
1647 }
1648
1649 B.addAttribute(KindStr.str(), ValStr.str());
1650 } else {
1651 assert((Record[i] == 5 || Record[i] == 6) &&(((Record[i] == 5 || Record[i] == 6) && "Invalid attribute group entry"
) ? static_cast<void> (0) : __assert_fail ("(Record[i] == 5 || Record[i] == 6) && \"Invalid attribute group entry\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1652, __PRETTY_FUNCTION__))
1652 "Invalid attribute group entry")(((Record[i] == 5 || Record[i] == 6) && "Invalid attribute group entry"
) ? static_cast<void> (0) : __assert_fail ("(Record[i] == 5 || Record[i] == 6) && \"Invalid attribute group entry\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1652, __PRETTY_FUNCTION__))
;
1653 bool HasType = Record[i] == 6;
1654 Attribute::AttrKind Kind;
1655 if (Error Err = parseAttrKind(Record[++i], &Kind))
1656 return Err;
1657 if (Kind == Attribute::ByVal) {
1658 B.addByValAttr(HasType ? getTypeByID(Record[++i]) : nullptr);
1659 } else if (Kind == Attribute::StructRet) {
1660 B.addStructRetAttr(HasType ? getTypeByID(Record[++i]) : nullptr);
1661 } else if (Kind == Attribute::ByRef) {
1662 B.addByRefAttr(getTypeByID(Record[++i]));
1663 } else if (Kind == Attribute::Preallocated) {
1664 B.addPreallocatedAttr(getTypeByID(Record[++i]));
1665 }
1666 }
1667 }
1668
1669 UpgradeAttributes(B);
1670 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
1671 break;
1672 }
1673 }
1674 }
1675}
1676
1677Error BitcodeReader::parseTypeTable() {
1678 if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1679 return Err;
1680
1681 return parseTypeTableBody();
1682}
1683
1684Error BitcodeReader::parseTypeTableBody() {
1685 if (!TypeList.empty())
1686 return error("Invalid multiple blocks");
1687
1688 SmallVector<uint64_t, 64> Record;
1689 unsigned NumRecords = 0;
1690
1691 SmallString<64> TypeName;
1692
1693 // Read all the records for this type table.
1694 while (true) {
1695 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1696 if (!MaybeEntry)
1697 return MaybeEntry.takeError();
1698 BitstreamEntry Entry = MaybeEntry.get();
1699
1700 switch (Entry.Kind) {
1701 case BitstreamEntry::SubBlock: // Handled for us already.
1702 case BitstreamEntry::Error:
1703 return error("Malformed block");
1704 case BitstreamEntry::EndBlock:
1705 if (NumRecords != TypeList.size())
1706 return error("Malformed block");
1707 return Error::success();
1708 case BitstreamEntry::Record:
1709 // The interesting case.
1710 break;
1711 }
1712
1713 // Read a record.
1714 Record.clear();
1715 Type *ResultTy = nullptr;
1716 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1717 if (!MaybeRecord)
1718 return MaybeRecord.takeError();
1719 switch (MaybeRecord.get()) {
1720 default:
1721 return error("Invalid value");
1722 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1723 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1724 // type list. This allows us to reserve space.
1725 if (Record.empty())
1726 return error("Invalid record");
1727 TypeList.resize(Record[0]);
1728 continue;
1729 case bitc::TYPE_CODE_VOID: // VOID
1730 ResultTy = Type::getVoidTy(Context);
1731 break;
1732 case bitc::TYPE_CODE_HALF: // HALF
1733 ResultTy = Type::getHalfTy(Context);
1734 break;
1735 case bitc::TYPE_CODE_BFLOAT: // BFLOAT
1736 ResultTy = Type::getBFloatTy(Context);
1737 break;
1738 case bitc::TYPE_CODE_FLOAT: // FLOAT
1739 ResultTy = Type::getFloatTy(Context);
1740 break;
1741 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
1742 ResultTy = Type::getDoubleTy(Context);
1743 break;
1744 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
1745 ResultTy = Type::getX86_FP80Ty(Context);
1746 break;
1747 case bitc::TYPE_CODE_FP128: // FP128
1748 ResultTy = Type::getFP128Ty(Context);
1749 break;
1750 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1751 ResultTy = Type::getPPC_FP128Ty(Context);
1752 break;
1753 case bitc::TYPE_CODE_LABEL: // LABEL
1754 ResultTy = Type::getLabelTy(Context);
1755 break;
1756 case bitc::TYPE_CODE_METADATA: // METADATA
1757 ResultTy = Type::getMetadataTy(Context);
1758 break;
1759 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1760 ResultTy = Type::getX86_MMXTy(Context);
1761 break;
1762 case bitc::TYPE_CODE_TOKEN: // TOKEN
1763 ResultTy = Type::getTokenTy(Context);
1764 break;
1765 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1766 if (Record.empty())
1767 return error("Invalid record");
1768
1769 uint64_t NumBits = Record[0];
1770 if (NumBits < IntegerType::MIN_INT_BITS ||
1771 NumBits > IntegerType::MAX_INT_BITS)
1772 return error("Bitwidth for integer type out of range");
1773 ResultTy = IntegerType::get(Context, NumBits);
1774 break;
1775 }
1776 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1777 // [pointee type, address space]
1778 if (Record.empty())
1779 return error("Invalid record");
1780 unsigned AddressSpace = 0;
1781 if (Record.size() == 2)
1782 AddressSpace = Record[1];
1783 ResultTy = getTypeByID(Record[0]);
1784 if (!ResultTy ||
1785 !PointerType::isValidElementType(ResultTy))
1786 return error("Invalid type");
1787 ResultTy = PointerType::get(ResultTy, AddressSpace);
1788 break;
1789 }
1790 case bitc::TYPE_CODE_FUNCTION_OLD: {
1791 // Deprecated, but still needed to read old bitcode files.
1792 // FUNCTION: [vararg, attrid, retty, paramty x N]
1793 if (Record.size() < 3)
1794 return error("Invalid record");
1795 SmallVector<Type*, 8> ArgTys;
1796 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1797 if (Type *T = getTypeByID(Record[i]))
1798 ArgTys.push_back(T);
1799 else
1800 break;
1801 }
1802
1803 ResultTy = getTypeByID(Record[2]);
1804 if (!ResultTy || ArgTys.size() < Record.size()-3)
1805 return error("Invalid type");
1806
1807 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1808 break;
1809 }
1810 case bitc::TYPE_CODE_FUNCTION: {
1811 // FUNCTION: [vararg, retty, paramty x N]
1812 if (Record.size() < 2)
1813 return error("Invalid record");
1814 SmallVector<Type*, 8> ArgTys;
1815 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1816 if (Type *T = getTypeByID(Record[i])) {
1817 if (!FunctionType::isValidArgumentType(T))
1818 return error("Invalid function argument type");
1819 ArgTys.push_back(T);
1820 }
1821 else
1822 break;
1823 }
1824
1825 ResultTy = getTypeByID(Record[1]);
1826 if (!ResultTy || ArgTys.size() < Record.size()-2)
1827 return error("Invalid type");
1828
1829 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1830 break;
1831 }
1832 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
1833 if (Record.empty())
1834 return error("Invalid record");
1835 SmallVector<Type*, 8> EltTys;
1836 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1837 if (Type *T = getTypeByID(Record[i]))
1838 EltTys.push_back(T);
1839 else
1840 break;
1841 }
1842 if (EltTys.size() != Record.size()-1)
1843 return error("Invalid type");
1844 ResultTy = StructType::get(Context, EltTys, Record[0]);
1845 break;
1846 }
1847 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
1848 if (convertToString(Record, 0, TypeName))
1849 return error("Invalid record");
1850 continue;
1851
1852 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1853 if (Record.empty())
1854 return error("Invalid record");
1855
1856 if (NumRecords >= TypeList.size())
1857 return error("Invalid TYPE table");
1858
1859 // Check to see if this was forward referenced, if so fill in the temp.
1860 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1861 if (Res) {
1862 Res->setName(TypeName);
1863 TypeList[NumRecords] = nullptr;
1864 } else // Otherwise, create a new struct.
1865 Res = createIdentifiedStructType(Context, TypeName);
1866 TypeName.clear();
1867
1868 SmallVector<Type*, 8> EltTys;
1869 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1870 if (Type *T = getTypeByID(Record[i]))
1871 EltTys.push_back(T);
1872 else
1873 break;
1874 }
1875 if (EltTys.size() != Record.size()-1)
1876 return error("Invalid record");
1877 Res->setBody(EltTys, Record[0]);
1878 ResultTy = Res;
1879 break;
1880 }
1881 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
1882 if (Record.size() != 1)
1883 return error("Invalid record");
1884
1885 if (NumRecords >= TypeList.size())
1886 return error("Invalid TYPE table");
1887
1888 // Check to see if this was forward referenced, if so fill in the temp.
1889 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1890 if (Res) {
1891 Res->setName(TypeName);
1892 TypeList[NumRecords] = nullptr;
1893 } else // Otherwise, create a new struct with no body.
1894 Res = createIdentifiedStructType(Context, TypeName);
1895 TypeName.clear();
1896 ResultTy = Res;
1897 break;
1898 }
1899 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
1900 if (Record.size() < 2)
1901 return error("Invalid record");
1902 ResultTy = getTypeByID(Record[1]);
1903 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
1904 return error("Invalid type");
1905 ResultTy = ArrayType::get(ResultTy, Record[0]);
1906 break;
1907 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or
1908 // [numelts, eltty, scalable]
1909 if (Record.size() < 2)
1910 return error("Invalid record");
1911 if (Record[0] == 0)
1912 return error("Invalid vector length");
1913 ResultTy = getTypeByID(Record[1]);
1914 if (!ResultTy || !StructType::isValidElementType(ResultTy))
1915 return error("Invalid type");
1916 bool Scalable = Record.size() > 2 ? Record[2] : false;
1917 ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
1918 break;
1919 }
1920
1921 if (NumRecords >= TypeList.size())
1922 return error("Invalid TYPE table");
1923 if (TypeList[NumRecords])
1924 return error(
1925 "Invalid TYPE table: Only named structs can be forward referenced");
1926 assert(ResultTy && "Didn't read a type?")((ResultTy && "Didn't read a type?") ? static_cast<
void> (0) : __assert_fail ("ResultTy && \"Didn't read a type?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 1926, __PRETTY_FUNCTION__))
;
1927 TypeList[NumRecords++] = ResultTy;
1928 }
1929}
1930
1931Error BitcodeReader::parseOperandBundleTags() {
1932 if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1933 return Err;
1934
1935 if (!BundleTags.empty())
1936 return error("Invalid multiple blocks");
1937
1938 SmallVector<uint64_t, 64> Record;
1939
1940 while (true) {
1941 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1942 if (!MaybeEntry)
1943 return MaybeEntry.takeError();
1944 BitstreamEntry Entry = MaybeEntry.get();
1945
1946 switch (Entry.Kind) {
1947 case BitstreamEntry::SubBlock: // Handled for us already.
1948 case BitstreamEntry::Error:
1949 return error("Malformed block");
1950 case BitstreamEntry::EndBlock:
1951 return Error::success();
1952 case BitstreamEntry::Record:
1953 // The interesting case.
1954 break;
1955 }
1956
1957 // Tags are implicitly mapped to integers by their order.
1958
1959 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1960 if (!MaybeRecord)
1961 return MaybeRecord.takeError();
1962 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
1963 return error("Invalid record");
1964
1965 // OPERAND_BUNDLE_TAG: [strchr x N]
1966 BundleTags.emplace_back();
1967 if (convertToString(Record, 0, BundleTags.back()))
1968 return error("Invalid record");
1969 Record.clear();
1970 }
1971}
1972
1973Error BitcodeReader::parseSyncScopeNames() {
1974 if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
1975 return Err;
1976
1977 if (!SSIDs.empty())
1978 return error("Invalid multiple synchronization scope names blocks");
1979
1980 SmallVector<uint64_t, 64> Record;
1981 while (true) {
1982 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1983 if (!MaybeEntry)
1984 return MaybeEntry.takeError();
1985 BitstreamEntry Entry = MaybeEntry.get();
1986
1987 switch (Entry.Kind) {
1988 case BitstreamEntry::SubBlock: // Handled for us already.
1989 case BitstreamEntry::Error:
1990 return error("Malformed block");
1991 case BitstreamEntry::EndBlock:
1992 if (SSIDs.empty())
1993 return error("Invalid empty synchronization scope names block");
1994 return Error::success();
1995 case BitstreamEntry::Record:
1996 // The interesting case.
1997 break;
1998 }
1999
2000 // Synchronization scope names are implicitly mapped to synchronization
2001 // scope IDs by their order.
2002
2003 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2004 if (!MaybeRecord)
2005 return MaybeRecord.takeError();
2006 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
2007 return error("Invalid record");
2008
2009 SmallString<16> SSN;
2010 if (convertToString(Record, 0, SSN))
2011 return error("Invalid record");
2012
2013 SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN));
2014 Record.clear();
2015 }
2016}
2017
2018/// Associate a value with its name from the given index in the provided record.
2019Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2020 unsigned NameIndex, Triple &TT) {
2021 SmallString<128> ValueName;
2022 if (convertToString(Record, NameIndex, ValueName))
2023 return error("Invalid record");
2024 unsigned ValueID = Record[0];
2025 if (ValueID >= ValueList.size() || !ValueList[ValueID])
2026 return error("Invalid record");
2027 Value *V = ValueList[ValueID];
2028
2029 StringRef NameStr(ValueName.data(), ValueName.size());
2030 if (NameStr.find_first_of(0) != StringRef::npos)
2031 return error("Invalid value name");
2032 V->setName(NameStr);
2033 auto *GO = dyn_cast<GlobalObject>(V);
2034 if (GO) {
2035 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
2036 if (TT.supportsCOMDAT())
2037 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
2038 else
2039 GO->setComdat(nullptr);
2040 }
2041 }
2042 return V;
2043}
2044
2045/// Helper to note and return the current location, and jump to the given
2046/// offset.
2047static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset,
2048 BitstreamCursor &Stream) {
2049 // Save the current parsing location so we can jump back at the end
2050 // of the VST read.
2051 uint64_t CurrentBit = Stream.GetCurrentBitNo();
2052 if (Error JumpFailed = Stream.JumpToBit(Offset * 32))
2053 return std::move(JumpFailed);
2054 Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2055 if (!MaybeEntry)
2056 return MaybeEntry.takeError();
2057 assert(MaybeEntry.get().Kind == BitstreamEntry::SubBlock)((MaybeEntry.get().Kind == BitstreamEntry::SubBlock) ? static_cast
<void> (0) : __assert_fail ("MaybeEntry.get().Kind == BitstreamEntry::SubBlock"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 2057, __PRETTY_FUNCTION__))
;
2058 assert(MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID)((MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID) ? static_cast
<void> (0) : __assert_fail ("MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 2058, __PRETTY_FUNCTION__))
;
2059 return CurrentBit;
2060}
2061
2062void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2063 Function *F,
2064 ArrayRef<uint64_t> Record) {
2065 // Note that we subtract 1 here because the offset is relative to one word
2066 // before the start of the identification or module block, which was
2067 // historically always the start of the regular bitcode header.
2068 uint64_t FuncWordOffset = Record[1] - 1;
2069 uint64_t FuncBitOffset = FuncWordOffset * 32;
2070 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2071 // Set the LastFunctionBlockBit to point to the last function block.
2072 // Later when parsing is resumed after function materialization,
2073 // we can simply skip that last function block.
2074 if (FuncBitOffset > LastFunctionBlockBit)
2075 LastFunctionBlockBit = FuncBitOffset;
2076}
2077
2078/// Read a new-style GlobalValue symbol table.
2079Error BitcodeReader::parseGlobalValueSymbolTable() {
2080 unsigned FuncBitcodeOffsetDelta =
2081 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2082
2083 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2084 return Err;
2085
2086 SmallVector<uint64_t, 64> Record;
2087 while (true) {
2088 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2089 if (!MaybeEntry)
2090 return MaybeEntry.takeError();
2091 BitstreamEntry Entry = MaybeEntry.get();
2092
2093 switch (Entry.Kind) {
2094 case BitstreamEntry::SubBlock:
2095 case BitstreamEntry::Error:
2096 return error("Malformed block");
2097 case BitstreamEntry::EndBlock:
2098 return Error::success();
2099 case BitstreamEntry::Record:
2100 break;
2101 }
2102
2103 Record.clear();
2104 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2105 if (!MaybeRecord)
2106 return MaybeRecord.takeError();
2107 switch (MaybeRecord.get()) {
2108 case bitc::VST_CODE_FNENTRY: // [valueid, offset]
2109 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
2110 cast<Function>(ValueList[Record[0]]), Record);
2111 break;
2112 }
2113 }
2114}
2115
2116/// Parse the value symbol table at either the current parsing location or
2117/// at the given bit offset if provided.
2118Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
2119 uint64_t CurrentBit;
2120 // Pass in the Offset to distinguish between calling for the module-level
2121 // VST (where we want to jump to the VST offset) and the function-level
2122 // VST (where we don't).
2123 if (Offset > 0) {
2124 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
2125 if (!MaybeCurrentBit)
2126 return MaybeCurrentBit.takeError();
2127 CurrentBit = MaybeCurrentBit.get();
2128 // If this module uses a string table, read this as a module-level VST.
2129 if (UseStrtab) {
2130 if (Error Err = parseGlobalValueSymbolTable())
2131 return Err;
2132 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2133 return JumpFailed;
2134 return Error::success();
2135 }
2136 // Otherwise, the VST will be in a similar format to a function-level VST,
2137 // and will contain symbol names.
2138 }
2139
2140 // Compute the delta between the bitcode indices in the VST (the word offset
2141 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2142 // expected by the lazy reader. The reader's EnterSubBlock expects to have
2143 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2144 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2145 // just before entering the VST subblock because: 1) the EnterSubBlock
2146 // changes the AbbrevID width; 2) the VST block is nested within the same
2147 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2148 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2149 // jump to the FUNCTION_BLOCK using this offset later, we don't want
2150 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2151 unsigned FuncBitcodeOffsetDelta =
2152 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2153
2154 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2155 return Err;
2156
2157 SmallVector<uint64_t, 64> Record;
2158
2159 Triple TT(TheModule->getTargetTriple());
2160
2161 // Read all the records for this value table.
2162 SmallString<128> ValueName;
2163
2164 while (true) {
2165 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2166 if (!MaybeEntry)
2167 return MaybeEntry.takeError();
2168 BitstreamEntry Entry = MaybeEntry.get();
2169
2170 switch (Entry.Kind) {
2171 case BitstreamEntry::SubBlock: // Handled for us already.
2172 case BitstreamEntry::Error:
2173 return error("Malformed block");
2174 case BitstreamEntry::EndBlock:
2175 if (Offset > 0)
2176 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2177 return JumpFailed;
2178 return Error::success();
2179 case BitstreamEntry::Record:
2180 // The interesting case.
2181 break;
2182 }
2183
2184 // Read a record.
2185 Record.clear();
2186 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2187 if (!MaybeRecord)
2188 return MaybeRecord.takeError();
2189 switch (MaybeRecord.get()) {
2190 default: // Default behavior: unknown type.
2191 break;
2192 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
2193 Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2194 if (Error Err = ValOrErr.takeError())
2195 return Err;
2196 ValOrErr.get();
2197 break;
2198 }
2199 case bitc::VST_CODE_FNENTRY: {
2200 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2201 Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2202 if (Error Err = ValOrErr.takeError())
2203 return Err;
2204 Value *V = ValOrErr.get();
2205
2206 // Ignore function offsets emitted for aliases of functions in older
2207 // versions of LLVM.
2208 if (auto *F = dyn_cast<Function>(V))
2209 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2210 break;
2211 }
2212 case bitc::VST_CODE_BBENTRY: {
2213 if (convertToString(Record, 1, ValueName))
2214 return error("Invalid record");
2215 BasicBlock *BB = getBasicBlock(Record[0]);
2216 if (!BB)
2217 return error("Invalid record");
2218
2219 BB->setName(StringRef(ValueName.data(), ValueName.size()));
2220 ValueName.clear();
2221 break;
2222 }
2223 }
2224 }
2225}
2226
2227/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2228/// encoding.
2229uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2230 if ((V & 1) == 0)
2231 return V >> 1;
2232 if (V != 1)
2233 return -(V >> 1);
2234 // There is no such thing as -0 with integers. "-0" really means MININT.
2235 return 1ULL << 63;
2236}
2237
2238/// Resolve all of the initializers for global values and aliases that we can.
2239Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2240 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2241 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>
2242 IndirectSymbolInitWorklist;
2243 std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist;
2244 std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist;
2245 std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist;
2246
2247 GlobalInitWorklist.swap(GlobalInits);
2248 IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2249 FunctionPrefixWorklist.swap(FunctionPrefixes);
2250 FunctionPrologueWorklist.swap(FunctionPrologues);
2251 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
2252
2253 while (!GlobalInitWorklist.empty()) {
2254 unsigned ValID = GlobalInitWorklist.back().second;
2255 if (ValID >= ValueList.size()) {
2256 // Not ready to resolve this yet, it requires something later in the file.
2257 GlobalInits.push_back(GlobalInitWorklist.back());
2258 } else {
2259 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2260 GlobalInitWorklist.back().first->setInitializer(C);
2261 else
2262 return error("Expected a constant");
2263 }
2264 GlobalInitWorklist.pop_back();
2265 }
2266
2267 while (!IndirectSymbolInitWorklist.empty()) {
2268 unsigned ValID = IndirectSymbolInitWorklist.back().second;
2269 if (ValID >= ValueList.size()) {
2270 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2271 } else {
2272 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2273 if (!C)
2274 return error("Expected a constant");
2275 GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first;
2276 if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType())
2277 return error("Alias and aliasee types don't match");
2278 GIS->setIndirectSymbol(C);
2279 }
2280 IndirectSymbolInitWorklist.pop_back();
2281 }
2282
2283 while (!FunctionPrefixWorklist.empty()) {
2284 unsigned ValID = FunctionPrefixWorklist.back().second;
2285 if (ValID >= ValueList.size()) {
2286 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2287 } else {
2288 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2289 FunctionPrefixWorklist.back().first->setPrefixData(C);
2290 else
2291 return error("Expected a constant");
2292 }
2293 FunctionPrefixWorklist.pop_back();
2294 }
2295
2296 while (!FunctionPrologueWorklist.empty()) {
2297 unsigned ValID = FunctionPrologueWorklist.back().second;
2298 if (ValID >= ValueList.size()) {
2299 FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2300 } else {
2301 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2302 FunctionPrologueWorklist.back().first->setPrologueData(C);
2303 else
2304 return error("Expected a constant");
2305 }
2306 FunctionPrologueWorklist.pop_back();
2307 }
2308
2309 while (!FunctionPersonalityFnWorklist.empty()) {
2310 unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2311 if (ValID >= ValueList.size()) {
2312 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2313 } else {
2314 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2315 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2316 else
2317 return error("Expected a constant");
2318 }
2319 FunctionPersonalityFnWorklist.pop_back();
2320 }
2321
2322 return Error::success();
2323}
2324
2325APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
2326 SmallVector<uint64_t, 8> Words(Vals.size());
2327 transform(Vals, Words.begin(),
2328 BitcodeReader::decodeSignRotatedValue);
2329
2330 return APInt(TypeBits, Words);
2331}
2332
2333Error BitcodeReader::parseConstants() {
2334 if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2335 return Err;
2336
2337 SmallVector<uint64_t, 64> Record;
2338
2339 // Read all the records for this value table.
2340 Type *CurTy = Type::getInt32Ty(Context);
2341 Type *CurFullTy = Type::getInt32Ty(Context);
2342 unsigned NextCstNo = ValueList.size();
2343
2344 struct DelayedShufTy {
2345 VectorType *OpTy;
2346 VectorType *RTy;
2347 Type *CurFullTy;
2348 uint64_t Op0Idx;
2349 uint64_t Op1Idx;
2350 uint64_t Op2Idx;
2351 unsigned CstNo;
2352 };
2353 std::vector<DelayedShufTy> DelayedShuffles;
2354 while (true) {
2355 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2356 if (!MaybeEntry)
2357 return MaybeEntry.takeError();
2358 BitstreamEntry Entry = MaybeEntry.get();
2359
2360 switch (Entry.Kind) {
2361 case BitstreamEntry::SubBlock: // Handled for us already.
2362 case BitstreamEntry::Error:
2363 return error("Malformed block");
2364 case BitstreamEntry::EndBlock:
2365 // Once all the constants have been read, go through and resolve forward
2366 // references.
2367 //
2368 // We have to treat shuffles specially because they don't have three
2369 // operands anymore. We need to convert the shuffle mask into an array,
2370 // and we can't convert a forward reference.
2371 for (auto &DelayedShuffle : DelayedShuffles) {
2372 VectorType *OpTy = DelayedShuffle.OpTy;
2373 VectorType *RTy = DelayedShuffle.RTy;
2374 uint64_t Op0Idx = DelayedShuffle.Op0Idx;
2375 uint64_t Op1Idx = DelayedShuffle.Op1Idx;
2376 uint64_t Op2Idx = DelayedShuffle.Op2Idx;
2377 uint64_t CstNo = DelayedShuffle.CstNo;
2378 Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, OpTy);
2379 Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy);
2380 Type *ShufTy =
2381 VectorType::get(Type::getInt32Ty(Context), RTy->getElementCount());
2382 Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, ShufTy);
2383 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
2384 return error("Invalid shufflevector operands");
2385 SmallVector<int, 16> Mask;
2386 ShuffleVectorInst::getShuffleMask(Op2, Mask);
2387 Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask);
2388 ValueList.assignValue(V, CstNo, DelayedShuffle.CurFullTy);
2389 }
2390
2391 if (NextCstNo != ValueList.size())
2392 return error("Invalid constant reference");
2393
2394 ValueList.resolveConstantForwardRefs();
2395 return Error::success();
2396 case BitstreamEntry::Record:
2397 // The interesting case.
2398 break;
2399 }
2400
2401 // Read a record.
2402 Record.clear();
2403 Type *VoidType = Type::getVoidTy(Context);
2404 Value *V = nullptr;
2405 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
2406 if (!MaybeBitCode)
2407 return MaybeBitCode.takeError();
2408 switch (unsigned BitCode = MaybeBitCode.get()) {
2409 default: // Default behavior: unknown constant
2410 case bitc::CST_CODE_UNDEF: // UNDEF
2411 V = UndefValue::get(CurTy);
2412 break;
2413 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2414 if (Record.empty())
2415 return error("Invalid record");
2416 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2417 return error("Invalid record");
2418 if (TypeList[Record[0]] == VoidType)
2419 return error("Invalid constant type");
2420 CurFullTy = TypeList[Record[0]];
2421 CurTy = flattenPointerTypes(CurFullTy);
2422 continue; // Skip the ValueList manipulation.
2423 case bitc::CST_CODE_NULL: // NULL
2424 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy())
2425 return error("Invalid type for a constant null value");
2426 V = Constant::getNullValue(CurTy);
2427 break;
2428 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
2429 if (!CurTy->isIntegerTy() || Record.empty())
2430 return error("Invalid record");
2431 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
2432 break;
2433 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2434 if (!CurTy->isIntegerTy() || Record.empty())
2435 return error("Invalid record");
2436
2437 APInt VInt =
2438 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2439 V = ConstantInt::get(Context, VInt);
2440
2441 break;
2442 }
2443 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
2444 if (Record.empty())
2445 return error("Invalid record");
2446 if (CurTy->isHalfTy())
2447 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(),
2448 APInt(16, (uint16_t)Record[0])));
2449 else if (CurTy->isBFloatTy())
2450 V = ConstantFP::get(Context, APFloat(APFloat::BFloat(),
2451 APInt(16, (uint32_t)Record[0])));
2452 else if (CurTy->isFloatTy())
2453 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(),
2454 APInt(32, (uint32_t)Record[0])));
2455 else if (CurTy->isDoubleTy())
2456 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(),
2457 APInt(64, Record[0])));
2458 else if (CurTy->isX86_FP80Ty()) {
2459 // Bits are not stored the same way as a normal i80 APInt, compensate.
2460 uint64_t Rearrange[2];
2461 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2462 Rearrange[1] = Record[0] >> 48;
2463 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(),
2464 APInt(80, Rearrange)));
2465 } else if (CurTy->isFP128Ty())
2466 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(),
2467 APInt(128, Record)));
2468 else if (CurTy->isPPC_FP128Ty())
2469 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(),
2470 APInt(128, Record)));
2471 else
2472 V = UndefValue::get(CurTy);
2473 break;
2474 }
2475
2476 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2477 if (Record.empty())
2478 return error("Invalid record");
2479
2480 unsigned Size = Record.size();
2481 SmallVector<Constant*, 16> Elts;
2482
2483 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2484 for (unsigned i = 0; i != Size; ++i)
2485 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
2486 STy->getElementType(i)));
2487 V = ConstantStruct::get(STy, Elts);
2488 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2489 Type *EltTy = ATy->getElementType();
2490 for (unsigned i = 0; i != Size; ++i)
2491 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2492 V = ConstantArray::get(ATy, Elts);
2493 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2494 Type *EltTy = VTy->getElementType();
2495 for (unsigned i = 0; i != Size; ++i)
2496 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2497 V = ConstantVector::get(Elts);
2498 } else {
2499 V = UndefValue::get(CurTy);
2500 }
2501 break;
2502 }
2503 case bitc::CST_CODE_STRING: // STRING: [values]
2504 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2505 if (Record.empty())
2506 return error("Invalid record");
2507
2508 SmallString<16> Elts(Record.begin(), Record.end());
2509 V = ConstantDataArray::getString(Context, Elts,
2510 BitCode == bitc::CST_CODE_CSTRING);
2511 break;
2512 }
2513 case bitc::CST_CODE_DATA: {// DATA: [n x value]
2514 if (Record.empty())
2515 return error("Invalid record");
2516
2517 Type *EltTy;
2518 if (auto *Array = dyn_cast<ArrayType>(CurTy))
2519 EltTy = Array->getElementType();
2520 else
2521 EltTy = cast<VectorType>(CurTy)->getElementType();
2522 if (EltTy->isIntegerTy(8)) {
2523 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2524 if (isa<VectorType>(CurTy))
2525 V = ConstantDataVector::get(Context, Elts);
2526 else
2527 V = ConstantDataArray::get(Context, Elts);
2528 } else if (EltTy->isIntegerTy(16)) {
2529 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2530 if (isa<VectorType>(CurTy))
2531 V = ConstantDataVector::get(Context, Elts);
2532 else
2533 V = ConstantDataArray::get(Context, Elts);
2534 } else if (EltTy->isIntegerTy(32)) {
2535 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2536 if (isa<VectorType>(CurTy))
2537 V = ConstantDataVector::get(Context, Elts);
2538 else
2539 V = ConstantDataArray::get(Context, Elts);
2540 } else if (EltTy->isIntegerTy(64)) {
2541 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2542 if (isa<VectorType>(CurTy))
2543 V = ConstantDataVector::get(Context, Elts);
2544 else
2545 V = ConstantDataArray::get(Context, Elts);
2546 } else if (EltTy->isHalfTy()) {
2547 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2548 if (isa<VectorType>(CurTy))
2549 V = ConstantDataVector::getFP(EltTy, Elts);
2550 else
2551 V = ConstantDataArray::getFP(EltTy, Elts);
2552 } else if (EltTy->isBFloatTy()) {
2553 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2554 if (isa<VectorType>(CurTy))
2555 V = ConstantDataVector::getFP(EltTy, Elts);
2556 else
2557 V = ConstantDataArray::getFP(EltTy, Elts);
2558 } else if (EltTy->isFloatTy()) {
2559 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2560 if (isa<VectorType>(CurTy))
2561 V = ConstantDataVector::getFP(EltTy, Elts);
2562 else
2563 V = ConstantDataArray::getFP(EltTy, Elts);
2564 } else if (EltTy->isDoubleTy()) {
2565 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2566 if (isa<VectorType>(CurTy))
2567 V = ConstantDataVector::getFP(EltTy, Elts);
2568 else
2569 V = ConstantDataArray::getFP(EltTy, Elts);
2570 } else {
2571 return error("Invalid type for value");
2572 }
2573 break;
2574 }
2575 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
2576 if (Record.size() < 2)
2577 return error("Invalid record");
2578 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
2579 if (Opc < 0) {
2580 V = UndefValue::get(CurTy); // Unknown unop.
2581 } else {
2582 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2583 unsigned Flags = 0;
2584 V = ConstantExpr::get(Opc, LHS, Flags);
2585 }
2586 break;
2587 }
2588 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
2589 if (Record.size() < 3)
2590 return error("Invalid record");
2591 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
2592 if (Opc < 0) {
2593 V = UndefValue::get(CurTy); // Unknown binop.
2594 } else {
2595 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2596 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
2597 unsigned Flags = 0;
2598 if (Record.size() >= 4) {
2599 if (Opc == Instruction::Add ||
2600 Opc == Instruction::Sub ||
2601 Opc == Instruction::Mul ||
2602 Opc == Instruction::Shl) {
2603 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2604 Flags |= OverflowingBinaryOperator::NoSignedWrap;
2605 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2606 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2607 } else if (Opc == Instruction::SDiv ||
2608 Opc == Instruction::UDiv ||
2609 Opc == Instruction::LShr ||
2610 Opc == Instruction::AShr) {
2611 if (Record[3] & (1 << bitc::PEO_EXACT))
2612 Flags |= SDivOperator::IsExact;
2613 }
2614 }
2615 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
2616 }
2617 break;
2618 }
2619 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
2620 if (Record.size() < 3)
2621 return error("Invalid record");
2622 int Opc = getDecodedCastOpcode(Record[0]);
2623 if (Opc < 0) {
2624 V = UndefValue::get(CurTy); // Unknown cast.
2625 } else {
2626 Type *OpTy = getTypeByID(Record[1]);
2627 if (!OpTy)
2628 return error("Invalid record");
2629 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
2630 V = UpgradeBitCastExpr(Opc, Op, CurTy);
2631 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
2632 }
2633 break;
2634 }
2635 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
2636 case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
2637 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
2638 // operands]
2639 unsigned OpNum = 0;
2640 Type *PointeeType = nullptr;
2641 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX ||
2642 Record.size() % 2)
2643 PointeeType = getTypeByID(Record[OpNum++]);
2644
2645 bool InBounds = false;
2646 Optional<unsigned> InRangeIndex;
2647 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) {
2648 uint64_t Op = Record[OpNum++];
2649 InBounds = Op & 1;
2650 InRangeIndex = Op >> 1;
2651 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
2652 InBounds = true;
2653
2654 SmallVector<Constant*, 16> Elts;
2655 Type *Elt0FullTy = nullptr;
2656 while (OpNum != Record.size()) {
2657 if (!Elt0FullTy)
2658 Elt0FullTy = getFullyStructuredTypeByID(Record[OpNum]);
2659 Type *ElTy = getTypeByID(Record[OpNum++]);
2660 if (!ElTy)
2661 return error("Invalid record");
2662 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
2663 }
2664
2665 if (Elts.size() < 1)
2666 return error("Invalid gep with no operands");
2667
2668 Type *ImplicitPointeeType =
2669 getPointerElementFlatType(Elt0FullTy->getScalarType());
2670 if (!PointeeType)
2671 PointeeType = ImplicitPointeeType;
2672 else if (PointeeType != ImplicitPointeeType)
2673 return error("Explicit gep operator type does not match pointee type "
2674 "of pointer operand");
2675
2676 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2677 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2678 InBounds, InRangeIndex);
2679 break;
2680 }
2681 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
2682 if (Record.size() < 3)
2683 return error("Invalid record");
2684
2685 Type *SelectorTy = Type::getInt1Ty(Context);
2686
2687 // The selector might be an i1, an <n x i1>, or a <vscale x n x i1>
2688 // Get the type from the ValueList before getting a forward ref.
2689 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
2690 if (Value *V = ValueList[Record[0]])
2691 if (SelectorTy != V->getType())
2692 SelectorTy = VectorType::get(SelectorTy,
2693 VTy->getElementCount());
2694
2695 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2696 SelectorTy),
2697 ValueList.getConstantFwdRef(Record[1],CurTy),
2698 ValueList.getConstantFwdRef(Record[2],CurTy));
2699 break;
2700 }
2701 case bitc::CST_CODE_CE_EXTRACTELT
2702 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2703 if (Record.size() < 3)
2704 return error("Invalid record");
2705 VectorType *OpTy =
2706 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2707 if (!OpTy)
2708 return error("Invalid record");
2709 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2710 Constant *Op1 = nullptr;
2711 if (Record.size() == 4) {
2712 Type *IdxTy = getTypeByID(Record[2]);
2713 if (!IdxTy)
2714 return error("Invalid record");
2715 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2716 } else {
2717 // Deprecated, but still needed to read old bitcode files.
2718 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2719 }
2720 if (!Op1)
2721 return error("Invalid record");
2722 V = ConstantExpr::getExtractElement(Op0, Op1);
2723 break;
2724 }
2725 case bitc::CST_CODE_CE_INSERTELT
2726 : { // CE_INSERTELT: [opval, opval, opty, opval]
2727 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2728 if (Record.size() < 3 || !OpTy)
2729 return error("Invalid record");
2730 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2731 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2732 OpTy->getElementType());
2733 Constant *Op2 = nullptr;
2734 if (Record.size() == 4) {
2735 Type *IdxTy = getTypeByID(Record[2]);
2736 if (!IdxTy)
2737 return error("Invalid record");
2738 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2739 } else {
2740 // Deprecated, but still needed to read old bitcode files.
2741 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2742 }
2743 if (!Op2)
2744 return error("Invalid record");
2745 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2746 break;
2747 }
2748 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2749 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2750 if (Record.size() < 3 || !OpTy)
2751 return error("Invalid record");
2752 DelayedShuffles.push_back(
2753 {OpTy, OpTy, CurFullTy, Record[0], Record[1], Record[2], NextCstNo});
2754 ++NextCstNo;
2755 continue;
2756 }
2757 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2758 VectorType *RTy = dyn_cast<VectorType>(CurTy);
2759 VectorType *OpTy =
2760 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2761 if (Record.size() < 4 || !RTy || !OpTy)
2762 return error("Invalid record");
2763 DelayedShuffles.push_back(
2764 {OpTy, RTy, CurFullTy, Record[1], Record[2], Record[3], NextCstNo});
2765 ++NextCstNo;
2766 continue;
2767 }
2768 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
2769 if (Record.size() < 4)
2770 return error("Invalid record");
2771 Type *OpTy = getTypeByID(Record[0]);
2772 if (!OpTy)
2773 return error("Invalid record");
2774 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2775 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2776
2777 if (OpTy->isFPOrFPVectorTy())
2778 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2779 else
2780 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2781 break;
2782 }
2783 // This maintains backward compatibility, pre-asm dialect keywords.
2784 // Deprecated, but still needed to read old bitcode files.
2785 case bitc::CST_CODE_INLINEASM_OLD: {
2786 if (Record.size() < 2)
2787 return error("Invalid record");
2788 std::string AsmStr, ConstrStr;
2789 bool HasSideEffects = Record[0] & 1;
2790 bool IsAlignStack = Record[0] >> 1;
2791 unsigned AsmStrSize = Record[1];
2792 if (2+AsmStrSize >= Record.size())
2793 return error("Invalid record");
2794 unsigned ConstStrSize = Record[2+AsmStrSize];
2795 if (3+AsmStrSize+ConstStrSize > Record.size())
2796 return error("Invalid record");
2797
2798 for (unsigned i = 0; i != AsmStrSize; ++i)
2799 AsmStr += (char)Record[2+i];
2800 for (unsigned i = 0; i != ConstStrSize; ++i)
2801 ConstrStr += (char)Record[3+AsmStrSize+i];
2802 UpgradeInlineAsmString(&AsmStr);
2803 V = InlineAsm::get(
2804 cast<FunctionType>(getPointerElementFlatType(CurFullTy)), AsmStr,
2805 ConstrStr, HasSideEffects, IsAlignStack);
2806 break;
2807 }
2808 // This version adds support for the asm dialect keywords (e.g.,
2809 // inteldialect).
2810 case bitc::CST_CODE_INLINEASM: {
2811 if (Record.size() < 2)
2812 return error("Invalid record");
2813 std::string AsmStr, ConstrStr;
2814 bool HasSideEffects = Record[0] & 1;
2815 bool IsAlignStack = (Record[0] >> 1) & 1;
2816 unsigned AsmDialect = Record[0] >> 2;
2817 unsigned AsmStrSize = Record[1];
2818 if (2+AsmStrSize >= Record.size())
2819 return error("Invalid record");
2820 unsigned ConstStrSize = Record[2+AsmStrSize];
2821 if (3+AsmStrSize+ConstStrSize > Record.size())
2822 return error("Invalid record");
2823
2824 for (unsigned i = 0; i != AsmStrSize; ++i)
2825 AsmStr += (char)Record[2+i];
2826 for (unsigned i = 0; i != ConstStrSize; ++i)
2827 ConstrStr += (char)Record[3+AsmStrSize+i];
2828 UpgradeInlineAsmString(&AsmStr);
2829 V = InlineAsm::get(
2830 cast<FunctionType>(getPointerElementFlatType(CurFullTy)), AsmStr,
2831 ConstrStr, HasSideEffects, IsAlignStack,
2832 InlineAsm::AsmDialect(AsmDialect));
2833 break;
2834 }
2835 case bitc::CST_CODE_BLOCKADDRESS:{
2836 if (Record.size() < 3)
2837 return error("Invalid record");
2838 Type *FnTy = getTypeByID(Record[0]);
2839 if (!FnTy)
2840 return error("Invalid record");
2841 Function *Fn =
2842 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
2843 if (!Fn)
2844 return error("Invalid record");
2845
2846 // If the function is already parsed we can insert the block address right
2847 // away.
2848 BasicBlock *BB;
2849 unsigned BBID = Record[2];
2850 if (!BBID)
2851 // Invalid reference to entry block.
2852 return error("Invalid ID");
2853 if (!Fn->empty()) {
2854 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
2855 for (size_t I = 0, E = BBID; I != E; ++I) {
2856 if (BBI == BBE)
2857 return error("Invalid ID");
2858 ++BBI;
2859 }
2860 BB = &*BBI;
2861 } else {
2862 // Otherwise insert a placeholder and remember it so it can be inserted
2863 // when the function is parsed.
2864 auto &FwdBBs = BasicBlockFwdRefs[Fn];
2865 if (FwdBBs.empty())
2866 BasicBlockFwdRefQueue.push_back(Fn);
2867 if (FwdBBs.size() < BBID + 1)
2868 FwdBBs.resize(BBID + 1);
2869 if (!FwdBBs[BBID])
2870 FwdBBs[BBID] = BasicBlock::Create(Context);
2871 BB = FwdBBs[BBID];
2872 }
2873 V = BlockAddress::get(Fn, BB);
2874 break;
2875 }
2876 }
2877
2878 assert(V->getType() == flattenPointerTypes(CurFullTy) &&((V->getType() == flattenPointerTypes(CurFullTy) &&
"Incorrect fully structured type provided for Constant") ? static_cast
<void> (0) : __assert_fail ("V->getType() == flattenPointerTypes(CurFullTy) && \"Incorrect fully structured type provided for Constant\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 2879, __PRETTY_FUNCTION__))
2879 "Incorrect fully structured type provided for Constant")((V->getType() == flattenPointerTypes(CurFullTy) &&
"Incorrect fully structured type provided for Constant") ? static_cast
<void> (0) : __assert_fail ("V->getType() == flattenPointerTypes(CurFullTy) && \"Incorrect fully structured type provided for Constant\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 2879, __PRETTY_FUNCTION__))
;
2880 ValueList.assignValue(V, NextCstNo, CurFullTy);
2881 ++NextCstNo;
2882 }
2883}
2884
2885Error BitcodeReader::parseUseLists() {
2886 if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
2887 return Err;
2888
2889 // Read all the records.
2890 SmallVector<uint64_t, 64> Record;
2891
2892 while (true) {
2893 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2894 if (!MaybeEntry)
2895 return MaybeEntry.takeError();
2896 BitstreamEntry Entry = MaybeEntry.get();
2897
2898 switch (Entry.Kind) {
2899 case BitstreamEntry::SubBlock: // Handled for us already.
2900 case BitstreamEntry::Error:
2901 return error("Malformed block");
2902 case BitstreamEntry::EndBlock:
2903 return Error::success();
2904 case BitstreamEntry::Record:
2905 // The interesting case.
2906 break;
2907 }
2908
2909 // Read a use list record.
2910 Record.clear();
2911 bool IsBB = false;
2912 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2913 if (!MaybeRecord)
2914 return MaybeRecord.takeError();
2915 switch (MaybeRecord.get()) {
2916 default: // Default behavior: unknown type.
2917 break;
2918 case bitc::USELIST_CODE_BB:
2919 IsBB = true;
2920 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2921 case bitc::USELIST_CODE_DEFAULT: {
2922 unsigned RecordLength = Record.size();
2923 if (RecordLength < 3)
2924 // Records should have at least an ID and two indexes.
2925 return error("Invalid record");
2926 unsigned ID = Record.back();
2927 Record.pop_back();
2928
2929 Value *V;
2930 if (IsBB) {
2931 assert(ID < FunctionBBs.size() && "Basic block not found")((ID < FunctionBBs.size() && "Basic block not found"
) ? static_cast<void> (0) : __assert_fail ("ID < FunctionBBs.size() && \"Basic block not found\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 2931, __PRETTY_FUNCTION__))
;
2932 V = FunctionBBs[ID];
2933 } else
2934 V = ValueList[ID];
2935 unsigned NumUses = 0;
2936 SmallDenseMap<const Use *, unsigned, 16> Order;
2937 for (const Use &U : V->materialized_uses()) {
2938 if (++NumUses > Record.size())
2939 break;
2940 Order[&U] = Record[NumUses - 1];
2941 }
2942 if (Order.size() != Record.size() || NumUses > Record.size())
2943 // Mismatches can happen if the functions are being materialized lazily
2944 // (out-of-order), or a value has been upgraded.
2945 break;
2946
2947 V->sortUseList([&](const Use &L, const Use &R) {
2948 return Order.lookup(&L) < Order.lookup(&R);
2949 });
2950 break;
2951 }
2952 }
2953 }
2954}
2955
2956/// When we see the block for metadata, remember where it is and then skip it.
2957/// This lets us lazily deserialize the metadata.
2958Error BitcodeReader::rememberAndSkipMetadata() {
2959 // Save the current stream state.
2960 uint64_t CurBit = Stream.GetCurrentBitNo();
2961 DeferredMetadataInfo.push_back(CurBit);
2962
2963 // Skip over the block for now.
2964 if (Error Err = Stream.SkipBlock())
2965 return Err;
2966 return Error::success();
2967}
2968
2969Error BitcodeReader::materializeMetadata() {
2970 for (uint64_t BitPos : DeferredMetadataInfo) {
2971 // Move the bit stream to the saved position.
2972 if (Error JumpFailed = Stream.JumpToBit(BitPos))
2973 return JumpFailed;
2974 if (Error Err = MDLoader->parseModuleMetadata())
2975 return Err;
2976 }
2977
2978 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2979 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
2980 // multiple times.
2981 if (!TheModule->getNamedMetadata("llvm.linker.options")) {
2982 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
2983 NamedMDNode *LinkerOpts =
2984 TheModule->getOrInsertNamedMetadata("llvm.linker.options");
2985 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
2986 LinkerOpts->addOperand(cast<MDNode>(MDOptions));
2987 }
2988 }
2989
2990 DeferredMetadataInfo.clear();
2991 return Error::success();
2992}
2993
2994void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
2995
2996/// When we see the block for a function body, remember where it is and then
2997/// skip it. This lets us lazily deserialize the functions.
2998Error BitcodeReader::rememberAndSkipFunctionBody() {
2999 // Get the function we are talking about.
3000 if (FunctionsWithBodies.empty())
3001 return error("Insufficient function protos");
3002
3003 Function *Fn = FunctionsWithBodies.back();
3004 FunctionsWithBodies.pop_back();
3005
3006 // Save the current stream state.
3007 uint64_t CurBit = Stream.GetCurrentBitNo();
3008 assert((((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] ==
CurBit) && "Mismatch between VST and scanned function offsets"
) ? static_cast<void> (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3010, __PRETTY_FUNCTION__))
3009 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&(((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] ==
CurBit) && "Mismatch between VST and scanned function offsets"
) ? static_cast<void> (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3010, __PRETTY_FUNCTION__))
3010 "Mismatch between VST and scanned function offsets")(((DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] ==
CurBit) && "Mismatch between VST and scanned function offsets"
) ? static_cast<void> (0) : __assert_fail ("(DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && \"Mismatch between VST and scanned function offsets\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3010, __PRETTY_FUNCTION__))
;
3011 DeferredFunctionInfo[Fn] = CurBit;
3012
3013 // Skip over the function block for now.
3014 if (Error Err = Stream.SkipBlock())
3015 return Err;
3016 return Error::success();
3017}
3018
3019Error BitcodeReader::globalCleanup() {
3020 // Patch the initializers for globals and aliases up.
3021 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3022 return Err;
3023 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3024 return error("Malformed global initializer set");
3025
3026 // Look for intrinsic functions which need to be upgraded at some point
3027 // and functions that need to have their function attributes upgraded.
3028 for (Function &F : *TheModule) {
3029 MDLoader->upgradeDebugIntrinsics(F);
3030 Function *NewFn;
3031 if (UpgradeIntrinsicFunction(&F, NewFn))
3032 UpgradedIntrinsics[&F] = NewFn;
3033 else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
3034 // Some types could be renamed during loading if several modules are
3035 // loaded in the same LLVMContext (LTO scenario). In this case we should
3036 // remangle intrinsics names as well.
3037 RemangledIntrinsics[&F] = Remangled.getValue();
3038 // Look for functions that rely on old function attribute behavior.
3039 UpgradeFunctionAttributes(F);
3040 }
3041
3042 // Look for global variables which need to be renamed.
3043 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
3044 for (GlobalVariable &GV : TheModule->globals())
3045 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))
3046 UpgradedVariables.emplace_back(&GV, Upgraded);
3047 for (auto &Pair : UpgradedVariables) {
3048 Pair.first->eraseFromParent();
3049 TheModule->getGlobalList().push_back(Pair.second);
3050 }
3051
3052 // Force deallocation of memory for these vectors to favor the client that
3053 // want lazy deserialization.
3054 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
3055 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>().swap(
3056 IndirectSymbolInits);
3057 return Error::success();
3058}
3059
3060/// Support for lazy parsing of function bodies. This is required if we
3061/// either have an old bitcode file without a VST forward declaration record,
3062/// or if we have an anonymous function being materialized, since anonymous
3063/// functions do not have a name and are therefore not in the VST.
3064Error BitcodeReader::rememberAndSkipFunctionBodies() {
3065 if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))
3066 return JumpFailed;
3067
3068 if (Stream.AtEndOfStream())
3069 return error("Could not find function in stream");
3070
3071 if (!SeenFirstFunctionBody)
3072 return error("Trying to materialize functions before seeing function blocks");
3073
3074 // An old bitcode file with the symbol table at the end would have
3075 // finished the parse greedily.
3076 assert(SeenValueSymbolTable)((SeenValueSymbolTable) ? static_cast<void> (0) : __assert_fail
("SeenValueSymbolTable", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3076, __PRETTY_FUNCTION__))
;
3077
3078 SmallVector<uint64_t, 64> Record;
3079
3080 while (true) {
3081 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3082 if (!MaybeEntry)
3083 return MaybeEntry.takeError();
3084 llvm::BitstreamEntry Entry = MaybeEntry.get();
3085
3086 switch (Entry.Kind) {
3087 default:
3088 return error("Expect SubBlock");
3089 case BitstreamEntry::SubBlock:
3090 switch (Entry.ID) {
3091 default:
3092 return error("Expect function block");
3093 case bitc::FUNCTION_BLOCK_ID:
3094 if (Error Err = rememberAndSkipFunctionBody())
3095 return Err;
3096 NextUnreadBit = Stream.GetCurrentBitNo();
3097 return Error::success();
3098 }
3099 }
3100 }
3101}
3102
3103bool BitcodeReaderBase::readBlockInfo() {
3104 Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
3105 Stream.ReadBlockInfoBlock();
3106 if (!MaybeNewBlockInfo)
3107 return true; // FIXME Handle the error.
3108 Optional<BitstreamBlockInfo> NewBlockInfo =
3109 std::move(MaybeNewBlockInfo.get());
3110 if (!NewBlockInfo)
3111 return true;
3112 BlockInfo = std::move(*NewBlockInfo);
3113 return false;
3114}
3115
3116Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
3117 // v1: [selection_kind, name]
3118 // v2: [strtab_offset, strtab_size, selection_kind]
3119 StringRef Name;
3120 std::tie(Name, Record) = readNameFromStrtab(Record);
3121
3122 if (Record.empty())
3123 return error("Invalid record");
3124 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3125 std::string OldFormatName;
3126 if (!UseStrtab) {
3127 if (Record.size() < 2)
3128 return error("Invalid record");
3129 unsigned ComdatNameSize = Record[1];
3130 OldFormatName.reserve(ComdatNameSize);
3131 for (unsigned i = 0; i != ComdatNameSize; ++i)
3132 OldFormatName += (char)Record[2 + i];
3133 Name = OldFormatName;
3134 }
3135 Comdat *C = TheModule->getOrInsertComdat(Name);
3136 C->setSelectionKind(SK);
3137 ComdatList.push_back(C);
3138 return Error::success();
3139}
3140
3141static void inferDSOLocal(GlobalValue *GV) {
3142 // infer dso_local from linkage and visibility if it is not encoded.
3143 if (GV->hasLocalLinkage() ||
3144 (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()))
3145 GV->setDSOLocal(true);
3146}
3147
3148Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
3149 // v1: [pointer type, isconst, initid, linkage, alignment, section,
3150 // visibility, threadlocal, unnamed_addr, externally_initialized,
3151 // dllstorageclass, comdat, attributes, preemption specifier,
3152 // partition strtab offset, partition strtab size] (name in VST)
3153 // v2: [strtab_offset, strtab_size, v1]
3154 StringRef Name;
3155 std::tie(Name, Record) = readNameFromStrtab(Record);
3156
3157 if (Record.size() < 6)
3158 return error("Invalid record");
3159 Type *FullTy = getFullyStructuredTypeByID(Record[0]);
3160 Type *Ty = flattenPointerTypes(FullTy);
3161 if (!Ty)
3162 return error("Invalid record");
3163 bool isConstant = Record[1] & 1;
3164 bool explicitType = Record[1] & 2;
3165 unsigned AddressSpace;
3166 if (explicitType) {
3167 AddressSpace = Record[1] >> 2;
3168 } else {
3169 if (!Ty->isPointerTy())
3170 return error("Invalid type for value");
3171 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3172 std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
3173 }
3174
3175 uint64_t RawLinkage = Record[3];
3176 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
3177 MaybeAlign Alignment;
3178 if (Error Err = parseAlignmentValue(Record[4], Alignment))
3179 return Err;
3180 std::string Section;
3181 if (Record[5]) {
3182 if (Record[5] - 1 >= SectionTable.size())
3183 return error("Invalid ID");
3184 Section = SectionTable[Record[5] - 1];
3185 }
3186 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
3187 // Local linkage must have default visibility.
3188 // auto-upgrade `hidden` and `protected` for old bitcode.
3189 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3190 Visibility = getDecodedVisibility(Record[6]);
3191
3192 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3193 if (Record.size() > 7)
3194 TLM = getDecodedThreadLocalMode(Record[7]);
3195
3196 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3197 if (Record.size() > 8)
3198 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
3199
3200 bool ExternallyInitialized = false;
3201 if (Record.size() > 9)
3202 ExternallyInitialized = Record[9];
3203
3204 GlobalVariable *NewGV =
3205 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
3206 nullptr, TLM, AddressSpace, ExternallyInitialized);
3207 NewGV->setAlignment(Alignment);
3208 if (!Section.empty())
3209 NewGV->setSection(Section);
3210 NewGV->setVisibility(Visibility);
3211 NewGV->setUnnamedAddr(UnnamedAddr);
3212
3213 if (Record.size() > 10)
3214 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
3215 else
3216 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3217
3218 FullTy = PointerType::get(FullTy, AddressSpace);
3219 assert(NewGV->getType() == flattenPointerTypes(FullTy) &&((NewGV->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully specified type for GlobalVariable") ? static_cast
<void> (0) : __assert_fail ("NewGV->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully specified type for GlobalVariable\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3220, __PRETTY_FUNCTION__))
3220 "Incorrect fully specified type for GlobalVariable")((NewGV->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully specified type for GlobalVariable") ? static_cast
<void> (0) : __assert_fail ("NewGV->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully specified type for GlobalVariable\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3220, __PRETTY_FUNCTION__))
;
3221 ValueList.push_back(NewGV, FullTy);
3222
3223 // Remember which value to use for the global initializer.
3224 if (unsigned InitID = Record[2])
3225 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
3226
3227 if (Record.size() > 11) {
3228 if (unsigned ComdatID = Record[11]) {
3229 if (ComdatID > ComdatList.size())
3230 return error("Invalid global variable comdat ID");
3231 NewGV->setComdat(ComdatList[ComdatID - 1]);
3232 }
3233 } else if (hasImplicitComdat(RawLinkage)) {
3234 NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3235 }
3236
3237 if (Record.size() > 12) {
3238 auto AS = getAttributes(Record[12]).getFnAttributes();
3239 NewGV->setAttributes(AS);
3240 }
3241
3242 if (Record.size() > 13) {
3243 NewGV->setDSOLocal(getDecodedDSOLocal(Record[13]));
3244 }
3245 inferDSOLocal(NewGV);
3246
3247 // Check whether we have enough values to read a partition name.
3248 if (Record.size() > 15)
3249 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
3250
3251 return Error::success();
3252}
3253
3254Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
3255 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3256 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3257 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
3258 // v2: [strtab_offset, strtab_size, v1]
3259 StringRef Name;
3260 std::tie(Name, Record) = readNameFromStrtab(Record);
3261
3262 if (Record.size() < 8)
3263 return error("Invalid record");
3264 Type *FullFTy = getFullyStructuredTypeByID(Record[0]);
3265 Type *FTy = flattenPointerTypes(FullFTy);
3266 if (!FTy)
3267 return error("Invalid record");
3268 if (isa<PointerType>(FTy))
3269 std::tie(FullFTy, FTy) = getPointerElementTypes(FullFTy);
3270
3271 if (!isa<FunctionType>(FTy))
3272 return error("Invalid type for value");
3273 auto CC = static_cast<CallingConv::ID>(Record[1]);
3274 if (CC & ~CallingConv::MaxID)
3275 return error("Invalid calling convention ID");
3276
3277 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
3278 if (Record.size() > 16)
3279 AddrSpace = Record[16];
3280
3281 Function *Func =
3282 Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,
3283 AddrSpace, Name, TheModule);
3284
3285 assert(Func->getFunctionType() == flattenPointerTypes(FullFTy) &&((Func->getFunctionType() == flattenPointerTypes(FullFTy) &&
"Incorrect fully specified type provided for function") ? static_cast
<void> (0) : __assert_fail ("Func->getFunctionType() == flattenPointerTypes(FullFTy) && \"Incorrect fully specified type provided for function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3286, __PRETTY_FUNCTION__))
3286 "Incorrect fully specified type provided for function")((Func->getFunctionType() == flattenPointerTypes(FullFTy) &&
"Incorrect fully specified type provided for function") ? static_cast
<void> (0) : __assert_fail ("Func->getFunctionType() == flattenPointerTypes(FullFTy) && \"Incorrect fully specified type provided for function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3286, __PRETTY_FUNCTION__))
;
3287 FunctionTypes[Func] = cast<FunctionType>(FullFTy);
3288
3289 Func->setCallingConv(CC);
3290 bool isProto = Record[2];
3291 uint64_t RawLinkage = Record[3];
3292 Func->setLinkage(getDecodedLinkage(RawLinkage));
3293 Func->setAttributes(getAttributes(Record[4]));
3294
3295 // Upgrade any old-style byval or sret without a type by propagating the
3296 // argument's pointee type. There should be no opaque pointers where the byval
3297 // type is implicit.
3298 for (unsigned i = 0; i != Func->arg_size(); ++i) {
3299 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet}) {
3300 if (!Func->hasParamAttribute(i, Kind))
3301 continue;
3302
3303 Func->removeParamAttr(i, Kind);
3304
3305 Type *PTy = cast<FunctionType>(FullFTy)->getParamType(i);
3306 Type *PtrEltTy = getPointerElementFlatType(PTy);
3307 Attribute NewAttr =
3308 Kind == Attribute::ByVal
3309 ? Attribute::getWithByValType(Context, PtrEltTy)
3310 : Attribute::getWithStructRetType(Context, PtrEltTy);
3311 Func->addParamAttr(i, NewAttr);
3312 }
3313 }
3314
3315 MaybeAlign Alignment;
3316 if (Error Err = parseAlignmentValue(Record[5], Alignment))
3317 return Err;
3318 Func->setAlignment(Alignment);
3319 if (Record[6]) {
3320 if (Record[6] - 1 >= SectionTable.size())
3321 return error("Invalid ID");
3322 Func->setSection(SectionTable[Record[6] - 1]);
3323 }
3324 // Local linkage must have default visibility.
3325 // auto-upgrade `hidden` and `protected` for old bitcode.
3326 if (!Func->hasLocalLinkage())
3327 Func->setVisibility(getDecodedVisibility(Record[7]));
3328 if (Record.size() > 8 && Record[8]) {
3329 if (Record[8] - 1 >= GCTable.size())
3330 return error("Invalid ID");
3331 Func->setGC(GCTable[Record[8] - 1]);
3332 }
3333 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3334 if (Record.size() > 9)
3335 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
3336 Func->setUnnamedAddr(UnnamedAddr);
3337 if (Record.size() > 10 && Record[10] != 0)
3338 FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1));
3339
3340 if (Record.size() > 11)
3341 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3342 else
3343 upgradeDLLImportExportLinkage(Func, RawLinkage);
3344
3345 if (Record.size() > 12) {
3346 if (unsigned ComdatID = Record[12]) {
3347 if (ComdatID > ComdatList.size())
3348 return error("Invalid function comdat ID");
3349 Func->setComdat(ComdatList[ComdatID - 1]);
3350 }
3351 } else if (hasImplicitComdat(RawLinkage)) {
3352 Func->setComdat(reinterpret_cast<Comdat *>(1));
3353 }
3354
3355 if (Record.size() > 13 && Record[13] != 0)
3356 FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1));
3357
3358 if (Record.size() > 14 && Record[14] != 0)
3359 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3360
3361 if (Record.size() > 15) {
3362 Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
3363 }
3364 inferDSOLocal(Func);
3365
3366 // Record[16] is the address space number.
3367
3368 // Check whether we have enough values to read a partition name.
3369 if (Record.size() > 18)
3370 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
3371
3372 Type *FullTy = PointerType::get(FullFTy, AddrSpace);
3373 assert(Func->getType() == flattenPointerTypes(FullTy) &&((Func->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully specified type provided for Function") ? static_cast
<void> (0) : __assert_fail ("Func->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully specified type provided for Function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3374, __PRETTY_FUNCTION__))
3374 "Incorrect fully specified type provided for Function")((Func->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully specified type provided for Function") ? static_cast
<void> (0) : __assert_fail ("Func->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully specified type provided for Function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3374, __PRETTY_FUNCTION__))
;
3375 ValueList.push_back(Func, FullTy);
3376
3377 // If this is a function with a body, remember the prototype we are
3378 // creating now, so that we can match up the body with them later.
3379 if (!isProto) {
3380 Func->setIsMaterializable(true);
3381 FunctionsWithBodies.push_back(Func);
3382 DeferredFunctionInfo[Func] = 0;
3383 }
3384 return Error::success();
3385}
3386
3387Error BitcodeReader::parseGlobalIndirectSymbolRecord(
3388 unsigned BitCode, ArrayRef<uint64_t> Record) {
3389 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3390 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3391 // dllstorageclass, threadlocal, unnamed_addr,
3392 // preemption specifier] (name in VST)
3393 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3394 // visibility, dllstorageclass, threadlocal, unnamed_addr,
3395 // preemption specifier] (name in VST)
3396 // v2: [strtab_offset, strtab_size, v1]
3397 StringRef Name;
3398 std::tie(Name, Record) = readNameFromStrtab(Record);
3399
3400 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
3401 if (Record.size() < (3 + (unsigned)NewRecord))
3402 return error("Invalid record");
3403 unsigned OpNum = 0;
3404 Type *FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
3405 Type *Ty = flattenPointerTypes(FullTy);
3406 if (!Ty)
3407 return error("Invalid record");
3408
3409 unsigned AddrSpace;
3410 if (!NewRecord) {
3411 auto *PTy = dyn_cast<PointerType>(Ty);
3412 if (!PTy)
3413 return error("Invalid type for value");
3414 std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
3415 AddrSpace = PTy->getAddressSpace();
3416 } else {
3417 AddrSpace = Record[OpNum++];
3418 }
3419
3420 auto Val = Record[OpNum++];
3421 auto Linkage = Record[OpNum++];
3422 GlobalIndirectSymbol *NewGA;
3423 if (BitCode == bitc::MODULE_CODE_ALIAS ||
3424 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
3425 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3426 TheModule);
3427 else
3428 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3429 nullptr, TheModule);
3430
3431 assert(NewGA->getValueType() == flattenPointerTypes(FullTy) &&((NewGA->getValueType() == flattenPointerTypes(FullTy) &&
"Incorrect fully structured type provided for GlobalIndirectSymbol"
) ? static_cast<void> (0) : __assert_fail ("NewGA->getValueType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for GlobalIndirectSymbol\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3432, __PRETTY_FUNCTION__))
3432 "Incorrect fully structured type provided for GlobalIndirectSymbol")((NewGA->getValueType() == flattenPointerTypes(FullTy) &&
"Incorrect fully structured type provided for GlobalIndirectSymbol"
) ? static_cast<void> (0) : __assert_fail ("NewGA->getValueType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for GlobalIndirectSymbol\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3432, __PRETTY_FUNCTION__))
;
3433 // Local linkage must have default visibility.
3434 // auto-upgrade `hidden` and `protected` for old bitcode.
3435 if (OpNum != Record.size()) {
3436 auto VisInd = OpNum++;
3437 if (!NewGA->hasLocalLinkage())
3438 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3439 }
3440 if (BitCode == bitc::MODULE_CODE_ALIAS ||
3441 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
3442 if (OpNum != Record.size())
3443 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
3444 else
3445 upgradeDLLImportExportLinkage(NewGA, Linkage);
3446 if (OpNum != Record.size())
3447 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3448 if (OpNum != Record.size())
3449 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++]));
3450 }
3451 if (OpNum != Record.size())
3452 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
3453 inferDSOLocal(NewGA);
3454
3455 // Check whether we have enough values to read a partition name.
3456 if (OpNum + 1 < Record.size()) {
3457 NewGA->setPartition(
3458 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
3459 OpNum += 2;
Value stored to 'OpNum' is never read
3460 }
3461
3462 FullTy = PointerType::get(FullTy, AddrSpace);
3463 assert(NewGA->getType() == flattenPointerTypes(FullTy) &&((NewGA->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully structured type provided for GlobalIndirectSymbol"
) ? static_cast<void> (0) : __assert_fail ("NewGA->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for GlobalIndirectSymbol\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3464, __PRETTY_FUNCTION__))
3464 "Incorrect fully structured type provided for GlobalIndirectSymbol")((NewGA->getType() == flattenPointerTypes(FullTy) &&
"Incorrect fully structured type provided for GlobalIndirectSymbol"
) ? static_cast<void> (0) : __assert_fail ("NewGA->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for GlobalIndirectSymbol\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3464, __PRETTY_FUNCTION__))
;
3465 ValueList.push_back(NewGA, FullTy);
3466 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
3467 return Error::success();
3468}
3469
3470Error BitcodeReader::parseModule(uint64_t ResumeBit,
3471 bool ShouldLazyLoadMetadata,
3472 DataLayoutCallbackTy DataLayoutCallback) {
3473 if (ResumeBit) {
3474 if (Error JumpFailed = Stream.JumpToBit(ResumeBit))
3475 return JumpFailed;
3476 } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3477 return Err;
3478
3479 SmallVector<uint64_t, 64> Record;
3480
3481 // Parts of bitcode parsing depend on the datalayout. Make sure we
3482 // finalize the datalayout before we run any of that code.
3483 bool ResolvedDataLayout = false;
3484 auto ResolveDataLayout = [&] {
3485 if (ResolvedDataLayout)
3486 return;
3487
3488 // datalayout and triple can't be parsed after this point.
3489 ResolvedDataLayout = true;
3490
3491 // Upgrade data layout string.
3492 std::string DL = llvm::UpgradeDataLayoutString(
3493 TheModule->getDataLayoutStr(), TheModule->getTargetTriple());
3494 TheModule->setDataLayout(DL);
3495
3496 if (auto LayoutOverride =
3497 DataLayoutCallback(TheModule->getTargetTriple()))
3498 TheModule->setDataLayout(*LayoutOverride);
3499 };
3500
3501 // Read all the records for this module.
3502 while (true) {
3503 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3504 if (!MaybeEntry)
3505 return MaybeEntry.takeError();
3506 llvm::BitstreamEntry Entry = MaybeEntry.get();
3507
3508 switch (Entry.Kind) {
3509 case BitstreamEntry::Error:
3510 return error("Malformed block");
3511 case BitstreamEntry::EndBlock:
3512 ResolveDataLayout();
3513 return globalCleanup();
3514
3515 case BitstreamEntry::SubBlock:
3516 switch (Entry.ID) {
3517 default: // Skip unknown content.
3518 if (Error Err = Stream.SkipBlock())
3519 return Err;
3520 break;
3521 case bitc::BLOCKINFO_BLOCK_ID:
3522 if (readBlockInfo())
3523 return error("Malformed block");
3524 break;
3525 case bitc::PARAMATTR_BLOCK_ID:
3526 if (Error Err = parseAttributeBlock())
3527 return Err;
3528 break;
3529 case bitc::PARAMATTR_GROUP_BLOCK_ID:
3530 if (Error Err = parseAttributeGroupBlock())
3531 return Err;
3532 break;
3533 case bitc::TYPE_BLOCK_ID_NEW:
3534 if (Error Err = parseTypeTable())
3535 return Err;
3536 break;
3537 case bitc::VALUE_SYMTAB_BLOCK_ID:
3538 if (!SeenValueSymbolTable) {
3539 // Either this is an old form VST without function index and an
3540 // associated VST forward declaration record (which would have caused
3541 // the VST to be jumped to and parsed before it was encountered
3542 // normally in the stream), or there were no function blocks to
3543 // trigger an earlier parsing of the VST.
3544 assert(VSTOffset == 0 || FunctionsWithBodies.empty())((VSTOffset == 0 || FunctionsWithBodies.empty()) ? static_cast
<void> (0) : __assert_fail ("VSTOffset == 0 || FunctionsWithBodies.empty()"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3544, __PRETTY_FUNCTION__))
;
3545 if (Error Err = parseValueSymbolTable())
3546 return Err;
3547 SeenValueSymbolTable = true;
3548 } else {
3549 // We must have had a VST forward declaration record, which caused
3550 // the parser to jump to and parse the VST earlier.
3551 assert(VSTOffset > 0)((VSTOffset > 0) ? static_cast<void> (0) : __assert_fail
("VSTOffset > 0", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3551, __PRETTY_FUNCTION__))
;
3552 if (Error Err = Stream.SkipBlock())
3553 return Err;
3554 }
3555 break;
3556 case bitc::CONSTANTS_BLOCK_ID:
3557 if (Error Err = parseConstants())
3558 return Err;
3559 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3560 return Err;
3561 break;
3562 case bitc::METADATA_BLOCK_ID:
3563 if (ShouldLazyLoadMetadata) {
3564 if (Error Err = rememberAndSkipMetadata())
3565 return Err;
3566 break;
3567 }
3568 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata")((DeferredMetadataInfo.empty() && "Unexpected deferred metadata"
) ? static_cast<void> (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Unexpected deferred metadata\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3568, __PRETTY_FUNCTION__))
;
3569 if (Error Err = MDLoader->parseModuleMetadata())
3570 return Err;
3571 break;
3572 case bitc::METADATA_KIND_BLOCK_ID:
3573 if (Error Err = MDLoader->parseMetadataKinds())
3574 return Err;
3575 break;
3576 case bitc::FUNCTION_BLOCK_ID:
3577 ResolveDataLayout();
3578
3579 // If this is the first function body we've seen, reverse the
3580 // FunctionsWithBodies list.
3581 if (!SeenFirstFunctionBody) {
3582 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3583 if (Error Err = globalCleanup())
3584 return Err;
3585 SeenFirstFunctionBody = true;
3586 }
3587
3588 if (VSTOffset > 0) {
3589 // If we have a VST forward declaration record, make sure we
3590 // parse the VST now if we haven't already. It is needed to
3591 // set up the DeferredFunctionInfo vector for lazy reading.
3592 if (!SeenValueSymbolTable) {
3593 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
3594 return Err;
3595 SeenValueSymbolTable = true;
3596 // Fall through so that we record the NextUnreadBit below.
3597 // This is necessary in case we have an anonymous function that
3598 // is later materialized. Since it will not have a VST entry we
3599 // need to fall back to the lazy parse to find its offset.
3600 } else {
3601 // If we have a VST forward declaration record, but have already
3602 // parsed the VST (just above, when the first function body was
3603 // encountered here), then we are resuming the parse after
3604 // materializing functions. The ResumeBit points to the
3605 // start of the last function block recorded in the
3606 // DeferredFunctionInfo map. Skip it.
3607 if (Error Err = Stream.SkipBlock())
3608 return Err;
3609 continue;
3610 }
3611 }
3612
3613 // Support older bitcode files that did not have the function
3614 // index in the VST, nor a VST forward declaration record, as
3615 // well as anonymous functions that do not have VST entries.
3616 // Build the DeferredFunctionInfo vector on the fly.
3617 if (Error Err = rememberAndSkipFunctionBody())
3618 return Err;
3619
3620 // Suspend parsing when we reach the function bodies. Subsequent
3621 // materialization calls will resume it when necessary. If the bitcode
3622 // file is old, the symbol table will be at the end instead and will not
3623 // have been seen yet. In this case, just finish the parse now.
3624 if (SeenValueSymbolTable) {
3625 NextUnreadBit = Stream.GetCurrentBitNo();
3626 // After the VST has been parsed, we need to make sure intrinsic name
3627 // are auto-upgraded.
3628 return globalCleanup();
3629 }
3630 break;
3631 case bitc::USELIST_BLOCK_ID:
3632 if (Error Err = parseUseLists())
3633 return Err;
3634 break;
3635 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3636 if (Error Err = parseOperandBundleTags())
3637 return Err;
3638 break;
3639 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID:
3640 if (Error Err = parseSyncScopeNames())
3641 return Err;
3642 break;
3643 }
3644 continue;
3645
3646 case BitstreamEntry::Record:
3647 // The interesting case.
3648 break;
3649 }
3650
3651 // Read a record.
3652 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3653 if (!MaybeBitCode)
3654 return MaybeBitCode.takeError();
3655 switch (unsigned BitCode = MaybeBitCode.get()) {
3656 default: break; // Default behavior, ignore unknown content.
3657 case bitc::MODULE_CODE_VERSION: {
3658 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
3659 if (!VersionOrErr)
3660 return VersionOrErr.takeError();
3661 UseRelativeIDs = *VersionOrErr >= 1;
3662 break;
3663 }
3664 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
3665 if (ResolvedDataLayout)
3666 return error("target triple too late in module");
3667 std::string S;
3668 if (convertToString(Record, 0, S))
3669 return error("Invalid record");
3670 TheModule->setTargetTriple(S);
3671 break;
3672 }
3673 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
3674 if (ResolvedDataLayout)
3675 return error("datalayout too late in module");
3676 std::string S;
3677 if (convertToString(Record, 0, S))
3678 return error("Invalid record");
3679 TheModule->setDataLayout(S);
3680 break;
3681 }
3682 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
3683 std::string S;
3684 if (convertToString(Record, 0, S))
3685 return error("Invalid record");
3686 TheModule->setModuleInlineAsm(S);
3687 break;
3688 }
3689 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
3690 // Deprecated, but still needed to read old bitcode files.
3691 std::string S;
3692 if (convertToString(Record, 0, S))
3693 return error("Invalid record");
3694 // Ignore value.
3695 break;
3696 }
3697 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
3698 std::string S;
3699 if (convertToString(Record, 0, S))
3700 return error("Invalid record");
3701 SectionTable.push_back(S);
3702 break;
3703 }
3704 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
3705 std::string S;
3706 if (convertToString(Record, 0, S))
3707 return error("Invalid record");
3708 GCTable.push_back(S);
3709 break;
3710 }
3711 case bitc::MODULE_CODE_COMDAT:
3712 if (Error Err = parseComdatRecord(Record))
3713 return Err;
3714 break;
3715 case bitc::MODULE_CODE_GLOBALVAR:
3716 if (Error Err = parseGlobalVarRecord(Record))
3717 return Err;
3718 break;
3719 case bitc::MODULE_CODE_FUNCTION:
3720 ResolveDataLayout();
3721 if (Error Err = parseFunctionRecord(Record))
3722 return Err;
3723 break;
3724 case bitc::MODULE_CODE_IFUNC:
3725 case bitc::MODULE_CODE_ALIAS:
3726 case bitc::MODULE_CODE_ALIAS_OLD:
3727 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
3728 return Err;
3729 break;
3730 /// MODULE_CODE_VSTOFFSET: [offset]
3731 case bitc::MODULE_CODE_VSTOFFSET:
3732 if (Record.empty())
3733 return error("Invalid record");
3734 // Note that we subtract 1 here because the offset is relative to one word
3735 // before the start of the identification or module block, which was
3736 // historically always the start of the regular bitcode header.
3737 VSTOffset = Record[0] - 1;
3738 break;
3739 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3740 case bitc::MODULE_CODE_SOURCE_FILENAME:
3741 SmallString<128> ValueName;
3742 if (convertToString(Record, 0, ValueName))
3743 return error("Invalid record");
3744 TheModule->setSourceFileName(ValueName);
3745 break;
3746 }
3747 Record.clear();
3748 }
3749}
3750
3751Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
3752 bool IsImporting,
3753 DataLayoutCallbackTy DataLayoutCallback) {
3754 TheModule = M;
3755 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting,
3756 [&](unsigned ID) { return getTypeByID(ID); });
3757 return parseModule(0, ShouldLazyLoadMetadata, DataLayoutCallback);
3758}
3759
3760Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
3761 if (!isa<PointerType>(PtrType))
3762 return error("Load/Store operand is not a pointer type");
3763 Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3764
3765 if (ValType && ValType != ElemType)
3766 return error("Explicit load/store type does not match pointee "
3767 "type of pointer operand");
3768 if (!PointerType::isLoadableOrStorableType(ElemType))
3769 return error("Cannot load/store from pointer");
3770 return Error::success();
3771}
3772
3773void BitcodeReader::propagateByValSRetTypes(CallBase *CB,
3774 ArrayRef<Type *> ArgsFullTys) {
3775 for (unsigned i = 0; i != CB->arg_size(); ++i) {
3776 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet}) {
3777 if (!CB->paramHasAttr(i, Kind))
3778 continue;
3779
3780 CB->removeParamAttr(i, Kind);
3781
3782 Type *PtrEltTy = getPointerElementFlatType(ArgsFullTys[i]);
3783 Attribute NewAttr =
3784 Kind == Attribute::ByVal
3785 ? Attribute::getWithByValType(Context, PtrEltTy)
3786 : Attribute::getWithStructRetType(Context, PtrEltTy);
3787 CB->addParamAttr(i, NewAttr);
3788 }
3789 }
3790}
3791
3792/// Lazily parse the specified function body block.
3793Error BitcodeReader::parseFunctionBody(Function *F) {
3794 if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
3795 return Err;
3796
3797 // Unexpected unresolved metadata when parsing function.
3798 if (MDLoader->hasFwdRefs())
3799 return error("Invalid function metadata: incoming forward references");
3800
3801 InstructionList.clear();
3802 unsigned ModuleValueListSize = ValueList.size();
3803 unsigned ModuleMDLoaderSize = MDLoader->size();
3804
3805 // Add all the function arguments to the value table.
3806 unsigned ArgNo = 0;
3807 FunctionType *FullFTy = FunctionTypes[F];
3808 for (Argument &I : F->args()) {
3809 assert(I.getType() == flattenPointerTypes(FullFTy->getParamType(ArgNo)) &&((I.getType() == flattenPointerTypes(FullFTy->getParamType
(ArgNo)) && "Incorrect fully specified type for Function Argument"
) ? static_cast<void> (0) : __assert_fail ("I.getType() == flattenPointerTypes(FullFTy->getParamType(ArgNo)) && \"Incorrect fully specified type for Function Argument\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3810, __PRETTY_FUNCTION__))
3810 "Incorrect fully specified type for Function Argument")((I.getType() == flattenPointerTypes(FullFTy->getParamType
(ArgNo)) && "Incorrect fully specified type for Function Argument"
) ? static_cast<void> (0) : __assert_fail ("I.getType() == flattenPointerTypes(FullFTy->getParamType(ArgNo)) && \"Incorrect fully specified type for Function Argument\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3810, __PRETTY_FUNCTION__))
;
3811 ValueList.push_back(&I, FullFTy->getParamType(ArgNo++));
3812 }
3813 unsigned NextValueNo = ValueList.size();
3814 BasicBlock *CurBB = nullptr;
3815 unsigned CurBBNo = 0;
3816
3817 DebugLoc LastLoc;
3818 auto getLastInstruction = [&]() -> Instruction * {
3819 if (CurBB && !CurBB->empty())
3820 return &CurBB->back();
3821 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
3822 !FunctionBBs[CurBBNo - 1]->empty())
3823 return &FunctionBBs[CurBBNo - 1]->back();
3824 return nullptr;
3825 };
3826
3827 std::vector<OperandBundleDef> OperandBundles;
3828
3829 // Read all the records.
3830 SmallVector<uint64_t, 64> Record;
3831
3832 while (true) {
3833 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3834 if (!MaybeEntry)
3835 return MaybeEntry.takeError();
3836 llvm::BitstreamEntry Entry = MaybeEntry.get();
3837
3838 switch (Entry.Kind) {
3839 case BitstreamEntry::Error:
3840 return error("Malformed block");
3841 case BitstreamEntry::EndBlock:
3842 goto OutOfRecordLoop;
3843
3844 case BitstreamEntry::SubBlock:
3845 switch (Entry.ID) {
3846 default: // Skip unknown content.
3847 if (Error Err = Stream.SkipBlock())
3848 return Err;
3849 break;
3850 case bitc::CONSTANTS_BLOCK_ID:
3851 if (Error Err = parseConstants())
3852 return Err;
3853 NextValueNo = ValueList.size();
3854 break;
3855 case bitc::VALUE_SYMTAB_BLOCK_ID:
3856 if (Error Err = parseValueSymbolTable())
3857 return Err;
3858 break;
3859 case bitc::METADATA_ATTACHMENT_ID:
3860 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
3861 return Err;
3862 break;
3863 case bitc::METADATA_BLOCK_ID:
3864 assert(DeferredMetadataInfo.empty() &&((DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level"
) ? static_cast<void> (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3865, __PRETTY_FUNCTION__))
3865 "Must read all module-level metadata before function-level")((DeferredMetadataInfo.empty() && "Must read all module-level metadata before function-level"
) ? static_cast<void> (0) : __assert_fail ("DeferredMetadataInfo.empty() && \"Must read all module-level metadata before function-level\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3865, __PRETTY_FUNCTION__))
;
3866 if (Error Err = MDLoader->parseFunctionMetadata())
3867 return Err;
3868 break;
3869 case bitc::USELIST_BLOCK_ID:
3870 if (Error Err = parseUseLists())
3871 return Err;
3872 break;
3873 }
3874 continue;
3875
3876 case BitstreamEntry::Record:
3877 // The interesting case.
3878 break;
3879 }
3880
3881 // Read a record.
3882 Record.clear();
3883 Instruction *I = nullptr;
3884 Type *FullTy = nullptr;
3885 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3886 if (!MaybeBitCode)
3887 return MaybeBitCode.takeError();
3888 switch (unsigned BitCode = MaybeBitCode.get()) {
3889 default: // Default behavior: reject
3890 return error("Invalid value");
3891 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
3892 if (Record.empty() || Record[0] == 0)
3893 return error("Invalid record");
3894 // Create all the basic blocks for the function.
3895 FunctionBBs.resize(Record[0]);
3896
3897 // See if anything took the address of blocks in this function.
3898 auto BBFRI = BasicBlockFwdRefs.find(F);
3899 if (BBFRI == BasicBlockFwdRefs.end()) {
3900 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
3901 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
3902 } else {
3903 auto &BBRefs = BBFRI->second;
3904 // Check for invalid basic block references.
3905 if (BBRefs.size() > FunctionBBs.size())
3906 return error("Invalid ID");
3907 assert(!BBRefs.empty() && "Unexpected empty array")((!BBRefs.empty() && "Unexpected empty array") ? static_cast
<void> (0) : __assert_fail ("!BBRefs.empty() && \"Unexpected empty array\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3907, __PRETTY_FUNCTION__))
;
3908 assert(!BBRefs.front() && "Invalid reference to entry block")((!BBRefs.front() && "Invalid reference to entry block"
) ? static_cast<void> (0) : __assert_fail ("!BBRefs.front() && \"Invalid reference to entry block\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 3908, __PRETTY_FUNCTION__))
;
3909 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
3910 ++I)
3911 if (I < RE && BBRefs[I]) {
3912 BBRefs[I]->insertInto(F);
3913 FunctionBBs[I] = BBRefs[I];
3914 } else {
3915 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
3916 }
3917
3918 // Erase from the table.
3919 BasicBlockFwdRefs.erase(BBFRI);
3920 }
3921
3922 CurBB = FunctionBBs[0];
3923 continue;
3924 }
3925
3926 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
3927 // This record indicates that the last instruction is at the same
3928 // location as the previous instruction with a location.
3929 I = getLastInstruction();
3930
3931 if (!I)
3932 return error("Invalid record");
3933 I->setDebugLoc(LastLoc);
3934 I = nullptr;
3935 continue;
3936
3937 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
3938 I = getLastInstruction();
3939 if (!I || Record.size() < 4)
3940 return error("Invalid record");
3941
3942 unsigned Line = Record[0], Col = Record[1];
3943 unsigned ScopeID = Record[2], IAID = Record[3];
3944 bool isImplicitCode = Record.size() == 5 && Record[4];
3945
3946 MDNode *Scope = nullptr, *IA = nullptr;
3947 if (ScopeID) {
3948 Scope = dyn_cast_or_null<MDNode>(
3949 MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));
3950 if (!Scope)
3951 return error("Invalid record");
3952 }
3953 if (IAID) {
3954 IA = dyn_cast_or_null<MDNode>(
3955 MDLoader->getMetadataFwdRefOrLoad(IAID - 1));
3956 if (!IA)
3957 return error("Invalid record");
3958 }
3959 LastLoc = DebugLoc::get(Line, Col, Scope, IA, isImplicitCode);
3960 I->setDebugLoc(LastLoc);
3961 I = nullptr;
3962 continue;
3963 }
3964 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
3965 unsigned OpNum = 0;
3966 Value *LHS;
3967 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3968 OpNum+1 > Record.size())
3969 return error("Invalid record");
3970
3971 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
3972 if (Opc == -1)
3973 return error("Invalid record");
3974 I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
3975 InstructionList.push_back(I);
3976 if (OpNum < Record.size()) {
3977 if (isa<FPMathOperator>(I)) {
3978 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3979 if (FMF.any())
3980 I->setFastMathFlags(FMF);
3981 }
3982 }
3983 break;
3984 }
3985 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
3986 unsigned OpNum = 0;
3987 Value *LHS, *RHS;
3988 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3989 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
3990 OpNum+1 > Record.size())
3991 return error("Invalid record");
3992
3993 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
3994 if (Opc == -1)
3995 return error("Invalid record");
3996 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3997 InstructionList.push_back(I);
3998 if (OpNum < Record.size()) {
3999 if (Opc == Instruction::Add ||
4000 Opc == Instruction::Sub ||
4001 Opc == Instruction::Mul ||
4002 Opc == Instruction::Shl) {
4003 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
4004 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
4005 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
4006 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
4007 } else if (Opc == Instruction::SDiv ||
4008 Opc == Instruction::UDiv ||
4009 Opc == Instruction::LShr ||
4010 Opc == Instruction::AShr) {
4011 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
4012 cast<BinaryOperator>(I)->setIsExact(true);
4013 } else if (isa<FPMathOperator>(I)) {
4014 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
4015 if (FMF.any())
4016 I->setFastMathFlags(FMF);
4017 }
4018
4019 }
4020 break;
4021 }
4022 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
4023 unsigned OpNum = 0;
4024 Value *Op;
4025 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4026 OpNum+2 != Record.size())
4027 return error("Invalid record");
4028
4029 FullTy = getFullyStructuredTypeByID(Record[OpNum]);
4030 Type *ResTy = flattenPointerTypes(FullTy);
4031 int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
4032 if (Opc == -1 || !ResTy)
4033 return error("Invalid record");
4034 Instruction *Temp = nullptr;
4035 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4036 if (Temp) {
4037 InstructionList.push_back(Temp);
4038 assert(CurBB && "No current BB?")((CurBB && "No current BB?") ? static_cast<void>
(0) : __assert_fail ("CurBB && \"No current BB?\"", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4038, __PRETTY_FUNCTION__))
;
4039 CurBB->getInstList().push_back(Temp);
4040 }
4041 } else {
4042 auto CastOp = (Instruction::CastOps)Opc;
4043 if (!CastInst::castIsValid(CastOp, Op, ResTy))
4044 return error("Invalid cast");
4045 I = CastInst::Create(CastOp, Op, ResTy);
4046 }
4047 InstructionList.push_back(I);
4048 break;
4049 }
4050 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
4051 case bitc::FUNC_CODE_INST_GEP_OLD:
4052 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
4053 unsigned OpNum = 0;
4054
4055 Type *Ty;
4056 bool InBounds;
4057
4058 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4059 InBounds = Record[OpNum++];
4060 FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
4061 Ty = flattenPointerTypes(FullTy);
4062 } else {
4063 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4064 Ty = nullptr;
4065 }
4066
4067 Value *BasePtr;
4068 Type *FullBaseTy = nullptr;
4069 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, &FullBaseTy))
4070 return error("Invalid record");
4071
4072 if (!Ty) {
4073 std::tie(FullTy, Ty) =
4074 getPointerElementTypes(FullBaseTy->getScalarType());
4075 } else if (Ty != getPointerElementFlatType(FullBaseTy->getScalarType()))
4076 return error(
4077 "Explicit gep type does not match pointee type of pointer operand");
4078
4079 SmallVector<Value*, 16> GEPIdx;
4080 while (OpNum != Record.size()) {
4081 Value *Op;
4082 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4083 return error("Invalid record");
4084 GEPIdx.push_back(Op);
4085 }
4086
4087 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
4088 FullTy = GetElementPtrInst::getGEPReturnType(FullTy, I, GEPIdx);
4089
4090 InstructionList.push_back(I);
4091 if (InBounds)
4092 cast<GetElementPtrInst>(I)->setIsInBounds(true);
4093 break;
4094 }
4095
4096 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
4097 // EXTRACTVAL: [opty, opval, n x indices]
4098 unsigned OpNum = 0;
4099 Value *Agg;
4100 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, &FullTy))
4101 return error("Invalid record");
4102
4103 unsigned RecSize = Record.size();
4104 if (OpNum == RecSize)
4105 return error("EXTRACTVAL: Invalid instruction with 0 indices");
4106
4107 SmallVector<unsigned, 4> EXTRACTVALIdx;
4108 for (; OpNum != RecSize; ++OpNum) {
4109 bool IsArray = FullTy->isArrayTy();
4110 bool IsStruct = FullTy->isStructTy();
4111 uint64_t Index = Record[OpNum];
4112
4113 if (!IsStruct && !IsArray)
4114 return error("EXTRACTVAL: Invalid type");
4115 if ((unsigned)Index != Index)
4116 return error("Invalid value");
4117 if (IsStruct && Index >= FullTy->getStructNumElements())
4118 return error("EXTRACTVAL: Invalid struct index");
4119 if (IsArray && Index >= FullTy->getArrayNumElements())
4120 return error("EXTRACTVAL: Invalid array index");
4121 EXTRACTVALIdx.push_back((unsigned)Index);
4122
4123 if (IsStruct)
4124 FullTy = FullTy->getStructElementType(Index);
4125 else
4126 FullTy = FullTy->getArrayElementType();
4127 }
4128
4129 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
4130 InstructionList.push_back(I);
4131 break;
4132 }
4133
4134 case bitc::FUNC_CODE_INST_INSERTVAL: {
4135 // INSERTVAL: [opty, opval, opty, opval, n x indices]
4136 unsigned OpNum = 0;
4137 Value *Agg;
4138 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, &FullTy))
4139 return error("Invalid record");
4140 Value *Val;
4141 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
4142 return error("Invalid record");
4143
4144 unsigned RecSize = Record.size();
4145 if (OpNum == RecSize)
4146 return error("INSERTVAL: Invalid instruction with 0 indices");
4147
4148 SmallVector<unsigned, 4> INSERTVALIdx;
4149 Type *CurTy = Agg->getType();
4150 for (; OpNum != RecSize; ++OpNum) {
4151 bool IsArray = CurTy->isArrayTy();
4152 bool IsStruct = CurTy->isStructTy();
4153 uint64_t Index = Record[OpNum];
4154
4155 if (!IsStruct && !IsArray)
4156 return error("INSERTVAL: Invalid type");
4157 if ((unsigned)Index != Index)
4158 return error("Invalid value");
4159 if (IsStruct && Index >= CurTy->getStructNumElements())
4160 return error("INSERTVAL: Invalid struct index");
4161 if (IsArray && Index >= CurTy->getArrayNumElements())
4162 return error("INSERTVAL: Invalid array index");
4163
4164 INSERTVALIdx.push_back((unsigned)Index);
4165 if (IsStruct)
4166 CurTy = CurTy->getStructElementType(Index);
4167 else
4168 CurTy = CurTy->getArrayElementType();
4169 }
4170
4171 if (CurTy != Val->getType())
4172 return error("Inserted value type doesn't match aggregate type");
4173
4174 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
4175 InstructionList.push_back(I);
4176 break;
4177 }
4178
4179 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
4180 // obsolete form of select
4181 // handles select i1 ... in old bitcode
4182 unsigned OpNum = 0;
4183 Value *TrueVal, *FalseVal, *Cond;
4184 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, &FullTy) ||
4185 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4186 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
4187 return error("Invalid record");
4188
4189 I = SelectInst::Create(Cond, TrueVal, FalseVal);
4190 InstructionList.push_back(I);
4191 break;
4192 }
4193
4194 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4195 // new form of select
4196 // handles select i1 or select [N x i1]
4197 unsigned OpNum = 0;
4198 Value *TrueVal, *FalseVal, *Cond;
4199 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, &FullTy) ||
4200 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4201 getValueTypePair(Record, OpNum, NextValueNo, Cond))
4202 return error("Invalid record");
4203
4204 // select condition can be either i1 or [N x i1]
4205 if (VectorType* vector_type =
4206 dyn_cast<VectorType>(Cond->getType())) {
4207 // expect <n x i1>
4208 if (vector_type->getElementType() != Type::getInt1Ty(Context))
4209 return error("Invalid type for value");
4210 } else {
4211 // expect i1
4212 if (Cond->getType() != Type::getInt1Ty(Context))
4213 return error("Invalid type for value");
4214 }
4215
4216 I = SelectInst::Create(Cond, TrueVal, FalseVal);
4217 InstructionList.push_back(I);
4218 if (OpNum < Record.size() && isa<FPMathOperator>(I)) {
4219 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
4220 if (FMF.any())
4221 I->setFastMathFlags(FMF);
4222 }
4223 break;
4224 }
4225
4226 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
4227 unsigned OpNum = 0;
4228 Value *Vec, *Idx;
4229 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, &FullTy) ||
4230 getValueTypePair(Record, OpNum, NextValueNo, Idx))
4231 return error("Invalid record");
4232 if (!Vec->getType()->isVectorTy())
4233 return error("Invalid type for value");
4234 I = ExtractElementInst::Create(Vec, Idx);
4235 FullTy = cast<VectorType>(FullTy)->getElementType();
4236 InstructionList.push_back(I);
4237 break;
4238 }
4239
4240 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
4241 unsigned OpNum = 0;
4242 Value *Vec, *Elt, *Idx;
4243 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, &FullTy))
4244 return error("Invalid record");
4245 if (!Vec->getType()->isVectorTy())
4246 return error("Invalid type for value");
4247 if (popValue(Record, OpNum, NextValueNo,
4248 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
4249 getValueTypePair(Record, OpNum, NextValueNo, Idx))
4250 return error("Invalid record");
4251 I = InsertElementInst::Create(Vec, Elt, Idx);
4252 InstructionList.push_back(I);
4253 break;
4254 }
4255
4256 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4257 unsigned OpNum = 0;
4258 Value *Vec1, *Vec2, *Mask;
4259 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, &FullTy) ||
4260 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
4261 return error("Invalid record");
4262
4263 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
4264 return error("Invalid record");
4265 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
4266 return error("Invalid type for value");
4267
4268 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
4269 FullTy =
4270 VectorType::get(cast<VectorType>(FullTy)->getElementType(),
4271 cast<VectorType>(Mask->getType())->getElementCount());
4272 InstructionList.push_back(I);
4273 break;
4274 }
4275
4276 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
4277 // Old form of ICmp/FCmp returning bool
4278 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4279 // both legal on vectors but had different behaviour.
4280 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4281 // FCmp/ICmp returning bool or vector of bool
4282
4283 unsigned OpNum = 0;
4284 Value *LHS, *RHS;
4285 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4286 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4287 return error("Invalid record");
4288
4289 if (OpNum >= Record.size())
4290 return error(
4291 "Invalid record: operand number exceeded available operands");
4292
4293 unsigned PredVal = Record[OpNum];
4294 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4295 FastMathFlags FMF;
4296 if (IsFP && Record.size() > OpNum+1)
4297 FMF = getDecodedFastMathFlags(Record[++OpNum]);
4298
4299 if (OpNum+1 != Record.size())
4300 return error("Invalid record");
4301
4302 if (LHS->getType()->isFPOrFPVectorTy())
4303 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
4304 else
4305 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4306
4307 if (FMF.any())
4308 I->setFastMathFlags(FMF);
4309 InstructionList.push_back(I);
4310 break;
4311 }
4312
4313 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
4314 {
4315 unsigned Size = Record.size();
4316 if (Size == 0) {
4317 I = ReturnInst::Create(Context);
4318 InstructionList.push_back(I);
4319 break;
4320 }
4321
4322 unsigned OpNum = 0;
4323 Value *Op = nullptr;
4324 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4325 return error("Invalid record");
4326 if (OpNum != Record.size())
4327 return error("Invalid record");
4328
4329 I = ReturnInst::Create(Context, Op);
4330 InstructionList.push_back(I);
4331 break;
4332 }
4333 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
4334 if (Record.size() != 1 && Record.size() != 3)
4335 return error("Invalid record");
4336 BasicBlock *TrueDest = getBasicBlock(Record[0]);
4337 if (!TrueDest)
4338 return error("Invalid record");
4339
4340 if (Record.size() == 1) {
4341 I = BranchInst::Create(TrueDest);
4342 InstructionList.push_back(I);
4343 }
4344 else {
4345 BasicBlock *FalseDest = getBasicBlock(Record[1]);
4346 Value *Cond = getValue(Record, 2, NextValueNo,
4347 Type::getInt1Ty(Context));
4348 if (!FalseDest || !Cond)
4349 return error("Invalid record");
4350 I = BranchInst::Create(TrueDest, FalseDest, Cond);
4351 InstructionList.push_back(I);
4352 }
4353 break;
4354 }
4355 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
4356 if (Record.size() != 1 && Record.size() != 2)
4357 return error("Invalid record");
4358 unsigned Idx = 0;
4359 Value *CleanupPad =
4360 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4361 if (!CleanupPad)
4362 return error("Invalid record");
4363 BasicBlock *UnwindDest = nullptr;
4364 if (Record.size() == 2) {
4365 UnwindDest = getBasicBlock(Record[Idx++]);
4366 if (!UnwindDest)
4367 return error("Invalid record");
4368 }
4369
4370 I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
4371 InstructionList.push_back(I);
4372 break;
4373 }
4374 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4375 if (Record.size() != 2)
4376 return error("Invalid record");
4377 unsigned Idx = 0;
4378 Value *CatchPad =
4379 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4380 if (!CatchPad)
4381 return error("Invalid record");
4382 BasicBlock *BB = getBasicBlock(Record[Idx++]);
4383 if (!BB)
4384 return error("Invalid record");
4385
4386 I = CatchReturnInst::Create(CatchPad, BB);
4387 InstructionList.push_back(I);
4388 break;
4389 }
4390 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4391 // We must have, at minimum, the outer scope and the number of arguments.
4392 if (Record.size() < 2)
4393 return error("Invalid record");
4394
4395 unsigned Idx = 0;
4396
4397 Value *ParentPad =
4398 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4399
4400 unsigned NumHandlers = Record[Idx++];
4401
4402 SmallVector<BasicBlock *, 2> Handlers;
4403 for (unsigned Op = 0; Op != NumHandlers; ++Op) {
4404 BasicBlock *BB = getBasicBlock(Record[Idx++]);
4405 if (!BB)
4406 return error("Invalid record");
4407 Handlers.push_back(BB);
4408 }
4409
4410 BasicBlock *UnwindDest = nullptr;
4411 if (Idx + 1 == Record.size()) {
4412 UnwindDest = getBasicBlock(Record[Idx++]);
4413 if (!UnwindDest)
4414 return error("Invalid record");
4415 }
4416
4417 if (Record.size() != Idx)
4418 return error("Invalid record");
4419
4420 auto *CatchSwitch =
4421 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4422 for (BasicBlock *Handler : Handlers)
4423 CatchSwitch->addHandler(Handler);
4424 I = CatchSwitch;
4425 InstructionList.push_back(I);
4426 break;
4427 }
4428 case bitc::FUNC_CODE_INST_CATCHPAD:
4429 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4430 // We must have, at minimum, the outer scope and the number of arguments.
4431 if (Record.size() < 2)
4432 return error("Invalid record");
4433
4434 unsigned Idx = 0;
4435
4436 Value *ParentPad =
4437 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4438
4439 unsigned NumArgOperands = Record[Idx++];
4440
4441 SmallVector<Value *, 2> Args;
4442 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4443 Value *Val;
4444 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4445 return error("Invalid record");
4446 Args.push_back(Val);
4447 }
4448
4449 if (Record.size() != Idx)
4450 return error("Invalid record");
4451
4452 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4453 I = CleanupPadInst::Create(ParentPad, Args);
4454 else
4455 I = CatchPadInst::Create(ParentPad, Args);
4456 InstructionList.push_back(I);
4457 break;
4458 }
4459 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4460 // Check magic
4461 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4462 // "New" SwitchInst format with case ranges. The changes to write this
4463 // format were reverted but we still recognize bitcode that uses it.
4464 // Hopefully someday we will have support for case ranges and can use
4465 // this format again.
4466
4467 Type *OpTy = getTypeByID(Record[1]);
4468 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4469
4470 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
4471 BasicBlock *Default = getBasicBlock(Record[3]);
4472 if (!OpTy || !Cond || !Default)
4473 return error("Invalid record");
4474
4475 unsigned NumCases = Record[4];
4476
4477 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4478 InstructionList.push_back(SI);
4479
4480 unsigned CurIdx = 5;
4481 for (unsigned i = 0; i != NumCases; ++i) {
4482 SmallVector<ConstantInt*, 1> CaseVals;
4483 unsigned NumItems = Record[CurIdx++];
4484 for (unsigned ci = 0; ci != NumItems; ++ci) {
4485 bool isSingleNumber = Record[CurIdx++];
4486
4487 APInt Low;
4488 unsigned ActiveWords = 1;
4489 if (ValueBitWidth > 64)
4490 ActiveWords = Record[CurIdx++];
4491 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
4492 ValueBitWidth);
4493 CurIdx += ActiveWords;
4494
4495 if (!isSingleNumber) {
4496 ActiveWords = 1;
4497 if (ValueBitWidth > 64)
4498 ActiveWords = Record[CurIdx++];
4499 APInt High = readWideAPInt(
4500 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
4501 CurIdx += ActiveWords;
4502
4503 // FIXME: It is not clear whether values in the range should be
4504 // compared as signed or unsigned values. The partially
4505 // implemented changes that used this format in the past used
4506 // unsigned comparisons.
4507 for ( ; Low.ule(High); ++Low)
4508 CaseVals.push_back(ConstantInt::get(Context, Low));
4509 } else
4510 CaseVals.push_back(ConstantInt::get(Context, Low));
4511 }
4512 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
4513 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4514 cve = CaseVals.end(); cvi != cve; ++cvi)
4515 SI->addCase(*cvi, DestBB);
4516 }
4517 I = SI;
4518 break;
4519 }
4520
4521 // Old SwitchInst format without case ranges.
4522
4523 if (Record.size() < 3 || (Record.size() & 1) == 0)
4524 return error("Invalid record");
4525 Type *OpTy = getTypeByID(Record[0]);
4526 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
4527 BasicBlock *Default = getBasicBlock(Record[2]);
4528 if (!OpTy || !Cond || !Default)
4529 return error("Invalid record");
4530 unsigned NumCases = (Record.size()-3)/2;
4531 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4532 InstructionList.push_back(SI);
4533 for (unsigned i = 0, e = NumCases; i != e; ++i) {
4534 ConstantInt *CaseVal =
4535 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4536 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
4537 if (!CaseVal || !DestBB) {
4538 delete SI;
4539 return error("Invalid record");
4540 }
4541 SI->addCase(CaseVal, DestBB);
4542 }
4543 I = SI;
4544 break;
4545 }
4546 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
4547 if (Record.size() < 2)
4548 return error("Invalid record");
4549 Type *OpTy = getTypeByID(Record[0]);
4550 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
4551 if (!OpTy || !Address)
4552 return error("Invalid record");
4553 unsigned NumDests = Record.size()-2;
4554 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
4555 InstructionList.push_back(IBI);
4556 for (unsigned i = 0, e = NumDests; i != e; ++i) {
4557 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4558 IBI->addDestination(DestBB);
4559 } else {
4560 delete IBI;
4561 return error("Invalid record");
4562 }
4563 }
4564 I = IBI;
4565 break;
4566 }
4567
4568 case bitc::FUNC_CODE_INST_INVOKE: {
4569 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4570 if (Record.size() < 4)
4571 return error("Invalid record");
4572 unsigned OpNum = 0;
4573 AttributeList PAL = getAttributes(Record[OpNum++]);
4574 unsigned CCInfo = Record[OpNum++];
4575 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4576 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
4577
4578 FunctionType *FTy = nullptr;
4579 FunctionType *FullFTy = nullptr;
4580 if ((CCInfo >> 13) & 1) {
4581 FullFTy =
4582 dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
4583 if (!FullFTy)
4584 return error("Explicit invoke type is not a function type");
4585 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4586 }
4587
4588 Value *Callee;
4589 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, &FullTy))
4590 return error("Invalid record");
4591
4592 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
4593 if (!CalleeTy)
4594 return error("Callee is not a pointer");
4595 if (!FTy) {
4596 FullFTy =
4597 dyn_cast<FunctionType>(cast<PointerType>(FullTy)->getElementType());
4598 if (!FullFTy)
4599 return error("Callee is not of pointer to function type");
4600 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4601 } else if (getPointerElementFlatType(FullTy) != FTy)
4602 return error("Explicit invoke type does not match pointee type of "
4603 "callee operand");
4604 if (Record.size() < FTy->getNumParams() + OpNum)
4605 return error("Insufficient operands to call");
4606
4607 SmallVector<Value*, 16> Ops;
4608 SmallVector<Type *, 16> ArgsFullTys;
4609 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
4610 Ops.push_back(getValue(Record, OpNum, NextValueNo,
4611 FTy->getParamType(i)));
4612 ArgsFullTys.push_back(FullFTy->getParamType(i));
4613 if (!Ops.back())
4614 return error("Invalid record");
4615 }
4616
4617 if (!FTy->isVarArg()) {
4618 if (Record.size() != OpNum)
4619 return error("Invalid record");
4620 } else {
4621 // Read type/value pairs for varargs params.
4622 while (OpNum != Record.size()) {
4623 Value *Op;
4624 Type *FullTy;
4625 if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy))
4626 return error("Invalid record");
4627 Ops.push_back(Op);
4628 ArgsFullTys.push_back(FullTy);
4629 }
4630 }
4631
4632 I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops,
4633 OperandBundles);
4634 FullTy = FullFTy->getReturnType();
4635 OperandBundles.clear();
4636 InstructionList.push_back(I);
4637 cast<InvokeInst>(I)->setCallingConv(
4638 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
4639 cast<InvokeInst>(I)->setAttributes(PAL);
4640 propagateByValSRetTypes(cast<CallBase>(I), ArgsFullTys);
4641
4642 break;
4643 }
4644 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4645 unsigned Idx = 0;
4646 Value *Val = nullptr;
4647 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4648 return error("Invalid record");
4649 I = ResumeInst::Create(Val);
4650 InstructionList.push_back(I);
4651 break;
4652 }
4653 case bitc::FUNC_CODE_INST_CALLBR: {
4654 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]
4655 unsigned OpNum = 0;
4656 AttributeList PAL = getAttributes(Record[OpNum++]);
4657 unsigned CCInfo = Record[OpNum++];
4658
4659 BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]);
4660 unsigned NumIndirectDests = Record[OpNum++];
4661 SmallVector<BasicBlock *, 16> IndirectDests;
4662 for (unsigned i = 0, e = NumIndirectDests; i != e; ++i)
4663 IndirectDests.push_back(getBasicBlock(Record[OpNum++]));
4664
4665 FunctionType *FTy = nullptr;
4666 FunctionType *FullFTy = nullptr;
4667 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
4668 FullFTy =
4669 dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
4670 if (!FullFTy)
4671 return error("Explicit call type is not a function type");
4672 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4673 }
4674
4675 Value *Callee;
4676 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, &FullTy))
4677 return error("Invalid record");
4678
4679 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
4680 if (!OpTy)
4681 return error("Callee is not a pointer type");
4682 if (!FTy) {
4683 FullFTy =
4684 dyn_cast<FunctionType>(cast<PointerType>(FullTy)->getElementType());
4685 if (!FullFTy)
4686 return error("Callee is not of pointer to function type");
4687 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4688 } else if (getPointerElementFlatType(FullTy) != FTy)
4689 return error("Explicit call type does not match pointee type of "
4690 "callee operand");
4691 if (Record.size() < FTy->getNumParams() + OpNum)
4692 return error("Insufficient operands to call");
4693
4694 SmallVector<Value*, 16> Args;
4695 // Read the fixed params.
4696 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
4697 if (FTy->getParamType(i)->isLabelTy())
4698 Args.push_back(getBasicBlock(Record[OpNum]));
4699 else
4700 Args.push_back(getValue(Record, OpNum, NextValueNo,
4701 FTy->getParamType(i)));
4702 if (!Args.back())
4703 return error("Invalid record");
4704 }
4705
4706 // Read type/value pairs for varargs params.
4707 if (!FTy->isVarArg()) {
4708 if (OpNum != Record.size())
4709 return error("Invalid record");
4710 } else {
4711 while (OpNum != Record.size()) {
4712 Value *Op;
4713 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4714 return error("Invalid record");
4715 Args.push_back(Op);
4716 }
4717 }
4718
4719 I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args,
4720 OperandBundles);
4721 FullTy = FullFTy->getReturnType();
4722 OperandBundles.clear();
4723 InstructionList.push_back(I);
4724 cast<CallBrInst>(I)->setCallingConv(
4725 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
4726 cast<CallBrInst>(I)->setAttributes(PAL);
4727 break;
4728 }
4729 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
4730 I = new UnreachableInst(Context);
4731 InstructionList.push_back(I);
4732 break;
4733 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
4734 if (Record.empty())
4735 return error("Invalid record");
4736 // The first record specifies the type.
4737 FullTy = getFullyStructuredTypeByID(Record[0]);
4738 Type *Ty = flattenPointerTypes(FullTy);
4739 if (!Ty)
4740 return error("Invalid record");
4741
4742 // Phi arguments are pairs of records of [value, basic block].
4743 // There is an optional final record for fast-math-flags if this phi has a
4744 // floating-point type.
4745 size_t NumArgs = (Record.size() - 1) / 2;
4746 PHINode *PN = PHINode::Create(Ty, NumArgs);
4747 if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN))
4748 return error("Invalid record");
4749 InstructionList.push_back(PN);
4750
4751 for (unsigned i = 0; i != NumArgs; i++) {
4752 Value *V;
4753 // With the new function encoding, it is possible that operands have
4754 // negative IDs (for forward references). Use a signed VBR
4755 // representation to keep the encoding small.
4756 if (UseRelativeIDs)
4757 V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty);
4758 else
4759 V = getValue(Record, i * 2 + 1, NextValueNo, Ty);
4760 BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]);
4761 if (!V || !BB)
4762 return error("Invalid record");
4763 PN->addIncoming(V, BB);
4764 }
4765 I = PN;
4766
4767 // If there are an even number of records, the final record must be FMF.
4768 if (Record.size() % 2 == 0) {
4769 assert(isa<FPMathOperator>(I) && "Unexpected phi type")((isa<FPMathOperator>(I) && "Unexpected phi type"
) ? static_cast<void> (0) : __assert_fail ("isa<FPMathOperator>(I) && \"Unexpected phi type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4769, __PRETTY_FUNCTION__))
;
4770 FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]);
4771 if (FMF.any())
4772 I->setFastMathFlags(FMF);
4773 }
4774
4775 break;
4776 }
4777
4778 case bitc::FUNC_CODE_INST_LANDINGPAD:
4779 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
4780 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4781 unsigned Idx = 0;
4782 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4783 if (Record.size() < 3)
4784 return error("Invalid record");
4785 } else {
4786 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD)((BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) ? static_cast
<void> (0) : __assert_fail ("BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4786, __PRETTY_FUNCTION__))
;
4787 if (Record.size() < 4)
4788 return error("Invalid record");
4789 }
4790 FullTy = getFullyStructuredTypeByID(Record[Idx++]);
4791 Type *Ty = flattenPointerTypes(FullTy);
4792 if (!Ty)
4793 return error("Invalid record");
4794 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4795 Value *PersFn = nullptr;
4796 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4797 return error("Invalid record");
4798
4799 if (!F->hasPersonalityFn())
4800 F->setPersonalityFn(cast<Constant>(PersFn));
4801 else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4802 return error("Personality function mismatch");
4803 }
4804
4805 bool IsCleanup = !!Record[Idx++];
4806 unsigned NumClauses = Record[Idx++];
4807 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
4808 LP->setCleanup(IsCleanup);
4809 for (unsigned J = 0; J != NumClauses; ++J) {
4810 LandingPadInst::ClauseType CT =
4811 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4812 Value *Val;
4813
4814 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4815 delete LP;
4816 return error("Invalid record");
4817 }
4818
4819 assert((CT != LandingPadInst::Catch ||(((CT != LandingPadInst::Catch || !isa<ArrayType>(Val->
getType())) && "Catch clause has a invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4821, __PRETTY_FUNCTION__))
4820 !isa<ArrayType>(Val->getType())) &&(((CT != LandingPadInst::Catch || !isa<ArrayType>(Val->
getType())) && "Catch clause has a invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4821, __PRETTY_FUNCTION__))
4821 "Catch clause has a invalid type!")(((CT != LandingPadInst::Catch || !isa<ArrayType>(Val->
getType())) && "Catch clause has a invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Catch || !isa<ArrayType>(Val->getType())) && \"Catch clause has a invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4821, __PRETTY_FUNCTION__))
;
4822 assert((CT != LandingPadInst::Filter ||(((CT != LandingPadInst::Filter || isa<ArrayType>(Val->
getType())) && "Filter clause has invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4824, __PRETTY_FUNCTION__))
4823 isa<ArrayType>(Val->getType())) &&(((CT != LandingPadInst::Filter || isa<ArrayType>(Val->
getType())) && "Filter clause has invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4824, __PRETTY_FUNCTION__))
4824 "Filter clause has invalid type!")(((CT != LandingPadInst::Filter || isa<ArrayType>(Val->
getType())) && "Filter clause has invalid type!") ? static_cast
<void> (0) : __assert_fail ("(CT != LandingPadInst::Filter || isa<ArrayType>(Val->getType())) && \"Filter clause has invalid type!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 4824, __PRETTY_FUNCTION__))
;
4825 LP->addClause(cast<Constant>(Val));
4826 }
4827
4828 I = LP;
4829 InstructionList.push_back(I);
4830 break;
4831 }
4832
4833 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4834 if (Record.size() != 4)
4835 return error("Invalid record");
4836 using APV = AllocaPackedValues;
4837 const uint64_t Rec = Record[3];
4838 const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec);
4839 const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec);
4840 FullTy = getFullyStructuredTypeByID(Record[0]);
4841 Type *Ty = flattenPointerTypes(FullTy);
4842 if (!Bitfield::get<APV::ExplicitType>(Rec)) {
4843 auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4844 if (!PTy)
4845 return error("Old-style alloca with a non-pointer type");
4846 std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4847 }
4848 Type *OpTy = getTypeByID(Record[1]);
4849 Value *Size = getFnValueByID(Record[2], OpTy);
4850 MaybeAlign Align;
4851 if (Error Err =
4852 parseAlignmentValue(Bitfield::get<APV::Align>(Rec), Align)) {
4853 return Err;
4854 }
4855 if (!Ty || !Size)
4856 return error("Invalid record");
4857
4858 // FIXME: Make this an optional field.
4859 const DataLayout &DL = TheModule->getDataLayout();
4860 unsigned AS = DL.getAllocaAddrSpace();
4861
4862 SmallPtrSet<Type *, 4> Visited;
4863 if (!Align && !Ty->isSized(&Visited))
4864 return error("alloca of unsized type");
4865 if (!Align)
4866 Align = DL.getPrefTypeAlign(Ty);
4867
4868 AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align);
4869 AI->setUsedWithInAlloca(InAlloca);
4870 AI->setSwiftError(SwiftError);
4871 I = AI;
4872 FullTy = PointerType::get(FullTy, AS);
4873 InstructionList.push_back(I);
4874 break;
4875 }
4876 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
4877 unsigned OpNum = 0;
4878 Value *Op;
4879 if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy) ||
4880 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
4881 return error("Invalid record");
4882
4883 if (!isa<PointerType>(Op->getType()))
4884 return error("Load operand is not a pointer type");
4885
4886 Type *Ty = nullptr;
4887 if (OpNum + 3 == Record.size()) {
4888 FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
4889 Ty = flattenPointerTypes(FullTy);
4890 } else
4891 std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4892
4893 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4894 return Err;
4895
4896 MaybeAlign Align;
4897 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4898 return Err;
4899 SmallPtrSet<Type *, 4> Visited;
4900 if (!Align && !Ty->isSized(&Visited))
4901 return error("load of unsized type");
4902 if (!Align)
4903 Align = TheModule->getDataLayout().getABITypeAlign(Ty);
4904 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align);
4905 InstructionList.push_back(I);
4906 break;
4907 }
4908 case bitc::FUNC_CODE_INST_LOADATOMIC: {
4909 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4910 unsigned OpNum = 0;
4911 Value *Op;
4912 if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy) ||
4913 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
4914 return error("Invalid record");
4915
4916 if (!isa<PointerType>(Op->getType()))
4917 return error("Load operand is not a pointer type");
4918
4919 Type *Ty = nullptr;
4920 if (OpNum + 5 == Record.size()) {
4921 FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
4922 Ty = flattenPointerTypes(FullTy);
4923 } else
4924 std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4925
4926 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4927 return Err;
4928
4929 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4930 if (Ordering == AtomicOrdering::NotAtomic ||
4931 Ordering == AtomicOrdering::Release ||
4932 Ordering == AtomicOrdering::AcquireRelease)
4933 return error("Invalid record");
4934 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4935 return error("Invalid record");
4936 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4937
4938 MaybeAlign Align;
4939 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4940 return Err;
4941 if (!Align)
4942 return error("Alignment missing from atomic load");
4943 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID);
4944 InstructionList.push_back(I);
4945 break;
4946 }
4947 case bitc::FUNC_CODE_INST_STORE:
4948 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
4949 unsigned OpNum = 0;
4950 Value *Val, *Ptr;
4951 Type *FullTy;
4952 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
4953 (BitCode == bitc::FUNC_CODE_INST_STORE
4954 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4955 : popValue(Record, OpNum, NextValueNo,
4956 getPointerElementFlatType(FullTy), Val)) ||
4957 OpNum + 2 != Record.size())
4958 return error("Invalid record");
4959
4960 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4961 return Err;
4962 MaybeAlign Align;
4963 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4964 return Err;
4965 SmallPtrSet<Type *, 4> Visited;
4966 if (!Align && !Val->getType()->isSized(&Visited))
4967 return error("store of unsized type");
4968 if (!Align)
4969 Align = TheModule->getDataLayout().getABITypeAlign(Val->getType());
4970 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align);
4971 InstructionList.push_back(I);
4972 break;
4973 }
4974 case bitc::FUNC_CODE_INST_STOREATOMIC:
4975 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
4976 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
4977 unsigned OpNum = 0;
4978 Value *Val, *Ptr;
4979 Type *FullTy;
4980 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
4981 !isa<PointerType>(Ptr->getType()) ||
4982 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4983 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4984 : popValue(Record, OpNum, NextValueNo,
4985 getPointerElementFlatType(FullTy), Val)) ||
4986 OpNum + 4 != Record.size())
4987 return error("Invalid record");
4988
4989 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4990 return Err;
4991 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4992 if (Ordering == AtomicOrdering::NotAtomic ||
4993 Ordering == AtomicOrdering::Acquire ||
4994 Ordering == AtomicOrdering::AcquireRelease)
4995 return error("Invalid record");
4996 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4997 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4998 return error("Invalid record");
4999
5000 MaybeAlign Align;
5001 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
5002 return Err;
5003 if (!Align)
5004 return error("Alignment missing from atomic store");
5005 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID);
5006 InstructionList.push_back(I);
5007 break;
5008 }
5009 case bitc::FUNC_CODE_INST_CMPXCHG_OLD: {
5010 // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope,
5011 // failure_ordering?, weak?]
5012 const size_t NumRecords = Record.size();
5013 unsigned OpNum = 0;
5014 Value *Ptr = nullptr;
5015 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy))
5016 return error("Invalid record");
5017
5018 if (!isa<PointerType>(Ptr->getType()))
5019 return error("Cmpxchg operand is not a pointer type");
5020
5021 Value *Cmp = nullptr;
5022 if (popValue(Record, OpNum, NextValueNo,
5023 getPointerElementFlatType(FullTy), Cmp))
5024 return error("Invalid record");
5025
5026 FullTy = cast<PointerType>(FullTy)->getElementType();
5027
5028 Value *New = nullptr;
5029 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
5030 NumRecords < OpNum + 3 || NumRecords > OpNum + 5)
5031 return error("Invalid record");
5032
5033 const AtomicOrdering SuccessOrdering =
5034 getDecodedOrdering(Record[OpNum + 1]);
5035 if (SuccessOrdering == AtomicOrdering::NotAtomic ||
5036 SuccessOrdering == AtomicOrdering::Unordered)
5037 return error("Invalid record");
5038
5039 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);
5040
5041 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
5042 return Err;
5043
5044 const AtomicOrdering FailureOrdering =
5045 NumRecords < 7
5046 ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering)
5047 : getDecodedOrdering(Record[OpNum + 3]);
5048
5049 const Align Alignment(
5050 TheModule->getDataLayout().getTypeStoreSize(Cmp->getType()));
5051
5052 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering,
5053 FailureOrdering, SSID);
5054 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
5055 FullTy = StructType::get(Context, {FullTy, Type::getInt1Ty(Context)});
5056
5057 if (NumRecords < 8) {
5058 // Before weak cmpxchgs existed, the instruction simply returned the
5059 // value loaded from memory, so bitcode files from that era will be
5060 // expecting the first component of a modern cmpxchg.
5061 CurBB->getInstList().push_back(I);
5062 I = ExtractValueInst::Create(I, 0);
5063 FullTy = cast<StructType>(FullTy)->getElementType(0);
5064 } else {
5065 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]);
5066 }
5067
5068 InstructionList.push_back(I);
5069 break;
5070 }
5071 case bitc::FUNC_CODE_INST_CMPXCHG: {
5072 // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope,
5073 // failure_ordering, weak]
5074 const size_t NumRecords = Record.size();
5075 unsigned OpNum = 0;
5076 Value *Ptr = nullptr;
5077 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy))
5078 return error("Invalid record");
5079
5080 if (!isa<PointerType>(Ptr->getType()))
5081 return error("Cmpxchg operand is not a pointer type");
5082
5083 Value *Cmp = nullptr;
5084 if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, &FullTy))
5085 return error("Invalid record");
5086
5087 Value *Val = nullptr;
5088 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), Val) ||
5089 NumRecords < OpNum + 3 || NumRecords > OpNum + 5)
5090 return error("Invalid record");
5091
5092 const AtomicOrdering SuccessOrdering =
5093 getDecodedOrdering(Record[OpNum + 1]);
5094 if (SuccessOrdering == AtomicOrdering::NotAtomic ||
5095 SuccessOrdering == AtomicOrdering::Unordered)
5096 return error("Invalid record");
5097
5098 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);
5099
5100 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
5101 return Err;
5102
5103 const AtomicOrdering FailureOrdering =
5104 getDecodedOrdering(Record[OpNum + 3]);
5105
5106 const Align Alignment(
5107 TheModule->getDataLayout().getTypeStoreSize(Cmp->getType()));
5108
5109 I = new AtomicCmpXchgInst(Ptr, Cmp, Val, Alignment, SuccessOrdering,
5110 FailureOrdering, SSID);
5111 FullTy = StructType::get(Context, {FullTy, Type::getInt1Ty(Context)});
5112 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
5113 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]);
5114
5115 InstructionList.push_back(I);
5116 break;
5117 }
5118 case bitc::FUNC_CODE_INST_ATOMICRMW: {
5119 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid]
5120 unsigned OpNum = 0;
5121 Value *Ptr, *Val;
5122 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
5123 !isa<PointerType>(Ptr->getType()) ||
5124 popValue(Record, OpNum, NextValueNo,
5125 getPointerElementFlatType(FullTy), Val) ||
5126 OpNum + 4 != Record.size())
5127 return error("Invalid record");
5128 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
5129 if (Operation < AtomicRMWInst::FIRST_BINOP ||
5130 Operation > AtomicRMWInst::LAST_BINOP)
5131 return error("Invalid record");
5132 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5133 if (Ordering == AtomicOrdering::NotAtomic ||
5134 Ordering == AtomicOrdering::Unordered)
5135 return error("Invalid record");
5136 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
5137 Align Alignment(
5138 TheModule->getDataLayout().getTypeStoreSize(Val->getType()));
5139 I = new AtomicRMWInst(Operation, Ptr, Val, Alignment, Ordering, SSID);
5140 FullTy = getPointerElementFlatType(FullTy);
5141 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
5142 InstructionList.push_back(I);
5143 break;
5144 }
5145 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid]
5146 if (2 != Record.size())
5147 return error("Invalid record");
5148 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
5149 if (Ordering == AtomicOrdering::NotAtomic ||
5150 Ordering == AtomicOrdering::Unordered ||
5151 Ordering == AtomicOrdering::Monotonic)
5152 return error("Invalid record");
5153 SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]);
5154 I = new FenceInst(Context, Ordering, SSID);
5155 InstructionList.push_back(I);
5156 break;
5157 }
5158 case bitc::FUNC_CODE_INST_CALL: {
5159 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
5160 if (Record.size() < 3)
5161 return error("Invalid record");
5162
5163 unsigned OpNum = 0;
5164 AttributeList PAL = getAttributes(Record[OpNum++]);
5165 unsigned CCInfo = Record[OpNum++];
5166
5167 FastMathFlags FMF;
5168 if ((CCInfo >> bitc::CALL_FMF) & 1) {
5169 FMF = getDecodedFastMathFlags(Record[OpNum++]);
5170 if (!FMF.any())
5171 return error("Fast math flags indicator set for call with no FMF");
5172 }
5173
5174 FunctionType *FTy = nullptr;
5175 FunctionType *FullFTy = nullptr;
5176 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
5177 FullFTy =
5178 dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
5179 if (!FullFTy)
5180 return error("Explicit call type is not a function type");
5181 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
5182 }
5183
5184 Value *Callee;
5185 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, &FullTy))
5186 return error("Invalid record");
5187
5188 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
5189 if (!OpTy)
5190 return error("Callee is not a pointer type");
5191 if (!FTy) {
5192 FullFTy =
5193 dyn_cast<FunctionType>(cast<PointerType>(FullTy)->getElementType());
5194 if (!FullFTy)
5195 return error("Callee is not of pointer to function type");
5196 FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
5197 } else if (getPointerElementFlatType(FullTy) != FTy)
5198 return error("Explicit call type does not match pointee type of "
5199 "callee operand");
5200 if (Record.size() < FTy->getNumParams() + OpNum)
5201 return error("Insufficient operands to call");
5202
5203 SmallVector<Value*, 16> Args;
5204 SmallVector<Type*, 16> ArgsFullTys;
5205 // Read the fixed params.
5206 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5207 if (FTy->getParamType(i)->isLabelTy())
5208 Args.push_back(getBasicBlock(Record[OpNum]));
5209 else
5210 Args.push_back(getValue(Record, OpNum, NextValueNo,
5211 FTy->getParamType(i)));
5212 ArgsFullTys.push_back(FullFTy->getParamType(i));
5213 if (!Args.back())
5214 return error("Invalid record");
5215 }
5216
5217 // Read type/value pairs for varargs params.
5218 if (!FTy->isVarArg()) {
5219 if (OpNum != Record.size())
5220 return error("Invalid record");
5221 } else {
5222 while (OpNum != Record.size()) {
5223 Value *Op;
5224 Type *FullTy;
5225 if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy))
5226 return error("Invalid record");
5227 Args.push_back(Op);
5228 ArgsFullTys.push_back(FullTy);
5229 }
5230 }
5231
5232 I = CallInst::Create(FTy, Callee, Args, OperandBundles);
5233 FullTy = FullFTy->getReturnType();
5234 OperandBundles.clear();
5235 InstructionList.push_back(I);
5236 cast<CallInst>(I)->setCallingConv(
5237 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
5238 CallInst::TailCallKind TCK = CallInst::TCK_None;
5239 if (CCInfo & 1 << bitc::CALL_TAIL)
5240 TCK = CallInst::TCK_Tail;
5241 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
5242 TCK = CallInst::TCK_MustTail;
5243 if (CCInfo & (1 << bitc::CALL_NOTAIL))
5244 TCK = CallInst::TCK_NoTail;
5245 cast<CallInst>(I)->setTailCallKind(TCK);
5246 cast<CallInst>(I)->setAttributes(PAL);
5247 propagateByValSRetTypes(cast<CallBase>(I), ArgsFullTys);
5248 if (FMF.any()) {
5249 if (!isa<FPMathOperator>(I))
5250 return error("Fast-math-flags specified for call without "
5251 "floating-point scalar or vector return type");
5252 I->setFastMathFlags(FMF);
5253 }
5254 break;
5255 }
5256 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
5257 if (Record.size() < 3)
5258 return error("Invalid record");
5259 Type *OpTy = getTypeByID(Record[0]);
5260 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
5261 FullTy = getFullyStructuredTypeByID(Record[2]);
5262 Type *ResTy = flattenPointerTypes(FullTy);
5263 if (!OpTy || !Op || !ResTy)
5264 return error("Invalid record");
5265 I = new VAArgInst(Op, ResTy);
5266 InstructionList.push_back(I);
5267 break;
5268 }
5269
5270 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
5271 // A call or an invoke can be optionally prefixed with some variable
5272 // number of operand bundle blocks. These blocks are read into
5273 // OperandBundles and consumed at the next call or invoke instruction.
5274
5275 if (Record.empty() || Record[0] >= BundleTags.size())
5276 return error("Invalid record");
5277
5278 std::vector<Value *> Inputs;
5279
5280 unsigned OpNum = 1;
5281 while (OpNum != Record.size()) {
5282 Value *Op;
5283 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5284 return error("Invalid record");
5285 Inputs.push_back(Op);
5286 }
5287
5288 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
5289 continue;
5290 }
5291
5292 case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval]
5293 unsigned OpNum = 0;
5294 Value *Op = nullptr;
5295 if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy))
5296 return error("Invalid record");
5297 if (OpNum != Record.size())
5298 return error("Invalid record");
5299
5300 I = new FreezeInst(Op);
5301 InstructionList.push_back(I);
5302 break;
5303 }
5304 }
5305
5306 // Add instruction to end of current BB. If there is no current BB, reject
5307 // this file.
5308 if (!CurBB) {
5309 I->deleteValue();
5310 return error("Invalid instruction with no BB");
5311 }
5312 if (!OperandBundles.empty()) {
5313 I->deleteValue();
5314 return error("Operand bundles found with no consumer");
5315 }
5316 CurBB->getInstList().push_back(I);
5317
5318 // If this was a terminator instruction, move to the next block.
5319 if (I->isTerminator()) {
5320 ++CurBBNo;
5321 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
5322 }
5323
5324 // Non-void values get registered in the value table for future use.
5325 if (!I->getType()->isVoidTy()) {
5326 if (!FullTy) {
5327 FullTy = I->getType();
5328 assert(((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5329 !FullTy->isPointerTy() && !isa<StructType>(FullTy) &&((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5330 !isa<ArrayType>(FullTy) &&((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5331 (!isa<VectorType>(FullTy) ||((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5332 cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() ||((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5333 cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) &&((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5334 "Structured types must be assigned with corresponding non-opaque "((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
5335 "pointer type")((!FullTy->isPointerTy() && !isa<StructType>
(FullTy) && !isa<ArrayType>(FullTy) && (
!isa<VectorType>(FullTy) || cast<VectorType>(FullTy
)->getElementType()->isFloatingPointTy() || cast<VectorType
>(FullTy)->getElementType()->isIntegerTy()) &&
"Structured types must be assigned with corresponding non-opaque "
"pointer type") ? static_cast<void> (0) : __assert_fail
("!FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && \"Structured types must be assigned with corresponding non-opaque \" \"pointer type\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5335, __PRETTY_FUNCTION__))
;
5336 }
5337
5338 assert(I->getType() == flattenPointerTypes(FullTy) &&((I->getType() == flattenPointerTypes(FullTy) && "Incorrect fully structured type provided for Instruction"
) ? static_cast<void> (0) : __assert_fail ("I->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for Instruction\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5339, __PRETTY_FUNCTION__))
5339 "Incorrect fully structured type provided for Instruction")((I->getType() == flattenPointerTypes(FullTy) && "Incorrect fully structured type provided for Instruction"
) ? static_cast<void> (0) : __assert_fail ("I->getType() == flattenPointerTypes(FullTy) && \"Incorrect fully structured type provided for Instruction\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5339, __PRETTY_FUNCTION__))
;
5340 ValueList.assignValue(I, NextValueNo++, FullTy);
5341 }
5342 }
5343
5344OutOfRecordLoop:
5345
5346 if (!OperandBundles.empty())
5347 return error("Operand bundles found with no consumer");
5348
5349 // Check the function list for unresolved values.
5350 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
5351 if (!A->getParent()) {
5352 // We found at least one unresolved value. Nuke them all to avoid leaks.
5353 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
5354 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
5355 A->replaceAllUsesWith(UndefValue::get(A->getType()));
5356 delete A;
5357 }
5358 }
5359 return error("Never resolved value found in function");
5360 }
5361 }
5362
5363 // Unexpected unresolved metadata about to be dropped.
5364 if (MDLoader->hasFwdRefs())
5365 return error("Invalid function metadata: outgoing forward refs");
5366
5367 // Trim the value list down to the size it was before we parsed this function.
5368 ValueList.shrinkTo(ModuleValueListSize);
5369 MDLoader->shrinkTo(ModuleMDLoaderSize);
5370 std::vector<BasicBlock*>().swap(FunctionBBs);
5371 return Error::success();
5372}
5373
5374/// Find the function body in the bitcode stream
5375Error BitcodeReader::findFunctionInStream(
5376 Function *F,
5377 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
5378 while (DeferredFunctionInfoIterator->second == 0) {
5379 // This is the fallback handling for the old format bitcode that
5380 // didn't contain the function index in the VST, or when we have
5381 // an anonymous function which would not have a VST entry.
5382 // Assert that we have one of those two cases.
5383 assert(VSTOffset == 0 || !F->hasName())((VSTOffset == 0 || !F->hasName()) ? static_cast<void>
(0) : __assert_fail ("VSTOffset == 0 || !F->hasName()", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5383, __PRETTY_FUNCTION__))
;
5384 // Parse the next body in the stream and set its position in the
5385 // DeferredFunctionInfo map.
5386 if (Error Err = rememberAndSkipFunctionBodies())
5387 return Err;
5388 }
5389 return Error::success();
5390}
5391
5392SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) {
5393 if (Val == SyncScope::SingleThread || Val == SyncScope::System)
5394 return SyncScope::ID(Val);
5395 if (Val >= SSIDs.size())
5396 return SyncScope::System; // Map unknown synchronization scopes to system.
5397 return SSIDs[Val];
5398}
5399
5400//===----------------------------------------------------------------------===//
5401// GVMaterializer implementation
5402//===----------------------------------------------------------------------===//
5403
5404Error BitcodeReader::materialize(GlobalValue *GV) {
5405 Function *F = dyn_cast<Function>(GV);
5406 // If it's not a function or is already material, ignore the request.
5407 if (!F || !F->isMaterializable())
5408 return Error::success();
5409
5410 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
5411 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!")((DFII != DeferredFunctionInfo.end() && "Deferred function not found!"
) ? static_cast<void> (0) : __assert_fail ("DFII != DeferredFunctionInfo.end() && \"Deferred function not found!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5411, __PRETTY_FUNCTION__))
;
5412 // If its position is recorded as 0, its body is somewhere in the stream
5413 // but we haven't seen it yet.
5414 if (DFII->second == 0)
5415 if (Error Err = findFunctionInStream(F, DFII))
5416 return Err;
5417
5418 // Materialize metadata before parsing any function bodies.
5419 if (Error Err = materializeMetadata())
5420 return Err;
5421
5422 // Move the bit stream to the saved position of the deferred function body.
5423 if (Error JumpFailed = Stream.JumpToBit(DFII->second))
5424 return JumpFailed;
5425 if (Error Err = parseFunctionBody(F))
5426 return Err;
5427 F->setIsMaterializable(false);
5428
5429 if (StripDebugInfo)
5430 stripDebugInfo(*F);
5431
5432 // Upgrade any old intrinsic calls in the function.
5433 for (auto &I : UpgradedIntrinsics) {
5434 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
5435 UI != UE;) {
5436 User *U = *UI;
5437 ++UI;
5438 if (CallInst *CI = dyn_cast<CallInst>(U))
5439 UpgradeIntrinsicCall(CI, I.second);
5440 }
5441 }
5442
5443 // Update calls to the remangled intrinsics
5444 for (auto &I : RemangledIntrinsics)
5445 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
5446 UI != UE;)
5447 // Don't expect any other users than call sites
5448 cast<CallBase>(*UI++)->setCalledFunction(I.second);
5449
5450 // Finish fn->subprogram upgrade for materialized functions.
5451 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
5452 F->setSubprogram(SP);
5453
5454 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
5455 if (!MDLoader->isStrippingTBAA()) {
5456 for (auto &I : instructions(F)) {
5457 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa);
5458 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA))
5459 continue;
5460 MDLoader->setStripTBAA(true);
5461 stripTBAA(F->getParent());
5462 }
5463 }
5464
5465 // "Upgrade" older incorrect branch weights by dropping them.
5466 for (auto &I : instructions(F)) {
5467 if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) {
5468 if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) {
5469 MDString *MDS = cast<MDString>(MD->getOperand(0));
5470 StringRef ProfName = MDS->getString();
5471 // Check consistency of !prof branch_weights metadata.
5472 if (!ProfName.equals("branch_weights"))
5473 continue;
5474 unsigned ExpectedNumOperands = 0;
5475 if (BranchInst *BI = dyn_cast<BranchInst>(&I))
5476 ExpectedNumOperands = BI->getNumSuccessors();
5477 else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
5478 ExpectedNumOperands = SI->getNumSuccessors();
5479 else if (isa<CallInst>(&I))
5480 ExpectedNumOperands = 1;
5481 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
5482 ExpectedNumOperands = IBI->getNumDestinations();
5483 else if (isa<SelectInst>(&I))
5484 ExpectedNumOperands = 2;
5485 else
5486 continue; // ignore and continue.
5487
5488 // If branch weight doesn't match, just strip branch weight.
5489 if (MD->getNumOperands() != 1 + ExpectedNumOperands)
5490 I.setMetadata(LLVMContext::MD_prof, nullptr);
5491 }
5492 }
5493 }
5494
5495 // Look for functions that rely on old function attribute behavior.
5496 UpgradeFunctionAttributes(*F);
5497
5498 // Bring in any functions that this function forward-referenced via
5499 // blockaddresses.
5500 return materializeForwardReferencedFunctions();
5501}
5502
5503Error BitcodeReader::materializeModule() {
5504 if (Error Err = materializeMetadata())
5505 return Err;
5506
5507 // Promise to materialize all forward references.
5508 WillMaterializeAllForwardRefs = true;
5509
5510 // Iterate over the module, deserializing any functions that are still on
5511 // disk.
5512 for (Function &F : *TheModule) {
5513 if (Error Err = materialize(&F))
5514 return Err;
5515 }
5516 // At this point, if there are any function bodies, parse the rest of
5517 // the bits in the module past the last function block we have recorded
5518 // through either lazy scanning or the VST.
5519 if (LastFunctionBlockBit || NextUnreadBit)
5520 if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit
5521 ? LastFunctionBlockBit
5522 : NextUnreadBit))
5523 return Err;
5524
5525 // Check that all block address forward references got resolved (as we
5526 // promised above).
5527 if (!BasicBlockFwdRefs.empty())
5528 return error("Never resolved function from blockaddress");
5529
5530 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5531 // delete the old functions to clean up. We can't do this unless the entire
5532 // module is materialized because there could always be another function body
5533 // with calls to the old function.
5534 for (auto &I : UpgradedIntrinsics) {
5535 for (auto *U : I.first->users()) {
5536 if (CallInst *CI = dyn_cast<CallInst>(U))
5537 UpgradeIntrinsicCall(CI, I.second);
5538 }
5539 if (!I.first->use_empty())
5540 I.first->replaceAllUsesWith(I.second);
5541 I.first->eraseFromParent();
5542 }
5543 UpgradedIntrinsics.clear();
5544 // Do the same for remangled intrinsics
5545 for (auto &I : RemangledIntrinsics) {
5546 I.first->replaceAllUsesWith(I.second);
5547 I.first->eraseFromParent();
5548 }
5549 RemangledIntrinsics.clear();
5550
5551 UpgradeDebugInfo(*TheModule);
5552
5553 UpgradeModuleFlags(*TheModule);
5554
5555 UpgradeARCRuntime(*TheModule);
5556
5557 return Error::success();
5558}
5559
5560std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
5561 return IdentifiedStructTypes;
5562}
5563
5564ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5565 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,
5566 StringRef ModulePath, unsigned ModuleId)
5567 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),
5568 ModulePath(ModulePath), ModuleId(ModuleId) {}
5569
5570void ModuleSummaryIndexBitcodeReader::addThisModule() {
5571 TheIndex.addModule(ModulePath, ModuleId);
5572}
5573
5574ModuleSummaryIndex::ModuleInfo *
5575ModuleSummaryIndexBitcodeReader::getThisModule() {
5576 return TheIndex.getModule(ModulePath);
5577}
5578
5579std::pair<ValueInfo, GlobalValue::GUID>
5580ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) {
5581 auto VGI = ValueIdToValueInfoMap[ValueId];
5582 assert(VGI.first)((VGI.first) ? static_cast<void> (0) : __assert_fail ("VGI.first"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5582, __PRETTY_FUNCTION__))
;
5583 return VGI;
5584}
5585
5586void ModuleSummaryIndexBitcodeReader::setValueGUID(
5587 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage,
5588 StringRef SourceFileName) {
5589 std::string GlobalId =
5590 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
5591 auto ValueGUID = GlobalValue::getGUID(GlobalId);
5592 auto OriginalNameID = ValueGUID;
5593 if (GlobalValue::isLocalLinkage(Linkage))
5594 OriginalNameID = GlobalValue::getGUID(ValueName);
5595 if (PrintSummaryGUIDs)
5596 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
5597 << ValueName << "\n";
5598
5599 // UseStrtab is false for legacy summary formats and value names are
5600 // created on stack. In that case we save the name in a string saver in
5601 // the index so that the value name can be recorded.
5602 ValueIdToValueInfoMap[ValueID] = std::make_pair(
5603 TheIndex.getOrInsertValueInfo(
5604 ValueGUID,
5605 UseStrtab ? ValueName : TheIndex.saveString(ValueName)),
5606 OriginalNameID);
5607}
5608
5609// Specialized value symbol table parser used when reading module index
5610// blocks where we don't actually create global values. The parsed information
5611// is saved in the bitcode reader for use when later parsing summaries.
5612Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
5613 uint64_t Offset,
5614 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {
5615 // With a strtab the VST is not required to parse the summary.
5616 if (UseStrtab)
5617 return Error::success();
5618
5619 assert(Offset > 0 && "Expected non-zero VST offset")((Offset > 0 && "Expected non-zero VST offset") ? static_cast
<void> (0) : __assert_fail ("Offset > 0 && \"Expected non-zero VST offset\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5619, __PRETTY_FUNCTION__))
;
5620 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
5621 if (!MaybeCurrentBit)
5622 return MaybeCurrentBit.takeError();
5623 uint64_t CurrentBit = MaybeCurrentBit.get();
5624
5625 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5626 return Err;
5627
5628 SmallVector<uint64_t, 64> Record;
5629
5630 // Read all the records for this value table.
5631 SmallString<128> ValueName;
5632
5633 while (true) {
5634 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
5635 if (!MaybeEntry)
5636 return MaybeEntry.takeError();
5637 BitstreamEntry Entry = MaybeEntry.get();
5638
5639 switch (Entry.Kind) {
5640 case BitstreamEntry::SubBlock: // Handled for us already.
5641 case BitstreamEntry::Error:
5642 return error("Malformed block");
5643 case BitstreamEntry::EndBlock:
5644 // Done parsing VST, jump back to wherever we came from.
5645 if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
5646 return JumpFailed;
5647 return Error::success();
5648 case BitstreamEntry::Record:
5649 // The interesting case.
5650 break;
5651 }
5652
5653 // Read a record.
5654 Record.clear();
5655 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
5656 if (!MaybeRecord)
5657 return MaybeRecord.takeError();
5658 switch (MaybeRecord.get()) {
5659 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5660 break;
5661 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
5662 if (convertToString(Record, 1, ValueName))
5663 return error("Invalid record");
5664 unsigned ValueID = Record[0];
5665 assert(!SourceFileName.empty())((!SourceFileName.empty()) ? static_cast<void> (0) : __assert_fail
("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5665, __PRETTY_FUNCTION__))
;
5666 auto VLI = ValueIdToLinkageMap.find(ValueID);
5667 assert(VLI != ValueIdToLinkageMap.end() &&((VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?"
) ? static_cast<void> (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5668, __PRETTY_FUNCTION__))
5668 "No linkage found for VST entry?")((VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?"
) ? static_cast<void> (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5668, __PRETTY_FUNCTION__))
;
5669 auto Linkage = VLI->second;
5670 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
5671 ValueName.clear();
5672 break;
5673 }
5674 case bitc::VST_CODE_FNENTRY: {
5675 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
5676 if (convertToString(Record, 2, ValueName))
5677 return error("Invalid record");
5678 unsigned ValueID = Record[0];
5679 assert(!SourceFileName.empty())((!SourceFileName.empty()) ? static_cast<void> (0) : __assert_fail
("!SourceFileName.empty()", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5679, __PRETTY_FUNCTION__))
;
5680 auto VLI = ValueIdToLinkageMap.find(ValueID);
5681 assert(VLI != ValueIdToLinkageMap.end() &&((VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?"
) ? static_cast<void> (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5682, __PRETTY_FUNCTION__))
5682 "No linkage found for VST entry?")((VLI != ValueIdToLinkageMap.end() && "No linkage found for VST entry?"
) ? static_cast<void> (0) : __assert_fail ("VLI != ValueIdToLinkageMap.end() && \"No linkage found for VST entry?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5682, __PRETTY_FUNCTION__))
;
5683 auto Linkage = VLI->second;
5684 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
5685 ValueName.clear();
5686 break;
5687 }
5688 case bitc::VST_CODE_COMBINED_ENTRY: {
5689 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
5690 unsigned ValueID = Record[0];
5691 GlobalValue::GUID RefGUID = Record[1];
5692 // The "original name", which is the second value of the pair will be
5693 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5694 ValueIdToValueInfoMap[ValueID] =
5695 std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID);
5696 break;
5697 }
5698 }
5699 }
5700}
5701
5702// Parse just the blocks needed for building the index out of the module.
5703// At the end of this routine the module Index is populated with a map
5704// from global value id to GlobalValueSummary objects.
5705Error ModuleSummaryIndexBitcodeReader::parseModule() {
5706 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5707 return Err;
5708
5709 SmallVector<uint64_t, 64> Record;
5710 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;
5711 unsigned ValueId = 0;
5712
5713 // Read the index for this module.
5714 while (true) {
5715 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5716 if (!MaybeEntry)
5717 return MaybeEntry.takeError();
5718 llvm::BitstreamEntry Entry = MaybeEntry.get();
5719
5720 switch (Entry.Kind) {
5721 case BitstreamEntry::Error:
5722 return error("Malformed block");
5723 case BitstreamEntry::EndBlock:
5724 return Error::success();
5725
5726 case BitstreamEntry::SubBlock:
5727 switch (Entry.ID) {
5728 default: // Skip unknown content.
5729 if (Error Err = Stream.SkipBlock())
5730 return Err;
5731 break;
5732 case bitc::BLOCKINFO_BLOCK_ID:
5733 // Need to parse these to get abbrev ids (e.g. for VST)
5734 if (readBlockInfo())
5735 return error("Malformed block");
5736 break;
5737 case bitc::VALUE_SYMTAB_BLOCK_ID:
5738 // Should have been parsed earlier via VSTOffset, unless there
5739 // is no summary section.
5740 assert(((SeenValueSymbolTable && VSTOffset > 0) ||((((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary
) && "Expected early VST parse via VSTOffset record")
? static_cast<void> (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5742, __PRETTY_FUNCTION__))
5741 !SeenGlobalValSummary) &&((((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary
) && "Expected early VST parse via VSTOffset record")
? static_cast<void> (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5742, __PRETTY_FUNCTION__))
5742 "Expected early VST parse via VSTOffset record")((((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary
) && "Expected early VST parse via VSTOffset record")
? static_cast<void> (0) : __assert_fail ("((SeenValueSymbolTable && VSTOffset > 0) || !SeenGlobalValSummary) && \"Expected early VST parse via VSTOffset record\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5742, __PRETTY_FUNCTION__))
;
5743 if (Error Err = Stream.SkipBlock())
5744 return Err;
5745 break;
5746 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
5747 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
5748 // Add the module if it is a per-module index (has a source file name).
5749 if (!SourceFileName.empty())
5750 addThisModule();
5751 assert(!SeenValueSymbolTable &&((!SeenValueSymbolTable && "Already read VST when parsing summary block?"
) ? static_cast<void> (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5752, __PRETTY_FUNCTION__))
5752 "Already read VST when parsing summary block?")((!SeenValueSymbolTable && "Already read VST when parsing summary block?"
) ? static_cast<void> (0) : __assert_fail ("!SeenValueSymbolTable && \"Already read VST when parsing summary block?\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5752, __PRETTY_FUNCTION__))
;
5753 // We might not have a VST if there were no values in the
5754 // summary. An empty summary block generated when we are
5755 // performing ThinLTO compiles so we don't later invoke
5756 // the regular LTO process on them.
5757 if (VSTOffset > 0) {
5758 if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap))
5759 return Err;
5760 SeenValueSymbolTable = true;
5761 }
5762 SeenGlobalValSummary = true;
5763 if (Error Err = parseEntireSummary(Entry.ID))
5764 return Err;
5765 break;
5766 case bitc::MODULE_STRTAB_BLOCK_ID:
5767 if (Error Err = parseModuleStringTable())
5768 return Err;
5769 break;
5770 }
5771 continue;
5772
5773 case BitstreamEntry::Record: {
5774 Record.clear();
5775 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
5776 if (!MaybeBitCode)
5777 return MaybeBitCode.takeError();
5778 switch (MaybeBitCode.get()) {
5779 default:
5780 break; // Default behavior, ignore unknown content.
5781 case bitc::MODULE_CODE_VERSION: {
5782 if (Error Err = parseVersionRecord(Record).takeError())
5783 return Err;
5784 break;
5785 }
5786 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5787 case bitc::MODULE_CODE_SOURCE_FILENAME: {
5788 SmallString<128> ValueName;
5789 if (convertToString(Record, 0, ValueName))
5790 return error("Invalid record");
5791 SourceFileName = ValueName.c_str();
5792 break;
5793 }
5794 /// MODULE_CODE_HASH: [5*i32]
5795 case bitc::MODULE_CODE_HASH: {
5796 if (Record.size() != 5)
5797 return error("Invalid hash length " + Twine(Record.size()).str());
5798 auto &Hash = getThisModule()->second.second;
5799 int Pos = 0;
5800 for (auto &Val : Record) {
5801 assert(!(Val >> 32) && "Unexpected high bits set")((!(Val >> 32) && "Unexpected high bits set") ?
static_cast<void> (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5801, __PRETTY_FUNCTION__))
;
5802 Hash[Pos++] = Val;
5803 }
5804 break;
5805 }
5806 /// MODULE_CODE_VSTOFFSET: [offset]
5807 case bitc::MODULE_CODE_VSTOFFSET:
5808 if (Record.empty())
5809 return error("Invalid record");
5810 // Note that we subtract 1 here because the offset is relative to one
5811 // word before the start of the identification or module block, which
5812 // was historically always the start of the regular bitcode header.
5813 VSTOffset = Record[0] - 1;
5814 break;
5815 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
5816 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
5817 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
5818 // v2: [strtab offset, strtab size, v1]
5819 case bitc::MODULE_CODE_GLOBALVAR:
5820 case bitc::MODULE_CODE_FUNCTION:
5821 case bitc::MODULE_CODE_ALIAS: {
5822 StringRef Name;
5823 ArrayRef<uint64_t> GVRecord;
5824 std::tie(Name, GVRecord) = readNameFromStrtab(Record);
5825 if (GVRecord.size() <= 3)
5826 return error("Invalid record");
5827 uint64_t RawLinkage = GVRecord[3];
5828 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5829 if (!UseStrtab) {
5830 ValueIdToLinkageMap[ValueId++] = Linkage;
5831 break;
5832 }
5833
5834 setValueGUID(ValueId++, Name, Linkage, SourceFileName);
5835 break;
5836 }
5837 }
5838 }
5839 continue;
5840 }
5841 }
5842}
5843
5844std::vector<ValueInfo>
5845ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {
5846 std::vector<ValueInfo> Ret;
5847 Ret.reserve(Record.size());
5848 for (uint64_t RefValueId : Record)
5849 Ret.push_back(getValueInfoFromValueId(RefValueId).first);
5850 return Ret;
5851}
5852
5853std::vector<FunctionSummary::EdgeTy>
5854ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
5855 bool IsOldProfileFormat,
5856 bool HasProfile, bool HasRelBF) {
5857 std::vector<FunctionSummary::EdgeTy> Ret;
5858 Ret.reserve(Record.size());
5859 for (unsigned I = 0, E = Record.size(); I != E; ++I) {
5860 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
5861 uint64_t RelBF = 0;
5862 ValueInfo Callee = getValueInfoFromValueId(Record[I]).first;
5863 if (IsOldProfileFormat) {
5864 I += 1; // Skip old callsitecount field
5865 if (HasProfile)
5866 I += 1; // Skip old profilecount field
5867 } else if (HasProfile)
5868 Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]);
5869 else if (HasRelBF)
5870 RelBF = Record[++I];
5871 Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)});
5872 }
5873 return Ret;
5874}
5875
5876static void
5877parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot,
5878 WholeProgramDevirtResolution &Wpd) {
5879 uint64_t ArgNum = Record[Slot++];
5880 WholeProgramDevirtResolution::ByArg &B =
5881 Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}];
5882 Slot += ArgNum;
5883
5884 B.TheKind =
5885 static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]);
5886 B.Info = Record[Slot++];
5887 B.Byte = Record[Slot++];
5888 B.Bit = Record[Slot++];
5889}
5890
5891static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record,
5892 StringRef Strtab, size_t &Slot,
5893 TypeIdSummary &TypeId) {
5894 uint64_t Id = Record[Slot++];
5895 WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];
5896
5897 Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]);
5898 Wpd.SingleImplName = {Strtab.data() + Record[Slot],
5899 static_cast<size_t>(Record[Slot + 1])};
5900 Slot += 2;
5901
5902 uint64_t ResByArgNum = Record[Slot++];
5903 for (uint64_t I = 0; I != ResByArgNum; ++I)
5904 parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd);
5905}
5906
5907static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record,
5908 StringRef Strtab,
5909 ModuleSummaryIndex &TheIndex) {
5910 size_t Slot = 0;
5911 TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(
5912 {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])});
5913 Slot += 2;
5914
5915 TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]);
5916 TypeId.TTRes.SizeM1BitWidth = Record[Slot++];
5917 TypeId.TTRes.AlignLog2 = Record[Slot++];
5918 TypeId.TTRes.SizeM1 = Record[Slot++];
5919 TypeId.TTRes.BitMask = Record[Slot++];
5920 TypeId.TTRes.InlineBits = Record[Slot++];
5921
5922 while (Slot < Record.size())
5923 parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId);
5924}
5925
5926std::vector<FunctionSummary::ParamAccess>
5927ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) {
5928 auto ReadRange = [&]() {
5929 APInt Lower(FunctionSummary::ParamAccess::RangeWidth,
5930 BitcodeReader::decodeSignRotatedValue(Record.front()));
5931 Record = Record.drop_front();
5932 APInt Upper(FunctionSummary::ParamAccess::RangeWidth,
5933 BitcodeReader::decodeSignRotatedValue(Record.front()));
5934 Record = Record.drop_front();
5935 ConstantRange Range{Lower, Upper};
5936 assert(!Range.isFullSet())((!Range.isFullSet()) ? static_cast<void> (0) : __assert_fail
("!Range.isFullSet()", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5936, __PRETTY_FUNCTION__))
;
5937 assert(!Range.isUpperSignWrapped())((!Range.isUpperSignWrapped()) ? static_cast<void> (0) :
__assert_fail ("!Range.isUpperSignWrapped()", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Bitcode/Reader/BitcodeReader.cpp"
, 5937, __PRETTY_FUNCTION__))
;
5938 return Range;
5939 };
5940
5941 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses;
5942 while (!Record.empty()) {
5943 PendingParamAccesses.emplace_back();
5944 FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back();
5945 ParamAccess.ParamNo = Record.front();
5946 Record = Record.drop_front();
5947 ParamAccess.Use = ReadRange();
5948 ParamAccess.Calls.resize(Record.front());
5949 Record = Record.drop_front();
5950 for (auto &Call : ParamAccess.Calls) {
5951 Call.ParamNo = Record.front();
5952 Record = Record.drop_front();
5953 Call.Callee = getValueInfoFromValueId(Record.front()).first;
5954 Record = Record.drop_front();
5955 Call.Offsets = ReadRange();
5956 }
5957 }
5958 return PendingParamAccesses;
5959}
5960
5961void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo(
5962 ArrayRef<uint64_t> Record, size_t &Slot,
5963 TypeIdCompatibleVtableInfo &TypeId) {
5964 uint64_t Offset = Record[Slot++];
5965 ValueInfo Callee = getValueInfoFromValueId(Record[Slot++]).first;
5966 TypeId.push_back({Offset, Callee});
5967}
5968
5969void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(
5970 ArrayRef<uint64_t> Record) {
5971 size_t Slot = 0;
5972 TypeIdCompatibleVtableInfo &TypeId =
5973 TheIndex.getOrInsertTypeIdCompatibleVtableSummary(
5974 {Strtab.data() + Record[Slot],
5975 static_cast<size_t>(Record[Slot + 1])});
5976 Slot += 2;
5977