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/AsmParser/Parser.h"
18 #include "llvm/AsmParser/SlotMapping.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineMemOperand.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/ModuleSlotTracker.h"
31 #include "llvm/IR/ValueSymbolTable.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetInstrInfo.h"
35 #include "llvm/Target/TargetSubtargetInfo.h"
36 
37 using namespace llvm;
38 
39 namespace {
40 
41 /// A wrapper struct around the 'MachineOperand' struct that includes a source
42 /// range and other attributes.
43 struct ParsedMachineOperand {
44   MachineOperand Operand;
45   StringRef::iterator Begin;
46   StringRef::iterator End;
47   Optional<unsigned> TiedDefIdx;
48 
49   ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
50                        StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
51       : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
52     if (TiedDefIdx)
53       assert(Operand.isReg() && Operand.isUse() &&
54              "Only used register operands can be tied");
55   }
56 };
57 
58 class MIParser {
59   SourceMgr &SM;
60   MachineFunction &MF;
61   SMDiagnostic &Error;
62   StringRef Source, CurrentSource;
63   MIToken Token;
64   const PerFunctionMIParsingState &PFS;
65   /// Maps from indices to unnamed global values and metadata nodes.
66   const SlotMapping &IRSlots;
67   /// Maps from instruction names to op codes.
68   StringMap<unsigned> Names2InstrOpCodes;
69   /// Maps from register names to registers.
70   StringMap<unsigned> Names2Regs;
71   /// Maps from register mask names to register masks.
72   StringMap<const uint32_t *> Names2RegMasks;
73   /// Maps from subregister names to subregister indices.
74   StringMap<unsigned> Names2SubRegIndices;
75   /// Maps from slot numbers to function's unnamed basic blocks.
76   DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
77   /// Maps from slot numbers to function's unnamed values.
78   DenseMap<unsigned, const Value *> Slots2Values;
79   /// Maps from target index names to target indices.
80   StringMap<int> Names2TargetIndices;
81   /// Maps from direct target flag names to the direct target flag values.
82   StringMap<unsigned> Names2DirectTargetFlags;
83   /// Maps from direct target flag names to the bitmask target flag values.
84   StringMap<unsigned> Names2BitmaskTargetFlags;
85 
86 public:
87   MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
88            StringRef Source, const PerFunctionMIParsingState &PFS,
89            const SlotMapping &IRSlots);
90 
91   /// \p SkipChar gives the number of characters to skip before looking
92   /// for the next token.
93   void lex(unsigned SkipChar = 0);
94 
95   /// Report an error at the current location with the given message.
96   ///
97   /// This function always return true.
98   bool error(const Twine &Msg);
99 
100   /// Report an error at the given location with the given message.
101   ///
102   /// This function always return true.
103   bool error(StringRef::iterator Loc, const Twine &Msg);
104 
105   bool
106   parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
107   bool parseBasicBlocks();
108   bool parse(MachineInstr *&MI);
109   bool parseStandaloneMBB(MachineBasicBlock *&MBB);
110   bool parseStandaloneNamedRegister(unsigned &Reg);
111   bool parseStandaloneVirtualRegister(unsigned &Reg);
112   bool parseStandaloneStackObject(int &FI);
113   bool parseStandaloneMDNode(MDNode *&Node);
114 
115   bool
116   parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
117   bool parseBasicBlock(MachineBasicBlock &MBB);
118   bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
119   bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
120 
121   bool parseRegister(unsigned &Reg);
122   bool parseRegisterFlag(unsigned &Flags);
123   bool parseSubRegisterIndex(unsigned &SubReg);
124   bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
125   bool parseSize(unsigned &Size);
126   bool parseRegisterOperand(MachineOperand &Dest,
127                             Optional<unsigned> &TiedDefIdx, bool IsDef = false);
128   bool parseImmediateOperand(MachineOperand &Dest);
129   bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
130                        const Constant *&C);
131   bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
132   bool parseIRType(StringRef::iterator Loc, StringRef Source, unsigned &Read,
133                    Type *&Ty);
134   // \p MustBeSized defines whether or not \p Ty must be sized.
135   bool parseIRType(StringRef::iterator Loc, Type *&Ty, bool MustBeSized = true);
136   bool parseTypedImmediateOperand(MachineOperand &Dest);
137   bool parseFPImmediateOperand(MachineOperand &Dest);
138   bool parseMBBReference(MachineBasicBlock *&MBB);
139   bool parseMBBOperand(MachineOperand &Dest);
140   bool parseStackFrameIndex(int &FI);
141   bool parseStackObjectOperand(MachineOperand &Dest);
142   bool parseFixedStackFrameIndex(int &FI);
143   bool parseFixedStackObjectOperand(MachineOperand &Dest);
144   bool parseGlobalValue(GlobalValue *&GV);
145   bool parseGlobalAddressOperand(MachineOperand &Dest);
146   bool parseConstantPoolIndexOperand(MachineOperand &Dest);
147   bool parseSubRegisterIndexOperand(MachineOperand &Dest);
148   bool parseJumpTableIndexOperand(MachineOperand &Dest);
149   bool parseExternalSymbolOperand(MachineOperand &Dest);
150   bool parseMDNode(MDNode *&Node);
151   bool parseMetadataOperand(MachineOperand &Dest);
152   bool parseCFIOffset(int &Offset);
153   bool parseCFIRegister(unsigned &Reg);
154   bool parseCFIOperand(MachineOperand &Dest);
155   bool parseIRBlock(BasicBlock *&BB, const Function &F);
156   bool parseBlockAddressOperand(MachineOperand &Dest);
157   bool parseTargetIndexOperand(MachineOperand &Dest);
158   bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
159   bool parseMachineOperand(MachineOperand &Dest,
160                            Optional<unsigned> &TiedDefIdx);
161   bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
162                                          Optional<unsigned> &TiedDefIdx);
163   bool parseOffset(int64_t &Offset);
164   bool parseAlignment(unsigned &Alignment);
165   bool parseOperandsOffset(MachineOperand &Op);
166   bool parseIRValue(const Value *&V);
167   bool parseMemoryOperandFlag(unsigned &Flags);
168   bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
169   bool parseMachinePointerInfo(MachinePointerInfo &Dest);
170   bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
171 
172 private:
173   /// Convert the integer literal in the current token into an unsigned integer.
174   ///
175   /// Return true if an error occurred.
176   bool getUnsigned(unsigned &Result);
177 
178   /// Convert the integer literal in the current token into an uint64.
179   ///
180   /// Return true if an error occurred.
181   bool getUint64(uint64_t &Result);
182 
183   /// If the current token is of the given kind, consume it and return false.
184   /// Otherwise report an error and return true.
185   bool expectAndConsume(MIToken::TokenKind TokenKind);
186 
187   /// If the current token is of the given kind, consume it and return true.
188   /// Otherwise return false.
189   bool consumeIfPresent(MIToken::TokenKind TokenKind);
190 
191   void initNames2InstrOpCodes();
192 
193   /// Try to convert an instruction name to an opcode. Return true if the
194   /// instruction name is invalid.
195   bool parseInstrName(StringRef InstrName, unsigned &OpCode);
196 
197   bool parseInstruction(unsigned &OpCode, unsigned &Flags);
198 
199   bool assignRegisterTies(MachineInstr &MI,
200                           ArrayRef<ParsedMachineOperand> Operands);
201 
202   bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
203                               const MCInstrDesc &MCID);
204 
205   void initNames2Regs();
206 
207   /// Try to convert a register name to a register number. Return true if the
208   /// register name is invalid.
209   bool getRegisterByName(StringRef RegName, unsigned &Reg);
210 
211   void initNames2RegMasks();
212 
213   /// Check if the given identifier is a name of a register mask.
214   ///
215   /// Return null if the identifier isn't a register mask.
216   const uint32_t *getRegMask(StringRef Identifier);
217 
218   void initNames2SubRegIndices();
219 
220   /// Check if the given identifier is a name of a subregister index.
221   ///
222   /// Return 0 if the name isn't a subregister index class.
223   unsigned getSubRegIndex(StringRef Name);
224 
225   const BasicBlock *getIRBlock(unsigned Slot);
226   const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
227 
228   const Value *getIRValue(unsigned Slot);
229 
230   void initNames2TargetIndices();
231 
232   /// Try to convert a name of target index to the corresponding target index.
233   ///
234   /// Return true if the name isn't a name of a target index.
235   bool getTargetIndex(StringRef Name, int &Index);
236 
237   void initNames2DirectTargetFlags();
238 
239   /// Try to convert a name of a direct target flag to the corresponding
240   /// target flag.
241   ///
242   /// Return true if the name isn't a name of a direct flag.
243   bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
244 
245   void initNames2BitmaskTargetFlags();
246 
247   /// Try to convert a name of a bitmask target flag to the corresponding
248   /// target flag.
249   ///
250   /// Return true if the name isn't a name of a bitmask target flag.
251   bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
252 };
253 
254 } // end anonymous namespace
255 
256 MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
257                    StringRef Source, const PerFunctionMIParsingState &PFS,
258                    const SlotMapping &IRSlots)
259     : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
260       PFS(PFS), IRSlots(IRSlots) {}
261 
262 void MIParser::lex(unsigned SkipChar) {
263   CurrentSource = lexMIToken(
264       CurrentSource.data() + SkipChar, Token,
265       [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
266 }
267 
268 bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
269 
270 bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
271   assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
272   const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
273   if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
274     // Create an ordinary diagnostic when the source manager's buffer is the
275     // source string.
276     Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
277     return true;
278   }
279   // Create a diagnostic for a YAML string literal.
280   Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
281                        Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
282                        Source, None, None);
283   return true;
284 }
285 
286 static const char *toString(MIToken::TokenKind TokenKind) {
287   switch (TokenKind) {
288   case MIToken::comma:
289     return "','";
290   case MIToken::equal:
291     return "'='";
292   case MIToken::colon:
293     return "':'";
294   case MIToken::lparen:
295     return "'('";
296   case MIToken::rparen:
297     return "')'";
298   default:
299     return "<unknown token>";
300   }
301 }
302 
303 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
304   if (Token.isNot(TokenKind))
305     return error(Twine("expected ") + toString(TokenKind));
306   lex();
307   return false;
308 }
309 
310 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
311   if (Token.isNot(TokenKind))
312     return false;
313   lex();
314   return true;
315 }
316 
317 bool MIParser::parseBasicBlockDefinition(
318     DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
319   assert(Token.is(MIToken::MachineBasicBlockLabel));
320   unsigned ID = 0;
321   if (getUnsigned(ID))
322     return true;
323   auto Loc = Token.location();
324   auto Name = Token.stringValue();
325   lex();
326   bool HasAddressTaken = false;
327   bool IsLandingPad = false;
328   unsigned Alignment = 0;
329   BasicBlock *BB = nullptr;
330   if (consumeIfPresent(MIToken::lparen)) {
331     do {
332       // TODO: Report an error when multiple same attributes are specified.
333       switch (Token.kind()) {
334       case MIToken::kw_address_taken:
335         HasAddressTaken = true;
336         lex();
337         break;
338       case MIToken::kw_landing_pad:
339         IsLandingPad = true;
340         lex();
341         break;
342       case MIToken::kw_align:
343         if (parseAlignment(Alignment))
344           return true;
345         break;
346       case MIToken::IRBlock:
347         // TODO: Report an error when both name and ir block are specified.
348         if (parseIRBlock(BB, *MF.getFunction()))
349           return true;
350         lex();
351         break;
352       default:
353         break;
354       }
355     } while (consumeIfPresent(MIToken::comma));
356     if (expectAndConsume(MIToken::rparen))
357       return true;
358   }
359   if (expectAndConsume(MIToken::colon))
360     return true;
361 
362   if (!Name.empty()) {
363     BB = dyn_cast_or_null<BasicBlock>(
364         MF.getFunction()->getValueSymbolTable().lookup(Name));
365     if (!BB)
366       return error(Loc, Twine("basic block '") + Name +
367                             "' is not defined in the function '" +
368                             MF.getName() + "'");
369   }
370   auto *MBB = MF.CreateMachineBasicBlock(BB);
371   MF.insert(MF.end(), MBB);
372   bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
373   if (!WasInserted)
374     return error(Loc, Twine("redefinition of machine basic block with id #") +
375                           Twine(ID));
376   if (Alignment)
377     MBB->setAlignment(Alignment);
378   if (HasAddressTaken)
379     MBB->setHasAddressTaken();
380   MBB->setIsEHPad(IsLandingPad);
381   return false;
382 }
383 
384 bool MIParser::parseBasicBlockDefinitions(
385     DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
386   lex();
387   // Skip until the first machine basic block.
388   while (Token.is(MIToken::Newline))
389     lex();
390   if (Token.isErrorOrEOF())
391     return Token.isError();
392   if (Token.isNot(MIToken::MachineBasicBlockLabel))
393     return error("expected a basic block definition before instructions");
394   unsigned BraceDepth = 0;
395   do {
396     if (parseBasicBlockDefinition(MBBSlots))
397       return true;
398     bool IsAfterNewline = false;
399     // Skip until the next machine basic block.
400     while (true) {
401       if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
402           Token.isErrorOrEOF())
403         break;
404       else if (Token.is(MIToken::MachineBasicBlockLabel))
405         return error("basic block definition should be located at the start of "
406                      "the line");
407       else if (consumeIfPresent(MIToken::Newline)) {
408         IsAfterNewline = true;
409         continue;
410       }
411       IsAfterNewline = false;
412       if (Token.is(MIToken::lbrace))
413         ++BraceDepth;
414       if (Token.is(MIToken::rbrace)) {
415         if (!BraceDepth)
416           return error("extraneous closing brace ('}')");
417         --BraceDepth;
418       }
419       lex();
420     }
421     // Verify that we closed all of the '{' at the end of a file or a block.
422     if (!Token.isError() && BraceDepth)
423       return error("expected '}'"); // FIXME: Report a note that shows '{'.
424   } while (!Token.isErrorOrEOF());
425   return Token.isError();
426 }
427 
428 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
429   assert(Token.is(MIToken::kw_liveins));
430   lex();
431   if (expectAndConsume(MIToken::colon))
432     return true;
433   if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
434     return false;
435   do {
436     if (Token.isNot(MIToken::NamedRegister))
437       return error("expected a named register");
438     unsigned Reg = 0;
439     if (parseRegister(Reg))
440       return true;
441     MBB.addLiveIn(Reg);
442     lex();
443   } while (consumeIfPresent(MIToken::comma));
444   return false;
445 }
446 
447 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
448   assert(Token.is(MIToken::kw_successors));
449   lex();
450   if (expectAndConsume(MIToken::colon))
451     return true;
452   if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
453     return false;
454   do {
455     if (Token.isNot(MIToken::MachineBasicBlock))
456       return error("expected a machine basic block reference");
457     MachineBasicBlock *SuccMBB = nullptr;
458     if (parseMBBReference(SuccMBB))
459       return true;
460     lex();
461     unsigned Weight = 0;
462     if (consumeIfPresent(MIToken::lparen)) {
463       if (Token.isNot(MIToken::IntegerLiteral))
464         return error("expected an integer literal after '('");
465       if (getUnsigned(Weight))
466         return true;
467       lex();
468       if (expectAndConsume(MIToken::rparen))
469         return true;
470     }
471     MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
472   } while (consumeIfPresent(MIToken::comma));
473   MBB.normalizeSuccProbs();
474   return false;
475 }
476 
477 bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
478   // Skip the definition.
479   assert(Token.is(MIToken::MachineBasicBlockLabel));
480   lex();
481   if (consumeIfPresent(MIToken::lparen)) {
482     while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
483       lex();
484     consumeIfPresent(MIToken::rparen);
485   }
486   consumeIfPresent(MIToken::colon);
487 
488   // Parse the liveins and successors.
489   // N.B: Multiple lists of successors and liveins are allowed and they're
490   // merged into one.
491   // Example:
492   //   liveins: %edi
493   //   liveins: %esi
494   //
495   // is equivalent to
496   //   liveins: %edi, %esi
497   while (true) {
498     if (Token.is(MIToken::kw_successors)) {
499       if (parseBasicBlockSuccessors(MBB))
500         return true;
501     } else if (Token.is(MIToken::kw_liveins)) {
502       if (parseBasicBlockLiveins(MBB))
503         return true;
504     } else if (consumeIfPresent(MIToken::Newline)) {
505       continue;
506     } else
507       break;
508     if (!Token.isNewlineOrEOF())
509       return error("expected line break at the end of a list");
510     lex();
511   }
512 
513   // Parse the instructions.
514   bool IsInBundle = false;
515   MachineInstr *PrevMI = nullptr;
516   while (true) {
517     if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
518       return false;
519     else if (consumeIfPresent(MIToken::Newline))
520       continue;
521     if (consumeIfPresent(MIToken::rbrace)) {
522       // The first parsing pass should verify that all closing '}' have an
523       // opening '{'.
524       assert(IsInBundle);
525       IsInBundle = false;
526       continue;
527     }
528     MachineInstr *MI = nullptr;
529     if (parse(MI))
530       return true;
531     MBB.insert(MBB.end(), MI);
532     if (IsInBundle) {
533       PrevMI->setFlag(MachineInstr::BundledSucc);
534       MI->setFlag(MachineInstr::BundledPred);
535     }
536     PrevMI = MI;
537     if (Token.is(MIToken::lbrace)) {
538       if (IsInBundle)
539         return error("nested instruction bundles are not allowed");
540       lex();
541       // This instruction is the start of the bundle.
542       MI->setFlag(MachineInstr::BundledSucc);
543       IsInBundle = true;
544       if (!Token.is(MIToken::Newline))
545         // The next instruction can be on the same line.
546         continue;
547     }
548     assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
549     lex();
550   }
551   return false;
552 }
553 
554 bool MIParser::parseBasicBlocks() {
555   lex();
556   // Skip until the first machine basic block.
557   while (Token.is(MIToken::Newline))
558     lex();
559   if (Token.isErrorOrEOF())
560     return Token.isError();
561   // The first parsing pass should have verified that this token is a MBB label
562   // in the 'parseBasicBlockDefinitions' method.
563   assert(Token.is(MIToken::MachineBasicBlockLabel));
564   do {
565     MachineBasicBlock *MBB = nullptr;
566     if (parseMBBReference(MBB))
567       return true;
568     if (parseBasicBlock(*MBB))
569       return true;
570     // The method 'parseBasicBlock' should parse the whole block until the next
571     // block or the end of file.
572     assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
573   } while (Token.isNot(MIToken::Eof));
574   return false;
575 }
576 
577 bool MIParser::parse(MachineInstr *&MI) {
578   // Parse any register operands before '='
579   MachineOperand MO = MachineOperand::CreateImm(0);
580   SmallVector<ParsedMachineOperand, 8> Operands;
581   while (Token.isRegister() || Token.isRegisterFlag()) {
582     auto Loc = Token.location();
583     Optional<unsigned> TiedDefIdx;
584     if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
585       return true;
586     Operands.push_back(
587         ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
588     if (Token.isNot(MIToken::comma))
589       break;
590     lex();
591   }
592   if (!Operands.empty() && expectAndConsume(MIToken::equal))
593     return true;
594 
595   unsigned OpCode, Flags = 0;
596   if (Token.isError() || parseInstruction(OpCode, Flags))
597     return true;
598 
599   Type *Ty = nullptr;
600   if (isPreISelGenericOpcode(OpCode)) {
601     // For generic opcode, a type is mandatory.
602     auto Loc = Token.location();
603     if (parseIRType(Loc, Ty))
604       return true;
605   }
606 
607   // Parse the remaining machine operands.
608   while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
609          Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
610     auto Loc = Token.location();
611     Optional<unsigned> TiedDefIdx;
612     if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
613       return true;
614     Operands.push_back(
615         ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
616     if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
617         Token.is(MIToken::lbrace))
618       break;
619     if (Token.isNot(MIToken::comma))
620       return error("expected ',' before the next machine operand");
621     lex();
622   }
623 
624   DebugLoc DebugLocation;
625   if (Token.is(MIToken::kw_debug_location)) {
626     lex();
627     if (Token.isNot(MIToken::exclaim))
628       return error("expected a metadata node after 'debug-location'");
629     MDNode *Node = nullptr;
630     if (parseMDNode(Node))
631       return true;
632     DebugLocation = DebugLoc(Node);
633   }
634 
635   // Parse the machine memory operands.
636   SmallVector<MachineMemOperand *, 2> MemOperands;
637   if (Token.is(MIToken::coloncolon)) {
638     lex();
639     while (!Token.isNewlineOrEOF()) {
640       MachineMemOperand *MemOp = nullptr;
641       if (parseMachineMemoryOperand(MemOp))
642         return true;
643       MemOperands.push_back(MemOp);
644       if (Token.isNewlineOrEOF())
645         break;
646       if (Token.isNot(MIToken::comma))
647         return error("expected ',' before the next machine memory operand");
648       lex();
649     }
650   }
651 
652   const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
653   if (!MCID.isVariadic()) {
654     // FIXME: Move the implicit operand verification to the machine verifier.
655     if (verifyImplicitOperands(Operands, MCID))
656       return true;
657   }
658 
659   // TODO: Check for extraneous machine operands.
660   MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
661   MI->setFlags(Flags);
662   if (Ty)
663     MI->setType(Ty);
664   for (const auto &Operand : Operands)
665     MI->addOperand(MF, Operand.Operand);
666   if (assignRegisterTies(*MI, Operands))
667     return true;
668   if (MemOperands.empty())
669     return false;
670   MachineInstr::mmo_iterator MemRefs =
671       MF.allocateMemRefsArray(MemOperands.size());
672   std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
673   MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
674   return false;
675 }
676 
677 bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
678   lex();
679   if (Token.isNot(MIToken::MachineBasicBlock))
680     return error("expected a machine basic block reference");
681   if (parseMBBReference(MBB))
682     return true;
683   lex();
684   if (Token.isNot(MIToken::Eof))
685     return error(
686         "expected end of string after the machine basic block reference");
687   return false;
688 }
689 
690 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
691   lex();
692   if (Token.isNot(MIToken::NamedRegister))
693     return error("expected a named register");
694   if (parseRegister(Reg))
695     return true;
696   lex();
697   if (Token.isNot(MIToken::Eof))
698     return error("expected end of string after the register reference");
699   return false;
700 }
701 
702 bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
703   lex();
704   if (Token.isNot(MIToken::VirtualRegister))
705     return error("expected a virtual register");
706   if (parseRegister(Reg))
707     return true;
708   lex();
709   if (Token.isNot(MIToken::Eof))
710     return error("expected end of string after the register reference");
711   return false;
712 }
713 
714 bool MIParser::parseStandaloneStackObject(int &FI) {
715   lex();
716   if (Token.isNot(MIToken::StackObject))
717     return error("expected a stack object");
718   if (parseStackFrameIndex(FI))
719     return true;
720   if (Token.isNot(MIToken::Eof))
721     return error("expected end of string after the stack object reference");
722   return false;
723 }
724 
725 bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
726   lex();
727   if (Token.isNot(MIToken::exclaim))
728     return error("expected a metadata node");
729   if (parseMDNode(Node))
730     return true;
731   if (Token.isNot(MIToken::Eof))
732     return error("expected end of string after the metadata node");
733   return false;
734 }
735 
736 static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
737   assert(MO.isImplicit());
738   return MO.isDef() ? "implicit-def" : "implicit";
739 }
740 
741 static std::string getRegisterName(const TargetRegisterInfo *TRI,
742                                    unsigned Reg) {
743   assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
744   return StringRef(TRI->getName(Reg)).lower();
745 }
746 
747 /// Return true if the parsed machine operands contain a given machine operand.
748 static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
749                                 ArrayRef<ParsedMachineOperand> Operands) {
750   for (const auto &I : Operands) {
751     if (ImplicitOperand.isIdenticalTo(I.Operand))
752       return true;
753   }
754   return false;
755 }
756 
757 bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
758                                       const MCInstrDesc &MCID) {
759   if (MCID.isCall())
760     // We can't verify call instructions as they can contain arbitrary implicit
761     // register and register mask operands.
762     return false;
763 
764   // Gather all the expected implicit operands.
765   SmallVector<MachineOperand, 4> ImplicitOperands;
766   if (MCID.ImplicitDefs)
767     for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
768       ImplicitOperands.push_back(
769           MachineOperand::CreateReg(*ImpDefs, true, true));
770   if (MCID.ImplicitUses)
771     for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
772       ImplicitOperands.push_back(
773           MachineOperand::CreateReg(*ImpUses, false, true));
774 
775   const auto *TRI = MF.getSubtarget().getRegisterInfo();
776   assert(TRI && "Expected target register info");
777   for (const auto &I : ImplicitOperands) {
778     if (isImplicitOperandIn(I, Operands))
779       continue;
780     return error(Operands.empty() ? Token.location() : Operands.back().End,
781                  Twine("missing implicit register operand '") +
782                      printImplicitRegisterFlag(I) + " %" +
783                      getRegisterName(TRI, I.getReg()) + "'");
784   }
785   return false;
786 }
787 
788 bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
789   if (Token.is(MIToken::kw_frame_setup)) {
790     Flags |= MachineInstr::FrameSetup;
791     lex();
792   }
793   if (Token.isNot(MIToken::Identifier))
794     return error("expected a machine instruction");
795   StringRef InstrName = Token.stringValue();
796   if (parseInstrName(InstrName, OpCode))
797     return error(Twine("unknown machine instruction name '") + InstrName + "'");
798   lex();
799   return false;
800 }
801 
802 bool MIParser::parseRegister(unsigned &Reg) {
803   switch (Token.kind()) {
804   case MIToken::underscore:
805     Reg = 0;
806     break;
807   case MIToken::NamedRegister: {
808     StringRef Name = Token.stringValue();
809     if (getRegisterByName(Name, Reg))
810       return error(Twine("unknown register name '") + Name + "'");
811     break;
812   }
813   case MIToken::VirtualRegister: {
814     unsigned ID;
815     if (getUnsigned(ID))
816       return true;
817     const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
818     if (RegInfo == PFS.VirtualRegisterSlots.end())
819       return error(Twine("use of undefined virtual register '%") + Twine(ID) +
820                    "'");
821     Reg = RegInfo->second;
822     break;
823   }
824   // TODO: Parse other register kinds.
825   default:
826     llvm_unreachable("The current token should be a register");
827   }
828   return false;
829 }
830 
831 bool MIParser::parseRegisterFlag(unsigned &Flags) {
832   const unsigned OldFlags = Flags;
833   switch (Token.kind()) {
834   case MIToken::kw_implicit:
835     Flags |= RegState::Implicit;
836     break;
837   case MIToken::kw_implicit_define:
838     Flags |= RegState::ImplicitDefine;
839     break;
840   case MIToken::kw_def:
841     Flags |= RegState::Define;
842     break;
843   case MIToken::kw_dead:
844     Flags |= RegState::Dead;
845     break;
846   case MIToken::kw_killed:
847     Flags |= RegState::Kill;
848     break;
849   case MIToken::kw_undef:
850     Flags |= RegState::Undef;
851     break;
852   case MIToken::kw_internal:
853     Flags |= RegState::InternalRead;
854     break;
855   case MIToken::kw_early_clobber:
856     Flags |= RegState::EarlyClobber;
857     break;
858   case MIToken::kw_debug_use:
859     Flags |= RegState::Debug;
860     break;
861   default:
862     llvm_unreachable("The current token should be a register flag");
863   }
864   if (OldFlags == Flags)
865     // We know that the same flag is specified more than once when the flags
866     // weren't modified.
867     return error("duplicate '" + Token.stringValue() + "' register flag");
868   lex();
869   return false;
870 }
871 
872 bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
873   assert(Token.is(MIToken::colon));
874   lex();
875   if (Token.isNot(MIToken::Identifier))
876     return error("expected a subregister index after ':'");
877   auto Name = Token.stringValue();
878   SubReg = getSubRegIndex(Name);
879   if (!SubReg)
880     return error(Twine("use of unknown subregister index '") + Name + "'");
881   lex();
882   return false;
883 }
884 
885 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
886   if (!consumeIfPresent(MIToken::kw_tied_def))
887     return error("expected 'tied-def' after '('");
888   if (Token.isNot(MIToken::IntegerLiteral))
889     return error("expected an integer literal after 'tied-def'");
890   if (getUnsigned(TiedDefIdx))
891     return true;
892   lex();
893   if (expectAndConsume(MIToken::rparen))
894     return true;
895   return false;
896 }
897 
898 bool MIParser::parseSize(unsigned &Size) {
899   if (Token.isNot(MIToken::IntegerLiteral))
900     return error("expected an integer literal for the size");
901   if (getUnsigned(Size))
902     return true;
903   lex();
904   if (expectAndConsume(MIToken::rparen))
905     return true;
906   return false;
907 }
908 
909 bool MIParser::assignRegisterTies(MachineInstr &MI,
910                                   ArrayRef<ParsedMachineOperand> Operands) {
911   SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
912   for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
913     if (!Operands[I].TiedDefIdx)
914       continue;
915     // The parser ensures that this operand is a register use, so we just have
916     // to check the tied-def operand.
917     unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
918     if (DefIdx >= E)
919       return error(Operands[I].Begin,
920                    Twine("use of invalid tied-def operand index '" +
921                          Twine(DefIdx) + "'; instruction has only ") +
922                        Twine(E) + " operands");
923     const auto &DefOperand = Operands[DefIdx].Operand;
924     if (!DefOperand.isReg() || !DefOperand.isDef())
925       // FIXME: add note with the def operand.
926       return error(Operands[I].Begin,
927                    Twine("use of invalid tied-def operand index '") +
928                        Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
929                        " isn't a defined register");
930     // Check that the tied-def operand wasn't tied elsewhere.
931     for (const auto &TiedPair : TiedRegisterPairs) {
932       if (TiedPair.first == DefIdx)
933         return error(Operands[I].Begin,
934                      Twine("the tied-def operand #") + Twine(DefIdx) +
935                          " is already tied with another register operand");
936     }
937     TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
938   }
939   // FIXME: Verify that for non INLINEASM instructions, the def and use tied
940   // indices must be less than tied max.
941   for (const auto &TiedPair : TiedRegisterPairs)
942     MI.tieOperands(TiedPair.first, TiedPair.second);
943   return false;
944 }
945 
946 bool MIParser::parseRegisterOperand(MachineOperand &Dest,
947                                     Optional<unsigned> &TiedDefIdx,
948                                     bool IsDef) {
949   unsigned Reg;
950   unsigned Flags = IsDef ? RegState::Define : 0;
951   while (Token.isRegisterFlag()) {
952     if (parseRegisterFlag(Flags))
953       return true;
954   }
955   if (!Token.isRegister())
956     return error("expected a register after register flags");
957   if (parseRegister(Reg))
958     return true;
959   lex();
960   unsigned SubReg = 0;
961   if (Token.is(MIToken::colon)) {
962     if (parseSubRegisterIndex(SubReg))
963       return true;
964   }
965   if ((Flags & RegState::Define) == 0) {
966     if (consumeIfPresent(MIToken::lparen)) {
967       unsigned Idx;
968       if (parseRegisterTiedDefIndex(Idx))
969         return true;
970       TiedDefIdx = Idx;
971     }
972   } else if (consumeIfPresent(MIToken::lparen)) {
973     // Virtual registers may have a size with GlobalISel.
974     if (!TargetRegisterInfo::isVirtualRegister(Reg))
975       return error("unexpected size on physical register");
976     unsigned Size;
977     if (parseSize(Size))
978       return true;
979 
980     MachineRegisterInfo &MRI = MF.getRegInfo();
981     MRI.setSize(Reg, Size);
982   } else if (PFS.GenericVRegs.count(Reg)) {
983     // Generic virtual registers must have a size.
984     // If we end up here this means the size hasn't been specified and
985     // this is bad!
986     return error("generic virtual registers must have a size");
987   }
988   Dest = MachineOperand::CreateReg(
989       Reg, Flags & RegState::Define, Flags & RegState::Implicit,
990       Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
991       Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
992       Flags & RegState::InternalRead);
993   return false;
994 }
995 
996 bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
997   assert(Token.is(MIToken::IntegerLiteral));
998   const APSInt &Int = Token.integerValue();
999   if (Int.getMinSignedBits() > 64)
1000     return error("integer literal is too large to be an immediate operand");
1001   Dest = MachineOperand::CreateImm(Int.getExtValue());
1002   lex();
1003   return false;
1004 }
1005 
1006 bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1007                                const Constant *&C) {
1008   auto Source = StringValue.str(); // The source has to be null terminated.
1009   SMDiagnostic Err;
1010   C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
1011                          &IRSlots);
1012   if (!C)
1013     return error(Loc + Err.getColumnNo(), Err.getMessage());
1014   return false;
1015 }
1016 
1017 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1018   if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1019     return true;
1020   lex();
1021   return false;
1022 }
1023 
1024 bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
1025                            unsigned &Read, Type *&Ty) {
1026   auto Source = StringValue.str(); // The source has to be null terminated.
1027   SMDiagnostic Err;
1028   Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
1029                             *MF.getFunction()->getParent(), &IRSlots);
1030   if (!Ty)
1031     return error(Loc + Err.getColumnNo(), Err.getMessage());
1032   return false;
1033 }
1034 
1035 bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
1036                            bool MustBeSized) {
1037   // At this point we enter in the IR world, i.e., to get the correct type,
1038   // we need to hand off the whole string, not just the current token.
1039   // E.g., <4 x i64> would give '<' as a token and there is not much
1040   // the IR parser can do with that.
1041   unsigned Read = 0;
1042   if (parseIRType(Loc, StringRef(Loc), Read, Ty))
1043     return true;
1044   // The type must be sized, otherwise there is not much the backend
1045   // can do with it.
1046   if (MustBeSized && !Ty->isSized())
1047     return error("expected a sized type");
1048   // The next token is Read characters from the Loc.
1049   // However, the current location is not Loc, but Loc + the length of Token.
1050   // Therefore, subtract the length of Token (range().end() - Loc) to the
1051   // number of characters to skip before the next token.
1052   lex(Read - (Token.range().end() - Loc));
1053   return false;
1054 }
1055 
1056 bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1057   assert(Token.is(MIToken::IntegerType));
1058   auto Loc = Token.location();
1059   lex();
1060   if (Token.isNot(MIToken::IntegerLiteral))
1061     return error("expected an integer literal");
1062   const Constant *C = nullptr;
1063   if (parseIRConstant(Loc, C))
1064     return true;
1065   Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1066   return false;
1067 }
1068 
1069 bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1070   auto Loc = Token.location();
1071   lex();
1072   if (Token.isNot(MIToken::FloatingPointLiteral))
1073     return error("expected a floating point literal");
1074   const Constant *C = nullptr;
1075   if (parseIRConstant(Loc, C))
1076     return true;
1077   Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1078   return false;
1079 }
1080 
1081 bool MIParser::getUnsigned(unsigned &Result) {
1082   assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1083   const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1084   uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1085   if (Val64 == Limit)
1086     return error("expected 32-bit integer (too large)");
1087   Result = Val64;
1088   return false;
1089 }
1090 
1091 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1092   assert(Token.is(MIToken::MachineBasicBlock) ||
1093          Token.is(MIToken::MachineBasicBlockLabel));
1094   unsigned Number;
1095   if (getUnsigned(Number))
1096     return true;
1097   auto MBBInfo = PFS.MBBSlots.find(Number);
1098   if (MBBInfo == PFS.MBBSlots.end())
1099     return error(Twine("use of undefined machine basic block #") +
1100                  Twine(Number));
1101   MBB = MBBInfo->second;
1102   if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1103     return error(Twine("the name of machine basic block #") + Twine(Number) +
1104                  " isn't '" + Token.stringValue() + "'");
1105   return false;
1106 }
1107 
1108 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1109   MachineBasicBlock *MBB;
1110   if (parseMBBReference(MBB))
1111     return true;
1112   Dest = MachineOperand::CreateMBB(MBB);
1113   lex();
1114   return false;
1115 }
1116 
1117 bool MIParser::parseStackFrameIndex(int &FI) {
1118   assert(Token.is(MIToken::StackObject));
1119   unsigned ID;
1120   if (getUnsigned(ID))
1121     return true;
1122   auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1123   if (ObjectInfo == PFS.StackObjectSlots.end())
1124     return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1125                  "'");
1126   StringRef Name;
1127   if (const auto *Alloca =
1128           MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1129     Name = Alloca->getName();
1130   if (!Token.stringValue().empty() && Token.stringValue() != Name)
1131     return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1132                  "' isn't '" + Token.stringValue() + "'");
1133   lex();
1134   FI = ObjectInfo->second;
1135   return false;
1136 }
1137 
1138 bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1139   int FI;
1140   if (parseStackFrameIndex(FI))
1141     return true;
1142   Dest = MachineOperand::CreateFI(FI);
1143   return false;
1144 }
1145 
1146 bool MIParser::parseFixedStackFrameIndex(int &FI) {
1147   assert(Token.is(MIToken::FixedStackObject));
1148   unsigned ID;
1149   if (getUnsigned(ID))
1150     return true;
1151   auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1152   if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1153     return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1154                  Twine(ID) + "'");
1155   lex();
1156   FI = ObjectInfo->second;
1157   return false;
1158 }
1159 
1160 bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1161   int FI;
1162   if (parseFixedStackFrameIndex(FI))
1163     return true;
1164   Dest = MachineOperand::CreateFI(FI);
1165   return false;
1166 }
1167 
1168 bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1169   switch (Token.kind()) {
1170   case MIToken::NamedGlobalValue: {
1171     const Module *M = MF.getFunction()->getParent();
1172     GV = M->getNamedValue(Token.stringValue());
1173     if (!GV)
1174       return error(Twine("use of undefined global value '") + Token.range() +
1175                    "'");
1176     break;
1177   }
1178   case MIToken::GlobalValue: {
1179     unsigned GVIdx;
1180     if (getUnsigned(GVIdx))
1181       return true;
1182     if (GVIdx >= IRSlots.GlobalValues.size())
1183       return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1184                    "'");
1185     GV = IRSlots.GlobalValues[GVIdx];
1186     break;
1187   }
1188   default:
1189     llvm_unreachable("The current token should be a global value");
1190   }
1191   return false;
1192 }
1193 
1194 bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1195   GlobalValue *GV = nullptr;
1196   if (parseGlobalValue(GV))
1197     return true;
1198   lex();
1199   Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1200   if (parseOperandsOffset(Dest))
1201     return true;
1202   return false;
1203 }
1204 
1205 bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1206   assert(Token.is(MIToken::ConstantPoolItem));
1207   unsigned ID;
1208   if (getUnsigned(ID))
1209     return true;
1210   auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1211   if (ConstantInfo == PFS.ConstantPoolSlots.end())
1212     return error("use of undefined constant '%const." + Twine(ID) + "'");
1213   lex();
1214   Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1215   if (parseOperandsOffset(Dest))
1216     return true;
1217   return false;
1218 }
1219 
1220 bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1221   assert(Token.is(MIToken::JumpTableIndex));
1222   unsigned ID;
1223   if (getUnsigned(ID))
1224     return true;
1225   auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1226   if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1227     return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1228   lex();
1229   Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1230   return false;
1231 }
1232 
1233 bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1234   assert(Token.is(MIToken::ExternalSymbol));
1235   const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1236   lex();
1237   Dest = MachineOperand::CreateES(Symbol);
1238   if (parseOperandsOffset(Dest))
1239     return true;
1240   return false;
1241 }
1242 
1243 bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1244   assert(Token.is(MIToken::SubRegisterIndex));
1245   StringRef Name = Token.stringValue();
1246   unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1247   if (SubRegIndex == 0)
1248     return error(Twine("unknown subregister index '") + Name + "'");
1249   lex();
1250   Dest = MachineOperand::CreateImm(SubRegIndex);
1251   return false;
1252 }
1253 
1254 bool MIParser::parseMDNode(MDNode *&Node) {
1255   assert(Token.is(MIToken::exclaim));
1256   auto Loc = Token.location();
1257   lex();
1258   if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1259     return error("expected metadata id after '!'");
1260   unsigned ID;
1261   if (getUnsigned(ID))
1262     return true;
1263   auto NodeInfo = IRSlots.MetadataNodes.find(ID);
1264   if (NodeInfo == IRSlots.MetadataNodes.end())
1265     return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1266   lex();
1267   Node = NodeInfo->second.get();
1268   return false;
1269 }
1270 
1271 bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1272   MDNode *Node = nullptr;
1273   if (parseMDNode(Node))
1274     return true;
1275   Dest = MachineOperand::CreateMetadata(Node);
1276   return false;
1277 }
1278 
1279 bool MIParser::parseCFIOffset(int &Offset) {
1280   if (Token.isNot(MIToken::IntegerLiteral))
1281     return error("expected a cfi offset");
1282   if (Token.integerValue().getMinSignedBits() > 32)
1283     return error("expected a 32 bit integer (the cfi offset is too large)");
1284   Offset = (int)Token.integerValue().getExtValue();
1285   lex();
1286   return false;
1287 }
1288 
1289 bool MIParser::parseCFIRegister(unsigned &Reg) {
1290   if (Token.isNot(MIToken::NamedRegister))
1291     return error("expected a cfi register");
1292   unsigned LLVMReg;
1293   if (parseRegister(LLVMReg))
1294     return true;
1295   const auto *TRI = MF.getSubtarget().getRegisterInfo();
1296   assert(TRI && "Expected target register info");
1297   int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1298   if (DwarfReg < 0)
1299     return error("invalid DWARF register");
1300   Reg = (unsigned)DwarfReg;
1301   lex();
1302   return false;
1303 }
1304 
1305 bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1306   auto Kind = Token.kind();
1307   lex();
1308   auto &MMI = MF.getMMI();
1309   int Offset;
1310   unsigned Reg;
1311   unsigned CFIIndex;
1312   switch (Kind) {
1313   case MIToken::kw_cfi_same_value:
1314     if (parseCFIRegister(Reg))
1315       return true;
1316     CFIIndex =
1317         MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1318     break;
1319   case MIToken::kw_cfi_offset:
1320     if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1321         parseCFIOffset(Offset))
1322       return true;
1323     CFIIndex =
1324         MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1325     break;
1326   case MIToken::kw_cfi_def_cfa_register:
1327     if (parseCFIRegister(Reg))
1328       return true;
1329     CFIIndex =
1330         MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1331     break;
1332   case MIToken::kw_cfi_def_cfa_offset:
1333     if (parseCFIOffset(Offset))
1334       return true;
1335     // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1336     CFIIndex = MMI.addFrameInst(
1337         MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1338     break;
1339   case MIToken::kw_cfi_def_cfa:
1340     if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1341         parseCFIOffset(Offset))
1342       return true;
1343     // NB: MCCFIInstruction::createDefCfa negates the offset.
1344     CFIIndex =
1345         MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1346     break;
1347   default:
1348     // TODO: Parse the other CFI operands.
1349     llvm_unreachable("The current token should be a cfi operand");
1350   }
1351   Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1352   return false;
1353 }
1354 
1355 bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1356   switch (Token.kind()) {
1357   case MIToken::NamedIRBlock: {
1358     BB = dyn_cast_or_null<BasicBlock>(
1359         F.getValueSymbolTable().lookup(Token.stringValue()));
1360     if (!BB)
1361       return error(Twine("use of undefined IR block '") + Token.range() + "'");
1362     break;
1363   }
1364   case MIToken::IRBlock: {
1365     unsigned SlotNumber = 0;
1366     if (getUnsigned(SlotNumber))
1367       return true;
1368     BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1369     if (!BB)
1370       return error(Twine("use of undefined IR block '%ir-block.") +
1371                    Twine(SlotNumber) + "'");
1372     break;
1373   }
1374   default:
1375     llvm_unreachable("The current token should be an IR block reference");
1376   }
1377   return false;
1378 }
1379 
1380 bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1381   assert(Token.is(MIToken::kw_blockaddress));
1382   lex();
1383   if (expectAndConsume(MIToken::lparen))
1384     return true;
1385   if (Token.isNot(MIToken::GlobalValue) &&
1386       Token.isNot(MIToken::NamedGlobalValue))
1387     return error("expected a global value");
1388   GlobalValue *GV = nullptr;
1389   if (parseGlobalValue(GV))
1390     return true;
1391   auto *F = dyn_cast<Function>(GV);
1392   if (!F)
1393     return error("expected an IR function reference");
1394   lex();
1395   if (expectAndConsume(MIToken::comma))
1396     return true;
1397   BasicBlock *BB = nullptr;
1398   if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1399     return error("expected an IR block reference");
1400   if (parseIRBlock(BB, *F))
1401     return true;
1402   lex();
1403   if (expectAndConsume(MIToken::rparen))
1404     return true;
1405   Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1406   if (parseOperandsOffset(Dest))
1407     return true;
1408   return false;
1409 }
1410 
1411 bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1412   assert(Token.is(MIToken::kw_target_index));
1413   lex();
1414   if (expectAndConsume(MIToken::lparen))
1415     return true;
1416   if (Token.isNot(MIToken::Identifier))
1417     return error("expected the name of the target index");
1418   int Index = 0;
1419   if (getTargetIndex(Token.stringValue(), Index))
1420     return error("use of undefined target index '" + Token.stringValue() + "'");
1421   lex();
1422   if (expectAndConsume(MIToken::rparen))
1423     return true;
1424   Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1425   if (parseOperandsOffset(Dest))
1426     return true;
1427   return false;
1428 }
1429 
1430 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1431   assert(Token.is(MIToken::kw_liveout));
1432   const auto *TRI = MF.getSubtarget().getRegisterInfo();
1433   assert(TRI && "Expected target register info");
1434   uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1435   lex();
1436   if (expectAndConsume(MIToken::lparen))
1437     return true;
1438   while (true) {
1439     if (Token.isNot(MIToken::NamedRegister))
1440       return error("expected a named register");
1441     unsigned Reg = 0;
1442     if (parseRegister(Reg))
1443       return true;
1444     lex();
1445     Mask[Reg / 32] |= 1U << (Reg % 32);
1446     // TODO: Report an error if the same register is used more than once.
1447     if (Token.isNot(MIToken::comma))
1448       break;
1449     lex();
1450   }
1451   if (expectAndConsume(MIToken::rparen))
1452     return true;
1453   Dest = MachineOperand::CreateRegLiveOut(Mask);
1454   return false;
1455 }
1456 
1457 bool MIParser::parseMachineOperand(MachineOperand &Dest,
1458                                    Optional<unsigned> &TiedDefIdx) {
1459   switch (Token.kind()) {
1460   case MIToken::kw_implicit:
1461   case MIToken::kw_implicit_define:
1462   case MIToken::kw_def:
1463   case MIToken::kw_dead:
1464   case MIToken::kw_killed:
1465   case MIToken::kw_undef:
1466   case MIToken::kw_internal:
1467   case MIToken::kw_early_clobber:
1468   case MIToken::kw_debug_use:
1469   case MIToken::underscore:
1470   case MIToken::NamedRegister:
1471   case MIToken::VirtualRegister:
1472     return parseRegisterOperand(Dest, TiedDefIdx);
1473   case MIToken::IntegerLiteral:
1474     return parseImmediateOperand(Dest);
1475   case MIToken::IntegerType:
1476     return parseTypedImmediateOperand(Dest);
1477   case MIToken::kw_half:
1478   case MIToken::kw_float:
1479   case MIToken::kw_double:
1480   case MIToken::kw_x86_fp80:
1481   case MIToken::kw_fp128:
1482   case MIToken::kw_ppc_fp128:
1483     return parseFPImmediateOperand(Dest);
1484   case MIToken::MachineBasicBlock:
1485     return parseMBBOperand(Dest);
1486   case MIToken::StackObject:
1487     return parseStackObjectOperand(Dest);
1488   case MIToken::FixedStackObject:
1489     return parseFixedStackObjectOperand(Dest);
1490   case MIToken::GlobalValue:
1491   case MIToken::NamedGlobalValue:
1492     return parseGlobalAddressOperand(Dest);
1493   case MIToken::ConstantPoolItem:
1494     return parseConstantPoolIndexOperand(Dest);
1495   case MIToken::JumpTableIndex:
1496     return parseJumpTableIndexOperand(Dest);
1497   case MIToken::ExternalSymbol:
1498     return parseExternalSymbolOperand(Dest);
1499   case MIToken::SubRegisterIndex:
1500     return parseSubRegisterIndexOperand(Dest);
1501   case MIToken::exclaim:
1502     return parseMetadataOperand(Dest);
1503   case MIToken::kw_cfi_same_value:
1504   case MIToken::kw_cfi_offset:
1505   case MIToken::kw_cfi_def_cfa_register:
1506   case MIToken::kw_cfi_def_cfa_offset:
1507   case MIToken::kw_cfi_def_cfa:
1508     return parseCFIOperand(Dest);
1509   case MIToken::kw_blockaddress:
1510     return parseBlockAddressOperand(Dest);
1511   case MIToken::kw_target_index:
1512     return parseTargetIndexOperand(Dest);
1513   case MIToken::kw_liveout:
1514     return parseLiveoutRegisterMaskOperand(Dest);
1515   case MIToken::Error:
1516     return true;
1517   case MIToken::Identifier:
1518     if (const auto *RegMask = getRegMask(Token.stringValue())) {
1519       Dest = MachineOperand::CreateRegMask(RegMask);
1520       lex();
1521       break;
1522     }
1523   // fallthrough
1524   default:
1525     // FIXME: Parse the MCSymbol machine operand.
1526     return error("expected a machine operand");
1527   }
1528   return false;
1529 }
1530 
1531 bool MIParser::parseMachineOperandAndTargetFlags(
1532     MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1533   unsigned TF = 0;
1534   bool HasTargetFlags = false;
1535   if (Token.is(MIToken::kw_target_flags)) {
1536     HasTargetFlags = true;
1537     lex();
1538     if (expectAndConsume(MIToken::lparen))
1539       return true;
1540     if (Token.isNot(MIToken::Identifier))
1541       return error("expected the name of the target flag");
1542     if (getDirectTargetFlag(Token.stringValue(), TF)) {
1543       if (getBitmaskTargetFlag(Token.stringValue(), TF))
1544         return error("use of undefined target flag '" + Token.stringValue() +
1545                      "'");
1546     }
1547     lex();
1548     while (Token.is(MIToken::comma)) {
1549       lex();
1550       if (Token.isNot(MIToken::Identifier))
1551         return error("expected the name of the target flag");
1552       unsigned BitFlag = 0;
1553       if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1554         return error("use of undefined target flag '" + Token.stringValue() +
1555                      "'");
1556       // TODO: Report an error when using a duplicate bit target flag.
1557       TF |= BitFlag;
1558       lex();
1559     }
1560     if (expectAndConsume(MIToken::rparen))
1561       return true;
1562   }
1563   auto Loc = Token.location();
1564   if (parseMachineOperand(Dest, TiedDefIdx))
1565     return true;
1566   if (!HasTargetFlags)
1567     return false;
1568   if (Dest.isReg())
1569     return error(Loc, "register operands can't have target flags");
1570   Dest.setTargetFlags(TF);
1571   return false;
1572 }
1573 
1574 bool MIParser::parseOffset(int64_t &Offset) {
1575   if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1576     return false;
1577   StringRef Sign = Token.range();
1578   bool IsNegative = Token.is(MIToken::minus);
1579   lex();
1580   if (Token.isNot(MIToken::IntegerLiteral))
1581     return error("expected an integer literal after '" + Sign + "'");
1582   if (Token.integerValue().getMinSignedBits() > 64)
1583     return error("expected 64-bit integer (too large)");
1584   Offset = Token.integerValue().getExtValue();
1585   if (IsNegative)
1586     Offset = -Offset;
1587   lex();
1588   return false;
1589 }
1590 
1591 bool MIParser::parseAlignment(unsigned &Alignment) {
1592   assert(Token.is(MIToken::kw_align));
1593   lex();
1594   if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1595     return error("expected an integer literal after 'align'");
1596   if (getUnsigned(Alignment))
1597     return true;
1598   lex();
1599   return false;
1600 }
1601 
1602 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1603   int64_t Offset = 0;
1604   if (parseOffset(Offset))
1605     return true;
1606   Op.setOffset(Offset);
1607   return false;
1608 }
1609 
1610 bool MIParser::parseIRValue(const Value *&V) {
1611   switch (Token.kind()) {
1612   case MIToken::NamedIRValue: {
1613     V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1614     break;
1615   }
1616   case MIToken::IRValue: {
1617     unsigned SlotNumber = 0;
1618     if (getUnsigned(SlotNumber))
1619       return true;
1620     V = getIRValue(SlotNumber);
1621     break;
1622   }
1623   case MIToken::NamedGlobalValue:
1624   case MIToken::GlobalValue: {
1625     GlobalValue *GV = nullptr;
1626     if (parseGlobalValue(GV))
1627       return true;
1628     V = GV;
1629     break;
1630   }
1631   case MIToken::QuotedIRValue: {
1632     const Constant *C = nullptr;
1633     if (parseIRConstant(Token.location(), Token.stringValue(), C))
1634       return true;
1635     V = C;
1636     break;
1637   }
1638   default:
1639     llvm_unreachable("The current token should be an IR block reference");
1640   }
1641   if (!V)
1642     return error(Twine("use of undefined IR value '") + Token.range() + "'");
1643   return false;
1644 }
1645 
1646 bool MIParser::getUint64(uint64_t &Result) {
1647   assert(Token.hasIntegerValue());
1648   if (Token.integerValue().getActiveBits() > 64)
1649     return error("expected 64-bit integer (too large)");
1650   Result = Token.integerValue().getZExtValue();
1651   return false;
1652 }
1653 
1654 bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1655   const unsigned OldFlags = Flags;
1656   switch (Token.kind()) {
1657   case MIToken::kw_volatile:
1658     Flags |= MachineMemOperand::MOVolatile;
1659     break;
1660   case MIToken::kw_non_temporal:
1661     Flags |= MachineMemOperand::MONonTemporal;
1662     break;
1663   case MIToken::kw_invariant:
1664     Flags |= MachineMemOperand::MOInvariant;
1665     break;
1666   // TODO: parse the target specific memory operand flags.
1667   default:
1668     llvm_unreachable("The current token should be a memory operand flag");
1669   }
1670   if (OldFlags == Flags)
1671     // We know that the same flag is specified more than once when the flags
1672     // weren't modified.
1673     return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1674   lex();
1675   return false;
1676 }
1677 
1678 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1679   switch (Token.kind()) {
1680   case MIToken::kw_stack:
1681     PSV = MF.getPSVManager().getStack();
1682     break;
1683   case MIToken::kw_got:
1684     PSV = MF.getPSVManager().getGOT();
1685     break;
1686   case MIToken::kw_jump_table:
1687     PSV = MF.getPSVManager().getJumpTable();
1688     break;
1689   case MIToken::kw_constant_pool:
1690     PSV = MF.getPSVManager().getConstantPool();
1691     break;
1692   case MIToken::FixedStackObject: {
1693     int FI;
1694     if (parseFixedStackFrameIndex(FI))
1695       return true;
1696     PSV = MF.getPSVManager().getFixedStack(FI);
1697     // The token was already consumed, so use return here instead of break.
1698     return false;
1699   }
1700   case MIToken::StackObject: {
1701     int FI;
1702     if (parseStackFrameIndex(FI))
1703       return true;
1704     PSV = MF.getPSVManager().getFixedStack(FI);
1705     // The token was already consumed, so use return here instead of break.
1706     return false;
1707   }
1708   case MIToken::kw_call_entry: {
1709     lex();
1710     switch (Token.kind()) {
1711     case MIToken::GlobalValue:
1712     case MIToken::NamedGlobalValue: {
1713       GlobalValue *GV = nullptr;
1714       if (parseGlobalValue(GV))
1715         return true;
1716       PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1717       break;
1718     }
1719     case MIToken::ExternalSymbol:
1720       PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1721           MF.createExternalSymbolName(Token.stringValue()));
1722       break;
1723     default:
1724       return error(
1725           "expected a global value or an external symbol after 'call-entry'");
1726     }
1727     break;
1728   }
1729   default:
1730     llvm_unreachable("The current token should be pseudo source value");
1731   }
1732   lex();
1733   return false;
1734 }
1735 
1736 bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1737   if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1738       Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1739       Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1740       Token.is(MIToken::kw_call_entry)) {
1741     const PseudoSourceValue *PSV = nullptr;
1742     if (parseMemoryPseudoSourceValue(PSV))
1743       return true;
1744     int64_t Offset = 0;
1745     if (parseOffset(Offset))
1746       return true;
1747     Dest = MachinePointerInfo(PSV, Offset);
1748     return false;
1749   }
1750   if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1751       Token.isNot(MIToken::GlobalValue) &&
1752       Token.isNot(MIToken::NamedGlobalValue) &&
1753       Token.isNot(MIToken::QuotedIRValue))
1754     return error("expected an IR value reference");
1755   const Value *V = nullptr;
1756   if (parseIRValue(V))
1757     return true;
1758   if (!V->getType()->isPointerTy())
1759     return error("expected a pointer IR value");
1760   lex();
1761   int64_t Offset = 0;
1762   if (parseOffset(Offset))
1763     return true;
1764   Dest = MachinePointerInfo(V, Offset);
1765   return false;
1766 }
1767 
1768 bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1769   if (expectAndConsume(MIToken::lparen))
1770     return true;
1771   unsigned Flags = 0;
1772   while (Token.isMemoryOperandFlag()) {
1773     if (parseMemoryOperandFlag(Flags))
1774       return true;
1775   }
1776   if (Token.isNot(MIToken::Identifier) ||
1777       (Token.stringValue() != "load" && Token.stringValue() != "store"))
1778     return error("expected 'load' or 'store' memory operation");
1779   if (Token.stringValue() == "load")
1780     Flags |= MachineMemOperand::MOLoad;
1781   else
1782     Flags |= MachineMemOperand::MOStore;
1783   lex();
1784 
1785   if (Token.isNot(MIToken::IntegerLiteral))
1786     return error("expected the size integer literal after memory operation");
1787   uint64_t Size;
1788   if (getUint64(Size))
1789     return true;
1790   lex();
1791 
1792   MachinePointerInfo Ptr = MachinePointerInfo();
1793   if (Token.is(MIToken::Identifier)) {
1794     const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1795     if (Token.stringValue() != Word)
1796       return error(Twine("expected '") + Word + "'");
1797     lex();
1798 
1799     if (parseMachinePointerInfo(Ptr))
1800       return true;
1801   }
1802   unsigned BaseAlignment = Size;
1803   AAMDNodes AAInfo;
1804   MDNode *Range = nullptr;
1805   while (consumeIfPresent(MIToken::comma)) {
1806     switch (Token.kind()) {
1807     case MIToken::kw_align:
1808       if (parseAlignment(BaseAlignment))
1809         return true;
1810       break;
1811     case MIToken::md_tbaa:
1812       lex();
1813       if (parseMDNode(AAInfo.TBAA))
1814         return true;
1815       break;
1816     case MIToken::md_alias_scope:
1817       lex();
1818       if (parseMDNode(AAInfo.Scope))
1819         return true;
1820       break;
1821     case MIToken::md_noalias:
1822       lex();
1823       if (parseMDNode(AAInfo.NoAlias))
1824         return true;
1825       break;
1826     case MIToken::md_range:
1827       lex();
1828       if (parseMDNode(Range))
1829         return true;
1830       break;
1831     // TODO: Report an error on duplicate metadata nodes.
1832     default:
1833       return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1834                    "'!noalias' or '!range'");
1835     }
1836   }
1837   if (expectAndConsume(MIToken::rparen))
1838     return true;
1839   Dest =
1840       MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1841   return false;
1842 }
1843 
1844 void MIParser::initNames2InstrOpCodes() {
1845   if (!Names2InstrOpCodes.empty())
1846     return;
1847   const auto *TII = MF.getSubtarget().getInstrInfo();
1848   assert(TII && "Expected target instruction info");
1849   for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1850     Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1851 }
1852 
1853 bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1854   initNames2InstrOpCodes();
1855   auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1856   if (InstrInfo == Names2InstrOpCodes.end())
1857     return true;
1858   OpCode = InstrInfo->getValue();
1859   return false;
1860 }
1861 
1862 void MIParser::initNames2Regs() {
1863   if (!Names2Regs.empty())
1864     return;
1865   // The '%noreg' register is the register 0.
1866   Names2Regs.insert(std::make_pair("noreg", 0));
1867   const auto *TRI = MF.getSubtarget().getRegisterInfo();
1868   assert(TRI && "Expected target register info");
1869   for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1870     bool WasInserted =
1871         Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1872             .second;
1873     (void)WasInserted;
1874     assert(WasInserted && "Expected registers to be unique case-insensitively");
1875   }
1876 }
1877 
1878 bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1879   initNames2Regs();
1880   auto RegInfo = Names2Regs.find(RegName);
1881   if (RegInfo == Names2Regs.end())
1882     return true;
1883   Reg = RegInfo->getValue();
1884   return false;
1885 }
1886 
1887 void MIParser::initNames2RegMasks() {
1888   if (!Names2RegMasks.empty())
1889     return;
1890   const auto *TRI = MF.getSubtarget().getRegisterInfo();
1891   assert(TRI && "Expected target register info");
1892   ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1893   ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1894   assert(RegMasks.size() == RegMaskNames.size());
1895   for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1896     Names2RegMasks.insert(
1897         std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1898 }
1899 
1900 const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1901   initNames2RegMasks();
1902   auto RegMaskInfo = Names2RegMasks.find(Identifier);
1903   if (RegMaskInfo == Names2RegMasks.end())
1904     return nullptr;
1905   return RegMaskInfo->getValue();
1906 }
1907 
1908 void MIParser::initNames2SubRegIndices() {
1909   if (!Names2SubRegIndices.empty())
1910     return;
1911   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1912   for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1913     Names2SubRegIndices.insert(
1914         std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1915 }
1916 
1917 unsigned MIParser::getSubRegIndex(StringRef Name) {
1918   initNames2SubRegIndices();
1919   auto SubRegInfo = Names2SubRegIndices.find(Name);
1920   if (SubRegInfo == Names2SubRegIndices.end())
1921     return 0;
1922   return SubRegInfo->getValue();
1923 }
1924 
1925 static void initSlots2BasicBlocks(
1926     const Function &F,
1927     DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1928   ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1929   MST.incorporateFunction(F);
1930   for (auto &BB : F) {
1931     if (BB.hasName())
1932       continue;
1933     int Slot = MST.getLocalSlot(&BB);
1934     if (Slot == -1)
1935       continue;
1936     Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1937   }
1938 }
1939 
1940 static const BasicBlock *getIRBlockFromSlot(
1941     unsigned Slot,
1942     const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1943   auto BlockInfo = Slots2BasicBlocks.find(Slot);
1944   if (BlockInfo == Slots2BasicBlocks.end())
1945     return nullptr;
1946   return BlockInfo->second;
1947 }
1948 
1949 const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1950   if (Slots2BasicBlocks.empty())
1951     initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1952   return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1953 }
1954 
1955 const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1956   if (&F == MF.getFunction())
1957     return getIRBlock(Slot);
1958   DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1959   initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1960   return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1961 }
1962 
1963 static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1964                            DenseMap<unsigned, const Value *> &Slots2Values) {
1965   int Slot = MST.getLocalSlot(V);
1966   if (Slot == -1)
1967     return;
1968   Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1969 }
1970 
1971 /// Creates the mapping from slot numbers to function's unnamed IR values.
1972 static void initSlots2Values(const Function &F,
1973                              DenseMap<unsigned, const Value *> &Slots2Values) {
1974   ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1975   MST.incorporateFunction(F);
1976   for (const auto &Arg : F.args())
1977     mapValueToSlot(&Arg, MST, Slots2Values);
1978   for (const auto &BB : F) {
1979     mapValueToSlot(&BB, MST, Slots2Values);
1980     for (const auto &I : BB)
1981       mapValueToSlot(&I, MST, Slots2Values);
1982   }
1983 }
1984 
1985 const Value *MIParser::getIRValue(unsigned Slot) {
1986   if (Slots2Values.empty())
1987     initSlots2Values(*MF.getFunction(), Slots2Values);
1988   auto ValueInfo = Slots2Values.find(Slot);
1989   if (ValueInfo == Slots2Values.end())
1990     return nullptr;
1991   return ValueInfo->second;
1992 }
1993 
1994 void MIParser::initNames2TargetIndices() {
1995   if (!Names2TargetIndices.empty())
1996     return;
1997   const auto *TII = MF.getSubtarget().getInstrInfo();
1998   assert(TII && "Expected target instruction info");
1999   auto Indices = TII->getSerializableTargetIndices();
2000   for (const auto &I : Indices)
2001     Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2002 }
2003 
2004 bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2005   initNames2TargetIndices();
2006   auto IndexInfo = Names2TargetIndices.find(Name);
2007   if (IndexInfo == Names2TargetIndices.end())
2008     return true;
2009   Index = IndexInfo->second;
2010   return false;
2011 }
2012 
2013 void MIParser::initNames2DirectTargetFlags() {
2014   if (!Names2DirectTargetFlags.empty())
2015     return;
2016   const auto *TII = MF.getSubtarget().getInstrInfo();
2017   assert(TII && "Expected target instruction info");
2018   auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2019   for (const auto &I : Flags)
2020     Names2DirectTargetFlags.insert(
2021         std::make_pair(StringRef(I.second), I.first));
2022 }
2023 
2024 bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2025   initNames2DirectTargetFlags();
2026   auto FlagInfo = Names2DirectTargetFlags.find(Name);
2027   if (FlagInfo == Names2DirectTargetFlags.end())
2028     return true;
2029   Flag = FlagInfo->second;
2030   return false;
2031 }
2032 
2033 void MIParser::initNames2BitmaskTargetFlags() {
2034   if (!Names2BitmaskTargetFlags.empty())
2035     return;
2036   const auto *TII = MF.getSubtarget().getInstrInfo();
2037   assert(TII && "Expected target instruction info");
2038   auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2039   for (const auto &I : Flags)
2040     Names2BitmaskTargetFlags.insert(
2041         std::make_pair(StringRef(I.second), I.first));
2042 }
2043 
2044 bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2045   initNames2BitmaskTargetFlags();
2046   auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2047   if (FlagInfo == Names2BitmaskTargetFlags.end())
2048     return true;
2049   Flag = FlagInfo->second;
2050   return false;
2051 }
2052 
2053 bool llvm::parseMachineBasicBlockDefinitions(MachineFunction &MF, StringRef Src,
2054                                              PerFunctionMIParsingState &PFS,
2055                                              const SlotMapping &IRSlots,
2056                                              SMDiagnostic &Error) {
2057   SourceMgr SM;
2058   SM.AddNewSourceBuffer(
2059       MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
2060       SMLoc());
2061   return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2062       .parseBasicBlockDefinitions(PFS.MBBSlots);
2063 }
2064 
2065 bool llvm::parseMachineInstructions(MachineFunction &MF, StringRef Src,
2066                                     const PerFunctionMIParsingState &PFS,
2067                                     const SlotMapping &IRSlots,
2068                                     SMDiagnostic &Error) {
2069   SourceMgr SM;
2070   SM.AddNewSourceBuffer(
2071       MemoryBuffer::getMemBuffer(Src, "", /*RequiresNullTerminator=*/false),
2072       SMLoc());
2073   return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseBasicBlocks();
2074 }
2075 
2076 bool llvm::parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
2077                              MachineFunction &MF, StringRef Src,
2078                              const PerFunctionMIParsingState &PFS,
2079                              const SlotMapping &IRSlots, SMDiagnostic &Error) {
2080   return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMBB(MBB);
2081 }
2082 
2083 bool llvm::parseNamedRegisterReference(unsigned &Reg, SourceMgr &SM,
2084                                        MachineFunction &MF, StringRef Src,
2085                                        const PerFunctionMIParsingState &PFS,
2086                                        const SlotMapping &IRSlots,
2087                                        SMDiagnostic &Error) {
2088   return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2089       .parseStandaloneNamedRegister(Reg);
2090 }
2091 
2092 bool llvm::parseVirtualRegisterReference(unsigned &Reg, SourceMgr &SM,
2093                                          MachineFunction &MF, StringRef Src,
2094                                          const PerFunctionMIParsingState &PFS,
2095                                          const SlotMapping &IRSlots,
2096                                          SMDiagnostic &Error) {
2097   return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2098       .parseStandaloneVirtualRegister(Reg);
2099 }
2100 
2101 bool llvm::parseStackObjectReference(int &FI, SourceMgr &SM,
2102                                      MachineFunction &MF, StringRef Src,
2103                                      const PerFunctionMIParsingState &PFS,
2104                                      const SlotMapping &IRSlots,
2105                                      SMDiagnostic &Error) {
2106   return MIParser(SM, MF, Error, Src, PFS, IRSlots)
2107       .parseStandaloneStackObject(FI);
2108 }
2109 
2110 bool llvm::parseMDNode(MDNode *&Node, SourceMgr &SM, MachineFunction &MF,
2111                        StringRef Src, const PerFunctionMIParsingState &PFS,
2112                        const SlotMapping &IRSlots, SMDiagnostic &Error) {
2113   return MIParser(SM, MF, Error, Src, PFS, IRSlots).parseStandaloneMDNode(Node);
2114 }
2115