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