MIR Serialization: Serialize simple MachineRegisterInfo attributes.
[oota-llvm.git] / lib / CodeGen / MIRPrinter.cpp
1 //===- 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"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/MIRYamlMapping.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/YAMLTraits.h"
25 #include "llvm/Target/TargetInstrInfo.h"
26 #include "llvm/Target/TargetSubtargetInfo.h"
27
28 using namespace llvm;
29
30 namespace {
31
32 /// This class prints out the machine functions using the MIR serialization
33 /// format.
34 class MIRPrinter {
35   raw_ostream &OS;
36
37 public:
38   MIRPrinter(raw_ostream &OS) : OS(OS) {}
39
40   void print(const MachineFunction &MF);
41
42   void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo);
43   void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
44 };
45
46 /// This class prints out the machine instructions using the MIR serialization
47 /// format.
48 class MIPrinter {
49   raw_ostream &OS;
50
51 public:
52   MIPrinter(raw_ostream &OS) : OS(OS) {}
53
54   void print(const MachineInstr &MI);
55   void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
56 };
57
58 } // end anonymous namespace
59
60 namespace llvm {
61 namespace yaml {
62
63 /// This struct serializes the LLVM IR module.
64 template <> 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
77 void MIRPrinter::print(const MachineFunction &MF) {
78   yaml::MachineFunction YamlMF;
79   YamlMF.Name = MF.getName();
80   YamlMF.Alignment = MF.getAlignment();
81   YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
82   YamlMF.HasInlineAsm = MF.hasInlineAsm();
83   convert(YamlMF, MF.getRegInfo());
84   for (const auto &MBB : MF) {
85     yaml::MachineBasicBlock YamlMBB;
86     convert(YamlMBB, MBB);
87     YamlMF.BasicBlocks.push_back(YamlMBB);
88   }
89   yaml::Output Out(OS);
90   Out << YamlMF;
91 }
92
93 void 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
100 void 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();
110
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
122 void MIPrinter::print(const MachineInstr &MI) {
123   const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
124   const auto *TRI = SubTarget.getRegisterInfo();
125   assert(TRI && "Expected target register info");
126   const auto *TII = SubTarget.getInstrInfo();
127   assert(TII && "Expected target instruction info");
128
129   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 << " = ";
140   OS << TII->getName(MI.getOpcode());
141   // 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
154 static void printReg(unsigned Reg, raw_ostream &OS,
155                      const TargetRegisterInfo *TRI) {
156   // TODO: Print Stack Slots.
157   // TODO: Print virtual registers.
158   if (!Reg)
159     OS << '_';
160   else if (Reg < TRI->getNumRegs())
161     OS << '%' << StringRef(TRI->getName(Reg)).lower();
162   else
163     llvm_unreachable("Can't print this kind of register yet");
164 }
165
166 void 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;
173   case MachineOperand::MO_Immediate:
174     OS << Op.getImm();
175     break;
176   default:
177     // TODO: Print the other machine operands.
178     llvm_unreachable("Can't print this machine operand at the moment");
179   }
180 }
181
182 void llvm::printMIR(raw_ostream &OS, const Module &M) {
183   yaml::Output Out(OS);
184   Out << const_cast<Module &>(M);
185 }
186
187 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
188   MIRPrinter Printer(OS);
189   Printer.print(MF);
190 }