Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/CodeGen/MIRParser/MIParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file implements the parsing of machine instructions.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "MILexer.h"
15
#include "MIParser.h"
16
#include "llvm/ADT/APInt.h"
17
#include "llvm/ADT/APSInt.h"
18
#include "llvm/ADT/ArrayRef.h"
19
#include "llvm/ADT/DenseMap.h"
20
#include "llvm/ADT/None.h"
21
#include "llvm/ADT/Optional.h"
22
#include "llvm/ADT/SmallVector.h"
23
#include "llvm/ADT/StringMap.h"
24
#include "llvm/ADT/StringSwitch.h"
25
#include "llvm/ADT/StringRef.h"
26
#include "llvm/ADT/Twine.h"
27
#include "llvm/AsmParser/Parser.h"
28
#include "llvm/AsmParser/SlotMapping.h"
29
#include "llvm/CodeGen/MIRPrinter.h"
30
#include "llvm/CodeGen/MachineBasicBlock.h"
31
#include "llvm/CodeGen/MachineFrameInfo.h"
32
#include "llvm/CodeGen/MachineFunction.h"
33
#include "llvm/CodeGen/MachineInstr.h"
34
#include "llvm/CodeGen/MachineInstrBuilder.h"
35
#include "llvm/CodeGen/MachineMemOperand.h"
36
#include "llvm/CodeGen/MachineModuleInfo.h"
37
#include "llvm/CodeGen/MachineOperand.h"
38
#include "llvm/CodeGen/MachineRegisterInfo.h"
39
#include "llvm/IR/BasicBlock.h"
40
#include "llvm/IR/Constants.h"
41
#include "llvm/IR/DataLayout.h"
42
#include "llvm/IR/DebugInfoMetadata.h"
43
#include "llvm/IR/DebugLoc.h"
44
#include "llvm/IR/Function.h"
45
#include "llvm/IR/InstrTypes.h"
46
#include "llvm/IR/Instructions.h"
47
#include "llvm/IR/Intrinsics.h"
48
#include "llvm/IR/Metadata.h"
49
#include "llvm/IR/Module.h"
50
#include "llvm/IR/ModuleSlotTracker.h"
51
#include "llvm/IR/Type.h"
52
#include "llvm/IR/Value.h"
53
#include "llvm/IR/ValueSymbolTable.h"
54
#include "llvm/MC/LaneBitmask.h"
55
#include "llvm/MC/MCDwarf.h"
56
#include "llvm/MC/MCInstrDesc.h"
57
#include "llvm/MC/MCRegisterInfo.h"
58
#include "llvm/Support/AtomicOrdering.h"
59
#include "llvm/Support/BranchProbability.h"
60
#include "llvm/Support/Casting.h"
61
#include "llvm/Support/ErrorHandling.h"
62
#include "llvm/Support/LowLevelTypeImpl.h"
63
#include "llvm/Support/MemoryBuffer.h"
64
#include "llvm/Support/SMLoc.h"
65
#include "llvm/Support/SourceMgr.h"
66
#include "llvm/Support/raw_ostream.h"
67
#include "llvm/Target/TargetInstrInfo.h"
68
#include "llvm/Target/TargetIntrinsicInfo.h"
69
#include "llvm/Target/TargetMachine.h"
70
#include "llvm/Target/TargetRegisterInfo.h"
71
#include "llvm/Target/TargetSubtargetInfo.h"
72
#include <algorithm>
73
#include <cassert>
74
#include <cctype>
75
#include <cstddef>
76
#include <cstdint>
77
#include <limits>
78
#include <string>
79
#include <utility>
80
81
using namespace llvm;
82
83
PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
84
    SourceMgr &SM, const SlotMapping &IRSlots,
85
    const Name2RegClassMap &Names2RegClasses,
86
    const Name2RegBankMap &Names2RegBanks)
87
  : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
88
2.33k
    Names2RegBanks(Names2RegBanks) {
89
2.33k
}
90
91
22.0k
VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
92
22.0k
  auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
93
22.0k
  if (
I.second22.0k
) {
94
7.72k
    MachineRegisterInfo &MRI = MF.getRegInfo();
95
7.72k
    VRegInfo *Info = new (Allocator) VRegInfo;
96
7.72k
    Info->VReg = MRI.createIncompleteVirtualRegister();
97
7.72k
    I.first->second = Info;
98
7.72k
  }
99
22.0k
  return *I.first->second;
100
22.0k
}
101
102
namespace {
103
104
/// A wrapper struct around the 'MachineOperand' struct that includes a source
105
/// range and other attributes.
106
struct ParsedMachineOperand {
107
  MachineOperand Operand;
108
  StringRef::iterator Begin;
109
  StringRef::iterator End;
110
  Optional<unsigned> TiedDefIdx;
111
112
  ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
113
                       StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
114
52.5k
      : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
115
52.5k
    if (TiedDefIdx)
116
52.5k
      assert(Operand.isReg() && Operand.isUse() &&
117
52.5k
             "Only used register operands can be tied");
118
52.5k
  }
119
};
120
121
class MIParser {
122
  MachineFunction &MF;
123
  SMDiagnostic &Error;
124
  StringRef Source, CurrentSource;
125
  MIToken Token;
126
  PerFunctionMIParsingState &PFS;
127
  /// Maps from instruction names to op codes.
128
  StringMap<unsigned> Names2InstrOpCodes;
129
  /// Maps from register names to registers.
130
  StringMap<unsigned> Names2Regs;
131
  /// Maps from register mask names to register masks.
132
  StringMap<const uint32_t *> Names2RegMasks;
133
  /// Maps from subregister names to subregister indices.
134
  StringMap<unsigned> Names2SubRegIndices;
135
  /// Maps from slot numbers to function's unnamed basic blocks.
136
  DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
137
  /// Maps from slot numbers to function's unnamed values.
138
  DenseMap<unsigned, const Value *> Slots2Values;
139
  /// Maps from target index names to target indices.
140
  StringMap<int> Names2TargetIndices;
141
  /// Maps from direct target flag names to the direct target flag values.
142
  StringMap<unsigned> Names2DirectTargetFlags;
143
  /// Maps from direct target flag names to the bitmask target flag values.
144
  StringMap<unsigned> Names2BitmaskTargetFlags;
145
  /// Maps from MMO target flag names to MMO target flag values.
146
  StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
147
148
public:
149
  MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
150
           StringRef Source);
151
152
  /// \p SkipChar gives the number of characters to skip before looking
153
  /// for the next token.
154
  void lex(unsigned SkipChar = 0);
155
156
  /// Report an error at the current location with the given message.
157
  ///
158
  /// This function always return true.
159
  bool error(const Twine &Msg);
160
161
  /// Report an error at the given location with the given message.
162
  ///
163
  /// This function always return true.
164
  bool error(StringRef::iterator Loc, const Twine &Msg);
165
166
  bool
167
  parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
168
  bool parseBasicBlocks();
169
  bool parse(MachineInstr *&MI);
170
  bool parseStandaloneMBB(MachineBasicBlock *&MBB);
171
  bool parseStandaloneNamedRegister(unsigned &Reg);
172
  bool parseStandaloneVirtualRegister(VRegInfo *&Info);
173
  bool parseStandaloneRegister(unsigned &Reg);
174
  bool parseStandaloneStackObject(int &FI);
175
  bool parseStandaloneMDNode(MDNode *&Node);
176
177
  bool
178
  parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
179
  bool parseBasicBlock(MachineBasicBlock &MBB,
180
                       MachineBasicBlock *&AddFalthroughFrom);
181
  bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
182
  bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
183
184
  bool parseNamedRegister(unsigned &Reg);
185
  bool parseVirtualRegister(VRegInfo *&Info);
186
  bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
187
  bool parseRegisterFlag(unsigned &Flags);
188
  bool parseRegisterClassOrBank(VRegInfo &RegInfo);
189
  bool parseSubRegisterIndex(unsigned &SubReg);
190
  bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
191
  bool parseRegisterOperand(MachineOperand &Dest,
192
                            Optional<unsigned> &TiedDefIdx, bool IsDef = false);
193
  bool parseImmediateOperand(MachineOperand &Dest);
194
  bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
195
                       const Constant *&C);
196
  bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
197
  bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
198
  bool parseTypedImmediateOperand(MachineOperand &Dest);
199
  bool parseFPImmediateOperand(MachineOperand &Dest);
200
  bool parseMBBReference(MachineBasicBlock *&MBB);
201
  bool parseMBBOperand(MachineOperand &Dest);
202
  bool parseStackFrameIndex(int &FI);
203
  bool parseStackObjectOperand(MachineOperand &Dest);
204
  bool parseFixedStackFrameIndex(int &FI);
205
  bool parseFixedStackObjectOperand(MachineOperand &Dest);
206
  bool parseGlobalValue(GlobalValue *&GV);
207
  bool parseGlobalAddressOperand(MachineOperand &Dest);
208
  bool parseConstantPoolIndexOperand(MachineOperand &Dest);
209
  bool parseSubRegisterIndexOperand(MachineOperand &Dest);
210
  bool parseJumpTableIndexOperand(MachineOperand &Dest);
211
  bool parseExternalSymbolOperand(MachineOperand &Dest);
212
  bool parseMDNode(MDNode *&Node);
213
  bool parseDIExpression(MDNode *&Node);
214
  bool parseMetadataOperand(MachineOperand &Dest);
215
  bool parseCFIOffset(int &Offset);
216
  bool parseCFIRegister(unsigned &Reg);
217
  bool parseCFIOperand(MachineOperand &Dest);
218
  bool parseIRBlock(BasicBlock *&BB, const Function &F);
219
  bool parseBlockAddressOperand(MachineOperand &Dest);
220
  bool parseIntrinsicOperand(MachineOperand &Dest);
221
  bool parsePredicateOperand(MachineOperand &Dest);
222
  bool parseTargetIndexOperand(MachineOperand &Dest);
223
  bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
224
  bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
225
  bool parseMachineOperand(MachineOperand &Dest,
226
                           Optional<unsigned> &TiedDefIdx);
227
  bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
228
                                         Optional<unsigned> &TiedDefIdx);
229
  bool parseOffset(int64_t &Offset);
230
  bool parseAlignment(unsigned &Alignment);
231
  bool parseOperandsOffset(MachineOperand &Op);
232
  bool parseIRValue(const Value *&V);
233
  bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
234
  bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
235
  bool parseMachinePointerInfo(MachinePointerInfo &Dest);
236
  bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
237
  bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
238
  bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
239
240
private:
241
  /// Convert the integer literal in the current token into an unsigned integer.
242
  ///
243
  /// Return true if an error occurred.
244
  bool getUnsigned(unsigned &Result);
245
246
  /// Convert the integer literal in the current token into an uint64.
247
  ///
248
  /// Return true if an error occurred.
249
  bool getUint64(uint64_t &Result);
250
251
  /// Convert the hexadecimal literal in the current token into an unsigned
252
  ///  APInt with a minimum bitwidth required to represent the value.
253
  ///
254
  /// Return true if the literal does not represent an integer value.
255
  bool getHexUint(APInt &Result);
256
257
  /// If the current token is of the given kind, consume it and return false.
258
  /// Otherwise report an error and return true.
259
  bool expectAndConsume(MIToken::TokenKind TokenKind);
260
261
  /// If the current token is of the given kind, consume it and return true.
262
  /// Otherwise return false.
263
  bool consumeIfPresent(MIToken::TokenKind TokenKind);
264
265
  void initNames2InstrOpCodes();
266
267
  /// Try to convert an instruction name to an opcode. Return true if the
268
  /// instruction name is invalid.
269
  bool parseInstrName(StringRef InstrName, unsigned &OpCode);
270
271
  bool parseInstruction(unsigned &OpCode, unsigned &Flags);
272
273
  bool assignRegisterTies(MachineInstr &MI,
274
                          ArrayRef<ParsedMachineOperand> Operands);
275
276
  bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
277
                              const MCInstrDesc &MCID);
278
279
  void initNames2Regs();
280
281
  /// Try to convert a register name to a register number. Return true if the
282
  /// register name is invalid.
283
  bool getRegisterByName(StringRef RegName, unsigned &Reg);
284
285
  void initNames2RegMasks();
286
287
  /// Check if the given identifier is a name of a register mask.
288
  ///
289
  /// Return null if the identifier isn't a register mask.
290
  const uint32_t *getRegMask(StringRef Identifier);
291
292
  void initNames2SubRegIndices();
293
294
  /// Check if the given identifier is a name of a subregister index.
295
  ///
296
  /// Return 0 if the name isn't a subregister index class.
297
  unsigned getSubRegIndex(StringRef Name);
298
299
  const BasicBlock *getIRBlock(unsigned Slot);
300
  const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
301
302
  const Value *getIRValue(unsigned Slot);
303
304
  void initNames2TargetIndices();
305
306
  /// Try to convert a name of target index to the corresponding target index.
307
  ///
308
  /// Return true if the name isn't a name of a target index.
309
  bool getTargetIndex(StringRef Name, int &Index);
310
311
  void initNames2DirectTargetFlags();
312
313
  /// Try to convert a name of a direct target flag to the corresponding
314
  /// target flag.
315
  ///
316
  /// Return true if the name isn't a name of a direct flag.
317
  bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
318
319
  void initNames2BitmaskTargetFlags();
320
321
  /// Try to convert a name of a bitmask target flag to the corresponding
322
  /// target flag.
323
  ///
324
  /// Return true if the name isn't a name of a bitmask target flag.
325
  bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
326
327
  void initNames2MMOTargetFlags();
328
329
  /// Try to convert a name of a MachineMemOperand target flag to the
330
  /// corresponding target flag.
331
  ///
332
  /// Return true if the name isn't a name of a target MMO flag.
333
  bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag);
334
335
  /// parseStringConstant
336
  ///   ::= StringConstant
337
  bool parseStringConstant(std::string &Result);
338
};
339
340
} // end anonymous namespace
341
342
MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
343
                   StringRef Source)
344
    : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
