Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MIRParser/MIParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MIParser.cpp - Machine instructions parser 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
// This file implements the parsing of machine instructions.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/CodeGen/MIRParser/MIParser.h"
14
#include "MILexer.h"
15
#include "llvm/ADT/APInt.h"
16
#include "llvm/ADT/APSInt.h"
17
#include "llvm/ADT/ArrayRef.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/None.h"
20
#include "llvm/ADT/Optional.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/ADT/StringMap.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/ADT/StringSwitch.h"
25
#include "llvm/ADT/Twine.h"
26
#include "llvm/Analysis/MemoryLocation.h"
27
#include "llvm/AsmParser/Parser.h"
28
#include "llvm/AsmParser/SlotMapping.h"
29
#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
30
#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
31
#include "llvm/CodeGen/MIRPrinter.h"
32
#include "llvm/CodeGen/MachineBasicBlock.h"
33
#include "llvm/CodeGen/MachineFrameInfo.h"
34
#include "llvm/CodeGen/MachineFunction.h"
35
#include "llvm/CodeGen/MachineInstr.h"
36
#include "llvm/CodeGen/MachineInstrBuilder.h"
37
#include "llvm/CodeGen/MachineMemOperand.h"
38
#include "llvm/CodeGen/MachineOperand.h"
39
#include "llvm/CodeGen/MachineRegisterInfo.h"
40
#include "llvm/CodeGen/TargetInstrInfo.h"
41
#include "llvm/CodeGen/TargetRegisterInfo.h"
42
#include "llvm/CodeGen/TargetSubtargetInfo.h"
43
#include "llvm/IR/BasicBlock.h"
44
#include "llvm/IR/Constants.h"
45
#include "llvm/IR/DataLayout.h"
46
#include "llvm/IR/DebugInfoMetadata.h"
47
#include "llvm/IR/DebugLoc.h"
48
#include "llvm/IR/Function.h"
49
#include "llvm/IR/InstrTypes.h"
50
#include "llvm/IR/Instructions.h"
51
#include "llvm/IR/Intrinsics.h"
52
#include "llvm/IR/Metadata.h"
53
#include "llvm/IR/Module.h"
54
#include "llvm/IR/ModuleSlotTracker.h"
55
#include "llvm/IR/Type.h"
56
#include "llvm/IR/Value.h"
57
#include "llvm/IR/ValueSymbolTable.h"
58
#include "llvm/MC/LaneBitmask.h"
59
#include "llvm/MC/MCContext.h"
60
#include "llvm/MC/MCDwarf.h"
61
#include "llvm/MC/MCInstrDesc.h"
62
#include "llvm/MC/MCRegisterInfo.h"
63
#include "llvm/Support/AtomicOrdering.h"
64
#include "llvm/Support/BranchProbability.h"
65
#include "llvm/Support/Casting.h"
66
#include "llvm/Support/ErrorHandling.h"
67
#include "llvm/Support/LowLevelTypeImpl.h"
68
#include "llvm/Support/MemoryBuffer.h"
69
#include "llvm/Support/SMLoc.h"
70
#include "llvm/Support/SourceMgr.h"
71
#include "llvm/Support/raw_ostream.h"
72
#include "llvm/Target/TargetIntrinsicInfo.h"
73
#include "llvm/Target/TargetMachine.h"
74
#include <algorithm>
75
#include <cassert>
76
#include <cctype>
77
#include <cstddef>
78
#include <cstdint>
79
#include <limits>
80
#include <string>
81
#include <utility>
82
83
using namespace llvm;
84
85
void PerTargetMIParsingState::setTarget(
86
9.06k
  const TargetSubtargetInfo &NewSubtarget) {
87
9.06k
88
9.06k
  // If the subtarget changed, over conservatively assume everything is invalid.
89
9.06k
  if (&Subtarget == &NewSubtarget)
90
8.89k
    return;
91
172
92
172
  Names2InstrOpCodes.clear();
93
172
  Names2Regs.clear();
94
172
  Names2RegMasks.clear();
95
172
  Names2SubRegIndices.clear();
96
172
  Names2TargetIndices.clear();
97
172
  Names2DirectTargetFlags.clear();
98
172
  Names2BitmaskTargetFlags.clear();
99
172
  Names2MMOTargetFlags.clear();
100
172
101
172
  initNames2RegClasses();
102
172
  initNames2RegBanks();
103
172
}
104
105
117k
void PerTargetMIParsingState::initNames2Regs() {
106
117k
  if (!Names2Regs.empty())
107
114k
    return;
108
2.29k
109
2.29k
  // The '%noreg' register is the register 0.
110
2.29k
  Names2Regs.insert(std::make_pair("noreg", 0));
111
2.29k
  const auto *TRI = Subtarget.getRegisterInfo();
112
2.29k
  assert(TRI && "Expected target register info");
113
2.29k
114
3.52M
  for (unsigned I = 0, E = TRI->getNumRegs(); I < E; 
++I3.52M
) {
115
3.52M
    bool WasInserted =
116
3.52M
        Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
117
3.52M
            .second;
118
3.52M
    (void)WasInserted;
119
3.52M
    assert(WasInserted && "Expected registers to be unique case-insensitively");
120
3.52M
  }
121
2.29k
}
122
123
bool PerTargetMIParsingState::getRegisterByName(StringRef RegName,
124
117k
                                                unsigned &Reg) {
125
117k
  initNames2Regs();
126
117k
  auto RegInfo = Names2Regs.find(RegName);
127
117k
  if (RegInfo == Names2Regs.end())
128
3
    return true;
129
117k
  Reg = RegInfo->getValue();
130
117k
  return false;
131
117k
}
132
133
71.8k
void PerTargetMIParsingState::initNames2InstrOpCodes() {
134
71.8k
  if (!Names2InstrOpCodes.empty())
135
69.5k
    return;
136
2.33k
  const auto *TII = Subtarget.getInstrInfo();
137
2.33k
  assert(TII && "Expected target instruction info");
138
24.0M
  for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; 
++I24.0M
)
139
24.0M
    Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
140
2.33k
}
141
142
bool PerTargetMIParsingState::parseInstrName(StringRef InstrName,
143
71.8k
                                             unsigned &OpCode) {
144
71.8k
  initNames2InstrOpCodes();
145
71.8k
  auto InstrInfo = Names2InstrOpCodes.find(InstrName);
146
71.8k
  if (InstrInfo == Names2InstrOpCodes.end())
147
1
    return true;
148
71.8k
  OpCode = InstrInfo->getValue();
149
71.8k
  return false;
150
71.8k
}
151
152
1.79k
void PerTargetMIParsingState::initNames2RegMasks() {
153
1.79k
  if (!Names2RegMasks.empty())
154
1.38k
    return;
155
402
  const auto *TRI = Subtarget.getRegisterInfo();
156
402
  assert(TRI && "Expected target register info");
157
402
  ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
158
402
  ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
159
402
  assert(RegMasks.size() == RegMaskNames.size());
160
7.81k
  for (size_t I = 0, E = RegMasks.size(); I < E; 
++I7.40k
)
161
7.40k
    Names2RegMasks.insert(
162
7.40k
        std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
163
402
}
164
165
1.79k
const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier) {
166
1.79k
  initNames2RegMasks();
167
1.79k
  auto RegMaskInfo = Names2RegMasks.find(Identifier);
168
1.79k
  if (RegMaskInfo == Names2RegMasks.end())
169
1.45k
    return nullptr;
170
338
  return RegMaskInfo->getValue();
171
338
}
172
173
1.89k
void PerTargetMIParsingState::initNames2SubRegIndices() {
174
1.89k
  if (!Names2SubRegIndices.empty())
175
1.74k
    return;
176
146
  const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
177
15.0k
  for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; 
++I14.9k
)
178
14.9k
    Names2SubRegIndices.insert(
179
14.9k
        std::make_pair(TRI->getSubRegIndexName(I), I));
180
146
}
181
182
1.89k
unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name) {
183
1.89k
  initNames2SubRegIndices();
184
1.89k
  auto SubRegInfo = Names2SubRegIndices.find(Name);
185
1.89k
  if (SubRegInfo == Names2SubRegIndices.end())
186
2
    return 0;
187
1.89k
  return SubRegInfo->getValue();
188
1.89k
}
189
190
3
void PerTargetMIParsingState::initNames2TargetIndices() {
191
3
  if (!Names2TargetIndices.empty())
192
1
    return;
193
2
  const auto *TII = Subtarget.getInstrInfo();
194
2
  assert(TII && "Expected target instruction info");
195
2
  auto Indices = TII->getSerializableTargetIndices();
196
2
  for (const auto &I : Indices)
197
10
    Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
198
2
}
199
200
3
bool PerTargetMIParsingState::getTargetIndex(StringRef Name, int &Index) {
201
3
  initNames2TargetIndices();
202
3
  auto IndexInfo = Names2TargetIndices.find(Name);
203
3
  if (IndexInfo == Names2TargetIndices.end())
204
1
    return true;
205
2
  Index = IndexInfo->second;
206
2
  return false;
207
2
}
208
209
497
void PerTargetMIParsingState::initNames2DirectTargetFlags() {
210
497
  if (!Names2DirectTargetFlags.empty())
211
443
    return;
212
54
213
54
  const auto *TII = Subtarget.getInstrInfo();
214
54
  assert(TII && "Expected target instruction info");
215
54
  auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
216
54
  for (const auto &I : Flags)
217
589
    Names2DirectTargetFlags.insert(
218
589
        std::make_pair(StringRef(I.second), I.first));
219
54
}
220
221
bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name,
222
497
                                                  unsigned &Flag) {
223
497
  initNames2DirectTargetFlags();
224
497
  auto FlagInfo = Names2DirectTargetFlags.find(Name);
225
497
  if (FlagInfo == Names2DirectTargetFlags.end())
226
4
    return true;
227
493
  Flag = FlagInfo->second;
228
493
  return false;
229
493
}
230
231
71
void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
232
71
  if (!Names2BitmaskTargetFlags.empty())
233
55
    return;
234
16
235
16
  const auto *TII = Subtarget.getInstrInfo();
236
16
  assert(TII && "Expected target instruction info");
237
16
  auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
238
16
  for (const auto &I : Flags)
239
85
    Names2BitmaskTargetFlags.insert(
240
85
        std::make_pair(StringRef(I.second), I.first));
241
16
}
242
243
bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name,
244
71
                                                   unsigned &Flag) {
245
71
  initNames2BitmaskTargetFlags();
246
71
  auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
247
71
  if (FlagInfo == Names2BitmaskTargetFlags.end())
248
2
    return true;
249
69
  Flag = FlagInfo->second;
250
69
  return false;
251
69
}
252
253
24
void PerTargetMIParsingState::initNames2MMOTargetFlags() {
254
24
  if (!Names2MMOTargetFlags.empty())
255
21
    return;
256
3
257
3
  const auto *TII = Subtarget.getInstrInfo();
258
3
  assert(TII && "Expected target instruction info");
259
3
  auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
260
3
  for (const auto &I : Flags)
261
6
    Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
262
3
}
263
264
bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name,
265
24
                                               MachineMemOperand::Flags &Flag) {
266
24
  initNames2MMOTargetFlags();
267
24
  auto FlagInfo = Names2MMOTargetFlags.find(Name);
268
24
  if (FlagInfo == Names2MMOTargetFlags.end())
269
1
    return true;
270
23
  Flag = FlagInfo->second;
271
23
  return false;
272
23
}
273
274
2.40k
void PerTargetMIParsingState::initNames2RegClasses() {
275
2.40k
  if (!Names2RegClasses.empty())
276
172
    return;
277
2.23k
278
2.23k
  const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
279
234k
  for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; 
++I232k
) {
280
232k
    const auto *RC = TRI->getRegClass(I);
281
232k
    Names2RegClasses.insert(
282
232k
        std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
283
232k
  }
284
2.23k
}
285
286
2.40k
void PerTargetMIParsingState::initNames2RegBanks() {
287
2.40k
  if (!Names2RegBanks.empty())
288
156
    return;
289
2.24k
290
2.24k
  const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
291
2.24k
  // If the target does not support GlobalISel, we may not have a
292
2.24k
  // register bank info.
293
2.24k
  if (!RBI)
294
181
    return;
295
2.06k
296
8.19k
  
for (unsigned I = 0, E = RBI->getNumRegBanks(); 2.06k
I < E;
++I6.12k
) {
297
6.12k
    const auto &RegBank = RBI->getRegBank(I);
298
6.12k
    Names2RegBanks.insert(
299
6.12k
        std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
300
6.12k
  }
301
2.06k
}
302
303
const TargetRegisterClass *
304
40.5k
PerTargetMIParsingState::getRegClass(StringRef Name) {
305
40.5k
  auto RegClassInfo = Names2RegClasses.find(Name);
306
40.5k
  if (RegClassInfo == Names2RegClasses.end())
307
29.4k
    return nullptr;
308
11.1k
  return RegClassInfo->getValue();
309
11.1k
}
310
311
13.0k
const RegisterBank *PerTargetMIParsingState::getRegBank(StringRef Name) {
312
13.0k
  auto RegBankInfo = Names2RegBanks.find(Name);
313
13.0k
  if (RegBankInfo == Names2RegBanks.end())
314
1
    return nullptr;
315
13.0k
  return RegBankInfo->getValue();
316
13.0k
}
317
318
PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
319
    SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T)
320
11.2k
  : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) {
321
11.2k
}
322
323
101k
VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
324
101k
  auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
325
101k
  if (I.second) {
326
41.8k
    MachineRegisterInfo &MRI = MF.getRegInfo();
327
41.8k
    VRegInfo *Info = new (Allocator) VRegInfo;
328
41.8k
    Info->VReg = MRI.createIncompleteVirtualRegister();
329
41.8k
    I.first->second = Info;
330
41.8k
  }
331
101k
  return *I.first->second;
332
101k
}
333
334
57
VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
335
57
  assert(RegName != "" && "Expected named reg.");
336
57
337
57
  auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
338
57
  if (I.second) {
339
27
    VRegInfo *Info = new (Allocator) VRegInfo;
340
27
    Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
341
27
    I.first->second = Info;
342
27
  }
343
57
  return *I.first->second;
344
57
}
345
346
namespace {
347
348
/// A wrapper struct around the 'MachineOperand' struct that includes a source
349
/// range and other attributes.
350
struct ParsedMachineOperand {
351
  MachineOperand Operand;
352
  StringRef::iterator Begin;
353
  StringRef::iterator End;
354
  Optional<unsigned> TiedDefIdx;
355
356
  ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
357
                       StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
358
195k
      : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
359
195k
    if (TiedDefIdx)
360
195k
      assert(Operand.isReg() && Operand.isUse() &&
361
195k
             "Only used register operands can be tied");
362
195k
  }
363
};
364
365
class MIParser {
366
  MachineFunction &MF;
367
  SMDiagnostic &Error;
368
  StringRef Source, CurrentSource;
369
  MIToken Token;
370
  PerFunctionMIParsingState &PFS;
371
  /// Maps from slot numbers to function's unnamed basic blocks.
372
  DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
373
  /// Maps from slot numbers to function's unnamed values.
374
  DenseMap<unsigned, const Value *> Slots2Values;
375
376
public:
377
  MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
378
           StringRef Source);
379
380
  /// \p SkipChar gives the number of characters to skip before looking
381
  /// for the next token.
382
  void lex(unsigned SkipChar = 0);
383
384
  /// Report an error at the current location with the given message.
385
  ///
386
  /// This function always return true.
387
  bool error(const Twine &Msg);
388
389
  /// Report an error at the given location with the given message.
390
  ///
391
  /// This function always return true.
392
  bool error(StringRef::iterator Loc, const Twine &Msg);
393
394
  bool
395
  parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
396
  bool parseBasicBlocks();
397
  bool parse(MachineInstr *&MI);
398
  bool parseStandaloneMBB(MachineBasicBlock *&MBB);
399
  bool parseStandaloneNamedRegister(unsigned &Reg);
400
  bool parseStandaloneVirtualRegister(VRegInfo *&Info);
401
  bool parseStandaloneRegister(unsigned &Reg);
402
  bool parseStandaloneStackObject(int &FI);
403
  bool parseStandaloneMDNode(MDNode *&Node);
404
405
  bool
406
  parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
407
  bool parseBasicBlock(MachineBasicBlock &MBB,
408
                       MachineBasicBlock *&AddFalthroughFrom);
409
  bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
410
  bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
411
412
  bool parseNamedRegister(unsigned &Reg);
413
  bool parseVirtualRegister(VRegInfo *&Info);
414
  bool parseNamedVirtualRegister(VRegInfo *&Info);
415
  bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
416
  bool parseRegisterFlag(unsigned &Flags);
