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