blob: 97c323aaba4300d5c1d1b2a8c41b827ee37adf0c [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- 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 Lorenz60541c12015-07-09 19:55:27 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000020#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000021#include "llvm/IR/BasicBlock.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000022#include "llvm/IR/Instructions.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000023#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000024#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000030
31using namespace llvm;
32
33namespace {
34
35/// This class prints out the machine functions using the MIR serialization
36/// format.
37class MIRPrinter {
38 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000039 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz345c1442015-06-15 23:52:35 +000040
41public:
42 MIRPrinter(raw_ostream &OS) : OS(OS) {}
43
44 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000045
Alex Lorenz28148ba2015-07-09 22:23:13 +000046 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
47 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000048 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000049 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
50 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000051 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000052 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000053 void convertStackObjects(yaml::MachineFunction &MF,
54 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000055
56private:
57 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000058};
59
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000060/// This class prints out the machine instructions using the MIR serialization
61/// format.
62class MIPrinter {
63 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000064 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000065 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066
67public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000068 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000069 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000070 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000071
72 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000073 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000074 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000075};
76
Alex Lorenz345c1442015-06-15 23:52:35 +000077} // end anonymous namespace
78
79namespace llvm {
80namespace yaml {
81
82/// This struct serializes the LLVM IR module.
83template <> 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 Lorenz15a00a82015-07-14 21:18:25 +000096static 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 Lorenz345c1442015-06-15 23:52:35 +0000109void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000110 initRegisterMaskIds(MF);
111
Alex Lorenz345c1442015-06-15 23:52:35 +0000112 yaml::MachineFunction YamlMF;
113 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000114 YamlMF.Alignment = MF.getAlignment();
115 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
116 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000117 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000118 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000119 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000120
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000121 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000122 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
123 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
124 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000125 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000126 // 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 Lorenzec6b26b2015-06-26 17:07:27 +0000132 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000133 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000134 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000135 YamlMF.BasicBlocks.push_back(YamlMBB);
136 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000137 yaml::Output Out(OS);
138 Out << YamlMF;
139}
140
Alex Lorenz54565cf2015-06-24 19:56:10 +0000141void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000142 const MachineRegisterInfo &RegInfo,
143 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000144 MF.IsSSA = RegInfo.isSSA();
145 MF.TracksRegLiveness = RegInfo.tracksLiveness();
146 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000147
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 Lorenz54565cf2015-06-24 19:56:10 +0000157}
158
Alex Lorenz60541c12015-07-09 19:55:27 +0000159void 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 Lorenzf6bc8662015-07-10 18:13:57 +0000176void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
177 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000178 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000179 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000180 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 Lorenzf6bc8662015-07-10 18:13:57 +0000201 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 Lorenz37643a02015-07-15 22:14:49 +0000207 if (const auto *Alloca = MFI.getObjectAllocation(I))
208 YamlObject.Name.Value =
209 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000210 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
211 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000212 : MFI.isVariableSizedObjectIndex(I)
213 ? yaml::MachineStackObject::VariableSized
214 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000215 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 Lorenz900b5cb2015-07-07 23:27:53 +0000225void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000226 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
244void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000245 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000246 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000247 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
248 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000249 // TODO: Serialize unnamed BB references.
250 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000251 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000252 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000253 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000254 YamlMBB.Alignment = MBB.getAlignment();
255 YamlMBB.AddressTaken = MBB.hasAddressTaken();
256 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000257 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000258 std::string Str;
259 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000260 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000261 YamlMBB.Successors.push_back(StrOS.str());
262 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000263 // 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 Lorenz8e0a1b42015-06-22 17:02:30 +0000272 // 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 Lorenz900b5cb2015-07-07 23:27:53 +0000277 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000278 YamlMBB.Instructions.push_back(StrOS.str());
279 Str.clear();
280 }
281}
282
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000283void 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 Lorenz8e0a1b42015-06-22 17:02:30 +0000290void MIPrinter::print(const MachineInstr &MI) {
291 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000292 const auto *TRI = SubTarget.getRegisterInfo();
293 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000294 const auto *TII = SubTarget.getInstrInfo();
295 assert(TII && "Expected target instruction info");
296
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000297 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 Lorenz8e0a1b42015-06-22 17:02:30 +0000308 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000309 // 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 Lorenz5d26fa82015-06-30 18:00:16 +0000322void 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 Lorenzf3db51de2015-06-23 16:35:26 +0000330void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
331 switch (Op.getType()) {
332 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000333 // TODO: Print the other register flags.
334 if (Op.isImplicit())
335 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000336 if (Op.isDead())
337 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000338 if (Op.isKill())
339 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000340 if (Op.isUndef())
341 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000342 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000343 // Print the sub register.
344 if (Op.getSubReg() != 0)
345 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000346 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000347 case MachineOperand::MO_Immediate:
348 OS << Op.getImm();
349 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000350 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000351 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000352 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000353 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000354 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000355 // TODO: Print offset and target flags.
356 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000357 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 Lorenzf3db51de2015-06-23 16:35:26 +0000365 default:
366 // TODO: Print the other machine operands.
367 llvm_unreachable("Can't print this machine operand at the moment");
368 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000369}
370
Alex Lorenz345c1442015-06-15 23:52:35 +0000371void llvm::printMIR(raw_ostream &OS, const Module &M) {
372 yaml::Output Out(OS);
373 Out << const_cast<Module &>(M);
374}
375
376void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
377 MIRPrinter Printer(OS);
378 Printer.print(MF);
379}