blob: 5e7871e7ee886f6e6e222c754c8f8d175dff9799 [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;
36
37public:
38 MIRPrinter(raw_ostream &OS) : OS(OS) {}
39
40 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000041
Alex Lorenz54565cf2015-06-24 19:56:10 +000042 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo);
Alex Lorenz5d6108e2015-06-26 22:56:48 +000043 void convert(const Module &M, yaml::MachineBasicBlock &YamlMBB,
44 const MachineBasicBlock &MBB);
Alex Lorenz345c1442015-06-15 23:52:35 +000045};
46
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000047/// This class prints out the machine instructions using the MIR serialization
48/// format.
49class MIPrinter {
Alex Lorenz5d6108e2015-06-26 22:56:48 +000050 const Module &M;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000051 raw_ostream &OS;
52
53public:
Alex Lorenz5d6108e2015-06-26 22:56:48 +000054 MIPrinter(const Module &M, raw_ostream &OS) : M(M), OS(OS) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000055
56 void print(const MachineInstr &MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000057 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058};
59
Alex Lorenz345c1442015-06-15 23:52:35 +000060} // end anonymous namespace
61
62namespace llvm {
63namespace yaml {
64
65/// This struct serializes the LLVM IR module.
66template <> struct BlockScalarTraits<Module> {
67 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
68 Mod.print(OS, nullptr);
69 }
70 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
71 llvm_unreachable("LLVM Module is supposed to be parsed separately");
72 return "";
73 }
74};
75
76} // end namespace yaml
77} // end namespace llvm
78
79void MIRPrinter::print(const MachineFunction &MF) {
80 yaml::MachineFunction YamlMF;
81 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000082 YamlMF.Alignment = MF.getAlignment();
83 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
84 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz54565cf2015-06-24 19:56:10 +000085 convert(YamlMF, MF.getRegInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +000086
87 int I = 0;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000088 const auto &M = *MF.getFunction()->getParent();
Alex Lorenz4f093bf2015-06-19 17:43:07 +000089 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +000090 // TODO: Allow printing of non sequentially numbered MBBs.
91 // This is currently needed as the basic block references get their index
92 // from MBB.getNumber(), thus it should be sequential so that the parser can
93 // map back to the correct MBBs when parsing the output.
94 assert(MBB.getNumber() == I++ &&
95 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +000096 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +000097 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000098 convert(M, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000099 YamlMF.BasicBlocks.push_back(YamlMBB);
100 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000101 yaml::Output Out(OS);
102 Out << YamlMF;
103}
104
Alex Lorenz54565cf2015-06-24 19:56:10 +0000105void MIRPrinter::convert(yaml::MachineFunction &MF,
106 const MachineRegisterInfo &RegInfo) {
107 MF.IsSSA = RegInfo.isSSA();
108 MF.TracksRegLiveness = RegInfo.tracksLiveness();
109 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
110}
111
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000112void MIRPrinter::convert(const Module &M, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000113 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000114 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
115 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000116 // TODO: Serialize unnamed BB references.
117 if (const auto *BB = MBB.getBasicBlock())
118 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
119 else
120 YamlMBB.Name = "";
121 YamlMBB.Alignment = MBB.getAlignment();
122 YamlMBB.AddressTaken = MBB.hasAddressTaken();
123 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000124
125 // Print the machine instructions.
126 YamlMBB.Instructions.reserve(MBB.size());
127 std::string Str;
128 for (const auto &MI : MBB) {
129 raw_string_ostream StrOS(Str);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000130 MIPrinter(M, StrOS).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000131 YamlMBB.Instructions.push_back(StrOS.str());
132 Str.clear();
133 }
134}
135
136void MIPrinter::print(const MachineInstr &MI) {
137 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000138 const auto *TRI = SubTarget.getRegisterInfo();
139 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000140 const auto *TII = SubTarget.getInstrInfo();
141 assert(TII && "Expected target instruction info");
142
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000143 unsigned I = 0, E = MI.getNumOperands();
144 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
145 !MI.getOperand(I).isImplicit();
146 ++I) {
147 if (I)
148 OS << ", ";
149 print(MI.getOperand(I), TRI);
150 }
151
152 if (I)
153 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000154 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000155 // TODO: Print the instruction flags, machine mem operands.
156 if (I < E)
157 OS << ' ';
158
159 bool NeedComma = false;
160 for (; I < E; ++I) {
161 if (NeedComma)
162 OS << ", ";
163 print(MI.getOperand(I), TRI);
164 NeedComma = true;
165 }
166}
167
168static void printReg(unsigned Reg, raw_ostream &OS,
169 const TargetRegisterInfo *TRI) {
170 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000171 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000172 if (!Reg)
173 OS << '_';
174 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000175 OS << '%' << StringRef(TRI->getName(Reg)).lower();
176 else
177 llvm_unreachable("Can't print this kind of register yet");
178}
179
180void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
181 switch (Op.getType()) {
182 case MachineOperand::MO_Register:
183 // TODO: Print register flags.
184 printReg(Op.getReg(), OS, TRI);
185 // TODO: Print sub register.
186 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000187 case MachineOperand::MO_Immediate:
188 OS << Op.getImm();
189 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000190 case MachineOperand::MO_MachineBasicBlock:
191 OS << "%bb." << Op.getMBB()->getNumber();
192 if (const auto *BB = Op.getMBB()->getBasicBlock()) {
193 if (BB->hasName())
194 OS << '.' << BB->getName();
195 }
196 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000197 case MachineOperand::MO_GlobalAddress:
198 // FIXME: Make this faster - print as operand will create a slot tracker to
199 // print unnamed values for the whole module every time it's called, which
200 // is inefficient.
201 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, &M);
202 // TODO: Print offset and target flags.
203 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000204 default:
205 // TODO: Print the other machine operands.
206 llvm_unreachable("Can't print this machine operand at the moment");
207 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000208}
209
Alex Lorenz345c1442015-06-15 23:52:35 +0000210void llvm::printMIR(raw_ostream &OS, const Module &M) {
211 yaml::Output Out(OS);
212 Out << const_cast<Module &>(M);
213}
214
215void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
216 MIRPrinter Printer(OS);
217 Printer.print(MF);
218}