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