blob: ae1bb36c6aac7089b831c8d6a3324ed292649dd3 [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"
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000021#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000022#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000024#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenzab980492015-07-20 20:51:18 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000028#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineJumpTableInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000032#include "llvm/CodeGen/MachineOperand.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000034#include "llvm/CodeGen/MIRPrinter.h"
35#include "llvm/CodeGen/MIRYamlMapping.h"
36#include "llvm/CodeGen/PseudoSourceValue.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000037#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000038#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000039#include "llvm/IR/DebugInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000040#include "llvm/IR/DebugLoc.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalValue.h"
43#include "llvm/IR/InstrTypes.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000044#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000045#include "llvm/IR/Intrinsics.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000046#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000047#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000048#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000049#include "llvm/IR/Value.h"
50#include "llvm/MC/LaneBitmask.h"
51#include "llvm/MC/MCDwarf.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000052#include "llvm/MC/MCSymbol.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000053#include "llvm/Support/AtomicOrdering.h"
54#include "llvm/Support/BranchProbability.h"
55#include "llvm/Support/Casting.h"
56#include "llvm/Support/CommandLine.h"
57#include "llvm/Support/ErrorHandling.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000058#include "llvm/Support/Format.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000059#include "llvm/Support/LowLevelTypeImpl.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000060#include "llvm/Support/raw_ostream.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000061#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000062#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000063#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000064#include "llvm/Target/TargetMachine.h"
65#include "llvm/Target/TargetRegisterInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066#include "llvm/Target/TargetSubtargetInfo.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
Matthias Braun89401142017-05-05 21:09:30 +000078static cl::opt<bool> SimplifyMIR("simplify-mir",
79 cl::desc("Leave out unnecessary information when printing MIR"));
80
Alex Lorenz345c1442015-06-15 23:52:35 +000081namespace {
82
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000083/// This structure describes how to print out stack object references.
84struct FrameIndexOperand {
85 std::string Name;
86 unsigned ID;
87 bool IsFixed;
88
89 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
90 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
91
92 /// Return an ordinary stack object reference.
93 static FrameIndexOperand create(StringRef Name, unsigned ID) {
94 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
95 }
96
97 /// Return a fixed stack object reference.
98 static FrameIndexOperand createFixed(unsigned ID) {
99 return FrameIndexOperand("", ID, /*IsFixed=*/true);
100 }
101};
102
Alex Lorenz618b2832015-07-30 16:54:38 +0000103} // end anonymous namespace
104
105namespace llvm {
106
Alex Lorenz345c1442015-06-15 23:52:35 +0000107/// This class prints out the machine functions using the MIR serialization
108/// format.
109class MIRPrinter {
110 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000111 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000112 /// Maps from stack object indices to operand indices which will be used when
113 /// printing frame index machine operands.
114 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +0000115
116public:
117 MIRPrinter(raw_ostream &OS) : OS(OS) {}
118
119 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000120
Alex Lorenz28148ba2015-07-09 22:23:13 +0000121 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
122 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000123 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
124 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +0000125 void convert(yaml::MachineFunction &MF,
126 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000127 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
128 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +0000129 void convertStackObjects(yaml::MachineFunction &YMF,
130 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000131
132private:
133 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000134};
135
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000136/// This class prints out the machine instructions using the MIR serialization
137/// format.
138class MIPrinter {
139 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000140 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000141 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000143 /// Synchronization scope names registered with LLVMContext.
144 SmallVector<StringRef, 8> SSNs;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000145
Matthias Braun89401142017-05-05 21:09:30 +0000146 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
147 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
148
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000149public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000150 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000151 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
152 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
153 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
154 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000155
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000156 void print(const MachineBasicBlock &MBB);
157
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000158 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000159 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000160 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000161 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000162 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000163 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000164 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000165 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000166 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000167 LLT TypeToPrint, bool IsDef = false);
Geoff Berry6748abe2017-07-13 02:28:54 +0000168 void print(const LLVMContext &Context, const TargetInstrInfo &TII,
169 const MachineMemOperand &Op);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000170 void printSyncScope(const LLVMContext &Context, SyncScope::ID SSID);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000171
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000172 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000173};
174
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000175} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000176
177namespace llvm {
178namespace yaml {
179
180/// This struct serializes the LLVM IR module.
181template <> struct BlockScalarTraits<Module> {
182 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
183 Mod.print(OS, nullptr);
184 }
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000185
Alex Lorenz345c1442015-06-15 23:52:35 +0000186 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
187 llvm_unreachable("LLVM Module is supposed to be parsed separately");
188 return "";
189 }
190};
191
192} // end namespace yaml
193} // end namespace llvm
194
Alex Lorenz15a00a82015-07-14 21:18:25 +0000195static void printReg(unsigned Reg, raw_ostream &OS,
196 const TargetRegisterInfo *TRI) {
197 // TODO: Print Stack Slots.
198 if (!Reg)
199 OS << '_';
200 else if (TargetRegisterInfo::isVirtualRegister(Reg))
201 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
202 else if (Reg < TRI->getNumRegs())
203 OS << '%' << StringRef(TRI->getName(Reg)).lower();
204 else
205 llvm_unreachable("Can't print this kind of register yet");
206}
207
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000208static void printReg(unsigned Reg, yaml::StringValue &Dest,
209 const TargetRegisterInfo *TRI) {
210 raw_string_ostream OS(Dest.Value);
211 printReg(Reg, OS, TRI);
212}
213
Alex Lorenz345c1442015-06-15 23:52:35 +0000214void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000215 initRegisterMaskIds(MF);
216
Alex Lorenz345c1442015-06-15 23:52:35 +0000217 yaml::MachineFunction YamlMF;
218 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000219 YamlMF.Alignment = MF.getAlignment();
220 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000221
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000222 YamlMF.Legalized = MF.getProperties().hasProperty(
223 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000224 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
225 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000226 YamlMF.Selected = MF.getProperties().hasProperty(
227 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000228
Alex Lorenz28148ba2015-07-09 22:23:13 +0000229 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000230 ModuleSlotTracker MST(MF.getFunction()->getParent());
231 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000232 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000233 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000234 if (const auto *ConstantPool = MF.getConstantPool())
235 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000236 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
237 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000238 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
239 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000240 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000241 if (IsNewlineNeeded)
242 StrOS << "\n";
243 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
244 .print(MBB);
245 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000246 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000247 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000248 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000249 if (!SimplifyMIR)
250 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000251 Out << YamlMF;
252}
253
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000254static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
255 const TargetRegisterInfo *TRI) {
256 assert(RegMask && "Can't print an empty register mask");
257 OS << StringRef("CustomRegMask(");
258
259 bool IsRegInRegMaskFound = false;
260 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
261 // Check whether the register is asserted in regmask.
262 if (RegMask[I / 32] & (1u << (I % 32))) {
263 if (IsRegInRegMaskFound)
264 OS << ',';
265 printReg(I, OS, TRI);
266 IsRegInRegMaskFound = true;
267 }
268 }
269
270 OS << ')';
271}
272
Alex Lorenz54565cf2015-06-24 19:56:10 +0000273void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000274 const MachineRegisterInfo &RegInfo,
275 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000276 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000277
278 // Print the virtual register definitions.
279 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
280 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
281 yaml::VirtualRegisterDefinition VReg;
282 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000283 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000284 VReg.Class =
285 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000286 else if (RegInfo.getRegBankOrNull(Reg))
287 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000288 else {
289 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000290 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000291 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000292 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000293 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
294 if (PreferredReg)
295 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000296 MF.VirtualRegisters.push_back(VReg);
297 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000298
299 // Print the live ins.
300 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
301 yaml::MachineFunctionLiveIn LiveIn;
302 printReg(I->first, LiveIn.Register, TRI);
303 if (I->second)
304 printReg(I->second, LiveIn.VirtualRegister, TRI);
305 MF.LiveIns.push_back(LiveIn);
306 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000307
308 // Prints the callee saved registers.
309 if (RegInfo.isUpdatedCSRsInitialized()) {
310 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
311 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
312 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000313 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000314 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000315 CalleeSavedRegisters.push_back(Reg);
316 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000317 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000318 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000319}
320
Alex Lorenza6f9a372015-07-29 21:09:09 +0000321void MIRPrinter::convert(ModuleSlotTracker &MST,
322 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000323 const MachineFrameInfo &MFI) {
324 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
325 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
326 YamlMFI.HasStackMap = MFI.hasStackMap();
327 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
328 YamlMFI.StackSize = MFI.getStackSize();
329 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
330 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
331 YamlMFI.AdjustsStack = MFI.adjustsStack();
332 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000333 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
334 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000335 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
336 YamlMFI.HasVAStart = MFI.hasVAStart();
337 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000338 if (MFI.getSavePoint()) {
339 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
340 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
341 .printMBBReference(*MFI.getSavePoint());
342 }
343 if (MFI.getRestorePoint()) {
344 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
345 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
346 .printMBBReference(*MFI.getRestorePoint());
347 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000348}
349
Matthias Braunef331ef2016-11-30 23:48:50 +0000350void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
351 const MachineFunction &MF,
352 ModuleSlotTracker &MST) {
353 const MachineFrameInfo &MFI = MF.getFrameInfo();
354 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000355 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000356 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000357 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
358 if (MFI.isDeadObjectIndex(I))
359 continue;
360
361 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000362 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000363 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
364 ? yaml::FixedMachineStackObject::SpillSlot
365 : yaml::FixedMachineStackObject::DefaultType;
366 YamlObject.Offset = MFI.getObjectOffset(I);
367 YamlObject.Size = MFI.getObjectSize(I);
368 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000369 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzde491f02015-07-13 18:07:26 +0000370 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
371 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000372 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000373 StackObjectOperandMapping.insert(
374 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000375 }
376
377 // Process ordinary stack objects.
378 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000379 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
380 if (MFI.isDeadObjectIndex(I))
381 continue;
382
383 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000384 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000385 if (const auto *Alloca = MFI.getObjectAllocation(I))
386 YamlObject.Name.Value =
387 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000388 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
389 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000390 : MFI.isVariableSizedObjectIndex(I)
391 ? yaml::MachineStackObject::VariableSized
392 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000393 YamlObject.Offset = MFI.getObjectOffset(I);
394 YamlObject.Size = MFI.getObjectSize(I);
395 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000396 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000397
Matthias Braunef331ef2016-11-30 23:48:50 +0000398 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000399 StackObjectOperandMapping.insert(std::make_pair(
400 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000401 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000402
403 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
404 yaml::StringValue Reg;
405 printReg(CSInfo.getReg(), Reg, TRI);
406 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
407 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
408 "Invalid stack object index");
409 const FrameIndexOperand &StackObject = StackObjectInfo->second;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000410 if (StackObject.IsFixed) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000411 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000412 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
413 CSInfo.isRestored();
414 } else {
Matthias Braunef331ef2016-11-30 23:48:50 +0000415 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000416 YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
417 CSInfo.isRestored();
418 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000419 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000420 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
421 auto LocalObject = MFI.getLocalFrameObjectMap(I);
422 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
423 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
424 "Invalid stack object index");
425 const FrameIndexOperand &StackObject = StackObjectInfo->second;
426 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000427 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000428 }
Alex Lorenza314d812015-08-18 22:26:26 +0000429
430 // Print the stack object references in the frame information class after
431 // converting the stack objects.
432 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000433 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000434 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
435 .printStackObjectReference(MFI.getStackProtectorIndex());
436 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000437
438 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000439 for (const MachineFunction::VariableDbgInfo &DebugVar :
440 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000441 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
442 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
443 "Invalid stack object index");
444 const FrameIndexOperand &StackObject = StackObjectInfo->second;
445 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000446 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000447 {
448 raw_string_ostream StrOS(Object.DebugVar.Value);
449 DebugVar.Var->printAsOperand(StrOS, MST);
450 }
451 {
452 raw_string_ostream StrOS(Object.DebugExpr.Value);
453 DebugVar.Expr->printAsOperand(StrOS, MST);
454 }
455 {
456 raw_string_ostream StrOS(Object.DebugLoc.Value);
457 DebugVar.Loc->printAsOperand(StrOS, MST);
458 }
459 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000460}
461
Alex Lorenzab980492015-07-20 20:51:18 +0000462void MIRPrinter::convert(yaml::MachineFunction &MF,
463 const MachineConstantPool &ConstantPool) {
464 unsigned ID = 0;
465 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
Alex Lorenzab980492015-07-20 20:51:18 +0000466 std::string Str;
467 raw_string_ostream StrOS(Str);
Diana Picusd5a00b02017-08-02 11:09:30 +0000468 if (Constant.isMachineConstantPoolEntry()) {
469 Constant.Val.MachineCPVal->print(StrOS);
470 } else {
471 Constant.Val.ConstVal->printAsOperand(StrOS);
472 }
473
474 yaml::MachineConstantPoolValue YamlConstant;
Alex Lorenzab980492015-07-20 20:51:18 +0000475 YamlConstant.ID = ID++;
476 YamlConstant.Value = StrOS.str();
477 YamlConstant.Alignment = Constant.getAlignment();
Diana Picusd5a00b02017-08-02 11:09:30 +0000478 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
479
Alex Lorenzab980492015-07-20 20:51:18 +0000480 MF.Constants.push_back(YamlConstant);
481 }
482}
483
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000484void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000485 yaml::MachineJumpTable &YamlJTI,
486 const MachineJumpTableInfo &JTI) {
487 YamlJTI.Kind = JTI.getEntryKind();
488 unsigned ID = 0;
489 for (const auto &Table : JTI.getJumpTables()) {
490 std::string Str;
491 yaml::MachineJumpTable::Entry Entry;
492 Entry.ID = ID++;
493 for (const auto *MBB : Table.MBBs) {
494 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000495 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
496 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000497 Entry.Blocks.push_back(StrOS.str());
498 Str.clear();
499 }
500 YamlJTI.Entries.push_back(Entry);
501 }
502}
503
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000504void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
505 const auto *TRI = MF.getSubtarget().getRegisterInfo();
506 unsigned I = 0;
507 for (const uint32_t *Mask : TRI->getRegMasks())
508 RegisterMaskIds.insert(std::make_pair(Mask, I++));
509}
510
Matthias Braun89401142017-05-05 21:09:30 +0000511void llvm::guessSuccessors(const MachineBasicBlock &MBB,
512 SmallVectorImpl<MachineBasicBlock*> &Result,
513 bool &IsFallthrough) {
514 SmallPtrSet<MachineBasicBlock*,8> Seen;
515
516 for (const MachineInstr &MI : MBB) {
517 if (MI.isPHI())
518 continue;
519 for (const MachineOperand &MO : MI.operands()) {
520 if (!MO.isMBB())
521 continue;
522 MachineBasicBlock *Succ = MO.getMBB();
523 auto RP = Seen.insert(Succ);
524 if (RP.second)
525 Result.push_back(Succ);
526 }
527 }
528 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
529 IsFallthrough = I == MBB.end() || !I->isBarrier();
530}
531
532bool
533MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
534 if (MBB.succ_size() <= 1)
535 return true;
536 if (!MBB.hasSuccessorProbabilities())
537 return true;
538
539 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
540 MBB.Probs.end());
541 BranchProbability::normalizeProbabilities(Normalized.begin(),
542 Normalized.end());
543 SmallVector<BranchProbability,8> Equal(Normalized.size());
544 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
545
546 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
547}
548
549bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
550 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
551 bool GuessedFallthrough;
552 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
553 if (GuessedFallthrough) {
554 const MachineFunction &MF = *MBB.getParent();
555 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
556 if (NextI != MF.end()) {
557 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
558 if (!is_contained(GuessedSuccs, Next))
559 GuessedSuccs.push_back(Next);
560 }
561 }
562 if (GuessedSuccs.size() != MBB.succ_size())
563 return false;
564 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
565}
566
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000567void MIPrinter::print(const MachineBasicBlock &MBB) {
568 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
569 OS << "bb." << MBB.getNumber();
570 bool HasAttributes = false;
571 if (const auto *BB = MBB.getBasicBlock()) {
572 if (BB->hasName()) {
573 OS << "." << BB->getName();
574 } else {
575 HasAttributes = true;
576 OS << " (";
577 int Slot = MST.getLocalSlot(BB);
578 if (Slot == -1)
579 OS << "<ir-block badref>";
580 else
581 OS << (Twine("%ir-block.") + Twine(Slot)).str();
582 }
583 }
584 if (MBB.hasAddressTaken()) {
585 OS << (HasAttributes ? ", " : " (");
586 OS << "address-taken";
587 HasAttributes = true;
588 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000589 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000590 OS << (HasAttributes ? ", " : " (");
591 OS << "landing-pad";
592 HasAttributes = true;
593 }
594 if (MBB.getAlignment()) {
595 OS << (HasAttributes ? ", " : " (");
596 OS << "align " << MBB.getAlignment();
597 HasAttributes = true;
598 }
599 if (HasAttributes)
600 OS << ")";
601 OS << ":\n";
602
603 bool HasLineAttributes = false;
604 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000605 bool canPredictProbs = canPredictBranchProbabilities(MBB);
Quentin Colombetd652aeb2017-09-19 23:34:12 +0000606 // Even if the list of successors is empty, if we cannot guess it,
607 // we need to print it to tell the parser that the list is empty.
608 // This is needed, because MI model unreachable as empty blocks
609 // with an empty successor list. If the parser would see that
610 // without the successor list, it would guess the code would
611 // fallthrough.
612 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
613 !canPredictSuccessors(MBB)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000614 OS.indent(2) << "successors: ";
615 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
616 if (I != MBB.succ_begin())
617 OS << ", ";
618 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000619 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000620 OS << '('
621 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
622 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000623 }
624 OS << "\n";
625 HasLineAttributes = true;
626 }
627
628 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000629 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
630 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
631 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000632 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000633 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000634 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000635 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000636 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000637 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000638 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000639 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000640 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000641 }
642 OS << "\n";
643 HasLineAttributes = true;
644 }
645
646 if (HasLineAttributes)
647 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000648 bool IsInBundle = false;
649 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
650 const MachineInstr &MI = *I;
651 if (IsInBundle && !MI.isInsideBundle()) {
652 OS.indent(2) << "}\n";
653 IsInBundle = false;
654 }
655 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000656 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000657 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
658 OS << " {";
659 IsInBundle = true;
660 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000661 OS << "\n";
662 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000663 if (IsInBundle)
664 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000665}
666
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000667/// Return true when an instruction has tied register that can't be determined
668/// by the instruction's descriptor.
669static bool hasComplexRegisterTies(const MachineInstr &MI) {
670 const MCInstrDesc &MCID = MI.getDesc();
671 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
672 const auto &Operand = MI.getOperand(I);
673 if (!Operand.isReg() || Operand.isDef())
674 // Ignore the defined registers as MCID marks only the uses as tied.
675 continue;
676 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
677 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
678 if (ExpectedTiedIdx != TiedIdx)
679 return true;
680 }
681 return false;
682}
683
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000684static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
685 SmallBitVector &PrintedTypes,
686 const MachineRegisterInfo &MRI) {
687 const MachineOperand &Op = MI.getOperand(OpIdx);
688 if (!Op.isReg())
689 return LLT{};
690
691 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
692 return MRI.getType(Op.getReg());
693
694 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
695 if (!OpInfo.isGenericType())
696 return MRI.getType(Op.getReg());
697
698 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
699 return LLT{};
700
701 PrintedTypes.set(OpInfo.getGenericTypeIndex());
702 return MRI.getType(Op.getReg());
703}
704
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000705void MIPrinter::print(const MachineInstr &MI) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000706 const auto *MF = MI.getMF();
Quentin Colombet4e14a492016-03-07 21:57:52 +0000707 const auto &MRI = MF->getRegInfo();
708 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000709 const auto *TRI = SubTarget.getRegisterInfo();
710 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000711 const auto *TII = SubTarget.getInstrInfo();
712 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000713 if (MI.isCFIInstruction())
714 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000715
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000716 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000717 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000718 unsigned I = 0, E = MI.getNumOperands();
719 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
720 !MI.getOperand(I).isImplicit();
721 ++I) {
722 if (I)
723 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000724 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
725 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000726 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000727 }
728
729 if (I)
730 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000731 if (MI.getFlag(MachineInstr::FrameSetup))
732 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000733 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000734 if (I < E)
735 OS << ' ';
736
737 bool NeedComma = false;
738 for (; I < E; ++I) {
739 if (NeedComma)
740 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000741 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
742 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000743 NeedComma = true;
744 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000745
746 if (MI.getDebugLoc()) {
747 if (NeedComma)
748 OS << ',';
749 OS << " debug-location ";
750 MI.getDebugLoc()->printAsOperand(OS, MST);
751 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000752
753 if (!MI.memoperands_empty()) {
754 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000755 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000756 bool NeedComma = false;
757 for (const auto *Op : MI.memoperands()) {
758 if (NeedComma)
759 OS << ", ";
Geoff Berry6748abe2017-07-13 02:28:54 +0000760 print(Context, *TII, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000761 NeedComma = true;
762 }
763 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000764}
765
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000766void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
767 OS << "%bb." << MBB.getNumber();
768 if (const auto *BB = MBB.getBasicBlock()) {
769 if (BB->hasName())
770 OS << '.' << BB->getName();
771 }
772}
773
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000774static void printIRSlotNumber(raw_ostream &OS, int Slot) {
775 if (Slot == -1)
776 OS << "<badref>";
777 else
778 OS << Slot;
779}
780
Alex Lorenzdeb53492015-07-28 17:28:03 +0000781void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
782 OS << "%ir-block.";
783 if (BB.hasName()) {
784 printLLVMNameWithoutPrefix(OS, BB.getName());
785 return;
786 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000787 const Function *F = BB.getParent();
788 int Slot;
789 if (F == MST.getCurrentFunction()) {
790 Slot = MST.getLocalSlot(&BB);
791 } else {
792 ModuleSlotTracker CustomMST(F->getParent(),
793 /*ShouldInitializeAllMetadata=*/false);
794 CustomMST.incorporateFunction(*F);
795 Slot = CustomMST.getLocalSlot(&BB);
796 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000797 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000798}
799
Alex Lorenz4af7e612015-08-03 23:08:19 +0000800void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000801 if (isa<GlobalValue>(V)) {
802 V.printAsOperand(OS, /*PrintType=*/false, MST);
803 return;
804 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000805 if (isa<Constant>(V)) {
806 // Machine memory operands can load/store to/from constant value pointers.
807 OS << '`';
808 V.printAsOperand(OS, /*PrintType=*/true, MST);
809 OS << '`';
810 return;
811 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000812 OS << "%ir.";
813 if (V.hasName()) {
814 printLLVMNameWithoutPrefix(OS, V.getName());
815 return;
816 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000817 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000818}
819
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000820void MIPrinter::printStackObjectReference(int FrameIndex) {
821 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
822 assert(ObjectInfo != StackObjectOperandMapping.end() &&
823 "Invalid frame index");
824 const FrameIndexOperand &Operand = ObjectInfo->second;
825 if (Operand.IsFixed) {
826 OS << "%fixed-stack." << Operand.ID;
827 return;
828 }
829 OS << "%stack." << Operand.ID;
830 if (!Operand.Name.empty())
831 OS << '.' << Operand.Name;
832}
833
Alex Lorenz5672a892015-08-05 22:26:15 +0000834void MIPrinter::printOffset(int64_t Offset) {
835 if (Offset == 0)
836 return;
837 if (Offset < 0) {
838 OS << " - " << -Offset;
839 return;
840 }
841 OS << " + " << Offset;
842}
843
Alex Lorenz49873a82015-08-06 00:44:07 +0000844static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
845 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
846 for (const auto &I : Flags) {
847 if (I.first == TF) {
848 return I.second;
849 }
850 }
851 return nullptr;
852}
853
854void MIPrinter::printTargetFlags(const MachineOperand &Op) {
855 if (!Op.getTargetFlags())
856 return;
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000857 const auto *TII = Op.getParent()->getMF()->getSubtarget().getInstrInfo();
Alex Lorenz49873a82015-08-06 00:44:07 +0000858 assert(TII && "expected instruction info");
859 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
860 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000861 const bool HasDirectFlags = Flags.first;
862 const bool HasBitmaskFlags = Flags.second;
863 if (!HasDirectFlags && !HasBitmaskFlags) {
864 OS << "<unknown>) ";
865 return;
866 }
867 if (HasDirectFlags) {
868 if (const auto *Name = getTargetFlagName(TII, Flags.first))
869 OS << Name;
870 else
871 OS << "<unknown target flag>";
872 }
873 if (!HasBitmaskFlags) {
874 OS << ") ";
875 return;
876 }
877 bool IsCommaNeeded = HasDirectFlags;
878 unsigned BitMask = Flags.second;
879 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
880 for (const auto &Mask : BitMasks) {
881 // Check if the flag's bitmask has the bits of the current mask set.
882 if ((BitMask & Mask.first) == Mask.first) {
883 if (IsCommaNeeded)
884 OS << ", ";
885 IsCommaNeeded = true;
886 OS << Mask.second;
887 // Clear the bits which were serialized from the flag's bitmask.
888 BitMask &= ~(Mask.first);
889 }
890 }
891 if (BitMask) {
892 // When the resulting flag's bitmask isn't zero, we know that we didn't
893 // serialize all of the bit flags.
894 if (IsCommaNeeded)
895 OS << ", ";
896 OS << "<unknown bitmask target flag>";
897 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000898 OS << ") ";
899}
900
Alex Lorenzef5c1962015-07-28 23:02:45 +0000901static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
902 const auto *TII = MF.getSubtarget().getInstrInfo();
903 assert(TII && "expected instruction info");
904 auto Indices = TII->getSerializableTargetIndices();
905 for (const auto &I : Indices) {
906 if (I.first == Index) {
907 return I.second;
908 }
909 }
910 return nullptr;
911}
912
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000913void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000914 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
915 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000916 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000917 switch (Op.getType()) {
918 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000919 if (Op.isImplicit())
920 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000921 else if (!IsDef && Op.isDef())
922 // Print the 'def' flag only when the operand is defined after '='.
923 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000924 if (Op.isInternalRead())
925 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000926 if (Op.isDead())
927 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000928 if (Op.isKill())
929 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000930 if (Op.isUndef())
931 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000932 if (Op.isEarlyClobber())
933 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000934 if (Op.isDebug())
935 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000936 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000937 // Print the sub register.
938 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000939 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000940 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
941 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000942 if (TypeToPrint.isValid())
943 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000944 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000945 case MachineOperand::MO_Immediate:
946 OS << Op.getImm();
947 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000948 case MachineOperand::MO_CImmediate:
949 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
950 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000951 case MachineOperand::MO_FPImmediate:
952 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
953 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000954 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000955 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000956 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000957 case MachineOperand::MO_FrameIndex:
958 printStackObjectReference(Op.getIndex());
959 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000960 case MachineOperand::MO_ConstantPoolIndex:
961 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000962 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000963 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000964 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000965 OS << "target-index(";
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000966 if (const auto *Name =
967 getTargetIndexName(*Op.getParent()->getMF(), Op.getIndex()))
Alex Lorenzef5c1962015-07-28 23:02:45 +0000968 OS << Name;
969 else
970 OS << "<unknown>";
971 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000972 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000973 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000974 case MachineOperand::MO_JumpTableIndex:
975 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000976 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000977 case MachineOperand::MO_ExternalSymbol: {
978 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000979 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000980 if (Name.empty()) {
981 OS << "\"\"";
982 } else {
983 printLLVMNameWithoutPrefix(OS, Name);
984 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000985 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000986 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000987 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000988 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000989 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000990 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000991 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000992 case MachineOperand::MO_BlockAddress:
993 OS << "blockaddress(";
994 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
995 MST);
996 OS << ", ";
997 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
998 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000999 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +00001000 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001001 case MachineOperand::MO_RegisterMask: {
1002 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
1003 if (RegMaskInfo != RegisterMaskIds.end())
1004 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
1005 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001006 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001007 break;
1008 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001009 case MachineOperand::MO_RegisterLiveOut: {
1010 const uint32_t *RegMask = Op.getRegLiveOut();
1011 OS << "liveout(";
1012 bool IsCommaNeeded = false;
1013 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
1014 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
1015 if (IsCommaNeeded)
1016 OS << ", ";
1017 printReg(Reg, OS, TRI);
1018 IsCommaNeeded = true;
1019 }
1020 }
1021 OS << ")";
1022 break;
1023 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001024 case MachineOperand::MO_Metadata:
1025 Op.getMetadata()->printAsOperand(OS, MST);
1026 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001027 case MachineOperand::MO_MCSymbol:
1028 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1029 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001030 case MachineOperand::MO_CFIIndex: {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001031 const MachineFunction &MF = *Op.getParent()->getMF();
Matthias Braunf23ef432016-11-30 23:48:42 +00001032 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001033 break;
1034 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001035 case MachineOperand::MO_IntrinsicID: {
1036 Intrinsic::ID ID = Op.getIntrinsicID();
1037 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001038 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001039 else {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001040 const MachineFunction &MF = *Op.getParent()->getMF();
Tim Northover6b3bd612016-07-29 20:32:59 +00001041 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1042 OS << "intrinsic(@" << TII->getName(ID) << ')';
1043 }
1044 break;
1045 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001046 case MachineOperand::MO_Predicate: {
1047 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1048 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1049 << CmpInst::getPredicateName(Pred) << ')';
1050 break;
1051 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001052 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001053}
1054
Geoff Berry6748abe2017-07-13 02:28:54 +00001055static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
1056 unsigned TMMOFlag) {
1057 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
1058 for (const auto &I : Flags) {
1059 if (I.first == TMMOFlag) {
1060 return I.second;
1061 }
1062 }
1063 return nullptr;
1064}
1065
1066void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII,
1067 const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001068 OS << '(';
Alex Lorenza518b792015-08-04 00:24:45 +00001069 if (Op.isVolatile())
1070 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001071 if (Op.isNonTemporal())
1072 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001073 if (Op.isDereferenceable())
1074 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001075 if (Op.isInvariant())
1076 OS << "invariant ";
Geoff Berry6748abe2017-07-13 02:28:54 +00001077 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1)
1078 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1)
1079 << "\" ";
1080 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2)
1081 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2)
1082 << "\" ";
1083 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3)
1084 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3)
1085 << "\" ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001086 if (Op.isLoad())
1087 OS << "load ";
1088 else {
1089 assert(Op.isStore() && "Non load machine operand must be a store");
1090 OS << "store ";
1091 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001092
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001093 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001094
1095 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1096 OS << toIRString(Op.getOrdering()) << ' ';
1097 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1098 OS << toIRString(Op.getFailureOrdering()) << ' ';
1099
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001100 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001101 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001102 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001103 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001104 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1105 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001106 assert(PVal && "Expected a pseudo source value");
1107 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001108 case PseudoSourceValue::Stack:
1109 OS << "stack";
1110 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001111 case PseudoSourceValue::GOT:
1112 OS << "got";
1113 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001114 case PseudoSourceValue::JumpTable:
1115 OS << "jump-table";
1116 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001117 case PseudoSourceValue::ConstantPool:
1118 OS << "constant-pool";
1119 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001120 case PseudoSourceValue::FixedStack:
1121 printStackObjectReference(
1122 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1123 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001124 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001125 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001126 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1127 OS, /*PrintType=*/false, MST);
1128 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001129 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001130 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001131 printLLVMNameWithoutPrefix(
1132 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001133 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001134 case PseudoSourceValue::TargetCustom:
1135 llvm_unreachable("TargetCustom pseudo source values are not supported");
1136 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001137 }
1138 }
Alex Lorenz83127732015-08-07 20:26:52 +00001139 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001140 if (Op.getBaseAlignment() != Op.getSize())
1141 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001142 auto AAInfo = Op.getAAInfo();
1143 if (AAInfo.TBAA) {
1144 OS << ", !tbaa ";
1145 AAInfo.TBAA->printAsOperand(OS, MST);
1146 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001147 if (AAInfo.Scope) {
1148 OS << ", !alias.scope ";
1149 AAInfo.Scope->printAsOperand(OS, MST);
1150 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001151 if (AAInfo.NoAlias) {
1152 OS << ", !noalias ";
1153 AAInfo.NoAlias->printAsOperand(OS, MST);
1154 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001155 if (Op.getRanges()) {
1156 OS << ", !range ";
1157 Op.getRanges()->printAsOperand(OS, MST);
1158 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001159 OS << ')';
1160}
1161
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001162void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1163 switch (SSID) {
1164 case SyncScope::System: {
1165 break;
1166 }
1167 default: {
1168 if (SSNs.empty())
1169 Context.getSyncScopeNames(SSNs);
1170
1171 OS << "syncscope(\"";
1172 PrintEscapedString(SSNs[SSID], OS);
1173 OS << "\") ";
1174 break;
1175 }
1176 }
1177}
1178
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001179static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1180 const TargetRegisterInfo *TRI) {
1181 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1182 if (Reg == -1) {
1183 OS << "<badreg>";
1184 return;
1185 }
1186 printReg(Reg, OS, TRI);
1187}
1188
1189void MIPrinter::print(const MCCFIInstruction &CFI,
1190 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001191 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001192 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001193 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001194 if (CFI.getLabel())
1195 OS << "<mcsymbol> ";
1196 printCFIRegister(CFI.getRegister(), OS, TRI);
1197 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001198 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001199 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001200 if (CFI.getLabel())
1201 OS << "<mcsymbol> ";
1202 printCFIRegister(CFI.getRegister(), OS, TRI);
1203 OS << ", " << CFI.getOffset();
1204 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001205 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001206 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001207 if (CFI.getLabel())
1208 OS << "<mcsymbol> ";
1209 printCFIRegister(CFI.getRegister(), OS, TRI);
1210 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001211 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001212 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001213 if (CFI.getLabel())
1214 OS << "<mcsymbol> ";
1215 OS << CFI.getOffset();
1216 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001217 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001218 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001219 if (CFI.getLabel())
1220 OS << "<mcsymbol> ";
1221 printCFIRegister(CFI.getRegister(), OS, TRI);
1222 OS << ", " << CFI.getOffset();
1223 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001224 default:
1225 // TODO: Print the other CFI Operations.
1226 OS << "<unserializable cfi operation>";
1227 break;
1228 }
1229}
1230
Alex Lorenz345c1442015-06-15 23:52:35 +00001231void llvm::printMIR(raw_ostream &OS, const Module &M) {
1232 yaml::Output Out(OS);
1233 Out << const_cast<Module &>(M);
1234}
1235
1236void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1237 MIRPrinter Printer(OS);
1238 Printer.print(MF);
1239}