417
  bool parseRegisterClassOrBank(VRegInfo &RegInfo);
418
  bool parseSubRegisterIndex(unsigned &SubReg);
419
  bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
420
  bool parseRegisterOperand(MachineOperand &Dest,
421
                            Optional<unsigned> &TiedDefIdx, bool IsDef = false);
422
  bool parseImmediateOperand(MachineOperand &Dest);
423
  bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
424
                       const Constant *&C);
425
  bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
426
  bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
427
  bool parseTypedImmediateOperand(MachineOperand &Dest);
428
  bool parseFPImmediateOperand(MachineOperand &Dest);
429
  bool parseMBBReference(MachineBasicBlock *&MBB);
430
  bool parseMBBOperand(MachineOperand &Dest);
431
  bool parseStackFrameIndex(int &FI);
432
  bool parseStackObjectOperand(MachineOperand &Dest);
433
  bool parseFixedStackFrameIndex(int &FI);
434
  bool parseFixedStackObjectOperand(MachineOperand &Dest);
435
  bool parseGlobalValue(GlobalValue *&GV);
436
  bool parseGlobalAddressOperand(MachineOperand &Dest);
437
  bool parseConstantPoolIndexOperand(MachineOperand &Dest);
438
  bool parseSubRegisterIndexOperand(MachineOperand &Dest);
439
  bool parseJumpTableIndexOperand(MachineOperand &Dest);
440
  bool parseExternalSymbolOperand(MachineOperand &Dest);
441
  bool parseMCSymbolOperand(MachineOperand &Dest);
442
  bool parseMDNode(MDNode *&Node);
443
  bool parseDIExpression(MDNode *&Expr);
444
  bool parseDILocation(MDNode *&Expr);
445
  bool parseMetadataOperand(MachineOperand &Dest);
446
  bool parseCFIOffset(int &Offset);
447
  bool parseCFIRegister(unsigned &Reg);
448
  bool parseCFIEscapeValues(std::string& Values);
449
  bool parseCFIOperand(MachineOperand &Dest);
450
  bool parseIRBlock(BasicBlock *&BB, const Function &F);
451
  bool parseBlockAddressOperand(MachineOperand &Dest);
452
  bool parseIntrinsicOperand(MachineOperand &Dest);
453
  bool parsePredicateOperand(MachineOperand &Dest);
454
  bool parseTargetIndexOperand(MachineOperand &Dest);
455
  bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
456
  bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
457
  bool parseMachineOperand(MachineOperand &Dest,
458
                           Optional<unsigned> &TiedDefIdx);
459
  bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
460
                                         Optional<unsigned> &TiedDefIdx);
461
  bool parseOffset(int64_t &Offset);
462
  bool parseAlignment(unsigned &Alignment);
463
  bool parseAddrspace(unsigned &Addrspace);
464
  bool parseOperandsOffset(MachineOperand &Op);
465
  bool parseIRValue(const Value *&V);
466
  bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
467
  bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
468
  bool parseMachinePointerInfo(MachinePointerInfo &Dest);
469
  bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
470
  bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
471
  bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
472
  bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
473
474
private:
475
  /// Convert the integer literal in the current token into an unsigned integer.
476
  ///
477
  /// Return true if an error occurred.
478
  bool getUnsigned(unsigned &Result);
479
480
  /// Convert the integer literal in the current token into an uint64.
481
  ///
482
  /// Return true if an error occurred.
483
  bool getUint64(uint64_t &Result);
484
485
  /// Convert the hexadecimal literal in the current token into an unsigned
486
  ///  APInt with a minimum bitwidth required to represent the value.
487
  ///
488
  /// Return true if the literal does not represent an integer value.
489
  bool getHexUint(APInt &Result);
490
491
  /// If the current token is of the given kind, consume it and return false.
492
  /// Otherwise report an error and return true.
493
  bool expectAndConsume(MIToken::TokenKind TokenKind);
494
495
  /// If the current token is of the given kind, consume it and return true.
496
  /// Otherwise return false.
497
  bool consumeIfPresent(MIToken::TokenKind TokenKind);
498
499
  bool parseInstruction(unsigned &OpCode, unsigned &Flags);
500
501
  bool assignRegisterTies(MachineInstr &MI,
502
                          ArrayRef<ParsedMachineOperand> Operands);
503
504
  bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
505
                              const MCInstrDesc &MCID);
506
507
  const BasicBlock *getIRBlock(unsigned Slot);
508
  const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
509
510
  const Value *getIRValue(unsigned Slot);
511
512
  /// Get or create an MCSymbol for a given name.
513
  MCSymbol *getOrCreateMCSymbol(StringRef Name);
514
515
  /// parseStringConstant
516
  ///   ::= StringConstant
517
  bool parseStringConstant(std::string &Result);
518
};
519
520
} // end anonymous namespace
521
522
MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
523
                   StringRef Source)
524
    : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
525
49.8k
{}
526
527
2.16M
void MIParser::lex(unsigned SkipChar) {
528
2.16M
  CurrentSource = lexMIToken(
529
2.16M
      CurrentSource.data() + SkipChar, Token,
530
2.16M
      [this](StringRef::iterator Loc, const Twine &Msg) 
{ error(Loc, Msg); }4
);
531
2.16M
}
532
533
97
bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
534
535
117
bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
536
117
  const SourceMgr &SM = *PFS.SM;
537
117
  assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
538
117
  const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
539
117
  if (Loc >= Buffer.getBufferStart() && 
Loc <= Buffer.getBufferEnd()115
) {
540
106
    // Create an ordinary diagnostic when the source manager's buffer is the
541
106
    // source string.
542
106
    Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
543
106
    return true;
544
106
  }
545
11
  // Create a diagnostic for a YAML string literal.
546
11
  Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
547
11
                       Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
548
11
                       Source, None, None);
549
11
  return true;
550
11
}
551
552
2
static const char *toString(MIToken::TokenKind TokenKind) {
553
2
  switch (TokenKind) {
554
2
  case MIToken::comma:
555
1
    return "','";
556
2
  case MIToken::equal:
557
0
    return "'='";
558
2
  case MIToken::colon:
559
1
    return "':'";
560
2
  case MIToken::lparen:
561
0
    return "'('";
562
2
  case MIToken::rparen:
563
0
    return "')'";
564
2
  default:
565
0
    return "<unknown token>";
566
2
  }
567
2
}
568
569
142k
bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
570
142k
  if (Token.isNot(TokenKind))
571
2
    return error(Twine("expected ") + toString(TokenKind));
572
142k
  lex();
573
142k
  return false;
574
142k
}
575
576
1.56M
bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
577
1.56M
  if (Token.isNot(TokenKind))
578
1.11M
    return false;
579
456k
  lex();
580
456k
  return true;
581
456k
}
582
583
bool MIParser::parseBasicBlockDefinition(
584
14.4k
    DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
585
14.4k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
586
14.4k
  unsigned ID = 0;
587
14.4k
  if (getUnsigned(ID))
588
0
    return true;
589
14.4k
  auto Loc = Token.location();
590
14.4k
  auto Name = Token.stringValue();
591
14.4k
  lex();
592
14.4k
  bool HasAddressTaken = false;
593
14.4k
  bool IsLandingPad = false;
594
14.4k
  unsigned Alignment = 0;
595
14.4k
  BasicBlock *BB = nullptr;
596
14.4k
  if (consumeIfPresent(MIToken::lparen)) {
597
1.43k
    do {
598
1.43k
      // TODO: Report an error when multiple same attributes are specified.
599
1.43k
      switch (Token.kind()) {
600
1.43k
      case MIToken::kw_address_taken:
601
20
        HasAddressTaken = true;
602
20
        lex();
603
20
        break;
604
1.43k
      case MIToken::kw_landing_pad:
605
11
        IsLandingPad = true;
606
11
        lex();
607
11
        break;
608
1.43k
      case MIToken::kw_align:
609
14
        if (parseAlignment(Alignment))
610
0
          return true;
611
14
        break;
612
1.38k
      case MIToken::IRBlock:
613
1.38k
        // TODO: Report an error when both name and ir block are specified.
614
1.38k
        if (parseIRBlock(BB, MF.getFunction()))
615
1
          return true;
616
1.38k
        lex();
617
1.38k
        break;
618
1.38k
      default:
619
6
        break;
620
1.43k
      }
621
1.43k
    } while (consumeIfPresent(MIToken::comma));
622
1.43k
    
if (1.43k
expectAndConsume(MIToken::rparen)1.43k
)
623
0
      return true;
624
14.4k
  }
625
14.4k
  if (expectAndConsume(MIToken::colon))
626
1
    return true;
627
14.4k
628
14.4k
  if (!Name.empty()) {
629
3.13k
    BB = dyn_cast_or_null<BasicBlock>(
630
3.13k
        MF.getFunction().getValueSymbolTable()->lookup(Name));
631
3.13k
    if (!BB)
632
1
      return error(Loc, Twine("basic block '") + Name +
633
1
                            "' is not defined in the function '" +
634
1
                            MF.getName() + "'");
635
14.4k
  }
636
14.4k
  auto *MBB = MF.CreateMachineBasicBlock(BB);
637
14.4k
  MF.insert(MF.end(), MBB);
638
14.4k
  bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
639
14.4k
  if (!WasInserted)
640
1
    return error(Loc, Twine("redefinition of machine basic block with id #") +
641
1
                          Twine(ID));
642
14.4k
  if (Alignment)
643
14
    MBB->setAlignment(Alignment);
644
14.4k
  if (HasAddressTaken)
645
20
    MBB->setHasAddressTaken();
646
14.4k
  MBB->setIsEHPad(IsLandingPad);
647
14.4k
  return false;
648
14.4k
}
649
650
bool MIParser::parseBasicBlockDefinitions(
651
11.2k
    DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
652
11.2k
  lex();
653
11.2k
  // Skip until the first machine basic block.
654
27.5k
  while (Token.is(MIToken::Newline))
655
16.2k
    lex();
656
11.2k
  if (Token.isErrorOrEOF())
657
13
    return Token.isError();
658
11.2k
  if (Token.isNot(MIToken::MachineBasicBlockLabel))
659
1
    return error("expected a basic block definition before instructions");
660
11.2k
  unsigned BraceDepth = 0;
661
14.4k
  do {
662
14.4k
    if (parseBasicBlockDefinition(MBBSlots))
663
4
      return true;
664
14.4k
    bool IsAfterNewline = false;
665
14.4k
    // Skip until the next machine basic block.
666
1.01M
    while (true) {
667
1.01M
      if ((Token.is(MIToken::MachineBasicBlockLabel) && 
IsAfterNewline3.18k
) ||
668
1.01M
          
Token.isErrorOrEOF()1.00M
)
669
14.4k
        break;
670
997k
      else if (Token.is(MIToken::MachineBasicBlockLabel))
671
1
        return error("basic block definition should be located at the start of "
672
1
                     "the line");
673
997k
      else if (consumeIfPresent(MIToken::Newline)) {
674
232k
        IsAfterNewline = true;
675
232k
        continue;
676
232k
      }
677
765k
      IsAfterNewline = false;
678
765k
      if (Token.is(MIToken::lbrace))
679
71
        ++BraceDepth;
680
765k
      if (Token.is(MIToken::rbrace)) {
681
71
        if (!BraceDepth)
682
1
          return error("extraneous closing brace ('}')");
683
70
        --BraceDepth;
684
70
      }
685
765k
      lex();
686
765k
    }
687
14.4k
    // Verify that we closed all of the '{' at the end of a file or a block.
688
14.4k
    
if (14.4k
!Token.isError()14.4k
&&
BraceDepth14.4k
)
689
1
      return error("expected '}'"); // FIXME: Report a note that shows '{'.
690
14.4k
  } while (!Token.isErrorOrEOF());
691
11.2k
  
return Token.isError()11.2k
;
692
11.2k
}
693
694
9.80k
bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
695
9.80k
  assert(Token.is(MIToken::kw_liveins));
696
9.80k
  lex();
697
9.80k
  if (expectAndConsume(MIToken::colon))
698
0
    return true;
699
9.80k
  if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
700
24
    return false;
701
19.4k
  
do 9.77k
{
702
19.4k
    if (Token.isNot(MIToken::NamedRegister))
703
1
      return error("expected a named register");
704
19.4k
    unsigned Reg = 0;
705
19.4k
    if (parseNamedRegister(Reg))
706
0
      return true;
707
19.4k
    lex();
708
19.4k
    LaneBitmask Mask = LaneBitmask::getAll();
709
19.4k
    if (consumeIfPresent(MIToken::colon)) {
710
46
      // Parse lane mask.
711
46
      if (Token.isNot(MIToken::IntegerLiteral) &&
712
46
          
Token.isNot(MIToken::HexLiteral)45
)
713
0
        return error("expected a lane mask");
714
46
      static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
715
46
                    "Use correct get-function for lane mask");
716
46
      LaneBitmask::Type V;
717
46
      if (getUnsigned(V))
718
0
        return error("invalid lane mask value");
719
46
      Mask = LaneBitmask(V);
720
46
      lex();
721
46
    }
722
19.4k
    MBB.addLiveIn(Reg, Mask);
723
19.4k
  } while (consumeIfPresent(MIToken::comma));
724
9.77k
  
return false9.77k
;
725
9.77k
}
726
727
1.70k
bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
728
1.70k
  assert(Token.is(MIToken::kw_successors));
729
1.70k
  lex();
730
1.70k
  if (expectAndConsume(MIToken::colon))
731
0
    return true;
732
1.70k
  if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
733
38
    return false;
734
2.57k
  
do 1.66k
{
735
2.57k
    if (Token.isNot(MIToken::MachineBasicBlock))
736
1
      return error("expected a machine basic block reference");
737
2.57k
    MachineBasicBlock *SuccMBB = nullptr;
738
2.57k
    if (parseMBBReference(SuccMBB))
739
0
      return true;
740
2.57k
    lex();
741
2.57k
    unsigned Weight = 0;
742
2.57k
    if (consumeIfPresent(MIToken::lparen)) {
743
1.37k
      if (Token.isNot(MIToken::IntegerLiteral) &&
744
1.37k
          
Token.isNot(MIToken::HexLiteral)1.34k
)
745
1
        return error("expected an integer literal after '('");
746
1.37k
      if (getUnsigned(Weight))
747
0
        return true;
748
1.37k
      lex();
749
1.37k
      if (expectAndConsume(MIToken::rparen))
750
0
        return true;
751
2.57k
    }
752
2.57k
    MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
753
2.57k
  } while (consumeIfPresent(MIToken::comma));
754
1.66k
  MBB.normalizeSuccProbs();
755
1.66k
  return false;
