blob: 070c05d7f0effa07927c2af44b3399526c43424a [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);
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000165 void print(const MachineInstr &MI, unsigned OpIdx,
166 const TargetRegisterInfo *TRI, 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
Justin Bogner6c452832017-10-24 18:04:54 +0000273static void printRegClassOrBank(unsigned Reg, raw_ostream &OS,
274 const MachineRegisterInfo &RegInfo,
275 const TargetRegisterInfo *TRI) {
276 if (RegInfo.getRegClassOrNull(Reg))
277 OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
278 else if (RegInfo.getRegBankOrNull(Reg))
279 OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
280 else {
281 OS << "_";
282 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
283 "Generic registers must have a valid type");
284 }
285}
286
287static void printRegClassOrBank(unsigned Reg, yaml::StringValue &Dest,
288 const MachineRegisterInfo &RegInfo,
289 const TargetRegisterInfo *TRI) {
290 raw_string_ostream OS(Dest.Value);
291 printRegClassOrBank(Reg, OS, RegInfo, TRI);
292}
293
294
Alex Lorenz54565cf2015-06-24 19:56:10 +0000295void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000296 const MachineRegisterInfo &RegInfo,
297 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000298 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000299
300 // Print the virtual register definitions.
301 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
302 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
303 yaml::VirtualRegisterDefinition VReg;
304 VReg.ID = I;
Justin Bogner6c452832017-10-24 18:04:54 +0000305 printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000306 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
307 if (PreferredReg)
308 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000309 MF.VirtualRegisters.push_back(VReg);
310 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000311
312 // Print the live ins.
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000313 for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000314 yaml::MachineFunctionLiveIn LiveIn;
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000315 printReg(LI.first, LiveIn.Register, TRI);
316 if (LI.second)
317 printReg(LI.second, LiveIn.VirtualRegister, TRI);
Alex Lorenz12045a42015-07-27 17:42:45 +0000318 MF.LiveIns.push_back(LiveIn);
319 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000320
321 // Prints the callee saved registers.
322 if (RegInfo.isUpdatedCSRsInitialized()) {
323 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
324 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
325 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000326 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000327 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000328 CalleeSavedRegisters.push_back(Reg);
329 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000330 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000331 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000332}
333
Alex Lorenza6f9a372015-07-29 21:09:09 +0000334void MIRPrinter::convert(ModuleSlotTracker &MST,
335 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000336 const MachineFrameInfo &MFI) {
337 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
338 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
339 YamlMFI.HasStackMap = MFI.hasStackMap();
340 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
341 YamlMFI.StackSize = MFI.getStackSize();
342 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
343 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
344 YamlMFI.AdjustsStack = MFI.adjustsStack();
345 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000346 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
347 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000348 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
349 YamlMFI.HasVAStart = MFI.hasVAStart();
350 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000351 if (MFI.getSavePoint()) {
352 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
353 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
354 .printMBBReference(*MFI.getSavePoint());
355 }
356 if (MFI.getRestorePoint()) {
357 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
358 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
359 .printMBBReference(*MFI.getRestorePoint());
360 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000361}
362
Matthias Braunef331ef2016-11-30 23:48:50 +0000363void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
364 const MachineFunction &MF,
365 ModuleSlotTracker &MST) {
366 const MachineFrameInfo &MFI = MF.getFrameInfo();
367 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000368 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000369 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000370 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
371 if (MFI.isDeadObjectIndex(I))
372 continue;
373
374 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000375 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000376 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
377 ? yaml::FixedMachineStackObject::SpillSlot
378 : yaml::FixedMachineStackObject::DefaultType;
379 YamlObject.Offset = MFI.getObjectOffset(I);
380 YamlObject.Size = MFI.getObjectSize(I);
381 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000382 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzde491f02015-07-13 18:07:26 +0000383 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
384 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000385 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000386 StackObjectOperandMapping.insert(
387 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000388 }
389
390 // Process ordinary stack objects.
391 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000392 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
393 if (MFI.isDeadObjectIndex(I))
394 continue;
395
396 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000397 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000398 if (const auto *Alloca = MFI.getObjectAllocation(I))
399 YamlObject.Name.Value =
400 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000401 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
402 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000403 : MFI.isVariableSizedObjectIndex(I)
404 ? yaml::MachineStackObject::VariableSized
405 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000406 YamlObject.Offset = MFI.getObjectOffset(I);
407 YamlObject.Size = MFI.getObjectSize(I);
408 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000409 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000410
Matthias Braunef331ef2016-11-30 23:48:50 +0000411 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000412 StackObjectOperandMapping.insert(std::make_pair(
413 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000414 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000415
416 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
417 yaml::StringValue Reg;
418 printReg(CSInfo.getReg(), Reg, TRI);
419 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
420 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
421 "Invalid stack object index");
422 const FrameIndexOperand &StackObject = StackObjectInfo->second;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000423 if (StackObject.IsFixed) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000424 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000425 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
426 CSInfo.isRestored();
427 } else {
Matthias Braunef331ef2016-11-30 23:48:50 +0000428 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000429 YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
430 CSInfo.isRestored();
431 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000432 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000433 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
434 auto LocalObject = MFI.getLocalFrameObjectMap(I);
435 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
436 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
437 "Invalid stack object index");
438 const FrameIndexOperand &StackObject = StackObjectInfo->second;
439 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000440 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000441 }
Alex Lorenza314d812015-08-18 22:26:26 +0000442
443 // Print the stack object references in the frame information class after
444 // converting the stack objects.
445 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000446 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000447 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
448 .printStackObjectReference(MFI.getStackProtectorIndex());
449 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000450
451 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000452 for (const MachineFunction::VariableDbgInfo &DebugVar :
453 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000454 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
455 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
456 "Invalid stack object index");
457 const FrameIndexOperand &StackObject = StackObjectInfo->second;
458 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000459 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000460 {
461 raw_string_ostream StrOS(Object.DebugVar.Value);
462 DebugVar.Var->printAsOperand(StrOS, MST);
463 }
464 {
465 raw_string_ostream StrOS(Object.DebugExpr.Value);
466 DebugVar.Expr->printAsOperand(StrOS, MST);
467 }
468 {
469 raw_string_ostream StrOS(Object.DebugLoc.Value);
470 DebugVar.Loc->printAsOperand(StrOS, MST);
471 }
472 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000473}
474
Alex Lorenzab980492015-07-20 20:51:18 +0000475void MIRPrinter::convert(yaml::MachineFunction &MF,
476 const MachineConstantPool &ConstantPool) {
477 unsigned ID = 0;
478 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
Alex Lorenzab980492015-07-20 20:51:18 +0000479 std::string Str;
480 raw_string_ostream StrOS(Str);
Diana Picusd5a00b02017-08-02 11:09:30 +0000481 if (Constant.isMachineConstantPoolEntry()) {
482 Constant.Val.MachineCPVal->print(StrOS);
483 } else {
484 Constant.Val.ConstVal->printAsOperand(StrOS);
485 }
486
487 yaml::MachineConstantPoolValue YamlConstant;
Alex Lorenzab980492015-07-20 20:51:18 +0000488 YamlConstant.ID = ID++;
489 YamlConstant.Value = StrOS.str();
490 YamlConstant.Alignment = Constant.getAlignment();
Diana Picusd5a00b02017-08-02 11:09:30 +0000491 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
492
Alex Lorenzab980492015-07-20 20:51:18 +0000493 MF.Constants.push_back(YamlConstant);
494 }
495}
496
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000497void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000498 yaml::MachineJumpTable &YamlJTI,
499 const MachineJumpTableInfo &JTI) {
500 YamlJTI.Kind = JTI.getEntryKind();
501 unsigned ID = 0;
502 for (const auto &Table : JTI.getJumpTables()) {
503 std::string Str;
504 yaml::MachineJumpTable::Entry Entry;
505 Entry.ID = ID++;
506 for (const auto *MBB : Table.MBBs) {
507 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000508 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
509 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000510 Entry.Blocks.push_back(StrOS.str());
511 Str.clear();
512 }
513 YamlJTI.Entries.push_back(Entry);
514 }
515}
516
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000517void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
518 const auto *TRI = MF.getSubtarget().getRegisterInfo();
519 unsigned I = 0;
520 for (const uint32_t *Mask : TRI->getRegMasks())
521 RegisterMaskIds.insert(std::make_pair(Mask, I++));
522}
523
Matthias Braun89401142017-05-05 21:09:30 +0000524void llvm::guessSuccessors(const MachineBasicBlock &MBB,
525 SmallVectorImpl<MachineBasicBlock*> &Result,
526 bool &IsFallthrough) {
527 SmallPtrSet<MachineBasicBlock*,8> Seen;
528
529 for (const MachineInstr &MI : MBB) {
530 if (MI.isPHI())
531 continue;
532 for (const MachineOperand &MO : MI.operands()) {
533 if (!MO.isMBB())
534 continue;
535 MachineBasicBlock *Succ = MO.getMBB();
536 auto RP = Seen.insert(Succ);
537 if (RP.second)
538 Result.push_back(Succ);
539 }
540 }
541 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
542 IsFallthrough = I == MBB.end() || !I->isBarrier();
543}
544
545bool
546MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
547 if (MBB.succ_size() <= 1)
548 return true;
549 if (!MBB.hasSuccessorProbabilities())
550 return true;
551
552 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
553 MBB.Probs.end());
554 BranchProbability::normalizeProbabilities(Normalized.begin(),
555 Normalized.end());
556 SmallVector<BranchProbability,8> Equal(Normalized.size());
557 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
558
559 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
560}
561
562bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
563 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
564 bool GuessedFallthrough;
565 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
566 if (GuessedFallthrough) {
567 const MachineFunction &MF = *MBB.getParent();
568 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
569 if (NextI != MF.end()) {
570 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
571 if (!is_contained(GuessedSuccs, Next))
572 GuessedSuccs.push_back(Next);
573 }
574 }
575 if (GuessedSuccs.size() != MBB.succ_size())
576 return false;
577 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
578}
579
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000580void MIPrinter::print(const MachineBasicBlock &MBB) {
581 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
582 OS << "bb." << MBB.getNumber();
583 bool HasAttributes = false;
584 if (const auto *BB = MBB.getBasicBlock()) {
585 if (BB->hasName()) {
586 OS << "." << BB->getName();
587 } else {
588 HasAttributes = true;
589 OS << " (";
590 int Slot = MST.getLocalSlot(BB);
591 if (Slot == -1)
592 OS << "<ir-block badref>";
593 else
594 OS << (Twine("%ir-block.") + Twine(Slot)).str();
595 }
596 }
597 if (MBB.hasAddressTaken()) {
598 OS << (HasAttributes ? ", " : " (");
599 OS << "address-taken";
600 HasAttributes = true;
601 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000602 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000603 OS << (HasAttributes ? ", " : " (");
604 OS << "landing-pad";
605 HasAttributes = true;
606 }
607 if (MBB.getAlignment()) {
608 OS << (HasAttributes ? ", " : " (");
609 OS << "align " << MBB.getAlignment();
610 HasAttributes = true;
611 }
612 if (HasAttributes)
613 OS << ")";
614 OS << ":\n";
615
616 bool HasLineAttributes = false;
617 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000618 bool canPredictProbs = canPredictBranchProbabilities(MBB);
Quentin Colombetd652aeb2017-09-19 23:34:12 +0000619 // Even if the list of successors is empty, if we cannot guess it,
620 // we need to print it to tell the parser that the list is empty.
621 // This is needed, because MI model unreachable as empty blocks
622 // with an empty successor list. If the parser would see that
623 // without the successor list, it would guess the code would
624 // fallthrough.
625 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
626 !canPredictSuccessors(MBB)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000627 OS.indent(2) << "successors: ";
628 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
629 if (I != MBB.succ_begin())
630 OS << ", ";
631 printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000632 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000633 OS << '('
634 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
635 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000636 }
637 OS << "\n";
638 HasLineAttributes = true;
639 }
640
641 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000642 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
643 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
644 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000645 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000646 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000647 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000648 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000649 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000650 First = false;
Matthias Braun11723322017-01-05 20:01:19 +0000651 printReg(LI.PhysReg, OS, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000652 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000653 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000654 }
655 OS << "\n";
656 HasLineAttributes = true;
657 }
658
659 if (HasLineAttributes)
660 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000661 bool IsInBundle = false;
662 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
663 const MachineInstr &MI = *I;
664 if (IsInBundle && !MI.isInsideBundle()) {
665 OS.indent(2) << "}\n";
666 IsInBundle = false;
667 }
668 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000669 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000670 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
671 OS << " {";
672 IsInBundle = true;
673 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000674 OS << "\n";
675 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000676 if (IsInBundle)
677 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000678}
679
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000680/// Return true when an instruction has tied register that can't be determined
681/// by the instruction's descriptor.
682static bool hasComplexRegisterTies(const MachineInstr &MI) {
683 const MCInstrDesc &MCID = MI.getDesc();
684 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
685 const auto &Operand = MI.getOperand(I);
686 if (!Operand.isReg() || Operand.isDef())
687 // Ignore the defined registers as MCID marks only the uses as tied.
688 continue;
689 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
690 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
691 if (ExpectedTiedIdx != TiedIdx)
692 return true;
693 }
694 return false;
695}
696
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000697static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
698 SmallBitVector &PrintedTypes,
699 const MachineRegisterInfo &MRI) {
700 const MachineOperand &Op = MI.getOperand(OpIdx);
701 if (!Op.isReg())
702 return LLT{};
703
704 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
705 return MRI.getType(Op.getReg());
706
707 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
708 if (!OpInfo.isGenericType())
709 return MRI.getType(Op.getReg());
710
711 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
712 return LLT{};
713
714 PrintedTypes.set(OpInfo.getGenericTypeIndex());
715 return MRI.getType(Op.getReg());
716}
717
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000718void MIPrinter::print(const MachineInstr &MI) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000719 const auto *MF = MI.getMF();
Quentin Colombet4e14a492016-03-07 21:57:52 +0000720 const auto &MRI = MF->getRegInfo();
721 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000722 const auto *TRI = SubTarget.getRegisterInfo();
723 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000724 const auto *TII = SubTarget.getInstrInfo();
725 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000726 if (MI.isCFIInstruction())
727 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000728
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000729 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000730 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000731 unsigned I = 0, E = MI.getNumOperands();
732 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
733 !MI.getOperand(I).isImplicit();
734 ++I) {
735 if (I)
736 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000737 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000738 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000739 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000740 }
741
742 if (I)
743 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000744 if (MI.getFlag(MachineInstr::FrameSetup))
745 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000746 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000747 if (I < E)
748 OS << ' ';
749
750 bool NeedComma = false;
751 for (; I < E; ++I) {
752 if (NeedComma)
753 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000754 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000755 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000756 NeedComma = true;
757 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000758
759 if (MI.getDebugLoc()) {
760 if (NeedComma)
761 OS << ',';
762 OS << " debug-location ";
763 MI.getDebugLoc()->printAsOperand(OS, MST);
764 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000765
766 if (!MI.memoperands_empty()) {
767 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000768 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000769 bool NeedComma = false;
770 for (const auto *Op : MI.memoperands()) {
771 if (NeedComma)
772 OS << ", ";
Geoff Berry6748abe2017-07-13 02:28:54 +0000773 print(Context, *TII, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000774 NeedComma = true;
775 }
776 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000777}
778
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000779void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
780 OS << "%bb." << MBB.getNumber();
781 if (const auto *BB = MBB.getBasicBlock()) {
782 if (BB->hasName())
783 OS << '.' << BB->getName();
784 }
785}
786
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000787static void printIRSlotNumber(raw_ostream &OS, int Slot) {
788 if (Slot == -1)
789 OS << "<badref>";
790 else
791 OS << Slot;
792}
793
Alex Lorenzdeb53492015-07-28 17:28:03 +0000794void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
795 OS << "%ir-block.";
796 if (BB.hasName()) {
797 printLLVMNameWithoutPrefix(OS, BB.getName());
798 return;
799 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000800 const Function *F = BB.getParent();
801 int Slot;
802 if (F == MST.getCurrentFunction()) {
803 Slot = MST.getLocalSlot(&BB);
804 } else {
805 ModuleSlotTracker CustomMST(F->getParent(),
806 /*ShouldInitializeAllMetadata=*/false);
807 CustomMST.incorporateFunction(*F);
808 Slot = CustomMST.getLocalSlot(&BB);
809 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000810 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000811}
812
Alex Lorenz4af7e612015-08-03 23:08:19 +0000813void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000814 if (isa<GlobalValue>(V)) {
815 V.printAsOperand(OS, /*PrintType=*/false, MST);
816 return;
817 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000818 if (isa<Constant>(V)) {
819 // Machine memory operands can load/store to/from constant value pointers.
820 OS << '`';
821 V.printAsOperand(OS, /*PrintType=*/true, MST);
822 OS << '`';
823 return;
824 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000825 OS << "%ir.";
826 if (V.hasName()) {
827 printLLVMNameWithoutPrefix(OS, V.getName());
828 return;
829 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000830 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000831}
832
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000833void MIPrinter::printStackObjectReference(int FrameIndex) {
834 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
835 assert(ObjectInfo != StackObjectOperandMapping.end() &&
836 "Invalid frame index");
837 const FrameIndexOperand &Operand = ObjectInfo->second;
838 if (Operand.IsFixed) {
839 OS << "%fixed-stack." << Operand.ID;
840 return;
841 }
842 OS << "%stack." << Operand.ID;
843 if (!Operand.Name.empty())
844 OS << '.' << Operand.Name;
845}
846
Alex Lorenz5672a892015-08-05 22:26:15 +0000847void MIPrinter::printOffset(int64_t Offset) {
848 if (Offset == 0)
849 return;
850 if (Offset < 0) {
851 OS << " - " << -Offset;
852 return;
853 }
854 OS << " + " << Offset;
855}
856
Alex Lorenz49873a82015-08-06 00:44:07 +0000857static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
858 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
859 for (const auto &I : Flags) {
860 if (I.first == TF) {
861 return I.second;
862 }
863 }
864 return nullptr;
865}
866
867void MIPrinter::printTargetFlags(const MachineOperand &Op) {
868 if (!Op.getTargetFlags())
869 return;
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000870 const auto *TII = Op.getParent()->getMF()->getSubtarget().getInstrInfo();
Alex Lorenz49873a82015-08-06 00:44:07 +0000871 assert(TII && "expected instruction info");
872 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
873 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000874 const bool HasDirectFlags = Flags.first;
875 const bool HasBitmaskFlags = Flags.second;
876 if (!HasDirectFlags && !HasBitmaskFlags) {
877 OS << "<unknown>) ";
878 return;
879 }
880 if (HasDirectFlags) {
881 if (const auto *Name = getTargetFlagName(TII, Flags.first))
882 OS << Name;
883 else
884 OS << "<unknown target flag>";
885 }
886 if (!HasBitmaskFlags) {
887 OS << ") ";
888 return;
889 }
890 bool IsCommaNeeded = HasDirectFlags;
891 unsigned BitMask = Flags.second;
892 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
893 for (const auto &Mask : BitMasks) {
894 // Check if the flag's bitmask has the bits of the current mask set.
895 if ((BitMask & Mask.first) == Mask.first) {
896 if (IsCommaNeeded)
897 OS << ", ";
898 IsCommaNeeded = true;
899 OS << Mask.second;
900 // Clear the bits which were serialized from the flag's bitmask.
901 BitMask &= ~(Mask.first);
902 }
903 }
904 if (BitMask) {
905 // When the resulting flag's bitmask isn't zero, we know that we didn't
906 // serialize all of the bit flags.
907 if (IsCommaNeeded)
908 OS << ", ";
909 OS << "<unknown bitmask target flag>";
910 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000911 OS << ") ";
912}
913
Alex Lorenzef5c1962015-07-28 23:02:45 +0000914static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
915 const auto *TII = MF.getSubtarget().getInstrInfo();
916 assert(TII && "expected instruction info");
917 auto Indices = TII->getSerializableTargetIndices();
918 for (const auto &I : Indices) {
919 if (I.first == Index) {
920 return I.second;
921 }
922 }
923 return nullptr;
924}
925
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000926void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
927 const TargetRegisterInfo *TRI,
928 bool ShouldPrintRegisterTies, LLT TypeToPrint,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000929 bool IsDef) {
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000930 const MachineOperand &Op = MI.getOperand(OpIdx);
Alex Lorenz49873a82015-08-06 00:44:07 +0000931 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000932 switch (Op.getType()) {
Justin Bogner6c452832017-10-24 18:04:54 +0000933 case MachineOperand::MO_Register: {
934 unsigned Reg = Op.getReg();
Alex Lorenzcb268d42015-07-06 23:07:26 +0000935 if (Op.isImplicit())
936 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000937 else if (!IsDef && Op.isDef())
938 // Print the 'def' flag only when the operand is defined after '='.
939 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000940 if (Op.isInternalRead())
941 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000942 if (Op.isDead())
943 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000944 if (Op.isKill())
945 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000946 if (Op.isUndef())
947 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000948 if (Op.isEarlyClobber())
949 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000950 if (Op.isDebug())
951 OS << "debug-use ";
Justin Bogner6c452832017-10-24 18:04:54 +0000952 printReg(Reg, OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000953 // Print the sub register.
954 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000955 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Justin Bogner6c452832017-10-24 18:04:54 +0000956 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
957 const MachineRegisterInfo &MRI = Op.getParent()->getMF()->getRegInfo();
958 if (IsDef || MRI.def_empty(Reg)) {
959 OS << ':';
960 printRegClassOrBank(Reg, OS, MRI, TRI);
961 }
962 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000963 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000964 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(OpIdx) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000965 if (TypeToPrint.isValid())
966 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000967 break;
Justin Bogner6c452832017-10-24 18:04:54 +0000968 }
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000969 case MachineOperand::MO_Immediate:
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000970 if (MI.isOperandSubregIdx(OpIdx))
971 OS << "%subreg." << TRI->getSubRegIndexName(Op.getImm());
972 else
973 OS << Op.getImm();
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000974 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000975 case MachineOperand::MO_CImmediate:
976 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
977 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000978 case MachineOperand::MO_FPImmediate:
979 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
980 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000981 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000982 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000983 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000984 case MachineOperand::MO_FrameIndex:
985 printStackObjectReference(Op.getIndex());
986 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000987 case MachineOperand::MO_ConstantPoolIndex:
988 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000989 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000990 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000991 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000992 OS << "target-index(";
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000993 if (const auto *Name =
994 getTargetIndexName(*Op.getParent()->getMF(), Op.getIndex()))
Alex Lorenzef5c1962015-07-28 23:02:45 +0000995 OS << Name;
996 else
997 OS << "<unknown>";
998 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000999 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +00001000 break;
Alex Lorenz31d70682015-07-15 23:38:35 +00001001 case MachineOperand::MO_JumpTableIndex:
1002 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001003 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +00001004 case MachineOperand::MO_ExternalSymbol: {
1005 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001006 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +00001007 if (Name.empty()) {
1008 OS << "\"\"";
1009 } else {
1010 printLLVMNameWithoutPrefix(OS, Name);
1011 }
Alex Lorenz5672a892015-08-05 22:26:15 +00001012 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001013 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +00001014 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001015 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +00001016 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +00001017 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001018 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001019 case MachineOperand::MO_BlockAddress:
1020 OS << "blockaddress(";
1021 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
1022 MST);
1023 OS << ", ";
1024 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
1025 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +00001026 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +00001027 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001028 case MachineOperand::MO_RegisterMask: {
1029 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
1030 if (RegMaskInfo != RegisterMaskIds.end())
1031 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
1032 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001033 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001034 break;
1035 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001036 case MachineOperand::MO_RegisterLiveOut: {
1037 const uint32_t *RegMask = Op.getRegLiveOut();
1038 OS << "liveout(";
1039 bool IsCommaNeeded = false;
1040 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
1041 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
1042 if (IsCommaNeeded)
1043 OS << ", ";
1044 printReg(Reg, OS, TRI);
1045 IsCommaNeeded = true;
1046 }
1047 }
1048 OS << ")";
1049 break;
1050 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001051 case MachineOperand::MO_Metadata:
1052 Op.getMetadata()->printAsOperand(OS, MST);
1053 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001054 case MachineOperand::MO_MCSymbol:
1055 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1056 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001057 case MachineOperand::MO_CFIIndex: {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001058 const MachineFunction &MF = *Op.getParent()->getMF();
Matthias Braunf23ef432016-11-30 23:48:42 +00001059 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001060 break;
1061 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001062 case MachineOperand::MO_IntrinsicID: {
1063 Intrinsic::ID ID = Op.getIntrinsicID();
1064 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001065 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001066 else {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001067 const MachineFunction &MF = *Op.getParent()->getMF();
Tim Northover6b3bd612016-07-29 20:32:59 +00001068 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1069 OS << "intrinsic(@" << TII->getName(ID) << ')';
1070 }
1071 break;
1072 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001073 case MachineOperand::MO_Predicate: {
1074 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1075 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1076 << CmpInst::getPredicateName(Pred) << ')';
1077 break;
1078 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001079 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001080}
1081
Geoff Berry6748abe2017-07-13 02:28:54 +00001082static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
1083 unsigned TMMOFlag) {
1084 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
1085 for (const auto &I : Flags) {
1086 if (I.first == TMMOFlag) {
1087 return I.second;
1088 }
1089 }
1090 return nullptr;
1091}
1092
1093void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII,
1094 const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001095 OS << '(';
Alex Lorenza518b792015-08-04 00:24:45 +00001096 if (Op.isVolatile())
1097 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001098 if (Op.isNonTemporal())
1099 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001100 if (Op.isDereferenceable())
1101 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001102 if (Op.isInvariant())
1103 OS << "invariant ";
Geoff Berry6748abe2017-07-13 02:28:54 +00001104 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1)
1105 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1)
1106 << "\" ";
1107 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2)
1108 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2)
1109 << "\" ";
1110 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3)
1111 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3)
1112 << "\" ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001113 if (Op.isLoad())
1114 OS << "load ";
1115 else {
1116 assert(Op.isStore() && "Non load machine operand must be a store");
1117 OS << "store ";
1118 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001119
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001120 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001121
1122 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1123 OS << toIRString(Op.getOrdering()) << ' ';
1124 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1125 OS << toIRString(Op.getFailureOrdering()) << ' ';
1126
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001127 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001128 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001129 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001130 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001131 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1132 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001133 assert(PVal && "Expected a pseudo source value");
1134 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001135 case PseudoSourceValue::Stack:
1136 OS << "stack";
1137 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001138 case PseudoSourceValue::GOT:
1139 OS << "got";
1140 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001141 case PseudoSourceValue::JumpTable:
1142 OS << "jump-table";
1143 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001144 case PseudoSourceValue::ConstantPool:
1145 OS << "constant-pool";
1146 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001147 case PseudoSourceValue::FixedStack:
1148 printStackObjectReference(
1149 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1150 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001151 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001152 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001153 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1154 OS, /*PrintType=*/false, MST);
1155 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001156 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001157 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001158 printLLVMNameWithoutPrefix(
1159 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001160 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001161 case PseudoSourceValue::TargetCustom:
1162 llvm_unreachable("TargetCustom pseudo source values are not supported");
1163 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001164 }
1165 }
Alex Lorenz83127732015-08-07 20:26:52 +00001166 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001167 if (Op.getBaseAlignment() != Op.getSize())
1168 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001169 auto AAInfo = Op.getAAInfo();
1170 if (AAInfo.TBAA) {
1171 OS << ", !tbaa ";
1172 AAInfo.TBAA->printAsOperand(OS, MST);
1173 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001174 if (AAInfo.Scope) {
1175 OS << ", !alias.scope ";
1176 AAInfo.Scope->printAsOperand(OS, MST);
1177 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001178 if (AAInfo.NoAlias) {
1179 OS << ", !noalias ";
1180 AAInfo.NoAlias->printAsOperand(OS, MST);
1181 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001182 if (Op.getRanges()) {
1183 OS << ", !range ";
1184 Op.getRanges()->printAsOperand(OS, MST);
1185 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001186 OS << ')';
1187}
1188
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001189void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1190 switch (SSID) {
1191 case SyncScope::System: {
1192 break;
1193 }
1194 default: {
1195 if (SSNs.empty())
1196 Context.getSyncScopeNames(SSNs);
1197
1198 OS << "syncscope(\"";
1199 PrintEscapedString(SSNs[SSID], OS);
1200 OS << "\") ";
1201 break;
1202 }
1203 }
1204}
1205
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001206static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1207 const TargetRegisterInfo *TRI) {
1208 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1209 if (Reg == -1) {
1210 OS << "<badreg>";
1211 return;
1212 }
1213 printReg(Reg, OS, TRI);
1214}
1215
1216void MIPrinter::print(const MCCFIInstruction &CFI,
1217 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001218 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001219 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001220 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001221 if (CFI.getLabel())
1222 OS << "<mcsymbol> ";
1223 printCFIRegister(CFI.getRegister(), OS, TRI);
1224 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001225 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001226 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001227 if (CFI.getLabel())
1228 OS << "<mcsymbol> ";
1229 printCFIRegister(CFI.getRegister(), OS, TRI);
1230 OS << ", " << CFI.getOffset();
1231 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001232 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001233 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001234 if (CFI.getLabel())
1235 OS << "<mcsymbol> ";
1236 printCFIRegister(CFI.getRegister(), OS, TRI);
1237 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001238 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001239 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001240 if (CFI.getLabel())
1241 OS << "<mcsymbol> ";
1242 OS << CFI.getOffset();
1243 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001244 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001245 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001246 if (CFI.getLabel())
1247 OS << "<mcsymbol> ";
1248 printCFIRegister(CFI.getRegister(), OS, TRI);
1249 OS << ", " << CFI.getOffset();
1250 break;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001251 case MCCFIInstruction::OpRestore:
1252 OS << "restore ";
1253 if (CFI.getLabel())
1254 OS << "<mcsymbol> ";
1255 printCFIRegister(CFI.getRegister(), OS, TRI);
1256 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001257 default:
1258 // TODO: Print the other CFI Operations.
1259 OS << "<unserializable cfi operation>";
1260 break;
1261 }
1262}
1263
Alex Lorenz345c1442015-06-15 23:52:35 +00001264void llvm::printMIR(raw_ostream &OS, const Module &M) {
1265 yaml::Output Out(OS);
1266 Out << const_cast<Module &>(M);
1267}
1268
1269void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1270 MIRPrinter Printer(OS);
1271 Printer.print(MF);
1272}