blob: de747783468749ba719a7393fd27e64cc3fd359d [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"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000025#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000027
28using namespace llvm;
29
30namespace {
31
32/// This class prints out the machine functions using the MIR serialization
33/// format.
34class MIRPrinter {
35 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000036 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz345c1442015-06-15 23:52:35 +000037
38public:
39 MIRPrinter(raw_ostream &OS) : OS(OS) {}
40
41 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000042
Alex Lorenz54565cf2015-06-24 19:56:10 +000043 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo);
Alex Lorenz5d6108e2015-06-26 22:56:48 +000044 void convert(const Module &M, yaml::MachineBasicBlock &YamlMBB,
45 const MachineBasicBlock &MBB);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000046
47private:
48 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000049};
50
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000051/// This class prints out the machine instructions using the MIR serialization
52/// format.
53class MIPrinter {
Alex Lorenz5d6108e2015-06-26 22:56:48 +000054 const Module &M;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000055 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000056 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000057
58public:
Alex Lorenz8f6f4282015-06-29 16:57:06 +000059 MIPrinter(const Module &M, raw_ostream &OS,
60 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
61 : M(M), OS(OS), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000062
63 void print(const MachineInstr &MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000064 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000065};
66
Alex Lorenz345c1442015-06-15 23:52:35 +000067} // end anonymous namespace
68
69namespace llvm {
70namespace yaml {
71
72/// This struct serializes the LLVM IR module.
73template <> struct BlockScalarTraits<Module> {
74 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
75 Mod.print(OS, nullptr);
76 }
77 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
78 llvm_unreachable("LLVM Module is supposed to be parsed separately");
79 return "";
80 }
81};
82
83} // end namespace yaml
84} // end namespace llvm
85
86void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +000087 initRegisterMaskIds(MF);
88
Alex Lorenz345c1442015-06-15 23:52:35 +000089 yaml::MachineFunction YamlMF;
90 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000091 YamlMF.Alignment = MF.getAlignment();
92 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
93 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz54565cf2015-06-24 19:56:10 +000094 convert(YamlMF, MF.getRegInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +000095
96 int I = 0;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000097 const auto &M = *MF.getFunction()->getParent();
Alex Lorenz4f093bf2015-06-19 17:43:07 +000098 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +000099 // TODO: Allow printing of non sequentially numbered MBBs.
100 // This is currently needed as the basic block references get their index
101 // from MBB.getNumber(), thus it should be sequential so that the parser can
102 // map back to the correct MBBs when parsing the output.
103 assert(MBB.getNumber() == I++ &&
104 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000105 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000106 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000107 convert(M, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000108 YamlMF.BasicBlocks.push_back(YamlMBB);
109 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000110 yaml::Output Out(OS);
111 Out << YamlMF;
112}
113
Alex Lorenz54565cf2015-06-24 19:56:10 +0000114void MIRPrinter::convert(yaml::MachineFunction &MF,
115 const MachineRegisterInfo &RegInfo) {
116 MF.IsSSA = RegInfo.isSSA();
117 MF.TracksRegLiveness = RegInfo.tracksLiveness();
118 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
119}
120
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000121void MIRPrinter::convert(const Module &M, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000122 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000123 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
124 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000125 // TODO: Serialize unnamed BB references.
126 if (const auto *BB = MBB.getBasicBlock())
127 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
128 else
129 YamlMBB.Name = "";
130 YamlMBB.Alignment = MBB.getAlignment();
131 YamlMBB.AddressTaken = MBB.hasAddressTaken();
132 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000133
134 // Print the machine instructions.
135 YamlMBB.Instructions.reserve(MBB.size());
136 std::string Str;
137 for (const auto &MI : MBB) {
138 raw_string_ostream StrOS(Str);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000139 MIPrinter(M, StrOS, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000140 YamlMBB.Instructions.push_back(StrOS.str());
141 Str.clear();
142 }
143}
144
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000145void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
146 const auto *TRI = MF.getSubtarget().getRegisterInfo();
147 unsigned I = 0;
148 for (const uint32_t *Mask : TRI->getRegMasks())
149 RegisterMaskIds.insert(std::make_pair(Mask, I++));
150}
151
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000152void MIPrinter::print(const MachineInstr &MI) {
153 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000154 const auto *TRI = SubTarget.getRegisterInfo();
155 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000156 const auto *TII = SubTarget.getInstrInfo();
157 assert(TII && "Expected target instruction info");
158
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000159 unsigned I = 0, E = MI.getNumOperands();
160 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
161 !MI.getOperand(I).isImplicit();
162 ++I) {
163 if (I)
164 OS << ", ";
165 print(MI.getOperand(I), TRI);
166 }
167
168 if (I)
169 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000170 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171 // TODO: Print the instruction flags, machine mem operands.
172 if (I < E)
173 OS << ' ';
174
175 bool NeedComma = false;
176 for (; I < E; ++I) {
177 if (NeedComma)
178 OS << ", ";
179 print(MI.getOperand(I), TRI);
180 NeedComma = true;
181 }
182}
183
184static void printReg(unsigned Reg, raw_ostream &OS,
185 const TargetRegisterInfo *TRI) {
186 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000187 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000188 if (!Reg)
189 OS << '_';
190 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000191 OS << '%' << StringRef(TRI->getName(Reg)).lower();
192 else
193 llvm_unreachable("Can't print this kind of register yet");
194}
195
196void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
197 switch (Op.getType()) {
198 case MachineOperand::MO_Register:
199 // TODO: Print register flags.
200 printReg(Op.getReg(), OS, TRI);
201 // TODO: Print sub register.
202 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000203 case MachineOperand::MO_Immediate:
204 OS << Op.getImm();
205 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000206 case MachineOperand::MO_MachineBasicBlock:
207 OS << "%bb." << Op.getMBB()->getNumber();
208 if (const auto *BB = Op.getMBB()->getBasicBlock()) {
209 if (BB->hasName())
210 OS << '.' << BB->getName();
211 }
212 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000213 case MachineOperand::MO_GlobalAddress:
214 // FIXME: Make this faster - print as operand will create a slot tracker to
215 // print unnamed values for the whole module every time it's called, which
216 // is inefficient.
217 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, &M);
218 // TODO: Print offset and target flags.
219 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000220 case MachineOperand::MO_RegisterMask: {
221 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
222 if (RegMaskInfo != RegisterMaskIds.end())
223 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
224 else
225 llvm_unreachable("Can't print this machine register mask yet.");
226 break;
227 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000228 default:
229 // TODO: Print the other machine operands.
230 llvm_unreachable("Can't print this machine operand at the moment");
231 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000232}
233
Alex Lorenz345c1442015-06-15 23:52:35 +0000234void llvm::printMIR(raw_ostream &OS, const Module &M) {
235 yaml::Output Out(OS);
236 Out << const_cast<Module &>(M);
237}
238
239void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
240 MIRPrinter Printer(OS);
241 Printer.print(MF);
242}