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