345
6.21k
{}
346
347
429k
void MIParser::lex(unsigned SkipChar) {
348
429k
  CurrentSource = lexMIToken(
349
429k
      CurrentSource.data() + SkipChar, Token,
350
4
      [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
351
429k
}
352
353
78
bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
354
355
96
bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
356
96
  const SourceMgr &SM = *PFS.SM;
357
96
  assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
358
96
  const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
359
96
  if (
Loc >= Buffer.getBufferStart() && 96
Loc <= Buffer.getBufferEnd()95
) {
360
90
    // Create an ordinary diagnostic when the source manager's buffer is the
361
90
    // source string.
362
90
    Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
363
90
    return true;
364
90
  }
365
6
  // Create a diagnostic for a YAML string literal.
366
6
  Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
367
6
                       Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
368
6
                       Source, None, None);
369
6
  return true;
370
6
}
371
372
2
static const char *toString(MIToken::TokenKind TokenKind) {
373
2
  switch (TokenKind) {
374
1
  case MIToken::comma:
375
1
    return "','";
376
0
  case MIToken::equal:
377
0
    return "'='";
378
1
  case MIToken::colon:
379
1
    return "':'";
380
0
  case MIToken::lparen:
381
0
    return "'('";
382
0
  case MIToken::rparen:
383
0
    return "')'";
384
0
  default:
385
0
    return "<unknown token>";
386
0
  }
387
0
}
388
389
29.2k
bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
390
29.2k
  if (Token.isNot(TokenKind))
391
2
    return error(Twine("expected ") + toString(TokenKind));
392
29.2k
  lex();
393
29.2k
  return false;
394
29.2k
}
395
396
320k
bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
397
320k
  if (Token.isNot(TokenKind))
398
255k
    return false;
399
65.6k
  lex();
400
65.6k
  return true;
401
65.6k
}
402
403
bool MIParser::parseBasicBlockDefinition(
404
3.29k
    DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
405
3.29k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
406
3.29k
  unsigned ID = 0;
407
3.29k
  if (getUnsigned(ID))
408
0
    return true;
409
3.29k
  auto Loc = Token.location();
410
3.29k
  auto Name = Token.stringValue();
411
3.29k
  lex();
412
3.29k
  bool HasAddressTaken = false;
413
3.29k
  bool IsLandingPad = false;
414
3.29k
  unsigned Alignment = 0;
415
3.29k
  BasicBlock *BB = nullptr;
416
3.29k
  if (
consumeIfPresent(MIToken::lparen)3.29k
) {
417
745
    do {
418
745
      // TODO: Report an error when multiple same attributes are specified.
419
745
      switch (Token.kind()) {
420
12
      case MIToken::kw_address_taken:
421
12
        HasAddressTaken = true;
422
12
        lex();
423
12
        break;
424
0
      case MIToken::kw_landing_pad:
425
0
        IsLandingPad = true;
426
0
        lex();
427
0
        break;
428
4
      case MIToken::kw_align:
429
4
        if (parseAlignment(Alignment))
430
0
          return true;
431
4
        break;
432
729
      case MIToken::IRBlock:
433
729
        // TODO: Report an error when both name and ir block are specified.
434
729
        if (parseIRBlock(BB, *MF.getFunction()))
435
1
          return true;
436
728
        lex();
437
728
        break;
438
0
      default:
439
0
        break;
440
744
      }
441
744
    } while (consumeIfPresent(MIToken::comma));
442
742
    
if (742
expectAndConsume(MIToken::rparen)742
)
443
0
      return true;
444
3.29k
  }
445
3.29k
  
if (3.29k
expectAndConsume(MIToken::colon)3.29k
)
446
1
    return true;
447
3.29k
448
3.29k
  
if (3.29k
!Name.empty()3.29k
) {
449
947
    BB = dyn_cast_or_null<BasicBlock>(
450
947
        MF.getFunction()->getValueSymbolTable()->lookup(Name));
451
947
    if (!BB)
452
1
      return error(Loc, Twine("basic block '") + Name +
453
1
                            "' is not defined in the function '" +
454
1
                            MF.getName() + "'");
455
3.29k
  }
456
3.29k
  auto *MBB = MF.CreateMachineBasicBlock(BB);
457
3.29k
  MF.insert(MF.end(), MBB);
458
3.29k
  bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
459
3.29k
  if (!WasInserted)
460
1
    return error(Loc, Twine("redefinition of machine basic block with id #") +
461
1
                          Twine(ID));
462
3.29k
  
if (3.29k
Alignment3.29k
)
463
4
    MBB->setAlignment(Alignment);
464
3.29k
  if (HasAddressTaken)
465
12
    MBB->setHasAddressTaken();
466
3.29k
  MBB->setIsEHPad(IsLandingPad);
467
3.29k
  return false;
468
3.29k
}
469
470
bool MIParser::parseBasicBlockDefinitions(
471
2.32k
    DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
472
2.32k
  lex();
473
2.32k
  // Skip until the first machine basic block.
474
2.36k
  while (Token.is(MIToken::Newline))
475
43
    lex();
476
2.32k
  if (Token.isErrorOrEOF())
477
11
    return Token.isError();
478
2.31k
  
if (2.31k
Token.isNot(MIToken::MachineBasicBlockLabel)2.31k
)
479
1
    return error("expected a basic block definition before instructions");
480
2.31k
  unsigned BraceDepth = 0;
481
3.29k
  do {
482
3.29k
    if (parseBasicBlockDefinition(MBBSlots))
483
4
      return true;
484
3.29k
    bool IsAfterNewline = false;
485
3.29k
    // Skip until the next machine basic block.
486
206k
    while (
true206k
) {
487
206k
      if (
(Token.is(MIToken::MachineBasicBlockLabel) && 206k
IsAfterNewline986
) ||
488
205k
          Token.isErrorOrEOF())
489
3.28k
        break;
490
203k
      else 
if (203k
Token.is(MIToken::MachineBasicBlockLabel)203k
)
491
1
        return error("basic block definition should be located at the start of "
492
1
                     "the line");
493
203k
      else 
if (203k
consumeIfPresent(MIToken::Newline)203k
) {
494
35.1k
        IsAfterNewline = true;
495
35.1k
        continue;
496
35.1k
      }
497
168k
      IsAfterNewline = false;
498
168k
      if (Token.is(MIToken::lbrace))
499
7
        ++BraceDepth;
500
168k
      if (
Token.is(MIToken::rbrace)168k
) {
501
7
        if (!BraceDepth)
502
1
          return error("extraneous closing brace ('}')");
503
6
        --BraceDepth;
504
6
      }
505
168k
      lex();
506
168k
    }
507
3.29k
    // Verify that we closed all of the '{' at the end of a file or a block.
508
3.28k
    
if (3.28k
!Token.isError() && 3.28k
BraceDepth3.28k
)
509
1
      return error("expected '}'"); // FIXME: Report a note that shows '{'.
510
3.28k
  } while (!Token.isErrorOrEOF());
511
2.30k
  return Token.isError();
512
2.32k
}
513
514
1.96k
bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
515
1.96k
  assert(Token.is(MIToken::kw_liveins));
516
1.96k
  lex();
517
1.96k
  if (expectAndConsume(MIToken::colon))
518
0
    return true;
519
1.96k
  
if (1.96k
Token.isNewlineOrEOF()1.96k
) // Allow an empty list of liveins.
520
4
    return false;
521
1.96k
  
do 1.96k
{
522
3.83k
    if (Token.isNot(MIToken::NamedRegister))
523
1
      return error("expected a named register");
524
3.83k
    unsigned Reg = 0;
525
3.83k
    if (parseNamedRegister(Reg))
526
0
      return true;
527
3.83k
    lex();
528
3.83k
    LaneBitmask Mask = LaneBitmask::getAll();
529
3.83k
    if (
consumeIfPresent(MIToken::colon)3.83k
) {
530
19
      // Parse lane mask.
531
19
      if (Token.isNot(MIToken::IntegerLiteral) &&
532
18
          Token.isNot(MIToken::HexLiteral))
533
0
        return error("expected a lane mask");
534
19
      static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
535
19
                    "Use correct get-function for lane mask");
536
19
      LaneBitmask::Type V;
537
19
      if (getUnsigned(V))
538
0
        return error("invalid lane mask value");
539
19
      Mask = LaneBitmask(V);
540
19
      lex();
541
19
    }
542
3.83k
    MBB.addLiveIn(Reg, Mask);
543
3.83k
  } while (consumeIfPresent(MIToken::comma));
544
1.96k
  return false;
545
1.96k
}
546
547
442
bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
548
442
  assert(Token.is(MIToken::kw_successors));
549
442
  lex();
550
442
  if (expectAndConsume(MIToken::colon))
551
0
    return true;
552
442
  
if (442
Token.isNewlineOrEOF()442
) // Allow an empty list of successors.
553
11
    return false;
554
431
  
do 431
{
555
691
    if (Token.isNot(MIToken::MachineBasicBlock))
556
1
      return error("expected a machine basic block reference");
557
690
    MachineBasicBlock *SuccMBB = nullptr;
558
690
    if (parseMBBReference(SuccMBB))
559
0
      return true;
560
690
    lex();
561
690
    unsigned Weight = 0;
562
690
    if (
consumeIfPresent(MIToken::lparen)690
) {
563
413
      if (Token.isNot(MIToken::IntegerLiteral) &&
564
383
          Token.isNot(MIToken::HexLiteral))
565
1
        return error("expected an integer literal after '('");
566
412
      
if (412
getUnsigned(Weight)412
)
567
0
        return true;
568
412
      lex();
569
412
      if (expectAndConsume(MIToken::rparen))
570
0
        return true;
571
689
    }
572
689
    MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
573
689
  } while (consumeIfPresent(MIToken::comma));
574
429
  MBB.normalizeSuccProbs();
575
429
  return false;
