blob: 9446849ffd2318ff6953a5b1688a01bb263eee95 [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 Lorenz4f093bf2015-06-19 17:43:07 +000043 void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
Alex Lorenz345c1442015-06-15 23:52:35 +000044};
45
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000046/// This class prints out the machine instructions using the MIR serialization
47/// format.
48class MIPrinter {
49 raw_ostream &OS;
50
51public:
52 MIPrinter(raw_ostream &OS) : OS(OS) {}
53
54 void print(const MachineInstr &MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000055 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000056};
57
Alex Lorenz345c1442015-06-15 23:52:35 +000058} // end anonymous namespace
59
60namespace llvm {
61namespace yaml {
62
63/// This struct serializes the LLVM IR module.
64template <> struct BlockScalarTraits<Module> {
65 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
66 Mod.print(OS, nullptr);
67 }
68 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
69 llvm_unreachable("LLVM Module is supposed to be parsed separately");
70 return "";
71 }
72};
73
74} // end namespace yaml
75} // end namespace llvm
76
77void MIRPrinter::print(const MachineFunction &MF) {
78 yaml::MachineFunction YamlMF;
79 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000080 YamlMF.Alignment = MF.getAlignment();
81 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
82 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz54565cf2015-06-24 19:56:10 +000083 convert(YamlMF, MF.getRegInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +000084
85 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +000086 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +000087 // TODO: Allow printing of non sequentially numbered MBBs.
88 // This is currently needed as the basic block references get their index
89 // from MBB.getNumber(), thus it should be sequential so that the parser can
90 // map back to the correct MBBs when parsing the output.
91 assert(MBB.getNumber() == I++ &&
92 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +000093 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +000094 yaml::MachineBasicBlock YamlMBB;
95 convert(YamlMBB, MBB);
96 YamlMF.BasicBlocks.push_back(YamlMBB);
97 }
Alex Lorenz345c1442015-06-15 23:52:35 +000098 yaml::Output Out(OS);
99 Out << YamlMF;
100}
101
Alex Lorenz54565cf2015-06-24 19:56:10 +0000102void MIRPrinter::convert(yaml::MachineFunction &MF,
103 const MachineRegisterInfo &RegInfo) {
104 MF.IsSSA = RegInfo.isSSA();
105 MF.TracksRegLiveness = RegInfo.tracksLiveness();
106 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
107}
108
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000109void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB,
110 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000111 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
112 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000113 // TODO: Serialize unnamed BB references.
114 if (const auto *BB = MBB.getBasicBlock())
115 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
116 else
117 YamlMBB.Name = "";
118 YamlMBB.Alignment = MBB.getAlignment();
119 YamlMBB.AddressTaken = MBB.hasAddressTaken();
120 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000121
122 // Print the machine instructions.
123 YamlMBB.Instructions.reserve(MBB.size());
124 std::string Str;
125 for (const auto &MI : MBB) {
126 raw_string_ostream StrOS(Str);
127 MIPrinter(StrOS).print(MI);
128 YamlMBB.Instructions.push_back(StrOS.str());
129 Str.clear();
130 }
131}
132
133void MIPrinter::print(const MachineInstr &MI) {
134 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000135 const auto *TRI = SubTarget.getRegisterInfo();
136 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000137 const auto *TII = SubTarget.getInstrInfo();
138 assert(TII && "Expected target instruction info");
139
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000140 unsigned I = 0, E = MI.getNumOperands();
141 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
142 !MI.getOperand(I).isImplicit();
143 ++I) {
144 if (I)
145 OS << ", ";
146 print(MI.getOperand(I), TRI);
147 }
148
149 if (I)
150 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000151 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000152 // TODO: Print the instruction flags, machine mem operands.
153 if (I < E)
154 OS << ' ';
155
156 bool NeedComma = false;
157 for (; I < E; ++I) {
158 if (NeedComma)
159 OS << ", ";
160 print(MI.getOperand(I), TRI);
161 NeedComma = true;
162 }
163}
164
165static void printReg(unsigned Reg, raw_ostream &OS,
166 const TargetRegisterInfo *TRI) {
167 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000168 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000169 if (!Reg)
170 OS << '_';
171 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000172 OS << '%' << StringRef(TRI->getName(Reg)).lower();
173 else
174 llvm_unreachable("Can't print this kind of register yet");
175}
176
177void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
178 switch (Op.getType()) {
179 case MachineOperand::MO_Register:
180 // TODO: Print register flags.
181 printReg(Op.getReg(), OS, TRI);
182 // TODO: Print sub register.
183 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000184 case MachineOperand::MO_Immediate:
185 OS << Op.getImm();
186 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000187 case MachineOperand::MO_MachineBasicBlock:
188 OS << "%bb." << Op.getMBB()->getNumber();
189 if (const auto *BB = Op.getMBB()->getBasicBlock()) {
190 if (BB->hasName())
191 OS << '.' << BB->getName();
192 }
193 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194 default:
195 // TODO: Print the other machine operands.
196 llvm_unreachable("Can't print this machine operand at the moment");
197 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000198}
199
Alex Lorenz345c1442015-06-15 23:52:35 +0000200void llvm::printMIR(raw_ostream &OS, const Module &M) {
201 yaml::Output Out(OS);
202 Out << const_cast<Module &>(M);
203}
204
205void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
206 MIRPrinter Printer(OS);
207 Printer.print(MF);
208}