blob: f34cef7bcb488789f106c7e5184d4e4d8f2b413d [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 Lorenzdeb53492015-07-28 17:28:03 +000024#include "llvm/IR/Constants.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000025#include "llvm/IR/Instructions.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000026#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000027#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000028#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000034
35using namespace llvm;
36
37namespace {
38
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000039/// This structure describes how to print out stack object references.
40struct FrameIndexOperand {
41 std::string Name;
42 unsigned ID;
43 bool IsFixed;
44
45 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
46 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
47
48 /// Return an ordinary stack object reference.
49 static FrameIndexOperand create(StringRef Name, unsigned ID) {
50 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
51 }
52
53 /// Return a fixed stack object reference.
54 static FrameIndexOperand createFixed(unsigned ID) {
55 return FrameIndexOperand("", ID, /*IsFixed=*/true);
56 }
57};
58
Alex Lorenz618b2832015-07-30 16:54:38 +000059} // end anonymous namespace
60
61namespace llvm {
62
Alex Lorenz345c1442015-06-15 23:52:35 +000063/// This class prints out the machine functions using the MIR serialization
64/// format.
65class MIRPrinter {
66 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000067 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000068 /// Maps from stack object indices to operand indices which will be used when
69 /// printing frame index machine operands.
70 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000071
72public:
73 MIRPrinter(raw_ostream &OS) : OS(OS) {}
74
75 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000076
Alex Lorenz28148ba2015-07-09 22:23:13 +000077 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
78 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000079 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
80 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000081 void convert(yaml::MachineFunction &MF,
82 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000083 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
84 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000085 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000086 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000087 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000088 const MachineFrameInfo &MFI,
89 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000090
91private:
92 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000093};
94
Alex Lorenz618b2832015-07-30 16:54:38 +000095} // end namespace llvm
96
97namespace {
98
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000099/// This class prints out the machine instructions using the MIR serialization
100/// format.
101class MIPrinter {
102 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000103 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000104 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000105 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106
107public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000108 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000109 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
110 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
111 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
112 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113
114 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000115 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000116 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000117 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000118 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000119
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000120 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000121};
122
Alex Lorenz345c1442015-06-15 23:52:35 +0000123} // end anonymous namespace
124
125namespace llvm {
126namespace yaml {
127
128/// This struct serializes the LLVM IR module.
129template <> struct BlockScalarTraits<Module> {
130 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
131 Mod.print(OS, nullptr);
132 }
133 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
134 llvm_unreachable("LLVM Module is supposed to be parsed separately");
135 return "";
136 }
137};
138
139} // end namespace yaml
140} // end namespace llvm
141
Alex Lorenz15a00a82015-07-14 21:18:25 +0000142static void printReg(unsigned Reg, raw_ostream &OS,
143 const TargetRegisterInfo *TRI) {
144 // TODO: Print Stack Slots.
145 if (!Reg)
146 OS << '_';
147 else if (TargetRegisterInfo::isVirtualRegister(Reg))
148 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
149 else if (Reg < TRI->getNumRegs())
150 OS << '%' << StringRef(TRI->getName(Reg)).lower();
151 else
152 llvm_unreachable("Can't print this kind of register yet");
153}
154
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000155static void printReg(unsigned Reg, yaml::StringValue &Dest,
156 const TargetRegisterInfo *TRI) {
157 raw_string_ostream OS(Dest.Value);
158 printReg(Reg, OS, TRI);
159}
160
Alex Lorenz345c1442015-06-15 23:52:35 +0000161void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000162 initRegisterMaskIds(MF);
163
Alex Lorenz345c1442015-06-15 23:52:35 +0000164 yaml::MachineFunction YamlMF;
165 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000166 YamlMF.Alignment = MF.getAlignment();
167 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
168 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000169 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000170 ModuleSlotTracker MST(MF.getFunction()->getParent());
171 MST.incorporateFunction(*MF.getFunction());
172 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000173 convertStackObjects(YamlMF, *MF.getFrameInfo(),
174 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000175 if (const auto *ConstantPool = MF.getConstantPool())
176 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000177 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
178 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000179 for (const auto &MBB : MF) {
180 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000181 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000182 YamlMF.BasicBlocks.push_back(YamlMBB);
183 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000184 yaml::Output Out(OS);
185 Out << YamlMF;
186}
187
Alex Lorenz54565cf2015-06-24 19:56:10 +0000188void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000189 const MachineRegisterInfo &RegInfo,
190 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000191 MF.IsSSA = RegInfo.isSSA();
192 MF.TracksRegLiveness = RegInfo.tracksLiveness();
193 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000194
195 // Print the virtual register definitions.
196 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
197 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
198 yaml::VirtualRegisterDefinition VReg;
199 VReg.ID = I;
200 VReg.Class =
201 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000202 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
203 if (PreferredReg)
204 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000205 MF.VirtualRegisters.push_back(VReg);
206 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000207
208 // Print the live ins.
209 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
210 yaml::MachineFunctionLiveIn LiveIn;
211 printReg(I->first, LiveIn.Register, TRI);
212 if (I->second)
213 printReg(I->second, LiveIn.VirtualRegister, TRI);
214 MF.LiveIns.push_back(LiveIn);
215 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000216}
217
Alex Lorenza6f9a372015-07-29 21:09:09 +0000218void MIRPrinter::convert(ModuleSlotTracker &MST,
219 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000220 const MachineFrameInfo &MFI) {
221 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
222 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
223 YamlMFI.HasStackMap = MFI.hasStackMap();
224 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
225 YamlMFI.StackSize = MFI.getStackSize();
226 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
227 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
228 YamlMFI.AdjustsStack = MFI.adjustsStack();
229 YamlMFI.HasCalls = MFI.hasCalls();
230 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
231 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
232 YamlMFI.HasVAStart = MFI.hasVAStart();
233 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000234 if (MFI.getSavePoint()) {
235 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
236 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
237 .printMBBReference(*MFI.getSavePoint());
238 }
239 if (MFI.getRestorePoint()) {
240 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
241 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
242 .printMBBReference(*MFI.getRestorePoint());
243 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000244}
245
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000246void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000247 const MachineFrameInfo &MFI,
248 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000249 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000250 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000251 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
252 if (MFI.isDeadObjectIndex(I))
253 continue;
254
255 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000256 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000257 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
258 ? yaml::FixedMachineStackObject::SpillSlot
259 : yaml::FixedMachineStackObject::DefaultType;
260 YamlObject.Offset = MFI.getObjectOffset(I);
261 YamlObject.Size = MFI.getObjectSize(I);
262 YamlObject.Alignment = MFI.getObjectAlignment(I);
263 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
264 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
265 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000266 StackObjectOperandMapping.insert(
267 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000268 }
269
270 // Process ordinary stack objects.
271 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000272 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
273 if (MFI.isDeadObjectIndex(I))
274 continue;
275
276 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000277 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000278 if (const auto *Alloca = MFI.getObjectAllocation(I))
279 YamlObject.Name.Value =
280 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000281 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
282 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000283 : MFI.isVariableSizedObjectIndex(I)
284 ? yaml::MachineStackObject::VariableSized
285 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000286 YamlObject.Offset = MFI.getObjectOffset(I);
287 YamlObject.Size = MFI.getObjectSize(I);
288 YamlObject.Alignment = MFI.getObjectAlignment(I);
289
290 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000291 StackObjectOperandMapping.insert(std::make_pair(
292 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000293 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000294
295 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
296 yaml::StringValue Reg;
297 printReg(CSInfo.getReg(), Reg, TRI);
298 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
299 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
300 "Invalid stack object index");
301 const FrameIndexOperand &StackObject = StackObjectInfo->second;
302 if (StackObject.IsFixed)
303 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
304 else
305 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
306 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000307}
308
Alex Lorenzab980492015-07-20 20:51:18 +0000309void MIRPrinter::convert(yaml::MachineFunction &MF,
310 const MachineConstantPool &ConstantPool) {
311 unsigned ID = 0;
312 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
313 // TODO: Serialize target specific constant pool entries.
314 if (Constant.isMachineConstantPoolEntry())
315 llvm_unreachable("Can't print target specific constant pool entries yet");
316
317 yaml::MachineConstantPoolValue YamlConstant;
318 std::string Str;
319 raw_string_ostream StrOS(Str);
320 Constant.Val.ConstVal->printAsOperand(StrOS);
321 YamlConstant.ID = ID++;
322 YamlConstant.Value = StrOS.str();
323 YamlConstant.Alignment = Constant.getAlignment();
324 MF.Constants.push_back(YamlConstant);
325 }
326}
327
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000328void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000329 yaml::MachineJumpTable &YamlJTI,
330 const MachineJumpTableInfo &JTI) {
331 YamlJTI.Kind = JTI.getEntryKind();
332 unsigned ID = 0;
333 for (const auto &Table : JTI.getJumpTables()) {
334 std::string Str;
335 yaml::MachineJumpTable::Entry Entry;
336 Entry.ID = ID++;
337 for (const auto *MBB : Table.MBBs) {
338 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000339 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
340 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000341 Entry.Blocks.push_back(StrOS.str());
342 Str.clear();
343 }
344 YamlJTI.Entries.push_back(Entry);
345 }
346}
347
348void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000349 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000350 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000351 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
352 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000353 if (const auto *BB = MBB.getBasicBlock()) {
354 if (BB->hasName()) {
355 YamlMBB.Name.Value = BB->getName();
356 } else {
357 int Slot = MST.getLocalSlot(BB);
358 if (Slot == -1)
359 YamlMBB.IRBlock.Value = "<badref>";
360 else
361 YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str();
362 }
363 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000364 YamlMBB.Alignment = MBB.getAlignment();
365 YamlMBB.AddressTaken = MBB.hasAddressTaken();
366 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000367 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000368 std::string Str;
369 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000370 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
371 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000372 YamlMBB.Successors.push_back(StrOS.str());
373 }
Alex Lorenz618b2832015-07-30 16:54:38 +0000374 if (MBB.hasSuccessorWeights()) {
375 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I)
376 YamlMBB.SuccessorWeights.push_back(
377 yaml::UnsignedValue(MBB.getSuccWeight(I)));
378 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000379 // Print the live in registers.
380 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
381 assert(TRI && "Expected target register info");
382 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
383 std::string Str;
384 raw_string_ostream StrOS(Str);
385 printReg(*I, StrOS, TRI);
386 YamlMBB.LiveIns.push_back(StrOS.str());
387 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000388 // Print the machine instructions.
389 YamlMBB.Instructions.reserve(MBB.size());
390 std::string Str;
391 for (const auto &MI : MBB) {
392 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000393 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000394 YamlMBB.Instructions.push_back(StrOS.str());
395 Str.clear();
396 }
397}
398
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000399void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
400 const auto *TRI = MF.getSubtarget().getRegisterInfo();
401 unsigned I = 0;
402 for (const uint32_t *Mask : TRI->getRegMasks())
403 RegisterMaskIds.insert(std::make_pair(Mask, I++));
404}
405
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000406void MIPrinter::print(const MachineInstr &MI) {
407 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000408 const auto *TRI = SubTarget.getRegisterInfo();
409 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000410 const auto *TII = SubTarget.getInstrInfo();
411 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000412 if (MI.isCFIInstruction())
413 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000414
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000415 unsigned I = 0, E = MI.getNumOperands();
416 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
417 !MI.getOperand(I).isImplicit();
418 ++I) {
419 if (I)
420 OS << ", ";
421 print(MI.getOperand(I), TRI);
422 }
423
424 if (I)
425 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000426 if (MI.getFlag(MachineInstr::FrameSetup))
427 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000428 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000429 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000430 if (I < E)
431 OS << ' ';
432
433 bool NeedComma = false;
434 for (; I < E; ++I) {
435 if (NeedComma)
436 OS << ", ";
437 print(MI.getOperand(I), TRI);
438 NeedComma = true;
439 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000440
441 if (MI.getDebugLoc()) {
442 if (NeedComma)
443 OS << ',';
444 OS << " debug-location ";
445 MI.getDebugLoc()->printAsOperand(OS, MST);
446 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000447}
448
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000449void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
450 OS << "%bb." << MBB.getNumber();
451 if (const auto *BB = MBB.getBasicBlock()) {
452 if (BB->hasName())
453 OS << '.' << BB->getName();
454 }
455}
456
Alex Lorenzdeb53492015-07-28 17:28:03 +0000457void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
458 OS << "%ir-block.";
459 if (BB.hasName()) {
460 printLLVMNameWithoutPrefix(OS, BB.getName());
461 return;
462 }
463 int Slot = MST.getLocalSlot(&BB);
464 if (Slot == -1)
465 OS << "<badref>";
466 else
467 OS << Slot;
468}
469
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000470void MIPrinter::printStackObjectReference(int FrameIndex) {
471 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
472 assert(ObjectInfo != StackObjectOperandMapping.end() &&
473 "Invalid frame index");
474 const FrameIndexOperand &Operand = ObjectInfo->second;
475 if (Operand.IsFixed) {
476 OS << "%fixed-stack." << Operand.ID;
477 return;
478 }
479 OS << "%stack." << Operand.ID;
480 if (!Operand.Name.empty())
481 OS << '.' << Operand.Name;
482}
483
Alex Lorenzef5c1962015-07-28 23:02:45 +0000484static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
485 const auto *TII = MF.getSubtarget().getInstrInfo();
486 assert(TII && "expected instruction info");
487 auto Indices = TII->getSerializableTargetIndices();
488 for (const auto &I : Indices) {
489 if (I.first == Index) {
490 return I.second;
491 }
492 }
493 return nullptr;
494}
495
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000496void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
497 switch (Op.getType()) {
498 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000499 // TODO: Print the other register flags.
500 if (Op.isImplicit())
501 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000502 if (Op.isDead())
503 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000504 if (Op.isKill())
505 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000506 if (Op.isUndef())
507 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000508 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000509 // Print the sub register.
510 if (Op.getSubReg() != 0)
511 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000512 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000513 case MachineOperand::MO_Immediate:
514 OS << Op.getImm();
515 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000516 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000517 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000518 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000519 case MachineOperand::MO_FrameIndex:
520 printStackObjectReference(Op.getIndex());
521 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000522 case MachineOperand::MO_ConstantPoolIndex:
523 OS << "%const." << Op.getIndex();
524 // TODO: Print offset and target flags.
525 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000526 case MachineOperand::MO_TargetIndex: {
527 OS << "target-index(";
528 if (const auto *Name = getTargetIndexName(
529 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
530 OS << Name;
531 else
532 OS << "<unknown>";
533 OS << ')';
534 // TODO: Print the offset and target flags.
535 break;
536 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000537 case MachineOperand::MO_JumpTableIndex:
538 OS << "%jump-table." << Op.getIndex();
539 // TODO: Print target flags.
540 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000541 case MachineOperand::MO_ExternalSymbol:
542 OS << '$';
543 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
544 // TODO: Print the target flags.
545 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000546 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000547 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000548 // TODO: Print offset and target flags.
549 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000550 case MachineOperand::MO_BlockAddress:
551 OS << "blockaddress(";
552 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
553 MST);
554 OS << ", ";
555 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
556 OS << ')';
557 // TODO: Print offset and target flags.
558 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000559 case MachineOperand::MO_RegisterMask: {
560 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
561 if (RegMaskInfo != RegisterMaskIds.end())
562 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
563 else
564 llvm_unreachable("Can't print this machine register mask yet.");
565 break;
566 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000567 case MachineOperand::MO_Metadata:
568 Op.getMetadata()->printAsOperand(OS, MST);
569 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000570 case MachineOperand::MO_CFIIndex: {
571 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000572 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000573 break;
574 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000575 default:
576 // TODO: Print the other machine operands.
577 llvm_unreachable("Can't print this machine operand at the moment");
578 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000579}
580
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000581static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
582 const TargetRegisterInfo *TRI) {
583 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
584 if (Reg == -1) {
585 OS << "<badreg>";
586 return;
587 }
588 printReg(Reg, OS, TRI);
589}
590
591void MIPrinter::print(const MCCFIInstruction &CFI,
592 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000593 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000594 case MCCFIInstruction::OpOffset:
595 OS << ".cfi_offset ";
596 if (CFI.getLabel())
597 OS << "<mcsymbol> ";
598 printCFIRegister(CFI.getRegister(), OS, TRI);
599 OS << ", " << CFI.getOffset();
600 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000601 case MCCFIInstruction::OpDefCfaRegister:
602 OS << ".cfi_def_cfa_register ";
603 if (CFI.getLabel())
604 OS << "<mcsymbol> ";
605 printCFIRegister(CFI.getRegister(), OS, TRI);
606 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000607 case MCCFIInstruction::OpDefCfaOffset:
608 OS << ".cfi_def_cfa_offset ";
609 if (CFI.getLabel())
610 OS << "<mcsymbol> ";
611 OS << CFI.getOffset();
612 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000613 case MCCFIInstruction::OpDefCfa:
614 OS << ".cfi_def_cfa ";
615 if (CFI.getLabel())
616 OS << "<mcsymbol> ";
617 printCFIRegister(CFI.getRegister(), OS, TRI);
618 OS << ", " << CFI.getOffset();
619 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000620 default:
621 // TODO: Print the other CFI Operations.
622 OS << "<unserializable cfi operation>";
623 break;
624 }
625}
626
Alex Lorenz345c1442015-06-15 23:52:35 +0000627void llvm::printMIR(raw_ostream &OS, const Module &M) {
628 yaml::Output Out(OS);
629 Out << const_cast<Module &>(M);
630}
631
632void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
633 MIRPrinter Printer(OS);
634 Printer.print(MF);
635}