756
1.66k
}
757
758
bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
759
14.3k
                               MachineBasicBlock *&AddFalthroughFrom) {
760
14.3k
  // Skip the definition.
761
14.3k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
762
14.3k
  lex();
763
14.3k
  if (consumeIfPresent(MIToken::lparen)) {
764
2.87k
    while (Token.isNot(MIToken::rparen) && 
!Token.isErrorOrEOF()1.44k
)
765
1.44k
      lex();
766
1.43k
    consumeIfPresent(MIToken::rparen);
767
1.43k
  }
768
14.3k
  consumeIfPresent(MIToken::colon);
769
14.3k
770
14.3k
  // Parse the liveins and successors.
771
14.3k
  // N.B: Multiple lists of successors and liveins are allowed and they're
772
14.3k
  // merged into one.
773
14.3k
  // Example:
774
14.3k
  //   liveins: %edi
775
14.3k
  //   liveins: %esi
776
14.3k
  //
777
14.3k
  // is equivalent to
778
14.3k
  //   liveins: %edi, %esi
779
14.3k
  bool ExplicitSuccessors = false;
780
154k
  while (true) {
781
154k
    if (Token.is(MIToken::kw_successors)) {
782
1.70k
      if (parseBasicBlockSuccessors(MBB))
783
2
        return true;
784
1.69k
      ExplicitSuccessors = true;
785
152k
    } else if (Token.is(MIToken::kw_liveins)) {
786
9.80k
      if (parseBasicBlockLiveins(MBB))
787
1
        return true;
788
142k
    } else if (consumeIfPresent(MIToken::Newline)) {
789
128k
      continue;
790
128k
    } else
791
14.3k
      break;
792
11.5k
    if (!Token.isNewlineOrEOF())
793
1
      return error("expected line break at the end of a list");
794
11.4k
    lex();
795
11.4k
  }
796
14.3k
797
14.3k
  // Parse the instructions.
798
14.3k
  bool IsInBundle = false;
799
14.3k
  MachineInstr *PrevMI = nullptr;
800
106k
  while (!Token.is(MIToken::MachineBasicBlockLabel) &&
801
106k
         
!Token.is(MIToken::Eof)103k
) {
802
92.2k
    if (consumeIfPresent(MIToken::Newline))
803
20.3k
      continue;
804
71.9k
    if (consumeIfPresent(MIToken::rbrace)) {
805
68
      // The first parsing pass should verify that all closing '}' have an
806
68
      // opening '{'.
807
68
      assert(IsInBundle);
808
68
      IsInBundle = false;
809
68
      continue;
810
68
    }
811
71.8k
    MachineInstr *MI = nullptr;
812
71.8k
    if (parse(MI))
813
86
      return true;
814
71.7k
    MBB.insert(MBB.end(), MI);
815
71.7k
    if (IsInBundle) {
816
147
      PrevMI->setFlag(MachineInstr::BundledSucc);
817
147
      MI->setFlag(MachineInstr::BundledPred);
818
147
    }
819
71.7k
    PrevMI = MI;
820
71.7k
    if (Token.is(MIToken::lbrace)) {
821
70
      if (IsInBundle)
822
1
        return error("nested instruction bundles are not allowed");
823
69
      lex();
824
69
      // This instruction is the start of the bundle.
825
69
      MI->setFlag(MachineInstr::BundledSucc);
826
69
      IsInBundle = true;
827
69
      if (!Token.is(MIToken::Newline))
828
1
        // The next instruction can be on the same line.
829
1
        continue;
830
71.7k
    }
831
71.7k
    assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
832
71.7k
    lex();
833
71.7k
  }
834
14.3k
835
14.3k
  // Construct successor list by searching for basic block machine operands.
836
14.3k
  
if (14.3k
!ExplicitSuccessors14.3k
) {
837
12.6k
    SmallVector<MachineBasicBlock*,4> Successors;
838
12.6k
    bool IsFallthrough;
839
12.6k
    guessSuccessors(MBB, Successors, IsFallthrough);
840
12.6k
    for (MachineBasicBlock *Succ : Successors)
841
948
      MBB.addSuccessor(Succ);
842
12.6k
843
12.6k
    if (IsFallthrough) {
844
5.67k
      AddFalthroughFrom = &MBB;
845
6.94k
    } else {
846
6.94k
      MBB.normalizeSuccProbs();
847
6.94k
    }
848
12.6k
  }
849
14.3k
850
14.3k
  return false;
851
14.3k
}
852
853
11.2k
bool MIParser::parseBasicBlocks() {
854
11.2k
  lex();
855
11.2k
  // Skip until the first machine basic block.
856
27.5k
  while (Token.is(MIToken::Newline))
857
16.2k
    lex();
858
11.2k
  if (Token.isErrorOrEOF())
859
13
    return Token.isError();
860
11.2k
  // The first parsing pass should have verified that this token is a MBB label
861
11.2k
  // in the 'parseBasicBlockDefinitions' method.
862
11.2k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
863
11.2k
  MachineBasicBlock *AddFalthroughFrom = nullptr;
864
14.3k
  do {
865
14.3k
    MachineBasicBlock *MBB = nullptr;
866
14.3k
    if (parseMBBReference(MBB))
867
0
      return true;
868
14.3k
    if (AddFalthroughFrom) {
869
706
      if (!AddFalthroughFrom->isSuccessor(MBB))
870
683
        AddFalthroughFrom->addSuccessor(MBB);
871
706
      AddFalthroughFrom->normalizeSuccProbs();
872
706
      AddFalthroughFrom = nullptr;
873
706
    }
874
14.3k
    if (parseBasicBlock(*MBB, AddFalthroughFrom))
875
91
      return true;
876
14.3k
    // The method 'parseBasicBlock' should parse the whole block until the next
877
14.3k
    // block or the end of file.
878
14.3k
    assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
879
14.3k
  } while (Token.isNot(MIToken::Eof));
880
11.2k
  
return false11.1k
;
881
11.2k
}
882
883
71.8k
bool MIParser::parse(MachineInstr *&MI) {
884
71.8k
  // Parse any register operands before '='
885
71.8k
  MachineOperand MO = MachineOperand::CreateImm(0);
886
71.8k
  SmallVector<ParsedMachineOperand, 8> Operands;
887
72.8k
  while (Token.isRegister() || 
Token.isRegisterFlag()17.2k
) {
888
57.1k
    auto Loc = Token.location();
889
57.1k
    Optional<unsigned> TiedDefIdx;
890
57.1k
    if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
891
17
      return true;
892
57.1k
    Operands.push_back(
893
57.1k
        ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
894
57.1k
    if (Token.isNot(MIToken::comma))
895
56.2k
      break;
896
969
    lex();
897
969
  }
898
71.8k
  
if (71.8k
!Operands.empty()71.8k
&&
expectAndConsume(MIToken::equal)56.2k
)
899
0
    return true;
900
71.8k
901
71.8k
  unsigned OpCode, Flags = 0;
902
71.8k
  if (Token.isError() || parseInstruction(OpCode, Flags))
903
1
    return true;
904
71.8k
905
71.8k
  // Parse the remaining machine operands.
906
141k
  
while (71.8k
!Token.isNewlineOrEOF() &&
Token.isNot(MIToken::kw_pre_instr_symbol)139k
&&
907
141k
         
Token.isNot(MIToken::kw_post_instr_symbol)139k
&&
908
141k
         
Token.isNot(MIToken::kw_debug_location)139k
&&
909
141k
         
Token.isNot(MIToken::coloncolon)138k
&&
Token.isNot(MIToken::lbrace)138k
) {
910
138k
    auto Loc = Token.location();
911
138k
    Optional<unsigned> TiedDefIdx;
912
138k
    if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
913
44
      return true;
914
138k
    if (OpCode == TargetOpcode::DBG_VALUE && 
MO.isReg()2.05k
)
915
853
      MO.setIsDebug();
916
138k
    Operands.push_back(
917
138k
        ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
918
138k
    if (Token.isNewlineOrEOF() || 
Token.is(MIToken::coloncolon)74.8k
||
919
138k
        
Token.is(MIToken::lbrace)70.2k
)
920
68.4k
      break;
921
70.1k
    if (Token.isNot(MIToken::comma))
922
1
      return error("expected ',' before the next machine operand");
923
70.1k
    lex();
924
70.1k
  }
925
71.8k
926
71.8k
  MCSymbol *PreInstrSymbol = nullptr;
927
71.7k
  if (Token.is(MIToken::kw_pre_instr_symbol))
928
4
    if (parsePreOrPostInstrSymbol(PreInstrSymbol))
929
0
      return true;
930
71.7k
  MCSymbol *PostInstrSymbol = nullptr;
931
71.7k
  if (Token.is(MIToken::kw_post_instr_symbol))
932
4
    if (parsePreOrPostInstrSymbol(PostInstrSymbol))
933
0
      return true;
934
71.7k
935
71.7k
  DebugLoc DebugLocation;
936
71.7k
  if (Token.is(MIToken::kw_debug_location)) {
937
1.17k
    lex();
938
1.17k
    MDNode *Node = nullptr;
939
1.17k
    if (Token.is(MIToken::exclaim)) {
940
1.17k
      if (parseMDNode(Node))
941
0
        return true;
942
6
    } else if (Token.is(MIToken::md_dilocation)) {
943
5
      if (parseDILocation(Node))
944
0
        return true;
945
1
    } else
946
1
      return error("expected a metadata node after 'debug-location'");
947
1.17k
    if (!isa<DILocation>(Node))
948
1
      return error("referenced metadata is not a DILocation");
949
1.17k
    DebugLocation = DebugLoc(Node);
950
1.17k
  }
951
71.7k
952
71.7k
  // Parse the machine memory operands.
953
71.7k
  SmallVector<MachineMemOperand *, 2> MemOperands;
954
71.7k
  if (Token.is(MIToken::coloncolon)) {
955
4.81k
    lex();
956
4.95k
    while (!Token.isNewlineOrEOF()) {
957
4.95k
      MachineMemOperand *MemOp = nullptr;
958
4.95k
      if (parseMachineMemoryOperand(MemOp))
959
14
        return true;
960
4.93k
      MemOperands.push_back(MemOp);
961
4.93k
      if (Token.isNewlineOrEOF())
962
4.79k
        break;
963
144
      if (Token.isNot(MIToken::comma))
964
1
        return error("expected ',' before the next machine memory operand");
965
143
      lex();
966
143
    }
967
4.81k
  }
968
71.7k
969
71.7k
  const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
970
71.7k
  if (!MCID.isVariadic()) {
971
67.3k
    // FIXME: Move the implicit operand verification to the machine verifier.
972
67.3k
    if (verifyImplicitOperands(Operands, MCID))
973
3
      return true;
974
71.7k
  }
975
71.7k
976
71.7k
  // TODO: Check for extraneous machine operands.
977
71.7k
  MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
978
71.7k
  MI->setFlags(Flags);
979
71.7k
  for (const auto &Operand : Operands)
980
195k
    MI->addOperand(MF, Operand.Operand);
981
71.7k
  if (assignRegisterTies(*MI, Operands))
982
3
    return true;
983
71.7k
  if (PreInstrSymbol)
984
4
    MI->setPreInstrSymbol(MF, PreInstrSymbol);
985
71.7k
  if (PostInstrSymbol)
986
4
    MI->setPostInstrSymbol(MF, PostInstrSymbol);
987
71.7k
  if (!MemOperands.empty())
988
4.79k
    MI->setMemRefs(MF, MemOperands);
989
71.7k
  return false;
990
71.7k
}
991
992
178
bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
993
178
  lex();
994
178
  if (Token.isNot(MIToken::MachineBasicBlock))
995
0
    return error("expected a machine basic block reference");
996
178
  if (parseMBBReference(MBB))
997
0
    return true;
998
178
  lex();
999
178
  if (Token.isNot(MIToken::Eof))
1000
0
    return error(
1001
0
        "expected end of string after the machine basic block reference");
1002
178
  return false;
1003
178
}
1004
1005
25.6k
bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
1006
25.6k
  lex();
1007
25.6k
  if (Token.isNot(MIToken::NamedRegister))
1008
6
    return error("expected a named register");
1009
25.6k
  if (parseNamedRegister(Reg))
1010
2
    return true;
1011
25.6k
  lex();
1012
25.6k
  if (Token.isNot(MIToken::Eof))
1013
0
    return error("expected end of string after the register reference");
1014
25.6k
  return false;
1015
25.6k
}
1016
1017
1.39k
bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1018
1.39k
  lex();
1019
1.39k
  if (Token.isNot(MIToken::VirtualRegister))
1020
1
    return error("expected a virtual register");
1021
1.39k
  if (parseVirtualRegister(Info))
1022
0
    return true;
1023
1.39k
  lex();
1024
1.39k
  if (Token.isNot(MIToken::Eof))
1025
0
    return error("expected end of string after the register reference");
1026
1.39k
  return false;
1027
1.39k
}
1028
1029
64
bool MIParser::parseStandaloneRegister(unsigned &Reg) {
1030
64
  lex();
1031
64
  if (Token.isNot(MIToken::NamedRegister) &&
1032
64
      
Token.isNot(MIToken::VirtualRegister)7
)
1033
0
    return error("expected either a named or virtual register");
1034
64
1035
64
  VRegInfo *Info;
1036
64
  if (parseRegister(Reg, Info))
1037
0
    return true;
1038
64
1039
64
  lex();
1040
64
  if (Token.isNot(MIToken::Eof))
1041
0
    return error("expected end of string after the register reference");
1042
64
  return false;
1043
64
}
1044
1045
4
bool MIParser::parseStandaloneStackObject(int &FI) {
1046
4
  lex();
1047
4
  if (Token.isNot(MIToken::StackObject))
1048
1
    return error("expected a stack object");
1049
3
  if (parseStackFrameIndex(FI))
1050
0
    return true;
1051
3
  if (Token.isNot(MIToken::Eof))
1052
0
    return error("expected end of string after the stack object reference");
1053
3
  return false;
1054
3
}
1055
1056
25
bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1057
25
  lex();
1058
25
  if (Token.is(MIToken::exclaim)) {
1059
17
    if (parseMDNode(Node))
1060
0
      return true;
1061
8
  } else if (Token.is(MIToken::md_diexpr)) {
1062
7
    if (parseDIExpression(Node))
1063
0
      return true;
1064
1
  } else if (Token.is(MIToken::md_dilocation)) {
1065
0
    if (parseDILocation(Node))
1066
0
      return true;
1067
1
  } else
1068
1
    return error("expected a metadata node");
1069
24
  if (Token.isNot(MIToken::Eof))
1070
0
    return error("expected end of string after the metadata node");
1071
24
  return false;
1072
24
}
1073
1074
3
static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1075
3
  assert(MO.isImplicit());
1076
3
  return MO.isDef() ? 
"implicit-def"0
: "implicit";
1077
3
}
1078
1079
static std::string getRegisterName(const TargetRegisterInfo *TRI,
1080
3
                                   unsigned Reg) {
1081
3
  assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
1082
3
  return StringRef(TRI->getName(Reg)).lower();
1083
3
}
1084
1085
/// Return true if the parsed machine operands contain a given machine operand.
1086
static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1087
9.36k
                                ArrayRef<ParsedMachineOperand> Operands) {
1088
44.2k
  for (const auto &I : Operands) {
1089
44.2k
    if (ImplicitOperand.isIdenticalTo(I.Operand))
1090
9.36k
      return true;
1091
44.2k
  }
1092
9.36k
  
return false3
;
1093
9.36k
}
1094
1095
bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1096
67.3k
                                      const MCInstrDesc &MCID) {
1097
67.3k
  if (MCID.isCall())
1098
388
    // We can't verify call instructions as they can contain arbitrary implicit
1099
388
    // register and register mask operands.
1100
388
    return false;
1101
66.9k
1102
66.9k
  // Gather all the expected implicit operands.
1103
66.9k
  SmallVector<MachineOperand, 4> ImplicitOperands;
1104
66.9k
  if (MCID.ImplicitDefs)
1105
5.02k
    
for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); 2.35k
*ImpDefs;
++ImpDefs2.67k
)
1106
2.67k
      ImplicitOperands.push_back(
1107
2.67k
          MachineOperand::CreateReg(*ImpDefs, true, true));
1108
66.9k
  if (MCID.ImplicitUses)
1109
12.1k
    
for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); 5.45k
*ImpUses;
++ImpUses6.69k
)
1110
6.69k
      ImplicitOperands.push_back(
1111
6.69k
          MachineOperand::CreateReg(*ImpUses, false, true));
1112
66.9k
1113
66.9k
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1114
66.9k
  assert(TRI && "Expected target register info");
1115
66.9k
  for (const auto &I : ImplicitOperands) {
1116
9.36k
    if (isImplicitOperandIn(I, Operands))
1117
9.36k
      continue;
1118
3
    return error(Operands.empty() ? 
Token.location()0
: Operands.back().End,
1119
3
                 Twine("missing implicit register operand '") +
1120
3
                     printImplicitRegisterFlag(I) + " $" +
1121
3
                     getRegisterName(TRI, I.getReg()) + "'");
1122
3
  }
1123
66.9k
  
return false66.9k
;
1124
66.9k
}
1125
1126
71.8k
bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1127
71.8k
  // Allow frame and fast math flags for OPCODE
1128
72.7k
  while (Token.is(MIToken::kw_frame_setup) ||
1129
72.7k
         
Token.is(MIToken::kw_frame_destroy)72.2k
||
1130
72.7k
         
Token.is(MIToken::kw_nnan)72.0k
||
1131
72.7k
         
Token.is(MIToken::kw_ninf)71.9k
||
1132
72.7k
         
Token.is(MIToken::kw_nsz)71.9k
||
1133
72.7k
         
Token.is(MIToken::kw_arcp)71.9k
||
1134
72.7k
         
Token.is(MIToken::kw_contract)71.9k
||
1135
72.7k
         
Token.is(MIToken::kw_afn)71.9k
||
1136
72.7k
         
Token.is(MIToken::kw_reassoc)71.9k
||
1137
72.7k
         
Token.is(MIToken::kw_nuw)71.8k
||
1138
72.7k
         
Token.is(MIToken::kw_nsw)71.8k
||
1139
72.7k
         
Token.is(MIToken::kw_exact)71.8k
||
1140
72.7k
         
Token.is(MIToken::kw_fpexcept)71.8k
) {
1141
877
    // Mine frame and fast math flags
1142
877
    if (Token.is(MIToken::kw_frame_setup))
1143
476
      Flags |= MachineInstr::FrameSetup;
1144
877
    if (Token.is(MIToken::kw_frame_destroy))
1145
188
      Flags |= MachineInstr::FrameDestroy;
1146
877
    if (Token.is(MIToken::kw_nnan))
1147
81
      Flags |= MachineInstr::FmNoNans;
1148
877
    if (Token.is(MIToken::kw_ninf))
1149
21
      Flags |= MachineInstr::FmNoInfs;
1150
877
    if (Token.is(MIToken::kw_nsz))
1151
29
      Flags |= MachineInstr::FmNsz;
1152
877
    if (Token.is(MIToken::kw_arcp))
1153
8
      Flags |= MachineInstr::FmArcp;
1154
877
    if (Token.is(MIToken::kw_contract))
1155
9
      Flags |= MachineInstr::FmContract;
1156
877
    if (Token.is(MIToken::kw_afn))
1157
5
      Flags |= MachineInstr::FmAfn;
1158
877
    if (Token.is(MIToken::kw_reassoc))
1159
9
      Flags |= MachineInstr::FmReassoc;
1160
877
    if (Token.is(MIToken::kw_nuw))
1161
14
      Flags |= MachineInstr::NoUWrap;
1162
877
    if (Token.is(MIToken::kw_nsw))
1163
36
      Flags |= MachineInstr::NoSWrap;
1164
877
    if (Token.is(MIToken::kw_exact))
1165
1
      Flags |= MachineInstr::IsExact;
1166
877
    if (Token.is(MIToken::kw_fpexcept))
1167
0
      Flags |= MachineInstr::FPExcept;
1168
877
1169
877
    lex();
1170
877
  }
