LLVM 23.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/InstrTypes.h"
51#include "llvm/IR/Intrinsics.h"
52#include "llvm/IR/Metadata.h"
53#include "llvm/IR/Module.h"
55#include "llvm/IR/Type.h"
56#include "llvm/IR/Value.h"
58#include "llvm/MC/LaneBitmask.h"
59#include "llvm/MC/MCContext.h"
60#include "llvm/MC/MCDwarf.h"
61#include "llvm/MC/MCInstrDesc.h"
67#include "llvm/Support/SMLoc.h"
70#include <cassert>
71#include <cctype>
72#include <cstddef>
73#include <cstdint>
74#include <limits>
75#include <string>
76#include <utility>
77
78using namespace llvm;
79
81 const TargetSubtargetInfo &NewSubtarget) {
82
83 // If the subtarget changed, over conservatively assume everything is invalid.
84 if (&Subtarget == &NewSubtarget)
85 return;
86
87 Names2InstrOpCodes.clear();
88 Names2Regs.clear();
89 Names2RegMasks.clear();
90 Names2SubRegIndices.clear();
91 Names2TargetIndices.clear();
92 Names2DirectTargetFlags.clear();
93 Names2BitmaskTargetFlags.clear();
94 Names2MMOTargetFlags.clear();
95
96 initNames2RegClasses();
97 initNames2RegBanks();
98}
99
100void PerTargetMIParsingState::initNames2Regs() {
101 if (!Names2Regs.empty())
102 return;
103
104 // The '%noreg' register is the register 0.
105 Names2Regs.insert(std::make_pair("noreg", 0));
106 const auto *TRI = Subtarget.getRegisterInfo();
107 assert(TRI && "Expected target register info");
108
109 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
110 bool WasInserted =
111 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
112 .second;
113 (void)WasInserted;
114 assert(WasInserted && "Expected registers to be unique case-insensitively");
115 }
116}
117
119 Register &Reg) {
120 initNames2Regs();
121 auto RegInfo = Names2Regs.find(RegName);
122 if (RegInfo == Names2Regs.end())
123 return true;
124 Reg = RegInfo->getValue();
125 return false;
126}
127
129 uint8_t &FlagValue) const {
130 const auto *TRI = Subtarget.getRegisterInfo();
131 std::optional<uint8_t> FV = TRI->getVRegFlagValue(FlagName);
132 if (!FV)
133 return true;
134 FlagValue = *FV;
135 return false;
136}
137
138void PerTargetMIParsingState::initNames2InstrOpCodes() {
139 if (!Names2InstrOpCodes.empty())
140 return;
141 const auto *TII = Subtarget.getInstrInfo();
142 assert(TII && "Expected target instruction info");
143 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
144 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
145}
146
148 unsigned &OpCode) {
149 initNames2InstrOpCodes();
150 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
151 if (InstrInfo == Names2InstrOpCodes.end())
152 return true;
153 OpCode = InstrInfo->getValue();
154 return false;
155}
156
157void PerTargetMIParsingState::initNames2RegMasks() {
158 if (!Names2RegMasks.empty())
159 return;
160 const auto *TRI = Subtarget.getRegisterInfo();
161 assert(TRI && "Expected target register info");
162 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
163 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
164 assert(RegMasks.size() == RegMaskNames.size());
165 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
166 Names2RegMasks.insert(
167 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
168}
169
171 initNames2RegMasks();
172 auto RegMaskInfo = Names2RegMasks.find(Identifier);
173 if (RegMaskInfo == Names2RegMasks.end())
174 return nullptr;
175 return RegMaskInfo->getValue();
176}
177
178void PerTargetMIParsingState::initNames2SubRegIndices() {
179 if (!Names2SubRegIndices.empty())
180 return;
181 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
182 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
183 Names2SubRegIndices.insert(
184 std::make_pair(TRI->getSubRegIndexName(I), I));
185}
186
188 initNames2SubRegIndices();
189 auto SubRegInfo = Names2SubRegIndices.find(Name);
190 if (SubRegInfo == Names2SubRegIndices.end())
191 return 0;
192 return SubRegInfo->getValue();
193}
194
195void PerTargetMIParsingState::initNames2TargetIndices() {
196 if (!Names2TargetIndices.empty())
197 return;
198 const auto *TII = Subtarget.getInstrInfo();
199 assert(TII && "Expected target instruction info");
200 auto Indices = TII->getSerializableTargetIndices();
201 for (const auto &I : Indices)
202 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
203}
204
206 initNames2TargetIndices();
207 auto IndexInfo = Names2TargetIndices.find(Name);
208 if (IndexInfo == Names2TargetIndices.end())
209 return true;
210 Index = IndexInfo->second;
211 return false;
212}
213
214void PerTargetMIParsingState::initNames2DirectTargetFlags() {
215 if (!Names2DirectTargetFlags.empty())
216 return;
217
218 const auto *TII = Subtarget.getInstrInfo();
219 assert(TII && "Expected target instruction info");
220 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
221 for (const auto &I : Flags)
222 Names2DirectTargetFlags.insert(
223 std::make_pair(StringRef(I.second), I.first));
224}
225
227 unsigned &Flag) {
228 initNames2DirectTargetFlags();
229 auto FlagInfo = Names2DirectTargetFlags.find(Name);
230 if (FlagInfo == Names2DirectTargetFlags.end())
231 return true;
232 Flag = FlagInfo->second;
233 return false;
234}
235
236void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
237 if (!Names2BitmaskTargetFlags.empty())
238 return;
239
240 const auto *TII = Subtarget.getInstrInfo();
241 assert(TII && "Expected target instruction info");
242 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
243 for (const auto &I : Flags)
244 Names2BitmaskTargetFlags.insert(
245 std::make_pair(StringRef(I.second), I.first));
246}
247
249 unsigned &Flag) {
250 initNames2BitmaskTargetFlags();
251 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
252 if (FlagInfo == Names2BitmaskTargetFlags.end())
253 return true;
254 Flag = FlagInfo->second;
255 return false;
256}
257
258void PerTargetMIParsingState::initNames2MMOTargetFlags() {
259 if (!Names2MMOTargetFlags.empty())
260 return;
261
262 const auto *TII = Subtarget.getInstrInfo();
263 assert(TII && "Expected target instruction info");
264 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
265 for (const auto &I : Flags)
266 Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
267}
268
271 initNames2MMOTargetFlags();
272 auto FlagInfo = Names2MMOTargetFlags.find(Name);
273 if (FlagInfo == Names2MMOTargetFlags.end())
274 return true;
275 Flag = FlagInfo->second;
276 return false;
277}
278
279void PerTargetMIParsingState::initNames2RegClasses() {
280 if (!Names2RegClasses.empty())
281 return;
282
283 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
284 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
285 const auto *RC = TRI->getRegClass(I);
286 Names2RegClasses.insert(
287 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
288 }
289}
290
291void PerTargetMIParsingState::initNames2RegBanks() {
292 if (!Names2RegBanks.empty())
293 return;
294
295 const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
296 // If the target does not support GlobalISel, we may not have a
297 // register bank info.
298 if (!RBI)
299 return;
300
301 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
302 const auto &RegBank = RBI->getRegBank(I);
303 Names2RegBanks.insert(
304 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
305 }
306}
307
310 auto RegClassInfo = Names2RegClasses.find(Name);
311 if (RegClassInfo == Names2RegClasses.end())
312 return nullptr;
313 return RegClassInfo->getValue();
314}
315
317 auto RegBankInfo = Names2RegBanks.find(Name);
318 if (RegBankInfo == Names2RegBanks.end())
319 return nullptr;
320 return RegBankInfo->getValue();
321}
322
327
329 auto I = VRegInfos.try_emplace(Num);
330 if (I.second) {
331 MachineRegisterInfo &MRI = MF.getRegInfo();
332 VRegInfo *Info = new (Allocator) VRegInfo;
334 I.first->second = Info;
335 }
336 return *I.first->second;
337}
338
340 assert(RegName != "" && "Expected named reg.");
341
342 auto I = VRegInfosNamed.try_emplace(RegName.str());
343 if (I.second) {
344 VRegInfo *Info = new (Allocator) VRegInfo;
345 Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
346 I.first->second = Info;
347 }
348 return *I.first->second;
349}
350
351static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
352 DenseMap<unsigned, const Value *> &Slots2Values) {
353 int Slot = MST.getLocalSlot(V);
354 if (Slot == -1)
355 return;
356 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
357}
358
359/// Creates the mapping from slot numbers to function's unnamed IR values.
360static void initSlots2Values(const Function &F,
361 DenseMap<unsigned, const Value *> &Slots2Values) {
362 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
364 for (const auto &Arg : F.args())
365 mapValueToSlot(&Arg, MST, Slots2Values);
366 for (const auto &BB : F) {
367 mapValueToSlot(&BB, MST, Slots2Values);
368 for (const auto &I : BB)
369 mapValueToSlot(&I, MST, Slots2Values);
370 }
371}
372
374 if (Slots2Values.empty())
375 initSlots2Values(MF.getFunction(), Slots2Values);
376 return Slots2Values.lookup(Slot);
377}
378
379namespace {
380
381/// A wrapper struct around the 'MachineOperand' struct that includes a source
382/// range and other attributes.
383struct ParsedMachineOperand {
384 MachineOperand Operand;
387 std::optional<unsigned> TiedDefIdx;
388
389 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
391 std::optional<unsigned> &TiedDefIdx)
392 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
393 if (TiedDefIdx)
394 assert(Operand.isReg() && Operand.isUse() &&
395 "Only used register operands can be tied");
396 }
397};
398
399class MIParser {
400 MachineFunction &MF;
401 SMDiagnostic &Error;
402 StringRef Source, CurrentSource;
403 SMRange SourceRange;
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 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
413 StringRef Source, SMRange SourceRange);
414
415 /// \p SkipChar gives the number of characters to skip before looking
416 /// for the next token.
417 void lex(unsigned SkipChar = 0);
418
419 /// Report an error at the current location with the given message.
420 ///
421 /// This function always return true.
422 bool error(const Twine &Msg);
423
424 /// Report an error at the given location with the given message.
425 ///
426 /// This function always return true.
427 bool error(StringRef::iterator Loc, const Twine &Msg);
428
429 bool
430 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
431 bool parseBasicBlocks();
432 bool parse(MachineInstr *&MI);
433 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
434 bool parseStandaloneNamedRegister(Register &Reg);
435 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
436 bool parseStandaloneRegister(Register &Reg);
437 bool parseStandaloneStackObject(int &FI);
438 bool parseStandaloneMDNode(MDNode *&Node);
440 bool parseMDTuple(MDNode *&MD, bool IsDistinct);
441 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
442 bool parseMetadata(Metadata *&MD);
443
444 bool
445 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
446 bool parseBasicBlock(MachineBasicBlock &MBB,
447 MachineBasicBlock *&AddFalthroughFrom);
448 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
449 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
450
451 bool parseNamedRegister(Register &Reg);
452 bool parseVirtualRegister(VRegInfo *&Info);
453 bool parseNamedVirtualRegister(VRegInfo *&Info);
454 bool parseRegister(Register &Reg, VRegInfo *&VRegInfo);
455 bool parseRegisterFlag(RegState &Flags);
456 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
457 bool parseSubRegisterIndex(unsigned &SubReg);
458 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
459 bool parseRegisterOperand(MachineOperand &Dest,
460 std::optional<unsigned> &TiedDefIdx,
461 bool IsDef = false);
462 bool parseImmediateOperand(MachineOperand &Dest);
463 bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
464 const Constant *&C);
465 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
466 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
467 bool parseTypedImmediateOperand(MachineOperand &Dest);
468 bool parseFPImmediateOperand(MachineOperand &Dest);
469 bool parseMBBReference(MachineBasicBlock *&MBB);
470 bool parseMBBOperand(MachineOperand &Dest);
471 bool parseStackFrameIndex(int &FI);
472 bool parseStackObjectOperand(MachineOperand &Dest);
473 bool parseFixedStackFrameIndex(int &FI);
474 bool parseFixedStackObjectOperand(MachineOperand &Dest);
475 bool parseGlobalValue(GlobalValue *&GV);
476 bool parseGlobalAddressOperand(MachineOperand &Dest);
477 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
478 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
479 bool parseJumpTableIndexOperand(MachineOperand &Dest);
480 bool parseExternalSymbolOperand(MachineOperand &Dest);
481 bool parseMCSymbolOperand(MachineOperand &Dest);
482 [[nodiscard]] bool parseMDNode(MDNode *&Node);
483 bool parseDIExpression(MDNode *&Expr);
484 bool parseDILocation(MDNode *&Expr);
485 bool parseMetadataOperand(MachineOperand &Dest);
486 bool parseCFIOffset(int &Offset);
487 bool parseCFIRegister(unsigned &Reg);
488 bool parseCFIAddressSpace(unsigned &AddressSpace);
489 bool parseCFIEscapeValues(std::string& Values);
490 bool parseCFIOperand(MachineOperand &Dest);
491 bool parseIRBlock(BasicBlock *&BB, const Function &F);
492 bool parseBlockAddressOperand(MachineOperand &Dest);
493 bool parseIntrinsicOperand(MachineOperand &Dest);
494 bool parsePredicateOperand(MachineOperand &Dest);
495 bool parseShuffleMaskOperand(MachineOperand &Dest);
496 bool parseTargetIndexOperand(MachineOperand &Dest);
497 bool parseDbgInstrRefOperand(MachineOperand &Dest);
498 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
499 bool parseLaneMaskOperand(MachineOperand &Dest);
500 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
501 bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
502 MachineOperand &Dest,
503 std::optional<unsigned> &TiedDefIdx);
504 bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
505 const unsigned OpIdx,
506 MachineOperand &Dest,
507 std::optional<unsigned> &TiedDefIdx);
508 bool parseOffset(int64_t &Offset);
509 bool parseIRBlockAddressTaken(BasicBlock *&BB);
510 bool parseAlignment(uint64_t &Alignment);
511 bool parseAddrspace(unsigned &Addrspace);
512 bool parseSectionID(std::optional<MBBSectionID> &SID);
513 bool parseBBID(std::optional<UniqueBBID> &BBID);
514 bool parseCallFrameSize(unsigned &CallFrameSize);
515 bool parsePrefetchTarget(CallsiteID &Target);
516 bool parseOperandsOffset(MachineOperand &Op);
517 bool parseIRValue(const Value *&V);
518 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
519 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
520 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
521 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
522 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
523 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
524 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
525 bool parseHeapAllocMarker(MDNode *&Node);
526 bool parsePCSections(MDNode *&Node);
527 bool parseMMRA(MDNode *&Node);
528
529 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
530 MachineOperand &Dest, const MIRFormatter &MF);
531
532private:
533 /// Convert the integer literal in the current token into an unsigned integer.
534 ///
535 /// Return true if an error occurred.
536 bool getUnsigned(unsigned &Result);
537
538 /// Convert the integer literal in the current token into an uint64.
539 ///
540 /// Return true if an error occurred.
541 bool getUint64(uint64_t &Result);
542
543 /// Convert the hexadecimal literal in the current token into an unsigned
544 /// APInt with a minimum bitwidth required to represent the value.
545 ///
546 /// Return true if the literal does not represent an integer value.
547 bool getHexUint(APInt &Result);
548
549 /// If the current token is of the given kind, consume it and return false.
550 /// Otherwise report an error and return true.
551 bool expectAndConsume(MIToken::TokenKind TokenKind);
552
553 /// If the current token is of the given kind, consume it and return true.
554 /// Otherwise return false.
555 bool consumeIfPresent(MIToken::TokenKind TokenKind);
556
557 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
558
559 bool assignRegisterTies(MachineInstr &MI,
561
562 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
563 const MCInstrDesc &MCID);
564
565 const BasicBlock *getIRBlock(unsigned Slot);
566 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
567
568 /// Get or create an MCSymbol for a given name.
569 MCSymbol *getOrCreateMCSymbol(StringRef Name);
570
571 /// parseStringConstant
572 /// ::= StringConstant
573 bool parseStringConstant(std::string &Result);
574
575 /// Map the location in the MI string to the corresponding location specified
576 /// in `SourceRange`.
577 SMLoc mapSMLoc(StringRef::iterator Loc);
578};
579
580} // end anonymous namespace
581
582MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
583 StringRef Source)
584 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
585{}
586
587MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
588 StringRef Source, SMRange SourceRange)
589 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source),
590 SourceRange(SourceRange), PFS(PFS) {}
591
592void MIParser::lex(unsigned SkipChar) {
593 CurrentSource = lexMIToken(
594 CurrentSource.substr(SkipChar), Token,
595 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
596}
597
598bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
599
600bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
601 const SourceMgr &SM = *PFS.SM;
602 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
603 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
604 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
605 // Create an ordinary diagnostic when the source manager's buffer is the
606 // source string.
608 return true;
609 }
610 // Create a diagnostic for a YAML string literal.
612 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
613 Source, {}, {});
614 return true;
615}
616
617SMLoc MIParser::mapSMLoc(StringRef::iterator Loc) {
618 assert(SourceRange.isValid() && "Invalid source range");
619 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
620 return SMLoc::getFromPointer(SourceRange.Start.getPointer() +
621 (Loc - Source.data()));
622}
623
624typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
626
627static const char *toString(MIToken::TokenKind TokenKind) {
628 switch (TokenKind) {
629 case MIToken::comma:
630 return "','";
631 case MIToken::equal:
632 return "'='";
633 case MIToken::colon:
634 return "':'";
635 case MIToken::lparen:
636 return "'('";
637 case MIToken::rparen:
638 return "')'";
639 default:
640 return "<unknown token>";
641 }
642}
643
644bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
645 if (Token.isNot(TokenKind))
646 return error(Twine("expected ") + toString(TokenKind));
647 lex();
648 return false;
649}
650
651bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
652 if (Token.isNot(TokenKind))
653 return false;
654 lex();
655 return true;
656}
657
658// Parse Machine Basic Block Section ID.
659bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
661 lex();
662 if (Token.is(MIToken::IntegerLiteral)) {
663 unsigned Value = 0;
664 if (getUnsigned(Value))
665 return error("Unknown Section ID");
666 SID = MBBSectionID{Value};
667 } else {
668 const StringRef &S = Token.stringValue();
669 if (S == "Exception")
671 else if (S == "Cold")
673 else
674 return error("Unknown Section ID");
675 }
676 lex();
677 return false;
678}
679
680// Parse Machine Basic Block ID.
681bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) {
682 if (Token.isNot(MIToken::kw_bb_id))
683 return error("expected 'bb_id'");
684 lex();
685 unsigned BaseID = 0;
686 unsigned CloneID = 0;
687 if (Token.is(MIToken::FloatingPointLiteral)) {
688 StringRef S = Token.range();
689 auto Parts = S.split('.');
690 if (Parts.first.getAsInteger(10, BaseID) ||
691 Parts.second.getAsInteger(10, CloneID))
692 return error("Unknown BB ID");
693 lex();
694 } else {
695 if (getUnsigned(BaseID))
696 return error("Unknown BB ID");
697 lex();
698 if (Token.is(MIToken::comma) || Token.is(MIToken::dot)) {
699 lex();
700 if (getUnsigned(CloneID))
701 return error("Unknown Clone ID");
702 lex();
703 } else if (Token.is(MIToken::IntegerLiteral)) {
704 if (getUnsigned(CloneID))
705 return error("Unknown Clone ID");
706 lex();
707 }
708 }
709 BBID = {BaseID, CloneID};
710 return false;
711}
712
713// Parse basic block call frame size.
714bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) {
716 lex();
717 unsigned Value = 0;
718 if (getUnsigned(Value))
719 return error("Unknown call frame size");
720 CallFrameSize = Value;
721 lex();
722 return false;
723}
724
725bool MIParser::parsePrefetchTarget(CallsiteID &Target) {
726 lex();
727 std::optional<UniqueBBID> BBID;
728 if (parseBBID(BBID))
729 return true;
730 Target.BBID = *BBID;
731 if (expectAndConsume(MIToken::comma))
732 return true;
733 return getUnsigned(Target.CallsiteIndex);
734}
735
736bool MIParser::parseBasicBlockDefinition(
739 unsigned ID = 0;
740 if (getUnsigned(ID))
741 return true;
742 auto Loc = Token.location();
743 auto Name = Token.stringValue();
744 lex();
745 bool MachineBlockAddressTaken = false;
746 BasicBlock *AddressTakenIRBlock = nullptr;
747 bool IsLandingPad = false;
748 bool IsInlineAsmBrIndirectTarget = false;
749 bool IsEHFuncletEntry = false;
750 bool IsEHScopeEntry = false;
751 std::optional<MBBSectionID> SectionID;
752 uint64_t Alignment = 0;
753 std::optional<UniqueBBID> BBID;
754 unsigned CallFrameSize = 0;
755 BasicBlock *BB = nullptr;
756 if (consumeIfPresent(MIToken::lparen)) {
757 do {
758 // TODO: Report an error when multiple same attributes are specified.
759 switch (Token.kind()) {
761 MachineBlockAddressTaken = true;
762 lex();
763 break;
765 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
766 return true;
767 break;
769 IsLandingPad = true;
770 lex();
771 break;
773 IsInlineAsmBrIndirectTarget = true;
774 lex();
775 break;
777 IsEHFuncletEntry = true;
778 lex();
779 break;
781 IsEHScopeEntry = true;
782 lex();
783 break;
785 if (parseAlignment(Alignment))
786 return true;
787 break;
788 case MIToken::IRBlock:
790 // TODO: Report an error when both name and ir block are specified.
791 if (parseIRBlock(BB, MF.getFunction()))
792 return true;
793 lex();
794 break;
796 if (parseSectionID(SectionID))
797 return true;
798 break;
800 if (parseBBID(BBID))
801 return true;
802 break;
804 if (parseCallFrameSize(CallFrameSize))
805 return true;
806 break;
807 default:
808 break;
809 }
810 } while (consumeIfPresent(MIToken::comma));
811 if (expectAndConsume(MIToken::rparen))
812 return true;
813 }
814 if (expectAndConsume(MIToken::colon))
815 return true;
816
817 if (!Name.empty()) {
819 MF.getFunction().getValueSymbolTable()->lookup(Name));
820 if (!BB)
821 return error(Loc, Twine("basic block '") + Name +
822 "' is not defined in the function '" +
823 MF.getName() + "'");
824 }
825 auto *MBB = MF.CreateMachineBasicBlock(BB, BBID);
826 MF.insert(MF.end(), MBB);
827 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
828 if (!WasInserted)
829 return error(Loc, Twine("redefinition of machine basic block with id #") +
830 Twine(ID));
831 if (Alignment)
832 MBB->setAlignment(Align(Alignment));
833 if (MachineBlockAddressTaken)
835 if (AddressTakenIRBlock)
836 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
837 MBB->setIsEHPad(IsLandingPad);
838 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
839 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
840 MBB->setIsEHScopeEntry(IsEHScopeEntry);
841 if (SectionID) {
842 MBB->setSectionID(*SectionID);
843 MF.setBBSectionsType(BasicBlockSection::List);
844 }
845 MBB->setCallFrameSize(CallFrameSize);
846 return false;
847}
848
849bool MIParser::parseBasicBlockDefinitions(
851 lex();
852 // Skip until the first machine basic block.
853 while (Token.is(MIToken::Newline))
854 lex();
855 if (Token.isErrorOrEOF())
856 return Token.isError();
857 if (Token.isNot(MIToken::MachineBasicBlockLabel))
858 return error("expected a basic block definition before instructions");
859 unsigned BraceDepth = 0;
860 do {
861 if (parseBasicBlockDefinition(MBBSlots))
862 return true;
863 bool IsAfterNewline = false;
864 // Skip until the next machine basic block.
865 while (true) {
866 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
867 Token.isErrorOrEOF())
868 break;
869 else if (Token.is(MIToken::MachineBasicBlockLabel))
870 return error("basic block definition should be located at the start of "
871 "the line");
872 else if (consumeIfPresent(MIToken::Newline)) {
873 IsAfterNewline = true;
874 continue;
875 }
876 IsAfterNewline = false;
877 if (Token.is(MIToken::lbrace))
878 ++BraceDepth;
879 if (Token.is(MIToken::rbrace)) {
880 if (!BraceDepth)
881 return error("extraneous closing brace ('}')");
882 --BraceDepth;
883 }
884 lex();
885 }
886 // Verify that we closed all of the '{' at the end of a file or a block.
887 if (!Token.isError() && BraceDepth)
888 return error("expected '}'"); // FIXME: Report a note that shows '{'.
889 } while (!Token.isErrorOrEOF());
890 return Token.isError();
891}
892
893bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
894 assert(Token.is(MIToken::kw_liveins));
895 lex();
896 if (expectAndConsume(MIToken::colon))
897 return true;
898 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
899 return false;
900 do {
901 if (Token.isNot(MIToken::NamedRegister))
902 return error("expected a named register");
904 if (parseNamedRegister(Reg))
905 return true;
906 lex();
908 if (consumeIfPresent(MIToken::colon)) {
909 // Parse lane mask.
910 if (Token.isNot(MIToken::IntegerLiteral) &&
911 Token.isNot(MIToken::HexLiteral))
912 return error("expected a lane mask");
913 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
914 "Use correct get-function for lane mask");
916 if (getUint64(V))
917 return error("invalid lane mask value");
918 Mask = LaneBitmask(V);
919 lex();
920 }
921 MBB.addLiveIn(Reg, Mask);
922 } while (consumeIfPresent(MIToken::comma));
923 return false;
924}
925
926bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
928 lex();
929 if (expectAndConsume(MIToken::colon))
930 return true;
931 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
932 return false;
933 do {
934 if (Token.isNot(MIToken::MachineBasicBlock))
935 return error("expected a machine basic block reference");
936 MachineBasicBlock *SuccMBB = nullptr;
937 if (parseMBBReference(SuccMBB))
938 return true;
939 lex();
940 unsigned Weight = 0;
941 if (consumeIfPresent(MIToken::lparen)) {
942 if (Token.isNot(MIToken::IntegerLiteral) &&
943 Token.isNot(MIToken::HexLiteral))
944 return error("expected an integer literal after '('");
945 if (getUnsigned(Weight))
946 return true;
947 lex();
948 if (expectAndConsume(MIToken::rparen))
949 return true;
950 }
952 } while (consumeIfPresent(MIToken::comma));
954 return false;
955}
956
957bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
958 MachineBasicBlock *&AddFalthroughFrom) {
959 // Skip the definition.
961 lex();
962 if (consumeIfPresent(MIToken::lparen)) {
963 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
964 lex();
965 consumeIfPresent(MIToken::rparen);
966 }
967 consumeIfPresent(MIToken::colon);
968
969 // Parse the liveins and successors.
970 // N.B: Multiple lists of successors and liveins are allowed and they're
971 // merged into one.
972 // Example:
973 // liveins: $edi
974 // liveins: $esi
975 //
976 // is equivalent to
977 // liveins: $edi, $esi
978 bool ExplicitSuccessors = false;
979 while (true) {
980 if (Token.is(MIToken::kw_successors)) {
981 if (parseBasicBlockSuccessors(MBB))
982 return true;
983 ExplicitSuccessors = true;
984 } else if (Token.is(MIToken::kw_liveins)) {
985 if (parseBasicBlockLiveins(MBB))
986 return true;
987 } else if (consumeIfPresent(MIToken::Newline)) {
988 continue;
989 } else {
990 break;
991 }
992 if (!Token.isNewlineOrEOF())
993 return error("expected line break at the end of a list");
994 lex();
995 }
996
997 // Parse the instructions.
998 bool IsInBundle = false;
999 MachineInstr *PrevMI = nullptr;
1000 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
1001 !Token.is(MIToken::Eof)) {
1002 if (consumeIfPresent(MIToken::Newline))
1003 continue;
1004 if (consumeIfPresent(MIToken::rbrace)) {
1005 // The first parsing pass should verify that all closing '}' have an
1006 // opening '{'.
1007 assert(IsInBundle);
1008 IsInBundle = false;
1009 continue;
1010 }
1011 MachineInstr *MI = nullptr;
1012 if (parse(MI))
1013 return true;
1014 MBB.insert(MBB.end(), MI);
1015 if (IsInBundle) {
1017 MI->setFlag(MachineInstr::BundledPred);
1018 }
1019 PrevMI = MI;
1020 if (Token.is(MIToken::lbrace)) {
1021 if (IsInBundle)
1022 return error("nested instruction bundles are not allowed");
1023 lex();
1024 // This instruction is the start of the bundle.
1025 MI->setFlag(MachineInstr::BundledSucc);
1026 IsInBundle = true;
1027 if (!Token.is(MIToken::Newline))
1028 // The next instruction can be on the same line.
1029 continue;
1030 }
1031 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
1032 lex();
1033 }
1034
1035 // Construct successor list by searching for basic block machine operands.
1036 if (!ExplicitSuccessors) {
1038 bool IsFallthrough;
1039 guessSuccessors(MBB, Successors, IsFallthrough);
1040 for (MachineBasicBlock *Succ : Successors)
1041 MBB.addSuccessor(Succ);
1042
1043 if (IsFallthrough) {
1044 AddFalthroughFrom = &MBB;
1045 } else {
1047 }
1048 }
1049
1050 return false;
1051}
1052
1053bool MIParser::parseBasicBlocks() {
1054 lex();
1055 // Skip until the first machine basic block.
1056 while (Token.is(MIToken::Newline))
1057 lex();
1058 if (Token.isErrorOrEOF())
1059 return Token.isError();
1060 // The first parsing pass should have verified that this token is a MBB label
1061 // in the 'parseBasicBlockDefinitions' method.
1063 MachineBasicBlock *AddFalthroughFrom = nullptr;
1064 do {
1065 MachineBasicBlock *MBB = nullptr;
1067 return true;
1068 if (AddFalthroughFrom) {
1069 if (!AddFalthroughFrom->isSuccessor(MBB))
1070 AddFalthroughFrom->addSuccessor(MBB);
1071 AddFalthroughFrom->normalizeSuccProbs();
1072 AddFalthroughFrom = nullptr;
1073 }
1074 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1075 return true;
1076 // The method 'parseBasicBlock' should parse the whole block until the next
1077 // block or the end of file.
1078 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1079 } while (Token.isNot(MIToken::Eof));
1080 return false;
1081}
1082
1083bool MIParser::parse(MachineInstr *&MI) {
1084 // Parse any register operands before '='
1087 while (Token.isRegister() || Token.isRegisterFlag()) {
1088 auto Loc = Token.location();
1089 std::optional<unsigned> TiedDefIdx;
1090 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1091 return true;
1092 Operands.push_back(
1093 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1094 if (Token.isNot(MIToken::comma))
1095 break;
1096 lex();
1097 }
1098 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1099 return true;
1100
1101 unsigned OpCode, Flags = 0;
1102 if (Token.isError() || parseInstruction(OpCode, Flags))
1103 return true;
1104
1105 // Parse the remaining machine operands.
1106 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1107 Token.isNot(MIToken::kw_post_instr_symbol) &&
1108 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1109 Token.isNot(MIToken::kw_pcsections) && Token.isNot(MIToken::kw_mmra) &&
1110 Token.isNot(MIToken::kw_cfi_type) &&
1111 Token.isNot(MIToken::kw_deactivation_symbol) &&
1112 Token.isNot(MIToken::kw_debug_location) &&
1113 Token.isNot(MIToken::kw_debug_instr_number) &&
1114 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1115 auto Loc = Token.location();
1116 std::optional<unsigned> TiedDefIdx;
1117 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1118 return true;
1119 Operands.push_back(
1120 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1121 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1122 Token.is(MIToken::lbrace))
1123 break;
1124 if (Token.isNot(MIToken::comma))
1125 return error("expected ',' before the next machine operand");
1126 lex();
1127 }
1128
1129 MCSymbol *PreInstrSymbol = nullptr;
1130 if (Token.is(MIToken::kw_pre_instr_symbol))
1131 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1132 return true;
1133 MCSymbol *PostInstrSymbol = nullptr;
1134 if (Token.is(MIToken::kw_post_instr_symbol))
1135 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1136 return true;
1137 MDNode *HeapAllocMarker = nullptr;
1138 if (Token.is(MIToken::kw_heap_alloc_marker))
1139 if (parseHeapAllocMarker(HeapAllocMarker))
1140 return true;
1141 MDNode *PCSections = nullptr;
1142 if (Token.is(MIToken::kw_pcsections))
1143 if (parsePCSections(PCSections))
1144 return true;
1145 MDNode *MMRA = nullptr;
1146 if (Token.is(MIToken::kw_mmra) && parseMMRA(MMRA))
1147 return true;
1148 unsigned CFIType = 0;
1149 if (Token.is(MIToken::kw_cfi_type)) {
1150 lex();
1151 if (Token.isNot(MIToken::IntegerLiteral))
1152 return error("expected an integer literal after 'cfi-type'");
1153 // getUnsigned is sufficient for 32-bit integers.
1154 if (getUnsigned(CFIType))
1155 return true;
1156 lex();
1157 // Lex past trailing comma if present.
1158 if (Token.is(MIToken::comma))
1159 lex();
1160 }
1161
1162 GlobalValue *DS = nullptr;
1163 if (Token.is(MIToken::kw_deactivation_symbol)) {
1164 lex();
1165 if (parseGlobalValue(DS))
1166 return true;
1167 lex();
1168 }
1169
1170 unsigned InstrNum = 0;
1171 if (Token.is(MIToken::kw_debug_instr_number)) {
1172 lex();
1173 if (Token.isNot(MIToken::IntegerLiteral))
1174 return error("expected an integer literal after 'debug-instr-number'");
1175 if (getUnsigned(InstrNum))
1176 return true;
1177 lex();
1178 // Lex past trailing comma if present.
1179 if (Token.is(MIToken::comma))
1180 lex();
1181 }
1182
1183 DebugLoc DebugLocation;
1184 if (Token.is(MIToken::kw_debug_location)) {
1185 lex();
1186 MDNode *Node = nullptr;
1187 if (Token.is(MIToken::exclaim)) {
1188 if (parseMDNode(Node))
1189 return true;
1190 } else if (Token.is(MIToken::md_dilocation)) {
1191 if (parseDILocation(Node))
1192 return true;
1193 } else {
1194 return error("expected a metadata node after 'debug-location'");
1195 }
1196 if (!isa<DILocation>(Node))
1197 return error("referenced metadata is not a DILocation");
1198 DebugLocation = DebugLoc(Node);
1199 }
1200
1201 // Parse the machine memory operands.
1203 if (Token.is(MIToken::coloncolon)) {
1204 lex();
1205 while (!Token.isNewlineOrEOF()) {
1206 MachineMemOperand *MemOp = nullptr;
1207 if (parseMachineMemoryOperand(MemOp))
1208 return true;
1209 MemOperands.push_back(MemOp);
1210 if (Token.isNewlineOrEOF())
1211 break;
1212 if (OpCode == TargetOpcode::BUNDLE && Token.is(MIToken::lbrace))
1213 break;
1214 if (Token.isNot(MIToken::comma))
1215 return error("expected ',' before the next machine memory operand");
1216 lex();
1217 }
1218 }
1219
1220 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1221 if (!MCID.isVariadic()) {
1222 // FIXME: Move the implicit operand verification to the machine verifier.
1223 if (verifyImplicitOperands(Operands, MCID))
1224 return true;
1225 }
1226
1227 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1228 MI->setFlags(Flags);
1229
1230 // Don't check the operands make sense, let the verifier catch any
1231 // improprieties.
1232 for (const auto &Operand : Operands)
1233 MI->addOperand(MF, Operand.Operand);
1234
1235 if (assignRegisterTies(*MI, Operands))
1236 return true;
1237 if (PreInstrSymbol)
1238 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1239 if (PostInstrSymbol)
1240 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1241 if (HeapAllocMarker)
1242 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1243 if (PCSections)
1244 MI->setPCSections(MF, PCSections);
1245 if (MMRA)
1246 MI->setMMRAMetadata(MF, MMRA);
1247 if (CFIType)
1248 MI->setCFIType(MF, CFIType);
1249 if (DS)
1250 MI->setDeactivationSymbol(MF, DS);
1251 if (!MemOperands.empty())
1252 MI->setMemRefs(MF, MemOperands);
1253 if (InstrNum)
1254 MI->setDebugInstrNum(InstrNum);
1255 return false;
1256}
1257
1258bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1259 lex();
1260 if (Token.isNot(MIToken::MachineBasicBlock))
1261 return error("expected a machine basic block reference");
1263 return true;
1264 lex();
1265 if (Token.isNot(MIToken::Eof))
1266 return error(
1267 "expected end of string after the machine basic block reference");
1268 return false;
1269}
1270
1271bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1272 lex();
1273 if (Token.isNot(MIToken::NamedRegister))
1274 return error("expected a named register");
1275 if (parseNamedRegister(Reg))
1276 return true;
1277 lex();
1278 if (Token.isNot(MIToken::Eof))
1279 return error("expected end of string after the register reference");
1280 return false;
1281}
1282
1283bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1284 lex();
1285 if (Token.isNot(MIToken::VirtualRegister))
1286 return error("expected a virtual register");
1287 if (parseVirtualRegister(Info))
1288 return true;
1289 lex();
1290 if (Token.isNot(MIToken::Eof))
1291 return error("expected end of string after the register reference");
1292 return false;
1293}
1294
1295bool MIParser::parseStandaloneRegister(Register &Reg) {
1296 lex();
1297 if (Token.isNot(MIToken::NamedRegister) &&
1298 Token.isNot(MIToken::VirtualRegister))
1299 return error("expected either a named or virtual register");
1300
1301 VRegInfo *Info;
1302 if (parseRegister(Reg, Info))
1303 return true;
1304
1305 lex();
1306 if (Token.isNot(MIToken::Eof))
1307 return error("expected end of string after the register reference");
1308 return false;
1309}
1310
1311bool MIParser::parseStandaloneStackObject(int &FI) {
1312 lex();
1313 if (Token.isNot(MIToken::StackObject))
1314 return error("expected a stack object");
1315 if (parseStackFrameIndex(FI))
1316 return true;
1317 if (Token.isNot(MIToken::Eof))
1318 return error("expected end of string after the stack object reference");
1319 return false;
1320}
1321
1322bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1323 lex();
1324 if (Token.is(MIToken::exclaim)) {
1325 if (parseMDNode(Node))
1326 return true;
1327 } else if (Token.is(MIToken::md_diexpr)) {
1328 if (parseDIExpression(Node))
1329 return true;
1330 } else if (Token.is(MIToken::md_dilocation)) {
1331 if (parseDILocation(Node))
1332 return true;
1333 } else {
1334 return error("expected a metadata node");
1335 }
1336 if (Token.isNot(MIToken::Eof))
1337 return error("expected end of string after the metadata node");
1338 return false;
1339}
1340
1341bool MIParser::parseMachineMetadata() {
1342 lex();
1343 if (Token.isNot(MIToken::exclaim))
1344 return error("expected a metadata node");
1345
1346 lex();
1347 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1348 return error("expected metadata id after '!'");
1349 unsigned ID = 0;
1350 if (getUnsigned(ID))
1351 return true;
1352 lex();
1353 if (expectAndConsume(MIToken::equal))
1354 return true;
1355 bool IsDistinct = Token.is(MIToken::kw_distinct);
1356 if (IsDistinct)
1357 lex();
1358 if (Token.isNot(MIToken::exclaim))
1359 return error("expected a metadata node");
1360 lex();
1361
1362 MDNode *MD;
1363 if (parseMDTuple(MD, IsDistinct))
1364 return true;
1365
1366 auto FI = PFS.MachineForwardRefMDNodes.find(ID);
1367 if (FI != PFS.MachineForwardRefMDNodes.end()) {
1368 FI->second.first->replaceAllUsesWith(MD);
1369 PFS.MachineForwardRefMDNodes.erase(FI);
1370
1371 assert(PFS.MachineMetadataNodes[ID] == MD && "Tracking VH didn't work");
1372 } else {
1373 auto [It, Inserted] = PFS.MachineMetadataNodes.try_emplace(ID);
1374 if (!Inserted)
1375 return error("Metadata id is already used");
1376 It->second.reset(MD);
1377 }
1378
1379 return false;
1380}
1381
1382bool MIParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
1384 if (parseMDNodeVector(Elts))
1385 return true;
1386 MD = (IsDistinct ? MDTuple::getDistinct
1387 : MDTuple::get)(MF.getFunction().getContext(), Elts);
1388 return false;
1389}
1390
1391bool MIParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
1392 if (Token.isNot(MIToken::lbrace))
1393 return error("expected '{' here");
1394 lex();
1395
1396 if (Token.is(MIToken::rbrace)) {
1397 lex();
1398 return false;
1399 }
1400
1401 do {
1402 Metadata *MD;
1403 if (parseMetadata(MD))
1404 return true;
1405
1406 Elts.push_back(MD);
1407
1408 if (Token.isNot(MIToken::comma))
1409 break;
1410 lex();
1411 } while (true);
1412
1413 if (Token.isNot(MIToken::rbrace))
1414 return error("expected end of metadata node");
1415 lex();
1416
1417 return false;
1418}
1419
1420// ::= !42
1421// ::= !"string"
1422bool MIParser::parseMetadata(Metadata *&MD) {
1423 if (Token.isNot(MIToken::exclaim))
1424 return error("expected '!' here");
1425 lex();
1426
1427 if (Token.is(MIToken::StringConstant)) {
1428 std::string Str;
1429 if (parseStringConstant(Str))
1430 return true;
1431 MD = MDString::get(MF.getFunction().getContext(), Str);
1432 return false;
1433 }
1434
1435 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1436 return error("expected metadata id after '!'");
1437
1438 SMLoc Loc = mapSMLoc(Token.location());
1439
1440 unsigned ID = 0;
1441 if (getUnsigned(ID))
1442 return true;
1443 lex();
1444
1445 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1446 if (NodeInfo != PFS.IRSlots.MetadataNodes.end()) {
1447 MD = NodeInfo->second.get();
1448 return false;
1449 }
1450 // Check machine metadata.
1451 NodeInfo = PFS.MachineMetadataNodes.find(ID);
1452 if (NodeInfo != PFS.MachineMetadataNodes.end()) {
1453 MD = NodeInfo->second.get();
1454 return false;
1455 }
1456 // Forward reference.
1457 auto &FwdRef = PFS.MachineForwardRefMDNodes[ID];
1458 FwdRef = std::make_pair(
1459 MDTuple::getTemporary(MF.getFunction().getContext(), {}), Loc);
1460 PFS.MachineMetadataNodes[ID].reset(FwdRef.first.get());
1461 MD = FwdRef.first.get();
1462
1463 return false;
1464}
1465
1466static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1467 assert(MO.isImplicit());
1468 return MO.isDef() ? "implicit-def" : "implicit";
1469}
1470
1471static std::string getRegisterName(const TargetRegisterInfo *TRI,
1472 Register Reg) {
1473 assert(Reg.isPhysical() && "expected phys reg");
1474 return StringRef(TRI->getName(Reg)).lower();
1475}
1476
1477/// Return true if the parsed machine operands contain a given machine operand.
1478static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1480 for (const auto &I : Operands) {
1481 if (ImplicitOperand.isIdenticalTo(I.Operand))
1482 return true;
1483 }
1484 return false;
1485}
1486
1487bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1488 const MCInstrDesc &MCID) {
1489 if (MCID.isCall())
1490 // We can't verify call instructions as they can contain arbitrary implicit
1491 // register and register mask operands.
1492 return false;
1493
1494 // Gather all the expected implicit operands.
1495 SmallVector<MachineOperand, 4> ImplicitOperands;
1496 for (MCPhysReg ImpDef : MCID.implicit_defs())
1497 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1498 for (MCPhysReg ImpUse : MCID.implicit_uses())
1499 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1500
1501 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1502 assert(TRI && "Expected target register info");
1503 for (const auto &I : ImplicitOperands) {
1504 if (isImplicitOperandIn(I, Operands))
1505 continue;
1506 return error(Operands.empty() ? Token.location() : Operands.back().End,
1507 Twine("missing implicit register operand '") +
1509 getRegisterName(TRI, I.getReg()) + "'");
1510 }
1511 return false;
1512}
1513
1514bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1515 // Allow frame and fast math flags for OPCODE
1516 // clang-format off
1517 while (Token.is(MIToken::kw_frame_setup) ||
1518 Token.is(MIToken::kw_frame_destroy) ||
1519 Token.is(MIToken::kw_nnan) ||
1520 Token.is(MIToken::kw_ninf) ||
1521 Token.is(MIToken::kw_nsz) ||
1522 Token.is(MIToken::kw_arcp) ||
1523 Token.is(MIToken::kw_contract) ||
1524 Token.is(MIToken::kw_afn) ||
1525 Token.is(MIToken::kw_reassoc) ||
1526 Token.is(MIToken::kw_nuw) ||
1527 Token.is(MIToken::kw_nsw) ||
1528 Token.is(MIToken::kw_exact) ||
1529 Token.is(MIToken::kw_nofpexcept) ||
1530 Token.is(MIToken::kw_noconvergent) ||
1531 Token.is(MIToken::kw_unpredictable) ||
1532 Token.is(MIToken::kw_nneg) ||
1533 Token.is(MIToken::kw_disjoint) ||
1534 Token.is(MIToken::kw_nusw) ||
1535 Token.is(MIToken::kw_samesign) ||
1536 Token.is(MIToken::kw_inbounds)) {
1537 // clang-format on
1538 // Mine frame and fast math flags
1539 if (Token.is(MIToken::kw_frame_setup))
1541 if (Token.is(MIToken::kw_frame_destroy))
1543 if (Token.is(MIToken::kw_nnan))
1545 if (Token.is(MIToken::kw_ninf))
1547 if (Token.is(MIToken::kw_nsz))
1549 if (Token.is(MIToken::kw_arcp))
1551 if (Token.is(MIToken::kw_contract))
1553 if (Token.is(MIToken::kw_afn))
1555 if (Token.is(MIToken::kw_reassoc))
1557 if (Token.is(MIToken::kw_nuw))
1559 if (Token.is(MIToken::kw_nsw))
1561 if (Token.is(MIToken::kw_exact))
1563 if (Token.is(MIToken::kw_nofpexcept))
1565 if (Token.is(MIToken::kw_unpredictable))
1567 if (Token.is(MIToken::kw_noconvergent))
1569 if (Token.is(MIToken::kw_nneg))
1571 if (Token.is(MIToken::kw_disjoint))
1573 if (Token.is(MIToken::kw_nusw))
1575 if (Token.is(MIToken::kw_samesign))
1577 if (Token.is(MIToken::kw_inbounds))
1579
1580 lex();
1581 }
1582 if (Token.isNot(MIToken::Identifier))
1583 return error("expected a machine instruction");
1584 StringRef InstrName = Token.stringValue();
1585 if (PFS.Target.parseInstrName(InstrName, OpCode))
1586 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1587 lex();
1588 return false;
1589}
1590
1591bool MIParser::parseNamedRegister(Register &Reg) {
1592 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1593 StringRef Name = Token.stringValue();
1594 if (PFS.Target.getRegisterByName(Name, Reg))
1595 return error(Twine("unknown register name '") + Name + "'");
1596 return false;
1597}
1598
1599bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1600 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1601 StringRef Name = Token.stringValue();
1602 // TODO: Check that the VReg name is not the same as a physical register name.
1603 // If it is, then print a warning (when warnings are implemented).
1604 Info = &PFS.getVRegInfoNamed(Name);
1605 return false;
1606}
1607
1608bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1609 if (Token.is(MIToken::NamedVirtualRegister))
1610 return parseNamedVirtualRegister(Info);
1611 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1612 unsigned ID;
1613 if (getUnsigned(ID))
1614 return true;
1615 Info = &PFS.getVRegInfo(ID);
1616 return false;
1617}
1618
1619bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1620 switch (Token.kind()) {
1622 Reg = 0;
1623 return false;
1625 return parseNamedRegister(Reg);
1628 if (parseVirtualRegister(Info))
1629 return true;
1630 Reg = Info->VReg;
1631 return false;
1632 // TODO: Parse other register kinds.
1633 default:
1634 llvm_unreachable("The current token should be a register");
1635 }
1636}
1637
1638bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1639 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1640 return error("expected '_', register class, or register bank name");
1641 StringRef::iterator Loc = Token.location();
1642 StringRef Name = Token.stringValue();
1643
1644 // Was it a register class?
1645 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1646 if (RC) {
1647 lex();
1648
1649 switch (RegInfo.Kind) {
1650 case VRegInfo::UNKNOWN:
1651 case VRegInfo::NORMAL:
1652 RegInfo.Kind = VRegInfo::NORMAL;
1653 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1654 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1655 return error(Loc, Twine("conflicting register classes, previously: ") +
1656 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1657 }
1658 RegInfo.D.RC = RC;
1659 RegInfo.Explicit = true;
1660 return false;
1661
1662 case VRegInfo::GENERIC:
1663 case VRegInfo::REGBANK:
1664 return error(Loc, "register class specification on generic register");
1665 }
1666 llvm_unreachable("Unexpected register kind");
1667 }
1668
1669 // Should be a register bank or a generic register.
1670 const RegisterBank *RegBank = nullptr;
1671 if (Name != "_") {
1672 RegBank = PFS.Target.getRegBank(Name);
1673 if (!RegBank)
1674 return error(Loc, "expected '_', register class, or register bank name");
1675 }
1676
1677 lex();
1678
1679 switch (RegInfo.Kind) {
1680 case VRegInfo::UNKNOWN:
1681 case VRegInfo::GENERIC:
1682 case VRegInfo::REGBANK:
1683 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1684 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1685 return error(Loc, "conflicting generic register banks");
1686 RegInfo.D.RegBank = RegBank;
1687 RegInfo.Explicit = true;
1688 return false;
1689
1690 case VRegInfo::NORMAL:
1691 return error(Loc, "register bank specification on normal register");
1692 }
1693 llvm_unreachable("Unexpected register kind");
1694}
1695
1696bool MIParser::parseRegisterFlag(RegState &Flags) {
1697 const RegState OldFlags = Flags;
1698 switch (Token.kind()) {
1701 break;
1704 break;
1705 case MIToken::kw_def:
1707 break;
1708 case MIToken::kw_dead:
1710 break;
1711 case MIToken::kw_killed:
1713 break;
1714 case MIToken::kw_undef:
1716 break;
1719 break;
1722 break;
1725 break;
1728 break;
1729 default:
1730 llvm_unreachable("The current token should be a register flag");
1731 }
1732 if (OldFlags == Flags)
1733 // We know that the same flag is specified more than once when the flags
1734 // weren't modified.
1735 return error("duplicate '" + Token.stringValue() + "' register flag");
1736 lex();
1737 return false;
1738}
1739
1740bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1741 assert(Token.is(MIToken::dot));
1742 lex();
1743 if (Token.isNot(MIToken::Identifier))
1744 return error("expected a subregister index after '.'");
1745 auto Name = Token.stringValue();
1746 SubReg = PFS.Target.getSubRegIndex(Name);
1747 if (!SubReg)
1748 return error(Twine("use of unknown subregister index '") + Name + "'");
1749 lex();
1750 return false;
1751}
1752
1753bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1754 assert(Token.is(MIToken::kw_tied_def));
1755 lex();
1756 if (Token.isNot(MIToken::IntegerLiteral))
1757 return error("expected an integer literal after 'tied-def'");
1758 if (getUnsigned(TiedDefIdx))
1759 return true;
1760 lex();
1761 return expectAndConsume(MIToken::rparen);
1762}
1763
1764bool MIParser::assignRegisterTies(MachineInstr &MI,
1766 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1767 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1768 if (!Operands[I].TiedDefIdx)
1769 continue;
1770 // The parser ensures that this operand is a register use, so we just have
1771 // to check the tied-def operand.
1772 unsigned DefIdx = *Operands[I].TiedDefIdx;
1773 if (DefIdx >= E)
1774 return error(Operands[I].Begin,
1775 Twine("use of invalid tied-def operand index '" +
1776 Twine(DefIdx) + "'; instruction has only ") +
1777 Twine(E) + " operands");
1778 const auto &DefOperand = Operands[DefIdx].Operand;
1779 if (!DefOperand.isReg() || !DefOperand.isDef())
1780 // FIXME: add note with the def operand.
1781 return error(Operands[I].Begin,
1782 Twine("use of invalid tied-def operand index '") +
1783 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1784 " isn't a defined register");
1785 // Check that the tied-def operand wasn't tied elsewhere.
1786 for (const auto &TiedPair : TiedRegisterPairs) {
1787 if (TiedPair.first == DefIdx)
1788 return error(Operands[I].Begin,
1789 Twine("the tied-def operand #") + Twine(DefIdx) +
1790 " is already tied with another register operand");
1791 }
1792 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1793 }
1794 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1795 // indices must be less than tied max.
1796 for (const auto &TiedPair : TiedRegisterPairs)
1797 MI.tieOperands(TiedPair.first, TiedPair.second);
1798 return false;
1799}
1800
1801bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1802 std::optional<unsigned> &TiedDefIdx,
1803 bool IsDef) {
1804 RegState Flags = getDefRegState(IsDef);
1805 while (Token.isRegisterFlag()) {
1806 if (parseRegisterFlag(Flags))
1807 return true;
1808 }
1809 // Update IsDef as we may have read a def flag.
1810 IsDef = hasRegState(Flags, RegState::Define);
1811 if (!Token.isRegister())
1812 return error("expected a register after register flags");
1813 Register Reg;
1814 VRegInfo *RegInfo;
1815 if (parseRegister(Reg, RegInfo))
1816 return true;
1817 lex();
1818 unsigned SubReg = 0;
1819 if (Token.is(MIToken::dot)) {
1820 if (parseSubRegisterIndex(SubReg))
1821 return true;
1822 if (!Reg.isVirtual())
1823 return error("subregister index expects a virtual register");
1824 }
1825 if (Token.is(MIToken::colon)) {
1826 if (!Reg.isVirtual())
1827 return error("register class specification expects a virtual register");
1828 lex();
1829 if (parseRegisterClassOrBank(*RegInfo))
1830 return true;
1831 }
1832
1833 if (consumeIfPresent(MIToken::lparen)) {
1834 // For a def, we only expect a type. For use we expect either a type or a
1835 // tied-def. Additionally, for physical registers, we don't expect a type.
1836 if (Token.is(MIToken::kw_tied_def)) {
1837 if (IsDef)
1838 return error("tied-def not supported for defs");
1839 unsigned Idx;
1840 if (parseRegisterTiedDefIndex(Idx))
1841 return true;
1842 TiedDefIdx = Idx;
1843 } else {
1844 if (!Reg.isVirtual())
1845 return error("unexpected type on physical register");
1846
1847 LLT Ty;
1848 // If type parsing fails, forwad the parse error for defs.
1849 if (parseLowLevelType(Token.location(), Ty))
1850 return IsDef ? true
1851 : error("expected tied-def or low-level type after '('");
1852
1853 if (expectAndConsume(MIToken::rparen))
1854 return true;
1855
1856 MachineRegisterInfo &MRI = MF.getRegInfo();
1857 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1858 return error("inconsistent type for generic virtual register");
1859
1860 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1861 MRI.setType(Reg, Ty);
1863 }
1864 } else if (IsDef && Reg.isVirtual()) {
1865 // Generic virtual registers defs must have a type.
1866 if (RegInfo->Kind == VRegInfo::GENERIC ||
1867 RegInfo->Kind == VRegInfo::REGBANK)
1868 return error("generic virtual registers must have a type");
1869 }
1870
1871 if (IsDef) {
1872 if (hasRegState(Flags, RegState::Kill))
1873 return error("cannot have a killed def operand");
1874 } else {
1875 if (hasRegState(Flags, RegState::Dead))
1876 return error("cannot have a dead use operand");
1877 }
1878
1880 Reg, IsDef, hasRegState(Flags, RegState::Implicit),
1883 hasRegState(Flags, RegState::EarlyClobber), SubReg,
1887
1888 return false;
1889}
1890
1891bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1893 const APSInt &Int = Token.integerValue();
1894 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1895 Dest = MachineOperand::CreateImm(*SImm);
1896 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1897 Dest = MachineOperand::CreateImm(*UImm);
1898 else
1899 return error("integer literal is too large to be an immediate operand");
1900 lex();
1901 return false;
1902}
1903
1904bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1905 const unsigned OpIdx,
1906 MachineOperand &Dest,
1907 const MIRFormatter &MF) {
1908 assert(Token.is(MIToken::dot));
1909 auto Loc = Token.location(); // record start position
1910 size_t Len = 1; // for "."
1911 lex();
1912
1913 // Handle the case that mnemonic starts with number.
1914 if (Token.is(MIToken::IntegerLiteral)) {
1915 Len += Token.range().size();
1916 lex();
1917 }
1918
1919 StringRef Src;
1920 if (Token.is(MIToken::comma))
1921 Src = StringRef(Loc, Len);
1922 else {
1923 assert(Token.is(MIToken::Identifier));
1924 Src = StringRef(Loc, Len + Token.stringValue().size());
1925 }
1926 int64_t Val;
1927 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1928 [this](StringRef::iterator Loc, const Twine &Msg)
1929 -> bool { return error(Loc, Msg); }))
1930 return true;
1931
1932 Dest = MachineOperand::CreateImm(Val);
1933 if (!Token.is(MIToken::comma))
1934 lex();
1935 return false;
1936}
1937
1939 PerFunctionMIParsingState &PFS, const Constant *&C,
1940 ErrorCallbackType ErrCB) {
1941 auto Source = StringValue.str(); // The source has to be null terminated.
1942 SMDiagnostic Err;
1943 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1944 &PFS.IRSlots);
1945 if (!C)
1946 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1947 return false;
1948}
1949
1950bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1951 const Constant *&C) {
1952 return ::parseIRConstant(
1953 Loc, StringValue, PFS, C,
1954 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1955 return error(Loc, Msg);
1956 });
1957}
1958
1959bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1960 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1961 return true;
1962 lex();
1963 return false;
1964}
1965
1966// See LLT implementation for bit size limits.
1968 return Size != 0 && isUInt<16>(Size);
1969}
1970
1972 return NumElts != 0 && isUInt<16>(NumElts);
1973}
1974
1975static bool verifyAddrSpace(uint64_t AddrSpace) {
1976 return isUInt<24>(AddrSpace);
1977}
1978
1979bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1980 if (Token.range().front() == 's' || Token.range().front() == 'p') {
1981 StringRef SizeStr = Token.range().drop_front();
1982 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1983 return error("expected integers after 's'/'p' type character");
1984 }
1985
1986 if (Token.range().front() == 's') {
1987 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1988 if (ScalarSize) {
1989 if (!verifyScalarSize(ScalarSize))
1990 return error("invalid size for scalar type");
1991 Ty = LLT::scalar(ScalarSize);
1992 } else {
1993 Ty = LLT::token();
1994 }
1995 lex();
1996 return false;
1997 } else if (Token.range().front() == 'p') {
1998 const DataLayout &DL = MF.getDataLayout();
1999 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
2000 if (!verifyAddrSpace(AS))
2001 return error("invalid address space number");
2002
2003 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2004 lex();
2005 return false;
2006 }
2007
2008 // Now we're looking for a vector.
2009 if (Token.isNot(MIToken::less))
2010 return error(Loc, "expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, "
2011 "or <vscale x M x pA> for GlobalISel type");
2012 lex();
2013
2014 bool HasVScale =
2015 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2016 if (HasVScale) {
2017 lex();
2018 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2019 return error("expected <vscale x M x sN> or <vscale x M x pA>");
2020 lex();
2021 }
2022
2023 auto GetError = [this, &HasVScale, Loc]() {
2024 if (HasVScale)
2025 return error(
2026 Loc, "expected <vscale x M x sN> or <vscale M x pA> for vector type");
2027 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
2028 };
2029
2030 if (Token.isNot(MIToken::IntegerLiteral))
2031 return GetError();
2032 uint64_t NumElements = Token.integerValue().getZExtValue();
2033 if (!verifyVectorElementCount(NumElements))
2034 return error("invalid number of vector elements");
2035
2036 lex();
2037
2038 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2039 return GetError();
2040 lex();
2041
2042 if (Token.range().front() != 's' && Token.range().front() != 'p')
2043 return GetError();
2044
2045 StringRef SizeStr = Token.range().drop_front();
2046 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
2047 return error("expected integers after 's'/'p' type character");
2048
2049 if (Token.range().front() == 's') {
2050 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
2051 if (!verifyScalarSize(ScalarSize))
2052 return error("invalid size for scalar element in vector");
2053 Ty = LLT::scalar(ScalarSize);
2054 } else if (Token.range().front() == 'p') {
2055 const DataLayout &DL = MF.getDataLayout();
2056 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
2057 if (!verifyAddrSpace(AS))
2058 return error("invalid address space number");
2059
2060 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2061 } else {
2062 return GetError();
2063 }
2064 lex();
2065
2066 if (Token.isNot(MIToken::greater))
2067 return GetError();
2068
2069 lex();
2070
2071 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2072 return false;
2073}
2074
2075bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2076 assert(Token.is(MIToken::Identifier));
2077 StringRef TypeStr = Token.range();
2078 if (TypeStr.front() != 'i' && TypeStr.front() != 's' &&
2079 TypeStr.front() != 'p')
2080 return error(
2081 "a typed immediate operand should start with one of 'i', 's', or 'p'");
2082 StringRef SizeStr = Token.range().drop_front();
2083 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
2084 return error("expected integers after 'i'/'s'/'p' type character");
2085
2086 auto Loc = Token.location();
2087 lex();
2088 if (Token.isNot(MIToken::IntegerLiteral)) {
2089 if (Token.isNot(MIToken::Identifier) ||
2090 !(Token.range() == "true" || Token.range() == "false"))
2091 return error("expected an integer literal");
2092 }
2093 const Constant *C = nullptr;
2094 if (parseIRConstant(Loc, C))
2095 return true;
2097 return false;
2098}
2099
2100bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2101 auto Loc = Token.location();
2102 lex();
2103 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2104 Token.isNot(MIToken::HexLiteral))
2105 return error("expected a floating point literal");
2106 const Constant *C = nullptr;
2107 if (parseIRConstant(Loc, C))
2108 return true;
2110 return false;
2111}
2112
2113static bool getHexUint(const MIToken &Token, APInt &Result) {
2115 StringRef S = Token.range();
2116 assert(S[0] == '0' && tolower(S[1]) == 'x');
2117 // This could be a floating point literal with a special prefix.
2118 if (!isxdigit(S[2]))
2119 return true;
2120 StringRef V = S.substr(2);
2121 APInt A(V.size()*4, V, 16);
2122
2123 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2124 // sure it isn't the case before constructing result.
2125 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2126 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2127 return false;
2128}
2129
2130static bool getUnsigned(const MIToken &Token, unsigned &Result,
2131 ErrorCallbackType ErrCB) {
2132 if (Token.hasIntegerValue()) {
2133 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2134 const APSInt &SInt = Token.integerValue();
2135 if (SInt.isNegative())
2136 return ErrCB(Token.location(), "expected unsigned integer");
2137 uint64_t Val64 = SInt.getLimitedValue(Limit);
2138 if (Val64 == Limit)
2139 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2140 Result = Val64;
2141 return false;
2142 }
2143 if (Token.is(MIToken::HexLiteral)) {
2144 APInt A;
2145 if (getHexUint(Token, A))
2146 return true;
2147 if (A.getBitWidth() > 32)
2148 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2149 Result = A.getZExtValue();
2150 return false;
2151 }
2152 return true;
2153}
2154
2155bool MIParser::getUnsigned(unsigned &Result) {
2156 return ::getUnsigned(
2157 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2158 return error(Loc, Msg);
2159 });
2160}
2161
2162bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2165 unsigned Number;
2166 if (getUnsigned(Number))
2167 return true;
2168 auto MBBInfo = PFS.MBBSlots.find(Number);
2169 if (MBBInfo == PFS.MBBSlots.end())
2170 return error(Twine("use of undefined machine basic block #") +
2171 Twine(Number));
2172 MBB = MBBInfo->second;
2173 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2174 // we drop the <irname> from the bb.<id>.<irname> format.
2175 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2176 return error(Twine("the name of machine basic block #") + Twine(Number) +
2177 " isn't '" + Token.stringValue() + "'");
2178 return false;
2179}
2180
2181bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2184 return true;
2186 lex();
2187 return false;
2188}
2189
2190bool MIParser::parseStackFrameIndex(int &FI) {
2191 assert(Token.is(MIToken::StackObject));
2192 unsigned ID;
2193 if (getUnsigned(ID))
2194 return true;
2195 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2196 if (ObjectInfo == PFS.StackObjectSlots.end())
2197 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2198 "'");
2200 if (const auto *Alloca =
2201 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2202 Name = Alloca->getName();
2203 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2204 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2205 "' isn't '" + Token.stringValue() + "'");
2206 lex();
2207 FI = ObjectInfo->second;
2208 return false;
2209}
2210
2211bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2212 int FI;
2213 if (parseStackFrameIndex(FI))
2214 return true;
2215 Dest = MachineOperand::CreateFI(FI);
2216 return false;
2217}
2218
2219bool MIParser::parseFixedStackFrameIndex(int &FI) {
2221 unsigned ID;
2222 if (getUnsigned(ID))
2223 return true;
2224 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2225 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2226 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2227 Twine(ID) + "'");
2228 lex();
2229 FI = ObjectInfo->second;
2230 return false;
2231}
2232
2233bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2234 int FI;
2235 if (parseFixedStackFrameIndex(FI))
2236 return true;
2237 Dest = MachineOperand::CreateFI(FI);
2238 return false;
2239}
2240
2241static bool parseGlobalValue(const MIToken &Token,
2243 ErrorCallbackType ErrCB) {
2244 switch (Token.kind()) {
2246 const Module *M = PFS.MF.getFunction().getParent();
2247 GV = M->getNamedValue(Token.stringValue());
2248 if (!GV)
2249 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2250 Token.range() + "'");
2251 break;
2252 }
2253 case MIToken::GlobalValue: {
2254 unsigned GVIdx;
2255 if (getUnsigned(Token, GVIdx, ErrCB))
2256 return true;
2257 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2258 if (!GV)
2259 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2260 Twine(GVIdx) + "'");
2261 break;
2262 }
2263 default:
2264 llvm_unreachable("The current token should be a global value");
2265 }
2266 return false;
2267}
2268
2269bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2270 return ::parseGlobalValue(
2271 Token, PFS, GV,
2272 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2273 return error(Loc, Msg);
2274 });
2275}
2276
2277bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2278 GlobalValue *GV = nullptr;
2279 if (parseGlobalValue(GV))
2280 return true;
2281 lex();
2282 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2283 if (parseOperandsOffset(Dest))
2284 return true;
2285 return false;
2286}
2287
2288bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2290 unsigned ID;
2291 if (getUnsigned(ID))
2292 return true;
2293 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2294 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2295 return error("use of undefined constant '%const." + Twine(ID) + "'");
2296 lex();
2297 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2298 if (parseOperandsOffset(Dest))
2299 return true;
2300 return false;
2301}
2302
2303bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2305 unsigned ID;
2306 if (getUnsigned(ID))
2307 return true;
2308 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2309 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2310 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2311 lex();
2312 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2313 return false;
2314}
2315
2316bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2318 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2319 lex();
2320 Dest = MachineOperand::CreateES(Symbol);
2321 if (parseOperandsOffset(Dest))
2322 return true;
2323 return false;
2324}
2325
2326bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2327 assert(Token.is(MIToken::MCSymbol));
2328 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2329 lex();
2330 Dest = MachineOperand::CreateMCSymbol(Symbol);
2331 if (parseOperandsOffset(Dest))
2332 return true;
2333 return false;
2334}
2335
2336bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2338 StringRef Name = Token.stringValue();
2339 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2340 if (SubRegIndex == 0)
2341 return error(Twine("unknown subregister index '") + Name + "'");
2342 lex();
2343 Dest = MachineOperand::CreateImm(SubRegIndex);
2344 return false;
2345}
2346
2347bool MIParser::parseMDNode(MDNode *&Node) {
2348 assert(Token.is(MIToken::exclaim));
2349
2350 auto Loc = Token.location();
2351 lex();
2352 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2353 return error("expected metadata id after '!'");
2354 unsigned ID;
2355 if (getUnsigned(ID))
2356 return true;
2357 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2358 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2359 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2360 if (NodeInfo == PFS.MachineMetadataNodes.end())
2361 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2362 }
2363 lex();
2364 Node = NodeInfo->second.get();
2365 return false;
2366}
2367
2368bool MIParser::parseDIExpression(MDNode *&Expr) {
2369 unsigned Read;
2371 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2372 &PFS.IRSlots);
2373 CurrentSource = CurrentSource.substr(Read);
2374 lex();
2375 if (!Expr)
2376 return error(Error.getMessage());
2377 return false;
2378}
2379
2380bool MIParser::parseDILocation(MDNode *&Loc) {
2381 assert(Token.is(MIToken::md_dilocation));
2382 lex();
2383
2384 bool HaveLine = false;
2385 unsigned Line = 0;
2386 unsigned Column = 0;
2387 MDNode *Scope = nullptr;
2388 MDNode *InlinedAt = nullptr;
2389 bool ImplicitCode = false;
2390 uint64_t AtomGroup = 0;
2391 uint64_t AtomRank = 0;
2392
2393 if (expectAndConsume(MIToken::lparen))
2394 return true;
2395
2396 if (Token.isNot(MIToken::rparen)) {
2397 do {
2398 if (Token.is(MIToken::Identifier)) {
2399 if (Token.stringValue() == "line") {
2400 lex();
2401 if (expectAndConsume(MIToken::colon))
2402 return true;
2403 if (Token.isNot(MIToken::IntegerLiteral) ||
2404 Token.integerValue().isSigned())
2405 return error("expected unsigned integer");
2406 Line = Token.integerValue().getZExtValue();
2407 HaveLine = true;
2408 lex();
2409 continue;
2410 }
2411 if (Token.stringValue() == "column") {
2412 lex();
2413 if (expectAndConsume(MIToken::colon))
2414 return true;
2415 if (Token.isNot(MIToken::IntegerLiteral) ||
2416 Token.integerValue().isSigned())
2417 return error("expected unsigned integer");
2418 Column = Token.integerValue().getZExtValue();
2419 lex();
2420 continue;
2421 }
2422 if (Token.stringValue() == "scope") {
2423 lex();
2424 if (expectAndConsume(MIToken::colon))
2425 return true;
2426 if (parseMDNode(Scope))
2427 return error("expected metadata node");
2428 if (!isa<DIScope>(Scope))
2429 return error("expected DIScope node");
2430 continue;
2431 }
2432 if (Token.stringValue() == "inlinedAt") {
2433 lex();
2434 if (expectAndConsume(MIToken::colon))
2435 return true;
2436 if (Token.is(MIToken::exclaim)) {
2437 if (parseMDNode(InlinedAt))
2438 return true;
2439 } else if (Token.is(MIToken::md_dilocation)) {
2440 if (parseDILocation(InlinedAt))
2441 return true;
2442 } else {
2443 return error("expected metadata node");
2444 }
2445 if (!isa<DILocation>(InlinedAt))
2446 return error("expected DILocation node");
2447 continue;
2448 }
2449 if (Token.stringValue() == "isImplicitCode") {
2450 lex();
2451 if (expectAndConsume(MIToken::colon))
2452 return true;
2453 if (!Token.is(MIToken::Identifier))
2454 return error("expected true/false");
2455 // As far as I can see, we don't have any existing need for parsing
2456 // true/false in MIR yet. Do it ad-hoc until there's something else
2457 // that needs it.
2458 if (Token.stringValue() == "true")
2459 ImplicitCode = true;
2460 else if (Token.stringValue() == "false")
2461 ImplicitCode = false;
2462 else
2463 return error("expected true/false");
2464 lex();
2465 continue;
2466 }
2467 if (Token.stringValue() == "atomGroup") {
2468 lex();
2469 if (expectAndConsume(MIToken::colon))
2470 return true;
2471 if (Token.isNot(MIToken::IntegerLiteral) ||
2472 Token.integerValue().isSigned())
2473 return error("expected unsigned integer");
2474 AtomGroup = Token.integerValue().getZExtValue();
2475 lex();
2476 continue;
2477 }
2478 if (Token.stringValue() == "atomRank") {
2479 lex();
2480 if (expectAndConsume(MIToken::colon))
2481 return true;
2482 if (Token.isNot(MIToken::IntegerLiteral) ||
2483 Token.integerValue().isSigned())
2484 return error("expected unsigned integer");
2485 AtomRank = Token.integerValue().getZExtValue();
2486 lex();
2487 continue;
2488 }
2489 }
2490 return error(Twine("invalid DILocation argument '") +
2491 Token.stringValue() + "'");
2492 } while (consumeIfPresent(MIToken::comma));
2493 }
2494
2495 if (expectAndConsume(MIToken::rparen))
2496 return true;
2497
2498 if (!HaveLine)
2499 return error("DILocation requires line number");
2500 if (!Scope)
2501 return error("DILocation requires a scope");
2502
2503 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2504 InlinedAt, ImplicitCode, AtomGroup, AtomRank);
2505 return false;
2506}
2507
2508bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2509 MDNode *Node = nullptr;
2510 if (Token.is(MIToken::exclaim)) {
2511 if (parseMDNode(Node))
2512 return true;
2513 } else if (Token.is(MIToken::md_diexpr)) {
2514 if (parseDIExpression(Node))
2515 return true;
2516 }
2517 Dest = MachineOperand::CreateMetadata(Node);
2518 return false;
2519}
2520
2521bool MIParser::parseCFIOffset(int &Offset) {
2522 if (Token.isNot(MIToken::IntegerLiteral))
2523 return error("expected a cfi offset");
2524 if (Token.integerValue().getSignificantBits() > 32)
2525 return error("expected a 32 bit integer (the cfi offset is too large)");
2526 Offset = (int)Token.integerValue().getExtValue();
2527 lex();
2528 return false;
2529}
2530
2531bool MIParser::parseCFIRegister(unsigned &Reg) {
2532 if (Token.isNot(MIToken::NamedRegister))
2533 return error("expected a cfi register");
2534 Register LLVMReg;
2535 if (parseNamedRegister(LLVMReg))
2536 return true;
2537 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2538 assert(TRI && "Expected target register info");
2539 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2540 if (DwarfReg < 0)
2541 return error("invalid DWARF register");
2542 Reg = (unsigned)DwarfReg;
2543 lex();
2544 return false;
2545}
2546
2547bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2548 if (Token.isNot(MIToken::IntegerLiteral))
2549 return error("expected a cfi address space literal");
2550 if (Token.integerValue().isSigned())
2551 return error("expected an unsigned integer (cfi address space)");
2552 AddressSpace = Token.integerValue().getZExtValue();
2553 lex();
2554 return false;
2555}
2556
2557bool MIParser::parseCFIEscapeValues(std::string &Values) {
2558 do {
2559 if (Token.isNot(MIToken::HexLiteral))
2560 return error("expected a hexadecimal literal");
2561 unsigned Value;
2562 if (getUnsigned(Value))
2563 return true;
2564 if (Value > UINT8_MAX)
2565 return error("expected a 8-bit integer (too large)");
2566 Values.push_back(static_cast<uint8_t>(Value));
2567 lex();
2568 } while (consumeIfPresent(MIToken::comma));
2569 return false;
2570}
2571
2572bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2573 auto Kind = Token.kind();
2574 lex();
2575 int Offset;
2576 unsigned Reg;
2577 unsigned AddressSpace;
2578 unsigned CFIIndex;
2579 switch (Kind) {
2581 if (parseCFIRegister(Reg))
2582 return true;
2583 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2584 break;
2586 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2587 parseCFIOffset(Offset))
2588 return true;
2589 CFIIndex =
2590 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2591 break;
2593 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2594 parseCFIOffset(Offset))
2595 return true;
2596 CFIIndex = MF.addFrameInst(
2598 break;
2600 if (parseCFIRegister(Reg))
2601 return true;
2602 CFIIndex =
2603 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2604 break;
2606 if (parseCFIOffset(Offset))
2607 return true;
2608 CFIIndex =
2609 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2610 break;
2612 if (parseCFIOffset(Offset))
2613 return true;
2614 CFIIndex = MF.addFrameInst(
2616 break;
2618 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2619 parseCFIOffset(Offset))
2620 return true;
2621 CFIIndex =
2622 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2623 break;
2625 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2626 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2627 parseCFIAddressSpace(AddressSpace))
2628 return true;
2629 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2630 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2631 break;
2633 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2634 break;
2636 if (parseCFIRegister(Reg))
2637 return true;
2638 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2639 break;
2641 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2642 break;
2644 if (parseCFIRegister(Reg))
2645 return true;
2646 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2647 break;
2649 unsigned Reg2;
2650 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2651 parseCFIRegister(Reg2))
2652 return true;
2653
2654 CFIIndex =
2655 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2656 break;
2657 }
2659 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2660 break;
2662 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2663 break;
2665 CFIIndex =
2666 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2667 break;
2669 std::string Values;
2670 if (parseCFIEscapeValues(Values))
2671 return true;
2672 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2673 break;
2674 }
2675 default:
2676 // TODO: Parse the other CFI operands.
2677 llvm_unreachable("The current token should be a cfi operand");
2678 }
2679 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2680 return false;
2681}
2682
2683bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2684 switch (Token.kind()) {
2685 case MIToken::NamedIRBlock: {
2687 F.getValueSymbolTable()->lookup(Token.stringValue()));
2688 if (!BB)
2689 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2690 break;
2691 }
2692 case MIToken::IRBlock: {
2693 unsigned SlotNumber = 0;
2694 if (getUnsigned(SlotNumber))
2695 return true;
2696 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2697 if (!BB)
2698 return error(Twine("use of undefined IR block '%ir-block.") +
2699 Twine(SlotNumber) + "'");
2700 break;
2701 }
2702 default:
2703 llvm_unreachable("The current token should be an IR block reference");
2704 }
2705 return false;
2706}
2707
2708bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2710 lex();
2711 if (expectAndConsume(MIToken::lparen))
2712 return true;
2713 if (Token.isNot(MIToken::GlobalValue) &&
2714 Token.isNot(MIToken::NamedGlobalValue))
2715 return error("expected a global value");
2716 GlobalValue *GV = nullptr;
2717 if (parseGlobalValue(GV))
2718 return true;
2719 auto *F = dyn_cast<Function>(GV);
2720 if (!F)
2721 return error("expected an IR function reference");
2722 lex();
2723 if (expectAndConsume(MIToken::comma))
2724 return true;
2725 BasicBlock *BB = nullptr;
2726 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2727 return error("expected an IR block reference");
2728 if (parseIRBlock(BB, *F))
2729 return true;
2730 lex();
2731 if (expectAndConsume(MIToken::rparen))
2732 return true;
2733 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2734 if (parseOperandsOffset(Dest))
2735 return true;
2736 return false;
2737}
2738
2739bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2740 assert(Token.is(MIToken::kw_intrinsic));
2741 lex();
2742 if (expectAndConsume(MIToken::lparen))
2743 return error("expected syntax intrinsic(@llvm.whatever)");
2744
2745 if (Token.isNot(MIToken::NamedGlobalValue))
2746 return error("expected syntax intrinsic(@llvm.whatever)");
2747
2748 std::string Name = std::string(Token.stringValue());
2749 lex();
2750
2751 if (expectAndConsume(MIToken::rparen))
2752 return error("expected ')' to terminate intrinsic name");
2753
2754 // Find out what intrinsic we're dealing with.
2757 return error("unknown intrinsic name");
2759
2760 return false;
2761}
2762
2763bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2764 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2765 bool IsFloat = Token.is(MIToken::kw_floatpred);
2766 lex();
2767
2768 if (expectAndConsume(MIToken::lparen))
2769 return error("expected syntax intpred(whatever) or floatpred(whatever");
2770
2771 if (Token.isNot(MIToken::Identifier))
2772 return error("whatever");
2773
2774 CmpInst::Predicate Pred;
2775 if (IsFloat) {
2776 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2777 .Case("false", CmpInst::FCMP_FALSE)
2778 .Case("oeq", CmpInst::FCMP_OEQ)
2779 .Case("ogt", CmpInst::FCMP_OGT)
2780 .Case("oge", CmpInst::FCMP_OGE)
2781 .Case("olt", CmpInst::FCMP_OLT)
2782 .Case("ole", CmpInst::FCMP_OLE)
2783 .Case("one", CmpInst::FCMP_ONE)
2784 .Case("ord", CmpInst::FCMP_ORD)
2785 .Case("uno", CmpInst::FCMP_UNO)
2786 .Case("ueq", CmpInst::FCMP_UEQ)
2787 .Case("ugt", CmpInst::FCMP_UGT)
2788 .Case("uge", CmpInst::FCMP_UGE)
2789 .Case("ult", CmpInst::FCMP_ULT)
2790 .Case("ule", CmpInst::FCMP_ULE)
2791 .Case("une", CmpInst::FCMP_UNE)
2792 .Case("true", CmpInst::FCMP_TRUE)
2794 if (!CmpInst::isFPPredicate(Pred))
2795 return error("invalid floating-point predicate");
2796 } else {
2797 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2798 .Case("eq", CmpInst::ICMP_EQ)
2799 .Case("ne", CmpInst::ICMP_NE)
2800 .Case("sgt", CmpInst::ICMP_SGT)
2801 .Case("sge", CmpInst::ICMP_SGE)
2802 .Case("slt", CmpInst::ICMP_SLT)
2803 .Case("sle", CmpInst::ICMP_SLE)
2804 .Case("ugt", CmpInst::ICMP_UGT)
2805 .Case("uge", CmpInst::ICMP_UGE)
2806 .Case("ult", CmpInst::ICMP_ULT)
2807 .Case("ule", CmpInst::ICMP_ULE)
2809 if (!CmpInst::isIntPredicate(Pred))
2810 return error("invalid integer predicate");
2811 }
2812
2813 lex();
2815 if (expectAndConsume(MIToken::rparen))
2816 return error("predicate should be terminated by ')'.");
2817
2818 return false;
2819}
2820
2821bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
2823
2824 lex();
2825 if (expectAndConsume(MIToken::lparen))
2826 return error("expected syntax shufflemask(<integer or undef>, ...)");
2827
2828 SmallVector<int, 32> ShufMask;
2829 do {
2830 if (Token.is(MIToken::kw_undef)) {
2831 ShufMask.push_back(-1);
2832 } else if (Token.is(MIToken::IntegerLiteral)) {
2833 const APSInt &Int = Token.integerValue();
2834 ShufMask.push_back(Int.getExtValue());
2835 } else {
2836 return error("expected integer constant");
2837 }
2838
2839 lex();
2840 } while (consumeIfPresent(MIToken::comma));
2841
2842 if (expectAndConsume(MIToken::rparen))
2843 return error("shufflemask should be terminated by ')'.");
2844
2845 if (ShufMask.size() < 2)
2846 return error("shufflemask should have > 1 element");
2847
2848 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
2849 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
2850 return false;
2851}
2852
2853bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
2855
2856 lex();
2857 if (expectAndConsume(MIToken::lparen))
2858 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2859
2860 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2861 return error("expected unsigned integer for instruction index");
2862 uint64_t InstrIdx = Token.integerValue().getZExtValue();
2863 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
2864 "Instruction reference's instruction index is too large");
2865 lex();
2866
2867 if (expectAndConsume(MIToken::comma))
2868 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2869
2870 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2871 return error("expected unsigned integer for operand index");
2872 uint64_t OpIdx = Token.integerValue().getZExtValue();
2873 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
2874 "Instruction reference's operand index is too large");
2875 lex();
2876
2877 if (expectAndConsume(MIToken::rparen))
2878 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2879
2880 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
2881 return false;
2882}
2883
2884bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2886 lex();
2887 if (expectAndConsume(MIToken::lparen))
2888 return true;
2889 if (Token.isNot(MIToken::Identifier))
2890 return error("expected the name of the target index");
2891 int Index = 0;
2892 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2893 return error("use of undefined target index '" + Token.stringValue() + "'");
2894 lex();
2895 if (expectAndConsume(MIToken::rparen))
2896 return true;
2897 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2898 if (parseOperandsOffset(Dest))
2899 return true;
2900 return false;
2901}
2902
2903bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2904 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2905 lex();
2906 if (expectAndConsume(MIToken::lparen))
2907 return true;
2908
2909 uint32_t *Mask = MF.allocateRegMask();
2910 do {
2911 if (Token.isNot(MIToken::rparen)) {
2912 if (Token.isNot(MIToken::NamedRegister))
2913 return error("expected a named register");
2914 Register Reg;
2915 if (parseNamedRegister(Reg))
2916 return true;
2917 lex();
2918 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
2919 }
2920
2921 // TODO: Report an error if the same register is used more than once.
2922 } while (consumeIfPresent(MIToken::comma));
2923
2924 if (expectAndConsume(MIToken::rparen))
2925 return true;
2926 Dest = MachineOperand::CreateRegMask(Mask);
2927 return false;
2928}
2929
2930bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
2931 assert(Token.is(MIToken::kw_lanemask));
2932
2933 lex();
2934 if (expectAndConsume(MIToken::lparen))
2935 return true;
2936
2937 // Parse lanemask.
2938 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
2939 return error("expected a valid lane mask value");
2940 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
2941 "Use correct get-function for lane mask.");
2943 if (getUint64(V))
2944 return true;
2945 LaneBitmask LaneMask(V);
2946 lex();
2947
2948 if (expectAndConsume(MIToken::rparen))
2949 return true;
2950
2951 Dest = MachineOperand::CreateLaneMask(LaneMask);
2952 return false;
2953}
2954
2955bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2956 assert(Token.is(MIToken::kw_liveout));
2957 uint32_t *Mask = MF.allocateRegMask();
2958 lex();
2959 if (expectAndConsume(MIToken::lparen))
2960 return true;
2961 while (true) {
2962 if (Token.isNot(MIToken::NamedRegister))
2963 return error("expected a named register");
2964 Register Reg;
2965 if (parseNamedRegister(Reg))
2966 return true;
2967 lex();
2968 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
2969 // TODO: Report an error if the same register is used more than once.
2970 if (Token.isNot(MIToken::comma))
2971 break;
2972 lex();
2973 }
2974 if (expectAndConsume(MIToken::rparen))
2975 return true;
2977 return false;
2978}
2979
2980bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
2981 MachineOperand &Dest,
2982 std::optional<unsigned> &TiedDefIdx) {
2983 switch (Token.kind()) {
2986 case MIToken::kw_def:
2987 case MIToken::kw_dead:
2988 case MIToken::kw_killed:
2989 case MIToken::kw_undef:
2998 return parseRegisterOperand(Dest, TiedDefIdx);
3000 return parseImmediateOperand(Dest);
3001 case MIToken::kw_half:
3002 case MIToken::kw_bfloat:
3003 case MIToken::kw_float:
3004 case MIToken::kw_double:
3006 case MIToken::kw_fp128:
3008 return parseFPImmediateOperand(Dest);
3010 return parseMBBOperand(Dest);
3012 return parseStackObjectOperand(Dest);
3014 return parseFixedStackObjectOperand(Dest);
3017 return parseGlobalAddressOperand(Dest);
3019 return parseConstantPoolIndexOperand(Dest);
3021 return parseJumpTableIndexOperand(Dest);
3023 return parseExternalSymbolOperand(Dest);
3024 case MIToken::MCSymbol:
3025 return parseMCSymbolOperand(Dest);
3027 return parseSubRegisterIndexOperand(Dest);
3028 case MIToken::md_diexpr:
3029 case MIToken::exclaim:
3030 return parseMetadataOperand(Dest);
3048 return parseCFIOperand(Dest);
3050 return parseBlockAddressOperand(Dest);
3052 return parseIntrinsicOperand(Dest);
3054 return parseTargetIndexOperand(Dest);
3056 return parseLaneMaskOperand(Dest);
3058 return parseLiveoutRegisterMaskOperand(Dest);
3061 return parsePredicateOperand(Dest);
3063 return parseShuffleMaskOperand(Dest);
3065 return parseDbgInstrRefOperand(Dest);
3066 case MIToken::Error:
3067 return true;
3069 if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
3070 Dest = MachineOperand::CreateRegMask(RegMask);
3071 lex();
3072 break;
3073 } else if (Token.stringValue() == "CustomRegMask") {
3074 return parseCustomRegisterMaskOperand(Dest);
3075 } else {
3076 return parseTypedImmediateOperand(Dest);
3077 }
3078 case MIToken::dot: {
3079 const auto *TII = MF.getSubtarget().getInstrInfo();
3080 if (const auto *Formatter = TII->getMIRFormatter()) {
3081 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3082 }
3083 [[fallthrough]];
3084 }
3085 default:
3086 // FIXME: Parse the MCSymbol machine operand.
3087 return error("expected a machine operand");
3088 }
3089 return false;
3090}
3091
3092bool MIParser::parseMachineOperandAndTargetFlags(
3093 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3094 std::optional<unsigned> &TiedDefIdx) {
3095 unsigned TF = 0;
3096 bool HasTargetFlags = false;
3097 if (Token.is(MIToken::kw_target_flags)) {
3098 HasTargetFlags = true;
3099 lex();
3100 if (expectAndConsume(MIToken::lparen))
3101 return true;
3102 if (Token.isNot(MIToken::Identifier))
3103 return error("expected the name of the target flag");
3104 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3105 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3106 return error("use of undefined target flag '" + Token.stringValue() +
3107 "'");
3108 }
3109 lex();
3110 while (Token.is(MIToken::comma)) {
3111 lex();
3112 if (Token.isNot(MIToken::Identifier))
3113 return error("expected the name of the target flag");
3114 unsigned BitFlag = 0;
3115 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3116 return error("use of undefined target flag '" + Token.stringValue() +
3117 "'");
3118 // TODO: Report an error when using a duplicate bit target flag.
3119 TF |= BitFlag;
3120 lex();
3121 }
3122 if (expectAndConsume(MIToken::rparen))
3123 return true;
3124 }
3125 auto Loc = Token.location();
3126 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3127 return true;
3128 if (!HasTargetFlags)
3129 return false;
3130 if (Dest.isReg())
3131 return error(Loc, "register operands can't have target flags");
3132 Dest.setTargetFlags(TF);
3133 return false;
3134}
3135
3136bool MIParser::parseOffset(int64_t &Offset) {
3137 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3138 return false;
3139 StringRef Sign = Token.range();
3140 bool IsNegative = Token.is(MIToken::minus);
3141 lex();
3142 if (Token.isNot(MIToken::IntegerLiteral))
3143 return error("expected an integer literal after '" + Sign + "'");
3144 if (Token.integerValue().getSignificantBits() > 64)
3145 return error("expected 64-bit integer (too large)");
3146 Offset = Token.integerValue().getExtValue();
3147 if (IsNegative)
3148 Offset = -Offset;
3149 lex();
3150 return false;
3151}
3152
3153bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3155 lex();
3156 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3157 return error("expected basic block after 'ir_block_address_taken'");
3158
3159 if (parseIRBlock(BB, MF.getFunction()))
3160 return true;
3161
3162 lex();
3163 return false;
3164}
3165
3166bool MIParser::parseAlignment(uint64_t &Alignment) {
3167 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3168 lex();
3169 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3170 return error("expected an integer literal after 'align'");
3171 if (getUint64(Alignment))
3172 return true;
3173 lex();
3174
3175 if (!isPowerOf2_64(Alignment))
3176 return error("expected a power-of-2 literal after 'align'");
3177
3178 return false;
3179}
3180
3181bool MIParser::parseAddrspace(unsigned &Addrspace) {
3182 assert(Token.is(MIToken::kw_addrspace));
3183 lex();
3184 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3185 return error("expected an integer literal after 'addrspace'");
3186 if (getUnsigned(Addrspace))
3187 return true;
3188 lex();
3189 return false;
3190}
3191
3192bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3193 int64_t Offset = 0;
3194 if (parseOffset(Offset))
3195 return true;
3196 Op.setOffset(Offset);
3197 return false;
3198}
3199
3200static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3201 const Value *&V, ErrorCallbackType ErrCB) {
3202 switch (Token.kind()) {
3203 case MIToken::NamedIRValue: {
3204 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3205 break;
3206 }
3207 case MIToken::IRValue: {
3208 unsigned SlotNumber = 0;
3209 if (getUnsigned(Token, SlotNumber, ErrCB))
3210 return true;
3211 V = PFS.getIRValue(SlotNumber);
3212 break;
3213 }
3215 case MIToken::GlobalValue: {
3216 GlobalValue *GV = nullptr;
3217 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3218 return true;
3219 V = GV;
3220 break;
3221 }
3223 const Constant *C = nullptr;
3224 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3225 return true;
3226 V = C;
3227 break;
3228 }
3230 V = nullptr;
3231 return false;
3232 default:
3233 llvm_unreachable("The current token should be an IR block reference");
3234 }
3235 if (!V)
3236 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3237 return false;
3238}
3239
3240bool MIParser::parseIRValue(const Value *&V) {
3241 return ::parseIRValue(
3242 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3243 return error(Loc, Msg);
3244 });
3245}
3246
3247bool MIParser::getUint64(uint64_t &Result) {
3248 if (Token.hasIntegerValue()) {
3249 if (Token.integerValue().getActiveBits() > 64)
3250 return error("expected 64-bit integer (too large)");
3251 Result = Token.integerValue().getZExtValue();
3252 return false;
3253 }
3254 if (Token.is(MIToken::HexLiteral)) {
3255 APInt A;
3256 if (getHexUint(A))
3257 return true;
3258 if (A.getBitWidth() > 64)
3259 return error("expected 64-bit integer (too large)");
3260 Result = A.getZExtValue();
3261 return false;
3262 }
3263 return true;
3264}
3265
3266bool MIParser::getHexUint(APInt &Result) {
3267 return ::getHexUint(Token, Result);
3268}
3269
3270bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3271 const auto OldFlags = Flags;
3272 switch (Token.kind()) {
3275 break;
3278 break;
3281 break;
3284 break;
3287 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3288 return error("use of undefined target MMO flag '" + Token.stringValue() +
3289 "'");
3290 Flags |= TF;
3291 break;
3292 }
3293 default:
3294 llvm_unreachable("The current token should be a memory operand flag");
3295 }
3296 if (OldFlags == Flags)
3297 // We know that the same flag is specified more than once when the flags
3298 // weren't modified.
3299 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3300 lex();
3301 return false;
3302}
3303
3304bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3305 switch (Token.kind()) {
3306 case MIToken::kw_stack:
3307 PSV = MF.getPSVManager().getStack();
3308 break;
3309 case MIToken::kw_got:
3310 PSV = MF.getPSVManager().getGOT();
3311 break;
3313 PSV = MF.getPSVManager().getJumpTable();
3314 break;
3316 PSV = MF.getPSVManager().getConstantPool();
3317 break;
3319 int FI;
3320 if (parseFixedStackFrameIndex(FI))
3321 return true;
3322 PSV = MF.getPSVManager().getFixedStack(FI);
3323 // The token was already consumed, so use return here instead of break.
3324 return false;
3325 }
3326 case MIToken::StackObject: {
3327 int FI;
3328 if (parseStackFrameIndex(FI))
3329 return true;
3330 PSV = MF.getPSVManager().getFixedStack(FI);
3331 // The token was already consumed, so use return here instead of break.
3332 return false;
3333 }
3335 lex();
3336 switch (Token.kind()) {
3339 GlobalValue *GV = nullptr;
3340 if (parseGlobalValue(GV))
3341 return true;
3342 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3343 break;
3344 }
3346 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3347 MF.createExternalSymbolName(Token.stringValue()));
3348 break;
3349 default:
3350 return error(
3351 "expected a global value or an external symbol after 'call-entry'");
3352 }
3353 break;
3354 case MIToken::kw_custom: {
3355 lex();
3356 const auto *TII = MF.getSubtarget().getInstrInfo();
3357 if (const auto *Formatter = TII->getMIRFormatter()) {
3358 if (Formatter->parseCustomPseudoSourceValue(
3359 Token.stringValue(), MF, PFS, PSV,
3360 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3361 return error(Loc, Msg);
3362 }))
3363 return true;
3364 } else {
3365 return error("unable to parse target custom pseudo source value");
3366 }
3367 break;
3368 }
3369 default:
3370 llvm_unreachable("The current token should be pseudo source value");
3371 }
3372 lex();
3373 return false;
3374}
3375
3376bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3377 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3378 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3379 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3380 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3381 const PseudoSourceValue *PSV = nullptr;
3382 if (parseMemoryPseudoSourceValue(PSV))
3383 return true;
3384 int64_t Offset = 0;
3385 if (parseOffset(Offset))
3386 return true;
3387 Dest = MachinePointerInfo(PSV, Offset);
3388 return false;
3389 }
3390 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3391 Token.isNot(MIToken::GlobalValue) &&
3392 Token.isNot(MIToken::NamedGlobalValue) &&
3393 Token.isNot(MIToken::QuotedIRValue) &&
3394 Token.isNot(MIToken::kw_unknown_address))
3395 return error("expected an IR value reference");
3396 const Value *V = nullptr;
3397 if (parseIRValue(V))
3398 return true;
3399 if (V && !V->getType()->isPointerTy())
3400 return error("expected a pointer IR value");
3401 lex();
3402 int64_t Offset = 0;
3403 if (parseOffset(Offset))
3404 return true;
3405 Dest = MachinePointerInfo(V, Offset);
3406 return false;
3407}
3408
3409bool MIParser::parseOptionalScope(LLVMContext &Context,
3410 SyncScope::ID &SSID) {
3411 SSID = SyncScope::System;
3412 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3413 lex();
3414 if (expectAndConsume(MIToken::lparen))
3415 return error("expected '(' in syncscope");
3416
3417 std::string SSN;
3418 if (parseStringConstant(SSN))
3419 return true;
3420
3421 SSID = Context.getOrInsertSyncScopeID(SSN);
3422 if (expectAndConsume(MIToken::rparen))
3423 return error("expected ')' in syncscope");
3424 }
3425
3426 return false;
3427}
3428
3429bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3431 if (Token.isNot(MIToken::Identifier))
3432 return false;
3433
3434 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3435 .Case("unordered", AtomicOrdering::Unordered)
3436 .Case("monotonic", AtomicOrdering::Monotonic)
3437 .Case("acquire", AtomicOrdering::Acquire)
3438 .Case("release", AtomicOrdering::Release)
3442
3443 if (Order != AtomicOrdering::NotAtomic) {
3444 lex();
3445 return false;
3446 }
3447
3448 return error("expected an atomic scope, ordering or a size specification");
3449}
3450
3451bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3452 if (expectAndConsume(MIToken::lparen))
3453 return true;
3455 while (Token.isMemoryOperandFlag()) {
3456 if (parseMemoryOperandFlag(Flags))
3457 return true;
3458 }
3459 if (Token.isNot(MIToken::Identifier) ||
3460 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3461 return error("expected 'load' or 'store' memory operation");
3462 if (Token.stringValue() == "load")
3464 else
3466 lex();
3467
3468 // Optional 'store' for operands that both load and store.
3469 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3471 lex();
3472 }
3473
3474 // Optional synchronization scope.
3475 SyncScope::ID SSID;
3476 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3477 return true;
3478
3479 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3480 AtomicOrdering Order, FailureOrder;
3481 if (parseOptionalAtomicOrdering(Order))
3482 return true;
3483
3484 if (parseOptionalAtomicOrdering(FailureOrder))
3485 return true;
3486
3487 if (Token.isNot(MIToken::IntegerLiteral) &&
3488 Token.isNot(MIToken::kw_unknown_size) &&
3489 Token.isNot(MIToken::lparen))
3490 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3491 "memory operation");
3492
3494 if (Token.is(MIToken::IntegerLiteral)) {
3495 uint64_t Size;
3496 if (getUint64(Size))
3497 return true;
3498
3499 // Convert from bytes to bits for storage.
3501 lex();
3502 } else if (Token.is(MIToken::kw_unknown_size)) {
3503 lex();
3504 } else {
3505 if (expectAndConsume(MIToken::lparen))
3506 return true;
3507 if (parseLowLevelType(Token.location(), MemoryType))
3508 return true;
3509 if (expectAndConsume(MIToken::rparen))
3510 return true;
3511 }
3512
3514 if (Token.is(MIToken::Identifier)) {
3515 const char *Word =
3518 ? "on"
3519 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3520 if (Token.stringValue() != Word)
3521 return error(Twine("expected '") + Word + "'");
3522 lex();
3523
3524 if (parseMachinePointerInfo(Ptr))
3525 return true;
3526 }
3527 uint64_t BaseAlignment =
3528 MemoryType.isValid()
3529 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3530 : 1;
3531 AAMDNodes AAInfo;
3532 MDNode *Range = nullptr;
3533 while (consumeIfPresent(MIToken::comma)) {
3534 switch (Token.kind()) {
3535 case MIToken::kw_align: {
3536 // align is printed if it is different than size.
3537 uint64_t Alignment;
3538 if (parseAlignment(Alignment))
3539 return true;
3540 if (Ptr.Offset & (Alignment - 1)) {
3541 // MachineMemOperand::getAlign never returns a value greater than the
3542 // alignment of offset, so this just guards against hand-written MIR
3543 // that specifies a large "align" value when it should probably use
3544 // "basealign" instead.
3545 return error("specified alignment is more aligned than offset");
3546 }
3547 BaseAlignment = Alignment;
3548 break;
3549 }
3551 // basealign is printed if it is different than align.
3552 if (parseAlignment(BaseAlignment))
3553 return true;
3554 break;
3556 if (parseAddrspace(Ptr.AddrSpace))
3557 return true;
3558 break;
3559 case MIToken::md_tbaa:
3560 lex();
3561 if (parseMDNode(AAInfo.TBAA))
3562 return true;
3563 break;
3565 lex();
3566 if (parseMDNode(AAInfo.Scope))
3567 return true;
3568 break;
3570 lex();
3571 if (parseMDNode(AAInfo.NoAlias))
3572 return true;
3573 break;
3575 lex();
3576 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3577 return true;
3578 break;
3579 case MIToken::md_range:
3580 lex();
3581 if (parseMDNode(Range))
3582 return true;
3583 break;
3584 // TODO: Report an error on duplicate metadata nodes.
3585 default:
3586 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3587 "'!noalias' or '!range' or '!noalias.addrspace'");
3588 }
3589 }
3590 if (expectAndConsume(MIToken::rparen))
3591 return true;
3592 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3593 AAInfo, Range, SSID, Order, FailureOrder);
3594 return false;
3595}
3596
3597bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3599 Token.is(MIToken::kw_post_instr_symbol)) &&
3600 "Invalid token for a pre- post-instruction symbol!");
3601 lex();
3602 if (Token.isNot(MIToken::MCSymbol))
3603 return error("expected a symbol after 'pre-instr-symbol'");
3604 Symbol = getOrCreateMCSymbol(Token.stringValue());
3605 lex();
3606 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3607 Token.is(MIToken::lbrace))
3608 return false;
3609 if (Token.isNot(MIToken::comma))
3610 return error("expected ',' before the next machine operand");
3611 lex();
3612 return false;
3613}
3614
3615bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3617 "Invalid token for a heap alloc marker!");
3618 lex();
3619 if (parseMDNode(Node))
3620 return true;
3621 if (!Node)
3622 return error("expected a MDNode after 'heap-alloc-marker'");
3623 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3624 Token.is(MIToken::lbrace))
3625 return false;
3626 if (Token.isNot(MIToken::comma))
3627 return error("expected ',' before the next machine operand");
3628 lex();
3629 return false;
3630}
3631
3632bool MIParser::parsePCSections(MDNode *&Node) {
3633 assert(Token.is(MIToken::kw_pcsections) &&
3634 "Invalid token for a PC sections!");
3635 lex();
3636 if (parseMDNode(Node))
3637 return true;
3638 if (!Node)
3639 return error("expected a MDNode after 'pcsections'");
3640 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3641 Token.is(MIToken::lbrace))
3642 return false;
3643 if (Token.isNot(MIToken::comma))
3644 return error("expected ',' before the next machine operand");
3645 lex();
3646 return false;
3647}
3648
3649bool MIParser::parseMMRA(MDNode *&Node) {
3650 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3651 lex();
3652 if (parseMDNode(Node))
3653 return true;
3654 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3655 Token.is(MIToken::lbrace))
3656 return false;
3657 if (Token.isNot(MIToken::comma))
3658 return error("expected ',' before the next machine operand");
3659 lex();
3660 return false;
3661}
3662
3664 const Function &F,
3665 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3666 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
3668 for (const auto &BB : F) {
3669 if (BB.hasName())
3670 continue;
3671 int Slot = MST.getLocalSlot(&BB);
3672 if (Slot == -1)
3673 continue;
3674 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3675 }
3676}
3677
3679 unsigned Slot,
3680 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3681 return Slots2BasicBlocks.lookup(Slot);
3682}
3683
3684const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3685 if (Slots2BasicBlocks.empty())
3686 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3687 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3688}
3689
3690const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3691 if (&F == &MF.getFunction())
3692 return getIRBlock(Slot);
3693 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3694 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3695 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3696}
3697
3698MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3699 // FIXME: Currently we can't recognize temporary or local symbols and call all
3700 // of the appropriate forms to create them. However, this handles basic cases
3701 // well as most of the special aspects are recognized by a prefix on their
3702 // name, and the input names should already be unique. For test cases, keeping
3703 // the symbol name out of the symbol table isn't terribly important.
3704 return MF.getContext().getOrCreateSymbol(Name);
3705}
3706
3707bool MIParser::parseStringConstant(std::string &Result) {
3708 if (Token.isNot(MIToken::StringConstant))
3709 return error("expected string constant");
3710 Result = std::string(Token.stringValue());
3711 lex();
3712 return false;
3713}
3714
3716 StringRef Src,
3718 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3719}
3720
3723 return MIParser(PFS, Error, Src).parseBasicBlocks();
3724}
3725
3729 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3730}
3731
3733 Register &Reg, StringRef Src,
3735 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3736}
3737
3739 Register &Reg, StringRef Src,
3741 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3742}
3743
3745 VRegInfo *&Info, StringRef Src,
3747 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3748}
3749
3752 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3753}
3754
3758 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
3759}
3762 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3763}
3764
3766 SMRange SrcRange, SMDiagnostic &Error) {
3767 return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata();
3768}
3769
3771 PerFunctionMIParsingState &PFS, const Value *&V,
3772 ErrorCallbackType ErrorCallback) {
3773 MIToken Token;
3774 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3775 ErrorCallback(Loc, Msg);
3776 });
3777 V = nullptr;
3778
3779 return ::parseIRValue(Token, PFS, V, ErrorCallback);
3780}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
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< 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...
dxil translate DXIL Translate Metadata
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(DataExtractor &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 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:360
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 bool verifyVectorElementCount(uint64_t NumElts)
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value * > &Slots2Values)
Definition MIParser.cpp:351
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
Definition MIParser.cpp:625
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
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
#define T
MachineInstr unsigned OpIdx
static constexpr unsigned SM(unsigned Version)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash, uint32_t &Attributes)
Parse Input that contains metadata.
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:1555
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:476
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
bool isNegative() const
Determine sign of this APSInt.
Definition APSInt.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & back() const
back - Get the last element.
Definition ArrayRef.h:151
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
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 BranchProbability getRaw(uint32_t N)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:686
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
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:123
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
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:817
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).
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:583
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:664
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:657
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:608
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:633
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:576
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:618
static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
Definition MCDwarf.h:649
static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state AArch64 negate RA state.
Definition MCDwarf.h:644
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:677
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:591
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:688
static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc={})
.cfi_window_save SPARC register window is saved.
Definition MCDwarf.h:639
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:599
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:682
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:671
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:626
Describe properties that are true of each instruction in the target description file.
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:1080
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition Metadata.h:1540
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1529
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition Metadata.h:1549
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
Root of the metadata hierarchy.
Definition Metadata.h:64
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:67
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:297
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
Represents a range in source code.
Definition SMLoc.h:47
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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
bool empty() const
Definition StringMap.h:108
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:321
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:730
const char * iterator
Definition StringRef.h:59
std::string str() const
str - 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:591
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:629
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
char front() const
front - Get the first character in the string.
Definition StringRef.h:146
LLVM_ABI std::string lower() const
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
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
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:262
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ 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.
@ 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.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
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:1739
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:284
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
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...
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:385
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:236
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:189
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)
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
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:195
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src, SMRange SourceRange, SMDiagnostic &Error)
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:792
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:786
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:789
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:210
bool hasIntegerValue() const
Definition MILexer.h:250
bool is(TokenKind K) const
Definition MILexer.h:237
StringRef stringValue() const
Return the token's string value.
Definition MILexer.h:246
@ kw_pre_instr_symbol
Definition MILexer.h:136
@ kw_deactivation_symbol
Definition MILexer.h:141
@ kw_call_frame_size
Definition MILexer.h:148
@ kw_cfi_aarch64_negate_ra_sign_state
Definition MILexer.h:100
@ kw_cfi_llvm_def_aspace_cfa
Definition MILexer.h:93
@ MachineBasicBlock
Definition MILexer.h:169
@ kw_dbg_instr_ref
Definition MILexer.h:84
@ NamedVirtualRegister
Definition MILexer.h:167
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:179
@ kw_cfi_window_save
Definition MILexer.h:99
@ kw_frame_destroy
Definition MILexer.h:64
@ kw_cfi_undefined
Definition MILexer.h:98
@ MachineBasicBlockLabel
Definition MILexer.h:168
@ kw_cfi_register
Definition MILexer.h:94
@ kw_inlineasm_br_indirect_target
Definition MILexer.h:128
@ kw_cfi_rel_offset
Definition MILexer.h:87
@ kw_ehfunclet_entry
Definition MILexer.h:130
@ kw_cfi_aarch64_negate_ra_sign_state_with_pc
Definition MILexer.h:101
@ kw_cfi_def_cfa_register
Definition MILexer.h:88
@ kw_cfi_same_value
Definition MILexer.h:85
@ kw_cfi_adjust_cfa_offset
Definition MILexer.h:90
@ kw_dereferenceable
Definition MILexer.h:55
@ kw_implicit_define
Definition MILexer.h:52
@ kw_cfi_def_cfa_offset
Definition MILexer.h:89
@ kw_machine_block_address_taken
Definition MILexer.h:147
@ kw_cfi_remember_state
Definition MILexer.h:95
@ kw_debug_instr_number
Definition MILexer.h:83
@ kw_post_instr_symbol
Definition MILexer.h:137
@ kw_cfi_restore_state
Definition MILexer.h:97
@ kw_ir_block_address_taken
Definition MILexer.h:146
@ kw_unknown_address
Definition MILexer.h:145
@ md_noalias_addrspace
Definition MILexer.h:159
@ kw_debug_location
Definition MILexer.h:82
@ kw_heap_alloc_marker
Definition MILexer.h:138
StringRef range() const
Definition MILexer.h:243
StringRef::iterator location() const
Definition MILexer.h:241
const APSInt & integerValue() const
Definition MILexer.h:248
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*.
VRegInfo & getVRegInfo(Register Num)
Definition MIParser.cpp:328
const SlotMapping & IRSlots
Definition MIParser.h:170
const Value * getIRValue(unsigned Slot)
Definition MIParser.cpp:373
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition MIParser.h:176
StringMap< VRegInfo * > VRegInfosNamed
Definition MIParser.h:178
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition MIParser.h:185
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition MIParser.cpp:323
PerTargetMIParsingState & Target
Definition MIParser.h:171
DenseMap< Register, VRegInfo * > VRegInfos
Definition MIParser.h:177
VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition MIParser.cpp:339
bool getVRegFlagValue(StringRef FlagName, uint8_t &FlagValue) const
Definition MIParser.cpp:128
bool getDirectTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a direct target flag to the corresponding target flag.
Definition MIParser.cpp:226
const RegisterBank * getRegBank(StringRef Name)
Check if the given identifier is a name of a register bank.
Definition MIParser.cpp:316
bool parseInstrName(StringRef InstrName, unsigned &OpCode)
Try to convert an instruction name to an opcode.
Definition MIParser.cpp:147
unsigned getSubRegIndex(StringRef Name)
Check if the given identifier is a name of a subregister index.
Definition MIParser.cpp:187
bool getTargetIndex(StringRef Name, int &Index)
Try to convert a name of target index to the corresponding target index.
Definition MIParser.cpp:205
void setTarget(const TargetSubtargetInfo &NewSubtarget)
Definition MIParser.cpp:80
bool getRegisterByName(StringRef RegName, Register &Reg)
Try to convert a register name to a register number.
Definition MIParser.cpp:118
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:269
bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a bitmask target flag to the corresponding target flag.
Definition MIParser.cpp:248
const TargetRegisterClass * getRegClass(StringRef Name)
Check if the given identifier is a name of a register class.
Definition MIParser.cpp:309
const uint32_t * getRegMask(StringRef Identifier)
Check if the given identifier is a name of a register mask.
Definition MIParser.cpp:170
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:45
union llvm::VRegInfo::@127225073067155374133234315364317264041071000132 D
const TargetRegisterClass * RC
Definition MIParser.h:44
enum llvm::VRegInfo::@374354327266250320012227113300214031244227062232 Kind
Register VReg
Definition MIParser.h:47
bool Explicit
VReg was explicitly specified in the .mir file.
Definition MIParser.h:42