blob: 789ab092998076ea35ec65a5ddb2e49a78ec6741 [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"
Matthias Braun8b5f9e42017-06-06 19:00:58 +000019#include "llvm/ADT/StringExtras.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000020#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
21#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000024#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000025#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000028#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000029#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000030#include "llvm/IR/DebugInfo.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000031#include "llvm/IR/IRPrintingPasses.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000032#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000033#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000034#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000035#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000036#include "llvm/MC/MCSymbol.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000037#include "llvm/Support/Format.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000038#include "llvm/Support/MemoryBuffer.h"
Matthias Braun89401142017-05-05 21:09:30 +000039#include "llvm/Support/Options.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000040#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000041#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000042#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000043#include "llvm/Target/TargetIntrinsicInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000044#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000045
46using namespace llvm;
47
Matthias Braun89401142017-05-05 21:09:30 +000048static cl::opt<bool> SimplifyMIR("simplify-mir",
49 cl::desc("Leave out unnecessary information when printing MIR"));
50
Alex Lorenz345c1442015-06-15 23:52:35 +000051namespace {
52
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000053/// This structure describes how to print out stack object references.
54struct FrameIndexOperand {
55 std::string Name;
56 unsigned ID;
57 bool IsFixed;
58
59 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
60 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
61
62 /// Return an ordinary stack object reference.
63 static FrameIndexOperand create(StringRef Name, unsigned ID) {
64 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
65 }
66
67 /// Return a fixed stack object reference.
68 static FrameIndexOperand createFixed(unsigned ID) {
69 return FrameIndexOperand("", ID, /*IsFixed=*/true);
70 }
71};
72
Alex Lorenz618b2832015-07-30 16:54:38 +000073} // end anonymous namespace
74
75namespace llvm {
76
Alex Lorenz345c1442015-06-15 23:52:35 +000077/// This class prints out the machine functions using the MIR serialization
78/// format.
79class MIRPrinter {
80 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000081 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000082 /// Maps from stack object indices to operand indices which will be used when
83 /// printing frame index machine operands.
84 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000085
86public:
87 MIRPrinter(raw_ostream &OS) : OS(OS) {}
88
89 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000090
Alex Lorenz28148ba2015-07-09 22:23:13 +000091 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
92 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000093 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
94 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000095 void convert(yaml::MachineFunction &MF,
96 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000097 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
98 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +000099 void convertStackObjects(yaml::MachineFunction &YMF,
100 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000101
102private:
103 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000104};
105
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000106/// This class prints out the machine instructions using the MIR serialization
107/// format.
108class MIPrinter {
109 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000110 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000111 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000112 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113
Matthias Braun89401142017-05-05 21:09:30 +0000114 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
115 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
116
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000117public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000118 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000119 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
120 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
121 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
122 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000123
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000124 void print(const MachineBasicBlock &MBB);
125
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000127 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000128 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000129 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000130 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000131 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000132 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000133 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000134 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000135 LLT TypeToPrint, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000136 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000137
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000138 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000139};
140
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000141} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000142
143namespace llvm {
144namespace yaml {
145
146/// This struct serializes the LLVM IR module.
147template <> struct BlockScalarTraits<Module> {
148 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
149 Mod.print(OS, nullptr);
150 }
151 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
152 llvm_unreachable("LLVM Module is supposed to be parsed separately");
153 return "";
154 }
155};
156
157} // end namespace yaml
158} // end namespace llvm
159
Alex Lorenz15a00a82015-07-14 21:18:25 +0000160static void printReg(unsigned Reg, raw_ostream &OS,
161 const TargetRegisterInfo *TRI) {
162 // TODO: Print Stack Slots.
163 if (!Reg)
164 OS << '_';
165 else if (TargetRegisterInfo::isVirtualRegister(Reg))
166 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
167 else if (Reg < TRI->getNumRegs())
168 OS << '%' << StringRef(TRI->getName(Reg)).lower();
169 else
170 llvm_unreachable("Can't print this kind of register yet");
171}
172
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000173static void printReg(unsigned Reg, yaml::StringValue &Dest,
174 const TargetRegisterInfo *TRI) {
175 raw_string_ostream OS(Dest.Value);
176 printReg(Reg, OS, TRI);
177}
178
Alex Lorenz345c1442015-06-15 23:52:35 +0000179void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000180 initRegisterMaskIds(MF);
181
Alex Lorenz345c1442015-06-15 23:52:35 +0000182 yaml::MachineFunction YamlMF;
183 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000184 YamlMF.Alignment = MF.getAlignment();
185 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000186
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000187 YamlMF.Legalized = MF.getProperties().hasProperty(
188 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000189 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
190 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000191 YamlMF.Selected = MF.getProperties().hasProperty(
192 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000193
Alex Lorenz28148ba2015-07-09 22:23:13 +0000194 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000195 ModuleSlotTracker MST(MF.getFunction()->getParent());
196 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000197 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000198 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000199 if (const auto *ConstantPool = MF.getConstantPool())
200 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000201 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
202 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000203 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
204 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000205 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000206 if (IsNewlineNeeded)
207 StrOS << "\n";
208 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
209 .print(MBB);
210 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000211 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000212 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000213 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000214 if (!SimplifyMIR)
215 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000216 Out << YamlMF;
217}
218
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000219static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
220 const TargetRegisterInfo *TRI) {
221 assert(RegMask && "Can't print an empty register mask");
222 OS << StringRef("CustomRegMask(");
223
224 bool IsRegInRegMaskFound = false;
225 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
226 // Check whether the register is asserted in regmask.
227 if (RegMask[I / 32] & (1u << (I % 32))) {
228 if (IsRegInRegMaskFound)
229 OS << ',';
230 printReg(I, OS, TRI);
231 IsRegInRegMaskFound = true;
232 }
233 }
234
235 OS << ')';
236}
237
Alex Lorenz54565cf2015-06-24 19:56:10 +0000238void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000239 const MachineRegisterInfo &RegInfo,
240 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000241 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000242
243 // Print the virtual register definitions.
244 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
245 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
246 yaml::VirtualRegisterDefinition VReg;
247 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000248 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000249 VReg.Class =
250 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000251 else if (RegInfo.getRegBankOrNull(Reg))
252 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000253 else {
254 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000255 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000256 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000257 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000258 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
259 if (PreferredReg)
260 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000261 MF.VirtualRegisters.push_back(VReg);
262 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000263
264 // Print the live ins.
265 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
266 yaml::MachineFunctionLiveIn LiveIn;
267 printReg(I->first, LiveIn.Register, TRI);
268 if (I->second)
269 printReg(I->second, LiveIn.VirtualRegister, TRI);
270 MF.LiveIns.push_back(LiveIn);
271 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000272
273 // Prints the callee saved registers.
274 if (RegInfo.isUpdatedCSRsInitialized()) {
275 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
276 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
277 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000278 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000279 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000280 CalleeSavedRegisters.push_back(Reg);
281 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000282 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000283 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000284}
285
Alex Lorenza6f9a372015-07-29 21:09:09 +0000286void MIRPrinter::convert(ModuleSlotTracker &MST,
287 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000288 const MachineFrameInfo &MFI) {
289 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
290 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
291 YamlMFI.HasStackMap = MFI.hasStackMap();
292 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
293 YamlMFI.StackSize = MFI.getStackSize();
294 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
295 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
296 YamlMFI.AdjustsStack = MFI.adjustsStack();
297 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000298 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
299 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000300 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
301 YamlMFI.HasVAStart = MFI.hasVAStart();
302 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000303 if (MFI.getSavePoint()) {
304 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
305 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
306 .printMBBReference(*MFI.getSavePoint());
307 }
308 if (MFI.getRestorePoint()) {
309 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
310 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
311 .printMBBReference(*MFI.getRestorePoint());
312 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000313}
314
Matthias Braunef331ef2016-11-30 23:48:50 +0000315void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
316 const MachineFunction &MF,
317 ModuleSlotTracker &MST) {
318 const MachineFrameInfo &MFI = MF.getFrameInfo();
319 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000320 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000321 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000322 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
323 if (MFI.isDeadObjectIndex(I))
324 continue;
325
326 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000327 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000328 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
329 ? yaml::FixedMachineStackObject::SpillSlot
330 : yaml::FixedMachineStackObject::DefaultType;
331 YamlObject.Offset = MFI.getObjectOffset(I);
332 YamlObject.Size = MFI.getObjectSize(I);
333 YamlObject.Alignment = MFI.getObjectAlignment(I);
334 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
335 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000336 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000337 StackObjectOperandMapping.insert(
338 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000339 }
340
341 // Process ordinary stack objects.
342 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000343 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
344 if (MFI.isDeadObjectIndex(I))
345 continue;
346
347 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000348 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000349 if (const auto *Alloca = MFI.getObjectAllocation(I))
350 YamlObject.Name.Value =
351 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000352 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
353 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000354 : MFI.isVariableSizedObjectIndex(I)
355 ? yaml::MachineStackObject::VariableSized
356 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000357 YamlObject.Offset = MFI.getObjectOffset(I);
358 YamlObject.Size = MFI.getObjectSize(I);
359 YamlObject.Alignment = MFI.getObjectAlignment(I);
360
Matthias Braunef331ef2016-11-30 23:48:50 +0000361 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000362 StackObjectOperandMapping.insert(std::make_pair(
363 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000364 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000365
366 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
367 yaml::StringValue Reg;
368 printReg(CSInfo.getReg(), Reg, TRI);
369 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
370 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
371 "Invalid stack object index");
372 const FrameIndexOperand &StackObject = StackObjectInfo->second;
373 if (StackObject.IsFixed)
Matthias Braunef331ef2016-11-30 23:48:50 +0000374 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000375 else
Matthias Braunef331ef2016-11-30 23:48:50 +0000376 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000377 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000378 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
379 auto LocalObject = MFI.getLocalFrameObjectMap(I);
380 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
381 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
382 "Invalid stack object index");
383 const FrameIndexOperand &StackObject = StackObjectInfo->second;
384 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000385 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000386 }
Alex Lorenza314d812015-08-18 22:26:26 +0000387
388 // Print the stack object references in the frame information class after
389 // converting the stack objects.
390 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000391 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000392 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
393 .printStackObjectReference(MFI.getStackProtectorIndex());
394 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000395
396 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000397 for (const MachineFunction::VariableDbgInfo &DebugVar :
398 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000399 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
400 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
401 "Invalid stack object index");
402 const FrameIndexOperand &StackObject = StackObjectInfo->second;
403 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000404 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000405 {
406 raw_string_ostream StrOS(Object.DebugVar.Value);
407 DebugVar.Var->printAsOperand(StrOS, MST);
408 }
409 {
410 raw_string_ostream StrOS(Object.DebugExpr.Value);
411 DebugVar.Expr->printAsOperand(StrOS, MST);
412 }
413 {
414 raw_string_ostream StrOS(Object.DebugLoc.Value);
415 DebugVar.Loc->printAsOperand(StrOS, MST);
416 }
417 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000418}
419
Alex Lorenzab980492015-07-20 20:51:18 +0000420void MIRPrinter::convert(yaml::MachineFunction &MF,
421 const MachineConstantPool &ConstantPool) {
422 unsigned ID = 0;
423 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
424 // TODO: Serialize target specific constant pool entries.
425 if (Constant.isMachineConstantPoolEntry())
426 llvm_unreachable("Can't print target specific constant pool entries yet");
427
428 yaml::MachineConstantPoolValue YamlConstant;
429 std::string Str;
430 raw_string_ostream StrOS(Str);
431 Constant.Val.ConstVal->printAsOperand(StrOS);
432 YamlConstant.ID = ID++;
433 YamlConstant.Value = StrOS.str();
434 YamlConstant.Alignment = Constant.getAlignment();
435 MF.Constants.push_back(YamlConstant);
436 }
437}
438
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000439void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000440 yaml::MachineJumpTable &YamlJTI,
441 const MachineJumpTableInfo &JTI) {
442 YamlJTI.Kind = JTI.getEntryKind();
443 unsigned ID = 0;
444 for (const auto &Table : JTI.getJumpTables()) {
445 std::string Str;
446 yaml::MachineJumpTable::Entry Entry;
447 Entry.ID = ID++;
448 for (const auto *MBB : Table.MBBs) {
449 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000450 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
451 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000452 Entry.Blocks.push_back(StrOS.str());
453 Str.clear();
454 }
455 YamlJTI.Entries.push_back(Entry);
456 }
457}
458
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000459void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
460 const auto *TRI = MF.getSubtarget().getRegisterInfo();
461 unsigned I = 0;
462 for (const uint32_t *Mask : TRI->getRegMasks())
463 RegisterMaskIds.insert(std::make_pair(Mask, I++));
464}
465
Matthias Braun89401142017-05-05 21:09:30 +0000466void llvm::guessSuccessors(const MachineBasicBlock &MBB,
467 SmallVectorImpl<MachineBasicBlock*> &Result,
468 bool &IsFallthrough) {
469 SmallPtrSet<MachineBasicBlock*,8> Seen;
470
471 for (const MachineInstr &MI : MBB) {
472 if (MI.isPHI())
473 continue;
474 for (const MachineOperand &MO : MI.operands()) {
475 if (!MO.isMBB())
476 continue;
477 MachineBasicBlock *Succ = MO.getMBB();
478 auto RP = Seen.insert(Succ);
479 if (RP.second)
480 Result.push_back(Succ);
481 }
482 }
483 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
484 IsFallthrough = I == MBB.end() || !I->isBarrier();
485}
486
487bool
488MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
489 if (MBB.succ_size() <= 1)
490 return true;
491 if (!MBB.hasSuccessorProbabilities())
492 return true;
493
494 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
495 MBB.Probs.end());
496 BranchProbability::normalizeProbabilities(Normalized.begin(),
497 Normalized.end());
498 SmallVector<BranchProbability,8> Equal(Normalized.size());
499 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
500
501 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
502}
503
504bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
505 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
506 bool GuessedFallthrough;
507 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
508 if (GuessedFallthrough) {
509 const MachineFunction &MF = *MBB.getParent();
510 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
511 if (NextI != MF.end()) {
512 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
513 if (!is_contained(GuessedSuccs, Next))
514 GuessedSuccs.push_back(Next);
515 }
516 }
517 if (GuessedSuccs.size() != MBB.succ_size())
518 return false;
519 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
520}
521
522
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000523void MIPrinter::print(const MachineBasicBlock &MBB) {
524 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
525 OS << "bb." << MBB.getNumber();
526 bool HasAttributes = false;
527 if (const auto *BB = MBB.getBasicBlock()) {
528 if (BB->hasName()) {
529 OS << "." << BB->getName();
530 } else {
531 HasAttributes = true;
532 OS << " (";
533 int Slot = MST.getLocalSlot(BB);
534 if (Slot == -1)
535 OS << "<ir-block badref>";
536 else
537 OS << (Twine("%ir-block.") + Twine(Slot)).str();
538 }
539 }
540 if (MBB.hasAddressTaken()) {
541 OS << (HasAttributes ? ", " : " (");
542 OS << "address-taken";
543 HasAttributes = true;
544 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000545 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000546 OS << (HasAttributes ? ", " : " (");
547 OS << "landing-pad";
548 HasAttributes = true;
549 }
550 if (MBB.getAlignment()) {
551 OS << (HasAttributes ? ", " : " (");
552 OS << "align " << MBB.getAlignment();
553 HasAttributes = true;
554 }
555 if (HasAttributes)
556 OS << ")";
557 OS << ":\n";
558
559 bool HasLineAttributes = false;
560 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000561 bool canPredictProbs = canPredictBranchProbabilities(MBB);
562 if (!MBB.succ_empty() && (!SimplifyMIR || !canPredictProbs ||
563 !canPredictSuccessors(MBB))) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000564 OS.indent(2) << "successors: ";
565 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
566 if (I != MBB.succ_begin())
567 OS << ", ";
568 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000569 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000570 OS << '('
571 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
572 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000573 }
574 OS << "\n";
575 HasLineAttributes = true;
576 }
577
578 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000579 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
580 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
581 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000582 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000583 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000584 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000585 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000586 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000587 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000588 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000589 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000590 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000591 }
592 OS << "\n";
593 HasLineAttributes = true;
594 }
595
596 if (HasLineAttributes)
597 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000598 bool IsInBundle = false;
599 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
600 const MachineInstr &MI = *I;
601 if (IsInBundle && !MI.isInsideBundle()) {
602 OS.indent(2) << "}\n";
603 IsInBundle = false;
604 }
605 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000606 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000607 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
608 OS << " {";
609 IsInBundle = true;
610 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000611 OS << "\n";
612 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000613 if (IsInBundle)
614 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615}
616
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000617/// Return true when an instruction has tied register that can't be determined
618/// by the instruction's descriptor.
619static bool hasComplexRegisterTies(const MachineInstr &MI) {
620 const MCInstrDesc &MCID = MI.getDesc();
621 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
622 const auto &Operand = MI.getOperand(I);
623 if (!Operand.isReg() || Operand.isDef())
624 // Ignore the defined registers as MCID marks only the uses as tied.
625 continue;
626 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
627 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
628 if (ExpectedTiedIdx != TiedIdx)
629 return true;
630 }
631 return false;
632}
633
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000634static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
635 SmallBitVector &PrintedTypes,
636 const MachineRegisterInfo &MRI) {
637 const MachineOperand &Op = MI.getOperand(OpIdx);
638 if (!Op.isReg())
639 return LLT{};
640
641 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
642 return MRI.getType(Op.getReg());
643
644 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
645 if (!OpInfo.isGenericType())
646 return MRI.getType(Op.getReg());
647
648 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
649 return LLT{};
650
651 PrintedTypes.set(OpInfo.getGenericTypeIndex());
652 return MRI.getType(Op.getReg());
653}
654
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000655void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000656 const auto *MF = MI.getParent()->getParent();
657 const auto &MRI = MF->getRegInfo();
658 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000659 const auto *TRI = SubTarget.getRegisterInfo();
660 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000661 const auto *TII = SubTarget.getInstrInfo();
662 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000663 if (MI.isCFIInstruction())
664 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000665
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000666 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000667 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000668 unsigned I = 0, E = MI.getNumOperands();
669 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
670 !MI.getOperand(I).isImplicit();
671 ++I) {
672 if (I)
673 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000674 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
675 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000676 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000677 }
678
679 if (I)
680 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000681 if (MI.getFlag(MachineInstr::FrameSetup))
682 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000683 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000684 if (I < E)
685 OS << ' ';
686
687 bool NeedComma = false;
688 for (; I < E; ++I) {
689 if (NeedComma)
690 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000691 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
692 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000693 NeedComma = true;
694 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000695
696 if (MI.getDebugLoc()) {
697 if (NeedComma)
698 OS << ',';
699 OS << " debug-location ";
700 MI.getDebugLoc()->printAsOperand(OS, MST);
701 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000702
703 if (!MI.memoperands_empty()) {
704 OS << " :: ";
705 bool NeedComma = false;
706 for (const auto *Op : MI.memoperands()) {
707 if (NeedComma)
708 OS << ", ";
709 print(*Op);
710 NeedComma = true;
711 }
712 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000713}
714
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000715void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
716 OS << "%bb." << MBB.getNumber();
717 if (const auto *BB = MBB.getBasicBlock()) {
718 if (BB->hasName())
719 OS << '.' << BB->getName();
720 }
721}
722
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000723static void printIRSlotNumber(raw_ostream &OS, int Slot) {
724 if (Slot == -1)
725 OS << "<badref>";
726 else
727 OS << Slot;
728}
729
Alex Lorenzdeb53492015-07-28 17:28:03 +0000730void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
731 OS << "%ir-block.";
732 if (BB.hasName()) {
733 printLLVMNameWithoutPrefix(OS, BB.getName());
734 return;
735 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000736 const Function *F = BB.getParent();
737 int Slot;
738 if (F == MST.getCurrentFunction()) {
739 Slot = MST.getLocalSlot(&BB);
740 } else {
741 ModuleSlotTracker CustomMST(F->getParent(),
742 /*ShouldInitializeAllMetadata=*/false);
743 CustomMST.incorporateFunction(*F);
744 Slot = CustomMST.getLocalSlot(&BB);
745 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000746 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000747}
748
Alex Lorenz4af7e612015-08-03 23:08:19 +0000749void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000750 if (isa<GlobalValue>(V)) {
751 V.printAsOperand(OS, /*PrintType=*/false, MST);
752 return;
753 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000754 if (isa<Constant>(V)) {
755 // Machine memory operands can load/store to/from constant value pointers.
756 OS << '`';
757 V.printAsOperand(OS, /*PrintType=*/true, MST);
758 OS << '`';
759 return;
760 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000761 OS << "%ir.";
762 if (V.hasName()) {
763 printLLVMNameWithoutPrefix(OS, V.getName());
764 return;
765 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000766 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000767}
768
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000769void MIPrinter::printStackObjectReference(int FrameIndex) {
770 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
771 assert(ObjectInfo != StackObjectOperandMapping.end() &&
772 "Invalid frame index");
773 const FrameIndexOperand &Operand = ObjectInfo->second;
774 if (Operand.IsFixed) {
775 OS << "%fixed-stack." << Operand.ID;
776 return;
777 }
778 OS << "%stack." << Operand.ID;
779 if (!Operand.Name.empty())
780 OS << '.' << Operand.Name;
781}
782
Alex Lorenz5672a892015-08-05 22:26:15 +0000783void MIPrinter::printOffset(int64_t Offset) {
784 if (Offset == 0)
785 return;
786 if (Offset < 0) {
787 OS << " - " << -Offset;
788 return;
789 }
790 OS << " + " << Offset;
791}
792
Alex Lorenz49873a82015-08-06 00:44:07 +0000793static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
794 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
795 for (const auto &I : Flags) {
796 if (I.first == TF) {
797 return I.second;
798 }
799 }
800 return nullptr;
801}
802
803void MIPrinter::printTargetFlags(const MachineOperand &Op) {
804 if (!Op.getTargetFlags())
805 return;
806 const auto *TII =
807 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
808 assert(TII && "expected instruction info");
809 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
810 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000811 const bool HasDirectFlags = Flags.first;
812 const bool HasBitmaskFlags = Flags.second;
813 if (!HasDirectFlags && !HasBitmaskFlags) {
814 OS << "<unknown>) ";
815 return;
816 }
817 if (HasDirectFlags) {
818 if (const auto *Name = getTargetFlagName(TII, Flags.first))
819 OS << Name;
820 else
821 OS << "<unknown target flag>";
822 }
823 if (!HasBitmaskFlags) {
824 OS << ") ";
825 return;
826 }
827 bool IsCommaNeeded = HasDirectFlags;
828 unsigned BitMask = Flags.second;
829 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
830 for (const auto &Mask : BitMasks) {
831 // Check if the flag's bitmask has the bits of the current mask set.
832 if ((BitMask & Mask.first) == Mask.first) {
833 if (IsCommaNeeded)
834 OS << ", ";
835 IsCommaNeeded = true;
836 OS << Mask.second;
837 // Clear the bits which were serialized from the flag's bitmask.
838 BitMask &= ~(Mask.first);
839 }
840 }
841 if (BitMask) {
842 // When the resulting flag's bitmask isn't zero, we know that we didn't
843 // serialize all of the bit flags.
844 if (IsCommaNeeded)
845 OS << ", ";
846 OS << "<unknown bitmask target flag>";
847 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000848 OS << ") ";
849}
850
Alex Lorenzef5c1962015-07-28 23:02:45 +0000851static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
852 const auto *TII = MF.getSubtarget().getInstrInfo();
853 assert(TII && "expected instruction info");
854 auto Indices = TII->getSerializableTargetIndices();
855 for (const auto &I : Indices) {
856 if (I.first == Index) {
857 return I.second;
858 }
859 }
860 return nullptr;
861}
862
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000863void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000864 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
865 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000866 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000867 switch (Op.getType()) {
868 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000869 if (Op.isImplicit())
870 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000871 else if (!IsDef && Op.isDef())
872 // Print the 'def' flag only when the operand is defined after '='.
873 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000874 if (Op.isInternalRead())
875 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000876 if (Op.isDead())
877 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000878 if (Op.isKill())
879 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000880 if (Op.isUndef())
881 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000882 if (Op.isEarlyClobber())
883 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000884 if (Op.isDebug())
885 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000886 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000887 // Print the sub register.
888 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000889 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000890 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
891 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000892 if (TypeToPrint.isValid())
893 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000894 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000895 case MachineOperand::MO_Immediate:
896 OS << Op.getImm();
897 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000898 case MachineOperand::MO_CImmediate:
899 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
900 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000901 case MachineOperand::MO_FPImmediate:
902 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
903 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000904 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000905 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000906 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000907 case MachineOperand::MO_FrameIndex:
908 printStackObjectReference(Op.getIndex());
909 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000910 case MachineOperand::MO_ConstantPoolIndex:
911 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000912 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000913 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000914 case MachineOperand::MO_TargetIndex: {
915 OS << "target-index(";
916 if (const auto *Name = getTargetIndexName(
917 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
918 OS << Name;
919 else
920 OS << "<unknown>";
921 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000922 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000923 break;
924 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000925 case MachineOperand::MO_JumpTableIndex:
926 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000927 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000928 case MachineOperand::MO_ExternalSymbol: {
929 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000930 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000931 if (Name.empty()) {
932 OS << "\"\"";
933 } else {
934 printLLVMNameWithoutPrefix(OS, Name);
935 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000936 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000937 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000938 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000939 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000940 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000941 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000942 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000943 case MachineOperand::MO_BlockAddress:
944 OS << "blockaddress(";
945 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
946 MST);
947 OS << ", ";
948 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
949 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000950 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000951 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000952 case MachineOperand::MO_RegisterMask: {
953 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
954 if (RegMaskInfo != RegisterMaskIds.end())
955 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
956 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000957 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000958 break;
959 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000960 case MachineOperand::MO_RegisterLiveOut: {
961 const uint32_t *RegMask = Op.getRegLiveOut();
962 OS << "liveout(";
963 bool IsCommaNeeded = false;
964 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
965 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
966 if (IsCommaNeeded)
967 OS << ", ";
968 printReg(Reg, OS, TRI);
969 IsCommaNeeded = true;
970 }
971 }
972 OS << ")";
973 break;
974 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000975 case MachineOperand::MO_Metadata:
976 Op.getMetadata()->printAsOperand(OS, MST);
977 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000978 case MachineOperand::MO_MCSymbol:
979 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
980 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000981 case MachineOperand::MO_CFIIndex: {
Matthias Braunf23ef432016-11-30 23:48:42 +0000982 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
983 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000984 break;
985 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000986 case MachineOperand::MO_IntrinsicID: {
987 Intrinsic::ID ID = Op.getIntrinsicID();
988 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000989 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000990 else {
991 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
992 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
993 OS << "intrinsic(@" << TII->getName(ID) << ')';
994 }
995 break;
996 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000997 case MachineOperand::MO_Predicate: {
998 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
999 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1000 << CmpInst::getPredicateName(Pred) << ')';
1001 break;
1002 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001003 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001004}
1005
Alex Lorenz4af7e612015-08-03 23:08:19 +00001006void MIPrinter::print(const MachineMemOperand &Op) {
1007 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001008 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001009 if (Op.isVolatile())
1010 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001011 if (Op.isNonTemporal())
1012 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001013 if (Op.isDereferenceable())
1014 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001015 if (Op.isInvariant())
1016 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001017 if (Op.isLoad())
1018 OS << "load ";
1019 else {
1020 assert(Op.isStore() && "Non load machine operand must be a store");
1021 OS << "store ";
1022 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001023
1024 if (Op.getSynchScope() == SynchronizationScope::SingleThread)
1025 OS << "singlethread ";
1026
1027 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1028 OS << toIRString(Op.getOrdering()) << ' ';
1029 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1030 OS << toIRString(Op.getFailureOrdering()) << ' ';
1031
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001032 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001033 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001034 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001035 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001036 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1037 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001038 assert(PVal && "Expected a pseudo source value");
1039 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001040 case PseudoSourceValue::Stack:
1041 OS << "stack";
1042 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001043 case PseudoSourceValue::GOT:
1044 OS << "got";
1045 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001046 case PseudoSourceValue::JumpTable:
1047 OS << "jump-table";
1048 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001049 case PseudoSourceValue::ConstantPool:
1050 OS << "constant-pool";
1051 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001052 case PseudoSourceValue::FixedStack:
1053 printStackObjectReference(
1054 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1055 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001056 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001057 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001058 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1059 OS, /*PrintType=*/false, MST);
1060 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001061 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001062 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001063 printLLVMNameWithoutPrefix(
1064 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001065 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001066 case PseudoSourceValue::TargetCustom:
1067 llvm_unreachable("TargetCustom pseudo source values are not supported");
1068 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001069 }
1070 }
Alex Lorenz83127732015-08-07 20:26:52 +00001071 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001072 if (Op.getBaseAlignment() != Op.getSize())
1073 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001074 auto AAInfo = Op.getAAInfo();
1075 if (AAInfo.TBAA) {
1076 OS << ", !tbaa ";
1077 AAInfo.TBAA->printAsOperand(OS, MST);
1078 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001079 if (AAInfo.Scope) {
1080 OS << ", !alias.scope ";
1081 AAInfo.Scope->printAsOperand(OS, MST);
1082 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001083 if (AAInfo.NoAlias) {
1084 OS << ", !noalias ";
1085 AAInfo.NoAlias->printAsOperand(OS, MST);
1086 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001087 if (Op.getRanges()) {
1088 OS << ", !range ";
1089 Op.getRanges()->printAsOperand(OS, MST);
1090 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001091 OS << ')';
1092}
1093
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001094static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1095 const TargetRegisterInfo *TRI) {
1096 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1097 if (Reg == -1) {
1098 OS << "<badreg>";
1099 return;
1100 }
1101 printReg(Reg, OS, TRI);
1102}
1103
1104void MIPrinter::print(const MCCFIInstruction &CFI,
1105 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001106 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001107 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001108 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001109 if (CFI.getLabel())
1110 OS << "<mcsymbol> ";
1111 printCFIRegister(CFI.getRegister(), OS, TRI);
1112 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001113 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001114 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001115 if (CFI.getLabel())
1116 OS << "<mcsymbol> ";
1117 printCFIRegister(CFI.getRegister(), OS, TRI);
1118 OS << ", " << CFI.getOffset();
1119 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001120 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001121 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001122 if (CFI.getLabel())
1123 OS << "<mcsymbol> ";
1124 printCFIRegister(CFI.getRegister(), OS, TRI);
1125 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001126 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001127 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001128 if (CFI.getLabel())
1129 OS << "<mcsymbol> ";
1130 OS << CFI.getOffset();
1131 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001132 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001133 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001134 if (CFI.getLabel())
1135 OS << "<mcsymbol> ";
1136 printCFIRegister(CFI.getRegister(), OS, TRI);
1137 OS << ", " << CFI.getOffset();
1138 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001139 default:
1140 // TODO: Print the other CFI Operations.
1141 OS << "<unserializable cfi operation>";
1142 break;
1143 }
1144}
1145
Alex Lorenz345c1442015-06-15 23:52:35 +00001146void llvm::printMIR(raw_ostream &OS, const Module &M) {
1147 yaml::Output Out(OS);
1148 Out << const_cast<Module &>(M);
1149}
1150
1151void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1152 MIRPrinter Printer(OS);
1153 Printer.print(MF);
1154}