1 //===- MIParser.cpp - Machine instructions parser implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the parsing of machine instructions.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Analysis/MemoryLocation.h"
28 #include "llvm/AsmParser/Parser.h"
29 #include "llvm/AsmParser/SlotMapping.h"
30 #include "llvm/CodeGen/MIRPrinter.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/TargetInstrInfo.h"
40 #include "llvm/CodeGen/TargetRegisterInfo.h"
41 #include "llvm/CodeGen/TargetSubtargetInfo.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DataLayout.h"
45 #include "llvm/IR/DebugInfoMetadata.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/InstrTypes.h"
49 #include "llvm/IR/Instructions.h"
50 #include "llvm/IR/Intrinsics.h"
51 #include "llvm/IR/Metadata.h"
52 #include "llvm/IR/Module.h"
53 #include "llvm/IR/ModuleSlotTracker.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/IR/Value.h"
56 #include "llvm/IR/ValueSymbolTable.h"
57 #include "llvm/MC/LaneBitmask.h"
58 #include "llvm/MC/MCContext.h"
59 #include "llvm/MC/MCDwarf.h"
60 #include "llvm/MC/MCInstrDesc.h"
61 #include "llvm/MC/MCRegisterInfo.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/LowLevelTypeImpl.h"
67 #include "llvm/Support/MemoryBuffer.h"
68 #include "llvm/Support/SMLoc.h"
69 #include "llvm/Support/SourceMgr.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Target/TargetIntrinsicInfo.h"
72 #include "llvm/Target/TargetMachine.h"
84 PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction
&MF
,
85 SourceMgr
&SM
, const SlotMapping
&IRSlots
,
86 const Name2RegClassMap
&Names2RegClasses
,
87 const Name2RegBankMap
&Names2RegBanks
)
88 : MF(MF
), SM(&SM
), IRSlots(IRSlots
), Names2RegClasses(Names2RegClasses
),
89 Names2RegBanks(Names2RegBanks
) {
92 VRegInfo
&PerFunctionMIParsingState::getVRegInfo(unsigned Num
) {
93 auto I
= VRegInfos
.insert(std::make_pair(Num
, nullptr));
95 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
96 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
97 Info
->VReg
= MRI
.createIncompleteVirtualRegister();
98 I
.first
->second
= Info
;
100 return *I
.first
->second
;
103 VRegInfo
&PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName
) {
104 assert(RegName
!= "" && "Expected named reg.");
106 auto I
= VRegInfosNamed
.insert(std::make_pair(RegName
.str(), nullptr));
108 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
109 Info
->VReg
= MF
.getRegInfo().createIncompleteVirtualRegister(RegName
);
110 I
.first
->second
= Info
;
112 return *I
.first
->second
;
117 /// A wrapper struct around the 'MachineOperand' struct that includes a source
118 /// range and other attributes.
119 struct ParsedMachineOperand
{
120 MachineOperand Operand
;
121 StringRef::iterator Begin
;
122 StringRef::iterator End
;
123 Optional
<unsigned> TiedDefIdx
;
125 ParsedMachineOperand(const MachineOperand
&Operand
, StringRef::iterator Begin
,
126 StringRef::iterator End
, Optional
<unsigned> &TiedDefIdx
)
127 : Operand(Operand
), Begin(Begin
), End(End
), TiedDefIdx(TiedDefIdx
) {
129 assert(Operand
.isReg() && Operand
.isUse() &&
130 "Only used register operands can be tied");
137 StringRef Source
, CurrentSource
;
139 PerFunctionMIParsingState
&PFS
;
140 /// Maps from instruction names to op codes.
141 StringMap
<unsigned> Names2InstrOpCodes
;
142 /// Maps from register names to registers.
143 StringMap
<unsigned> Names2Regs
;
144 /// Maps from register mask names to register masks.
145 StringMap
<const uint32_t *> Names2RegMasks
;
146 /// Maps from subregister names to subregister indices.
147 StringMap
<unsigned> Names2SubRegIndices
;
148 /// Maps from slot numbers to function's unnamed basic blocks.
149 DenseMap
<unsigned, const BasicBlock
*> Slots2BasicBlocks
;
150 /// Maps from slot numbers to function's unnamed values.
151 DenseMap
<unsigned, const Value
*> Slots2Values
;
152 /// Maps from target index names to target indices.
153 StringMap
<int> Names2TargetIndices
;
154 /// Maps from direct target flag names to the direct target flag values.
155 StringMap
<unsigned> Names2DirectTargetFlags
;
156 /// Maps from direct target flag names to the bitmask target flag values.
157 StringMap
<unsigned> Names2BitmaskTargetFlags
;
158 /// Maps from MMO target flag names to MMO target flag values.
159 StringMap
<MachineMemOperand::Flags
> Names2MMOTargetFlags
;
162 MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
165 /// \p SkipChar gives the number of characters to skip before looking
166 /// for the next token.
167 void lex(unsigned SkipChar
= 0);
169 /// Report an error at the current location with the given message.
171 /// This function always return true.
172 bool error(const Twine
&Msg
);
174 /// Report an error at the given location with the given message.
176 /// This function always return true.
177 bool error(StringRef::iterator Loc
, const Twine
&Msg
);
180 parseBasicBlockDefinitions(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
181 bool parseBasicBlocks();
182 bool parse(MachineInstr
*&MI
);
183 bool parseStandaloneMBB(MachineBasicBlock
*&MBB
);
184 bool parseStandaloneNamedRegister(unsigned &Reg
);
185 bool parseStandaloneVirtualRegister(VRegInfo
*&Info
);
186 bool parseStandaloneRegister(unsigned &Reg
);
187 bool parseStandaloneStackObject(int &FI
);
188 bool parseStandaloneMDNode(MDNode
*&Node
);
191 parseBasicBlockDefinition(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
192 bool parseBasicBlock(MachineBasicBlock
&MBB
,
193 MachineBasicBlock
*&AddFalthroughFrom
);
194 bool parseBasicBlockLiveins(MachineBasicBlock
&MBB
);
195 bool parseBasicBlockSuccessors(MachineBasicBlock
&MBB
);
197 bool parseNamedRegister(unsigned &Reg
);
198 bool parseVirtualRegister(VRegInfo
*&Info
);
199 bool parseNamedVirtualRegister(VRegInfo
*&Info
);
200 bool parseRegister(unsigned &Reg
, VRegInfo
*&VRegInfo
);
201 bool parseRegisterFlag(unsigned &Flags
);
202 bool parseRegisterClassOrBank(VRegInfo
&RegInfo
);
203 bool parseSubRegisterIndex(unsigned &SubReg
);
204 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx
);
205 bool parseRegisterOperand(MachineOperand
&Dest
,
206 Optional
<unsigned> &TiedDefIdx
, bool IsDef
= false);
207 bool parseImmediateOperand(MachineOperand
&Dest
);
208 bool parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
210 bool parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
);
211 bool parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
);
212 bool parseTypedImmediateOperand(MachineOperand
&Dest
);
213 bool parseFPImmediateOperand(MachineOperand
&Dest
);
214 bool parseMBBReference(MachineBasicBlock
*&MBB
);
215 bool parseMBBOperand(MachineOperand
&Dest
);
216 bool parseStackFrameIndex(int &FI
);
217 bool parseStackObjectOperand(MachineOperand
&Dest
);
218 bool parseFixedStackFrameIndex(int &FI
);
219 bool parseFixedStackObjectOperand(MachineOperand
&Dest
);
220 bool parseGlobalValue(GlobalValue
*&GV
);
221 bool parseGlobalAddressOperand(MachineOperand
&Dest
);
222 bool parseConstantPoolIndexOperand(MachineOperand
&Dest
);
223 bool parseSubRegisterIndexOperand(MachineOperand
&Dest
);
224 bool parseJumpTableIndexOperand(MachineOperand
&Dest
);
225 bool parseExternalSymbolOperand(MachineOperand
&Dest
);
226 bool parseMCSymbolOperand(MachineOperand
&Dest
);
227 bool parseMDNode(MDNode
*&Node
);
228 bool parseDIExpression(MDNode
*&Expr
);
229 bool parseMetadataOperand(MachineOperand
&Dest
);
230 bool parseCFIOffset(int &Offset
);
231 bool parseCFIRegister(unsigned &Reg
);
232 bool parseCFIEscapeValues(std::string
& Values
);
233 bool parseCFIOperand(MachineOperand
&Dest
);
234 bool parseIRBlock(BasicBlock
*&BB
, const Function
&F
);
235 bool parseBlockAddressOperand(MachineOperand
&Dest
);
236 bool parseIntrinsicOperand(MachineOperand
&Dest
);
237 bool parsePredicateOperand(MachineOperand
&Dest
);
238 bool parseTargetIndexOperand(MachineOperand
&Dest
);
239 bool parseCustomRegisterMaskOperand(MachineOperand
&Dest
);
240 bool parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
);
241 bool parseMachineOperand(MachineOperand
&Dest
,
242 Optional
<unsigned> &TiedDefIdx
);
243 bool parseMachineOperandAndTargetFlags(MachineOperand
&Dest
,
244 Optional
<unsigned> &TiedDefIdx
);
245 bool parseOffset(int64_t &Offset
);
246 bool parseAlignment(unsigned &Alignment
);
247 bool parseAddrspace(unsigned &Addrspace
);
248 bool parseOperandsOffset(MachineOperand
&Op
);
249 bool parseIRValue(const Value
*&V
);
250 bool parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
);
251 bool parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
);
252 bool parseMachinePointerInfo(MachinePointerInfo
&Dest
);
253 bool parseOptionalScope(LLVMContext
&Context
, SyncScope::ID
&SSID
);
254 bool parseOptionalAtomicOrdering(AtomicOrdering
&Order
);
255 bool parseMachineMemoryOperand(MachineMemOperand
*&Dest
);
256 bool parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
);
259 /// Convert the integer literal in the current token into an unsigned integer.
261 /// Return true if an error occurred.
262 bool getUnsigned(unsigned &Result
);
264 /// Convert the integer literal in the current token into an uint64.
266 /// Return true if an error occurred.
267 bool getUint64(uint64_t &Result
);
269 /// Convert the hexadecimal literal in the current token into an unsigned
270 /// APInt with a minimum bitwidth required to represent the value.
272 /// Return true if the literal does not represent an integer value.
273 bool getHexUint(APInt
&Result
);
275 /// If the current token is of the given kind, consume it and return false.
276 /// Otherwise report an error and return true.
277 bool expectAndConsume(MIToken::TokenKind TokenKind
);
279 /// If the current token is of the given kind, consume it and return true.
280 /// Otherwise return false.
281 bool consumeIfPresent(MIToken::TokenKind TokenKind
);
283 void initNames2InstrOpCodes();
285 /// Try to convert an instruction name to an opcode. Return true if the
286 /// instruction name is invalid.
287 bool parseInstrName(StringRef InstrName
, unsigned &OpCode
);
289 bool parseInstruction(unsigned &OpCode
, unsigned &Flags
);
291 bool assignRegisterTies(MachineInstr
&MI
,
292 ArrayRef
<ParsedMachineOperand
> Operands
);
294 bool verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
295 const MCInstrDesc
&MCID
);
297 void initNames2Regs();
299 /// Try to convert a register name to a register number. Return true if the
300 /// register name is invalid.
301 bool getRegisterByName(StringRef RegName
, unsigned &Reg
);
303 void initNames2RegMasks();
305 /// Check if the given identifier is a name of a register mask.
307 /// Return null if the identifier isn't a register mask.
308 const uint32_t *getRegMask(StringRef Identifier
);
310 void initNames2SubRegIndices();
312 /// Check if the given identifier is a name of a subregister index.
314 /// Return 0 if the name isn't a subregister index class.
315 unsigned getSubRegIndex(StringRef Name
);
317 const BasicBlock
*getIRBlock(unsigned Slot
);
318 const BasicBlock
*getIRBlock(unsigned Slot
, const Function
&F
);
320 const Value
*getIRValue(unsigned Slot
);
322 void initNames2TargetIndices();
324 /// Try to convert a name of target index to the corresponding target index.
326 /// Return true if the name isn't a name of a target index.
327 bool getTargetIndex(StringRef Name
, int &Index
);
329 void initNames2DirectTargetFlags();
331 /// Try to convert a name of a direct target flag to the corresponding
334 /// Return true if the name isn't a name of a direct flag.
335 bool getDirectTargetFlag(StringRef Name
, unsigned &Flag
);
337 void initNames2BitmaskTargetFlags();
339 /// Try to convert a name of a bitmask target flag to the corresponding
342 /// Return true if the name isn't a name of a bitmask target flag.
343 bool getBitmaskTargetFlag(StringRef Name
, unsigned &Flag
);
345 void initNames2MMOTargetFlags();
347 /// Try to convert a name of a MachineMemOperand target flag to the
348 /// corresponding target flag.
350 /// Return true if the name isn't a name of a target MMO flag.
351 bool getMMOTargetFlag(StringRef Name
, MachineMemOperand::Flags
&Flag
);
353 /// Get or create an MCSymbol for a given name.
354 MCSymbol
*getOrCreateMCSymbol(StringRef Name
);
356 /// parseStringConstant
357 /// ::= StringConstant
358 bool parseStringConstant(std::string
&Result
);
361 } // end anonymous namespace
363 MIParser::MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
365 : MF(PFS
.MF
), Error(Error
), Source(Source
), CurrentSource(Source
), PFS(PFS
)
368 void MIParser::lex(unsigned SkipChar
) {
369 CurrentSource
= lexMIToken(
370 CurrentSource
.data() + SkipChar
, Token
,
371 [this](StringRef::iterator Loc
, const Twine
&Msg
) { error(Loc
, Msg
); });
374 bool MIParser::error(const Twine
&Msg
) { return error(Token
.location(), Msg
); }
376 bool MIParser::error(StringRef::iterator Loc
, const Twine
&Msg
) {
377 const SourceMgr
&SM
= *PFS
.SM
;
378 assert(Loc
>= Source
.data() && Loc
<= (Source
.data() + Source
.size()));
379 const MemoryBuffer
&Buffer
= *SM
.getMemoryBuffer(SM
.getMainFileID());
380 if (Loc
>= Buffer
.getBufferStart() && Loc
<= Buffer
.getBufferEnd()) {
381 // Create an ordinary diagnostic when the source manager's buffer is the
383 Error
= SM
.GetMessage(SMLoc::getFromPointer(Loc
), SourceMgr::DK_Error
, Msg
);
386 // Create a diagnostic for a YAML string literal.
387 Error
= SMDiagnostic(SM
, SMLoc(), Buffer
.getBufferIdentifier(), 1,
388 Loc
- Source
.data(), SourceMgr::DK_Error
, Msg
.str(),
393 static const char *toString(MIToken::TokenKind TokenKind
) {
401 case MIToken::lparen
:
403 case MIToken::rparen
:
406 return "<unknown token>";
410 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind
) {
411 if (Token
.isNot(TokenKind
))
412 return error(Twine("expected ") + toString(TokenKind
));
417 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind
) {
418 if (Token
.isNot(TokenKind
))
424 bool MIParser::parseBasicBlockDefinition(
425 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
426 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
430 auto Loc
= Token
.location();
431 auto Name
= Token
.stringValue();
433 bool HasAddressTaken
= false;
434 bool IsLandingPad
= false;
435 unsigned Alignment
= 0;
436 BasicBlock
*BB
= nullptr;
437 if (consumeIfPresent(MIToken::lparen
)) {
439 // TODO: Report an error when multiple same attributes are specified.
440 switch (Token
.kind()) {
441 case MIToken::kw_address_taken
:
442 HasAddressTaken
= true;
445 case MIToken::kw_landing_pad
:
449 case MIToken::kw_align
:
450 if (parseAlignment(Alignment
))
453 case MIToken::IRBlock
:
454 // TODO: Report an error when both name and ir block are specified.
455 if (parseIRBlock(BB
, MF
.getFunction()))
462 } while (consumeIfPresent(MIToken::comma
));
463 if (expectAndConsume(MIToken::rparen
))
466 if (expectAndConsume(MIToken::colon
))
470 BB
= dyn_cast_or_null
<BasicBlock
>(
471 MF
.getFunction().getValueSymbolTable()->lookup(Name
));
473 return error(Loc
, Twine("basic block '") + Name
+
474 "' is not defined in the function '" +
477 auto *MBB
= MF
.CreateMachineBasicBlock(BB
);
478 MF
.insert(MF
.end(), MBB
);
479 bool WasInserted
= MBBSlots
.insert(std::make_pair(ID
, MBB
)).second
;
481 return error(Loc
, Twine("redefinition of machine basic block with id #") +
484 MBB
->setAlignment(Alignment
);
486 MBB
->setHasAddressTaken();
487 MBB
->setIsEHPad(IsLandingPad
);
491 bool MIParser::parseBasicBlockDefinitions(
492 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
494 // Skip until the first machine basic block.
495 while (Token
.is(MIToken::Newline
))
497 if (Token
.isErrorOrEOF())
498 return Token
.isError();
499 if (Token
.isNot(MIToken::MachineBasicBlockLabel
))
500 return error("expected a basic block definition before instructions");
501 unsigned BraceDepth
= 0;
503 if (parseBasicBlockDefinition(MBBSlots
))
505 bool IsAfterNewline
= false;
506 // Skip until the next machine basic block.
508 if ((Token
.is(MIToken::MachineBasicBlockLabel
) && IsAfterNewline
) ||
509 Token
.isErrorOrEOF())
511 else if (Token
.is(MIToken::MachineBasicBlockLabel
))
512 return error("basic block definition should be located at the start of "
514 else if (consumeIfPresent(MIToken::Newline
)) {
515 IsAfterNewline
= true;
518 IsAfterNewline
= false;
519 if (Token
.is(MIToken::lbrace
))
521 if (Token
.is(MIToken::rbrace
)) {
523 return error("extraneous closing brace ('}')");
528 // Verify that we closed all of the '{' at the end of a file or a block.
529 if (!Token
.isError() && BraceDepth
)
530 return error("expected '}'"); // FIXME: Report a note that shows '{'.
531 } while (!Token
.isErrorOrEOF());
532 return Token
.isError();
535 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock
&MBB
) {
536 assert(Token
.is(MIToken::kw_liveins
));
538 if (expectAndConsume(MIToken::colon
))
540 if (Token
.isNewlineOrEOF()) // Allow an empty list of liveins.
543 if (Token
.isNot(MIToken::NamedRegister
))
544 return error("expected a named register");
546 if (parseNamedRegister(Reg
))
549 LaneBitmask Mask
= LaneBitmask::getAll();
550 if (consumeIfPresent(MIToken::colon
)) {
552 if (Token
.isNot(MIToken::IntegerLiteral
) &&
553 Token
.isNot(MIToken::HexLiteral
))
554 return error("expected a lane mask");
555 static_assert(sizeof(LaneBitmask::Type
) == sizeof(unsigned),
556 "Use correct get-function for lane mask");
559 return error("invalid lane mask value");
560 Mask
= LaneBitmask(V
);
563 MBB
.addLiveIn(Reg
, Mask
);
564 } while (consumeIfPresent(MIToken::comma
));
568 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock
&MBB
) {
569 assert(Token
.is(MIToken::kw_successors
));
571 if (expectAndConsume(MIToken::colon
))
573 if (Token
.isNewlineOrEOF()) // Allow an empty list of successors.
576 if (Token
.isNot(MIToken::MachineBasicBlock
))
577 return error("expected a machine basic block reference");
578 MachineBasicBlock
*SuccMBB
= nullptr;
579 if (parseMBBReference(SuccMBB
))
583 if (consumeIfPresent(MIToken::lparen
)) {
584 if (Token
.isNot(MIToken::IntegerLiteral
) &&
585 Token
.isNot(MIToken::HexLiteral
))
586 return error("expected an integer literal after '('");
587 if (getUnsigned(Weight
))
590 if (expectAndConsume(MIToken::rparen
))
593 MBB
.addSuccessor(SuccMBB
, BranchProbability::getRaw(Weight
));
594 } while (consumeIfPresent(MIToken::comma
));
595 MBB
.normalizeSuccProbs();
599 bool MIParser::parseBasicBlock(MachineBasicBlock
&MBB
,
600 MachineBasicBlock
*&AddFalthroughFrom
) {
601 // Skip the definition.
602 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
604 if (consumeIfPresent(MIToken::lparen
)) {
605 while (Token
.isNot(MIToken::rparen
) && !Token
.isErrorOrEOF())
607 consumeIfPresent(MIToken::rparen
);
609 consumeIfPresent(MIToken::colon
);
611 // Parse the liveins and successors.
612 // N.B: Multiple lists of successors and liveins are allowed and they're
619 // liveins: %edi, %esi
620 bool ExplicitSuccessors
= false;
622 if (Token
.is(MIToken::kw_successors
)) {
623 if (parseBasicBlockSuccessors(MBB
))
625 ExplicitSuccessors
= true;
626 } else if (Token
.is(MIToken::kw_liveins
)) {
627 if (parseBasicBlockLiveins(MBB
))
629 } else if (consumeIfPresent(MIToken::Newline
)) {
633 if (!Token
.isNewlineOrEOF())
634 return error("expected line break at the end of a list");
638 // Parse the instructions.
639 bool IsInBundle
= false;
640 MachineInstr
*PrevMI
= nullptr;
641 while (!Token
.is(MIToken::MachineBasicBlockLabel
) &&
642 !Token
.is(MIToken::Eof
)) {
643 if (consumeIfPresent(MIToken::Newline
))
645 if (consumeIfPresent(MIToken::rbrace
)) {
646 // The first parsing pass should verify that all closing '}' have an
652 MachineInstr
*MI
= nullptr;
655 MBB
.insert(MBB
.end(), MI
);
657 PrevMI
->setFlag(MachineInstr::BundledSucc
);
658 MI
->setFlag(MachineInstr::BundledPred
);
661 if (Token
.is(MIToken::lbrace
)) {
663 return error("nested instruction bundles are not allowed");
665 // This instruction is the start of the bundle.
666 MI
->setFlag(MachineInstr::BundledSucc
);
668 if (!Token
.is(MIToken::Newline
))
669 // The next instruction can be on the same line.
672 assert(Token
.isNewlineOrEOF() && "MI is not fully parsed");
676 // Construct successor list by searching for basic block machine operands.
677 if (!ExplicitSuccessors
) {
678 SmallVector
<MachineBasicBlock
*,4> Successors
;
680 guessSuccessors(MBB
, Successors
, IsFallthrough
);
681 for (MachineBasicBlock
*Succ
: Successors
)
682 MBB
.addSuccessor(Succ
);
685 AddFalthroughFrom
= &MBB
;
687 MBB
.normalizeSuccProbs();
694 bool MIParser::parseBasicBlocks() {
696 // Skip until the first machine basic block.
697 while (Token
.is(MIToken::Newline
))
699 if (Token
.isErrorOrEOF())
700 return Token
.isError();
701 // The first parsing pass should have verified that this token is a MBB label
702 // in the 'parseBasicBlockDefinitions' method.
703 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
704 MachineBasicBlock
*AddFalthroughFrom
= nullptr;
706 MachineBasicBlock
*MBB
= nullptr;
707 if (parseMBBReference(MBB
))
709 if (AddFalthroughFrom
) {
710 if (!AddFalthroughFrom
->isSuccessor(MBB
))
711 AddFalthroughFrom
->addSuccessor(MBB
);
712 AddFalthroughFrom
->normalizeSuccProbs();
713 AddFalthroughFrom
= nullptr;
715 if (parseBasicBlock(*MBB
, AddFalthroughFrom
))
717 // The method 'parseBasicBlock' should parse the whole block until the next
718 // block or the end of file.
719 assert(Token
.is(MIToken::MachineBasicBlockLabel
) || Token
.is(MIToken::Eof
));
720 } while (Token
.isNot(MIToken::Eof
));
724 bool MIParser::parse(MachineInstr
*&MI
) {
725 // Parse any register operands before '='
726 MachineOperand MO
= MachineOperand::CreateImm(0);
727 SmallVector
<ParsedMachineOperand
, 8> Operands
;
728 while (Token
.isRegister() || Token
.isRegisterFlag()) {
729 auto Loc
= Token
.location();
730 Optional
<unsigned> TiedDefIdx
;
731 if (parseRegisterOperand(MO
, TiedDefIdx
, /*IsDef=*/true))
734 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
735 if (Token
.isNot(MIToken::comma
))
739 if (!Operands
.empty() && expectAndConsume(MIToken::equal
))
742 unsigned OpCode
, Flags
= 0;
743 if (Token
.isError() || parseInstruction(OpCode
, Flags
))
746 // Parse the remaining machine operands.
747 while (!Token
.isNewlineOrEOF() && Token
.isNot(MIToken::kw_pre_instr_symbol
) &&
748 Token
.isNot(MIToken::kw_post_instr_symbol
) &&
749 Token
.isNot(MIToken::kw_debug_location
) &&
750 Token
.isNot(MIToken::coloncolon
) && Token
.isNot(MIToken::lbrace
)) {
751 auto Loc
= Token
.location();
752 Optional
<unsigned> TiedDefIdx
;
753 if (parseMachineOperandAndTargetFlags(MO
, TiedDefIdx
))
756 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
757 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
758 Token
.is(MIToken::lbrace
))
760 if (Token
.isNot(MIToken::comma
))
761 return error("expected ',' before the next machine operand");
765 MCSymbol
*PreInstrSymbol
= nullptr;
766 if (Token
.is(MIToken::kw_pre_instr_symbol
))
767 if (parsePreOrPostInstrSymbol(PreInstrSymbol
))
769 MCSymbol
*PostInstrSymbol
= nullptr;
770 if (Token
.is(MIToken::kw_post_instr_symbol
))
771 if (parsePreOrPostInstrSymbol(PostInstrSymbol
))
774 DebugLoc DebugLocation
;
775 if (Token
.is(MIToken::kw_debug_location
)) {
777 if (Token
.isNot(MIToken::exclaim
))
778 return error("expected a metadata node after 'debug-location'");
779 MDNode
*Node
= nullptr;
780 if (parseMDNode(Node
))
782 if (!isa
<DILocation
>(Node
))
783 return error("referenced metadata is not a DILocation");
784 DebugLocation
= DebugLoc(Node
);
787 // Parse the machine memory operands.
788 SmallVector
<MachineMemOperand
*, 2> MemOperands
;
789 if (Token
.is(MIToken::coloncolon
)) {
791 while (!Token
.isNewlineOrEOF()) {
792 MachineMemOperand
*MemOp
= nullptr;
793 if (parseMachineMemoryOperand(MemOp
))
795 MemOperands
.push_back(MemOp
);
796 if (Token
.isNewlineOrEOF())
798 if (Token
.isNot(MIToken::comma
))
799 return error("expected ',' before the next machine memory operand");
804 const auto &MCID
= MF
.getSubtarget().getInstrInfo()->get(OpCode
);
805 if (!MCID
.isVariadic()) {
806 // FIXME: Move the implicit operand verification to the machine verifier.
807 if (verifyImplicitOperands(Operands
, MCID
))
811 // TODO: Check for extraneous machine operands.
812 MI
= MF
.CreateMachineInstr(MCID
, DebugLocation
, /*NoImplicit=*/true);
814 for (const auto &Operand
: Operands
)
815 MI
->addOperand(MF
, Operand
.Operand
);
816 if (assignRegisterTies(*MI
, Operands
))
819 MI
->setPreInstrSymbol(MF
, PreInstrSymbol
);
821 MI
->setPostInstrSymbol(MF
, PostInstrSymbol
);
822 if (!MemOperands
.empty())
823 MI
->setMemRefs(MF
, MemOperands
);
827 bool MIParser::parseStandaloneMBB(MachineBasicBlock
*&MBB
) {
829 if (Token
.isNot(MIToken::MachineBasicBlock
))
830 return error("expected a machine basic block reference");
831 if (parseMBBReference(MBB
))
834 if (Token
.isNot(MIToken::Eof
))
836 "expected end of string after the machine basic block reference");
840 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg
) {
842 if (Token
.isNot(MIToken::NamedRegister
))
843 return error("expected a named register");
844 if (parseNamedRegister(Reg
))
847 if (Token
.isNot(MIToken::Eof
))
848 return error("expected end of string after the register reference");
852 bool MIParser::parseStandaloneVirtualRegister(VRegInfo
*&Info
) {
854 if (Token
.isNot(MIToken::VirtualRegister
))
855 return error("expected a virtual register");
856 if (parseVirtualRegister(Info
))
859 if (Token
.isNot(MIToken::Eof
))
860 return error("expected end of string after the register reference");
864 bool MIParser::parseStandaloneRegister(unsigned &Reg
) {
866 if (Token
.isNot(MIToken::NamedRegister
) &&
867 Token
.isNot(MIToken::VirtualRegister
))
868 return error("expected either a named or virtual register");
871 if (parseRegister(Reg
, Info
))
875 if (Token
.isNot(MIToken::Eof
))
876 return error("expected end of string after the register reference");
880 bool MIParser::parseStandaloneStackObject(int &FI
) {
882 if (Token
.isNot(MIToken::StackObject
))
883 return error("expected a stack object");
884 if (parseStackFrameIndex(FI
))
886 if (Token
.isNot(MIToken::Eof
))
887 return error("expected end of string after the stack object reference");
891 bool MIParser::parseStandaloneMDNode(MDNode
*&Node
) {
893 if (Token
.is(MIToken::exclaim
)) {
894 if (parseMDNode(Node
))
896 } else if (Token
.is(MIToken::md_diexpr
)) {
897 if (parseDIExpression(Node
))
900 return error("expected a metadata node");
901 if (Token
.isNot(MIToken::Eof
))
902 return error("expected end of string after the metadata node");
906 static const char *printImplicitRegisterFlag(const MachineOperand
&MO
) {
907 assert(MO
.isImplicit());
908 return MO
.isDef() ? "implicit-def" : "implicit";
911 static std::string
getRegisterName(const TargetRegisterInfo
*TRI
,
913 assert(TargetRegisterInfo::isPhysicalRegister(Reg
) && "expected phys reg");
914 return StringRef(TRI
->getName(Reg
)).lower();
917 /// Return true if the parsed machine operands contain a given machine operand.
918 static bool isImplicitOperandIn(const MachineOperand
&ImplicitOperand
,
919 ArrayRef
<ParsedMachineOperand
> Operands
) {
920 for (const auto &I
: Operands
) {
921 if (ImplicitOperand
.isIdenticalTo(I
.Operand
))
927 bool MIParser::verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
928 const MCInstrDesc
&MCID
) {
930 // We can't verify call instructions as they can contain arbitrary implicit
931 // register and register mask operands.
934 // Gather all the expected implicit operands.
935 SmallVector
<MachineOperand
, 4> ImplicitOperands
;
936 if (MCID
.ImplicitDefs
)
937 for (const MCPhysReg
*ImpDefs
= MCID
.getImplicitDefs(); *ImpDefs
; ++ImpDefs
)
938 ImplicitOperands
.push_back(
939 MachineOperand::CreateReg(*ImpDefs
, true, true));
940 if (MCID
.ImplicitUses
)
941 for (const MCPhysReg
*ImpUses
= MCID
.getImplicitUses(); *ImpUses
; ++ImpUses
)
942 ImplicitOperands
.push_back(
943 MachineOperand::CreateReg(*ImpUses
, false, true));
945 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
946 assert(TRI
&& "Expected target register info");
947 for (const auto &I
: ImplicitOperands
) {
948 if (isImplicitOperandIn(I
, Operands
))
950 return error(Operands
.empty() ? Token
.location() : Operands
.back().End
,
951 Twine("missing implicit register operand '") +
952 printImplicitRegisterFlag(I
) + " $" +
953 getRegisterName(TRI
, I
.getReg()) + "'");
958 bool MIParser::parseInstruction(unsigned &OpCode
, unsigned &Flags
) {
959 // Allow frame and fast math flags for OPCODE
960 while (Token
.is(MIToken::kw_frame_setup
) ||
961 Token
.is(MIToken::kw_frame_destroy
) ||
962 Token
.is(MIToken::kw_nnan
) ||
963 Token
.is(MIToken::kw_ninf
) ||
964 Token
.is(MIToken::kw_nsz
) ||
965 Token
.is(MIToken::kw_arcp
) ||
966 Token
.is(MIToken::kw_contract
) ||
967 Token
.is(MIToken::kw_afn
) ||
968 Token
.is(MIToken::kw_reassoc
) ||
969 Token
.is(MIToken::kw_nuw
) ||
970 Token
.is(MIToken::kw_nsw
) ||
971 Token
.is(MIToken::kw_exact
)) {
972 // Mine frame and fast math flags
973 if (Token
.is(MIToken::kw_frame_setup
))
974 Flags
|= MachineInstr::FrameSetup
;
975 if (Token
.is(MIToken::kw_frame_destroy
))
976 Flags
|= MachineInstr::FrameDestroy
;
977 if (Token
.is(MIToken::kw_nnan
))
978 Flags
|= MachineInstr::FmNoNans
;
979 if (Token
.is(MIToken::kw_ninf
))
980 Flags
|= MachineInstr::FmNoInfs
;
981 if (Token
.is(MIToken::kw_nsz
))
982 Flags
|= MachineInstr::FmNsz
;
983 if (Token
.is(MIToken::kw_arcp
))
984 Flags
|= MachineInstr::FmArcp
;
985 if (Token
.is(MIToken::kw_contract
))
986 Flags
|= MachineInstr::FmContract
;
987 if (Token
.is(MIToken::kw_afn
))
988 Flags
|= MachineInstr::FmAfn
;
989 if (Token
.is(MIToken::kw_reassoc
))
990 Flags
|= MachineInstr::FmReassoc
;
991 if (Token
.is(MIToken::kw_nuw
))
992 Flags
|= MachineInstr::NoUWrap
;
993 if (Token
.is(MIToken::kw_nsw
))
994 Flags
|= MachineInstr::NoSWrap
;
995 if (Token
.is(MIToken::kw_exact
))
996 Flags
|= MachineInstr::IsExact
;
1000 if (Token
.isNot(MIToken::Identifier
))
1001 return error("expected a machine instruction");
1002 StringRef InstrName
= Token
.stringValue();
1003 if (parseInstrName(InstrName
, OpCode
))
1004 return error(Twine("unknown machine instruction name '") + InstrName
+ "'");
1009 bool MIParser::parseNamedRegister(unsigned &Reg
) {
1010 assert(Token
.is(MIToken::NamedRegister
) && "Needs NamedRegister token");
1011 StringRef Name
= Token
.stringValue();
1012 if (getRegisterByName(Name
, Reg
))
1013 return error(Twine("unknown register name '") + Name
+ "'");
1017 bool MIParser::parseNamedVirtualRegister(VRegInfo
*&Info
) {
1018 assert(Token
.is(MIToken::NamedVirtualRegister
) && "Expected NamedVReg token");
1019 StringRef Name
= Token
.stringValue();
1020 // TODO: Check that the VReg name is not the same as a physical register name.
1021 // If it is, then print a warning (when warnings are implemented).
1022 Info
= &PFS
.getVRegInfoNamed(Name
);
1026 bool MIParser::parseVirtualRegister(VRegInfo
*&Info
) {
1027 if (Token
.is(MIToken::NamedVirtualRegister
))
1028 return parseNamedVirtualRegister(Info
);
1029 assert(Token
.is(MIToken::VirtualRegister
) && "Needs VirtualRegister token");
1031 if (getUnsigned(ID
))
1033 Info
= &PFS
.getVRegInfo(ID
);
1037 bool MIParser::parseRegister(unsigned &Reg
, VRegInfo
*&Info
) {
1038 switch (Token
.kind()) {
1039 case MIToken::underscore
:
1042 case MIToken::NamedRegister
:
1043 return parseNamedRegister(Reg
);
1044 case MIToken::NamedVirtualRegister
:
1045 case MIToken::VirtualRegister
:
1046 if (parseVirtualRegister(Info
))
1050 // TODO: Parse other register kinds.
1052 llvm_unreachable("The current token should be a register");
1056 bool MIParser::parseRegisterClassOrBank(VRegInfo
&RegInfo
) {
1057 if (Token
.isNot(MIToken::Identifier
) && Token
.isNot(MIToken::underscore
))
1058 return error("expected '_', register class, or register bank name");
1059 StringRef::iterator Loc
= Token
.location();
1060 StringRef Name
= Token
.stringValue();
1062 // Was it a register class?
1063 auto RCNameI
= PFS
.Names2RegClasses
.find(Name
);
1064 if (RCNameI
!= PFS
.Names2RegClasses
.end()) {
1066 const TargetRegisterClass
&RC
= *RCNameI
->getValue();
1068 switch (RegInfo
.Kind
) {
1069 case VRegInfo::UNKNOWN
:
1070 case VRegInfo::NORMAL
:
1071 RegInfo
.Kind
= VRegInfo::NORMAL
;
1072 if (RegInfo
.Explicit
&& RegInfo
.D
.RC
!= &RC
) {
1073 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
1074 return error(Loc
, Twine("conflicting register classes, previously: ") +
1075 Twine(TRI
.getRegClassName(RegInfo
.D
.RC
)));
1078 RegInfo
.Explicit
= true;
1081 case VRegInfo::GENERIC
:
1082 case VRegInfo::REGBANK
:
1083 return error(Loc
, "register class specification on generic register");
1085 llvm_unreachable("Unexpected register kind");
1088 // Should be a register bank or a generic register.
1089 const RegisterBank
*RegBank
= nullptr;
1091 auto RBNameI
= PFS
.Names2RegBanks
.find(Name
);
1092 if (RBNameI
== PFS
.Names2RegBanks
.end())
1093 return error(Loc
, "expected '_', register class, or register bank name");
1094 RegBank
= RBNameI
->getValue();
1099 switch (RegInfo
.Kind
) {
1100 case VRegInfo::UNKNOWN
:
1101 case VRegInfo::GENERIC
:
1102 case VRegInfo::REGBANK
:
1103 RegInfo
.Kind
= RegBank
? VRegInfo::REGBANK
: VRegInfo::GENERIC
;
1104 if (RegInfo
.Explicit
&& RegInfo
.D
.RegBank
!= RegBank
)
1105 return error(Loc
, "conflicting generic register banks");
1106 RegInfo
.D
.RegBank
= RegBank
;
1107 RegInfo
.Explicit
= true;
1110 case VRegInfo::NORMAL
:
1111 return error(Loc
, "register bank specification on normal register");
1113 llvm_unreachable("Unexpected register kind");
1116 bool MIParser::parseRegisterFlag(unsigned &Flags
) {
1117 const unsigned OldFlags
= Flags
;
1118 switch (Token
.kind()) {
1119 case MIToken::kw_implicit
:
1120 Flags
|= RegState::Implicit
;
1122 case MIToken::kw_implicit_define
:
1123 Flags
|= RegState::ImplicitDefine
;
1125 case MIToken::kw_def
:
1126 Flags
|= RegState::Define
;
1128 case MIToken::kw_dead
:
1129 Flags
|= RegState::Dead
;
1131 case MIToken::kw_killed
:
1132 Flags
|= RegState::Kill
;
1134 case MIToken::kw_undef
:
1135 Flags
|= RegState::Undef
;
1137 case MIToken::kw_internal
:
1138 Flags
|= RegState::InternalRead
;
1140 case MIToken::kw_early_clobber
:
1141 Flags
|= RegState::EarlyClobber
;
1143 case MIToken::kw_debug_use
:
1144 Flags
|= RegState::Debug
;
1146 case MIToken::kw_renamable
:
1147 Flags
|= RegState::Renamable
;
1150 llvm_unreachable("The current token should be a register flag");
1152 if (OldFlags
== Flags
)
1153 // We know that the same flag is specified more than once when the flags
1154 // weren't modified.
1155 return error("duplicate '" + Token
.stringValue() + "' register flag");
1160 bool MIParser::parseSubRegisterIndex(unsigned &SubReg
) {
1161 assert(Token
.is(MIToken::dot
));
1163 if (Token
.isNot(MIToken::Identifier
))
1164 return error("expected a subregister index after '.'");
1165 auto Name
= Token
.stringValue();
1166 SubReg
= getSubRegIndex(Name
);
1168 return error(Twine("use of unknown subregister index '") + Name
+ "'");
1173 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx
) {
1174 if (!consumeIfPresent(MIToken::kw_tied_def
))
1176 if (Token
.isNot(MIToken::IntegerLiteral
))
1177 return error("expected an integer literal after 'tied-def'");
1178 if (getUnsigned(TiedDefIdx
))
1181 if (expectAndConsume(MIToken::rparen
))
1186 bool MIParser::assignRegisterTies(MachineInstr
&MI
,
1187 ArrayRef
<ParsedMachineOperand
> Operands
) {
1188 SmallVector
<std::pair
<unsigned, unsigned>, 4> TiedRegisterPairs
;
1189 for (unsigned I
= 0, E
= Operands
.size(); I
!= E
; ++I
) {
1190 if (!Operands
[I
].TiedDefIdx
)
1192 // The parser ensures that this operand is a register use, so we just have
1193 // to check the tied-def operand.
1194 unsigned DefIdx
= Operands
[I
].TiedDefIdx
.getValue();
1196 return error(Operands
[I
].Begin
,
1197 Twine("use of invalid tied-def operand index '" +
1198 Twine(DefIdx
) + "'; instruction has only ") +
1199 Twine(E
) + " operands");
1200 const auto &DefOperand
= Operands
[DefIdx
].Operand
;
1201 if (!DefOperand
.isReg() || !DefOperand
.isDef())
1202 // FIXME: add note with the def operand.
1203 return error(Operands
[I
].Begin
,
1204 Twine("use of invalid tied-def operand index '") +
1205 Twine(DefIdx
) + "'; the operand #" + Twine(DefIdx
) +
1206 " isn't a defined register");
1207 // Check that the tied-def operand wasn't tied elsewhere.
1208 for (const auto &TiedPair
: TiedRegisterPairs
) {
1209 if (TiedPair
.first
== DefIdx
)
1210 return error(Operands
[I
].Begin
,
1211 Twine("the tied-def operand #") + Twine(DefIdx
) +
1212 " is already tied with another register operand");
1214 TiedRegisterPairs
.push_back(std::make_pair(DefIdx
, I
));
1216 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1217 // indices must be less than tied max.
1218 for (const auto &TiedPair
: TiedRegisterPairs
)
1219 MI
.tieOperands(TiedPair
.first
, TiedPair
.second
);
1223 bool MIParser::parseRegisterOperand(MachineOperand
&Dest
,
1224 Optional
<unsigned> &TiedDefIdx
,
1226 unsigned Flags
= IsDef
? RegState::Define
: 0;
1227 while (Token
.isRegisterFlag()) {
1228 if (parseRegisterFlag(Flags
))
1231 if (!Token
.isRegister())
1232 return error("expected a register after register flags");
1235 if (parseRegister(Reg
, RegInfo
))
1238 unsigned SubReg
= 0;
1239 if (Token
.is(MIToken::dot
)) {
1240 if (parseSubRegisterIndex(SubReg
))
1242 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1243 return error("subregister index expects a virtual register");
1245 if (Token
.is(MIToken::colon
)) {
1246 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1247 return error("register class specification expects a virtual register");
1249 if (parseRegisterClassOrBank(*RegInfo
))
1252 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1253 if ((Flags
& RegState::Define
) == 0) {
1254 if (consumeIfPresent(MIToken::lparen
)) {
1256 if (!parseRegisterTiedDefIndex(Idx
))
1259 // Try a redundant low-level type.
1261 if (parseLowLevelType(Token
.location(), Ty
))
1262 return error("expected tied-def or low-level type after '('");
1264 if (expectAndConsume(MIToken::rparen
))
1267 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1268 return error("inconsistent type for generic virtual register");
1270 MRI
.setType(Reg
, Ty
);
1273 } else if (consumeIfPresent(MIToken::lparen
)) {
1274 // Virtual registers may have a tpe with GlobalISel.
1275 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1276 return error("unexpected type on physical register");
1279 if (parseLowLevelType(Token
.location(), Ty
))
1282 if (expectAndConsume(MIToken::rparen
))
1285 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1286 return error("inconsistent type for generic virtual register");
1288 MRI
.setType(Reg
, Ty
);
1289 } else if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
1290 // Generic virtual registers must have a type.
1291 // If we end up here this means the type hasn't been specified and
1293 if (RegInfo
->Kind
== VRegInfo::GENERIC
||
1294 RegInfo
->Kind
== VRegInfo::REGBANK
)
1295 return error("generic virtual registers must have a type");
1297 Dest
= MachineOperand::CreateReg(
1298 Reg
, Flags
& RegState::Define
, Flags
& RegState::Implicit
,
1299 Flags
& RegState::Kill
, Flags
& RegState::Dead
, Flags
& RegState::Undef
,
1300 Flags
& RegState::EarlyClobber
, SubReg
, Flags
& RegState::Debug
,
1301 Flags
& RegState::InternalRead
, Flags
& RegState::Renamable
);
1306 bool MIParser::parseImmediateOperand(MachineOperand
&Dest
) {
1307 assert(Token
.is(MIToken::IntegerLiteral
));
1308 const APSInt
&Int
= Token
.integerValue();
1309 if (Int
.getMinSignedBits() > 64)
1310 return error("integer literal is too large to be an immediate operand");
1311 Dest
= MachineOperand::CreateImm(Int
.getExtValue());
1316 bool MIParser::parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
1317 const Constant
*&C
) {
1318 auto Source
= StringValue
.str(); // The source has to be null terminated.
1320 C
= parseConstantValue(Source
, Err
, *MF
.getFunction().getParent(),
1323 return error(Loc
+ Err
.getColumnNo(), Err
.getMessage());
1327 bool MIParser::parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
) {
1328 if (parseIRConstant(Loc
, StringRef(Loc
, Token
.range().end() - Loc
), C
))
1334 bool MIParser::parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
) {
1335 if (Token
.range().front() == 's' || Token
.range().front() == 'p') {
1336 StringRef SizeStr
= Token
.range().drop_front();
1337 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1338 return error("expected integers after 's'/'p' type character");
1341 if (Token
.range().front() == 's') {
1342 Ty
= LLT::scalar(APSInt(Token
.range().drop_front()).getZExtValue());
1345 } else if (Token
.range().front() == 'p') {
1346 const DataLayout
&DL
= MF
.getDataLayout();
1347 unsigned AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1348 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1353 // Now we're looking for a vector.
1354 if (Token
.isNot(MIToken::less
))
1356 "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1359 if (Token
.isNot(MIToken::IntegerLiteral
))
1360 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1361 uint64_t NumElements
= Token
.integerValue().getZExtValue();
1364 if (Token
.isNot(MIToken::Identifier
) || Token
.stringValue() != "x")
1365 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1368 if (Token
.range().front() != 's' && Token
.range().front() != 'p')
1369 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1370 StringRef SizeStr
= Token
.range().drop_front();
1371 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1372 return error("expected integers after 's'/'p' type character");
1374 if (Token
.range().front() == 's')
1375 Ty
= LLT::scalar(APSInt(Token
.range().drop_front()).getZExtValue());
1376 else if (Token
.range().front() == 'p') {
1377 const DataLayout
&DL
= MF
.getDataLayout();
1378 unsigned AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1379 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1381 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1384 if (Token
.isNot(MIToken::greater
))
1385 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1388 Ty
= LLT::vector(NumElements
, Ty
);
1392 bool MIParser::parseTypedImmediateOperand(MachineOperand
&Dest
) {
1393 assert(Token
.is(MIToken::Identifier
));
1394 StringRef TypeStr
= Token
.range();
1395 if (TypeStr
.front() != 'i' && TypeStr
.front() != 's' &&
1396 TypeStr
.front() != 'p')
1398 "a typed immediate operand should start with one of 'i', 's', or 'p'");
1399 StringRef SizeStr
= Token
.range().drop_front();
1400 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1401 return error("expected integers after 'i'/'s'/'p' type character");
1403 auto Loc
= Token
.location();
1405 if (Token
.isNot(MIToken::IntegerLiteral
)) {
1406 if (Token
.isNot(MIToken::Identifier
) ||
1407 !(Token
.range() == "true" || Token
.range() == "false"))
1408 return error("expected an integer literal");
1410 const Constant
*C
= nullptr;
1411 if (parseIRConstant(Loc
, C
))
1413 Dest
= MachineOperand::CreateCImm(cast
<ConstantInt
>(C
));
1417 bool MIParser::parseFPImmediateOperand(MachineOperand
&Dest
) {
1418 auto Loc
= Token
.location();
1420 if (Token
.isNot(MIToken::FloatingPointLiteral
) &&
1421 Token
.isNot(MIToken::HexLiteral
))
1422 return error("expected a floating point literal");
1423 const Constant
*C
= nullptr;
1424 if (parseIRConstant(Loc
, C
))
1426 Dest
= MachineOperand::CreateFPImm(cast
<ConstantFP
>(C
));
1430 bool MIParser::getUnsigned(unsigned &Result
) {
1431 if (Token
.hasIntegerValue()) {
1432 const uint64_t Limit
= uint64_t(std::numeric_limits
<unsigned>::max()) + 1;
1433 uint64_t Val64
= Token
.integerValue().getLimitedValue(Limit
);
1435 return error("expected 32-bit integer (too large)");
1439 if (Token
.is(MIToken::HexLiteral
)) {
1443 if (A
.getBitWidth() > 32)
1444 return error("expected 32-bit integer (too large)");
1445 Result
= A
.getZExtValue();
1451 bool MIParser::parseMBBReference(MachineBasicBlock
*&MBB
) {
1452 assert(Token
.is(MIToken::MachineBasicBlock
) ||
1453 Token
.is(MIToken::MachineBasicBlockLabel
));
1455 if (getUnsigned(Number
))
1457 auto MBBInfo
= PFS
.MBBSlots
.find(Number
);
1458 if (MBBInfo
== PFS
.MBBSlots
.end())
1459 return error(Twine("use of undefined machine basic block #") +
1461 MBB
= MBBInfo
->second
;
1462 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1463 // we drop the <irname> from the bb.<id>.<irname> format.
1464 if (!Token
.stringValue().empty() && Token
.stringValue() != MBB
->getName())
1465 return error(Twine("the name of machine basic block #") + Twine(Number
) +
1466 " isn't '" + Token
.stringValue() + "'");
1470 bool MIParser::parseMBBOperand(MachineOperand
&Dest
) {
1471 MachineBasicBlock
*MBB
;
1472 if (parseMBBReference(MBB
))
1474 Dest
= MachineOperand::CreateMBB(MBB
);
1479 bool MIParser::parseStackFrameIndex(int &FI
) {
1480 assert(Token
.is(MIToken::StackObject
));
1482 if (getUnsigned(ID
))
1484 auto ObjectInfo
= PFS
.StackObjectSlots
.find(ID
);
1485 if (ObjectInfo
== PFS
.StackObjectSlots
.end())
1486 return error(Twine("use of undefined stack object '%stack.") + Twine(ID
) +
1489 if (const auto *Alloca
=
1490 MF
.getFrameInfo().getObjectAllocation(ObjectInfo
->second
))
1491 Name
= Alloca
->getName();
1492 if (!Token
.stringValue().empty() && Token
.stringValue() != Name
)
1493 return error(Twine("the name of the stack object '%stack.") + Twine(ID
) +
1494 "' isn't '" + Token
.stringValue() + "'");
1496 FI
= ObjectInfo
->second
;
1500 bool MIParser::parseStackObjectOperand(MachineOperand
&Dest
) {
1502 if (parseStackFrameIndex(FI
))
1504 Dest
= MachineOperand::CreateFI(FI
);
1508 bool MIParser::parseFixedStackFrameIndex(int &FI
) {
1509 assert(Token
.is(MIToken::FixedStackObject
));
1511 if (getUnsigned(ID
))
1513 auto ObjectInfo
= PFS
.FixedStackObjectSlots
.find(ID
);
1514 if (ObjectInfo
== PFS
.FixedStackObjectSlots
.end())
1515 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1518 FI
= ObjectInfo
->second
;
1522 bool MIParser::parseFixedStackObjectOperand(MachineOperand
&Dest
) {
1524 if (parseFixedStackFrameIndex(FI
))
1526 Dest
= MachineOperand::CreateFI(FI
);
1530 bool MIParser::parseGlobalValue(GlobalValue
*&GV
) {
1531 switch (Token
.kind()) {
1532 case MIToken::NamedGlobalValue
: {
1533 const Module
*M
= MF
.getFunction().getParent();
1534 GV
= M
->getNamedValue(Token
.stringValue());
1536 return error(Twine("use of undefined global value '") + Token
.range() +
1540 case MIToken::GlobalValue
: {
1542 if (getUnsigned(GVIdx
))
1544 if (GVIdx
>= PFS
.IRSlots
.GlobalValues
.size())
1545 return error(Twine("use of undefined global value '@") + Twine(GVIdx
) +
1547 GV
= PFS
.IRSlots
.GlobalValues
[GVIdx
];
1551 llvm_unreachable("The current token should be a global value");
1556 bool MIParser::parseGlobalAddressOperand(MachineOperand
&Dest
) {
1557 GlobalValue
*GV
= nullptr;
1558 if (parseGlobalValue(GV
))
1561 Dest
= MachineOperand::CreateGA(GV
, /*Offset=*/0);
1562 if (parseOperandsOffset(Dest
))
1567 bool MIParser::parseConstantPoolIndexOperand(MachineOperand
&Dest
) {
1568 assert(Token
.is(MIToken::ConstantPoolItem
));
1570 if (getUnsigned(ID
))
1572 auto ConstantInfo
= PFS
.ConstantPoolSlots
.find(ID
);
1573 if (ConstantInfo
== PFS
.ConstantPoolSlots
.end())
1574 return error("use of undefined constant '%const." + Twine(ID
) + "'");
1576 Dest
= MachineOperand::CreateCPI(ID
, /*Offset=*/0);
1577 if (parseOperandsOffset(Dest
))
1582 bool MIParser::parseJumpTableIndexOperand(MachineOperand
&Dest
) {
1583 assert(Token
.is(MIToken::JumpTableIndex
));
1585 if (getUnsigned(ID
))
1587 auto JumpTableEntryInfo
= PFS
.JumpTableSlots
.find(ID
);
1588 if (JumpTableEntryInfo
== PFS
.JumpTableSlots
.end())
1589 return error("use of undefined jump table '%jump-table." + Twine(ID
) + "'");
1591 Dest
= MachineOperand::CreateJTI(JumpTableEntryInfo
->second
);
1595 bool MIParser::parseExternalSymbolOperand(MachineOperand
&Dest
) {
1596 assert(Token
.is(MIToken::ExternalSymbol
));
1597 const char *Symbol
= MF
.createExternalSymbolName(Token
.stringValue());
1599 Dest
= MachineOperand::CreateES(Symbol
);
1600 if (parseOperandsOffset(Dest
))
1605 bool MIParser::parseMCSymbolOperand(MachineOperand
&Dest
) {
1606 assert(Token
.is(MIToken::MCSymbol
));
1607 MCSymbol
*Symbol
= getOrCreateMCSymbol(Token
.stringValue());
1609 Dest
= MachineOperand::CreateMCSymbol(Symbol
);
1610 if (parseOperandsOffset(Dest
))
1615 bool MIParser::parseSubRegisterIndexOperand(MachineOperand
&Dest
) {
1616 assert(Token
.is(MIToken::SubRegisterIndex
));
1617 StringRef Name
= Token
.stringValue();
1618 unsigned SubRegIndex
= getSubRegIndex(Token
.stringValue());
1619 if (SubRegIndex
== 0)
1620 return error(Twine("unknown subregister index '") + Name
+ "'");
1622 Dest
= MachineOperand::CreateImm(SubRegIndex
);
1626 bool MIParser::parseMDNode(MDNode
*&Node
) {
1627 assert(Token
.is(MIToken::exclaim
));
1629 auto Loc
= Token
.location();
1631 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
1632 return error("expected metadata id after '!'");
1634 if (getUnsigned(ID
))
1636 auto NodeInfo
= PFS
.IRSlots
.MetadataNodes
.find(ID
);
1637 if (NodeInfo
== PFS
.IRSlots
.MetadataNodes
.end())
1638 return error(Loc
, "use of undefined metadata '!" + Twine(ID
) + "'");
1640 Node
= NodeInfo
->second
.get();
1644 bool MIParser::parseDIExpression(MDNode
*&Expr
) {
1645 assert(Token
.is(MIToken::md_diexpr
));
1648 // FIXME: Share this parsing with the IL parser.
1649 SmallVector
<uint64_t, 8> Elements
;
1651 if (expectAndConsume(MIToken::lparen
))
1654 if (Token
.isNot(MIToken::rparen
)) {
1656 if (Token
.is(MIToken::Identifier
)) {
1657 if (unsigned Op
= dwarf::getOperationEncoding(Token
.stringValue())) {
1659 Elements
.push_back(Op
);
1662 return error(Twine("invalid DWARF op '") + Token
.stringValue() + "'");
1665 if (Token
.isNot(MIToken::IntegerLiteral
) ||
1666 Token
.integerValue().isSigned())
1667 return error("expected unsigned integer");
1669 auto &U
= Token
.integerValue();
1670 if (U
.ugt(UINT64_MAX
))
1671 return error("element too large, limit is " + Twine(UINT64_MAX
));
1672 Elements
.push_back(U
.getZExtValue());
1675 } while (consumeIfPresent(MIToken::comma
));
1678 if (expectAndConsume(MIToken::rparen
))
1681 Expr
= DIExpression::get(MF
.getFunction().getContext(), Elements
);
1685 bool MIParser::parseMetadataOperand(MachineOperand
&Dest
) {
1686 MDNode
*Node
= nullptr;
1687 if (Token
.is(MIToken::exclaim
)) {
1688 if (parseMDNode(Node
))
1690 } else if (Token
.is(MIToken::md_diexpr
)) {
1691 if (parseDIExpression(Node
))
1694 Dest
= MachineOperand::CreateMetadata(Node
);
1698 bool MIParser::parseCFIOffset(int &Offset
) {
1699 if (Token
.isNot(MIToken::IntegerLiteral
))
1700 return error("expected a cfi offset");
1701 if (Token
.integerValue().getMinSignedBits() > 32)
1702 return error("expected a 32 bit integer (the cfi offset is too large)");
1703 Offset
= (int)Token
.integerValue().getExtValue();
1708 bool MIParser::parseCFIRegister(unsigned &Reg
) {
1709 if (Token
.isNot(MIToken::NamedRegister
))
1710 return error("expected a cfi register");
1712 if (parseNamedRegister(LLVMReg
))
1714 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
1715 assert(TRI
&& "Expected target register info");
1716 int DwarfReg
= TRI
->getDwarfRegNum(LLVMReg
, true);
1718 return error("invalid DWARF register");
1719 Reg
= (unsigned)DwarfReg
;
1724 bool MIParser::parseCFIEscapeValues(std::string
&Values
) {
1726 if (Token
.isNot(MIToken::HexLiteral
))
1727 return error("expected a hexadecimal literal");
1729 if (getUnsigned(Value
))
1731 if (Value
> UINT8_MAX
)
1732 return error("expected a 8-bit integer (too large)");
1733 Values
.push_back(static_cast<uint8_t>(Value
));
1735 } while (consumeIfPresent(MIToken::comma
));
1739 bool MIParser::parseCFIOperand(MachineOperand
&Dest
) {
1740 auto Kind
= Token
.kind();
1746 case MIToken::kw_cfi_same_value
:
1747 if (parseCFIRegister(Reg
))
1749 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg
));
1751 case MIToken::kw_cfi_offset
:
1752 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
1753 parseCFIOffset(Offset
))
1756 MF
.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg
, Offset
));
1758 case MIToken::kw_cfi_rel_offset
:
1759 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
1760 parseCFIOffset(Offset
))
1762 CFIIndex
= MF
.addFrameInst(
1763 MCCFIInstruction::createRelOffset(nullptr, Reg
, Offset
));
1765 case MIToken::kw_cfi_def_cfa_register
:
1766 if (parseCFIRegister(Reg
))
1769 MF
.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg
));
1771 case MIToken::kw_cfi_def_cfa_offset
:
1772 if (parseCFIOffset(Offset
))
1774 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1775 CFIIndex
= MF
.addFrameInst(
1776 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset
));
1778 case MIToken::kw_cfi_adjust_cfa_offset
:
1779 if (parseCFIOffset(Offset
))
1781 CFIIndex
= MF
.addFrameInst(
1782 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset
));
1784 case MIToken::kw_cfi_def_cfa
:
1785 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
1786 parseCFIOffset(Offset
))
1788 // NB: MCCFIInstruction::createDefCfa negates the offset.
1790 MF
.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg
, -Offset
));
1792 case MIToken::kw_cfi_remember_state
:
1793 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
1795 case MIToken::kw_cfi_restore
:
1796 if (parseCFIRegister(Reg
))
1798 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg
));
1800 case MIToken::kw_cfi_restore_state
:
1801 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
1803 case MIToken::kw_cfi_undefined
:
1804 if (parseCFIRegister(Reg
))
1806 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg
));
1808 case MIToken::kw_cfi_register
: {
1810 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
1811 parseCFIRegister(Reg2
))
1815 MF
.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg
, Reg2
));
1818 case MIToken::kw_cfi_window_save
:
1819 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
1821 case MIToken::kw_cfi_escape
: {
1823 if (parseCFIEscapeValues(Values
))
1825 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values
));
1829 // TODO: Parse the other CFI operands.
1830 llvm_unreachable("The current token should be a cfi operand");
1832 Dest
= MachineOperand::CreateCFIIndex(CFIIndex
);
1836 bool MIParser::parseIRBlock(BasicBlock
*&BB
, const Function
&F
) {
1837 switch (Token
.kind()) {
1838 case MIToken::NamedIRBlock
: {
1839 BB
= dyn_cast_or_null
<BasicBlock
>(
1840 F
.getValueSymbolTable()->lookup(Token
.stringValue()));
1842 return error(Twine("use of undefined IR block '") + Token
.range() + "'");
1845 case MIToken::IRBlock
: {
1846 unsigned SlotNumber
= 0;
1847 if (getUnsigned(SlotNumber
))
1849 BB
= const_cast<BasicBlock
*>(getIRBlock(SlotNumber
, F
));
1851 return error(Twine("use of undefined IR block '%ir-block.") +
1852 Twine(SlotNumber
) + "'");
1856 llvm_unreachable("The current token should be an IR block reference");
1861 bool MIParser::parseBlockAddressOperand(MachineOperand
&Dest
) {
1862 assert(Token
.is(MIToken::kw_blockaddress
));
1864 if (expectAndConsume(MIToken::lparen
))
1866 if (Token
.isNot(MIToken::GlobalValue
) &&
1867 Token
.isNot(MIToken::NamedGlobalValue
))
1868 return error("expected a global value");
1869 GlobalValue
*GV
= nullptr;
1870 if (parseGlobalValue(GV
))
1872 auto *F
= dyn_cast
<Function
>(GV
);
1874 return error("expected an IR function reference");
1876 if (expectAndConsume(MIToken::comma
))
1878 BasicBlock
*BB
= nullptr;
1879 if (Token
.isNot(MIToken::IRBlock
) && Token
.isNot(MIToken::NamedIRBlock
))
1880 return error("expected an IR block reference");
1881 if (parseIRBlock(BB
, *F
))
1884 if (expectAndConsume(MIToken::rparen
))
1886 Dest
= MachineOperand::CreateBA(BlockAddress::get(F
, BB
), /*Offset=*/0);
1887 if (parseOperandsOffset(Dest
))
1892 bool MIParser::parseIntrinsicOperand(MachineOperand
&Dest
) {
1893 assert(Token
.is(MIToken::kw_intrinsic
));
1895 if (expectAndConsume(MIToken::lparen
))
1896 return error("expected syntax intrinsic(@llvm.whatever)");
1898 if (Token
.isNot(MIToken::NamedGlobalValue
))
1899 return error("expected syntax intrinsic(@llvm.whatever)");
1901 std::string Name
= Token
.stringValue();
1904 if (expectAndConsume(MIToken::rparen
))
1905 return error("expected ')' to terminate intrinsic name");
1907 // Find out what intrinsic we're dealing with, first try the global namespace
1908 // and then the target's private intrinsics if that fails.
1909 const TargetIntrinsicInfo
*TII
= MF
.getTarget().getIntrinsicInfo();
1910 Intrinsic::ID ID
= Function::lookupIntrinsicID(Name
);
1911 if (ID
== Intrinsic::not_intrinsic
&& TII
)
1912 ID
= static_cast<Intrinsic::ID
>(TII
->lookupName(Name
));
1914 if (ID
== Intrinsic::not_intrinsic
)
1915 return error("unknown intrinsic name");
1916 Dest
= MachineOperand::CreateIntrinsicID(ID
);
1921 bool MIParser::parsePredicateOperand(MachineOperand
&Dest
) {
1922 assert(Token
.is(MIToken::kw_intpred
) || Token
.is(MIToken::kw_floatpred
));
1923 bool IsFloat
= Token
.is(MIToken::kw_floatpred
);
1926 if (expectAndConsume(MIToken::lparen
))
1927 return error("expected syntax intpred(whatever) or floatpred(whatever");
1929 if (Token
.isNot(MIToken::Identifier
))
1930 return error("whatever");
1932 CmpInst::Predicate Pred
;
1934 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
1935 .Case("false", CmpInst::FCMP_FALSE
)
1936 .Case("oeq", CmpInst::FCMP_OEQ
)
1937 .Case("ogt", CmpInst::FCMP_OGT
)
1938 .Case("oge", CmpInst::FCMP_OGE
)
1939 .Case("olt", CmpInst::FCMP_OLT
)
1940 .Case("ole", CmpInst::FCMP_OLE
)
1941 .Case("one", CmpInst::FCMP_ONE
)
1942 .Case("ord", CmpInst::FCMP_ORD
)
1943 .Case("uno", CmpInst::FCMP_UNO
)
1944 .Case("ueq", CmpInst::FCMP_UEQ
)
1945 .Case("ugt", CmpInst::FCMP_UGT
)
1946 .Case("uge", CmpInst::FCMP_UGE
)
1947 .Case("ult", CmpInst::FCMP_ULT
)
1948 .Case("ule", CmpInst::FCMP_ULE
)
1949 .Case("une", CmpInst::FCMP_UNE
)
1950 .Case("true", CmpInst::FCMP_TRUE
)
1951 .Default(CmpInst::BAD_FCMP_PREDICATE
);
1952 if (!CmpInst::isFPPredicate(Pred
))
1953 return error("invalid floating-point predicate");
1955 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
1956 .Case("eq", CmpInst::ICMP_EQ
)
1957 .Case("ne", CmpInst::ICMP_NE
)
1958 .Case("sgt", CmpInst::ICMP_SGT
)
1959 .Case("sge", CmpInst::ICMP_SGE
)
1960 .Case("slt", CmpInst::ICMP_SLT
)
1961 .Case("sle", CmpInst::ICMP_SLE
)
1962 .Case("ugt", CmpInst::ICMP_UGT
)
1963 .Case("uge", CmpInst::ICMP_UGE
)
1964 .Case("ult", CmpInst::ICMP_ULT
)
1965 .Case("ule", CmpInst::ICMP_ULE
)
1966 .Default(CmpInst::BAD_ICMP_PREDICATE
);
1967 if (!CmpInst::isIntPredicate(Pred
))
1968 return error("invalid integer predicate");
1972 Dest
= MachineOperand::CreatePredicate(Pred
);
1973 if (expectAndConsume(MIToken::rparen
))
1974 return error("predicate should be terminated by ')'.");
1979 bool MIParser::parseTargetIndexOperand(MachineOperand
&Dest
) {
1980 assert(Token
.is(MIToken::kw_target_index
));
1982 if (expectAndConsume(MIToken::lparen
))
1984 if (Token
.isNot(MIToken::Identifier
))
1985 return error("expected the name of the target index");
1987 if (getTargetIndex(Token
.stringValue(), Index
))
1988 return error("use of undefined target index '" + Token
.stringValue() + "'");
1990 if (expectAndConsume(MIToken::rparen
))
1992 Dest
= MachineOperand::CreateTargetIndex(unsigned(Index
), /*Offset=*/0);
1993 if (parseOperandsOffset(Dest
))
1998 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand
&Dest
) {
1999 assert(Token
.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2001 if (expectAndConsume(MIToken::lparen
))
2004 uint32_t *Mask
= MF
.allocateRegMask();
2006 if (Token
.isNot(MIToken::NamedRegister
))
2007 return error("expected a named register");
2009 if (parseNamedRegister(Reg
))
2012 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2013 // TODO: Report an error if the same register is used more than once.
2014 if (Token
.isNot(MIToken::comma
))
2019 if (expectAndConsume(MIToken::rparen
))
2021 Dest
= MachineOperand::CreateRegMask(Mask
);
2025 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
) {
2026 assert(Token
.is(MIToken::kw_liveout
));
2027 uint32_t *Mask
= MF
.allocateRegMask();
2029 if (expectAndConsume(MIToken::lparen
))
2032 if (Token
.isNot(MIToken::NamedRegister
))
2033 return error("expected a named register");
2035 if (parseNamedRegister(Reg
))
2038 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2039 // TODO: Report an error if the same register is used more than once.
2040 if (Token
.isNot(MIToken::comma
))
2044 if (expectAndConsume(MIToken::rparen
))
2046 Dest
= MachineOperand::CreateRegLiveOut(Mask
);
2050 bool MIParser::parseMachineOperand(MachineOperand
&Dest
,
2051 Optional
<unsigned> &TiedDefIdx
) {
2052 switch (Token
.kind()) {
2053 case MIToken::kw_implicit
:
2054 case MIToken::kw_implicit_define
:
2055 case MIToken::kw_def
:
2056 case MIToken::kw_dead
:
2057 case MIToken::kw_killed
:
2058 case MIToken::kw_undef
:
2059 case MIToken::kw_internal
:
2060 case MIToken::kw_early_clobber
:
2061 case MIToken::kw_debug_use
:
2062 case MIToken::kw_renamable
:
2063 case MIToken::underscore
:
2064 case MIToken::NamedRegister
:
2065 case MIToken::VirtualRegister
:
2066 case MIToken::NamedVirtualRegister
:
2067 return parseRegisterOperand(Dest
, TiedDefIdx
);
2068 case MIToken::IntegerLiteral
:
2069 return parseImmediateOperand(Dest
);
2070 case MIToken::kw_half
:
2071 case MIToken::kw_float
:
2072 case MIToken::kw_double
:
2073 case MIToken::kw_x86_fp80
:
2074 case MIToken::kw_fp128
:
2075 case MIToken::kw_ppc_fp128
:
2076 return parseFPImmediateOperand(Dest
);
2077 case MIToken::MachineBasicBlock
:
2078 return parseMBBOperand(Dest
);
2079 case MIToken::StackObject
:
2080 return parseStackObjectOperand(Dest
);
2081 case MIToken::FixedStackObject
:
2082 return parseFixedStackObjectOperand(Dest
);
2083 case MIToken::GlobalValue
:
2084 case MIToken::NamedGlobalValue
:
2085 return parseGlobalAddressOperand(Dest
);
2086 case MIToken::ConstantPoolItem
:
2087 return parseConstantPoolIndexOperand(Dest
);
2088 case MIToken::JumpTableIndex
:
2089 return parseJumpTableIndexOperand(Dest
);
2090 case MIToken::ExternalSymbol
:
2091 return parseExternalSymbolOperand(Dest
);
2092 case MIToken::MCSymbol
:
2093 return parseMCSymbolOperand(Dest
);
2094 case MIToken::SubRegisterIndex
:
2095 return parseSubRegisterIndexOperand(Dest
);
2096 case MIToken::md_diexpr
:
2097 case MIToken::exclaim
:
2098 return parseMetadataOperand(Dest
);
2099 case MIToken::kw_cfi_same_value
:
2100 case MIToken::kw_cfi_offset
:
2101 case MIToken::kw_cfi_rel_offset
:
2102 case MIToken::kw_cfi_def_cfa_register
:
2103 case MIToken::kw_cfi_def_cfa_offset
:
2104 case MIToken::kw_cfi_adjust_cfa_offset
:
2105 case MIToken::kw_cfi_escape
:
2106 case MIToken::kw_cfi_def_cfa
:
2107 case MIToken::kw_cfi_register
:
2108 case MIToken::kw_cfi_remember_state
:
2109 case MIToken::kw_cfi_restore
:
2110 case MIToken::kw_cfi_restore_state
:
2111 case MIToken::kw_cfi_undefined
:
2112 case MIToken::kw_cfi_window_save
:
2113 return parseCFIOperand(Dest
);
2114 case MIToken::kw_blockaddress
:
2115 return parseBlockAddressOperand(Dest
);
2116 case MIToken::kw_intrinsic
:
2117 return parseIntrinsicOperand(Dest
);
2118 case MIToken::kw_target_index
:
2119 return parseTargetIndexOperand(Dest
);
2120 case MIToken::kw_liveout
:
2121 return parseLiveoutRegisterMaskOperand(Dest
);
2122 case MIToken::kw_floatpred
:
2123 case MIToken::kw_intpred
:
2124 return parsePredicateOperand(Dest
);
2125 case MIToken::Error
:
2127 case MIToken::Identifier
:
2128 if (const auto *RegMask
= getRegMask(Token
.stringValue())) {
2129 Dest
= MachineOperand::CreateRegMask(RegMask
);
2132 } else if (Token
.stringValue() == "CustomRegMask") {
2133 return parseCustomRegisterMaskOperand(Dest
);
2135 return parseTypedImmediateOperand(Dest
);
2137 // FIXME: Parse the MCSymbol machine operand.
2138 return error("expected a machine operand");
2143 bool MIParser::parseMachineOperandAndTargetFlags(
2144 MachineOperand
&Dest
, Optional
<unsigned> &TiedDefIdx
) {
2146 bool HasTargetFlags
= false;
2147 if (Token
.is(MIToken::kw_target_flags
)) {
2148 HasTargetFlags
= true;
2150 if (expectAndConsume(MIToken::lparen
))
2152 if (Token
.isNot(MIToken::Identifier
))
2153 return error("expected the name of the target flag");
2154 if (getDirectTargetFlag(Token
.stringValue(), TF
)) {
2155 if (getBitmaskTargetFlag(Token
.stringValue(), TF
))
2156 return error("use of undefined target flag '" + Token
.stringValue() +
2160 while (Token
.is(MIToken::comma
)) {
2162 if (Token
.isNot(MIToken::Identifier
))
2163 return error("expected the name of the target flag");
2164 unsigned BitFlag
= 0;
2165 if (getBitmaskTargetFlag(Token
.stringValue(), BitFlag
))
2166 return error("use of undefined target flag '" + Token
.stringValue() +
2168 // TODO: Report an error when using a duplicate bit target flag.
2172 if (expectAndConsume(MIToken::rparen
))
2175 auto Loc
= Token
.location();
2176 if (parseMachineOperand(Dest
, TiedDefIdx
))
2178 if (!HasTargetFlags
)
2181 return error(Loc
, "register operands can't have target flags");
2182 Dest
.setTargetFlags(TF
);
2186 bool MIParser::parseOffset(int64_t &Offset
) {
2187 if (Token
.isNot(MIToken::plus
) && Token
.isNot(MIToken::minus
))
2189 StringRef Sign
= Token
.range();
2190 bool IsNegative
= Token
.is(MIToken::minus
);
2192 if (Token
.isNot(MIToken::IntegerLiteral
))
2193 return error("expected an integer literal after '" + Sign
+ "'");
2194 if (Token
.integerValue().getMinSignedBits() > 64)
2195 return error("expected 64-bit integer (too large)");
2196 Offset
= Token
.integerValue().getExtValue();
2203 bool MIParser::parseAlignment(unsigned &Alignment
) {
2204 assert(Token
.is(MIToken::kw_align
));
2206 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
2207 return error("expected an integer literal after 'align'");
2208 if (getUnsigned(Alignment
))
2214 bool MIParser::parseAddrspace(unsigned &Addrspace
) {
2215 assert(Token
.is(MIToken::kw_addrspace
));
2217 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
2218 return error("expected an integer literal after 'addrspace'");
2219 if (getUnsigned(Addrspace
))
2225 bool MIParser::parseOperandsOffset(MachineOperand
&Op
) {
2227 if (parseOffset(Offset
))
2229 Op
.setOffset(Offset
);
2233 bool MIParser::parseIRValue(const Value
*&V
) {
2234 switch (Token
.kind()) {
2235 case MIToken::NamedIRValue
: {
2236 V
= MF
.getFunction().getValueSymbolTable()->lookup(Token
.stringValue());
2239 case MIToken::IRValue
: {
2240 unsigned SlotNumber
= 0;
2241 if (getUnsigned(SlotNumber
))
2243 V
= getIRValue(SlotNumber
);
2246 case MIToken::NamedGlobalValue
:
2247 case MIToken::GlobalValue
: {
2248 GlobalValue
*GV
= nullptr;
2249 if (parseGlobalValue(GV
))
2254 case MIToken::QuotedIRValue
: {
2255 const Constant
*C
= nullptr;
2256 if (parseIRConstant(Token
.location(), Token
.stringValue(), C
))
2262 llvm_unreachable("The current token should be an IR block reference");
2265 return error(Twine("use of undefined IR value '") + Token
.range() + "'");
2269 bool MIParser::getUint64(uint64_t &Result
) {
2270 if (Token
.hasIntegerValue()) {
2271 if (Token
.integerValue().getActiveBits() > 64)
2272 return error("expected 64-bit integer (too large)");
2273 Result
= Token
.integerValue().getZExtValue();
2276 if (Token
.is(MIToken::HexLiteral
)) {
2280 if (A
.getBitWidth() > 64)
2281 return error("expected 64-bit integer (too large)");
2282 Result
= A
.getZExtValue();
2288 bool MIParser::getHexUint(APInt
&Result
) {
2289 assert(Token
.is(MIToken::HexLiteral
));
2290 StringRef S
= Token
.range();
2291 assert(S
[0] == '0' && tolower(S
[1]) == 'x');
2292 // This could be a floating point literal with a special prefix.
2293 if (!isxdigit(S
[2]))
2295 StringRef V
= S
.substr(2);
2296 APInt
A(V
.size()*4, V
, 16);
2298 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2299 // sure it isn't the case before constructing result.
2300 unsigned NumBits
= (A
== 0) ? 32 : A
.getActiveBits();
2301 Result
= APInt(NumBits
, ArrayRef
<uint64_t>(A
.getRawData(), A
.getNumWords()));
2305 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
) {
2306 const auto OldFlags
= Flags
;
2307 switch (Token
.kind()) {
2308 case MIToken::kw_volatile
:
2309 Flags
|= MachineMemOperand::MOVolatile
;
2311 case MIToken::kw_non_temporal
:
2312 Flags
|= MachineMemOperand::MONonTemporal
;
2314 case MIToken::kw_dereferenceable
:
2315 Flags
|= MachineMemOperand::MODereferenceable
;
2317 case MIToken::kw_invariant
:
2318 Flags
|= MachineMemOperand::MOInvariant
;
2320 case MIToken::StringConstant
: {
2321 MachineMemOperand::Flags TF
;
2322 if (getMMOTargetFlag(Token
.stringValue(), TF
))
2323 return error("use of undefined target MMO flag '" + Token
.stringValue() +
2329 llvm_unreachable("The current token should be a memory operand flag");
2331 if (OldFlags
== Flags
)
2332 // We know that the same flag is specified more than once when the flags
2333 // weren't modified.
2334 return error("duplicate '" + Token
.stringValue() + "' memory operand flag");
2339 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
) {
2340 switch (Token
.kind()) {
2341 case MIToken::kw_stack
:
2342 PSV
= MF
.getPSVManager().getStack();
2344 case MIToken::kw_got
:
2345 PSV
= MF
.getPSVManager().getGOT();
2347 case MIToken::kw_jump_table
:
2348 PSV
= MF
.getPSVManager().getJumpTable();
2350 case MIToken::kw_constant_pool
:
2351 PSV
= MF
.getPSVManager().getConstantPool();
2353 case MIToken::FixedStackObject
: {
2355 if (parseFixedStackFrameIndex(FI
))
2357 PSV
= MF
.getPSVManager().getFixedStack(FI
);
2358 // The token was already consumed, so use return here instead of break.
2361 case MIToken::StackObject
: {
2363 if (parseStackFrameIndex(FI
))
2365 PSV
= MF
.getPSVManager().getFixedStack(FI
);
2366 // The token was already consumed, so use return here instead of break.
2369 case MIToken::kw_call_entry
:
2371 switch (Token
.kind()) {
2372 case MIToken::GlobalValue
:
2373 case MIToken::NamedGlobalValue
: {
2374 GlobalValue
*GV
= nullptr;
2375 if (parseGlobalValue(GV
))
2377 PSV
= MF
.getPSVManager().getGlobalValueCallEntry(GV
);
2380 case MIToken::ExternalSymbol
:
2381 PSV
= MF
.getPSVManager().getExternalSymbolCallEntry(
2382 MF
.createExternalSymbolName(Token
.stringValue()));
2386 "expected a global value or an external symbol after 'call-entry'");
2390 llvm_unreachable("The current token should be pseudo source value");
2396 bool MIParser::parseMachinePointerInfo(MachinePointerInfo
&Dest
) {
2397 if (Token
.is(MIToken::kw_constant_pool
) || Token
.is(MIToken::kw_stack
) ||
2398 Token
.is(MIToken::kw_got
) || Token
.is(MIToken::kw_jump_table
) ||
2399 Token
.is(MIToken::FixedStackObject
) || Token
.is(MIToken::StackObject
) ||
2400 Token
.is(MIToken::kw_call_entry
)) {
2401 const PseudoSourceValue
*PSV
= nullptr;
2402 if (parseMemoryPseudoSourceValue(PSV
))
2405 if (parseOffset(Offset
))
2407 Dest
= MachinePointerInfo(PSV
, Offset
);
2410 if (Token
.isNot(MIToken::NamedIRValue
) && Token
.isNot(MIToken::IRValue
) &&
2411 Token
.isNot(MIToken::GlobalValue
) &&
2412 Token
.isNot(MIToken::NamedGlobalValue
) &&
2413 Token
.isNot(MIToken::QuotedIRValue
))
2414 return error("expected an IR value reference");
2415 const Value
*V
= nullptr;
2416 if (parseIRValue(V
))
2418 if (!V
->getType()->isPointerTy())
2419 return error("expected a pointer IR value");
2422 if (parseOffset(Offset
))
2424 Dest
= MachinePointerInfo(V
, Offset
);
2428 bool MIParser::parseOptionalScope(LLVMContext
&Context
,
2429 SyncScope::ID
&SSID
) {
2430 SSID
= SyncScope::System
;
2431 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "syncscope") {
2433 if (expectAndConsume(MIToken::lparen
))
2434 return error("expected '(' in syncscope");
2437 if (parseStringConstant(SSN
))
2440 SSID
= Context
.getOrInsertSyncScopeID(SSN
);
2441 if (expectAndConsume(MIToken::rparen
))
2442 return error("expected ')' in syncscope");
2448 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering
&Order
) {
2449 Order
= AtomicOrdering::NotAtomic
;
2450 if (Token
.isNot(MIToken::Identifier
))
2453 Order
= StringSwitch
<AtomicOrdering
>(Token
.stringValue())
2454 .Case("unordered", AtomicOrdering::Unordered
)
2455 .Case("monotonic", AtomicOrdering::Monotonic
)
2456 .Case("acquire", AtomicOrdering::Acquire
)
2457 .Case("release", AtomicOrdering::Release
)
2458 .Case("acq_rel", AtomicOrdering::AcquireRelease
)
2459 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent
)
2460 .Default(AtomicOrdering::NotAtomic
);
2462 if (Order
!= AtomicOrdering::NotAtomic
) {
2467 return error("expected an atomic scope, ordering or a size specification");
2470 bool MIParser::parseMachineMemoryOperand(MachineMemOperand
*&Dest
) {
2471 if (expectAndConsume(MIToken::lparen
))
2473 MachineMemOperand::Flags Flags
= MachineMemOperand::MONone
;
2474 while (Token
.isMemoryOperandFlag()) {
2475 if (parseMemoryOperandFlag(Flags
))
2478 if (Token
.isNot(MIToken::Identifier
) ||
2479 (Token
.stringValue() != "load" && Token
.stringValue() != "store"))
2480 return error("expected 'load' or 'store' memory operation");
2481 if (Token
.stringValue() == "load")
2482 Flags
|= MachineMemOperand::MOLoad
;
2484 Flags
|= MachineMemOperand::MOStore
;
2487 // Optional 'store' for operands that both load and store.
2488 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "store") {
2489 Flags
|= MachineMemOperand::MOStore
;
2493 // Optional synchronization scope.
2495 if (parseOptionalScope(MF
.getFunction().getContext(), SSID
))
2498 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2499 AtomicOrdering Order
, FailureOrder
;
2500 if (parseOptionalAtomicOrdering(Order
))
2503 if (parseOptionalAtomicOrdering(FailureOrder
))
2506 if (Token
.isNot(MIToken::IntegerLiteral
) &&
2507 Token
.isNot(MIToken::kw_unknown_size
))
2508 return error("expected the size integer literal or 'unknown-size' after "
2509 "memory operation");
2511 if (Token
.is(MIToken::IntegerLiteral
)) {
2512 if (getUint64(Size
))
2514 } else if (Token
.is(MIToken::kw_unknown_size
)) {
2515 Size
= MemoryLocation::UnknownSize
;
2519 MachinePointerInfo Ptr
= MachinePointerInfo();
2520 if (Token
.is(MIToken::Identifier
)) {
2522 ((Flags
& MachineMemOperand::MOLoad
) &&
2523 (Flags
& MachineMemOperand::MOStore
))
2525 : Flags
& MachineMemOperand::MOLoad
? "from" : "into";
2526 if (Token
.stringValue() != Word
)
2527 return error(Twine("expected '") + Word
+ "'");
2530 if (parseMachinePointerInfo(Ptr
))
2533 unsigned BaseAlignment
= (Size
!= MemoryLocation::UnknownSize
? Size
: 1);
2535 MDNode
*Range
= nullptr;
2536 while (consumeIfPresent(MIToken::comma
)) {
2537 switch (Token
.kind()) {
2538 case MIToken::kw_align
:
2539 if (parseAlignment(BaseAlignment
))
2542 case MIToken::kw_addrspace
:
2543 if (parseAddrspace(Ptr
.AddrSpace
))
2546 case MIToken::md_tbaa
:
2548 if (parseMDNode(AAInfo
.TBAA
))
2551 case MIToken::md_alias_scope
:
2553 if (parseMDNode(AAInfo
.Scope
))
2556 case MIToken::md_noalias
:
2558 if (parseMDNode(AAInfo
.NoAlias
))
2561 case MIToken::md_range
:
2563 if (parseMDNode(Range
))
2566 // TODO: Report an error on duplicate metadata nodes.
2568 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2569 "'!noalias' or '!range'");
2572 if (expectAndConsume(MIToken::rparen
))
2574 Dest
= MF
.getMachineMemOperand(Ptr
, Flags
, Size
, BaseAlignment
, AAInfo
, Range
,
2575 SSID
, Order
, FailureOrder
);
2579 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
) {
2580 assert((Token
.is(MIToken::kw_pre_instr_symbol
) ||
2581 Token
.is(MIToken::kw_post_instr_symbol
)) &&
2582 "Invalid token for a pre- post-instruction symbol!");
2584 if (Token
.isNot(MIToken::MCSymbol
))
2585 return error("expected a symbol after 'pre-instr-symbol'");
2586 Symbol
= getOrCreateMCSymbol(Token
.stringValue());
2588 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
2589 Token
.is(MIToken::lbrace
))
2591 if (Token
.isNot(MIToken::comma
))
2592 return error("expected ',' before the next machine operand");
2597 void MIParser::initNames2InstrOpCodes() {
2598 if (!Names2InstrOpCodes
.empty())
2600 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2601 assert(TII
&& "Expected target instruction info");
2602 for (unsigned I
= 0, E
= TII
->getNumOpcodes(); I
< E
; ++I
)
2603 Names2InstrOpCodes
.insert(std::make_pair(StringRef(TII
->getName(I
)), I
));
2606 bool MIParser::parseInstrName(StringRef InstrName
, unsigned &OpCode
) {
2607 initNames2InstrOpCodes();
2608 auto InstrInfo
= Names2InstrOpCodes
.find(InstrName
);
2609 if (InstrInfo
== Names2InstrOpCodes
.end())
2611 OpCode
= InstrInfo
->getValue();
2615 void MIParser::initNames2Regs() {
2616 if (!Names2Regs
.empty())
2618 // The '%noreg' register is the register 0.
2619 Names2Regs
.insert(std::make_pair("noreg", 0));
2620 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
2621 assert(TRI
&& "Expected target register info");
2622 for (unsigned I
= 0, E
= TRI
->getNumRegs(); I
< E
; ++I
) {
2624 Names2Regs
.insert(std::make_pair(StringRef(TRI
->getName(I
)).lower(), I
))
2627 assert(WasInserted
&& "Expected registers to be unique case-insensitively");
2631 bool MIParser::getRegisterByName(StringRef RegName
, unsigned &Reg
) {
2633 auto RegInfo
= Names2Regs
.find(RegName
);
2634 if (RegInfo
== Names2Regs
.end())
2636 Reg
= RegInfo
->getValue();
2640 void MIParser::initNames2RegMasks() {
2641 if (!Names2RegMasks
.empty())
2643 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
2644 assert(TRI
&& "Expected target register info");
2645 ArrayRef
<const uint32_t *> RegMasks
= TRI
->getRegMasks();
2646 ArrayRef
<const char *> RegMaskNames
= TRI
->getRegMaskNames();
2647 assert(RegMasks
.size() == RegMaskNames
.size());
2648 for (size_t I
= 0, E
= RegMasks
.size(); I
< E
; ++I
)
2649 Names2RegMasks
.insert(
2650 std::make_pair(StringRef(RegMaskNames
[I
]).lower(), RegMasks
[I
]));
2653 const uint32_t *MIParser::getRegMask(StringRef Identifier
) {
2654 initNames2RegMasks();
2655 auto RegMaskInfo
= Names2RegMasks
.find(Identifier
);
2656 if (RegMaskInfo
== Names2RegMasks
.end())
2658 return RegMaskInfo
->getValue();
2661 void MIParser::initNames2SubRegIndices() {
2662 if (!Names2SubRegIndices
.empty())
2664 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
2665 for (unsigned I
= 1, E
= TRI
->getNumSubRegIndices(); I
< E
; ++I
)
2666 Names2SubRegIndices
.insert(
2667 std::make_pair(StringRef(TRI
->getSubRegIndexName(I
)).lower(), I
));
2670 unsigned MIParser::getSubRegIndex(StringRef Name
) {
2671 initNames2SubRegIndices();
2672 auto SubRegInfo
= Names2SubRegIndices
.find(Name
);
2673 if (SubRegInfo
== Names2SubRegIndices
.end())
2675 return SubRegInfo
->getValue();
2678 static void initSlots2BasicBlocks(
2680 DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
2681 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
2682 MST
.incorporateFunction(F
);
2683 for (auto &BB
: F
) {
2686 int Slot
= MST
.getLocalSlot(&BB
);
2689 Slots2BasicBlocks
.insert(std::make_pair(unsigned(Slot
), &BB
));
2693 static const BasicBlock
*getIRBlockFromSlot(
2695 const DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
2696 auto BlockInfo
= Slots2BasicBlocks
.find(Slot
);
2697 if (BlockInfo
== Slots2BasicBlocks
.end())
2699 return BlockInfo
->second
;
2702 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
) {
2703 if (Slots2BasicBlocks
.empty())
2704 initSlots2BasicBlocks(MF
.getFunction(), Slots2BasicBlocks
);
2705 return getIRBlockFromSlot(Slot
, Slots2BasicBlocks
);
2708 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
, const Function
&F
) {
2709 if (&F
== &MF
.getFunction())
2710 return getIRBlock(Slot
);
2711 DenseMap
<unsigned, const BasicBlock
*> CustomSlots2BasicBlocks
;
2712 initSlots2BasicBlocks(F
, CustomSlots2BasicBlocks
);
2713 return getIRBlockFromSlot(Slot
, CustomSlots2BasicBlocks
);
2716 static void mapValueToSlot(const Value
*V
, ModuleSlotTracker
&MST
,
2717 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
2718 int Slot
= MST
.getLocalSlot(V
);
2721 Slots2Values
.insert(std::make_pair(unsigned(Slot
), V
));
2724 /// Creates the mapping from slot numbers to function's unnamed IR values.
2725 static void initSlots2Values(const Function
&F
,
2726 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
2727 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
2728 MST
.incorporateFunction(F
);
2729 for (const auto &Arg
: F
.args())
2730 mapValueToSlot(&Arg
, MST
, Slots2Values
);
2731 for (const auto &BB
: F
) {
2732 mapValueToSlot(&BB
, MST
, Slots2Values
);
2733 for (const auto &I
: BB
)
2734 mapValueToSlot(&I
, MST
, Slots2Values
);
2738 const Value
*MIParser::getIRValue(unsigned Slot
) {
2739 if (Slots2Values
.empty())
2740 initSlots2Values(MF
.getFunction(), Slots2Values
);
2741 auto ValueInfo
= Slots2Values
.find(Slot
);
2742 if (ValueInfo
== Slots2Values
.end())
2744 return ValueInfo
->second
;
2747 void MIParser::initNames2TargetIndices() {
2748 if (!Names2TargetIndices
.empty())
2750 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2751 assert(TII
&& "Expected target instruction info");
2752 auto Indices
= TII
->getSerializableTargetIndices();
2753 for (const auto &I
: Indices
)
2754 Names2TargetIndices
.insert(std::make_pair(StringRef(I
.second
), I
.first
));
2757 bool MIParser::getTargetIndex(StringRef Name
, int &Index
) {
2758 initNames2TargetIndices();
2759 auto IndexInfo
= Names2TargetIndices
.find(Name
);
2760 if (IndexInfo
== Names2TargetIndices
.end())
2762 Index
= IndexInfo
->second
;
2766 void MIParser::initNames2DirectTargetFlags() {
2767 if (!Names2DirectTargetFlags
.empty())
2769 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2770 assert(TII
&& "Expected target instruction info");
2771 auto Flags
= TII
->getSerializableDirectMachineOperandTargetFlags();
2772 for (const auto &I
: Flags
)
2773 Names2DirectTargetFlags
.insert(
2774 std::make_pair(StringRef(I
.second
), I
.first
));
2777 bool MIParser::getDirectTargetFlag(StringRef Name
, unsigned &Flag
) {
2778 initNames2DirectTargetFlags();
2779 auto FlagInfo
= Names2DirectTargetFlags
.find(Name
);
2780 if (FlagInfo
== Names2DirectTargetFlags
.end())
2782 Flag
= FlagInfo
->second
;
2786 void MIParser::initNames2BitmaskTargetFlags() {
2787 if (!Names2BitmaskTargetFlags
.empty())
2789 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2790 assert(TII
&& "Expected target instruction info");
2791 auto Flags
= TII
->getSerializableBitmaskMachineOperandTargetFlags();
2792 for (const auto &I
: Flags
)
2793 Names2BitmaskTargetFlags
.insert(
2794 std::make_pair(StringRef(I
.second
), I
.first
));
2797 bool MIParser::getBitmaskTargetFlag(StringRef Name
, unsigned &Flag
) {
2798 initNames2BitmaskTargetFlags();
2799 auto FlagInfo
= Names2BitmaskTargetFlags
.find(Name
);
2800 if (FlagInfo
== Names2BitmaskTargetFlags
.end())
2802 Flag
= FlagInfo
->second
;
2806 void MIParser::initNames2MMOTargetFlags() {
2807 if (!Names2MMOTargetFlags
.empty())
2809 const auto *TII
= MF
.getSubtarget().getInstrInfo();
2810 assert(TII
&& "Expected target instruction info");
2811 auto Flags
= TII
->getSerializableMachineMemOperandTargetFlags();
2812 for (const auto &I
: Flags
)
2813 Names2MMOTargetFlags
.insert(
2814 std::make_pair(StringRef(I
.second
), I
.first
));
2817 bool MIParser::getMMOTargetFlag(StringRef Name
,
2818 MachineMemOperand::Flags
&Flag
) {
2819 initNames2MMOTargetFlags();
2820 auto FlagInfo
= Names2MMOTargetFlags
.find(Name
);
2821 if (FlagInfo
== Names2MMOTargetFlags
.end())
2823 Flag
= FlagInfo
->second
;
2827 MCSymbol
*MIParser::getOrCreateMCSymbol(StringRef Name
) {
2828 // FIXME: Currently we can't recognize temporary or local symbols and call all
2829 // of the appropriate forms to create them. However, this handles basic cases
2830 // well as most of the special aspects are recognized by a prefix on their
2831 // name, and the input names should already be unique. For test cases, keeping
2832 // the symbol name out of the symbol table isn't terribly important.
2833 return MF
.getContext().getOrCreateSymbol(Name
);
2836 bool MIParser::parseStringConstant(std::string
&Result
) {
2837 if (Token
.isNot(MIToken::StringConstant
))
2838 return error("expected string constant");
2839 Result
= Token
.stringValue();
2844 bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState
&PFS
,
2846 SMDiagnostic
&Error
) {
2847 return MIParser(PFS
, Error
, Src
).parseBasicBlockDefinitions(PFS
.MBBSlots
);
2850 bool llvm::parseMachineInstructions(PerFunctionMIParsingState
&PFS
,
2851 StringRef Src
, SMDiagnostic
&Error
) {
2852 return MIParser(PFS
, Error
, Src
).parseBasicBlocks();
2855 bool llvm::parseMBBReference(PerFunctionMIParsingState
&PFS
,
2856 MachineBasicBlock
*&MBB
, StringRef Src
,
2857 SMDiagnostic
&Error
) {
2858 return MIParser(PFS
, Error
, Src
).parseStandaloneMBB(MBB
);
2861 bool llvm::parseRegisterReference(PerFunctionMIParsingState
&PFS
,
2862 unsigned &Reg
, StringRef Src
,
2863 SMDiagnostic
&Error
) {
2864 return MIParser(PFS
, Error
, Src
).parseStandaloneRegister(Reg
);
2867 bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState
&PFS
,
2868 unsigned &Reg
, StringRef Src
,
2869 SMDiagnostic
&Error
) {
2870 return MIParser(PFS
, Error
, Src
).parseStandaloneNamedRegister(Reg
);
2873 bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState
&PFS
,
2874 VRegInfo
*&Info
, StringRef Src
,
2875 SMDiagnostic
&Error
) {
2876 return MIParser(PFS
, Error
, Src
).parseStandaloneVirtualRegister(Info
);
2879 bool llvm::parseStackObjectReference(PerFunctionMIParsingState
&PFS
,
2880 int &FI
, StringRef Src
,
2881 SMDiagnostic
&Error
) {
2882 return MIParser(PFS
, Error
, Src
).parseStandaloneStackObject(FI
);
2885 bool llvm::parseMDNode(PerFunctionMIParsingState
&PFS
,
2886 MDNode
*&Node
, StringRef Src
, SMDiagnostic
&Error
) {
2887 return MIParser(PFS
, Error
, Src
).parseStandaloneMDNode(Node
);