blob: 92dc0aea421fa0f370d9d505ec840067a4362f76 [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
David Blaikie3f833ed2017-11-08 01:01:31 +000015#include "llvm/CodeGen/MIRPrinter.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/None.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000018#include "llvm/ADT/STLExtras.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000019#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000022#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000023#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000025#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000026#include "llvm/CodeGen/MIRYamlMapping.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenzab980492015-07-20 20:51:18 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000030#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000031#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineJumpTableInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000034#include "llvm/CodeGen/MachineOperand.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000040#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000041#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000042#include "llvm/IR/DebugInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000043#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/Function.h"
45#include "llvm/IR/GlobalValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000046#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000047#include "llvm/IR/InstrTypes.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000048#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000049#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000050#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000051#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000052#include "llvm/IR/Value.h"
53#include "llvm/MC/LaneBitmask.h"
54#include "llvm/MC/MCDwarf.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000055#include "llvm/MC/MCSymbol.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000056#include "llvm/Support/AtomicOrdering.h"
57#include "llvm/Support/BranchProbability.h"
58#include "llvm/Support/Casting.h"
59#include "llvm/Support/CommandLine.h"
60#include "llvm/Support/ErrorHandling.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000061#include "llvm/Support/Format.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000062#include "llvm/Support/LowLevelTypeImpl.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000063#include "llvm/Support/YAMLTraits.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000064#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000065#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000066#include "llvm/Target/TargetMachine.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000067#include <algorithm>
68#include <cassert>
69#include <cinttypes>
70#include <cstdint>
71#include <iterator>
72#include <string>
73#include <utility>
74#include <vector>
Alex Lorenz345c1442015-06-15 23:52:35 +000075
76using namespace llvm;
77
Zachary Turner8065f0b2017-12-01 00:53:10 +000078static cl::opt<bool> SimplifyMIR(
79 "simplify-mir", cl::Hidden,
Matthias Braun89401142017-05-05 21:09:30 +000080 cl::desc("Leave out unnecessary information when printing MIR"));
81
Alex Lorenz345c1442015-06-15 23:52:35 +000082namespace {
83
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000084/// This structure describes how to print out stack object references.
85struct FrameIndexOperand {
86 std::string Name;
87 unsigned ID;
88 bool IsFixed;
89
90 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
91 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
92
93 /// Return an ordinary stack object reference.
94 static FrameIndexOperand create(StringRef Name, unsigned ID) {
95 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
96 }
97
98 /// Return a fixed stack object reference.
99 static FrameIndexOperand createFixed(unsigned ID) {
100 return FrameIndexOperand("", ID, /*IsFixed=*/true);
101 }
102};
103
Alex Lorenz618b2832015-07-30 16:54:38 +0000104} // end anonymous namespace
105
106namespace llvm {
107
Alex Lorenz345c1442015-06-15 23:52:35 +0000108/// This class prints out the machine functions using the MIR serialization
109/// format.
110class MIRPrinter {
111 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000112 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000113 /// Maps from stack object indices to operand indices which will be used when
114 /// printing frame index machine operands.
115 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +0000116
117public:
118 MIRPrinter(raw_ostream &OS) : OS(OS) {}
119
120 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000121
Alex Lorenz28148ba2015-07-09 22:23:13 +0000122 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
123 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000124 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
125 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +0000126 void convert(yaml::MachineFunction &MF,
127 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000128 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
129 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +0000130 void convertStackObjects(yaml::MachineFunction &YMF,
131 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000132
133private:
134 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000135};
136
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000137/// This class prints out the machine instructions using the MIR serialization
138/// format.
139class MIPrinter {
140 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000141 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000142 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000143 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000144 /// Synchronization scope names registered with LLVMContext.
145 SmallVector<StringRef, 8> SSNs;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000146
Matthias Braun89401142017-05-05 21:09:30 +0000147 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
148 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
149
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000150public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000151 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000152 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
153 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
154 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
155 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000156
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000157 void print(const MachineBasicBlock &MBB);
158
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000159 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000160 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000161 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000162 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000163 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000164 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000165 void printTargetFlags(const MachineOperand &Op);
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000166 void print(const MachineInstr &MI, unsigned OpIdx,
167 const TargetRegisterInfo *TRI, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000168 LLT TypeToPrint, bool IsDef = false);
Geoff Berry6748abe2017-07-13 02:28:54 +0000169 void print(const LLVMContext &Context, const TargetInstrInfo &TII,
170 const MachineMemOperand &Op);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000171 void printSyncScope(const LLVMContext &Context, SyncScope::ID SSID);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000172
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000173 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000174};
175
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000176} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000177
178namespace llvm {
179namespace yaml {
180
181/// This struct serializes the LLVM IR module.
182template <> struct BlockScalarTraits<Module> {
183 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
184 Mod.print(OS, nullptr);
185 }
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000186
Alex Lorenz345c1442015-06-15 23:52:35 +0000187 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
188 llvm_unreachable("LLVM Module is supposed to be parsed separately");
189 return "";
190 }
191};
192
193} // end namespace yaml
194} // end namespace llvm
195
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000196static void printRegMIR(unsigned Reg, yaml::StringValue &Dest,
197 const TargetRegisterInfo *TRI) {
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000198 raw_string_ostream OS(Dest.Value);
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000199 OS << printReg(Reg, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000200}
201
Alex Lorenz345c1442015-06-15 23:52:35 +0000202void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000203 initRegisterMaskIds(MF);
204
Alex Lorenz345c1442015-06-15 23:52:35 +0000205 yaml::MachineFunction YamlMF;
206 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000207 YamlMF.Alignment = MF.getAlignment();
208 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000209
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000210 YamlMF.Legalized = MF.getProperties().hasProperty(
211 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000212 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
213 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000214 YamlMF.Selected = MF.getProperties().hasProperty(
215 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000216
Alex Lorenz28148ba2015-07-09 22:23:13 +0000217 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000218 ModuleSlotTracker MST(MF.getFunction()->getParent());
219 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000220 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000221 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000222 if (const auto *ConstantPool = MF.getConstantPool())
223 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000224 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
225 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000226 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
227 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000228 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000229 if (IsNewlineNeeded)
230 StrOS << "\n";
231 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
232 .print(MBB);
233 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000234 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000235 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000236 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000237 if (!SimplifyMIR)
238 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000239 Out << YamlMF;
240}
241
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000242static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
243 const TargetRegisterInfo *TRI) {
244 assert(RegMask && "Can't print an empty register mask");
245 OS << StringRef("CustomRegMask(");
246
247 bool IsRegInRegMaskFound = false;
248 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
249 // Check whether the register is asserted in regmask.
250 if (RegMask[I / 32] & (1u << (I % 32))) {
251 if (IsRegInRegMaskFound)
252 OS << ',';
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000253 OS << printReg(I, TRI);
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000254 IsRegInRegMaskFound = true;
255 }
256 }
257
258 OS << ')';
259}
260
Justin Bogner6c452832017-10-24 18:04:54 +0000261static void printRegClassOrBank(unsigned Reg, raw_ostream &OS,
262 const MachineRegisterInfo &RegInfo,
263 const TargetRegisterInfo *TRI) {
264 if (RegInfo.getRegClassOrNull(Reg))
265 OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
266 else if (RegInfo.getRegBankOrNull(Reg))
267 OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
268 else {
269 OS << "_";
270 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
271 "Generic registers must have a valid type");
272 }
273}
274
275static void printRegClassOrBank(unsigned Reg, yaml::StringValue &Dest,
276 const MachineRegisterInfo &RegInfo,
277 const TargetRegisterInfo *TRI) {
278 raw_string_ostream OS(Dest.Value);
279 printRegClassOrBank(Reg, OS, RegInfo, TRI);
280}
281
282
Alex Lorenz54565cf2015-06-24 19:56:10 +0000283void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000284 const MachineRegisterInfo &RegInfo,
285 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000286 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000287
288 // Print the virtual register definitions.
289 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
290 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
291 yaml::VirtualRegisterDefinition VReg;
292 VReg.ID = I;
Justin Bogner6c452832017-10-24 18:04:54 +0000293 printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000294 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
295 if (PreferredReg)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000296 printRegMIR(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000297 MF.VirtualRegisters.push_back(VReg);
298 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000299
300 // Print the live ins.
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000301 for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000302 yaml::MachineFunctionLiveIn LiveIn;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000303 printRegMIR(LI.first, LiveIn.Register, TRI);
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000304 if (LI.second)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000305 printRegMIR(LI.second, LiveIn.VirtualRegister, TRI);
Alex Lorenz12045a42015-07-27 17:42:45 +0000306 MF.LiveIns.push_back(LiveIn);
307 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000308
309 // Prints the callee saved registers.
310 if (RegInfo.isUpdatedCSRsInitialized()) {
311 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
312 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
313 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000314 yaml::FlowStringValue Reg;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000315 printRegMIR(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000316 CalleeSavedRegisters.push_back(Reg);
317 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000318 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000319 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000320}
321
Alex Lorenza6f9a372015-07-29 21:09:09 +0000322void MIRPrinter::convert(ModuleSlotTracker &MST,
323 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000324 const MachineFrameInfo &MFI) {
325 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
326 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
327 YamlMFI.HasStackMap = MFI.hasStackMap();
328 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
329 YamlMFI.StackSize = MFI.getStackSize();
330 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
331 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
332 YamlMFI.AdjustsStack = MFI.adjustsStack();
333 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000334 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
335 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000336 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
337 YamlMFI.HasVAStart = MFI.hasVAStart();
338 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000339 if (MFI.getSavePoint()) {
340 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
341 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
342 .printMBBReference(*MFI.getSavePoint());
343 }
344 if (MFI.getRestorePoint()) {
345 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
346 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
347 .printMBBReference(*MFI.getRestorePoint());
348 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000349}
350
Matthias Braunef331ef2016-11-30 23:48:50 +0000351void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
352 const MachineFunction &MF,
353 ModuleSlotTracker &MST) {
354 const MachineFrameInfo &MFI = MF.getFrameInfo();
355 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000356 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000357 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000358 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
359 if (MFI.isDeadObjectIndex(I))
360 continue;
361
362 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000363 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000364 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
365 ? yaml::FixedMachineStackObject::SpillSlot
366 : yaml::FixedMachineStackObject::DefaultType;
367 YamlObject.Offset = MFI.getObjectOffset(I);
368 YamlObject.Size = MFI.getObjectSize(I);
369 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000370 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzde491f02015-07-13 18:07:26 +0000371 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
372 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000373 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000374 StackObjectOperandMapping.insert(
375 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000376 }
377
378 // Process ordinary stack objects.
379 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000380 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
381 if (MFI.isDeadObjectIndex(I))
382 continue;
383
384 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000385 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000386 if (const auto *Alloca = MFI.getObjectAllocation(I))
387 YamlObject.Name.Value =
388 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000389 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
390 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000391 : MFI.isVariableSizedObjectIndex(I)
392 ? yaml::MachineStackObject::VariableSized
393 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000394 YamlObject.Offset = MFI.getObjectOffset(I);
395 YamlObject.Size = MFI.getObjectSize(I);
396 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000397 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000398
Matthias Braunef331ef2016-11-30 23:48:50 +0000399 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000400 StackObjectOperandMapping.insert(std::make_pair(
401 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000402 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000403
404 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
405 yaml::StringValue Reg;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000406 printRegMIR(CSInfo.getReg(), Reg, TRI);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000407 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
408 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
409 "Invalid stack object index");
410 const FrameIndexOperand &StackObject = StackObjectInfo->second;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000411 if (StackObject.IsFixed) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000412 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000413 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
414 CSInfo.isRestored();
415 } else {
Matthias Braunef331ef2016-11-30 23:48:50 +0000416 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000417 YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
418 CSInfo.isRestored();
419 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000420 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000421 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
422 auto LocalObject = MFI.getLocalFrameObjectMap(I);
423 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
424 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
425 "Invalid stack object index");
426 const FrameIndexOperand &StackObject = StackObjectInfo->second;
427 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000428 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000429 }
Alex Lorenza314d812015-08-18 22:26:26 +0000430
431 // Print the stack object references in the frame information class after
432 // converting the stack objects.
433 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000434 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000435 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
436 .printStackObjectReference(MFI.getStackProtectorIndex());
437 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000438
439 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000440 for (const MachineFunction::VariableDbgInfo &DebugVar :
441 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000442 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
443 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
444 "Invalid stack object index");
445 const FrameIndexOperand &StackObject = StackObjectInfo->second;
446 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000447 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000448 {
449 raw_string_ostream StrOS(Object.DebugVar.Value);
450 DebugVar.Var->printAsOperand(StrOS, MST);
451 }
452 {
453 raw_string_ostream StrOS(Object.DebugExpr.Value);
454 DebugVar.Expr->printAsOperand(StrOS, MST);
455 }
456 {
457 raw_string_ostream StrOS(Object.DebugLoc.Value);
458 DebugVar.Loc->printAsOperand(StrOS, MST);
459 }
460 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000461}
462
Alex Lorenzab980492015-07-20 20:51:18 +0000463void MIRPrinter::convert(yaml::MachineFunction &MF,
464 const MachineConstantPool &ConstantPool) {
465 unsigned ID = 0;
466 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
Alex Lorenzab980492015-07-20 20:51:18 +0000467 std::string Str;
468 raw_string_ostream StrOS(Str);
Diana Picusd5a00b02017-08-02 11:09:30 +0000469 if (Constant.isMachineConstantPoolEntry()) {
470 Constant.Val.MachineCPVal->print(StrOS);
471 } else {
472 Constant.Val.ConstVal->printAsOperand(StrOS);
473 }
474
475 yaml::MachineConstantPoolValue YamlConstant;
Alex Lorenzab980492015-07-20 20:51:18 +0000476 YamlConstant.ID = ID++;
477 YamlConstant.Value = StrOS.str();
478 YamlConstant.Alignment = Constant.getAlignment();
Diana Picusd5a00b02017-08-02 11:09:30 +0000479 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
480
Alex Lorenzab980492015-07-20 20:51:18 +0000481 MF.Constants.push_back(YamlConstant);
482 }
483}
484
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000485void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000486 yaml::MachineJumpTable &YamlJTI,
487 const MachineJumpTableInfo &JTI) {
488 YamlJTI.Kind = JTI.getEntryKind();
489 unsigned ID = 0;
490 for (const auto &Table : JTI.getJumpTables()) {
491 std::string Str;
492 yaml::MachineJumpTable::Entry Entry;
493 Entry.ID = ID++;
494 for (const auto *MBB : Table.MBBs) {
495 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000496 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
497 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000498 Entry.Blocks.push_back(StrOS.str());
499 Str.clear();
500 }
501 YamlJTI.Entries.push_back(Entry);
502 }
503}
504
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000505void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
506 const auto *TRI = MF.getSubtarget().getRegisterInfo();
507 unsigned I = 0;
508 for (const uint32_t *Mask : TRI->getRegMasks())
509 RegisterMaskIds.insert(std::make_pair(Mask, I++));
510}
511
Matthias Braun89401142017-05-05 21:09:30 +0000512void llvm::guessSuccessors(const MachineBasicBlock &MBB,
513 SmallVectorImpl<MachineBasicBlock*> &Result,
514 bool &IsFallthrough) {
515 SmallPtrSet<MachineBasicBlock*,8> Seen;
516
517 for (const MachineInstr &MI : MBB) {
518 if (MI.isPHI())
519 continue;
520 for (const MachineOperand &MO : MI.operands()) {
521 if (!MO.isMBB())
522 continue;
523 MachineBasicBlock *Succ = MO.getMBB();
524 auto RP = Seen.insert(Succ);
525 if (RP.second)
526 Result.push_back(Succ);
527 }
528 }
529 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
530 IsFallthrough = I == MBB.end() || !I->isBarrier();
531}
532
533bool
534MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
535 if (MBB.succ_size() <= 1)
536 return true;
537 if (!MBB.hasSuccessorProbabilities())
538 return true;
539
540 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
541 MBB.Probs.end());
542 BranchProbability::normalizeProbabilities(Normalized.begin(),
543 Normalized.end());
544 SmallVector<BranchProbability,8> Equal(Normalized.size());
545 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
546
547 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
548}
549
550bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
551 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
552 bool GuessedFallthrough;
553 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
554 if (GuessedFallthrough) {
555 const MachineFunction &MF = *MBB.getParent();
556 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
557 if (NextI != MF.end()) {
558 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
559 if (!is_contained(GuessedSuccs, Next))
560 GuessedSuccs.push_back(Next);
561 }
562 }
563 if (GuessedSuccs.size() != MBB.succ_size())
564 return false;
565 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
566}
567
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000568void MIPrinter::print(const MachineBasicBlock &MBB) {
569 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
570 OS << "bb." << MBB.getNumber();
571 bool HasAttributes = false;
572 if (const auto *BB = MBB.getBasicBlock()) {
573 if (BB->hasName()) {
574 OS << "." << BB->getName();
575 } else {
576 HasAttributes = true;
577 OS << " (";
578 int Slot = MST.getLocalSlot(BB);
579 if (Slot == -1)
580 OS << "<ir-block badref>";
581 else
582 OS << (Twine("%ir-block.") + Twine(Slot)).str();
583 }
584 }
585 if (MBB.hasAddressTaken()) {
586 OS << (HasAttributes ? ", " : " (");
587 OS << "address-taken";
588 HasAttributes = true;
589 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000590 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000591 OS << (HasAttributes ? ", " : " (");
592 OS << "landing-pad";
593 HasAttributes = true;
594 }
595 if (MBB.getAlignment()) {
596 OS << (HasAttributes ? ", " : " (");
597 OS << "align " << MBB.getAlignment();
598 HasAttributes = true;
599 }
600 if (HasAttributes)
601 OS << ")";
602 OS << ":\n";
603
604 bool HasLineAttributes = false;
605 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000606 bool canPredictProbs = canPredictBranchProbabilities(MBB);
Quentin Colombetd652aeb2017-09-19 23:34:12 +0000607 // Even if the list of successors is empty, if we cannot guess it,
608 // we need to print it to tell the parser that the list is empty.
609 // This is needed, because MI model unreachable as empty blocks
610 // with an empty successor list. If the parser would see that
611 // without the successor list, it would guess the code would
612 // fallthrough.
613 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
614 !canPredictSuccessors(MBB)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615 OS.indent(2) << "successors: ";
616 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
617 if (I != MBB.succ_begin())
618 OS << ", ";
619 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000620 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000621 OS << '('
622 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
623 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000624 }
625 OS << "\n";
626 HasLineAttributes = true;
627 }
628
629 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000630 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
631 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
632 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000633 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000634 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000635 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000636 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000637 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000638 First = false;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000639 OS << printReg(LI.PhysReg, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000640 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000641 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000642 }
643 OS << "\n";
644 HasLineAttributes = true;
645 }
646
647 if (HasLineAttributes)
648 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000649 bool IsInBundle = false;
650 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
651 const MachineInstr &MI = *I;
652 if (IsInBundle && !MI.isInsideBundle()) {
653 OS.indent(2) << "}\n";
654 IsInBundle = false;
655 }
656 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000657 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000658 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
659 OS << " {";
660 IsInBundle = true;
661 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000662 OS << "\n";
663 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000664 if (IsInBundle)
665 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000666}
667
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000668/// Return true when an instruction has tied register that can't be determined
669/// by the instruction's descriptor.
670static bool hasComplexRegisterTies(const MachineInstr &MI) {
671 const MCInstrDesc &MCID = MI.getDesc();
672 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
673 const auto &Operand = MI.getOperand(I);
674 if (!Operand.isReg() || Operand.isDef())
675 // Ignore the defined registers as MCID marks only the uses as tied.
676 continue;
677 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
678 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
679 if (ExpectedTiedIdx != TiedIdx)
680 return true;
681 }
682 return false;
683}
684
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000685static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
686 SmallBitVector &PrintedTypes,
687 const MachineRegisterInfo &MRI) {
688 const MachineOperand &Op = MI.getOperand(OpIdx);
689 if (!Op.isReg())
690 return LLT{};
691
692 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
693 return MRI.getType(Op.getReg());
694
695 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
696 if (!OpInfo.isGenericType())
697 return MRI.getType(Op.getReg());
698
699 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
700 return LLT{};
701
702 PrintedTypes.set(OpInfo.getGenericTypeIndex());
703 return MRI.getType(Op.getReg());
704}
705
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000706void MIPrinter::print(const MachineInstr &MI) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000707 const auto *MF = MI.getMF();
Quentin Colombet4e14a492016-03-07 21:57:52 +0000708 const auto &MRI = MF->getRegInfo();
709 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000710 const auto *TRI = SubTarget.getRegisterInfo();
711 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000712 const auto *TII = SubTarget.getInstrInfo();
713 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000714 if (MI.isCFIInstruction())
715 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000716
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000717 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000718 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000719 unsigned I = 0, E = MI.getNumOperands();
720 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
721 !MI.getOperand(I).isImplicit();
722 ++I) {
723 if (I)
724 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000725 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000726 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000727 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000728 }
729
730 if (I)
731 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000732 if (MI.getFlag(MachineInstr::FrameSetup))
733 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000734 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000735 if (I < E)
736 OS << ' ';
737
738 bool NeedComma = false;
739 for (; I < E; ++I) {
740 if (NeedComma)
741 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000742 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000743 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000744 NeedComma = true;
745 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000746
747 if (MI.getDebugLoc()) {
748 if (NeedComma)
749 OS << ',';
750 OS << " debug-location ";
751 MI.getDebugLoc()->printAsOperand(OS, MST);
752 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000753
754 if (!MI.memoperands_empty()) {
755 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000756 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000757 bool NeedComma = false;
758 for (const auto *Op : MI.memoperands()) {
759 if (NeedComma)
760 OS << ", ";
Geoff Berry6748abe2017-07-13 02:28:54 +0000761 print(Context, *TII, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000762 NeedComma = true;
763 }
764 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000765}
766
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000767void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
768 OS << "%bb." << MBB.getNumber();
769 if (const auto *BB = MBB.getBasicBlock()) {
770 if (BB->hasName())
771 OS << '.' << BB->getName();
772 }
773}
774
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000775static void printIRSlotNumber(raw_ostream &OS, int Slot) {
776 if (Slot == -1)
777 OS << "<badref>";
778 else
779 OS << Slot;
780}
781
Alex Lorenzdeb53492015-07-28 17:28:03 +0000782void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
783 OS << "%ir-block.";
784 if (BB.hasName()) {
785 printLLVMNameWithoutPrefix(OS, BB.getName());
786 return;
787 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000788 const Function *F = BB.getParent();
789 int Slot;
790 if (F == MST.getCurrentFunction()) {
791 Slot = MST.getLocalSlot(&BB);
792 } else {
793 ModuleSlotTracker CustomMST(F->getParent(),
794 /*ShouldInitializeAllMetadata=*/false);
795 CustomMST.incorporateFunction(*F);
796 Slot = CustomMST.getLocalSlot(&BB);
797 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000798 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000799}
800
Alex Lorenz4af7e612015-08-03 23:08:19 +0000801void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000802 if (isa<GlobalValue>(V)) {
803 V.printAsOperand(OS, /*PrintType=*/false, MST);
804 return;
805 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000806 if (isa<Constant>(V)) {
807 // Machine memory operands can load/store to/from constant value pointers.
808 OS << '`';
809 V.printAsOperand(OS, /*PrintType=*/true, MST);
810 OS << '`';
811 return;
812 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000813 OS << "%ir.";
814 if (V.hasName()) {
815 printLLVMNameWithoutPrefix(OS, V.getName());
816 return;
817 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000818 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000819}
820
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000821void MIPrinter::printStackObjectReference(int FrameIndex) {
822 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
823 assert(ObjectInfo != StackObjectOperandMapping.end() &&
824 "Invalid frame index");
825 const FrameIndexOperand &Operand = ObjectInfo->second;
826 if (Operand.IsFixed) {
827 OS << "%fixed-stack." << Operand.ID;
828 return;
829 }
830 OS << "%stack." << Operand.ID;
831 if (!Operand.Name.empty())
832 OS << '.' << Operand.Name;
833}
834
Alex Lorenz5672a892015-08-05 22:26:15 +0000835void MIPrinter::printOffset(int64_t Offset) {
836 if (Offset == 0)
837 return;
838 if (Offset < 0) {
839 OS << " - " << -Offset;
840 return;
841 }
842 OS << " + " << Offset;
843}
844
Alex Lorenz49873a82015-08-06 00:44:07 +0000845static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
846 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
847 for (const auto &I : Flags) {
848 if (I.first == TF) {
849 return I.second;
850 }
851 }
852 return nullptr;
853}
854
855void MIPrinter::printTargetFlags(const MachineOperand &Op) {
856 if (!Op.getTargetFlags())
857 return;
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000858 const auto *TII = Op.getParent()->getMF()->getSubtarget().getInstrInfo();
Alex Lorenz49873a82015-08-06 00:44:07 +0000859 assert(TII && "expected instruction info");
860 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
861 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000862 const bool HasDirectFlags = Flags.first;
863 const bool HasBitmaskFlags = Flags.second;
864 if (!HasDirectFlags && !HasBitmaskFlags) {
865 OS << "<unknown>) ";
866 return;
867 }
868 if (HasDirectFlags) {
869 if (const auto *Name = getTargetFlagName(TII, Flags.first))
870 OS << Name;
871 else
872 OS << "<unknown target flag>";
873 }
874 if (!HasBitmaskFlags) {
875 OS << ") ";
876 return;
877 }
878 bool IsCommaNeeded = HasDirectFlags;
879 unsigned BitMask = Flags.second;
880 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
881 for (const auto &Mask : BitMasks) {
882 // Check if the flag's bitmask has the bits of the current mask set.
883 if ((BitMask & Mask.first) == Mask.first) {
884 if (IsCommaNeeded)
885 OS << ", ";
886 IsCommaNeeded = true;
887 OS << Mask.second;
888 // Clear the bits which were serialized from the flag's bitmask.
889 BitMask &= ~(Mask.first);
890 }
891 }
892 if (BitMask) {
893 // When the resulting flag's bitmask isn't zero, we know that we didn't
894 // serialize all of the bit flags.
895 if (IsCommaNeeded)
896 OS << ", ";
897 OS << "<unknown bitmask target flag>";
898 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000899 OS << ") ";
900}
901
Alex Lorenzef5c1962015-07-28 23:02:45 +0000902static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
903 const auto *TII = MF.getSubtarget().getInstrInfo();
904 assert(TII && "expected instruction info");
905 auto Indices = TII->getSerializableTargetIndices();
906 for (const auto &I : Indices) {
907 if (I.first == Index) {
908 return I.second;
909 }
910 }
911 return nullptr;
912}
913
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000914void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
915 const TargetRegisterInfo *TRI,
916 bool ShouldPrintRegisterTies, LLT TypeToPrint,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000917 bool IsDef) {
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000918 const MachineOperand &Op = MI.getOperand(OpIdx);
Alex Lorenz49873a82015-08-06 00:44:07 +0000919 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000920 switch (Op.getType()) {
Justin Bogner6c452832017-10-24 18:04:54 +0000921 case MachineOperand::MO_Register: {
922 unsigned Reg = Op.getReg();
Alex Lorenzcb268d42015-07-06 23:07:26 +0000923 if (Op.isImplicit())
924 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000925 else if (!IsDef && Op.isDef())
926 // Print the 'def' flag only when the operand is defined after '='.
927 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000928 if (Op.isInternalRead())
929 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000930 if (Op.isDead())
931 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000932 if (Op.isKill())
933 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000934 if (Op.isUndef())
935 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000936 if (Op.isEarlyClobber())
937 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000938 if (Op.isDebug())
939 OS << "debug-use ";
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000940 OS << printReg(Reg, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000941 // Print the sub register.
942 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000943 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Justin Bogner6c452832017-10-24 18:04:54 +0000944 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
945 const MachineRegisterInfo &MRI = Op.getParent()->getMF()->getRegInfo();
946 if (IsDef || MRI.def_empty(Reg)) {
947 OS << ':';
948 printRegClassOrBank(Reg, OS, MRI, TRI);
949 }
950 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000951 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000952 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(OpIdx) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000953 if (TypeToPrint.isValid())
954 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000955 break;
Justin Bogner6c452832017-10-24 18:04:54 +0000956 }
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000957 case MachineOperand::MO_Immediate:
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000958 if (MI.isOperandSubregIdx(OpIdx))
959 OS << "%subreg." << TRI->getSubRegIndexName(Op.getImm());
960 else
961 OS << Op.getImm();
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000962 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000963 case MachineOperand::MO_CImmediate:
964 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
965 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000966 case MachineOperand::MO_FPImmediate:
967 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
968 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000969 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000970 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000971 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000972 case MachineOperand::MO_FrameIndex:
973 printStackObjectReference(Op.getIndex());
974 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000975 case MachineOperand::MO_ConstantPoolIndex:
976 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000977 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000978 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000979 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000980 OS << "target-index(";
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000981 if (const auto *Name =
982 getTargetIndexName(*Op.getParent()->getMF(), Op.getIndex()))
Alex Lorenzef5c1962015-07-28 23:02:45 +0000983 OS << Name;
984 else
985 OS << "<unknown>";
986 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000987 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000988 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000989 case MachineOperand::MO_JumpTableIndex:
990 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000991 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000992 case MachineOperand::MO_ExternalSymbol: {
993 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000994 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000995 if (Name.empty()) {
996 OS << "\"\"";
997 } else {
998 printLLVMNameWithoutPrefix(OS, Name);
999 }
Alex Lorenz5672a892015-08-05 22:26:15 +00001000 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001001 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +00001002 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001003 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +00001004 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +00001005 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001006 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001007 case MachineOperand::MO_BlockAddress:
1008 OS << "blockaddress(";
1009 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
1010 MST);
1011 OS << ", ";
1012 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
1013 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +00001014 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +00001015 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001016 case MachineOperand::MO_RegisterMask: {
1017 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
1018 if (RegMaskInfo != RegisterMaskIds.end())
1019 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
1020 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001021 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001022 break;
1023 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001024 case MachineOperand::MO_RegisterLiveOut: {
1025 const uint32_t *RegMask = Op.getRegLiveOut();
1026 OS << "liveout(";
1027 bool IsCommaNeeded = false;
1028 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
1029 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
1030 if (IsCommaNeeded)
1031 OS << ", ";
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001032 OS << printReg(Reg, TRI);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001033 IsCommaNeeded = true;
1034 }
1035 }
1036 OS << ")";
1037 break;
1038 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001039 case MachineOperand::MO_Metadata:
1040 Op.getMetadata()->printAsOperand(OS, MST);
1041 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001042 case MachineOperand::MO_MCSymbol:
1043 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1044 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001045 case MachineOperand::MO_CFIIndex: {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001046 const MachineFunction &MF = *Op.getParent()->getMF();
Matthias Braunf23ef432016-11-30 23:48:42 +00001047 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001048 break;
1049 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001050 case MachineOperand::MO_IntrinsicID: {
1051 Intrinsic::ID ID = Op.getIntrinsicID();
1052 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001053 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001054 else {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001055 const MachineFunction &MF = *Op.getParent()->getMF();
Tim Northover6b3bd612016-07-29 20:32:59 +00001056 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1057 OS << "intrinsic(@" << TII->getName(ID) << ')';
1058 }
1059 break;
1060 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001061 case MachineOperand::MO_Predicate: {
1062 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1063 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1064 << CmpInst::getPredicateName(Pred) << ')';
1065 break;
1066 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001067 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001068}
1069
Geoff Berry6748abe2017-07-13 02:28:54 +00001070static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
1071 unsigned TMMOFlag) {
1072 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
1073 for (const auto &I : Flags) {
1074 if (I.first == TMMOFlag) {
1075 return I.second;
1076 }
1077 }
1078 return nullptr;
1079}
1080
1081void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII,
1082 const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001083 OS << '(';
Alex Lorenza518b792015-08-04 00:24:45 +00001084 if (Op.isVolatile())
1085 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001086 if (Op.isNonTemporal())
1087 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001088 if (Op.isDereferenceable())
1089 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001090 if (Op.isInvariant())
1091 OS << "invariant ";
Geoff Berry6748abe2017-07-13 02:28:54 +00001092 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1)
1093 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1)
1094 << "\" ";
1095 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2)
1096 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2)
1097 << "\" ";
1098 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3)
1099 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3)
1100 << "\" ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001101
1102 assert((Op.isLoad() || Op.isStore()) && "machine memory operand must be a load or store (or both)");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001103 if (Op.isLoad())
1104 OS << "load ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001105 if (Op.isStore())
Alex Lorenz4af7e612015-08-03 23:08:19 +00001106 OS << "store ";
Tim Northoverb73e3092017-02-13 22:14:08 +00001107
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001108 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001109
1110 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1111 OS << toIRString(Op.getOrdering()) << ' ';
1112 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1113 OS << toIRString(Op.getFailureOrdering()) << ' ';
1114
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001115 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001116 if (const Value *Val = Op.getValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001117 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1118 : Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001119 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001120 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001121 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1122 : Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001123 assert(PVal && "Expected a pseudo source value");
1124 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001125 case PseudoSourceValue::Stack:
1126 OS << "stack";
1127 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001128 case PseudoSourceValue::GOT:
1129 OS << "got";
1130 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001131 case PseudoSourceValue::JumpTable:
1132 OS << "jump-table";
1133 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001134 case PseudoSourceValue::ConstantPool:
1135 OS << "constant-pool";
1136 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001137 case PseudoSourceValue::FixedStack:
1138 printStackObjectReference(
1139 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1140 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001141 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001142 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001143 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1144 OS, /*PrintType=*/false, MST);
1145 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001146 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001147 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001148 printLLVMNameWithoutPrefix(
1149 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001150 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001151 case PseudoSourceValue::TargetCustom:
1152 llvm_unreachable("TargetCustom pseudo source values are not supported");
1153 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001154 }
1155 }
Alex Lorenz83127732015-08-07 20:26:52 +00001156 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001157 if (Op.getBaseAlignment() != Op.getSize())
1158 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001159 auto AAInfo = Op.getAAInfo();
1160 if (AAInfo.TBAA) {
1161 OS << ", !tbaa ";
1162 AAInfo.TBAA->printAsOperand(OS, MST);
1163 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001164 if (AAInfo.Scope) {
1165 OS << ", !alias.scope ";
1166 AAInfo.Scope->printAsOperand(OS, MST);
1167 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001168 if (AAInfo.NoAlias) {
1169 OS << ", !noalias ";
1170 AAInfo.NoAlias->printAsOperand(OS, MST);
1171 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001172 if (Op.getRanges()) {
1173 OS << ", !range ";
1174 Op.getRanges()->printAsOperand(OS, MST);
1175 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001176 OS << ')';
1177}
1178
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001179void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1180 switch (SSID) {
1181 case SyncScope::System: {
1182 break;
1183 }
1184 default: {
1185 if (SSNs.empty())
1186 Context.getSyncScopeNames(SSNs);
1187
1188 OS << "syncscope(\"";
1189 PrintEscapedString(SSNs[SSID], OS);
1190 OS << "\") ";
1191 break;
1192 }
1193 }
1194}
1195
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001196static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1197 const TargetRegisterInfo *TRI) {
1198 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1199 if (Reg == -1) {
1200 OS << "<badreg>";
1201 return;
1202 }
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001203 OS << printReg(Reg, TRI);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001204}
1205
1206void MIPrinter::print(const MCCFIInstruction &CFI,
1207 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001208 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001209 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001210 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001211 if (CFI.getLabel())
1212 OS << "<mcsymbol> ";
1213 printCFIRegister(CFI.getRegister(), OS, TRI);
1214 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001215 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001216 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001217 if (CFI.getLabel())
1218 OS << "<mcsymbol> ";
1219 printCFIRegister(CFI.getRegister(), OS, TRI);
1220 OS << ", " << CFI.getOffset();
1221 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001222 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001223 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001224 if (CFI.getLabel())
1225 OS << "<mcsymbol> ";
1226 printCFIRegister(CFI.getRegister(), OS, TRI);
1227 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001228 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001229 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001230 if (CFI.getLabel())
1231 OS << "<mcsymbol> ";
1232 OS << CFI.getOffset();
1233 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001234 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001235 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001236 if (CFI.getLabel())
1237 OS << "<mcsymbol> ";
1238 printCFIRegister(CFI.getRegister(), OS, TRI);
1239 OS << ", " << CFI.getOffset();
1240 break;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001241 case MCCFIInstruction::OpRestore:
1242 OS << "restore ";
1243 if (CFI.getLabel())
1244 OS << "<mcsymbol> ";
1245 printCFIRegister(CFI.getRegister(), OS, TRI);
1246 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001247 default:
1248 // TODO: Print the other CFI Operations.
1249 OS << "<unserializable cfi operation>";
1250 break;
1251 }
1252}
1253
Alex Lorenz345c1442015-06-15 23:52:35 +00001254void llvm::printMIR(raw_ostream &OS, const Module &M) {
1255 yaml::Output Out(OS);
1256 Out << const_cast<Module &>(M);
1257}
1258
1259void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1260 MIRPrinter Printer(OS);
1261 Printer.print(MF);
1262}