blob: 574a0ddee1bb86f6ac750734d0231024878625b0 [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
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000108 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
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 Lorenzab4cbcf2015-07-24 20:35:40 +0000143static void printReg(unsigned Reg, yaml::StringValue &Dest,
144 const TargetRegisterInfo *TRI) {
145 raw_string_ostream OS(Dest.Value);
146 printReg(Reg, OS, TRI);
147}
148
Alex Lorenz345c1442015-06-15 23:52:35 +0000149void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000150 initRegisterMaskIds(MF);
151
Alex Lorenz345c1442015-06-15 23:52:35 +0000152 yaml::MachineFunction YamlMF;
153 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000154 YamlMF.Alignment = MF.getAlignment();
155 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
156 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000157 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000158 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000159 convertStackObjects(YamlMF, *MF.getFrameInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000160 if (const auto *ConstantPool = MF.getConstantPool())
161 convert(YamlMF, *ConstantPool);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000162
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000163 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000164 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
165 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
166 int I = 0;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000167 for (const auto &MBB : MF) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000168 // TODO: Allow printing of non sequentially numbered MBBs.
169 // This is currently needed as the basic block references get their index
170 // from MBB.getNumber(), thus it should be sequential so that the parser can
171 // map back to the correct MBBs when parsing the output.
172 assert(MBB.getNumber() == I++ &&
173 "Can't print MBBs that aren't sequentially numbered");
Alex Lorenzec6b26b2015-06-26 17:07:27 +0000174 (void)I;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000175 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000176 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000177 YamlMF.BasicBlocks.push_back(YamlMBB);
178 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000179 yaml::Output Out(OS);
180 Out << YamlMF;
181}
182
Alex Lorenz54565cf2015-06-24 19:56:10 +0000183void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000184 const MachineRegisterInfo &RegInfo,
185 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000186 MF.IsSSA = RegInfo.isSSA();
187 MF.TracksRegLiveness = RegInfo.tracksLiveness();
188 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000189
190 // Print the virtual register definitions.
191 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
192 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
193 yaml::VirtualRegisterDefinition VReg;
194 VReg.ID = I;
195 VReg.Class =
196 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000197 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
198 if (PreferredReg)
199 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000200 MF.VirtualRegisters.push_back(VReg);
201 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000202}
203
Alex Lorenz60541c12015-07-09 19:55:27 +0000204void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
205 const MachineFrameInfo &MFI) {
206 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
207 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
208 YamlMFI.HasStackMap = MFI.hasStackMap();
209 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
210 YamlMFI.StackSize = MFI.getStackSize();
211 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
212 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
213 YamlMFI.AdjustsStack = MFI.adjustsStack();
214 YamlMFI.HasCalls = MFI.hasCalls();
215 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
216 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
217 YamlMFI.HasVAStart = MFI.hasVAStart();
218 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
219}
220
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000221void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
222 const MachineFrameInfo &MFI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000223 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000224 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000225 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
226 if (MFI.isDeadObjectIndex(I))
227 continue;
228
229 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000230 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000231 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
232 ? yaml::FixedMachineStackObject::SpillSlot
233 : yaml::FixedMachineStackObject::DefaultType;
234 YamlObject.Offset = MFI.getObjectOffset(I);
235 YamlObject.Size = MFI.getObjectSize(I);
236 YamlObject.Alignment = MFI.getObjectAlignment(I);
237 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
238 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
239 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000240 StackObjectOperandMapping.insert(
241 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000242 }
243
244 // Process ordinary stack objects.
245 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000246 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
247 if (MFI.isDeadObjectIndex(I))
248 continue;
249
250 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000251 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000252 if (const auto *Alloca = MFI.getObjectAllocation(I))
253 YamlObject.Name.Value =
254 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000255 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
256 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000257 : MFI.isVariableSizedObjectIndex(I)
258 ? yaml::MachineStackObject::VariableSized
259 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000260 YamlObject.Offset = MFI.getObjectOffset(I);
261 YamlObject.Size = MFI.getObjectSize(I);
262 YamlObject.Alignment = MFI.getObjectAlignment(I);
263
264 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000265 StackObjectOperandMapping.insert(std::make_pair(
266 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000267 }
268}
269
Alex Lorenzab980492015-07-20 20:51:18 +0000270void MIRPrinter::convert(yaml::MachineFunction &MF,
271 const MachineConstantPool &ConstantPool) {
272 unsigned ID = 0;
273 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
274 // TODO: Serialize target specific constant pool entries.
275 if (Constant.isMachineConstantPoolEntry())
276 llvm_unreachable("Can't print target specific constant pool entries yet");
277
278 yaml::MachineConstantPoolValue YamlConstant;
279 std::string Str;
280 raw_string_ostream StrOS(Str);
281 Constant.Val.ConstVal->printAsOperand(StrOS);
282 YamlConstant.ID = ID++;
283 YamlConstant.Value = StrOS.str();
284 YamlConstant.Alignment = Constant.getAlignment();
285 MF.Constants.push_back(YamlConstant);
286 }
287}
288
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000289void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000290 yaml::MachineJumpTable &YamlJTI,
291 const MachineJumpTableInfo &JTI) {
292 YamlJTI.Kind = JTI.getEntryKind();
293 unsigned ID = 0;
294 for (const auto &Table : JTI.getJumpTables()) {
295 std::string Str;
296 yaml::MachineJumpTable::Entry Entry;
297 Entry.ID = ID++;
298 for (const auto *MBB : Table.MBBs) {
299 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000300 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
301 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000302 Entry.Blocks.push_back(StrOS.str());
303 Str.clear();
304 }
305 YamlJTI.Entries.push_back(Entry);
306 }
307}
308
309void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000310 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000311 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000312 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
313 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000314 // TODO: Serialize unnamed BB references.
315 if (const auto *BB = MBB.getBasicBlock())
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000316 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000317 else
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000318 YamlMBB.Name.Value = "";
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000319 YamlMBB.Alignment = MBB.getAlignment();
320 YamlMBB.AddressTaken = MBB.hasAddressTaken();
321 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000322 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000323 std::string Str;
324 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000325 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
326 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000327 YamlMBB.Successors.push_back(StrOS.str());
328 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000329 // Print the live in registers.
330 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
331 assert(TRI && "Expected target register info");
332 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
333 std::string Str;
334 raw_string_ostream StrOS(Str);
335 printReg(*I, StrOS, TRI);
336 YamlMBB.LiveIns.push_back(StrOS.str());
337 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000338 // Print the machine instructions.
339 YamlMBB.Instructions.reserve(MBB.size());
340 std::string Str;
341 for (const auto &MI : MBB) {
342 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000343 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000344 YamlMBB.Instructions.push_back(StrOS.str());
345 Str.clear();
346 }
347}
348
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000349void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
350 const auto *TRI = MF.getSubtarget().getRegisterInfo();
351 unsigned I = 0;
352 for (const uint32_t *Mask : TRI->getRegMasks())
353 RegisterMaskIds.insert(std::make_pair(Mask, I++));
354}
355
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000356void MIPrinter::print(const MachineInstr &MI) {
357 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000358 const auto *TRI = SubTarget.getRegisterInfo();
359 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000360 const auto *TII = SubTarget.getInstrInfo();
361 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000362 if (MI.isCFIInstruction())
363 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000364
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000365 unsigned I = 0, E = MI.getNumOperands();
366 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
367 !MI.getOperand(I).isImplicit();
368 ++I) {
369 if (I)
370 OS << ", ";
371 print(MI.getOperand(I), TRI);
372 }
373
374 if (I)
375 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000376 if (MI.getFlag(MachineInstr::FrameSetup))
377 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000378 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000379 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000380 if (I < E)
381 OS << ' ';
382
383 bool NeedComma = false;
384 for (; I < E; ++I) {
385 if (NeedComma)
386 OS << ", ";
387 print(MI.getOperand(I), TRI);
388 NeedComma = true;
389 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000390
391 if (MI.getDebugLoc()) {
392 if (NeedComma)
393 OS << ',';
394 OS << " debug-location ";
395 MI.getDebugLoc()->printAsOperand(OS, MST);
396 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000397}
398
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000399void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
400 OS << "%bb." << MBB.getNumber();
401 if (const auto *BB = MBB.getBasicBlock()) {
402 if (BB->hasName())
403 OS << '.' << BB->getName();
404 }
405}
406
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000407void MIPrinter::printStackObjectReference(int FrameIndex) {
408 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
409 assert(ObjectInfo != StackObjectOperandMapping.end() &&
410 "Invalid frame index");
411 const FrameIndexOperand &Operand = ObjectInfo->second;
412 if (Operand.IsFixed) {
413 OS << "%fixed-stack." << Operand.ID;
414 return;
415 }
416 OS << "%stack." << Operand.ID;
417 if (!Operand.Name.empty())
418 OS << '.' << Operand.Name;
419}
420
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000421void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
422 switch (Op.getType()) {
423 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000424 // TODO: Print the other register flags.
425 if (Op.isImplicit())
426 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000427 if (Op.isDead())
428 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000429 if (Op.isKill())
430 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000431 if (Op.isUndef())
432 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000433 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000434 // Print the sub register.
435 if (Op.getSubReg() != 0)
436 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000437 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000438 case MachineOperand::MO_Immediate:
439 OS << Op.getImm();
440 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000441 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000442 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000443 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000444 case MachineOperand::MO_FrameIndex:
445 printStackObjectReference(Op.getIndex());
446 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000447 case MachineOperand::MO_ConstantPoolIndex:
448 OS << "%const." << Op.getIndex();
449 // TODO: Print offset and target flags.
450 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000451 case MachineOperand::MO_JumpTableIndex:
452 OS << "%jump-table." << Op.getIndex();
453 // TODO: Print target flags.
454 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000455 case MachineOperand::MO_ExternalSymbol:
456 OS << '$';
457 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
458 // TODO: Print the target flags.
459 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000460 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000461 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000462 // TODO: Print offset and target flags.
463 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000464 case MachineOperand::MO_RegisterMask: {
465 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
466 if (RegMaskInfo != RegisterMaskIds.end())
467 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
468 else
469 llvm_unreachable("Can't print this machine register mask yet.");
470 break;
471 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000472 case MachineOperand::MO_Metadata:
473 Op.getMetadata()->printAsOperand(OS, MST);
474 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000475 case MachineOperand::MO_CFIIndex: {
476 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000477 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000478 break;
479 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000480 default:
481 // TODO: Print the other machine operands.
482 llvm_unreachable("Can't print this machine operand at the moment");
483 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000484}
485
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000486static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
487 const TargetRegisterInfo *TRI) {
488 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
489 if (Reg == -1) {
490 OS << "<badreg>";
491 return;
492 }
493 printReg(Reg, OS, TRI);
494}
495
496void MIPrinter::print(const MCCFIInstruction &CFI,
497 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000498 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000499 case MCCFIInstruction::OpOffset:
500 OS << ".cfi_offset ";
501 if (CFI.getLabel())
502 OS << "<mcsymbol> ";
503 printCFIRegister(CFI.getRegister(), OS, TRI);
504 OS << ", " << CFI.getOffset();
505 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000506 case MCCFIInstruction::OpDefCfaOffset:
507 OS << ".cfi_def_cfa_offset ";
508 if (CFI.getLabel())
509 OS << "<mcsymbol> ";
510 OS << CFI.getOffset();
511 break;
512 default:
513 // TODO: Print the other CFI Operations.
514 OS << "<unserializable cfi operation>";
515 break;
516 }
517}
518
Alex Lorenz345c1442015-06-15 23:52:35 +0000519void llvm::printMIR(raw_ostream &OS, const Module &M) {
520 yaml::Output Out(OS);
521 Out << const_cast<Module &>(M);
522}
523
524void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
525 MIRPrinter Printer(OS);
526 Printer.print(MF);
527}