LLVM  4.0.0
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/StringMap.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/AsmParser/Parser.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Module.h"
34 #include "llvm/Support/SourceMgr.h"
39 #include <cctype>
40 
41 using namespace llvm;
42 
44  SourceMgr &SM, const SlotMapping &IRSlots)
45  : MF(MF), SM(&SM), IRSlots(IRSlots) {
46 }
47 
49  auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
50  if (I.second) {
52  VRegInfo *Info = new (Allocator) VRegInfo;
54  I.first->second = Info;
55  }
56  return *I.first->second;
57 }
58 
59 namespace {
60 
61 /// A wrapper struct around the 'MachineOperand' struct that includes a source
62 /// range and other attributes.
63 struct ParsedMachineOperand {
64  MachineOperand Operand;
65  StringRef::iterator Begin;
67  Optional<unsigned> TiedDefIdx;
68 
69  ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
71  : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
72  if (TiedDefIdx)
73  assert(Operand.isReg() && Operand.isUse() &&
74  "Only used register operands can be tied");
75  }
76 };
77 
78 class MIParser {
79  MachineFunction &MF;
81  StringRef Source, CurrentSource;
82  MIToken Token;
84  /// Maps from instruction names to op codes.
85  StringMap<unsigned> Names2InstrOpCodes;
86  /// Maps from register names to registers.
87  StringMap<unsigned> Names2Regs;
88  /// Maps from register mask names to register masks.
89  StringMap<const uint32_t *> Names2RegMasks;
90  /// Maps from subregister names to subregister indices.
91  StringMap<unsigned> Names2SubRegIndices;
92  /// Maps from slot numbers to function's unnamed basic blocks.
94  /// Maps from slot numbers to function's unnamed values.
96  /// Maps from target index names to target indices.
97  StringMap<int> Names2TargetIndices;
98  /// Maps from direct target flag names to the direct target flag values.
99  StringMap<unsigned> Names2DirectTargetFlags;
100  /// Maps from direct target flag names to the bitmask target flag values.
101  StringMap<unsigned> Names2BitmaskTargetFlags;
102 
103 public:
105  StringRef Source);
106 
107  /// \p SkipChar gives the number of characters to skip before looking
108  /// for the next token.
109  void lex(unsigned SkipChar = 0);
110 
111  /// Report an error at the current location with the given message.
112  ///
113  /// This function always return true.
114  bool error(const Twine &Msg);
115 
116  /// Report an error at the given location with the given message.
117  ///
118  /// This function always return true.
119  bool error(StringRef::iterator Loc, const Twine &Msg);
120 
121  bool
122  parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
123  bool parseBasicBlocks();
124  bool parse(MachineInstr *&MI);
125  bool parseStandaloneMBB(MachineBasicBlock *&MBB);
126  bool parseStandaloneNamedRegister(unsigned &Reg);
127  bool parseStandaloneVirtualRegister(VRegInfo *&Info);
128  bool parseStandaloneRegister(unsigned &Reg);
129  bool parseStandaloneStackObject(int &FI);
130  bool parseStandaloneMDNode(MDNode *&Node);
131 
132  bool
133  parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
134  bool parseBasicBlock(MachineBasicBlock &MBB);
135  bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
136  bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
137 
138  bool parseNamedRegister(unsigned &Reg);
139  bool parseVirtualRegister(VRegInfo *&Info);
140  bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
141  bool parseRegisterFlag(unsigned &Flags);
142  bool parseSubRegisterIndex(unsigned &SubReg);
143  bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
144  bool parseRegisterOperand(MachineOperand &Dest,
145  Optional<unsigned> &TiedDefIdx, bool IsDef = false);
146  bool parseImmediateOperand(MachineOperand &Dest);
147  bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
148  const Constant *&C);
149  bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
150  bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
151  bool parseTypedImmediateOperand(MachineOperand &Dest);
152  bool parseFPImmediateOperand(MachineOperand &Dest);
154  bool parseMBBOperand(MachineOperand &Dest);
155  bool parseStackFrameIndex(int &FI);
156  bool parseStackObjectOperand(MachineOperand &Dest);
157  bool parseFixedStackFrameIndex(int &FI);
158  bool parseFixedStackObjectOperand(MachineOperand &Dest);
159  bool parseGlobalValue(GlobalValue *&GV);
160  bool parseGlobalAddressOperand(MachineOperand &Dest);
161  bool parseConstantPoolIndexOperand(MachineOperand &Dest);
162  bool parseSubRegisterIndexOperand(MachineOperand &Dest);
163  bool parseJumpTableIndexOperand(MachineOperand &Dest);
164  bool parseExternalSymbolOperand(MachineOperand &Dest);
165  bool parseMDNode(MDNode *&Node);
166  bool parseMetadataOperand(MachineOperand &Dest);
167  bool parseCFIOffset(int &Offset);
168  bool parseCFIRegister(unsigned &Reg);
169  bool parseCFIOperand(MachineOperand &Dest);
170  bool parseIRBlock(BasicBlock *&BB, const Function &F);
171  bool parseBlockAddressOperand(MachineOperand &Dest);
172  bool parseIntrinsicOperand(MachineOperand &Dest);
173  bool parsePredicateOperand(MachineOperand &Dest);
174  bool parseTargetIndexOperand(MachineOperand &Dest);
175  bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
176  bool parseMachineOperand(MachineOperand &Dest,
177  Optional<unsigned> &TiedDefIdx);
178  bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
179  Optional<unsigned> &TiedDefIdx);
180  bool parseOffset(int64_t &Offset);
181  bool parseAlignment(unsigned &Alignment);
182  bool parseOperandsOffset(MachineOperand &Op);
183  bool parseIRValue(const Value *&V);
184  bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
185  bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
186  bool parseMachinePointerInfo(MachinePointerInfo &Dest);
187  bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
188 
189 private:
190  /// Convert the integer literal in the current token into an unsigned integer.
191  ///
192  /// Return true if an error occurred.
193  bool getUnsigned(unsigned &Result);
194 
195  /// Convert the integer literal in the current token into an uint64.
196  ///
197  /// Return true if an error occurred.
198  bool getUint64(uint64_t &Result);
199 
200  /// Convert the hexadecimal literal in the current token into an unsigned
201  /// APInt with a minimum bitwidth required to represent the value.
202  ///
203  /// Return true if the literal does not represent an integer value.
204  bool getHexUint(APInt &Result);
205 
206  /// If the current token is of the given kind, consume it and return false.
207  /// Otherwise report an error and return true.
208  bool expectAndConsume(MIToken::TokenKind TokenKind);
209 
210  /// If the current token is of the given kind, consume it and return true.
211  /// Otherwise return false.
212  bool consumeIfPresent(MIToken::TokenKind TokenKind);
213 
214  void initNames2InstrOpCodes();
215 
216  /// Try to convert an instruction name to an opcode. Return true if the
217  /// instruction name is invalid.
218  bool parseInstrName(StringRef InstrName, unsigned &OpCode);
219 
220  bool parseInstruction(unsigned &OpCode, unsigned &Flags);
221 
222  bool assignRegisterTies(MachineInstr &MI,
224 
225  bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
226  const MCInstrDesc &MCID);
227 
228  void initNames2Regs();
229 
230  /// Try to convert a register name to a register number. Return true if the
231  /// register name is invalid.
232  bool getRegisterByName(StringRef RegName, unsigned &Reg);
233 
234  void initNames2RegMasks();
235 
236  /// Check if the given identifier is a name of a register mask.
237  ///
238  /// Return null if the identifier isn't a register mask.
239  const uint32_t *getRegMask(StringRef Identifier);
240 
241  void initNames2SubRegIndices();
242 
243  /// Check if the given identifier is a name of a subregister index.
244  ///
245  /// Return 0 if the name isn't a subregister index class.
246  unsigned getSubRegIndex(StringRef Name);
247 
248  const BasicBlock *getIRBlock(unsigned Slot);
249  const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
250 
251  const Value *getIRValue(unsigned Slot);
252 
253  void initNames2TargetIndices();
254 
255  /// Try to convert a name of target index to the corresponding target index.
256  ///
257  /// Return true if the name isn't a name of a target index.
258  bool getTargetIndex(StringRef Name, int &Index);
259 
260  void initNames2DirectTargetFlags();
261 
262  /// Try to convert a name of a direct target flag to the corresponding
263  /// target flag.
264  ///
265  /// Return true if the name isn't a name of a direct flag.
266  bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
267 
268  void initNames2BitmaskTargetFlags();
269 
270  /// Try to convert a name of a bitmask target flag to the corresponding
271  /// target flag.
272  ///
273  /// Return true if the name isn't a name of a bitmask target flag.
274  bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
275 };
276 
277 } // end anonymous namespace
278 
279 MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
281  : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
282 {}
283 
284 void MIParser::lex(unsigned SkipChar) {
285  CurrentSource = lexMIToken(
286  CurrentSource.data() + SkipChar, Token,
287  [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
288 }
289 
290 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
291 
292 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
293  const SourceMgr &SM = *PFS.SM;
294  assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
295  const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
296  if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
297  // Create an ordinary diagnostic when the source manager's buffer is the
298  // source string.
300  return true;
301  }
302  // Create a diagnostic for a YAML string literal.
303  Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
304  Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
305  Source, None, None);
306  return true;
307 }
308 
309 static const char *toString(MIToken::TokenKind TokenKind) {
310  switch (TokenKind) {
311  case MIToken::comma:
312  return "','";
313  case MIToken::equal:
314  return "'='";
315  case MIToken::colon:
316  return "':'";
317  case MIToken::lparen:
318  return "'('";
319  case MIToken::rparen:
320  return "')'";
321  default:
322  return "<unknown token>";
323  }
324 }
325 
326 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
327  if (Token.isNot(TokenKind))
328  return error(Twine("expected ") + toString(TokenKind));
329  lex();
330  return false;
331 }
332 
333 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
334  if (Token.isNot(TokenKind))
335  return false;
336  lex();
337  return true;
338 }
339 
340 bool MIParser::parseBasicBlockDefinition(
343  unsigned ID = 0;
344  if (getUnsigned(ID))
345  return true;
346  auto Loc = Token.location();
347  auto Name = Token.stringValue();
348  lex();
349  bool HasAddressTaken = false;
350  bool IsLandingPad = false;
351  unsigned Alignment = 0;
352  BasicBlock *BB = nullptr;
353  if (consumeIfPresent(MIToken::lparen)) {
354  do {
355  // TODO: Report an error when multiple same attributes are specified.
356  switch (Token.kind()) {
358  HasAddressTaken = true;
359  lex();
360  break;
362  IsLandingPad = true;
363  lex();
364  break;
365  case MIToken::kw_align:
366  if (parseAlignment(Alignment))
367  return true;
368  break;
369  case MIToken::IRBlock:
370  // TODO: Report an error when both name and ir block are specified.
371  if (parseIRBlock(BB, *MF.getFunction()))
372  return true;
373  lex();
374  break;
375  default:
376  break;
377  }
378  } while (consumeIfPresent(MIToken::comma));
379  if (expectAndConsume(MIToken::rparen))
380  return true;
381  }
382  if (expectAndConsume(MIToken::colon))
383  return true;
384 
385  if (!Name.empty()) {
386  BB = dyn_cast_or_null<BasicBlock>(
387  MF.getFunction()->getValueSymbolTable()->lookup(Name));
388  if (!BB)
389  return error(Loc, Twine("basic block '") + Name +
390  "' is not defined in the function '" +
391  MF.getName() + "'");
392  }
393  auto *MBB = MF.CreateMachineBasicBlock(BB);
394  MF.insert(MF.end(), MBB);
395  bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
396  if (!WasInserted)
397  return error(Loc, Twine("redefinition of machine basic block with id #") +
398  Twine(ID));
399  if (Alignment)
400  MBB->setAlignment(Alignment);
401  if (HasAddressTaken)
403  MBB->setIsEHPad(IsLandingPad);
404  return false;
405 }
406 
407 bool MIParser::parseBasicBlockDefinitions(
409  lex();
410  // Skip until the first machine basic block.
411  while (Token.is(MIToken::Newline))
412  lex();
413  if (Token.isErrorOrEOF())
414  return Token.isError();
416  return error("expected a basic block definition before instructions");
417  unsigned BraceDepth = 0;
418  do {
419  if (parseBasicBlockDefinition(MBBSlots))
420  return true;
421  bool IsAfterNewline = false;
422  // Skip until the next machine basic block.
423  while (true) {
424  if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
425  Token.isErrorOrEOF())
426  break;
428  return error("basic block definition should be located at the start of "
429  "the line");
430  else if (consumeIfPresent(MIToken::Newline)) {
431  IsAfterNewline = true;
432  continue;
433  }
434  IsAfterNewline = false;
435  if (Token.is(MIToken::lbrace))
436  ++BraceDepth;
437  if (Token.is(MIToken::rbrace)) {
438  if (!BraceDepth)
439  return error("extraneous closing brace ('}')");
440  --BraceDepth;
441  }
442  lex();
443  }
444  // Verify that we closed all of the '{' at the end of a file or a block.
445  if (!Token.isError() && BraceDepth)
446  return error("expected '}'"); // FIXME: Report a note that shows '{'.
447  } while (!Token.isErrorOrEOF());
448  return Token.isError();
449 }
450 
451 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
453  lex();
454  if (expectAndConsume(MIToken::colon))
455  return true;
456  if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
457  return false;
458  do {
459  if (Token.isNot(MIToken::NamedRegister))
460  return error("expected a named register");
461  unsigned Reg = 0;
462  if (parseNamedRegister(Reg))
463  return true;
464  lex();
466  if (consumeIfPresent(MIToken::colon)) {
467  // Parse lane mask.
468  if (Token.isNot(MIToken::IntegerLiteral) &&
469  Token.isNot(MIToken::HexLiteral))
470  return error("expected a lane mask");
471  static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
472  "Use correct get-function for lane mask");
474  if (getUnsigned(V))
475  return error("invalid lane mask value");
476  Mask = LaneBitmask(V);
477  lex();
478  }
479  MBB.addLiveIn(Reg, Mask);
480  } while (consumeIfPresent(MIToken::comma));
481  return false;
482 }
483 
484 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
486  lex();
487  if (expectAndConsume(MIToken::colon))
488  return true;
489  if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
490  return false;
491  do {
493  return error("expected a machine basic block reference");
494  MachineBasicBlock *SuccMBB = nullptr;
495  if (parseMBBReference(SuccMBB))
496  return true;
497  lex();
498  unsigned Weight = 0;
499  if (consumeIfPresent(MIToken::lparen)) {
500  if (Token.isNot(MIToken::IntegerLiteral) &&
501  Token.isNot(MIToken::HexLiteral))
502  return error("expected an integer literal after '('");
503  if (getUnsigned(Weight))
504  return true;
505  lex();
506  if (expectAndConsume(MIToken::rparen))
507  return true;
508  }
509  MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
510  } while (consumeIfPresent(MIToken::comma));
511  MBB.normalizeSuccProbs();
512  return false;
513 }
514 
515 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
516  // Skip the definition.
518  lex();
519  if (consumeIfPresent(MIToken::lparen)) {
520  while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
521  lex();
522  consumeIfPresent(MIToken::rparen);
523  }
524  consumeIfPresent(MIToken::colon);
525 
526  // Parse the liveins and successors.
527  // N.B: Multiple lists of successors and liveins are allowed and they're
528  // merged into one.
529  // Example:
530  // liveins: %edi
531  // liveins: %esi
532  //
533  // is equivalent to
534  // liveins: %edi, %esi
535  while (true) {
536  if (Token.is(MIToken::kw_successors)) {
537  if (parseBasicBlockSuccessors(MBB))
538  return true;
539  } else if (Token.is(MIToken::kw_liveins)) {
540  if (parseBasicBlockLiveins(MBB))
541  return true;
542  } else if (consumeIfPresent(MIToken::Newline)) {
543  continue;
544  } else
545  break;
546  if (!Token.isNewlineOrEOF())
547  return error("expected line break at the end of a list");
548  lex();
549  }
550 
551  // Parse the instructions.
552  bool IsInBundle = false;
553  MachineInstr *PrevMI = nullptr;
554  while (true) {
556  return false;
557  else if (consumeIfPresent(MIToken::Newline))
558  continue;
559  if (consumeIfPresent(MIToken::rbrace)) {
560  // The first parsing pass should verify that all closing '}' have an
561  // opening '{'.
562  assert(IsInBundle);
563  IsInBundle = false;
564  continue;
565  }
566  MachineInstr *MI = nullptr;
567  if (parse(MI))
568  return true;
569  MBB.insert(MBB.end(), MI);
570  if (IsInBundle) {
573  }
574  PrevMI = MI;
575  if (Token.is(MIToken::lbrace)) {
576  if (IsInBundle)
577  return error("nested instruction bundles are not allowed");
578  lex();
579  // This instruction is the start of the bundle.
581  IsInBundle = true;
582  if (!Token.is(MIToken::Newline))
583  // The next instruction can be on the same line.
584  continue;
585  }
586  assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
587  lex();
588  }
589  return false;
590 }
591 
592 bool MIParser::parseBasicBlocks() {
593  lex();
594  // Skip until the first machine basic block.
595  while (Token.is(MIToken::Newline))
596  lex();
597  if (Token.isErrorOrEOF())
598  return Token.isError();
599  // The first parsing pass should have verified that this token is a MBB label
600  // in the 'parseBasicBlockDefinitions' method.
602  do {
603  MachineBasicBlock *MBB = nullptr;
604  if (parseMBBReference(MBB))
605  return true;
606  if (parseBasicBlock(*MBB))
607  return true;
608  // The method 'parseBasicBlock' should parse the whole block until the next
609  // block or the end of file.
611  } while (Token.isNot(MIToken::Eof));
612  return false;
613 }
614 
615 bool MIParser::parse(MachineInstr *&MI) {
616  // Parse any register operands before '='
619  while (Token.isRegister() || Token.isRegisterFlag()) {
620  auto Loc = Token.location();
621  Optional<unsigned> TiedDefIdx;
622  if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
623  return true;
624  Operands.push_back(
625  ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
626  if (Token.isNot(MIToken::comma))
627  break;
628  lex();
629  }
630  if (!Operands.empty() && expectAndConsume(MIToken::equal))
631  return true;
632 
633  unsigned OpCode, Flags = 0;
634  if (Token.isError() || parseInstruction(OpCode, Flags))
635  return true;
636 
637  // Parse the remaining machine operands.
638  while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
639  Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
640  auto Loc = Token.location();
641  Optional<unsigned> TiedDefIdx;
642  if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
643  return true;
644  Operands.push_back(
645  ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
646  if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
648  break;
649  if (Token.isNot(MIToken::comma))
650  return error("expected ',' before the next machine operand");
651  lex();
652  }
653 
654  DebugLoc DebugLocation;
656  lex();
657  if (Token.isNot(MIToken::exclaim))
658  return error("expected a metadata node after 'debug-location'");
659  MDNode *Node = nullptr;
660  if (parseMDNode(Node))
661  return true;
662  DebugLocation = DebugLoc(Node);
663  }
664 
665  // Parse the machine memory operands.
667  if (Token.is(MIToken::coloncolon)) {
668  lex();
669  while (!Token.isNewlineOrEOF()) {
670  MachineMemOperand *MemOp = nullptr;
671  if (parseMachineMemoryOperand(MemOp))
672  return true;
673  MemOperands.push_back(MemOp);
674  if (Token.isNewlineOrEOF())
675  break;
676  if (Token.isNot(MIToken::comma))
677  return error("expected ',' before the next machine memory operand");
678  lex();
679  }
680  }
681 
682  const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
683  if (!MCID.isVariadic()) {
684  // FIXME: Move the implicit operand verification to the machine verifier.
685  if (verifyImplicitOperands(Operands, MCID))
686  return true;
687  }
688 
689  // TODO: Check for extraneous machine operands.
690  MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
691  MI->setFlags(Flags);
692  for (const auto &Operand : Operands)
693  MI->addOperand(MF, Operand.Operand);
694  if (assignRegisterTies(*MI, Operands))
695  return true;
696  if (MemOperands.empty())
697  return false;
699  MF.allocateMemRefsArray(MemOperands.size());
700  std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
701  MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
702  return false;
703 }
704 
705 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
706  lex();
708  return error("expected a machine basic block reference");
709  if (parseMBBReference(MBB))
710  return true;
711  lex();
712  if (Token.isNot(MIToken::Eof))
713  return error(
714  "expected end of string after the machine basic block reference");
715  return false;
716 }
717 
718 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
719  lex();
720  if (Token.isNot(MIToken::NamedRegister))
721  return error("expected a named register");
722  if (parseNamedRegister(Reg))
723  return true;
724  lex();
725  if (Token.isNot(MIToken::Eof))
726  return error("expected end of string after the register reference");
727  return false;
728 }
729 
730 bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
731  lex();
732  if (Token.isNot(MIToken::VirtualRegister))
733  return error("expected a virtual register");
734  if (parseVirtualRegister(Info))
735  return true;
736  lex();
737  if (Token.isNot(MIToken::Eof))
738  return error("expected end of string after the register reference");
739  return false;
740 }
741 
742 bool MIParser::parseStandaloneRegister(unsigned &Reg) {
743  lex();
744  if (Token.isNot(MIToken::NamedRegister) &&
746  return error("expected either a named or virtual register");
747 
748  VRegInfo *Info;
749  if (parseRegister(Reg, Info))
750  return true;
751 
752  lex();
753  if (Token.isNot(MIToken::Eof))
754  return error("expected end of string after the register reference");
755  return false;
756 }
757 
758 bool MIParser::parseStandaloneStackObject(int &FI) {
759  lex();
760  if (Token.isNot(MIToken::StackObject))
761  return error("expected a stack object");
762  if (parseStackFrameIndex(FI))
763  return true;
764  if (Token.isNot(MIToken::Eof))
765  return error("expected end of string after the stack object reference");
766  return false;
767 }
768 
769 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
770  lex();
771  if (Token.isNot(MIToken::exclaim))
772  return error("expected a metadata node");
773  if (parseMDNode(Node))
774  return true;
775  if (Token.isNot(MIToken::Eof))
776  return error("expected end of string after the metadata node");
777  return false;
778 }
779 
780 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
781  assert(MO.isImplicit());
782  return MO.isDef() ? "implicit-def" : "implicit";
783 }
784 
785 static std::string getRegisterName(const TargetRegisterInfo *TRI,
786  unsigned Reg) {
787  assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
788  return StringRef(TRI->getName(Reg)).lower();
789 }
790 
791 /// Return true if the parsed machine operands contain a given machine operand.
792 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
794  for (const auto &I : Operands) {
795  if (ImplicitOperand.isIdenticalTo(I.Operand))
796  return true;
797  }
798  return false;
799 }
800 
801 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
802  const MCInstrDesc &MCID) {
803  if (MCID.isCall())
804  // We can't verify call instructions as they can contain arbitrary implicit
805  // register and register mask operands.
806  return false;
807 
808  // Gather all the expected implicit operands.
809  SmallVector<MachineOperand, 4> ImplicitOperands;
810  if (MCID.ImplicitDefs)
811  for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
812  ImplicitOperands.push_back(
813  MachineOperand::CreateReg(*ImpDefs, true, true));
814  if (MCID.ImplicitUses)
815  for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
816  ImplicitOperands.push_back(
817  MachineOperand::CreateReg(*ImpUses, false, true));
818 
819  const auto *TRI = MF.getSubtarget().getRegisterInfo();
820  assert(TRI && "Expected target register info");
821  for (const auto &I : ImplicitOperands) {
822  if (isImplicitOperandIn(I, Operands))
823  continue;
824  return error(Operands.empty() ? Token.location() : Operands.back().End,
825  Twine("missing implicit register operand '") +
826  printImplicitRegisterFlag(I) + " %" +
827  getRegisterName(TRI, I.getReg()) + "'");
828  }
829  return false;
830 }
831 
832 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
833  if (Token.is(MIToken::kw_frame_setup)) {
834  Flags |= MachineInstr::FrameSetup;
835  lex();
836  }
837  if (Token.isNot(MIToken::Identifier))
838  return error("expected a machine instruction");
839  StringRef InstrName = Token.stringValue();
840  if (parseInstrName(InstrName, OpCode))
841  return error(Twine("unknown machine instruction name '") + InstrName + "'");
842  lex();
843  return false;
844 }
845 
846 bool MIParser::parseNamedRegister(unsigned &Reg) {
847  assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
848  StringRef Name = Token.stringValue();
849  if (getRegisterByName(Name, Reg))
850  return error(Twine("unknown register name '") + Name + "'");
851  return false;
852 }
853 
854 bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
855  assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
856  unsigned ID;
857  if (getUnsigned(ID))
858  return true;
859  Info = &PFS.getVRegInfo(ID);
860  return false;
861 }
862 
863 bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
864  switch (Token.kind()) {
865  case MIToken::underscore:
866  Reg = 0;
867  return false;
869  return parseNamedRegister(Reg);
871  if (parseVirtualRegister(Info))
872  return true;
873  Reg = Info->VReg;
874  return false;
875  // TODO: Parse other register kinds.
876  default:
877  llvm_unreachable("The current token should be a register");
878  }
879 }
880 
881 bool MIParser::parseRegisterFlag(unsigned &Flags) {
882  const unsigned OldFlags = Flags;
883  switch (Token.kind()) {
885  Flags |= RegState::Implicit;
886  break;
888  Flags |= RegState::ImplicitDefine;
889  break;
890  case MIToken::kw_def:
891  Flags |= RegState::Define;
892  break;
893  case MIToken::kw_dead:
894  Flags |= RegState::Dead;
895  break;
896  case MIToken::kw_killed:
897  Flags |= RegState::Kill;
898  break;
899  case MIToken::kw_undef:
900  Flags |= RegState::Undef;
901  break;
903  Flags |= RegState::InternalRead;
904  break;
906  Flags |= RegState::EarlyClobber;
907  break;
909  Flags |= RegState::Debug;
910  break;
911  default:
912  llvm_unreachable("The current token should be a register flag");
913  }
914  if (OldFlags == Flags)
915  // We know that the same flag is specified more than once when the flags
916  // weren't modified.
917  return error("duplicate '" + Token.stringValue() + "' register flag");
918  lex();
919  return false;
920 }
921 
922 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
924  lex();
925  if (Token.isNot(MIToken::Identifier))
926  return error("expected a subregister index after '.'");
927  auto Name = Token.stringValue();
928  SubReg = getSubRegIndex(Name);
929  if (!SubReg)
930  return error(Twine("use of unknown subregister index '") + Name + "'");
931  lex();
932  return false;
933 }
934 
935 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
936  if (!consumeIfPresent(MIToken::kw_tied_def))
937  return true;
938  if (Token.isNot(MIToken::IntegerLiteral))
939  return error("expected an integer literal after 'tied-def'");
940  if (getUnsigned(TiedDefIdx))
941  return true;
942  lex();
943  if (expectAndConsume(MIToken::rparen))
944  return true;
945  return false;
946 }
947 
948 bool MIParser::assignRegisterTies(MachineInstr &MI,
950  SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
951  for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
952  if (!Operands[I].TiedDefIdx)
953  continue;
954  // The parser ensures that this operand is a register use, so we just have
955  // to check the tied-def operand.
956  unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
957  if (DefIdx >= E)
958  return error(Operands[I].Begin,
959  Twine("use of invalid tied-def operand index '" +
960  Twine(DefIdx) + "'; instruction has only ") +
961  Twine(E) + " operands");
962  const auto &DefOperand = Operands[DefIdx].Operand;
963  if (!DefOperand.isReg() || !DefOperand.isDef())
964  // FIXME: add note with the def operand.
965  return error(Operands[I].Begin,
966  Twine("use of invalid tied-def operand index '") +
967  Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
968  " isn't a defined register");
969  // Check that the tied-def operand wasn't tied elsewhere.
970  for (const auto &TiedPair : TiedRegisterPairs) {
971  if (TiedPair.first == DefIdx)
972  return error(Operands[I].Begin,
973  Twine("the tied-def operand #") + Twine(DefIdx) +
974  " is already tied with another register operand");
975  }
976  TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
977  }
978  // FIXME: Verify that for non INLINEASM instructions, the def and use tied
979  // indices must be less than tied max.
980  for (const auto &TiedPair : TiedRegisterPairs)
981  MI.tieOperands(TiedPair.first, TiedPair.second);
982  return false;
983 }
984 
985 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
986  Optional<unsigned> &TiedDefIdx,
987  bool IsDef) {
988  unsigned Flags = IsDef ? RegState::Define : 0;
989  while (Token.isRegisterFlag()) {
990  if (parseRegisterFlag(Flags))
991  return true;
992  }
993  if (!Token.isRegister())
994  return error("expected a register after register flags");
995  unsigned Reg;
996  VRegInfo *RegInfo;
997  if (parseRegister(Reg, RegInfo))
998  return true;
999  lex();
1000  unsigned SubReg = 0;
1001  if (Token.is(MIToken::dot)) {
1002  if (parseSubRegisterIndex(SubReg))
1003  return true;
1005  return error("subregister index expects a virtual register");
1006  }
1007  MachineRegisterInfo &MRI = MF.getRegInfo();
1008  if ((Flags & RegState::Define) == 0) {
1009  if (consumeIfPresent(MIToken::lparen)) {
1010  unsigned Idx;
1011  if (!parseRegisterTiedDefIndex(Idx))
1012  TiedDefIdx = Idx;
1013  else {
1014  // Try a redundant low-level type.
1015  LLT Ty;
1016  if (parseLowLevelType(Token.location(), Ty))
1017  return error("expected tied-def or low-level type after '('");
1018 
1019  if (expectAndConsume(MIToken::rparen))
1020  return true;
1021 
1022  if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1023  return error("inconsistent type for generic virtual register");
1024 
1025  MRI.setType(Reg, Ty);
1026  }
1027  }
1028  } else if (consumeIfPresent(MIToken::lparen)) {
1029  // Virtual registers may have a tpe with GlobalISel.
1031  return error("unexpected type on physical register");
1032 
1033  LLT Ty;
1034  if (parseLowLevelType(Token.location(), Ty))
1035  return true;
1036 
1037  if (expectAndConsume(MIToken::rparen))
1038  return true;
1039 
1040  if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1041  return error("inconsistent type for generic virtual register");
1042 
1043  MRI.setType(Reg, Ty);
1044  } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1045  // Generic virtual registers must have a type.
1046  // If we end up here this means the type hasn't been specified and
1047  // this is bad!
1048  if (RegInfo->Kind == VRegInfo::GENERIC ||
1049  RegInfo->Kind == VRegInfo::REGBANK)
1050  return error("generic virtual registers must have a type");
1051  }
1053  Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1054  Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1055  Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1056  Flags & RegState::InternalRead);
1057  return false;
1058 }
1059 
1060 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1062  const APSInt &Int = Token.integerValue();
1063  if (Int.getMinSignedBits() > 64)
1064  return error("integer literal is too large to be an immediate operand");
1065  Dest = MachineOperand::CreateImm(Int.getExtValue());
1066  lex();
1067  return false;
1068 }
1069 
1070 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1071  const Constant *&C) {
1072  auto Source = StringValue.str(); // The source has to be null terminated.
1073  SMDiagnostic Err;
1074  C = parseConstantValue(Source, Err, *MF.getFunction()->getParent(),
1075  &PFS.IRSlots);
1076  if (!C)
1077  return error(Loc + Err.getColumnNo(), Err.getMessage());
1078  return false;
1079 }
1080 
1081 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1082  if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1083  return true;
1084  lex();
1085  return false;
1086 }
1087 
1088 bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1089  if (Token.is(MIToken::ScalarType)) {
1090  Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1091  lex();
1092  return false;
1093  } else if (Token.is(MIToken::PointerType)) {
1094  const DataLayout &DL = MF.getFunction()->getParent()->getDataLayout();
1095  unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1096  Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1097  lex();
1098  return false;
1099  }
1100 
1101  // Now we're looking for a vector.
1102  if (Token.isNot(MIToken::less))
1103  return error(Loc,
1104  "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1105 
1106  lex();
1107 
1108  if (Token.isNot(MIToken::IntegerLiteral))
1109  return error(Loc, "expected <N x sM> for vctor type");
1110  uint64_t NumElements = Token.integerValue().getZExtValue();
1111  lex();
1112 
1113  if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1114  return error(Loc, "expected '<N x sM>' for vector type");
1115  lex();
1116 
1117  if (Token.isNot(MIToken::ScalarType))
1118  return error(Loc, "expected '<N x sM>' for vector type");
1119  uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1120  lex();
1121 
1122  if (Token.isNot(MIToken::greater))
1123  return error(Loc, "expected '<N x sM>' for vector type");
1124  lex();
1125 
1126  Ty = LLT::vector(NumElements, ScalarSize);
1127  return false;
1128 }
1129 
1130 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1132  auto Loc = Token.location();
1133  lex();
1134  if (Token.isNot(MIToken::IntegerLiteral))
1135  return error("expected an integer literal");
1136  const Constant *C = nullptr;
1137  if (parseIRConstant(Loc, C))
1138  return true;
1139  Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1140  return false;
1141 }
1142 
1143 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1144  auto Loc = Token.location();
1145  lex();
1146  if (Token.isNot(MIToken::FloatingPointLiteral) &&
1147  Token.isNot(MIToken::HexLiteral))
1148  return error("expected a floating point literal");
1149  const Constant *C = nullptr;
1150  if (parseIRConstant(Loc, C))
1151  return true;
1152  Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1153  return false;
1154 }
1155 
1156 bool MIParser::getUnsigned(unsigned &Result) {
1157  if (Token.hasIntegerValue()) {
1158  const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1159  uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1160  if (Val64 == Limit)
1161  return error("expected 32-bit integer (too large)");
1162  Result = Val64;
1163  return false;
1164  }
1165  if (Token.is(MIToken::HexLiteral)) {
1166  APInt A;
1167  if (getHexUint(A))
1168  return true;
1169  if (A.getBitWidth() > 32)
1170  return error("expected 32-bit integer (too large)");
1171  Result = A.getZExtValue();
1172  return false;
1173  }
1174  return true;
1175 }
1176 
1180  unsigned Number;
1181  if (getUnsigned(Number))
1182  return true;
1183  auto MBBInfo = PFS.MBBSlots.find(Number);
1184  if (MBBInfo == PFS.MBBSlots.end())
1185  return error(Twine("use of undefined machine basic block #") +
1186  Twine(Number));
1187  MBB = MBBInfo->second;
1188  if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1189  return error(Twine("the name of machine basic block #") + Twine(Number) +
1190  " isn't '" + Token.stringValue() + "'");
1191  return false;
1192 }
1193 
1194 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1196  if (parseMBBReference(MBB))
1197  return true;
1198  Dest = MachineOperand::CreateMBB(MBB);
1199  lex();
1200  return false;
1201 }
1202 
1203 bool MIParser::parseStackFrameIndex(int &FI) {
1205  unsigned ID;
1206  if (getUnsigned(ID))
1207  return true;
1208  auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1209  if (ObjectInfo == PFS.StackObjectSlots.end())
1210  return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1211  "'");
1212  StringRef Name;
1213  if (const auto *Alloca =
1214  MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
1215  Name = Alloca->getName();
1216  if (!Token.stringValue().empty() && Token.stringValue() != Name)
1217  return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1218  "' isn't '" + Token.stringValue() + "'");
1219  lex();
1220  FI = ObjectInfo->second;
1221  return false;
1222 }
1223 
1224 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1225  int FI;
1226  if (parseStackFrameIndex(FI))
1227  return true;
1228  Dest = MachineOperand::CreateFI(FI);
1229  return false;
1230 }
1231 
1232 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1234  unsigned ID;
1235  if (getUnsigned(ID))
1236  return true;
1237  auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1238  if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1239  return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1240  Twine(ID) + "'");
1241  lex();
1242  FI = ObjectInfo->second;
1243  return false;
1244 }
1245 
1246 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1247  int FI;
1248  if (parseFixedStackFrameIndex(FI))
1249  return true;
1250  Dest = MachineOperand::CreateFI(FI);
1251  return false;
1252 }
1253 
1254 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1255  switch (Token.kind()) {
1257  const Module *M = MF.getFunction()->getParent();
1258  GV = M->getNamedValue(Token.stringValue());
1259  if (!GV)
1260  return error(Twine("use of undefined global value '") + Token.range() +
1261  "'");
1262  break;
1263  }
1264  case MIToken::GlobalValue: {
1265  unsigned GVIdx;
1266  if (getUnsigned(GVIdx))
1267  return true;
1268  if (GVIdx >= PFS.IRSlots.GlobalValues.size())
1269  return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1270  "'");
1271  GV = PFS.IRSlots.GlobalValues[GVIdx];
1272  break;
1273  }
1274  default:
1275  llvm_unreachable("The current token should be a global value");
1276  }
1277  return false;
1278 }
1279 
1280 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1281  GlobalValue *GV = nullptr;
1282  if (parseGlobalValue(GV))
1283  return true;
1284  lex();
1285  Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1286  if (parseOperandsOffset(Dest))
1287  return true;
1288  return false;
1289 }
1290 
1291 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1293  unsigned ID;
1294  if (getUnsigned(ID))
1295  return true;
1296  auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1297  if (ConstantInfo == PFS.ConstantPoolSlots.end())
1298  return error("use of undefined constant '%const." + Twine(ID) + "'");
1299  lex();
1300  Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1301  if (parseOperandsOffset(Dest))
1302  return true;
1303  return false;
1304 }
1305 
1306 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1308  unsigned ID;
1309  if (getUnsigned(ID))
1310  return true;
1311  auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1312  if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1313  return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1314  lex();
1315  Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1316  return false;
1317 }
1318 
1319 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1321  const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1322  lex();
1323  Dest = MachineOperand::CreateES(Symbol);
1324  if (parseOperandsOffset(Dest))
1325  return true;
1326  return false;
1327 }
1328 
1329 bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1331  StringRef Name = Token.stringValue();
1332  unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1333  if (SubRegIndex == 0)
1334  return error(Twine("unknown subregister index '") + Name + "'");
1335  lex();
1336  Dest = MachineOperand::CreateImm(SubRegIndex);
1337  return false;
1338 }
1339 
1340 bool MIParser::parseMDNode(MDNode *&Node) {
1342  auto Loc = Token.location();
1343  lex();
1344  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1345  return error("expected metadata id after '!'");
1346  unsigned ID;
1347  if (getUnsigned(ID))
1348  return true;
1349  auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1350  if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
1351  return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1352  lex();
1353  Node = NodeInfo->second.get();
1354  return false;
1355 }
1356 
1357 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1358  MDNode *Node = nullptr;
1359  if (parseMDNode(Node))
1360  return true;
1361  Dest = MachineOperand::CreateMetadata(Node);
1362  return false;
1363 }
1364 
1365 bool MIParser::parseCFIOffset(int &Offset) {
1366  if (Token.isNot(MIToken::IntegerLiteral))
1367  return error("expected a cfi offset");
1368  if (Token.integerValue().getMinSignedBits() > 32)
1369  return error("expected a 32 bit integer (the cfi offset is too large)");
1370  Offset = (int)Token.integerValue().getExtValue();
1371  lex();
1372  return false;
1373 }
1374 
1375 bool MIParser::parseCFIRegister(unsigned &Reg) {
1376  if (Token.isNot(MIToken::NamedRegister))
1377  return error("expected a cfi register");
1378  unsigned LLVMReg;
1379  if (parseNamedRegister(LLVMReg))
1380  return true;
1381  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1382  assert(TRI && "Expected target register info");
1383  int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1384  if (DwarfReg < 0)
1385  return error("invalid DWARF register");
1386  Reg = (unsigned)DwarfReg;
1387  lex();
1388  return false;
1389 }
1390 
1391 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1392  auto Kind = Token.kind();
1393  lex();
1394  int Offset;
1395  unsigned Reg;
1396  unsigned CFIIndex;
1397  switch (Kind) {
1399  if (parseCFIRegister(Reg))
1400  return true;
1401  CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1402  break;
1404  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1405  parseCFIOffset(Offset))
1406  return true;
1407  CFIIndex =
1408  MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1409  break;
1411  if (parseCFIRegister(Reg))
1412  return true;
1413  CFIIndex =
1414  MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1415  break;
1417  if (parseCFIOffset(Offset))
1418  return true;
1419  // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1420  CFIIndex = MF.addFrameInst(
1421  MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1422  break;
1424  if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1425  parseCFIOffset(Offset))
1426  return true;
1427  // NB: MCCFIInstruction::createDefCfa negates the offset.
1428  CFIIndex =
1429  MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1430  break;
1431  default:
1432  // TODO: Parse the other CFI operands.
1433  llvm_unreachable("The current token should be a cfi operand");
1434  }
1435  Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1436  return false;
1437 }
1438 
1439 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1440  switch (Token.kind()) {
1441  case MIToken::NamedIRBlock: {
1442  BB = dyn_cast_or_null<BasicBlock>(
1443  F.getValueSymbolTable()->lookup(Token.stringValue()));
1444  if (!BB)
1445  return error(Twine("use of undefined IR block '") + Token.range() + "'");
1446  break;
1447  }
1448  case MIToken::IRBlock: {
1449  unsigned SlotNumber = 0;
1450  if (getUnsigned(SlotNumber))
1451  return true;
1452  BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1453  if (!BB)
1454  return error(Twine("use of undefined IR block '%ir-block.") +
1455  Twine(SlotNumber) + "'");
1456  break;
1457  }
1458  default:
1459  llvm_unreachable("The current token should be an IR block reference");
1460  }
1461  return false;
1462 }
1463 
1464 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1466  lex();
1467  if (expectAndConsume(MIToken::lparen))
1468  return true;
1469  if (Token.isNot(MIToken::GlobalValue) &&
1471  return error("expected a global value");
1472  GlobalValue *GV = nullptr;
1473  if (parseGlobalValue(GV))
1474  return true;
1475  auto *F = dyn_cast<Function>(GV);
1476  if (!F)
1477  return error("expected an IR function reference");
1478  lex();
1479  if (expectAndConsume(MIToken::comma))
1480  return true;
1481  BasicBlock *BB = nullptr;
1482  if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1483  return error("expected an IR block reference");
1484  if (parseIRBlock(BB, *F))
1485  return true;
1486  lex();
1487  if (expectAndConsume(MIToken::rparen))
1488  return true;
1489  Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1490  if (parseOperandsOffset(Dest))
1491  return true;
1492  return false;
1493 }
1494 
1495 bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1497  lex();
1498  if (expectAndConsume(MIToken::lparen))
1499  return error("expected syntax intrinsic(@llvm.whatever)");
1500 
1501  if (Token.isNot(MIToken::NamedGlobalValue))
1502  return error("expected syntax intrinsic(@llvm.whatever)");
1503 
1504  std::string Name = Token.stringValue();
1505  lex();
1506 
1507  if (expectAndConsume(MIToken::rparen))
1508  return error("expected ')' to terminate intrinsic name");
1509 
1510  // Find out what intrinsic we're dealing with, first try the global namespace
1511  // and then the target's private intrinsics if that fails.
1512  const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1514  if (ID == Intrinsic::not_intrinsic && TII)
1515  ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1516 
1517  if (ID == Intrinsic::not_intrinsic)
1518  return error("unknown intrinsic name");
1520 
1521  return false;
1522 }
1523 
1524 bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1526  bool IsFloat = Token.is(MIToken::kw_floatpred);
1527  lex();
1528 
1529  if (expectAndConsume(MIToken::lparen))
1530  return error("expected syntax intpred(whatever) or floatpred(whatever");
1531 
1532  if (Token.isNot(MIToken::Identifier))
1533  return error("whatever");
1534 
1535  CmpInst::Predicate Pred;
1536  if (IsFloat) {
1537  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1538  .Case("false", CmpInst::FCMP_FALSE)
1539  .Case("oeq", CmpInst::FCMP_OEQ)
1540  .Case("ogt", CmpInst::FCMP_OGT)
1541  .Case("oge", CmpInst::FCMP_OGE)
1542  .Case("olt", CmpInst::FCMP_OLT)
1543  .Case("ole", CmpInst::FCMP_OLE)
1544  .Case("one", CmpInst::FCMP_ONE)
1545  .Case("ord", CmpInst::FCMP_ORD)
1546  .Case("uno", CmpInst::FCMP_UNO)
1547  .Case("ueq", CmpInst::FCMP_UEQ)
1548  .Case("ugt", CmpInst::FCMP_UGT)
1549  .Case("uge", CmpInst::FCMP_UGE)
1550  .Case("ult", CmpInst::FCMP_ULT)
1551  .Case("ule", CmpInst::FCMP_ULE)
1552  .Case("une", CmpInst::FCMP_UNE)
1553  .Case("true", CmpInst::FCMP_TRUE)
1555  if (!CmpInst::isFPPredicate(Pred))
1556  return error("invalid floating-point predicate");
1557  } else {
1558  Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1559  .Case("eq", CmpInst::ICMP_EQ)
1560  .Case("ne", CmpInst::ICMP_NE)
1561  .Case("sgt", CmpInst::ICMP_SGT)
1562  .Case("sge", CmpInst::ICMP_SGE)
1563  .Case("slt", CmpInst::ICMP_SLT)
1564  .Case("sle", CmpInst::ICMP_SLE)
1565  .Case("ugt", CmpInst::ICMP_UGT)
1566  .Case("uge", CmpInst::ICMP_UGE)
1567  .Case("ult", CmpInst::ICMP_ULT)
1568  .Case("ule", CmpInst::ICMP_ULE)
1570  if (!CmpInst::isIntPredicate(Pred))
1571  return error("invalid integer predicate");
1572  }
1573 
1574  lex();
1575  Dest = MachineOperand::CreatePredicate(Pred);
1576  if (expectAndConsume(MIToken::rparen))
1577  return error("predicate should be terminated by ')'.");
1578 
1579  return false;
1580 }
1581 
1582 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1584  lex();
1585  if (expectAndConsume(MIToken::lparen))
1586  return true;
1587  if (Token.isNot(MIToken::Identifier))
1588  return error("expected the name of the target index");
1589  int Index = 0;
1590  if (getTargetIndex(Token.stringValue(), Index))
1591  return error("use of undefined target index '" + Token.stringValue() + "'");
1592  lex();
1593  if (expectAndConsume(MIToken::rparen))
1594  return true;
1595  Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1596  if (parseOperandsOffset(Dest))
1597  return true;
1598  return false;
1599 }
1600 
1601 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1603  const auto *TRI = MF.getSubtarget().getRegisterInfo();
1604  assert(TRI && "Expected target register info");
1605  uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1606  lex();
1607  if (expectAndConsume(MIToken::lparen))
1608  return true;
1609  while (true) {
1610  if (Token.isNot(MIToken::NamedRegister))
1611  return error("expected a named register");
1612  unsigned Reg;
1613  if (parseNamedRegister(Reg))
1614  return true;
1615  lex();
1616  Mask[Reg / 32] |= 1U << (Reg % 32);
1617  // TODO: Report an error if the same register is used more than once.
1618  if (Token.isNot(MIToken::comma))
1619  break;
1620  lex();
1621  }
1622  if (expectAndConsume(MIToken::rparen))
1623  return true;
1624  Dest = MachineOperand::CreateRegLiveOut(Mask);
1625  return false;
1626 }
1627 
1628 bool MIParser::parseMachineOperand(MachineOperand &Dest,
1629  Optional<unsigned> &TiedDefIdx) {
1630  switch (Token.kind()) {
1631  case MIToken::kw_implicit:
1633  case MIToken::kw_def:
1634  case MIToken::kw_dead:
1635  case MIToken::kw_killed:
1636  case MIToken::kw_undef:
1637  case MIToken::kw_internal:
1639  case MIToken::kw_debug_use:
1640  case MIToken::underscore:
1643  return parseRegisterOperand(Dest, TiedDefIdx);
1645  return parseImmediateOperand(Dest);
1646  case MIToken::IntegerType:
1647  return parseTypedImmediateOperand(Dest);
1648  case MIToken::kw_half:
1649  case MIToken::kw_float:
1650  case MIToken::kw_double:
1651  case MIToken::kw_x86_fp80:
1652  case MIToken::kw_fp128:
1653  case MIToken::kw_ppc_fp128:
1654  return parseFPImmediateOperand(Dest);
1656  return parseMBBOperand(Dest);
1657  case MIToken::StackObject:
1658  return parseStackObjectOperand(Dest);
1660  return parseFixedStackObjectOperand(Dest);
1661  case MIToken::GlobalValue:
1663  return parseGlobalAddressOperand(Dest);
1665  return parseConstantPoolIndexOperand(Dest);
1667  return parseJumpTableIndexOperand(Dest);
1669  return parseExternalSymbolOperand(Dest);
1671  return parseSubRegisterIndexOperand(Dest);
1672  case MIToken::exclaim:
1673  return parseMetadataOperand(Dest);
1679  return parseCFIOperand(Dest);
1681  return parseBlockAddressOperand(Dest);
1682  case MIToken::kw_intrinsic:
1683  return parseIntrinsicOperand(Dest);
1685  return parseTargetIndexOperand(Dest);
1686  case MIToken::kw_liveout:
1687  return parseLiveoutRegisterMaskOperand(Dest);
1688  case MIToken::kw_floatpred:
1689  case MIToken::kw_intpred:
1690  return parsePredicateOperand(Dest);
1691  case MIToken::Error:
1692  return true;
1693  case MIToken::Identifier:
1694  if (const auto *RegMask = getRegMask(Token.stringValue())) {
1695  Dest = MachineOperand::CreateRegMask(RegMask);
1696  lex();
1697  break;
1698  }
1700  default:
1701  // FIXME: Parse the MCSymbol machine operand.
1702  return error("expected a machine operand");
1703  }
1704  return false;
1705 }
1706 
1707 bool MIParser::parseMachineOperandAndTargetFlags(
1708  MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1709  unsigned TF = 0;
1710  bool HasTargetFlags = false;
1711  if (Token.is(MIToken::kw_target_flags)) {
1712  HasTargetFlags = true;
1713  lex();
1714  if (expectAndConsume(MIToken::lparen))
1715  return true;
1716  if (Token.isNot(MIToken::Identifier))
1717  return error("expected the name of the target flag");
1718  if (getDirectTargetFlag(Token.stringValue(), TF)) {
1719  if (getBitmaskTargetFlag(Token.stringValue(), TF))
1720  return error("use of undefined target flag '" + Token.stringValue() +
1721  "'");
1722  }
1723  lex();
1724  while (Token.is(MIToken::comma)) {
1725  lex();
1726  if (Token.isNot(MIToken::Identifier))
1727  return error("expected the name of the target flag");
1728  unsigned BitFlag = 0;
1729  if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1730  return error("use of undefined target flag '" + Token.stringValue() +
1731  "'");
1732  // TODO: Report an error when using a duplicate bit target flag.
1733  TF |= BitFlag;
1734  lex();
1735  }
1736  if (expectAndConsume(MIToken::rparen))
1737  return true;
1738  }
1739  auto Loc = Token.location();
1740  if (parseMachineOperand(Dest, TiedDefIdx))
1741  return true;
1742  if (!HasTargetFlags)
1743  return false;
1744  if (Dest.isReg())
1745  return error(Loc, "register operands can't have target flags");
1746  Dest.setTargetFlags(TF);
1747  return false;
1748 }
1749 
1750 bool MIParser::parseOffset(int64_t &Offset) {
1751  if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1752  return false;
1753  StringRef Sign = Token.range();
1754  bool IsNegative = Token.is(MIToken::minus);
1755  lex();
1756  if (Token.isNot(MIToken::IntegerLiteral))
1757  return error("expected an integer literal after '" + Sign + "'");
1758  if (Token.integerValue().getMinSignedBits() > 64)
1759  return error("expected 64-bit integer (too large)");
1760  Offset = Token.integerValue().getExtValue();
1761  if (IsNegative)
1762  Offset = -Offset;
1763  lex();
1764  return false;
1765 }
1766 
1767 bool MIParser::parseAlignment(unsigned &Alignment) {
1769  lex();
1770  if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1771  return error("expected an integer literal after 'align'");
1772  if (getUnsigned(Alignment))
1773  return true;
1774  lex();
1775  return false;
1776 }
1777 
1778 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1779  int64_t Offset = 0;
1780  if (parseOffset(Offset))
1781  return true;
1782  Op.setOffset(Offset);
1783  return false;
1784 }
1785 
1786 bool MIParser::parseIRValue(const Value *&V) {
1787  switch (Token.kind()) {
1788  case MIToken::NamedIRValue: {
1789  V = MF.getFunction()->getValueSymbolTable()->lookup(Token.stringValue());
1790  break;
1791  }
1792  case MIToken::IRValue: {
1793  unsigned SlotNumber = 0;
1794  if (getUnsigned(SlotNumber))
1795  return true;
1796  V = getIRValue(SlotNumber);
1797  break;
1798  }
1800  case MIToken::GlobalValue: {
1801  GlobalValue *GV = nullptr;
1802  if (parseGlobalValue(GV))
1803  return true;
1804  V = GV;
1805  break;
1806  }
1807  case MIToken::QuotedIRValue: {
1808  const Constant *C = nullptr;
1809  if (parseIRConstant(Token.location(), Token.stringValue(), C))
1810  return true;
1811  V = C;
1812  break;
1813  }
1814  default:
1815  llvm_unreachable("The current token should be an IR block reference");
1816  }
1817  if (!V)
1818  return error(Twine("use of undefined IR value '") + Token.range() + "'");
1819  return false;
1820 }
1821 
1822 bool MIParser::getUint64(uint64_t &Result) {
1823  if (Token.hasIntegerValue()) {
1824  if (Token.integerValue().getActiveBits() > 64)
1825  return error("expected 64-bit integer (too large)");
1826  Result = Token.integerValue().getZExtValue();
1827  return false;
1828  }
1829  if (Token.is(MIToken::HexLiteral)) {
1830  APInt A;
1831  if (getHexUint(A))
1832  return true;
1833  if (A.getBitWidth() > 64)
1834  return error("expected 64-bit integer (too large)");
1835  Result = A.getZExtValue();
1836  return false;
1837  }
1838  return true;
1839 }
1840 
1841 bool MIParser::getHexUint(APInt &Result) {
1843  StringRef S = Token.range();
1844  assert(S[0] == '0' && tolower(S[1]) == 'x');
1845  // This could be a floating point literal with a special prefix.
1846  if (!isxdigit(S[2]))
1847  return true;
1848  StringRef V = S.substr(2);
1849  APInt A(V.size()*4, V, 16);
1850  Result = APInt(A.getActiveBits(),
1852  return false;
1853 }
1854 
1855 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
1856  const auto OldFlags = Flags;
1857  switch (Token.kind()) {
1858  case MIToken::kw_volatile:
1860  break;
1863  break;
1866  break;
1867  case MIToken::kw_invariant:
1869  break;
1870  // TODO: parse the target specific memory operand flags.
1871  default:
1872  llvm_unreachable("The current token should be a memory operand flag");
1873  }
1874  if (OldFlags == Flags)
1875  // We know that the same flag is specified more than once when the flags
1876  // weren't modified.
1877  return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1878  lex();
1879  return false;
1880 }
1881 
1882 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1883  switch (Token.kind()) {
1884  case MIToken::kw_stack:
1885  PSV = MF.getPSVManager().getStack();
1886  break;
1887  case MIToken::kw_got:
1888  PSV = MF.getPSVManager().getGOT();
1889  break;
1891  PSV = MF.getPSVManager().getJumpTable();
1892  break;
1894  PSV = MF.getPSVManager().getConstantPool();
1895  break;
1897  int FI;
1898  if (parseFixedStackFrameIndex(FI))
1899  return true;
1900  PSV = MF.getPSVManager().getFixedStack(FI);
1901  // The token was already consumed, so use return here instead of break.
1902  return false;
1903  }
1904  case MIToken::StackObject: {
1905  int FI;
1906  if (parseStackFrameIndex(FI))
1907  return true;
1908  PSV = MF.getPSVManager().getFixedStack(FI);
1909  // The token was already consumed, so use return here instead of break.
1910  return false;
1911  }
1912  case MIToken::kw_call_entry: {
1913  lex();
1914  switch (Token.kind()) {
1915  case MIToken::GlobalValue:
1917  GlobalValue *GV = nullptr;
1918  if (parseGlobalValue(GV))
1919  return true;
1920  PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1921  break;
1922  }
1924  PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1925  MF.createExternalSymbolName(Token.stringValue()));
1926  break;
1927  default:
1928  return error(
1929  "expected a global value or an external symbol after 'call-entry'");
1930  }
1931  break;
1932  }
1933  default:
1934  llvm_unreachable("The current token should be pseudo source value");
1935  }
1936  lex();
1937  return false;
1938 }
1939 
1940 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1945  const PseudoSourceValue *PSV = nullptr;
1946  if (parseMemoryPseudoSourceValue(PSV))
1947  return true;
1948  int64_t Offset = 0;
1949  if (parseOffset(Offset))
1950  return true;
1951  Dest = MachinePointerInfo(PSV, Offset);
1952  return false;
1953  }
1954  if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1955  Token.isNot(MIToken::GlobalValue) &&
1958  return error("expected an IR value reference");
1959  const Value *V = nullptr;
1960  if (parseIRValue(V))
1961  return true;
1962  if (!V->getType()->isPointerTy())
1963  return error("expected a pointer IR value");
1964  lex();
1965  int64_t Offset = 0;
1966  if (parseOffset(Offset))
1967  return true;
1968  Dest = MachinePointerInfo(V, Offset);
1969  return false;
1970 }
1971 
1972 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1973  if (expectAndConsume(MIToken::lparen))
1974  return true;
1976  while (Token.isMemoryOperandFlag()) {
1977  if (parseMemoryOperandFlag(Flags))
1978  return true;
1979  }
1980  if (Token.isNot(MIToken::Identifier) ||
1981  (Token.stringValue() != "load" && Token.stringValue() != "store"))
1982  return error("expected 'load' or 'store' memory operation");
1983  if (Token.stringValue() == "load")
1984  Flags |= MachineMemOperand::MOLoad;
1985  else
1986  Flags |= MachineMemOperand::MOStore;
1987  lex();
1988 
1989  if (Token.isNot(MIToken::IntegerLiteral))
1990  return error("expected the size integer literal after memory operation");
1991  uint64_t Size;
1992  if (getUint64(Size))
1993  return true;
1994  lex();
1995 
1997  if (Token.is(MIToken::Identifier)) {
1998  const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1999  if (Token.stringValue() != Word)
2000  return error(Twine("expected '") + Word + "'");
2001  lex();
2002 
2003  if (parseMachinePointerInfo(Ptr))
2004  return true;
2005  }
2006  unsigned BaseAlignment = Size;
2007  AAMDNodes AAInfo;
2008  MDNode *Range = nullptr;
2009  while (consumeIfPresent(MIToken::comma)) {
2010  switch (Token.kind()) {
2011  case MIToken::kw_align:
2012  if (parseAlignment(BaseAlignment))
2013  return true;
2014  break;
2015  case MIToken::md_tbaa:
2016  lex();
2017  if (parseMDNode(AAInfo.TBAA))
2018  return true;
2019  break;
2021  lex();
2022  if (parseMDNode(AAInfo.Scope))
2023  return true;
2024  break;
2025  case MIToken::md_noalias:
2026  lex();
2027  if (parseMDNode(AAInfo.NoAlias))
2028  return true;
2029  break;
2030  case MIToken::md_range:
2031  lex();
2032  if (parseMDNode(Range))
2033  return true;
2034  break;
2035  // TODO: Report an error on duplicate metadata nodes.
2036  default:
2037  return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2038  "'!noalias' or '!range'");
2039  }
2040  }
2041  if (expectAndConsume(MIToken::rparen))
2042  return true;
2043  Dest =
2044  MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
2045  return false;
2046 }
2047 
2048 void MIParser::initNames2InstrOpCodes() {
2049  if (!Names2InstrOpCodes.empty())
2050  return;
2051  const auto *TII = MF.getSubtarget().getInstrInfo();
2052  assert(TII && "Expected target instruction info");
2053  for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2054  Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2055 }
2056 
2057 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2058  initNames2InstrOpCodes();
2059  auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2060  if (InstrInfo == Names2InstrOpCodes.end())
2061  return true;
2062  OpCode = InstrInfo->getValue();
2063  return false;
2064 }
2065 
2066 void MIParser::initNames2Regs() {
2067  if (!Names2Regs.empty())
2068  return;
2069  // The '%noreg' register is the register 0.
2070  Names2Regs.insert(std::make_pair("noreg", 0));
2071  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2072  assert(TRI && "Expected target register info");
2073  for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2074  bool WasInserted =
2075  Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2076  .second;
2077  (void)WasInserted;
2078  assert(WasInserted && "Expected registers to be unique case-insensitively");
2079  }
2080 }
2081 
2082 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2083  initNames2Regs();
2084  auto RegInfo = Names2Regs.find(RegName);
2085  if (RegInfo == Names2Regs.end())
2086  return true;
2087  Reg = RegInfo->getValue();
2088  return false;
2089 }
2090 
2091 void MIParser::initNames2RegMasks() {
2092  if (!Names2RegMasks.empty())
2093  return;
2094  const auto *TRI = MF.getSubtarget().getRegisterInfo();
2095  assert(TRI && "Expected target register info");
2096  ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2097  ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2098  assert(RegMasks.size() == RegMaskNames.size());
2099  for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2100  Names2RegMasks.insert(
2101  std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2102 }
2103 
2104 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2105  initNames2RegMasks();
2106  auto RegMaskInfo = Names2RegMasks.find(Identifier);
2107  if (RegMaskInfo == Names2RegMasks.end())
2108  return nullptr;
2109  return RegMaskInfo->getValue();
2110 }
2111 
2112 void MIParser::initNames2SubRegIndices() {
2113  if (!Names2SubRegIndices.empty())
2114  return;
2115  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2116  for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2117  Names2SubRegIndices.insert(
2118  std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2119 }
2120 
2121 unsigned MIParser::getSubRegIndex(StringRef Name) {
2122  initNames2SubRegIndices();
2123  auto SubRegInfo = Names2SubRegIndices.find(Name);
2124  if (SubRegInfo == Names2SubRegIndices.end())
2125  return 0;
2126  return SubRegInfo->getValue();
2127 }
2128 
2130  const Function &F,
2131  DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2132  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2133  MST.incorporateFunction(F);
2134  for (auto &BB : F) {
2135  if (BB.hasName())
2136  continue;
2137  int Slot = MST.getLocalSlot(&BB);
2138  if (Slot == -1)
2139  continue;
2140  Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2141  }
2142 }
2143 
2145  unsigned Slot,
2146  const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2147  auto BlockInfo = Slots2BasicBlocks.find(Slot);
2148  if (BlockInfo == Slots2BasicBlocks.end())
2149  return nullptr;
2150  return BlockInfo->second;
2151 }
2152 
2153 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2154  if (Slots2BasicBlocks.empty())
2155  initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
2156  return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2157 }
2158 
2159 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2160  if (&F == MF.getFunction())
2161  return getIRBlock(Slot);
2162  DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2163  initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2164  return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2165 }
2166 
2167 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2168  DenseMap<unsigned, const Value *> &Slots2Values) {
2169  int Slot = MST.getLocalSlot(V);
2170  if (Slot == -1)
2171  return;
2172  Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2173 }
2174 
2175 /// Creates the mapping from slot numbers to function's unnamed IR values.
2176 static void initSlots2Values(const Function &F,
2177  DenseMap<unsigned, const Value *> &Slots2Values) {
2178  ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2179  MST.incorporateFunction(F);
2180  for (const auto &Arg : F.args())
2181  mapValueToSlot(&Arg, MST, Slots2Values);
2182  for (const auto &BB : F) {
2183  mapValueToSlot(&BB, MST, Slots2Values);
2184  for (const auto &I : BB)
2185  mapValueToSlot(&I, MST, Slots2Values);
2186  }
2187 }
2188 
2189 const Value *MIParser::getIRValue(unsigned Slot) {
2190  if (Slots2Values.empty())
2191  initSlots2Values(*MF.getFunction(), Slots2Values);
2192  auto ValueInfo = Slots2Values.find(Slot);
2193  if (ValueInfo == Slots2Values.end())
2194  return nullptr;
2195  return ValueInfo->second;
2196 }
2197 
2198 void MIParser::initNames2TargetIndices() {
2199  if (!Names2TargetIndices.empty())
2200  return;
2201  const auto *TII = MF.getSubtarget().getInstrInfo();
2202  assert(TII && "Expected target instruction info");
2203  auto Indices = TII->getSerializableTargetIndices();
2204  for (const auto &I : Indices)
2205  Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2206 }
2207 
2208 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2209  initNames2TargetIndices();
2210  auto IndexInfo = Names2TargetIndices.find(Name);
2211  if (IndexInfo == Names2TargetIndices.end())
2212  return true;
2213  Index = IndexInfo->second;
2214  return false;
2215 }
2216 
2217 void MIParser::initNames2DirectTargetFlags() {
2218  if (!Names2DirectTargetFlags.empty())
2219  return;
2220  const auto *TII = MF.getSubtarget().getInstrInfo();
2221  assert(TII && "Expected target instruction info");
2222  auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2223  for (const auto &I : Flags)
2224  Names2DirectTargetFlags.insert(
2225  std::make_pair(StringRef(I.second), I.first));
2226 }
2227 
2228 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2229  initNames2DirectTargetFlags();
2230  auto FlagInfo = Names2DirectTargetFlags.find(Name);
2231  if (FlagInfo == Names2DirectTargetFlags.end())
2232  return true;
2233  Flag = FlagInfo->second;
2234  return false;
2235 }
2236 
2237 void MIParser::initNames2BitmaskTargetFlags() {
2238  if (!Names2BitmaskTargetFlags.empty())
2239  return;
2240  const auto *TII = MF.getSubtarget().getInstrInfo();
2241  assert(TII && "Expected target instruction info");
2242  auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2243  for (const auto &I : Flags)
2244  Names2BitmaskTargetFlags.insert(
2245  std::make_pair(StringRef(I.second), I.first));
2246 }
2247 
2248 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2249  initNames2BitmaskTargetFlags();
2250  auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2251  if (FlagInfo == Names2BitmaskTargetFlags.end())
2252  return true;
2253  Flag = FlagInfo->second;
2254  return false;
2255 }
2256 
2258  StringRef Src,
2259  SMDiagnostic &Error) {
2260  return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2261 }
2262 
2264  StringRef Src, SMDiagnostic &Error) {
2265  return MIParser(PFS, Error, Src).parseBasicBlocks();
2266 }
2267 
2269  MachineBasicBlock *&MBB, StringRef Src,
2270  SMDiagnostic &Error) {
2271  return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
2272 }
2273 
2275  unsigned &Reg, StringRef Src,
2276  SMDiagnostic &Error) {
2277  return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2278 }
2279 
2281  unsigned &Reg, StringRef Src,
2282  SMDiagnostic &Error) {
2283  return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
2284 }
2285 
2287  VRegInfo *&Info, StringRef Src,
2288  SMDiagnostic &Error) {
2289  return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
2290 }
2291 
2293  int &FI, StringRef Src,
2294  SMDiagnostic &Error) {
2295  return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
2296 }
2297 
2299  MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2300  return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
2301 }
const NoneType None
Definition: None.h:23
bool isImplicit() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
Definition: MIParser.cpp:2257
static MachineOperand CreateCImm(const ConstantInt *CI)
uint64_t Token
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1309
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:642
bool hasName() const
Definition: Value.h:236
const char * getBufferStart() const
Definition: MemoryBuffer.h:55
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:639
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:70
void setTargetFlags(unsigned F)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
static BranchProbability getRaw(uint32_t N)
static LaneBitmask getAll()
Definition: LaneBitmask.h:75
static MachineOperand CreateJTI(unsigned Idx, unsigned char TargetFlags=0)
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition: MCDwarf.h:396
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned less or equal
Definition: InstrTypes.h:906
unsigned less than
Definition: InstrTypes.h:905
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
Definition: AsmWriter.cpp:734
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
Definition: MIParser.cpp:2129
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:886
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:896
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:830
DenseMap< unsigned, VRegInfo * > VRegInfos
Definition: MIParser.h:55
Manage lifetime of a slot tracker for printing IR.
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2268
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:383
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...
void setAlignment(unsigned Align)
Set alignment of the basic block.
static MachineOperand CreateIntrinsicID(Intrinsic::ID ID)
static const BasicBlock * getIRBlockFromSlot(unsigned Slot, const DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
Definition: MIParser.cpp:2144
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:172
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:891
unsigned getMainFileID() const
Definition: SourceMgr.h:106
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:890
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
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.
struct fuzzer::@269 Flags
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
const HexagonInstrInfo * TII
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 isCall() const
Return true if the instruction is a call.
Definition: MCInstrDesc.h:242
bool isReg() const
isReg - Tests if this is a MO_Register operand.
unsigned SubReg
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
Reg
All possible values of the reg field in the ModR/M byte.
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:887
FixedWord< 27 > Word
The memory access is dereferenceable (i.e., doesn't trap).
static MachineOperand CreateRegMask(const uint32_t *Mask)
CreateRegMask - Creates a register mask operand referencing Mask.
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
Definition: regcomp.c:64
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
static std::string getRegisterName(const TargetRegisterInfo *TRI, unsigned Reg)
Definition: MIParser.cpp:785
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
#define F(x, y, z)
Definition: MD5.cpp:51
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
MachineBasicBlock * MBB
StringRef getMessage() const
Definition: SourceMgr.h:260
virtual unsigned lookupName(const char *Name, unsigned Len) const =0
Look up target intrinsic by name.
const MCPhysReg * getImplicitDefs() const
Return a list of registers that are potentially written by any instance of this machine instruction...
Definition: MCInstrDesc.h:525
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1279
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Definition: LowLevelType.h:51
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2286
static MachineOperand CreatePredicate(unsigned Pred)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:196
VRegInfo & getVRegInfo(unsigned VReg)
Definition: MIParser.cpp:48
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:436
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:2292
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
static const char * printImplicitRegisterFlag(const MachineOperand &MO)
Definition: MIParser.cpp:780
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:369
static Intrinsic::ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
Definition: Function.cpp:486
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned char TargetFlags=0)
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register)
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition: MCDwarf.h:376
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1356
static MachineOperand CreateFPImm(const ConstantFP *CFP)
This is an important base class in LLVM.
Definition: Constant.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
This file contains the declarations for the subclasses of Constant, which represent the different fla...
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots)
Definition: MIParser.cpp:43
const MCPhysReg * ImplicitDefs
Definition: MCInstrDesc.h:173
bool isIntPredicate() const
Definition: InstrTypes.h:978
void setType(unsigned VReg, LLT Ty)
Set the low-level type of VReg to Ty.
BumpPtrAllocator Allocator
Definition: MIParser.h:49
static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset, unsigned char TargetFlags=0)
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, unsigned &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2280
void setFlag(MIFlag Flag)
Set a MI flag.
Definition: MachineInstr.h:166
unsigned createIncompleteVirtualRegister()
Creates a new virtual register that has no register class, register bank or size assigned yet...
uint32_t Offset
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:880
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1255
static const unsigned End
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)
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:889
The memory access is non-temporal.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:136
MachineFunction & MF
Definition: MIParser.h:50
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
void setOffset(int64_t Offset)
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:897
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:720
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:225
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:213
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...
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:895
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:150
signed greater than
Definition: InstrTypes.h:907
static void initSlots2Values(const Function &F, DenseMap< unsigned, const Value * > &Slots2Values)
Creates the mapping from slot numbers to function's unnamed IR values.
Definition: MIParser.cpp:2176
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:2263
TargetIntrinsicInfo - Interface to description of machine instruction set.
Struct to hold value either by GUID or GlobalValue*.
The memory access writes data.
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:884
const char * iterator
Definition: StringRef.h:49
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.
int getColumnNo() const
Definition: SourceMgr.h:258
void setFlags(unsigned flags)
Definition: MachineInstr.h:170
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:894
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, ArrayRef< ParsedMachineOperand > Operands)
Return true if the parsed machine operands contain a given machine operand.
Definition: MIParser.cpp:792
signed less than
Definition: InstrTypes.h:909
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:625
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:645
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition: Function.h:527
virtual std::string getName(unsigned IID, Type **Tys=nullptr, unsigned numTys=0) const =0
Return the name of a target intrinsic, e.g.
signed less or equal
Definition: InstrTypes.h:910
StringRef getName() const
Return the name of the corresponding LLVM basic block, or "(null)".
Class for arbitrary precision integers.
Definition: APInt.h:77
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:65
Special value supplied for machine level alias analysis.
static MachineOperand CreateES(const char *SymName, unsigned char TargetFlags=0)
const char * getSubRegIndexName(unsigned SubIdx) const
Return the human-readable symbolic target-specific name for the specified SubRegIndex.
void setHasAddressTaken()
Set this block to reflect that it potentially is the target of an indirect branch.
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:2298
bool isFPPredicate() const
Definition: InstrTypes.h:977
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:37
Flags
Flags values. These may be or'd together.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1132
Representation of each machine instruction.
Definition: MachineInstr.h:52
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:97
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:578
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned greater or equal
Definition: InstrTypes.h:904
static MachineOperand CreateCFIIndex(unsigned CFIIndex)
unsigned VReg
Definition: MIParser.h:44
static MachineOperand CreateImm(int64_t Val)
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
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:349
The memory access always returns the same value (or traps).
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:888
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition: MIParser.h:54
bool isValid() const
Definition: LowLevelType.h:88
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:287
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const MCPhysReg * getImplicitUses() const
Return a list of registers that are potentially read by any instance of this machine instruction...
Definition: MCInstrDesc.h:503
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:892
const char * getBufferEnd() const
Definition: MemoryBuffer.h:56
const unsigned Kind
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value * > &Slots2Values)
Definition: MIParser.cpp:2167
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:883
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
LLVM Value Representation.
Definition: Value.h:71
static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space (defaulting to 0).
Definition: LowLevelType.h:57
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:893
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:239
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
Error error(const Twine &Message)
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
IRTranslator LLVM IR MI
unsigned greater than
Definition: InstrTypes.h:903
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
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:136
static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
Definition: LowLevelType.h:63
int * Ptr
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
Represents a location in source code.
Definition: SMLoc.h:24
bool parseRegisterReference(PerFunctionMIParsingState &PFS, unsigned &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:2274
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:885
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
static MachineOperand CreateFI(int Idx)
void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd)
Assign this MachineInstr's memory reference descriptor list.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:93
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
iterator_range< arg_iterator > args()
Definition: Function.h:568
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:882
signed greater or equal
Definition: InstrTypes.h:908
const MCPhysReg * ImplicitUses
Definition: MCInstrDesc.h:172
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:228
unsigned getNumWords() const
Get the number of words.
Definition: APInt.h:1262
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.