1171
71.8k
  if (Token.isNot(MIToken::Identifier))
1172
0
    return error("expected a machine instruction");
1173
71.8k
  StringRef InstrName = Token.stringValue();
1174
71.8k
  if (PFS.Target.parseInstrName(InstrName, OpCode))
1175
1
    return error(Twine("unknown machine instruction name '") + InstrName + "'");
1176
71.8k
  lex();
1177
71.8k
  return false;
1178
71.8k
}
1179
1180
117k
bool MIParser::parseNamedRegister(unsigned &Reg) {
1181
117k
  assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1182
117k
  StringRef Name = Token.stringValue();
1183
117k
  if (PFS.Target.getRegisterByName(Name, Reg))
1184
3
    return error(Twine("unknown register name '") + Name + "'");
1185
117k
  return false;
1186
117k
}
1187
1188
57
bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1189
57
  assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1190
57
  StringRef Name = Token.stringValue();
1191
57
  // TODO: Check that the VReg name is not the same as a physical register name.
1192
57
  //       If it is, then print a warning (when warnings are implemented).
1193
57
  Info = &PFS.getVRegInfoNamed(Name);
1194
57
  return false;
1195
57
}
1196
1197
84.5k
bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1198
84.5k
  if (Token.is(MIToken::NamedVirtualRegister))
1199
57
    return parseNamedVirtualRegister(Info);
1200
84.4k
  assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1201
84.4k
  unsigned ID;
1202
84.4k
  if (getUnsigned(ID))
1203
0
    return true;
1204
84.4k
  Info = &PFS.getVRegInfo(ID);
1205
84.4k
  return false;
1206
84.4k
}
1207
1208
155k
bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
1209
155k
  switch (Token.kind()) {
1210
155k
  case MIToken::underscore:
1211
686
    Reg = 0;
1212
686
    return false;
1213
155k
  case MIToken::NamedRegister:
1214
71.7k
    return parseNamedRegister(Reg);
1215
155k
  case MIToken::NamedVirtualRegister:
1216
83.1k
  case MIToken::VirtualRegister:
1217
83.1k
    if (parseVirtualRegister(Info))
1218
0
      return true;
1219
83.1k
    Reg = Info->VReg;
1220
83.1k
    return false;
1221
83.1k
  // TODO: Parse other register kinds.
1222
83.1k
  default:
1223
0
    llvm_unreachable("The current token should be a register");
1224
155k
  }
1225
155k
}
1226
1227
29.2k
bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1228
29.2k
  if (Token.isNot(MIToken::Identifier) && 
Token.isNot(MIToken::underscore)16.4k
)
1229
0
    return error("expected '_', register class, or register bank name");
1230
29.2k
  StringRef::iterator Loc = Token.location();
1231
29.2k
  StringRef Name = Token.stringValue();
1232
29.2k
1233
29.2k
  // Was it a register class?
1234
29.2k
  const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1235
29.2k
  if (RC) {
1236
4.68k
    lex();
1237
4.68k
1238
4.68k
    switch (RegInfo.Kind) {
1239
4.68k
    case VRegInfo::UNKNOWN:
1240
4.68k
    case VRegInfo::NORMAL:
1241
4.68k
      RegInfo.Kind = VRegInfo::NORMAL;
1242
4.68k
      if (RegInfo.Explicit && 
RegInfo.D.RC != RC1.34k
) {
1243
1
        const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1244
1
        return error(Loc, Twine("conflicting register classes, previously: ") +
1245
1
                     Twine(TRI.getRegClassName(RegInfo.D.RC)));
1246
1
      }
1247
4.67k
      RegInfo.D.RC = RC;
1248
4.67k
      RegInfo.Explicit = true;
1249
4.67k
      return false;
1250
4.67k
1251
4.67k
    case VRegInfo::GENERIC:
1252
0
    case VRegInfo::REGBANK:
1253
0
      return error(Loc, "register class specification on generic register");
1254
0
    }
1255
0
    llvm_unreachable("Unexpected register kind");
1256
0
  }
1257
24.5k
1258
24.5k
  // Should be a register bank or a generic register.
1259
24.5k
  const RegisterBank *RegBank = nullptr;
1260
24.5k
  if (Name != "_") {
1261
8.10k
    RegBank = PFS.Target.getRegBank(Name);
1262
8.10k
    if (!RegBank)
1263
0
      return error(Loc, "expected '_', register class, or register bank name");
1264
24.5k
  }
1265
24.5k
1266
24.5k
  lex();
1267
24.5k
1268
24.5k
  switch (RegInfo.Kind) {
1269
24.5k
  case VRegInfo::UNKNOWN:
1270
24.5k
  case VRegInfo::GENERIC:
1271
24.5k
  case VRegInfo::REGBANK:
1272
24.5k
    RegInfo.Kind = RegBank ? 
VRegInfo::REGBANK8.10k
:
VRegInfo::GENERIC16.4k
;
1273
24.5k
    if (RegInfo.Explicit && 
RegInfo.D.RegBank != RegBank3.54k
)
1274
0
      return error(Loc, "conflicting generic register banks");
1275
24.5k
    RegInfo.D.RegBank = RegBank;
1276
24.5k
    RegInfo.Explicit = true;
1277
24.5k
    return false;
1278
24.5k
1279
24.5k
  case VRegInfo::NORMAL:
1280
0
    return error(Loc, "register bank specification on normal register");
1281
0
  }
1282
0
  llvm_unreachable("Unexpected register kind");
1283
0
}
1284
1285
32.6k
bool MIParser::parseRegisterFlag(unsigned &Flags) {
1286
32.6k
  const unsigned OldFlags = Flags;
1287
32.6k
  switch (Token.kind()) {
1288
32.6k
  case MIToken::kw_implicit:
1289
13.6k
    Flags |= RegState::Implicit;
1290
13.6k
    break;
1291
32.6k
  case MIToken::kw_implicit_define:
1292
4.56k
    Flags |= RegState::ImplicitDefine;
1293
4.56k
    break;
1294
32.6k
  case MIToken::kw_def:
1295
216
    Flags |= RegState::Define;
1296
216
    break;
1297
32.6k
  case MIToken::kw_dead:
1298
2.22k
    Flags |= RegState::Dead;
1299
2.22k
    break;
1300
32.6k
  case MIToken::kw_killed:
1301
7.13k
    Flags |= RegState::Kill;
1302
7.13k
    break;
1303
32.6k
  case MIToken::kw_undef:
1304
1.51k
    Flags |= RegState::Undef;
1305
1.51k
    break;
1306
32.6k
  case MIToken::kw_internal:
1307
17
    Flags |= RegState::InternalRead;
1308
17
    break;
1309
32.6k
  case MIToken::kw_early_clobber:
1310
291
    Flags |= RegState::EarlyClobber;
1311
291
    break;
1312
32.6k
  case MIToken::kw_debug_use:
1313
27
    Flags |= RegState::Debug;
1314
27
    break;
1315
32.6k
  case MIToken::kw_renamable:
1316
2.95k
    Flags |= RegState::Renamable;
1317
2.95k
    break;
1318
32.6k
  default:
1319
0
    llvm_unreachable("The current token should be a register flag");
1320
32.6k
  }
1321
32.6k
  if (OldFlags == Flags)
1322
1
    // We know that the same flag is specified more than once when the flags
1323
1
    // weren't modified.
1324
1
    return error("duplicate '" + Token.stringValue() + "' register flag");
1325
32.6k
  lex();
1326
32.6k
  return false;
1327
32.6k
}
1328
1329
1.52k
bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1330
1.52k
  assert(Token.is(MIToken::dot));
1331
1.52k
  lex();
1332
1.52k
  if (Token.isNot(MIToken::Identifier))
1333
1
    return error("expected a subregister index after '.'");
1334
1.52k
  auto Name = Token.stringValue();
1335
1.52k
  SubReg = PFS.Target.getSubRegIndex(Name);
1336
1.52k
  if (!SubReg)
1337
1
    return error(Twine("use of unknown subregister index '") + Name + "'");
1338
1.52k
  lex();
1339
1.52k
  return false;
1340
1.52k
}
1341
1342
10.5k
bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1343
10.5k
  if (!consumeIfPresent(MIToken::kw_tied_def))
1344
10.5k
    return true;
1345
24
  if (Token.isNot(MIToken::IntegerLiteral))
1346
1
    return error("expected an integer literal after 'tied-def'");
1347
23
  if (getUnsigned(TiedDefIdx))
1348
0
    return true;
1349
23
  lex();
1350
23
  if (expectAndConsume(MIToken::rparen))
1351
0
    return true;
1352
23
  return false;
1353
23
}
1354
1355
bool MIParser::assignRegisterTies(MachineInstr &MI,
1356
71.7k
                                  ArrayRef<ParsedMachineOperand> Operands) {
1357
71.7k
  SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1358
267k
  for (unsigned I = 0, E = Operands.size(); I != E; 
++I195k
) {
1359
195k
    if (!Operands[I].TiedDefIdx)
1360
195k
      continue;
1361
23
    // The parser ensures that this operand is a register use, so we just have
1362
23
    // to check the tied-def operand.
1363
23
    unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1364
23
    if (DefIdx >= E)
1365
1
      return error(Operands[I].Begin,
1366
1
                   Twine("use of invalid tied-def operand index '" +
1367
1
                         Twine(DefIdx) + "'; instruction has only ") +
1368
1
                       Twine(E) + " operands");
1369
22
    const auto &DefOperand = Operands[DefIdx].Operand;
1370
22
    if (!DefOperand.isReg() || 
!DefOperand.isDef()21
)
1371
1
      // FIXME: add note with the def operand.
1372
1
      return error(Operands[I].Begin,
1373
1
                   Twine("use of invalid tied-def operand index '") +
1374
1
                       Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1375
1
                       " isn't a defined register");
1376
21
    // Check that the tied-def operand wasn't tied elsewhere.
1377
21
    for (const auto &TiedPair : TiedRegisterPairs) {
1378
3
      if (TiedPair.first == DefIdx)
1379
1
        return error(Operands[I].Begin,
1380
1
                     Twine("the tied-def operand #") + Twine(DefIdx) +
1381
1
                         " is already tied with another register operand");
1382
3
    }
1383
21
    TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1384
20
  }
1385
71.7k
  // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1386
71.7k
  // indices must be less than tied max.
1387
71.7k
  
for (const auto &TiedPair : TiedRegisterPairs)71.7k
1388
19
    MI.tieOperands(TiedPair.first, TiedPair.second);
1389
71.7k
  return false;
1390
71.7k
}
1391
1392
bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1393
                                    Optional<unsigned> &TiedDefIdx,
1394
155k
                                    bool IsDef) {
1395
155k
  unsigned Flags = IsDef ? 
RegState::Define57.1k
:
098.3k
;
1396
188k
  while (Token.isRegisterFlag()) {
1397
32.6k
    if (parseRegisterFlag(Flags))
1398
1
      return true;
1399
32.6k
  }
1400
155k
  
if (155k
!Token.isRegister()155k
)
1401
1
    return error("expected a register after register flags");
1402
155k
  unsigned Reg;
1403
155k
  VRegInfo *RegInfo;
1404
155k
  if (parseRegister(Reg, RegInfo))
1405
1
    return true;
1406
155k
  lex();
1407
155k
  unsigned SubReg = 0;
1408
155k
  if (Token.is(MIToken::dot)) {
1409
1.52k
    if (parseSubRegisterIndex(SubReg))
1410
2
      return true;
1411
1.52k
    if (!TargetRegisterInfo::isVirtualRegister(Reg))
1412
1
      return error("subregister index expects a virtual register");
1413
155k
  }
1414
155k
  if (Token.is(MIToken::colon)) {
1415
29.2k
    if (!TargetRegisterInfo::isVirtualRegister(Reg))
1416
1
      return error("register class specification expects a virtual register");
1417
29.2k
    lex();
1418
29.2k
    if (parseRegisterClassOrBank(*RegInfo))
1419
1
        return true;
1420
155k
  }
1421
155k
  MachineRegisterInfo &MRI = MF.getRegInfo();
1422
155k
  if ((Flags & RegState::Define) == 0) {
1423
93.5k
    if (consumeIfPresent(MIToken::lparen)) {
1424
10.5k
      unsigned Idx;
1425
10.5k
      if (!parseRegisterTiedDefIndex(Idx))
1426
23
        TiedDefIdx = Idx;
1427
10.5k
      else {
1428
10.5k
        // Try a redundant low-level type.
1429
10.5k
        LLT Ty;
1430
10.5k
        if (parseLowLevelType(Token.location(), Ty))
1431
2
          return error("expected tied-def or low-level type after '('");
1432
10.5k
1433
10.5k
        if (expectAndConsume(MIToken::rparen))
1434
0
          return true;
1435
10.5k
1436
10.5k
        if (MRI.getType(Reg).isValid() && 
MRI.getType(Reg) != Ty10.5k
)
1437
0
          return error("inconsistent type for generic virtual register");
1438
10.5k
1439
10.5k
        MRI.setType(Reg, Ty);
1440
10.5k
      }
1441
10.5k
    }
1442
93.5k
  } else 
if (61.9k
consumeIfPresent(MIToken::lparen)61.9k
) {
1443
31.7k
    // Virtual registers may have a tpe with GlobalISel.
1444
31.7k
    if (!TargetRegisterInfo::isVirtualRegister(Reg))
1445
1
      return error("unexpected type on physical register");
1446
31.7k
1447
31.7k
    LLT Ty;
1448
31.7k
    if (parseLowLevelType(Token.location(), Ty))
1449
11
      return true;
1450
31.7k
1451
31.7k
    if (expectAndConsume(MIToken::rparen))
1452
0
      return true;
1453
31.7k
1454
31.7k
    if (MRI.getType(Reg).isValid() && 
MRI.getType(Reg) != Ty7
)
1455
0
      return error("inconsistent type for generic virtual register");
1456
31.7k
1457
31.7k
    MRI.setType(Reg, Ty);
1458
31.7k
  } else 
if (30.1k
TargetRegisterInfo::isVirtualRegister(Reg)30.1k
) {
1459
9.06k
    // Generic virtual registers must have a type.
1460
9.06k
    // If we end up here this means the type hasn't been specified and
1461
9.06k
    // this is bad!
1462
9.06k
    if (RegInfo->Kind == VRegInfo::GENERIC ||
1463
9.06k
        
RegInfo->Kind == VRegInfo::REGBANK9.06k
)
1464
2
      return error("generic virtual registers must have a type");
1465
155k
  }
1466
155k
  Dest = MachineOperand::CreateReg(
1467
155k
      Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1468
155k
      Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1469
155k
      Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1470
155k
      Flags & RegState::InternalRead, Flags & RegState::Renamable);
1471
155k
1472
155k
  return false;
1473
155k
}
1474
1475
29.6k
bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1476
29.6k
  assert(Token.is(MIToken::IntegerLiteral));
1477
29.6k
  const APSInt &Int = Token.integerValue();
1478
29.6k
  if (Int.getMinSignedBits() > 64)
1479
1
    return error("integer literal is too large to be an immediate operand");
1480
29.6k
  Dest = MachineOperand::CreateImm(Int.getExtValue());
1481
29.6k
  lex();
1482
29.6k
  return false;
