LLVM 17.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"
42#include "llvm/IR/BasicBlock.h"
43#include "llvm/IR/Constants.h"
44#include "llvm/IR/DataLayout.h"
46#include "llvm/IR/DebugLoc.h"
47#include "llvm/IR/Function.h"
48#include "llvm/IR/InstrTypes.h"
50#include "llvm/IR/Intrinsics.h"
51#include "llvm/IR/Metadata.h"
52#include "llvm/IR/Module.h"
54#include "llvm/IR/Type.h"
55#include "llvm/IR/Value.h"
57#include "llvm/MC/LaneBitmask.h"
58#include "llvm/MC/MCContext.h"
59#include "llvm/MC/MCDwarf.h"
60#include "llvm/MC/MCInstrDesc.h"
67#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
129void PerTargetMIParsingState::initNames2InstrOpCodes() {
130 if (!Names2InstrOpCodes.empty())
131 return;
132 const auto *TII = Subtarget.getInstrInfo();
133 assert(TII && "Expected target instruction info");
134 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
135 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
136}
137
139 unsigned &OpCode) {
140 initNames2InstrOpCodes();
141 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
142 if (InstrInfo == Names2InstrOpCodes.end())
143 return true;
144 OpCode = InstrInfo->getValue();
145 return false;
146}
147
148void PerTargetMIParsingState::initNames2RegMasks() {
149 if (!Names2RegMasks.empty())
150 return;
151 const auto *TRI = Subtarget.getRegisterInfo();
152 assert(TRI && "Expected target register info");
153 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
154 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
155 assert(RegMasks.size() == RegMaskNames.size());
156 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
157 Names2RegMasks.insert(
158 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
159}
160
162 initNames2RegMasks();
163 auto RegMaskInfo = Names2RegMasks.find(Identifier);
164 if (RegMaskInfo == Names2RegMasks.end())
165 return nullptr;
166 return RegMaskInfo->getValue();
167}
168
169void PerTargetMIParsingState::initNames2SubRegIndices() {
170 if (!Names2SubRegIndices.empty())
171 return;
172 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
173 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
174 Names2SubRegIndices.insert(
175 std::make_pair(TRI->getSubRegIndexName(I), I));
176}
177
179 initNames2SubRegIndices();
180 auto SubRegInfo = Names2SubRegIndices.find(Name);
181 if (SubRegInfo == Names2SubRegIndices.end())
182 return 0;
183 return SubRegInfo->getValue();
184}
185
186void PerTargetMIParsingState::initNames2TargetIndices() {
187 if (!Names2TargetIndices.empty())
188 return;
189 const auto *TII = Subtarget.getInstrInfo();
190 assert(TII && "Expected target instruction info");
191 auto Indices = TII->getSerializableTargetIndices();
192 for (const auto &I : Indices)
193 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
194}
195
197 initNames2TargetIndices();
198 auto IndexInfo = Names2TargetIndices.find(Name);
199 if (IndexInfo == Names2TargetIndices.end())
200 return true;
201 Index = IndexInfo->second;
202 return false;
203}
204
205void PerTargetMIParsingState::initNames2DirectTargetFlags() {
206 if (!Names2DirectTargetFlags.empty())
207 return;
208
209 const auto *TII = Subtarget.getInstrInfo();
210 assert(TII && "Expected target instruction info");
212 for (const auto &I : Flags)
213 Names2DirectTargetFlags.insert(
214 std::make_pair(StringRef(I.second), I.first));
215}
216
218 unsigned &Flag) {
219 initNames2DirectTargetFlags();
220 auto FlagInfo = Names2DirectTargetFlags.find(Name);
221 if (FlagInfo == Names2DirectTargetFlags.end())
222 return true;
223 Flag = FlagInfo->second;
224 return false;
225}
226
227void PerTargetMIParsingState::initNames2BitmaskTargetFlags() {
228 if (!Names2BitmaskTargetFlags.empty())
229 return;
230
231 const auto *TII = Subtarget.getInstrInfo();
232 assert(TII && "Expected target instruction info");
234 for (const auto &I : Flags)
235 Names2BitmaskTargetFlags.insert(
236 std::make_pair(StringRef(I.second), I.first));
237}
238
240 unsigned &Flag) {
241 initNames2BitmaskTargetFlags();
242 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
243 if (FlagInfo == Names2BitmaskTargetFlags.end())
244 return true;
245 Flag = FlagInfo->second;
246 return false;
247}
248
249void PerTargetMIParsingState::initNames2MMOTargetFlags() {
250 if (!Names2MMOTargetFlags.empty())
251 return;
252
253 const auto *TII = Subtarget.getInstrInfo();
254 assert(TII && "Expected target instruction info");
255 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
256 for (const auto &I : Flags)
257 Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first));
258}
259
262 initNames2MMOTargetFlags();
263 auto FlagInfo = Names2MMOTargetFlags.find(Name);
264 if (FlagInfo == Names2MMOTargetFlags.end())
265 return true;
266 Flag = FlagInfo->second;
267 return false;
268}
269
270void PerTargetMIParsingState::initNames2RegClasses() {
271 if (!Names2RegClasses.empty())
272 return;
273
274 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
275 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
276 const auto *RC = TRI->getRegClass(I);
277 Names2RegClasses.insert(
278 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
279 }
280}
281
282void PerTargetMIParsingState::initNames2RegBanks() {
283 if (!Names2RegBanks.empty())
284 return;
285
286 const RegisterBankInfo *RBI = Subtarget.getRegBankInfo();
287 // If the target does not support GlobalISel, we may not have a
288 // register bank info.
289 if (!RBI)
290 return;
291
292 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
293 const auto &RegBank = RBI->getRegBank(I);
294 Names2RegBanks.insert(
295 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
296 }
297}
298
301 auto RegClassInfo = Names2RegClasses.find(Name);
302 if (RegClassInfo == Names2RegClasses.end())
303 return nullptr;
304 return RegClassInfo->getValue();
305}
306
308 auto RegBankInfo = Names2RegBanks.find(Name);
309 if (RegBankInfo == Names2RegBanks.end())
310 return nullptr;
311 return RegBankInfo->getValue();
312}
313
315 SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T)
316 : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) {
317}
318
320 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
321 if (I.second) {
324 Info->VReg = MRI.createIncompleteVirtualRegister();
325 I.first->second = Info;
326 }
327 return *I.first->second;
328}
329
331 assert(RegName != "" && "Expected named reg.");
332
333 auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
334 if (I.second) {
337 I.first->second = Info;
338 }
339 return *I.first->second;
340}
341
342static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
343 DenseMap<unsigned, const Value *> &Slots2Values) {
344 int Slot = MST.getLocalSlot(V);
345 if (Slot == -1)
346 return;
347 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
348}
349
350/// Creates the mapping from slot numbers to function's unnamed IR values.
351static void initSlots2Values(const Function &F,
352 DenseMap<unsigned, const Value *> &Slots2Values) {
353 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
355 for (const auto &Arg : F.args())
356 mapValueToSlot(&Arg, MST, Slots2Values);
357 for (const auto &BB : F) {
358 mapValueToSlot(&BB, MST, Slots2Values);
359 for (const auto &I : BB)
360 mapValueToSlot(&I, MST, Slots2Values);
361 }
362}
363
365 if (Slots2Values.empty())
367 return Slots2Values.lookup(Slot);
368}
369
370namespace {
371
372/// A wrapper struct around the 'MachineOperand' struct that includes a source
373/// range and other attributes.
374struct ParsedMachineOperand {
375 MachineOperand Operand;
378 std::optional<unsigned> TiedDefIdx;
379
380 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
382 std::optional<unsigned> &TiedDefIdx)
383 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
384 if (TiedDefIdx)
385 assert(Operand.isReg() && Operand.isUse() &&
386 "Only used register operands can be tied");
387 }
388};
389
390class MIParser {
391 MachineFunction &MF;
393 StringRef Source, CurrentSource;
394 SMRange SourceRange;
395 MIToken Token;
397 /// Maps from slot numbers to function's unnamed basic blocks.
399
400public:
402 StringRef Source);
404 StringRef Source, SMRange SourceRange);
405
406 /// \p SkipChar gives the number of characters to skip before looking
407 /// for the next token.
408 void lex(unsigned SkipChar = 0);
409
410 /// Report an error at the current location with the given message.
411 ///
412 /// This function always return true.
413 bool error(const Twine &Msg);
414
415 /// Report an error at the given location with the given message.
416 ///
417 /// This function always return true.
418 bool error(StringRef::iterator Loc, const Twine &Msg);
419
420 bool
421 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
422 bool parseBasicBlocks();
423 bool parse(MachineInstr *&MI);
424 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
425 bool parseStandaloneNamedRegister(Register &Reg);
426 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
427 bool parseStandaloneRegister(Register &Reg);
428 bool parseStandaloneStackObject(int &FI);
429 bool parseStandaloneMDNode(MDNode *&Node);
431 bool parseMDTuple(MDNode *&MD, bool IsDistinct);
432 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
433 bool parseMetadata(Metadata *&MD);
434
435 bool
436 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
437 bool parseBasicBlock(MachineBasicBlock &MBB,
438 MachineBasicBlock *&AddFalthroughFrom);
439 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
440 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
441
442 bool parseNamedRegister(Register &Reg);
443 bool parseVirtualRegister(VRegInfo *&Info);
444 bool parseNamedVirtualRegister(VRegInfo *&Info);
445 bool parseRegister(Register &Reg, VRegInfo *&VRegInfo);
446 bool parseRegisterFlag(unsigned &Flags);
447 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
448 bool parseSubRegisterIndex(unsigned &SubReg);
449 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
450 bool parseRegisterOperand(MachineOperand &Dest,
451 std::optional<unsigned> &TiedDefIdx,
452 bool IsDef = false);
453 bool parseImmediateOperand(MachineOperand &Dest);
454 bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
455 const Constant *&C);
457 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
458 bool parseTypedImmediateOperand(MachineOperand &Dest);
459 bool parseFPImmediateOperand(MachineOperand &Dest);
461 bool parseMBBOperand(MachineOperand &Dest);
462 bool parseStackFrameIndex(int &FI);
463 bool parseStackObjectOperand(MachineOperand &Dest);
464 bool parseFixedStackFrameIndex(int &FI);
465 bool parseFixedStackObjectOperand(MachineOperand &Dest);
466 bool parseGlobalValue(GlobalValue *&GV);
467 bool parseGlobalAddressOperand(MachineOperand &Dest);
468 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
469 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
470 bool parseJumpTableIndexOperand(MachineOperand &Dest);
471 bool parseExternalSymbolOperand(MachineOperand &Dest);
472 bool parseMCSymbolOperand(MachineOperand &Dest);
473 bool parseMDNode(MDNode *&Node);
474 bool parseDIExpression(MDNode *&Expr);
475 bool parseDILocation(MDNode *&Expr);
476 bool parseMetadataOperand(MachineOperand &Dest);
477 bool parseCFIOffset(int &Offset);
478 bool parseCFIRegister(Register &Reg);
479 bool parseCFIAddressSpace(unsigned &AddressSpace);
480 bool parseCFIEscapeValues(std::string& Values);
481 bool parseCFIOperand(MachineOperand &Dest);
482 bool parseIRBlock(BasicBlock *&BB, const Function &F);
483 bool parseBlockAddressOperand(MachineOperand &Dest);
484 bool parseIntrinsicOperand(MachineOperand &Dest);
485 bool parsePredicateOperand(MachineOperand &Dest);
486 bool parseShuffleMaskOperand(MachineOperand &Dest);
487 bool parseTargetIndexOperand(MachineOperand &Dest);
488 bool parseDbgInstrRefOperand(MachineOperand &Dest);
489 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
490 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
491 bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
492 MachineOperand &Dest,
493 std::optional<unsigned> &TiedDefIdx);
494 bool parseMachineOperandAndTargetFlags(const unsigned OpCode,
495 const unsigned OpIdx,
496 MachineOperand &Dest,
497 std::optional<unsigned> &TiedDefIdx);
498 bool parseOffset(int64_t &Offset);
499 bool parseIRBlockAddressTaken(BasicBlock *&BB);
500 bool parseAlignment(uint64_t &Alignment);
501 bool parseAddrspace(unsigned &Addrspace);
502 bool parseSectionID(std::optional<MBBSectionID> &SID);
503 bool parseBBID(std::optional<unsigned> &BBID);
504 bool parseOperandsOffset(MachineOperand &Op);
505 bool parseIRValue(const Value *&V);
506 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
507 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
508 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
509 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
510 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
511 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
512 bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol);
513 bool parseHeapAllocMarker(MDNode *&Node);
514 bool parsePCSections(MDNode *&Node);
515
516 bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
517 MachineOperand &Dest, const MIRFormatter &MF);
518
519private:
520 /// Convert the integer literal in the current token into an unsigned integer.
521 ///
522 /// Return true if an error occurred.
523 bool getUnsigned(unsigned &Result);
524
525 /// Convert the integer literal in the current token into an uint64.
526 ///
527 /// Return true if an error occurred.
528 bool getUint64(uint64_t &Result);
529
530 /// Convert the hexadecimal literal in the current token into an unsigned
531 /// APInt with a minimum bitwidth required to represent the value.
532 ///
533 /// Return true if the literal does not represent an integer value.
534 bool getHexUint(APInt &Result);
535
536 /// If the current token is of the given kind, consume it and return false.
537 /// Otherwise report an error and return true.
538 bool expectAndConsume(MIToken::TokenKind TokenKind);
539
540 /// If the current token is of the given kind, consume it and return true.
541 /// Otherwise return false.
542 bool consumeIfPresent(MIToken::TokenKind TokenKind);
543
544 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
545
546 bool assignRegisterTies(MachineInstr &MI,
548
549 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
550 const MCInstrDesc &MCID);
551
552 const BasicBlock *getIRBlock(unsigned Slot);
553 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
554
555 /// Get or create an MCSymbol for a given name.
556 MCSymbol *getOrCreateMCSymbol(StringRef Name);
557
558 /// parseStringConstant
559 /// ::= StringConstant
560 bool parseStringConstant(std::string &Result);
561
562 /// Map the location in the MI string to the corresponding location specified
563 /// in `SourceRange`.
564 SMLoc mapSMLoc(StringRef::iterator Loc);
565};
566
567} // end anonymous namespace
568
569MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
570 StringRef Source)
571 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
572{}
573
574MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
575 StringRef Source, SMRange SourceRange)
576 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source),
577 SourceRange(SourceRange), PFS(PFS) {}
578
579void MIParser::lex(unsigned SkipChar) {
580 CurrentSource = lexMIToken(
581 CurrentSource.slice(SkipChar, StringRef::npos), Token,
582 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
583}
584
585bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
586
587bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
588 const SourceMgr &SM = *PFS.SM;
589 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
590 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
591 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
592 // Create an ordinary diagnostic when the source manager's buffer is the
593 // source string.
595 return true;
596 }
597 // Create a diagnostic for a YAML string literal.
598 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
599 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
600 Source, std::nullopt, std::nullopt);
601 return true;
602}
603
604SMLoc MIParser::mapSMLoc(StringRef::iterator Loc) {
605 assert(SourceRange.isValid() && "Invalid source range");
606 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
607 return SMLoc::getFromPointer(SourceRange.Start.getPointer() +
608 (Loc - Source.data()));
609}
610
611typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
613
614static const char *toString(MIToken::TokenKind TokenKind) {
615 switch (TokenKind) {
616 case MIToken::comma:
617 return "','";
618 case MIToken::equal:
619 return "'='";
620 case MIToken::colon:
621 return "':'";
622 case MIToken::lparen:
623 return "'('";
624 case MIToken::rparen:
625 return "')'";
626 default:
627 return "<unknown token>";
628 }
629}
630
631bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
632 if (Token.isNot(TokenKind))
633 return error(Twine("expected ") + toString(TokenKind));
634 lex();
635 return false;
636}
637
638bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
639 if (Token.isNot(TokenKind))
640 return false;
641 lex();
642 return true;
643}
644
645// Parse Machine Basic Block Section ID.
646bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) {
648 lex();
649 if (Token.is(MIToken::IntegerLiteral)) {
650 unsigned Value = 0;
651 if (getUnsigned(Value))
652 return error("Unknown Section ID");
653 SID = MBBSectionID{Value};
654 } else {
655 const StringRef &S = Token.stringValue();
656 if (S == "Exception")
658 else if (S == "Cold")
660 else
661 return error("Unknown Section ID");
662 }
663 lex();
664 return false;
665}
666
667// Parse Machine Basic Block ID.
668bool MIParser::parseBBID(std::optional<unsigned> &BBID) {
669 assert(Token.is(MIToken::kw_bb_id));
670 lex();
671 unsigned Value = 0;
672 if (getUnsigned(Value))
673 return error("Unknown BB ID");
674 BBID = Value;
675 lex();
676 return false;
677}
678
679bool MIParser::parseBasicBlockDefinition(
682 unsigned ID = 0;
683 if (getUnsigned(ID))
684 return true;
685 auto Loc = Token.location();
686 auto Name = Token.stringValue();
687 lex();
688 bool MachineBlockAddressTaken = false;
689 BasicBlock *AddressTakenIRBlock = nullptr;
690 bool IsLandingPad = false;
691 bool IsInlineAsmBrIndirectTarget = false;
692 bool IsEHFuncletEntry = false;
693 std::optional<MBBSectionID> SectionID;
694 uint64_t Alignment = 0;
695 std::optional<unsigned> BBID;
696 BasicBlock *BB = nullptr;
697 if (consumeIfPresent(MIToken::lparen)) {
698 do {
699 // TODO: Report an error when multiple same attributes are specified.
700 switch (Token.kind()) {
702 MachineBlockAddressTaken = true;
703 lex();
704 break;
706 if (parseIRBlockAddressTaken(AddressTakenIRBlock))
707 return true;
708 break;
710 IsLandingPad = true;
711 lex();
712 break;
714 IsInlineAsmBrIndirectTarget = true;
715 lex();
716 break;
718 IsEHFuncletEntry = true;
719 lex();
720 break;
722 if (parseAlignment(Alignment))
723 return true;
724 break;
725 case MIToken::IRBlock:
727 // TODO: Report an error when both name and ir block are specified.
728 if (parseIRBlock(BB, MF.getFunction()))
729 return true;
730 lex();
731 break;
733 if (parseSectionID(SectionID))
734 return true;
735 break;
737 if (parseBBID(BBID))
738 return true;
739 break;
740 default:
741 break;
742 }
743 } while (consumeIfPresent(MIToken::comma));
744 if (expectAndConsume(MIToken::rparen))
745 return true;
746 }
747 if (expectAndConsume(MIToken::colon))
748 return true;
749
750 if (!Name.empty()) {
751 BB = dyn_cast_or_null<BasicBlock>(
752 MF.getFunction().getValueSymbolTable()->lookup(Name));
753 if (!BB)
754 return error(Loc, Twine("basic block '") + Name +
755 "' is not defined in the function '" +
756 MF.getName() + "'");
757 }
758 auto *MBB = MF.CreateMachineBasicBlock(BB);
759 MF.insert(MF.end(), MBB);
760 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
761 if (!WasInserted)
762 return error(Loc, Twine("redefinition of machine basic block with id #") +
763 Twine(ID));
764 if (Alignment)
765 MBB->setAlignment(Align(Alignment));
766 if (MachineBlockAddressTaken)
768 if (AddressTakenIRBlock)
769 MBB->setAddressTakenIRBlock(AddressTakenIRBlock);
770 MBB->setIsEHPad(IsLandingPad);
771 MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget);
772 MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
773 if (SectionID) {
774 MBB->setSectionID(*SectionID);
775 MF.setBBSectionsType(BasicBlockSection::List);
776 }
777 if (BBID.has_value()) {
778 // BBSectionsType is set to `List` if any basic blocks has `SectionID`.
779 // Here, we set it to `Labels` if it hasn't been set above.
780 if (!MF.hasBBSections())
781 MF.setBBSectionsType(BasicBlockSection::Labels);
782 MBB->setBBID(BBID.value());
783 }
784 return false;
785}
786
787bool MIParser::parseBasicBlockDefinitions(
789 lex();
790 // Skip until the first machine basic block.
791 while (Token.is(MIToken::Newline))
792 lex();
793 if (Token.isErrorOrEOF())
794 return Token.isError();
795 if (Token.isNot(MIToken::MachineBasicBlockLabel))
796 return error("expected a basic block definition before instructions");
797 unsigned BraceDepth = 0;
798 do {
799 if (parseBasicBlockDefinition(MBBSlots))
800 return true;
801 bool IsAfterNewline = false;
802 // Skip until the next machine basic block.
803 while (true) {
804 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
805 Token.isErrorOrEOF())
806 break;
807 else if (Token.is(MIToken::MachineBasicBlockLabel))
808 return error("basic block definition should be located at the start of "
809 "the line");
810 else if (consumeIfPresent(MIToken::Newline)) {
811 IsAfterNewline = true;
812 continue;
813 }
814 IsAfterNewline = false;
815 if (Token.is(MIToken::lbrace))
816 ++BraceDepth;
817 if (Token.is(MIToken::rbrace)) {
818 if (!BraceDepth)
819 return error("extraneous closing brace ('}')");
820 --BraceDepth;
821 }
822 lex();
823 }
824 // Verify that we closed all of the '{' at the end of a file or a block.
825 if (!Token.isError() && BraceDepth)
826 return error("expected '}'"); // FIXME: Report a note that shows '{'.
827 } while (!Token.isErrorOrEOF());
828 return Token.isError();
829}
830
831bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
832 assert(Token.is(MIToken::kw_liveins));
833 lex();
834 if (expectAndConsume(MIToken::colon))
835 return true;
836 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
837 return false;
838 do {
839 if (Token.isNot(MIToken::NamedRegister))
840 return error("expected a named register");
842 if (parseNamedRegister(Reg))
843 return true;
844 lex();
846 if (consumeIfPresent(MIToken::colon)) {
847 // Parse lane mask.
848 if (Token.isNot(MIToken::IntegerLiteral) &&
849 Token.isNot(MIToken::HexLiteral))
850 return error("expected a lane mask");
851 static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
852 "Use correct get-function for lane mask");
854 if (getUint64(V))
855 return error("invalid lane mask value");
856 Mask = LaneBitmask(V);
857 lex();
858 }
859 MBB.addLiveIn(Reg, Mask);
860 } while (consumeIfPresent(MIToken::comma));
861 return false;
862}
863
864bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
866 lex();
867 if (expectAndConsume(MIToken::colon))
868 return true;
869 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
870 return false;
871 do {
872 if (Token.isNot(MIToken::MachineBasicBlock))
873 return error("expected a machine basic block reference");
874 MachineBasicBlock *SuccMBB = nullptr;
875 if (parseMBBReference(SuccMBB))
876 return true;
877 lex();
878 unsigned Weight = 0;
879 if (consumeIfPresent(MIToken::lparen)) {
880 if (Token.isNot(MIToken::IntegerLiteral) &&
881 Token.isNot(MIToken::HexLiteral))
882 return error("expected an integer literal after '('");
883 if (getUnsigned(Weight))
884 return true;
885 lex();
886 if (expectAndConsume(MIToken::rparen))
887 return true;
888 }
890 } while (consumeIfPresent(MIToken::comma));
892 return false;
893}
894
895bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
896 MachineBasicBlock *&AddFalthroughFrom) {
897 // Skip the definition.
899 lex();
900 if (consumeIfPresent(MIToken::lparen)) {
901 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
902 lex();
903 consumeIfPresent(MIToken::rparen);
904 }
905 consumeIfPresent(MIToken::colon);
906
907 // Parse the liveins and successors.
908 // N.B: Multiple lists of successors and liveins are allowed and they're
909 // merged into one.
910 // Example:
911 // liveins: $edi
912 // liveins: $esi
913 //
914 // is equivalent to
915 // liveins: $edi, $esi
916 bool ExplicitSuccessors = false;
917 while (true) {
918 if (Token.is(MIToken::kw_successors)) {
919 if (parseBasicBlockSuccessors(MBB))
920 return true;
921 ExplicitSuccessors = true;
922 } else if (Token.is(MIToken::kw_liveins)) {
923 if (parseBasicBlockLiveins(MBB))
924 return true;
925 } else if (consumeIfPresent(MIToken::Newline)) {
926 continue;
927 } else
928 break;
929 if (!Token.isNewlineOrEOF())
930 return error("expected line break at the end of a list");
931 lex();
932 }
933
934 // Parse the instructions.
935 bool IsInBundle = false;
936 MachineInstr *PrevMI = nullptr;
937 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
938 !Token.is(MIToken::Eof)) {
939 if (consumeIfPresent(MIToken::Newline))
940 continue;
941 if (consumeIfPresent(MIToken::rbrace)) {
942 // The first parsing pass should verify that all closing '}' have an
943 // opening '{'.
944 assert(IsInBundle);
945 IsInBundle = false;
946 continue;
947 }
948 MachineInstr *MI = nullptr;
949 if (parse(MI))
950 return true;
951 MBB.insert(MBB.end(), MI);
952 if (IsInBundle) {
955 }
956 PrevMI = MI;
957 if (Token.is(MIToken::lbrace)) {
958 if (IsInBundle)
959 return error("nested instruction bundles are not allowed");
960 lex();
961 // This instruction is the start of the bundle.
963 IsInBundle = true;
964 if (!Token.is(MIToken::Newline))
965 // The next instruction can be on the same line.
966 continue;
967 }
968 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
969 lex();
970 }
971
972 // Construct successor list by searching for basic block machine operands.
973 if (!ExplicitSuccessors) {
975 bool IsFallthrough;
976 guessSuccessors(MBB, Successors, IsFallthrough);
977 for (MachineBasicBlock *Succ : Successors)
978 MBB.addSuccessor(Succ);
979
980 if (IsFallthrough) {
981 AddFalthroughFrom = &MBB;
982 } else {
984 }
985 }
986
987 return false;
988}
989
990bool MIParser::parseBasicBlocks() {
991 lex();
992 // Skip until the first machine basic block.
993 while (Token.is(MIToken::Newline))
994 lex();
995 if (Token.isErrorOrEOF())
996 return Token.isError();
997 // The first parsing pass should have verified that this token is a MBB label
998 // in the 'parseBasicBlockDefinitions' method.
1000 MachineBasicBlock *AddFalthroughFrom = nullptr;
1001 do {
1002 MachineBasicBlock *MBB = nullptr;
1004 return true;
1005 if (AddFalthroughFrom) {
1006 if (!AddFalthroughFrom->isSuccessor(MBB))
1007 AddFalthroughFrom->addSuccessor(MBB);
1008 AddFalthroughFrom->normalizeSuccProbs();
1009 AddFalthroughFrom = nullptr;
1010 }
1011 if (parseBasicBlock(*MBB, AddFalthroughFrom))
1012 return true;
1013 // The method 'parseBasicBlock' should parse the whole block until the next
1014 // block or the end of file.
1015 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
1016 } while (Token.isNot(MIToken::Eof));
1017 return false;
1018}
1019
1020bool MIParser::parse(MachineInstr *&MI) {
1021 // Parse any register operands before '='
1024 while (Token.isRegister() || Token.isRegisterFlag()) {
1025 auto Loc = Token.location();
1026 std::optional<unsigned> TiedDefIdx;
1027 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
1028 return true;
1029 Operands.push_back(
1030 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1031 if (Token.isNot(MIToken::comma))
1032 break;
1033 lex();
1034 }
1035 if (!Operands.empty() && expectAndConsume(MIToken::equal))
1036 return true;
1037
1038 unsigned OpCode, Flags = 0;
1039 if (Token.isError() || parseInstruction(OpCode, Flags))
1040 return true;
1041
1042 // Parse the remaining machine operands.
1043 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) &&
1044 Token.isNot(MIToken::kw_post_instr_symbol) &&
1045 Token.isNot(MIToken::kw_heap_alloc_marker) &&
1046 Token.isNot(MIToken::kw_pcsections) &&
1047 Token.isNot(MIToken::kw_cfi_type) &&
1048 Token.isNot(MIToken::kw_debug_location) &&
1049 Token.isNot(MIToken::kw_debug_instr_number) &&
1050 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
1051 auto Loc = Token.location();
1052 std::optional<unsigned> TiedDefIdx;
1053 if (parseMachineOperandAndTargetFlags(OpCode, Operands.size(), MO, TiedDefIdx))
1054 return true;
1055 Operands.push_back(
1056 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
1057 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
1058 Token.is(MIToken::lbrace))
1059 break;
1060 if (Token.isNot(MIToken::comma))
1061 return error("expected ',' before the next machine operand");
1062 lex();
1063 }
1064
1065 MCSymbol *PreInstrSymbol = nullptr;
1066 if (Token.is(MIToken::kw_pre_instr_symbol))
1067 if (parsePreOrPostInstrSymbol(PreInstrSymbol))
1068 return true;
1069 MCSymbol *PostInstrSymbol = nullptr;
1070 if (Token.is(MIToken::kw_post_instr_symbol))
1071 if (parsePreOrPostInstrSymbol(PostInstrSymbol))
1072 return true;
1073 MDNode *HeapAllocMarker = nullptr;
1074 if (Token.is(MIToken::kw_heap_alloc_marker))
1075 if (parseHeapAllocMarker(HeapAllocMarker))
1076 return true;
1077 MDNode *PCSections = nullptr;
1078 if (Token.is(MIToken::kw_pcsections))
1079 if (parsePCSections(PCSections))
1080 return true;
1081
1082 unsigned CFIType = 0;
1083 if (Token.is(MIToken::kw_cfi_type)) {
1084 lex();
1085 if (Token.isNot(MIToken::IntegerLiteral))
1086 return error("expected an integer literal after 'cfi-type'");
1087 // getUnsigned is sufficient for 32-bit integers.
1088 if (getUnsigned(CFIType))
1089 return true;
1090 lex();
1091 // Lex past trailing comma if present.
1092 if (Token.is(MIToken::comma))
1093 lex();
1094 }
1095
1096 unsigned InstrNum = 0;
1097 if (Token.is(MIToken::kw_debug_instr_number)) {
1098 lex();
1099 if (Token.isNot(MIToken::IntegerLiteral))
1100 return error("expected an integer literal after 'debug-instr-number'");
1101 if (getUnsigned(InstrNum))
1102 return true;
1103 lex();
1104 // Lex past trailing comma if present.
1105 if (Token.is(MIToken::comma))
1106 lex();
1107 }
1108
1109 DebugLoc DebugLocation;
1110 if (Token.is(MIToken::kw_debug_location)) {
1111 lex();
1112 MDNode *Node = nullptr;
1113 if (Token.is(MIToken::exclaim)) {
1114 if (parseMDNode(Node))
1115 return true;
1116 } else if (Token.is(MIToken::md_dilocation)) {
1117 if (parseDILocation(Node))
1118 return true;
1119 } else
1120 return error("expected a metadata node after 'debug-location'");
1121 if (!isa<DILocation>(Node))
1122 return error("referenced metadata is not a DILocation");
1123 DebugLocation = DebugLoc(Node);
1124 }
1125
1126 // Parse the machine memory operands.
1128 if (Token.is(MIToken::coloncolon)) {
1129 lex();
1130 while (!Token.isNewlineOrEOF()) {
1131 MachineMemOperand *MemOp = nullptr;
1132 if (parseMachineMemoryOperand(MemOp))
1133 return true;
1134 MemOperands.push_back(MemOp);
1135 if (Token.isNewlineOrEOF())
1136 break;
1137 if (Token.isNot(MIToken::comma))
1138 return error("expected ',' before the next machine memory operand");
1139 lex();
1140 }
1141 }
1142
1143 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
1144 if (!MCID.isVariadic()) {
1145 // FIXME: Move the implicit operand verification to the machine verifier.
1146 if (verifyImplicitOperands(Operands, MCID))
1147 return true;
1148 }
1149
1150 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
1151 MI->setFlags(Flags);
1152
1153 unsigned NumExplicitOps = 0;
1154 for (const auto &Operand : Operands) {
1155 bool IsImplicitOp = Operand.Operand.isReg() && Operand.Operand.isImplicit();
1156 if (!IsImplicitOp) {
1157 if (!MCID.isVariadic() && NumExplicitOps >= MCID.getNumOperands() &&
1158 !Operand.Operand.isValidExcessOperand())
1159 return error(Operand.Begin, "too many operands for instruction");
1160
1161 ++NumExplicitOps;
1162 }
1163
1164 MI->addOperand(MF, Operand.Operand);
1165 }
1166
1167 if (assignRegisterTies(*MI, Operands))
1168 return true;
1169 if (PreInstrSymbol)
1170 MI->setPreInstrSymbol(MF, PreInstrSymbol);
1171 if (PostInstrSymbol)
1172 MI->setPostInstrSymbol(MF, PostInstrSymbol);
1173 if (HeapAllocMarker)
1174 MI->setHeapAllocMarker(MF, HeapAllocMarker);
1175 if (PCSections)
1176 MI->setPCSections(MF, PCSections);
1177 if (CFIType)
1178 MI->setCFIType(MF, CFIType);
1179 if (!MemOperands.empty())
1180 MI->setMemRefs(MF, MemOperands);
1181 if (InstrNum)
1182 MI->setDebugInstrNum(InstrNum);
1183 return false;
1184}
1185
1186bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
1187 lex();
1188 if (Token.isNot(MIToken::MachineBasicBlock))
1189 return error("expected a machine basic block reference");
1191 return true;
1192 lex();
1193 if (Token.isNot(MIToken::Eof))
1194 return error(
1195 "expected end of string after the machine basic block reference");
1196 return false;
1197}
1198
1199bool MIParser::parseStandaloneNamedRegister(Register &Reg) {
1200 lex();
1201 if (Token.isNot(MIToken::NamedRegister))
1202 return error("expected a named register");
1203 if (parseNamedRegister(Reg))
1204 return true;
1205 lex();
1206 if (Token.isNot(MIToken::Eof))
1207 return error("expected end of string after the register reference");
1208 return false;
1209}
1210
1211bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
1212 lex();
1213 if (Token.isNot(MIToken::VirtualRegister))
1214 return error("expected a virtual register");
1215 if (parseVirtualRegister(Info))
1216 return true;
1217 lex();
1218 if (Token.isNot(MIToken::Eof))
1219 return error("expected end of string after the register reference");
1220 return false;
1221}
1222
1223bool MIParser::parseStandaloneRegister(Register &Reg) {
1224 lex();
1225 if (Token.isNot(MIToken::NamedRegister) &&
1226 Token.isNot(MIToken::VirtualRegister))
1227 return error("expected either a named or virtual register");
1228
1229 VRegInfo *Info;
1230 if (parseRegister(Reg, Info))
1231 return true;
1232
1233 lex();
1234 if (Token.isNot(MIToken::Eof))
1235 return error("expected end of string after the register reference");
1236 return false;
1237}
1238
1239bool MIParser::parseStandaloneStackObject(int &FI) {
1240 lex();
1241 if (Token.isNot(MIToken::StackObject))
1242 return error("expected a stack object");
1243 if (parseStackFrameIndex(FI))
1244 return true;
1245 if (Token.isNot(MIToken::Eof))
1246 return error("expected end of string after the stack object reference");
1247 return false;
1248}
1249
1250bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
1251 lex();
1252 if (Token.is(MIToken::exclaim)) {
1253 if (parseMDNode(Node))
1254 return true;
1255 } else if (Token.is(MIToken::md_diexpr)) {
1256 if (parseDIExpression(Node))
1257 return true;
1258 } else if (Token.is(MIToken::md_dilocation)) {
1259 if (parseDILocation(Node))
1260 return true;
1261 } else
1262 return error("expected a metadata node");
1263 if (Token.isNot(MIToken::Eof))
1264 return error("expected end of string after the metadata node");
1265 return false;
1266}
1267
1268bool MIParser::parseMachineMetadata() {
1269 lex();
1270 if (Token.isNot(MIToken::exclaim))
1271 return error("expected a metadata node");
1272
1273 lex();
1274 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1275 return error("expected metadata id after '!'");
1276 unsigned ID = 0;
1277 if (getUnsigned(ID))
1278 return true;
1279 lex();
1280 if (expectAndConsume(MIToken::equal))
1281 return true;
1282 bool IsDistinct = Token.is(MIToken::kw_distinct);
1283 if (IsDistinct)
1284 lex();
1285 if (Token.isNot(MIToken::exclaim))
1286 return error("expected a metadata node");
1287 lex();
1288
1289 MDNode *MD;
1290 if (parseMDTuple(MD, IsDistinct))
1291 return true;
1292
1293 auto FI = PFS.MachineForwardRefMDNodes.find(ID);
1294 if (FI != PFS.MachineForwardRefMDNodes.end()) {
1295 FI->second.first->replaceAllUsesWith(MD);
1296 PFS.MachineForwardRefMDNodes.erase(FI);
1297
1298 assert(PFS.MachineMetadataNodes[ID] == MD && "Tracking VH didn't work");
1299 } else {
1300 if (PFS.MachineMetadataNodes.count(ID))
1301 return error("Metadata id is already used");
1302 PFS.MachineMetadataNodes[ID].reset(MD);
1303 }
1304
1305 return false;
1306}
1307
1308bool MIParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
1310 if (parseMDNodeVector(Elts))
1311 return true;
1312 MD = (IsDistinct ? MDTuple::getDistinct
1313 : MDTuple::get)(MF.getFunction().getContext(), Elts);
1314 return false;
1315}
1316
1317bool MIParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
1318 if (Token.isNot(MIToken::lbrace))
1319 return error("expected '{' here");
1320 lex();
1321
1322 if (Token.is(MIToken::rbrace)) {
1323 lex();
1324 return false;
1325 }
1326
1327 do {
1328 Metadata *MD;
1329 if (parseMetadata(MD))
1330 return true;
1331
1332 Elts.push_back(MD);
1333
1334 if (Token.isNot(MIToken::comma))
1335 break;
1336 lex();
1337 } while (true);
1338
1339 if (Token.isNot(MIToken::rbrace))
1340 return error("expected end of metadata node");
1341 lex();
1342
1343 return false;
1344}
1345
1346// ::= !42
1347// ::= !"string"
1348bool MIParser::parseMetadata(Metadata *&MD) {
1349 if (Token.isNot(MIToken::exclaim))
1350 return error("expected '!' here");
1351 lex();
1352
1353 if (Token.is(MIToken::StringConstant)) {
1354 std::string Str;
1355 if (parseStringConstant(Str))
1356 return true;
1357 MD = MDString::get(MF.getFunction().getContext(), Str);
1358 return false;
1359 }
1360
1361 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1362 return error("expected metadata id after '!'");
1363
1364 SMLoc Loc = mapSMLoc(Token.location());
1365
1366 unsigned ID = 0;
1367 if (getUnsigned(ID))
1368 return true;
1369 lex();
1370
1371 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1372 if (NodeInfo != PFS.IRSlots.MetadataNodes.end()) {
1373 MD = NodeInfo->second.get();
1374 return false;
1375 }
1376 // Check machine metadata.
1377 NodeInfo = PFS.MachineMetadataNodes.find(ID);
1378 if (NodeInfo != PFS.MachineMetadataNodes.end()) {
1379 MD = NodeInfo->second.get();
1380 return false;
1381 }
1382 // Forward reference.
1383 auto &FwdRef = PFS.MachineForwardRefMDNodes[ID];
1384 FwdRef = std::make_pair(
1385 MDTuple::getTemporary(MF.getFunction().getContext(), std::nullopt), Loc);
1386 PFS.MachineMetadataNodes[ID].reset(FwdRef.first.get());
1387 MD = FwdRef.first.get();
1388
1389 return false;
1390}
1391
1392static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
1393 assert(MO.isImplicit());
1394 return MO.isDef() ? "implicit-def" : "implicit";
1395}
1396
1397static std::string getRegisterName(const TargetRegisterInfo *TRI,
1398 Register Reg) {
1399 assert(Reg.isPhysical() && "expected phys reg");
1400 return StringRef(TRI->getName(Reg)).lower();
1401}
1402
1403/// Return true if the parsed machine operands contain a given machine operand.
1404static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
1406 for (const auto &I : Operands) {
1407 if (ImplicitOperand.isIdenticalTo(I.Operand))
1408 return true;
1409 }
1410 return false;
1411}
1412
1413bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
1414 const MCInstrDesc &MCID) {
1415 if (MCID.isCall())
1416 // We can't verify call instructions as they can contain arbitrary implicit
1417 // register and register mask operands.
1418 return false;
1419
1420 // Gather all the expected implicit operands.
1421 SmallVector<MachineOperand, 4> ImplicitOperands;
1422 for (MCPhysReg ImpDef : MCID.implicit_defs())
1423 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpDef, true, true));
1424 for (MCPhysReg ImpUse : MCID.implicit_uses())
1425 ImplicitOperands.push_back(MachineOperand::CreateReg(ImpUse, false, true));
1426
1427 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1428 assert(TRI && "Expected target register info");
1429 for (const auto &I : ImplicitOperands) {
1431 continue;
1432 return error(Operands.empty() ? Token.location() : Operands.back().End,
1433 Twine("missing implicit register operand '") +
1435 getRegisterName(TRI, I.getReg()) + "'");
1436 }
1437 return false;
1438}
1439
1440bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
1441 // Allow frame and fast math flags for OPCODE
1442 while (Token.is(MIToken::kw_frame_setup) ||
1443 Token.is(MIToken::kw_frame_destroy) ||
1444 Token.is(MIToken::kw_nnan) ||
1445 Token.is(MIToken::kw_ninf) ||
1446 Token.is(MIToken::kw_nsz) ||
1447 Token.is(MIToken::kw_arcp) ||
1448 Token.is(MIToken::kw_contract) ||
1449 Token.is(MIToken::kw_afn) ||
1450 Token.is(MIToken::kw_reassoc) ||
1451 Token.is(MIToken::kw_nuw) ||
1452 Token.is(MIToken::kw_nsw) ||
1453 Token.is(MIToken::kw_exact) ||
1454 Token.is(MIToken::kw_nofpexcept)) {
1455 // Mine frame and fast math flags
1456 if (Token.is(MIToken::kw_frame_setup))
1458 if (Token.is(MIToken::kw_frame_destroy))
1460 if (Token.is(MIToken::kw_nnan))
1462 if (Token.is(MIToken::kw_ninf))
1464 if (Token.is(MIToken::kw_nsz))
1466 if (Token.is(MIToken::kw_arcp))
1468 if (Token.is(MIToken::kw_contract))
1470 if (Token.is(MIToken::kw_afn))
1472 if (Token.is(MIToken::kw_reassoc))
1474 if (Token.is(MIToken::kw_nuw))
1476 if (Token.is(MIToken::kw_nsw))
1478 if (Token.is(MIToken::kw_exact))
1480 if (Token.is(MIToken::kw_nofpexcept))
1482
1483 lex();
1484 }
1485 if (Token.isNot(MIToken::Identifier))
1486 return error("expected a machine instruction");
1487 StringRef InstrName = Token.stringValue();
1488 if (PFS.Target.parseInstrName(InstrName, OpCode))
1489 return error(Twine("unknown machine instruction name '") + InstrName + "'");
1490 lex();
1491 return false;
1492}
1493
1494bool MIParser::parseNamedRegister(Register &Reg) {
1495 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
1496 StringRef Name = Token.stringValue();
1497 if (PFS.Target.getRegisterByName(Name, Reg))
1498 return error(Twine("unknown register name '") + Name + "'");
1499 return false;
1500}
1501
1502bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
1503 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
1504 StringRef Name = Token.stringValue();
1505 // TODO: Check that the VReg name is not the same as a physical register name.
1506 // If it is, then print a warning (when warnings are implemented).
1507 Info = &PFS.getVRegInfoNamed(Name);
1508 return false;
1509}
1510
1511bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
1512 if (Token.is(MIToken::NamedVirtualRegister))
1513 return parseNamedVirtualRegister(Info);
1514 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
1515 unsigned ID;
1516 if (getUnsigned(ID))
1517 return true;
1518 Info = &PFS.getVRegInfo(ID);
1519 return false;
1520}
1521
1522bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) {
1523 switch (Token.kind()) {
1525 Reg = 0;
1526 return false;
1528 return parseNamedRegister(Reg);
1531 if (parseVirtualRegister(Info))
1532 return true;
1533 Reg = Info->VReg;
1534 return false;
1535 // TODO: Parse other register kinds.
1536 default:
1537 llvm_unreachable("The current token should be a register");
1538 }
1539}
1540
1541bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1542 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1543 return error("expected '_', register class, or register bank name");
1544 StringRef::iterator Loc = Token.location();
1545 StringRef Name = Token.stringValue();
1546
1547 // Was it a register class?
1548 const TargetRegisterClass *RC = PFS.Target.getRegClass(Name);
1549 if (RC) {
1550 lex();
1551
1552 switch (RegInfo.Kind) {
1553 case VRegInfo::UNKNOWN:
1554 case VRegInfo::NORMAL:
1556 if (RegInfo.Explicit && RegInfo.D.RC != RC) {
1557 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1558 return error(Loc, Twine("conflicting register classes, previously: ") +
1559 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1560 }
1561 RegInfo.D.RC = RC;
1562 RegInfo.Explicit = true;
1563 return false;
1564
1565 case VRegInfo::GENERIC:
1566 case VRegInfo::REGBANK:
1567 return error(Loc, "register class specification on generic register");
1568 }
1569 llvm_unreachable("Unexpected register kind");
1570 }
1571
1572 // Should be a register bank or a generic register.
1573 const RegisterBank *RegBank = nullptr;
1574 if (Name != "_") {
1575 RegBank = PFS.Target.getRegBank(Name);
1576 if (!RegBank)
1577 return error(Loc, "expected '_', register class, or register bank name");
1578 }
1579
1580 lex();
1581
1582 switch (RegInfo.Kind) {
1583 case VRegInfo::UNKNOWN:
1584 case VRegInfo::GENERIC:
1585 case VRegInfo::REGBANK:
1587 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1588 return error(Loc, "conflicting generic register banks");
1589 RegInfo.D.RegBank = RegBank;
1590 RegInfo.Explicit = true;
1591 return false;
1592
1593 case VRegInfo::NORMAL:
1594 return error(Loc, "register bank specification on normal register");
1595 }
1596 llvm_unreachable("Unexpected register kind");
1597}
1598
1599bool MIParser::parseRegisterFlag(unsigned &Flags) {
1600 const unsigned OldFlags = Flags;
1601 switch (Token.kind()) {
1604 break;
1607 break;
1608 case MIToken::kw_def:
1610 break;
1611 case MIToken::kw_dead:
1613 break;
1614 case MIToken::kw_killed:
1616 break;
1617 case MIToken::kw_undef:
1619 break;
1622 break;
1625 break;
1628 break;
1631 break;
1632 default:
1633 llvm_unreachable("The current token should be a register flag");
1634 }
1635 if (OldFlags == Flags)
1636 // We know that the same flag is specified more than once when the flags
1637 // weren't modified.
1638 return error("duplicate '" + Token.stringValue() + "' register flag");
1639 lex();
1640 return false;
1641}
1642
1643bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1644 assert(Token.is(MIToken::dot));
1645 lex();
1646 if (Token.isNot(MIToken::Identifier))
1647 return error("expected a subregister index after '.'");
1648 auto Name = Token.stringValue();
1649 SubReg = PFS.Target.getSubRegIndex(Name);
1650 if (!SubReg)
1651 return error(Twine("use of unknown subregister index '") + Name + "'");
1652 lex();
1653 return false;
1654}
1655
1656bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1657 if (!consumeIfPresent(MIToken::kw_tied_def))
1658 return true;
1659 if (Token.isNot(MIToken::IntegerLiteral))
1660 return error("expected an integer literal after 'tied-def'");
1661 if (getUnsigned(TiedDefIdx))
1662 return true;
1663 lex();
1664 if (expectAndConsume(MIToken::rparen))
1665 return true;
1666 return false;
1667}
1668
1669bool MIParser::assignRegisterTies(MachineInstr &MI,
1671 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1672 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1673 if (!Operands[I].TiedDefIdx)
1674 continue;
1675 // The parser ensures that this operand is a register use, so we just have
1676 // to check the tied-def operand.
1677 unsigned DefIdx = *Operands[I].TiedDefIdx;
1678 if (DefIdx >= E)
1679 return error(Operands[I].Begin,
1680 Twine("use of invalid tied-def operand index '" +
1681 Twine(DefIdx) + "'; instruction has only ") +
1682 Twine(E) + " operands");
1683 const auto &DefOperand = Operands[DefIdx].Operand;
1684 if (!DefOperand.isReg() || !DefOperand.isDef())
1685 // FIXME: add note with the def operand.
1686 return error(Operands[I].Begin,
1687 Twine("use of invalid tied-def operand index '") +
1688 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1689 " isn't a defined register");
1690 // Check that the tied-def operand wasn't tied elsewhere.
1691 for (const auto &TiedPair : TiedRegisterPairs) {
1692 if (TiedPair.first == DefIdx)
1693 return error(Operands[I].Begin,
1694 Twine("the tied-def operand #") + Twine(DefIdx) +
1695 " is already tied with another register operand");
1696 }
1697 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1698 }
1699 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1700 // indices must be less than tied max.
1701 for (const auto &TiedPair : TiedRegisterPairs)
1702 MI.tieOperands(TiedPair.first, TiedPair.second);
1703 return false;
1704}
1705
1706bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1707 std::optional<unsigned> &TiedDefIdx,
1708 bool IsDef) {
1709 unsigned Flags = IsDef ? RegState::Define : 0;
1710 while (Token.isRegisterFlag()) {
1711 if (parseRegisterFlag(Flags))
1712 return true;
1713 }
1714 if (!Token.isRegister())
1715 return error("expected a register after register flags");
1716 Register Reg;
1718 if (parseRegister(Reg, RegInfo))
1719 return true;
1720 lex();
1721 unsigned SubReg = 0;
1722 if (Token.is(MIToken::dot)) {
1723 if (parseSubRegisterIndex(SubReg))
1724 return true;
1725 if (!Reg.isVirtual())
1726 return error("subregister index expects a virtual register");
1727 }
1728 if (Token.is(MIToken::colon)) {
1729 if (!Reg.isVirtual())
1730 return error("register class specification expects a virtual register");
1731 lex();
1732 if (parseRegisterClassOrBank(*RegInfo))
1733 return true;
1734 }
1735 MachineRegisterInfo &MRI = MF.getRegInfo();
1736 if ((Flags & RegState::Define) == 0) {
1737 if (consumeIfPresent(MIToken::lparen)) {
1738 unsigned Idx;
1739 if (!parseRegisterTiedDefIndex(Idx))
1740 TiedDefIdx = Idx;
1741 else {
1742 // Try a redundant low-level type.
1743 LLT Ty;
1744 if (parseLowLevelType(Token.location(), Ty))
1745 return error("expected tied-def or low-level type after '('");
1746
1747 if (expectAndConsume(MIToken::rparen))
1748 return true;
1749
1750 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1751 return error("inconsistent type for generic virtual register");
1752
1753 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1754 MRI.setType(Reg, Ty);
1755 }
1756 }
1757 } else if (consumeIfPresent(MIToken::lparen)) {
1758 // Virtual registers may have a tpe with GlobalISel.
1759 if (!Reg.isVirtual())
1760 return error("unexpected type on physical register");
1761
1762 LLT Ty;
1763 if (parseLowLevelType(Token.location(), Ty))
1764 return true;
1765
1766 if (expectAndConsume(MIToken::rparen))
1767 return true;
1768
1769 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1770 return error("inconsistent type for generic virtual register");
1771
1772 MRI.setRegClassOrRegBank(Reg, static_cast<RegisterBank *>(nullptr));
1773 MRI.setType(Reg, Ty);
1774 } else if (Reg.isVirtual()) {
1775 // Generic virtual registers must have a type.
1776 // If we end up here this means the type hasn't been specified and
1777 // this is bad!
1778 if (RegInfo->Kind == VRegInfo::GENERIC ||
1780 return error("generic virtual registers must have a type");
1781 }
1782
1783 if (Flags & RegState::Define) {
1784 if (Flags & RegState::Kill)
1785 return error("cannot have a killed def operand");
1786 } else {
1787 if (Flags & RegState::Dead)
1788 return error("cannot have a dead use operand");
1789 }
1790
1792 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1793 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1796
1797 return false;
1798}
1799
1800bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1802 const APSInt &Int = Token.integerValue();
1803 if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value())
1804 Dest = MachineOperand::CreateImm(*SImm);
1805 else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value())
1806 Dest = MachineOperand::CreateImm(*UImm);
1807 else
1808 return error("integer literal is too large to be an immediate operand");
1809 lex();
1810 return false;
1811}
1812
1813bool MIParser::parseTargetImmMnemonic(const unsigned OpCode,
1814 const unsigned OpIdx,
1815 MachineOperand &Dest,
1816 const MIRFormatter &MF) {
1817 assert(Token.is(MIToken::dot));
1818 auto Loc = Token.location(); // record start position
1819 size_t Len = 1; // for "."
1820 lex();
1821
1822 // Handle the case that mnemonic starts with number.
1823 if (Token.is(MIToken::IntegerLiteral)) {
1824 Len += Token.range().size();
1825 lex();
1826 }
1827
1828 StringRef Src;
1829 if (Token.is(MIToken::comma))
1830 Src = StringRef(Loc, Len);
1831 else {
1832 assert(Token.is(MIToken::Identifier));
1833 Src = StringRef(Loc, Len + Token.stringValue().size());
1834 }
1835 int64_t Val;
1836 if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Val,
1837 [this](StringRef::iterator Loc, const Twine &Msg)
1838 -> bool { return error(Loc, Msg); }))
1839 return true;
1840
1841 Dest = MachineOperand::CreateImm(Val);
1842 if (!Token.is(MIToken::comma))
1843 lex();
1844 return false;
1845}
1846
1847static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1848 PerFunctionMIParsingState &PFS, const Constant *&C,
1849 ErrorCallbackType ErrCB) {
1850 auto Source = StringValue.str(); // The source has to be null terminated.
1851 SMDiagnostic Err;
1852 C = parseConstantValue(Source, Err, *PFS.MF.getFunction().getParent(),
1853 &PFS.IRSlots);
1854 if (!C)
1855 return ErrCB(Loc + Err.getColumnNo(), Err.getMessage());
1856 return false;
1857}
1858
1859bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1860 const Constant *&C) {
1861 return ::parseIRConstant(
1862 Loc, StringValue, PFS, C,
1863 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
1864 return error(Loc, Msg);
1865 });
1866}
1867
1868bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1869 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1870 return true;
1871 lex();
1872 return false;
1873}
1874
1875// See LLT implementation for bit size limits.
1877 return Size != 0 && isUInt<16>(Size);
1878}
1879
1881 return NumElts != 0 && isUInt<16>(NumElts);
1882}
1883
1884static bool verifyAddrSpace(uint64_t AddrSpace) {
1885 return isUInt<24>(AddrSpace);
1886}
1887
1888bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1889 if (Token.range().front() == 's' || Token.range().front() == 'p') {
1890 StringRef SizeStr = Token.range().drop_front();
1891 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1892 return error("expected integers after 's'/'p' type character");
1893 }
1894
1895 if (Token.range().front() == 's') {
1896 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1897 if (!verifyScalarSize(ScalarSize))
1898 return error("invalid size for scalar type");
1899
1900 Ty = LLT::scalar(ScalarSize);
1901 lex();
1902 return false;
1903 } else if (Token.range().front() == 'p') {
1904 const DataLayout &DL = MF.getDataLayout();
1905 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
1906 if (!verifyAddrSpace(AS))
1907 return error("invalid address space number");
1908
1909 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1910 lex();
1911 return false;
1912 }
1913
1914 // Now we're looking for a vector.
1915 if (Token.isNot(MIToken::less))
1916 return error(Loc,
1917 "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type");
1918 lex();
1919
1920 if (Token.isNot(MIToken::IntegerLiteral))
1921 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1922 uint64_t NumElements = Token.integerValue().getZExtValue();
1923 if (!verifyVectorElementCount(NumElements))
1924 return error("invalid number of vector elements");
1925
1926 lex();
1927
1928 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1929 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1930 lex();
1931
1932 if (Token.range().front() != 's' && Token.range().front() != 'p')
1933 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1934 StringRef SizeStr = Token.range().drop_front();
1935 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1936 return error("expected integers after 's'/'p' type character");
1937
1938 if (Token.range().front() == 's') {
1939 auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1940 if (!verifyScalarSize(ScalarSize))
1941 return error("invalid size for scalar type");
1942 Ty = LLT::scalar(ScalarSize);
1943 } else if (Token.range().front() == 'p') {
1944 const DataLayout &DL = MF.getDataLayout();
1945 uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue();
1946 if (!verifyAddrSpace(AS))
1947 return error("invalid address space number");
1948
1949 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1950 } else
1951 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1952 lex();
1953
1954 if (Token.isNot(MIToken::greater))
1955 return error(Loc, "expected <M x sN> or <M x pA> for vector type");
1956 lex();
1957
1958 Ty = LLT::fixed_vector(NumElements, Ty);
1959 return false;
1960}
1961
1962bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1963 assert(Token.is(MIToken::Identifier));
1964 StringRef TypeStr = Token.range();
1965 if (TypeStr.front() != 'i' && TypeStr.front() != 's' &&
1966 TypeStr.front() != 'p')
1967 return error(
1968 "a typed immediate operand should start with one of 'i', 's', or 'p'");
1969 StringRef SizeStr = Token.range().drop_front();
1970 if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit))
1971 return error("expected integers after 'i'/'s'/'p' type character");
1972
1973 auto Loc = Token.location();
1974 lex();
1975 if (Token.isNot(MIToken::IntegerLiteral)) {
1976 if (Token.isNot(MIToken::Identifier) ||
1977 !(Token.range() == "true" || Token.range() == "false"))
1978 return error("expected an integer literal");
1979 }
1980 const Constant *C = nullptr;
1981 if (parseIRConstant(Loc, C))
1982 return true;
1983 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1984 return false;
1985}
1986
1987bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1988 auto Loc = Token.location();
1989 lex();
1990 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1991 Token.isNot(MIToken::HexLiteral))
1992 return error("expected a floating point literal");
1993 const Constant *C = nullptr;
1994 if (parseIRConstant(Loc, C))
1995 return true;
1996 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1997 return false;
1998}
1999
2000static bool getHexUint(const MIToken &Token, APInt &Result) {
2002 StringRef S = Token.range();
2003 assert(S[0] == '0' && tolower(S[1]) == 'x');
2004 // This could be a floating point literal with a special prefix.
2005 if (!isxdigit(S[2]))
2006 return true;
2007 StringRef V = S.substr(2);
2008 APInt A(V.size()*4, V, 16);
2009
2010 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2011 // sure it isn't the case before constructing result.
2012 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2013 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2014 return false;
2015}
2016
2017static bool getUnsigned(const MIToken &Token, unsigned &Result,
2018 ErrorCallbackType ErrCB) {
2019 if (Token.hasIntegerValue()) {
2020 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
2021 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
2022 if (Val64 == Limit)
2023 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2024 Result = Val64;
2025 return false;
2026 }
2027 if (Token.is(MIToken::HexLiteral)) {
2028 APInt A;
2029 if (getHexUint(Token, A))
2030 return true;
2031 if (A.getBitWidth() > 32)
2032 return ErrCB(Token.location(), "expected 32-bit integer (too large)");
2033 Result = A.getZExtValue();
2034 return false;
2035 }
2036 return true;
2037}
2038
2039bool MIParser::getUnsigned(unsigned &Result) {
2040 return ::getUnsigned(
2041 Token, Result, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2042 return error(Loc, Msg);
2043 });
2044}
2045
2046bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
2049 unsigned Number;
2050 if (getUnsigned(Number))
2051 return true;
2052 auto MBBInfo = PFS.MBBSlots.find(Number);
2053 if (MBBInfo == PFS.MBBSlots.end())
2054 return error(Twine("use of undefined machine basic block #") +
2055 Twine(Number));
2056 MBB = MBBInfo->second;
2057 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
2058 // we drop the <irname> from the bb.<id>.<irname> format.
2059 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
2060 return error(Twine("the name of machine basic block #") + Twine(Number) +
2061 " isn't '" + Token.stringValue() + "'");
2062 return false;
2063}
2064
2065bool MIParser::parseMBBOperand(MachineOperand &Dest) {
2068 return true;
2070 lex();
2071 return false;
2072}
2073
2074bool MIParser::parseStackFrameIndex(int &FI) {
2075 assert(Token.is(MIToken::StackObject));
2076 unsigned ID;
2077 if (getUnsigned(ID))
2078 return true;
2079 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
2080 if (ObjectInfo == PFS.StackObjectSlots.end())
2081 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
2082 "'");
2084 if (const auto *Alloca =
2085 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
2086 Name = Alloca->getName();
2087 if (!Token.stringValue().empty() && Token.stringValue() != Name)
2088 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
2089 "' isn't '" + Token.stringValue() + "'");
2090 lex();
2091 FI = ObjectInfo->second;
2092 return false;
2093}
2094
2095bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
2096 int FI;
2097 if (parseStackFrameIndex(FI))
2098 return true;
2099 Dest = MachineOperand::CreateFI(FI);
2100 return false;
2101}
2102
2103bool MIParser::parseFixedStackFrameIndex(int &FI) {
2105 unsigned ID;
2106 if (getUnsigned(ID))
2107 return true;
2108 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
2109 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
2110 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
2111 Twine(ID) + "'");
2112 lex();
2113 FI = ObjectInfo->second;
2114 return false;
2115}
2116
2117bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
2118 int FI;
2119 if (parseFixedStackFrameIndex(FI))
2120 return true;
2121 Dest = MachineOperand::CreateFI(FI);
2122 return false;
2123}
2124
2125static bool parseGlobalValue(const MIToken &Token,
2127 ErrorCallbackType ErrCB) {
2128 switch (Token.kind()) {
2130 const Module *M = PFS.MF.getFunction().getParent();
2131 GV = M->getNamedValue(Token.stringValue());
2132 if (!GV)
2133 return ErrCB(Token.location(), Twine("use of undefined global value '") +
2134 Token.range() + "'");
2135 break;
2136 }
2137 case MIToken::GlobalValue: {
2138 unsigned GVIdx;
2139 if (getUnsigned(Token, GVIdx, ErrCB))
2140 return true;
2141 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
2142 return ErrCB(Token.location(), Twine("use of undefined global value '@") +
2143 Twine(GVIdx) + "'");
2144 GV = PFS.IRSlots.GlobalValues[GVIdx];
2145 break;
2146 }
2147 default:
2148 llvm_unreachable("The current token should be a global value");
2149 }
2150 return false;
2151}
2152
2153bool MIParser::parseGlobalValue(GlobalValue *&GV) {
2154 return ::parseGlobalValue(
2155 Token, PFS, GV,
2156 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
2157 return error(Loc, Msg);
2158 });
2159}
2160
2161bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
2162 GlobalValue *GV = nullptr;
2163 if (parseGlobalValue(GV))
2164 return true;
2165 lex();
2166 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
2167 if (parseOperandsOffset(Dest))
2168 return true;
2169 return false;
2170}
2171
2172bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
2174 unsigned ID;
2175 if (getUnsigned(ID))
2176 return true;
2177 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
2178 if (ConstantInfo == PFS.ConstantPoolSlots.end())
2179 return error("use of undefined constant '%const." + Twine(ID) + "'");
2180 lex();
2181 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
2182 if (parseOperandsOffset(Dest))
2183 return true;
2184 return false;
2185}
2186
2187bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
2189 unsigned ID;
2190 if (getUnsigned(ID))
2191 return true;
2192 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
2193 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
2194 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
2195 lex();
2196 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
2197 return false;
2198}
2199
2200bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
2202 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
2203 lex();
2204 Dest = MachineOperand::CreateES(Symbol);
2205 if (parseOperandsOffset(Dest))
2206 return true;
2207 return false;
2208}
2209
2210bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) {
2211 assert(Token.is(MIToken::MCSymbol));
2212 MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue());
2213 lex();
2214 Dest = MachineOperand::CreateMCSymbol(Symbol);
2215 if (parseOperandsOffset(Dest))
2216 return true;
2217 return false;
2218}
2219
2220bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
2222 StringRef Name = Token.stringValue();
2223 unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue());
2224 if (SubRegIndex == 0)
2225 return error(Twine("unknown subregister index '") + Name + "'");
2226 lex();
2227 Dest = MachineOperand::CreateImm(SubRegIndex);
2228 return false;
2229}
2230
2231bool MIParser::parseMDNode(MDNode *&Node) {
2232 assert(Token.is(MIToken::exclaim));
2233
2234 auto Loc = Token.location();
2235 lex();
2236 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2237 return error("expected metadata id after '!'");
2238 unsigned ID;
2239 if (getUnsigned(ID))
2240 return true;
2241 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
2242 if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) {
2243 NodeInfo = PFS.MachineMetadataNodes.find(ID);
2244 if (NodeInfo == PFS.MachineMetadataNodes.end())
2245 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
2246 }
2247 lex();
2248 Node = NodeInfo->second.get();
2249 return false;
2250}
2251
2252bool MIParser::parseDIExpression(MDNode *&Expr) {
2253 assert(Token.is(MIToken::md_diexpr));
2254 lex();
2255
2256 // FIXME: Share this parsing with the IL parser.
2258
2259 if (expectAndConsume(MIToken::lparen))
2260 return true;
2261
2262 if (Token.isNot(MIToken::rparen)) {
2263 do {
2264 if (Token.is(MIToken::Identifier)) {
2265 if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
2266 lex();
2267 Elements.push_back(Op);
2268 continue;
2269 }
2270 if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) {
2271 lex();
2272 Elements.push_back(Enc);
2273 continue;
2274 }
2275 return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
2276 }
2277
2278 if (Token.isNot(MIToken::IntegerLiteral) ||
2279 Token.integerValue().isSigned())
2280 return error("expected unsigned integer");
2281
2282 auto &U = Token.integerValue();
2283 if (U.ugt(UINT64_MAX))
2284 return error("element too large, limit is " + Twine(UINT64_MAX));
2285 Elements.push_back(U.getZExtValue());
2286 lex();
2287
2288 } while (consumeIfPresent(MIToken::comma));
2289 }
2290
2291 if (expectAndConsume(MIToken::rparen))
2292 return true;
2293
2294 Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
2295 return false;
2296}
2297
2298bool MIParser::parseDILocation(MDNode *&Loc) {
2299 assert(Token.is(MIToken::md_dilocation));
2300 lex();
2301
2302 bool HaveLine = false;
2303 unsigned Line = 0;
2304 unsigned Column = 0;
2305 MDNode *Scope = nullptr;
2306 MDNode *InlinedAt = nullptr;
2307 bool ImplicitCode = false;
2308
2309 if (expectAndConsume(MIToken::lparen))
2310 return true;
2311
2312 if (Token.isNot(MIToken::rparen)) {
2313 do {
2314 if (Token.is(MIToken::Identifier)) {
2315 if (Token.stringValue() == "line") {
2316 lex();
2317 if (expectAndConsume(MIToken::colon))
2318 return true;
2319 if (Token.isNot(MIToken::IntegerLiteral) ||
2320 Token.integerValue().isSigned())
2321 return error("expected unsigned integer");
2322 Line = Token.integerValue().getZExtValue();
2323 HaveLine = true;
2324 lex();
2325 continue;
2326 }
2327 if (Token.stringValue() == "column") {
2328 lex();
2329 if (expectAndConsume(MIToken::colon))
2330 return true;
2331 if (Token.isNot(MIToken::IntegerLiteral) ||
2332 Token.integerValue().isSigned())
2333 return error("expected unsigned integer");
2334 Column = Token.integerValue().getZExtValue();
2335 lex();
2336 continue;
2337 }
2338 if (Token.stringValue() == "scope") {
2339 lex();
2340 if (expectAndConsume(MIToken::colon))
2341 return true;
2342 if (parseMDNode(Scope))
2343 return error("expected metadata node");
2344 if (!isa<DIScope>(Scope))
2345 return error("expected DIScope node");
2346 continue;
2347 }
2348 if (Token.stringValue() == "inlinedAt") {
2349 lex();
2350 if (expectAndConsume(MIToken::colon))
2351 return true;
2352 if (Token.is(MIToken::exclaim)) {
2353 if (parseMDNode(InlinedAt))
2354 return true;
2355 } else if (Token.is(MIToken::md_dilocation)) {
2356 if (parseDILocation(InlinedAt))
2357 return true;
2358 } else
2359 return error("expected metadata node");
2360 if (!isa<DILocation>(InlinedAt))
2361 return error("expected DILocation node");
2362 continue;
2363 }
2364 if (Token.stringValue() == "isImplicitCode") {
2365 lex();
2366 if (expectAndConsume(MIToken::colon))
2367 return true;
2368 if (!Token.is(MIToken::Identifier))
2369 return error("expected true/false");
2370 // As far as I can see, we don't have any existing need for parsing
2371 // true/false in MIR yet. Do it ad-hoc until there's something else
2372 // that needs it.
2373 if (Token.stringValue() == "true")
2374 ImplicitCode = true;
2375 else if (Token.stringValue() == "false")
2376 ImplicitCode = false;
2377 else
2378 return error("expected true/false");
2379 lex();
2380 continue;
2381 }
2382 }
2383 return error(Twine("invalid DILocation argument '") +
2384 Token.stringValue() + "'");
2385 } while (consumeIfPresent(MIToken::comma));
2386 }
2387
2388 if (expectAndConsume(MIToken::rparen))
2389 return true;
2390
2391 if (!HaveLine)
2392 return error("DILocation requires line number");
2393 if (!Scope)
2394 return error("DILocation requires a scope");
2395
2396 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
2397 InlinedAt, ImplicitCode);
2398 return false;
2399}
2400
2401bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
2402 MDNode *Node = nullptr;
2403 if (Token.is(MIToken::exclaim)) {
2404 if (parseMDNode(Node))
2405 return true;
2406 } else if (Token.is(MIToken::md_diexpr)) {
2407 if (parseDIExpression(Node))
2408 return true;
2409 }
2411 return false;
2412}
2413
2414bool MIParser::parseCFIOffset(int &Offset) {
2415 if (Token.isNot(MIToken::IntegerLiteral))
2416 return error("expected a cfi offset");
2417 if (Token.integerValue().getSignificantBits() > 32)
2418 return error("expected a 32 bit integer (the cfi offset is too large)");
2419 Offset = (int)Token.integerValue().getExtValue();
2420 lex();
2421 return false;
2422}
2423
2424bool MIParser::parseCFIRegister(Register &Reg) {
2425 if (Token.isNot(MIToken::NamedRegister))
2426 return error("expected a cfi register");
2427 Register LLVMReg;
2428 if (parseNamedRegister(LLVMReg))
2429 return true;
2430 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2431 assert(TRI && "Expected target register info");
2432 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2433 if (DwarfReg < 0)
2434 return error("invalid DWARF register");
2435 Reg = (unsigned)DwarfReg;
2436 lex();
2437 return false;
2438}
2439
2440bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) {
2441 if (Token.isNot(MIToken::IntegerLiteral))
2442 return error("expected a cfi address space literal");
2443 if (Token.integerValue().isSigned())
2444 return error("expected an unsigned integer (cfi address space)");
2445 AddressSpace = Token.integerValue().getZExtValue();
2446 lex();
2447 return false;
2448}
2449
2450bool MIParser::parseCFIEscapeValues(std::string &Values) {
2451 do {
2452 if (Token.isNot(MIToken::HexLiteral))
2453 return error("expected a hexadecimal literal");
2454 unsigned Value;
2455 if (getUnsigned(Value))
2456 return true;
2457 if (Value > UINT8_MAX)
2458 return error("expected a 8-bit integer (too large)");
2459 Values.push_back(static_cast<uint8_t>(Value));
2460 lex();
2461 } while (consumeIfPresent(MIToken::comma));
2462 return false;
2463}
2464
2465bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2466 auto Kind = Token.kind();
2467 lex();
2468 int Offset;
2469 Register Reg;
2470 unsigned AddressSpace;
2471 unsigned CFIIndex;
2472 switch (Kind) {
2474 if (parseCFIRegister(Reg))
2475 return true;
2476 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2477 break;
2479 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2480 parseCFIOffset(Offset))
2481 return true;
2482 CFIIndex =
2483 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2484 break;
2486 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2487 parseCFIOffset(Offset))
2488 return true;
2489 CFIIndex = MF.addFrameInst(
2491 break;
2493 if (parseCFIRegister(Reg))
2494 return true;
2495 CFIIndex =
2496 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2497 break;
2499 if (parseCFIOffset(Offset))
2500 return true;
2501 CFIIndex =
2502 MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Offset));
2503 break;
2505 if (parseCFIOffset(Offset))
2506 return true;
2507 CFIIndex = MF.addFrameInst(
2509 break;
2511 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2512 parseCFIOffset(Offset))
2513 return true;
2514 CFIIndex =
2515 MF.addFrameInst(MCCFIInstruction::cfiDefCfa(nullptr, Reg, Offset));
2516 break;
2518 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2519 parseCFIOffset(Offset) || expectAndConsume(MIToken::comma) ||
2520 parseCFIAddressSpace(AddressSpace))
2521 return true;
2522 CFIIndex = MF.addFrameInst(MCCFIInstruction::createLLVMDefAspaceCfa(
2523 nullptr, Reg, Offset, AddressSpace));
2524 break;
2526 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2527 break;
2529 if (parseCFIRegister(Reg))
2530 return true;
2531 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2532 break;
2534 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2535 break;
2537 if (parseCFIRegister(Reg))
2538 return true;
2539 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2540 break;
2542 Register Reg2;
2543 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2544 parseCFIRegister(Reg2))
2545 return true;
2546
2547 CFIIndex =
2548 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2549 break;
2550 }
2552 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2553 break;
2555 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2556 break;
2558 std::string Values;
2559 if (parseCFIEscapeValues(Values))
2560 return true;
2561 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2562 break;
2563 }
2564 default:
2565 // TODO: Parse the other CFI operands.
2566 llvm_unreachable("The current token should be a cfi operand");
2567 }
2568 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2569 return false;
2570}
2571
2572bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2573 switch (Token.kind()) {
2574 case MIToken::NamedIRBlock: {
2575 BB = dyn_cast_or_null<BasicBlock>(
2576 F.getValueSymbolTable()->lookup(Token.stringValue()));
2577 if (!BB)
2578 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2579 break;
2580 }
2581 case MIToken::IRBlock: {
2582 unsigned SlotNumber = 0;
2583 if (getUnsigned(SlotNumber))
2584 return true;
2585 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2586 if (!BB)
2587 return error(Twine("use of undefined IR block '%ir-block.") +
2588 Twine(SlotNumber) + "'");
2589 break;
2590 }
2591 default:
2592 llvm_unreachable("The current token should be an IR block reference");
2593 }
2594 return false;
2595}
2596
2597bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2599 lex();
2600 if (expectAndConsume(MIToken::lparen))
2601 return true;
2602 if (Token.isNot(MIToken::GlobalValue) &&
2603 Token.isNot(MIToken::NamedGlobalValue))
2604 return error("expected a global value");
2605 GlobalValue *GV = nullptr;
2606 if (parseGlobalValue(GV))
2607 return true;
2608 auto *F = dyn_cast<Function>(GV);
2609 if (!F)
2610 return error("expected an IR function reference");
2611 lex();
2612 if (expectAndConsume(MIToken::comma))
2613 return true;
2614 BasicBlock *BB = nullptr;
2615 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2616 return error("expected an IR block reference");
2617 if (parseIRBlock(BB, *F))
2618 return true;
2619 lex();
2620 if (expectAndConsume(MIToken::rparen))
2621 return true;
2622 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2623 if (parseOperandsOffset(Dest))
2624 return true;
2625 return false;
2626}
2627
2628bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2629 assert(Token.is(MIToken::kw_intrinsic));
2630 lex();
2631 if (expectAndConsume(MIToken::lparen))
2632 return error("expected syntax intrinsic(@llvm.whatever)");
2633
2634 if (Token.isNot(MIToken::NamedGlobalValue))
2635 return error("expected syntax intrinsic(@llvm.whatever)");
2636
2637 std::string Name = std::string(Token.stringValue());
2638 lex();
2639
2640 if (expectAndConsume(MIToken::rparen))
2641 return error("expected ')' to terminate intrinsic name");
2642
2643 // Find out what intrinsic we're dealing with, first try the global namespace
2644 // and then the target's private intrinsics if that fails.
2645 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
2648 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
2649
2651 return error("unknown intrinsic name");
2653
2654 return false;
2655}
2656
2657bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2658 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
2659 bool IsFloat = Token.is(MIToken::kw_floatpred);
2660 lex();
2661
2662 if (expectAndConsume(MIToken::lparen))
2663 return error("expected syntax intpred(whatever) or floatpred(whatever");
2664
2665 if (Token.isNot(MIToken::Identifier))
2666 return error("whatever");
2667
2668 CmpInst::Predicate Pred;
2669 if (IsFloat) {
2670 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2671 .Case("false", CmpInst::FCMP_FALSE)
2672 .Case("oeq", CmpInst::FCMP_OEQ)
2673 .Case("ogt", CmpInst::FCMP_OGT)
2674 .Case("oge", CmpInst::FCMP_OGE)
2675 .Case("olt", CmpInst::FCMP_OLT)
2676 .Case("ole", CmpInst::FCMP_OLE)
2677 .Case("one", CmpInst::FCMP_ONE)
2678 .Case("ord", CmpInst::FCMP_ORD)
2679 .Case("uno", CmpInst::FCMP_UNO)
2680 .Case("ueq", CmpInst::FCMP_UEQ)
2681 .Case("ugt", CmpInst::FCMP_UGT)
2682 .Case("uge", CmpInst::FCMP_UGE)
2683 .Case("ult", CmpInst::FCMP_ULT)
2684 .Case("ule", CmpInst::FCMP_ULE)
2685 .Case("une", CmpInst::FCMP_UNE)
2686 .Case("true", CmpInst::FCMP_TRUE)
2688 if (!CmpInst::isFPPredicate(Pred))
2689 return error("invalid floating-point predicate");
2690 } else {
2691 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2692 .Case("eq", CmpInst::ICMP_EQ)
2693 .Case("ne", CmpInst::ICMP_NE)
2694 .Case("sgt", CmpInst::ICMP_SGT)
2695 .Case("sge", CmpInst::ICMP_SGE)
2696 .Case("slt", CmpInst::ICMP_SLT)
2697 .Case("sle", CmpInst::ICMP_SLE)
2698 .Case("ugt", CmpInst::ICMP_UGT)
2699 .Case("uge", CmpInst::ICMP_UGE)
2700 .Case("ult", CmpInst::ICMP_ULT)
2701 .Case("ule", CmpInst::ICMP_ULE)
2703 if (!CmpInst::isIntPredicate(Pred))
2704 return error("invalid integer predicate");
2705 }
2706
2707 lex();
2709 if (expectAndConsume(MIToken::rparen))
2710 return error("predicate should be terminated by ')'.");
2711
2712 return false;
2713}
2714
2715bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) {
2717
2718 lex();
2719 if (expectAndConsume(MIToken::lparen))
2720 return error("expected syntax shufflemask(<integer or undef>, ...)");
2721
2722 SmallVector<int, 32> ShufMask;
2723 do {
2724 if (Token.is(MIToken::kw_undef)) {
2725 ShufMask.push_back(-1);
2726 } else if (Token.is(MIToken::IntegerLiteral)) {
2727 const APSInt &Int = Token.integerValue();
2728 ShufMask.push_back(Int.getExtValue());
2729 } else
2730 return error("expected integer constant");
2731
2732 lex();
2733 } while (consumeIfPresent(MIToken::comma));
2734
2735 if (expectAndConsume(MIToken::rparen))
2736 return error("shufflemask should be terminated by ')'.");
2737
2738 ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(ShufMask);
2739 Dest = MachineOperand::CreateShuffleMask(MaskAlloc);
2740 return false;
2741}
2742
2743bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) {
2745
2746 lex();
2747 if (expectAndConsume(MIToken::lparen))
2748 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2749
2750 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2751 return error("expected unsigned integer for instruction index");
2752 uint64_t InstrIdx = Token.integerValue().getZExtValue();
2753 assert(InstrIdx <= std::numeric_limits<unsigned>::max() &&
2754 "Instruction reference's instruction index is too large");
2755 lex();
2756
2757 if (expectAndConsume(MIToken::comma))
2758 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2759
2760 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isNegative())
2761 return error("expected unsigned integer for operand index");
2762 uint64_t OpIdx = Token.integerValue().getZExtValue();
2763 assert(OpIdx <= std::numeric_limits<unsigned>::max() &&
2764 "Instruction reference's operand index is too large");
2765 lex();
2766
2767 if (expectAndConsume(MIToken::rparen))
2768 return error("expected syntax dbg-instr-ref(<unsigned>, <unsigned>)");
2769
2770 Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx);
2771 return false;
2772}
2773
2774bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2776 lex();
2777 if (expectAndConsume(MIToken::lparen))
2778 return true;
2779 if (Token.isNot(MIToken::Identifier))
2780 return error("expected the name of the target index");
2781 int Index = 0;
2782 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2783 return error("use of undefined target index '" + Token.stringValue() + "'");
2784 lex();
2785 if (expectAndConsume(MIToken::rparen))
2786 return true;
2787 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2788 if (parseOperandsOffset(Dest))
2789 return true;
2790 return false;
2791}
2792
2793bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2794 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
2795 lex();
2796 if (expectAndConsume(MIToken::lparen))
2797 return true;
2798
2799 uint32_t *Mask = MF.allocateRegMask();
2800 do {
2801 if (Token.isNot(MIToken::rparen)) {
2802 if (Token.isNot(MIToken::NamedRegister))
2803 return error("expected a named register");
2804 Register Reg;
2805 if (parseNamedRegister(Reg))
2806 return true;
2807 lex();
2808 Mask[Reg / 32] |= 1U << (Reg % 32);
2809 }
2810
2811 // TODO: Report an error if the same register is used more than once.
2812 } while (consumeIfPresent(MIToken::comma));
2813
2814 if (expectAndConsume(MIToken::rparen))
2815 return true;
2816 Dest = MachineOperand::CreateRegMask(Mask);
2817 return false;
2818}
2819
2820bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2821 assert(Token.is(MIToken::kw_liveout));
2822 uint32_t *Mask = MF.allocateRegMask();
2823 lex();
2824 if (expectAndConsume(MIToken::lparen))
2825 return true;
2826 while (true) {
2827 if (Token.isNot(MIToken::NamedRegister))
2828 return error("expected a named register");
2829 Register Reg;
2830 if (parseNamedRegister(Reg))
2831 return true;
2832 lex();
2833 Mask[Reg / 32] |= 1U << (Reg % 32);
2834 // TODO: Report an error if the same register is used more than once.
2835 if (Token.isNot(MIToken::comma))
2836 break;
2837 lex();
2838 }
2839 if (expectAndConsume(MIToken::rparen))
2840 return true;
2842 return false;
2843}
2844
2845bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
2846 MachineOperand &Dest,
2847 std::optional<unsigned> &TiedDefIdx) {
2848 switch (Token.kind()) {
2851 case MIToken::kw_def:
2852 case MIToken::kw_dead:
2853 case MIToken::kw_killed:
2854 case MIToken::kw_undef:
2863 return parseRegisterOperand(Dest, TiedDefIdx);
2865 return parseImmediateOperand(Dest);
2866 case MIToken::kw_half:
2867 case MIToken::kw_float:
2868 case MIToken::kw_double:
2870 case MIToken::kw_fp128:
2872 return parseFPImmediateOperand(Dest);
2874 return parseMBBOperand(Dest);
2876 return parseStackObjectOperand(Dest);
2878 return parseFixedStackObjectOperand(Dest);
2881 return parseGlobalAddressOperand(Dest);
2883 return parseConstantPoolIndexOperand(Dest);
2885 return parseJumpTableIndexOperand(Dest);
2887 return parseExternalSymbolOperand(Dest);
2888 case MIToken::MCSymbol:
2889 return parseMCSymbolOperand(Dest);
2891 return parseSubRegisterIndexOperand(Dest);
2892 case MIToken::md_diexpr:
2893 case MIToken::exclaim:
2894 return parseMetadataOperand(Dest);
2911 return parseCFIOperand(Dest);
2913 return parseBlockAddressOperand(Dest);
2915 return parseIntrinsicOperand(Dest);
2917 return parseTargetIndexOperand(Dest);
2919 return parseLiveoutRegisterMaskOperand(Dest);
2922 return parsePredicateOperand(Dest);
2924 return parseShuffleMaskOperand(Dest);
2926 return parseDbgInstrRefOperand(Dest);
2927 case MIToken::Error:
2928 return true;
2930 if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
2931 Dest = MachineOperand::CreateRegMask(RegMask);
2932 lex();
2933 break;
2934 } else if (Token.stringValue() == "CustomRegMask") {
2935 return parseCustomRegisterMaskOperand(Dest);
2936 } else
2937 return parseTypedImmediateOperand(Dest);
2938 case MIToken::dot: {
2939 const auto *TII = MF.getSubtarget().getInstrInfo();
2940 if (const auto *Formatter = TII->getMIRFormatter()) {
2941 return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
2942 }
2943 [[fallthrough]];
2944 }
2945 default:
2946 // FIXME: Parse the MCSymbol machine operand.
2947 return error("expected a machine operand");
2948 }
2949 return false;
2950}
2951
2952bool MIParser::parseMachineOperandAndTargetFlags(
2953 const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest,
2954 std::optional<unsigned> &TiedDefIdx) {
2955 unsigned TF = 0;
2956 bool HasTargetFlags = false;
2957 if (Token.is(MIToken::kw_target_flags)) {
2958 HasTargetFlags = true;
2959 lex();
2960 if (expectAndConsume(MIToken::lparen))
2961 return true;
2962 if (Token.isNot(MIToken::Identifier))
2963 return error("expected the name of the target flag");
2964 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
2965 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
2966 return error("use of undefined target flag '" + Token.stringValue() +
2967 "'");
2968 }
2969 lex();
2970 while (Token.is(MIToken::comma)) {
2971 lex();
2972 if (Token.isNot(MIToken::Identifier))
2973 return error("expected the name of the target flag");
2974 unsigned BitFlag = 0;
2975 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2976 return error("use of undefined target flag '" + Token.stringValue() +
2977 "'");
2978 // TODO: Report an error when using a duplicate bit target flag.
2979 TF |= BitFlag;
2980 lex();
2981 }
2982 if (expectAndConsume(MIToken::rparen))
2983 return true;
2984 }
2985 auto Loc = Token.location();
2986 if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx))
2987 return true;
2988 if (!HasTargetFlags)
2989 return false;
2990 if (Dest.isReg())
2991 return error(Loc, "register operands can't have target flags");
2992 Dest.setTargetFlags(TF);
2993 return false;
2994}
2995
2996bool MIParser::parseOffset(int64_t &Offset) {
2997 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2998 return false;
2999 StringRef Sign = Token.range();
3000 bool IsNegative = Token.is(MIToken::minus);
3001 lex();
3002 if (Token.isNot(MIToken::IntegerLiteral))
3003 return error("expected an integer literal after '" + Sign + "'");
3004 if (Token.integerValue().getSignificantBits() > 64)
3005 return error("expected 64-bit integer (too large)");
3006 Offset = Token.integerValue().getExtValue();
3007 if (IsNegative)
3008 Offset = -Offset;
3009 lex();
3010 return false;
3011}
3012
3013bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) {
3015 lex();
3016 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
3017 return error("expected basic block after 'ir_block_address_taken'");
3018
3019 if (parseIRBlock(BB, MF.getFunction()))
3020 return true;
3021
3022 lex();
3023 return false;
3024}
3025
3026bool MIParser::parseAlignment(uint64_t &Alignment) {
3027 assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign));
3028 lex();
3029 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3030 return error("expected an integer literal after 'align'");
3031 if (getUint64(Alignment))
3032 return true;
3033 lex();
3034
3035 if (!isPowerOf2_64(Alignment))
3036 return error("expected a power-of-2 literal after 'align'");
3037
3038 return false;
3039}
3040
3041bool MIParser::parseAddrspace(unsigned &Addrspace) {
3042 assert(Token.is(MIToken::kw_addrspace));
3043 lex();
3044 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
3045 return error("expected an integer literal after 'addrspace'");
3046 if (getUnsigned(Addrspace))
3047 return true;
3048 lex();
3049 return false;
3050}
3051
3052bool MIParser::parseOperandsOffset(MachineOperand &Op) {
3053 int64_t Offset = 0;
3054 if (parseOffset(Offset))
3055 return true;
3056 Op.setOffset(Offset);
3057 return false;
3058}
3059
3060static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS,
3061 const Value *&V, ErrorCallbackType ErrCB) {
3062 switch (Token.kind()) {
3063 case MIToken::NamedIRValue: {
3064 V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
3065 break;
3066 }
3067 case MIToken::IRValue: {
3068 unsigned SlotNumber = 0;
3069 if (getUnsigned(Token, SlotNumber, ErrCB))
3070 return true;
3071 V = PFS.getIRValue(SlotNumber);
3072 break;
3073 }
3075 case MIToken::GlobalValue: {
3076 GlobalValue *GV = nullptr;
3077 if (parseGlobalValue(Token, PFS, GV, ErrCB))
3078 return true;
3079 V = GV;
3080 break;
3081 }
3083 const Constant *C = nullptr;
3084 if (parseIRConstant(Token.location(), Token.stringValue(), PFS, C, ErrCB))
3085 return true;
3086 V = C;
3087 break;
3088 }
3090 V = nullptr;
3091 return false;
3092 default:
3093 llvm_unreachable("The current token should be an IR block reference");
3094 }
3095 if (!V)
3096 return ErrCB(Token.location(), Twine("use of undefined IR value '") + Token.range() + "'");
3097 return false;
3098}
3099
3100bool MIParser::parseIRValue(const Value *&V) {
3101 return ::parseIRValue(
3102 Token, PFS, V, [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3103 return error(Loc, Msg);
3104 });
3105}
3106
3107bool MIParser::getUint64(uint64_t &Result) {
3108 if (Token.hasIntegerValue()) {
3109 if (Token.integerValue().getActiveBits() > 64)
3110 return error("expected 64-bit integer (too large)");
3111 Result = Token.integerValue().getZExtValue();
3112 return false;
3113 }
3114 if (Token.is(MIToken::HexLiteral)) {
3115 APInt A;
3116 if (getHexUint(A))
3117 return true;
3118 if (A.getBitWidth() > 64)
3119 return error("expected 64-bit integer (too large)");
3120 Result = A.getZExtValue();
3121 return false;
3122 }
3123 return true;
3124}
3125
3126bool MIParser::getHexUint(APInt &Result) {
3127 return ::getHexUint(Token, Result);
3128}
3129
3130bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
3131 const auto OldFlags = Flags;
3132 switch (Token.kind()) {
3135 break;
3138 break;
3141 break;
3144 break;
3147 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
3148 return error("use of undefined target MMO flag '" + Token.stringValue() +
3149 "'");
3150 Flags |= TF;
3151 break;
3152 }
3153 default:
3154 llvm_unreachable("The current token should be a memory operand flag");
3155 }
3156 if (OldFlags == Flags)
3157 // We know that the same flag is specified more than once when the flags
3158 // weren't modified.
3159 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
3160 lex();
3161 return false;
3162}
3163
3164bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
3165 switch (Token.kind()) {
3166 case MIToken::kw_stack:
3167 PSV = MF.getPSVManager().getStack();
3168 break;
3169 case MIToken::kw_got:
3170 PSV = MF.getPSVManager().getGOT();
3171 break;
3173 PSV = MF.getPSVManager().getJumpTable();
3174 break;
3176 PSV = MF.getPSVManager().getConstantPool();
3177 break;
3179 int FI;
3180 if (parseFixedStackFrameIndex(FI))
3181 return true;
3182 PSV = MF.getPSVManager().getFixedStack(FI);
3183 // The token was already consumed, so use return here instead of break.
3184 return false;
3185 }
3186 case MIToken::StackObject: {
3187 int FI;
3188 if (parseStackFrameIndex(FI))
3189 return true;
3190 PSV = MF.getPSVManager().getFixedStack(FI);
3191 // The token was already consumed, so use return here instead of break.
3192 return false;
3193 }
3195 lex();
3196 switch (Token.kind()) {
3199 GlobalValue *GV = nullptr;
3200 if (parseGlobalValue(GV))
3201 return true;
3202 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
3203 break;
3204 }
3206 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
3207 MF.createExternalSymbolName(Token.stringValue()));
3208 break;
3209 default:
3210 return error(
3211 "expected a global value or an external symbol after 'call-entry'");
3212 }
3213 break;
3214 case MIToken::kw_custom: {
3215 lex();
3216 const auto *TII = MF.getSubtarget().getInstrInfo();
3217 if (const auto *Formatter = TII->getMIRFormatter()) {
3218 if (Formatter->parseCustomPseudoSourceValue(
3219 Token.stringValue(), MF, PFS, PSV,
3220 [this](StringRef::iterator Loc, const Twine &Msg) -> bool {
3221 return error(Loc, Msg);
3222 }))
3223 return true;
3224 } else
3225 return error("unable to parse target custom pseudo source value");
3226 break;
3227 }
3228 default:
3229 llvm_unreachable("The current token should be pseudo source value");
3230 }
3231 lex();
3232 return false;
3233}
3234
3235bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
3236 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
3237 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
3238 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
3239 Token.is(MIToken::kw_call_entry) || Token.is(MIToken::kw_custom)) {
3240 const PseudoSourceValue *PSV = nullptr;
3241 if (parseMemoryPseudoSourceValue(PSV))
3242 return true;
3243 int64_t Offset = 0;
3244 if (parseOffset(Offset))
3245 return true;
3246 Dest = MachinePointerInfo(PSV, Offset);
3247 return false;
3248 }
3249 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
3250 Token.isNot(MIToken::GlobalValue) &&
3251 Token.isNot(MIToken::NamedGlobalValue) &&
3252 Token.isNot(MIToken::QuotedIRValue) &&
3253 Token.isNot(MIToken::kw_unknown_address))
3254 return error("expected an IR value reference");
3255 const Value *V = nullptr;
3256 if (parseIRValue(V))
3257 return true;
3258 if (V && !V->getType()->isPointerTy())
3259 return error("expected a pointer IR value");
3260 lex();
3261 int64_t Offset = 0;
3262 if (parseOffset(Offset))
3263 return true;
3264 Dest = MachinePointerInfo(V, Offset);
3265 return false;
3266}
3267
3268bool MIParser::parseOptionalScope(LLVMContext &Context,
3269 SyncScope::ID &SSID) {
3270 SSID = SyncScope::System;
3271 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
3272 lex();
3273 if (expectAndConsume(MIToken::lparen))
3274 return error("expected '(' in syncscope");
3275
3276 std::string SSN;
3277 if (parseStringConstant(SSN))
3278 return true;
3279
3280 SSID = Context.getOrInsertSyncScopeID(SSN);
3281 if (expectAndConsume(MIToken::rparen))
3282 return error("expected ')' in syncscope");
3283 }
3284
3285 return false;
3286}
3287
3288bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
3290 if (Token.isNot(MIToken::Identifier))
3291 return false;
3292
3293 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
3294 .Case("unordered", AtomicOrdering::Unordered)
3295 .Case("monotonic", AtomicOrdering::Monotonic)
3296 .Case("acquire", AtomicOrdering::Acquire)
3297 .Case("release", AtomicOrdering::Release)
3301
3302 if (Order != AtomicOrdering::NotAtomic) {
3303 lex();
3304 return false;
3305 }
3306
3307 return error("expected an atomic scope, ordering or a size specification");
3308}
3309
3310bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
3311 if (expectAndConsume(MIToken::lparen))
3312 return true;
3314 while (Token.isMemoryOperandFlag()) {
3315 if (parseMemoryOperandFlag(Flags))
3316 return true;
3317 }
3318 if (Token.isNot(MIToken::Identifier) ||
3319 (Token.stringValue() != "load" && Token.stringValue() != "store"))
3320 return error("expected 'load' or 'store' memory operation");
3321 if (Token.stringValue() == "load")
3323 else
3325 lex();
3326
3327 // Optional 'store' for operands that both load and store.
3328 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
3330 lex();
3331 }
3332
3333 // Optional synchronization scope.
3334 SyncScope::ID SSID;
3335 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
3336 return true;
3337
3338 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
3339 AtomicOrdering Order, FailureOrder;
3340 if (parseOptionalAtomicOrdering(Order))
3341 return true;
3342
3343 if (parseOptionalAtomicOrdering(FailureOrder))
3344 return true;
3345
3347 if (Token.isNot(MIToken::IntegerLiteral) &&
3348 Token.isNot(MIToken::kw_unknown_size) &&
3349 Token.isNot(MIToken::lparen))
3350 return error("expected memory LLT, the size integer literal or 'unknown-size' after "
3351 "memory operation");
3352
3354 if (Token.is(MIToken::IntegerLiteral)) {
3355 if (getUint64(Size))
3356 return true;
3357
3358 // Convert from bytes to bits for storage.
3360 lex();
3361 } else if (Token.is(MIToken::kw_unknown_size)) {
3363 lex();
3364 } else {
3365 if (expectAndConsume(MIToken::lparen))
3366 return true;
3367 if (parseLowLevelType(Token.location(), MemoryType))
3368 return true;
3369 if (expectAndConsume(MIToken::rparen))
3370 return true;
3371
3372 Size = MemoryType.getSizeInBytes();
3373 }
3374
3376 if (Token.is(MIToken::Identifier)) {
3377 const char *Word =
3380 ? "on"
3381 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
3382 if (Token.stringValue() != Word)
3383 return error(Twine("expected '") + Word + "'");
3384 lex();
3385
3386 if (parseMachinePointerInfo(Ptr))
3387 return true;
3388 }
3389 uint64_t BaseAlignment =
3391 AAMDNodes AAInfo;
3392 MDNode *Range = nullptr;
3393 while (consumeIfPresent(MIToken::comma)) {
3394 switch (Token.kind()) {
3395 case MIToken::kw_align: {
3396 // align is printed if it is different than size.
3397 uint64_t Alignment;
3398 if (parseAlignment(Alignment))
3399 return true;
3400 if (Ptr.Offset & (Alignment - 1)) {
3401 // MachineMemOperand::getAlign never returns a value greater than the
3402 // alignment of offset, so this just guards against hand-written MIR
3403 // that specifies a large "align" value when it should probably use
3404 // "basealign" instead.
3405 return error("specified alignment is more aligned than offset");
3406 }
3407 BaseAlignment = Alignment;
3408 break;
3409 }
3411 // basealign is printed if it is different than align.
3412 if (parseAlignment(BaseAlignment))
3413 return true;
3414 break;
3416 if (parseAddrspace(Ptr.AddrSpace))
3417 return true;
3418 break;
3419 case MIToken::md_tbaa:
3420 lex();
3421 if (parseMDNode(AAInfo.TBAA))
3422 return true;
3423 break;
3425 lex();
3426 if (parseMDNode(AAInfo.Scope))
3427 return true;
3428 break;
3430 lex();
3431 if (parseMDNode(AAInfo.NoAlias))
3432 return true;
3433 break;
3434 case MIToken::md_range:
3435 lex();
3436 if (parseMDNode(Range))
3437 return true;
3438 break;
3439 // TODO: Report an error on duplicate metadata nodes.
3440 default:
3441 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
3442 "'!noalias' or '!range'");
3443 }
3444 }
3445 if (expectAndConsume(MIToken::rparen))
3446 return true;
3447 Dest = MF.getMachineMemOperand(Ptr, Flags, MemoryType, Align(BaseAlignment),
3448 AAInfo, Range, SSID, Order, FailureOrder);
3449 return false;
3450}
3451
3452bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
3454 Token.is(MIToken::kw_post_instr_symbol)) &&
3455 "Invalid token for a pre- post-instruction symbol!");
3456 lex();
3457 if (Token.isNot(MIToken::MCSymbol))
3458 return error("expected a symbol after 'pre-instr-symbol'");
3459 Symbol = getOrCreateMCSymbol(Token.stringValue());
3460 lex();
3461 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3462 Token.is(MIToken::lbrace))
3463 return false;
3464 if (Token.isNot(MIToken::comma))
3465 return error("expected ',' before the next machine operand");
3466 lex();
3467 return false;
3468}
3469
3470bool MIParser::parseHeapAllocMarker(MDNode *&Node) {
3472 "Invalid token for a heap alloc marker!");
3473 lex();
3475 if (!Node)
3476 return error("expected a MDNode after 'heap-alloc-marker'");
3477 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3478 Token.is(MIToken::lbrace))
3479 return false;
3480 if (Token.isNot(MIToken::comma))
3481 return error("expected ',' before the next machine operand");
3482 lex();
3483 return false;
3484}
3485
3486bool MIParser::parsePCSections(MDNode *&Node) {
3487 assert(Token.is(MIToken::kw_pcsections) &&
3488 "Invalid token for a PC sections!");
3489 lex();
3491 if (!Node)
3492 return error("expected a MDNode after 'pcsections'");
3493 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
3494 Token.is(MIToken::lbrace))
3495 return false;
3496 if (Token.isNot(MIToken::comma))
3497 return error("expected ',' before the next machine operand");
3498 lex();
3499 return false;
3500}
3501
3503 const Function &F,
3504 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3505 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
3507 for (const auto &BB : F) {
3508 if (BB.hasName())
3509 continue;
3510 int Slot = MST.getLocalSlot(&BB);
3511 if (Slot == -1)
3512 continue;
3513 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
3514 }
3515}
3516
3518 unsigned Slot,
3519 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
3520 return Slots2BasicBlocks.lookup(Slot);
3521}
3522
3523const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
3524 if (Slots2BasicBlocks.empty())
3525 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
3526 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
3527}
3528
3529const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
3530 if (&F == &MF.getFunction())
3531 return getIRBlock(Slot);
3532 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
3533 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
3534 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
3535}
3536
3537MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
3538 // FIXME: Currently we can't recognize temporary or local symbols and call all
3539 // of the appropriate forms to create them. However, this handles basic cases
3540 // well as most of the special aspects are recognized by a prefix on their
3541 // name, and the input names should already be unique. For test cases, keeping
3542 // the symbol name out of the symbol table isn't terribly important.
3543 return MF.getContext().getOrCreateSymbol(Name);
3544}
3545
3546bool MIParser::parseStringConstant(std::string &Result) {
3547 if (Token.isNot(MIToken::StringConstant))
3548 return error("expected string constant");
3549 Result = std::string(Token.stringValue());
3550 lex();
3551 return false;
3552}
3553
3555 StringRef Src,
3557 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
3558}
3559
3562 return MIParser(PFS, Error, Src).parseBasicBlocks();
3563}
3564
3568 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3569}
3570
3572 Register &Reg, StringRef Src,
3574 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3575}
3576
3578 Register &Reg, StringRef Src,
3580 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3581}
3582
3584 VRegInfo *&Info, StringRef Src,
3586 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3587}
3588
3590 int &FI, StringRef Src,
3592 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3593}
3594
3596 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
3597 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3598}
3599
3601 SMRange SrcRange, SMDiagnostic &Error) {
3602 return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata();
3603}
3604
3606 PerFunctionMIParsingState &PFS, const Value *&V,
3607 ErrorCallbackType ErrorCallback) {
3608 MIToken Token;
3609 Src = lexMIToken(Src, Token, [&](StringRef::iterator Loc, const Twine &Msg) {
3610 ErrorCallback(Loc, Msg);
3611 });
3612 V = nullptr;
3613
3614 return ::parseIRValue(Token, PFS, V, ErrorCallback);
3615}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the StringMap class.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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...
Atomic ordering constants.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
std::string Name
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define RegName(no)
A common definition of LaneBitmask for use in TableGen and CodeGen.
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static const char * printImplicitRegisterFlag(const MachineOperand &MO)
Definition: MIParser.cpp:1392
static const BasicBlock * getIRBlockFromSlot(unsigned Slot, const DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
Definition: MIParser.cpp:3517
static std::string getRegisterName(const TargetRegisterInfo *TRI, Register Reg)
Definition: MIParser.cpp:1397
static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue, PerFunctionMIParsingState &PFS, const Constant *&C, ErrorCallbackType ErrCB)
Definition: MIParser.cpp:1847
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:351
static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS, const Value *&V, ErrorCallbackType ErrCB)
Definition: MIParser.cpp:3060
static bool verifyScalarSize(uint64_t Size)
Definition: MIParser.cpp:1876
static bool getUnsigned(const MIToken &Token, unsigned &Result, ErrorCallbackType ErrCB)
Definition: MIParser.cpp:2017
static bool getHexUint(const MIToken &Token, APInt &Result)
Definition: MIParser.cpp:2000
static bool verifyVectorElementCount(uint64_t NumElts)
Definition: MIParser.cpp:1880
static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, DenseMap< unsigned, const Value * > &Slots2Values)
Definition: MIParser.cpp:342
static void initSlots2BasicBlocks(const Function &F, DenseMap< unsigned, const BasicBlock * > &Slots2BasicBlocks)
Definition: MIParser.cpp:3502
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
Definition: MIParser.cpp:612
static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, ArrayRef< ParsedMachineOperand > Operands)
Return true if the parsed machine operands contain a given machine operand.
Definition: MIParser.cpp:1404
static bool parseGlobalValue(const MIToken &Token, PerFunctionMIParsingState &PFS, GlobalValue *&GV, ErrorCallbackType ErrCB)
Definition: MIParser.cpp:2125
static bool verifyAddrSpace(uint64_t AddrSpace)
Definition: MIParser.cpp:1884
mir Rename Register Operands
unsigned const TargetRegisterInfo * TRI
unsigned Reg
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
return InstrInfo
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash, uint32_t &Attributes)
Parse Input that contains metadata.
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define error(X)
@ Flags
Definition: TextStubV5.cpp:93
Class for arbitrary precision integers.
Definition: APInt.h:75
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1494
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:463
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1769
static BranchProbability getRaw(uint32_t N)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:718
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:721
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:735
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:747
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:748
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:724
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:733
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:722
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:723
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:742
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:741
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:745
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:732
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:726
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:729
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:743
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:730
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:725
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:727
@ ICMP_EQ
equal
Definition: InstrTypes.h:739
@ ICMP_NE
not equal
Definition: InstrTypes.h:740
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:746
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:734
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:744
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:731
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:720
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:728
bool isFPPredicate() const
Definition: InstrTypes.h:825
bool isIntPredicate() const
Definition: InstrTypes.h:826
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
A debug info location.
Definition: DebugLoc.h:33
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:202
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static Intrinsic::ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
Definition: Function.cpp:860
ValueSymbolTable * getValueSymbolTable()
getSymbolTable() - Return the symbol table if any, otherwise nullptr.
Definition: Function.h:748
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
Return an array that contains the direct target flag values and their names.
ArrayRef< std::pair< unsigned, const char * > > getSerializableBitmaskMachineOperandTargetFlags() const override
Return an array that contains the bitmask target flag values and their names.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:547
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register)
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
Definition: MCDwarf.h:604
static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register)
.cfi_same_value Current value of Register is the same as in the previous frame.
Definition: MCDwarf.h:616
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register)
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition: MCDwarf.h:540
static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register)
.cfi_undefined From now on the previous value of Register can't be restored anymore.
Definition: MCDwarf.h:610
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment)
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
Definition: MCDwarf.h:554
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition: MCDwarf.h:533
static MCCFIInstruction createRememberState(MCSymbol *L)
.cfi_remember_state Save all current rules for all registers.
Definition: MCDwarf.h:621
static MCCFIInstruction createLLVMDefAspaceCfa(MCSymbol *L, unsigned Register, int Offset, unsigned AddressSpace)
.cfi_llvm_def_aspace_cfa defines the rule for computing the CFA to be the result of evaluating the DW...
Definition: MCDwarf.h:562
static MCCFIInstruction createWindowSave(MCSymbol *L)
.cfi_window_save SPARC register window is saved.
Definition: MCDwarf.h:592
static MCCFIInstruction createRestoreState(MCSymbol *L)
.cfi_restore_state Restore the previously saved state.
Definition: MCDwarf.h:626
static MCCFIInstruction createNegateRAState(MCSymbol *L)
.cfi_negate_ra_state AArch64 negate RA state.
Definition: MCDwarf.h:597
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition: MCDwarf.h:632
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition: MCDwarf.h:571
static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_rel_offset Previous value of Register is saved at offset Offset from the current CFA register.
Definition: MCDwarf.h:579
static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1, unsigned Register2)
.cfi_register Previous value of Register1 is saved in register Register2.
Definition: MCDwarf.h:586
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
ArrayRef< MCPhysReg > implicit_defs() const
Return a list of registers that are potentially written by any instance of this machine instruction.
Definition: MCInstrDesc.h:577
bool isCall() const
Return true if the instruction is a call.
Definition: MCInstrDesc.h:287
ArrayRef< MCPhysReg > implicit_uses() const
Return a list of registers that are potentially read by any instance of this machine instruction.
Definition: MCInstrDesc.h:565
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Metadata node.
Definition: Metadata.h:943
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:1132
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1399
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:497
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition: Metadata.h:1367
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1356
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition: Metadata.h:1376
MIRFormater - Interface to format MIR operand based on target.
Definition: MIRFormatter.h:28
virtual bool parseImmMnemonic(const unsigned OpCode, const unsigned OpIdx, StringRef Src, int64_t &Imm, ErrorCallbackType ErrorCallback) const
Implement target specific parsing of immediate mnemonics.
Definition: MIRFormatter.h:46
static 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...
Definition: MIParser.cpp:3605
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.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void setAlignment(Align A)
Set alignment of the basic block.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void setSectionID(MBBSectionID V)
Sets the section ID for this basic block.
void setIsInlineAsmBrIndirectTarget(bool V=true)
Indicates if this is the indirect dest of an INLINEASM_BR.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
void setBBID(unsigned V)
Sets the fixed BBID of this basic block.
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:68
void setFlag(MIFlag Flag)
Set a MI flag.
Definition: MachineInstr.h:362
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)
bool isImplicit() const
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)
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,...
Register createIncompleteVirtualRegister(StringRef Name="")
Creates a new virtual register that has no register class, register bank or size assigned yet.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:76
const char * getBufferEnd() const
Definition: MemoryBuffer.h:67
const char * getBufferStart() const
Definition: MemoryBuffer.h:66
Root of the metadata hierarchy.
Definition: Metadata.h:61
Manage lifetime of a slot tracker for printing IR.
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
Definition: AsmWriter.cpp:888
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:874
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Special value supplied for machine level alias analysis.
Holds all the information related to register banks.
unsigned getNumRegBanks() const
Get the total number of register banks.
RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
This class implements the register bank concept.
Definition: RegisterBank.h:28
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
Represents a location in source code.
Definition: SMLoc.h:23
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
Represents a range in source code.
Definition: SMLoc.h:48
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
unsigned getMainFileID() const
Definition: SourceMgr.h:132
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:125
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.
Definition: SourceMgr.cpp:274
bool empty() const
Definition: StringMap.h:94
iterator end()
Definition: StringMap.h:204
iterator find(StringRef Key)
Definition: StringMap.h:217
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:286
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - 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:558
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:596
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
std::string lower() const
Definition: StringRef.cpp:111
static constexpr size_t npos
Definition: StringRef.h:52
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
TargetIntrinsicInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const RegisterBankInfo * getRegBankInfo() const
If the information for the register banks is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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:74
bool hasName() const
Definition: Value.h:261
An efficient, type-erasing, non-owning reference to a callable.
unsigned getOperationEncoding(StringRef OperationEncodingString)
Definition: Dwarf.cpp:161
unsigned getAttributeEncoding(StringRef EncodingString)
Definition: Dwarf.cpp:208
#define UINT64_MAX
Definition: DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const CustomOperand< const MCSubtargetInfo & > Msg[]
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.
Definition: BitmaskEnum.h:119
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Debug
Register 'use' is for debugging purpose.
@ Dead
Unused definition.
@ Renamable
Register that may be renamed.
@ Define
Register definition.
@ InternalRead
Register reads a value that is defined inside the same instruction or bundle.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
@ EarlyClobber
Register definition happens before uses.
@ System
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:57
Reg
All possible values of the reg field in the ModR/M byte.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
support::ulittle32_t Word
Definition: IRSymtab.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
bool parseStackObjectReference(PerFunctionMIParsingState &PFS, int &FI, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3589
bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3595
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:1819
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...
AddressSpace
Definition: NVPTXBaseInfo.h:21
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:297
bool parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine basic block definitions, and skip the machine instructions.
Definition: MIParser.cpp:3554
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...
Definition: MIRPrinter.cpp:604
bool parseMBBReference(PerFunctionMIParsingState &PFS, MachineBasicBlock *&MBB, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3565
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:469
AtomicOrdering
Atomic ordering for LLVM's memory model.
bool parseMachineInstructions(PerFunctionMIParsingState &PFS, StringRef Src, SMDiagnostic &Error)
Parse the machine instructions.
Definition: MIParser.cpp:3560
bool parseRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3571
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:187
bool parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src, SMRange SourceRange, SMDiagnostic &Error)
Definition: MIParser.cpp:3600
bool parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, VRegInfo *&Info, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3583
bool parseNamedRegisterReference(PerFunctionMIParsingState &PFS, Register &Reg, StringRef Src, SMDiagnostic &Error)
Definition: MIParser.cpp:3577
RegisterKind Kind
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:651
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:674
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:668
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:677
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
static const MBBSectionID ExceptionSectionID
static const MBBSectionID ColdSectionID
A token produced by the machine instruction lexer.
Definition: MILexer.h:26
TokenKind kind() const
Definition: MILexer.h:195
bool hasIntegerValue() const
Definition: MILexer.h:235
bool is(TokenKind K) const
Definition: MILexer.h:222
StringRef stringValue() const
Return the token's string value.
Definition: MILexer.h:231
@ kw_target_flags
Definition: MILexer.h:104
@ kw_landing_pad
Definition: MILexer.h:118
@ kw_blockaddress
Definition: MILexer.h:95
@ kw_pre_instr_symbol
Definition: MILexer.h:126
@ md_alias_scope
Definition: MILexer.h:143
@ kw_intrinsic
Definition: MILexer.h:96
@ NamedGlobalValue
Definition: MILexer.h:157
@ SubRegisterIndex
Definition: MILexer.h:175
@ kw_frame_setup
Definition: MILexer.h:63
@ kw_cfi_aarch64_negate_ra_sign_state
Definition: MILexer.h:94
@ ConstantPoolItem
Definition: MILexer.h:168
@ kw_cfi_llvm_def_aspace_cfa
Definition: MILexer.h:87
@ MachineBasicBlock
Definition: MILexer.h:154
@ kw_dbg_instr_ref
Definition: MILexer.h:78
@ NamedVirtualRegister
Definition: MILexer.h:152
@ kw_early_clobber
Definition: MILexer.h:59
@ kw_cfi_offset
Definition: MILexer.h:80
@ FloatingPointLiteral
Definition: MILexer.h:164
@ kw_debug_use
Definition: MILexer.h:60
@ kw_cfi_window_save
Definition: MILexer.h:93
@ kw_constant_pool
Definition: MILexer.h:114
@ kw_frame_destroy
Definition: MILexer.h:64
@ kw_cfi_undefined
Definition: MILexer.h:92
@ StringConstant
Definition: MILexer.h:176
@ MachineBasicBlockLabel
Definition: MILexer.h:153
@ kw_cfi_restore
Definition: MILexer.h:90
@ kw_non_temporal
Definition: MILexer.h:106
@ kw_cfi_register
Definition: MILexer.h:88
@ kw_inlineasm_br_indirect_target
Definition: MILexer.h:119
@ kw_cfi_rel_offset
Definition: MILexer.h:81
@ kw_ehfunclet_entry
Definition: MILexer.h:120
@ kw_cfi_def_cfa_register
Definition: MILexer.h:82
@ FixedStackObject
Definition: MILexer.h:156
@ kw_cfi_same_value
Definition: MILexer.h:79
@ kw_target_index
Definition: MILexer.h:97
@ kw_cfi_adjust_cfa_offset
Definition: MILexer.h:84
@ kw_dereferenceable
Definition: MILexer.h:55
@ kw_implicit_define
Definition: MILexer.h:52
@ kw_cfi_def_cfa
Definition: MILexer.h:86
@ kw_cfi_escape
Definition: MILexer.h:85
@ VirtualRegister
Definition: MILexer.h:167
@ kw_cfi_def_cfa_offset
Definition: MILexer.h:83
@ kw_machine_block_address_taken
Definition: MILexer.h:136
@ kw_renamable
Definition: MILexer.h:61
@ ExternalSymbol
Definition: MILexer.h:159
@ kw_unknown_size
Definition: MILexer.h:133
@ IntegerLiteral
Definition: MILexer.h:163
@ kw_cfi_remember_state
Definition: MILexer.h:89
@ kw_debug_instr_number
Definition: MILexer.h:77
@ kw_post_instr_symbol
Definition: MILexer.h:127
@ kw_cfi_restore_state
Definition: MILexer.h:91
@ kw_nofpexcept
Definition: MILexer.h:75
@ kw_ir_block_address_taken
Definition: MILexer.h:135
@ kw_unknown_address
Definition: MILexer.h:134
@ JumpTableIndex
Definition: MILexer.h:169
@ kw_shufflemask
Definition: MILexer.h:125
@ kw_debug_location
Definition: MILexer.h:76
@ kw_heap_alloc_marker
Definition: MILexer.h:128
StringRef range() const
Definition: MILexer.h:228
StringRef::iterator location() const
Definition: MILexer.h:226
const APSInt & integerValue() const
Definition: MILexer.h:233
This class contains a discriminated union of information about pointers in memory operands,...
VRegInfo & getVRegInfo(Register Num)
Definition: MIParser.cpp:319
const SlotMapping & IRSlots
Definition: MIParser.h:166
const Value * getIRValue(unsigned Slot)
Definition: MIParser.cpp:364
DenseMap< unsigned, MachineBasicBlock * > MBBSlots
Definition: MIParser.h:172
StringMap< VRegInfo * > VRegInfosNamed
Definition: MIParser.h:174
DenseMap< unsigned, const Value * > Slots2Values
Maps from slot numbers to function's unnamed values.
Definition: MIParser.h:181
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &Target)
Definition: MIParser.cpp:314
DenseMap< Register, VRegInfo * > VRegInfos
Definition: MIParser.h:173
VRegInfo & getVRegInfoNamed(StringRef RegName)
Definition: MIParser.cpp:330
BumpPtrAllocator Allocator
Definition: MIParser.h:163
bool getDirectTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a direct target flag to the corresponding target flag.
Definition: MIParser.cpp:217
const RegisterBank * getRegBank(StringRef Name)
Check if the given identifier is a name of a register bank.
Definition: MIParser.cpp:307
bool parseInstrName(StringRef InstrName, unsigned &OpCode)
Try to convert an instruction name to an opcode.
Definition: MIParser.cpp:138
unsigned getSubRegIndex(StringRef Name)
Check if the given identifier is a name of a subregister index.
Definition: MIParser.cpp:178
bool getTargetIndex(StringRef Name, int &Index)
Try to convert a name of target index to the corresponding target index.
Definition: MIParser.cpp:196
void setTarget(const TargetSubtargetInfo &NewSubtarget)
Definition: MIParser.cpp:81
bool getRegisterByName(StringRef RegName, Register &Reg)
Try to convert a register name to a register number.
Definition: MIParser.cpp:119
bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag)
Try to convert a name of a MachineMemOperand target flag to the corresponding target flag.
Definition: MIParser.cpp:260
bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag)
Try to convert a name of a bitmask target flag to the corresponding target flag.
Definition: MIParser.cpp:239
const TargetRegisterClass * getRegClass(StringRef Name)
Check if the given identifier is a name of a register class.
Definition: MIParser.cpp:300
const uint32_t * getRegMask(StringRef Identifier)
Check if the given identifier is a name of a register mask.
Definition: MIParser.cpp:161
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:32
std::vector< GlobalValue * > GlobalValues
Definition: SlotMapping.h:33
Definition: regcomp.c:192