blob: db56696391804ec44383a1373bae1e309a346000 [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;
217 }
218 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
219}
220
Matthias Braun7bda1952017-06-06 00:44:35 +0000221std::unique_ptr<Module> MIRParserImpl::parseIRModule() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000222 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000223 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000224 return nullptr;
225 // Create an empty module when the MIR file is empty.
Matthias Braun7bda1952017-06-06 00:44:35 +0000226 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000227 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000228 }
229
Alex Lorenz78d78312015-05-28 22:41:12 +0000230 std::unique_ptr<Module> M;
231 // Parse the block scalar manually so that we can return unique pointer
232 // without having to go trough YAML traits.
233 if (const auto *BSN =
234 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000235 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000236 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000237 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000238 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000239 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000240 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000241 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000242 In.nextDocument();
243 if (!In.setCurrentDocument())
Matthias Braun7bda1952017-06-06 00:44:35 +0000244 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000245 } else {
246 // Create an new, empty module.
247 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000248 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000249 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000250 return M;
251}
252
Matthias Braun7bda1952017-06-06 00:44:35 +0000253bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
254 if (NoMIRDocuments)
255 return false;
256
257 // Parse the machine functions.
258 do {
259 if (parseMachineFunction(M, MMI))
260 return true;
261 In.nextDocument();
262 } while (In.setCurrentDocument());
263
Alex Lorenz735c47e2015-06-15 20:30:22 +0000264 return false;
265}
266
Matthias Braun7bda1952017-06-06 00:44:35 +0000267/// Create an empty function with the given name.
268static Function *createDummyFunction(StringRef Name, Module &M) {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000269 auto &Context = M.getContext();
270 Function *F = cast<Function>(M.getOrInsertFunction(
271 Name, FunctionType::get(Type::getVoidTy(Context), false)));
272 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
273 new UnreachableInst(Context, BB);
Matthias Braun7bda1952017-06-06 00:44:35 +0000274 return F;
275}
276
277bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
278 // Parse the yaml.
279 yaml::MachineFunction YamlMF;
280 yaml::EmptyContext Ctx;
281 yaml::yamlize(In, YamlMF, false, Ctx);
282 if (In.error())
283 return true;
284
285 // Search for the corresponding IR function.
286 StringRef FunctionName = YamlMF.Name;
287 Function *F = M.getFunction(FunctionName);
288 if (!F) {
289 if (NoLLVMIR) {
290 F = createDummyFunction(FunctionName, M);
291 } else {
292 return error(Twine("function '") + FunctionName +
293 "' isn't defined in the provided LLVM IR");
294 }
295 }
296 if (MMI.getMachineFunction(*F) != nullptr)
297 return error(Twine("redefinition of machine function '") + FunctionName +
298 "'");
299
300 // Create the MachineFunction.
301 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
302 if (initializeMachineFunction(YamlMF, MF))
303 return true;
304
305 return false;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000306}
307
Matthias Braun79f85b32016-08-24 01:32:41 +0000308static bool isSSA(const MachineFunction &MF) {
309 const MachineRegisterInfo &MRI = MF.getRegInfo();
310 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
311 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
312 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
313 return false;
314 }
315 return true;
316}
317
Matthias Braun90799ce2016-08-23 21:19:49 +0000318void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000319 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000320
321 bool HasPHI = false;
322 bool HasInlineAsm = false;
323 for (const MachineBasicBlock &MBB : MF) {
324 for (const MachineInstr &MI : MBB) {
325 if (MI.isPHI())
326 HasPHI = true;
327 if (MI.isInlineAsm())
328 HasInlineAsm = true;
329 }
330 }
331 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000332 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000333 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000334
335 if (isSSA(MF))
336 Properties.set(MachineFunctionProperties::Property::IsSSA);
337 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000338 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000339
340 const MachineRegisterInfo &MRI = MF.getRegInfo();
341 if (MRI.getNumVirtRegs() == 0)
342 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000343}
344
Matthias Braun7bda1952017-06-06 00:44:35 +0000345bool
346MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
347 MachineFunction &MF) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000348 // TODO: Recreate the machine function.
Matthias Braunde5fea22017-01-18 00:59:19 +0000349 initNames2RegClasses(MF);
350 initNames2RegBanks(MF);
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000351 if (YamlMF.Alignment)
352 MF.setAlignment(YamlMF.Alignment);
353 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000354
355 if (YamlMF.Legalized)
356 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000357 if (YamlMF.RegBankSelected)
358 MF.getProperties().set(
359 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000360 if (YamlMF.Selected)
361 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000362
Matthias Braunde5fea22017-01-18 00:59:19 +0000363 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
364 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000365 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000366 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000367 if (!YamlMF.Constants.empty()) {
368 auto *ConstantPool = MF.getConstantPool();
369 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000370 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000371 return true;
372 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000373
Matthias Braune35861d2016-07-13 23:27:50 +0000374 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000375 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000376 SourceMgr BlockSM;
377 BlockSM.AddNewSourceBuffer(
378 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
379 SMLoc());
380 PFS.SM = &BlockSM;
381 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000382 reportDiagnostic(
383 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
384 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000385 }
Matthias Braune35861d2016-07-13 23:27:50 +0000386 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000387
Alex Lorenza6f9a372015-07-29 21:09:09 +0000388 // Initialize the frame information after creating all the MBBs so that the
389 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000390 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000391 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000392 // Initialize the jump table after creating all the MBBs so that the MBB
393 // references can be resolved.
394 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000395 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000396 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000397 // Parse the machine instructions after creating all of the MBBs so that the
398 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000399 StringRef InsnStr = YamlMF.Body.Value.Value;
400 SourceMgr InsnSM;
401 InsnSM.AddNewSourceBuffer(
402 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
403 SMLoc());
404 PFS.SM = &InsnSM;
405 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000406 reportDiagnostic(
407 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
408 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000409 }
Matthias Braune35861d2016-07-13 23:27:50 +0000410 PFS.SM = &SM;
411
Matthias Braun74ad41c2016-10-11 03:13:01 +0000412 if (setupRegisterInfo(PFS, YamlMF))
413 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000414
415 computeFunctionProperties(MF);
416
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000417 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000418 return false;
419}
420
Matthias Braun74ad41c2016-10-11 03:13:01 +0000421bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
422 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000423 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000424 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000425 assert(RegInfo.tracksLiveness());
426 if (!YamlMF.TracksRegLiveness)
427 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000428
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000429 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000430 // Parse the virtual register information.
431 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000432 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
433 if (Info.Explicit)
434 return error(VReg.ID.SourceRange.Start,
435 Twine("redefinition of virtual register '%") +
436 Twine(VReg.ID.Value) + "'");
437 Info.Explicit = true;
438
Quentin Colombet050b2112016-03-08 01:17:03 +0000439 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000440 Info.Kind = VRegInfo::GENERIC;
Quentin Colombet050b2112016-03-08 01:17:03 +0000441 } else {
442 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000443 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000444 Info.Kind = VRegInfo::NORMAL;
445 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000446 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000447 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000448 if (!RegBank)
449 return error(
450 VReg.Class.SourceRange.Start,
451 Twine("use of undefined register class or register bank '") +
452 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000453 Info.Kind = VRegInfo::REGBANK;
454 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000455 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000456 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000457
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000458 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000459 if (Info.Kind != VRegInfo::NORMAL)
460 return error(VReg.Class.SourceRange.Start,
461 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000462
463 if (parseRegisterReference(PFS, Info.PreferredReg,
464 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000465 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000466 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000467 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000468
469 // Parse the liveins.
470 for (const auto &LiveIn : YamlMF.LiveIns) {
471 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000472 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000473 return error(Error, LiveIn.Register.SourceRange);
474 unsigned VReg = 0;
475 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000476 VRegInfo *Info;
477 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000478 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000479 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000480 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000481 }
482 RegInfo.addLiveIn(Reg, VReg);
483 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000484
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000485 // Parse the callee saved registers (Registers that will
486 // be saved for the caller).
487 if (YamlMF.CalleeSavedRegisters) {
488 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
489 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
490 unsigned Reg = 0;
491 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
492 return error(Error, RegSource.SourceRange);
493 CalleeSavedRegisters.push_back(Reg);
494 }
495 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000496 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000497
Alex Lorenz54565cf2015-06-24 19:56:10 +0000498 return false;
499}
500
Matthias Braun74ad41c2016-10-11 03:13:01 +0000501bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000502 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000503 MachineFunction &MF = PFS.MF;
504 MachineRegisterInfo &MRI = MF.getRegInfo();
505 bool Error = false;
506 // Create VRegs
507 for (auto P : PFS.VRegInfos) {
508 const VRegInfo &Info = *P.second;
509 unsigned Reg = Info.VReg;
510 switch (Info.Kind) {
511 case VRegInfo::UNKNOWN:
512 error(Twine("Cannot determine class/bank of virtual register ") +
513 Twine(P.first) + " in function '" + MF.getName() + "'");
514 Error = true;
515 break;
516 case VRegInfo::NORMAL:
517 MRI.setRegClass(Reg, Info.D.RC);
518 if (Info.PreferredReg != 0)
519 MRI.setSimpleHint(Reg, Info.PreferredReg);
520 break;
521 case VRegInfo::GENERIC:
522 break;
523 case VRegInfo::REGBANK:
524 MRI.setRegBank(Reg, *Info.D.RegBank);
525 break;
526 }
527 }
528
529 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000530 for (const MachineBasicBlock &MBB : MF) {
531 for (const MachineInstr &MI : MBB) {
532 for (const MachineOperand &MO : MI.operands()) {
533 if (!MO.isRegMask())
534 continue;
535 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000536 }
537 }
538 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000539
540 // FIXME: This is a temporary workaround until the reserved registers can be
541 // serialized.
542 MRI.freezeReservedRegs(MF);
543 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000544}
545
Matthias Braun83947862016-07-13 22:23:23 +0000546bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
547 const yaml::MachineFunction &YamlMF) {
548 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000549 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000550 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000551 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000552 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
553 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
554 MFI.setHasStackMap(YamlMFI.HasStackMap);
555 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
556 MFI.setStackSize(YamlMFI.StackSize);
557 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
558 if (YamlMFI.MaxAlignment)
559 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
560 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
561 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000562 if (YamlMFI.MaxCallFrameSize != ~0u)
563 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Alex Lorenz60541c12015-07-09 19:55:27 +0000564 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
565 MFI.setHasVAStart(YamlMFI.HasVAStart);
566 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000567 if (!YamlMFI.SavePoint.Value.empty()) {
568 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000569 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000570 return true;
571 MFI.setSavePoint(MBB);
572 }
573 if (!YamlMFI.RestorePoint.Value.empty()) {
574 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000575 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000576 return true;
577 MFI.setRestorePoint(MBB);
578 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000579
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000580 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000581 // Initialize the fixed frame objects.
582 for (const auto &Object : YamlMF.FixedStackObjects) {
583 int ObjectIdx;
584 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
585 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
586 Object.IsImmutable, Object.IsAliased);
587 else
588 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
589 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000590 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000591 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
592 ObjectIdx))
593 .second)
594 return error(Object.ID.SourceRange.Start,
595 Twine("redefinition of fixed stack object '%fixed-stack.") +
596 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000597 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000598 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000599 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000600 }
601
602 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000603 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000604 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000605 const AllocaInst *Alloca = nullptr;
606 const yaml::StringValue &Name = Object.Name;
607 if (!Name.Value.empty()) {
608 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000609 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000610 if (!Alloca)
611 return error(Name.SourceRange.Start,
612 "alloca instruction named '" + Name.Value +
613 "' isn't defined in the function '" + F.getName() +
614 "'");
615 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000616 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000617 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000618 else
619 ObjectIdx = MFI.CreateStackObject(
620 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000621 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000622 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000623 MFI.setStackID(ObjectIdx, Object.StackID);
624
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000625 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
626 .second)
627 return error(Object.ID.SourceRange.Start,
628 Twine("redefinition of stack object '%stack.") +
629 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000630 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000631 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000632 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000633 if (Object.LocalOffset)
634 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000635 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000636 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000637 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000638 MFI.setCalleeSavedInfo(CSIInfo);
639 if (!CSIInfo.empty())
640 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000641
642 // Initialize the various stack object references after initializing the
643 // stack objects.
644 if (!YamlMFI.StackProtector.Value.empty()) {
645 SMDiagnostic Error;
646 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000647 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000648 return error(Error, YamlMFI.StackProtector.SourceRange);
649 MFI.setStackProtectorIndex(FI);
650 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000651 return false;
652}
653
Matthias Braun83947862016-07-13 22:23:23 +0000654bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000655 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000656 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000657 if (RegisterSource.Value.empty())
658 return false;
659 unsigned Reg = 0;
660 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000661 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000662 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000663 CalleeSavedInfo CSI(Reg, FrameIdx);
664 CSI.setRestored(IsRestored);
665 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000666 return false;
667}
668
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000669/// Verify that given node is of a certain type. Return true on error.
670template <typename T>
671static bool typecheckMDNode(T *&Result, MDNode *Node,
672 const yaml::StringValue &Source,
673 StringRef TypeString, MIRParserImpl &Parser) {
674 if (!Node)
675 return false;
676 Result = dyn_cast<T>(Node);
677 if (!Result)
678 return Parser.error(Source.SourceRange.Start,
679 "expected a reference to a '" + TypeString +
680 "' metadata node");
681 return false;
682}
683
Matthias Braun83947862016-07-13 22:23:23 +0000684bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000685 const yaml::MachineStackObject &Object, int FrameIdx) {
686 // Debug information can only be attached to stack objects; Fixed stack
687 // objects aren't supported.
688 assert(FrameIdx >= 0 && "Expected a stack object frame index");
689 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000690 if (parseMDNode(PFS, Var, Object.DebugVar) ||
691 parseMDNode(PFS, Expr, Object.DebugExpr) ||
692 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000693 return true;
694 if (!Var && !Expr && !Loc)
695 return false;
696 DILocalVariable *DIVar = nullptr;
697 DIExpression *DIExpr = nullptr;
698 DILocation *DILoc = nullptr;
699 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
700 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
701 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
702 return true;
Matthias Braunef331ef2016-11-30 23:48:50 +0000703 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000704 return false;
705}
706
Matthias Braun74ad41c2016-10-11 03:13:01 +0000707bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000708 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000709 if (Source.Value.empty())
710 return false;
711 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000712 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000713 return error(Error, Source.SourceRange);
714 return false;
715}
716
Matthias Braun83947862016-07-13 22:23:23 +0000717bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
718 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
719 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
720 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000721 const auto &M = *MF.getFunction()->getParent();
722 SMDiagnostic Error;
723 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000724 if (YamlConstant.IsTargetSpecific)
725 // FIXME: Support target-specific constant pools
726 return error(YamlConstant.Value.SourceRange.Start,
727 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000728 const Constant *Value = dyn_cast_or_null<Constant>(
729 parseConstantValue(YamlConstant.Value.Value, Error, M));
730 if (!Value)
731 return error(Error, YamlConstant.Value.SourceRange);
732 unsigned Alignment =
733 YamlConstant.Alignment
734 ? YamlConstant.Alignment
735 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000736 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
737 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
738 .second)
739 return error(YamlConstant.ID.SourceRange.Start,
740 Twine("redefinition of constant pool item '%const.") +
741 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000742 }
743 return false;
744}
745
Matthias Braun83947862016-07-13 22:23:23 +0000746bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
747 const yaml::MachineJumpTable &YamlJTI) {
748 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000749 for (const auto &Entry : YamlJTI.Entries) {
750 std::vector<MachineBasicBlock *> Blocks;
751 for (const auto &MBBSource : Entry.Blocks) {
752 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000753 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000754 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000755 Blocks.push_back(MBB);
756 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000757 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000758 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
759 .second)
760 return error(Entry.ID.SourceRange.Start,
761 Twine("redefinition of jump table entry '%jump-table.") +
762 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000763 }
764 return false;
765}
766
Matthias Braun74ad41c2016-10-11 03:13:01 +0000767bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000768 MachineBasicBlock *&MBB,
769 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000770 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000771 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000772 return error(Error, Source.SourceRange);
773 return false;
774}
775
Alex Lorenz51af1602015-06-23 22:39:23 +0000776SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
777 SMRange SourceRange) {
778 assert(SourceRange.isValid() && "Invalid source range");
779 SMLoc Loc = SourceRange.Start;
780 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
781 *Loc.getPointer() == '\'';
782 // Translate the location of the error from the location in the MI string to
783 // the corresponding location in the MIR file.
784 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
785 (HasQuote ? 1 : 0));
786
787 // TODO: Translate any source ranges as well.
788 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
789 Error.getFixIts());
790}
791
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000792SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
793 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000794 assert(SourceRange.isValid());
795
796 // Translate the location of the error from the location in the llvm IR string
797 // to the corresponding location in the MIR file.
798 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
799 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
800 unsigned Column = Error.getColumnNo();
801 StringRef LineStr = Error.getLineContents();
802 SMLoc Loc = Error.getLoc();
803
804 // Get the full line and adjust the column number by taking the indentation of
805 // LLVM IR into account.
806 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
807 L != E; ++L) {
808 if (L.line_number() == Line) {
809 LineStr = *L;
810 Loc = SMLoc::getFromPointer(LineStr.data());
811 auto Indent = LineStr.find(Error.getLineContents());
812 if (Indent != StringRef::npos)
813 Column += Indent;
814 break;
815 }
816 }
817
818 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
819 Error.getMessage(), LineStr, Error.getRanges(),
820 Error.getFixIts());
821}
822
Alex Lorenz28148ba2015-07-09 22:23:13 +0000823void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
824 if (!Names2RegClasses.empty())
825 return;
826 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
827 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
828 const auto *RC = TRI->getRegClass(I);
829 Names2RegClasses.insert(
830 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
831 }
832}
833
Quentin Colombet876ddf82016-04-08 16:40:43 +0000834void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
835 if (!Names2RegBanks.empty())
836 return;
837 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
838 // If the target does not support GlobalISel, we may not have a
839 // register bank info.
840 if (!RBI)
841 return;
842 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
843 const auto &RegBank = RBI->getRegBank(I);
844 Names2RegBanks.insert(
845 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
846 }
847}
848
Alex Lorenz28148ba2015-07-09 22:23:13 +0000849const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
850 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000851 auto RegClassInfo = Names2RegClasses.find(Name);
852 if (RegClassInfo == Names2RegClasses.end())
853 return nullptr;
854 return RegClassInfo->getValue();
855}
856
Quentin Colombet876ddf82016-04-08 16:40:43 +0000857const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
858 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000859 auto RegBankInfo = Names2RegBanks.find(Name);
860 if (RegBankInfo == Names2RegBanks.end())
861 return nullptr;
862 return RegBankInfo->getValue();
863}
864
Alex Lorenz735c47e2015-06-15 20:30:22 +0000865MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
866 : Impl(std::move(Impl)) {}
867
868MIRParser::~MIRParser() {}
869
Matthias Braun7bda1952017-06-06 00:44:35 +0000870std::unique_ptr<Module> MIRParser::parseIRModule() {
871 return Impl->parseIRModule();
872}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000873
Matthias Braun7bda1952017-06-06 00:44:35 +0000874bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
875 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000876}
877
878std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
879 SMDiagnostic &Error,
880 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000881 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000882 if (std::error_code EC = FileOrErr.getError()) {
883 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
884 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000885 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000886 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000887 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000888}
889
Alex Lorenz735c47e2015-06-15 20:30:22 +0000890std::unique_ptr<MIRParser>
891llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
892 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000893 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000894 if (Context.shouldDiscardValueNames()) {
895 Context.diagnose(DiagnosticInfoMIRParser(
896 DS_Error,
897 SMDiagnostic(
898 Filename, SourceMgr::DK_Error,
899 "Can't read MIR with a Context that discards named Values")));
900 return nullptr;
901 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000902 return llvm::make_unique<MIRParser>(
903 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000904}