LLVM  8.0.0svn
MIParser.cpp
Go to the documentation of this file.
1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the parsing of machine instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MIParser.h"
15 #include "MILexer.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/Twine.h"
28 #include "llvm/AsmParser/Parser.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/InstrTypes.h"
49 #include "llvm/IR/Instructions.h"
50 #include "llvm/IR/Intrinsics.h"
51 #include "llvm/IR/Metadata.h"
52 #include "llvm/IR/Module.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/IR/Value.h"
57 #include "llvm/MC/LaneBitmask.h"
58 #include "llvm/MC/MCContext.h"
59 #include "llvm/MC/MCDwarf.h"
60 #include "llvm/MC/MCInstrDesc.h"
61 #include "llvm/MC/MCRegisterInfo.h"
64 #include "llvm/Support/Casting.h"
68 #include "llvm/Support/SMLoc.h"
69 #include "llvm/Support/SourceMgr.h"
73 #include <algorithm>
74 #include <cassert>
75 #include <cctype>
76 #include <cstddef>
77 #include <cstdint>
78 #include <limits>
79 #include <string>
80 #include <utility>
81 
82 using namespace llvm;
83 
85  SourceMgr &SM, const SlotMapping &IRSlots,
86  const Name2RegClassMap &Names2RegClasses,
87  const Name2RegBankMap &Names2RegBanks)
88  : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
89  Names2RegBanks(Names2RegBanks) {
90 }
91 
93  auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
94  if (I.second) {
96  VRegInfo *Info = new (Allocator) VRegInfo;
98  I.first->second = Info;
99  }
100  return *I.first->second;
101 }
102 
104  assert(RegName != "" && "Expected named reg.");
105 
106  auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
107  if (I.second) {
108  VRegInfo *Info = new (Allocator) VRegInfo;
110  I.first->second = Info;
111  }
112  return *I.first->second;
113 }
114 
115 namespace {
116 
117 /// A wrapper struct around the 'MachineOperand' struct that includes a source
118 /// range and other attributes.
119 struct ParsedMachineOperand {
120  MachineOperand Operand;
121  StringRef::iterator Begin;
123  Optional<unsigned> TiedDefIdx;
124 
125  ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
126  StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
127  : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
128  if (TiedDefIdx)
129  assert(Operand.isReg() && Operand.isUse() &&
130  "Only used register operands can be tied");
131  }
132 };
133 
134 class MIParser {
137  StringRef Source, CurrentSource;
138  MIToken Token;
140  /// Maps from instruction names to op codes.
141  StringMap<unsigned> Names2InstrOpCodes;
142  /// Maps from register names to registers.
143  StringMap<unsigned> Names2Regs;
144  /// Maps from register mask names to register masks.
145  StringMap<const uint32_t *> Names2RegMasks;
146  /// Maps from subregister names to subregister indices.
147  StringMap<unsigned> Names2SubRegIndices;
148  /// Maps from slot numbers to function's unnamed basic blocks.
149  DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
150  /// Maps from slot numbers to function's unnamed values.
152  /// Maps from target index names to target indices.
153  StringMap<int> Names2TargetIndices;
154  /// Maps from direct target flag names to the direct target flag values.
155  StringMap<unsigned> Names2DirectTargetFlags;
156  /// Maps from direct target flag names to the bitmask target flag values.
157  StringMap<unsigned> Names2BitmaskTargetFlags;
158  /// Maps from MMO target flag names to MMO target flag values.
159  StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
160 
161 public:
162  MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
163  StringRef Source);
164 
165  /// \p SkipChar gives the number of characters to skip before looking
166  /// for the next token.
167  void lex(unsigned SkipChar = 0);
168 
169  /// Report an error at the current location with the given message.
170  ///
171  /// This function always return true.
172  bool error(const Twine &Msg);
173 
174  /// Report an error at the given location with the given message.
175  ///
176  /// This function always return true.
177  bool error(StringRef::iterator Loc, const Twine &Msg);
178 
179  bool
180  parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
181  bool parseBasicBlocks();
182  bool parse(MachineInstr *&MI);
183  bool parseStandaloneMBB(MachineBasicBlock *&MBB);
184  bool parseStandaloneNamedRegister(unsigned &Reg);
185  bool parseStandaloneVirtualRegister(VRegInfo *&Info);
186  bool parseStandaloneRegister(unsigned &Reg);
187  bool parseStandaloneStackObject(int &FI);
188  bool parseStandaloneMDNode(MDNode *&Node);
189 
190  bool
191  parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
192  bool parseBasicBlock(MachineBasicBlock &MBB,
193  MachineBasicBlock *&AddFalthroughFrom);
194  bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
195  bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
196 
197  bool parseNamedRegister(unsigned &Reg);
198  bool parseVirtualRegister(VRegInfo *&Info);
199  bool parseNamedVirtualRegister(VRegInfo *&Info);
200  bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
201  bool parseRegisterFlag(unsigned &Flags);
202  bool parseRegisterClassOrBank(VRegInfo &RegInfo);
203  bool parseSubRegisterIndex(unsigned &SubReg);
204  bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
205  bool parseRegisterOperand(MachineOperand &Dest,
206  Optional<unsigned> &TiedDefIdx, bool IsDef = false);
207  bool parseImmediateOperand(MachineOperand &Dest);
208  bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
209  const Constant *&C);
210  bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
211  bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
212  bool parseTypedImmediateOperand(MachineOperand &Dest);
213  bool parseFPImmediateOperand(MachineOperand &Dest);
215  bool parseMBBOperand(MachineOperand &Dest);
216  bool parseStackFrameIndex(int &FI);
217  bool parseStackObjectOperand(MachineOperand &Dest);
218  bool parseFixedStackFrameIndex(int &FI);
219  bool parseFixedStackObjectOperand(MachineOperand &Dest);
220  bool parseGlobalValue(GlobalValue *&GV);
221  bool parseGlobalAddressOperand(MachineOperand &Dest);
222  bool parseConstantPoolIndexOperand(MachineOperand &Dest);
223  bool parseSubRegisterIndexOperand(MachineOperand &Dest);
224  bool parseJumpTableIndexOperand(MachineOperand &Dest);
225  bool parseExternalSymbolOperand(MachineOperand &Dest);
226  bool parseMCSymbolOperand(MachineOperand &Dest);
227  bool parseMDNode(MDNode *&Node);
228  bool parseDIExpression(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  Operands.push_back(
756  ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
757  if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
758  Token.is(MIToken::lbrace))
759  break;
760  if (Token.isNot(MIToken::comma))
761  return error("expected ',' before the next machine operand");
762  lex();
763  }
764 
765  MCSymbol *PreInstrSymbol = nullptr;
766  if (Token.is(MIToken::kw_pre_instr_symbol))
767  if (parsePreOrPostInstrSymbol(PreInstrSymbol))
768  return true;
769  MCSymbol *PostInstrSymbol = nullptr;
770  if (Token.is(MIToken::kw_post_instr_symbol))
771  if (parsePreOrPostInstrSymbol(PostInstrSymbol))
772  return true;
773 
774  DebugLoc DebugLocation;
775  if (Token.is(MIToken::kw_debug_location)) {
776  lex();
777  if (Token.isNot(MIToken::exclaim))
778  return error("expected a metadata node after 'debug-location'");
779  MDNode *Node = nullptr;
780  if (parseMDNode(Node))
781  return true;
782  DebugLocation = DebugLoc(Node);
783  }
784 
785  // Parse the machine memory operands.
787  if (Token.is(MIToken::coloncolon)) {
788  lex();
789  while (!Token.isNewlineOrEOF()) {
790  MachineMemOperand *MemOp = nullptr;
791  if (parseMachineMemoryOperand(MemOp))
792  return true;
793  MemOperands.push_back(MemOp);
794  if (Token.isNewlineOrEOF())
795  break;
796  if (Token.isNot(MIToken::comma))
797  return error("expected ',' before the next machine memory operand");
798  lex();
799  }
800  }
801 
802  const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
803  if (!MCID.isVariadic()) {
804  // FIXME: Move the implicit operand verification to the machine verifier.
805  if (verifyImplicitOperands(Operands, MCID))
806  return true;
807  }
808 
809  // TODO: Check for extraneous machine operands.
810  MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
811  MI->setFlags(Flags);
812  for (const auto &Operand : Operands)
813  MI->addOperand(MF, Operand.Operand);
814  if (assignRegisterTies(*MI, Operands))
815  return true;
816  if (PreInstrSymbol)
817  MI->setPreInstrSymbol(MF, PreInstrSymbol);
818  if (PostInstrSymbol)
819  MI->setPostInstrSymbol(MF, PostInstrSymbol);
820  if (!MemOperands.empty())
821  MI->setMemRefs(MF, MemOperands);
822  return false;
823 }
824 
825 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
826  lex();
827  if (Token.isNot(MIToken::MachineBasicBlock))
828  return error("expected a machine basic block reference");
829  if (parseMBBReference(MBB))
830  return true;
831  lex();
832  if (Token.isNot(MIToken::Eof))
833  return error(
834  "expected end of string after the machine basic block reference");
835  return false;
836 }
837 
838 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
839  lex();
840  if (Token.isNot(MIToken::NamedRegister))
841  return error("expected a named register");
842  if (parseNamedRegister(Reg))
843  return true;
844  lex();
845  if (Token.isNot(MIToken::Eof))
846  return error("expected end of string after the register reference");
847  return false;
848 }
849 
850 bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
851  lex();
852  if (Token.isNot(MIToken::VirtualRegister))
853  return error("expected a virtual register");
854  if (parseVirtualRegister(Info))
855  return true;
856  lex();
857  if (Token.isNot(MIToken::Eof))
858  return error("expected end of string after the register reference");
859  return false;
860 }
861 
862 bool MIParser::parseStandaloneRegister(unsigned &Reg) {
863  lex();
864  if (Token.isNot(MIToken::NamedRegister) &&
865  Token.isNot(MIToken::VirtualRegister))
866  return error("expected either a named or virtual register");
867 
868  VRegInfo *Info;
869  if (parseRegister(Reg, Info))
870  return true;
871 
872  lex();
873  if (Token.isNot(MIToken::Eof))
874  return error("expected end of string after the register reference");
875  return false;
876 }
877 
878 bool MIParser::parseStandaloneStackObject(int &FI) {
879  lex();
880  if (Token.isNot(MIToken::StackObject))
881  return error("expected a stack object");
882  if (parseStackFrameIndex(FI))
883  return true;
884  if (Token.isNot(MIToken::Eof))
885  return error("expected end of string after the stack object reference");
886  return false;
887 }
888 
889 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
890  lex();
891  if (Token.is(MIToken::exclaim)) {
892  if (parseMDNode(Node))
893  return true;
894  } else if (Token.is(MIToken::md_diexpr)) {
895  if (parseDIExpression(Node))
896  return true;
897  } else
898  return error("expected a metadata node");
899  if (Token.isNot(MIToken::Eof))
900  return error("expected end of string after the metadata node");
901  return false;
902 }
903 
904 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
905  assert(MO.isImplicit());
906  return MO.isDef() ? "implicit-def" : "implicit";
907 }
908 
909 static std::string getRegisterName(const TargetRegisterInfo *TRI,
910  unsigned Reg) {
911  assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
912  return StringRef(TRI->getName(Reg)).lower();
913 }
914 
915 /// Return true if the parsed machine operands contain a given machine operand.
916 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
918  for (const auto &I : Operands) {
919  if (ImplicitOperand.isIdenticalTo(I.Operand))
920  return true;
921  }
922  return false;
923 }
924 
925 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
926  const MCInstrDesc &MCID) {
927  if (MCID.isCall())
928  // We can't verify call instructions as they can contain arbitrary implicit
929  // register and register mask operands.
930  return false;
931 
932  // Gather all the expected implicit operands.
933  SmallVector<MachineOperand, 4> ImplicitOperands;
934  if (MCID.ImplicitDefs)
935  for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
936  ImplicitOperands.push_back(
937  MachineOperand::CreateReg(*ImpDefs, true, true));
938  if (MCID.ImplicitUses)
939  for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
940  ImplicitOperands.push_back(
941  MachineOperand::CreateReg(*ImpUses, false, true));
942 
943  const auto *TRI = MF.getSubtarget().getRegisterInfo();
944  assert(TRI && "Expected target register info");
945  for (const auto &I : ImplicitOperands) {
946  if (isImplicitOperandIn(I, Operands))
947  continue;
948  return error(Operands.empty() ? Token.location() : Operands.back().End,
949  Twine("missing implicit register operand '") +
950  printImplicitRegisterFlag(I) + " $" +
951  getRegisterName(TRI, I.getReg()) + "'");
952  }
953  return false;
954 }
955 
956 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
957  // Allow frame and fast math flags for OPCODE
958  while (Token.is(MIToken::kw_frame_setup) ||
959  Token.is(MIToken::kw_frame_destroy) ||
960  Token.is(MIToken::kw_nnan) ||
961  Token.is(MIToken::kw_ninf) ||
962  Token.is(MIToken::kw_nsz) ||
963  Token.is(MIToken::kw_arcp) ||
964  Token.is(MIToken::kw_contract) ||
965  Token.is(MIToken::kw_afn) ||
966  Token.is(MIToken::kw_reassoc) ||
967  Token.is(MIToken::kw_nuw) ||
968  Token.is(MIToken::kw_nsw) ||
969  Token.is(MIToken::kw_exact)) {
970  // Mine frame and fast math flags
971  if (Token.is(MIToken::kw_frame_setup))
972  Flags |= MachineInstr::FrameSetup;
973  if (Token.is(MIToken::kw_frame_destroy))
975  if (Token.is(MIToken::kw_nnan))
976  Flags |= MachineInstr::FmNoNans;
977  if (Token.is(MIToken::kw_ninf))
978  Flags |= MachineInstr::FmNoInfs;
979  if (Token.is(MIToken::kw_nsz))
980  Flags |= MachineInstr::FmNsz;
981  if (Token.is(MIToken::kw_arcp))
982  Flags |= MachineInstr::FmArcp;
983  if (Token.is(MIToken::kw_contract))
984  Flags |= MachineInstr::FmContract;
985  if (Token.is(MIToken::kw_afn))
986  Flags |= MachineInstr::FmAfn;
987  if (Token.is(MIToken::kw_reassoc))
988  Flags |= MachineInstr::FmReassoc;
989  if (Token.is(MIToken::kw_nuw))
990  Flags |= MachineInstr::NoUWrap;
991  if (Token.is(MIToken::kw_nsw))
992  Flags |= MachineInstr::NoSWrap;
993  if (Token.is(MIToken::kw_exact))
994  Flags |= MachineInstr::IsExact;
995 
996  lex();
997  }
998  if (Token.isNot(MIToken::Identifier))
999  return error("expected a machine instruction");
1000  StringRef InstrName = Token.stringValue();
1001  if (parseInstrName(InstrName, OpCode))
1002  return error(Twine("unknown machine instruction name '") + InstrName + "'");
1003  lex();
1004  return false;
1005 }
1006 
1007 bool MIParser::parseNamedRegister(unsigned &Reg) {
1008  assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1009  StringRef Name = Token.stringValue();
1010  if (getRegisterByName(Name, Reg))
1011  return error(Twine("unknown register name '") + Name + "'");
1012  return false;
1013 }
1014 
1015 bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1016  assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1017  StringRef Name = Token.stringValue();
1018  // TODO: Check that the VReg name is not the same as a physical register name.
1019  // If it is, then print a warning (when warnings are implemented).
1020  Info = &PFS.getVRegInfoNamed(Name);
1021  return false;
1022 }
1023 
1024 bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1025  if (Token.is(MIToken::NamedVirtualRegister))
1026  return parseNamedVirtualRegister(Info);
1027  assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1028  unsigned ID;
1029  if (getUnsigned(ID))
1030  return true;
1031  Info = &PFS.getVRegInfo(ID);
1032  return false;
1033 }
1034 
1035 bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
1036  switch (Token.kind()) {
1037  case MIToken::underscore:
1038  Reg = 0;
1039  return false;
1041  return parseNamedRegister(Reg);
1044  if (parseVirtualRegister(Info))
1045  return true;
1046  Reg = Info->VReg;
1047  return false;
1048  // TODO: Parse other register kinds.
1049  default:
1050  llvm_unreachable("The current token should be a register");
1051  }
1052 }
1053 
1054 bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1055  if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1056  return error("expected '_', register class, or register bank name");
1057  StringRef::iterator Loc = Token.location();
1058  StringRef Name = Token.stringValue();
1059 
1060  // Was it a register class?
1061  auto RCNameI = PFS.Names2RegClasses.find(Name);
1062  if (RCNameI != PFS.Names2RegClasses.end()) {
1063  lex();
1064  const TargetRegisterClass &RC = *RCNameI->getValue();
1065 
1066  switch (RegInfo.Kind) {
1067  case VRegInfo::UNKNOWN:
1068  case VRegInfo::NORMAL:
1069  RegInfo.Kind = VRegInfo::NORMAL;
1070  if (RegInfo.Explicit && RegInfo.D.RC != &RC) {
1072  return error(Loc, Twine("conflicting register classes, previously: ") +
1073  Twine(TRI.getRegClassName(RegInfo.D.RC)));
1074  }
1075  RegInfo.D.RC = &RC;
1076  RegInfo.Explicit = true;
1077  return false;
1078 
1079  case VRegInfo::GENERIC:
1080  case VRegInfo::REGBANK:
1081  return error(Loc, "register class specification on generic register");
1082  }
1083  llvm_unreachable("Unexpected register kind");
1084  }
1085 
1086  // Should be a register bank or a generic register.
1087  const RegisterBank *RegBank = nullptr;
1088  if (Name != "_") {
1089  auto RBNameI = PFS.Names2RegBanks.find(Name);
1090  if (RBNameI == PFS.Names2RegBanks.end())
1091  return error(Loc, "expected '_', register class, or register bank name");
1092  RegBank = RBNameI->getValue();
1093  }
1094 
1095  lex();
1096 
1097  switch (RegInfo.Kind) {
1098  case VRegInfo::UNKNOWN:
1099  case VRegInfo::GENERIC:
1100  case VRegInfo::REGBANK:
1101  RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1102  if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1103  return error(Loc, "conflicting generic register banks");
1104  RegInfo.D.RegBank = RegBank;
1105  RegInfo.Explicit = true;
1106  return false;
1107 
1108  case VRegInfo::NORMAL:
1109  return error(Loc, "register bank specification on normal register");
1110  }
1111  llvm_unreachable("Unexpected register kind");
1112 }
1113 
1114 bool MIParser::parseRegisterFlag(unsigned &Flags) {
1115  const unsigned OldFlags = Flags;
1116  switch (Token.kind()) {
1117  case MIToken::kw_implicit:
1118  Flags |= RegState::Implicit;
1119  break;
1121  Flags |= RegState::ImplicitDefine;
1122  break;
1123  case MIToken::kw_def:
1124  Flags |= RegState::Define;
1125  break;
1126  case MIToken::kw_dead:
1127  Flags |= RegState::Dead;
1128  break;
1129  case MIToken::kw_killed:
1130  Flags |= RegState::Kill;
1131  break;
1132  case MIToken::kw_undef:
1133  Flags |= RegState::Undef;
1134  break;
1135  case MIToken::kw_internal:
1136  Flags |= RegState::InternalRead;
1137  break;
1139  Flags |= RegState::EarlyClobber;
1140  break;
1141  case MIToken::kw_debug_use:
1142  Flags |= RegState::Debug;
1143  break;
1144  case MIToken::kw_renamable:
1145  Flags |= RegState::Renamable;
1146  break;
1147  default:
1148  llvm_unreachable("The current token should be a register flag");
1149  }
1150  if (OldFlags == Flags)
1151  // We know that the same flag is specified more than once when the flags
1152  // weren't modified.
1153  return error("duplicate '" + Token.stringValue() + "' register flag");
1154  lex();
1155  return false;
1156 }
1157 
1158 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1159  assert(Token.is(MIToken::dot));
1160  lex();
1161  if (Token.isNot(MIToken::Identifier))
1162  return error("expected a subregister index after '.'");
1163  auto Name = Token.stringValue();
1164  SubReg = getSubRegIndex(Name);
1165  if (!SubReg)
1166  return error(Twine("use of unknown subregister index '") + Name + "'");
1167  lex();
1168  return false;
1169 }
1170 
1171 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1172  if (!consumeIfPresent(MIToken::kw_tied_def))
1173  return true;
1174  if (Token.isNot(MIToken::IntegerLiteral))
1175  return error("expected an integer literal after 'tied-def'");
1176  if (getUnsigned(TiedDefIdx))
1177  return true;
1178  lex();
1179  if (expectAndConsume(MIToken::rparen))
1180  return true;
1181  return false;
1182 }
1183 
1184 bool MIParser::assignRegisterTies(MachineInstr &MI,
1185  ArrayRef<ParsedMachineOperand> Operands) {
1186  SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1187  for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1188  if (!Operands[I].TiedDefIdx)
1189  continue;
1190  // The parser ensures that this operand is a register use, so we just have
1191  // to check the tied-def operand.
1192  unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1193  if (DefIdx >= E)
1194  return error(Operands[I].Begin,
1195  Twine("use of invalid tied-def operand index '" +
1196  Twine(DefIdx) + "'; instruction has only ") +
1197  Twine(E) + " operands");
1198  const auto &DefOperand = Operands[DefIdx].Operand;
1199  if (!DefOperand.isReg() || !DefOperand.isDef())
1200  // FIXME: add note with the def operand.
1201  return error(Operands[I].Begin,
1202  Twine("use of invalid tied-def operand index '") +
1203  Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1204  " isn't a defined register");
1205  // Check that the tied-def operand wasn't tied elsewhere.
1206  for (const auto &TiedPair : TiedRegisterPairs) {
1207  if (TiedPair.first == DefIdx)
1208  return error(Operands[I].Begin,
1209  Twine("the tied-def operand #") + Twine(DefIdx) +
1210  " is already tied with another register operand");
1211  }
1212  TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1213  }
1214  // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1215  // indices must be less than tied max.
1216  for (const auto &TiedPair : TiedRegisterPairs)
1217  MI.tieOperands(TiedPair.first, TiedPair.second);
1218  return false;
1219 }
1220 
1221 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1222  Optional<unsigned> &TiedDefIdx,
1223  bool IsDef) {
1224  unsigned Flags = IsDef ? RegState::Define : 0;
1225  while (Token.isRegisterFlag()) {
1226  if (parseRegisterFlag(Flags))
1227  return true;
1228  }
1229  if (!Token.isRegister())
1230  return error("expected a register after register flags");
1231  unsigned Reg;
1232  VRegInfo *RegInfo;
1233  if (parseRegister(Reg, RegInfo))
1234  return true;
1235  lex();
1236  unsigned SubReg = 0;
1237  if (Token.is(MIToken::dot)) {
1238  if (parseSubRegisterIndex(SubReg))
1239  return true;
1241  return error("subregister index expects a virtual register");
1242  }
1243  if (Token.is(MIToken::colon)) {
1245  return error("register class specification expects a virtual register");
1246  lex();
1247  if (parseRegisterClassOrBank(*RegInfo))
1248  return true;
1249  }
1251  if ((Flags & RegState::Define) == 0) {
1252  if (consumeIfPresent(MIToken::lparen)) {
1253  unsigned Idx;
1254  if (!parseRegisterTiedDefIndex(Idx))
1255  TiedDefIdx = Idx;
1256  else {
1257  // Try a redundant low-level type.
1258  LLT Ty;
1259  if (parseLowLevelType(Token.location(), Ty))
1260  return error("expected tied-def or low-level type after '('");
1261 
1262  if (expectAndConsume(MIToken::rparen))
1263  return true;
1264 
1265  if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1266  return error("inconsistent type for generic virtual register");
1267 
1268  MRI.setType(Reg, Ty);
1269  }
1270  }
1271  } else if (consumeIfPresent(MIToken::lparen)) {
1272  // Virtual registers may have a tpe with GlobalISel.
1274  return error("unexpected type on physical register");
1275 
1276  LLT Ty;
1277  if (parseLowLevelType(Token.location(), Ty))
1278  return true;
1279 
1280  if (expectAndConsume(MIToken::rparen))
1281  return true;
1282 
1283  if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1284  return error("inconsistent type for generic virtual register");
1285 
1286  MRI.setType(Reg, Ty);
1287  } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1288  // Generic virtual registers must have a type.
1289  // If we end up here this means the type hasn't been specified and
1290  // this is bad!
1291  if (RegInfo->Kind == VRegInfo::GENERIC ||
1292  RegInfo->Kind == VRegInfo::REGBANK)
1293  return error("generic virtual registers must have a type");
1294  }
1296  Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1297  Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1298  Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1299  Flags & RegState::InternalRead, Flags & RegState::Renamable);
1300 
1301  return false;
1302 }
1303 
1304 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1305  assert(Token.is(MIToken::IntegerLiteral));
1306  const APSInt &Int = Token.integerValue();
1307  if (Int.getMinSignedBits() > 64)
1308  return error("integer literal is too large to be an immediate operand");
1309  Dest = MachineOperand::CreateImm(Int.getExtValue());
1310  lex();
1311  return false;
1312 }
1313 
1314 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1315  const Constant *&C) {
1316  auto Source = StringValue.str(); // The source has to be null terminated.
1317  SMDiagnostic Err;
1318  C = parseConstantValue(Source, Err, *MF.getFunction().getParent(),
1319  &PFS.IRSlots);
1320  if (!C)
1321  return error(Loc + Err.getColumnNo(), Err.getMessage());
1322  return false;
1323 }
1324 
1325 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1326  if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1327  return true;
1328  lex();
1329  return false;
1330 }
1331 
1332 bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1333  if (Token.range().front() == 's' || Token.range().front() == 'p') {
1334  StringRef SizeStr = Token.range().drop_front();
1335  if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1336  return error("expected integers after 's'/'p' type character");
1337  }
1338 
1339  if (Token.range().front() == 's') {
1340  Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1341  lex();
1342  return false;
1343  } else if (Token.range().front() == 'p') {
1344  const DataLayout &DL = MF.getDataLayout();
1345  unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1346  Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1347  lex();
1348  return false;
1349  }
1350 
1351  // Now we're looking for a vector.
1352  if (Token.isNot(MIToken::less))
1353  return error(Loc,
1354  "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1355  lex();
1356 
1357  if (Token.isNot(MIToken::IntegerLiteral))
1358  return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1359  uint64_t NumElements = Token.integerValue().getZExtValue();
1360  lex();
1361 
1362  if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1363  return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1364  lex();
1365 
1366  if (Token.range().front() != 's' && Token.range().front() != 'p')
1367  return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1368  StringRef SizeStr = Token.range().drop_front();
1369  if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1370  return error("expected integers after 's'/'p' type character");
1371 
1372  if (Token.range().front() == 's')
1373  Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1374  else if (Token.range().front() == 'p') {
1375  const DataLayout &DL = MF.getDataLayout();
1376  unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1377  Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1378  } else
1379  return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1380  lex();
1381 
1382  if (Token.isNot(MIToken::greater))
1383  return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1384  lex();
1385 
1386  Ty = LLT::vector(NumElements, Ty);
1387  return false;
1388 }
1389 
1390 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1391  assert(Token.is(MIToken::Identifier));
1392  StringRef TypeStr = Token.range();
1393  if (TypeStr.front() != 'i' && TypeStr.front() != 's' &&
1394  TypeStr.front() != 'p')
1395  return error(
1396  "a typed immediate operand should start with one of 'i', 's', or 'p'");
1397  StringRef SizeStr = Token.range().drop_front();
1398  if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1399  return error("expected integers after 'i'/'s'/'p' type character");
1400 
1401  auto Loc = Token.location();
1402  lex();
1403  if (Token.isNot(MIToken::IntegerLiteral)) {
1404  if (Token.isNot(MIToken::Identifier) ||
1405  !(Token.range() == "true" || Token.range() == "false"))
1406  return error("expected an integer literal");
1407  }
1408  const Constant *C = nullptr;
1409  if (parseIRConstant(Loc, C))
1410  return true;
1411  Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1412  return false;
1413 }
1414 
1415 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1416  auto Loc = Token.location();
1417  lex();
1418  if (Token.isNot(MIToken::FloatingPointLiteral) &&
1419  Token.isNot(MIToken::HexLiteral))
1420  return error("expected a floating point literal");
1421  const Constant *C = nullptr;
1422  if (parseIRConstant(Loc, C))
1423  return true;
1424  Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1425  return false;
1426 }
1427 
1428 bool MIParser::getUnsigned(unsigned &Result) {
1429  if (Token.hasIntegerValue()) {
1430  const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1431  uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1432  if (Val64 == Limit)
1433  return error("expected 32-bit integer (too large)");
1434  Result = Val64;
1435  return false;
1436  }
1437  if (Token.is(MIToken::HexLiteral)) {
1438  APInt A;
1439  if (getHexUint(A))
1440  return true;
1441  if (A.getBitWidth() > 32)
1442  return error("expected 32-bit integer (too large)");
1443  Result = A.getZExtValue();
1444  return false;
1445  }
1446  return true;
1447 }
1448 
1450  assert(Token.is(MIToken::MachineBasicBlock) ||
1452  unsigned Number;
1453  if (getUnsigned(Number))
1454  return true;
1455  auto MBBInfo = PFS.MBBSlots.find(Number);
1456  if (MBBInfo == PFS.MBBSlots.end())
1457  return error(Twine("use of undefined machine basic block #") +
1458  Twine(Number));
1459  MBB = MBBInfo->second;
1460  // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1461  // we drop the <irname> from the bb.<id>.<irname> format.
1462  if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1463  return error(Twine("the name of machine basic block #") + Twine(Number) +
1464  " isn't '" + Token.stringValue() + "'");
1465  return false;
1466 }
1467 
1468 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1469  MachineBasicBlock *MBB;
1470  if (parseMBBReference(MBB))
1471  return true;
1472  Dest = MachineOperand::CreateMBB(MBB);
1473  lex();
1474  return false;
1475 }
1476 
1477 bool MIParser::parseStackFrameIndex(int &FI) {
1478  assert(Token.is(MIToken::StackObject));
1479  unsigned ID;
1480  if (getUnsigned(ID))
1481  return true;
1482  auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1483  if (ObjectInfo == PFS.StackObjectSlots.end())
1484  return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1485  "'");
1486  StringRef Name;
1487  if (const auto *Alloca =
1488  MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
1489  Name = Alloca->getName();
1490  if (!Token.stringValue().empty() && Token.stringValue() != Name)
1491  return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1492  "' isn't '" + Token.stringValue() + "'");
1493  lex();
1494  FI = ObjectInfo->second;
1495  return false;
1496 }
1497 
1498 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1499  int FI;
1500  if (parseStackFrameIndex(FI))
1501  return true;
1502  Dest = MachineOperand::CreateFI(FI);
1503  return false;
1504 }
1505 
1506 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1507  assert(Token.is(MIToken::FixedStackObject));
1508  unsigned ID;
1509  if (getUnsigned(ID))
1510  return true;
1511  auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1512  if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1513  return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1514  Twine(ID) + "'");
1515  lex();
1516  FI = ObjectInfo->second;
1517  return false;
1518 }
1519 
1520 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1521  int FI;
1522  if (parseFixedStackFrameIndex(FI))
1523  return true;
1524  Dest = MachineOperand::CreateFI(FI);
1525  return false;
1526 }
1527 
1528 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1529  switch (Token.kind()) {
1531  const Module *M = MF.getFunction().getParent();
1532  GV = M->getNamedValue(Token.stringValue());
1533  if (!GV)
1534  return error(Twine("use of undefined global value '") + Token.range() +
1535  "'");
1536  break;
1537  }
1538  case MIToken::GlobalValue: {
1539  unsigned GVIdx;
1540  if (getUnsigned(GVIdx))
1541  return true;
1542  if (GVIdx >= PFS.IRSlots.GlobalValues.size())
1543  return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1544  "'");
1545  GV = PFS.IRSlots.GlobalValues[GVIdx];
1546  break;
1547  }
1548  default:
1549  llvm_unreachable("The current token should be a global value");
1550  }
1551  return false;
1552 }
1553 
1554 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1555  GlobalValue *GV = nullptr;
1556  if (parseGlobalValue(GV))
1557  return true;
1558  lex();
1559  Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1560  if (parseOperandsOffset(Dest))
1561  return true;
1562  return false;
1563 }
1564 
1565 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1566  assert(Token.is(MIToken::ConstantPoolItem));
1567  unsigned ID;
1568  if (getUnsigned(ID))
1569  return true;
1570  auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1571  if (ConstantInfo == PFS.ConstantPoolSlots.end())
1572  return error("use of undefined constant '%const." + Twine(ID) + "'");
1573  lex();
1574  Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1575  if (parseOperandsOffset(Dest))
1576  return true;
1577  return false;
1578 }
1579 
1580 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1581  assert(Token.is(MIToken::JumpTableIndex));
1582  unsigned ID;
1583  if (getUnsigned(ID))
1584  return true;
1585  auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1586  if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1587  return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1588  lex();
1589  Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1590  return false;
1591 }
1592 
1593 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1594  assert(Token.is(MIToken::ExternalSymbol));
1595  const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1596  lex();
1597  Dest = MachineOperand::CreateES(Symbol);
1598  if (parseOperandsOffset(Dest))
1599  return true;
1600  return false;
1601 }
1602 
1603 bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
1604  assert(Token.is(MIToken::MCSymbol));
1605  MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
1606  lex();
1607  Dest = MachineOperand::CreateMCSymbol(Symbol);
1608  if (parseOperandsOffset(Dest))
1609  return true;
1610  return false;
1611 }
1612 
1613 bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1614  assert(Token.is(MIToken::SubRegisterIndex));
1615  StringRef Name = Token.stringValue();
1616  unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1617  if (SubRegIndex == 0)
1618  return error(Twine("unknown subregister index '") + Name + "'");
1619  lex();
1620  Dest = MachineOperand::CreateImm(SubRegIndex);
1621  return false;
1622 }
1623 
1624 bool MIParser::parseMDNode(MDNode *&Node) {
1625  assert(Token.is(MIToken::exclaim));
1626 
1627  auto Loc = Token.location();
1628  lex();
1629  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1630  return error("expected metadata id after '!'");
1631  unsigned ID;
1632  if (getUnsigned(ID))
1633  return true;
1634  auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1635  if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
1636  return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1637  lex();
1638  Node = NodeInfo->second.get();
1639  return false;
1640 }
1641 
1642 bool MIParser::parseDIExpression(MDNode *&Expr) {
1643  assert(Token.is(MIToken::md_diexpr));
1644  lex();
1645 
1646  // FIXME: Share this parsing with the IL parser.
1647  SmallVector<uint64_t, 8> Elements;
1648 
1649  if (expectAndConsume(MIToken::lparen))
1650  return true;
1651 
1652  if (Token.isNot(MIToken::rparen)) {
1653  do {
1654  if (Token.is(MIToken::Identifier)) {
1655  if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
1656  lex();
1657  Elements.push_back(Op);
1658  continue;
1659  }
1660  return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1661  }
1662 
1663  if (Token.isNot(MIToken::IntegerLiteral) ||
1664  Token.integerValue().isSigned())
1665  return error("expected unsigned integer");
1666 
1667  auto &U = Token.integerValue();
1668  if (U.ugt(UINT64_MAX))
1669  return error("element too large, limit is " + Twine(UINT64_MAX));
1670  Elements.push_back(U.getZExtValue());
1671  lex();
1672 
1673  } while (consumeIfPresent(MIToken::comma));
1674  }
1675 
1676  if (expectAndConsume(MIToken::rparen))
1677  return true;
1678 
1679  Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
1680  return false;
1681 }
1682 
1683 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1684  MDNode *Node = nullptr;
1685  if (Token.is(MIToken::exclaim)) {
1686  if (parseMDNode(Node))
1687  return true;
1688  } else if (Token.is(MIToken::md_diexpr)) {
1689  if (parseDIExpression(Node))
1690  return true;
1691  }
1692  Dest = MachineOperand::CreateMetadata(Node);
1693  return false;
1694 }
1695 
1696 bool MIParser::parseCFIOffset(int &Offset) {
1697  if (Token.isNot(MIToken::IntegerLiteral))
1698  return error("expected a cfi offset");
1699  if (Token.integerValue().getMinSignedBits() > 32)
1700  return error("expected a 32 bit integer (the cfi offset is too large)");
1701  Offset = (int)Token.integerValue().getExtValue();
1702  lex();
1703  return false;
1704 }
1705 
1706 bool MIParser::parseCFIRegister(unsigned &Reg) {
1707  if (Token.isNot(MIToken::NamedRegister))
1708  return error("expected a cfi register");
1709  unsigned LLVMReg;
1710  if (parseNamedRegister(LLVMReg))
1711  return true;
1712  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1713  assert(TRI && "Expected target register info");
1714  int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1715  if (DwarfReg < 0)
1716  return error("invalid DWARF register");
1717  Reg = (unsigned)DwarfReg;
1718  lex();
1719  return false;
1720 }
1721 
1722 bool MIParser::parseCFIEscapeValues(std::string &Values) {
1723  do {
1724  if (Token.isNot(MIToken::HexLiteral))
1725  return error("expected a hexadecimal literal");
1726  unsigned Value;
1727  if (getUnsigned(Value))
1728  return true;
1729  if (Value > UINT8_MAX)
1730  return error("expected a 8-bit integer (too large)");
1731  Values.push_back(static_cast<uint8_t>(Value));
1732  lex();
1733  } while (consumeIfPresent(MIToken::comma));
1734  return false;
1735 }
1736 
1737 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1738  auto Kind = Token.kind();
1739  lex();
1740  int Offset;
1741  unsigned Reg;
1742  unsigned CFIIndex;
1743  switch (Kind) {
1745  if (parseCFIRegister(Reg))
1746  return true;
1747  CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1748  break;
1750  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1751  parseCFIOffset(Offset))
1752  return true;
1753  CFIIndex =
1754  MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1755  break;
1757  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1758  parseCFIOffset(Offset))
1759  return true;
1760  CFIIndex = MF.addFrameInst(
1761  MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
1762  break;
1764  if (parseCFIRegister(Reg))
1765  return true;
1766  CFIIndex =
1768  break;
1770  if (parseCFIOffset(Offset))
1771  return true;
1772  // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1773  CFIIndex = MF.addFrameInst(
1774  MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1775  break;
1777  if (parseCFIOffset(Offset))
1778  return true;
1779  CFIIndex = MF.addFrameInst(
1780  MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
1781  break;
1783  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1784  parseCFIOffset(Offset))
1785  return true;
1786  // NB: MCCFIInstruction::createDefCfa negates the offset.
1787  CFIIndex =
1788  MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1789  break;
1792  break;
1794  if (parseCFIRegister(Reg))
1795  return true;
1796  CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
1797  break;
1800  break;
1802  if (parseCFIRegister(Reg))
1803  return true;
1804  CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
1805  break;
1806  case MIToken::kw_cfi_register: {
1807  unsigned Reg2;
1808  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1809  parseCFIRegister(Reg2))
1810  return true;
1811 
1812  CFIIndex =
1813  MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
1814  break;
1815  }
1817  CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
1818  break;
1819  case MIToken::kw_cfi_escape: {
1820  std::string Values;
1821  if (parseCFIEscapeValues(Values))
1822  return true;
1823  CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
1824  break;
1825  }
1826  default:
1827  // TODO: Parse the other CFI operands.
1828  llvm_unreachable("The current token should be a cfi operand");
1829  }
1830  Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1831  return false;
1832 }
1833 
1834 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1835  switch (Token.kind()) {
1836  case MIToken::NamedIRBlock: {
1837  BB = dyn_cast_or_null<BasicBlock>(
1838  F.getValueSymbolTable()->lookup(Token.stringValue()));
1839  if (!BB)
1840  return error(Twine("use of undefined IR block '") + Token.range() + "'");
1841  break;
1842  }
1843  case MIToken::IRBlock: {
1844  unsigned SlotNumber = 0;
1845  if (getUnsigned(SlotNumber))
1846  return true;
1847  BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1848  if (!BB)
1849  return error(Twine("use of undefined IR block '%ir-block.") +
1850  Twine(SlotNumber) + "'");
1851  break;
1852  }
1853  default:
1854  llvm_unreachable("The current token should be an IR block reference");
1855  }
1856  return false;
1857 }
1858 
1859 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1860  assert(Token.is(MIToken::kw_blockaddress));
1861  lex();
1862  if (expectAndConsume(MIToken::lparen))
1863  return true;
1864  if (Token.isNot(MIToken::GlobalValue) &&
1865  Token.isNot(MIToken::NamedGlobalValue))
1866  return error("expected a global value");
1867  GlobalValue *GV = nullptr;
1868  if (parseGlobalValue(GV))
1869  return true;
1870  auto *F = dyn_cast<Function>(GV);
1871  if (!F)
1872  return error("expected an IR function reference");
1873  lex();
1874  if (expectAndConsume(MIToken::comma))
1875  return true;
1876  BasicBlock *BB = nullptr;
1877  if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1878  return error("expected an IR block reference");
1879  if (parseIRBlock(BB, *F))
1880  return true;
1881  lex();
1882  if (expectAndConsume(MIToken::rparen))
1883  return true;
1884  Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1885  if (parseOperandsOffset(Dest))
1886  return true;
1887  return false;
1888 }
1889 
1890 bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1891  assert(Token.is(MIToken::kw_intrinsic));
1892  lex();
1893  if (expectAndConsume(MIToken::lparen))
1894  return error("expected syntax intrinsic(@llvm.whatever)");
1895 
1896  if (Token.isNot(MIToken::NamedGlobalValue))
1897  return error("expected syntax intrinsic(@llvm.whatever)");
1898 
1899  std::string Name = Token.stringValue();
1900  lex();
1901 
1902  if (expectAndConsume(MIToken::rparen))
1903  return error("expected ')' to terminate intrinsic name");
1904 
1905  // Find out what intrinsic we're dealing with, first try the global namespace
1906  // and then the target's private intrinsics if that fails.
1909  if (ID == Intrinsic::not_intrinsic && TII)
1910  ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1911 
1912  if (ID == Intrinsic::not_intrinsic)
1913  return error("unknown intrinsic name");
1915 
1916  return false;
1917 }
1918 
1919 bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1920  assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1921  bool IsFloat = Token.is(MIToken::kw_floatpred);
1922  lex();
1923 
1924  if (expectAndConsume(MIToken::lparen))
1925  return error("expected syntax intpred(whatever) or floatpred(whatever");
1926 
1927  if (Token.isNot(MIToken::Identifier))
1928  return error("whatever");
1929 
1930  CmpInst::Predicate Pred;
1931  if (IsFloat) {
1932  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1933  .Case("false", CmpInst::FCMP_FALSE)
1934  .Case("oeq", CmpInst::FCMP_OEQ)
1935  .Case("ogt", CmpInst::FCMP_OGT)
1936  .Case("oge", CmpInst::FCMP_OGE)
1937  .Case("olt", CmpInst::FCMP_OLT)
1938  .Case("ole", CmpInst::FCMP_OLE)
1939  .Case("one", CmpInst::FCMP_ONE)
1940  .Case("ord", CmpInst::FCMP_ORD)
1941  .Case("uno", CmpInst::FCMP_UNO)
1942  .Case("ueq", CmpInst::FCMP_UEQ)
1943  .Case("ugt", CmpInst::FCMP_UGT)
1944  .Case("uge", CmpInst::FCMP_UGE)
1945  .Case("ult", CmpInst::FCMP_ULT)
1946  .Case("ule", CmpInst::FCMP_ULE)
1947  .Case("une", CmpInst::FCMP_UNE)
1948  .Case("true", CmpInst::FCMP_TRUE)
1950  if (!CmpInst::isFPPredicate(Pred))
1951  return error("invalid floating-point predicate");
1952  } else {
1953  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1954  .Case("eq", CmpInst::ICMP_EQ)
1955  .Case("ne", CmpInst::ICMP_NE)
1956  .Case("sgt", CmpInst::ICMP_SGT)
1957  .Case("sge", CmpInst::ICMP_SGE)
1958  .Case("slt", CmpInst::ICMP_SLT)
1959  .Case("sle", CmpInst::ICMP_SLE)
1960  .Case("ugt", CmpInst::ICMP_UGT)
1961  .Case("uge", CmpInst::ICMP_UGE)
1962  .Case("ult", CmpInst::ICMP_ULT)
1963  .Case("ule", CmpInst::ICMP_ULE)
1965  if (!CmpInst::isIntPredicate(Pred))
1966  return error("invalid integer predicate");
1967  }
1968 
1969  lex();
1970  Dest = MachineOperand::CreatePredicate(Pred);
1971  if (expectAndConsume(MIToken::rparen))
1972  return error("predicate should be terminated by ')'.");
1973 
1974  return false;
1975 }
1976 
1977 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1978  assert(Token.is(MIToken::kw_target_index));
1979  lex();
1980  if (expectAndConsume(MIToken::lparen))
1981  return true;
1982  if (Token.isNot(MIToken::Identifier))
1983  return error("expected the name of the target index");
1984  int Index = 0;
1985  if (getTargetIndex(Token.stringValue(), Index))
1986  return error("use of undefined target index '" + Token.stringValue() + "'");
1987  lex();
1988  if (expectAndConsume(MIToken::rparen))
1989  return true;
1990  Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1991  if (parseOperandsOffset(Dest))
1992  return true;
1993  return false;
1994 }
1995 
1996 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1997  assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
1998  lex();
1999  if (expectAndConsume(MIToken::lparen))
2000  return true;
2001 
2003  while (true) {
2004  if (Token.isNot(MIToken::NamedRegister))
2005  return error("expected a named register");
2006  unsigned Reg;
2007  if (parseNamedRegister(Reg))
2008  return true;
2009  lex();
2010  Mask[Reg / 32] |= 1U << (Reg % 32);
2011  // TODO: Report an error if the same register is used more than once.
2012  if (Token.isNot(MIToken::comma))
2013  break;
2014  lex();
2015  }
2016 
2017  if (expectAndConsume(MIToken::rparen))
2018  return true;
2019  Dest = MachineOperand::CreateRegMask(Mask);
2020  return false;
2021 }
2022 
2023 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2024  assert(Token.is(MIToken::kw_liveout));
2026  lex();
2027  if (expectAndConsume(MIToken::lparen))
2028  return true;
2029  while (true) {
2030  if (Token.isNot(MIToken::NamedRegister))
2031  return error("expected a named register");
2032  unsigned Reg;
2033  if (parseNamedRegister(Reg))
2034  return true;
2035  lex();
2036  Mask[Reg / 32] |= 1U << (Reg % 32);
2037  // TODO: Report an error if the same register is used more than once.
2038  if (Token.isNot(MIToken::comma))
2039  break;
2040  lex();
2041  }
2042  if (expectAndConsume(MIToken::rparen))
2043  return true;
2044  Dest = MachineOperand::CreateRegLiveOut(Mask);
2045  return false;
2046 }
2047 
2048 bool MIParser::parseMachineOperand(MachineOperand &Dest,
2049  Optional<unsigned> &TiedDefIdx) {
2050  switch (Token.kind()) {
2051  case MIToken::kw_implicit:
2053  case MIToken::kw_def:
2054  case MIToken::kw_dead:
2055  case MIToken::kw_killed:
2056  case MIToken::kw_undef:
2057  case MIToken::kw_internal:
2059  case MIToken::kw_debug_use:
2060  case MIToken::kw_renamable:
2061  case MIToken::underscore:
2065  return parseRegisterOperand(Dest, TiedDefIdx);
2067  return parseImmediateOperand(Dest);
2068  case MIToken::kw_half:
2069  case MIToken::kw_float:
2070  case MIToken::kw_double:
2071  case MIToken::kw_x86_fp80:
2072  case MIToken::kw_fp128:
2073  case MIToken::kw_ppc_fp128:
2074  return parseFPImmediateOperand(Dest);
2076  return parseMBBOperand(Dest);
2077  case MIToken::StackObject:
2078  return parseStackObjectOperand(Dest);
2080  return parseFixedStackObjectOperand(Dest);
2081  case MIToken::GlobalValue:
2083  return parseGlobalAddressOperand(Dest);
2085  return parseConstantPoolIndexOperand(Dest);
2087  return parseJumpTableIndexOperand(Dest);
2089  return parseExternalSymbolOperand(Dest);
2090  case MIToken::MCSymbol:
2091  return parseMCSymbolOperand(Dest);
2093  return parseSubRegisterIndexOperand(Dest);
2094  case MIToken::md_diexpr:
2095  case MIToken::exclaim:
2096  return parseMetadataOperand(Dest);
2111  return parseCFIOperand(Dest);
2113  return parseBlockAddressOperand(Dest);
2114  case MIToken::kw_intrinsic:
2115  return parseIntrinsicOperand(Dest);
2117  return parseTargetIndexOperand(Dest);
2118  case MIToken::kw_liveout:
2119  return parseLiveoutRegisterMaskOperand(Dest);
2120  case MIToken::kw_floatpred:
2121  case MIToken::kw_intpred:
2122  return parsePredicateOperand(Dest);
2123  case MIToken::Error:
2124  return true;
2125  case MIToken::Identifier:
2126  if (const auto *RegMask = getRegMask(Token.stringValue())) {
2127  Dest = MachineOperand::CreateRegMask(RegMask);
2128  lex();
2129  break;
2130  } else if (Token.stringValue() == "CustomRegMask") {
2131  return parseCustomRegisterMaskOperand(Dest);
2132  } else
2133  return parseTypedImmediateOperand(Dest);
2134  default:
2135  // FIXME: Parse the MCSymbol machine operand.
2136  return error("expected a machine operand");
2137  }
2138  return false;
2139 }
2140 
2141 bool MIParser::parseMachineOperandAndTargetFlags(
2142  MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
2143  unsigned TF = 0;
2144  bool HasTargetFlags = false;
2145  if (Token.is(MIToken::kw_target_flags)) {
2146  HasTargetFlags = true;
2147  lex();
2148  if (expectAndConsume(MIToken::lparen))
2149  return true;
2150  if (Token.isNot(MIToken::Identifier))
2151  return error("expected the name of the target flag");
2152  if (getDirectTargetFlag(Token.stringValue(), TF)) {
2153  if (getBitmaskTargetFlag(Token.stringValue(), TF))
2154  return error("use of undefined target flag '" + Token.stringValue() +
2155  "'");
2156  }
2157  lex();
2158  while (Token.is(MIToken::comma)) {
2159  lex();
2160  if (Token.isNot(MIToken::Identifier))
2161  return error("expected the name of the target flag");
2162  unsigned BitFlag = 0;
2163  if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2164  return error("use of undefined target flag '" + Token.stringValue() +
2165  "'");
2166  // TODO: Report an error when using a duplicate bit target flag.
2167  TF |= BitFlag;
2168  lex();
2169  }
2170  if (expectAndConsume(MIToken::rparen))
2171  return true;
2172  }
2173  auto Loc = Token.location();
2174  if (parseMachineOperand(Dest, TiedDefIdx))
2175  return true;
2176  if (!HasTargetFlags)
2177  return false;
2178  if (Dest.isReg())
2179  return error(Loc, "register operands can't have target flags");
2180  Dest.setTargetFlags(TF);
2181  return false;
2182 }
2183 
2184 bool MIParser::parseOffset(int64_t &Offset) {
2185  if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2186  return false;
2187  StringRef Sign = Token.range();
2188  bool IsNegative = Token.is(MIToken::minus);
2189  lex();
2190  if (Token.isNot(MIToken::IntegerLiteral))
2191  return error("expected an integer literal after '" + Sign + "'");
2192  if (Token.integerValue().getMinSignedBits() > 64)
2193  return error("expected 64-bit integer (too large)");
2194  Offset = Token.integerValue().getExtValue();
2195  if (IsNegative)
2196  Offset = -Offset;
2197  lex();
2198  return false;
2199 }
2200 
2201 bool MIParser::parseAlignment(unsigned &Alignment) {
2202  assert(Token.is(MIToken::kw_align));
2203  lex();
2204  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2205  return error("expected an integer literal after 'align'");
2206  if (getUnsigned(Alignment))
2207  return true;
2208  lex();
2209  return false;
2210 }
2211 
2212 bool MIParser::parseAddrspace(unsigned &Addrspace) {
2213  assert(Token.is(MIToken::kw_addrspace));
2214  lex();
2215  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2216  return error("expected an integer literal after 'addrspace'");
2217  if (getUnsigned(Addrspace))
2218  return true;
2219  lex();
2220  return false;
2221 }
2222 
2223 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2224  int64_t Offset = 0;
2225  if (parseOffset(Offset))
2226  return true;
2227  Op.setOffset(Offset);
2228  return false;
2229 }
2230 
2231 bool MIParser::parseIRValue(const Value *&V) {
2232  switch (Token.kind()) {
2233  case MIToken::NamedIRValue: {
2234  V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
2235  break;
2236  }
2237  case MIToken::IRValue: {
2238  unsigned SlotNumber = 0;
2239  if (getUnsigned(SlotNumber))
2240  return true;
2241  V = getIRValue(SlotNumber);
2242  break;
2243  }
2245  case MIToken::GlobalValue: {
2246  GlobalValue *GV = nullptr;
2247  if (parseGlobalValue(GV))
2248  return true;
2249  V = GV;
2250  break;
2251  }
2252  case MIToken::QuotedIRValue: {
2253  const Constant *C = nullptr;
2254  if (parseIRConstant(Token.location(), Token.stringValue(), C))
2255  return true;
2256  V = C;
2257  break;
2258  }
2259  default:
2260  llvm_unreachable("The current token should be an IR block reference");
2261  }
2262  if (!V)
2263  return error(Twine("use of undefined IR value '") + Token.range() + "'");
2264  return false;
2265 }
2266 
2267 bool MIParser::getUint64(uint64_t &Result) {
2268  if (Token.hasIntegerValue()) {
2269  if (Token.integerValue().getActiveBits() > 64)
2270  return error("expected 64-bit integer (too large)");
2271  Result = Token.integerValue().getZExtValue();
2272  return false;
2273  }
2274  if (Token.is(MIToken::HexLiteral)) {
2275  APInt A;
2276  if (getHexUint(A))
2277  return true;
2278  if (A.getBitWidth() > 64)
2279  return error("expected 64-bit integer (too large)");
2280  Result = A.getZExtValue();
2281  return false;
2282  }
2283  return true;
2284 }
2285 
2286 bool MIParser::getHexUint(APInt &Result) {
2287  assert(Token.is(MIToken::HexLiteral));
2288  StringRef S = Token.range();
2289  assert(S[0] == '0' && tolower(S[1]) == 'x');
2290  // This could be a floating point literal with a special prefix.
2291  if (!isxdigit(S[2]))
2292  return true;
2293  StringRef V = S.substr(2);
2294  APInt A(V.size()*4, V, 16);
2295 
2296  // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2297  // sure it isn't the case before constructing result.
2298  unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2299  Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2300  return false;
2301 }
2302 
2303 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2304  const auto OldFlags = Flags;
2305  switch (Token.kind()) {
2306  case MIToken::kw_volatile:
2308  break;
2311  break;
2314  break;
2315  case MIToken::kw_invariant:
2317  break;
2318  case MIToken::StringConstant: {
2320  if (getMMOTargetFlag(Token.stringValue(), TF))
2321  return error("use of undefined target MMO flag '" + Token.stringValue() +
2322  "'");
2323  Flags |= TF;
2324  break;
2325  }
2326  default:
2327  llvm_unreachable("The current token should be a memory operand flag");
2328  }
2329  if (OldFlags == Flags)
2330  // We know that the same flag is specified more than once when the flags
2331  // weren't modified.
2332  return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2333  lex();
2334  return false;
2335 }
2336 
2337 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2338  switch (Token.kind()) {
2339  case MIToken::kw_stack:
2340  PSV = MF.getPSVManager().getStack();
2341  break;
2342  case MIToken::kw_got:
2343  PSV = MF.getPSVManager().getGOT();
2344  break;
2346  PSV = MF.getPSVManager().getJumpTable();
2347  break;
2349  PSV = MF.getPSVManager().getConstantPool();
2350  break;
2352  int FI;
2353  if (parseFixedStackFrameIndex(FI))
2354  return true;
2355  PSV = MF.getPSVManager().getFixedStack(FI);
2356  // The token was already consumed, so use return here instead of break.
2357  return false;
2358  }
2359  case MIToken::StackObject: {
2360  int FI;
2361  if (parseStackFrameIndex(FI))
2362  return true;
2363  PSV = MF.getPSVManager().getFixedStack(FI);
2364  // The token was already consumed, so use return here instead of break.
2365  return false;
2366  }
2368  lex();
2369  switch (Token.kind()) {
2370  case MIToken::GlobalValue:
2372  GlobalValue *GV = nullptr;
2373  if (parseGlobalValue(GV))
2374  return true;
2376  break;
2377  }
2380  MF.createExternalSymbolName(Token.stringValue()));
2381  break;
2382  default:
2383  return error(
2384  "expected a global value or an external symbol after 'call-entry'");
2385  }
2386  break;
2387  default:
2388  llvm_unreachable("The current token should be pseudo source value");
2389  }
2390  lex();
2391  return false;
2392 }
2393 
2394 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2395  if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
2396  Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
2397  Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2398  Token.is(MIToken::kw_call_entry)) {
2399  const PseudoSourceValue *PSV = nullptr;
2400  if (parseMemoryPseudoSourceValue(PSV))
2401  return true;
2402  int64_t Offset = 0;
2403  if (parseOffset(Offset))
2404  return true;
2405  Dest = MachinePointerInfo(PSV, Offset);
2406  return false;
2407  }
2408  if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2409  Token.isNot(MIToken::GlobalValue) &&
2410  Token.isNot(MIToken::NamedGlobalValue) &&
2411  Token.isNot(MIToken::QuotedIRValue))
2412  return error("expected an IR value reference");
2413  const Value *V = nullptr;
2414  if (parseIRValue(V))
2415  return true;
2416  if (!V->getType()->isPointerTy())
2417  return error("expected a pointer IR value");
2418  lex();
2419  int64_t Offset = 0;
2420  if (parseOffset(Offset))
2421  return true;
2422  Dest = MachinePointerInfo(V, Offset);
2423  return false;
2424 }
2425 
2426 bool MIParser::parseOptionalScope(LLVMContext &Context,
2427  SyncScope::ID &SSID) {
2428  SSID = SyncScope::System;
2429  if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
2430  lex();
2431  if (expectAndConsume(MIToken::lparen))
2432  return error("expected '(' in syncscope");
2433 
2434  std::string SSN;
2435  if (parseStringConstant(SSN))
2436  return true;
2437 
2438  SSID = Context.getOrInsertSyncScopeID(SSN);
2439  if (expectAndConsume(MIToken::rparen))
2440  return error("expected ')' in syncscope");
2441  }
2442 
2443  return false;
2444 }
2445 
2446 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2447  Order = AtomicOrdering::NotAtomic;
2448  if (Token.isNot(MIToken::Identifier))
2449  return false;
2450 
2451  Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2452  .Case("unordered", AtomicOrdering::Unordered)
2453  .Case("monotonic", AtomicOrdering::Monotonic)
2454  .Case("acquire", AtomicOrdering::Acquire)
2455  .Case("release", AtomicOrdering::Release)
2459 
2460  if (Order != AtomicOrdering::NotAtomic) {
2461  lex();
2462  return false;
2463  }
2464 
2465  return error("expected an atomic scope, ordering or a size specification");
2466 }
2467 
2468 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2469  if (expectAndConsume(MIToken::lparen))
2470  return true;
2472  while (Token.isMemoryOperandFlag()) {
2473  if (parseMemoryOperandFlag(Flags))
2474  return true;
2475  }
2476  if (Token.isNot(MIToken::Identifier) ||
2477  (Token.stringValue() != "load" && Token.stringValue() != "store"))
2478  return error("expected 'load' or 'store' memory operation");
2479  if (Token.stringValue() == "load")
2480  Flags |= MachineMemOperand::MOLoad;
2481  else
2482  Flags |= MachineMemOperand::MOStore;
2483  lex();
2484 
2485  // Optional 'store' for operands that both load and store.
2486  if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
2487  Flags |= MachineMemOperand::MOStore;
2488  lex();
2489  }
2490 
2491  // Optional synchronization scope.
2492  SyncScope::ID SSID;
2493  if (parseOptionalScope(MF.getFunction().getContext(), SSID))
2494  return true;
2495 
2496  // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2497  AtomicOrdering Order, FailureOrder;
2498  if (parseOptionalAtomicOrdering(Order))
2499  return true;
2500 
2501  if (parseOptionalAtomicOrdering(FailureOrder))
2502  return true;
2503 
2504  if (Token.isNot(MIToken::IntegerLiteral) &&
2505  Token.isNot(MIToken::kw_unknown_size))
2506  return error("expected the size integer literal or 'unknown-size' after "
2507  "memory operation");
2508  uint64_t Size;
2509  if (Token.is(MIToken::IntegerLiteral)) {
2510  if (getUint64(Size))
2511  return true;
2512  } else if (Token.is(MIToken::kw_unknown_size)) {
2514  }
2515  lex();
2516 
2518  if (Token.is(MIToken::Identifier)) {
2519  const char *Word =
2520  ((Flags & MachineMemOperand::MOLoad) &&
2521  (Flags & MachineMemOperand::MOStore))
2522  ? "on"
2523  : Flags & MachineMemOperand::MOLoad ? "from" : "into";
2524  if (Token.stringValue() != Word)
2525  return error(Twine("expected '") + Word + "'");
2526  lex();
2527 
2528  if (parseMachinePointerInfo(Ptr))
2529  return true;
2530  }
2531  unsigned BaseAlignment = (Size != MemoryLocation::UnknownSize ? Size : 1);
2532  AAMDNodes AAInfo;
2533  MDNode *Range = nullptr;
2534  while (consumeIfPresent(MIToken::comma)) {
2535  switch (Token.kind()) {
2536  case MIToken::kw_align:
2537  if (parseAlignment(BaseAlignment))
2538  return true;
2539  break;
2540  case MIToken::kw_addrspace:
2541  if (parseAddrspace(Ptr.AddrSpace))
2542  return true;
2543  break;
2544  case MIToken::md_tbaa:
2545  lex();
2546  if (parseMDNode(AAInfo.TBAA))
2547  return true;
2548  break;
2550  lex();
2551  if (parseMDNode(AAInfo.Scope))
2552  return true;
2553  break;
2554  case MIToken::md_noalias:
2555  lex();
2556  if (parseMDNode(AAInfo.NoAlias))
2557  return true;
2558  break;
2559  case MIToken::md_range:
2560  lex();
2561  if (parseMDNode(Range))
2562  return true;
2563  break;
2564  // TODO: Report an error on duplicate metadata nodes.
2565  default:
2566  return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2567  "'!noalias' or '!range'");
2568  }
2569  }
2570  if (expectAndConsume(MIToken::rparen))
2571  return true;
2572  Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2573  SSID, Order, FailureOrder);
2574  return false;
2575 }
2576 
2577 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
2578  assert((Token.is(MIToken::kw_pre_instr_symbol) ||
2579  Token.is(MIToken::kw_post_instr_symbol)) &&
2580  "Invalid token for a pre- post-instruction symbol!");
2581  lex();
2582  if (Token.isNot(MIToken::MCSymbol))
2583  return error("expected a symbol after 'pre-instr-symbol'");
2584  Symbol = getOrCreateMCSymbol(Token.stringValue());
2585  lex();
2586  if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
2587  Token.is(MIToken::lbrace))
2588  return false;
2589  if (Token.isNot(MIToken::comma))
2590  return error("expected ',' before the next machine operand");
2591  lex();
2592  return false;
2593 }
2594 
2595 void MIParser::initNames2InstrOpCodes() {
2596  if (!Names2InstrOpCodes.empty())
2597  return;
2598  const auto *TII = MF.getSubtarget().getInstrInfo();
2599  assert(TII && "Expected target instruction info");
2600  for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2601  Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2602 }
2603 
2604 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2605  initNames2InstrOpCodes();
2606  auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2607  if (InstrInfo == Names2InstrOpCodes.end())
2608  return true;
2609  OpCode = InstrInfo->getValue();
2610  return false;
2611 }
2612 
2613 void MIParser::initNames2Regs() {
2614  if (!Names2Regs.empty())
2615  return;
2616  // The '%noreg' register is the register 0.
2617  Names2Regs.insert(std::make_pair("noreg", 0));
2618  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2619  assert(TRI && "Expected target register info");
2620  for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2621  bool WasInserted =
2622  Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2623  .second;
2624  (void)WasInserted;
2625  assert(WasInserted && "Expected registers to be unique case-insensitively");
2626  }
2627 }
2628 
2629 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2630  initNames2Regs();
2631  auto RegInfo = Names2Regs.find(RegName);
2632  if (RegInfo == Names2Regs.end())
2633  return true;
2634  Reg = RegInfo->getValue();
2635  return false;
2636 }
2637 
2638 void MIParser::initNames2RegMasks() {
2639  if (!Names2RegMasks.empty())
2640  return;
2641  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2642  assert(TRI && "Expected target register info");
2643  ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2644  ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2645  assert(RegMasks.size() == RegMaskNames.size());
2646  for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2647  Names2RegMasks.insert(
2648  std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2649 }
2650 
2651 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2652  initNames2RegMasks();
2653  auto RegMaskInfo = Names2RegMasks.find(Identifier);
2654  if (RegMaskInfo == Names2RegMasks.end())
2655  return nullptr;
2656  return RegMaskInfo->getValue();
2657 }
2658 
2659 void MIParser::initNames2SubRegIndices() {
2660  if (!Names2SubRegIndices.empty())
2661  return;
2663  for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2664  Names2SubRegIndices.insert(
2665  std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2666 }
2667 
2668 unsigned MIParser::getSubRegIndex(StringRef Name) {
2669  initNames2SubRegIndices();
2670  auto SubRegInfo = Names2SubRegIndices.find(Name);
2671  if (SubRegInfo == Names2SubRegIndices.end())
2672  return 0;
2673  return SubRegInfo->getValue();
2674 }
2675 
2677  const Function &F,
2678  DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2679  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2680  MST.incorporateFunction(F);
2681  for (auto &BB : F) {
2682  if (BB.hasName())
2683  continue;
2684  int Slot = MST.getLocalSlot(&BB);
2685  if (Slot == -1)
2686  continue;
2687  Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2688  }
2689 }
2690 
2692  unsigned Slot,
2693  const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2694  auto BlockInfo = Slots2BasicBlocks.find(Slot);
2695  if (BlockInfo == Slots2BasicBlocks.end())
2696  return nullptr;
2697  return BlockInfo->second;
2698 }
2699 
2700 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2701  if (Slots2BasicBlocks.empty())
2702  initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
2703  return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2704 }
2705 
2706 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2707  if (&F == &MF.getFunction())
2708  return getIRBlock(Slot);
2709  DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2710  initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2711  return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2712 }
2713 
2714 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2715  DenseMap<unsigned, const Value *> &Slots2Values) {
2716  int Slot = MST.getLocalSlot(V);
2717  if (Slot == -1)
2718  return;
2719  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2720 }
2721 
2722 /// Creates the mapping from slot numbers to function's unnamed IR values.
2723 static void initSlots2Values(const Function &F,
2724  DenseMap<unsigned, const Value *> &Slots2Values) {
2725  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2726  MST.incorporateFunction(F);
2727  for (const auto &Arg : F.args())
2728  mapValueToSlot(&Arg, MST, Slots2Values);
2729  for (const auto &BB : F) {
2730  mapValueToSlot(&BB, MST, Slots2Values);
2731  for (const auto &I : BB)
2732  mapValueToSlot(&I, MST, Slots2Values);
2733  }
2734 }
2735 
2736 const Value *MIParser::getIRValue(unsigned Slot) {
2737  if (Slots2Values.empty())
2738  initSlots2Values(MF.getFunction(), Slots2Values);
2739  auto ValueInfo = Slots2Values.find(Slot);
2740  if (ValueInfo == Slots2Values.end())
2741  return nullptr;
2742  return ValueInfo->second;
2743 }
2744 
2745 void MIParser::initNames2TargetIndices() {
2746  if (!Names2TargetIndices.empty())
2747  return;
2748  const auto *TII = MF.getSubtarget().getInstrInfo();
2749  assert(TII && "Expected target instruction info");
2750  auto Indices = TII->getSerializableTargetIndices();
2751  for (const auto &I : Indices)
2752  Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2753 }
2754 
2755 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2756  initNames2TargetIndices();
2757  auto IndexInfo = Names2TargetIndices.find(Name);
2758  if (IndexInfo == Names2TargetIndices.end())
2759  return true;
2760  Index = IndexInfo->second;
2761  return false;
2762 }
2763 
2764 void MIParser::initNames2DirectTargetFlags() {
2765  if (!Names2DirectTargetFlags.empty())
2766  return;
2767  const auto *TII = MF.getSubtarget().getInstrInfo();
2768  assert(TII && "Expected target instruction info");
2770  for (const auto &I : Flags)
2771  Names2DirectTargetFlags.insert(
2772  std::make_pair(StringRef(I.second), I.first));
2773 }
2774 
2775 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2776  initNames2DirectTargetFlags();
2777  auto FlagInfo = Names2DirectTargetFlags.find(Name);
2778  if (FlagInfo == Names2DirectTargetFlags.end())
2779  return true;
2780  Flag = FlagInfo->second;
2781  return false;
2782 }
2783 
2784 void MIParser::initNames2BitmaskTargetFlags() {
2785  if (!Names2BitmaskTargetFlags.empty())
2786  return;
2787  const auto *TII = MF.getSubtarget().getInstrInfo();
2788  assert(TII && "Expected target instruction info");
2790  for (const auto &I : Flags)
2791  Names2BitmaskTargetFlags.insert(
2792  std::make_pair(StringRef(I.second), I.first));
2793 }
2794 
2795 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2796  initNames2BitmaskTargetFlags();
2797  auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2798  if (FlagInfo == Names2BitmaskTargetFlags.end())
2799  return true;
2800  Flag = FlagInfo->second;
2801  return false;
2802 }
2803 
2804 void MIParser::initNames2MMOTargetFlags() {
2805  if (!Names2MMOTargetFlags.empty())
2806  return;
2807  const auto *TII = MF.getSubtarget().getInstrInfo();
2808  assert(TII && "Expected target instruction info");
2809  auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
2810  for (const auto &I : Flags)
2811  Names2MMOTargetFlags.insert(
2812  std::make_pair(StringRef(I.second), I.first));
2813 }
2814 
2815 bool MIParser::getMMOTargetFlag(StringRef Name,
2816  MachineMemOperand::Flags &Flag) {
2817  initNames2MMOTargetFlags();
2818  auto FlagInfo = Names2MMOTargetFlags.find(Name);
2819  if (FlagInfo == Names2MMOTargetFlags.end())
2820  return true;
2821  Flag = FlagInfo->second;
2822  return false;
2823 }
2824 
2825 MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
2826  // FIXME: Currently we can't recognize temporary or local symbols and call all
2827  // of the appropriate forms to create them. However, this handles basic cases
2828  // well as most of the special aspects are recognized by a prefix on their
2829  // name, and the input names should already be unique. For test cases, keeping
2830  // the symbol name out of the symbol table isn't terribly important.
2831  return MF.getContext().getOrCreateSymbol(Name);
2832 }
2833 
2834 bool MIParser::parseStringConstant(std::string &Result) {
2835  if (Token.isNot(MIToken::StringConstant))
2836  return error("expected string constant");
2837  Result = Token.stringValue();
2838  lex();
2839  return false;
2840 }
2841 
2843  StringRef Src,
2844  SMDiagnostic &Error) {
2845  return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2846 }
2847 
2849  StringRef Src, SMDiagnostic &Error) {
2850  return MIParser(PFS, Error, Src).parseBasicBlocks();
2851 }
2852 
2854  MachineBasicBlock *&MBB, StringRef Src,
2855  SMDiagnostic &Error) {
2856  return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
2857 }
2858 
2860  unsigned &Reg, StringRef Src,
2861  SMDiagnostic &Error) {
2862  return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2863 }
2864 
2866  unsigned &Reg, StringRef Src,
2867  SMDiagnostic &Error) {
2868  return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
2869 }
2870 
2872  VRegInfo *&Info, StringRef Src,
2873  SMDiagnostic &Error) {
2874  return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
2875 }
2876 
2878  int &FI, StringRef Src,
2879  SMDiagnostic &Error) {
2880  return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
2881 }
2882 
2884  MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2885  return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
2886 }
bool isFPPredicate() const
Definition: InstrTypes.h:776
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:70
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
Definition: MIParser.cpp:2842
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock *> &Slots2BasicBlocks)
Definition: MIParser.cpp:2676
A common definition of LaneBitmask for use in TableGen and CodeGen.
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1557
static MachineOperand CreateCImm(const ConstantInt *CI)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
DenseMap< unsigned, unsigned > JumpTableSlots
Definition: MIParser.h:63
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
LLVMContext & Context
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:158
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:661
Atomic ordering constants.
DenseMap< unsigned, unsigned > ConstantPoolSlots
Definition: MIParser.h:62
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:658
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:148
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:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:532
const MCPhysReg * getImplicitUses() const
Return a list of registers that are potentially read by any instance of this machine instruction...
Definition: MCInstrDesc.h:518
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
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:711
unsigned less than
Definition: InstrTypes.h:710
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
Definition: AsmWriter.cpp:850
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:691
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:333
DenseMap< unsigned, int > StackObjectSlots
Definition: MIParser.h:61
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:701
#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:1042
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:360
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:864
F(f)
DenseMap< unsigned, VRegInfo * > VRegInfos
Definition: MIParser.h:58
Manage lifetime of a slot tracker for printing IR.
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2853
const TargetRegisterClass * RC
Definition: MIParser.h:39
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
static MachineOperand CreateIntrinsicID(Intrinsic::ID ID)
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1503
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:521
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:191
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:696
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:498
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:695
std::map< unsigned, TrackingMDNodeRef > MetadataNodes
Definition: SlotMapping.h:35
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:963
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
const HexagonInstrInfo * TII
static const BasicBlock * getIRBlockFromSlot(unsigned Slot, const DenseMap< unsigned, const BasicBlock *> &Slots2BasicBlocks)
Definition: MIParser.cpp:2691
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition: MIParser.cpp:103
support::ulittle32_t Word
Definition: IRSymtab.h:51
unsigned SubReg
static constexpr LaneBitmask getAll()
Definition: LaneBitmask.h:84
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:692
const Name2RegBankMap & Names2RegBanks
Definition: MIParser.h:55
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
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:60
static std::string getRegisterName(const TargetRegisterInfo *TRI, unsigned Reg)
Definition: MIParser.cpp:909
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:131
#define UINT64_MAX
Definition: DataTypes.h:83
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
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:112
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
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:2871
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...
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:118
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:146
unsigned getMainFileID() const
Definition: SourceMgr.h:140
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:527
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2877
const MCPhysReg * getImplicitDefs() const
Return a list of registers that are potentially written by any instance of this machine instruction...
Definition: MCInstrDesc.h:540
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:904
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
static Intrinsic::ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
Definition: Function.cpp:536
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned char TargetFlags=0)
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
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:69
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:1411
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:149
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:42
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:224
StringMap< VRegInfo * > VRegInfosNamed
Definition: MIParser.h:59
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:50
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:2865
void setFlag(MIFlag Flag)
Set a MI flag.
Definition: MachineInstr.h:300
bool isValid() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:685
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)
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:515
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:694
The memory access is non-temporal.
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:59
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:505
MachineFunction & MF
Definition: MIParser.h:51
std::vector< GlobalValue * > GlobalValues
Definition: SlotMapping.h:34
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:42
void setOffset(int64_t Offset)
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:702
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:836
const SlotMapping & IRSlots
Definition: MIParser.h:53
static MCCFIInstruction createRestoreState(MCSymbol *L)
.cfi_restore_state Restore the previously saved state.
Definition: MCDwarf.h:537
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:700
#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:712
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:2848
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE 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:645
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:689
const Name2RegClassMap & Names2RegClasses
Definition: MIParser.h:54
unsigned createIncompleteVirtualRegister(StringRef Name="")
Creates a new virtual register that has no register class, register bank or size assigned yet...
static MachineOperand CreateMetadata(const MDNode *Meta)
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
void setFlags(unsigned flags)
Definition: MachineInstr.h:304
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:699
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
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:916
StringRef getMessage() const
Definition: SourceMgr.h:291
signed less than
Definition: InstrTypes.h:714
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:664
uint32_t * allocateRegMask()
Allocate and initialize a register mask with NumRegister bits.
This class implements the register bank concept.
Definition: RegisterBank.h:29
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:42
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition: Function.h:648
bool isIntPredicate() const
Definition: InstrTypes.h:777
signed less or equal
Definition: InstrTypes.h:715
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
Class for arbitrary precision integers.
Definition: APInt.h:70
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)...
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
This struct contains the mappings from the slot numbers to unnamed metadata nodes, global values and types.
Definition: SlotMapping.h:33
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2883
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:37
Flags
Flags values. These may be or&#39;d together.
amdgpu Simplify well known AMD library false Value Value * Arg
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:37
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:61
int getColumnNo() const
Definition: SourceMgr.h:289
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
unsigned greater or equal
Definition: InstrTypes.h:709
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:45
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:42
static MachineOperand CreateImm(int64_t Val)
VRegInfo & getVRegInfo(unsigned Num)
Definition: MIParser.cpp:92
#define I(x, y, z)
Definition: MD5.cpp:58
The memory access always returns the same value (or traps).
iterator end()
Definition: DenseMap.h:79
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:693
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition: MIParser.h:57
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:323
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
uint32_t Size
Definition: Profile.cpp:47
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:697
const char * getBufferStart() const
Definition: MemoryBuffer.h:60
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:18
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const unsigned Kind
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:688
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
LLVM Value Representation.
Definition: Value.h:73
static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space (defaulting to 0).
A token produced by the machine instruction lexer.
Definition: MILexer.h:28
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:698
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
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:81
IRTranslator LLVM IR MI
unsigned greater than
Definition: InstrTypes.h:708
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
union llvm::VRegInfo::@327 D
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:40
Represents a location in source code.
Definition: SMLoc.h:24
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals)
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition: MCDwarf.h:543
bool parseRegisterReference(PerFunctionMIParsingState &PFS, unsigned &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2859
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:690
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value *> &Slots2Values)
Definition: MIParser.cpp:2714
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:2723
static MachineOperand CreateFI(int Idx)
const PseudoSourceValue * getConstantPool()
Return a pseudo source value referencing the constant pool.
iterator end()
Definition: StringMap.h:318
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:689
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:687
signed greater or equal
Definition: InstrTypes.h:713
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
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:260
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, const Name2RegClassMap &Names2RegClasses, const Name2RegBankMap &Names2RegBanks)
Definition: MIParser.cpp:84
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.