1483
29.6k
}
1484
1485
bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1486
2.15k
                               const Constant *&C) {
1487
2.15k
  auto Source = StringValue.str(); // The source has to be null terminated.
1488
2.15k
  SMDiagnostic Err;
1489
2.15k
  C = parseConstantValue(Source, Err, *MF.getFunction().getParent(),
1490
2.15k
                         &PFS.IRSlots);
1491
2.15k
  if (!C)
1492
1
    return error(Loc + Err.getColumnNo(), Err.getMessage());
1493
2.14k
  return false;
1494
2.14k
}
1495
1496
1.57k
bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1497
1.57k
  if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1498
1
    return true;
1499
1.57k
  lex();
1500
1.57k
  return false;
1501
1.57k
}
1502
1503
// See LLT implemntation for bit size limits.
1504
38.0k
static bool verifyScalarSize(uint64_t Size) {
1505
38.0k
  return Size != 0 && 
isUInt<16>(Size)38.0k
;
1506
38.0k
}
1507
1508
7.06k
static bool verifyVectorElementCount(uint64_t NumElts) {
1509
7.06k
  return NumElts != 0 && 
isUInt<16>(NumElts)7.05k
;
1510
7.06k
}
1511
1512
4.26k
static bool verifyAddrSpace(uint64_t AddrSpace) {
1513
4.26k
  return isUInt<24>(AddrSpace);
1514
4.26k
}
1515
1516
42.3k
bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1517
42.3k
  if (Token.range().front() == 's' || 
Token.range().front() == 'p'11.1k
) {
1518
35.3k
    StringRef SizeStr = Token.range().drop_front();
1519
35.3k
    if (SizeStr.size() == 0 || 
!llvm::all_of(SizeStr, isdigit)35.3k
)
1520
1
      return error("expected integers after 's'/'p' type character");
1521
42.3k
  }
1522
42.3k
1523
42.3k
  if (Token.range().front() == 's') {
1524
31.2k
    auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1525
31.2k
    if (!verifyScalarSize(ScalarSize))
1526
2
      return error("invalid size for scalar type");
1527
31.1k
1528
31.1k
    Ty = LLT::scalar(ScalarSize);
1529
31.1k
    lex();
1530
31.1k
    return false;
1531
31.1k
  } else 
if (11.1k
Token.range().front() == 'p'11.1k
) {
1532
4.10k
    const DataLayout &DL = MF.getDataLayout();
1533
4.10k
    uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
1534
4.10k
    if (!verifyAddrSpace(AS))
1535
2
      return error("invalid address space number");
1536
4.10k
1537
4.10k
    Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1538
4.10k
    lex();
1539
4.10k
    return false;
1540
4.10k
  }
1541
7.06k
1542
7.06k
  // Now we're looking for a vector.
1543
7.06k
  if (Token.isNot(MIToken::less))
1544
3
    return error(Loc,
1545
3
                 "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1546
7.06k
  lex();
1547
7.06k
1548
7.06k
  if (Token.isNot(MIToken::IntegerLiteral))
1549
0
    return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1550
7.06k
  uint64_t NumElements = Token.integerValue().getZExtValue();
1551
7.06k
  if (!verifyVectorElementCount(NumElements))
1552
2
    return error("invalid number of vector elements");
1553
7.05k
1554
7.05k
  lex();
1555
7.05k
1556
7.05k
  if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1557
0
    return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1558
7.05k
  lex();
1559
7.05k
1560
7.05k
  if (Token.range().front() != 's' && 
Token.range().front() != 'p'161
)
1561
1
    return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1562
7.05k
  StringRef SizeStr = Token.range().drop_front();
1563
7.05k
  if (SizeStr.size() == 0 || 
!llvm::all_of(SizeStr, isdigit)7.05k
)
1564
1
    return error("expected integers after 's'/'p' type character");
1565
7.05k
1566
7.05k
  if (Token.range().front() == 's') {
1567
6.89k
    auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1568
6.89k
    if (!verifyScalarSize(ScalarSize))
1569
1
      return error("invalid size for scalar type");
1570
6.89k
    Ty = LLT::scalar(ScalarSize);
1571
6.89k
  } else 
if (159
Token.range().front() == 'p'159
) {
1572
159
    const DataLayout &DL = MF.getDataLayout();
1573
159
    uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
1574
159
    if (!verifyAddrSpace(AS))
1575
0
      return error("invalid address space number");
1576
159
1577
159
    Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1578
159
  } else
1579
0
    return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1580
7.05k
  lex();
1581
7.05k
1582
7.05k
  if (Token.isNot(MIToken::greater))
1583
0
    return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1584
7.05k
  lex();
1585
7.05k
1586
7.05k
  Ty = LLT::vector(NumElements, Ty);
1587
7.05k
  return false;
1588
7.05k
}
1589
1590
1.45k
bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1591
1.45k
  assert(Token.is(MIToken::Identifier));
1592
1.45k
  StringRef TypeStr = Token.range();
1593
1.45k
  if (TypeStr.front() != 'i' && 
TypeStr.front() != 's'1
&&
1594
1.45k
      
TypeStr.front() != 'p'1
)
1595
1
    return error(
1596
1
        "a typed immediate operand should start with one of 'i', 's', or 'p'");
1597
1.45k
  StringRef SizeStr = Token.range().drop_front();
1598
1.45k
  if (SizeStr.size() == 0 || 
!llvm::all_of(SizeStr, isdigit)1.45k
)
1599
1
    return error("expected integers after 'i'/'s'/'p' type character");
1600
1.45k
1601
1.45k
  auto Loc = Token.location();
1602
1.45k
  lex();
1603
1.45k
  if (Token.isNot(MIToken::IntegerLiteral)) {
1604
22
    if (Token.isNot(MIToken::Identifier) ||
1605
22
        !(Token.range() == "true" || 
Token.range() == "false"6
))
1606
0
      return error("expected an integer literal");
1607
1.45k
  }
1608
1.45k
  const Constant *C = nullptr;
1609
1.45k
  if (parseIRConstant(Loc, C))
1610
0
    return true;
1611
1.45k
  Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1612
1.45k
  return false;
1613
1.45k
}
1614
1615
125
bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1616
125
  auto Loc = Token.location();
1617
125
  lex();
1618
125
  if (Token.isNot(MIToken::FloatingPointLiteral) &&
1619
125
      
Token.isNot(MIToken::HexLiteral)15
)
1620
1
    return error("expected a floating point literal");
1621
124
  const Constant *C = nullptr;
1622
124
  if (parseIRConstant(Loc, C))
1623
1
    return true;
1624
123
  Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1625
123
  return false;
1626
123
}
1627
1628
128k
bool MIParser::getUnsigned(unsigned &Result) {
1629
128k
  if (Token.hasIntegerValue()) {
1630
126k
    const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1631
126k
    uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1632
126k
    if (Val64 == Limit)
1633
1
      return error("expected 32-bit integer (too large)");
1634
126k
    Result = Val64;
1635
126k
    return false;
1636
126k
  }
1637
1.39k
  if (Token.is(MIToken::HexLiteral)) {
1638
1.39k
    APInt A;
1639
1.39k
    if (getHexUint(A))
1640
0
      return true;
1641
1.39k
    if (A.getBitWidth() > 32)
1642
0
      return error("expected 32-bit integer (too large)");
1643
1.39k
    Result = A.getZExtValue();
1644
1.39k
    return false;
1645
1.39k
  }
1646
0
  return true;
1647
0
}
1648
1649
20.4k
bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1650
20.4k
  assert(Token.is(MIToken::MachineBasicBlock) ||
1651
20.4k
         Token.is(MIToken::MachineBasicBlockLabel));
1652
20.4k
  unsigned Number;
1653
20.4k
  if (getUnsigned(Number))
1654
1
    return true;
1655
20.4k
  auto MBBInfo = PFS.MBBSlots.find(Number);
1656
20.4k
  if (MBBInfo == PFS.MBBSlots.end())
1657
1
    return error(Twine("use of undefined machine basic block #") +
1658
1
                 Twine(Number));
1659
20.4k
  MBB = MBBInfo->second;
1660
20.4k
  // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1661
20.4k
  // we drop the <irname> from the bb.<id>.<irname> format.
1662
20.4k
  if (!Token.stringValue().empty() && 
Token.stringValue() != MBB->getName()3.59k
)
1663
1
    return error(Twine("the name of machine basic block #") + Twine(Number) +
1664
1
                 " isn't '" + Token.stringValue() + "'");
1665
20.4k
  return false;
1666
20.4k
}
1667
1668
3.32k
bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1669
3.32k
  MachineBasicBlock *MBB;
1670
3.32k
  if (parseMBBReference(MBB))
1671
3
    return true;
1672
3.32k
  Dest = MachineOperand::CreateMBB(MBB);
1673
3.32k
  lex();
1674
3.32k
  return false;
1675
3.32k
}
1676
1677
959
bool MIParser::parseStackFrameIndex(int &FI) {
1678
959
  assert(Token.is(MIToken::StackObject));
1679
959
  unsigned ID;
1680
959
  if (getUnsigned(ID))
1681
0
    return true;
1682
959
  auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1683
959
  if (ObjectInfo == PFS.StackObjectSlots.end())
1684
1
    return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1685
1
                 "'");
1686
958
  StringRef Name;
1687
958
  if (const auto *Alloca =
1688
256
          MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
1689
256
    Name = Alloca->getName();
1690
958
  if (!Token.stringValue().empty() && 
Token.stringValue() != Name255
)
1691
1
    return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1692
1
                 "' isn't '" + Token.stringValue() + "'");
1693
957
  lex();
1694
957
  FI = ObjectInfo->second;
1695
957
  return false;
1696
957
}
1697
1698
364
bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1699
364
  int FI;
1700
364
  if (parseStackFrameIndex(FI))
1701
2
    return true;
1702
362
  Dest = MachineOperand::CreateFI(FI);
1703
362
  return false;
1704
362
}
1705
1706
290
bool MIParser::parseFixedStackFrameIndex(int &FI) {
1707
290
  assert(Token.is(MIToken::FixedStackObject));
1708
290
  unsigned ID;
1709
290
  if (getUnsigned(ID))
1710
0
    return true;
1711
290
  auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1712
290
  if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1713
1
    return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1714
1
                 Twine(ID) + "'");
1715
289
  lex();
1716
289
  FI = ObjectInfo->second;
1717
289
  return false;
1718
289
}
1719
1720
139
bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1721
139
  int FI;
1722
139
  if (parseFixedStackFrameIndex(FI))
1723
1
    return true;
1724
138
  Dest = MachineOperand::CreateFI(FI);
1725
138
  return false;
1726
138
}
1727
1728
1.32k
bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1729
1.32k
  switch (Token.kind()) {
1730
1.32k
  case MIToken::NamedGlobalValue: {
1731
1.31k
    const Module *M = MF.getFunction().getParent();
1732
1.31k
    GV = M->getNamedValue(Token.stringValue());
1733
1.31k
    if (!GV)
1734
1
      return error(Twine("use of undefined global value '") + Token.range() +
1735
1
                   "'");
1736
1.31k
    break;
1737
1.31k
  }
1738
1.31k
  case MIToken::GlobalValue: {
1739
6
    unsigned GVIdx;
1740
6
    if (getUnsigned(GVIdx))
1741
0
      return true;
1742
6
    if (GVIdx >= PFS.IRSlots.GlobalValues.size())
1743
1
      return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1744
1
                   "'");
1745
5
    GV = PFS.IRSlots.GlobalValues[GVIdx];
1746
5
    break;
1747
5
  }
1748
5
  default:
1749
0
    llvm_unreachable("The current token should be a global value");
1750
1.32k
  }
1751
1.32k
  return false;
1752
1.32k
}
1753
1754
1.07k
bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1755
1.07k
  GlobalValue *GV = nullptr;
1756
1.07k
  if (parseGlobalValue(GV))
1757
2
    return true;
1758
1.07k
  lex();
1759
1.07k
  Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1760
1.07k
  if (parseOperandsOffset(Dest))
1761
2
    return true;
1762
1.07k
  return false;
1763
1.07k
}
1764
1765
49
bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1766
49
  assert(Token.is(MIToken::ConstantPoolItem));
1767
49
  unsigned ID;
1768
49
  if (getUnsigned(ID))
1769
0
    return true;
1770
49
  auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1771
49
  if (ConstantInfo == PFS.ConstantPoolSlots.end())
1772
1
    return error("use of undefined constant '%const." + Twine(ID) + "'");
1773
48
  lex();
1774
48
  Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1775
48
  if (parseOperandsOffset(Dest))
1776
0
    return true;
1777
48
  return false;
1778
48
}
1779
1780
26
bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1781
26
  assert(Token.is(MIToken::JumpTableIndex));
1782
26
  unsigned ID;
1783
26
  if (getUnsigned(ID))
1784
0
    return true;
1785
26
  auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1786
26
  if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1787
1
    return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1788
25
  lex();
1789
25
  Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1790
25
  return false;
1791
25
}
1792
1793
251
bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1794
251
  assert(Token.is(MIToken::ExternalSymbol));
1795
251
  const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1796
251
  lex();
1797
251
  Dest = MachineOperand::CreateES(Symbol);
1798
251
  if (parseOperandsOffset(Dest))
1799
0
    return true;
1800
251
  return false;
1801
251
}
1802
1803
19
bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
1804
19
  assert(Token.is(MIToken::MCSymbol));
1805
19
  MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
1806
19
  lex();
1807
19
  Dest = MachineOperand::CreateMCSymbol(Symbol);
1808
19
  if (parseOperandsOffset(Dest))
1809
0
    return true;
1810
19
  return false;
1811
19
}
1812
1813
371
bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1814
371
  assert(Token.is(MIToken::SubRegisterIndex));
1815
371
  StringRef Name = Token.stringValue();
1816
371
  unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
1817
371
  if (SubRegIndex == 0)
1818
1
    return error(Twine("unknown subregister index '") + Name + "'");
1819
370
  lex();
1820
370
  Dest = MachineOperand::CreateImm(SubRegIndex);
1821
370
  return false;
1822
370
}
1823
1824
2.05k
bool MIParser::parseMDNode(MDNode *&Node) {
1825
2.05k
  assert(Token.is(MIToken::exclaim));
1826
2.05k
1827
2.05k
  auto Loc = Token.location();
1828
2.05k
  lex();
1829
2.05k
  if (Token.isNot(MIToken::IntegerLiteral) || 
Token.integerValue().isSigned()2.05k
)
1830
1
    return error("expected metadata id after '!'");
1831
2.05k
  unsigned ID;
1832
2.05k
  if (getUnsigned(ID))
1833
0
    return true;
1834
2.05k
  auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1835
2.05k
  if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
1836
1
    return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1837
2.05k
  lex();
1838
2.05k
  Node = NodeInfo->second.get();
1839
2.05k
  return false;
1840
2.05k
}
1841
1842
304
bool MIParser::parseDIExpression(MDNode *&Expr) {
1843
304
  assert(Token.is(MIToken::md_diexpr));
1844
304
  lex();
1845
304
1846
304
  // FIXME: Share this parsing with the IL parser.
1847
304
  SmallVector<uint64_t, 8> Elements;
1848
304
1849
304
  if (expectAndConsume(MIToken::lparen))
1850
0
    return true;
1851
304
1852
304
  if (Token.isNot(MIToken::rparen)) {
1853
237
    do {
1854
237
      if (Token.is(MIToken::Identifier)) {
1855
154
        if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
1856
152
          lex();
1857
152
          Elements.push_back(Op);
1858
152
          continue;
1859
152
        }
1860
2
        if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) {
1861
2
          lex();
1862
2
          Elements.push_back(Enc);
1863
2
          continue;
1864
2
        }
1865
0
        return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1866
0
      }
1867
83
1868
83
      if (Token.isNot(MIToken::IntegerLiteral) ||
1869
83
          Token.integerValue().isSigned())
1870
0
        return error("expected unsigned integer");
1871
83
1872
83
      auto &U = Token.integerValue();
1873
83
      if (U.ugt(UINT64_MAX))
1874
0
        return error("element too large, limit is " + Twine(UINT64_MAX));
1875
83
      Elements.push_back(U.getZExtValue());
1876
83
      lex();
1877
83
1878
237
    } while (consumeIfPresent(MIToken::comma));
1879
68
  }
1880
304
1881
304
  if (expectAndConsume(MIToken::rparen))
1882
0
    return true;
1883
304
1884
304
  Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
1885
304
  return false;