576
442
}
577
578
bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
579
3.23k
                               MachineBasicBlock *&AddFalthroughFrom) {
580
3.23k
  // Skip the definition.
581
3.23k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
582
3.23k
  lex();
583
3.23k
  if (
consumeIfPresent(MIToken::lparen)3.23k
) {
584
1.48k
    while (
Token.isNot(MIToken::rparen) && 1.48k
!Token.isErrorOrEOF()745
)
585
745
      lex();
586
737
    consumeIfPresent(MIToken::rparen);
587
737
  }
588
3.23k
  consumeIfPresent(MIToken::colon);
589
3.23k
590
3.23k
  // Parse the liveins and successors.
591
3.23k
  // N.B: Multiple lists of successors and liveins are allowed and they're
592
3.23k
  // merged into one.
593
3.23k
  // Example:
594
3.23k
  //   liveins: %edi
595
3.23k
  //   liveins: %esi
596
3.23k
  //
597
3.23k
  // is equivalent to
598
3.23k
  //   liveins: %edi, %esi
599
3.23k
  bool ExplicitSuccessors = false;
600
12.1k
  while (
true12.1k
) {
601
12.1k
    if (
Token.is(MIToken::kw_successors)12.1k
) {
602
442
      if (parseBasicBlockSuccessors(MBB))
603
2
        return true;
604
440
      ExplicitSuccessors = true;
605
12.1k
    } else 
if (11.6k
Token.is(MIToken::kw_liveins)11.6k
) {
606
1.96k
      if (parseBasicBlockLiveins(MBB))
607
1
        return true;
608
9.72k
    } else 
if (9.72k
consumeIfPresent(MIToken::Newline)9.72k
) {
609
6.50k
      continue;
610
6.50k
    } else
611
3.22k
      break;
612
2.40k
    
if (2.40k
!Token.isNewlineOrEOF()2.40k
)
613
1
      return error("expected line break at the end of a list");
614
2.40k
    lex();
615
2.40k
  }
616
3.23k
617
3.23k
  // Parse the instructions.
618
3.22k
  bool IsInBundle = false;
619
3.22k
  MachineInstr *PrevMI = nullptr;
620
28.9k
  while (!Token.is(MIToken::MachineBasicBlockLabel) &&
621
28.0k
         
!Token.is(MIToken::Eof)28.0k
) {
622
25.7k
    if (consumeIfPresent(MIToken::Newline))
623
8.64k
      continue;
624
17.1k
    
if (17.1k
consumeIfPresent(MIToken::rbrace)17.1k
) {
625
4
      // The first parsing pass should verify that all closing '}' have an
626
4
      // opening '{'.
627
4
      assert(IsInBundle);
628
4
      IsInBundle = false;
629
4
      continue;
630
4
    }
631
17.1k
    MachineInstr *MI = nullptr;
632
17.1k
    if (parse(MI))
633
70
      return true;
634
17.0k
    MBB.insert(MBB.end(), MI);
635
17.0k
    if (
IsInBundle17.0k
) {
636
12
      PrevMI->setFlag(MachineInstr::BundledSucc);
637
12
      MI->setFlag(MachineInstr::BundledPred);
638
12
    }
639
17.0k
    PrevMI = MI;
640
17.0k
    if (
Token.is(MIToken::lbrace)17.0k
) {
641
6
      if (IsInBundle)
642
1
        return error("nested instruction bundles are not allowed");
643
5
      lex();
644
5
      // This instruction is the start of the bundle.
645
5
      MI->setFlag(MachineInstr::BundledSucc);
646
5
      IsInBundle = true;
647
5
      if (!Token.is(MIToken::Newline))
648
5
        // The next instruction can be on the same line.
649
1
        continue;
650
17.0k
    }
651
17.0k
    assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
652
17.0k
    lex();
653
17.0k
  }
654
3.22k
655
3.22k
  // Construct successor list by searching for basic block machine operands.
656
3.15k
  
if (3.15k
!ExplicitSuccessors3.15k
) {
657
2.72k
    SmallVector<MachineBasicBlock*,4> Successors;
658
2.72k
    bool IsFallthrough;
659
2.72k
    guessSuccessors(MBB, Successors, IsFallthrough);
660
2.72k
    for (MachineBasicBlock *Succ : Successors)
661
354
      MBB.addSuccessor(Succ);
662
2.72k
663
2.72k
    if (
IsFallthrough2.72k
) {
664
784
      AddFalthroughFrom = &MBB;
665
2.72k
    } else {
666
1.94k
      MBB.normalizeSuccProbs();
667
1.94k
    }
668
2.72k
  }
669
3.15k
670
3.15k
  return false;
671
3.23k
}
672
673
2.30k
bool MIParser::parseBasicBlocks() {
674
2.30k
  lex();
675
2.30k
  // Skip until the first machine basic block.
676
2.34k
  while (Token.is(MIToken::Newline))
677
38
    lex();
678
2.30k
  if (Token.isErrorOrEOF())
679
11
    return Token.isError();
680
2.29k
  // The first parsing pass should have verified that this token is a MBB label
681
2.29k
  // in the 'parseBasicBlockDefinitions' method.
682
2.30k
  assert(Token.is(MIToken::MachineBasicBlockLabel));
683
2.29k
  MachineBasicBlock *AddFalthroughFrom = nullptr;
684
3.23k
  do {
685
3.23k
    MachineBasicBlock *MBB = nullptr;
686
3.23k
    if (parseMBBReference(MBB))
687
0
      return true;
688
3.23k
    
if (3.23k
AddFalthroughFrom3.23k
) {
689
251
      if (!AddFalthroughFrom->isSuccessor(MBB))
690
245
        AddFalthroughFrom->addSuccessor(MBB);
691
251
      AddFalthroughFrom->normalizeSuccProbs();
692
251
      AddFalthroughFrom = nullptr;
693
251
    }
694
3.23k
    if (parseBasicBlock(*MBB, AddFalthroughFrom))
695
75
      return true;
696
3.15k
    // The method 'parseBasicBlock' should parse the whole block until the next
697
3.15k
    // block or the end of file.
698
3.23k
    assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
699
3.15k
  } while (Token.isNot(MIToken::Eof));
700
2.21k
  return false;
701
2.30k
}
702
703
17.1k
bool MIParser::parse(MachineInstr *&MI) {
704
17.1k
  // Parse any register operands before '='
705
17.1k
  MachineOperand MO = MachineOperand::CreateImm(0);
706
17.1k
  SmallVector<ParsedMachineOperand, 8> Operands;
707
17.2k
  while (
Token.isRegister() || 17.2k
Token.isRegisterFlag()4.77k
) {
708
12.6k
    auto Loc = Token.location();
709
12.6k
    Optional<unsigned> TiedDefIdx;
710
12.6k
    if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
711
6
      return true;
712
12.6k
    Operands.push_back(
713
12.6k
        ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
714
12.6k
    if (Token.isNot(MIToken::comma))
715
12.5k
      break;
716
136
    lex();
717
136
  }
718
17.1k
  
if (17.1k
!Operands.empty() && 17.1k
expectAndConsume(MIToken::equal)12.5k
)
719
0
    return true;
720
17.1k
721
17.1k
  unsigned OpCode, Flags = 0;
722
17.1k
  if (
Token.isError() || 17.1k
parseInstruction(OpCode, Flags)17.1k
)
723
1
    return true;
724
17.1k
725
17.1k
  // Parse the remaining machine operands.
726
41.1k
  
while (17.1k
!Token.isNewlineOrEOF() && 41.1k
Token.isNot(MIToken::kw_debug_location)40.3k
&&
727
41.1k
         
Token.isNot(MIToken::coloncolon)39.9k
&&
Token.isNot(MIToken::lbrace)39.9k
) {
728
39.9k
    auto Loc = Token.location();
729
39.9k
    Optional<unsigned> TiedDefIdx;
730
39.9k
    if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
731
42
      return true;
732
39.8k
    Operands.push_back(
733
39.8k
        ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
734
39.8k
    if (
Token.isNewlineOrEOF() || 39.8k
Token.is(MIToken::coloncolon)24.8k
||
735
23.9k
        Token.is(MIToken::lbrace))
736
15.8k
      break;
737
23.9k
    
if (23.9k
Token.isNot(MIToken::comma)23.9k
)
738
1
      return error("expected ',' before the next machine operand");
739
23.9k
    lex();
740
23.9k
  }
741
17.1k
742
17.0k
  DebugLoc DebugLocation;
743
17.0k
  if (
Token.is(MIToken::kw_debug_location)17.0k
) {
744
422
    lex();
745
422
    if (Token.isNot(MIToken::exclaim))
746
1
      return error("expected a metadata node after 'debug-location'");
747
421
    MDNode *Node = nullptr;
748
421
    if (parseMDNode(Node))
749
0
      return true;
750
421
    DebugLocation = DebugLoc(Node);
751
421
  }
752
17.0k
753
17.0k
  // Parse the machine memory operands.
754
17.0k
  SmallVector<MachineMemOperand *, 2> MemOperands;
755
17.0k
  if (
Token.is(MIToken::coloncolon)17.0k
) {
756
974
    lex();
757
994
    while (
!Token.isNewlineOrEOF()994
) {
758
994
      MachineMemOperand *MemOp = nullptr;
759
994
      if (parseMachineMemoryOperand(MemOp))
760
12
        return true;
761
982
      MemOperands.push_back(MemOp);
762
982
      if (Token.isNewlineOrEOF())
763
961
        break;
764
21
      
if (21
Token.isNot(MIToken::comma)21
)
765
1
        return error("expected ',' before the next machine memory operand");
766
20
      lex();
767
20
    }
768
974
  }
769
17.0k
770
17.0k
  const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
771
17.0k
  if (
!MCID.isVariadic()17.0k
) {
772
15.4k
    // FIXME: Move the implicit operand verification to the machine verifier.
773
15.4k
    if (verifyImplicitOperands(Operands, MCID))
774
3
      return true;
775
17.0k
  }
776
17.0k
777
17.0k
  // TODO: Check for extraneous machine operands.
778
17.0k
  MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
779
17.0k
  MI->setFlags(Flags);
780
17.0k
  for (const auto &Operand : Operands)
781
52.3k
    MI->addOperand(MF, Operand.Operand);
782
17.0k
  if (assignRegisterTies(*MI, Operands))
783
3
    return true;
784
17.0k
  
if (17.0k
MemOperands.empty()17.0k
)
785
16.1k
    return false;
786
961
  MachineInstr::mmo_iterator MemRefs =
787
961
      MF.allocateMemRefsArray(MemOperands.size());
788
961
  std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
789
961
  MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
790
961
  return false;
791
961
}
792
793
87
bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
794
87
  lex();
795
87
  if (Token.isNot(MIToken::MachineBasicBlock))
796
0
    return error("expected a machine basic block reference");
797
87
  
if (87
parseMBBReference(MBB)87
)
798
0
    return true;
799
87
  lex();
800
87
  if (Token.isNot(MIToken::Eof))
801
0
    return error(
802
0
        "expected end of string after the machine basic block reference");
803
87
  return false;
804
87
}
805
806
1.32k
bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
807
1.32k
  lex();
808
1.32k
  if (Token.isNot(MIToken::NamedRegister))
809
2
    return error("expected a named register");
810
1.32k
  
if (1.32k
parseNamedRegister(Reg)1.32k
)
811
1
    return true;
812
1.32k
  lex();
813
1.32k
  if (Token.isNot(MIToken::Eof))
814
0
    return error("expected end of string after the register reference");
815
1.32k
  return false;
816
1.32k
}
817
818
161
bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
819
161
  lex();
820
161
  if (Token.isNot(MIToken::VirtualRegister))
821
1
    return error("expected a virtual register");
822
160
  
if (160
parseVirtualRegister(Info)160
)
823
0
    return true;
824
160
  lex();
825
160
  if (Token.isNot(MIToken::Eof))
826
0
    return error("expected end of string after the register reference");
827
160
  return false;
828
160
}
829
830
9
bool MIParser::parseStandaloneRegister(unsigned &Reg) {
831
9
  lex();
832
9
  if (Token.isNot(MIToken::NamedRegister) &&
833
5
      Token.isNot(MIToken::VirtualRegister))
834
0
    return error("expected either a named or virtual register");
835
9
836
9
  VRegInfo *Info;
837
9
  if (parseRegister(Reg, Info))
838
0
    return true;
839
9
840
9
  lex();
841
9
  if (Token.isNot(MIToken::Eof))
842
0
    return error("expected end of string after the register reference");
843
9
  return false;
844
9
}
845
846
2
bool MIParser::parseStandaloneStackObject(int &FI) {
847
2
  lex();
848
2
  if (Token.isNot(MIToken::StackObject))
849
1
    return error("expected a stack object");
850
1
  
if (1
parseStackFrameIndex(FI)1
)
851
0
    return true;
852
1
  
if (1
Token.isNot(MIToken::Eof)1
)
853
0
    return error("expected end of string after the stack object reference");
854
1
  return false;
855
1
}
856
857
7
bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
858
7
  lex();
859
7
  if (
Token.is(MIToken::exclaim)7
) {
860
5
    if (parseMDNode(Node))
861
0
      return true;
862
2
  } else 
if (2
Token.is(MIToken::md_diexpr)2
) {
863
1
    if (parseDIExpression(Node))
864
0
      return true;
865
2
  } else
866
1
    return error("expected a metadata node");
867
6
  
if (6
Token.isNot(MIToken::Eof)6
)
868
0
    return error("expected end of string after the metadata node");
869
6
  return false;
870
6
}
871
872
3
static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
873
3
  assert(MO.isImplicit());
874
3
  return MO.isDef() ? 
"implicit-def"0
:
"implicit"3
;
875
3
}
876
877
static std::string getRegisterName(const TargetRegisterInfo *TRI,
878
3
                                   unsigned Reg) {
879
3
  assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
880
3
  return StringRef(TRI->getName(Reg)).lower();
881
3
}
882
883
/// Return true if the parsed machine operands contain a given machine operand.
884
static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
885
2.94k
                                ArrayRef<ParsedMachineOperand> Operands) {
886
13.7k
  for (const auto &I : Operands) {
887
13.7k
    if (ImplicitOperand.isIdenticalTo(I.Operand))
888
2.94k
      return true;
889
3
  }
890
3
  return false;
891
3
}
892
893
bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
894
15.4k
                                      const MCInstrDesc &MCID) {
895
15.4k
  if (MCID.isCall())
896
15.4k
    // We can't verify call instructions as they can contain arbitrary implicit
897
15.4k
    // register and register mask operands.
898
111
    return false;
899
15.3k
900
15.3k
  // Gather all the expected implicit operands.
901
15.3k
  SmallVector<MachineOperand, 4> ImplicitOperands;
902
15.3k
  if (MCID.ImplicitDefs)
903
1.94k
    
for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); 921
*ImpDefs1.94k
;
++ImpDefs1.02k
)
904
1.02k
      ImplicitOperands.push_back(
905
1.02k
          MachineOperand::CreateReg(*ImpDefs, true, true));
906
15.3k
  if (MCID.ImplicitUses)
907
3.61k
    
for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); 1.69k
*ImpUses3.61k
;
++ImpUses1.92k
)
908
1.92k
      ImplicitOperands.push_back(
909
1.92k
          MachineOperand::CreateReg(*ImpUses, false, true));
910
15.3k
911
15.3k
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
912
15.3k
  assert(TRI && "Expected target register info");
913
2.94k
  for (const auto &I : ImplicitOperands) {
914
2.94k
    if (isImplicitOperandIn(I, Operands))
915
2.94k
      continue;
916
3
    
return error(Operands.empty() ? 3
Token.location()0
:
Operands.back().End3
,
917
2.94k
                 Twine("missing implicit register operand '") +
918
2.94k
                     printImplicitRegisterFlag(I) + " %" +
919
2.94k
                     getRegisterName(TRI, I.getReg()) + "'");
920
2.94k
  }
921
15.3k
  return false;
922
15.3k
}
923
924
17.1k
bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
925
17.1k
  if (
Token.is(MIToken::kw_frame_setup)17.1k
) {
926
112
    Flags |= MachineInstr::FrameSetup;
927
112
    lex();
928
112
  }
929
17.1k
  if (Token.isNot(MIToken::Identifier))
930
0
    return error("expected a machine instruction");
931
17.1k
  StringRef InstrName = Token.stringValue();
932
17.1k
  if (parseInstrName(InstrName, OpCode))
933
1
    return error(Twine("unknown machine instruction name '") + InstrName + "'");
934
17.1k
  lex();
935
17.1k
  return false;
936
17.1k
}
937
938
27.2k
bool MIParser::parseNamedRegister(unsigned &Reg) {
939
27.2k
  assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
940
27.2k
  StringRef Name = Token.stringValue();
941
27.2k
  if (getRegisterByName(Name, Reg))
942
2
    return error(Twine("unknown register name '") + Name + "'");
943
27.2k
  return false;
944
27.2k
}
945
946
14.7k
bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
947
14.7k
  assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
948
14.7k
  unsigned ID;
949
14.7k
  if (getUnsigned(ID))
950
0
    return true;
951
14.7k
  Info = &PFS.getVRegInfo(ID);
952
14.7k
  return false;
953
14.7k
}
954
955
40.6k
bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
956
40.6k
  switch (Token.kind()) {
957
4.08k
  case MIToken::underscore:
958
4.08k
    Reg = 0;
959
4.08k
    return false;
960
21.9k
  case MIToken::NamedRegister:
961
21.9k
    return parseNamedRegister(Reg);
962
14.6k
  case MIToken::VirtualRegister:
963
14.6k
    if (parseVirtualRegister(Info))
964
0
      return true;
965
14.6k
    Reg = Info->VReg;
966
14.6k
    return false;
967
14.6k
  // TODO: Parse other register kinds.
968
0
  default:
969
0
    llvm_unreachable("The current token should be a register");
970
0
  }
971
0
}
972
973
407
bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
974
407
  if (
Token.isNot(MIToken::Identifier) && 407
Token.isNot(MIToken::underscore)183
)
975
0
    return error("expected '_', register class, or register bank name");
976
407
  StringRef::iterator Loc = Token.location();
977
407
  StringRef Name = Token.stringValue();
978
407
979
407
  // Was it a register class?
980
407
  auto RCNameI = PFS.Names2RegClasses.find(Name);
