blob: b7e2957ded09916251e3fc6552a4a81f8c32c15e [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 Lorenz8e0a1b42015-06-22 17:02:30 +000015#include "MIParser.h"
Alex Lorenz33f0aef2015-06-26 16:46:11 +000016#include "llvm/ADT/DenseMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000017#include "llvm/ADT/STLExtras.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000020#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000021#include "llvm/AsmParser/SlotMapping.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000022#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
23#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
24#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"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000030#include "llvm/IR/BasicBlock.h"
Reid Kleckner28865802016-04-14 18:29:59 +000031#include "llvm/IR/DebugInfo.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000032#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000033#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000034#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000035#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000036#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000037#include "llvm/Support/LineIterator.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000038#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000039#include "llvm/Support/SMLoc.h"
40#include "llvm/Support/SourceMgr.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000041#include "llvm/Support/YAMLTraits.h"
42#include <memory>
43
44using namespace llvm;
45
Alex Lorenz735c47e2015-06-15 20:30:22 +000046namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000047
48/// This class implements the parsing of LLVM IR that's embedded inside a MIR
49/// file.
50class MIRParserImpl {
51 SourceMgr SM;
Matthias Braun7bda1952017-06-06 00:44:35 +000052 yaml::Input In;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000053 StringRef Filename;
54 LLVMContext &Context;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000055 SlotMapping IRSlots;
Alex Lorenz28148ba2015-07-09 22:23:13 +000056 /// Maps from register class names to register classes.
Matthias Braunde5fea22017-01-18 00:59:19 +000057 Name2RegClassMap Names2RegClasses;
Quentin Colombet876ddf82016-04-08 16:40:43 +000058 /// Maps from register bank names to register banks.
Matthias Braunde5fea22017-01-18 00:59:19 +000059 Name2RegBankMap Names2RegBanks;
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
67public:
Matthias Braun7bda1952017-06-06 00:44:35 +000068 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
69 StringRef Filename, LLVMContext &Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000070
Alex Lorenz735c47e2015-06-15 20:30:22 +000071 void reportDiagnostic(const SMDiagnostic &Diag);
72
73 /// Report an error with the given message at unknown location.
74 ///
75 /// Always returns true.
76 bool error(const Twine &Message);
77
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000078 /// Report an error with the given message at the given location.
79 ///
80 /// Always returns true.
81 bool error(SMLoc Loc, const Twine &Message);
82
Alex Lorenz0fd7c622015-06-30 17:55:00 +000083 /// Report a given error with the location translated from the location in an
84 /// embedded string literal to a location in the MIR file.
85 ///
86 /// Always returns true.
87 bool error(const SMDiagnostic &Error, SMRange SourceRange);
88
Alex Lorenz78d78312015-05-28 22:41:12 +000089 /// Try to parse the optional LLVM module and the machine functions in the MIR
90 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000091 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000092 /// Return null if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +000093 std::unique_ptr<Module> parseIRModule();
94
95 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
Alex Lorenz78d78312015-05-28 22:41:12 +000096
97 /// Parse the machine function in the current YAML document.
98 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000099 ///
Alex Lorenz78d78312015-05-28 22:41:12 +0000100 /// Return true if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000101 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000102
Alex Lorenz735c47e2015-06-15 20:30:22 +0000103 /// Initialize the machine function to the state that's described in the MIR
104 /// file.
105 ///
106 /// Return true if error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000107 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
108 MachineFunction &MF);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000109
Matthias Braun74ad41c2016-10-11 03:13:01 +0000110 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
111 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000112
Matthias Braun74ad41c2016-10-11 03:13:01 +0000113 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000114 const yaml::MachineFunction &YamlMF);
115
Matthias Braun83947862016-07-13 22:23:23 +0000116 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
117 const yaml::MachineFunction &YamlMF);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000118
Matthias Braun83947862016-07-13 22:23:23 +0000119 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000120 std::vector<CalleeSavedInfo> &CSIInfo,
121 const yaml::StringValue &RegisterSource,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000122 bool IsRestored, int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000123
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000124 template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000125 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000126 const T &Object,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000127 int FrameIdx);
128
Matthias Braun83947862016-07-13 22:23:23 +0000129 bool initializeConstantPool(PerFunctionMIParsingState &PFS,
130 MachineConstantPool &ConstantPool,
131 const yaml::MachineFunction &YamlMF);
Alex Lorenzab980492015-07-20 20:51:18 +0000132
Matthias Braun83947862016-07-13 22:23:23 +0000133 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
134 const yaml::MachineJumpTable &YamlJTI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000135
Alex Lorenz09b832c2015-05-29 17:05:41 +0000136private:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000137 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
Matthias Braun83947862016-07-13 22:23:23 +0000138 const yaml::StringValue &Source);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000139
Matthias Braun74ad41c2016-10-11 03:13:01 +0000140 bool parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000141 MachineBasicBlock *&MBB,
142 const yaml::StringValue &Source);
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000143
Alex Lorenz51af1602015-06-23 22:39:23 +0000144 /// Return a MIR diagnostic converted from an MI string diagnostic.
145 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
146 SMRange SourceRange);
147
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000148 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
149 /// block scalar string.
150 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
151 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000152
Alex Lorenz28148ba2015-07-09 22:23:13 +0000153 void initNames2RegClasses(const MachineFunction &MF);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000154 void initNames2RegBanks(const MachineFunction &MF);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000155
156 /// Check if the given identifier is a name of a register class.
157 ///
158 /// Return null if the name isn't a register class.
159 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
160 StringRef Name);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000161
162 /// Check if the given identifier is a name of a register bank.
163 ///
164 /// Return null if the name isn't a register bank.
165 const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name);
Matthias Braun90799ce2016-08-23 21:19:49 +0000166
167 void computeFunctionProperties(MachineFunction &MF);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000168};
169
Alex Lorenz735c47e2015-06-15 20:30:22 +0000170} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000171
Matthias Braun7bda1952017-06-06 00:44:35 +0000172static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
173 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
174}
175
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000176MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
177 StringRef Filename, LLVMContext &Context)
Matthias Braun7bda1952017-06-06 00:44:35 +0000178 : SM(),
179 In(SM.getMemoryBuffer(
180 SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))->getBuffer(),
181 nullptr, handleYAMLDiag, this),
182 Filename(Filename),
183 Context(Context) {
184 In.setContext(&In);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000185}
186
Alex Lorenz735c47e2015-06-15 20:30:22 +0000187bool MIRParserImpl::error(const Twine &Message) {
188 Context.diagnose(DiagnosticInfoMIRParser(
189 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
190 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000191}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000192
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000193bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
194 Context.diagnose(DiagnosticInfoMIRParser(
195 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
196 return true;
197}
198
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000199bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
200 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
201 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
202 return true;
203}
204
Alex Lorenz735c47e2015-06-15 20:30:22 +0000205void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
206 DiagnosticSeverity Kind;
207 switch (Diag.getKind()) {
208 case SourceMgr::DK_Error:
209 Kind = DS_Error;
210 break;
211 case SourceMgr::DK_Warning:
212 Kind = DS_Warning;
213 break;
214 case SourceMgr::DK_Note:
215 Kind = DS_Note;
216 break;
Adam Nemet01104ae2017-10-12 23:56:02 +0000217 case SourceMgr::DK_Remark:
218 llvm_unreachable("remark unexpected");
219 break;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000220 }
221 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
222}
223
Matthias Braun7bda1952017-06-06 00:44:35 +0000224std::unique_ptr<Module> MIRParserImpl::parseIRModule() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000225 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000226 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000227 return nullptr;
228 // Create an empty module when the MIR file is empty.
Matthias Braun7bda1952017-06-06 00:44:35 +0000229 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000230 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000231 }
232
Alex Lorenz78d78312015-05-28 22:41:12 +0000233 std::unique_ptr<Module> M;
234 // Parse the block scalar manually so that we can return unique pointer
235 // without having to go trough YAML traits.
236 if (const auto *BSN =
237 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000238 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000239 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000240 Context, &IRSlots, /*UpgradeDebugInfo=*/false);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000241 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000242 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000243 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000244 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000245 In.nextDocument();
246 if (!In.setCurrentDocument())
Matthias Braun7bda1952017-06-06 00:44:35 +0000247 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000248 } else {
249 // Create an new, empty module.
250 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000251 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000252 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000253 return M;
254}
255
Matthias Braun7bda1952017-06-06 00:44:35 +0000256bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
257 if (NoMIRDocuments)
258 return false;
259
260 // Parse the machine functions.
261 do {
262 if (parseMachineFunction(M, MMI))
263 return true;
264 In.nextDocument();
265 } while (In.setCurrentDocument());
266
Alex Lorenz735c47e2015-06-15 20:30:22 +0000267 return false;
268}
269
Matthias Braun7bda1952017-06-06 00:44:35 +0000270/// Create an empty function with the given name.
271static Function *createDummyFunction(StringRef Name, Module &M) {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000272 auto &Context = M.getContext();
273 Function *F = cast<Function>(M.getOrInsertFunction(
274 Name, FunctionType::get(Type::getVoidTy(Context), false)));
275 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
276 new UnreachableInst(Context, BB);
Matthias Braun7bda1952017-06-06 00:44:35 +0000277 return F;
278}
279
280bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
281 // Parse the yaml.
282 yaml::MachineFunction YamlMF;
283 yaml::EmptyContext Ctx;
284 yaml::yamlize(In, YamlMF, false, Ctx);
285 if (In.error())
286 return true;
287
288 // Search for the corresponding IR function.
289 StringRef FunctionName = YamlMF.Name;
290 Function *F = M.getFunction(FunctionName);
291 if (!F) {
292 if (NoLLVMIR) {
293 F = createDummyFunction(FunctionName, M);
294 } else {
295 return error(Twine("function '") + FunctionName +
296 "' isn't defined in the provided LLVM IR");
297 }
298 }
299 if (MMI.getMachineFunction(*F) != nullptr)
300 return error(Twine("redefinition of machine function '") + FunctionName +
301 "'");
302
303 // Create the MachineFunction.
304 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
305 if (initializeMachineFunction(YamlMF, MF))
306 return true;
307
308 return false;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000309}
310
Matthias Braun79f85b32016-08-24 01:32:41 +0000311static bool isSSA(const MachineFunction &MF) {
312 const MachineRegisterInfo &MRI = MF.getRegInfo();
313 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
314 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
315 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
316 return false;
317 }
318 return true;
319}
320
Matthias Braun90799ce2016-08-23 21:19:49 +0000321void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000322 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000323
324 bool HasPHI = false;
325 bool HasInlineAsm = false;
326 for (const MachineBasicBlock &MBB : MF) {
327 for (const MachineInstr &MI : MBB) {
328 if (MI.isPHI())
329 HasPHI = true;
330 if (MI.isInlineAsm())
331 HasInlineAsm = true;
332 }
333 }
334 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000335 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000336 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000337
338 if (isSSA(MF))
339 Properties.set(MachineFunctionProperties::Property::IsSSA);
340 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000341 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000342
343 const MachineRegisterInfo &MRI = MF.getRegInfo();
344 if (MRI.getNumVirtRegs() == 0)
345 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000346}
347
Matthias Braun7bda1952017-06-06 00:44:35 +0000348bool
349MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
350 MachineFunction &MF) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000351 // TODO: Recreate the machine function.
Matthias Braunde5fea22017-01-18 00:59:19 +0000352 initNames2RegClasses(MF);
353 initNames2RegBanks(MF);
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000354 if (YamlMF.Alignment)
355 MF.setAlignment(YamlMF.Alignment);
356 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Sanjin Sijaric625d08e2018-10-24 21:07:38 +0000357 MF.setHasWinCFI(YamlMF.HasWinCFI);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000358
359 if (YamlMF.Legalized)
360 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000361 if (YamlMF.RegBankSelected)
362 MF.getProperties().set(
363 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000364 if (YamlMF.Selected)
365 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Roman Tereshin3054ece2018-02-28 17:55:45 +0000366 if (YamlMF.FailedISel)
367 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000368
Matthias Braunde5fea22017-01-18 00:59:19 +0000369 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
370 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000371 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000372 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000373 if (!YamlMF.Constants.empty()) {
374 auto *ConstantPool = MF.getConstantPool();
375 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000376 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000377 return true;
378 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000379
Matthias Braune35861d2016-07-13 23:27:50 +0000380 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000381 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000382 SourceMgr BlockSM;
383 BlockSM.AddNewSourceBuffer(
384 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
385 SMLoc());
386 PFS.SM = &BlockSM;
387 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000388 reportDiagnostic(
389 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
390 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000391 }
Matthias Braune35861d2016-07-13 23:27:50 +0000392 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000393
Alex Lorenza6f9a372015-07-29 21:09:09 +0000394 // Initialize the frame information after creating all the MBBs so that the
395 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000396 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000397 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000398 // Initialize the jump table after creating all the MBBs so that the MBB
399 // references can be resolved.
400 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000401 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000402 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000403 // Parse the machine instructions after creating all of the MBBs so that the
404 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000405 StringRef InsnStr = YamlMF.Body.Value.Value;
406 SourceMgr InsnSM;
407 InsnSM.AddNewSourceBuffer(
408 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
409 SMLoc());
410 PFS.SM = &InsnSM;
411 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000412 reportDiagnostic(
413 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
414 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000415 }
Matthias Braune35861d2016-07-13 23:27:50 +0000416 PFS.SM = &SM;
417
Matthias Braun74ad41c2016-10-11 03:13:01 +0000418 if (setupRegisterInfo(PFS, YamlMF))
419 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000420
421 computeFunctionProperties(MF);
422
Matthias Braun5c290dc2018-01-19 03:16:36 +0000423 MF.getSubtarget().mirFileLoaded(MF);
424
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000425 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000426 return false;
427}
428
Matthias Braun74ad41c2016-10-11 03:13:01 +0000429bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
430 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000431 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000432 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000433 assert(RegInfo.tracksLiveness());
434 if (!YamlMF.TracksRegLiveness)
435 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000436
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000437 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000438 // Parse the virtual register information.
439 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000440 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
441 if (Info.Explicit)
442 return error(VReg.ID.SourceRange.Start,
443 Twine("redefinition of virtual register '%") +
444 Twine(VReg.ID.Value) + "'");
445 Info.Explicit = true;
446
Quentin Colombet050b2112016-03-08 01:17:03 +0000447 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000448 Info.Kind = VRegInfo::GENERIC;
Justin Bogner6c7663f2017-11-17 18:51:20 +0000449 Info.D.RegBank = nullptr;
Quentin Colombet050b2112016-03-08 01:17:03 +0000450 } else {
451 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000452 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000453 Info.Kind = VRegInfo::NORMAL;
454 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000455 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000456 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000457 if (!RegBank)
458 return error(
459 VReg.Class.SourceRange.Start,
460 Twine("use of undefined register class or register bank '") +
461 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000462 Info.Kind = VRegInfo::REGBANK;
463 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000464 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000465 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000466
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000467 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000468 if (Info.Kind != VRegInfo::NORMAL)
469 return error(VReg.Class.SourceRange.Start,
470 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000471
472 if (parseRegisterReference(PFS, Info.PreferredReg,
473 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000474 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000475 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000476 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000477
478 // Parse the liveins.
479 for (const auto &LiveIn : YamlMF.LiveIns) {
480 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000481 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000482 return error(Error, LiveIn.Register.SourceRange);
483 unsigned VReg = 0;
484 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000485 VRegInfo *Info;
486 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000487 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000488 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000489 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000490 }
491 RegInfo.addLiveIn(Reg, VReg);
492 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000493
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000494 // Parse the callee saved registers (Registers that will
495 // be saved for the caller).
496 if (YamlMF.CalleeSavedRegisters) {
497 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
498 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
499 unsigned Reg = 0;
500 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
501 return error(Error, RegSource.SourceRange);
502 CalleeSavedRegisters.push_back(Reg);
503 }
504 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000505 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000506
Alex Lorenz54565cf2015-06-24 19:56:10 +0000507 return false;
508}
509
Matthias Braun74ad41c2016-10-11 03:13:01 +0000510bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000511 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000512 MachineFunction &MF = PFS.MF;
513 MachineRegisterInfo &MRI = MF.getRegInfo();
514 bool Error = false;
515 // Create VRegs
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000516 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000517 unsigned Reg = Info.VReg;
518 switch (Info.Kind) {
519 case VRegInfo::UNKNOWN:
520 error(Twine("Cannot determine class/bank of virtual register ") +
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000521 Name + " in function '" + MF.getName() + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000522 Error = true;
523 break;
524 case VRegInfo::NORMAL:
525 MRI.setRegClass(Reg, Info.D.RC);
526 if (Info.PreferredReg != 0)
527 MRI.setSimpleHint(Reg, Info.PreferredReg);
528 break;
529 case VRegInfo::GENERIC:
530 break;
531 case VRegInfo::REGBANK:
532 MRI.setRegBank(Reg, *Info.D.RegBank);
533 break;
534 }
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000535 };
536
537 for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end();
538 I != E; I++) {
539 const VRegInfo &Info = *I->second;
540 populateVRegInfo(Info, Twine(I->first()));
541 }
542
543 for (auto P : PFS.VRegInfos) {
544 const VRegInfo &Info = *P.second;
545 populateVRegInfo(Info, Twine(P.first));
Matthias Braun74ad41c2016-10-11 03:13:01 +0000546 }
547
548 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000549 for (const MachineBasicBlock &MBB : MF) {
550 for (const MachineInstr &MI : MBB) {
551 for (const MachineOperand &MO : MI.operands()) {
552 if (!MO.isRegMask())
553 continue;
554 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000555 }
556 }
557 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000558
559 // FIXME: This is a temporary workaround until the reserved registers can be
560 // serialized.
561 MRI.freezeReservedRegs(MF);
562 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000563}
564
Matthias Braun83947862016-07-13 22:23:23 +0000565bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
566 const yaml::MachineFunction &YamlMF) {
567 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000568 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000569 const Function &F = MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000570 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000571 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
572 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
573 MFI.setHasStackMap(YamlMFI.HasStackMap);
574 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
575 MFI.setStackSize(YamlMFI.StackSize);
576 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
577 if (YamlMFI.MaxAlignment)
578 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
579 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
580 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000581 if (YamlMFI.MaxCallFrameSize != ~0u)
582 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Reid Kleckner9ea2c012018-10-01 21:59:45 +0000583 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
Alex Lorenz60541c12015-07-09 19:55:27 +0000584 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
585 MFI.setHasVAStart(YamlMFI.HasVAStart);
586 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Francis Visoiu Mistrih537d7ee2018-04-06 08:56:25 +0000587 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000588 if (!YamlMFI.SavePoint.Value.empty()) {
589 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000590 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000591 return true;
592 MFI.setSavePoint(MBB);
593 }
594 if (!YamlMFI.RestorePoint.Value.empty()) {
595 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000596 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000597 return true;
598 MFI.setRestorePoint(MBB);
599 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000600
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000601 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000602 // Initialize the fixed frame objects.
603 for (const auto &Object : YamlMF.FixedStackObjects) {
604 int ObjectIdx;
605 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
606 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
607 Object.IsImmutable, Object.IsAliased);
608 else
609 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
610 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000611 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000612 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
613 ObjectIdx))
614 .second)
615 return error(Object.ID.SourceRange.Start,
616 Twine("redefinition of fixed stack object '%fixed-stack.") +
617 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000618 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000619 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000620 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000621 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
622 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000623 }
624
625 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000626 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000627 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000628 const AllocaInst *Alloca = nullptr;
629 const yaml::StringValue &Name = Object.Name;
630 if (!Name.Value.empty()) {
631 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000632 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000633 if (!Alloca)
634 return error(Name.SourceRange.Start,
635 "alloca instruction named '" + Name.Value +
636 "' isn't defined in the function '" + F.getName() +
637 "'");
638 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000639 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000640 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000641 else
642 ObjectIdx = MFI.CreateStackObject(
643 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000644 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000645 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000646 MFI.setStackID(ObjectIdx, Object.StackID);
647
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000648 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
649 .second)
650 return error(Object.ID.SourceRange.Start,
651 Twine("redefinition of stack object '%stack.") +
652 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000653 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000654 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000655 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000656 if (Object.LocalOffset)
657 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000658 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000659 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000660 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000661 MFI.setCalleeSavedInfo(CSIInfo);
662 if (!CSIInfo.empty())
663 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000664
665 // Initialize the various stack object references after initializing the
666 // stack objects.
667 if (!YamlMFI.StackProtector.Value.empty()) {
668 SMDiagnostic Error;
669 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000670 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000671 return error(Error, YamlMFI.StackProtector.SourceRange);
672 MFI.setStackProtectorIndex(FI);
673 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000674 return false;
675}
676
Matthias Braun83947862016-07-13 22:23:23 +0000677bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000678 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000679 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000680 if (RegisterSource.Value.empty())
681 return false;
682 unsigned Reg = 0;
683 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000684 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000685 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000686 CalleeSavedInfo CSI(Reg, FrameIdx);
687 CSI.setRestored(IsRestored);
688 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000689 return false;
690}
691
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000692/// Verify that given node is of a certain type. Return true on error.
693template <typename T>
694static bool typecheckMDNode(T *&Result, MDNode *Node,
695 const yaml::StringValue &Source,
696 StringRef TypeString, MIRParserImpl &Parser) {
697 if (!Node)
698 return false;
699 Result = dyn_cast<T>(Node);
700 if (!Result)
701 return Parser.error(Source.SourceRange.Start,
702 "expected a reference to a '" + TypeString +
703 "' metadata node");
704 return false;
705}
706
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000707template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000708bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000709 const T &Object, int FrameIdx) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000710 // Debug information can only be attached to stack objects; Fixed stack
711 // objects aren't supported.
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000712 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000713 if (parseMDNode(PFS, Var, Object.DebugVar) ||
714 parseMDNode(PFS, Expr, Object.DebugExpr) ||
715 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000716 return true;
717 if (!Var && !Expr && !Loc)
718 return false;
719 DILocalVariable *DIVar = nullptr;
720 DIExpression *DIExpr = nullptr;
721 DILocation *DILoc = nullptr;
722 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
723 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
724 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
725 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000726 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000727 return false;
728}
729
Matthias Braun74ad41c2016-10-11 03:13:01 +0000730bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000731 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000732 if (Source.Value.empty())
733 return false;
734 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000735 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000736 return error(Error, Source.SourceRange);
737 return false;
738}
739
Matthias Braun83947862016-07-13 22:23:23 +0000740bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
741 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
742 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
743 const MachineFunction &MF = PFS.MF;
Matthias Braunf1caa282017-12-15 22:22:58 +0000744 const auto &M = *MF.getFunction().getParent();
Alex Lorenzab980492015-07-20 20:51:18 +0000745 SMDiagnostic Error;
746 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000747 if (YamlConstant.IsTargetSpecific)
748 // FIXME: Support target-specific constant pools
749 return error(YamlConstant.Value.SourceRange.Start,
750 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000751 const Constant *Value = dyn_cast_or_null<Constant>(
752 parseConstantValue(YamlConstant.Value.Value, Error, M));
753 if (!Value)
754 return error(Error, YamlConstant.Value.SourceRange);
755 unsigned Alignment =
756 YamlConstant.Alignment
757 ? YamlConstant.Alignment
758 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000759 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
760 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
761 .second)
762 return error(YamlConstant.ID.SourceRange.Start,
763 Twine("redefinition of constant pool item '%const.") +
764 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000765 }
766 return false;
767}
768
Matthias Braun83947862016-07-13 22:23:23 +0000769bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
770 const yaml::MachineJumpTable &YamlJTI) {
771 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000772 for (const auto &Entry : YamlJTI.Entries) {
773 std::vector<MachineBasicBlock *> Blocks;
774 for (const auto &MBBSource : Entry.Blocks) {
775 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000776 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000777 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000778 Blocks.push_back(MBB);
779 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000780 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000781 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
782 .second)
783 return error(Entry.ID.SourceRange.Start,
784 Twine("redefinition of jump table entry '%jump-table.") +
785 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000786 }
787 return false;
788}
789
Matthias Braun74ad41c2016-10-11 03:13:01 +0000790bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000791 MachineBasicBlock *&MBB,
792 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000793 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000794 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000795 return error(Error, Source.SourceRange);
796 return false;
797}
798
Alex Lorenz51af1602015-06-23 22:39:23 +0000799SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
800 SMRange SourceRange) {
801 assert(SourceRange.isValid() && "Invalid source range");
802 SMLoc Loc = SourceRange.Start;
803 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
804 *Loc.getPointer() == '\'';
805 // Translate the location of the error from the location in the MI string to
806 // the corresponding location in the MIR file.
807 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
808 (HasQuote ? 1 : 0));
809
810 // TODO: Translate any source ranges as well.
811 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
812 Error.getFixIts());
813}
814
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000815SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
816 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000817 assert(SourceRange.isValid());
818
819 // Translate the location of the error from the location in the llvm IR string
820 // to the corresponding location in the MIR file.
821 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
822 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
823 unsigned Column = Error.getColumnNo();
824 StringRef LineStr = Error.getLineContents();
825 SMLoc Loc = Error.getLoc();
826
827 // Get the full line and adjust the column number by taking the indentation of
828 // LLVM IR into account.
829 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
830 L != E; ++L) {
831 if (L.line_number() == Line) {
832 LineStr = *L;
833 Loc = SMLoc::getFromPointer(LineStr.data());
834 auto Indent = LineStr.find(Error.getLineContents());
835 if (Indent != StringRef::npos)
836 Column += Indent;
837 break;
838 }
839 }
840
841 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
842 Error.getMessage(), LineStr, Error.getRanges(),
843 Error.getFixIts());
844}
845
Alex Lorenz28148ba2015-07-09 22:23:13 +0000846void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
847 if (!Names2RegClasses.empty())
848 return;
849 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
850 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
851 const auto *RC = TRI->getRegClass(I);
852 Names2RegClasses.insert(
853 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
854 }
855}
856
Quentin Colombet876ddf82016-04-08 16:40:43 +0000857void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
858 if (!Names2RegBanks.empty())
859 return;
860 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
861 // If the target does not support GlobalISel, we may not have a
862 // register bank info.
863 if (!RBI)
864 return;
865 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
866 const auto &RegBank = RBI->getRegBank(I);
867 Names2RegBanks.insert(
868 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
869 }
870}
871
Alex Lorenz28148ba2015-07-09 22:23:13 +0000872const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
873 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000874 auto RegClassInfo = Names2RegClasses.find(Name);
875 if (RegClassInfo == Names2RegClasses.end())
876 return nullptr;
877 return RegClassInfo->getValue();
878}
879
Quentin Colombet876ddf82016-04-08 16:40:43 +0000880const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
881 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000882 auto RegBankInfo = Names2RegBanks.find(Name);
883 if (RegBankInfo == Names2RegBanks.end())
884 return nullptr;
885 return RegBankInfo->getValue();
886}
887
Alex Lorenz735c47e2015-06-15 20:30:22 +0000888MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
889 : Impl(std::move(Impl)) {}
890
891MIRParser::~MIRParser() {}
892
Matthias Braun7bda1952017-06-06 00:44:35 +0000893std::unique_ptr<Module> MIRParser::parseIRModule() {
894 return Impl->parseIRModule();
895}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000896
Matthias Braun7bda1952017-06-06 00:44:35 +0000897bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
898 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000899}
900
901std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
902 SMDiagnostic &Error,
903 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000904 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000905 if (std::error_code EC = FileOrErr.getError()) {
906 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
907 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000908 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000909 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000910 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000911}
912
Alex Lorenz735c47e2015-06-15 20:30:22 +0000913std::unique_ptr<MIRParser>
914llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
915 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000916 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000917 if (Context.shouldDiscardValueNames()) {
918 Context.diagnose(DiagnosticInfoMIRParser(
919 DS_Error,
920 SMDiagnostic(
921 Filename, SourceMgr::DK_Error,
922 "Can't read MIR with a Context that discards named Values")));
923 return nullptr;
924 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000925 return llvm::make_unique<MIRParser>(
926 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000927}