1886
304
}
1887
1888
6
bool MIParser::parseDILocation(MDNode *&Loc) {
1889
6
  assert(Token.is(MIToken::md_dilocation));
1890
6
  lex();
1891
6
1892
6
  bool HaveLine = false;
1893
6
  unsigned Line = 0;
1894
6
  unsigned Column = 0;
1895
6
  MDNode *Scope = nullptr;
1896
6
  MDNode *InlinedAt = nullptr;
1897
6
  bool ImplicitCode = false;
1898
6
1899
6
  if (expectAndConsume(MIToken::lparen))
1900
0
    return true;
1901
6
1902
6
  if (Token.isNot(MIToken::rparen)) {
1903
17
    do {
1904
17
      if (Token.is(MIToken::Identifier)) {
1905
17
        if (Token.stringValue() == "line") {
1906
6
          lex();
1907
6
          if (expectAndConsume(MIToken::colon))
1908
0
            return true;
1909
6
          if (Token.isNot(MIToken::IntegerLiteral) ||
1910
6
              Token.integerValue().isSigned())
1911
0
            return error("expected unsigned integer");
1912
6
          Line = Token.integerValue().getZExtValue();
1913
6
          HaveLine = true;
1914
6
          lex();
1915
6
          continue;
1916
6
        }
1917
11
        if (Token.stringValue() == "column") {
1918
2
          lex();
1919
2
          if (expectAndConsume(MIToken::colon))
1920
0
            return true;
1921
2
          if (Token.isNot(MIToken::IntegerLiteral) ||
1922
2
              Token.integerValue().isSigned())
1923
0
            return error("expected unsigned integer");
1924
2
          Column = Token.integerValue().getZExtValue();
1925
2
          lex();
1926
2
          continue;
1927
2
        }
1928
9
        if (Token.stringValue() == "scope") {
1929
6
          lex();
1930
6
          if (expectAndConsume(MIToken::colon))
1931
0
            return true;
1932
6
          if (parseMDNode(Scope))
1933
0
            return error("expected metadata node");
1934
6
          if (!isa<DIScope>(Scope))
1935
0
            return error("expected DIScope node");
1936
6
          continue;
1937
6
        }
1938
3
        if (Token.stringValue() == "inlinedAt") {
1939
2
          lex();
1940
2
          if (expectAndConsume(MIToken::colon))
1941
0
            return true;
1942
2
          if (Token.is(MIToken::exclaim)) {
1943
1
            if (parseMDNode(InlinedAt))
1944
0
              return true;
1945
1
          } else if (Token.is(MIToken::md_dilocation)) {
1946
1
            if (parseDILocation(InlinedAt))
1947
0
              return true;
1948
0
          } else
1949
0
            return error("expected metadata node");
1950
2
          if (!isa<DILocation>(InlinedAt))
1951
0
            return error("expected DILocation node");
1952
2
          continue;
1953
2
        }
1954
1
        if (Token.stringValue() == "isImplicitCode") {
1955
1
          lex();
1956
1
          if (expectAndConsume(MIToken::colon))
1957
0
            return true;
1958
1
          if (!Token.is(MIToken::Identifier))
1959
0
            return error("expected true/false");
1960
1
          // As far as I can see, we don't have any existing need for parsing
1961
1
          // true/false in MIR yet. Do it ad-hoc until there's something else
1962
1
          // that needs it.
1963
1
          if (Token.stringValue() == "true")
1964
1
            ImplicitCode = true;
1965
0
          else if (Token.stringValue() == "false")
1966
0
            ImplicitCode = false;
1967
0
          else
1968
0
            return error("expected true/false");
1969
1
          lex();
1970
1
          continue;
1971
1
        }
1972
1
      }
1973
0
      return error(Twine("invalid DILocation argument '") +
1974
0
                   Token.stringValue() + "'");
1975
17
    } while (consumeIfPresent(MIToken::comma));
1976
6
  }
1977
6
1978
6
  if (expectAndConsume(MIToken::rparen))
1979
0
    return true;
1980
6
1981
6
  if (!HaveLine)
1982
0
    return error("DILocation requires line number");
1983
6
  if (!Scope)
1984
0
    return error("DILocation requires a scope");
1985
6
1986
6
  Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
1987
6
                        InlinedAt, ImplicitCode);
1988
6
  return false;
1989
6
}
1990
1991
963
bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1992
963
  MDNode *Node = nullptr;
1993
963
  if (Token.is(MIToken::exclaim)) {
1994
666
    if (parseMDNode(Node))
1995
2
      return true;
1996
297
  } else if (Token.is(MIToken::md_diexpr)) {
1997
297
    if (parseDIExpression(Node))
1998
0
      return true;
1999
961
  }
2000
961
  Dest = MachineOperand::CreateMetadata(Node);
2001
961
  return false;
2002
961
}
2003
2004
366
bool MIParser::parseCFIOffset(int &Offset) {
2005
366
  if (Token.isNot(MIToken::IntegerLiteral))
2006
1
    return error("expected a cfi offset");
2007
365
  if (Token.integerValue().getMinSignedBits() > 32)
2008
1
    return error("expected a 32 bit integer (the cfi offset is too large)");
2009
364
  Offset = (int)Token.integerValue().getExtValue();
2010
364
  lex();
2011
364
  return false;
2012
364
}
2013
2014
268
bool MIParser::parseCFIRegister(unsigned &Reg) {
2015
268
  if (Token.isNot(MIToken::NamedRegister))
2016
1
    return error("expected a cfi register");
2017
267
  unsigned LLVMReg;
2018
267
  if (parseNamedRegister(LLVMReg))
2019
0
    return true;
2020
267
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2021
267
  assert(TRI && "Expected target register info");
2022
267
  int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2023
267
  if (DwarfReg < 0)
2024
0
    return error("invalid DWARF register");
2025
267
  Reg = (unsigned)DwarfReg;
2026
267
  lex();
2027
267
  return false;
2028
267
}
2029
2030
1
bool MIParser::parseCFIEscapeValues(std::string &Values) {
2031
3
  do {
2032
3
    if (Token.isNot(MIToken::HexLiteral))
2033
0
      return error("expected a hexadecimal literal");
2034
3
    unsigned Value;
2035
3
    if (getUnsigned(Value))
2036
0
      return true;
2037
3
    if (Value > UINT8_MAX)
2038
3
      
return error("expected a 8-bit integer (too large)")0
;
2039
3
    Values.push_back(static_cast<uint8_t>(Value));
2040
3
    lex();
2041
3
  } while (consumeIfPresent(MIToken::comma));
2042
1
  return false;
2043
1
}
2044
2045
403
bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2046
403
  auto Kind = Token.kind();
2047
403
  lex();
2048
403
  int Offset;
2049
403
  unsigned Reg;
2050
403
  unsigned CFIIndex;
2051
403
  switch (Kind) {
2052
403
  case MIToken::kw_cfi_same_value:
2053
3
    if (parseCFIRegister(Reg))
2054
0
      return true;
2055
3
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2056
3
    break;
2057
223
  case MIToken::kw_cfi_offset:
2058
223
    if (parseCFIRegister(Reg) || 
expectAndConsume(MIToken::comma)222
||
2059
223
        
parseCFIOffset(Offset)221
)
2060
2
      return true;
2061
221
    CFIIndex =
2062
221
        MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2063
221
    break;
2064
221
  case MIToken::kw_cfi_rel_offset:
2065
1
    if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2066
1
        parseCFIOffset(Offset))
2067
0
      return true;
2068
1
    CFIIndex = MF.addFrameInst(
2069
1
        MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
2070
1
    break;
2071
22
  case MIToken::kw_cfi_def_cfa_register:
2072
22
    if (parseCFIRegister(Reg))
2073
0
      return true;
2074
22
    CFIIndex =
2075
22
        MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2076
22
    break;
2077
129
  case MIToken::kw_cfi_def_cfa_offset:
2078
129
    if (parseCFIOffset(Offset))
2079
2
      return true;
2080
127
    // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
2081
127
    CFIIndex = MF.addFrameInst(
2082
127
        MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
2083
127
    break;
2084
127
  case MIToken::kw_cfi_adjust_cfa_offset:
2085
2
    if (parseCFIOffset(Offset))
2086
0
      return true;
2087
2
    CFIIndex = MF.addFrameInst(
2088
2
        MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
2089
2
    break;
2090
13
  case MIToken::kw_cfi_def_cfa:
2091
13
    if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2092
13
        parseCFIOffset(Offset))
2093
0
      return true;
2094
13
    // NB: MCCFIInstruction::createDefCfa negates the offset.
2095
13
    CFIIndex =
2096
13
        MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
2097
13
    break;
2098
13
  case MIToken::kw_cfi_remember_state:
2099
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2100
1
    break;
2101
13
  case MIToken::kw_cfi_restore:
2102
3
    if (parseCFIRegister(Reg))
2103
0
      return true;
2104
3
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2105
3
    break;
2106
3
  case MIToken::kw_cfi_restore_state:
2107
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2108
1
    break;
2109
3
  case MIToken::kw_cfi_undefined:
2110
1
    if (parseCFIRegister(Reg))
2111
0
      return true;
2112
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2113
1
    break;
2114
1
  case MIToken::kw_cfi_register: {
2115
1
    unsigned Reg2;
2116
1
    if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2117
1
        parseCFIRegister(Reg2))
2118
0
      return true;
2119
1
2120
1
    CFIIndex =
2121
1
        MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2122
1
    break;
2123
1
  }
2124
1
  case MIToken::kw_cfi_window_save:
2125
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2126
1
    break;
2127
1
  case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2128
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2129
1
    break;
2130
1
  case MIToken::kw_cfi_escape: {
2131
1
    std::string Values;
2132
1
    if (parseCFIEscapeValues(Values))
2133
0
      return true;
2134
1
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2135
1
    break;
2136
1
  }
2137
1
  default:
2138
0
    // TODO: Parse the other CFI operands.
2139
0
    llvm_unreachable("The current token should be a cfi operand");
2140
399
  }
2141
399
  Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2142
399
  return false;
2143
399
}
2144
2145
1.40k
bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2146
1.40k
  switch (Token.kind()) {
2147
1.40k
  case MIToken::NamedIRBlock: {
2148
17
    BB = dyn_cast_or_null<BasicBlock>(
2149
17
        F.getValueSymbolTable()->lookup(Token.stringValue()));
2150
17
    if (!BB)
2151
1
      return error(Twine("use of undefined IR block '") + Token.range() + "'");
2152
16
    break;
2153
16
  }
2154
1.39k
  case MIToken::IRBlock: {
2155
1.39k
    unsigned SlotNumber = 0;
2156
1.39k
    if (getUnsigned(SlotNumber))
2157
0
      return true;
2158
1.39k
    BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2159
1.39k
    if (!BB)
2160
2
      return error(Twine("use of undefined IR block '%ir-block.") +
2161
2
                   Twine(SlotNumber) + "'");
2162
1.38k
    break;
2163
1.38k
  }
2164
1.38k
  default:
2165
0
    llvm_unreachable("The current token should be an IR block reference");
2166
1.40k
  }
2167
1.40k
  return false;
2168
1.40k
}
2169
2170
23
bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2171
23
  assert(Token.is(MIToken::kw_blockaddress));
2172
23
  lex();
2173
23
  if (expectAndConsume(MIToken::lparen))
2174
0
    return true;
2175
23
  if (Token.isNot(MIToken::GlobalValue) &&
2176
23
      Token.isNot(MIToken::NamedGlobalValue))
2177
1
    return error("expected a global value");
2178
22
  GlobalValue *GV = nullptr;
2179
22
  if (parseGlobalValue(GV))
2180
0
    return true;
2181
22
  auto *F = dyn_cast<Function>(GV);
2182
22
  if (!F)
2183
1
    return error("expected an IR function reference");
2184
21
  lex();
2185
21
  if (expectAndConsume(MIToken::comma))
2186
0
    return true;
2187
21
  BasicBlock *BB = nullptr;
2188
21
  if (Token.isNot(MIToken::IRBlock) && 
Token.isNot(MIToken::NamedIRBlock)18
)
2189
1
    return error("expected an IR block reference");
2190
20
  if (parseIRBlock(BB, *F))
2191
2
    return true;
2192
18
  lex();
2193
18
  if (expectAndConsume(MIToken::rparen))
2194
0
    return true;
2195
18
  Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2196
18
  if (parseOperandsOffset(Dest))
2197
0
    return true;
2198
18
  return false;
2199
18
}
2200
2201
243
bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2202
243
  assert(Token.is(MIToken::kw_intrinsic));
2203
243
  lex();
2204
243
  if (expectAndConsume(MIToken::lparen))
2205
0
    return error("expected syntax intrinsic(@llvm.whatever)");
2206
243
2207
243
  if (Token.isNot(MIToken::NamedGlobalValue))
2208
0
    return error("expected syntax intrinsic(@llvm.whatever)");
2209
243
2210
243
  std::string Name = Token.stringValue();
2211
243
  lex();
2212
243
2213
243
  if (expectAndConsume(MIToken::rparen))
2214
0
    return error("expected ')' to terminate intrinsic name");
2215
243
2216
243
  // Find out what intrinsic we're dealing with, first try the global namespace
2217
243
  // and then the target's private intrinsics if that fails.
2218
243
  const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
2219
243
  Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
2220
243
  if (ID == Intrinsic::not_intrinsic && 
TII0
)
2221
0
    ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
2222
243
2223
243
  if (ID == Intrinsic::not_intrinsic)
2224
0
    return error("unknown intrinsic name");
2225
243
  Dest = MachineOperand::CreateIntrinsicID(ID);
2226
243
2227
243
  return false;
2228
243
}
2229
2230
1.54k
bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2231
1.54k
  assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2232
1.54k
  bool IsFloat = Token.is(MIToken::kw_floatpred);
2233
1.54k
  lex();
2234
1.54k
2235
1.54k
  if (expectAndConsume(MIToken::lparen))
2236
0
    return error("expected syntax intpred(whatever) or floatpred(whatever");
2237
1.54k
2238
1.54k
  if (Token.isNot(MIToken::Identifier))
2239
0
    return error("whatever");
2240
1.54k
2241
1.54k
  CmpInst::Predicate Pred;
2242
1.54k
  if (IsFloat) {
2243
463
    Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2244
463
               .Case("false", CmpInst::FCMP_FALSE)
2245
463
               .Case("oeq", CmpInst::FCMP_OEQ)
2246
463
               .Case("ogt", CmpInst::FCMP_OGT)
2247
463
               .Case("oge", CmpInst::FCMP_OGE)
2248
463
               .Case("olt", CmpInst::FCMP_OLT)
2249
463
               .Case("ole", CmpInst::FCMP_OLE)
2250
463
               .Case("one", CmpInst::FCMP_ONE)
2251
463
               .Case("ord", CmpInst::FCMP_ORD)
2252
463
               .Case("uno", CmpInst::FCMP_UNO)
2253
463
               .Case("ueq", CmpInst::FCMP_UEQ)
2254
463
               .Case("ugt", CmpInst::FCMP_UGT)
2255
463
               .Case("uge", CmpInst::FCMP_UGE)
2256
463
               .Case("ult", CmpInst::FCMP_ULT)
2257
463
               .Case("ule", CmpInst::FCMP_ULE)
2258
463
               .Case("une", CmpInst::FCMP_UNE)
2259
463
               .Case("true", CmpInst::FCMP_TRUE)
2260
463
               .Default(CmpInst::BAD_FCMP_PREDICATE);
2261
463
    if (!CmpInst::isFPPredicate(Pred))
2262
0
      return error("invalid floating-point predicate");
2263
1.08k
  } else {
2264
1.08k
    Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2265
1.08k
               .Case("eq", CmpInst::ICMP_EQ)
2266
1.08k
               .Case("ne", CmpInst::ICMP_NE)
2267
1.08k
               .Case("sgt", CmpInst::ICMP_SGT)
2268
1.08k
               .Case("sge", CmpInst::ICMP_SGE)
2269
1.08k
               .Case("slt", CmpInst::ICMP_SLT)
2270
1.08k
               .Case("sle", CmpInst::ICMP_SLE)
2271
1.08k
               .Case("ugt", CmpInst::ICMP_UGT)
2272
1.08k
               .Case("uge", CmpInst::ICMP_UGE)
2273
1.08k
               .Case("ult", CmpInst::ICMP_ULT)
2274
1.08k
               .Case("ule", CmpInst::ICMP_ULE)
2275
1.08k
               .Default(CmpInst::BAD_ICMP_PREDICATE);
2276
1.08k
    if (!CmpInst::isIntPredicate(Pred))
2277
0
      return error("invalid integer predicate");
2278
1.54k
  }
2279
1.54k
2280
1.54k
  lex();
2281
1.54k
  Dest = MachineOperand::CreatePredicate(Pred);
2282
1.54k
  if (expectAndConsume(MIToken::rparen))
2283
0
    return error("predicate should be terminated by ')'.");
2284
1.54k
2285
1.54k
  return false;
2286
1.54k
}
2287
2288
4
bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2289
4
  assert(Token.is(MIToken::kw_target_index));
2290
4
  lex();
