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;
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 parseSymbolicInlineAsmOperand(unsigned OpIdx, MachineOperand &Dest);
458 bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
459 const Constant *&C);
460 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
461 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
462 bool parseTypedImmediateOperand(MachineOperand &Dest);
463 bool parseFPImmediateOperand(MachineOperand &Dest);
464 bool parseMBBReference(MachineBasicBlock *&MBB);
465 bool parseMBBOperand(MachineOperand &Dest);
466 bool parseStackFrameIndex(int &FI);
467 bool parseStackObjectOperand(MachineOperand &Dest);
468 bool parseFixedStackFrameIndex(int &FI);
469 bool parseFixedStackObjectOperand(MachineOperand &Dest);
470 bool parseGlobalValue(GlobalValue *&GV);
471 bool parseGlobalAddressOperand(MachineOperand &Dest);
472 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
473 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
474 bool parseJumpTableIndexOperand(MachineOperand &Dest);
475 bool parseExternalSymbolOperand(MachineOperand &Dest);
476 bool parseMCSymbolOperand(MachineOperand &Dest);
477 [[nodiscard]] bool parseMDNode(MDNode *&Node);
478 bool parseDIExpression(MDNode *&Expr);
479 bool parseDILocation(MDNode *&Expr);
480 bool parseMetadataOperand(MachineOperand &Dest);
481 bool parseCFIOffset(int &Offset);
482 bool parseCFIUnsigned(unsigned &Value);
483 bool parseCFIRegister(unsigned &Reg);
484 bool parseCFIAddressSpace(unsigned &AddressSpace);
485 bool parseCFIEscapeValues(std::string& Values);
486 bool parseCFIOperand(MachineOperand &Dest);
487 bool parseIRBlock(BasicBlock *&BB, const Function &F);
488 bool parseBlockAddressOperand(MachineOperand &Dest);
489 bool parseIntrinsicOperand(MachineOperand &Dest);
490 bool parsePredicateOperand(MachineOperand &Dest);
491 bool parseShuffleMaskOperand(MachineOperand &Dest);
492 bool parseTargetIndexOperand(MachineOperand &Dest);
493 bool parseDbgInstrRefOperand(MachineOperand &Dest);
494 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
495 bool parseLaneMaskOperand(MachineOperand &Dest);
496 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
497 bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
498 MachineOperand &Dest,
499 std::optional<unsigned> &TiedDefIdx);
500 bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
501 const unsigned OpIdx,
502 MachineOperand &Dest,
503 std::optional<unsigned> &TiedDefIdx);
504 bool parseOffset(int64_t &Offset);
505 bool parseIRBlockAddressTaken(BasicBlock *&BB);
506 bool parseAlignment(uint64_t &Alignment);
507 bool parseAddrspace(unsigned &Addrspace);
508 bool parseSectionID(std::optional<MBBSectionID> &SID);
509 bool parseBBID(std::optional<UniqueBBID> &BBID);
510 bool parseCallFrameSize(unsigned &CallFrameSize);
511 bool parseMaxBytesForAlignment(unsigned &MaxBytesForAlignment);
512 bool parsePrefetchTarget(CallsiteID &Target);
513 bool parseOperandsOffset(MachineOperand &Op);
514 bool parseIRValue(const Value *&V);
515 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
516 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
517 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
518 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
519 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
520 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
521 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
522 bool parseHeapAllocMarker(MDNode *&Node);
523 bool parsePCSections(MDNode *&Node);
524 bool parseMMRA(MDNode *&Node);
525
526 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
527 MachineOperand &Dest, const MIRFormatter &MF);
528
529private:
530 /// Convert the integer literal in the current token into an unsigned integer.
531 ///
532 /// Return true if an error occurred.
533 bool getUnsigned(unsigned &Result);
534
535 /// Convert the integer literal in the current token into an uint64.
536 ///
537 /// Return true if an error occurred.
538 bool getUint64(uint64_t &Result);
539
540 /// Convert the hexadecimal literal in the current token into an unsigned
541 /// APInt with a minimum bitwidth required to represent the value.
542 ///
543 /// Return true if the literal does not represent an integer value.
544 bool getHexUint(APInt &Result);
545
546 /// If the current token is of the given kind, consume it and return false.
547 /// Otherwise report an error and return true.
548 bool expectAndConsume(MIToken::TokenKind TokenKind);
549
550 /// If the current token is of the given kind, consume it and return true.
551 /// Otherwise return false.
552 bool consumeIfPresent(MIToken::TokenKind TokenKind);
553
554 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
555
556 bool assignRegisterTies(MachineInstr &MI,
558
559 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
560 const MCInstrDesc &MCID);
561
562 const BasicBlock *getIRBlock(unsigned Slot);
563 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
564
565 /// Get or create an MCSymbol for a given name.
566 MCSymbol *getOrCreateMCSymbol(StringRef Name);
567
568 /// parseStringConstant
569 /// ::= StringConstant
570 bool parseStringConstant(std::string &Result);
571};
572
573} // end anonymous namespace
574
575MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
576 StringRef Source)
577 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
578{}
579
580void MIParser::lex(unsigned SkipChar) {
581 CurrentSource = lexMIToken(
582 CurrentSource.substr(SkipChar), Token,
583 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
584}
585
586bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
587
588bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
589 const SourceMgr &SM = *PFS.SM;
590 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
591 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
592 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
593 // Create an ordinary diagnostic when the source manager's buffer is the
594 // source string.
596 return true;
597 }
598 // Create a diagnostic for a YAML string literal.
599 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
600 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
601 Source, {}, {});
602 return true;
603}
604
605typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
607
608static const char *toString(MIToken::TokenKind TokenKind) {
609 switch (TokenKind) {
610 case MIToken::comma:
611 return "','";
612 case MIToken::equal:
613 return "'='";
614 case MIToken::colon:
615 return "':'";
616 case MIToken::lparen:
617 return "'('";
618 case MIToken::rparen:
619 return "')'";
620 default:
621 return "<unknown token>";
622 }
623}
624
625bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
626 if (Token.isNot(TokenKind))
627 return error(Twine("expected ") + toString(TokenKind));
628 lex();
629 return false;
630}
631
632bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
633 if (Token.isNot(TokenKind))
634 return false;
635 lex();
636 return true;
637}
638
639// Parse Machine Basic Block Section ID.
640bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
642 lex();
643 if (Token.is(MIToken::IntegerLiteral)) {
644 unsigned Value = 0;
645 if (getUnsigned(Value))
646 return error("Unknown Section ID");
647 SID = MBBSectionID{Value};
648 } else {
649 const StringRef &S = Token.stringValue();
650 if (S == "Exception")
652 else if (S == "Cold")
654 else
655 return error("Unknown Section ID");
656 }
657 lex();
658 return false;
659}
660
661// Parse Machine Basic Block ID.
662bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) {
663 if (Token.isNot(MIToken::kw_bb_id))
664 return error("expected 'bb_id'");
665 lex();
666 unsigned BaseID = 0;
667 unsigned CloneID = 0;
668 if (Token.is(MIToken::FloatingPointLiteral)) {
669 StringRef S = Token.range();
670 auto Parts = S.split('.');
671 if (Parts.first.getAsInteger(10, BaseID) ||
672 Parts.second.getAsInteger(10, CloneID))
673 return error("Unknown BB ID");
674 lex();
675 } else {
676 if (getUnsigned(BaseID))
677 return error("Unknown BB ID");
678 lex();
679 if (Token.is(MIToken::comma) || Token.is(MIToken::dot)) {
680 lex();
681 if (getUnsigned(CloneID))
682 return error("Unknown Clone ID");
683 lex();
684 } else if (Token.is(MIToken::IntegerLiteral)) {
685 if (getUnsigned(CloneID))
686 return error("Unknown Clone ID");
687 lex();
688 }
689 }
690 BBID = {BaseID, CloneID};
691 return false;
692}
693
694// Parse basic block call frame size.
695bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) {
697 lex();
698 unsigned Value = 0;
699 if (getUnsigned(Value))
700 return error("Unknown call frame size");
701 CallFrameSize = Value;
702 lex();
703 return false;
704}
705
706// Parse the maximum number of bytes permitted for basic block alignment
707// padding.
708bool MIParser::parseMaxBytesForAlignment(unsigned &MaxBytesForAlignment) {
710 lex();
711 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
712 return error("expected an integer literal after 'max-bytes-for-alignment'");
713 unsigned Value = 0;
714 if (getUnsigned(Value))
715 return true;
716 MaxBytesForAlignment = Value;
717 lex();
718 return false;
719}
720
721bool MIParser::parsePrefetchTarget(CallsiteID &Target) {
722 lex();
723 std::optional<UniqueBBID> BBID;
724 if (parseBBID(BBID))
725 return true;
726 Target.BBID = *BBID;
727 if (expectAndConsume(MIToken::comma))
728 return true;
729 return getUnsigned(Target.CallsiteIndex);
730}
731
732bool MIParser::parseBasicBlockDefinition(
735 unsigned ID = 0;
736 if (getUnsigned(ID))
737 return true;
738 auto Loc = Token.location();
739 auto Name = Token.stringValue();
740 lex();
741 bool MachineBlockAddressTaken = false;
742 BasicBlock *AddressTakenIRBlock = nullptr;
743 bool IsLandingPad = false;
744 bool IsInlineAsmBrIndirectTarget = false;
745 bool IsEHFuncletEntry = false;
746 bool IsEHScopeEntry = false;
747 bool IsCleanupFuncletEntry = false;
748 bool IsEHContTarget = false;
749 std::optional<MBBSectionID> SectionID;
751 unsigned MaxBytesForAlignment = 0;
752 std::optional<UniqueBBID> BBID;
753 unsigned CallFrameSize = 0;
754 BasicBlock *BB = nullptr;
755 if (consumeIfPresent(MIToken::lparen)) {
756 do {
757 // TODO: Report an error when multiple same attributes are specified.
758 switch (Token.kind()) {
760 MachineBlockAddressTaken = true;
761 lex();
762 break;
764 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
765 return true;
766 break;
768 IsLandingPad = true;
769 lex();
770 break;
772 IsInlineAsmBrIndirectTarget = true;
773 lex();
774 break;
776 IsEHFuncletEntry = true;
777 lex();
778 break;
780 IsEHScopeEntry = true;
781 lex();
782 break;
784 IsCleanupFuncletEntry = true;
785 lex();
786 break;
788 IsEHContTarget = true;
789 lex();
790 break;
792 if (parseAlignment(Alignment))
793 return true;
794 break;
796 if (parseMaxBytesForAlignment(MaxBytesForAlignment))
797 return true;
798 break;
799 case MIToken::IRBlock:
801 // TODO: Report an error when both name and ir block are specified.
802 if (parseIRBlock(BB, MF.getFunction()))
803 return true;
804 lex();
805 break;
807 if (parseSectionID(SectionID))
808 return true;
809 break;
811 if (parseBBID(BBID))
812 return true;
813 break;
815 if (parseCallFrameSize(CallFrameSize))
816 return true;
817 break;
818 default:
819 break;
820 }
821 } while (consumeIfPresent(MIToken::comma));
822 if (expectAndConsume(MIToken::rparen))
823 return true;
824 }
825 if (expectAndConsume(MIToken::colon))
826 return true;
827
828 if (!Name.empty()) {
830 MF.getFunction().getValueSymbolTable()->lookup(Name));
831 if (!BB)
832 return error(Loc, Twine("basic block '") + Name +
833 "' is not defined in the function '" +
834 MF.getName() + "'");
835 }
836 auto *MBB = MF.CreateMachineBasicBlock(BB, BBID);
837 MF.insert(MF.end(), MBB);
838 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
839 if (!WasInserted)
840 return error(Loc, Twine("redefinition of machine basic block with id #") +
841 Twine(ID));
842 if (Alignment)
843 MBB->setAlignment(Align(Alignment));
844 else if (MaxBytesForAlignment)
845 return error(Loc, "'max-bytes-for-alignment' requires 'align'");
846 MBB->setMaxBytesForAlignment(MaxBytesForAlignment);
847 if (MachineBlockAddressTaken)
849 if (AddressTakenIRBlock)
850 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
851 MBB->setIsEHPad(IsLandingPad);
852 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
853 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
854 MBB->setIsEHScopeEntry(IsEHScopeEntry);
855 MBB->setIsCleanupFuncletEntry(IsCleanupFuncletEntry);
856 MBB->setIsEHContTarget(IsEHContTarget);
857 if (SectionID) {
858 MBB->setSectionID(*SectionID);
859 MF.setBBSectionsType(BasicBlockSection::List);
860 }
861 MBB->setCallFrameSize(CallFrameSize);
862 return false;
863}
864
865bool MIParser::parseBasicBlockDefinitions(
867 lex();
868 // Skip until the first machine basic block.
869 while (Token.is(MIToken::Newline))
870 lex();
871 if (Token.isErrorOrEOF())
872 return Token.isError();
873 if (Token.isNot(MIToken::MachineBasicBlockLabel))
874 return error("expected a basic block definition before instructions");
875 unsigned BraceDepth = 0;
876 do {
877 if (parseBasicBlockDefinition(MBBSlots))
878 return true;
879 bool IsAfterNewline = false;
880 // Skip until the next machine basic block.
881 while (true) {
882 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
883 Token.isErrorOrEOF())
884 break;
885 else if (Token.is(MIToken::MachineBasicBlockLabel))
886 return error("basic block definition should be located at the start of "
887 "the line");
888 else if (consumeIfPresent(MIToken::Newline)) {
889 IsAfterNewline = true;
890 continue;
891 }
892 IsAfterNewline = false;
893 if (Token.is(MIToken::lbrace))
894 ++BraceDepth;
895 if (Token.is(MIToken::rbrace)) {
896 if (!BraceDepth)
897 return error("extraneous closing brace ('}')");
898 --BraceDepth;
899 }
900 lex();
901 }
902 // Verify that we closed all of the '{' at the end of a file or a block.
903 if (!Token.isError() && BraceDepth)
904 return error("expected '}'"); // FIXME: Report a note that shows '{'.
905 } while (!Token.isErrorOrEOF());
906 return Token.isError();
907}
908
909bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
910 assert(Token.is(MIToken::kw_liveins));
911 lex();
912 if (expectAndConsume(MIToken::colon))
913 return true;
914 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
915 return false;
916 do {
917 if (Token.isNot(MIToken::NamedRegister))
918 return error("expected a named register");
920 if (parseNamedRegister(Reg))
921 return true;
922 lex();
924 if (consumeIfPresent(MIToken::colon)) {
925 // Parse lane mask.
926 if (Token.isNot(MIToken::IntegerLiteral) &&
927 Token.isNot(MIToken::HexLiteral))
928 return error("expected a lane mask");
929 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
930 "Use correct get-function for lane mask");
932 if (getUint64(V))
933 return error("invalid lane mask value");
934 Mask = LaneBitmask(V);
935 lex();
936 }
937 MBB.addLiveIn(Reg, Mask);
938 } while (consumeIfPresent(MIToken::comma));
939 return false;
940}
941
942bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
944 lex();
945 if (expectAndConsume(MIToken::colon))
946 return true;
947 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
948 return false;
949 do {
950 if (Token.isNot(MIToken::MachineBasicBlock))
951 return error("expected a machine basic block reference");
952 MachineBasicBlock *SuccMBB = nullptr;
953 if (parseMBBReference(SuccMBB))
954 return true;
955 lex();
956 unsigned Weight = 0;
957 if (consumeIfPresent(MIToken::lparen)) {
958 if (Token.isNot(MIToken::IntegerLiteral) &&
959 Token.isNot(MIToken::HexLiteral))
960 return error("expected an integer literal after '('");
961 if (getUnsigned(Weight))
962 return true;
963 lex();
964 if (expectAndConsume(MIToken::rparen))
965 return true;
966 }
968 } while (consumeIfPresent(MIToken::comma));
970 return false;
971}
972
973bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
974 MachineBasicBlock *&AddFalthroughFrom) {
975 // Skip the definition.
977 lex();
978 if (consumeIfPresent(MIToken::lparen)) {
979 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
980 lex();
981 consumeIfPresent(MIToken::rparen);
982 }
983 consumeIfPresent(MIToken::colon);
984
985 // Parse the liveins and successors.
986 // N.B: Multiple lists of successors and liveins are allowed and they're
987 // merged into one.
988 // Example:
989 // liveins: $edi
990 // liveins: $esi
991 //
992 // is equivalent to
993 // liveins: $edi, $esi
994 bool ExplicitSuccessors = false;
995 while (true) {
996 if (Token.is(MIToken::kw_successors)) {
997 if (parseBasicBlockSuccessors(MBB))
998 return true;
999 ExplicitSuccessors = true;
1000 } else if (Token.is(MIToken::kw_liveins)) {
1001 if (parseBasicBlockLiveins(MBB))
1002 return true;
1003 } else if (consumeIfPresent(MIToken::Newline)) {
1004 continue;
1005 } else {
1006 break;
1007 }
1008 if (!Token.isNewlineOrEOF())
1009 return error("expected line break at the end of a list");
1010 lex();
1011 }
1012
1013 // Parse the instructions.
1014 bool IsInBundle = false;
1015 MachineInstr *PrevMI = nullptr;
1016 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
1017 !Token.is(MIToken::Eof)) {
1018 if (consumeIfPresent(MIToken::Newline))
1019 continue;
1020 if (consumeIfPresent(MIToken::rbrace)) {
1021 // The first parsing pass should verify that all closing '}' have an
1022 // opening '{'.
1023 assert(IsInBundle);
1024 IsInBundle = false;
1025 continue;
1026 }
1027 MachineInstr *MI = nullptr;
1028 if (parse(MI))
1029 return true;
1030 MBB.insert(MBB.end(), MI);
1031 if (IsInBundle) {
1033 MI->setFlag(MachineInstr::BundledPred);
1034 }
1035 PrevMI = MI;
1036 if (Token.is(MIToken::lbrace)) {
1037 if (IsInBundle)
1038 return error("nested instruction bundles are not allowed");
1039 lex();
1040 // This instruction is the start of the bundle.
1041 MI->setFlag(MachineInstr::BundledSucc);
1042 IsInBundle = true;
1043 if (!Token.is(MIToken::Newline))
1044 // The next instruction can be on the same line.
1045 continue;
1046 }
1047 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
1048 lex();
1049 }
1050
1051 // Construct successor list by searching for basic block machine operands.
1052 if (!ExplicitSuccessors) {
1054 bool IsFallthrough;
1055 guessSuccessors(MBB, Successors, IsFallthrough);
1056 for (MachineBasicBlock *Succ : Successors)
1057 MBB.addSuccessor(Succ);
1058
1059 if (IsFallthrough) {
1060 AddFalthroughFrom = &MBB;
1061 } else {
1063 }
1064 }
1065
1066 return false;
1067}
1068
1069bool MIParser::parseBasicBlocks() {
1070 lex();
1071 // Skip until the first machine basic block.
1072 while (Token.is(MIToken::Newline))
1073 lex();
1074 if (Token.isErrorOrEOF())
1075 return Token.isError();
1076 // The first parsing pass should have verified that this token is a MBB label
1077 // in the 'parseBasicBlockDefinitions' method.
1079 MachineBasicBlock *AddFalthroughFrom = nullptr;
1080 do {
1081 MachineBasicBlock *MBB = nullptr;
1083 return true;
1084 if (AddFalthroughFrom) {
1085 if (!AddFalthroughFrom->isSuccessor(MBB))
1086 AddFalthroughFrom->addSuccessor(MBB);
1087 AddFalthroughFrom->normalizeSuccProbs();
1088 AddFalthroughFrom = nullptr;
1089 }
1090 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1091 return true;
1092 // The method 'parseBasicBlock' should parse the whole block until the next
1093 // block or the end of file.
1094 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1095 } while (Token.isNot(MIToken::Eof));
1096 return false;
1097}
1098
1099bool MIParser::parse(MachineInstr *&MI) {
1100 // Parse any register operands before '='
1103 while (Token.isRegister() || Token.isRegisterFlag()) {
1104 auto Loc = Token.location();
1105 std::optional<unsigned> TiedDefIdx;
1106 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1107 return true;
1108 Operands.push_back(
1109 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1110 if (Token.isNot(MIToken::comma))
1111 break;
1112 lex();
1113 }
1114 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1115 return true;
1116
1117 unsigned OpCode, Flags = 0;
1118 if (Token.isError() || parseInstruction(OpCode, Flags))
1119 return true;
1120
1121 // Parse the remaining machine operands.
1122 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1123 Token.isNot(MIToken::kw_post_instr_symbol) &&
1124 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1125 Token.isNot(MIToken::kw_pcsections) && Token.isNot(MIToken::kw_mmra) &&
1126 Token.isNot(MIToken::kw_cfi_type) &&
1127 Token.isNot(MIToken::kw_deactivation_symbol) &&
1128 Token.isNot(MIToken::kw_debug_location) &&
1129 Token.isNot(MIToken::kw_debug_instr_number) &&
1130 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1131 auto Loc = Token.location();
1132 std::optional<unsigned> TiedDefIdx;
1133 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1134 return true;
1135 Operands.push_back(
1136 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1137 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1138 Token.is(MIToken::lbrace))
1139 break;
1140 if (Token.isNot(MIToken::comma))
1141 return error("expected ',' before the next machine operand");
1142 lex();
1143 }
1144
1145 MCSymbol *PreInstrSymbol = nullptr;
1146 if (Token.is(MIToken::kw_pre_instr_symbol))
1147 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1148 return true;
1149 MCSymbol *PostInstrSymbol = nullptr;
1150 if (Token.is(MIToken::kw_post_instr_symbol))
1151 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1152 return true;
1153 MDNode *HeapAllocMarker = nullptr;
1154 if (Token.is(MIToken::kw_heap_alloc_marker))
1155 if (parseHeapAllocMarker(HeapAllocMarker))
1156 return true;
1157 MDNode *PCSections = nullptr;
1158 if (Token.is(MIToken::kw_pcsections))
1159 if (parsePCSections(PCSections))
1160 return true;
1161 MDNode *MMRA = nullptr;
1162 if (Token.is(MIToken::kw_mmra) && parseMMRA(MMRA))
1163 return true;
1164 unsigned CFIType = 0;
1165 if (Token.is(MIToken::kw_cfi_type)) {
1166 lex();
1167 if (Token.isNot(MIToken::IntegerLiteral))
1168 return error("expected an integer literal after 'cfi-type'");
1169 // getUnsigned is sufficient for 32-bit integers.
1170 if (getUnsigned(CFIType))
1171 return true;
1172 lex();
1173 // Lex past trailing comma if present.
1174 if (Token.is(MIToken::comma))
1175 lex();
1176 }
1177
1178 GlobalValue *DS = nullptr;
1179 if (Token.is(MIToken::kw_deactivation_symbol)) {
1180 lex();
1181 if (parseGlobalValue(DS))
1182 return true;
1183 lex();
1184 }
1185
1186 unsigned InstrNum = 0;
1187 if (Token.is(MIToken::kw_debug_instr_number)) {
1188 lex();
1189 if (Token.isNot(MIToken::IntegerLiteral))
1190 return error("expected an integer literal after 'debug-instr-number'");
1191 if (getUnsigned(InstrNum))
1192 return true;
1193 lex();
1194 // Lex past trailing comma if present.
1195 if (Token.is(MIToken::comma))
1196 lex();
1197 }
1198
1199 DebugLoc DebugLocation;
1200 if (Token.is(MIToken::kw_debug_location)) {
1201 lex();
1202 MDNode *Node = nullptr;
1203 if (Token.is(MIToken::exclaim)) {
1204 if (parseMDNode(Node))
1205 return true;
1206 } else if (Token.is(MIToken::md_dilocation)) {
1207 if (parseDILocation(Node))
1208 return true;
1209 } else {
1210 return error("expected a metadata node after 'debug-location'");
1211 }
1212 DebugLocation = DebugLoc(dyn_cast<DILocation>(Node));
1213 if (!DebugLocation)
1214 return error("referenced metadata is not a DILocation");
1215 }
1216
1217 // Parse the machine memory operands.
1219 if (Token.is(MIToken::coloncolon)) {
1220 lex();
1221 while (!Token.isNewlineOrEOF()) {
1222 MachineMemOperand *MemOp = nullptr;
1223 if (parseMachineMemoryOperand(MemOp))
1224 return true;
1225 MemOperands.push_back(MemOp);
1226 if (Token.isNewlineOrEOF())
1227 break;
1228 if (OpCode == TargetOpcode::BUNDLE && Token.is(MIToken::lbrace))
1229 break;
1230 if (Token.isNot(MIToken::comma))
1231 return error("expected ',' before the next machine memory operand");
1232 lex();
1233 }
1234 }
1235
1236 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1237 if (!MCID.isVariadic()) {
1238 // FIXME: Move the implicit operand verification to the machine verifier.
1239 if (verifyImplicitOperands(Operands, MCID))
1240 return true;
1241 }
1242
1243 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1244 MI->setFlags(Flags);
1245
1246 // Don't check the operands make sense, let the verifier catch any
1247 // improprieties.
1248 for (const auto &Operand : Operands)
1249 MI->addOperand(MF, Operand.Operand);
1250
1251 if (assignRegisterTies(*MI, Operands))
1252 return true;
1253 if (PreInstrSymbol)
1254 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1255 if (PostInstrSymbol)
1256 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1257 if (HeapAllocMarker)
1258 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1259 if (PCSections)
1260 MI->setPCSections(MF, PCSections);
1261 if (MMRA)
1262 MI->setMMRAMetadata(MF, MMRA);
1263 if (CFIType)
1264 MI->setCFIType(MF, CFIType);
1265 if (DS)
1266 MI->setDeactivationSymbol(MF, DS);
1267 if (!MemOperands.empty())
1268 MI->setMemRefs(MF, MemOperands);
1269 if (InstrNum)
1270 MI->setDebugInstrNum(InstrNum);
1271 return false;
1272}
1273
1274bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1275 lex();
1276 if (Token.isNot(MIToken::MachineBasicBlock))
1277 return error("expected a machine basic block reference");
1279 return true;
1280 lex();
1281 if (Token.isNot(MIToken::Eof))
1282 return error(
1283 "expected end of string after the machine basic block reference");
1284 return false;
1285}
1286
1287bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1288 lex();
1289 if (Token.isNot(MIToken::NamedRegister))
1290 return error("expected a named register");
1291 if (parseNamedRegister(Reg))
1292 return true;
1293 lex();
1294 if (Token.isNot(MIToken::Eof))
1295 return error("expected end of string after the register reference");
1296 return false;
1297}
1298
1299bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1300 lex();
1301 if (Token.isNot(MIToken::VirtualRegister))
1302 return error("expected a virtual register");
1303 if (parseVirtualRegister(Info))
1304 return true;
1305 lex();
1306 if (Token.isNot(MIToken::Eof))
1307 return error("expected end of string after the register reference");
1308 return false;
1309}
1310
1311bool MIParser::parseStandaloneRegister(Register &Reg) {
1312 lex();
1313 if (Token.isNot(MIToken::NamedRegister) &&
1314 Token.isNot(MIToken::VirtualRegister))
1315 return error("expected either a named or virtual register");
1316
1317 VRegInfo *Info;
1318 if (parseRegister(Reg, Info))
1319 return true;
1320
1321 lex();
1322 if (Token.isNot(MIToken::Eof))
1323 return error("expected end of string after the register reference");
1324 return false;
1325}
1326
1327bool MIParser::parseStandaloneStackObject(int &FI) {
1328 lex();
1329 if (Token.isNot(MIToken::StackObject))
1330 return error("expected a stack object");
1331 if (parseStackFrameIndex(FI))
1332 return true;
1333 if (Token.isNot(MIToken::Eof))
1334 return error("expected end of string after the stack object reference");
1335 return false;
1336}
1337
1338bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1339 lex();
1340 if (Token.is(MIToken::exclaim)) {
1341 if (parseMDNode(Node))
1342 return true;
1343 } else if (Token.is(MIToken::md_diexpr)) {
1344 if (parseDIExpression(Node))
1345 return true;
1346 } else if (Token.is(MIToken::md_dilocation)) {
1347 if (parseDILocation(Node))
1348 return true;
1349 } else {
1350 return error("expected a metadata node");
1351 }
1352 if (Token.isNot(MIToken::Eof))
1353 return error("expected end of string after the metadata node");
1354 return false;
1355}
1356
1357static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1358 assert(MO.isImplicit());
1359 return MO.isDef() ? "implicit-def" : "implicit";
1360}
1361
1362static std::string getRegisterName(const TargetRegisterInfo *TRI,
1363 Register Reg) {
1364 assert(Reg.isPhysical() && "expected phys reg");
1365 return StringRef(TRI->getName(Reg)).lower();
1366}
1367
1368/// Return true if the parsed machine operands contain a given machine operand.
1369static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1371 for (const auto &I : Operands) {
1372 if (ImplicitOperand.isIdenticalTo(I.Operand))
1373 return true;
1374 }
1375 return false;
1376}
1377
1378bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1379 const MCInstrDesc &MCID) {
1380 if (MCID.isCall())
1381 // We can't verify call instructions as they can contain arbitrary implicit
1382 // register and register mask operands.
1383 return false;
1384
1385 // Gather all the expected implicit operands.
1386 SmallVector<MachineOperand, 4> ImplicitOperands;
1387 for (MCPhysReg ImpDef : MCID.implicit_defs())
1388 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1389 for (MCPhysReg ImpUse : MCID.implicit_uses())
1390 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1391
1392 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1393 assert(TRI && "Expected target register info");
1394 for (const auto &I : ImplicitOperands) {
1396 continue;
1397 return error(Operands.empty() ? Token.location() : Operands.back().End,
1398 Twine("missing implicit register operand '") +
1400 getRegisterName(TRI, I.getReg()) + "'");
1401 }
1402 return false;
1403}
1404
1405bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1406 // Allow frame and fast math flags for OPCODE
1407 // clang-format off
1408 while (Token.is(MIToken::kw_frame_setup) ||
1409 Token.is(MIToken::kw_frame_destroy) ||
1410 Token.is(MIToken::kw_nnan) ||
1411 Token.is(MIToken::kw_ninf) ||
1412 Token.is(MIToken::kw_nsz) ||
1413 Token.is(MIToken::kw_arcp) ||
1414 Token.is(MIToken::kw_contract) ||
1415 Token.is(MIToken::kw_afn) ||
1416 Token.is(MIToken::kw_reassoc) ||
1417 Token.is(MIToken::kw_nuw) ||
1418 Token.is(MIToken::kw_nsw) ||
1419 Token.is(MIToken::kw_exact) ||
1420 Token.is(MIToken::kw_nofpexcept) ||
1421 Token.is(MIToken::kw_noconvergent) ||
1422 Token.is(MIToken::kw_unpredictable) ||
1423 Token.is(MIToken::kw_nneg) ||
1424 Token.is(MIToken::kw_disjoint) ||
1425 Token.is(MIToken::kw_nusw) ||
1426 Token.is(MIToken::kw_samesign) ||
1427 Token.is(MIToken::kw_inbounds) ||
1428 Token.is(MIToken::kw_nonnull) ||
1429 Token.is(MIToken::kw_lr_split)) {
1430 // clang-format on
1431 // Mine frame and fast math flags
1432 if (Token.is(MIToken::kw_frame_setup))
1434 if (Token.is(MIToken::kw_frame_destroy))
1436 if (Token.is(MIToken::kw_nnan))
1438 if (Token.is(MIToken::kw_ninf))
1440 if (Token.is(MIToken::kw_nsz))
1442 if (Token.is(MIToken::kw_arcp))
1444 if (Token.is(MIToken::kw_contract))
1446 if (Token.is(MIToken::kw_afn))
1448 if (Token.is(MIToken::kw_reassoc))
1450 if (Token.is(MIToken::kw_nuw))
1452 if (Token.is(MIToken::kw_nsw))
1454 if (Token.is(MIToken::kw_exact))
1456 if (Token.is(MIToken::kw_nofpexcept))
1458 if (Token.is(MIToken::kw_unpredictable))
1460 if (Token.is(MIToken::kw_noconvergent))
1462 if (Token.is(MIToken::kw_nneg))
1464 if (Token.is(MIToken::kw_disjoint))
1466 if (Token.is(MIToken::kw_nusw))
1468 if (Token.is(MIToken::kw_samesign))
1470 if (Token.is(MIToken::kw_inbounds))
1472 if (Token.is(MIToken::kw_nonnull))
1474 if (Token.is(MIToken::kw_lr_split))
1476
1477 lex();
1478 }
1479 if (Token.isNot(MIToken::Identifier))
1480 return error("expected a machine instruction");
1481 StringRef InstrName = Token.stringValue();
1482 if (PFS.Target.parseInstrName(InstrName, OpCode))
1483 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1484 lex();
1485 return false;
1486}
1487
1488bool MIParser::parseNamedRegister(Register &Reg) {
1489 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1490 StringRef Name = Token.stringValue();
1491 if (PFS.Target.getRegisterByName(Name, Reg))
1492 return error(Twine("unknown register name '") + Name + "'");
1493 return false;
1494}
1495
1496bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1497 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1498 StringRef Name = Token.stringValue();
1499 // TODO: Check that the VReg name is not the same as a physical register name.
1500 // If it is, then print a warning (when warnings are implemented).
1501 Info = &PFS.getVRegInfoNamed(Name);
1502 return false;
1503}
1504
1505bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1506 if (Token.is(MIToken::NamedVirtualRegister))
1507 return parseNamedVirtualRegister(Info);
1508 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1509 unsigned ID;
1510 if (getUnsigned(ID))
1511 return true;
1512 Info = &PFS.getVRegInfo(ID);
1513 return false;
1514}
1515
1516bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1517 switch (Token.kind()) {
1519 Reg = 0;
1520 return false;
1522 return parseNamedRegister(Reg);
1525 if (parseVirtualRegister(Info))
1526 return true;
1527 Reg = Info->VReg;
1528 return false;
1529 // TODO: Parse other register kinds.
1530 default:
1531 llvm_unreachable("The current token should be a register");
1532 }
1533}
1534
1535bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1536 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1537 return error("expected '_', register class, or register bank name");
1538 StringRef::iterator Loc = Token.location();
1539 StringRef Name = Token.stringValue();
1540
1541 // Was it a register class?
1542 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1543 if (RC) {
1544 lex();
1545
1546 switch (RegInfo.Kind) {
1547 case VRegInfo::UNKNOWN:
1548 case VRegInfo::NORMAL:
1549 RegInfo.Kind = VRegInfo::NORMAL;
1550 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1551 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1552 return error(Loc, Twine("conflicting register classes, previously: ") +
1553 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1554 }
1555 RegInfo.D.RC = RC;
1556 RegInfo.Explicit = true;
1557 return false;
1558
1559 case VRegInfo::GENERIC:
1560 case VRegInfo::REGBANK:
1561 return error(Loc, "register class specification on generic register");
1562 }
1563 llvm_unreachable("Unexpected register kind");
1564 }
1565
1566 // Should be a register bank or a generic register.
1567 const RegisterBank *RegBank = nullptr;
1568 if (Name != "_") {
1569 RegBank = PFS.Target.getRegBank(Name);
1570 if (!RegBank)
1571 return error(Loc, "expected '_', register class, or register bank name");
1572 }
1573
1574 lex();
1575
1576 switch (RegInfo.Kind) {
1577 case VRegInfo::UNKNOWN:
1578 case VRegInfo::GENERIC:
1579 case VRegInfo::REGBANK:
1580 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1581 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1582 return error(Loc, "conflicting generic register banks");
1583 RegInfo.D.RegBank = RegBank;
1584 RegInfo.Explicit = true;
1585 return false;
1586
1587 case VRegInfo::NORMAL:
1588 return error(Loc, "register bank specification on normal register");
1589 }
1590 llvm_unreachable("Unexpected register kind");
1591}
1592
1593bool MIParser::parseRegisterFlag(RegState &Flags) {
1594 const RegState OldFlags = Flags;
1595 switch (Token.kind()) {
1598 break;
1601 break;
1602 case MIToken::kw_def:
1604 break;
1605 case MIToken::kw_dead:
1607 break;
1608 case MIToken::kw_killed:
1610 break;
1611 case MIToken::kw_undef:
1613 break;
1616 break;
1619 break;
1622 break;
1625 break;
1626 default:
1627 llvm_unreachable("The current token should be a register flag");
1628 }
1629 if (OldFlags == Flags)
1630 // We know that the same flag is specified more than once when the flags
1631 // weren't modified.
1632 return error("duplicate '" + Token.stringValue() + "' register flag");
1633 lex();
1634 return false;
1635}
1636
1637bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1638 assert(Token.is(MIToken::dot));
1639 lex();
1640 if (Token.isNot(MIToken::Identifier))
1641 return error("expected a subregister index after '.'");
1642 auto Name = Token.stringValue();
1643 SubReg = PFS.Target.getSubRegIndex(Name);
1644 if (!SubReg)
1645 return error(Twine("use of unknown subregister index '") + Name + "'");
1646 lex();
1647 return false;
1648}
1649
1650bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1651 assert(Token.is(MIToken::kw_tied_def));
1652 lex();
1653 if (Token.isNot(MIToken::IntegerLiteral))
1654 return error("expected an integer literal after 'tied-def'");
1655 if (getUnsigned(TiedDefIdx))
1656 return true;
1657 lex();
1658 return expectAndConsume(MIToken::rparen);
1659}
1660
1661bool MIParser::assignRegisterTies(MachineInstr &MI,
1663 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1664 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1665 if (!Operands[I].TiedDefIdx)
1666 continue;
1667 // The parser ensures that this operand is a register use, so we just have
1668 // to check the tied-def operand.
1669 unsigned DefIdx = *Operands[I].TiedDefIdx;
1670 if (DefIdx >= E)
1671 return error(Operands[I].Begin,
1672 Twine("use of invalid tied-def operand index '" +
1673 Twine(DefIdx) + "'; instruction has only ") +
1674 Twine(E) + " operands");
1675 const auto &DefOperand = Operands[DefIdx].Operand;
1676 if (!DefOperand.isReg() || !DefOperand.isDef())
1677 // FIXME: add note with the def operand.
1678 return error(Operands[I].Begin,
1679 Twine("use of invalid tied-def operand index '") +
1680 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1681 " isn't a defined register");
1682 // Check that the tied-def operand wasn't tied elsewhere.
1683 for (const auto &TiedPair : TiedRegisterPairs) {
1684 if (TiedPair.first == DefIdx)
1685 return error(Operands[I].Begin,
1686 Twine("the tied-def operand #") + Twine(DefIdx) +
1687 " is already tied with another register operand");
1688 }
1689 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1690 }
1691 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1692 // indices must be less than tied max.
1693 for (const auto &TiedPair : TiedRegisterPairs)
1694 MI.tieOperands(TiedPair.first, TiedPair.second);
1695 return false;
1696}
1697
1698bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1699 std::optional<unsigned> &TiedDefIdx,
1700 bool IsDef) {
1701 RegState Flags = getDefRegState(IsDef);
1702 while (Token.isRegisterFlag()) {
1703 if (parseRegisterFlag(Flags))
1704 return true;
1705 }
1706 // Update IsDef as we may have read a def flag.
1707 IsDef = hasRegState(Flags, RegState::Define);
1708 if (!Token.isRegister())
1709 return error("expected a register after register flags");
1710 Register Reg;
1711 VRegInfo *RegInfo;
1712 if (parseRegister(Reg, RegInfo))
1713 return true;
1714 lex();
1715 unsigned SubReg = 0;
1716 if (Token.is(MIToken::dot)) {
1717 if (parseSubRegisterIndex(SubReg))
1718 return true;
1719 if (!Reg.isVirtual())
1720 return error("subregister index expects a virtual register");
1721 }
1722 if (Token.is(MIToken::colon)) {
1723 if (!Reg.isVirtual())
1724 return error("register class specification expects a virtual register");
1725 lex();
1726 if (parseRegisterClassOrBank(*RegInfo))
1727 return true;
1728 }
1729
1730 if (consumeIfPresent(MIToken::lparen)) {
1731 // For a def, we only expect a type. For use we expect either a type or a
1732 // tied-def. Additionally, for physical registers, we don't expect a type.
1733 if (Token.is(MIToken::kw_tied_def)) {
1734 if (IsDef)
1735 return error("tied-def not supported for defs");
1736 unsigned Idx;
1737 if (parseRegisterTiedDefIndex(Idx))
1738 return true;
1739 TiedDefIdx = Idx;
1740 } else {
1741 if (!Reg.isVirtual())
1742 return error("unexpected type on physical register");
1743
1744 LLT Ty;
1745 // If type parsing fails, forwad the parse error for defs.
1746 if (parseLowLevelType(Token.location(), Ty))
1747 return IsDef ? true
1748 : error("expected tied-def or low-level type after '('");
1749
1750 if (expectAndConsume(MIToken::rparen))
1751 return true;
1752
1753 MachineRegisterInfo &MRI = MF.getRegInfo();
1754 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1755 return error("inconsistent type for generic virtual register");
1756
1757 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1758 MRI.setType(Reg, Ty);
1760 }
1761 } else if (IsDef && Reg.isVirtual()) {
1762 // Generic virtual registers defs must have a type.
1763 if (RegInfo->Kind == VRegInfo::GENERIC ||
1764 RegInfo->Kind == VRegInfo::REGBANK)
1765 return error("generic virtual registers must have a type");
1766 }
1767
1768 if (IsDef) {
1769 if (hasRegState(Flags, RegState::Kill))
1770 return error("cannot have a killed def operand");
1771 } else {
1772 if (hasRegState(Flags, RegState::Dead))
1773 return error("cannot have a dead use operand");
1774 }
1775
1777 Reg, IsDef, hasRegState(Flags, RegState::Implicit),
1780 hasRegState(Flags, RegState::EarlyClobber), SubReg,
1784
1785 return false;
1786}
1787
1788bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1790 const APSInt &Int = Token.integerValue();
1791 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1792 Dest = MachineOperand::CreateImm(*SImm);
1793 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1794 Dest = MachineOperand::CreateImm(*UImm);
1795 else
1796 return error("integer literal is too large to be an immediate operand");
1797 lex();
1798 return false;
1799}
1800
1801bool MIParser::parseSymbolicInlineAsmOperand(unsigned OpIdx,
1802 MachineOperand &Dest) {
1804 assert(Token.is(MIToken::Identifier) &&
1805 "expected symbolic inline asm operand");
1806
1807 // Parse ExtraInfo flags.
1808 if (OpIdx == InlineAsm::MIOp_ExtraInfo) {
1809 unsigned ExtraInfo = 0;
1810 for (;;) {
1811 if (Token.isNot(MIToken::Identifier))
1812 break;
1813
1814 StringRef FlagName = Token.stringValue();
1815 unsigned Flag = StringSwitch<unsigned>(FlagName)
1817 .Case("mayload", InlineAsm::Extra_MayLoad)
1818 .Case("maystore", InlineAsm::Extra_MayStore)
1819 .Case("isconvergent", InlineAsm::Extra_IsConvergent)
1820 .Case("alignstack", InlineAsm::Extra_IsAlignStack)
1822 .Case("attdialect", 0)
1823 .Case("inteldialect", InlineAsm::Extra_AsmDialect)
1824 .Default(~0u);
1825 if (Flag == ~0u)
1826 return error("unknown inline asm extra info flag '" + FlagName + "'");
1827
1828 ExtraInfo |= Flag;
1829 lex();
1830 }
1831
1832 Dest = MachineOperand::CreateImm(ExtraInfo);
1833 return false;
1834 }
1835
1836 // Parse symbolic form: kind[:constraint].
1837 StringRef KindStr = Token.stringValue();
1838 constexpr auto InvalidKind = static_cast<InlineAsm::Kind>(0);
1841 .Case("regdef", InlineAsm::Kind::RegDef)
1842 .Case("reguse", InlineAsm::Kind::RegUse)
1844 .Case("clobber", InlineAsm::Kind::Clobber)
1845 .Case("imm", InlineAsm::Kind::Imm)
1846 .Case("mem", InlineAsm::Kind::Mem)
1847 .Default(InvalidKind);
1848 if (K == InvalidKind)
1849 return error("unknown inline asm operand kind '" + KindStr + "'");
1850
1851 lex();
1852
1853 // Create the flag with default of 1 operand.
1854 InlineAsm::Flag F(K, 1);
1855
1856 // Parse optional tiedto constraint: tiedto:$N.
1857 if (Token.is(MIToken::Identifier) && Token.stringValue() == "tiedto") {
1858 lex();
1859 if (Token.isNot(MIToken::colon))
1860 return error("expected ':' after 'tiedto'");
1861 lex();
1862 if (Token.isNot(MIToken::NamedRegister))
1863 return error("expected '$N' operand number after 'tiedto:'");
1864 unsigned OperandNo;
1865 if (Token.stringValue().getAsInteger(10, OperandNo))
1866 return error("invalid operand number in tiedto constraint");
1867 lex();
1868
1869 F.setMatchingOp(OperandNo);
1870
1872 return false;
1873 }
1874
1875 // Parse optional constraint after ':'.
1876 if (Token.isNot(MIToken::colon)) {
1878 return false;
1879 }
1880
1881 lex();
1882
1883 if (Token.isNot(MIToken::Identifier))
1884 return error("expected register class or memory constraint name after ':'");
1885
1886 StringRef ConstraintStr = Token.stringValue();
1887 if (K == InlineAsm::Kind::Mem) {
1920 return error("unknown memory constraint '" + ConstraintStr + "'");
1921 F.setMemConstraint(CC);
1922 } else if (K == InlineAsm::Kind::RegDef || K == InlineAsm::Kind::RegUse ||
1924 const TargetRegisterClass *RC =
1925 PFS.Target.getRegClass(ConstraintStr.lower());
1926 if (!RC)
1927 return error("unknown register class '" + ConstraintStr + "'");
1928 F.setRegClass(RC->getID());
1929 }
1930
1931 lex();
1932
1934 return false;
1935}
1936
1937bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1938 const unsigned OpIdx,
1939 MachineOperand &Dest,
1940 const MIRFormatter &MF) {
1941 assert(Token.is(MIToken::dot));
1942 auto Loc = Token.location(); // record start position
1943 size_t Len = 1; // for "."
1944 lex();
1945
1946 // Handle the case that mnemonic starts with number.
1947 if (Token.is(MIToken::IntegerLiteral)) {
1948 Len += Token.range().size();
1949 lex();
1950 }
1951
1952 StringRef Src;
1953 if (Token.is(MIToken::comma))
1954 Src = StringRef(Loc, Len);
1955 else {
1956 assert(Token.is(MIToken::Identifier));
1957 Src = StringRef(Loc, Len + Token.stringValue().size());
1958 }
1959 int64_t Val;
1960 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1961 [this](StringRef::iterator Loc, const Twine &Msg)
1962 -> bool { return error(Loc, Msg); }))
1963 return true;
1964
1965 Dest = MachineOperand::CreateImm(Val);
1966 if (!Token.is(MIToken::comma))
1967 lex();
1968 return false;
1969}
1970
1972 PerFunctionMIParsingState &PFS, const Constant *&C,
1973 ErrorCallbackType ErrCB) {
1974 auto Source = StringValue.str(); // The source has to be null terminated.
1975 SMDiagnostic Err;
1976 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1977 &PFS.IRSlots);
1978 if (!C)
1979 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1980 return false;
1981}
1982
1983bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1984 const Constant *&C) {
1985 return ::parseIRConstant(
1986 Loc, StringValue, PFS, C,
1987 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1988 return error(Loc, Msg);
1989 });
1990}
1991
1992bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1993 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1994 return true;
1995 lex();
1996 return false;
1997}
1998
1999// See LLT implementation for bit size limits.
2001 return Size != 0 && isUInt<16>(Size);
2002}
2003
2004static bool verifyVectorElementCount(uint64_t NumElts, bool HasVScale) {
2005 // A fixed-length vector needs at least two elements.
2006 return NumElts != 0 && (HasVScale || NumElts != 1) && isUInt<16>(NumElts);
2007}
2008
2009static bool verifyAddrSpace(uint64_t AddrSpace) {
2010 return isUInt<24>(AddrSpace);
2011}
2012
2013bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
2014 StringRef TypeDigits = Token.range();
2015 if (TypeDigits.consume_front("s") || TypeDigits.consume_front("i") ||
2016 TypeDigits.consume_front("f") || TypeDigits.consume_front("p") ||
2017 TypeDigits.consume_front("bf")) {
2018 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2019 return error(
2020 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2021 }
2022
2023 bool Scalar = Token.range().starts_with("s");
2024 if (Scalar || Token.range().starts_with("i")) {
2025 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2026 if (!ScalarSize) {
2027 Ty = LLT::token();
2028 lex();
2029 return false;
2030 }
2031
2032 if (!verifyScalarSize(ScalarSize))
2033 return error("invalid size for scalar type");
2034
2035 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2036 lex();
2037 return false;
2038 }
2039
2040 if (Token.range().starts_with("p")) {
2041 const DataLayout &DL = MF.getDataLayout();
2042 uint64_t AS = APSInt(TypeDigits).getZExtValue();
2043 if (!verifyAddrSpace(AS))
2044 return error("invalid address space number");
2045
2046 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2047 lex();
2048 return false;
2049 }
2050
2051 if (Token.range().starts_with("f") || Token.range().starts_with("bf")) {
2052 auto ScalarSize = APSInt(TypeDigits).getZExtValue();
2053 if (!ScalarSize || !verifyScalarSize(ScalarSize))
2054 return error("invalid size for scalar type");
2055
2056 if (Token.range().starts_with("bf") && ScalarSize != 16)
2057 return error("invalid size for bfloat");
2058
2059 Ty = Token.range().starts_with("bf") ? LLT::bfloat16()
2060 : LLT::floatIEEE(ScalarSize);
2061 lex();
2062 return false;
2063 }
2064
2065 // Now we're looking for a vector.
2066 if (Token.isNot(MIToken::less))
2067 return error(Loc, "expected tN, pA, <M x tN>, <M x pA>, <vscale x M x tN>, "
2068 "or <vscale x M x pA> for GlobalISel type, "
2069 "where t = {'s', 'i', 'f', 'bf'}");
2070 lex();
2071
2072 bool HasVScale =
2073 Token.is(MIToken::Identifier) && Token.stringValue() == "vscale";
2074 if (HasVScale) {
2075 lex();
2076 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2077 return error(
2078 "expected <vscale x M x tN>, where t = {'s', 'i', 'f', 'bf', 'p'}");
2079 lex();
2080 }
2081
2082 auto GetError = [this, &HasVScale, Loc]() {
2083 if (HasVScale)
2084 return error(Loc, "expected <vscale x M x tN> for vector type, where t = "
2085 "{'s', 'i', 'f', 'bf', 'p'}");
2086 return error(Loc, "expected <M x tN> for vector type, where t = {'s', 'i', "
2087 "'f', 'bf', 'p'}");
2088 };
2089
2090 if (Token.isNot(MIToken::IntegerLiteral))
2091 return GetError();
2092 uint64_t NumElements = Token.integerValue().getZExtValue();
2093 if (!verifyVectorElementCount(NumElements, HasVScale))
2094 return error("invalid number of vector elements");
2095
2096 lex();
2097
2098 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
2099 return GetError();
2100 lex();
2101
2102 StringRef VectorTyDigits = Token.range();
2103 if (!VectorTyDigits.consume_front("s") &&
2104 !VectorTyDigits.consume_front("i") &&
2105 !VectorTyDigits.consume_front("f") &&
2106 !VectorTyDigits.consume_front("p") && !VectorTyDigits.consume_front("bf"))
2107 return GetError();
2108
2109 if (VectorTyDigits.empty() || !llvm::all_of(VectorTyDigits, isdigit))
2110 return error(
2111 "expected integers after 's'/'i'/'f'/'bf'/'p' type identifier");
2112
2113 Scalar = Token.range().starts_with("s");
2114 if (Scalar || Token.range().starts_with("i")) {
2115 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2116 if (!verifyScalarSize(ScalarSize))
2117 return error("invalid size for scalar element in vector");
2118 Ty = Scalar ? LLT::scalar(ScalarSize) : LLT::integer(ScalarSize);
2119 } else if (Token.range().starts_with("p")) {
2120 const DataLayout &DL = MF.getDataLayout();
2121 uint64_t AS = APSInt(VectorTyDigits).getZExtValue();
2122 if (!verifyAddrSpace(AS))
2123 return error("invalid address space number");
2124
2125 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
2126 } else if (Token.range().starts_with("f")) {
2127 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2128 if (!verifyScalarSize(ScalarSize))
2129 return error("invalid size for float element in vector");
2130 Ty = LLT::floatIEEE(ScalarSize);
2131 } else if (Token.range().starts_with("bf")) {
2132 auto ScalarSize = APSInt(VectorTyDigits).getZExtValue();
2133 if (!verifyScalarSize(ScalarSize))
2134 return error("invalid size for bfloat element in vector");
2135 Ty = LLT::bfloat16();
2136 } else {
2137 return GetError();
2138 }
2139 lex();
2140
2141 if (Token.isNot(MIToken::greater))
2142 return GetError();
2143
2144 lex();
2145
2146 Ty = LLT::vector(ElementCount::get(NumElements, HasVScale), Ty);
2147 return false;
2148}
2149
2150bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
2151 assert(Token.is(MIToken::Identifier));
2152 StringRef TypeDigits = Token.range();
2153 if (!TypeDigits.consume_front("i") && !TypeDigits.consume_front("s") &&
2154 !TypeDigits.consume_front("p") && !TypeDigits.consume_front("f") &&
2155 !TypeDigits.consume_front("bf"))
2156 return error("a typed immediate operand should start with one of 'i', "
2157 "'s', 'f', 'bf', or 'p'");
2158 if (TypeDigits.empty() || !llvm::all_of(TypeDigits, isdigit))
2159 return error(
2160 "expected integers after 'i'/'s'/'f'/'bf'/'p' type identifier");
2161
2162 auto Loc = Token.location();
2163 lex();
2164 if (Token.isNot(MIToken::IntegerLiteral)) {
2165 if (Token.isNot(MIToken::Identifier) ||
2166 !(Token.range() == "true" || Token.range() == "false"))
2167 return error("expected an integer literal");
2168 }
2169 const Constant *C = nullptr;
2170 if (parseIRConstant(Loc, C))
2171 return true;
2173 return false;
2174}
2175
2176bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
2177 auto Loc = Token.location();
2178 lex();
2179 if (Token.isNot(MIToken::FloatingPointLiteral) &&
2180 Token.isNot(MIToken::HexLiteral))
2181 return error("expected a floating point literal");
2182 const Constant *C = nullptr;
2183 if (parseIRConstant(Loc, C))
2184 return true;
2186 return false;
2187}
2188
2189static bool getHexUint(const MIToken &Token, APInt &Result) {
2191 StringRef S = Token.range();
2192 assert(S[0] == '0' && tolower(S[1]) == 'x');
2193 // This could be a floating point literal with a special prefix.
2194 if (!isxdigit(S[2]))
2195 return true;
2196 StringRef V = S.substr(2);
2197 APInt A(V.size()*4, V, 16);
2198
2199 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2200 // sure it isn't the case before constructing result.
2201 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2202 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2203 return false;
2204}
2205
2206static bool getUnsigned(const MIToken &Token, unsigned &Result,
2207 ErrorCallbackType ErrCB) {
2208 if (Token.hasIntegerValue()) {
2209 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2210 const APSInt &SInt = Token.integerValue();
2211 if (SInt.isNegative())
2212 return ErrCB(Token.location(), "expected unsigned integer");
2213 uint64_t Val64 = SInt.getLimitedValue(Limit);
2214 if (Val64 == Limit)
2215 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2216 Result = Val64;
2217 return false;
2218 }
2219 if (Token.is(MIToken::HexLiteral)) {
2220 APInt A;
2221 if (getHexUint(Token, A))
2222 return true;
2223 if (A.getBitWidth() > 32)
2224 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2225 Result = A.getZExtValue();
2226 return false;
2227 }
2228 return true;
2229}
2230
2231bool MIParser::getUnsigned(unsigned &Result) {
2232 return ::getUnsigned(
2233 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2234 return error(Loc, Msg);
2235 });
2236}
2237
2238bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2241 unsigned Number;
2242 if (getUnsigned(Number))
2243 return true;
2244 auto MBBInfo = PFS.MBBSlots.find(Number);
2245 if (MBBInfo == PFS.MBBSlots.end())
2246 return error(Twine("use of undefined machine basic block #") +
2247 Twine(Number));
2248 MBB = MBBInfo->second;
2249 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2250 // we drop the <irname> from the bb.<id>.<irname> format.
2251 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2252 return error(Twine("the name of machine basic block #") + Twine(Number) +
2253 " isn't '" + Token.stringValue() + "'");
2254 return false;
2255}
2256
2257bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2260 return true;
2262 lex();
2263 return false;
2264}
2265
2266bool MIParser::parseStackFrameIndex(int &FI) {
2267 assert(Token.is(MIToken::StackObject));
2268 unsigned ID;
2269 if (getUnsigned(ID))
2270 return true;
2271 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2272 if (ObjectInfo == PFS.StackObjectSlots.end())
2273 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2274 "'");
2276 if (const auto *Alloca =
2277 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2278 Name = Alloca->getName();
2279 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2280 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2281 "' isn't '" + Token.stringValue() + "'");
2282 lex();
2283 FI = ObjectInfo->second;
2284 return false;
2285}
2286
2287bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2288 int FI;
2289 if (parseStackFrameIndex(FI))
2290 return true;
2291 Dest = MachineOperand::CreateFI(FI);
2292 return false;
2293}
2294
2295bool MIParser::parseFixedStackFrameIndex(int &FI) {
2297 unsigned ID;
2298 if (getUnsigned(ID))
2299 return true;
2300 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2301 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2302 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2303 Twine(ID) + "'");
2304 lex();
2305 FI = ObjectInfo->second;
2306 return false;
2307}
2308
2309bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2310 int FI;
2311 if (parseFixedStackFrameIndex(FI))
2312 return true;
2313 Dest = MachineOperand::CreateFI(FI);
2314 return false;
2315}
2316
2317static bool parseGlobalValue(const MIToken &Token,
2319 ErrorCallbackType ErrCB) {
2320 switch (Token.kind()) {
2322 const Module *M = PFS.MF.getFunction().getParent();
2323 GV = M->getNamedValue(Token.stringValue());
2324 if (!GV)
2325 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2326 Token.range() + "'");
2327 break;
2328 }
2329 case MIToken::GlobalValue: {
2330 unsigned GVIdx;
2331 if (getUnsigned(Token, GVIdx, ErrCB))
2332 return true;
2333 GV = PFS.IRSlots.GlobalValues.get(GVIdx);
2334 if (!GV)
2335 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2336 Twine(GVIdx) + "'");
2337 break;
2338 }
2339 default:
2340 llvm_unreachable("The current token should be a global value");
2341 }
2342 return false;
2343}
2344
2345bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2346 return ::parseGlobalValue(
2347 Token, PFS, GV,
2348 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2349 return error(Loc, Msg);
2350 });
2351}
2352
2353bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2354 GlobalValue *GV = nullptr;
2355 if (parseGlobalValue(GV))
2356 return true;
2357 lex();
2358 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2359 if (parseOperandsOffset(Dest))
2360 return true;
2361 return false;
2362}
2363
2364bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2366 unsigned ID;
2367 if (getUnsigned(ID))
2368 return true;
2369 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2370 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2371 return error("use of undefined constant '%const." + Twine(ID) + "'");
2372 lex();
2373 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2374 if (parseOperandsOffset(Dest))
2375 return true;
2376 return false;
2377}
2378
2379bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2381 unsigned ID;
2382 if (getUnsigned(ID))
2383 return true;
2384 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2385 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2386 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2387 lex();
2388 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2389 return false;
2390}
2391
2392bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2394 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2395 lex();
2396 Dest = MachineOperand::CreateES(Symbol);
2397 if (parseOperandsOffset(Dest))
2398 return true;
2399 return false;
2400}
2401
2402bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2403 assert(Token.is(MIToken::MCSymbol));
2404 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2405 lex();
2406 Dest = MachineOperand::CreateMCSymbol(Symbol);
2407 if (parseOperandsOffset(Dest))
2408 return true;
2409 return false;
2410}
2411
2412bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2414 StringRef Name = Token.stringValue();
2415 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2416 if (SubRegIndex == 0)
2417 return error(Twine("unknown subregister index '") + Name + "'");
2418 lex();
2419 Dest = MachineOperand::CreateImm(SubRegIndex);
2420 return false;
2421}
2422
2423bool MIParser::parseMDNode(MDNode *&Node) {
2424 assert(Token.is(MIToken::exclaim));
2425
2426 auto Loc = Token.location();
2427 lex();
2428 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2429 return error("expected metadata id after '!'");
2430 unsigned ID;
2431 if (getUnsigned(ID))
2432 return true;
2433 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2434 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2435 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2436 if (NodeInfo == PFS.MachineMetadataNodes.end())
2437 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2438 }
2439 lex();
2440 Node = NodeInfo->second.get();
2441 return false;
2442}
2443
2444bool MIParser::parseDIExpression(MDNode *&Expr) {
2445 unsigned Read;
2447 CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2448 &PFS.IRSlots);
2449 CurrentSource = CurrentSource.substr(Read);
2450 lex();
2451 if (!Expr)
2452 return error(Error.getMessage());
2453 return false;
2454}
2455
2456bool MIParser::parseDILocation(MDNode *&Loc) {
2457 assert(Token.is(MIToken::md_dilocation));
2458 lex();
2459
2460 bool HaveLine = false;
2461 unsigned Line = 0;
2462 unsigned Column = 0;
2463 MDNode *Scope = nullptr;
2464 MDNode *InlinedAt = nullptr;
2465 bool ImplicitCode = false;
2466 uint64_t AtomGroup = 0;
2467 uint64_t AtomRank = 0;
2468 MDNode *IRLayers = nullptr;
2469
2470 if (expectAndConsume(MIToken::lparen))
2471 return true;
2472
2473 if (Token.isNot(MIToken::rparen)) {
2474 do {
2475 if (Token.is(MIToken::Identifier)) {
2476 if (Token.stringValue() == "line") {
2477 lex();
2478 if (expectAndConsume(MIToken::colon))
2479 return true;
2480 if (Token.isNot(MIToken::IntegerLiteral) ||
2481 Token.integerValue().isSigned())
2482 return error("expected unsigned integer");
2483 Line = Token.integerValue().getZExtValue();
2484 HaveLine = true;
2485 lex();
2486 continue;
2487 }
2488 if (Token.stringValue() == "column") {
2489 lex();
2490 if (expectAndConsume(MIToken::colon))
2491 return true;
2492 if (Token.isNot(MIToken::IntegerLiteral) ||
2493 Token.integerValue().isSigned())
2494 return error("expected unsigned integer");
2495 Column = Token.integerValue().getZExtValue();
2496 lex();
2497 continue;
2498 }
2499 if (Token.stringValue() == "scope") {
2500 lex();
2501 if (expectAndConsume(MIToken::colon))
2502 return true;
2503 if (parseMDNode(Scope))
2504 return error("expected metadata node");
2505 if (!isa<DIScope>(Scope))
2506 return error("expected DIScope node");
2507 continue;
2508 }
2509 if (Token.stringValue() == "inlinedAt") {
2510 lex();
2511 if (expectAndConsume(MIToken::colon))
2512 return true;
2513 if (Token.is(MIToken::exclaim)) {
2514 if (parseMDNode(InlinedAt))
2515 return true;
2516 } else if (Token.is(MIToken::md_dilocation)) {
2517 if (parseDILocation(InlinedAt))
2518 return true;
2519 } else {
2520 return error("expected metadata node");
2521 }
2522 if (!isa<DILocation>(InlinedAt))
2523 return error("expected DILocation node");
2524 continue;
2525 }
2526 if (Token.stringValue() == "isImplicitCode") {
2527 lex();
2528 if (expectAndConsume(MIToken::colon))
2529 return true;
2530 if (!Token.is(MIToken::Identifier))
2531 return error("expected true/false");
2532 // As far as I can see, we don't have any existing need for parsing
2533 // true/false in MIR yet. Do it ad-hoc until there's something else
2534 // that needs it.
2535 if (Token.stringValue() == "true")
2536 ImplicitCode = true;
2537 else if (Token.stringValue() == "false")
2538 ImplicitCode = false;
2539 else
2540 return error("expected true/false");
2541 lex();
2542 continue;
2543 }
2544 if (Token.stringValue() == "atomGroup") {
2545 lex();
2546 if (expectAndConsume(MIToken::colon))
2547 return true;
2548 if (Token.isNot(MIToken::IntegerLiteral) ||
2549 Token.integerValue().isSigned())
2550 return error("expected unsigned integer");
2551 AtomGroup = Token.integerValue().getZExtValue();
2552 lex();
2553 continue;
2554 }
2555 if (Token.stringValue() == "atomRank") {
2556 lex();
2557 if (expectAndConsume(MIToken::colon))
2558 return true;
2559 if (Token.isNot(MIToken::IntegerLiteral) ||
2560 Token.integerValue().isSigned())
2561 return error("expected unsigned integer");
2562 AtomRank = Token.integerValue().getZExtValue();
2563 lex();
2564 continue;
2565 }
2566 if (Token.stringValue() == "irlayers") {
2567 lex();
2568 if (expectAndConsume(MIToken::colon))
2569 return true;
2570 if (parseMDNode(IRLayers))
2571 return error("expected metadata node");
2572 if (!isa<DILayerLocList>(IRLayers))
2573 return error("expected DILayerLocList node");
2574 continue;
2575 }
2576 }
2577 return error(Twine("invalid DILocation argument '") +
2578 Token.stringValue() + "'");
2579 } while (consumeIfPresent(MIToken::comma));
2580 }
2581
2582 if (expectAndConsume(MIToken::rparen))
2583 return true;
2584
2585 if (!HaveLine)
2586 return error("DILocation requires line number");
2587 if (!Scope)
2588 return error("DILocation requires a scope");
2589
2590 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2591 InlinedAt, ImplicitCode, AtomGroup, AtomRank, IRLayers);
2592 return false;
2593}
2594
2595bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2596 MDNode *Node = nullptr;
2597 if (Token.is(MIToken::exclaim)) {
2598 if (parseMDNode(Node))
2599 return true;
2600 } else if (Token.is(MIToken::md_diexpr)) {
2601 if (parseDIExpression(Node))
2602 return true;
2603 }
2604 Dest = MachineOperand::CreateMetadata(Node);
2605 return false;
2606}
2607
2608bool MIParser::parseCFIOffset(int &Offset) {
2609 if (Token.isNot(MIToken::IntegerLiteral))
2610 return error("expected a cfi offset");
2611 if (Token.integerValue().getSignificantBits() > 32)
2612 return error("expected a 32 bit integer (the cfi offset is too large)");
2613 Offset = (int)Token.integerValue().getExtValue();
2614 lex();
2615 return false;
2616}
2617
2618bool MIParser::parseCFIUnsigned(unsigned &Value) {
2619 if (getUnsigned(Value))
2620 return true;
2621 lex();
2622 return false;
2623}
2624
2625bool MIParser::parseCFIRegister(unsigned &Reg) {
2626 if (Token.isNot(MIToken::NamedRegister))
2627 return error("expected a cfi register");
2628 Register LLVMReg;
2629 if (parseNamedRegister(LLVMReg))
2630 return true;
2631 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2632 assert(TRI && "Expected target register info");
2633 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2634 if (DwarfReg < 0)
2635 return error("invalid DWARF register");
2636 Reg = (unsigned)DwarfReg;
2637 lex();
2638 return false;
2639}
2640
2641bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2642 if (Token.isNot(MIToken::IntegerLiteral))
2643 return error("expected a cfi address space literal");
2644 if (Token.integerValue().isSigned())
2645 return error("expected an unsigned integer (cfi address space)");
2646 AddressSpace = Token.integerValue().getZExtValue();
2647 lex();
2648 return false;
2649}
2650
2651bool MIParser::parseCFIEscapeValues(std::string &Values) {
2652 do {
2653 if (Token.isNot(MIToken::HexLiteral))
2654 return error("expected a hexadecimal literal");
2655 unsigned Value;
2656 if (getUnsigned(Value))
2657 return true;
2658 if (Value > UINT8_MAX)
2659 return error("expected a 8-bit integer (too large)");
2660 Values.push_back(static_cast<uint8_t>(Value));
2661 lex();
2662 } while (consumeIfPresent(MIToken::comma));
2663 return false;
2664}
2665
2666bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2667 auto Kind = Token.kind();
2668 lex();
2669 int Offset;
2670 unsigned Reg;
2671 unsigned AddressSpace;
2672 unsigned CFIIndex;
2673 switch (Kind) {
2675 if (parseCFIRegister(Reg))
2676 return true;
2677 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2678 break;
2680 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2681 parseCFIOffset(Offset))
2682 return true;
2683 CFIIndex =
2684 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2685 break;
2687 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2688 parseCFIOffset(Offset))
2689 return true;
2690 CFIIndex = MF.addFrameInst(
2692 break;
2694 if (parseCFIRegister(Reg))
2695 return true;
2696 CFIIndex =
2697 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2698 break;
2700 if (parseCFIOffset(Offset))
2701 return true;
2702 CFIIndex =
2703 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2704 break;
2706 if (parseCFIOffset(Offset))
2707 return true;
2708 CFIIndex = MF.addFrameInst(
2710 break;
2712 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2713 parseCFIOffset(Offset))
2714 return true;
2715 CFIIndex =
2716 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2717 break;
2719 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2720 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2721 parseCFIAddressSpace(AddressSpace))
2722 return true;
2723 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2724 nullptr, Reg, Offset, AddressSpace, SMLoc()));
2725 break;
2727 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2728 break;
2730 if (parseCFIRegister(Reg))
2731 return true;
2732 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2733 break;
2735 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2736 break;
2738 if (parseCFIRegister(Reg))
2739 return true;
2740 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2741 break;
2743 unsigned Reg2;
2744 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2745 parseCFIRegister(Reg2))
2746 return true;
2747
2748 CFIIndex =
2749 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2750 break;
2751 }
2753 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2754 break;
2756 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2757 break;
2759 CFIIndex =
2760 MF.addFrameInst(MCCFIInstruction::createNegateRAStateWithPC(nullptr));
2761 break;
2763 unsigned State;
2764 MCSymbol *PACSym = nullptr;
2765 if (parseCFIUnsigned(State) || expectAndConsume(MIToken::comma))
2766 return true;
2767 if (Token.is(MIToken::MCSymbol)) {
2768 PACSym = getOrCreateMCSymbol(Token.stringValue());
2769 lex();
2770 CFIIndex = MF.addFrameInst(
2771 MCCFIInstruction::createSetRAState(nullptr, State, PACSym));
2772 } else if (Token.is(MIToken::IntegerLiteral)) {
2773 int Offset;
2774 if (parseCFIOffset(Offset))
2775 return true;
2776 CFIIndex = MF.addFrameInst(
2778 } else {
2779 return error("expected '<mcsymbol ...>' or integer offset for "
2780 "cfi_set_ra_state");
2781 }
2782 break;
2783 }
2785 unsigned Reg, R1, R2;
2786 unsigned R1Size, R2Size;
2787 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2788 parseCFIRegister(R1) || expectAndConsume(MIToken::comma) ||
2789 parseCFIUnsigned(R1Size) || expectAndConsume(MIToken::comma) ||
2790 parseCFIRegister(R2) || expectAndConsume(MIToken::comma) ||
2791 parseCFIUnsigned(R2Size))
2792 return true;
2793
2794 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMRegisterPair(
2795 nullptr, Reg, R1, R1Size, R2, R2Size));
2796 break;
2797 }
2799 std::vector<MCCFIInstruction::VectorRegisterWithLane> VectorRegisters;
2800 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma))
2801 return true;
2802 do {
2803 unsigned VR;
2804 unsigned Lane, Size;
2805 if (parseCFIRegister(VR) || expectAndConsume(MIToken::comma) ||
2806 parseCFIUnsigned(Lane) || expectAndConsume(MIToken::comma) ||
2807 parseCFIUnsigned(Size))
2808 return true;
2809 VectorRegisters.push_back({VR, Lane, Size});
2810 } while (consumeIfPresent(MIToken::comma));
2811
2812 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisters(
2813 nullptr, Reg, std::move(VectorRegisters)));
2814 break;
2815 }
2817 unsigned Reg, MaskReg;
2818 unsigned RegSize, MaskRegSize;
2819 int Offset = 0;
2820
2821 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2822 parseCFIUnsigned(RegSize) || expectAndConsume(MIToken::comma) ||
2823 parseCFIRegister(MaskReg) || expectAndConsume(MIToken::comma) ||
2824 parseCFIUnsigned(MaskRegSize) || expectAndConsume(MIToken::comma) ||
2825 parseCFIOffset(Offset))
2826 return true;
2827
2828 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorOffset(
2829 nullptr, Reg, RegSize, MaskReg, MaskRegSize, Offset));
2830 break;
2831 }
2833 unsigned Reg, SpillReg, MaskReg;
2834 unsigned SpillRegLaneSize, MaskRegSize;
2835
2836 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2837 parseCFIRegister(SpillReg) || expectAndConsume(MIToken::comma) ||
2838 parseCFIUnsigned(SpillRegLaneSize) ||
2839 expectAndConsume(MIToken::comma) || parseCFIRegister(MaskReg) ||
2840 expectAndConsume(MIToken::comma) || parseCFIUnsigned(MaskRegSize))
2841 return true;
2842
2843 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMVectorRegisterMask(
2844 nullptr, Reg, SpillReg, SpillRegLaneSize, MaskReg, MaskRegSize));
2845 break;
2846 }
2848 std::string Values;
2849 if (parseCFIEscapeValues(Values))
2850 return true;
2851 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2852 break;
2853 }
2854 default:
2855 // TODO: Parse the other CFI operands.
2856 llvm_unreachable("The current token should be a cfi operand");
2857 }
2858 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2859 return false;
2860}
2861
2862bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2863 switch (Token.kind()) {
2864 case MIToken::NamedIRBlock: {
2866 F.getValueSymbolTable()->lookup(Token.stringValue()));
2867 if (!BB)
2868 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2869 break;
2870 }
2871 case MIToken::IRBlock: {
2872 unsigned SlotNumber = 0;
2873 if (getUnsigned(SlotNumber))
2874 return true;
2875 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2876 if (!BB)
2877 return error(Twine("use of undefined IR block '%ir-block.") +
2878 Twine(SlotNumber) + "'");
2879 break;
2880 }
2881 default:
2882 llvm_unreachable("The current token should be an IR block reference");
2883 }
2884 return false;
2885}
2886
2887bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2889 lex();
2890 if (expectAndConsume(MIToken::lparen))
2891 return true;
2892 if (Token.isNot(MIToken::GlobalValue) &&
2893 Token.isNot(MIToken::NamedGlobalValue))
2894 return error("expected a global value");
2895 GlobalValue *GV = nullptr;
2896 if (parseGlobalValue(GV))
2897 return true;
2898 auto *F = dyn_cast<Function>(GV);
2899 if (!F)
2900 return error("expected an IR function reference");
2901 lex();
2902 if (expectAndConsume(MIToken::comma))
2903 return true;
2904 BasicBlock *BB = nullptr;
2905 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2906 return error("expected an IR block reference");
2907 if (parseIRBlock(BB, *F))
2908 return true;
2909 lex();
2910 if (expectAndConsume(MIToken::rparen))
2911 return true;
2912 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2913 if (parseOperandsOffset(Dest))
2914 return true;
2915 return false;
2916}
2917
2918bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2919 assert(Token.is(MIToken::kw_intrinsic));
2920 lex();
2921 if (expectAndConsume(MIToken::lparen))
2922 return error("expected syntax intrinsic(@llvm.whatever)");
2923
2924 if (Token.isNot(MIToken::NamedGlobalValue))
2925 return error("expected syntax intrinsic(@llvm.whatever)");
2926
2927 std::string Name = std::string(Token.stringValue());
2928 lex();
2929
2930 if (expectAndConsume(MIToken::rparen))
2931 return error("expected ')' to terminate intrinsic name");
2932
2933 // Find out what intrinsic we're dealing with.
2935 if (ID == Intrinsic::not_intrinsic)
2936 return error("unknown intrinsic name");
2938
2939 return false;
2940}
2941
2942bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2943 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2944 bool IsFloat = Token.is(MIToken::kw_floatpred);
2945 lex();
2946
2947 if (expectAndConsume(MIToken::lparen))
2948 return error("expected syntax intpred(whatever) or floatpred(whatever");
2949
2950 if (Token.isNot(MIToken::Identifier))
2951 return error("whatever");
2952
2953 CmpInst::Predicate Pred;
2954 if (IsFloat) {
2955 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2956 .Case("false", CmpInst::FCMP_FALSE)
2957 .Case("oeq", CmpInst::FCMP_OEQ)
2958 .Case("ogt", CmpInst::FCMP_OGT)
2959 .Case("oge", CmpInst::FCMP_OGE)
2960 .Case("olt", CmpInst::FCMP_OLT)
2961 .Case("ole", CmpInst::FCMP_OLE)
2962 .Case("one", CmpInst::FCMP_ONE)
2963 .Case("ord", CmpInst::FCMP_ORD)
2964 .Case("uno", CmpInst::FCMP_UNO)
2965 .Case("ueq", CmpInst::FCMP_UEQ)
2966 .Case("ugt", CmpInst::FCMP_UGT)
2967 .Case("uge", CmpInst::FCMP_UGE)
2968 .Case("ult", CmpInst::FCMP_ULT)
2969 .Case("ule", CmpInst::FCMP_ULE)
2970 .Case("une", CmpInst::FCMP_UNE)
2971 .Case("true", CmpInst::FCMP_TRUE)
2973 if (!CmpInst::isFPPredicate(Pred))
2974 return error("invalid floating-point predicate");
2975 } else {
2976 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2977 .Case("eq", CmpInst::ICMP_EQ)
2978 .Case("ne", CmpInst::ICMP_NE)
2979 .Case("sgt", CmpInst::ICMP_SGT)
2980 .Case("sge", CmpInst::ICMP_SGE)
2981 .Case("slt", CmpInst::ICMP_SLT)
2982 .Case("sle", CmpInst::ICMP_SLE)
2983 .Case("ugt", CmpInst::ICMP_UGT)
2984 .Case("uge", CmpInst::ICMP_UGE)
2985 .Case("ult", CmpInst::ICMP_ULT)
2986 .Case("ule", CmpInst::ICMP_ULE)
2988 if (!CmpInst::isIntPredicate(Pred))
2989 return error("invalid integer predicate");
2990 }
2991
2992 lex();
2994 if (expectAndConsume(MIToken::rparen))
2995 return error("predicate should be terminated by ')'.");
2996
2997 return false;
2998}
2999
3000bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
3002
3003 lex();
3004 if (expectAndConsume(MIToken::lparen))
3005 return error("expected syntax shufflemask(<integer or undef>, ...)");
3006
3007 SmallVector<int, 32> ShufMask;
3008 do {
3009 if (Token.is(MIToken::kw_undef)) {
3010 ShufMask.push_back(-1);
3011 } else if (Token.is(MIToken::IntegerLiteral)) {
3012 const APSInt &Int = Token.integerValue();
3013 ShufMask.push_back(Int.getExtValue());
3014 } else {
3015 return error("expected integer constant");
3016 }
3017
3018 lex();
3019 } while (consumeIfPresent(MIToken::comma));
3020
3021 if (expectAndConsume(MIToken::rparen))
3022 return error("shufflemask should be terminated by ')'.");
3023
3024 if (ShufMask.size() < 2)
3025 return error("shufflemask should have > 1 element");
3026
3027 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
3028 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
3029 return false;
3030}
3031
3032bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
3034
3035 lex();
3036 if (expectAndConsume(MIToken::lparen))
3037 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3038
3039 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3040 return error("expected unsigned integer for instruction index");
3041 uint64_t InstrIdx = Token.integerValue().getZExtValue();
3042 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
3043 "Instruction reference's instruction index is too large");
3044 lex();
3045
3046 if (expectAndConsume(MIToken::comma))
3047 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3048
3049 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
3050 return error("expected unsigned integer for operand index");
3051 uint64_t OpIdx = Token.integerValue().getZExtValue();
3052 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
3053 "Instruction reference's operand index is too large");
3054 lex();
3055
3056 if (expectAndConsume(MIToken::rparen))
3057 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
3058
3059 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
3060 return false;
3061}
3062
3063bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
3065 lex();
3066 if (expectAndConsume(MIToken::lparen))
3067 return true;
3068 if (Token.isNot(MIToken::Identifier))
3069 return error("expected the name of the target index");
3070 int Index = 0;
3071 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
3072 return error("use of undefined target index '" + Token.stringValue() + "'");
3073 lex();
3074 if (expectAndConsume(MIToken::rparen))
3075 return true;
3076 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
3077 if (parseOperandsOffset(Dest))
3078 return true;
3079 return false;
3080}
3081
3082bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
3083 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
3084 lex();
3085 if (expectAndConsume(MIToken::lparen))
3086 return true;
3087
3088 uint32_t *Mask = MF.allocateRegMask();
3089 do {
3090 if (Token.isNot(MIToken::rparen)) {
3091 if (Token.isNot(MIToken::NamedRegister))
3092 return error("expected a named register");
3093 Register Reg;
3094 if (parseNamedRegister(Reg))
3095 return true;
3096 lex();
3097 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3098 }
3099
3100 // TODO: Report an error if the same register is used more than once.
3101 } while (consumeIfPresent(MIToken::comma));
3102
3103 if (expectAndConsume(MIToken::rparen))
3104 return true;
3105 Dest = MachineOperand::CreateRegMask(Mask);
3106 return false;
3107}
3108
3109bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
3110 assert(Token.is(MIToken::kw_lanemask));
3111
3112 lex();
3113 if (expectAndConsume(MIToken::lparen))
3114 return true;
3115
3116 // Parse lanemask.
3117 if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
3118 return error("expected a valid lane mask value");
3119 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
3120 "Use correct get-function for lane mask.");
3122 if (getUint64(V))
3123 return true;
3124 LaneBitmask LaneMask(V);
3125 lex();
3126
3127 if (expectAndConsume(MIToken::rparen))
3128 return true;
3129
3130 Dest = MachineOperand::CreateLaneMask(LaneMask);
3131 return false;
3132}
3133
3134bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
3135 assert(Token.is(MIToken::kw_liveout));
3136 uint32_t *Mask = MF.allocateRegMask();
3137 lex();
3138 if (expectAndConsume(MIToken::lparen))
3139 return true;
3140 while (true) {
3141 if (Token.isNot(MIToken::NamedRegister))
3142 return error("expected a named register");
3143 Register Reg;
3144 if (parseNamedRegister(Reg))
3145 return true;
3146 lex();
3147 Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32);
3148 // TODO: Report an error if the same register is used more than once.
3149 if (Token.isNot(MIToken::comma))
3150 break;
3151 lex();
3152 }
3153 if (expectAndConsume(MIToken::rparen))
3154 return true;
3156 return false;
3157}
3158
3159bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
3160 MachineOperand &Dest,
3161 std::optional<unsigned> &TiedDefIdx) {
3162 switch (Token.kind()) {
3165 case MIToken::kw_def:
3166 case MIToken::kw_dead:
3167 case MIToken::kw_killed:
3168 case MIToken::kw_undef:
3177 return parseRegisterOperand(Dest, TiedDefIdx);
3179 // TODO: Forbid numeric operands for INLINEASM once the transition to the
3180 // symbolic form is over.
3181 return parseImmediateOperand(Dest);
3182 case MIToken::kw_half:
3183 case MIToken::kw_bfloat:
3184 case MIToken::kw_float:
3185 case MIToken::kw_double:
3187 case MIToken::kw_fp128:
3189 return parseFPImmediateOperand(Dest);
3191 return parseMBBOperand(Dest);
3193 return parseStackObjectOperand(Dest);
3195 return parseFixedStackObjectOperand(Dest);
3198 return parseGlobalAddressOperand(Dest);
3200 return parseConstantPoolIndexOperand(Dest);
3202 return parseJumpTableIndexOperand(Dest);
3204 return parseExternalSymbolOperand(Dest);
3205 case MIToken::MCSymbol:
3206 return parseMCSymbolOperand(Dest);
3208 return parseSubRegisterIndexOperand(Dest);
3209 case MIToken::md_diexpr:
3210 case MIToken::exclaim:
3211 return parseMetadataOperand(Dest);
3234 return parseCFIOperand(Dest);
3236 return parseBlockAddressOperand(Dest);
3238 return parseIntrinsicOperand(Dest);
3240 return parseTargetIndexOperand(Dest);
3242 return parseLaneMaskOperand(Dest);
3244 return parseLiveoutRegisterMaskOperand(Dest);
3247 return parsePredicateOperand(Dest);
3249 return parseShuffleMaskOperand(Dest);
3251 return parseDbgInstrRefOperand(Dest);
3252 case MIToken::Error:
3253 return true;
3254 case MIToken::Identifier: {
3255 bool IsInlineAsm = OpCode == TargetOpcode::INLINEASM ||
3256 OpCode == TargetOpcode::INLINEASM_BR;
3257 if (IsInlineAsm)
3258 return parseSymbolicInlineAsmOperand(OpIdx, Dest);
3259
3260 StringRef Id = Token.stringValue();
3261 if (const auto *RegMask = PFS.Target.getRegMask(Id)) {
3262 Dest = MachineOperand::CreateRegMask(RegMask);
3263 lex();
3264 break;
3265 } else if (Id == "CustomRegMask") {
3266 return parseCustomRegisterMaskOperand(Dest);
3267 } else {
3268 return parseTypedImmediateOperand(Dest);
3269 }
3270 }
3271 case MIToken::dot: {
3272 const auto *TII = MF.getSubtarget().getInstrInfo();
3273 if (const auto *Formatter = TII->getMIRFormatter()) {
3274 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
3275 }
3276 [[fallthrough]];
3277 }
3278 default:
3279 // FIXME: Parse the MCSymbol machine operand.
3280 return error("expected a machine operand");
3281 }
3282 return false;
3283}
3284
3285bool MIParser::parseMachineOperandAndTargetFlags(
3286 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
3287 std::optional<unsigned> &TiedDefIdx) {
3288 unsigned TF = 0;
3289 bool HasTargetFlags = false;
3290 if (Token.is(MIToken::kw_target_flags)) {
3291 HasTargetFlags = true;
3292 lex();
3293 if (expectAndConsume(MIToken::lparen))
3294 return true;
3295 if (Token.isNot(MIToken::Identifier))
3296 return error("expected the name of the target flag");
3297 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
3298 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
3299 return error("use of undefined target flag '" + Token.stringValue() +
3300 "'");
3301 }
3302 lex();
3303 while (Token.is(MIToken::comma)) {
3304 lex();
3305 if (Token.isNot(MIToken::Identifier))
3306 return error("expected the name of the target flag");
3307 unsigned BitFlag = 0;
3308 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
3309 return error("use of undefined target flag '" + Token.stringValue() +
3310 "'");
3311 // TODO: Report an error when using a duplicate bit target flag.
3312 TF |= BitFlag;
3313 lex();
3314 }
3315 if (expectAndConsume(MIToken::rparen))
3316 return true;
3317 }
3318 auto Loc = Token.location();
3319 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
3320 return true;
3321 if (!HasTargetFlags)
3322 return false;
3323 if (Dest.isReg())
3324 return error(Loc, "register operands can't have target flags");
3325 Dest.setTargetFlags(TF);
3326 return false;
3327}
3328
3329bool MIParser::parseOffset(int64_t &Offset) {
3330 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
3331 return false;
3332 StringRef Sign = Token.range();
3333 bool IsNegative = Token.is(MIToken::minus);
3334 lex();
3335 if (Token.isNot(MIToken::IntegerLiteral))
3336 return error("expected an integer literal after '" + Sign + "'");
3337 if (Token.integerValue().getSignificantBits() > 64)
3338 return error("expected 64-bit integer (too large)");
3339 Offset = Token.integerValue().getExtValue();
3340 if (IsNegative)
3341 Offset = -Offset;
3342 lex();
3343 return false;
3344}
3345
3346bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3348 lex();
3349 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3350 return error("expected basic block after 'ir_block_address_taken'");
3351
3352 if (parseIRBlock(BB, MF.getFunction()))
3353 return true;
3354
3355 lex();
3356 return false;
3357}
3358
3359bool MIParser::parseAlignment(uint64_t &Alignment) {
3360 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3361 lex();
3362 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3363 return error("expected an integer literal after 'align'");
3364 if (getUint64(Alignment))
3365 return true;
3366 lex();
3367
3368 if (!isPowerOf2_64(Alignment))
3369 return error("expected a power-of-2 literal after 'align'");
3370
3371 return false;
3372}
3373
3374bool MIParser::parseAddrspace(unsigned &Addrspace) {
3375 assert(Token.is(MIToken::kw_addrspace));
3376 lex();
3377 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3378 return error("expected an integer literal after 'addrspace'");
3379 if (getUnsigned(Addrspace))
3380 return true;
3381 lex();
3382 return false;
3383}
3384
3385bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3386 int64_t Offset = 0;
3387 if (parseOffset(Offset))
3388 return true;
3389 Op.setOffset(Offset);
3390 return false;
3391}
3392
3393static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3394 const Value *&V, ErrorCallbackType ErrCB) {
3395 switch (Token.kind()) {
3396 case MIToken::NamedIRValue: {
3397 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3398 break;
3399 }
3400 case MIToken::IRValue: {
3401 unsigned SlotNumber = 0;
3402 if (getUnsigned(Token, SlotNumber, ErrCB))
3403 return true;
3404 V = PFS.getIRValue(SlotNumber);
3405 break;
3406 }
3408 case MIToken::GlobalValue: {
3409 GlobalValue *GV = nullptr;
3410 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3411 return true;
3412 V = GV;
3413 break;
3414 }
3416 const Constant *C = nullptr;
3417 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3418 return true;
3419 V = C;
3420 break;
3421 }
3423 V = nullptr;
3424 return false;
3425 default:
3426 llvm_unreachable("The current token should be an IR block reference");
3427 }
3428 if (!V)
3429 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3430 return false;
3431}
3432
3433bool MIParser::parseIRValue(const Value *&V) {
3434 return ::parseIRValue(
3435 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3436 return error(Loc, Msg);
3437 });
3438}
3439
3440bool MIParser::getUint64(uint64_t &Result) {
3441 if (Token.hasIntegerValue()) {
3442 if (Token.integerValue().getActiveBits() > 64)
3443 return error("expected 64-bit integer (too large)");
3444 Result = Token.integerValue().getZExtValue();
3445 return false;
3446 }
3447 if (Token.is(MIToken::HexLiteral)) {
3448 APInt A;
3449 if (getHexUint(A))
3450 return true;
3451 if (A.getBitWidth() > 64)
3452 return error("expected 64-bit integer (too large)");
3453 Result = A.getZExtValue();
3454 return false;
3455 }
3456 return true;
3457}
3458
3459bool MIParser::getHexUint(APInt &Result) {
3460 return ::getHexUint(Token, Result);
3461}
3462
3463bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3464 const auto OldFlags = Flags;
3465 switch (Token.kind()) {
3468 break;
3471 break;
3474 break;
3477 break;
3480 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3481 return error("use of undefined target MMO flag '" + Token.stringValue() +
3482 "'");
3483 Flags |= TF;
3484 break;
3485 }
3486 default:
3487 llvm_unreachable("The current token should be a memory operand flag");
3488 }
3489 if (OldFlags == Flags)
3490 // We know that the same flag is specified more than once when the flags
3491 // weren't modified.
3492 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3493 lex();
3494 return false;
3495}
3496
3497bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3498 switch (Token.kind()) {
3499 case MIToken::kw_stack:
3500 PSV = MF.getPSVManager().getStack();
3501 break;
3502 case MIToken::kw_got:
3503 PSV = MF.getPSVManager().getGOT();
3504 break;
3506 PSV = MF.getPSVManager().getJumpTable();
3507 break;
3509 PSV = MF.getPSVManager().getConstantPool();
3510 break;
3512 int FI;
3513 if (parseFixedStackFrameIndex(FI))
3514 return true;
3515 PSV = MF.getPSVManager().getFixedStack(FI);
3516 // The token was already consumed, so use return here instead of break.
3517 return false;
3518 }
3519 case MIToken::StackObject: {
3520 int FI;
3521 if (parseStackFrameIndex(FI))
3522 return true;
3523 PSV = MF.getPSVManager().getFixedStack(FI);
3524 // The token was already consumed, so use return here instead of break.
3525 return false;
3526 }
3528 lex();
3529 switch (Token.kind()) {
3532 GlobalValue *GV = nullptr;
3533 if (parseGlobalValue(GV))
3534 return true;
3535 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3536 break;
3537 }
3539 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3540 MF.createExternalSymbolName(Token.stringValue()));
3541 break;
3542 default:
3543 return error(
3544 "expected a global value or an external symbol after 'call-entry'");
3545 }
3546 break;
3547 case MIToken::kw_custom: {
3548 lex();
3549 const auto *TII = MF.getSubtarget().getInstrInfo();
3550 if (const auto *Formatter = TII->getMIRFormatter()) {
3551 if (Formatter->parseCustomPseudoSourceValue(
3552 Token.stringValue(), MF, PFS, PSV,
3553 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3554 return error(Loc, Msg);
3555 }))
3556 return true;
3557 } else {
3558 return error("unable to parse target custom pseudo source value");
3559 }
3560 break;
3561 }
3562 default:
3563 llvm_unreachable("The current token should be pseudo source value");
3564 }
3565 lex();
3566 return false;
3567}
3568
3569bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3570 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3571 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3572 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3573 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3574 const PseudoSourceValue *PSV = nullptr;
3575 if (parseMemoryPseudoSourceValue(PSV))
3576 return true;
3577 int64_t Offset = 0;
3578 if (parseOffset(Offset))
3579 return true;
3580 Dest = MachinePointerInfo(PSV, Offset);
3581 return false;
3582 }
3583 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3584 Token.isNot(MIToken::GlobalValue) &&
3585 Token.isNot(MIToken::NamedGlobalValue) &&
3586 Token.isNot(MIToken::QuotedIRValue) &&
3587 Token.isNot(MIToken::kw_unknown_address))
3588 return error("expected an IR value reference");
3589 const Value *V = nullptr;
3590 if (parseIRValue(V))
3591 return true;
3592 if (V && !V->getType()->isPointerTy())
3593 return error("expected a pointer IR value");
3594 lex();
3595 int64_t Offset = 0;
3596 if (parseOffset(Offset))
3597 return true;
3598 Dest = MachinePointerInfo(V, Offset);
3599 return false;
3600}
3601
3602bool MIParser::parseOptionalScope(LLVMContext &Context,
3603 SyncScope::ID &SSID) {
3604 SSID = SyncScope::System;
3605 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3606 lex();
3607 if (expectAndConsume(MIToken::lparen))
3608 return error("expected '(' in syncscope");
3609
3610 std::string SSN;
3611 if (parseStringConstant(SSN))
3612 return true;
3613
3614 SSID = Context.getOrInsertSyncScopeID(SSN);
3615 if (expectAndConsume(MIToken::rparen))
3616 return error("expected ')' in syncscope");
3617 }
3618
3619 return false;
3620}
3621
3622bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3624 if (Token.isNot(MIToken::Identifier))
3625 return false;
3626
3627 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3628 .Case("unordered", AtomicOrdering::Unordered)
3629 .Case("monotonic", AtomicOrdering::Monotonic)
3630 .Case("acquire", AtomicOrdering::Acquire)
3631 .Case("release", AtomicOrdering::Release)
3635
3636 if (Order != AtomicOrdering::NotAtomic) {
3637 lex();
3638 return false;
3639 }
3640
3641 return error("expected an atomic scope, ordering or a size specification");
3642}
3643
3644bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3645 if (expectAndConsume(MIToken::lparen))
3646 return true;
3648 while (Token.isMemoryOperandFlag()) {
3649 if (parseMemoryOperandFlag(Flags))
3650 return true;
3651 }
3652 if (Token.isNot(MIToken::Identifier) ||
3653 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3654 return error("expected 'load' or 'store' memory operation");
3655 if (Token.stringValue() == "load")
3657 else
3659 lex();
3660
3661 // Optional 'store' for operands that both load and store.
3662 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3664 lex();
3665 }
3666
3667 // Optional synchronization scope.
3668 SyncScope::ID SSID;
3669 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3670 return true;
3671
3672 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3673 AtomicOrdering Order, FailureOrder;
3674 if (parseOptionalAtomicOrdering(Order))
3675 return true;
3676
3677 if (parseOptionalAtomicOrdering(FailureOrder))
3678 return true;
3679
3680 if (Token.isNot(MIToken::IntegerLiteral) &&
3681 Token.isNot(MIToken::kw_unknown_size) &&
3682 Token.isNot(MIToken::lparen))
3683 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3684 "memory operation");
3685
3687 if (Token.is(MIToken::IntegerLiteral)) {
3688 uint64_t Size;
3689 if (getUint64(Size))
3690 return true;
3691
3692 // Convert from bytes to bits for storage.
3694 lex();
3695 } else if (Token.is(MIToken::kw_unknown_size)) {
3696 lex();
3697 } else {
3698 if (expectAndConsume(MIToken::lparen))
3699 return true;
3700 if (parseLowLevelType(Token.location(), MemoryType))
3701 return true;
3702 if (expectAndConsume(MIToken::rparen))
3703 return true;
3704 }
3705
3707 if (Token.is(MIToken::Identifier)) {
3708 const char *Word =
3711 ? "on"
3712 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3713 if (Token.stringValue() != Word)
3714 return error(Twine("expected '") + Word + "'");
3715 lex();
3716
3717 if (parseMachinePointerInfo(Ptr))
3718 return true;
3719 }
3720 uint64_t BaseAlignment =
3721 MemoryType.isValid()
3722 ? PowerOf2Ceil(MemoryType.getSizeInBytes().getKnownMinValue())
3723 : 1;
3724 AAMDNodes AAInfo;
3725 MDNode *Range = nullptr;
3726 MDNode *MemCacheHint = nullptr;
3727 while (consumeIfPresent(MIToken::comma)) {
3728 switch (Token.kind()) {
3729 case MIToken::kw_align: {
3730 // align is printed if it is different than size.
3732 if (parseAlignment(Alignment))
3733 return true;
3734 if (Ptr.Offset & (Alignment - 1)) {
3735 // MachineMemOperand::getAlign never returns a value greater than the
3736 // alignment of offset, so this just guards against hand-written MIR
3737 // that specifies a large "align" value when it should probably use
3738 // "basealign" instead.
3739 return error("specified alignment is more aligned than offset");
3740 }
3741 BaseAlignment = Alignment;
3742 break;
3743 }
3745 // basealign is printed if it is different than align.
3746 if (parseAlignment(BaseAlignment))
3747 return true;
3748 break;
3750 if (parseAddrspace(Ptr.AddrSpace))
3751 return true;
3752 break;
3753 case MIToken::md_tbaa:
3754 lex();
3755 if (parseMDNode(AAInfo.TBAA))
3756 return true;
3757 break;
3759 lex();
3760 if (parseMDNode(AAInfo.Scope))
3761 return true;
3762 break;
3764 lex();
3765 if (parseMDNode(AAInfo.NoAlias))
3766 return true;
3767 break;
3769 lex();
3770 if (parseMDNode(AAInfo.NoAliasAddrSpace))
3771 return true;
3772 break;
3773 case MIToken::md_range:
3774 lex();
3775 if (parseMDNode(Range))
3776 return true;
3777 break;
3779 lex();
3780 if (parseMDNode(MemCacheHint))
3781 return true;
3782 break;
3783 // TODO: Report an error on duplicate metadata nodes.
3784 default:
3785 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3786 "'!noalias' or '!range' or '!mem.cache_hint' or "
3787 "'!noalias.addrspace'");
3788 }
3789 }
3790 if (expectAndConsume(MIToken::rparen))
3791 return true;
3792 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3793 MMOMetadata(AAInfo, Range, MemCacheHint), SSID,
3794 Order, FailureOrder);
3795 return false;
3796}
3797
3798bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3800 Token.is(MIToken::kw_post_instr_symbol)) &&
3801 "Invalid token for a pre- post-instruction symbol!");
3802 lex();
3803 if (Token.isNot(MIToken::MCSymbol))
3804 return error("expected a symbol after 'pre-instr-symbol'");
3805 Symbol = getOrCreateMCSymbol(Token.stringValue());
3806 lex();
3807 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3808 Token.is(MIToken::lbrace))
3809 return false;
3810 if (Token.isNot(MIToken::comma))
3811 return error("expected ',' before the next machine operand");
3812 lex();
3813 return false;
3814}
3815
3816bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3818 "Invalid token for a heap alloc marker!");
3819 lex();
3820 if (parseMDNode(Node))
3821 return true;
3822 if (!Node)
3823 return error("expected a MDNode after 'heap-alloc-marker'");
3824 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3825 Token.is(MIToken::lbrace))
3826 return false;
3827 if (Token.isNot(MIToken::comma))
3828 return error("expected ',' before the next machine operand");
3829 lex();
3830 return false;
3831}
3832
3833bool MIParser::parsePCSections(MDNode *&Node) {
3834 assert(Token.is(MIToken::kw_pcsections) &&
3835 "Invalid token for a PC sections!");
3836 lex();
3837 if (parseMDNode(Node))
3838 return true;
3839 if (!Node)
3840 return error("expected a MDNode after 'pcsections'");
3841 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3842 Token.is(MIToken::lbrace))
3843 return false;
3844 if (Token.isNot(MIToken::comma))
3845 return error("expected ',' before the next machine operand");
3846 lex();
3847 return false;
3848}
3849
3850bool MIParser::parseMMRA(MDNode *&Node) {
3851 assert(Token.is(MIToken::kw_mmra) && "Invalid token for MMRA!");
3852 lex();
3853 if (parseMDNode(Node))
3854 return true;
3855 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3856 Token.is(MIToken::lbrace))
3857 return false;
3858 if (Token.isNot(MIToken::comma))
3859 return error("expected ',' before the next machine operand");
3860 lex();
3861 return false;
3862}
3863
3865 const Function &F,
3866 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3867 ModuleSlotTracker MST(F.getParent());
3869 for (const auto &BB : F) {
3870 if (BB.hasName())
3871 continue;
3872 int Slot = MST.getLocalSlot(&BB);
3873 if (Slot == -1)
3874 continue;
3875 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3876 }
3877}
3878
3880 unsigned Slot,
3881 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3882 return Slots2BasicBlocks.lookup(Slot);
3883}
3884
3885const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3886 if (Slots2BasicBlocks.empty())
3887 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3888 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3889}
3890
3891const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3892 if (&F == &MF.getFunction())
3893 return getIRBlock(Slot);
3894 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3895 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3896 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3897}
3898
3899MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3900 // FIXME: Currently we can't recognize temporary or local symbols and call all
3901 // of the appropriate forms to create them. However, this handles basic cases
3902 // well as most of the special aspects are recognized by a prefix on their
3903 // name, and the input names should already be unique. For test cases, keeping
3904 // the symbol name out of the symbol table isn't terribly important.
3905 return MF.getContext().getOrCreateSymbol(Name);
3906}
3907
3908bool MIParser::parseStringConstant(std::string &Result) {
3909 if (Token.isNot(MIToken::StringConstant))
3910 return error("expected string constant");
3911 Result = std::string(Token.stringValue());
3912 lex();
3913 return false;
3914}
3915
3917 StringRef Src,
3919 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3920}
3921
3924 return MIParser(PFS, Error, Src).parseBasicBlocks();
3925}
3926
3930 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3931}
3932
3934 Register &Reg, StringRef Src,
3936 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3937}
3938
3940 Register &Reg, StringRef Src,
3942 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3943}
3944
3946 VRegInfo *&Info, StringRef Src,
3948 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3949}
3950
3953 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3954}
3955
3959 return MIParser(PFS, Error, Src).parsePrefetchTarget(Target);
3960}
3963 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3964}
3965
3967 PerFunctionMIParsingState &PFS, const Value *&V,
3968 ErrorCallbackType ErrorCallback) {
3969 MIToken Token;
3970 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3971 ErrorCallback(Loc, Msg);
3972 });
3973 V = nullptr;
3974
3975 return ::parseIRValue(Token, PFS, V, ErrorCallback);
3976}
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
unsigned 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.
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.
#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:606
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))
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:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:794
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:828
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:311
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:802
Module * getParent()
Get the module that this global value is contained inside of...
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
static constexpr LLT bfloat16()
static LLT floatIEEE(unsigned SizeInBits)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition MCDwarf.h:635
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:797
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:732
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:787
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:725
static MCCFIInstruction createSetRAState(MCSymbol *L, unsigned State, MCSymbol *PACSym=nullptr, SMLoc Loc={})
.cfi_set_ra_state AArch64 set RA sign state,
Definition MCDwarf.h:708
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:660
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:808
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:685
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:628
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:670
static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
Definition MCDwarf.h:701
static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state AArch64 negate RA state.
Definition MCDwarf.h:696
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:745
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:777
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:643
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:756
static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc={})
.cfi_window_save SPARC register window is saved.
Definition MCDwarf.h:691
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:651
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:750
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:739
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:678
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:1081
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
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.
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 Register createIncompleteVirtualRegister(StringRef Name="")
Creates a new virtual register that has no register class, register bank or size assigned yet.
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
void noteNewVirtualRegister(Register Reg)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
const char * getBufferEnd() const
const char * getBufferStart() const
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:305
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:148
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:736
const char * iterator
Definition StringRef.h:60
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
LLVM_ABI std::string lower() const
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:661
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
LLVM Value Representation.
Definition Value.h:75
bool hasName() const
Definition Value.h: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:58
support::ulittle32_t Word
Definition IRSymtab.h:53
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp: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)
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:774
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:803
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:797
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:791
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:800
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:221
bool hasIntegerValue() const
Definition MILexer.h:261
bool is(TokenKind K) const
Definition MILexer.h:248
StringRef stringValue() const
Return the token's string value.
Definition MILexer.h:257
@ kw_pre_instr_symbol
Definition MILexer.h:144
@ kw_deactivation_symbol
Definition MILexer.h:149
@ kw_call_frame_size
Definition MILexer.h:156
@ 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:180
@ kw_dbg_instr_ref
Definition MILexer.h:85
@ NamedVirtualRegister
Definition MILexer.h:178
@ 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:190
@ 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:179
@ 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_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:157
@ kw_cfi_def_cfa_offset
Definition MILexer.h:90
@ md_mem_cache_hint
Definition MILexer.h:171
@ 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:169
@ kw_debug_location
Definition MILexer.h:83
@ kw_heap_alloc_marker
Definition MILexer.h:146
StringRef range() const
Definition MILexer.h:254
StringRef::iterator location() const
Definition MILexer.h:252
const APSInt & integerValue() const
Definition MILexer.h:259
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