blob: c91255f959283006c50aade5e599d716b6ca8109 [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,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000240 Context, &IRSlots);
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);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000365
Matthias Braunde5fea22017-01-18 00:59:19 +0000366 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
367 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000368 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000369 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000370 if (!YamlMF.Constants.empty()) {
371 auto *ConstantPool = MF.getConstantPool();
372 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000373 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000374 return true;
375 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000376
Matthias Braune35861d2016-07-13 23:27:50 +0000377 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000378 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000379 SourceMgr BlockSM;
380 BlockSM.AddNewSourceBuffer(
381 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
382 SMLoc());
383 PFS.SM = &BlockSM;
384 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000385 reportDiagnostic(
386 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
387 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000388 }
Matthias Braune35861d2016-07-13 23:27:50 +0000389 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000390
Alex Lorenza6f9a372015-07-29 21:09:09 +0000391 // Initialize the frame information after creating all the MBBs so that the
392 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000393 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000394 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000395 // Initialize the jump table after creating all the MBBs so that the MBB
396 // references can be resolved.
397 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000398 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000399 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000400 // Parse the machine instructions after creating all of the MBBs so that the
401 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000402 StringRef InsnStr = YamlMF.Body.Value.Value;
403 SourceMgr InsnSM;
404 InsnSM.AddNewSourceBuffer(
405 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
406 SMLoc());
407 PFS.SM = &InsnSM;
408 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000409 reportDiagnostic(
410 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
411 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000412 }
Matthias Braune35861d2016-07-13 23:27:50 +0000413 PFS.SM = &SM;
414
Matthias Braun74ad41c2016-10-11 03:13:01 +0000415 if (setupRegisterInfo(PFS, YamlMF))
416 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000417
418 computeFunctionProperties(MF);
419
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000420 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000421 return false;
422}
423
Matthias Braun74ad41c2016-10-11 03:13:01 +0000424bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
425 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000426 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000427 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000428 assert(RegInfo.tracksLiveness());
429 if (!YamlMF.TracksRegLiveness)
430 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000431
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000432 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000433 // Parse the virtual register information.
434 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000435 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
436 if (Info.Explicit)
437 return error(VReg.ID.SourceRange.Start,
438 Twine("redefinition of virtual register '%") +
439 Twine(VReg.ID.Value) + "'");
440 Info.Explicit = true;
441
Quentin Colombet050b2112016-03-08 01:17:03 +0000442 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000443 Info.Kind = VRegInfo::GENERIC;
Quentin Colombet050b2112016-03-08 01:17:03 +0000444 } else {
445 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000446 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000447 Info.Kind = VRegInfo::NORMAL;
448 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000449 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000450 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000451 if (!RegBank)
452 return error(
453 VReg.Class.SourceRange.Start,
454 Twine("use of undefined register class or register bank '") +
455 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000456 Info.Kind = VRegInfo::REGBANK;
457 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000458 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000459 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000460
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000461 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000462 if (Info.Kind != VRegInfo::NORMAL)
463 return error(VReg.Class.SourceRange.Start,
464 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000465
466 if (parseRegisterReference(PFS, Info.PreferredReg,
467 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000468 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000469 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000470 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000471
472 // Parse the liveins.
473 for (const auto &LiveIn : YamlMF.LiveIns) {
474 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000475 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000476 return error(Error, LiveIn.Register.SourceRange);
477 unsigned VReg = 0;
478 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000479 VRegInfo *Info;
480 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000481 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000482 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000483 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000484 }
485 RegInfo.addLiveIn(Reg, VReg);
486 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000487
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000488 // Parse the callee saved registers (Registers that will
489 // be saved for the caller).
490 if (YamlMF.CalleeSavedRegisters) {
491 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
492 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
493 unsigned Reg = 0;
494 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
495 return error(Error, RegSource.SourceRange);
496 CalleeSavedRegisters.push_back(Reg);
497 }
498 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000499 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000500
Alex Lorenz54565cf2015-06-24 19:56:10 +0000501 return false;
502}
503
Matthias Braun74ad41c2016-10-11 03:13:01 +0000504bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000505 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000506 MachineFunction &MF = PFS.MF;
507 MachineRegisterInfo &MRI = MF.getRegInfo();
508 bool Error = false;
509 // Create VRegs
510 for (auto P : PFS.VRegInfos) {
511 const VRegInfo &Info = *P.second;
512 unsigned Reg = Info.VReg;
513 switch (Info.Kind) {
514 case VRegInfo::UNKNOWN:
515 error(Twine("Cannot determine class/bank of virtual register ") +
516 Twine(P.first) + " in function '" + MF.getName() + "'");
517 Error = true;
518 break;
519 case VRegInfo::NORMAL:
520 MRI.setRegClass(Reg, Info.D.RC);
521 if (Info.PreferredReg != 0)
522 MRI.setSimpleHint(Reg, Info.PreferredReg);
523 break;
524 case VRegInfo::GENERIC:
525 break;
526 case VRegInfo::REGBANK:
527 MRI.setRegBank(Reg, *Info.D.RegBank);
528 break;
529 }
530 }
531
532 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000533 for (const MachineBasicBlock &MBB : MF) {
534 for (const MachineInstr &MI : MBB) {
535 for (const MachineOperand &MO : MI.operands()) {
536 if (!MO.isRegMask())
537 continue;
538 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000539 }
540 }
541 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000542
543 // FIXME: This is a temporary workaround until the reserved registers can be
544 // serialized.
545 MRI.freezeReservedRegs(MF);
546 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000547}
548
Matthias Braun83947862016-07-13 22:23:23 +0000549bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
550 const yaml::MachineFunction &YamlMF) {
551 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000552 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000553 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000554 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000555 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
556 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
557 MFI.setHasStackMap(YamlMFI.HasStackMap);
558 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
559 MFI.setStackSize(YamlMFI.StackSize);
560 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
561 if (YamlMFI.MaxAlignment)
562 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
563 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
564 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000565 if (YamlMFI.MaxCallFrameSize != ~0u)
566 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Alex Lorenz60541c12015-07-09 19:55:27 +0000567 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
568 MFI.setHasVAStart(YamlMFI.HasVAStart);
569 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000570 if (!YamlMFI.SavePoint.Value.empty()) {
571 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000572 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000573 return true;
574 MFI.setSavePoint(MBB);
575 }
576 if (!YamlMFI.RestorePoint.Value.empty()) {
577 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000578 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000579 return true;
580 MFI.setRestorePoint(MBB);
581 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000582
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000583 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000584 // Initialize the fixed frame objects.
585 for (const auto &Object : YamlMF.FixedStackObjects) {
586 int ObjectIdx;
587 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
588 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
589 Object.IsImmutable, Object.IsAliased);
590 else
591 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
592 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000593 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000594 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
595 ObjectIdx))
596 .second)
597 return error(Object.ID.SourceRange.Start,
598 Twine("redefinition of fixed stack object '%fixed-stack.") +
599 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000600 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000601 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000602 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000603 }
604
605 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000606 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000607 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000608 const AllocaInst *Alloca = nullptr;
609 const yaml::StringValue &Name = Object.Name;
610 if (!Name.Value.empty()) {
611 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000612 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000613 if (!Alloca)
614 return error(Name.SourceRange.Start,
615 "alloca instruction named '" + Name.Value +
616 "' isn't defined in the function '" + F.getName() +
617 "'");
618 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000619 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000620 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000621 else
622 ObjectIdx = MFI.CreateStackObject(
623 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000624 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000625 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000626 MFI.setStackID(ObjectIdx, Object.StackID);
627
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000628 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
629 .second)
630 return error(Object.ID.SourceRange.Start,
631 Twine("redefinition of stack object '%stack.") +
632 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000633 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000634 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000635 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000636 if (Object.LocalOffset)
637 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000638 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000639 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000640 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000641 MFI.setCalleeSavedInfo(CSIInfo);
642 if (!CSIInfo.empty())
643 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000644
645 // Initialize the various stack object references after initializing the
646 // stack objects.
647 if (!YamlMFI.StackProtector.Value.empty()) {
648 SMDiagnostic Error;
649 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000650 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000651 return error(Error, YamlMFI.StackProtector.SourceRange);
652 MFI.setStackProtectorIndex(FI);
653 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000654 return false;
655}
656
Matthias Braun83947862016-07-13 22:23:23 +0000657bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000658 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000659 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000660 if (RegisterSource.Value.empty())
661 return false;
662 unsigned Reg = 0;
663 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000664 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000665 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000666 CalleeSavedInfo CSI(Reg, FrameIdx);
667 CSI.setRestored(IsRestored);
668 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000669 return false;
670}
671
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000672/// Verify that given node is of a certain type. Return true on error.
673template <typename T>
674static bool typecheckMDNode(T *&Result, MDNode *Node,
675 const yaml::StringValue &Source,
676 StringRef TypeString, MIRParserImpl &Parser) {
677 if (!Node)
678 return false;
679 Result = dyn_cast<T>(Node);
680 if (!Result)
681 return Parser.error(Source.SourceRange.Start,
682 "expected a reference to a '" + TypeString +
683 "' metadata node");
684 return false;
685}
686
Matthias Braun83947862016-07-13 22:23:23 +0000687bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000688 const yaml::MachineStackObject &Object, int FrameIdx) {
689 // Debug information can only be attached to stack objects; Fixed stack
690 // objects aren't supported.
691 assert(FrameIdx >= 0 && "Expected a stack object frame index");
692 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000693 if (parseMDNode(PFS, Var, Object.DebugVar) ||
694 parseMDNode(PFS, Expr, Object.DebugExpr) ||
695 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000696 return true;
697 if (!Var && !Expr && !Loc)
698 return false;
699 DILocalVariable *DIVar = nullptr;
700 DIExpression *DIExpr = nullptr;
701 DILocation *DILoc = nullptr;
702 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
703 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
704 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
705 return true;
Matthias Braunef331ef2016-11-30 23:48:50 +0000706 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000707 return false;
708}
709
Matthias Braun74ad41c2016-10-11 03:13:01 +0000710bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000711 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000712 if (Source.Value.empty())
713 return false;
714 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000715 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000716 return error(Error, Source.SourceRange);
717 return false;
718}
719
Matthias Braun83947862016-07-13 22:23:23 +0000720bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
721 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
722 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
723 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000724 const auto &M = *MF.getFunction()->getParent();
725 SMDiagnostic Error;
726 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000727 if (YamlConstant.IsTargetSpecific)
728 // FIXME: Support target-specific constant pools
729 return error(YamlConstant.Value.SourceRange.Start,
730 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000731 const Constant *Value = dyn_cast_or_null<Constant>(
732 parseConstantValue(YamlConstant.Value.Value, Error, M));
733 if (!Value)
734 return error(Error, YamlConstant.Value.SourceRange);
735 unsigned Alignment =
736 YamlConstant.Alignment
737 ? YamlConstant.Alignment
738 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000739 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
740 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
741 .second)
742 return error(YamlConstant.ID.SourceRange.Start,
743 Twine("redefinition of constant pool item '%const.") +
744 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000745 }
746 return false;
747}
748
Matthias Braun83947862016-07-13 22:23:23 +0000749bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
750 const yaml::MachineJumpTable &YamlJTI) {
751 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000752 for (const auto &Entry : YamlJTI.Entries) {
753 std::vector<MachineBasicBlock *> Blocks;
754 for (const auto &MBBSource : Entry.Blocks) {
755 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000756 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000757 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000758 Blocks.push_back(MBB);
759 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000760 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000761 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
762 .second)
763 return error(Entry.ID.SourceRange.Start,
764 Twine("redefinition of jump table entry '%jump-table.") +
765 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000766 }
767 return false;
768}
769
Matthias Braun74ad41c2016-10-11 03:13:01 +0000770bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000771 MachineBasicBlock *&MBB,
772 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000773 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000774 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000775 return error(Error, Source.SourceRange);
776 return false;
777}
778
Alex Lorenz51af1602015-06-23 22:39:23 +0000779SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
780 SMRange SourceRange) {
781 assert(SourceRange.isValid() && "Invalid source range");
782 SMLoc Loc = SourceRange.Start;
783 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
784 *Loc.getPointer() == '\'';
785 // Translate the location of the error from the location in the MI string to
786 // the corresponding location in the MIR file.
787 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
788 (HasQuote ? 1 : 0));
789
790 // TODO: Translate any source ranges as well.
791 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
792 Error.getFixIts());
793}
794
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000795SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
796 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000797 assert(SourceRange.isValid());
798
799 // Translate the location of the error from the location in the llvm IR string
800 // to the corresponding location in the MIR file.
801 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
802 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
803 unsigned Column = Error.getColumnNo();
804 StringRef LineStr = Error.getLineContents();
805 SMLoc Loc = Error.getLoc();
806
807 // Get the full line and adjust the column number by taking the indentation of
808 // LLVM IR into account.
809 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
810 L != E; ++L) {
811 if (L.line_number() == Line) {
812 LineStr = *L;
813 Loc = SMLoc::getFromPointer(LineStr.data());
814 auto Indent = LineStr.find(Error.getLineContents());
815 if (Indent != StringRef::npos)
816 Column += Indent;
817 break;
818 }
819 }
820
821 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
822 Error.getMessage(), LineStr, Error.getRanges(),
823 Error.getFixIts());
824}
825
Alex Lorenz28148ba2015-07-09 22:23:13 +0000826void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
827 if (!Names2RegClasses.empty())
828 return;
829 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
830 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
831 const auto *RC = TRI->getRegClass(I);
832 Names2RegClasses.insert(
833 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
834 }
835}
836
Quentin Colombet876ddf82016-04-08 16:40:43 +0000837void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
838 if (!Names2RegBanks.empty())
839 return;
840 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
841 // If the target does not support GlobalISel, we may not have a
842 // register bank info.
843 if (!RBI)
844 return;
845 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
846 const auto &RegBank = RBI->getRegBank(I);
847 Names2RegBanks.insert(
848 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
849 }
850}
851
Alex Lorenz28148ba2015-07-09 22:23:13 +0000852const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
853 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000854 auto RegClassInfo = Names2RegClasses.find(Name);
855 if (RegClassInfo == Names2RegClasses.end())
856 return nullptr;
857 return RegClassInfo->getValue();
858}
859
Quentin Colombet876ddf82016-04-08 16:40:43 +0000860const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
861 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000862 auto RegBankInfo = Names2RegBanks.find(Name);
863 if (RegBankInfo == Names2RegBanks.end())
864 return nullptr;
865 return RegBankInfo->getValue();
866}
867
Alex Lorenz735c47e2015-06-15 20:30:22 +0000868MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
869 : Impl(std::move(Impl)) {}
870
871MIRParser::~MIRParser() {}
872
Matthias Braun7bda1952017-06-06 00:44:35 +0000873std::unique_ptr<Module> MIRParser::parseIRModule() {
874 return Impl->parseIRModule();
875}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000876
Matthias Braun7bda1952017-06-06 00:44:35 +0000877bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
878 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000879}
880
881std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
882 SMDiagnostic &Error,
883 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000884 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000885 if (std::error_code EC = FileOrErr.getError()) {
886 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
887 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000888 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000889 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000890 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000891}
892
Alex Lorenz735c47e2015-06-15 20:30:22 +0000893std::unique_ptr<MIRParser>
894llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
895 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000896 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000897 if (Context.shouldDiscardValueNames()) {
898 Context.diagnose(DiagnosticInfoMIRParser(
899 DS_Error,
900 SMDiagnostic(
901 Filename, SourceMgr::DK_Error,
902 "Can't read MIR with a Context that discards named Values")));
903 return nullptr;
904 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000905 return llvm::make_unique<MIRParser>(
906 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000907}