2291
4
  if (expectAndConsume(MIToken::lparen))
2292
0
    return true;
2293
4
  if (Token.isNot(MIToken::Identifier))
2294
1
    return error("expected the name of the target index");
2295
3
  int Index = 0;
2296
3
  if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2297
1
    return error("use of undefined target index '" + Token.stringValue() + "'");
2298
2
  lex();
2299
2
  if (expectAndConsume(MIToken::rparen))
2300
0
    return true;
2301
2
  Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2302
2
  if (parseOperandsOffset(Dest))
2303
0
    return true;
2304
2
  return false;
2305
2
}
2306
2307
1
bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2308
1
  assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2309
1
  lex();
2310
1
  if (expectAndConsume(MIToken::lparen))
2311
0
    return true;
2312
1
2313
1
  uint32_t *Mask = MF.allocateRegMask();
2314
62
  while (true) {
2315
62
    if (Token.isNot(MIToken::NamedRegister))
2316
0
      return error("expected a named register");
2317
62
    unsigned Reg;
2318
62
    if (parseNamedRegister(Reg))
2319
0
      return true;
2320
62
    lex();
2321
62
    Mask[Reg / 32] |= 1U << (Reg % 32);
2322
62
    // TODO: Report an error if the same register is used more than once.
2323
62
    if (Token.isNot(MIToken::comma))
2324
1
      break;
2325
61
    lex();
2326
61
  }
2327
1
2328
1
  if (expectAndConsume(MIToken::rparen))
2329
0
    return true;
2330
1
  Dest = MachineOperand::CreateRegMask(Mask);
2331
1
  return false;
2332
1
}
2333
2334
1
bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2335
1
  assert(Token.is(MIToken::kw_liveout));
2336
1
  uint32_t *Mask = MF.allocateRegMask();
2337
1
  lex();
2338
1
  if (expectAndConsume(MIToken::lparen))
2339
0
    return true;
2340
4
  
while (1
true) {
2341
4
    if (Token.isNot(MIToken::NamedRegister))
2342
0
      return error("expected a named register");
2343
4
    unsigned Reg;
2344
4
    if (parseNamedRegister(Reg))
2345
0
      return true;
2346
4
    lex();
2347
4
    Mask[Reg / 32] |= 1U << (Reg % 32);
2348
4
    // TODO: Report an error if the same register is used more than once.
2349
4
    if (Token.isNot(MIToken::comma))
2350
1
      break;
2351
3
    lex();
2352
3
  }
2353
1
  if (expectAndConsume(MIToken::rparen))
2354
0
    return true;
2355
1
  Dest = MachineOperand::CreateRegLiveOut(Mask);
2356
1
  return false;
2357
1
}
2358
2359
bool MIParser::parseMachineOperand(MachineOperand &Dest,
2360
138k
                                   Optional<unsigned> &TiedDefIdx) {
2361
138k
  switch (Token.kind()) {
2362
138k
  case MIToken::kw_implicit:
2363
98.3k
  case MIToken::kw_implicit_define:
2364
98.3k
  case MIToken::kw_def:
2365
98.3k
  case MIToken::kw_dead:
2366
98.3k
  case MIToken::kw_killed:
2367
98.3k
  case MIToken::kw_undef:
2368
98.3k
  case MIToken::kw_internal:
2369
98.3k
  case MIToken::kw_early_clobber:
2370
98.3k
  case MIToken::kw_debug_use:
2371
98.3k
  case MIToken::kw_renamable:
2372
98.3k
  case MIToken::underscore:
2373
98.3k
  case MIToken::NamedRegister:
2374
98.3k
  case MIToken::VirtualRegister:
2375
98.3k
  case MIToken::NamedVirtualRegister:
2376
98.3k
    return parseRegisterOperand(Dest, TiedDefIdx);
2377
98.3k
  case MIToken::IntegerLiteral:
2378
29.6k
    return parseImmediateOperand(Dest);
2379
98.3k
  case MIToken::kw_half:
2380
125
  case MIToken::kw_float:
2381
125
  case MIToken::kw_double:
2382
125
  case MIToken::kw_x86_fp80:
2383
125
  case MIToken::kw_fp128:
2384
125
  case MIToken::kw_ppc_fp128:
2385
125
    return parseFPImmediateOperand(Dest);
2386
3.32k
  case MIToken::MachineBasicBlock:
2387
3.32k
    return parseMBBOperand(Dest);
2388
364
  case MIToken::StackObject:
2389
364
    return parseStackObjectOperand(Dest);
2390
139
  case MIToken::FixedStackObject:
2391
139
    return parseFixedStackObjectOperand(Dest);
2392
1.07k
  case MIToken::GlobalValue:
2393
1.07k
  case MIToken::NamedGlobalValue:
2394
1.07k
    return parseGlobalAddressOperand(Dest);
2395
1.07k
  case MIToken::ConstantPoolItem:
2396
49
    return parseConstantPoolIndexOperand(Dest);
2397
1.07k
  case MIToken::JumpTableIndex:
2398
26
    return parseJumpTableIndexOperand(Dest);
2399
1.07k
  case MIToken::ExternalSymbol:
2400
251
    return parseExternalSymbolOperand(Dest);
2401
1.07k
  case MIToken::MCSymbol:
2402
19
    return parseMCSymbolOperand(Dest);
2403
1.07k
  case MIToken::SubRegisterIndex:
2404
371
    return parseSubRegisterIndexOperand(Dest);
2405
1.07k
  case MIToken::md_diexpr:
2406
963
  case MIToken::exclaim:
2407
963
    return parseMetadataOperand(Dest);
2408
963
  case MIToken::kw_cfi_same_value:
2409
403
  case MIToken::kw_cfi_offset:
2410
403
  case MIToken::kw_cfi_rel_offset:
2411
403
  case MIToken::kw_cfi_def_cfa_register:
2412
403
  case MIToken::kw_cfi_def_cfa_offset:
2413
403
  case MIToken::kw_cfi_adjust_cfa_offset:
2414
403
  case MIToken::kw_cfi_escape:
2415
403
  case MIToken::kw_cfi_def_cfa:
2416
403
  case MIToken::kw_cfi_register:
2417
403
  case MIToken::kw_cfi_remember_state:
2418
403
  case MIToken::kw_cfi_restore:
2419
403
  case MIToken::kw_cfi_restore_state:
2420
403
  case MIToken::kw_cfi_undefined:
2421
403
  case MIToken::kw_cfi_window_save:
2422
403
  case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2423
403
    return parseCFIOperand(Dest);
2424
403
  case MIToken::kw_blockaddress:
2425
23
    return parseBlockAddressOperand(Dest);
2426
403
  case MIToken::kw_intrinsic:
2427
243
    return parseIntrinsicOperand(Dest);
2428
403
  case MIToken::kw_target_index:
2429
4
    return parseTargetIndexOperand(Dest);
2430
403
  case MIToken::kw_liveout:
2431
1
    return parseLiveoutRegisterMaskOperand(Dest);
2432
1.54k
  case MIToken::kw_floatpred:
2433
1.54k
  case MIToken::kw_intpred:
2434
1.54k
    return parsePredicateOperand(Dest);
2435
1.54k
  case MIToken::Error:
2436
0
    return true;
2437
1.79k
  case MIToken::Identifier:
2438
1.79k
    if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
2439
338
      Dest = MachineOperand::CreateRegMask(RegMask);
2440
338
      lex();
2441
338
      break;
2442
1.45k
    } else if (Token.stringValue() == "CustomRegMask") {
2443
1
      return parseCustomRegisterMaskOperand(Dest);
2444
1
    } else
2445
1.45k
      return parseTypedImmediateOperand(Dest);
2446
1
  default:
2447
1
    // FIXME: Parse the MCSymbol machine operand.
2448
1
    return error("expected a machine operand");
2449
338
  }
2450
338
  return false;
2451
338
}
2452
2453
bool MIParser::parseMachineOperandAndTargetFlags(
2454
138k
    MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
2455
138k
  unsigned TF = 0;
2456
138k
  bool HasTargetFlags = false;
2457
138k
  if (Token.is(MIToken::kw_target_flags)) {
2458
498
    HasTargetFlags = true;
2459
498
    lex();
2460
498
    if (expectAndConsume(MIToken::lparen))
2461
0
      return true;
2462
498
    if (Token.isNot(MIToken::Identifier))
2463
1
      return error("expected the name of the target flag");
2464
497
    if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
2465
4
      if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
2466
1
        return error("use of undefined target flag '" + Token.stringValue() +
2467
1
                     "'");
2468
496
    }
2469
496
    lex();
2470
562
    while (Token.is(MIToken::comma)) {
2471
68
      lex();
2472
68
      if (Token.isNot(MIToken::Identifier))
2473
1
        return error("expected the name of the target flag");
2474
67
      unsigned BitFlag = 0;
2475
67
      if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2476
1
        return error("use of undefined target flag '" + Token.stringValue() +
2477
1
                     "'");
2478
66
      // TODO: Report an error when using a duplicate bit target flag.
2479
66
      TF |= BitFlag;
2480
66
      lex();
2481
66
    }
2482
496
    
if (494
expectAndConsume(MIToken::rparen)494
)
2483
0
      return true;
2484
138k
  }
2485
138k
  auto Loc = Token.location();
2486
138k
  if (parseMachineOperand(Dest, TiedDefIdx))
2487
39
    return true;
2488
138k
  if (!HasTargetFlags)
2489
138k
    return false;
2490
494
  if (Dest.isReg())
2491
1
    return error(Loc, "register operands can't have target flags");
2492
493
  Dest.setTargetFlags(TF);
2493
493
  return false;
2494
493
}
2495
2496
4.53k
bool MIParser::parseOffset(int64_t &Offset) {
2497
4.53k
  if (Token.isNot(MIToken::plus) && 
Token.isNot(MIToken::minus)4.44k
)
2498
4.44k
    return false;
2499
91
  StringRef Sign = Token.range();
2500
91
  bool IsNegative = Token.is(MIToken::minus);
2501
91
  lex();
2502
91
  if (Token.isNot(MIToken::IntegerLiteral))
2503
1
    return error("expected an integer literal after '" + Sign + "'");
2504
90
  if (Token.integerValue().getMinSignedBits() > 64)
2505
1
    return error("expected 64-bit integer (too large)");
2506
89
  Offset = Token.integerValue().getExtValue();
2507
89
  if (IsNegative)
2508
4
    Offset = -Offset;
2509
89
  lex();
2510
89
  return false;
2511
89
}
2512
2513
1.03k
bool MIParser::parseAlignment(unsigned &Alignment) {
2514
1.03k
  assert(Token.is(MIToken::kw_align));
2515
1.03k
  lex();
2516
1.03k
  if (Token.isNot(MIToken::IntegerLiteral) || 
Token.integerValue().isSigned()1.03k
)
2517
2
    return error("expected an integer literal after 'align'");
2518
1.03k
  if (getUnsigned(Alignment))
2519
0
    return true;
2520
1.03k
  lex();
2521
1.03k
2522
1.03k
  if (!isPowerOf2_32(Alignment))
2523
1
    return error("expected a power-of-2 literal after 'align'");
2524
1.03k
2525
1.03k
  return false;
2526
1.03k
}
2527
2528
1.43k
bool MIParser::parseAddrspace(unsigned &Addrspace) {
2529
1.43k
  assert(Token.is(MIToken::kw_addrspace));
2530
1.43k
  lex();
2531
1.43k
  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2532
0
    return error("expected an integer literal after 'addrspace'");
2533
1.43k
  if (getUnsigned(Addrspace))
2534
0
    return true;
2535
1.43k
  lex();
2536
1.43k
  return false;
2537
1.43k
}
2538
2539
1.41k
bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2540
1.41k
  int64_t Offset = 0;
2541
1.41k
  if (parseOffset(Offset))
2542
2
    return true;
2543
1.40k
  Op.setOffset(Offset);
2544
1.40k
  return false;
2545
1.40k
}
2546
2547
2.30k
bool MIParser::parseIRValue(const Value *&V) {
2548
2.30k
  switch (Token.kind()) {
2549
2.30k
  case MIToken::NamedIRValue: {
2550
1.33k
    V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
2551
1.33k
    break;
2552
2.30k
  }
2553
2.30k
  case MIToken::IRValue: {
2554
172
    unsigned SlotNumber = 0;
2555
172
    if (getUnsigned(SlotNumber))
2556
0
      return true;
2557
172
    V = getIRValue(SlotNumber);
2558
172
    break;
2559
172
  }
2560
217
  case MIToken::NamedGlobalValue:
2561
217
  case MIToken::GlobalValue: {
2562
217
    GlobalValue *GV = nullptr;
2563
217
    if (parseGlobalValue(GV))
2564
0
      return true;
2565
217
    V = GV;
2566
217
    break;
2567
217
  }
2568
576
  case MIToken::QuotedIRValue: {
2569
576
    const Constant *C = nullptr;
2570
576
    if (parseIRConstant(Token.location(), Token.stringValue(), C))
2571
0
      return true;
2572
576
    V = C;
2573
576
    break;
2574
576
  }
2575
576
  default:
2576
0
    llvm_unreachable("The current token should be an IR block reference");
2577
2.30k
  }
2578
2.30k
  if (!V)
2579
1
    return error(Twine("use of undefined IR value '") + Token.range() + "'");
2580
2.29k
  return false;
2581
2.29k
}
2582
2583
4.94k
bool MIParser::getUint64(uint64_t &Result) {
2584
4.94k
  if (Token.hasIntegerValue()) {
2585
4.94k
    if (Token.integerValue().getActiveBits() > 64)
2586
1
      return error("expected 64-bit integer (too large)");
2587
4.94k
    Result = Token.integerValue().getZExtValue();
2588
4.94k
    return false;
2589
4.94k
  }
2590
0
  if (Token.is(MIToken::HexLiteral)) {
2591
0
    APInt A;
2592
0
    if (getHexUint(A))
2593
0
      return true;
2594
0
    if (A.getBitWidth() > 64)
2595
0
      return error("expected 64-bit integer (too large)");
2596
0
    Result = A.getZExtValue();
2597
0
    return false;
2598
0
  }
2599
0
  return true;
2600
0
}
2601
2602
1.39k
bool MIParser::getHexUint(APInt &Result) {
2603
1.39k
  assert(Token.is(MIToken::HexLiteral));
2604
1.39k
  StringRef S = Token.range();
2605
1.39k
  assert(S[0] == '0' && tolower(S[1]) == 'x');
2606
1.39k
  // This could be a floating point literal with a special prefix.
2607
1.39k
  if (!isxdigit(S[2]))
2608
0
    return true;
2609
1.39k
  StringRef V = S.substr(2);
2610
1.39k
  APInt A(V.size()*4, V, 16);
2611
1.39k
2612
1.39k
  // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2613
1.39k
  // sure it isn't the case before constructing result.
2614
1.39k
  unsigned NumBits = (A == 0) ? 
320
: A.getActiveBits();
2615
1.39k
  Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2616
1.39k
  return false;
2617
1.39k
}
2618
2619
1.38k
bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2620
1.38k
  const auto OldFlags = Flags;
2621
1.38k
  switch (Token.kind()) {
2622
1.38k
  case MIToken::kw_volatile:
2623
499
    Flags |= MachineMemOperand::MOVolatile;
2624
499
    break;
2625
1.38k
  case MIToken::kw_non_temporal:
2626
60
    Flags |= MachineMemOperand::MONonTemporal;
2627
60
    break;
2628
1.38k
  case MIToken::kw_dereferenceable:
2629
474
    Flags |= MachineMemOperand::MODereferenceable;
2630
474
    break;
2631
1.38k
  case MIToken::kw_invariant:
2632
329
    Flags |= MachineMemOperand::MOInvariant;
2633
329
    break;
2634
1.38k
  case MIToken::StringConstant: {
2635
24
    MachineMemOperand::Flags TF;
2636
24
    if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
2637
1
      return error("use of undefined target MMO flag '" + Token.stringValue() +
2638
1
                   "'");
2639
23
    Flags |= TF;
2640
23
    break;
2641
23
  }
2642
23
  default:
2643
0
    llvm_unreachable("The current token should be a memory operand flag");
2644
1.38k
  }
2645
1.38k
  if (OldFlags == Flags)
2646
1
    // We know that the same flag is specified more than once when the flags
2647
1
    // weren't modified.
2648
1
    return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2649
1.38k
  lex();
2650
1.38k
  return false;
