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