1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that prints out the LLVM IR and machine
11 // functions using the MIR serialization format.
13 //===----------------------------------------------------------------------===//
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"
32 /// This class prints out the machine functions using the MIR serialization
38 MIRPrinter(raw_ostream &OS) : OS(OS) {}
40 void print(const MachineFunction &MF);
42 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo);
43 void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
46 /// This class prints out the machine instructions using the MIR serialization
52 MIPrinter(raw_ostream &OS) : OS(OS) {}
54 void print(const MachineInstr &MI);
55 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
58 } // end anonymous namespace
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);
68 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
69 llvm_unreachable("LLVM Module is supposed to be parsed separately");
74 } // end namespace yaml
75 } // end namespace llvm
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);
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();
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>";
107 YamlMBB.Alignment = MBB.getAlignment();
108 YamlMBB.AddressTaken = MBB.hasAddressTaken();
109 YamlMBB.IsLandingPad = MBB.isLandingPad();
111 // Print the machine instructions.
112 YamlMBB.Instructions.reserve(MBB.size());
114 for (const auto &MI : MBB) {
115 raw_string_ostream StrOS(Str);
116 MIPrinter(StrOS).print(MI);
117 YamlMBB.Instructions.push_back(StrOS.str());
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");
129 unsigned I = 0, E = MI.getNumOperands();
130 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
131 !MI.getOperand(I).isImplicit();
135 print(MI.getOperand(I), TRI);
140 OS << TII->getName(MI.getOpcode());
141 // TODO: Print the instruction flags, machine mem operands.
145 bool NeedComma = false;
149 print(MI.getOperand(I), TRI);
154 static void printReg(unsigned Reg, raw_ostream &OS,
155 const TargetRegisterInfo *TRI) {
156 // TODO: Print Stack Slots.
157 // TODO: Print virtual registers.
160 else if (Reg < TRI->getNumRegs())
161 OS << '%' << StringRef(TRI->getName(Reg)).lower();
163 llvm_unreachable("Can't print this kind of register yet");
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.
173 case MachineOperand::MO_Immediate:
177 // TODO: Print the other machine operands.
178 llvm_unreachable("Can't print this machine operand at the moment");
182 void llvm::printMIR(raw_ostream &OS, const Module &M) {
183 yaml::Output Out(OS);
184 Out << const_cast<Module &>(M);
187 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
188 MIRPrinter Printer(OS);