blob: 9af64823d0457960d9c86fc64f50cdda311e8295 [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 Lorenzf3db51de2015-06-23 16:35:26 +0000121 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000122 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000123
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000124 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000125};
126
Alex Lorenz345c1442015-06-15 23:52:35 +0000127} // end anonymous namespace
128
129namespace llvm {
130namespace yaml {
131
132/// This struct serializes the LLVM IR module.
133template <> struct BlockScalarTraits<Module> {
134 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
135 Mod.print(OS, nullptr);
136 }
137 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
138 llvm_unreachable("LLVM Module is supposed to be parsed separately");
139 return "";
140 }
141};
142
143} // end namespace yaml
144} // end namespace llvm
145
Alex Lorenz15a00a82015-07-14 21:18:25 +0000146static void printReg(unsigned Reg, raw_ostream &OS,
147 const TargetRegisterInfo *TRI) {
148 // TODO: Print Stack Slots.
149 if (!Reg)
150 OS << '_';
151 else if (TargetRegisterInfo::isVirtualRegister(Reg))
152 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
153 else if (Reg < TRI->getNumRegs())
154 OS << '%' << StringRef(TRI->getName(Reg)).lower();
155 else
156 llvm_unreachable("Can't print this kind of register yet");
157}
158
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000159static void printReg(unsigned Reg, yaml::StringValue &Dest,
160 const TargetRegisterInfo *TRI) {
161 raw_string_ostream OS(Dest.Value);
162 printReg(Reg, OS, TRI);
163}
164
Alex Lorenz345c1442015-06-15 23:52:35 +0000165void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000166 initRegisterMaskIds(MF);
167
Alex Lorenz345c1442015-06-15 23:52:35 +0000168 yaml::MachineFunction YamlMF;
169 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000170 YamlMF.Alignment = MF.getAlignment();
171 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
172 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000173 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000174 ModuleSlotTracker MST(MF.getFunction()->getParent());
175 MST.incorporateFunction(*MF.getFunction());
176 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000177 convertStackObjects(YamlMF, *MF.getFrameInfo(),
178 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000179 if (const auto *ConstantPool = MF.getConstantPool())
180 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000181 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
182 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000183 for (const auto &MBB : MF) {
184 yaml::MachineBasicBlock YamlMBB;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000185 convert(MST, YamlMBB, MBB);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000186 YamlMF.BasicBlocks.push_back(YamlMBB);
187 }
Alex Lorenz345c1442015-06-15 23:52:35 +0000188 yaml::Output Out(OS);
189 Out << YamlMF;
190}
191
Alex Lorenz54565cf2015-06-24 19:56:10 +0000192void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000193 const MachineRegisterInfo &RegInfo,
194 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000195 MF.IsSSA = RegInfo.isSSA();
196 MF.TracksRegLiveness = RegInfo.tracksLiveness();
197 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000198
199 // Print the virtual register definitions.
200 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
201 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
202 yaml::VirtualRegisterDefinition VReg;
203 VReg.ID = I;
204 VReg.Class =
205 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000206 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
207 if (PreferredReg)
208 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000209 MF.VirtualRegisters.push_back(VReg);
210 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000211
212 // Print the live ins.
213 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
214 yaml::MachineFunctionLiveIn LiveIn;
215 printReg(I->first, LiveIn.Register, TRI);
216 if (I->second)
217 printReg(I->second, LiveIn.VirtualRegister, TRI);
218 MF.LiveIns.push_back(LiveIn);
219 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000220}
221
Alex Lorenza6f9a372015-07-29 21:09:09 +0000222void MIRPrinter::convert(ModuleSlotTracker &MST,
223 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000224 const MachineFrameInfo &MFI) {
225 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
226 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
227 YamlMFI.HasStackMap = MFI.hasStackMap();
228 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
229 YamlMFI.StackSize = MFI.getStackSize();
230 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
231 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
232 YamlMFI.AdjustsStack = MFI.adjustsStack();
233 YamlMFI.HasCalls = MFI.hasCalls();
234 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
235 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
236 YamlMFI.HasVAStart = MFI.hasVAStart();
237 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000238 if (MFI.getSavePoint()) {
239 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
240 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
241 .printMBBReference(*MFI.getSavePoint());
242 }
243 if (MFI.getRestorePoint()) {
244 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
245 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
246 .printMBBReference(*MFI.getRestorePoint());
247 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000248}
249
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000250void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000251 const MachineFrameInfo &MFI,
252 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000253 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000254 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000255 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
256 if (MFI.isDeadObjectIndex(I))
257 continue;
258
259 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000260 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000261 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
262 ? yaml::FixedMachineStackObject::SpillSlot
263 : yaml::FixedMachineStackObject::DefaultType;
264 YamlObject.Offset = MFI.getObjectOffset(I);
265 YamlObject.Size = MFI.getObjectSize(I);
266 YamlObject.Alignment = MFI.getObjectAlignment(I);
267 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
268 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
269 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000270 StackObjectOperandMapping.insert(
271 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000272 }
273
274 // Process ordinary stack objects.
275 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000276 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
277 if (MFI.isDeadObjectIndex(I))
278 continue;
279
280 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000281 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000282 if (const auto *Alloca = MFI.getObjectAllocation(I))
283 YamlObject.Name.Value =
284 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000285 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
286 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000287 : MFI.isVariableSizedObjectIndex(I)
288 ? yaml::MachineStackObject::VariableSized
289 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000290 YamlObject.Offset = MFI.getObjectOffset(I);
291 YamlObject.Size = MFI.getObjectSize(I);
292 YamlObject.Alignment = MFI.getObjectAlignment(I);
293
294 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000295 StackObjectOperandMapping.insert(std::make_pair(
296 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000297 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000298
299 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
300 yaml::StringValue Reg;
301 printReg(CSInfo.getReg(), Reg, TRI);
302 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
303 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
304 "Invalid stack object index");
305 const FrameIndexOperand &StackObject = StackObjectInfo->second;
306 if (StackObject.IsFixed)
307 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
308 else
309 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
310 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000311}
312
Alex Lorenzab980492015-07-20 20:51:18 +0000313void MIRPrinter::convert(yaml::MachineFunction &MF,
314 const MachineConstantPool &ConstantPool) {
315 unsigned ID = 0;
316 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
317 // TODO: Serialize target specific constant pool entries.
318 if (Constant.isMachineConstantPoolEntry())
319 llvm_unreachable("Can't print target specific constant pool entries yet");
320
321 yaml::MachineConstantPoolValue YamlConstant;
322 std::string Str;
323 raw_string_ostream StrOS(Str);
324 Constant.Val.ConstVal->printAsOperand(StrOS);
325 YamlConstant.ID = ID++;
326 YamlConstant.Value = StrOS.str();
327 YamlConstant.Alignment = Constant.getAlignment();
328 MF.Constants.push_back(YamlConstant);
329 }
330}
331
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000332void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000333 yaml::MachineJumpTable &YamlJTI,
334 const MachineJumpTableInfo &JTI) {
335 YamlJTI.Kind = JTI.getEntryKind();
336 unsigned ID = 0;
337 for (const auto &Table : JTI.getJumpTables()) {
338 std::string Str;
339 yaml::MachineJumpTable::Entry Entry;
340 Entry.ID = ID++;
341 for (const auto *MBB : Table.MBBs) {
342 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000343 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
344 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000345 Entry.Blocks.push_back(StrOS.str());
346 Str.clear();
347 }
348 YamlJTI.Entries.push_back(Entry);
349 }
350}
351
352void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000353 yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000354 const MachineBasicBlock &MBB) {
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000355 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
356 YamlMBB.ID = (unsigned)MBB.getNumber();
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000357 if (const auto *BB = MBB.getBasicBlock()) {
358 if (BB->hasName()) {
359 YamlMBB.Name.Value = BB->getName();
360 } else {
361 int Slot = MST.getLocalSlot(BB);
362 if (Slot == -1)
363 YamlMBB.IRBlock.Value = "<badref>";
364 else
365 YamlMBB.IRBlock.Value = (Twine("%ir-block.") + Twine(Slot)).str();
366 }
367 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000368 YamlMBB.Alignment = MBB.getAlignment();
369 YamlMBB.AddressTaken = MBB.hasAddressTaken();
370 YamlMBB.IsLandingPad = MBB.isLandingPad();
Alex Lorenzeb5112b2015-06-30 18:32:02 +0000371 for (const auto *SuccMBB : MBB.successors()) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000372 std::string Str;
373 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000374 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
375 .printMBBReference(*SuccMBB);
Alex Lorenzf09df002015-06-30 18:16:42 +0000376 YamlMBB.Successors.push_back(StrOS.str());
377 }
Alex Lorenz618b2832015-07-30 16:54:38 +0000378 if (MBB.hasSuccessorWeights()) {
379 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I)
380 YamlMBB.SuccessorWeights.push_back(
381 yaml::UnsignedValue(MBB.getSuccWeight(I)));
382 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000383 // Print the live in registers.
384 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
385 assert(TRI && "Expected target register info");
386 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
387 std::string Str;
388 raw_string_ostream StrOS(Str);
389 printReg(*I, StrOS, TRI);
390 YamlMBB.LiveIns.push_back(StrOS.str());
391 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000392 // Print the machine instructions.
393 YamlMBB.Instructions.reserve(MBB.size());
394 std::string Str;
395 for (const auto &MI : MBB) {
396 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000397 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping).print(MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000398 YamlMBB.Instructions.push_back(StrOS.str());
399 Str.clear();
400 }
401}
402
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000403void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
404 const auto *TRI = MF.getSubtarget().getRegisterInfo();
405 unsigned I = 0;
406 for (const uint32_t *Mask : TRI->getRegMasks())
407 RegisterMaskIds.insert(std::make_pair(Mask, I++));
408}
409
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000410void MIPrinter::print(const MachineInstr &MI) {
411 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000412 const auto *TRI = SubTarget.getRegisterInfo();
413 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000414 const auto *TII = SubTarget.getInstrInfo();
415 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000416 if (MI.isCFIInstruction())
417 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000418
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000419 unsigned I = 0, E = MI.getNumOperands();
420 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
421 !MI.getOperand(I).isImplicit();
422 ++I) {
423 if (I)
424 OS << ", ";
425 print(MI.getOperand(I), TRI);
426 }
427
428 if (I)
429 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000430 if (MI.getFlag(MachineInstr::FrameSetup))
431 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000432 OS << TII->getName(MI.getOpcode());
Alex Lorenz4af7e612015-08-03 23:08:19 +0000433 // TODO: Print the bundling instruction flags.
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000434 if (I < E)
435 OS << ' ';
436
437 bool NeedComma = false;
438 for (; I < E; ++I) {
439 if (NeedComma)
440 OS << ", ";
441 print(MI.getOperand(I), TRI);
442 NeedComma = true;
443 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000444
445 if (MI.getDebugLoc()) {
446 if (NeedComma)
447 OS << ',';
448 OS << " debug-location ";
449 MI.getDebugLoc()->printAsOperand(OS, MST);
450 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000451
452 if (!MI.memoperands_empty()) {
453 OS << " :: ";
454 bool NeedComma = false;
455 for (const auto *Op : MI.memoperands()) {
456 if (NeedComma)
457 OS << ", ";
458 print(*Op);
459 NeedComma = true;
460 }
461 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000462}
463
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000464void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
465 OS << "%bb." << MBB.getNumber();
466 if (const auto *BB = MBB.getBasicBlock()) {
467 if (BB->hasName())
468 OS << '.' << BB->getName();
469 }
470}
471
Alex Lorenzdeb53492015-07-28 17:28:03 +0000472void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
473 OS << "%ir-block.";
474 if (BB.hasName()) {
475 printLLVMNameWithoutPrefix(OS, BB.getName());
476 return;
477 }
478 int Slot = MST.getLocalSlot(&BB);
479 if (Slot == -1)
480 OS << "<badref>";
481 else
482 OS << Slot;
483}
484
Alex Lorenz4af7e612015-08-03 23:08:19 +0000485void MIPrinter::printIRValueReference(const Value &V) {
486 OS << "%ir.";
487 if (V.hasName()) {
488 printLLVMNameWithoutPrefix(OS, V.getName());
489 return;
490 }
491 // TODO: Serialize the unnamed IR value references.
492 OS << "<unserializable ir value>";
493}
494
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000495void MIPrinter::printStackObjectReference(int FrameIndex) {
496 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
497 assert(ObjectInfo != StackObjectOperandMapping.end() &&
498 "Invalid frame index");
499 const FrameIndexOperand &Operand = ObjectInfo->second;
500 if (Operand.IsFixed) {
501 OS << "%fixed-stack." << Operand.ID;
502 return;
503 }
504 OS << "%stack." << Operand.ID;
505 if (!Operand.Name.empty())
506 OS << '.' << Operand.Name;
507}
508
Alex Lorenz5672a892015-08-05 22:26:15 +0000509void MIPrinter::printOffset(int64_t Offset) {
510 if (Offset == 0)
511 return;
512 if (Offset < 0) {
513 OS << " - " << -Offset;
514 return;
515 }
516 OS << " + " << Offset;
517}
518
Alex Lorenzef5c1962015-07-28 23:02:45 +0000519static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
520 const auto *TII = MF.getSubtarget().getInstrInfo();
521 assert(TII && "expected instruction info");
522 auto Indices = TII->getSerializableTargetIndices();
523 for (const auto &I : Indices) {
524 if (I.first == Index) {
525 return I.second;
526 }
527 }
528 return nullptr;
529}
530
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000531void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
532 switch (Op.getType()) {
533 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000534 // TODO: Print the other register flags.
535 if (Op.isImplicit())
536 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000537 if (Op.isDead())
538 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000539 if (Op.isKill())
540 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000541 if (Op.isUndef())
542 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000543 if (Op.isEarlyClobber())
544 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000545 if (Op.isDebug())
546 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000547 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000548 // Print the sub register.
549 if (Op.getSubReg() != 0)
550 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000551 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000552 case MachineOperand::MO_Immediate:
553 OS << Op.getImm();
554 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000555 case MachineOperand::MO_CImmediate:
556 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
557 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000558 case MachineOperand::MO_FPImmediate:
559 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
560 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000561 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000562 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000563 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000564 case MachineOperand::MO_FrameIndex:
565 printStackObjectReference(Op.getIndex());
566 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000567 case MachineOperand::MO_ConstantPoolIndex:
568 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000569 printOffset(Op.getOffset());
570 // TODO: Print the target flags.
Alex Lorenzab980492015-07-20 20:51:18 +0000571 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000572 case MachineOperand::MO_TargetIndex: {
573 OS << "target-index(";
574 if (const auto *Name = getTargetIndexName(
575 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
576 OS << Name;
577 else
578 OS << "<unknown>";
579 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000580 printOffset(Op.getOffset());
581 // TODO: Print the target flags.
Alex Lorenzef5c1962015-07-28 23:02:45 +0000582 break;
583 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000584 case MachineOperand::MO_JumpTableIndex:
585 OS << "%jump-table." << Op.getIndex();
586 // TODO: Print target flags.
587 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000588 case MachineOperand::MO_ExternalSymbol:
589 OS << '$';
590 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000591 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000592 // TODO: Print the target flags.
593 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000594 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000595 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000596 printOffset(Op.getOffset());
597 // TODO: Print the target flags.
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000598 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000599 case MachineOperand::MO_BlockAddress:
600 OS << "blockaddress(";
601 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
602 MST);
603 OS << ", ";
604 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
605 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000606 printOffset(Op.getOffset());
607 // TODO: Print the target flags.
Alex Lorenzdeb53492015-07-28 17:28:03 +0000608 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000609 case MachineOperand::MO_RegisterMask: {
610 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
611 if (RegMaskInfo != RegisterMaskIds.end())
612 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
613 else
614 llvm_unreachable("Can't print this machine register mask yet.");
615 break;
616 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000617 case MachineOperand::MO_Metadata:
618 Op.getMetadata()->printAsOperand(OS, MST);
619 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000620 case MachineOperand::MO_CFIIndex: {
621 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000622 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000623 break;
624 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000625 default:
626 // TODO: Print the other machine operands.
627 llvm_unreachable("Can't print this machine operand at the moment");
628 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000629}
630
Alex Lorenz4af7e612015-08-03 23:08:19 +0000631void MIPrinter::print(const MachineMemOperand &Op) {
632 OS << '(';
633 // TODO: Print operand's other flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000634 if (Op.isVolatile())
635 OS << "volatile ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000636 if (Op.isLoad())
637 OS << "load ";
638 else {
639 assert(Op.isStore() && "Non load machine operand must be a store");
640 OS << "store ";
641 }
642 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
643 if (const Value *Val = Op.getValue())
644 printIRValueReference(*Val);
645 // TODO: Print PseudoSourceValue.
646 // TODO: Print the base alignment.
647 // TODO: Print the metadata attributes.
648 OS << ')';
649}
650
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000651static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
652 const TargetRegisterInfo *TRI) {
653 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
654 if (Reg == -1) {
655 OS << "<badreg>";
656 return;
657 }
658 printReg(Reg, OS, TRI);
659}
660
661void MIPrinter::print(const MCCFIInstruction &CFI,
662 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000663 switch (CFI.getOperation()) {
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000664 case MCCFIInstruction::OpOffset:
665 OS << ".cfi_offset ";
666 if (CFI.getLabel())
667 OS << "<mcsymbol> ";
668 printCFIRegister(CFI.getRegister(), OS, TRI);
669 OS << ", " << CFI.getOffset();
670 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000671 case MCCFIInstruction::OpDefCfaRegister:
672 OS << ".cfi_def_cfa_register ";
673 if (CFI.getLabel())
674 OS << "<mcsymbol> ";
675 printCFIRegister(CFI.getRegister(), OS, TRI);
676 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000677 case MCCFIInstruction::OpDefCfaOffset:
678 OS << ".cfi_def_cfa_offset ";
679 if (CFI.getLabel())
680 OS << "<mcsymbol> ";
681 OS << CFI.getOffset();
682 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000683 case MCCFIInstruction::OpDefCfa:
684 OS << ".cfi_def_cfa ";
685 if (CFI.getLabel())
686 OS << "<mcsymbol> ";
687 printCFIRegister(CFI.getRegister(), OS, TRI);
688 OS << ", " << CFI.getOffset();
689 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000690 default:
691 // TODO: Print the other CFI Operations.
692 OS << "<unserializable cfi operation>";
693 break;
694 }
695}
696
Alex Lorenz345c1442015-06-15 23:52:35 +0000697void llvm::printMIR(raw_ostream &OS, const Module &M) {
698 yaml::Output Out(OS);
699 Out << const_cast<Module &>(M);
700}
701
702void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
703 MIRPrinter Printer(OS);
704 Printer.print(MF);
705}