blob: 849866a10404ac7124b5a90bf6b58ba17dbab2b7 [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
Matthias Braun89401142017-05-05 21:09:30 +000015#include "llvm/CodeGen/MIRPrinter.h"
16
Alex Lorenz345c1442015-06-15 23:52:35 +000017#include "llvm/ADT/STLExtras.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000018#include "llvm/ADT/SmallBitVector.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000019#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
20#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000023#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000027#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000028#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000029#include "llvm/IR/DebugInfo.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000030#include "llvm/IR/IRPrintingPasses.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000031#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000032#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000033#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000034#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000035#include "llvm/MC/MCSymbol.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000036#include "llvm/Support/Format.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000037#include "llvm/Support/MemoryBuffer.h"
Matthias Braun89401142017-05-05 21:09:30 +000038#include "llvm/Support/Options.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000039#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000040#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000041#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000042#include "llvm/Target/TargetIntrinsicInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000043#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000044
45using namespace llvm;
46
Matthias Braun89401142017-05-05 21:09:30 +000047static cl::opt<bool> SimplifyMIR("simplify-mir",
48 cl::desc("Leave out unnecessary information when printing MIR"));
49
Alex Lorenz345c1442015-06-15 23:52:35 +000050namespace {
51
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000052/// This structure describes how to print out stack object references.
53struct FrameIndexOperand {
54 std::string Name;
55 unsigned ID;
56 bool IsFixed;
57
58 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
59 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
60
61 /// Return an ordinary stack object reference.
62 static FrameIndexOperand create(StringRef Name, unsigned ID) {
63 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
64 }
65
66 /// Return a fixed stack object reference.
67 static FrameIndexOperand createFixed(unsigned ID) {
68 return FrameIndexOperand("", ID, /*IsFixed=*/true);
69 }
70};
71
Alex Lorenz618b2832015-07-30 16:54:38 +000072} // end anonymous namespace
73
74namespace llvm {
75
Alex Lorenz345c1442015-06-15 23:52:35 +000076/// This class prints out the machine functions using the MIR serialization
77/// format.
78class MIRPrinter {
79 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000080 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000081 /// Maps from stack object indices to operand indices which will be used when
82 /// printing frame index machine operands.
83 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000084
85public:
86 MIRPrinter(raw_ostream &OS) : OS(OS) {}
87
88 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000089
Alex Lorenz28148ba2015-07-09 22:23:13 +000090 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
91 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000092 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
93 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000094 void convert(yaml::MachineFunction &MF,
95 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000096 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
97 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +000098 void convertStackObjects(yaml::MachineFunction &YMF,
99 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000100
101private:
102 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000103};
104
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105/// This class prints out the machine instructions using the MIR serialization
106/// format.
107class MIPrinter {
108 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000109 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000110 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000111 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000112
Matthias Braun89401142017-05-05 21:09:30 +0000113 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
114 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
115
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000116public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000117 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000118 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
119 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
120 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
121 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000122
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000123 void print(const MachineBasicBlock &MBB);
124
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000125 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000126 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000127 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000128 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000129 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000130 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000131 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000132 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000133 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000134 LLT TypeToPrint, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000135 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000136
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000137 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000138};
139
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000140} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000141
142namespace llvm {
143namespace yaml {
144
145/// This struct serializes the LLVM IR module.
146template <> struct BlockScalarTraits<Module> {
147 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
148 Mod.print(OS, nullptr);
149 }
150 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
151 llvm_unreachable("LLVM Module is supposed to be parsed separately");
152 return "";
153 }
154};
155
156} // end namespace yaml
157} // end namespace llvm
158
Alex Lorenz15a00a82015-07-14 21:18:25 +0000159static void printReg(unsigned Reg, raw_ostream &OS,
160 const TargetRegisterInfo *TRI) {
161 // TODO: Print Stack Slots.
162 if (!Reg)
163 OS << '_';
164 else if (TargetRegisterInfo::isVirtualRegister(Reg))
165 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
166 else if (Reg < TRI->getNumRegs())
167 OS << '%' << StringRef(TRI->getName(Reg)).lower();
168 else
169 llvm_unreachable("Can't print this kind of register yet");
170}
171
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000172static void printReg(unsigned Reg, yaml::StringValue &Dest,
173 const TargetRegisterInfo *TRI) {
174 raw_string_ostream OS(Dest.Value);
175 printReg(Reg, OS, TRI);
176}
177
Alex Lorenz345c1442015-06-15 23:52:35 +0000178void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000179 initRegisterMaskIds(MF);
180
Alex Lorenz345c1442015-06-15 23:52:35 +0000181 yaml::MachineFunction YamlMF;
182 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000183 YamlMF.Alignment = MF.getAlignment();
184 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000185
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000186 YamlMF.Legalized = MF.getProperties().hasProperty(
187 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000188 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
189 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000190 YamlMF.Selected = MF.getProperties().hasProperty(
191 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000192
Alex Lorenz28148ba2015-07-09 22:23:13 +0000193 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000194 ModuleSlotTracker MST(MF.getFunction()->getParent());
195 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000196 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000197 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000198 if (const auto *ConstantPool = MF.getConstantPool())
199 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000200 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
201 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000202 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
203 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000204 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000205 if (IsNewlineNeeded)
206 StrOS << "\n";
207 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
208 .print(MBB);
209 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000210 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000211 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000212 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000213 if (!SimplifyMIR)
214 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000215 Out << YamlMF;
216}
217
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000218static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
219 const TargetRegisterInfo *TRI) {
220 assert(RegMask && "Can't print an empty register mask");
221 OS << StringRef("CustomRegMask(");
222
223 bool IsRegInRegMaskFound = false;
224 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
225 // Check whether the register is asserted in regmask.
226 if (RegMask[I / 32] & (1u << (I % 32))) {
227 if (IsRegInRegMaskFound)
228 OS << ',';
229 printReg(I, OS, TRI);
230 IsRegInRegMaskFound = true;
231 }
232 }
233
234 OS << ')';
235}
236
Alex Lorenz54565cf2015-06-24 19:56:10 +0000237void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000238 const MachineRegisterInfo &RegInfo,
239 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000240 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000241
242 // Print the virtual register definitions.
243 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
244 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
245 yaml::VirtualRegisterDefinition VReg;
246 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000247 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000248 VReg.Class =
249 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000250 else if (RegInfo.getRegBankOrNull(Reg))
251 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000252 else {
253 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000254 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000255 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000256 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000257 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
258 if (PreferredReg)
259 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000260 MF.VirtualRegisters.push_back(VReg);
261 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000262
263 // Print the live ins.
264 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
265 yaml::MachineFunctionLiveIn LiveIn;
266 printReg(I->first, LiveIn.Register, TRI);
267 if (I->second)
268 printReg(I->second, LiveIn.VirtualRegister, TRI);
269 MF.LiveIns.push_back(LiveIn);
270 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000271
272 // Prints the callee saved registers.
273 if (RegInfo.isUpdatedCSRsInitialized()) {
274 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
275 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
276 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000277 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000278 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000279 CalleeSavedRegisters.push_back(Reg);
280 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000281 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000282 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000283}
284
Alex Lorenza6f9a372015-07-29 21:09:09 +0000285void MIRPrinter::convert(ModuleSlotTracker &MST,
286 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000287 const MachineFrameInfo &MFI) {
288 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
289 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
290 YamlMFI.HasStackMap = MFI.hasStackMap();
291 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
292 YamlMFI.StackSize = MFI.getStackSize();
293 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
294 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
295 YamlMFI.AdjustsStack = MFI.adjustsStack();
296 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000297 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
298 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000299 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
300 YamlMFI.HasVAStart = MFI.hasVAStart();
301 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000302 if (MFI.getSavePoint()) {
303 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
304 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
305 .printMBBReference(*MFI.getSavePoint());
306 }
307 if (MFI.getRestorePoint()) {
308 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
309 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
310 .printMBBReference(*MFI.getRestorePoint());
311 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000312}
313
Matthias Braunef331ef2016-11-30 23:48:50 +0000314void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
315 const MachineFunction &MF,
316 ModuleSlotTracker &MST) {
317 const MachineFrameInfo &MFI = MF.getFrameInfo();
318 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000319 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000320 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000321 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
322 if (MFI.isDeadObjectIndex(I))
323 continue;
324
325 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000326 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000327 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
328 ? yaml::FixedMachineStackObject::SpillSlot
329 : yaml::FixedMachineStackObject::DefaultType;
330 YamlObject.Offset = MFI.getObjectOffset(I);
331 YamlObject.Size = MFI.getObjectSize(I);
332 YamlObject.Alignment = MFI.getObjectAlignment(I);
333 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
334 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000335 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000336 StackObjectOperandMapping.insert(
337 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000338 }
339
340 // Process ordinary stack objects.
341 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000342 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
343 if (MFI.isDeadObjectIndex(I))
344 continue;
345
346 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000347 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000348 if (const auto *Alloca = MFI.getObjectAllocation(I))
349 YamlObject.Name.Value =
350 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000351 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
352 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000353 : MFI.isVariableSizedObjectIndex(I)
354 ? yaml::MachineStackObject::VariableSized
355 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000356 YamlObject.Offset = MFI.getObjectOffset(I);
357 YamlObject.Size = MFI.getObjectSize(I);
358 YamlObject.Alignment = MFI.getObjectAlignment(I);
359
Matthias Braunef331ef2016-11-30 23:48:50 +0000360 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000361 StackObjectOperandMapping.insert(std::make_pair(
362 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000363 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000364
365 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
366 yaml::StringValue Reg;
367 printReg(CSInfo.getReg(), Reg, TRI);
368 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
369 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
370 "Invalid stack object index");
371 const FrameIndexOperand &StackObject = StackObjectInfo->second;
372 if (StackObject.IsFixed)
Matthias Braunef331ef2016-11-30 23:48:50 +0000373 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000374 else
Matthias Braunef331ef2016-11-30 23:48:50 +0000375 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000376 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000377 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
378 auto LocalObject = MFI.getLocalFrameObjectMap(I);
379 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
380 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
381 "Invalid stack object index");
382 const FrameIndexOperand &StackObject = StackObjectInfo->second;
383 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000384 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000385 }
Alex Lorenza314d812015-08-18 22:26:26 +0000386
387 // Print the stack object references in the frame information class after
388 // converting the stack objects.
389 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000390 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000391 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
392 .printStackObjectReference(MFI.getStackProtectorIndex());
393 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000394
395 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000396 for (const MachineFunction::VariableDbgInfo &DebugVar :
397 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000398 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
399 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
400 "Invalid stack object index");
401 const FrameIndexOperand &StackObject = StackObjectInfo->second;
402 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000403 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000404 {
405 raw_string_ostream StrOS(Object.DebugVar.Value);
406 DebugVar.Var->printAsOperand(StrOS, MST);
407 }
408 {
409 raw_string_ostream StrOS(Object.DebugExpr.Value);
410 DebugVar.Expr->printAsOperand(StrOS, MST);
411 }
412 {
413 raw_string_ostream StrOS(Object.DebugLoc.Value);
414 DebugVar.Loc->printAsOperand(StrOS, MST);
415 }
416 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000417}
418
Alex Lorenzab980492015-07-20 20:51:18 +0000419void MIRPrinter::convert(yaml::MachineFunction &MF,
420 const MachineConstantPool &ConstantPool) {
421 unsigned ID = 0;
422 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
423 // TODO: Serialize target specific constant pool entries.
424 if (Constant.isMachineConstantPoolEntry())
425 llvm_unreachable("Can't print target specific constant pool entries yet");
426
427 yaml::MachineConstantPoolValue YamlConstant;
428 std::string Str;
429 raw_string_ostream StrOS(Str);
430 Constant.Val.ConstVal->printAsOperand(StrOS);
431 YamlConstant.ID = ID++;
432 YamlConstant.Value = StrOS.str();
433 YamlConstant.Alignment = Constant.getAlignment();
434 MF.Constants.push_back(YamlConstant);
435 }
436}
437
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000438void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000439 yaml::MachineJumpTable &YamlJTI,
440 const MachineJumpTableInfo &JTI) {
441 YamlJTI.Kind = JTI.getEntryKind();
442 unsigned ID = 0;
443 for (const auto &Table : JTI.getJumpTables()) {
444 std::string Str;
445 yaml::MachineJumpTable::Entry Entry;
446 Entry.ID = ID++;
447 for (const auto *MBB : Table.MBBs) {
448 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000449 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
450 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000451 Entry.Blocks.push_back(StrOS.str());
452 Str.clear();
453 }
454 YamlJTI.Entries.push_back(Entry);
455 }
456}
457
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000458void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
459 const auto *TRI = MF.getSubtarget().getRegisterInfo();
460 unsigned I = 0;
461 for (const uint32_t *Mask : TRI->getRegMasks())
462 RegisterMaskIds.insert(std::make_pair(Mask, I++));
463}
464
Matthias Braun89401142017-05-05 21:09:30 +0000465void llvm::guessSuccessors(const MachineBasicBlock &MBB,
466 SmallVectorImpl<MachineBasicBlock*> &Result,
467 bool &IsFallthrough) {
468 SmallPtrSet<MachineBasicBlock*,8> Seen;
469
470 for (const MachineInstr &MI : MBB) {
471 if (MI.isPHI())
472 continue;
473 for (const MachineOperand &MO : MI.operands()) {
474 if (!MO.isMBB())
475 continue;
476 MachineBasicBlock *Succ = MO.getMBB();
477 auto RP = Seen.insert(Succ);
478 if (RP.second)
479 Result.push_back(Succ);
480 }
481 }
482 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
483 IsFallthrough = I == MBB.end() || !I->isBarrier();
484}
485
486bool
487MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
488 if (MBB.succ_size() <= 1)
489 return true;
490 if (!MBB.hasSuccessorProbabilities())
491 return true;
492
493 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
494 MBB.Probs.end());
495 BranchProbability::normalizeProbabilities(Normalized.begin(),
496 Normalized.end());
497 SmallVector<BranchProbability,8> Equal(Normalized.size());
498 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
499
500 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
501}
502
503bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
504 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
505 bool GuessedFallthrough;
506 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
507 if (GuessedFallthrough) {
508 const MachineFunction &MF = *MBB.getParent();
509 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
510 if (NextI != MF.end()) {
511 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
512 if (!is_contained(GuessedSuccs, Next))
513 GuessedSuccs.push_back(Next);
514 }
515 }
516 if (GuessedSuccs.size() != MBB.succ_size())
517 return false;
518 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
519}
520
521
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000522void MIPrinter::print(const MachineBasicBlock &MBB) {
523 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
524 OS << "bb." << MBB.getNumber();
525 bool HasAttributes = false;
526 if (const auto *BB = MBB.getBasicBlock()) {
527 if (BB->hasName()) {
528 OS << "." << BB->getName();
529 } else {
530 HasAttributes = true;
531 OS << " (";
532 int Slot = MST.getLocalSlot(BB);
533 if (Slot == -1)
534 OS << "<ir-block badref>";
535 else
536 OS << (Twine("%ir-block.") + Twine(Slot)).str();
537 }
538 }
539 if (MBB.hasAddressTaken()) {
540 OS << (HasAttributes ? ", " : " (");
541 OS << "address-taken";
542 HasAttributes = true;
543 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000544 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000545 OS << (HasAttributes ? ", " : " (");
546 OS << "landing-pad";
547 HasAttributes = true;
548 }
549 if (MBB.getAlignment()) {
550 OS << (HasAttributes ? ", " : " (");
551 OS << "align " << MBB.getAlignment();
552 HasAttributes = true;
553 }
554 if (HasAttributes)
555 OS << ")";
556 OS << ":\n";
557
558 bool HasLineAttributes = false;
559 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000560 bool canPredictProbs = canPredictBranchProbabilities(MBB);
561 if (!MBB.succ_empty() && (!SimplifyMIR || !canPredictProbs ||
562 !canPredictSuccessors(MBB))) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000563 OS.indent(2) << "successors: ";
564 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
565 if (I != MBB.succ_begin())
566 OS << ", ";
567 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000568 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000569 OS << '('
570 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
571 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000572 }
573 OS << "\n";
574 HasLineAttributes = true;
575 }
576
577 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000578 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
579 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
580 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000581 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000582 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000583 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000584 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000585 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000586 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000587 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000588 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000589 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000590 }
591 OS << "\n";
592 HasLineAttributes = true;
593 }
594
595 if (HasLineAttributes)
596 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000597 bool IsInBundle = false;
598 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
599 const MachineInstr &MI = *I;
600 if (IsInBundle && !MI.isInsideBundle()) {
601 OS.indent(2) << "}\n";
602 IsInBundle = false;
603 }
604 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000605 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000606 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
607 OS << " {";
608 IsInBundle = true;
609 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000610 OS << "\n";
611 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000612 if (IsInBundle)
613 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000614}
615
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000616/// Return true when an instruction has tied register that can't be determined
617/// by the instruction's descriptor.
618static bool hasComplexRegisterTies(const MachineInstr &MI) {
619 const MCInstrDesc &MCID = MI.getDesc();
620 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
621 const auto &Operand = MI.getOperand(I);
622 if (!Operand.isReg() || Operand.isDef())
623 // Ignore the defined registers as MCID marks only the uses as tied.
624 continue;
625 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
626 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
627 if (ExpectedTiedIdx != TiedIdx)
628 return true;
629 }
630 return false;
631}
632
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000633static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
634 SmallBitVector &PrintedTypes,
635 const MachineRegisterInfo &MRI) {
636 const MachineOperand &Op = MI.getOperand(OpIdx);
637 if (!Op.isReg())
638 return LLT{};
639
640 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
641 return MRI.getType(Op.getReg());
642
643 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
644 if (!OpInfo.isGenericType())
645 return MRI.getType(Op.getReg());
646
647 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
648 return LLT{};
649
650 PrintedTypes.set(OpInfo.getGenericTypeIndex());
651 return MRI.getType(Op.getReg());
652}
653
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000654void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000655 const auto *MF = MI.getParent()->getParent();
656 const auto &MRI = MF->getRegInfo();
657 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000658 const auto *TRI = SubTarget.getRegisterInfo();
659 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000660 const auto *TII = SubTarget.getInstrInfo();
661 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000662 if (MI.isCFIInstruction())
663 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000664
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000665 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000666 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000667 unsigned I = 0, E = MI.getNumOperands();
668 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
669 !MI.getOperand(I).isImplicit();
670 ++I) {
671 if (I)
672 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000673 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
674 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000675 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000676 }
677
678 if (I)
679 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000680 if (MI.getFlag(MachineInstr::FrameSetup))
681 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000682 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000683 if (I < E)
684 OS << ' ';
685
686 bool NeedComma = false;
687 for (; I < E; ++I) {
688 if (NeedComma)
689 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000690 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
691 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000692 NeedComma = true;
693 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000694
695 if (MI.getDebugLoc()) {
696 if (NeedComma)
697 OS << ',';
698 OS << " debug-location ";
699 MI.getDebugLoc()->printAsOperand(OS, MST);
700 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000701
702 if (!MI.memoperands_empty()) {
703 OS << " :: ";
704 bool NeedComma = false;
705 for (const auto *Op : MI.memoperands()) {
706 if (NeedComma)
707 OS << ", ";
708 print(*Op);
709 NeedComma = true;
710 }
711 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000712}
713
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000714void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
715 OS << "%bb." << MBB.getNumber();
716 if (const auto *BB = MBB.getBasicBlock()) {
717 if (BB->hasName())
718 OS << '.' << BB->getName();
719 }
720}
721
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000722static void printIRSlotNumber(raw_ostream &OS, int Slot) {
723 if (Slot == -1)
724 OS << "<badref>";
725 else
726 OS << Slot;
727}
728
Alex Lorenzdeb53492015-07-28 17:28:03 +0000729void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
730 OS << "%ir-block.";
731 if (BB.hasName()) {
732 printLLVMNameWithoutPrefix(OS, BB.getName());
733 return;
734 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000735 const Function *F = BB.getParent();
736 int Slot;
737 if (F == MST.getCurrentFunction()) {
738 Slot = MST.getLocalSlot(&BB);
739 } else {
740 ModuleSlotTracker CustomMST(F->getParent(),
741 /*ShouldInitializeAllMetadata=*/false);
742 CustomMST.incorporateFunction(*F);
743 Slot = CustomMST.getLocalSlot(&BB);
744 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000745 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000746}
747
Alex Lorenz4af7e612015-08-03 23:08:19 +0000748void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000749 if (isa<GlobalValue>(V)) {
750 V.printAsOperand(OS, /*PrintType=*/false, MST);
751 return;
752 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000753 if (isa<Constant>(V)) {
754 // Machine memory operands can load/store to/from constant value pointers.
755 OS << '`';
756 V.printAsOperand(OS, /*PrintType=*/true, MST);
757 OS << '`';
758 return;
759 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000760 OS << "%ir.";
761 if (V.hasName()) {
762 printLLVMNameWithoutPrefix(OS, V.getName());
763 return;
764 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000765 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000766}
767
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000768void MIPrinter::printStackObjectReference(int FrameIndex) {
769 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
770 assert(ObjectInfo != StackObjectOperandMapping.end() &&
771 "Invalid frame index");
772 const FrameIndexOperand &Operand = ObjectInfo->second;
773 if (Operand.IsFixed) {
774 OS << "%fixed-stack." << Operand.ID;
775 return;
776 }
777 OS << "%stack." << Operand.ID;
778 if (!Operand.Name.empty())
779 OS << '.' << Operand.Name;
780}
781
Alex Lorenz5672a892015-08-05 22:26:15 +0000782void MIPrinter::printOffset(int64_t Offset) {
783 if (Offset == 0)
784 return;
785 if (Offset < 0) {
786 OS << " - " << -Offset;
787 return;
788 }
789 OS << " + " << Offset;
790}
791
Alex Lorenz49873a82015-08-06 00:44:07 +0000792static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
793 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
794 for (const auto &I : Flags) {
795 if (I.first == TF) {
796 return I.second;
797 }
798 }
799 return nullptr;
800}
801
802void MIPrinter::printTargetFlags(const MachineOperand &Op) {
803 if (!Op.getTargetFlags())
804 return;
805 const auto *TII =
806 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
807 assert(TII && "expected instruction info");
808 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
809 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000810 const bool HasDirectFlags = Flags.first;
811 const bool HasBitmaskFlags = Flags.second;
812 if (!HasDirectFlags && !HasBitmaskFlags) {
813 OS << "<unknown>) ";
814 return;
815 }
816 if (HasDirectFlags) {
817 if (const auto *Name = getTargetFlagName(TII, Flags.first))
818 OS << Name;
819 else
820 OS << "<unknown target flag>";
821 }
822 if (!HasBitmaskFlags) {
823 OS << ") ";
824 return;
825 }
826 bool IsCommaNeeded = HasDirectFlags;
827 unsigned BitMask = Flags.second;
828 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
829 for (const auto &Mask : BitMasks) {
830 // Check if the flag's bitmask has the bits of the current mask set.
831 if ((BitMask & Mask.first) == Mask.first) {
832 if (IsCommaNeeded)
833 OS << ", ";
834 IsCommaNeeded = true;
835 OS << Mask.second;
836 // Clear the bits which were serialized from the flag's bitmask.
837 BitMask &= ~(Mask.first);
838 }
839 }
840 if (BitMask) {
841 // When the resulting flag's bitmask isn't zero, we know that we didn't
842 // serialize all of the bit flags.
843 if (IsCommaNeeded)
844 OS << ", ";
845 OS << "<unknown bitmask target flag>";
846 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000847 OS << ") ";
848}
849
Alex Lorenzef5c1962015-07-28 23:02:45 +0000850static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
851 const auto *TII = MF.getSubtarget().getInstrInfo();
852 assert(TII && "expected instruction info");
853 auto Indices = TII->getSerializableTargetIndices();
854 for (const auto &I : Indices) {
855 if (I.first == Index) {
856 return I.second;
857 }
858 }
859 return nullptr;
860}
861
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000862void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000863 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
864 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000865 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000866 switch (Op.getType()) {
867 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000868 if (Op.isImplicit())
869 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000870 else if (!IsDef && Op.isDef())
871 // Print the 'def' flag only when the operand is defined after '='.
872 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000873 if (Op.isInternalRead())
874 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000875 if (Op.isDead())
876 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000877 if (Op.isKill())
878 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000879 if (Op.isUndef())
880 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000881 if (Op.isEarlyClobber())
882 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000883 if (Op.isDebug())
884 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000885 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000886 // Print the sub register.
887 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000888 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000889 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
890 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000891 if (TypeToPrint.isValid())
892 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000893 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000894 case MachineOperand::MO_Immediate:
895 OS << Op.getImm();
896 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000897 case MachineOperand::MO_CImmediate:
898 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
899 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000900 case MachineOperand::MO_FPImmediate:
901 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
902 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000903 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000904 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000905 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000906 case MachineOperand::MO_FrameIndex:
907 printStackObjectReference(Op.getIndex());
908 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000909 case MachineOperand::MO_ConstantPoolIndex:
910 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000911 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000912 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000913 case MachineOperand::MO_TargetIndex: {
914 OS << "target-index(";
915 if (const auto *Name = getTargetIndexName(
916 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
917 OS << Name;
918 else
919 OS << "<unknown>";
920 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000921 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000922 break;
923 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000924 case MachineOperand::MO_JumpTableIndex:
925 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000926 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000927 case MachineOperand::MO_ExternalSymbol:
928 OS << '$';
929 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000930 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000931 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000932 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000933 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000934 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000935 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000936 case MachineOperand::MO_BlockAddress:
937 OS << "blockaddress(";
938 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
939 MST);
940 OS << ", ";
941 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
942 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000943 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000944 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000945 case MachineOperand::MO_RegisterMask: {
946 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
947 if (RegMaskInfo != RegisterMaskIds.end())
948 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
949 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000950 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000951 break;
952 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000953 case MachineOperand::MO_RegisterLiveOut: {
954 const uint32_t *RegMask = Op.getRegLiveOut();
955 OS << "liveout(";
956 bool IsCommaNeeded = false;
957 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
958 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
959 if (IsCommaNeeded)
960 OS << ", ";
961 printReg(Reg, OS, TRI);
962 IsCommaNeeded = true;
963 }
964 }
965 OS << ")";
966 break;
967 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000968 case MachineOperand::MO_Metadata:
969 Op.getMetadata()->printAsOperand(OS, MST);
970 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000971 case MachineOperand::MO_MCSymbol:
972 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
973 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000974 case MachineOperand::MO_CFIIndex: {
Matthias Braunf23ef432016-11-30 23:48:42 +0000975 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
976 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000977 break;
978 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000979 case MachineOperand::MO_IntrinsicID: {
980 Intrinsic::ID ID = Op.getIntrinsicID();
981 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000982 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000983 else {
984 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
985 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
986 OS << "intrinsic(@" << TII->getName(ID) << ')';
987 }
988 break;
989 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000990 case MachineOperand::MO_Predicate: {
991 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
992 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
993 << CmpInst::getPredicateName(Pred) << ')';
994 break;
995 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000996 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000997}
998
Alex Lorenz4af7e612015-08-03 23:08:19 +0000999void MIPrinter::print(const MachineMemOperand &Op) {
1000 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001001 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001002 if (Op.isVolatile())
1003 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001004 if (Op.isNonTemporal())
1005 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001006 if (Op.isDereferenceable())
1007 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001008 if (Op.isInvariant())
1009 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001010 if (Op.isLoad())
1011 OS << "load ";
1012 else {
1013 assert(Op.isStore() && "Non load machine operand must be a store");
1014 OS << "store ";
1015 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001016
1017 if (Op.getSynchScope() == SynchronizationScope::SingleThread)
1018 OS << "singlethread ";
1019
1020 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1021 OS << toIRString(Op.getOrdering()) << ' ';
1022 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1023 OS << toIRString(Op.getFailureOrdering()) << ' ';
1024
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001025 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001026 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001027 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001028 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001029 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1030 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001031 assert(PVal && "Expected a pseudo source value");
1032 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001033 case PseudoSourceValue::Stack:
1034 OS << "stack";
1035 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001036 case PseudoSourceValue::GOT:
1037 OS << "got";
1038 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001039 case PseudoSourceValue::JumpTable:
1040 OS << "jump-table";
1041 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001042 case PseudoSourceValue::ConstantPool:
1043 OS << "constant-pool";
1044 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001045 case PseudoSourceValue::FixedStack:
1046 printStackObjectReference(
1047 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1048 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001049 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001050 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001051 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1052 OS, /*PrintType=*/false, MST);
1053 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001054 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001055 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001056 printLLVMNameWithoutPrefix(
1057 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001058 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001059 case PseudoSourceValue::TargetCustom:
1060 llvm_unreachable("TargetCustom pseudo source values are not supported");
1061 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001062 }
1063 }
Alex Lorenz83127732015-08-07 20:26:52 +00001064 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001065 if (Op.getBaseAlignment() != Op.getSize())
1066 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001067 auto AAInfo = Op.getAAInfo();
1068 if (AAInfo.TBAA) {
1069 OS << ", !tbaa ";
1070 AAInfo.TBAA->printAsOperand(OS, MST);
1071 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001072 if (AAInfo.Scope) {
1073 OS << ", !alias.scope ";
1074 AAInfo.Scope->printAsOperand(OS, MST);
1075 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001076 if (AAInfo.NoAlias) {
1077 OS << ", !noalias ";
1078 AAInfo.NoAlias->printAsOperand(OS, MST);
1079 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001080 if (Op.getRanges()) {
1081 OS << ", !range ";
1082 Op.getRanges()->printAsOperand(OS, MST);
1083 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001084 OS << ')';
1085}
1086
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001087static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1088 const TargetRegisterInfo *TRI) {
1089 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1090 if (Reg == -1) {
1091 OS << "<badreg>";
1092 return;
1093 }
1094 printReg(Reg, OS, TRI);
1095}
1096
1097void MIPrinter::print(const MCCFIInstruction &CFI,
1098 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001099 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001100 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001101 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001102 if (CFI.getLabel())
1103 OS << "<mcsymbol> ";
1104 printCFIRegister(CFI.getRegister(), OS, TRI);
1105 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001106 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001107 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001108 if (CFI.getLabel())
1109 OS << "<mcsymbol> ";
1110 printCFIRegister(CFI.getRegister(), OS, TRI);
1111 OS << ", " << CFI.getOffset();
1112 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001113 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001114 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001115 if (CFI.getLabel())
1116 OS << "<mcsymbol> ";
1117 printCFIRegister(CFI.getRegister(), OS, TRI);
1118 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001119 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001120 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001121 if (CFI.getLabel())
1122 OS << "<mcsymbol> ";
1123 OS << CFI.getOffset();
1124 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001125 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001126 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001127 if (CFI.getLabel())
1128 OS << "<mcsymbol> ";
1129 printCFIRegister(CFI.getRegister(), OS, TRI);
1130 OS << ", " << CFI.getOffset();
1131 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001132 default:
1133 // TODO: Print the other CFI Operations.
1134 OS << "<unserializable cfi operation>";
1135 break;
1136 }
1137}
1138
Alex Lorenz345c1442015-06-15 23:52:35 +00001139void llvm::printMIR(raw_ostream &OS, const Module &M) {
1140 yaml::Output Out(OS);
1141 Out << const_cast<Module &>(M);
1142}
1143
1144void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1145 MIRPrinter Printer(OS);
1146 Printer.print(MF);
1147}