981
407
  if (
RCNameI != PFS.Names2RegClasses.end()407
) {
982
90
    lex();
983
90
    const TargetRegisterClass &RC = *RCNameI->getValue();
984
90
985
90
    switch (RegInfo.Kind) {
986
90
    case VRegInfo::UNKNOWN:
987
90
    case VRegInfo::NORMAL:
988
90
      RegInfo.Kind = VRegInfo::NORMAL;
989
90
      if (
RegInfo.Explicit && 90
RegInfo.D.RC != &RC5
) {
990
1
        const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
991
1
        return error(Loc, Twine("conflicting register classes, previously: ") +
992
1
                     Twine(TRI.getRegClassName(RegInfo.D.RC)));
993
1
      }
994
89
      RegInfo.D.RC = &RC;
995
89
      RegInfo.Explicit = true;
996
89
      return false;
997
89
998
0
    case VRegInfo::GENERIC:
999
0
    case VRegInfo::REGBANK:
1000
0
      return error(Loc, "register class specification on generic register");
1001
0
    }
1002
0
    
llvm_unreachable0
("Unexpected register kind");
1003
0
  }
1004
317
1005
317
  // Should be a register bank or a generic register.
1006
317
  const RegisterBank *RegBank = nullptr;
1007
317
  if (
Name != "_"317
) {
1008
134
    auto RBNameI = PFS.Names2RegBanks.find(Name);
1009
134
    if (RBNameI == PFS.Names2RegBanks.end())
1010
0
      return error(Loc, "expected '_', register class, or register bank name");
1011
134
    RegBank = RBNameI->getValue();
1012
134
  }
1013
317
1014
317
  lex();
1015
317
1016
317
  switch (RegInfo.Kind) {
1017
317
  case VRegInfo::UNKNOWN:
1018
317
  case VRegInfo::GENERIC:
1019
317
  case VRegInfo::REGBANK:
1020
317
    RegInfo.Kind = RegBank ? 
VRegInfo::REGBANK134
:
VRegInfo::GENERIC183
;
1021
317
    if (
RegInfo.Explicit && 317
RegInfo.D.RegBank != RegBank1
)
1022
0
      return error(Loc, "conflicting generic register banks");
1023
317
    RegInfo.D.RegBank = RegBank;
1024
317
    RegInfo.Explicit = true;
1025
317
    return false;
1026
317
1027
0
  case VRegInfo::NORMAL:
1028
0
    return error(Loc, "register bank specification on normal register");
1029
0
  }
1030
0
  
llvm_unreachable0
("Unexpected register kind");
1031
0
}
1032
1033
9.13k
bool MIParser::parseRegisterFlag(unsigned &Flags) {
1034
9.13k
  const unsigned OldFlags = Flags;
1035
9.13k
  switch (Token.kind()) {
1036
3.59k
  case MIToken::kw_implicit:
1037
3.59k
    Flags |= RegState::Implicit;
1038
3.59k
    break;
1039
1.83k
  case MIToken::kw_implicit_define:
1040
1.83k
    Flags |= RegState::ImplicitDefine;
1041
1.83k
    break;
1042
38
  case MIToken::kw_def:
1043
38
    Flags |= RegState::Define;
1044
38
    break;
1045
923
  case MIToken::kw_dead:
1046
923
    Flags |= RegState::Dead;
1047
923
    break;
1048
2.01k
  case MIToken::kw_killed:
1049
2.01k
    Flags |= RegState::Kill;
1050
2.01k
    break;
1051
343
  case MIToken::kw_undef:
1052
343
    Flags |= RegState::Undef;
1053
343
    break;
1054
6
  case MIToken::kw_internal:
1055
6
    Flags |= RegState::InternalRead;
1056
6
    break;
1057
80
  case MIToken::kw_early_clobber:
1058
80
    Flags |= RegState::EarlyClobber;
1059
80
    break;
1060
306
  case MIToken::kw_debug_use:
1061
306
    Flags |= RegState::Debug;
1062
306
    break;
1063
0
  default:
1064
0
    llvm_unreachable("The current token should be a register flag");
1065
9.13k
  }
1066
9.13k
  
if (9.13k
OldFlags == Flags9.13k
)
1067
9.13k
    // We know that the same flag is specified more than once when the flags
1068
9.13k
    // weren't modified.
1069
1
    return error("duplicate '" + Token.stringValue() + "' register flag");
1070
9.13k
  lex();
1071
9.13k
  return false;
1072
9.13k
}
1073
1074
399
bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1075
399
  assert(Token.is(MIToken::dot));
1076
399
  lex();
1077
399
  if (Token.isNot(MIToken::Identifier))
1078
1
    return error("expected a subregister index after '.'");
1079
398
  auto Name = Token.stringValue();
1080
398
  SubReg = getSubRegIndex(Name);
1081
398
  if (!SubReg)
1082
1
    return error(Twine("use of unknown subregister index '") + Name + "'");
1083
397
  lex();
1084
397
  return false;
1085
397
}
1086
1087
2.53k
bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1088
2.53k
  if (!consumeIfPresent(MIToken::kw_tied_def))
1089
2.51k
    return true;
1090
17
  
if (17
Token.isNot(MIToken::IntegerLiteral)17
)
1091
1
    return error("expected an integer literal after 'tied-def'");
1092
16
  
if (16
getUnsigned(TiedDefIdx)16
)
1093
0
    return true;
1094
16
  lex();
1095
16
  if (expectAndConsume(MIToken::rparen))
1096
0
    return true;
1097
16
  return false;
1098
16
}
1099
1100
bool MIParser::assignRegisterTies(MachineInstr &MI,
1101
17.0k
                                  ArrayRef<ParsedMachineOperand> Operands) {
1102
17.0k
  SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1103
69.4k
  for (unsigned I = 0, E = Operands.size(); 
I != E69.4k
;
++I52.3k
) {
1104
52.3k
    if (!Operands[I].TiedDefIdx)
1105
52.3k
      continue;
1106
16
    // The parser ensures that this operand is a register use, so we just have
1107
16
    // to check the tied-def operand.
1108
16
    unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1109
16
    if (DefIdx >= E)
1110
1
      return error(Operands[I].Begin,
1111
1
                   Twine("use of invalid tied-def operand index '" +
1112
1
                         Twine(DefIdx) + "'; instruction has only ") +
1113
1
                       Twine(E) + " operands");
1114
15
    const auto &DefOperand = Operands[DefIdx].Operand;
1115
15
    if (
!DefOperand.isReg() || 15
!DefOperand.isDef()14
)
1116
15
      // FIXME: add note with the def operand.
1117
1
      return error(Operands[I].Begin,
1118
1
                   Twine("use of invalid tied-def operand index '") +
1119
1
                       Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1120
1
                       " isn't a defined register");
1121
14
    // Check that the tied-def operand wasn't tied elsewhere.
1122
14
    
for (const auto &TiedPair : TiedRegisterPairs) 14
{
1123
2
      if (TiedPair.first == DefIdx)
1124
1
        return error(Operands[I].Begin,
1125
1
                     Twine("the tied-def operand #") + Twine(DefIdx) +
1126
1
                         " is already tied with another register operand");
1127
13
    }
1128
13
    TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1129
13
  }
1130
17.0k
  // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1131
17.0k
  // indices must be less than tied max.
1132
17.0k
  for (const auto &TiedPair : TiedRegisterPairs)
1133
12
    MI.tieOperands(TiedPair.first, TiedPair.second);
1134
17.0k
  return false;
1135
17.0k
}
1136
1137
bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1138
                                    Optional<unsigned> &TiedDefIdx,
1139
40.6k
                                    bool IsDef) {
1140
40.6k
  unsigned Flags = IsDef ? 
RegState::Define12.6k
:
028.0k
;
1141
49.8k
  while (
Token.isRegisterFlag()49.8k
) {
1142
9.13k
    if (parseRegisterFlag(Flags))
1143
1
      return true;
1144
9.13k
  }
1145
40.6k
  
if (40.6k
!Token.isRegister()40.6k
)
1146
1
    return error("expected a register after register flags");
1147
40.6k
  unsigned Reg;
1148
40.6k
  VRegInfo *RegInfo;
1149
40.6k
  if (parseRegister(Reg, RegInfo))
1150
1
    return true;
1151
40.6k
  lex();
1152
40.6k
  unsigned SubReg = 0;
1153
40.6k
  if (
Token.is(MIToken::dot)40.6k
) {
1154
399
    if (parseSubRegisterIndex(SubReg))
1155
2
      return true;
1156
397
    
if (397
!TargetRegisterInfo::isVirtualRegister(Reg)397
)
1157
1
      return error("subregister index expects a virtual register");
1158
40.6k
  }
1159
40.6k
  
if (40.6k
Token.is(MIToken::colon)40.6k
) {
1160
408
    if (!TargetRegisterInfo::isVirtualRegister(Reg))
1161
1
      return error("register class specification expects a virtual register");
1162
407
    lex();
1163
407
    if (parseRegisterClassOrBank(*RegInfo))
1164
1
        return true;
1165
40.6k
  }
1166
40.6k
  MachineRegisterInfo &MRI = MF.getRegInfo();
1167
40.6k
  if (
(Flags & RegState::Define) == 040.6k
) {
1168
26.1k
    if (
consumeIfPresent(MIToken::lparen)26.1k
) {
1169
2.53k
      unsigned Idx;
1170
2.53k
      if (!parseRegisterTiedDefIndex(Idx))
1171
16
        TiedDefIdx = Idx;
1172
2.51k
      else {
1173
2.51k
        // Try a redundant low-level type.
1174
2.51k
        LLT Ty;
1175
2.51k
        if (parseLowLevelType(Token.location(), Ty))
1176
2
          return error("expected tied-def or low-level type after '('");
1177
2.51k
1178
2.51k
        
if (2.51k
expectAndConsume(MIToken::rparen)2.51k
)
1179
0
          return true;
1180
2.51k
1181
2.51k
        
if (2.51k
MRI.getType(Reg).isValid() && 2.51k
MRI.getType(Reg) != Ty2.51k
)
1182
0
          return error("inconsistent type for generic virtual register");
1183
2.51k
1184
2.51k
        MRI.setType(Reg, Ty);
1185
2.51k
      }
1186
2.53k
    }
1187
40.6k
  } else 
if (14.5k
consumeIfPresent(MIToken::lparen)14.5k
) {
1188
4.72k
    // Virtual registers may have a tpe with GlobalISel.
1189
4.72k
    if (!TargetRegisterInfo::isVirtualRegister(Reg))
1190
1
      return error("unexpected type on physical register");
1191
4.72k
1192
4.72k
    LLT Ty;
1193
4.72k
    if (parseLowLevelType(Token.location(), Ty))
1194
0
      return true;
1195
4.72k
1196
4.72k
    
if (4.72k
expectAndConsume(MIToken::rparen)4.72k
)
1197
0
      return true;
1198
4.72k
1199
4.72k
    
if (4.72k
MRI.getType(Reg).isValid() && 4.72k
MRI.getType(Reg) != Ty1
)
1200
0
      return error("inconsistent type for generic virtual register");
1201
4.72k
1202
4.72k
    MRI.setType(Reg, Ty);
1203
14.5k
  } else 
if (9.78k
TargetRegisterInfo::isVirtualRegister(Reg)9.78k
) {
1204
2.27k
    // Generic virtual registers must have a type.
1205
2.27k
    // If we end up here this means the type hasn't been specified and
1206
2.27k
    // this is bad!
1207
2.27k
    if (RegInfo->Kind == VRegInfo::GENERIC ||
1208
2.27k
        RegInfo->Kind == VRegInfo::REGBANK)
1209
2
      return error("generic virtual registers must have a type");
1210
40.6k
  }
1211
40.6k
  Dest = MachineOperand::CreateReg(
1212
40.6k
      Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1213
40.6k
      Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1214
40.6k
      Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1215
40.6k
      Flags & RegState::InternalRead);
1216
40.6k
  return false;
1217
40.6k
}
1218
1219
9.39k
bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1220
9.39k
  assert(Token.is(MIToken::IntegerLiteral));
1221
9.39k
  const APSInt &Int = Token.integerValue();
1222
9.39k
  if (Int.getMinSignedBits() > 64)
1223
1
    return error("integer literal is too large to be an immediate operand");
1224
9.39k
  Dest = MachineOperand::CreateImm(Int.getExtValue());
1225
9.39k
  lex();
1226
9.39k
  return false;
1227
9.39k
}
1228
1229
bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1230
354
                               const Constant *&C) {
1231
354
  auto Source = StringValue.str(); // The source has to be null terminated.
1232
354
  SMDiagnostic Err;
1233
354
  C = parseConstantValue(Source, Err, *MF.getFunction()->getParent(),
1234
354
                         &PFS.IRSlots);
1235
354
  if (!C)
1236
1
    return error(Loc + Err.getColumnNo(), Err.getMessage());
1237
353
  return false;
1238
353
}
1239
1240
221
bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1241
221
  if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1242
1
    return true;
1243
220
  lex();
1244
220
  return false;
1245
220
}
1246
1247
7.24k
bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1248
7.24k
  if (
Token.is(MIToken::ScalarType)7.24k
) {
1249
5.39k
    Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1250
5.39k
    lex();
1251
5.39k
    return false;
1252
1.85k
  } else 
if (1.85k
Token.is(MIToken::PointerType)1.85k
) {
1253
850
    const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1254
850
    unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1255
850
    Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1256
850
    lex();
1257
850
    return false;
1258
850
  }
1259
1.00k
1260
1.00k
  // Now we're looking for a vector.
1261
1.00k
  
if (1.00k
Token.isNot(MIToken::less)1.00k
)
1262
2
    return error(Loc,
1263
2
                 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1264
1.00k
1265
1.00k
  lex();
1266
1.00k
1267
1.00k
  if (Token.isNot(MIToken::IntegerLiteral))
1268
0
    return error(Loc, "expected <N x sM> for vctor type");
1269
1.00k
  uint64_t NumElements = Token.integerValue().getZExtValue();
1270
1.00k
  lex();
1271
1.00k
1272
1.00k
  if (
Token.isNot(MIToken::Identifier) || 1.00k
Token.stringValue() != "x"1.00k
)
1273
0
    return error(Loc, "expected '<N x sM>' for vector type");
1274
1.00k
  lex();
1275
1.00k
1276
1.00k
  if (Token.isNot(MIToken::ScalarType))
1277
0
    return error(Loc, "expected '<N x sM>' for vector type");
1278
1.00k
  uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1279
1.00k
  lex();
1280
1.00k
1281
1.00k
  if (Token.isNot(MIToken::greater))
1282
0
    return error(Loc, "expected '<N x sM>' for vector type");
1283
1.00k
  lex();
1284
1.00k
1285
1.00k
  Ty = LLT::vector(NumElements, ScalarSize);
1286
1.00k
  return false;
1287
1.00k
}
1288
1289
183
bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1290
183
  assert(Token.is(MIToken::IntegerType));
