blob: 51e4948d2285180596838b3fdb87017672405e03 [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the class that parses the optional LLVM IR and machine
11// functions that are stored in MIR files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/MIRParser/MIRParser.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "MIParser.h"
Alex Lorenz33f0aef2015-06-26 16:46:11 +000017#include "llvm/ADT/DenseMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000018#include "llvm/ADT/STLExtras.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000019#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000021#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000022#include "llvm/AsmParser/SlotMapping.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000023#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
24#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
25#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000028#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000031#include "llvm/IR/BasicBlock.h"
Reid Kleckner28865802016-04-14 18:29:59 +000032#include "llvm/IR/DebugInfo.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000033#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000034#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000035#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000036#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000037#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000038#include "llvm/Support/LineIterator.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000039#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000040#include "llvm/Support/SMLoc.h"
41#include "llvm/Support/SourceMgr.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000042#include "llvm/Support/YAMLTraits.h"
43#include <memory>
44
45using namespace llvm;
46
Alex Lorenz735c47e2015-06-15 20:30:22 +000047namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000048
49/// This class implements the parsing of LLVM IR that's embedded inside a MIR
50/// file.
51class MIRParserImpl {
52 SourceMgr SM;
Matthias Braun7bda1952017-06-06 00:44:35 +000053 yaml::Input In;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000054 StringRef Filename;
55 LLVMContext &Context;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000056 SlotMapping IRSlots;
Alex Lorenz28148ba2015-07-09 22:23:13 +000057 /// Maps from register class names to register classes.
Matthias Braunde5fea22017-01-18 00:59:19 +000058 Name2RegClassMap Names2RegClasses;
Quentin Colombet876ddf82016-04-08 16:40:43 +000059 /// Maps from register bank names to register banks.
Matthias Braunde5fea22017-01-18 00:59:19 +000060 Name2RegBankMap Names2RegBanks;
Matthias Braun7bda1952017-06-06 00:44:35 +000061 /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are
62 /// created and inserted into the given module when this is true.
63 bool NoLLVMIR = false;
64 /// True when a well formed MIR file does not contain any MIR/machine function
65 /// parts.
66 bool NoMIRDocuments = false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000067
68public:
Matthias Braun7bda1952017-06-06 00:44:35 +000069 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
70 StringRef Filename, LLVMContext &Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000071
Alex Lorenz735c47e2015-06-15 20:30:22 +000072 void reportDiagnostic(const SMDiagnostic &Diag);
73
74 /// Report an error with the given message at unknown location.
75 ///
76 /// Always returns true.
77 bool error(const Twine &Message);
78
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000079 /// Report an error with the given message at the given location.
80 ///
81 /// Always returns true.
82 bool error(SMLoc Loc, const Twine &Message);
83
Alex Lorenz0fd7c622015-06-30 17:55:00 +000084 /// Report a given error with the location translated from the location in an
85 /// embedded string literal to a location in the MIR file.
86 ///
87 /// Always returns true.
88 bool error(const SMDiagnostic &Error, SMRange SourceRange);
89
Alex Lorenz78d78312015-05-28 22:41:12 +000090 /// Try to parse the optional LLVM module and the machine functions in the MIR
91 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000092 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000093 /// Return null if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +000094 std::unique_ptr<Module> parseIRModule();
95
96 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
Alex Lorenz78d78312015-05-28 22:41:12 +000097
98 /// Parse the machine function in the current YAML document.
99 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000100 ///
Alex Lorenz78d78312015-05-28 22:41:12 +0000101 /// Return true if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000102 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000103
Alex Lorenz735c47e2015-06-15 20:30:22 +0000104 /// Initialize the machine function to the state that's described in the MIR
105 /// file.
106 ///
107 /// Return true if error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000108 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
109 MachineFunction &MF);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000110
Matthias Braun74ad41c2016-10-11 03:13:01 +0000111 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
112 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000113
Matthias Braun74ad41c2016-10-11 03:13:01 +0000114 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000115 const yaml::MachineFunction &YamlMF);
116
Matthias Braun83947862016-07-13 22:23:23 +0000117 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
118 const yaml::MachineFunction &YamlMF);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000119
Matthias Braun83947862016-07-13 22:23:23 +0000120 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000121 std::vector<CalleeSavedInfo> &CSIInfo,
122 const yaml::StringValue &RegisterSource,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000123 bool IsRestored, int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000124
Matthias Braun83947862016-07-13 22:23:23 +0000125 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000126 const yaml::MachineStackObject &Object,
127 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);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000357
358 if (YamlMF.Legalized)
359 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000360 if (YamlMF.RegBankSelected)
361 MF.getProperties().set(
362 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000363 if (YamlMF.Selected)
364 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Roman Tereshin3054ece2018-02-28 17:55:45 +0000365 if (YamlMF.FailedISel)
366 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000367
Matthias Braunde5fea22017-01-18 00:59:19 +0000368 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
369 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000370 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000371 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000372 if (!YamlMF.Constants.empty()) {
373 auto *ConstantPool = MF.getConstantPool();
374 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000375 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000376 return true;
377 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000378
Matthias Braune35861d2016-07-13 23:27:50 +0000379 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000380 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000381 SourceMgr BlockSM;
382 BlockSM.AddNewSourceBuffer(
383 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
384 SMLoc());
385 PFS.SM = &BlockSM;
386 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000387 reportDiagnostic(
388 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
389 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000390 }
Matthias Braune35861d2016-07-13 23:27:50 +0000391 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000392
Alex Lorenza6f9a372015-07-29 21:09:09 +0000393 // Initialize the frame information after creating all the MBBs so that the
394 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000395 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000396 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000397 // Initialize the jump table after creating all the MBBs so that the MBB
398 // references can be resolved.
399 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000400 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000401 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000402 // Parse the machine instructions after creating all of the MBBs so that the
403 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000404 StringRef InsnStr = YamlMF.Body.Value.Value;
405 SourceMgr InsnSM;
406 InsnSM.AddNewSourceBuffer(
407 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
408 SMLoc());
409 PFS.SM = &InsnSM;
410 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000411 reportDiagnostic(
412 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
413 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000414 }
Matthias Braune35861d2016-07-13 23:27:50 +0000415 PFS.SM = &SM;
416
Matthias Braun74ad41c2016-10-11 03:13:01 +0000417 if (setupRegisterInfo(PFS, YamlMF))
418 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000419
420 computeFunctionProperties(MF);
421
Matthias Braun5c290dc2018-01-19 03:16:36 +0000422 MF.getSubtarget().mirFileLoaded(MF);
423
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000424 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000425 return false;
426}
427
Matthias Braun74ad41c2016-10-11 03:13:01 +0000428bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
429 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000430 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000431 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000432 assert(RegInfo.tracksLiveness());
433 if (!YamlMF.TracksRegLiveness)
434 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000435
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000436 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000437 // Parse the virtual register information.
438 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000439 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
440 if (Info.Explicit)
441 return error(VReg.ID.SourceRange.Start,
442 Twine("redefinition of virtual register '%") +
443 Twine(VReg.ID.Value) + "'");
444 Info.Explicit = true;
445
Quentin Colombet050b2112016-03-08 01:17:03 +0000446 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000447 Info.Kind = VRegInfo::GENERIC;
Justin Bogner6c7663f2017-11-17 18:51:20 +0000448 Info.D.RegBank = nullptr;
Quentin Colombet050b2112016-03-08 01:17:03 +0000449 } else {
450 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000451 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000452 Info.Kind = VRegInfo::NORMAL;
453 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000454 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000455 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000456 if (!RegBank)
457 return error(
458 VReg.Class.SourceRange.Start,
459 Twine("use of undefined register class or register bank '") +
460 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000461 Info.Kind = VRegInfo::REGBANK;
462 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000463 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000464 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000465
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000466 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000467 if (Info.Kind != VRegInfo::NORMAL)
468 return error(VReg.Class.SourceRange.Start,
469 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000470
471 if (parseRegisterReference(PFS, Info.PreferredReg,
472 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000473 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000474 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000475 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000476
477 // Parse the liveins.
478 for (const auto &LiveIn : YamlMF.LiveIns) {
479 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000480 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000481 return error(Error, LiveIn.Register.SourceRange);
482 unsigned VReg = 0;
483 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000484 VRegInfo *Info;
485 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000486 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000487 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000488 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000489 }
490 RegInfo.addLiveIn(Reg, VReg);
491 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000492
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000493 // Parse the callee saved registers (Registers that will
494 // be saved for the caller).
495 if (YamlMF.CalleeSavedRegisters) {
496 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
497 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
498 unsigned Reg = 0;
499 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
500 return error(Error, RegSource.SourceRange);
501 CalleeSavedRegisters.push_back(Reg);
502 }
503 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000504 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000505
Alex Lorenz54565cf2015-06-24 19:56:10 +0000506 return false;
507}
508
Matthias Braun74ad41c2016-10-11 03:13:01 +0000509bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000510 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000511 MachineFunction &MF = PFS.MF;
512 MachineRegisterInfo &MRI = MF.getRegInfo();
513 bool Error = false;
514 // Create VRegs
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000515 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000516 unsigned Reg = Info.VReg;
517 switch (Info.Kind) {
518 case VRegInfo::UNKNOWN:
519 error(Twine("Cannot determine class/bank of virtual register ") +
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000520 Name + " in function '" + MF.getName() + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000521 Error = true;
522 break;
523 case VRegInfo::NORMAL:
524 MRI.setRegClass(Reg, Info.D.RC);
525 if (Info.PreferredReg != 0)
526 MRI.setSimpleHint(Reg, Info.PreferredReg);
527 break;
528 case VRegInfo::GENERIC:
529 break;
530 case VRegInfo::REGBANK:
531 MRI.setRegBank(Reg, *Info.D.RegBank);
532 break;
533 }
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000534 };
535
536 for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end();
537 I != E; I++) {
538 const VRegInfo &Info = *I->second;
539 populateVRegInfo(Info, Twine(I->first()));
540 }
541
542 for (auto P : PFS.VRegInfos) {
543 const VRegInfo &Info = *P.second;
544 populateVRegInfo(Info, Twine(P.first));
Matthias Braun74ad41c2016-10-11 03:13:01 +0000545 }
546
547 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000548 for (const MachineBasicBlock &MBB : MF) {
549 for (const MachineInstr &MI : MBB) {
550 for (const MachineOperand &MO : MI.operands()) {
551 if (!MO.isRegMask())
552 continue;
553 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000554 }
555 }
556 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000557
558 // FIXME: This is a temporary workaround until the reserved registers can be
559 // serialized.
560 MRI.freezeReservedRegs(MF);
561 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000562}
563
Matthias Braun83947862016-07-13 22:23:23 +0000564bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
565 const yaml::MachineFunction &YamlMF) {
566 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000567 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000568 const Function &F = MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000569 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000570 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
571 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
572 MFI.setHasStackMap(YamlMFI.HasStackMap);
573 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
574 MFI.setStackSize(YamlMFI.StackSize);
575 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
576 if (YamlMFI.MaxAlignment)
577 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
578 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
579 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000580 if (YamlMFI.MaxCallFrameSize != ~0u)
581 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Alex Lorenz60541c12015-07-09 19:55:27 +0000582 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
583 MFI.setHasVAStart(YamlMFI.HasVAStart);
584 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000585 if (!YamlMFI.SavePoint.Value.empty()) {
586 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000587 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000588 return true;
589 MFI.setSavePoint(MBB);
590 }
591 if (!YamlMFI.RestorePoint.Value.empty()) {
592 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000593 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000594 return true;
595 MFI.setRestorePoint(MBB);
596 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000597
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000598 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000599 // Initialize the fixed frame objects.
600 for (const auto &Object : YamlMF.FixedStackObjects) {
601 int ObjectIdx;
602 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
603 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
604 Object.IsImmutable, Object.IsAliased);
605 else
606 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
607 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000608 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000609 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
610 ObjectIdx))
611 .second)
612 return error(Object.ID.SourceRange.Start,
613 Twine("redefinition of fixed stack object '%fixed-stack.") +
614 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000615 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000616 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000617 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000618 }
619
620 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000621 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000622 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000623 const AllocaInst *Alloca = nullptr;
624 const yaml::StringValue &Name = Object.Name;
625 if (!Name.Value.empty()) {
626 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000627 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000628 if (!Alloca)
629 return error(Name.SourceRange.Start,
630 "alloca instruction named '" + Name.Value +
631 "' isn't defined in the function '" + F.getName() +
632 "'");
633 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000634 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000635 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000636 else
637 ObjectIdx = MFI.CreateStackObject(
638 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000639 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000640 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000641 MFI.setStackID(ObjectIdx, Object.StackID);
642
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000643 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
644 .second)
645 return error(Object.ID.SourceRange.Start,
646 Twine("redefinition of stack object '%stack.") +
647 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000648 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000649 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000650 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000651 if (Object.LocalOffset)
652 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000653 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000654 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000655 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000656 MFI.setCalleeSavedInfo(CSIInfo);
657 if (!CSIInfo.empty())
658 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000659
660 // Initialize the various stack object references after initializing the
661 // stack objects.
662 if (!YamlMFI.StackProtector.Value.empty()) {
663 SMDiagnostic Error;
664 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000665 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000666 return error(Error, YamlMFI.StackProtector.SourceRange);
667 MFI.setStackProtectorIndex(FI);
668 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000669 return false;
670}
671
Matthias Braun83947862016-07-13 22:23:23 +0000672bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000673 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000674 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000675 if (RegisterSource.Value.empty())
676 return false;
677 unsigned Reg = 0;
678 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000679 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000680 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000681 CalleeSavedInfo CSI(Reg, FrameIdx);
682 CSI.setRestored(IsRestored);
683 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000684 return false;
685}
686
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000687/// Verify that given node is of a certain type. Return true on error.
688template <typename T>
689static bool typecheckMDNode(T *&Result, MDNode *Node,
690 const yaml::StringValue &Source,
691 StringRef TypeString, MIRParserImpl &Parser) {
692 if (!Node)
693 return false;
694 Result = dyn_cast<T>(Node);
695 if (!Result)
696 return Parser.error(Source.SourceRange.Start,
697 "expected a reference to a '" + TypeString +
698 "' metadata node");
699 return false;
700}
701
Matthias Braun83947862016-07-13 22:23:23 +0000702bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000703 const yaml::MachineStackObject &Object, int FrameIdx) {
704 // Debug information can only be attached to stack objects; Fixed stack
705 // objects aren't supported.
706 assert(FrameIdx >= 0 && "Expected a stack object frame index");
707 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000708 if (parseMDNode(PFS, Var, Object.DebugVar) ||
709 parseMDNode(PFS, Expr, Object.DebugExpr) ||
710 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000711 return true;
712 if (!Var && !Expr && !Loc)
713 return false;
714 DILocalVariable *DIVar = nullptr;
715 DIExpression *DIExpr = nullptr;
716 DILocation *DILoc = nullptr;
717 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
718 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
719 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
720 return true;
Matthias Braunef331ef2016-11-30 23:48:50 +0000721 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000722 return false;
723}
724
Matthias Braun74ad41c2016-10-11 03:13:01 +0000725bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000726 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000727 if (Source.Value.empty())
728 return false;
729 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000730 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000731 return error(Error, Source.SourceRange);
732 return false;
733}
734
Matthias Braun83947862016-07-13 22:23:23 +0000735bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
736 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
737 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
738 const MachineFunction &MF = PFS.MF;
Matthias Braunf1caa282017-12-15 22:22:58 +0000739 const auto &M = *MF.getFunction().getParent();
Alex Lorenzab980492015-07-20 20:51:18 +0000740 SMDiagnostic Error;
741 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000742 if (YamlConstant.IsTargetSpecific)
743 // FIXME: Support target-specific constant pools
744 return error(YamlConstant.Value.SourceRange.Start,
745 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000746 const Constant *Value = dyn_cast_or_null<Constant>(
747 parseConstantValue(YamlConstant.Value.Value, Error, M));
748 if (!Value)
749 return error(Error, YamlConstant.Value.SourceRange);
750 unsigned Alignment =
751 YamlConstant.Alignment
752 ? YamlConstant.Alignment
753 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000754 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
755 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
756 .second)
757 return error(YamlConstant.ID.SourceRange.Start,
758 Twine("redefinition of constant pool item '%const.") +
759 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000760 }
761 return false;
762}
763
Matthias Braun83947862016-07-13 22:23:23 +0000764bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
765 const yaml::MachineJumpTable &YamlJTI) {
766 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000767 for (const auto &Entry : YamlJTI.Entries) {
768 std::vector<MachineBasicBlock *> Blocks;
769 for (const auto &MBBSource : Entry.Blocks) {
770 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000771 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000772 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000773 Blocks.push_back(MBB);
774 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000775 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000776 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
777 .second)
778 return error(Entry.ID.SourceRange.Start,
779 Twine("redefinition of jump table entry '%jump-table.") +
780 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000781 }
782 return false;
783}
784
Matthias Braun74ad41c2016-10-11 03:13:01 +0000785bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000786 MachineBasicBlock *&MBB,
787 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000788 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000789 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000790 return error(Error, Source.SourceRange);
791 return false;
792}
793
Alex Lorenz51af1602015-06-23 22:39:23 +0000794SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
795 SMRange SourceRange) {
796 assert(SourceRange.isValid() && "Invalid source range");
797 SMLoc Loc = SourceRange.Start;
798 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
799 *Loc.getPointer() == '\'';
800 // Translate the location of the error from the location in the MI string to
801 // the corresponding location in the MIR file.
802 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
803 (HasQuote ? 1 : 0));
804
805 // TODO: Translate any source ranges as well.
806 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
807 Error.getFixIts());
808}
809
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000810SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
811 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000812 assert(SourceRange.isValid());
813
814 // Translate the location of the error from the location in the llvm IR string
815 // to the corresponding location in the MIR file.
816 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
817 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
818 unsigned Column = Error.getColumnNo();
819 StringRef LineStr = Error.getLineContents();
820 SMLoc Loc = Error.getLoc();
821
822 // Get the full line and adjust the column number by taking the indentation of
823 // LLVM IR into account.
824 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
825 L != E; ++L) {
826 if (L.line_number() == Line) {
827 LineStr = *L;
828 Loc = SMLoc::getFromPointer(LineStr.data());
829 auto Indent = LineStr.find(Error.getLineContents());
830 if (Indent != StringRef::npos)
831 Column += Indent;
832 break;
833 }
834 }
835
836 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
837 Error.getMessage(), LineStr, Error.getRanges(),
838 Error.getFixIts());
839}
840
Alex Lorenz28148ba2015-07-09 22:23:13 +0000841void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
842 if (!Names2RegClasses.empty())
843 return;
844 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
845 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
846 const auto *RC = TRI->getRegClass(I);
847 Names2RegClasses.insert(
848 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
849 }
850}
851
Quentin Colombet876ddf82016-04-08 16:40:43 +0000852void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
853 if (!Names2RegBanks.empty())
854 return;
855 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
856 // If the target does not support GlobalISel, we may not have a
857 // register bank info.
858 if (!RBI)
859 return;
860 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
861 const auto &RegBank = RBI->getRegBank(I);
862 Names2RegBanks.insert(
863 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
864 }
865}
866
Alex Lorenz28148ba2015-07-09 22:23:13 +0000867const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
868 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000869 auto RegClassInfo = Names2RegClasses.find(Name);
870 if (RegClassInfo == Names2RegClasses.end())
871 return nullptr;
872 return RegClassInfo->getValue();
873}
874
Quentin Colombet876ddf82016-04-08 16:40:43 +0000875const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
876 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000877 auto RegBankInfo = Names2RegBanks.find(Name);
878 if (RegBankInfo == Names2RegBanks.end())
879 return nullptr;
880 return RegBankInfo->getValue();
881}
882
Alex Lorenz735c47e2015-06-15 20:30:22 +0000883MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
884 : Impl(std::move(Impl)) {}
885
886MIRParser::~MIRParser() {}
887
Matthias Braun7bda1952017-06-06 00:44:35 +0000888std::unique_ptr<Module> MIRParser::parseIRModule() {
889 return Impl->parseIRModule();
890}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000891
Matthias Braun7bda1952017-06-06 00:44:35 +0000892bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
893 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000894}
895
896std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
897 SMDiagnostic &Error,
898 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000899 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000900 if (std::error_code EC = FileOrErr.getError()) {
901 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
902 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000903 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000904 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000905 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000906}
907
Alex Lorenz735c47e2015-06-15 20:30:22 +0000908std::unique_ptr<MIRParser>
909llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
910 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000911 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000912 if (Context.shouldDiscardValueNames()) {
913 Context.diagnose(DiagnosticInfoMIRParser(
914 DS_Error,
915 SMDiagnostic(
916 Filename, SourceMgr::DK_Error,
917 "Can't read MIR with a Context that discards named Values")));
918 return nullptr;
919 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000920 return llvm::make_unique<MIRParser>(
921 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000922}