Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/Bitcode/BitcodeReader.h"
10
#include "MetadataLoader.h"
11
#include "ValueList.h"
12
#include "llvm/ADT/APFloat.h"
13
#include "llvm/ADT/APInt.h"
14
#include "llvm/ADT/ArrayRef.h"
15
#include "llvm/ADT/DenseMap.h"
16
#include "llvm/ADT/Optional.h"
17
#include "llvm/ADT/STLExtras.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/StringRef.h"
21
#include "llvm/ADT/Triple.h"
22
#include "llvm/ADT/Twine.h"
23
#include "llvm/Bitstream/BitstreamReader.h"
24
#include "llvm/Bitcode/LLVMBitCodes.h"
25
#include "llvm/Config/llvm-config.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
59
static Error error(const Twine &Message) {
104
59
  return make_error<StringError>(
105
59
      Message, make_error_code(BitcodeError::CorruptedBitcode));
106
59
}
107
108
9.12k
static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) {
109
9.12k
  if (!Stream.canSkipToPos(4))
110
2
    return createStringError(std::errc::illegal_byte_sequence,
111
2
                             "file too small to contain bitcode header");
112
9.12k
  for (unsigned C : {'B', 'C'})
113
18.2k
    if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
114
18.2k
      if (Res.get() != C)
115
1
        return createStringError(std::errc::illegal_byte_sequence,
116
1
                                 "file doesn't start with bitcode header");
117
0
    } else
118
0
      return Res.takeError();
119
9.12k
  
for (unsigned C : {0x0, 0xC, 0xE, 0xD})9.12k
120
36.5k
    if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) {
121
36.5k
      if (Res.get() != C)
122
0
        return createStringError(std::errc::illegal_byte_sequence,
123
0
                                 "file doesn't start with bitcode header");
124
0
    } else
125
0
      return Res.takeError();
126
9.12k
  return Error::success();
127
9.12k
}
128
129
9.12k
static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) {
130
9.12k
  const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
131
9.12k
  const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
132
9.12k
133
9.12k
  if (Buffer.getBufferSize() & 3)
134
0
    return error("Invalid bitcode signature");
135
9.12k
136
9.12k
  // If we have a wrapper header, parse it and ignore the non-bc file contents.
137
9.12k
  // The magic number is 0x0B17C0DE stored in little endian.
138
9.12k
  if (isBitcodeWrapper(BufPtr, BufEnd))
139
558
    if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
140
0
      return error("Invalid bitcode wrapper header");
141
9.12k
142
9.12k
  BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
143
9.12k
  if (Error Err = hasInvalidBitcodeHeader(Stream))
144
3
    return std::move(Err);
145
9.12k
146
9.12k
  return std::move(Stream);
147
9.12k
}
148
149
/// Convert a string from a record into an std::string, return true on failure.
150
template <typename StrTy>
151
static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
152
8.97M
                            StrTy &Result) {
153
8.97M
  if (Idx > Record.size())
154
0
    return true;
155
8.97M
156
122M
  
for (unsigned i = Idx, e = Record.size(); 8.97M
i != e;
++i113M
)
157
113M
    Result += (char)Record[i];
158
8.97M
  return false;
159
8.97M
}
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
152
43.3k
                            StrTy &Result) {
153
43.3k
  if (Idx > Record.size())
154
0
    return true;
155
43.3k
156
775k
  
for (unsigned i = Idx, e = Record.size(); 43.3k
i != e;
++i732k
)
157
732k
    Result += (char)Record[i];
158
43.3k
  return false;
159
43.3k
}
BitcodeReader.cpp:bool convertToString<llvm::SmallString<128u> >(llvm::ArrayRef<unsigned long long>, unsigned int, llvm::SmallString<128u>&)
Line
Count
Source
152
8.83M
                            StrTy &Result) {
153
8.83M
  if (Idx > Record.size())
154
0
    return true;
155
8.83M
156
119M
  
for (unsigned i = Idx, e = Record.size(); 8.83M
i != e;
++i110M
)
157
110M
    Result += (char)Record[i];
158
8.83M
  return false;
159
8.83M
}
BitcodeReader.cpp:bool convertToString<llvm::SmallString<64u> >(llvm::ArrayRef<unsigned long long>, unsigned int, llvm::SmallString<64u>&)
Line
Count
Source
152
89.0k
                            StrTy &Result) {
153
89.0k
  if (Idx > Record.size())
154
0
    return true;
155
89.0k
156
2.72M
  
for (unsigned i = Idx, e = Record.size(); 89.0k
i != e;
++i2.63M
)
157
2.63M
    Result += (char)Record[i];
158
89.0k
  return false;
159
89.0k
}
BitcodeReader.cpp:bool convertToString<llvm::SmallString<16u> >(llvm::ArrayRef<unsigned long long>, unsigned int, llvm::SmallString<16u>&)
Line
Count
Source
152
6.94k
                            StrTy &Result) {
153
6.94k
  if (Idx > Record.size())
154
0
    return true;
155
6.94k
156
48.5k
  
for (unsigned i = Idx, e = Record.size(); 6.94k
i != e;
++i41.6k
)
157
41.6k
    Result += (char)Record[i];
158
6.94k
  return false;
159
6.94k
}
160
161
// Strip all the TBAA attachment for the module.
162
2
static void stripTBAA(Module *M) {
163
4
  for (auto &F : *M) {
164
4
    if (F.isMaterializable())
165
2
      continue;
166
2
    for (auto &I : instructions(F))
167
16
      I.setMetadata(LLVMContext::MD_tbaa, nullptr);
168
2
  }
169
2
}
170
171
/// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
172
/// "epoch" encoded in the bitcode, and return the producer name if any.
173
7.66k
static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
174
7.66k
  if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
175
0
    return std::move(Err);
176
7.66k
177
7.66k
  // Read all the records.
178
7.66k
  SmallVector<uint64_t, 64> Record;
179
7.66k
180
7.66k
  std::string ProducerIdentification;
181
7.66k
182
22.9k
  while (true) {
183
22.9k
    BitstreamEntry Entry;
184
22.9k
    if (Expected<BitstreamEntry> Res = Stream.advance())
185
22.9k
      Entry = Res.get();
186
18.4E
    else
187
18.4E
      return Res.takeError();
188
22.9k
189
22.9k
    switch (Entry.Kind) {
190
22.9k
    default:
191
0
    case BitstreamEntry::Error:
192
0
      return error("Malformed block");
193
7.65k
    case BitstreamEntry::EndBlock:
194
7.65k
      return ProducerIdentification;
195
15.3k
    case BitstreamEntry::Record:
196
15.3k
      // The interesting case.
197
15.3k
      break;
198
15.3k
    }
199
15.3k
200
15.3k
    // Read a record.
201
15.3k
    Record.clear();
202
15.3k
    Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
203
15.3k
    if (!MaybeBitCode)
204
0
      return MaybeBitCode.takeError();
205
15.3k
    switch (MaybeBitCode.get()) {
206
15.3k
    default: // Default behavior: reject
207
0
      return error("Invalid value");
208
15.3k
    case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
209
7.65k
      convertToString(Record, 0, ProducerIdentification);
210
7.65k
      break;
211
15.3k
    case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
212
7.66k
      unsigned epoch = (unsigned)Record[0];
213
7.66k
      if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
214
0
        return error(
215
0
          Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
216
0
          "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
217
0
      }
218
7.66k
    }
219
15.3k
    }
220
15.3k
  }
221
7.66k
}
222
223
0
static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
224
0
  // We expect a number of well-defined blocks, though we don't necessarily
225
0
  // need to understand them all.
226
0
  while (true) {
227
0
    if (Stream.AtEndOfStream())
228
0
      return "";
229
0
230
0
    BitstreamEntry Entry;
231
0
    if (Expected<BitstreamEntry> Res = Stream.advance())
232
0
      Entry = std::move(Res.get());
233
0
    else
234
0
      return Res.takeError();
235
0
236
0
    switch (Entry.Kind) {
237
0
    case BitstreamEntry::EndBlock:
238
0
    case BitstreamEntry::Error:
239
0
      return error("Malformed block");
240
0
241
0
    case BitstreamEntry::SubBlock:
242
0
      if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
243
0
        return readIdentificationBlock(Stream);
244
0
245
0
      // Ignore other sub-blocks.
246
0
      if (Error Err = Stream.SkipBlock())
247
0
        return std::move(Err);
248
0
      continue;
249
0
    case BitstreamEntry::Record:
250
0
      if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
251
0
        continue;
252
0
      else
253
0
        return Skipped.takeError();
254
0
    }
255
0
  }
256
0
}
257
258
2
static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
259
2
  if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
260
0
    return std::move(Err);
261
2
262
2
  SmallVector<uint64_t, 64> Record;
263
2
  // Read all the records for this module.
264
2
265
15
  while (true) {
266
15
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
267
15
    if (!MaybeEntry)
268
0
      return MaybeEntry.takeError();
269
15
    BitstreamEntry Entry = MaybeEntry.get();
270
15
271
15
    switch (Entry.Kind) {
272
15
    case BitstreamEntry::SubBlock: // Handled for us already.
273
0
    case BitstreamEntry::Error:
274
0
      return error("Malformed block");
275
0
    case BitstreamEntry::EndBlock:
276
0
      return false;
277
15
    case BitstreamEntry::Record:
278
15
      // The interesting case.
279
15
      break;
280
15
    }
281
15
282
15
    // Read a record.
283
15
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
284
15
    if (!MaybeRecord)
285
0
      return MaybeRecord.takeError();
286
15
    switch (MaybeRecord.get()) {
287
15
    default:
288
7
      break; // Default behavior, ignore unknown content.
289
15
    case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
290
8
      std::string S;
291
8
      if (convertToString(Record, 0, S))
292
0
        return error("Invalid record");
293
8
      // Check for the i386 and other (x86_64, ARM) conventions
294
8
      if (S.find("__DATA,__objc_catlist") != std::string::npos ||
295
8
          
S.find("__OBJC,__category") != std::string::npos7
)
296
2
        return true;
297
6
      break;
298
6
    }
299
13
    }
300
13
    Record.clear();
301
13
  }
302
2
  
llvm_unreachable0
("Exit infinite loop");
303
2
}
304
305
2
static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
306
2
  // We expect a number of well-defined blocks, though we don't necessarily
307
2
  // need to understand them all.
308
4
  while (true) {
309
4
    BitstreamEntry Entry;
310
4
    if (Expected<BitstreamEntry> Res = Stream.advance())
311
4
      Entry = std::move(Res.get());
312
0
    else
313
0
      return Res.takeError();
314
4
315
4
    switch (Entry.Kind) {
316
4
    case BitstreamEntry::Error:
317
0
      return error("Malformed block");
318
4
    case BitstreamEntry::EndBlock:
319
0
      return false;
320
4
321
4
    case BitstreamEntry::SubBlock:
322
4
      if (Entry.ID == bitc::MODULE_BLOCK_ID)
323
2
        return hasObjCCategoryInModule(Stream);
324
2
325
2
      // Ignore other sub-blocks.
326
2
      if (Error Err = Stream.SkipBlock())
327
0
        return std::move(Err);
328
2
      continue;
329
2
330
2
    case BitstreamEntry::Record:
331
0
      if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
332
0
        continue;
333
0
      else
334
0
        return Skipped.takeError();
335
4
    }
336
4
  }
337
2
}
338
339
14
static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) {
340
14
  if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
341
0
    return std::move(Err);
342
14
343
14
  SmallVector<uint64_t, 64> Record;
344
14
345
14
  std::string Triple;
346
14
347
14
  // Read all the records for this module.
348
114
  while (true) {
349
114
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
350
114
    if (!MaybeEntry)
351
0
      return MaybeEntry.takeError();
352
114
    BitstreamEntry Entry = MaybeEntry.get();
353
114
354
114
    switch (Entry.Kind) {
355
114
    case BitstreamEntry::SubBlock: // Handled for us already.
356
0
    case BitstreamEntry::Error:
357
0
      return error("Malformed block");
358
14
    case BitstreamEntry::EndBlock:
359
14
      return Triple;
360
100
    case BitstreamEntry::Record:
361
100
      // The interesting case.
362
100
      break;
363
100
    }
364
100
365
100
    // Read a record.
366
100
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
367
100
    if (!MaybeRecord)
368
0
      return MaybeRecord.takeError();
369
100
    switch (MaybeRecord.get()) {
370
100
    
default: break86
; // Default behavior, ignore unknown content.
371
100
    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
372
14
      std::string S;
373
14
      if (convertToString(Record, 0, S))
374
0
        return error("Invalid record");
375
14
      Triple = S;
376
14
      break;
377
14
    }
378
100
    }
379
100
    Record.clear();
380
100
  }
381
14
  
llvm_unreachable0
("Exit infinite loop");
382
14
}
383
384
14
static Expected<std::string> readTriple(BitstreamCursor &Stream) {
385
14
  // We expect a number of well-defined blocks, though we don't necessarily
386
14
  // need to understand them all.
387
28
  while (true) {
388
28
    Expected<BitstreamEntry> MaybeEntry = Stream.advance();
389
28
    if (!MaybeEntry)
390
0
      return MaybeEntry.takeError();
391
28
    BitstreamEntry Entry = MaybeEntry.get();
392
28
393
28
    switch (Entry.Kind) {
394
28
    case BitstreamEntry::Error:
395
0
      return error("Malformed block");
396
28
    case BitstreamEntry::EndBlock:
397
0
      return "";
398
28
399
28
    case BitstreamEntry::SubBlock:
400
28
      if (Entry.ID == bitc::MODULE_BLOCK_ID)
401
14
        return readModuleTriple(Stream);
402
14
403
14
      // Ignore other sub-blocks.
404
14
      if (Error Err = Stream.SkipBlock())
405
0
        return std::move(Err);
406
14
      continue;
407
14
408
14
    case BitstreamEntry::Record:
409
0
      if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
410
0
        continue;
411
0
      else
412
0
        return Skipped.takeError();
413
28
    }
414
28
  }
415
14
}
416
417
namespace {
418
419
class BitcodeReaderBase {
420
protected:
421
  BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
422
8.59k
      : Stream(std::move(Stream)), Strtab(Strtab) {
423
8.59k
    this->Stream.setBlockInfo(&BlockInfo);
424
8.59k
  }
425
426
  BitstreamBlockInfo BlockInfo;
427
  BitstreamCursor Stream;
428
  StringRef Strtab;
429
430
  /// In version 2 of the bitcode we store names of global values and comdats in
431
  /// a string table rather than in the VST.
432
  bool UseStrtab = false;
433
434
  Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
435
436
  /// If this module uses a string table, pop the reference to the string table
437
  /// and return the referenced string and the rest of the record. Otherwise
438
  /// just return the record itself.
439
  std::pair<StringRef, ArrayRef<uint64_t>>
440
  readNameFromStrtab(ArrayRef<uint64_t> Record);
441
442
  bool readBlockInfo();
443
444
  // Contains an arbitrary and optional string identifying the bitcode producer
445
  std::string ProducerIdentification;
446
447
  Error error(const Twine &Message);
448
};
449
450
} // end anonymous namespace
451
452
48
Error BitcodeReaderBase::error(const Twine &Message) {
453
48
  std::string FullMsg = Message.str();
454
48
  if (!ProducerIdentification.empty())
455
9
    FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
456
9
               LLVM_VERSION_STRING "')";
457
48
  return ::error(FullMsg);
458
48
}
459
460
Expected<unsigned>
461
8.57k
BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
462
8.57k
  if (Record.empty())
463
0
    return error("Invalid record");
464
8.57k
  unsigned ModuleVersion = Record[0];
465
8.57k
  if (ModuleVersion > 2)
466
0
    return error("Invalid value");
467
8.57k
  UseStrtab = ModuleVersion >= 2;
468
8.57k
  return ModuleVersion;
469
8.57k
}
470
471
std::pair<StringRef, ArrayRef<uint64_t>>
472
1.46M
BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
473
1.46M
  if (!UseStrtab)
474
1.42M
    return {"", Record};
475
39.9k
  // Invalid reference. Let the caller complain about the record being empty.
476
39.9k
  if (Record[0] + Record[1] > Strtab.size())
477
0
    return {"", {}};
478
39.9k
  return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
479
39.9k
}
480
481
namespace {
482
483
class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
484
  LLVMContext &Context;
485
  Module *TheModule = nullptr;
486
  // Next offset to start scanning for lazy parsing of function bodies.
487
  uint64_t NextUnreadBit = 0;
488
  // Last function offset found in the VST.
489
  uint64_t LastFunctionBlockBit = 0;
490
  bool SeenValueSymbolTable = false;
491
  uint64_t VSTOffset = 0;
492
493
  std::vector<std::string> SectionTable;
494
  std::vector<std::string> GCTable;
495
496
  std::vector<Type*> TypeList;
497
  DenseMap<Function *, FunctionType *> FunctionTypes;
498
  BitcodeReaderValueList ValueList;
499
  Optional<MetadataLoader> MDLoader;
500
  std::vector<Comdat *> ComdatList;
501
  SmallVector<Instruction *, 64> InstructionList;
502
503
  std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
504
  std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> IndirectSymbolInits;
505
  std::vector<std::pair<Function *, unsigned>> FunctionPrefixes;
506
  std::vector<std::pair<Function *, unsigned>> FunctionPrologues;
507
  std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFns;
508
509
  /// The set of attributes by index.  Index zero in the file is for null, and
510
  /// is thus not represented here.  As such all indices are off by one.
511
  std::vector<AttributeList> MAttributes;
512
513
  /// The set of attribute groups.
514
  std::map<unsigned, AttributeList> MAttributeGroups;
515
516
  /// While parsing a function body, this is a list of the basic blocks for the
517
  /// function.
518
  std::vector<BasicBlock*> FunctionBBs;
519
520
  // When reading the module header, this list is populated with functions that
521
  // have bodies later in the file.
522
  std::vector<Function*> FunctionsWithBodies;
523
524
  // When intrinsic functions are encountered which require upgrading they are
525
  // stored here with their replacement function.
526
  using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
527
  UpdatedIntrinsicMap UpgradedIntrinsics;
528
  // Intrinsics which were remangled because of types rename
529
  UpdatedIntrinsicMap RemangledIntrinsics;
530
531
  // Several operations happen after the module header has been read, but
532
  // before function bodies are processed. This keeps track of whether
533
  // we've done this yet.
534
  bool SeenFirstFunctionBody = false;
535
536
  /// When function bodies are initially scanned, this map contains info about
537
  /// where to find deferred function body in the stream.
538
  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
539
540
  /// When Metadata block is initially scanned when parsing the module, we may
541
  /// choose to defer parsing of the metadata. This vector contains info about
542
  /// which Metadata blocks are deferred.
543
  std::vector<uint64_t> DeferredMetadataInfo;
544
545
  /// These are basic blocks forward-referenced by block addresses.  They are
546
  /// inserted lazily into functions when they're loaded.  The basic block ID is
547
  /// its index into the vector.
548
  DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
549
  std::deque<Function *> BasicBlockFwdRefQueue;
550
551
  /// Indicates that we are using a new encoding for instruction operands where
552
  /// most operands in the current FUNCTION_BLOCK are encoded relative to the
553
  /// instruction number, for a more compact encoding.  Some instruction
554
  /// operands are not relative to the instruction ID: basic block numbers, and
555
  /// types. Once the old style function blocks have been phased out, we would
556
  /// not need this flag.
557
  bool UseRelativeIDs = false;
558
559
  /// True if all functions will be materialized, negating the need to process
560
  /// (e.g.) blockaddress forward references.
561
  bool WillMaterializeAllForwardRefs = false;
562
563
  bool StripDebugInfo = false;
564
  TBAAVerifier TBAAVerifyHelper;
565
566
  std::vector<std::string> BundleTags;
567
  SmallVector<SyncScope::ID, 8> SSIDs;
568
569
public:
570
  BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
571
                StringRef ProducerIdentification, LLVMContext &Context);
572
573
  Error materializeForwardReferencedFunctions();
574
575
  Error materialize(GlobalValue *GV) override;
576
  Error materializeModule() override;
577
  std::vector<StructType *> getIdentifiedStructTypes() const override;
578
579
  /// Main interface to parsing a bitcode buffer.
580
  /// \returns true if an error occurred.
581
  Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false,
582
                         bool IsImporting = false);
583
584
  static uint64_t decodeSignRotatedValue(uint64_t V);
585
586
  /// Materialize any deferred Metadata block.
587
  Error materializeMetadata() override;
588
589
  void setStripDebugInfo() override;
590
591
private:
592
  std::vector<StructType *> IdentifiedStructTypes;
593
  StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
594
  StructType *createIdentifiedStructType(LLVMContext &Context);
595
596
  /// Map all pointer types within \param Ty to the opaque pointer
597
  /// type in the same address space if opaque pointers are being
598
  /// used, otherwise nop. This converts a bitcode-reader internal
599
  /// type into one suitable for use in a Value.
600
15.8M
  Type *flattenPointerTypes(Type *Ty) {
601
15.8M
    return Ty;
602
15.8M
  }
603
604
  /// Given a fully structured pointer type (i.e. not opaque), return
605
  /// the flattened form of its element, suitable for use in a Value.
606
3.45M
  Type *getPointerElementFlatType(Type *Ty) {
607
3.45M
    return flattenPointerTypes(cast<PointerType>(Ty)->getElementType());
608
3.45M
  }
609
610
  /// Given a fully structured pointer type, get its element type in
611
  /// both fully structured form, and flattened form suitable for use
612
  /// in a Value.
613
1.56k
  std::pair<Type *, Type *> getPointerElementTypes(Type *FullTy) {
614
1.56k
    Type *ElTy = cast<PointerType>(FullTy)->getElementType();
615
1.56k
    return std::make_pair(ElTy, flattenPointerTypes(ElTy));
616
1.56k
  }
617
618
  /// Return the flattened type (suitable for use in a Value)
619
  /// specified by the given \param ID .
620
4.51M
  Type *getTypeByID(unsigned ID) {
621
4.51M
    return flattenPointerTypes(getFullyStructuredTypeByID(ID));
622
4.51M
  }
623
624
  /// Return the fully structured (bitcode-reader internal) type
625
  /// corresponding to the given \param ID .
626
  Type *getFullyStructuredTypeByID(unsigned ID);
627
628
18.9M
  Value *getFnValueByID(unsigned ID, Type *Ty, Type **FullTy = nullptr) {
629
18.9M
    if (Ty && 
Ty->isMetadataTy()5.78M
)
630
9.84k
      return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
631
18.9M
    return ValueList.getValueFwdRef(ID, Ty, FullTy);
632
18.9M
  }
633
634
9.84k
  Metadata *getFnMetadataByID(unsigned ID) {
635
9.84k
    return MDLoader->getMetadataFwdRefOrLoad(ID);
636
9.84k
  }
637
638
4.14M
  BasicBlock *getBasicBlock(unsigned ID) const {
639
4.14M
    if (ID >= FunctionBBs.size()) 
return nullptr0
; // Invalid ID
640
4.14M
    return FunctionBBs[ID];
641
4.14M
  }
642
643
2.34M
  AttributeList getAttributes(unsigned i) const {
644
2.34M
    if (i-1 < MAttributes.size())
645
2.23M
      return MAttributes[i-1];
646
107k
    return AttributeList();
647
107k
  }
648
649
  /// Read a value/type pair out of the specified record from slot 'Slot'.
650
  /// Increment Slot past the number of slots used in the record. Return true on
651
  /// failure.
652
  bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
653
                        unsigned InstNum, Value *&ResVal,
654
13.1M
                        Type **FullTy = nullptr) {
655
13.1M
    if (Slot == Record.size()) 
return true0
;
656
13.1M
    unsigned ValNo = (unsigned)Record[Slot++];
657
13.1M
    // Adjust the ValNo, if it was encoded relative to the InstNum.
658
13.1M
    if (UseRelativeIDs)
659
13.1M
      ValNo = InstNum - ValNo;
660
13.1M
    if (ValNo < InstNum) {
661
13.1M
      // If this is not a forward reference, just return the value we already
662
13.1M
      // have.
663
13.1M
      ResVal = getFnValueByID(ValNo, nullptr, FullTy);
664
13.1M
      return ResVal == nullptr;
665
13.1M
    }
666
6.99k
    if (Slot == Record.size())
667
0
      return true;
668
6.99k
669
6.99k
    unsigned TypeNo = (unsigned)Record[Slot++];
670
6.99k
    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
671
6.99k
    if (FullTy)
672
1.39k
      *FullTy = getFullyStructuredTypeByID(TypeNo);
673
6.99k
    return ResVal == nullptr;
674
6.99k
  }
675
676
  /// Read a value out of the specified record from slot 'Slot'. Increment Slot
677
  /// past the number of slots used by the value in the record. Return true if
678
  /// there is an error.
679
  bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
680
1.50M
                unsigned InstNum, Type *Ty, Value *&ResVal) {
681
1.50M
    if (getValue(Record, Slot, InstNum, Ty, ResVal))
682
1
      return true;
683
1.50M
    // All values currently take a single record slot.
684
1.50M
    ++Slot;
685
1.50M
    return false;
686
1.50M
  }
687
688
  /// Like popValue, but does not increment the Slot number.
689
  bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
690
1.50M
                unsigned InstNum, Type *Ty, Value *&ResVal) {
691
1.50M
    ResVal = getValue(Record, Slot, InstNum, Ty);
692
1.50M
    return ResVal == nullptr;
693
1.50M
  }
694
695
  /// Version of getValue that returns ResVal directly, or 0 if there is an
696
  /// error.
697
  Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
698
4.88M
                  unsigned InstNum, Type *Ty) {
699
4.88M
    if (Slot == Record.size()) 
return nullptr0
;
700
4.88M
    unsigned ValNo = (unsigned)Record[Slot];
701
4.88M
    // Adjust the ValNo, if it was encoded relative to the InstNum.
702
4.88M
    if (UseRelativeIDs)
703
4.88M
      ValNo = InstNum - ValNo;
704
4.88M
    return getFnValueByID(ValNo, Ty);
705
4.88M
  }
706
707
  /// Like getValue, but decodes signed VBRs.
708
  Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
709
809k
                        unsigned InstNum, Type *Ty) {
710
809k
    if (Slot == Record.size()) 
return nullptr0
;
711
809k
    unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
712
809k
    // Adjust the ValNo, if it was encoded relative to the InstNum.
713
809k
    if (UseRelativeIDs)
714
809k
      ValNo = InstNum - ValNo;
715
809k
    return getFnValueByID(ValNo, Ty);
716
809k
  }
717
718
  /// Upgrades old-style typeless byval attributes by adding the corresponding
719
  /// argument's pointee type.
720
  void propagateByValTypes(CallBase *CB, ArrayRef<Type *> ArgsFullTys);
721
722
  /// Converts alignment exponent (i.e. power of two (or zero)) to the
723
  /// corresponding alignment to use. If alignment is too large, returns
724
  /// a corresponding error code.
725
  Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
726
  Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
727
  Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false);
728
729
  Error parseComdatRecord(ArrayRef<uint64_t> Record);
730
  Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
731
  Error parseFunctionRecord(ArrayRef<uint64_t> Record);
732
  Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
733
                                        ArrayRef<uint64_t> Record);
734
735
  Error parseAttributeBlock();
736
  Error parseAttributeGroupBlock();
737
  Error parseTypeTable();
738
  Error parseTypeTableBody();
739
  Error parseOperandBundleTags();
740
  Error parseSyncScopeNames();
741
742
  Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
743
                                unsigned NameIndex, Triple &TT);
744
  void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
745
                               ArrayRef<uint64_t> Record);
746
  Error parseValueSymbolTable(uint64_t Offset = 0);
747
  Error parseGlobalValueSymbolTable();
748
  Error parseConstants();
749
  Error rememberAndSkipFunctionBodies();
750
  Error rememberAndSkipFunctionBody();
751
  /// Save the positions of the Metadata blocks and skip parsing the blocks.
752
  Error rememberAndSkipMetadata();
753
  Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
754
  Error parseFunctionBody(Function *F);
755
  Error globalCleanup();
756
  Error resolveGlobalAndIndirectSymbolInits();
757
  Error parseUseLists();
