Bug Summary

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