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