758
  Error findFunctionInStream(
759
      Function *F,
760
      DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
761
762
  SyncScope::ID getDecodedSyncScopeID(unsigned Val);
763
};
764
765
/// Class to manage reading and parsing function summary index bitcode
766
/// files/sections.
767
class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
768
  /// The module index built during parsing.
769
  ModuleSummaryIndex &TheIndex;
770
771
  /// Indicates whether we have encountered a global value summary section
772
  /// yet during parsing.
773
  bool SeenGlobalValSummary = false;
774
775
  /// Indicates whether we have already parsed the VST, used for error checking.
776
  bool SeenValueSymbolTable = false;
777
778
  /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
779
  /// Used to enable on-demand parsing of the VST.
780
  uint64_t VSTOffset = 0;
781
782
  // Map to save ValueId to ValueInfo association that was recorded in the
783
  // ValueSymbolTable. It is used after the VST is parsed to convert
784
  // call graph edges read from the function summary from referencing
785
  // callees by their ValueId to using the ValueInfo instead, which is how
786
  // they are recorded in the summary index being built.
787
  // We save a GUID which refers to the same global as the ValueInfo, but
788
  // ignoring the linkage, i.e. for values other than local linkage they are
789
  // identical.
790
  DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>>
791
      ValueIdToValueInfoMap;
792
793
  /// Map populated during module path string table parsing, from the
794
  /// module ID to a string reference owned by the index's module
795
  /// path string table, used to correlate with combined index
796
  /// summary records.
797
  DenseMap<uint64_t, StringRef> ModuleIdMap;
798
799
  /// Original source file name recorded in a bitcode record.
800
  std::string SourceFileName;
801
802
  /// The string identifier given to this module by the client, normally the
803
  /// path to the bitcode file.
804
  StringRef ModulePath;
805
806
  /// For per-module summary indexes, the unique numerical identifier given to
807
  /// this module by the client.
808
  unsigned ModuleId;
809
810
public:
811
  ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
812
                                  ModuleSummaryIndex &TheIndex,
813
                                  StringRef ModulePath, unsigned ModuleId);
814
815
  Error parseModule();
816
817
private:
818
  void setValueGUID(uint64_t ValueID, StringRef ValueName,
819
                    GlobalValue::LinkageTypes Linkage,
820
                    StringRef SourceFileName);
821
  Error parseValueSymbolTable(
822
      uint64_t Offset,
823
      DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
824
  std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record);
825
  std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
826
                                                    bool IsOldProfileFormat,
827
                                                    bool HasProfile,
828
                                                    bool HasRelBF);
829
  Error parseEntireSummary(unsigned ID);
830
  Error parseModuleStringTable();
831
  void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
832
  void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot,
833
                                       TypeIdCompatibleVtableInfo &TypeId);
834
835
  std::pair<ValueInfo, GlobalValue::GUID>
836
  getValueInfoFromValueId(unsigned ValueId);
837
838
  void addThisModule();
839
  ModuleSummaryIndex::ModuleInfo *getThisModule();
840
};
841
842
} // end anonymous namespace
843
844
std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx,
845
4
                                                    Error Err) {
846
4
  if (Err) {
847
4
    std::error_code EC;
848
4
    handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
849
4
      EC = EIB.convertToErrorCode();
850
4
      Ctx.emitError(EIB.message());
851
4
    });
852
4
    return EC;
853
4
  }
854
0
  return std::error_code();
855
0
}
856
857
BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
858
                             StringRef ProducerIdentification,
859
                             LLVMContext &Context)
860
    : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
861
7.79k
      ValueList(Context, Stream.SizeInBytes()) {
862
7.79k
  this->ProducerIdentification = ProducerIdentification;
863
7.79k
}
864
865
339k
Error BitcodeReader::materializeForwardReferencedFunctions() {
866
339k
  if (WillMaterializeAllForwardRefs)
867
337k
    return Error::success();
868
2.39k
869
2.39k
  // Prevent recursion.
870
2.39k
  WillMaterializeAllForwardRefs = true;
871
2.39k
872
2.40k
  while (!BasicBlockFwdRefQueue.empty()) {
873
10
    Function *F = BasicBlockFwdRefQueue.front();
874
10
    BasicBlockFwdRefQueue.pop_front();
875
10
    assert(F && "Expected valid function");
876
10
    if (!BasicBlockFwdRefs.count(F))
877
0
      // Already materialized.
878
0
      continue;
879
10
880
10
    // Check for a function that isn't materializable to prevent an infinite
881
10
    // loop.  When parsing a blockaddress stored in a global variable, there
882
10
    // isn't a trivial way to check if a function will have a body without a
883
10
    // linear search through FunctionsWithBodies, so just check it here.
884
10
    if (!F->isMaterializable())
885
0
      return error("Never resolved function from blockaddress");
886
10
887
10
    // Try to materialize F.
888
10
    if (Error Err = materialize(F))
889
0
      return Err;
890
10
  }
891
2.39k
  assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
892
2.39k
893
2.39k
  // Reset state.
894
2.39k
  WillMaterializeAllForwardRefs = false;
895
2.39k
  return Error::success();
896
2.39k
}
897
898
//===----------------------------------------------------------------------===//
899
//  Helper functions to implement forward reference resolution, etc.
900
//===----------------------------------------------------------------------===//
901
902
91.8k
static bool hasImplicitComdat(size_t Val) {
903
91.8k
  switch (Val) {
904
91.8k
  default:
905
91.8k
    return false;
906
91.8k
  case 1:  // Old WeakAnyLinkage
907
17
  case 4:  // Old LinkOnceAnyLinkage
908
17
  case 10: // Old WeakODRLinkage
909
17
  case 11: // Old LinkOnceODRLinkage
910
17
    return true;
911
91.8k
  }
912
91.8k
}
913
914
1.45M
static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
915
1.45M
  switch (Val) {
916
1.45M
  default: // Map unknown/new linkages to external
917
728k
  case 0:
918
728k
    return GlobalValue::ExternalLinkage;
919
1.87k
  case 2:
920
1.87k
    return GlobalValue::AppendingLinkage;
921
14.4k
  case 3:
922
14.4k
    return GlobalValue::InternalLinkage;
923
4
  case 5:
924
4
    return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
925
4
  case 6:
926
4
    return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
927
92.3k
  case 7:
928
92.3k
    return GlobalValue::ExternalWeakLinkage;
929
115
  case 8:
930
115
    return GlobalValue::CommonLinkage;
931
264k
  case 9:
932
264k
    return GlobalValue::PrivateLinkage;
933
175
  case 12:
934
175
    return GlobalValue::AvailableExternallyLinkage;
935
4
  case 13:
936
4
    return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
937
4
  case 14:
938
4
    return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
939
8
  case 15:
940
8
    return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
941
154k
  case 1: // Old value with implicit comdat.
942
154k
  case 16:
943
154k
    return GlobalValue::WeakAnyLinkage;
944
154k
  case 10: // Old value with implicit comdat.
945
200
  case 17:
946
200
    return GlobalValue::WeakODRLinkage;
947
83.8k
  case 4: // Old value with implicit comdat.
948
83.8k
  case 18:
949
83.8k
    return GlobalValue::LinkOnceAnyLinkage;
950
118k
  case 11: // Old value with implicit comdat.
951
118k
  case 19:
952
118k
    return GlobalValue::LinkOnceODRLinkage;
953
1.45M
  }
954
1.45M
}
955
956
1.65k
static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) {
957
1.65k
  FunctionSummary::FFlags Flags;
958
1.65k
  Flags.ReadNone = RawFlags & 0x1;
959
1.65k
  Flags.ReadOnly = (RawFlags >> 1) & 0x1;
960
1.65k
  Flags.NoRecurse = (RawFlags >> 2) & 0x1;
961
1.65k
  Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
962
1.65k
  Flags.NoInline = (RawFlags >> 4) & 0x1;
963
1.65k
  return Flags;
964
1.65k
}
965
966
/// Decode the flags for GlobalValue in the summary.
967
static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
968
2.50k
                                                            uint64_t Version) {
969
2.50k
  // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
970
2.50k
  // like getDecodedLinkage() above. Any future change to the linkage enum and
971
2.50k
  // to getDecodedLinkage() will need to be taken into account here as above.
972
2.50k
  auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
973
2.50k
  RawFlags = RawFlags >> 4;
974
2.50k
  bool NotEligibleToImport = (RawFlags & 0x1) || 
Version < 32.38k
;
975
2.50k
  // The Live flag wasn't introduced until version 3. For dead stripping
976
2.50k
  // to work correctly on earlier versions, we must conservatively treat all
977
2.50k
  // values as live.
978
2.50k
  bool Live = (RawFlags & 0x2) || 
Version < 32.43k
;
979
2.50k
  bool Local = (RawFlags & 0x4);
980
2.50k
  bool AutoHide = (RawFlags & 0x8);
981
2.50k
982
2.50k
  return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live, Local, AutoHide);
983
2.50k
}
984
985
// Decode the flags for GlobalVariable in the summary
986
446
static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) {
987
446
  return GlobalVarSummary::GVarFlags((RawFlags & 0x1) ? 
true311
:
false135
,
988
446
                                     (RawFlags & 0x2) ? 
true304
:
false142
);
989
446
}
990
991
1.15M
static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
992
1.15M
  switch (Val) {
993
1.15M
  default: // Map unknown visibilities to default.
994
1.15M
  case 0: return GlobalValue::DefaultVisibility;
995
866
  case 1: return GlobalValue::HiddenVisibility;
996
67
  case 2: return GlobalValue::ProtectedVisibility;
997
1.15M
  }
998
1.15M
}
999
1000
static GlobalValue::DLLStorageClassTypes
1001
1.36M
getDecodedDLLStorageClass(unsigned Val) {
1002
1.36M
  switch (Val) {
1003
1.36M
  default: // Map unknown values to default.
1004
1.36M
  case 0: return GlobalValue::DefaultStorageClass;
1005
48
  case 1: return GlobalValue::DLLImportStorageClass;
1006
55
  case 2: return GlobalValue::DLLExportStorageClass;
1007
1.36M
  }
1008
1.36M
}
1009
1010
27.5k
static bool getDecodedDSOLocal(unsigned Val) {
1011
27.5k
  switch(Val) {
1012
27.5k
  default: // Map unknown values to preemptable.
1013
14.3k
  case 0:  return false;
1014
13.2k
  case 1:  return true;
1015
27.5k
  }
1016
27.5k
}
1017
1018
230k
static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
1019
230k
  switch (Val) {
1020
230k
    
case 0: return GlobalVariable::NotThreadLocal230k
;
1021
230k
    default: // Map unknown non-zero value to general dynamic.
1022
35
    case 1: return GlobalVariable::GeneralDynamicTLSModel;
1023
33
    case 2: return GlobalVariable::LocalDynamicTLSModel;
1024
32
    case 3: return GlobalVariable::InitialExecTLSModel;
1025
32
    case 4: return GlobalVariable::LocalExecTLSModel;
1026
230k
  }
1027
230k
}
1028
1029
1.36M
static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
1030
1.36M
  switch (Val) {
1031
1.36M
    default: // Map unknown to UnnamedAddr::None.
1032
918k
    case 0: return GlobalVariable::UnnamedAddr::None;
1033
201k
    case 1: return GlobalVariable::UnnamedAddr::Global;
1034
245k
    case 2: return GlobalVariable::UnnamedAddr::Local;
1035
1.36M
  }
1036
1.36M
}
1037
1038
740k
static int getDecodedCastOpcode(unsigned Val) {
1039
740k
  switch (Val) {
1040
740k
  
default: return -10
;
1041
740k
  
case bitc::CAST_TRUNC : return Instruction::Trunc123k
;
1042
740k
  
case bitc::CAST_ZEXT : return Instruction::ZExt39.0k
;
1043
740k
  
case bitc::CAST_SEXT : return Instruction::SExt149k
;
1044
740k
  
case bitc::CAST_FPTOUI : return Instruction::FPToUI2.80k
;
1045
740k
  
case bitc::CAST_FPTOSI : return Instruction::FPToSI113
;
1046
740k
  
case bitc::CAST_UITOFP : return Instruction::UIToFP12.4k
;
1047
740k
  
case bitc::CAST_SITOFP : return Instruction::SIToFP4.17k
;
1048
740k
  
case bitc::CAST_FPTRUNC : return Instruction::FPTrunc2.87k
;
1049
740k
  
case bitc::CAST_FPEXT : return Instruction::FPExt10.0k
;
1050
740k
  
case bitc::CAST_PTRTOINT: return Instruction::PtrToInt31.5k
;
1051
740k
  
case bitc::CAST_INTTOPTR: return Instruction::IntToPtr18.1k
;
1052
740k
  
case bitc::CAST_BITCAST : return Instruction::BitCast346k
;
1053
740k
  
case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast48
;
1054
740k
  }
1055
740k
}
1056
1057
52
static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
1058
52
  bool IsFP = Ty->isFPOrFPVectorTy();
1059
52
  // UnOps are only valid for int/fp or vector of int/fp types
1060
52
  if (!IsFP && 
!Ty->isIntOrIntVectorTy()0
)
1061
0
    return -1;
1062
52
1063
52
  switch (Val) {
1064
52
  default:
1065
0
    return -1;
1066
52
  case bitc::UNOP_NEG:
1067
52
    return IsFP ? Instruction::FNeg : 
-10
;
1068
52
  }
1069
52
}
1070
1071
586k
static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
1072
586k
  bool IsFP = Ty->isFPOrFPVectorTy();
1073
586k
  // BinOps are only valid for int/fp or vector of int/fp types
1074
586k
  if (!IsFP && 
!Ty->isIntOrIntVectorTy()555k
)
1075
1
    return -1;
1076
586k
1077
586k
  switch (Val) {
1078
586k
  default:
1079
0
    return -1;
1080
586k
  case bitc::BINOP_ADD:
1081
224k
    return IsFP ? 
Instruction::FAdd6.19k
:
Instruction::Add217k
;
1082
586k
  case bitc::BINOP_SUB:
1083
53.0k
    return IsFP ? 
Instruction::FSub1.49k
:
Instruction::Sub51.5k
;
1084
586k
  case bitc::BINOP_MUL:
1085
80.7k
    return IsFP ? 
Instruction::FMul14.1k
:
Instruction::Mul66.6k
;
1086
586k
  case bitc::BINOP_UDIV:
1087
5.61k
    return IsFP ? 
-10
: Instruction::UDiv;
1088
586k
  case bitc::BINOP_SDIV:
1089
10.4k
    return IsFP ? 
Instruction::FDiv8.41k
:
Instruction::SDiv2.00k
;
1090
586k
  case bitc::BINOP_UREM:
1091
19
    return IsFP ? 
-10
: Instruction::URem;
1092
586k
  case bitc::BINOP_SREM:
1093
1.46k
    return IsFP ? 
Instruction::FRem51
:
Instruction::SRem1.41k
;
1094
586k
  case bitc::BINOP_SHL:
1095
30.0k
    return IsFP ? 
-10
: Instruction::Shl;
1096
586k
  case bitc::BINOP_LSHR:
1097
22.7k
    return IsFP ? 
-10
: Instruction::LShr;
1098
586k
  case bitc::BINOP_ASHR:
1099
5.90k
    return IsFP ? 
-10
: Instruction::AShr;
1100
586k
  case bitc::BINOP_AND:
1101
118k
    return IsFP ? 
-10
: Instruction::And;
1102
586k
  case bitc::BINOP_OR:
1103
25.5k
    return IsFP ? 
-10
: Instruction::Or;
1104
586k
  case bitc::BINOP_XOR:
1105
7.76k
    return IsFP ? 
-10
: Instruction::Xor;
1106
586k
  }
1107
586k
}
1108
1109
18.2k
static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
1110
18.2k
  switch (Val) {
1111
18.2k
  
default: return AtomicRMWInst::BAD_BINOP0
;
1112
18.2k
  
case bitc::RMW_XCHG: return AtomicRMWInst::Xchg5.58k
;
1113
18.2k
  
case bitc::RMW_ADD: return AtomicRMWInst::Add9.79k
;
1114
18.2k
  
case bitc::RMW_SUB: return AtomicRMWInst::Sub2.79k
;
1115
18.2k
  
case bitc::RMW_AND: return AtomicRMWInst::And10
;
1116
18.2k
  
case bitc::RMW_NAND: return AtomicRMWInst::Nand10
;
1117
18.2k
  
case bitc::RMW_OR: return AtomicRMWInst::Or19
;
1118
18.2k
  
case bitc::RMW_XOR: return AtomicRMWInst::Xor10
;
1119
18.2k
  
case bitc::RMW_MAX: return AtomicRMWInst::Max10
;
1120
18.2k
  
case bitc::RMW_MIN: return AtomicRMWInst::Min10
;
1121
18.2k
  
case bitc::RMW_UMAX: return AtomicRMWInst::UMax10
;
1122
18.2k
  
case bitc::RMW_UMIN: return AtomicRMWInst::UMin10
;
1123
18.2k
  
case bitc::RMW_FADD: return AtomicRMWInst::FAdd12
;
1124
18.2k
  
case bitc::RMW_FSUB: return AtomicRMWInst::FSub2
;
1125
18.2k
  }
1126
18.2k
}
1127
1128
34.5k
static AtomicOrdering getDecodedOrdering(unsigned Val) {
1129
34.5k
  switch (Val) {
1130
34.5k
  
case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic0
;
1131
34.5k
  
case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered55
;
1132
34.5k
  
case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic576
;
1133
34.5k
  
case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire155
;
1134
34.5k
  
case bitc::ORDERING_RELEASE: return AtomicOrdering::Release5.65k
;
1135
34.5k
  
case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease69
;
1136
34.5k
  default: // Map unknown orderings to sequentially-consistent.
1137
28.0k
  case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1138
34.5k
  }
1139
34.5k
}
1140
1141
3.78k
static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
1142
3.78k
  switch (Val) {
1143
3.78k
  default: // Map unknown selection kinds to any.
1144
3.73k
  case bitc::COMDAT_SELECTION_KIND_ANY:
1145
3.73k
    return Comdat::Any;
1146
12
  case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
1147
12
    return Comdat::ExactMatch;
1148
14
  case bitc::COMDAT_SELECTION_KIND_LARGEST:
1149
14
    return Comdat::Largest;
1150
12
  case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
1151
12
    return Comdat::NoDuplicates;
1152
10
  case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
1153
10
    return Comdat::SameSize;
1154
3.78k
  }
1155
3.78k
}
1156
1157
345
static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
1158
345
  FastMathFlags FMF;
1159
345
  if (0 != (Val & bitc::UnsafeAlgebra))
1160
10
    FMF.setFast();
1161
345
  if (0 != (Val & bitc::AllowReassoc))
1162
67
    FMF.setAllowReassoc();
1163
345
  if (0 != (Val & bitc::NoNaNs))
1164
219
    FMF.setNoNaNs();
1165
345
  if (0 != (Val & bitc::NoInfs))
1166
101
    FMF.setNoInfs();
1167
345
  if (0 != (Val & bitc::NoSignedZeros))
1168
90
    FMF.setNoSignedZeros();
1169
345
  if (0 != (Val & bitc::AllowReciprocal))
1170
76
    FMF.setAllowReciprocal();
1171
345
  if (0 != (Val & bitc::AllowContract))
1172
65
    FMF.setAllowContract(true);
1173
345
  if (0 != (Val & bitc::ApproxFunc))
1174
67
    FMF.setApproxFunc();
1175
345
  return FMF;
1176
345
}
1177
1178
91.8k
static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1179
91.8k
  switch (Val) {
1180
91.8k
  
case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break4
;
1181
91.8k
  
case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break4
;
1182
91.8k
  }
1183
91.8k
}
1184
1185
12.2M
Type *BitcodeReader::getFullyStructuredTypeByID(unsigned ID) {
1186
12.2M
  // The type table size is always specified correctly.
1187
12.2M
  if (ID >= TypeList.size())
1188
1
    return nullptr;
1189
12.2M
1190
12.2M
  if (Type *Ty = TypeList[ID])
1191
12.2M
    return Ty;
1192
2.88k
1193
2.88k
  // If we have a forward reference, the only possible case is when it is to a
1194
2.88k
  // named struct.  Just create a placeholder for now.
1195
2.88k
  return TypeList[ID] = createIdentifiedStructType(Context);
1196
2.88k
}
1197
1198
StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1199
86.2k
                                                      StringRef Name) {
1200
86.2k
  auto *Ret = StructType::create(Context, Name);
1201
86.2k
  IdentifiedStructTypes.push_back(Ret);
1202
86.2k
  return Ret;
1203
86.2k
}
1204
1205
2.89k
StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1206
2.89k
  auto *Ret = StructType::create(Context);
1207
2.89k
  IdentifiedStructTypes.push_back(Ret);
1208
2.89k
  return Ret;
1209
2.89k
}
1210
1211
//===----------------------------------------------------------------------===//
1212
//  Functions for parsing blocks from the bitcode file
1213
//===----------------------------------------------------------------------===//
1214
1215
1.06k
static uint64_t getRawAttributeMask(Attribute::AttrKind Val) {
1216
1.06k
  switch (Val) {
1217
1.06k
  case Attribute::EndAttrKinds:
1218
0
    llvm_unreachable("Synthetic enumerators which should never get here");
1219
1.06k
1220
1.06k
  
case Attribute::None: return 018
;
1221
1.06k
  
case Attribute::ZExt: return 1 << 018
;
1222
1.06k
  
case Attribute::SExt: return 1 << 118
;
1223
1.06k
  
case Attribute::NoReturn: return 1 << 218
;
1224
1.06k
  
case Attribute::InReg: return 1 << 318
;
1225
1.06k
  
case Attribute::StructRet: return 1 << 418
;
1226
1.06k
  
case Attribute::NoUnwind: return 1 << 518
;
1227
1.06k
  
case Attribute::NoAlias: return 1 << 618
;
1228
1.06k
  
case Attribute::ByVal: return 1 << 718
;
1229
1.06k
  
case Attribute::Nest: return 1 << 818
;
1230
1.06k
  
case Attribute::ReadNone: return 1 << 918
;
1231
1.06k
  
case Attribute::ReadOnly: return 1 << 1018
;
1232
1.06k
  
case Attribute::NoInline: return 1 << 1118
;
1233
1.06k
  
case Attribute::AlwaysInline: return 1 << 1218
;
1234
1.06k
  
case Attribute::OptimizeForSize: return 1 << 1318
;
1235
1.06k
  
case Attribute::StackProtect: return 1 << 1418
;
1236
1.06k
  
case Attribute::StackProtectReq: return 1 << 1518
;
1237
1.06k
  
case Attribute::Alignment: return 31 << 1618
;
1238
1.06k
  
case Attribute::NoCapture: return 1 << 2118
;
1239
1.06k
  
case Attribute::NoRedZone: return 1 << 2218
;
1240
1.06k
  
case Attribute::NoImplicitFloat: return 1 << 2318
;
1241
1.06k
  
case Attribute::Naked: return 1 << 2418
;
1242
1.06k
  
case Attribute::InlineHint: return 1 << 2518
;
1243
1.06k
  
case Attribute::StackAlignment: return 7 << 2618
;
1244
1.06k
  
case Attribute::ReturnsTwice: return 1 << 2918
;
1245
1.06k
  
case Attribute::UWTable: return 1 << 3018
;
1246
1.06k
  
case Attribute::NonLazyBind: return 1U << 3118
;
1247
1.06k
  
case Attribute::SanitizeAddress: return 1ULL << 3218
;
1248
1.06k
  
case Attribute::MinSize: return 1ULL << 3318
;
1249
1.06k
  
case Attribute::NoDuplicate: return 1ULL << 3418
;
1250
1.06k
  
case Attribute::StackProtectStrong: return 1ULL << 3518
;
1251
1.06k
  
case Attribute::SanitizeThread: return 1ULL << 3618
;
1252
1.06k
  
case Attribute::SanitizeMemory: return 1ULL << 3718
;
1253
1.06k
  
case Attribute::NoBuiltin: return 1ULL << 3818
;
1254
1.06k
  
case Attribute::Returned: return 1ULL << 3918
;
1255
1.06k
  
case Attribute::Cold: return 1ULL << 4018
;
1256
1.06k
  
case Attribute::Builtin: return 1ULL << 4118
;
1257
1.06k
  
case Attribute::OptimizeNone: return 1ULL << 4218
;
1258
1.06k
  
case Attribute::InAlloca: return 1ULL << 4318
;
1259
1.06k
  
case Attribute::NonNull: return 1ULL << 4418
;
1260
1.06k
  
case Attribute::JumpTable: return 1ULL << 4518
;
1261
1.06k
  
case Attribute::Convergent: return 1ULL << 4618
;
1262
1.06k
  
case Attribute::SafeStack: return 1ULL << 4718
;
1263
1.06k
  
case Attribute::NoRecurse: return 1ULL << 4818
;
1264
1.06k
  
case Attribute::InaccessibleMemOnly: return 1ULL << 4918
;
1265
1.06k
  
case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 5018
;
1266
1.06k
  
case Attribute::SwiftSelf: return 1ULL << 5118
;
1267
1.06k
  
case Attribute::SwiftError: return 1ULL << 5218
;
1268
1.06k
  
case Attribute::WriteOnly: return 1ULL << 5318
;
1269
1.06k
  
case Attribute::Speculatable: return 1ULL << 5418
;
1270
1.06k
  
case Attribute::StrictFP: return 1ULL << 5518
;
1271
1.06k
  
case Attribute::SanitizeHWAddress: return 1ULL << 5618
;
1272
1.06k
  
case Attribute::NoCfCheck: return 1ULL << 5718
;
1273
1.06k
  
case Attribute::OptForFuzzing: return 1ULL << 5818
;
1274
1.06k
  
case Attribute::ShadowCallStack: return 1ULL << 5918
;
1275
1.06k
  case Attribute::SpeculativeLoadHardening:
1276
18
    return 1ULL << 60;
1277
1.06k
  case Attribute::ImmArg:
1278
18
    return 1ULL << 61;
1279
1.06k
  case Attribute::WillReturn:
1280
18
    return 1ULL << 62;
1281
1.06k
  case Attribute::NoFree:
1282
18
    return 1ULL << 63;
1283
1.06k
  case Attribute::NoSync:
1284
0
    llvm_unreachable("nosync attribute not supported in raw format");
1285
1.06k
    
break0
;
1286
1.06k
  case Attribute::Dereferenceable:
1287
0
    llvm_unreachable("dereferenceable attribute not supported in raw format");
1288
1.06k
    
break0
;
1289
1.06k
  case Attribute::DereferenceableOrNull:
1290
0
    llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
1291
1.06k
                     "format");
1292
1.06k
    
break0
;
1293
1.06k
  case Attribute::ArgMemOnly:
1294
0
    llvm_unreachable("argmemonly attribute not supported in raw format");
1295
1.06k
    
break0
;
1296
1.06k
  case Attribute::AllocSize:
1297
0
    llvm_unreachable("allocsize not supported in raw format");
1298
1.06k
    
break0
;
1299
1.06k
  case Attribute::SanitizeMemTag:
1300
0
    llvm_unreachable("sanitize_memtag attribute not supported in raw format");
1301
1.06k
    
break0
;
1302
0
  }
1303
0
  llvm_unreachable("Unsupported attribute type");
1304
0
}
1305
1306
18
static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) {
1307
18
  if (!Val) 
return0
;
1308
18
1309
1.18k
  
for (Attribute::AttrKind I = Attribute::None; 18
I != Attribute::EndAttrKinds;
1310
1.17k
       I = Attribute::AttrKind(I + 1)) {
1311
1.17k
    if (I == Attribute::SanitizeMemTag ||
1312
1.17k
        
I == Attribute::Dereferenceable1.15k
||
1313
1.17k
        
I == Attribute::DereferenceableOrNull1.13k
||
1314
1.17k
        
I == Attribute::ArgMemOnly1.11k
||
1315
1.17k
        
I == Attribute::AllocSize1.09k
||
1316
1.17k
        
I == Attribute::NoSync1.08k
)
1317
108
      continue;
1318
1.06k
    if (uint64_t A = (Val & getRawAttributeMask(I))) {
1319
27
      if (I == Attribute::Alignment)
1320
0
        B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1321
27
      else if (I == Attribute::StackAlignment)
1322
0
        B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1323
27
      else
1324
27
        B.addAttribute(I);
1325
27
    }
1326
1.06k
  }
1327
18
}
1328
1329
/// This fills an AttrBuilder object with the LLVM attributes that have
1330
/// been decoded from the given integer. This function must stay in sync with
1331
/// 'encodeLLVMAttributesForBitcode'.
1332
static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1333
18
                                           uint64_t EncodedAttrs) {
1334
18
  // FIXME: Remove in 4.0.
1335
18
1336
18
  // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
1337
18
  // the bits above 31 down by 11 bits.
1338
18
  unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1339
18
  assert((!Alignment || isPowerOf2_32(Alignment)) &&
1340
18
         "Alignment must be a power of two.");
1341
18
1342
18
  if (Alignment)
1343
0
    B.addAlignmentAttr(Alignment);
1344
18
  addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1345
18
                          (EncodedAttrs & 0xffff));
