1 //===- MIRParser.cpp - MIR serialization format parser implementation -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that parses the optional LLVM IR and machine
11 // functions that are stored in MIR files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MIRParser/MIRParser.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/AsmParser/Parser.h"
22 #include "llvm/AsmParser/SlotMapping.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/MIRYamlMapping.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ValueSymbolTable.h"
34 #include "llvm/Support/LineIterator.h"
35 #include "llvm/Support/SMLoc.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/YAMLTraits.h"
45 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
51 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
53 /// Maps from register class names to register classes.
54 StringMap<const TargetRegisterClass *> Names2RegClasses;
57 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
58 LLVMContext &Context);
60 void reportDiagnostic(const SMDiagnostic &Diag);
62 /// Report an error with the given message at unknown location.
64 /// Always returns true.
65 bool error(const Twine &Message);
67 /// Report an error with the given message at the given location.
69 /// Always returns true.
70 bool error(SMLoc Loc, const Twine &Message);
72 /// Report a given error with the location translated from the location in an
73 /// embedded string literal to a location in the MIR file.
75 /// Always returns true.
76 bool error(const SMDiagnostic &Error, SMRange SourceRange);
78 /// Try to parse the optional LLVM module and the machine functions in the MIR
81 /// Return null if an error occurred.
82 std::unique_ptr<Module> parse();
84 /// Parse the machine function in the current YAML document.
86 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
87 /// A dummy IR function is created and inserted into the given module when
88 /// this parameter is true.
90 /// Return true if an error occurred.
91 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
93 /// Initialize the machine function to the state that's described in the MIR
96 /// Return true if error occurred.
97 bool initializeMachineFunction(MachineFunction &MF);
99 bool initializeRegisterInfo(MachineFunction &MF,
100 const yaml::MachineFunction &YamlMF,
101 PerFunctionMIParsingState &PFS);
103 void inferRegisterInfo(MachineFunction &MF,
104 const yaml::MachineFunction &YamlMF);
106 bool initializeFrameInfo(MachineFunction &MF,
107 const yaml::MachineFunction &YamlMF,
108 PerFunctionMIParsingState &PFS);
110 bool parseCalleeSavedRegister(MachineFunction &MF,
111 PerFunctionMIParsingState &PFS,
112 std::vector<CalleeSavedInfo> &CSIInfo,
113 const yaml::StringValue &RegisterSource,
116 bool initializeConstantPool(MachineConstantPool &ConstantPool,
117 const yaml::MachineFunction &YamlMF,
118 const MachineFunction &MF,
119 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
121 bool initializeJumpTableInfo(MachineFunction &MF,
122 const yaml::MachineJumpTable &YamlJTI,
123 PerFunctionMIParsingState &PFS);
126 bool parseMBBReference(MachineBasicBlock *&MBB,
127 const yaml::StringValue &Source, MachineFunction &MF,
128 const PerFunctionMIParsingState &PFS);
130 /// Return a MIR diagnostic converted from an MI string diagnostic.
131 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
132 SMRange SourceRange);
134 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
135 /// block scalar string.
136 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
137 SMRange SourceRange);
139 /// Create an empty function with the given name.
140 void createDummyFunction(StringRef Name, Module &M);
142 void initNames2RegClasses(const MachineFunction &MF);
144 /// Check if the given identifier is a name of a register class.
146 /// Return null if the name isn't a register class.
147 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
151 } // end namespace llvm
153 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
154 StringRef Filename, LLVMContext &Context)
155 : SM(), Filename(Filename), Context(Context) {
156 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
159 bool MIRParserImpl::error(const Twine &Message) {
160 Context.diagnose(DiagnosticInfoMIRParser(
161 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
165 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
166 Context.diagnose(DiagnosticInfoMIRParser(
167 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
171 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
172 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
173 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
177 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
178 DiagnosticSeverity Kind;
179 switch (Diag.getKind()) {
180 case SourceMgr::DK_Error:
183 case SourceMgr::DK_Warning:
186 case SourceMgr::DK_Note:
190 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
193 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
194 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
197 std::unique_ptr<Module> MIRParserImpl::parse() {
198 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
199 /*Ctxt=*/nullptr, handleYAMLDiag, this);
202 if (!In.setCurrentDocument()) {
205 // Create an empty module when the MIR file is empty.
206 return llvm::make_unique<Module>(Filename, Context);
209 std::unique_ptr<Module> M;
210 bool NoLLVMIR = false;
211 // Parse the block scalar manually so that we can return unique pointer
212 // without having to go trough YAML traits.
213 if (const auto *BSN =
214 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
216 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
219 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
223 if (!In.setCurrentDocument())
226 // Create an new, empty module.
227 M = llvm::make_unique<Module>(Filename, Context);
231 // Parse the machine functions.
233 if (parseMachineFunction(In, *M, NoLLVMIR))
236 } while (In.setCurrentDocument());
241 bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
243 auto MF = llvm::make_unique<yaml::MachineFunction>();
244 yaml::yamlize(In, *MF, false);
247 auto FunctionName = MF->Name;
248 if (Functions.find(FunctionName) != Functions.end())
249 return error(Twine("redefinition of machine function '") + FunctionName +
251 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
253 createDummyFunction(FunctionName, M);
254 else if (!M.getFunction(FunctionName))
255 return error(Twine("function '") + FunctionName +
256 "' isn't defined in the provided LLVM IR");
260 void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
261 auto &Context = M.getContext();
262 Function *F = cast<Function>(M.getOrInsertFunction(
263 Name, FunctionType::get(Type::getVoidTy(Context), false)));
264 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
265 new UnreachableInst(Context, BB);
268 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
269 auto It = Functions.find(MF.getName());
270 if (It == Functions.end())
271 return error(Twine("no machine function information for function '") +
272 MF.getName() + "' in the MIR file");
273 // TODO: Recreate the machine function.
274 const yaml::MachineFunction &YamlMF = *It->getValue();
275 if (YamlMF.Alignment)
276 MF.setAlignment(YamlMF.Alignment);
277 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
278 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
279 PerFunctionMIParsingState PFS;
280 if (initializeRegisterInfo(MF, YamlMF, PFS))
282 if (!YamlMF.Constants.empty()) {
283 auto *ConstantPool = MF.getConstantPool();
284 assert(ConstantPool && "Constant pool must be created");
285 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
286 PFS.ConstantPoolSlots))
291 if (parseMachineBasicBlockDefinitions(MF, YamlMF.Body.Value.Value, PFS,
294 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
299 return error(Twine("machine function '") + Twine(MF.getName()) +
300 "' requires at least one machine basic block in its body");
301 // Initialize the frame information after creating all the MBBs so that the
302 // MBB references in the frame information can be resolved.
303 if (initializeFrameInfo(MF, YamlMF, PFS))
305 // Initialize the jump table after creating all the MBBs so that the MBB
306 // references can be resolved.
307 if (!YamlMF.JumpTableInfo.Entries.empty() &&
308 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
310 // Parse the machine instructions after creating all of the MBBs so that the
311 // parser can resolve the MBB references.
312 if (parseMachineInstructions(MF, YamlMF.Body.Value.Value, PFS, IRSlots,
315 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
318 inferRegisterInfo(MF, YamlMF);
319 // FIXME: This is a temporary workaround until the reserved registers can be
321 MF.getRegInfo().freezeReservedRegs(MF);
326 bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
327 const yaml::MachineFunction &YamlMF,
328 PerFunctionMIParsingState &PFS) {
329 MachineRegisterInfo &RegInfo = MF.getRegInfo();
330 assert(RegInfo.isSSA());
333 assert(RegInfo.tracksLiveness());
334 if (!YamlMF.TracksRegLiveness)
335 RegInfo.invalidateLiveness();
336 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
339 // Parse the virtual register information.
340 for (const auto &VReg : YamlMF.VirtualRegisters) {
341 const auto *RC = getRegClass(MF, VReg.Class.Value);
343 return error(VReg.Class.SourceRange.Start,
344 Twine("use of undefined register class '") +
345 VReg.Class.Value + "'");
346 unsigned Reg = RegInfo.createVirtualRegister(RC);
347 if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg))
349 return error(VReg.ID.SourceRange.Start,
350 Twine("redefinition of virtual register '%") +
351 Twine(VReg.ID.Value) + "'");
352 if (!VReg.PreferredRegister.Value.empty()) {
353 unsigned PreferredReg = 0;
354 if (parseNamedRegisterReference(PreferredReg, SM, MF,
355 VReg.PreferredRegister.Value, PFS,
357 return error(Error, VReg.PreferredRegister.SourceRange);
358 RegInfo.setSimpleHint(Reg, PreferredReg);
362 // Parse the liveins.
363 for (const auto &LiveIn : YamlMF.LiveIns) {
365 if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
367 return error(Error, LiveIn.Register.SourceRange);
369 if (!LiveIn.VirtualRegister.Value.empty()) {
370 if (parseVirtualRegisterReference(
371 VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
372 return error(Error, LiveIn.VirtualRegister.SourceRange);
374 RegInfo.addLiveIn(Reg, VReg);
377 // Parse the callee saved register mask.
378 BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size());
379 if (!YamlMF.CalleeSavedRegisters)
381 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
383 if (parseNamedRegisterReference(Reg, SM, MF, RegSource.Value, PFS, IRSlots,
385 return error(Error, RegSource.SourceRange);
386 CalleeSavedRegisterMask[Reg] = true;
388 RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip());
392 void MIRParserImpl::inferRegisterInfo(MachineFunction &MF,
393 const yaml::MachineFunction &YamlMF) {
394 if (YamlMF.CalleeSavedRegisters)
396 for (const MachineBasicBlock &MBB : MF) {
397 for (const MachineInstr &MI : MBB) {
398 for (const MachineOperand &MO : MI.operands()) {
401 MF.getRegInfo().addPhysRegsUsedFromRegMask(MO.getRegMask());
407 bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
408 const yaml::MachineFunction &YamlMF,
409 PerFunctionMIParsingState &PFS) {
410 MachineFrameInfo &MFI = *MF.getFrameInfo();
411 const Function &F = *MF.getFunction();
412 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
413 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
414 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
415 MFI.setHasStackMap(YamlMFI.HasStackMap);
416 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
417 MFI.setStackSize(YamlMFI.StackSize);
418 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
419 if (YamlMFI.MaxAlignment)
420 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
421 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
422 MFI.setHasCalls(YamlMFI.HasCalls);
423 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
424 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
425 MFI.setHasVAStart(YamlMFI.HasVAStart);
426 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
427 if (!YamlMFI.SavePoint.Value.empty()) {
428 MachineBasicBlock *MBB = nullptr;
429 if (parseMBBReference(MBB, YamlMFI.SavePoint, MF, PFS))
431 MFI.setSavePoint(MBB);
433 if (!YamlMFI.RestorePoint.Value.empty()) {
434 MachineBasicBlock *MBB = nullptr;
435 if (parseMBBReference(MBB, YamlMFI.RestorePoint, MF, PFS))
437 MFI.setRestorePoint(MBB);
440 std::vector<CalleeSavedInfo> CSIInfo;
441 // Initialize the fixed frame objects.
442 for (const auto &Object : YamlMF.FixedStackObjects) {
444 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
445 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
446 Object.IsImmutable, Object.IsAliased);
448 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
449 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
450 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
453 return error(Object.ID.SourceRange.Start,
454 Twine("redefinition of fixed stack object '%fixed-stack.") +
455 Twine(Object.ID.Value) + "'");
456 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
461 // Initialize the ordinary frame objects.
462 for (const auto &Object : YamlMF.StackObjects) {
464 const AllocaInst *Alloca = nullptr;
465 const yaml::StringValue &Name = Object.Name;
466 if (!Name.Value.empty()) {
467 Alloca = dyn_cast_or_null<AllocaInst>(
468 F.getValueSymbolTable().lookup(Name.Value));
470 return error(Name.SourceRange.Start,
471 "alloca instruction named '" + Name.Value +
472 "' isn't defined in the function '" + F.getName() +
475 if (Object.Type == yaml::MachineStackObject::VariableSized)
476 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
478 ObjectIdx = MFI.CreateStackObject(
479 Object.Size, Object.Alignment,
480 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
481 MFI.setObjectOffset(ObjectIdx, Object.Offset);
482 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
484 return error(Object.ID.SourceRange.Start,
485 Twine("redefinition of stack object '%stack.") +
486 Twine(Object.ID.Value) + "'");
487 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
490 if (Object.LocalOffset)
491 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
493 MFI.setCalleeSavedInfo(CSIInfo);
494 if (!CSIInfo.empty())
495 MFI.setCalleeSavedInfoValid(true);
497 // Initialize the various stack object references after initializing the
499 if (!YamlMFI.StackProtector.Value.empty()) {
502 if (parseStackObjectReference(FI, SM, MF, YamlMFI.StackProtector.Value, PFS,
504 return error(Error, YamlMFI.StackProtector.SourceRange);
505 MFI.setStackProtectorIndex(FI);
510 bool MIRParserImpl::parseCalleeSavedRegister(
511 MachineFunction &MF, PerFunctionMIParsingState &PFS,
512 std::vector<CalleeSavedInfo> &CSIInfo,
513 const yaml::StringValue &RegisterSource, int FrameIdx) {
514 if (RegisterSource.Value.empty())
518 if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
520 return error(Error, RegisterSource.SourceRange);
521 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
525 bool MIRParserImpl::initializeConstantPool(
526 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
527 const MachineFunction &MF,
528 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
529 const auto &M = *MF.getFunction()->getParent();
531 for (const auto &YamlConstant : YamlMF.Constants) {
532 const Constant *Value = dyn_cast_or_null<Constant>(
533 parseConstantValue(YamlConstant.Value.Value, Error, M));
535 return error(Error, YamlConstant.Value.SourceRange);
537 YamlConstant.Alignment
538 ? YamlConstant.Alignment
539 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
540 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
541 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
543 return error(YamlConstant.ID.SourceRange.Start,
544 Twine("redefinition of constant pool item '%const.") +
545 Twine(YamlConstant.ID.Value) + "'");
550 bool MIRParserImpl::initializeJumpTableInfo(
551 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
552 PerFunctionMIParsingState &PFS) {
553 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
554 for (const auto &Entry : YamlJTI.Entries) {
555 std::vector<MachineBasicBlock *> Blocks;
556 for (const auto &MBBSource : Entry.Blocks) {
557 MachineBasicBlock *MBB = nullptr;
558 if (parseMBBReference(MBB, MBBSource.Value, MF, PFS))
560 Blocks.push_back(MBB);
562 unsigned Index = JTI->createJumpTableIndex(Blocks);
563 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
565 return error(Entry.ID.SourceRange.Start,
566 Twine("redefinition of jump table entry '%jump-table.") +
567 Twine(Entry.ID.Value) + "'");
572 bool MIRParserImpl::parseMBBReference(MachineBasicBlock *&MBB,
573 const yaml::StringValue &Source,
575 const PerFunctionMIParsingState &PFS) {
577 if (llvm::parseMBBReference(MBB, SM, MF, Source.Value, PFS, IRSlots, Error))
578 return error(Error, Source.SourceRange);
582 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
583 SMRange SourceRange) {
584 assert(SourceRange.isValid() && "Invalid source range");
585 SMLoc Loc = SourceRange.Start;
586 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
587 *Loc.getPointer() == '\'';
588 // Translate the location of the error from the location in the MI string to
589 // the corresponding location in the MIR file.
590 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
593 // TODO: Translate any source ranges as well.
594 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
598 SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
599 SMRange SourceRange) {
600 assert(SourceRange.isValid());
602 // Translate the location of the error from the location in the llvm IR string
603 // to the corresponding location in the MIR file.
604 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
605 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
606 unsigned Column = Error.getColumnNo();
607 StringRef LineStr = Error.getLineContents();
608 SMLoc Loc = Error.getLoc();
610 // Get the full line and adjust the column number by taking the indentation of
611 // LLVM IR into account.
612 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
614 if (L.line_number() == Line) {
616 Loc = SMLoc::getFromPointer(LineStr.data());
617 auto Indent = LineStr.find(Error.getLineContents());
618 if (Indent != StringRef::npos)
624 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
625 Error.getMessage(), LineStr, Error.getRanges(),
629 void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
630 if (!Names2RegClasses.empty())
632 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
633 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
634 const auto *RC = TRI->getRegClass(I);
635 Names2RegClasses.insert(
636 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
640 const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
642 initNames2RegClasses(MF);
643 auto RegClassInfo = Names2RegClasses.find(Name);
644 if (RegClassInfo == Names2RegClasses.end())
646 return RegClassInfo->getValue();
649 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
650 : Impl(std::move(Impl)) {}
652 MIRParser::~MIRParser() {}
654 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
656 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
657 return Impl->initializeMachineFunction(MF);
660 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
662 LLVMContext &Context) {
663 auto FileOrErr = MemoryBuffer::getFile(Filename);
664 if (std::error_code EC = FileOrErr.getError()) {
665 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
666 "Could not open input file: " + EC.message());
669 return createMIRParser(std::move(FileOrErr.get()), Context);
672 std::unique_ptr<MIRParser>
673 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
674 LLVMContext &Context) {
675 auto Filename = Contents->getBufferIdentifier();
676 return llvm::make_unique<MIRParser>(
677 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));