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