1291
183
  auto Loc = Token.location();
1292
183
  lex();
1293
183
  if (Token.isNot(MIToken::IntegerLiteral))
1294
0
    return error("expected an integer literal");
1295
183
  const Constant *C = nullptr;
1296
183
  if (parseIRConstant(Loc, C))
1297
0
    return true;
1298
183
  Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1299
183
  return false;
1300
183
}
1301
1302
39
bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1303
39
  auto Loc = Token.location();
1304
39
  lex();
1305
39
  if (Token.isNot(MIToken::FloatingPointLiteral) &&
1306
3
      Token.isNot(MIToken::HexLiteral))
1307
1
    return error("expected a floating point literal");
1308
38
  const Constant *C = nullptr;
1309
38
  if (parseIRConstant(Loc, C))
1310
1
    return true;
1311
37
  Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1312
37
  return false;
1313
37
}
1314
1315
25.2k
bool MIParser::getUnsigned(unsigned &Result) {
1316
25.2k
  if (
Token.hasIntegerValue()25.2k
) {
1317
24.8k
    const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1318
24.8k
    uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1319
24.8k
    if (Val64 == Limit)
1320
1
      return error("expected 32-bit integer (too large)");
1321
24.8k
    Result = Val64;
1322
24.8k
    return false;
1323
24.8k
  }
1324
400
  
if (400
Token.is(MIToken::HexLiteral)400
) {
1325
400
    APInt A;
1326
400
    if (getHexUint(A))
1327
0
      return true;
1328
400
    
if (400
A.getBitWidth() > 32400
)
1329
0
      return error("expected 32-bit integer (too large)");
1330
400
    Result = A.getZExtValue();
1331
400
    return false;
1332
400
  }
1333
0
  return true;
1334
0
}
1335
1336
4.90k
bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1337
4.90k
  assert(Token.is(MIToken::MachineBasicBlock) ||
1338
4.90k
         Token.is(MIToken::MachineBasicBlockLabel));
1339
4.90k
  unsigned Number;
1340
4.90k
  if (getUnsigned(Number))
1341
1
    return true;
1342
4.90k
  auto MBBInfo = PFS.MBBSlots.find(Number);
1343
4.90k
  if (MBBInfo == PFS.MBBSlots.end())
1344
1
    return error(Twine("use of undefined machine basic block #") +
1345
1
                 Twine(Number));
1346
4.90k
  MBB = MBBInfo->second;
1347
4.90k
  if (
!Token.stringValue().empty() && 4.90k
Token.stringValue() != MBB->getName()1.73k
)
1348
1
    return error(Twine("the name of machine basic block #") + Twine(Number) +
1349
1
                 " isn't '" + Token.stringValue() + "'");
1350
4.90k
  return false;
1351
4.90k
}
1352
1353
899
bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1354
899
  MachineBasicBlock *MBB;
1355
899
  if (parseMBBReference(MBB))
1356
3
    return true;
1357
896
  Dest = MachineOperand::CreateMBB(MBB);
1358
896
  lex();
1359
896
  return false;
1360
896
}
1361
1362
88
bool MIParser::parseStackFrameIndex(int &FI) {
1363
88
  assert(Token.is(MIToken::StackObject));
1364
88
  unsigned ID;
1365
88
  if (getUnsigned(ID))
1366
0
    return true;
1367
88
  auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1368
88
  if (ObjectInfo == PFS.StackObjectSlots.end())
1369
1
    return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1370
1
                 "'");
1371
87
  StringRef Name;
1372
87
  if (const auto *Alloca =
1373
87
          MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
1374
32
    Name = Alloca->getName();
1375
87
  if (
!Token.stringValue().empty() && 87
Token.stringValue() != Name31
)
1376
1
    return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1377
1
                 "' isn't '" + Token.stringValue() + "'");
1378
86
  lex();
1379
86
  FI = ObjectInfo->second;
1380
86
  return false;
1381
86
}
1382
1383
35
bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1384
35
  int FI;
1385
35
  if (parseStackFrameIndex(FI))
1386
2
    return true;
1387
33
  Dest = MachineOperand::CreateFI(FI);
1388
33
  return false;
1389
33
}
1390
1391
51
bool MIParser::parseFixedStackFrameIndex(int &FI) {
1392
51
  assert(Token.is(MIToken::FixedStackObject));
1393
51
  unsigned ID;
1394
51
  if (getUnsigned(ID))
1395
0
    return true;
1396
51
  auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1397
51
  if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1398
1
    return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1399
1
                 Twine(ID) + "'");
1400
50
  lex();
1401
50
  FI = ObjectInfo->second;
1402
50
  return false;
1403
50
}
1404
1405
20
bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1406
20
  int FI;
1407
20
  if (parseFixedStackFrameIndex(FI))
1408
1
    return true;
1409
19
  Dest = MachineOperand::CreateFI(FI);
1410
19
  return false;
1411
19
}
1412
1413
401
bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1414
401
  switch (Token.kind()) {
1415
397
  case MIToken::NamedGlobalValue: {
1416
397
    const Module *M = MF.getFunction()->getParent();
1417
397
    GV = M->getNamedValue(Token.stringValue());
1418
397
    if (!GV)
1419
1
      return error(Twine("use of undefined global value '") + Token.range() +
1420
1
                   "'");
1421
396
    break;
1422
396
  }
1423
4
  case MIToken::GlobalValue: {
1424
4
    unsigned GVIdx;
1425
4
    if (getUnsigned(GVIdx))
1426
0
      return true;
1427
4
    
if (4
GVIdx >= PFS.IRSlots.GlobalValues.size()4
)
1428
1
      return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1429
1
                   "'");
1430
3
    GV = PFS.IRSlots.GlobalValues[GVIdx];
1431
3
    break;
1432
3
  }
1433
0
  default:
1434
0
    llvm_unreachable("The current token should be a global value");
1435
399
  }
1436
399
  return false;
1437
399
}
1438
1439
297
bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1440
297
  GlobalValue *GV = nullptr;
1441
297
  if (parseGlobalValue(GV))
1442
2
    return true;
1443
295
  lex();
1444
295
  Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1445
295
  if (parseOperandsOffset(Dest))
1446
2
    return true;
1447
293
  return false;
1448
293
}
1449
1450
16
bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1451
16
  assert(Token.is(MIToken::ConstantPoolItem));
1452
16
  unsigned ID;
1453
16
  if (getUnsigned(ID))
1454
0
    return true;
1455
16
  auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1456
16
  if (ConstantInfo == PFS.ConstantPoolSlots.end())
1457
1
    return error("use of undefined constant '%const." + Twine(ID) + "'");
1458
15
  lex();
1459
15
  Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1460
15
  if (parseOperandsOffset(Dest))
1461
0
    return true;
1462
15
  return false;
1463
15
}
1464
1465
12
bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1466
12
  assert(Token.is(MIToken::JumpTableIndex));
1467
12
  unsigned ID;
1468
12
  if (getUnsigned(ID))
1469
0
    return true;
1470
12
  auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1471
12
  if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1472
1
    return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1473
11
  lex();
1474
11
  Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1475
11
  return false;
1476
11
}
1477
1478
51
bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1479
51
  assert(Token.is(MIToken::ExternalSymbol));
1480
51
  const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1481
51
  lex();
1482
51
  Dest = MachineOperand::CreateES(Symbol);
1483
51
  if (parseOperandsOffset(Dest))
1484
0
    return true;
1485
51
  return false;
1486
51
}
1487
1488
53
bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1489
53
  assert(Token.is(MIToken::SubRegisterIndex));
1490
53
  StringRef Name = Token.stringValue();
1491
53
  unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1492
53
  if (SubRegIndex == 0)
1493
1
    return error(Twine("unknown subregister index '") + Name + "'");
1494
52
  lex();
1495
52
  Dest = MachineOperand::CreateImm(SubRegIndex);
1496
52
  return false;
1497
52
}
1498
1499
823
bool MIParser::parseMDNode(MDNode *&Node) {
1500
823
  assert(Token.is(MIToken::exclaim));
1501
823
1502
823
  auto Loc = Token.location();
1503
823
  lex();
1504
823
  if (
Token.isNot(MIToken::IntegerLiteral) || 823
Token.integerValue().isSigned()822
)
1505
1
    return error("expected metadata id after '!'");
1506
822
  unsigned ID;
1507
822
  if (getUnsigned(ID))
1508
0
    return true;
1509
822
  auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1510
822
  if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
1511
1
    return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1512
821
  lex();
1513
821
  Node = NodeInfo->second.get();
1514
821
  return false;
1515
821
}
1516
1517
18
bool MIParser::parseDIExpression(MDNode *&Expr) {
1518
18
  assert(Token.is(MIToken::md_diexpr));
1519
18
  lex();
1520
18
1521
18
  // FIXME: Share this parsing with the IL parser.
1522
18
  SmallVector<uint64_t, 8> Elements;
1523
18
1524
18
  if (expectAndConsume(MIToken::lparen))
1525
0
    return true;
1526
18
1527
18
  
if (18
Token.isNot(MIToken::rparen)18
) {
1528
6
    do {
1529
6
      if (
Token.is(MIToken::Identifier)6
) {
1530
4
        if (unsigned 
Op4
= dwarf::getOperationEncoding(Token.stringValue())) {
1531
4
          lex();
1532
4
          Elements.push_back(Op);
1533
4
          continue;
1534
4
        }
1535
0
        return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1536
0
      }
1537
2
1538
2
      
if (2
Token.isNot(MIToken::IntegerLiteral) ||
1539
2
          Token.integerValue().isSigned())
1540
0
        return error("expected unsigned integer");
1541
2
1542
2
      auto &U = Token.integerValue();
1543
2
      if (U.ugt(UINT64_MAX))
1544
0
        return error("element too large, limit is " + Twine(UINT64_MAX));
1545
2
      Elements.push_back(U.getZExtValue());
1546
2
      lex();
1547
2
1548
6
    } while (consumeIfPresent(MIToken::comma));
1549
2
  }
1550
18
1551
18
  
if (18
expectAndConsume(MIToken::rparen)18
)
1552
0
    return true;
1553
18
1554
18
  Expr = DIExpression::get(MF.getFunction()->getContext(), Elements);
1555
18
  return false;
1556
18
}
1557
1558
383
bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1559
383
  MDNode *Node = nullptr;
1560
383
  if (
Token.is(MIToken::exclaim)383
) {
1561
366
    if (parseMDNode(Node))
1562
2
      return true;
1563
17
  } else 
if (17
Token.is(MIToken::md_diexpr)17
) {
1564
17
    if (parseDIExpression(Node))
1565
0
      return true;
1566
381
  }
1567
381
  Dest = MachineOperand::CreateMetadata(Node);
1568
381
  return false;
1569
381
}
1570
1571
101
bool MIParser::parseCFIOffset(int &Offset) {
1572
101
  if (Token.isNot(MIToken::IntegerLiteral))
1573
1
    return error("expected a cfi offset");
1574
100
  
if (100
Token.integerValue().getMinSignedBits() > 32100
)
1575
1
    return error("expected a 32 bit integer (the cfi offset is too large)");
1576
99
  Offset = (int)Token.integerValue().getExtValue();
1577
99
  lex();
1578
99
  return false;
1579
99
}
1580
1581
73
bool MIParser::parseCFIRegister(unsigned &Reg) {
1582
73
  if (Token.isNot(MIToken::NamedRegister))
1583
1
    return error("expected a cfi register");
1584
72
  unsigned LLVMReg;
1585
72
  if (parseNamedRegister(LLVMReg))
1586
0
    return true;
1587
72
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1588
72
  assert(TRI && "Expected target register info");
1589
72
  int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1590
72
  if (DwarfReg < 0)
1591
0
    return error("invalid DWARF register");
1592
72
  Reg = (unsigned)DwarfReg;
1593
72
  lex();
1594
72
  return false;
1595
72
}
1596
1597
115
bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1598
115
  auto Kind = Token.kind();
1599
115
  lex();
1600
115
  int Offset;
1601
115
  unsigned Reg;
1602
115
  unsigned CFIIndex;
1603
115
  switch (Kind) {
1604
2
  case MIToken::kw_cfi_same_value:
1605
2
    if (parseCFIRegister(Reg))
1606
0
      return true;
1607
2
    CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1608
2
    break;
1609
60
  case MIToken::kw_cfi_offset:
1610
60
    if (
parseCFIRegister(Reg) || 60
expectAndConsume(MIToken::comma)59
||
1611
58
        parseCFIOffset(Offset))
1612
2
      return true;
1613
58
    CFIIndex =
1614
58
        MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1615
58
    break;
1616
10
  case MIToken::kw_cfi_def_cfa_register:
1617
10
    if (parseCFIRegister(Reg))
1618
0
      return true;
1619
10
    CFIIndex =
1620
10
        MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1621
10
    break;
1622
42
  case MIToken::kw_cfi_def_cfa_offset:
1623
42
    if (parseCFIOffset(Offset))
1624
2
      return true;
1625
40
    // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1626
40
    CFIIndex = MF.addFrameInst(
1627
40
        MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1628
40
    break;
1629
1
  case MIToken::kw_cfi_def_cfa:
1630
1
    if (
parseCFIRegister(Reg) || 1
expectAndConsume(MIToken::comma)1
||
1631
1
        parseCFIOffset(Offset))
1632
0
      return true;
1633
1
    // NB: MCCFIInstruction::createDefCfa negates the offset.
1634
1
    CFIIndex =
1635
1
        MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1636
1
    break;
1637
0
  default:
1638
0
    // TODO: Parse the other CFI operands.
1639
0
    llvm_unreachable("The current token should be a cfi operand");
1640
111
  }
1641
111
  Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1642
111
  return false;
1643
111
}
1644
1645
736
bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1646
736
  switch (Token.kind()) {
1647
4
  case MIToken::NamedIRBlock: {
1648
4
    BB = dyn_cast_or_null<BasicBlock>(
1649
4
        F.getValueSymbolTable()->lookup(Token.stringValue()));
1650
4
    if (!BB)
1651
1
      return error(Twine("use of undefined IR block '") + Token.range() + "'");
1652
3
    break;
1653
3
  }
1654
732
  case MIToken::IRBlock: {
1655
732
    unsigned SlotNumber = 0;
1656
732
    if (getUnsigned(SlotNumber))
1657
0
      return true;
1658
732
    BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1659
732
    if (!BB)
1660
2
      return error(Twine("use of undefined IR block '%ir-block.") +
1661
2
                   Twine(SlotNumber) + "'");
1662
730
    break;
1663
730
  }
1664
0
  default:
1665
0
    llvm_unreachable("The current token should be an IR block reference");
1666
733
  }
