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