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