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