blob: bd04acd049dbaeda58b273c60b01b7a39f823308 [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;
53 StringRef Filename;
54 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000055 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
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;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000061
62public:
63 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
64 LLVMContext &Context);
65
Alex Lorenz735c47e2015-06-15 20:30:22 +000066 void reportDiagnostic(const SMDiagnostic &Diag);
67
68 /// Report an error with the given message at unknown location.
69 ///
70 /// Always returns true.
71 bool error(const Twine &Message);
72
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000073 /// Report an error with the given message at the given location.
74 ///
75 /// Always returns true.
76 bool error(SMLoc Loc, const Twine &Message);
77
Alex Lorenz0fd7c622015-06-30 17:55:00 +000078 /// Report a given error with the location translated from the location in an
79 /// embedded string literal to a location in the MIR file.
80 ///
81 /// Always returns true.
82 bool error(const SMDiagnostic &Error, SMRange SourceRange);
83
Alex Lorenz78d78312015-05-28 22:41:12 +000084 /// Try to parse the optional LLVM module and the machine functions in the MIR
85 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000086 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000087 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000088 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000089
90 /// Parse the machine function in the current YAML document.
91 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000092 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
93 /// A dummy IR function is created and inserted into the given module when
94 /// this parameter is true.
95 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000096 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000097 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000098
Alex Lorenz735c47e2015-06-15 20:30:22 +000099 /// Initialize the machine function to the state that's described in the MIR
100 /// file.
101 ///
102 /// Return true if error occurred.
103 bool initializeMachineFunction(MachineFunction &MF);
104
Matthias Braun74ad41c2016-10-11 03:13:01 +0000105 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
106 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000107
Matthias Braun74ad41c2016-10-11 03:13:01 +0000108 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000109 const yaml::MachineFunction &YamlMF);
110
Matthias Braun83947862016-07-13 22:23:23 +0000111 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
112 const yaml::MachineFunction &YamlMF);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000113
Matthias Braun83947862016-07-13 22:23:23 +0000114 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000115 std::vector<CalleeSavedInfo> &CSIInfo,
116 const yaml::StringValue &RegisterSource,
117 int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000118
Matthias Braun83947862016-07-13 22:23:23 +0000119 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000120 const yaml::MachineStackObject &Object,
121 int FrameIdx);
122
Matthias Braun83947862016-07-13 22:23:23 +0000123 bool initializeConstantPool(PerFunctionMIParsingState &PFS,
124 MachineConstantPool &ConstantPool,
125 const yaml::MachineFunction &YamlMF);
Alex Lorenzab980492015-07-20 20:51:18 +0000126
Matthias Braun83947862016-07-13 22:23:23 +0000127 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
128 const yaml::MachineJumpTable &YamlJTI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000129
Alex Lorenz09b832c2015-05-29 17:05:41 +0000130private:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000131 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
Matthias Braun83947862016-07-13 22:23:23 +0000132 const yaml::StringValue &Source);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000133
Matthias Braun74ad41c2016-10-11 03:13:01 +0000134 bool parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000135 MachineBasicBlock *&MBB,
136 const yaml::StringValue &Source);
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000137
Alex Lorenz51af1602015-06-23 22:39:23 +0000138 /// Return a MIR diagnostic converted from an MI string diagnostic.
139 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
140 SMRange SourceRange);
141
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000142 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
143 /// block scalar string.
144 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
145 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000146
147 /// Create an empty function with the given name.
148 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000149
150 void initNames2RegClasses(const MachineFunction &MF);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000151 void initNames2RegBanks(const MachineFunction &MF);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000152
153 /// Check if the given identifier is a name of a register class.
154 ///
155 /// Return null if the name isn't a register class.
156 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
157 StringRef Name);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000158
159 /// Check if the given identifier is a name of a register bank.
160 ///
161 /// Return null if the name isn't a register bank.
162 const RegisterBank *getRegBank(const MachineFunction &MF, StringRef Name);
Matthias Braun90799ce2016-08-23 21:19:49 +0000163
164 void computeFunctionProperties(MachineFunction &MF);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000165};
166
Alex Lorenz735c47e2015-06-15 20:30:22 +0000167} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000168
169MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
170 StringRef Filename, LLVMContext &Context)
171 : SM(), Filename(Filename), Context(Context) {
172 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
173}
174
Alex Lorenz735c47e2015-06-15 20:30:22 +0000175bool MIRParserImpl::error(const Twine &Message) {
176 Context.diagnose(DiagnosticInfoMIRParser(
177 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
178 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000179}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000180
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000181bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
182 Context.diagnose(DiagnosticInfoMIRParser(
183 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
184 return true;
185}
186
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000187bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
188 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
189 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
190 return true;
191}
192
Alex Lorenz735c47e2015-06-15 20:30:22 +0000193void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
194 DiagnosticSeverity Kind;
195 switch (Diag.getKind()) {
196 case SourceMgr::DK_Error:
197 Kind = DS_Error;
198 break;
199 case SourceMgr::DK_Warning:
200 Kind = DS_Warning;
201 break;
202 case SourceMgr::DK_Note:
203 Kind = DS_Note;
204 break;
205 }
206 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
207}
208
209static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
210 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
211}
212
213std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000214 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000215 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000216 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000217
218 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000219 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000220 return nullptr;
221 // Create an empty module when the MIR file is empty.
222 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000223 }
224
Alex Lorenz78d78312015-05-28 22:41:12 +0000225 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000226 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000227 // Parse the block scalar manually so that we can return unique pointer
228 // without having to go trough YAML traits.
229 if (const auto *BSN =
230 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000231 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000232 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000233 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000234 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000235 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000236 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000237 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000238 In.nextDocument();
239 if (!In.setCurrentDocument())
240 return M;
241 } else {
242 // Create an new, empty module.
243 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000244 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000245 }
246
247 // Parse the machine functions.
248 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000249 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000250 return nullptr;
251 In.nextDocument();
252 } while (In.setCurrentDocument());
253
254 return M;
255}
256
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000257bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
258 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000259 auto MF = llvm::make_unique<yaml::MachineFunction>();
Zachary Turner35377f82016-09-08 18:22:44 +0000260 yaml::EmptyContext Ctx;
261 yaml::yamlize(In, *MF, false, Ctx);
Alex Lorenz78d78312015-05-28 22:41:12 +0000262 if (In.error())
263 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000264 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000265 if (Functions.find(FunctionName) != Functions.end())
266 return error(Twine("redefinition of machine function '") + FunctionName +
267 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000268 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000269 if (NoLLVMIR)
270 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000271 else if (!M.getFunction(FunctionName))
272 return error(Twine("function '") + FunctionName +
273 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000274 return false;
275}
276
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000277void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
278 auto &Context = M.getContext();
279 Function *F = cast<Function>(M.getOrInsertFunction(
280 Name, FunctionType::get(Type::getVoidTy(Context), false)));
281 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
282 new UnreachableInst(Context, BB);
283}
284
Matthias Braun79f85b32016-08-24 01:32:41 +0000285static bool isSSA(const MachineFunction &MF) {
286 const MachineRegisterInfo &MRI = MF.getRegInfo();
287 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
288 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
289 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
290 return false;
291 }
292 return true;
293}
294
Matthias Braun90799ce2016-08-23 21:19:49 +0000295void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000296 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000297
298 bool HasPHI = false;
299 bool HasInlineAsm = false;
300 for (const MachineBasicBlock &MBB : MF) {
301 for (const MachineInstr &MI : MBB) {
302 if (MI.isPHI())
303 HasPHI = true;
304 if (MI.isInlineAsm())
305 HasInlineAsm = true;
306 }
307 }
308 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000309 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000310 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000311
312 if (isSSA(MF))
313 Properties.set(MachineFunctionProperties::Property::IsSSA);
314 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000315 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000316
317 const MachineRegisterInfo &MRI = MF.getRegInfo();
318 if (MRI.getNumVirtRegs() == 0)
319 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000320}
321
Alex Lorenz735c47e2015-06-15 20:30:22 +0000322bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
323 auto It = Functions.find(MF.getName());
324 if (It == Functions.end())
325 return error(Twine("no machine function information for function '") +
326 MF.getName() + "' in the MIR file");
327 // TODO: Recreate the machine function.
Matthias Braunde5fea22017-01-18 00:59:19 +0000328 initNames2RegClasses(MF);
329 initNames2RegBanks(MF);
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000330 const yaml::MachineFunction &YamlMF = *It->getValue();
331 if (YamlMF.Alignment)
332 MF.setAlignment(YamlMF.Alignment);
333 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000334
Tim Northover0d98b032017-03-15 18:38:13 +0000335 if (YamlMF.NoVRegs)
336 MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000337 if (YamlMF.Legalized)
338 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000339 if (YamlMF.RegBankSelected)
340 MF.getProperties().set(
341 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000342 if (YamlMF.Selected)
343 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000344
Matthias Braunde5fea22017-01-18 00:59:19 +0000345 PerFunctionMIParsingState PFS(MF, SM, IRSlots, Names2RegClasses,
346 Names2RegBanks);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000347 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000348 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000349 if (!YamlMF.Constants.empty()) {
350 auto *ConstantPool = MF.getConstantPool();
351 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000352 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000353 return true;
354 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000355
Matthias Braune35861d2016-07-13 23:27:50 +0000356 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000357 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000358 SourceMgr BlockSM;
359 BlockSM.AddNewSourceBuffer(
360 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
361 SMLoc());
362 PFS.SM = &BlockSM;
363 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000364 reportDiagnostic(
365 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
366 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000367 }
Matthias Braune35861d2016-07-13 23:27:50 +0000368 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000369
Alex Lorenza6f9a372015-07-29 21:09:09 +0000370 // Initialize the frame information after creating all the MBBs so that the
371 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000372 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000373 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000374 // Initialize the jump table after creating all the MBBs so that the MBB
375 // references can be resolved.
376 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000377 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000378 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000379 // Parse the machine instructions after creating all of the MBBs so that the
380 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000381 StringRef InsnStr = YamlMF.Body.Value.Value;
382 SourceMgr InsnSM;
383 InsnSM.AddNewSourceBuffer(
384 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
385 SMLoc());
386 PFS.SM = &InsnSM;
387 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000388 reportDiagnostic(
389 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
390 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000391 }
Matthias Braune35861d2016-07-13 23:27:50 +0000392 PFS.SM = &SM;
393
Matthias Braun74ad41c2016-10-11 03:13:01 +0000394 if (setupRegisterInfo(PFS, YamlMF))
395 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000396
397 computeFunctionProperties(MF);
398
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000399 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000400 return false;
401}
402
Matthias Braun74ad41c2016-10-11 03:13:01 +0000403bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
404 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000405 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000406 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000407 assert(RegInfo.tracksLiveness());
408 if (!YamlMF.TracksRegLiveness)
409 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000410
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000411 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000412 // Parse the virtual register information.
413 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000414 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
415 if (Info.Explicit)
416 return error(VReg.ID.SourceRange.Start,
417 Twine("redefinition of virtual register '%") +
418 Twine(VReg.ID.Value) + "'");
419 Info.Explicit = true;
420
Quentin Colombet050b2112016-03-08 01:17:03 +0000421 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000422 Info.Kind = VRegInfo::GENERIC;
Quentin Colombet050b2112016-03-08 01:17:03 +0000423 } else {
424 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000425 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000426 Info.Kind = VRegInfo::NORMAL;
427 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000428 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000429 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000430 if (!RegBank)
431 return error(
432 VReg.Class.SourceRange.Start,
433 Twine("use of undefined register class or register bank '") +
434 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000435 Info.Kind = VRegInfo::REGBANK;
436 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000437 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000438 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000439
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000440 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000441 if (Info.Kind != VRegInfo::NORMAL)
442 return error(VReg.Class.SourceRange.Start,
443 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000444
445 if (parseRegisterReference(PFS, Info.PreferredReg,
446 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000447 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000448 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000449 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000450
451 // Parse the liveins.
452 for (const auto &LiveIn : YamlMF.LiveIns) {
453 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000454 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000455 return error(Error, LiveIn.Register.SourceRange);
456 unsigned VReg = 0;
457 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000458 VRegInfo *Info;
459 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000460 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000461 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000462 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000463 }
464 RegInfo.addLiveIn(Reg, VReg);
465 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000466
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000467 // Parse the callee saved registers (Registers that will
468 // be saved for the caller).
469 if (YamlMF.CalleeSavedRegisters) {
470 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
471 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
472 unsigned Reg = 0;
473 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
474 return error(Error, RegSource.SourceRange);
475 CalleeSavedRegisters.push_back(Reg);
476 }
477 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000478 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000479
Alex Lorenz54565cf2015-06-24 19:56:10 +0000480 return false;
481}
482
Matthias Braun74ad41c2016-10-11 03:13:01 +0000483bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000484 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000485 MachineFunction &MF = PFS.MF;
486 MachineRegisterInfo &MRI = MF.getRegInfo();
487 bool Error = false;
488 // Create VRegs
489 for (auto P : PFS.VRegInfos) {
490 const VRegInfo &Info = *P.second;
491 unsigned Reg = Info.VReg;
492 switch (Info.Kind) {
493 case VRegInfo::UNKNOWN:
494 error(Twine("Cannot determine class/bank of virtual register ") +
495 Twine(P.first) + " in function '" + MF.getName() + "'");
496 Error = true;
497 break;
498 case VRegInfo::NORMAL:
499 MRI.setRegClass(Reg, Info.D.RC);
500 if (Info.PreferredReg != 0)
501 MRI.setSimpleHint(Reg, Info.PreferredReg);
502 break;
503 case VRegInfo::GENERIC:
504 break;
505 case VRegInfo::REGBANK:
506 MRI.setRegBank(Reg, *Info.D.RegBank);
507 break;
508 }
509 }
510
511 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000512 for (const MachineBasicBlock &MBB : MF) {
513 for (const MachineInstr &MI : MBB) {
514 for (const MachineOperand &MO : MI.operands()) {
515 if (!MO.isRegMask())
516 continue;
517 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000518 }
519 }
520 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000521
522 // FIXME: This is a temporary workaround until the reserved registers can be
523 // serialized.
524 MRI.freezeReservedRegs(MF);
525 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000526}
527
Matthias Braun83947862016-07-13 22:23:23 +0000528bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
529 const yaml::MachineFunction &YamlMF) {
530 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000531 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000532 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000533 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000534 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
535 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
536 MFI.setHasStackMap(YamlMFI.HasStackMap);
537 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
538 MFI.setStackSize(YamlMFI.StackSize);
539 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
540 if (YamlMFI.MaxAlignment)
541 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
542 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
543 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000544 if (YamlMFI.MaxCallFrameSize != ~0u)
545 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Alex Lorenz60541c12015-07-09 19:55:27 +0000546 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
547 MFI.setHasVAStart(YamlMFI.HasVAStart);
548 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000549 if (!YamlMFI.SavePoint.Value.empty()) {
550 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000551 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000552 return true;
553 MFI.setSavePoint(MBB);
554 }
555 if (!YamlMFI.RestorePoint.Value.empty()) {
556 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000557 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000558 return true;
559 MFI.setRestorePoint(MBB);
560 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000561
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000562 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000563 // Initialize the fixed frame objects.
564 for (const auto &Object : YamlMF.FixedStackObjects) {
565 int ObjectIdx;
566 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
567 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
568 Object.IsImmutable, Object.IsAliased);
569 else
570 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
571 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000572 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
573 ObjectIdx))
574 .second)
575 return error(Object.ID.SourceRange.Start,
576 Twine("redefinition of fixed stack object '%fixed-stack.") +
577 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000578 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000579 ObjectIdx))
580 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000581 }
582
583 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000584 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000585 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000586 const AllocaInst *Alloca = nullptr;
587 const yaml::StringValue &Name = Object.Name;
588 if (!Name.Value.empty()) {
589 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000590 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000591 if (!Alloca)
592 return error(Name.SourceRange.Start,
593 "alloca instruction named '" + Name.Value +
594 "' isn't defined in the function '" + F.getName() +
595 "'");
596 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000597 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000598 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000599 else
600 ObjectIdx = MFI.CreateStackObject(
601 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000602 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000603 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000604 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
605 .second)
606 return error(Object.ID.SourceRange.Start,
607 Twine("redefinition of stack object '%stack.") +
608 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000609 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000610 ObjectIdx))
611 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000612 if (Object.LocalOffset)
613 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000614 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000615 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000616 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000617 MFI.setCalleeSavedInfo(CSIInfo);
618 if (!CSIInfo.empty())
619 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000620
621 // Initialize the various stack object references after initializing the
622 // stack objects.
623 if (!YamlMFI.StackProtector.Value.empty()) {
624 SMDiagnostic Error;
625 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000626 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000627 return error(Error, YamlMFI.StackProtector.SourceRange);
628 MFI.setStackProtectorIndex(FI);
629 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000630 return false;
631}
632
Matthias Braun83947862016-07-13 22:23:23 +0000633bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000634 std::vector<CalleeSavedInfo> &CSIInfo,
635 const yaml::StringValue &RegisterSource, int FrameIdx) {
636 if (RegisterSource.Value.empty())
637 return false;
638 unsigned Reg = 0;
639 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000640 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000641 return error(Error, RegisterSource.SourceRange);
642 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000643 return false;
644}
645
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000646/// Verify that given node is of a certain type. Return true on error.
647template <typename T>
648static bool typecheckMDNode(T *&Result, MDNode *Node,
649 const yaml::StringValue &Source,
650 StringRef TypeString, MIRParserImpl &Parser) {
651 if (!Node)
652 return false;
653 Result = dyn_cast<T>(Node);
654 if (!Result)
655 return Parser.error(Source.SourceRange.Start,
656 "expected a reference to a '" + TypeString +
657 "' metadata node");
658 return false;
659}
660
Matthias Braun83947862016-07-13 22:23:23 +0000661bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000662 const yaml::MachineStackObject &Object, int FrameIdx) {
663 // Debug information can only be attached to stack objects; Fixed stack
664 // objects aren't supported.
665 assert(FrameIdx >= 0 && "Expected a stack object frame index");
666 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000667 if (parseMDNode(PFS, Var, Object.DebugVar) ||
668 parseMDNode(PFS, Expr, Object.DebugExpr) ||
669 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000670 return true;
671 if (!Var && !Expr && !Loc)
672 return false;
673 DILocalVariable *DIVar = nullptr;
674 DIExpression *DIExpr = nullptr;
675 DILocation *DILoc = nullptr;
676 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
677 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
678 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
679 return true;
Matthias Braunef331ef2016-11-30 23:48:50 +0000680 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000681 return false;
682}
683
Matthias Braun74ad41c2016-10-11 03:13:01 +0000684bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000685 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000686 if (Source.Value.empty())
687 return false;
688 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000689 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000690 return error(Error, Source.SourceRange);
691 return false;
692}
693
Matthias Braun83947862016-07-13 22:23:23 +0000694bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
695 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
696 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
697 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000698 const auto &M = *MF.getFunction()->getParent();
699 SMDiagnostic Error;
700 for (const auto &YamlConstant : YamlMF.Constants) {
701 const Constant *Value = dyn_cast_or_null<Constant>(
702 parseConstantValue(YamlConstant.Value.Value, Error, M));
703 if (!Value)
704 return error(Error, YamlConstant.Value.SourceRange);
705 unsigned Alignment =
706 YamlConstant.Alignment
707 ? YamlConstant.Alignment
708 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000709 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
710 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
711 .second)
712 return error(YamlConstant.ID.SourceRange.Start,
713 Twine("redefinition of constant pool item '%const.") +
714 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000715 }
716 return false;
717}
718
Matthias Braun83947862016-07-13 22:23:23 +0000719bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
720 const yaml::MachineJumpTable &YamlJTI) {
721 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000722 for (const auto &Entry : YamlJTI.Entries) {
723 std::vector<MachineBasicBlock *> Blocks;
724 for (const auto &MBBSource : Entry.Blocks) {
725 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000726 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000727 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000728 Blocks.push_back(MBB);
729 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000730 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000731 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
732 .second)
733 return error(Entry.ID.SourceRange.Start,
734 Twine("redefinition of jump table entry '%jump-table.") +
735 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000736 }
737 return false;
738}
739
Matthias Braun74ad41c2016-10-11 03:13:01 +0000740bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000741 MachineBasicBlock *&MBB,
742 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000743 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000744 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000745 return error(Error, Source.SourceRange);
746 return false;
747}
748
Alex Lorenz51af1602015-06-23 22:39:23 +0000749SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
750 SMRange SourceRange) {
751 assert(SourceRange.isValid() && "Invalid source range");
752 SMLoc Loc = SourceRange.Start;
753 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
754 *Loc.getPointer() == '\'';
755 // Translate the location of the error from the location in the MI string to
756 // the corresponding location in the MIR file.
757 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
758 (HasQuote ? 1 : 0));
759
760 // TODO: Translate any source ranges as well.
761 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
762 Error.getFixIts());
763}
764
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000765SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
766 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000767 assert(SourceRange.isValid());
768
769 // Translate the location of the error from the location in the llvm IR string
770 // to the corresponding location in the MIR file.
771 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
772 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
773 unsigned Column = Error.getColumnNo();
774 StringRef LineStr = Error.getLineContents();
775 SMLoc Loc = Error.getLoc();
776
777 // Get the full line and adjust the column number by taking the indentation of
778 // LLVM IR into account.
779 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
780 L != E; ++L) {
781 if (L.line_number() == Line) {
782 LineStr = *L;
783 Loc = SMLoc::getFromPointer(LineStr.data());
784 auto Indent = LineStr.find(Error.getLineContents());
785 if (Indent != StringRef::npos)
786 Column += Indent;
787 break;
788 }
789 }
790
791 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
792 Error.getMessage(), LineStr, Error.getRanges(),
793 Error.getFixIts());
794}
795
Alex Lorenz28148ba2015-07-09 22:23:13 +0000796void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
797 if (!Names2RegClasses.empty())
798 return;
799 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
800 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
801 const auto *RC = TRI->getRegClass(I);
802 Names2RegClasses.insert(
803 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
804 }
805}
806
Quentin Colombet876ddf82016-04-08 16:40:43 +0000807void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
808 if (!Names2RegBanks.empty())
809 return;
810 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
811 // If the target does not support GlobalISel, we may not have a
812 // register bank info.
813 if (!RBI)
814 return;
815 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
816 const auto &RegBank = RBI->getRegBank(I);
817 Names2RegBanks.insert(
818 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
819 }
820}
821
Alex Lorenz28148ba2015-07-09 22:23:13 +0000822const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
823 StringRef Name) {
Alex Lorenz28148ba2015-07-09 22:23:13 +0000824 auto RegClassInfo = Names2RegClasses.find(Name);
825 if (RegClassInfo == Names2RegClasses.end())
826 return nullptr;
827 return RegClassInfo->getValue();
828}
829
Quentin Colombet876ddf82016-04-08 16:40:43 +0000830const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
831 StringRef Name) {
Quentin Colombet876ddf82016-04-08 16:40:43 +0000832 auto RegBankInfo = Names2RegBanks.find(Name);
833 if (RegBankInfo == Names2RegBanks.end())
834 return nullptr;
835 return RegBankInfo->getValue();
836}
837
Alex Lorenz735c47e2015-06-15 20:30:22 +0000838MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
839 : Impl(std::move(Impl)) {}
840
841MIRParser::~MIRParser() {}
842
843std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
844
845bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
846 return Impl->initializeMachineFunction(MF);
847}
848
849std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
850 SMDiagnostic &Error,
851 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000852 auto FileOrErr = MemoryBuffer::getFile(Filename);
853 if (std::error_code EC = FileOrErr.getError()) {
854 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
855 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000856 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000857 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000858 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000859}
860
Alex Lorenz735c47e2015-06-15 20:30:22 +0000861std::unique_ptr<MIRParser>
862llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
863 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000864 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000865 if (Context.shouldDiscardValueNames()) {
866 Context.diagnose(DiagnosticInfoMIRParser(
867 DS_Error,
868 SMDiagnostic(
869 Filename, SourceMgr::DK_Error,
870 "Can't read MIR with a Context that discards named Values")));
871 return nullptr;
872 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000873 return llvm::make_unique<MIRParser>(
874 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000875}