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(TRI->getSubRegIndexName(I), 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  if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) {
1858  lex();
1859  Elements.push_back(Enc);
1860  continue;
1861  }
1862  return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1863  }
1864 
1865  if (Token.isNot(MIToken::IntegerLiteral) ||
1866  Token.integerValue().isSigned())
1867  return error("expected unsigned integer");
1868 
1869  auto &U = Token.integerValue();
1870  if (U.ugt(UINT64_MAX))
1871  return error("element too large, limit is " + Twine(UINT64_MAX));
1872  Elements.push_back(U.getZExtValue());
1873  lex();
1874 
1875  } while (consumeIfPresent(MIToken::comma));
1876  }
1877 
1878  if (expectAndConsume(MIToken::rparen))
1879  return true;
1880 
1881  Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
1882  return false;
1883 }
1884 
1885 bool MIParser::parseDILocation(MDNode *&Loc) {
1886  assert(Token.is(MIToken::md_dilocation));
1887  lex();
1888 
1889  bool HaveLine = false;
1890  unsigned Line = 0;
1891  unsigned Column = 0;
1892  MDNode *Scope = nullptr;
1893  MDNode *InlinedAt = nullptr;
1894  bool ImplicitCode = false;
1895 
1896  if (expectAndConsume(MIToken::lparen))
1897  return true;
1898 
1899  if (Token.isNot(MIToken::rparen)) {
1900  do {
1901  if (Token.is(MIToken::Identifier)) {
1902  if (Token.stringValue() == "line") {
1903  lex();
1904  if (expectAndConsume(MIToken::colon))
1905  return true;
1906  if (Token.isNot(MIToken::IntegerLiteral) ||
1907  Token.integerValue().isSigned())
1908  return error("expected unsigned integer");
1909  Line = Token.integerValue().getZExtValue();
1910  HaveLine = true;
1911  lex();
1912  continue;
1913  }
1914  if (Token.stringValue() == "column") {
1915  lex();
1916  if (expectAndConsume(MIToken::colon))
1917  return true;
1918  if (Token.isNot(MIToken::IntegerLiteral) ||
1919  Token.integerValue().isSigned())
1920  return error("expected unsigned integer");
1921  Column = Token.integerValue().getZExtValue();
1922  lex();
1923  continue;
1924  }
1925  if (Token.stringValue() == "scope") {
1926  lex();
1927  if (expectAndConsume(MIToken::colon))
1928  return true;
1929  if (parseMDNode(Scope))
1930  return error("expected metadata node");
1931  if (!isa<DIScope>(Scope))
1932  return error("expected DIScope node");
1933  continue;
1934  }
1935  if (Token.stringValue() == "inlinedAt") {
1936  lex();
1937  if (expectAndConsume(MIToken::colon))
1938  return true;
1939  if (Token.is(MIToken::exclaim)) {
1940  if (parseMDNode(InlinedAt))
1941  return true;
1942  } else if (Token.is(MIToken::md_dilocation)) {
1943  if (parseDILocation(InlinedAt))
1944  return true;
1945  } else
1946  return error("expected metadata node");
1947  if (!isa<DILocation>(InlinedAt))
1948  return error("expected DILocation node");
1949  continue;
1950  }
1951  if (Token.stringValue() == "isImplicitCode") {
1952  lex();
1953  if (expectAndConsume(MIToken::colon))
1954  return true;
1955  if (!Token.is(MIToken::Identifier))
1956  return error("expected true/false");
1957  // As far as I can see, we don't have any existing need for parsing
1958  // true/false in MIR yet. Do it ad-hoc until there's something else
1959  // that needs it.
1960  if (Token.stringValue() == "true")
1961  ImplicitCode = true;
1962  else if (Token.stringValue() == "false")
1963  ImplicitCode = false;
1964  else
1965  return error("expected true/false");
1966  lex();
1967  continue;
1968  }
1969  }
1970  return error(Twine("invalid DILocation argument '") +
1971  Token.stringValue() + "'");
1972  } while (consumeIfPresent(MIToken::comma));
1973  }
1974 
1975  if (expectAndConsume(MIToken::rparen))
1976  return true;
1977 
1978  if (!HaveLine)
1979  return error("DILocation requires line number");
1980  if (!Scope)
1981  return error("DILocation requires a scope");
1982 
1983  Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
1984  InlinedAt, ImplicitCode);
1985  return false;
1986 }
1987 
1988 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1989  MDNode *Node = nullptr;
1990  if (Token.is(MIToken::exclaim)) {
1991  if (parseMDNode(Node))
1992  return true;
1993  } else if (Token.is(MIToken::md_diexpr)) {
1994  if (parseDIExpression(Node))
1995  return true;
1996  }
1997  Dest = MachineOperand::CreateMetadata(Node);
1998  return false;
1999 }
2000 
2001 bool MIParser::parseCFIOffset(int &Offset) {
2002  if (Token.isNot(MIToken::IntegerLiteral))
2003  return error("expected a cfi offset");
2004  if (Token.integerValue().getMinSignedBits() > 32)
2005  return error("expected a 32 bit integer (the cfi offset is too large)");
2006  Offset = (int)Token.integerValue().getExtValue();
2007  lex();
2008  return false;
2009 }
2010 
2011 bool MIParser::parseCFIRegister(unsigned &Reg) {
2012  if (Token.isNot(MIToken::NamedRegister))
2013  return error("expected a cfi register");
2014  unsigned LLVMReg;
2015  if (parseNamedRegister(LLVMReg))
2016  return true;
2017  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2018  assert(TRI && "Expected target register info");
2019  int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2020  if (DwarfReg < 0)
2021  return error("invalid DWARF register");
2022  Reg = (unsigned)DwarfReg;
2023  lex();
2024  return false;
2025 }
2026 
2027 bool MIParser::parseCFIEscapeValues(std::string &Values) {
2028  do {
2029  if (Token.isNot(MIToken::HexLiteral))
2030  return error("expected a hexadecimal literal");
2031  unsigned Value;
2032  if (getUnsigned(Value))
2033  return true;
2034  if (Value > UINT8_MAX)
2035  return error("expected a 8-bit integer (too large)");
2036  Values.push_back(static_cast<uint8_t>(Value));
2037  lex();
2038  } while (consumeIfPresent(MIToken::comma));
2039  return false;
2040 }
2041 
2042 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2043  auto Kind = Token.kind();
2044  lex();
2045  int Offset;
2046  unsigned Reg;
2047  unsigned CFIIndex;
2048  switch (Kind) {
2050  if (parseCFIRegister(Reg))
2051  return true;
2052  CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2053  break;
2055  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2056  parseCFIOffset(Offset))
2057  return true;
2058  CFIIndex =
2059  MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2060  break;
2062  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2063  parseCFIOffset(Offset))
2064  return true;
2065  CFIIndex = MF.addFrameInst(
2066  MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
2067  break;
2069  if (parseCFIRegister(Reg))
2070  return true;
2071  CFIIndex =
2073  break;
2075  if (parseCFIOffset(Offset))
2076  return true;
2077  // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
2078  CFIIndex = MF.addFrameInst(
2079  MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
2080  break;
2082  if (parseCFIOffset(Offset))
2083  return true;
2084  CFIIndex = MF.addFrameInst(
2085  MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
2086  break;
2088  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2089  parseCFIOffset(Offset))
2090  return true;
2091  // NB: MCCFIInstruction::createDefCfa negates the offset.
2092  CFIIndex =
2093  MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
2094  break;
2097  break;
2099  if (parseCFIRegister(Reg))
2100  return true;
2101  CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2102  break;
2105  break;
2107  if (parseCFIRegister(Reg))
2108  return true;
2109  CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2110  break;
2111  case MIToken::kw_cfi_register: {
2112  unsigned Reg2;
2113  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2114  parseCFIRegister(Reg2))
2115  return true;
2116 
2117  CFIIndex =
2118  MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2119  break;
2120  }
2122  CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2123  break;
2126  break;
2127  case MIToken::kw_cfi_escape: {
2128  std::string Values;
2129  if (parseCFIEscapeValues(Values))
2130  return true;
2131  CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2132  break;
2133  }
2134  default:
2135  // TODO: Parse the other CFI operands.
2136  llvm_unreachable("The current token should be a cfi operand");
2137  }
2138  Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2139  return false;
2140 }
2141 
2142 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2143  switch (Token.kind()) {
2144  case MIToken::NamedIRBlock: {
2145  BB = dyn_cast_or_null<BasicBlock>(
2146  F.getValueSymbolTable()->lookup(Token.stringValue()));
2147  if (!BB)
2148  return error(Twine("use of undefined IR block '") + Token.range() + "'");
2149  break;
2150  }
2151  case MIToken::IRBlock: {
2152  unsigned SlotNumber = 0;
2153  if (getUnsigned(SlotNumber))
2154  return true;
2155  BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2156  if (!BB)
2157  return error(Twine("use of undefined IR block '%ir-block.") +
2158  Twine(SlotNumber) + "'");
2159  break;
2160  }
2161  default:
2162  llvm_unreachable("The current token should be an IR block reference");
2163  }
2164  return false;
2165 }
2166 
2167 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2168  assert(Token.is(MIToken::kw_blockaddress));
2169  lex();
2170  if (expectAndConsume(MIToken::lparen))
2171  return true;
2172  if (Token.isNot(MIToken::GlobalValue) &&
2173  Token.isNot(MIToken::NamedGlobalValue))
2174  return error("expected a global value");
2175  GlobalValue *GV = nullptr;
2176  if (parseGlobalValue(GV))
2177  return true;
2178  auto *F = dyn_cast<Function>(GV);
2179  if (!F)
2180  return error("expected an IR function reference");
2181  lex();
2182  if (expectAndConsume(MIToken::comma))
2183  return true;
2184  BasicBlock *BB = nullptr;
2185  if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2186  return error("expected an IR block reference");
2187  if (parseIRBlock(BB, *F))
2188  return true;
2189  lex();
2190  if (expectAndConsume(MIToken::rparen))
2191  return true;
2192  Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2193  if (parseOperandsOffset(Dest))
2194  return true;
2195  return false;
2196 }
2197 
2198 bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2199  assert(Token.is(MIToken::kw_intrinsic));
2200  lex();
2201  if (expectAndConsume(MIToken::lparen))
2202  return error("expected syntax intrinsic(@llvm.whatever)");
2203 
2204  if (Token.isNot(MIToken::NamedGlobalValue))
2205  return error("expected syntax intrinsic(@llvm.whatever)");
2206 
2207  std::string Name = Token.stringValue();
2208  lex();
2209 
2210  if (expectAndConsume(MIToken::rparen))
2211  return error("expected ')' to terminate intrinsic name");
2212 
2213  // Find out what intrinsic we're dealing with, first try the global namespace
2214  // and then the target's private intrinsics if that fails.
2217  if (ID == Intrinsic::not_intrinsic && TII)
2218  ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
2219 
2220  if (ID == Intrinsic::not_intrinsic)
2221  return error("unknown intrinsic name");
2223 
2224  return false;
2225 }
2226 
2227 bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2228  assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2229  bool IsFloat = Token.is(MIToken::kw_floatpred);
2230  lex();
2231 
2232  if (expectAndConsume(MIToken::lparen))
2233  return error("expected syntax intpred(whatever) or floatpred(whatever");
2234 
2235  if (Token.isNot(MIToken::Identifier))
2236  return error("whatever");
2237 
2238  CmpInst::Predicate Pred;
2239  if (IsFloat) {
2240  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2241  .Case("false", CmpInst::FCMP_FALSE)
2242  .Case("oeq", CmpInst::FCMP_OEQ)
2243  .Case("ogt", CmpInst::FCMP_OGT)
2244  .Case("oge", CmpInst::FCMP_OGE)
2245  .Case("olt", CmpInst::FCMP_OLT)
2246  .Case("ole", CmpInst::FCMP_OLE)
2247  .Case("one", CmpInst::FCMP_ONE)
2248  .Case("ord", CmpInst::FCMP_ORD)
2249  .Case("uno", CmpInst::FCMP_UNO)
2250  .Case("ueq", CmpInst::FCMP_UEQ)
2251  .Case("ugt", CmpInst::FCMP_UGT)
2252  .Case("uge", CmpInst::FCMP_UGE)
2253  .Case("ult", CmpInst::FCMP_ULT)
2254  .Case("ule", CmpInst::FCMP_ULE)
2255  .Case("une", CmpInst::FCMP_UNE)
2256  .Case("true", CmpInst::FCMP_TRUE)
2258  if (!CmpInst::isFPPredicate(Pred))
2259  return error("invalid floating-point predicate");
2260  } else {
2261  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2262  .Case("eq", CmpInst::ICMP_EQ)
2263  .Case("ne", CmpInst::ICMP_NE)
2264  .Case("sgt", CmpInst::ICMP_SGT)
2265  .Case("sge", CmpInst::ICMP_SGE)
2266  .Case("slt", CmpInst::ICMP_SLT)
2267  .Case("sle", CmpInst::ICMP_SLE)
2268  .Case("ugt", CmpInst::ICMP_UGT)
2269  .Case("uge", CmpInst::ICMP_UGE)
2270  .Case("ult", CmpInst::ICMP_ULT)
2271  .Case("ule", CmpInst::ICMP_ULE)
2273  if (!CmpInst::isIntPredicate(Pred))
2274  return error("invalid integer predicate");
2275  }
2276 
2277  lex();
2278  Dest = MachineOperand::CreatePredicate(Pred);
2279  if (expectAndConsume(MIToken::rparen))
2280  return error("predicate should be terminated by ')'.");
2281 
2282  return false;
2283 }
2284 
2285 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2286  assert(Token.is(MIToken::kw_target_index));
2287  lex();
2288  if (expectAndConsume(MIToken::lparen))
2289  return true;
2290  if (Token.isNot(MIToken::Identifier))
2291  return error("expected the name of the target index");
2292  int Index = 0;
2293  if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2294  return error("use of undefined target index '" + Token.stringValue() + "'");
2295  lex();
2296  if (expectAndConsume(MIToken::rparen))
2297  return true;
2298  Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2299  if (parseOperandsOffset(Dest))
2300  return true;
2301  return false;
2302 }
2303 
2304 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2305  assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2306  lex();
2307  if (expectAndConsume(MIToken::lparen))
2308  return true;
2309 
2311  while (true) {
2312  if (Token.isNot(MIToken::NamedRegister))
2313  return error("expected a named register");
2314  unsigned Reg;
2315  if (parseNamedRegister(Reg))
2316  return true;
2317  lex();
2318  Mask[Reg / 32] |= 1U << (Reg % 32);
2319  // TODO: Report an error if the same register is used more than once.
2320  if (Token.isNot(MIToken::comma))
2321  break;
2322  lex();
2323  }
2324 
2325  if (expectAndConsume(MIToken::rparen))
2326  return true;
2327  Dest = MachineOperand::CreateRegMask(Mask);
2328  return false;
2329 }
2330 
2331 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2332  assert(Token.is(MIToken::kw_liveout));
2334  lex();
2335  if (expectAndConsume(MIToken::lparen))
2336  return true;
2337  while (true) {
2338  if (Token.isNot(MIToken::NamedRegister))
2339  return error("expected a named register");
2340  unsigned Reg;
2341  if (parseNamedRegister(Reg))
2342  return true;
2343  lex();
2344  Mask[Reg / 32] |= 1U << (Reg % 32);
2345  // TODO: Report an error if the same register is used more than once.
2346  if (Token.isNot(MIToken::comma))
2347  break;
2348  lex();
2349  }
2350  if (expectAndConsume(MIToken::rparen))
2351  return true;
2352  Dest = MachineOperand::CreateRegLiveOut(Mask);
2353  return false;
2354 }
2355 
2356 bool MIParser::parseMachineOperand(MachineOperand &Dest,
2357  Optional<unsigned> &TiedDefIdx) {
2358  switch (Token.kind()) {
2359  case MIToken::kw_implicit:
2361  case MIToken::kw_def:
2362  case MIToken::kw_dead:
2363  case MIToken::kw_killed:
2364  case MIToken::kw_undef:
2365  case MIToken::kw_internal:
2367  case MIToken::kw_debug_use:
2368  case MIToken::kw_renamable:
2369  case MIToken::underscore:
2373  return parseRegisterOperand(Dest, TiedDefIdx);
2375  return parseImmediateOperand(Dest);
2376  case MIToken::kw_half:
2377  case MIToken::kw_float:
2378  case MIToken::kw_double:
2379  case MIToken::kw_x86_fp80:
2380  case MIToken::kw_fp128:
2381  case MIToken::kw_ppc_fp128:
2382  return parseFPImmediateOperand(Dest);
2384  return parseMBBOperand(Dest);
2385  case MIToken::StackObject:
2386  return parseStackObjectOperand(Dest);
2388  return parseFixedStackObjectOperand(Dest);
2389  case MIToken::GlobalValue:
2391  return parseGlobalAddressOperand(Dest);
2393  return parseConstantPoolIndexOperand(Dest);
2395  return parseJumpTableIndexOperand(Dest);
2397  return parseExternalSymbolOperand(Dest);
2398  case MIToken::MCSymbol:
2399  return parseMCSymbolOperand(Dest);
2401  return parseSubRegisterIndexOperand(Dest);
2402  case MIToken::md_diexpr:
2403  case MIToken::exclaim:
2404  return parseMetadataOperand(Dest);
2420  return parseCFIOperand(Dest);
2422  return parseBlockAddressOperand(Dest);
2423  case MIToken::kw_intrinsic:
2424  return parseIntrinsicOperand(Dest);
2426  return parseTargetIndexOperand(Dest);
2427  case MIToken::kw_liveout:
2428  return parseLiveoutRegisterMaskOperand(Dest);
2429  case MIToken::kw_floatpred:
2430  case MIToken::kw_intpred:
2431  return parsePredicateOperand(Dest);
2432  case MIToken::Error:
2433  return true;
2434  case MIToken::Identifier:
2435  if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
2436  Dest = MachineOperand::CreateRegMask(RegMask);
2437  lex();
2438  break;
2439  } else if (Token.stringValue() == "CustomRegMask") {
2440  return parseCustomRegisterMaskOperand(Dest);
2441  } else
2442  return parseTypedImmediateOperand(Dest);
2443  default:
2444  // FIXME: Parse the MCSymbol machine operand.
2445  return error("expected a machine operand");
2446  }
2447  return false;
2448 }
2449 
2450 bool MIParser::parseMachineOperandAndTargetFlags(
2451  MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
2452  unsigned TF = 0;
2453  bool HasTargetFlags = false;
2454  if (Token.is(MIToken::kw_target_flags)) {
2455  HasTargetFlags = true;
2456  lex();
2457  if (expectAndConsume(MIToken::lparen))
2458  return true;
2459  if (Token.isNot(MIToken::Identifier))
2460  return error("expected the name of the target flag");
2461  if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
2462  if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
2463  return error("use of undefined target flag '" + Token.stringValue() +
2464  "'");
2465  }
2466  lex();
2467  while (Token.is(MIToken::comma)) {
2468  lex();
2469  if (Token.isNot(MIToken::Identifier))
2470  return error("expected the name of the target flag");
2471  unsigned BitFlag = 0;
2472  if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2473  return error("use of undefined target flag '" + Token.stringValue() +
2474  "'");
2475  // TODO: Report an error when using a duplicate bit target flag.
2476  TF |= BitFlag;
2477  lex();
2478  }
2479  if (expectAndConsume(MIToken::rparen))
2480  return true;
2481  }
2482  auto Loc = Token.location();
2483  if (parseMachineOperand(Dest, TiedDefIdx))
2484  return true;
2485  if (!HasTargetFlags)
2486  return false;
2487  if (Dest.isReg())
2488  return error(Loc, "register operands can't have target flags");
2489  Dest.setTargetFlags(TF);
2490  return false;
2491 }
2492 
2493 bool MIParser::parseOffset(int64_t &Offset) {
2494  if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2495  return false;
2496  StringRef Sign = Token.range();
2497  bool IsNegative = Token.is(MIToken::minus);
2498  lex();
2499  if (Token.isNot(MIToken::IntegerLiteral))
2500  return error("expected an integer literal after '" + Sign + "'");
2501  if (Token.integerValue().getMinSignedBits() > 64)
2502  return error("expected 64-bit integer (too large)");
2503  Offset = Token.integerValue().getExtValue();
2504  if (IsNegative)
2505  Offset = -Offset;
2506  lex();
2507  return false;
2508 }
2509 
2510 bool MIParser::parseAlignment(unsigned &Alignment) {
2511  assert(Token.is(MIToken::kw_align));
2512  lex();
2513  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2514  return error("expected an integer literal after 'align'");
2515  if (getUnsigned(Alignment))
2516  return true;
2517  lex();
2518 
2519  if (!isPowerOf2_32(Alignment))
2520  return error("expected a power-of-2 literal after 'align'");
2521 
2522  return false;
2523 }
2524 
2525 bool MIParser::parseAddrspace(unsigned &Addrspace) {
2526  assert(Token.is(MIToken::kw_addrspace));
2527  lex();
2528  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2529  return error("expected an integer literal after 'addrspace'");
2530  if (getUnsigned(Addrspace))
2531  return true;
2532  lex();
2533  return false;
2534 }
2535 
2536 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2537  int64_t Offset = 0;
2538  if (parseOffset(Offset))
2539  return true;
2540  Op.setOffset(Offset);
2541  return false;
2542 }
2543 
2544 bool MIParser::parseIRValue(const Value *&V) {
2545  switch (Token.kind()) {
2546  case MIToken::NamedIRValue: {
2547  V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
2548  break;
2549  }
2550  case MIToken::IRValue: {
2551  unsigned SlotNumber = 0;
2552  if (getUnsigned(SlotNumber))
2553  return true;
2554  V = getIRValue(SlotNumber);
2555  break;
2556  }
2558  case MIToken::GlobalValue: {
2559  GlobalValue *GV = nullptr;
2560  if (parseGlobalValue(GV))
2561  return true;
2562  V = GV;
2563  break;
2564  }
2565  case MIToken::QuotedIRValue: {
2566  const Constant *C = nullptr;
2567  if (parseIRConstant(Token.location(), Token.stringValue(), C))
2568  return true;
2569  V = C;
2570  break;
2571  }
2572  default:
2573  llvm_unreachable("The current token should be an IR block reference");
2574  }
2575  if (!V)
2576  return error(Twine("use of undefined IR value '") + Token.range() + "'");
2577  return false;
2578 }
2579 
2580 bool MIParser::getUint64(uint64_t &Result) {
2581  if (Token.hasIntegerValue()) {
2582  if (Token.integerValue().getActiveBits() > 64)
2583  return error("expected 64-bit integer (too large)");
2584  Result = Token.integerValue().getZExtValue();
2585  return false;
2586  }
2587  if (Token.is(MIToken::HexLiteral)) {
2588  APInt A;
2589  if (getHexUint(A))
2590  return true;
2591  if (A.getBitWidth() > 64)
2592  return error("expected 64-bit integer (too large)");
2593  Result = A.getZExtValue();
2594  return false;
2595  }
2596  return true;
2597 }
2598 
2599 bool MIParser::getHexUint(APInt &Result) {
2600  assert(Token.is(MIToken::HexLiteral));
2601  StringRef S = Token.range();
2602  assert(S[0] == '0' && tolower(S[1]) == 'x');
2603  // This could be a floating point literal with a special prefix.
2604  if (!isxdigit(S[2]))
2605  return true;
2606  StringRef V = S.substr(2);
2607  APInt A(V.size()*4, V, 16);
2608 
2609  // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2610  // sure it isn't the case before constructing result.
2611  unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2612  Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2613  return false;
2614 }
2615 
2616 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2617  const auto OldFlags = Flags;
2618  switch (Token.kind()) {
2619  case MIToken::kw_volatile:
2621  break;
2624  break;
2627  break;
2628  case MIToken::kw_invariant:
2630  break;
2631  case MIToken::StringConstant: {
2633  if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
2634  return error("use of undefined target MMO flag '" + Token.stringValue() +
2635  "'");
2636  Flags |= TF;
2637  break;
2638  }
2639  default:
2640  llvm_unreachable("The current token should be a memory operand flag");
2641  }
2642  if (OldFlags == Flags)
2643  // We know that the same flag is specified more than once when the flags
2644  // weren't modified.
2645  return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2646  lex();
2647  return false;
2648 }
2649 
2650 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2651  switch (Token.kind()) {
2652  case MIToken::kw_stack:
2653  PSV = MF.getPSVManager().getStack();
2654  break;
2655  case MIToken::kw_got:
2656  PSV = MF.getPSVManager().getGOT();
2657  break;
2659  PSV = MF.getPSVManager().getJumpTable();
2660  break;
2662  PSV = MF.getPSVManager().getConstantPool();
2663  break;
2665  int FI;
2666  if (parseFixedStackFrameIndex(FI))
2667  return true;
2668  PSV = MF.getPSVManager().getFixedStack(FI);
2669  // The token was already consumed, so use return here instead of break.
2670  return false;
2671  }
2672  case MIToken::StackObject: {
2673  int FI;
2674  if (parseStackFrameIndex(FI))
2675  return true;
2676  PSV = MF.getPSVManager().getFixedStack(FI);
2677  // The token was already consumed, so use return here instead of break.
2678  return false;
2679  }
2681  lex();
2682  switch (Token.kind()) {
2683  case MIToken::GlobalValue:
2685  GlobalValue *GV = nullptr;
2686  if (parseGlobalValue(GV))
2687  return true;
2689  break;
2690  }
2693  MF.createExternalSymbolName(Token.stringValue()));
2694  break;
2695  default:
2696  return error(
2697  "expected a global value or an external symbol after 'call-entry'");
2698  }
2699  break;
2700  default:
2701  llvm_unreachable("The current token should be pseudo source value");
2702  }
2703  lex();
2704  return false;
2705 }
2706 
2707 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2708  if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
2709  Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
2710  Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2711  Token.is(MIToken::kw_call_entry)) {
2712  const PseudoSourceValue *PSV = nullptr;
2713  if (parseMemoryPseudoSourceValue(PSV))
2714  return true;
2715  int64_t Offset = 0;
2716  if (parseOffset(Offset))
2717  return true;
2718  Dest = MachinePointerInfo(PSV, Offset);
2719  return false;
2720  }
2721  if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2722  Token.isNot(MIToken::GlobalValue) &&
2723  Token.isNot(MIToken::NamedGlobalValue) &&
2724  Token.isNot(MIToken::QuotedIRValue))
2725  return error("expected an IR value reference");
2726  const Value *V = nullptr;
2727  if (parseIRValue(V))
2728  return true;
2729  if (!V->getType()->isPointerTy())
2730  return error("expected a pointer IR value");
2731  lex();
2732  int64_t Offset = 0;
2733  if (parseOffset(Offset))
2734  return true;
2735  Dest = MachinePointerInfo(V, Offset);
2736  return false;
2737 }
2738 
2739 bool MIParser::parseOptionalScope(LLVMContext &Context,
2740  SyncScope::ID &SSID) {
2741  SSID = SyncScope::System;
2742  if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
2743  lex();
2744  if (expectAndConsume(MIToken::lparen))
2745  return error("expected '(' in syncscope");
2746 
2747  std::string SSN;
2748  if (parseStringConstant(SSN))
2749  return true;
2750 
2751  SSID = Context.getOrInsertSyncScopeID(SSN);
2752  if (expectAndConsume(MIToken::rparen))
2753  return error("expected ')' in syncscope");
2754  }
2755 
2756  return false;
2757 }
2758 
2759 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2760  Order = AtomicOrdering::NotAtomic;
2761  if (Token.isNot(MIToken::Identifier))
2762  return false;
2763 
2764  Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2765  .Case("unordered", AtomicOrdering::Unordered)
2766  .Case("monotonic", AtomicOrdering::Monotonic)
2767  .Case("acquire", AtomicOrdering::Acquire)
2768  .Case("release", AtomicOrdering::Release)
2772 
2773  if (Order != AtomicOrdering::NotAtomic) {
2774  lex();
2775  return false;
2776  }
2777 
2778  return error("expected an atomic scope, ordering or a size specification");
2779 }
2780 
2781 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2782  if (expectAndConsume(MIToken::lparen))
2783  return true;
2785  while (Token.isMemoryOperandFlag()) {
2786  if (parseMemoryOperandFlag(Flags))
2787  return true;
2788  }
2789  if (Token.isNot(MIToken::Identifier) ||
2790  (Token.stringValue() != "load" && Token.stringValue() != "store"))
2791  return error("expected 'load' or 'store' memory operation");
2792  if (Token.stringValue() == "load")
2793  Flags |= MachineMemOperand::MOLoad;
2794  else
2795  Flags |= MachineMemOperand::MOStore;
2796  lex();
2797 
2798  // Optional 'store' for operands that both load and store.
2799  if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
2800  Flags |= MachineMemOperand::MOStore;
2801  lex();
2802  }
2803 
2804  // Optional synchronization scope.
2805  SyncScope::ID SSID;
2806  if (parseOptionalScope(MF.getFunction().getContext(), SSID))
2807  return true;
2808 
2809  // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2810  AtomicOrdering Order, FailureOrder;
2811  if (parseOptionalAtomicOrdering(Order))
2812  return true;
2813 
2814  if (parseOptionalAtomicOrdering(FailureOrder))
2815  return true;
2816 
2817  if (Token.isNot(MIToken::IntegerLiteral) &&
2818  Token.isNot(MIToken::kw_unknown_size))
2819  return error("expected the size integer literal or 'unknown-size' after "
2820  "memory operation");
2821  uint64_t Size;
2822  if (Token.is(MIToken::IntegerLiteral)) {
2823  if (getUint64(Size))
2824  return true;
2825  } else if (Token.is(MIToken::kw_unknown_size)) {
2827  }
2828  lex();
2829 
2831  if (Token.is(MIToken::Identifier)) {
2832  const char *Word =
2833  ((Flags & MachineMemOperand::MOLoad) &&
2834  (Flags & MachineMemOperand::MOStore))
2835  ? "on"
2836  : Flags & MachineMemOperand::MOLoad ? "from" : "into";
2837  if (Token.stringValue() != Word)
2838  return error(Twine("expected '") + Word + "'");
2839  lex();
2840 
2841  if (parseMachinePointerInfo(Ptr))
2842  return true;
2843  }
2844  unsigned BaseAlignment = (Size != MemoryLocation::UnknownSize ? Size : 1);
2845  AAMDNodes AAInfo;
2846  MDNode *Range = nullptr;
2847  while (consumeIfPresent(MIToken::comma)) {
2848  switch (Token.kind()) {
2849  case MIToken::kw_align:
2850  if (parseAlignment(BaseAlignment))
2851  return true;
2852  break;
2853  case MIToken::kw_addrspace:
2854  if (parseAddrspace(Ptr.AddrSpace))
2855  return true;
2856  break;
2857  case MIToken::md_tbaa:
2858  lex();
2859  if (parseMDNode(AAInfo.TBAA))
2860  return true;
2861  break;
2863  lex();
2864  if (parseMDNode(AAInfo.Scope))
2865  return true;
2866  break;
2867  case MIToken::md_noalias:
2868  lex();
2869  if (parseMDNode(AAInfo.NoAlias))
2870  return true;
2871  break;
2872  case MIToken::md_range:
2873  lex();
2874  if (parseMDNode(Range))
2875  return true;
2876  break;
2877  // TODO: Report an error on duplicate metadata nodes.
2878  default:
2879  return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2880  "'!noalias' or '!range'");
2881  }
2882  }
2883  if (expectAndConsume(MIToken::rparen))
2884  return true;
2885  Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2886  SSID, Order, FailureOrder);
2887  return false;
2888 }
2889 
2890 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
2891  assert((Token.is(MIToken::kw_pre_instr_symbol) ||
2892  Token.is(MIToken::kw_post_instr_symbol)) &&
2893  "Invalid token for a pre- post-instruction symbol!");
2894  lex();
2895  if (Token.isNot(MIToken::MCSymbol))
2896  return error("expected a symbol after 'pre-instr-symbol'");
2897  Symbol = getOrCreateMCSymbol(Token.stringValue());
2898  lex();
2899  if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
2900  Token.is(MIToken::lbrace))
2901  return false;
2902  if (Token.isNot(MIToken::comma))
2903  return error("expected ',' before the next machine operand");
2904  lex();
2905  return false;
2906 }
2907 
2909  const Function &F,
2910  DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2911  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2912  MST.incorporateFunction(F);
2913  for (auto &BB : F) {
2914  if (BB.hasName())
2915  continue;
2916  int Slot = MST.getLocalSlot(&BB);
2917  if (Slot == -1)
2918  continue;
2919  Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2920  }
2921 }
2922 
2924  unsigned Slot,
2925  const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2926  auto BlockInfo = Slots2BasicBlocks.find(Slot);
2927  if (BlockInfo == Slots2BasicBlocks.end())
2928  return nullptr;
2929  return BlockInfo->second;
2930 }
2931 
2932 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2933  if (Slots2BasicBlocks.empty())
2934  initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
2935  return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2936 }
2937 
2938 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2939  if (&F == &MF.getFunction())
2940  return getIRBlock(Slot);
2941  DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2942  initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2943  return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2944 }
2945 
2946 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2947  DenseMap<unsigned, const Value *> &Slots2Values) {
2948  int Slot = MST.getLocalSlot(V);
2949  if (Slot == -1)
2950  return;
2951  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2952 }
2953 
2954 /// Creates the mapping from slot numbers to function's unnamed IR values.
2955 static void initSlots2Values(const Function &F,
2956  DenseMap<unsigned, const Value *> &Slots2Values) {
2957  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2958  MST.incorporateFunction(F);
2959  for (const auto &Arg : F.args())
2960  mapValueToSlot(&Arg, MST, Slots2Values);
2961  for (const auto &BB : F) {
2962  mapValueToSlot(&BB, MST, Slots2Values);
2963  for (const auto &I : BB)
2964  mapValueToSlot(&I, MST, Slots2Values);
2965  }
2966 }
2967 
2968 const Value *MIParser::getIRValue(unsigned Slot) {
2969  if (Slots2Values.empty())
2970  initSlots2Values(MF.getFunction(), Slots2Values);
2971  auto ValueInfo = Slots2Values.find(Slot);
2972  if (ValueInfo == Slots2Values.end())
2973  return nullptr;
2974  return ValueInfo->second;
2975 }
2976 
2977 MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
2978  // FIXME: Currently we can't recognize temporary or local symbols and call all
2979  // of the appropriate forms to create them. However, this handles basic cases
2980  // well as most of the special aspects are recognized by a prefix on their
2981  // name, and the input names should already be unique. For test cases, keeping
2982  // the symbol name out of the symbol table isn't terribly important.
2983  return MF.getContext().getOrCreateSymbol(Name);
2984 }
2985 
2986 bool MIParser::parseStringConstant(std::string &Result) {
2987  if (Token.isNot(MIToken::StringConstant))
2988  return error("expected string constant");
2989  Result = Token.stringValue();
2990  lex();
2991  return false;
2992 }
2993 
2995  StringRef Src,
2996  SMDiagnostic &Error) {
2997  return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2998 }
2999 
3001  StringRef Src, SMDiagnostic &Error) {
3002  return MIParser(PFS, Error, Src).parseBasicBlocks();
3003 }
3004 
3006  MachineBasicBlock *&MBB, StringRef Src,
3007  SMDiagnostic &Error) {
3008  return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3009 }
3010 
3012  unsigned &Reg, StringRef Src,
3013  SMDiagnostic &Error) {
3014  return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3015 }
3016 
3018  unsigned &Reg, StringRef Src,
3019  SMDiagnostic &Error) {
3020  return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3021 }
3022 
3024  VRegInfo *&Info, StringRef Src,
3025  SMDiagnostic &Error) {
3026  return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3027 }
3028 
3030  int &FI, StringRef Src,
3031  SMDiagnostic &Error) {
3032  return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3033 }
3034 
3036  MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
3037  return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3038 }
bool isFPPredicate() const
Definition: InstrTypes.h:801
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:2994
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock *> &Slots2BasicBlocks)
Definition: MIParser.cpp:2908
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
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:735
unsigned less than
Definition: InstrTypes.h:734
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:715
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:725
#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:3005
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
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:720
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:719
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:2923
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:716
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:3023
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...
union llvm::VRegInfo::@174 D
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:3029
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:538
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
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:54
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:3017
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:709
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:718
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:726
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:196
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:724
#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:736
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:3000
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:713
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:723
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:738
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:653
bool isIntPredicate() const
Definition: InstrTypes.h:802
signed less or equal
Definition: InstrTypes.h:739
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:3035
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:733
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:717
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:721
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:712
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:722
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
unsigned getAttributeEncoding(StringRef EncodingString)
Definition: Dwarf.cpp:196
IRTranslator LLVM IR MI
unsigned greater than
Definition: InstrTypes.h:732
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:3011
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:714
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value *> &Slots2Values)
Definition: MIParser.cpp:2946
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:2955
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:694
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:711
signed greater or equal
Definition: InstrTypes.h:737
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.