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