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