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