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 parsePrefetchTarget(CallsiteID &Target);
512 bool parseOperandsOffset(MachineOperand &Op);
513 bool parseIRValue(const Value *&V);
514 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
515 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
516 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
517 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
518 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
519 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
520 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
521 bool parseHeapAllocMarker(MDNode *&Node);
522 bool parsePCSections(MDNode *&Node);
523 bool parseMMRA(MDNode *&Node);
524
525 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
526 MachineOperand &Dest, const MIRFormatter &MF);
527
528private:
529 /// Convert the integer literal in the current token into an unsigned integer.
530 ///
531 /// Return true if an error occurred.
532 bool getUnsigned(unsigned &Result);
533
534 /// Convert the integer literal in the current token into an uint64.
535 ///
536 /// Return true if an error occurred.
537 bool getUint64(uint64_t &Result);
538
539 /// Convert the hexadecimal literal in the current token into an unsigned
540 /// APInt with a minimum bitwidth required to represent the value.
541 ///
542 /// Return true if the literal does not represent an integer value.
543 bool getHexUint(APInt &Result);
544
545 /// If the current token is of the given kind, consume it and return false.
546 /// Otherwise report an error and return true.
547 bool expectAndConsume(MIToken::TokenKind TokenKind);
548
549 /// If the current token is of the given kind, consume it and return true.
550 /// Otherwise return false.
551 bool consumeIfPresent(MIToken::TokenKind TokenKind);
552
553 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
554
555 bool assignRegisterTies(MachineInstr &MI,
557
558 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
559 const MCInstrDesc &MCID);
560
561 const BasicBlock *getIRBlock(unsigned Slot);
562 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
563
564 /// Get or create an MCSymbol for a given name.
565 MCSymbol *getOrCreateMCSymbol(StringRef Name);
566
567 /// parseStringConstant
568 /// ::= StringConstant
569 bool parseStringConstant(std::string &Result);
570};
571
572} // end anonymous namespace
573
574MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
575 StringRef Source)
576 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
577{}
578
579void MIParser::lex(unsigned SkipChar) {
580 CurrentSource = lexMIToken(
581 CurrentSource.substr(SkipChar), Token,
582 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
583}
584
585bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
586
587bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
588 const SourceMgr &SM = *PFS.SM;
589 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
590 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
591 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
592 // Create an ordinary diagnostic when the source manager's buffer is the
593 // source string.
595 return true;
596 }
597 // Create a diagnostic for a YAML string literal.
598 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
599 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
600 Source, {}, {});
601 return true;
602}
603
604typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
606
607static const char *toString(MIToken::TokenKind TokenKind) {
608 switch (TokenKind) {
609 case MIToken::comma:
610 return "','";
611 case MIToken::equal:
612 return "'='";
613 case MIToken::colon:
614 return "':'";
615 case MIToken::lparen:
616 return "'('";
617 case MIToken::rparen:
618 return "')'";
619 default:
620 return "<unknown token>";
621 }
622}
623
624bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
625 if (Token.isNot(TokenKind))
626 return error(Twine("expected ") + toString(TokenKind));
627 lex();
628 return false;
629}
630
631bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
632 if (Token.isNot(TokenKind))
633 return false;
634 lex();
635 return true;
636}
637
638// Parse Machine Basic Block Section ID.
639bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
641 lex();
642 if (Token.is(MIToken::IntegerLiteral)) {
643 unsigned Value = 0;
644 if (getUnsigned(Value))
645 return error("Unknown Section ID");
646 SID = MBBSectionID{Value};
647 } else {
648 const StringRef &S = Token.stringValue();
649 if (S == "Exception")
651 else if (S == "Cold")
653 else
654 return error("Unknown Section ID");
655 }
656 lex();
657 return false;
658}
659
660// Parse Machine Basic Block ID.
661bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) {
662 if (Token.isNot(MIToken::kw_bb_id))
663 return error("expected 'bb_id'");
664 lex();
665 unsigned BaseID = 0;
666 unsigned CloneID = 0;
667 if (Token.is(MIToken::FloatingPointLiteral)) {
668 StringRef S = Token.range();
669 auto Parts = S.split('.');
670 if (Parts.first.getAsInteger(10, BaseID) ||
671 Parts.second.getAsInteger(10, CloneID))
672 return error("Unknown BB ID");
673 lex();
674 } else {
675 if (getUnsigned(BaseID))
676 return error("Unknown BB ID");
677 lex();
678 if (Token.is(MIToken::comma) || Token.is(MIToken::dot)) {
679 lex();
680 if (getUnsigned(CloneID))
681 return error("Unknown Clone ID");
682 lex();
683 } else if (Token.is(MIToken::IntegerLiteral)) {
684 if (getUnsigned(CloneID))
685 return error("Unknown Clone ID");
686 lex();
687 }
688 }
689 BBID = {BaseID, CloneID};
690 return false;
691}
692
693// Parse basic block call frame size.
694bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) {
696 lex();
697 unsigned Value = 0;
698 if (getUnsigned(Value))
699 return error("Unknown call frame size");
700 CallFrameSize = Value;
701 lex();
702 return false;
703}
704
705bool MIParser::parsePrefetchTarget(CallsiteID &Target) {
706 lex();
707 std::optional<UniqueBBID> BBID;
708 if (parseBBID(BBID))
709 return true;
710 Target.BBID = *BBID;
711 if (expectAndConsume(MIToken::comma))
712 return true;
713 return getUnsigned(Target.CallsiteIndex);
714}
715
716bool MIParser::parseBasicBlockDefinition(
719 unsigned ID = 0;
720 if (getUnsigned(ID))
721 return true;
722 auto Loc = Token.location();
723 auto Name = Token.stringValue();
724 lex();
725 bool MachineBlockAddressTaken = false;
726 BasicBlock *AddressTakenIRBlock = nullptr;
727 bool IsLandingPad = false;
728 bool IsInlineAsmBrIndirectTarget = false;
729 bool IsEHFuncletEntry = false;
730 bool IsEHScopeEntry = false;
731 std::optional<MBBSectionID> SectionID;
733 std::optional<UniqueBBID> BBID;
734 unsigned CallFrameSize = 0;
735 BasicBlock *BB = nullptr;
736 if (consumeIfPresent(MIToken::lparen)) {
737 do {
738 // TODO: Report an error when multiple same attributes are specified.
739 switch (Token.kind()) {
741 MachineBlockAddressTaken = true;
742 lex();
743 break;
745 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
746 return true;
747 break;
749 IsLandingPad = true;
750 lex();
751 break;
753 IsInlineAsmBrIndirectTarget = true;
754 lex();
755 break;
757 IsEHFuncletEntry = true;
758 lex();
759 break;
761 IsEHScopeEntry = true;
762 lex();
763 break;
765 if (parseAlignment(Alignment))
766 return true;
767 break;
768 case MIToken::IRBlock:
770 // TODO: Report an error when both name and ir block are specified.
771 if (parseIRBlock(BB, MF.getFunction()))
772 return true;
773 lex();
774 break;
776 if (parseSectionID(SectionID))
777 return true;
778 break;
780 if (parseBBID(BBID))
781 return true;
782 break;
784 if (parseCallFrameSize(CallFrameSize))
785 return true;
786 break;
787 default:
788 break;
789 }
790 } while (consumeIfPresent(MIToken::comma));
791 if (expectAndConsume(MIToken::rparen))
792 return true;
793 }
794 if (expectAndConsume(MIToken::colon))
795 return true;
796
797 if (!Name.empty()) {
799 MF.getFunction().getValueSymbolTable()->lookup(Name));
800 if (!BB)
801 return error(Loc, Twine("basic block '") + Name +
802 "' is not defined in the function '" +
803 MF.getName() + "'");
804 }
805 auto *MBB = MF.CreateMachineBasicBlock(BB, BBID);
806 MF.insert(MF.end(), MBB);
807 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
808 if (!WasInserted)
809 return error(Loc, Twine("redefinition of machine basic block with id #") +
810 Twine(ID));
811 if (Alignment)
812 MBB->setAlignment(Align(Alignment));
813 if (MachineBlockAddressTaken)
815 if (AddressTakenIRBlock)
816 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
817 MBB->setIsEHPad(IsLandingPad);
818 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
819 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
820 MBB->setIsEHScopeEntry(IsEHScopeEntry);
821 if (SectionID) {
822 MBB->setSectionID(*SectionID);
823 MF.setBBSectionsType(BasicBlockSection::List);
824 }
825 MBB->setCallFrameSize(CallFrameSize);
826 return false;
827}
828
829bool MIParser::parseBasicBlockDefinitions(
831 lex();
832 // Skip until the first machine basic block.
833 while (Token.is(MIToken::Newline))
834 lex();
835 if (Token.isErrorOrEOF())
836 return Token.isError();
837 if (Token.isNot(MIToken::MachineBasicBlockLabel))
838 return error("expected a basic block definition before instructions");
839 unsigned BraceDepth = 0;
840 do {
841 if (parseBasicBlockDefinition(MBBSlots))
842 return true;
843 bool IsAfterNewline = false;
844 // Skip until the next machine basic block.
845 while (true) {
846 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
847 Token.isErrorOrEOF())
848 break;
849 else if (Token.is(MIToken::MachineBasicBlockLabel))
850 return error("basic block definition should be located at the start of "
851 "the line");
852 else if (consumeIfPresent(MIToken::Newline)) {
853 IsAfterNewline = true;
854 continue;
855 }
856 IsAfterNewline = false;
857 if (Token.is(MIToken::lbrace))
858 ++BraceDepth;
859 if (Token.is(MIToken::rbrace)) {
860 if (!BraceDepth)
861 return error("extraneous closing brace ('}')");
862 --BraceDepth;
863 }
864 lex();
865 }
866 // Verify that we closed all of the '{' at the end of a file or a block.
867 if (!Token.isError() && BraceDepth)
868 return error("expected '}'"); // FIXME: Report a note that shows '{'.
869 } while (!Token.isErrorOrEOF());
870 return Token.isError();
871}
872
873bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
874 assert(Token.is(MIToken::kw_liveins));
875 lex();
876 if (expectAndConsume(MIToken::colon))
877 return true;
878 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
879 return false;
880 do {
881 if (Token.isNot(MIToken::NamedRegister))
882 return error("expected a named register");
884 if (parseNamedRegister(Reg))
885 return true;
886 lex();
888 if (consumeIfPresent(MIToken::colon)) {
889 // Parse lane mask.
890 if (Token.isNot(MIToken::IntegerLiteral) &&
891 Token.isNot(MIToken::HexLiteral))
892 return error("expected a lane mask");
893 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
894 "Use correct get-function for lane mask");
896 if (getUint64(V))
897 return error("invalid lane mask value");
898 Mask = LaneBitmask(V);
899 lex();
900 }
901 MBB.addLiveIn(Reg, Mask);
902 } while (consumeIfPresent(MIToken::comma));
903 return false;
904}
905
906bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
908 lex();
909 if (expectAndConsume(MIToken::colon))
910 return true;
911 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
912 return false;
913 do {
914 if (Token.isNot(MIToken::MachineBasicBlock))
915 return error("expected a machine basic block reference");
916 MachineBasicBlock *SuccMBB = nullptr;
917 if (parseMBBReference(SuccMBB))
918 return true;
919 lex();
920 unsigned Weight = 0;
921 if (consumeIfPresent(MIToken::lparen)) {
922 if (Token.isNot(MIToken::IntegerLiteral) &&
923 Token.isNot(MIToken::HexLiteral))
924 return error("expected an integer literal after '('");
925 if (getUnsigned(Weight))
926 return true;
927 lex();
928 if (expectAndConsume(MIToken::rparen))
929 return true;
930 }
932 } while (consumeIfPresent(MIToken::comma));
934 return false;
935}
936
937bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
938 MachineBasicBlock *&AddFalthroughFrom) {
939 // Skip the definition.
941 lex();
942 if (consumeIfPresent(MIToken::lparen)) {
943 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
944 lex();
945 consumeIfPresent(MIToken::rparen);
946 }
947 consumeIfPresent(MIToken::colon);
948
949 // Parse the liveins and successors.
950 // N.B: Multiple lists of successors and liveins are allowed and they're
951 // merged into one.
952 // Example:
953 // liveins: $edi
954 // liveins: $esi
955 //
956 // is equivalent to
957 // liveins: $edi, $esi
958 bool ExplicitSuccessors = false;
959 while (true) {
960 if (Token.is(MIToken::kw_successors)) {
961 if (parseBasicBlockSuccessors(MBB))
962 return true;
963 ExplicitSuccessors = true;
964 } else if (Token.is(MIToken::kw_liveins)) {
965 if (parseBasicBlockLiveins(MBB))
966 return true;
967 } else if (consumeIfPresent(MIToken::Newline)) {
968 continue;
969 } else {
970 break;
971 }
972 if (!Token.isNewlineOrEOF())
973 return error("expected line break at the end of a list");
974 lex();
975 }
976
977 // Parse the instructions.
978 bool IsInBundle = false;
979 MachineInstr *PrevMI = nullptr;
980 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
981 !Token.is(MIToken::Eof)) {
982 if (consumeIfPresent(MIToken::Newline))
983 continue;
984 if (consumeIfPresent(MIToken::rbrace)) {
985 // The first parsing pass should verify that all closing '}' have an
986 // opening '{'.
987 assert(IsInBundle);
988 IsInBundle = false;
989 continue;
990 }
991 MachineInstr *MI = nullptr;
992 if (parse(MI))
993 return true;
994 MBB.insert(MBB.end(), MI);
995 if (IsInBundle) {
998 }
999 PrevMI = MI;
1000 if (Token.is(MIToken::lbrace)) {
1001 if (IsInBundle)
1002 return error("nested instruction bundles are not allowed");
1003 lex();
1004 // This instruction is the start of the bundle.
1005 MI->setFlag(MachineInstr::BundledSucc);
1006 IsInBundle = true;
1007 if (!Token.is(MIToken::Newline))
1008 // The next instruction can be on the same line.
1009 continue;
1010 }
1011 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
1012 lex();
1013 }
1014
1015 // Construct successor list by searching for basic block machine operands.
1016 if (!ExplicitSuccessors) {
1018 bool IsFallthrough;
1019 guessSuccessors(MBB, Successors, IsFallthrough);
1020 for (MachineBasicBlock *Succ : Successors)
1021 MBB.addSuccessor(Succ);
1022
1023 if (IsFallthrough) {
1024 AddFalthroughFrom = &MBB;
1025 } else {
1027 }
1028 }
1029
1030 return false;
1031}
1032
1033bool MIParser::parseBasicBlocks() {
1034 lex();
1035 // Skip until the first machine basic block.
1036 while (Token.is(MIToken::Newline))
1037 lex();
1038 if (Token.isErrorOrEOF())
1039 return Token.isError();
1040 // The first parsing pass should have verified that this token is a MBB label
1041 // in the 'parseBasicBlockDefinitions' method.
1043 MachineBasicBlock *AddFalthroughFrom = nullptr;
1044 do {
1045 MachineBasicBlock *MBB = nullptr;
1047 return true;
1048 if (AddFalthroughFrom) {
1049 if (!AddFalthroughFrom->isSuccessor(MBB))
1050 AddFalthroughFrom->addSuccessor(MBB);
1051 AddFalthroughFrom->normalizeSuccProbs();
1052 AddFalthroughFrom = nullptr;
1053 }
1054 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1055 return true;
1056 // The method 'parseBasicBlock' should parse the whole block until the next
1057 // block or the end of file.
1058 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1059 } while (Token.isNot(MIToken::Eof));
1060 return false;
1061}
1062
1063bool MIParser::parse(MachineInstr *&MI) {
1064 // Parse any register operands before '='
1067 while (Token.isRegister() || Token.isRegisterFlag()) {
1068 auto Loc = Token.location();
1069 std::optional<unsigned> TiedDefIdx;
1070 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1071 return true;
1072 Operands.push_back(
1073 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1074 if (Token.isNot(MIToken::comma))
1075 break;
1076 lex();
1077 }
1078 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1079 return true;
1080
1081 unsigned OpCode, Flags = 0;
1082 if (Token.isError() || parseInstruction(OpCode, Flags))
1083 return true;
1084
1085 // Parse the remaining machine operands.
1086 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1087 Token.isNot(MIToken::kw_post_instr_symbol) &&
1088 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1089 Token.isNot(MIToken::kw_pcsections) && Token.isNot(MIToken::kw_mmra) &&
1090 Token.isNot(MIToken::kw_cfi_type) &&
1091 Token.isNot(MIToken::kw_deactivation_symbol) &&
1092 Token.isNot(MIToken::kw_debug_location) &&
1093 Token.isNot(MIToken::kw_debug_instr_number) &&
1094 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1095 auto Loc = Token.location();
1096 std::optional<unsigned> TiedDefIdx;
1097 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1098 return true;
1099 Operands.push_back(
1100 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1101 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1102 Token.is(MIToken::lbrace))
1103 break;
1104 if (Token.isNot(MIToken::comma))
1105 return error("expected ',' before the next machine operand");
1106 lex();
1107 }
1108
1109 MCSymbol *PreInstrSymbol = nullptr;
1110 if (Token.is(MIToken::kw_pre_instr_symbol))
1111 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1112 return true;
1113 MCSymbol *PostInstrSymbol = nullptr;
1114 if (Token.is(MIToken::kw_post_instr_symbol))
1115 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1116 return true;
1117 MDNode *HeapAllocMarker = nullptr;
1118 if (Token.is(MIToken::kw_heap_alloc_marker))
1119 if (parseHeapAllocMarker(HeapAllocMarker))
1120 return true;
1121 MDNode *PCSections = nullptr;
1122 if (Token.is(MIToken::kw_pcsections))
1123 if (parsePCSections(PCSections))
1124 return true;
1125 MDNode *MMRA = nullptr;
1126 if (Token.is(MIToken::kw_mmra) && parseMMRA(MMRA))
1127 return true;
1128 unsigned CFIType = 0;
1129 if (Token.is(MIToken::kw_cfi_type)) {
1130 lex();
1131 if (Token.isNot(MIToken::IntegerLiteral))
1132 return error("expected an integer literal after 'cfi-type'");
1133 // getUnsigned is sufficient for 32-bit integers.
1134 if (getUnsigned(CFIType))
1135 return true;
1136 lex();
1137 // Lex past trailing comma if present.
1138 if (Token.is(MIToken::comma))
1139 lex();
1140 }
1141
1142 GlobalValue *DS = nullptr;
1143 if (Token.is(MIToken::kw_deactivation_symbol)) {
1144 lex();
1145 if (parseGlobalValue(DS))
1146 return true;
1147 lex();
1148 }
1149
1150 unsigned InstrNum = 0;
1151 if (Token.is(MIToken::kw_debug_instr_number)) {
1152 lex();
1153 if (Token.isNot(MIToken::IntegerLiteral))
1154 return error("expected an integer literal after 'debug-instr-number'");
1155 if (getUnsigned(InstrNum))
1156 return true;
1157 lex();
1158 // Lex past trailing comma if present.
1159 if (Token.is(MIToken::comma))
1160 lex();
1161 }
1162
1163 DebugLoc DebugLocation;
1164 if (Token.is(MIToken::kw_debug_location)) {
1165 lex();
1166 MDNode *Node = nullptr;
1167 if (Token.is(MIToken::exclaim)) {
1168 if (parseMDNode(Node))
1169 return true;
1170 } else if (Token.is(MIToken::md_dilocation)) {
1171 if (parseDILocation(Node))
1172 return true;
1173 } else {
1174 return error("expected a metadata node after 'debug-location'");
1175 }
1176 DebugLocation = DebugLoc(dyn_cast<DILocation>(Node));
1177 if (!DebugLocation)
1178 return error("referenced metadata is not a DILocation");
1179 }
1180
1181 // Parse the machine memory operands.
1183 if (Token.is(MIToken::coloncolon)) {
1184 lex();
1185 while (!Token.isNewlineOrEOF()) {
1186 MachineMemOperand *MemOp = nullptr;
1187 if (parseMachineMemoryOperand(MemOp))
1188 return true;
1189 MemOperands.push_back(MemOp);
1190 if (Token.isNewlineOrEOF())
1191 break;
1192 if (OpCode == TargetOpcode::BUNDLE && Token.is(MIToken::lbrace))
1193 break;
1194 if (Token.isNot(MIToken::comma))
1195 return error("expected ',' before the next machine memory operand");
1196 lex();
1197 }
1198 }
1199
1200 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1201 if (!MCID.isVariadic()) {
1202 // FIXME: Move the implicit operand verification to the machine verifier.
1203 if (verifyImplicitOperands(Operands, MCID))
1204 return true;
1205 }
1206
1207 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1208 MI->setFlags(Flags);
1209
1210 // Don't check the operands make sense, let the verifier catch any
1211 // improprieties.
1212 for (const auto &Operand : Operands)
1213 MI->addOperand(MF, Operand.Operand);
1214
1215 if (assignRegisterTies(*MI, Operands))
1216 return true;
1217 if (PreInstrSymbol)
1218 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1219 if (PostInstrSymbol)
1220 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1221 if (HeapAllocMarker)
1222 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1223 if (PCSections)
1224 MI->setPCSections(MF, PCSections);
1225 if (MMRA)
1226 MI->setMMRAMetadata(MF, MMRA);
1227 if (CFIType)
1228 MI->setCFIType(MF, CFIType);
1229 if (DS)
1230 MI->setDeactivationSymbol(MF, DS);
1231 if (!MemOperands.empty())
1232 MI->setMemRefs(MF, MemOperands);
1233 if (InstrNum)
1234 MI->setDebugInstrNum(InstrNum);
1235 return false;
1236}
1237
1238bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1239 lex();
1240 if (Token.isNot(MIToken::MachineBasicBlock))
1241 return error("expected a machine basic block reference");
1243 return true;
1244 lex();
1245 if (Token.isNot(MIToken::Eof))
1246 return error(
1247 "expected end of string after the machine basic block reference");
1248 return false;
1249}
1250
1251bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1252 lex();
1253 if (Token.isNot(MIToken::NamedRegister))
1254 return error("expected a named register");
1255 if (parseNamedRegister(Reg))
1256 return true;
1257 lex();
1258 if (Token.isNot(MIToken::Eof))
1259 return error("expected end of string after the register reference");
1260 return false;
1261}
1262
1263bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1264 lex();
1265 if (Token.isNot(MIToken::VirtualRegister))
1266 return error("expected a virtual register");
1267 if (parseVirtualRegister(Info))
1268 return true;
1269 lex();
1270 if (Token.isNot(MIToken::Eof))
1271 return error("expected end of string after the register reference");
1272 return false;
1273}
1274
1275bool MIParser::parseStandaloneRegister(Register &Reg) {
1276 lex();
1277 if (Token.isNot(MIToken::NamedRegister) &&
1278 Token.isNot(MIToken::VirtualRegister))
1279 return error("expected either a named or virtual register");
1280
1281 VRegInfo *Info;
1282 if (parseRegister(Reg, Info))
1283 return true;
1284
1285 lex();
1286 if (Token.isNot(MIToken::Eof))
1287 return error("expected end of string after the register reference");
1288 return false;
1289}
1290
1291bool MIParser::parseStandaloneStackObject(int &FI) {
1292 lex();
1293 if (Token.isNot(MIToken::StackObject))
1294 return error("expected a stack object");
1295 if (parseStackFrameIndex(FI))
1296 return true;
1297 if (Token.isNot(MIToken::Eof))
1298 return error("expected end of string after the stack object reference");
1299 return false;
1300}
1301
1302bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1303 lex();
1304 if (Token.is(MIToken::exclaim)) {
1305 if (parseMDNode(Node))
1306 return true;
1307 } else if (Token.is(MIToken::md_diexpr)) {
1308 if (parseDIExpression(Node))
1309 return true;
1310 } else if (Token.is(MIToken::md_dilocation)) {
1311 if (parseDILocation(Node))
1312 return true;
1313 } else {
1314 return error("expected a metadata node");
1315 }
1316 if (Token.isNot(MIToken::Eof))
1317 return error("expected end of string after the metadata node");
1318 return false;
1319}
1320
1321static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1322 assert(MO.isImplicit());
1323 return MO.isDef() ? "implicit-def" : "implicit";
1324}
1325
1326static std::string getRegisterName(const TargetRegisterInfo *TRI,
1327 Register Reg) {
1328 assert(Reg.isPhysical() && "expected phys reg");
1329 return StringRef(TRI->getName(Reg)).lower();
1330}
1331
1332/// Return true if the parsed machine operands contain a given machine operand.
1333static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1335 for (const auto &I : Operands) {
1336 if (ImplicitOperand.isIdenticalTo(I.Operand))
1337 return true;
1338 }
1339 return false;
1340}
1341
1342bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1343 const MCInstrDesc &MCID) {
1344 if (MCID.isCall())
1345 // We can't verify call instructions as they can contain arbitrary implicit
1346 // register and register mask operands.
1347 return false;
1348
1349 // Gather all the expected implicit operands.
1350 SmallVector<MachineOperand, 4> ImplicitOperands;
1351 for (MCPhysReg ImpDef : MCID.implicit_defs())
1352 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1353 for (MCPhysReg ImpUse : MCID.implicit_uses())
1354 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1355
1356 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1357 assert(TRI && "Expected target register info");
1358 for (const auto &I : ImplicitOperands) {
1360 continue;
1361 return error(Operands.empty() ? Token.location() : Operands.back().End,
1362 Twine("missing implicit register operand '") +
1364 getRegisterName(TRI, I.getReg()) + "'");
1365 }
1366 return false;
1367}
1368
1369bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1370 // Allow frame and fast math flags for OPCODE
1371 // clang-format off
1372 while (Token.is(MIToken::kw_frame_setup) ||
1373 Token.is(MIToken::kw_frame_destroy) ||
1374 Token.is(MIToken::kw_nnan) ||
1375 Token.is(MIToken::kw_ninf) ||
1376 Token.is(MIToken::kw_nsz) ||
1377 Token.is(MIToken::kw_arcp) ||
1378 Token.is(MIToken::kw_contract) ||
1379 Token.is(MIToken::kw_afn) ||
1380 Token.is(MIToken::kw_reassoc) ||
1381 Token.is(MIToken::kw_nuw) ||
1382 Token.is(MIToken::kw_nsw) ||
1383 Token.is(MIToken::kw_exact) ||
1384 Token.is(MIToken::kw_nofpexcept) ||
1385 Token.is(MIToken::kw_noconvergent) ||
1386 Token.is(MIToken::kw_unpredictable) ||
1387 Token.is(MIToken::kw_nneg) ||
1388 Token.is(MIToken::kw_disjoint) ||
1389 Token.is(MIToken::kw_nusw) ||
1390 Token.is(MIToken::kw_samesign) ||
1391 Token.is(MIToken::kw_inbounds) ||
1392 Token.is(MIToken::kw_nonnull) ||
1393 Token.is(MIToken::kw_lr_split)) {
1394 // clang-format on
1395 // Mine frame and fast math flags
1396 if (Token.is(MIToken::kw_frame_setup))
1398 if (Token.is(MIToken::kw_frame_destroy))
1400 if (Token.is(MIToken::kw_nnan))
1402 if (Token.is(MIToken::kw_ninf))
1404 if (Token.is(MIToken::kw_nsz))
1406 if (Token.is(MIToken::kw_arcp))
1408 if (Token.is(MIToken::kw_contract))
1410 if (Token.is(MIToken::kw_afn))
1412 if (Token.is(MIToken::kw_reassoc))
1414 if (Token.is(MIToken::kw_nuw))
1416 if (Token.is(MIToken::kw_nsw))
1418 if (Token.is(MIToken::kw_exact))
1420 if (Token.is(MIToken::kw_nofpexcept))
1422 if (Token.is(MIToken::kw_unpredictable))
1424 if (Token.is(MIToken::kw_noconvergent))
1426 if (Token.is(MIToken::kw_nneg))
1428 if (Token.is(MIToken::kw_disjoint))
1430 if (Token.is(MIToken::kw_nusw))
1432 if (Token.is(MIToken::kw_samesign))
1434 if (Token.is(MIToken::kw_inbounds))
1436 if (Token.is(MIToken::kw_nonnull))
1438 if (Token.is(MIToken::kw_lr_split))
1440
1441 lex();
1442 }
1443 if (Token.isNot(MIToken::Identifier))
1444 return error("expected a machine instruction");
1445 StringRef InstrName = Token.stringValue();
1446 if (PFS.Target.parseInstrName(InstrName, OpCode))
1447 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1448 lex();
1449 return false;
1450}
1451
1452bool MIParser::parseNamedRegister(Register &Reg) {
1453 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1454 StringRef Name = Token.stringValue();
1455 if (PFS.Target.getRegisterByName(Name, Reg))
1456 return error(Twine("unknown register name '") + Name + "'");
1457 return false;
1458}
1459
1460bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1461 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1462 StringRef Name = Token.stringValue();
1463 // TODO: Check that the VReg name is not the same as a physical register name.
1464 // If it is, then print a warning (when warnings are implemented).
1465 Info = &PFS.getVRegInfoNamed(Name);
1466 return false;
1467}
1468
1469bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1470 if (Token.is(MIToken::NamedVirtualRegister))
1471 return parseNamedVirtualRegister(Info);
1472 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1473 unsigned ID;
1474 if (getUnsigned(ID))
1475 return true;
1476 Info = &PFS.getVRegInfo(ID);
1477 return false;
1478}
1479
1480bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1481 switch (Token.kind()) {
1483 Reg = 0;
1484 return false;
1486 return parseNamedRegister(Reg);
1489 if (parseVirtualRegister(Info))
1490 return true;
1491 Reg = Info->VReg;
1492 return false;
1493 // TODO: Parse other register kinds.
1494 default:
1495 llvm_unreachable("The current token should be a register");
1496 }
1497}
1498
1499bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1500 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1501 return error("expected '_', register class, or register bank name");
1502 StringRef::iterator Loc = Token.location();
1503 StringRef Name = Token.stringValue();
1504
1505 // Was it a register class?
1506 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1507 if (RC) {
1508 lex();
1509
1510 switch (RegInfo.Kind) {
1511 case VRegInfo::UNKNOWN:
1512 case VRegInfo::NORMAL:
1513 RegInfo.Kind = VRegInfo::NORMAL;
1514 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1515 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1516 return error(Loc, Twine("conflicting register classes, previously: ") +
1517 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1518 }
1519 RegInfo.D.RC = RC;
1520 RegInfo.Explicit = true;
1521 return false;
1522
1523 case VRegInfo::GENERIC:
1524 case VRegInfo::REGBANK:
1525 return error(Loc, "register class specification on generic register");
1526 }
1527 llvm_unreachable("Unexpected register kind");
1528 }
1529
1530 // Should be a register bank or a generic register.
1531 const RegisterBank *RegBank = nullptr;
1532 if (Name != "_") {
1533 RegBank = PFS.Target.getRegBank(Name);
1534 if (!RegBank)
1535 return error(Loc, "expected '_', register class, or register bank name");
1536 }
1537
1538 lex();
1539
1540 switch (RegInfo.Kind) {
1541 case VRegInfo::UNKNOWN:
1542 case VRegInfo::GENERIC:
1543 case VRegInfo::REGBANK:
1544 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1545 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1546 return error(Loc, "conflicting generic register banks");
1547 RegInfo.D.RegBank = RegBank;
1548 RegInfo.Explicit = true;
1549 return false;
1550
1551 case VRegInfo::NORMAL:
1552 return error(Loc, "register bank specification on normal register");
1553 }
1554 llvm_unreachable("Unexpected register kind");
1555}
1556
1557bool MIParser::parseRegisterFlag(RegState &Flags) {
1558 const RegState OldFlags = Flags;
1559 switch (Token.kind()) {
1562 break;
1565 break;
1566 case MIToken::kw_def:
1568 break;
1569 case MIToken::kw_dead:
1571 break;
1572 case MIToken::kw_killed:
1574 break;
1575 case MIToken::kw_undef:
1577 break;
1580 break;
1583 break;
1586 break;
1589 break;
1590 default:
1591 llvm_unreachable("The current token should be a register flag");
1592 }
1593 if (OldFlags == Flags)
1594 // We know that the same flag is specified more than once when the flags
1595 // weren't modified.
1596 return error("duplicate '" + Token.stringValue() + "' register flag");
1597 lex();
1598 return false;
1599}
1600
1601bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1602 assert(Token.is(MIToken::dot));
1603 lex();
1604 if (Token.isNot(MIToken::Identifier))
1605 return error("expected a subregister index after '.'");
1606 auto Name = Token.stringValue();
1607 SubReg = PFS.Target.getSubRegIndex(Name);
1608 if (!SubReg)
1609 return error(Twine("use of unknown subregister index '") + Name + "'");
1610 lex();
1611 return false;
1612}
1613
1614bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1615 assert(Token.is(MIToken::kw_tied_def));
1616 lex();
1617 if (Token.isNot(MIToken::IntegerLiteral))
1618 return error("expected an integer literal after 'tied-def'");
1619 if (getUnsigned(TiedDefIdx))
1620 return true;
1621 lex();
1622 return expectAndConsume(MIToken::rparen);
1623}
1624
1625bool MIParser::assignRegisterTies(MachineInstr &MI,
1627 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1628 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1629 if (!Operands[I].TiedDefIdx)
1630 continue;
1631 // The parser ensures that this operand is a register use, so we just have
1632 // to check the tied-def operand.
1633 unsigned DefIdx = *Operands[I].TiedDefIdx;
1634 if (DefIdx >= E)
1635 return error(Operands[I].Begin,
1636 Twine("use of invalid tied-def operand index '" +
1637 Twine(DefIdx) + "'; instruction has only ") +
1638 Twine(E) + " operands");
1639 const auto &DefOperand = Operands[DefIdx].Operand;
1640 if (!DefOperand.isReg() || !DefOperand.isDef())
1641 // FIXME: add note with the def operand.
1642 return error(Operands[I].Begin,
1643 Twine("use of invalid tied-def operand index '") +
1644 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1645 " isn't a defined register");
1646 // Check that the tied-def operand wasn't tied elsewhere.
1647 for (const auto &TiedPair : TiedRegisterPairs) {
1648 if (TiedPair.first == DefIdx)
1649 return error(Operands[I].Begin,
1650 Twine("the tied-def operand #") + Twine(DefIdx) +
1651 " is already tied with another register operand");
1652 }
1653 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1654 }
1655 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1656 // indices must be less than tied max.
1657 for (const auto &TiedPair : TiedRegisterPairs)
1658 MI.tieOperands(TiedPair.first, TiedPair.second);
1659 return false;
1660}
1661
1662bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1663 std::optional<unsigned> &TiedDefIdx,
1664 bool IsDef) {
1665 RegState Flags = getDefRegState(IsDef);
1666 while (Token.isRegisterFlag()) {
1667 if (parseRegisterFlag(Flags))
1668 return true;
1669 }
1670 // Update IsDef as we may have read a def flag.
1671 IsDef = hasRegState(Flags, RegState::Define);
1672 if (!Token.isRegister())
1673 return error("expected a register after register flags");
1674 Register Reg;
1675 VRegInfo *RegInfo;
1676 if (parseRegister(Reg, RegInfo))
1677 return true;
1678 lex();
1679 unsigned SubReg = 0;
1680 if (Token.is(MIToken::dot)) {
1681 if (parseSubRegisterIndex(SubReg))
1682 return true;
1683 if (!Reg.isVirtual())
1684 return error("subregister index expects a virtual register");
1685 }
1686 if (Token.is(MIToken::colon)) {
1687 if (!Reg.isVirtual())
1688 return error("register class specification expects a virtual register");
1689 lex();
1690 if (parseRegisterClassOrBank(*RegInfo))
1691 return true;
1692 }
1693
1694 if (consumeIfPresent(MIToken::lparen)) {
1695 // For a def, we only expect a type. For use we expect either a type or a
1696 // tied-def. Additionally, for physical registers, we don't expect a type.
1697 if (Token.is(MIToken::kw_tied_def)) {
1698 if (IsDef)
1699 return error("tied-def not supported for defs");
1700 unsigned Idx;
1701 if (parseRegisterTiedDefIndex(Idx))
1702 return true;
1703 TiedDefIdx = Idx;
1704 } else {
1705 if (!Reg.isVirtual())
1706 return error("unexpected type on physical register");
1707
1708 LLT Ty;
1709 // If type parsing fails, forwad the parse error for defs.
1710 if (parseLowLevelType(Token.location(), Ty))
1711 return IsDef ? true
1712 : error("expected tied-def or low-level type after '('");
1713
1714 if (expectAndConsume(MIToken::rparen))
1715 return true;
1716
1717 MachineRegisterInfo &MRI = MF.getRegInfo();
1718 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1719 return error("inconsistent type for generic virtual register");
1720
1721 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1722 MRI.setType(Reg, Ty);
1724 }
1725 } else if (IsDef && Reg.isVirtual()) {
1726 // Generic virtual registers defs must have a type.
1727 if (RegInfo->Kind == VRegInfo::GENERIC ||
1728 RegInfo->Kind == VRegInfo::REGBANK)
1729 return error("generic virtual registers must have a type");
1730 }
1731
1732 if (IsDef) {
1733 if (hasRegState(Flags, RegState::Kill))
1734 return error("cannot have a killed def operand");
1735 } else {
1736 if (hasRegState(Flags, RegState::Dead))
1737 return error("cannot have a dead use operand");
1738 }
1739
1741 Reg, IsDef, hasRegState(Flags, RegState::Implicit),
1744 hasRegState(Flags, RegState::EarlyClobber), SubReg,
1748
1749 return false;
1750}
1751
1752bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1754 const APSInt &Int = Token.integerValue();
1755 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1756 Dest = MachineOperand::CreateImm(*SImm);
1757 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1758 Dest = MachineOperand::CreateImm(*UImm);
1759 else
1760 return error("integer literal is too large to be an immediate operand");
1761 lex();
1762 return false;
1763}
1764
1765bool MIParser::parseSymbolicInlineAsmOperand(unsigned OpIdx,
1766 MachineOperand &Dest) {
1768 assert(Token.is(MIToken::Identifier) &&
1769 "expected symbolic inline asm operand");
1770
1771 // Parse ExtraInfo flags.
1772 if (OpIdx == InlineAsm::MIOp_ExtraInfo) {
1773 unsigned ExtraInfo = 0;
1774 for (;;) {
1775 if (Token.isNot(MIToken::Identifier))
1776 break;
1777
1778 StringRef FlagName = Token.stringValue();
1779 unsigned Flag = StringSwitch<unsigned>(FlagName)
1781 .Case("mayload", InlineAsm::Extra_MayLoad)
1782 .Case("maystore", InlineAsm::Extra_MayStore)
1783 .Case("isconvergent", InlineAsm::Extra_IsConvergent)
1784 .Case("alignstack", InlineAsm::Extra_IsAlignStack)
1786 .Case("attdialect", 0)
1787 .Case("inteldialect", InlineAsm::Extra_AsmDialect)
1788 .Default(~0u);
1789 if (Flag == ~0u)
1790 return error("unknown inline asm extra info flag '" + FlagName + "'");
1791
1792 ExtraInfo |= Flag;
1793 lex();
1794 }
1795
1796 Dest = MachineOperand::CreateImm(ExtraInfo);
1797 return false;
1798 }
1799
1800 // Parse symbolic form: kind[:constraint].
1801 StringRef KindStr = Token.stringValue();
1802 constexpr auto InvalidKind = static_cast<InlineAsm::Kind>(0);
1805 .Case("regdef", InlineAsm::Kind::RegDef)
1806 .Case("reguse", InlineAsm::Kind::RegUse)
1808 .Case("clobber", InlineAsm::Kind::Clobber)
1809 .Case("imm", InlineAsm::Kind::Imm)
1810 .Case("mem", InlineAsm::Kind::Mem)
1811 .Default(InvalidKind);
1812 if (K == InvalidKind)
1813 return error("unknown inline asm operand kind '" + KindStr + "'");
1814
1815 lex();
1816
1817 // Create the flag with default of 1 operand.
1818 InlineAsm::Flag F(K, 1);
1819
1820 // Parse optional tiedto constraint: tiedto:$N.
1821 if (Token.is(MIToken::Identifier) && Token.stringValue() == "tiedto") {
1822 lex();
1823 if (Token.isNot(MIToken::colon))
1824 return error("expected ':' after 'tiedto'");
1825 lex();
1826 if (Token.isNot(MIToken::NamedRegister))
1827 return error("expected '$N' operand number after 'tiedto:'");
1828 unsigned OperandNo;
1829 if (Token.stringValue().getAsInteger(10, OperandNo))
1830 return error("invalid operand number in tiedto constraint");
1831 lex();
1832
1833 F.setMatchingOp(OperandNo);
1834
1836 return false;
1837 }
1838
1839 // Parse optional constraint after ':'.
1840 if (Token.isNot(MIToken::colon)) {
1842 return false;
1843 }
1844
1845 lex();
1846
1847 if (Token.isNot(MIToken::Identifier))
1848 return error("expected register class or memory constraint name after ':'");
1849
1850 StringRef ConstraintStr = Token.stringValue();
1851 if (K == InlineAsm::Kind::Mem) {
1884 return error("unknown memory constraint '" + ConstraintStr + "'");
1885 F.setMemConstraint(CC);
1886 } else if (K == InlineAsm::Kind::RegDef || K == InlineAsm::Kind::RegUse ||
1888 const TargetRegisterClass *RC =
1889 PFS.Target.getRegClass(ConstraintStr.lower());
1890 if (!RC)
1891 return error("unknown register class '" + ConstraintStr + "'");
1892 F.setRegClass(RC->getID());
1893 }
1894
1895 lex();
1896
1898 return false;
1899}
1900
1901bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1902 const unsigned OpIdx,
1903 MachineOperand &Dest,
1904 const MIRFormatter &MF) {
1905 assert(Token.is(MIToken::dot));
1906 auto Loc = Token.location(); // record start position
1907 size_t Len = 1; // for "."
1908 lex();
1909
1910 // Handle the case that mnemonic starts with number.
1911 if (Token.is(MIToken::IntegerLiteral)) {
1912 Len += Token.range().size();
1913 lex();
1914 }
1915
1916 StringRef Src;
1917 if (Token.is(MIToken::comma))
1918 Src = StringRef(Loc, Len);
1919 else {
1920 assert(Token.is(MIToken::Identifier));
1921 Src = StringRef(Loc, Len + Token.stringValue().size());
1922 }
1923 int64_t Val;
1924 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1925 [this](StringRef::iterator Loc, const Twine &Msg)
1926 -> bool { return error(Loc, Msg); }))
1927 return true;
1928
1929 Dest = MachineOperand::CreateImm(Val);
1930 if (!Token.is(MIToken::comma))
1931 lex();
1932 return false;
1933}
1934
1936 PerFunctionMIParsingState &PFS, const Constant *&C,
1937 ErrorCallbackType ErrCB) {
1938 auto Source = StringValue.str(); // The source has to be null terminated.
1939 SMDiagnostic Err;
1940 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1941 &PFS.IRSlots);
1942 if (!C)
1943 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1944 return false;
1945}
1946
1947bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1948 const Constant *&C) {
1949 return ::parseIRConstant(
1950 Loc, StringValue, PFS, C,
1951 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1952 return error(Loc, Msg);
1953 });
1954}
1955
1956bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1957 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1958 return true;
1959 lex();
1960 return false;
1961}
1962
1963// See LLT implementation for bit size limits.
1965 return Size != 0 && isUInt<16>(Size);
1966}
1967
1968static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale) {
1969 // A fixed-length vector needs at least two elements.
1970 return NumElts != 0 && (HasVScale || NumElts != 1) && isUInt<16>(NumElts);
1971}
1972
1973static bool verifyAddrSpace(uint64_t AddrSpace) {
1974 return isUInt<24>(AddrSpace);
1975}
1976
1977bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1978 StringRef TypeDigits = Token.range();
1979 if (TypeDigits.consume_front("s") || TypeDigits.consume_front("i") ||
1980 TypeDigits.consume_front("f") || TypeDigits.consume_front("p") ||
1981 TypeDigits.consume_front("bf")) {
1982 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
1983 return error(
1984 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
1985 }
1986
1987 bool Scalar = Token.range().starts_with("s");
1988 if (Scalar || Token.range().starts_with("i")) {
1989 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
1990 if (!ScalarSize) {
1991 Ty = LLT::token();
1992 lex();
1993 return false;
1994 }
1995
1996 if (!verifyScalarSize(ScalarSize))
1997 return error("invalid size for scalar type");
1998
1999 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2000 lex();
2001 return false;
2002 }
2003
2004 if (Token.range().starts_with("p")) {
2005 const DataLayout &DL = MF.getDataLayout();
2006 uint64_t AS = APSInt(TypeDigits).getZExtValue();
2007 if (!verifyAddrSpace(AS))
2008 return error("invalid address space number");
2009
2010 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2011 lex();
2012 return false;
2013 }
2014
2015 if (Token.range().starts_with("f") || Token.range().starts_with("bf")) {
2016 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2017 if (!ScalarSize || !verifyScalarSize(ScalarSize))
2018 return error("invalid size for scalar type");
2019
2020 if (Token.range().starts_with("bf") && ScalarSize != 16)
2021 return error("invalid size for bfloat");
2022
2023 Ty = Token.range().starts_with("bf") ? LLT::bfloat16()
2024 : LLT::floatIEEE(ScalarSize);
2025 lex();
2026 return false;
2027 }
2028
2029 // Now we're looking for a vector.
2030 if (Token.isNot(MIToken::less))
2031 return error(Loc, "expected tN, pA, <M x tN>, <M x pA>, <vscale x M x tN>, "
2032 "or <vscale x M x pA> for GlobalISel type, "
2033 "where t = {'s', 'i', 'f', 'bf'}");
2034 lex();
2035
2036 bool HasVScale =
2037 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2038 if (HasVScale) {
2039 lex();
2040 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2041 return error(
2042 "expected <vscale x M x tN>, where t = {'s', 'i', 'f', 'bf', 'p'}");
2043 lex();
2044 }
2045
2046 auto GetError = [this, &HasVScale, Loc]() {
2047 if (HasVScale)
2048 return error(Loc, "expected <vscale x M x tN> for vector type, where t = "
2049 "{'s', 'i', 'f', 'bf', 'p'}");
2050 return error(Loc, "expected <M x tN> for vector type, where t = {'s', 'i', "
2051 "'f', 'bf', 'p'}");
2052 };
2053
2054 if (Token.isNot(MIToken::IntegerLiteral))
2055 return GetError();
2056 uint64_t NumElements = Token.integerValue().getZExtValue();
2057 if (!verifyVectorElementCount(NumElements, HasVScale))
2058 return error("invalid number of vector elements");
2059
2060 lex();
2061
2062 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2063 return GetError();
2064 lex();
2065
2066 StringRef VectorTyDigits = Token.range();
2067 if (!VectorTyDigits.consume_front("s") &&
2068 !VectorTyDigits.consume_front("i") &&
2069 !VectorTyDigits.consume_front("f") &&
2070 !VectorTyDigits.consume_front("p") && !VectorTyDigits.consume_front("bf"))
2071 return GetError();
2072
2073 if (VectorTyDigits.empty() || !llvm::all_of(VectorTyDigits, isdigit))
2074 return error(
2075 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2076
2077 Scalar = Token.range().starts_with("s");
2078 if (Scalar || Token.range().starts_with("i")) {
2079 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2080 if (!verifyScalarSize(ScalarSize))
2081 return error("invalid size for scalar element in vector");
2082 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2083 } else if (Token.range().starts_with("p")) {
2084 const DataLayout &DL = MF.getDataLayout();
2085 uint64_t AS = APSInt(VectorTyDigits).getZExtValue();
2086 if (!verifyAddrSpace(AS))
2087 return error("invalid address space number");
2088
2089 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2090 } else if (Token.range().starts_with("f")) {
2091 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2092 if (!verifyScalarSize(ScalarSize))
2093 return error("invalid size for float element in vector");
2094 Ty = LLT::floatIEEE(ScalarSize);
2095 } else if (Token.range().starts_with("bf")) {
2096 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2097 if (!verifyScalarSize(ScalarSize))
2098 return error("invalid size for bfloat element in vector");
2099 Ty = LLT::bfloat16();
2100 } else {
2101 return GetError();
2102 }
2103 lex();
2104
2105 if (Token.isNot(MIToken::greater))
2106 return GetError();
2107
2108 lex();
2109
2110 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2111 return false;
2112}
2113
2114bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2115 assert(Token.is(MIToken::Identifier));
2116 StringRef TypeDigits = Token.range();
2117 if (!TypeDigits.consume_front("i") && !TypeDigits.consume_front("s") &&
2118 !TypeDigits.consume_front("p") && !TypeDigits.consume_front("f") &&
2119 !TypeDigits.consume_front("bf"))
2120 return error("a typed immediate operand should start with one of 'i', "
2121 "'s', 'f', 'bf', or 'p'");
2122 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2123 return error(
2124 "expected integers after 'i'/'s'/'f'/'bf'/'p' type identifier");
2125
2126 auto Loc = Token.location();
2127 lex();
2128 if (Token.isNot(MIToken::IntegerLiteral)) {
2129 if (Token.isNot(MIToken::Identifier) ||
2130 !(Token.range() == "true" || Token.range() == "false"))
2131 return error("expected an integer literal");
2132 }
2133 const Constant *C = nullptr;
2134 if (parseIRConstant(Loc, C))
2135 return true;
2137 return false;
2138}
2139
2140bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2141 auto Loc = Token.location();
2142 lex();
2143 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2144 Token.isNot(MIToken::HexLiteral))
2145 return error("expected a floating point literal");
2146 const Constant *C = nullptr;
2147 if (parseIRConstant(Loc, C))
2148 return true;
2150 return false;
2151}
2152
2153static bool getHexUint(const MIToken &Token, APInt &Result) {
2155 StringRef S = Token.range();
2156 assert(S[0] == '0' && tolower(S[1]) == 'x');
2157 // This could be a floating point literal with a special prefix.
2158 if (!isxdigit(S[2]))
2159 return true;
2160 StringRef V = S.substr(2);
2161 APInt A(V.size()*4, V, 16);
2162
2163 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2164 // sure it isn't the case before constructing result.
2165 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2166 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2167 return false;
2168}
2169
2170static bool getUnsigned(const MIToken &Token, unsigned &Result,
2171 ErrorCallbackType ErrCB) {
2172 if (Token.hasIntegerValue()) {
2173 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2174 const APSInt &SInt = Token.integerValue();
2175 if (SInt.isNegative())
2176 return ErrCB(Token.location(), "expected unsigned integer");
2177 uint64_t Val64 = SInt.getLimitedValue(Limit);
2178 if (Val64 == Limit)
2179 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2180 Result = Val64;
2181 return false;
2182 }
2183 if (Token.is(MIToken::HexLiteral)) {
2184 APInt A;
2185 if (getHexUint(Token, A))
2186 return true;
2187 if (A.getBitWidth() > 32)
2188 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2189 Result = A.getZExtValue();
2190 return false;
2191 }
2192 return true;
2193}
2194
2195bool MIParser::getUnsigned(unsigned &Result) {
2196 return ::getUnsigned(
2197 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2198 return error(Loc, Msg);
2199 });
2200}
2201
2202bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2205 unsigned Number;
2206 if (getUnsigned(Number))
2207 return true;
2208 auto MBBInfo = PFS.MBBSlots.find(Number);
2209 if (MBBInfo == PFS.MBBSlots.end())
2210 return error(Twine("use of undefined machine basic block #") +
2211 Twine(Number));
2212 MBB = MBBInfo->second;
2213 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2214 // we drop the <irname> from the bb.<id>.<irname> format.
2215 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2216 return error(Twine("the name of machine basic block #") + Twine(Number) +
2217 " isn't '" + Token.stringValue() + "'");
2218 return false;
2219}
2220
2221bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2224 return true;
2226 lex();
2227 return false;
2228}
2229
2230bool MIParser::parseStackFrameIndex(int &FI) {
2231 assert(Token.is(MIToken::StackObject));
2232 unsigned ID;
2233 if (getUnsigned(ID))
2234 return true;
2235 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2236 if (ObjectInfo == PFS.StackObjectSlots.end())
2237 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2238 "'");
2240 if (const auto *Alloca =
2241 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2242 Name = Alloca->getName();
2243 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2244 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2245 "' isn't '" + Token.stringValue() + "'");
2246 lex();
2247 FI = ObjectInfo->second;
2248 return false;
2249}
2250
2251bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2252 int FI;
2253 if (parseStackFrameIndex(FI))
2254 return true;
2255 Dest = MachineOperand::CreateFI(FI);
2256 return false;
2257}
2258
2259bool MIParser::parseFixedStackFrameIndex(int &FI) {
2261 unsigned ID;
2262 if (getUnsigned(ID))
2263 return true;
2264 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2265 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2266 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2267 Twine(ID) + "'");
2268 lex();
2269 FI = ObjectInfo->second;
2270 return false;
2271}
2272
2273bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2274 int FI;
2275 if (parseFixedStackFrameIndex(FI))
2276 return true;
2277 Dest = MachineOperand::CreateFI(FI);
2278 return false;
2279}
2280
2281static bool parseGlobalValue(const MIToken &Token,
2283 ErrorCallbackType ErrCB) {
2284 switch (Token.kind()) {
2286 const Module *M = PFS.MF.getFunction().getParent();
2287 GV = M->getNamedValue(Token.stringValue());
2288 if (!GV)
2289 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2290 Token.range() + "'");
2291 break;
2292 }
2293 case MIToken::GlobalValue: {
2294 unsigned GVIdx;
2295 if (getUnsigned(Token, GVIdx, ErrCB))
2296 return true;
2297 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2298 if (!GV)
2299 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2300 Twine(GVIdx) + "'");
2301 break;
2302 }
2303 default:
2304 llvm_unreachable("The current token should be a global value");
2305 }
2306 return false;
2307}
2308
2309bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2310 return ::parseGlobalValue(
2311 Token, PFS, GV,
2312 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2313 return error(Loc, Msg);
2314 });
2315}
2316
2317bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2318 GlobalValue *GV = nullptr;
2319 if (parseGlobalValue(GV))
2320 return true;
2321 lex();
2322 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2323 if (parseOperandsOffset(Dest))
2324 return true;
2325 return false;
2326}
2327
2328bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2330 unsigned ID;
2331 if (getUnsigned(ID))
2332 return true;
2333 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2334 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2335 return error("use of undefined constant '%const." + Twine(ID) + "'");
2336 lex();
2337 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2338 if (parseOperandsOffset(Dest))
2339 return true;
2340 return false;
2341}
2342
2343bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2345 unsigned ID;
2346 if (getUnsigned(ID))
2347 return true;
2348 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2349 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2350 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2351 lex();
2352 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2353 return false;
2354}
2355
2356bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2358 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2359 lex();
2360 Dest = MachineOperand::CreateES(Symbol);
2361 if (parseOperandsOffset(Dest))
2362 return true;
2363 return false;
2364}
2365
2366bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2367 assert(Token.is(MIToken::MCSymbol));
2368 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2369 lex();
2370 Dest = MachineOperand::CreateMCSymbol(Symbol);
2371 if (parseOperandsOffset(Dest))
2372 return true;
2373 return false;
2374}
2375
2376bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2378 StringRef Name = Token.stringValue();
2379 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2380 if (SubRegIndex == 0)
2381 return error(Twine("unknown subregister index '") + Name + "'");
2382 lex();
2383 Dest = MachineOperand::CreateImm(SubRegIndex);
2384 return false;
2385}
2386
2387bool MIParser::parseMDNode(MDNode *&Node) {
2388 assert(Token.is(MIToken::exclaim));
2389
2390 auto Loc = Token.location();
2391 lex();
2392 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2393 return error("expected metadata id after '!'");
2394 unsigned ID;
2395 if (getUnsigned(ID))
2396 return true;
2397 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2398 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2399 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2400 if (NodeInfo == PFS.MachineMetadataNodes.end())
2401 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2402 }
2403 lex();
2404 Node = NodeInfo->second.get();
2405 return false;
2406}
2407
2408bool MIParser::parseDIExpression(MDNode *&Expr) {
2409 unsigned Read;
2411 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2412 &PFS.IRSlots);
2413 CurrentSource = CurrentSource.substr(Read);
2414 lex();
2415 if (!Expr)
2416 return error(Error.getMessage());
2417 return false;
2418}
2419
2420bool MIParser::parseDILocation(MDNode *&Loc) {
2421 assert(Token.is(MIToken::md_dilocation));
2422 lex();
2423
2424 bool HaveLine = false;
2425 unsigned Line = 0;
2426 unsigned Column = 0;
2427 MDNode *Scope = nullptr;
2428 MDNode *InlinedAt = nullptr;
2429 bool ImplicitCode = false;
2430 uint64_t AtomGroup = 0;
2431 uint64_t AtomRank = 0;
2432
2433 if (expectAndConsume(MIToken::lparen))
2434 return true;
2435
2436 if (Token.isNot(MIToken::rparen)) {
2437 do {
2438 if (Token.is(MIToken::Identifier)) {
2439 if (Token.stringValue() == "line") {
2440 lex();
2441 if (expectAndConsume(MIToken::colon))
2442 return true;
2443 if (Token.isNot(MIToken::IntegerLiteral) ||
2444 Token.integerValue().isSigned())
2445 return error("expected unsigned integer");
2446 Line = Token.integerValue().getZExtValue();
2447 HaveLine = true;
2448 lex();
2449 continue;
2450 }
2451 if (Token.stringValue() == "column") {
2452 lex();
2453 if (expectAndConsume(MIToken::colon))
2454 return true;
2455 if (Token.isNot(MIToken::IntegerLiteral) ||
2456 Token.integerValue().isSigned())
2457 return error("expected unsigned integer");
2458 Column = Token.integerValue().getZExtValue();
2459 lex();
2460 continue;
2461 }
2462 if (Token.stringValue() == "scope") {
2463 lex();
2464 if (expectAndConsume(MIToken::colon))
2465 return true;
2466 if (parseMDNode(Scope))
2467 return error("expected metadata node");
2468 if (!isa<DIScope>(Scope))
2469 return error("expected DIScope node");
2470 continue;
2471 }
2472 if (Token.stringValue() == "inlinedAt") {
2473 lex();
2474 if (expectAndConsume(MIToken::colon))
2475 return true;
2476 if (Token.is(MIToken::exclaim)) {
2477 if (parseMDNode(InlinedAt))
2478 return true;
2479 } else if (Token.is(MIToken::md_dilocation)) {
2480 if (parseDILocation(InlinedAt))
2481 return true;
2482 } else {
2483 return error("expected metadata node");
2484 }
2485 if (!isa<DILocation>(InlinedAt))
2486 return error("expected DILocation node");
2487 continue;
2488 }
2489 if (Token.stringValue() == "isImplicitCode") {
2490 lex();
2491 if (expectAndConsume(MIToken::colon))
2492 return true;
2493 if (!Token.is(MIToken::Identifier))
2494 return error("expected true/false");
2495 // As far as I can see, we don't have any existing need for parsing
2496 // true/false in MIR yet. Do it ad-hoc until there's something else
2497 // that needs it.
2498 if (Token.stringValue() == "true")
2499 ImplicitCode = true;
2500 else if (Token.stringValue() == "false")
2501 ImplicitCode = false;
2502 else
2503 return error("expected true/false");
2504 lex();
2505 continue;
2506 }
2507 if (Token.stringValue() == "atomGroup") {
2508 lex();
2509 if (expectAndConsume(MIToken::colon))
2510 return true;
2511 if (Token.isNot(MIToken::IntegerLiteral) ||
2512 Token.integerValue().isSigned())
2513 return error("expected unsigned integer");
2514 AtomGroup = Token.integerValue().getZExtValue();
2515 lex();
2516 continue;
2517 }
2518 if (Token.stringValue() == "atomRank") {
2519 lex();
2520 if (expectAndConsume(MIToken::colon))
2521 return true;
2522 if (Token.isNot(MIToken::IntegerLiteral) ||
2523 Token.integerValue().isSigned())
2524 return error("expected unsigned integer");
2525 AtomRank = Token.integerValue().getZExtValue();
2526 lex();
2527 continue;
2528 }
2529 }
2530 return error(Twine("invalid DILocation argument '") +
2531 Token.stringValue() + "'");
2532 } while (consumeIfPresent(MIToken::comma));
2533 }
2534
2535 if (expectAndConsume(MIToken::rparen))
2536 return true;
2537
2538 if (!HaveLine)
2539 return error("DILocation requires line number");
2540 if (!Scope)
2541 return error("DILocation requires a scope");
2542
2543 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2544 InlinedAt, ImplicitCode, AtomGroup, AtomRank);
2545 return false;
2546}
2547
2548bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2549 MDNode *Node = nullptr;
2550 if (Token.is(MIToken::exclaim)) {
2551 if (parseMDNode(Node))
2552 return true;
2553 } else if (Token.is(MIToken::md_diexpr)) {
2554 if (parseDIExpression(Node))
2555 return true;
2556 }
2557 Dest = MachineOperand::CreateMetadata(Node);
2558 return false;
2559}
2560
2561bool MIParser::parseCFIOffset(int &Offset) {
2562 if (Token.isNot(MIToken::IntegerLiteral))
2563 return error("expected a cfi offset");
2564 if (Token.integerValue().getSignificantBits() > 32)
2565 return error("expected a 32 bit integer (the cfi offset is too large)");
2566 Offset = (int)Token.integerValue().getExtValue();
2567 lex();
2568 return false;
2569}
2570
2571bool MIParser::parseCFIUnsigned(unsigned &Value) {
2572 if (getUnsigned(Value))
2573 return true;
2574 lex();
2575 return false;
2576}
2577
2578bool MIParser::parseCFIRegister(unsigned &Reg) {
2579 if (Token.isNot(MIToken::NamedRegister))
2580 return error("expected a cfi register");
2581 Register LLVMReg;
2582 if (parseNamedRegister(LLVMReg))
2583 return true;
2584 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2585 assert(TRI && "Expected target register info");
2586 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2587 if (DwarfReg < 0)
2588 return error("invalid DWARF register");
2589 Reg = (unsigned)DwarfReg;
2590 lex();
2591 return false;
2592}
2593
2594bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2595 if (Token.isNot(MIToken::IntegerLiteral))
2596 return error("expected a cfi address space literal");
2597 if (Token.integerValue().isSigned())
2598 return error("expected an unsigned integer (cfi address space)");
2599 AddressSpace = Token.integerValue().getZExtValue();
2600 lex();
2601 return false;
2602}
2603
2604bool MIParser::parseCFIEscapeValues(std::string &Values) {
2605 do {
2606 if (Token.isNot(MIToken::HexLiteral))
2607 return error("expected a hexadecimal literal");
2608 unsigned Value;
2609 if (getUnsigned(Value))
2610 return true;
2611 if (Value > UINT8_MAX)
2612 return error("expected a 8-bit integer (too large)");
2613 Values.push_back(static_cast<uint8_t>(Value));
2614 lex();
2615 } while (consumeIfPresent(MIToken::comma));
2616 return false;
2617}
2618
2619bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2620 auto Kind = Token.kind();
2621 lex();
2622 int Offset;
2623 unsigned Reg;
2624 unsigned AddressSpace;
2625 unsigned CFIIndex;
2626 switch (Kind) {
2628 if (parseCFIRegister(Reg))
2629 return true;
2630 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2631 break;
2633 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2634 parseCFIOffset(Offset))
2635 return true;
2636 CFIIndex =
2637 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2638 break;
2640 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2641 parseCFIOffset(Offset))
2642 return true;
2643 CFIIndex = MF.addFrameInst(
2645 break;
2647 if (parseCFIRegister(Reg))
2648 return true;
2649 CFIIndex =
2650 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2651 break;
2653 if (parseCFIOffset(Offset))
2654 return true;
2655 CFIIndex =
2656 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2657 break;
2659 if (parseCFIOffset(Offset))
2660 return true;
2661 CFIIndex = MF.addFrameInst(
2663 break;
2665 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2666 parseCFIOffset(Offset))
2667 return true;
2668 CFIIndex =
2669 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2670 break;
2672 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2673 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2674 parseCFIAddressSpace(AddressSpace))
2675 return true;
2676 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2677 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2678 break;
2680 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2681 break;
2683 if (parseCFIRegister(Reg))
2684 return true;
2685 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2686 break;
2688 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2689 break;
2691 if (parseCFIRegister(Reg))
2692 return true;
2693 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2694 break;
2696 unsigned Reg2;
2697 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2698 parseCFIRegister(Reg2))
2699 return true;
2700
2701 CFIIndex =
2702 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2703 break;
2704 }
2706 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2707 break;
2709 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2710 break;
2712 CFIIndex =
2713 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2714 break;
2716 unsigned State;
2717 MCSymbol *PACSym = nullptr;
2718 if (parseCFIUnsigned(State) || expectAndConsume(MIToken::comma))
2719 return true;
2720 if (Token.is(MIToken::MCSymbol)) {
2721 PACSym = getOrCreateMCSymbol(Token.stringValue());
2722 lex();
2723 CFIIndex = MF.addFrameInst(
2724 MCCFIInstruction::createSetRAState(nullptr, State, PACSym));
2725 } else if (Token.is(MIToken::IntegerLiteral)) {
2726 int Offset;
2727 if (parseCFIOffset(Offset))
2728 return true;
2729 CFIIndex = MF.addFrameInst(
2731 } else {
2732 return error("expected '<mcsymbol ...>' or integer offset for "
2733 "cfi_set_ra_state");
2734 }
2735 break;
2736 }
2738 unsigned Reg, R1, R2;
2739 unsigned R1Size, R2Size;
2740 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2741 parseCFIRegister(R1) || expectAndConsume(MIToken::comma) ||
2742 parseCFIUnsigned(R1Size) || expectAndConsume(MIToken::comma) ||
2743 parseCFIRegister(R2) || expectAndConsume(MIToken::comma) ||
2744 parseCFIUnsigned(R2Size))
2745 return true;
2746
2747 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMRegisterPair(
2748 nullptr, Reg, R1, R1Size, R2, R2Size));
2749 break;
2750 }
2752 std::vector<MCCFIInstruction::VectorRegisterWithLane> VectorRegisters;
2753 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma))
2754 return true;
2755 do {
2756 unsigned VR;
2757 unsigned Lane, Size;
2758 if (parseCFIRegister(VR) || expectAndConsume(MIToken::comma) ||
2759 parseCFIUnsigned(Lane) || expectAndConsume(MIToken::comma) ||
2760 parseCFIUnsigned(Size))
2761 return true;
2762 VectorRegisters.push_back({VR, Lane, Size});
2763 } while (consumeIfPresent(MIToken::comma));
2764
2765 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisters(
2766 nullptr, Reg, std::move(VectorRegisters)));
2767 break;
2768 }
2770 unsigned Reg, MaskReg;
2771 unsigned RegSize, MaskRegSize;
2772 int Offset = 0;
2773
2774 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2775 parseCFIUnsigned(RegSize) || expectAndConsume(MIToken::comma) ||
2776 parseCFIRegister(MaskReg) || expectAndConsume(MIToken::comma) ||
2777 parseCFIUnsigned(MaskRegSize) || expectAndConsume(MIToken::comma) ||
2778 parseCFIOffset(Offset))
2779 return true;
2780
2781 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorOffset(
2782 nullptr, Reg, RegSize, MaskReg, MaskRegSize, Offset));
2783 break;
2784 }
2786 unsigned Reg, SpillReg, MaskReg;
2787 unsigned SpillRegLaneSize, MaskRegSize;
2788
2789 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2790 parseCFIRegister(SpillReg) || expectAndConsume(MIToken::comma) ||
2791 parseCFIUnsigned(SpillRegLaneSize) ||
2792 expectAndConsume(MIToken::comma) || parseCFIRegister(MaskReg) ||
2793 expectAndConsume(MIToken::comma) || parseCFIUnsigned(MaskRegSize))
2794 return true;
2795
2796 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisterMask(
2797 nullptr, Reg, SpillReg, SpillRegLaneSize, MaskReg, MaskRegSize));
2798 break;
2799 }
2801 std::string Values;
2802 if (parseCFIEscapeValues(Values))
2803 return true;
2804 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2805 break;
2806 }
2807 default:
2808 // TODO: Parse the other CFI operands.
2809 llvm_unreachable("The current token should be a cfi operand");
2810 }
2811 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2812 return false;
2813}
2814
2815bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2816 switch (Token.kind()) {
2817 case MIToken::NamedIRBlock: {
2819 F.getValueSymbolTable()->lookup(Token.stringValue()));
2820 if (!BB)
2821 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2822 break;
2823 }
2824 case MIToken::IRBlock: {
2825 unsigned SlotNumber = 0;
2826 if (getUnsigned(SlotNumber))
2827 return true;
2828 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2829 if (!BB)
2830 return error(Twine("use of undefined IR block '%ir-block.") +
2831 Twine(SlotNumber) + "'");
2832 break;
2833 }
2834 default:
2835 llvm_unreachable("The current token should be an IR block reference");
2836 }
2837 return false;
2838}
2839
2840bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2842 lex();
2843 if (expectAndConsume(MIToken::lparen))
2844 return true;
2845 if (Token.isNot(MIToken::GlobalValue) &&
2846 Token.isNot(MIToken::NamedGlobalValue))
2847 return error("expected a global value");
2848 GlobalValue *GV = nullptr;
2849 if (parseGlobalValue(GV))
2850 return true;
2851 auto *F = dyn_cast<Function>(GV);
2852 if (!F)
2853 return error("expected an IR function reference");
2854 lex();
2855 if (expectAndConsume(MIToken::comma))
2856 return true;
2857 BasicBlock *BB = nullptr;
2858 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2859 return error("expected an IR block reference");
2860 if (parseIRBlock(BB, *F))
2861 return true;
2862 lex();
2863 if (expectAndConsume(MIToken::rparen))
2864 return true;
2865 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2866 if (parseOperandsOffset(Dest))
2867 return true;
2868 return false;
2869}
2870
2871bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2872 assert(Token.is(MIToken::kw_intrinsic));
2873 lex();
2874 if (expectAndConsume(MIToken::lparen))
2875 return error("expected syntax intrinsic(@llvm.whatever)");
2876
2877 if (Token.isNot(MIToken::NamedGlobalValue))
2878 return error("expected syntax intrinsic(@llvm.whatever)");
2879
2880 std::string Name = std::string(Token.stringValue());
2881 lex();
2882
2883 if (expectAndConsume(MIToken::rparen))
2884 return error("expected ')' to terminate intrinsic name");
2885
2886 // Find out what intrinsic we're dealing with.
2888 if (ID == Intrinsic::not_intrinsic)
2889 return error("unknown intrinsic name");
2891
2892 return false;
2893}
2894
2895bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2896 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2897 bool IsFloat = Token.is(MIToken::kw_floatpred);
2898 lex();
2899
2900 if (expectAndConsume(MIToken::lparen))
2901 return error("expected syntax intpred(whatever) or floatpred(whatever");
2902
2903 if (Token.isNot(MIToken::Identifier))
2904 return error("whatever");
2905
2906 CmpInst::Predicate Pred;
2907 if (IsFloat) {
2908 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2909 .Case("false", CmpInst::FCMP_FALSE)
2910 .Case("oeq", CmpInst::FCMP_OEQ)
2911 .Case("ogt", CmpInst::FCMP_OGT)
2912 .Case("oge", CmpInst::FCMP_OGE)
2913 .Case("olt", CmpInst::FCMP_OLT)
2914 .Case("ole", CmpInst::FCMP_OLE)
2915 .Case("one", CmpInst::FCMP_ONE)
2916 .Case("ord", CmpInst::FCMP_ORD)
2917 .Case("uno", CmpInst::FCMP_UNO)
2918 .Case("ueq", CmpInst::FCMP_UEQ)
2919 .Case("ugt", CmpInst::FCMP_UGT)
2920 .Case("uge", CmpInst::FCMP_UGE)
2921 .Case("ult", CmpInst::FCMP_ULT)
2922 .Case("ule", CmpInst::FCMP_ULE)
2923 .Case("une", CmpInst::FCMP_UNE)
2924 .Case("true", CmpInst::FCMP_TRUE)
2926 if (!CmpInst::isFPPredicate(Pred))
2927 return error("invalid floating-point predicate");
2928 } else {
2929 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2930 .Case("eq", CmpInst::ICMP_EQ)
2931 .Case("ne", CmpInst::ICMP_NE)
2932 .Case("sgt", CmpInst::ICMP_SGT)
2933 .Case("sge", CmpInst::ICMP_SGE)
2934 .Case("slt", CmpInst::ICMP_SLT)
2935 .Case("sle", CmpInst::ICMP_SLE)
2936 .Case("ugt", CmpInst::ICMP_UGT)
2937 .Case("uge", CmpInst::ICMP_UGE)
2938 .Case("ult", CmpInst::ICMP_ULT)
2939 .Case("ule", CmpInst::ICMP_ULE)
2941 if (!CmpInst::isIntPredicate(Pred))
2942 return error("invalid integer predicate");
2943 }
2944
2945 lex();
2947 if (expectAndConsume(MIToken::rparen))
2948 return error("predicate should be terminated by ')'.");
2949
2950 return false;
2951}
2952
2953bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
2955
2956 lex();
2957 if (expectAndConsume(MIToken::lparen))
2958 return error("expected syntax shufflemask(<integer or undef>, ...)");
2959
2960 SmallVector<int, 32> ShufMask;
2961 do {
2962 if (Token.is(MIToken::kw_undef)) {
2963 ShufMask.push_back(-1);
2964 } else if (Token.is(MIToken::IntegerLiteral)) {
2965 const APSInt &Int = Token.integerValue();
2966 ShufMask.push_back(Int.getExtValue());
2967 } else {
2968 return error("expected integer constant");
2969 }
2970
2971 lex();
2972 } while (consumeIfPresent(MIToken::comma));
2973
2974 if (expectAndConsume(MIToken::rparen))
2975 return error("shufflemask should be terminated by ')'.");
2976
2977 if (ShufMask.size() < 2)
2978 return error("shufflemask should have > 1 element");
2979
2980 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
2981 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
2982 return false;
2983}
2984
2985bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
2987
2988 lex();
2989 if (expectAndConsume(MIToken::lparen))
2990 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2991
2992 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2993 return error("expected unsigned integer for instruction index");
2994 uint64_t InstrIdx = Token.integerValue().getZExtValue();
2995 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
2996 "Instruction reference's instruction index is too large");
2997 lex();
2998
2999 if (expectAndConsume(MIToken::comma))
3000 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3001
3002 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3003 return error("expected unsigned integer for operand index");
3004 uint64_t OpIdx = Token.integerValue().getZExtValue();
3005 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
3006 "Instruction reference's operand index is too large");
3007 lex();
3008
3009 if (expectAndConsume(MIToken::rparen))
3010 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3011
3012 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
3013 return false;
3014}
3015
3016bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
3018 lex();
3019 if (expectAndConsume(MIToken::lparen))
3020 return true;
3021 if (Token.isNot(MIToken::Identifier))
3022 return error("expected the name of the target index");
3023 int Index = 0;
3024 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
3025 return error("use of undefined target index '" + Token.stringValue() + "'");
3026 lex();
3027 if (expectAndConsume(MIToken::rparen))
3028 return true;
3029 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
3030 if (parseOperandsOffset(Dest))
3031 return true;
3032 return false;
3033}
3034
3035bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
3036 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
3037 lex();
3038 if (expectAndConsume(MIToken::lparen))
3039 return true;
3040
3041 uint32_t *Mask = MF.allocateRegMask();
3042 do {
3043 if (Token.isNot(MIToken::rparen)) {
3044 if (Token.isNot(MIToken::NamedRegister))
3045 return error("expected a named register");
3046 Register Reg;
3047 if (parseNamedRegister(Reg))
3048 return true;
3049 lex();
3050 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3051 }
3052
3053 // TODO: Report an error if the same register is used more than once.
3054 } while (consumeIfPresent(MIToken::comma));
3055
3056 if (expectAndConsume(MIToken::rparen))
3057 return true;
3058 Dest = MachineOperand::CreateRegMask(Mask);
3059 return false;
3060}
3061
3062bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
3063 assert(Token.is(MIToken::kw_lanemask));
3064
3065 lex();
3066 if (expectAndConsume(MIToken::lparen))
3067 return true;
3068
3069 // Parse lanemask.
3070 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
3071 return error("expected a valid lane mask value");
3072 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
3073 "Use correct get-function for lane mask.");
3075 if (getUint64(V))
3076 return true;
3077 LaneBitmask LaneMask(V);
3078 lex();
3079
3080 if (expectAndConsume(MIToken::rparen))
3081 return true;
3082
3083 Dest = MachineOperand::CreateLaneMask(LaneMask);
3084 return false;
3085}
3086
3087bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
3088 assert(Token.is(MIToken::kw_liveout));
3089 uint32_t *Mask = MF.allocateRegMask();
3090 lex();
3091 if (expectAndConsume(MIToken::lparen))
3092 return true;
3093 while (true) {
3094 if (Token.isNot(MIToken::NamedRegister))
3095 return error("expected a named register");
3096 Register Reg;
3097 if (parseNamedRegister(Reg))
3098 return true;
3099 lex();
3100 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3101 // TODO: Report an error if the same register is used more than once.
3102 if (Token.isNot(MIToken::comma))
3103 break;
3104 lex();
3105 }
3106 if (expectAndConsume(MIToken::rparen))
3107 return true;
3109 return false;
3110}
3111
3112bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
3113 MachineOperand &Dest,
3114 std::optional<unsigned> &TiedDefIdx) {
3115 switch (Token.kind()) {
3118 case MIToken::kw_def:
3119 case MIToken::kw_dead:
3120 case MIToken::kw_killed:
3121 case MIToken::kw_undef:
3130 return parseRegisterOperand(Dest, TiedDefIdx);
3132 // TODO: Forbid numeric operands for INLINEASM once the transition to the
3133 // symbolic form is over.
3134 return parseImmediateOperand(Dest);
3135 case MIToken::kw_half:
3136 case MIToken::kw_bfloat:
3137 case MIToken::kw_float:
3138 case MIToken::kw_double:
3140 case MIToken::kw_fp128:
3142 return parseFPImmediateOperand(Dest);
3144 return parseMBBOperand(Dest);
3146 return parseStackObjectOperand(Dest);
3148 return parseFixedStackObjectOperand(Dest);
3151 return parseGlobalAddressOperand(Dest);
3153 return parseConstantPoolIndexOperand(Dest);
3155 return parseJumpTableIndexOperand(Dest);
3157 return parseExternalSymbolOperand(Dest);
3158 case MIToken::MCSymbol:
3159 return parseMCSymbolOperand(Dest);
3161 return parseSubRegisterIndexOperand(Dest);
3162 case MIToken::md_diexpr:
3163 case MIToken::exclaim:
3164 return parseMetadataOperand(Dest);
3187 return parseCFIOperand(Dest);
3189 return parseBlockAddressOperand(Dest);
3191 return parseIntrinsicOperand(Dest);
3193 return parseTargetIndexOperand(Dest);
3195 return parseLaneMaskOperand(Dest);
3197 return parseLiveoutRegisterMaskOperand(Dest);
3200 return parsePredicateOperand(Dest);
3202 return parseShuffleMaskOperand(Dest);
3204 return parseDbgInstrRefOperand(Dest);
3205 case MIToken::Error:
3206 return true;
3207 case MIToken::Identifier: {
3208 bool IsInlineAsm = OpCode == TargetOpcode::INLINEASM ||
3209 OpCode == TargetOpcode::INLINEASM_BR;
3210 if (IsInlineAsm)
3211 return parseSymbolicInlineAsmOperand(OpIdx, Dest);
3212
3213 StringRef Id = Token.stringValue();
3214 if (const auto *RegMask = PFS.Target.getRegMask(Id)) {
3215 Dest = MachineOperand::CreateRegMask(RegMask);
3216 lex();
3217 break;
3218 } else if (Id == "CustomRegMask") {
3219 return parseCustomRegisterMaskOperand(Dest);
3220 } else {
3221 return parseTypedImmediateOperand(Dest);
3222 }
3223 }
3224 case MIToken::dot: {
3225 const auto *TII = MF.getSubtarget().getInstrInfo();
3226 if (const auto *Formatter = TII->getMIRFormatter()) {
3227 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3228 }
3229 [[fallthrough]];
3230 }
3231 default:
3232 // FIXME: Parse the MCSymbol machine operand.
3233 return error("expected a machine operand");
3234 }
3235 return false;
3236}
3237
3238bool MIParser::parseMachineOperandAndTargetFlags(
3239 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3240 std::optional<unsigned> &TiedDefIdx) {
3241 unsigned TF = 0;
3242 bool HasTargetFlags = false;
3243 if (Token.is(MIToken::kw_target_flags)) {
3244 HasTargetFlags = true;
3245 lex();
3246 if (expectAndConsume(MIToken::lparen))
3247 return true;
3248 if (Token.isNot(MIToken::Identifier))
3249 return error("expected the name of the target flag");
3250 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3251 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3252 return error("use of undefined target flag '" + Token.stringValue() +
3253 "'");
3254 }
3255 lex();
3256 while (Token.is(MIToken::comma)) {
3257 lex();
3258 if (Token.isNot(MIToken::Identifier))
3259 return error("expected the name of the target flag");
3260 unsigned BitFlag = 0;
3261 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3262 return error("use of undefined target flag '" + Token.stringValue() +
3263 "'");
3264 // TODO: Report an error when using a duplicate bit target flag.
3265 TF |= BitFlag;
3266 lex();
3267 }
3268 if (expectAndConsume(MIToken::rparen))
3269 return true;
3270 }
3271 auto Loc = Token.location();
3272 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3273 return true;
3274 if (!HasTargetFlags)
3275 return false;
3276 if (Dest.isReg())
3277 return error(Loc, "register operands can't have target flags");
3278 Dest.setTargetFlags(TF);
3279 return false;
3280}
3281
3282bool MIParser::parseOffset(int64_t &Offset) {
3283 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3284 return false;
3285 StringRef Sign = Token.range();
3286 bool IsNegative = Token.is(MIToken::minus);
3287 lex();
3288 if (Token.isNot(MIToken::IntegerLiteral))
3289 return error("expected an integer literal after '" + Sign + "'");
3290 if (Token.integerValue().getSignificantBits() > 64)
3291 return error("expected 64-bit integer (too large)");
3292 Offset = Token.integerValue().getExtValue();
3293 if (IsNegative)
3294 Offset = -Offset;
3295 lex();
3296 return false;
3297}
3298
3299bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3301 lex();
3302 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3303 return error("expected basic block after 'ir_block_address_taken'");
3304
3305 if (parseIRBlock(BB, MF.getFunction()))
3306 return true;
3307
3308 lex();
3309 return false;
3310}
3311
3312bool MIParser::parseAlignment(uint64_t &Alignment) {
3313 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3314 lex();
3315 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3316 return error("expected an integer literal after 'align'");
3317 if (getUint64(Alignment))
3318 return true;
3319 lex();
3320
3321 if (!isPowerOf2_64(Alignment))
3322 return error("expected a power-of-2 literal after 'align'");
3323
3324 return false;
3325}
3326
3327bool MIParser::parseAddrspace(unsigned &Addrspace) {
3328 assert(Token.is(MIToken::kw_addrspace));
3329 lex();
3330 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3331 return error("expected an integer literal after 'addrspace'");
3332 if (getUnsigned(Addrspace))
3333 return true;
3334 lex();
3335 return false;
3336}
3337
3338bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3339 int64_t Offset = 0;
3340 if (parseOffset(Offset))
3341 return true;
3342 Op.setOffset(Offset);
3343 return false;
3344}
3345
3346static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3347 const Value *&V, ErrorCallbackType ErrCB) {
3348 switch (Token.kind()) {
3349 case MIToken::NamedIRValue: {
3350 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3351 break;
3352 }
3353 case MIToken::IRValue: {
3354 unsigned SlotNumber = 0;
3355 if (getUnsigned(Token, SlotNumber, ErrCB))
3356 return true;
3357 V = PFS.getIRValue(SlotNumber);
3358 break;
3359 }
3361 case MIToken::GlobalValue: {
3362 GlobalValue *GV = nullptr;
3363 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3364 return true;
3365 V = GV;
3366 break;
3367 }
3369 const Constant *C = nullptr;
3370 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3371 return true;
3372 V = C;
3373 break;
3374 }
3376 V = nullptr;
3377 return false;
3378 default:
3379 llvm_unreachable("The current token should be an IR block reference");
3380 }
3381 if (!V)
3382 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3383 return false;
3384}
3385
3386bool MIParser::parseIRValue(const Value *&V) {
3387 return ::parseIRValue(
3388 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3389 return error(Loc, Msg);
3390 });
3391}
3392
3393bool MIParser::getUint64(uint64_t &Result) {
3394 if (Token.hasIntegerValue()) {
3395 if (Token.integerValue().getActiveBits() > 64)
3396 return error("expected 64-bit integer (too large)");
3397 Result = Token.integerValue().getZExtValue();
3398 return false;
3399 }
3400 if (Token.is(MIToken::HexLiteral)) {
3401 APInt A;
3402 if (getHexUint(A))
3403 return true;
3404 if (A.getBitWidth() > 64)
3405 return error("expected 64-bit integer (too large)");
3406 Result = A.getZExtValue();
3407 return false;
3408 }
3409 return true;
3410}
3411
3412bool MIParser::getHexUint(APInt &Result) {
3413 return ::getHexUint(Token, Result);
3414}
3415
3416bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3417 const auto OldFlags = Flags;
3418 switch (Token.kind()) {
3421 break;
3424 break;
3427 break;
3430 break;
3433 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3434 return error("use of undefined target MMO flag '" + Token.stringValue() +
3435 "'");
3436 Flags |= TF;
3437 break;
3438 }
3439 default:
3440 llvm_unreachable("The current token should be a memory operand flag");
3441 }
3442 if (OldFlags == Flags)
3443 // We know that the same flag is specified more than once when the flags
3444 // weren't modified.
3445 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3446 lex();
3447 return false;
3448}
3449
3450bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3451 switch (Token.kind()) {
3452 case MIToken::kw_stack:
3453 PSV = MF.getPSVManager().getStack();
3454 break;
3455 case MIToken::kw_got:
3456 PSV = MF.getPSVManager().getGOT();
3457 break;
3459 PSV = MF.getPSVManager().getJumpTable();
3460 break;
3462 PSV = MF.getPSVManager().getConstantPool();
3463 break;
3465 int FI;
3466 if (parseFixedStackFrameIndex(FI))
3467 return true;
3468 PSV = MF.getPSVManager().getFixedStack(FI);
3469 // The token was already consumed, so use return here instead of break.
3470 return false;
3471 }
3472 case MIToken::StackObject: {
3473 int FI;
3474 if (parseStackFrameIndex(FI))
3475 return true;
3476 PSV = MF.getPSVManager().getFixedStack(FI);
3477 // The token was already consumed, so use return here instead of break.
3478 return false;
3479 }
3481 lex();
3482 switch (Token.kind()) {
3485 GlobalValue *GV = nullptr;
3486 if (parseGlobalValue(GV))
3487 return true;
3488 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3489 break;
3490 }
3492 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3493 MF.createExternalSymbolName(Token.stringValue()));
3494 break;
3495 default:
3496 return error(
3497 "expected a global value or an external symbol after 'call-entry'");
3498 }
3499 break;
3500 case MIToken::kw_custom: {
3501 lex();
3502 const auto *TII = MF.getSubtarget().getInstrInfo();
3503 if (const auto *Formatter = TII->getMIRFormatter()) {
3504 if (Formatter->parseCustomPseudoSourceValue(
3505 Token.stringValue(), MF, PFS, PSV,
3506 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3507 return error(Loc, Msg);
3508 }))
3509 return true;
3510 } else {
3511 return error("unable to parse target custom pseudo source value");
3512 }
3513 break;
3514 }
3515 default:
3516 llvm_unreachable("The current token should be pseudo source value");
3517 }
3518 lex();
3519 return false;
3520}
3521
3522bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3523 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3524 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3525 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3526 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3527 const PseudoSourceValue *PSV = nullptr;
3528 if (parseMemoryPseudoSourceValue(PSV))
3529 return true;
3530 int64_t Offset = 0;
3531 if (parseOffset(Offset))
3532 return true;
3533 Dest = MachinePointerInfo(PSV, Offset);
3534 return false;
3535 }
3536 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3537 Token.isNot(MIToken::GlobalValue) &&
3538 Token.isNot(MIToken::NamedGlobalValue) &&
3539 Token.isNot(MIToken::QuotedIRValue) &&
3540 Token.isNot(MIToken::kw_unknown_address))
3541 return error("expected an IR value reference");
3542 const Value *V = nullptr;
3543 if (parseIRValue(V))
3544 return true;
3545 if (V && !V->getType()->isPointerTy())
3546 return error("expected a pointer IR value");
3547 lex();
3548 int64_t Offset = 0;
3549 if (parseOffset(Offset))
3550 return true;
3551 Dest = MachinePointerInfo(V, Offset);
3552 return false;
3553}
3554
3555bool MIParser::parseOptionalScope(LLVMContext &Context,
3556 SyncScope::ID &SSID) {
3557 SSID = SyncScope::System;
3558 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3559 lex();
3560 if (expectAndConsume(MIToken::lparen))
3561 return error("expected '(' in syncscope");
3562
3563 std::string SSN;
3564 if (parseStringConstant(SSN))
3565 return true;
3566
3567 SSID = Context.getOrInsertSyncScopeID(SSN);
3568 if (expectAndConsume(MIToken::rparen))
3569 return error("expected ')' in syncscope");
3570 }
3571
3572 return false;
3573}
3574
3575bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3577 if (Token.isNot(MIToken::Identifier))
3578 return false;
3579
3580 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3581 .Case("unordered", AtomicOrdering::Unordered)
3582 .Case("monotonic", AtomicOrdering::Monotonic)
3583 .Case("acquire", AtomicOrdering::Acquire)
3584 .Case("release", AtomicOrdering::Release)
3588
3589 if (Order != AtomicOrdering::NotAtomic) {
3590 lex();
3591 return false;
3592 }
3593
3594 return error("expected an atomic scope, ordering or a size specification");
3595}
3596
3597bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3598 if (expectAndConsume(MIToken::lparen))
3599 return true;
3601 while (Token.isMemoryOperandFlag()) {
3602 if (parseMemoryOperandFlag(Flags))
3603 return true;
3604 }
3605 if (Token.isNot(MIToken::Identifier) ||
3606 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3607 return error("expected 'load' or 'store' memory operation");
3608 if (Token.stringValue() == "load")
3610 else
3612 lex();
3613
3614 // Optional 'store' for operands that both load and store.
3615 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3617 lex();
3618 }
3619
3620 // Optional synchronization scope.
3621 SyncScope::ID SSID;
3622 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3623 return true;
3624
3625 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3626 AtomicOrdering Order, FailureOrder;
3627 if (parseOptionalAtomicOrdering(Order))
3628 return true;
3629
3630 if (parseOptionalAtomicOrdering(FailureOrder))
3631 return true;
3632
3633 if (Token.isNot(MIToken::IntegerLiteral) &&
3634 Token.isNot(MIToken::kw_unknown_size) &&
3635 Token.isNot(MIToken::lparen))
3636 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3637 "memory operation");
3638
3640 if (Token.is(MIToken::IntegerLiteral)) {
3641 uint64_t Size;
3642 if (getUint64(Size))
3643 return true;
3644
3645 // Convert from bytes to bits for storage.
3647 lex();
3648 } else if (Token.is(MIToken::kw_unknown_size)) {
3649 lex();
3650 } else {
3651 if (expectAndConsume(MIToken::lparen))
3652 return true;
3653 if (parseLowLevelType(Token.location(), MemoryType))
3654 return true;
3655 if (expectAndConsume(MIToken::rparen))
3656 return true;
3657 }
3658
3660 if (Token.is(MIToken::Identifier)) {
3661 const char *Word =
3664 ? "on"
3665 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3666 if (Token.stringValue() != Word)
3667 return error(Twine("expected '") + Word + "'");
3668 lex();
3669
3670 if (parseMachinePointerInfo(Ptr))
3671 return true;
3672 }
3673 uint64_t BaseAlignment =
3674 MemoryType.isValid()
3675 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3676 : 1;
3677 AAMDNodes AAInfo;
3678 MDNode *Range = nullptr;
3679 MDNode *MemCacheHint = nullptr;
3680 while (consumeIfPresent(MIToken::comma)) {
3681 switch (Token.kind()) {
3682 case MIToken::kw_align: {
3683 // align is printed if it is different than size.
3685 if (parseAlignment(Alignment))
3686 return true;
3687 if (Ptr.Offset & (Alignment - 1)) {
3688 // MachineMemOperand::getAlign never returns a value greater than the
3689 // alignment of offset, so this just guards against hand-written MIR
3690 // that specifies a large "align" value when it should probably use
3691 // "basealign" instead.
3692 return error("specified alignment is more aligned than offset");
3693 }
3694 BaseAlignment = Alignment;
3695 break;
3696 }
3698 // basealign is printed if it is different than align.
3699 if (parseAlignment(BaseAlignment))
3700 return true;
3701 break;
3703 if (parseAddrspace(Ptr.AddrSpace))
3704 return true;
3705 break;
3706 case MIToken::md_tbaa:
3707 lex();
3708 if (parseMDNode(AAInfo.TBAA))
3709 return true;
3710 break;
3712 lex();
3713 if (parseMDNode(AAInfo.Scope))
3714 return true;
3715 break;
3717 lex();
3718 if (parseMDNode(AAInfo.NoAlias))
3719 return true;
3720 break;
3722 lex();
3723 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3724 return true;
3725 break;
3726 case MIToken::md_range:
3727 lex();
3728 if (parseMDNode(Range))
3729 return true;
3730 break;
3732 lex();
3733 if (parseMDNode(MemCacheHint))
3734 return true;
3735 break;
3736 // TODO: Report an error on duplicate metadata nodes.
3737 default:
3738 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3739 "'!noalias' or '!range' or '!mem.cache_hint' or "
3740 "'!noalias.addrspace'");
3741 }
3742 }
3743 if (expectAndConsume(MIToken::rparen))
3744 return true;
3745 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3746 MMOMetadata(AAInfo, Range, MemCacheHint), SSID,
3747 Order, FailureOrder);
3748 return false;
3749}
3750
3751bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3753 Token.is(MIToken::kw_post_instr_symbol)) &&
3754 "Invalid token for a pre- post-instruction symbol!");
3755 lex();
3756 if (Token.isNot(MIToken::MCSymbol))
3757 return error("expected a symbol after 'pre-instr-symbol'");
3758 Symbol = getOrCreateMCSymbol(Token.stringValue());
3759 lex();
3760 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3761 Token.is(MIToken::lbrace))
3762 return false;
3763 if (Token.isNot(MIToken::comma))
3764 return error("expected ',' before the next machine operand");
3765 lex();
3766 return false;
3767}
3768
3769bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3771 "Invalid token for a heap alloc marker!");
3772 lex();
3773 if (parseMDNode(Node))
3774 return true;
3775 if (!Node)
3776 return error("expected a MDNode after 'heap-alloc-marker'");
3777 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3778 Token.is(MIToken::lbrace))
3779 return false;
3780 if (Token.isNot(MIToken::comma))
3781 return error("expected ',' before the next machine operand");
3782 lex();
3783 return false;
3784}
3785
3786bool MIParser::parsePCSections(MDNode *&Node) {
3787 assert(Token.is(MIToken::kw_pcsections) &&
3788 "Invalid token for a PC sections!");
3789 lex();
3790 if (parseMDNode(Node))
3791 return true;
3792 if (!Node)
3793 return error("expected a MDNode after 'pcsections'");
3794 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3795 Token.is(MIToken::lbrace))
3796 return false;
3797 if (Token.isNot(MIToken::comma))
3798 return error("expected ',' before the next machine operand");
3799 lex();
3800 return false;
3801}
3802
3803bool MIParser::parseMMRA(MDNode *&Node) {
3804 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3805 lex();
3806 if (parseMDNode(Node))
3807 return true;
3808 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3809 Token.is(MIToken::lbrace))
3810 return false;
3811 if (Token.isNot(MIToken::comma))
3812 return error("expected ',' before the next machine operand");
3813 lex();
3814 return false;
3815}
3816
3818 const Function &F,
3819 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3820 ModuleSlotTracker MST(F.getParent());
3822 for (const auto &BB : F) {
3823 if (BB.hasName())
3824 continue;
3825 int Slot = MST.getLocalSlot(&BB);
3826 if (Slot == -1)
3827 continue;
3828 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3829 }
3830}
3831
3833 unsigned Slot,
3834 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3835 return Slots2BasicBlocks.lookup(Slot);
3836}
3837
3838const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3839 if (Slots2BasicBlocks.empty())
3840 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3841 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3842}
3843
3844const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3845 if (&F == &MF.getFunction())
3846 return getIRBlock(Slot);
3847 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3848 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3849 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3850}
3851
3852MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3853 // FIXME: Currently we can't recognize temporary or local symbols and call all
3854 // of the appropriate forms to create them. However, this handles basic cases
3855 // well as most of the special aspects are recognized by a prefix on their
3856 // name, and the input names should already be unique. For test cases, keeping
3857 // the symbol name out of the symbol table isn't terribly important.
3858 return MF.getContext().getOrCreateSymbol(Name);
3859}
3860
3861bool MIParser::parseStringConstant(std::string &Result) {
3862 if (Token.isNot(MIToken::StringConstant))
3863 return error("expected string constant");
3864 Result = std::string(Token.stringValue());
3865 lex();
3866 return false;
3867}
3868
3870 StringRef Src,
3872 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3873}
3874
3877 return MIParser(PFS, Error, Src).parseBasicBlocks();
3878}
3879
3883 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3884}
3885
3887 Register &Reg, StringRef Src,
3889 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3890}
3891
3893 Register &Reg, StringRef Src,
3895 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3896}
3897
3899 VRegInfo *&Info, StringRef Src,
3901 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3902}
3903
3906 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3907}
3908
3912 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
3913}
3916 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3917}
3918
3920 PerFunctionMIParsingState &PFS, const Value *&V,
3921 ErrorCallbackType ErrorCallback) {
3922 MIToken Token;
3923 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3924 ErrorCallback(Loc, Msg);
3925 });
3926 V = nullptr;
3927
3928 return ::parseIRValue(Token, PFS, V, ErrorCallback);
3929}
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
unsigned 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:605
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:1561
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:472
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:285
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:319
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:311
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:68
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:1578
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.
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 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 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:308
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:37
unsigned getMainFileID() const
Definition SourceMgr.h:151
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition SourceMgr.h:144
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:81
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:58
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)
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:218
bool hasIntegerValue() const
Definition MILexer.h:258
bool is(TokenKind K) const
Definition MILexer.h:245
StringRef stringValue() const
Return the token's string value.
Definition MILexer.h:254
@ kw_pre_instr_symbol
Definition MILexer.h:142
@ kw_deactivation_symbol
Definition MILexer.h:147
@ kw_call_frame_size
Definition MILexer.h:154
@ 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:177
@ kw_dbg_instr_ref
Definition MILexer.h:85
@ NamedVirtualRegister
Definition MILexer.h:175
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:187
@ 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:176
@ 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_cfi_def_cfa_offset
Definition MILexer.h:90
@ md_mem_cache_hint
Definition MILexer.h:168
@ kw_machine_block_address_taken
Definition MILexer.h:153
@ kw_cfi_remember_state
Definition MILexer.h:96
@ kw_debug_instr_number
Definition MILexer.h:84
@ kw_post_instr_symbol
Definition MILexer.h:143
@ kw_cfi_restore_state
Definition MILexer.h:98
@ kw_ir_block_address_taken
Definition MILexer.h:152
@ kw_unknown_address
Definition MILexer.h:151
@ md_noalias_addrspace
Definition MILexer.h:166
@ kw_debug_location
Definition MILexer.h:83
@ kw_heap_alloc_marker
Definition MILexer.h:144
StringRef range() const
Definition MILexer.h:251
StringRef::iterator location() const
Definition MILexer.h:249
const APSInt & integerValue() const
Definition MILexer.h:256
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:172
LLVM_ABI const Value * getIRValue(unsigned Slot)
Definition MIParser.cpp:374
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition MIParser.h:177
StringMap< VRegInfo * > VRegInfosNamed
Definition MIParser.h:179
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition MIParser.h:186
LLVM_ABI PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition MIParser.cpp:324
PerTargetMIParsingState & Target
Definition MIParser.h:173
DenseMap< Register, VRegInfo * > VRegInfos
Definition MIParser.h:178
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