f9595f88b0b57149eee52f4a8669fff2f5182379
[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
85   int I = 0;
86   for (const auto &MBB : MF) {
87     // 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");
93     yaml::MachineBasicBlock YamlMBB;
94     convert(YamlMBB, MBB);
95     YamlMF.BasicBlocks.push_back(YamlMBB);
96   }
97   yaml::Output Out(OS);
98   Out << YamlMF;
99 }
100
101 void MIRPrinter::convert(yaml::MachineFunction &MF,
102                          const MachineRegisterInfo &RegInfo) {
103   MF.IsSSA = RegInfo.isSSA();
104   MF.TracksRegLiveness = RegInfo.tracksLiveness();
105   MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
106 }
107
108 void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB,
109                          const MachineBasicBlock &MBB) {
110   assert(MBB.getNumber() >= 0 && "Invalid MBB number");
111   YamlMBB.ID = (unsigned)MBB.getNumber();
112   // TODO: Serialize unnamed BB references.
113   if (const auto *BB = MBB.getBasicBlock())
114     YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
115   else
116     YamlMBB.Name = "";
117   YamlMBB.Alignment = MBB.getAlignment();
118   YamlMBB.AddressTaken = MBB.hasAddressTaken();
119   YamlMBB.IsLandingPad = MBB.isLandingPad();
120
121   // Print the machine instructions.
122   YamlMBB.Instructions.reserve(MBB.size());
123   std::string Str;
124   for (const auto &MI : MBB) {
125     raw_string_ostream StrOS(Str);
126     MIPrinter(StrOS).print(MI);
127     YamlMBB.Instructions.push_back(StrOS.str());
128     Str.clear();
129   }
130 }
131
132 void MIPrinter::print(const MachineInstr &MI) {
133   const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
134   const auto *TRI = SubTarget.getRegisterInfo();
135   assert(TRI && "Expected target register info");
136   const auto *TII = SubTarget.getInstrInfo();
137   assert(TII && "Expected target instruction info");
138
139   unsigned I = 0, E = MI.getNumOperands();
140   for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
141          !MI.getOperand(I).isImplicit();
142        ++I) {
143     if (I)
144       OS << ", ";
145     print(MI.getOperand(I), TRI);
146   }
147
148   if (I)
149     OS << " = ";
150   OS << TII->getName(MI.getOpcode());
151   // TODO: Print the instruction flags, machine mem operands.
152   if (I < E)
153     OS << ' ';
154
155   bool NeedComma = false;
156   for (; I < E; ++I) {
157     if (NeedComma)
158       OS << ", ";
159     print(MI.getOperand(I), TRI);
160     NeedComma = true;
161   }
162 }
163
164 static void printReg(unsigned Reg, raw_ostream &OS,
165                      const TargetRegisterInfo *TRI) {
166   // TODO: Print Stack Slots.
167   // TODO: Print virtual registers.
168   if (!Reg)
169     OS << '_';
170   else if (Reg < TRI->getNumRegs())
171     OS << '%' << StringRef(TRI->getName(Reg)).lower();
172   else
173     llvm_unreachable("Can't print this kind of register yet");
174 }
175
176 void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
177   switch (Op.getType()) {
178   case MachineOperand::MO_Register:
179     // TODO: Print register flags.
180     printReg(Op.getReg(), OS, TRI);
181     // TODO: Print sub register.
182     break;
183   case MachineOperand::MO_Immediate:
184     OS << Op.getImm();
185     break;
186   case MachineOperand::MO_MachineBasicBlock:
187     OS << "%bb." << Op.getMBB()->getNumber();
188     if (const auto *BB = Op.getMBB()->getBasicBlock()) {
189       if (BB->hasName())
190         OS << '.' << BB->getName();
191     }
192     break;
193   default:
194     // TODO: Print the other machine operands.
195     llvm_unreachable("Can't print this machine operand at the moment");
196   }
197 }
198
199 void llvm::printMIR(raw_ostream &OS, const Module &M) {
200   yaml::Output Out(OS);
201   Out << const_cast<Module &>(M);
202 }
203
204 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
205   MIRPrinter Printer(OS);
206   Printer.print(MF);
207 }