blob: 6dae8b84d37cac0ad3bc4b5d34ac1685790757a7 [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 Braun83947862016-07-13 22:23:23 +0000105 bool initializeRegisterInfo(PerFunctionMIParsingState &PFS,
106 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000107
Matthias Braun83947862016-07-13 22:23:23 +0000108 void inferRegisterInfo(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 Braun83947862016-07-13 22:23:23 +0000131 bool parseMDNode(const PerFunctionMIParsingState &PFS, MDNode *&Node,
132 const yaml::StringValue &Source);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000133
Matthias Braun83947862016-07-13 22:23:23 +0000134 bool parseMBBReference(const PerFunctionMIParsingState &PFS,
135 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>();
260 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000261 if (In.error())
262 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000263 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000264 if (Functions.find(FunctionName) != Functions.end())
265 return error(Twine("redefinition of machine function '") + FunctionName +
266 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000267 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000268 if (NoLLVMIR)
269 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000270 else if (!M.getFunction(FunctionName))
271 return error(Twine("function '") + FunctionName +
272 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000273 return false;
274}
275
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000276void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
277 auto &Context = M.getContext();
278 Function *F = cast<Function>(M.getOrInsertFunction(
279 Name, FunctionType::get(Type::getVoidTy(Context), false)));
280 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
281 new UnreachableInst(Context, BB);
282}
283
Matthias Braun79f85b32016-08-24 01:32:41 +0000284static bool isSSA(const MachineFunction &MF) {
285 const MachineRegisterInfo &MRI = MF.getRegInfo();
286 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
287 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
288 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
289 return false;
290 }
291 return true;
292}
293
Matthias Braun90799ce2016-08-23 21:19:49 +0000294void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000295 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000296
297 bool HasPHI = false;
298 bool HasInlineAsm = false;
299 for (const MachineBasicBlock &MBB : MF) {
300 for (const MachineInstr &MI : MBB) {
301 if (MI.isPHI())
302 HasPHI = true;
303 if (MI.isInlineAsm())
304 HasInlineAsm = true;
305 }
306 }
307 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000308 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000309 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000310
311 if (isSSA(MF))
312 Properties.set(MachineFunctionProperties::Property::IsSSA);
313 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000314 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000315
316 const MachineRegisterInfo &MRI = MF.getRegInfo();
317 if (MRI.getNumVirtRegs() == 0)
318 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000319}
320
Alex Lorenz735c47e2015-06-15 20:30:22 +0000321bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
322 auto It = Functions.find(MF.getName());
323 if (It == Functions.end())
324 return error(Twine("no machine function information for function '") +
325 MF.getName() + "' in the MIR file");
326 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000327 const yaml::MachineFunction &YamlMF = *It->getValue();
328 if (YamlMF.Alignment)
329 MF.setAlignment(YamlMF.Alignment);
330 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000331
332 if (YamlMF.Legalized)
333 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000334 if (YamlMF.RegBankSelected)
335 MF.getProperties().set(
336 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000337 if (YamlMF.Selected)
338 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000339
Matthias Braune35861d2016-07-13 23:27:50 +0000340 PerFunctionMIParsingState PFS(MF, SM, IRSlots);
Matthias Braun83947862016-07-13 22:23:23 +0000341 if (initializeRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000342 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000343 if (!YamlMF.Constants.empty()) {
344 auto *ConstantPool = MF.getConstantPool();
345 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000346 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000347 return true;
348 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000349
Matthias Braune35861d2016-07-13 23:27:50 +0000350 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000351 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000352 SourceMgr BlockSM;
353 BlockSM.AddNewSourceBuffer(
354 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
355 SMLoc());
356 PFS.SM = &BlockSM;
357 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000358 reportDiagnostic(
359 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
360 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000361 }
Matthias Braune35861d2016-07-13 23:27:50 +0000362 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000363
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000364 if (MF.empty())
Alex Lorenzc8704b02015-07-09 21:21:33 +0000365 return error(Twine("machine function '") + Twine(MF.getName()) +
366 "' requires at least one machine basic block in its body");
Alex Lorenza6f9a372015-07-29 21:09:09 +0000367 // Initialize the frame information after creating all the MBBs so that the
368 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000369 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000370 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000371 // Initialize the jump table after creating all the MBBs so that the MBB
372 // references can be resolved.
373 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000374 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000375 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000376 // Parse the machine instructions after creating all of the MBBs so that the
377 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000378 StringRef InsnStr = YamlMF.Body.Value.Value;
379 SourceMgr InsnSM;
380 InsnSM.AddNewSourceBuffer(
381 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
382 SMLoc());
383 PFS.SM = &InsnSM;
384 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000385 reportDiagnostic(
386 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
387 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000388 }
Matthias Braune35861d2016-07-13 23:27:50 +0000389 PFS.SM = &SM;
390
391 inferRegisterInfo(PFS, YamlMF);
Matthias Braun90799ce2016-08-23 21:19:49 +0000392
393 computeFunctionProperties(MF);
394
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000395 // FIXME: This is a temporary workaround until the reserved registers can be
396 // serialized.
397 MF.getRegInfo().freezeReservedRegs(MF);
398 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000399 return false;
400}
401
Matthias Braun83947862016-07-13 22:23:23 +0000402bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS,
403 const yaml::MachineFunction &YamlMF) {
404 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000405 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000406 assert(RegInfo.tracksLiveness());
407 if (!YamlMF.TracksRegLiveness)
408 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000409
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000410 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000411 // Parse the virtual register information.
412 for (const auto &VReg : YamlMF.VirtualRegisters) {
Quentin Colombet050b2112016-03-08 01:17:03 +0000413 unsigned Reg;
414 if (StringRef(VReg.Class.Value).equals("_")) {
415 // This is a generic virtual register.
416 // The size will be set appropriately when we reach the definition.
417 Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
Quentin Colombet2c646962016-06-08 23:27:46 +0000418 PFS.GenericVRegs.insert(Reg);
Quentin Colombet050b2112016-03-08 01:17:03 +0000419 } else {
420 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000421 if (RC) {
422 Reg = RegInfo.createVirtualRegister(RC);
423 } else {
424 const auto *RegBank = getRegBank(MF, VReg.Class.Value);
425 if (!RegBank)
426 return error(
427 VReg.Class.SourceRange.Start,
428 Twine("use of undefined register class or register bank '") +
429 VReg.Class.Value + "'");
430 Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
431 RegInfo.setRegBank(Reg, *RegBank);
Quentin Colombet2c646962016-06-08 23:27:46 +0000432 PFS.GenericVRegs.insert(Reg);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000433 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000434 }
Alex Lorenza06c0c62015-07-30 21:54:10 +0000435 if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg))
436 .second)
437 return error(VReg.ID.SourceRange.Start,
438 Twine("redefinition of virtual register '%") +
439 Twine(VReg.ID.Value) + "'");
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000440 if (!VReg.PreferredRegister.Value.empty()) {
441 unsigned PreferredReg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000442 if (parseNamedRegisterReference(PFS, PreferredReg,
443 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000444 return error(Error, VReg.PreferredRegister.SourceRange);
445 RegInfo.setSimpleHint(Reg, PreferredReg);
446 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000447 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000448
449 // Parse the liveins.
450 for (const auto &LiveIn : YamlMF.LiveIns) {
451 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000452 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000453 return error(Error, LiveIn.Register.SourceRange);
454 unsigned VReg = 0;
455 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braune35861d2016-07-13 23:27:50 +0000456 if (parseVirtualRegisterReference(PFS, VReg, LiveIn.VirtualRegister.Value,
457 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000458 return error(Error, LiveIn.VirtualRegister.SourceRange);
459 }
460 RegInfo.addLiveIn(Reg, VReg);
461 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000462
463 // Parse the callee saved register mask.
464 BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size());
465 if (!YamlMF.CalleeSavedRegisters)
466 return false;
467 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
468 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000469 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
Alex Lorenzc4838082015-08-11 00:32:49 +0000470 return error(Error, RegSource.SourceRange);
471 CalleeSavedRegisterMask[Reg] = true;
472 }
473 RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip());
Alex Lorenz54565cf2015-06-24 19:56:10 +0000474 return false;
475}
476
Matthias Braun83947862016-07-13 22:23:23 +0000477void MIRParserImpl::inferRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000478 const yaml::MachineFunction &YamlMF) {
479 if (YamlMF.CalleeSavedRegisters)
480 return;
Matthias Braun83947862016-07-13 22:23:23 +0000481 MachineRegisterInfo &MRI = PFS.MF.getRegInfo();
482 for (const MachineBasicBlock &MBB : PFS.MF) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000483 for (const MachineInstr &MI : MBB) {
484 for (const MachineOperand &MO : MI.operands()) {
485 if (!MO.isRegMask())
486 continue;
Matthias Braun83947862016-07-13 22:23:23 +0000487 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000488 }
489 }
490 }
491}
492
Matthias Braun83947862016-07-13 22:23:23 +0000493bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
494 const yaml::MachineFunction &YamlMF) {
495 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000496 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000497 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000498 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000499 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
500 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
501 MFI.setHasStackMap(YamlMFI.HasStackMap);
502 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
503 MFI.setStackSize(YamlMFI.StackSize);
504 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
505 if (YamlMFI.MaxAlignment)
506 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
507 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
508 MFI.setHasCalls(YamlMFI.HasCalls);
509 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
510 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
511 MFI.setHasVAStart(YamlMFI.HasVAStart);
512 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000513 if (!YamlMFI.SavePoint.Value.empty()) {
514 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000515 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000516 return true;
517 MFI.setSavePoint(MBB);
518 }
519 if (!YamlMFI.RestorePoint.Value.empty()) {
520 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000521 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000522 return true;
523 MFI.setRestorePoint(MBB);
524 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000525
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000526 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000527 // Initialize the fixed frame objects.
528 for (const auto &Object : YamlMF.FixedStackObjects) {
529 int ObjectIdx;
530 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
531 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
532 Object.IsImmutable, Object.IsAliased);
533 else
534 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
535 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000536 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
537 ObjectIdx))
538 .second)
539 return error(Object.ID.SourceRange.Start,
540 Twine("redefinition of fixed stack object '%fixed-stack.") +
541 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000542 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000543 ObjectIdx))
544 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000545 }
546
547 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000548 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000549 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000550 const AllocaInst *Alloca = nullptr;
551 const yaml::StringValue &Name = Object.Name;
552 if (!Name.Value.empty()) {
553 Alloca = dyn_cast_or_null<AllocaInst>(
554 F.getValueSymbolTable().lookup(Name.Value));
555 if (!Alloca)
556 return error(Name.SourceRange.Start,
557 "alloca instruction named '" + Name.Value +
558 "' isn't defined in the function '" + F.getName() +
559 "'");
560 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000561 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000562 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000563 else
564 ObjectIdx = MFI.CreateStackObject(
565 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000566 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000567 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000568 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
569 .second)
570 return error(Object.ID.SourceRange.Start,
571 Twine("redefinition of stack object '%stack.") +
572 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000573 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000574 ObjectIdx))
575 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000576 if (Object.LocalOffset)
577 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000578 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000579 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000580 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000581 MFI.setCalleeSavedInfo(CSIInfo);
582 if (!CSIInfo.empty())
583 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000584
585 // Initialize the various stack object references after initializing the
586 // stack objects.
587 if (!YamlMFI.StackProtector.Value.empty()) {
588 SMDiagnostic Error;
589 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000590 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000591 return error(Error, YamlMFI.StackProtector.SourceRange);
592 MFI.setStackProtectorIndex(FI);
593 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000594 return false;
595}
596
Matthias Braun83947862016-07-13 22:23:23 +0000597bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000598 std::vector<CalleeSavedInfo> &CSIInfo,
599 const yaml::StringValue &RegisterSource, int FrameIdx) {
600 if (RegisterSource.Value.empty())
601 return false;
602 unsigned Reg = 0;
603 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000604 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000605 return error(Error, RegisterSource.SourceRange);
606 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000607 return false;
608}
609
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000610/// Verify that given node is of a certain type. Return true on error.
611template <typename T>
612static bool typecheckMDNode(T *&Result, MDNode *Node,
613 const yaml::StringValue &Source,
614 StringRef TypeString, MIRParserImpl &Parser) {
615 if (!Node)
616 return false;
617 Result = dyn_cast<T>(Node);
618 if (!Result)
619 return Parser.error(Source.SourceRange.Start,
620 "expected a reference to a '" + TypeString +
621 "' metadata node");
622 return false;
623}
624
Matthias Braun83947862016-07-13 22:23:23 +0000625bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000626 const yaml::MachineStackObject &Object, int FrameIdx) {
627 // Debug information can only be attached to stack objects; Fixed stack
628 // objects aren't supported.
629 assert(FrameIdx >= 0 && "Expected a stack object frame index");
630 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000631 if (parseMDNode(PFS, Var, Object.DebugVar) ||
632 parseMDNode(PFS, Expr, Object.DebugExpr) ||
633 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000634 return true;
635 if (!Var && !Expr && !Loc)
636 return false;
637 DILocalVariable *DIVar = nullptr;
638 DIExpression *DIExpr = nullptr;
639 DILocation *DILoc = nullptr;
640 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
641 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
642 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
643 return true;
Matthias Braun83947862016-07-13 22:23:23 +0000644 PFS.MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000645 return false;
646}
647
Matthias Braun83947862016-07-13 22:23:23 +0000648bool MIRParserImpl::parseMDNode(const PerFunctionMIParsingState &PFS,
649 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000650 if (Source.Value.empty())
651 return false;
652 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000653 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000654 return error(Error, Source.SourceRange);
655 return false;
656}
657
Matthias Braun83947862016-07-13 22:23:23 +0000658bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
659 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
660 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
661 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000662 const auto &M = *MF.getFunction()->getParent();
663 SMDiagnostic Error;
664 for (const auto &YamlConstant : YamlMF.Constants) {
665 const Constant *Value = dyn_cast_or_null<Constant>(
666 parseConstantValue(YamlConstant.Value.Value, Error, M));
667 if (!Value)
668 return error(Error, YamlConstant.Value.SourceRange);
669 unsigned Alignment =
670 YamlConstant.Alignment
671 ? YamlConstant.Alignment
672 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000673 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
674 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
675 .second)
676 return error(YamlConstant.ID.SourceRange.Start,
677 Twine("redefinition of constant pool item '%const.") +
678 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000679 }
680 return false;
681}
682
Matthias Braun83947862016-07-13 22:23:23 +0000683bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
684 const yaml::MachineJumpTable &YamlJTI) {
685 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000686 for (const auto &Entry : YamlJTI.Entries) {
687 std::vector<MachineBasicBlock *> Blocks;
688 for (const auto &MBBSource : Entry.Blocks) {
689 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000690 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000691 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000692 Blocks.push_back(MBB);
693 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000694 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000695 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
696 .second)
697 return error(Entry.ID.SourceRange.Start,
698 Twine("redefinition of jump table entry '%jump-table.") +
699 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000700 }
701 return false;
702}
703
Matthias Braun83947862016-07-13 22:23:23 +0000704bool MIRParserImpl::parseMBBReference(const PerFunctionMIParsingState &PFS,
705 MachineBasicBlock *&MBB,
706 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000707 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000708 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000709 return error(Error, Source.SourceRange);
710 return false;
711}
712
Alex Lorenz51af1602015-06-23 22:39:23 +0000713SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
714 SMRange SourceRange) {
715 assert(SourceRange.isValid() && "Invalid source range");
716 SMLoc Loc = SourceRange.Start;
717 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
718 *Loc.getPointer() == '\'';
719 // Translate the location of the error from the location in the MI string to
720 // the corresponding location in the MIR file.
721 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
722 (HasQuote ? 1 : 0));
723
724 // TODO: Translate any source ranges as well.
725 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
726 Error.getFixIts());
727}
728
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000729SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
730 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000731 assert(SourceRange.isValid());
732
733 // Translate the location of the error from the location in the llvm IR string
734 // to the corresponding location in the MIR file.
735 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
736 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
737 unsigned Column = Error.getColumnNo();
738 StringRef LineStr = Error.getLineContents();
739 SMLoc Loc = Error.getLoc();
740
741 // Get the full line and adjust the column number by taking the indentation of
742 // LLVM IR into account.
743 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
744 L != E; ++L) {
745 if (L.line_number() == Line) {
746 LineStr = *L;
747 Loc = SMLoc::getFromPointer(LineStr.data());
748 auto Indent = LineStr.find(Error.getLineContents());
749 if (Indent != StringRef::npos)
750 Column += Indent;
751 break;
752 }
753 }
754
755 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
756 Error.getMessage(), LineStr, Error.getRanges(),
757 Error.getFixIts());
758}
759
Alex Lorenz28148ba2015-07-09 22:23:13 +0000760void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
761 if (!Names2RegClasses.empty())
762 return;
763 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
764 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
765 const auto *RC = TRI->getRegClass(I);
766 Names2RegClasses.insert(
767 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
768 }
769}
770
Quentin Colombet876ddf82016-04-08 16:40:43 +0000771void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
772 if (!Names2RegBanks.empty())
773 return;
774 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
775 // If the target does not support GlobalISel, we may not have a
776 // register bank info.
777 if (!RBI)
778 return;
779 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
780 const auto &RegBank = RBI->getRegBank(I);
781 Names2RegBanks.insert(
782 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
783 }
784}
785
Alex Lorenz28148ba2015-07-09 22:23:13 +0000786const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
787 StringRef Name) {
788 initNames2RegClasses(MF);
789 auto RegClassInfo = Names2RegClasses.find(Name);
790 if (RegClassInfo == Names2RegClasses.end())
791 return nullptr;
792 return RegClassInfo->getValue();
793}
794
Quentin Colombet876ddf82016-04-08 16:40:43 +0000795const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
796 StringRef Name) {
797 initNames2RegBanks(MF);
798 auto RegBankInfo = Names2RegBanks.find(Name);
799 if (RegBankInfo == Names2RegBanks.end())
800 return nullptr;
801 return RegBankInfo->getValue();
802}
803
Alex Lorenz735c47e2015-06-15 20:30:22 +0000804MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
805 : Impl(std::move(Impl)) {}
806
807MIRParser::~MIRParser() {}
808
809std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
810
811bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
812 return Impl->initializeMachineFunction(MF);
813}
814
815std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
816 SMDiagnostic &Error,
817 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000818 auto FileOrErr = MemoryBuffer::getFile(Filename);
819 if (std::error_code EC = FileOrErr.getError()) {
820 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
821 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000822 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000823 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000824 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000825}
826
Alex Lorenz735c47e2015-06-15 20:30:22 +0000827std::unique_ptr<MIRParser>
828llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
829 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000830 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000831 return llvm::make_unique<MIRParser>(
832 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000833}