blob: e8a358e5209cd5ba6b9c9ead35f183f8a3a24e07 [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,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000167 LLT TypeToPrint, bool PrintDef = true);
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, yaml::StringValue &Dest,
261 const MachineRegisterInfo &RegInfo,
262 const TargetRegisterInfo *TRI) {
263 raw_string_ostream OS(Dest.Value);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000264 OS << printRegClassOrBank(Reg, RegInfo, TRI);
Justin Bogner6c452832017-10-24 18:04:54 +0000265}
266
267
Alex Lorenz54565cf2015-06-24 19:56:10 +0000268void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000269 const MachineRegisterInfo &RegInfo,
270 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000271 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000272
273 // Print the virtual register definitions.
274 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
275 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
276 yaml::VirtualRegisterDefinition VReg;
277 VReg.ID = I;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000278 ::printRegClassOrBank(Reg, VReg.Class, RegInfo, TRI);
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000279 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
280 if (PreferredReg)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000281 printRegMIR(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000282 MF.VirtualRegisters.push_back(VReg);
283 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000284
285 // Print the live ins.
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000286 for (std::pair<unsigned, unsigned> LI : RegInfo.liveins()) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000287 yaml::MachineFunctionLiveIn LiveIn;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000288 printRegMIR(LI.first, LiveIn.Register, TRI);
Krzysztof Parzyszek72518ea2017-10-16 19:08:41 +0000289 if (LI.second)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000290 printRegMIR(LI.second, LiveIn.VirtualRegister, TRI);
Alex Lorenz12045a42015-07-27 17:42:45 +0000291 MF.LiveIns.push_back(LiveIn);
292 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000293
294 // Prints the callee saved registers.
295 if (RegInfo.isUpdatedCSRsInitialized()) {
296 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
297 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
298 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000299 yaml::FlowStringValue Reg;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000300 printRegMIR(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000301 CalleeSavedRegisters.push_back(Reg);
302 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000303 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000304 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000305}
306
Alex Lorenza6f9a372015-07-29 21:09:09 +0000307void MIRPrinter::convert(ModuleSlotTracker &MST,
308 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000309 const MachineFrameInfo &MFI) {
310 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
311 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
312 YamlMFI.HasStackMap = MFI.hasStackMap();
313 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
314 YamlMFI.StackSize = MFI.getStackSize();
315 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
316 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
317 YamlMFI.AdjustsStack = MFI.adjustsStack();
318 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000319 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
320 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000321 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
322 YamlMFI.HasVAStart = MFI.hasVAStart();
323 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000324 if (MFI.getSavePoint()) {
325 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000326 StrOS << printMBBReference(*MFI.getSavePoint());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000327 }
328 if (MFI.getRestorePoint()) {
329 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000330 StrOS << printMBBReference(*MFI.getRestorePoint());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000331 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000332}
333
Matthias Braunef331ef2016-11-30 23:48:50 +0000334void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
335 const MachineFunction &MF,
336 ModuleSlotTracker &MST) {
337 const MachineFrameInfo &MFI = MF.getFrameInfo();
338 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000339 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000340 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000341 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
342 if (MFI.isDeadObjectIndex(I))
343 continue;
344
345 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000346 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000347 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
348 ? yaml::FixedMachineStackObject::SpillSlot
349 : yaml::FixedMachineStackObject::DefaultType;
350 YamlObject.Offset = MFI.getObjectOffset(I);
351 YamlObject.Size = MFI.getObjectSize(I);
352 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000353 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzde491f02015-07-13 18:07:26 +0000354 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
355 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
Matthias Braunef331ef2016-11-30 23:48:50 +0000356 YMF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000357 StackObjectOperandMapping.insert(
358 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000359 }
360
361 // Process ordinary stack objects.
362 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000363 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
364 if (MFI.isDeadObjectIndex(I))
365 continue;
366
367 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000368 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000369 if (const auto *Alloca = MFI.getObjectAllocation(I))
370 YamlObject.Name.Value =
371 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000372 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
373 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000374 : MFI.isVariableSizedObjectIndex(I)
375 ? yaml::MachineStackObject::VariableSized
376 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000377 YamlObject.Offset = MFI.getObjectOffset(I);
378 YamlObject.Size = MFI.getObjectSize(I);
379 YamlObject.Alignment = MFI.getObjectAlignment(I);
Matt Arsenaultdb782732017-07-20 21:03:45 +0000380 YamlObject.StackID = MFI.getStackID(I);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000381
Matthias Braunef331ef2016-11-30 23:48:50 +0000382 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000383 StackObjectOperandMapping.insert(std::make_pair(
384 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000385 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000386
387 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
388 yaml::StringValue Reg;
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000389 printRegMIR(CSInfo.getReg(), Reg, TRI);
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000390 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
391 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
392 "Invalid stack object index");
393 const FrameIndexOperand &StackObject = StackObjectInfo->second;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000394 if (StackObject.IsFixed) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000395 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000396 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRestored =
397 CSInfo.isRestored();
398 } else {
Matthias Braunef331ef2016-11-30 23:48:50 +0000399 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Matthias Braun5c3e8a42017-09-28 18:52:14 +0000400 YMF.StackObjects[StackObject.ID].CalleeSavedRestored =
401 CSInfo.isRestored();
402 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000403 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000404 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
405 auto LocalObject = MFI.getLocalFrameObjectMap(I);
406 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
407 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
408 "Invalid stack object index");
409 const FrameIndexOperand &StackObject = StackObjectInfo->second;
410 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000411 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000412 }
Alex Lorenza314d812015-08-18 22:26:26 +0000413
414 // Print the stack object references in the frame information class after
415 // converting the stack objects.
416 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000417 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000418 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
419 .printStackObjectReference(MFI.getStackProtectorIndex());
420 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000421
422 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000423 for (const MachineFunction::VariableDbgInfo &DebugVar :
424 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000425 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
426 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
427 "Invalid stack object index");
428 const FrameIndexOperand &StackObject = StackObjectInfo->second;
429 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000430 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000431 {
432 raw_string_ostream StrOS(Object.DebugVar.Value);
433 DebugVar.Var->printAsOperand(StrOS, MST);
434 }
435 {
436 raw_string_ostream StrOS(Object.DebugExpr.Value);
437 DebugVar.Expr->printAsOperand(StrOS, MST);
438 }
439 {
440 raw_string_ostream StrOS(Object.DebugLoc.Value);
441 DebugVar.Loc->printAsOperand(StrOS, MST);
442 }
443 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000444}
445
Alex Lorenzab980492015-07-20 20:51:18 +0000446void MIRPrinter::convert(yaml::MachineFunction &MF,
447 const MachineConstantPool &ConstantPool) {
448 unsigned ID = 0;
449 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
Alex Lorenzab980492015-07-20 20:51:18 +0000450 std::string Str;
451 raw_string_ostream StrOS(Str);
Diana Picusd5a00b02017-08-02 11:09:30 +0000452 if (Constant.isMachineConstantPoolEntry()) {
453 Constant.Val.MachineCPVal->print(StrOS);
454 } else {
455 Constant.Val.ConstVal->printAsOperand(StrOS);
456 }
457
458 yaml::MachineConstantPoolValue YamlConstant;
Alex Lorenzab980492015-07-20 20:51:18 +0000459 YamlConstant.ID = ID++;
460 YamlConstant.Value = StrOS.str();
461 YamlConstant.Alignment = Constant.getAlignment();
Diana Picusd5a00b02017-08-02 11:09:30 +0000462 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry();
463
Alex Lorenzab980492015-07-20 20:51:18 +0000464 MF.Constants.push_back(YamlConstant);
465 }
466}
467
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000468void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000469 yaml::MachineJumpTable &YamlJTI,
470 const MachineJumpTableInfo &JTI) {
471 YamlJTI.Kind = JTI.getEntryKind();
472 unsigned ID = 0;
473 for (const auto &Table : JTI.getJumpTables()) {
474 std::string Str;
475 yaml::MachineJumpTable::Entry Entry;
476 Entry.ID = ID++;
477 for (const auto *MBB : Table.MBBs) {
478 raw_string_ostream StrOS(Str);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000479 StrOS << printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000480 Entry.Blocks.push_back(StrOS.str());
481 Str.clear();
482 }
483 YamlJTI.Entries.push_back(Entry);
484 }
485}
486
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000487void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
488 const auto *TRI = MF.getSubtarget().getRegisterInfo();
489 unsigned I = 0;
490 for (const uint32_t *Mask : TRI->getRegMasks())
491 RegisterMaskIds.insert(std::make_pair(Mask, I++));
492}
493
Matthias Braun89401142017-05-05 21:09:30 +0000494void llvm::guessSuccessors(const MachineBasicBlock &MBB,
495 SmallVectorImpl<MachineBasicBlock*> &Result,
496 bool &IsFallthrough) {
497 SmallPtrSet<MachineBasicBlock*,8> Seen;
498
499 for (const MachineInstr &MI : MBB) {
500 if (MI.isPHI())
501 continue;
502 for (const MachineOperand &MO : MI.operands()) {
503 if (!MO.isMBB())
504 continue;
505 MachineBasicBlock *Succ = MO.getMBB();
506 auto RP = Seen.insert(Succ);
507 if (RP.second)
508 Result.push_back(Succ);
509 }
510 }
511 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
512 IsFallthrough = I == MBB.end() || !I->isBarrier();
513}
514
515bool
516MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
517 if (MBB.succ_size() <= 1)
518 return true;
519 if (!MBB.hasSuccessorProbabilities())
520 return true;
521
522 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
523 MBB.Probs.end());
524 BranchProbability::normalizeProbabilities(Normalized.begin(),
525 Normalized.end());
526 SmallVector<BranchProbability,8> Equal(Normalized.size());
527 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
528
529 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
530}
531
532bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
533 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
534 bool GuessedFallthrough;
535 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
536 if (GuessedFallthrough) {
537 const MachineFunction &MF = *MBB.getParent();
538 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
539 if (NextI != MF.end()) {
540 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
541 if (!is_contained(GuessedSuccs, Next))
542 GuessedSuccs.push_back(Next);
543 }
544 }
545 if (GuessedSuccs.size() != MBB.succ_size())
546 return false;
547 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
548}
549
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000550void MIPrinter::print(const MachineBasicBlock &MBB) {
551 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
552 OS << "bb." << MBB.getNumber();
553 bool HasAttributes = false;
554 if (const auto *BB = MBB.getBasicBlock()) {
555 if (BB->hasName()) {
556 OS << "." << BB->getName();
557 } else {
558 HasAttributes = true;
559 OS << " (";
560 int Slot = MST.getLocalSlot(BB);
561 if (Slot == -1)
562 OS << "<ir-block badref>";
563 else
564 OS << (Twine("%ir-block.") + Twine(Slot)).str();
565 }
566 }
567 if (MBB.hasAddressTaken()) {
568 OS << (HasAttributes ? ", " : " (");
569 OS << "address-taken";
570 HasAttributes = true;
571 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000572 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000573 OS << (HasAttributes ? ", " : " (");
574 OS << "landing-pad";
575 HasAttributes = true;
576 }
577 if (MBB.getAlignment()) {
578 OS << (HasAttributes ? ", " : " (");
579 OS << "align " << MBB.getAlignment();
580 HasAttributes = true;
581 }
582 if (HasAttributes)
583 OS << ")";
584 OS << ":\n";
585
586 bool HasLineAttributes = false;
587 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000588 bool canPredictProbs = canPredictBranchProbabilities(MBB);
Quentin Colombetd652aeb2017-09-19 23:34:12 +0000589 // Even if the list of successors is empty, if we cannot guess it,
590 // we need to print it to tell the parser that the list is empty.
591 // This is needed, because MI model unreachable as empty blocks
592 // with an empty successor list. If the parser would see that
593 // without the successor list, it would guess the code would
594 // fallthrough.
595 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs ||
596 !canPredictSuccessors(MBB)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000597 OS.indent(2) << "successors: ";
598 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
599 if (I != MBB.succ_begin())
600 OS << ", ";
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000601 OS << printMBBReference(**I);
Matthias Braun89401142017-05-05 21:09:30 +0000602 if (!SimplifyMIR || !canPredictProbs)
Geoff Berryb51774a2016-11-18 19:37:24 +0000603 OS << '('
604 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
605 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000606 }
607 OS << "\n";
608 HasLineAttributes = true;
609 }
610
611 // Print the live in registers.
Matthias Braun11723322017-01-05 20:01:19 +0000612 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
613 if (MRI.tracksLiveness() && !MBB.livein_empty()) {
614 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000615 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000616 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000617 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000618 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000619 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000620 First = false;
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000621 OS << printReg(LI.PhysReg, &TRI);
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000622 if (!LI.LaneMask.all())
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000623 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000624 }
625 OS << "\n";
626 HasLineAttributes = true;
627 }
628
629 if (HasLineAttributes)
630 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000631 bool IsInBundle = false;
632 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
633 const MachineInstr &MI = *I;
634 if (IsInBundle && !MI.isInsideBundle()) {
635 OS.indent(2) << "}\n";
636 IsInBundle = false;
637 }
638 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000639 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000640 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
641 OS << " {";
642 IsInBundle = true;
643 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000644 OS << "\n";
645 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000646 if (IsInBundle)
647 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000648}
649
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000650void MIPrinter::print(const MachineInstr &MI) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000651 const auto *MF = MI.getMF();
Quentin Colombet4e14a492016-03-07 21:57:52 +0000652 const auto &MRI = MF->getRegInfo();
653 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000654 const auto *TRI = SubTarget.getRegisterInfo();
655 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000656 const auto *TII = SubTarget.getInstrInfo();
657 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000658 if (MI.isCFIInstruction())
659 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000660
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000661 SmallBitVector PrintedTypes(8);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000662 bool ShouldPrintRegisterTies = MI.hasComplexRegisterTies();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000663 unsigned I = 0, E = MI.getNumOperands();
664 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
665 !MI.getOperand(I).isImplicit();
666 ++I) {
667 if (I)
668 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000669 print(MI, I, TRI, ShouldPrintRegisterTies,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000670 MI.getTypeToPrint(I, PrintedTypes, MRI),
671 /*PrintDef=*/false);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000672 }
673
674 if (I)
675 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000676 if (MI.getFlag(MachineInstr::FrameSetup))
677 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000678 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000679 if (I < E)
680 OS << ' ';
681
682 bool NeedComma = false;
683 for (; I < E; ++I) {
684 if (NeedComma)
685 OS << ", ";
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000686 print(MI, I, TRI, ShouldPrintRegisterTies,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000687 MI.getTypeToPrint(I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000688 NeedComma = true;
689 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000690
691 if (MI.getDebugLoc()) {
692 if (NeedComma)
693 OS << ',';
694 OS << " debug-location ";
695 MI.getDebugLoc()->printAsOperand(OS, MST);
696 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000697
698 if (!MI.memoperands_empty()) {
699 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000700 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000701 bool NeedComma = false;
702 for (const auto *Op : MI.memoperands()) {
703 if (NeedComma)
704 OS << ", ";
Geoff Berry6748abe2017-07-13 02:28:54 +0000705 print(Context, *TII, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000706 NeedComma = true;
707 }
708 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000709}
710
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000711static void printIRSlotNumber(raw_ostream &OS, int Slot) {
712 if (Slot == -1)
713 OS << "<badref>";
714 else
715 OS << Slot;
716}
717
Alex Lorenzdeb53492015-07-28 17:28:03 +0000718void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
719 OS << "%ir-block.";
720 if (BB.hasName()) {
721 printLLVMNameWithoutPrefix(OS, BB.getName());
722 return;
723 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000724 const Function *F = BB.getParent();
725 int Slot;
726 if (F == MST.getCurrentFunction()) {
727 Slot = MST.getLocalSlot(&BB);
728 } else {
729 ModuleSlotTracker CustomMST(F->getParent(),
730 /*ShouldInitializeAllMetadata=*/false);
731 CustomMST.incorporateFunction(*F);
732 Slot = CustomMST.getLocalSlot(&BB);
733 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000734 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000735}
736
Alex Lorenz4af7e612015-08-03 23:08:19 +0000737void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000738 if (isa<GlobalValue>(V)) {
739 V.printAsOperand(OS, /*PrintType=*/false, MST);
740 return;
741 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000742 if (isa<Constant>(V)) {
743 // Machine memory operands can load/store to/from constant value pointers.
744 OS << '`';
745 V.printAsOperand(OS, /*PrintType=*/true, MST);
746 OS << '`';
747 return;
748 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000749 OS << "%ir.";
750 if (V.hasName()) {
751 printLLVMNameWithoutPrefix(OS, V.getName());
752 return;
753 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000754 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000755}
756
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000757void MIPrinter::printStackObjectReference(int FrameIndex) {
758 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
759 assert(ObjectInfo != StackObjectOperandMapping.end() &&
760 "Invalid frame index");
761 const FrameIndexOperand &Operand = ObjectInfo->second;
762 if (Operand.IsFixed) {
763 OS << "%fixed-stack." << Operand.ID;
764 return;
765 }
766 OS << "%stack." << Operand.ID;
767 if (!Operand.Name.empty())
768 OS << '.' << Operand.Name;
769}
770
Alex Lorenz5672a892015-08-05 22:26:15 +0000771void MIPrinter::printOffset(int64_t Offset) {
772 if (Offset == 0)
773 return;
774 if (Offset < 0) {
775 OS << " - " << -Offset;
776 return;
777 }
778 OS << " + " << Offset;
779}
780
Alex Lorenz49873a82015-08-06 00:44:07 +0000781static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
782 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
783 for (const auto &I : Flags) {
784 if (I.first == TF) {
785 return I.second;
786 }
787 }
788 return nullptr;
789}
790
791void MIPrinter::printTargetFlags(const MachineOperand &Op) {
792 if (!Op.getTargetFlags())
793 return;
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000794 const auto *TII = Op.getParent()->getMF()->getSubtarget().getInstrInfo();
Alex Lorenz49873a82015-08-06 00:44:07 +0000795 assert(TII && "expected instruction info");
796 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
797 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000798 const bool HasDirectFlags = Flags.first;
799 const bool HasBitmaskFlags = Flags.second;
800 if (!HasDirectFlags && !HasBitmaskFlags) {
801 OS << "<unknown>) ";
802 return;
803 }
804 if (HasDirectFlags) {
805 if (const auto *Name = getTargetFlagName(TII, Flags.first))
806 OS << Name;
807 else
808 OS << "<unknown target flag>";
809 }
810 if (!HasBitmaskFlags) {
811 OS << ") ";
812 return;
813 }
814 bool IsCommaNeeded = HasDirectFlags;
815 unsigned BitMask = Flags.second;
816 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
817 for (const auto &Mask : BitMasks) {
818 // Check if the flag's bitmask has the bits of the current mask set.
819 if ((BitMask & Mask.first) == Mask.first) {
820 if (IsCommaNeeded)
821 OS << ", ";
822 IsCommaNeeded = true;
823 OS << Mask.second;
824 // Clear the bits which were serialized from the flag's bitmask.
825 BitMask &= ~(Mask.first);
826 }
827 }
828 if (BitMask) {
829 // When the resulting flag's bitmask isn't zero, we know that we didn't
830 // serialize all of the bit flags.
831 if (IsCommaNeeded)
832 OS << ", ";
833 OS << "<unknown bitmask target flag>";
834 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000835 OS << ") ";
836}
837
Alex Lorenzef5c1962015-07-28 23:02:45 +0000838static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
839 const auto *TII = MF.getSubtarget().getInstrInfo();
840 assert(TII && "expected instruction info");
841 auto Indices = TII->getSerializableTargetIndices();
842 for (const auto &I : Indices) {
843 if (I.first == Index) {
844 return I.second;
845 }
846 }
847 return nullptr;
848}
849
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000850void MIPrinter::print(const MachineInstr &MI, unsigned OpIdx,
851 const TargetRegisterInfo *TRI,
852 bool ShouldPrintRegisterTies, LLT TypeToPrint,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000853 bool PrintDef) {
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000854 const MachineOperand &Op = MI.getOperand(OpIdx);
Alex Lorenz49873a82015-08-06 00:44:07 +0000855 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000856 switch (Op.getType()) {
Justin Bogner6c452832017-10-24 18:04:54 +0000857 case MachineOperand::MO_Register: {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000858 unsigned TiedOperandIdx = 0;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000859 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000860 TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
861 const TargetIntrinsicInfo *TII = MI.getMF()->getTarget().getIntrinsicInfo();
862 Op.print(OS, MST, TypeToPrint, PrintDef, ShouldPrintRegisterTies,
863 TiedOperandIdx, TRI, TII);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000864 break;
Justin Bogner6c452832017-10-24 18:04:54 +0000865 }
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000866 case MachineOperand::MO_Immediate:
Bjorn Petterssona42ed3e2017-11-06 21:46:06 +0000867 if (MI.isOperandSubregIdx(OpIdx))
868 OS << "%subreg." << TRI->getSubRegIndexName(Op.getImm());
869 else
870 OS << Op.getImm();
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000871 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000872 case MachineOperand::MO_CImmediate:
873 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
874 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000875 case MachineOperand::MO_FPImmediate:
876 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
877 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000878 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000879 OS << printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000880 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000881 case MachineOperand::MO_FrameIndex:
882 printStackObjectReference(Op.getIndex());
883 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000884 case MachineOperand::MO_ConstantPoolIndex:
885 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000886 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000887 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000888 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000889 OS << "target-index(";
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000890 if (const auto *Name =
891 getTargetIndexName(*Op.getParent()->getMF(), Op.getIndex()))
Alex Lorenzef5c1962015-07-28 23:02:45 +0000892 OS << Name;
893 else
894 OS << "<unknown>";
895 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000896 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000897 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000898 case MachineOperand::MO_JumpTableIndex:
899 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000900 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000901 case MachineOperand::MO_ExternalSymbol: {
902 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000903 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000904 if (Name.empty()) {
905 OS << "\"\"";
906 } else {
907 printLLVMNameWithoutPrefix(OS, Name);
908 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000909 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000910 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000911 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000912 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000913 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000914 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000915 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000916 case MachineOperand::MO_BlockAddress:
917 OS << "blockaddress(";
918 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
919 MST);
920 OS << ", ";
921 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
922 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000923 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000924 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000925 case MachineOperand::MO_RegisterMask: {
926 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
927 if (RegMaskInfo != RegisterMaskIds.end())
928 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
929 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000930 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000931 break;
932 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000933 case MachineOperand::MO_RegisterLiveOut: {
934 const uint32_t *RegMask = Op.getRegLiveOut();
935 OS << "liveout(";
936 bool IsCommaNeeded = false;
937 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
938 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
939 if (IsCommaNeeded)
940 OS << ", ";
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +0000941 OS << printReg(Reg, TRI);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000942 IsCommaNeeded = true;
943 }
944 }
945 OS << ")";
946 break;
947 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000948 case MachineOperand::MO_Metadata:
949 Op.getMetadata()->printAsOperand(OS, MST);
950 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000951 case MachineOperand::MO_MCSymbol:
952 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
953 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000954 case MachineOperand::MO_CFIIndex: {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000955 const MachineFunction &MF = *Op.getParent()->getMF();
Matthias Braunf23ef432016-11-30 23:48:42 +0000956 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000957 break;
958 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000959 case MachineOperand::MO_IntrinsicID: {
960 Intrinsic::ID ID = Op.getIntrinsicID();
961 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000962 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000963 else {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000964 const MachineFunction &MF = *Op.getParent()->getMF();
Tim Northover6b3bd612016-07-29 20:32:59 +0000965 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
966 OS << "intrinsic(@" << TII->getName(ID) << ')';
967 }
968 break;
969 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000970 case MachineOperand::MO_Predicate: {
971 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
972 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
973 << CmpInst::getPredicateName(Pred) << ')';
974 break;
975 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000976 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000977}
978
Geoff Berry6748abe2017-07-13 02:28:54 +0000979static const char *getTargetMMOFlagName(const TargetInstrInfo &TII,
980 unsigned TMMOFlag) {
981 auto Flags = TII.getSerializableMachineMemOperandTargetFlags();
982 for (const auto &I : Flags) {
983 if (I.first == TMMOFlag) {
984 return I.second;
985 }
986 }
987 return nullptr;
988}
989
990void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII,
991 const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000992 OS << '(';
Alex Lorenza518b792015-08-04 00:24:45 +0000993 if (Op.isVolatile())
994 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000995 if (Op.isNonTemporal())
996 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000997 if (Op.isDereferenceable())
998 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000999 if (Op.isInvariant())
1000 OS << "invariant ";
Geoff Berry6748abe2017-07-13 02:28:54 +00001001 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1)
1002 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1)
1003 << "\" ";
1004 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2)
1005 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2)
1006 << "\" ";
1007 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3)
1008 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3)
1009 << "\" ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001010
1011 assert((Op.isLoad() || Op.isStore()) && "machine memory operand must be a load or store (or both)");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001012 if (Op.isLoad())
1013 OS << "load ";
Daniel Sanders17d277b2017-11-28 18:57:02 +00001014 if (Op.isStore())
Alex Lorenz4af7e612015-08-03 23:08:19 +00001015 OS << "store ";
Tim Northoverb73e3092017-02-13 22:14:08 +00001016
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001017 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001018
1019 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1020 OS << toIRString(Op.getOrdering()) << ' ';
1021 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1022 OS << toIRString(Op.getFailureOrdering()) << ' ';
1023
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001024 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001025 if (const Value *Val = Op.getValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001026 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1027 : Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001028 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001029 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00001030 OS << ((Op.isLoad() && Op.isStore()) ? " on "
1031 : Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001032 assert(PVal && "Expected a pseudo source value");
1033 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001034 case PseudoSourceValue::Stack:
1035 OS << "stack";
1036 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001037 case PseudoSourceValue::GOT:
1038 OS << "got";
1039 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001040 case PseudoSourceValue::JumpTable:
1041 OS << "jump-table";
1042 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001043 case PseudoSourceValue::ConstantPool:
1044 OS << "constant-pool";
1045 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001046 case PseudoSourceValue::FixedStack:
1047 printStackObjectReference(
1048 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1049 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001050 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001051 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001052 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1053 OS, /*PrintType=*/false, MST);
1054 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001055 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001056 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001057 printLLVMNameWithoutPrefix(
1058 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001059 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001060 case PseudoSourceValue::TargetCustom:
1061 llvm_unreachable("TargetCustom pseudo source values are not supported");
1062 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001063 }
1064 }
Alex Lorenz83127732015-08-07 20:26:52 +00001065 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001066 if (Op.getBaseAlignment() != Op.getSize())
1067 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001068 auto AAInfo = Op.getAAInfo();
1069 if (AAInfo.TBAA) {
1070 OS << ", !tbaa ";
1071 AAInfo.TBAA->printAsOperand(OS, MST);
1072 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001073 if (AAInfo.Scope) {
1074 OS << ", !alias.scope ";
1075 AAInfo.Scope->printAsOperand(OS, MST);
1076 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001077 if (AAInfo.NoAlias) {
1078 OS << ", !noalias ";
1079 AAInfo.NoAlias->printAsOperand(OS, MST);
1080 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001081 if (Op.getRanges()) {
1082 OS << ", !range ";
1083 Op.getRanges()->printAsOperand(OS, MST);
1084 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001085 OS << ')';
1086}
1087
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001088void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1089 switch (SSID) {
1090 case SyncScope::System: {
1091 break;
1092 }
1093 default: {
1094 if (SSNs.empty())
1095 Context.getSyncScopeNames(SSNs);
1096
1097 OS << "syncscope(\"";
1098 PrintEscapedString(SSNs[SSID], OS);
1099 OS << "\") ";
1100 break;
1101 }
1102 }
1103}
1104
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001105static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1106 const TargetRegisterInfo *TRI) {
1107 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1108 if (Reg == -1) {
1109 OS << "<badreg>";
1110 return;
1111 }
Francis Visoiu Mistrihc71cced2017-11-30 16:12:24 +00001112 OS << printReg(Reg, TRI);
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001113}
1114
1115void MIPrinter::print(const MCCFIInstruction &CFI,
1116 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001117 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001118 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001119 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001120 if (CFI.getLabel())
1121 OS << "<mcsymbol> ";
1122 printCFIRegister(CFI.getRegister(), OS, TRI);
1123 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001124 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001125 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001126 if (CFI.getLabel())
1127 OS << "<mcsymbol> ";
1128 printCFIRegister(CFI.getRegister(), OS, TRI);
1129 OS << ", " << CFI.getOffset();
1130 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001131 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001132 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001133 if (CFI.getLabel())
1134 OS << "<mcsymbol> ";
1135 printCFIRegister(CFI.getRegister(), OS, TRI);
1136 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001137 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001138 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001139 if (CFI.getLabel())
1140 OS << "<mcsymbol> ";
1141 OS << CFI.getOffset();
1142 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001143 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001144 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001145 if (CFI.getLabel())
1146 OS << "<mcsymbol> ";
1147 printCFIRegister(CFI.getRegister(), OS, TRI);
1148 OS << ", " << CFI.getOffset();
1149 break;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001150 case MCCFIInstruction::OpRestore:
1151 OS << "restore ";
1152 if (CFI.getLabel())
1153 OS << "<mcsymbol> ";
1154 printCFIRegister(CFI.getRegister(), OS, TRI);
1155 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001156 default:
1157 // TODO: Print the other CFI Operations.
1158 OS << "<unserializable cfi operation>";
1159 break;
1160 }
1161}
1162
Alex Lorenz345c1442015-06-15 23:52:35 +00001163void llvm::printMIR(raw_ostream &OS, const Module &M) {
1164 yaml::Output Out(OS);
1165 Out << const_cast<Module &>(M);
1166}
1167
1168void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1169 MIRPrinter Printer(OS);
1170 Printer.print(MF);
1171}