blob: c3a655aec0b4203b4bc83a71a7c8cc6436792802 [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"
Alex Lorenzab980492015-07-20 20:51:18 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000018#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000021#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000022#include "llvm/IR/BasicBlock.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000023#include "llvm/IR/Instructions.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000024#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000025#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000031
32using namespace llvm;
33
34namespace {
35
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000036/// This structure describes how to print out stack object references.
37struct FrameIndexOperand {
38 std::string Name;
39 unsigned ID;
40 bool IsFixed;
41
42 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
43 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
44
45 /// Return an ordinary stack object reference.
46 static FrameIndexOperand create(StringRef Name, unsigned ID) {
47 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
48 }
49
50 /// Return a fixed stack object reference.
51 static FrameIndexOperand createFixed(unsigned ID) {
52 return FrameIndexOperand("", ID, /*IsFixed=*/true);
53 }
54};
55
Alex Lorenz345c1442015-06-15 23:52:35 +000056/// This class prints out the machine functions using the MIR serialization
57/// format.
58class MIRPrinter {
59 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000060 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000061 /// Maps from stack object indices to operand indices which will be used when
62 /// printing frame index machine operands.
63 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000064
65public:
66 MIRPrinter(raw_ostream &OS) : OS(OS) {}
67
68 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000069
Alex Lorenz28148ba2015-07-09 22:23:13 +000070 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
71 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000072 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000073 void convert(yaml::MachineFunction &MF,
74 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000075 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
76 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000077 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000078 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000079 void convertStackObjects(yaml::MachineFunction &MF,
80 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000081
82private:
83 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000084};
85
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000086/// This class prints out the machine instructions using the MIR serialization
87/// format.
88class MIPrinter {
89 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000090 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000091 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000092 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000093
94public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000095 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000096 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
97 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
98 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
99 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100
101 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000102 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000103 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000104 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105};
106
Alex Lorenz345c1442015-06-15 23:52:35 +0000107} // end anonymous namespace
108
109namespace llvm {
110namespace yaml {
111
112/// This struct serializes the LLVM IR module.
113template <> struct BlockScalarTraits<Module> {
114 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
115 Mod.print(OS, nullptr);
116 }
117 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
118 llvm_unreachable("LLVM Module is supposed to be parsed separately");
119 return "";
120 }
121};
122
123} // end namespace yaml
124} // end namespace llvm
125
Alex Lorenz15a00a82015-07-14 21:18:25 +0000126static void printReg(unsigned Reg, raw_ostream &OS,
127 const TargetRegisterInfo *TRI) {
128 // TODO: Print Stack Slots.
129 if (!Reg)
130 OS << '_';
131 else if (TargetRegisterInfo::isVirtualRegister(Reg))
132 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
133 else if (Reg < TRI->getNumRegs())
134 OS << '%' << StringRef(TRI->getName(Reg)).lower();
135 else
136 llvm_unreachable("Can't print this kind of register yet");
137}
138
Alex Lorenz345c1442015-06-15 23:52:35 +0000139void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000140 initRegisterMaskIds(MF);
141
Alex Lorenz345c1442015-06-15 23:52:35 +0000142 yaml::MachineFunction YamlMF;
143 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000144 YamlMF.Alignment = MF.getAlignment();
145 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
146 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000147 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000148 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000149 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000150 if (const auto *ConstantPool = MF.getConstantPool())
151 convert(YamlMF, *ConstantPool);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000152
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000153 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000154 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
155 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
156 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000157 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000158 // TODO: Allow printing of non sequentially numbered MBBs.
159 // This is currently needed as the basic block references get their index
160 // from MBB.getNumber(), thus it should be sequential so that the parser can
161 // map back to the correct MBBs when parsing the output.
162 assert(MBB.getNumber() == I++ &&
163 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000164 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000165 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000166 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000167 YamlMF.BasicBlocks.push_back(YamlMBB);
168 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000169 yaml::Output Out(OS);
170 Out << YamlMF;
171}
172
Alex Lorenz54565cf2015-06-24 19:56:10 +0000173void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000174 const MachineRegisterInfo &RegInfo,
175 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000176 MF.IsSSA = RegInfo.isSSA();
177 MF.TracksRegLiveness = RegInfo.tracksLiveness();
178 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000179
180 // Print the virtual register definitions.
181 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
182 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
183 yaml::VirtualRegisterDefinition VReg;
184 VReg.ID = I;
185 VReg.Class =
186 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
187 MF.VirtualRegisters.push_back(VReg);
188 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000189}
190
Alex Lorenz60541c12015-07-09 19:55:27 +0000191void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
192 const MachineFrameInfo &MFI) {
193 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
194 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
195 YamlMFI.HasStackMap = MFI.hasStackMap();
196 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
197 YamlMFI.StackSize = MFI.getStackSize();
198 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
199 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
200 YamlMFI.AdjustsStack = MFI.adjustsStack();
201 YamlMFI.HasCalls = MFI.hasCalls();
202 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
203 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
204 YamlMFI.HasVAStart = MFI.hasVAStart();
205 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
206}
207
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000208void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
209 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000210 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000211 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000212 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
213 if (MFI.isDeadObjectIndex(I))
214 continue;
215
216 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000217 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000218 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
219 ? yaml::FixedMachineStackObject::SpillSlot
220 : yaml::FixedMachineStackObject::DefaultType;
221 YamlObject.Offset = MFI.getObjectOffset(I);
222 YamlObject.Size = MFI.getObjectSize(I);
223 YamlObject.Alignment = MFI.getObjectAlignment(I);
224 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
225 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
226 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000227 StackObjectOperandMapping.insert(
228 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000229 }
230
231 // Process ordinary stack objects.
232 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000233 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
234 if (MFI.isDeadObjectIndex(I))
235 continue;
236
237 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000238 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000239 if (const auto *Alloca = MFI.getObjectAllocation(I))
240 YamlObject.Name.Value =
241 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000242 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
243 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000244 : MFI.isVariableSizedObjectIndex(I)
245 ? yaml::MachineStackObject::VariableSized
246 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000247 YamlObject.Offset = MFI.getObjectOffset(I);
248 YamlObject.Size = MFI.getObjectSize(I);
249 YamlObject.Alignment = MFI.getObjectAlignment(I);
250
251 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000252 StackObjectOperandMapping.insert(std::make_pair(
253 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000254 }
255}
256
Alex Lorenzab980492015-07-20 20:51:18 +0000257void MIRPrinter::convert(yaml::MachineFunction &MF,
258 const MachineConstantPool &ConstantPool) {
259 unsigned ID = 0;
260 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
261 // TODO: Serialize target specific constant pool entries.
262 if (Constant.isMachineConstantPoolEntry())
263 llvm_unreachable("Can't print target specific constant pool entries yet");
264
265 yaml::MachineConstantPoolValue YamlConstant;
266 std::string Str;
267 raw_string_ostream StrOS(Str);
268 Constant.Val.ConstVal->printAsOperand(StrOS);
269 YamlConstant.ID = ID++;
270 YamlConstant.Value = StrOS.str();
271 YamlConstant.Alignment = Constant.getAlignment();
272 MF.Constants.push_back(YamlConstant);
273 }
274}
275
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000276void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000277 yaml::MachineJumpTable &YamlJTI,
278 const MachineJumpTableInfo &JTI) {
279 YamlJTI.Kind = JTI.getEntryKind();
280 unsigned ID = 0;
281 for (const auto &Table : JTI.getJumpTables()) {
282 std::string Str;
283 yaml::MachineJumpTable::Entry Entry;
284 Entry.ID = ID++;
285 for (const auto *MBB : Table.MBBs) {
286 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000287 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
288 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000289 Entry.Blocks.push_back(StrOS.str());
290 Str.clear();
291 }
292 YamlJTI.Entries.push_back(Entry);
293 }
294}
295
296void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000297 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000298 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000299 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
300 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000301 // TODO: Serialize unnamed BB references.
302 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000303 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000304 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000305 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000306 YamlMBB.Alignment = MBB.getAlignment();
307 YamlMBB.AddressTaken = MBB.hasAddressTaken();
308 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000309 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000310 std::string Str;
311 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000312 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
313 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000314 YamlMBB.Successors.push_back(StrOS.str());
315 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000316 // Print the live in registers.
317 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
318 assert(TRI && "Expected target register info");
319 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
320 std::string Str;
321 raw_string_ostream StrOS(Str);
322 printReg(*I, StrOS, TRI);
323 YamlMBB.LiveIns.push_back(StrOS.str());
324 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000325 // Print the machine instructions.
326 YamlMBB.Instructions.reserve(MBB.size());
327 std::string Str;
328 for (const auto &MI : MBB) {
329 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000330 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000331 YamlMBB.Instructions.push_back(StrOS.str());
332 Str.clear();
333 }
334}
335
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000336void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
337 const auto *TRI = MF.getSubtarget().getRegisterInfo();
338 unsigned I = 0;
339 for (const uint32_t *Mask : TRI->getRegMasks())
340 RegisterMaskIds.insert(std::make_pair(Mask, I++));
341}
342
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000343void MIPrinter::print(const MachineInstr &MI) {
344 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000345 const auto *TRI = SubTarget.getRegisterInfo();
346 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000347 const auto *TII = SubTarget.getInstrInfo();
348 assert(TII && "Expected target instruction info");
349
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000350 unsigned I = 0, E = MI.getNumOperands();
351 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
352 !MI.getOperand(I).isImplicit();
353 ++I) {
354 if (I)
355 OS << ", ";
356 print(MI.getOperand(I), TRI);
357 }
358
359 if (I)
360 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000361 if (MI.getFlag(MachineInstr::FrameSetup))
362 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000363 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000364 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000365 if (I < E)
366 OS << ' ';
367
368 bool NeedComma = false;
369 for (; I < E; ++I) {
370 if (NeedComma)
371 OS << ", ";
372 print(MI.getOperand(I), TRI);
373 NeedComma = true;
374 }
375}
376
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000377void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
378 OS << "%bb." << MBB.getNumber();
379 if (const auto *BB = MBB.getBasicBlock()) {
380 if (BB->hasName())
381 OS << '.' << BB->getName();
382 }
383}
384
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000385void MIPrinter::printStackObjectReference(int FrameIndex) {
386 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
387 assert(ObjectInfo != StackObjectOperandMapping.end() &&
388 "Invalid frame index");
389 const FrameIndexOperand &Operand = ObjectInfo->second;
390 if (Operand.IsFixed) {
391 OS << "%fixed-stack." << Operand.ID;
392 return;
393 }
394 OS << "%stack." << Operand.ID;
395 if (!Operand.Name.empty())
396 OS << '.' << Operand.Name;
397}
398
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000399void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
400 switch (Op.getType()) {
401 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000402 // TODO: Print the other register flags.
403 if (Op.isImplicit())
404 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000405 if (Op.isDead())
406 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000407 if (Op.isKill())
408 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000409 if (Op.isUndef())
410 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000411 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000412 // Print the sub register.
413 if (Op.getSubReg() != 0)
414 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000415 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000416 case MachineOperand::MO_Immediate:
417 OS << Op.getImm();
418 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000419 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000420 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000421 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000422 case MachineOperand::MO_FrameIndex:
423 printStackObjectReference(Op.getIndex());
424 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000425 case MachineOperand::MO_ConstantPoolIndex:
426 OS << "%const." << Op.getIndex();
427 // TODO: Print offset and target flags.
428 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000429 case MachineOperand::MO_JumpTableIndex:
430 OS << "%jump-table." << Op.getIndex();
431 // TODO: Print target flags.
432 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000433 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000434 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000435 // TODO: Print offset and target flags.
436 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000437 case MachineOperand::MO_RegisterMask: {
438 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
439 if (RegMaskInfo != RegisterMaskIds.end())
440 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
441 else
442 llvm_unreachable("Can't print this machine register mask yet.");
443 break;
444 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000445 default:
446 // TODO: Print the other machine operands.
447 llvm_unreachable("Can't print this machine operand at the moment");
448 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000449}
450
Alex Lorenz345c1442015-06-15 23:52:35 +0000451void llvm::printMIR(raw_ostream &OS, const Module &M) {
452 yaml::Output Out(OS);
453 Out << const_cast<Module &>(M);
454}
455
456void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
457 MIRPrinter Printer(OS);
458 Printer.print(MF);
459}