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