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 | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 21 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 24 | #include "llvm/IR/ModuleSlotTracker.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Support/YAMLTraits.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetInstrInfo.h" |
| 29 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | /// This class prints out the machine functions using the MIR serialization |
| 36 | /// format. |
| 37 | class MIRPrinter { |
| 38 | raw_ostream &OS; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 39 | DenseMap<const uint32_t *, unsigned> RegisterMaskIds; |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
| 42 | MIRPrinter(raw_ostream &OS) : OS(OS) {} |
| 43 | |
| 44 | void print(const MachineFunction &MF); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 45 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 46 | void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo, |
| 47 | const TargetRegisterInfo *TRI); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 48 | void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 49 | void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI, |
| 50 | const MachineJumpTableInfo &JTI); |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 51 | void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 52 | const MachineBasicBlock &MBB); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 53 | void convertStackObjects(yaml::MachineFunction &MF, |
| 54 | const MachineFrameInfo &MFI); |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | void initRegisterMaskIds(const MachineFunction &MF); |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 60 | /// This class prints out the machine instructions using the MIR serialization |
| 61 | /// format. |
| 62 | class MIPrinter { |
| 63 | raw_ostream &OS; |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 64 | ModuleSlotTracker &MST; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 65 | const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 66 | |
| 67 | public: |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 68 | MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST, |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 69 | const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds) |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 70 | : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {} |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 71 | |
| 72 | void print(const MachineInstr &MI); |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 73 | void printMBBReference(const MachineBasicBlock &MBB); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 74 | void print(const MachineOperand &Op, const TargetRegisterInfo *TRI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 77 | } // end anonymous namespace |
| 78 | |
| 79 | namespace llvm { |
| 80 | namespace yaml { |
| 81 | |
| 82 | /// This struct serializes the LLVM IR module. |
| 83 | template <> struct BlockScalarTraits<Module> { |
| 84 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 85 | Mod.print(OS, nullptr); |
| 86 | } |
| 87 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 88 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 89 | return ""; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | } // end namespace yaml |
| 94 | } // end namespace llvm |
| 95 | |
Alex Lorenz | 15a00a8 | 2015-07-14 21:18:25 +0000 | [diff] [blame] | 96 | static void printReg(unsigned Reg, raw_ostream &OS, |
| 97 | const TargetRegisterInfo *TRI) { |
| 98 | // TODO: Print Stack Slots. |
| 99 | if (!Reg) |
| 100 | OS << '_'; |
| 101 | else if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 102 | OS << '%' << TargetRegisterInfo::virtReg2Index(Reg); |
| 103 | else if (Reg < TRI->getNumRegs()) |
| 104 | OS << '%' << StringRef(TRI->getName(Reg)).lower(); |
| 105 | else |
| 106 | llvm_unreachable("Can't print this kind of register yet"); |
| 107 | } |
| 108 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 109 | void MIRPrinter::print(const MachineFunction &MF) { |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 110 | initRegisterMaskIds(MF); |
| 111 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 112 | yaml::MachineFunction YamlMF; |
| 113 | YamlMF.Name = MF.getName(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 114 | YamlMF.Alignment = MF.getAlignment(); |
| 115 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 116 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 117 | convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo()); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 118 | convert(YamlMF.FrameInfo, *MF.getFrameInfo()); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 119 | convertStackObjects(YamlMF, *MF.getFrameInfo()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 120 | |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 121 | ModuleSlotTracker MST(MF.getFunction()->getParent()); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 122 | if (const auto *JumpTableInfo = MF.getJumpTableInfo()) |
| 123 | convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo); |
| 124 | int I = 0; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 125 | for (const auto &MBB : MF) { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 126 | // TODO: Allow printing of non sequentially numbered MBBs. |
| 127 | // This is currently needed as the basic block references get their index |
| 128 | // from MBB.getNumber(), thus it should be sequential so that the parser can |
| 129 | // map back to the correct MBBs when parsing the output. |
| 130 | assert(MBB.getNumber() == I++ && |
| 131 | "Can't print MBBs that aren't sequentially numbered"); |
Alex Lorenz | ec6b26b | 2015-06-26 17:07:27 +0000 | [diff] [blame] | 132 | (void)I; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 133 | yaml::MachineBasicBlock YamlMBB; |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 134 | convert(MST, YamlMBB, MBB); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 135 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 136 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 137 | yaml::Output Out(OS); |
| 138 | Out << YamlMF; |
| 139 | } |
| 140 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 141 | void MIRPrinter::convert(yaml::MachineFunction &MF, |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 142 | const MachineRegisterInfo &RegInfo, |
| 143 | const TargetRegisterInfo *TRI) { |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 144 | MF.IsSSA = RegInfo.isSSA(); |
| 145 | MF.TracksRegLiveness = RegInfo.tracksLiveness(); |
| 146 | MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled(); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 147 | |
| 148 | // Print the virtual register definitions. |
| 149 | for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) { |
| 150 | unsigned Reg = TargetRegisterInfo::index2VirtReg(I); |
| 151 | yaml::VirtualRegisterDefinition VReg; |
| 152 | VReg.ID = I; |
| 153 | VReg.Class = |
| 154 | StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower(); |
| 155 | MF.VirtualRegisters.push_back(VReg); |
| 156 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 159 | void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI, |
| 160 | const MachineFrameInfo &MFI) { |
| 161 | YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken(); |
| 162 | YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken(); |
| 163 | YamlMFI.HasStackMap = MFI.hasStackMap(); |
| 164 | YamlMFI.HasPatchPoint = MFI.hasPatchPoint(); |
| 165 | YamlMFI.StackSize = MFI.getStackSize(); |
| 166 | YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment(); |
| 167 | YamlMFI.MaxAlignment = MFI.getMaxAlignment(); |
| 168 | YamlMFI.AdjustsStack = MFI.adjustsStack(); |
| 169 | YamlMFI.HasCalls = MFI.hasCalls(); |
| 170 | YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize(); |
| 171 | YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment(); |
| 172 | YamlMFI.HasVAStart = MFI.hasVAStart(); |
| 173 | YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc(); |
| 174 | } |
| 175 | |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 176 | void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF, |
| 177 | const MachineFrameInfo &MFI) { |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 178 | // Process fixed stack objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 179 | unsigned ID = 0; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 180 | for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) { |
| 181 | if (MFI.isDeadObjectIndex(I)) |
| 182 | continue; |
| 183 | |
| 184 | yaml::FixedMachineStackObject YamlObject; |
| 185 | YamlObject.ID = ID++; |
| 186 | YamlObject.Type = MFI.isSpillSlotObjectIndex(I) |
| 187 | ? yaml::FixedMachineStackObject::SpillSlot |
| 188 | : yaml::FixedMachineStackObject::DefaultType; |
| 189 | YamlObject.Offset = MFI.getObjectOffset(I); |
| 190 | YamlObject.Size = MFI.getObjectSize(I); |
| 191 | YamlObject.Alignment = MFI.getObjectAlignment(I); |
| 192 | YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I); |
| 193 | YamlObject.IsAliased = MFI.isAliasedObjectIndex(I); |
| 194 | MF.FixedStackObjects.push_back(YamlObject); |
| 195 | // TODO: Store the mapping between fixed object IDs and object indices to |
| 196 | // print the fixed stack object references correctly. |
| 197 | } |
| 198 | |
| 199 | // Process ordinary stack objects. |
| 200 | ID = 0; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 201 | for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) { |
| 202 | if (MFI.isDeadObjectIndex(I)) |
| 203 | continue; |
| 204 | |
| 205 | yaml::MachineStackObject YamlObject; |
| 206 | YamlObject.ID = ID++; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 207 | if (const auto *Alloca = MFI.getObjectAllocation(I)) |
| 208 | YamlObject.Name.Value = |
| 209 | Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>"; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 210 | YamlObject.Type = MFI.isSpillSlotObjectIndex(I) |
| 211 | ? yaml::MachineStackObject::SpillSlot |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 212 | : MFI.isVariableSizedObjectIndex(I) |
| 213 | ? yaml::MachineStackObject::VariableSized |
| 214 | : yaml::MachineStackObject::DefaultType; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 215 | YamlObject.Offset = MFI.getObjectOffset(I); |
| 216 | YamlObject.Size = MFI.getObjectSize(I); |
| 217 | YamlObject.Alignment = MFI.getObjectAlignment(I); |
| 218 | |
| 219 | MF.StackObjects.push_back(YamlObject); |
| 220 | // TODO: Store the mapping between object IDs and object indices to print |
| 221 | // the stack object references correctly. |
| 222 | } |
| 223 | } |
| 224 | |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 225 | void MIRPrinter::convert(ModuleSlotTracker &MST, |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame^] | 226 | yaml::MachineJumpTable &YamlJTI, |
| 227 | const MachineJumpTableInfo &JTI) { |
| 228 | YamlJTI.Kind = JTI.getEntryKind(); |
| 229 | unsigned ID = 0; |
| 230 | for (const auto &Table : JTI.getJumpTables()) { |
| 231 | std::string Str; |
| 232 | yaml::MachineJumpTable::Entry Entry; |
| 233 | Entry.ID = ID++; |
| 234 | for (const auto *MBB : Table.MBBs) { |
| 235 | raw_string_ostream StrOS(Str); |
| 236 | MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*MBB); |
| 237 | Entry.Blocks.push_back(StrOS.str()); |
| 238 | Str.clear(); |
| 239 | } |
| 240 | YamlJTI.Entries.push_back(Entry); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void MIRPrinter::convert(ModuleSlotTracker &MST, |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 245 | yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 246 | const MachineBasicBlock &MBB) { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 247 | assert(MBB.getNumber() >= 0 && "Invalid MBB number"); |
| 248 | YamlMBB.ID = (unsigned)MBB.getNumber(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 249 | // TODO: Serialize unnamed BB references. |
| 250 | if (const auto *BB = MBB.getBasicBlock()) |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 251 | YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>"; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 252 | else |
Alex Lorenz | b1f9ce8 | 2015-07-08 20:22:20 +0000 | [diff] [blame] | 253 | YamlMBB.Name.Value = ""; |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 254 | YamlMBB.Alignment = MBB.getAlignment(); |
| 255 | YamlMBB.AddressTaken = MBB.hasAddressTaken(); |
| 256 | YamlMBB.IsLandingPad = MBB.isLandingPad(); |
Alex Lorenz | eb5112b | 2015-06-30 18:32:02 +0000 | [diff] [blame] | 257 | for (const auto *SuccMBB : MBB.successors()) { |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 258 | std::string Str; |
| 259 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 260 | MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB); |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 261 | YamlMBB.Successors.push_back(StrOS.str()); |
| 262 | } |
Alex Lorenz | 9fab370 | 2015-07-14 21:24:41 +0000 | [diff] [blame] | 263 | // Print the live in registers. |
| 264 | const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo(); |
| 265 | assert(TRI && "Expected target register info"); |
| 266 | for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) { |
| 267 | std::string Str; |
| 268 | raw_string_ostream StrOS(Str); |
| 269 | printReg(*I, StrOS, TRI); |
| 270 | YamlMBB.LiveIns.push_back(StrOS.str()); |
| 271 | } |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 272 | // Print the machine instructions. |
| 273 | YamlMBB.Instructions.reserve(MBB.size()); |
| 274 | std::string Str; |
| 275 | for (const auto &MI : MBB) { |
| 276 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 277 | MIPrinter(StrOS, MST, RegisterMaskIds).print(MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 278 | YamlMBB.Instructions.push_back(StrOS.str()); |
| 279 | Str.clear(); |
| 280 | } |
| 281 | } |
| 282 | |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 283 | void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) { |
| 284 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); |
| 285 | unsigned I = 0; |
| 286 | for (const uint32_t *Mask : TRI->getRegMasks()) |
| 287 | RegisterMaskIds.insert(std::make_pair(Mask, I++)); |
| 288 | } |
| 289 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 290 | void MIPrinter::print(const MachineInstr &MI) { |
| 291 | const auto &SubTarget = MI.getParent()->getParent()->getSubtarget(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 292 | const auto *TRI = SubTarget.getRegisterInfo(); |
| 293 | assert(TRI && "Expected target register info"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 294 | const auto *TII = SubTarget.getInstrInfo(); |
| 295 | assert(TII && "Expected target instruction info"); |
| 296 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 297 | unsigned I = 0, E = MI.getNumOperands(); |
| 298 | for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() && |
| 299 | !MI.getOperand(I).isImplicit(); |
| 300 | ++I) { |
| 301 | if (I) |
| 302 | OS << ", "; |
| 303 | print(MI.getOperand(I), TRI); |
| 304 | } |
| 305 | |
| 306 | if (I) |
| 307 | OS << " = "; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 308 | OS << TII->getName(MI.getOpcode()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 309 | // TODO: Print the instruction flags, machine mem operands. |
| 310 | if (I < E) |
| 311 | OS << ' '; |
| 312 | |
| 313 | bool NeedComma = false; |
| 314 | for (; I < E; ++I) { |
| 315 | if (NeedComma) |
| 316 | OS << ", "; |
| 317 | print(MI.getOperand(I), TRI); |
| 318 | NeedComma = true; |
| 319 | } |
| 320 | } |
| 321 | |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 322 | void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) { |
| 323 | OS << "%bb." << MBB.getNumber(); |
| 324 | if (const auto *BB = MBB.getBasicBlock()) { |
| 325 | if (BB->hasName()) |
| 326 | OS << '.' << BB->getName(); |
| 327 | } |
| 328 | } |
| 329 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 330 | void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) { |
| 331 | switch (Op.getType()) { |
| 332 | case MachineOperand::MO_Register: |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 333 | // TODO: Print the other register flags. |
| 334 | if (Op.isImplicit()) |
| 335 | OS << (Op.isDef() ? "implicit-def " : "implicit "); |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 336 | if (Op.isDead()) |
| 337 | OS << "dead "; |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 338 | if (Op.isKill()) |
| 339 | OS << "killed "; |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 340 | if (Op.isUndef()) |
| 341 | OS << "undef "; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 342 | printReg(Op.getReg(), OS, TRI); |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 343 | // Print the sub register. |
| 344 | if (Op.getSubReg() != 0) |
| 345 | OS << ':' << TRI->getSubRegIndexName(Op.getSubReg()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 346 | break; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 347 | case MachineOperand::MO_Immediate: |
| 348 | OS << Op.getImm(); |
| 349 | break; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 350 | case MachineOperand::MO_MachineBasicBlock: |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 351 | printMBBReference(*Op.getMBB()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 352 | break; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 353 | case MachineOperand::MO_GlobalAddress: |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 354 | Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 355 | // TODO: Print offset and target flags. |
| 356 | break; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 357 | case MachineOperand::MO_RegisterMask: { |
| 358 | auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask()); |
| 359 | if (RegMaskInfo != RegisterMaskIds.end()) |
| 360 | OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower(); |
| 361 | else |
| 362 | llvm_unreachable("Can't print this machine register mask yet."); |
| 363 | break; |
| 364 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 365 | default: |
| 366 | // TODO: Print the other machine operands. |
| 367 | llvm_unreachable("Can't print this machine operand at the moment"); |
| 368 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 371 | void llvm::printMIR(raw_ostream &OS, const Module &M) { |
| 372 | yaml::Output Out(OS); |
| 373 | Out << const_cast<Module &>(M); |
| 374 | } |
| 375 | |
| 376 | void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { |
| 377 | MIRPrinter Printer(OS); |
| 378 | Printer.print(MF); |
| 379 | } |