blob: 3598cbe22ceba460fe42d347c151ba109e6ed60b [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the class that parses the optional LLVM IR and machine
10// functions that are stored in MIR files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MIRParser/MIRParser.h"
Alex Lorenz33f0aef2015-06-26 16:46:11 +000015#include "llvm/ADT/DenseMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000016#include "llvm/ADT/STLExtras.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000019#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000020#include "llvm/AsmParser/SlotMapping.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000021#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
22#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +000023#include "llvm/CodeGen/MIRParser/MIParser.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000024#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000027#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000028#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000030#include "llvm/IR/BasicBlock.h"
Reid Kleckner28865802016-04-14 18:29:59 +000031#include "llvm/IR/DebugInfo.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000032#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000033#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000034#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000035#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000036#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000037#include "llvm/Support/LineIterator.h"
Quentin Colombet876ddf82016-04-08 16:40:43 +000038#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000039#include "llvm/Support/SMLoc.h"
40#include "llvm/Support/SourceMgr.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000041#include "llvm/Support/YAMLTraits.h"
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +000042#include "llvm/Target/TargetMachine.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000043#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;
Matthias Braun7bda1952017-06-06 00:44:35 +000053 yaml::Input In;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000054 StringRef Filename;
55 LLVMContext &Context;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000056 SlotMapping IRSlots;
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +000057 std::unique_ptr<PerTargetMIParsingState> Target;
58
Matthias Braun7bda1952017-06-06 00:44:35 +000059 /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are
60 /// created and inserted into the given module when this is true.
61 bool NoLLVMIR = false;
62 /// True when a well formed MIR file does not contain any MIR/machine function
63 /// parts.
64 bool NoMIRDocuments = false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000065
66public:
Matthias Braun7bda1952017-06-06 00:44:35 +000067 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
68 StringRef Filename, LLVMContext &Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000069
Alex Lorenz735c47e2015-06-15 20:30:22 +000070 void reportDiagnostic(const SMDiagnostic &Diag);
71
72 /// Report an error with the given message at unknown location.
73 ///
74 /// Always returns true.
75 bool error(const Twine &Message);
76
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000077 /// Report an error with the given message at the given location.
78 ///
79 /// Always returns true.
80 bool error(SMLoc Loc, const Twine &Message);
81
Alex Lorenz0fd7c622015-06-30 17:55:00 +000082 /// Report a given error with the location translated from the location in an
83 /// embedded string literal to a location in the MIR file.
84 ///
85 /// Always returns true.
86 bool error(const SMDiagnostic &Error, SMRange SourceRange);
87
Alex Lorenz78d78312015-05-28 22:41:12 +000088 /// Try to parse the optional LLVM module and the machine functions in the MIR
89 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000090 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000091 /// Return null if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +000092 std::unique_ptr<Module> parseIRModule();
93
94 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
Alex Lorenz78d78312015-05-28 22:41:12 +000095
96 /// Parse the machine function in the current YAML document.
97 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000098 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000099 /// Return true if an error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000100 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000101
Alex Lorenz735c47e2015-06-15 20:30:22 +0000102 /// Initialize the machine function to the state that's described in the MIR
103 /// file.
104 ///
105 /// Return true if error occurred.
Matthias Braun7bda1952017-06-06 00:44:35 +0000106 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
107 MachineFunction &MF);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000108
Matthias Braun74ad41c2016-10-11 03:13:01 +0000109 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
110 const yaml::MachineFunction &YamlMF);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000111
Matthias Braun74ad41c2016-10-11 03:13:01 +0000112 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000113 const yaml::MachineFunction &YamlMF);
114
Matthias Braun83947862016-07-13 22:23:23 +0000115 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
116 const yaml::MachineFunction &YamlMF);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000117
Matthias Braun83947862016-07-13 22:23:23 +0000118 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000119 std::vector<CalleeSavedInfo> &CSIInfo,
120 const yaml::StringValue &RegisterSource,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000121 bool IsRestored, int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000122
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000123 template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000124 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000125 const T &Object,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000126 int FrameIdx);
127
Matthias Braun83947862016-07-13 22:23:23 +0000128 bool initializeConstantPool(PerFunctionMIParsingState &PFS,
129 MachineConstantPool &ConstantPool,
130 const yaml::MachineFunction &YamlMF);
Alex Lorenzab980492015-07-20 20:51:18 +0000131
Matthias Braun83947862016-07-13 22:23:23 +0000132 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
133 const yaml::MachineJumpTable &YamlJTI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000134
Alex Lorenz09b832c2015-05-29 17:05:41 +0000135private:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000136 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
Matthias Braun83947862016-07-13 22:23:23 +0000137 const yaml::StringValue &Source);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000138
Matthias Braun74ad41c2016-10-11 03:13:01 +0000139 bool parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000140 MachineBasicBlock *&MBB,
141 const yaml::StringValue &Source);
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000142
Alex Lorenz51af1602015-06-23 22:39:23 +0000143 /// Return a MIR diagnostic converted from an MI string diagnostic.
144 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
145 SMRange SourceRange);
146
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000147 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
148 /// block scalar string.
149 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
150 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000151
Matthias Braun90799ce2016-08-23 21:19:49 +0000152 void computeFunctionProperties(MachineFunction &MF);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000153};
154
Alex Lorenz735c47e2015-06-15 20:30:22 +0000155} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000156
Matthias Braun7bda1952017-06-06 00:44:35 +0000157static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
158 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
159}
160
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000161MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
162 StringRef Filename, LLVMContext &Context)
Matthias Braun7bda1952017-06-06 00:44:35 +0000163 : SM(),
164 In(SM.getMemoryBuffer(
165 SM.AddNewSourceBuffer(std::move(Contents), SMLoc()))->getBuffer(),
166 nullptr, handleYAMLDiag, this),
167 Filename(Filename),
168 Context(Context) {
169 In.setContext(&In);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000170}
171
Alex Lorenz735c47e2015-06-15 20:30:22 +0000172bool MIRParserImpl::error(const Twine &Message) {
173 Context.diagnose(DiagnosticInfoMIRParser(
174 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
175 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000176}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000177
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000178bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
179 Context.diagnose(DiagnosticInfoMIRParser(
180 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
181 return true;
182}
183
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000184bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
185 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
186 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
187 return true;
188}
189
Alex Lorenz735c47e2015-06-15 20:30:22 +0000190void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
191 DiagnosticSeverity Kind;
192 switch (Diag.getKind()) {
193 case SourceMgr::DK_Error:
194 Kind = DS_Error;
195 break;
196 case SourceMgr::DK_Warning:
197 Kind = DS_Warning;
198 break;
199 case SourceMgr::DK_Note:
200 Kind = DS_Note;
201 break;
Adam Nemet01104ae2017-10-12 23:56:02 +0000202 case SourceMgr::DK_Remark:
203 llvm_unreachable("remark unexpected");
204 break;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000205 }
206 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
207}
208
Matthias Braun7bda1952017-06-06 00:44:35 +0000209std::unique_ptr<Module> MIRParserImpl::parseIRModule() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000210 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000211 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000212 return nullptr;
213 // Create an empty module when the MIR file is empty.
Matthias Braun7bda1952017-06-06 00:44:35 +0000214 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000215 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000216 }
217
Alex Lorenz78d78312015-05-28 22:41:12 +0000218 std::unique_ptr<Module> M;
219 // Parse the block scalar manually so that we can return unique pointer
220 // without having to go trough YAML traits.
221 if (const auto *BSN =
222 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000223 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000224 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000225 Context, &IRSlots, /*UpgradeDebugInfo=*/false);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000226 if (!M) {
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000227 reportDiagnostic(diagFromBlockStringDiag(Error, BSN->getSourceRange()));
Matthias Braund6f95622016-07-14 00:42:37 +0000228 return nullptr;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000229 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000230 In.nextDocument();
231 if (!In.setCurrentDocument())
Matthias Braun7bda1952017-06-06 00:44:35 +0000232 NoMIRDocuments = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000233 } else {
234 // Create an new, empty module.
235 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000236 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000237 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000238 return M;
239}
240
Matthias Braun7bda1952017-06-06 00:44:35 +0000241bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
242 if (NoMIRDocuments)
243 return false;
244
245 // Parse the machine functions.
246 do {
247 if (parseMachineFunction(M, MMI))
248 return true;
249 In.nextDocument();
250 } while (In.setCurrentDocument());
251
Alex Lorenz735c47e2015-06-15 20:30:22 +0000252 return false;
253}
254
Matthias Braun7bda1952017-06-06 00:44:35 +0000255/// Create an empty function with the given name.
256static Function *createDummyFunction(StringRef Name, Module &M) {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000257 auto &Context = M.getContext();
James Y Knight13680222019-02-01 02:28:03 +0000258 Function *F =
259 Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
260 Function::ExternalLinkage, Name, M);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000261 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
262 new UnreachableInst(Context, BB);
Matthias Braun7bda1952017-06-06 00:44:35 +0000263 return F;
264}
265
266bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI) {
267 // Parse the yaml.
268 yaml::MachineFunction YamlMF;
269 yaml::EmptyContext Ctx;
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +0000270
271 const LLVMTargetMachine &TM = MMI.getTarget();
272 YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
273 TM.createDefaultFuncInfoYAML());
274
Matthias Braun7bda1952017-06-06 00:44:35 +0000275 yaml::yamlize(In, YamlMF, false, Ctx);
276 if (In.error())
277 return true;
278
279 // Search for the corresponding IR function.
280 StringRef FunctionName = YamlMF.Name;
281 Function *F = M.getFunction(FunctionName);
282 if (!F) {
283 if (NoLLVMIR) {
284 F = createDummyFunction(FunctionName, M);
285 } else {
286 return error(Twine("function '") + FunctionName +
287 "' isn't defined in the provided LLVM IR");
288 }
289 }
290 if (MMI.getMachineFunction(*F) != nullptr)
291 return error(Twine("redefinition of machine function '") + FunctionName +
292 "'");
293
294 // Create the MachineFunction.
295 MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
296 if (initializeMachineFunction(YamlMF, MF))
297 return true;
298
299 return false;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000300}
301
Matthias Braun79f85b32016-08-24 01:32:41 +0000302static bool isSSA(const MachineFunction &MF) {
303 const MachineRegisterInfo &MRI = MF.getRegInfo();
304 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
305 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
306 if (!MRI.hasOneDef(Reg) && !MRI.def_empty(Reg))
307 return false;
308 }
309 return true;
310}
311
Matthias Braun90799ce2016-08-23 21:19:49 +0000312void MIRParserImpl::computeFunctionProperties(MachineFunction &MF) {
Matthias Braun79f85b32016-08-24 01:32:41 +0000313 MachineFunctionProperties &Properties = MF.getProperties();
Matthias Brauna319e2c2016-08-24 22:34:06 +0000314
315 bool HasPHI = false;
316 bool HasInlineAsm = false;
317 for (const MachineBasicBlock &MBB : MF) {
318 for (const MachineInstr &MI : MBB) {
319 if (MI.isPHI())
320 HasPHI = true;
321 if (MI.isInlineAsm())
322 HasInlineAsm = true;
323 }
324 }
325 if (!HasPHI)
Matthias Braun79f85b32016-08-24 01:32:41 +0000326 Properties.set(MachineFunctionProperties::Property::NoPHIs);
Matthias Brauna319e2c2016-08-24 22:34:06 +0000327 MF.setHasInlineAsm(HasInlineAsm);
Matthias Braun79f85b32016-08-24 01:32:41 +0000328
329 if (isSSA(MF))
330 Properties.set(MachineFunctionProperties::Property::IsSSA);
331 else
Quentin Colombete609a9a2016-08-26 22:09:11 +0000332 Properties.reset(MachineFunctionProperties::Property::IsSSA);
Matthias Braun1eb47362016-08-25 01:27:13 +0000333
334 const MachineRegisterInfo &MRI = MF.getRegInfo();
335 if (MRI.getNumVirtRegs() == 0)
336 Properties.set(MachineFunctionProperties::Property::NoVRegs);
Matthias Braun90799ce2016-08-23 21:19:49 +0000337}
338
Matthias Braun7bda1952017-06-06 00:44:35 +0000339bool
340MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
341 MachineFunction &MF) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000342 // TODO: Recreate the machine function.
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000343 if (Target) {
344 // Avoid clearing state if we're using the same subtarget again.
345 Target->setTarget(MF.getSubtarget());
346 } else {
347 Target.reset(new PerTargetMIParsingState(MF.getSubtarget()));
348 }
349
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000350 if (YamlMF.Alignment)
351 MF.setAlignment(YamlMF.Alignment);
352 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
Sanjin Sijaric625d08e2018-10-24 21:07:38 +0000353 MF.setHasWinCFI(YamlMF.HasWinCFI);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000354
355 if (YamlMF.Legalized)
356 MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000357 if (YamlMF.RegBankSelected)
358 MF.getProperties().set(
359 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000360 if (YamlMF.Selected)
361 MF.getProperties().set(MachineFunctionProperties::Property::Selected);
Roman Tereshin3054ece2018-02-28 17:55:45 +0000362 if (YamlMF.FailedISel)
363 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000364
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000365 PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000366 if (parseRegisterInfo(PFS, YamlMF))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000367 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000368 if (!YamlMF.Constants.empty()) {
369 auto *ConstantPool = MF.getConstantPool();
370 assert(ConstantPool && "Constant pool must be created");
Matthias Braun83947862016-07-13 22:23:23 +0000371 if (initializeConstantPool(PFS, *ConstantPool, YamlMF))
Alex Lorenzab980492015-07-20 20:51:18 +0000372 return true;
373 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000374
Matthias Braune35861d2016-07-13 23:27:50 +0000375 StringRef BlockStr = YamlMF.Body.Value.Value;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000376 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000377 SourceMgr BlockSM;
378 BlockSM.AddNewSourceBuffer(
379 MemoryBuffer::getMemBuffer(BlockStr, "",/*RequiresNullTerminator=*/false),
380 SMLoc());
381 PFS.SM = &BlockSM;
382 if (parseMachineBasicBlockDefinitions(PFS, BlockStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000383 reportDiagnostic(
384 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
385 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000386 }
Matthias Braune35861d2016-07-13 23:27:50 +0000387 PFS.SM = &SM;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000388
Alex Lorenza6f9a372015-07-29 21:09:09 +0000389 // Initialize the frame information after creating all the MBBs so that the
390 // MBB references in the frame information can be resolved.
Matthias Braun83947862016-07-13 22:23:23 +0000391 if (initializeFrameInfo(PFS, YamlMF))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000392 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000393 // Initialize the jump table after creating all the MBBs so that the MBB
394 // references can be resolved.
395 if (!YamlMF.JumpTableInfo.Entries.empty() &&
Matthias Braun83947862016-07-13 22:23:23 +0000396 initializeJumpTableInfo(PFS, YamlMF.JumpTableInfo))
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000397 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000398 // Parse the machine instructions after creating all of the MBBs so that the
399 // parser can resolve the MBB references.
Matthias Braune35861d2016-07-13 23:27:50 +0000400 StringRef InsnStr = YamlMF.Body.Value.Value;
401 SourceMgr InsnSM;
402 InsnSM.AddNewSourceBuffer(
403 MemoryBuffer::getMemBuffer(InsnStr, "", /*RequiresNullTerminator=*/false),
404 SMLoc());
405 PFS.SM = &InsnSM;
406 if (parseMachineInstructions(PFS, InsnStr, Error)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000407 reportDiagnostic(
408 diagFromBlockStringDiag(Error, YamlMF.Body.Value.SourceRange));
409 return true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000410 }
Matthias Braune35861d2016-07-13 23:27:50 +0000411 PFS.SM = &SM;
412
Matthias Braun74ad41c2016-10-11 03:13:01 +0000413 if (setupRegisterInfo(PFS, YamlMF))
414 return true;
Matthias Braun90799ce2016-08-23 21:19:49 +0000415
Matt Arsenaultbc6d07c2019-03-14 22:54:43 +0000416 if (YamlMF.MachineFuncInfo) {
417 const LLVMTargetMachine &TM = MF.getTarget();
418 // Note this is called after the initial constructor of the
419 // MachineFunctionInfo based on the MachineFunction, which may depend on the
420 // IR.
421
422 SMRange SrcRange;
423 if (TM.parseMachineFunctionInfo(*YamlMF.MachineFuncInfo, PFS, Error,
424 SrcRange)) {
425 return error(Error, SrcRange);
426 }
427 }
428
Matthias Braun90799ce2016-08-23 21:19:49 +0000429 computeFunctionProperties(MF);
430
Matthias Braun5c290dc2018-01-19 03:16:36 +0000431 MF.getSubtarget().mirFileLoaded(MF);
432
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000433 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000434 return false;
435}
436
Matthias Braun74ad41c2016-10-11 03:13:01 +0000437bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
438 const yaml::MachineFunction &YamlMF) {
Matthias Braun83947862016-07-13 22:23:23 +0000439 MachineFunction &MF = PFS.MF;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000440 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000441 assert(RegInfo.tracksLiveness());
442 if (!YamlMF.TracksRegLiveness)
443 RegInfo.invalidateLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000444
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000445 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000446 // Parse the virtual register information.
447 for (const auto &VReg : YamlMF.VirtualRegisters) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000448 VRegInfo &Info = PFS.getVRegInfo(VReg.ID.Value);
449 if (Info.Explicit)
450 return error(VReg.ID.SourceRange.Start,
451 Twine("redefinition of virtual register '%") +
452 Twine(VReg.ID.Value) + "'");
453 Info.Explicit = true;
454
Quentin Colombet050b2112016-03-08 01:17:03 +0000455 if (StringRef(VReg.Class.Value).equals("_")) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000456 Info.Kind = VRegInfo::GENERIC;
Justin Bogner6c7663f2017-11-17 18:51:20 +0000457 Info.D.RegBank = nullptr;
Quentin Colombet050b2112016-03-08 01:17:03 +0000458 } else {
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000459 const auto *RC = Target->getRegClass(VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000460 if (RC) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000461 Info.Kind = VRegInfo::NORMAL;
462 Info.D.RC = RC;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000463 } else {
Matt Arsenaultbdfb6cf2019-03-12 20:42:12 +0000464 const RegisterBank *RegBank = Target->getRegBank(VReg.Class.Value);
Quentin Colombet876ddf82016-04-08 16:40:43 +0000465 if (!RegBank)
466 return error(
467 VReg.Class.SourceRange.Start,
468 Twine("use of undefined register class or register bank '") +
469 VReg.Class.Value + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000470 Info.Kind = VRegInfo::REGBANK;
471 Info.D.RegBank = RegBank;
Quentin Colombet876ddf82016-04-08 16:40:43 +0000472 }
Quentin Colombet050b2112016-03-08 01:17:03 +0000473 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000474
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000475 if (!VReg.PreferredRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000476 if (Info.Kind != VRegInfo::NORMAL)
477 return error(VReg.Class.SourceRange.Start,
478 Twine("preferred register can only be set for normal vregs"));
Tom Stellard9c884e42016-11-15 00:03:14 +0000479
480 if (parseRegisterReference(PFS, Info.PreferredReg,
481 VReg.PreferredRegister.Value, Error))
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000482 return error(Error, VReg.PreferredRegister.SourceRange);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000483 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000484 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000485
486 // Parse the liveins.
487 for (const auto &LiveIn : YamlMF.LiveIns) {
488 unsigned Reg = 0;
Matthias Braune35861d2016-07-13 23:27:50 +0000489 if (parseNamedRegisterReference(PFS, Reg, LiveIn.Register.Value, Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000490 return error(Error, LiveIn.Register.SourceRange);
491 unsigned VReg = 0;
492 if (!LiveIn.VirtualRegister.Value.empty()) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000493 VRegInfo *Info;
494 if (parseVirtualRegisterReference(PFS, Info, LiveIn.VirtualRegister.Value,
Matthias Braune35861d2016-07-13 23:27:50 +0000495 Error))
Alex Lorenz12045a42015-07-27 17:42:45 +0000496 return error(Error, LiveIn.VirtualRegister.SourceRange);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000497 VReg = Info->VReg;
Alex Lorenz12045a42015-07-27 17:42:45 +0000498 }
499 RegInfo.addLiveIn(Reg, VReg);
500 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000501
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000502 // Parse the callee saved registers (Registers that will
503 // be saved for the caller).
504 if (YamlMF.CalleeSavedRegisters) {
505 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
506 for (const auto &RegSource : YamlMF.CalleeSavedRegisters.getValue()) {
507 unsigned Reg = 0;
508 if (parseNamedRegisterReference(PFS, Reg, RegSource.Value, Error))
509 return error(Error, RegSource.SourceRange);
510 CalleeSavedRegisters.push_back(Reg);
511 }
512 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
Alex Lorenzc4838082015-08-11 00:32:49 +0000513 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000514
Alex Lorenz54565cf2015-06-24 19:56:10 +0000515 return false;
516}
517
Matthias Braun74ad41c2016-10-11 03:13:01 +0000518bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
Alex Lorenzc4838082015-08-11 00:32:49 +0000519 const yaml::MachineFunction &YamlMF) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000520 MachineFunction &MF = PFS.MF;
521 MachineRegisterInfo &MRI = MF.getRegInfo();
522 bool Error = false;
523 // Create VRegs
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000524 auto populateVRegInfo = [&] (const VRegInfo &Info, Twine Name) {
Matthias Braun74ad41c2016-10-11 03:13:01 +0000525 unsigned Reg = Info.VReg;
526 switch (Info.Kind) {
527 case VRegInfo::UNKNOWN:
528 error(Twine("Cannot determine class/bank of virtual register ") +
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000529 Name + " in function '" + MF.getName() + "'");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000530 Error = true;
531 break;
532 case VRegInfo::NORMAL:
533 MRI.setRegClass(Reg, Info.D.RC);
534 if (Info.PreferredReg != 0)
535 MRI.setSimpleHint(Reg, Info.PreferredReg);
536 break;
537 case VRegInfo::GENERIC:
538 break;
539 case VRegInfo::REGBANK:
540 MRI.setRegBank(Reg, *Info.D.RegBank);
541 break;
542 }
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000543 };
544
545 for (auto I = PFS.VRegInfosNamed.begin(), E = PFS.VRegInfosNamed.end();
546 I != E; I++) {
547 const VRegInfo &Info = *I->second;
548 populateVRegInfo(Info, Twine(I->first()));
549 }
550
551 for (auto P : PFS.VRegInfos) {
552 const VRegInfo &Info = *P.second;
553 populateVRegInfo(Info, Twine(P.first));
Matthias Braun74ad41c2016-10-11 03:13:01 +0000554 }
555
556 // Compute MachineRegisterInfo::UsedPhysRegMask
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000557 for (const MachineBasicBlock &MBB : MF) {
558 for (const MachineInstr &MI : MBB) {
559 for (const MachineOperand &MO : MI.operands()) {
560 if (!MO.isRegMask())
561 continue;
562 MRI.addPhysRegsUsedFromRegMask(MO.getRegMask());
Alex Lorenzc4838082015-08-11 00:32:49 +0000563 }
564 }
565 }
Matthias Braun74ad41c2016-10-11 03:13:01 +0000566
567 // FIXME: This is a temporary workaround until the reserved registers can be
568 // serialized.
569 MRI.freezeReservedRegs(MF);
570 return Error;
Alex Lorenzc4838082015-08-11 00:32:49 +0000571}
572
Matthias Braun83947862016-07-13 22:23:23 +0000573bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
574 const yaml::MachineFunction &YamlMF) {
575 MachineFunction &MF = PFS.MF;
Matthias Braun941a7052016-07-28 18:40:00 +0000576 MachineFrameInfo &MFI = MF.getFrameInfo();
Matthias Braunf1caa282017-12-15 22:22:58 +0000577 const Function &F = MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000578 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000579 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
580 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
581 MFI.setHasStackMap(YamlMFI.HasStackMap);
582 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
583 MFI.setStackSize(YamlMFI.StackSize);
584 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
585 if (YamlMFI.MaxAlignment)
586 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
587 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
588 MFI.setHasCalls(YamlMFI.HasCalls);
Matthias Braunab9438c2017-05-01 22:32:25 +0000589 if (YamlMFI.MaxCallFrameSize != ~0u)
590 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
Reid Kleckner9ea2c012018-10-01 21:59:45 +0000591 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
Alex Lorenz60541c12015-07-09 19:55:27 +0000592 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
593 MFI.setHasVAStart(YamlMFI.HasVAStart);
594 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Francis Visoiu Mistrih537d7ee2018-04-06 08:56:25 +0000595 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000596 if (!YamlMFI.SavePoint.Value.empty()) {
597 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000598 if (parseMBBReference(PFS, MBB, YamlMFI.SavePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000599 return true;
600 MFI.setSavePoint(MBB);
601 }
602 if (!YamlMFI.RestorePoint.Value.empty()) {
603 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000604 if (parseMBBReference(PFS, MBB, YamlMFI.RestorePoint))
Alex Lorenza6f9a372015-07-29 21:09:09 +0000605 return true;
606 MFI.setRestorePoint(MBB);
607 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000608
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000609 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000610 // Initialize the fixed frame objects.
611 for (const auto &Object : YamlMF.FixedStackObjects) {
612 int ObjectIdx;
613 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
614 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
615 Object.IsImmutable, Object.IsAliased);
616 else
617 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
618 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000619 MFI.setStackID(ObjectIdx, Object.StackID);
Alex Lorenz1d9a3032015-08-10 23:45:02 +0000620 if (!PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID.Value,
621 ObjectIdx))
622 .second)
623 return error(Object.ID.SourceRange.Start,
624 Twine("redefinition of fixed stack object '%fixed-stack.") +
625 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000626 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000627 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000628 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000629 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
630 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000631 }
632
633 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000634 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000635 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000636 const AllocaInst *Alloca = nullptr;
637 const yaml::StringValue &Name = Object.Name;
638 if (!Name.Value.empty()) {
639 Alloca = dyn_cast_or_null<AllocaInst>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000640 F.getValueSymbolTable()->lookup(Name.Value));
Alex Lorenz37643a02015-07-15 22:14:49 +0000641 if (!Alloca)
642 return error(Name.SourceRange.Start,
643 "alloca instruction named '" + Name.Value +
644 "' isn't defined in the function '" + F.getName() +
645 "'");
646 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000647 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000648 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000649 else
650 ObjectIdx = MFI.CreateStackObject(
651 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000652 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000653 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000654 MFI.setStackID(ObjectIdx, Object.StackID);
655
Alex Lorenzc5d35ba2015-08-10 23:50:41 +0000656 if (!PFS.StackObjectSlots.insert(std::make_pair(Object.ID.Value, ObjectIdx))
657 .second)
658 return error(Object.ID.SourceRange.Start,
659 Twine("redefinition of stack object '%stack.") +
660 Twine(Object.ID.Value) + "'");
Matthias Braun83947862016-07-13 22:23:23 +0000661 if (parseCalleeSavedRegister(PFS, CSIInfo, Object.CalleeSavedRegister,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000662 Object.CalleeSavedRestored, ObjectIdx))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000663 return true;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000664 if (Object.LocalOffset)
665 MFI.mapLocalFrameObject(ObjectIdx, Object.LocalOffset.getValue());
Matthias Braun83947862016-07-13 22:23:23 +0000666 if (parseStackObjectsDebugInfo(PFS, Object, ObjectIdx))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000667 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000668 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000669 MFI.setCalleeSavedInfo(CSIInfo);
670 if (!CSIInfo.empty())
671 MFI.setCalleeSavedInfoValid(true);
Alex Lorenza314d812015-08-18 22:26:26 +0000672
673 // Initialize the various stack object references after initializing the
674 // stack objects.
675 if (!YamlMFI.StackProtector.Value.empty()) {
676 SMDiagnostic Error;
677 int FI;
Matthias Braune35861d2016-07-13 23:27:50 +0000678 if (parseStackObjectReference(PFS, FI, YamlMFI.StackProtector.Value, Error))
Alex Lorenza314d812015-08-18 22:26:26 +0000679 return error(Error, YamlMFI.StackProtector.SourceRange);
680 MFI.setStackProtectorIndex(FI);
681 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000682 return false;
683}
684
Matthias Braun83947862016-07-13 22:23:23 +0000685bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000686 std::vector<CalleeSavedInfo> &CSIInfo,
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000687 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000688 if (RegisterSource.Value.empty())
689 return false;
690 unsigned Reg = 0;
691 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000692 if (parseNamedRegisterReference(PFS, Reg, RegisterSource.Value, Error))
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000693 return error(Error, RegisterSource.SourceRange);
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000694 CalleeSavedInfo CSI(Reg, FrameIdx);
695 CSI.setRestored(IsRestored);
696 CSIInfo.push_back(CSI);
Alex Lorenz60541c12015-07-09 19:55:27 +0000697 return false;
698}
699
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000700/// Verify that given node is of a certain type. Return true on error.
701template <typename T>
702static bool typecheckMDNode(T *&Result, MDNode *Node,
703 const yaml::StringValue &Source,
704 StringRef TypeString, MIRParserImpl &Parser) {
705 if (!Node)
706 return false;
707 Result = dyn_cast<T>(Node);
708 if (!Result)
709 return Parser.error(Source.SourceRange.Start,
710 "expected a reference to a '" + TypeString +
711 "' metadata node");
712 return false;
713}
714
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000715template <typename T>
Matthias Braun83947862016-07-13 22:23:23 +0000716bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000717 const T &Object, int FrameIdx) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000718 // Debug information can only be attached to stack objects; Fixed stack
719 // objects aren't supported.
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000720 MDNode *Var = nullptr, *Expr = nullptr, *Loc = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000721 if (parseMDNode(PFS, Var, Object.DebugVar) ||
722 parseMDNode(PFS, Expr, Object.DebugExpr) ||
723 parseMDNode(PFS, Loc, Object.DebugLoc))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000724 return true;
725 if (!Var && !Expr && !Loc)
726 return false;
727 DILocalVariable *DIVar = nullptr;
728 DIExpression *DIExpr = nullptr;
729 DILocation *DILoc = nullptr;
730 if (typecheckMDNode(DIVar, Var, Object.DebugVar, "DILocalVariable", *this) ||
731 typecheckMDNode(DIExpr, Expr, Object.DebugExpr, "DIExpression", *this) ||
732 typecheckMDNode(DILoc, Loc, Object.DebugLoc, "DILocation", *this))
733 return true;
Francis Visoiu Mistrih57fcd342018-04-25 18:58:06 +0000734 PFS.MF.setVariableDbgInfo(DIVar, DIExpr, FrameIdx, DILoc);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000735 return false;
736}
737
Matthias Braun74ad41c2016-10-11 03:13:01 +0000738bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000739 MDNode *&Node, const yaml::StringValue &Source) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000740 if (Source.Value.empty())
741 return false;
742 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000743 if (llvm::parseMDNode(PFS, Node, Source.Value, Error))
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000744 return error(Error, Source.SourceRange);
745 return false;
746}
747
Matthias Braun83947862016-07-13 22:23:23 +0000748bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
749 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
750 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
751 const MachineFunction &MF = PFS.MF;
Matthias Braunf1caa282017-12-15 22:22:58 +0000752 const auto &M = *MF.getFunction().getParent();
Alex Lorenzab980492015-07-20 20:51:18 +0000753 SMDiagnostic Error;
754 for (const auto &YamlConstant : YamlMF.Constants) {
Diana Picusd5a00b02017-08-02 11:09:30 +0000755 if (YamlConstant.IsTargetSpecific)
756 // FIXME: Support target-specific constant pools
757 return error(YamlConstant.Value.SourceRange.Start,
758 "Can't parse target-specific constant pool entries yet");
Alex Lorenzab980492015-07-20 20:51:18 +0000759 const Constant *Value = dyn_cast_or_null<Constant>(
760 parseConstantValue(YamlConstant.Value.Value, Error, M));
761 if (!Value)
762 return error(Error, YamlConstant.Value.SourceRange);
763 unsigned Alignment =
764 YamlConstant.Alignment
765 ? YamlConstant.Alignment
766 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
Alex Lorenz60bf5992015-07-30 22:00:17 +0000767 unsigned Index = ConstantPool.getConstantPoolIndex(Value, Alignment);
768 if (!ConstantPoolSlots.insert(std::make_pair(YamlConstant.ID.Value, Index))
769 .second)
770 return error(YamlConstant.ID.SourceRange.Start,
771 Twine("redefinition of constant pool item '%const.") +
772 Twine(YamlConstant.ID.Value) + "'");
Alex Lorenzab980492015-07-20 20:51:18 +0000773 }
774 return false;
775}
776
Matthias Braun83947862016-07-13 22:23:23 +0000777bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
778 const yaml::MachineJumpTable &YamlJTI) {
779 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000780 for (const auto &Entry : YamlJTI.Entries) {
781 std::vector<MachineBasicBlock *> Blocks;
782 for (const auto &MBBSource : Entry.Blocks) {
783 MachineBasicBlock *MBB = nullptr;
Matthias Braun83947862016-07-13 22:23:23 +0000784 if (parseMBBReference(PFS, MBB, MBBSource.Value))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000785 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000786 Blocks.push_back(MBB);
787 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000788 unsigned Index = JTI->createJumpTableIndex(Blocks);
Alex Lorenz59ed5912015-07-31 23:13:23 +0000789 if (!PFS.JumpTableSlots.insert(std::make_pair(Entry.ID.Value, Index))
790 .second)
791 return error(Entry.ID.SourceRange.Start,
792 Twine("redefinition of jump table entry '%jump-table.") +
793 Twine(Entry.ID.Value) + "'");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000794 }
795 return false;
796}
797
Matthias Braun74ad41c2016-10-11 03:13:01 +0000798bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braun83947862016-07-13 22:23:23 +0000799 MachineBasicBlock *&MBB,
800 const yaml::StringValue &Source) {
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000801 SMDiagnostic Error;
Matthias Braune35861d2016-07-13 23:27:50 +0000802 if (llvm::parseMBBReference(PFS, MBB, Source.Value, Error))
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000803 return error(Error, Source.SourceRange);
804 return false;
805}
806
Alex Lorenz51af1602015-06-23 22:39:23 +0000807SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
808 SMRange SourceRange) {
809 assert(SourceRange.isValid() && "Invalid source range");
810 SMLoc Loc = SourceRange.Start;
811 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
812 *Loc.getPointer() == '\'';
813 // Translate the location of the error from the location in the MI string to
814 // the corresponding location in the MIR file.
815 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
816 (HasQuote ? 1 : 0));
817
818 // TODO: Translate any source ranges as well.
819 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
820 Error.getFixIts());
821}
822
Alex Lorenz9b62cf62015-08-13 20:30:11 +0000823SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
824 SMRange SourceRange) {
Alex Lorenz09b832c2015-05-29 17:05:41 +0000825 assert(SourceRange.isValid());
826
827 // Translate the location of the error from the location in the llvm IR string
828 // to the corresponding location in the MIR file.
829 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
830 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
831 unsigned Column = Error.getColumnNo();
832 StringRef LineStr = Error.getLineContents();
833 SMLoc Loc = Error.getLoc();
834
835 // Get the full line and adjust the column number by taking the indentation of
836 // LLVM IR into account.
837 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
838 L != E; ++L) {
839 if (L.line_number() == Line) {
840 LineStr = *L;
841 Loc = SMLoc::getFromPointer(LineStr.data());
842 auto Indent = LineStr.find(Error.getLineContents());
843 if (Indent != StringRef::npos)
844 Column += Indent;
845 break;
846 }
847 }
848
849 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
850 Error.getMessage(), LineStr, Error.getRanges(),
851 Error.getFixIts());
852}
853
Alex Lorenz735c47e2015-06-15 20:30:22 +0000854MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
855 : Impl(std::move(Impl)) {}
856
857MIRParser::~MIRParser() {}
858
Matthias Braun7bda1952017-06-06 00:44:35 +0000859std::unique_ptr<Module> MIRParser::parseIRModule() {
860 return Impl->parseIRModule();
861}
Alex Lorenz735c47e2015-06-15 20:30:22 +0000862
Matthias Braun7bda1952017-06-06 00:44:35 +0000863bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
864 return Impl->parseMachineFunctions(M, MMI);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000865}
866
867std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
868 SMDiagnostic &Error,
869 LLVMContext &Context) {
Matthias Braun7e23fc02017-06-06 20:06:57 +0000870 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000871 if (std::error_code EC = FileOrErr.getError()) {
872 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
873 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000874 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000875 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000876 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000877}
878
Alex Lorenz735c47e2015-06-15 20:30:22 +0000879std::unique_ptr<MIRParser>
880llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
881 LLVMContext &Context) {
Mehdi Amini05188a62016-09-17 05:41:02 +0000882 auto Filename = Contents->getBufferIdentifier();
Mehdi Amini8d904e92016-09-17 05:33:58 +0000883 if (Context.shouldDiscardValueNames()) {
884 Context.diagnose(DiagnosticInfoMIRParser(
885 DS_Error,
886 SMDiagnostic(
887 Filename, SourceMgr::DK_Error,
888 "Can't read MIR with a Context that discards named Values")));
889 return nullptr;
890 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000891 return llvm::make_unique<MIRParser>(
892 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000893}