blob: 31be0f650a8f66eecc797141e633305eaf4f10f2 [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/StringRef.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000019#include "llvm/ADT/StringMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000022#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000024#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000027#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000028#include "llvm/IR/BasicBlock.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000029#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000030#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000031#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000032#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000033#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000034#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000035#include "llvm/Support/SMLoc.h"
36#include "llvm/Support/SourceMgr.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/YAMLTraits.h"
39#include <memory>
40
41using namespace llvm;
42
Alex Lorenz735c47e2015-06-15 20:30:22 +000043namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000044
45/// This class implements the parsing of LLVM IR that's embedded inside a MIR
46/// file.
47class MIRParserImpl {
48 SourceMgr SM;
49 StringRef Filename;
50 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000051 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000052 SlotMapping IRSlots;
Alex Lorenz28148ba2015-07-09 22:23:13 +000053 /// Maps from register class names to register classes.
54 StringMap<const TargetRegisterClass *> Names2RegClasses;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000055
56public:
57 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
58 LLVMContext &Context);
59
Alex Lorenz735c47e2015-06-15 20:30:22 +000060 void reportDiagnostic(const SMDiagnostic &Diag);
61
62 /// Report an error with the given message at unknown location.
63 ///
64 /// Always returns true.
65 bool error(const Twine &Message);
66
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000067 /// Report an error with the given message at the given location.
68 ///
69 /// Always returns true.
70 bool error(SMLoc Loc, const Twine &Message);
71
Alex Lorenz0fd7c622015-06-30 17:55:00 +000072 /// Report a given error with the location translated from the location in an
73 /// embedded string literal to a location in the MIR file.
74 ///
75 /// Always returns true.
76 bool error(const SMDiagnostic &Error, SMRange SourceRange);
77
Alex Lorenz78d78312015-05-28 22:41:12 +000078 /// Try to parse the optional LLVM module and the machine functions in the MIR
79 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000080 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000081 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000082 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000083
84 /// Parse the machine function in the current YAML document.
85 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000086 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
87 /// A dummy IR function is created and inserted into the given module when
88 /// this parameter is true.
89 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000090 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000091 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000092
Alex Lorenz735c47e2015-06-15 20:30:22 +000093 /// Initialize the machine function to the state that's described in the MIR
94 /// file.
95 ///
96 /// Return true if error occurred.
97 bool initializeMachineFunction(MachineFunction &MF);
98
Alex Lorenzdb07c402015-07-28 16:48:37 +000099 bool initializeRegisterInfo(MachineFunction &MF,
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000100 const yaml::MachineFunction &YamlMF,
101 PerFunctionMIParsingState &PFS);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000102
Alex Lorenzc4838082015-08-11 00:32:49 +0000103 void inferRegisterInfo(MachineFunction &MF,
104 const yaml::MachineFunction &YamlMF);
105
Alex Lorenzdb07c402015-07-28 16:48:37 +0000106 bool initializeFrameInfo(MachineFunction &MF,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000107 const yaml::MachineFunction &YamlMF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000108 PerFunctionMIParsingState &PFS);
109
110 bool parseCalleeSavedRegister(MachineFunction &MF,
111 PerFunctionMIParsingState &PFS,
112 std::vector<CalleeSavedInfo> &CSIInfo,
113 const yaml::StringValue &RegisterSource,
114 int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000115
Alex Lorenzab980492015-07-20 20:51:18 +0000116 bool initializeConstantPool(MachineConstantPool &ConstantPool,
117 const yaml::MachineFunction &YamlMF,
118 const MachineFunction &MF,
119 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
120
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000121 bool initializeJumpTableInfo(MachineFunction &MF,
122 const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000123 PerFunctionMIParsingState &PFS);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000124
Alex Lorenz09b832c2015-05-29 17:05:41 +0000125private:
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000126 bool parseMBBReference(MachineBasicBlock *&MBB,
127 const yaml::StringValue &Source, MachineFunction &MF,
128 const PerFunctionMIParsingState &PFS);
129
Alex Lorenz51af1602015-06-23 22:39:23 +0000130 /// Return a MIR diagnostic converted from an MI string diagnostic.
131 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
132 SMRange SourceRange);
133
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000134 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
135 /// block scalar string.
136 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
137 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000138
139 /// Create an empty function with the given name.
140 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000141
142 void initNames2RegClasses(const MachineFunction &MF);
143
144 /// Check if the given identifier is a name of a register class.
145 ///
146 /// Return null if the name isn't a register class.
147 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
148 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000149};
150
Alex Lorenz735c47e2015-06-15 20:30:22 +0000151} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000152
153MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
154 StringRef Filename, LLVMContext &Context)
155 : SM(), Filename(Filename), Context(Context) {
156 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
157}
158
Alex Lorenz735c47e2015-06-15 20:30:22 +0000159bool MIRParserImpl::error(const Twine &Message) {
160 Context.diagnose(DiagnosticInfoMIRParser(
161 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
162 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000163}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000164
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000165bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
166 Context.diagnose(DiagnosticInfoMIRParser(
167 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
168 return true;
169}
170
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000171bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
172 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
173 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
174 return true;
175}
176
Alex Lorenz735c47e2015-06-15 20:30:22 +0000177void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
178 DiagnosticSeverity Kind;
179 switch (Diag.getKind()) {
180 case SourceMgr::DK_Error:
181 Kind = DS_Error;
182 break;
183 case SourceMgr::DK_Warning:
184 Kind = DS_Warning;
185 break;
186 case SourceMgr::DK_Note:
187 Kind = DS_Note;
188 break;
189 }
190 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
191}
192
193static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
194 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
195}
196
197std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000198 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000199 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000200 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000201
202 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000203 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000204 return nullptr;
205 // Create an empty module when the MIR file is empty.
206 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000207 }
208
Alex Lorenz78d78312015-05-28 22:41:12 +0000209 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000210 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000211 // Parse the block scalar manually so that we can return unique pointer
212 // without having to go trough YAML traits.
213 if (const auto *BSN =
214 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000215 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000216 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000217 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000218 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000219 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000220 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000221 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000222 In.nextDocument();
223 if (!In.setCurrentDocument())
224 return M;
225 } else {
226 // Create an new, empty module.
227 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000228 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000229 }
230
231 // Parse the machine functions.
232 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000233 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000234 return nullptr;
235 In.nextDocument();
236 } while (In.setCurrentDocument());
237
238 return M;
239}
240
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000241bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
242 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000243 auto MF = llvm::make_unique<yaml::MachineFunction>();
244 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000245 if (In.error())
246 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000247 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000248 if (Functions.find(FunctionName) != Functions.end())
249 return error(Twine("redefinition of machine function '") + FunctionName +
250 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000251 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000252 if (NoLLVMIR)
253 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000254 else if (!M.getFunction(FunctionName))
255 return error(Twine("function '") + FunctionName +
256 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000257 return false;
258}
259
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000260void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
261 auto &Context = M.getContext();
262 Function *F = cast<Function>(M.getOrInsertFunction(
263 Name, FunctionType::get(Type::getVoidTy(Context), false)));
264 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
265 new UnreachableInst(Context, BB);
266}
267
Alex Lorenz735c47e2015-06-15 20:30:22 +0000268bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
269 auto It = Functions.find(MF.getName());
270 if (It == Functions.end())
271 return error(Twine("no machine function information for function '") +
272 MF.getName() + "' in the MIR file");
273 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000274 const yaml::MachineFunction &YamlMF = *It->getValue();
275 if (YamlMF.Alignment)
276 MF.setAlignment(YamlMF.Alignment);
277 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
278 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000279 PerFunctionMIParsingState PFS;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000280 if (initializeRegisterInfo(MF, YamlMF, PFS))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000281 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000282 if (!YamlMF.Constants.empty()) {
283 auto *ConstantPool = MF.getConstantPool();
284 assert(ConstantPool && "Constant pool must be created");
285 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
286 PFS.ConstantPoolSlots))
287 return true;
288 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000289
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000290 SMDiagnostic Error;
291 if (parseMachineBasicBlockDefinitions(MF, YamlMF.Body.Value.Value, PFS,
292 IRSlots, Error)) {
293 reportDiagnostic(
294 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
295 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000296 }
297
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000298 if (MF.empty())
Alex Lorenzc8704b02015-07-09 21:21:33 +0000299 return error(Twine("machine function '") + Twine(MF.getName()) +
300 "' requires at least one machine basic block in its body");
Alex Lorenza6f9a372015-07-29 21:09:09 +0000301 // Initialize the frame information after creating all the MBBs so that the
302 // MBB references in the frame information can be resolved.
303 if (initializeFrameInfo(MF, YamlMF, PFS))
304 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000305 // Initialize the jump table after creating all the MBBs so that the MBB
306 // references can be resolved.
307 if (!YamlMF.JumpTableInfo.Entries.empty() &&
308 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
309 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000310 // Parse the machine instructions after creating all of the MBBs so that the
311 // parser can resolve the MBB references.
312 if (parseMachineInstructions(MF, YamlMF.Body.Value.Value, PFS, IRSlots,
313 Error)) {
314 reportDiagnostic(
315 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
316 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000317 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000318 inferRegisterInfo(MF, YamlMF);
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000319 // FIXME: This is a temporary workaround until the reserved registers can be
320 // serialized.
321 MF.getRegInfo().freezeReservedRegs(MF);
322 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000323 return false;
324}
325
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000326bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000327 const yaml::MachineFunction &YamlMF,
328 PerFunctionMIParsingState &PFS) {
Alex Lorenzdb07c402015-07-28 16:48:37 +0000329 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000330 assert(RegInfo.isSSA());
331 if (!YamlMF.IsSSA)
332 RegInfo.leaveSSA();
333 assert(RegInfo.tracksLiveness());
334 if (!YamlMF.TracksRegLiveness)
335 RegInfo.invalidateLiveness();
336 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000337
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000338 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000339 // Parse the virtual register information.
340 for (const auto &VReg : YamlMF.VirtualRegisters) {
341 const auto *RC = getRegClass(MF, VReg.Class.Value);
342 if (!RC)
343 return error(VReg.Class.SourceRange.Start,
344 Twine("use of undefined register class '") +
345 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000346 unsigned Reg = RegInfo.createVirtualRegister(RC);
Alex Lorenza06c0c62015-07-30 21:54:10 +0000347 if (!PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID.Value, Reg))
348 .second)
349 return error(VReg.ID.SourceRange.Start,
350 Twine("redefinition of virtual register '%") +
351 Twine(VReg.ID.Value) + "'");
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000352 if (!VReg.PreferredRegister.Value.empty()) {
353 unsigned PreferredReg = 0;
354 if (parseNamedRegisterReference(PreferredReg, SM, MF,
355 VReg.PreferredRegister.Value, PFS,
356 IRSlots, Error))
357 return error(Error, VReg.PreferredRegister.SourceRange);
358 RegInfo.setSimpleHint(Reg, PreferredReg);
359 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000360 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000361
362 // Parse the liveins.
363 for (const auto &LiveIn : YamlMF.LiveIns) {
364 unsigned Reg = 0;
365 if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
366 IRSlots, Error))
367 return error(Error, LiveIn.Register.SourceRange);
368 unsigned VReg = 0;
369 if (!LiveIn.VirtualRegister.Value.empty()) {
370 if (parseVirtualRegisterReference(
371 VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
372 return error(Error, LiveIn.VirtualRegister.SourceRange);
373 }
374 RegInfo.addLiveIn(Reg, VReg);
375 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000376
377 // Parse the callee saved register mask.
378 BitVector CalleeSavedRegisterMask(RegInfo.getUsedPhysRegsMask().size());
379 if (!YamlMF.CalleeSavedRegisters)
380 return false;
381 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
382 unsigned Reg = 0;
383 if (parseNamedRegisterReference(Reg, SM, MF, RegSource.Value, PFS, IRSlots,
384 Error))
385 return error(Error, RegSource.SourceRange);
386 CalleeSavedRegisterMask[Reg] = true;
387 }
388 RegInfo.setUsedPhysRegMask(CalleeSavedRegisterMask.flip());
Alex Lorenz54565cf2015-06-24 19:56:10 +0000389 return false;
390}
391
Alex Lorenzc4838082015-08-11 00:32:49 +0000392void MIRParserImpl::inferRegisterInfo(MachineFunction &MF,
393 const yaml::MachineFunction &YamlMF) {
394 if (YamlMF.CalleeSavedRegisters)
395 return;
396 for (const MachineBasicBlock &MBB : MF) {
397 for (const MachineInstr &MI : MBB) {
398 for (const MachineOperand &MO : MI.operands()) {
399 if (!MO.isRegMask())
400 continue;
401 MF.getRegInfo().addPhysRegsUsedFromRegMask(MO.getRegMask());
402 }
403 }
404 }
405}
406
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000407bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000408 const yaml::MachineFunction &YamlMF,
409 PerFunctionMIParsingState &PFS) {
Alex Lorenzdb07c402015-07-28 16:48:37 +0000410 MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000411 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000412 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000413 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
414 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
415 MFI.setHasStackMap(YamlMFI.HasStackMap);
416 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
417 MFI.setStackSize(YamlMFI.StackSize);
418 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
419 if (YamlMFI.MaxAlignment)
420 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
421 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
422 MFI.setHasCalls(YamlMFI.HasCalls);
423 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
424 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
425 MFI.setHasVAStart(YamlMFI.HasVAStart);
426 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000427 if (!YamlMFI.SavePoint.Value.empty()) {
428 MachineBasicBlock *MBB = nullptr;
429 if (parseMBBReference(MBB, YamlMFI.SavePoint, MF, PFS))
430 return true;
431 MFI.setSavePoint(MBB);
432 }
433 if (!YamlMFI.RestorePoint.Value.empty()) {
434 MachineBasicBlock *MBB = nullptr;
435 if (parseMBBReference(MBB, YamlMFI.RestorePoint, MF, PFS))
436 return true;
437 MFI.setRestorePoint(MBB);
438 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000439
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000440 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000441 // Initialize the fixed frame objects.
442 for (const auto &Object : YamlMF.FixedStackObjects) {
443 int ObjectIdx;
444 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
445 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
446 Object.IsImmutable, Object.IsAliased);
447 else
448 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
449 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000450 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
451 ObjectIdx))
452 .second)
453 return error(Object.ID.SourceRange.Start,
454 Twine("redefinition of fixed stack object '%fixed-stack.") +
455 Twine(Object.ID.Value) + "'");
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000456 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
457 ObjectIdx))
458 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000459 }
460
461 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000462 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000463 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000464 const AllocaInst *Alloca = nullptr;
465 const yaml::StringValue &Name = Object.Name;
466 if (!Name.Value.empty()) {
467 Alloca = dyn_cast_or_null<AllocaInst>(
468 F.getValueSymbolTable().lookup(Name.Value));
469 if (!Alloca)
470 return error(Name.SourceRange.Start,
471 "alloca instruction named '" + Name.Value +
472 "' isn't defined in the function '" + F.getName() +
473 "'");
474 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000475 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000476 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000477 else
478 ObjectIdx = MFI.CreateStackObject(
479 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000480 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000481 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000482 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
483 .second)
484 return error(Object.ID.SourceRange.Start,
485 Twine("redefinition of stack object '%stack.") +
486 Twine(Object.ID.Value) + "'");
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000487 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
488 ObjectIdx))
489 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000490 if (Object.LocalOffset)
491 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000492 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000493 MFI.setCalleeSavedInfo(CSIInfo);
494 if (!CSIInfo.empty())
495 MFI.setCalleeSavedInfoValid(true);
496 return false;
497}
498
499bool MIRParserImpl::parseCalleeSavedRegister(
500 MachineFunction &MF, PerFunctionMIParsingState &PFS,
501 std::vector<CalleeSavedInfo> &CSIInfo,
502 const yaml::StringValue &RegisterSource, int FrameIdx) {
503 if (RegisterSource.Value.empty())
504 return false;
505 unsigned Reg = 0;
506 SMDiagnostic Error;
507 if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
508 IRSlots, Error))
509 return error(Error, RegisterSource.SourceRange);
510 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000511 return false;
512}
513
Alex Lorenzab980492015-07-20 20:51:18 +0000514bool MIRParserImpl::initializeConstantPool(
515 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
516 const MachineFunction &MF,
517 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
518 const auto &M = *MF.getFunction()->getParent();
519 SMDiagnostic Error;
520 for (const auto &YamlConstant : YamlMF.Constants) {
521 const Constant *Value = dyn_cast_or_null<Constant>(
522 parseConstantValue(YamlConstant.Value.Value, Error, M));
523 if (!Value)
524 return error(Error, YamlConstant.Value.SourceRange);
525 unsigned Alignment =
526 YamlConstant.Alignment
527 ? YamlConstant.Alignment
528 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000529 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
530 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
531 .second)
532 return error(YamlConstant.ID.SourceRange.Start,
533 Twine("redefinition of constant pool item '%const.") +
534 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000535 }
536 return false;
537}
538
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000539bool MIRParserImpl::initializeJumpTableInfo(
540 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000541 PerFunctionMIParsingState &PFS) {
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000542 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000543 for (const auto &Entry : YamlJTI.Entries) {
544 std::vector<MachineBasicBlock *> Blocks;
545 for (const auto &MBBSource : Entry.Blocks) {
546 MachineBasicBlock *MBB = nullptr;
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000547 if (parseMBBReference(MBB, MBBSource.Value, MF, PFS))
548 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000549 Blocks.push_back(MBB);
550 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000551 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000552 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
553 .second)
554 return error(Entry.ID.SourceRange.Start,
555 Twine("redefinition of jump table entry '%jump-table.") +
556 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000557 }
558 return false;
559}
560
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000561bool MIRParserImpl::parseMBBReference(MachineBasicBlock *&MBB,
562 const yaml::StringValue &Source,
563 MachineFunction &MF,
564 const PerFunctionMIParsingState &PFS) {
565 SMDiagnostic Error;
566 if (llvm::parseMBBReference(MBB, SM, MF, Source.Value, PFS, IRSlots, Error))
567 return error(Error, Source.SourceRange);
568 return false;
569}
570
Alex Lorenz51af1602015-06-23 22:39:23 +0000571SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
572 SMRange SourceRange) {
573 assert(SourceRange.isValid() && "Invalid source range");
574 SMLoc Loc = SourceRange.Start;
575 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
576 *Loc.getPointer() == '\'';
577 // Translate the location of the error from the location in the MI string to
578 // the corresponding location in the MIR file.
579 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
580 (HasQuote ? 1 : 0));
581
582 // TODO: Translate any source ranges as well.
583 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
584 Error.getFixIts());
585}
586
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000587SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
588 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000589 assert(SourceRange.isValid());
590
591 // Translate the location of the error from the location in the llvm IR string
592 // to the corresponding location in the MIR file.
593 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
594 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
595 unsigned Column = Error.getColumnNo();
596 StringRef LineStr = Error.getLineContents();
597 SMLoc Loc = Error.getLoc();
598
599 // Get the full line and adjust the column number by taking the indentation of
600 // LLVM IR into account.
601 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
602 L != E; ++L) {
603 if (L.line_number() == Line) {
604 LineStr = *L;
605 Loc = SMLoc::getFromPointer(LineStr.data());
606 auto Indent = LineStr.find(Error.getLineContents());
607 if (Indent != StringRef::npos)
608 Column += Indent;
609 break;
610 }
611 }
612
613 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
614 Error.getMessage(), LineStr, Error.getRanges(),
615 Error.getFixIts());
616}
617
Alex Lorenz28148ba2015-07-09 22:23:13 +0000618void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
619 if (!Names2RegClasses.empty())
620 return;
621 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
622 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
623 const auto *RC = TRI->getRegClass(I);
624 Names2RegClasses.insert(
625 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
626 }
627}
628
629const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
630 StringRef Name) {
631 initNames2RegClasses(MF);
632 auto RegClassInfo = Names2RegClasses.find(Name);
633 if (RegClassInfo == Names2RegClasses.end())
634 return nullptr;
635 return RegClassInfo->getValue();
636}
637
Alex Lorenz735c47e2015-06-15 20:30:22 +0000638MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
639 : Impl(std::move(Impl)) {}
640
641MIRParser::~MIRParser() {}
642
643std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
644
645bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
646 return Impl->initializeMachineFunction(MF);
647}
648
649std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
650 SMDiagnostic &Error,
651 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000652 auto FileOrErr = MemoryBuffer::getFile(Filename);
653 if (std::error_code EC = FileOrErr.getError()) {
654 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
655 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000656 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000657 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000658 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000659}
660
Alex Lorenz735c47e2015-06-15 20:30:22 +0000661std::unique_ptr<MIRParser>
662llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
663 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000664 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000665 return llvm::make_unique<MIRParser>(
666 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000667}