blob: 2fc53d78290139295f5938054ce3c3c46af6b342 [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();
James Y Knightf47d6b32019-01-31 20:35:56 +0000273 Function *F =
274 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
275 Function::ExternalLinkage, Name, M);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000276 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
277 new UnreachableInst(Context, BB);
Matthias Braun7bda1952017-06-06 00:44:35 +0000278 return F;
279}
280
281bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
282 // Parse the yaml.
283 yaml::MachineFunction YamlMF;
284 yaml::EmptyContext Ctx;
285 yaml::yamlize(In, YamlMF, false, Ctx);
286 if (In.error())
287 return true;
288
289 // Search for the corresponding IR function.
290 StringRef FunctionName = YamlMF.Name;
291 Function *F = M.getFunction(FunctionName);
292 if (!F) {
293 if (NoLLVMIR) {
294 F = createDummyFunction(FunctionName, M);
295 } else {
296 return error(Twine("function '") + FunctionName +
297 "' isn't defined in the provided LLVM IR");
298 }
299 }
300 if (MMI.getMachineFunction(*F) != nullptr)
301 return error(Twine("redefinition of machine function '") + FunctionName +
302 "'");
303
304 // Create the MachineFunction.
305 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
306 if (initializeMachineFunction(YamlMF, MF))
307 return true;
308
309 return false;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000310}
311
Matthias Braun79f85b32016-08-24 01:32:41 +0000312static bool isSSA(const MachineFunction &MF) {
313 const MachineRegisterInfo &MRI = MF.getRegInfo();
314 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
315 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
316 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
317 return false;
318 }
319 return true;
320}
321
Matthias Braun90799ce2016-08-23 21:19:49 +0000322void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000323 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000324
325 bool HasPHI = false;
326 bool HasInlineAsm = false;
327 for (const MachineBasicBlock &MBB : MF) {
328 for (const MachineInstr &MI : MBB) {
329 if (MI.isPHI())
330 HasPHI = true;
331 if (MI.isInlineAsm())
332 HasInlineAsm = true;
333 }
334 }
335 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000336 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000337 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000338
339 if (isSSA(MF))
340 Properties.set(MachineFunctionProperties::Property::IsSSA);
341 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000342 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000343
344 const MachineRegisterInfo &MRI = MF.getRegInfo();
345 if (MRI.getNumVirtRegs() == 0)
346 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000347}
348
Matthias Braun7bda1952017-06-06 00:44:35 +0000349bool
350MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
351 MachineFunction &MF) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000352 // TODO: Recreate the machine function.
Matthias Braunde5fea22017-01-18 00:59:19 +0000353 initNames2RegClasses(MF);
354 initNames2RegBanks(MF);
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000355 if (YamlMF.Alignment)
356 MF.setAlignment(YamlMF.Alignment);
357 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Sanjin Sijaric625d08e2018-10-24 21:07:38 +0000358 MF.setHasWinCFI(YamlMF.HasWinCFI);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000359
360 if (YamlMF.Legalized)
361 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000362 if (YamlMF.RegBankSelected)
363 MF.getProperties().set(
364 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000365 if (YamlMF.Selected)
366 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Roman Tereshin3054ece2018-02-28 17:55:45 +0000367 if (YamlMF.FailedISel)
368 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000369
Matthias Braunde5fea22017-01-18 00:59:19 +0000370 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
371 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000372 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000373 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000374 if (!YamlMF.Constants.empty()) {
375 auto *ConstantPool = MF.getConstantPool();
376 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000377 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000378 return true;
379 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000380
Matthias Braune35861d2016-07-13 23:27:50 +0000381 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000382 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000383 SourceMgr BlockSM;
384 BlockSM.AddNewSourceBuffer(
385 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
386 SMLoc());
387 PFS.SM = &BlockSM;
388 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000389 reportDiagnostic(
390 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
391 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000392 }
Matthias Braune35861d2016-07-13 23:27:50 +0000393 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000394
Alex Lorenza6f9a372015-07-29 21:09:09 +0000395 // Initialize the frame information after creating all the MBBs so that the
396 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000397 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000398 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000399 // Initialize the jump table after creating all the MBBs so that the MBB
400 // references can be resolved.
401 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000402 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000403 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000404 // Parse the machine instructions after creating all of the MBBs so that the
405 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000406 StringRef InsnStr = YamlMF.Body.Value.Value;
407 SourceMgr InsnSM;
408 InsnSM.AddNewSourceBuffer(
409 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
410 SMLoc());
411 PFS.SM = &InsnSM;
412 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000413 reportDiagnostic(
414 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
415 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000416 }
Matthias Braune35861d2016-07-13 23:27:50 +0000417 PFS.SM = &SM;
418
Matthias Braun74ad41c2016-10-11 03:13:01 +0000419 if (setupRegisterInfo(PFS, YamlMF))
420 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000421
422 computeFunctionProperties(MF);
423
Matthias Braun5c290dc2018-01-19 03:16:36 +0000424 MF.getSubtarget().mirFileLoaded(MF);
425
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000426 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000427 return false;
428}
429
Matthias Braun74ad41c2016-10-11 03:13:01 +0000430bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
431 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000432 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000433 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000434 assert(RegInfo.tracksLiveness());
435 if (!YamlMF.TracksRegLiveness)
436 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000437
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000438 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000439 // Parse the virtual register information.
440 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000441 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
442 if (Info.Explicit)
443 return error(VReg.ID.SourceRange.Start,
444 Twine("redefinition of virtual register '%") +
445 Twine(VReg.ID.Value) + "'");
446 Info.Explicit = true;
447
Quentin Colombet050b2112016-03-08 01:17:03 +0000448 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000449 Info.Kind = VRegInfo::GENERIC;
Justin Bogner6c7663f2017-11-17 18:51:20 +0000450 Info.D.RegBank = nullptr;
Quentin Colombet050b2112016-03-08 01:17:03 +0000451 } else {
452 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000453 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000454 Info.Kind = VRegInfo::NORMAL;
455 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000456 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000457 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000458 if (!RegBank)
459 return error(
460 VReg.Class.SourceRange.Start,
461 Twine("use of undefined register class or register bank '") +
462 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000463 Info.Kind = VRegInfo::REGBANK;
464 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000465 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000466 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000467
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000468 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000469 if (Info.Kind != VRegInfo::NORMAL)
470 return error(VReg.Class.SourceRange.Start,
471 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000472
473 if (parseRegisterReference(PFS, Info.PreferredReg,
474 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000475 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000476 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000477 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000478
479 // Parse the liveins.
480 for (const auto &LiveIn : YamlMF.LiveIns) {
481 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000482 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000483 return error(Error, LiveIn.Register.SourceRange);
484 unsigned VReg = 0;
485 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000486 VRegInfo *Info;
487 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000488 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000489 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000490 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000491 }
492 RegInfo.addLiveIn(Reg, VReg);
493 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000494
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000495 // Parse the callee saved registers (Registers that will
496 // be saved for the caller).
497 if (YamlMF.CalleeSavedRegisters) {
498 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
499 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
500 unsigned Reg = 0;
501 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
502 return error(Error, RegSource.SourceRange);
503 CalleeSavedRegisters.push_back(Reg);
504 }
505 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000506 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000507
Alex Lorenz54565cf2015-06-24 19:56:10 +0000508 return false;
509}
510
Matthias Braun74ad41c2016-10-11 03:13:01 +0000511bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000512 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000513 MachineFunction &MF = PFS.MF;
514 MachineRegisterInfo &MRI = MF.getRegInfo();
515 bool Error = false;
516 // Create VRegs
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000517 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000518 unsigned Reg = Info.VReg;
519 switch (Info.Kind) {
520 case VRegInfo::UNKNOWN:
521 error(Twine("Cannot determine class/bank of virtual register ") +
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000522 Name + " in function '" + MF.getName() + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000523 Error = true;
524 break;
525 case VRegInfo::NORMAL:
526 MRI.setRegClass(Reg, Info.D.RC);
527 if (Info.PreferredReg != 0)
528 MRI.setSimpleHint(Reg, Info.PreferredReg);
529 break;
530 case VRegInfo::GENERIC:
531 break;
532 case VRegInfo::REGBANK:
533 MRI.setRegBank(Reg, *Info.D.RegBank);
534 break;
535 }
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000536 };
537
538 for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end();
539 I != E; I++) {
540 const VRegInfo &Info = *I->second;
541 populateVRegInfo(Info, Twine(I->first()));
542 }
543
544 for (auto P : PFS.VRegInfos) {
545 const VRegInfo &Info = *P.second;
546 populateVRegInfo(Info, Twine(P.first));
Matthias Braun74ad41c2016-10-11 03:13:01 +0000547 }
548
549 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000550 for (const MachineBasicBlock &MBB : MF) {
551 for (const MachineInstr &MI : MBB) {
552 for (const MachineOperand &MO : MI.operands()) {
553 if (!MO.isRegMask())
554 continue;
555 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000556 }
557 }
558 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000559
560 // FIXME: This is a temporary workaround until the reserved registers can be
561 // serialized.
562 MRI.freezeReservedRegs(MF);
563 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000564}
565
Matthias Braun83947862016-07-13 22:23:23 +0000566bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
567 const yaml::MachineFunction &YamlMF) {
568 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000569 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000570 const Function &F = MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000571 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000572 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
573 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
574 MFI.setHasStackMap(YamlMFI.HasStackMap);
575 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
576 MFI.setStackSize(YamlMFI.StackSize);
577 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
578 if (YamlMFI.MaxAlignment)
579 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
580 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
581 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000582 if (YamlMFI.MaxCallFrameSize != ~0u)
583 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Reid Kleckner9ea2c012018-10-01 21:59:45 +0000584 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
Alex Lorenz60541c12015-07-09 19:55:27 +0000585 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
586 MFI.setHasVAStart(YamlMFI.HasVAStart);
587 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Francis Visoiu Mistrih537d7ee2018-04-06 08:56:25 +0000588 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000589 if (!YamlMFI.SavePoint.Value.empty()) {
590 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000591 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000592 return true;
593 MFI.setSavePoint(MBB);
594 }
595 if (!YamlMFI.RestorePoint.Value.empty()) {
596 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000597 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000598 return true;
599 MFI.setRestorePoint(MBB);
600 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000601
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000602 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000603 // Initialize the fixed frame objects.
604 for (const auto &Object : YamlMF.FixedStackObjects) {
605 int ObjectIdx;
606 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
607 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
608 Object.IsImmutable, Object.IsAliased);
609 else
610 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
611 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000612 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000613 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
614 ObjectIdx))
615 .second)
616 return error(Object.ID.SourceRange.Start,
617 Twine("redefinition of fixed stack object '%fixed-stack.") +
618 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000619 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000620 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000621 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000622 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
623 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000624 }
625
626 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000627 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000628 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000629 const AllocaInst *Alloca = nullptr;
630 const yaml::StringValue &Name = Object.Name;
631 if (!Name.Value.empty()) {
632 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000633 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000634 if (!Alloca)
635 return error(Name.SourceRange.Start,
636 "alloca instruction named '" + Name.Value +
637 "' isn't defined in the function '" + F.getName() +
638 "'");
639 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000640 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000641 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000642 else
643 ObjectIdx = MFI.CreateStackObject(
644 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000645 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000646 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000647 MFI.setStackID(ObjectIdx, Object.StackID);
648
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000649 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
650 .second)
651 return error(Object.ID.SourceRange.Start,
652 Twine("redefinition of stack object '%stack.") +
653 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000654 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000655 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000656 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000657 if (Object.LocalOffset)
658 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000659 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000660 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000661 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000662 MFI.setCalleeSavedInfo(CSIInfo);
663 if (!CSIInfo.empty())
664 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000665
666 // Initialize the various stack object references after initializing the
667 // stack objects.
668 if (!YamlMFI.StackProtector.Value.empty()) {
669 SMDiagnostic Error;
670 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000671 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000672 return error(Error, YamlMFI.StackProtector.SourceRange);
673 MFI.setStackProtectorIndex(FI);
674 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000675 return false;
676}
677
Matthias Braun83947862016-07-13 22:23:23 +0000678bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000679 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000680 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000681 if (RegisterSource.Value.empty())
682 return false;
683 unsigned Reg = 0;
684 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000685 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000686 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000687 CalleeSavedInfo CSI(Reg, FrameIdx);
688 CSI.setRestored(IsRestored);
689 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000690 return false;
691}
692
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000693/// Verify that given node is of a certain type. Return true on error.
694template <typename T>
695static bool typecheckMDNode(T *&Result, MDNode *Node,
696 const yaml::StringValue &Source,
697 StringRef TypeString, MIRParserImpl &Parser) {
698 if (!Node)
699 return false;
700 Result = dyn_cast<T>(Node);
701 if (!Result)
702 return Parser.error(Source.SourceRange.Start,
703 "expected a reference to a '" + TypeString +
704 "' metadata node");
705 return false;
706}
707
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000708template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000709bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000710 const T &Object, int FrameIdx) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000711 // Debug information can only be attached to stack objects; Fixed stack
712 // objects aren't supported.
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000713 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000714 if (parseMDNode(PFS, Var, Object.DebugVar) ||
715 parseMDNode(PFS, Expr, Object.DebugExpr) ||
716 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000717 return true;
718 if (!Var && !Expr && !Loc)
719 return false;
720 DILocalVariable *DIVar = nullptr;
721 DIExpression *DIExpr = nullptr;
722 DILocation *DILoc = nullptr;
723 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
724 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
725 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
726 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000727 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000728 return false;
729}
730
Matthias Braun74ad41c2016-10-11 03:13:01 +0000731bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000732 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000733 if (Source.Value.empty())
734 return false;
735 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000736 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000737 return error(Error, Source.SourceRange);
738 return false;
739}
740
Matthias Braun83947862016-07-13 22:23:23 +0000741bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
742 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
743 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
744 const MachineFunction &MF = PFS.MF;
Matthias Braunf1caa282017-12-15 22:22:58 +0000745 const auto &M = *MF.getFunction().getParent();
Alex Lorenzab980492015-07-20 20:51:18 +0000746 SMDiagnostic Error;
747 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000748 if (YamlConstant.IsTargetSpecific)
749 // FIXME: Support target-specific constant pools
750 return error(YamlConstant.Value.SourceRange.Start,
751 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000752 const Constant *Value = dyn_cast_or_null<Constant>(
753 parseConstantValue(YamlConstant.Value.Value, Error, M));
754 if (!Value)
755 return error(Error, YamlConstant.Value.SourceRange);
756 unsigned Alignment =
757 YamlConstant.Alignment
758 ? YamlConstant.Alignment
759 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000760 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
761 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
762 .second)
763 return error(YamlConstant.ID.SourceRange.Start,
764 Twine("redefinition of constant pool item '%const.") +
765 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000766 }
767 return false;
768}
769
Matthias Braun83947862016-07-13 22:23:23 +0000770bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
771 const yaml::MachineJumpTable &YamlJTI) {
772 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000773 for (const auto &Entry : YamlJTI.Entries) {
774 std::vector<MachineBasicBlock *> Blocks;
775 for (const auto &MBBSource : Entry.Blocks) {
776 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000777 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000778 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000779 Blocks.push_back(MBB);
780 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000781 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000782 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
783 .second)
784 return error(Entry.ID.SourceRange.Start,
785 Twine("redefinition of jump table entry '%jump-table.") +
786 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000787 }
788 return false;
789}
790
Matthias Braun74ad41c2016-10-11 03:13:01 +0000791bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000792 MachineBasicBlock *&MBB,
793 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000794 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000795 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000796 return error(Error, Source.SourceRange);
797 return false;
798}
799
Alex Lorenz51af1602015-06-23 22:39:23 +0000800SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
801 SMRange SourceRange) {
802 assert(SourceRange.isValid() && "Invalid source range");
803 SMLoc Loc = SourceRange.Start;
804 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
805 *Loc.getPointer() == '\'';
806 // Translate the location of the error from the location in the MI string to
807 // the corresponding location in the MIR file.
808 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
809 (HasQuote ? 1 : 0));
810
811 // TODO: Translate any source ranges as well.
812 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
813 Error.getFixIts());
814}
815
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000816SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
817 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000818 assert(SourceRange.isValid());
819
820 // Translate the location of the error from the location in the llvm IR string
821 // to the corresponding location in the MIR file.
822 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
823 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
824 unsigned Column = Error.getColumnNo();
825 StringRef LineStr = Error.getLineContents();
826 SMLoc Loc = Error.getLoc();
827
828 // Get the full line and adjust the column number by taking the indentation of
829 // LLVM IR into account.
830 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
831 L != E; ++L) {
832 if (L.line_number() == Line) {
833 LineStr = *L;
834 Loc = SMLoc::getFromPointer(LineStr.data());
835 auto Indent = LineStr.find(Error.getLineContents());
836 if (Indent != StringRef::npos)
837 Column += Indent;
838 break;
839 }
840 }
841
842 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
843 Error.getMessage(), LineStr, Error.getRanges(),
844 Error.getFixIts());
845}
846
Alex Lorenz28148ba2015-07-09 22:23:13 +0000847void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
848 if (!Names2RegClasses.empty())
849 return;
850 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
851 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
852 const auto *RC = TRI->getRegClass(I);
853 Names2RegClasses.insert(
854 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
855 }
856}
857
Quentin Colombet876ddf82016-04-08 16:40:43 +0000858void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
859 if (!Names2RegBanks.empty())
860 return;
861 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
862 // If the target does not support GlobalISel, we may not have a
863 // register bank info.
864 if (!RBI)
865 return;
866 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
867 const auto &RegBank = RBI->getRegBank(I);
868 Names2RegBanks.insert(
869 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
870 }
871}
872
Alex Lorenz28148ba2015-07-09 22:23:13 +0000873const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
874 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000875 auto RegClassInfo = Names2RegClasses.find(Name);
876 if (RegClassInfo == Names2RegClasses.end())
877 return nullptr;
878 return RegClassInfo->getValue();
879}
880
Quentin Colombet876ddf82016-04-08 16:40:43 +0000881const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
882 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000883 auto RegBankInfo = Names2RegBanks.find(Name);
884 if (RegBankInfo == Names2RegBanks.end())
885 return nullptr;
886 return RegBankInfo->getValue();
887}
888
Alex Lorenz735c47e2015-06-15 20:30:22 +0000889MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
890 : Impl(std::move(Impl)) {}
891
892MIRParser::~MIRParser() {}
893
Matthias Braun7bda1952017-06-06 00:44:35 +0000894std::unique_ptr<Module> MIRParser::parseIRModule() {
895 return Impl->parseIRModule();
896}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000897
Matthias Braun7bda1952017-06-06 00:44:35 +0000898bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
899 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000900}
901
902std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
903 SMDiagnostic &Error,
904 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000905 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000906 if (std::error_code EC = FileOrErr.getError()) {
907 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
908 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000909 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000910 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000911 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000912}
913
Alex Lorenz735c47e2015-06-15 20:30:22 +0000914std::unique_ptr<MIRParser>
915llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
916 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000917 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000918 if (Context.shouldDiscardValueNames()) {
919 Context.diagnose(DiagnosticInfoMIRParser(
920 DS_Error,
921 SMDiagnostic(
922 Filename, SourceMgr::DK_Error,
923 "Can't read MIR with a Context that discards named Values")));
924 return nullptr;
925 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000926 return llvm::make_unique<MIRParser>(
927 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000928}