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 if (!isa<DILocation>(Node))
1200 return error("referenced metadata is not a DILocation");
1201 DebugLocation = DebugLoc(Node);
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 // clang-format on
1541 // Mine frame and fast math flags
1542 if (Token.is(MIToken::kw_frame_setup))
1544 if (Token.is(MIToken::kw_frame_destroy))
1546 if (Token.is(MIToken::kw_nnan))
1548 if (Token.is(MIToken::kw_ninf))
1550 if (Token.is(MIToken::kw_nsz))
1552 if (Token.is(MIToken::kw_arcp))
1554 if (Token.is(MIToken::kw_contract))
1556 if (Token.is(MIToken::kw_afn))
1558 if (Token.is(MIToken::kw_reassoc))
1560 if (Token.is(MIToken::kw_nuw))
1562 if (Token.is(MIToken::kw_nsw))
1564 if (Token.is(MIToken::kw_exact))
1566 if (Token.is(MIToken::kw_nofpexcept))
1568 if (Token.is(MIToken::kw_unpredictable))
1570 if (Token.is(MIToken::kw_noconvergent))
1572 if (Token.is(MIToken::kw_nneg))
1574 if (Token.is(MIToken::kw_disjoint))
1576 if (Token.is(MIToken::kw_nusw))
1578 if (Token.is(MIToken::kw_samesign))
1580 if (Token.is(MIToken::kw_inbounds))
1582
1583 lex();
1584 }
1585 if (Token.isNot(MIToken::Identifier))
1586 return error("expected a machine instruction");
1587 StringRef InstrName = Token.stringValue();
1588 if (PFS.Target.parseInstrName(InstrName, OpCode))
1589 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1590 lex();
1591 return false;
1592}
1593
1594bool MIParser::parseNamedRegister(Register &Reg) {
1595 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1596 StringRef Name = Token.stringValue();
1597 if (PFS.Target.getRegisterByName(Name, Reg))
1598 return error(Twine("unknown register name '") + Name + "'");
1599 return false;
1600}
1601
1602bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1603 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1604 StringRef Name = Token.stringValue();
1605 // TODO: Check that the VReg name is not the same as a physical register name.
1606 // If it is, then print a warning (when warnings are implemented).
1607 Info = &PFS.getVRegInfoNamed(Name);
1608 return false;
1609}
1610
1611bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1612 if (Token.is(MIToken::NamedVirtualRegister))
1613 return parseNamedVirtualRegister(Info);
1614 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1615 unsigned ID;
1616 if (getUnsigned(ID))
1617 return true;
1618 Info = &PFS.getVRegInfo(ID);
1619 return false;
1620}
1621
1622bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1623 switch (Token.kind()) {
1625 Reg = 0;
1626 return false;
1628 return parseNamedRegister(Reg);
1631 if (parseVirtualRegister(Info))
1632 return true;
1633 Reg = Info->VReg;
1634 return false;
1635 // TODO: Parse other register kinds.
1636 default:
1637 llvm_unreachable("The current token should be a register");
1638 }
1639}
1640
1641bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1642 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1643 return error("expected '_', register class, or register bank name");
1644 StringRef::iterator Loc = Token.location();
1645 StringRef Name = Token.stringValue();
1646
1647 // Was it a register class?
1648 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1649 if (RC) {
1650 lex();
1651
1652 switch (RegInfo.Kind) {
1653 case VRegInfo::UNKNOWN:
1654 case VRegInfo::NORMAL:
1655 RegInfo.Kind = VRegInfo::NORMAL;
1656 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1657 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1658 return error(Loc, Twine("conflicting register classes, previously: ") +
1659 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1660 }
1661 RegInfo.D.RC = RC;
1662 RegInfo.Explicit = true;
1663 return false;
1664
1665 case VRegInfo::GENERIC:
1666 case VRegInfo::REGBANK:
1667 return error(Loc, "register class specification on generic register");
1668 }
1669 llvm_unreachable("Unexpected register kind");
1670 }
1671
1672 // Should be a register bank or a generic register.
1673 const RegisterBank *RegBank = nullptr;
1674 if (Name != "_") {
1675 RegBank = PFS.Target.getRegBank(Name);
1676 if (!RegBank)
1677 return error(Loc, "expected '_', register class, or register bank name");
1678 }
1679
1680 lex();
1681
1682 switch (RegInfo.Kind) {
1683 case VRegInfo::UNKNOWN:
1684 case VRegInfo::GENERIC:
1685 case VRegInfo::REGBANK:
1686 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1687 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1688 return error(Loc, "conflicting generic register banks");
1689 RegInfo.D.RegBank = RegBank;
1690 RegInfo.Explicit = true;
1691 return false;
1692
1693 case VRegInfo::NORMAL:
1694 return error(Loc, "register bank specification on normal register");
1695 }
1696 llvm_unreachable("Unexpected register kind");
1697}
1698
1699bool MIParser::parseRegisterFlag(RegState &Flags) {
1700 const RegState OldFlags = Flags;
1701 switch (Token.kind()) {
1704 break;
1707 break;
1708 case MIToken::kw_def:
1710 break;
1711 case MIToken::kw_dead:
1713 break;
1714 case MIToken::kw_killed:
1716 break;
1717 case MIToken::kw_undef:
1719 break;
1722 break;
1725 break;
1728 break;
1731 break;
1732 default:
1733 llvm_unreachable("The current token should be a register flag");
1734 }
1735 if (OldFlags == Flags)
1736 // We know that the same flag is specified more than once when the flags
1737 // weren't modified.
1738 return error("duplicate '" + Token.stringValue() + "' register flag");
1739 lex();
1740 return false;
1741}
1742
1743bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1744 assert(Token.is(MIToken::dot));
1745 lex();
1746 if (Token.isNot(MIToken::Identifier))
1747 return error("expected a subregister index after '.'");
1748 auto Name = Token.stringValue();
1749 SubReg = PFS.Target.getSubRegIndex(Name);
1750 if (!SubReg)
1751 return error(Twine("use of unknown subregister index '") + Name + "'");
1752 lex();
1753 return false;
1754}
1755
1756bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1757 assert(Token.is(MIToken::kw_tied_def));
1758 lex();
1759 if (Token.isNot(MIToken::IntegerLiteral))
1760 return error("expected an integer literal after 'tied-def'");
1761 if (getUnsigned(TiedDefIdx))
1762 return true;
1763 lex();
1764 return expectAndConsume(MIToken::rparen);
1765}
1766
1767bool MIParser::assignRegisterTies(MachineInstr &MI,
1769 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1770 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1771 if (!Operands[I].TiedDefIdx)
1772 continue;
1773 // The parser ensures that this operand is a register use, so we just have
1774 // to check the tied-def operand.
1775 unsigned DefIdx = *Operands[I].TiedDefIdx;
1776 if (DefIdx >= E)
1777 return error(Operands[I].Begin,
1778 Twine("use of invalid tied-def operand index '" +
1779 Twine(DefIdx) + "'; instruction has only ") +
1780 Twine(E) + " operands");
1781 const auto &DefOperand = Operands[DefIdx].Operand;
1782 if (!DefOperand.isReg() || !DefOperand.isDef())
1783 // FIXME: add note with the def operand.
1784 return error(Operands[I].Begin,
1785 Twine("use of invalid tied-def operand index '") +
1786 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1787 " isn't a defined register");
1788 // Check that the tied-def operand wasn't tied elsewhere.
1789 for (const auto &TiedPair : TiedRegisterPairs) {
1790 if (TiedPair.first == DefIdx)
1791 return error(Operands[I].Begin,
1792 Twine("the tied-def operand #") + Twine(DefIdx) +
1793 " is already tied with another register operand");
1794 }
1795 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1796 }
1797 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1798 // indices must be less than tied max.
1799 for (const auto &TiedPair : TiedRegisterPairs)
1800 MI.tieOperands(TiedPair.first, TiedPair.second);
1801 return false;
1802}
1803
1804bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1805 std::optional<unsigned> &TiedDefIdx,
1806 bool IsDef) {
1807 RegState Flags = getDefRegState(IsDef);
1808 while (Token.isRegisterFlag()) {
1809 if (parseRegisterFlag(Flags))
1810 return true;
1811 }
1812 // Update IsDef as we may have read a def flag.
1813 IsDef = hasRegState(Flags, RegState::Define);
1814 if (!Token.isRegister())
1815 return error("expected a register after register flags");
1816 Register Reg;
1817 VRegInfo *RegInfo;
1818 if (parseRegister(Reg, RegInfo))
1819 return true;
1820 lex();
1821 unsigned SubReg = 0;
1822 if (Token.is(MIToken::dot)) {
1823 if (parseSubRegisterIndex(SubReg))
1824 return true;
1825 if (!Reg.isVirtual())
1826 return error("subregister index expects a virtual register");
1827 }
1828 if (Token.is(MIToken::colon)) {
1829 if (!Reg.isVirtual())
1830 return error("register class specification expects a virtual register");
1831 lex();
1832 if (parseRegisterClassOrBank(*RegInfo))
1833 return true;
1834 }
1835
1836 if (consumeIfPresent(MIToken::lparen)) {
1837 // For a def, we only expect a type. For use we expect either a type or a
1838 // tied-def. Additionally, for physical registers, we don't expect a type.
1839 if (Token.is(MIToken::kw_tied_def)) {
1840 if (IsDef)
1841 return error("tied-def not supported for defs");
1842 unsigned Idx;
1843 if (parseRegisterTiedDefIndex(Idx))
1844 return true;
1845 TiedDefIdx = Idx;
1846 } else {
1847 if (!Reg.isVirtual())
1848 return error("unexpected type on physical register");
1849
1850 LLT Ty;
1851 // If type parsing fails, forwad the parse error for defs.
1852 if (parseLowLevelType(Token.location(), Ty))
1853 return IsDef ? true
1854 : error("expected tied-def or low-level type after '('");
1855
1856 if (expectAndConsume(MIToken::rparen))
1857 return true;
1858
1859 MachineRegisterInfo &MRI = MF.getRegInfo();
1860 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1861 return error("inconsistent type for generic virtual register");
1862
1863 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1864 MRI.setType(Reg, Ty);
1866 }
1867 } else if (IsDef && Reg.isVirtual()) {
1868 // Generic virtual registers defs must have a type.
1869 if (RegInfo->Kind == VRegInfo::GENERIC ||
1870 RegInfo->Kind == VRegInfo::REGBANK)
1871 return error("generic virtual registers must have a type");
1872 }
1873
1874 if (IsDef) {
1875 if (hasRegState(Flags, RegState::Kill))
1876 return error("cannot have a killed def operand");
1877 } else {
1878 if (hasRegState(Flags, RegState::Dead))
1879 return error("cannot have a dead use operand");
1880 }
1881
1883 Reg, IsDef, hasRegState(Flags, RegState::Implicit),
1886 hasRegState(Flags, RegState::EarlyClobber), SubReg,
1890
1891 return false;
1892}
1893
1894bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1896 const APSInt &Int = Token.integerValue();
1897 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1898 Dest = MachineOperand::CreateImm(*SImm);
1899 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1900 Dest = MachineOperand::CreateImm(*UImm);
1901 else
1902 return error("integer literal is too large to be an immediate operand");
1903 lex();
1904 return false;
1905}
1906
1907bool MIParser::parseSymbolicInlineAsmOperand(unsigned OpIdx,
1908 MachineOperand &Dest) {
1910 assert(Token.is(MIToken::Identifier) &&
1911 "expected symbolic inline asm operand");
1912
1913 // Parse ExtraInfo flags.
1915 unsigned ExtraInfo = 0;
1916 for (;;) {
1917 if (Token.isNot(MIToken::Identifier))
1918 break;
1919
1920 StringRef FlagName = Token.stringValue();
1921 unsigned Flag = StringSwitch<unsigned>(FlagName)
1923 .Case("mayload", InlineAsm::Extra_MayLoad)
1924 .Case("maystore", InlineAsm::Extra_MayStore)
1925 .Case("isconvergent", InlineAsm::Extra_IsConvergent)
1926 .Case("alignstack", InlineAsm::Extra_IsAlignStack)
1928 .Case("attdialect", 0)
1929 .Case("inteldialect", InlineAsm::Extra_AsmDialect)
1930 .Default(~0u);
1931 if (Flag == ~0u)
1932 return error("unknown inline asm extra info flag '" + FlagName + "'");
1933
1934 ExtraInfo |= Flag;
1935 lex();
1936 }
1937
1938 Dest = MachineOperand::CreateImm(ExtraInfo);
1939 return false;
1940 }
1941
1942 // Parse symbolic form: kind[:constraint].
1943 StringRef KindStr = Token.stringValue();
1944 constexpr auto InvalidKind = static_cast<InlineAsm::Kind>(0);
1947 .Case("regdef", InlineAsm::Kind::RegDef)
1948 .Case("reguse", InlineAsm::Kind::RegUse)
1950 .Case("clobber", InlineAsm::Kind::Clobber)
1951 .Case("imm", InlineAsm::Kind::Imm)
1952 .Case("mem", InlineAsm::Kind::Mem)
1953 .Default(InvalidKind);
1954 if (K == InvalidKind)
1955 return error("unknown inline asm operand kind '" + KindStr + "'");
1956
1957 lex();
1958
1959 // Create the flag with default of 1 operand.
1960 InlineAsm::Flag F(K, 1);
1961
1962 // Parse optional tiedto constraint: tiedto:$N.
1963 if (Token.is(MIToken::Identifier) && Token.stringValue() == "tiedto") {
1964 lex();
1965 if (Token.isNot(MIToken::colon))
1966 return error("expected ':' after 'tiedto'");
1967 lex();
1968 if (Token.isNot(MIToken::NamedRegister))
1969 return error("expected '$N' operand number after 'tiedto:'");
1970 unsigned OperandNo;
1971 if (Token.stringValue().getAsInteger(10, OperandNo))
1972 return error("invalid operand number in tiedto constraint");
1973 lex();
1974
1975 F.setMatchingOp(OperandNo);
1976
1978 return false;
1979 }
1980
1981 // Parse optional constraint after ':'.
1982 if (Token.isNot(MIToken::colon)) {
1984 return false;
1985 }
1986
1987 lex();
1988
1989 if (Token.isNot(MIToken::Identifier))
1990 return error("expected register class or memory constraint name after ':'");
1991
1992 StringRef ConstraintStr = Token.stringValue();
1993 if (K == InlineAsm::Kind::Mem) {
2026 return error("unknown memory constraint '" + ConstraintStr + "'");
2027 F.setMemConstraint(CC);
2028 } else if (K == InlineAsm::Kind::RegDef || K == InlineAsm::Kind::RegUse ||
2030 const TargetRegisterClass *RC =
2031 PFS.Target.getRegClass(ConstraintStr.lower());
2032 if (!RC)
2033 return error("unknown register class '" + ConstraintStr + "'");
2034 F.setRegClass(RC->getID());
2035 }
2036
2037 lex();
2038
2040 return false;
2041}
2042
2043bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
2044 const unsigned OpIdx,
2045 MachineOperand &Dest,
2046 const MIRFormatter &MF) {
2047 assert(Token.is(MIToken::dot));
2048 auto Loc = Token.location(); // record start position
2049 size_t Len = 1; // for "."
2050 lex();
2051
2052 // Handle the case that mnemonic starts with number.
2053 if (Token.is(MIToken::IntegerLiteral)) {
2054 Len += Token.range().size();
2055 lex();
2056 }
2057
2058 StringRef Src;
2059 if (Token.is(MIToken::comma))
2060 Src = StringRef(Loc, Len);
2061 else {
2062 assert(Token.is(MIToken::Identifier));
2063 Src = StringRef(Loc, Len + Token.stringValue().size());
2064 }
2065 int64_t Val;
2066 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
2067 [this](StringRef::iterator Loc, const Twine &Msg)
2068 -> bool { return error(Loc, Msg); }))
2069 return true;
2070
2071 Dest = MachineOperand::CreateImm(Val);
2072 if (!Token.is(MIToken::comma))
2073 lex();
2074 return false;
2075}
2076
2078 PerFunctionMIParsingState &PFS, const Constant *&C,
2079 ErrorCallbackType ErrCB) {
2080 auto Source = StringValue.str(); // The source has to be null terminated.
2081 SMDiagnostic Err;
2082 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
2083 &PFS.IRSlots);
2084 if (!C)
2085 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
2086 return false;
2087}
2088
2089bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
2090 const Constant *&C) {
2091 return ::parseIRConstant(
2092 Loc, StringValue, PFS, C,
2093 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2094 return error(Loc, Msg);
2095 });
2096}
2097
2098bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
2099 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
2100 return true;
2101 lex();
2102 return false;
2103}
2104
2105// See LLT implementation for bit size limits.
2107 return Size != 0 && isUInt<16>(Size);
2108}
2109
2111 return NumElts != 0 && isUInt<16>(NumElts);
2112}
2113
2114static bool verifyAddrSpace(uint64_t AddrSpace) {
2115 return isUInt<24>(AddrSpace);
2116}
2117
2118bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
2119 StringRef TypeDigits = Token.range();
2120 if (TypeDigits.consume_front("s") || TypeDigits.consume_front("i") ||
2121 TypeDigits.consume_front("f") || TypeDigits.consume_front("p") ||
2122 TypeDigits.consume_front("bf")) {
2123 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2124 return error(
2125 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2126 }
2127
2128 bool Scalar = Token.range().starts_with("s");
2129 if (Scalar || Token.range().starts_with("i")) {
2130 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2131 if (!ScalarSize) {
2132 Ty = LLT::token();
2133 lex();
2134 return false;
2135 }
2136
2137 if (!verifyScalarSize(ScalarSize))
2138 return error("invalid size for scalar type");
2139
2140 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2141 lex();
2142 return false;
2143 }
2144
2145 if (Token.range().starts_with("p")) {
2146 const DataLayout &DL = MF.getDataLayout();
2147 uint64_t AS = APSInt(TypeDigits).getZExtValue();
2148 if (!verifyAddrSpace(AS))
2149 return error("invalid address space number");
2150
2151 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2152 lex();
2153 return false;
2154 }
2155
2156 if (Token.range().starts_with("f") || Token.range().starts_with("bf")) {
2157 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2158 if (!ScalarSize || !verifyScalarSize(ScalarSize))
2159 return error("invalid size for scalar type");
2160
2161 if (Token.range().starts_with("bf") && ScalarSize != 16)
2162 return error("invalid size for bfloat");
2163
2164 Ty = Token.range().starts_with("bf") ? LLT::bfloat16()
2165 : LLT::floatIEEE(ScalarSize);
2166 lex();
2167 return false;
2168 }
2169
2170 // Now we're looking for a vector.
2171 if (Token.isNot(MIToken::less))
2172 return error(Loc, "expected tN, pA, <M x tN>, <M x pA>, <vscale x M x tN>, "
2173 "or <vscale x M x pA> for GlobalISel type, "
2174 "where t = {'s', 'i', 'f', 'bf'}");
2175 lex();
2176
2177 bool HasVScale =
2178 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2179 if (HasVScale) {
2180 lex();
2181 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2182 return error(
2183 "expected <vscale x M x tN>, where t = {'s', 'i', 'f', 'bf', 'p'}");
2184 lex();
2185 }
2186
2187 auto GetError = [this, &HasVScale, Loc]() {
2188 if (HasVScale)
2189 return error(Loc, "expected <vscale x M x tN> for vector type, where t = "
2190 "{'s', 'i', 'f', 'bf', 'p'}");
2191 return error(Loc, "expected <M x tN> for vector type, where t = {'s', 'i', "
2192 "'f', 'bf', 'p'}");
2193 };
2194
2195 if (Token.isNot(MIToken::IntegerLiteral))
2196 return GetError();
2197 uint64_t NumElements = Token.integerValue().getZExtValue();
2198 if (!verifyVectorElementCount(NumElements))
2199 return error("invalid number of vector elements");
2200
2201 lex();
2202
2203 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2204 return GetError();
2205 lex();
2206
2207 StringRef VectorTyDigits = Token.range();
2208 if (!VectorTyDigits.consume_front("s") &&
2209 !VectorTyDigits.consume_front("i") &&
2210 !VectorTyDigits.consume_front("f") &&
2211 !VectorTyDigits.consume_front("p") && !VectorTyDigits.consume_front("bf"))
2212 return GetError();
2213
2214 if (VectorTyDigits.empty() || !llvm::all_of(VectorTyDigits, isdigit))
2215 return error(
2216 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2217
2218 Scalar = Token.range().starts_with("s");
2219 if (Scalar || Token.range().starts_with("i")) {
2220 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2221 if (!verifyScalarSize(ScalarSize))
2222 return error("invalid size for scalar element in vector");
2223 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2224 } else if (Token.range().starts_with("p")) {
2225 const DataLayout &DL = MF.getDataLayout();
2226 uint64_t AS = APSInt(VectorTyDigits).getZExtValue();
2227 if (!verifyAddrSpace(AS))
2228 return error("invalid address space number");
2229
2230 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2231 } else if (Token.range().starts_with("f")) {
2232 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2233 if (!verifyScalarSize(ScalarSize))
2234 return error("invalid size for float element in vector");
2235 Ty = LLT::floatIEEE(ScalarSize);
2236 } else if (Token.range().starts_with("bf")) {
2237 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2238 if (!verifyScalarSize(ScalarSize))
2239 return error("invalid size for bfloat element in vector");
2240 Ty = LLT::bfloat16();
2241 } else {
2242 return GetError();
2243 }
2244 lex();
2245
2246 if (Token.isNot(MIToken::greater))
2247 return GetError();
2248
2249 lex();
2250
2251 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2252 return false;
2253}
2254
2255bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2256 assert(Token.is(MIToken::Identifier));
2257 StringRef TypeDigits = Token.range();
2258 if (!TypeDigits.consume_front("i") && !TypeDigits.consume_front("s") &&
2259 !TypeDigits.consume_front("p") && !TypeDigits.consume_front("f") &&
2260 !TypeDigits.consume_front("bf"))
2261 return error("a typed immediate operand should start with one of 'i', "
2262 "'s', 'f', 'bf', or 'p'");
2263 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2264 return error(
2265 "expected integers after 'i'/'s'/'f'/'bf'/'p' type identifier");
2266
2267 auto Loc = Token.location();
2268 lex();
2269 if (Token.isNot(MIToken::IntegerLiteral)) {
2270 if (Token.isNot(MIToken::Identifier) ||
2271 !(Token.range() == "true" || Token.range() == "false"))
2272 return error("expected an integer literal");
2273 }
2274 const Constant *C = nullptr;
2275 if (parseIRConstant(Loc, C))
2276 return true;
2278 return false;
2279}
2280
2281bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2282 auto Loc = Token.location();
2283 lex();
2284 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2285 Token.isNot(MIToken::HexLiteral))
2286 return error("expected a floating point literal");
2287 const Constant *C = nullptr;
2288 if (parseIRConstant(Loc, C))
2289 return true;
2291 return false;
2292}
2293
2294static bool getHexUint(const MIToken &Token, APInt &Result) {
2296 StringRef S = Token.range();
2297 assert(S[0] == '0' && tolower(S[1]) == 'x');
2298 // This could be a floating point literal with a special prefix.
2299 if (!isxdigit(S[2]))
2300 return true;
2301 StringRef V = S.substr(2);
2302 APInt A(V.size()*4, V, 16);
2303
2304 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2305 // sure it isn't the case before constructing result.
2306 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2307 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2308 return false;
2309}
2310
2311static bool getUnsigned(const MIToken &Token, unsigned &Result,
2312 ErrorCallbackType ErrCB) {
2313 if (Token.hasIntegerValue()) {
2314 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2315 const APSInt &SInt = Token.integerValue();
2316 if (SInt.isNegative())
2317 return ErrCB(Token.location(), "expected unsigned integer");
2318 uint64_t Val64 = SInt.getLimitedValue(Limit);
2319 if (Val64 == Limit)
2320 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2321 Result = Val64;
2322 return false;
2323 }
2324 if (Token.is(MIToken::HexLiteral)) {
2325 APInt A;
2326 if (getHexUint(Token, A))
2327 return true;
2328 if (A.getBitWidth() > 32)
2329 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2330 Result = A.getZExtValue();
2331 return false;
2332 }
2333 return true;
2334}
2335
2336bool MIParser::getUnsigned(unsigned &Result) {
2337 return ::getUnsigned(
2338 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2339 return error(Loc, Msg);
2340 });
2341}
2342
2343bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2346 unsigned Number;
2347 if (getUnsigned(Number))
2348 return true;
2349 auto MBBInfo = PFS.MBBSlots.find(Number);
2350 if (MBBInfo == PFS.MBBSlots.end())
2351 return error(Twine("use of undefined machine basic block #") +
2352 Twine(Number));
2353 MBB = MBBInfo->second;
2354 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2355 // we drop the <irname> from the bb.<id>.<irname> format.
2356 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2357 return error(Twine("the name of machine basic block #") + Twine(Number) +
2358 " isn't '" + Token.stringValue() + "'");
2359 return false;
2360}
2361
2362bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2365 return true;
2367 lex();
2368 return false;
2369}
2370
2371bool MIParser::parseStackFrameIndex(int &FI) {
2372 assert(Token.is(MIToken::StackObject));
2373 unsigned ID;
2374 if (getUnsigned(ID))
2375 return true;
2376 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2377 if (ObjectInfo == PFS.StackObjectSlots.end())
2378 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2379 "'");
2381 if (const auto *Alloca =
2382 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2383 Name = Alloca->getName();
2384 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2385 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2386 "' isn't '" + Token.stringValue() + "'");
2387 lex();
2388 FI = ObjectInfo->second;
2389 return false;
2390}
2391
2392bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2393 int FI;
2394 if (parseStackFrameIndex(FI))
2395 return true;
2396 Dest = MachineOperand::CreateFI(FI);
2397 return false;
2398}
2399
2400bool MIParser::parseFixedStackFrameIndex(int &FI) {
2402 unsigned ID;
2403 if (getUnsigned(ID))
2404 return true;
2405 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2406 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2407 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2408 Twine(ID) + "'");
2409 lex();
2410 FI = ObjectInfo->second;
2411 return false;
2412}
2413
2414bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2415 int FI;
2416 if (parseFixedStackFrameIndex(FI))
2417 return true;
2418 Dest = MachineOperand::CreateFI(FI);
2419 return false;
2420}
2421
2422static bool parseGlobalValue(const MIToken &Token,
2424 ErrorCallbackType ErrCB) {
2425 switch (Token.kind()) {
2427 const Module *M = PFS.MF.getFunction().getParent();
2428 GV = M->getNamedValue(Token.stringValue());
2429 if (!GV)
2430 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2431 Token.range() + "'");
2432 break;
2433 }
2434 case MIToken::GlobalValue: {
2435 unsigned GVIdx;
2436 if (getUnsigned(Token, GVIdx, ErrCB))
2437 return true;
2438 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2439 if (!GV)
2440 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2441 Twine(GVIdx) + "'");
2442 break;
2443 }
2444 default:
2445 llvm_unreachable("The current token should be a global value");
2446 }
2447 return false;
2448}
2449
2450bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2451 return ::parseGlobalValue(
2452 Token, PFS, GV,
2453 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2454 return error(Loc, Msg);
2455 });
2456}
2457
2458bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2459 GlobalValue *GV = nullptr;
2460 if (parseGlobalValue(GV))
2461 return true;
2462 lex();
2463 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2464 if (parseOperandsOffset(Dest))
2465 return true;
2466 return false;
2467}
2468
2469bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2471 unsigned ID;
2472 if (getUnsigned(ID))
2473 return true;
2474 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2475 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2476 return error("use of undefined constant '%const." + Twine(ID) + "'");
2477 lex();
2478 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2479 if (parseOperandsOffset(Dest))
2480 return true;
2481 return false;
2482}
2483
2484bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2486 unsigned ID;
2487 if (getUnsigned(ID))
2488 return true;
2489 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2490 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2491 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2492 lex();
2493 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2494 return false;
2495}
2496
2497bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2499 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2500 lex();
2501 Dest = MachineOperand::CreateES(Symbol);
2502 if (parseOperandsOffset(Dest))
2503 return true;
2504 return false;
2505}
2506
2507bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2508 assert(Token.is(MIToken::MCSymbol));
2509 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2510 lex();
2511 Dest = MachineOperand::CreateMCSymbol(Symbol);
2512 if (parseOperandsOffset(Dest))
2513 return true;
2514 return false;
2515}
2516
2517bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2519 StringRef Name = Token.stringValue();
2520 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2521 if (SubRegIndex == 0)
2522 return error(Twine("unknown subregister index '") + Name + "'");
2523 lex();
2524 Dest = MachineOperand::CreateImm(SubRegIndex);
2525 return false;
2526}
2527
2528bool MIParser::parseMDNode(MDNode *&Node) {
2529 assert(Token.is(MIToken::exclaim));
2530
2531 auto Loc = Token.location();
2532 lex();
2533 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2534 return error("expected metadata id after '!'");
2535 unsigned ID;
2536 if (getUnsigned(ID))
2537 return true;
2538 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2539 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2540 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2541 if (NodeInfo == PFS.MachineMetadataNodes.end())
2542 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2543 }
2544 lex();
2545 Node = NodeInfo->second.get();
2546 return false;
2547}
2548
2549bool MIParser::parseDIExpression(MDNode *&Expr) {
2550 unsigned Read;
2552 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2553 &PFS.IRSlots);
2554 CurrentSource = CurrentSource.substr(Read);
2555 lex();
2556 if (!Expr)
2557 return error(Error.getMessage());
2558 return false;
2559}
2560
2561bool MIParser::parseDILocation(MDNode *&Loc) {
2562 assert(Token.is(MIToken::md_dilocation));
2563 lex();
2564
2565 bool HaveLine = false;
2566 unsigned Line = 0;
2567 unsigned Column = 0;
2568 MDNode *Scope = nullptr;
2569 MDNode *InlinedAt = nullptr;
2570 bool ImplicitCode = false;
2571 uint64_t AtomGroup = 0;
2572 uint64_t AtomRank = 0;
2573
2574 if (expectAndConsume(MIToken::lparen))
2575 return true;
2576
2577 if (Token.isNot(MIToken::rparen)) {
2578 do {
2579 if (Token.is(MIToken::Identifier)) {
2580 if (Token.stringValue() == "line") {
2581 lex();
2582 if (expectAndConsume(MIToken::colon))
2583 return true;
2584 if (Token.isNot(MIToken::IntegerLiteral) ||
2585 Token.integerValue().isSigned())
2586 return error("expected unsigned integer");
2587 Line = Token.integerValue().getZExtValue();
2588 HaveLine = true;
2589 lex();
2590 continue;
2591 }
2592 if (Token.stringValue() == "column") {
2593 lex();
2594 if (expectAndConsume(MIToken::colon))
2595 return true;
2596 if (Token.isNot(MIToken::IntegerLiteral) ||
2597 Token.integerValue().isSigned())
2598 return error("expected unsigned integer");
2599 Column = Token.integerValue().getZExtValue();
2600 lex();
2601 continue;
2602 }
2603 if (Token.stringValue() == "scope") {
2604 lex();
2605 if (expectAndConsume(MIToken::colon))
2606 return true;
2607 if (parseMDNode(Scope))
2608 return error("expected metadata node");
2609 if (!isa<DIScope>(Scope))
2610 return error("expected DIScope node");
2611 continue;
2612 }
2613 if (Token.stringValue() == "inlinedAt") {
2614 lex();
2615 if (expectAndConsume(MIToken::colon))
2616 return true;
2617 if (Token.is(MIToken::exclaim)) {
2618 if (parseMDNode(InlinedAt))
2619 return true;
2620 } else if (Token.is(MIToken::md_dilocation)) {
2621 if (parseDILocation(InlinedAt))
2622 return true;
2623 } else {
2624 return error("expected metadata node");
2625 }
2626 if (!isa<DILocation>(InlinedAt))
2627 return error("expected DILocation node");
2628 continue;
2629 }
2630 if (Token.stringValue() == "isImplicitCode") {
2631 lex();
2632 if (expectAndConsume(MIToken::colon))
2633 return true;
2634 if (!Token.is(MIToken::Identifier))
2635 return error("expected true/false");
2636 // As far as I can see, we don't have any existing need for parsing
2637 // true/false in MIR yet. Do it ad-hoc until there's something else
2638 // that needs it.
2639 if (Token.stringValue() == "true")
2640 ImplicitCode = true;
2641 else if (Token.stringValue() == "false")
2642 ImplicitCode = false;
2643 else
2644 return error("expected true/false");
2645 lex();
2646 continue;
2647 }
2648 if (Token.stringValue() == "atomGroup") {
2649 lex();
2650 if (expectAndConsume(MIToken::colon))
2651 return true;
2652 if (Token.isNot(MIToken::IntegerLiteral) ||
2653 Token.integerValue().isSigned())
2654 return error("expected unsigned integer");
2655 AtomGroup = Token.integerValue().getZExtValue();
2656 lex();
2657 continue;
2658 }
2659 if (Token.stringValue() == "atomRank") {
2660 lex();
2661 if (expectAndConsume(MIToken::colon))
2662 return true;
2663 if (Token.isNot(MIToken::IntegerLiteral) ||
2664 Token.integerValue().isSigned())
2665 return error("expected unsigned integer");
2666 AtomRank = Token.integerValue().getZExtValue();
2667 lex();
2668 continue;
2669 }
2670 }
2671 return error(Twine("invalid DILocation argument '") +
2672 Token.stringValue() + "'");
2673 } while (consumeIfPresent(MIToken::comma));
2674 }
2675
2676 if (expectAndConsume(MIToken::rparen))
2677 return true;
2678
2679 if (!HaveLine)
2680 return error("DILocation requires line number");
2681 if (!Scope)
2682 return error("DILocation requires a scope");
2683
2684 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2685 InlinedAt, ImplicitCode, AtomGroup, AtomRank);
2686 return false;
2687}
2688
2689bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2690 MDNode *Node = nullptr;
2691 if (Token.is(MIToken::exclaim)) {
2692 if (parseMDNode(Node))
2693 return true;
2694 } else if (Token.is(MIToken::md_diexpr)) {
2695 if (parseDIExpression(Node))
2696 return true;
2697 }
2698 Dest = MachineOperand::CreateMetadata(Node);
2699 return false;
2700}
2701
2702bool MIParser::parseCFIOffset(int &Offset) {
2703 if (Token.isNot(MIToken::IntegerLiteral))
2704 return error("expected a cfi offset");
2705 if (Token.integerValue().getSignificantBits() > 32)
2706 return error("expected a 32 bit integer (the cfi offset is too large)");
2707 Offset = (int)Token.integerValue().getExtValue();
2708 lex();
2709 return false;
2710}
2711
2712bool MIParser::parseCFIUnsigned(unsigned &Value) {
2713 if (getUnsigned(Value))
2714 return true;
2715 lex();
2716 return false;
2717}
2718
2719bool MIParser::parseCFIRegister(unsigned &Reg) {
2720 if (Token.isNot(MIToken::NamedRegister))
2721 return error("expected a cfi register");
2722 Register LLVMReg;
2723 if (parseNamedRegister(LLVMReg))
2724 return true;
2725 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2726 assert(TRI && "Expected target register info");
2727 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2728 if (DwarfReg < 0)
2729 return error("invalid DWARF register");
2730 Reg = (unsigned)DwarfReg;
2731 lex();
2732 return false;
2733}
2734
2735bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2736 if (Token.isNot(MIToken::IntegerLiteral))
2737 return error("expected a cfi address space literal");
2738 if (Token.integerValue().isSigned())
2739 return error("expected an unsigned integer (cfi address space)");
2740 AddressSpace = Token.integerValue().getZExtValue();
2741 lex();
2742 return false;
2743}
2744
2745bool MIParser::parseCFIEscapeValues(std::string &Values) {
2746 do {
2747 if (Token.isNot(MIToken::HexLiteral))
2748 return error("expected a hexadecimal literal");
2749 unsigned Value;
2750 if (getUnsigned(Value))
2751 return true;
2752 if (Value > UINT8_MAX)
2753 return error("expected a 8-bit integer (too large)");
2754 Values.push_back(static_cast<uint8_t>(Value));
2755 lex();
2756 } while (consumeIfPresent(MIToken::comma));
2757 return false;
2758}
2759
2760bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2761 auto Kind = Token.kind();
2762 lex();
2763 int Offset;
2764 unsigned Reg;
2765 unsigned AddressSpace;
2766 unsigned CFIIndex;
2767 switch (Kind) {
2769 if (parseCFIRegister(Reg))
2770 return true;
2771 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2772 break;
2774 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2775 parseCFIOffset(Offset))
2776 return true;
2777 CFIIndex =
2778 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2779 break;
2781 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2782 parseCFIOffset(Offset))
2783 return true;
2784 CFIIndex = MF.addFrameInst(
2786 break;
2788 if (parseCFIRegister(Reg))
2789 return true;
2790 CFIIndex =
2791 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2792 break;
2794 if (parseCFIOffset(Offset))
2795 return true;
2796 CFIIndex =
2797 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2798 break;
2800 if (parseCFIOffset(Offset))
2801 return true;
2802 CFIIndex = MF.addFrameInst(
2804 break;
2806 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2807 parseCFIOffset(Offset))
2808 return true;
2809 CFIIndex =
2810 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2811 break;
2813 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2814 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2815 parseCFIAddressSpace(AddressSpace))
2816 return true;
2817 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2818 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2819 break;
2821 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2822 break;
2824 if (parseCFIRegister(Reg))
2825 return true;
2826 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2827 break;
2829 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2830 break;
2832 if (parseCFIRegister(Reg))
2833 return true;
2834 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2835 break;
2837 unsigned Reg2;
2838 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2839 parseCFIRegister(Reg2))
2840 return true;
2841
2842 CFIIndex =
2843 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2844 break;
2845 }
2847 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2848 break;
2850 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2851 break;
2853 CFIIndex =
2854 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2855 break;
2857 unsigned Reg, R1, R2;
2858 unsigned R1Size, R2Size;
2859 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2860 parseCFIRegister(R1) || expectAndConsume(MIToken::comma) ||
2861 parseCFIUnsigned(R1Size) || expectAndConsume(MIToken::comma) ||
2862 parseCFIRegister(R2) || expectAndConsume(MIToken::comma) ||
2863 parseCFIUnsigned(R2Size))
2864 return true;
2865
2866 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMRegisterPair(
2867 nullptr, Reg, R1, R1Size, R2, R2Size));
2868 break;
2869 }
2871 std::vector<MCCFIInstruction::VectorRegisterWithLane> VectorRegisters;
2872 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma))
2873 return true;
2874 do {
2875 unsigned VR;
2876 unsigned Lane, Size;
2877 if (parseCFIRegister(VR) || expectAndConsume(MIToken::comma) ||
2878 parseCFIUnsigned(Lane) || expectAndConsume(MIToken::comma) ||
2879 parseCFIUnsigned(Size))
2880 return true;
2881 VectorRegisters.push_back({VR, Lane, Size});
2882 } while (consumeIfPresent(MIToken::comma));
2883
2884 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisters(
2885 nullptr, Reg, std::move(VectorRegisters)));
2886 break;
2887 }
2889 unsigned Reg, MaskReg;
2890 unsigned RegSize, MaskRegSize;
2891 int Offset = 0;
2892
2893 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2894 parseCFIUnsigned(RegSize) || expectAndConsume(MIToken::comma) ||
2895 parseCFIRegister(MaskReg) || expectAndConsume(MIToken::comma) ||
2896 parseCFIUnsigned(MaskRegSize) || expectAndConsume(MIToken::comma) ||
2897 parseCFIOffset(Offset))
2898 return true;
2899
2900 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorOffset(
2901 nullptr, Reg, RegSize, MaskReg, MaskRegSize, Offset));
2902 break;
2903 }
2905 unsigned Reg, SpillReg, MaskReg;
2906 unsigned SpillRegLaneSize, MaskRegSize;
2907
2908 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2909 parseCFIRegister(SpillReg) || expectAndConsume(MIToken::comma) ||
2910 parseCFIUnsigned(SpillRegLaneSize) ||
2911 expectAndConsume(MIToken::comma) || parseCFIRegister(MaskReg) ||
2912 expectAndConsume(MIToken::comma) || parseCFIUnsigned(MaskRegSize))
2913 return true;
2914
2915 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisterMask(
2916 nullptr, Reg, SpillReg, SpillRegLaneSize, MaskReg, MaskRegSize));
2917 break;
2918 }
2920 std::string Values;
2921 if (parseCFIEscapeValues(Values))
2922 return true;
2923 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2924 break;
2925 }
2926 default:
2927 // TODO: Parse the other CFI operands.
2928 llvm_unreachable("The current token should be a cfi operand");
2929 }
2930 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2931 return false;
2932}
2933
2934bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2935 switch (Token.kind()) {
2936 case MIToken::NamedIRBlock: {
2938 F.getValueSymbolTable()->lookup(Token.stringValue()));
2939 if (!BB)
2940 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2941 break;
2942 }
2943 case MIToken::IRBlock: {
2944 unsigned SlotNumber = 0;
2945 if (getUnsigned(SlotNumber))
2946 return true;
2947 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2948 if (!BB)
2949 return error(Twine("use of undefined IR block '%ir-block.") +
2950 Twine(SlotNumber) + "'");
2951 break;
2952 }
2953 default:
2954 llvm_unreachable("The current token should be an IR block reference");
2955 }
2956 return false;
2957}
2958
2959bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2961 lex();
2962 if (expectAndConsume(MIToken::lparen))
2963 return true;
2964 if (Token.isNot(MIToken::GlobalValue) &&
2965 Token.isNot(MIToken::NamedGlobalValue))
2966 return error("expected a global value");
2967 GlobalValue *GV = nullptr;
2968 if (parseGlobalValue(GV))
2969 return true;
2970 auto *F = dyn_cast<Function>(GV);
2971 if (!F)
2972 return error("expected an IR function reference");
2973 lex();
2974 if (expectAndConsume(MIToken::comma))
2975 return true;
2976 BasicBlock *BB = nullptr;
2977 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2978 return error("expected an IR block reference");
2979 if (parseIRBlock(BB, *F))
2980 return true;
2981 lex();
2982 if (expectAndConsume(MIToken::rparen))
2983 return true;
2984 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2985 if (parseOperandsOffset(Dest))
2986 return true;
2987 return false;
2988}
2989
2990bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2991 assert(Token.is(MIToken::kw_intrinsic));
2992 lex();
2993 if (expectAndConsume(MIToken::lparen))
2994 return error("expected syntax intrinsic(@llvm.whatever)");
2995
2996 if (Token.isNot(MIToken::NamedGlobalValue))
2997 return error("expected syntax intrinsic(@llvm.whatever)");
2998
2999 std::string Name = std::string(Token.stringValue());
3000 lex();
3001
3002 if (expectAndConsume(MIToken::rparen))
3003 return error("expected ')' to terminate intrinsic name");
3004
3005 // Find out what intrinsic we're dealing with.
3008 return error("unknown intrinsic name");
3010
3011 return false;
3012}
3013
3014bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
3015 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
3016 bool IsFloat = Token.is(MIToken::kw_floatpred);
3017 lex();
3018
3019 if (expectAndConsume(MIToken::lparen))
3020 return error("expected syntax intpred(whatever) or floatpred(whatever");
3021
3022 if (Token.isNot(MIToken::Identifier))
3023 return error("whatever");
3024
3025 CmpInst::Predicate Pred;
3026 if (IsFloat) {
3027 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
3028 .Case("false", CmpInst::FCMP_FALSE)
3029 .Case("oeq", CmpInst::FCMP_OEQ)
3030 .Case("ogt", CmpInst::FCMP_OGT)
3031 .Case("oge", CmpInst::FCMP_OGE)
3032 .Case("olt", CmpInst::FCMP_OLT)
3033 .Case("ole", CmpInst::FCMP_OLE)
3034 .Case("one", CmpInst::FCMP_ONE)
3035 .Case("ord", CmpInst::FCMP_ORD)
3036 .Case("uno", CmpInst::FCMP_UNO)
3037 .Case("ueq", CmpInst::FCMP_UEQ)
3038 .Case("ugt", CmpInst::FCMP_UGT)
3039 .Case("uge", CmpInst::FCMP_UGE)
3040 .Case("ult", CmpInst::FCMP_ULT)
3041 .Case("ule", CmpInst::FCMP_ULE)
3042 .Case("une", CmpInst::FCMP_UNE)
3043 .Case("true", CmpInst::FCMP_TRUE)
3045 if (!CmpInst::isFPPredicate(Pred))
3046 return error("invalid floating-point predicate");
3047 } else {
3048 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
3049 .Case("eq", CmpInst::ICMP_EQ)
3050 .Case("ne", CmpInst::ICMP_NE)
3051 .Case("sgt", CmpInst::ICMP_SGT)
3052 .Case("sge", CmpInst::ICMP_SGE)
3053 .Case("slt", CmpInst::ICMP_SLT)
3054 .Case("sle", CmpInst::ICMP_SLE)
3055 .Case("ugt", CmpInst::ICMP_UGT)
3056 .Case("uge", CmpInst::ICMP_UGE)
3057 .Case("ult", CmpInst::ICMP_ULT)
3058 .Case("ule", CmpInst::ICMP_ULE)
3060 if (!CmpInst::isIntPredicate(Pred))
3061 return error("invalid integer predicate");
3062 }
3063
3064 lex();
3066 if (expectAndConsume(MIToken::rparen))
3067 return error("predicate should be terminated by ')'.");
3068
3069 return false;
3070}
3071
3072bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
3074
3075 lex();
3076 if (expectAndConsume(MIToken::lparen))
3077 return error("expected syntax shufflemask(<integer or undef>, ...)");
3078
3079 SmallVector<int, 32> ShufMask;
3080 do {
3081 if (Token.is(MIToken::kw_undef)) {
3082 ShufMask.push_back(-1);
3083 } else if (Token.is(MIToken::IntegerLiteral)) {
3084 const APSInt &Int = Token.integerValue();
3085 ShufMask.push_back(Int.getExtValue());
3086 } else {
3087 return error("expected integer constant");
3088 }
3089
3090 lex();
3091 } while (consumeIfPresent(MIToken::comma));
3092
3093 if (expectAndConsume(MIToken::rparen))
3094 return error("shufflemask should be terminated by ')'.");
3095
3096 if (ShufMask.size() < 2)
3097 return error("shufflemask should have > 1 element");
3098
3099 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
3100 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
3101 return false;
3102}
3103
3104bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
3106
3107 lex();
3108 if (expectAndConsume(MIToken::lparen))
3109 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3110
3111 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3112 return error("expected unsigned integer for instruction index");
3113 uint64_t InstrIdx = Token.integerValue().getZExtValue();
3114 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
3115 "Instruction reference's instruction index is too large");
3116 lex();
3117
3118 if (expectAndConsume(MIToken::comma))
3119 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3120
3121 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3122 return error("expected unsigned integer for operand index");
3123 uint64_t OpIdx = Token.integerValue().getZExtValue();
3124 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
3125 "Instruction reference's operand index is too large");
3126 lex();
3127
3128 if (expectAndConsume(MIToken::rparen))
3129 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3130
3131 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
3132 return false;
3133}
3134
3135bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
3137 lex();
3138 if (expectAndConsume(MIToken::lparen))
3139 return true;
3140 if (Token.isNot(MIToken::Identifier))
3141 return error("expected the name of the target index");
3142 int Index = 0;
3143 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
3144 return error("use of undefined target index '" + Token.stringValue() + "'");
3145 lex();
3146 if (expectAndConsume(MIToken::rparen))
3147 return true;
3148 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
3149 if (parseOperandsOffset(Dest))
3150 return true;
3151 return false;
3152}
3153
3154bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
3155 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
3156 lex();
3157 if (expectAndConsume(MIToken::lparen))
3158 return true;
3159
3160 uint32_t *Mask = MF.allocateRegMask();
3161 do {
3162 if (Token.isNot(MIToken::rparen)) {
3163 if (Token.isNot(MIToken::NamedRegister))
3164 return error("expected a named register");
3165 Register Reg;
3166 if (parseNamedRegister(Reg))
3167 return true;
3168 lex();
3169 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3170 }
3171
3172 // TODO: Report an error if the same register is used more than once.
3173 } while (consumeIfPresent(MIToken::comma));
3174
3175 if (expectAndConsume(MIToken::rparen))
3176 return true;
3177 Dest = MachineOperand::CreateRegMask(Mask);
3178 return false;
3179}
3180
3181bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
3182 assert(Token.is(MIToken::kw_lanemask));
3183
3184 lex();
3185 if (expectAndConsume(MIToken::lparen))
3186 return true;
3187
3188 // Parse lanemask.
3189 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
3190 return error("expected a valid lane mask value");
3191 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
3192 "Use correct get-function for lane mask.");
3194 if (getUint64(V))
3195 return true;
3196 LaneBitmask LaneMask(V);
3197 lex();
3198
3199 if (expectAndConsume(MIToken::rparen))
3200 return true;
3201
3202 Dest = MachineOperand::CreateLaneMask(LaneMask);
3203 return false;
3204}
3205
3206bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
3207 assert(Token.is(MIToken::kw_liveout));
3208 uint32_t *Mask = MF.allocateRegMask();
3209 lex();
3210 if (expectAndConsume(MIToken::lparen))
3211 return true;
3212 while (true) {
3213 if (Token.isNot(MIToken::NamedRegister))
3214 return error("expected a named register");
3215 Register Reg;
3216 if (parseNamedRegister(Reg))
3217 return true;
3218 lex();
3219 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3220 // TODO: Report an error if the same register is used more than once.
3221 if (Token.isNot(MIToken::comma))
3222 break;
3223 lex();
3224 }
3225 if (expectAndConsume(MIToken::rparen))
3226 return true;
3228 return false;
3229}
3230
3231bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
3232 MachineOperand &Dest,
3233 std::optional<unsigned> &TiedDefIdx) {
3234 switch (Token.kind()) {
3237 case MIToken::kw_def:
3238 case MIToken::kw_dead:
3239 case MIToken::kw_killed:
3240 case MIToken::kw_undef:
3249 return parseRegisterOperand(Dest, TiedDefIdx);
3251 // TODO: Forbid numeric operands for INLINEASM once the transition to the
3252 // symbolic form is over.
3253 return parseImmediateOperand(Dest);
3254 case MIToken::kw_half:
3255 case MIToken::kw_bfloat:
3256 case MIToken::kw_float:
3257 case MIToken::kw_double:
3259 case MIToken::kw_fp128:
3261 return parseFPImmediateOperand(Dest);
3263 return parseMBBOperand(Dest);
3265 return parseStackObjectOperand(Dest);
3267 return parseFixedStackObjectOperand(Dest);
3270 return parseGlobalAddressOperand(Dest);
3272 return parseConstantPoolIndexOperand(Dest);
3274 return parseJumpTableIndexOperand(Dest);
3276 return parseExternalSymbolOperand(Dest);
3277 case MIToken::MCSymbol:
3278 return parseMCSymbolOperand(Dest);
3280 return parseSubRegisterIndexOperand(Dest);
3281 case MIToken::md_diexpr:
3282 case MIToken::exclaim:
3283 return parseMetadataOperand(Dest);
3305 return parseCFIOperand(Dest);
3307 return parseBlockAddressOperand(Dest);
3309 return parseIntrinsicOperand(Dest);
3311 return parseTargetIndexOperand(Dest);
3313 return parseLaneMaskOperand(Dest);
3315 return parseLiveoutRegisterMaskOperand(Dest);
3318 return parsePredicateOperand(Dest);
3320 return parseShuffleMaskOperand(Dest);
3322 return parseDbgInstrRefOperand(Dest);
3323 case MIToken::Error:
3324 return true;
3325 case MIToken::Identifier: {
3326 bool IsInlineAsm = OpCode == TargetOpcode::INLINEASM ||
3327 OpCode == TargetOpcode::INLINEASM_BR;
3328 if (IsInlineAsm)
3329 return parseSymbolicInlineAsmOperand(OpIdx, Dest);
3330
3331 StringRef Id = Token.stringValue();
3332 if (const auto *RegMask = PFS.Target.getRegMask(Id)) {
3333 Dest = MachineOperand::CreateRegMask(RegMask);
3334 lex();
3335 break;
3336 } else if (Id == "CustomRegMask") {
3337 return parseCustomRegisterMaskOperand(Dest);
3338 } else {
3339 return parseTypedImmediateOperand(Dest);
3340 }
3341 }
3342 case MIToken::dot: {
3343 const auto *TII = MF.getSubtarget().getInstrInfo();
3344 if (const auto *Formatter = TII->getMIRFormatter()) {
3345 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3346 }
3347 [[fallthrough]];
3348 }
3349 default:
3350 // FIXME: Parse the MCSymbol machine operand.
3351 return error("expected a machine operand");
3352 }
3353 return false;
3354}
3355
3356bool MIParser::parseMachineOperandAndTargetFlags(
3357 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3358 std::optional<unsigned> &TiedDefIdx) {
3359 unsigned TF = 0;
3360 bool HasTargetFlags = false;
3361 if (Token.is(MIToken::kw_target_flags)) {
3362 HasTargetFlags = true;
3363 lex();
3364 if (expectAndConsume(MIToken::lparen))
3365 return true;
3366 if (Token.isNot(MIToken::Identifier))
3367 return error("expected the name of the target flag");
3368 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3369 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3370 return error("use of undefined target flag '" + Token.stringValue() +
3371 "'");
3372 }
3373 lex();
3374 while (Token.is(MIToken::comma)) {
3375 lex();
3376 if (Token.isNot(MIToken::Identifier))
3377 return error("expected the name of the target flag");
3378 unsigned BitFlag = 0;
3379 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3380 return error("use of undefined target flag '" + Token.stringValue() +
3381 "'");
3382 // TODO: Report an error when using a duplicate bit target flag.
3383 TF |= BitFlag;
3384 lex();
3385 }
3386 if (expectAndConsume(MIToken::rparen))
3387 return true;
3388 }
3389 auto Loc = Token.location();
3390 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3391 return true;
3392 if (!HasTargetFlags)
3393 return false;
3394 if (Dest.isReg())
3395 return error(Loc, "register operands can't have target flags");
3396 Dest.setTargetFlags(TF);
3397 return false;
3398}
3399
3400bool MIParser::parseOffset(int64_t &Offset) {
3401 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3402 return false;
3403 StringRef Sign = Token.range();
3404 bool IsNegative = Token.is(MIToken::minus);
3405 lex();
3406 if (Token.isNot(MIToken::IntegerLiteral))
3407 return error("expected an integer literal after '" + Sign + "'");
3408 if (Token.integerValue().getSignificantBits() > 64)
3409 return error("expected 64-bit integer (too large)");
3410 Offset = Token.integerValue().getExtValue();
3411 if (IsNegative)
3412 Offset = -Offset;
3413 lex();
3414 return false;
3415}
3416
3417bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3419 lex();
3420 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3421 return error("expected basic block after 'ir_block_address_taken'");
3422
3423 if (parseIRBlock(BB, MF.getFunction()))
3424 return true;
3425
3426 lex();
3427 return false;
3428}
3429
3430bool MIParser::parseAlignment(uint64_t &Alignment) {
3431 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3432 lex();
3433 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3434 return error("expected an integer literal after 'align'");
3435 if (getUint64(Alignment))
3436 return true;
3437 lex();
3438
3439 if (!isPowerOf2_64(Alignment))
3440 return error("expected a power-of-2 literal after 'align'");
3441
3442 return false;
3443}
3444
3445bool MIParser::parseAddrspace(unsigned &Addrspace) {
3446 assert(Token.is(MIToken::kw_addrspace));
3447 lex();
3448 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3449 return error("expected an integer literal after 'addrspace'");
3450 if (getUnsigned(Addrspace))
3451 return true;
3452 lex();
3453 return false;
3454}
3455
3456bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3457 int64_t Offset = 0;
3458 if (parseOffset(Offset))
3459 return true;
3460 Op.setOffset(Offset);
3461 return false;
3462}
3463
3464static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3465 const Value *&V, ErrorCallbackType ErrCB) {
3466 switch (Token.kind()) {
3467 case MIToken::NamedIRValue: {
3468 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3469 break;
3470 }
3471 case MIToken::IRValue: {
3472 unsigned SlotNumber = 0;
3473 if (getUnsigned(Token, SlotNumber, ErrCB))
3474 return true;
3475 V = PFS.getIRValue(SlotNumber);
3476 break;
3477 }
3479 case MIToken::GlobalValue: {
3480 GlobalValue *GV = nullptr;
3481 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3482 return true;
3483 V = GV;
3484 break;
3485 }
3487 const Constant *C = nullptr;
3488 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3489 return true;
3490 V = C;
3491 break;
3492 }
3494 V = nullptr;
3495 return false;
3496 default:
3497 llvm_unreachable("The current token should be an IR block reference");
3498 }
3499 if (!V)
3500 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3501 return false;
3502}
3503
3504bool MIParser::parseIRValue(const Value *&V) {
3505 return ::parseIRValue(
3506 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3507 return error(Loc, Msg);
3508 });
3509}
3510
3511bool MIParser::getUint64(uint64_t &Result) {
3512 if (Token.hasIntegerValue()) {
3513 if (Token.integerValue().getActiveBits() > 64)
3514 return error("expected 64-bit integer (too large)");
3515 Result = Token.integerValue().getZExtValue();
3516 return false;
3517 }
3518 if (Token.is(MIToken::HexLiteral)) {
3519 APInt A;
3520 if (getHexUint(A))
3521 return true;
3522 if (A.getBitWidth() > 64)
3523 return error("expected 64-bit integer (too large)");
3524 Result = A.getZExtValue();
3525 return false;
3526 }
3527 return true;
3528}
3529
3530bool MIParser::getHexUint(APInt &Result) {
3531 return ::getHexUint(Token, Result);
3532}
3533
3534bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3535 const auto OldFlags = Flags;
3536 switch (Token.kind()) {
3539 break;
3542 break;
3545 break;
3548 break;
3551 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3552 return error("use of undefined target MMO flag '" + Token.stringValue() +
3553 "'");
3554 Flags |= TF;
3555 break;
3556 }
3557 default:
3558 llvm_unreachable("The current token should be a memory operand flag");
3559 }
3560 if (OldFlags == Flags)
3561 // We know that the same flag is specified more than once when the flags
3562 // weren't modified.
3563 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3564 lex();
3565 return false;
3566}
3567
3568bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3569 switch (Token.kind()) {
3570 case MIToken::kw_stack:
3571 PSV = MF.getPSVManager().getStack();
3572 break;
3573 case MIToken::kw_got:
3574 PSV = MF.getPSVManager().getGOT();
3575 break;
3577 PSV = MF.getPSVManager().getJumpTable();
3578 break;
3580 PSV = MF.getPSVManager().getConstantPool();
3581 break;
3583 int FI;
3584 if (parseFixedStackFrameIndex(FI))
3585 return true;
3586 PSV = MF.getPSVManager().getFixedStack(FI);
3587 // The token was already consumed, so use return here instead of break.
3588 return false;
3589 }
3590 case MIToken::StackObject: {
3591 int FI;
3592 if (parseStackFrameIndex(FI))
3593 return true;
3594 PSV = MF.getPSVManager().getFixedStack(FI);
3595 // The token was already consumed, so use return here instead of break.
3596 return false;
3597 }
3599 lex();
3600 switch (Token.kind()) {
3603 GlobalValue *GV = nullptr;
3604 if (parseGlobalValue(GV))
3605 return true;
3606 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3607 break;
3608 }
3610 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3611 MF.createExternalSymbolName(Token.stringValue()));
3612 break;
3613 default:
3614 return error(
3615 "expected a global value or an external symbol after 'call-entry'");
3616 }
3617 break;
3618 case MIToken::kw_custom: {
3619 lex();
3620 const auto *TII = MF.getSubtarget().getInstrInfo();
3621 if (const auto *Formatter = TII->getMIRFormatter()) {
3622 if (Formatter->parseCustomPseudoSourceValue(
3623 Token.stringValue(), MF, PFS, PSV,
3624 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3625 return error(Loc, Msg);
3626 }))
3627 return true;
3628 } else {
3629 return error("unable to parse target custom pseudo source value");
3630 }
3631 break;
3632 }
3633 default:
3634 llvm_unreachable("The current token should be pseudo source value");
3635 }
3636 lex();
3637 return false;
3638}
3639
3640bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3641 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3642 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3643 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3644 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3645 const PseudoSourceValue *PSV = nullptr;
3646 if (parseMemoryPseudoSourceValue(PSV))
3647 return true;
3648 int64_t Offset = 0;
3649 if (parseOffset(Offset))
3650 return true;
3651 Dest = MachinePointerInfo(PSV, Offset);
3652 return false;
3653 }
3654 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3655 Token.isNot(MIToken::GlobalValue) &&
3656 Token.isNot(MIToken::NamedGlobalValue) &&
3657 Token.isNot(MIToken::QuotedIRValue) &&
3658 Token.isNot(MIToken::kw_unknown_address))
3659 return error("expected an IR value reference");
3660 const Value *V = nullptr;
3661 if (parseIRValue(V))
3662 return true;
3663 if (V && !V->getType()->isPointerTy())
3664 return error("expected a pointer IR value");
3665 lex();
3666 int64_t Offset = 0;
3667 if (parseOffset(Offset))
3668 return true;
3669 Dest = MachinePointerInfo(V, Offset);
3670 return false;
3671}
3672
3673bool MIParser::parseOptionalScope(LLVMContext &Context,
3674 SyncScope::ID &SSID) {
3675 SSID = SyncScope::System;
3676 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3677 lex();
3678 if (expectAndConsume(MIToken::lparen))
3679 return error("expected '(' in syncscope");
3680
3681 std::string SSN;
3682 if (parseStringConstant(SSN))
3683 return true;
3684
3685 SSID = Context.getOrInsertSyncScopeID(SSN);
3686 if (expectAndConsume(MIToken::rparen))
3687 return error("expected ')' in syncscope");
3688 }
3689
3690 return false;
3691}
3692
3693bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3695 if (Token.isNot(MIToken::Identifier))
3696 return false;
3697
3698 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3699 .Case("unordered", AtomicOrdering::Unordered)
3700 .Case("monotonic", AtomicOrdering::Monotonic)
3701 .Case("acquire", AtomicOrdering::Acquire)
3702 .Case("release", AtomicOrdering::Release)
3706
3707 if (Order != AtomicOrdering::NotAtomic) {
3708 lex();
3709 return false;
3710 }
3711
3712 return error("expected an atomic scope, ordering or a size specification");
3713}
3714
3715bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3716 if (expectAndConsume(MIToken::lparen))
3717 return true;
3719 while (Token.isMemoryOperandFlag()) {
3720 if (parseMemoryOperandFlag(Flags))
3721 return true;
3722 }
3723 if (Token.isNot(MIToken::Identifier) ||
3724 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3725 return error("expected 'load' or 'store' memory operation");
3726 if (Token.stringValue() == "load")
3728 else
3730 lex();
3731
3732 // Optional 'store' for operands that both load and store.
3733 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3735 lex();
3736 }
3737
3738 // Optional synchronization scope.
3739 SyncScope::ID SSID;
3740 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3741 return true;
3742
3743 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3744 AtomicOrdering Order, FailureOrder;
3745 if (parseOptionalAtomicOrdering(Order))
3746 return true;
3747
3748 if (parseOptionalAtomicOrdering(FailureOrder))
3749 return true;
3750
3751 if (Token.isNot(MIToken::IntegerLiteral) &&
3752 Token.isNot(MIToken::kw_unknown_size) &&
3753 Token.isNot(MIToken::lparen))
3754 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3755 "memory operation");
3756
3758 if (Token.is(MIToken::IntegerLiteral)) {
3759 uint64_t Size;
3760 if (getUint64(Size))
3761 return true;
3762
3763 // Convert from bytes to bits for storage.
3765 lex();
3766 } else if (Token.is(MIToken::kw_unknown_size)) {
3767 lex();
3768 } else {
3769 if (expectAndConsume(MIToken::lparen))
3770 return true;
3771 if (parseLowLevelType(Token.location(), MemoryType))
3772 return true;
3773 if (expectAndConsume(MIToken::rparen))
3774 return true;
3775 }
3776
3778 if (Token.is(MIToken::Identifier)) {
3779 const char *Word =
3782 ? "on"
3783 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3784 if (Token.stringValue() != Word)
3785 return error(Twine("expected '") + Word + "'");
3786 lex();
3787
3788 if (parseMachinePointerInfo(Ptr))
3789 return true;
3790 }
3791 uint64_t BaseAlignment =
3792 MemoryType.isValid()
3793 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3794 : 1;
3795 AAMDNodes AAInfo;
3796 MDNode *Range = nullptr;
3797 while (consumeIfPresent(MIToken::comma)) {
3798 switch (Token.kind()) {
3799 case MIToken::kw_align: {
3800 // align is printed if it is different than size.
3801 uint64_t Alignment;
3802 if (parseAlignment(Alignment))
3803 return true;
3804 if (Ptr.Offset & (Alignment - 1)) {
3805 // MachineMemOperand::getAlign never returns a value greater than the
3806 // alignment of offset, so this just guards against hand-written MIR
3807 // that specifies a large "align" value when it should probably use
3808 // "basealign" instead.
3809 return error("specified alignment is more aligned than offset");
3810 }
3811 BaseAlignment = Alignment;
3812 break;
3813 }
3815 // basealign is printed if it is different than align.
3816 if (parseAlignment(BaseAlignment))
3817 return true;
3818 break;
3820 if (parseAddrspace(Ptr.AddrSpace))
3821 return true;
3822 break;
3823 case MIToken::md_tbaa:
3824 lex();
3825 if (parseMDNode(AAInfo.TBAA))
3826 return true;
3827 break;
3829 lex();
3830 if (parseMDNode(AAInfo.Scope))
3831 return true;
3832 break;
3834 lex();
3835 if (parseMDNode(AAInfo.NoAlias))
3836 return true;
3837 break;
3839 lex();
3840 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3841 return true;
3842 break;
3843 case MIToken::md_range:
3844 lex();
3845 if (parseMDNode(Range))
3846 return true;
3847 break;
3848 // TODO: Report an error on duplicate metadata nodes.
3849 default:
3850 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3851 "'!noalias' or '!range' or '!noalias.addrspace'");
3852 }
3853 }
3854 if (expectAndConsume(MIToken::rparen))
3855 return true;
3856 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3857 AAInfo, Range, SSID, Order, FailureOrder);
3858 return false;
3859}
3860
3861bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3863 Token.is(MIToken::kw_post_instr_symbol)) &&
3864 "Invalid token for a pre- post-instruction symbol!");
3865 lex();
3866 if (Token.isNot(MIToken::MCSymbol))
3867 return error("expected a symbol after 'pre-instr-symbol'");
3868 Symbol = getOrCreateMCSymbol(Token.stringValue());
3869 lex();
3870 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3871 Token.is(MIToken::lbrace))
3872 return false;
3873 if (Token.isNot(MIToken::comma))
3874 return error("expected ',' before the next machine operand");
3875 lex();
3876 return false;
3877}
3878
3879bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3881 "Invalid token for a heap alloc marker!");
3882 lex();
3883 if (parseMDNode(Node))
3884 return true;
3885 if (!Node)
3886 return error("expected a MDNode after 'heap-alloc-marker'");
3887 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3888 Token.is(MIToken::lbrace))
3889 return false;
3890 if (Token.isNot(MIToken::comma))
3891 return error("expected ',' before the next machine operand");
3892 lex();
3893 return false;
3894}
3895
3896bool MIParser::parsePCSections(MDNode *&Node) {
3897 assert(Token.is(MIToken::kw_pcsections) &&
3898 "Invalid token for a PC sections!");
3899 lex();
3900 if (parseMDNode(Node))
3901 return true;
3902 if (!Node)
3903 return error("expected a MDNode after 'pcsections'");
3904 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3905 Token.is(MIToken::lbrace))
3906 return false;
3907 if (Token.isNot(MIToken::comma))
3908 return error("expected ',' before the next machine operand");
3909 lex();
3910 return false;
3911}
3912
3913bool MIParser::parseMMRA(MDNode *&Node) {
3914 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3915 lex();
3916 if (parseMDNode(Node))
3917 return true;
3918 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3919 Token.is(MIToken::lbrace))
3920 return false;
3921 if (Token.isNot(MIToken::comma))
3922 return error("expected ',' before the next machine operand");
3923 lex();
3924 return false;
3925}
3926
3928 const Function &F,
3929 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3930 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
3932 for (const auto &BB : F) {
3933 if (BB.hasName())
3934 continue;
3935 int Slot = MST.getLocalSlot(&BB);
3936 if (Slot == -1)
3937 continue;
3938 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3939 }
3940}
3941
3943 unsigned Slot,
3944 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3945 return Slots2BasicBlocks.lookup(Slot);
3946}
3947
3948const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3949 if (Slots2BasicBlocks.empty())
3950 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3951 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3952}
3953
3954const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3955 if (&F == &MF.getFunction())
3956 return getIRBlock(Slot);
3957 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3958 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3959 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3960}
3961
3962MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3963 // FIXME: Currently we can't recognize temporary or local symbols and call all
3964 // of the appropriate forms to create them. However, this handles basic cases
3965 // well as most of the special aspects are recognized by a prefix on their
3966 // name, and the input names should already be unique. For test cases, keeping
3967 // the symbol name out of the symbol table isn't terribly important.
3968 return MF.getContext().getOrCreateSymbol(Name);
3969}
3970
3971bool MIParser::parseStringConstant(std::string &Result) {
3972 if (Token.isNot(MIToken::StringConstant))
3973 return error("expected string constant");
3974 Result = std::string(Token.stringValue());
3975 lex();
3976 return false;
3977}
3978
3980 StringRef Src,
3982 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3983}
3984
3987 return MIParser(PFS, Error, Src).parseBasicBlocks();
3988}
3989
3993 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3994}
3995
3997 Register &Reg, StringRef Src,
3999 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
4000}
4001
4003 Register &Reg, StringRef Src,
4005 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
4006}
4007
4009 VRegInfo *&Info, StringRef Src,
4011 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
4012}
4013
4016 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
4017}
4018
4022 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
4023}
4026 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
4027}
4028
4030 SMRange SrcRange, SMDiagnostic &Error) {
4031 return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata();
4032}
4033
4035 PerFunctionMIParsingState &PFS, const Value *&V,
4036 ErrorCallbackType ErrorCallback) {
4037 MIToken Token;
4038 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
4039 ErrorCallback(Loc, Msg);
4040 });
4041 V = nullptr;
4042
4043 return ::parseIRValue(Token, PFS, V, ErrorCallback);
4044}
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))
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:1563
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 BranchProbability getRaw(uint32_t N)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:686
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:123
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:205
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:239
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition Function.h:817
Module * getParent()
Get the module that this global value is contained inside of...
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
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.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1080
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition Metadata.h:1540
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1529
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition Metadata.h:1549
MIRFormater - Interface to format MIR operand based on target.
virtual bool parseImmMnemonic(const unsigned OpCode, const unsigned OpIdx, StringRef Src, int64_t &Imm, ErrorCallbackType ErrorCallback) const
Implement target specific parsing of immediate mnemonics.
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
static LLVM_ABI bool parseIRValue(StringRef Src, MachineFunction &MF, PerFunctionMIParsingState &PFS, const Value *&V, ErrorCallbackType ErrorCallback)
Helper functions to parse IR value from MIR serialization format which will be useful for target spec...
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
void setAddressTakenIRBlock(BasicBlock *BB)
Set this block to reflect that it corresponds to an IR-level basic block with a BlockAddress.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void setCallFrameSize(unsigned N)
Set the call frame size on entry to this basic block.
void setAlignment(Align A)
Set alignment of the basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void setSectionID(MBBSectionID V)
Sets the section ID for this basic block.
void setIsInlineAsmBrIndirectTarget(bool V=true)
Indicates if this is the indirect dest of an INLINEASM_BR.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
void setIsEHPad(bool V=true)
Indicates the block is a landing pad.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
void setFlag(MIFlag Flag)
Set a MI flag.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
static MachineOperand CreateES(const char *SymName, unsigned TargetFlags=0)
static MachineOperand CreateFPImm(const ConstantFP *CFP)
static MachineOperand CreateCFIIndex(unsigned CFIIndex)
static MachineOperand CreateRegMask(const uint32_t *Mask)
CreateRegMask - Creates a register mask operand referencing Mask.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineOperand CreateCImm(const ConstantInt *CI)
static MachineOperand CreateMetadata(const MDNode *Meta)
static MachineOperand CreatePredicate(unsigned Pred)
static MachineOperand CreateImm(int64_t Val)
static MachineOperand CreateShuffleMask(ArrayRef< int > Mask)
static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags=0)
static MachineOperand CreateDbgInstrRef(unsigned InstrIdx, unsigned OpIdx)
static MachineOperand CreateRegLiveOut(const uint32_t *Mask)
static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset, unsigned TargetFlags=0)
void setTargetFlags(unsigned F)
static MachineOperand CreateLaneMask(LaneBitmask LaneMask)
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned TargetFlags=0)
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset, unsigned TargetFlags=0)
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
static MachineOperand CreateIntrinsicID(Intrinsic::ID ID)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
void setRegClassOrRegBank(Register Reg, const RegClassOrRegBank &RCOrRB)
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI Register createIncompleteVirtualRegister(StringRef Name="")
Creates a new virtual register that has no register class, register bank or size assigned yet.
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
void noteNewVirtualRegister(Register Reg)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
const char * getBufferEnd() const
const char * getBufferStart() const
Root of the metadata hierarchy.
Definition Metadata.h:64
Manage lifetime of a slot tracker for printing IR.
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
void incorporateFunction(const Function &F)
Incorporate the given function.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Special value supplied for machine level alias analysis.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
unsigned getNumRegBanks() const
Get the total number of register banks.
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h: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:108
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:321
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:730
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:591
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:655
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
unsigned getID() const
Return the register class ID number.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
LLVM Value Representation.
Definition Value.h:75
bool hasName() const
Definition Value.h: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:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:557
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Kill
The last use of a register.
@ InternalRead
Register reads a value that is defined inside the same instruction or bundle.
@ Undef
Value of the register doesn't matter.
@ EarlyClobber
Register definition happens before uses.
@ Define
Register definition.
@ Renamable
Register that may be renamed.
@ Debug
Register 'use' is for debugging purpose.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
bool parsePrefetchTarget(PerFunctionMIParsingState &PFS, CallsiteID &Target, StringRef Src, SMDiagnostic &Error)
LLVM_ABI void guessSuccessors(const MachineBasicBlock &MBB, SmallVectorImpl< MachineBasicBlock * > &Result, bool &IsFallthrough)
Determine a possible list of successors of a basic block based on the basic block machine operand bei...
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:385
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition Parser.cpp:236
constexpr RegState getDefRegState(bool B)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr bool hasRegState(RegState Value, RegState Test)
AtomicOrdering
Atomic ordering for LLVM's memory model.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
bool parseRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
LLVM_ABI Constant * parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type and a constant value in the given string.
Definition Parser.cpp:195
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src, SMRange SourceRange, SMDiagnostic &Error)
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:792
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:786
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:789
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82
LLVM_ABI static const MBBSectionID ExceptionSectionID
LLVM_ABI static const MBBSectionID ColdSectionID
A token produced by the machine instruction lexer.
Definition MILexer.h:26
TokenKind kind() const
Definition MILexer.h:214
bool hasIntegerValue() const
Definition MILexer.h:254
bool is(TokenKind K) const
Definition MILexer.h:241
StringRef stringValue() const
Return the token's string value.
Definition MILexer.h:250
@ 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:173
@ kw_dbg_instr_ref
Definition MILexer.h:84
@ NamedVirtualRegister
Definition MILexer.h:171
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:183
@ 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:172
@ 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:163
@ kw_debug_location
Definition MILexer.h:82
@ kw_heap_alloc_marker
Definition MILexer.h:142
StringRef range() const
Definition MILexer.h:247
StringRef::iterator location() const
Definition MILexer.h:245
const APSInt & integerValue() const
Definition MILexer.h:252
This class contains a discriminated union of information about pointers in memory operands,...
int64_t Offset
Offset - This is an offset from the base Value*.
VRegInfo & getVRegInfo(Register Num)
Definition MIParser.cpp:329
const SlotMapping & IRSlots
Definition MIParser.h:170
const Value * getIRValue(unsigned Slot)
Definition MIParser.cpp:374
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition MIParser.h:176
StringMap< VRegInfo * > VRegInfosNamed
Definition MIParser.h:178
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition MIParser.h:185
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition MIParser.cpp:324
PerTargetMIParsingState & Target
Definition MIParser.h:171
DenseMap< Register, VRegInfo * > VRegInfos
Definition MIParser.h:177
VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition MIParser.cpp:340
bool getVRegFlagValue(StringRef FlagName, uint8_t &FlagValue) const
Definition MIParser.cpp:129
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
const RegisterBank * getRegBank(StringRef Name)
Check if the given identifier is a name of a register bank.
Definition MIParser.cpp:317
bool parseInstrName(StringRef InstrName, unsigned &OpCode)
Try to convert an instruction name to an opcode.
Definition MIParser.cpp:148
unsigned getSubRegIndex(StringRef Name)
Check if the given identifier is a name of a subregister index.
Definition MIParser.cpp:188
bool getTargetIndex(StringRef Name, int &Index)
Try to convert a name of target index to the corresponding target index.
Definition MIParser.cpp:206
void setTarget(const TargetSubtargetInfo &NewSubtarget)
Definition MIParser.cpp:81
bool getRegisterByName(StringRef RegName, Register &Reg)
Try to convert a register name to a register number.
Definition MIParser.cpp:119
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
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
const TargetRegisterClass * getRegClass(StringRef Name)
Check if the given identifier is a name of a register class.
Definition MIParser.cpp:310
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:45
union llvm::VRegInfo::@127225073067155374133234315364317264041071000132 D
const TargetRegisterClass * RC
Definition MIParser.h:44
enum llvm::VRegInfo::@374354327266250320012227113300214031244227062232 Kind
Register VReg
Definition MIParser.h:47
bool Explicit
VReg was explicitly specified in the .mir file.
Definition MIParser.h:42