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