1667
733
  return false;
1668
733
}
1669
1670
10
bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1671
10
  assert(Token.is(MIToken::kw_blockaddress));
1672
10
  lex();
1673
10
  if (expectAndConsume(MIToken::lparen))
1674
0
    return true;
1675
10
  
if (10
Token.isNot(MIToken::GlobalValue) &&
1676
10
      Token.isNot(MIToken::NamedGlobalValue))
1677
1
    return error("expected a global value");
1678
9
  GlobalValue *GV = nullptr;
1679
9
  if (parseGlobalValue(GV))
1680
0
    return true;
1681
9
  auto *F = dyn_cast<Function>(GV);
1682
9
  if (!F)
1683
1
    return error("expected an IR function reference");
1684
8
  lex();
1685
8
  if (expectAndConsume(MIToken::comma))
1686
0
    return true;
1687
8
  BasicBlock *BB = nullptr;
1688
8
  if (
Token.isNot(MIToken::IRBlock) && 8
Token.isNot(MIToken::NamedIRBlock)5
)
1689
1
    return error("expected an IR block reference");
1690
7
  
if (7
parseIRBlock(BB, *F)7
)
1691
2
    return true;
1692
5
  lex();
1693
5
  if (expectAndConsume(MIToken::rparen))
1694
0
    return true;
1695
5
  Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1696
5
  if (parseOperandsOffset(Dest))
1697
0
    return true;
1698
5
  return false;
1699
5
}
1700
1701
7
bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1702
7
  assert(Token.is(MIToken::kw_intrinsic));
1703
7
  lex();
1704
7
  if (expectAndConsume(MIToken::lparen))
1705
0
    return error("expected syntax intrinsic(@llvm.whatever)");
1706
7
1707
7
  
if (7
Token.isNot(MIToken::NamedGlobalValue)7
)
1708
0
    return error("expected syntax intrinsic(@llvm.whatever)");
1709
7
1710
7
  std::string Name = Token.stringValue();
1711
7
  lex();
1712
7
1713
7
  if (expectAndConsume(MIToken::rparen))
1714
0
    return error("expected ')' to terminate intrinsic name");
1715
7
1716
7
  // Find out what intrinsic we're dealing with, first try the global namespace
1717
7
  // and then the target's private intrinsics if that fails.
1718
7
  const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1719
7
  Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1720
7
  if (
ID == Intrinsic::not_intrinsic && 7
TII0
)
1721
0
    ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1722
7
1723
7
  if (ID == Intrinsic::not_intrinsic)
1724
0
    return error("unknown intrinsic name");
1725
7
  Dest = MachineOperand::CreateIntrinsicID(ID);
1726
7
1727
7
  return false;
1728
7
}
1729
1730
226
bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1731
226
  assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1732
226
  bool IsFloat = Token.is(MIToken::kw_floatpred);
1733
226
  lex();
1734
226
1735
226
  if (expectAndConsume(MIToken::lparen))
1736
0
    return error("expected syntax intpred(whatever) or floatpred(whatever");
1737
226
1738
226
  
if (226
Token.isNot(MIToken::Identifier)226
)
1739
0
    return error("whatever");
1740
226
1741
226
  CmpInst::Predicate Pred;
1742
226
  if (
IsFloat226
) {
1743
139
    Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1744
139
               .Case("false", CmpInst::FCMP_FALSE)
1745
139
               .Case("oeq", CmpInst::FCMP_OEQ)
1746
139
               .Case("ogt", CmpInst::FCMP_OGT)
1747
139
               .Case("oge", CmpInst::FCMP_OGE)
1748
139
               .Case("olt", CmpInst::FCMP_OLT)
1749
139
               .Case("ole", CmpInst::FCMP_OLE)
1750
139
               .Case("one", CmpInst::FCMP_ONE)
1751
139
               .Case("ord", CmpInst::FCMP_ORD)
1752
139
               .Case("uno", CmpInst::FCMP_UNO)
1753
139
               .Case("ueq", CmpInst::FCMP_UEQ)
1754
139
               .Case("ugt", CmpInst::FCMP_UGT)
1755
139
               .Case("uge", CmpInst::FCMP_UGE)
1756
139
               .Case("ult", CmpInst::FCMP_ULT)
1757
139
               .Case("ule", CmpInst::FCMP_ULE)
1758
139
               .Case("une", CmpInst::FCMP_UNE)
1759
139
               .Case("true", CmpInst::FCMP_TRUE)
1760
139
               .Default(CmpInst::BAD_FCMP_PREDICATE);
1761
139
    if (!CmpInst::isFPPredicate(Pred))
1762
0
      return error("invalid floating-point predicate");
1763
87
  } else {
1764
87
    Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1765
87
               .Case("eq", CmpInst::ICMP_EQ)
1766
87
               .Case("ne", CmpInst::ICMP_NE)
1767
87
               .Case("sgt", CmpInst::ICMP_SGT)
1768
87
               .Case("sge", CmpInst::ICMP_SGE)
1769
87
               .Case("slt", CmpInst::ICMP_SLT)
1770
87
               .Case("sle", CmpInst::ICMP_SLE)
1771
87
               .Case("ugt", CmpInst::ICMP_UGT)
1772
87
               .Case("uge", CmpInst::ICMP_UGE)
1773
87
               .Case("ult", CmpInst::ICMP_ULT)
1774
87
               .Case("ule", CmpInst::ICMP_ULE)
1775
87
               .Default(CmpInst::BAD_ICMP_PREDICATE);
1776
87
    if (!CmpInst::isIntPredicate(Pred))
1777
0
      return error("invalid integer predicate");
1778
226
  }
1779
226
1780
226
  lex();
1781
226
  Dest = MachineOperand::CreatePredicate(Pred);
1782
226
  if (expectAndConsume(MIToken::rparen))
1783
0
    return error("predicate should be terminated by ')'.");
1784
226
1785
226
  return false;
1786
226
}
1787
1788
4
bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1789
4
  assert(Token.is(MIToken::kw_target_index));
1790
4
  lex();
1791
4
  if (expectAndConsume(MIToken::lparen))
1792
0
    return true;
1793
4
  
if (4
Token.isNot(MIToken::Identifier)4
)
1794
1
    return error("expected the name of the target index");
1795
3
  int Index = 0;
1796
3
  if (getTargetIndex(Token.stringValue(), Index))
1797
1
    return error("use of undefined target index '" + Token.stringValue() + "'");
1798
2
  lex();
1799
2
  if (expectAndConsume(MIToken::rparen))
1800
0
    return true;
1801
2
  Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1802
2
  if (parseOperandsOffset(Dest))
1803
0
    return true;
1804
2
  return false;
1805
2
}
1806
1807
1
bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1808
1
  assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
1809
1
  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1810
1
  assert(TRI && "Expected target register info");
1811
1
  lex();
1812
1
  if (expectAndConsume(MIToken::lparen))
1813
0
    return true;
1814
1
1815
1
  uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1816
45
  while (
true45
) {
1817
45
    if (Token.isNot(MIToken::NamedRegister))
1818
0
      return error("expected a named register");
1819
45
    unsigned Reg;
1820
45
    if (parseNamedRegister(Reg))
1821
0
      return true;
1822
45
    lex();
1823
45
    Mask[Reg / 32] |= 1U << (Reg % 32);
1824
45
    // TODO: Report an error if the same register is used more than once.
1825
45
    if (Token.isNot(MIToken::comma))
1826
1
      break;
1827
44
    lex();
1828
44
  }
1829
1
1830
1
  
if (1
expectAndConsume(MIToken::rparen)1
)
1831
0
    return true;
1832
1
  Dest = MachineOperand::CreateRegMask(Mask);
1833
1
  return false;
1834
1
}
1835
1836
1
bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1837
1
  assert(Token.is(MIToken::kw_liveout));
1838
1
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1839
1
  assert(TRI && "Expected target register info");
1840
1
  uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1841
1
  lex();
1842
1
  if (expectAndConsume(MIToken::lparen))
1843
0
    return true;
1844
4
  
while (1
true4
) {
1845
4
    if (Token.isNot(MIToken::NamedRegister))
1846
0
      return error("expected a named register");
1847
4
    unsigned Reg;
1848
4
    if (parseNamedRegister(Reg))
1849
0
      return true;
1850
4
    lex();
1851
4
    Mask[Reg / 32] |= 1U << (Reg % 32);
1852
4
    // TODO: Report an error if the same register is used more than once.
1853
4
    if (Token.isNot(MIToken::comma))
1854
1
      break;
1855
3
    lex();
1856
3
  }
1857
1
  
if (1
expectAndConsume(MIToken::rparen)1
)
1858
0
    return true;
1859
1
  Dest = MachineOperand::CreateRegLiveOut(Mask);
1860
1
  return false;
1861
1
}
1862
1863
bool MIParser::parseMachineOperand(MachineOperand &Dest,
1864
39.9k
                                   Optional<unsigned> &TiedDefIdx) {
1865
39.9k
  switch (Token.kind()) {
1866
28.0k
  case MIToken::kw_implicit:
1867
28.0k
  case MIToken::kw_implicit_define:
1868
28.0k
  case MIToken::kw_def:
1869
28.0k
  case MIToken::kw_dead:
1870
28.0k
  case MIToken::kw_killed:
1871
28.0k
  case MIToken::kw_undef:
1872
28.0k
  case MIToken::kw_internal:
1873
28.0k
  case MIToken::kw_early_clobber:
1874
28.0k
  case MIToken::kw_debug_use:
1875
28.0k
  case MIToken::underscore:
1876
28.0k
  case MIToken::NamedRegister:
1877
28.0k
  case MIToken::VirtualRegister:
1878
28.0k
    return parseRegisterOperand(Dest, TiedDefIdx);
1879
9.39k
  case MIToken::IntegerLiteral:
1880
9.39k
    return parseImmediateOperand(Dest);
1881
183
  case MIToken::IntegerType:
1882
183
    return parseTypedImmediateOperand(Dest);
1883
39
  case MIToken::kw_half:
1884
39
  case MIToken::kw_float:
1885
39
  case MIToken::kw_double:
1886
39
  case MIToken::kw_x86_fp80:
1887
39
  case MIToken::kw_fp128:
1888
39
  case MIToken::kw_ppc_fp128:
1889
39
    return parseFPImmediateOperand(Dest);
1890
899
  case MIToken::MachineBasicBlock:
1891
899
    return parseMBBOperand(Dest);
1892
35
  case MIToken::StackObject:
1893
35
    return parseStackObjectOperand(Dest);
1894
20
  case MIToken::FixedStackObject:
1895
20
    return parseFixedStackObjectOperand(Dest);
1896
297
  case MIToken::GlobalValue:
1897
297
  case MIToken::NamedGlobalValue:
1898
297
    return parseGlobalAddressOperand(Dest);
1899
16
  case MIToken::ConstantPoolItem:
1900
16
    return parseConstantPoolIndexOperand(Dest);
1901
12
  case MIToken::JumpTableIndex:
1902
12
    return parseJumpTableIndexOperand(Dest);
1903
51
  case MIToken::ExternalSymbol:
1904
51
    return parseExternalSymbolOperand(Dest);
1905
53
  case MIToken::SubRegisterIndex:
1906
53
    return parseSubRegisterIndexOperand(Dest);
1907
383
  case MIToken::md_diexpr:
1908
383
  case MIToken::exclaim:
1909
383
    return parseMetadataOperand(Dest);
1910
115
  case MIToken::kw_cfi_same_value:
1911
115
  case MIToken::kw_cfi_offset:
1912
115
  case MIToken::kw_cfi_def_cfa_register:
1913
115
  case MIToken::kw_cfi_def_cfa_offset:
1914
115
  case MIToken::kw_cfi_def_cfa:
1915
115
    return parseCFIOperand(Dest);
1916
10
  case MIToken::kw_blockaddress:
1917
10
    return parseBlockAddressOperand(Dest);
1918
7
  case MIToken::kw_intrinsic:
1919
7
    return parseIntrinsicOperand(Dest);
1920
4
  case MIToken::kw_target_index:
1921
4
    return parseTargetIndexOperand(Dest);
1922
1
  case MIToken::kw_liveout:
1923
1
    return parseLiveoutRegisterMaskOperand(Dest);
1924
226
  case MIToken::kw_floatpred:
1925
226
  case MIToken::kw_intpred:
1926
226
    return parsePredicateOperand(Dest);
1927
0
  case MIToken::Error:
1928
0
    return true;
1929
115
  case MIToken::Identifier:
1930
115
    if (const auto *
RegMask115
= getRegMask(Token.stringValue())) {
1931
114
      Dest = MachineOperand::CreateRegMask(RegMask);
1932
114
      lex();
1933
114
      break;
1934
114
    } else
1935
1
      return parseCustomRegisterMaskOperand(Dest);
1936
1
  default:
1937
1
    // FIXME: Parse the MCSymbol machine operand.
1938
1
    return error("expected a machine operand");
1939
114
  }
1940
114
  return false;
1941
114
}
1942
1943
bool MIParser::parseMachineOperandAndTargetFlags(
1944
39.9k
    MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1945
39.9k
  unsigned TF = 0;
1946
39.9k
  bool HasTargetFlags = false;
1947
39.9k
  if (
Token.is(MIToken::kw_target_flags)39.9k
) {
1948
48
    HasTargetFlags = true;
1949
48
    lex();
1950
48
    if (expectAndConsume(MIToken::lparen))
1951
0
      return true;
1952
48
    
if (48
Token.isNot(MIToken::Identifier)48
)
1953
1
      return error("expected the name of the target flag");
1954
47
    
if (47
getDirectTargetFlag(Token.stringValue(), TF)47
) {
1955
4
      if (getBitmaskTargetFlag(Token.stringValue(), TF))
1956
1
        return error("use of undefined target flag '" + Token.stringValue() +
1957
1
                     "'");
1958
46
    }
1959
46
    lex();
1960
56
    while (
Token.is(MIToken::comma)56
) {
1961
12
      lex();
1962
12
      if (Token.isNot(MIToken::Identifier))
1963
1
        return error("expected the name of the target flag");
1964
11
      unsigned BitFlag = 0;
1965
11
      if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1966
1
        return error("use of undefined target flag '" + Token.stringValue() +
1967
1
                     "'");
1968
10
      // TODO: Report an error when using a duplicate bit target flag.
1969
10
      TF |= BitFlag;
1970
10
      lex();
1971
10
    }
1972
44
    
if (44
expectAndConsume(MIToken::rparen)44
)
1973
0
      return true;
1974
39.9k
  }
1975
39.9k
  auto Loc = Token.location();
1976
39.9k
  if (parseMachineOperand(Dest, TiedDefIdx))
1977
37
    return true;
1978
39.8k
  
if (39.8k
!HasTargetFlags39.8k
)
1979
39.8k
    return false;
1980
44
  
if (44
Dest.isReg()44
)
1981
1
    return error(Loc, "register operands can't have target flags");
1982
43
  Dest.setTargetFlags(TF);
1983
43
  return false;
1984
43
}
1985
1986
1.11k
bool MIParser::parseOffset(int64_t &Offset) {
1987
1.11k
  if (
Token.isNot(MIToken::plus) && 1.11k
Token.isNot(MIToken::minus)1.09k
)
1988
1.09k
    return false;
1989
29
  StringRef Sign = Token.range();
1990
29
  bool IsNegative = Token.is(MIToken::minus);
1991
29
  lex();
1992
29
  if (Token.isNot(MIToken::IntegerLiteral))
1993
1
    return error("expected an integer literal after '" + Sign + "'");
1994
28
  
if (28
Token.integerValue().getMinSignedBits() > 6428
)
1995
1
    return error("expected 64-bit integer (too large)");
1996
27
  Offset = Token.integerValue().getExtValue();
1997
27
  if (IsNegative)
1998
4
    Offset = -Offset;
1999
1.11k
  lex();
2000
1.11k
  return false;
2001
1.11k
}
2002
2003
83
bool MIParser::parseAlignment(unsigned &Alignment) {
2004
83
  assert(Token.is(MIToken::kw_align));
2005
83
  lex();
2006
83
  if (
Token.isNot(MIToken::IntegerLiteral) || 83
Token.integerValue().isSigned()82
)
2007
2
    return error("expected an integer literal after 'align'");
2008
81
  
if (81
getUnsigned(Alignment)81
)
2009
0
    return true;
2010
81
  lex();
2011
81
  return false;
2012
81
}
2013
2014
368
bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2015
368
  int64_t Offset = 0;