1346
18
}
1347
1348
5.12k
Error BitcodeReader::parseAttributeBlock() {
1349
5.12k
  if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1350
0
    return Err;
1351
5.12k
1352
5.12k
  if (!MAttributes.empty())
1353
0
    return error("Invalid multiple blocks");
1354
5.12k
1355
5.12k
  SmallVector<uint64_t, 64> Record;
1356
5.12k
1357
5.12k
  SmallVector<AttributeList, 8> Attrs;
1358
5.12k
1359
5.12k
  // Read all the records.
1360
86.7k
  while (
true86.7k
) {
1361
86.7k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1362
86.7k
    if (!MaybeEntry)
1363
0
      return MaybeEntry.takeError();
1364
86.7k
    BitstreamEntry Entry = MaybeEntry.get();
1365
86.7k
1366
86.7k
    switch (Entry.Kind) {
1367
86.7k
    case BitstreamEntry::SubBlock: // Handled for us already.
1368
0
    case BitstreamEntry::Error:
1369
0
      return error("Malformed block");
1370
5.12k
    case BitstreamEntry::EndBlock:
1371
5.12k
      return Error::success();
1372
81.6k
    case BitstreamEntry::Record:
1373
81.6k
      // The interesting case.
1374
81.6k
      break;
1375
81.6k
    }
1376
81.6k
1377
81.6k
    // Read a record.
1378
81.6k
    Record.clear();
1379
81.6k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1380
81.6k
    if (!MaybeRecord)
1381
0
      return MaybeRecord.takeError();
1382
81.6k
    switch (MaybeRecord.get()) {
1383
81.6k
    default:  // Default behavior: ignore.
1384
0
      break;
1385
81.6k
    case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1386
16
      // FIXME: Remove in 4.0.
1387
16
      if (Record.size() & 1)
1388
0
        return error("Invalid record");
1389
16
1390
34
      
for (unsigned i = 0, e = Record.size(); 16
i != e;
i += 218
) {
1391
18
        AttrBuilder B;
1392
18
        decodeLLVMAttributesForBitcode(B, Record[i+1]);
1393
18
        Attrs.push_back(AttributeList::get(Context, Record[i], B));
1394
18
      }
1395
16
1396
16
      MAttributes.push_back(AttributeList::get(Context, Attrs));
1397
16
      Attrs.clear();
1398
16
      break;
1399
81.6k
    case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1400
264k
      for (unsigned i = 0, e = Record.size(); i != e; 
++i183k
)
1401
183k
        Attrs.push_back(MAttributeGroups[Record[i]]);
1402
81.6k
1403
81.6k
      MAttributes.push_back(AttributeList::get(Context, Attrs));
1404
81.6k
      Attrs.clear();
1405
81.6k
      break;
1406
81.6k
    }
1407
81.6k
  }
1408
5.12k
}
1409
1410
// Returns Attribute::None on unrecognized codes.
1411
125k
static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
1412
125k
  switch (Code) {
1413
125k
  default:
1414
7
    return Attribute::None;
1415
125k
  case bitc::ATTR_KIND_ALIGNMENT:
1416
249
    return Attribute::Alignment;
1417
125k
  case bitc::ATTR_KIND_ALWAYS_INLINE:
1418
4.19k
    return Attribute::AlwaysInline;
1419
125k
  case bitc::ATTR_KIND_ARGMEMONLY:
1420
1.74k
    return Attribute::ArgMemOnly;
1421
125k
  case bitc::ATTR_KIND_BUILTIN:
1422
18
    return Attribute::Builtin;
1423
125k
  case bitc::ATTR_KIND_BY_VAL:
1424
44
    return Attribute::ByVal;
1425
125k
  case bitc::ATTR_KIND_IN_ALLOCA:
1426
20
    return Attribute::InAlloca;
1427
125k
  case bitc::ATTR_KIND_COLD:
1428
29
    return Attribute::Cold;
1429
125k
  case bitc::ATTR_KIND_CONVERGENT:
1430
15
    return Attribute::Convergent;
1431
125k
  case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
1432
13
    return Attribute::InaccessibleMemOnly;
1433
125k
  case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
1434
18
    return Attribute::InaccessibleMemOrArgMemOnly;
1435
125k
  case bitc::ATTR_KIND_INLINE_HINT:
1436
1.41k
    return Attribute::InlineHint;
1437
125k
  case bitc::ATTR_KIND_IN_REG:
1438
43
    return Attribute::InReg;
1439
125k
  case bitc::ATTR_KIND_JUMP_TABLE:
1440
15
    return Attribute::JumpTable;
1441
125k
  case bitc::ATTR_KIND_MIN_SIZE:
1442
22
    return Attribute::MinSize;
1443
125k
  case bitc::ATTR_KIND_NAKED:
1444
21
    return Attribute::Naked;
1445
125k
  case bitc::ATTR_KIND_NEST:
1446
21
    return Attribute::Nest;
1447
125k
  case bitc::ATTR_KIND_NO_ALIAS:
1448
32.6k
    return Attribute::NoAlias;
1449
125k
  case bitc::ATTR_KIND_NO_BUILTIN:
1450
2.81k
    return Attribute::NoBuiltin;
1451
125k
  case bitc::ATTR_KIND_NO_CAPTURE:
1452
4.62k
    return Attribute::NoCapture;
1453
125k
  case bitc::ATTR_KIND_NO_DUPLICATE:
1454
10
    return Attribute::NoDuplicate;
1455
125k
  case bitc::ATTR_KIND_NOFREE:
1456
8
    return Attribute::NoFree;
1457
125k
  case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1458
21
    return Attribute::NoImplicitFloat;
1459
125k
  case bitc::ATTR_KIND_NO_INLINE:
1460
3.85k
    return Attribute::NoInline;
1461
125k
  case bitc::ATTR_KIND_NO_RECURSE:
1462
1.81k
    return Attribute::NoRecurse;
1463
125k
  case bitc::ATTR_KIND_NON_LAZY_BIND:
1464
23
    return Attribute::NonLazyBind;
1465
125k
  case bitc::ATTR_KIND_NON_NULL:
1466
8.40k
    return Attribute::NonNull;
1467
125k
  case bitc::ATTR_KIND_DEREFERENCEABLE:
1468
13.7k
    return Attribute::Dereferenceable;
1469
125k
  case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1470
46
    return Attribute::DereferenceableOrNull;
1471
125k
  case bitc::ATTR_KIND_ALLOC_SIZE:
1472
10
    return Attribute::AllocSize;
1473
125k
  case bitc::ATTR_KIND_NO_RED_ZONE:
1474
22
    return Attribute::NoRedZone;
1475
125k
  case bitc::ATTR_KIND_NO_RETURN:
1476
55
    return Attribute::NoReturn;
1477
125k
  case bitc::ATTR_KIND_NOSYNC:
1478
5
    return Attribute::NoSync;
1479
125k
  case bitc::ATTR_KIND_NOCF_CHECK:
1480
0
    return Attribute::NoCfCheck;
1481
125k
  case bitc::ATTR_KIND_NO_UNWIND:
1482
20.3k
    return Attribute::NoUnwind;
1483
125k
  case bitc::ATTR_KIND_OPT_FOR_FUZZING:
1484
0
    return Attribute::OptForFuzzing;
1485
125k
  case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1486
25
    return Attribute::OptimizeForSize;
1487
125k
  case bitc::ATTR_KIND_OPTIMIZE_NONE:
1488
665
    return Attribute::OptimizeNone;
1489
125k
  case bitc::ATTR_KIND_READ_NONE:
1490
5.84k
    return Attribute::ReadNone;
1491
125k
  case bitc::ATTR_KIND_READ_ONLY:
1492
1.65k
    return Attribute::ReadOnly;
1493
125k
  case bitc::ATTR_KIND_RETURNED:
1494
21
    return Attribute::Returned;
1495
125k
  case bitc::ATTR_KIND_RETURNS_TWICE:
1496
21
    return Attribute::ReturnsTwice;
1497
125k
  case bitc::ATTR_KIND_S_EXT:
1498
388
    return Attribute::SExt;
1499
125k
  case bitc::ATTR_KIND_SPECULATABLE:
1500
86
    return Attribute::Speculatable;
1501
125k
  case bitc::ATTR_KIND_STACK_ALIGNMENT:
1502
33
    return Attribute::StackAlignment;
1503
125k
  case bitc::ATTR_KIND_STACK_PROTECT:
1504
63
    return Attribute::StackProtect;
1505
125k
  case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1506
21
    return Attribute::StackProtectReq;
1507
125k
  case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1508
23
    return Attribute::StackProtectStrong;
1509
125k
  case bitc::ATTR_KIND_SAFESTACK:
1510
9
    return Attribute::SafeStack;
1511
125k
  case bitc::ATTR_KIND_SHADOWCALLSTACK:
1512
5
    return Attribute::ShadowCallStack;
1513
125k
  case bitc::ATTR_KIND_STRICT_FP:
1514
5
    return Attribute::StrictFP;
1515
125k
  case bitc::ATTR_KIND_STRUCT_RET:
1516
6.97k
    return Attribute::StructRet;
1517
125k
  case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1518
21
    return Attribute::SanitizeAddress;
1519
125k
  case bitc::ATTR_KIND_SANITIZE_HWADDRESS:
1520
5
    return Attribute::SanitizeHWAddress;
1521
125k
  case bitc::ATTR_KIND_SANITIZE_THREAD:
1522
21
    return Attribute::SanitizeThread;
1523
125k
  case bitc::ATTR_KIND_SANITIZE_MEMORY:
1524
21
    return Attribute::SanitizeMemory;
1525
125k
  case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
1526
0
    return Attribute::SpeculativeLoadHardening;
1527
125k
  case bitc::ATTR_KIND_SWIFT_ERROR:
1528
10
    return Attribute::SwiftError;
1529
125k
  case bitc::ATTR_KIND_SWIFT_SELF:
1530
5
    return Attribute::SwiftSelf;
1531
125k
  case bitc::ATTR_KIND_UW_TABLE:
1532
1.46k
    return Attribute::UWTable;
1533
125k
  case bitc::ATTR_KIND_WILLRETURN:
1534
5
    return Attribute::WillReturn;
1535
125k
  case bitc::ATTR_KIND_WRITEONLY:
1536
1.48k
    return Attribute::WriteOnly;
1537
125k
  case bitc::ATTR_KIND_Z_EXT:
1538
9.77k
    return Attribute::ZExt;
1539
125k
  case bitc::ATTR_KIND_IMMARG:
1540
140
    return Attribute::ImmArg;
1541
125k
  case bitc::ATTR_KIND_SANITIZE_MEMTAG:
1542
5
    return Attribute::SanitizeMemTag;
1543
125k
  }
1544
125k
}
1545
1546
Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1547
3.84M
                                         unsigned &Alignment) {
1548
3.84M
  // Note: Alignment in bitcode files is incremented by 1, so that zero
1549
3.84M
  // can be used for default alignment.
1550
3.84M
  if (Exponent > Value::MaxAlignmentExponent + 1)
1551
1
    return error("Invalid alignment value");
1552
3.84M
  Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1553
3.84M
  return Error::success();
1554
3.84M
}
1555
1556
125k
Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
1557
125k
  *Kind = getAttrFromCode(Code);
1558
125k
  if (*Kind == Attribute::None)
1559
7
    return error("Unknown attribute kind (" + Twine(Code) + ")");
1560
125k
  return Error::success();
1561
125k
}
1562
1563
5.12k
Error BitcodeReader::parseAttributeGroupBlock() {
1564
5.12k
  if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1565
0
    return Err;
1566
5.12k
1567
5.12k
  if (!MAttributeGroups.empty())
1568
0
    return error("Invalid multiple blocks");
1569
5.12k
1570
5.12k
  SmallVector<uint64_t, 64> Record;
1571
5.12k
1572
5.12k
  // Read all the records.
1573
107k
  while (
true107k
) {
1574
107k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1575
107k
    if (!MaybeEntry)
1576
0
      return MaybeEntry.takeError();
1577
107k
    BitstreamEntry Entry = MaybeEntry.get();
1578
107k
1579
107k
    switch (Entry.Kind) {
1580
107k
    case BitstreamEntry::SubBlock: // Handled for us already.
1581
0
    case BitstreamEntry::Error:
1582
0
      return error("Malformed block");
1583
5.11k
    case BitstreamEntry::EndBlock:
1584
5.11k
      return Error::success();
1585
102k
    case BitstreamEntry::Record:
1586
102k
      // The interesting case.
1587
102k
      break;
1588
102k
    }
1589
102k
1590
102k
    // Read a record.
1591
102k
    Record.clear();
1592
102k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1593
102k
    if (!MaybeRecord)
1594
0
      return MaybeRecord.takeError();
1595
102k
    switch (MaybeRecord.get()) {
1596
102k
    default:  // Default behavior: ignore.
1597
0
      break;
1598
102k
    case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1599
102k
      if (Record.size() < 3)
1600
0
        return error("Invalid record");
1601
102k
1602
102k
      uint64_t GrpID = Record[0];
1603
102k
      uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1604
102k
1605
102k
      AttrBuilder B;
1606
330k
      for (unsigned i = 2, e = Record.size(); i != e; 
++i227k
) {
1607
227k
        if (Record[i] == 0) {        // Enum attribute
1608
111k
          Attribute::AttrKind Kind;
1609
111k
          if (Error Err = parseAttrKind(Record[++i], &Kind))
1610
7
            return Err;
1611
111k
1612
111k
          // Upgrade old-style byval attribute to one with a type, even if it's
1613
111k
          // nullptr. We will have to insert the real type when we associate
1614
111k
          // this AttributeList with a function.
1615
111k
          if (Kind == Attribute::ByVal)
1616
12
            B.addByValAttr(nullptr);
1617
111k
1618
111k
          B.addAttribute(Kind);
1619
116k
        } else if (Record[i] == 1) { // Integer attribute
1620
14.0k
          Attribute::AttrKind Kind;
1621
14.0k
          if (Error Err = parseAttrKind(Record[++i], &Kind))
1622
0
            return Err;
1623
14.0k
          if (Kind == Attribute::Alignment)
1624
249
            B.addAlignmentAttr(Record[++i]);
1625
13.8k
          else if (Kind == Attribute::StackAlignment)
1626
33
            B.addStackAlignmentAttr(Record[++i]);
1627
13.7k
          else if (Kind == Attribute::Dereferenceable)
1628
13.7k
            B.addDereferenceableAttr(Record[++i]);
1629
56
          else if (Kind == Attribute::DereferenceableOrNull)
1630
46
            B.addDereferenceableOrNullAttr(Record[++i]);
1631
10
          else if (Kind == Attribute::AllocSize)
1632
10
            B.addAllocSizeAttrFromRawRepr(Record[++i]);
1633
102k
        } else if (Record[i] == 3 || 
Record[i] == 494.3k
) { // String attribute
1634
102k
          bool HasValue = (Record[i++] == 4);
1635
102k
          SmallString<64> KindStr;
1636
102k
          SmallString<64> ValStr;
1637
102k
1638
2.07M
          while (Record[i] != 0 && 
i != e1.96M
)
1639
1.96M
            KindStr += Record[i++];
1640
102k
          assert(Record[i] == 0 && "Kind string not null terminated");
1641
102k
1642
102k
          if (HasValue) {
1643
94.3k
            // Has a value associated with it.
1644
94.3k
            ++i; // Skip the '0' that terminates the "kind" string.
1645
564k
            while (Record[i] != 0 && 
i != e469k
)
1646
469k
              ValStr += Record[i++];
1647
94.3k
            assert(Record[i] == 0 && "Value string not null terminated");
1648
94.3k
          }
1649
102k
1650
102k
          B.addAttribute(KindStr.str(), ValStr.str());
1651
102k
        } else {
1652
32
          assert((Record[i] == 5 || Record[i] == 6) &&
1653
32
                 "Invalid attribute group entry");
1654
32
          bool HasType = Record[i] == 6;
1655
32
          Attribute::AttrKind Kind;
1656
32
          if (Error Err = parseAttrKind(Record[++i], &Kind))
1657
0
            return Err;
1658
32
          if (Kind == Attribute::ByVal)
1659
32
            B.addByValAttr(HasType ? 
getTypeByID(Record[++i])20
:
nullptr12
);
1660
32
        }
1661
227k
      }
1662
102k
1663
102k
      MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
1664
102k
      break;
1665
102k
    }
1666
102k
    }
1667
102k
  }
1668
5.12k
}
1669
1670
7.76k
Error BitcodeReader::parseTypeTable() {
1671
7.76k
  if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1672
0
    return Err;
1673
7.76k
1674
7.76k
  return parseTypeTableBody();
1675
7.76k
}
1676
1677
7.76k
Error BitcodeReader::parseTypeTableBody() {
1678
7.76k
  if (!TypeList.empty())
1679
0
    return error("Invalid multiple blocks");
1680
7.76k
1681
7.76k
  SmallVector<uint64_t, 64> Record;
1682
7.76k
  unsigned NumRecords = 0;
1683
7.76k
1684
7.76k
  SmallString<64> TypeName;
1685
7.76k
1686
7.76k
  // Read all the records for this type table.
1687
1.65M
  while (
true1.65M
) {
1688
1.65M
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1689
1.65M
    if (!MaybeEntry)
1690
0
      return MaybeEntry.takeError();
1691
1.65M
    BitstreamEntry Entry = MaybeEntry.get();
1692
1.65M
1693
1.65M
    switch (Entry.Kind) {
1694
1.65M
    case BitstreamEntry::SubBlock: // Handled for us already.
1695
0
    case BitstreamEntry::Error:
1696
0
      return error("Malformed block");
1697
7.76k
    case BitstreamEntry::EndBlock:
1698
7.76k
      if (NumRecords != TypeList.size())
1699
0
        return error("Malformed block");
1700
7.76k
      return Error::success();
1701
1.64M
    case BitstreamEntry::Record:
1702
1.64M
      // The interesting case.
1703
1.64M
      break;
1704
1.64M
    }
1705
1.64M
1706
1.64M
    // Read a record.
1707
1.64M
    Record.clear();
1708
1.64M
    Type *ResultTy = nullptr;
1709
1.64M
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1710
1.64M
    if (!MaybeRecord)
1711
0
      return MaybeRecord.takeError();
1712
1.64M
    switch (MaybeRecord.get()) {
1713
1.64M
    default:
1714
0
      return error("Invalid value");
1715
1.64M
    case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1716
7.76k
      // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1717
7.76k
      // type list.  This allows us to reserve space.
1718
7.76k
      if (Record.size() < 1)
1719
0
        return error("Invalid record");
1720
7.76k
      TypeList.resize(Record[0]);
1721
7.76k
      continue;
1722
7.76k
    case bitc::TYPE_CODE_VOID:      // VOID
1723
7.08k
      ResultTy = Type::getVoidTy(Context);
1724
7.08k
      break;
1725
7.76k
    case bitc::TYPE_CODE_HALF:     // HALF
1726
36
      ResultTy = Type::getHalfTy(Context);
1727
36
      break;
1728
7.76k
    case bitc::TYPE_CODE_FLOAT:     // FLOAT
1729
4.54k
      ResultTy = Type::getFloatTy(Context);
1730
4.54k
      break;
1731
7.76k
    case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
1732
4.41k
      ResultTy = Type::getDoubleTy(Context);
1733
4.41k
      break;
1734
7.76k
    case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
1735
26
      ResultTy = Type::getX86_FP80Ty(Context);
1736
26
      break;
1737
7.76k
    case bitc::TYPE_CODE_FP128:     // FP128
1738
22
      ResultTy = Type::getFP128Ty(Context);
1739
22
      break;
1740
7.76k
    case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1741
22
      ResultTy = Type::getPPC_FP128Ty(Context);
1742
22
      break;
1743
7.76k
    case bitc::TYPE_CODE_LABEL:     // LABEL
1744
4.79k
      ResultTy = Type::getLabelTy(Context);
1745
4.79k
      break;
1746
7.76k
    case bitc::TYPE_CODE_METADATA:  // METADATA
1747
7.69k
      ResultTy = Type::getMetadataTy(Context);
1748
7.69k
      break;
1749
7.76k
    case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
1750
17
      ResultTy = Type::getX86_MMXTy(Context);
1751
17
      break;
1752
7.76k
    case bitc::TYPE_CODE_TOKEN:     // TOKEN
1753
18
      ResultTy = Type::getTokenTy(Context);
1754
18
      break;
1755
26.4k
    case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1756
26.4k
      if (Record.size() < 1)
1757
0
        return error("Invalid record");
1758
26.4k
1759
26.4k
      uint64_t NumBits = Record[0];
1760
26.4k
      if (NumBits < IntegerType::MIN_INT_BITS ||
1761
26.4k
          NumBits > IntegerType::MAX_INT_BITS)
1762
0
        return error("Bitwidth for integer type out of range");
1763
26.4k
      ResultTy = IntegerType::get(Context, NumBits);
1764
26.4k
      break;
1765
26.4k
    }
1766
751k
    case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1767
751k
                                    //          [pointee type, address space]
1768
751k
      if (Record.size() < 1)
1769
0
        return error("Invalid record");
1770
751k
      unsigned AddressSpace = 0;
1771
751k
      if (Record.size() == 2)
1772
751k
        AddressSpace = Record[1];
1773
751k
      ResultTy = getTypeByID(Record[0]);
1774
751k
      if (!ResultTy ||
1775
751k
          
!PointerType::isValidElementType(ResultTy)751k
)
1776
1
        return error("Invalid type");
1777
751k
      ResultTy = PointerType::get(ResultTy, AddressSpace);
1778
751k
      break;
1779
751k
    }
1780
751k
    case bitc::TYPE_CODE_FUNCTION_OLD: {
1781
0
      // FIXME: attrid is dead, remove it in LLVM 4.0
1782
0
      // FUNCTION: [vararg, attrid, retty, paramty x N]
1783
0
      if (Record.size() < 3)
1784
0
        return error("Invalid record");
1785
0
      SmallVector<Type*, 8> ArgTys;
1786
0
      for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1787
0
        if (Type *T = getTypeByID(Record[i]))
1788
0
          ArgTys.push_back(T);
1789
0
        else
1790
0
          break;
1791
0
      }
1792
0
1793
0
      ResultTy = getTypeByID(Record[2]);
1794
0
      if (!ResultTy || ArgTys.size() < Record.size()-3)
1795
0
        return error("Invalid type");
1796
0
1797
0
      ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1798
0
      break;
1799
0
    }
1800
459k
    case bitc::TYPE_CODE_FUNCTION: {
1801
459k
      // FUNCTION: [vararg, retty, paramty x N]
1802
459k
      if (Record.size() < 2)
1803
0
        return error("Invalid record");
1804
459k
      SmallVector<Type*, 8> ArgTys;
1805
1.57M
      for (unsigned i = 2, e = Record.size(); i != e; 
++i1.11M
) {
1806
1.11M
        if (Type *T = getTypeByID(Record[i])) {
1807
1.11M
          if (!FunctionType::isValidArgumentType(T))
1808
1
            return error("Invalid function argument type");
1809
1.11M
          ArgTys.push_back(T);
1810
1.11M
        }
1811
0
        else
1812
0
          break;
1813
1.11M
      }
1814
459k
1815
459k
      ResultTy = getTypeByID(Record[1]);
1816
459k
      if (
!ResultTy459k
|| ArgTys.size() < Record.size()-2)
1817
0
        return error("Invalid type");
1818
459k
1819
459k
      ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1820
459k
      break;
1821
459k
    }
1822
459k
    case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
1823
5.07k
      if (Record.size() < 1)
1824
0
        return error("Invalid record");
1825
5.07k
      SmallVector<Type*, 8> EltTys;
1826
25.2k
      for (unsigned i = 1, e = Record.size(); i != e; 
++i20.1k
) {
1827
20.1k
        if (Type *T = getTypeByID(Record[i]))
1828
20.1k
          EltTys.push_back(T);
1829
0
        else
1830
0
          break;
1831
20.1k
      }
1832
5.07k
      if (EltTys.size() != Record.size()-1)
1833
0
        return error("Invalid type");
1834
5.07k
      ResultTy = StructType::get(Context, EltTys, Record[0]);
1835
5.07k
      break;
1836
5.07k
    }
1837
89.0k
    case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
1838
89.0k
      if (convertToString(Record, 0, TypeName))
1839
0
        return error("Invalid record");
1840
89.0k
      continue;
1841
89.0k
1842
89.0k
    case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1843
73.8k
      if (Record.size() < 1)
1844
0
        return error("Invalid record");
1845
73.8k
1846
73.8k
      if (NumRecords >= TypeList.size())
1847
0
        return error("Invalid TYPE table");
1848
73.8k
1849
73.8k
      // Check to see if this was forward referenced, if so fill in the temp.
1850
73.8k
      StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1851
73.8k
      if (Res) {
1852
2.88k
        Res->setName(TypeName);
1853
2.88k
        TypeList[NumRecords] = nullptr;
1854
2.88k
      } else  // Otherwise, create a new struct.
1855
70.9k
        Res = createIdentifiedStructType(Context, TypeName);
1856
73.8k
      TypeName.clear();
1857
73.8k
1858
73.8k
      SmallVector<Type*, 8> EltTys;
1859
488k
      for (unsigned i = 1, e = Record.size(); i != e; 
++i414k
) {
1860
414k
        if (Type *T = getTypeByID(Record[i]))
1861
414k
          EltTys.push_back(T);
1862
0
        else
1863
0
          break;
1864
414k
      }
1865
73.8k
      if (EltTys.size() != Record.size()-1)
1866
0
        return error("Invalid record");
1867
73.8k
      Res->setBody(EltTys, Record[0]);
1868
73.8k
      ResultTy = Res;
1869
73.8k
      break;
1870
73.8k
    }
1871
73.8k
    case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
1872
15.3k
      if (Record.size() != 1)
1873
0
        return error("Invalid record");
1874
15.3k
1875
15.3k
      if (NumRecords >= TypeList.size())
1876
0
        return error("Invalid TYPE table");
1877
15.3k
1878
15.3k
      // Check to see if this was forward referenced, if so fill in the temp.
1879
15.3k
      StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1880
15.3k
      if (Res) {
1881
0
        Res->setName(TypeName);
1882
0
        TypeList[NumRecords] = nullptr;
1883
0
      } else  // Otherwise, create a new struct with no body.
1884
15.3k
        Res = createIdentifiedStructType(Context, TypeName);
1885
15.3k
      TypeName.clear();
1886
15.3k
      ResultTy = Res;
1887
15.3k
      break;
1888
15.3k
    }
1889
142k
    case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
1890
142k
      if (Record.size() < 2)
1891
0
        return error("Invalid record");
1892
142k
      ResultTy = getTypeByID(Record[1]);
1893
142k
      if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
1894
1
        return error("Invalid type");
1895
142k
      ResultTy = ArrayType::get(ResultTy, Record[0]);
1896
142k
      break;
1897
142k
    case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty] or
1898
44.7k
                                    //         [numelts, eltty, scalable]
1899
44.7k
      if (Record.size() < 2)
1900
0
        return error("Invalid record");
1901
44.7k
      if (Record[0] == 0)
1902
1
        return error("Invalid vector length");
1903
44.7k
      ResultTy = getTypeByID(Record[1]);
1904
44.7k
      if (!ResultTy || !StructType::isValidElementType(ResultTy))
1905
1
        return error("Invalid type");
1906
44.7k
      bool Scalable = Record.size() > 2 ? 
