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