blob: d5cf9244199e0123717c3346290012d8173a339b [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
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 prints out the LLVM IR and machine
11// functions using the MIR serialization format.
12//
13//===----------------------------------------------------------------------===//
14
15#include "MIRPrinter.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000020#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000021#include "llvm/IR/BasicBlock.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000022#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000023#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000029
30using namespace llvm;
31
32namespace {
33
34/// This class prints out the machine functions using the MIR serialization
35/// format.
36class MIRPrinter {
37 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000038 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz345c1442015-06-15 23:52:35 +000039
40public:
41 MIRPrinter(raw_ostream &OS) : OS(OS) {}
42
43 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000044
Alex Lorenz28148ba2015-07-09 22:23:13 +000045 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
46 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000047 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000048 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000049 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000050 void convertStackObjects(yaml::MachineFunction &MF,
51 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000052
53private:
54 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000055};
56
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000057/// This class prints out the machine instructions using the MIR serialization
58/// format.
59class MIPrinter {
60 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000061 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000062 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063
64public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000065 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000066 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000067 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000068
69 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000070 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000071 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072};
73
Alex Lorenz345c1442015-06-15 23:52:35 +000074} // end anonymous namespace
75
76namespace llvm {
77namespace yaml {
78
79/// This struct serializes the LLVM IR module.
80template <> struct BlockScalarTraits<Module> {
81 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
82 Mod.print(OS, nullptr);
83 }
84 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
85 llvm_unreachable("LLVM Module is supposed to be parsed separately");
86 return "";
87 }
88};
89
90} // end namespace yaml
91} // end namespace llvm
92
Alex Lorenz15a00a82015-07-14 21:18:25 +000093static void printReg(unsigned Reg, raw_ostream &OS,
94 const TargetRegisterInfo *TRI) {
95 // TODO: Print Stack Slots.
96 if (!Reg)
97 OS << '_';
98 else if (TargetRegisterInfo::isVirtualRegister(Reg))
99 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
100 else if (Reg < TRI->getNumRegs())
101 OS << '%' << StringRef(TRI->getName(Reg)).lower();
102 else
103 llvm_unreachable("Can't print this kind of register yet");
104}
105
Alex Lorenz345c1442015-06-15 23:52:35 +0000106void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000107 initRegisterMaskIds(MF);
108
Alex Lorenz345c1442015-06-15 23:52:35 +0000109 yaml::MachineFunction YamlMF;
110 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000111 YamlMF.Alignment = MF.getAlignment();
112 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
113 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000114 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000115 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000116 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000117
118 int I = 0;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000119 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000120 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000121 // TODO: Allow printing of non sequentially numbered MBBs.
122 // This is currently needed as the basic block references get their index
123 // from MBB.getNumber(), thus it should be sequential so that the parser can
124 // map back to the correct MBBs when parsing the output.
125 assert(MBB.getNumber() == I++ &&
126 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000127 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000128 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000129 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000130 YamlMF.BasicBlocks.push_back(YamlMBB);
131 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000132 yaml::Output Out(OS);
133 Out << YamlMF;
134}
135
Alex Lorenz54565cf2015-06-24 19:56:10 +0000136void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000137 const MachineRegisterInfo &RegInfo,
138 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000139 MF.IsSSA = RegInfo.isSSA();
140 MF.TracksRegLiveness = RegInfo.tracksLiveness();
141 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000142
143 // Print the virtual register definitions.
144 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
145 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
146 yaml::VirtualRegisterDefinition VReg;
147 VReg.ID = I;
148 VReg.Class =
149 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
150 MF.VirtualRegisters.push_back(VReg);
151 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000152}
153
Alex Lorenz60541c12015-07-09 19:55:27 +0000154void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
155 const MachineFrameInfo &MFI) {
156 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
157 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
158 YamlMFI.HasStackMap = MFI.hasStackMap();
159 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
160 YamlMFI.StackSize = MFI.getStackSize();
161 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
162 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
163 YamlMFI.AdjustsStack = MFI.adjustsStack();
164 YamlMFI.HasCalls = MFI.hasCalls();
165 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
166 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
167 YamlMFI.HasVAStart = MFI.hasVAStart();
168 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
169}
170
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000171void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
172 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000173 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000174 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000175 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
176 if (MFI.isDeadObjectIndex(I))
177 continue;
178
179 yaml::FixedMachineStackObject YamlObject;
180 YamlObject.ID = ID++;
181 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
182 ? yaml::FixedMachineStackObject::SpillSlot
183 : yaml::FixedMachineStackObject::DefaultType;
184 YamlObject.Offset = MFI.getObjectOffset(I);
185 YamlObject.Size = MFI.getObjectSize(I);
186 YamlObject.Alignment = MFI.getObjectAlignment(I);
187 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
188 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
189 MF.FixedStackObjects.push_back(YamlObject);
190 // TODO: Store the mapping between fixed object IDs and object indices to
191 // print the fixed stack object references correctly.
192 }
193
194 // Process ordinary stack objects.
195 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000196 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
197 if (MFI.isDeadObjectIndex(I))
198 continue;
199
200 yaml::MachineStackObject YamlObject;
201 YamlObject.ID = ID++;
202 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
203 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000204 : MFI.isVariableSizedObjectIndex(I)
205 ? yaml::MachineStackObject::VariableSized
206 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000207 YamlObject.Offset = MFI.getObjectOffset(I);
208 YamlObject.Size = MFI.getObjectSize(I);
209 YamlObject.Alignment = MFI.getObjectAlignment(I);
210
211 MF.StackObjects.push_back(YamlObject);
212 // TODO: Store the mapping between object IDs and object indices to print
213 // the stack object references correctly.
214 }
215}
216
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000217void MIRPrinter::convert(ModuleSlotTracker &MST,
218 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000219 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000220 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
221 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000222 // TODO: Serialize unnamed BB references.
223 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000224 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000225 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000226 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000227 YamlMBB.Alignment = MBB.getAlignment();
228 YamlMBB.AddressTaken = MBB.hasAddressTaken();
229 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000230 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000231 std::string Str;
232 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000233 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000234 YamlMBB.Successors.push_back(StrOS.str());
235 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000236 // Print the live in registers.
237 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
238 assert(TRI && "Expected target register info");
239 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
240 std::string Str;
241 raw_string_ostream StrOS(Str);
242 printReg(*I, StrOS, TRI);
243 YamlMBB.LiveIns.push_back(StrOS.str());
244 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000245 // Print the machine instructions.
246 YamlMBB.Instructions.reserve(MBB.size());
247 std::string Str;
248 for (const auto &MI : MBB) {
249 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000250 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000251 YamlMBB.Instructions.push_back(StrOS.str());
252 Str.clear();
253 }
254}
255
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000256void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
257 const auto *TRI = MF.getSubtarget().getRegisterInfo();
258 unsigned I = 0;
259 for (const uint32_t *Mask : TRI->getRegMasks())
260 RegisterMaskIds.insert(std::make_pair(Mask, I++));
261}
262
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000263void MIPrinter::print(const MachineInstr &MI) {
264 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000265 const auto *TRI = SubTarget.getRegisterInfo();
266 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000267 const auto *TII = SubTarget.getInstrInfo();
268 assert(TII && "Expected target instruction info");
269
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000270 unsigned I = 0, E = MI.getNumOperands();
271 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
272 !MI.getOperand(I).isImplicit();
273 ++I) {
274 if (I)
275 OS << ", ";
276 print(MI.getOperand(I), TRI);
277 }
278
279 if (I)
280 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000281 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000282 // TODO: Print the instruction flags, machine mem operands.
283 if (I < E)
284 OS << ' ';
285
286 bool NeedComma = false;
287 for (; I < E; ++I) {
288 if (NeedComma)
289 OS << ", ";
290 print(MI.getOperand(I), TRI);
291 NeedComma = true;
292 }
293}
294
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000295void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
296 OS << "%bb." << MBB.getNumber();
297 if (const auto *BB = MBB.getBasicBlock()) {
298 if (BB->hasName())
299 OS << '.' << BB->getName();
300 }
301}
302
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000303void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
304 switch (Op.getType()) {
305 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000306 // TODO: Print the other register flags.
307 if (Op.isImplicit())
308 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000309 if (Op.isDead())
310 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000311 if (Op.isKill())
312 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000313 if (Op.isUndef())
314 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000315 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000316 // Print the sub register.
317 if (Op.getSubReg() != 0)
318 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000319 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000320 case MachineOperand::MO_Immediate:
321 OS << Op.getImm();
322 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000323 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000324 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000325 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000326 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000327 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000328 // TODO: Print offset and target flags.
329 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000330 case MachineOperand::MO_RegisterMask: {
331 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
332 if (RegMaskInfo != RegisterMaskIds.end())
333 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
334 else
335 llvm_unreachable("Can't print this machine register mask yet.");
336 break;
337 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000338 default:
339 // TODO: Print the other machine operands.
340 llvm_unreachable("Can't print this machine operand at the moment");
341 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000342}
343
Alex Lorenz345c1442015-06-15 23:52:35 +0000344void llvm::printMIR(raw_ostream &OS, const Module &M) {
345 yaml::Output Out(OS);
346 Out << const_cast<Module &>(M);
347}
348
349void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
350 MIRPrinter Printer(OS);
351 Printer.print(MF);
352}