Record[2]2
:
false44.7k
;
1907
44.7k
      ResultTy = VectorType::get(ResultTy, Record[0], Scalable);
1908
44.7k
      break;
1909
1.54M
    }
1910
1.54M
1911
1.54M
    if (NumRecords >= TypeList.size())
1912
0
      return error("Invalid TYPE table");
1913
1.54M
    if (TypeList[NumRecords])
1914
1
      return error(
1915
1
          "Invalid TYPE table: Only named structs can be forward referenced");
1916
1.54M
    assert(ResultTy && "Didn't read a type?");
1917
1.54M
    TypeList[NumRecords++] = ResultTy;
1918
1.54M
  }
1919
7.76k
}
1920
1921
7.65k
Error BitcodeReader::parseOperandBundleTags() {
1922
7.65k
  if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1923
0
    return Err;
1924
7.65k
1925
7.65k
  if (!BundleTags.empty())
1926
0
    return error("Invalid multiple blocks");
1927
7.65k
1928
7.65k
  SmallVector<uint64_t, 64> Record;
1929
7.65k
1930
30.6k
  while (
true30.6k
) {
1931
30.6k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1932
30.6k
    if (!MaybeEntry)
1933
0
      return MaybeEntry.takeError();
1934
30.6k
    BitstreamEntry Entry = MaybeEntry.get();
1935
30.6k
1936
30.6k
    switch (Entry.Kind) {
1937
30.6k
    case BitstreamEntry::SubBlock: // Handled for us already.
1938
0
    case BitstreamEntry::Error:
1939
0
      return error("Malformed block");
1940
7.66k
    case BitstreamEntry::EndBlock:
1941
7.66k
      return Error::success();
1942
22.9k
    case BitstreamEntry::Record:
1943
22.9k
      // The interesting case.
1944
22.9k
      break;
1945
22.9k
    }
1946
22.9k
1947
22.9k
    // Tags are implicitly mapped to integers by their order.
1948
22.9k
1949
22.9k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1950
22.9k
    if (!MaybeRecord)
1951
0
      return MaybeRecord.takeError();
1952
22.9k
    if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG)
1953
0
      return error("Invalid record");
1954
22.9k
1955
22.9k
    // OPERAND_BUNDLE_TAG: [strchr x N]
1956
22.9k
    BundleTags.emplace_back();
1957
22.9k
    if (convertToString(Record, 0, BundleTags.back()))
1958
0
      return error("Invalid record");
1959
22.9k
    Record.clear();
1960
22.9k
  }
1961
7.65k
}
1962
1963
3.45k
Error BitcodeReader::parseSyncScopeNames() {
1964
3.45k
  if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
1965
0
    return Err;
1966
3.45k
1967
3.45k
  if (!SSIDs.empty())
1968
0
    return error("Invalid multiple synchronization scope names blocks");
1969
3.45k
1970
3.45k
  SmallVector<uint64_t, 64> Record;
1971
10.4k
  while (
true10.4k
) {
1972
10.4k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
1973
10.4k
    if (!MaybeEntry)
1974
0
      return MaybeEntry.takeError();
1975
10.4k
    BitstreamEntry Entry = MaybeEntry.get();
1976
10.4k
1977
10.4k
    switch (Entry.Kind) {
1978
10.4k
    case BitstreamEntry::SubBlock: // Handled for us already.
1979
0
    case BitstreamEntry::Error:
1980
0
      return error("Malformed block");
1981
3.46k
    case BitstreamEntry::EndBlock:
1982
3.46k
      if (SSIDs.empty())
1983
0
        return error("Invalid empty synchronization scope names block");
1984
3.46k
      return Error::success();
1985
6.94k
    case BitstreamEntry::Record:
1986
6.94k
      // The interesting case.
1987
6.94k
      break;
1988
6.94k
    }
1989
6.94k
1990
6.94k
    // Synchronization scope names are implicitly mapped to synchronization
1991
6.94k
    // scope IDs by their order.
1992
6.94k
1993
6.94k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
1994
6.94k
    if (!MaybeRecord)
1995
0
      return MaybeRecord.takeError();
1996
6.94k
    if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME)
1997
0
      return error("Invalid record");
1998
6.94k
1999
6.94k
    SmallString<16> SSN;
2000
6.94k
    if (convertToString(Record, 0, SSN))
2001
0
      return error("Invalid record");
2002
6.94k
2003
6.94k
    SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN));
2004
6.94k
    Record.clear();
2005
6.94k
  }
2006
3.45k
}
2007
2008
/// Associate a value with its name from the given index in the provided record.
2009
Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
2010
7.38M
                                             unsigned NameIndex, Triple &TT) {
2011
7.38M
  SmallString<128> ValueName;
2012
7.38M
  if (convertToString(Record, NameIndex, ValueName))
2013
0
    return error("Invalid record");
2014
7.38M
  unsigned ValueID = Record[0];
2015
7.38M
  if (ValueID >= ValueList.size() || !ValueList[ValueID])
2016
0
    return error("Invalid record");
2017
7.38M
  Value *V = ValueList[ValueID];
2018
7.38M
2019
7.38M
  StringRef NameStr(ValueName.data(), ValueName.size());
2020
7.38M
  if (NameStr.find_first_of(0) != StringRef::npos)
2021
0
    return error("Invalid value name");
2022
7.38M
  V->setName(NameStr);
2023
7.38M
  auto *GO = dyn_cast<GlobalObject>(V);
2024
7.38M
  if (GO) {
2025
1.41M
    if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
2026
17
      if (TT.supportsCOMDAT())
2027
12
        GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
2028
5
      else
2029
5
        GO->setComdat(nullptr);
2030
17
    }
2031
1.41M
  }
2032
7.38M
  return V;
2033
7.38M
}
2034
2035
/// Helper to note and return the current location, and jump to the given
2036
/// offset.
2037
static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset,
2038
6.93k
                                                 BitstreamCursor &Stream) {
2039
6.93k
  // Save the current parsing location so we can jump back at the end
2040
6.93k
  // of the VST read.
2041
6.93k
  uint64_t CurrentBit = Stream.GetCurrentBitNo();
2042
6.93k
  if (Error JumpFailed = Stream.JumpToBit(Offset * 32))
2043
0
    return std::move(JumpFailed);
2044
6.93k
  Expected<BitstreamEntry> MaybeEntry = Stream.advance();
2045
6.93k
  if (!MaybeEntry)
2046
0
    return MaybeEntry.takeError();
2047
6.93k
  assert(MaybeEntry.get().Kind == BitstreamEntry::SubBlock);
2048
6.93k
  assert(MaybeEntry.get().ID == bitc::VALUE_SYMTAB_BLOCK_ID);
2049
6.93k
  return CurrentBit;
2050
6.93k
}
2051
2052
void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
2053
                                            Function *F,
2054
337k
                                            ArrayRef<uint64_t> Record) {
2055
337k
  // Note that we subtract 1 here because the offset is relative to one word
2056
337k
  // before the start of the identification or module block, which was
2057
337k
  // historically always the start of the regular bitcode header.
2058
337k
  uint64_t FuncWordOffset = Record[1] - 1;
2059
337k
  uint64_t FuncBitOffset = FuncWordOffset * 32;
2060
337k
  DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2061
337k
  // Set the LastFunctionBlockBit to point to the last function block.
2062
337k
  // Later when parsing is resumed after function materialization,
2063
337k
  // we can simply skip that last function block.
2064
337k
  if (FuncBitOffset > LastFunctionBlockBit)
2065
23.6k
    LastFunctionBlockBit = FuncBitOffset;
2066
337k
}
2067
2068
/// Read a new-style GlobalValue symbol table.
2069
2.75k
Error BitcodeReader::parseGlobalValueSymbolTable() {
2070
2.75k
  unsigned FuncBitcodeOffsetDelta =
2071
2.75k
      Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2072
2.75k
2073
2.75k
  if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2074
0
    return Err;
2075
2.75k
2076
2.75k
  SmallVector<uint64_t, 64> Record;
2077
16.6k
  while (true) {
2078
16.6k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2079
16.6k
    if (!MaybeEntry)
2080
0
      return MaybeEntry.takeError();
2081
16.6k
    BitstreamEntry Entry = MaybeEntry.get();
2082
16.6k
2083
16.6k
    switch (Entry.Kind) {
2084
16.6k
    case BitstreamEntry::SubBlock:
2085
0
    case BitstreamEntry::Error:
2086
0
      return error("Malformed block");
2087
2.75k
    case BitstreamEntry::EndBlock:
2088
2.75k
      return Error::success();
2089
13.8k
    case BitstreamEntry::Record:
2090
13.8k
      break;
2091
13.8k
    }
2092
13.8k
2093
13.8k
    Record.clear();
2094
13.8k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2095
13.8k
    if (!MaybeRecord)
2096
0
      return MaybeRecord.takeError();
2097
13.8k
    switch (MaybeRecord.get()) {
2098
13.8k
    case bitc::VST_CODE_FNENTRY: // [valueid, offset]
2099
13.8k
      setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
2100
13.8k
                              cast<Function>(ValueList[Record[0]]), Record);
2101
13.8k
      break;
2102
13.8k
    }
2103
13.8k
  }
2104
2.75k
}
2105
2106
/// Parse the value symbol table at either the current parsing location or
2107
/// at the given bit offset if provided.
2108
334k
Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
2109
334k
  uint64_t CurrentBit;
2110
334k
  // Pass in the Offset to distinguish between calling for the module-level
2111
334k
  // VST (where we want to jump to the VST offset) and the function-level
2112
334k
  // VST (where we don't).
2113
334k
  if (Offset > 0) {
2114
6.91k
    Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream);
2115
6.91k
    if (!MaybeCurrentBit)
2116
0
      return MaybeCurrentBit.takeError();
2117
6.91k
    CurrentBit = MaybeCurrentBit.get();
2118
6.91k
    // If this module uses a string table, read this as a module-level VST.
2119
6.91k
    if (UseStrtab) {
2120
2.75k
      if (Error Err = parseGlobalValueSymbolTable())
2121
0
        return Err;
2122
2.75k
      if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2123
0
        return JumpFailed;
2124
2.75k
      return Error::success();
2125
2.75k
    }
2126
6.91k
    // Otherwise, the VST will be in a similar format to a function-level VST,
2127
6.91k
    // and will contain symbol names.
2128
6.91k
  }
2129
332k
2130
332k
  // Compute the delta between the bitcode indices in the VST (the word offset
2131
332k
  // to the word-aligned ENTER_SUBBLOCK for the function block, and that
2132
332k
  // expected by the lazy reader. The reader's EnterSubBlock expects to have
2133
332k
  // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
2134
332k
  // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
2135
332k
  // just before entering the VST subblock because: 1) the EnterSubBlock
2136
332k
  // changes the AbbrevID width; 2) the VST block is nested within the same
2137
332k
  // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
2138
332k
  // AbbrevID width before calling EnterSubBlock; and 3) when we want to
2139
332k
  // jump to the FUNCTION_BLOCK using this offset later, we don't want
2140
332k
  // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
2141
332k
  unsigned FuncBitcodeOffsetDelta =
2142
332k
      Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
2143
332k
2144
332k
  if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
2145
0
    return Err;
2146
332k
2147
332k
  SmallVector<uint64_t, 64> Record;
2148
332k
2149
332k
  Triple TT(TheModule->getTargetTriple());
2150
332k
2151
332k
  // Read all the records for this value table.
2152
332k
  SmallString<128> ValueName;
2153
332k
2154
9.15M
  while (
true9.15M
) {
2155
9.15M
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2156
9.15M
    if (!MaybeEntry)
2157
0
      return MaybeEntry.takeError();
2158
9.15M
    BitstreamEntry Entry = MaybeEntry.get();
2159
9.15M
2160
9.15M
    switch (Entry.Kind) {
2161
9.15M
    case BitstreamEntry::SubBlock: // Handled for us already.
2162
0
    case BitstreamEntry::Error:
2163
0
      return error("Malformed block");
2164
332k
    case BitstreamEntry::EndBlock:
2165
332k
      if (Offset > 0)
2166
4.18k
        if (Error JumpFailed = Stream.JumpToBit(CurrentBit))
2167
0
          return JumpFailed;
2168
332k
      return Error::success();
2169
8.82M
    case BitstreamEntry::Record:
2170
8.82M
      // The interesting case.
2171
8.82M
      break;
2172
8.82M
    }
2173
8.82M
2174
8.82M
    // Read a record.
2175
8.82M
    Record.clear();
2176
8.82M
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2177
8.82M
    if (!MaybeRecord)
2178
0
      return MaybeRecord.takeError();
2179
8.82M
    switch (MaybeRecord.get()) {
2180
8.82M
    default:  // Default behavior: unknown type.
2181
0
      break;
2182
8.82M
    case bitc::VST_CODE_ENTRY: {  // VST_CODE_ENTRY: [valueid, namechar x N]
2183
7.05M
      Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2184
7.05M
      if (Error Err = ValOrErr.takeError())
2185
0
        return Err;
2186
7.05M
      ValOrErr.get();
2187
7.05M
      break;
2188
7.05M
    }
2189
7.05M
    case bitc::VST_CODE_FNENTRY: {
2190
324k
      // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2191
324k
      Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2192
324k
      if (Error Err = ValOrErr.takeError())
2193
0
        return Err;
2194
324k
      Value *V = ValOrErr.get();
2195
324k
2196
324k
      // Ignore function offsets emitted for aliases of functions in older
2197
324k
      // versions of LLVM.
2198
324k
      if (auto *F = dyn_cast<Function>(V))
2199
324k
        setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2200
324k
      break;
2201
324k
    }
2202
1.44M
    case bitc::VST_CODE_BBENTRY: {
2203
1.44M
      if (convertToString(Record, 1, ValueName))
2204
0
        return error("Invalid record");
2205
1.44M
      BasicBlock *BB = getBasicBlock(Record[0]);
2206
1.44M
      if (!BB)
2207
0
        return error("Invalid record");
2208
1.44M
2209
1.44M
      BB->setName(StringRef(ValueName.data(), ValueName.size()));
2210
1.44M
      ValueName.clear();
2211
1.44M
      break;
2212
1.44M
    }
2213
8.82M
    }
2214
8.82M
  }
2215
332k
}
2216
2217
/// Decode a signed value stored with the sign bit in the LSB for dense VBR
2218
/// encoding.
2219
1.56M
uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2220
1.56M
  if ((V & 1) == 0)
2221
1.18M
    return V >> 1;
2222
382k
  if (V != 1)
2223
382k
    return -(V >> 1);
2224
8
  // There is no such thing as -0 with integers.  "-0" really means MININT.
2225
8
  return 1ULL << 63;
2226
8
}
2227
2228
/// Resolve all of the initializers for global values and aliases that we can.
2229
27.1k
Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2230
27.1k
  std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2231
27.1k
  std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>
2232
27.1k
      IndirectSymbolInitWorklist;
2233
27.1k
  std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist;
2234
27.1k
  std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist;
2235
27.1k
  std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist;
2236
27.1k
2237
27.1k
  GlobalInitWorklist.swap(GlobalInits);
2238
27.1k
  IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2239
27.1k
  FunctionPrefixWorklist.swap(FunctionPrefixes);
2240
27.1k
  FunctionPrologueWorklist.swap(FunctionPrologues);
2241
27.1k
  FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
2242
27.1k
2243
345k
  while (!GlobalInitWorklist.empty()) {
2244
318k
    unsigned ValID = GlobalInitWorklist.back().second;
2245
318k
    if (ValID >= ValueList.size()) {
2246
0
      // Not ready to resolve this yet, it requires something later in the file.
2247
0
      GlobalInits.push_back(GlobalInitWorklist.back());
2248
318k
    } else {
2249
318k
      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2250
318k
        GlobalInitWorklist.back().first->setInitializer(C);
2251
18.4E
      else
2252
18.4E
        return error("Expected a constant");
2253
318k
    }
2254
318k
    GlobalInitWorklist.pop_back();
2255
318k
  }
2256
27.1k
2257
28.0k
  
while (27.1k
!IndirectSymbolInitWorklist.empty()) {
2258
912
    unsigned ValID = IndirectSymbolInitWorklist.back().second;
2259
912
    if (ValID >= ValueList.size()) {
2260
0
      IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2261
912
    } else {
2262
912
      Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2263
912
      if (!C)
2264
0
        return error("Expected a constant");
2265
912
      GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first;
2266
912
      if (isa<GlobalAlias>(GIS) && 
C->getType() != GIS->getType()848
)
2267
1
        return error("Alias and aliasee types don't match");
2268
911
      GIS->setIndirectSymbol(C);
2269
911
    }
2270
912
    IndirectSymbolInitWorklist.pop_back();
2271
911
  }
2272
27.1k
2273
27.1k
  
while (27.1k
!FunctionPrefixWorklist.empty()) {
2274
31
    unsigned ValID = FunctionPrefixWorklist.back().second;
2275
31
    if (ValID >= ValueList.size()) {
2276
0
      FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2277
31
    } else {
2278
31
      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2279
31
        FunctionPrefixWorklist.back().first->setPrefixData(C);
2280
0
      else
2281
0
        return error("Expected a constant");
2282
31
    }
2283
31
    FunctionPrefixWorklist.pop_back();
2284
31
  }
2285
27.1k
2286
27.1k
  
while (27.1k
!FunctionPrologueWorklist.empty()) {
2287
25
    unsigned ValID = FunctionPrologueWorklist.back().second;
2288
25
    if (ValID >= ValueList.size()) {
2289
0
      FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2290
25
    } else {
2291
25
      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2292
25
        FunctionPrologueWorklist.back().first->setPrologueData(C);
2293
0
      else
2294
0
        return error("Expected a constant");
2295
25
    }
2296
25
    FunctionPrologueWorklist.pop_back();
2297
25
  }
2298
27.1k
2299
27.2k
  
while (27.1k
!FunctionPersonalityFnWorklist.empty()) {
2300
153
    unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2301
153
    if (ValID >= ValueList.size()) {
2302
0
      FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2303
153
    } else {
2304
153
      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2305
153
        FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2306
0
      else
2307
0
        return error("Expected a constant");
2308
153
    }
2309
153
    FunctionPersonalityFnWorklist.pop_back();
2310
153
  }
2311
27.1k
2312
27.1k
  return Error::success();
2313
27.1k
}
2314
2315
47
static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
2316
47
  SmallVector<uint64_t, 8> Words(Vals.size());
2317
47
  transform(Vals, Words.begin(),
2318
47
                 BitcodeReader::decodeSignRotatedValue);
2319
47
2320
47
  return APInt(TypeBits, Words);
2321
47
}
2322
2323
159k
Error BitcodeReader::parseConstants() {
2324
159k
  if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2325
0
    return Err;
2326
159k
2327
159k
  SmallVector<uint64_t, 64> Record;
2328
159k
2329
159k
  // Read all the records for this value table.
2330
159k
  Type *CurTy = Type::getInt32Ty(Context);
2331
159k
  Type *CurFullTy = Type::getInt32Ty(Context);
2332
159k
  unsigned NextCstNo = ValueList.size();
2333
159k
2334
2.25M
  while (
true2.25M
) {
2335
2.25M
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2336
2.25M
    if (!MaybeEntry)
2337
0
      return MaybeEntry.takeError();
2338
2.25M
    BitstreamEntry Entry = MaybeEntry.get();
2339
2.25M
2340
2.25M
    switch (Entry.Kind) {
2341
2.25M
    case BitstreamEntry::SubBlock: // Handled for us already.
2342
0
    case BitstreamEntry::Error:
2343
0
      return error("Malformed block");
2344
159k
    case BitstreamEntry::EndBlock:
2345
159k
      if (NextCstNo != ValueList.size())
2346
0
        return error("Invalid constant reference");
2347
159k
2348
159k
      // Once all the constants have been read, go through and resolve forward
2349
159k
      // references.
2350
159k
      ValueList.resolveConstantForwardRefs();
2351
159k
      return Error::success();
2352
2.09M
    case BitstreamEntry::Record:
2353
2.09M
      // The interesting case.
2354
2.09M
      break;
2355
2.09M
    }
2356
2.09M
2357
2.09M
    // Read a record.
2358
2.09M
    Record.clear();
2359
2.09M
    Type *VoidType = Type::getVoidTy(Context);
2360
2.09M
    Value *V = nullptr;
2361
2.09M
    Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
2362
2.09M
    if (!MaybeBitCode)
2363
0
      return MaybeBitCode.takeError();
2364
2.09M
    switch (unsigned BitCode = MaybeBitCode.get()) {
2365
2.09M
    default:  // Default behavior: unknown constant
2366
6.45k
    case bitc::CST_CODE_UNDEF:     // UNDEF
2367
6.45k
      V = UndefValue::get(CurTy);
2368
6.45k
      break;
2369
539k
    case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
2370
539k
      if (Record.empty())
2371
0
        return error("Invalid record");
2372
539k
      if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2373
0
        return error("Invalid record");
2374
539k
      if (TypeList[Record[0]] == VoidType)
2375
1
        return error("Invalid constant type");
2376
539k
      CurFullTy = TypeList[Record[0]];
2377
539k
      CurTy = flattenPointerTypes(CurFullTy);
2378
539k
      continue;  // Skip the ValueList manipulation.
2379
539k
    case bitc::CST_CODE_NULL:      // NULL
2380
72.9k
      V = Constant::getNullValue(CurTy);
2381
72.9k
      break;
2382
753k
    case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
2383
753k
      if (!CurTy->isIntegerTy() || Record.empty())
2384
0
        return error("Invalid record");
2385
753k
      V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
2386
753k
      break;
2387
753k
    case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2388
25
      if (!CurTy->isIntegerTy() || Record.empty())
2389
0
        return error("Invalid record");
2390
25
2391
25
      APInt VInt =
2392
25
          readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2393
25
      V = ConstantInt::get(Context, VInt);
2394
25
2395
25
      break;
2396
25
    }
2397
18.6k
    case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
2398
18.6k
      if (Record.empty())
2399
0
        return error("Invalid record");
2400
18.6k
      if (CurTy->isHalfTy())
2401
14
        V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(),
2402
14
                                             APInt(16, (uint16_t)Record[0])));
2403
18.6k
      else if (CurTy->isFloatTy())
2404
5.77k
        V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(),
2405
5.77k
                                             APInt(32, (uint32_t)Record[0])));
2406
12.8k
      else if (CurTy->isDoubleTy())
2407
12.8k
        V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(),
2408
12.8k
                                             APInt(64, Record[0])));
2409
10
      else if (CurTy->isX86_FP80Ty()) {
2410
5
        // Bits are not stored the same way as a normal i80 APInt, compensate.
2411
5
        uint64_t Rearrange[2];
2412
5
        Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2413
5
        Rearrange[1] = Record[0] >> 48;
2414
5
        V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(),
2415
5
                                             APInt(80, Rearrange)));
2416
5
      } else if (CurTy->isFP128Ty())
2417
3
        V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(),
2418
3
                                             APInt(128, Record)));
2419
2
      else if (CurTy->isPPC_FP128Ty())
2420
2
        V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(),
2421
2
                                             APInt(128, Record)));
2422
0
      else
2423
0
        V = UndefValue::get(CurTy);
2424
18.6k
      break;
2425
18.6k
    }
2426
18.6k
2427
78.9k
    case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2428
78.9k
      if (Record.empty())
2429
0
        return error("Invalid record");
2430
78.9k
2431
78.9k
      unsigned Size = Record.size();
2432
78.9k
      SmallVector<Constant*, 16> Elts;
2433
78.9k
2434
78.9k
      if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2435
428k
        for (unsigned i = 0; i != Size; 
++i356k
)
2436
356k
          Elts.push_back(ValueList.getConstantFwdRef(Record[i],
2437
356k
                                                     STy->getElementType(i)));
2438
72.0k
        V = ConstantStruct::get(STy, Elts);
2439
72.0k
      } else 
if (ArrayType *6.92k
ATy6.92k
= dyn_cast<ArrayType>(CurTy)) {
2440
6.44k
        Type *EltTy = ATy->getElementType();
2441
58.1k
        for (unsigned i = 0; i != Size; 
++i51.7k
)
2442
51.7k
          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2443
6.44k
        V = ConstantArray::get(ATy, Elts);
2444
6.44k
      } else 
if (VectorType *483
VTy483
= dyn_cast<VectorType>(CurTy)) {
2445
483
        Type *EltTy = VTy->getElementType();
2446
4.55k
        for (unsigned i = 0; i != Size; 
++i4.07k
)
2447
4.07k
          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2448
483
        V = ConstantVector::get(Elts);
2449
483
      } else {
2450
0
        V = UndefValue::get(CurTy);
2451
0
      }
2452
78.9k
      break;
2453
78.9k
    }
2454
248k
    case bitc::CST_CODE_STRING:    // STRING: [values]
2455
248k
    case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2456
248k
      if (Record.empty())
2457
0
        return error("Invalid record");
2458
248k
2459
248k
      SmallString<16> Elts(Record.begin(), Record.end());
2460
248k
      V = ConstantDataArray::getString(Context, Elts,
2461
248k
                                       BitCode == bitc::CST_CODE_CSTRING);
2462
248k
      break;
2463
248k
    }
2464
248k
    case bitc::CST_CODE_DATA: {// DATA: [n x value]
2465
13.9k
      if (Record.empty())
2466
0
        return error("Invalid record");
2467
13.9k
2468
13.9k
      Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2469
13.9k
      if (EltTy->isIntegerTy(8)) {
2470
175
        SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2471
175
        if (isa<VectorType>(CurTy))
2472
175
          V = ConstantDataVector::get(Context, Elts);
2473
0
        else
2474
0
          V = ConstantDataArray::get(Context, Elts);
2475
13.7k
      } else if (EltTy->isIntegerTy(16)) {
2476
1.61k
        SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2477
1.61k
        if (isa<VectorType>(CurTy))
2478
212
          V = ConstantDataVector::get(Context, Elts);
2479
1.39k
        else
2480
1.39k
          V = ConstantDataArray::get(Context, Elts);
2481
12.1k
      } else if (EltTy->isIntegerTy(32)) {
2482
10.5k
        SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2483
10.5k
        if (isa<VectorType>(CurTy))
2484
10.5k
          V = ConstantDataVector::get(Context, Elts);
2485
90
        else
2486
90
          V = ConstantDataArray::get(Context, Elts);
2487
10.5k
      } else 
if (1.57k
EltTy->isIntegerTy(64)1.57k
) {
2488
1.46k
        SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2489
1.46k
        if (isa<VectorType>(CurTy))
2490
103
          V = ConstantDataVector::get(Context, Elts);
2491
1.36k
        else
2492
1.36k
          V = ConstantDataArray::get(Context, Elts);
2493
1.46k
      } else 
if (106
EltTy->isHalfTy()106
) {
2494
16
        SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2495
16
        if (isa<VectorType>(CurTy))
2496
8
          V = ConstantDataVector::getFP(Context, Elts);
2497
8
        else
2498
8
          V = ConstantDataArray::getFP(Context, Elts);
2499
90
      } else if (EltTy->isFloatTy()) {
2500
66
        SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2501
66
        if (isa<VectorType>(CurTy))
2502
54
          V = ConstantDataVector::getFP(Context, Elts);
2503
12
        else
2504
12
          V = ConstantDataArray::getFP(Context, Elts);
2505
66
      } else 
if (24
EltTy->isDoubleTy()24
) {
2506
24
        SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2507
24
        if (isa<VectorType>(CurTy))
2508
16
          V = ConstantDataVector::getFP(Context, Elts);
2509
8
        else
2510
8
          V = ConstantDataArray::getFP(Context, Elts);
2511
24
      } else {
2512
0
        return error("Invalid type for value");
2513
0
      }
2514
13.9k
      break;
2515
13.9k
    }
2516
13.9k
    case bitc::CST_CODE_CE_UNOP: {  // CE_UNOP: [opcode, opval]
2517
0
      if (Record.size() < 2)
2518
0
        return error("Invalid record");
2519
0
      int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
2520
0
      if (Opc < 0) {
2521
0
        V = UndefValue::get(CurTy);  // Unknown unop.
2522
0
      } else {
2523
0
        Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2524
0
        unsigned Flags = 0;
2525
0
        V = ConstantExpr::get(Opc, LHS, Flags);
2526
0
      }
2527
0
      break;
2528
0
    }
