blob: aa0f38036b124236a4497288bf292cb845b5f703 [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the class that prints out the LLVM IR and machine
11// functions using the MIR serialization format.
12//
13//===----------------------------------------------------------------------===//
14
David Blaikie3f833ed2017-11-08 01:01:31 +000015#include "llvm/CodeGen/MIRPrinter.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/None.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000018#include "llvm/ADT/STLExtras.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000019#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000022#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000023#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000025#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000026#include "llvm/CodeGen/MIRYamlMapping.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenzab980492015-07-20 20:51:18 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000030#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000031#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineJumpTableInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000034#include "llvm/CodeGen/MachineOperand.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000040#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000041#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000042#include "llvm/IR/DebugInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000043#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/Function.h"
45#include "llvm/IR/GlobalValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000046#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000047#include "llvm/IR/InstrTypes.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000048#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000049#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000050#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000051#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000052#include "llvm/IR/Value.h"
53#include "llvm/MC/LaneBitmask.h"
54#include "llvm/MC/MCDwarf.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000055#include "llvm/MC/MCSymbol.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000056#include "llvm/Support/AtomicOrdering.h"
57#include "llvm/Support/BranchProbability.h"
58#include "llvm/Support/Casting.h"
59#include "llvm/Support/CommandLine.h"
60#include "llvm/Support/ErrorHandling.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000061#include "llvm/Support/Format.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000062#include "llvm/Support/LowLevelTypeImpl.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000063#include "llvm/Support/YAMLTraits.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000064#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000065#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000066#include "llvm/Target/TargetMachine.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000067#include <algorithm>
68#include <cassert>
69#include <cinttypes>
70#include <cstdint>
71#include <iterator>
72#include <string>
73#include <utility>
74#include <vector>
Alex Lorenz345c1442015-06-15 23:52:35 +000075
76using namespace llvm;
77
Zachary Turner8065f0b2017-12-01 00:53:10 +000078static cl::opt<bool> SimplifyMIR(
79 "simplify-mir", cl::Hidden,
Matthias Braun89401142017-05-05 21:09:30 +000080 cl::desc("Leave out unnecessary information when printing MIR"));
81
Alex Lorenz345c1442015-06-15 23:52:35 +000082namespace {
83
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000084/// This structure describes how to print out stack object references.
85struct FrameIndexOperand {
86 std::string Name;
87 unsigned ID;
88 bool IsFixed;
89
90 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
91 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
92
93 /// Return an ordinary stack object reference.
94 static FrameIndexOperand create(StringRef Name, unsigned ID) {
95 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
96 }
97
98 /// Return a fixed stack object reference.
99 static FrameIndexOperand createFixed(unsigned ID) {
100 return FrameIndexOperand("", ID, /*IsFixed=*/true);
101 }
102};
103
Alex Lorenz618b2832015-07-30 16:54:38 +0000104} // end anonymous namespace
105
106namespace llvm {
107
Alex Lorenz345c1442015-06-15 23:52:35 +0000108/// This class prints out the machine functions using the MIR serialization
109/// format.
110class MIRPrinter {
111 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000112 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000113 /// Maps from stack object indices to operand indices which will be used when
114 /// printing frame index machine operands.
115 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +0000116
117public:
118 MIRPrinter(raw_ostream &OS) : OS(OS) {}
119
120 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000121
Alex Lorenz28148ba2015-07-09 22:23:13 +0000122 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
123 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000124 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
125 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +0000126 void convert(yaml::MachineFunction &MF,
127 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000128 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
129 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +0000130 void convertStackObjects(yaml::MachineFunction &YMF,
131 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000132
133private:
134 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000135};
136
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000137/// This class prints out the machine instructions using the MIR serialization
138/// format.
139class MIPrinter {
140 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000141 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000142 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000143 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000144 /// Synchronization scope names registered with LLVMContext.
145 SmallVector<StringRef, 8> SSNs;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000146
Matthias Braun89401142017-05-05 21:09:30 +0000147 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
148 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
149
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000150public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000151 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000152 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
153 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
154 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
155 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000156
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000157 void print(const MachineBasicBlock &MBB);
158
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000159 void print(const MachineInstr &MI);
Alex 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
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000195static void printRegMIR(unsigned Reg, yaml::StringValue &Dest,
196 const TargetRegisterInfo *TRI) {
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000197 raw_string_ostream OS(Dest.Value);
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000198 OS << printReg(Reg, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000199}
200
Alex Lorenz345c1442015-06-15 23:52:35 +0000201void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000202 initRegisterMaskIds(MF);
203
Alex Lorenz345c1442015-06-15 23:52:35 +0000204 yaml::MachineFunction YamlMF;
205 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000206 YamlMF.Alignment = MF.getAlignment();
207 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000208
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000209 YamlMF.Legalized = MF.getProperties().hasProperty(
210 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000211 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
212 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000213 YamlMF.Selected = MF.getProperties().hasProperty(
214 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000215
Alex Lorenz28148ba2015-07-09 22:23:13 +0000216 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000217 ModuleSlotTracker MST(MF.getFunction()->getParent());
218 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000219 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000220 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000221 if (const auto *ConstantPool = MF.getConstantPool())
222 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000223 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
224 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000225 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
226 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000227 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000228 if (IsNewlineNeeded)
229 StrOS << "\n";
230 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
231 .print(MBB);
232 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000233 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000234 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000235 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000236 if (!SimplifyMIR)
237 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000238 Out << YamlMF;
239}
240
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000241static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
242 const TargetRegisterInfo *TRI) {
243 assert(RegMask && "Can't print an empty register mask");
244 OS << StringRef("CustomRegMask(");
245
246 bool IsRegInRegMaskFound = false;
247 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
248 // Check whether the register is asserted in regmask.
249 if (RegMask[I / 32] & (1u << (I % 32))) {
250 if (IsRegInRegMaskFound)
251 OS << ',';
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000252 OS << printReg(I, TRI);
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000253 IsRegInRegMaskFound = true;
254 }
255 }
256
257 OS << ')';
258}
259
Justin Bogner6c452832017-10-24 18:04:54 +0000260static void printRegClassOrBank(unsigned Reg, raw_ostream &OS,
261 const MachineRegisterInfo &RegInfo,
262 const TargetRegisterInfo *TRI) {
263 if (RegInfo.getRegClassOrNull(Reg))
264 OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
265 else if (RegInfo.getRegBankOrNull(Reg))
266 OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
267 else {
268 OS << "_";
269 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
270 "Generic registers must have a valid type");
271 }
272}
273
274static void printRegClassOrBank(unsigned Reg, yaml::StringValue &Dest,
275 const MachineRegisterInfo &RegInfo,
276 const TargetRegisterInfo *TRI) {
277 raw_string_ostream OS(Dest.Value);
278 printRegClassOrBank(Reg, OS, RegInfo, TRI);
279}
280
281
Alex Lorenz54565cf2015-06-24 19:56:10 +0000282void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000283 const MachineRegisterInfo &RegInfo,
284 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000285 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000286
287 // Print the virtual register definitions.
288 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
289 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
290 yaml::VirtualRegisterDefinition VReg;
291 VReg.ID = I;
Justin Bogner6c452832017-10-24 18:04:54 +0000292 printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000293 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
294 if (PreferredReg)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000295 printRegMIR(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.
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000300 for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000301 yaml::MachineFunctionLiveIn LiveIn;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000302 printRegMIR(LI.first, LiveIn.Register, TRI);
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000303 if (LI.second)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000304 printRegMIR(LI.second, LiveIn.VirtualRegister, TRI);
Alex Lorenz12045a42015-07-27 17:42:45 +0000305 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;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000314 printRegMIR(*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);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000340 StrOS << printMBBReference(*MFI.getSavePoint());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000341 }
342 if (MFI.getRestorePoint()) {
343 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000344 StrOS << printMBBReference(*MFI.getRestorePoint());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000345 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000346}
347
Matthias Braunef331ef2016-11-30 23:48:50 +0000348void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
349 const MachineFunction &MF,
350 ModuleSlotTracker &MST) {
351 const MachineFrameInfo &MFI = MF.getFrameInfo();
352 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000353 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000354 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000355 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
356 if (MFI.isDeadObjectIndex(I))
357 continue;
358
359 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000360 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000361 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
362 ? yaml::FixedMachineStackObject::SpillSlot
363 : yaml::FixedMachineStackObject::DefaultType;
364 YamlObject.Offset = MFI.getObjectOffset(I);
365 YamlObject.Size = MFI.getObjectSize(I);
366 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000367 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzde491f02015-07-13 18:07:26 +0000368 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
369 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000370 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000371 StackObjectOperandMapping.insert(
372 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000373 }
374
375 // Process ordinary stack objects.
376 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000377 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
378 if (MFI.isDeadObjectIndex(I))
379 continue;
380
381 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000382 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000383 if (const auto *Alloca = MFI.getObjectAllocation(I))
384 YamlObject.Name.Value =
385 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000386 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
387 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000388 : MFI.isVariableSizedObjectIndex(I)
389 ? yaml::MachineStackObject::VariableSized
390 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000391 YamlObject.Offset = MFI.getObjectOffset(I);
392 YamlObject.Size = MFI.getObjectSize(I);
393 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000394 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000395
Matthias Braunef331ef2016-11-30 23:48:50 +0000396 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000397 StackObjectOperandMapping.insert(std::make_pair(
398 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000399 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000400
401 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
402 yaml::StringValue Reg;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000403 printRegMIR(CSInfo.getReg(), Reg, TRI);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000404 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
405 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
406 "Invalid stack object index");
407 const FrameIndexOperand &StackObject = StackObjectInfo->second;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000408 if (StackObject.IsFixed) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000409 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000410 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
411 CSInfo.isRestored();
412 } else {
Matthias Braunef331ef2016-11-30 23:48:50 +0000413 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000414 YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
415 CSInfo.isRestored();
416 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000417 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000418 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
419 auto LocalObject = MFI.getLocalFrameObjectMap(I);
420 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
421 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
422 "Invalid stack object index");
423 const FrameIndexOperand &StackObject = StackObjectInfo->second;
424 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000425 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000426 }
Alex Lorenza314d812015-08-18 22:26:26 +0000427
428 // Print the stack object references in the frame information class after
429 // converting the stack objects.
430 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000431 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000432 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
433 .printStackObjectReference(MFI.getStackProtectorIndex());
434 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000435
436 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000437 for (const MachineFunction::VariableDbgInfo &DebugVar :
438 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000439 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
440 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
441 "Invalid stack object index");
442 const FrameIndexOperand &StackObject = StackObjectInfo->second;
443 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000444 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000445 {
446 raw_string_ostream StrOS(Object.DebugVar.Value);
447 DebugVar.Var->printAsOperand(StrOS, MST);
448 }
449 {
450 raw_string_ostream StrOS(Object.DebugExpr.Value);
451 DebugVar.Expr->printAsOperand(StrOS, MST);
452 }
453 {
454 raw_string_ostream StrOS(Object.DebugLoc.Value);
455 DebugVar.Loc->printAsOperand(StrOS, MST);
456 }
457 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000458}
459
Alex Lorenzab980492015-07-20 20:51:18 +0000460void MIRPrinter::convert(yaml::MachineFunction &MF,
461 const MachineConstantPool &ConstantPool) {
462 unsigned ID = 0;
463 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
Alex Lorenzab980492015-07-20 20:51:18 +0000464 std::string Str;
465 raw_string_ostream StrOS(Str);
Diana Picusd5a00b02017-08-02 11:09:30 +0000466 if (Constant.isMachineConstantPoolEntry()) {
467 Constant.Val.MachineCPVal->print(StrOS);
468 } else {
469 Constant.Val.ConstVal->printAsOperand(StrOS);
470 }
471
472 yaml::MachineConstantPoolValue YamlConstant;
Alex Lorenzab980492015-07-20 20:51:18 +0000473 YamlConstant.ID = ID++;
474 YamlConstant.Value = StrOS.str();
475 YamlConstant.Alignment = Constant.getAlignment();
Diana Picusd5a00b02017-08-02 11:09:30 +0000476 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
477
Alex Lorenzab980492015-07-20 20:51:18 +0000478 MF.Constants.push_back(YamlConstant);
479 }
480}
481
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000482void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000483 yaml::MachineJumpTable &YamlJTI,
484 const MachineJumpTableInfo &JTI) {
485 YamlJTI.Kind = JTI.getEntryKind();
486 unsigned ID = 0;
487 for (const auto &Table : JTI.getJumpTables()) {
488 std::string Str;
489 yaml::MachineJumpTable::Entry Entry;
490 Entry.ID = ID++;
491 for (const auto *MBB : Table.MBBs) {
492 raw_string_ostream StrOS(Str);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000493 StrOS << printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000494 Entry.Blocks.push_back(StrOS.str());
495 Str.clear();
496 }
497 YamlJTI.Entries.push_back(Entry);
498 }
499}
500
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000501void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
502 const auto *TRI = MF.getSubtarget().getRegisterInfo();
503 unsigned I = 0;
504 for (const uint32_t *Mask : TRI->getRegMasks())
505 RegisterMaskIds.insert(std::make_pair(Mask, I++));
506}
507
Matthias Braun89401142017-05-05 21:09:30 +0000508void llvm::guessSuccessors(const MachineBasicBlock &MBB,
509 SmallVectorImpl<MachineBasicBlock*> &Result,
510 bool &IsFallthrough) {
511 SmallPtrSet<MachineBasicBlock*,8> Seen;
512
513 for (const MachineInstr &MI : MBB) {
514 if (MI.isPHI())
515 continue;
516 for (const MachineOperand &MO : MI.operands()) {
517 if (!MO.isMBB())
518 continue;
519 MachineBasicBlock *Succ = MO.getMBB();
520 auto RP = Seen.insert(Succ);
521 if (RP.second)
522 Result.push_back(Succ);
523 }
524 }
525 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
526 IsFallthrough = I == MBB.end() || !I->isBarrier();
527}
528
529bool
530MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
531 if (MBB.succ_size() <= 1)
532 return true;
533 if (!MBB.hasSuccessorProbabilities())
534 return true;
535
536 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
537 MBB.Probs.end());
538 BranchProbability::normalizeProbabilities(Normalized.begin(),
539 Normalized.end());
540 SmallVector<BranchProbability,8> Equal(Normalized.size());
541 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
542
543 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
544}
545
546bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
547 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
548 bool GuessedFallthrough;
549 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
550 if (GuessedFallthrough) {
551 const MachineFunction &MF = *MBB.getParent();
552 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
553 if (NextI != MF.end()) {
554 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
555 if (!is_contained(GuessedSuccs, Next))
556 GuessedSuccs.push_back(Next);
557 }
558 }
559 if (GuessedSuccs.size() != MBB.succ_size())
560 return false;
561 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
562}
563
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000564void MIPrinter::print(const MachineBasicBlock &MBB) {
565 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
566 OS << "bb." << MBB.getNumber();
567 bool HasAttributes = false;
568 if (const auto *BB = MBB.getBasicBlock()) {
569 if (BB->hasName()) {
570 OS << "." << BB->getName();
571 } else {
572 HasAttributes = true;
573 OS << " (";
574 int Slot = MST.getLocalSlot(BB);
575 if (Slot == -1)
576 OS << "<ir-block badref>";
577 else
578 OS << (Twine("%ir-block.") + Twine(Slot)).str();
579 }
580 }
581 if (MBB.hasAddressTaken()) {
582 OS << (HasAttributes ? ", " : " (");
583 OS << "address-taken";
584 HasAttributes = true;
585 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000586 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000587 OS << (HasAttributes ? ", " : " (");
588 OS << "landing-pad";
589 HasAttributes = true;
590 }
591 if (MBB.getAlignment()) {
592 OS << (HasAttributes ? ", " : " (");
593 OS << "align " << MBB.getAlignment();
594 HasAttributes = true;
595 }
596 if (HasAttributes)
597 OS << ")";
598 OS << ":\n";
599
600 bool HasLineAttributes = false;
601 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000602 bool canPredictProbs = canPredictBranchProbabilities(MBB);
Quentin Colombetd652aeb2017-09-19 23:34:12 +0000603 // Even if the list of successors is empty, if we cannot guess it,
604 // we need to print it to tell the parser that the list is empty.
605 // This is needed, because MI model unreachable as empty blocks
606 // with an empty successor list. If the parser would see that
607 // without the successor list, it would guess the code would
608 // fallthrough.
609 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
610 !canPredictSuccessors(MBB)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000611 OS.indent(2) << "successors: ";
612 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
613 if (I != MBB.succ_begin())
614 OS << ", ";
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000615 OS << printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000616 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000617 OS << '('
618 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
619 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000620 }
621 OS << "\n";
622 HasLineAttributes = true;
623 }
624
625 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000626 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
627 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
628 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000629 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000630 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000631 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000632 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000633 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000634 First = false;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000635 OS << printReg(LI.PhysReg, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000636 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000637 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000638 }
639 OS << "\n";
640 HasLineAttributes = true;
641 }
642
643 if (HasLineAttributes)
644 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000645 bool IsInBundle = false;
646 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
647 const MachineInstr &MI = *I;
648 if (IsInBundle && !MI.isInsideBundle()) {
649 OS.indent(2) << "}\n";
650 IsInBundle = false;
651 }
652 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000653 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000654 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
655 OS << " {";
656 IsInBundle = true;
657 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000658 OS << "\n";
659 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000660 if (IsInBundle)
661 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000662}
663
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000664/// Return true when an instruction has tied register that can't be determined
665/// by the instruction's descriptor.
666static bool hasComplexRegisterTies(const MachineInstr &MI) {
667 const MCInstrDesc &MCID = MI.getDesc();
668 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
669 const auto &Operand = MI.getOperand(I);
670 if (!Operand.isReg() || Operand.isDef())
671 // Ignore the defined registers as MCID marks only the uses as tied.
672 continue;
673 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
674 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
675 if (ExpectedTiedIdx != TiedIdx)
676 return true;
677 }
678 return false;
679}
680
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000681static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
682 SmallBitVector &PrintedTypes,
683 const MachineRegisterInfo &MRI) {
684 const MachineOperand &Op = MI.getOperand(OpIdx);
685 if (!Op.isReg())
686 return LLT{};
687
688 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
689 return MRI.getType(Op.getReg());
690
691 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
692 if (!OpInfo.isGenericType())
693 return MRI.getType(Op.getReg());
694
695 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
696 return LLT{};
697
698 PrintedTypes.set(OpInfo.getGenericTypeIndex());
699 return MRI.getType(Op.getReg());
700}
701
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000702void MIPrinter::print(const MachineInstr &MI) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000703 const auto *MF = MI.getMF();
Quentin Colombet4e14a492016-03-07 21:57:52 +0000704 const auto &MRI = MF->getRegInfo();
705 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000706 const auto *TRI = SubTarget.getRegisterInfo();
707 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000708 const auto *TII = SubTarget.getInstrInfo();
709 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000710 if (MI.isCFIInstruction())
711 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000712
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000713 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000714 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000715 unsigned I = 0, E = MI.getNumOperands();
716 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
717 !MI.getOperand(I).isImplicit();
718 ++I) {
719 if (I)
720 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000721 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000722 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000723 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000724 }
725
726 if (I)
727 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000728 if (MI.getFlag(MachineInstr::FrameSetup))
729 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000730 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000731 if (I < E)
732 OS << ' ';
733
734 bool NeedComma = false;
735 for (; I < E; ++I) {
736 if (NeedComma)
737 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000738 print(MI, I, TRI, ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000739 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000740 NeedComma = true;
741 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000742
743 if (MI.getDebugLoc()) {
744 if (NeedComma)
745 OS << ',';
746 OS << " debug-location ";
747 MI.getDebugLoc()->printAsOperand(OS, MST);
748 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000749
750 if (!MI.memoperands_empty()) {
751 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000752 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000753 bool NeedComma = false;
754 for (const auto *Op : MI.memoperands()) {
755 if (NeedComma)
756 OS << ", ";
Geoff Berry6748abe2017-07-13 02:28:54 +0000757 print(Context, *TII, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000758 NeedComma = true;
759 }
760 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000761}
762
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000763static void printIRSlotNumber(raw_ostream &OS, int Slot) {
764 if (Slot == -1)
765 OS << "<badref>";
766 else
767 OS << Slot;
768}
769
Alex Lorenzdeb53492015-07-28 17:28:03 +0000770void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
771 OS << "%ir-block.";
772 if (BB.hasName()) {
773 printLLVMNameWithoutPrefix(OS, BB.getName());
774 return;
775 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000776 const Function *F = BB.getParent();
777 int Slot;
778 if (F == MST.getCurrentFunction()) {
779 Slot = MST.getLocalSlot(&BB);
780 } else {
781 ModuleSlotTracker CustomMST(F->getParent(),
782 /*ShouldInitializeAllMetadata=*/false);
783 CustomMST.incorporateFunction(*F);
784 Slot = CustomMST.getLocalSlot(&BB);
785 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000786 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000787}
788
Alex Lorenz4af7e612015-08-03 23:08:19 +0000789void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000790 if (isa<GlobalValue>(V)) {
791 V.printAsOperand(OS, /*PrintType=*/false, MST);
792 return;
793 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000794 if (isa<Constant>(V)) {
795 // Machine memory operands can load/store to/from constant value pointers.
796 OS << '`';
797 V.printAsOperand(OS, /*PrintType=*/true, MST);
798 OS << '`';
799 return;
800 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000801 OS << "%ir.";
802 if (V.hasName()) {
803 printLLVMNameWithoutPrefix(OS, V.getName());
804 return;
805 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000806 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000807}
808
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000809void MIPrinter::printStackObjectReference(int FrameIndex) {
810 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
811 assert(ObjectInfo != StackObjectOperandMapping.end() &&
812 "Invalid frame index");
813 const FrameIndexOperand &Operand = ObjectInfo->second;
814 if (Operand.IsFixed) {
815 OS << "%fixed-stack." << Operand.ID;
816 return;
817 }
818 OS << "%stack." << Operand.ID;
819 if (!Operand.Name.empty())
820 OS << '.' << Operand.Name;
821}
822
Alex Lorenz5672a892015-08-05 22:26:15 +0000823void MIPrinter::printOffset(int64_t Offset) {
824 if (Offset == 0)
825 return;
826 if (Offset < 0) {
827 OS << " - " << -Offset;
828 return;
829 }
830 OS << " + " << Offset;
831}
832
Alex Lorenz49873a82015-08-06 00:44:07 +0000833static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
834 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
835 for (const auto &I : Flags) {
836 if (I.first == TF) {
837 return I.second;
838 }
839 }
840 return nullptr;
841}
842
843void MIPrinter::printTargetFlags(const MachineOperand &Op) {
844 if (!Op.getTargetFlags())
845 return;
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000846 const auto *TII = Op.getParent()->getMF()->getSubtarget().getInstrInfo();
Alex Lorenz49873a82015-08-06 00:44:07 +0000847 assert(TII && "expected instruction info");
848 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
849 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000850 const bool HasDirectFlags = Flags.first;
851 const bool HasBitmaskFlags = Flags.second;
852 if (!HasDirectFlags && !HasBitmaskFlags) {
853 OS << "<unknown>) ";
854 return;
855 }
856 if (HasDirectFlags) {
857 if (const auto *Name = getTargetFlagName(TII, Flags.first))
858 OS << Name;
859 else
860 OS << "<unknown target flag>";
861 }
862 if (!HasBitmaskFlags) {
863 OS << ") ";
864 return;
865 }
866 bool IsCommaNeeded = HasDirectFlags;
867 unsigned BitMask = Flags.second;
868 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
869 for (const auto &Mask : BitMasks) {
870 // Check if the flag's bitmask has the bits of the current mask set.
871 if ((BitMask & Mask.first) == Mask.first) {
872 if (IsCommaNeeded)
873 OS << ", ";
874 IsCommaNeeded = true;
875 OS << Mask.second;
876 // Clear the bits which were serialized from the flag's bitmask.
877 BitMask &= ~(Mask.first);
878 }
879 }
880 if (BitMask) {
881 // When the resulting flag's bitmask isn't zero, we know that we didn't
882 // serialize all of the bit flags.
883 if (IsCommaNeeded)
884 OS << ", ";
885 OS << "<unknown bitmask target flag>";
886 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000887 OS << ") ";
888}
889
Alex Lorenzef5c1962015-07-28 23:02:45 +0000890static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
891 const auto *TII = MF.getSubtarget().getInstrInfo();
892 assert(TII && "expected instruction info");
893 auto Indices = TII->getSerializableTargetIndices();
894 for (const auto &I : Indices) {
895 if (I.first == Index) {
896 return I.second;
897 }
898 }
899 return nullptr;
900}
901
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000902void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
903 const TargetRegisterInfo *TRI,
904 bool ShouldPrintRegisterTies, LLT TypeToPrint,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000905 bool IsDef) {
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000906 const MachineOperand &Op = MI.getOperand(OpIdx);
Alex Lorenz49873a82015-08-06 00:44:07 +0000907 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000908 switch (Op.getType()) {
Justin Bogner6c452832017-10-24 18:04:54 +0000909 case MachineOperand::MO_Register: {
910 unsigned Reg = Op.getReg();
Alex Lorenzcb268d42015-07-06 23:07:26 +0000911 if (Op.isImplicit())
912 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000913 else if (!IsDef && Op.isDef())
914 // Print the 'def' flag only when the operand is defined after '='.
915 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000916 if (Op.isInternalRead())
917 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000918 if (Op.isDead())
919 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000920 if (Op.isKill())
921 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000922 if (Op.isUndef())
923 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000924 if (Op.isEarlyClobber())
925 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000926 if (Op.isDebug())
927 OS << "debug-use ";
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000928 OS << printReg(Reg, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000929 // Print the sub register.
930 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000931 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Justin Bogner6c452832017-10-24 18:04:54 +0000932 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
933 const MachineRegisterInfo &MRI = Op.getParent()->getMF()->getRegInfo();
934 if (IsDef || MRI.def_empty(Reg)) {
935 OS << ':';
936 printRegClassOrBank(Reg, OS, MRI, TRI);
937 }
938 }
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000939 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000940 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(OpIdx) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000941 if (TypeToPrint.isValid())
942 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000943 break;
Justin Bogner6c452832017-10-24 18:04:54 +0000944 }
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000945 case MachineOperand::MO_Immediate:
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000946 if (MI.isOperandSubregIdx(OpIdx))
947 OS << "%subreg." << TRI->getSubRegIndexName(Op.getImm());
948 else
949 OS << Op.getImm();
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000950 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000951 case MachineOperand::MO_CImmediate:
952 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
953 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000954 case MachineOperand::MO_FPImmediate:
955 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
956 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000957 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000958 OS << printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000959 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000960 case MachineOperand::MO_FrameIndex:
961 printStackObjectReference(Op.getIndex());
962 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000963 case MachineOperand::MO_ConstantPoolIndex:
964 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000965 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000966 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000967 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000968 OS << "target-index(";
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000969 if (const auto *Name =
970 getTargetIndexName(*Op.getParent()->getMF(), Op.getIndex()))
Alex Lorenzef5c1962015-07-28 23:02:45 +0000971 OS << Name;
972 else
973 OS << "<unknown>";
974 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000975 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000976 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000977 case MachineOperand::MO_JumpTableIndex:
978 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000979 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000980 case MachineOperand::MO_ExternalSymbol: {
981 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000982 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000983 if (Name.empty()) {
984 OS << "\"\"";
985 } else {
986 printLLVMNameWithoutPrefix(OS, Name);
987 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000988 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000989 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000990 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000991 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000992 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000993 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000994 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000995 case MachineOperand::MO_BlockAddress:
996 OS << "blockaddress(";
997 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
998 MST);
999 OS << ", ";
1000 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
1001 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +00001002 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +00001003 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001004 case MachineOperand::MO_RegisterMask: {
1005 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
1006 if (RegMaskInfo != RegisterMaskIds.end())
1007 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
1008 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001009 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +00001010 break;
1011 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001012 case MachineOperand::MO_RegisterLiveOut: {
1013 const uint32_t *RegMask = Op.getRegLiveOut();
1014 OS << "liveout(";
1015 bool IsCommaNeeded = false;
1016 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
1017 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
1018 if (IsCommaNeeded)
1019 OS << ", ";
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001020 OS << printReg(Reg, TRI);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001021 IsCommaNeeded = true;
1022 }
1023 }
1024 OS << ")";
1025 break;
1026 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001027 case MachineOperand::MO_Metadata:
1028 Op.getMetadata()->printAsOperand(OS, MST);
1029 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001030 case MachineOperand::MO_MCSymbol:
1031 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1032 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001033 case MachineOperand::MO_CFIIndex: {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001034 const MachineFunction &MF = *Op.getParent()->getMF();
Matthias Braunf23ef432016-11-30 23:48:42 +00001035 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001036 break;
1037 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001038 case MachineOperand::MO_IntrinsicID: {
1039 Intrinsic::ID ID = Op.getIntrinsicID();
1040 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001041 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001042 else {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001043 const MachineFunction &MF = *Op.getParent()->getMF();
Tim Northover6b3bd612016-07-29 20:32:59 +00001044 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1045 OS << "intrinsic(@" << TII->getName(ID) << ')';
1046 }
1047 break;
1048 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001049 case MachineOperand::MO_Predicate: {
1050 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1051 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1052 << CmpInst::getPredicateName(Pred) << ')';
1053 break;
1054 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001055 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001056}
1057
Geoff Berry6748abe2017-07-13 02:28:54 +00001058static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
1059 unsigned TMMOFlag) {
1060 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
1061 for (const auto &I : Flags) {
1062 if (I.first == TMMOFlag) {
1063 return I.second;
1064 }
1065 }
1066 return nullptr;
1067}
1068
1069void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII,
1070 const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001071 OS << '(';
Alex Lorenza518b792015-08-04 00:24:45 +00001072 if (Op.isVolatile())
1073 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001074 if (Op.isNonTemporal())
1075 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001076 if (Op.isDereferenceable())
1077 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001078 if (Op.isInvariant())
1079 OS << "invariant ";
Geoff Berry6748abe2017-07-13 02:28:54 +00001080 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1)
1081 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1)
1082 << "\" ";
1083 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2)
1084 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2)
1085 << "\" ";
1086 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3)
1087 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3)
1088 << "\" ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001089
1090 assert((Op.isLoad() || Op.isStore()) && "machine memory operand must be a load or store (or both)");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001091 if (Op.isLoad())
1092 OS << "load ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001093 if (Op.isStore())
Alex Lorenz4af7e612015-08-03 23:08:19 +00001094 OS << "store ";
Tim Northoverb73e3092017-02-13 22:14:08 +00001095
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001096 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001097
1098 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1099 OS << toIRString(Op.getOrdering()) << ' ';
1100 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1101 OS << toIRString(Op.getFailureOrdering()) << ' ';
1102
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001103 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001104 if (const Value *Val = Op.getValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001105 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1106 : Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001107 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001108 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001109 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1110 : Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001111 assert(PVal && "Expected a pseudo source value");
1112 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001113 case PseudoSourceValue::Stack:
1114 OS << "stack";
1115 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001116 case PseudoSourceValue::GOT:
1117 OS << "got";
1118 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001119 case PseudoSourceValue::JumpTable:
1120 OS << "jump-table";
1121 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001122 case PseudoSourceValue::ConstantPool:
1123 OS << "constant-pool";
1124 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001125 case PseudoSourceValue::FixedStack:
1126 printStackObjectReference(
1127 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1128 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001129 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001130 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001131 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1132 OS, /*PrintType=*/false, MST);
1133 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001134 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001135 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001136 printLLVMNameWithoutPrefix(
1137 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001138 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001139 case PseudoSourceValue::TargetCustom:
1140 llvm_unreachable("TargetCustom pseudo source values are not supported");
1141 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001142 }
1143 }
Alex Lorenz83127732015-08-07 20:26:52 +00001144 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001145 if (Op.getBaseAlignment() != Op.getSize())
1146 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001147 auto AAInfo = Op.getAAInfo();
1148 if (AAInfo.TBAA) {
1149 OS << ", !tbaa ";
1150 AAInfo.TBAA->printAsOperand(OS, MST);
1151 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001152 if (AAInfo.Scope) {
1153 OS << ", !alias.scope ";
1154 AAInfo.Scope->printAsOperand(OS, MST);
1155 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001156 if (AAInfo.NoAlias) {
1157 OS << ", !noalias ";
1158 AAInfo.NoAlias->printAsOperand(OS, MST);
1159 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001160 if (Op.getRanges()) {
1161 OS << ", !range ";
1162 Op.getRanges()->printAsOperand(OS, MST);
1163 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001164 OS << ')';
1165}
1166
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001167void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1168 switch (SSID) {
1169 case SyncScope::System: {
1170 break;
1171 }
1172 default: {
1173 if (SSNs.empty())
1174 Context.getSyncScopeNames(SSNs);
1175
1176 OS << "syncscope(\"";
1177 PrintEscapedString(SSNs[SSID], OS);
1178 OS << "\") ";
1179 break;
1180 }
1181 }
1182}
1183
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001184static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1185 const TargetRegisterInfo *TRI) {
1186 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1187 if (Reg == -1) {
1188 OS << "<badreg>";
1189 return;
1190 }
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001191 OS << printReg(Reg, TRI);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001192}
1193
1194void MIPrinter::print(const MCCFIInstruction &CFI,
1195 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001196 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001197 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001198 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001199 if (CFI.getLabel())
1200 OS << "<mcsymbol> ";
1201 printCFIRegister(CFI.getRegister(), OS, TRI);
1202 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001203 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001204 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001205 if (CFI.getLabel())
1206 OS << "<mcsymbol> ";
1207 printCFIRegister(CFI.getRegister(), OS, TRI);
1208 OS << ", " << CFI.getOffset();
1209 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001210 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001211 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001212 if (CFI.getLabel())
1213 OS << "<mcsymbol> ";
1214 printCFIRegister(CFI.getRegister(), OS, TRI);
1215 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001216 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001217 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001218 if (CFI.getLabel())
1219 OS << "<mcsymbol> ";
1220 OS << CFI.getOffset();
1221 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001222 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001223 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001224 if (CFI.getLabel())
1225 OS << "<mcsymbol> ";
1226 printCFIRegister(CFI.getRegister(), OS, TRI);
1227 OS << ", " << CFI.getOffset();
1228 break;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001229 case MCCFIInstruction::OpRestore:
1230 OS << "restore ";
1231 if (CFI.getLabel())
1232 OS << "<mcsymbol> ";
1233 printCFIRegister(CFI.getRegister(), OS, TRI);
1234 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001235 default:
1236 // TODO: Print the other CFI Operations.
1237 OS << "<unserializable cfi operation>";
1238 break;
1239 }
1240}
1241
Alex Lorenz345c1442015-06-15 23:52:35 +00001242void llvm::printMIR(raw_ostream &OS, const Module &M) {
1243 yaml::Output Out(OS);
1244 Out << const_cast<Module &>(M);
1245}
1246
1247void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1248 MIRPrinter Printer(OS);
1249 Printer.print(MF);
1250}