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" |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineConstantPool.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 23 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame^] | 24 | #include "llvm/IR/Constants.h" |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 26 | #include "llvm/IR/IRPrintingPasses.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Module.h" |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 28 | #include "llvm/IR/ModuleSlotTracker.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/Support/YAMLTraits.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 39 | /// This structure describes how to print out stack object references. |
| 40 | struct FrameIndexOperand { |
| 41 | std::string Name; |
| 42 | unsigned ID; |
| 43 | bool IsFixed; |
| 44 | |
| 45 | FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed) |
| 46 | : Name(Name.str()), ID(ID), IsFixed(IsFixed) {} |
| 47 | |
| 48 | /// Return an ordinary stack object reference. |
| 49 | static FrameIndexOperand create(StringRef Name, unsigned ID) { |
| 50 | return FrameIndexOperand(Name, ID, /*IsFixed=*/false); |
| 51 | } |
| 52 | |
| 53 | /// Return a fixed stack object reference. |
| 54 | static FrameIndexOperand createFixed(unsigned ID) { |
| 55 | return FrameIndexOperand("", ID, /*IsFixed=*/true); |
| 56 | } |
| 57 | }; |
| 58 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 59 | /// This class prints out the machine functions using the MIR serialization |
| 60 | /// format. |
| 61 | class MIRPrinter { |
| 62 | raw_ostream &OS; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 63 | DenseMap<const uint32_t *, unsigned> RegisterMaskIds; |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 64 | /// Maps from stack object indices to operand indices which will be used when |
| 65 | /// printing frame index machine operands. |
| 66 | DenseMap<int, FrameIndexOperand> StackObjectOperandMapping; |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 67 | |
| 68 | public: |
| 69 | MIRPrinter(raw_ostream &OS) : OS(OS) {} |
| 70 | |
| 71 | void print(const MachineFunction &MF); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 72 | |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 73 | void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo, |
| 74 | const TargetRegisterInfo *TRI); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 75 | void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 76 | void convert(yaml::MachineFunction &MF, |
| 77 | const MachineConstantPool &ConstantPool); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 78 | void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI, |
| 79 | const MachineJumpTableInfo &JTI); |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 80 | void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 81 | const MachineBasicBlock &MBB); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 82 | void convertStackObjects(yaml::MachineFunction &MF, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 83 | const MachineFrameInfo &MFI, |
| 84 | const TargetRegisterInfo *TRI); |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | void initRegisterMaskIds(const MachineFunction &MF); |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 90 | /// This class prints out the machine instructions using the MIR serialization |
| 91 | /// format. |
| 92 | class MIPrinter { |
| 93 | raw_ostream &OS; |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 94 | ModuleSlotTracker &MST; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 95 | const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds; |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 96 | const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 97 | |
| 98 | public: |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 99 | MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST, |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 100 | const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds, |
| 101 | const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping) |
| 102 | : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds), |
| 103 | StackObjectOperandMapping(StackObjectOperandMapping) {} |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 104 | |
| 105 | void print(const MachineInstr &MI); |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 106 | void printMBBReference(const MachineBasicBlock &MBB); |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame^] | 107 | void printIRBlockReference(const BasicBlock &BB); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 108 | void printStackObjectReference(int FrameIndex); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 109 | void print(const MachineOperand &Op, const TargetRegisterInfo *TRI); |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 110 | |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 111 | void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 114 | } // end anonymous namespace |
| 115 | |
| 116 | namespace llvm { |
| 117 | namespace yaml { |
| 118 | |
| 119 | /// This struct serializes the LLVM IR module. |
| 120 | template <> struct BlockScalarTraits<Module> { |
| 121 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 122 | Mod.print(OS, nullptr); |
| 123 | } |
| 124 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 125 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 126 | return ""; |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | } // end namespace yaml |
| 131 | } // end namespace llvm |
| 132 | |
Alex Lorenz | 15a00a8 | 2015-07-14 21:18:25 +0000 | [diff] [blame] | 133 | static void printReg(unsigned Reg, raw_ostream &OS, |
| 134 | const TargetRegisterInfo *TRI) { |
| 135 | // TODO: Print Stack Slots. |
| 136 | if (!Reg) |
| 137 | OS << '_'; |
| 138 | else if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 139 | OS << '%' << TargetRegisterInfo::virtReg2Index(Reg); |
| 140 | else if (Reg < TRI->getNumRegs()) |
| 141 | OS << '%' << StringRef(TRI->getName(Reg)).lower(); |
| 142 | else |
| 143 | llvm_unreachable("Can't print this kind of register yet"); |
| 144 | } |
| 145 | |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 146 | static void printReg(unsigned Reg, yaml::StringValue &Dest, |
| 147 | const TargetRegisterInfo *TRI) { |
| 148 | raw_string_ostream OS(Dest.Value); |
| 149 | printReg(Reg, OS, TRI); |
| 150 | } |
| 151 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 152 | void MIRPrinter::print(const MachineFunction &MF) { |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 153 | initRegisterMaskIds(MF); |
| 154 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 155 | yaml::MachineFunction YamlMF; |
| 156 | YamlMF.Name = MF.getName(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 157 | YamlMF.Alignment = MF.getAlignment(); |
| 158 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 159 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 160 | convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo()); |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 161 | convert(YamlMF.FrameInfo, *MF.getFrameInfo()); |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 162 | convertStackObjects(YamlMF, *MF.getFrameInfo(), |
| 163 | MF.getSubtarget().getRegisterInfo()); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 164 | if (const auto *ConstantPool = MF.getConstantPool()) |
| 165 | convert(YamlMF, *ConstantPool); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 166 | |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 167 | ModuleSlotTracker MST(MF.getFunction()->getParent()); |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 168 | MST.incorporateFunction(*MF.getFunction()); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 169 | if (const auto *JumpTableInfo = MF.getJumpTableInfo()) |
| 170 | convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 171 | for (const auto &MBB : MF) { |
| 172 | yaml::MachineBasicBlock YamlMBB; |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 173 | convert(MST, YamlMBB, MBB); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 174 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 175 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 176 | yaml::Output Out(OS); |
| 177 | Out << YamlMF; |
| 178 | } |
| 179 | |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 180 | void MIRPrinter::convert(yaml::MachineFunction &MF, |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 181 | const MachineRegisterInfo &RegInfo, |
| 182 | const TargetRegisterInfo *TRI) { |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 183 | MF.IsSSA = RegInfo.isSSA(); |
| 184 | MF.TracksRegLiveness = RegInfo.tracksLiveness(); |
| 185 | MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled(); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 186 | |
| 187 | // Print the virtual register definitions. |
| 188 | for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) { |
| 189 | unsigned Reg = TargetRegisterInfo::index2VirtReg(I); |
| 190 | yaml::VirtualRegisterDefinition VReg; |
| 191 | VReg.ID = I; |
| 192 | VReg.Class = |
| 193 | StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower(); |
Alex Lorenz | ab4cbcf | 2015-07-24 20:35:40 +0000 | [diff] [blame] | 194 | unsigned PreferredReg = RegInfo.getSimpleHint(Reg); |
| 195 | if (PreferredReg) |
| 196 | printReg(PreferredReg, VReg.PreferredRegister, TRI); |
Alex Lorenz | 28148ba | 2015-07-09 22:23:13 +0000 | [diff] [blame] | 197 | MF.VirtualRegisters.push_back(VReg); |
| 198 | } |
Alex Lorenz | 12045a4 | 2015-07-27 17:42:45 +0000 | [diff] [blame] | 199 | |
| 200 | // Print the live ins. |
| 201 | for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) { |
| 202 | yaml::MachineFunctionLiveIn LiveIn; |
| 203 | printReg(I->first, LiveIn.Register, TRI); |
| 204 | if (I->second) |
| 205 | printReg(I->second, LiveIn.VirtualRegister, TRI); |
| 206 | MF.LiveIns.push_back(LiveIn); |
| 207 | } |
Alex Lorenz | 54565cf | 2015-06-24 19:56:10 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Alex Lorenz | 60541c1 | 2015-07-09 19:55:27 +0000 | [diff] [blame] | 210 | void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI, |
| 211 | const MachineFrameInfo &MFI) { |
| 212 | YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken(); |
| 213 | YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken(); |
| 214 | YamlMFI.HasStackMap = MFI.hasStackMap(); |
| 215 | YamlMFI.HasPatchPoint = MFI.hasPatchPoint(); |
| 216 | YamlMFI.StackSize = MFI.getStackSize(); |
| 217 | YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment(); |
| 218 | YamlMFI.MaxAlignment = MFI.getMaxAlignment(); |
| 219 | YamlMFI.AdjustsStack = MFI.adjustsStack(); |
| 220 | YamlMFI.HasCalls = MFI.hasCalls(); |
| 221 | YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize(); |
| 222 | YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment(); |
| 223 | YamlMFI.HasVAStart = MFI.hasVAStart(); |
| 224 | YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc(); |
| 225 | } |
| 226 | |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 227 | void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF, |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 228 | const MachineFrameInfo &MFI, |
| 229 | const TargetRegisterInfo *TRI) { |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 230 | // Process fixed stack objects. |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 231 | unsigned ID = 0; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 232 | for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) { |
| 233 | if (MFI.isDeadObjectIndex(I)) |
| 234 | continue; |
| 235 | |
| 236 | yaml::FixedMachineStackObject YamlObject; |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 237 | YamlObject.ID = ID; |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 238 | YamlObject.Type = MFI.isSpillSlotObjectIndex(I) |
| 239 | ? yaml::FixedMachineStackObject::SpillSlot |
| 240 | : yaml::FixedMachineStackObject::DefaultType; |
| 241 | YamlObject.Offset = MFI.getObjectOffset(I); |
| 242 | YamlObject.Size = MFI.getObjectSize(I); |
| 243 | YamlObject.Alignment = MFI.getObjectAlignment(I); |
| 244 | YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I); |
| 245 | YamlObject.IsAliased = MFI.isAliasedObjectIndex(I); |
| 246 | MF.FixedStackObjects.push_back(YamlObject); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 247 | StackObjectOperandMapping.insert( |
| 248 | std::make_pair(I, FrameIndexOperand::createFixed(ID++))); |
Alex Lorenz | de491f0 | 2015-07-13 18:07:26 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // Process ordinary stack objects. |
| 252 | ID = 0; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 253 | for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) { |
| 254 | if (MFI.isDeadObjectIndex(I)) |
| 255 | continue; |
| 256 | |
| 257 | yaml::MachineStackObject YamlObject; |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 258 | YamlObject.ID = ID; |
Alex Lorenz | 37643a0 | 2015-07-15 22:14:49 +0000 | [diff] [blame] | 259 | if (const auto *Alloca = MFI.getObjectAllocation(I)) |
| 260 | YamlObject.Name.Value = |
| 261 | Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>"; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 262 | YamlObject.Type = MFI.isSpillSlotObjectIndex(I) |
| 263 | ? yaml::MachineStackObject::SpillSlot |
Alex Lorenz | 418f3ec | 2015-07-14 00:26:26 +0000 | [diff] [blame] | 264 | : MFI.isVariableSizedObjectIndex(I) |
| 265 | ? yaml::MachineStackObject::VariableSized |
| 266 | : yaml::MachineStackObject::DefaultType; |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 267 | YamlObject.Offset = MFI.getObjectOffset(I); |
| 268 | YamlObject.Size = MFI.getObjectSize(I); |
| 269 | YamlObject.Alignment = MFI.getObjectAlignment(I); |
| 270 | |
| 271 | MF.StackObjects.push_back(YamlObject); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 272 | StackObjectOperandMapping.insert(std::make_pair( |
| 273 | I, FrameIndexOperand::create(YamlObject.Name.Value, ID++))); |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 274 | } |
Alex Lorenz | 1bb48de | 2015-07-24 22:22:50 +0000 | [diff] [blame] | 275 | |
| 276 | for (const auto &CSInfo : MFI.getCalleeSavedInfo()) { |
| 277 | yaml::StringValue Reg; |
| 278 | printReg(CSInfo.getReg(), Reg, TRI); |
| 279 | auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx()); |
| 280 | assert(StackObjectInfo != StackObjectOperandMapping.end() && |
| 281 | "Invalid stack object index"); |
| 282 | const FrameIndexOperand &StackObject = StackObjectInfo->second; |
| 283 | if (StackObject.IsFixed) |
| 284 | MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg; |
| 285 | else |
| 286 | MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg; |
| 287 | } |
Alex Lorenz | f6bc866 | 2015-07-10 18:13:57 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 290 | void MIRPrinter::convert(yaml::MachineFunction &MF, |
| 291 | const MachineConstantPool &ConstantPool) { |
| 292 | unsigned ID = 0; |
| 293 | for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) { |
| 294 | // TODO: Serialize target specific constant pool entries. |
| 295 | if (Constant.isMachineConstantPoolEntry()) |
| 296 | llvm_unreachable("Can't print target specific constant pool entries yet"); |
| 297 | |
| 298 | yaml::MachineConstantPoolValue YamlConstant; |
| 299 | std::string Str; |
| 300 | raw_string_ostream StrOS(Str); |
| 301 | Constant.Val.ConstVal->printAsOperand(StrOS); |
| 302 | YamlConstant.ID = ID++; |
| 303 | YamlConstant.Value = StrOS.str(); |
| 304 | YamlConstant.Alignment = Constant.getAlignment(); |
| 305 | MF.Constants.push_back(YamlConstant); |
| 306 | } |
| 307 | } |
| 308 | |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 309 | void MIRPrinter::convert(ModuleSlotTracker &MST, |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 310 | yaml::MachineJumpTable &YamlJTI, |
| 311 | const MachineJumpTableInfo &JTI) { |
| 312 | YamlJTI.Kind = JTI.getEntryKind(); |
| 313 | unsigned ID = 0; |
| 314 | for (const auto &Table : JTI.getJumpTables()) { |
| 315 | std::string Str; |
| 316 | yaml::MachineJumpTable::Entry Entry; |
| 317 | Entry.ID = ID++; |
| 318 | for (const auto *MBB : Table.MBBs) { |
| 319 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 320 | MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) |
| 321 | .printMBBReference(*MBB); |
Alex Lorenz | 6799e9b | 2015-07-15 23:31:07 +0000 | [diff] [blame] | 322 | Entry.Blocks.push_back(StrOS.str()); |
| 323 | Str.clear(); |
| 324 | } |
| 325 | YamlJTI.Entries.push_back(Entry); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void MIRPrinter::convert(ModuleSlotTracker &MST, |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 330 | yaml::MachineBasicBlock &YamlMBB, |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 331 | const MachineBasicBlock &MBB) { |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 332 | assert(MBB.getNumber() >= 0 && "Invalid MBB number"); |
| 333 | YamlMBB.ID = (unsigned)MBB.getNumber(); |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 334 | if (const auto *BB = MBB.getBasicBlock()) { |
| 335 | if (BB->hasName()) { |
| 336 | YamlMBB.Name.Value = BB->getName(); |
| 337 | } else { |
| 338 | int Slot = MST.getLocalSlot(BB); |
| 339 | if (Slot == -1) |
| 340 | YamlMBB.IRBlock.Value = "<badref>"; |
| 341 | else |
| 342 | YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str(); |
| 343 | } |
| 344 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 345 | YamlMBB.Alignment = MBB.getAlignment(); |
| 346 | YamlMBB.AddressTaken = MBB.hasAddressTaken(); |
| 347 | YamlMBB.IsLandingPad = MBB.isLandingPad(); |
Alex Lorenz | eb5112b | 2015-06-30 18:32:02 +0000 | [diff] [blame] | 348 | for (const auto *SuccMBB : MBB.successors()) { |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 349 | std::string Str; |
| 350 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 351 | MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) |
| 352 | .printMBBReference(*SuccMBB); |
Alex Lorenz | f09df00 | 2015-06-30 18:16:42 +0000 | [diff] [blame] | 353 | YamlMBB.Successors.push_back(StrOS.str()); |
| 354 | } |
Alex Lorenz | 9fab370 | 2015-07-14 21:24:41 +0000 | [diff] [blame] | 355 | // Print the live in registers. |
| 356 | const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo(); |
| 357 | assert(TRI && "Expected target register info"); |
| 358 | for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) { |
| 359 | std::string Str; |
| 360 | raw_string_ostream StrOS(Str); |
| 361 | printReg(*I, StrOS, TRI); |
| 362 | YamlMBB.LiveIns.push_back(StrOS.str()); |
| 363 | } |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 364 | // Print the machine instructions. |
| 365 | YamlMBB.Instructions.reserve(MBB.size()); |
| 366 | std::string Str; |
| 367 | for (const auto &MI : MBB) { |
| 368 | raw_string_ostream StrOS(Str); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 369 | MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 370 | YamlMBB.Instructions.push_back(StrOS.str()); |
| 371 | Str.clear(); |
| 372 | } |
| 373 | } |
| 374 | |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 375 | void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) { |
| 376 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); |
| 377 | unsigned I = 0; |
| 378 | for (const uint32_t *Mask : TRI->getRegMasks()) |
| 379 | RegisterMaskIds.insert(std::make_pair(Mask, I++)); |
| 380 | } |
| 381 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 382 | void MIPrinter::print(const MachineInstr &MI) { |
| 383 | const auto &SubTarget = MI.getParent()->getParent()->getSubtarget(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 384 | const auto *TRI = SubTarget.getRegisterInfo(); |
| 385 | assert(TRI && "Expected target register info"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 386 | const auto *TII = SubTarget.getInstrInfo(); |
| 387 | assert(TII && "Expected target instruction info"); |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 388 | if (MI.isCFIInstruction()) |
| 389 | assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 390 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 391 | unsigned I = 0, E = MI.getNumOperands(); |
| 392 | for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() && |
| 393 | !MI.getOperand(I).isImplicit(); |
| 394 | ++I) { |
| 395 | if (I) |
| 396 | OS << ", "; |
| 397 | print(MI.getOperand(I), TRI); |
| 398 | } |
| 399 | |
| 400 | if (I) |
| 401 | OS << " = "; |
Alex Lorenz | e5a4466 | 2015-07-17 00:24:15 +0000 | [diff] [blame] | 402 | if (MI.getFlag(MachineInstr::FrameSetup)) |
| 403 | OS << "frame-setup "; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 404 | OS << TII->getName(MI.getOpcode()); |
Alex Lorenz | e5a4466 | 2015-07-17 00:24:15 +0000 | [diff] [blame] | 405 | // TODO: Print the bundling instruction flags, machine mem operands. |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 406 | if (I < E) |
| 407 | OS << ' '; |
| 408 | |
| 409 | bool NeedComma = false; |
| 410 | for (; I < E; ++I) { |
| 411 | if (NeedComma) |
| 412 | OS << ", "; |
| 413 | print(MI.getOperand(I), TRI); |
| 414 | NeedComma = true; |
| 415 | } |
Alex Lorenz | 46d760d | 2015-07-22 21:15:11 +0000 | [diff] [blame] | 416 | |
| 417 | if (MI.getDebugLoc()) { |
| 418 | if (NeedComma) |
| 419 | OS << ','; |
| 420 | OS << " debug-location "; |
| 421 | MI.getDebugLoc()->printAsOperand(OS, MST); |
| 422 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 425 | void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) { |
| 426 | OS << "%bb." << MBB.getNumber(); |
| 427 | if (const auto *BB = MBB.getBasicBlock()) { |
| 428 | if (BB->hasName()) |
| 429 | OS << '.' << BB->getName(); |
| 430 | } |
| 431 | } |
| 432 | |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame^] | 433 | void MIPrinter::printIRBlockReference(const BasicBlock &BB) { |
| 434 | OS << "%ir-block."; |
| 435 | if (BB.hasName()) { |
| 436 | printLLVMNameWithoutPrefix(OS, BB.getName()); |
| 437 | return; |
| 438 | } |
| 439 | int Slot = MST.getLocalSlot(&BB); |
| 440 | if (Slot == -1) |
| 441 | OS << "<badref>"; |
| 442 | else |
| 443 | OS << Slot; |
| 444 | } |
| 445 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 446 | void MIPrinter::printStackObjectReference(int FrameIndex) { |
| 447 | auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex); |
| 448 | assert(ObjectInfo != StackObjectOperandMapping.end() && |
| 449 | "Invalid frame index"); |
| 450 | const FrameIndexOperand &Operand = ObjectInfo->second; |
| 451 | if (Operand.IsFixed) { |
| 452 | OS << "%fixed-stack." << Operand.ID; |
| 453 | return; |
| 454 | } |
| 455 | OS << "%stack." << Operand.ID; |
| 456 | if (!Operand.Name.empty()) |
| 457 | OS << '.' << Operand.Name; |
| 458 | } |
| 459 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 460 | void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) { |
| 461 | switch (Op.getType()) { |
| 462 | case MachineOperand::MO_Register: |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 463 | // TODO: Print the other register flags. |
| 464 | if (Op.isImplicit()) |
| 465 | OS << (Op.isDef() ? "implicit-def " : "implicit "); |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 466 | if (Op.isDead()) |
| 467 | OS << "dead "; |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 468 | if (Op.isKill()) |
| 469 | OS << "killed "; |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 470 | if (Op.isUndef()) |
| 471 | OS << "undef "; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 472 | printReg(Op.getReg(), OS, TRI); |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 473 | // Print the sub register. |
| 474 | if (Op.getSubReg() != 0) |
| 475 | OS << ':' << TRI->getSubRegIndexName(Op.getSubReg()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 476 | break; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 477 | case MachineOperand::MO_Immediate: |
| 478 | OS << Op.getImm(); |
| 479 | break; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 480 | case MachineOperand::MO_MachineBasicBlock: |
Alex Lorenz | 5d26fa8 | 2015-06-30 18:00:16 +0000 | [diff] [blame] | 481 | printMBBReference(*Op.getMBB()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 482 | break; |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 483 | case MachineOperand::MO_FrameIndex: |
| 484 | printStackObjectReference(Op.getIndex()); |
| 485 | break; |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 486 | case MachineOperand::MO_ConstantPoolIndex: |
| 487 | OS << "%const." << Op.getIndex(); |
| 488 | // TODO: Print offset and target flags. |
| 489 | break; |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 490 | case MachineOperand::MO_JumpTableIndex: |
| 491 | OS << "%jump-table." << Op.getIndex(); |
| 492 | // TODO: Print target flags. |
| 493 | break; |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 494 | case MachineOperand::MO_ExternalSymbol: |
| 495 | OS << '$'; |
| 496 | printLLVMNameWithoutPrefix(OS, Op.getSymbolName()); |
| 497 | // TODO: Print the target flags. |
| 498 | break; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 499 | case MachineOperand::MO_GlobalAddress: |
Alex Lorenz | 900b5cb | 2015-07-07 23:27:53 +0000 | [diff] [blame] | 500 | Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 501 | // TODO: Print offset and target flags. |
| 502 | break; |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame^] | 503 | case MachineOperand::MO_BlockAddress: |
| 504 | OS << "blockaddress("; |
| 505 | Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false, |
| 506 | MST); |
| 507 | OS << ", "; |
| 508 | printIRBlockReference(*Op.getBlockAddress()->getBasicBlock()); |
| 509 | OS << ')'; |
| 510 | // TODO: Print offset and target flags. |
| 511 | break; |
Alex Lorenz | 8f6f428 | 2015-06-29 16:57:06 +0000 | [diff] [blame] | 512 | case MachineOperand::MO_RegisterMask: { |
| 513 | auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask()); |
| 514 | if (RegMaskInfo != RegisterMaskIds.end()) |
| 515 | OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower(); |
| 516 | else |
| 517 | llvm_unreachable("Can't print this machine register mask yet."); |
| 518 | break; |
| 519 | } |
Alex Lorenz | 35e4446 | 2015-07-22 17:58:46 +0000 | [diff] [blame] | 520 | case MachineOperand::MO_Metadata: |
| 521 | Op.getMetadata()->printAsOperand(OS, MST); |
| 522 | break; |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 523 | case MachineOperand::MO_CFIIndex: { |
| 524 | const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI(); |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 525 | print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI); |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 526 | break; |
| 527 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 528 | default: |
| 529 | // TODO: Print the other machine operands. |
| 530 | llvm_unreachable("Can't print this machine operand at the moment"); |
| 531 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 534 | static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS, |
| 535 | const TargetRegisterInfo *TRI) { |
| 536 | int Reg = TRI->getLLVMRegNum(DwarfReg, true); |
| 537 | if (Reg == -1) { |
| 538 | OS << "<badreg>"; |
| 539 | return; |
| 540 | } |
| 541 | printReg(Reg, OS, TRI); |
| 542 | } |
| 543 | |
| 544 | void MIPrinter::print(const MCCFIInstruction &CFI, |
| 545 | const TargetRegisterInfo *TRI) { |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 546 | switch (CFI.getOperation()) { |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 547 | case MCCFIInstruction::OpOffset: |
| 548 | OS << ".cfi_offset "; |
| 549 | if (CFI.getLabel()) |
| 550 | OS << "<mcsymbol> "; |
| 551 | printCFIRegister(CFI.getRegister(), OS, TRI); |
| 552 | OS << ", " << CFI.getOffset(); |
| 553 | break; |
Alex Lorenz | 5b0d5f6 | 2015-07-27 20:39:03 +0000 | [diff] [blame] | 554 | case MCCFIInstruction::OpDefCfaRegister: |
| 555 | OS << ".cfi_def_cfa_register "; |
| 556 | if (CFI.getLabel()) |
| 557 | OS << "<mcsymbol> "; |
| 558 | printCFIRegister(CFI.getRegister(), OS, TRI); |
| 559 | break; |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 560 | case MCCFIInstruction::OpDefCfaOffset: |
| 561 | OS << ".cfi_def_cfa_offset "; |
| 562 | if (CFI.getLabel()) |
| 563 | OS << "<mcsymbol> "; |
| 564 | OS << CFI.getOffset(); |
| 565 | break; |
| 566 | default: |
| 567 | // TODO: Print the other CFI Operations. |
| 568 | OS << "<unserializable cfi operation>"; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 573 | void llvm::printMIR(raw_ostream &OS, const Module &M) { |
| 574 | yaml::Output Out(OS); |
| 575 | Out << const_cast<Module &>(M); |
| 576 | } |
| 577 | |
| 578 | void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { |
| 579 | MIRPrinter Printer(OS); |
| 580 | Printer.print(MF); |
| 581 | } |