2529
139
    case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
2530
139
      if (Record.size() < 3)
2531
0
        return error("Invalid record");
2532
139
      int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
2533
139
      if (Opc < 0) {
2534
0
        V = UndefValue::get(CurTy);  // Unknown binop.
2535
139
      } else {
2536
139
        Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2537
139
        Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
2538
139
        unsigned Flags = 0;
2539
139
        if (Record.size() >= 4) {
2540
90
          if (Opc == Instruction::Add ||
2541
90
              
Opc == Instruction::Sub70
||
2542
90
              
Opc == Instruction::Mul45
||
2543
90
              
Opc == Instruction::Shl25
) {
2544
70
            if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2545
55
              Flags |= OverflowingBinaryOperator::NoSignedWrap;
2546
70
            if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2547
50
              Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2548
70
          } else 
if (20
Opc == Instruction::SDiv20
||
2549
20
                     
Opc == Instruction::UDiv15
||
2550
20
                     
Opc == Instruction::LShr10
||
2551
20
                     
Opc == Instruction::AShr5
) {
2552
20
            if (Record[3] & (1 << bitc::PEO_EXACT))
2553
20
              Flags |= SDivOperator::IsExact;
2554
20
          }
2555
90
        }
2556
139
        V = ConstantExpr::get(Opc, LHS, RHS, Flags);
2557
139
      }
2558
139
      break;
2559
139
    }
2560
21.2k
    case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
2561
21.2k
      if (Record.size() < 3)
2562
0
        return error("Invalid record");
2563
21.2k
      int Opc = getDecodedCastOpcode(Record[0]);
2564
21.2k
      if (Opc < 0) {
2565
0
        V = UndefValue::get(CurTy);  // Unknown cast.
2566
21.2k
      } else {
2567
21.2k
        Type *OpTy = getTypeByID(Record[1]);
2568
21.2k
        if (!OpTy)
2569
0
          return error("Invalid record");
2570
21.2k
        Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
2571
21.2k
        V = UpgradeBitCastExpr(Opc, Op, CurTy);
2572
21.2k
        if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
2573
21.2k
      }
2574
21.2k
      break;
2575
21.2k
    }
2576
342k
    case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
2577
342k
    case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
2578
342k
    case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
2579
342k
                                                     // operands]
2580
342k
      unsigned OpNum = 0;
2581
342k
      Type *PointeeType = nullptr;
2582
342k
      if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX ||
2583
342k
          
Record.size() % 2342k
)
2584
342k
        PointeeType = getTypeByID(Record[OpNum++]);
2585
342k
2586
342k
      bool InBounds = false;
2587
342k
      Optional<unsigned> InRangeIndex;
2588
342k
      if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) {
2589
39
        uint64_t Op = Record[OpNum++];
2590
39
        InBounds = Op & 1;
2591
39
        InRangeIndex = Op >> 1;
2592
342k
      } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
2593
342k
        InBounds = true;
2594
342k
2595
342k
      SmallVector<Constant*, 16> Elts;
2596
342k
      Type *Elt0FullTy = nullptr;
2597
1.37M
      while (OpNum != Record.size()) {
2598
1.02M
        if (!Elt0FullTy)
2599
342k
          Elt0FullTy = getFullyStructuredTypeByID(Record[OpNum]);
2600
1.02M
        Type *ElTy = getTypeByID(Record[OpNum++]);
2601
1.02M
        if (!ElTy)
2602
0
          return error("Invalid record");
2603
1.02M
        Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
2604
1.02M
      }
2605
342k
2606
342k
      if (Elts.size() < 1)
2607
1
        return error("Invalid gep with no operands");
2608
342k
2609
342k
      Type *ImplicitPointeeType =
2610
342k
          getPointerElementFlatType(Elt0FullTy->getScalarType());
2611
342k
      if (!PointeeType)
2612
8
        PointeeType = ImplicitPointeeType;
2613
342k
      else if (PointeeType != ImplicitPointeeType)
2614
1
        return error("Explicit gep operator type does not match pointee type "
2615
1
                     "of pointer operand");
2616
342k
2617
342k
      ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2618
342k
      V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2619
342k
                                         InBounds, InRangeIndex);
2620
342k
      break;
2621
342k
    }
2622
342k
    case bitc::CST_CODE_CE_SELECT: {  // CE_SELECT: [opval#, opval#, opval#]
2623
6
      if (Record.size() < 3)
2624
0
        return error("Invalid record");
2625
6
2626
6
      Type *SelectorTy = Type::getInt1Ty(Context);
2627
6
2628
6
      // The selector might be an i1 or an <n x i1>
2629
6
      // Get the type from the ValueList before getting a forward ref.
2630
6
      if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
2631
5
        if (Value *V = ValueList[Record[0]])
2632
5
          if (SelectorTy != V->getType())
2633
0
            SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
2634
6
2635
6
      V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2636
6
                                                              SelectorTy),
2637
6
                                  ValueList.getConstantFwdRef(Record[1],CurTy),
2638
6
                                  ValueList.getConstantFwdRef(Record[2],CurTy));
2639
6
      break;
2640
6
    }
2641
15
    case bitc::CST_CODE_CE_EXTRACTELT
2642
15
        : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2643
15
      if (Record.size() < 3)
2644
0
        return error("Invalid record");
2645
15
      VectorType *OpTy =
2646
15
        dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2647
15
      if (!OpTy)
2648
0
        return error("Invalid record");
2649
15
      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2650
15
      Constant *Op1 = nullptr;
2651
15
      if (Record.size() == 4) {
2652
15
        Type *IdxTy = getTypeByID(Record[2]);
2653
15
        if (!IdxTy)
2654
0
          return error("Invalid record");
2655
15
        Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2656
15
      } else // TODO: Remove with llvm 4.0
2657
0
        Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2658
15
      if (!Op1)
2659
0
        return error("Invalid record");
2660
15
      V = ConstantExpr::getExtractElement(Op0, Op1);
2661
15
      break;
2662
15
    }
2663
15
    case bitc::CST_CODE_CE_INSERTELT
2664
0
        : { // CE_INSERTELT: [opval, opval, opty, opval]
2665
0
      VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2666
0
      if (Record.size() < 3 || !OpTy)
2667
0
        return error("Invalid record");
2668
0
      Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2669
0
      Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2670
0
                                                  OpTy->getElementType());
2671
0
      Constant *Op2 = nullptr;
2672
0
      if (Record.size() == 4) {
2673
0
        Type *IdxTy = getTypeByID(Record[2]);
2674
0
        if (!IdxTy)
2675
0
          return error("Invalid record");
2676
0
        Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2677
0
      } else // TODO: Remove with llvm 4.0
2678
0
        Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2679
0
      if (!Op2)
2680
0
        return error("Invalid record");
2681
0
      V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2682
0
      break;
2683
0
    }
2684
0
    case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2685
0
      VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2686
0
      if (Record.size() < 3 || !OpTy)
2687
0
        return error("Invalid record");
2688
0
      Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2689
0
      Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
2690
0
      Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2691
0
                                                 OpTy->getNumElements());
2692
0
      Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
2693
0
      V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2694
0
      break;
2695
0
    }
2696
0
    case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2697
0
      VectorType *RTy = dyn_cast<VectorType>(CurTy);
2698
0
      VectorType *OpTy =
2699
0
        dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2700
0
      if (Record.size() < 4 || !RTy || !OpTy)
2701
0
        return error("Invalid record");
2702
0
      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2703
0
      Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2704
0
      Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2705
0
                                                 RTy->getNumElements());
2706
0
      Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
2707
0
      V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2708
0
      break;
2709
0
    }
2710
52
    case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
2711
52
      if (Record.size() < 4)
2712
0
        return error("Invalid record");
2713
52
      Type *OpTy = getTypeByID(Record[0]);
2714
52
      if (!OpTy)
2715
0
        return error("Invalid record");
2716
52
      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2717
52
      Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2718
52
2719
52
      if (OpTy->isFPOrFPVectorTy())
2720
0
        V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2721
52
      else
2722
52
        V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2723
52
      break;
2724
52
    }
2725
52
    // This maintains backward compatibility, pre-asm dialect keywords.
2726
52
    // FIXME: Remove with the 4.0 release.
2727
52
    case bitc::CST_CODE_INLINEASM_OLD: {
2728
0
      if (Record.size() < 2)
2729
0
        return error("Invalid record");
2730
0
      std::string AsmStr, ConstrStr;
2731
0
      bool HasSideEffects = Record[0] & 1;
2732
0
      bool IsAlignStack = Record[0] >> 1;
2733
0
      unsigned AsmStrSize = Record[1];
2734
0
      if (2+AsmStrSize >= Record.size())
2735
0
        return error("Invalid record");
2736
0
      unsigned ConstStrSize = Record[2+AsmStrSize];
2737
0
      if (3+AsmStrSize+ConstStrSize > Record.size())
2738
0
        return error("Invalid record");
2739
0
2740
0
      for (unsigned i = 0; i != AsmStrSize; ++i)
2741
0
        AsmStr += (char)Record[2+i];
2742
0
      for (unsigned i = 0; i != ConstStrSize; ++i)
2743
0
        ConstrStr += (char)Record[3+AsmStrSize+i];
2744
0
      UpgradeInlineAsmString(&AsmStr);
2745
0
      V = InlineAsm::get(
2746
0
          cast<FunctionType>(getPointerElementFlatType(CurFullTy)), AsmStr,
2747
0
          ConstrStr, HasSideEffects, IsAlignStack);
2748
0
      break;
2749
0
    }
2750
0
    // This version adds support for the asm dialect keywords (e.g.,
2751
0
    // inteldialect).
2752
75
    case bitc::CST_CODE_INLINEASM: {
2753
75
      if (Record.size() < 2)
2754
0
        return error("Invalid record");
2755
75
      std::string AsmStr, ConstrStr;
2756
75
      bool HasSideEffects = Record[0] & 1;
2757
75
      bool IsAlignStack = (Record[0] >> 1) & 1;
2758
75
      unsigned AsmDialect = Record[0] >> 2;
2759
75
      unsigned AsmStrSize = Record[1];
2760
75
      if (2+AsmStrSize >= Record.size())
2761
0
        return error("Invalid record");
2762
75
      unsigned ConstStrSize = Record[2+AsmStrSize];
2763
75
      if (3+AsmStrSize+ConstStrSize > Record.size())
2764
0
        return error("Invalid record");
2765
75
2766
805
      
for (unsigned i = 0; 75
i != AsmStrSize;
++i730
)
2767
730
        AsmStr += (char)Record[2+i];
2768
1.44k
      for (unsigned i = 0; i != ConstStrSize; 
++i1.36k
)
2769
1.36k
        ConstrStr += (char)Record[3+AsmStrSize+i];
2770
75
      UpgradeInlineAsmString(&AsmStr);
2771
75
      V = InlineAsm::get(
2772
75
          cast<FunctionType>(getPointerElementFlatType(CurFullTy)), AsmStr,
2773
75
          ConstrStr, HasSideEffects, IsAlignStack,
2774
75
          InlineAsm::AsmDialect(AsmDialect));
2775
75
      break;
2776
75
    }
2777
83
    case bitc::CST_CODE_BLOCKADDRESS:{
2778
83
      if (Record.size() < 3)
2779
0
        return error("Invalid record");
2780
83
      Type *FnTy = getTypeByID(Record[0]);
2781
83
      if (!FnTy)
2782
0
        return error("Invalid record");
2783
83
      Function *Fn =
2784
83
        dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
2785
83
      if (!Fn)
2786
0
        return error("Invalid record");
2787
83
2788
83
      // If the function is already parsed we can insert the block address right
2789
83
      // away.
2790
83
      BasicBlock *BB;
2791
83
      unsigned BBID = Record[2];
2792
83
      if (!BBID)
2793
0
        // Invalid reference to entry block.
2794
0
        return error("Invalid ID");
2795
83
      if (!Fn->empty()) {
2796
33
        Function::iterator BBI = Fn->begin(), BBE = Fn->end();
2797
131
        for (size_t I = 0, E = BBID; I != E; 
++I98
) {
2798
98
          if (BBI == BBE)
2799
0
            return error("Invalid ID");
2800
98
          ++BBI;
2801
98
        }
2802
33
        BB = &*BBI;
2803
50
      } else {
2804
50
        // Otherwise insert a placeholder and remember it so it can be inserted
2805
50
        // when the function is parsed.
2806
50
        auto &FwdBBs = BasicBlockFwdRefs[Fn];
2807
50
        if (FwdBBs.empty())
2808
45
          BasicBlockFwdRefQueue.push_back(Fn);
2809
50
        if (FwdBBs.size() < BBID + 1)
2810
45
          FwdBBs.resize(BBID + 1);
2811
50
        if (!FwdBBs[BBID])
2812
45
          FwdBBs[BBID] = BasicBlock::Create(Context);
2813
50
        BB = FwdBBs[BBID];
2814
50
      }
2815
83
      V = BlockAddress::get(Fn, BB);
2816
83
      break;
2817
1.55M
    }
2818
1.55M
    }
2819
1.55M
2820
1.55M
    assert(V->getType() == flattenPointerTypes(CurFullTy) &&
2821
1.55M
           "Incorrect fully structured type provided for Constant");
2822
1.55M
    ValueList.assignValue(V, NextCstNo, CurFullTy);
2823
1.55M
    ++NextCstNo;
2824
1.55M
  }
2825
159k
}
2826
2827
625
Error BitcodeReader::parseUseLists() {
2828
625
  if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
2829
0
    return Err;
2830
625
2831
625
  // Read all the records.
2832
625
  SmallVector<uint64_t, 64> Record;
2833
625
2834
1.89k
  while (true) {
2835
1.89k
    Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks();
2836
1.89k
    if (!MaybeEntry)
2837
0
      return MaybeEntry.takeError();
2838
1.89k
    BitstreamEntry Entry = MaybeEntry.get();
2839
1.89k
2840
1.89k
    switch (Entry.Kind) {
2841
1.89k
    case BitstreamEntry::SubBlock: // Handled for us already.
2842
0
    case BitstreamEntry::Error:
2843
0
      return error("Malformed block");
2844
625
    case BitstreamEntry::EndBlock:
2845
625
      return Error::success();
2846
1.27k
    case BitstreamEntry::Record:
2847
1.27k
      // The interesting case.
2848
1.27k
      break;
2849
1.27k
    }
2850
1.27k
2851
1.27k
    // Read a use list record.
2852
1.27k
    Record.clear();
2853
1.27k
    bool IsBB = false;
2854
1.27k
    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2855
1.27k
    if (!MaybeRecord)
2856
0
      return MaybeRecord.takeError();
2857
1.27k
    switch (MaybeRecord.get()) {
2858
1.27k
    default:  // Default behavior: unknown type.
2859
0
      break;
2860
1.27k
    case bitc::USELIST_CODE_BB:
2861
66
      IsBB = true;
2862
66
      LLVM_FALLTHROUGH;
2863
1.27k
    case bitc::USELIST_CODE_DEFAULT: {
2864
1.27k
      unsigned RecordLength = Record.size();
2865
1.27k
      if (RecordLength < 3)
2866
0
        // Records should have at least an ID and two indexes.
2867
0
        return error("Invalid record");
2868
1.27k
      unsigned ID = Record.back();
2869
1.27k
      Record.pop_back();
2870
1.27k
2871
1.27k
      Value *V;
2872
1.27k
      if (IsBB) {
2873
66
        assert(ID < FunctionBBs.size() && "Basic block not found");
2874
66
        V = FunctionBBs[ID];
2875
66
      } else
2876
1.20k
        V = ValueList[ID];
2877
1.27k
      unsigned NumUses = 0;
2878
1.27k
      SmallDenseMap<const Use *, unsigned, 16> Order;
2879
9.15k
      for (const Use &U : V->materialized_uses()) {
2880
9.15k
        if (++NumUses > Record.size())
2881
3
          break;
2882
9.15k
        Order[&U] = Record[NumUses - 1];
2883
9.15k
      }
2884
1.27k
      if (Order.size() != Record.size() || 
NumUses > Record.size()1.26k
)
2885
7
        // Mismatches can happen if the functions are being materialized lazily
2886
7
        // (out-of-order), or a value has been upgraded.
2887
7
        break;
2888
1.26k
2889
35.0k
      
V->sortUseList([&](const Use &L, const Use &R) 1.26k
{
2890
35.0k
        return Order.lookup(&L) < Order.lookup(&R);
2891
35.0k
      });
2892
1.26k
      break;
2893
1.26k
    }
2894
1.27k
    }
2895
1.27k
  }
2896
625
}
2897
2898
/// When we see the block for metadata, remember where it is and then skip it.
2899
/// This lets us lazily deserialize the metadata.
2900
446
Error BitcodeReader::rememberAndSkipMetadata() {
2901
446
  // Save the current stream state.
2902
446
  uint64_t CurBit = Stream.GetCurrentBitNo();
2903
446
  DeferredMetadataInfo.push_back(CurBit);
2904
446
2905
446
  // Skip over the block for now.
2906
446
  if (Error Err = Stream.SkipBlock())
2907
0
    return Err;
2908
446
  return Error::success();
2909
446
}
2910
2911
346k
Error BitcodeReader::materializeMetadata() {
2912
346k
  for (uint64_t BitPos : DeferredMetadataInfo) {
2913
429
    // Move the bit stream to the saved position.
2914
429
    if (Error JumpFailed = Stream.JumpToBit(BitPos))
2915
0
      return JumpFailed;
2916
429
    if (Error Err = MDLoader->parseModuleMetadata())
2917
0
      return Err;
2918
429
  }
2919
346k
2920
346k
  // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2921
346k
  // metadata.
2922
346k
  if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
2923
1
    NamedMDNode *LinkerOpts =
2924
1
        TheModule->getOrInsertNamedMetadata("llvm.linker.options");
2925
1
    for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
2926
2
      LinkerOpts->addOperand(cast<MDNode>(MDOptions));
2927
1
  }
2928
346k
2929
346k
  DeferredMetadataInfo.clear();
2930
346k
  return Error::success();
2931
346k
}
2932
2933
618
void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
2934
2935
/// When we see the block for a function body, remember where it is and then
2936
/// skip it.  This lets us lazily deserialize the functions.
2937
7.33k
Error BitcodeReader::rememberAndSkipFunctionBody() {
2938
7.33k
  // Get the function we are talking about.
2939
7.33k
  if (FunctionsWithBodies.empty())
2940
0
    return error("Insufficient function protos");
2941
7.33k
2942
7.33k
  Function *Fn = FunctionsWithBodies.back();
2943
7.33k
  FunctionsWithBodies.pop_back();
2944
7.33k
2945
7.33k
  // Save the current stream state.
2946
7.33k
  uint64_t CurBit = Stream.GetCurrentBitNo();
2947
7.33k
  assert(
2948
7.33k
      (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
2949
7.33k
      "Mismatch between VST and scanned function offsets");
2950
7.33k
  DeferredFunctionInfo[Fn] = CurBit;
2951
7.33k
2952
7.33k
  // Skip over the function block for now.
2953
7.33k
  if (Error Err = Stream.SkipBlock())
2954
0
    return Err;
2955
7.33k
  return Error::success();
2956
7.33k
}
2957
2958
21.1k
Error BitcodeReader::globalCleanup() {
2959
21.1k
  // Patch the initializers for globals and aliases up.
2960
21.1k
  if (Error Err = resolveGlobalAndIndirectSymbolInits())
2961
1
    return Err;
2962
21.1k
  
if (21.1k
!GlobalInits.empty()21.1k
|| !IndirectSymbolInits.empty())
2963
0
    return error("Malformed global initializer set");
2964
21.1k
2965
21.1k
  // Look for intrinsic functions which need to be upgraded at some point
2966
3.41M
  
for (Function &F : *TheModule)21.1k
{
2967
3.41M
    MDLoader->upgradeDebugIntrinsics(F);
2968
3.41M
    Function *NewFn;
2969
3.41M
    if (UpgradeIntrinsicFunction(&F, NewFn))
2970
11.2k
      UpgradedIntrinsics[&F] = NewFn;
2971
3.40M
    else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
2972
0
      // Some types could be renamed during loading if several modules are
2973
0
      // loaded in the same LLVMContext (LTO scenario). In this case we should
2974
0
      // remangle intrinsics names as well.
2975
0
      RemangledIntrinsics[&F] = Remangled.getValue();
2976
3.41M
  }
2977
21.1k
2978
21.1k
  // Look for global variables which need to be renamed.
2979
21.1k
  std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables;
2980
21.1k
  for (GlobalVariable &GV : TheModule->globals())
2981
960k
    if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV))
2982
4
      UpgradedVariables.emplace_back(&GV, Upgraded);
2983
21.1k
  for (auto &Pair : UpgradedVariables) {
2984
4
    Pair.first->eraseFromParent();
2985
4
    TheModule->getGlobalList().push_back(Pair.second);
2986
4
  }
2987
21.1k
2988
21.1k
  // Force deallocation of memory for these vectors to favor the client that
2989
21.1k
  // want lazy deserialization.
2990
21.1k
  std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
2991
21.1k
  std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>().swap(
2992
21.1k
      IndirectSymbolInits);
2993
21.1k
  return Error::success();
2994
21.1k
}
2995
2996
/// Support for lazy parsing of function bodies. This is required if we
2997
/// either have an old bitcode file without a VST forward declaration record,
2998
/// or if we have an anonymous function being materialized, since anonymous
2999
/// functions do not have a name and are therefore not in the VST.
3000
332
Error BitcodeReader::rememberAndSkipFunctionBodies() {
3001
332
  if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit))
3002
0
    return JumpFailed;
3003
332
3004
332
  if (Stream.AtEndOfStream())
3005
0
    return error("Could not find function in stream");
3006
332
3007
332
  if (!SeenFirstFunctionBody)
3008
1
    return error("Trying to materialize functions before seeing function blocks");
3009
331
3010
331
  // An old bitcode file with the symbol table at the end would have
3011
331
  // finished the parse greedily.
3012
331
  assert(SeenValueSymbolTable);
3013
331
3014
331
  SmallVector<uint64_t, 64> Record;
3015
331
3016
331
  while (true) {
3017
331
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3018
331
    if (!MaybeEntry)
3019
0
      return MaybeEntry.takeError();
3020
331
    llvm::BitstreamEntry Entry = MaybeEntry.get();
3021
331
3022
331
    switch (Entry.Kind) {
3023
331
    default:
3024
0
      return error("Expect SubBlock");
3025
331
    case BitstreamEntry::SubBlock:
3026
331
      switch (Entry.ID) {
3027
331
      default:
3028
0
        return error("Expect function block");
3029
331
      case bitc::FUNCTION_BLOCK_ID:
3030
331
        if (Error Err = rememberAndSkipFunctionBody())
3031
0
          return Err;
3032
331
        NextUnreadBit = Stream.GetCurrentBitNo();
3033
331
        return Error::success();
3034
331
      }
3035
331
    }
3036
331
  }
3037
331
}
3038
3039
8.37k
bool BitcodeReaderBase::readBlockInfo() {
3040
8.37k
  Expected<Optional<BitstreamBlockInfo>> MaybeNewBlockInfo =
3041
8.37k
      Stream.ReadBlockInfoBlock();
3042
8.37k
  if (!MaybeNewBlockInfo)
3043
0
    return true; // FIXME Handle the error.
3044
8.37k
  Optional<BitstreamBlockInfo> NewBlockInfo =
3045
8.37k
      std::move(MaybeNewBlockInfo.get());
3046
8.37k
  if (!NewBlockInfo)
3047
0
    return true;
3048
8.37k
  BlockInfo = std::move(*NewBlockInfo);
3049
8.37k
  return false;
3050
8.37k
}
3051
3052
3.78k
Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
3053
3.78k
  // v1: [selection_kind, name]
3054
3.78k
  // v2: [strtab_offset, strtab_size, selection_kind]
3055
3.78k
  StringRef Name;
3056
3.78k
  std::tie(Name, Record) = readNameFromStrtab(Record);
3057
3.78k
3058
3.78k
  if (Record.empty())
3059
0
    return error("Invalid record");
3060
3.78k
  Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3061
3.78k
  std::string OldFormatName;
3062
3.78k
  if (!UseStrtab) {
3063
1.42k
    if (Record.size() < 2)
3064
0
      return error("Invalid record");
3065
1.42k
    unsigned ComdatNameSize = Record[1];
3066
1.42k
    OldFormatName.reserve(ComdatNameSize);
3067
22.7k
    for (unsigned i = 0; i != ComdatNameSize; 
++i21.3k
)
3068
21.3k
      OldFormatName += (char)Record[2 + i];
3069
1.42k
    Name = OldFormatName;
3070
1.42k
  }
3071
3.78k
  Comdat *C = TheModule->getOrInsertComdat(Name);
3072
3.78k
  C->setSelectionKind(SK);
3073
3.78k
  ComdatList.push_back(C);
3074
3.78k
  return Error::success();
3075
3.78k
}
3076
3077
1.45M
static void inferDSOLocal(GlobalValue *GV) {
3078
1.45M
  // infer dso_local from linkage and visibility if it is not encoded.
3079
1.45M
  if (GV->hasLocalLinkage() ||
3080
1.45M
      
(1.17M
!GV->hasDefaultVisibility()1.17M
&&
!GV->hasExternalWeakLinkage()933
))
3081
279k
    GV->setDSOLocal(true);
3082
1.45M
}
3083
3084
320k
Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
3085
320k
  // v1: [pointer type, isconst, initid, linkage, alignment, section,
3086
320k
  // visibility, threadlocal, unnamed_addr, externally_initialized,
3087
320k
  // dllstorageclass, comdat, attributes, preemption specifier,
3088
320k
  // partition strtab offset, partition strtab size] (name in VST)
3089
320k
  // v2: [strtab_offset, strtab_size, v1]
3090
320k
  StringRef Name;
3091
320k
  std::tie(Name, Record) = readNameFromStrtab(Record);
3092
320k
3093
320k
  if (Record.size() < 6)
3094
0
    return error("Invalid record");
3095
320k
  Type *FullTy = getFullyStructuredTypeByID(Record[0]);
3096
320k
  Type *Ty = flattenPointerTypes(FullTy);
3097
320k
  if (!Ty)
3098
0
    return error("Invalid record");
3099
320k
  bool isConstant = Record[1] & 1;
3100
320k
  bool explicitType = Record[1] & 2;
3101
320k
  unsigned AddressSpace;
3102
320k
  if (explicitType) {
3103
320k
    AddressSpace = Record[1] >> 2;
3104
320k
  } else {
3105
178
    if (!Ty->isPointerTy())
3106
0
      return error("Invalid type for value");
3107
178
    AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3108
178
    std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
3109
178
  }
3110
320k
3111
320k
  uint64_t RawLinkage = Record[3];
3112
320k
  GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
3113
320k
  unsigned Alignment;
3114
320k
  if (Error Err = parseAlignmentValue(Record[4], Alignment))
3115
0
    return Err;
3116
320k
  std::string Section;
3117
320k
  if (Record[5]) {
3118
2.00k
    if (Record[5] - 1 >= SectionTable.size())
3119
0
      return error("Invalid ID");
3120
2.00k
    Section = SectionTable[Record[5] - 1];
3121
2.00k
  }
3122
320k
  GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
3123
320k
  // Local linkage must have default visibility.
3124
320k
  if (Record.size() > 6 && 
!GlobalValue::isLocalLinkage(Linkage)229k
)
3125
28.7k
    // FIXME: Change to an error if non-default in 4.0.
3126
28.7k
    Visibility = getDecodedVisibility(Record[6]);
3127
320k
3128
320k
  GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3129
320k
  if (Record.size() > 7)
3130
229k
    TLM = getDecodedThreadLocalMode(Record[7]);
3131
320k
3132
320k
  GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3133
320k
  if (Record.size() > 8)
3134
229k
    UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
3135
320k
3136
320k
  bool ExternallyInitialized = false;
3137
320k
  if (Record.size() > 9)
3138
229k
    ExternallyInitialized = Record[9];
3139
320k
3140
320k
  GlobalVariable *NewGV =
3141
320k
      new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
3142
320k
                         nullptr, TLM, AddressSpace, ExternallyInitialized);
3143
320k
  NewGV->setAlignment(Alignment);
