blob: e162a8f73e2568ab546e226b2485fb575b5927be [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 Lorenz4af7e612015-08-03 23:08:19 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000023#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000024#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000025#include "llvm/IR/Constants.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000026#include "llvm/IR/Instructions.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000027#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000028#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000035
36using namespace llvm;
37
38namespace {
39
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000040/// This structure describes how to print out stack object references.
41struct FrameIndexOperand {
42 std::string Name;
43 unsigned ID;
44 bool IsFixed;
45
46 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
47 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
48
49 /// Return an ordinary stack object reference.
50 static FrameIndexOperand create(StringRef Name, unsigned ID) {
51 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
52 }
53
54 /// Return a fixed stack object reference.
55 static FrameIndexOperand createFixed(unsigned ID) {
56 return FrameIndexOperand("", ID, /*IsFixed=*/true);
57 }
58};
59
Alex Lorenz618b2832015-07-30 16:54:38 +000060} // end anonymous namespace
61
62namespace llvm {
63
Alex Lorenz345c1442015-06-15 23:52:35 +000064/// This class prints out the machine functions using the MIR serialization
65/// format.
66class MIRPrinter {
67 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000068 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000069 /// Maps from stack object indices to operand indices which will be used when
70 /// printing frame index machine operands.
71 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000072
73public:
74 MIRPrinter(raw_ostream &OS) : OS(OS) {}
75
76 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000077
Alex Lorenz28148ba2015-07-09 22:23:13 +000078 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
79 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000080 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
81 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000082 void convert(yaml::MachineFunction &MF,
83 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000084 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
85 const MachineJumpTableInfo &JTI);
Alex Lorenz900b5cb2015-07-07 23:27:53 +000086 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz5d6108e2015-06-26 22:56:48 +000087 const MachineBasicBlock &MBB);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000088 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000089 const MachineFrameInfo &MFI,
90 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000091
92private:
93 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000094};
95
Alex Lorenz618b2832015-07-30 16:54:38 +000096} // end namespace llvm
97
98namespace {
99
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100/// This class prints out the machine instructions using the MIR serialization
101/// format.
102class MIPrinter {
103 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000104 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000105 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000106 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000107
108public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000109 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000110 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
111 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
112 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
113 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000114
115 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000116 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000117 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000118 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000119 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000120 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000121 void printTargetFlags(const MachineOperand &Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000122 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000123 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000124
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000125 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126};
127
Alex Lorenz345c1442015-06-15 23:52:35 +0000128} // end anonymous namespace
129
130namespace llvm {
131namespace yaml {
132
133/// This struct serializes the LLVM IR module.
134template <> struct BlockScalarTraits<Module> {
135 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
136 Mod.print(OS, nullptr);
137 }
138 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
139 llvm_unreachable("LLVM Module is supposed to be parsed separately");
140 return "";
141 }
142};
143
144} // end namespace yaml
145} // end namespace llvm
146
Alex Lorenz15a00a82015-07-14 21:18:25 +0000147static void printReg(unsigned Reg, raw_ostream &OS,
148 const TargetRegisterInfo *TRI) {
149 // TODO: Print Stack Slots.
150 if (!Reg)
151 OS << '_';
152 else if (TargetRegisterInfo::isVirtualRegister(Reg))
153 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
154 else if (Reg < TRI->getNumRegs())
155 OS << '%' << StringRef(TRI->getName(Reg)).lower();
156 else
157 llvm_unreachable("Can't print this kind of register yet");
158}
159
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000160static void printReg(unsigned Reg, yaml::StringValue &Dest,
161 const TargetRegisterInfo *TRI) {
162 raw_string_ostream OS(Dest.Value);
163 printReg(Reg, OS, TRI);
164}
165
Alex Lorenz345c1442015-06-15 23:52:35 +0000166void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000167 initRegisterMaskIds(MF);
168
Alex Lorenz345c1442015-06-15 23:52:35 +0000169 yaml::MachineFunction YamlMF;
170 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000171 YamlMF.Alignment = MF.getAlignment();
172 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
173 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000174 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000175 ModuleSlotTracker MST(MF.getFunction()->getParent());
176 MST.incorporateFunction(*MF.getFunction());
177 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000178 convertStackObjects(YamlMF, *MF.getFrameInfo(),
179 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000180 if (const auto *ConstantPool = MF.getConstantPool())
181 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000182 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
183 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000184 for (const auto &MBB : MF) {
185 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000186 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000187 YamlMF.BasicBlocks.push_back(YamlMBB);
188 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000189 yaml::Output Out(OS);
190 Out << YamlMF;
191}
192
Alex Lorenz54565cf2015-06-24 19:56:10 +0000193void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000194 const MachineRegisterInfo &RegInfo,
195 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000196 MF.IsSSA = RegInfo.isSSA();
197 MF.TracksRegLiveness = RegInfo.tracksLiveness();
198 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000199
200 // Print the virtual register definitions.
201 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
202 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
203 yaml::VirtualRegisterDefinition VReg;
204 VReg.ID = I;
205 VReg.Class =
206 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000207 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
208 if (PreferredReg)
209 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000210 MF.VirtualRegisters.push_back(VReg);
211 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000212
213 // Print the live ins.
214 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
215 yaml::MachineFunctionLiveIn LiveIn;
216 printReg(I->first, LiveIn.Register, TRI);
217 if (I->second)
218 printReg(I->second, LiveIn.VirtualRegister, TRI);
219 MF.LiveIns.push_back(LiveIn);
220 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000221}
222
Alex Lorenza6f9a372015-07-29 21:09:09 +0000223void MIRPrinter::convert(ModuleSlotTracker &MST,
224 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000225 const MachineFrameInfo &MFI) {
226 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
227 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
228 YamlMFI.HasStackMap = MFI.hasStackMap();
229 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
230 YamlMFI.StackSize = MFI.getStackSize();
231 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
232 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
233 YamlMFI.AdjustsStack = MFI.adjustsStack();
234 YamlMFI.HasCalls = MFI.hasCalls();
235 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
236 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
237 YamlMFI.HasVAStart = MFI.hasVAStart();
238 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000239 if (MFI.getSavePoint()) {
240 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
241 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
242 .printMBBReference(*MFI.getSavePoint());
243 }
244 if (MFI.getRestorePoint()) {
245 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
246 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
247 .printMBBReference(*MFI.getRestorePoint());
248 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000249}
250
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000251void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000252 const MachineFrameInfo &MFI,
253 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000254 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000255 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000256 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
257 if (MFI.isDeadObjectIndex(I))
258 continue;
259
260 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000261 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000262 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
263 ? yaml::FixedMachineStackObject::SpillSlot
264 : yaml::FixedMachineStackObject::DefaultType;
265 YamlObject.Offset = MFI.getObjectOffset(I);
266 YamlObject.Size = MFI.getObjectSize(I);
267 YamlObject.Alignment = MFI.getObjectAlignment(I);
268 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
269 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
270 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000271 StackObjectOperandMapping.insert(
272 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000273 }
274
275 // Process ordinary stack objects.
276 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000277 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
278 if (MFI.isDeadObjectIndex(I))
279 continue;
280
281 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000282 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000283 if (const auto *Alloca = MFI.getObjectAllocation(I))
284 YamlObject.Name.Value =
285 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000286 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
287 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000288 : MFI.isVariableSizedObjectIndex(I)
289 ? yaml::MachineStackObject::VariableSized
290 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000291 YamlObject.Offset = MFI.getObjectOffset(I);
292 YamlObject.Size = MFI.getObjectSize(I);
293 YamlObject.Alignment = MFI.getObjectAlignment(I);
294
295 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000296 StackObjectOperandMapping.insert(std::make_pair(
297 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000298 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000299
300 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
301 yaml::StringValue Reg;
302 printReg(CSInfo.getReg(), Reg, TRI);
303 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
304 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
305 "Invalid stack object index");
306 const FrameIndexOperand &StackObject = StackObjectInfo->second;
307 if (StackObject.IsFixed)
308 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
309 else
310 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
311 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000312}
313
Alex Lorenzab980492015-07-20 20:51:18 +0000314void MIRPrinter::convert(yaml::MachineFunction &MF,
315 const MachineConstantPool &ConstantPool) {
316 unsigned ID = 0;
317 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
318 // TODO: Serialize target specific constant pool entries.
319 if (Constant.isMachineConstantPoolEntry())
320 llvm_unreachable("Can't print target specific constant pool entries yet");
321
322 yaml::MachineConstantPoolValue YamlConstant;
323 std::string Str;
324 raw_string_ostream StrOS(Str);
325 Constant.Val.ConstVal->printAsOperand(StrOS);
326 YamlConstant.ID = ID++;
327 YamlConstant.Value = StrOS.str();
328 YamlConstant.Alignment = Constant.getAlignment();
329 MF.Constants.push_back(YamlConstant);
330 }
331}
332
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000333void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000334 yaml::MachineJumpTable &YamlJTI,
335 const MachineJumpTableInfo &JTI) {
336 YamlJTI.Kind = JTI.getEntryKind();
337 unsigned ID = 0;
338 for (const auto &Table : JTI.getJumpTables()) {
339 std::string Str;
340 yaml::MachineJumpTable::Entry Entry;
341 Entry.ID = ID++;
342 for (const auto *MBB : Table.MBBs) {
343 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000344 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
345 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000346 Entry.Blocks.push_back(StrOS.str());
347 Str.clear();
348 }
349 YamlJTI.Entries.push_back(Entry);
350 }
351}
352
353void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000354 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000355 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000356 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
357 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000358 if (const auto *BB = MBB.getBasicBlock()) {
359 if (BB->hasName()) {
360 YamlMBB.Name.Value = BB->getName();
361 } else {
362 int Slot = MST.getLocalSlot(BB);
363 if (Slot == -1)
364 YamlMBB.IRBlock.Value = "<badref>";
365 else
366 YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str();
367 }
368 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000369 YamlMBB.Alignment = MBB.getAlignment();
370 YamlMBB.AddressTaken = MBB.hasAddressTaken();
371 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000372 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000373 std::string Str;
374 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000375 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
376 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000377 YamlMBB.Successors.push_back(StrOS.str());
378 }
Alex Lorenz618b2832015-07-30 16:54:38 +0000379 if (MBB.hasSuccessorWeights()) {
380 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I)
381 YamlMBB.SuccessorWeights.push_back(
382 yaml::UnsignedValue(MBB.getSuccWeight(I)));
383 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000384 // Print the live in registers.
385 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
386 assert(TRI && "Expected target register info");
387 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
388 std::string Str;
389 raw_string_ostream StrOS(Str);
390 printReg(*I, StrOS, TRI);
391 YamlMBB.LiveIns.push_back(StrOS.str());
392 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000393 // Print the machine instructions.
394 YamlMBB.Instructions.reserve(MBB.size());
395 std::string Str;
396 for (const auto &MI : MBB) {
397 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000398 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000399 YamlMBB.Instructions.push_back(StrOS.str());
400 Str.clear();
401 }
402}
403
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000404void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
405 const auto *TRI = MF.getSubtarget().getRegisterInfo();
406 unsigned I = 0;
407 for (const uint32_t *Mask : TRI->getRegMasks())
408 RegisterMaskIds.insert(std::make_pair(Mask, I++));
409}
410
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000411void MIPrinter::print(const MachineInstr &MI) {
412 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000413 const auto *TRI = SubTarget.getRegisterInfo();
414 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000415 const auto *TII = SubTarget.getInstrInfo();
416 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000417 if (MI.isCFIInstruction())
418 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000419
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000420 unsigned I = 0, E = MI.getNumOperands();
421 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
422 !MI.getOperand(I).isImplicit();
423 ++I) {
424 if (I)
425 OS << ", ";
426 print(MI.getOperand(I), TRI);
427 }
428
429 if (I)
430 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000431 if (MI.getFlag(MachineInstr::FrameSetup))
432 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000433 OS << TII->getName(MI.getOpcode());
Alex Lorenz4af7e612015-08-03 23:08:19 +0000434 // TODO: Print the bundling instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000435 if (I < E)
436 OS << ' ';
437
438 bool NeedComma = false;
439 for (; I < E; ++I) {
440 if (NeedComma)
441 OS << ", ";
442 print(MI.getOperand(I), TRI);
443 NeedComma = true;
444 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000445
446 if (MI.getDebugLoc()) {
447 if (NeedComma)
448 OS << ',';
449 OS << " debug-location ";
450 MI.getDebugLoc()->printAsOperand(OS, MST);
451 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000452
453 if (!MI.memoperands_empty()) {
454 OS << " :: ";
455 bool NeedComma = false;
456 for (const auto *Op : MI.memoperands()) {
457 if (NeedComma)
458 OS << ", ";
459 print(*Op);
460 NeedComma = true;
461 }
462 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000463}
464
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000465void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
466 OS << "%bb." << MBB.getNumber();
467 if (const auto *BB = MBB.getBasicBlock()) {
468 if (BB->hasName())
469 OS << '.' << BB->getName();
470 }
471}
472
Alex Lorenzdeb53492015-07-28 17:28:03 +0000473void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
474 OS << "%ir-block.";
475 if (BB.hasName()) {
476 printLLVMNameWithoutPrefix(OS, BB.getName());
477 return;
478 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000479 const Function *F = BB.getParent();
480 int Slot;
481 if (F == MST.getCurrentFunction()) {
482 Slot = MST.getLocalSlot(&BB);
483 } else {
484 ModuleSlotTracker CustomMST(F->getParent(),
485 /*ShouldInitializeAllMetadata=*/false);
486 CustomMST.incorporateFunction(*F);
487 Slot = CustomMST.getLocalSlot(&BB);
488 }
Alex Lorenzdeb53492015-07-28 17:28:03 +0000489 if (Slot == -1)
490 OS << "<badref>";
491 else
492 OS << Slot;
493}
494
Alex Lorenz4af7e612015-08-03 23:08:19 +0000495void MIPrinter::printIRValueReference(const Value &V) {
496 OS << "%ir.";
497 if (V.hasName()) {
498 printLLVMNameWithoutPrefix(OS, V.getName());
499 return;
500 }
501 // TODO: Serialize the unnamed IR value references.
502 OS << "<unserializable ir value>";
503}
504
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000505void MIPrinter::printStackObjectReference(int FrameIndex) {
506 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
507 assert(ObjectInfo != StackObjectOperandMapping.end() &&
508 "Invalid frame index");
509 const FrameIndexOperand &Operand = ObjectInfo->second;
510 if (Operand.IsFixed) {
511 OS << "%fixed-stack." << Operand.ID;
512 return;
513 }
514 OS << "%stack." << Operand.ID;
515 if (!Operand.Name.empty())
516 OS << '.' << Operand.Name;
517}
518
Alex Lorenz5672a892015-08-05 22:26:15 +0000519void MIPrinter::printOffset(int64_t Offset) {
520 if (Offset == 0)
521 return;
522 if (Offset < 0) {
523 OS << " - " << -Offset;
524 return;
525 }
526 OS << " + " << Offset;
527}
528
Alex Lorenz49873a82015-08-06 00:44:07 +0000529static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
530 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
531 for (const auto &I : Flags) {
532 if (I.first == TF) {
533 return I.second;
534 }
535 }
536 return nullptr;
537}
538
539void MIPrinter::printTargetFlags(const MachineOperand &Op) {
540 if (!Op.getTargetFlags())
541 return;
542 const auto *TII =
543 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
544 assert(TII && "expected instruction info");
545 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
546 OS << "target-flags(";
547 if (const auto *Name = getTargetFlagName(TII, Flags.first))
548 OS << Name;
549 else
550 OS << "<unknown target flag>";
551 // TODO: Print the target's bit flags.
552 OS << ") ";
553}
554
Alex Lorenzef5c1962015-07-28 23:02:45 +0000555static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
556 const auto *TII = MF.getSubtarget().getInstrInfo();
557 assert(TII && "expected instruction info");
558 auto Indices = TII->getSerializableTargetIndices();
559 for (const auto &I : Indices) {
560 if (I.first == Index) {
561 return I.second;
562 }
563 }
564 return nullptr;
565}
566
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000567void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000568 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000569 switch (Op.getType()) {
570 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000571 // TODO: Print the other register flags.
572 if (Op.isImplicit())
573 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000574 if (Op.isDead())
575 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000576 if (Op.isKill())
577 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000578 if (Op.isUndef())
579 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000580 if (Op.isEarlyClobber())
581 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000582 if (Op.isDebug())
583 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000584 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000585 // Print the sub register.
586 if (Op.getSubReg() != 0)
587 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000588 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000589 case MachineOperand::MO_Immediate:
590 OS << Op.getImm();
591 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000592 case MachineOperand::MO_CImmediate:
593 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
594 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000595 case MachineOperand::MO_FPImmediate:
596 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
597 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000598 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000599 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000600 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000601 case MachineOperand::MO_FrameIndex:
602 printStackObjectReference(Op.getIndex());
603 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000604 case MachineOperand::MO_ConstantPoolIndex:
605 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000606 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000607 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000608 case MachineOperand::MO_TargetIndex: {
609 OS << "target-index(";
610 if (const auto *Name = getTargetIndexName(
611 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
612 OS << Name;
613 else
614 OS << "<unknown>";
615 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000616 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000617 break;
618 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000619 case MachineOperand::MO_JumpTableIndex:
620 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000621 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000622 case MachineOperand::MO_ExternalSymbol:
623 OS << '$';
624 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000625 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000626 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000627 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000628 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000629 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000630 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000631 case MachineOperand::MO_BlockAddress:
632 OS << "blockaddress(";
633 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
634 MST);
635 OS << ", ";
636 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
637 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000638 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000639 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000640 case MachineOperand::MO_RegisterMask: {
641 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
642 if (RegMaskInfo != RegisterMaskIds.end())
643 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
644 else
645 llvm_unreachable("Can't print this machine register mask yet.");
646 break;
647 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000648 case MachineOperand::MO_Metadata:
649 Op.getMetadata()->printAsOperand(OS, MST);
650 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000651 case MachineOperand::MO_CFIIndex: {
652 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000653 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000654 break;
655 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000656 default:
657 // TODO: Print the other machine operands.
658 llvm_unreachable("Can't print this machine operand at the moment");
659 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000660}
661
Alex Lorenz4af7e612015-08-03 23:08:19 +0000662void MIPrinter::print(const MachineMemOperand &Op) {
663 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000664 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000665 if (Op.isVolatile())
666 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000667 if (Op.isNonTemporal())
668 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000669 if (Op.isInvariant())
670 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000671 if (Op.isLoad())
672 OS << "load ";
673 else {
674 assert(Op.isStore() && "Non load machine operand must be a store");
675 OS << "store ";
676 }
677 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
678 if (const Value *Val = Op.getValue())
679 printIRValueReference(*Val);
680 // TODO: Print PseudoSourceValue.
681 // TODO: Print the base alignment.
682 // TODO: Print the metadata attributes.
683 OS << ')';
684}
685
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000686static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
687 const TargetRegisterInfo *TRI) {
688 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
689 if (Reg == -1) {
690 OS << "<badreg>";
691 return;
692 }
693 printReg(Reg, OS, TRI);
694}
695
696void MIPrinter::print(const MCCFIInstruction &CFI,
697 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000698 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000699 case MCCFIInstruction::OpOffset:
700 OS << ".cfi_offset ";
701 if (CFI.getLabel())
702 OS << "<mcsymbol> ";
703 printCFIRegister(CFI.getRegister(), OS, TRI);
704 OS << ", " << CFI.getOffset();
705 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000706 case MCCFIInstruction::OpDefCfaRegister:
707 OS << ".cfi_def_cfa_register ";
708 if (CFI.getLabel())
709 OS << "<mcsymbol> ";
710 printCFIRegister(CFI.getRegister(), OS, TRI);
711 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000712 case MCCFIInstruction::OpDefCfaOffset:
713 OS << ".cfi_def_cfa_offset ";
714 if (CFI.getLabel())
715 OS << "<mcsymbol> ";
716 OS << CFI.getOffset();
717 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000718 case MCCFIInstruction::OpDefCfa:
719 OS << ".cfi_def_cfa ";
720 if (CFI.getLabel())
721 OS << "<mcsymbol> ";
722 printCFIRegister(CFI.getRegister(), OS, TRI);
723 OS << ", " << CFI.getOffset();
724 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000725 default:
726 // TODO: Print the other CFI Operations.
727 OS << "<unserializable cfi operation>";
728 break;
729 }
730}
731
Alex Lorenz345c1442015-06-15 23:52:35 +0000732void llvm::printMIR(raw_ostream &OS, const Module &M) {
733 yaml::Output Out(OS);
734 Out << const_cast<Module &>(M);
735}
736
737void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
738 MIRPrinter Printer(OS);
739 Printer.print(MF);
740}