blob: 293fc7358b8ed010b8c201f18332913a0cd1fb76 [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);
213 Out << YamlMF;
214}
215
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000216static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
217 const TargetRegisterInfo *TRI) {
218 assert(RegMask && "Can't print an empty register mask");
219 OS << StringRef("CustomRegMask(");
220
221 bool IsRegInRegMaskFound = false;
222 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
223 // Check whether the register is asserted in regmask.
224 if (RegMask[I / 32] & (1u << (I % 32))) {
225 if (IsRegInRegMaskFound)
226 OS << ',';
227 printReg(I, OS, TRI);
228 IsRegInRegMaskFound = true;
229 }
230 }
231
232 OS << ')';
233}
234
Alex Lorenz54565cf2015-06-24 19:56:10 +0000235void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000236 const MachineRegisterInfo &RegInfo,
237 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000238 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000239
240 // Print the virtual register definitions.
241 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
242 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
243 yaml::VirtualRegisterDefinition VReg;
244 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000245 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000246 VReg.Class =
247 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000248 else if (RegInfo.getRegBankOrNull(Reg))
249 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000250 else {
251 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000252 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000253 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000254 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000255 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
256 if (PreferredReg)
257 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000258 MF.VirtualRegisters.push_back(VReg);
259 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000260
261 // Print the live ins.
262 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
263 yaml::MachineFunctionLiveIn LiveIn;
264 printReg(I->first, LiveIn.Register, TRI);
265 if (I->second)
266 printReg(I->second, LiveIn.VirtualRegister, TRI);
267 MF.LiveIns.push_back(LiveIn);
268 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000269
270 // Prints the callee saved registers.
271 if (RegInfo.isUpdatedCSRsInitialized()) {
272 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
273 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
274 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000275 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000276 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000277 CalleeSavedRegisters.push_back(Reg);
278 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000279 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000280 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000281}
282
Alex Lorenza6f9a372015-07-29 21:09:09 +0000283void MIRPrinter::convert(ModuleSlotTracker &MST,
284 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000285 const MachineFrameInfo &MFI) {
286 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
287 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
288 YamlMFI.HasStackMap = MFI.hasStackMap();
289 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
290 YamlMFI.StackSize = MFI.getStackSize();
291 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
292 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
293 YamlMFI.AdjustsStack = MFI.adjustsStack();
294 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000295 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
296 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000297 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
298 YamlMFI.HasVAStart = MFI.hasVAStart();
299 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000300 if (MFI.getSavePoint()) {
301 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
302 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
303 .printMBBReference(*MFI.getSavePoint());
304 }
305 if (MFI.getRestorePoint()) {
306 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
307 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
308 .printMBBReference(*MFI.getRestorePoint());
309 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000310}
311
Matthias Braunef331ef2016-11-30 23:48:50 +0000312void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
313 const MachineFunction &MF,
314 ModuleSlotTracker &MST) {
315 const MachineFrameInfo &MFI = MF.getFrameInfo();
316 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000317 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000318 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000319 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
320 if (MFI.isDeadObjectIndex(I))
321 continue;
322
323 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000324 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000325 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
326 ? yaml::FixedMachineStackObject::SpillSlot
327 : yaml::FixedMachineStackObject::DefaultType;
328 YamlObject.Offset = MFI.getObjectOffset(I);
329 YamlObject.Size = MFI.getObjectSize(I);
330 YamlObject.Alignment = MFI.getObjectAlignment(I);
331 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
332 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000333 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000334 StackObjectOperandMapping.insert(
335 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000336 }
337
338 // Process ordinary stack objects.
339 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000340 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
341 if (MFI.isDeadObjectIndex(I))
342 continue;
343
344 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000345 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000346 if (const auto *Alloca = MFI.getObjectAllocation(I))
347 YamlObject.Name.Value =
348 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000349 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
350 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000351 : MFI.isVariableSizedObjectIndex(I)
352 ? yaml::MachineStackObject::VariableSized
353 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000354 YamlObject.Offset = MFI.getObjectOffset(I);
355 YamlObject.Size = MFI.getObjectSize(I);
356 YamlObject.Alignment = MFI.getObjectAlignment(I);
357
Matthias Braunef331ef2016-11-30 23:48:50 +0000358 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000359 StackObjectOperandMapping.insert(std::make_pair(
360 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000361 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000362
363 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
364 yaml::StringValue Reg;
365 printReg(CSInfo.getReg(), Reg, TRI);
366 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
367 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
368 "Invalid stack object index");
369 const FrameIndexOperand &StackObject = StackObjectInfo->second;
370 if (StackObject.IsFixed)
Matthias Braunef331ef2016-11-30 23:48:50 +0000371 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000372 else
Matthias Braunef331ef2016-11-30 23:48:50 +0000373 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000374 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000375 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
376 auto LocalObject = MFI.getLocalFrameObjectMap(I);
377 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
378 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
379 "Invalid stack object index");
380 const FrameIndexOperand &StackObject = StackObjectInfo->second;
381 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000382 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000383 }
Alex Lorenza314d812015-08-18 22:26:26 +0000384
385 // Print the stack object references in the frame information class after
386 // converting the stack objects.
387 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000388 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000389 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
390 .printStackObjectReference(MFI.getStackProtectorIndex());
391 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000392
393 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000394 for (const MachineFunction::VariableDbgInfo &DebugVar :
395 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000396 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
397 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
398 "Invalid stack object index");
399 const FrameIndexOperand &StackObject = StackObjectInfo->second;
400 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000401 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000402 {
403 raw_string_ostream StrOS(Object.DebugVar.Value);
404 DebugVar.Var->printAsOperand(StrOS, MST);
405 }
406 {
407 raw_string_ostream StrOS(Object.DebugExpr.Value);
408 DebugVar.Expr->printAsOperand(StrOS, MST);
409 }
410 {
411 raw_string_ostream StrOS(Object.DebugLoc.Value);
412 DebugVar.Loc->printAsOperand(StrOS, MST);
413 }
414 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000415}
416
Alex Lorenzab980492015-07-20 20:51:18 +0000417void MIRPrinter::convert(yaml::MachineFunction &MF,
418 const MachineConstantPool &ConstantPool) {
419 unsigned ID = 0;
420 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
421 // TODO: Serialize target specific constant pool entries.
422 if (Constant.isMachineConstantPoolEntry())
423 llvm_unreachable("Can't print target specific constant pool entries yet");
424
425 yaml::MachineConstantPoolValue YamlConstant;
426 std::string Str;
427 raw_string_ostream StrOS(Str);
428 Constant.Val.ConstVal->printAsOperand(StrOS);
429 YamlConstant.ID = ID++;
430 YamlConstant.Value = StrOS.str();
431 YamlConstant.Alignment = Constant.getAlignment();
432 MF.Constants.push_back(YamlConstant);
433 }
434}
435
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000436void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000437 yaml::MachineJumpTable &YamlJTI,
438 const MachineJumpTableInfo &JTI) {
439 YamlJTI.Kind = JTI.getEntryKind();
440 unsigned ID = 0;
441 for (const auto &Table : JTI.getJumpTables()) {
442 std::string Str;
443 yaml::MachineJumpTable::Entry Entry;
444 Entry.ID = ID++;
445 for (const auto *MBB : Table.MBBs) {
446 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000447 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
448 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000449 Entry.Blocks.push_back(StrOS.str());
450 Str.clear();
451 }
452 YamlJTI.Entries.push_back(Entry);
453 }
454}
455
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000456void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
457 const auto *TRI = MF.getSubtarget().getRegisterInfo();
458 unsigned I = 0;
459 for (const uint32_t *Mask : TRI->getRegMasks())
460 RegisterMaskIds.insert(std::make_pair(Mask, I++));
461}
462
Matthias Braun89401142017-05-05 21:09:30 +0000463void llvm::guessSuccessors(const MachineBasicBlock &MBB,
464 SmallVectorImpl<MachineBasicBlock*> &Result,
465 bool &IsFallthrough) {
466 SmallPtrSet<MachineBasicBlock*,8> Seen;
467
468 for (const MachineInstr &MI : MBB) {
469 if (MI.isPHI())
470 continue;
471 for (const MachineOperand &MO : MI.operands()) {
472 if (!MO.isMBB())
473 continue;
474 MachineBasicBlock *Succ = MO.getMBB();
475 auto RP = Seen.insert(Succ);
476 if (RP.second)
477 Result.push_back(Succ);
478 }
479 }
480 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
481 IsFallthrough = I == MBB.end() || !I->isBarrier();
482}
483
484bool
485MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
486 if (MBB.succ_size() <= 1)
487 return true;
488 if (!MBB.hasSuccessorProbabilities())
489 return true;
490
491 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
492 MBB.Probs.end());
493 BranchProbability::normalizeProbabilities(Normalized.begin(),
494 Normalized.end());
495 SmallVector<BranchProbability,8> Equal(Normalized.size());
496 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
497
498 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
499}
500
501bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
502 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
503 bool GuessedFallthrough;
504 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
505 if (GuessedFallthrough) {
506 const MachineFunction &MF = *MBB.getParent();
507 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
508 if (NextI != MF.end()) {
509 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
510 if (!is_contained(GuessedSuccs, Next))
511 GuessedSuccs.push_back(Next);
512 }
513 }
514 if (GuessedSuccs.size() != MBB.succ_size())
515 return false;
516 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
517}
518
519
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000520void MIPrinter::print(const MachineBasicBlock &MBB) {
521 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
522 OS << "bb." << MBB.getNumber();
523 bool HasAttributes = false;
524 if (const auto *BB = MBB.getBasicBlock()) {
525 if (BB->hasName()) {
526 OS << "." << BB->getName();
527 } else {
528 HasAttributes = true;
529 OS << " (";
530 int Slot = MST.getLocalSlot(BB);
531 if (Slot == -1)
532 OS << "<ir-block badref>";
533 else
534 OS << (Twine("%ir-block.") + Twine(Slot)).str();
535 }
536 }
537 if (MBB.hasAddressTaken()) {
538 OS << (HasAttributes ? ", " : " (");
539 OS << "address-taken";
540 HasAttributes = true;
541 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000542 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000543 OS << (HasAttributes ? ", " : " (");
544 OS << "landing-pad";
545 HasAttributes = true;
546 }
547 if (MBB.getAlignment()) {
548 OS << (HasAttributes ? ", " : " (");
549 OS << "align " << MBB.getAlignment();
550 HasAttributes = true;
551 }
552 if (HasAttributes)
553 OS << ")";
554 OS << ":\n";
555
556 bool HasLineAttributes = false;
557 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000558 bool canPredictProbs = canPredictBranchProbabilities(MBB);
559 if (!MBB.succ_empty() && (!SimplifyMIR || !canPredictProbs ||
560 !canPredictSuccessors(MBB))) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000561 OS.indent(2) << "successors: ";
562 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
563 if (I != MBB.succ_begin())
564 OS << ", ";
565 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000566 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000567 OS << '('
568 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
569 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000570 }
571 OS << "\n";
572 HasLineAttributes = true;
573 }
574
575 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000576 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
577 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
578 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000579 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000580 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000581 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000582 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000583 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000584 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000585 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000586 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000587 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000588 }
589 OS << "\n";
590 HasLineAttributes = true;
591 }
592
593 if (HasLineAttributes)
594 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000595 bool IsInBundle = false;
596 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
597 const MachineInstr &MI = *I;
598 if (IsInBundle && !MI.isInsideBundle()) {
599 OS.indent(2) << "}\n";
600 IsInBundle = false;
601 }
602 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000603 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000604 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
605 OS << " {";
606 IsInBundle = true;
607 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000608 OS << "\n";
609 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000610 if (IsInBundle)
611 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000612}
613
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000614/// Return true when an instruction has tied register that can't be determined
615/// by the instruction's descriptor.
616static bool hasComplexRegisterTies(const MachineInstr &MI) {
617 const MCInstrDesc &MCID = MI.getDesc();
618 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
619 const auto &Operand = MI.getOperand(I);
620 if (!Operand.isReg() || Operand.isDef())
621 // Ignore the defined registers as MCID marks only the uses as tied.
622 continue;
623 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
624 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
625 if (ExpectedTiedIdx != TiedIdx)
626 return true;
627 }
628 return false;
629}
630
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000631static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
632 SmallBitVector &PrintedTypes,
633 const MachineRegisterInfo &MRI) {
634 const MachineOperand &Op = MI.getOperand(OpIdx);
635 if (!Op.isReg())
636 return LLT{};
637
638 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
639 return MRI.getType(Op.getReg());
640
641 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
642 if (!OpInfo.isGenericType())
643 return MRI.getType(Op.getReg());
644
645 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
646 return LLT{};
647
648 PrintedTypes.set(OpInfo.getGenericTypeIndex());
649 return MRI.getType(Op.getReg());
650}
651
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000652void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000653 const auto *MF = MI.getParent()->getParent();
654 const auto &MRI = MF->getRegInfo();
655 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000656 const auto *TRI = SubTarget.getRegisterInfo();
657 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000658 const auto *TII = SubTarget.getInstrInfo();
659 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000660 if (MI.isCFIInstruction())
661 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000662
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000663 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000664 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000665 unsigned I = 0, E = MI.getNumOperands();
666 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
667 !MI.getOperand(I).isImplicit();
668 ++I) {
669 if (I)
670 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000671 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
672 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000673 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000674 }
675
676 if (I)
677 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000678 if (MI.getFlag(MachineInstr::FrameSetup))
679 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000680 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000681 if (I < E)
682 OS << ' ';
683
684 bool NeedComma = false;
685 for (; I < E; ++I) {
686 if (NeedComma)
687 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000688 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
689 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000690 NeedComma = true;
691 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000692
693 if (MI.getDebugLoc()) {
694 if (NeedComma)
695 OS << ',';
696 OS << " debug-location ";
697 MI.getDebugLoc()->printAsOperand(OS, MST);
698 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000699
700 if (!MI.memoperands_empty()) {
701 OS << " :: ";
702 bool NeedComma = false;
703 for (const auto *Op : MI.memoperands()) {
704 if (NeedComma)
705 OS << ", ";
706 print(*Op);
707 NeedComma = true;
708 }
709 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000710}
711
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000712void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
713 OS << "%bb." << MBB.getNumber();
714 if (const auto *BB = MBB.getBasicBlock()) {
715 if (BB->hasName())
716 OS << '.' << BB->getName();
717 }
718}
719
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000720static void printIRSlotNumber(raw_ostream &OS, int Slot) {
721 if (Slot == -1)
722 OS << "<badref>";
723 else
724 OS << Slot;
725}
726
Alex Lorenzdeb53492015-07-28 17:28:03 +0000727void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
728 OS << "%ir-block.";
729 if (BB.hasName()) {
730 printLLVMNameWithoutPrefix(OS, BB.getName());
731 return;
732 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000733 const Function *F = BB.getParent();
734 int Slot;
735 if (F == MST.getCurrentFunction()) {
736 Slot = MST.getLocalSlot(&BB);
737 } else {
738 ModuleSlotTracker CustomMST(F->getParent(),
739 /*ShouldInitializeAllMetadata=*/false);
740 CustomMST.incorporateFunction(*F);
741 Slot = CustomMST.getLocalSlot(&BB);
742 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000743 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000744}
745
Alex Lorenz4af7e612015-08-03 23:08:19 +0000746void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000747 if (isa<GlobalValue>(V)) {
748 V.printAsOperand(OS, /*PrintType=*/false, MST);
749 return;
750 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000751 if (isa<Constant>(V)) {
752 // Machine memory operands can load/store to/from constant value pointers.
753 OS << '`';
754 V.printAsOperand(OS, /*PrintType=*/true, MST);
755 OS << '`';
756 return;
757 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000758 OS << "%ir.";
759 if (V.hasName()) {
760 printLLVMNameWithoutPrefix(OS, V.getName());
761 return;
762 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000763 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000764}
765
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000766void MIPrinter::printStackObjectReference(int FrameIndex) {
767 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
768 assert(ObjectInfo != StackObjectOperandMapping.end() &&
769 "Invalid frame index");
770 const FrameIndexOperand &Operand = ObjectInfo->second;
771 if (Operand.IsFixed) {
772 OS << "%fixed-stack." << Operand.ID;
773 return;
774 }
775 OS << "%stack." << Operand.ID;
776 if (!Operand.Name.empty())
777 OS << '.' << Operand.Name;
778}
779
Alex Lorenz5672a892015-08-05 22:26:15 +0000780void MIPrinter::printOffset(int64_t Offset) {
781 if (Offset == 0)
782 return;
783 if (Offset < 0) {
784 OS << " - " << -Offset;
785 return;
786 }
787 OS << " + " << Offset;
788}
789
Alex Lorenz49873a82015-08-06 00:44:07 +0000790static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
791 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
792 for (const auto &I : Flags) {
793 if (I.first == TF) {
794 return I.second;
795 }
796 }
797 return nullptr;
798}
799
800void MIPrinter::printTargetFlags(const MachineOperand &Op) {
801 if (!Op.getTargetFlags())
802 return;
803 const auto *TII =
804 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
805 assert(TII && "expected instruction info");
806 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
807 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000808 const bool HasDirectFlags = Flags.first;
809 const bool HasBitmaskFlags = Flags.second;
810 if (!HasDirectFlags && !HasBitmaskFlags) {
811 OS << "<unknown>) ";
812 return;
813 }
814 if (HasDirectFlags) {
815 if (const auto *Name = getTargetFlagName(TII, Flags.first))
816 OS << Name;
817 else
818 OS << "<unknown target flag>";
819 }
820 if (!HasBitmaskFlags) {
821 OS << ") ";
822 return;
823 }
824 bool IsCommaNeeded = HasDirectFlags;
825 unsigned BitMask = Flags.second;
826 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
827 for (const auto &Mask : BitMasks) {
828 // Check if the flag's bitmask has the bits of the current mask set.
829 if ((BitMask & Mask.first) == Mask.first) {
830 if (IsCommaNeeded)
831 OS << ", ";
832 IsCommaNeeded = true;
833 OS << Mask.second;
834 // Clear the bits which were serialized from the flag's bitmask.
835 BitMask &= ~(Mask.first);
836 }
837 }
838 if (BitMask) {
839 // When the resulting flag's bitmask isn't zero, we know that we didn't
840 // serialize all of the bit flags.
841 if (IsCommaNeeded)
842 OS << ", ";
843 OS << "<unknown bitmask target flag>";
844 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000845 OS << ") ";
846}
847
Alex Lorenzef5c1962015-07-28 23:02:45 +0000848static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
849 const auto *TII = MF.getSubtarget().getInstrInfo();
850 assert(TII && "expected instruction info");
851 auto Indices = TII->getSerializableTargetIndices();
852 for (const auto &I : Indices) {
853 if (I.first == Index) {
854 return I.second;
855 }
856 }
857 return nullptr;
858}
859
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000860void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000861 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
862 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000863 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000864 switch (Op.getType()) {
865 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000866 if (Op.isImplicit())
867 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000868 else if (!IsDef && Op.isDef())
869 // Print the 'def' flag only when the operand is defined after '='.
870 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000871 if (Op.isInternalRead())
872 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000873 if (Op.isDead())
874 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000875 if (Op.isKill())
876 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000877 if (Op.isUndef())
878 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000879 if (Op.isEarlyClobber())
880 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000881 if (Op.isDebug())
882 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000883 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000884 // Print the sub register.
885 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000886 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000887 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
888 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000889 if (TypeToPrint.isValid())
890 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000891 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000892 case MachineOperand::MO_Immediate:
893 OS << Op.getImm();
894 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000895 case MachineOperand::MO_CImmediate:
896 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
897 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000898 case MachineOperand::MO_FPImmediate:
899 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
900 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000901 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000902 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000903 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000904 case MachineOperand::MO_FrameIndex:
905 printStackObjectReference(Op.getIndex());
906 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000907 case MachineOperand::MO_ConstantPoolIndex:
908 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000909 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000910 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000911 case MachineOperand::MO_TargetIndex: {
912 OS << "target-index(";
913 if (const auto *Name = getTargetIndexName(
914 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
915 OS << Name;
916 else
917 OS << "<unknown>";
918 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000919 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000920 break;
921 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000922 case MachineOperand::MO_JumpTableIndex:
923 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000924 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000925 case MachineOperand::MO_ExternalSymbol:
926 OS << '$';
927 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000928 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000929 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000930 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000931 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000932 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000933 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000934 case MachineOperand::MO_BlockAddress:
935 OS << "blockaddress(";
936 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
937 MST);
938 OS << ", ";
939 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
940 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000941 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000942 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000943 case MachineOperand::MO_RegisterMask: {
944 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
945 if (RegMaskInfo != RegisterMaskIds.end())
946 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
947 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000948 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000949 break;
950 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000951 case MachineOperand::MO_RegisterLiveOut: {
952 const uint32_t *RegMask = Op.getRegLiveOut();
953 OS << "liveout(";
954 bool IsCommaNeeded = false;
955 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
956 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
957 if (IsCommaNeeded)
958 OS << ", ";
959 printReg(Reg, OS, TRI);
960 IsCommaNeeded = true;
961 }
962 }
963 OS << ")";
964 break;
965 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000966 case MachineOperand::MO_Metadata:
967 Op.getMetadata()->printAsOperand(OS, MST);
968 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000969 case MachineOperand::MO_MCSymbol:
970 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
971 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000972 case MachineOperand::MO_CFIIndex: {
Matthias Braunf23ef432016-11-30 23:48:42 +0000973 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
974 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000975 break;
976 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000977 case MachineOperand::MO_IntrinsicID: {
978 Intrinsic::ID ID = Op.getIntrinsicID();
979 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000980 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000981 else {
982 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
983 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
984 OS << "intrinsic(@" << TII->getName(ID) << ')';
985 }
986 break;
987 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000988 case MachineOperand::MO_Predicate: {
989 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
990 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
991 << CmpInst::getPredicateName(Pred) << ')';
992 break;
993 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000994 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000995}
996
Alex Lorenz4af7e612015-08-03 23:08:19 +0000997void MIPrinter::print(const MachineMemOperand &Op) {
998 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000999 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001000 if (Op.isVolatile())
1001 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001002 if (Op.isNonTemporal())
1003 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001004 if (Op.isDereferenceable())
1005 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001006 if (Op.isInvariant())
1007 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001008 if (Op.isLoad())
1009 OS << "load ";
1010 else {
1011 assert(Op.isStore() && "Non load machine operand must be a store");
1012 OS << "store ";
1013 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001014
1015 if (Op.getSynchScope() == SynchronizationScope::SingleThread)
1016 OS << "singlethread ";
1017
1018 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1019 OS << toIRString(Op.getOrdering()) << ' ';
1020 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1021 OS << toIRString(Op.getFailureOrdering()) << ' ';
1022
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001023 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001024 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001025 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001026 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001027 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1028 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001029 assert(PVal && "Expected a pseudo source value");
1030 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001031 case PseudoSourceValue::Stack:
1032 OS << "stack";
1033 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001034 case PseudoSourceValue::GOT:
1035 OS << "got";
1036 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001037 case PseudoSourceValue::JumpTable:
1038 OS << "jump-table";
1039 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001040 case PseudoSourceValue::ConstantPool:
1041 OS << "constant-pool";
1042 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001043 case PseudoSourceValue::FixedStack:
1044 printStackObjectReference(
1045 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1046 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001047 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001048 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001049 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1050 OS, /*PrintType=*/false, MST);
1051 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001052 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001053 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001054 printLLVMNameWithoutPrefix(
1055 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001056 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001057 case PseudoSourceValue::TargetCustom:
1058 llvm_unreachable("TargetCustom pseudo source values are not supported");
1059 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001060 }
1061 }
Alex Lorenz83127732015-08-07 20:26:52 +00001062 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001063 if (Op.getBaseAlignment() != Op.getSize())
1064 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001065 auto AAInfo = Op.getAAInfo();
1066 if (AAInfo.TBAA) {
1067 OS << ", !tbaa ";
1068 AAInfo.TBAA->printAsOperand(OS, MST);
1069 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001070 if (AAInfo.Scope) {
1071 OS << ", !alias.scope ";
1072 AAInfo.Scope->printAsOperand(OS, MST);
1073 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001074 if (AAInfo.NoAlias) {
1075 OS << ", !noalias ";
1076 AAInfo.NoAlias->printAsOperand(OS, MST);
1077 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001078 if (Op.getRanges()) {
1079 OS << ", !range ";
1080 Op.getRanges()->printAsOperand(OS, MST);
1081 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001082 OS << ')';
1083}
1084
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001085static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1086 const TargetRegisterInfo *TRI) {
1087 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1088 if (Reg == -1) {
1089 OS << "<badreg>";
1090 return;
1091 }
1092 printReg(Reg, OS, TRI);
1093}
1094
1095void MIPrinter::print(const MCCFIInstruction &CFI,
1096 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001097 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001098 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001099 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001100 if (CFI.getLabel())
1101 OS << "<mcsymbol> ";
1102 printCFIRegister(CFI.getRegister(), OS, TRI);
1103 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001104 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001105 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001106 if (CFI.getLabel())
1107 OS << "<mcsymbol> ";
1108 printCFIRegister(CFI.getRegister(), OS, TRI);
1109 OS << ", " << CFI.getOffset();
1110 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001111 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001112 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001113 if (CFI.getLabel())
1114 OS << "<mcsymbol> ";
1115 printCFIRegister(CFI.getRegister(), OS, TRI);
1116 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001117 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001118 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001119 if (CFI.getLabel())
1120 OS << "<mcsymbol> ";
1121 OS << CFI.getOffset();
1122 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001123 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001124 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001125 if (CFI.getLabel())
1126 OS << "<mcsymbol> ";
1127 printCFIRegister(CFI.getRegister(), OS, TRI);
1128 OS << ", " << CFI.getOffset();
1129 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001130 default:
1131 // TODO: Print the other CFI Operations.
1132 OS << "<unserializable cfi operation>";
1133 break;
1134 }
1135}
1136
Alex Lorenz345c1442015-06-15 23:52:35 +00001137void llvm::printMIR(raw_ostream &OS, const Module &M) {
1138 yaml::Output Out(OS);
1139 Out << const_cast<Module &>(M);
1140}
1141
1142void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1143 MIRPrinter Printer(OS);
1144 Printer.print(MF);
1145}