Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 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" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 20 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 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" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 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); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 41 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame^] | 42 | void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 43 | void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB); |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 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); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 55 | void print(const MachineOperand &Op, const TargetRegisterInfo *TRI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 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(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 80 | YamlMF.Alignment = MF.getAlignment(); |
| 81 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 82 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame^] | 83 | convert(YamlMF, MF.getRegInfo()); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 84 | for (const auto &MBB : MF) { |
| 85 | yaml::MachineBasicBlock YamlMBB; |
| 86 | convert(YamlMBB, MBB); |
| 87 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 88 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 89 | yaml::Output Out(OS); |
| 90 | Out << YamlMF; |
| 91 | } |
| 92 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame^] | 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 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 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(); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 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(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 124 | const auto *TRI = SubTarget.getRegisterInfo(); |
| 125 | assert(TRI && "Expected target register info"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 126 | const auto *TII = SubTarget.getInstrInfo(); |
| 127 | assert(TII && "Expected target instruction info"); |
| 128 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 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 << " = "; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 140 | OS << TII->getName(MI.getOpcode()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 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. |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 157 | // TODO: Print virtual registers. |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 158 | if (!Reg) |
| 159 | OS << '_'; |
| 160 | else if (Reg < TRI->getNumRegs()) |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 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; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 173 | case MachineOperand::MO_Immediate: |
| 174 | OS << Op.getImm(); |
| 175 | break; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 176 | default: |
| 177 | // TODO: Print the other machine operands. |
| 178 | llvm_unreachable("Can't print this machine operand at the moment"); |
| 179 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 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 | } |