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