blob: 3ca98240d94855bd780b6929de5c5eef0a333fc1 [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 Lorenz8f6f4282015-06-29 16:57:06 +000050
51private:
52 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000053};
54
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000055/// This class prints out the machine instructions using the MIR serialization
56/// format.
57class MIPrinter {
58 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000059 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000060 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061
62public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000063 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000064 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000065 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066
67 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000068 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000069 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000070};
71
Alex Lorenz345c1442015-06-15 23:52:35 +000072} // end anonymous namespace
73
74namespace llvm {
75namespace yaml {
76
77/// This struct serializes the LLVM IR module.
78template <> struct BlockScalarTraits<Module> {
79 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
80 Mod.print(OS, nullptr);
81 }
82 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
83 llvm_unreachable("LLVM Module is supposed to be parsed separately");
84 return "";
85 }
86};
87
88} // end namespace yaml
89} // end namespace llvm
90
91void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +000092 initRegisterMaskIds(MF);
93
Alex Lorenz345c1442015-06-15 23:52:35 +000094 yaml::MachineFunction YamlMF;
95 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000096 YamlMF.Alignment = MF.getAlignment();
97 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
98 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +000099 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000100 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000101
102 int I = 0;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000103 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000104 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000105 // TODO: Allow printing of non sequentially numbered MBBs.
106 // This is currently needed as the basic block references get their index
107 // from MBB.getNumber(), thus it should be sequential so that the parser can
108 // map back to the correct MBBs when parsing the output.
109 assert(MBB.getNumber() == I++ &&
110 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000111 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000112 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000113 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000114 YamlMF.BasicBlocks.push_back(YamlMBB);
115 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000116 yaml::Output Out(OS);
117 Out << YamlMF;
118}
119
Alex Lorenz54565cf2015-06-24 19:56:10 +0000120void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000121 const MachineRegisterInfo &RegInfo,
122 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000123 MF.IsSSA = RegInfo.isSSA();
124 MF.TracksRegLiveness = RegInfo.tracksLiveness();
125 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000126
127 // Print the virtual register definitions.
128 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
129 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
130 yaml::VirtualRegisterDefinition VReg;
131 VReg.ID = I;
132 VReg.Class =
133 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
134 MF.VirtualRegisters.push_back(VReg);
135 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000136}
137
Alex Lorenz60541c12015-07-09 19:55:27 +0000138void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
139 const MachineFrameInfo &MFI) {
140 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
141 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
142 YamlMFI.HasStackMap = MFI.hasStackMap();
143 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
144 YamlMFI.StackSize = MFI.getStackSize();
145 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
146 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
147 YamlMFI.AdjustsStack = MFI.adjustsStack();
148 YamlMFI.HasCalls = MFI.hasCalls();
149 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
150 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
151 YamlMFI.HasVAStart = MFI.hasVAStart();
152 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
153}
154
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000155void MIRPrinter::convert(ModuleSlotTracker &MST,
156 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000157 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000158 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
159 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000160 // TODO: Serialize unnamed BB references.
161 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000162 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000163 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000164 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000165 YamlMBB.Alignment = MBB.getAlignment();
166 YamlMBB.AddressTaken = MBB.hasAddressTaken();
167 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000168 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000169 std::string Str;
170 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000171 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000172 YamlMBB.Successors.push_back(StrOS.str());
173 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000174
175 // Print the machine instructions.
176 YamlMBB.Instructions.reserve(MBB.size());
177 std::string Str;
178 for (const auto &MI : MBB) {
179 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000180 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000181 YamlMBB.Instructions.push_back(StrOS.str());
182 Str.clear();
183 }
184}
185
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000186void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
187 const auto *TRI = MF.getSubtarget().getRegisterInfo();
188 unsigned I = 0;
189 for (const uint32_t *Mask : TRI->getRegMasks())
190 RegisterMaskIds.insert(std::make_pair(Mask, I++));
191}
192
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000193void MIPrinter::print(const MachineInstr &MI) {
194 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000195 const auto *TRI = SubTarget.getRegisterInfo();
196 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000197 const auto *TII = SubTarget.getInstrInfo();
198 assert(TII && "Expected target instruction info");
199
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000200 unsigned I = 0, E = MI.getNumOperands();
201 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
202 !MI.getOperand(I).isImplicit();
203 ++I) {
204 if (I)
205 OS << ", ";
206 print(MI.getOperand(I), TRI);
207 }
208
209 if (I)
210 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000211 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000212 // TODO: Print the instruction flags, machine mem operands.
213 if (I < E)
214 OS << ' ';
215
216 bool NeedComma = false;
217 for (; I < E; ++I) {
218 if (NeedComma)
219 OS << ", ";
220 print(MI.getOperand(I), TRI);
221 NeedComma = true;
222 }
223}
224
225static void printReg(unsigned Reg, raw_ostream &OS,
226 const TargetRegisterInfo *TRI) {
227 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000228 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000229 if (!Reg)
230 OS << '_';
231 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000232 OS << '%' << StringRef(TRI->getName(Reg)).lower();
233 else
234 llvm_unreachable("Can't print this kind of register yet");
235}
236
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000237void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
238 OS << "%bb." << MBB.getNumber();
239 if (const auto *BB = MBB.getBasicBlock()) {
240 if (BB->hasName())
241 OS << '.' << BB->getName();
242 }
243}
244
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000245void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
246 switch (Op.getType()) {
247 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000248 // TODO: Print the other register flags.
249 if (Op.isImplicit())
250 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000251 if (Op.isDead())
252 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000253 if (Op.isKill())
254 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000255 if (Op.isUndef())
256 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000257 printReg(Op.getReg(), OS, TRI);
258 // TODO: Print sub register.
259 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000260 case MachineOperand::MO_Immediate:
261 OS << Op.getImm();
262 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000263 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000264 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000265 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000266 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000267 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000268 // TODO: Print offset and target flags.
269 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000270 case MachineOperand::MO_RegisterMask: {
271 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
272 if (RegMaskInfo != RegisterMaskIds.end())
273 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
274 else
275 llvm_unreachable("Can't print this machine register mask yet.");
276 break;
277 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000278 default:
279 // TODO: Print the other machine operands.
280 llvm_unreachable("Can't print this machine operand at the moment");
281 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000282}
283
Alex Lorenz345c1442015-06-15 23:52:35 +0000284void llvm::printMIR(raw_ostream &OS, const Module &M) {
285 yaml::Output Out(OS);
286 Out << const_cast<Module &>(M);
287}
288
289void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
290 MIRPrinter Printer(OS);
291 Printer.print(MF);
292}