LLVM 24.0.0git
MIParser.cpp
Go to the documentation of this file.
1//===- MIParser.cpp - Machine instructions parser implementation ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the parsing of machine instructions.
10//
11//===----------------------------------------------------------------------===//
12
14#include "MILexer.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/APSInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
43#include "llvm/IR/BasicBlock.h"
44#include "llvm/IR/Constants.h"
45#include "llvm/IR/DataLayout.h"
47#include "llvm/IR/DebugLoc.h"
48#include "llvm/IR/Function.h"
49#include "llvm/IR/InlineAsm.h"
50#include "llvm/IR/InstrTypes.h"
52#include "llvm/IR/Intrinsics.h"
53#include "llvm/IR/Metadata.h"
54#include "llvm/IR/Module.h"
56#include "llvm/IR/Type.h"
57#include "llvm/IR/Value.h"
59#include "llvm/MC/LaneBitmask.h"
60#include "llvm/MC/MCContext.h"
61#include "llvm/MC/MCDwarf.h"
62#include "llvm/MC/MCInstrDesc.h"
68#include "llvm/Support/SMLoc.h"
71#include <cassert>
72#include <cctype>
73#include <cstddef>
74#include <cstdint>
75#include <limits>
76#include <string>
77#include <utility>
78
79using namespace llvm;
80
82 const TargetSubtargetInfo &NewSubtarget) {
83
84 // If the subtarget changed, over conservatively assume everything is invalid.
85 if (&Subtarget == &NewSubtarget)
86 return;
87
88 Names2InstrOpCodes.clear();
89 Names2Regs.clear();
90 Names2RegMasks.clear();
91 Names2SubRegIndices.clear();
92 Names2TargetIndices.clear();
93 Names2DirectTargetFlags.clear();
94 Names2BitmaskTargetFlags.clear();
95 Names2MMOTargetFlags.clear();
96
97 initNames2RegClasses();
98 initNames2RegBanks();
99}
100
101void PerTargetMIParsingState::initNames2Regs() {
102 if (!Names2Regs.empty())
103 return;
104
105 // The '%noreg' register is the register 0.
106 Names2Regs.insert(std::make_pair("noreg", 0));
107 const auto *TRI = Subtarget.getRegisterInfo();
108 assert(TRI && "Expected target register info");
109
110 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
111 bool WasInserted =
112 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
113 .second;
114 (void)WasInserted;
115 assert(WasInserted && "Expected registers to be unique case-insensitively");
116 }
117}
118
120 Register &Reg) {
121 initNames2Regs();
122 auto RegInfo = Names2Regs.find(RegName);
123 if (RegInfo == Names2Regs.end())
124 return true;
125 Reg = RegInfo->getValue();
126 return false;
127}
128
130 uint8_t &FlagValue) const {
131 const auto *TRI = Subtarget.getRegisterInfo();
132 std::optional<uint8_t> FV = TRI->getVRegFlagValue(FlagName);
133 if (!FV)
134 return true;
135 FlagValue = *FV;
136 return false;
137}
138
139void PerTargetMIParsingState::initNames2InstrOpCodes() {
140 if (!Names2InstrOpCodes.empty())
141 return;
142 const auto *TII = Subtarget.getInstrInfo();
143 assert(TII && "Expected target instruction info");
144 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
145 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
146}
147
149 unsigned &OpCode) {
150 initNames2InstrOpCodes();
151 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
152 if (InstrInfo == Names2InstrOpCodes.end())
153 return true;
154 OpCode = InstrInfo->getValue();
155 return false;
156}
157
158void PerTargetMIParsingState::initNames2RegMasks() {
159 if (!Names2RegMasks.empty())
160 return;
161 const auto *TRI = Subtarget.getRegisterInfo();
162 assert(TRI && "Expected target register info");
163 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
164 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
165 assert(RegMasks.size() == RegMaskNames.size());
166 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
167 Names2RegMasks.insert(
168 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
169}
170
172 initNames2RegMasks();
173 auto RegMaskInfo = Names2RegMasks.find(Identifier);
174 if (RegMaskInfo == Names2RegMasks.end())
175 return nullptr;
176 return RegMaskInfo->getValue();
177}
178
179void PerTargetMIParsingState::initNames2SubRegIndices() {
180 if (!Names2SubRegIndices.empty())
181 return;
182 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
183 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
184 Names2SubRegIndices.insert(
185 std::make_pair(TRI->getSubRegIndexName(I), I));
186}
187
189 initNames2SubRegIndices();
190 auto SubRegInfo = Names2SubRegIndices.find(Name);
191 if (SubRegInfo == Names2SubRegIndices.end())
192 return 0;
193 return SubRegInfo->getValue();
194}
195
196void PerTargetMIParsingState::initNames2TargetIndices() {
197 if (!Names2TargetIndices.empty())
198 return;
199 const auto *TII = Subtarget.getInstrInfo();
200 assert(TII && "Expected target instruction info");
201 auto Indices = TII->getSerializableTargetIndices();
202 for (const auto &I : Indices)
203 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
204}
205
207 initNames2TargetIndices();
208 auto IndexInfo = Names2TargetIndices.find(Name);
209 if (IndexInfo == Names2TargetIndices.end())
210 return true;
211 Index = IndexInfo->second;
212 return false;
213}
214
215void PerTargetMIParsingState::initNames2DirectTargetFlags() {
216 if (!Names2DirectTargetFlags.empty())
217 return;
218
219 const auto *TII = Subtarget.getInstrInfo();
220 assert(TII && "Expected target instruction info");
221 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
222 for (const auto &I : Flags)
223 Names2DirectTargetFlags.insert(
224 std::make_pair(StringRef(I.second), I.first));
225}
226
228 unsigned &Flag) {
229 initNames2DirectTargetFlags();
230 auto FlagInfo = Names2DirectTargetFlags.find(Name);
231 if (FlagInfo == Names2DirectTargetFlags.end())
232 return true;
233 Flag = FlagInfo->second;
234 return false;
235}
236
237void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
238 if (!Names2BitmaskTargetFlags.empty())
239 return;
240
241 const auto *TII = Subtarget.getInstrInfo();
242 assert(TII && "Expected target instruction info");
243 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
244 for (const auto &I : Flags)
245 Names2BitmaskTargetFlags.insert(
246 std::make_pair(StringRef(I.second), I.first));
247}
248
250 unsigned &Flag) {
251 initNames2BitmaskTargetFlags();
252 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
253 if (FlagInfo == Names2BitmaskTargetFlags.end())
254 return true;
255 Flag = FlagInfo->second;
256 return false;
257}
258
259void PerTargetMIParsingState::initNames2MMOTargetFlags() {
260 if (!Names2MMOTargetFlags.empty())
261 return;
262
263 const auto *TII = Subtarget.getInstrInfo();
264 assert(TII && "Expected target instruction info");
265 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
266 for (const auto &I : Flags)
267 Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
268}
269
272 initNames2MMOTargetFlags();
273 auto FlagInfo = Names2MMOTargetFlags.find(Name);
274 if (FlagInfo == Names2MMOTargetFlags.end())
275 return true;
276 Flag = FlagInfo->second;
277 return false;
278}
279
280void PerTargetMIParsingState::initNames2RegClasses() {
281 if (!Names2RegClasses.empty())
282 return;
283
284 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
285 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
286 const auto *RC = TRI->getRegClass(I);
287 Names2RegClasses.insert(
288 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
289 }
290}
291
292void PerTargetMIParsingState::initNames2RegBanks() {
293 if (!Names2RegBanks.empty())
294 return;
295
296 const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
297 // If the target does not support GlobalISel, we may not have a
298 // register bank info.
299 if (!RBI)
300 return;
301
302 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
303 const auto &RegBank = RBI->getRegBank(I);
304 Names2RegBanks.insert(
305 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
306 }
307}
308
311 auto RegClassInfo = Names2RegClasses.find(Name);
312 if (RegClassInfo == Names2RegClasses.end())
313 return nullptr;
314 return RegClassInfo->getValue();
315}
316
318 auto RegBankInfo = Names2RegBanks.find(Name);
319 if (RegBankInfo == Names2RegBanks.end())
320 return nullptr;
321 return RegBankInfo->getValue();
322}
323
328
330 auto I = VRegInfos.try_emplace(Num);
331 if (I.second) {
332 MachineRegisterInfo &MRI = MF.getRegInfo();
333 VRegInfo *Info = new (Allocator) VRegInfo;
335 I.first->second = Info;
336 }
337 return *I.first->second;
338}
339
341 assert(RegName != "" && "Expected named reg.");
342
343 auto I = VRegInfosNamed.try_emplace(RegName.str());
344 if (I.second) {
345 VRegInfo *Info = new (Allocator) VRegInfo;
346 Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
347 I.first->second = Info;
348 }
349 return *I.first->second;
350}
351
352static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
353 DenseMap<unsigned, const Value *> &Slots2Values) {
354 int Slot = MST.getLocalSlot(V);
355 if (Slot == -1)
356 return;
357 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
358}
359
360/// Creates the mapping from slot numbers to function's unnamed IR values.
361static void initSlots2Values(const Function &F,
362 DenseMap<unsigned, const Value *> &Slots2Values) {
363 ModuleSlotTracker MST(F.getParent());
365 for (const auto &Arg : F.args())
366 mapValueToSlot(&Arg, MST, Slots2Values);
367 for (const auto &BB : F) {
368 mapValueToSlot(&BB, MST, Slots2Values);
369 for (const auto &I : BB)
370 mapValueToSlot(&I, MST, Slots2Values);
371 }
372}
373
375 if (Slots2Values.empty())
376 initSlots2Values(MF.getFunction(), Slots2Values);
377 return Slots2Values.lookup(Slot);
378}
379
380namespace {
381
382/// A wrapper struct around the 'MachineOperand' struct that includes a source
383/// range and other attributes.
384struct ParsedMachineOperand {
385 MachineOperand Operand;
388 std::optional<unsigned> TiedDefIdx;
389
390 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
392 std::optional<unsigned> &TiedDefIdx)
393 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
394 if (TiedDefIdx)
395 assert(Operand.isReg() && Operand.isUse() &&
396 "Only used register operands can be tied");
397 }
398};
399
400class MIParser {
401 MachineFunction &MF;
402 SMDiagnostic &Error;
403 StringRef Source, CurrentSource;
404 MIToken Token;
405 PerFunctionMIParsingState &PFS;
406 /// Maps from slot numbers to function's unnamed basic blocks.
407 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
408
409public:
410 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
411 StringRef Source);
412
413 /// \p SkipChar gives the number of characters to skip before looking
414 /// for the next token.
415 void lex(unsigned SkipChar = 0);
416
417 /// Report an error at the current location with the given message.
418 ///
419 /// This function always return true.
420 bool error(const Twine &Msg);
421
422 /// Report an error at the given location with the given message.
423 ///
424 /// This function always return true.
425 bool error(StringRef::iterator Loc, const Twine &Msg);
426
427 bool
428 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
429 bool parseBasicBlocks();
430 bool parse(MachineInstr *&MI);
431 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
432 bool parseStandaloneNamedRegister(Register &Reg);
433 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
434 bool parseStandaloneRegister(Register &Reg);
435 bool parseStandaloneStackObject(int &FI);
436 bool parseStandaloneMDNode(MDNode *&Node);
437
438 bool
439 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
440 bool parseBasicBlock(MachineBasicBlock &MBB,
441 MachineBasicBlock *&AddFalthroughFrom);
442 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
443 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
444
445 bool parseNamedRegister(Register &Reg);
446 bool parseVirtualRegister(VRegInfo *&Info);
447 bool parseNamedVirtualRegister(VRegInfo *&Info);
448 bool parseRegister(Register &Reg, VRegInfo *&VRegInfo);
449 bool parseRegisterFlag(RegState &Flags);
450 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
451 bool parseSubRegisterIndex(unsigned &SubReg);
452 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
453 bool parseRegisterOperand(MachineOperand &Dest,
454 std::optional<unsigned> &TiedDefIdx,
455 bool IsDef = false);
456 bool parseImmediateOperand(MachineOperand &Dest);
457 bool parseSymbolicInlineAsmOperand(unsigned OpIdx, MachineOperand &Dest);
458 bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
459 const Constant *&C);
460 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
461 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
462 bool parseTypedImmediateOperand(MachineOperand &Dest);
463 bool parseFPImmediateOperand(MachineOperand &Dest);
464 bool parseMBBReference(MachineBasicBlock *&MBB);
465 bool parseMBBOperand(MachineOperand &Dest);
466 bool parseStackFrameIndex(int &FI);
467 bool parseStackObjectOperand(MachineOperand &Dest);
468 bool parseFixedStackFrameIndex(int &FI);
469 bool parseFixedStackObjectOperand(MachineOperand &Dest);
470 bool parseGlobalValue(GlobalValue *&GV);
471 bool parseGlobalAddressOperand(MachineOperand &Dest);
472 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
473 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
474 bool parseJumpTableIndexOperand(MachineOperand &Dest);
475 bool parseExternalSymbolOperand(MachineOperand &Dest);
476 bool parseMCSymbolOperand(MachineOperand &Dest);
477 [[nodiscard]] bool parseMDNode(MDNode *&Node);
478 bool parseDIExpression(MDNode *&Expr);
479 bool parseDILocation(MDNode *&Expr);
480 bool parseMetadataOperand(MachineOperand &Dest);
481 bool parseCFIOffset(int &Offset);
482 bool parseCFIUnsigned(unsigned &Value);
483 bool parseCFIRegister(unsigned &Reg);
484 bool parseCFIAddressSpace(unsigned &AddressSpace);
485 bool parseCFIEscapeValues(std::string& Values);
486 bool parseCFIOperand(MachineOperand &Dest);
487 bool parseIRBlock(BasicBlock *&BB, const Function &F);
488 bool parseBlockAddressOperand(MachineOperand &Dest);
489 bool parseIntrinsicOperand(MachineOperand &Dest);
490 bool parsePredicateOperand(MachineOperand &Dest);
491 bool parseShuffleMaskOperand(MachineOperand &Dest);
492 bool parseTargetIndexOperand(MachineOperand &Dest);
493 bool parseDbgInstrRefOperand(MachineOperand &Dest);
494 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
495 bool parseLaneMaskOperand(MachineOperand &Dest);
496 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
497 bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
498 MachineOperand &Dest,
499 std::optional<unsigned> &TiedDefIdx);
500 bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
501 const unsigned OpIdx,
502 MachineOperand &Dest,
503 std::optional<unsigned> &TiedDefIdx);
504 bool parseOffset(int64_t &Offset);
505 bool parseIRBlockAddressTaken(BasicBlock *&BB);
506 bool parseAlignment(uint64_t &Alignment);
507 bool parseAddrspace(unsigned &Addrspace);
508 bool parseSectionID(std::optional<MBBSectionID> &SID);
509 bool parseBBID(std::optional<UniqueBBID> &BBID);
510 bool parseCallFrameSize(unsigned &CallFrameSize);
511 bool parseMaxBytesForAlignment(unsigned &MaxBytesForAlignment);
512 bool parsePrefetchTarget(CallsiteID &Target);
513 bool parseOperandsOffset(MachineOperand &Op);
514 bool parseIRValue(const Value *&V);
515 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
516 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
517 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
518 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
519 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
520 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
521 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
522 bool parseHeapAllocMarker(MDNode *&Node);
523 bool parsePCSections(MDNode *&Node);
524 bool parseMMRA(MDNode *&Node);
525
526 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
527 MachineOperand &Dest, const MIRFormatter &MF);
528
529private:
530 /// Convert the integer literal in the current token into an unsigned integer.
531 ///
532 /// Return true if an error occurred.
533 bool getUnsigned(unsigned &Result);
534
535 /// Convert the integer literal in the current token into an uint64.
536 ///
537 /// Return true if an error occurred.
538 bool getUint64(uint64_t &Result);
539
540 /// Convert the hexadecimal literal in the current token into an unsigned
541 /// APInt with a minimum bitwidth required to represent the value.
542 ///
543 /// Return true if the literal does not represent an integer value.
544 bool getHexUint(APInt &Result);
545
546 /// If the current token is of the given kind, consume it and return false.
547 /// Otherwise report an error and return true.
548 bool expectAndConsume(MIToken::TokenKind TokenKind);
549
550 /// If the current token is of the given kind, consume it and return true.
551 /// Otherwise return false.
552 bool consumeIfPresent(MIToken::TokenKind TokenKind);
553
554 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
555
556 bool assignRegisterTies(MachineInstr &MI,
558
559 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
560 const MCInstrDesc &MCID);
561
562 const BasicBlock *getIRBlock(unsigned Slot);
563 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
564
565 /// Get or create an MCSymbol for a given name.
566 MCSymbol *getOrCreateMCSymbol(StringRef Name);
567
568 /// parseStringConstant
569 /// ::= StringConstant
570 bool parseStringConstant(std::string &Result);
571};
572
573} // end anonymous namespace
574
575MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
576 StringRef Source)
577 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
578{}
579
580void MIParser::lex(unsigned SkipChar) {
581 CurrentSource = lexMIToken(
582 CurrentSource.substr(SkipChar), Token,
583 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
584}
585
586bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
587
588bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
589 const SourceMgr &SM = *PFS.SM;
590 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
591 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
592 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
593 // Create an ordinary diagnostic when the source manager's buffer is the
594 // source string.
596 return true;
597 }
598 // Create a diagnostic for a YAML string literal.
599 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
600 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
601 Source, {}, {});
602 return true;
603}
604
605typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
607
608static const char *toString(MIToken::TokenKind TokenKind) {
609 switch (TokenKind) {
610 case MIToken::comma:
611 return "','";
612 case MIToken::equal:
613 return "'='";
614 case MIToken::colon:
615 return "':'";
616 case MIToken::lparen:
617 return "'('";
618 case MIToken::rparen:
619 return "')'";
620 default:
621 return "<unknown token>";
622 }
623}
624
625bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
626 if (Token.isNot(TokenKind))
627 return error(Twine("expected ") + toString(TokenKind));
628 lex();
629 return false;
630}
631
632bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
633 if (Token.isNot(TokenKind))
634 return false;
635 lex();
636 return true;
637}
638
639// Parse Machine Basic Block Section ID.
640bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
642 lex();
643 if (Token.is(MIToken::IntegerLiteral)) {
644 unsigned Value = 0;
645 if (getUnsigned(Value))
646 return error("Unknown Section ID");
647 SID = MBBSectionID{Value};
648 } else {
649 const StringRef &S = Token.stringValue();
650 if (S == "Exception")
652 else if (S == "Cold")
654 else
655 return error("Unknown Section ID");
656 }
657 lex();
658 return false;
659}
660
661// Parse Machine Basic Block ID.
662bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) {
663 if (Token.isNot(MIToken::kw_bb_id))
664 return error("expected 'bb_id'");
665 lex();
666 unsigned BaseID = 0;
667 unsigned CloneID = 0;
668 if (Token.is(MIToken::FloatingPointLiteral)) {
669 StringRef S = Token.range();
670 auto Parts = S.split('.');
671 if (Parts.first.getAsInteger(10, BaseID) ||
672 Parts.second.getAsInteger(10, CloneID))
673 return error("Unknown BB ID");
674 lex();
675 } else {
676 if (getUnsigned(BaseID))
677 return error("Unknown BB ID");
678 lex();
679 if (Token.is(MIToken::comma) || Token.is(MIToken::dot)) {
680 lex();
681 if (getUnsigned(CloneID))
682 return error("Unknown Clone ID");
683 lex();
684 } else if (Token.is(MIToken::IntegerLiteral)) {
685 if (getUnsigned(CloneID))
686 return error("Unknown Clone ID");
687 lex();
688 }
689 }
690 BBID = {BaseID, CloneID};
691 return false;
692}
693
694// Parse basic block call frame size.
695bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) {
697 lex();
698 unsigned Value = 0;
699 if (getUnsigned(Value))
700 return error("Unknown call frame size");
701 CallFrameSize = Value;
702 lex();
703 return false;
704}
705
706// Parse the maximum number of bytes permitted for basic block alignment
707// padding.
708bool MIParser::parseMaxBytesForAlignment(unsigned &MaxBytesForAlignment) {
710 lex();
711 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
712 return error("expected an integer literal after 'max-bytes-for-alignment'");
713 unsigned Value = 0;
714 if (getUnsigned(Value))
715 return true;
716 MaxBytesForAlignment = Value;
717 lex();
718 return false;
719}
720
721bool MIParser::parsePrefetchTarget(CallsiteID &Target) {
722 lex();
723 std::optional<UniqueBBID> BBID;
724 if (parseBBID(BBID))
725 return true;
726 Target.BBID = *BBID;
727 if (expectAndConsume(MIToken::comma))
728 return true;
729 return getUnsigned(Target.CallsiteIndex);
730}
731
732bool MIParser::parseBasicBlockDefinition(
735 unsigned ID = 0;
736 if (getUnsigned(ID))
737 return true;
738 auto Loc = Token.location();
739 auto Name = Token.stringValue();
740 lex();
741 bool MachineBlockAddressTaken = false;
742 BasicBlock *AddressTakenIRBlock = nullptr;
743 bool IsLandingPad = false;
744 bool IsInlineAsmBrIndirectTarget = false;
745 bool IsEHFuncletEntry = false;
746 bool IsEHScopeEntry = false;
747 bool IsCleanupFuncletEntry = false;
748 bool IsEHContTarget = false;
749 std::optional<MBBSectionID> SectionID;
751 unsigned MaxBytesForAlignment = 0;
752 std::optional<UniqueBBID> BBID;
753 unsigned CallFrameSize = 0;
754 BasicBlock *BB = nullptr;
755 if (consumeIfPresent(MIToken::lparen)) {
756 do {
757 // TODO: Report an error when multiple same attributes are specified.
758 switch (Token.kind()) {
760 MachineBlockAddressTaken = true;
761 lex();
762 break;
764 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
765 return true;
766 break;
768 IsLandingPad = true;
769 lex();
770 break;
772 IsInlineAsmBrIndirectTarget = true;
773 lex();
774 break;
776 IsEHFuncletEntry = true;
777 lex();
778 break;
780 IsEHScopeEntry = true;
781 lex();
782 break;
784 IsCleanupFuncletEntry = true;
785 lex();
786 break;
788 IsEHContTarget = true;
789 lex();
790 break;
792 if (parseAlignment(Alignment))
793 return true;
794 break;
796 if (parseMaxBytesForAlignment(MaxBytesForAlignment))
797 return true;
798 break;
799 case MIToken::IRBlock:
801 // TODO: Report an error when both name and ir block are specified.
802 if (parseIRBlock(BB, MF.getFunction()))
803 return true;
804 lex();
805 break;
807 if (parseSectionID(SectionID))
808 return true;
809 break;
811 if (parseBBID(BBID))
812 return true;
813 break;
815 if (parseCallFrameSize(CallFrameSize))
816 return true;
817 break;
818 default:
819 break;
820 }
821 } while (consumeIfPresent(MIToken::comma));
822 if (expectAndConsume(MIToken::rparen))
823 return true;
824 }
825 if (expectAndConsume(MIToken::colon))
826 return true;
827
828 if (!Name.empty()) {
830 MF.getFunction().getValueSymbolTable()->lookup(Name));
831 if (!BB)
832 return error(Loc, Twine("basic block '") + Name +
833 "' is not defined in the function '" +
834 MF.getName() + "'");
835 }
836 auto *MBB = MF.CreateMachineBasicBlock(BB, BBID);
837 MF.insert(MF.end(), MBB);
838 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
839 if (!WasInserted)
840 return error(Loc, Twine("redefinition of machine basic block with id #") +
841 Twine(ID));
842 if (Alignment)
843 MBB->setAlignment(Align(Alignment));
844 else if (MaxBytesForAlignment)
845 return error(Loc, "'max-bytes-for-alignment' requires 'align'");
846 MBB->setMaxBytesForAlignment(MaxBytesForAlignment);
847 if (MachineBlockAddressTaken)
849 if (AddressTakenIRBlock)
850 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
851 MBB->setIsEHPad(IsLandingPad);
852 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
853 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
854 MBB->setIsEHScopeEntry(IsEHScopeEntry);
855 MBB->setIsCleanupFuncletEntry(IsCleanupFuncletEntry);
856 MBB->setIsEHContTarget(IsEHContTarget);
857 if (SectionID) {
858 MBB->setSectionID(*SectionID);
859 MF.setBBSectionsType(BasicBlockSection::List);
860 }
861 MBB->setCallFrameSize(CallFrameSize);
862 return false;
863}
864
865bool MIParser::parseBasicBlockDefinitions(
867 lex();
868 // Skip until the first machine basic block.
869 while (Token.is(MIToken::Newline))
870 lex();
871 if (Token.isErrorOrEOF())
872 return Token.isError();
873 if (Token.isNot(MIToken::MachineBasicBlockLabel))
874 return error("expected a basic block definition before instructions");
875 unsigned BraceDepth = 0;
876 do {
877 if (parseBasicBlockDefinition(MBBSlots))
878 return true;
879 bool IsAfterNewline = false;
880 // Skip until the next machine basic block.
881 while (true) {
882 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
883 Token.isErrorOrEOF())
884 break;
885 else if (Token.is(MIToken::MachineBasicBlockLabel))
886 return error("basic block definition should be located at the start of "
887 "the line");
888 else if (consumeIfPresent(MIToken::Newline)) {
889 IsAfterNewline = true;
890 continue;
891 }
892 IsAfterNewline = false;
893 if (Token.is(MIToken::lbrace))
894 ++BraceDepth;
895 if (Token.is(MIToken::rbrace)) {
896 if (!BraceDepth)
897 return error("extraneous closing brace ('}')");
898 --BraceDepth;
899 }
900 lex();
901 }
902 // Verify that we closed all of the '{' at the end of a file or a block.
903 if (!Token.isError() && BraceDepth)
904 return error("expected '}'"); // FIXME: Report a note that shows '{'.
905 } while (!Token.isErrorOrEOF());
906 return Token.isError();
907}
908
909bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
910 assert(Token.is(MIToken::kw_liveins));
911 lex();
912 if (expectAndConsume(MIToken::colon))
913 return true;
914 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
915 return false;
916 do {
917 if (Token.isNot(MIToken::NamedRegister))
918 return error("expected a named register");
920 if (parseNamedRegister(Reg))
921 return true;
922 lex();
924 if (consumeIfPresent(MIToken::colon)) {
925 // Parse lane mask.
926 if (Token.isNot(MIToken::IntegerLiteral) &&
927 Token.isNot(MIToken::HexLiteral))
928 return error("expected a lane mask");
929 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
930 "Use correct get-function for lane mask");
932 if (getUint64(V))
933 return error("invalid lane mask value");
934 Mask = LaneBitmask(V);
935 lex();
936 }
937 MBB.addLiveIn(Reg, Mask);
938 } while (consumeIfPresent(MIToken::comma));
939 return false;
940}
941
942bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
944 lex();
945 if (expectAndConsume(MIToken::colon))
946 return true;
947 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
948 return false;
949 do {
950 if (Token.isNot(MIToken::MachineBasicBlock))
951 return error("expected a machine basic block reference");
952 MachineBasicBlock *SuccMBB = nullptr;
953 if (parseMBBReference(SuccMBB))
954 return true;
955 lex();
956 unsigned Weight = 0;
957 if (consumeIfPresent(MIToken::lparen)) {
958 if (Token.isNot(MIToken::IntegerLiteral) &&
959 Token.isNot(MIToken::HexLiteral))
960 return error("expected an integer literal after '('");
961 if (getUnsigned(Weight))
962 return true;
963 lex();
964 if (expectAndConsume(MIToken::rparen))
965 return true;
966 }
968 } while (consumeIfPresent(MIToken::comma));
970 return false;
971}
972
973bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
974 MachineBasicBlock *&AddFalthroughFrom) {
975 // Skip the definition.
977 lex();
978 if (consumeIfPresent(MIToken::lparen)) {
979 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
980 lex();
981 consumeIfPresent(MIToken::rparen);
982 }
983 consumeIfPresent(MIToken::colon);
984
985 // Parse the liveins and successors.
986 // N.B: Multiple lists of successors and liveins are allowed and they're
987 // merged into one.
988 // Example:
989 // liveins: $edi
990 // liveins: $esi
991 //
992 // is equivalent to
993 // liveins: $edi, $esi
994 bool ExplicitSuccessors = false;
995 while (true) {
996 if (Token.is(MIToken::kw_successors)) {
997 if (parseBasicBlockSuccessors(MBB))
998 return true;
999 ExplicitSuccessors = true;
1000 } else if (Token.is(MIToken::kw_liveins)) {
1001 if (parseBasicBlockLiveins(MBB))
1002 return true;
1003 } else if (consumeIfPresent(MIToken::Newline)) {
1004 continue;
1005 } else {
1006 break;
1007 }
1008 if (!Token.isNewlineOrEOF())
1009 return error("expected line break at the end of a list");
1010 lex();
1011 }
1012
1013 // Parse the instructions.
1014 bool IsInBundle = false;
1015 MachineInstr *PrevMI = nullptr;
1016 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
1017 !Token.is(MIToken::Eof)) {
1018 if (consumeIfPresent(MIToken::Newline))
1019 continue;
1020 if (consumeIfPresent(MIToken::rbrace)) {
1021 // The first parsing pass should verify that all closing '}' have an
1022 // opening '{'.
1023 assert(IsInBundle);
1024 IsInBundle = false;
1025 continue;
1026 }
1027 MachineInstr *MI = nullptr;
1028 if (parse(MI))
1029 return true;
1030 MBB.insert(MBB.end(), MI);
1031 if (IsInBundle) {
1033 MI->setFlag(MachineInstr::BundledPred);
1034 }
1035 PrevMI = MI;
1036 if (Token.is(MIToken::lbrace)) {
1037 if (IsInBundle)
1038 return error("nested instruction bundles are not allowed");
1039 lex();
1040 // This instruction is the start of the bundle.
1041 MI->setFlag(MachineInstr::BundledSucc);
1042 IsInBundle = true;
1043 if (!Token.is(MIToken::Newline))
1044 // The next instruction can be on the same line.
1045 continue;
1046 }
1047 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
1048 lex();
1049 }
1050
1051 // Construct successor list by searching for basic block machine operands.
1052 if (!ExplicitSuccessors) {
1054 bool IsFallthrough;
1055 guessSuccessors(MBB, Successors, IsFallthrough);
1056 for (MachineBasicBlock *Succ : Successors)
1057 MBB.addSuccessor(Succ);
1058
1059 if (IsFallthrough) {
1060 AddFalthroughFrom = &MBB;
1061 } else {
1063 }
1064 }
1065
1066 return false;
1067}
1068
1069bool MIParser::parseBasicBlocks() {
1070 lex();
1071 // Skip until the first machine basic block.
1072 while (Token.is(MIToken::Newline))
1073 lex();
1074 if (Token.isErrorOrEOF())
1075 return Token.isError();
1076 // The first parsing pass should have verified that this token is a MBB label
1077 // in the 'parseBasicBlockDefinitions' method.
1079 MachineBasicBlock *AddFalthroughFrom = nullptr;
1080 do {
1081 MachineBasicBlock *MBB = nullptr;
1083 return true;
1084 if (AddFalthroughFrom) {
1085 if (!AddFalthroughFrom->isSuccessor(MBB))
1086 AddFalthroughFrom->addSuccessor(MBB);
1087 AddFalthroughFrom->normalizeSuccProbs();
1088 AddFalthroughFrom = nullptr;
1089 }
1090 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1091 return true;
1092 // The method 'parseBasicBlock' should parse the whole block until the next
1093 // block or the end of file.
1094 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1095 } while (Token.isNot(MIToken::Eof));
1096 return false;
1097}
1098
1099bool MIParser::parse(MachineInstr *&MI) {
1100 // Parse any register operands before '='
1103 while (Token.isRegister() || Token.isRegisterFlag()) {
1104 auto Loc = Token.location();
1105 std::optional<unsigned> TiedDefIdx;
1106 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1107 return true;
1108 Operands.push_back(
1109 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1110 if (Token.isNot(MIToken::comma))
1111 break;
1112 lex();
1113 }
1114 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1115 return true;
1116
1117 unsigned OpCode, Flags = 0;
1118 if (Token.isError() || parseInstruction(OpCode, Flags))
1119 return true;
1120
1121 // Parse the remaining machine operands.
1122 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1123 Token.isNot(MIToken::kw_post_instr_symbol) &&
1124 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1125 Token.isNot(MIToken::kw_pcsections) && Token.isNot(MIToken::kw_mmra) &&
1126 Token.isNot(MIToken::kw_cfi_type) &&
1127 Token.isNot(MIToken::kw_deactivation_symbol) &&
1128 Token.isNot(MIToken::kw_debug_location) &&
1129 Token.isNot(MIToken::kw_debug_instr_number) &&
1130 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1131 auto Loc = Token.location();
1132 std::optional<unsigned> TiedDefIdx;
1133 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1134 return true;
1135 Operands.push_back(
1136 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1137 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1138 Token.is(MIToken::lbrace))
1139 break;
1140 if (Token.isNot(MIToken::comma))
1141 return error("expected ',' before the next machine operand");
1142 lex();
1143 }
1144
1145 MCSymbol *PreInstrSymbol = nullptr;
1146 if (Token.is(MIToken::kw_pre_instr_symbol))
1147 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1148 return true;
1149 MCSymbol *PostInstrSymbol = nullptr;
1150 if (Token.is(MIToken::kw_post_instr_symbol))
1151 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1152 return true;
1153 MDNode *HeapAllocMarker = nullptr;
1154 if (Token.is(MIToken::kw_heap_alloc_marker))
1155 if (parseHeapAllocMarker(HeapAllocMarker))
1156 return true;
1157 MDNode *PCSections = nullptr;
1158 if (Token.is(MIToken::kw_pcsections))
1159 if (parsePCSections(PCSections))
1160 return true;
1161 MDNode *MMRA = nullptr;
1162 if (Token.is(MIToken::kw_mmra) && parseMMRA(MMRA))
1163 return true;
1164 unsigned CFIType = 0;
1165 if (Token.is(MIToken::kw_cfi_type)) {
1166 lex();
1167 if (Token.isNot(MIToken::IntegerLiteral))
1168 return error("expected an integer literal after 'cfi-type'");
1169 // getUnsigned is sufficient for 32-bit integers.
1170 if (getUnsigned(CFIType))
1171 return true;
1172 lex();
1173 // Lex past trailing comma if present.
1174 if (Token.is(MIToken::comma))
1175 lex();
1176 }
1177
1178 GlobalValue *DS = nullptr;
1179 if (Token.is(MIToken::kw_deactivation_symbol)) {
1180 lex();
1181 if (parseGlobalValue(DS))
1182 return true;
1183 lex();
1184 }
1185
1186 unsigned InstrNum = 0;
1187 if (Token.is(MIToken::kw_debug_instr_number)) {
1188 lex();
1189 if (Token.isNot(MIToken::IntegerLiteral))
1190 return error("expected an integer literal after 'debug-instr-number'");
1191 if (getUnsigned(InstrNum))
1192 return true;
1193 lex();
1194 // Lex past trailing comma if present.
1195 if (Token.is(MIToken::comma))
1196 lex();
1197 }
1198
1199 DebugLoc DebugLocation;
1200 if (Token.is(MIToken::kw_debug_location)) {
1201 lex();
1202 MDNode *Node = nullptr;
1203 if (Token.is(MIToken::exclaim)) {
1204 if (parseMDNode(Node))
1205 return true;
1206 } else if (Token.is(MIToken::md_dilocation)) {
1207 if (parseDILocation(Node))
1208 return true;
1209 } else {
1210 return error("expected a metadata node after 'debug-location'");
1211 }
1212 DebugLocation = DebugLoc(dyn_cast<DILocation>(Node));
1213 if (!DebugLocation)
1214 return error("referenced metadata is not a DILocation");
1215 }
1216
1217 // Parse the machine memory operands.
1219 if (Token.is(MIToken::coloncolon)) {
1220 lex();
1221 while (!Token.isNewlineOrEOF()) {
1222 MachineMemOperand *MemOp = nullptr;
1223 if (parseMachineMemoryOperand(MemOp))
1224 return true;
1225 MemOperands.push_back(MemOp);
1226 if (Token.isNewlineOrEOF())
1227 break;
1228 if (OpCode == TargetOpcode::BUNDLE && Token.is(MIToken::lbrace))
1229 break;
1230 if (Token.isNot(MIToken::comma))
1231 return error("expected ',' before the next machine memory operand");
1232 lex();
1233 }
1234 }
1235
1236 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1237 if (!MCID.isVariadic()) {
1238 // FIXME: Move the implicit operand verification to the machine verifier.
1239 if (verifyImplicitOperands(Operands, MCID))
1240 return true;
1241 }
1242
1243 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1244 MI->setFlags(Flags);
1245
1246 // Don't check the operands make sense, let the verifier catch any
1247 // improprieties.
1248 for (const auto &Operand : Operands)
1249 MI->addOperand(MF, Operand.Operand);
1250
1251 if (assignRegisterTies(*MI, Operands))
1252 return true;
1253 if (PreInstrSymbol)
1254 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1255 if (PostInstrSymbol)
1256 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1257 if (HeapAllocMarker)
1258 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1259 if (PCSections)
1260 MI->setPCSections(MF, PCSections);
1261 if (MMRA)
1262 MI->setMMRAMetadata(MF, MMRA);
1263 if (CFIType)
1264 MI->setCFIType(MF, CFIType);
1265 if (DS)
1266 MI->setDeactivationSymbol(MF, DS);
1267 if (!MemOperands.empty())
1268 MI->setMemRefs(MF, MemOperands);
1269 if (InstrNum)
1270 MI->setDebugInstrNum(InstrNum);
1271 return false;
1272}
1273
1274bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1275 lex();
1276 if (Token.isNot(MIToken::MachineBasicBlock))
1277 return error("expected a machine basic block reference");
1279 return true;
1280 lex();
1281 if (Token.isNot(MIToken::Eof))
1282 return error(
1283 "expected end of string after the machine basic block reference");
1284 return false;
1285}
1286
1287bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1288 lex();
1289 if (Token.isNot(MIToken::NamedRegister))
1290 return error("expected a named register");
1291 if (parseNamedRegister(Reg))
1292 return true;
1293 lex();
1294 if (Token.isNot(MIToken::Eof))
1295 return error("expected end of string after the register reference");
1296 return false;
1297}
1298
1299bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1300 lex();
1301 if (Token.isNot(MIToken::VirtualRegister))
1302 return error("expected a virtual register");
1303 if (parseVirtualRegister(Info))
1304 return true;
1305 lex();
1306 if (Token.isNot(MIToken::Eof))
1307 return error("expected end of string after the register reference");
1308 return false;
1309}
1310
1311bool MIParser::parseStandaloneRegister(Register &Reg) {
1312 lex();
1313 if (Token.isNot(MIToken::NamedRegister) &&
1314 Token.isNot(MIToken::VirtualRegister))
1315 return error("expected either a named or virtual register");
1316
1317 VRegInfo *Info;
1318 if (parseRegister(Reg, Info))
1319 return true;
1320
1321 lex();
1322 if (Token.isNot(MIToken::Eof))
1323 return error("expected end of string after the register reference");
1324 return false;
1325}
1326
1327bool MIParser::parseStandaloneStackObject(int &FI) {
1328 lex();
1329 if (Token.isNot(MIToken::StackObject))
1330 return error("expected a stack object");
1331 if (parseStackFrameIndex(FI))
1332 return true;
1333 if (Token.isNot(MIToken::Eof))
1334 return error("expected end of string after the stack object reference");
1335 return false;
1336}
1337
1338bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1339 lex();
1340 if (Token.is(MIToken::exclaim)) {
1341 if (parseMDNode(Node))
1342 return true;
1343 } else if (Token.is(MIToken::md_diexpr)) {
1344 if (parseDIExpression(Node))
1345 return true;
1346 } else if (Token.is(MIToken::md_dilocation)) {
1347 if (parseDILocation(Node))
1348 return true;
1349 } else {
1350 return error("expected a metadata node");
1351 }
1352 if (Token.isNot(MIToken::Eof))
1353 return error("expected end of string after the metadata node");
1354 return false;
1355}
1356
1357static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1358 assert(MO.isImplicit());
1359 return MO.isDef() ? "implicit-def" : "implicit";
1360}
1361
1362static std::string getRegisterName(const TargetRegisterInfo *TRI,
1363 Register Reg) {
1364 assert(Reg.isPhysical() && "expected phys reg");
1365 return StringRef(TRI->getName(Reg)).lower();
1366}
1367
1368/// Return true if the parsed machine operands contain a given machine operand.
1369static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1371 for (const auto &I : Operands) {
1372 if (ImplicitOperand.isIdenticalTo(I.Operand))
1373 return true;
1374 }
1375 return false;
1376}
1377
1378bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1379 const MCInstrDesc &MCID) {
1380 if (MCID.isCall())
1381 // We can't verify call instructions as they can contain arbitrary implicit
1382 // register and register mask operands.
1383 return false;
1384
1385 // Gather all the expected implicit operands.
1386 SmallVector<MachineOperand, 4> ImplicitOperands;
1387 for (MCPhysReg ImpDef : MCID.implicit_defs())
1388 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1389 for (MCPhysReg ImpUse : MCID.implicit_uses())
1390 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1391
1392 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1393 assert(TRI && "Expected target register info");
1394 for (const auto &I : ImplicitOperands) {
1396 continue;
1397 return error(Operands.empty() ? Token.location() : Operands.back().End,
1398 Twine("missing implicit register operand '") +
1400 getRegisterName(TRI, I.getReg()) + "'");
1401 }
1402 return false;
1403}
1404
1405bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1406 // Allow frame and fast math flags for OPCODE
1407 // clang-format off
1408 while (Token.is(MIToken::kw_frame_setup) ||
1409 Token.is(MIToken::kw_frame_destroy) ||
1410 Token.is(MIToken::kw_nnan) ||
1411 Token.is(MIToken::kw_ninf) ||
1412 Token.is(MIToken::kw_nsz) ||
1413 Token.is(MIToken::kw_arcp) ||
1414 Token.is(MIToken::kw_contract) ||
1415 Token.is(MIToken::kw_afn) ||
1416 Token.is(MIToken::kw_reassoc) ||
1417 Token.is(MIToken::kw_nuw) ||
1418 Token.is(MIToken::kw_nsw) ||
1419 Token.is(MIToken::kw_exact) ||
1420 Token.is(MIToken::kw_nofpexcept) ||
1421 Token.is(MIToken::kw_noconvergent) ||
1422 Token.is(MIToken::kw_unpredictable) ||
1423 Token.is(MIToken::kw_nneg) ||
1424 Token.is(MIToken::kw_disjoint) ||
1425 Token.is(MIToken::kw_nusw) ||
1426 Token.is(MIToken::kw_samesign) ||
1427 Token.is(MIToken::kw_inbounds) ||
1428 Token.is(MIToken::kw_nonnull) ||
1429 Token.is(MIToken::kw_lr_split)) {
1430 // clang-format on
1431 // Mine frame and fast math flags
1432 if (Token.is(MIToken::kw_frame_setup))
1434 if (Token.is(MIToken::kw_frame_destroy))
1436 if (Token.is(MIToken::kw_nnan))
1438 if (Token.is(MIToken::kw_ninf))
1440 if (Token.is(MIToken::kw_nsz))
1442 if (Token.is(MIToken::kw_arcp))
1444 if (Token.is(MIToken::kw_contract))
1446 if (Token.is(MIToken::kw_afn))
1448 if (Token.is(MIToken::kw_reassoc))
1450 if (Token.is(MIToken::kw_nuw))
1452 if (Token.is(MIToken::kw_nsw))
1454 if (Token.is(MIToken::kw_exact))
1456 if (Token.is(MIToken::kw_nofpexcept))
1458 if (Token.is(MIToken::kw_unpredictable))
1460 if (Token.is(MIToken::kw_noconvergent))
1462 if (Token.is(MIToken::kw_nneg))
1464 if (Token.is(MIToken::kw_disjoint))
1466 if (Token.is(MIToken::kw_nusw))
1468 if (Token.is(MIToken::kw_samesign))
1470 if (Token.is(MIToken::kw_inbounds))
1472 if (Token.is(MIToken::kw_nonnull))
1474 if (Token.is(MIToken::kw_lr_split))
1476
1477 lex();
1478 }
1479 if (Token.isNot(MIToken::Identifier))
1480 return error("expected a machine instruction");
1481 StringRef InstrName = Token.stringValue();
1482 if (PFS.Target.parseInstrName(InstrName, OpCode))
1483 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1484 lex();
1485 return false;
1486}
1487
1488bool MIParser::parseNamedRegister(Register &Reg) {
1489 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1490 StringRef Name = Token.stringValue();
1491 if (PFS.Target.getRegisterByName(Name, Reg))
1492 return error(Twine("unknown register name '") + Name + "'");
1493 return false;
1494}
1495
1496bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1497 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1498 StringRef Name = Token.stringValue();
1499 // TODO: Check that the VReg name is not the same as a physical register name.
1500 // If it is, then print a warning (when warnings are implemented).
1501 Info = &PFS.getVRegInfoNamed(Name);
1502 return false;
1503}
1504
1505bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1506 if (Token.is(MIToken::NamedVirtualRegister))
1507 return parseNamedVirtualRegister(Info);
1508 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1509 unsigned ID;
1510 if (getUnsigned(ID))
1511 return true;
1512 Info = &PFS.getVRegInfo(ID);
1513 return false;
1514}
1515
1516bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1517 switch (Token.kind()) {
1519 Reg = 0;
1520 return false;
1522 return parseNamedRegister(Reg);
1525 if (parseVirtualRegister(Info))
1526 return true;
1527 Reg = Info->VReg;
1528 return false;
1529 // TODO: Parse other register kinds.
1530 default:
1531 llvm_unreachable("The current token should be a register");
1532 }
1533}
1534
1535bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1536 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1537 return error("expected '_', register class, or register bank name");
1538 StringRef::iterator Loc = Token.location();
1539 StringRef Name = Token.stringValue();
1540
1541 // Was it a register class?
1542 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1543 if (RC) {
1544 lex();
1545
1546 switch (RegInfo.Kind) {
1547 case VRegInfo::UNKNOWN:
1548 case VRegInfo::NORMAL:
1549 RegInfo.Kind = VRegInfo::NORMAL;
1550 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1551 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1552 return error(Loc, Twine("conflicting register classes, previously: ") +
1553 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1554 }
1555 RegInfo.D.RC = RC;
1556 RegInfo.Explicit = true;
1557 return false;
1558
1559 case VRegInfo::GENERIC:
1560 case VRegInfo::REGBANK:
1561 return error(Loc, "register class specification on generic register");
1562 }
1563 llvm_unreachable("Unexpected register kind");
1564 }
1565
1566 // Should be a register bank or a generic register.
1567 const RegisterBank *RegBank = nullptr;
1568 if (Name != "_") {
1569 RegBank = PFS.Target.getRegBank(Name);
1570 if (!RegBank)
1571 return error(Loc, "expected '_', register class, or register bank name");
1572 }
1573
1574 lex();
1575
1576 switch (RegInfo.Kind) {
1577 case VRegInfo::UNKNOWN:
1578 case VRegInfo::GENERIC:
1579 case VRegInfo::REGBANK:
1580 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1581 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1582 return error(Loc, "conflicting generic register banks");
1583 RegInfo.D.RegBank = RegBank;
1584 RegInfo.Explicit = true;
1585 return false;
1586
1587 case VRegInfo::NORMAL:
1588 return error(Loc, "register bank specification on normal register");
1589 }
1590 llvm_unreachable("Unexpected register kind");
1591}
1592
1593bool MIParser::parseRegisterFlag(RegState &Flags) {
1594 const RegState OldFlags = Flags;
1595 switch (Token.kind()) {
1598 break;
1601 break;
1602 case MIToken::kw_def:
1604 break;
1605 case MIToken::kw_dead:
1607 break;
1608 case MIToken::kw_killed:
1610 break;
1611 case MIToken::kw_undef:
1613 break;
1616 break;
1619 break;
1622 break;
1625 break;
1626 default:
1627 llvm_unreachable("The current token should be a register flag");
1628 }
1629 if (OldFlags == Flags)
1630 // We know that the same flag is specified more than once when the flags
1631 // weren't modified.
1632 return error("duplicate '" + Token.stringValue() + "' register flag");
1633 lex();
1634 return false;
1635}
1636
1637bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1638 assert(Token.is(MIToken::dot));
1639 lex();
1640 if (Token.isNot(MIToken::Identifier))
1641 return error("expected a subregister index after '.'");
1642 auto Name = Token.stringValue();
1643 SubReg = PFS.Target.getSubRegIndex(Name);
1644 if (!SubReg)
1645 return error(Twine("use of unknown subregister index '") + Name + "'");
1646 lex();
1647 return false;
1648}
1649
1650bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1651 assert(Token.is(MIToken::kw_tied_def));
1652 lex();
1653 if (Token.isNot(MIToken::IntegerLiteral))
1654 return error("expected an integer literal after 'tied-def'");
1655 if (getUnsigned(TiedDefIdx))
1656 return true;
1657 lex();
1658 return expectAndConsume(MIToken::rparen);
1659}
1660
1661bool MIParser::assignRegisterTies(MachineInstr &MI,
1663 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1664 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1665 if (!Operands[I].TiedDefIdx)
1666 continue;
1667 // The parser ensures that this operand is a register use, so we just have
1668 // to check the tied-def operand.
1669 unsigned DefIdx = *Operands[I].TiedDefIdx;
1670 if (DefIdx >= E)
1671 return error(Operands[I].Begin,
1672 Twine("use of invalid tied-def operand index '" +
1673 Twine(DefIdx) + "'; instruction has only ") +
1674 Twine(E) + " operands");
1675 const auto &DefOperand = Operands[DefIdx].Operand;
1676 if (!DefOperand.isReg() || !DefOperand.isDef())
1677 // FIXME: add note with the def operand.
1678 return error(Operands[I].Begin,
1679 Twine("use of invalid tied-def operand index '") +
1680 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1681 " isn't a defined register");
1682 // Check that the tied-def operand wasn't tied elsewhere.
1683 for (const auto &TiedPair : TiedRegisterPairs) {
1684 if (TiedPair.first == DefIdx)
1685 return error(Operands[I].Begin,
1686 Twine("the tied-def operand #") + Twine(DefIdx) +
1687 " is already tied with another register operand");
1688 }
1689 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1690 }
1691 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1692 // indices must be less than tied max.
1693 for (const auto &TiedPair : TiedRegisterPairs)
1694 MI.tieOperands(TiedPair.first, TiedPair.second);
1695 return false;
1696}
1697
1698bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1699 std::optional<unsigned> &TiedDefIdx,
1700 bool IsDef) {
1701 RegState Flags = getDefRegState(IsDef);
1702 while (Token.isRegisterFlag()) {
1703 if (parseRegisterFlag(Flags))
1704 return true;
1705 }
1706 // Update IsDef as we may have read a def flag.
1707 IsDef = hasRegState(Flags, RegState::Define);
1708 if (!Token.isRegister())
1709 return error("expected a register after register flags");
1710 Register Reg;
1711 VRegInfo *RegInfo;
1712 if (parseRegister(Reg, RegInfo))
1713 return true;
1714 lex();
1715 unsigned SubReg = 0;
1716 if (Token.is(MIToken::dot)) {
1717 if (parseSubRegisterIndex(SubReg))
1718 return true;
1719 if (!Reg.isVirtual())
1720 return error("subregister index expects a virtual register");
1721 }
1722 if (Token.is(MIToken::colon)) {
1723 if (!Reg.isVirtual())
1724 return error("register class specification expects a virtual register");
1725 lex();
1726 if (parseRegisterClassOrBank(*RegInfo))
1727 return true;
1728 }
1729
1730 if (consumeIfPresent(MIToken::lparen)) {
1731 // For a def, we only expect a type. For use we expect either a type or a
1732 // tied-def. Additionally, for physical registers, we don't expect a type.
1733 if (Token.is(MIToken::kw_tied_def)) {
1734 if (IsDef)
1735 return error("tied-def not supported for defs");
1736 unsigned Idx;
1737 if (parseRegisterTiedDefIndex(Idx))
1738 return true;
1739 TiedDefIdx = Idx;
1740 } else {
1741 if (!Reg.isVirtual())
1742 return error("unexpected type on physical register");
1743
1744 LLT Ty;
1745 // If type parsing fails, forwad the parse error for defs.
1746 if (parseLowLevelType(Token.location(), Ty))
1747 return IsDef ? true
1748 : error("expected tied-def or low-level type after '('");
1749
1750 if (expectAndConsume(MIToken::rparen))
1751 return true;
1752
1753 MachineRegisterInfo &MRI = MF.getRegInfo();
1754 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1755 return error("inconsistent type for generic virtual register");
1756
1757 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1758 MRI.setType(Reg, Ty);
1760 }
1761 } else if (IsDef && Reg.isVirtual()) {
1762 // Generic virtual registers defs must have a type.
1763 if (RegInfo->Kind == VRegInfo::GENERIC ||
1764 RegInfo->Kind == VRegInfo::REGBANK)
1765 return error("generic virtual registers must have a type");
1766 }
1767
1768 if (IsDef) {
1769 if (hasRegState(Flags, RegState::Kill))
1770 return error("cannot have a killed def operand");
1771 } else {
1772 if (hasRegState(Flags, RegState::Dead))
1773 return error("cannot have a dead use operand");
1774 }
1775
1777 Reg, IsDef, hasRegState(Flags, RegState::Implicit),
1780 hasRegState(Flags, RegState::EarlyClobber), SubReg,
1784
1785 return false;
1786}
1787
1788bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1790 const APSInt &Int = Token.integerValue();
1791 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1792 Dest = MachineOperand::CreateImm(*SImm);
1793 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1794 Dest = MachineOperand::CreateImm(*UImm);
1795 else
1796 return error("integer literal is too large to be an immediate operand");
1797 lex();
1798 return false;
1799}
1800
1801bool MIParser::parseSymbolicInlineAsmOperand(unsigned OpIdx,
1802 MachineOperand &Dest) {
1804 assert(Token.is(MIToken::Identifier) &&
1805 "expected symbolic inline asm operand");
1806
1807 // Parse ExtraInfo flags.
1808 if (OpIdx == InlineAsm::MIOp_ExtraInfo) {
1809 unsigned ExtraInfo = 0;
1810 for (;;) {
1811 if (Token.isNot(MIToken::Identifier))
1812 break;
1813
1814 StringRef FlagName = Token.stringValue();
1815 unsigned Flag = StringSwitch<unsigned>(FlagName)
1817 .Case("mayload", InlineAsm::Extra_MayLoad)
1818 .Case("maystore", InlineAsm::Extra_MayStore)
1819 .Case("isconvergent", InlineAsm::Extra_IsConvergent)
1820 .Case("alignstack", InlineAsm::Extra_IsAlignStack)
1822 .Case("attdialect", 0)
1823 .Case("inteldialect", InlineAsm::Extra_AsmDialect)
1824 .Default(~0u);
1825 if (Flag == ~0u)
1826 return error("unknown inline asm extra info flag '" + FlagName + "'");
1827
1828 ExtraInfo |= Flag;
1829 lex();
1830 }
1831
1832 Dest = MachineOperand::CreateImm(ExtraInfo);
1833 return false;
1834 }
1835
1836 // Parse symbolic form: kind[:constraint].
1837 StringRef KindStr = Token.stringValue();
1838 constexpr auto InvalidKind = static_cast<InlineAsm::Kind>(0);
1841 .Case("regdef", InlineAsm::Kind::RegDef)
1842 .Case("reguse", InlineAsm::Kind::RegUse)
1844 .Case("clobber", InlineAsm::Kind::Clobber)
1845 .Case("imm", InlineAsm::Kind::Imm)
1846 .Case("mem", InlineAsm::Kind::Mem)
1847 .Default(InvalidKind);
1848 if (K == InvalidKind)
1849 return error("unknown inline asm operand kind '" + KindStr + "'");
1850
1851 lex();
1852
1853 // Create the flag with default of 1 operand.
1854 InlineAsm::Flag F(K, 1);
1855
1856 // Parse optional tiedto constraint: tiedto:$N.
1857 if (Token.is(MIToken::Identifier) && Token.stringValue() == "tiedto") {
1858 lex();
1859 if (Token.isNot(MIToken::colon))
1860 return error("expected ':' after 'tiedto'");
1861 lex();
1862 if (Token.isNot(MIToken::NamedRegister))
1863 return error("expected '$N' operand number after 'tiedto:'");
1864 unsigned OperandNo;
1865 if (Token.stringValue().getAsInteger(10, OperandNo))
1866 return error("invalid operand number in tiedto constraint");
1867 lex();
1868
1869 F.setMatchingOp(OperandNo);
1870
1872 return false;
1873 }
1874
1875 // Parse an optional constraint after ':'.
1876 if (Token.is(MIToken::colon)) {
1877 lex();
1878
1879 if (Token.isNot(MIToken::Identifier))
1880 return error(
1881 "expected register class or memory constraint name after ':'");
1882
1883 StringRef ConstraintStr = Token.stringValue();
1884 if (F.isMemKind()) {
1917 return error("unknown memory constraint '" + ConstraintStr + "'");
1918 F.setMemConstraint(CC);
1919 } else if (F.isRegDefKind() || F.isRegUseKind() ||
1920 F.isRegDefEarlyClobberKind()) {
1921 const TargetRegisterClass *RC =
1922 PFS.Target.getRegClass(ConstraintStr.lower());
1923 if (!RC)
1924 return error("unknown register class '" + ConstraintStr + "'");
1925 F.setRegClass(RC->getID());
1926 }
1927
1928 lex();
1929 }
1930
1931 // Parse an optional 'foldable' marker: the register allocator may fold the
1932 // operand's register to a stack slot (see
1933 // InlineAsm::Flag::setRegMayBeFolded()).
1934 if (Token.is(MIToken::Identifier) && Token.stringValue() == "foldable") {
1935 if (!F.isRegDefKind() && !F.isRegUseKind() && !F.isRegDefEarlyClobberKind())
1936 return error("only a register operand can be 'foldable'");
1937
1938 F.setRegMayBeFolded(true);
1939 lex();
1940 }
1941
1943 return false;
1944}
1945
1946bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1947 const unsigned OpIdx,
1948 MachineOperand &Dest,
1949 const MIRFormatter &MF) {
1950 assert(Token.is(MIToken::dot));
1951 auto Loc = Token.location(); // record start position
1952 size_t Len = 1; // for "."
1953 lex();
1954
1955 // Handle the case that mnemonic starts with number.
1956 if (Token.is(MIToken::IntegerLiteral)) {
1957 Len += Token.range().size();
1958 lex();
1959 }
1960
1961 StringRef Src;
1962 if (Token.is(MIToken::comma))
1963 Src = StringRef(Loc, Len);
1964 else {
1965 assert(Token.is(MIToken::Identifier));
1966 Src = StringRef(Loc, Len + Token.stringValue().size());
1967 }
1968 int64_t Val;
1969 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1970 [this](StringRef::iterator Loc, const Twine &Msg)
1971 -> bool { return error(Loc, Msg); }))
1972 return true;
1973
1974 Dest = MachineOperand::CreateImm(Val);
1975 if (!Token.is(MIToken::comma))
1976 lex();
1977 return false;
1978}
1979
1981 PerFunctionMIParsingState &PFS, const Constant *&C,
1982 ErrorCallbackType ErrCB) {
1983 auto Source = StringValue.str(); // The source has to be null terminated.
1984 SMDiagnostic Err;
1985 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1986 &PFS.IRSlots);
1987 if (!C)
1988 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1989 return false;
1990}
1991
1992bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1993 const Constant *&C) {
1994 return ::parseIRConstant(
1995 Loc, StringValue, PFS, C,
1996 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1997 return error(Loc, Msg);
1998 });
1999}
2000
2001bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
2002 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
2003 return true;
2004 lex();
2005 return false;
2006}
2007
2008// See LLT implementation for bit size limits.
2010 return Size != 0 && isUInt<16>(Size);
2011}
2012
2013static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale) {
2014 // A fixed-length vector needs at least two elements.
2015 return NumElts != 0 && (HasVScale || NumElts != 1) && isUInt<16>(NumElts);
2016}
2017
2018static bool verifyAddrSpace(uint64_t AddrSpace) {
2019 return isUInt<24>(AddrSpace);
2020}
2021
2022bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
2023 StringRef TypeDigits = Token.range();
2024 if (TypeDigits.consume_front("s") || TypeDigits.consume_front("i") ||
2025 TypeDigits.consume_front("f") || TypeDigits.consume_front("p") ||
2026 TypeDigits.consume_front("bf")) {
2027 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2028 return error(
2029 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2030 }
2031
2032 bool Scalar = Token.range().starts_with("s");
2033 if (Scalar || Token.range().starts_with("i")) {
2034 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2035 if (!ScalarSize) {
2036 Ty = LLT::token();
2037 lex();
2038 return false;
2039 }
2040
2041 if (!verifyScalarSize(ScalarSize))
2042 return error("invalid size for scalar type");
2043
2044 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2045 lex();
2046 return false;
2047 }
2048
2049 if (Token.range().starts_with("p")) {
2050 const DataLayout &DL = MF.getDataLayout();
2051 uint64_t AS = APSInt(TypeDigits).getZExtValue();
2052 if (!verifyAddrSpace(AS))
2053 return error("invalid address space number");
2054
2055 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2056 lex();
2057 return false;
2058 }
2059
2060 if (Token.range().starts_with("f") || Token.range().starts_with("bf")) {
2061 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2062 if (!ScalarSize || !verifyScalarSize(ScalarSize))
2063 return error("invalid size for scalar type");
2064
2065 if (Token.range().starts_with("bf") && ScalarSize != 16)
2066 return error("invalid size for bfloat");
2067
2068 Ty = Token.range().starts_with("bf") ? LLT::bfloat16()
2069 : LLT::floatIEEE(ScalarSize);
2070 lex();
2071 return false;
2072 }
2073
2074 // Now we're looking for a vector.
2075 if (Token.isNot(MIToken::less))
2076 return error(Loc, "expected tN, pA, <M x tN>, <M x pA>, <vscale x M x tN>, "
2077 "or <vscale x M x pA> for GlobalISel type, "
2078 "where t = {'s', 'i', 'f', 'bf'}");
2079 lex();
2080
2081 bool HasVScale =
2082 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2083 if (HasVScale) {
2084 lex();
2085 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2086 return error(
2087 "expected <vscale x M x tN>, where t = {'s', 'i', 'f', 'bf', 'p'}");
2088 lex();
2089 }
2090
2091 auto GetError = [this, &HasVScale, Loc]() {
2092 if (HasVScale)
2093 return error(Loc, "expected <vscale x M x tN> for vector type, where t = "
2094 "{'s', 'i', 'f', 'bf', 'p'}");
2095 return error(Loc, "expected <M x tN> for vector type, where t = {'s', 'i', "
2096 "'f', 'bf', 'p'}");
2097 };
2098
2099 if (Token.isNot(MIToken::IntegerLiteral))
2100 return GetError();
2101 uint64_t NumElements = Token.integerValue().getZExtValue();
2102 if (!verifyVectorElementCount(NumElements, HasVScale))
2103 return error("invalid number of vector elements");
2104
2105 lex();
2106
2107 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2108 return GetError();
2109 lex();
2110
2111 StringRef VectorTyDigits = Token.range();
2112 if (!VectorTyDigits.consume_front("s") &&
2113 !VectorTyDigits.consume_front("i") &&
2114 !VectorTyDigits.consume_front("f") &&
2115 !VectorTyDigits.consume_front("p") && !VectorTyDigits.consume_front("bf"))
2116 return GetError();
2117
2118 if (VectorTyDigits.empty() || !llvm::all_of(VectorTyDigits, isdigit))
2119 return error(
2120 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2121
2122 Scalar = Token.range().starts_with("s");
2123 if (Scalar || Token.range().starts_with("i")) {
2124 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2125 if (!verifyScalarSize(ScalarSize))
2126 return error("invalid size for scalar element in vector");
2127 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2128 } else if (Token.range().starts_with("p")) {
2129 const DataLayout &DL = MF.getDataLayout();
2130 uint64_t AS = APSInt(VectorTyDigits).getZExtValue();
2131 if (!verifyAddrSpace(AS))
2132 return error("invalid address space number");
2133
2134 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2135 } else if (Token.range().starts_with("f")) {
2136 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2137 if (!verifyScalarSize(ScalarSize))
2138 return error("invalid size for float element in vector");
2139 Ty = LLT::floatIEEE(ScalarSize);
2140 } else if (Token.range().starts_with("bf")) {
2141 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2142 if (!verifyScalarSize(ScalarSize))
2143 return error("invalid size for bfloat element in vector");
2144 Ty = LLT::bfloat16();
2145 } else {
2146 return GetError();
2147 }
2148 lex();
2149
2150 if (Token.isNot(MIToken::greater))
2151 return GetError();
2152
2153 lex();
2154
2155 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2156 return false;
2157}
2158
2159bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2160 assert(Token.is(MIToken::Identifier));
2161 StringRef TypeDigits = Token.range();
2162 if (!TypeDigits.consume_front("i") && !TypeDigits.consume_front("s") &&
2163 !TypeDigits.consume_front("p") && !TypeDigits.consume_front("f") &&
2164 !TypeDigits.consume_front("bf"))
2165 return error("a typed immediate operand should start with one of 'i', "
2166 "'s', 'f', 'bf', or 'p'");
2167 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2168 return error(
2169 "expected integers after 'i'/'s'/'f'/'bf'/'p' type identifier");
2170
2171 auto Loc = Token.location();
2172 lex();
2173 if (Token.isNot(MIToken::IntegerLiteral)) {
2174 if (Token.isNot(MIToken::Identifier) ||
2175 !(Token.range() == "true" || Token.range() == "false"))
2176 return error("expected an integer literal");
2177 }
2178 const Constant *C = nullptr;
2179 if (parseIRConstant(Loc, C))
2180 return true;
2182 return false;
2183}
2184
2185bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2186 auto Loc = Token.location();
2187 lex();
2188 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2189 Token.isNot(MIToken::HexLiteral))
2190 return error("expected a floating point literal");
2191 const Constant *C = nullptr;
2192 if (parseIRConstant(Loc, C))
2193 return true;
2195 return false;
2196}
2197
2198static bool getHexUint(const MIToken &Token, APInt &Result) {
2200 StringRef S = Token.range();
2201 assert(S[0] == '0' && tolower(S[1]) == 'x');
2202 // This could be a floating point literal with a special prefix.
2203 if (!isxdigit(S[2]))
2204 return true;
2205 StringRef V = S.substr(2);
2206 APInt A(V.size()*4, V, 16);
2207
2208 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2209 // sure it isn't the case before constructing result.
2210 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2211 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2212 return false;
2213}
2214
2215static bool getUnsigned(const MIToken &Token, unsigned &Result,
2216 ErrorCallbackType ErrCB) {
2217 if (Token.hasIntegerValue()) {
2218 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2219 const APSInt &SInt = Token.integerValue();
2220 if (SInt.isNegative())
2221 return ErrCB(Token.location(), "expected unsigned integer");
2222 uint64_t Val64 = SInt.getLimitedValue(Limit);
2223 if (Val64 == Limit)
2224 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2225 Result = Val64;
2226 return false;
2227 }
2228 if (Token.is(MIToken::HexLiteral)) {
2229 APInt A;
2230 if (getHexUint(Token, A))
2231 return true;
2232 if (A.getBitWidth() > 32)
2233 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2234 Result = A.getZExtValue();
2235 return false;
2236 }
2237 return true;
2238}
2239
2240bool MIParser::getUnsigned(unsigned &Result) {
2241 return ::getUnsigned(
2242 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2243 return error(Loc, Msg);
2244 });
2245}
2246
2247bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2250 unsigned Number;
2251 if (getUnsigned(Number))
2252 return true;
2253 auto MBBInfo = PFS.MBBSlots.find(Number);
2254 if (MBBInfo == PFS.MBBSlots.end())
2255 return error(Twine("use of undefined machine basic block #") +
2256 Twine(Number));
2257 MBB = MBBInfo->second;
2258 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2259 // we drop the <irname> from the bb.<id>.<irname> format.
2260 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2261 return error(Twine("the name of machine basic block #") + Twine(Number) +
2262 " isn't '" + Token.stringValue() + "'");
2263 return false;
2264}
2265
2266bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2269 return true;
2271 lex();
2272 return false;
2273}
2274
2275bool MIParser::parseStackFrameIndex(int &FI) {
2276 assert(Token.is(MIToken::StackObject));
2277 unsigned ID;
2278 if (getUnsigned(ID))
2279 return true;
2280 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2281 if (ObjectInfo == PFS.StackObjectSlots.end())
2282 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2283 "'");
2285 if (const auto *Alloca =
2286 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2287 Name = Alloca->getName();
2288 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2289 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2290 "' isn't '" + Token.stringValue() + "'");
2291 lex();
2292 FI = ObjectInfo->second;
2293 return false;
2294}
2295
2296bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2297 int FI;
2298 if (parseStackFrameIndex(FI))
2299 return true;
2300 Dest = MachineOperand::CreateFI(FI);
2301 return false;
2302}
2303
2304bool MIParser::parseFixedStackFrameIndex(int &FI) {
2306 unsigned ID;
2307 if (getUnsigned(ID))
2308 return true;
2309 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2310 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2311 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2312 Twine(ID) + "'");
2313 lex();
2314 FI = ObjectInfo->second;
2315 return false;
2316}
2317
2318bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2319 int FI;
2320 if (parseFixedStackFrameIndex(FI))
2321 return true;
2322 Dest = MachineOperand::CreateFI(FI);
2323 return false;
2324}
2325
2326static bool parseGlobalValue(const MIToken &Token,
2328 ErrorCallbackType ErrCB) {
2329 switch (Token.kind()) {
2331 const Module *M = PFS.MF.getFunction().getParent();
2332 GV = M->getNamedValue(Token.stringValue());
2333 if (!GV)
2334 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2335 Token.range() + "'");
2336 break;
2337 }
2338 case MIToken::GlobalValue: {
2339 unsigned GVIdx;
2340 if (getUnsigned(Token, GVIdx, ErrCB))
2341 return true;
2342 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2343 if (!GV)
2344 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2345 Twine(GVIdx) + "'");
2346 break;
2347 }
2348 default:
2349 llvm_unreachable("The current token should be a global value");
2350 }
2351 return false;
2352}
2353
2354bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2355 return ::parseGlobalValue(
2356 Token, PFS, GV,
2357 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2358 return error(Loc, Msg);
2359 });
2360}
2361
2362bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2363 GlobalValue *GV = nullptr;
2364 if (parseGlobalValue(GV))
2365 return true;
2366 lex();
2367 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2368 if (parseOperandsOffset(Dest))
2369 return true;
2370 return false;
2371}
2372
2373bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2375 unsigned ID;
2376 if (getUnsigned(ID))
2377 return true;
2378 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2379 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2380 return error("use of undefined constant '%const." + Twine(ID) + "'");
2381 lex();
2382 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2383 if (parseOperandsOffset(Dest))
2384 return true;
2385 return false;
2386}
2387
2388bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2390 unsigned ID;
2391 if (getUnsigned(ID))
2392 return true;
2393 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2394 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2395 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2396 lex();
2397 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2398 return false;
2399}
2400
2401bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2403 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2404 lex();
2405 Dest = MachineOperand::CreateES(Symbol);
2406 if (parseOperandsOffset(Dest))
2407 return true;
2408 return false;
2409}
2410
2411bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2412 assert(Token.is(MIToken::MCSymbol));
2413 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2414 lex();
2415 Dest = MachineOperand::CreateMCSymbol(Symbol);
2416 if (parseOperandsOffset(Dest))
2417 return true;
2418 return false;
2419}
2420
2421bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2423 StringRef Name = Token.stringValue();
2424 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2425 if (SubRegIndex == 0)
2426 return error(Twine("unknown subregister index '") + Name + "'");
2427 lex();
2428 Dest = MachineOperand::CreateImm(SubRegIndex);
2429 return false;
2430}
2431
2432bool MIParser::parseMDNode(MDNode *&Node) {
2433 assert(Token.is(MIToken::exclaim));
2434
2435 auto Loc = Token.location();
2436 lex();
2437 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2438 return error("expected metadata id after '!'");
2439 unsigned ID;
2440 if (getUnsigned(ID))
2441 return true;
2442 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2443 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2444 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2445 if (NodeInfo == PFS.MachineMetadataNodes.end())
2446 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2447 }
2448 lex();
2449 Node = NodeInfo->second.get();
2450 return false;
2451}
2452
2453bool MIParser::parseDIExpression(MDNode *&Expr) {
2454 unsigned Read;
2456 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2457 &PFS.IRSlots);
2458 CurrentSource = CurrentSource.substr(Read);
2459 lex();
2460 if (!Expr)
2461 return error(Error.getMessage());
2462 return false;
2463}
2464
2465bool MIParser::parseDILocation(MDNode *&Loc) {
2466 assert(Token.is(MIToken::md_dilocation));
2467 lex();
2468
2469 bool HaveLine = false;
2470 unsigned Line = 0;
2471 unsigned Column = 0;
2472 MDNode *Scope = nullptr;
2473 MDNode *InlinedAt = nullptr;
2474 bool ImplicitCode = false;
2475 uint64_t AtomGroup = 0;
2476 uint64_t AtomRank = 0;
2477 MDNode *IRLayers = nullptr;
2478
2479 if (expectAndConsume(MIToken::lparen))
2480 return true;
2481
2482 if (Token.isNot(MIToken::rparen)) {
2483 do {
2484 if (Token.is(MIToken::Identifier)) {
2485 if (Token.stringValue() == "line") {
2486 lex();
2487 if (expectAndConsume(MIToken::colon))
2488 return true;
2489 if (Token.isNot(MIToken::IntegerLiteral) ||
2490 Token.integerValue().isSigned())
2491 return error("expected unsigned integer");
2492 Line = Token.integerValue().getZExtValue();
2493 HaveLine = true;
2494 lex();
2495 continue;
2496 }
2497 if (Token.stringValue() == "column") {
2498 lex();
2499 if (expectAndConsume(MIToken::colon))
2500 return true;
2501 if (Token.isNot(MIToken::IntegerLiteral) ||
2502 Token.integerValue().isSigned())
2503 return error("expected unsigned integer");
2504 Column = Token.integerValue().getZExtValue();
2505 lex();
2506 continue;
2507 }
2508 if (Token.stringValue() == "scope") {
2509 lex();
2510 if (expectAndConsume(MIToken::colon))
2511 return true;
2512 if (parseMDNode(Scope))
2513 return error("expected metadata node");
2514 if (!isa<DIScope>(Scope))
2515 return error("expected DIScope node");
2516 continue;
2517 }
2518 if (Token.stringValue() == "inlinedAt") {
2519 lex();
2520 if (expectAndConsume(MIToken::colon))
2521 return true;
2522 if (Token.is(MIToken::exclaim)) {
2523 if (parseMDNode(InlinedAt))
2524 return true;
2525 } else if (Token.is(MIToken::md_dilocation)) {
2526 if (parseDILocation(InlinedAt))
2527 return true;
2528 } else {
2529 return error("expected metadata node");
2530 }
2531 if (!isa<DILocation>(InlinedAt))
2532 return error("expected DILocation node");
2533 continue;
2534 }
2535 if (Token.stringValue() == "isImplicitCode") {
2536 lex();
2537 if (expectAndConsume(MIToken::colon))
2538 return true;
2539 if (!Token.is(MIToken::Identifier))
2540 return error("expected true/false");
2541 // As far as I can see, we don't have any existing need for parsing
2542 // true/false in MIR yet. Do it ad-hoc until there's something else
2543 // that needs it.
2544 if (Token.stringValue() == "true")
2545 ImplicitCode = true;
2546 else if (Token.stringValue() == "false")
2547 ImplicitCode = false;
2548 else
2549 return error("expected true/false");
2550 lex();
2551 continue;
2552 }
2553 if (Token.stringValue() == "atomGroup") {
2554 lex();
2555 if (expectAndConsume(MIToken::colon))
2556 return true;
2557 if (Token.isNot(MIToken::IntegerLiteral) ||
2558 Token.integerValue().isSigned())
2559 return error("expected unsigned integer");
2560 AtomGroup = Token.integerValue().getZExtValue();
2561 lex();
2562 continue;
2563 }
2564 if (Token.stringValue() == "atomRank") {
2565 lex();
2566 if (expectAndConsume(MIToken::colon))
2567 return true;
2568 if (Token.isNot(MIToken::IntegerLiteral) ||
2569 Token.integerValue().isSigned())
2570 return error("expected unsigned integer");
2571 AtomRank = Token.integerValue().getZExtValue();
2572 lex();
2573 continue;
2574 }
2575 if (Token.stringValue() == "irlayers") {
2576 lex();
2577 if (expectAndConsume(MIToken::colon))
2578 return true;
2579 if (parseMDNode(IRLayers))
2580 return error("expected metadata node");
2581 if (!isa<DILayerLocList>(IRLayers))
2582 return error("expected DILayerLocList node");
2583 continue;
2584 }
2585 }
2586 return error(Twine("invalid DILocation argument '") +
2587 Token.stringValue() + "'");
2588 } while (consumeIfPresent(MIToken::comma));
2589 }
2590
2591 if (expectAndConsume(MIToken::rparen))
2592 return true;
2593
2594 if (!HaveLine)
2595 return error("DILocation requires line number");
2596 if (!Scope)
2597 return error("DILocation requires a scope");
2598
2599 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2600 InlinedAt, ImplicitCode, AtomGroup, AtomRank, IRLayers);
2601 return false;
2602}
2603
2604bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2605 MDNode *Node = nullptr;
2606 if (Token.is(MIToken::exclaim)) {
2607 if (parseMDNode(Node))
2608 return true;
2609 } else if (Token.is(MIToken::md_diexpr)) {
2610 if (parseDIExpression(Node))
2611 return true;
2612 }
2613 Dest = MachineOperand::CreateMetadata(Node);
2614 return false;
2615}
2616
2617bool MIParser::parseCFIOffset(int &Offset) {
2618 if (Token.isNot(MIToken::IntegerLiteral))
2619 return error("expected a cfi offset");
2620 if (Token.integerValue().getSignificantBits() > 32)
2621 return error("expected a 32 bit integer (the cfi offset is too large)");
2622 Offset = (int)Token.integerValue().getExtValue();
2623 lex();
2624 return false;
2625}
2626
2627bool MIParser::parseCFIUnsigned(unsigned &Value) {
2628 if (getUnsigned(Value))
2629 return true;
2630 lex();
2631 return false;
2632}
2633
2634bool MIParser::parseCFIRegister(unsigned &Reg) {
2635 if (Token.isNot(MIToken::NamedRegister))
2636 return error("expected a cfi register");
2637 Register LLVMReg;
2638 if (parseNamedRegister(LLVMReg))
2639 return true;
2640 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2641 assert(TRI && "Expected target register info");
2642 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2643 if (DwarfReg < 0)
2644 return error("invalid DWARF register");
2645 Reg = (unsigned)DwarfReg;
2646 lex();
2647 return false;
2648}
2649
2650bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2651 if (Token.isNot(MIToken::IntegerLiteral))
2652 return error("expected a cfi address space literal");
2653 if (Token.integerValue().isSigned())
2654 return error("expected an unsigned integer (cfi address space)");
2655 AddressSpace = Token.integerValue().getZExtValue();
2656 lex();
2657 return false;
2658}
2659
2660bool MIParser::parseCFIEscapeValues(std::string &Values) {
2661 do {
2662 if (Token.isNot(MIToken::HexLiteral))
2663 return error("expected a hexadecimal literal");
2664 unsigned Value;
2665 if (getUnsigned(Value))
2666 return true;
2667 if (Value > UINT8_MAX)
2668 return error("expected a 8-bit integer (too large)");
2669 Values.push_back(static_cast<uint8_t>(Value));
2670 lex();
2671 } while (consumeIfPresent(MIToken::comma));
2672 return false;
2673}
2674
2675bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2676 auto Kind = Token.kind();
2677 lex();
2678 int Offset;
2679 unsigned Reg;
2680 unsigned AddressSpace;
2681 unsigned CFIIndex;
2682 switch (Kind) {
2684 if (parseCFIRegister(Reg))
2685 return true;
2686 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2687 break;
2689 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2690 parseCFIOffset(Offset))
2691 return true;
2692 CFIIndex =
2693 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2694 break;
2696 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2697 parseCFIOffset(Offset))
2698 return true;
2699 CFIIndex = MF.addFrameInst(
2701 break;
2703 if (parseCFIRegister(Reg))
2704 return true;
2705 CFIIndex =
2706 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2707 break;
2709 if (parseCFIOffset(Offset))
2710 return true;
2711 CFIIndex =
2712 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2713 break;
2715 if (parseCFIOffset(Offset))
2716 return true;
2717 CFIIndex = MF.addFrameInst(
2719 break;
2721 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2722 parseCFIOffset(Offset))
2723 return true;
2724 CFIIndex =
2725 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2726 break;
2728 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2729 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2730 parseCFIAddressSpace(AddressSpace))
2731 return true;
2732 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2733 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2734 break;
2736 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2737 break;
2739 if (parseCFIRegister(Reg))
2740 return true;
2741 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2742 break;
2744 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2745 break;
2747 if (parseCFIRegister(Reg))
2748 return true;
2749 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2750 break;
2752 unsigned Reg2;
2753 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2754 parseCFIRegister(Reg2))
2755 return true;
2756
2757 CFIIndex =
2758 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2759 break;
2760 }
2762 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2763 break;
2765 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2766 break;
2768 CFIIndex =
2769 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2770 break;
2772 unsigned State;
2773 MCSymbol *PACSym = nullptr;
2774 if (parseCFIUnsigned(State) || expectAndConsume(MIToken::comma))
2775 return true;
2776 if (Token.is(MIToken::MCSymbol)) {
2777 PACSym = getOrCreateMCSymbol(Token.stringValue());
2778 lex();
2779 CFIIndex = MF.addFrameInst(
2780 MCCFIInstruction::createSetRAState(nullptr, State, PACSym));
2781 } else if (Token.is(MIToken::IntegerLiteral)) {
2782 int Offset;
2783 if (parseCFIOffset(Offset))
2784 return true;
2785 CFIIndex = MF.addFrameInst(
2787 } else {
2788 return error("expected '<mcsymbol ...>' or integer offset for "
2789 "cfi_set_ra_state");
2790 }
2791 break;
2792 }
2794 unsigned Reg, R1, R2;
2795 unsigned R1Size, R2Size;
2796 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2797 parseCFIRegister(R1) || expectAndConsume(MIToken::comma) ||
2798 parseCFIUnsigned(R1Size) || expectAndConsume(MIToken::comma) ||
2799 parseCFIRegister(R2) || expectAndConsume(MIToken::comma) ||
2800 parseCFIUnsigned(R2Size))
2801 return true;
2802
2803 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMRegisterPair(
2804 nullptr, Reg, R1, R1Size, R2, R2Size));
2805 break;
2806 }
2808 std::vector<MCCFIInstruction::VectorRegisterWithLane> VectorRegisters;
2809 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma))
2810 return true;
2811 do {
2812 unsigned VR;
2813 unsigned Lane, Size;
2814 if (parseCFIRegister(VR) || expectAndConsume(MIToken::comma) ||
2815 parseCFIUnsigned(Lane) || expectAndConsume(MIToken::comma) ||
2816 parseCFIUnsigned(Size))
2817 return true;
2818 VectorRegisters.push_back({VR, Lane, Size});
2819 } while (consumeIfPresent(MIToken::comma));
2820
2821 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisters(
2822 nullptr, Reg, std::move(VectorRegisters)));
2823 break;
2824 }
2826 unsigned Reg, MaskReg;
2827 unsigned RegSize, MaskRegSize;
2828 int Offset = 0;
2829
2830 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2831 parseCFIUnsigned(RegSize) || expectAndConsume(MIToken::comma) ||
2832 parseCFIRegister(MaskReg) || expectAndConsume(MIToken::comma) ||
2833 parseCFIUnsigned(MaskRegSize) || expectAndConsume(MIToken::comma) ||
2834 parseCFIOffset(Offset))
2835 return true;
2836
2837 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorOffset(
2838 nullptr, Reg, RegSize, MaskReg, MaskRegSize, Offset));
2839 break;
2840 }
2842 unsigned Reg, SpillReg, MaskReg;
2843 unsigned SpillRegLaneSize, MaskRegSize;
2844
2845 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2846 parseCFIRegister(SpillReg) || expectAndConsume(MIToken::comma) ||
2847 parseCFIUnsigned(SpillRegLaneSize) ||
2848 expectAndConsume(MIToken::comma) || parseCFIRegister(MaskReg) ||
2849 expectAndConsume(MIToken::comma) || parseCFIUnsigned(MaskRegSize))
2850 return true;
2851
2852 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisterMask(
2853 nullptr, Reg, SpillReg, SpillRegLaneSize, MaskReg, MaskRegSize));
2854 break;
2855 }
2857 std::string Values;
2858 if (parseCFIEscapeValues(Values))
2859 return true;
2860 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2861 break;
2862 }
2863 default:
2864 // TODO: Parse the other CFI operands.
2865 llvm_unreachable("The current token should be a cfi operand");
2866 }
2867 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2868 return false;
2869}
2870
2871bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2872 switch (Token.kind()) {
2873 case MIToken::NamedIRBlock: {
2875 F.getValueSymbolTable()->lookup(Token.stringValue()));
2876 if (!BB)
2877 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2878 break;
2879 }
2880 case MIToken::IRBlock: {
2881 unsigned SlotNumber = 0;
2882 if (getUnsigned(SlotNumber))
2883 return true;
2884 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2885 if (!BB)
2886 return error(Twine("use of undefined IR block '%ir-block.") +
2887 Twine(SlotNumber) + "'");
2888 break;
2889 }
2890 default:
2891 llvm_unreachable("The current token should be an IR block reference");
2892 }
2893 return false;
2894}
2895
2896bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2898 lex();
2899 if (expectAndConsume(MIToken::lparen))
2900 return true;
2901 if (Token.isNot(MIToken::GlobalValue) &&
2902 Token.isNot(MIToken::NamedGlobalValue))
2903 return error("expected a global value");
2904 GlobalValue *GV = nullptr;
2905 if (parseGlobalValue(GV))
2906 return true;
2907 auto *F = dyn_cast<Function>(GV);
2908 if (!F)
2909 return error("expected an IR function reference");
2910 lex();
2911 if (expectAndConsume(MIToken::comma))
2912 return true;
2913 BasicBlock *BB = nullptr;
2914 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2915 return error("expected an IR block reference");
2916 if (parseIRBlock(BB, *F))
2917 return true;
2918 lex();
2919 if (expectAndConsume(MIToken::rparen))
2920 return true;
2921 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2922 if (parseOperandsOffset(Dest))
2923 return true;
2924 return false;
2925}
2926
2927bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2928 assert(Token.is(MIToken::kw_intrinsic));
2929 lex();
2930 if (expectAndConsume(MIToken::lparen))
2931 return error("expected syntax intrinsic(@llvm.whatever)");
2932
2933 if (Token.isNot(MIToken::NamedGlobalValue))
2934 return error("expected syntax intrinsic(@llvm.whatever)");
2935
2936 std::string Name = std::string(Token.stringValue());
2937 lex();
2938
2939 if (expectAndConsume(MIToken::rparen))
2940 return error("expected ')' to terminate intrinsic name");
2941
2942 // Find out what intrinsic we're dealing with.
2944 if (ID == Intrinsic::not_intrinsic)
2945 return error("unknown intrinsic name");
2947
2948 return false;
2949}
2950
2951bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2952 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2953 bool IsFloat = Token.is(MIToken::kw_floatpred);
2954 lex();
2955
2956 if (expectAndConsume(MIToken::lparen))
2957 return error("expected syntax intpred(whatever) or floatpred(whatever");
2958
2959 if (Token.isNot(MIToken::Identifier))
2960 return error("whatever");
2961
2962 CmpInst::Predicate Pred;
2963 if (IsFloat) {
2964 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2965 .Case("false", CmpInst::FCMP_FALSE)
2966 .Case("oeq", CmpInst::FCMP_OEQ)
2967 .Case("ogt", CmpInst::FCMP_OGT)
2968 .Case("oge", CmpInst::FCMP_OGE)
2969 .Case("olt", CmpInst::FCMP_OLT)
2970 .Case("ole", CmpInst::FCMP_OLE)
2971 .Case("one", CmpInst::FCMP_ONE)
2972 .Case("ord", CmpInst::FCMP_ORD)
2973 .Case("uno", CmpInst::FCMP_UNO)
2974 .Case("ueq", CmpInst::FCMP_UEQ)
2975 .Case("ugt", CmpInst::FCMP_UGT)
2976 .Case("uge", CmpInst::FCMP_UGE)
2977 .Case("ult", CmpInst::FCMP_ULT)
2978 .Case("ule", CmpInst::FCMP_ULE)
2979 .Case("une", CmpInst::FCMP_UNE)
2980 .Case("true", CmpInst::FCMP_TRUE)
2982 if (!CmpInst::isFPPredicate(Pred))
2983 return error("invalid floating-point predicate");
2984 } else {
2985 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2986 .Case("eq", CmpInst::ICMP_EQ)
2987 .Case("ne", CmpInst::ICMP_NE)
2988 .Case("sgt", CmpInst::ICMP_SGT)
2989 .Case("sge", CmpInst::ICMP_SGE)
2990 .Case("slt", CmpInst::ICMP_SLT)
2991 .Case("sle", CmpInst::ICMP_SLE)
2992 .Case("ugt", CmpInst::ICMP_UGT)
2993 .Case("uge", CmpInst::ICMP_UGE)
2994 .Case("ult", CmpInst::ICMP_ULT)
2995 .Case("ule", CmpInst::ICMP_ULE)
2997 if (!CmpInst::isIntPredicate(Pred))
2998 return error("invalid integer predicate");
2999 }
3000
3001 lex();
3003 if (expectAndConsume(MIToken::rparen))
3004 return error("predicate should be terminated by ')'.");
3005
3006 return false;
3007}
3008
3009bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
3011
3012 lex();
3013 if (expectAndConsume(MIToken::lparen))
3014 return error("expected syntax shufflemask(<integer or undef>, ...)");
3015
3016 SmallVector<int, 32> ShufMask;
3017 do {
3018 if (Token.is(MIToken::kw_undef)) {
3019 ShufMask.push_back(-1);
3020 } else if (Token.is(MIToken::IntegerLiteral)) {
3021 const APSInt &Int = Token.integerValue();
3022 ShufMask.push_back(Int.getExtValue());
3023 } else {
3024 return error("expected integer constant");
3025 }
3026
3027 lex();
3028 } while (consumeIfPresent(MIToken::comma));
3029
3030 if (expectAndConsume(MIToken::rparen))
3031 return error("shufflemask should be terminated by ')'.");
3032
3033 if (ShufMask.size() < 2)
3034 return error("shufflemask should have > 1 element");
3035
3036 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
3037 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
3038 return false;
3039}
3040
3041bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
3043
3044 lex();
3045 if (expectAndConsume(MIToken::lparen))
3046 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3047
3048 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3049 return error("expected unsigned integer for instruction index");
3050 uint64_t InstrIdx = Token.integerValue().getZExtValue();
3051 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
3052 "Instruction reference's instruction index is too large");
3053 lex();
3054
3055 if (expectAndConsume(MIToken::comma))
3056 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3057
3058 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3059 return error("expected unsigned integer for operand index");
3060 uint64_t OpIdx = Token.integerValue().getZExtValue();
3061 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
3062 "Instruction reference's operand index is too large");
3063 lex();
3064
3065 if (expectAndConsume(MIToken::rparen))
3066 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3067
3068 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
3069 return false;
3070}
3071
3072bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
3074 lex();
3075 if (expectAndConsume(MIToken::lparen))
3076 return true;
3077 if (Token.isNot(MIToken::Identifier))
3078 return error("expected the name of the target index");
3079 int Index = 0;
3080 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
3081 return error("use of undefined target index '" + Token.stringValue() + "'");
3082 lex();
3083 if (expectAndConsume(MIToken::rparen))
3084 return true;
3085 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
3086 if (parseOperandsOffset(Dest))
3087 return true;
3088 return false;
3089}
3090
3091bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
3092 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
3093 lex();
3094 if (expectAndConsume(MIToken::lparen))
3095 return true;
3096
3097 uint32_t *Mask = MF.allocateRegMask();
3098 do {
3099 if (Token.isNot(MIToken::rparen)) {
3100 if (Token.isNot(MIToken::NamedRegister))
3101 return error("expected a named register");
3102 Register Reg;
3103 if (parseNamedRegister(Reg))
3104 return true;
3105 lex();
3106 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3107 }
3108
3109 // TODO: Report an error if the same register is used more than once.
3110 } while (consumeIfPresent(MIToken::comma));
3111
3112 if (expectAndConsume(MIToken::rparen))
3113 return true;
3114 Dest = MachineOperand::CreateRegMask(Mask);
3115 return false;
3116}
3117
3118bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
3119 assert(Token.is(MIToken::kw_lanemask));
3120
3121 lex();
3122 if (expectAndConsume(MIToken::lparen))
3123 return true;
3124
3125 // Parse lanemask.
3126 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
3127 return error("expected a valid lane mask value");
3128 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
3129 "Use correct get-function for lane mask.");
3131 if (getUint64(V))
3132 return true;
3133 LaneBitmask LaneMask(V);
3134 lex();
3135
3136 if (expectAndConsume(MIToken::rparen))
3137 return true;
3138
3139 Dest = MachineOperand::CreateLaneMask(LaneMask);
3140 return false;
3141}
3142
3143bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
3144 assert(Token.is(MIToken::kw_liveout));
3145 uint32_t *Mask = MF.allocateRegMask();
3146 lex();
3147 if (expectAndConsume(MIToken::lparen))
3148 return true;
3149 while (true) {
3150 if (Token.isNot(MIToken::NamedRegister))
3151 return error("expected a named register");
3152 Register Reg;
3153 if (parseNamedRegister(Reg))
3154 return true;
3155 lex();
3156 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3157 // TODO: Report an error if the same register is used more than once.
3158 if (Token.isNot(MIToken::comma))
3159 break;
3160 lex();
3161 }
3162 if (expectAndConsume(MIToken::rparen))
3163 return true;
3165 return false;
3166}
3167
3168bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
3169 MachineOperand &Dest,
3170 std::optional<unsigned> &TiedDefIdx) {
3171 switch (Token.kind()) {
3174 case MIToken::kw_def:
3175 case MIToken::kw_dead:
3176 case MIToken::kw_killed:
3177 case MIToken::kw_undef:
3186 return parseRegisterOperand(Dest, TiedDefIdx);
3188 // TODO: Forbid numeric operands for INLINEASM once the transition to the
3189 // symbolic form is over.
3190 return parseImmediateOperand(Dest);
3191 case MIToken::kw_half:
3192 case MIToken::kw_bfloat:
3193 case MIToken::kw_float:
3194 case MIToken::kw_double:
3196 case MIToken::kw_fp128:
3198 return parseFPImmediateOperand(Dest);
3200 return parseMBBOperand(Dest);
3202 return parseStackObjectOperand(Dest);
3204 return parseFixedStackObjectOperand(Dest);
3207 return parseGlobalAddressOperand(Dest);
3209 return parseConstantPoolIndexOperand(Dest);
3211 return parseJumpTableIndexOperand(Dest);
3213 return parseExternalSymbolOperand(Dest);
3214 case MIToken::MCSymbol:
3215 return parseMCSymbolOperand(Dest);
3217 return parseSubRegisterIndexOperand(Dest);
3218 case MIToken::md_diexpr:
3219 case MIToken::exclaim:
3220 return parseMetadataOperand(Dest);
3243 return parseCFIOperand(Dest);
3245 return parseBlockAddressOperand(Dest);
3247 return parseIntrinsicOperand(Dest);
3249 return parseTargetIndexOperand(Dest);
3251 return parseLaneMaskOperand(Dest);
3253 return parseLiveoutRegisterMaskOperand(Dest);
3256 return parsePredicateOperand(Dest);
3258 return parseShuffleMaskOperand(Dest);
3260 return parseDbgInstrRefOperand(Dest);
3261 case MIToken::Error:
3262 return true;
3263 case MIToken::Identifier: {
3264 bool IsInlineAsm = OpCode == TargetOpcode::INLINEASM ||
3265 OpCode == TargetOpcode::INLINEASM_BR;
3266 if (IsInlineAsm)
3267 return parseSymbolicInlineAsmOperand(OpIdx, Dest);
3268
3269 StringRef Id = Token.stringValue();
3270 if (const auto *RegMask = PFS.Target.getRegMask(Id)) {
3271 Dest = MachineOperand::CreateRegMask(RegMask);
3272 lex();
3273 break;
3274 } else if (Id == "CustomRegMask") {
3275 return parseCustomRegisterMaskOperand(Dest);
3276 } else {
3277 return parseTypedImmediateOperand(Dest);
3278 }
3279 }
3280 case MIToken::dot: {
3281 const auto *TII = MF.getSubtarget().getInstrInfo();
3282 if (const auto *Formatter = TII->getMIRFormatter()) {
3283 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3284 }
3285 [[fallthrough]];
3286 }
3287 default:
3288 // FIXME: Parse the MCSymbol machine operand.
3289 return error("expected a machine operand");
3290 }
3291 return false;
3292}
3293
3294bool MIParser::parseMachineOperandAndTargetFlags(
3295 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3296 std::optional<unsigned> &TiedDefIdx) {
3297 unsigned TF = 0;
3298 bool HasTargetFlags = false;
3299 if (Token.is(MIToken::kw_target_flags)) {
3300 HasTargetFlags = true;
3301 lex();
3302 if (expectAndConsume(MIToken::lparen))
3303 return true;
3304 if (Token.isNot(MIToken::Identifier))
3305 return error("expected the name of the target flag");
3306 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3307 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3308 return error("use of undefined target flag '" + Token.stringValue() +
3309 "'");
3310 }
3311 lex();
3312 while (Token.is(MIToken::comma)) {
3313 lex();
3314 if (Token.isNot(MIToken::Identifier))
3315 return error("expected the name of the target flag");
3316 unsigned BitFlag = 0;
3317 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3318 return error("use of undefined target flag '" + Token.stringValue() +
3319 "'");
3320 // TODO: Report an error when using a duplicate bit target flag.
3321 TF |= BitFlag;
3322 lex();
3323 }
3324 if (expectAndConsume(MIToken::rparen))
3325 return true;
3326 }
3327 auto Loc = Token.location();
3328 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3329 return true;
3330 if (!HasTargetFlags)
3331 return false;
3332 if (Dest.isReg())
3333 return error(Loc, "register operands can't have target flags");
3334 Dest.setTargetFlags(TF);
3335 return false;
3336}
3337
3338bool MIParser::parseOffset(int64_t &Offset) {
3339 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3340 return false;
3341 StringRef Sign = Token.range();
3342 bool IsNegative = Token.is(MIToken::minus);
3343 lex();
3344 if (Token.isNot(MIToken::IntegerLiteral))
3345 return error("expected an integer literal after '" + Sign + "'");
3346 if (Token.integerValue().getSignificantBits() > 64)
3347 return error("expected 64-bit integer (too large)");
3348 Offset = Token.integerValue().getExtValue();
3349 if (IsNegative)
3350 Offset = -Offset;
3351 lex();
3352 return false;
3353}
3354
3355bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3357 lex();
3358 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3359 return error("expected basic block after 'ir_block_address_taken'");
3360
3361 if (parseIRBlock(BB, MF.getFunction()))
3362 return true;
3363
3364 lex();
3365 return false;
3366}
3367
3368bool MIParser::parseAlignment(uint64_t &Alignment) {
3369 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3370 lex();
3371 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3372 return error("expected an integer literal after 'align'");
3373 if (getUint64(Alignment))
3374 return true;
3375 lex();
3376
3377 if (!isPowerOf2_64(Alignment))
3378 return error("expected a power-of-2 literal after 'align'");
3379
3380 return false;
3381}
3382
3383bool MIParser::parseAddrspace(unsigned &Addrspace) {
3384 assert(Token.is(MIToken::kw_addrspace));
3385 lex();
3386 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3387 return error("expected an integer literal after 'addrspace'");
3388 if (getUnsigned(Addrspace))
3389 return true;
3390 lex();
3391 return false;
3392}
3393
3394bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3395 int64_t Offset = 0;
3396 if (parseOffset(Offset))
3397 return true;
3398 Op.setOffset(Offset);
3399 return false;
3400}
3401
3402static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3403 const Value *&V, ErrorCallbackType ErrCB) {
3404 switch (Token.kind()) {
3405 case MIToken::NamedIRValue: {
3406 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3407 break;
3408 }
3409 case MIToken::IRValue: {
3410 unsigned SlotNumber = 0;
3411 if (getUnsigned(Token, SlotNumber, ErrCB))
3412 return true;
3413 V = PFS.getIRValue(SlotNumber);
3414 break;
3415 }
3417 case MIToken::GlobalValue: {
3418 GlobalValue *GV = nullptr;
3419 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3420 return true;
3421 V = GV;
3422 break;
3423 }
3425 const Constant *C = nullptr;
3426 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3427 return true;
3428 V = C;
3429 break;
3430 }
3432 V = nullptr;
3433 return false;
3434 default:
3435 llvm_unreachable("The current token should be an IR block reference");
3436 }
3437 if (!V)
3438 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3439 return false;
3440}
3441
3442bool MIParser::parseIRValue(const Value *&V) {
3443 return ::parseIRValue(
3444 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3445 return error(Loc, Msg);
3446 });
3447}
3448
3449bool MIParser::getUint64(uint64_t &Result) {
3450 if (Token.hasIntegerValue()) {
3451 if (Token.integerValue().getActiveBits() > 64)
3452 return error("expected 64-bit integer (too large)");
3453 Result = Token.integerValue().getZExtValue();
3454 return false;
3455 }
3456 if (Token.is(MIToken::HexLiteral)) {
3457 APInt A;
3458 if (getHexUint(A))
3459 return true;
3460 if (A.getBitWidth() > 64)
3461 return error("expected 64-bit integer (too large)");
3462 Result = A.getZExtValue();
3463 return false;
3464 }
3465 return true;
3466}
3467
3468bool MIParser::getHexUint(APInt &Result) {
3469 return ::getHexUint(Token, Result);
3470}
3471
3472bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3473 const auto OldFlags = Flags;
3474 switch (Token.kind()) {
3477 break;
3480 break;
3483 break;
3486 break;
3489 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3490 return error("use of undefined target MMO flag '" + Token.stringValue() +
3491 "'");
3492 Flags |= TF;
3493 break;
3494 }
3495 default:
3496 llvm_unreachable("The current token should be a memory operand flag");
3497 }
3498 if (OldFlags == Flags)
3499 // We know that the same flag is specified more than once when the flags
3500 // weren't modified.
3501 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3502 lex();
3503 return false;
3504}
3505
3506bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3507 switch (Token.kind()) {
3508 case MIToken::kw_stack:
3509 PSV = MF.getPSVManager().getStack();
3510 break;
3511 case MIToken::kw_got:
3512 PSV = MF.getPSVManager().getGOT();
3513 break;
3515 PSV = MF.getPSVManager().getJumpTable();
3516 break;
3518 PSV = MF.getPSVManager().getConstantPool();
3519 break;
3521 int FI;
3522 if (parseFixedStackFrameIndex(FI))
3523 return true;
3524 PSV = MF.getPSVManager().getFixedStack(FI);
3525 // The token was already consumed, so use return here instead of break.
3526 return false;
3527 }
3528 case MIToken::StackObject: {
3529 int FI;
3530 if (parseStackFrameIndex(FI))
3531 return true;
3532 PSV = MF.getPSVManager().getFixedStack(FI);
3533 // The token was already consumed, so use return here instead of break.
3534 return false;
3535 }
3537 lex();
3538 switch (Token.kind()) {
3541 GlobalValue *GV = nullptr;
3542 if (parseGlobalValue(GV))
3543 return true;
3544 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3545 break;
3546 }
3548 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3549 MF.createExternalSymbolName(Token.stringValue()));
3550 break;
3551 default:
3552 return error(
3553 "expected a global value or an external symbol after 'call-entry'");
3554 }
3555 break;
3556 case MIToken::kw_custom: {
3557 lex();
3558 const auto *TII = MF.getSubtarget().getInstrInfo();
3559 if (const auto *Formatter = TII->getMIRFormatter()) {
3560 if (Formatter->parseCustomPseudoSourceValue(
3561 Token.stringValue(), MF, PFS, PSV,
3562 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3563 return error(Loc, Msg);
3564 }))
3565 return true;
3566 } else {
3567 return error("unable to parse target custom pseudo source value");
3568 }
3569 break;
3570 }
3571 default:
3572 llvm_unreachable("The current token should be pseudo source value");
3573 }
3574 lex();
3575 return false;
3576}
3577
3578bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3579 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3580 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3581 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3582 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3583 const PseudoSourceValue *PSV = nullptr;
3584 if (parseMemoryPseudoSourceValue(PSV))
3585 return true;
3586 int64_t Offset = 0;
3587 if (parseOffset(Offset))
3588 return true;
3589 Dest = MachinePointerInfo(PSV, Offset);
3590 return false;
3591 }
3592 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3593 Token.isNot(MIToken::GlobalValue) &&
3594 Token.isNot(MIToken::NamedGlobalValue) &&
3595 Token.isNot(MIToken::QuotedIRValue) &&
3596 Token.isNot(MIToken::kw_unknown_address))
3597 return error("expected an IR value reference");
3598 const Value *V = nullptr;
3599 if (parseIRValue(V))
3600 return true;
3601 if (V && !V->getType()->isPointerTy())
3602 return error("expected a pointer IR value");
3603 lex();
3604 int64_t Offset = 0;
3605 if (parseOffset(Offset))
3606 return true;
3607 Dest = MachinePointerInfo(V, Offset);
3608 return false;
3609}
3610
3611bool MIParser::parseOptionalScope(LLVMContext &Context,
3612 SyncScope::ID &SSID) {
3613 SSID = SyncScope::System;
3614 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3615 lex();
3616 if (expectAndConsume(MIToken::lparen))
3617 return error("expected '(' in syncscope");
3618
3619 std::string SSN;
3620 if (parseStringConstant(SSN))
3621 return true;
3622
3623 SSID = Context.getOrInsertSyncScopeID(SSN);
3624 if (expectAndConsume(MIToken::rparen))
3625 return error("expected ')' in syncscope");
3626 }
3627
3628 return false;
3629}
3630
3631bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3633 if (Token.isNot(MIToken::Identifier))
3634 return false;
3635
3636 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3637 .Case("unordered", AtomicOrdering::Unordered)
3638 .Case("monotonic", AtomicOrdering::Monotonic)
3639 .Case("acquire", AtomicOrdering::Acquire)
3640 .Case("release", AtomicOrdering::Release)
3644
3645 if (Order != AtomicOrdering::NotAtomic) {
3646 lex();
3647 return false;
3648 }
3649
3650 return error("expected an atomic scope, ordering or a size specification");
3651}
3652
3653bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3654 if (expectAndConsume(MIToken::lparen))
3655 return true;
3657 while (Token.isMemoryOperandFlag()) {
3658 if (parseMemoryOperandFlag(Flags))
3659 return true;
3660 }
3661 if (Token.isNot(MIToken::Identifier) ||
3662 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3663 return error("expected 'load' or 'store' memory operation");
3664 if (Token.stringValue() == "load")
3666 else
3668 lex();
3669
3670 // Optional 'store' for operands that both load and store.
3671 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3673 lex();
3674 }
3675
3676 // Optional synchronization scope.
3677 SyncScope::ID SSID;
3678 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3679 return true;
3680
3681 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3682 AtomicOrdering Order, FailureOrder;
3683 if (parseOptionalAtomicOrdering(Order))
3684 return true;
3685
3686 if (parseOptionalAtomicOrdering(FailureOrder))
3687 return true;
3688
3689 if (Token.isNot(MIToken::IntegerLiteral) &&
3690 Token.isNot(MIToken::kw_unknown_size) &&
3691 Token.isNot(MIToken::lparen))
3692 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3693 "memory operation");
3694
3696 if (Token.is(MIToken::IntegerLiteral)) {
3697 uint64_t Size;
3698 if (getUint64(Size))
3699 return true;
3700
3701 // Convert from bytes to bits for storage.
3703 lex();
3704 } else if (Token.is(MIToken::kw_unknown_size)) {
3705 lex();
3706 } else {
3707 if (expectAndConsume(MIToken::lparen))
3708 return true;
3709 if (parseLowLevelType(Token.location(), MemoryType))
3710 return true;
3711 if (expectAndConsume(MIToken::rparen))
3712 return true;
3713 }
3714
3716 if (Token.is(MIToken::Identifier)) {
3717 const char *Word =
3720 ? "on"
3721 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3722 if (Token.stringValue() != Word)
3723 return error(Twine("expected '") + Word + "'");
3724 lex();
3725
3726 if (parseMachinePointerInfo(Ptr))
3727 return true;
3728 }
3729 uint64_t BaseAlignment =
3730 MemoryType.isValid()
3731 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3732 : 1;
3733 AAMDNodes AAInfo;
3734 MDNode *Range = nullptr;
3735 MDNode *MemCacheHint = nullptr;
3736 while (consumeIfPresent(MIToken::comma)) {
3737 switch (Token.kind()) {
3738 case MIToken::kw_align: {
3739 // align is printed if it is different than size.
3741 if (parseAlignment(Alignment))
3742 return true;
3743 if (Ptr.Offset & (Alignment - 1)) {
3744 // MachineMemOperand::getAlign never returns a value greater than the
3745 // alignment of offset, so this just guards against hand-written MIR
3746 // that specifies a large "align" value when it should probably use
3747 // "basealign" instead.
3748 return error("specified alignment is more aligned than offset");
3749 }
3750 BaseAlignment = Alignment;
3751 break;
3752 }
3754 // basealign is printed if it is different than align.
3755 if (parseAlignment(BaseAlignment))
3756 return true;
3757 break;
3759 if (parseAddrspace(Ptr.AddrSpace))
3760 return true;
3761 break;
3762 case MIToken::md_tbaa:
3763 lex();
3764 if (parseMDNode(AAInfo.TBAA))
3765 return true;
3766 break;
3768 lex();
3769 if (parseMDNode(AAInfo.Scope))
3770 return true;
3771 break;
3773 lex();
3774 if (parseMDNode(AAInfo.NoAlias))
3775 return true;
3776 break;
3778 lex();
3779 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3780 return true;
3781 break;
3782 case MIToken::md_range:
3783 lex();
3784 if (parseMDNode(Range))
3785 return true;
3786 break;
3788 lex();
3789 if (parseMDNode(MemCacheHint))
3790 return true;
3791 break;
3792 // TODO: Report an error on duplicate metadata nodes.
3793 default:
3794 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3795 "'!noalias' or '!range' or '!mem.cache_hint' or "
3796 "'!noalias.addrspace'");
3797 }
3798 }
3799 if (expectAndConsume(MIToken::rparen))
3800 return true;
3801 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3802 MMOMetadata(AAInfo, Range, MemCacheHint), SSID,
3803 Order, FailureOrder);
3804 return false;
3805}
3806
3807bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3809 Token.is(MIToken::kw_post_instr_symbol)) &&
3810 "Invalid token for a pre- post-instruction symbol!");
3811 lex();
3812 if (Token.isNot(MIToken::MCSymbol))
3813 return error("expected a symbol after 'pre-instr-symbol'");
3814 Symbol = getOrCreateMCSymbol(Token.stringValue());
3815 lex();
3816 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3817 Token.is(MIToken::lbrace))
3818 return false;
3819 if (Token.isNot(MIToken::comma))
3820 return error("expected ',' before the next machine operand");
3821 lex();
3822 return false;
3823}
3824
3825bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3827 "Invalid token for a heap alloc marker!");
3828 lex();
3829 if (parseMDNode(Node))
3830 return true;
3831 if (!Node)
3832 return error("expected a MDNode after 'heap-alloc-marker'");
3833 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3834 Token.is(MIToken::lbrace))
3835 return false;
3836 if (Token.isNot(MIToken::comma))
3837 return error("expected ',' before the next machine operand");
3838 lex();
3839 return false;
3840}
3841
3842bool MIParser::parsePCSections(MDNode *&Node) {
3843 assert(Token.is(MIToken::kw_pcsections) &&
3844 "Invalid token for a PC sections!");
3845 lex();
3846 if (parseMDNode(Node))
3847 return true;
3848 if (!Node)
3849 return error("expected a MDNode after 'pcsections'");
3850 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3851 Token.is(MIToken::lbrace))
3852 return false;
3853 if (Token.isNot(MIToken::comma))
3854 return error("expected ',' before the next machine operand");
3855 lex();
3856 return false;
3857}
3858
3859bool MIParser::parseMMRA(MDNode *&Node) {
3860 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3861 lex();
3862 if (parseMDNode(Node))
3863 return true;
3864 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3865 Token.is(MIToken::lbrace))
3866 return false;
3867 if (Token.isNot(MIToken::comma))
3868 return error("expected ',' before the next machine operand");
3869 lex();
3870 return false;
3871}
3872
3874 const Function &F,
3875 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3876 ModuleSlotTracker MST(F.getParent());
3878 for (const auto &BB : F) {
3879 if (BB.hasName())
3880 continue;
3881 int Slot = MST.getLocalSlot(&BB);
3882 if (Slot == -1)
3883 continue;
3884 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3885 }
3886}
3887
3889 unsigned Slot,
3890 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3891 return Slots2BasicBlocks.lookup(Slot);
3892}
3893
3894const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3895 if (Slots2BasicBlocks.empty())
3896 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3897 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3898}
3899
3900const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3901 if (&F == &MF.getFunction())
3902 return getIRBlock(Slot);
3903 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3904 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3905 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3906}
3907
3908MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3909 // FIXME: Currently we can't recognize temporary or local symbols and call all
3910 // of the appropriate forms to create them. However, this handles basic cases
3911 // well as most of the special aspects are recognized by a prefix on their
3912 // name, and the input names should already be unique. For test cases, keeping
3913 // the symbol name out of the symbol table isn't terribly important.
3914 return MF.getContext().getOrCreateSymbol(Name);
3915}
3916
3917bool MIParser::parseStringConstant(std::string &Result) {
3918 if (Token.isNot(MIToken::StringConstant))
3919 return error("expected string constant");
3920 Result = std::string(Token.stringValue());
3921 lex();
3922 return false;
3923}
3924
3926 StringRef Src,
3928 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3929}
3930
3933 return MIParser(PFS, Error, Src).parseBasicBlocks();
3934}
3935
3939 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3940}
3941
3943 Register &Reg, StringRef Src,
3945 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3946}
3947
3949 Register &Reg, StringRef Src,
3951 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3952}
3953
3955 VRegInfo *&Info, StringRef Src,
3957 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3958}
3959
3962 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3963}
3964
3968 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
3969}
3972 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3973}
3974
3976 PerFunctionMIParsingState &PFS, const Value *&V,
3977 ErrorCallbackType ErrorCallback) {
3978 MIToken Token;
3979 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3980 ErrorCallback(Loc, Msg);
3981 });
3982 V = nullptr;
3983
3984 return ::parseIRValue(Token, PFS, V, ErrorCallback);
3985}
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
uint64_t uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
basic Basic Alias true
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Error parseAlignment(StringRef Str, Align &Alignment, StringRef Name, bool AllowZero=false)
Attempts to parse an alignment component of a specification.
This file defines the DenseMap class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define RegName(no)
A common definition of LaneBitmask for use in TableGen and CodeGen.
static llvm::Error parse(GsymDataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Definition LineTable.cpp:54
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static const char * printImplicitRegisterFlag(const MachineOperand &MO)
static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale)
static const BasicBlock * getIRBlockFromSlot(unsigned Slot, const DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
static std::string getRegisterName(const TargetRegisterInfo *TRI, Register Reg)
static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue, PerFunctionMIParsingState &PFS, const Constant *&C, ErrorCallbackType ErrCB)
static void initSlots2Values(const Function &F, DenseMap< unsigned, const Value * > &Slots2Values)
Creates the mapping from slot numbers to function's unnamed IR values.
Definition MIParser.cpp:361
static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS, const Value *&V, ErrorCallbackType ErrCB)
static bool verifyScalarSize(uint64_t Size)
static bool getUnsigned(const MIToken &Token, unsigned &Result, ErrorCallbackType ErrCB)
static bool getHexUint(const MIToken &Token, APInt &Result)
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value * > &Slots2Values)
Definition MIParser.cpp:352
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
Definition MIParser.cpp:606
static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, ArrayRef< ParsedMachineOperand > Operands)
Return true if the parsed machine operands contain a given machine operand.
static bool parseGlobalValue(const MIToken &Token, PerFunctionMIParsingState &PFS, GlobalValue *&GV, ErrorCallbackType ErrCB)
static bool verifyAddrSpace(uint64_t AddrSpace)
Register Reg
Register const TargetRegisterInfo * TRI
#define R2(n)
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
SI Fold Operands
const char * Msg
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define error(X)
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:471
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
bool isNegative() const
Determine sign of this APSInt.
Definition APSInt.h:50
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
static constexpr BranchProbability getRaw(uint32_t N)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:793
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:312
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition Function.h:802
Module * getParent()
Get the module that this global value is contained inside of...
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
static constexpr LLT bfloat16()
static LLT floatIEEE(unsigned SizeInBits)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition MCDwarf.h:635
static MCCFIInstruction createLLVMVectorOffset(MCSymbol *L, unsigned Register, unsigned RegisterSizeInBits, unsigned MaskRegister, unsigned MaskRegisterSizeInBits, int64_t Offset, SMLoc Loc={})
.cfi_llvm_vector_offset Previous value of Register is saved at Offset from CFA.
Definition MCDwarf.h:797
static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_undefined From now on the previous value of Register can't be restored anymore.
Definition MCDwarf.h:732
static MCCFIInstruction createLLVMVectorRegisters(MCSymbol *L, unsigned Register, ArrayRef< VectorRegisterWithLane > VectorRegisters, SMLoc Loc={})
.cfi_llvm_vector_registers Previous value of Register is saved in lanes of vector registers.
Definition MCDwarf.h:787
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
Definition MCDwarf.h:725
static MCCFIInstruction createSetRAState(MCSymbol *L, unsigned State, MCSymbol *PACSym=nullptr, SMLoc Loc={})
.cfi_set_ra_state AArch64 set RA sign state,
Definition MCDwarf.h:708
static MCCFIInstruction createLLVMDefAspaceCfa(MCSymbol *L, unsigned Register, int64_t Offset, unsigned AddressSpace, SMLoc Loc)
.cfi_llvm_def_aspace_cfa defines the rule for computing the CFA to be the result of evaluating the DW...
Definition MCDwarf.h:660
static MCCFIInstruction createLLVMVectorRegisterMask(MCSymbol *L, unsigned Register, unsigned SpillRegister, unsigned SpillRegisterLaneSizeInBits, unsigned MaskRegister, unsigned MaskRegisterSizeInBits, SMLoc Loc={})
.cfi_llvm_vector_register_mask Previous value of Register is saved in SpillRegister,...
Definition MCDwarf.h:808
static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1, unsigned Register2, SMLoc Loc={})
.cfi_register Previous value of Register1 is saved in register Register2.
Definition MCDwarf.h:685
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition MCDwarf.h:628
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition MCDwarf.h:670
static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
Definition MCDwarf.h:701
static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state AArch64 negate RA state.
Definition MCDwarf.h:696
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:745
static MCCFIInstruction createLLVMRegisterPair(MCSymbol *L, unsigned Register, unsigned R1, unsigned R1SizeInBits, unsigned R2, unsigned R2SizeInBits, SMLoc Loc={})
.cfi_llvm_register_pair Previous value of Register is saved in R1:R2.
Definition MCDwarf.h:777
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:643
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition MCDwarf.h:756
static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc={})
.cfi_window_save SPARC register window is saved.
Definition MCDwarf.h:691
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment, SMLoc Loc={})
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
Definition MCDwarf.h:651
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:750
static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_same_value Current value of Register is the same as in the previous frame.
Definition MCDwarf.h:739
static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_rel_offset Previous value of Register is saved at offset Offset from the current CFA register.
Definition MCDwarf.h:678
Describe properties that are true of each instruction in the target description file.
unsigned getID() const
getID() - Return the register class ID number.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1081
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
MIRFormater - Interface to format MIR operand based on target.
virtual bool parseImmMnemonic(const unsigned OpCode, const unsigned OpIdx, StringRef Src, int64_t &Imm, ErrorCallbackType ErrorCallback) const
Implement target specific parsing of immediate mnemonics.
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
static LLVM_ABI bool parseIRValue(StringRef Src, MachineFunction &MF, PerFunctionMIParsingState &PFS, const Value *&V, ErrorCallbackType ErrorCallback)
Helper functions to parse IR value from MIR serialization format which will be useful for target spec...
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
void setAddressTakenIRBlock(BasicBlock *BB)
Set this block to reflect that it corresponds to an IR-level basic block with a BlockAddress.
void setIsCleanupFuncletEntry(bool V=true)
Indicates if this is the entry block of a cleanup funclet.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void setCallFrameSize(unsigned N)
Set the call frame size on entry to this basic block.
void setMaxBytesForAlignment(unsigned MaxBytes)
Set the maximum amount of padding allowed for aligning the basic block.
void setAlignment(Align A)
Set alignment of the basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void setSectionID(MBBSectionID V)
Sets the section ID for this basic block.
void setIsInlineAsmBrIndirectTarget(bool V=true)
Indicates if this is the indirect dest of an INLINEASM_BR.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
void setFlag(MIFlag Flag)
Set a MI flag.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
static MachineOperand CreateES(const char *SymName, unsigned TargetFlags=0)
static MachineOperand CreateFPImm(const ConstantFP *CFP)
static MachineOperand CreateCFIIndex(unsigned CFIIndex)
static MachineOperand CreateRegMask(const uint32_t *Mask)
CreateRegMask - Creates a register mask operand referencing Mask.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineOperand CreateCImm(const ConstantInt *CI)
static MachineOperand CreateMetadata(const MDNode *Meta)
static MachineOperand CreatePredicate(unsigned Pred)
static MachineOperand CreateImm(int64_t Val)
static MachineOperand CreateShuffleMask(ArrayRef< int > Mask)
static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags=0)
static MachineOperand CreateDbgInstrRef(unsigned InstrIdx, unsigned OpIdx)
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset, unsigned TargetFlags=0)
void setTargetFlags(unsigned F)
static MachineOperand CreateLaneMask(LaneBitmask LaneMask)
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned TargetFlags=0)
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
static MachineOperand CreateIntrinsicID(Intrinsic::ID ID)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
void setRegClassOrRegBank(Register Reg, const RegClassOrRegBank &RCOrRB)
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI Register createIncompleteVirtualRegister(StringRef Name="")
Creates a new virtual register that has no register class, register bank or size assigned yet.
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
void noteNewVirtualRegister(Register Reg)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
const char * getBufferEnd() const
const char * getBufferStart() const
Manage lifetime of a slot tracker for printing IR.
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
void incorporateFunction(const Function &F)
Incorporate the given function.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Special value supplied for machine level alias analysis.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
unsigned getNumRegBanks() const
Get the total number of register banks.
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:307
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
unsigned getMainFileID() const
Definition SourceMgr.h:150
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition SourceMgr.h:141
LLVM_ABI SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const
Return an SMDiagnostic at the specified location with the specified string.
bool empty() const
Definition StringMap.h:103
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:311
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:736
const char * iterator
Definition StringRef.h:60
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
LLVM_ABI std::string lower() const
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:661
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
LLVM Value Representation.
Definition Value.h:75
bool hasName() const
Definition Value.h:263
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
LLVM_ABI ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:66
support::ulittle32_t Word
Definition IRSymtab.h:53
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
LLVM_ABI bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Kill
The last use of a register.
@ InternalRead
Register reads a value that is defined inside the same instruction or bundle.
@ Undef
Value of the register doesn't matter.
@ EarlyClobber
Register definition happens before uses.
@ Define
Register definition.
@ Renamable
Register that may be renamed.
@ Debug
Register 'use' is for debugging purpose.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
LLVM_ABI bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
LLVM_ABI bool parsePrefetchTarget(PerFunctionMIParsingState &PFS, CallsiteID &Target, StringRef Src, SMDiagnostic &Error)
LLVM_ABI void guessSuccessors(const MachineBasicBlock &MBB, SmallVectorImpl< MachineBasicBlock * > &Result, bool &IsFallthrough)
Determine a possible list of successors of a basic block based on the basic block machine operand bei...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:380
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition Parser.cpp:238
constexpr RegState getDefRegState(bool B)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr bool hasRegState(RegState Value, RegState Test)
AtomicOrdering
Atomic ordering for LLVM's memory model.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
LLVM_ABI bool parseRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
LLVM_ABI Constant * parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type and a constant value in the given string.
Definition Parser.cpp:197
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:774
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:803
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:797
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:791
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:800
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82
LLVM_ABI static const MBBSectionID ExceptionSectionID
LLVM_ABI static const MBBSectionID ColdSectionID
A token produced by the machine instruction lexer.
Definition MILexer.h:26
TokenKind kind() const
Definition MILexer.h:221
bool hasIntegerValue() const
Definition MILexer.h:261
bool is(TokenKind K) const
Definition MILexer.h:248
StringRef stringValue() const
Return the token's string value.
Definition MILexer.h:257
@ kw_pre_instr_symbol
Definition MILexer.h:144
@ kw_deactivation_symbol
Definition MILexer.h:149
@ kw_call_frame_size
Definition MILexer.h:156
@ kw_cfi_aarch64_negate_ra_sign_state
Definition MILexer.h:101
@ kw_cfi_llvm_def_aspace_cfa
Definition MILexer.h:94
@ MachineBasicBlock
Definition MILexer.h:180
@ kw_dbg_instr_ref
Definition MILexer.h:85
@ NamedVirtualRegister
Definition MILexer.h:178
@ kw_early_clobber
Definition MILexer.h:59
@ kw_cleanup_funclet_entry
Definition MILexer.h:137
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:190
@ kw_cfi_window_save
Definition MILexer.h:100
@ kw_cfi_llvm_register_pair
Definition MILexer.h:104
@ kw_frame_destroy
Definition MILexer.h:64
@ kw_cfi_undefined
Definition MILexer.h:99
@ MachineBasicBlockLabel
Definition MILexer.h:179
@ kw_cfi_llvm_vector_offset
Definition MILexer.h:106
@ kw_cfi_register
Definition MILexer.h:95
@ kw_inlineasm_br_indirect_target
Definition MILexer.h:134
@ kw_cfi_rel_offset
Definition MILexer.h:88
@ kw_cfi_llvm_vector_registers
Definition MILexer.h:105
@ kw_ehfunclet_entry
Definition MILexer.h:136
@ kw_cfi_llvm_vector_register_mask
Definition MILexer.h:107
@ kw_cfi_aarch64_negate_ra_sign_state_with_pc
Definition MILexer.h:102
@ kw_cfi_def_cfa_register
Definition MILexer.h:89
@ kw_cfi_same_value
Definition MILexer.h:86
@ kw_cfi_set_ra_state
Definition MILexer.h:103
@ kw_cfi_adjust_cfa_offset
Definition MILexer.h:91
@ kw_dereferenceable
Definition MILexer.h:55
@ kw_implicit_define
Definition MILexer.h:52
@ kw_max_bytes_for_alignment
Definition MILexer.h:157
@ kw_cfi_def_cfa_offset
Definition MILexer.h:90
@ md_mem_cache_hint
Definition MILexer.h:171
@ kw_machine_block_address_taken
Definition MILexer.h:155
@ kw_cfi_remember_state
Definition MILexer.h:96
@ kw_debug_instr_number
Definition MILexer.h:84
@ kw_post_instr_symbol
Definition MILexer.h:145
@ kw_cfi_restore_state
Definition MILexer.h:98
@ kw_ir_block_address_taken
Definition MILexer.h:154
@ kw_unknown_address
Definition MILexer.h:153
@ md_noalias_addrspace
Definition MILexer.h:169
@ kw_debug_location
Definition MILexer.h:83
@ kw_heap_alloc_marker
Definition MILexer.h:146
StringRef range() const
Definition MILexer.h:254
StringRef::iterator location() const
Definition MILexer.h:252
const APSInt & integerValue() const
Definition MILexer.h:259
LLVM IR metadata carried by a MachineMemOperand.
This class contains a discriminated union of information about pointers in memory operands,...
int64_t Offset
Offset - This is an offset from the base Value*.
LLVM_ABI VRegInfo & getVRegInfo(Register Num)
Definition MIParser.cpp:329
const SlotMapping & IRSlots
Definition MIParser.h:173
LLVM_ABI const Value * getIRValue(unsigned Slot)
Definition MIParser.cpp:374
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition MIParser.h:178
StringMap< VRegInfo * > VRegInfosNamed
Definition MIParser.h:180
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition MIParser.h:187
LLVM_ABI PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition MIParser.cpp:324
PerTargetMIParsingState & Target
Definition MIParser.h:174
DenseMap< Register, VRegInfo * > VRegInfos
Definition MIParser.h:179
LLVM_ABI VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition MIParser.cpp:340
LLVM_ABI bool getVRegFlagValue(StringRef FlagName, uint8_t &FlagValue) const
Definition MIParser.cpp:129
LLVM_ABI bool getDirectTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a direct target flag to the corresponding target flag.
Definition MIParser.cpp:227
LLVM_ABI const RegisterBank * getRegBank(StringRef Name)
Check if the given identifier is a name of a register bank.
Definition MIParser.cpp:317
LLVM_ABI bool parseInstrName(StringRef InstrName, unsigned &OpCode)
Try to convert an instruction name to an opcode.
Definition MIParser.cpp:148
LLVM_ABI unsigned getSubRegIndex(StringRef Name)
Check if the given identifier is a name of a subregister index.
Definition MIParser.cpp:188
LLVM_ABI bool getTargetIndex(StringRef Name, int &Index)
Try to convert a name of target index to the corresponding target index.
Definition MIParser.cpp:206
LLVM_ABI void setTarget(const TargetSubtargetInfo &NewSubtarget)
Definition MIParser.cpp:81
LLVM_ABI bool getRegisterByName(StringRef RegName, Register &Reg)
Try to convert a register name to a register number.
Definition MIParser.cpp:119
LLVM_ABI bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag)
Try to convert a name of a MachineMemOperand target flag to the corresponding target flag.
Definition MIParser.cpp:270
LLVM_ABI bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a bitmask target flag to the corresponding target flag.
Definition MIParser.cpp:249
LLVM_ABI const TargetRegisterClass * getRegClass(StringRef Name)
Check if the given identifier is a name of a register class.
Definition MIParser.cpp:310
LLVM_ABI const uint32_t * getRegMask(StringRef Identifier)
Check if the given identifier is a name of a register mask.
Definition MIParser.cpp:171
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition SlotMapping.h:32
NumberedValues< GlobalValue * > GlobalValues
Definition SlotMapping.h:33
const RegisterBank * RegBank
Definition MIParser.h:46
union llvm::VRegInfo::@127225073067155374133234315364317264041071000132 D
const TargetRegisterClass * RC
Definition MIParser.h:45
enum llvm::VRegInfo::@374354327266250320012227113300214031244227062232 Kind
Register VReg
Definition MIParser.h:48
bool Explicit
VReg was explicitly specified in the .mir file.
Definition MIParser.h:43