blob: f955bdc6186a7c18d5fdfc65a3b283446f28fd02 [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00006//
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"
Alex Lorenz33f0aef2015-06-26 16:46:11 +000015#include "llvm/ADT/DenseMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000016#include "llvm/ADT/STLExtras.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000019#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000020#include "llvm/AsmParser/SlotMapping.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000021#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
22#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +000023#include "llvm/CodeGen/MIRParser/MIParser.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000024#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000027#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Sander de Smalen5d6ee762019-06-17 09:13:29 +000030#include "llvm/CodeGen/TargetFrameLowering.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000031#include "llvm/IR/BasicBlock.h"
Reid Kleckner28865802016-04-14 18:29:59 +000032#include "llvm/IR/DebugInfo.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000033#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000034#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000035#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000036#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000037#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000038#include "llvm/Support/LineIterator.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000039#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000040#include "llvm/Support/SMLoc.h"
41#include "llvm/Support/SourceMgr.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000042#include "llvm/Support/YAMLTraits.h"
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +000043#include "llvm/Target/TargetMachine.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000044#include <memory>
45
46using namespace llvm;
47
Alex Lorenz735c47e2015-06-15 20:30:22 +000048namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000049
50/// This class implements the parsing of LLVM IR that's embedded inside a MIR
51/// file.
52class MIRParserImpl {
53 SourceMgr SM;
Matthias Braun7bda1952017-06-06 00:44:35 +000054 yaml::Input In;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000055 StringRef Filename;
56 LLVMContext &Context;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000057 SlotMapping IRSlots;
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +000058 std::unique_ptr<PerTargetMIParsingState> Target;
59
Matthias Braun7bda1952017-06-06 00:44:35 +000060 /// 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;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000066
Matt Arsenault5518a022019-12-07 00:06:34 +053067 std::function<void(Function &)> ProcessIRFunction;
68
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000069public:
Matt Arsenault5518a022019-12-07 00:06:34 +053070 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
71 LLVMContext &Context,
72 std::function<void(Function &)> ProcessIRFunction);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000073
Alex Lorenz735c47e2015-06-15 20:30:22 +000074 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
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000081 /// 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
Alex Lorenz0fd7c622015-06-30 17:55:00 +000086 /// 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
Alex Lorenz78d78312015-05-28 22:41:12 +000092 /// Try to parse the optional LLVM module and the machine functions in the MIR
93 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000094 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000095 /// Return null if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +000096 std::unique_ptr<Module> parseIRModule();
97
Matt Arsenault5518a022019-12-07 00:06:34 +053098 /// Create an empty function with the given name.
99 Function *createDummyFunction(StringRef Name, Module &M);
100
Matthias Braun7bda1952017-06-06 00:44:35 +0000101 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
Alex Lorenz78d78312015-05-28 22:41:12 +0000102
103 /// Parse the machine function in the current YAML document.
104 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000105 ///
Alex Lorenz78d78312015-05-28 22:41:12 +0000106 /// Return true if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000107 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000108
Alex Lorenz735c47e2015-06-15 20:30:22 +0000109 /// Initialize the machine function to the state that's described in the MIR
110 /// file.
111 ///
112 /// Return true if error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000113 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
114 MachineFunction &MF);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000115
Matthias Braun74ad41c2016-10-11 03:13:01 +0000116 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
117 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000118
Matthias Braun74ad41c2016-10-11 03:13:01 +0000119 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000120 const yaml::MachineFunction &YamlMF);
121
Matthias Braun83947862016-07-13 22:23:23 +0000122 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
123 const yaml::MachineFunction &YamlMF);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000124
Djordje Todorovica7cde102019-06-27 07:48:06 +0000125 bool initializeCallSiteInfo(PerFunctionMIParsingState &PFS,
126 const yaml::MachineFunction &YamlMF);
127
Matthias Braun83947862016-07-13 22:23:23 +0000128 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000129 std::vector<CalleeSavedInfo> &CSIInfo,
130 const yaml::StringValue &RegisterSource,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000131 bool IsRestored, int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000132
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000133 template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000134 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000135 const T &Object,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000136 int FrameIdx);
137
Matthias Braun83947862016-07-13 22:23:23 +0000138 bool initializeConstantPool(PerFunctionMIParsingState &PFS,
139 MachineConstantPool &ConstantPool,
140 const yaml::MachineFunction &YamlMF);
Alex Lorenzab980492015-07-20 20:51:18 +0000141
Matthias Braun83947862016-07-13 22:23:23 +0000142 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
143 const yaml::MachineJumpTable &YamlJTI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000144
Alex Lorenz09b832c2015-05-29 17:05:41 +0000145private:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000146 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
Matthias Braun83947862016-07-13 22:23:23 +0000147 const yaml::StringValue &Source);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000148
Matthias Braun74ad41c2016-10-11 03:13:01 +0000149 bool parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000150 MachineBasicBlock *&MBB,
151 const yaml::StringValue &Source);
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000152
Alex Lorenz51af1602015-06-23 22:39:23 +0000153 /// Return a MIR diagnostic converted from an MI string diagnostic.
154 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
155 SMRange SourceRange);
156
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000157 /// 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);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000161
Matthias Braun90799ce2016-08-23 21:19:49 +0000162 void computeFunctionProperties(MachineFunction &MF);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000163};
164
Alex Lorenz735c47e2015-06-15 20:30:22 +0000165} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000166
Matthias Braun7bda1952017-06-06 00:44:35 +0000167static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
168 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
169}
170
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000171MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
Matt Arsenault5518a022019-12-07 00:06:34 +0530172 StringRef Filename, LLVMContext &Context,
173 std::function<void(Function &)> Callback)
Matthias Braun7bda1952017-06-06 00:44:35 +0000174 : SM(),
Matt Arsenault5518a022019-12-07 00:06:34 +0530175 In(SM.getMemoryBuffer(SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))
176 ->getBuffer(),
177 nullptr, handleYAMLDiag, this),
178 Filename(Filename), Context(Context), ProcessIRFunction(Callback) {
Matthias Braun7bda1952017-06-06 00:44:35 +0000179 In.setContext(&In);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000180}
181
Alex Lorenz735c47e2015-06-15 20:30:22 +0000182bool MIRParserImpl::error(const Twine &Message) {
183 Context.diagnose(DiagnosticInfoMIRParser(
184 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
185 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000186}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000187
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000188bool 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
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000194bool 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
Alex Lorenz735c47e2015-06-15 20:30:22 +0000200void 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;
Adam Nemet01104ae2017-10-12 23:56:02 +0000212 case SourceMgr::DK_Remark:
213 llvm_unreachable("remark unexpected");
214 break;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000215 }
216 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
217}
218
Matthias Braun7bda1952017-06-06 00:44:35 +0000219std::unique_ptr<Module> MIRParserImpl::parseIRModule() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000220 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000221 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000222 return nullptr;
223 // Create an empty module when the MIR file is empty.
Matthias Braun7bda1952017-06-06 00:44:35 +0000224 NoMIRDocuments = true;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000225 return std::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000226 }
227
Alex Lorenz78d78312015-05-28 22:41:12 +0000228 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())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000233 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000234 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000235 Context, &IRSlots, /*UpgradeDebugInfo=*/false);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000236 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000237 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000238 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000239 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000240 In.nextDocument();
241 if (!In.setCurrentDocument())
Matthias Braun7bda1952017-06-06 00:44:35 +0000242 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000243 } else {
244 // Create an new, empty module.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000245 M = std::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000246 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000247 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000248 return M;
249}
250
Matthias Braun7bda1952017-06-06 00:44:35 +0000251bool 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
Alex Lorenz735c47e2015-06-15 20:30:22 +0000262 return false;
263}
264
Matt Arsenault5518a022019-12-07 00:06:34 +0530265Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000266 auto &Context = M.getContext();
James Y Knight13680222019-02-01 02:28:03 +0000267 Function *F =
268 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
269 Function::ExternalLinkage, Name, M);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000270 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
271 new UnreachableInst(Context, BB);
Matt Arsenault5518a022019-12-07 00:06:34 +0530272
273 if (ProcessIRFunction)
274 ProcessIRFunction(*F);
275
Matthias Braun7bda1952017-06-06 00:44:35 +0000276 return F;
277}
278
279bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
280 // Parse the yaml.
281 yaml::MachineFunction YamlMF;
282 yaml::EmptyContext Ctx;
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +0000283
284 const LLVMTargetMachine &TM = MMI.getTarget();
285 YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
286 TM.createDefaultFuncInfoYAML());
287
Matthias Braun7bda1952017-06-06 00:44:35 +0000288 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;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000313}
314
Matthias Braun79f85b32016-08-24 01:32:41 +0000315static bool isSSA(const MachineFunction &MF) {
316 const MachineRegisterInfo &MRI = MF.getRegInfo();
317 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000318 unsigned Reg = Register::index2VirtReg(I);
Matthias Braun79f85b32016-08-24 01:32:41 +0000319 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
320 return false;
321 }
322 return true;
323}
324
Matthias Braun90799ce2016-08-23 21:19:49 +0000325void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000326 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000327
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)
Matthias Braun79f85b32016-08-24 01:32:41 +0000339 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000340 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000341
342 if (isSSA(MF))
343 Properties.set(MachineFunctionProperties::Property::IsSSA);
344 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000345 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000346
347 const MachineRegisterInfo &MRI = MF.getRegInfo();
348 if (MRI.getNumVirtRegs() == 0)
349 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000350}
351
Djordje Todorovica7cde102019-06-27 07:48:06 +0000352bool 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.") +
David Stenbergb71d8d42019-09-20 14:41:41 +0000367 " Unable to reference instruction at bb: " +
Djordje Todorovica7cde102019-06-27 07:48:06 +0000368 Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset));
David Stenberg88df53e2019-08-19 12:41:22 +0000369 auto CallI = std::next(CallB->instr_begin(), MILoc.Offset);
370 if (!CallI->isCall(MachineInstr::IgnoreBundle))
Djordje Todorovica7cde102019-06-27 07:48:06 +0000371 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
Djordje Todorovicc15c68a2020-03-09 11:02:35 +0100384 if (TM.Options.EmitCallSiteInfo)
Djordje Todorovica7cde102019-06-27 07:48:06 +0000385 MF.addCallArgsForwardingRegs(&*CallI, std::move(CSInfo));
386 }
387
Djordje Todorovicc15c68a2020-03-09 11:02:35 +0100388 if (YamlMF.CallSitesInfo.size() && !TM.Options.EmitCallSiteInfo)
Djordje Todorovica7cde102019-06-27 07:48:06 +0000389 return error(Twine("Call site info provided but not used"));
390 return false;
391}
392
Matthias Braun7bda1952017-06-06 00:44:35 +0000393bool
394MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
395 MachineFunction &MF) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000396 // TODO: Recreate the machine function.
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000397 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
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000404 if (YamlMF.Alignment)
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000405 MF.setAlignment(Align(YamlMF.Alignment));
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000406 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Sanjin Sijaric625d08e2018-10-24 21:07:38 +0000407 MF.setHasWinCFI(YamlMF.HasWinCFI);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000408
409 if (YamlMF.Legalized)
410 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000411 if (YamlMF.RegBankSelected)
412 MF.getProperties().set(
413 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000414 if (YamlMF.Selected)
415 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Roman Tereshin3054ece2018-02-28 17:55:45 +0000416 if (YamlMF.FailedISel)
417 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000418
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000419 PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000420 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000421 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000422 if (!YamlMF.Constants.empty()) {
423 auto *ConstantPool = MF.getConstantPool();
424 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000425 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000426 return true;
427 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000428
Matthias Braune35861d2016-07-13 23:27:50 +0000429 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000430 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000431 SourceMgr BlockSM;
432 BlockSM.AddNewSourceBuffer(
433 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
434 SMLoc());
435 PFS.SM = &BlockSM;
436 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000437 reportDiagnostic(
438 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
439 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000440 }
Matthias Braune35861d2016-07-13 23:27:50 +0000441 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000442
Alex Lorenza6f9a372015-07-29 21:09:09 +0000443 // Initialize the frame information after creating all the MBBs so that the
444 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000445 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000446 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000447 // Initialize the jump table after creating all the MBBs so that the MBB
448 // references can be resolved.
449 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000450 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000451 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000452 // Parse the machine instructions after creating all of the MBBs so that the
453 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000454 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)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000461 reportDiagnostic(
462 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
463 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000464 }
Matthias Braune35861d2016-07-13 23:27:50 +0000465 PFS.SM = &SM;
466
Matthias Braun74ad41c2016-10-11 03:13:01 +0000467 if (setupRegisterInfo(PFS, YamlMF))
468 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000469
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +0000470 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
Matt Arsenault733b8572019-03-27 16:12:26 +0000483 // 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
Matthias Braun90799ce2016-08-23 21:19:49 +0000491 computeFunctionProperties(MF);
492
Djordje Todorovica7cde102019-06-27 07:48:06 +0000493 if (initializeCallSiteInfo(PFS, YamlMF))
494 return false;
495
Matthias Braun5c290dc2018-01-19 03:16:36 +0000496 MF.getSubtarget().mirFileLoaded(MF);
497
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000498 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000499 return false;
500}
501
Matthias Braun74ad41c2016-10-11 03:13:01 +0000502bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
503 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000504 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000505 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000506 assert(RegInfo.tracksLiveness());
507 if (!YamlMF.TracksRegLiveness)
508 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000509
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000510 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000511 // Parse the virtual register information.
512 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000513 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
Quentin Colombet050b2112016-03-08 01:17:03 +0000520 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000521 Info.Kind = VRegInfo::GENERIC;
Justin Bogner6c7663f2017-11-17 18:51:20 +0000522 Info.D.RegBank = nullptr;
Quentin Colombet050b2112016-03-08 01:17:03 +0000523 } else {
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000524 const auto *RC = Target->getRegClass(VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000525 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000526 Info.Kind = VRegInfo::NORMAL;
527 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000528 } else {
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000529 const RegisterBank *RegBank = Target->getRegBank(VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000530 if (!RegBank)
531 return error(
532 VReg.Class.SourceRange.Start,
533 Twine("use of undefined register class or register bank '") +
534 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000535 Info.Kind = VRegInfo::REGBANK;
536 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000537 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000538 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000539
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000540 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000541 if (Info.Kind != VRegInfo::NORMAL)
542 return error(VReg.Class.SourceRange.Start,
543 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000544
545 if (parseRegisterReference(PFS, Info.PreferredReg,
546 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000547 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000548 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000549 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000550
551 // Parse the liveins.
552 for (const auto &LiveIn : YamlMF.LiveIns) {
553 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000554 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000555 return error(Error, LiveIn.Register.SourceRange);
556 unsigned VReg = 0;
557 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000558 VRegInfo *Info;
559 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000560 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000561 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000562 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000563 }
564 RegInfo.addLiveIn(Reg, VReg);
565 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000566
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000567 // 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);
Alex Lorenzc4838082015-08-11 00:32:49 +0000578 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000579
Alex Lorenz54565cf2015-06-24 19:56:10 +0000580 return false;
581}
582
Matthias Braun74ad41c2016-10-11 03:13:01 +0000583bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000584 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000585 MachineFunction &MF = PFS.MF;
586 MachineRegisterInfo &MRI = MF.getRegInfo();
587 bool Error = false;
588 // Create VRegs
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000589 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000590 unsigned Reg = Info.VReg;
591 switch (Info.Kind) {
592 case VRegInfo::UNKNOWN:
593 error(Twine("Cannot determine class/bank of virtual register ") +
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000594 Name + " in function '" + MF.getName() + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000595 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 }
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000608 };
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));
Matthias Braun74ad41c2016-10-11 03:13:01 +0000619 }
620
621 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000622 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());
Alex Lorenzc4838082015-08-11 00:32:49 +0000628 }
629 }
630 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000631
Matthias Braun74ad41c2016-10-11 03:13:01 +0000632 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000633}
634
Matthias Braun83947862016-07-13 22:23:23 +0000635bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
636 const yaml::MachineFunction &YamlMF) {
637 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000638 MachineFrameInfo &MFI = MF.getFrameInfo();
Sander de Smalen5d6ee762019-06-17 09:13:29 +0000639 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
Matthias Braunf1caa282017-12-15 22:22:58 +0000640 const Function &F = MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000641 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000642 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);
Matthias Braunab9438c2017-05-01 22:32:25 +0000652 if (YamlMFI.MaxCallFrameSize != ~0u)
653 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Reid Kleckner9ea2c012018-10-01 21:59:45 +0000654 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
Alex Lorenz60541c12015-07-09 19:55:27 +0000655 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
656 MFI.setHasVAStart(YamlMFI.HasVAStart);
657 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Francis Visoiu Mistrih537d7ee2018-04-06 08:56:25 +0000658 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000659 if (!YamlMFI.SavePoint.Value.empty()) {
660 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000661 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000662 return true;
663 MFI.setSavePoint(MBB);
664 }
665 if (!YamlMFI.RestorePoint.Value.empty()) {
666 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000667 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000668 return true;
669 MFI.setRestorePoint(MBB);
670 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000671
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000672 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000673 // 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);
Sander de Smalen5d6ee762019-06-17 09:13:29 +0000681
682 if (!TFI->isSupportedStackID(Object.StackID))
683 return error(Object.ID.SourceRange.Start,
684 Twine("StackID is not supported by target"));
Matt Arsenaultdb782732017-07-20 21:03:45 +0000685 MFI.setStackID(ObjectIdx, Object.StackID);
Sander de Smalen7f23e0a2019-04-02 09:46:52 +0000686 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000687 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) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000693 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000694 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000695 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000696 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
697 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000698 }
699
700 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000701 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000702 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000703 const AllocaInst *Alloca = nullptr;
704 const yaml::StringValue &Name = Object.Name;
705 if (!Name.Value.empty()) {
706 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000707 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000708 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 }
Sander de Smalen5d6ee762019-06-17 09:13:29 +0000714 if (!TFI->isSupportedStackID(Object.StackID))
715 return error(Object.ID.SourceRange.Start,
716 Twine("StackID is not supported by target"));
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000717 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000718 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000719 else
720 ObjectIdx = MFI.CreateStackObject(
721 Object.Size, Object.Alignment,
Sander de Smalen7f23e0a2019-04-02 09:46:52 +0000722 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca,
723 Object.StackID);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000724 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000725
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000726 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) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000731 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000732 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000733 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000734 if (Object.LocalOffset)
735 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000736 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000737 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000738 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000739 MFI.setCalleeSavedInfo(CSIInfo);
740 if (!CSIInfo.empty())
741 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000742
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;
Matthias Braune35861d2016-07-13 23:27:50 +0000748 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000749 return error(Error, YamlMFI.StackProtector.SourceRange);
750 MFI.setStackProtectorIndex(FI);
751 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000752 return false;
753}
754
Matthias Braun83947862016-07-13 22:23:23 +0000755bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000756 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000757 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000758 if (RegisterSource.Value.empty())
759 return false;
760 unsigned Reg = 0;
761 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000762 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000763 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000764 CalleeSavedInfo CSI(Reg, FrameIdx);
765 CSI.setRestored(IsRestored);
766 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000767 return false;
768}
769
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000770/// Verify that given node is of a certain type. Return true on error.
771template <typename T>
772static 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
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000785template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000786bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000787 const T &Object, int FrameIdx) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000788 // Debug information can only be attached to stack objects; Fixed stack
789 // objects aren't supported.
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000790 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000791 if (parseMDNode(PFS, Var, Object.DebugVar) ||
792 parseMDNode(PFS, Expr, Object.DebugExpr) ||
793 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000794 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;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000804 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000805 return false;
806}
807
Matthias Braun74ad41c2016-10-11 03:13:01 +0000808bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000809 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000810 if (Source.Value.empty())
811 return false;
812 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000813 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000814 return error(Error, Source.SourceRange);
815 return false;
816}
817
Matthias Braun83947862016-07-13 22:23:23 +0000818bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
819 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
820 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
821 const MachineFunction &MF = PFS.MF;
Matthias Braunf1caa282017-12-15 22:22:58 +0000822 const auto &M = *MF.getFunction().getParent();
Alex Lorenzab980492015-07-20 20:51:18 +0000823 SMDiagnostic Error;
824 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000825 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");
Alex Lorenzab980492015-07-20 20:51:18 +0000829 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());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000837 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) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000843 }
844 return false;
845}
846
Matthias Braun83947862016-07-13 22:23:23 +0000847bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
848 const yaml::MachineJumpTable &YamlJTI) {
849 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000850 for (const auto &Entry : YamlJTI.Entries) {
851 std::vector<MachineBasicBlock *> Blocks;
852 for (const auto &MBBSource : Entry.Blocks) {
853 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000854 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000855 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000856 Blocks.push_back(MBB);
857 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000858 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000859 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) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000864 }
865 return false;
866}
867
Matthias Braun74ad41c2016-10-11 03:13:01 +0000868bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000869 MachineBasicBlock *&MBB,
870 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000871 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000872 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000873 return error(Error, Source.SourceRange);
874 return false;
875}
876
Alex Lorenz51af1602015-06-23 22:39:23 +0000877SMDiagnostic 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
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000893SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
894 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000895 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
Alex Lorenz735c47e2015-06-15 20:30:22 +0000924MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
925 : Impl(std::move(Impl)) {}
926
927MIRParser::~MIRParser() {}
928
Matthias Braun7bda1952017-06-06 00:44:35 +0000929std::unique_ptr<Module> MIRParser::parseIRModule() {
930 return Impl->parseIRModule();
931}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000932
Matthias Braun7bda1952017-06-06 00:44:35 +0000933bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
934 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000935}
936
Matt Arsenault5518a022019-12-07 00:06:34 +0530937std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(
938 StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
939 std::function<void(Function &)> ProcessIRFunction) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000940 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000941 if (std::error_code EC = FileOrErr.getError()) {
942 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
943 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000944 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000945 }
Matt Arsenault5518a022019-12-07 00:06:34 +0530946 return createMIRParser(std::move(FileOrErr.get()), Context,
947 ProcessIRFunction);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000948}
949
Alex Lorenz735c47e2015-06-15 20:30:22 +0000950std::unique_ptr<MIRParser>
951llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
Matt Arsenault5518a022019-12-07 00:06:34 +0530952 LLVMContext &Context,
953 std::function<void(Function &)> ProcessIRFunction) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000954 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000955 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 }
Matt Arsenault5518a022019-12-07 00:06:34 +0530963 return std::make_unique<MIRParser>(std::make_unique<MIRParserImpl>(
964 std::move(Contents), Filename, Context, ProcessIRFunction));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000965}