blob: a1c28bc5697b3695094cd61a158056d091dba228 [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
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000035/// This structure describes how to print out stack object references.
36struct 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 Lorenz345c1442015-06-15 23:52:35 +000055/// This class prints out the machine functions using the MIR serialization
56/// format.
57class MIRPrinter {
58 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000059 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000060 /// 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 Lorenz345c1442015-06-15 23:52:35 +000063
64public:
65 MIRPrinter(raw_ostream &OS) : OS(OS) {}
66
67 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000068
Alex Lorenz28148ba2015-07-09 22:23:13 +000069 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
70 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000071 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000072 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
73 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000074 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000075 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000076 void convertStackObjects(yaml::MachineFunction &MF,
77 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000078
79private:
80 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000081};
82
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083/// This class prints out the machine instructions using the MIR serialization
84/// format.
85class MIPrinter {
86 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000087 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000088 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000089 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090
91public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000092 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000093 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
94 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
95 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
96 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000097
98 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +000099 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000100 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000101 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000102};
103
Alex Lorenz345c1442015-06-15 23:52:35 +0000104} // end anonymous namespace
105
106namespace llvm {
107namespace yaml {
108
109/// This struct serializes the LLVM IR module.
110template <> 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 Lorenz15a00a82015-07-14 21:18:25 +0000123static 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 Lorenz345c1442015-06-15 23:52:35 +0000136void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000137 initRegisterMaskIds(MF);
138
Alex Lorenz345c1442015-06-15 23:52:35 +0000139 yaml::MachineFunction YamlMF;
140 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000141 YamlMF.Alignment = MF.getAlignment();
142 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
143 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000144 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000145 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000146 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000147
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000148 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000149 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
150 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
151 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000152 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000153 // 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 Lorenzec6b26b2015-06-26 17:07:27 +0000159 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000160 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000161 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000162 YamlMF.BasicBlocks.push_back(YamlMBB);
163 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000164 yaml::Output Out(OS);
165 Out << YamlMF;
166}
167
Alex Lorenz54565cf2015-06-24 19:56:10 +0000168void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000169 const MachineRegisterInfo &RegInfo,
170 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000171 MF.IsSSA = RegInfo.isSSA();
172 MF.TracksRegLiveness = RegInfo.tracksLiveness();
173 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000174
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 Lorenz54565cf2015-06-24 19:56:10 +0000184}
185
Alex Lorenz60541c12015-07-09 19:55:27 +0000186void 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 Lorenzf6bc8662015-07-10 18:13:57 +0000203void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
204 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000205 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000206 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000207 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
208 if (MFI.isDeadObjectIndex(I))
209 continue;
210
211 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000212 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000213 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 Lorenz7feaf7c2015-07-16 23:37:45 +0000222 StackObjectOperandMapping.insert(
223 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000224 }
225
226 // Process ordinary stack objects.
227 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000228 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
229 if (MFI.isDeadObjectIndex(I))
230 continue;
231
232 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000233 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000234 if (const auto *Alloca = MFI.getObjectAllocation(I))
235 YamlObject.Name.Value =
236 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000237 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
238 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000239 : MFI.isVariableSizedObjectIndex(I)
240 ? yaml::MachineStackObject::VariableSized
241 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000242 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 Lorenz7feaf7c2015-07-16 23:37:45 +0000247 StackObjectOperandMapping.insert(std::make_pair(
248 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000249 }
250}
251
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000252void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000253 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 Lorenz7feaf7c2015-07-16 23:37:45 +0000263 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
264 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000265 Entry.Blocks.push_back(StrOS.str());
266 Str.clear();
267 }
268 YamlJTI.Entries.push_back(Entry);
269 }
270}
271
272void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000273 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000274 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000275 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
276 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000277 // TODO: Serialize unnamed BB references.
278 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000279 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000280 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000281 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000282 YamlMBB.Alignment = MBB.getAlignment();
283 YamlMBB.AddressTaken = MBB.hasAddressTaken();
284 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000285 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000286 std::string Str;
287 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000288 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
289 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000290 YamlMBB.Successors.push_back(StrOS.str());
291 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000292 // 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 Lorenz8e0a1b42015-06-22 17:02:30 +0000301 // 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 Lorenz7feaf7c2015-07-16 23:37:45 +0000306 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000307 YamlMBB.Instructions.push_back(StrOS.str());
308 Str.clear();
309 }
310}
311
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000312void 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 Lorenz8e0a1b42015-06-22 17:02:30 +0000319void MIPrinter::print(const MachineInstr &MI) {
320 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000321 const auto *TRI = SubTarget.getRegisterInfo();
322 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000323 const auto *TII = SubTarget.getInstrInfo();
324 assert(TII && "Expected target instruction info");
325
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000326 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 Lorenze5a44662015-07-17 00:24:15 +0000337 if (MI.getFlag(MachineInstr::FrameSetup))
338 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000339 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000340 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000341 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 Lorenz5d26fa82015-06-30 18:00:16 +0000353void 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 Lorenz7feaf7c2015-07-16 23:37:45 +0000361void 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 Lorenzf3db51de2015-06-23 16:35:26 +0000375void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
376 switch (Op.getType()) {
377 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000378 // TODO: Print the other register flags.
379 if (Op.isImplicit())
380 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000381 if (Op.isDead())
382 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000383 if (Op.isKill())
384 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000385 if (Op.isUndef())
386 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000387 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000388 // Print the sub register.
389 if (Op.getSubReg() != 0)
390 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000391 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000392 case MachineOperand::MO_Immediate:
393 OS << Op.getImm();
394 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000395 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000396 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000397 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000398 case MachineOperand::MO_FrameIndex:
399 printStackObjectReference(Op.getIndex());
400 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000401 case MachineOperand::MO_JumpTableIndex:
402 OS << "%jump-table." << Op.getIndex();
403 // TODO: Print target flags.
404 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000405 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000406 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000407 // TODO: Print offset and target flags.
408 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000409 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 Lorenzf3db51de2015-06-23 16:35:26 +0000417 default:
418 // TODO: Print the other machine operands.
419 llvm_unreachable("Can't print this machine operand at the moment");
420 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000421}
422
Alex Lorenz345c1442015-06-15 23:52:35 +0000423void llvm::printMIR(raw_ostream &OS, const Module &M) {
424 yaml::Output Out(OS);
425 Out << const_cast<Module &>(M);
426}
427
428void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
429 MIRPrinter Printer(OS);
430 Printer.print(MF);
431}