2016
368
  if (parseOffset(Offset))
2017
2
    return true;
2018
366
  Op.setOffset(Offset);
2019
366
  return false;
2020
366
}
2021
2022
650
bool MIParser::parseIRValue(const Value *&V) {
2023
650
  switch (Token.kind()) {
2024
404
  case MIToken::NamedIRValue: {
2025
404
    V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
2026
404
    break;
2027
650
  }
2028
22
  case MIToken::IRValue: {
2029
22
    unsigned SlotNumber = 0;
2030
22
    if (getUnsigned(SlotNumber))
2031
0
      return true;
2032
22
    V = getIRValue(SlotNumber);
2033
22
    break;
2034
22
  }
2035
91
  case MIToken::NamedGlobalValue:
2036
91
  case MIToken::GlobalValue: {
2037
91
    GlobalValue *GV = nullptr;
2038
91
    if (parseGlobalValue(GV))
2039
0
      return true;
2040
91
    V = GV;
2041
91
    break;
2042
91
  }
2043
133
  case MIToken::QuotedIRValue: {
2044
133
    const Constant *C = nullptr;
2045
133
    if (parseIRConstant(Token.location(), Token.stringValue(), C))
2046
0
      return true;
2047
133
    V = C;
2048
133
    break;
2049
133
  }
2050
0
  default:
2051
0
    llvm_unreachable("The current token should be an IR block reference");
2052
650
  }
2053
650
  
if (650
!V650
)
2054
1
    return error(Twine("use of undefined IR value '") + Token.range() + "'");
2055
649
  return false;
2056
649
}
2057
2058
990
bool MIParser::getUint64(uint64_t &Result) {
2059
990
  if (
Token.hasIntegerValue()990
) {
2060
990
    if (Token.integerValue().getActiveBits() > 64)
2061
1
      return error("expected 64-bit integer (too large)");
2062
989
    Result = Token.integerValue().getZExtValue();
2063
989
    return false;
2064
989
  }
2065
0
  
if (0
Token.is(MIToken::HexLiteral)0
) {
2066
0
    APInt A;
2067
0
    if (getHexUint(A))
2068
0
      return true;
2069
0
    
if (0
A.getBitWidth() > 640
)
2070
0
      return error("expected 64-bit integer (too large)");
2071
0
    Result = A.getZExtValue();
2072
0
    return false;
2073
0
  }
2074
0
  return true;
2075
0
}
2076
2077
400
bool MIParser::getHexUint(APInt &Result) {
2078
400
  assert(Token.is(MIToken::HexLiteral));
2079
400
  StringRef S = Token.range();
2080
400
  assert(S[0] == '0' && tolower(S[1]) == 'x');
2081
400
  // This could be a floating point literal with a special prefix.
2082
400
  if (!isxdigit(S[2]))
2083
0
    return true;
2084
400
  StringRef V = S.substr(2);
2085
400
  APInt A(V.size()*4, V, 16);
2086
400
2087
400
  // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2088
400
  // sure it isn't the case before constructing result.
2089
400
  unsigned NumBits = (A == 0) ? 
320
:
A.getActiveBits()400
;
2090
400
  Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2091
400
  return false;
2092
400
}
2093
2094
313
bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2095
313
  const auto OldFlags = Flags;
2096
313
  switch (Token.kind()) {
2097
86
  case MIToken::kw_volatile:
2098
86
    Flags |= MachineMemOperand::MOVolatile;
2099
86
    break;
2100
56
  case MIToken::kw_non_temporal:
2101
56
    Flags |= MachineMemOperand::MONonTemporal;
2102
56
    break;
2103
85
  case MIToken::kw_dereferenceable:
2104
85
    Flags |= MachineMemOperand::MODereferenceable;
2105
85
    break;
2106
63
  case MIToken::kw_invariant:
2107
63
    Flags |= MachineMemOperand::MOInvariant;
2108
63
    break;
2109
23
  case MIToken::StringConstant: {
2110
23
    MachineMemOperand::Flags TF;
2111
23
    if (getMMOTargetFlag(Token.stringValue(), TF))
2112
1
      return error("use of undefined target MMO flag '" + Token.stringValue() +
2113
1
                   "'");
2114
22
    Flags |= TF;
2115
22
    break;
2116
22
  }
2117
0
  default:
2118
0
    llvm_unreachable("The current token should be a memory operand flag");
2119
312
  }
2120
312
  
if (312
OldFlags == Flags312
)
2121
312
    // We know that the same flag is specified more than once when the flags
2122
312
    // weren't modified.
2123
1
    return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2124
311
  lex();
2125
311
  return false;
2126
311
}
2127
2128
104
bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2129
104
  switch (Token.kind()) {
2130
1
  case MIToken::kw_stack:
2131
1
    PSV = MF.getPSVManager().getStack();
2132
1
    break;
2133
5
  case MIToken::kw_got:
2134
5
    PSV = MF.getPSVManager().getGOT();
2135
5
    break;
2136
4
  case MIToken::kw_jump_table:
2137
4
    PSV = MF.getPSVManager().getJumpTable();
2138
4
    break;
2139
5
  case MIToken::kw_constant_pool:
2140
5
    PSV = MF.getPSVManager().getConstantPool();
2141
5
    break;
2142
31
  case MIToken::FixedStackObject: {
2143
31
    int FI;
2144
31
    if (parseFixedStackFrameIndex(FI))
2145
0
      return true;
2146
31
    PSV = MF.getPSVManager().getFixedStack(FI);
2147
31
    // The token was already consumed, so use return here instead of break.
2148
31
    return false;
2149
31
  }
2150
52
  case MIToken::StackObject: {
2151
52
    int FI;
2152
52
    if (parseStackFrameIndex(FI))
2153
0
      return true;
2154
52
    PSV = MF.getPSVManager().getFixedStack(FI);
2155
52
    // The token was already consumed, so use return here instead of break.
2156
52
    return false;
2157
52
  }
2158
6
  case MIToken::kw_call_entry:
2159
6
    lex();
2160
6
    switch (Token.kind()) {
2161
4
    case MIToken::GlobalValue:
2162
4
    case MIToken::NamedGlobalValue: {
2163
4
      GlobalValue *GV = nullptr;
2164
4
      if (parseGlobalValue(GV))
2165
0
        return true;
2166
4
      PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2167
4
      break;
2168
4
    }
2169
1
    case MIToken::ExternalSymbol:
2170
1
      PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2171
1
          MF.createExternalSymbolName(Token.stringValue()));
2172
1
      break;
2173
1
    default:
2174
1
      return error(
2175
1
          "expected a global value or an external symbol after 'call-entry'");
2176
5
    }
2177
5
    break;
2178
0
  default:
2179
0
    llvm_unreachable("The current token should be pseudo source value");
2180
20
  }
2181
20
  lex();
2182
20
  return false;
2183
20
}
2184
2185
755
bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2186
755
  if (
Token.is(MIToken::kw_constant_pool) || 755
Token.is(MIToken::kw_stack)750
||
2187
755
      
Token.is(MIToken::kw_got)749
||
Token.is(MIToken::kw_jump_table)744
||
2188
755
      
Token.is(MIToken::FixedStackObject)740
||
Token.is(MIToken::StackObject)709
||
2189
755
      
Token.is(MIToken::kw_call_entry)657
) {
2190
104
    const PseudoSourceValue *PSV = nullptr;
2191
104
    if (parseMemoryPseudoSourceValue(PSV))
2192
1
      return true;
2193
103
    int64_t Offset = 0;
2194
103
    if (parseOffset(Offset))
2195
0
      return true;
2196
103
    Dest = MachinePointerInfo(PSV, Offset);
2197
103
    return false;
2198
103
  }
2199
651
  
if (651
Token.isNot(MIToken::NamedIRValue) && 651
Token.isNot(MIToken::IRValue)247
&&
2200
225
      Token.isNot(MIToken::GlobalValue) &&
2201
224
      Token.isNot(MIToken::NamedGlobalValue) &&
2202
134
      Token.isNot(MIToken::QuotedIRValue))
2203
1
    return error("expected an IR value reference");
2204
650
  const Value *V = nullptr;
2205
650
  if (parseIRValue(V))
2206
1
    return true;
2207
649
  
if (649
!V->getType()->isPointerTy()649
)
2208
1
    return error("expected a pointer IR value");
2209
648
  lex();
2210
648
  int64_t Offset = 0;
2211
648
  if (parseOffset(Offset))
2212
0
    return true;
2213
648
  Dest = MachinePointerInfo(V, Offset);
2214
648
  return false;
2215
648
}
2216
2217
bool MIParser::parseOptionalScope(LLVMContext &Context,
2218
991
                                  SyncScope::ID &SSID) {
2219
991
  SSID = SyncScope::System;
2220
991
  if (
Token.is(MIToken::Identifier) && 991
Token.stringValue() == "syncscope"14
) {
2221
6
    lex();
2222
6
    if (expectAndConsume(MIToken::lparen))
2223
0
      return error("expected '(' in syncscope");
2224
6
2225
6
    std::string SSN;
2226
6
    if (parseStringConstant(SSN))
2227
0
      return true;
2228
6
2229
6
    SSID = Context.getOrInsertSyncScopeID(SSN);
2230
6
    if (expectAndConsume(MIToken::rparen))
2231
0
      return error("expected ')' in syncscope");
2232
991
  }
2233
991
2234
991
  return false;
2235
991
}
2236
2237
1.98k
bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2238
1.98k
  Order = AtomicOrdering::NotAtomic;
2239
1.98k
  if (Token.isNot(MIToken::Identifier))
2240
1.96k
    return false;
2241
14
2242
14
  Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2243
14
              .Case("unordered", AtomicOrdering::Unordered)
2244
14
              .Case("monotonic", AtomicOrdering::Monotonic)
2245
14
              .Case("acquire", AtomicOrdering::Acquire)
2246
14
              .Case("release", AtomicOrdering::Release)
2247
14
              .Case("acq_rel", AtomicOrdering::AcquireRelease)
2248
14
              .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2249
14
              .Default(AtomicOrdering::NotAtomic);
2250
14
2251
14
  if (
Order != AtomicOrdering::NotAtomic14
) {
2252
13
    lex();
2253
13
    return false;
2254
13
  }
2255
1
2256
1
  return error("expected an atomic scope, ordering or a size integer literal");
2257
1
}
2258
2259
994
bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2260
994
  if (expectAndConsume(MIToken::lparen))
2261
0
    return true;
2262
994
  MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
2263
1.30k
  while (
Token.isMemoryOperandFlag()1.30k
) {
2264
313
    if (parseMemoryOperandFlag(Flags))
2265
2
      return true;
2266
313
  }
2267
992
  
if (992
Token.isNot(MIToken::Identifier) ||
2268
991
      
(Token.stringValue() != "load" && 991
Token.stringValue() != "store"380
))
2269
1
    return error("expected 'load' or 'store' memory operation");
2270
991
  
if (991
Token.stringValue() == "load"991
)
2271
611
    Flags |= MachineMemOperand::MOLoad;
2272
991
  else
2273
380
    Flags |= MachineMemOperand::MOStore;
