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 .Default(InvalidKind);
1883 if (K == InvalidKind)
1884 return error("unknown inline asm operand kind '" + KindStr + "'");
1885
1886 lex();
1887
1888 // Create the flag with default of 1 operand.
1889 InlineAsm::Flag F(K, 1);
1890
1891 // Parse optional tiedto constraint: tiedto:$N.
1892 if (Token.is(MIToken::Identifier) && Token.stringValue() == "tiedto") {
1893 lex();
1894 if (Token.isNot(MIToken::colon))
1895 return error("expected ':' after 'tiedto'");
1896 lex();
1897 if (Token.isNot(MIToken::NamedRegister))
1898 return error("expected '$N' operand number after 'tiedto:'");
1899 unsigned OperandNo;
1900 if (Token.stringValue().getAsInteger(10, OperandNo))
1901 return error("invalid operand number in tiedto constraint");
1902 lex();
1903
1904 F.setMatchingOp(OperandNo);
1905 if (parseInlineAsmOperandCount(F))
1906 return true;
1907
1909 return false;
1910 }
1911
1912 // Parse an optional constraint after ':'.
1913 if (Token.is(MIToken::colon)) {
1914 lex();
1915
1916 if (Token.isNot(MIToken::Identifier))
1917 return error(
1918 "expected register class or memory constraint name after ':'");
1919
1920 StringRef ConstraintStr = Token.stringValue();
1921 if (F.isMemKind()) {
1955 return error("unknown memory constraint '" + ConstraintStr + "'");
1956 F.setMemConstraint(CC);
1957 } else if (F.isRegDefKind() || F.isRegUseKind() ||
1958 F.isRegDefEarlyClobberKind()) {
1959 const TargetRegisterClass *RC =
1960 PFS.Target.getRegClass(ConstraintStr.lower());
1961 if (!RC)
1962 return error("unknown register class '" + ConstraintStr + "'");
1963 F.setRegClass(RC->getID());
1964 }
1965
1966 lex();
1967 }
1968
1969 // Parse an optional 'foldable' marker: the register allocator may fold the
1970 // operand's register to a stack slot (see
1971 // InlineAsm::Flag::setRegMayBeFolded()).
1972 if (Token.is(MIToken::Identifier) && Token.stringValue() == "foldable") {
1973 if (!F.isRegDefKind() && !F.isRegUseKind() && !F.isRegDefEarlyClobberKind())
1974 return error("only a register operand can be 'foldable'");
1975
1976 F.setRegMayBeFolded(true);
1977 lex();
1978 }
1979
1980 if (parseInlineAsmOperandCount(F))
1981 return true;
1982
1984 return false;
1985}
1986
1987bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1988 const unsigned OpIdx,
1989 MachineOperand &Dest,
1990 const MIRFormatter &MF) {
1991 assert(Token.is(MIToken::dot));
1992 auto Loc = Token.location(); // record start position
1993 size_t Len = 1; // for "."
1994 lex();
1995
1996 // Handle the case that mnemonic starts with number.
1997 if (Token.is(MIToken::IntegerLiteral)) {
1998 Len += Token.range().size();
1999 lex();
2000 }
2001
2002 StringRef Src;
2003 if (Token.is(MIToken::comma))
2004 Src = StringRef(Loc, Len);
2005 else {
2006 assert(Token.is(MIToken::Identifier));
2007 Src = StringRef(Loc, Len + Token.stringValue().size());
2008 }
2009 int64_t Val;
2010 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
2011 [this](StringRef::iterator Loc, const Twine &Msg)
2012 -> bool { return error(Loc, Msg); }))
2013 return true;
2014
2015 Dest = MachineOperand::CreateImm(Val);
2016 if (!Token.is(MIToken::comma))
2017 lex();
2018 return false;
2019}
2020
2022 PerFunctionMIParsingState &PFS, const Constant *&C,
2023 ErrorCallbackType ErrCB) {
2024 auto Source = StringValue.str(); // The source has to be null terminated.
2025 SMDiagnostic Err;
2026 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
2027 &PFS.IRSlots);
2028 if (!C)
2029 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
2030 return false;
2031}
2032
2033bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
2034 const Constant *&C) {
2035 return ::parseIRConstant(
2036 Loc, StringValue, PFS, C,
2037 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2038 return error(Loc, Msg);
2039 });
2040}
2041
2042bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
2043 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
2044 return true;
2045 lex();
2046 return false;
2047}
2048
2049// See LLT implementation for bit size limits.
2051 return Size != 0 && isUInt<16>(Size);
2052}
2053
2054static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale) {
2055 // A fixed-length vector needs at least two elements.
2056 return NumElts != 0 && (HasVScale || NumElts != 1) && isUInt<16>(NumElts);
2057}
2058
2059static bool verifyAddrSpace(uint64_t AddrSpace) {
2060 return isUInt<24>(AddrSpace);
2061}
2062
2063bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
2064 StringRef TypeDigits = Token.range();
2065 if (TypeDigits.consume_front("s") || TypeDigits.consume_front("i") ||
2066 TypeDigits.consume_front("f") || TypeDigits.consume_front("p") ||
2067 TypeDigits.consume_front("bf")) {
2068 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2069 return error(
2070 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2071 }
2072
2073 bool Scalar = Token.range().starts_with("s");
2074 if (Scalar || Token.range().starts_with("i")) {
2075 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2076 if (!ScalarSize) {
2077 Ty = LLT::token();
2078 lex();
2079 return false;
2080 }
2081
2082 if (!verifyScalarSize(ScalarSize))
2083 return error("invalid size for scalar type");
2084
2085 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2086 lex();
2087 return false;
2088 }
2089
2090 if (Token.range().starts_with("p")) {
2091 const DataLayout &DL = MF.getDataLayout();
2092 uint64_t AS = APSInt(TypeDigits).getZExtValue();
2093 if (!verifyAddrSpace(AS))
2094 return error("invalid address space number");
2095
2096 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2097 lex();
2098 return false;
2099 }
2100
2101 if (Token.range().starts_with("f") || Token.range().starts_with("bf")) {
2102 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2103 if (!ScalarSize || !verifyScalarSize(ScalarSize))
2104 return error("invalid size for scalar type");
2105
2106 if (Token.range().starts_with("bf") && ScalarSize != 16)
2107 return error("invalid size for bfloat");
2108
2109 Ty = Token.range().starts_with("bf") ? LLT::bfloat16()
2110 : LLT::floatIEEE(ScalarSize);
2111 lex();
2112 return false;
2113 }
2114
2115 // Now we're looking for a vector.
2116 if (Token.isNot(MIToken::less))
2117 return error(Loc, "expected tN, pA, <M x tN>, <M x pA>, <vscale x M x tN>, "
2118 "or <vscale x M x pA> for GlobalISel type, "
2119 "where t = {'s', 'i', 'f', 'bf'}");
2120 lex();
2121
2122 bool HasVScale =
2123 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2124 if (HasVScale) {
2125 lex();
2126 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2127 return error(
2128 "expected <vscale x M x tN>, where t = {'s', 'i', 'f', 'bf', 'p'}");
2129 lex();
2130 }
2131
2132 auto GetError = [this, &HasVScale, Loc]() {
2133 if (HasVScale)
2134 return error(Loc, "expected <vscale x M x tN> for vector type, where t = "
2135 "{'s', 'i', 'f', 'bf', 'p'}");
2136 return error(Loc, "expected <M x tN> for vector type, where t = {'s', 'i', "
2137 "'f', 'bf', 'p'}");
2138 };
2139
2140 if (Token.isNot(MIToken::IntegerLiteral))
2141 return GetError();
2142 uint64_t NumElements = Token.integerValue().getZExtValue();
2143 if (!verifyVectorElementCount(NumElements, HasVScale))
2144 return error("invalid number of vector elements");
2145
2146 lex();
2147
2148 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2149 return GetError();
2150 lex();
2151
2152 StringRef VectorTyDigits = Token.range();
2153 if (!VectorTyDigits.consume_front("s") &&
2154 !VectorTyDigits.consume_front("i") &&
2155 !VectorTyDigits.consume_front("f") &&
2156 !VectorTyDigits.consume_front("p") && !VectorTyDigits.consume_front("bf"))
2157 return GetError();
2158
2159 if (VectorTyDigits.empty() || !llvm::all_of(VectorTyDigits, isdigit))
2160 return error(
2161 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2162
2163 Scalar = Token.range().starts_with("s");
2164 if (Scalar || Token.range().starts_with("i")) {
2165 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2166 if (!verifyScalarSize(ScalarSize))
2167 return error("invalid size for scalar element in vector");
2168 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2169 } else if (Token.range().starts_with("p")) {
2170 const DataLayout &DL = MF.getDataLayout();
2171 uint64_t AS = APSInt(VectorTyDigits).getZExtValue();
2172 if (!verifyAddrSpace(AS))
2173 return error("invalid address space number");
2174
2175 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2176 } else if (Token.range().starts_with("f")) {
2177 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2178 if (!verifyScalarSize(ScalarSize))
2179 return error("invalid size for float element in vector");
2180 Ty = LLT::floatIEEE(ScalarSize);
2181 } else if (Token.range().starts_with("bf")) {
2182 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2183 if (!verifyScalarSize(ScalarSize))
2184 return error("invalid size for bfloat element in vector");
2185 Ty = LLT::bfloat16();
2186 } else {
2187 return GetError();
2188 }
2189 lex();
2190
2191 if (Token.isNot(MIToken::greater))
2192 return GetError();
2193
2194 lex();
2195
2196 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2197 return false;
2198}
2199
2200bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2201 assert(Token.is(MIToken::Identifier));
2202 StringRef TypeDigits = Token.range();
2203 if (!TypeDigits.consume_front("i") && !TypeDigits.consume_front("s") &&
2204 !TypeDigits.consume_front("p") && !TypeDigits.consume_front("f") &&
2205 !TypeDigits.consume_front("bf"))
2206 return error("a typed immediate operand should start with one of 'i', "
2207 "'s', 'f', 'bf', or 'p'");
2208 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2209 return error(
2210 "expected integers after 'i'/'s'/'f'/'bf'/'p' type identifier");
2211
2212 auto Loc = Token.location();
2213 lex();
2214 if (Token.isNot(MIToken::IntegerLiteral)) {
2215 if (Token.isNot(MIToken::Identifier) ||
2216 !(Token.range() == "true" || Token.range() == "false"))
2217 return error("expected an integer literal");
2218 }
2219 const Constant *C = nullptr;
2220 if (parseIRConstant(Loc, C))
2221 return true;
2223 return false;
2224}
2225
2226bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2227 auto Loc = Token.location();
2228 lex();
2229 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2230 Token.isNot(MIToken::HexLiteral))
2231 return error("expected a floating point literal");
2232 const Constant *C = nullptr;
2233 if (parseIRConstant(Loc, C))
2234 return true;
2236 return false;
2237}
2238
2239static bool getHexUint(const MIToken &Token, APInt &Result) {
2241 StringRef S = Token.range();
2242 assert(S[0] == '0' && tolower(S[1]) == 'x');
2243 // This could be a floating point literal with a special prefix.
2244 if (!isxdigit(S[2]))
2245 return true;
2246 StringRef V = S.substr(2);
2247 APInt A(V.size()*4, V, 16);
2248
2249 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2250 // sure it isn't the case before constructing result.
2251 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2252 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2253 return false;
2254}
2255
2256static bool getUnsigned(const MIToken &Token, unsigned &Result,
2257 ErrorCallbackType ErrCB) {
2258 if (Token.hasIntegerValue()) {
2259 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2260 const APSInt &SInt = Token.integerValue();
2261 if (SInt.isNegative())
2262 return ErrCB(Token.location(), "expected unsigned integer");
2263 uint64_t Val64 = SInt.getLimitedValue(Limit);
2264 if (Val64 == Limit)
2265 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2266 Result = Val64;
2267 return false;
2268 }
2269 if (Token.is(MIToken::HexLiteral)) {
2270 APInt A;
2271 if (getHexUint(Token, A))
2272 return true;
2273 if (A.getBitWidth() > 32)
2274 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2275 Result = A.getZExtValue();
2276 return false;
2277 }
2278 return true;
2279}
2280
2281bool MIParser::getUnsigned(unsigned &Result) {
2282 return ::getUnsigned(
2283 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2284 return error(Loc, Msg);
2285 });
2286}
2287
2288bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2291 unsigned Number;
2292 if (getUnsigned(Number))
2293 return true;
2294 auto MBBInfo = PFS.MBBSlots.find(Number);
2295 if (MBBInfo == PFS.MBBSlots.end())
2296 return error(Twine("use of undefined machine basic block #") +
2297 Twine(Number));
2298 MBB = MBBInfo->second;
2299 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2300 // we drop the <irname> from the bb.<id>.<irname> format.
2301 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2302 return error(Twine("the name of machine basic block #") + Twine(Number) +
2303 " isn't '" + Token.stringValue() + "'");
2304 return false;
2305}
2306
2307bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2310 return true;
2312 lex();
2313 return false;
2314}
2315
2316bool MIParser::parseStackFrameIndex(int &FI) {
2317 assert(Token.is(MIToken::StackObject));
2318 unsigned ID;
2319 if (getUnsigned(ID))
2320 return true;
2321 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2322 if (ObjectInfo == PFS.StackObjectSlots.end())
2323 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2324 "'");
2326 if (const auto *Alloca =
2327 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2328 Name = Alloca->getName();
2329 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2330 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2331 "' isn't '" + Token.stringValue() + "'");
2332 lex();
2333 FI = ObjectInfo->second;
2334 return false;
2335}
2336
2337bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2338 int FI;
2339 if (parseStackFrameIndex(FI))
2340 return true;
2341 Dest = MachineOperand::CreateFI(FI);
2342 return false;
2343}
2344
2345bool MIParser::parseFixedStackFrameIndex(int &FI) {
2347 unsigned ID;
2348 if (getUnsigned(ID))
2349 return true;
2350 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2351 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2352 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2353 Twine(ID) + "'");
2354 lex();
2355 FI = ObjectInfo->second;
2356 return false;
2357}
2358
2359bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2360 int FI;
2361 if (parseFixedStackFrameIndex(FI))
2362 return true;
2363 Dest = MachineOperand::CreateFI(FI);
2364 return false;
2365}
2366
2367static bool parseGlobalValue(const MIToken &Token,
2369 ErrorCallbackType ErrCB) {
2370 switch (Token.kind()) {
2372 const Module *M = PFS.MF.getFunction().getParent();
2373 GV = M->getNamedValue(Token.stringValue());
2374 if (!GV)
2375 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2376 Token.range() + "'");
2377 break;
2378 }
2379 case MIToken::GlobalValue: {
2380 unsigned GVIdx;
2381 if (getUnsigned(Token, GVIdx, ErrCB))
2382 return true;
2383 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2384 if (!GV)
2385 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2386 Twine(GVIdx) + "'");
2387 break;
2388 }
2389 default:
2390 llvm_unreachable("The current token should be a global value");
2391 }
2392 return false;
2393}
2394
2395bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2396 return ::parseGlobalValue(
2397 Token, PFS, GV,
2398 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2399 return error(Loc, Msg);
2400 });
2401}
2402
2403bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2404 GlobalValue *GV = nullptr;
2405 if (parseGlobalValue(GV))
2406 return true;
2407 lex();
2408 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2409 if (parseOperandsOffset(Dest))
2410 return true;
2411 return false;
2412}
2413
2414bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2416 unsigned ID;
2417 if (getUnsigned(ID))
2418 return true;
2419 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2420 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2421 return error("use of undefined constant '%const." + Twine(ID) + "'");
2422 lex();
2423 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2424 if (parseOperandsOffset(Dest))
2425 return true;
2426 return false;
2427}
2428
2429bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2431 unsigned ID;
2432 if (getUnsigned(ID))
2433 return true;
2434 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2435 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2436 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2437 lex();
2438 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2439 return false;
2440}
2441
2442bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2444 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2445 lex();
2446 Dest = MachineOperand::CreateES(Symbol);
2447 if (parseOperandsOffset(Dest))
2448 return true;
2449 return false;
2450}
2451
2452bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2453 assert(Token.is(MIToken::MCSymbol));
2454 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2455 lex();
2456 Dest = MachineOperand::CreateMCSymbol(Symbol);
2457 if (parseOperandsOffset(Dest))
2458 return true;
2459 return false;
2460}
2461
2462bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2464 StringRef Name = Token.stringValue();
2465 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2466 if (SubRegIndex == 0)
2467 return error(Twine("unknown subregister index '") + Name + "'");
2468 lex();
2469 Dest = MachineOperand::CreateImm(SubRegIndex);
2470 return false;
2471}
2472
2473bool MIParser::parseMDNode(MDNode *&Node) {
2474 assert(Token.is(MIToken::exclaim));
2475
2476 auto Loc = Token.location();
2477 lex();
2478 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2479 return error("expected metadata id after '!'");
2480 unsigned ID;
2481 if (getUnsigned(ID))
2482 return true;
2483 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2484 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2485 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2486 if (NodeInfo == PFS.MachineMetadataNodes.end())
2487 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2488 }
2489 lex();
2490 Node = NodeInfo->second.get();
2491 return false;
2492}
2493
2494bool MIParser::parseDIExpression(MDNode *&Expr) {
2495 unsigned Read;
2497 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2498 &PFS.IRSlots);
2499 CurrentSource = CurrentSource.substr(Read);
2500 lex();
2501 if (!Expr)
2502 return error(Error.getMessage());
2503 return false;
2504}
2505
2506bool MIParser::parseDILocation(MDNode *&Loc) {
2507 assert(Token.is(MIToken::md_dilocation));
2508 lex();
2509
2510 bool HaveLine = false;
2511 unsigned Line = 0;
2512 unsigned Column = 0;
2513 MDNode *Scope = nullptr;
2514 MDNode *InlinedAt = nullptr;
2515 bool ImplicitCode = false;
2516 uint64_t AtomGroup = 0;
2517 uint64_t AtomRank = 0;
2518 MDNode *IRLayers = nullptr;
2519
2520 if (expectAndConsume(MIToken::lparen))
2521 return true;
2522
2523 if (Token.isNot(MIToken::rparen)) {
2524 do {
2525 if (Token.is(MIToken::Identifier)) {
2526 if (Token.stringValue() == "line") {
2527 lex();
2528 if (expectAndConsume(MIToken::colon))
2529 return true;
2530 if (Token.isNot(MIToken::IntegerLiteral) ||
2531 Token.integerValue().isSigned())
2532 return error("expected unsigned integer");
2533 Line = Token.integerValue().getZExtValue();
2534 HaveLine = true;
2535 lex();
2536 continue;
2537 }
2538 if (Token.stringValue() == "column") {
2539 lex();
2540 if (expectAndConsume(MIToken::colon))
2541 return true;
2542 if (Token.isNot(MIToken::IntegerLiteral) ||
2543 Token.integerValue().isSigned())
2544 return error("expected unsigned integer");
2545 Column = Token.integerValue().getZExtValue();
2546 lex();
2547 continue;
2548 }
2549 if (Token.stringValue() == "scope") {
2550 lex();
2551 if (expectAndConsume(MIToken::colon))
2552 return true;
2553 if (parseMDNode(Scope))
2554 return error("expected metadata node");
2555 if (!isa<DIScope>(Scope))
2556 return error("expected DIScope node");
2557 continue;
2558 }
2559 if (Token.stringValue() == "inlinedAt") {
2560 lex();
2561 if (expectAndConsume(MIToken::colon))
2562 return true;
2563 if (Token.is(MIToken::exclaim)) {
2564 if (parseMDNode(InlinedAt))
2565 return true;
2566 } else if (Token.is(MIToken::md_dilocation)) {
2567 if (parseDILocation(InlinedAt))
2568 return true;
2569 } else {
2570 return error("expected metadata node");
2571 }
2572 if (!isa<DILocation>(InlinedAt))
2573 return error("expected DILocation node");
2574 continue;
2575 }
2576 if (Token.stringValue() == "isImplicitCode") {
2577 lex();
2578 if (expectAndConsume(MIToken::colon))
2579 return true;
2580 if (!Token.is(MIToken::Identifier))
2581 return error("expected true/false");
2582 // As far as I can see, we don't have any existing need for parsing
2583 // true/false in MIR yet. Do it ad-hoc until there's something else
2584 // that needs it.
2585 if (Token.stringValue() == "true")
2586 ImplicitCode = true;
2587 else if (Token.stringValue() == "false")
2588 ImplicitCode = false;
2589 else
2590 return error("expected true/false");
2591 lex();
2592 continue;
2593 }
2594 if (Token.stringValue() == "atomGroup") {
2595 lex();
2596 if (expectAndConsume(MIToken::colon))
2597 return true;
2598 if (Token.isNot(MIToken::IntegerLiteral) ||
2599 Token.integerValue().isSigned())
2600 return error("expected unsigned integer");
2601 AtomGroup = Token.integerValue().getZExtValue();
2602 lex();
2603 continue;
2604 }
2605 if (Token.stringValue() == "atomRank") {
2606 lex();
2607 if (expectAndConsume(MIToken::colon))
2608 return true;
2609 if (Token.isNot(MIToken::IntegerLiteral) ||
2610 Token.integerValue().isSigned())
2611 return error("expected unsigned integer");
2612 AtomRank = Token.integerValue().getZExtValue();
2613 lex();
2614 continue;
2615 }
2616 if (Token.stringValue() == "irlayers") {
2617 lex();
2618 if (expectAndConsume(MIToken::colon))
2619 return true;
2620 if (parseMDNode(IRLayers))
2621 return error("expected metadata node");
2622 if (!isa<DILayerLocList>(IRLayers))
2623 return error("expected DILayerLocList node");
2624 continue;
2625 }
2626 }
2627 return error(Twine("invalid DILocation argument '") +
2628 Token.stringValue() + "'");
2629 } while (consumeIfPresent(MIToken::comma));
2630 }
2631
2632 if (expectAndConsume(MIToken::rparen))
2633 return true;
2634
2635 if (!HaveLine)
2636 return error("DILocation requires line number");
2637 if (!Scope)
2638 return error("DILocation requires a scope");
2639
2640 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2641 InlinedAt, ImplicitCode, AtomGroup, AtomRank, IRLayers);
2642 return false;
2643}
2644
2645bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2646 MDNode *Node = nullptr;
2647 if (Token.is(MIToken::exclaim)) {
2648 if (parseMDNode(Node))
2649 return true;
2650 } else if (Token.is(MIToken::md_diexpr)) {
2651 if (parseDIExpression(Node))
2652 return true;
2653 }
2654 Dest = MachineOperand::CreateMetadata(Node);
2655 return false;
2656}
2657
2658bool MIParser::parseCFIOffset(int64_t &Offset) {
2659 if (Token.isNot(MIToken::IntegerLiteral))
2660 return error("expected a cfi offset");
2661 if (!Token.integerValue().isRepresentableByInt64())
2662 return error("expected a 64 bit integer (the cfi offset is too large)");
2663 Offset = Token.integerValue().getExtValue();
2664 lex();
2665 return false;
2666}
2667
2668bool MIParser::parseCFIUnsigned(unsigned &Value) {
2669 if (getUnsigned(Value))
2670 return true;
2671 lex();
2672 return false;
2673}
2674
2675bool MIParser::parseCFIRegister(unsigned &Reg) {
2676 if (Token.isNot(MIToken::NamedRegister))
2677 return error("expected a cfi register");
2678 Register LLVMReg;
2679 if (parseNamedRegister(LLVMReg))
2680 return true;
2681 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2682 assert(TRI && "Expected target register info");
2683 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2684 if (DwarfReg < 0)
2685 return error("invalid DWARF register");
2686 Reg = (unsigned)DwarfReg;
2687 lex();
2688 return false;
2689}
2690
2691bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2692 if (Token.isNot(MIToken::IntegerLiteral))
2693 return error("expected a cfi address space literal");
2694 return parseCFIUnsigned(AddressSpace);
2695}
2696
2697bool MIParser::parseCFIEscapeValues(std::string &Values) {
2698 do {
2699 if (Token.isNot(MIToken::HexLiteral))
2700 return error("expected a hexadecimal literal");
2701 unsigned Value;
2702 if (getUnsigned(Value))
2703 return true;
2704 if (Value > UINT8_MAX)
2705 return error("expected a 8-bit integer (too large)");
2706 Values.push_back(static_cast<uint8_t>(Value));
2707 lex();
2708 } while (consumeIfPresent(MIToken::comma));
2709 return false;
2710}
2711
2712bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2713 auto Kind = Token.kind();
2714 lex();
2715 int64_t Offset;
2716 unsigned Reg;
2717 unsigned AddressSpace;
2718 unsigned CFIIndex;
2719 switch (Kind) {
2721 if (parseCFIRegister(Reg))
2722 return true;
2723 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2724 break;
2726 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2727 parseCFIOffset(Offset))
2728 return true;
2729 CFIIndex =
2730 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2731 break;
2733 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2734 parseCFIOffset(Offset))
2735 return true;
2736 CFIIndex = MF.addFrameInst(
2738 break;
2740 if (parseCFIRegister(Reg))
2741 return true;
2742 CFIIndex =
2743 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2744 break;
2746 if (parseCFIOffset(Offset))
2747 return true;
2748 CFIIndex =
2749 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2750 break;
2752 if (parseCFIOffset(Offset))
2753 return true;
2754 CFIIndex = MF.addFrameInst(
2756 break;
2758 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2759 parseCFIOffset(Offset))
2760 return true;
2761 CFIIndex =
2762 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2763 break;
2765 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2766 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2767 parseCFIAddressSpace(AddressSpace))
2768 return true;
2769 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2770 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2771 break;
2773 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2774 break;
2776 if (parseCFIRegister(Reg))
2777 return true;
2778 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2779 break;
2781 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2782 break;
2784 if (parseCFIRegister(Reg))
2785 return true;
2786 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2787 break;
2789 unsigned Reg2;
2790 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2791 parseCFIRegister(Reg2))
2792 return true;
2793
2794 CFIIndex =
2795 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2796 break;
2797 }
2799 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2800 break;
2802 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2803 break;
2805 CFIIndex =
2806 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2807 break;
2809 unsigned State;
2810 MCSymbol *PACSym = nullptr;
2811 if (parseCFIUnsigned(State) || expectAndConsume(MIToken::comma))
2812 return true;
2813 if (Token.is(MIToken::MCSymbol)) {
2814 PACSym = getOrCreateMCSymbol(Token.stringValue());
2815 lex();
2816 CFIIndex = MF.addFrameInst(
2817 MCCFIInstruction::createSetRAState(nullptr, State, PACSym));
2818 } else if (Token.is(MIToken::IntegerLiteral)) {
2819 int64_t Offset;
2820 if (parseCFIOffset(Offset))
2821 return true;
2822 CFIIndex = MF.addFrameInst(
2824 } else {
2825 return error("expected '<mcsymbol ...>' or integer offset for "
2826 "cfi_set_ra_state");
2827 }
2828 break;
2829 }
2831 unsigned Reg, R1, R2;
2832 unsigned R1Size, R2Size;
2833 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2834 parseCFIRegister(R1) || expectAndConsume(MIToken::comma) ||
2835 parseCFIUnsigned(R1Size) || expectAndConsume(MIToken::comma) ||
2836 parseCFIRegister(R2) || expectAndConsume(MIToken::comma) ||
2837 parseCFIUnsigned(R2Size))
2838 return true;
2839
2840 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMRegisterPair(
2841 nullptr, Reg, R1, R1Size, R2, R2Size));
2842 break;
2843 }
2845 std::vector<MCCFIInstruction::VectorRegisterWithLane> VectorRegisters;
2846 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma))
2847 return true;
2848 do {
2849 unsigned VR;
2850 unsigned Lane, Size;
2851 if (parseCFIRegister(VR) || expectAndConsume(MIToken::comma) ||
2852 parseCFIUnsigned(Lane) || expectAndConsume(MIToken::comma) ||
2853 parseCFIUnsigned(Size))
2854 return true;
2855 VectorRegisters.push_back({VR, Lane, Size});
2856 } while (consumeIfPresent(MIToken::comma));
2857
2858 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisters(
2859 nullptr, Reg, std::move(VectorRegisters)));
2860 break;
2861 }
2863 unsigned Reg, MaskReg;
2864 unsigned RegSize, MaskRegSize;
2865 int64_t Offset = 0;
2866
2867 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2868 parseCFIUnsigned(RegSize) || expectAndConsume(MIToken::comma) ||
2869 parseCFIRegister(MaskReg) || expectAndConsume(MIToken::comma) ||
2870 parseCFIUnsigned(MaskRegSize) || expectAndConsume(MIToken::comma) ||
2871 parseCFIOffset(Offset))
2872 return true;
2873
2874 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorOffset(
2875 nullptr, Reg, RegSize, MaskReg, MaskRegSize, Offset));
2876 break;
2877 }
2879 unsigned Reg, SpillReg, MaskReg;
2880 unsigned SpillRegLaneSize, MaskRegSize;
2881
2882 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2883 parseCFIRegister(SpillReg) || expectAndConsume(MIToken::comma) ||
2884 parseCFIUnsigned(SpillRegLaneSize) ||
2885 expectAndConsume(MIToken::comma) || parseCFIRegister(MaskReg) ||
2886 expectAndConsume(MIToken::comma) || parseCFIUnsigned(MaskRegSize))
2887 return true;
2888
2889 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisterMask(
2890 nullptr, Reg, SpillReg, SpillRegLaneSize, MaskReg, MaskRegSize));
2891 break;
2892 }
2894 std::string Values;
2895 if (parseCFIEscapeValues(Values))
2896 return true;
2897 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2898 break;
2899 }
2900 default:
2901 // TODO: Parse the other CFI operands.
2902 llvm_unreachable("The current token should be a cfi operand");
2903 }
2904 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2905 return false;
2906}
2907
2908bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2909 switch (Token.kind()) {
2910 case MIToken::NamedIRBlock: {
2912 F.getValueSymbolTable()->lookup(Token.stringValue()));
2913 if (!BB)
2914 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2915 break;
2916 }
2917 case MIToken::IRBlock: {
2918 unsigned SlotNumber = 0;
2919 if (getUnsigned(SlotNumber))
2920 return true;
2921 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2922 if (!BB)
2923 return error(Twine("use of undefined IR block '%ir-block.") +
2924 Twine(SlotNumber) + "'");
2925 break;
2926 }
2927 default:
2928 llvm_unreachable("The current token should be an IR block reference");
2929 }
2930 return false;
2931}
2932
2933bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2935 lex();
2936 if (expectAndConsume(MIToken::lparen))
2937 return true;
2938 if (Token.isNot(MIToken::GlobalValue) &&
2939 Token.isNot(MIToken::NamedGlobalValue))
2940 return error("expected a global value");
2941 GlobalValue *GV = nullptr;
2942 if (parseGlobalValue(GV))
2943 return true;
2944 auto *F = dyn_cast<Function>(GV);
2945 if (!F)
2946 return error("expected an IR function reference");
2947 lex();
2948 if (expectAndConsume(MIToken::comma))
2949 return true;
2950 BasicBlock *BB = nullptr;
2951 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2952 return error("expected an IR block reference");
2953 if (parseIRBlock(BB, *F))
2954 return true;
2955 lex();
2956 if (expectAndConsume(MIToken::rparen))
2957 return true;
2958 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2959 if (parseOperandsOffset(Dest))
2960 return true;
2961 return false;
2962}
2963
2964bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2965 assert(Token.is(MIToken::kw_intrinsic));
2966 lex();
2967 if (expectAndConsume(MIToken::lparen))
2968 return error("expected syntax intrinsic(@llvm.whatever)");
2969
2970 if (Token.isNot(MIToken::NamedGlobalValue))
2971 return error("expected syntax intrinsic(@llvm.whatever)");
2972
2973 std::string Name = std::string(Token.stringValue());
2974 lex();
2975
2976 if (expectAndConsume(MIToken::rparen))
2977 return error("expected ')' to terminate intrinsic name");
2978
2979 // Find out what intrinsic we're dealing with.
2981 if (ID == Intrinsic::not_intrinsic)
2982 return error("unknown intrinsic name");
2984
2985 return false;
2986}
2987
2988bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2989 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2990 bool IsFloat = Token.is(MIToken::kw_floatpred);
2991 lex();
2992
2993 if (expectAndConsume(MIToken::lparen))
2994 return error("expected syntax intpred(whatever) or floatpred(whatever");
2995
2996 if (Token.isNot(MIToken::Identifier))
2997 return error("whatever");
2998
3000 if (IsFloat) {
3001 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
3002 .Case("false", CmpInst::FCMP_FALSE)
3003 .Case("oeq", CmpInst::FCMP_OEQ)
3004 .Case("ogt", CmpInst::FCMP_OGT)
3005 .Case("oge", CmpInst::FCMP_OGE)
3006 .Case("olt", CmpInst::FCMP_OLT)
3007 .Case("ole", CmpInst::FCMP_OLE)
3008 .Case("one", CmpInst::FCMP_ONE)
3009 .Case("ord", CmpInst::FCMP_ORD)
3010 .Case("uno", CmpInst::FCMP_UNO)
3011 .Case("ueq", CmpInst::FCMP_UEQ)
3012 .Case("ugt", CmpInst::FCMP_UGT)
3013 .Case("uge", CmpInst::FCMP_UGE)
3014 .Case("ult", CmpInst::FCMP_ULT)
3015 .Case("ule", CmpInst::FCMP_ULE)
3016 .Case("une", CmpInst::FCMP_UNE)
3017 .Case("true", CmpInst::FCMP_TRUE)
3020 return error("invalid floating-point predicate");
3021 } else {
3022 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
3023 .Case("eq", CmpInst::ICMP_EQ)
3024 .Case("ne", CmpInst::ICMP_NE)
3025 .Case("sgt", CmpInst::ICMP_SGT)
3026 .Case("sge", CmpInst::ICMP_SGE)
3027 .Case("slt", CmpInst::ICMP_SLT)
3028 .Case("sle", CmpInst::ICMP_SLE)
3029 .Case("ugt", CmpInst::ICMP_UGT)
3030 .Case("uge", CmpInst::ICMP_UGE)
3031 .Case("ult", CmpInst::ICMP_ULT)
3032 .Case("ule", CmpInst::ICMP_ULE)
3035 return error("invalid integer predicate");
3036 }
3037
3038 lex();
3040 if (expectAndConsume(MIToken::rparen))
3041 return error("predicate should be terminated by ')'.");
3042
3043 return false;
3044}
3045
3046bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
3048
3049 lex();
3050 if (expectAndConsume(MIToken::lparen))
3051 return error("expected syntax shufflemask(<integer or undef>, ...)");
3052
3053 SmallVector<int, 32> ShufMask;
3054 do {
3055 if (Token.is(MIToken::kw_undef)) {
3056 ShufMask.push_back(-1);
3057 } else if (Token.is(MIToken::IntegerLiteral)) {
3058 const APSInt &Int = Token.integerValue();
3059 ShufMask.push_back(Int.getExtValue());
3060 } else {
3061 return error("expected integer constant");
3062 }
3063
3064 lex();
3065 } while (consumeIfPresent(MIToken::comma));
3066
3067 if (expectAndConsume(MIToken::rparen))
3068 return error("shufflemask should be terminated by ')'.");
3069
3070 if (ShufMask.size() < 2)
3071 return error("shufflemask should have > 1 element");
3072
3073 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
3074 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
3075 return false;
3076}
3077
3078bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
3080
3081 lex();
3082 if (expectAndConsume(MIToken::lparen))
3083 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3084
3085 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3086 return error("expected unsigned integer for instruction index");
3087 uint64_t InstrIdx = Token.integerValue().getZExtValue();
3088 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
3089 "Instruction reference's instruction index is too large");
3090 lex();
3091
3092 if (expectAndConsume(MIToken::comma))
3093 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3094
3095 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3096 return error("expected unsigned integer for operand index");
3097 uint64_t OpIdx = Token.integerValue().getZExtValue();
3098 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
3099 "Instruction reference's operand index is too large");
3100 lex();
3101
3102 if (expectAndConsume(MIToken::rparen))
3103 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3104
3105 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
3106 return false;
3107}
3108
3109bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
3111 lex();
3112 if (expectAndConsume(MIToken::lparen))
3113 return true;
3114 if (Token.isNot(MIToken::Identifier))
3115 return error("expected the name of the target index");
3116 int Index = 0;
3117 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
3118 return error("use of undefined target index '" + Token.stringValue() + "'");
3119 lex();
3120 if (expectAndConsume(MIToken::rparen))
3121 return true;
3122 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
3123 if (parseOperandsOffset(Dest))
3124 return true;
3125 return false;
3126}
3127
3128bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
3129 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
3130 lex();
3131 if (expectAndConsume(MIToken::lparen))
3132 return true;
3133
3134 uint32_t *Mask = MF.allocateRegMask();
3135 do {
3136 if (Token.isNot(MIToken::rparen)) {
3137 if (Token.isNot(MIToken::NamedRegister))
3138 return error("expected a named register");
3139 Register Reg;
3140 if (parseNamedRegister(Reg))
3141 return true;
3142 lex();
3143 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3144 }
3145
3146 // TODO: Report an error if the same register is used more than once.
3147 } while (consumeIfPresent(MIToken::comma));
3148
3149 if (expectAndConsume(MIToken::rparen))
3150 return true;
3151 Dest = MachineOperand::CreateRegMask(Mask);
3152 return false;
3153}
3154
3155bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
3156 assert(Token.is(MIToken::kw_lanemask));
3157
3158 lex();
3159 if (expectAndConsume(MIToken::lparen))
3160 return true;
3161
3162 // Parse lanemask.
3163 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
3164 return error("expected a valid lane mask value");
3165 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
3166 "Use correct get-function for lane mask.");
3168 if (getUint64(V))
3169 return true;
3170 LaneBitmask LaneMask(V);
3171 lex();
3172
3173 if (expectAndConsume(MIToken::rparen))
3174 return true;
3175
3176 Dest = MachineOperand::CreateLaneMask(LaneMask);
3177 return false;
3178}
3179
3180bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
3181 assert(Token.is(MIToken::kw_liveout));
3182 uint32_t *Mask = MF.allocateRegMask();
3183 lex();
3184 if (expectAndConsume(MIToken::lparen))
3185 return true;
3186 while (true) {
3187 if (Token.isNot(MIToken::NamedRegister))
3188 return error("expected a named register");
3189 Register Reg;
3190 if (parseNamedRegister(Reg))
3191 return true;
3192 lex();
3193 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3194 // TODO: Report an error if the same register is used more than once.
3195 if (Token.isNot(MIToken::comma))
3196 break;
3197 lex();
3198 }
3199 if (expectAndConsume(MIToken::rparen))
3200 return true;
3202 return false;
3203}
3204
3205bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
3206 MachineOperand &Dest,
3207 std::optional<unsigned> &TiedDefIdx) {
3208 switch (Token.kind()) {
3211 case MIToken::kw_def:
3212 case MIToken::kw_dead:
3213 case MIToken::kw_killed:
3214 case MIToken::kw_undef:
3223 return parseRegisterOperand(Dest, TiedDefIdx);
3225 // TODO: Forbid numeric operands for INLINEASM once the transition to the
3226 // symbolic form is over.
3227 return parseImmediateOperand(Dest);
3228 case MIToken::kw_half:
3229 case MIToken::kw_bfloat:
3230 case MIToken::kw_float:
3231 case MIToken::kw_double:
3233 case MIToken::kw_fp128:
3235 return parseFPImmediateOperand(Dest);
3237 return parseMBBOperand(Dest);
3239 return parseStackObjectOperand(Dest);
3241 return parseFixedStackObjectOperand(Dest);
3244 return parseGlobalAddressOperand(Dest);
3246 return parseConstantPoolIndexOperand(Dest);
3248 return parseJumpTableIndexOperand(Dest);
3250 return parseExternalSymbolOperand(Dest);
3251 case MIToken::MCSymbol:
3252 return parseMCSymbolOperand(Dest);
3254 return parseSubRegisterIndexOperand(Dest);
3255 case MIToken::md_diexpr:
3256 case MIToken::exclaim:
3257 return parseMetadataOperand(Dest);
3280 return parseCFIOperand(Dest);
3282 return parseBlockAddressOperand(Dest);
3284 return parseIntrinsicOperand(Dest);
3286 return parseTargetIndexOperand(Dest);
3288 return parseLaneMaskOperand(Dest);
3290 return parseLiveoutRegisterMaskOperand(Dest);
3293 return parsePredicateOperand(Dest);
3295 return parseShuffleMaskOperand(Dest);
3297 return parseDbgInstrRefOperand(Dest);
3298 case MIToken::Error:
3299 return true;
3300 case MIToken::Identifier: {
3301 bool IsInlineAsm = OpCode == TargetOpcode::INLINEASM ||
3302 OpCode == TargetOpcode::INLINEASM_BR;
3303 if (IsInlineAsm)
3304 return parseSymbolicInlineAsmOperand(OpIdx, Dest);
3305
3306 StringRef Id = Token.stringValue();
3307 if (const auto *RegMask = PFS.Target.getRegMask(Id)) {
3308 Dest = MachineOperand::CreateRegMask(RegMask);
3309 lex();
3310 break;
3311 } else if (Id == "CustomRegMask") {
3312 return parseCustomRegisterMaskOperand(Dest);
3313 } else {
3314 return parseTypedImmediateOperand(Dest);
3315 }
3316 }
3317 case MIToken::dot: {
3318 const auto *TII = MF.getSubtarget().getInstrInfo();
3319 if (const auto *Formatter = TII->getMIRFormatter()) {
3320 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3321 }
3322 [[fallthrough]];
3323 }
3324 default:
3325 // FIXME: Parse the MCSymbol machine operand.
3326 return error("expected a machine operand");
3327 }
3328 return false;
3329}
3330
3331bool MIParser::parseMachineOperandAndTargetFlags(
3332 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3333 std::optional<unsigned> &TiedDefIdx) {
3334 unsigned TF = 0;
3335 bool HasTargetFlags = false;
3336 if (Token.is(MIToken::kw_target_flags)) {
3337 HasTargetFlags = true;
3338 lex();
3339 if (expectAndConsume(MIToken::lparen))
3340 return true;
3341 if (Token.isNot(MIToken::Identifier))
3342 return error("expected the name of the target flag");
3343 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3344 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3345 return error("use of undefined target flag '" + Token.stringValue() +
3346 "'");
3347 }
3348 lex();
3349 while (Token.is(MIToken::comma)) {
3350 lex();
3351 if (Token.isNot(MIToken::Identifier))
3352 return error("expected the name of the target flag");
3353 unsigned BitFlag = 0;
3354 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3355 return error("use of undefined target flag '" + Token.stringValue() +
3356 "'");
3357 // TODO: Report an error when using a duplicate bit target flag.
3358 TF |= BitFlag;
3359 lex();
3360 }
3361 if (expectAndConsume(MIToken::rparen))
3362 return true;
3363 }
3364 auto Loc = Token.location();
3365 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3366 return true;
3367 if (!HasTargetFlags)
3368 return false;
3369 if (Dest.isReg())
3370 return error(Loc, "register operands can't have target flags");
3371 Dest.setTargetFlags(TF);
3372 return false;
3373}
3374
3375bool MIParser::parseOffset(int64_t &Offset) {
3376 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3377 return false;
3378 StringRef Sign = Token.range();
3379 bool IsNegative = Token.is(MIToken::minus);
3380 lex();
3381 if (Token.isNot(MIToken::IntegerLiteral))
3382 return error("expected an integer literal after '" + Sign + "'");
3383 if (Token.integerValue().getSignificantBits() > 64)
3384 return error("expected 64-bit integer (too large)");
3385 Offset = Token.integerValue().getExtValue();
3386 if (IsNegative)
3387 Offset = -Offset;
3388 lex();
3389 return false;
3390}
3391
3392bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3394 lex();
3395 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3396 return error("expected basic block after 'ir_block_address_taken'");
3397
3398 if (parseIRBlock(BB, MF.getFunction()))
3399 return true;
3400
3401 lex();
3402 return false;
3403}
3404
3405bool MIParser::parseAlignment(uint64_t &Alignment) {
3406 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3407 lex();
3408 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3409 return error("expected an integer literal after 'align'");
3410 if (getUint64(Alignment))
3411 return true;
3412 lex();
3413
3414 if (!isPowerOf2_64(Alignment))
3415 return error("expected a power-of-2 literal after 'align'");
3416
3417 return false;
3418}
3419
3420bool MIParser::parseAddrspace(unsigned &Addrspace) {
3421 assert(Token.is(MIToken::kw_addrspace));
3422 lex();
3423 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3424 return error("expected an integer literal after 'addrspace'");
3425 if (getUnsigned(Addrspace))
3426 return true;
3427 lex();
3428 return false;
3429}
3430
3431bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3432 int64_t Offset = 0;
3433 if (parseOffset(Offset))
3434 return true;
3435 Op.setOffset(Offset);
3436 return false;
3437}
3438
3439static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3440 const Value *&V, ErrorCallbackType ErrCB) {
3441 switch (Token.kind()) {
3442 case MIToken::NamedIRValue: {
3444 break;
3445 }
3446 case MIToken::IRValue: {
3447 unsigned SlotNumber = 0;
3448 if (getUnsigned(Token, SlotNumber, ErrCB))
3449 return true;
3450 V = PFS.getIRValue(SlotNumber);
3451 break;
3452 }
3454 case MIToken::GlobalValue: {
3455 GlobalValue *GV = nullptr;
3456 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3457 return true;
3458 V = GV;
3459 break;
3460 }
3462 const Constant *C = nullptr;
3463 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3464 return true;
3465 V = C;
3466 break;
3467 }
3469 V = nullptr;
3470 return false;
3471 default:
3472 llvm_unreachable("The current token should be an IR block reference");
3473 }
3474 if (!V)
3475 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3476 return false;
3477}
3478
3479bool MIParser::parseIRValue(const Value *&V) {
3480 return ::parseIRValue(
3481 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3482 return error(Loc, Msg);
3483 });
3484}
3485
3486bool MIParser::getUint64(uint64_t &Result) {
3487 if (Token.hasIntegerValue()) {
3488 if (Token.integerValue().getActiveBits() > 64)
3489 return error("expected 64-bit integer (too large)");
3490 Result = Token.integerValue().getZExtValue();
3491 return false;
3492 }
3493 if (Token.is(MIToken::HexLiteral)) {
3494 APInt A;
3495 if (getHexUint(A))
3496 return true;
3497 if (A.getBitWidth() > 64)
3498 return error("expected 64-bit integer (too large)");
3499 Result = A.getZExtValue();
3500 return false;
3501 }
3502 return true;
3503}
3504
3505bool MIParser::getHexUint(APInt &Result) {
3506 return ::getHexUint(Token, Result);
3507}
3508
3509bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3510 const auto OldFlags = Flags;
3511 switch (Token.kind()) {
3514 break;
3517 break;
3520 break;
3523 break;
3526 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3527 return error("use of undefined target MMO flag '" + Token.stringValue() +
3528 "'");
3529 Flags |= TF;
3530 break;
3531 }
3532 default:
3533 llvm_unreachable("The current token should be a memory operand flag");
3534 }
3535 if (OldFlags == Flags)
3536 // We know that the same flag is specified more than once when the flags
3537 // weren't modified.
3538 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3539 lex();
3540 return false;
3541}
3542
3543bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3544 switch (Token.kind()) {
3545 case MIToken::kw_stack:
3546 PSV = MF.getPSVManager().getStack();
3547 break;
3548 case MIToken::kw_got:
3549 PSV = MF.getPSVManager().getGOT();
3550 break;
3552 PSV = MF.getPSVManager().getJumpTable();
3553 break;
3555 PSV = MF.getPSVManager().getConstantPool();
3556 break;
3558 int FI;
3559 if (parseFixedStackFrameIndex(FI))
3560 return true;
3561 PSV = MF.getPSVManager().getFixedStack(FI);
3562 // The token was already consumed, so use return here instead of break.
3563 return false;
3564 }
3565 case MIToken::StackObject: {
3566 int FI;
3567 if (parseStackFrameIndex(FI))
3568 return true;
3569 PSV = MF.getPSVManager().getFixedStack(FI);
3570 // The token was already consumed, so use return here instead of break.
3571 return false;
3572 }
3574 lex();
3575 switch (Token.kind()) {
3578 GlobalValue *GV = nullptr;
3579 if (parseGlobalValue(GV))
3580 return true;
3581 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3582 break;
3583 }
3585 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3586 MF.createExternalSymbolName(Token.stringValue()));
3587 break;
3588 default:
3589 return error(
3590 "expected a global value or an external symbol after 'call-entry'");
3591 }
3592 break;
3593 case MIToken::kw_custom: {
3594 lex();
3595 const auto *TII = MF.getSubtarget().getInstrInfo();
3596 if (const auto *Formatter = TII->getMIRFormatter()) {
3597 if (Formatter->parseCustomPseudoSourceValue(
3598 Token.stringValue(), MF, PFS, PSV,
3599 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3600 return error(Loc, Msg);
3601 }))
3602 return true;
3603 } else {
3604 return error("unable to parse target custom pseudo source value");
3605 }
3606 break;
3607 }
3608 default:
3609 llvm_unreachable("The current token should be pseudo source value");
3610 }
3611 lex();
3612 return false;
3613}
3614
3615bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3616 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3617 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3618 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3619 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3620 const PseudoSourceValue *PSV = nullptr;
3621 if (parseMemoryPseudoSourceValue(PSV))
3622 return true;
3623 int64_t Offset = 0;
3624 if (parseOffset(Offset))
3625 return true;
3626 Dest = MachinePointerInfo(PSV, Offset);
3627 return false;
3628 }
3629 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3630 Token.isNot(MIToken::GlobalValue) &&
3631 Token.isNot(MIToken::NamedGlobalValue) &&
3632 Token.isNot(MIToken::QuotedIRValue) &&
3633 Token.isNot(MIToken::kw_unknown_address))
3634 return error("expected an IR value reference");
3635 const Value *V = nullptr;
3636 if (parseIRValue(V))
3637 return true;
3638 if (V && !V->getType()->isPointerTy())
3639 return error("expected a pointer IR value");
3640 lex();
3641 int64_t Offset = 0;
3642 if (parseOffset(Offset))
3643 return true;
3644 Dest = MachinePointerInfo(V, Offset);
3645 return false;
3646}
3647
3648bool MIParser::parseOptionalScope(LLVMContext &Context,
3649 SyncScope::ID &SSID) {
3650 SSID = SyncScope::System;
3651 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3652 lex();
3653 if (expectAndConsume(MIToken::lparen))
3654 return error("expected '(' in syncscope");
3655
3656 std::string SSN;
3657 if (parseStringConstant(SSN))
3658 return true;
3659
3660 SSID = Context.getOrInsertSyncScopeID(SSN);
3661 if (expectAndConsume(MIToken::rparen))
3662 return error("expected ')' in syncscope");
3663 }
3664
3665 return false;
3666}
3667
3668bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3670 if (Token.isNot(MIToken::Identifier))
3671 return false;
3672
3673 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3674 .Case("unordered", AtomicOrdering::Unordered)
3675 .Case("monotonic", AtomicOrdering::Monotonic)
3676 .Case("acquire", AtomicOrdering::Acquire)
3677 .Case("release", AtomicOrdering::Release)
3681
3682 if (Order != AtomicOrdering::NotAtomic) {
3683 lex();
3684 return false;
3685 }
3686
3687 return error("expected an atomic scope, ordering or a size specification");
3688}
3689
3690bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3691 if (expectAndConsume(MIToken::lparen))
3692 return true;
3694 while (Token.isMemoryOperandFlag()) {
3695 if (parseMemoryOperandFlag(Flags))
3696 return true;
3697 }
3698 if (Token.isNot(MIToken::Identifier) ||
3699 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3700 return error("expected 'load' or 'store' memory operation");
3701 if (Token.stringValue() == "load")
3703 else
3705 lex();
3706
3707 // Optional 'store' for operands that both load and store.
3708 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3710 lex();
3711 }
3712
3713 // Optional synchronization scope.
3714 SyncScope::ID SSID;
3715 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3716 return true;
3717
3718 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3719 AtomicOrdering Order, FailureOrder;
3720 if (parseOptionalAtomicOrdering(Order))
3721 return true;
3722
3723 if (parseOptionalAtomicOrdering(FailureOrder))
3724 return true;
3725
3726 if (Token.isNot(MIToken::IntegerLiteral) &&
3727 Token.isNot(MIToken::kw_unknown_size) &&
3728 Token.isNot(MIToken::lparen))
3729 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3730 "memory operation");
3731
3733 if (Token.is(MIToken::IntegerLiteral)) {
3734 uint64_t Size;
3735 if (getUint64(Size))
3736 return true;
3737
3738 // Convert from bytes to bits for storage.
3740 lex();
3741 } else if (Token.is(MIToken::kw_unknown_size)) {
3742 lex();
3743 } else {
3744 if (expectAndConsume(MIToken::lparen))
3745 return true;
3746 if (parseLowLevelType(Token.location(), MemoryType))
3747 return true;
3748 if (expectAndConsume(MIToken::rparen))
3749 return true;
3750 }
3751
3753 if (Token.is(MIToken::Identifier)) {
3754 const char *Word =
3757 ? "on"
3758 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3759 if (Token.stringValue() != Word)
3760 return error(Twine("expected '") + Word + "'");
3761 lex();
3762
3763 if (parseMachinePointerInfo(Ptr))
3764 return true;
3765 }
3766 uint64_t BaseAlignment =
3767 MemoryType.isValid()
3768 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3769 : 1;
3770 AAMDNodes AAInfo;
3771 MDNode *Range = nullptr;
3772 MDNode *MemCacheHint = nullptr;
3773 while (consumeIfPresent(MIToken::comma)) {
3774 switch (Token.kind()) {
3775 case MIToken::kw_align: {
3776 // align is printed if it is different than size.
3778 if (parseAlignment(Alignment))
3779 return true;
3780 if (Ptr.Offset & (Alignment - 1)) {
3781 // MachineMemOperand::getAlign never returns a value greater than the
3782 // alignment of offset, so this just guards against hand-written MIR
3783 // that specifies a large "align" value when it should probably use
3784 // "basealign" instead.
3785 return error("specified alignment is more aligned than offset");
3786 }
3787 BaseAlignment = Alignment;
3788 break;
3789 }
3791 // basealign is printed if it is different than align.
3792 if (parseAlignment(BaseAlignment))
3793 return true;
3794 break;
3796 if (parseAddrspace(Ptr.AddrSpace))
3797 return true;
3798 break;
3799 case MIToken::md_tbaa:
3800 lex();
3801 if (parseMDNode(AAInfo.TBAA))
3802 return true;
3803 break;
3805 lex();
3806 if (parseMDNode(AAInfo.Scope))
3807 return true;
3808 break;
3810 lex();
3811 if (parseMDNode(AAInfo.NoAlias))
3812 return true;
3813 break;
3815 lex();
3816 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3817 return true;
3818 break;
3819 case MIToken::md_range:
3820 lex();
3821 if (parseMDNode(Range))
3822 return true;
3823 break;
3825 lex();
3826 if (parseMDNode(MemCacheHint))
3827 return true;
3828 break;
3829 // TODO: Report an error on duplicate metadata nodes.
3830 default:
3831 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3832 "'!noalias' or '!range' or '!mem.cache_hint' or "
3833 "'!noalias.addrspace'");
3834 }
3835 }
3836 if (expectAndConsume(MIToken::rparen))
3837 return true;
3838 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3839 MMOMetadata(AAInfo, Range, MemCacheHint), SSID,
3840 Order, FailureOrder);
3841 return false;
3842}
3843
3844bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3846 Token.is(MIToken::kw_post_instr_symbol)) &&
3847 "Invalid token for a pre- post-instruction symbol!");
3848 lex();
3849 if (Token.isNot(MIToken::MCSymbol))
3850 return error("expected a symbol after 'pre-instr-symbol'");
3851 Symbol = getOrCreateMCSymbol(Token.stringValue());
3852 lex();
3853 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3854 Token.is(MIToken::lbrace))
3855 return false;
3856 if (Token.isNot(MIToken::comma))
3857 return error("expected ',' before the next machine operand");
3858 lex();
3859 return false;
3860}
3861
3862bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3864 "Invalid token for a heap alloc marker!");
3865 lex();
3866 if (parseMDNode(Node))
3867 return true;
3868 if (!Node)
3869 return error("expected a MDNode after 'heap-alloc-marker'");
3870 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3871 Token.is(MIToken::lbrace))
3872 return false;
3873 if (Token.isNot(MIToken::comma))
3874 return error("expected ',' before the next machine operand");
3875 lex();
3876 return false;
3877}
3878
3879bool MIParser::parsePCSections(MDNode *&Node) {
3880 assert(Token.is(MIToken::kw_pcsections) &&
3881 "Invalid token for a PC sections!");
3882 lex();
3883 if (parseMDNode(Node))
3884 return true;
3885 if (!Node)
3886 return error("expected a MDNode after 'pcsections'");
3887 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3888 Token.is(MIToken::lbrace))
3889 return false;
3890 if (Token.isNot(MIToken::comma))
3891 return error("expected ',' before the next machine operand");
3892 lex();
3893 return false;
3894}
3895
3896bool MIParser::parseMMRA(MDNode *&Node) {
3897 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3898 lex();
3899 if (parseMDNode(Node))
3900 return true;
3901 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3902 Token.is(MIToken::lbrace))
3903 return false;
3904 if (Token.isNot(MIToken::comma))
3905 return error("expected ',' before the next machine operand");
3906 lex();
3907 return false;
3908}
3909
3911 const Function &F,
3912 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3913 ModuleSlotTracker MST(F.getParent());
3915 for (const auto &BB : F) {
3916 if (BB.hasName())
3917 continue;
3918 int Slot = MST.getLocalSlot(&BB);
3919 if (Slot == -1)
3920 continue;
3921 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3922 }
3923}
3924
3926 unsigned Slot,
3927 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3928 return Slots2BasicBlocks.lookup(Slot);
3929}
3930
3931const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3932 if (Slots2BasicBlocks.empty())
3933 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3934 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3935}
3936
3937const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3938 if (&F == &MF.getFunction())
3939 return getIRBlock(Slot);
3940 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3941 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3942 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3943}
3944
3945MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3946 // FIXME: Currently we can't recognize temporary or local symbols and call all
3947 // of the appropriate forms to create them. However, this handles basic cases
3948 // well as most of the special aspects are recognized by a prefix on their
3949 // name, and the input names should already be unique. For test cases, keeping
3950 // the symbol name out of the symbol table isn't terribly important.
3951 return MF.getContext().getOrCreateSymbol(Name);
3952}
3953
3954bool MIParser::parseStringConstant(std::string &Result) {
3955 if (Token.isNot(MIToken::StringConstant))
3956 return error("expected string constant");
3957 Result = std::string(Token.stringValue());
3958 lex();
3959 return false;
3960}
3961
3963 StringRef Src,
3965 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3966}
3967
3970 return MIParser(PFS, Error, Src).parseBasicBlocks();
3971}
3972
3976 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3977}
3978
3980 Register &Reg, StringRef Src,
3982 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3983}
3984
3986 Register &Reg, StringRef Src,
3988 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3989}
3990
3992 VRegInfo *&Info, StringRef Src,
3994 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3995}
3996
3999 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
4000}
4001
4005 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
4006}
4009 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
4010}
4011
4013 PerFunctionMIParsingState &PFS, const Value *&V,
4014 ErrorCallbackType ErrorCallback) {
4015 MIToken Token;
4016 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
4017 ErrorCallback(Loc, Msg);
4018 });
4019 V = nullptr;
4020
4021 return ::parseIRValue(Token, PFS, V, ErrorCallback);
4022}
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:62
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
static constexpr BranchProbability getRaw(uint32_t N)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
This is an important base class in LLVM.
Definition Constant.h:44
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h: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:370
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:638
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:800
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:735
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:790
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:728
static MCCFIInstruction createSetRAState(MCSymbol *L, unsigned State, MCSymbol *PACSym=nullptr, SMLoc Loc={})
.cfi_set_ra_state AArch64 set RA sign state,
Definition MCDwarf.h:711
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:663
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:811
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:688
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:631
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:673
static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
Definition MCDwarf.h:704
static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state AArch64 negate RA state.
Definition MCDwarf.h:699
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:748
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:780
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:646
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:759
static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc={})
.cfi_window_save SPARC register window is saved.
Definition MCDwarf.h:694
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:654
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:753
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:742
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:681
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