3144
320k
  if (!Section.empty())
3145
2.00k
    NewGV->setSection(Section);
3146
320k
  NewGV->setVisibility(Visibility);
3147
320k
  NewGV->setUnnamedAddr(UnnamedAddr);
3148
320k
3149
320k
  if (Record.size() > 10)
3150
229k
    NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
3151
91.4k
  else
3152
91.4k
    upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3153
320k
3154
320k
  FullTy = PointerType::get(FullTy, AddressSpace);
3155
320k
  assert(NewGV->getType() == flattenPointerTypes(FullTy) &&
3156
320k
         "Incorrect fully specified type for GlobalVariable");
3157
320k
  ValueList.push_back(NewGV, FullTy);
3158
320k
3159
320k
  // Remember which value to use for the global initializer.
3160
320k
  if (unsigned InitID = Record[2])
3161
318k
    GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
3162
320k
3163
320k
  if (Record.size() > 11) {
3164
229k
    if (unsigned ComdatID = Record[11]) {
3165
799
      if (ComdatID > ComdatList.size())
3166
1
        return error("Invalid global variable comdat ID");
3167
798
      NewGV->setComdat(ComdatList[ComdatID - 1]);
3168
798
    }
3169
229k
  } else 
if (91.4k
hasImplicitComdat(RawLinkage)91.4k
) {
3170
11
    NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3171
11
  }
3172
320k
3173
320k
  
if (320k
Record.size() > 12320k
) {
3174
7.05k
    auto AS = getAttributes(Record[12]).getFnAttributes();
3175
7.05k
    NewGV->setAttributes(AS);
3176
7.05k
  }
3177
320k
3178
320k
  if (Record.size() > 13) {
3179
7.03k
    NewGV->setDSOLocal(getDecodedDSOLocal(Record[13]));
3180
7.03k
  }
3181
320k
  inferDSOLocal(NewGV);
3182
320k
3183
320k
  // Check whether we have enough values to read a partition name.
3184
320k
  if (Record.size() > 15)
3185
7.00k
    NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15]));
3186
320k
3187
320k
  return Error::success();
3188
320k
}
3189
3190
1.13M
Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
3191
1.13M
  // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
3192
1.13M
  // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
3193
1.13M
  // prefixdata,  personalityfn, preemption specifier, addrspace] (name in VST)
3194
1.13M
  // v2: [strtab_offset, strtab_size, v1]
3195
1.13M
  StringRef Name;
3196
1.13M
  std::tie(Name, Record) = readNameFromStrtab(Record);
3197
1.13M
3198
1.13M
  if (Record.size() < 8)
3199
0
    return error("Invalid record");
3200
1.13M
  Type *FullFTy = getFullyStructuredTypeByID(Record[0]);
3201
1.13M
  Type *FTy = flattenPointerTypes(FullFTy);
3202
1.13M
  if (!FTy)
3203
0
    return error("Invalid record");
3204
1.13M
  if (isa<PointerType>(FTy))
3205
656
    std::tie(FullFTy, FTy) = getPointerElementTypes(FullFTy);
3206
1.13M
3207
1.13M
  if (!isa<FunctionType>(FTy))
3208
0
    return error("Invalid type for value");
3209
1.13M
  auto CC = static_cast<CallingConv::ID>(Record[1]);
3210
1.13M
  if (CC & ~CallingConv::MaxID)
3211
0
    return error("Invalid calling convention ID");
3212
1.13M
3213
1.13M
  unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
3214
1.13M
  if (Record.size() > 16)
3215
19.3k
    AddrSpace = Record[16];
3216
1.13M
3217
1.13M
  Function *Func =
3218
1.13M
      Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage,
3219
1.13M
                       AddrSpace, Name, TheModule);
3220
1.13M
3221
1.13M
  assert(Func->getFunctionType() == flattenPointerTypes(FullFTy) &&
3222
1.13M
         "Incorrect fully specified type provided for function");
3223
1.13M
  FunctionTypes[Func] = cast<FunctionType>(FullFTy);
3224
1.13M
3225
1.13M
  Func->setCallingConv(CC);
3226
1.13M
  bool isProto = Record[2];
3227
1.13M
  uint64_t RawLinkage = Record[3];
3228
1.13M
  Func->setLinkage(getDecodedLinkage(RawLinkage));
3229
1.13M
  Func->setAttributes(getAttributes(Record[4]));
3230
1.13M
3231
1.13M
  // Upgrade any old-style byval without a type by propagating the argument's
3232
1.13M
  // pointee type. There should be no opaque pointers where the byval type is
3233
1.13M
  // implicit.
3234
3.52M
  for (unsigned i = 0; i != Func->arg_size(); 
++i2.39M
) {
3235
2.39M
    if (!Func->hasParamAttribute(i, Attribute::ByVal))
3236
2.39M
      continue;
3237
38
3238
38
    Type *PTy = cast<FunctionType>(FullFTy)->getParamType(i);
3239
38
    Func->removeParamAttr(i, Attribute::ByVal);
3240
38
    Func->addParamAttr(i, Attribute::getWithByValType(
3241
38
                              Context, getPointerElementFlatType(PTy)));
3242
38
  }
3243
1.13M
3244
1.13M
  unsigned Alignment;
3245
1.13M
  if (Error Err = parseAlignmentValue(Record[5], Alignment))
3246
0
    return Err;
3247
1.13M
  Func->setAlignment(Alignment);
3248
1.13M
  if (Record[6]) {
3249
46
    if (Record[6] - 1 >= SectionTable.size())
3250
0
      return error("Invalid ID");
3251
46
    Func->setSection(SectionTable[Record[6] - 1]);
3252
46
  }
3253
1.13M
  // Local linkage must have default visibility.
3254
1.13M
  if (!Func->hasLocalLinkage())
3255
1.12M
    // FIXME: Change to an error if non-default in 4.0.
3256
1.12M
    Func->setVisibility(getDecodedVisibility(Record[7]));
3257
1.13M
  if (
Record.size() > 81.13M
&& Record[8]) {
3258
32
    if (Record[8] - 1 >= GCTable.size())
3259
1
      return error("Invalid ID");
3260
31
    Func->setGC(GCTable[Record[8] - 1]);
3261
31
  }
3262
1.13M
  GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3263
1.13M
  if (Record.size() > 9)
3264
1.13M
    UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
3265
1.13M
  Func->setUnnamedAddr(UnnamedAddr);
3266
1.13M
  if (Record.size() > 10 && 
Record[10] != 01.13M
)
3267
25
    FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1));
3268
1.13M
3269
1.13M
  if (Record.size() > 11)
3270
1.13M
    Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3271
419
  else
3272
419
    upgradeDLLImportExportLinkage(Func, RawLinkage);
3273
1.13M
3274
1.13M
  if (Record.size() > 12) {
3275
1.13M
    if (unsigned ComdatID = Record[12]) {
3276
3.97k
      if (ComdatID > ComdatList.size())
3277
0
        return error("Invalid function comdat ID");
3278
3.97k
      Func->setComdat(ComdatList[ComdatID - 1]);
3279
3.97k
    }
3280
1.13M
  } else 
if (432
hasImplicitComdat(RawLinkage)432
) {
3281
6
    Func->setComdat(reinterpret_cast<Comdat *>(1));
3282
6
  }
3283
1.13M
3284
1.13M
  if (Record.size() > 13 && 
Record[13] != 01.13M
)
3285
31
    FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1));
3286
1.13M
3287
1.13M
  if (Record.size() > 14 && 
Record[14] != 01.13M
)
3288
153
    FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3289
1.13M
3290
1.13M
  if (Record.size() > 15) {
3291
19.8k
    Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
3292
19.8k
  }
3293
1.13M
  inferDSOLocal(Func);
3294
1.13M
3295
1.13M
  // Record[16] is the address space number.
3296
1.13M
3297
1.13M
  // Check whether we have enough values to read a partition name.
3298
1.13M
  if (Record.size() > 18)
3299
19.2k
    Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
3300
1.13M
3301
1.13M
  Type *FullTy = PointerType::get(FullFTy, AddrSpace);
3302
1.13M
  assert(Func->getType() == flattenPointerTypes(FullTy) &&
3303
1.13M
         "Incorrect fully specified type provided for Function");
3304
1.13M
  ValueList.push_back(Func, FullTy);
3305
1.13M
3306
1.13M
  // If this is a function with a body, remember the prototype we are
3307
1.13M
  // creating now, so that we can match up the body with them later.
3308
1.13M
  if (!isProto) {
3309
338k
    Func->setIsMaterializable(true);
3310
338k
    FunctionsWithBodies.push_back(Func);
3311
338k
    DeferredFunctionInfo[Func] = 0;
3312
338k
  }
3313
1.13M
  return Error::success();
3314
1.13M
}
3315
3316
Error BitcodeReader::parseGlobalIndirectSymbolRecord(
3317
913
    unsigned BitCode, ArrayRef<uint64_t> Record) {
3318
913
  // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3319
913
  // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3320
913
  // dllstorageclass, threadlocal, unnamed_addr,
3321
913
  // preemption specifier] (name in VST)
3322
913
  // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3323
913
  // visibility, dllstorageclass, threadlocal, unnamed_addr,
3324
913
  // preemption specifier] (name in VST)
3325
913
  // v2: [strtab_offset, strtab_size, v1]
3326
913
  StringRef Name;
3327
913
  std::tie(Name, Record) = readNameFromStrtab(Record);
3328
913
3329
913
  bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
3330
913
  if (Record.size() < (3 + (unsigned)NewRecord))
3331
0
    return error("Invalid record");
3332
913
  unsigned OpNum = 0;
3333
913
  Type *FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
3334
913
  Type *Ty = flattenPointerTypes(FullTy);
3335
913
  if (!Ty)
3336
0
    return error("Invalid record");
3337
913
3338
913
  unsigned AddrSpace;
3339
913
  if (!NewRecord) {
3340
64
    auto *PTy = dyn_cast<PointerType>(Ty);
3341
64
    if (!PTy)
3342
0
      return error("Invalid type for value");
3343
64
    std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
3344
64
    AddrSpace = PTy->getAddressSpace();
3345
849
  } else {
3346
849
    AddrSpace = Record[OpNum++];
3347
849
  }
3348
913
3349
913
  auto Val = Record[OpNum++];
3350
913
  auto Linkage = Record[OpNum++];
3351
913
  GlobalIndirectSymbol *NewGA;
3352
913
  if (BitCode == bitc::MODULE_CODE_ALIAS ||
3353
913
      
BitCode == bitc::MODULE_CODE_ALIAS_OLD128
)
3354
848
    NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3355
848
                                TheModule);
3356
65
  else
3357
65
    NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3358
65
                                nullptr, TheModule);
3359
913
3360
913
  assert(NewGA->getValueType() == flattenPointerTypes(FullTy) &&
3361
913
         "Incorrect fully structured type provided for GlobalIndirectSymbol");
3362
913
  // Old bitcode files didn't have visibility field.
3363
913
  // Local linkage must have default visibility.
3364
913
  if (OpNum != Record.size()) {
3365
913
    auto VisInd = OpNum++;
3366
913
    if (!NewGA->hasLocalLinkage())
3367
821
      // FIXME: Change to an error if non-default in 4.0.
3368
821
      NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3369
913
  }
3370
913
  if (BitCode == bitc::MODULE_CODE_ALIAS ||
3371
913
      
BitCode == bitc::MODULE_CODE_ALIAS_OLD128
) {
3372
849
    if (OpNum != Record.size())
3373
832
      NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
3374
17
    else
3375
17
      upgradeDLLImportExportLinkage(NewGA, Linkage);
3376
849
    if (OpNum != Record.size())
3377
824
      NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3378
849
    if (OpNum != Record.size())
3379
823
      NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++]));
3380
849
  }
3381
913
  if (OpNum != Record.size())
3382
740
    NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
3383
913
  inferDSOLocal(NewGA);
3384
913
3385
913
  // Check whether we have enough values to read a partition name.
3386
913
  if (OpNum + 1 < Record.size()) {
3387
702
    NewGA->setPartition(
3388
702
        StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1]));
3389
702
    OpNum += 2;
3390
702
  }
3391
913
3392
913
  FullTy = PointerType::get(FullTy, AddrSpace);
3393
913
  assert(NewGA->getType() == flattenPointerTypes(FullTy) &&
3394
913
         "Incorrect fully structured type provided for GlobalIndirectSymbol");
3395
913
  ValueList.push_back(NewGA, FullTy);
3396
913
  IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
3397
913
  return Error::success();
3398
913
}
3399
3400
Error BitcodeReader::parseModule(uint64_t ResumeBit,
3401
14.1k
                                 bool ShouldLazyLoadMetadata) {
3402
14.1k
  if (ResumeBit) {
3403
6.37k
    if (Error JumpFailed = Stream.JumpToBit(ResumeBit))
3404
0
      return JumpFailed;
3405
7.79k
  } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3406
0
    return Err;
3407
14.1k
3408
14.1k
  SmallVector<uint64_t, 64> Record;
3409
14.1k
3410
14.1k
  // Read all the records for this module.
3411
1.57M
  while (
true1.57M
) {
3412
1.57M
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3413
1.57M
    if (!MaybeEntry)
3414
0
      return MaybeEntry.takeError();
3415
1.57M
    llvm::BitstreamEntry Entry = MaybeEntry.get();
3416
1.57M
3417
1.57M
    switch (Entry.Kind) {
3418
1.57M
    case BitstreamEntry::Error:
3419
0
      return error("Malformed block");
3420
1.57M
    case BitstreamEntry::EndBlock:
3421
7.14k
      return globalCleanup();
3422
1.57M
3423
1.57M
    case BitstreamEntry::SubBlock:
3424
75.9k
      switch (Entry.ID) {
3425
75.9k
      default:  // Skip unknown content.
3426
579
        if (Error Err = Stream.SkipBlock())
3427
0
          return Err;
3428
579
        break;
3429
7.75k
      case bitc::BLOCKINFO_BLOCK_ID:
3430
7.75k
        if (readBlockInfo())
3431
0
          return error("Malformed block");
3432
7.75k
        break;
3433
7.75k
      case bitc::PARAMATTR_BLOCK_ID:
3434
5.12k
        if (Error Err = parseAttributeBlock())
3435
0
          return Err;
3436
5.12k
        break;
3437
5.12k
      case bitc::PARAMATTR_GROUP_BLOCK_ID:
3438
5.12k
        if (Error Err = parseAttributeGroupBlock())
3439
7
          return Err;
3440
5.11k
        break;
3441
7.76k
      case bitc::TYPE_BLOCK_ID_NEW:
3442
7.76k
        if (Error Err = parseTypeTable())
3443
6
          return Err;
3444
7.75k
        break;
3445
7.75k
      case bitc::VALUE_SYMTAB_BLOCK_ID:
3446
7.14k
        if (!SeenValueSymbolTable) {
3447
812
          // Either this is an old form VST without function index and an
3448
812
          // associated VST forward declaration record (which would have caused
3449
812
          // the VST to be jumped to and parsed before it was encountered
3450
812
          // normally in the stream), or there were no function blocks to
3451
812
          // trigger an earlier parsing of the VST.
3452
812
          assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3453
812
          if (Error Err = parseValueSymbolTable())
3454
0
            return Err;
3455
812
          SeenValueSymbolTable = true;
3456
6.33k
        } else {
3457
6.33k
          // We must have had a VST forward declaration record, which caused
3458
6.33k
          // the parser to jump to and parse the VST earlier.
3459
6.33k
          assert(VSTOffset > 0);
3460
6.33k
          if (Error Err = Stream.SkipBlock())
3461
0
            return Err;
3462
7.14k
        }
3463
7.14k
        break;
3464
7.14k
      case bitc::CONSTANTS_BLOCK_ID:
3465
5.95k
        if (Error Err = parseConstants())
3466
0
          return Err;
3467
5.95k
        if (Error Err = resolveGlobalAndIndirectSymbolInits())
3468
0
          return Err;
3469
5.95k
        break;
3470
5.95k
      case bitc::METADATA_BLOCK_ID:
3471
5.43k
        if (ShouldLazyLoadMetadata) {
3472
446
          if (Error Err = rememberAndSkipMetadata())
3473
0
            return Err;
3474
446
          break;
3475
446
        }
3476
4.98k
        assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3477
4.98k
        if (Error Err = MDLoader->parseModuleMetadata())
3478
0
          return Err;
3479
4.98k
        break;
3480
7.65k
      case bitc::METADATA_KIND_BLOCK_ID:
3481
7.65k
        if (Error Err = MDLoader->parseMetadataKinds())
3482
0
          return Err;
3483
7.65k
        break;
3484
12.1k
      case bitc::FUNCTION_BLOCK_ID:
3485
12.1k
        // If this is the first function body we've seen, reverse the
3486
12.1k
        // FunctionsWithBodies list.
3487
12.1k
        if (!SeenFirstFunctionBody) {
3488
7.01k
          std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3489
7.01k
          if (Error Err = globalCleanup())
3490
1
            return Err;
3491
7.01k
          SeenFirstFunctionBody = true;
3492
7.01k
        }
3493
12.1k
3494
12.1k
        
if (12.1k
VSTOffset > 012.1k
) {
3495
12.0k
          // If we have a VST forward declaration record, make sure we
3496
12.0k
          // parse the VST now if we haven't already. It is needed to
3497
12.0k
          // set up the DeferredFunctionInfo vector for lazy reading.
3498
12.0k
          if (!SeenValueSymbolTable) {
3499
6.92k
            if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
3500
0
              return Err;
3501
6.92k
            SeenValueSymbolTable = true;
3502
6.92k
            // Fall through so that we record the NextUnreadBit below.
3503
6.92k
            // This is necessary in case we have an anonymous function that
3504
6.92k
            // is later materialized. Since it will not have a VST entry we
3505
6.92k
            // need to fall back to the lazy parse to find its offset.
3506
6.92k
          } else {
3507
5.15k
            // If we have a VST forward declaration record, but have already
3508
5.15k
            // parsed the VST (just above, when the first function body was
3509
5.15k
            // encountered here), then we are resuming the parse after
3510
5.15k
            // materializing functions. The ResumeBit points to the
3511
5.15k
            // start of the last function block recorded in the
3512
5.15k
            // DeferredFunctionInfo map. Skip it.
3513
5.15k
            if (Error Err = Stream.SkipBlock())
3514
0
              return Err;
3515
5.15k
            continue;
3516
5.15k
          }
3517
12.0k
        }
3518
7.01k
3519
7.01k
        // Support older bitcode files that did not have the function
3520
7.01k
        // index in the VST, nor a VST forward declaration record, as
3521
7.01k
        // well as anonymous functions that do not have VST entries.
3522
7.01k
        // Build the DeferredFunctionInfo vector on the fly.
3523
7.01k
        if (Error Err = rememberAndSkipFunctionBody())
3524
0
          return Err;
3525
7.01k
3526
7.01k
        // Suspend parsing when we reach the function bodies. Subsequent
3527
7.01k
        // materialization calls will resume it when necessary. If the bitcode
3528
7.01k
        // file is old, the symbol table will be at the end instead and will not
3529
7.01k
        // have been seen yet. In this case, just finish the parse now.
3530
7.01k
        if (SeenValueSymbolTable) {
3531
7.01k
          NextUnreadBit = Stream.GetCurrentBitNo();
3532
7.01k
          // After the VST has been parsed, we need to make sure intrinsic name
3533
7.01k
          // are auto-upgraded.
3534
7.01k
          return globalCleanup();
3535
7.01k
        }
3536
2
        break;
3537
127
      case bitc::USELIST_BLOCK_ID:
3538
127
        if (Error Err = parseUseLists())
3539
0
          return Err;
3540
127
        break;
3541
7.65k
      case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3542
7.65k
        if (Error Err = parseOperandBundleTags())
3543
0
          return Err;
3544
7.65k
        break;
3545
7.65k
      case bitc::SYNC_SCOPE_NAMES_BLOCK_ID:
3546
3.45k
        if (Error Err = parseSyncScopeNames())
3547
0
          return Err;
3548
3.45k
        break;
3549
63.7k
      }
3550
63.7k
      continue;
3551
63.7k
3552
1.49M
    case BitstreamEntry::Record:
3553
1.49M
      // The interesting case.
3554
1.49M
      break;
3555
1.49M
    }
3556
1.49M
3557
1.49M
    // Read a record.
3558
1.49M
    Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3559
1.49M
    if (!MaybeBitCode)
3560
0
      return MaybeBitCode.takeError();
3561
1.49M
    switch (unsigned BitCode = MaybeBitCode.get()) {
3562
1.49M
    
default: break197
; // Default behavior, ignore unknown content.
3563
1.49M
    case bitc::MODULE_CODE_VERSION: {
3564
7.78k
      Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
3565
7.78k
      if (!VersionOrErr)
3566
0
        return VersionOrErr.takeError();
3567
7.78k
      UseRelativeIDs = *VersionOrErr >= 1;
3568
7.78k
      break;
3569
7.78k
    }
3570
7.78k
    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
3571
6.12k
      std::string S;
3572
6.12k
      if (convertToString(Record, 0, S))
3573
0
        return error("Invalid record");
3574
6.12k
      TheModule->setTargetTriple(S);
3575
6.12k
      break;
3576
6.12k
    }
3577
6.12k
    case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
3578
6.00k
      std::string S;
3579
6.00k
      if (convertToString(Record, 0, S))
3580
0
        return error("Invalid record");
3581
6.00k
      TheModule->setDataLayout(S);
3582
6.00k
      break;
3583
6.00k
    }
3584
6.00k
    case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
3585
63
      std::string S;
3586
63
      if (convertToString(Record, 0, S))
3587
0
        return error("Invalid record");
3588
63
      TheModule->setModuleInlineAsm(S);
3589
63
      break;
3590
63
    }
3591
63
    case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
3592
0
      // FIXME: Remove in 4.0.
3593
0
      std::string S;
3594
0
      if (convertToString(Record, 0, S))
3595
0
        return error("Invalid record");
3596
0
      // Ignore value.
3597
0
      break;
3598
0
    }
3599
454
    case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
3600
454
      std::string S;
3601
454
      if (convertToString(Record, 0, S))
3602
0
        return error("Invalid record");
3603
454
      SectionTable.push_back(S);
3604
454
      break;
3605
454
    }
3606
454
    case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
3607
21
      std::string S;
3608
21
      if (convertToString(Record, 0, S))
3609
0
        return error("Invalid record");
3610
21
      GCTable.push_back(S);
3611
21
      break;
3612
21
    }
3613
3.78k
    case bitc::MODULE_CODE_COMDAT:
3614
3.78k
      if (Error Err = parseComdatRecord(Record))
3615
0
        return Err;
3616
3.78k
      break;
3617
320k
    case bitc::MODULE_CODE_GLOBALVAR:
3618
320k
      if (Error Err = parseGlobalVarRecord(Record))
3619
1
        return Err;
3620
320k
      break;
3621
1.13M
    case bitc::MODULE_CODE_FUNCTION:
3622
1.13M
      if (Error Err = parseFunctionRecord(Record))
3623
1
        return Err;
3624
1.13M
      break;
3625
1.13M
    case bitc::MODULE_CODE_IFUNC:
3626
913
    case bitc::MODULE_CODE_ALIAS:
3627
913
    case bitc::MODULE_CODE_ALIAS_OLD:
3628
913
      if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
3629
0
        return Err;
3630
913
      break;
3631
913
    /// MODULE_CODE_VSTOFFSET: [offset]
3632
7.65k
    case bitc::MODULE_CODE_VSTOFFSET:
3633
7.65k
      if (Record.size() < 1)
3634
0
        return error("Invalid record");
3635
7.65k
      // Note that we subtract 1 here because the offset is relative to one word
3636
7.65k
      // before the start of the identification or module block, which was
3637
7.65k
      // historically always the start of the regular bitcode header.
3638
7.65k
      VSTOffset = Record[0] - 1;
3639
7.65k
      break;
3640
7.65k
    /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3641
7.65k
    case bitc::MODULE_CODE_SOURCE_FILENAME:
3642
7.65k
      SmallString<128> ValueName;
3643
7.65k
      if (convertToString(Record, 0, ValueName))
3644
0
        return error("Invalid record");
3645
7.65k
      TheModule->setSourceFileName(ValueName);
3646
7.65k
      break;
3647
1.49M
    }
3648
1.49M
    Record.clear();
3649
1.49M
  }
3650
14.1k
}
3651
3652
Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
3653
7.79k
                                      bool IsImporting) {
3654
7.79k
  TheModule = M;
3655
7.79k
  MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting,
3656
90.5k
                            [&](unsigned ID) { return getTypeByID(ID); });
3657
7.79k
  return parseModule(0, ShouldLazyLoadMetadata);
3658
7.79k
}
3659
3660
2.33M
Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
3661
2.33M
  if (!isa<PointerType>(PtrType))
3662
0
    return error("Load/Store operand is not a pointer type");
3663
2.33M
  Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3664
2.33M
3665
2.33M
  if (ValType && ValType != ElemType)
3666
1
    return error("Explicit load/store type does not match pointee "
3667
1
                 "type of pointer operand");
3668
2.33M
  if (!PointerType::isLoadableOrStorableType(ElemType))
3669
0
    return error("Cannot load/store from pointer");
3670
2.33M
  return Error::success();
3671
2.33M
}
3672
3673
void BitcodeReader::propagateByValTypes(CallBase *CB,
3674
1.20M
                                        ArrayRef<Type *> ArgsFullTys) {
3675
3.92M
  for (unsigned i = 0; i != CB->arg_size(); 
++i2.72M
) {
3676
2.72M
    if (!CB->paramHasAttr(i, Attribute::ByVal))
3677
2.72M
      continue;
3678
11
3679
11
    CB->removeParamAttr(i, Attribute::ByVal);
3680
11
    CB->addParamAttr(
3681
11
        i, Attribute::getWithByValType(
3682
11
               Context, getPointerElementFlatType(ArgsFullTys[i])));
3683
11
  }
3684
1.20M
}
3685
3686
/// Lazily parse the specified function body block.
3687
337k
Error BitcodeReader::parseFunctionBody(Function *F) {
3688
337k
  if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
3689
0
    return Err;
3690
337k
3691
337k
  // Unexpected unresolved metadata when parsing function.
3692
337k
  if (MDLoader->hasFwdRefs())
3693
0
    return error("Invalid function metadata: incoming forward references");
3694
337k
3695
337k
  InstructionList.clear();
3696
337k
  unsigned ModuleValueListSize = ValueList.size();
3697
337k
  unsigned ModuleMDLoaderSize = MDLoader->size();
3698
337k
3699
337k
  // Add all the function arguments to the value table.
3700
337k
  unsigned ArgNo = 0;
3701
337k
  FunctionType *FullFTy = FunctionTypes[F];
3702
656k
  for (Argument &I : F->args()) {
3703
656k
    assert(I.getType() == flattenPointerTypes(FullFTy->getParamType(ArgNo)) &&
3704
656k
           "Incorrect fully specified type for Function Argument");
3705
656k
    ValueList.push_back(&I, FullFTy->getParamType(ArgNo++));
3706
656k
  }
3707
337k
  unsigned NextValueNo = ValueList.size();
3708
337k
  BasicBlock *CurBB = nullptr;
3709
337k
  unsigned CurBBNo = 0;
3710
337k
3711
337k
  DebugLoc LastLoc;
3712
337k
  auto getLastInstruction = [&]() -> Instruction * {
3713
10.2k
    if (CurBB && 
!CurBB->empty()10.0k
)
3714
9.93k
      return &CurBB->back();
3715
300
    else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
3716
300
             !FunctionBBs[CurBBNo - 1]->empty())
3717
300
      return &FunctionBBs[CurBBNo - 1]->back();
3718
0
    return nullptr;
3719
0
  };
3720
337k
3721
337k
  std::vector<OperandBundleDef> OperandBundles;
3722
337k
3723
337k
  // Read all the records.
3724
337k
  SmallVector<uint64_t, 64> Record;
