blob: 18c976bcd3e79988e944d6bfd7837c2f993ef37c [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 Lorenz5d26fa82015-06-30 18:00:16 +000064 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000065 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066};
67
Alex Lorenz345c1442015-06-15 23:52:35 +000068} // end anonymous namespace
69
70namespace llvm {
71namespace yaml {
72
73/// This struct serializes the LLVM IR module.
74template <> struct BlockScalarTraits<Module> {
75 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
76 Mod.print(OS, nullptr);
77 }
78 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
79 llvm_unreachable("LLVM Module is supposed to be parsed separately");
80 return "";
81 }
82};
83
84} // end namespace yaml
85} // end namespace llvm
86
87void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +000088 initRegisterMaskIds(MF);
89
Alex Lorenz345c1442015-06-15 23:52:35 +000090 yaml::MachineFunction YamlMF;
91 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000092 YamlMF.Alignment = MF.getAlignment();
93 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
94 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz54565cf2015-06-24 19:56:10 +000095 convert(YamlMF, MF.getRegInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +000096
97 int I = 0;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000098 const auto &M = *MF.getFunction()->getParent();
Alex Lorenz4f093bf2015-06-19 17:43:07 +000099 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000100 // TODO: Allow printing of non sequentially numbered MBBs.
101 // This is currently needed as the basic block references get their index
102 // from MBB.getNumber(), thus it should be sequential so that the parser can
103 // map back to the correct MBBs when parsing the output.
104 assert(MBB.getNumber() == I++ &&
105 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000106 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000107 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000108 convert(M, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000109 YamlMF.BasicBlocks.push_back(YamlMBB);
110 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000111 yaml::Output Out(OS);
112 Out << YamlMF;
113}
114
Alex Lorenz54565cf2015-06-24 19:56:10 +0000115void MIRPrinter::convert(yaml::MachineFunction &MF,
116 const MachineRegisterInfo &RegInfo) {
117 MF.IsSSA = RegInfo.isSSA();
118 MF.TracksRegLiveness = RegInfo.tracksLiveness();
119 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
120}
121
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000122void MIRPrinter::convert(const Module &M, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000123 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000124 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
125 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000126 // TODO: Serialize unnamed BB references.
127 if (const auto *BB = MBB.getBasicBlock())
128 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
129 else
130 YamlMBB.Name = "";
131 YamlMBB.Alignment = MBB.getAlignment();
132 YamlMBB.AddressTaken = MBB.hasAddressTaken();
133 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000134
135 // Print the machine instructions.
136 YamlMBB.Instructions.reserve(MBB.size());
137 std::string Str;
138 for (const auto &MI : MBB) {
139 raw_string_ostream StrOS(Str);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000140 MIPrinter(M, StrOS, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000141 YamlMBB.Instructions.push_back(StrOS.str());
142 Str.clear();
143 }
144}
145
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000146void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
147 const auto *TRI = MF.getSubtarget().getRegisterInfo();
148 unsigned I = 0;
149 for (const uint32_t *Mask : TRI->getRegMasks())
150 RegisterMaskIds.insert(std::make_pair(Mask, I++));
151}
152
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000153void MIPrinter::print(const MachineInstr &MI) {
154 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000155 const auto *TRI = SubTarget.getRegisterInfo();
156 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000157 const auto *TII = SubTarget.getInstrInfo();
158 assert(TII && "Expected target instruction info");
159
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000160 unsigned I = 0, E = MI.getNumOperands();
161 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
162 !MI.getOperand(I).isImplicit();
163 ++I) {
164 if (I)
165 OS << ", ";
166 print(MI.getOperand(I), TRI);
167 }
168
169 if (I)
170 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000171 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000172 // TODO: Print the instruction flags, machine mem operands.
173 if (I < E)
174 OS << ' ';
175
176 bool NeedComma = false;
177 for (; I < E; ++I) {
178 if (NeedComma)
179 OS << ", ";
180 print(MI.getOperand(I), TRI);
181 NeedComma = true;
182 }
183}
184
185static void printReg(unsigned Reg, raw_ostream &OS,
186 const TargetRegisterInfo *TRI) {
187 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000188 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000189 if (!Reg)
190 OS << '_';
191 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000192 OS << '%' << StringRef(TRI->getName(Reg)).lower();
193 else
194 llvm_unreachable("Can't print this kind of register yet");
195}
196
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000197void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
198 OS << "%bb." << MBB.getNumber();
199 if (const auto *BB = MBB.getBasicBlock()) {
200 if (BB->hasName())
201 OS << '.' << BB->getName();
202 }
203}
204
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000205void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
206 switch (Op.getType()) {
207 case MachineOperand::MO_Register:
208 // TODO: Print register flags.
209 printReg(Op.getReg(), OS, TRI);
210 // TODO: Print sub register.
211 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000212 case MachineOperand::MO_Immediate:
213 OS << Op.getImm();
214 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000215 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000216 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000217 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000218 case MachineOperand::MO_GlobalAddress:
219 // FIXME: Make this faster - print as operand will create a slot tracker to
220 // print unnamed values for the whole module every time it's called, which
221 // is inefficient.
222 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, &M);
223 // TODO: Print offset and target flags.
224 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000225 case MachineOperand::MO_RegisterMask: {
226 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
227 if (RegMaskInfo != RegisterMaskIds.end())
228 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
229 else
230 llvm_unreachable("Can't print this machine register mask yet.");
231 break;
232 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000233 default:
234 // TODO: Print the other machine operands.
235 llvm_unreachable("Can't print this machine operand at the moment");
236 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000237}
238
Alex Lorenz345c1442015-06-15 23:52:35 +0000239void llvm::printMIR(raw_ostream &OS, const Module &M) {
240 yaml::Output Out(OS);
241 Out << const_cast<Module &>(M);
242}
243
244void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
245 MIRPrinter Printer(OS);
246 Printer.print(MF);
247}