blob: 035b0ae52460641638fb2c1048a9d99d345bb6e2 [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);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000163};
164
Alex Lorenz735c47e2015-06-15 20:30:22 +0000165} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000166
167MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
168 StringRef Filename, LLVMContext &Context)
169 : SM(), Filename(Filename), Context(Context) {
170 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
171}
172
Alex Lorenz735c47e2015-06-15 20:30:22 +0000173bool MIRParserImpl::error(const Twine &Message) {
174 Context.diagnose(DiagnosticInfoMIRParser(
175 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
176 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000177}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000178
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000179bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
180 Context.diagnose(DiagnosticInfoMIRParser(
181 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
182 return true;
183}
184
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000185bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
186 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
187 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
188 return true;
189}
190
Alex Lorenz735c47e2015-06-15 20:30:22 +0000191void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
192 DiagnosticSeverity Kind;
193 switch (Diag.getKind()) {
194 case SourceMgr::DK_Error:
195 Kind = DS_Error;
196 break;
197 case SourceMgr::DK_Warning:
198 Kind = DS_Warning;
199 break;
200 case SourceMgr::DK_Note:
201 Kind = DS_Note;
202 break;
203 }
204 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
205}
206
207static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
208 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
209}
210
211std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000212 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000213 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000214 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000215
216 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000217 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000218 return nullptr;
219 // Create an empty module when the MIR file is empty.
220 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000221 }
222
Alex Lorenz78d78312015-05-28 22:41:12 +0000223 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000224 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000225 // Parse the block scalar manually so that we can return unique pointer
226 // without having to go trough YAML traits.
227 if (const auto *BSN =
228 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000229 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000230 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000231 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000232 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000233 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000234 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000235 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000236 In.nextDocument();
237 if (!In.setCurrentDocument())
238 return M;
239 } else {
240 // Create an new, empty module.
241 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000242 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000243 }
244
245 // Parse the machine functions.
246 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000247 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000248 return nullptr;
249 In.nextDocument();
250 } while (In.setCurrentDocument());
251
252 return M;
253}
254
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000255bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
256 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000257 auto MF = llvm::make_unique<yaml::MachineFunction>();
258 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000259 if (In.error())
260 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000261 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000262 if (Functions.find(FunctionName) != Functions.end())
263 return error(Twine("redefinition of machine function '") + FunctionName +
264 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000265 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000266 if (NoLLVMIR)
267 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000268 else if (!M.getFunction(FunctionName))
269 return error(Twine("function '") + FunctionName +
270 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000271 return false;
272}
273
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000274void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
275 auto &Context = M.getContext();
276 Function *F = cast<Function>(M.getOrInsertFunction(
277 Name, FunctionType::get(Type::getVoidTy(Context), false)));
278 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
279 new UnreachableInst(Context, BB);
280}
281
Alex Lorenz735c47e2015-06-15 20:30:22 +0000282bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
283 auto It = Functions.find(MF.getName());
284 if (It == Functions.end())
285 return error(Twine("no machine function information for function '") +
286 MF.getName() + "' in the MIR file");
287 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000288 const yaml::MachineFunction &YamlMF = *It->getValue();
289 if (YamlMF.Alignment)
290 MF.setAlignment(YamlMF.Alignment);
291 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
292 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Derek Schuffad154c82016-03-28 17:05:30 +0000293 if (YamlMF.AllVRegsAllocated)
294 MF.getProperties().set(MachineFunctionProperties::Property::AllVRegsAllocated);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000295
296 if (YamlMF.Legalized)
297 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
298
Matthias Braune35861d2016-07-13 23:27:50 +0000299 PerFunctionMIParsingState PFS(MF, SM, IRSlots);
Matthias Braun83947862016-07-13 22:23:23 +0000300 if (initializeRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000301 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000302 if (!YamlMF.Constants.empty()) {
303 auto *ConstantPool = MF.getConstantPool();
304 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000305 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000306 return true;
307 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000308
Matthias Braune35861d2016-07-13 23:27:50 +0000309 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000310 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000311 SourceMgr BlockSM;
312 BlockSM.AddNewSourceBuffer(
313 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
314 SMLoc());
315 PFS.SM = &BlockSM;
316 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000317 reportDiagnostic(
318 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
319 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000320 }
Matthias Braune35861d2016-07-13 23:27:50 +0000321 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000322
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000323 if (MF.empty())
Alex Lorenzc8704b02015-07-09 21:21:33 +0000324 return error(Twine("machine function '") + Twine(MF.getName()) +
325 "' requires at least one machine basic block in its body");
Alex Lorenza6f9a372015-07-29 21:09:09 +0000326 // Initialize the frame information after creating all the MBBs so that the
327 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000328 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000329 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000330 // Initialize the jump table after creating all the MBBs so that the MBB
331 // references can be resolved.
332 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000333 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000334 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000335 // Parse the machine instructions after creating all of the MBBs so that the
336 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000337 StringRef InsnStr = YamlMF.Body.Value.Value;
338 SourceMgr InsnSM;
339 InsnSM.AddNewSourceBuffer(
340 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
341 SMLoc());
342 PFS.SM = &InsnSM;
343 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000344 reportDiagnostic(
345 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
346 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000347 }
Matthias Braune35861d2016-07-13 23:27:50 +0000348 PFS.SM = &SM;
349
350 inferRegisterInfo(PFS, YamlMF);
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000351 // FIXME: This is a temporary workaround until the reserved registers can be
352 // serialized.
353 MF.getRegInfo().freezeReservedRegs(MF);
354 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000355 return false;
356}
357
Matthias Braun83947862016-07-13 22:23:23 +0000358bool MIRParserImpl::initializeRegisterInfo(PerFunctionMIParsingState &PFS,
359 const yaml::MachineFunction &YamlMF) {
360 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000361 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000362 assert(RegInfo.isSSA());
363 if (!YamlMF.IsSSA)
364 RegInfo.leaveSSA();
365 assert(RegInfo.tracksLiveness());
366 if (!YamlMF.TracksRegLiveness)
367 RegInfo.invalidateLiveness();
368 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000369
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000370 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000371 // Parse the virtual register information.
372 for (const auto &VReg : YamlMF.VirtualRegisters) {
Quentin Colombet050b2112016-03-08 01:17:03 +0000373 unsigned Reg;
374 if (StringRef(VReg.Class.Value).equals("_")) {
375 // This is a generic virtual register.
376 // The size will be set appropriately when we reach the definition.
377 Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
Quentin Colombet2c646962016-06-08 23:27:46 +0000378 PFS.GenericVRegs.insert(Reg);
Quentin Colombet050b2112016-03-08 01:17:03 +0000379 } else {
380 const auto *RC = getRegClass(MF, VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000381 if (RC) {
382 Reg = RegInfo.createVirtualRegister(RC);
383 } else {
384 const auto *RegBank = getRegBank(MF, VReg.Class.Value);
385 if (!RegBank)
386 return error(
387 VReg.Class.SourceRange.Start,
388 Twine("use of undefined register class or register bank '") +
389 VReg.Class.Value + "'");
390 Reg = RegInfo.createGenericVirtualRegister(/*Size*/ 1);
391 RegInfo.setRegBank(Reg, *RegBank);
Quentin Colombet2c646962016-06-08 23:27:46 +0000392 PFS.GenericVRegs.insert(Reg);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000393 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000394 }
Alex Lorenza06c0c62015-07-30 21:54:10 +0000395 if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg))
396 .second)
397 return error(VReg.ID.SourceRange.Start,
398 Twine("redefinition of virtual register '%") +
399 Twine(VReg.ID.Value) + "'");
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000400 if (!VReg.PreferredRegister.Value.empty()) {
401 unsigned PreferredReg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000402 if (parseNamedRegisterReference(PFS, PreferredReg,
403 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000404 return error(Error, VReg.PreferredRegister.SourceRange);
405 RegInfo.setSimpleHint(Reg, PreferredReg);
406 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000407 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000408
409 // Parse the liveins.
410 for (const auto &LiveIn : YamlMF.LiveIns) {
411 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000412 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000413 return error(Error, LiveIn.Register.SourceRange);
414 unsigned VReg = 0;
415 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braune35861d2016-07-13 23:27:50 +0000416 if (parseVirtualRegisterReference(PFS, VReg, LiveIn.VirtualRegister.Value,
417 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000418 return error(Error, LiveIn.VirtualRegister.SourceRange);
419 }
420 RegInfo.addLiveIn(Reg, VReg);
421 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000422
423 // Parse the callee saved register mask.
424 BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size());
425 if (!YamlMF.CalleeSavedRegisters)
426 return false;
427 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
428 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000429 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
Alex Lorenzc4838082015-08-11 00:32:49 +0000430 return error(Error, RegSource.SourceRange);
431 CalleeSavedRegisterMask[Reg] = true;
432 }
433 RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip());
Alex Lorenz54565cf2015-06-24 19:56:10 +0000434 return false;
435}
436
Matthias Braun83947862016-07-13 22:23:23 +0000437void MIRParserImpl::inferRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000438 const yaml::MachineFunction &YamlMF) {
439 if (YamlMF.CalleeSavedRegisters)
440 return;
Matthias Braun83947862016-07-13 22:23:23 +0000441 MachineRegisterInfo &MRI = PFS.MF.getRegInfo();
442 for (const MachineBasicBlock &MBB : PFS.MF) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000443 for (const MachineInstr &MI : MBB) {
444 for (const MachineOperand &MO : MI.operands()) {
445 if (!MO.isRegMask())
446 continue;
Matthias Braun83947862016-07-13 22:23:23 +0000447 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000448 }
449 }
450 }
451}
452
Matthias Braun83947862016-07-13 22:23:23 +0000453bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
454 const yaml::MachineFunction &YamlMF) {
455 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000456 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000457 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000458 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000459 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
460 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
461 MFI.setHasStackMap(YamlMFI.HasStackMap);
462 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
463 MFI.setStackSize(YamlMFI.StackSize);
464 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
465 if (YamlMFI.MaxAlignment)
466 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
467 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
468 MFI.setHasCalls(YamlMFI.HasCalls);
469 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
470 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
471 MFI.setHasVAStart(YamlMFI.HasVAStart);
472 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000473 if (!YamlMFI.SavePoint.Value.empty()) {
474 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000475 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000476 return true;
477 MFI.setSavePoint(MBB);
478 }
479 if (!YamlMFI.RestorePoint.Value.empty()) {
480 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000481 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000482 return true;
483 MFI.setRestorePoint(MBB);
484 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000485
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000486 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000487 // Initialize the fixed frame objects.
488 for (const auto &Object : YamlMF.FixedStackObjects) {
489 int ObjectIdx;
490 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
491 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
492 Object.IsImmutable, Object.IsAliased);
493 else
494 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
495 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000496 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
497 ObjectIdx))
498 .second)
499 return error(Object.ID.SourceRange.Start,
500 Twine("redefinition of fixed stack object '%fixed-stack.") +
501 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000502 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000503 ObjectIdx))
504 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000505 }
506
507 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000508 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000509 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000510 const AllocaInst *Alloca = nullptr;
511 const yaml::StringValue &Name = Object.Name;
512 if (!Name.Value.empty()) {
513 Alloca = dyn_cast_or_null<AllocaInst>(
514 F.getValueSymbolTable().lookup(Name.Value));
515 if (!Alloca)
516 return error(Name.SourceRange.Start,
517 "alloca instruction named '" + Name.Value +
518 "' isn't defined in the function '" + F.getName() +
519 "'");
520 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000521 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000522 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000523 else
524 ObjectIdx = MFI.CreateStackObject(
525 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000526 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000527 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000528 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
529 .second)
530 return error(Object.ID.SourceRange.Start,
531 Twine("redefinition of stack object '%stack.") +
532 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000533 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000534 ObjectIdx))
535 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000536 if (Object.LocalOffset)
537 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000538 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000539 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000540 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000541 MFI.setCalleeSavedInfo(CSIInfo);
542 if (!CSIInfo.empty())
543 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000544
545 // Initialize the various stack object references after initializing the
546 // stack objects.
547 if (!YamlMFI.StackProtector.Value.empty()) {
548 SMDiagnostic Error;
549 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000550 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000551 return error(Error, YamlMFI.StackProtector.SourceRange);
552 MFI.setStackProtectorIndex(FI);
553 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000554 return false;
555}
556
Matthias Braun83947862016-07-13 22:23:23 +0000557bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000558 std::vector<CalleeSavedInfo> &CSIInfo,
559 const yaml::StringValue &RegisterSource, int FrameIdx) {
560 if (RegisterSource.Value.empty())
561 return false;
562 unsigned Reg = 0;
563 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000564 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000565 return error(Error, RegisterSource.SourceRange);
566 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000567 return false;
568}
569
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000570/// Verify that given node is of a certain type. Return true on error.
571template <typename T>
572static bool typecheckMDNode(T *&Result, MDNode *Node,
573 const yaml::StringValue &Source,
574 StringRef TypeString, MIRParserImpl &Parser) {
575 if (!Node)
576 return false;
577 Result = dyn_cast<T>(Node);
578 if (!Result)
579 return Parser.error(Source.SourceRange.Start,
580 "expected a reference to a '" + TypeString +
581 "' metadata node");
582 return false;
583}
584
Matthias Braun83947862016-07-13 22:23:23 +0000585bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000586 const yaml::MachineStackObject &Object, int FrameIdx) {
587 // Debug information can only be attached to stack objects; Fixed stack
588 // objects aren't supported.
589 assert(FrameIdx >= 0 && "Expected a stack object frame index");
590 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000591 if (parseMDNode(PFS, Var, Object.DebugVar) ||
592 parseMDNode(PFS, Expr, Object.DebugExpr) ||
593 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000594 return true;
595 if (!Var && !Expr && !Loc)
596 return false;
597 DILocalVariable *DIVar = nullptr;
598 DIExpression *DIExpr = nullptr;
599 DILocation *DILoc = nullptr;
600 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
601 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
602 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
603 return true;
Matthias Braun83947862016-07-13 22:23:23 +0000604 PFS.MF.getMMI().setVariableDbgInfo(DIVar, DIExpr, unsigned(FrameIdx), DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000605 return false;
606}
607
Matthias Braun83947862016-07-13 22:23:23 +0000608bool MIRParserImpl::parseMDNode(const PerFunctionMIParsingState &PFS,
609 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000610 if (Source.Value.empty())
611 return false;
612 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000613 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000614 return error(Error, Source.SourceRange);
615 return false;
616}
617
Matthias Braun83947862016-07-13 22:23:23 +0000618bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
619 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
620 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
621 const MachineFunction &MF = PFS.MF;
Alex Lorenzab980492015-07-20 20:51:18 +0000622 const auto &M = *MF.getFunction()->getParent();
623 SMDiagnostic Error;
624 for (const auto &YamlConstant : YamlMF.Constants) {
625 const Constant *Value = dyn_cast_or_null<Constant>(
626 parseConstantValue(YamlConstant.Value.Value, Error, M));
627 if (!Value)
628 return error(Error, YamlConstant.Value.SourceRange);
629 unsigned Alignment =
630 YamlConstant.Alignment
631 ? YamlConstant.Alignment
632 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000633 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
634 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
635 .second)
636 return error(YamlConstant.ID.SourceRange.Start,
637 Twine("redefinition of constant pool item '%const.") +
638 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000639 }
640 return false;
641}
642
Matthias Braun83947862016-07-13 22:23:23 +0000643bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
644 const yaml::MachineJumpTable &YamlJTI) {
645 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000646 for (const auto &Entry : YamlJTI.Entries) {
647 std::vector<MachineBasicBlock *> Blocks;
648 for (const auto &MBBSource : Entry.Blocks) {
649 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000650 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000651 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000652 Blocks.push_back(MBB);
653 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000654 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000655 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
656 .second)
657 return error(Entry.ID.SourceRange.Start,
658 Twine("redefinition of jump table entry '%jump-table.") +
659 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000660 }
661 return false;
662}
663
Matthias Braun83947862016-07-13 22:23:23 +0000664bool MIRParserImpl::parseMBBReference(const PerFunctionMIParsingState &PFS,
665 MachineBasicBlock *&MBB,
666 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000667 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000668 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000669 return error(Error, Source.SourceRange);
670 return false;
671}
672
Alex Lorenz51af1602015-06-23 22:39:23 +0000673SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
674 SMRange SourceRange) {
675 assert(SourceRange.isValid() && "Invalid source range");
676 SMLoc Loc = SourceRange.Start;
677 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
678 *Loc.getPointer() == '\'';
679 // Translate the location of the error from the location in the MI string to
680 // the corresponding location in the MIR file.
681 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
682 (HasQuote ? 1 : 0));
683
684 // TODO: Translate any source ranges as well.
685 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
686 Error.getFixIts());
687}
688
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000689SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
690 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000691 assert(SourceRange.isValid());
692
693 // Translate the location of the error from the location in the llvm IR string
694 // to the corresponding location in the MIR file.
695 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
696 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
697 unsigned Column = Error.getColumnNo();
698 StringRef LineStr = Error.getLineContents();
699 SMLoc Loc = Error.getLoc();
700
701 // Get the full line and adjust the column number by taking the indentation of
702 // LLVM IR into account.
703 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
704 L != E; ++L) {
705 if (L.line_number() == Line) {
706 LineStr = *L;
707 Loc = SMLoc::getFromPointer(LineStr.data());
708 auto Indent = LineStr.find(Error.getLineContents());
709 if (Indent != StringRef::npos)
710 Column += Indent;
711 break;
712 }
713 }
714
715 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
716 Error.getMessage(), LineStr, Error.getRanges(),
717 Error.getFixIts());
718}
719
Alex Lorenz28148ba2015-07-09 22:23:13 +0000720void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
721 if (!Names2RegClasses.empty())
722 return;
723 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
724 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
725 const auto *RC = TRI->getRegClass(I);
726 Names2RegClasses.insert(
727 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
728 }
729}
730
Quentin Colombet876ddf82016-04-08 16:40:43 +0000731void MIRParserImpl::initNames2RegBanks(const MachineFunction &MF) {
732 if (!Names2RegBanks.empty())
733 return;
734 const RegisterBankInfo *RBI = MF.getSubtarget().getRegBankInfo();
735 // If the target does not support GlobalISel, we may not have a
736 // register bank info.
737 if (!RBI)
738 return;
739 for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) {
740 const auto &RegBank = RBI->getRegBank(I);
741 Names2RegBanks.insert(
742 std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank));
743 }
744}
745
Alex Lorenz28148ba2015-07-09 22:23:13 +0000746const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
747 StringRef Name) {
748 initNames2RegClasses(MF);
749 auto RegClassInfo = Names2RegClasses.find(Name);
750 if (RegClassInfo == Names2RegClasses.end())
751 return nullptr;
752 return RegClassInfo->getValue();
753}
754
Quentin Colombet876ddf82016-04-08 16:40:43 +0000755const RegisterBank *MIRParserImpl::getRegBank(const MachineFunction &MF,
756 StringRef Name) {
757 initNames2RegBanks(MF);
758 auto RegBankInfo = Names2RegBanks.find(Name);
759 if (RegBankInfo == Names2RegBanks.end())
760 return nullptr;
761 return RegBankInfo->getValue();
762}
763
Alex Lorenz735c47e2015-06-15 20:30:22 +0000764MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
765 : Impl(std::move(Impl)) {}
766
767MIRParser::~MIRParser() {}
768
769std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
770
771bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
772 return Impl->initializeMachineFunction(MF);
773}
774
775std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
776 SMDiagnostic &Error,
777 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000778 auto FileOrErr = MemoryBuffer::getFile(Filename);
779 if (std::error_code EC = FileOrErr.getError()) {
780 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
781 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000782 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000783 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000784 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000785}
786
Alex Lorenz735c47e2015-06-15 20:30:22 +0000787std::unique_ptr<MIRParser>
788llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
789 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000790 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000791 return llvm::make_unique<MIRParser>(
792 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000793}