blob: d95caf833ceac99b34e61c222ad78c9c2ad06108 [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.
58 StringMap<const TargetRegisterClass *> Names2RegClasses;
Quentin Colombet876ddf82016-04-08 16:40:43 +000059 /// Maps from register bank names to register banks.
60 StringMap<const RegisterBank *> 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.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000328 const yaml::MachineFunction &YamlMF = *It->getValue();
329 if (YamlMF.Alignment)
330 MF.setAlignment(YamlMF.Alignment);
331 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000332
333 if (YamlMF.Legalized)
334 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000335 if (YamlMF.RegBankSelected)
336 MF.getProperties().set(
337 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000338 if (YamlMF.Selected)
339 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000340
Matthias Braune35861d2016-07-13 23:27:50 +0000341 PerFunctionMIParsingState PFS(MF, SM, IRSlots);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000342 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000343 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000344 if (!YamlMF.Constants.empty()) {
345 auto *ConstantPool = MF.getConstantPool();
346 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000347 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000348 return true;
349 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000350
Matthias Braune35861d2016-07-13 23:27:50 +0000351 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000352 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000353 SourceMgr BlockSM;
354 BlockSM.AddNewSourceBuffer(
355 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
356 SMLoc());
357 PFS.SM = &BlockSM;
358 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000359 reportDiagnostic(
360 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
361 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000362 }
Matthias Braune35861d2016-07-13 23:27:50 +0000363 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000364
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000365 if (MF.empty())
Alex Lorenzc8704b02015-07-09 21:21:33 +0000366 return error(Twine("machine function '") + Twine(MF.getName()) +
367 "' requires at least one machine basic block in its body");
Alex Lorenza6f9a372015-07-29 21:09:09 +0000368 // Initialize the frame information after creating all the MBBs so that the
369 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000370 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000371 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000372 // Initialize the jump table after creating all the MBBs so that the MBB
373 // references can be resolved.
374 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000375 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000376 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000377 // Parse the machine instructions after creating all of the MBBs so that the
378 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000379 StringRef InsnStr = YamlMF.Body.Value.Value;
380 SourceMgr InsnSM;
381 InsnSM.AddNewSourceBuffer(
382 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
383 SMLoc());
384 PFS.SM = &InsnSM;
385 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000386 reportDiagnostic(
387 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
388 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000389 }
Matthias Braune35861d2016-07-13 23:27:50 +0000390 PFS.SM = &SM;
391
Matthias Braun74ad41c2016-10-11 03:13:01 +0000392 if (setupRegisterInfo(PFS, YamlMF))
393 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000394
395 computeFunctionProperties(MF);
396
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000397 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000398 return false;
399}
400
Matthias Braun74ad41c2016-10-11 03:13:01 +0000401bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
402 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000403 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000404 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000405 assert(RegInfo.tracksLiveness());
406 if (!YamlMF.TracksRegLiveness)
407 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000408
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000409 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000410 // Parse the virtual register information.
411 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000412 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
413 if (Info.Explicit)
414 return error(VReg.ID.SourceRange.Start,
415 Twine("redefinition of virtual register '%") +
416 Twine(VReg.ID.Value) + "'");
417 Info.Explicit = true;
418
Quentin Colombet050b2112016-03-08 01:17:03 +0000419 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000420 Info.Kind = VRegInfo::GENERIC;
Quentin Colombet050b2112016-03-08 01:17:03 +0000421 } else {
422 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000423 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000424 Info.Kind = VRegInfo::NORMAL;
425 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000426 } else {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000427 const RegisterBank *RegBank = getRegBank(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000428 if (!RegBank)
429 return error(
430 VReg.Class.SourceRange.Start,
431 Twine("use of undefined register class or register bank '") +
432 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000433 Info.Kind = VRegInfo::REGBANK;
434 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000435 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000436 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000437
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000438 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000439 if (Info.Kind != VRegInfo::NORMAL)
440 return error(VReg.Class.SourceRange.Start,
441 Twine("preferred register can only be set for normal vregs"));
442 if (parseNamedRegisterReference(PFS, Info.PreferredReg,
Matthias Braune35861d2016-07-13 23:27:50 +0000443 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000444 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000445 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000446 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000447
448 // Parse the liveins.
449 for (const auto &LiveIn : YamlMF.LiveIns) {
450 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000451 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000452 return error(Error, LiveIn.Register.SourceRange);
453 unsigned VReg = 0;
454 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000455 VRegInfo *Info;
456 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000457 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000458 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000459 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000460 }
461 RegInfo.addLiveIn(Reg, VReg);
462 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000463
464 // Parse the callee saved register mask.
465 BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size());
466 if (!YamlMF.CalleeSavedRegisters)
467 return false;
468 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
469 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000470 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
Alex Lorenzc4838082015-08-11 00:32:49 +0000471 return error(Error, RegSource.SourceRange);
472 CalleeSavedRegisterMask[Reg] = true;
473 }
474 RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip());
Alex Lorenz54565cf2015-06-24 19:56:10 +0000475 return false;
476}
477
Matthias Braun74ad41c2016-10-11 03:13:01 +0000478bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000479 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000480 MachineFunction &MF = PFS.MF;
481 MachineRegisterInfo &MRI = MF.getRegInfo();
482 bool Error = false;
483 // Create VRegs
484 for (auto P : PFS.VRegInfos) {
485 const VRegInfo &Info = *P.second;
486 unsigned Reg = Info.VReg;
487 switch (Info.Kind) {
488 case VRegInfo::UNKNOWN:
489 error(Twine("Cannot determine class/bank of virtual register ") +
490 Twine(P.first) + " in function '" + MF.getName() + "'");
491 Error = true;
492 break;
493 case VRegInfo::NORMAL:
494 MRI.setRegClass(Reg, Info.D.RC);
495 if (Info.PreferredReg != 0)
496 MRI.setSimpleHint(Reg, Info.PreferredReg);
497 break;
498 case VRegInfo::GENERIC:
499 break;
500 case VRegInfo::REGBANK:
501 MRI.setRegBank(Reg, *Info.D.RegBank);
502 break;
503 }
504 }
505
506 // Compute MachineRegisterInfo::UsedPhysRegMask
507 if (!YamlMF.CalleeSavedRegisters) {
508 for (const MachineBasicBlock &MBB : MF) {
509 for (const MachineInstr &MI : MBB) {
510 for (const MachineOperand &MO : MI.operands()) {
511 if (!MO.isRegMask())
512 continue;
513 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
514 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000515 }
516 }
517 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000518
519 // FIXME: This is a temporary workaround until the reserved registers can be
520 // serialized.
521 MRI.freezeReservedRegs(MF);
522 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000523}
524
Matthias Braun83947862016-07-13 22:23:23 +0000525bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
526 const yaml::MachineFunction &YamlMF) {
527 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000528 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000529 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000530 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000531 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
532 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
533 MFI.setHasStackMap(YamlMFI.HasStackMap);
534 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
535 MFI.setStackSize(YamlMFI.StackSize);
536 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
537 if (YamlMFI.MaxAlignment)
538 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
539 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
540 MFI.setHasCalls(YamlMFI.HasCalls);
541 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
542 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
543 MFI.setHasVAStart(YamlMFI.HasVAStart);
544 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000545 if (!YamlMFI.SavePoint.Value.empty()) {
546 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000547 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000548 return true;
549 MFI.setSavePoint(MBB);
550 }
551 if (!YamlMFI.RestorePoint.Value.empty()) {
552 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000553 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000554 return true;
555 MFI.setRestorePoint(MBB);
556 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000557
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000558 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000559 // Initialize the fixed frame objects.
560 for (const auto &Object : YamlMF.FixedStackObjects) {
561 int ObjectIdx;
562 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
563 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
564 Object.IsImmutable, Object.IsAliased);
565 else
566 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
567 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000568 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
569 ObjectIdx))
570 .second)
571 return error(Object.ID.SourceRange.Start,
572 Twine("redefinition of fixed stack object '%fixed-stack.") +
573 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000574 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000575 ObjectIdx))
576 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000577 }
578
579 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000580 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000581 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000582 const AllocaInst *Alloca = nullptr;
583 const yaml::StringValue &Name = Object.Name;
584 if (!Name.Value.empty()) {
585 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000586 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000587 if (!Alloca)
588 return error(Name.SourceRange.Start,
589 "alloca instruction named '" + Name.Value +
590 "' isn't defined in the function '" + F.getName() +
591 "'");
592 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000593 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000594 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000595 else
596 ObjectIdx = MFI.CreateStackObject(
597 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000598 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000599 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000600 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
601 .second)
602 return error(Object.ID.SourceRange.Start,
603 Twine("redefinition of stack object '%stack.") +
604 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000605 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000606 ObjectIdx))
607 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000608 if (Object.LocalOffset)
609 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000610 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000611 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000612 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000613 MFI.setCalleeSavedInfo(CSIInfo);
614 if (!CSIInfo.empty())
615 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000616
617 // Initialize the various stack object references after initializing the
618 // stack objects.
619 if (!YamlMFI.StackProtector.Value.empty()) {
620 SMDiagnostic Error;
621 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000622 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000623 return error(Error, YamlMFI.StackProtector.SourceRange);
624 MFI.setStackProtectorIndex(FI);
625 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000626 return false;
627}
628
Matthias Braun83947862016-07-13 22:23:23 +0000629bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000630 std::vector<CalleeSavedInfo> &CSIInfo,
631 const yaml::StringValue &RegisterSource, int FrameIdx) {
632 if (RegisterSource.Value.empty())
633 return false;
634 unsigned Reg = 0;
635 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000636 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000637 return error(Error, RegisterSource.SourceRange);
638 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000639 return false;
640}
641
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000642/// Verify that given node is of a certain type. Return true on error.
643template <typename T>
644static bool typecheckMDNode(T *&Result, MDNode *Node,
645 const yaml::StringValue &Source,
646 StringRef TypeString, MIRParserImpl &Parser) {
647 if (!Node)
648 return false;
649 Result = dyn_cast<T>(Node);
650 if (!Result)
651 return Parser.error(Source.SourceRange.Start,
652 "expected a reference to a '" + TypeString +
653 "' metadata node");
654 return false;
655}
656
Matthias Braun83947862016-07-13 22:23:23 +0000657bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000658 const yaml::MachineStackObject &Object, int FrameIdx) {
659 // Debug information can only be attached to stack objects; Fixed stack
660 // objects aren't supported.
661 assert(FrameIdx >= 0 && "Expected a stack object frame index");
662 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000663 if (parseMDNode(PFS, Var, Object.DebugVar) ||
664 parseMDNode(PFS, Expr, Object.DebugExpr) ||
665 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000666 return true;
667 if (!Var && !Expr && !Loc)
668 return false;
669 DILocalVariable *DIVar = nullptr;
670 DIExpression *DIExpr = nullptr;
671 DILocation *DILoc = nullptr;
672 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
673 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
674 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
675 return true;
Matthias Braun83947862016-07-13 22:23:23 +0000676 PFS.MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000677 return false;
678}
679
Matthias Braun74ad41c2016-10-11 03:13:01 +0000680bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000681 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000682 if (Source.Value.empty())
683 return false;
684 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000685 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000686 return error(Error, Source.SourceRange);
687 return false;
688}
689
Matthias Braun83947862016-07-13 22:23:23 +0000690bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
691 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
692 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
693 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000694 const auto &M = *MF.getFunction()->getParent();
695 SMDiagnostic Error;
696 for (const auto &YamlConstant : YamlMF.Constants) {
697 const Constant *Value = dyn_cast_or_null<Constant>(
698 parseConstantValue(YamlConstant.Value.Value, Error, M));
699 if (!Value)
700 return error(Error, YamlConstant.Value.SourceRange);
701 unsigned Alignment =
702 YamlConstant.Alignment
703 ? YamlConstant.Alignment
704 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000705 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
706 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
707 .second)
708 return error(YamlConstant.ID.SourceRange.Start,
709 Twine("redefinition of constant pool item '%const.") +
710 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000711 }
712 return false;
713}
714
Matthias Braun83947862016-07-13 22:23:23 +0000715bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
716 const yaml::MachineJumpTable &YamlJTI) {
717 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000718 for (const auto &Entry : YamlJTI.Entries) {
719 std::vector<MachineBasicBlock *> Blocks;
720 for (const auto &MBBSource : Entry.Blocks) {
721 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000722 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000723 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000724 Blocks.push_back(MBB);
725 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000726 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000727 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
728 .second)
729 return error(Entry.ID.SourceRange.Start,
730 Twine("redefinition of jump table entry '%jump-table.") +
731 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000732 }
733 return false;
734}
735
Matthias Braun74ad41c2016-10-11 03:13:01 +0000736bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000737 MachineBasicBlock *&MBB,
738 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000739 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000740 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000741 return error(Error, Source.SourceRange);
742 return false;
743}
744
Alex Lorenz51af1602015-06-23 22:39:23 +0000745SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
746 SMRange SourceRange) {
747 assert(SourceRange.isValid() && "Invalid source range");
748 SMLoc Loc = SourceRange.Start;
749 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
750 *Loc.getPointer() == '\'';
751 // Translate the location of the error from the location in the MI string to
752 // the corresponding location in the MIR file.
753 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
754 (HasQuote ? 1 : 0));
755
756 // TODO: Translate any source ranges as well.
757 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
758 Error.getFixIts());
759}
760
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000761SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
762 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000763 assert(SourceRange.isValid());
764
765 // Translate the location of the error from the location in the llvm IR string
766 // to the corresponding location in the MIR file.
767 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
768 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
769 unsigned Column = Error.getColumnNo();
770 StringRef LineStr = Error.getLineContents();
771 SMLoc Loc = Error.getLoc();
772
773 // Get the full line and adjust the column number by taking the indentation of
774 // LLVM IR into account.
775 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
776 L != E; ++L) {
777 if (L.line_number() == Line) {
778 LineStr = *L;
779 Loc = SMLoc::getFromPointer(LineStr.data());
780 auto Indent = LineStr.find(Error.getLineContents());
781 if (Indent != StringRef::npos)
782 Column += Indent;
783 break;
784 }
785 }
786
787 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
788 Error.getMessage(), LineStr, Error.getRanges(),
789 Error.getFixIts());
790}
791
Alex Lorenz28148ba2015-07-09 22:23:13 +0000792void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
793 if (!Names2RegClasses.empty())
794 return;
795 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
796 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
797 const auto *RC = TRI->getRegClass(I);
798 Names2RegClasses.insert(
799 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
800 }
801}
802
Quentin Colombet876ddf82016-04-08 16:40:43 +0000803void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
804 if (!Names2RegBanks.empty())
805 return;
806 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
807 // If the target does not support GlobalISel, we may not have a
808 // register bank info.
809 if (!RBI)
810 return;
811 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
812 const auto &RegBank = RBI->getRegBank(I);
813 Names2RegBanks.insert(
814 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
815 }
816}
817
Alex Lorenz28148ba2015-07-09 22:23:13 +0000818const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
819 StringRef Name) {
820 initNames2RegClasses(MF);
821 auto RegClassInfo = Names2RegClasses.find(Name);
822 if (RegClassInfo == Names2RegClasses.end())
823 return nullptr;
824 return RegClassInfo->getValue();
825}
826
Quentin Colombet876ddf82016-04-08 16:40:43 +0000827const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
828 StringRef Name) {
829 initNames2RegBanks(MF);
830 auto RegBankInfo = Names2RegBanks.find(Name);
831 if (RegBankInfo == Names2RegBanks.end())
832 return nullptr;
833 return RegBankInfo->getValue();
834}
835
Alex Lorenz735c47e2015-06-15 20:30:22 +0000836MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
837 : Impl(std::move(Impl)) {}
838
839MIRParser::~MIRParser() {}
840
841std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
842
843bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
844 return Impl->initializeMachineFunction(MF);
845}
846
847std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
848 SMDiagnostic &Error,
849 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000850 auto FileOrErr = MemoryBuffer::getFile(Filename);
851 if (std::error_code EC = FileOrErr.getError()) {
852 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
853 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000854 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000855 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000856 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000857}
858
Alex Lorenz735c47e2015-06-15 20:30:22 +0000859std::unique_ptr<MIRParser>
860llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
861 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000862 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000863 if (Context.shouldDiscardValueNames()) {
864 Context.diagnose(DiagnosticInfoMIRParser(
865 DS_Error,
866 SMDiagnostic(
867 Filename, SourceMgr::DK_Error,
868 "Can't read MIR with a Context that discards named Values")));
869 return nullptr;
870 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000871 return llvm::make_unique<MIRParser>(
872 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000873}