2274
991
  lex();
2275
991
2276
991
  // Optional synchronization scope.
2277
991
  SyncScope::ID SSID;
2278
991
  if (parseOptionalScope(MF.getFunction()->getContext(), SSID))
2279
0
    return true;
2280
991
2281
991
  // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2282
991
  AtomicOrdering Order, FailureOrder;
2283
991
  if (parseOptionalAtomicOrdering(Order))
2284
1
    return true;
2285
990
2286
990
  
if (990
parseOptionalAtomicOrdering(FailureOrder)990
)
2287
0
    return true;
2288
990
2289
990
  
if (990
Token.isNot(MIToken::IntegerLiteral)990
)
2290
0
    return error("expected the size integer literal after memory operation");
2291
990
  uint64_t Size;
2292
990
  if (getUint64(Size))
2293
1
    return true;
2294
989
  lex();
2295
989
2296
989
  MachinePointerInfo Ptr = MachinePointerInfo();
2297
989
  if (
Token.is(MIToken::Identifier)989
) {
2298
755
    const char *Word = Flags & MachineMemOperand::MOLoad ? 
"from"484
:
"into"271
;
2299
755
    if (Token.stringValue() != Word)
2300
0
      return error(Twine("expected '") + Word + "'");
2301
755
    lex();
2302
755
2303
755
    if (parseMachinePointerInfo(Ptr))
2304
4
      return true;
2305
985
  }
2306
985
  unsigned BaseAlignment = Size;
2307
985
  AAMDNodes AAInfo;
2308
985
  MDNode *Range = nullptr;
2309
1.09k
  while (
consumeIfPresent(MIToken::comma)1.09k
) {
2310
111
    switch (Token.kind()) {
2311
79
    case MIToken::kw_align:
2312
79
      if (parseAlignment(BaseAlignment))
2313
2
        return true;
2314
77
      break;
2315
28
    case MIToken::md_tbaa:
2316
28
      lex();
2317
28
      if (parseMDNode(AAInfo.TBAA))
2318
0
        return true;
2319
28
      break;
2320
1
    case MIToken::md_alias_scope:
2321
1
      lex();
2322
1
      if (parseMDNode(AAInfo.Scope))
2323
0
        return true;
2324
1
      break;
2325
1
    case MIToken::md_noalias:
2326
1
      lex();
2327
1
      if (parseMDNode(AAInfo.NoAlias))
2328
0
        return true;
2329
1
      break;
2330
1
    case MIToken::md_range:
2331
1
      lex();
2332
1
      if (parseMDNode(Range))
2333
0
        return true;
2334
1
      break;
2335
1
    // TODO: Report an error on duplicate metadata nodes.
2336
1
    default:
2337
1
      return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2338
1
                   "'!noalias' or '!range'");
2339
111
    }
2340
111
  }
2341
982
  
if (982
expectAndConsume(MIToken::rparen)982
)
2342
0
    return true;
2343
982
  Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2344
982
                                 SSID, Order, FailureOrder);
2345
982
  return false;
2346
982
}
2347
2348
17.1k
void MIParser::initNames2InstrOpCodes() {
2349
17.1k
  if (!Names2InstrOpCodes.empty())
2350
14.8k
    return;
2351
2.26k
  const auto *TII = MF.getSubtarget().getInstrInfo();
2352
2.26k
  assert(TII && "Expected target instruction info");
2353
20.0M
  for (unsigned I = 0, E = TII->getNumOpcodes(); 
I < E20.0M
;
++I20.0M
)
2354
20.0M
    Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2355
17.1k
}
2356
2357
17.1k
bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2358
17.1k
  initNames2InstrOpCodes();
2359
17.1k
  auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2360
17.1k
  if (InstrInfo == Names2InstrOpCodes.end())
2361
1
    return true;
2362
17.1k
  OpCode = InstrInfo->getValue();
2363
17.1k
  return false;
2364
17.1k
}
2365
2366
27.2k
void MIParser::initNames2Regs() {
2367
27.2k
  if (!Names2Regs.empty())
2368
23.7k
    return;
2369
3.48k
  // The '%noreg' register is the register 0.
2370
3.48k
  Names2Regs.insert(std::make_pair("noreg", 0));
2371
3.48k
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2372
3.48k
  assert(TRI && "Expected target register info");
2373
2.25M
  for (unsigned I = 0, E = TRI->getNumRegs(); 
I < E2.25M
;
++I2.24M
) {
2374
2.24M
    bool WasInserted =
2375
2.24M
        Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2376
2.24M
            .second;
2377
2.24M
    (void)WasInserted;
2378
2.24M
    assert(WasInserted && "Expected registers to be unique case-insensitively");
2379
2.24M
  }
2380
27.2k
}
2381
2382
27.2k
bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2383
27.2k
  initNames2Regs();
2384
27.2k
  auto RegInfo = Names2Regs.find(RegName);
2385
27.2k
  if (RegInfo == Names2Regs.end())
2386
2
    return true;
2387
27.2k
  Reg = RegInfo->getValue();
2388
27.2k
  return false;
2389
27.2k
}
2390
2391
115
void MIParser::initNames2RegMasks() {
2392
115
  if (!Names2RegMasks.empty())
2393
52
    return;
2394
63
  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2395
63
  assert(TRI && "Expected target register info");
2396
63
  ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2397
63
  ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2398
63
  assert(RegMasks.size() == RegMaskNames.size());
2399
1.67k
  for (size_t I = 0, E = RegMasks.size(); 
I < E1.67k
;
++I1.61k
)
2400
1.61k
    Names2RegMasks.insert(
2401
1.61k
        std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2402
115
}
2403
2404
115
const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2405
115
  initNames2RegMasks();
2406
115
  auto RegMaskInfo = Names2RegMasks.find(Identifier);
2407
115
  if (RegMaskInfo == Names2RegMasks.end())
2408
1
    return nullptr;
2409
114
  return RegMaskInfo->getValue();
2410
114
}
2411
2412
451
void MIParser::initNames2SubRegIndices() {
2413
451
  if (!Names2SubRegIndices.empty())
2414
368
    return;
2415
83
  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2416
4.57k
  for (unsigned I = 1, E = TRI->getNumSubRegIndices(); 
I < E4.57k
;
++I4.49k
)
2417
4.49k
    Names2SubRegIndices.insert(
2418
4.49k
        std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2419
451
}
2420
2421
451
unsigned MIParser::getSubRegIndex(StringRef Name) {
2422
451
  initNames2SubRegIndices();
2423
451
  auto SubRegInfo = Names2SubRegIndices.find(Name);
2424
451
  if (SubRegInfo == Names2SubRegIndices.end())
2425
2
    return 0;
2426
449
  return SubRegInfo->getValue();
2427
449
}
2428
2429
static void initSlots2BasicBlocks(
2430
    const Function &F,
2431
728
    DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2432
728
  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2433
728
  MST.incorporateFunction(F);
2434
785
  for (auto &BB : F) {
2435
785
    if (BB.hasName())
2436
54
      continue;
2437
731
    int Slot = MST.getLocalSlot(&BB);
2438
731
    if (Slot == -1)
2439
0
      continue;
2440
731
    Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2441
731
  }
2442
728
}
2443
2444
static const BasicBlock *getIRBlockFromSlot(
2445
    unsigned Slot,
2446
732
    const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2447
732
  auto BlockInfo = Slots2BasicBlocks.find(Slot);
2448
732
  if (BlockInfo == Slots2BasicBlocks.end())
2449
2
    return nullptr;
2450
730
  return BlockInfo->second;
2451
730
}
2452
2453
731
const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2454
731
  if (Slots2BasicBlocks.empty())
2455
727
    initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2456
731
  return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2457
731
}
2458
2459
732
const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2460
732
  if (&F == MF.getFunction())
2461
731
    return getIRBlock(Slot);
2462
1
  DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2463
1
  initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2464
1
  return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2465
1
}
2466
2467
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2468
133
                           DenseMap<unsigned, const Value *> &Slots2Values) {
2469
133
  int Slot = MST.getLocalSlot(V);
2470
133
  if (Slot == -1)
2471
86
    return;
2472
47
  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2473
47
}
2474
2475
/// Creates the mapping from slot numbers to function's unnamed IR values.
2476
static void initSlots2Values(const Function &F,
2477
8
                             DenseMap<unsigned, const Value *> &Slots2Values) {
2478
8
  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2479
8
  MST.incorporateFunction(F);
2480
8
  for (const auto &Arg : F.args())
2481
9
    mapValueToSlot(&Arg, MST, Slots2Values);
2482
14
  for (const auto &BB : F) {
2483
14
    mapValueToSlot(&BB, MST, Slots2Values);
2484
14
    for (const auto &I : BB)
2485
110
      mapValueToSlot(&I, MST, Slots2Values);
2486
14
  }
2487
8
}
2488
2489
22
const Value *MIParser::getIRValue(unsigned Slot) {
2490
22
  if (Slots2Values.empty())
2491
8
    initSlots2Values(*MF.getFunction(), Slots2Values);
2492
22
  auto ValueInfo = Slots2Values.find(Slot);
2493
22
  if (ValueInfo == Slots2Values.end())
2494
0
    return nullptr;
2495
22
  return ValueInfo->second;
2496
22
}
2497
2498
3
void MIParser::initNames2TargetIndices() {
2499
3
  if (!Names2TargetIndices.empty())
2500
0
    return;
2501
3
  const auto *TII = MF.getSubtarget().getInstrInfo();
2502
3
  assert(TII && "Expected target instruction info");
2503
3
  auto Indices = TII->getSerializableTargetIndices();
2504
3
  for (const auto &I : Indices)
2505
15
    Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2506
3
}
2507
2508
3
bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2509
3
  initNames2TargetIndices();
2510
3
  auto IndexInfo = Names2TargetIndices.find(Name);
2511
3
  if (IndexInfo == Names2TargetIndices.end())
2512
1
    return true;
2513
2
  Index = IndexInfo->second;
2514
2
  return false;
2515
2
}
2516
2517
47
void MIParser::initNames2DirectTargetFlags() {
2518
47
  if (!Names2DirectTargetFlags.empty())
2519
31
    return;
2520
16
  const auto *TII = MF.getSubtarget().getInstrInfo();
2521
16
  assert(TII && "Expected target instruction info");
2522
16
  auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2523
16
  for (const auto &I : Flags)
2524
170
    Names2DirectTargetFlags.insert(
2525
170
        std::make_pair(StringRef(I.second), I.first));
2526
47
}
2527
2528
47
bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2529
47
  initNames2DirectTargetFlags();
2530
47
  auto FlagInfo = Names2DirectTargetFlags.find(Name);
2531
47
  if (FlagInfo == Names2DirectTargetFlags.end())
2532
4
    return true;
2533
43
  Flag = FlagInfo->second;
2534
43
  return false;
2535
43
}
2536
2537
15
void MIParser::initNames2BitmaskTargetFlags() {
2538
15
  if (!Names2BitmaskTargetFlags.empty())
2539
8
    return;
2540
7
  const auto *TII = MF.getSubtarget().getInstrInfo();
2541
7
  assert(TII && "Expected target instruction info");
2542
7
  auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2543
7
  for (const auto &I : Flags)
2544
16
    Names2BitmaskTargetFlags.insert(
2545
16
        std::make_pair(StringRef(I.second), I.first));
2546
15
}
2547
2548
15
bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2549
15
  initNames2BitmaskTargetFlags();
2550
15
  auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2551
15
  if (FlagInfo == Names2BitmaskTargetFlags.end())
2552
2
    return true;
2553
13
  Flag = FlagInfo->second;
2554
13
  return false;
2555
13
}
2556
2557
23
void MIParser::initNames2MMOTargetFlags() {
2558
23
  if (!Names2MMOTargetFlags.empty())
2559
5
    return;
2560
18
  const auto *TII = MF.getSubtarget().getInstrInfo();
2561
18
  assert(TII && "Expected target instruction info");
2562
18
  auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
2563
18
  for (const auto &I : Flags)
2564
36
    Names2MMOTargetFlags.insert(
2565
36
        std::make_pair(StringRef(I.second), I.first));
2566
23
}
2567
2568
bool MIParser::getMMOTargetFlag(StringRef Name,
2569
23
                                MachineMemOperand::Flags &Flag) {
2570
23
  initNames2MMOTargetFlags();
2571
23
  auto FlagInfo = Names2MMOTargetFlags.find(Name);
2572
23
  if (FlagInfo == Names2MMOTargetFlags.end())
2573
1
    return true;
2574
22
  Flag = FlagInfo->second;
2575
22
  return false;
2576
22
}
2577
2578
6
bool MIParser::parseStringConstant(std::string &Result) {
2579
6
  if (Token.isNot(MIToken::StringConstant))
2580
0
    return error("expected string constant");
2581
6
  Result = Token.stringValue();
2582
6
  lex();
2583
6
  return false;
2584
6
}
2585
2586
bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2587
                                             StringRef Src,
2588
2.32k
                                             SMDiagnostic &Error) {
2589
2.32k
  return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2590
2.32k
}
2591
2592
bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
2593
2.30k
                                    StringRef Src, SMDiagnostic &Error) {
2594
2.30k
  return MIParser(PFS, Error, Src).parseBasicBlocks();
2595
2.30k
}
2596
2597
bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
2598
                             MachineBasicBlock *&MBB, StringRef Src,
2599
87
                             SMDiagnostic &Error) {
2600
87
  return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
2601
87
}
2602
2603
bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
2604
                                  unsigned &Reg, StringRef Src,
2605
9
                                  SMDiagnostic &Error) {
2606
9
  return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2607
9
}
2608
2609
bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
2610
                                       unsigned &Reg, StringRef Src,
2611
1.32k
                                       SMDiagnostic &Error) {
2612
1.32k
  return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
2613
1.32k
}
2614
2615
bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2616
                                         VRegInfo *&Info, StringRef Src,
2617
161
                                         SMDiagnostic &Error) {
2618
161
  return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
2619
161
}
2620
2621
bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
2622
                                     int &FI, StringRef Src,
2623
2
                                     SMDiagnostic &Error) {
2624
2
  return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
2625
2
}
2626
2627
bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
2628
7
                       MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2629
7
  return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
2630
7
}