1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the parsing of machine instructions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MIRParser/MIParser.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/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Analysis/MemoryLocation.h"
25 #include "llvm/AsmParser/Parser.h"
26 #include "llvm/AsmParser/SlotMapping.h"
27 #include "llvm/CodeGen/LowLevelType.h"
28 #include "llvm/CodeGen/MIRFormatter.h"
29 #include "llvm/CodeGen/MIRPrinter.h"
30 #include "llvm/CodeGen/MachineBasicBlock.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunction.h"
33 #include "llvm/CodeGen/MachineInstr.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineMemOperand.h"
36 #include "llvm/CodeGen/MachineOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/RegisterBank.h"
39 #include "llvm/CodeGen/RegisterBankInfo.h"
40 #include "llvm/CodeGen/TargetInstrInfo.h"
41 #include "llvm/CodeGen/TargetRegisterInfo.h"
42 #include "llvm/CodeGen/TargetSubtargetInfo.h"
43 #include "llvm/IR/BasicBlock.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/DebugInfoMetadata.h"
47 #include "llvm/IR/DebugLoc.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/InstrTypes.h"
50 #include "llvm/IR/Instructions.h"
51 #include "llvm/IR/Intrinsics.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/ModuleSlotTracker.h"
55 #include "llvm/IR/Type.h"
56 #include "llvm/IR/Value.h"
57 #include "llvm/IR/ValueSymbolTable.h"
58 #include "llvm/MC/LaneBitmask.h"
59 #include "llvm/MC/MCContext.h"
60 #include "llvm/MC/MCDwarf.h"
61 #include "llvm/MC/MCInstrDesc.h"
62 #include "llvm/Support/AtomicOrdering.h"
63 #include "llvm/Support/BranchProbability.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/SMLoc.h"
68 #include "llvm/Support/SourceMgr.h"
69 #include "llvm/Target/TargetIntrinsicInfo.h"
70 #include "llvm/Target/TargetMachine.h"
81 void PerTargetMIParsingState::setTarget(
82 const TargetSubtargetInfo
&NewSubtarget
) {
84 // If the subtarget changed, over conservatively assume everything is invalid.
85 if (&Subtarget
== &NewSubtarget
)
88 Names2InstrOpCodes
.clear();
90 Names2RegMasks
.clear();
91 Names2SubRegIndices
.clear();
92 Names2TargetIndices
.clear();
93 Names2DirectTargetFlags
.clear();
94 Names2BitmaskTargetFlags
.clear();
95 Names2MMOTargetFlags
.clear();
97 initNames2RegClasses();
101 void PerTargetMIParsingState::initNames2Regs() {
102 if (!Names2Regs
.empty())
105 // The '%noreg' register is the register 0.
106 Names2Regs
.insert(std::make_pair("noreg", 0));
107 const auto *TRI
= Subtarget
.getRegisterInfo();
108 assert(TRI
&& "Expected target register info");
110 for (unsigned I
= 0, E
= TRI
->getNumRegs(); I
< E
; ++I
) {
112 Names2Regs
.insert(std::make_pair(StringRef(TRI
->getName(I
)).lower(), I
))
115 assert(WasInserted
&& "Expected registers to be unique case-insensitively");
119 bool PerTargetMIParsingState::getRegisterByName(StringRef RegName
,
122 auto RegInfo
= Names2Regs
.find(RegName
);
123 if (RegInfo
== Names2Regs
.end())
125 Reg
= RegInfo
->getValue();
129 void PerTargetMIParsingState::initNames2InstrOpCodes() {
130 if (!Names2InstrOpCodes
.empty())
132 const auto *TII
= Subtarget
.getInstrInfo();
133 assert(TII
&& "Expected target instruction info");
134 for (unsigned I
= 0, E
= TII
->getNumOpcodes(); I
< E
; ++I
)
135 Names2InstrOpCodes
.insert(std::make_pair(StringRef(TII
->getName(I
)), I
));
138 bool PerTargetMIParsingState::parseInstrName(StringRef InstrName
,
140 initNames2InstrOpCodes();
141 auto InstrInfo
= Names2InstrOpCodes
.find(InstrName
);
142 if (InstrInfo
== Names2InstrOpCodes
.end())
144 OpCode
= InstrInfo
->getValue();
148 void PerTargetMIParsingState::initNames2RegMasks() {
149 if (!Names2RegMasks
.empty())
151 const auto *TRI
= Subtarget
.getRegisterInfo();
152 assert(TRI
&& "Expected target register info");
153 ArrayRef
<const uint32_t *> RegMasks
= TRI
->getRegMasks();
154 ArrayRef
<const char *> RegMaskNames
= TRI
->getRegMaskNames();
155 assert(RegMasks
.size() == RegMaskNames
.size());
156 for (size_t I
= 0, E
= RegMasks
.size(); I
< E
; ++I
)
157 Names2RegMasks
.insert(
158 std::make_pair(StringRef(RegMaskNames
[I
]).lower(), RegMasks
[I
]));
161 const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier
) {
162 initNames2RegMasks();
163 auto RegMaskInfo
= Names2RegMasks
.find(Identifier
);
164 if (RegMaskInfo
== Names2RegMasks
.end())
166 return RegMaskInfo
->getValue();
169 void PerTargetMIParsingState::initNames2SubRegIndices() {
170 if (!Names2SubRegIndices
.empty())
172 const TargetRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
173 for (unsigned I
= 1, E
= TRI
->getNumSubRegIndices(); I
< E
; ++I
)
174 Names2SubRegIndices
.insert(
175 std::make_pair(TRI
->getSubRegIndexName(I
), I
));
178 unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name
) {
179 initNames2SubRegIndices();
180 auto SubRegInfo
= Names2SubRegIndices
.find(Name
);
181 if (SubRegInfo
== Names2SubRegIndices
.end())
183 return SubRegInfo
->getValue();
186 void PerTargetMIParsingState::initNames2TargetIndices() {
187 if (!Names2TargetIndices
.empty())
189 const auto *TII
= Subtarget
.getInstrInfo();
190 assert(TII
&& "Expected target instruction info");
191 auto Indices
= TII
->getSerializableTargetIndices();
192 for (const auto &I
: Indices
)
193 Names2TargetIndices
.insert(std::make_pair(StringRef(I
.second
), I
.first
));
196 bool PerTargetMIParsingState::getTargetIndex(StringRef Name
, int &Index
) {
197 initNames2TargetIndices();
198 auto IndexInfo
= Names2TargetIndices
.find(Name
);
199 if (IndexInfo
== Names2TargetIndices
.end())
201 Index
= IndexInfo
->second
;
205 void PerTargetMIParsingState::initNames2DirectTargetFlags() {
206 if (!Names2DirectTargetFlags
.empty())
209 const auto *TII
= Subtarget
.getInstrInfo();
210 assert(TII
&& "Expected target instruction info");
211 auto Flags
= TII
->getSerializableDirectMachineOperandTargetFlags();
212 for (const auto &I
: Flags
)
213 Names2DirectTargetFlags
.insert(
214 std::make_pair(StringRef(I
.second
), I
.first
));
217 bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name
,
219 initNames2DirectTargetFlags();
220 auto FlagInfo
= Names2DirectTargetFlags
.find(Name
);
221 if (FlagInfo
== Names2DirectTargetFlags
.end())
223 Flag
= FlagInfo
->second
;
227 void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
228 if (!Names2BitmaskTargetFlags
.empty())
231 const auto *TII
= Subtarget
.getInstrInfo();
232 assert(TII
&& "Expected target instruction info");
233 auto Flags
= TII
->getSerializableBitmaskMachineOperandTargetFlags();
234 for (const auto &I
: Flags
)
235 Names2BitmaskTargetFlags
.insert(
236 std::make_pair(StringRef(I
.second
), I
.first
));
239 bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name
,
241 initNames2BitmaskTargetFlags();
242 auto FlagInfo
= Names2BitmaskTargetFlags
.find(Name
);
243 if (FlagInfo
== Names2BitmaskTargetFlags
.end())
245 Flag
= FlagInfo
->second
;
249 void PerTargetMIParsingState::initNames2MMOTargetFlags() {
250 if (!Names2MMOTargetFlags
.empty())
253 const auto *TII
= Subtarget
.getInstrInfo();
254 assert(TII
&& "Expected target instruction info");
255 auto Flags
= TII
->getSerializableMachineMemOperandTargetFlags();
256 for (const auto &I
: Flags
)
257 Names2MMOTargetFlags
.insert(std::make_pair(StringRef(I
.second
), I
.first
));
260 bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name
,
261 MachineMemOperand::Flags
&Flag
) {
262 initNames2MMOTargetFlags();
263 auto FlagInfo
= Names2MMOTargetFlags
.find(Name
);
264 if (FlagInfo
== Names2MMOTargetFlags
.end())
266 Flag
= FlagInfo
->second
;
270 void PerTargetMIParsingState::initNames2RegClasses() {
271 if (!Names2RegClasses
.empty())
274 const TargetRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
275 for (unsigned I
= 0, E
= TRI
->getNumRegClasses(); I
< E
; ++I
) {
276 const auto *RC
= TRI
->getRegClass(I
);
277 Names2RegClasses
.insert(
278 std::make_pair(StringRef(TRI
->getRegClassName(RC
)).lower(), RC
));
282 void PerTargetMIParsingState::initNames2RegBanks() {
283 if (!Names2RegBanks
.empty())
286 const RegisterBankInfo
*RBI
= Subtarget
.getRegBankInfo();
287 // If the target does not support GlobalISel, we may not have a
288 // register bank info.
292 for (unsigned I
= 0, E
= RBI
->getNumRegBanks(); I
< E
; ++I
) {
293 const auto &RegBank
= RBI
->getRegBank(I
);
294 Names2RegBanks
.insert(
295 std::make_pair(StringRef(RegBank
.getName()).lower(), &RegBank
));
299 const TargetRegisterClass
*
300 PerTargetMIParsingState::getRegClass(StringRef Name
) {
301 auto RegClassInfo
= Names2RegClasses
.find(Name
);
302 if (RegClassInfo
== Names2RegClasses
.end())
304 return RegClassInfo
->getValue();
307 const RegisterBank
*PerTargetMIParsingState::getRegBank(StringRef Name
) {
308 auto RegBankInfo
= Names2RegBanks
.find(Name
);
309 if (RegBankInfo
== Names2RegBanks
.end())
311 return RegBankInfo
->getValue();
314 PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction
&MF
,
315 SourceMgr
&SM
, const SlotMapping
&IRSlots
, PerTargetMIParsingState
&T
)
316 : MF(MF
), SM(&SM
), IRSlots(IRSlots
), Target(T
) {
319 VRegInfo
&PerFunctionMIParsingState::getVRegInfo(Register Num
) {
320 auto I
= VRegInfos
.insert(std::make_pair(Num
, nullptr));
322 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
323 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
324 Info
->VReg
= MRI
.createIncompleteVirtualRegister();
325 I
.first
->second
= Info
;
327 return *I
.first
->second
;
330 VRegInfo
&PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName
) {
331 assert(RegName
!= "" && "Expected named reg.");
333 auto I
= VRegInfosNamed
.insert(std::make_pair(RegName
.str(), nullptr));
335 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
336 Info
->VReg
= MF
.getRegInfo().createIncompleteVirtualRegister(RegName
);
337 I
.first
->second
= Info
;
339 return *I
.first
->second
;
342 static void mapValueToSlot(const Value
*V
, ModuleSlotTracker
&MST
,
343 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
344 int Slot
= MST
.getLocalSlot(V
);
347 Slots2Values
.insert(std::make_pair(unsigned(Slot
), V
));
350 /// Creates the mapping from slot numbers to function's unnamed IR values.
351 static void initSlots2Values(const Function
&F
,
352 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
353 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
354 MST
.incorporateFunction(F
);
355 for (const auto &Arg
: F
.args())
356 mapValueToSlot(&Arg
, MST
, Slots2Values
);
357 for (const auto &BB
: F
) {
358 mapValueToSlot(&BB
, MST
, Slots2Values
);
359 for (const auto &I
: BB
)
360 mapValueToSlot(&I
, MST
, Slots2Values
);
364 const Value
* PerFunctionMIParsingState::getIRValue(unsigned Slot
) {
365 if (Slots2Values
.empty())
366 initSlots2Values(MF
.getFunction(), Slots2Values
);
367 return Slots2Values
.lookup(Slot
);
372 /// A wrapper struct around the 'MachineOperand' struct that includes a source
373 /// range and other attributes.
374 struct ParsedMachineOperand
{
375 MachineOperand Operand
;
376 StringRef::iterator Begin
;
377 StringRef::iterator End
;
378 std::optional
<unsigned> TiedDefIdx
;
380 ParsedMachineOperand(const MachineOperand
&Operand
, StringRef::iterator Begin
,
381 StringRef::iterator End
,
382 std::optional
<unsigned> &TiedDefIdx
)
383 : Operand(Operand
), Begin(Begin
), End(End
), TiedDefIdx(TiedDefIdx
) {
385 assert(Operand
.isReg() && Operand
.isUse() &&
386 "Only used register operands can be tied");
393 StringRef Source
, CurrentSource
;
396 PerFunctionMIParsingState
&PFS
;
397 /// Maps from slot numbers to function's unnamed basic blocks.
398 DenseMap
<unsigned, const BasicBlock
*> Slots2BasicBlocks
;
401 MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
403 MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
404 StringRef Source
, SMRange SourceRange
);
406 /// \p SkipChar gives the number of characters to skip before looking
407 /// for the next token.
408 void lex(unsigned SkipChar
= 0);
410 /// Report an error at the current location with the given message.
412 /// This function always return true.
413 bool error(const Twine
&Msg
);
415 /// Report an error at the given location with the given message.
417 /// This function always return true.
418 bool error(StringRef::iterator Loc
, const Twine
&Msg
);
421 parseBasicBlockDefinitions(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
422 bool parseBasicBlocks();
423 bool parse(MachineInstr
*&MI
);
424 bool parseStandaloneMBB(MachineBasicBlock
*&MBB
);
425 bool parseStandaloneNamedRegister(Register
&Reg
);
426 bool parseStandaloneVirtualRegister(VRegInfo
*&Info
);
427 bool parseStandaloneRegister(Register
&Reg
);
428 bool parseStandaloneStackObject(int &FI
);
429 bool parseStandaloneMDNode(MDNode
*&Node
);
430 bool parseMachineMetadata();
431 bool parseMDTuple(MDNode
*&MD
, bool IsDistinct
);
432 bool parseMDNodeVector(SmallVectorImpl
<Metadata
*> &Elts
);
433 bool parseMetadata(Metadata
*&MD
);
436 parseBasicBlockDefinition(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
437 bool parseBasicBlock(MachineBasicBlock
&MBB
,
438 MachineBasicBlock
*&AddFalthroughFrom
);
439 bool parseBasicBlockLiveins(MachineBasicBlock
&MBB
);
440 bool parseBasicBlockSuccessors(MachineBasicBlock
&MBB
);
442 bool parseNamedRegister(Register
&Reg
);
443 bool parseVirtualRegister(VRegInfo
*&Info
);
444 bool parseNamedVirtualRegister(VRegInfo
*&Info
);
445 bool parseRegister(Register
&Reg
, VRegInfo
*&VRegInfo
);
446 bool parseRegisterFlag(unsigned &Flags
);
447 bool parseRegisterClassOrBank(VRegInfo
&RegInfo
);
448 bool parseSubRegisterIndex(unsigned &SubReg
);
449 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx
);
450 bool parseRegisterOperand(MachineOperand
&Dest
,
451 std::optional
<unsigned> &TiedDefIdx
,
453 bool parseImmediateOperand(MachineOperand
&Dest
);
454 bool parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
456 bool parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
);
457 bool parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
);
458 bool parseTypedImmediateOperand(MachineOperand
&Dest
);
459 bool parseFPImmediateOperand(MachineOperand
&Dest
);
460 bool parseMBBReference(MachineBasicBlock
*&MBB
);
461 bool parseMBBOperand(MachineOperand
&Dest
);
462 bool parseStackFrameIndex(int &FI
);
463 bool parseStackObjectOperand(MachineOperand
&Dest
);
464 bool parseFixedStackFrameIndex(int &FI
);
465 bool parseFixedStackObjectOperand(MachineOperand
&Dest
);
466 bool parseGlobalValue(GlobalValue
*&GV
);
467 bool parseGlobalAddressOperand(MachineOperand
&Dest
);
468 bool parseConstantPoolIndexOperand(MachineOperand
&Dest
);
469 bool parseSubRegisterIndexOperand(MachineOperand
&Dest
);
470 bool parseJumpTableIndexOperand(MachineOperand
&Dest
);
471 bool parseExternalSymbolOperand(MachineOperand
&Dest
);
472 bool parseMCSymbolOperand(MachineOperand
&Dest
);
473 [[nodiscard
]] bool parseMDNode(MDNode
*&Node
);
474 bool parseDIExpression(MDNode
*&Expr
);
475 bool parseDILocation(MDNode
*&Expr
);
476 bool parseMetadataOperand(MachineOperand
&Dest
);
477 bool parseCFIOffset(int &Offset
);
478 bool parseCFIRegister(Register
&Reg
);
479 bool parseCFIAddressSpace(unsigned &AddressSpace
);
480 bool parseCFIEscapeValues(std::string
& Values
);
481 bool parseCFIOperand(MachineOperand
&Dest
);
482 bool parseIRBlock(BasicBlock
*&BB
, const Function
&F
);
483 bool parseBlockAddressOperand(MachineOperand
&Dest
);
484 bool parseIntrinsicOperand(MachineOperand
&Dest
);
485 bool parsePredicateOperand(MachineOperand
&Dest
);
486 bool parseShuffleMaskOperand(MachineOperand
&Dest
);
487 bool parseTargetIndexOperand(MachineOperand
&Dest
);
488 bool parseDbgInstrRefOperand(MachineOperand
&Dest
);
489 bool parseCustomRegisterMaskOperand(MachineOperand
&Dest
);
490 bool parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
);
491 bool parseMachineOperand(const unsigned OpCode
, const unsigned OpIdx
,
492 MachineOperand
&Dest
,
493 std::optional
<unsigned> &TiedDefIdx
);
494 bool parseMachineOperandAndTargetFlags(const unsigned OpCode
,
495 const unsigned OpIdx
,
496 MachineOperand
&Dest
,
497 std::optional
<unsigned> &TiedDefIdx
);
498 bool parseOffset(int64_t &Offset
);
499 bool parseIRBlockAddressTaken(BasicBlock
*&BB
);
500 bool parseAlignment(uint64_t &Alignment
);
501 bool parseAddrspace(unsigned &Addrspace
);
502 bool parseSectionID(std::optional
<MBBSectionID
> &SID
);
503 bool parseBBID(std::optional
<UniqueBBID
> &BBID
);
504 bool parseCallFrameSize(unsigned &CallFrameSize
);
505 bool parseOperandsOffset(MachineOperand
&Op
);
506 bool parseIRValue(const Value
*&V
);
507 bool parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
);
508 bool parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
);
509 bool parseMachinePointerInfo(MachinePointerInfo
&Dest
);
510 bool parseOptionalScope(LLVMContext
&Context
, SyncScope::ID
&SSID
);
511 bool parseOptionalAtomicOrdering(AtomicOrdering
&Order
);
512 bool parseMachineMemoryOperand(MachineMemOperand
*&Dest
);
513 bool parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
);
514 bool parseHeapAllocMarker(MDNode
*&Node
);
515 bool parsePCSections(MDNode
*&Node
);
517 bool parseTargetImmMnemonic(const unsigned OpCode
, const unsigned OpIdx
,
518 MachineOperand
&Dest
, const MIRFormatter
&MF
);
521 /// Convert the integer literal in the current token into an unsigned integer.
523 /// Return true if an error occurred.
524 bool getUnsigned(unsigned &Result
);
526 /// Convert the integer literal in the current token into an uint64.
528 /// Return true if an error occurred.
529 bool getUint64(uint64_t &Result
);
531 /// Convert the hexadecimal literal in the current token into an unsigned
532 /// APInt with a minimum bitwidth required to represent the value.
534 /// Return true if the literal does not represent an integer value.
535 bool getHexUint(APInt
&Result
);
537 /// If the current token is of the given kind, consume it and return false.
538 /// Otherwise report an error and return true.
539 bool expectAndConsume(MIToken::TokenKind TokenKind
);
541 /// If the current token is of the given kind, consume it and return true.
542 /// Otherwise return false.
543 bool consumeIfPresent(MIToken::TokenKind TokenKind
);
545 bool parseInstruction(unsigned &OpCode
, unsigned &Flags
);
547 bool assignRegisterTies(MachineInstr
&MI
,
548 ArrayRef
<ParsedMachineOperand
> Operands
);
550 bool verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
551 const MCInstrDesc
&MCID
);
553 const BasicBlock
*getIRBlock(unsigned Slot
);
554 const BasicBlock
*getIRBlock(unsigned Slot
, const Function
&F
);
556 /// Get or create an MCSymbol for a given name.
557 MCSymbol
*getOrCreateMCSymbol(StringRef Name
);
559 /// parseStringConstant
560 /// ::= StringConstant
561 bool parseStringConstant(std::string
&Result
);
563 /// Map the location in the MI string to the corresponding location specified
564 /// in `SourceRange`.
565 SMLoc
mapSMLoc(StringRef::iterator Loc
);
568 } // end anonymous namespace
570 MIParser::MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
572 : MF(PFS
.MF
), Error(Error
), Source(Source
), CurrentSource(Source
), PFS(PFS
)
575 MIParser::MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
576 StringRef Source
, SMRange SourceRange
)
577 : MF(PFS
.MF
), Error(Error
), Source(Source
), CurrentSource(Source
),
578 SourceRange(SourceRange
), PFS(PFS
) {}
580 void MIParser::lex(unsigned SkipChar
) {
581 CurrentSource
= lexMIToken(
582 CurrentSource
.slice(SkipChar
, StringRef::npos
), Token
,
583 [this](StringRef::iterator Loc
, const Twine
&Msg
) { error(Loc
, Msg
); });
586 bool MIParser::error(const Twine
&Msg
) { return error(Token
.location(), Msg
); }
588 bool MIParser::error(StringRef::iterator Loc
, const Twine
&Msg
) {
589 const SourceMgr
&SM
= *PFS
.SM
;
590 assert(Loc
>= Source
.data() && Loc
<= (Source
.data() + Source
.size()));
591 const MemoryBuffer
&Buffer
= *SM
.getMemoryBuffer(SM
.getMainFileID());
592 if (Loc
>= Buffer
.getBufferStart() && Loc
<= Buffer
.getBufferEnd()) {
593 // Create an ordinary diagnostic when the source manager's buffer is the
595 Error
= SM
.GetMessage(SMLoc::getFromPointer(Loc
), SourceMgr::DK_Error
, Msg
);
598 // Create a diagnostic for a YAML string literal.
599 Error
= SMDiagnostic(SM
, SMLoc(), Buffer
.getBufferIdentifier(), 1,
600 Loc
- Source
.data(), SourceMgr::DK_Error
, Msg
.str(),
601 Source
, std::nullopt
, std::nullopt
);
605 SMLoc
MIParser::mapSMLoc(StringRef::iterator Loc
) {
606 assert(SourceRange
.isValid() && "Invalid source range");
607 assert(Loc
>= Source
.data() && Loc
<= (Source
.data() + Source
.size()));
608 return SMLoc::getFromPointer(SourceRange
.Start
.getPointer() +
609 (Loc
- Source
.data()));
612 typedef function_ref
<bool(StringRef::iterator Loc
, const Twine
&)>
615 static const char *toString(MIToken::TokenKind TokenKind
) {
623 case MIToken::lparen
:
625 case MIToken::rparen
:
628 return "<unknown token>";
632 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind
) {
633 if (Token
.isNot(TokenKind
))
634 return error(Twine("expected ") + toString(TokenKind
));
639 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind
) {
640 if (Token
.isNot(TokenKind
))
646 // Parse Machine Basic Block Section ID.
647 bool MIParser::parseSectionID(std::optional
<MBBSectionID
> &SID
) {
648 assert(Token
.is(MIToken::kw_bbsections
));
650 if (Token
.is(MIToken::IntegerLiteral
)) {
652 if (getUnsigned(Value
))
653 return error("Unknown Section ID");
654 SID
= MBBSectionID
{Value
};
656 const StringRef
&S
= Token
.stringValue();
657 if (S
== "Exception")
658 SID
= MBBSectionID::ExceptionSectionID
;
659 else if (S
== "Cold")
660 SID
= MBBSectionID::ColdSectionID
;
662 return error("Unknown Section ID");
668 // Parse Machine Basic Block ID.
669 bool MIParser::parseBBID(std::optional
<UniqueBBID
> &BBID
) {
670 assert(Token
.is(MIToken::kw_bb_id
));
673 unsigned CloneID
= 0;
674 if (getUnsigned(BaseID
))
675 return error("Unknown BB ID");
677 if (Token
.is(MIToken::IntegerLiteral
)) {
678 if (getUnsigned(CloneID
))
679 return error("Unknown Clone ID");
682 BBID
= {BaseID
, CloneID
};
686 // Parse basic block call frame size.
687 bool MIParser::parseCallFrameSize(unsigned &CallFrameSize
) {
688 assert(Token
.is(MIToken::kw_call_frame_size
));
691 if (getUnsigned(Value
))
692 return error("Unknown call frame size");
693 CallFrameSize
= Value
;
698 bool MIParser::parseBasicBlockDefinition(
699 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
700 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
704 auto Loc
= Token
.location();
705 auto Name
= Token
.stringValue();
707 bool MachineBlockAddressTaken
= false;
708 BasicBlock
*AddressTakenIRBlock
= nullptr;
709 bool IsLandingPad
= false;
710 bool IsInlineAsmBrIndirectTarget
= false;
711 bool IsEHFuncletEntry
= false;
712 std::optional
<MBBSectionID
> SectionID
;
713 uint64_t Alignment
= 0;
714 std::optional
<UniqueBBID
> BBID
;
715 unsigned CallFrameSize
= 0;
716 BasicBlock
*BB
= nullptr;
717 if (consumeIfPresent(MIToken::lparen
)) {
719 // TODO: Report an error when multiple same attributes are specified.
720 switch (Token
.kind()) {
721 case MIToken::kw_machine_block_address_taken
:
722 MachineBlockAddressTaken
= true;
725 case MIToken::kw_ir_block_address_taken
:
726 if (parseIRBlockAddressTaken(AddressTakenIRBlock
))
729 case MIToken::kw_landing_pad
:
733 case MIToken::kw_inlineasm_br_indirect_target
:
734 IsInlineAsmBrIndirectTarget
= true;
737 case MIToken::kw_ehfunclet_entry
:
738 IsEHFuncletEntry
= true;
741 case MIToken::kw_align
:
742 if (parseAlignment(Alignment
))
745 case MIToken::IRBlock
:
746 case MIToken::NamedIRBlock
:
747 // TODO: Report an error when both name and ir block are specified.
748 if (parseIRBlock(BB
, MF
.getFunction()))
752 case MIToken::kw_bbsections
:
753 if (parseSectionID(SectionID
))
756 case MIToken::kw_bb_id
:
760 case MIToken::kw_call_frame_size
:
761 if (parseCallFrameSize(CallFrameSize
))
767 } while (consumeIfPresent(MIToken::comma
));
768 if (expectAndConsume(MIToken::rparen
))
771 if (expectAndConsume(MIToken::colon
))
775 BB
= dyn_cast_or_null
<BasicBlock
>(
776 MF
.getFunction().getValueSymbolTable()->lookup(Name
));
778 return error(Loc
, Twine("basic block '") + Name
+
779 "' is not defined in the function '" +
782 auto *MBB
= MF
.CreateMachineBasicBlock(BB
);
783 MF
.insert(MF
.end(), MBB
);
784 bool WasInserted
= MBBSlots
.insert(std::make_pair(ID
, MBB
)).second
;
786 return error(Loc
, Twine("redefinition of machine basic block with id #") +
789 MBB
->setAlignment(Align(Alignment
));
790 if (MachineBlockAddressTaken
)
791 MBB
->setMachineBlockAddressTaken();
792 if (AddressTakenIRBlock
)
793 MBB
->setAddressTakenIRBlock(AddressTakenIRBlock
);
794 MBB
->setIsEHPad(IsLandingPad
);
795 MBB
->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget
);
796 MBB
->setIsEHFuncletEntry(IsEHFuncletEntry
);
798 MBB
->setSectionID(*SectionID
);
799 MF
.setBBSectionsType(BasicBlockSection::List
);
801 if (BBID
.has_value()) {
802 // BBSectionsType is set to `List` if any basic blocks has `SectionID`.
803 // Here, we set it to `Labels` if it hasn't been set above.
804 if (!MF
.hasBBSections())
805 MF
.setBBSectionsType(BasicBlockSection::Labels
);
806 MBB
->setBBID(BBID
.value());
808 MBB
->setCallFrameSize(CallFrameSize
);
812 bool MIParser::parseBasicBlockDefinitions(
813 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
815 // Skip until the first machine basic block.
816 while (Token
.is(MIToken::Newline
))
818 if (Token
.isErrorOrEOF())
819 return Token
.isError();
820 if (Token
.isNot(MIToken::MachineBasicBlockLabel
))
821 return error("expected a basic block definition before instructions");
822 unsigned BraceDepth
= 0;
824 if (parseBasicBlockDefinition(MBBSlots
))
826 bool IsAfterNewline
= false;
827 // Skip until the next machine basic block.
829 if ((Token
.is(MIToken::MachineBasicBlockLabel
) && IsAfterNewline
) ||
830 Token
.isErrorOrEOF())
832 else if (Token
.is(MIToken::MachineBasicBlockLabel
))
833 return error("basic block definition should be located at the start of "
835 else if (consumeIfPresent(MIToken::Newline
)) {
836 IsAfterNewline
= true;
839 IsAfterNewline
= false;
840 if (Token
.is(MIToken::lbrace
))
842 if (Token
.is(MIToken::rbrace
)) {
844 return error("extraneous closing brace ('}')");
849 // Verify that we closed all of the '{' at the end of a file or a block.
850 if (!Token
.isError() && BraceDepth
)
851 return error("expected '}'"); // FIXME: Report a note that shows '{'.
852 } while (!Token
.isErrorOrEOF());
853 return Token
.isError();
856 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock
&MBB
) {
857 assert(Token
.is(MIToken::kw_liveins
));
859 if (expectAndConsume(MIToken::colon
))
861 if (Token
.isNewlineOrEOF()) // Allow an empty list of liveins.
864 if (Token
.isNot(MIToken::NamedRegister
))
865 return error("expected a named register");
867 if (parseNamedRegister(Reg
))
870 LaneBitmask Mask
= LaneBitmask::getAll();
871 if (consumeIfPresent(MIToken::colon
)) {
873 if (Token
.isNot(MIToken::IntegerLiteral
) &&
874 Token
.isNot(MIToken::HexLiteral
))
875 return error("expected a lane mask");
876 static_assert(sizeof(LaneBitmask::Type
) == sizeof(uint64_t),
877 "Use correct get-function for lane mask");
880 return error("invalid lane mask value");
881 Mask
= LaneBitmask(V
);
884 MBB
.addLiveIn(Reg
, Mask
);
885 } while (consumeIfPresent(MIToken::comma
));
889 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock
&MBB
) {
890 assert(Token
.is(MIToken::kw_successors
));
892 if (expectAndConsume(MIToken::colon
))
894 if (Token
.isNewlineOrEOF()) // Allow an empty list of successors.
897 if (Token
.isNot(MIToken::MachineBasicBlock
))
898 return error("expected a machine basic block reference");
899 MachineBasicBlock
*SuccMBB
= nullptr;
900 if (parseMBBReference(SuccMBB
))
904 if (consumeIfPresent(MIToken::lparen
)) {
905 if (Token
.isNot(MIToken::IntegerLiteral
) &&
906 Token
.isNot(MIToken::HexLiteral
))
907 return error("expected an integer literal after '('");
908 if (getUnsigned(Weight
))
911 if (expectAndConsume(MIToken::rparen
))
914 MBB
.addSuccessor(SuccMBB
, BranchProbability::getRaw(Weight
));
915 } while (consumeIfPresent(MIToken::comma
));
916 MBB
.normalizeSuccProbs();
920 bool MIParser::parseBasicBlock(MachineBasicBlock
&MBB
,
921 MachineBasicBlock
*&AddFalthroughFrom
) {
922 // Skip the definition.
923 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
925 if (consumeIfPresent(MIToken::lparen
)) {
926 while (Token
.isNot(MIToken::rparen
) && !Token
.isErrorOrEOF())
928 consumeIfPresent(MIToken::rparen
);
930 consumeIfPresent(MIToken::colon
);
932 // Parse the liveins and successors.
933 // N.B: Multiple lists of successors and liveins are allowed and they're
940 // liveins: $edi, $esi
941 bool ExplicitSuccessors
= false;
943 if (Token
.is(MIToken::kw_successors
)) {
944 if (parseBasicBlockSuccessors(MBB
))
946 ExplicitSuccessors
= true;
947 } else if (Token
.is(MIToken::kw_liveins
)) {
948 if (parseBasicBlockLiveins(MBB
))
950 } else if (consumeIfPresent(MIToken::Newline
)) {
954 if (!Token
.isNewlineOrEOF())
955 return error("expected line break at the end of a list");
959 // Parse the instructions.
960 bool IsInBundle
= false;
961 MachineInstr
*PrevMI
= nullptr;
962 while (!Token
.is(MIToken::MachineBasicBlockLabel
) &&
963 !Token
.is(MIToken::Eof
)) {
964 if (consumeIfPresent(MIToken::Newline
))
966 if (consumeIfPresent(MIToken::rbrace
)) {
967 // The first parsing pass should verify that all closing '}' have an
973 MachineInstr
*MI
= nullptr;
976 MBB
.insert(MBB
.end(), MI
);
978 PrevMI
->setFlag(MachineInstr::BundledSucc
);
979 MI
->setFlag(MachineInstr::BundledPred
);
982 if (Token
.is(MIToken::lbrace
)) {
984 return error("nested instruction bundles are not allowed");
986 // This instruction is the start of the bundle.
987 MI
->setFlag(MachineInstr::BundledSucc
);
989 if (!Token
.is(MIToken::Newline
))
990 // The next instruction can be on the same line.
993 assert(Token
.isNewlineOrEOF() && "MI is not fully parsed");
997 // Construct successor list by searching for basic block machine operands.
998 if (!ExplicitSuccessors
) {
999 SmallVector
<MachineBasicBlock
*,4> Successors
;
1001 guessSuccessors(MBB
, Successors
, IsFallthrough
);
1002 for (MachineBasicBlock
*Succ
: Successors
)
1003 MBB
.addSuccessor(Succ
);
1005 if (IsFallthrough
) {
1006 AddFalthroughFrom
= &MBB
;
1008 MBB
.normalizeSuccProbs();
1015 bool MIParser::parseBasicBlocks() {
1017 // Skip until the first machine basic block.
1018 while (Token
.is(MIToken::Newline
))
1020 if (Token
.isErrorOrEOF())
1021 return Token
.isError();
1022 // The first parsing pass should have verified that this token is a MBB label
1023 // in the 'parseBasicBlockDefinitions' method.
1024 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
1025 MachineBasicBlock
*AddFalthroughFrom
= nullptr;
1027 MachineBasicBlock
*MBB
= nullptr;
1028 if (parseMBBReference(MBB
))
1030 if (AddFalthroughFrom
) {
1031 if (!AddFalthroughFrom
->isSuccessor(MBB
))
1032 AddFalthroughFrom
->addSuccessor(MBB
);
1033 AddFalthroughFrom
->normalizeSuccProbs();
1034 AddFalthroughFrom
= nullptr;
1036 if (parseBasicBlock(*MBB
, AddFalthroughFrom
))
1038 // The method 'parseBasicBlock' should parse the whole block until the next
1039 // block or the end of file.
1040 assert(Token
.is(MIToken::MachineBasicBlockLabel
) || Token
.is(MIToken::Eof
));
1041 } while (Token
.isNot(MIToken::Eof
));
1045 bool MIParser::parse(MachineInstr
*&MI
) {
1046 // Parse any register operands before '='
1047 MachineOperand MO
= MachineOperand::CreateImm(0);
1048 SmallVector
<ParsedMachineOperand
, 8> Operands
;
1049 while (Token
.isRegister() || Token
.isRegisterFlag()) {
1050 auto Loc
= Token
.location();
1051 std::optional
<unsigned> TiedDefIdx
;
1052 if (parseRegisterOperand(MO
, TiedDefIdx
, /*IsDef=*/true))
1055 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
1056 if (Token
.isNot(MIToken::comma
))
1060 if (!Operands
.empty() && expectAndConsume(MIToken::equal
))
1063 unsigned OpCode
, Flags
= 0;
1064 if (Token
.isError() || parseInstruction(OpCode
, Flags
))
1067 // Parse the remaining machine operands.
1068 while (!Token
.isNewlineOrEOF() && Token
.isNot(MIToken::kw_pre_instr_symbol
) &&
1069 Token
.isNot(MIToken::kw_post_instr_symbol
) &&
1070 Token
.isNot(MIToken::kw_heap_alloc_marker
) &&
1071 Token
.isNot(MIToken::kw_pcsections
) &&
1072 Token
.isNot(MIToken::kw_cfi_type
) &&
1073 Token
.isNot(MIToken::kw_debug_location
) &&
1074 Token
.isNot(MIToken::kw_debug_instr_number
) &&
1075 Token
.isNot(MIToken::coloncolon
) && Token
.isNot(MIToken::lbrace
)) {
1076 auto Loc
= Token
.location();
1077 std::optional
<unsigned> TiedDefIdx
;
1078 if (parseMachineOperandAndTargetFlags(OpCode
, Operands
.size(), MO
, TiedDefIdx
))
1081 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
1082 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
1083 Token
.is(MIToken::lbrace
))
1085 if (Token
.isNot(MIToken::comma
))
1086 return error("expected ',' before the next machine operand");
1090 MCSymbol
*PreInstrSymbol
= nullptr;
1091 if (Token
.is(MIToken::kw_pre_instr_symbol
))
1092 if (parsePreOrPostInstrSymbol(PreInstrSymbol
))
1094 MCSymbol
*PostInstrSymbol
= nullptr;
1095 if (Token
.is(MIToken::kw_post_instr_symbol
))
1096 if (parsePreOrPostInstrSymbol(PostInstrSymbol
))
1098 MDNode
*HeapAllocMarker
= nullptr;
1099 if (Token
.is(MIToken::kw_heap_alloc_marker
))
1100 if (parseHeapAllocMarker(HeapAllocMarker
))
1102 MDNode
*PCSections
= nullptr;
1103 if (Token
.is(MIToken::kw_pcsections
))
1104 if (parsePCSections(PCSections
))
1107 unsigned CFIType
= 0;
1108 if (Token
.is(MIToken::kw_cfi_type
)) {
1110 if (Token
.isNot(MIToken::IntegerLiteral
))
1111 return error("expected an integer literal after 'cfi-type'");
1112 // getUnsigned is sufficient for 32-bit integers.
1113 if (getUnsigned(CFIType
))
1116 // Lex past trailing comma if present.
1117 if (Token
.is(MIToken::comma
))
1121 unsigned InstrNum
= 0;
1122 if (Token
.is(MIToken::kw_debug_instr_number
)) {
1124 if (Token
.isNot(MIToken::IntegerLiteral
))
1125 return error("expected an integer literal after 'debug-instr-number'");
1126 if (getUnsigned(InstrNum
))
1129 // Lex past trailing comma if present.
1130 if (Token
.is(MIToken::comma
))
1134 DebugLoc DebugLocation
;
1135 if (Token
.is(MIToken::kw_debug_location
)) {
1137 MDNode
*Node
= nullptr;
1138 if (Token
.is(MIToken::exclaim
)) {
1139 if (parseMDNode(Node
))
1141 } else if (Token
.is(MIToken::md_dilocation
)) {
1142 if (parseDILocation(Node
))
1145 return error("expected a metadata node after 'debug-location'");
1146 if (!isa
<DILocation
>(Node
))
1147 return error("referenced metadata is not a DILocation");
1148 DebugLocation
= DebugLoc(Node
);
1151 // Parse the machine memory operands.
1152 SmallVector
<MachineMemOperand
*, 2> MemOperands
;
1153 if (Token
.is(MIToken::coloncolon
)) {
1155 while (!Token
.isNewlineOrEOF()) {
1156 MachineMemOperand
*MemOp
= nullptr;
1157 if (parseMachineMemoryOperand(MemOp
))
1159 MemOperands
.push_back(MemOp
);
1160 if (Token
.isNewlineOrEOF())
1162 if (Token
.isNot(MIToken::comma
))
1163 return error("expected ',' before the next machine memory operand");
1168 const auto &MCID
= MF
.getSubtarget().getInstrInfo()->get(OpCode
);
1169 if (!MCID
.isVariadic()) {
1170 // FIXME: Move the implicit operand verification to the machine verifier.
1171 if (verifyImplicitOperands(Operands
, MCID
))
1175 MI
= MF
.CreateMachineInstr(MCID
, DebugLocation
, /*NoImplicit=*/true);
1176 MI
->setFlags(Flags
);
1178 unsigned NumExplicitOps
= 0;
1179 for (const auto &Operand
: Operands
) {
1180 bool IsImplicitOp
= Operand
.Operand
.isReg() && Operand
.Operand
.isImplicit();
1181 if (!IsImplicitOp
) {
1182 if (!MCID
.isVariadic() && NumExplicitOps
>= MCID
.getNumOperands() &&
1183 !Operand
.Operand
.isValidExcessOperand())
1184 return error(Operand
.Begin
, "too many operands for instruction");
1189 MI
->addOperand(MF
, Operand
.Operand
);
1192 if (assignRegisterTies(*MI
, Operands
))
1195 MI
->setPreInstrSymbol(MF
, PreInstrSymbol
);
1196 if (PostInstrSymbol
)
1197 MI
->setPostInstrSymbol(MF
, PostInstrSymbol
);
1198 if (HeapAllocMarker
)
1199 MI
->setHeapAllocMarker(MF
, HeapAllocMarker
);
1201 MI
->setPCSections(MF
, PCSections
);
1203 MI
->setCFIType(MF
, CFIType
);
1204 if (!MemOperands
.empty())
1205 MI
->setMemRefs(MF
, MemOperands
);
1207 MI
->setDebugInstrNum(InstrNum
);
1211 bool MIParser::parseStandaloneMBB(MachineBasicBlock
*&MBB
) {
1213 if (Token
.isNot(MIToken::MachineBasicBlock
))
1214 return error("expected a machine basic block reference");
1215 if (parseMBBReference(MBB
))
1218 if (Token
.isNot(MIToken::Eof
))
1220 "expected end of string after the machine basic block reference");
1224 bool MIParser::parseStandaloneNamedRegister(Register
&Reg
) {
1226 if (Token
.isNot(MIToken::NamedRegister
))
1227 return error("expected a named register");
1228 if (parseNamedRegister(Reg
))
1231 if (Token
.isNot(MIToken::Eof
))
1232 return error("expected end of string after the register reference");
1236 bool MIParser::parseStandaloneVirtualRegister(VRegInfo
*&Info
) {
1238 if (Token
.isNot(MIToken::VirtualRegister
))
1239 return error("expected a virtual register");
1240 if (parseVirtualRegister(Info
))
1243 if (Token
.isNot(MIToken::Eof
))
1244 return error("expected end of string after the register reference");
1248 bool MIParser::parseStandaloneRegister(Register
&Reg
) {
1250 if (Token
.isNot(MIToken::NamedRegister
) &&
1251 Token
.isNot(MIToken::VirtualRegister
))
1252 return error("expected either a named or virtual register");
1255 if (parseRegister(Reg
, Info
))
1259 if (Token
.isNot(MIToken::Eof
))
1260 return error("expected end of string after the register reference");
1264 bool MIParser::parseStandaloneStackObject(int &FI
) {
1266 if (Token
.isNot(MIToken::StackObject
))
1267 return error("expected a stack object");
1268 if (parseStackFrameIndex(FI
))
1270 if (Token
.isNot(MIToken::Eof
))
1271 return error("expected end of string after the stack object reference");
1275 bool MIParser::parseStandaloneMDNode(MDNode
*&Node
) {
1277 if (Token
.is(MIToken::exclaim
)) {
1278 if (parseMDNode(Node
))
1280 } else if (Token
.is(MIToken::md_diexpr
)) {
1281 if (parseDIExpression(Node
))
1283 } else if (Token
.is(MIToken::md_dilocation
)) {
1284 if (parseDILocation(Node
))
1287 return error("expected a metadata node");
1288 if (Token
.isNot(MIToken::Eof
))
1289 return error("expected end of string after the metadata node");
1293 bool MIParser::parseMachineMetadata() {
1295 if (Token
.isNot(MIToken::exclaim
))
1296 return error("expected a metadata node");
1299 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
1300 return error("expected metadata id after '!'");
1302 if (getUnsigned(ID
))
1305 if (expectAndConsume(MIToken::equal
))
1307 bool IsDistinct
= Token
.is(MIToken::kw_distinct
);
1310 if (Token
.isNot(MIToken::exclaim
))
1311 return error("expected a metadata node");
1315 if (parseMDTuple(MD
, IsDistinct
))
1318 auto FI
= PFS
.MachineForwardRefMDNodes
.find(ID
);
1319 if (FI
!= PFS
.MachineForwardRefMDNodes
.end()) {
1320 FI
->second
.first
->replaceAllUsesWith(MD
);
1321 PFS
.MachineForwardRefMDNodes
.erase(FI
);
1323 assert(PFS
.MachineMetadataNodes
[ID
] == MD
&& "Tracking VH didn't work");
1325 if (PFS
.MachineMetadataNodes
.count(ID
))
1326 return error("Metadata id is already used");
1327 PFS
.MachineMetadataNodes
[ID
].reset(MD
);
1333 bool MIParser::parseMDTuple(MDNode
*&MD
, bool IsDistinct
) {
1334 SmallVector
<Metadata
*, 16> Elts
;
1335 if (parseMDNodeVector(Elts
))
1337 MD
= (IsDistinct
? MDTuple::getDistinct
1338 : MDTuple::get
)(MF
.getFunction().getContext(), Elts
);
1342 bool MIParser::parseMDNodeVector(SmallVectorImpl
<Metadata
*> &Elts
) {
1343 if (Token
.isNot(MIToken::lbrace
))
1344 return error("expected '{' here");
1347 if (Token
.is(MIToken::rbrace
)) {
1354 if (parseMetadata(MD
))
1359 if (Token
.isNot(MIToken::comma
))
1364 if (Token
.isNot(MIToken::rbrace
))
1365 return error("expected end of metadata node");
1373 bool MIParser::parseMetadata(Metadata
*&MD
) {
1374 if (Token
.isNot(MIToken::exclaim
))
1375 return error("expected '!' here");
1378 if (Token
.is(MIToken::StringConstant
)) {
1380 if (parseStringConstant(Str
))
1382 MD
= MDString::get(MF
.getFunction().getContext(), Str
);
1386 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
1387 return error("expected metadata id after '!'");
1389 SMLoc Loc
= mapSMLoc(Token
.location());
1392 if (getUnsigned(ID
))
1396 auto NodeInfo
= PFS
.IRSlots
.MetadataNodes
.find(ID
);
1397 if (NodeInfo
!= PFS
.IRSlots
.MetadataNodes
.end()) {
1398 MD
= NodeInfo
->second
.get();
1401 // Check machine metadata.
1402 NodeInfo
= PFS
.MachineMetadataNodes
.find(ID
);
1403 if (NodeInfo
!= PFS
.MachineMetadataNodes
.end()) {
1404 MD
= NodeInfo
->second
.get();
1407 // Forward reference.
1408 auto &FwdRef
= PFS
.MachineForwardRefMDNodes
[ID
];
1409 FwdRef
= std::make_pair(
1410 MDTuple::getTemporary(MF
.getFunction().getContext(), std::nullopt
), Loc
);
1411 PFS
.MachineMetadataNodes
[ID
].reset(FwdRef
.first
.get());
1412 MD
= FwdRef
.first
.get();
1417 static const char *printImplicitRegisterFlag(const MachineOperand
&MO
) {
1418 assert(MO
.isImplicit());
1419 return MO
.isDef() ? "implicit-def" : "implicit";
1422 static std::string
getRegisterName(const TargetRegisterInfo
*TRI
,
1424 assert(Reg
.isPhysical() && "expected phys reg");
1425 return StringRef(TRI
->getName(Reg
)).lower();
1428 /// Return true if the parsed machine operands contain a given machine operand.
1429 static bool isImplicitOperandIn(const MachineOperand
&ImplicitOperand
,
1430 ArrayRef
<ParsedMachineOperand
> Operands
) {
1431 for (const auto &I
: Operands
) {
1432 if (ImplicitOperand
.isIdenticalTo(I
.Operand
))
1438 bool MIParser::verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
1439 const MCInstrDesc
&MCID
) {
1441 // We can't verify call instructions as they can contain arbitrary implicit
1442 // register and register mask operands.
1445 // Gather all the expected implicit operands.
1446 SmallVector
<MachineOperand
, 4> ImplicitOperands
;
1447 for (MCPhysReg ImpDef
: MCID
.implicit_defs())
1448 ImplicitOperands
.push_back(MachineOperand::CreateReg(ImpDef
, true, true));
1449 for (MCPhysReg ImpUse
: MCID
.implicit_uses())
1450 ImplicitOperands
.push_back(MachineOperand::CreateReg(ImpUse
, false, true));
1452 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
1453 assert(TRI
&& "Expected target register info");
1454 for (const auto &I
: ImplicitOperands
) {
1455 if (isImplicitOperandIn(I
, Operands
))
1457 return error(Operands
.empty() ? Token
.location() : Operands
.back().End
,
1458 Twine("missing implicit register operand '") +
1459 printImplicitRegisterFlag(I
) + " $" +
1460 getRegisterName(TRI
, I
.getReg()) + "'");
1465 bool MIParser::parseInstruction(unsigned &OpCode
, unsigned &Flags
) {
1466 // Allow frame and fast math flags for OPCODE
1468 while (Token
.is(MIToken::kw_frame_setup
) ||
1469 Token
.is(MIToken::kw_frame_destroy
) ||
1470 Token
.is(MIToken::kw_nnan
) ||
1471 Token
.is(MIToken::kw_ninf
) ||
1472 Token
.is(MIToken::kw_nsz
) ||
1473 Token
.is(MIToken::kw_arcp
) ||
1474 Token
.is(MIToken::kw_contract
) ||
1475 Token
.is(MIToken::kw_afn
) ||
1476 Token
.is(MIToken::kw_reassoc
) ||
1477 Token
.is(MIToken::kw_nuw
) ||
1478 Token
.is(MIToken::kw_nsw
) ||
1479 Token
.is(MIToken::kw_exact
) ||
1480 Token
.is(MIToken::kw_nofpexcept
) ||
1481 Token
.is(MIToken::kw_noconvergent
) ||
1482 Token
.is(MIToken::kw_unpredictable
)) {
1484 // Mine frame and fast math flags
1485 if (Token
.is(MIToken::kw_frame_setup
))
1486 Flags
|= MachineInstr::FrameSetup
;
1487 if (Token
.is(MIToken::kw_frame_destroy
))
1488 Flags
|= MachineInstr::FrameDestroy
;
1489 if (Token
.is(MIToken::kw_nnan
))
1490 Flags
|= MachineInstr::FmNoNans
;
1491 if (Token
.is(MIToken::kw_ninf
))
1492 Flags
|= MachineInstr::FmNoInfs
;
1493 if (Token
.is(MIToken::kw_nsz
))
1494 Flags
|= MachineInstr::FmNsz
;
1495 if (Token
.is(MIToken::kw_arcp
))
1496 Flags
|= MachineInstr::FmArcp
;
1497 if (Token
.is(MIToken::kw_contract
))
1498 Flags
|= MachineInstr::FmContract
;
1499 if (Token
.is(MIToken::kw_afn
))
1500 Flags
|= MachineInstr::FmAfn
;
1501 if (Token
.is(MIToken::kw_reassoc
))
1502 Flags
|= MachineInstr::FmReassoc
;
1503 if (Token
.is(MIToken::kw_nuw
))
1504 Flags
|= MachineInstr::NoUWrap
;
1505 if (Token
.is(MIToken::kw_nsw
))
1506 Flags
|= MachineInstr::NoSWrap
;
1507 if (Token
.is(MIToken::kw_exact
))
1508 Flags
|= MachineInstr::IsExact
;
1509 if (Token
.is(MIToken::kw_nofpexcept
))
1510 Flags
|= MachineInstr::NoFPExcept
;
1511 if (Token
.is(MIToken::kw_unpredictable
))
1512 Flags
|= MachineInstr::Unpredictable
;
1513 if (Token
.is(MIToken::kw_noconvergent
))
1514 Flags
|= MachineInstr::NoConvergent
;
1518 if (Token
.isNot(MIToken::Identifier
))
1519 return error("expected a machine instruction");
1520 StringRef InstrName
= Token
.stringValue();
1521 if (PFS
.Target
.parseInstrName(InstrName
, OpCode
))
1522 return error(Twine("unknown machine instruction name '") + InstrName
+ "'");
1527 bool MIParser::parseNamedRegister(Register
&Reg
) {
1528 assert(Token
.is(MIToken::NamedRegister
) && "Needs NamedRegister token");
1529 StringRef Name
= Token
.stringValue();
1530 if (PFS
.Target
.getRegisterByName(Name
, Reg
))
1531 return error(Twine("unknown register name '") + Name
+ "'");
1535 bool MIParser::parseNamedVirtualRegister(VRegInfo
*&Info
) {
1536 assert(Token
.is(MIToken::NamedVirtualRegister
) && "Expected NamedVReg token");
1537 StringRef Name
= Token
.stringValue();
1538 // TODO: Check that the VReg name is not the same as a physical register name.
1539 // If it is, then print a warning (when warnings are implemented).
1540 Info
= &PFS
.getVRegInfoNamed(Name
);
1544 bool MIParser::parseVirtualRegister(VRegInfo
*&Info
) {
1545 if (Token
.is(MIToken::NamedVirtualRegister
))
1546 return parseNamedVirtualRegister(Info
);
1547 assert(Token
.is(MIToken::VirtualRegister
) && "Needs VirtualRegister token");
1549 if (getUnsigned(ID
))
1551 Info
= &PFS
.getVRegInfo(ID
);
1555 bool MIParser::parseRegister(Register
&Reg
, VRegInfo
*&Info
) {
1556 switch (Token
.kind()) {
1557 case MIToken::underscore
:
1560 case MIToken::NamedRegister
:
1561 return parseNamedRegister(Reg
);
1562 case MIToken::NamedVirtualRegister
:
1563 case MIToken::VirtualRegister
:
1564 if (parseVirtualRegister(Info
))
1568 // TODO: Parse other register kinds.
1570 llvm_unreachable("The current token should be a register");
1574 bool MIParser::parseRegisterClassOrBank(VRegInfo
&RegInfo
) {
1575 if (Token
.isNot(MIToken::Identifier
) && Token
.isNot(MIToken::underscore
))
1576 return error("expected '_', register class, or register bank name");
1577 StringRef::iterator Loc
= Token
.location();
1578 StringRef Name
= Token
.stringValue();
1580 // Was it a register class?
1581 const TargetRegisterClass
*RC
= PFS
.Target
.getRegClass(Name
);
1585 switch (RegInfo
.Kind
) {
1586 case VRegInfo::UNKNOWN
:
1587 case VRegInfo::NORMAL
:
1588 RegInfo
.Kind
= VRegInfo::NORMAL
;
1589 if (RegInfo
.Explicit
&& RegInfo
.D
.RC
!= RC
) {
1590 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
1591 return error(Loc
, Twine("conflicting register classes, previously: ") +
1592 Twine(TRI
.getRegClassName(RegInfo
.D
.RC
)));
1595 RegInfo
.Explicit
= true;
1598 case VRegInfo::GENERIC
:
1599 case VRegInfo::REGBANK
:
1600 return error(Loc
, "register class specification on generic register");
1602 llvm_unreachable("Unexpected register kind");
1605 // Should be a register bank or a generic register.
1606 const RegisterBank
*RegBank
= nullptr;
1608 RegBank
= PFS
.Target
.getRegBank(Name
);
1610 return error(Loc
, "expected '_', register class, or register bank name");
1615 switch (RegInfo
.Kind
) {
1616 case VRegInfo::UNKNOWN
:
1617 case VRegInfo::GENERIC
:
1618 case VRegInfo::REGBANK
:
1619 RegInfo
.Kind
= RegBank
? VRegInfo::REGBANK
: VRegInfo::GENERIC
;
1620 if (RegInfo
.Explicit
&& RegInfo
.D
.RegBank
!= RegBank
)
1621 return error(Loc
, "conflicting generic register banks");
1622 RegInfo
.D
.RegBank
= RegBank
;
1623 RegInfo
.Explicit
= true;
1626 case VRegInfo::NORMAL
:
1627 return error(Loc
, "register bank specification on normal register");
1629 llvm_unreachable("Unexpected register kind");
1632 bool MIParser::parseRegisterFlag(unsigned &Flags
) {
1633 const unsigned OldFlags
= Flags
;
1634 switch (Token
.kind()) {
1635 case MIToken::kw_implicit
:
1636 Flags
|= RegState::Implicit
;
1638 case MIToken::kw_implicit_define
:
1639 Flags
|= RegState::ImplicitDefine
;
1641 case MIToken::kw_def
:
1642 Flags
|= RegState::Define
;
1644 case MIToken::kw_dead
:
1645 Flags
|= RegState::Dead
;
1647 case MIToken::kw_killed
:
1648 Flags
|= RegState::Kill
;
1650 case MIToken::kw_undef
:
1651 Flags
|= RegState::Undef
;
1653 case MIToken::kw_internal
:
1654 Flags
|= RegState::InternalRead
;
1656 case MIToken::kw_early_clobber
:
1657 Flags
|= RegState::EarlyClobber
;
1659 case MIToken::kw_debug_use
:
1660 Flags
|= RegState::Debug
;
1662 case MIToken::kw_renamable
:
1663 Flags
|= RegState::Renamable
;
1666 llvm_unreachable("The current token should be a register flag");
1668 if (OldFlags
== Flags
)
1669 // We know that the same flag is specified more than once when the flags
1670 // weren't modified.
1671 return error("duplicate '" + Token
.stringValue() + "' register flag");
1676 bool MIParser::parseSubRegisterIndex(unsigned &SubReg
) {
1677 assert(Token
.is(MIToken::dot
));
1679 if (Token
.isNot(MIToken::Identifier
))
1680 return error("expected a subregister index after '.'");
1681 auto Name
= Token
.stringValue();
1682 SubReg
= PFS
.Target
.getSubRegIndex(Name
);
1684 return error(Twine("use of unknown subregister index '") + Name
+ "'");
1689 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx
) {
1690 if (!consumeIfPresent(MIToken::kw_tied_def
))
1692 if (Token
.isNot(MIToken::IntegerLiteral
))
1693 return error("expected an integer literal after 'tied-def'");
1694 if (getUnsigned(TiedDefIdx
))
1697 if (expectAndConsume(MIToken::rparen
))
1702 bool MIParser::assignRegisterTies(MachineInstr
&MI
,
1703 ArrayRef
<ParsedMachineOperand
> Operands
) {
1704 SmallVector
<std::pair
<unsigned, unsigned>, 4> TiedRegisterPairs
;
1705 for (unsigned I
= 0, E
= Operands
.size(); I
!= E
; ++I
) {
1706 if (!Operands
[I
].TiedDefIdx
)
1708 // The parser ensures that this operand is a register use, so we just have
1709 // to check the tied-def operand.
1710 unsigned DefIdx
= *Operands
[I
].TiedDefIdx
;
1712 return error(Operands
[I
].Begin
,
1713 Twine("use of invalid tied-def operand index '" +
1714 Twine(DefIdx
) + "'; instruction has only ") +
1715 Twine(E
) + " operands");
1716 const auto &DefOperand
= Operands
[DefIdx
].Operand
;
1717 if (!DefOperand
.isReg() || !DefOperand
.isDef())
1718 // FIXME: add note with the def operand.
1719 return error(Operands
[I
].Begin
,
1720 Twine("use of invalid tied-def operand index '") +
1721 Twine(DefIdx
) + "'; the operand #" + Twine(DefIdx
) +
1722 " isn't a defined register");
1723 // Check that the tied-def operand wasn't tied elsewhere.
1724 for (const auto &TiedPair
: TiedRegisterPairs
) {
1725 if (TiedPair
.first
== DefIdx
)
1726 return error(Operands
[I
].Begin
,
1727 Twine("the tied-def operand #") + Twine(DefIdx
) +
1728 " is already tied with another register operand");
1730 TiedRegisterPairs
.push_back(std::make_pair(DefIdx
, I
));
1732 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1733 // indices must be less than tied max.
1734 for (const auto &TiedPair
: TiedRegisterPairs
)
1735 MI
.tieOperands(TiedPair
.first
, TiedPair
.second
);
1739 bool MIParser::parseRegisterOperand(MachineOperand
&Dest
,
1740 std::optional
<unsigned> &TiedDefIdx
,
1742 unsigned Flags
= IsDef
? RegState::Define
: 0;
1743 while (Token
.isRegisterFlag()) {
1744 if (parseRegisterFlag(Flags
))
1747 if (!Token
.isRegister())
1748 return error("expected a register after register flags");
1751 if (parseRegister(Reg
, RegInfo
))
1754 unsigned SubReg
= 0;
1755 if (Token
.is(MIToken::dot
)) {
1756 if (parseSubRegisterIndex(SubReg
))
1758 if (!Reg
.isVirtual())
1759 return error("subregister index expects a virtual register");
1761 if (Token
.is(MIToken::colon
)) {
1762 if (!Reg
.isVirtual())
1763 return error("register class specification expects a virtual register");
1765 if (parseRegisterClassOrBank(*RegInfo
))
1768 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1769 if ((Flags
& RegState::Define
) == 0) {
1770 if (consumeIfPresent(MIToken::lparen
)) {
1772 if (!parseRegisterTiedDefIndex(Idx
))
1775 // Try a redundant low-level type.
1777 if (parseLowLevelType(Token
.location(), Ty
))
1778 return error("expected tied-def or low-level type after '('");
1780 if (expectAndConsume(MIToken::rparen
))
1783 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1784 return error("inconsistent type for generic virtual register");
1786 MRI
.setRegClassOrRegBank(Reg
, static_cast<RegisterBank
*>(nullptr));
1787 MRI
.setType(Reg
, Ty
);
1790 } else if (consumeIfPresent(MIToken::lparen
)) {
1791 // Virtual registers may have a tpe with GlobalISel.
1792 if (!Reg
.isVirtual())
1793 return error("unexpected type on physical register");
1796 if (parseLowLevelType(Token
.location(), Ty
))
1799 if (expectAndConsume(MIToken::rparen
))
1802 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1803 return error("inconsistent type for generic virtual register");
1805 MRI
.setRegClassOrRegBank(Reg
, static_cast<RegisterBank
*>(nullptr));
1806 MRI
.setType(Reg
, Ty
);
1807 } else if (Reg
.isVirtual()) {
1808 // Generic virtual registers must have a type.
1809 // If we end up here this means the type hasn't been specified and
1811 if (RegInfo
->Kind
== VRegInfo::GENERIC
||
1812 RegInfo
->Kind
== VRegInfo::REGBANK
)
1813 return error("generic virtual registers must have a type");
1816 if (Flags
& RegState::Define
) {
1817 if (Flags
& RegState::Kill
)
1818 return error("cannot have a killed def operand");
1820 if (Flags
& RegState::Dead
)
1821 return error("cannot have a dead use operand");
1824 Dest
= MachineOperand::CreateReg(
1825 Reg
, Flags
& RegState::Define
, Flags
& RegState::Implicit
,
1826 Flags
& RegState::Kill
, Flags
& RegState::Dead
, Flags
& RegState::Undef
,
1827 Flags
& RegState::EarlyClobber
, SubReg
, Flags
& RegState::Debug
,
1828 Flags
& RegState::InternalRead
, Flags
& RegState::Renamable
);
1833 bool MIParser::parseImmediateOperand(MachineOperand
&Dest
) {
1834 assert(Token
.is(MIToken::IntegerLiteral
));
1835 const APSInt
&Int
= Token
.integerValue();
1836 if (auto SImm
= Int
.trySExtValue(); Int
.isSigned() && SImm
.has_value())
1837 Dest
= MachineOperand::CreateImm(*SImm
);
1838 else if (auto UImm
= Int
.tryZExtValue(); !Int
.isSigned() && UImm
.has_value())
1839 Dest
= MachineOperand::CreateImm(*UImm
);
1841 return error("integer literal is too large to be an immediate operand");
1846 bool MIParser::parseTargetImmMnemonic(const unsigned OpCode
,
1847 const unsigned OpIdx
,
1848 MachineOperand
&Dest
,
1849 const MIRFormatter
&MF
) {
1850 assert(Token
.is(MIToken::dot
));
1851 auto Loc
= Token
.location(); // record start position
1852 size_t Len
= 1; // for "."
1855 // Handle the case that mnemonic starts with number.
1856 if (Token
.is(MIToken::IntegerLiteral
)) {
1857 Len
+= Token
.range().size();
1862 if (Token
.is(MIToken::comma
))
1863 Src
= StringRef(Loc
, Len
);
1865 assert(Token
.is(MIToken::Identifier
));
1866 Src
= StringRef(Loc
, Len
+ Token
.stringValue().size());
1869 if (MF
.parseImmMnemonic(OpCode
, OpIdx
, Src
, Val
,
1870 [this](StringRef::iterator Loc
, const Twine
&Msg
)
1871 -> bool { return error(Loc
, Msg
); }))
1874 Dest
= MachineOperand::CreateImm(Val
);
1875 if (!Token
.is(MIToken::comma
))
1880 static bool parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
1881 PerFunctionMIParsingState
&PFS
, const Constant
*&C
,
1882 ErrorCallbackType ErrCB
) {
1883 auto Source
= StringValue
.str(); // The source has to be null terminated.
1885 C
= parseConstantValue(Source
, Err
, *PFS
.MF
.getFunction().getParent(),
1888 return ErrCB(Loc
+ Err
.getColumnNo(), Err
.getMessage());
1892 bool MIParser::parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
1893 const Constant
*&C
) {
1894 return ::parseIRConstant(
1895 Loc
, StringValue
, PFS
, C
,
1896 [this](StringRef::iterator Loc
, const Twine
&Msg
) -> bool {
1897 return error(Loc
, Msg
);
1901 bool MIParser::parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
) {
1902 if (parseIRConstant(Loc
, StringRef(Loc
, Token
.range().end() - Loc
), C
))
1908 // See LLT implementation for bit size limits.
1909 static bool verifyScalarSize(uint64_t Size
) {
1910 return Size
!= 0 && isUInt
<16>(Size
);
1913 static bool verifyVectorElementCount(uint64_t NumElts
) {
1914 return NumElts
!= 0 && isUInt
<16>(NumElts
);
1917 static bool verifyAddrSpace(uint64_t AddrSpace
) {
1918 return isUInt
<24>(AddrSpace
);
1921 bool MIParser::parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
) {
1922 if (Token
.range().front() == 's' || Token
.range().front() == 'p') {
1923 StringRef SizeStr
= Token
.range().drop_front();
1924 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1925 return error("expected integers after 's'/'p' type character");
1928 if (Token
.range().front() == 's') {
1929 auto ScalarSize
= APSInt(Token
.range().drop_front()).getZExtValue();
1930 if (!verifyScalarSize(ScalarSize
))
1931 return error("invalid size for scalar type");
1933 Ty
= LLT::scalar(ScalarSize
);
1936 } else if (Token
.range().front() == 'p') {
1937 const DataLayout
&DL
= MF
.getDataLayout();
1938 uint64_t AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1939 if (!verifyAddrSpace(AS
))
1940 return error("invalid address space number");
1942 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1947 // Now we're looking for a vector.
1948 if (Token
.isNot(MIToken::less
))
1950 "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1953 if (Token
.isNot(MIToken::IntegerLiteral
))
1954 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1955 uint64_t NumElements
= Token
.integerValue().getZExtValue();
1956 if (!verifyVectorElementCount(NumElements
))
1957 return error("invalid number of vector elements");
1961 if (Token
.isNot(MIToken::Identifier
) || Token
.stringValue() != "x")
1962 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1965 if (Token
.range().front() != 's' && Token
.range().front() != 'p')
1966 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1967 StringRef SizeStr
= Token
.range().drop_front();
1968 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1969 return error("expected integers after 's'/'p' type character");
1971 if (Token
.range().front() == 's') {
1972 auto ScalarSize
= APSInt(Token
.range().drop_front()).getZExtValue();
1973 if (!verifyScalarSize(ScalarSize
))
1974 return error("invalid size for scalar type");
1975 Ty
= LLT::scalar(ScalarSize
);
1976 } else if (Token
.range().front() == 'p') {
1977 const DataLayout
&DL
= MF
.getDataLayout();
1978 uint64_t AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1979 if (!verifyAddrSpace(AS
))
1980 return error("invalid address space number");
1982 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1984 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1987 if (Token
.isNot(MIToken::greater
))
1988 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1991 Ty
= LLT::fixed_vector(NumElements
, Ty
);
1995 bool MIParser::parseTypedImmediateOperand(MachineOperand
&Dest
) {
1996 assert(Token
.is(MIToken::Identifier
));
1997 StringRef TypeStr
= Token
.range();
1998 if (TypeStr
.front() != 'i' && TypeStr
.front() != 's' &&
1999 TypeStr
.front() != 'p')
2001 "a typed immediate operand should start with one of 'i', 's', or 'p'");
2002 StringRef SizeStr
= Token
.range().drop_front();
2003 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
2004 return error("expected integers after 'i'/'s'/'p' type character");
2006 auto Loc
= Token
.location();
2008 if (Token
.isNot(MIToken::IntegerLiteral
)) {
2009 if (Token
.isNot(MIToken::Identifier
) ||
2010 !(Token
.range() == "true" || Token
.range() == "false"))
2011 return error("expected an integer literal");
2013 const Constant
*C
= nullptr;
2014 if (parseIRConstant(Loc
, C
))
2016 Dest
= MachineOperand::CreateCImm(cast
<ConstantInt
>(C
));
2020 bool MIParser::parseFPImmediateOperand(MachineOperand
&Dest
) {
2021 auto Loc
= Token
.location();
2023 if (Token
.isNot(MIToken::FloatingPointLiteral
) &&
2024 Token
.isNot(MIToken::HexLiteral
))
2025 return error("expected a floating point literal");
2026 const Constant
*C
= nullptr;
2027 if (parseIRConstant(Loc
, C
))
2029 Dest
= MachineOperand::CreateFPImm(cast
<ConstantFP
>(C
));
2033 static bool getHexUint(const MIToken
&Token
, APInt
&Result
) {
2034 assert(Token
.is(MIToken::HexLiteral
));
2035 StringRef S
= Token
.range();
2036 assert(S
[0] == '0' && tolower(S
[1]) == 'x');
2037 // This could be a floating point literal with a special prefix.
2038 if (!isxdigit(S
[2]))
2040 StringRef V
= S
.substr(2);
2041 APInt
A(V
.size()*4, V
, 16);
2043 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2044 // sure it isn't the case before constructing result.
2045 unsigned NumBits
= (A
== 0) ? 32 : A
.getActiveBits();
2046 Result
= APInt(NumBits
, ArrayRef
<uint64_t>(A
.getRawData(), A
.getNumWords()));
2050 static bool getUnsigned(const MIToken
&Token
, unsigned &Result
,
2051 ErrorCallbackType ErrCB
) {
2052 if (Token
.hasIntegerValue()) {
2053 const uint64_t Limit
= uint64_t(std::numeric_limits
<unsigned>::max()) + 1;
2054 uint64_t Val64
= Token
.integerValue().getLimitedValue(Limit
);
2056 return ErrCB(Token
.location(), "expected 32-bit integer (too large)");
2060 if (Token
.is(MIToken::HexLiteral
)) {
2062 if (getHexUint(Token
, A
))
2064 if (A
.getBitWidth() > 32)
2065 return ErrCB(Token
.location(), "expected 32-bit integer (too large)");
2066 Result
= A
.getZExtValue();
2072 bool MIParser::getUnsigned(unsigned &Result
) {
2073 return ::getUnsigned(
2074 Token
, Result
, [this](StringRef::iterator Loc
, const Twine
&Msg
) -> bool {
2075 return error(Loc
, Msg
);
2079 bool MIParser::parseMBBReference(MachineBasicBlock
*&MBB
) {
2080 assert(Token
.is(MIToken::MachineBasicBlock
) ||
2081 Token
.is(MIToken::MachineBasicBlockLabel
));
2083 if (getUnsigned(Number
))
2085 auto MBBInfo
= PFS
.MBBSlots
.find(Number
);
2086 if (MBBInfo
== PFS
.MBBSlots
.end())
2087 return error(Twine("use of undefined machine basic block #") +
2089 MBB
= MBBInfo
->second
;
2090 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2091 // we drop the <irname> from the bb.<id>.<irname> format.
2092 if (!Token
.stringValue().empty() && Token
.stringValue() != MBB
->getName())
2093 return error(Twine("the name of machine basic block #") + Twine(Number
) +
2094 " isn't '" + Token
.stringValue() + "'");
2098 bool MIParser::parseMBBOperand(MachineOperand
&Dest
) {
2099 MachineBasicBlock
*MBB
;
2100 if (parseMBBReference(MBB
))
2102 Dest
= MachineOperand::CreateMBB(MBB
);
2107 bool MIParser::parseStackFrameIndex(int &FI
) {
2108 assert(Token
.is(MIToken::StackObject
));
2110 if (getUnsigned(ID
))
2112 auto ObjectInfo
= PFS
.StackObjectSlots
.find(ID
);
2113 if (ObjectInfo
== PFS
.StackObjectSlots
.end())
2114 return error(Twine("use of undefined stack object '%stack.") + Twine(ID
) +
2117 if (const auto *Alloca
=
2118 MF
.getFrameInfo().getObjectAllocation(ObjectInfo
->second
))
2119 Name
= Alloca
->getName();
2120 if (!Token
.stringValue().empty() && Token
.stringValue() != Name
)
2121 return error(Twine("the name of the stack object '%stack.") + Twine(ID
) +
2122 "' isn't '" + Token
.stringValue() + "'");
2124 FI
= ObjectInfo
->second
;
2128 bool MIParser::parseStackObjectOperand(MachineOperand
&Dest
) {
2130 if (parseStackFrameIndex(FI
))
2132 Dest
= MachineOperand::CreateFI(FI
);
2136 bool MIParser::parseFixedStackFrameIndex(int &FI
) {
2137 assert(Token
.is(MIToken::FixedStackObject
));
2139 if (getUnsigned(ID
))
2141 auto ObjectInfo
= PFS
.FixedStackObjectSlots
.find(ID
);
2142 if (ObjectInfo
== PFS
.FixedStackObjectSlots
.end())
2143 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2146 FI
= ObjectInfo
->second
;
2150 bool MIParser::parseFixedStackObjectOperand(MachineOperand
&Dest
) {
2152 if (parseFixedStackFrameIndex(FI
))
2154 Dest
= MachineOperand::CreateFI(FI
);
2158 static bool parseGlobalValue(const MIToken
&Token
,
2159 PerFunctionMIParsingState
&PFS
, GlobalValue
*&GV
,
2160 ErrorCallbackType ErrCB
) {
2161 switch (Token
.kind()) {
2162 case MIToken::NamedGlobalValue
: {
2163 const Module
*M
= PFS
.MF
.getFunction().getParent();
2164 GV
= M
->getNamedValue(Token
.stringValue());
2166 return ErrCB(Token
.location(), Twine("use of undefined global value '") +
2167 Token
.range() + "'");
2170 case MIToken::GlobalValue
: {
2172 if (getUnsigned(Token
, GVIdx
, ErrCB
))
2174 if (GVIdx
>= PFS
.IRSlots
.GlobalValues
.size())
2175 return ErrCB(Token
.location(), Twine("use of undefined global value '@") +
2176 Twine(GVIdx
) + "'");
2177 GV
= PFS
.IRSlots
.GlobalValues
[GVIdx
];
2181 llvm_unreachable("The current token should be a global value");
2186 bool MIParser::parseGlobalValue(GlobalValue
*&GV
) {
2187 return ::parseGlobalValue(
2189 [this](StringRef::iterator Loc
, const Twine
&Msg
) -> bool {
2190 return error(Loc
, Msg
);
2194 bool MIParser::parseGlobalAddressOperand(MachineOperand
&Dest
) {
2195 GlobalValue
*GV
= nullptr;
2196 if (parseGlobalValue(GV
))
2199 Dest
= MachineOperand::CreateGA(GV
, /*Offset=*/0);
2200 if (parseOperandsOffset(Dest
))
2205 bool MIParser::parseConstantPoolIndexOperand(MachineOperand
&Dest
) {
2206 assert(Token
.is(MIToken::ConstantPoolItem
));
2208 if (getUnsigned(ID
))
2210 auto ConstantInfo
= PFS
.ConstantPoolSlots
.find(ID
);
2211 if (ConstantInfo
== PFS
.ConstantPoolSlots
.end())
2212 return error("use of undefined constant '%const." + Twine(ID
) + "'");
2214 Dest
= MachineOperand::CreateCPI(ID
, /*Offset=*/0);
2215 if (parseOperandsOffset(Dest
))
2220 bool MIParser::parseJumpTableIndexOperand(MachineOperand
&Dest
) {
2221 assert(Token
.is(MIToken::JumpTableIndex
));
2223 if (getUnsigned(ID
))
2225 auto JumpTableEntryInfo
= PFS
.JumpTableSlots
.find(ID
);
2226 if (JumpTableEntryInfo
== PFS
.JumpTableSlots
.end())
2227 return error("use of undefined jump table '%jump-table." + Twine(ID
) + "'");
2229 Dest
= MachineOperand::CreateJTI(JumpTableEntryInfo
->second
);
2233 bool MIParser::parseExternalSymbolOperand(MachineOperand
&Dest
) {
2234 assert(Token
.is(MIToken::ExternalSymbol
));
2235 const char *Symbol
= MF
.createExternalSymbolName(Token
.stringValue());
2237 Dest
= MachineOperand::CreateES(Symbol
);
2238 if (parseOperandsOffset(Dest
))
2243 bool MIParser::parseMCSymbolOperand(MachineOperand
&Dest
) {
2244 assert(Token
.is(MIToken::MCSymbol
));
2245 MCSymbol
*Symbol
= getOrCreateMCSymbol(Token
.stringValue());
2247 Dest
= MachineOperand::CreateMCSymbol(Symbol
);
2248 if (parseOperandsOffset(Dest
))
2253 bool MIParser::parseSubRegisterIndexOperand(MachineOperand
&Dest
) {
2254 assert(Token
.is(MIToken::SubRegisterIndex
));
2255 StringRef Name
= Token
.stringValue();
2256 unsigned SubRegIndex
= PFS
.Target
.getSubRegIndex(Token
.stringValue());
2257 if (SubRegIndex
== 0)
2258 return error(Twine("unknown subregister index '") + Name
+ "'");
2260 Dest
= MachineOperand::CreateImm(SubRegIndex
);
2264 bool MIParser::parseMDNode(MDNode
*&Node
) {
2265 assert(Token
.is(MIToken::exclaim
));
2267 auto Loc
= Token
.location();
2269 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
2270 return error("expected metadata id after '!'");
2272 if (getUnsigned(ID
))
2274 auto NodeInfo
= PFS
.IRSlots
.MetadataNodes
.find(ID
);
2275 if (NodeInfo
== PFS
.IRSlots
.MetadataNodes
.end()) {
2276 NodeInfo
= PFS
.MachineMetadataNodes
.find(ID
);
2277 if (NodeInfo
== PFS
.MachineMetadataNodes
.end())
2278 return error(Loc
, "use of undefined metadata '!" + Twine(ID
) + "'");
2281 Node
= NodeInfo
->second
.get();
2285 bool MIParser::parseDIExpression(MDNode
*&Expr
) {
2286 assert(Token
.is(MIToken::md_diexpr
));
2289 // FIXME: Share this parsing with the IL parser.
2290 SmallVector
<uint64_t, 8> Elements
;
2292 if (expectAndConsume(MIToken::lparen
))
2295 if (Token
.isNot(MIToken::rparen
)) {
2297 if (Token
.is(MIToken::Identifier
)) {
2298 if (unsigned Op
= dwarf::getOperationEncoding(Token
.stringValue())) {
2300 Elements
.push_back(Op
);
2303 if (unsigned Enc
= dwarf::getAttributeEncoding(Token
.stringValue())) {
2305 Elements
.push_back(Enc
);
2308 return error(Twine("invalid DWARF op '") + Token
.stringValue() + "'");
2311 if (Token
.isNot(MIToken::IntegerLiteral
) ||
2312 Token
.integerValue().isSigned())
2313 return error("expected unsigned integer");
2315 auto &U
= Token
.integerValue();
2316 if (U
.ugt(UINT64_MAX
))
2317 return error("element too large, limit is " + Twine(UINT64_MAX
));
2318 Elements
.push_back(U
.getZExtValue());
2321 } while (consumeIfPresent(MIToken::comma
));
2324 if (expectAndConsume(MIToken::rparen
))
2327 Expr
= DIExpression::get(MF
.getFunction().getContext(), Elements
);
2331 bool MIParser::parseDILocation(MDNode
*&Loc
) {
2332 assert(Token
.is(MIToken::md_dilocation
));
2335 bool HaveLine
= false;
2337 unsigned Column
= 0;
2338 MDNode
*Scope
= nullptr;
2339 MDNode
*InlinedAt
= nullptr;
2340 bool ImplicitCode
= false;
2342 if (expectAndConsume(MIToken::lparen
))
2345 if (Token
.isNot(MIToken::rparen
)) {
2347 if (Token
.is(MIToken::Identifier
)) {
2348 if (Token
.stringValue() == "line") {
2350 if (expectAndConsume(MIToken::colon
))
2352 if (Token
.isNot(MIToken::IntegerLiteral
) ||
2353 Token
.integerValue().isSigned())
2354 return error("expected unsigned integer");
2355 Line
= Token
.integerValue().getZExtValue();
2360 if (Token
.stringValue() == "column") {
2362 if (expectAndConsume(MIToken::colon
))
2364 if (Token
.isNot(MIToken::IntegerLiteral
) ||
2365 Token
.integerValue().isSigned())
2366 return error("expected unsigned integer");
2367 Column
= Token
.integerValue().getZExtValue();
2371 if (Token
.stringValue() == "scope") {
2373 if (expectAndConsume(MIToken::colon
))
2375 if (parseMDNode(Scope
))
2376 return error("expected metadata node");
2377 if (!isa
<DIScope
>(Scope
))
2378 return error("expected DIScope node");
2381 if (Token
.stringValue() == "inlinedAt") {
2383 if (expectAndConsume(MIToken::colon
))
2385 if (Token
.is(MIToken::exclaim
)) {
2386 if (parseMDNode(InlinedAt
))
2388 } else if (Token
.is(MIToken::md_dilocation
)) {
2389 if (parseDILocation(InlinedAt
))
2392 return error("expected metadata node");
2393 if (!isa
<DILocation
>(InlinedAt
))
2394 return error("expected DILocation node");
2397 if (Token
.stringValue() == "isImplicitCode") {
2399 if (expectAndConsume(MIToken::colon
))
2401 if (!Token
.is(MIToken::Identifier
))
2402 return error("expected true/false");
2403 // As far as I can see, we don't have any existing need for parsing
2404 // true/false in MIR yet. Do it ad-hoc until there's something else
2406 if (Token
.stringValue() == "true")
2407 ImplicitCode
= true;
2408 else if (Token
.stringValue() == "false")
2409 ImplicitCode
= false;
2411 return error("expected true/false");
2416 return error(Twine("invalid DILocation argument '") +
2417 Token
.stringValue() + "'");
2418 } while (consumeIfPresent(MIToken::comma
));
2421 if (expectAndConsume(MIToken::rparen
))
2425 return error("DILocation requires line number");
2427 return error("DILocation requires a scope");
2429 Loc
= DILocation::get(MF
.getFunction().getContext(), Line
, Column
, Scope
,
2430 InlinedAt
, ImplicitCode
);
2434 bool MIParser::parseMetadataOperand(MachineOperand
&Dest
) {
2435 MDNode
*Node
= nullptr;
2436 if (Token
.is(MIToken::exclaim
)) {
2437 if (parseMDNode(Node
))
2439 } else if (Token
.is(MIToken::md_diexpr
)) {
2440 if (parseDIExpression(Node
))
2443 Dest
= MachineOperand::CreateMetadata(Node
);
2447 bool MIParser::parseCFIOffset(int &Offset
) {
2448 if (Token
.isNot(MIToken::IntegerLiteral
))
2449 return error("expected a cfi offset");
2450 if (Token
.integerValue().getSignificantBits() > 32)
2451 return error("expected a 32 bit integer (the cfi offset is too large)");
2452 Offset
= (int)Token
.integerValue().getExtValue();
2457 bool MIParser::parseCFIRegister(Register
&Reg
) {
2458 if (Token
.isNot(MIToken::NamedRegister
))
2459 return error("expected a cfi register");
2461 if (parseNamedRegister(LLVMReg
))
2463 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
2464 assert(TRI
&& "Expected target register info");
2465 int DwarfReg
= TRI
->getDwarfRegNum(LLVMReg
, true);
2467 return error("invalid DWARF register");
2468 Reg
= (unsigned)DwarfReg
;
2473 bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace
) {
2474 if (Token
.isNot(MIToken::IntegerLiteral
))
2475 return error("expected a cfi address space literal");
2476 if (Token
.integerValue().isSigned())
2477 return error("expected an unsigned integer (cfi address space)");
2478 AddressSpace
= Token
.integerValue().getZExtValue();
2483 bool MIParser::parseCFIEscapeValues(std::string
&Values
) {
2485 if (Token
.isNot(MIToken::HexLiteral
))
2486 return error("expected a hexadecimal literal");
2488 if (getUnsigned(Value
))
2490 if (Value
> UINT8_MAX
)
2491 return error("expected a 8-bit integer (too large)");
2492 Values
.push_back(static_cast<uint8_t>(Value
));
2494 } while (consumeIfPresent(MIToken::comma
));
2498 bool MIParser::parseCFIOperand(MachineOperand
&Dest
) {
2499 auto Kind
= Token
.kind();
2503 unsigned AddressSpace
;
2506 case MIToken::kw_cfi_same_value
:
2507 if (parseCFIRegister(Reg
))
2509 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg
));
2511 case MIToken::kw_cfi_offset
:
2512 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2513 parseCFIOffset(Offset
))
2516 MF
.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg
, Offset
));
2518 case MIToken::kw_cfi_rel_offset
:
2519 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2520 parseCFIOffset(Offset
))
2522 CFIIndex
= MF
.addFrameInst(
2523 MCCFIInstruction::createRelOffset(nullptr, Reg
, Offset
));
2525 case MIToken::kw_cfi_def_cfa_register
:
2526 if (parseCFIRegister(Reg
))
2529 MF
.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg
));
2531 case MIToken::kw_cfi_def_cfa_offset
:
2532 if (parseCFIOffset(Offset
))
2535 MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset
));
2537 case MIToken::kw_cfi_adjust_cfa_offset
:
2538 if (parseCFIOffset(Offset
))
2540 CFIIndex
= MF
.addFrameInst(
2541 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset
));
2543 case MIToken::kw_cfi_def_cfa
:
2544 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2545 parseCFIOffset(Offset
))
2548 MF
.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg
, Offset
));
2550 case MIToken::kw_cfi_llvm_def_aspace_cfa
:
2551 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2552 parseCFIOffset(Offset
) || expectAndConsume(MIToken::comma
) ||
2553 parseCFIAddressSpace(AddressSpace
))
2555 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2556 nullptr, Reg
, Offset
, AddressSpace
, SMLoc()));
2558 case MIToken::kw_cfi_remember_state
:
2559 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2561 case MIToken::kw_cfi_restore
:
2562 if (parseCFIRegister(Reg
))
2564 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg
));
2566 case MIToken::kw_cfi_restore_state
:
2567 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2569 case MIToken::kw_cfi_undefined
:
2570 if (parseCFIRegister(Reg
))
2572 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg
));
2574 case MIToken::kw_cfi_register
: {
2576 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2577 parseCFIRegister(Reg2
))
2581 MF
.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg
, Reg2
));
2584 case MIToken::kw_cfi_window_save
:
2585 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2587 case MIToken::kw_cfi_aarch64_negate_ra_sign_state
:
2588 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2590 case MIToken::kw_cfi_escape
: {
2592 if (parseCFIEscapeValues(Values
))
2594 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values
));
2598 // TODO: Parse the other CFI operands.
2599 llvm_unreachable("The current token should be a cfi operand");
2601 Dest
= MachineOperand::CreateCFIIndex(CFIIndex
);
2605 bool MIParser::parseIRBlock(BasicBlock
*&BB
, const Function
&F
) {
2606 switch (Token
.kind()) {
2607 case MIToken::NamedIRBlock
: {
2608 BB
= dyn_cast_or_null
<BasicBlock
>(
2609 F
.getValueSymbolTable()->lookup(Token
.stringValue()));
2611 return error(Twine("use of undefined IR block '") + Token
.range() + "'");
2614 case MIToken::IRBlock
: {
2615 unsigned SlotNumber
= 0;
2616 if (getUnsigned(SlotNumber
))
2618 BB
= const_cast<BasicBlock
*>(getIRBlock(SlotNumber
, F
));
2620 return error(Twine("use of undefined IR block '%ir-block.") +
2621 Twine(SlotNumber
) + "'");
2625 llvm_unreachable("The current token should be an IR block reference");
2630 bool MIParser::parseBlockAddressOperand(MachineOperand
&Dest
) {
2631 assert(Token
.is(MIToken::kw_blockaddress
));
2633 if (expectAndConsume(MIToken::lparen
))
2635 if (Token
.isNot(MIToken::GlobalValue
) &&
2636 Token
.isNot(MIToken::NamedGlobalValue
))
2637 return error("expected a global value");
2638 GlobalValue
*GV
= nullptr;
2639 if (parseGlobalValue(GV
))
2641 auto *F
= dyn_cast
<Function
>(GV
);
2643 return error("expected an IR function reference");
2645 if (expectAndConsume(MIToken::comma
))
2647 BasicBlock
*BB
= nullptr;
2648 if (Token
.isNot(MIToken::IRBlock
) && Token
.isNot(MIToken::NamedIRBlock
))
2649 return error("expected an IR block reference");
2650 if (parseIRBlock(BB
, *F
))
2653 if (expectAndConsume(MIToken::rparen
))
2655 Dest
= MachineOperand::CreateBA(BlockAddress::get(F
, BB
), /*Offset=*/0);
2656 if (parseOperandsOffset(Dest
))
2661 bool MIParser::parseIntrinsicOperand(MachineOperand
&Dest
) {
2662 assert(Token
.is(MIToken::kw_intrinsic
));
2664 if (expectAndConsume(MIToken::lparen
))
2665 return error("expected syntax intrinsic(@llvm.whatever)");
2667 if (Token
.isNot(MIToken::NamedGlobalValue
))
2668 return error("expected syntax intrinsic(@llvm.whatever)");
2670 std::string Name
= std::string(Token
.stringValue());
2673 if (expectAndConsume(MIToken::rparen
))
2674 return error("expected ')' to terminate intrinsic name");
2676 // Find out what intrinsic we're dealing with, first try the global namespace
2677 // and then the target's private intrinsics if that fails.
2678 const TargetIntrinsicInfo
*TII
= MF
.getTarget().getIntrinsicInfo();
2679 Intrinsic::ID ID
= Function::lookupIntrinsicID(Name
);
2680 if (ID
== Intrinsic::not_intrinsic
&& TII
)
2681 ID
= static_cast<Intrinsic::ID
>(TII
->lookupName(Name
));
2683 if (ID
== Intrinsic::not_intrinsic
)
2684 return error("unknown intrinsic name");
2685 Dest
= MachineOperand::CreateIntrinsicID(ID
);
2690 bool MIParser::parsePredicateOperand(MachineOperand
&Dest
) {
2691 assert(Token
.is(MIToken::kw_intpred
) || Token
.is(MIToken::kw_floatpred
));
2692 bool IsFloat
= Token
.is(MIToken::kw_floatpred
);
2695 if (expectAndConsume(MIToken::lparen
))
2696 return error("expected syntax intpred(whatever) or floatpred(whatever");
2698 if (Token
.isNot(MIToken::Identifier
))
2699 return error("whatever");
2701 CmpInst::Predicate Pred
;
2703 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
2704 .Case("false", CmpInst::FCMP_FALSE
)
2705 .Case("oeq", CmpInst::FCMP_OEQ
)
2706 .Case("ogt", CmpInst::FCMP_OGT
)
2707 .Case("oge", CmpInst::FCMP_OGE
)
2708 .Case("olt", CmpInst::FCMP_OLT
)
2709 .Case("ole", CmpInst::FCMP_OLE
)
2710 .Case("one", CmpInst::FCMP_ONE
)
2711 .Case("ord", CmpInst::FCMP_ORD
)
2712 .Case("uno", CmpInst::FCMP_UNO
)
2713 .Case("ueq", CmpInst::FCMP_UEQ
)
2714 .Case("ugt", CmpInst::FCMP_UGT
)
2715 .Case("uge", CmpInst::FCMP_UGE
)
2716 .Case("ult", CmpInst::FCMP_ULT
)
2717 .Case("ule", CmpInst::FCMP_ULE
)
2718 .Case("une", CmpInst::FCMP_UNE
)
2719 .Case("true", CmpInst::FCMP_TRUE
)
2720 .Default(CmpInst::BAD_FCMP_PREDICATE
);
2721 if (!CmpInst::isFPPredicate(Pred
))
2722 return error("invalid floating-point predicate");
2724 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
2725 .Case("eq", CmpInst::ICMP_EQ
)
2726 .Case("ne", CmpInst::ICMP_NE
)
2727 .Case("sgt", CmpInst::ICMP_SGT
)
2728 .Case("sge", CmpInst::ICMP_SGE
)
2729 .Case("slt", CmpInst::ICMP_SLT
)
2730 .Case("sle", CmpInst::ICMP_SLE
)
2731 .Case("ugt", CmpInst::ICMP_UGT
)
2732 .Case("uge", CmpInst::ICMP_UGE
)
2733 .Case("ult", CmpInst::ICMP_ULT
)
2734 .Case("ule", CmpInst::ICMP_ULE
)
2735 .Default(CmpInst::BAD_ICMP_PREDICATE
);
2736 if (!CmpInst::isIntPredicate(Pred
))
2737 return error("invalid integer predicate");
2741 Dest
= MachineOperand::CreatePredicate(Pred
);
2742 if (expectAndConsume(MIToken::rparen
))
2743 return error("predicate should be terminated by ')'.");
2748 bool MIParser::parseShuffleMaskOperand(MachineOperand
&Dest
) {
2749 assert(Token
.is(MIToken::kw_shufflemask
));
2752 if (expectAndConsume(MIToken::lparen
))
2753 return error("expected syntax shufflemask(<integer or undef>, ...)");
2755 SmallVector
<int, 32> ShufMask
;
2757 if (Token
.is(MIToken::kw_undef
)) {
2758 ShufMask
.push_back(-1);
2759 } else if (Token
.is(MIToken::IntegerLiteral
)) {
2760 const APSInt
&Int
= Token
.integerValue();
2761 ShufMask
.push_back(Int
.getExtValue());
2763 return error("expected integer constant");
2766 } while (consumeIfPresent(MIToken::comma
));
2768 if (expectAndConsume(MIToken::rparen
))
2769 return error("shufflemask should be terminated by ')'.");
2771 ArrayRef
<int> MaskAlloc
= MF
.allocateShuffleMask(ShufMask
);
2772 Dest
= MachineOperand::CreateShuffleMask(MaskAlloc
);
2776 bool MIParser::parseDbgInstrRefOperand(MachineOperand
&Dest
) {
2777 assert(Token
.is(MIToken::kw_dbg_instr_ref
));
2780 if (expectAndConsume(MIToken::lparen
))
2781 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2783 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isNegative())
2784 return error("expected unsigned integer for instruction index");
2785 uint64_t InstrIdx
= Token
.integerValue().getZExtValue();
2786 assert(InstrIdx
<= std::numeric_limits
<unsigned>::max() &&
2787 "Instruction reference's instruction index is too large");
2790 if (expectAndConsume(MIToken::comma
))
2791 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2793 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isNegative())
2794 return error("expected unsigned integer for operand index");
2795 uint64_t OpIdx
= Token
.integerValue().getZExtValue();
2796 assert(OpIdx
<= std::numeric_limits
<unsigned>::max() &&
2797 "Instruction reference's operand index is too large");
2800 if (expectAndConsume(MIToken::rparen
))
2801 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2803 Dest
= MachineOperand::CreateDbgInstrRef(InstrIdx
, OpIdx
);
2807 bool MIParser::parseTargetIndexOperand(MachineOperand
&Dest
) {
2808 assert(Token
.is(MIToken::kw_target_index
));
2810 if (expectAndConsume(MIToken::lparen
))
2812 if (Token
.isNot(MIToken::Identifier
))
2813 return error("expected the name of the target index");
2815 if (PFS
.Target
.getTargetIndex(Token
.stringValue(), Index
))
2816 return error("use of undefined target index '" + Token
.stringValue() + "'");
2818 if (expectAndConsume(MIToken::rparen
))
2820 Dest
= MachineOperand::CreateTargetIndex(unsigned(Index
), /*Offset=*/0);
2821 if (parseOperandsOffset(Dest
))
2826 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand
&Dest
) {
2827 assert(Token
.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2829 if (expectAndConsume(MIToken::lparen
))
2832 uint32_t *Mask
= MF
.allocateRegMask();
2834 if (Token
.isNot(MIToken::rparen
)) {
2835 if (Token
.isNot(MIToken::NamedRegister
))
2836 return error("expected a named register");
2838 if (parseNamedRegister(Reg
))
2841 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2844 // TODO: Report an error if the same register is used more than once.
2845 } while (consumeIfPresent(MIToken::comma
));
2847 if (expectAndConsume(MIToken::rparen
))
2849 Dest
= MachineOperand::CreateRegMask(Mask
);
2853 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
) {
2854 assert(Token
.is(MIToken::kw_liveout
));
2855 uint32_t *Mask
= MF
.allocateRegMask();
2857 if (expectAndConsume(MIToken::lparen
))
2860 if (Token
.isNot(MIToken::NamedRegister
))
2861 return error("expected a named register");
2863 if (parseNamedRegister(Reg
))
2866 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2867 // TODO: Report an error if the same register is used more than once.
2868 if (Token
.isNot(MIToken::comma
))
2872 if (expectAndConsume(MIToken::rparen
))
2874 Dest
= MachineOperand::CreateRegLiveOut(Mask
);
2878 bool MIParser::parseMachineOperand(const unsigned OpCode
, const unsigned OpIdx
,
2879 MachineOperand
&Dest
,
2880 std::optional
<unsigned> &TiedDefIdx
) {
2881 switch (Token
.kind()) {
2882 case MIToken::kw_implicit
:
2883 case MIToken::kw_implicit_define
:
2884 case MIToken::kw_def
:
2885 case MIToken::kw_dead
:
2886 case MIToken::kw_killed
:
2887 case MIToken::kw_undef
:
2888 case MIToken::kw_internal
:
2889 case MIToken::kw_early_clobber
:
2890 case MIToken::kw_debug_use
:
2891 case MIToken::kw_renamable
:
2892 case MIToken::underscore
:
2893 case MIToken::NamedRegister
:
2894 case MIToken::VirtualRegister
:
2895 case MIToken::NamedVirtualRegister
:
2896 return parseRegisterOperand(Dest
, TiedDefIdx
);
2897 case MIToken::IntegerLiteral
:
2898 return parseImmediateOperand(Dest
);
2899 case MIToken::kw_half
:
2900 case MIToken::kw_float
:
2901 case MIToken::kw_double
:
2902 case MIToken::kw_x86_fp80
:
2903 case MIToken::kw_fp128
:
2904 case MIToken::kw_ppc_fp128
:
2905 return parseFPImmediateOperand(Dest
);
2906 case MIToken::MachineBasicBlock
:
2907 return parseMBBOperand(Dest
);
2908 case MIToken::StackObject
:
2909 return parseStackObjectOperand(Dest
);
2910 case MIToken::FixedStackObject
:
2911 return parseFixedStackObjectOperand(Dest
);
2912 case MIToken::GlobalValue
:
2913 case MIToken::NamedGlobalValue
:
2914 return parseGlobalAddressOperand(Dest
);
2915 case MIToken::ConstantPoolItem
:
2916 return parseConstantPoolIndexOperand(Dest
);
2917 case MIToken::JumpTableIndex
:
2918 return parseJumpTableIndexOperand(Dest
);
2919 case MIToken::ExternalSymbol
:
2920 return parseExternalSymbolOperand(Dest
);
2921 case MIToken::MCSymbol
:
2922 return parseMCSymbolOperand(Dest
);
2923 case MIToken::SubRegisterIndex
:
2924 return parseSubRegisterIndexOperand(Dest
);
2925 case MIToken::md_diexpr
:
2926 case MIToken::exclaim
:
2927 return parseMetadataOperand(Dest
);
2928 case MIToken::kw_cfi_same_value
:
2929 case MIToken::kw_cfi_offset
:
2930 case MIToken::kw_cfi_rel_offset
:
2931 case MIToken::kw_cfi_def_cfa_register
:
2932 case MIToken::kw_cfi_def_cfa_offset
:
2933 case MIToken::kw_cfi_adjust_cfa_offset
:
2934 case MIToken::kw_cfi_escape
:
2935 case MIToken::kw_cfi_def_cfa
:
2936 case MIToken::kw_cfi_llvm_def_aspace_cfa
:
2937 case MIToken::kw_cfi_register
:
2938 case MIToken::kw_cfi_remember_state
:
2939 case MIToken::kw_cfi_restore
:
2940 case MIToken::kw_cfi_restore_state
:
2941 case MIToken::kw_cfi_undefined
:
2942 case MIToken::kw_cfi_window_save
:
2943 case MIToken::kw_cfi_aarch64_negate_ra_sign_state
:
2944 return parseCFIOperand(Dest
);
2945 case MIToken::kw_blockaddress
:
2946 return parseBlockAddressOperand(Dest
);
2947 case MIToken::kw_intrinsic
:
2948 return parseIntrinsicOperand(Dest
);
2949 case MIToken::kw_target_index
:
2950 return parseTargetIndexOperand(Dest
);
2951 case MIToken::kw_liveout
:
2952 return parseLiveoutRegisterMaskOperand(Dest
);
2953 case MIToken::kw_floatpred
:
2954 case MIToken::kw_intpred
:
2955 return parsePredicateOperand(Dest
);
2956 case MIToken::kw_shufflemask
:
2957 return parseShuffleMaskOperand(Dest
);
2958 case MIToken::kw_dbg_instr_ref
:
2959 return parseDbgInstrRefOperand(Dest
);
2960 case MIToken::Error
:
2962 case MIToken::Identifier
:
2963 if (const auto *RegMask
= PFS
.Target
.getRegMask(Token
.stringValue())) {
2964 Dest
= MachineOperand::CreateRegMask(RegMask
);
2967 } else if (Token
.stringValue() == "CustomRegMask") {
2968 return parseCustomRegisterMaskOperand(Dest
);
2970 return parseTypedImmediateOperand(Dest
);
2971 case MIToken::dot
: {
2972 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2973 if (const auto *Formatter
= TII
->getMIRFormatter()) {
2974 return parseTargetImmMnemonic(OpCode
, OpIdx
, Dest
, *Formatter
);
2979 // FIXME: Parse the MCSymbol machine operand.
2980 return error("expected a machine operand");
2985 bool MIParser::parseMachineOperandAndTargetFlags(
2986 const unsigned OpCode
, const unsigned OpIdx
, MachineOperand
&Dest
,
2987 std::optional
<unsigned> &TiedDefIdx
) {
2989 bool HasTargetFlags
= false;
2990 if (Token
.is(MIToken::kw_target_flags
)) {
2991 HasTargetFlags
= true;
2993 if (expectAndConsume(MIToken::lparen
))
2995 if (Token
.isNot(MIToken::Identifier
))
2996 return error("expected the name of the target flag");
2997 if (PFS
.Target
.getDirectTargetFlag(Token
.stringValue(), TF
)) {
2998 if (PFS
.Target
.getBitmaskTargetFlag(Token
.stringValue(), TF
))
2999 return error("use of undefined target flag '" + Token
.stringValue() +
3003 while (Token
.is(MIToken::comma
)) {
3005 if (Token
.isNot(MIToken::Identifier
))
3006 return error("expected the name of the target flag");
3007 unsigned BitFlag
= 0;
3008 if (PFS
.Target
.getBitmaskTargetFlag(Token
.stringValue(), BitFlag
))
3009 return error("use of undefined target flag '" + Token
.stringValue() +
3011 // TODO: Report an error when using a duplicate bit target flag.
3015 if (expectAndConsume(MIToken::rparen
))
3018 auto Loc
= Token
.location();
3019 if (parseMachineOperand(OpCode
, OpIdx
, Dest
, TiedDefIdx
))
3021 if (!HasTargetFlags
)
3024 return error(Loc
, "register operands can't have target flags");
3025 Dest
.setTargetFlags(TF
);
3029 bool MIParser::parseOffset(int64_t &Offset
) {
3030 if (Token
.isNot(MIToken::plus
) && Token
.isNot(MIToken::minus
))
3032 StringRef Sign
= Token
.range();
3033 bool IsNegative
= Token
.is(MIToken::minus
);
3035 if (Token
.isNot(MIToken::IntegerLiteral
))
3036 return error("expected an integer literal after '" + Sign
+ "'");
3037 if (Token
.integerValue().getSignificantBits() > 64)
3038 return error("expected 64-bit integer (too large)");
3039 Offset
= Token
.integerValue().getExtValue();
3046 bool MIParser::parseIRBlockAddressTaken(BasicBlock
*&BB
) {
3047 assert(Token
.is(MIToken::kw_ir_block_address_taken
));
3049 if (Token
.isNot(MIToken::IRBlock
) && Token
.isNot(MIToken::NamedIRBlock
))
3050 return error("expected basic block after 'ir_block_address_taken'");
3052 if (parseIRBlock(BB
, MF
.getFunction()))
3059 bool MIParser::parseAlignment(uint64_t &Alignment
) {
3060 assert(Token
.is(MIToken::kw_align
) || Token
.is(MIToken::kw_basealign
));
3062 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
3063 return error("expected an integer literal after 'align'");
3064 if (getUint64(Alignment
))
3068 if (!isPowerOf2_64(Alignment
))
3069 return error("expected a power-of-2 literal after 'align'");
3074 bool MIParser::parseAddrspace(unsigned &Addrspace
) {
3075 assert(Token
.is(MIToken::kw_addrspace
));
3077 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
3078 return error("expected an integer literal after 'addrspace'");
3079 if (getUnsigned(Addrspace
))
3085 bool MIParser::parseOperandsOffset(MachineOperand
&Op
) {
3087 if (parseOffset(Offset
))
3089 Op
.setOffset(Offset
);
3093 static bool parseIRValue(const MIToken
&Token
, PerFunctionMIParsingState
&PFS
,
3094 const Value
*&V
, ErrorCallbackType ErrCB
) {
3095 switch (Token
.kind()) {
3096 case MIToken::NamedIRValue
: {
3097 V
= PFS
.MF
.getFunction().getValueSymbolTable()->lookup(Token
.stringValue());
3100 case MIToken::IRValue
: {
3101 unsigned SlotNumber
= 0;
3102 if (getUnsigned(Token
, SlotNumber
, ErrCB
))
3104 V
= PFS
.getIRValue(SlotNumber
);
3107 case MIToken::NamedGlobalValue
:
3108 case MIToken::GlobalValue
: {
3109 GlobalValue
*GV
= nullptr;
3110 if (parseGlobalValue(Token
, PFS
, GV
, ErrCB
))
3115 case MIToken::QuotedIRValue
: {
3116 const Constant
*C
= nullptr;
3117 if (parseIRConstant(Token
.location(), Token
.stringValue(), PFS
, C
, ErrCB
))
3122 case MIToken::kw_unknown_address
:
3126 llvm_unreachable("The current token should be an IR block reference");
3129 return ErrCB(Token
.location(), Twine("use of undefined IR value '") + Token
.range() + "'");
3133 bool MIParser::parseIRValue(const Value
*&V
) {
3134 return ::parseIRValue(
3135 Token
, PFS
, V
, [this](StringRef::iterator Loc
, const Twine
&Msg
) -> bool {
3136 return error(Loc
, Msg
);
3140 bool MIParser::getUint64(uint64_t &Result
) {
3141 if (Token
.hasIntegerValue()) {
3142 if (Token
.integerValue().getActiveBits() > 64)
3143 return error("expected 64-bit integer (too large)");
3144 Result
= Token
.integerValue().getZExtValue();
3147 if (Token
.is(MIToken::HexLiteral
)) {
3151 if (A
.getBitWidth() > 64)
3152 return error("expected 64-bit integer (too large)");
3153 Result
= A
.getZExtValue();
3159 bool MIParser::getHexUint(APInt
&Result
) {
3160 return ::getHexUint(Token
, Result
);
3163 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
) {
3164 const auto OldFlags
= Flags
;
3165 switch (Token
.kind()) {
3166 case MIToken::kw_volatile
:
3167 Flags
|= MachineMemOperand::MOVolatile
;
3169 case MIToken::kw_non_temporal
:
3170 Flags
|= MachineMemOperand::MONonTemporal
;
3172 case MIToken::kw_dereferenceable
:
3173 Flags
|= MachineMemOperand::MODereferenceable
;
3175 case MIToken::kw_invariant
:
3176 Flags
|= MachineMemOperand::MOInvariant
;
3178 case MIToken::StringConstant
: {
3179 MachineMemOperand::Flags TF
;
3180 if (PFS
.Target
.getMMOTargetFlag(Token
.stringValue(), TF
))
3181 return error("use of undefined target MMO flag '" + Token
.stringValue() +
3187 llvm_unreachable("The current token should be a memory operand flag");
3189 if (OldFlags
== Flags
)
3190 // We know that the same flag is specified more than once when the flags
3191 // weren't modified.
3192 return error("duplicate '" + Token
.stringValue() + "' memory operand flag");
3197 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
) {
3198 switch (Token
.kind()) {
3199 case MIToken::kw_stack
:
3200 PSV
= MF
.getPSVManager().getStack();
3202 case MIToken::kw_got
:
3203 PSV
= MF
.getPSVManager().getGOT();
3205 case MIToken::kw_jump_table
:
3206 PSV
= MF
.getPSVManager().getJumpTable();
3208 case MIToken::kw_constant_pool
:
3209 PSV
= MF
.getPSVManager().getConstantPool();
3211 case MIToken::FixedStackObject
: {
3213 if (parseFixedStackFrameIndex(FI
))
3215 PSV
= MF
.getPSVManager().getFixedStack(FI
);
3216 // The token was already consumed, so use return here instead of break.
3219 case MIToken::StackObject
: {
3221 if (parseStackFrameIndex(FI
))
3223 PSV
= MF
.getPSVManager().getFixedStack(FI
);
3224 // The token was already consumed, so use return here instead of break.
3227 case MIToken::kw_call_entry
:
3229 switch (Token
.kind()) {
3230 case MIToken::GlobalValue
:
3231 case MIToken::NamedGlobalValue
: {
3232 GlobalValue
*GV
= nullptr;
3233 if (parseGlobalValue(GV
))
3235 PSV
= MF
.getPSVManager().getGlobalValueCallEntry(GV
);
3238 case MIToken::ExternalSymbol
:
3239 PSV
= MF
.getPSVManager().getExternalSymbolCallEntry(
3240 MF
.createExternalSymbolName(Token
.stringValue()));
3244 "expected a global value or an external symbol after 'call-entry'");
3247 case MIToken::kw_custom
: {
3249 const auto *TII
= MF
.getSubtarget().getInstrInfo();
3250 if (const auto *Formatter
= TII
->getMIRFormatter()) {
3251 if (Formatter
->parseCustomPseudoSourceValue(
3252 Token
.stringValue(), MF
, PFS
, PSV
,
3253 [this](StringRef::iterator Loc
, const Twine
&Msg
) -> bool {
3254 return error(Loc
, Msg
);
3258 return error("unable to parse target custom pseudo source value");
3262 llvm_unreachable("The current token should be pseudo source value");
3268 bool MIParser::parseMachinePointerInfo(MachinePointerInfo
&Dest
) {
3269 if (Token
.is(MIToken::kw_constant_pool
) || Token
.is(MIToken::kw_stack
) ||
3270 Token
.is(MIToken::kw_got
) || Token
.is(MIToken::kw_jump_table
) ||
3271 Token
.is(MIToken::FixedStackObject
) || Token
.is(MIToken::StackObject
) ||
3272 Token
.is(MIToken::kw_call_entry
) || Token
.is(MIToken::kw_custom
)) {
3273 const PseudoSourceValue
*PSV
= nullptr;
3274 if (parseMemoryPseudoSourceValue(PSV
))
3277 if (parseOffset(Offset
))
3279 Dest
= MachinePointerInfo(PSV
, Offset
);
3282 if (Token
.isNot(MIToken::NamedIRValue
) && Token
.isNot(MIToken::IRValue
) &&
3283 Token
.isNot(MIToken::GlobalValue
) &&
3284 Token
.isNot(MIToken::NamedGlobalValue
) &&
3285 Token
.isNot(MIToken::QuotedIRValue
) &&
3286 Token
.isNot(MIToken::kw_unknown_address
))
3287 return error("expected an IR value reference");
3288 const Value
*V
= nullptr;
3289 if (parseIRValue(V
))
3291 if (V
&& !V
->getType()->isPointerTy())
3292 return error("expected a pointer IR value");
3295 if (parseOffset(Offset
))
3297 Dest
= MachinePointerInfo(V
, Offset
);
3301 bool MIParser::parseOptionalScope(LLVMContext
&Context
,
3302 SyncScope::ID
&SSID
) {
3303 SSID
= SyncScope::System
;
3304 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "syncscope") {
3306 if (expectAndConsume(MIToken::lparen
))
3307 return error("expected '(' in syncscope");
3310 if (parseStringConstant(SSN
))
3313 SSID
= Context
.getOrInsertSyncScopeID(SSN
);
3314 if (expectAndConsume(MIToken::rparen
))
3315 return error("expected ')' in syncscope");
3321 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering
&Order
) {
3322 Order
= AtomicOrdering::NotAtomic
;
3323 if (Token
.isNot(MIToken::Identifier
))
3326 Order
= StringSwitch
<AtomicOrdering
>(Token
.stringValue())
3327 .Case("unordered", AtomicOrdering::Unordered
)
3328 .Case("monotonic", AtomicOrdering::Monotonic
)
3329 .Case("acquire", AtomicOrdering::Acquire
)
3330 .Case("release", AtomicOrdering::Release
)
3331 .Case("acq_rel", AtomicOrdering::AcquireRelease
)
3332 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent
)
3333 .Default(AtomicOrdering::NotAtomic
);
3335 if (Order
!= AtomicOrdering::NotAtomic
) {
3340 return error("expected an atomic scope, ordering or a size specification");
3343 bool MIParser::parseMachineMemoryOperand(MachineMemOperand
*&Dest
) {
3344 if (expectAndConsume(MIToken::lparen
))
3346 MachineMemOperand::Flags Flags
= MachineMemOperand::MONone
;
3347 while (Token
.isMemoryOperandFlag()) {
3348 if (parseMemoryOperandFlag(Flags
))
3351 if (Token
.isNot(MIToken::Identifier
) ||
3352 (Token
.stringValue() != "load" && Token
.stringValue() != "store"))
3353 return error("expected 'load' or 'store' memory operation");
3354 if (Token
.stringValue() == "load")
3355 Flags
|= MachineMemOperand::MOLoad
;
3357 Flags
|= MachineMemOperand::MOStore
;
3360 // Optional 'store' for operands that both load and store.
3361 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "store") {
3362 Flags
|= MachineMemOperand::MOStore
;
3366 // Optional synchronization scope.
3368 if (parseOptionalScope(MF
.getFunction().getContext(), SSID
))
3371 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3372 AtomicOrdering Order
, FailureOrder
;
3373 if (parseOptionalAtomicOrdering(Order
))
3376 if (parseOptionalAtomicOrdering(FailureOrder
))
3380 if (Token
.isNot(MIToken::IntegerLiteral
) &&
3381 Token
.isNot(MIToken::kw_unknown_size
) &&
3382 Token
.isNot(MIToken::lparen
))
3383 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3384 "memory operation");
3386 uint64_t Size
= MemoryLocation::UnknownSize
;
3387 if (Token
.is(MIToken::IntegerLiteral
)) {
3388 if (getUint64(Size
))
3391 // Convert from bytes to bits for storage.
3392 MemoryType
= LLT::scalar(8 * Size
);
3394 } else if (Token
.is(MIToken::kw_unknown_size
)) {
3395 Size
= MemoryLocation::UnknownSize
;
3398 if (expectAndConsume(MIToken::lparen
))
3400 if (parseLowLevelType(Token
.location(), MemoryType
))
3402 if (expectAndConsume(MIToken::rparen
))
3405 Size
= MemoryType
.getSizeInBytes();
3408 MachinePointerInfo Ptr
= MachinePointerInfo();
3409 if (Token
.is(MIToken::Identifier
)) {
3411 ((Flags
& MachineMemOperand::MOLoad
) &&
3412 (Flags
& MachineMemOperand::MOStore
))
3414 : Flags
& MachineMemOperand::MOLoad
? "from" : "into";
3415 if (Token
.stringValue() != Word
)
3416 return error(Twine("expected '") + Word
+ "'");
3419 if (parseMachinePointerInfo(Ptr
))
3422 uint64_t BaseAlignment
=
3423 (Size
!= MemoryLocation::UnknownSize
? PowerOf2Ceil(Size
) : 1);
3425 MDNode
*Range
= nullptr;
3426 while (consumeIfPresent(MIToken::comma
)) {
3427 switch (Token
.kind()) {
3428 case MIToken::kw_align
: {
3429 // align is printed if it is different than size.
3431 if (parseAlignment(Alignment
))
3433 if (Ptr
.Offset
& (Alignment
- 1)) {
3434 // MachineMemOperand::getAlign never returns a value greater than the
3435 // alignment of offset, so this just guards against hand-written MIR
3436 // that specifies a large "align" value when it should probably use
3437 // "basealign" instead.
3438 return error("specified alignment is more aligned than offset");
3440 BaseAlignment
= Alignment
;
3443 case MIToken::kw_basealign
:
3444 // basealign is printed if it is different than align.
3445 if (parseAlignment(BaseAlignment
))
3448 case MIToken::kw_addrspace
:
3449 if (parseAddrspace(Ptr
.AddrSpace
))
3452 case MIToken::md_tbaa
:
3454 if (parseMDNode(AAInfo
.TBAA
))
3457 case MIToken::md_alias_scope
:
3459 if (parseMDNode(AAInfo
.Scope
))
3462 case MIToken::md_noalias
:
3464 if (parseMDNode(AAInfo
.NoAlias
))
3467 case MIToken::md_range
:
3469 if (parseMDNode(Range
))
3472 // TODO: Report an error on duplicate metadata nodes.
3474 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3475 "'!noalias' or '!range'");
3478 if (expectAndConsume(MIToken::rparen
))
3480 Dest
= MF
.getMachineMemOperand(Ptr
, Flags
, MemoryType
, Align(BaseAlignment
),
3481 AAInfo
, Range
, SSID
, Order
, FailureOrder
);
3485 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
) {
3486 assert((Token
.is(MIToken::kw_pre_instr_symbol
) ||
3487 Token
.is(MIToken::kw_post_instr_symbol
)) &&
3488 "Invalid token for a pre- post-instruction symbol!");
3490 if (Token
.isNot(MIToken::MCSymbol
))
3491 return error("expected a symbol after 'pre-instr-symbol'");
3492 Symbol
= getOrCreateMCSymbol(Token
.stringValue());
3494 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
3495 Token
.is(MIToken::lbrace
))
3497 if (Token
.isNot(MIToken::comma
))
3498 return error("expected ',' before the next machine operand");
3503 bool MIParser::parseHeapAllocMarker(MDNode
*&Node
) {
3504 assert(Token
.is(MIToken::kw_heap_alloc_marker
) &&
3505 "Invalid token for a heap alloc marker!");
3507 if (parseMDNode(Node
))
3510 return error("expected a MDNode after 'heap-alloc-marker'");
3511 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
3512 Token
.is(MIToken::lbrace
))
3514 if (Token
.isNot(MIToken::comma
))
3515 return error("expected ',' before the next machine operand");
3520 bool MIParser::parsePCSections(MDNode
*&Node
) {
3521 assert(Token
.is(MIToken::kw_pcsections
) &&
3522 "Invalid token for a PC sections!");
3524 if (parseMDNode(Node
))
3527 return error("expected a MDNode after 'pcsections'");
3528 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
3529 Token
.is(MIToken::lbrace
))
3531 if (Token
.isNot(MIToken::comma
))
3532 return error("expected ',' before the next machine operand");
3537 static void initSlots2BasicBlocks(
3539 DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
3540 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
3541 MST
.incorporateFunction(F
);
3542 for (const auto &BB
: F
) {
3545 int Slot
= MST
.getLocalSlot(&BB
);
3548 Slots2BasicBlocks
.insert(std::make_pair(unsigned(Slot
), &BB
));
3552 static const BasicBlock
*getIRBlockFromSlot(
3554 const DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
3555 return Slots2BasicBlocks
.lookup(Slot
);
3558 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
) {
3559 if (Slots2BasicBlocks
.empty())
3560 initSlots2BasicBlocks(MF
.getFunction(), Slots2BasicBlocks
);
3561 return getIRBlockFromSlot(Slot
, Slots2BasicBlocks
);
3564 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
, const Function
&F
) {
3565 if (&F
== &MF
.getFunction())
3566 return getIRBlock(Slot
);
3567 DenseMap
<unsigned, const BasicBlock
*> CustomSlots2BasicBlocks
;
3568 initSlots2BasicBlocks(F
, CustomSlots2BasicBlocks
);
3569 return getIRBlockFromSlot(Slot
, CustomSlots2BasicBlocks
);
3572 MCSymbol
*MIParser::getOrCreateMCSymbol(StringRef Name
) {
3573 // FIXME: Currently we can't recognize temporary or local symbols and call all
3574 // of the appropriate forms to create them. However, this handles basic cases
3575 // well as most of the special aspects are recognized by a prefix on their
3576 // name, and the input names should already be unique. For test cases, keeping
3577 // the symbol name out of the symbol table isn't terribly important.
3578 return MF
.getContext().getOrCreateSymbol(Name
);
3581 bool MIParser::parseStringConstant(std::string
&Result
) {
3582 if (Token
.isNot(MIToken::StringConstant
))
3583 return error("expected string constant");
3584 Result
= std::string(Token
.stringValue());
3589 bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState
&PFS
,
3591 SMDiagnostic
&Error
) {
3592 return MIParser(PFS
, Error
, Src
).parseBasicBlockDefinitions(PFS
.MBBSlots
);
3595 bool llvm::parseMachineInstructions(PerFunctionMIParsingState
&PFS
,
3596 StringRef Src
, SMDiagnostic
&Error
) {
3597 return MIParser(PFS
, Error
, Src
).parseBasicBlocks();
3600 bool llvm::parseMBBReference(PerFunctionMIParsingState
&PFS
,
3601 MachineBasicBlock
*&MBB
, StringRef Src
,
3602 SMDiagnostic
&Error
) {
3603 return MIParser(PFS
, Error
, Src
).parseStandaloneMBB(MBB
);
3606 bool llvm::parseRegisterReference(PerFunctionMIParsingState
&PFS
,
3607 Register
&Reg
, StringRef Src
,
3608 SMDiagnostic
&Error
) {
3609 return MIParser(PFS
, Error
, Src
).parseStandaloneRegister(Reg
);
3612 bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState
&PFS
,
3613 Register
&Reg
, StringRef Src
,
3614 SMDiagnostic
&Error
) {
3615 return MIParser(PFS
, Error
, Src
).parseStandaloneNamedRegister(Reg
);
3618 bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState
&PFS
,
3619 VRegInfo
*&Info
, StringRef Src
,
3620 SMDiagnostic
&Error
) {
3621 return MIParser(PFS
, Error
, Src
).parseStandaloneVirtualRegister(Info
);
3624 bool llvm::parseStackObjectReference(PerFunctionMIParsingState
&PFS
,
3625 int &FI
, StringRef Src
,
3626 SMDiagnostic
&Error
) {
3627 return MIParser(PFS
, Error
, Src
).parseStandaloneStackObject(FI
);
3630 bool llvm::parseMDNode(PerFunctionMIParsingState
&PFS
,
3631 MDNode
*&Node
, StringRef Src
, SMDiagnostic
&Error
) {
3632 return MIParser(PFS
, Error
, Src
).parseStandaloneMDNode(Node
);
3635 bool llvm::parseMachineMetadata(PerFunctionMIParsingState
&PFS
, StringRef Src
,
3636 SMRange SrcRange
, SMDiagnostic
&Error
) {
3637 return MIParser(PFS
, Error
, Src
, SrcRange
).parseMachineMetadata();
3640 bool MIRFormatter::parseIRValue(StringRef Src
, MachineFunction
&MF
,
3641 PerFunctionMIParsingState
&PFS
, const Value
*&V
,
3642 ErrorCallbackType ErrorCallback
) {
3644 Src
= lexMIToken(Src
, Token
, [&](StringRef::iterator Loc
, const Twine
&Msg
) {
3645 ErrorCallback(Loc
, Msg
);
3649 return ::parseIRValue(Token
, PFS
, V
, ErrorCallback
);