blob: 1adfdef67c32415dea4270e1bdccf7e4d768dc74 [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 Lorenz900b5cb2015-07-07 23:27:53 +000049 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000050 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000051 void convertStackObjects(yaml::MachineFunction &MF,
52 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000053
54private:
55 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000056};
57
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000058/// This class prints out the machine instructions using the MIR serialization
59/// format.
60class MIPrinter {
61 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000062 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000063 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000064
65public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000066 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000067 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000068 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000069
70 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000071 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000072 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000073};
74
Alex Lorenz345c1442015-06-15 23:52:35 +000075} // end anonymous namespace
76
77namespace llvm {
78namespace yaml {
79
80/// This struct serializes the LLVM IR module.
81template <> struct BlockScalarTraits<Module> {
82 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
83 Mod.print(OS, nullptr);
84 }
85 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
86 llvm_unreachable("LLVM Module is supposed to be parsed separately");
87 return "";
88 }
89};
90
91} // end namespace yaml
92} // end namespace llvm
93
Alex Lorenz15a00a82015-07-14 21:18:25 +000094static void printReg(unsigned Reg, raw_ostream &OS,
95 const TargetRegisterInfo *TRI) {
96 // TODO: Print Stack Slots.
97 if (!Reg)
98 OS << '_';
99 else if (TargetRegisterInfo::isVirtualRegister(Reg))
100 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
101 else if (Reg < TRI->getNumRegs())
102 OS << '%' << StringRef(TRI->getName(Reg)).lower();
103 else
104 llvm_unreachable("Can't print this kind of register yet");
105}
106
Alex Lorenz345c1442015-06-15 23:52:35 +0000107void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000108 initRegisterMaskIds(MF);
109
Alex Lorenz345c1442015-06-15 23:52:35 +0000110 yaml::MachineFunction YamlMF;
111 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000112 YamlMF.Alignment = MF.getAlignment();
113 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
114 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000115 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000116 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000117 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000118
119 int I = 0;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000120 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000121 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000122 // TODO: Allow printing of non sequentially numbered MBBs.
123 // This is currently needed as the basic block references get their index
124 // from MBB.getNumber(), thus it should be sequential so that the parser can
125 // map back to the correct MBBs when parsing the output.
126 assert(MBB.getNumber() == I++ &&
127 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000128 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000129 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000130 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000131 YamlMF.BasicBlocks.push_back(YamlMBB);
132 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000133 yaml::Output Out(OS);
134 Out << YamlMF;
135}
136
Alex Lorenz54565cf2015-06-24 19:56:10 +0000137void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000138 const MachineRegisterInfo &RegInfo,
139 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000140 MF.IsSSA = RegInfo.isSSA();
141 MF.TracksRegLiveness = RegInfo.tracksLiveness();
142 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000143
144 // Print the virtual register definitions.
145 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
146 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
147 yaml::VirtualRegisterDefinition VReg;
148 VReg.ID = I;
149 VReg.Class =
150 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
151 MF.VirtualRegisters.push_back(VReg);
152 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000153}
154
Alex Lorenz60541c12015-07-09 19:55:27 +0000155void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
156 const MachineFrameInfo &MFI) {
157 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
158 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
159 YamlMFI.HasStackMap = MFI.hasStackMap();
160 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
161 YamlMFI.StackSize = MFI.getStackSize();
162 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
163 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
164 YamlMFI.AdjustsStack = MFI.adjustsStack();
165 YamlMFI.HasCalls = MFI.hasCalls();
166 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
167 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
168 YamlMFI.HasVAStart = MFI.hasVAStart();
169 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
170}
171
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000172void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
173 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000174 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000175 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000176 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
177 if (MFI.isDeadObjectIndex(I))
178 continue;
179
180 yaml::FixedMachineStackObject YamlObject;
181 YamlObject.ID = ID++;
182 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
183 ? yaml::FixedMachineStackObject::SpillSlot
184 : yaml::FixedMachineStackObject::DefaultType;
185 YamlObject.Offset = MFI.getObjectOffset(I);
186 YamlObject.Size = MFI.getObjectSize(I);
187 YamlObject.Alignment = MFI.getObjectAlignment(I);
188 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
189 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
190 MF.FixedStackObjects.push_back(YamlObject);
191 // TODO: Store the mapping between fixed object IDs and object indices to
192 // print the fixed stack object references correctly.
193 }
194
195 // Process ordinary stack objects.
196 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000197 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
198 if (MFI.isDeadObjectIndex(I))
199 continue;
200
201 yaml::MachineStackObject YamlObject;
202 YamlObject.ID = ID++;
Alex Lorenz37643a02015-07-15 22:14:49 +0000203 if (const auto *Alloca = MFI.getObjectAllocation(I))
204 YamlObject.Name.Value =
205 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000206 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
207 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000208 : MFI.isVariableSizedObjectIndex(I)
209 ? yaml::MachineStackObject::VariableSized
210 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000211 YamlObject.Offset = MFI.getObjectOffset(I);
212 YamlObject.Size = MFI.getObjectSize(I);
213 YamlObject.Alignment = MFI.getObjectAlignment(I);
214
215 MF.StackObjects.push_back(YamlObject);
216 // TODO: Store the mapping between object IDs and object indices to print
217 // the stack object references correctly.
218 }
219}
220
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000221void MIRPrinter::convert(ModuleSlotTracker &MST,
222 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000223 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000224 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
225 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000226 // TODO: Serialize unnamed BB references.
227 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000228 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000229 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000230 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000231 YamlMBB.Alignment = MBB.getAlignment();
232 YamlMBB.AddressTaken = MBB.hasAddressTaken();
233 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000234 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000235 std::string Str;
236 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000237 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000238 YamlMBB.Successors.push_back(StrOS.str());
239 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000240 // Print the live in registers.
241 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
242 assert(TRI && "Expected target register info");
243 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
244 std::string Str;
245 raw_string_ostream StrOS(Str);
246 printReg(*I, StrOS, TRI);
247 YamlMBB.LiveIns.push_back(StrOS.str());
248 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000249 // Print the machine instructions.
250 YamlMBB.Instructions.reserve(MBB.size());
251 std::string Str;
252 for (const auto &MI : MBB) {
253 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000254 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000255 YamlMBB.Instructions.push_back(StrOS.str());
256 Str.clear();
257 }
258}
259
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000260void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
261 const auto *TRI = MF.getSubtarget().getRegisterInfo();
262 unsigned I = 0;
263 for (const uint32_t *Mask : TRI->getRegMasks())
264 RegisterMaskIds.insert(std::make_pair(Mask, I++));
265}
266
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000267void MIPrinter::print(const MachineInstr &MI) {
268 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000269 const auto *TRI = SubTarget.getRegisterInfo();
270 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000271 const auto *TII = SubTarget.getInstrInfo();
272 assert(TII && "Expected target instruction info");
273
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000274 unsigned I = 0, E = MI.getNumOperands();
275 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
276 !MI.getOperand(I).isImplicit();
277 ++I) {
278 if (I)
279 OS << ", ";
280 print(MI.getOperand(I), TRI);
281 }
282
283 if (I)
284 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000285 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000286 // TODO: Print the instruction flags, machine mem operands.
287 if (I < E)
288 OS << ' ';
289
290 bool NeedComma = false;
291 for (; I < E; ++I) {
292 if (NeedComma)
293 OS << ", ";
294 print(MI.getOperand(I), TRI);
295 NeedComma = true;
296 }
297}
298
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000299void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
300 OS << "%bb." << MBB.getNumber();
301 if (const auto *BB = MBB.getBasicBlock()) {
302 if (BB->hasName())
303 OS << '.' << BB->getName();
304 }
305}
306
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000307void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
308 switch (Op.getType()) {
309 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000310 // TODO: Print the other register flags.
311 if (Op.isImplicit())
312 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000313 if (Op.isDead())
314 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000315 if (Op.isKill())
316 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000317 if (Op.isUndef())
318 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000319 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000320 // Print the sub register.
321 if (Op.getSubReg() != 0)
322 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000323 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000324 case MachineOperand::MO_Immediate:
325 OS << Op.getImm();
326 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000327 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000328 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000329 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000330 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000331 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000332 // TODO: Print offset and target flags.
333 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000334 case MachineOperand::MO_RegisterMask: {
335 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
336 if (RegMaskInfo != RegisterMaskIds.end())
337 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
338 else
339 llvm_unreachable("Can't print this machine register mask yet.");
340 break;
341 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000342 default:
343 // TODO: Print the other machine operands.
344 llvm_unreachable("Can't print this machine operand at the moment");
345 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000346}
347
Alex Lorenz345c1442015-06-15 23:52:35 +0000348void llvm::printMIR(raw_ostream &OS, const Module &M) {
349 yaml::Output Out(OS);
350 Out << const_cast<Module &>(M);
351}
352
353void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
354 MIRPrinter Printer(OS);
355 Printer.print(MF);
356}