blob: c84cec9ed2ecc50245f4f4dca5fe5c839ad31845 [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 }
479 int Slot = MST.getLocalSlot(&BB);
480 if (Slot == -1)
481 OS << "<badref>";
482 else
483 OS << Slot;
484}
485
Alex Lorenz4af7e612015-08-03 23:08:19 +0000486void MIPrinter::printIRValueReference(const Value &V) {
487 OS << "%ir.";
488 if (V.hasName()) {
489 printLLVMNameWithoutPrefix(OS, V.getName());
490 return;
491 }
492 // TODO: Serialize the unnamed IR value references.
493 OS << "<unserializable ir value>";
494}
495
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000496void MIPrinter::printStackObjectReference(int FrameIndex) {
497 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
498 assert(ObjectInfo != StackObjectOperandMapping.end() &&
499 "Invalid frame index");
500 const FrameIndexOperand &Operand = ObjectInfo->second;
501 if (Operand.IsFixed) {
502 OS << "%fixed-stack." << Operand.ID;
503 return;
504 }
505 OS << "%stack." << Operand.ID;
506 if (!Operand.Name.empty())
507 OS << '.' << Operand.Name;
508}
509
Alex Lorenz5672a892015-08-05 22:26:15 +0000510void MIPrinter::printOffset(int64_t Offset) {
511 if (Offset == 0)
512 return;
513 if (Offset < 0) {
514 OS << " - " << -Offset;
515 return;
516 }
517 OS << " + " << Offset;
518}
519
Alex Lorenz49873a82015-08-06 00:44:07 +0000520static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
521 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
522 for (const auto &I : Flags) {
523 if (I.first == TF) {
524 return I.second;
525 }
526 }
527 return nullptr;
528}
529
530void MIPrinter::printTargetFlags(const MachineOperand &Op) {
531 if (!Op.getTargetFlags())
532 return;
533 const auto *TII =
534 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
535 assert(TII && "expected instruction info");
536 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
537 OS << "target-flags(";
538 if (const auto *Name = getTargetFlagName(TII, Flags.first))
539 OS << Name;
540 else
541 OS << "<unknown target flag>";
542 // TODO: Print the target's bit flags.
543 OS << ") ";
544}
545
Alex Lorenzef5c1962015-07-28 23:02:45 +0000546static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
547 const auto *TII = MF.getSubtarget().getInstrInfo();
548 assert(TII && "expected instruction info");
549 auto Indices = TII->getSerializableTargetIndices();
550 for (const auto &I : Indices) {
551 if (I.first == Index) {
552 return I.second;
553 }
554 }
555 return nullptr;
556}
557
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000558void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000559 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000560 switch (Op.getType()) {
561 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000562 // TODO: Print the other register flags.
563 if (Op.isImplicit())
564 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000565 if (Op.isDead())
566 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000567 if (Op.isKill())
568 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000569 if (Op.isUndef())
570 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000571 if (Op.isEarlyClobber())
572 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000573 if (Op.isDebug())
574 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000575 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000576 // Print the sub register.
577 if (Op.getSubReg() != 0)
578 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000579 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000580 case MachineOperand::MO_Immediate:
581 OS << Op.getImm();
582 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000583 case MachineOperand::MO_CImmediate:
584 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
585 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000586 case MachineOperand::MO_FPImmediate:
587 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
588 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000589 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000590 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000591 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000592 case MachineOperand::MO_FrameIndex:
593 printStackObjectReference(Op.getIndex());
594 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000595 case MachineOperand::MO_ConstantPoolIndex:
596 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000597 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000598 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000599 case MachineOperand::MO_TargetIndex: {
600 OS << "target-index(";
601 if (const auto *Name = getTargetIndexName(
602 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
603 OS << Name;
604 else
605 OS << "<unknown>";
606 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000607 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000608 break;
609 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000610 case MachineOperand::MO_JumpTableIndex:
611 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000612 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000613 case MachineOperand::MO_ExternalSymbol:
614 OS << '$';
615 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000616 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000617 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000618 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000619 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000620 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000621 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000622 case MachineOperand::MO_BlockAddress:
623 OS << "blockaddress(";
624 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
625 MST);
626 OS << ", ";
627 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
628 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000629 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000630 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000631 case MachineOperand::MO_RegisterMask: {
632 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
633 if (RegMaskInfo != RegisterMaskIds.end())
634 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
635 else
636 llvm_unreachable("Can't print this machine register mask yet.");
637 break;
638 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000639 case MachineOperand::MO_Metadata:
640 Op.getMetadata()->printAsOperand(OS, MST);
641 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000642 case MachineOperand::MO_CFIIndex: {
643 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000644 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000645 break;
646 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000647 default:
648 // TODO: Print the other machine operands.
649 llvm_unreachable("Can't print this machine operand at the moment");
650 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000651}
652
Alex Lorenz4af7e612015-08-03 23:08:19 +0000653void MIPrinter::print(const MachineMemOperand &Op) {
654 OS << '(';
655 // TODO: Print operand's other flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000656 if (Op.isVolatile())
657 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000658 if (Op.isNonTemporal())
659 OS << "non-temporal ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000660 if (Op.isLoad())
661 OS << "load ";
662 else {
663 assert(Op.isStore() && "Non load machine operand must be a store");
664 OS << "store ";
665 }
666 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
667 if (const Value *Val = Op.getValue())
668 printIRValueReference(*Val);
669 // TODO: Print PseudoSourceValue.
670 // TODO: Print the base alignment.
671 // TODO: Print the metadata attributes.
672 OS << ')';
673}
674
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000675static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
676 const TargetRegisterInfo *TRI) {
677 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
678 if (Reg == -1) {
679 OS << "<badreg>";
680 return;
681 }
682 printReg(Reg, OS, TRI);
683}
684
685void MIPrinter::print(const MCCFIInstruction &CFI,
686 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000687 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000688 case MCCFIInstruction::OpOffset:
689 OS << ".cfi_offset ";
690 if (CFI.getLabel())
691 OS << "<mcsymbol> ";
692 printCFIRegister(CFI.getRegister(), OS, TRI);
693 OS << ", " << CFI.getOffset();
694 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000695 case MCCFIInstruction::OpDefCfaRegister:
696 OS << ".cfi_def_cfa_register ";
697 if (CFI.getLabel())
698 OS << "<mcsymbol> ";
699 printCFIRegister(CFI.getRegister(), OS, TRI);
700 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000701 case MCCFIInstruction::OpDefCfaOffset:
702 OS << ".cfi_def_cfa_offset ";
703 if (CFI.getLabel())
704 OS << "<mcsymbol> ";
705 OS << CFI.getOffset();
706 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000707 case MCCFIInstruction::OpDefCfa:
708 OS << ".cfi_def_cfa ";
709 if (CFI.getLabel())
710 OS << "<mcsymbol> ";
711 printCFIRegister(CFI.getRegister(), OS, TRI);
712 OS << ", " << CFI.getOffset();
713 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000714 default:
715 // TODO: Print the other CFI Operations.
716 OS << "<unserializable cfi operation>";
717 break;
718 }
719}
720
Alex Lorenz345c1442015-06-15 23:52:35 +0000721void llvm::printMIR(raw_ostream &OS, const Module &M) {
722 yaml::Output Out(OS);
723 Out << const_cast<Module &>(M);
724}
725
726void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
727 MIRPrinter Printer(OS);
728 Printer.print(MF);
729}