1 //===- MIRParser.cpp - MIR serialization format 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 class that parses the optional LLVM IR and machine
10 // functions that are stored in MIR files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/MIRParser/MIRParser.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/AsmParser/Parser.h"
20 #include "llvm/AsmParser/SlotMapping.h"
21 #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
22 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
23 #include "llvm/CodeGen/MIRParser/MIParser.h"
24 #include "llvm/CodeGen/MIRYamlMapping.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/TargetFrameLowering.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DiagnosticInfo.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/ValueSymbolTable.h"
38 #include "llvm/Support/LineIterator.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/SMLoc.h"
41 #include "llvm/Support/SourceMgr.h"
42 #include "llvm/Support/YAMLTraits.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include <memory>
45 
46 using namespace llvm;
47 
48 namespace llvm {
49 
50 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
51 /// file.
52 class MIRParserImpl {
53   SourceMgr SM;
54   yaml::Input In;
55   StringRef Filename;
56   LLVMContext &Context;
57   SlotMapping IRSlots;
58   std::unique_ptr<PerTargetMIParsingState> Target;
59 
60   /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are
61   /// created and inserted into the given module when this is true.
62   bool NoLLVMIR = false;
63   /// True when a well formed MIR file does not contain any MIR/machine function
64   /// parts.
65   bool NoMIRDocuments = false;
66 
67   std::function<void(Function &)> ProcessIRFunction;
68 
69 public:
70   MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
71                 LLVMContext &Context,
72                 std::function<void(Function &)> ProcessIRFunction);
73 
74   void reportDiagnostic(const SMDiagnostic &Diag);
75 
76   /// Report an error with the given message at unknown location.
77   ///
78   /// Always returns true.
79   bool error(const Twine &Message);
80 
81   /// Report an error with the given message at the given location.
82   ///
83   /// Always returns true.
84   bool error(SMLoc Loc, const Twine &Message);
85 
86   /// Report a given error with the location translated from the location in an
87   /// embedded string literal to a location in the MIR file.
88   ///
89   /// Always returns true.
90   bool error(const SMDiagnostic &Error, SMRange SourceRange);
91 
92   /// Try to parse the optional LLVM module and the machine functions in the MIR
93   /// file.
94   ///
95   /// Return null if an error occurred.
96   std::unique_ptr<Module>
97   parseIRModule(DataLayoutCallbackTy DataLayoutCallback);
98 
99   /// Create an empty function with the given name.
100   Function *createDummyFunction(StringRef Name, Module &M);
101 
102   bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
103 
104   /// Parse the machine function in the current YAML document.
105   ///
106   ///
107   /// Return true if an error occurred.
108   bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
109 
110   /// Initialize the machine function to the state that's described in the MIR
111   /// file.
112   ///
113   /// Return true if error occurred.
114   bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
115                                  MachineFunction &MF);
116 
117   bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
118                          const yaml::MachineFunction &YamlMF);
119 
120   bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
121                          const yaml::MachineFunction &YamlMF);
122 
123   bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
124                            const yaml::MachineFunction &YamlMF);
125 
126   bool initializeCallSiteInfo(PerFunctionMIParsingState &PFS,
127                               const yaml::MachineFunction &YamlMF);
128 
129   bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
130                                 std::vector<CalleeSavedInfo> &CSIInfo,
131                                 const yaml::StringValue &RegisterSource,
132                                 bool IsRestored, int FrameIdx);
133 
134   template <typename T>
135   bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
136                                   const T &Object,
137                                   int FrameIdx);
138 
139   bool initializeConstantPool(PerFunctionMIParsingState &PFS,
140                               MachineConstantPool &ConstantPool,
141                               const yaml::MachineFunction &YamlMF);
142 
143   bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
144                                const yaml::MachineJumpTable &YamlJTI);
145 
146 private:
147   bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
148                    const yaml::StringValue &Source);
149 
150   bool parseMBBReference(PerFunctionMIParsingState &PFS,
151                          MachineBasicBlock *&MBB,
152                          const yaml::StringValue &Source);
153 
154   /// Return a MIR diagnostic converted from an MI string diagnostic.
155   SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
156                                     SMRange SourceRange);
157 
158   /// Return a MIR diagnostic converted from a diagnostic located in a YAML
159   /// block scalar string.
160   SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
161                                        SMRange SourceRange);
162 
163   void computeFunctionProperties(MachineFunction &MF);
164 };
165 
166 } // end namespace llvm
167 
168 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
169   reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
170 }
171 
172 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
173                              StringRef Filename, LLVMContext &Context,
174                              std::function<void(Function &)> Callback)
175     : SM(),
176       In(SM.getMemoryBuffer(SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))
177              ->getBuffer(),
178          nullptr, handleYAMLDiag, this),
179       Filename(Filename), Context(Context), ProcessIRFunction(Callback) {
180   In.setContext(&In);
181 }
182 
183 bool MIRParserImpl::error(const Twine &Message) {
184   Context.diagnose(DiagnosticInfoMIRParser(
185       DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
186   return true;
187 }
188 
189 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
190   Context.diagnose(DiagnosticInfoMIRParser(
191       DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
192   return true;
193 }
194 
195 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
196   assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
197   reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
198   return true;
199 }
200 
201 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
202   DiagnosticSeverity Kind;
203   switch (Diag.getKind()) {
204   case SourceMgr::DK_Error:
205     Kind = DS_Error;
206     break;
207   case SourceMgr::DK_Warning:
208     Kind = DS_Warning;
209     break;
210   case SourceMgr::DK_Note:
211     Kind = DS_Note;
212     break;
213   case SourceMgr::DK_Remark:
214     llvm_unreachable("remark unexpected");
215     break;
216   }
217   Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
218 }
219 
220 std::unique_ptr<Module>
221 MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) {
222   if (!In.setCurrentDocument()) {
223     if (In.error())
224       return nullptr;
225     // Create an empty module when the MIR file is empty.
226     NoMIRDocuments = true;
227     auto M = std::make_unique<Module>(Filename, Context);
228     if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple()))
229       M->setDataLayout(*LayoutOverride);
230     return M;
231   }
232 
233   std::unique_ptr<Module> M;
234   // Parse the block scalar manually so that we can return unique pointer
235   // without having to go trough YAML traits.
236   if (const auto *BSN =
237           dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
238     SMDiagnostic Error;
239     M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
240                       Context, &IRSlots, DataLayoutCallback);
241     if (!M) {
242       reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
243       return nullptr;
244     }
245     In.nextDocument();
246     if (!In.setCurrentDocument())
247       NoMIRDocuments = true;
248   } else {
249     // Create an new, empty module.
250     M = std::make_unique<Module>(Filename, Context);
251     if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple()))
252       M->setDataLayout(*LayoutOverride);
253     NoLLVMIR = true;
254   }
255   return M;
256 }
257 
258 bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
259   if (NoMIRDocuments)
260     return false;
261 
262   // Parse the machine functions.
263   do {
264     if (parseMachineFunction(M, MMI))
265       return true;
266     In.nextDocument();
267   } while (In.setCurrentDocument());
268 
269   return false;
270 }
271 
272 Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
273   auto &Context = M.getContext();
274   Function *F =
275       Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
276                        Function::ExternalLinkage, Name, M);
277   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
278   new UnreachableInst(Context, BB);
279 
280   if (ProcessIRFunction)
281     ProcessIRFunction(*F);
282 
283   return F;
284 }
285 
286 bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
287   // Parse the yaml.
288   yaml::MachineFunction YamlMF;
289   yaml::EmptyContext Ctx;
290 
291   const LLVMTargetMachine &TM = MMI.getTarget();
292   YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
293       TM.createDefaultFuncInfoYAML());
294 
295   yaml::yamlize(In, YamlMF, false, Ctx);
296   if (In.error())
297     return true;
298 
299   // Search for the corresponding IR function.
300   StringRef FunctionName = YamlMF.Name;
301   Function *F = M.getFunction(FunctionName);
302   if (!F) {
303     if (NoLLVMIR) {
304       F = createDummyFunction(FunctionName, M);
305     } else {
306       return error(Twine("function '") + FunctionName +
307                    "' isn't defined in the provided LLVM IR");
308     }
309   }
310   if (MMI.getMachineFunction(*F) != nullptr)
311     return error(Twine("redefinition of machine function '") + FunctionName +
312                  "'");
313 
314   // Create the MachineFunction.
315   MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
316   if (initializeMachineFunction(YamlMF, MF))
317     return true;
318 
319   return false;
320 }
321 
322 static bool isSSA(const MachineFunction &MF) {
323   const MachineRegisterInfo &MRI = MF.getRegInfo();
324   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
325     Register Reg = Register::index2VirtReg(I);
326     if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
327       return false;
328 
329     // Subregister defs are invalid in SSA.
330     const MachineOperand *RegDef = MRI.getOneDef(Reg);
331     if (RegDef && RegDef->getSubReg() != 0)
332       return false;
333   }
334   return true;
335 }
336 
337 void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
338   MachineFunctionProperties &Properties = MF.getProperties();
339 
340   bool HasPHI = false;
341   bool HasInlineAsm = false;
342   for (const MachineBasicBlock &MBB : MF) {
343     for (const MachineInstr &MI : MBB) {
344       if (MI.isPHI())
345         HasPHI = true;
346       if (MI.isInlineAsm())
347         HasInlineAsm = true;
348     }
349   }
350   if (!HasPHI)
351     Properties.set(MachineFunctionProperties::Property::NoPHIs);
352   MF.setHasInlineAsm(HasInlineAsm);
353 
354   if (isSSA(MF))
355     Properties.set(MachineFunctionProperties::Property::IsSSA);
356   else
357     Properties.reset(MachineFunctionProperties::Property::IsSSA);
358 
359   const MachineRegisterInfo &MRI = MF.getRegInfo();
360   if (MRI.getNumVirtRegs() == 0)
361     Properties.set(MachineFunctionProperties::Property::NoVRegs);
362 }
363 
364 bool MIRParserImpl::initializeCallSiteInfo(
365     PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) {
366   MachineFunction &MF = PFS.MF;
367   SMDiagnostic Error;
368   const LLVMTargetMachine &TM = MF.getTarget();
369   for (auto YamlCSInfo : YamlMF.CallSitesInfo) {
370     yaml::CallSiteInfo::MachineInstrLoc MILoc = YamlCSInfo.CallLocation;
371     if (MILoc.BlockNum >= MF.size())
372       return error(Twine(MF.getName()) +
373                    Twine(" call instruction block out of range.") +
374                    " Unable to reference bb:" + Twine(MILoc.BlockNum));
375     auto CallB = std::next(MF.begin(), MILoc.BlockNum);
376     if (MILoc.Offset >= CallB->size())
377       return error(Twine(MF.getName()) +
378                    Twine(" call instruction offset out of range.") +
379                    " Unable to reference instruction at bb: " +
380                    Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset));
381     auto CallI = std::next(CallB->instr_begin(), MILoc.Offset);
382     if (!CallI->isCall(MachineInstr::IgnoreBundle))
383       return error(Twine(MF.getName()) +
384                    Twine(" call site info should reference call "
385                          "instruction. Instruction at bb:") +
386                    Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) +
387                    " is not a call instruction");
388     MachineFunction::CallSiteInfo CSInfo;
389     for (auto ArgRegPair : YamlCSInfo.ArgForwardingRegs) {
390       Register Reg;
391       if (parseNamedRegisterReference(PFS, Reg, ArgRegPair.Reg.Value, Error))
392         return error(Error, ArgRegPair.Reg.SourceRange);
393       CSInfo.emplace_back(Reg, ArgRegPair.ArgNo);
394     }
395 
396     if (TM.Options.EmitCallSiteInfo)
397       MF.addCallArgsForwardingRegs(&*CallI, std::move(CSInfo));
398   }
399 
400   if (YamlMF.CallSitesInfo.size() && !TM.Options.EmitCallSiteInfo)
401     return error(Twine("Call site info provided but not used"));
402   return false;
403 }
404 
405 bool
406 MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
407                                          MachineFunction &MF) {
408   // TODO: Recreate the machine function.
409   if (Target) {
410     // Avoid clearing state if we're using the same subtarget again.
411     Target->setTarget(MF.getSubtarget());
412   } else {
413     Target.reset(new PerTargetMIParsingState(MF.getSubtarget()));
414   }
415 
416   MF.setAlignment(YamlMF.Alignment.valueOrOne());
417   MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
418   MF.setHasWinCFI(YamlMF.HasWinCFI);
419 
420   if (YamlMF.Legalized)
421     MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
422   if (YamlMF.RegBankSelected)
423     MF.getProperties().set(
424         MachineFunctionProperties::Property::RegBankSelected);
425   if (YamlMF.Selected)
426     MF.getProperties().set(MachineFunctionProperties::Property::Selected);
427   if (YamlMF.FailedISel)
428     MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
429 
430   PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target);
431   if (parseRegisterInfo(PFS, YamlMF))
432     return true;
433   if (!YamlMF.Constants.empty()) {
434     auto *ConstantPool = MF.getConstantPool();
435     assert(ConstantPool && "Constant pool must be created");
436     if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
437       return true;
438   }
439 
440   StringRef BlockStr = YamlMF.Body.Value.Value;
441   SMDiagnostic Error;
442   SourceMgr BlockSM;
443   BlockSM.AddNewSourceBuffer(
444       MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
445       SMLoc());
446   PFS.SM = &BlockSM;
447   if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
448     reportDiagnostic(
449         diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
450     return true;
451   }
452   // Check Basic Block Section Flags.
453   if (MF.getTarget().getBBSectionsType() == BasicBlockSection::Labels) {
454     MF.setBBSectionsType(BasicBlockSection::Labels);
455   } else if (MF.hasBBSections()) {
456     MF.assignBeginEndSections();
457   }
458   PFS.SM = &SM;
459 
460   // Initialize the frame information after creating all the MBBs so that the
461   // MBB references in the frame information can be resolved.
462   if (initializeFrameInfo(PFS, YamlMF))
463     return true;
464   // Initialize the jump table after creating all the MBBs so that the MBB
465   // references can be resolved.
466   if (!YamlMF.JumpTableInfo.Entries.empty() &&
467       initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
468     return true;
469   // Parse the machine instructions after creating all of the MBBs so that the
470   // parser can resolve the MBB references.
471   StringRef InsnStr = YamlMF.Body.Value.Value;
472   SourceMgr InsnSM;
473   InsnSM.AddNewSourceBuffer(
474       MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
475       SMLoc());
476   PFS.SM = &InsnSM;
477   if (parseMachineInstructions(PFS, InsnStr, Error)) {
478     reportDiagnostic(
479         diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
480     return true;
481   }
482   PFS.SM = &SM;
483 
484   if (setupRegisterInfo(PFS, YamlMF))
485     return true;
486 
487   if (YamlMF.MachineFuncInfo) {
488     const LLVMTargetMachine &TM = MF.getTarget();
489     // Note this is called after the initial constructor of the
490     // MachineFunctionInfo based on the MachineFunction, which may depend on the
491     // IR.
492 
493     SMRange SrcRange;
494     if (TM.parseMachineFunctionInfo(*YamlMF.MachineFuncInfo, PFS, Error,
495                                     SrcRange)) {
496       return error(Error, SrcRange);
497     }
498   }
499 
500   // Set the reserved registers after parsing MachineFuncInfo. The target may
501   // have been recording information used to select the reserved registers
502   // there.
503   // FIXME: This is a temporary workaround until the reserved registers can be
504   // serialized.
505   MachineRegisterInfo &MRI = MF.getRegInfo();
506   MRI.freezeReservedRegs(MF);
507 
508   computeFunctionProperties(MF);
509 
510   if (initializeCallSiteInfo(PFS, YamlMF))
511     return false;
512 
513   MF.getSubtarget().mirFileLoaded(MF);
514 
515   MF.verify();
516   return false;
517 }
518 
519 bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
520                                       const yaml::MachineFunction &YamlMF) {
521   MachineFunction &MF = PFS.MF;
522   MachineRegisterInfo &RegInfo = MF.getRegInfo();
523   assert(RegInfo.tracksLiveness());
524   if (!YamlMF.TracksRegLiveness)
525     RegInfo.invalidateLiveness();
526 
527   SMDiagnostic Error;
528   // Parse the virtual register information.
529   for (const auto &VReg : YamlMF.VirtualRegisters) {
530     VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
531     if (Info.Explicit)
532       return error(VReg.ID.SourceRange.Start,
533                    Twine("redefinition of virtual register '%") +
534                        Twine(VReg.ID.Value) + "'");
535     Info.Explicit = true;
536 
537     if (StringRef(VReg.Class.Value).equals("_")) {
538       Info.Kind = VRegInfo::GENERIC;
539       Info.D.RegBank = nullptr;
540     } else {
541       const auto *RC = Target->getRegClass(VReg.Class.Value);
542       if (RC) {
543         Info.Kind = VRegInfo::NORMAL;
544         Info.D.RC = RC;
545       } else {
546         const RegisterBank *RegBank = Target->getRegBank(VReg.Class.Value);
547         if (!RegBank)
548           return error(
549               VReg.Class.SourceRange.Start,
550               Twine("use of undefined register class or register bank '") +
551                   VReg.Class.Value + "'");
552         Info.Kind = VRegInfo::REGBANK;
553         Info.D.RegBank = RegBank;
554       }
555     }
556 
557     if (!VReg.PreferredRegister.Value.empty()) {
558       if (Info.Kind != VRegInfo::NORMAL)
559         return error(VReg.Class.SourceRange.Start,
560               Twine("preferred register can only be set for normal vregs"));
561 
562       if (parseRegisterReference(PFS, Info.PreferredReg,
563                                  VReg.PreferredRegister.Value, Error))
564         return error(Error, VReg.PreferredRegister.SourceRange);
565     }
566   }
567 
568   // Parse the liveins.
569   for (const auto &LiveIn : YamlMF.LiveIns) {
570     Register Reg;
571     if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
572       return error(Error, LiveIn.Register.SourceRange);
573     Register VReg;
574     if (!LiveIn.VirtualRegister.Value.empty()) {
575       VRegInfo *Info;
576       if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
577                                         Error))
578         return error(Error, LiveIn.VirtualRegister.SourceRange);
579       VReg = Info->VReg;
580     }
581     RegInfo.addLiveIn(Reg, VReg);
582   }
583 
584   // Parse the callee saved registers (Registers that will
585   // be saved for the caller).
586   if (YamlMF.CalleeSavedRegisters) {
587     SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
588     for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
589       Register Reg;
590       if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
591         return error(Error, RegSource.SourceRange);
592       CalleeSavedRegisters.push_back(Reg);
593     }
594     RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
595   }
596 
597   return false;
598 }
599 
600 bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
601                                       const yaml::MachineFunction &YamlMF) {
602   MachineFunction &MF = PFS.MF;
603   MachineRegisterInfo &MRI = MF.getRegInfo();
604   bool Error = false;
605   // Create VRegs
606   auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
607     Register Reg = Info.VReg;
608     switch (Info.Kind) {
609     case VRegInfo::UNKNOWN:
610       error(Twine("Cannot determine class/bank of virtual register ") +
611             Name + " in function '" + MF.getName() + "'");
612       Error = true;
613       break;
614     case VRegInfo::NORMAL:
615       MRI.setRegClass(Reg, Info.D.RC);
616       if (Info.PreferredReg != 0)
617         MRI.setSimpleHint(Reg, Info.PreferredReg);
618       break;
619     case VRegInfo::GENERIC:
620       break;
621     case VRegInfo::REGBANK:
622       MRI.setRegBank(Reg, *Info.D.RegBank);
623       break;
624     }
625   };
626 
627   for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end();
628        I != E; I++) {
629     const VRegInfo &Info = *I->second;
630     populateVRegInfo(Info, Twine(I->first()));
631   }
632 
633   for (auto P : PFS.VRegInfos) {
634     const VRegInfo &Info = *P.second;
635     populateVRegInfo(Info, Twine(P.first));
636   }
637 
638   // Compute MachineRegisterInfo::UsedPhysRegMask
639   for (const MachineBasicBlock &MBB : MF) {
640     // Make sure MRI knows about registers clobbered by unwinder.
641     const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
642     if (MBB.isEHPad())
643       if (auto *RegMask = TRI->getCustomEHPadPreservedMask(MF))
644         MRI.addPhysRegsUsedFromRegMask(RegMask);
645 
646     for (const MachineInstr &MI : MBB) {
647       for (const MachineOperand &MO : MI.operands()) {
648         if (!MO.isRegMask())
649           continue;
650         MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
651       }
652     }
653   }
654 
655   return Error;
656 }
657 
658 bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
659                                         const yaml::MachineFunction &YamlMF) {
660   MachineFunction &MF = PFS.MF;
661   MachineFrameInfo &MFI = MF.getFrameInfo();
662   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
663   const Function &F = MF.getFunction();
664   const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
665   MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
666   MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
667   MFI.setHasStackMap(YamlMFI.HasStackMap);
668   MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
669   MFI.setStackSize(YamlMFI.StackSize);
670   MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
671   if (YamlMFI.MaxAlignment)
672     MFI.ensureMaxAlignment(Align(YamlMFI.MaxAlignment));
673   MFI.setAdjustsStack(YamlMFI.AdjustsStack);
674   MFI.setHasCalls(YamlMFI.HasCalls);
675   if (YamlMFI.MaxCallFrameSize != ~0u)
676     MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
677   MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
678   MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
679   MFI.setHasVAStart(YamlMFI.HasVAStart);
680   MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
681   MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
682   if (!YamlMFI.SavePoint.Value.empty()) {
683     MachineBasicBlock *MBB = nullptr;
684     if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
685       return true;
686     MFI.setSavePoint(MBB);
687   }
688   if (!YamlMFI.RestorePoint.Value.empty()) {
689     MachineBasicBlock *MBB = nullptr;
690     if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
691       return true;
692     MFI.setRestorePoint(MBB);
693   }
694 
695   std::vector<CalleeSavedInfo> CSIInfo;
696   // Initialize the fixed frame objects.
697   for (const auto &Object : YamlMF.FixedStackObjects) {
698     int ObjectIdx;
699     if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
700       ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
701                                         Object.IsImmutable, Object.IsAliased);
702     else
703       ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
704 
705     if (!TFI->isSupportedStackID(Object.StackID))
706       return error(Object.ID.SourceRange.Start,
707                    Twine("StackID is not supported by target"));
708     MFI.setStackID(ObjectIdx, Object.StackID);
709     MFI.setObjectAlignment(ObjectIdx, Object.Alignment.valueOrOne());
710     if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
711                                                          ObjectIdx))
712              .second)
713       return error(Object.ID.SourceRange.Start,
714                    Twine("redefinition of fixed stack object '%fixed-stack.") +
715                        Twine(Object.ID.Value) + "'");
716     if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
717                                  Object.CalleeSavedRestored, ObjectIdx))
718       return true;
719     if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
720       return true;
721   }
722 
723   // Initialize the ordinary frame objects.
724   for (const auto &Object : YamlMF.StackObjects) {
725     int ObjectIdx;
726     const AllocaInst *Alloca = nullptr;
727     const yaml::StringValue &Name = Object.Name;
728     if (!Name.Value.empty()) {
729       Alloca = dyn_cast_or_null<AllocaInst>(
730           F.getValueSymbolTable()->lookup(Name.Value));
731       if (!Alloca)
732         return error(Name.SourceRange.Start,
733                      "alloca instruction named '" + Name.Value +
734                          "' isn't defined in the function '" + F.getName() +
735                          "'");
736     }
737     if (!TFI->isSupportedStackID(Object.StackID))
738       return error(Object.ID.SourceRange.Start,
739                    Twine("StackID is not supported by target"));
740     if (Object.Type == yaml::MachineStackObject::VariableSized)
741       ObjectIdx =
742           MFI.CreateVariableSizedObject(Object.Alignment.valueOrOne(), Alloca);
743     else
744       ObjectIdx = MFI.CreateStackObject(
745           Object.Size, Object.Alignment.valueOrOne(),
746           Object.Type == yaml::MachineStackObject::SpillSlot, Alloca,
747           Object.StackID);
748     MFI.setObjectOffset(ObjectIdx, Object.Offset);
749 
750     if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
751              .second)
752       return error(Object.ID.SourceRange.Start,
753                    Twine("redefinition of stack object '%stack.") +
754                        Twine(Object.ID.Value) + "'");
755     if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
756                                  Object.CalleeSavedRestored, ObjectIdx))
757       return true;
758     if (Object.LocalOffset)
759       MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
760     if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
761       return true;
762   }
763   MFI.setCalleeSavedInfo(CSIInfo);
764   if (!CSIInfo.empty())
765     MFI.setCalleeSavedInfoValid(true);
766 
767   // Initialize the various stack object references after initializing the
768   // stack objects.
769   if (!YamlMFI.StackProtector.Value.empty()) {
770     SMDiagnostic Error;
771     int FI;
772     if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
773       return error(Error, YamlMFI.StackProtector.SourceRange);
774     MFI.setStackProtectorIndex(FI);
775   }
776   return false;
777 }
778 
779 bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
780     std::vector<CalleeSavedInfo> &CSIInfo,
781     const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
782   if (RegisterSource.Value.empty())
783     return false;
784   Register Reg;
785   SMDiagnostic Error;
786   if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
787     return error(Error, RegisterSource.SourceRange);
788   CalleeSavedInfo CSI(Reg, FrameIdx);
789   CSI.setRestored(IsRestored);
790   CSIInfo.push_back(CSI);
791   return false;
792 }
793 
794 /// Verify that given node is of a certain type. Return true on error.
795 template <typename T>
796 static bool typecheckMDNode(T *&Result, MDNode *Node,
797                             const yaml::StringValue &Source,
798                             StringRef TypeString, MIRParserImpl &Parser) {
799   if (!Node)
800     return false;
801   Result = dyn_cast<T>(Node);
802   if (!Result)
803     return Parser.error(Source.SourceRange.Start,
804                         "expected a reference to a '" + TypeString +
805                             "' metadata node");
806   return false;
807 }
808 
809 template <typename T>
810 bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
811     const T &Object, int FrameIdx) {
812   // Debug information can only be attached to stack objects; Fixed stack
813   // objects aren't supported.
814   MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
815   if (parseMDNode(PFS, Var, Object.DebugVar) ||
816       parseMDNode(PFS, Expr, Object.DebugExpr) ||
817       parseMDNode(PFS, Loc, Object.DebugLoc))
818     return true;
819   if (!Var && !Expr && !Loc)
820     return false;
821   DILocalVariable *DIVar = nullptr;
822   DIExpression *DIExpr = nullptr;
823   DILocation *DILoc = nullptr;
824   if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
825       typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
826       typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
827     return true;
828   PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc);
829   return false;
830 }
831 
832 bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
833     MDNode *&Node, const yaml::StringValue &Source) {
834   if (Source.Value.empty())
835     return false;
836   SMDiagnostic Error;
837   if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
838     return error(Error, Source.SourceRange);
839   return false;
840 }
841 
842 bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
843     MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
844   DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
845   const MachineFunction &MF = PFS.MF;
846   const auto &M = *MF.getFunction().getParent();
847   SMDiagnostic Error;
848   for (const auto &YamlConstant : YamlMF.Constants) {
849     if (YamlConstant.IsTargetSpecific)
850       // FIXME: Support target-specific constant pools
851       return error(YamlConstant.Value.SourceRange.Start,
852                    "Can't parse target-specific constant pool entries yet");
853     const Constant *Value = dyn_cast_or_null<Constant>(
854         parseConstantValue(YamlConstant.Value.Value, Error, M));
855     if (!Value)
856       return error(Error, YamlConstant.Value.SourceRange);
857     const Align PrefTypeAlign =
858         M.getDataLayout().getPrefTypeAlign(Value->getType());
859     const Align Alignment = YamlConstant.Alignment.getValueOr(PrefTypeAlign);
860     unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
861     if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
862              .second)
863       return error(YamlConstant.ID.SourceRange.Start,
864                    Twine("redefinition of constant pool item '%const.") +
865                        Twine(YamlConstant.ID.Value) + "'");
866   }
867   return false;
868 }
869 
870 bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
871     const yaml::MachineJumpTable &YamlJTI) {
872   MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
873   for (const auto &Entry : YamlJTI.Entries) {
874     std::vector<MachineBasicBlock *> Blocks;
875     for (const auto &MBBSource : Entry.Blocks) {
876       MachineBasicBlock *MBB = nullptr;
877       if (parseMBBReference(PFS, MBB, MBBSource.Value))
878         return true;
879       Blocks.push_back(MBB);
880     }
881     unsigned Index = JTI->createJumpTableIndex(Blocks);
882     if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
883              .second)
884       return error(Entry.ID.SourceRange.Start,
885                    Twine("redefinition of jump table entry '%jump-table.") +
886                        Twine(Entry.ID.Value) + "'");
887   }
888   return false;
889 }
890 
891 bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
892                                       MachineBasicBlock *&MBB,
893                                       const yaml::StringValue &Source) {
894   SMDiagnostic Error;
895   if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
896     return error(Error, Source.SourceRange);
897   return false;
898 }
899 
900 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
901                                                  SMRange SourceRange) {
902   assert(SourceRange.isValid() && "Invalid source range");
903   SMLoc Loc = SourceRange.Start;
904   bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
905                   *Loc.getPointer() == '\'';
906   // Translate the location of the error from the location in the MI string to
907   // the corresponding location in the MIR file.
908   Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
909                            (HasQuote ? 1 : 0));
910 
911   // TODO: Translate any source ranges as well.
912   return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
913                        Error.getFixIts());
914 }
915 
916 SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
917                                                     SMRange SourceRange) {
918   assert(SourceRange.isValid());
919 
920   // Translate the location of the error from the location in the llvm IR string
921   // to the corresponding location in the MIR file.
922   auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
923   unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
924   unsigned Column = Error.getColumnNo();
925   StringRef LineStr = Error.getLineContents();
926   SMLoc Loc = Error.getLoc();
927 
928   // Get the full line and adjust the column number by taking the indentation of
929   // LLVM IR into account.
930   for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
931        L != E; ++L) {
932     if (L.line_number() == Line) {
933       LineStr = *L;
934       Loc = SMLoc::getFromPointer(LineStr.data());
935       auto Indent = LineStr.find(Error.getLineContents());
936       if (Indent != StringRef::npos)
937         Column += Indent;
938       break;
939     }
940   }
941 
942   return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
943                       Error.getMessage(), LineStr, Error.getRanges(),
944                       Error.getFixIts());
945 }
946 
947 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
948     : Impl(std::move(Impl)) {}
949 
950 MIRParser::~MIRParser() {}
951 
952 std::unique_ptr<Module>
953 MIRParser::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) {
954   return Impl->parseIRModule(DataLayoutCallback);
955 }
956 
957 bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
958   return Impl->parseMachineFunctions(M, MMI);
959 }
960 
961 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(
962     StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
963     std::function<void(Function &)> ProcessIRFunction) {
964   auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
965   if (std::error_code EC = FileOrErr.getError()) {
966     Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
967                          "Could not open input file: " + EC.message());
968     return nullptr;
969   }
970   return createMIRParser(std::move(FileOrErr.get()), Context,
971                          ProcessIRFunction);
972 }
973 
974 std::unique_ptr<MIRParser>
975 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
976                       LLVMContext &Context,
977                       std::function<void(Function &)> ProcessIRFunction) {
978   auto Filename = Contents->getBufferIdentifier();
979   if (Context.shouldDiscardValueNames()) {
980     Context.diagnose(DiagnosticInfoMIRParser(
981         DS_Error,
982         SMDiagnostic(
983             Filename, SourceMgr::DK_Error,
984             "Can't read MIR with a Context that discards named Values")));
985     return nullptr;
986   }
987   return std::make_unique<MIRParser>(std::make_unique<MIRParserImpl>(
988       std::move(Contents), Filename, Context, ProcessIRFunction));
989 }
990