blob: 84cf09f904c5a64c9521fcf0695ee2b099237817 [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 Lorenz345c1442015-06-15 23:52:35 +000059/// This class prints out the machine functions using the MIR serialization
60/// format.
61class MIRPrinter {
62 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000063 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000064 /// Maps from stack object indices to operand indices which will be used when
65 /// printing frame index machine operands.
66 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000067
68public:
69 MIRPrinter(raw_ostream &OS) : OS(OS) {}
70
71 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000072
Alex Lorenz28148ba2015-07-09 22:23:13 +000073 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
74 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000075 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
76 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000077 void convert(yaml::MachineFunction &MF,
78 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000079 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
80 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000081 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000082 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000083 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000084 const MachineFrameInfo &MFI,
85 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000086
87private:
88 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000089};
90
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000091/// This class prints out the machine instructions using the MIR serialization
92/// format.
93class MIPrinter {
94 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000095 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000096 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000097 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098
99public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000100 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000101 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
102 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
103 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
104 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105
106 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000107 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000108 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000109 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000110 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000111
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000112 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113};
114
Alex Lorenz345c1442015-06-15 23:52:35 +0000115} // end anonymous namespace
116
117namespace llvm {
118namespace yaml {
119
120/// This struct serializes the LLVM IR module.
121template <> struct BlockScalarTraits<Module> {
122 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
123 Mod.print(OS, nullptr);
124 }
125 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
126 llvm_unreachable("LLVM Module is supposed to be parsed separately");
127 return "";
128 }
129};
130
131} // end namespace yaml
132} // end namespace llvm
133
Alex Lorenz15a00a82015-07-14 21:18:25 +0000134static void printReg(unsigned Reg, raw_ostream &OS,
135 const TargetRegisterInfo *TRI) {
136 // TODO: Print Stack Slots.
137 if (!Reg)
138 OS << '_';
139 else if (TargetRegisterInfo::isVirtualRegister(Reg))
140 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
141 else if (Reg < TRI->getNumRegs())
142 OS << '%' << StringRef(TRI->getName(Reg)).lower();
143 else
144 llvm_unreachable("Can't print this kind of register yet");
145}
146
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000147static void printReg(unsigned Reg, yaml::StringValue &Dest,
148 const TargetRegisterInfo *TRI) {
149 raw_string_ostream OS(Dest.Value);
150 printReg(Reg, OS, TRI);
151}
152
Alex Lorenz345c1442015-06-15 23:52:35 +0000153void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000154 initRegisterMaskIds(MF);
155
Alex Lorenz345c1442015-06-15 23:52:35 +0000156 yaml::MachineFunction YamlMF;
157 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000158 YamlMF.Alignment = MF.getAlignment();
159 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
160 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000161 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000162 ModuleSlotTracker MST(MF.getFunction()->getParent());
163 MST.incorporateFunction(*MF.getFunction());
164 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000165 convertStackObjects(YamlMF, *MF.getFrameInfo(),
166 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000167 if (const auto *ConstantPool = MF.getConstantPool())
168 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000169 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
170 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000171 for (const auto &MBB : MF) {
172 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000173 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000174 YamlMF.BasicBlocks.push_back(YamlMBB);
175 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000176 yaml::Output Out(OS);
177 Out << YamlMF;
178}
179
Alex Lorenz54565cf2015-06-24 19:56:10 +0000180void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000181 const MachineRegisterInfo &RegInfo,
182 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000183 MF.IsSSA = RegInfo.isSSA();
184 MF.TracksRegLiveness = RegInfo.tracksLiveness();
185 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000186
187 // Print the virtual register definitions.
188 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
189 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
190 yaml::VirtualRegisterDefinition VReg;
191 VReg.ID = I;
192 VReg.Class =
193 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000194 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
195 if (PreferredReg)
196 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000197 MF.VirtualRegisters.push_back(VReg);
198 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000199
200 // Print the live ins.
201 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
202 yaml::MachineFunctionLiveIn LiveIn;
203 printReg(I->first, LiveIn.Register, TRI);
204 if (I->second)
205 printReg(I->second, LiveIn.VirtualRegister, TRI);
206 MF.LiveIns.push_back(LiveIn);
207 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000208}
209
Alex Lorenza6f9a372015-07-29 21:09:09 +0000210void MIRPrinter::convert(ModuleSlotTracker &MST,
211 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000212 const MachineFrameInfo &MFI) {
213 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
214 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
215 YamlMFI.HasStackMap = MFI.hasStackMap();
216 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
217 YamlMFI.StackSize = MFI.getStackSize();
218 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
219 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
220 YamlMFI.AdjustsStack = MFI.adjustsStack();
221 YamlMFI.HasCalls = MFI.hasCalls();
222 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
223 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
224 YamlMFI.HasVAStart = MFI.hasVAStart();
225 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000226 if (MFI.getSavePoint()) {
227 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
228 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
229 .printMBBReference(*MFI.getSavePoint());
230 }
231 if (MFI.getRestorePoint()) {
232 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
233 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
234 .printMBBReference(*MFI.getRestorePoint());
235 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000236}
237
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000238void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000239 const MachineFrameInfo &MFI,
240 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000241 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000242 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000243 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
244 if (MFI.isDeadObjectIndex(I))
245 continue;
246
247 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000248 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000249 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
250 ? yaml::FixedMachineStackObject::SpillSlot
251 : yaml::FixedMachineStackObject::DefaultType;
252 YamlObject.Offset = MFI.getObjectOffset(I);
253 YamlObject.Size = MFI.getObjectSize(I);
254 YamlObject.Alignment = MFI.getObjectAlignment(I);
255 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
256 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
257 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000258 StackObjectOperandMapping.insert(
259 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000260 }
261
262 // Process ordinary stack objects.
263 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000264 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
265 if (MFI.isDeadObjectIndex(I))
266 continue;
267
268 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000269 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000270 if (const auto *Alloca = MFI.getObjectAllocation(I))
271 YamlObject.Name.Value =
272 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000273 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
274 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000275 : MFI.isVariableSizedObjectIndex(I)
276 ? yaml::MachineStackObject::VariableSized
277 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000278 YamlObject.Offset = MFI.getObjectOffset(I);
279 YamlObject.Size = MFI.getObjectSize(I);
280 YamlObject.Alignment = MFI.getObjectAlignment(I);
281
282 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000283 StackObjectOperandMapping.insert(std::make_pair(
284 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000285 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000286
287 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
288 yaml::StringValue Reg;
289 printReg(CSInfo.getReg(), Reg, TRI);
290 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
291 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
292 "Invalid stack object index");
293 const FrameIndexOperand &StackObject = StackObjectInfo->second;
294 if (StackObject.IsFixed)
295 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
296 else
297 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
298 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000299}
300
Alex Lorenzab980492015-07-20 20:51:18 +0000301void MIRPrinter::convert(yaml::MachineFunction &MF,
302 const MachineConstantPool &ConstantPool) {
303 unsigned ID = 0;
304 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
305 // TODO: Serialize target specific constant pool entries.
306 if (Constant.isMachineConstantPoolEntry())
307 llvm_unreachable("Can't print target specific constant pool entries yet");
308
309 yaml::MachineConstantPoolValue YamlConstant;
310 std::string Str;
311 raw_string_ostream StrOS(Str);
312 Constant.Val.ConstVal->printAsOperand(StrOS);
313 YamlConstant.ID = ID++;
314 YamlConstant.Value = StrOS.str();
315 YamlConstant.Alignment = Constant.getAlignment();
316 MF.Constants.push_back(YamlConstant);
317 }
318}
319
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000320void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000321 yaml::MachineJumpTable &YamlJTI,
322 const MachineJumpTableInfo &JTI) {
323 YamlJTI.Kind = JTI.getEntryKind();
324 unsigned ID = 0;
325 for (const auto &Table : JTI.getJumpTables()) {
326 std::string Str;
327 yaml::MachineJumpTable::Entry Entry;
328 Entry.ID = ID++;
329 for (const auto *MBB : Table.MBBs) {
330 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000331 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
332 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000333 Entry.Blocks.push_back(StrOS.str());
334 Str.clear();
335 }
336 YamlJTI.Entries.push_back(Entry);
337 }
338}
339
340void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000341 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000342 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000343 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
344 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000345 if (const auto *BB = MBB.getBasicBlock()) {
346 if (BB->hasName()) {
347 YamlMBB.Name.Value = BB->getName();
348 } else {
349 int Slot = MST.getLocalSlot(BB);
350 if (Slot == -1)
351 YamlMBB.IRBlock.Value = "<badref>";
352 else
353 YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str();
354 }
355 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000356 YamlMBB.Alignment = MBB.getAlignment();
357 YamlMBB.AddressTaken = MBB.hasAddressTaken();
358 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000359 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000360 std::string Str;
361 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000362 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
363 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000364 YamlMBB.Successors.push_back(StrOS.str());
365 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000366 // Print the live in registers.
367 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
368 assert(TRI && "Expected target register info");
369 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
370 std::string Str;
371 raw_string_ostream StrOS(Str);
372 printReg(*I, StrOS, TRI);
373 YamlMBB.LiveIns.push_back(StrOS.str());
374 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000375 // Print the machine instructions.
376 YamlMBB.Instructions.reserve(MBB.size());
377 std::string Str;
378 for (const auto &MI : MBB) {
379 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000380 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000381 YamlMBB.Instructions.push_back(StrOS.str());
382 Str.clear();
383 }
384}
385
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000386void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
387 const auto *TRI = MF.getSubtarget().getRegisterInfo();
388 unsigned I = 0;
389 for (const uint32_t *Mask : TRI->getRegMasks())
390 RegisterMaskIds.insert(std::make_pair(Mask, I++));
391}
392
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000393void MIPrinter::print(const MachineInstr &MI) {
394 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000395 const auto *TRI = SubTarget.getRegisterInfo();
396 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000397 const auto *TII = SubTarget.getInstrInfo();
398 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000399 if (MI.isCFIInstruction())
400 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000401
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000402 unsigned I = 0, E = MI.getNumOperands();
403 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
404 !MI.getOperand(I).isImplicit();
405 ++I) {
406 if (I)
407 OS << ", ";
408 print(MI.getOperand(I), TRI);
409 }
410
411 if (I)
412 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000413 if (MI.getFlag(MachineInstr::FrameSetup))
414 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000415 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000416 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000417 if (I < E)
418 OS << ' ';
419
420 bool NeedComma = false;
421 for (; I < E; ++I) {
422 if (NeedComma)
423 OS << ", ";
424 print(MI.getOperand(I), TRI);
425 NeedComma = true;
426 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000427
428 if (MI.getDebugLoc()) {
429 if (NeedComma)
430 OS << ',';
431 OS << " debug-location ";
432 MI.getDebugLoc()->printAsOperand(OS, MST);
433 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000434}
435
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000436void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
437 OS << "%bb." << MBB.getNumber();
438 if (const auto *BB = MBB.getBasicBlock()) {
439 if (BB->hasName())
440 OS << '.' << BB->getName();
441 }
442}
443
Alex Lorenzdeb53492015-07-28 17:28:03 +0000444void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
445 OS << "%ir-block.";
446 if (BB.hasName()) {
447 printLLVMNameWithoutPrefix(OS, BB.getName());
448 return;
449 }
450 int Slot = MST.getLocalSlot(&BB);
451 if (Slot == -1)
452 OS << "<badref>";
453 else
454 OS << Slot;
455}
456
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000457void MIPrinter::printStackObjectReference(int FrameIndex) {
458 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
459 assert(ObjectInfo != StackObjectOperandMapping.end() &&
460 "Invalid frame index");
461 const FrameIndexOperand &Operand = ObjectInfo->second;
462 if (Operand.IsFixed) {
463 OS << "%fixed-stack." << Operand.ID;
464 return;
465 }
466 OS << "%stack." << Operand.ID;
467 if (!Operand.Name.empty())
468 OS << '.' << Operand.Name;
469}
470
Alex Lorenzef5c1962015-07-28 23:02:45 +0000471static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
472 const auto *TII = MF.getSubtarget().getInstrInfo();
473 assert(TII && "expected instruction info");
474 auto Indices = TII->getSerializableTargetIndices();
475 for (const auto &I : Indices) {
476 if (I.first == Index) {
477 return I.second;
478 }
479 }
480 return nullptr;
481}
482
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000483void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
484 switch (Op.getType()) {
485 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000486 // TODO: Print the other register flags.
487 if (Op.isImplicit())
488 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000489 if (Op.isDead())
490 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000491 if (Op.isKill())
492 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000493 if (Op.isUndef())
494 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000495 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000496 // Print the sub register.
497 if (Op.getSubReg() != 0)
498 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000499 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000500 case MachineOperand::MO_Immediate:
501 OS << Op.getImm();
502 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000503 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000504 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000505 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000506 case MachineOperand::MO_FrameIndex:
507 printStackObjectReference(Op.getIndex());
508 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000509 case MachineOperand::MO_ConstantPoolIndex:
510 OS << "%const." << Op.getIndex();
511 // TODO: Print offset and target flags.
512 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000513 case MachineOperand::MO_TargetIndex: {
514 OS << "target-index(";
515 if (const auto *Name = getTargetIndexName(
516 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
517 OS << Name;
518 else
519 OS << "<unknown>";
520 OS << ')';
521 // TODO: Print the offset and target flags.
522 break;
523 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000524 case MachineOperand::MO_JumpTableIndex:
525 OS << "%jump-table." << Op.getIndex();
526 // TODO: Print target flags.
527 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000528 case MachineOperand::MO_ExternalSymbol:
529 OS << '$';
530 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
531 // TODO: Print the target flags.
532 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000533 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000534 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000535 // TODO: Print offset and target flags.
536 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000537 case MachineOperand::MO_BlockAddress:
538 OS << "blockaddress(";
539 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
540 MST);
541 OS << ", ";
542 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
543 OS << ')';
544 // TODO: Print offset and target flags.
545 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000546 case MachineOperand::MO_RegisterMask: {
547 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
548 if (RegMaskInfo != RegisterMaskIds.end())
549 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
550 else
551 llvm_unreachable("Can't print this machine register mask yet.");
552 break;
553 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000554 case MachineOperand::MO_Metadata:
555 Op.getMetadata()->printAsOperand(OS, MST);
556 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000557 case MachineOperand::MO_CFIIndex: {
558 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000559 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000560 break;
561 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000562 default:
563 // TODO: Print the other machine operands.
564 llvm_unreachable("Can't print this machine operand at the moment");
565 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000566}
567
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000568static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
569 const TargetRegisterInfo *TRI) {
570 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
571 if (Reg == -1) {
572 OS << "<badreg>";
573 return;
574 }
575 printReg(Reg, OS, TRI);
576}
577
578void MIPrinter::print(const MCCFIInstruction &CFI,
579 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000580 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000581 case MCCFIInstruction::OpOffset:
582 OS << ".cfi_offset ";
583 if (CFI.getLabel())
584 OS << "<mcsymbol> ";
585 printCFIRegister(CFI.getRegister(), OS, TRI);
586 OS << ", " << CFI.getOffset();
587 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000588 case MCCFIInstruction::OpDefCfaRegister:
589 OS << ".cfi_def_cfa_register ";
590 if (CFI.getLabel())
591 OS << "<mcsymbol> ";
592 printCFIRegister(CFI.getRegister(), OS, TRI);
593 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000594 case MCCFIInstruction::OpDefCfaOffset:
595 OS << ".cfi_def_cfa_offset ";
596 if (CFI.getLabel())
597 OS << "<mcsymbol> ";
598 OS << CFI.getOffset();
599 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000600 case MCCFIInstruction::OpDefCfa:
601 OS << ".cfi_def_cfa ";
602 if (CFI.getLabel())
603 OS << "<mcsymbol> ";
604 printCFIRegister(CFI.getRegister(), OS, TRI);
605 OS << ", " << CFI.getOffset();
606 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000607 default:
608 // TODO: Print the other CFI Operations.
609 OS << "<unserializable cfi operation>";
610 break;
611 }
612}
613
Alex Lorenz345c1442015-06-15 23:52:35 +0000614void llvm::printMIR(raw_ostream &OS, const Module &M) {
615 yaml::Output Out(OS);
616 Out << const_cast<Module &>(M);
617}
618
619void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
620 MIRPrinter Printer(OS);
621 Printer.print(MF);
622}