blob: 5d13890e1ab95f64975b8ed026765ecff54c8d34 [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 Lorenz4f093bf2015-06-19 17:43:07 +000084 for (const auto &MBB : MF) {
85 yaml::MachineBasicBlock YamlMBB;
86 convert(YamlMBB, MBB);
87 YamlMF.BasicBlocks.push_back(YamlMBB);
88 }
Alex Lorenz345c1442015-06-15 23:52:35 +000089 yaml::Output Out(OS);
90 Out << YamlMF;
91}
92
Alex Lorenz54565cf2015-06-24 19:56:10 +000093void MIRPrinter::convert(yaml::MachineFunction &MF,
94 const MachineRegisterInfo &RegInfo) {
95 MF.IsSSA = RegInfo.isSSA();
96 MF.TracksRegLiveness = RegInfo.tracksLiveness();
97 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
98}
99
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000100void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB,
101 const MachineBasicBlock &MBB) {
102 // TODO: Serialize unnamed BB references.
103 if (const auto *BB = MBB.getBasicBlock())
104 YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
105 else
106 YamlMBB.Name = "";
107 YamlMBB.Alignment = MBB.getAlignment();
108 YamlMBB.AddressTaken = MBB.hasAddressTaken();
109 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000110
111 // Print the machine instructions.
112 YamlMBB.Instructions.reserve(MBB.size());
113 std::string Str;
114 for (const auto &MI : MBB) {
115 raw_string_ostream StrOS(Str);
116 MIPrinter(StrOS).print(MI);
117 YamlMBB.Instructions.push_back(StrOS.str());
118 Str.clear();
119 }
120}
121
122void MIPrinter::print(const MachineInstr &MI) {
123 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000124 const auto *TRI = SubTarget.getRegisterInfo();
125 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126 const auto *TII = SubTarget.getInstrInfo();
127 assert(TII && "Expected target instruction info");
128
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000129 unsigned I = 0, E = MI.getNumOperands();
130 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
131 !MI.getOperand(I).isImplicit();
132 ++I) {
133 if (I)
134 OS << ", ";
135 print(MI.getOperand(I), TRI);
136 }
137
138 if (I)
139 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000140 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000141 // TODO: Print the instruction flags, machine mem operands.
142 if (I < E)
143 OS << ' ';
144
145 bool NeedComma = false;
146 for (; I < E; ++I) {
147 if (NeedComma)
148 OS << ", ";
149 print(MI.getOperand(I), TRI);
150 NeedComma = true;
151 }
152}
153
154static void printReg(unsigned Reg, raw_ostream &OS,
155 const TargetRegisterInfo *TRI) {
156 // TODO: Print Stack Slots.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000157 // TODO: Print virtual registers.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000158 if (!Reg)
159 OS << '_';
160 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000161 OS << '%' << StringRef(TRI->getName(Reg)).lower();
162 else
163 llvm_unreachable("Can't print this kind of register yet");
164}
165
166void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
167 switch (Op.getType()) {
168 case MachineOperand::MO_Register:
169 // TODO: Print register flags.
170 printReg(Op.getReg(), OS, TRI);
171 // TODO: Print sub register.
172 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000173 case MachineOperand::MO_Immediate:
174 OS << Op.getImm();
175 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000176 default:
177 // TODO: Print the other machine operands.
178 llvm_unreachable("Can't print this machine operand at the moment");
179 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000180}
181
Alex Lorenz345c1442015-06-15 23:52:35 +0000182void llvm::printMIR(raw_ostream &OS, const Module &M) {
183 yaml::Output Out(OS);
184 Out << const_cast<Module &>(M);
185}
186
187void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
188 MIRPrinter Printer(OS);
189 Printer.print(MF);
190}