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 | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 43 | void convert(const Module &M, yaml::MachineBasicBlock &YamlMBB, |
| 44 | const MachineBasicBlock &MBB); |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 47 | /// This class prints out the machine instructions using the MIR serialization |
| 48 | /// format. |
| 49 | class MIPrinter { |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 50 | const Module &M; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 51 | raw_ostream &OS; |
| 52 | |
| 53 | public: |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 54 | MIPrinter(const Module &M, raw_ostream &OS) : M(M), OS(OS) {} |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 55 | |
| 56 | void print(const MachineInstr &MI); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 57 | void print(const MachineOperand &Op, const TargetRegisterInfo *TRI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 60 | } // end anonymous namespace |
| 61 | |
| 62 | namespace llvm { |
| 63 | namespace yaml { |
| 64 | |
| 65 | /// This struct serializes the LLVM IR module. |
| 66 | template <> struct BlockScalarTraits<Module> { |
| 67 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 68 | Mod.print(OS, nullptr); |
| 69 | } |
| 70 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 71 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 72 | return ""; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | } // end namespace yaml |
| 77 | } // end namespace llvm |
| 78 | |
| 79 | void MIRPrinter::print(const MachineFunction &MF) { |
| 80 | yaml::MachineFunction YamlMF; |
| 81 | YamlMF.Name = MF.getName(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 82 | YamlMF.Alignment = MF.getAlignment(); |
| 83 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 84 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 85 | convert(YamlMF, MF.getRegInfo()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 86 | |
| 87 | int I = 0; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 88 | const auto &M = *MF.getFunction()->getParent(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 89 | for (const auto &MBB : MF) { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 90 | // TODO: Allow printing of non sequentially numbered MBBs. |
| 91 | // This is currently needed as the basic block references get their index |
| 92 | // from MBB.getNumber(), thus it should be sequential so that the parser can |
| 93 | // map back to the correct MBBs when parsing the output. |
| 94 | assert(MBB.getNumber() == I++ && |
| 95 | "Can't print MBBs that aren't sequentially numbered"); |
Alex Lorenz | ec6b26b | 2015-06-26 17:07:27 +0000 | [diff] [blame] | 96 | (void)I; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 97 | yaml::MachineBasicBlock YamlMBB; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 98 | convert(M, YamlMBB, MBB); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 99 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 100 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 101 | yaml::Output Out(OS); |
| 102 | Out << YamlMF; |
| 103 | } |
| 104 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 105 | void MIRPrinter::convert(yaml::MachineFunction &MF, |
| 106 | const MachineRegisterInfo &RegInfo) { |
| 107 | MF.IsSSA = RegInfo.isSSA(); |
| 108 | MF.TracksRegLiveness = RegInfo.tracksLiveness(); |
| 109 | MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled(); |
| 110 | } |
| 111 | |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 112 | void MIRPrinter::convert(const Module &M, yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 113 | const MachineBasicBlock &MBB) { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 114 | assert(MBB.getNumber() >= 0 && "Invalid MBB number"); |
| 115 | YamlMBB.ID = (unsigned)MBB.getNumber(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 116 | // TODO: Serialize unnamed BB references. |
| 117 | if (const auto *BB = MBB.getBasicBlock()) |
| 118 | YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>"; |
| 119 | else |
| 120 | YamlMBB.Name = ""; |
| 121 | YamlMBB.Alignment = MBB.getAlignment(); |
| 122 | YamlMBB.AddressTaken = MBB.hasAddressTaken(); |
| 123 | YamlMBB.IsLandingPad = MBB.isLandingPad(); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 124 | |
| 125 | // Print the machine instructions. |
| 126 | YamlMBB.Instructions.reserve(MBB.size()); |
| 127 | std::string Str; |
| 128 | for (const auto &MI : MBB) { |
| 129 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 130 | MIPrinter(M, StrOS).print(MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 131 | YamlMBB.Instructions.push_back(StrOS.str()); |
| 132 | Str.clear(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void MIPrinter::print(const MachineInstr &MI) { |
| 137 | const auto &SubTarget = MI.getParent()->getParent()->getSubtarget(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 138 | const auto *TRI = SubTarget.getRegisterInfo(); |
| 139 | assert(TRI && "Expected target register info"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 140 | const auto *TII = SubTarget.getInstrInfo(); |
| 141 | assert(TII && "Expected target instruction info"); |
| 142 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 143 | unsigned I = 0, E = MI.getNumOperands(); |
| 144 | for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() && |
| 145 | !MI.getOperand(I).isImplicit(); |
| 146 | ++I) { |
| 147 | if (I) |
| 148 | OS << ", "; |
| 149 | print(MI.getOperand(I), TRI); |
| 150 | } |
| 151 | |
| 152 | if (I) |
| 153 | OS << " = "; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 154 | OS << TII->getName(MI.getOpcode()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 155 | // TODO: Print the instruction flags, machine mem operands. |
| 156 | if (I < E) |
| 157 | OS << ' '; |
| 158 | |
| 159 | bool NeedComma = false; |
| 160 | for (; I < E; ++I) { |
| 161 | if (NeedComma) |
| 162 | OS << ", "; |
| 163 | print(MI.getOperand(I), TRI); |
| 164 | NeedComma = true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void printReg(unsigned Reg, raw_ostream &OS, |
| 169 | const TargetRegisterInfo *TRI) { |
| 170 | // TODO: Print Stack Slots. |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 171 | // TODO: Print virtual registers. |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 172 | if (!Reg) |
| 173 | OS << '_'; |
| 174 | else if (Reg < TRI->getNumRegs()) |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 175 | OS << '%' << StringRef(TRI->getName(Reg)).lower(); |
| 176 | else |
| 177 | llvm_unreachable("Can't print this kind of register yet"); |
| 178 | } |
| 179 | |
| 180 | void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) { |
| 181 | switch (Op.getType()) { |
| 182 | case MachineOperand::MO_Register: |
| 183 | // TODO: Print register flags. |
| 184 | printReg(Op.getReg(), OS, TRI); |
| 185 | // TODO: Print sub register. |
| 186 | break; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 187 | case MachineOperand::MO_Immediate: |
| 188 | OS << Op.getImm(); |
| 189 | break; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 190 | case MachineOperand::MO_MachineBasicBlock: |
| 191 | OS << "%bb." << Op.getMBB()->getNumber(); |
| 192 | if (const auto *BB = Op.getMBB()->getBasicBlock()) { |
| 193 | if (BB->hasName()) |
| 194 | OS << '.' << BB->getName(); |
| 195 | } |
| 196 | break; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 197 | case MachineOperand::MO_GlobalAddress: |
| 198 | // FIXME: Make this faster - print as operand will create a slot tracker to |
| 199 | // print unnamed values for the whole module every time it's called, which |
| 200 | // is inefficient. |
| 201 | Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, &M); |
| 202 | // TODO: Print offset and target flags. |
| 203 | break; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 204 | default: |
| 205 | // TODO: Print the other machine operands. |
| 206 | llvm_unreachable("Can't print this machine operand at the moment"); |
| 207 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 210 | void llvm::printMIR(raw_ostream &OS, const Module &M) { |
| 211 | yaml::Output Out(OS); |
| 212 | Out << const_cast<Module &>(M); |
| 213 | } |
| 214 | |
| 215 | void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { |
| 216 | MIRPrinter Printer(OS); |
| 217 | Printer.print(MF); |
| 218 | } |