2651
1.38k
}
2652
2653
826
bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2654
826
  switch (Token.kind()) {
2655
826
  case MIToken::kw_stack:
2656
12
    PSV = MF.getPSVManager().getStack();
2657
12
    break;
2658
826
  case MIToken::kw_got:
2659
18
    PSV = MF.getPSVManager().getGOT();
2660
18
    break;
2661
826
  case MIToken::kw_jump_table:
2662
6
    PSV = MF.getPSVManager().getJumpTable();
2663
6
    break;
2664
826
  case MIToken::kw_constant_pool:
2665
31
    PSV = MF.getPSVManager().getConstantPool();
2666
31
    break;
2667
826
  case MIToken::FixedStackObject: {
2668
151
    int FI;
2669
151
    if (parseFixedStackFrameIndex(FI))
2670
0
      return true;
2671
151
    PSV = MF.getPSVManager().getFixedStack(FI);
2672
151
    // The token was already consumed, so use return here instead of break.
2673
151
    return false;
2674
151
  }
2675
592
  case MIToken::StackObject: {
2676
592
    int FI;
2677
592
    if (parseStackFrameIndex(FI))
2678
0
      return true;
2679
592
    PSV = MF.getPSVManager().getFixedStack(FI);
2680
592
    // The token was already consumed, so use return here instead of break.
2681
592
    return false;
2682
592
  }
2683
592
  case MIToken::kw_call_entry:
2684
16
    lex();
2685
16
    switch (Token.kind()) {
2686
16
    case MIToken::GlobalValue:
2687
9
    case MIToken::NamedGlobalValue: {
2688
9
      GlobalValue *GV = nullptr;
2689
9
      if (parseGlobalValue(GV))
2690
0
        return true;
2691
9
      PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2692
9
      break;
2693
9
    }
2694
9
    case MIToken::ExternalSymbol:
2695
6
      PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2696
6
          MF.createExternalSymbolName(Token.stringValue()));
2697
6
      break;
2698
9
    default:
2699
1
      return error(
2700
1
          "expected a global value or an external symbol after 'call-entry'");
2701
15
    }
2702
15
    break;
2703
15
  default:
2704
0
    llvm_unreachable("The current token should be pseudo source value");
2705
82
  }
2706
82
  lex();
2707
82
  return false;
2708
82
}
2709
2710
3.12k
bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2711
3.12k
  if (Token.is(MIToken::kw_constant_pool) || 
Token.is(MIToken::kw_stack)3.09k
||
2712
3.12k
      
Token.is(MIToken::kw_got)3.08k
||
Token.is(MIToken::kw_jump_table)3.06k
||
2713
3.12k
      
Token.is(MIToken::FixedStackObject)3.06k
||
Token.is(MIToken::StackObject)2.90k
||
2714
3.12k
      
Token.is(MIToken::kw_call_entry)2.31k
) {
2715
826
    const PseudoSourceValue *PSV = nullptr;
2716
826
    if (parseMemoryPseudoSourceValue(PSV))
2717
1
      return true;
2718
825
    int64_t Offset = 0;
2719
825
    if (parseOffset(Offset))
2720
0
      return true;
2721
825
    Dest = MachinePointerInfo(PSV, Offset);
2722
825
    return false;
2723
825
  }
2724
2.30k
  if (Token.isNot(MIToken::NamedIRValue) && 
Token.isNot(MIToken::IRValue)966
&&
2725
2.30k
      
Token.isNot(MIToken::GlobalValue)794
&&
2726
2.30k
      
Token.isNot(MIToken::NamedGlobalValue)793
&&
2727
2.30k
      
Token.isNot(MIToken::QuotedIRValue)577
)
2728
1
    return error("expected an IR value reference");
2729
2.30k
  const Value *V = nullptr;
2730
2.30k
  if (parseIRValue(V))
2731
1
    return true;
2732
2.29k
  if (!V->getType()->isPointerTy())
2733
1
    return error("expected a pointer IR value");
2734
2.29k
  lex();
2735
2.29k
  int64_t Offset = 0;
2736
2.29k
  if (parseOffset(Offset))
2737
0
    return true;
2738
2.29k
  Dest = MachinePointerInfo(V, Offset);
2739
2.29k
  return false;
2740
2.29k
}
2741
2742
bool MIParser::parseOptionalScope(LLVMContext &Context,
2743
4.95k
                                  SyncScope::ID &SSID) {
2744
4.95k
  SSID = SyncScope::System;
2745
4.95k
  if (Token.is(MIToken::Identifier) && 
Token.stringValue() == "syncscope"495
) {
2746
101
    lex();
2747
101
    if (expectAndConsume(MIToken::lparen))
2748
0
      return error("expected '(' in syncscope");
2749
101
2750
101
    std::string SSN;
2751
101
    if (parseStringConstant(SSN))
2752
0
      return true;
2753
101
2754
101
    SSID = Context.getOrInsertSyncScopeID(SSN);
2755
101
    if (expectAndConsume(MIToken::rparen))
2756
0
      return error("expected ')' in syncscope");
2757
4.95k
  }
2758
4.95k
2759
4.95k
  return false;
2760
4.95k
}
2761
2762
9.89k
bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2763
9.89k
  Order = AtomicOrdering::NotAtomic;
2764
9.89k
  if (Token.isNot(MIToken::Identifier))
2765
9.37k
    return false;
2766
529
2767
529
  Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2768
529
              .Case("unordered", AtomicOrdering::Unordered)
2769
529
              .Case("monotonic", AtomicOrdering::Monotonic)
2770
529
              .Case("acquire", AtomicOrdering::Acquire)
2771
529
              .Case("release", AtomicOrdering::Release)
2772
529
              .Case("acq_rel", AtomicOrdering::AcquireRelease)
2773
529
              .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2774
529
              .Default(AtomicOrdering::NotAtomic);
2775
529
2776
529
  if (Order != AtomicOrdering::NotAtomic) {
2777
528
    lex();
2778
528
    return false;
2779
528
  }
2780
1
2781
1
  return error("expected an atomic scope, ordering or a size specification");
2782
1
}
2783
2784
4.95k
bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2785
4.95k
  if (expectAndConsume(MIToken::lparen))
2786
0
    return true;
2787
4.95k
  MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
2788
6.33k
  while (Token.isMemoryOperandFlag()) {
2789
1.38k
    if (parseMemoryOperandFlag(Flags))
2790
2
      return true;
2791
1.38k
  }
2792
4.95k
  
if (4.95k
Token.isNot(MIToken::Identifier)4.95k
||
2793
4.95k
      
(4.95k
Token.stringValue() != "load"4.95k
&&
Token.stringValue() != "store"1.94k
))
2794
1
    return error("expected 'load' or 'store' memory operation");
2795
4.95k
  if (Token.stringValue() == "load")
2796
3.00k
    Flags |= MachineMemOperand::MOLoad;
2797
1.94k
  else
2798
1.94k
    Flags |= MachineMemOperand::MOStore;
2799
4.95k
  lex();
2800
4.95k
2801
4.95k
  // Optional 'store' for operands that both load and store.
2802
4.95k
  if (Token.is(MIToken::Identifier) && 
Token.stringValue() == "store"495
) {
2803
270
    Flags |= MachineMemOperand::MOStore;
2804
270
    lex();
2805
270
  }
2806
4.95k
2807
4.95k
  // Optional synchronization scope.
2808
4.95k
  SyncScope::ID SSID;
2809
4.95k
  if (parseOptionalScope(MF.getFunction().getContext(), SSID))
2810
0
    return true;
2811
4.95k
2812
4.95k
  // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2813
4.95k
  AtomicOrdering Order, FailureOrder;
2814
4.95k
  if (parseOptionalAtomicOrdering(Order))
2815
1
    return true;
2816
4.94k
2817
4.94k
  if (parseOptionalAtomicOrdering(FailureOrder))
2818
0
    return true;
2819
4.94k
2820
4.94k
  if (Token.isNot(MIToken::IntegerLiteral) &&
2821
4.94k
      
Token.isNot(MIToken::kw_unknown_size)3
)
2822
1
    return error("expected the size integer literal or 'unknown-size' after "
2823
1
                 "memory operation");
2824
4.94k
  uint64_t Size;
2825
4.94k
  if (Token.is(MIToken::IntegerLiteral)) {
2826
4.94k
    if (getUint64(Size))
2827
1
      return true;
2828
2
  } else if (Token.is(MIToken::kw_unknown_size)) {
2829
2
    Size = MemoryLocation::UnknownSize;
2830
2
  }
2831
4.94k
  lex();
2832
4.94k
2833
4.94k
  MachinePointerInfo Ptr = MachinePointerInfo();
2834
4.94k
  if (Token.is(MIToken::Identifier)) {
2835
3.12k
    const char *Word =
2836
3.12k
        ((Flags & MachineMemOperand::MOLoad) &&
2837
3.12k
         
(Flags & MachineMemOperand::MOStore)1.93k
)
2838
3.12k
            ? 
"on"58
2839
3.12k
            : 
Flags & MachineMemOperand::MOLoad 3.06k
?
"from"1.87k
:
"into"1.19k
;
2840
3.12k
    if (Token.stringValue() != Word)
2841
0
      return error(Twine("expected '") + Word + "'");
2842
3.12k
    lex();
2843
3.12k
2844
3.12k
    if (parseMachinePointerInfo(Ptr))
2845
4
      return true;
2846
4.94k
  }
2847
4.94k
  unsigned BaseAlignment = (Size != MemoryLocation::UnknownSize ? 
Size4.94k
:
12
);
2848
4.94k
  AAMDNodes AAInfo;
2849
4.94k
  MDNode *Range = nullptr;
2850
7.59k
  while (consumeIfPresent(MIToken::comma)) {
2851
2.65k
    switch (Token.kind()) {
2852
2.65k
    case MIToken::kw_align:
2853
1.02k
      if (parseAlignment(BaseAlignment))
2854
3
        return true;
2855
1.02k
      break;
2856
1.43k
    case MIToken::kw_addrspace:
2857
1.43k
      if (parseAddrspace(Ptr.AddrSpace))
2858
0
        return true;
2859
1.43k
      break;
2860
1.43k
    case MIToken::md_tbaa:
2861
189
      lex();
2862
189
      if (parseMDNode(AAInfo.TBAA))
2863
0
        return true;
2864
189
      break;
2865
189
    case MIToken::md_alias_scope:
2866
1
      lex();
2867
1
      if (parseMDNode(AAInfo.Scope))
2868
0
        return true;
2869
1
      break;
2870
1
    case MIToken::md_noalias:
2871
1
      lex();
2872
1
      if (parseMDNode(AAInfo.NoAlias))
2873
0
        return true;
2874
1
      break;
2875
1
    case MIToken::md_range:
2876
1
      lex();
2877
1
      if (parseMDNode(Range))
2878
0
        return true;
2879
1
      break;
2880
1
    // TODO: Report an error on duplicate metadata nodes.
2881
1
    default:
2882
1
      return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2883
1
                   "'!noalias' or '!range'");
2884
2.65k
    }
2885
2.65k
  }
2886
4.94k
  
if (4.93k
expectAndConsume(MIToken::rparen)4.93k
)
2887
0
    return true;
2888
4.93k
  Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2889
4.93k
                                 SSID, Order, FailureOrder);
2890
4.93k
  return false;
2891
4.93k
}
2892
2893
8
bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
2894
8
  assert((Token.is(MIToken::kw_pre_instr_symbol) ||
2895
8
          Token.is(MIToken::kw_post_instr_symbol)) &&
2896
8
         "Invalid token for a pre- post-instruction symbol!");
2897
8
  lex();
2898
8
  if (Token.isNot(MIToken::MCSymbol))
2899
0
    return error("expected a symbol after 'pre-instr-symbol'");
2900
8
  Symbol = getOrCreateMCSymbol(Token.stringValue());
2901
8
  lex();
2902
8
  if (Token.isNewlineOrEOF() || 
Token.is(MIToken::coloncolon)3
||
2903
8
      
Token.is(MIToken::lbrace)3
)
2904
5
    return false;
2905
3
  if (Token.isNot(MIToken::comma))
2906
0
    return error("expected ',' before the next machine operand");
2907
3
  lex();
2908
3
  return false;
2909
3
}
2910
2911
static void initSlots2BasicBlocks(
2912
    const Function &F,
2913
1.35k
    DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2914
1.35k
  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2915
1.35k
  MST.incorporateFunction(F);
2916
1.69k
  for (auto &BB : F) {
2917
1.69k
    if (BB.hasName())
2918
304
      continue;
2919
1.39k
    int Slot = MST.getLocalSlot(&BB);
2920
1.39k
    if (Slot == -1)
2921
0
      continue;
2922
1.39k
    Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2923
1.39k
  }
2924
1.35k
}
2925
2926
static const BasicBlock *getIRBlockFromSlot(
2927
    unsigned Slot,
2928
1.39k
    const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2929
1.39k
  auto BlockInfo = Slots2BasicBlocks.find(Slot);
2930
1.39k
  if (BlockInfo == Slots2BasicBlocks.end())
2931
2
    return nullptr;
2932
1.38k
  return BlockInfo->second;
2933
1.38k
}
2934
2935
1.38k
const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2936
1.38k
  if (Slots2BasicBlocks.empty())
2937
1.35k
    initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
2938
1.38k
  return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2939
1.38k
}
2940
2941
1.39k
const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2942
1.39k
  if (&F == &MF.getFunction())
2943
1.38k
    return getIRBlock(Slot);
2944
1
  DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2945
1
  initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2946
1
  return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2947
1
}
2948
2949
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2950
1.33k
                           DenseMap<unsigned, const Value *> &Slots2Values) {
2951
1.33k
  int Slot = MST.getLocalSlot(V);
2952
1.33k
  if (Slot == -1)
2953
862
    return;
2954
474
  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2955
474
}
2956
2957
/// Creates the mapping from slot numbers to function's unnamed IR values.
2958
static void initSlots2Values(const Function &F,
2959
42
                             DenseMap<unsigned, const Value *> &Slots2Values) {
2960
42
  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2961
42
  MST.incorporateFunction(F);
2962
42
  for (const auto &Arg : F.args())
2963
81
    mapValueToSlot(&Arg, MST, Slots2Values);
2964
123
  for (const auto &BB : F) {
2965
123
    mapValueToSlot(&BB, MST, Slots2Values);
2966
123
    for (const auto &I : BB)
2967
1.13k
      mapValueToSlot(&I, MST, Slots2Values);
2968
123
  }
2969
42
}
2970
2971
172
const Value *MIParser::getIRValue(unsigned Slot) {
2972
172
  if (Slots2Values.empty())
2973
42
    initSlots2Values(MF.getFunction(), Slots2Values);
2974
172
  auto ValueInfo = Slots2Values.find(Slot);
2975
172
  if (ValueInfo == Slots2Values.end())
2976
0
    return nullptr;
2977
172
  return ValueInfo->second;
2978
172
}
2979
2980
27
MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
2981
27
  // FIXME: Currently we can't recognize temporary or local symbols and call all
2982
27
  // of the appropriate forms to create them. However, this handles basic cases
2983
27
  // well as most of the special aspects are recognized by a prefix on their
2984
27
  // name, and the input names should already be unique. For test cases, keeping
2985
27
  // the symbol name out of the symbol table isn't terribly important.
2986
27
  return MF.getContext().getOrCreateSymbol(Name);
2987
27
}
2988
2989
101
bool MIParser::parseStringConstant(std::string &Result) {
2990
101
  if (Token.isNot(MIToken::StringConstant))
2991
0
    return error("expected string constant");
2992
101
  Result = Token.stringValue();
2993
101
  lex();
2994
101
  return false;
2995
101
}
2996
2997
bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2998
                                             StringRef Src,
2999
11.2k
                                             SMDiagnostic &Error) {
3000
11.2k
  return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3001
11.2k
}
3002
3003
bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
3004
11.2k
                                    StringRef Src, SMDiagnostic &Error) {
3005
11.2k
  return MIParser(PFS, Error, Src).parseBasicBlocks();
3006
11.2k
}
3007
3008
bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
3009
                             MachineBasicBlock *&MBB, StringRef Src,
3010
178
                             SMDiagnostic &Error) {
3011
178
  return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3012
178
}
3013
3014
bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
3015
                                  unsigned &Reg, StringRef Src,
3016
64
                                  SMDiagnostic &Error) {
3017
64
  return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3018
64
}
3019
3020
bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
3021
                                       unsigned &Reg, StringRef Src,
3022
25.6k
                                       SMDiagnostic &Error) {
3023
25.6k
  return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3024
25.6k
}
3025
3026
bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
3027
                                         VRegInfo *&Info, StringRef Src,
3028
1.39k
                                         SMDiagnostic &Error) {
3029
1.39k
  return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3030
1.39k
}
3031
3032
bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
3033
                                     int &FI, StringRef Src,
3034
4
                                     SMDiagnostic &Error) {
3035
4
  return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3036
4
}
3037
3038
bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
3039
25
                       MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
3040
25
  return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3041
25
}