blob: 0af2fbd94dd3282315123a0613934a9b58c2a6af [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 Lorenz345c1442015-06-15 23:52:35 +000022#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000023#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000029
30using namespace llvm;
31
32namespace {
33
34/// This class prints out the machine functions using the MIR serialization
35/// format.
36class MIRPrinter {
37 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000038 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz345c1442015-06-15 23:52:35 +000039
40public:
41 MIRPrinter(raw_ostream &OS) : OS(OS) {}
42
43 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000044
Alex Lorenz28148ba2015-07-09 22:23:13 +000045 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
46 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000047 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000048 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000049 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000050 void convertStackObjects(yaml::MachineFunction &MF,
51 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000052
53private:
54 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000055};
56
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000057/// This class prints out the machine instructions using the MIR serialization
58/// format.
59class MIPrinter {
60 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000061 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000062 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000063
64public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000065 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz8f6f4282015-06-29 16:57:06 +000066 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
Alex Lorenz900b5cb2015-07-07 23:27:53 +000067 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000068
69 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000070 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzf3db51de2015-06-23 16:35:26 +000071 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000072};
73
Alex Lorenz345c1442015-06-15 23:52:35 +000074} // end anonymous namespace
75
76namespace llvm {
77namespace yaml {
78
79/// This struct serializes the LLVM IR module.
80template <> struct BlockScalarTraits<Module> {
81 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
82 Mod.print(OS, nullptr);
83 }
84 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
85 llvm_unreachable("LLVM Module is supposed to be parsed separately");
86 return "";
87 }
88};
89
90} // end namespace yaml
91} // end namespace llvm
92
93void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +000094 initRegisterMaskIds(MF);
95
Alex Lorenz345c1442015-06-15 23:52:35 +000096 yaml::MachineFunction YamlMF;
97 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000098 YamlMF.Alignment = MF.getAlignment();
99 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
100 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000101 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000102 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000103 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000104
105 int I = 0;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000106 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000107 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000108 // TODO: Allow printing of non sequentially numbered MBBs.
109 // This is currently needed as the basic block references get their index
110 // from MBB.getNumber(), thus it should be sequential so that the parser can
111 // map back to the correct MBBs when parsing the output.
112 assert(MBB.getNumber() == I++ &&
113 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000114 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000115 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000116 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000117 YamlMF.BasicBlocks.push_back(YamlMBB);
118 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000119 yaml::Output Out(OS);
120 Out << YamlMF;
121}
122
Alex Lorenz54565cf2015-06-24 19:56:10 +0000123void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000124 const MachineRegisterInfo &RegInfo,
125 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000126 MF.IsSSA = RegInfo.isSSA();
127 MF.TracksRegLiveness = RegInfo.tracksLiveness();
128 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000129
130 // Print the virtual register definitions.
131 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
132 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
133 yaml::VirtualRegisterDefinition VReg;
134 VReg.ID = I;
135 VReg.Class =
136 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
137 MF.VirtualRegisters.push_back(VReg);
138 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000139}
140
Alex Lorenz60541c12015-07-09 19:55:27 +0000141void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
142 const MachineFrameInfo &MFI) {
143 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
144 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
145 YamlMFI.HasStackMap = MFI.hasStackMap();
146 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
147 YamlMFI.StackSize = MFI.getStackSize();
148 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
149 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
150 YamlMFI.AdjustsStack = MFI.adjustsStack();
151 YamlMFI.HasCalls = MFI.hasCalls();
152 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
153 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
154 YamlMFI.HasVAStart = MFI.hasVAStart();
155 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
156}
157
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000158void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
159 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000160 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000161 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000162 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
163 if (MFI.isDeadObjectIndex(I))
164 continue;
165
166 yaml::FixedMachineStackObject YamlObject;
167 YamlObject.ID = ID++;
168 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
169 ? yaml::FixedMachineStackObject::SpillSlot
170 : yaml::FixedMachineStackObject::DefaultType;
171 YamlObject.Offset = MFI.getObjectOffset(I);
172 YamlObject.Size = MFI.getObjectSize(I);
173 YamlObject.Alignment = MFI.getObjectAlignment(I);
174 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
175 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
176 MF.FixedStackObjects.push_back(YamlObject);
177 // TODO: Store the mapping between fixed object IDs and object indices to
178 // print the fixed stack object references correctly.
179 }
180
181 // Process ordinary stack objects.
182 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000183 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
184 if (MFI.isDeadObjectIndex(I))
185 continue;
186
187 yaml::MachineStackObject YamlObject;
188 YamlObject.ID = ID++;
189 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
190 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000191 : MFI.isVariableSizedObjectIndex(I)
192 ? yaml::MachineStackObject::VariableSized
193 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000194 YamlObject.Offset = MFI.getObjectOffset(I);
195 YamlObject.Size = MFI.getObjectSize(I);
196 YamlObject.Alignment = MFI.getObjectAlignment(I);
197
198 MF.StackObjects.push_back(YamlObject);
199 // TODO: Store the mapping between object IDs and object indices to print
200 // the stack object references correctly.
201 }
202}
203
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000204void MIRPrinter::convert(ModuleSlotTracker &MST,
205 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000206 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000207 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
208 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000209 // TODO: Serialize unnamed BB references.
210 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000211 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000212 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000213 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000214 YamlMBB.Alignment = MBB.getAlignment();
215 YamlMBB.AddressTaken = MBB.hasAddressTaken();
216 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000217 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000218 std::string Str;
219 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000220 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000221 YamlMBB.Successors.push_back(StrOS.str());
222 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000223
224 // Print the machine instructions.
225 YamlMBB.Instructions.reserve(MBB.size());
226 std::string Str;
227 for (const auto &MI : MBB) {
228 raw_string_ostream StrOS(Str);
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000229 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000230 YamlMBB.Instructions.push_back(StrOS.str());
231 Str.clear();
232 }
233}
234
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000235void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
236 const auto *TRI = MF.getSubtarget().getRegisterInfo();
237 unsigned I = 0;
238 for (const uint32_t *Mask : TRI->getRegMasks())
239 RegisterMaskIds.insert(std::make_pair(Mask, I++));
240}
241
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000242void MIPrinter::print(const MachineInstr &MI) {
243 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000244 const auto *TRI = SubTarget.getRegisterInfo();
245 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000246 const auto *TII = SubTarget.getInstrInfo();
247 assert(TII && "Expected target instruction info");
248
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000249 unsigned I = 0, E = MI.getNumOperands();
250 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
251 !MI.getOperand(I).isImplicit();
252 ++I) {
253 if (I)
254 OS << ", ";
255 print(MI.getOperand(I), TRI);
256 }
257
258 if (I)
259 OS << " = ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000260 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000261 // TODO: Print the instruction flags, machine mem operands.
262 if (I < E)
263 OS << ' ';
264
265 bool NeedComma = false;
266 for (; I < E; ++I) {
267 if (NeedComma)
268 OS << ", ";
269 print(MI.getOperand(I), TRI);
270 NeedComma = true;
271 }
272}
273
274static void printReg(unsigned Reg, raw_ostream &OS,
275 const TargetRegisterInfo *TRI) {
276 // TODO: Print Stack Slots.
Alex Lorenz12b554e2015-06-24 17:34:58 +0000277 if (!Reg)
278 OS << '_';
Alex Lorenz53464512015-07-10 22:51:20 +0000279 else if (TargetRegisterInfo::isVirtualRegister(Reg))
280 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
Alex Lorenz12b554e2015-06-24 17:34:58 +0000281 else if (Reg < TRI->getNumRegs())
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000282 OS << '%' << StringRef(TRI->getName(Reg)).lower();
283 else
284 llvm_unreachable("Can't print this kind of register yet");
285}
286
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000287void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
288 OS << "%bb." << MBB.getNumber();
289 if (const auto *BB = MBB.getBasicBlock()) {
290 if (BB->hasName())
291 OS << '.' << BB->getName();
292 }
293}
294
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000295void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
296 switch (Op.getType()) {
297 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000298 // TODO: Print the other register flags.
299 if (Op.isImplicit())
300 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000301 if (Op.isDead())
302 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000303 if (Op.isKill())
304 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000305 if (Op.isUndef())
306 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000307 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000308 // Print the sub register.
309 if (Op.getSubReg() != 0)
310 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000311 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000312 case MachineOperand::MO_Immediate:
313 OS << Op.getImm();
314 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000315 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000316 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000317 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000318 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000319 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000320 // TODO: Print offset and target flags.
321 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000322 case MachineOperand::MO_RegisterMask: {
323 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
324 if (RegMaskInfo != RegisterMaskIds.end())
325 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
326 else
327 llvm_unreachable("Can't print this machine register mask yet.");
328 break;
329 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000330 default:
331 // TODO: Print the other machine operands.
332 llvm_unreachable("Can't print this machine operand at the moment");
333 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000334}
335
Alex Lorenz345c1442015-06-15 23:52:35 +0000336void llvm::printMIR(raw_ostream &OS, const Module &M) {
337 yaml::Output Out(OS);
338 Out << const_cast<Module &>(M);
339}
340
341void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
342 MIRPrinter Printer(OS);
343 Printer.print(MF);
344}