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