Bug Summary

File:lib/Bitcode/Reader/BitcodeReader.cpp
Warning:line 4503, column 9
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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