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