blob: da1ebe15c8bda48ada2957022fc4d77a39486352 [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 Lorenzf4baeb52015-07-21 22:28:27 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000022#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000023#include "llvm/IR/BasicBlock.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000024#include "llvm/IR/Instructions.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000025#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000026#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000027#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000028#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000033
34using namespace llvm;
35
36namespace {
37
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000038/// This structure describes how to print out stack object references.
39struct FrameIndexOperand {
40 std::string Name;
41 unsigned ID;
42 bool IsFixed;
43
44 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
45 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
46
47 /// Return an ordinary stack object reference.
48 static FrameIndexOperand create(StringRef Name, unsigned ID) {
49 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
50 }
51
52 /// Return a fixed stack object reference.
53 static FrameIndexOperand createFixed(unsigned ID) {
54 return FrameIndexOperand("", ID, /*IsFixed=*/true);
55 }
56};
57
Alex Lorenz345c1442015-06-15 23:52:35 +000058/// This class prints out the machine functions using the MIR serialization
59/// format.
60class MIRPrinter {
61 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000062 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000063 /// Maps from stack object indices to operand indices which will be used when
64 /// printing frame index machine operands.
65 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000066
67public:
68 MIRPrinter(raw_ostream &OS) : OS(OS) {}
69
70 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000071
Alex Lorenz28148ba2015-07-09 22:23:13 +000072 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
73 const TargetRegisterInfo *TRI);
Alex Lorenz60541c12015-07-09 19:55:27 +000074 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000075 void convert(yaml::MachineFunction &MF,
76 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000077 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
78 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000079 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000080 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000081 void convertStackObjects(yaml::MachineFunction &MF,
82 const MachineFrameInfo &MFI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000083
84private:
85 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000086};
87
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000088/// This class prints out the machine instructions using the MIR serialization
89/// format.
90class MIPrinter {
91 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000092 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000093 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000094 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095
96public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000097 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000098 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
99 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
100 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
101 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000102
103 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000104 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000105 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000106 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000107
108 void print(const MCCFIInstruction &CFI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000109};
110
Alex Lorenz345c1442015-06-15 23:52:35 +0000111} // end anonymous namespace
112
113namespace llvm {
114namespace yaml {
115
116/// This struct serializes the LLVM IR module.
117template <> struct BlockScalarTraits<Module> {
118 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
119 Mod.print(OS, nullptr);
120 }
121 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
122 llvm_unreachable("LLVM Module is supposed to be parsed separately");
123 return "";
124 }
125};
126
127} // end namespace yaml
128} // end namespace llvm
129
Alex Lorenz15a00a82015-07-14 21:18:25 +0000130static void printReg(unsigned Reg, raw_ostream &OS,
131 const TargetRegisterInfo *TRI) {
132 // TODO: Print Stack Slots.
133 if (!Reg)
134 OS << '_';
135 else if (TargetRegisterInfo::isVirtualRegister(Reg))
136 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
137 else if (Reg < TRI->getNumRegs())
138 OS << '%' << StringRef(TRI->getName(Reg)).lower();
139 else
140 llvm_unreachable("Can't print this kind of register yet");
141}
142
Alex Lorenz345c1442015-06-15 23:52:35 +0000143void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000144 initRegisterMaskIds(MF);
145
Alex Lorenz345c1442015-06-15 23:52:35 +0000146 yaml::MachineFunction YamlMF;
147 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000148 YamlMF.Alignment = MF.getAlignment();
149 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
150 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000151 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000152 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000153 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000154 if (const auto *ConstantPool = MF.getConstantPool())
155 convert(YamlMF, *ConstantPool);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000156
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000157 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000158 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
159 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
160 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000161 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000162 // TODO: Allow printing of non sequentially numbered MBBs.
163 // This is currently needed as the basic block references get their index
164 // from MBB.getNumber(), thus it should be sequential so that the parser can
165 // map back to the correct MBBs when parsing the output.
166 assert(MBB.getNumber() == I++ &&
167 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000168 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000169 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000170 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000171 YamlMF.BasicBlocks.push_back(YamlMBB);
172 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000173 yaml::Output Out(OS);
174 Out << YamlMF;
175}
176
Alex Lorenz54565cf2015-06-24 19:56:10 +0000177void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000178 const MachineRegisterInfo &RegInfo,
179 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000180 MF.IsSSA = RegInfo.isSSA();
181 MF.TracksRegLiveness = RegInfo.tracksLiveness();
182 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000183
184 // Print the virtual register definitions.
185 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
186 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
187 yaml::VirtualRegisterDefinition VReg;
188 VReg.ID = I;
189 VReg.Class =
190 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
191 MF.VirtualRegisters.push_back(VReg);
192 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000193}
194
Alex Lorenz60541c12015-07-09 19:55:27 +0000195void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
196 const MachineFrameInfo &MFI) {
197 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
198 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
199 YamlMFI.HasStackMap = MFI.hasStackMap();
200 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
201 YamlMFI.StackSize = MFI.getStackSize();
202 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
203 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
204 YamlMFI.AdjustsStack = MFI.adjustsStack();
205 YamlMFI.HasCalls = MFI.hasCalls();
206 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
207 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
208 YamlMFI.HasVAStart = MFI.hasVAStart();
209 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
210}
211
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000212void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
213 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000214 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000215 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000216 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
217 if (MFI.isDeadObjectIndex(I))
218 continue;
219
220 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000221 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000222 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
223 ? yaml::FixedMachineStackObject::SpillSlot
224 : yaml::FixedMachineStackObject::DefaultType;
225 YamlObject.Offset = MFI.getObjectOffset(I);
226 YamlObject.Size = MFI.getObjectSize(I);
227 YamlObject.Alignment = MFI.getObjectAlignment(I);
228 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
229 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
230 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000231 StackObjectOperandMapping.insert(
232 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000233 }
234
235 // Process ordinary stack objects.
236 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000237 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
238 if (MFI.isDeadObjectIndex(I))
239 continue;
240
241 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000242 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000243 if (const auto *Alloca = MFI.getObjectAllocation(I))
244 YamlObject.Name.Value =
245 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000246 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
247 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000248 : MFI.isVariableSizedObjectIndex(I)
249 ? yaml::MachineStackObject::VariableSized
250 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000251 YamlObject.Offset = MFI.getObjectOffset(I);
252 YamlObject.Size = MFI.getObjectSize(I);
253 YamlObject.Alignment = MFI.getObjectAlignment(I);
254
255 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000256 StackObjectOperandMapping.insert(std::make_pair(
257 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000258 }
259}
260
Alex Lorenzab980492015-07-20 20:51:18 +0000261void MIRPrinter::convert(yaml::MachineFunction &MF,
262 const MachineConstantPool &ConstantPool) {
263 unsigned ID = 0;
264 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
265 // TODO: Serialize target specific constant pool entries.
266 if (Constant.isMachineConstantPoolEntry())
267 llvm_unreachable("Can't print target specific constant pool entries yet");
268
269 yaml::MachineConstantPoolValue YamlConstant;
270 std::string Str;
271 raw_string_ostream StrOS(Str);
272 Constant.Val.ConstVal->printAsOperand(StrOS);
273 YamlConstant.ID = ID++;
274 YamlConstant.Value = StrOS.str();
275 YamlConstant.Alignment = Constant.getAlignment();
276 MF.Constants.push_back(YamlConstant);
277 }
278}
279
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000280void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000281 yaml::MachineJumpTable &YamlJTI,
282 const MachineJumpTableInfo &JTI) {
283 YamlJTI.Kind = JTI.getEntryKind();
284 unsigned ID = 0;
285 for (const auto &Table : JTI.getJumpTables()) {
286 std::string Str;
287 yaml::MachineJumpTable::Entry Entry;
288 Entry.ID = ID++;
289 for (const auto *MBB : Table.MBBs) {
290 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000291 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
292 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000293 Entry.Blocks.push_back(StrOS.str());
294 Str.clear();
295 }
296 YamlJTI.Entries.push_back(Entry);
297 }
298}
299
300void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000301 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000302 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000303 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
304 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000305 // TODO: Serialize unnamed BB references.
306 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000307 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000308 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000309 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000310 YamlMBB.Alignment = MBB.getAlignment();
311 YamlMBB.AddressTaken = MBB.hasAddressTaken();
312 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000313 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000314 std::string Str;
315 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000316 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
317 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000318 YamlMBB.Successors.push_back(StrOS.str());
319 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000320 // Print the live in registers.
321 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
322 assert(TRI && "Expected target register info");
323 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
324 std::string Str;
325 raw_string_ostream StrOS(Str);
326 printReg(*I, StrOS, TRI);
327 YamlMBB.LiveIns.push_back(StrOS.str());
328 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000329 // Print the machine instructions.
330 YamlMBB.Instructions.reserve(MBB.size());
331 std::string Str;
332 for (const auto &MI : MBB) {
333 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000334 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000335 YamlMBB.Instructions.push_back(StrOS.str());
336 Str.clear();
337 }
338}
339
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000340void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
341 const auto *TRI = MF.getSubtarget().getRegisterInfo();
342 unsigned I = 0;
343 for (const uint32_t *Mask : TRI->getRegMasks())
344 RegisterMaskIds.insert(std::make_pair(Mask, I++));
345}
346
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000347void MIPrinter::print(const MachineInstr &MI) {
348 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000349 const auto *TRI = SubTarget.getRegisterInfo();
350 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000351 const auto *TII = SubTarget.getInstrInfo();
352 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000353 if (MI.isCFIInstruction())
354 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000355
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000356 unsigned I = 0, E = MI.getNumOperands();
357 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
358 !MI.getOperand(I).isImplicit();
359 ++I) {
360 if (I)
361 OS << ", ";
362 print(MI.getOperand(I), TRI);
363 }
364
365 if (I)
366 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000367 if (MI.getFlag(MachineInstr::FrameSetup))
368 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000369 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000370 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000371 if (I < E)
372 OS << ' ';
373
374 bool NeedComma = false;
375 for (; I < E; ++I) {
376 if (NeedComma)
377 OS << ", ";
378 print(MI.getOperand(I), TRI);
379 NeedComma = true;
380 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000381
382 if (MI.getDebugLoc()) {
383 if (NeedComma)
384 OS << ',';
385 OS << " debug-location ";
386 MI.getDebugLoc()->printAsOperand(OS, MST);
387 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000388}
389
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000390void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
391 OS << "%bb." << MBB.getNumber();
392 if (const auto *BB = MBB.getBasicBlock()) {
393 if (BB->hasName())
394 OS << '.' << BB->getName();
395 }
396}
397
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000398void MIPrinter::printStackObjectReference(int FrameIndex) {
399 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
400 assert(ObjectInfo != StackObjectOperandMapping.end() &&
401 "Invalid frame index");
402 const FrameIndexOperand &Operand = ObjectInfo->second;
403 if (Operand.IsFixed) {
404 OS << "%fixed-stack." << Operand.ID;
405 return;
406 }
407 OS << "%stack." << Operand.ID;
408 if (!Operand.Name.empty())
409 OS << '.' << Operand.Name;
410}
411
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000412void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
413 switch (Op.getType()) {
414 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000415 // TODO: Print the other register flags.
416 if (Op.isImplicit())
417 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000418 if (Op.isDead())
419 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000420 if (Op.isKill())
421 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000422 if (Op.isUndef())
423 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000424 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000425 // Print the sub register.
426 if (Op.getSubReg() != 0)
427 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000428 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000429 case MachineOperand::MO_Immediate:
430 OS << Op.getImm();
431 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000432 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000433 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000434 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000435 case MachineOperand::MO_FrameIndex:
436 printStackObjectReference(Op.getIndex());
437 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000438 case MachineOperand::MO_ConstantPoolIndex:
439 OS << "%const." << Op.getIndex();
440 // TODO: Print offset and target flags.
441 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000442 case MachineOperand::MO_JumpTableIndex:
443 OS << "%jump-table." << Op.getIndex();
444 // TODO: Print target flags.
445 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000446 case MachineOperand::MO_ExternalSymbol:
447 OS << '$';
448 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
449 // TODO: Print the target flags.
450 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000451 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000452 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000453 // TODO: Print offset and target flags.
454 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000455 case MachineOperand::MO_RegisterMask: {
456 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
457 if (RegMaskInfo != RegisterMaskIds.end())
458 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
459 else
460 llvm_unreachable("Can't print this machine register mask yet.");
461 break;
462 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000463 case MachineOperand::MO_Metadata:
464 Op.getMetadata()->printAsOperand(OS, MST);
465 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000466 case MachineOperand::MO_CFIIndex: {
467 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
468 print(MMI.getFrameInstructions()[Op.getCFIIndex()]);
469 break;
470 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000471 default:
472 // TODO: Print the other machine operands.
473 llvm_unreachable("Can't print this machine operand at the moment");
474 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000475}
476
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000477void MIPrinter::print(const MCCFIInstruction &CFI) {
478 switch (CFI.getOperation()) {
479 case MCCFIInstruction::OpDefCfaOffset:
480 OS << ".cfi_def_cfa_offset ";
481 if (CFI.getLabel())
482 OS << "<mcsymbol> ";
483 OS << CFI.getOffset();
484 break;
485 default:
486 // TODO: Print the other CFI Operations.
487 OS << "<unserializable cfi operation>";
488 break;
489 }
490}
491
Alex Lorenz345c1442015-06-15 23:52:35 +0000492void llvm::printMIR(raw_ostream &OS, const Module &M) {
493 yaml::Output Out(OS);
494 Out << const_cast<Module &>(M);
495}
496
497void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
498 MIRPrinter Printer(OS);
499 Printer.print(MF);
500}