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