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/None.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Analysis/MemoryLocation.h"
27 #include "llvm/AsmParser/Parser.h"
28 #include "llvm/AsmParser/SlotMapping.h"
29 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
30 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
31 #include "llvm/CodeGen/MIRPrinter.h"
32 #include "llvm/CodeGen/MachineBasicBlock.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineOperand.h"
39 #include "llvm/CodeGen/MachineRegisterInfo.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/MC/MCRegisterInfo.h"
63 #include "llvm/Support/AtomicOrdering.h"
64 #include "llvm/Support/BranchProbability.h"
65 #include "llvm/Support/Casting.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/LowLevelTypeImpl.h"
68 #include "llvm/Support/MemoryBuffer.h"
69 #include "llvm/Support/SMLoc.h"
70 #include "llvm/Support/SourceMgr.h"
71 #include "llvm/Support/raw_ostream.h"
72 #include "llvm/Target/TargetIntrinsicInfo.h"
73 #include "llvm/Target/TargetMachine.h"
85 void PerTargetMIParsingState::setTarget(
86 const TargetSubtargetInfo
&NewSubtarget
) {
88 // If the subtarget changed, over conservatively assume everything is invalid.
89 if (&Subtarget
== &NewSubtarget
)
92 Names2InstrOpCodes
.clear();
94 Names2RegMasks
.clear();
95 Names2SubRegIndices
.clear();
96 Names2TargetIndices
.clear();
97 Names2DirectTargetFlags
.clear();
98 Names2BitmaskTargetFlags
.clear();
99 Names2MMOTargetFlags
.clear();
101 initNames2RegClasses();
102 initNames2RegBanks();
105 void PerTargetMIParsingState::initNames2Regs() {
106 if (!Names2Regs
.empty())
109 // The '%noreg' register is the register 0.
110 Names2Regs
.insert(std::make_pair("noreg", 0));
111 const auto *TRI
= Subtarget
.getRegisterInfo();
112 assert(TRI
&& "Expected target register info");
114 for (unsigned I
= 0, E
= TRI
->getNumRegs(); I
< E
; ++I
) {
116 Names2Regs
.insert(std::make_pair(StringRef(TRI
->getName(I
)).lower(), I
))
119 assert(WasInserted
&& "Expected registers to be unique case-insensitively");
123 bool PerTargetMIParsingState::getRegisterByName(StringRef RegName
,
126 auto RegInfo
= Names2Regs
.find(RegName
);
127 if (RegInfo
== Names2Regs
.end())
129 Reg
= RegInfo
->getValue();
133 void PerTargetMIParsingState::initNames2InstrOpCodes() {
134 if (!Names2InstrOpCodes
.empty())
136 const auto *TII
= Subtarget
.getInstrInfo();
137 assert(TII
&& "Expected target instruction info");
138 for (unsigned I
= 0, E
= TII
->getNumOpcodes(); I
< E
; ++I
)
139 Names2InstrOpCodes
.insert(std::make_pair(StringRef(TII
->getName(I
)), I
));
142 bool PerTargetMIParsingState::parseInstrName(StringRef InstrName
,
144 initNames2InstrOpCodes();
145 auto InstrInfo
= Names2InstrOpCodes
.find(InstrName
);
146 if (InstrInfo
== Names2InstrOpCodes
.end())
148 OpCode
= InstrInfo
->getValue();
152 void PerTargetMIParsingState::initNames2RegMasks() {
153 if (!Names2RegMasks
.empty())
155 const auto *TRI
= Subtarget
.getRegisterInfo();
156 assert(TRI
&& "Expected target register info");
157 ArrayRef
<const uint32_t *> RegMasks
= TRI
->getRegMasks();
158 ArrayRef
<const char *> RegMaskNames
= TRI
->getRegMaskNames();
159 assert(RegMasks
.size() == RegMaskNames
.size());
160 for (size_t I
= 0, E
= RegMasks
.size(); I
< E
; ++I
)
161 Names2RegMasks
.insert(
162 std::make_pair(StringRef(RegMaskNames
[I
]).lower(), RegMasks
[I
]));
165 const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier
) {
166 initNames2RegMasks();
167 auto RegMaskInfo
= Names2RegMasks
.find(Identifier
);
168 if (RegMaskInfo
== Names2RegMasks
.end())
170 return RegMaskInfo
->getValue();
173 void PerTargetMIParsingState::initNames2SubRegIndices() {
174 if (!Names2SubRegIndices
.empty())
176 const TargetRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
177 for (unsigned I
= 1, E
= TRI
->getNumSubRegIndices(); I
< E
; ++I
)
178 Names2SubRegIndices
.insert(
179 std::make_pair(TRI
->getSubRegIndexName(I
), I
));
182 unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name
) {
183 initNames2SubRegIndices();
184 auto SubRegInfo
= Names2SubRegIndices
.find(Name
);
185 if (SubRegInfo
== Names2SubRegIndices
.end())
187 return SubRegInfo
->getValue();
190 void PerTargetMIParsingState::initNames2TargetIndices() {
191 if (!Names2TargetIndices
.empty())
193 const auto *TII
= Subtarget
.getInstrInfo();
194 assert(TII
&& "Expected target instruction info");
195 auto Indices
= TII
->getSerializableTargetIndices();
196 for (const auto &I
: Indices
)
197 Names2TargetIndices
.insert(std::make_pair(StringRef(I
.second
), I
.first
));
200 bool PerTargetMIParsingState::getTargetIndex(StringRef Name
, int &Index
) {
201 initNames2TargetIndices();
202 auto IndexInfo
= Names2TargetIndices
.find(Name
);
203 if (IndexInfo
== Names2TargetIndices
.end())
205 Index
= IndexInfo
->second
;
209 void PerTargetMIParsingState::initNames2DirectTargetFlags() {
210 if (!Names2DirectTargetFlags
.empty())
213 const auto *TII
= Subtarget
.getInstrInfo();
214 assert(TII
&& "Expected target instruction info");
215 auto Flags
= TII
->getSerializableDirectMachineOperandTargetFlags();
216 for (const auto &I
: Flags
)
217 Names2DirectTargetFlags
.insert(
218 std::make_pair(StringRef(I
.second
), I
.first
));
221 bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name
,
223 initNames2DirectTargetFlags();
224 auto FlagInfo
= Names2DirectTargetFlags
.find(Name
);
225 if (FlagInfo
== Names2DirectTargetFlags
.end())
227 Flag
= FlagInfo
->second
;
231 void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
232 if (!Names2BitmaskTargetFlags
.empty())
235 const auto *TII
= Subtarget
.getInstrInfo();
236 assert(TII
&& "Expected target instruction info");
237 auto Flags
= TII
->getSerializableBitmaskMachineOperandTargetFlags();
238 for (const auto &I
: Flags
)
239 Names2BitmaskTargetFlags
.insert(
240 std::make_pair(StringRef(I
.second
), I
.first
));
243 bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name
,
245 initNames2BitmaskTargetFlags();
246 auto FlagInfo
= Names2BitmaskTargetFlags
.find(Name
);
247 if (FlagInfo
== Names2BitmaskTargetFlags
.end())
249 Flag
= FlagInfo
->second
;
253 void PerTargetMIParsingState::initNames2MMOTargetFlags() {
254 if (!Names2MMOTargetFlags
.empty())
257 const auto *TII
= Subtarget
.getInstrInfo();
258 assert(TII
&& "Expected target instruction info");
259 auto Flags
= TII
->getSerializableMachineMemOperandTargetFlags();
260 for (const auto &I
: Flags
)
261 Names2MMOTargetFlags
.insert(std::make_pair(StringRef(I
.second
), I
.first
));
264 bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name
,
265 MachineMemOperand::Flags
&Flag
) {
266 initNames2MMOTargetFlags();
267 auto FlagInfo
= Names2MMOTargetFlags
.find(Name
);
268 if (FlagInfo
== Names2MMOTargetFlags
.end())
270 Flag
= FlagInfo
->second
;
274 void PerTargetMIParsingState::initNames2RegClasses() {
275 if (!Names2RegClasses
.empty())
278 const TargetRegisterInfo
*TRI
= Subtarget
.getRegisterInfo();
279 for (unsigned I
= 0, E
= TRI
->getNumRegClasses(); I
< E
; ++I
) {
280 const auto *RC
= TRI
->getRegClass(I
);
281 Names2RegClasses
.insert(
282 std::make_pair(StringRef(TRI
->getRegClassName(RC
)).lower(), RC
));
286 void PerTargetMIParsingState::initNames2RegBanks() {
287 if (!Names2RegBanks
.empty())
290 const RegisterBankInfo
*RBI
= Subtarget
.getRegBankInfo();
291 // If the target does not support GlobalISel, we may not have a
292 // register bank info.
296 for (unsigned I
= 0, E
= RBI
->getNumRegBanks(); I
< E
; ++I
) {
297 const auto &RegBank
= RBI
->getRegBank(I
);
298 Names2RegBanks
.insert(
299 std::make_pair(StringRef(RegBank
.getName()).lower(), &RegBank
));
303 const TargetRegisterClass
*
304 PerTargetMIParsingState::getRegClass(StringRef Name
) {
305 auto RegClassInfo
= Names2RegClasses
.find(Name
);
306 if (RegClassInfo
== Names2RegClasses
.end())
308 return RegClassInfo
->getValue();
311 const RegisterBank
*PerTargetMIParsingState::getRegBank(StringRef Name
) {
312 auto RegBankInfo
= Names2RegBanks
.find(Name
);
313 if (RegBankInfo
== Names2RegBanks
.end())
315 return RegBankInfo
->getValue();
318 PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction
&MF
,
319 SourceMgr
&SM
, const SlotMapping
&IRSlots
, PerTargetMIParsingState
&T
)
320 : MF(MF
), SM(&SM
), IRSlots(IRSlots
), Target(T
) {
323 VRegInfo
&PerFunctionMIParsingState::getVRegInfo(unsigned Num
) {
324 auto I
= VRegInfos
.insert(std::make_pair(Num
, nullptr));
326 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
327 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
328 Info
->VReg
= MRI
.createIncompleteVirtualRegister();
329 I
.first
->second
= Info
;
331 return *I
.first
->second
;
334 VRegInfo
&PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName
) {
335 assert(RegName
!= "" && "Expected named reg.");
337 auto I
= VRegInfosNamed
.insert(std::make_pair(RegName
.str(), nullptr));
339 VRegInfo
*Info
= new (Allocator
) VRegInfo
;
340 Info
->VReg
= MF
.getRegInfo().createIncompleteVirtualRegister(RegName
);
341 I
.first
->second
= Info
;
343 return *I
.first
->second
;
348 /// A wrapper struct around the 'MachineOperand' struct that includes a source
349 /// range and other attributes.
350 struct ParsedMachineOperand
{
351 MachineOperand Operand
;
352 StringRef::iterator Begin
;
353 StringRef::iterator End
;
354 Optional
<unsigned> TiedDefIdx
;
356 ParsedMachineOperand(const MachineOperand
&Operand
, StringRef::iterator Begin
,
357 StringRef::iterator End
, Optional
<unsigned> &TiedDefIdx
)
358 : Operand(Operand
), Begin(Begin
), End(End
), TiedDefIdx(TiedDefIdx
) {
360 assert(Operand
.isReg() && Operand
.isUse() &&
361 "Only used register operands can be tied");
368 StringRef Source
, CurrentSource
;
370 PerFunctionMIParsingState
&PFS
;
371 /// Maps from slot numbers to function's unnamed basic blocks.
372 DenseMap
<unsigned, const BasicBlock
*> Slots2BasicBlocks
;
373 /// Maps from slot numbers to function's unnamed values.
374 DenseMap
<unsigned, const Value
*> Slots2Values
;
377 MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
380 /// \p SkipChar gives the number of characters to skip before looking
381 /// for the next token.
382 void lex(unsigned SkipChar
= 0);
384 /// Report an error at the current location with the given message.
386 /// This function always return true.
387 bool error(const Twine
&Msg
);
389 /// Report an error at the given location with the given message.
391 /// This function always return true.
392 bool error(StringRef::iterator Loc
, const Twine
&Msg
);
395 parseBasicBlockDefinitions(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
396 bool parseBasicBlocks();
397 bool parse(MachineInstr
*&MI
);
398 bool parseStandaloneMBB(MachineBasicBlock
*&MBB
);
399 bool parseStandaloneNamedRegister(unsigned &Reg
);
400 bool parseStandaloneVirtualRegister(VRegInfo
*&Info
);
401 bool parseStandaloneRegister(unsigned &Reg
);
402 bool parseStandaloneStackObject(int &FI
);
403 bool parseStandaloneMDNode(MDNode
*&Node
);
406 parseBasicBlockDefinition(DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
);
407 bool parseBasicBlock(MachineBasicBlock
&MBB
,
408 MachineBasicBlock
*&AddFalthroughFrom
);
409 bool parseBasicBlockLiveins(MachineBasicBlock
&MBB
);
410 bool parseBasicBlockSuccessors(MachineBasicBlock
&MBB
);
412 bool parseNamedRegister(unsigned &Reg
);
413 bool parseVirtualRegister(VRegInfo
*&Info
);
414 bool parseNamedVirtualRegister(VRegInfo
*&Info
);
415 bool parseRegister(unsigned &Reg
, VRegInfo
*&VRegInfo
);
416 bool parseRegisterFlag(unsigned &Flags
);
417 bool parseRegisterClassOrBank(VRegInfo
&RegInfo
);
418 bool parseSubRegisterIndex(unsigned &SubReg
);
419 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx
);
420 bool parseRegisterOperand(MachineOperand
&Dest
,
421 Optional
<unsigned> &TiedDefIdx
, bool IsDef
= false);
422 bool parseImmediateOperand(MachineOperand
&Dest
);
423 bool parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
425 bool parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
);
426 bool parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
);
427 bool parseTypedImmediateOperand(MachineOperand
&Dest
);
428 bool parseFPImmediateOperand(MachineOperand
&Dest
);
429 bool parseMBBReference(MachineBasicBlock
*&MBB
);
430 bool parseMBBOperand(MachineOperand
&Dest
);
431 bool parseStackFrameIndex(int &FI
);
432 bool parseStackObjectOperand(MachineOperand
&Dest
);
433 bool parseFixedStackFrameIndex(int &FI
);
434 bool parseFixedStackObjectOperand(MachineOperand
&Dest
);
435 bool parseGlobalValue(GlobalValue
*&GV
);
436 bool parseGlobalAddressOperand(MachineOperand
&Dest
);
437 bool parseConstantPoolIndexOperand(MachineOperand
&Dest
);
438 bool parseSubRegisterIndexOperand(MachineOperand
&Dest
);
439 bool parseJumpTableIndexOperand(MachineOperand
&Dest
);
440 bool parseExternalSymbolOperand(MachineOperand
&Dest
);
441 bool parseMCSymbolOperand(MachineOperand
&Dest
);
442 bool parseMDNode(MDNode
*&Node
);
443 bool parseDIExpression(MDNode
*&Expr
);
444 bool parseDILocation(MDNode
*&Expr
);
445 bool parseMetadataOperand(MachineOperand
&Dest
);
446 bool parseCFIOffset(int &Offset
);
447 bool parseCFIRegister(unsigned &Reg
);
448 bool parseCFIEscapeValues(std::string
& Values
);
449 bool parseCFIOperand(MachineOperand
&Dest
);
450 bool parseIRBlock(BasicBlock
*&BB
, const Function
&F
);
451 bool parseBlockAddressOperand(MachineOperand
&Dest
);
452 bool parseIntrinsicOperand(MachineOperand
&Dest
);
453 bool parsePredicateOperand(MachineOperand
&Dest
);
454 bool parseTargetIndexOperand(MachineOperand
&Dest
);
455 bool parseCustomRegisterMaskOperand(MachineOperand
&Dest
);
456 bool parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
);
457 bool parseMachineOperand(MachineOperand
&Dest
,
458 Optional
<unsigned> &TiedDefIdx
);
459 bool parseMachineOperandAndTargetFlags(MachineOperand
&Dest
,
460 Optional
<unsigned> &TiedDefIdx
);
461 bool parseOffset(int64_t &Offset
);
462 bool parseAlignment(unsigned &Alignment
);
463 bool parseAddrspace(unsigned &Addrspace
);
464 bool parseOperandsOffset(MachineOperand
&Op
);
465 bool parseIRValue(const Value
*&V
);
466 bool parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
);
467 bool parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
);
468 bool parseMachinePointerInfo(MachinePointerInfo
&Dest
);
469 bool parseOptionalScope(LLVMContext
&Context
, SyncScope::ID
&SSID
);
470 bool parseOptionalAtomicOrdering(AtomicOrdering
&Order
);
471 bool parseMachineMemoryOperand(MachineMemOperand
*&Dest
);
472 bool parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
);
475 /// Convert the integer literal in the current token into an unsigned integer.
477 /// Return true if an error occurred.
478 bool getUnsigned(unsigned &Result
);
480 /// Convert the integer literal in the current token into an uint64.
482 /// Return true if an error occurred.
483 bool getUint64(uint64_t &Result
);
485 /// Convert the hexadecimal literal in the current token into an unsigned
486 /// APInt with a minimum bitwidth required to represent the value.
488 /// Return true if the literal does not represent an integer value.
489 bool getHexUint(APInt
&Result
);
491 /// If the current token is of the given kind, consume it and return false.
492 /// Otherwise report an error and return true.
493 bool expectAndConsume(MIToken::TokenKind TokenKind
);
495 /// If the current token is of the given kind, consume it and return true.
496 /// Otherwise return false.
497 bool consumeIfPresent(MIToken::TokenKind TokenKind
);
499 bool parseInstruction(unsigned &OpCode
, unsigned &Flags
);
501 bool assignRegisterTies(MachineInstr
&MI
,
502 ArrayRef
<ParsedMachineOperand
> Operands
);
504 bool verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
505 const MCInstrDesc
&MCID
);
507 const BasicBlock
*getIRBlock(unsigned Slot
);
508 const BasicBlock
*getIRBlock(unsigned Slot
, const Function
&F
);
510 const Value
*getIRValue(unsigned Slot
);
512 /// Get or create an MCSymbol for a given name.
513 MCSymbol
*getOrCreateMCSymbol(StringRef Name
);
515 /// parseStringConstant
516 /// ::= StringConstant
517 bool parseStringConstant(std::string
&Result
);
520 } // end anonymous namespace
522 MIParser::MIParser(PerFunctionMIParsingState
&PFS
, SMDiagnostic
&Error
,
524 : MF(PFS
.MF
), Error(Error
), Source(Source
), CurrentSource(Source
), PFS(PFS
)
527 void MIParser::lex(unsigned SkipChar
) {
528 CurrentSource
= lexMIToken(
529 CurrentSource
.data() + SkipChar
, Token
,
530 [this](StringRef::iterator Loc
, const Twine
&Msg
) { error(Loc
, Msg
); });
533 bool MIParser::error(const Twine
&Msg
) { return error(Token
.location(), Msg
); }
535 bool MIParser::error(StringRef::iterator Loc
, const Twine
&Msg
) {
536 const SourceMgr
&SM
= *PFS
.SM
;
537 assert(Loc
>= Source
.data() && Loc
<= (Source
.data() + Source
.size()));
538 const MemoryBuffer
&Buffer
= *SM
.getMemoryBuffer(SM
.getMainFileID());
539 if (Loc
>= Buffer
.getBufferStart() && Loc
<= Buffer
.getBufferEnd()) {
540 // Create an ordinary diagnostic when the source manager's buffer is the
542 Error
= SM
.GetMessage(SMLoc::getFromPointer(Loc
), SourceMgr::DK_Error
, Msg
);
545 // Create a diagnostic for a YAML string literal.
546 Error
= SMDiagnostic(SM
, SMLoc(), Buffer
.getBufferIdentifier(), 1,
547 Loc
- Source
.data(), SourceMgr::DK_Error
, Msg
.str(),
552 static const char *toString(MIToken::TokenKind TokenKind
) {
560 case MIToken::lparen
:
562 case MIToken::rparen
:
565 return "<unknown token>";
569 bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind
) {
570 if (Token
.isNot(TokenKind
))
571 return error(Twine("expected ") + toString(TokenKind
));
576 bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind
) {
577 if (Token
.isNot(TokenKind
))
583 bool MIParser::parseBasicBlockDefinition(
584 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
585 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
589 auto Loc
= Token
.location();
590 auto Name
= Token
.stringValue();
592 bool HasAddressTaken
= false;
593 bool IsLandingPad
= false;
594 unsigned Alignment
= 0;
595 BasicBlock
*BB
= nullptr;
596 if (consumeIfPresent(MIToken::lparen
)) {
598 // TODO: Report an error when multiple same attributes are specified.
599 switch (Token
.kind()) {
600 case MIToken::kw_address_taken
:
601 HasAddressTaken
= true;
604 case MIToken::kw_landing_pad
:
608 case MIToken::kw_align
:
609 if (parseAlignment(Alignment
))
612 case MIToken::IRBlock
:
613 // TODO: Report an error when both name and ir block are specified.
614 if (parseIRBlock(BB
, MF
.getFunction()))
621 } while (consumeIfPresent(MIToken::comma
));
622 if (expectAndConsume(MIToken::rparen
))
625 if (expectAndConsume(MIToken::colon
))
629 BB
= dyn_cast_or_null
<BasicBlock
>(
630 MF
.getFunction().getValueSymbolTable()->lookup(Name
));
632 return error(Loc
, Twine("basic block '") + Name
+
633 "' is not defined in the function '" +
636 auto *MBB
= MF
.CreateMachineBasicBlock(BB
);
637 MF
.insert(MF
.end(), MBB
);
638 bool WasInserted
= MBBSlots
.insert(std::make_pair(ID
, MBB
)).second
;
640 return error(Loc
, Twine("redefinition of machine basic block with id #") +
643 MBB
->setAlignment(Alignment
);
645 MBB
->setHasAddressTaken();
646 MBB
->setIsEHPad(IsLandingPad
);
650 bool MIParser::parseBasicBlockDefinitions(
651 DenseMap
<unsigned, MachineBasicBlock
*> &MBBSlots
) {
653 // Skip until the first machine basic block.
654 while (Token
.is(MIToken::Newline
))
656 if (Token
.isErrorOrEOF())
657 return Token
.isError();
658 if (Token
.isNot(MIToken::MachineBasicBlockLabel
))
659 return error("expected a basic block definition before instructions");
660 unsigned BraceDepth
= 0;
662 if (parseBasicBlockDefinition(MBBSlots
))
664 bool IsAfterNewline
= false;
665 // Skip until the next machine basic block.
667 if ((Token
.is(MIToken::MachineBasicBlockLabel
) && IsAfterNewline
) ||
668 Token
.isErrorOrEOF())
670 else if (Token
.is(MIToken::MachineBasicBlockLabel
))
671 return error("basic block definition should be located at the start of "
673 else if (consumeIfPresent(MIToken::Newline
)) {
674 IsAfterNewline
= true;
677 IsAfterNewline
= false;
678 if (Token
.is(MIToken::lbrace
))
680 if (Token
.is(MIToken::rbrace
)) {
682 return error("extraneous closing brace ('}')");
687 // Verify that we closed all of the '{' at the end of a file or a block.
688 if (!Token
.isError() && BraceDepth
)
689 return error("expected '}'"); // FIXME: Report a note that shows '{'.
690 } while (!Token
.isErrorOrEOF());
691 return Token
.isError();
694 bool MIParser::parseBasicBlockLiveins(MachineBasicBlock
&MBB
) {
695 assert(Token
.is(MIToken::kw_liveins
));
697 if (expectAndConsume(MIToken::colon
))
699 if (Token
.isNewlineOrEOF()) // Allow an empty list of liveins.
702 if (Token
.isNot(MIToken::NamedRegister
))
703 return error("expected a named register");
705 if (parseNamedRegister(Reg
))
708 LaneBitmask Mask
= LaneBitmask::getAll();
709 if (consumeIfPresent(MIToken::colon
)) {
711 if (Token
.isNot(MIToken::IntegerLiteral
) &&
712 Token
.isNot(MIToken::HexLiteral
))
713 return error("expected a lane mask");
714 static_assert(sizeof(LaneBitmask::Type
) == sizeof(unsigned),
715 "Use correct get-function for lane mask");
718 return error("invalid lane mask value");
719 Mask
= LaneBitmask(V
);
722 MBB
.addLiveIn(Reg
, Mask
);
723 } while (consumeIfPresent(MIToken::comma
));
727 bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock
&MBB
) {
728 assert(Token
.is(MIToken::kw_successors
));
730 if (expectAndConsume(MIToken::colon
))
732 if (Token
.isNewlineOrEOF()) // Allow an empty list of successors.
735 if (Token
.isNot(MIToken::MachineBasicBlock
))
736 return error("expected a machine basic block reference");
737 MachineBasicBlock
*SuccMBB
= nullptr;
738 if (parseMBBReference(SuccMBB
))
742 if (consumeIfPresent(MIToken::lparen
)) {
743 if (Token
.isNot(MIToken::IntegerLiteral
) &&
744 Token
.isNot(MIToken::HexLiteral
))
745 return error("expected an integer literal after '('");
746 if (getUnsigned(Weight
))
749 if (expectAndConsume(MIToken::rparen
))
752 MBB
.addSuccessor(SuccMBB
, BranchProbability::getRaw(Weight
));
753 } while (consumeIfPresent(MIToken::comma
));
754 MBB
.normalizeSuccProbs();
758 bool MIParser::parseBasicBlock(MachineBasicBlock
&MBB
,
759 MachineBasicBlock
*&AddFalthroughFrom
) {
760 // Skip the definition.
761 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
763 if (consumeIfPresent(MIToken::lparen
)) {
764 while (Token
.isNot(MIToken::rparen
) && !Token
.isErrorOrEOF())
766 consumeIfPresent(MIToken::rparen
);
768 consumeIfPresent(MIToken::colon
);
770 // Parse the liveins and successors.
771 // N.B: Multiple lists of successors and liveins are allowed and they're
778 // liveins: %edi, %esi
779 bool ExplicitSuccessors
= false;
781 if (Token
.is(MIToken::kw_successors
)) {
782 if (parseBasicBlockSuccessors(MBB
))
784 ExplicitSuccessors
= true;
785 } else if (Token
.is(MIToken::kw_liveins
)) {
786 if (parseBasicBlockLiveins(MBB
))
788 } else if (consumeIfPresent(MIToken::Newline
)) {
792 if (!Token
.isNewlineOrEOF())
793 return error("expected line break at the end of a list");
797 // Parse the instructions.
798 bool IsInBundle
= false;
799 MachineInstr
*PrevMI
= nullptr;
800 while (!Token
.is(MIToken::MachineBasicBlockLabel
) &&
801 !Token
.is(MIToken::Eof
)) {
802 if (consumeIfPresent(MIToken::Newline
))
804 if (consumeIfPresent(MIToken::rbrace
)) {
805 // The first parsing pass should verify that all closing '}' have an
811 MachineInstr
*MI
= nullptr;
814 MBB
.insert(MBB
.end(), MI
);
816 PrevMI
->setFlag(MachineInstr::BundledSucc
);
817 MI
->setFlag(MachineInstr::BundledPred
);
820 if (Token
.is(MIToken::lbrace
)) {
822 return error("nested instruction bundles are not allowed");
824 // This instruction is the start of the bundle.
825 MI
->setFlag(MachineInstr::BundledSucc
);
827 if (!Token
.is(MIToken::Newline
))
828 // The next instruction can be on the same line.
831 assert(Token
.isNewlineOrEOF() && "MI is not fully parsed");
835 // Construct successor list by searching for basic block machine operands.
836 if (!ExplicitSuccessors
) {
837 SmallVector
<MachineBasicBlock
*,4> Successors
;
839 guessSuccessors(MBB
, Successors
, IsFallthrough
);
840 for (MachineBasicBlock
*Succ
: Successors
)
841 MBB
.addSuccessor(Succ
);
844 AddFalthroughFrom
= &MBB
;
846 MBB
.normalizeSuccProbs();
853 bool MIParser::parseBasicBlocks() {
855 // Skip until the first machine basic block.
856 while (Token
.is(MIToken::Newline
))
858 if (Token
.isErrorOrEOF())
859 return Token
.isError();
860 // The first parsing pass should have verified that this token is a MBB label
861 // in the 'parseBasicBlockDefinitions' method.
862 assert(Token
.is(MIToken::MachineBasicBlockLabel
));
863 MachineBasicBlock
*AddFalthroughFrom
= nullptr;
865 MachineBasicBlock
*MBB
= nullptr;
866 if (parseMBBReference(MBB
))
868 if (AddFalthroughFrom
) {
869 if (!AddFalthroughFrom
->isSuccessor(MBB
))
870 AddFalthroughFrom
->addSuccessor(MBB
);
871 AddFalthroughFrom
->normalizeSuccProbs();
872 AddFalthroughFrom
= nullptr;
874 if (parseBasicBlock(*MBB
, AddFalthroughFrom
))
876 // The method 'parseBasicBlock' should parse the whole block until the next
877 // block or the end of file.
878 assert(Token
.is(MIToken::MachineBasicBlockLabel
) || Token
.is(MIToken::Eof
));
879 } while (Token
.isNot(MIToken::Eof
));
883 bool MIParser::parse(MachineInstr
*&MI
) {
884 // Parse any register operands before '='
885 MachineOperand MO
= MachineOperand::CreateImm(0);
886 SmallVector
<ParsedMachineOperand
, 8> Operands
;
887 while (Token
.isRegister() || Token
.isRegisterFlag()) {
888 auto Loc
= Token
.location();
889 Optional
<unsigned> TiedDefIdx
;
890 if (parseRegisterOperand(MO
, TiedDefIdx
, /*IsDef=*/true))
893 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
894 if (Token
.isNot(MIToken::comma
))
898 if (!Operands
.empty() && expectAndConsume(MIToken::equal
))
901 unsigned OpCode
, Flags
= 0;
902 if (Token
.isError() || parseInstruction(OpCode
, Flags
))
905 // Parse the remaining machine operands.
906 while (!Token
.isNewlineOrEOF() && Token
.isNot(MIToken::kw_pre_instr_symbol
) &&
907 Token
.isNot(MIToken::kw_post_instr_symbol
) &&
908 Token
.isNot(MIToken::kw_debug_location
) &&
909 Token
.isNot(MIToken::coloncolon
) && Token
.isNot(MIToken::lbrace
)) {
910 auto Loc
= Token
.location();
911 Optional
<unsigned> TiedDefIdx
;
912 if (parseMachineOperandAndTargetFlags(MO
, TiedDefIdx
))
914 if (OpCode
== TargetOpcode::DBG_VALUE
&& MO
.isReg())
917 ParsedMachineOperand(MO
, Loc
, Token
.location(), TiedDefIdx
));
918 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
919 Token
.is(MIToken::lbrace
))
921 if (Token
.isNot(MIToken::comma
))
922 return error("expected ',' before the next machine operand");
926 MCSymbol
*PreInstrSymbol
= nullptr;
927 if (Token
.is(MIToken::kw_pre_instr_symbol
))
928 if (parsePreOrPostInstrSymbol(PreInstrSymbol
))
930 MCSymbol
*PostInstrSymbol
= nullptr;
931 if (Token
.is(MIToken::kw_post_instr_symbol
))
932 if (parsePreOrPostInstrSymbol(PostInstrSymbol
))
935 DebugLoc DebugLocation
;
936 if (Token
.is(MIToken::kw_debug_location
)) {
938 MDNode
*Node
= nullptr;
939 if (Token
.is(MIToken::exclaim
)) {
940 if (parseMDNode(Node
))
942 } else if (Token
.is(MIToken::md_dilocation
)) {
943 if (parseDILocation(Node
))
946 return error("expected a metadata node after 'debug-location'");
947 if (!isa
<DILocation
>(Node
))
948 return error("referenced metadata is not a DILocation");
949 DebugLocation
= DebugLoc(Node
);
952 // Parse the machine memory operands.
953 SmallVector
<MachineMemOperand
*, 2> MemOperands
;
954 if (Token
.is(MIToken::coloncolon
)) {
956 while (!Token
.isNewlineOrEOF()) {
957 MachineMemOperand
*MemOp
= nullptr;
958 if (parseMachineMemoryOperand(MemOp
))
960 MemOperands
.push_back(MemOp
);
961 if (Token
.isNewlineOrEOF())
963 if (Token
.isNot(MIToken::comma
))
964 return error("expected ',' before the next machine memory operand");
969 const auto &MCID
= MF
.getSubtarget().getInstrInfo()->get(OpCode
);
970 if (!MCID
.isVariadic()) {
971 // FIXME: Move the implicit operand verification to the machine verifier.
972 if (verifyImplicitOperands(Operands
, MCID
))
976 // TODO: Check for extraneous machine operands.
977 MI
= MF
.CreateMachineInstr(MCID
, DebugLocation
, /*NoImplicit=*/true);
979 for (const auto &Operand
: Operands
)
980 MI
->addOperand(MF
, Operand
.Operand
);
981 if (assignRegisterTies(*MI
, Operands
))
984 MI
->setPreInstrSymbol(MF
, PreInstrSymbol
);
986 MI
->setPostInstrSymbol(MF
, PostInstrSymbol
);
987 if (!MemOperands
.empty())
988 MI
->setMemRefs(MF
, MemOperands
);
992 bool MIParser::parseStandaloneMBB(MachineBasicBlock
*&MBB
) {
994 if (Token
.isNot(MIToken::MachineBasicBlock
))
995 return error("expected a machine basic block reference");
996 if (parseMBBReference(MBB
))
999 if (Token
.isNot(MIToken::Eof
))
1001 "expected end of string after the machine basic block reference");
1005 bool MIParser::parseStandaloneNamedRegister(unsigned &Reg
) {
1007 if (Token
.isNot(MIToken::NamedRegister
))
1008 return error("expected a named register");
1009 if (parseNamedRegister(Reg
))
1012 if (Token
.isNot(MIToken::Eof
))
1013 return error("expected end of string after the register reference");
1017 bool MIParser::parseStandaloneVirtualRegister(VRegInfo
*&Info
) {
1019 if (Token
.isNot(MIToken::VirtualRegister
))
1020 return error("expected a virtual register");
1021 if (parseVirtualRegister(Info
))
1024 if (Token
.isNot(MIToken::Eof
))
1025 return error("expected end of string after the register reference");
1029 bool MIParser::parseStandaloneRegister(unsigned &Reg
) {
1031 if (Token
.isNot(MIToken::NamedRegister
) &&
1032 Token
.isNot(MIToken::VirtualRegister
))
1033 return error("expected either a named or virtual register");
1036 if (parseRegister(Reg
, Info
))
1040 if (Token
.isNot(MIToken::Eof
))
1041 return error("expected end of string after the register reference");
1045 bool MIParser::parseStandaloneStackObject(int &FI
) {
1047 if (Token
.isNot(MIToken::StackObject
))
1048 return error("expected a stack object");
1049 if (parseStackFrameIndex(FI
))
1051 if (Token
.isNot(MIToken::Eof
))
1052 return error("expected end of string after the stack object reference");
1056 bool MIParser::parseStandaloneMDNode(MDNode
*&Node
) {
1058 if (Token
.is(MIToken::exclaim
)) {
1059 if (parseMDNode(Node
))
1061 } else if (Token
.is(MIToken::md_diexpr
)) {
1062 if (parseDIExpression(Node
))
1064 } else if (Token
.is(MIToken::md_dilocation
)) {
1065 if (parseDILocation(Node
))
1068 return error("expected a metadata node");
1069 if (Token
.isNot(MIToken::Eof
))
1070 return error("expected end of string after the metadata node");
1074 static const char *printImplicitRegisterFlag(const MachineOperand
&MO
) {
1075 assert(MO
.isImplicit());
1076 return MO
.isDef() ? "implicit-def" : "implicit";
1079 static std::string
getRegisterName(const TargetRegisterInfo
*TRI
,
1081 assert(TargetRegisterInfo::isPhysicalRegister(Reg
) && "expected phys reg");
1082 return StringRef(TRI
->getName(Reg
)).lower();
1085 /// Return true if the parsed machine operands contain a given machine operand.
1086 static bool isImplicitOperandIn(const MachineOperand
&ImplicitOperand
,
1087 ArrayRef
<ParsedMachineOperand
> Operands
) {
1088 for (const auto &I
: Operands
) {
1089 if (ImplicitOperand
.isIdenticalTo(I
.Operand
))
1095 bool MIParser::verifyImplicitOperands(ArrayRef
<ParsedMachineOperand
> Operands
,
1096 const MCInstrDesc
&MCID
) {
1098 // We can't verify call instructions as they can contain arbitrary implicit
1099 // register and register mask operands.
1102 // Gather all the expected implicit operands.
1103 SmallVector
<MachineOperand
, 4> ImplicitOperands
;
1104 if (MCID
.ImplicitDefs
)
1105 for (const MCPhysReg
*ImpDefs
= MCID
.getImplicitDefs(); *ImpDefs
; ++ImpDefs
)
1106 ImplicitOperands
.push_back(
1107 MachineOperand::CreateReg(*ImpDefs
, true, true));
1108 if (MCID
.ImplicitUses
)
1109 for (const MCPhysReg
*ImpUses
= MCID
.getImplicitUses(); *ImpUses
; ++ImpUses
)
1110 ImplicitOperands
.push_back(
1111 MachineOperand::CreateReg(*ImpUses
, false, true));
1113 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
1114 assert(TRI
&& "Expected target register info");
1115 for (const auto &I
: ImplicitOperands
) {
1116 if (isImplicitOperandIn(I
, Operands
))
1118 return error(Operands
.empty() ? Token
.location() : Operands
.back().End
,
1119 Twine("missing implicit register operand '") +
1120 printImplicitRegisterFlag(I
) + " $" +
1121 getRegisterName(TRI
, I
.getReg()) + "'");
1126 bool MIParser::parseInstruction(unsigned &OpCode
, unsigned &Flags
) {
1127 // Allow frame and fast math flags for OPCODE
1128 while (Token
.is(MIToken::kw_frame_setup
) ||
1129 Token
.is(MIToken::kw_frame_destroy
) ||
1130 Token
.is(MIToken::kw_nnan
) ||
1131 Token
.is(MIToken::kw_ninf
) ||
1132 Token
.is(MIToken::kw_nsz
) ||
1133 Token
.is(MIToken::kw_arcp
) ||
1134 Token
.is(MIToken::kw_contract
) ||
1135 Token
.is(MIToken::kw_afn
) ||
1136 Token
.is(MIToken::kw_reassoc
) ||
1137 Token
.is(MIToken::kw_nuw
) ||
1138 Token
.is(MIToken::kw_nsw
) ||
1139 Token
.is(MIToken::kw_exact
) ||
1140 Token
.is(MIToken::kw_fpexcept
)) {
1141 // Mine frame and fast math flags
1142 if (Token
.is(MIToken::kw_frame_setup
))
1143 Flags
|= MachineInstr::FrameSetup
;
1144 if (Token
.is(MIToken::kw_frame_destroy
))
1145 Flags
|= MachineInstr::FrameDestroy
;
1146 if (Token
.is(MIToken::kw_nnan
))
1147 Flags
|= MachineInstr::FmNoNans
;
1148 if (Token
.is(MIToken::kw_ninf
))
1149 Flags
|= MachineInstr::FmNoInfs
;
1150 if (Token
.is(MIToken::kw_nsz
))
1151 Flags
|= MachineInstr::FmNsz
;
1152 if (Token
.is(MIToken::kw_arcp
))
1153 Flags
|= MachineInstr::FmArcp
;
1154 if (Token
.is(MIToken::kw_contract
))
1155 Flags
|= MachineInstr::FmContract
;
1156 if (Token
.is(MIToken::kw_afn
))
1157 Flags
|= MachineInstr::FmAfn
;
1158 if (Token
.is(MIToken::kw_reassoc
))
1159 Flags
|= MachineInstr::FmReassoc
;
1160 if (Token
.is(MIToken::kw_nuw
))
1161 Flags
|= MachineInstr::NoUWrap
;
1162 if (Token
.is(MIToken::kw_nsw
))
1163 Flags
|= MachineInstr::NoSWrap
;
1164 if (Token
.is(MIToken::kw_exact
))
1165 Flags
|= MachineInstr::IsExact
;
1166 if (Token
.is(MIToken::kw_fpexcept
))
1167 Flags
|= MachineInstr::FPExcept
;
1171 if (Token
.isNot(MIToken::Identifier
))
1172 return error("expected a machine instruction");
1173 StringRef InstrName
= Token
.stringValue();
1174 if (PFS
.Target
.parseInstrName(InstrName
, OpCode
))
1175 return error(Twine("unknown machine instruction name '") + InstrName
+ "'");
1180 bool MIParser::parseNamedRegister(unsigned &Reg
) {
1181 assert(Token
.is(MIToken::NamedRegister
) && "Needs NamedRegister token");
1182 StringRef Name
= Token
.stringValue();
1183 if (PFS
.Target
.getRegisterByName(Name
, Reg
))
1184 return error(Twine("unknown register name '") + Name
+ "'");
1188 bool MIParser::parseNamedVirtualRegister(VRegInfo
*&Info
) {
1189 assert(Token
.is(MIToken::NamedVirtualRegister
) && "Expected NamedVReg token");
1190 StringRef Name
= Token
.stringValue();
1191 // TODO: Check that the VReg name is not the same as a physical register name.
1192 // If it is, then print a warning (when warnings are implemented).
1193 Info
= &PFS
.getVRegInfoNamed(Name
);
1197 bool MIParser::parseVirtualRegister(VRegInfo
*&Info
) {
1198 if (Token
.is(MIToken::NamedVirtualRegister
))
1199 return parseNamedVirtualRegister(Info
);
1200 assert(Token
.is(MIToken::VirtualRegister
) && "Needs VirtualRegister token");
1202 if (getUnsigned(ID
))
1204 Info
= &PFS
.getVRegInfo(ID
);
1208 bool MIParser::parseRegister(unsigned &Reg
, VRegInfo
*&Info
) {
1209 switch (Token
.kind()) {
1210 case MIToken::underscore
:
1213 case MIToken::NamedRegister
:
1214 return parseNamedRegister(Reg
);
1215 case MIToken::NamedVirtualRegister
:
1216 case MIToken::VirtualRegister
:
1217 if (parseVirtualRegister(Info
))
1221 // TODO: Parse other register kinds.
1223 llvm_unreachable("The current token should be a register");
1227 bool MIParser::parseRegisterClassOrBank(VRegInfo
&RegInfo
) {
1228 if (Token
.isNot(MIToken::Identifier
) && Token
.isNot(MIToken::underscore
))
1229 return error("expected '_', register class, or register bank name");
1230 StringRef::iterator Loc
= Token
.location();
1231 StringRef Name
= Token
.stringValue();
1233 // Was it a register class?
1234 const TargetRegisterClass
*RC
= PFS
.Target
.getRegClass(Name
);
1238 switch (RegInfo
.Kind
) {
1239 case VRegInfo::UNKNOWN
:
1240 case VRegInfo::NORMAL
:
1241 RegInfo
.Kind
= VRegInfo::NORMAL
;
1242 if (RegInfo
.Explicit
&& RegInfo
.D
.RC
!= RC
) {
1243 const TargetRegisterInfo
&TRI
= *MF
.getSubtarget().getRegisterInfo();
1244 return error(Loc
, Twine("conflicting register classes, previously: ") +
1245 Twine(TRI
.getRegClassName(RegInfo
.D
.RC
)));
1248 RegInfo
.Explicit
= true;
1251 case VRegInfo::GENERIC
:
1252 case VRegInfo::REGBANK
:
1253 return error(Loc
, "register class specification on generic register");
1255 llvm_unreachable("Unexpected register kind");
1258 // Should be a register bank or a generic register.
1259 const RegisterBank
*RegBank
= nullptr;
1261 RegBank
= PFS
.Target
.getRegBank(Name
);
1263 return error(Loc
, "expected '_', register class, or register bank name");
1268 switch (RegInfo
.Kind
) {
1269 case VRegInfo::UNKNOWN
:
1270 case VRegInfo::GENERIC
:
1271 case VRegInfo::REGBANK
:
1272 RegInfo
.Kind
= RegBank
? VRegInfo::REGBANK
: VRegInfo::GENERIC
;
1273 if (RegInfo
.Explicit
&& RegInfo
.D
.RegBank
!= RegBank
)
1274 return error(Loc
, "conflicting generic register banks");
1275 RegInfo
.D
.RegBank
= RegBank
;
1276 RegInfo
.Explicit
= true;
1279 case VRegInfo::NORMAL
:
1280 return error(Loc
, "register bank specification on normal register");
1282 llvm_unreachable("Unexpected register kind");
1285 bool MIParser::parseRegisterFlag(unsigned &Flags
) {
1286 const unsigned OldFlags
= Flags
;
1287 switch (Token
.kind()) {
1288 case MIToken::kw_implicit
:
1289 Flags
|= RegState::Implicit
;
1291 case MIToken::kw_implicit_define
:
1292 Flags
|= RegState::ImplicitDefine
;
1294 case MIToken::kw_def
:
1295 Flags
|= RegState::Define
;
1297 case MIToken::kw_dead
:
1298 Flags
|= RegState::Dead
;
1300 case MIToken::kw_killed
:
1301 Flags
|= RegState::Kill
;
1303 case MIToken::kw_undef
:
1304 Flags
|= RegState::Undef
;
1306 case MIToken::kw_internal
:
1307 Flags
|= RegState::InternalRead
;
1309 case MIToken::kw_early_clobber
:
1310 Flags
|= RegState::EarlyClobber
;
1312 case MIToken::kw_debug_use
:
1313 Flags
|= RegState::Debug
;
1315 case MIToken::kw_renamable
:
1316 Flags
|= RegState::Renamable
;
1319 llvm_unreachable("The current token should be a register flag");
1321 if (OldFlags
== Flags
)
1322 // We know that the same flag is specified more than once when the flags
1323 // weren't modified.
1324 return error("duplicate '" + Token
.stringValue() + "' register flag");
1329 bool MIParser::parseSubRegisterIndex(unsigned &SubReg
) {
1330 assert(Token
.is(MIToken::dot
));
1332 if (Token
.isNot(MIToken::Identifier
))
1333 return error("expected a subregister index after '.'");
1334 auto Name
= Token
.stringValue();
1335 SubReg
= PFS
.Target
.getSubRegIndex(Name
);
1337 return error(Twine("use of unknown subregister index '") + Name
+ "'");
1342 bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx
) {
1343 if (!consumeIfPresent(MIToken::kw_tied_def
))
1345 if (Token
.isNot(MIToken::IntegerLiteral
))
1346 return error("expected an integer literal after 'tied-def'");
1347 if (getUnsigned(TiedDefIdx
))
1350 if (expectAndConsume(MIToken::rparen
))
1355 bool MIParser::assignRegisterTies(MachineInstr
&MI
,
1356 ArrayRef
<ParsedMachineOperand
> Operands
) {
1357 SmallVector
<std::pair
<unsigned, unsigned>, 4> TiedRegisterPairs
;
1358 for (unsigned I
= 0, E
= Operands
.size(); I
!= E
; ++I
) {
1359 if (!Operands
[I
].TiedDefIdx
)
1361 // The parser ensures that this operand is a register use, so we just have
1362 // to check the tied-def operand.
1363 unsigned DefIdx
= Operands
[I
].TiedDefIdx
.getValue();
1365 return error(Operands
[I
].Begin
,
1366 Twine("use of invalid tied-def operand index '" +
1367 Twine(DefIdx
) + "'; instruction has only ") +
1368 Twine(E
) + " operands");
1369 const auto &DefOperand
= Operands
[DefIdx
].Operand
;
1370 if (!DefOperand
.isReg() || !DefOperand
.isDef())
1371 // FIXME: add note with the def operand.
1372 return error(Operands
[I
].Begin
,
1373 Twine("use of invalid tied-def operand index '") +
1374 Twine(DefIdx
) + "'; the operand #" + Twine(DefIdx
) +
1375 " isn't a defined register");
1376 // Check that the tied-def operand wasn't tied elsewhere.
1377 for (const auto &TiedPair
: TiedRegisterPairs
) {
1378 if (TiedPair
.first
== DefIdx
)
1379 return error(Operands
[I
].Begin
,
1380 Twine("the tied-def operand #") + Twine(DefIdx
) +
1381 " is already tied with another register operand");
1383 TiedRegisterPairs
.push_back(std::make_pair(DefIdx
, I
));
1385 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1386 // indices must be less than tied max.
1387 for (const auto &TiedPair
: TiedRegisterPairs
)
1388 MI
.tieOperands(TiedPair
.first
, TiedPair
.second
);
1392 bool MIParser::parseRegisterOperand(MachineOperand
&Dest
,
1393 Optional
<unsigned> &TiedDefIdx
,
1395 unsigned Flags
= IsDef
? RegState::Define
: 0;
1396 while (Token
.isRegisterFlag()) {
1397 if (parseRegisterFlag(Flags
))
1400 if (!Token
.isRegister())
1401 return error("expected a register after register flags");
1404 if (parseRegister(Reg
, RegInfo
))
1407 unsigned SubReg
= 0;
1408 if (Token
.is(MIToken::dot
)) {
1409 if (parseSubRegisterIndex(SubReg
))
1411 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1412 return error("subregister index expects a virtual register");
1414 if (Token
.is(MIToken::colon
)) {
1415 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1416 return error("register class specification expects a virtual register");
1418 if (parseRegisterClassOrBank(*RegInfo
))
1421 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1422 if ((Flags
& RegState::Define
) == 0) {
1423 if (consumeIfPresent(MIToken::lparen
)) {
1425 if (!parseRegisterTiedDefIndex(Idx
))
1428 // Try a redundant low-level type.
1430 if (parseLowLevelType(Token
.location(), Ty
))
1431 return error("expected tied-def or low-level type after '('");
1433 if (expectAndConsume(MIToken::rparen
))
1436 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1437 return error("inconsistent type for generic virtual register");
1439 MRI
.setType(Reg
, Ty
);
1442 } else if (consumeIfPresent(MIToken::lparen
)) {
1443 // Virtual registers may have a tpe with GlobalISel.
1444 if (!TargetRegisterInfo::isVirtualRegister(Reg
))
1445 return error("unexpected type on physical register");
1448 if (parseLowLevelType(Token
.location(), Ty
))
1451 if (expectAndConsume(MIToken::rparen
))
1454 if (MRI
.getType(Reg
).isValid() && MRI
.getType(Reg
) != Ty
)
1455 return error("inconsistent type for generic virtual register");
1457 MRI
.setType(Reg
, Ty
);
1458 } else if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
1459 // Generic virtual registers must have a type.
1460 // If we end up here this means the type hasn't been specified and
1462 if (RegInfo
->Kind
== VRegInfo::GENERIC
||
1463 RegInfo
->Kind
== VRegInfo::REGBANK
)
1464 return error("generic virtual registers must have a type");
1466 Dest
= MachineOperand::CreateReg(
1467 Reg
, Flags
& RegState::Define
, Flags
& RegState::Implicit
,
1468 Flags
& RegState::Kill
, Flags
& RegState::Dead
, Flags
& RegState::Undef
,
1469 Flags
& RegState::EarlyClobber
, SubReg
, Flags
& RegState::Debug
,
1470 Flags
& RegState::InternalRead
, Flags
& RegState::Renamable
);
1475 bool MIParser::parseImmediateOperand(MachineOperand
&Dest
) {
1476 assert(Token
.is(MIToken::IntegerLiteral
));
1477 const APSInt
&Int
= Token
.integerValue();
1478 if (Int
.getMinSignedBits() > 64)
1479 return error("integer literal is too large to be an immediate operand");
1480 Dest
= MachineOperand::CreateImm(Int
.getExtValue());
1485 bool MIParser::parseIRConstant(StringRef::iterator Loc
, StringRef StringValue
,
1486 const Constant
*&C
) {
1487 auto Source
= StringValue
.str(); // The source has to be null terminated.
1489 C
= parseConstantValue(Source
, Err
, *MF
.getFunction().getParent(),
1492 return error(Loc
+ Err
.getColumnNo(), Err
.getMessage());
1496 bool MIParser::parseIRConstant(StringRef::iterator Loc
, const Constant
*&C
) {
1497 if (parseIRConstant(Loc
, StringRef(Loc
, Token
.range().end() - Loc
), C
))
1503 // See LLT implemntation for bit size limits.
1504 static bool verifyScalarSize(uint64_t Size
) {
1505 return Size
!= 0 && isUInt
<16>(Size
);
1508 static bool verifyVectorElementCount(uint64_t NumElts
) {
1509 return NumElts
!= 0 && isUInt
<16>(NumElts
);
1512 static bool verifyAddrSpace(uint64_t AddrSpace
) {
1513 return isUInt
<24>(AddrSpace
);
1516 bool MIParser::parseLowLevelType(StringRef::iterator Loc
, LLT
&Ty
) {
1517 if (Token
.range().front() == 's' || Token
.range().front() == 'p') {
1518 StringRef SizeStr
= Token
.range().drop_front();
1519 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1520 return error("expected integers after 's'/'p' type character");
1523 if (Token
.range().front() == 's') {
1524 auto ScalarSize
= APSInt(Token
.range().drop_front()).getZExtValue();
1525 if (!verifyScalarSize(ScalarSize
))
1526 return error("invalid size for scalar type");
1528 Ty
= LLT::scalar(ScalarSize
);
1531 } else if (Token
.range().front() == 'p') {
1532 const DataLayout
&DL
= MF
.getDataLayout();
1533 uint64_t AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1534 if (!verifyAddrSpace(AS
))
1535 return error("invalid address space number");
1537 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1542 // Now we're looking for a vector.
1543 if (Token
.isNot(MIToken::less
))
1545 "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1548 if (Token
.isNot(MIToken::IntegerLiteral
))
1549 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1550 uint64_t NumElements
= Token
.integerValue().getZExtValue();
1551 if (!verifyVectorElementCount(NumElements
))
1552 return error("invalid number of vector elements");
1556 if (Token
.isNot(MIToken::Identifier
) || Token
.stringValue() != "x")
1557 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1560 if (Token
.range().front() != 's' && Token
.range().front() != 'p')
1561 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1562 StringRef SizeStr
= Token
.range().drop_front();
1563 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1564 return error("expected integers after 's'/'p' type character");
1566 if (Token
.range().front() == 's') {
1567 auto ScalarSize
= APSInt(Token
.range().drop_front()).getZExtValue();
1568 if (!verifyScalarSize(ScalarSize
))
1569 return error("invalid size for scalar type");
1570 Ty
= LLT::scalar(ScalarSize
);
1571 } else if (Token
.range().front() == 'p') {
1572 const DataLayout
&DL
= MF
.getDataLayout();
1573 uint64_t AS
= APSInt(Token
.range().drop_front()).getZExtValue();
1574 if (!verifyAddrSpace(AS
))
1575 return error("invalid address space number");
1577 Ty
= LLT::pointer(AS
, DL
.getPointerSizeInBits(AS
));
1579 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1582 if (Token
.isNot(MIToken::greater
))
1583 return error(Loc
, "expected <M x sN> or <M x pA> for vector type");
1586 Ty
= LLT::vector(NumElements
, Ty
);
1590 bool MIParser::parseTypedImmediateOperand(MachineOperand
&Dest
) {
1591 assert(Token
.is(MIToken::Identifier
));
1592 StringRef TypeStr
= Token
.range();
1593 if (TypeStr
.front() != 'i' && TypeStr
.front() != 's' &&
1594 TypeStr
.front() != 'p')
1596 "a typed immediate operand should start with one of 'i', 's', or 'p'");
1597 StringRef SizeStr
= Token
.range().drop_front();
1598 if (SizeStr
.size() == 0 || !llvm::all_of(SizeStr
, isdigit
))
1599 return error("expected integers after 'i'/'s'/'p' type character");
1601 auto Loc
= Token
.location();
1603 if (Token
.isNot(MIToken::IntegerLiteral
)) {
1604 if (Token
.isNot(MIToken::Identifier
) ||
1605 !(Token
.range() == "true" || Token
.range() == "false"))
1606 return error("expected an integer literal");
1608 const Constant
*C
= nullptr;
1609 if (parseIRConstant(Loc
, C
))
1611 Dest
= MachineOperand::CreateCImm(cast
<ConstantInt
>(C
));
1615 bool MIParser::parseFPImmediateOperand(MachineOperand
&Dest
) {
1616 auto Loc
= Token
.location();
1618 if (Token
.isNot(MIToken::FloatingPointLiteral
) &&
1619 Token
.isNot(MIToken::HexLiteral
))
1620 return error("expected a floating point literal");
1621 const Constant
*C
= nullptr;
1622 if (parseIRConstant(Loc
, C
))
1624 Dest
= MachineOperand::CreateFPImm(cast
<ConstantFP
>(C
));
1628 bool MIParser::getUnsigned(unsigned &Result
) {
1629 if (Token
.hasIntegerValue()) {
1630 const uint64_t Limit
= uint64_t(std::numeric_limits
<unsigned>::max()) + 1;
1631 uint64_t Val64
= Token
.integerValue().getLimitedValue(Limit
);
1633 return error("expected 32-bit integer (too large)");
1637 if (Token
.is(MIToken::HexLiteral
)) {
1641 if (A
.getBitWidth() > 32)
1642 return error("expected 32-bit integer (too large)");
1643 Result
= A
.getZExtValue();
1649 bool MIParser::parseMBBReference(MachineBasicBlock
*&MBB
) {
1650 assert(Token
.is(MIToken::MachineBasicBlock
) ||
1651 Token
.is(MIToken::MachineBasicBlockLabel
));
1653 if (getUnsigned(Number
))
1655 auto MBBInfo
= PFS
.MBBSlots
.find(Number
);
1656 if (MBBInfo
== PFS
.MBBSlots
.end())
1657 return error(Twine("use of undefined machine basic block #") +
1659 MBB
= MBBInfo
->second
;
1660 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1661 // we drop the <irname> from the bb.<id>.<irname> format.
1662 if (!Token
.stringValue().empty() && Token
.stringValue() != MBB
->getName())
1663 return error(Twine("the name of machine basic block #") + Twine(Number
) +
1664 " isn't '" + Token
.stringValue() + "'");
1668 bool MIParser::parseMBBOperand(MachineOperand
&Dest
) {
1669 MachineBasicBlock
*MBB
;
1670 if (parseMBBReference(MBB
))
1672 Dest
= MachineOperand::CreateMBB(MBB
);
1677 bool MIParser::parseStackFrameIndex(int &FI
) {
1678 assert(Token
.is(MIToken::StackObject
));
1680 if (getUnsigned(ID
))
1682 auto ObjectInfo
= PFS
.StackObjectSlots
.find(ID
);
1683 if (ObjectInfo
== PFS
.StackObjectSlots
.end())
1684 return error(Twine("use of undefined stack object '%stack.") + Twine(ID
) +
1687 if (const auto *Alloca
=
1688 MF
.getFrameInfo().getObjectAllocation(ObjectInfo
->second
))
1689 Name
= Alloca
->getName();
1690 if (!Token
.stringValue().empty() && Token
.stringValue() != Name
)
1691 return error(Twine("the name of the stack object '%stack.") + Twine(ID
) +
1692 "' isn't '" + Token
.stringValue() + "'");
1694 FI
= ObjectInfo
->second
;
1698 bool MIParser::parseStackObjectOperand(MachineOperand
&Dest
) {
1700 if (parseStackFrameIndex(FI
))
1702 Dest
= MachineOperand::CreateFI(FI
);
1706 bool MIParser::parseFixedStackFrameIndex(int &FI
) {
1707 assert(Token
.is(MIToken::FixedStackObject
));
1709 if (getUnsigned(ID
))
1711 auto ObjectInfo
= PFS
.FixedStackObjectSlots
.find(ID
);
1712 if (ObjectInfo
== PFS
.FixedStackObjectSlots
.end())
1713 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1716 FI
= ObjectInfo
->second
;
1720 bool MIParser::parseFixedStackObjectOperand(MachineOperand
&Dest
) {
1722 if (parseFixedStackFrameIndex(FI
))
1724 Dest
= MachineOperand::CreateFI(FI
);
1728 bool MIParser::parseGlobalValue(GlobalValue
*&GV
) {
1729 switch (Token
.kind()) {
1730 case MIToken::NamedGlobalValue
: {
1731 const Module
*M
= MF
.getFunction().getParent();
1732 GV
= M
->getNamedValue(Token
.stringValue());
1734 return error(Twine("use of undefined global value '") + Token
.range() +
1738 case MIToken::GlobalValue
: {
1740 if (getUnsigned(GVIdx
))
1742 if (GVIdx
>= PFS
.IRSlots
.GlobalValues
.size())
1743 return error(Twine("use of undefined global value '@") + Twine(GVIdx
) +
1745 GV
= PFS
.IRSlots
.GlobalValues
[GVIdx
];
1749 llvm_unreachable("The current token should be a global value");
1754 bool MIParser::parseGlobalAddressOperand(MachineOperand
&Dest
) {
1755 GlobalValue
*GV
= nullptr;
1756 if (parseGlobalValue(GV
))
1759 Dest
= MachineOperand::CreateGA(GV
, /*Offset=*/0);
1760 if (parseOperandsOffset(Dest
))
1765 bool MIParser::parseConstantPoolIndexOperand(MachineOperand
&Dest
) {
1766 assert(Token
.is(MIToken::ConstantPoolItem
));
1768 if (getUnsigned(ID
))
1770 auto ConstantInfo
= PFS
.ConstantPoolSlots
.find(ID
);
1771 if (ConstantInfo
== PFS
.ConstantPoolSlots
.end())
1772 return error("use of undefined constant '%const." + Twine(ID
) + "'");
1774 Dest
= MachineOperand::CreateCPI(ID
, /*Offset=*/0);
1775 if (parseOperandsOffset(Dest
))
1780 bool MIParser::parseJumpTableIndexOperand(MachineOperand
&Dest
) {
1781 assert(Token
.is(MIToken::JumpTableIndex
));
1783 if (getUnsigned(ID
))
1785 auto JumpTableEntryInfo
= PFS
.JumpTableSlots
.find(ID
);
1786 if (JumpTableEntryInfo
== PFS
.JumpTableSlots
.end())
1787 return error("use of undefined jump table '%jump-table." + Twine(ID
) + "'");
1789 Dest
= MachineOperand::CreateJTI(JumpTableEntryInfo
->second
);
1793 bool MIParser::parseExternalSymbolOperand(MachineOperand
&Dest
) {
1794 assert(Token
.is(MIToken::ExternalSymbol
));
1795 const char *Symbol
= MF
.createExternalSymbolName(Token
.stringValue());
1797 Dest
= MachineOperand::CreateES(Symbol
);
1798 if (parseOperandsOffset(Dest
))
1803 bool MIParser::parseMCSymbolOperand(MachineOperand
&Dest
) {
1804 assert(Token
.is(MIToken::MCSymbol
));
1805 MCSymbol
*Symbol
= getOrCreateMCSymbol(Token
.stringValue());
1807 Dest
= MachineOperand::CreateMCSymbol(Symbol
);
1808 if (parseOperandsOffset(Dest
))
1813 bool MIParser::parseSubRegisterIndexOperand(MachineOperand
&Dest
) {
1814 assert(Token
.is(MIToken::SubRegisterIndex
));
1815 StringRef Name
= Token
.stringValue();
1816 unsigned SubRegIndex
= PFS
.Target
.getSubRegIndex(Token
.stringValue());
1817 if (SubRegIndex
== 0)
1818 return error(Twine("unknown subregister index '") + Name
+ "'");
1820 Dest
= MachineOperand::CreateImm(SubRegIndex
);
1824 bool MIParser::parseMDNode(MDNode
*&Node
) {
1825 assert(Token
.is(MIToken::exclaim
));
1827 auto Loc
= Token
.location();
1829 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
1830 return error("expected metadata id after '!'");
1832 if (getUnsigned(ID
))
1834 auto NodeInfo
= PFS
.IRSlots
.MetadataNodes
.find(ID
);
1835 if (NodeInfo
== PFS
.IRSlots
.MetadataNodes
.end())
1836 return error(Loc
, "use of undefined metadata '!" + Twine(ID
) + "'");
1838 Node
= NodeInfo
->second
.get();
1842 bool MIParser::parseDIExpression(MDNode
*&Expr
) {
1843 assert(Token
.is(MIToken::md_diexpr
));
1846 // FIXME: Share this parsing with the IL parser.
1847 SmallVector
<uint64_t, 8> Elements
;
1849 if (expectAndConsume(MIToken::lparen
))
1852 if (Token
.isNot(MIToken::rparen
)) {
1854 if (Token
.is(MIToken::Identifier
)) {
1855 if (unsigned Op
= dwarf::getOperationEncoding(Token
.stringValue())) {
1857 Elements
.push_back(Op
);
1860 if (unsigned Enc
= dwarf::getAttributeEncoding(Token
.stringValue())) {
1862 Elements
.push_back(Enc
);
1865 return error(Twine("invalid DWARF op '") + Token
.stringValue() + "'");
1868 if (Token
.isNot(MIToken::IntegerLiteral
) ||
1869 Token
.integerValue().isSigned())
1870 return error("expected unsigned integer");
1872 auto &U
= Token
.integerValue();
1873 if (U
.ugt(UINT64_MAX
))
1874 return error("element too large, limit is " + Twine(UINT64_MAX
));
1875 Elements
.push_back(U
.getZExtValue());
1878 } while (consumeIfPresent(MIToken::comma
));
1881 if (expectAndConsume(MIToken::rparen
))
1884 Expr
= DIExpression::get(MF
.getFunction().getContext(), Elements
);
1888 bool MIParser::parseDILocation(MDNode
*&Loc
) {
1889 assert(Token
.is(MIToken::md_dilocation
));
1892 bool HaveLine
= false;
1894 unsigned Column
= 0;
1895 MDNode
*Scope
= nullptr;
1896 MDNode
*InlinedAt
= nullptr;
1897 bool ImplicitCode
= false;
1899 if (expectAndConsume(MIToken::lparen
))
1902 if (Token
.isNot(MIToken::rparen
)) {
1904 if (Token
.is(MIToken::Identifier
)) {
1905 if (Token
.stringValue() == "line") {
1907 if (expectAndConsume(MIToken::colon
))
1909 if (Token
.isNot(MIToken::IntegerLiteral
) ||
1910 Token
.integerValue().isSigned())
1911 return error("expected unsigned integer");
1912 Line
= Token
.integerValue().getZExtValue();
1917 if (Token
.stringValue() == "column") {
1919 if (expectAndConsume(MIToken::colon
))
1921 if (Token
.isNot(MIToken::IntegerLiteral
) ||
1922 Token
.integerValue().isSigned())
1923 return error("expected unsigned integer");
1924 Column
= Token
.integerValue().getZExtValue();
1928 if (Token
.stringValue() == "scope") {
1930 if (expectAndConsume(MIToken::colon
))
1932 if (parseMDNode(Scope
))
1933 return error("expected metadata node");
1934 if (!isa
<DIScope
>(Scope
))
1935 return error("expected DIScope node");
1938 if (Token
.stringValue() == "inlinedAt") {
1940 if (expectAndConsume(MIToken::colon
))
1942 if (Token
.is(MIToken::exclaim
)) {
1943 if (parseMDNode(InlinedAt
))
1945 } else if (Token
.is(MIToken::md_dilocation
)) {
1946 if (parseDILocation(InlinedAt
))
1949 return error("expected metadata node");
1950 if (!isa
<DILocation
>(InlinedAt
))
1951 return error("expected DILocation node");
1954 if (Token
.stringValue() == "isImplicitCode") {
1956 if (expectAndConsume(MIToken::colon
))
1958 if (!Token
.is(MIToken::Identifier
))
1959 return error("expected true/false");
1960 // As far as I can see, we don't have any existing need for parsing
1961 // true/false in MIR yet. Do it ad-hoc until there's something else
1963 if (Token
.stringValue() == "true")
1964 ImplicitCode
= true;
1965 else if (Token
.stringValue() == "false")
1966 ImplicitCode
= false;
1968 return error("expected true/false");
1973 return error(Twine("invalid DILocation argument '") +
1974 Token
.stringValue() + "'");
1975 } while (consumeIfPresent(MIToken::comma
));
1978 if (expectAndConsume(MIToken::rparen
))
1982 return error("DILocation requires line number");
1984 return error("DILocation requires a scope");
1986 Loc
= DILocation::get(MF
.getFunction().getContext(), Line
, Column
, Scope
,
1987 InlinedAt
, ImplicitCode
);
1991 bool MIParser::parseMetadataOperand(MachineOperand
&Dest
) {
1992 MDNode
*Node
= nullptr;
1993 if (Token
.is(MIToken::exclaim
)) {
1994 if (parseMDNode(Node
))
1996 } else if (Token
.is(MIToken::md_diexpr
)) {
1997 if (parseDIExpression(Node
))
2000 Dest
= MachineOperand::CreateMetadata(Node
);
2004 bool MIParser::parseCFIOffset(int &Offset
) {
2005 if (Token
.isNot(MIToken::IntegerLiteral
))
2006 return error("expected a cfi offset");
2007 if (Token
.integerValue().getMinSignedBits() > 32)
2008 return error("expected a 32 bit integer (the cfi offset is too large)");
2009 Offset
= (int)Token
.integerValue().getExtValue();
2014 bool MIParser::parseCFIRegister(unsigned &Reg
) {
2015 if (Token
.isNot(MIToken::NamedRegister
))
2016 return error("expected a cfi register");
2018 if (parseNamedRegister(LLVMReg
))
2020 const auto *TRI
= MF
.getSubtarget().getRegisterInfo();
2021 assert(TRI
&& "Expected target register info");
2022 int DwarfReg
= TRI
->getDwarfRegNum(LLVMReg
, true);
2024 return error("invalid DWARF register");
2025 Reg
= (unsigned)DwarfReg
;
2030 bool MIParser::parseCFIEscapeValues(std::string
&Values
) {
2032 if (Token
.isNot(MIToken::HexLiteral
))
2033 return error("expected a hexadecimal literal");
2035 if (getUnsigned(Value
))
2037 if (Value
> UINT8_MAX
)
2038 return error("expected a 8-bit integer (too large)");
2039 Values
.push_back(static_cast<uint8_t>(Value
));
2041 } while (consumeIfPresent(MIToken::comma
));
2045 bool MIParser::parseCFIOperand(MachineOperand
&Dest
) {
2046 auto Kind
= Token
.kind();
2052 case MIToken::kw_cfi_same_value
:
2053 if (parseCFIRegister(Reg
))
2055 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg
));
2057 case MIToken::kw_cfi_offset
:
2058 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2059 parseCFIOffset(Offset
))
2062 MF
.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg
, Offset
));
2064 case MIToken::kw_cfi_rel_offset
:
2065 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2066 parseCFIOffset(Offset
))
2068 CFIIndex
= MF
.addFrameInst(
2069 MCCFIInstruction::createRelOffset(nullptr, Reg
, Offset
));
2071 case MIToken::kw_cfi_def_cfa_register
:
2072 if (parseCFIRegister(Reg
))
2075 MF
.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg
));
2077 case MIToken::kw_cfi_def_cfa_offset
:
2078 if (parseCFIOffset(Offset
))
2080 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
2081 CFIIndex
= MF
.addFrameInst(
2082 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset
));
2084 case MIToken::kw_cfi_adjust_cfa_offset
:
2085 if (parseCFIOffset(Offset
))
2087 CFIIndex
= MF
.addFrameInst(
2088 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset
));
2090 case MIToken::kw_cfi_def_cfa
:
2091 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2092 parseCFIOffset(Offset
))
2094 // NB: MCCFIInstruction::createDefCfa negates the offset.
2096 MF
.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg
, -Offset
));
2098 case MIToken::kw_cfi_remember_state
:
2099 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2101 case MIToken::kw_cfi_restore
:
2102 if (parseCFIRegister(Reg
))
2104 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg
));
2106 case MIToken::kw_cfi_restore_state
:
2107 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2109 case MIToken::kw_cfi_undefined
:
2110 if (parseCFIRegister(Reg
))
2112 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg
));
2114 case MIToken::kw_cfi_register
: {
2116 if (parseCFIRegister(Reg
) || expectAndConsume(MIToken::comma
) ||
2117 parseCFIRegister(Reg2
))
2121 MF
.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg
, Reg2
));
2124 case MIToken::kw_cfi_window_save
:
2125 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2127 case MIToken::kw_cfi_aarch64_negate_ra_sign_state
:
2128 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2130 case MIToken::kw_cfi_escape
: {
2132 if (parseCFIEscapeValues(Values
))
2134 CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values
));
2138 // TODO: Parse the other CFI operands.
2139 llvm_unreachable("The current token should be a cfi operand");
2141 Dest
= MachineOperand::CreateCFIIndex(CFIIndex
);
2145 bool MIParser::parseIRBlock(BasicBlock
*&BB
, const Function
&F
) {
2146 switch (Token
.kind()) {
2147 case MIToken::NamedIRBlock
: {
2148 BB
= dyn_cast_or_null
<BasicBlock
>(
2149 F
.getValueSymbolTable()->lookup(Token
.stringValue()));
2151 return error(Twine("use of undefined IR block '") + Token
.range() + "'");
2154 case MIToken::IRBlock
: {
2155 unsigned SlotNumber
= 0;
2156 if (getUnsigned(SlotNumber
))
2158 BB
= const_cast<BasicBlock
*>(getIRBlock(SlotNumber
, F
));
2160 return error(Twine("use of undefined IR block '%ir-block.") +
2161 Twine(SlotNumber
) + "'");
2165 llvm_unreachable("The current token should be an IR block reference");
2170 bool MIParser::parseBlockAddressOperand(MachineOperand
&Dest
) {
2171 assert(Token
.is(MIToken::kw_blockaddress
));
2173 if (expectAndConsume(MIToken::lparen
))
2175 if (Token
.isNot(MIToken::GlobalValue
) &&
2176 Token
.isNot(MIToken::NamedGlobalValue
))
2177 return error("expected a global value");
2178 GlobalValue
*GV
= nullptr;
2179 if (parseGlobalValue(GV
))
2181 auto *F
= dyn_cast
<Function
>(GV
);
2183 return error("expected an IR function reference");
2185 if (expectAndConsume(MIToken::comma
))
2187 BasicBlock
*BB
= nullptr;
2188 if (Token
.isNot(MIToken::IRBlock
) && Token
.isNot(MIToken::NamedIRBlock
))
2189 return error("expected an IR block reference");
2190 if (parseIRBlock(BB
, *F
))
2193 if (expectAndConsume(MIToken::rparen
))
2195 Dest
= MachineOperand::CreateBA(BlockAddress::get(F
, BB
), /*Offset=*/0);
2196 if (parseOperandsOffset(Dest
))
2201 bool MIParser::parseIntrinsicOperand(MachineOperand
&Dest
) {
2202 assert(Token
.is(MIToken::kw_intrinsic
));
2204 if (expectAndConsume(MIToken::lparen
))
2205 return error("expected syntax intrinsic(@llvm.whatever)");
2207 if (Token
.isNot(MIToken::NamedGlobalValue
))
2208 return error("expected syntax intrinsic(@llvm.whatever)");
2210 std::string Name
= Token
.stringValue();
2213 if (expectAndConsume(MIToken::rparen
))
2214 return error("expected ')' to terminate intrinsic name");
2216 // Find out what intrinsic we're dealing with, first try the global namespace
2217 // and then the target's private intrinsics if that fails.
2218 const TargetIntrinsicInfo
*TII
= MF
.getTarget().getIntrinsicInfo();
2219 Intrinsic::ID ID
= Function::lookupIntrinsicID(Name
);
2220 if (ID
== Intrinsic::not_intrinsic
&& TII
)
2221 ID
= static_cast<Intrinsic::ID
>(TII
->lookupName(Name
));
2223 if (ID
== Intrinsic::not_intrinsic
)
2224 return error("unknown intrinsic name");
2225 Dest
= MachineOperand::CreateIntrinsicID(ID
);
2230 bool MIParser::parsePredicateOperand(MachineOperand
&Dest
) {
2231 assert(Token
.is(MIToken::kw_intpred
) || Token
.is(MIToken::kw_floatpred
));
2232 bool IsFloat
= Token
.is(MIToken::kw_floatpred
);
2235 if (expectAndConsume(MIToken::lparen
))
2236 return error("expected syntax intpred(whatever) or floatpred(whatever");
2238 if (Token
.isNot(MIToken::Identifier
))
2239 return error("whatever");
2241 CmpInst::Predicate Pred
;
2243 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
2244 .Case("false", CmpInst::FCMP_FALSE
)
2245 .Case("oeq", CmpInst::FCMP_OEQ
)
2246 .Case("ogt", CmpInst::FCMP_OGT
)
2247 .Case("oge", CmpInst::FCMP_OGE
)
2248 .Case("olt", CmpInst::FCMP_OLT
)
2249 .Case("ole", CmpInst::FCMP_OLE
)
2250 .Case("one", CmpInst::FCMP_ONE
)
2251 .Case("ord", CmpInst::FCMP_ORD
)
2252 .Case("uno", CmpInst::FCMP_UNO
)
2253 .Case("ueq", CmpInst::FCMP_UEQ
)
2254 .Case("ugt", CmpInst::FCMP_UGT
)
2255 .Case("uge", CmpInst::FCMP_UGE
)
2256 .Case("ult", CmpInst::FCMP_ULT
)
2257 .Case("ule", CmpInst::FCMP_ULE
)
2258 .Case("une", CmpInst::FCMP_UNE
)
2259 .Case("true", CmpInst::FCMP_TRUE
)
2260 .Default(CmpInst::BAD_FCMP_PREDICATE
);
2261 if (!CmpInst::isFPPredicate(Pred
))
2262 return error("invalid floating-point predicate");
2264 Pred
= StringSwitch
<CmpInst::Predicate
>(Token
.stringValue())
2265 .Case("eq", CmpInst::ICMP_EQ
)
2266 .Case("ne", CmpInst::ICMP_NE
)
2267 .Case("sgt", CmpInst::ICMP_SGT
)
2268 .Case("sge", CmpInst::ICMP_SGE
)
2269 .Case("slt", CmpInst::ICMP_SLT
)
2270 .Case("sle", CmpInst::ICMP_SLE
)
2271 .Case("ugt", CmpInst::ICMP_UGT
)
2272 .Case("uge", CmpInst::ICMP_UGE
)
2273 .Case("ult", CmpInst::ICMP_ULT
)
2274 .Case("ule", CmpInst::ICMP_ULE
)
2275 .Default(CmpInst::BAD_ICMP_PREDICATE
);
2276 if (!CmpInst::isIntPredicate(Pred
))
2277 return error("invalid integer predicate");
2281 Dest
= MachineOperand::CreatePredicate(Pred
);
2282 if (expectAndConsume(MIToken::rparen
))
2283 return error("predicate should be terminated by ')'.");
2288 bool MIParser::parseTargetIndexOperand(MachineOperand
&Dest
) {
2289 assert(Token
.is(MIToken::kw_target_index
));
2291 if (expectAndConsume(MIToken::lparen
))
2293 if (Token
.isNot(MIToken::Identifier
))
2294 return error("expected the name of the target index");
2296 if (PFS
.Target
.getTargetIndex(Token
.stringValue(), Index
))
2297 return error("use of undefined target index '" + Token
.stringValue() + "'");
2299 if (expectAndConsume(MIToken::rparen
))
2301 Dest
= MachineOperand::CreateTargetIndex(unsigned(Index
), /*Offset=*/0);
2302 if (parseOperandsOffset(Dest
))
2307 bool MIParser::parseCustomRegisterMaskOperand(MachineOperand
&Dest
) {
2308 assert(Token
.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2310 if (expectAndConsume(MIToken::lparen
))
2313 uint32_t *Mask
= MF
.allocateRegMask();
2315 if (Token
.isNot(MIToken::NamedRegister
))
2316 return error("expected a named register");
2318 if (parseNamedRegister(Reg
))
2321 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2322 // TODO: Report an error if the same register is used more than once.
2323 if (Token
.isNot(MIToken::comma
))
2328 if (expectAndConsume(MIToken::rparen
))
2330 Dest
= MachineOperand::CreateRegMask(Mask
);
2334 bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand
&Dest
) {
2335 assert(Token
.is(MIToken::kw_liveout
));
2336 uint32_t *Mask
= MF
.allocateRegMask();
2338 if (expectAndConsume(MIToken::lparen
))
2341 if (Token
.isNot(MIToken::NamedRegister
))
2342 return error("expected a named register");
2344 if (parseNamedRegister(Reg
))
2347 Mask
[Reg
/ 32] |= 1U << (Reg
% 32);
2348 // TODO: Report an error if the same register is used more than once.
2349 if (Token
.isNot(MIToken::comma
))
2353 if (expectAndConsume(MIToken::rparen
))
2355 Dest
= MachineOperand::CreateRegLiveOut(Mask
);
2359 bool MIParser::parseMachineOperand(MachineOperand
&Dest
,
2360 Optional
<unsigned> &TiedDefIdx
) {
2361 switch (Token
.kind()) {
2362 case MIToken::kw_implicit
:
2363 case MIToken::kw_implicit_define
:
2364 case MIToken::kw_def
:
2365 case MIToken::kw_dead
:
2366 case MIToken::kw_killed
:
2367 case MIToken::kw_undef
:
2368 case MIToken::kw_internal
:
2369 case MIToken::kw_early_clobber
:
2370 case MIToken::kw_debug_use
:
2371 case MIToken::kw_renamable
:
2372 case MIToken::underscore
:
2373 case MIToken::NamedRegister
:
2374 case MIToken::VirtualRegister
:
2375 case MIToken::NamedVirtualRegister
:
2376 return parseRegisterOperand(Dest
, TiedDefIdx
);
2377 case MIToken::IntegerLiteral
:
2378 return parseImmediateOperand(Dest
);
2379 case MIToken::kw_half
:
2380 case MIToken::kw_float
:
2381 case MIToken::kw_double
:
2382 case MIToken::kw_x86_fp80
:
2383 case MIToken::kw_fp128
:
2384 case MIToken::kw_ppc_fp128
:
2385 return parseFPImmediateOperand(Dest
);
2386 case MIToken::MachineBasicBlock
:
2387 return parseMBBOperand(Dest
);
2388 case MIToken::StackObject
:
2389 return parseStackObjectOperand(Dest
);
2390 case MIToken::FixedStackObject
:
2391 return parseFixedStackObjectOperand(Dest
);
2392 case MIToken::GlobalValue
:
2393 case MIToken::NamedGlobalValue
:
2394 return parseGlobalAddressOperand(Dest
);
2395 case MIToken::ConstantPoolItem
:
2396 return parseConstantPoolIndexOperand(Dest
);
2397 case MIToken::JumpTableIndex
:
2398 return parseJumpTableIndexOperand(Dest
);
2399 case MIToken::ExternalSymbol
:
2400 return parseExternalSymbolOperand(Dest
);
2401 case MIToken::MCSymbol
:
2402 return parseMCSymbolOperand(Dest
);
2403 case MIToken::SubRegisterIndex
:
2404 return parseSubRegisterIndexOperand(Dest
);
2405 case MIToken::md_diexpr
:
2406 case MIToken::exclaim
:
2407 return parseMetadataOperand(Dest
);
2408 case MIToken::kw_cfi_same_value
:
2409 case MIToken::kw_cfi_offset
:
2410 case MIToken::kw_cfi_rel_offset
:
2411 case MIToken::kw_cfi_def_cfa_register
:
2412 case MIToken::kw_cfi_def_cfa_offset
:
2413 case MIToken::kw_cfi_adjust_cfa_offset
:
2414 case MIToken::kw_cfi_escape
:
2415 case MIToken::kw_cfi_def_cfa
:
2416 case MIToken::kw_cfi_register
:
2417 case MIToken::kw_cfi_remember_state
:
2418 case MIToken::kw_cfi_restore
:
2419 case MIToken::kw_cfi_restore_state
:
2420 case MIToken::kw_cfi_undefined
:
2421 case MIToken::kw_cfi_window_save
:
2422 case MIToken::kw_cfi_aarch64_negate_ra_sign_state
:
2423 return parseCFIOperand(Dest
);
2424 case MIToken::kw_blockaddress
:
2425 return parseBlockAddressOperand(Dest
);
2426 case MIToken::kw_intrinsic
:
2427 return parseIntrinsicOperand(Dest
);
2428 case MIToken::kw_target_index
:
2429 return parseTargetIndexOperand(Dest
);
2430 case MIToken::kw_liveout
:
2431 return parseLiveoutRegisterMaskOperand(Dest
);
2432 case MIToken::kw_floatpred
:
2433 case MIToken::kw_intpred
:
2434 return parsePredicateOperand(Dest
);
2435 case MIToken::Error
:
2437 case MIToken::Identifier
:
2438 if (const auto *RegMask
= PFS
.Target
.getRegMask(Token
.stringValue())) {
2439 Dest
= MachineOperand::CreateRegMask(RegMask
);
2442 } else if (Token
.stringValue() == "CustomRegMask") {
2443 return parseCustomRegisterMaskOperand(Dest
);
2445 return parseTypedImmediateOperand(Dest
);
2447 // FIXME: Parse the MCSymbol machine operand.
2448 return error("expected a machine operand");
2453 bool MIParser::parseMachineOperandAndTargetFlags(
2454 MachineOperand
&Dest
, Optional
<unsigned> &TiedDefIdx
) {
2456 bool HasTargetFlags
= false;
2457 if (Token
.is(MIToken::kw_target_flags
)) {
2458 HasTargetFlags
= true;
2460 if (expectAndConsume(MIToken::lparen
))
2462 if (Token
.isNot(MIToken::Identifier
))
2463 return error("expected the name of the target flag");
2464 if (PFS
.Target
.getDirectTargetFlag(Token
.stringValue(), TF
)) {
2465 if (PFS
.Target
.getBitmaskTargetFlag(Token
.stringValue(), TF
))
2466 return error("use of undefined target flag '" + Token
.stringValue() +
2470 while (Token
.is(MIToken::comma
)) {
2472 if (Token
.isNot(MIToken::Identifier
))
2473 return error("expected the name of the target flag");
2474 unsigned BitFlag
= 0;
2475 if (PFS
.Target
.getBitmaskTargetFlag(Token
.stringValue(), BitFlag
))
2476 return error("use of undefined target flag '" + Token
.stringValue() +
2478 // TODO: Report an error when using a duplicate bit target flag.
2482 if (expectAndConsume(MIToken::rparen
))
2485 auto Loc
= Token
.location();
2486 if (parseMachineOperand(Dest
, TiedDefIdx
))
2488 if (!HasTargetFlags
)
2491 return error(Loc
, "register operands can't have target flags");
2492 Dest
.setTargetFlags(TF
);
2496 bool MIParser::parseOffset(int64_t &Offset
) {
2497 if (Token
.isNot(MIToken::plus
) && Token
.isNot(MIToken::minus
))
2499 StringRef Sign
= Token
.range();
2500 bool IsNegative
= Token
.is(MIToken::minus
);
2502 if (Token
.isNot(MIToken::IntegerLiteral
))
2503 return error("expected an integer literal after '" + Sign
+ "'");
2504 if (Token
.integerValue().getMinSignedBits() > 64)
2505 return error("expected 64-bit integer (too large)");
2506 Offset
= Token
.integerValue().getExtValue();
2513 bool MIParser::parseAlignment(unsigned &Alignment
) {
2514 assert(Token
.is(MIToken::kw_align
));
2516 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
2517 return error("expected an integer literal after 'align'");
2518 if (getUnsigned(Alignment
))
2522 if (!isPowerOf2_32(Alignment
))
2523 return error("expected a power-of-2 literal after 'align'");
2528 bool MIParser::parseAddrspace(unsigned &Addrspace
) {
2529 assert(Token
.is(MIToken::kw_addrspace
));
2531 if (Token
.isNot(MIToken::IntegerLiteral
) || Token
.integerValue().isSigned())
2532 return error("expected an integer literal after 'addrspace'");
2533 if (getUnsigned(Addrspace
))
2539 bool MIParser::parseOperandsOffset(MachineOperand
&Op
) {
2541 if (parseOffset(Offset
))
2543 Op
.setOffset(Offset
);
2547 bool MIParser::parseIRValue(const Value
*&V
) {
2548 switch (Token
.kind()) {
2549 case MIToken::NamedIRValue
: {
2550 V
= MF
.getFunction().getValueSymbolTable()->lookup(Token
.stringValue());
2553 case MIToken::IRValue
: {
2554 unsigned SlotNumber
= 0;
2555 if (getUnsigned(SlotNumber
))
2557 V
= getIRValue(SlotNumber
);
2560 case MIToken::NamedGlobalValue
:
2561 case MIToken::GlobalValue
: {
2562 GlobalValue
*GV
= nullptr;
2563 if (parseGlobalValue(GV
))
2568 case MIToken::QuotedIRValue
: {
2569 const Constant
*C
= nullptr;
2570 if (parseIRConstant(Token
.location(), Token
.stringValue(), C
))
2576 llvm_unreachable("The current token should be an IR block reference");
2579 return error(Twine("use of undefined IR value '") + Token
.range() + "'");
2583 bool MIParser::getUint64(uint64_t &Result
) {
2584 if (Token
.hasIntegerValue()) {
2585 if (Token
.integerValue().getActiveBits() > 64)
2586 return error("expected 64-bit integer (too large)");
2587 Result
= Token
.integerValue().getZExtValue();
2590 if (Token
.is(MIToken::HexLiteral
)) {
2594 if (A
.getBitWidth() > 64)
2595 return error("expected 64-bit integer (too large)");
2596 Result
= A
.getZExtValue();
2602 bool MIParser::getHexUint(APInt
&Result
) {
2603 assert(Token
.is(MIToken::HexLiteral
));
2604 StringRef S
= Token
.range();
2605 assert(S
[0] == '0' && tolower(S
[1]) == 'x');
2606 // This could be a floating point literal with a special prefix.
2607 if (!isxdigit(S
[2]))
2609 StringRef V
= S
.substr(2);
2610 APInt
A(V
.size()*4, V
, 16);
2612 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2613 // sure it isn't the case before constructing result.
2614 unsigned NumBits
= (A
== 0) ? 32 : A
.getActiveBits();
2615 Result
= APInt(NumBits
, ArrayRef
<uint64_t>(A
.getRawData(), A
.getNumWords()));
2619 bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags
&Flags
) {
2620 const auto OldFlags
= Flags
;
2621 switch (Token
.kind()) {
2622 case MIToken::kw_volatile
:
2623 Flags
|= MachineMemOperand::MOVolatile
;
2625 case MIToken::kw_non_temporal
:
2626 Flags
|= MachineMemOperand::MONonTemporal
;
2628 case MIToken::kw_dereferenceable
:
2629 Flags
|= MachineMemOperand::MODereferenceable
;
2631 case MIToken::kw_invariant
:
2632 Flags
|= MachineMemOperand::MOInvariant
;
2634 case MIToken::StringConstant
: {
2635 MachineMemOperand::Flags TF
;
2636 if (PFS
.Target
.getMMOTargetFlag(Token
.stringValue(), TF
))
2637 return error("use of undefined target MMO flag '" + Token
.stringValue() +
2643 llvm_unreachable("The current token should be a memory operand flag");
2645 if (OldFlags
== Flags
)
2646 // We know that the same flag is specified more than once when the flags
2647 // weren't modified.
2648 return error("duplicate '" + Token
.stringValue() + "' memory operand flag");
2653 bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue
*&PSV
) {
2654 switch (Token
.kind()) {
2655 case MIToken::kw_stack
:
2656 PSV
= MF
.getPSVManager().getStack();
2658 case MIToken::kw_got
:
2659 PSV
= MF
.getPSVManager().getGOT();
2661 case MIToken::kw_jump_table
:
2662 PSV
= MF
.getPSVManager().getJumpTable();
2664 case MIToken::kw_constant_pool
:
2665 PSV
= MF
.getPSVManager().getConstantPool();
2667 case MIToken::FixedStackObject
: {
2669 if (parseFixedStackFrameIndex(FI
))
2671 PSV
= MF
.getPSVManager().getFixedStack(FI
);
2672 // The token was already consumed, so use return here instead of break.
2675 case MIToken::StackObject
: {
2677 if (parseStackFrameIndex(FI
))
2679 PSV
= MF
.getPSVManager().getFixedStack(FI
);
2680 // The token was already consumed, so use return here instead of break.
2683 case MIToken::kw_call_entry
:
2685 switch (Token
.kind()) {
2686 case MIToken::GlobalValue
:
2687 case MIToken::NamedGlobalValue
: {
2688 GlobalValue
*GV
= nullptr;
2689 if (parseGlobalValue(GV
))
2691 PSV
= MF
.getPSVManager().getGlobalValueCallEntry(GV
);
2694 case MIToken::ExternalSymbol
:
2695 PSV
= MF
.getPSVManager().getExternalSymbolCallEntry(
2696 MF
.createExternalSymbolName(Token
.stringValue()));
2700 "expected a global value or an external symbol after 'call-entry'");
2704 llvm_unreachable("The current token should be pseudo source value");
2710 bool MIParser::parseMachinePointerInfo(MachinePointerInfo
&Dest
) {
2711 if (Token
.is(MIToken::kw_constant_pool
) || Token
.is(MIToken::kw_stack
) ||
2712 Token
.is(MIToken::kw_got
) || Token
.is(MIToken::kw_jump_table
) ||
2713 Token
.is(MIToken::FixedStackObject
) || Token
.is(MIToken::StackObject
) ||
2714 Token
.is(MIToken::kw_call_entry
)) {
2715 const PseudoSourceValue
*PSV
= nullptr;
2716 if (parseMemoryPseudoSourceValue(PSV
))
2719 if (parseOffset(Offset
))
2721 Dest
= MachinePointerInfo(PSV
, Offset
);
2724 if (Token
.isNot(MIToken::NamedIRValue
) && Token
.isNot(MIToken::IRValue
) &&
2725 Token
.isNot(MIToken::GlobalValue
) &&
2726 Token
.isNot(MIToken::NamedGlobalValue
) &&
2727 Token
.isNot(MIToken::QuotedIRValue
))
2728 return error("expected an IR value reference");
2729 const Value
*V
= nullptr;
2730 if (parseIRValue(V
))
2732 if (!V
->getType()->isPointerTy())
2733 return error("expected a pointer IR value");
2736 if (parseOffset(Offset
))
2738 Dest
= MachinePointerInfo(V
, Offset
);
2742 bool MIParser::parseOptionalScope(LLVMContext
&Context
,
2743 SyncScope::ID
&SSID
) {
2744 SSID
= SyncScope::System
;
2745 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "syncscope") {
2747 if (expectAndConsume(MIToken::lparen
))
2748 return error("expected '(' in syncscope");
2751 if (parseStringConstant(SSN
))
2754 SSID
= Context
.getOrInsertSyncScopeID(SSN
);
2755 if (expectAndConsume(MIToken::rparen
))
2756 return error("expected ')' in syncscope");
2762 bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering
&Order
) {
2763 Order
= AtomicOrdering::NotAtomic
;
2764 if (Token
.isNot(MIToken::Identifier
))
2767 Order
= StringSwitch
<AtomicOrdering
>(Token
.stringValue())
2768 .Case("unordered", AtomicOrdering::Unordered
)
2769 .Case("monotonic", AtomicOrdering::Monotonic
)
2770 .Case("acquire", AtomicOrdering::Acquire
)
2771 .Case("release", AtomicOrdering::Release
)
2772 .Case("acq_rel", AtomicOrdering::AcquireRelease
)
2773 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent
)
2774 .Default(AtomicOrdering::NotAtomic
);
2776 if (Order
!= AtomicOrdering::NotAtomic
) {
2781 return error("expected an atomic scope, ordering or a size specification");
2784 bool MIParser::parseMachineMemoryOperand(MachineMemOperand
*&Dest
) {
2785 if (expectAndConsume(MIToken::lparen
))
2787 MachineMemOperand::Flags Flags
= MachineMemOperand::MONone
;
2788 while (Token
.isMemoryOperandFlag()) {
2789 if (parseMemoryOperandFlag(Flags
))
2792 if (Token
.isNot(MIToken::Identifier
) ||
2793 (Token
.stringValue() != "load" && Token
.stringValue() != "store"))
2794 return error("expected 'load' or 'store' memory operation");
2795 if (Token
.stringValue() == "load")
2796 Flags
|= MachineMemOperand::MOLoad
;
2798 Flags
|= MachineMemOperand::MOStore
;
2801 // Optional 'store' for operands that both load and store.
2802 if (Token
.is(MIToken::Identifier
) && Token
.stringValue() == "store") {
2803 Flags
|= MachineMemOperand::MOStore
;
2807 // Optional synchronization scope.
2809 if (parseOptionalScope(MF
.getFunction().getContext(), SSID
))
2812 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2813 AtomicOrdering Order
, FailureOrder
;
2814 if (parseOptionalAtomicOrdering(Order
))
2817 if (parseOptionalAtomicOrdering(FailureOrder
))
2820 if (Token
.isNot(MIToken::IntegerLiteral
) &&
2821 Token
.isNot(MIToken::kw_unknown_size
))
2822 return error("expected the size integer literal or 'unknown-size' after "
2823 "memory operation");
2825 if (Token
.is(MIToken::IntegerLiteral
)) {
2826 if (getUint64(Size
))
2828 } else if (Token
.is(MIToken::kw_unknown_size
)) {
2829 Size
= MemoryLocation::UnknownSize
;
2833 MachinePointerInfo Ptr
= MachinePointerInfo();
2834 if (Token
.is(MIToken::Identifier
)) {
2836 ((Flags
& MachineMemOperand::MOLoad
) &&
2837 (Flags
& MachineMemOperand::MOStore
))
2839 : Flags
& MachineMemOperand::MOLoad
? "from" : "into";
2840 if (Token
.stringValue() != Word
)
2841 return error(Twine("expected '") + Word
+ "'");
2844 if (parseMachinePointerInfo(Ptr
))
2847 unsigned BaseAlignment
= (Size
!= MemoryLocation::UnknownSize
? Size
: 1);
2849 MDNode
*Range
= nullptr;
2850 while (consumeIfPresent(MIToken::comma
)) {
2851 switch (Token
.kind()) {
2852 case MIToken::kw_align
:
2853 if (parseAlignment(BaseAlignment
))
2856 case MIToken::kw_addrspace
:
2857 if (parseAddrspace(Ptr
.AddrSpace
))
2860 case MIToken::md_tbaa
:
2862 if (parseMDNode(AAInfo
.TBAA
))
2865 case MIToken::md_alias_scope
:
2867 if (parseMDNode(AAInfo
.Scope
))
2870 case MIToken::md_noalias
:
2872 if (parseMDNode(AAInfo
.NoAlias
))
2875 case MIToken::md_range
:
2877 if (parseMDNode(Range
))
2880 // TODO: Report an error on duplicate metadata nodes.
2882 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2883 "'!noalias' or '!range'");
2886 if (expectAndConsume(MIToken::rparen
))
2888 Dest
= MF
.getMachineMemOperand(Ptr
, Flags
, Size
, BaseAlignment
, AAInfo
, Range
,
2889 SSID
, Order
, FailureOrder
);
2893 bool MIParser::parsePreOrPostInstrSymbol(MCSymbol
*&Symbol
) {
2894 assert((Token
.is(MIToken::kw_pre_instr_symbol
) ||
2895 Token
.is(MIToken::kw_post_instr_symbol
)) &&
2896 "Invalid token for a pre- post-instruction symbol!");
2898 if (Token
.isNot(MIToken::MCSymbol
))
2899 return error("expected a symbol after 'pre-instr-symbol'");
2900 Symbol
= getOrCreateMCSymbol(Token
.stringValue());
2902 if (Token
.isNewlineOrEOF() || Token
.is(MIToken::coloncolon
) ||
2903 Token
.is(MIToken::lbrace
))
2905 if (Token
.isNot(MIToken::comma
))
2906 return error("expected ',' before the next machine operand");
2911 static void initSlots2BasicBlocks(
2913 DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
2914 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
2915 MST
.incorporateFunction(F
);
2916 for (auto &BB
: F
) {
2919 int Slot
= MST
.getLocalSlot(&BB
);
2922 Slots2BasicBlocks
.insert(std::make_pair(unsigned(Slot
), &BB
));
2926 static const BasicBlock
*getIRBlockFromSlot(
2928 const DenseMap
<unsigned, const BasicBlock
*> &Slots2BasicBlocks
) {
2929 auto BlockInfo
= Slots2BasicBlocks
.find(Slot
);
2930 if (BlockInfo
== Slots2BasicBlocks
.end())
2932 return BlockInfo
->second
;
2935 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
) {
2936 if (Slots2BasicBlocks
.empty())
2937 initSlots2BasicBlocks(MF
.getFunction(), Slots2BasicBlocks
);
2938 return getIRBlockFromSlot(Slot
, Slots2BasicBlocks
);
2941 const BasicBlock
*MIParser::getIRBlock(unsigned Slot
, const Function
&F
) {
2942 if (&F
== &MF
.getFunction())
2943 return getIRBlock(Slot
);
2944 DenseMap
<unsigned, const BasicBlock
*> CustomSlots2BasicBlocks
;
2945 initSlots2BasicBlocks(F
, CustomSlots2BasicBlocks
);
2946 return getIRBlockFromSlot(Slot
, CustomSlots2BasicBlocks
);
2949 static void mapValueToSlot(const Value
*V
, ModuleSlotTracker
&MST
,
2950 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
2951 int Slot
= MST
.getLocalSlot(V
);
2954 Slots2Values
.insert(std::make_pair(unsigned(Slot
), V
));
2957 /// Creates the mapping from slot numbers to function's unnamed IR values.
2958 static void initSlots2Values(const Function
&F
,
2959 DenseMap
<unsigned, const Value
*> &Slots2Values
) {
2960 ModuleSlotTracker
MST(F
.getParent(), /*ShouldInitializeAllMetadata=*/false);
2961 MST
.incorporateFunction(F
);
2962 for (const auto &Arg
: F
.args())
2963 mapValueToSlot(&Arg
, MST
, Slots2Values
);
2964 for (const auto &BB
: F
) {
2965 mapValueToSlot(&BB
, MST
, Slots2Values
);
2966 for (const auto &I
: BB
)
2967 mapValueToSlot(&I
, MST
, Slots2Values
);
2971 const Value
*MIParser::getIRValue(unsigned Slot
) {
2972 if (Slots2Values
.empty())
2973 initSlots2Values(MF
.getFunction(), Slots2Values
);
2974 auto ValueInfo
= Slots2Values
.find(Slot
);
2975 if (ValueInfo
== Slots2Values
.end())
2977 return ValueInfo
->second
;
2980 MCSymbol
*MIParser::getOrCreateMCSymbol(StringRef Name
) {
2981 // FIXME: Currently we can't recognize temporary or local symbols and call all
2982 // of the appropriate forms to create them. However, this handles basic cases
2983 // well as most of the special aspects are recognized by a prefix on their
2984 // name, and the input names should already be unique. For test cases, keeping
2985 // the symbol name out of the symbol table isn't terribly important.
2986 return MF
.getContext().getOrCreateSymbol(Name
);
2989 bool MIParser::parseStringConstant(std::string
&Result
) {
2990 if (Token
.isNot(MIToken::StringConstant
))
2991 return error("expected string constant");
2992 Result
= Token
.stringValue();
2997 bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState
&PFS
,
2999 SMDiagnostic
&Error
) {
3000 return MIParser(PFS
, Error
, Src
).parseBasicBlockDefinitions(PFS
.MBBSlots
);
3003 bool llvm::parseMachineInstructions(PerFunctionMIParsingState
&PFS
,
3004 StringRef Src
, SMDiagnostic
&Error
) {
3005 return MIParser(PFS
, Error
, Src
).parseBasicBlocks();
3008 bool llvm::parseMBBReference(PerFunctionMIParsingState
&PFS
,
3009 MachineBasicBlock
*&MBB
, StringRef Src
,
3010 SMDiagnostic
&Error
) {
3011 return MIParser(PFS
, Error
, Src
).parseStandaloneMBB(MBB
);
3014 bool llvm::parseRegisterReference(PerFunctionMIParsingState
&PFS
,
3015 unsigned &Reg
, StringRef Src
,
3016 SMDiagnostic
&Error
) {
3017 return MIParser(PFS
, Error
, Src
).parseStandaloneRegister(Reg
);
3020 bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState
&PFS
,
3021 unsigned &Reg
, StringRef Src
,
3022 SMDiagnostic
&Error
) {
3023 return MIParser(PFS
, Error
, Src
).parseStandaloneNamedRegister(Reg
);
3026 bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState
&PFS
,
3027 VRegInfo
*&Info
, StringRef Src
,
3028 SMDiagnostic
&Error
) {
3029 return MIParser(PFS
, Error
, Src
).parseStandaloneVirtualRegister(Info
);
3032 bool llvm::parseStackObjectReference(PerFunctionMIParsingState
&PFS
,
3033 int &FI
, StringRef Src
,
3034 SMDiagnostic
&Error
) {
3035 return MIParser(PFS
, Error
, Src
).parseStandaloneStackObject(FI
);
3038 bool llvm::parseMDNode(PerFunctionMIParsingState
&PFS
,
3039 MDNode
*&Node
, StringRef Src
, SMDiagnostic
&Error
) {
3040 return MIParser(PFS
, Error
, Src
).parseStandaloneMDNode(Node
);