blob: 9413a7e381187fa1ed2b469bbc2ef185cc7d09b5 [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 Lorenz6ede3742015-07-21 16:59:53 +000024#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000025#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000026#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000032
33using namespace llvm;
34
35namespace {
36
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000037/// This structure describes how to print out stack object references.
38struct FrameIndexOperand {
39 std::string Name;
40 unsigned ID;
41 bool IsFixed;
42
43 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
44 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
45
46 /// Return an ordinary stack object reference.
47 static FrameIndexOperand create(StringRef Name, unsigned ID) {
48 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
49 }
50
51 /// Return a fixed stack object reference.
52 static FrameIndexOperand createFixed(unsigned ID) {
53 return FrameIndexOperand("", ID, /*IsFixed=*/true);
54 }
55};
56
Alex Lorenz345c1442015-06-15 23:52:35 +000057/// This class prints out the machine functions using the MIR serialization
58/// format.
59class MIRPrinter {
60 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000061 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000062 /// Maps from stack object indices to operand indices which will be used when
63 /// printing frame index machine operands.
64 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000065
66public:
67 MIRPrinter(raw_ostream &OS) : OS(OS) {}
68
69 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000070
Alex Lorenz28148ba2015-07-09 22:23:13 +000071 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
72 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000073 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000074 void convert(yaml::MachineFunction &MF,
75 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000076 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
77 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000078 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000079 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000080 void convertStackObjects(yaml::MachineFunction &MF,
81 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000082
83private:
84 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000085};
86
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000087/// This class prints out the machine instructions using the MIR serialization
88/// format.
89class MIPrinter {
90 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000091 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000092 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000093 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000094
95public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000096 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000097 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
98 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
99 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
100 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000101
102 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000103 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000104 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000105 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106};
107
Alex Lorenz345c1442015-06-15 23:52:35 +0000108} // end anonymous namespace
109
110namespace llvm {
111namespace yaml {
112
113/// This struct serializes the LLVM IR module.
114template <> struct BlockScalarTraits<Module> {
115 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
116 Mod.print(OS, nullptr);
117 }
118 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
119 llvm_unreachable("LLVM Module is supposed to be parsed separately");
120 return "";
121 }
122};
123
124} // end namespace yaml
125} // end namespace llvm
126
Alex Lorenz15a00a82015-07-14 21:18:25 +0000127static void printReg(unsigned Reg, raw_ostream &OS,
128 const TargetRegisterInfo *TRI) {
129 // TODO: Print Stack Slots.
130 if (!Reg)
131 OS << '_';
132 else if (TargetRegisterInfo::isVirtualRegister(Reg))
133 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
134 else if (Reg < TRI->getNumRegs())
135 OS << '%' << StringRef(TRI->getName(Reg)).lower();
136 else
137 llvm_unreachable("Can't print this kind of register yet");
138}
139
Alex Lorenz345c1442015-06-15 23:52:35 +0000140void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000141 initRegisterMaskIds(MF);
142
Alex Lorenz345c1442015-06-15 23:52:35 +0000143 yaml::MachineFunction YamlMF;
144 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000145 YamlMF.Alignment = MF.getAlignment();
146 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
147 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000148 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000149 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000150 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000151 if (const auto *ConstantPool = MF.getConstantPool())
152 convert(YamlMF, *ConstantPool);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000153
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000154 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000155 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
156 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
157 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000158 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000159 // TODO: Allow printing of non sequentially numbered MBBs.
160 // This is currently needed as the basic block references get their index
161 // from MBB.getNumber(), thus it should be sequential so that the parser can
162 // map back to the correct MBBs when parsing the output.
163 assert(MBB.getNumber() == I++ &&
164 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000165 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000166 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000167 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000168 YamlMF.BasicBlocks.push_back(YamlMBB);
169 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000170 yaml::Output Out(OS);
171 Out << YamlMF;
172}
173
Alex Lorenz54565cf2015-06-24 19:56:10 +0000174void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000175 const MachineRegisterInfo &RegInfo,
176 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000177 MF.IsSSA = RegInfo.isSSA();
178 MF.TracksRegLiveness = RegInfo.tracksLiveness();
179 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000180
181 // Print the virtual register definitions.
182 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
183 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
184 yaml::VirtualRegisterDefinition VReg;
185 VReg.ID = I;
186 VReg.Class =
187 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
188 MF.VirtualRegisters.push_back(VReg);
189 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000190}
191
Alex Lorenz60541c12015-07-09 19:55:27 +0000192void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
193 const MachineFrameInfo &MFI) {
194 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
195 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
196 YamlMFI.HasStackMap = MFI.hasStackMap();
197 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
198 YamlMFI.StackSize = MFI.getStackSize();
199 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
200 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
201 YamlMFI.AdjustsStack = MFI.adjustsStack();
202 YamlMFI.HasCalls = MFI.hasCalls();
203 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
204 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
205 YamlMFI.HasVAStart = MFI.hasVAStart();
206 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
207}
208
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000209void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
210 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000211 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000212 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000213 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
214 if (MFI.isDeadObjectIndex(I))
215 continue;
216
217 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000218 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000219 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
220 ? yaml::FixedMachineStackObject::SpillSlot
221 : yaml::FixedMachineStackObject::DefaultType;
222 YamlObject.Offset = MFI.getObjectOffset(I);
223 YamlObject.Size = MFI.getObjectSize(I);
224 YamlObject.Alignment = MFI.getObjectAlignment(I);
225 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
226 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
227 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000228 StackObjectOperandMapping.insert(
229 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000230 }
231
232 // Process ordinary stack objects.
233 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000234 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
235 if (MFI.isDeadObjectIndex(I))
236 continue;
237
238 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000239 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000240 if (const auto *Alloca = MFI.getObjectAllocation(I))
241 YamlObject.Name.Value =
242 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000243 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
244 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000245 : MFI.isVariableSizedObjectIndex(I)
246 ? yaml::MachineStackObject::VariableSized
247 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000248 YamlObject.Offset = MFI.getObjectOffset(I);
249 YamlObject.Size = MFI.getObjectSize(I);
250 YamlObject.Alignment = MFI.getObjectAlignment(I);
251
252 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000253 StackObjectOperandMapping.insert(std::make_pair(
254 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000255 }
256}
257
Alex Lorenzab980492015-07-20 20:51:18 +0000258void MIRPrinter::convert(yaml::MachineFunction &MF,
259 const MachineConstantPool &ConstantPool) {
260 unsigned ID = 0;
261 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
262 // TODO: Serialize target specific constant pool entries.
263 if (Constant.isMachineConstantPoolEntry())
264 llvm_unreachable("Can't print target specific constant pool entries yet");
265
266 yaml::MachineConstantPoolValue YamlConstant;
267 std::string Str;
268 raw_string_ostream StrOS(Str);
269 Constant.Val.ConstVal->printAsOperand(StrOS);
270 YamlConstant.ID = ID++;
271 YamlConstant.Value = StrOS.str();
272 YamlConstant.Alignment = Constant.getAlignment();
273 MF.Constants.push_back(YamlConstant);
274 }
275}
276
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000277void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000278 yaml::MachineJumpTable &YamlJTI,
279 const MachineJumpTableInfo &JTI) {
280 YamlJTI.Kind = JTI.getEntryKind();
281 unsigned ID = 0;
282 for (const auto &Table : JTI.getJumpTables()) {
283 std::string Str;
284 yaml::MachineJumpTable::Entry Entry;
285 Entry.ID = ID++;
286 for (const auto *MBB : Table.MBBs) {
287 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000288 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
289 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000290 Entry.Blocks.push_back(StrOS.str());
291 Str.clear();
292 }
293 YamlJTI.Entries.push_back(Entry);
294 }
295}
296
297void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000298 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000299 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000300 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
301 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000302 // TODO: Serialize unnamed BB references.
303 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000304 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000305 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000306 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000307 YamlMBB.Alignment = MBB.getAlignment();
308 YamlMBB.AddressTaken = MBB.hasAddressTaken();
309 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000310 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000311 std::string Str;
312 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000313 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
314 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000315 YamlMBB.Successors.push_back(StrOS.str());
316 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000317 // Print the live in registers.
318 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
319 assert(TRI && "Expected target register info");
320 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
321 std::string Str;
322 raw_string_ostream StrOS(Str);
323 printReg(*I, StrOS, TRI);
324 YamlMBB.LiveIns.push_back(StrOS.str());
325 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000326 // Print the machine instructions.
327 YamlMBB.Instructions.reserve(MBB.size());
328 std::string Str;
329 for (const auto &MI : MBB) {
330 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000331 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000332 YamlMBB.Instructions.push_back(StrOS.str());
333 Str.clear();
334 }
335}
336
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000337void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
338 const auto *TRI = MF.getSubtarget().getRegisterInfo();
339 unsigned I = 0;
340 for (const uint32_t *Mask : TRI->getRegMasks())
341 RegisterMaskIds.insert(std::make_pair(Mask, I++));
342}
343
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000344void MIPrinter::print(const MachineInstr &MI) {
345 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000346 const auto *TRI = SubTarget.getRegisterInfo();
347 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000348 const auto *TII = SubTarget.getInstrInfo();
349 assert(TII && "Expected target instruction info");
350
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000351 unsigned I = 0, E = MI.getNumOperands();
352 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
353 !MI.getOperand(I).isImplicit();
354 ++I) {
355 if (I)
356 OS << ", ";
357 print(MI.getOperand(I), TRI);
358 }
359
360 if (I)
361 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000362 if (MI.getFlag(MachineInstr::FrameSetup))
363 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000364 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000365 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000366 if (I < E)
367 OS << ' ';
368
369 bool NeedComma = false;
370 for (; I < E; ++I) {
371 if (NeedComma)
372 OS << ", ";
373 print(MI.getOperand(I), TRI);
374 NeedComma = true;
375 }
376}
377
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000378void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
379 OS << "%bb." << MBB.getNumber();
380 if (const auto *BB = MBB.getBasicBlock()) {
381 if (BB->hasName())
382 OS << '.' << BB->getName();
383 }
384}
385
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000386void MIPrinter::printStackObjectReference(int FrameIndex) {
387 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
388 assert(ObjectInfo != StackObjectOperandMapping.end() &&
389 "Invalid frame index");
390 const FrameIndexOperand &Operand = ObjectInfo->second;
391 if (Operand.IsFixed) {
392 OS << "%fixed-stack." << Operand.ID;
393 return;
394 }
395 OS << "%stack." << Operand.ID;
396 if (!Operand.Name.empty())
397 OS << '.' << Operand.Name;
398}
399
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000400void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
401 switch (Op.getType()) {
402 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000403 // TODO: Print the other register flags.
404 if (Op.isImplicit())
405 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000406 if (Op.isDead())
407 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000408 if (Op.isKill())
409 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000410 if (Op.isUndef())
411 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000412 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000413 // Print the sub register.
414 if (Op.getSubReg() != 0)
415 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000416 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000417 case MachineOperand::MO_Immediate:
418 OS << Op.getImm();
419 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000420 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000421 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000422 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000423 case MachineOperand::MO_FrameIndex:
424 printStackObjectReference(Op.getIndex());
425 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000426 case MachineOperand::MO_ConstantPoolIndex:
427 OS << "%const." << Op.getIndex();
428 // TODO: Print offset and target flags.
429 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000430 case MachineOperand::MO_JumpTableIndex:
431 OS << "%jump-table." << Op.getIndex();
432 // TODO: Print target flags.
433 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000434 case MachineOperand::MO_ExternalSymbol:
435 OS << '$';
436 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
437 // TODO: Print the target flags.
438 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000439 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000440 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000441 // TODO: Print offset and target flags.
442 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000443 case MachineOperand::MO_RegisterMask: {
444 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
445 if (RegMaskInfo != RegisterMaskIds.end())
446 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
447 else
448 llvm_unreachable("Can't print this machine register mask yet.");
449 break;
450 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000451 default:
452 // TODO: Print the other machine operands.
453 llvm_unreachable("Can't print this machine operand at the moment");
454 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000455}
456
Alex Lorenz345c1442015-06-15 23:52:35 +0000457void llvm::printMIR(raw_ostream &OS, const Module &M) {
458 yaml::Output Out(OS);
459 Out << const_cast<Module &>(M);
460}
461
462void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
463 MIRPrinter Printer(OS);
464 Printer.print(MF);
465}