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