blob: 4a66365cad3e250666bc210c0d78bca118f1e3b0 [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 Lorenz54565cf2015-06-24 19:56:10 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000019#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000020#include "llvm/IR/BasicBlock.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000021#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000022#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000028
29using namespace llvm;
30
31namespace {
32
33/// This class prints out the machine functions using the MIR serialization
34/// format.
35class MIRPrinter {
36 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000037 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz345c1442015-06-15 23:52:35 +000038
39public:
40 MIRPrinter(raw_ostream &OS) : OS(OS) {}
41
42 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000043
Alex Lorenz54565cf2015-06-24 19:56:10 +000044 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000045 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000046 const MachineBasicBlock &MBB);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000047
48private:
49 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000050};
51
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000052/// This class prints out the machine instructions using the MIR serialization
53/// format.
54class MIPrinter {
55 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000056 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000057 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058
59public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000060 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000061 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000062 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063
64 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000065 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000066 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067};
68
Alex Lorenz345c1442015-06-15 23:52:35 +000069} // end anonymous namespace
70
71namespace llvm {
72namespace yaml {
73
74/// This struct serializes the LLVM IR module.
75template <> struct BlockScalarTraits<Module> {
76 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
77 Mod.print(OS, nullptr);
78 }
79 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
80 llvm_unreachable("LLVM Module is supposed to be parsed separately");
81 return "";
82 }
83};
84
85} // end namespace yaml
86} // end namespace llvm
87
88void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +000089 initRegisterMaskIds(MF);
90
Alex Lorenz345c1442015-06-15 23:52:35 +000091 yaml::MachineFunction YamlMF;
92 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000093 YamlMF.Alignment = MF.getAlignment();
94 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
95 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz54565cf2015-06-24 19:56:10 +000096 convert(YamlMF, MF.getRegInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +000097
98 int I = 0;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000099 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000100 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000101 // TODO: Allow printing of non sequentially numbered MBBs.
102 // This is currently needed as the basic block references get their index
103 // from MBB.getNumber(), thus it should be sequential so that the parser can
104 // map back to the correct MBBs when parsing the output.
105 assert(MBB.getNumber() == I++ &&
106 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000107 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000108 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000109 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000110 YamlMF.BasicBlocks.push_back(YamlMBB);
111 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000112 yaml::Output Out(OS);
113 Out << YamlMF;
114}
115
Alex Lorenz54565cf2015-06-24 19:56:10 +0000116void MIRPrinter::convert(yaml::MachineFunction &MF,
117 const MachineRegisterInfo &RegInfo) {
118 MF.IsSSA = RegInfo.isSSA();
119 MF.TracksRegLiveness = RegInfo.tracksLiveness();
120 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
121}
122
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000123void MIRPrinter::convert(ModuleSlotTracker &MST,
124 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000125 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000126 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
127 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000128 // TODO: Serialize unnamed BB references.
129 if (const auto *BB = MBB.getBasicBlock())
130 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
131 else
132 YamlMBB.Name = "";
133 YamlMBB.Alignment = MBB.getAlignment();
134 YamlMBB.AddressTaken = MBB.hasAddressTaken();
135 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000136 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000137 std::string Str;
138 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000139 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000140 YamlMBB.Successors.push_back(StrOS.str());
141 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000142
143 // Print the machine instructions.
144 YamlMBB.Instructions.reserve(MBB.size());
145 std::string Str;
146 for (const auto &MI : MBB) {
147 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000148 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000149 YamlMBB.Instructions.push_back(StrOS.str());
150 Str.clear();
151 }
152}
153
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000154void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
155 const auto *TRI = MF.getSubtarget().getRegisterInfo();
156 unsigned I = 0;
157 for (const uint32_t *Mask : TRI->getRegMasks())
158 RegisterMaskIds.insert(std::make_pair(Mask, I++));
159}
160
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000161void MIPrinter::print(const MachineInstr &MI) {
162 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000163 const auto *TRI = SubTarget.getRegisterInfo();
164 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000165 const auto *TII = SubTarget.getInstrInfo();
166 assert(TII && "Expected target instruction info");
167
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000168 unsigned I = 0, E = MI.getNumOperands();
169 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
170 !MI.getOperand(I).isImplicit();
171 ++I) {
172 if (I)
173 OS << ", ";
174 print(MI.getOperand(I), TRI);
175 }
176
177 if (I)
178 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000179 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000180 // TODO: Print the instruction flags, machine mem operands.
181 if (I < E)
182 OS << ' ';
183
184 bool NeedComma = false;
185 for (; I < E; ++I) {
186 if (NeedComma)
187 OS << ", ";
188 print(MI.getOperand(I), TRI);
189 NeedComma = true;
190 }
191}
192
193static void printReg(unsigned Reg, raw_ostream &OS,
194 const TargetRegisterInfo *TRI) {
195 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000196 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000197 if (!Reg)
198 OS << '_';
199 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000200 OS << '%' << StringRef(TRI->getName(Reg)).lower();
201 else
202 llvm_unreachable("Can't print this kind of register yet");
203}
204
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000205void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
206 OS << "%bb." << MBB.getNumber();
207 if (const auto *BB = MBB.getBasicBlock()) {
208 if (BB->hasName())
209 OS << '.' << BB->getName();
210 }
211}
212
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000213void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
214 switch (Op.getType()) {
215 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000216 // TODO: Print the other register flags.
217 if (Op.isImplicit())
218 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000219 if (Op.isDead())
220 OS << "dead ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000221 printReg(Op.getReg(), OS, TRI);
222 // TODO: Print sub register.
223 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000224 case MachineOperand::MO_Immediate:
225 OS << Op.getImm();
226 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000227 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000228 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000229 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000230 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000231 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000232 // TODO: Print offset and target flags.
233 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000234 case MachineOperand::MO_RegisterMask: {
235 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
236 if (RegMaskInfo != RegisterMaskIds.end())
237 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
238 else
239 llvm_unreachable("Can't print this machine register mask yet.");
240 break;
241 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000242 default:
243 // TODO: Print the other machine operands.
244 llvm_unreachable("Can't print this machine operand at the moment");
245 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000246}
247
Alex Lorenz345c1442015-06-15 23:52:35 +0000248void llvm::printMIR(raw_ostream &OS, const Module &M) {
249 yaml::Output Out(OS);
250 Out << const_cast<Module &>(M);
251}
252
253void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
254 MIRPrinter Printer(OS);
255 Printer.print(MF);
256}