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