3725
337k
3726
10.9M
  while (true) {
3727
10.9M
    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3728
10.9M
    if (!MaybeEntry)
3729
0
      return MaybeEntry.takeError();
3730
10.9M
    llvm::BitstreamEntry Entry = MaybeEntry.get();
3731
10.9M
3732
10.9M
    switch (Entry.Kind) {
3733
10.9M
    case BitstreamEntry::Error:
3734
0
      return error("Malformed block");
3735
10.9M
    case BitstreamEntry::EndBlock:
3736
337k
      goto OutOfRecordLoop;
3737
10.9M
3738
10.9M
    case BitstreamEntry::SubBlock:
3739
668k
      switch (Entry.ID) {
3740
668k
      default:  // Skip unknown content.
3741
0
        if (Error Err = Stream.SkipBlock())
3742
0
          return Err;
3743
0
        break;
3744
153k
      case bitc::CONSTANTS_BLOCK_ID:
3745
153k
        if (Error Err = parseConstants())
3746
3
          return Err;
3747
153k
        NextValueNo = ValueList.size();
3748
153k
        break;
3749
327k
      case bitc::VALUE_SYMTAB_BLOCK_ID:
3750
327k
        if (Error Err = parseValueSymbolTable())
3751
0
          return Err;
3752
327k
        break;
3753
327k
      case bitc::METADATA_ATTACHMENT_ID:
3754
155k
        if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
3755
0
          return Err;
3756
155k
        break;
3757
155k
      case bitc::METADATA_BLOCK_ID:
3758
30.7k
        assert(DeferredMetadataInfo.empty() &&
3759
30.7k
               "Must read all module-level metadata before function-level");
3760
30.7k
        if (Error Err = MDLoader->parseFunctionMetadata())
3761
0
          return Err;
3762
30.7k
        break;
3763
30.7k
      case bitc::USELIST_BLOCK_ID:
3764
498
        if (Error Err = parseUseLists())
3765
0
          return Err;
3766
498
        break;
3767
668k
      }
3768
668k
      continue;
3769
668k
3770
9.93M
    case BitstreamEntry::Record:
3771
9.93M
      // The interesting case.
3772
9.93M
      break;
3773
9.93M
    }
3774
9.93M
3775
9.93M
    // Read a record.
3776
9.93M
    Record.clear();
3777
9.93M
    Instruction *I = nullptr;
3778
9.93M
    Type *FullTy = nullptr;
3779
9.93M
    Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record);
3780
9.93M
    if (!MaybeBitCode)
3781
0
      return MaybeBitCode.takeError();
3782
9.93M
    switch (unsigned BitCode = MaybeBitCode.get()) {
3783
9.93M
    default: // Default behavior: reject
3784
0
      return error("Invalid value");
3785
9.93M
    case bitc::FUNC_CODE_DECLAREBLOCKS: {   // DECLAREBLOCKS: [nblocks]
3786
337k
      if (
Record.size() < 1337k
|| Record[0] == 0)
3787
0
        return error("Invalid record");
3788
337k
      // Create all the basic blocks for the function.
3789
337k
      FunctionBBs.resize(Record[0]);
3790
337k
3791
337k
      // See if anything took the address of blocks in this function.
3792
337k
      auto BBFRI = BasicBlockFwdRefs.find(F);
3793
337k
      if (BBFRI == BasicBlockFwdRefs.end()) {
3794
1.90M
        for (unsigned i = 0, e = FunctionBBs.size(); i != e; 
++i1.56M
)
3795
1.56M
          FunctionBBs[i] = BasicBlock::Create(Context, "", F);
3796
337k
      } else {
3797
39
        auto &BBRefs = BBFRI->second;
3798
39
        // Check for invalid basic block references.
3799
39
        if (BBRefs.size() > FunctionBBs.size())
3800
0
          return error("Invalid ID");
3801
39
        assert(!BBRefs.empty() && "Unexpected empty array");
3802
39
        assert(!BBRefs.front() && "Invalid reference to entry block");
3803
144
        for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
3804
105
             ++I)
3805
105
          if (I < RE && 
BBRefs[I]92
) {
3806
45
            BBRefs[I]->insertInto(F);
3807
45
            FunctionBBs[I] = BBRefs[I];
3808
60
          } else {
3809
60
            FunctionBBs[I] = BasicBlock::Create(Context, "", F);
3810
60
          }
3811
39
3812
39
        // Erase from the table.
3813
39
        BasicBlockFwdRefs.erase(BBFRI);
3814
39
      }
3815
337k
3816
337k
      CurBB = FunctionBBs[0];
3817
337k
      continue;
3818
337k
    }
3819
337k
3820
337k
    case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
3821
5.23k
      // This record indicates that the last instruction is at the same
3822
5.23k
      // location as the previous instruction with a location.
3823
5.23k
      I = getLastInstruction();
3824
5.23k
3825
5.23k
      if (!I)
3826
0
        return error("Invalid record");
3827
5.23k
      I->setDebugLoc(LastLoc);
3828
5.23k
      I = nullptr;
3829
5.23k
      continue;
3830
5.23k
3831
5.23k
    case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
3832
5.00k
      I = getLastInstruction();
3833
5.00k
      if (!I || Record.size() < 4)
3834
0
        return error("Invalid record");
3835
5.00k
3836
5.00k
      unsigned Line = Record[0], Col = Record[1];
3837
5.00k
      unsigned ScopeID = Record[2], IAID = Record[3];
3838
5.00k
      bool isImplicitCode = Record.size() == 5 && 
Record[4]737
;
3839
5.00k
3840
5.00k
      MDNode *Scope = nullptr, *IA = nullptr;
3841
5.00k
      if (ScopeID) {
3842
5.00k
        Scope = dyn_cast_or_null<MDNode>(
3843
5.00k
            MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));
3844
5.00k
        if (!Scope)
3845
0
          return error("Invalid record");
3846
5.00k
      }
3847
5.00k
      if (IAID) {
3848
3.34k
        IA = dyn_cast_or_null<MDNode>(
3849
3.34k
            MDLoader->getMetadataFwdRefOrLoad(IAID - 1));
3850
3.34k
        if (!IA)
3851
0
          return error("Invalid record");
3852
5.00k
      }
3853
5.00k
      LastLoc = DebugLoc::get(Line, Col, Scope, IA, isImplicitCode);
3854
5.00k
      I->setDebugLoc(LastLoc);
3855
5.00k
      I = nullptr;
3856
5.00k
      continue;
3857
5.00k
    }
3858
5.00k
    case bitc::FUNC_CODE_INST_UNOP: {    // UNOP: [opval, ty, opcode]
3859
52
      unsigned OpNum = 0;
3860
52
      Value *LHS;
3861
52
      if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3862
52
          OpNum+1 > Record.size())
3863
0
        return error("Invalid record");
3864
52
3865
52
      int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
3866
52
      if (Opc == -1)
3867
0
        return error("Invalid record");
3868
52
      I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
3869
52
      InstructionList.push_back(I);
3870
52
      if (OpNum < Record.size()) {
3871
36
        if (isa<FPMathOperator>(I)) {
3872
36
          FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3873
36
          if (FMF.any())
3874
36
            I->setFastMathFlags(FMF);
3875
36
        }
3876
36
      }
3877
52
      break;
3878
52
    }
3879
585k
    case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
3880
585k
      unsigned OpNum = 0;
3881
585k
      Value *LHS, *RHS;
3882
585k
      if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3883
585k
          popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
3884
585k
          
OpNum+1 > Record.size()585k
)
3885
1
        return error("Invalid record");
3886
585k
3887
585k
      int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
3888
585k
      if (Opc == -1)
3889
1
        return error("Invalid record");
3890
585k
      I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3891
585k
      InstructionList.push_back(I);
3892
585k
      if (OpNum < Record.size()) {
3893
239k
        if (Opc == Instruction::Add ||
3894
239k
            
Opc == Instruction::Sub88.3k
||
3895
239k
            
Opc == Instruction::Mul64.9k
||
3896
239k
            
Opc == Instruction::Shl15.8k
) {
3897
234k
          if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3898
223k
            cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
3899
234k
          if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3900
61.9k
            cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
3901
234k
        } else 
if (4.63k
Opc == Instruction::SDiv4.63k
||
3902
4.63k
                   
Opc == Instruction::UDiv4.48k
||
3903
4.63k
                   
Opc == Instruction::LShr4.46k
||
3904
4.63k
                   
Opc == Instruction::AShr4.44k
) {
3905
4.38k
          if (Record[OpNum] & (1 << bitc::PEO_EXACT))
3906
4.38k
            cast<BinaryOperator>(I)->setIsExact(true);
3907
4.38k
        } else 
if (248
isa<FPMathOperator>(I)248
) {
3908
248
          FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3909
248
          if (FMF.any())
3910
248
            I->setFastMathFlags(FMF);
3911
248
        }
3912
239k
3913
239k
      }
3914
585k
      break;
3915
585k
    }
3916
718k
    case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
3917
718k
      unsigned OpNum = 0;
3918
718k
      Value *Op;
3919
718k
      if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3920
718k
          OpNum+2 != Record.size())
3921
0
        return error("Invalid record");
3922
718k
3923
718k
      FullTy = getFullyStructuredTypeByID(Record[OpNum]);
3924
718k
      Type *ResTy = flattenPointerTypes(FullTy);
3925
718k
      int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
3926
718k
      if (Opc == -1 || !ResTy)
3927
0
        return error("Invalid record");
3928
718k
      Instruction *Temp = nullptr;
3929
718k
      if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
3930
0
        if (Temp) {
3931
0
          InstructionList.push_back(Temp);
3932
0
          CurBB->getInstList().push_back(Temp);
3933
0
        }
3934
718k
      } else {
3935
718k
        auto CastOp = (Instruction::CastOps)Opc;
3936
718k
        if (!CastInst::castIsValid(CastOp, Op, ResTy))
3937
1
          return error("Invalid cast");
3938
718k
        I = CastInst::Create(CastOp, Op, ResTy);
3939
718k
      }
3940
718k
      InstructionList.push_back(I);
3941
718k
      break;
3942
718k
    }
3943
1.87M
    case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
3944
1.87M
    case bitc::FUNC_CODE_INST_GEP_OLD:
3945
1.87M
    case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
3946
1.87M
      unsigned OpNum = 0;
3947
1.87M
3948
1.87M
      Type *Ty;
3949
1.87M
      bool InBounds;
3950
1.87M
3951
1.87M
      if (BitCode == bitc::FUNC_CODE_INST_GEP) {
3952
1.87M
        InBounds = Record[OpNum++];
3953
1.87M
        FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
3954
1.87M
        Ty = flattenPointerTypes(FullTy);
3955
1.87M
      } else {
3956
320
        InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
3957
320
        Ty = nullptr;
3958
320
      }
3959
1.87M
3960
1.87M
      Value *BasePtr;
3961
1.87M
      Type *FullBaseTy = nullptr;
3962
1.87M
      if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, &FullBaseTy))
3963
0
        return error("Invalid record");
3964
1.87M
3965
1.87M
      if (!Ty) {
3966
320
        std::tie(FullTy, Ty) =
3967
320
            getPointerElementTypes(FullBaseTy->getScalarType());
3968
1.87M
      } else if (Ty != getPointerElementFlatType(FullBaseTy->getScalarType()))
3969
1
        return error(
3970
1
            "Explicit gep type does not match pointee type of pointer operand");
3971
1.87M
3972
1.87M
      SmallVector<Value*, 16> GEPIdx;
3973
6.48M
      while (OpNum != Record.size()) {
3974
4.60M
        Value *Op;
3975
4.60M
        if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3976
0
          return error("Invalid record");
3977
4.60M
        GEPIdx.push_back(Op);
3978
4.60M
      }
3979
1.87M
3980
1.87M
      I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
3981
1.87M
      FullTy = GetElementPtrInst::getGEPReturnType(FullTy, I, GEPIdx);
3982
1.87M
3983
1.87M
      InstructionList.push_back(I);
3984
1.87M
      if (InBounds)
3985
1.81M
        cast<GetElementPtrInst>(I)->setIsInBounds(true);
3986
1.87M
      break;
3987
1.87M
    }
3988
1.87M
3989
1.87M
    case bitc::FUNC_CODE_INST_EXTRACTVAL: {
3990
4.73k
                                       // EXTRACTVAL: [opty, opval, n x indices]
3991
4.73k
      unsigned OpNum = 0;
3992
4.73k
      Value *Agg;
3993
4.73k
      if (getValueTypePair(Record, OpNum, NextValueNo, Agg, &FullTy))
3994
0
        return error("Invalid record");
3995
4.73k
3996
4.73k
      unsigned RecSize = Record.size();
3997
4.73k
      if (OpNum == RecSize)
3998
1
        return error("EXTRACTVAL: Invalid instruction with 0 indices");
3999
4.73k
4000
4.73k
      SmallVector<unsigned, 4> EXTRACTVALIdx;
4001
9.52k
      for (; OpNum != RecSize; 
++OpNum4.79k
) {
4002
4.79k
        bool IsArray = FullTy->isArrayTy();
4003
4.79k
        bool IsStruct = FullTy->isStructTy();
4004
4.79k
        uint64_t Index = Record[OpNum];
4005
4.79k
4006
4.79k
        if (!IsStruct && 
!IsArray82
)
4007
1
          return error("EXTRACTVAL: Invalid type");
4008
4.79k
        if ((unsigned)Index != Index)
4009
0
          return error("Invalid value");
4010
4.79k
        if (IsStruct && 
Index >= FullTy->getStructNumElements()4.71k
)
4011
1
          return error("EXTRACTVAL: Invalid struct index");
4012
4.79k
        if (IsArray && 
Index >= FullTy->getArrayNumElements()81
)
4013
1
          return error("EXTRACTVAL: Invalid array index");
4014
4.79k
        EXTRACTVALIdx.push_back((unsigned)Index);
4015
4.79k
4016
4.79k
        if (IsStruct)
4017
4.71k
          FullTy = FullTy->getStructElementType(Index);
4018
80
        else
4019
80
          FullTy = FullTy->getArrayElementType();
4020
4.79k
      }
4021
4.73k
4022
4.73k
      I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
4023
4.72k
      InstructionList.push_back(I);
4024
4.72k
      break;
4025
4.73k
    }
4026
4.73k
4027
4.73k
    case bitc::FUNC_CODE_INST_INSERTVAL: {
4028
87
                           // INSERTVAL: [opty, opval, opty, opval, n x indices]
4029
87
      unsigned OpNum = 0;
4030
87
      Value *Agg;
4031
87
      if (getValueTypePair(Record, OpNum, NextValueNo, Agg, &FullTy))
4032
0
        return error("Invalid record");
4033
87
      Value *Val;
4034
87
      if (getValueTypePair(Record, OpNum, NextValueNo, Val))
4035
1
        return error("Invalid record");
4036
86
4037
86
      unsigned RecSize = Record.size();
4038
86
      if (OpNum == RecSize)
4039
1
        return error("INSERTVAL: Invalid instruction with 0 indices");
4040
85
4041
85
      SmallVector<unsigned, 4> INSERTVALIdx;
4042
85
      Type *CurTy = Agg->getType();
4043
208
      for (; OpNum != RecSize; 
++OpNum123
) {
4044
126
        bool IsArray = CurTy->isArrayTy();
4045
126
        bool IsStruct = CurTy->isStructTy();
4046
126
        uint64_t Index = Record[OpNum];
4047
126
4048
126
        if (!IsStruct && 
!IsArray47
)
4049
1
          return error("INSERTVAL: Invalid type");
4050
125
        if ((unsigned)Index != Index)
4051
0
          return error("Invalid value");
4052
125
        if (IsStruct && 
Index >= CurTy->getStructNumElements()79
)
4053
1
          return error("INSERTVAL: Invalid struct index");
4054
124
        if (IsArray && 
Index >= CurTy->getArrayNumElements()46
)
4055
1
          return error("INSERTVAL: Invalid array index");
4056
123
4057
123
        INSERTVALIdx.push_back((unsigned)Index);
4058
123
        if (IsStruct)
4059
78
          CurTy = CurTy->getStructElementType(Index);
4060
45
        else
4061
45
          CurTy = CurTy->getArrayElementType();
4062
123
      }
4063
85
4064
85
      
if (82
CurTy != Val->getType()82
)
4065
1
        return error("Inserted value type doesn't match aggregate type");
4066
81
4067
81
      I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
4068
81
      InstructionList.push_back(I);
4069
81
      break;
4070
81
    }
4071
81
4072
81
    case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
4073
0
      // obsolete form of select
4074
0
      // handles select i1 ... in old bitcode
4075
0
      unsigned OpNum = 0;
4076
0
      Value *TrueVal, *FalseVal, *Cond;
4077
0
      if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, &FullTy) ||
4078
0
          popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4079
0
          popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
4080
0
        return error("Invalid record");
4081
0
4082
0
      I = SelectInst::Create(Cond, TrueVal, FalseVal);
4083
0
      InstructionList.push_back(I);
4084
0
      break;
4085
0
    }
4086
0
4087
57.2k
    case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4088
57.2k
      // new form of select
4089
57.2k
      // handles select i1 or select [N x i1]
4090
57.2k
      unsigned OpNum = 0;
4091
57.2k
      Value *TrueVal, *FalseVal, *Cond;
4092
57.2k
      if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, &FullTy) ||
4093
57.2k
          popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4094
57.2k
          getValueTypePair(Record, OpNum, NextValueNo, Cond))
4095
0
        return error("Invalid record");
4096
57.2k
4097
57.2k
      // select condition can be either i1 or [N x i1]
4098
57.2k
      if (VectorType* vector_type =
4099
99
          dyn_cast<VectorType>(Cond->getType())) {
4100
99
        // expect <n x i1>
4101
99
        if (vector_type->getElementType() != Type::getInt1Ty(Context))
4102
0
          return error("Invalid type for value");
4103
57.1k
      } else {
4104
57.1k
        // expect i1
4105
57.1k
        if (Cond->getType() != Type::getInt1Ty(Context))
4106
0
          return error("Invalid type for value");
4107
57.2k
      }
4108
57.2k
4109
57.2k
      I = SelectInst::Create(Cond, TrueVal, FalseVal);
4110
57.2k
      InstructionList.push_back(I);
4111
57.2k
      if (OpNum < Record.size() && 
isa<FPMathOperator>(I)20
) {
4112
20
        FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
4113
20
        if (FMF.any())
4114
20
          I->setFastMathFlags(FMF);
4115
20
      }
4116
57.2k
      break;
4117
57.2k
    }
4118
57.2k
4119
57.2k
    case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
4120
3.00k
      unsigned OpNum = 0;
4121
3.00k
      Value *Vec, *Idx;
4122
3.00k
      if (getValueTypePair(Record, OpNum, NextValueNo, Vec, &FullTy) ||
4123
3.00k
          getValueTypePair(Record, OpNum, NextValueNo, Idx))
4124
0
        return error("Invalid record");
4125
3.00k
      if (!Vec->getType()->isVectorTy())
4126
1
        return error("Invalid type for value");
4127
3.00k
      I = ExtractElementInst::Create(Vec, Idx);
4128
3.00k
      FullTy = FullTy->getVectorElementType();
4129
3.00k
      InstructionList.push_back(I);
4130
3.00k
      break;
4131
3.00k
    }
4132
3.00k
4133
3.73k
    case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
4134
3.73k
      unsigned OpNum = 0;
4135
3.73k
      Value *Vec, *Elt, *Idx;
4136
3.73k
      if (getValueTypePair(Record, OpNum, NextValueNo, Vec, &FullTy))
4137
0
        return error("Invalid record");
4138
3.73k
      if (!Vec->getType()->isVectorTy())
4139
1
        return error("Invalid type for value");
4140
3.73k
      if (popValue(Record, OpNum, NextValueNo,
4141
3.73k
                   cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
4142
3.73k
          getValueTypePair(Record, OpNum, NextValueNo, Idx))
4143
0
        return error("Invalid record");
4144
3.73k
      I = InsertElementInst::Create(Vec, Elt, Idx);
4145
3.73k
      InstructionList.push_back(I);
4146
3.73k
      break;
4147
3.73k
    }
4148
3.73k
4149
13.2k
    case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4150
13.2k
      unsigned OpNum = 0;
4151
13.2k
      Value *Vec1, *Vec2, *Mask;
4152
13.2k
      if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, &FullTy) ||
4153
13.2k
          popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
4154
0
        return error("Invalid record");
4155
13.2k
4156
13.2k
      if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
4157
0
        return error("Invalid record");
4158
13.2k
      if (!Vec1->getType()->isVectorTy() || 
!Vec2->getType()->isVectorTy()13.2k
)
4159
1
        return error("Invalid type for value");
4160
13.2k
      I = new ShuffleVectorInst(Vec1, Vec2, Mask);
4161
13.2k
      FullTy = VectorType::get(FullTy->getVectorElementType(),
4162
13.2k
                               Mask->getType()->getVectorNumElements());
4163
13.2k
      InstructionList.push_back(I);
4164
13.2k
      break;
4165
13.2k
    }
4166
13.2k
4167
822k
    case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
4168
822k
      // Old form of ICmp/FCmp returning bool
4169
822k
      // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4170
822k
      // both legal on vectors but had different behaviour.
4171
822k
    case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4172
822k
      // FCmp/ICmp returning bool or vector of bool
4173
822k
4174
822k
      unsigned OpNum = 0;
4175
822k
      Value *LHS, *RHS;
4176
822k
      if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4177
822k
          popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4178
0
        return error("Invalid record");
4179
822k
4180
822k
      if (OpNum >= Record.size())
4181
1
        return error(
4182
1
            "Invalid record: operand number exceeded available operands");
4183
822k
4184
822k
      unsigned PredVal = Record[OpNum];
4185
822k
      bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4186
822k
      FastMathFlags FMF;
4187
822k
      if (IsFP && 
Record.size() > OpNum+17.19k
)
4188
3
        FMF = getDecodedFastMathFlags(Record[++OpNum]);
4189
822k
4190
822k
      if (OpNum+1 != Record.size())
4191
0
        return error("Invalid record");
4192
822k
4193
822k
      if (LHS->getType()->isFPOrFPVectorTy())
4194
7.19k
        I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
4195
815k
      else
4196
815k
        I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4197
822k
4198
822k
      if (FMF.any())
4199
3
        I->setFastMathFlags(FMF);
4200
822k
      InstructionList.push_back(I);
4201
822k
      break;
4202
822k
    }
4203
822k
4204
822k
    case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
4205
340k
      {
4206
340k
        unsigned Size = Record.size();
4207
340k
        if (Size == 0) {
4208
80.4k
          I = ReturnInst::Create(Context);
4209
80.4k
          InstructionList.push_back(I);
4210
80.4k
          break;
4211
80.4k
        }
4212
260k
4213
260k
        unsigned OpNum = 0;
4214
260k
        Value *Op = nullptr;
4215
260k
        if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4216
0
          return error("Invalid record");
4217
260k
        if (OpNum != Record.size())
4218
0
          return error("Invalid record");
4219
260k
4220
260k
        I = ReturnInst::Create(Context, Op);
4221
260k
        InstructionList.push_back(I);
4222
260k
        break;
4223
260k
      }
4224
1.21M
    case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
4225
1.21M
      if (Record.size() != 1 && 
Record.size() != 3643k
)
4226
0
        return error("Invalid record");
4227
1.21M
      BasicBlock *TrueDest = getBasicBlock(Record[0]);
4228
1.21M
      if (!TrueDest)
4229
0
        return error("Invalid record");
4230
1.21M
4231
1.21M
      if (Record.size() == 1) {
4232
569k
        I = BranchInst::Create(TrueDest);
4233
569k
        InstructionList.push_back(I);
4234
569k
      }
4235
643k
      else {
4236
643k
        BasicBlock *FalseDest = getBasicBlock(Record[1]);
4237
643k
        Value *Cond = getValue(Record, 2, NextValueNo,
4238
643k
                               Type::getInt1Ty(Context));
4239
643k
        if (!FalseDest || !Cond)
4240
0
          return error("Invalid record");
4241
643k
        I = BranchInst::Create(TrueDest, FalseDest, Cond);
4242
643k
        InstructionList.push_back(I);
4243
643k
      }
4244
1.21M
      break;
4245
1.21M
    }
4246
1.21M
    case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
4247
18
      if (Record.size() != 1 && 
Record.size() != 22
)
4248
0
        return error("Invalid record");
4249
18
      unsigned Idx = 0;
4250
18
      Value *CleanupPad =
4251
18
          getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4252
18
      if (!CleanupPad)
4253
0
        return error("Invalid record");
4254
18
      BasicBlock *UnwindDest = nullptr;
4255
18
      if (Record.size() == 2) {
4256
2
        UnwindDest = getBasicBlock(Record[Idx++]);
4257
2
        if (!UnwindDest)
4258
0
          return error("Invalid record");
4259
18
      }
4260
18
4261
18
      I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
4262
18
      InstructionList.push_back(I);
4263
18
      break;
4264
18
    }
4265
18
    case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4266
16
      if (Record.size() != 2)
4267
0
        return error("Invalid record");
4268
16
      unsigned Idx = 0;
4269
16
      Value *CatchPad =
4270
16
          getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4271
16
      if (!CatchPad)
4272
0
        return error("Invalid record");
4273
16
      BasicBlock *BB = getBasicBlock(Record[Idx++]);
4274
16
      if (!BB)
4275
0
        return error("Invalid record");
4276
16
4277
16
      I = CatchReturnInst::Create(CatchPad, BB);
4278
16
      InstructionList.push_back(I);
4279
16
      break;
4280
16
    }
4281
45
    case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4282
45
      // We must have, at minimum, the outer scope and the number of arguments.
4283
45
      if (Record.size() < 2)
4284
0
        return error("Invalid record");
4285
45
4286
45
      unsigned Idx = 0;
4287
45
4288
45
      Value *ParentPad =
4289
45
          getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4290
45
4291
45
      unsigned NumHandlers = Record[Idx++];
4292
45
4293
45
      SmallVector<BasicBlock *, 2> Handlers;
4294
92
      for (unsigned Op = 0; Op != NumHandlers; 
++Op47
) {
4295
47
        BasicBlock *BB = getBasicBlock(Record[Idx++]);
4296
47
        if (!BB)
4297
0
          return error("Invalid record");
4298
47
        Handlers.push_back(BB);
4299
47
      }
4300
45
4301
45
      BasicBlock *UnwindDest = nullptr;
4302
45
      if (Idx + 1 == Record.size()) {
4303
20
        UnwindDest = getBasicBlock(Record[Idx++]);
4304
20
        if (!UnwindDest)
4305
0
          return error("Invalid record");
4306
45
      }
4307
45
4308
45
      if (Record.size() != Idx)
4309
0
        return error("Invalid record");
4310
45
4311
45
      auto *CatchSwitch =
4312
45
          CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4313
45
      for (BasicBlock *Handler : Handlers)
4314
47
        CatchSwitch->addHandler(Handler);
4315
45
      I = CatchSwitch;
4316
45
      InstructionList.push_back(I);
4317
45
      break;
4318
45
    }
4319
91
    case bitc::FUNC_CODE_INST_CATCHPAD:
4320
91
    case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4321
91
      // We must have, at minimum, the outer scope and the number of arguments.
4322
91
      if (Record.size() < 2)
4323
0
        return error("Invalid record");
4324
91
4325
91
      unsigned Idx = 0;
4326
91
4327
91
      Value *ParentPad =
4328
91
          getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4329
91
4330
91
      unsigned NumArgOperands = Record[Idx++];
4331
91
4332
91
      SmallVector<Value *, 2> Args;
4333
136
      for (unsigned Op = 0; Op != NumArgOperands; 
++Op45
) {
4334
45
        Value *Val;
4335
45
        if (getValueTypePair(Record, Idx, NextValueNo, Val))
4336
0
          return error("Invalid record");
4337
45
        Args.push_back(Val);
4338
45
      }
4339
91
4340
91
      if (Record.size() != Idx)
4341
0
        return error("Invalid record");
4342
91
4343
91
      if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4344
44
        I = CleanupPadInst::Create(ParentPad, Args);
4345
47
      else
4346
47
        I = CatchPadInst::Create(ParentPad, Args);
4347
91
      InstructionList.push_back(I);
4348
91
      break;
4349
91
    }
4350
9.82k
    case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4351
9.82k
      // Check magic
4352
9.82k
      if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4353
3
        // "New" SwitchInst format with case ranges. The changes to write this
4354
3
        // format were reverted but we still recognize bitcode that uses it.
