blob: c524a9835f338eb7d3c1a939c3eb72df3d04cd13 [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
Eugene Zelenkofb69e662017-06-06 22:22:41 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/None.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000017#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000023#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenzab980492015-07-20 20:51:18 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000027#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000031#include "llvm/CodeGen/MachineOperand.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000033#include "llvm/CodeGen/MIRPrinter.h"
34#include "llvm/CodeGen/MIRYamlMapping.h"
35#include "llvm/CodeGen/PseudoSourceValue.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000036#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000037#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000038#include "llvm/IR/DebugInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000039#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalValue.h"
42#include "llvm/IR/InstrTypes.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000043#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000044#include "llvm/IR/Intrinsics.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000045#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000046#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000047#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000048#include "llvm/IR/Value.h"
49#include "llvm/MC/LaneBitmask.h"
50#include "llvm/MC/MCDwarf.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000051#include "llvm/MC/MCSymbol.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000052#include "llvm/Support/AtomicOrdering.h"
53#include "llvm/Support/BranchProbability.h"
54#include "llvm/Support/Casting.h"
55#include "llvm/Support/CommandLine.h"
56#include "llvm/Support/ErrorHandling.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000057#include "llvm/Support/Format.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000058#include "llvm/Support/LowLevelTypeImpl.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000059#include "llvm/Support/raw_ostream.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000060#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000061#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000062#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000063#include "llvm/Target/TargetMachine.h"
64#include "llvm/Target/TargetRegisterInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000065#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000066#include <algorithm>
67#include <cassert>
68#include <cinttypes>
69#include <cstdint>
70#include <iterator>
71#include <string>
72#include <utility>
73#include <vector>
Alex Lorenz345c1442015-06-15 23:52:35 +000074
75using namespace llvm;
76
Matthias Braun89401142017-05-05 21:09:30 +000077static cl::opt<bool> SimplifyMIR("simplify-mir",
78 cl::desc("Leave out unnecessary information when printing MIR"));
79
Alex Lorenz345c1442015-06-15 23:52:35 +000080namespace {
81
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000082/// This structure describes how to print out stack object references.
83struct FrameIndexOperand {
84 std::string Name;
85 unsigned ID;
86 bool IsFixed;
87
88 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
89 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
90
91 /// Return an ordinary stack object reference.
92 static FrameIndexOperand create(StringRef Name, unsigned ID) {
93 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
94 }
95
96 /// Return a fixed stack object reference.
97 static FrameIndexOperand createFixed(unsigned ID) {
98 return FrameIndexOperand("", ID, /*IsFixed=*/true);
99 }
100};
101
Alex Lorenz618b2832015-07-30 16:54:38 +0000102} // end anonymous namespace
103
104namespace llvm {
105
Alex Lorenz345c1442015-06-15 23:52:35 +0000106/// This class prints out the machine functions using the MIR serialization
107/// format.
108class MIRPrinter {
109 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000110 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000111 /// Maps from stack object indices to operand indices which will be used when
112 /// printing frame index machine operands.
113 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +0000114
115public:
116 MIRPrinter(raw_ostream &OS) : OS(OS) {}
117
118 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000119
Alex Lorenz28148ba2015-07-09 22:23:13 +0000120 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
121 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000122 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
123 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +0000124 void convert(yaml::MachineFunction &MF,
125 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000126 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
127 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +0000128 void convertStackObjects(yaml::MachineFunction &YMF,
129 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000130
131private:
132 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000133};
134
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000135/// This class prints out the machine instructions using the MIR serialization
136/// format.
137class MIPrinter {
138 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000139 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000140 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000141 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000142
Matthias Braun89401142017-05-05 21:09:30 +0000143 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
144 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
145
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000146public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000147 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000148 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
149 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
150 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
151 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000152
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000153 void print(const MachineBasicBlock &MBB);
154
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000155 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000156 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000157 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000158 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000159 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000160 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000161 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000162 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000163 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000164 LLT TypeToPrint, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000165 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000166
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000167 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000168};
169
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000170} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000171
172namespace llvm {
173namespace yaml {
174
175/// This struct serializes the LLVM IR module.
176template <> struct BlockScalarTraits<Module> {
177 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
178 Mod.print(OS, nullptr);
179 }
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000180
Alex Lorenz345c1442015-06-15 23:52:35 +0000181 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
182 llvm_unreachable("LLVM Module is supposed to be parsed separately");
183 return "";
184 }
185};
186
187} // end namespace yaml
188} // end namespace llvm
189
Alex Lorenz15a00a82015-07-14 21:18:25 +0000190static void printReg(unsigned Reg, raw_ostream &OS,
191 const TargetRegisterInfo *TRI) {
192 // TODO: Print Stack Slots.
193 if (!Reg)
194 OS << '_';
195 else if (TargetRegisterInfo::isVirtualRegister(Reg))
196 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
197 else if (Reg < TRI->getNumRegs())
198 OS << '%' << StringRef(TRI->getName(Reg)).lower();
199 else
200 llvm_unreachable("Can't print this kind of register yet");
201}
202
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000203static void printReg(unsigned Reg, yaml::StringValue &Dest,
204 const TargetRegisterInfo *TRI) {
205 raw_string_ostream OS(Dest.Value);
206 printReg(Reg, OS, TRI);
207}
208
Alex Lorenz345c1442015-06-15 23:52:35 +0000209void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000210 initRegisterMaskIds(MF);
211
Alex Lorenz345c1442015-06-15 23:52:35 +0000212 yaml::MachineFunction YamlMF;
213 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000214 YamlMF.Alignment = MF.getAlignment();
215 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000216
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000217 YamlMF.Legalized = MF.getProperties().hasProperty(
218 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000219 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
220 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000221 YamlMF.Selected = MF.getProperties().hasProperty(
222 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000223
Alex Lorenz28148ba2015-07-09 22:23:13 +0000224 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000225 ModuleSlotTracker MST(MF.getFunction()->getParent());
226 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000227 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000228 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000229 if (const auto *ConstantPool = MF.getConstantPool())
230 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000231 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
232 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000233 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
234 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000235 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000236 if (IsNewlineNeeded)
237 StrOS << "\n";
238 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
239 .print(MBB);
240 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000241 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000242 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000243 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000244 if (!SimplifyMIR)
245 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000246 Out << YamlMF;
247}
248
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000249static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
250 const TargetRegisterInfo *TRI) {
251 assert(RegMask && "Can't print an empty register mask");
252 OS << StringRef("CustomRegMask(");
253
254 bool IsRegInRegMaskFound = false;
255 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
256 // Check whether the register is asserted in regmask.
257 if (RegMask[I / 32] & (1u << (I % 32))) {
258 if (IsRegInRegMaskFound)
259 OS << ',';
260 printReg(I, OS, TRI);
261 IsRegInRegMaskFound = true;
262 }
263 }
264
265 OS << ')';
266}
267
Alex Lorenz54565cf2015-06-24 19:56:10 +0000268void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000269 const MachineRegisterInfo &RegInfo,
270 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000271 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000272
273 // Print the virtual register definitions.
274 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
275 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
276 yaml::VirtualRegisterDefinition VReg;
277 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000278 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000279 VReg.Class =
280 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000281 else if (RegInfo.getRegBankOrNull(Reg))
282 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000283 else {
284 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000285 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000286 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000287 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000288 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
289 if (PreferredReg)
290 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000291 MF.VirtualRegisters.push_back(VReg);
292 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000293
294 // Print the live ins.
295 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
296 yaml::MachineFunctionLiveIn LiveIn;
297 printReg(I->first, LiveIn.Register, TRI);
298 if (I->second)
299 printReg(I->second, LiveIn.VirtualRegister, TRI);
300 MF.LiveIns.push_back(LiveIn);
301 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000302
303 // Prints the callee saved registers.
304 if (RegInfo.isUpdatedCSRsInitialized()) {
305 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
306 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
307 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000308 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000309 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000310 CalleeSavedRegisters.push_back(Reg);
311 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000312 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000313 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000314}
315
Alex Lorenza6f9a372015-07-29 21:09:09 +0000316void MIRPrinter::convert(ModuleSlotTracker &MST,
317 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000318 const MachineFrameInfo &MFI) {
319 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
320 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
321 YamlMFI.HasStackMap = MFI.hasStackMap();
322 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
323 YamlMFI.StackSize = MFI.getStackSize();
324 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
325 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
326 YamlMFI.AdjustsStack = MFI.adjustsStack();
327 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000328 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
329 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000330 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
331 YamlMFI.HasVAStart = MFI.hasVAStart();
332 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000333 if (MFI.getSavePoint()) {
334 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
335 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
336 .printMBBReference(*MFI.getSavePoint());
337 }
338 if (MFI.getRestorePoint()) {
339 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
340 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
341 .printMBBReference(*MFI.getRestorePoint());
342 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000343}
344
Matthias Braunef331ef2016-11-30 23:48:50 +0000345void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
346 const MachineFunction &MF,
347 ModuleSlotTracker &MST) {
348 const MachineFrameInfo &MFI = MF.getFrameInfo();
349 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000350 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000351 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000352 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
353 if (MFI.isDeadObjectIndex(I))
354 continue;
355
356 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000357 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000358 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
359 ? yaml::FixedMachineStackObject::SpillSlot
360 : yaml::FixedMachineStackObject::DefaultType;
361 YamlObject.Offset = MFI.getObjectOffset(I);
362 YamlObject.Size = MFI.getObjectSize(I);
363 YamlObject.Alignment = MFI.getObjectAlignment(I);
364 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
365 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000366 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000367 StackObjectOperandMapping.insert(
368 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000369 }
370
371 // Process ordinary stack objects.
372 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000373 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
374 if (MFI.isDeadObjectIndex(I))
375 continue;
376
377 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000378 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000379 if (const auto *Alloca = MFI.getObjectAllocation(I))
380 YamlObject.Name.Value =
381 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000382 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
383 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000384 : MFI.isVariableSizedObjectIndex(I)
385 ? yaml::MachineStackObject::VariableSized
386 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000387 YamlObject.Offset = MFI.getObjectOffset(I);
388 YamlObject.Size = MFI.getObjectSize(I);
389 YamlObject.Alignment = MFI.getObjectAlignment(I);
390
Matthias Braunef331ef2016-11-30 23:48:50 +0000391 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000392 StackObjectOperandMapping.insert(std::make_pair(
393 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000394 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000395
396 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
397 yaml::StringValue Reg;
398 printReg(CSInfo.getReg(), Reg, TRI);
399 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
400 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
401 "Invalid stack object index");
402 const FrameIndexOperand &StackObject = StackObjectInfo->second;
403 if (StackObject.IsFixed)
Matthias Braunef331ef2016-11-30 23:48:50 +0000404 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000405 else
Matthias Braunef331ef2016-11-30 23:48:50 +0000406 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000407 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000408 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
409 auto LocalObject = MFI.getLocalFrameObjectMap(I);
410 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
411 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
412 "Invalid stack object index");
413 const FrameIndexOperand &StackObject = StackObjectInfo->second;
414 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000415 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000416 }
Alex Lorenza314d812015-08-18 22:26:26 +0000417
418 // Print the stack object references in the frame information class after
419 // converting the stack objects.
420 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000421 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000422 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
423 .printStackObjectReference(MFI.getStackProtectorIndex());
424 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000425
426 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000427 for (const MachineFunction::VariableDbgInfo &DebugVar :
428 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000429 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
430 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
431 "Invalid stack object index");
432 const FrameIndexOperand &StackObject = StackObjectInfo->second;
433 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000434 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000435 {
436 raw_string_ostream StrOS(Object.DebugVar.Value);
437 DebugVar.Var->printAsOperand(StrOS, MST);
438 }
439 {
440 raw_string_ostream StrOS(Object.DebugExpr.Value);
441 DebugVar.Expr->printAsOperand(StrOS, MST);
442 }
443 {
444 raw_string_ostream StrOS(Object.DebugLoc.Value);
445 DebugVar.Loc->printAsOperand(StrOS, MST);
446 }
447 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000448}
449
Alex Lorenzab980492015-07-20 20:51:18 +0000450void MIRPrinter::convert(yaml::MachineFunction &MF,
451 const MachineConstantPool &ConstantPool) {
452 unsigned ID = 0;
453 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
454 // TODO: Serialize target specific constant pool entries.
455 if (Constant.isMachineConstantPoolEntry())
456 llvm_unreachable("Can't print target specific constant pool entries yet");
457
458 yaml::MachineConstantPoolValue YamlConstant;
459 std::string Str;
460 raw_string_ostream StrOS(Str);
461 Constant.Val.ConstVal->printAsOperand(StrOS);
462 YamlConstant.ID = ID++;
463 YamlConstant.Value = StrOS.str();
464 YamlConstant.Alignment = Constant.getAlignment();
465 MF.Constants.push_back(YamlConstant);
466 }
467}
468
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000469void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000470 yaml::MachineJumpTable &YamlJTI,
471 const MachineJumpTableInfo &JTI) {
472 YamlJTI.Kind = JTI.getEntryKind();
473 unsigned ID = 0;
474 for (const auto &Table : JTI.getJumpTables()) {
475 std::string Str;
476 yaml::MachineJumpTable::Entry Entry;
477 Entry.ID = ID++;
478 for (const auto *MBB : Table.MBBs) {
479 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000480 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
481 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000482 Entry.Blocks.push_back(StrOS.str());
483 Str.clear();
484 }
485 YamlJTI.Entries.push_back(Entry);
486 }
487}
488
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000489void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
490 const auto *TRI = MF.getSubtarget().getRegisterInfo();
491 unsigned I = 0;
492 for (const uint32_t *Mask : TRI->getRegMasks())
493 RegisterMaskIds.insert(std::make_pair(Mask, I++));
494}
495
Matthias Braun89401142017-05-05 21:09:30 +0000496void llvm::guessSuccessors(const MachineBasicBlock &MBB,
497 SmallVectorImpl<MachineBasicBlock*> &Result,
498 bool &IsFallthrough) {
499 SmallPtrSet<MachineBasicBlock*,8> Seen;
500
501 for (const MachineInstr &MI : MBB) {
502 if (MI.isPHI())
503 continue;
504 for (const MachineOperand &MO : MI.operands()) {
505 if (!MO.isMBB())
506 continue;
507 MachineBasicBlock *Succ = MO.getMBB();
508 auto RP = Seen.insert(Succ);
509 if (RP.second)
510 Result.push_back(Succ);
511 }
512 }
513 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
514 IsFallthrough = I == MBB.end() || !I->isBarrier();
515}
516
517bool
518MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
519 if (MBB.succ_size() <= 1)
520 return true;
521 if (!MBB.hasSuccessorProbabilities())
522 return true;
523
524 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
525 MBB.Probs.end());
526 BranchProbability::normalizeProbabilities(Normalized.begin(),
527 Normalized.end());
528 SmallVector<BranchProbability,8> Equal(Normalized.size());
529 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
530
531 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
532}
533
534bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
535 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
536 bool GuessedFallthrough;
537 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
538 if (GuessedFallthrough) {
539 const MachineFunction &MF = *MBB.getParent();
540 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
541 if (NextI != MF.end()) {
542 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
543 if (!is_contained(GuessedSuccs, Next))
544 GuessedSuccs.push_back(Next);
545 }
546 }
547 if (GuessedSuccs.size() != MBB.succ_size())
548 return false;
549 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
550}
551
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000552void MIPrinter::print(const MachineBasicBlock &MBB) {
553 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
554 OS << "bb." << MBB.getNumber();
555 bool HasAttributes = false;
556 if (const auto *BB = MBB.getBasicBlock()) {
557 if (BB->hasName()) {
558 OS << "." << BB->getName();
559 } else {
560 HasAttributes = true;
561 OS << " (";
562 int Slot = MST.getLocalSlot(BB);
563 if (Slot == -1)
564 OS << "<ir-block badref>";
565 else
566 OS << (Twine("%ir-block.") + Twine(Slot)).str();
567 }
568 }
569 if (MBB.hasAddressTaken()) {
570 OS << (HasAttributes ? ", " : " (");
571 OS << "address-taken";
572 HasAttributes = true;
573 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000574 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000575 OS << (HasAttributes ? ", " : " (");
576 OS << "landing-pad";
577 HasAttributes = true;
578 }
579 if (MBB.getAlignment()) {
580 OS << (HasAttributes ? ", " : " (");
581 OS << "align " << MBB.getAlignment();
582 HasAttributes = true;
583 }
584 if (HasAttributes)
585 OS << ")";
586 OS << ":\n";
587
588 bool HasLineAttributes = false;
589 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000590 bool canPredictProbs = canPredictBranchProbabilities(MBB);
591 if (!MBB.succ_empty() && (!SimplifyMIR || !canPredictProbs ||
592 !canPredictSuccessors(MBB))) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000593 OS.indent(2) << "successors: ";
594 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
595 if (I != MBB.succ_begin())
596 OS << ", ";
597 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000598 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000599 OS << '('
600 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
601 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000602 }
603 OS << "\n";
604 HasLineAttributes = true;
605 }
606
607 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000608 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
609 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
610 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000611 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000612 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000613 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000614 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000616 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000617 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000618 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000619 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000620 }
621 OS << "\n";
622 HasLineAttributes = true;
623 }
624
625 if (HasLineAttributes)
626 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000627 bool IsInBundle = false;
628 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
629 const MachineInstr &MI = *I;
630 if (IsInBundle && !MI.isInsideBundle()) {
631 OS.indent(2) << "}\n";
632 IsInBundle = false;
633 }
634 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000635 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000636 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
637 OS << " {";
638 IsInBundle = true;
639 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000640 OS << "\n";
641 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000642 if (IsInBundle)
643 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000644}
645
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000646/// Return true when an instruction has tied register that can't be determined
647/// by the instruction's descriptor.
648static bool hasComplexRegisterTies(const MachineInstr &MI) {
649 const MCInstrDesc &MCID = MI.getDesc();
650 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
651 const auto &Operand = MI.getOperand(I);
652 if (!Operand.isReg() || Operand.isDef())
653 // Ignore the defined registers as MCID marks only the uses as tied.
654 continue;
655 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
656 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
657 if (ExpectedTiedIdx != TiedIdx)
658 return true;
659 }
660 return false;
661}
662
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000663static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
664 SmallBitVector &PrintedTypes,
665 const MachineRegisterInfo &MRI) {
666 const MachineOperand &Op = MI.getOperand(OpIdx);
667 if (!Op.isReg())
668 return LLT{};
669
670 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
671 return MRI.getType(Op.getReg());
672
673 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
674 if (!OpInfo.isGenericType())
675 return MRI.getType(Op.getReg());
676
677 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
678 return LLT{};
679
680 PrintedTypes.set(OpInfo.getGenericTypeIndex());
681 return MRI.getType(Op.getReg());
682}
683
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000684void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000685 const auto *MF = MI.getParent()->getParent();
686 const auto &MRI = MF->getRegInfo();
687 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000688 const auto *TRI = SubTarget.getRegisterInfo();
689 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000690 const auto *TII = SubTarget.getInstrInfo();
691 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000692 if (MI.isCFIInstruction())
693 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000694
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000695 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000696 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000697 unsigned I = 0, E = MI.getNumOperands();
698 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
699 !MI.getOperand(I).isImplicit();
700 ++I) {
701 if (I)
702 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000703 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
704 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000705 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000706 }
707
708 if (I)
709 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000710 if (MI.getFlag(MachineInstr::FrameSetup))
711 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000712 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000713 if (I < E)
714 OS << ' ';
715
716 bool NeedComma = false;
717 for (; I < E; ++I) {
718 if (NeedComma)
719 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000720 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
721 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000722 NeedComma = true;
723 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000724
725 if (MI.getDebugLoc()) {
726 if (NeedComma)
727 OS << ',';
728 OS << " debug-location ";
729 MI.getDebugLoc()->printAsOperand(OS, MST);
730 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000731
732 if (!MI.memoperands_empty()) {
733 OS << " :: ";
734 bool NeedComma = false;
735 for (const auto *Op : MI.memoperands()) {
736 if (NeedComma)
737 OS << ", ";
738 print(*Op);
739 NeedComma = true;
740 }
741 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000742}
743
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000744void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
745 OS << "%bb." << MBB.getNumber();
746 if (const auto *BB = MBB.getBasicBlock()) {
747 if (BB->hasName())
748 OS << '.' << BB->getName();
749 }
750}
751
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000752static void printIRSlotNumber(raw_ostream &OS, int Slot) {
753 if (Slot == -1)
754 OS << "<badref>";
755 else
756 OS << Slot;
757}
758
Alex Lorenzdeb53492015-07-28 17:28:03 +0000759void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
760 OS << "%ir-block.";
761 if (BB.hasName()) {
762 printLLVMNameWithoutPrefix(OS, BB.getName());
763 return;
764 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000765 const Function *F = BB.getParent();
766 int Slot;
767 if (F == MST.getCurrentFunction()) {
768 Slot = MST.getLocalSlot(&BB);
769 } else {
770 ModuleSlotTracker CustomMST(F->getParent(),
771 /*ShouldInitializeAllMetadata=*/false);
772 CustomMST.incorporateFunction(*F);
773 Slot = CustomMST.getLocalSlot(&BB);
774 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000775 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000776}
777
Alex Lorenz4af7e612015-08-03 23:08:19 +0000778void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000779 if (isa<GlobalValue>(V)) {
780 V.printAsOperand(OS, /*PrintType=*/false, MST);
781 return;
782 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000783 if (isa<Constant>(V)) {
784 // Machine memory operands can load/store to/from constant value pointers.
785 OS << '`';
786 V.printAsOperand(OS, /*PrintType=*/true, MST);
787 OS << '`';
788 return;
789 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000790 OS << "%ir.";
791 if (V.hasName()) {
792 printLLVMNameWithoutPrefix(OS, V.getName());
793 return;
794 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000795 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000796}
797
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000798void MIPrinter::printStackObjectReference(int FrameIndex) {
799 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
800 assert(ObjectInfo != StackObjectOperandMapping.end() &&
801 "Invalid frame index");
802 const FrameIndexOperand &Operand = ObjectInfo->second;
803 if (Operand.IsFixed) {
804 OS << "%fixed-stack." << Operand.ID;
805 return;
806 }
807 OS << "%stack." << Operand.ID;
808 if (!Operand.Name.empty())
809 OS << '.' << Operand.Name;
810}
811
Alex Lorenz5672a892015-08-05 22:26:15 +0000812void MIPrinter::printOffset(int64_t Offset) {
813 if (Offset == 0)
814 return;
815 if (Offset < 0) {
816 OS << " - " << -Offset;
817 return;
818 }
819 OS << " + " << Offset;
820}
821
Alex Lorenz49873a82015-08-06 00:44:07 +0000822static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
823 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
824 for (const auto &I : Flags) {
825 if (I.first == TF) {
826 return I.second;
827 }
828 }
829 return nullptr;
830}
831
832void MIPrinter::printTargetFlags(const MachineOperand &Op) {
833 if (!Op.getTargetFlags())
834 return;
835 const auto *TII =
836 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
837 assert(TII && "expected instruction info");
838 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
839 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000840 const bool HasDirectFlags = Flags.first;
841 const bool HasBitmaskFlags = Flags.second;
842 if (!HasDirectFlags && !HasBitmaskFlags) {
843 OS << "<unknown>) ";
844 return;
845 }
846 if (HasDirectFlags) {
847 if (const auto *Name = getTargetFlagName(TII, Flags.first))
848 OS << Name;
849 else
850 OS << "<unknown target flag>";
851 }
852 if (!HasBitmaskFlags) {
853 OS << ") ";
854 return;
855 }
856 bool IsCommaNeeded = HasDirectFlags;
857 unsigned BitMask = Flags.second;
858 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
859 for (const auto &Mask : BitMasks) {
860 // Check if the flag's bitmask has the bits of the current mask set.
861 if ((BitMask & Mask.first) == Mask.first) {
862 if (IsCommaNeeded)
863 OS << ", ";
864 IsCommaNeeded = true;
865 OS << Mask.second;
866 // Clear the bits which were serialized from the flag's bitmask.
867 BitMask &= ~(Mask.first);
868 }
869 }
870 if (BitMask) {
871 // When the resulting flag's bitmask isn't zero, we know that we didn't
872 // serialize all of the bit flags.
873 if (IsCommaNeeded)
874 OS << ", ";
875 OS << "<unknown bitmask target flag>";
876 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000877 OS << ") ";
878}
879
Alex Lorenzef5c1962015-07-28 23:02:45 +0000880static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
881 const auto *TII = MF.getSubtarget().getInstrInfo();
882 assert(TII && "expected instruction info");
883 auto Indices = TII->getSerializableTargetIndices();
884 for (const auto &I : Indices) {
885 if (I.first == Index) {
886 return I.second;
887 }
888 }
889 return nullptr;
890}
891
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000892void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000893 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
894 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000895 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000896 switch (Op.getType()) {
897 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000898 if (Op.isImplicit())
899 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000900 else if (!IsDef && Op.isDef())
901 // Print the 'def' flag only when the operand is defined after '='.
902 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000903 if (Op.isInternalRead())
904 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000905 if (Op.isDead())
906 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000907 if (Op.isKill())
908 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000909 if (Op.isUndef())
910 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000911 if (Op.isEarlyClobber())
912 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000913 if (Op.isDebug())
914 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000915 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000916 // Print the sub register.
917 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000918 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000919 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
920 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000921 if (TypeToPrint.isValid())
922 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000923 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000924 case MachineOperand::MO_Immediate:
925 OS << Op.getImm();
926 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000927 case MachineOperand::MO_CImmediate:
928 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
929 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000930 case MachineOperand::MO_FPImmediate:
931 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
932 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000933 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000934 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000935 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000936 case MachineOperand::MO_FrameIndex:
937 printStackObjectReference(Op.getIndex());
938 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000939 case MachineOperand::MO_ConstantPoolIndex:
940 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000941 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000942 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000943 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000944 OS << "target-index(";
945 if (const auto *Name = getTargetIndexName(
946 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
947 OS << Name;
948 else
949 OS << "<unknown>";
950 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000951 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000952 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000953 case MachineOperand::MO_JumpTableIndex:
954 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000955 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000956 case MachineOperand::MO_ExternalSymbol: {
957 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000958 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000959 if (Name.empty()) {
960 OS << "\"\"";
961 } else {
962 printLLVMNameWithoutPrefix(OS, Name);
963 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000964 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000965 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000966 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000967 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000968 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000969 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000970 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000971 case MachineOperand::MO_BlockAddress:
972 OS << "blockaddress(";
973 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
974 MST);
975 OS << ", ";
976 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
977 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000978 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000979 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000980 case MachineOperand::MO_RegisterMask: {
981 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
982 if (RegMaskInfo != RegisterMaskIds.end())
983 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
984 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000985 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000986 break;
987 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000988 case MachineOperand::MO_RegisterLiveOut: {
989 const uint32_t *RegMask = Op.getRegLiveOut();
990 OS << "liveout(";
991 bool IsCommaNeeded = false;
992 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
993 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
994 if (IsCommaNeeded)
995 OS << ", ";
996 printReg(Reg, OS, TRI);
997 IsCommaNeeded = true;
998 }
999 }
1000 OS << ")";
1001 break;
1002 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001003 case MachineOperand::MO_Metadata:
1004 Op.getMetadata()->printAsOperand(OS, MST);
1005 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001006 case MachineOperand::MO_MCSymbol:
1007 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1008 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001009 case MachineOperand::MO_CFIIndex: {
Matthias Braunf23ef432016-11-30 23:48:42 +00001010 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
1011 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001012 break;
1013 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001014 case MachineOperand::MO_IntrinsicID: {
1015 Intrinsic::ID ID = Op.getIntrinsicID();
1016 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001017 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001018 else {
1019 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
1020 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1021 OS << "intrinsic(@" << TII->getName(ID) << ')';
1022 }
1023 break;
1024 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001025 case MachineOperand::MO_Predicate: {
1026 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1027 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1028 << CmpInst::getPredicateName(Pred) << ')';
1029 break;
1030 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001031 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001032}
1033
Alex Lorenz4af7e612015-08-03 23:08:19 +00001034void MIPrinter::print(const MachineMemOperand &Op) {
1035 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001036 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001037 if (Op.isVolatile())
1038 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001039 if (Op.isNonTemporal())
1040 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001041 if (Op.isDereferenceable())
1042 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001043 if (Op.isInvariant())
1044 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001045 if (Op.isLoad())
1046 OS << "load ";
1047 else {
1048 assert(Op.isStore() && "Non load machine operand must be a store");
1049 OS << "store ";
1050 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001051
1052 if (Op.getSynchScope() == SynchronizationScope::SingleThread)
1053 OS << "singlethread ";
1054
1055 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1056 OS << toIRString(Op.getOrdering()) << ' ';
1057 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1058 OS << toIRString(Op.getFailureOrdering()) << ' ';
1059
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001060 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001061 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001062 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001063 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001064 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1065 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001066 assert(PVal && "Expected a pseudo source value");
1067 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001068 case PseudoSourceValue::Stack:
1069 OS << "stack";
1070 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001071 case PseudoSourceValue::GOT:
1072 OS << "got";
1073 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001074 case PseudoSourceValue::JumpTable:
1075 OS << "jump-table";
1076 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001077 case PseudoSourceValue::ConstantPool:
1078 OS << "constant-pool";
1079 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001080 case PseudoSourceValue::FixedStack:
1081 printStackObjectReference(
1082 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1083 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001084 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001085 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001086 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1087 OS, /*PrintType=*/false, MST);
1088 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001089 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001090 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001091 printLLVMNameWithoutPrefix(
1092 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001093 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001094 case PseudoSourceValue::TargetCustom:
1095 llvm_unreachable("TargetCustom pseudo source values are not supported");
1096 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001097 }
1098 }
Alex Lorenz83127732015-08-07 20:26:52 +00001099 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001100 if (Op.getBaseAlignment() != Op.getSize())
1101 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001102 auto AAInfo = Op.getAAInfo();
1103 if (AAInfo.TBAA) {
1104 OS << ", !tbaa ";
1105 AAInfo.TBAA->printAsOperand(OS, MST);
1106 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001107 if (AAInfo.Scope) {
1108 OS << ", !alias.scope ";
1109 AAInfo.Scope->printAsOperand(OS, MST);
1110 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001111 if (AAInfo.NoAlias) {
1112 OS << ", !noalias ";
1113 AAInfo.NoAlias->printAsOperand(OS, MST);
1114 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001115 if (Op.getRanges()) {
1116 OS << ", !range ";
1117 Op.getRanges()->printAsOperand(OS, MST);
1118 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001119 OS << ')';
1120}
1121
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001122static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1123 const TargetRegisterInfo *TRI) {
1124 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1125 if (Reg == -1) {
1126 OS << "<badreg>";
1127 return;
1128 }
1129 printReg(Reg, OS, TRI);
1130}
1131
1132void MIPrinter::print(const MCCFIInstruction &CFI,
1133 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001134 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001135 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001136 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001137 if (CFI.getLabel())
1138 OS << "<mcsymbol> ";
1139 printCFIRegister(CFI.getRegister(), OS, TRI);
1140 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001141 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001142 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001143 if (CFI.getLabel())
1144 OS << "<mcsymbol> ";
1145 printCFIRegister(CFI.getRegister(), OS, TRI);
1146 OS << ", " << CFI.getOffset();
1147 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001148 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001149 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001150 if (CFI.getLabel())
1151 OS << "<mcsymbol> ";
1152 printCFIRegister(CFI.getRegister(), OS, TRI);
1153 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001154 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001155 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001156 if (CFI.getLabel())
1157 OS << "<mcsymbol> ";
1158 OS << CFI.getOffset();
1159 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001160 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001161 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001162 if (CFI.getLabel())
1163 OS << "<mcsymbol> ";
1164 printCFIRegister(CFI.getRegister(), OS, TRI);
1165 OS << ", " << CFI.getOffset();
1166 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001167 default:
1168 // TODO: Print the other CFI Operations.
1169 OS << "<unserializable cfi operation>";
1170 break;
1171 }
1172}
1173
Alex Lorenz345c1442015-06-15 23:52:35 +00001174void llvm::printMIR(raw_ostream &OS, const Module &M) {
1175 yaml::Output Out(OS);
1176 Out << const_cast<Module &>(M);
1177}
1178
1179void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1180 MIRPrinter Printer(OS);
1181 Printer.print(MF);
1182}