Coverage Report

Created: 2017-10-03 07:32

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