blob: 24e9e381a132147e9ed60c65aafd1b3ca4507ba1 [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 Lorenz60541c12015-07-09 19:55:27 +000075 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000076 void convert(yaml::MachineFunction &MF,
77 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000078 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
79 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000080 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000081 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000082 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000083 const MachineFrameInfo &MFI,
84 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000085
86private:
87 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000088};
89
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000090/// This class prints out the machine instructions using the MIR serialization
91/// format.
92class MIPrinter {
93 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000094 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000095 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000096 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000097
98public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +000099 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000100 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
101 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
102 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
103 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000104
105 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000106 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000107 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000108 void printStackObjectReference(int FrameIndex);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000109 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000110
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000111 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000112};
113
Alex Lorenz345c1442015-06-15 23:52:35 +0000114} // end anonymous namespace
115
116namespace llvm {
117namespace yaml {
118
119/// This struct serializes the LLVM IR module.
120template <> struct BlockScalarTraits<Module> {
121 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
122 Mod.print(OS, nullptr);
123 }
124 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
125 llvm_unreachable("LLVM Module is supposed to be parsed separately");
126 return "";
127 }
128};
129
130} // end namespace yaml
131} // end namespace llvm
132
Alex Lorenz15a00a82015-07-14 21:18:25 +0000133static void printReg(unsigned Reg, raw_ostream &OS,
134 const TargetRegisterInfo *TRI) {
135 // TODO: Print Stack Slots.
136 if (!Reg)
137 OS << '_';
138 else if (TargetRegisterInfo::isVirtualRegister(Reg))
139 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
140 else if (Reg < TRI->getNumRegs())
141 OS << '%' << StringRef(TRI->getName(Reg)).lower();
142 else
143 llvm_unreachable("Can't print this kind of register yet");
144}
145
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000146static void printReg(unsigned Reg, yaml::StringValue &Dest,
147 const TargetRegisterInfo *TRI) {
148 raw_string_ostream OS(Dest.Value);
149 printReg(Reg, OS, TRI);
150}
151
Alex Lorenz345c1442015-06-15 23:52:35 +0000152void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000153 initRegisterMaskIds(MF);
154
Alex Lorenz345c1442015-06-15 23:52:35 +0000155 yaml::MachineFunction YamlMF;
156 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000157 YamlMF.Alignment = MF.getAlignment();
158 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
159 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000160 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenz60541c12015-07-09 19:55:27 +0000161 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000162 convertStackObjects(YamlMF, *MF.getFrameInfo(),
163 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000164 if (const auto *ConstantPool = MF.getConstantPool())
165 convert(YamlMF, *ConstantPool);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000166
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000167 ModuleSlotTracker MST(MF.getFunction()->getParent());
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000168 MST.incorporateFunction(*MF.getFunction());
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 Lorenz60541c12015-07-09 19:55:27 +0000210void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
211 const MachineFrameInfo &MFI) {
212 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
213 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
214 YamlMFI.HasStackMap = MFI.hasStackMap();
215 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
216 YamlMFI.StackSize = MFI.getStackSize();
217 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
218 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
219 YamlMFI.AdjustsStack = MFI.adjustsStack();
220 YamlMFI.HasCalls = MFI.hasCalls();
221 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
222 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
223 YamlMFI.HasVAStart = MFI.hasVAStart();
224 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
225}
226
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000227void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000228 const MachineFrameInfo &MFI,
229 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000230 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000231 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000232 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
233 if (MFI.isDeadObjectIndex(I))
234 continue;
235
236 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000237 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000238 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
239 ? yaml::FixedMachineStackObject::SpillSlot
240 : yaml::FixedMachineStackObject::DefaultType;
241 YamlObject.Offset = MFI.getObjectOffset(I);
242 YamlObject.Size = MFI.getObjectSize(I);
243 YamlObject.Alignment = MFI.getObjectAlignment(I);
244 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
245 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
246 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000247 StackObjectOperandMapping.insert(
248 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000249 }
250
251 // Process ordinary stack objects.
252 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000253 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
254 if (MFI.isDeadObjectIndex(I))
255 continue;
256
257 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000258 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000259 if (const auto *Alloca = MFI.getObjectAllocation(I))
260 YamlObject.Name.Value =
261 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000262 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
263 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000264 : MFI.isVariableSizedObjectIndex(I)
265 ? yaml::MachineStackObject::VariableSized
266 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000267 YamlObject.Offset = MFI.getObjectOffset(I);
268 YamlObject.Size = MFI.getObjectSize(I);
269 YamlObject.Alignment = MFI.getObjectAlignment(I);
270
271 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000272 StackObjectOperandMapping.insert(std::make_pair(
273 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000274 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000275
276 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
277 yaml::StringValue Reg;
278 printReg(CSInfo.getReg(), Reg, TRI);
279 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
280 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
281 "Invalid stack object index");
282 const FrameIndexOperand &StackObject = StackObjectInfo->second;
283 if (StackObject.IsFixed)
284 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
285 else
286 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
287 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000288}
289
Alex Lorenzab980492015-07-20 20:51:18 +0000290void MIRPrinter::convert(yaml::MachineFunction &MF,
291 const MachineConstantPool &ConstantPool) {
292 unsigned ID = 0;
293 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
294 // TODO: Serialize target specific constant pool entries.
295 if (Constant.isMachineConstantPoolEntry())
296 llvm_unreachable("Can't print target specific constant pool entries yet");
297
298 yaml::MachineConstantPoolValue YamlConstant;
299 std::string Str;
300 raw_string_ostream StrOS(Str);
301 Constant.Val.ConstVal->printAsOperand(StrOS);
302 YamlConstant.ID = ID++;
303 YamlConstant.Value = StrOS.str();
304 YamlConstant.Alignment = Constant.getAlignment();
305 MF.Constants.push_back(YamlConstant);
306 }
307}
308
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000309void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000310 yaml::MachineJumpTable &YamlJTI,
311 const MachineJumpTableInfo &JTI) {
312 YamlJTI.Kind = JTI.getEntryKind();
313 unsigned ID = 0;
314 for (const auto &Table : JTI.getJumpTables()) {
315 std::string Str;
316 yaml::MachineJumpTable::Entry Entry;
317 Entry.ID = ID++;
318 for (const auto *MBB : Table.MBBs) {
319 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000320 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
321 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000322 Entry.Blocks.push_back(StrOS.str());
323 Str.clear();
324 }
325 YamlJTI.Entries.push_back(Entry);
326 }
327}
328
329void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000330 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000331 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000332 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
333 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000334 if (const auto *BB = MBB.getBasicBlock()) {
335 if (BB->hasName()) {
336 YamlMBB.Name.Value = BB->getName();
337 } else {
338 int Slot = MST.getLocalSlot(BB);
339 if (Slot == -1)
340 YamlMBB.IRBlock.Value = "<badref>";
341 else
342 YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str();
343 }
344 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000345 YamlMBB.Alignment = MBB.getAlignment();
346 YamlMBB.AddressTaken = MBB.hasAddressTaken();
347 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000348 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000349 std::string Str;
350 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000351 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
352 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000353 YamlMBB.Successors.push_back(StrOS.str());
354 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000355 // Print the live in registers.
356 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
357 assert(TRI && "Expected target register info");
358 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
359 std::string Str;
360 raw_string_ostream StrOS(Str);
361 printReg(*I, StrOS, TRI);
362 YamlMBB.LiveIns.push_back(StrOS.str());
363 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000364 // Print the machine instructions.
365 YamlMBB.Instructions.reserve(MBB.size());
366 std::string Str;
367 for (const auto &MI : MBB) {
368 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000369 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000370 YamlMBB.Instructions.push_back(StrOS.str());
371 Str.clear();
372 }
373}
374
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000375void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
376 const auto *TRI = MF.getSubtarget().getRegisterInfo();
377 unsigned I = 0;
378 for (const uint32_t *Mask : TRI->getRegMasks())
379 RegisterMaskIds.insert(std::make_pair(Mask, I++));
380}
381
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000382void MIPrinter::print(const MachineInstr &MI) {
383 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000384 const auto *TRI = SubTarget.getRegisterInfo();
385 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000386 const auto *TII = SubTarget.getInstrInfo();
387 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000388 if (MI.isCFIInstruction())
389 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000390
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000391 unsigned I = 0, E = MI.getNumOperands();
392 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
393 !MI.getOperand(I).isImplicit();
394 ++I) {
395 if (I)
396 OS << ", ";
397 print(MI.getOperand(I), TRI);
398 }
399
400 if (I)
401 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000402 if (MI.getFlag(MachineInstr::FrameSetup))
403 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000404 OS << TII->getName(MI.getOpcode());
Alex Lorenze5a44662015-07-17 00:24:15 +0000405 // TODO: Print the bundling instruction flags, machine mem operands.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000406 if (I < E)
407 OS << ' ';
408
409 bool NeedComma = false;
410 for (; I < E; ++I) {
411 if (NeedComma)
412 OS << ", ";
413 print(MI.getOperand(I), TRI);
414 NeedComma = true;
415 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000416
417 if (MI.getDebugLoc()) {
418 if (NeedComma)
419 OS << ',';
420 OS << " debug-location ";
421 MI.getDebugLoc()->printAsOperand(OS, MST);
422 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000423}
424
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000425void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
426 OS << "%bb." << MBB.getNumber();
427 if (const auto *BB = MBB.getBasicBlock()) {
428 if (BB->hasName())
429 OS << '.' << BB->getName();
430 }
431}
432
Alex Lorenzdeb53492015-07-28 17:28:03 +0000433void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
434 OS << "%ir-block.";
435 if (BB.hasName()) {
436 printLLVMNameWithoutPrefix(OS, BB.getName());
437 return;
438 }
439 int Slot = MST.getLocalSlot(&BB);
440 if (Slot == -1)
441 OS << "<badref>";
442 else
443 OS << Slot;
444}
445
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000446void MIPrinter::printStackObjectReference(int FrameIndex) {
447 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
448 assert(ObjectInfo != StackObjectOperandMapping.end() &&
449 "Invalid frame index");
450 const FrameIndexOperand &Operand = ObjectInfo->second;
451 if (Operand.IsFixed) {
452 OS << "%fixed-stack." << Operand.ID;
453 return;
454 }
455 OS << "%stack." << Operand.ID;
456 if (!Operand.Name.empty())
457 OS << '.' << Operand.Name;
458}
459
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000460void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
461 switch (Op.getType()) {
462 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000463 // TODO: Print the other register flags.
464 if (Op.isImplicit())
465 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000466 if (Op.isDead())
467 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000468 if (Op.isKill())
469 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000470 if (Op.isUndef())
471 OS << "undef ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000472 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000473 // Print the sub register.
474 if (Op.getSubReg() != 0)
475 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000476 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000477 case MachineOperand::MO_Immediate:
478 OS << Op.getImm();
479 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000480 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000481 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000482 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000483 case MachineOperand::MO_FrameIndex:
484 printStackObjectReference(Op.getIndex());
485 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000486 case MachineOperand::MO_ConstantPoolIndex:
487 OS << "%const." << Op.getIndex();
488 // TODO: Print offset and target flags.
489 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000490 case MachineOperand::MO_JumpTableIndex:
491 OS << "%jump-table." << Op.getIndex();
492 // TODO: Print target flags.
493 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000494 case MachineOperand::MO_ExternalSymbol:
495 OS << '$';
496 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
497 // TODO: Print the target flags.
498 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000499 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000500 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000501 // TODO: Print offset and target flags.
502 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000503 case MachineOperand::MO_BlockAddress:
504 OS << "blockaddress(";
505 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
506 MST);
507 OS << ", ";
508 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
509 OS << ')';
510 // TODO: Print offset and target flags.
511 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000512 case MachineOperand::MO_RegisterMask: {
513 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
514 if (RegMaskInfo != RegisterMaskIds.end())
515 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
516 else
517 llvm_unreachable("Can't print this machine register mask yet.");
518 break;
519 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000520 case MachineOperand::MO_Metadata:
521 Op.getMetadata()->printAsOperand(OS, MST);
522 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000523 case MachineOperand::MO_CFIIndex: {
524 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000525 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000526 break;
527 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000528 default:
529 // TODO: Print the other machine operands.
530 llvm_unreachable("Can't print this machine operand at the moment");
531 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000532}
533
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000534static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
535 const TargetRegisterInfo *TRI) {
536 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
537 if (Reg == -1) {
538 OS << "<badreg>";
539 return;
540 }
541 printReg(Reg, OS, TRI);
542}
543
544void MIPrinter::print(const MCCFIInstruction &CFI,
545 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000546 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000547 case MCCFIInstruction::OpOffset:
548 OS << ".cfi_offset ";
549 if (CFI.getLabel())
550 OS << "<mcsymbol> ";
551 printCFIRegister(CFI.getRegister(), OS, TRI);
552 OS << ", " << CFI.getOffset();
553 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000554 case MCCFIInstruction::OpDefCfaRegister:
555 OS << ".cfi_def_cfa_register ";
556 if (CFI.getLabel())
557 OS << "<mcsymbol> ";
558 printCFIRegister(CFI.getRegister(), OS, TRI);
559 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000560 case MCCFIInstruction::OpDefCfaOffset:
561 OS << ".cfi_def_cfa_offset ";
562 if (CFI.getLabel())
563 OS << "<mcsymbol> ";
564 OS << CFI.getOffset();
565 break;
566 default:
567 // TODO: Print the other CFI Operations.
568 OS << "<unserializable cfi operation>";
569 break;
570 }
571}
572
Alex Lorenz345c1442015-06-15 23:52:35 +0000573void llvm::printMIR(raw_ostream &OS, const Module &M) {
574 yaml::Output Out(OS);
575 Out << const_cast<Module &>(M);
576}
577
578void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
579 MIRPrinter Printer(OS);
580 Printer.print(MF);
581}