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