4355
3
        // Hopefully someday we will have support for case ranges and can use
4356
3
        // this format again.
4357
3
4358
3
        Type *OpTy = getTypeByID(Record[1]);
4359
3
        unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4360
3
4361
3
        Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
4362
3
        BasicBlock *Default = getBasicBlock(Record[3]);
4363
3
        if (!OpTy || !Cond || !Default)
4364
0
          return error("Invalid record");
4365
3
4366
3
        unsigned NumCases = Record[4];
4367
3
4368
3
        SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4369
3
        InstructionList.push_back(SI);
4370
3
4371
3
        unsigned CurIdx = 5;
4372
25
        for (unsigned i = 0; i != NumCases; 
++i22
) {
4373
22
          SmallVector<ConstantInt*, 1> CaseVals;
4374
22
          unsigned NumItems = Record[CurIdx++];
4375
44
          for (unsigned ci = 0; ci != NumItems; 
++ci22
) {
4376
22
            bool isSingleNumber = Record[CurIdx++];
4377
22
4378
22
            APInt Low;
4379
22
            unsigned ActiveWords = 1;
4380
22
            if (ValueBitWidth > 64)
4381
0
              ActiveWords = Record[CurIdx++];
4382
22
            Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
4383
22
                                ValueBitWidth);
4384
22
            CurIdx += ActiveWords;
4385
22
4386
22
            if (!isSingleNumber) {
4387
0
              ActiveWords = 1;
4388
0
              if (ValueBitWidth > 64)
4389
0
                ActiveWords = Record[CurIdx++];
4390
0
              APInt High = readWideAPInt(
4391
0
                  makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
4392
0
              CurIdx += ActiveWords;
4393
0
4394
0
              // FIXME: It is not clear whether values in the range should be
4395
0
              // compared as signed or unsigned values. The partially
4396
0
              // implemented changes that used this format in the past used
4397
0
              // unsigned comparisons.
4398
0
              for ( ; Low.ule(High); ++Low)
4399
0
                CaseVals.push_back(ConstantInt::get(Context, Low));
4400
0
            } else
4401
22
              CaseVals.push_back(ConstantInt::get(Context, Low));
4402
22
          }
4403
22
          BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
4404
22
          for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4405
44
                 cve = CaseVals.end(); cvi != cve; 
++cvi22
)
4406
22
            SI->addCase(*cvi, DestBB);
4407
22
        }
4408
3
        I = SI;
4409
3
        break;
4410
3
      }
4411
9.82k
4412
9.82k
      // Old SwitchInst format without case ranges.
4413
9.82k
4414
9.82k
      if (Record.size() < 3 || (Record.size() & 1) == 0)
4415
0
        return error("Invalid record");
4416
9.82k
      Type *OpTy = getTypeByID(Record[0]);
4417
9.82k
      Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
4418
9.82k
      BasicBlock *Default = getBasicBlock(Record[2]);
4419
9.82k
      if (!OpTy || !Cond || !Default)
4420
0
        return error("Invalid record");
4421
9.82k
      unsigned NumCases = (Record.size()-3)/2;
4422
9.82k
      SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4423
9.82k
      InstructionList.push_back(SI);
4424
33.7k
      for (unsigned i = 0, e = NumCases; i != e; 
++i23.8k
) {
4425
23.8k
        ConstantInt *CaseVal =
4426
23.8k
          dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4427
23.8k
        BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
4428
23.8k
        if (!CaseVal || !DestBB) {
4429
0
          delete SI;
4430
0
          return error("Invalid record");
4431
0
        }
4432
23.8k
        SI->addCase(CaseVal, DestBB);
4433
23.8k
      }
4434
9.82k
      I = SI;
4435
9.82k
      break;
4436
9.82k
    }
4437
9.82k
    case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
4438
27
      if (Record.size() < 2)
4439
0
        return error("Invalid record");
4440
27
      Type *OpTy = getTypeByID(Record[0]);
4441
27
      Value *Address = getValue(Record, 1, NextValueNo, OpTy);
4442
27
      if (!OpTy || !Address)
4443
0
        return error("Invalid record");
4444
27
      unsigned NumDests = Record.size()-2;
4445
27
      IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
4446
27
      InstructionList.push_back(IBI);
4447
71
      for (unsigned i = 0, e = NumDests; i != e; 
++i44
) {
4448
44
        if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4449
44
          IBI->addDestination(DestBB);
4450
44
        } else {
4451
0
          delete IBI;
4452
0
          return error("Invalid record");
4453
0
        }
4454
44
      }
4455
27
      I = IBI;
4456
27
      break;
4457
27
    }
4458
27
4459
245
    case bitc::FUNC_CODE_INST_INVOKE: {
4460
245
      // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4461
245
      if (Record.size() < 4)
4462
0
        return error("Invalid record");
4463
245
      unsigned OpNum = 0;
4464
245
      AttributeList PAL = getAttributes(Record[OpNum++]);
4465
245
      unsigned CCInfo = Record[OpNum++];
4466
245
      BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4467
245
      BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
4468
245
4469
245
      FunctionType *FTy = nullptr;
4470
245
      FunctionType *FullFTy = nullptr;
4471
245
      if ((CCInfo >> 13) & 1) {
4472
240
        FullFTy =
4473
240
            dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
4474
240
        if (!FullFTy)
4475
1
          return error("Explicit invoke type is not a function type");
4476
239
        FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4477
239
      }
4478
245
4479
245
      Value *Callee;
4480
244
      if (getValueTypePair(Record, OpNum, NextValueNo, Callee, &FullTy))
4481
0
        return error("Invalid record");
4482
244
4483
244
      PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
4484
244
      if (!CalleeTy)
4485
0
        return error("Callee is not a pointer");
4486
244
      if (!FTy) {
4487
5
        FullFTy =
4488
5
            dyn_cast<FunctionType>(cast<PointerType>(FullTy)->getElementType());
4489
5
        if (!FullFTy)
4490
0
          return error("Callee is not of pointer to function type");
4491
5
        FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4492
239
      } else if (getPointerElementFlatType(FullTy) != FTy)
4493
1
        return error("Explicit invoke type does not match pointee type of "
4494
1
                     "callee operand");
4495
243
      if (Record.size() < FTy->getNumParams() + OpNum)
4496
0
        return error("Insufficient operands to call");
4497
243
4498
243
      SmallVector<Value*, 16> Ops;
4499
243
      SmallVector<Type *, 16> ArgsFullTys;
4500
286
      for (unsigned i = 0, e = FTy->getNumParams(); i != e; 
++i, ++OpNum43
) {
4501
43
        Ops.push_back(getValue(Record, OpNum, NextValueNo,
4502
43
                               FTy->getParamType(i)));
4503
43
        ArgsFullTys.push_back(FullFTy->getParamType(i));
4504
43
        if (!Ops.back())
4505
0
          return error("Invalid record");
4506
43
      }
4507
243
4508
243
      if (!FTy->isVarArg()) {
4509
237
        if (Record.size() != OpNum)
4510
0
          return error("Invalid record");
4511
6
      } else {
4512
6
        // Read type/value pairs for varargs params.
4513
18
        while (OpNum != Record.size()) {
4514
12
          Value *Op;
4515
12
          Type *FullTy;
4516
12
          if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy))
4517
0
            return error("Invalid record");
4518
12
          Ops.push_back(Op);
4519
12
          ArgsFullTys.push_back(FullTy);
4520
12
        }
4521
6
      }
4522
243
4523
243
      I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops,
4524
243
                             OperandBundles);
4525
243
      FullTy = FullFTy->getReturnType();
4526
243
      OperandBundles.clear();
4527
243
      InstructionList.push_back(I);
4528
243
      cast<InvokeInst>(I)->setCallingConv(
4529
243
          static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
4530
243
      cast<InvokeInst>(I)->setAttributes(PAL);
4531
243
      propagateByValTypes(cast<CallBase>(I), ArgsFullTys);
4532
243
4533
243
      break;
4534
243
    }
4535
243
    case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4536
20
      unsigned Idx = 0;
4537
20
      Value *Val = nullptr;
4538
20
      if (getValueTypePair(Record, Idx, NextValueNo, Val))
4539
0
        return error("Invalid record");
4540
20
      I = ResumeInst::Create(Val);
4541
20
      InstructionList.push_back(I);
4542
20
      break;
4543
20
    }
4544
20
    case bitc::FUNC_CODE_INST_CALLBR: {
4545
1
      // CALLBR: [attr, cc, norm, transfs, fty, fnid, args]
4546
1
      unsigned OpNum = 0;
4547
1
      AttributeList PAL = getAttributes(Record[OpNum++]);
4548
1
      unsigned CCInfo = Record[OpNum++];
4549
1
4550
1
      BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]);
4551
1
      unsigned NumIndirectDests = Record[OpNum++];
4552
1
      SmallVector<BasicBlock *, 16> IndirectDests;
4553
2
      for (unsigned i = 0, e = NumIndirectDests; i != e; 
++i1
)
4554
1
        IndirectDests.push_back(getBasicBlock(Record[OpNum++]));
4555
1
4556
1
      FunctionType *FTy = nullptr;
4557
1
      FunctionType *FullFTy = nullptr;
4558
1
      if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
4559
1
        FullFTy =
4560
1
            dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
4561
1
        if (!FullFTy)
4562
0
          return error("Explicit call type is not a function type");
4563
1
        FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4564
1
      }
4565
1
4566
1
      Value *Callee;
4567
1
      if (getValueTypePair(Record, OpNum, NextValueNo, Callee, &FullTy))
4568
0
        return error("Invalid record");
4569
1
4570
1
      PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
4571
1
      if (!OpTy)
4572
0
        return error("Callee is not a pointer type");
4573
1
      if (!FTy) {
4574
0
        FullFTy =
4575
0
            dyn_cast<FunctionType>(cast<PointerType>(FullTy)->getElementType());
4576
0
        if (!FullFTy)
4577
0
          return error("Callee is not of pointer to function type");
4578
0
        FTy = cast<FunctionType>(flattenPointerTypes(FullFTy));
4579
1
      } else if (getPointerElementFlatType(FullTy) != FTy)
4580
0
        return error("Explicit call type does not match pointee type of "
4581
0
                     "callee operand");
4582
1
      if (Record.size() < FTy->getNumParams() + OpNum)
4583
0
        return error("Insufficient operands to call");
4584
1
4585
1
      SmallVector<Value*, 16> Args;
4586
1
      // Read the fixed params.
4587
3
      for (unsigned i = 0, e = FTy->getNumParams(); i != e; 
++i, ++OpNum2
) {
4588
2
        if (FTy->getParamType(i)->isLabelTy())
4589
0
          Args.push_back(getBasicBlock(Record[OpNum]));
4590
2
        else
4591
2
          Args.push_back(getValue(Record, OpNum, NextValueNo,
4592
2
                                  FTy->getParamType(i)));
4593
2
        if (!Args.back())
4594
0
          return error("Invalid record");
4595
2
      }
4596
1
4597
1
      // Read type/value pairs for varargs params.
4598
1
      if (!FTy->isVarArg()) {
4599
1
        if (OpNum != Record.size())
4600
0
          return error("Invalid record");
4601
0
      } else {
4602
0
        while (OpNum != Record.size()) {
4603
0
          Value *Op;
4604
0
          if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4605
0
            return error("Invalid record");
4606
0
          Args.push_back(Op);
4607
0
        }
4608
0
      }
4609
1
4610
1
      I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args,
4611
1
                             OperandBundles);
4612
1
      FullTy = FullFTy->getReturnType();
4613
1
      OperandBundles.clear();
4614
1
      InstructionList.push_back(I);
4615
1
      cast<CallBrInst>(I)->setCallingConv(
4616
1
          static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
4617
1
      cast<CallBrInst>(I)->setAttributes(PAL);
4618
1
      break;
4619
1
    }
4620
1.58k
    case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
4621
1.58k
      I = new UnreachableInst(Context);
4622
1.58k
      InstructionList.push_back(I);
4623
1.58k
      break;
4624
311k
    case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
4625
311k
      if (Record.size() < 1 || ((Record.size()-1)&1))
4626
0
        return error("Invalid record");
4627
311k
      FullTy = getFullyStructuredTypeByID(Record[0]);
4628
311k
      Type *Ty = flattenPointerTypes(FullTy);
4629
311k
      if (!Ty)
4630
0
        return error("Invalid record");
4631
311k
4632
311k
      PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
4633
311k
      InstructionList.push_back(PN);
4634
311k
4635
1.12M
      for (unsigned i = 0, e = Record.size()-1; i != e; 
i += 2809k
) {
4636
809k
        Value *V;
4637
809k
        // With the new function encoding, it is possible that operands have
4638
809k
        // negative IDs (for forward references).  Use a signed VBR
4639
809k
        // representation to keep the encoding small.
4640
809k
        if (UseRelativeIDs)
4641
809k
          V = getValueSigned(Record, 1+i, NextValueNo, Ty);
4642
0
        else
4643
0
          V = getValue(Record, 1+i, NextValueNo, Ty);
4644
809k
        BasicBlock *BB = getBasicBlock(Record[2+i]);
4645
809k
        if (!V || !BB)
4646
0
          return error("Invalid record");
4647
809k
        PN->addIncoming(V, BB);
4648
809k
      }
4649
311k
      I = PN;
4650
311k
      break;
4651
311k
    }
4652
311k
4653
311k
    case bitc::FUNC_CODE_INST_LANDINGPAD:
4654
165
    case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
4655
165
      // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4656
165
      unsigned Idx = 0;
4657
165
      if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4658
157
        if (Record.size() < 3)
4659
0
          return error("Invalid record");
4660
8
      } else {
4661
8
        assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
4662
8
        if (Record.size() < 4)
4663
0
          return error("Invalid record");
4664
165
      }
4665
165
      FullTy = getFullyStructuredTypeByID(Record[Idx++]);
4666
165
      Type *Ty = flattenPointerTypes(FullTy);
4667
165
      if (!Ty)
4668
0
        return error("Invalid record");
4669
165
      if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4670
8
        Value *PersFn = nullptr;
4671
8
        if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4672
0
          return error("Invalid record");
4673
8
4674
8
        if (!F->hasPersonalityFn())
4675
5
          F->setPersonalityFn(cast<Constant>(PersFn));
4676
3
        else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4677
0
          return error("Personality function mismatch");
4678
165
      }
4679
165
4680
165
      bool IsCleanup = !!Record[Idx++];
4681
165
      unsigned NumClauses = Record[Idx++];
4682
165
      LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
4683
165
      LP->setCleanup(IsCleanup);
4684
222
      for (unsigned J = 0; J != NumClauses; 
++J57
) {
4685
57
        LandingPadInst::ClauseType CT =
4686
57
          LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4687
57
        Value *Val;
4688
57
4689
57
        if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4690
0
          delete LP;
4691
0
          return error("Invalid record");
4692
0
        }
4693
57
4694
57
        assert((CT != LandingPadInst::Catch ||
4695
57
                !isa<ArrayType>(Val->getType())) &&
4696
57
               "Catch clause has a invalid type!");
4697
57
        assert((CT != LandingPadInst::Filter ||
4698
57
                isa<ArrayType>(Val->getType())) &&
4699
57
               "Filter clause has invalid type!");
4700
57
        LP->addClause(cast<Constant>(Val));
4701
57
      }
4702
165
4703
165
      I = LP;
4704
165
      InstructionList.push_back(I);
4705
165
      break;
4706
165
    }
4707
165
4708
66.0k
    case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4709
66.0k
      if (Record.size() != 4)
4710
0
        return error("Invalid record");
4711
66.0k
      uint64_t AlignRecord = Record[3];
4712
66.0k
      const uint64_t InAllocaMask = uint64_t(1) << 5;
4713
66.0k
      const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
4714
66.0k
      const uint64_t SwiftErrorMask = uint64_t(1) << 7;
4715
66.0k
      const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask |
4716
66.0k
                                SwiftErrorMask;
4717
66.0k
      bool InAlloca = AlignRecord & InAllocaMask;
4718
66.0k
      bool SwiftError = AlignRecord & SwiftErrorMask;
4719
66.0k
      FullTy = getFullyStructuredTypeByID(Record[0]);
4720
66.0k
      Type *Ty = flattenPointerTypes(FullTy);
4721
66.0k
      if ((AlignRecord & ExplicitTypeMask) == 0) {
4722
50
        auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4723
50
        if (!PTy)
4724
0
          return error("Old-style alloca with a non-pointer type");
4725
50
        std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4726
50
      }
4727
66.0k
      Type *OpTy = getTypeByID(Record[1]);
4728
66.0k
      Value *Size = getFnValueByID(Record[2], OpTy);
4729
66.0k
      unsigned Align;
4730
66.0k
      if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
4731
0
        return Err;
4732
0
      }
4733
66.0k
      if (!Ty || !Size)
4734
0
        return error("Invalid record");
4735
66.0k
4736
66.0k
      // FIXME: Make this an optional field.
4737
66.0k
      const DataLayout &DL = TheModule->getDataLayout();
4738
66.0k
      unsigned AS = DL.getAllocaAddrSpace();
4739
66.0k
4740
66.0k
      AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align);
4741
66.0k
      AI->setUsedWithInAlloca(InAlloca);
4742
66.0k
      AI->setSwiftError(SwiftError);
4743
66.0k
      I = AI;
4744
66.0k
      FullTy = PointerType::get(FullTy, AS);
4745
66.0k
      InstructionList.push_back(I);
4746
66.0k
      break;
4747
66.0k
    }
4748
1.72M
    case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
4749
1.72M
      unsigned OpNum = 0;
4750
1.72M
      Value *Op;
4751
1.72M
      if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy) ||
4752
1.72M
          
(1.72M
OpNum + 2 != Record.size()1.72M
&&
OpNum + 3 != Record.size()1.72M
))
4753
1
        return error("Invalid record");
4754
1.72M
4755
1.72M
      if (!isa<PointerType>(Op->getType()))
4756
1
        return error("Load operand is not a pointer type");
4757
1.72M
4758
1.72M
      Type *Ty = nullptr;
4759
1.72M
      if (OpNum + 3 == Record.size()) {
4760
1.72M
        FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
4761
1.72M
        Ty = flattenPointerTypes(FullTy);
4762
1.72M
      } else
4763
263
        std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4764
1.72M
4765
1.72M
      if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4766
1
        return Err;
4767
1.72M
4768
1.72M
      unsigned Align;
4769
1.72M
      if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4770
1
        return Err;
4771
1.72M
      I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
4772
1.72M
      InstructionList.push_back(I);
4773
1.72M
      break;
4774
1.72M
    }
4775
1.72M
    case bitc::FUNC_CODE_INST_LOADATOMIC: {
4776
173
       // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4777
173
      unsigned OpNum = 0;
4778
173
      Value *Op;
4779
173
      if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy) ||
4780
173
          (OpNum + 4 != Record.size() && 
OpNum + 5 != Record.size()138
))
4781
0
        return error("Invalid record");
4782
173
4783
173
      if (!isa<PointerType>(Op->getType()))
4784
0
        return error("Load operand is not a pointer type");
4785
173
4786
173
      Type *Ty = nullptr;
4787
173
      if (OpNum + 5 == Record.size()) {
4788
138
        FullTy = getFullyStructuredTypeByID(Record[OpNum++]);
4789
138
        Ty = flattenPointerTypes(FullTy);
4790
138
      } else
4791
35
        std::tie(FullTy, Ty) = getPointerElementTypes(FullTy);
4792
173
4793
173
      if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4794
0
        return Err;
4795
173
4796
173
      AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4797
173
      if (Ordering == AtomicOrdering::NotAtomic ||
4798
173
          Ordering == AtomicOrdering::Release ||
4799
173
          Ordering == AtomicOrdering::AcquireRelease)
4800
0
        return error("Invalid record");
4801
173
      if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4802
0
        return error("Invalid record");
4803
173
      SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4804
173
4805
173
      unsigned Align;
4806
173
      if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4807
0
        return Err;
4808
173
      I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align, Ordering, SSID);
4809
173
      InstructionList.push_back(I);
4810
173
      break;
4811
173
    }
4812
597k
    case bitc::FUNC_CODE_INST_STORE:
4813
597k
    case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
4814
597k
      unsigned OpNum = 0;
4815
597k
      Value *Val, *Ptr;
4816
597k
      Type *FullTy;
4817
597k
      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
4818
597k
          (BitCode == bitc::FUNC_CODE_INST_STORE
4819
597k
               ? 
getValueTypePair(Record, OpNum, NextValueNo, Val)597k
4820
597k
               : popValue(Record, OpNum, NextValueNo,
4821
196
                          getPointerElementFlatType(FullTy), Val)) ||
4822
597k
          OpNum + 2 != Record.size())
4823
0
        return error("Invalid record");
4824
597k
4825
597k
      if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4826
0
        return Err;
4827
597k
      unsigned Align;
4828
597k
      if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4829
0
        return Err;
4830
597k
      I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
4831
597k
      InstructionList.push_back(I);
4832
597k
      break;
4833
597k
    }
4834
597k
    case bitc::FUNC_CODE_INST_STOREATOMIC:
4835
5.70k
    case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
4836
5.70k
      // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
4837
5.70k
      unsigned OpNum = 0;
4838
5.70k
      Value *Val, *Ptr;
4839
5.70k
      Type *FullTy;
4840
5.70k
      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
4841
5.70k
          !isa<PointerType>(Ptr->getType()) ||
4842
5.70k
          (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4843
5.70k
               ? 
getValueTypePair(Record, OpNum, NextValueNo, Val)5.66k
4844
5.70k
               : popValue(Record, OpNum, NextValueNo,
4845
35
                          getPointerElementFlatType(FullTy), Val)) ||
4846
5.70k
          
OpNum + 4 != Record.size()5.70k
)
4847
1
        return error("Invalid record");
4848
5.70k
4849
5.70k
      if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4850
0
        return Err;
4851
5.70k
      AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4852
5.70k
      if (Ordering == AtomicOrdering::NotAtomic ||
4853
5.70k
          Ordering == AtomicOrdering::Acquire ||
4854
5.70k
          Ordering == AtomicOrdering::AcquireRelease)
4855
0
        return error("Invalid record");
4856
5.70k
      SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4857
5.70k
      if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4858
0
        return error("Invalid record");
4859
5.70k
4860
5.70k
      unsigned Align;
4861
5.70k
      if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4862
0
        return Err;
4863
5.70k
      I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SSID);
4864
5.70k
      InstructionList.push_back(I);
4865
5.70k
      break;
4866
5.70k
    }
4867
5.70k
    case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
4868
4.48k
    case bitc::FUNC_CODE_INST_CMPXCHG: {
4869
4.48k
      // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, ssid,
4870
4.48k
      //          failureordering?, isweak?]
4871
4.48k
      unsigned OpNum = 0;
4872
4.48k
      Value *Ptr, *Cmp, *New;
4873
4.48k
      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy))
4874
0
        return error("Invalid record");
4875
4.48k
4876
4.48k
      if (!isa<PointerType>(Ptr->getType()))
4877
0
        return error("Cmpxchg operand is not a pointer type");
4878
4.48k
4879
4.48k
      if (BitCode == bitc::FUNC_CODE_INST_CMPXCHG) {
4880
4.42k
        if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, &FullTy))
4881
0
          return error("Invalid record");
4882
61
      } else if (popValue(Record, OpNum, NextValueNo,
4883
61
                          getPointerElementFlatType(FullTy), Cmp))
4884
0
        return error("Invalid record");
4885
61
      else
4886
61
        FullTy = cast<PointerType>(FullTy)->getElementType();
4887
4.48k
4888
4.48k
      if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
4889
4.48k
          Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
4890
0
        return error("Invalid record");
4891
4.48k
4892
4.48k
      AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
4893
4.48k
      if (SuccessOrdering == AtomicOrdering::NotAtomic ||
4894
4.48k
          SuccessOrdering == AtomicOrdering::Unordered)
4895
0
        return error("Invalid record");
4896
4.48k
      SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);
4897
4.48k
4898
4.48k
      if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
4899
0
        return Err;
4900
4.48k
      AtomicOrdering FailureOrdering;
4901
4.48k
      if (Record.size() < 7)
4902
50
        FailureOrdering =
4903
50
            AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
4904
4.43k
      else
4905
4.43k
        FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
4906
4.48k
4907
4.48k
      I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
4908
4.48k
                                SSID);
4909
4.48k
      FullTy = StructType::get(Context, {FullTy, Type::getInt1Ty(Context)});
4910
4.48k
      cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
4911
4.48k
4912
4.48k
      if (Record.size() < 8) {
4913
52
        // Before weak cmpxchgs existed, the instruction simply returned the
4914
52
        // value loaded from memory, so bitcode files from that era will be
4915
52
        // expecting the first component of a modern cmpxchg.
4916
52
        CurBB->getInstList().push_back(I);
4917
52
        I = ExtractValueInst::Create(I, 0);
4918
52
        FullTy = cast<StructType>(FullTy)->getElementType(0);
4919
4.43k
      } else {
4920
4.43k
        cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
4921
4.43k
      }
4922
4.48k
4923
4.48k
      InstructionList.push_back(I);
4924
4.48k
      break;
4925
4.48k
    }
4926
18.2k
    case bitc::FUNC_CODE_INST_ATOMICRMW: {
4927
18.2k
      // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid]
4928
18.2k
      unsigned OpNum = 0;
4929
18.2k
      Value *Ptr, *Val;
4930
18.2k
      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, &FullTy) ||
4931
18.2k
          !isa<PointerType>(Ptr->getType()) ||
4932
18.2k
          popValue(Record, OpNum, NextValueNo,
4933
18.2k
                   getPointerElementFlatType(FullTy), Val) ||
4934
18.2k
          
OpNum + 4 != Record.size()18.2k
)
4935
1
        return error("Invalid record");
4936
18.2k
      AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
4937
18.2k
      if (Operation < AtomicRMWInst::FIRST_BINOP ||
4938
18.2k
          Operation > AtomicRMWInst::LAST_BINOP)
4939
0
        return error("Invalid record");
4940
18.2k
      AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4941
18.2k
      if (Ordering == AtomicOrdering::NotAtomic ||
4942
18.2k
          Ordering == AtomicOrdering::Unordered)
4943
0
        return error("Invalid record");
4944
18.2k
      SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4945
18.2k
      I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
4946
18.2k
      FullTy = getPointerElementFlatType(FullTy);
4947
18.2k
      cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
4948
18.2k
      InstructionList.push_back(I);
4949
18.2k
      break;
4950
18.2k
    }
4951
18.2k
    case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid]
4952
1.46k
      if (2 != Record.size())
4953
0
        return error("Invalid record");
4954
1.46k
      AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
4955
1.46k
      if (Ordering == AtomicOrdering::NotAtomic ||
4956
1.46k
          Ordering == AtomicOrdering::Unordered ||
4957
1.46k
          Ordering == AtomicOrdering::Monotonic)
4958
0
        return error("Invalid record");
4959
1.46k
      SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]);
4960
1.46k
      I = new FenceInst(Context, Ordering, SSID);
4961
1.46k
      InstructionList.push_back(I);
4962
1.46k
      break;
4963
1.46k
    }
4964
1.20M
    case bitc::FUNC_CODE_INST_CALL: {
4965
1.20M
      // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
4966
1.20M
      if (Record.size() < 3)
4967
0
        return error("Invalid record");
4968
1.20M
4969
1.20M
      unsigned OpNum = 0;
4970
1.20M
      AttributeList PAL = getAttributes(Record[OpNum++]);
4971
1.20M
      unsigned CCInfo = Record[OpNum++];
4972
1.20M
4973
1.20M
      FastMathFlags FMF;
4974
1.20M
      if ((CCInfo >> bitc::CALL_FMF) & 1) {
4975
38
        FMF = getDecodedFastMathFlags(Record[OpNum++]);
4976
38
        if (!FMF.any())
4977
0
          return error("Fast math flags indicator set for call with no FMF");
4978
1.20M
      }
4979
1.20M
4980
1.20M
      FunctionType *FTy = nullptr;
4981
1.20M
      FunctionType *FullFTy = nullptr;
4982
1.20M
      if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) {
4983
1.19M
        FullFTy =
4984
1.19M
            dyn_cast<FunctionType>(getFullyStructuredTypeByID(Record[OpNum++]));
4985