blob: 4cc61420ab47a6c2b81eff15eaea54c241c04d07 [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the class that prints out the LLVM IR and machine
11// functions using the MIR serialization format.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenkofb69e662017-06-06 22:22:41 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/None.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000017#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/STLExtras.h"
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000021#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000022#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000024#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenzab980492015-07-20 20:51:18 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000028#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineJumpTableInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000032#include "llvm/CodeGen/MachineOperand.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000034#include "llvm/CodeGen/MIRPrinter.h"
35#include "llvm/CodeGen/MIRYamlMapping.h"
36#include "llvm/CodeGen/PseudoSourceValue.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000037#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000038#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000039#include "llvm/IR/DebugInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000040#include "llvm/IR/DebugLoc.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalValue.h"
43#include "llvm/IR/InstrTypes.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000044#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000045#include "llvm/IR/Intrinsics.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000046#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000047#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000048#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000049#include "llvm/IR/Value.h"
50#include "llvm/MC/LaneBitmask.h"
51#include "llvm/MC/MCDwarf.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000052#include "llvm/MC/MCSymbol.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000053#include "llvm/Support/AtomicOrdering.h"
54#include "llvm/Support/BranchProbability.h"
55#include "llvm/Support/Casting.h"
56#include "llvm/Support/CommandLine.h"
57#include "llvm/Support/ErrorHandling.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000058#include "llvm/Support/Format.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000059#include "llvm/Support/LowLevelTypeImpl.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000060#include "llvm/Support/raw_ostream.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000061#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000062#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000063#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000064#include "llvm/Target/TargetMachine.h"
65#include "llvm/Target/TargetRegisterInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000066#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000067#include <algorithm>
68#include <cassert>
69#include <cinttypes>
70#include <cstdint>
71#include <iterator>
72#include <string>
73#include <utility>
74#include <vector>
Alex Lorenz345c1442015-06-15 23:52:35 +000075
76using namespace llvm;
77
Matthias Braun89401142017-05-05 21:09:30 +000078static cl::opt<bool> SimplifyMIR("simplify-mir",
79 cl::desc("Leave out unnecessary information when printing MIR"));
80
Alex Lorenz345c1442015-06-15 23:52:35 +000081namespace {
82
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000083/// This structure describes how to print out stack object references.
84struct FrameIndexOperand {
85 std::string Name;
86 unsigned ID;
87 bool IsFixed;
88
89 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
90 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
91
92 /// Return an ordinary stack object reference.
93 static FrameIndexOperand create(StringRef Name, unsigned ID) {
94 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
95 }
96
97 /// Return a fixed stack object reference.
98 static FrameIndexOperand createFixed(unsigned ID) {
99 return FrameIndexOperand("", ID, /*IsFixed=*/true);
100 }
101};
102
Alex Lorenz618b2832015-07-30 16:54:38 +0000103} // end anonymous namespace
104
105namespace llvm {
106
Alex Lorenz345c1442015-06-15 23:52:35 +0000107/// This class prints out the machine functions using the MIR serialization
108/// format.
109class MIRPrinter {
110 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000111 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000112 /// Maps from stack object indices to operand indices which will be used when
113 /// printing frame index machine operands.
114 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +0000115
116public:
117 MIRPrinter(raw_ostream &OS) : OS(OS) {}
118
119 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000120
Alex Lorenz28148ba2015-07-09 22:23:13 +0000121 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
122 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000123 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
124 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +0000125 void convert(yaml::MachineFunction &MF,
126 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000127 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
128 const MachineJumpTableInfo &JTI);
Matthias Braunef331ef2016-11-30 23:48:50 +0000129 void convertStackObjects(yaml::MachineFunction &YMF,
130 const MachineFunction &MF, ModuleSlotTracker &MST);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000131
132private:
133 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000134};
135
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000136/// This class prints out the machine instructions using the MIR serialization
137/// format.
138class MIPrinter {
139 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000140 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000141 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000143 /// Synchronization scope names registered with LLVMContext.
144 SmallVector<StringRef, 8> SSNs;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000145
Matthias Braun89401142017-05-05 21:09:30 +0000146 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const;
147 bool canPredictSuccessors(const MachineBasicBlock &MBB) const;
148
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000149public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000150 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000151 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
152 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
153 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
154 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000155
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000156 void print(const MachineBasicBlock &MBB);
157
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000158 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000159 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000160 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000161 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000162 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000163 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000164 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000165 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000166 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000167 LLT TypeToPrint, bool IsDef = false);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000168 void print(const LLVMContext &Context, const MachineMemOperand &Op);
169 void printSyncScope(const LLVMContext &Context, SyncScope::ID SSID);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000170
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000171 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000172};
173
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000174} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000175
176namespace llvm {
177namespace yaml {
178
179/// This struct serializes the LLVM IR module.
180template <> struct BlockScalarTraits<Module> {
181 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
182 Mod.print(OS, nullptr);
183 }
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000184
Alex Lorenz345c1442015-06-15 23:52:35 +0000185 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
186 llvm_unreachable("LLVM Module is supposed to be parsed separately");
187 return "";
188 }
189};
190
191} // end namespace yaml
192} // end namespace llvm
193
Alex Lorenz15a00a82015-07-14 21:18:25 +0000194static void printReg(unsigned Reg, raw_ostream &OS,
195 const TargetRegisterInfo *TRI) {
196 // TODO: Print Stack Slots.
197 if (!Reg)
198 OS << '_';
199 else if (TargetRegisterInfo::isVirtualRegister(Reg))
200 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
201 else if (Reg < TRI->getNumRegs())
202 OS << '%' << StringRef(TRI->getName(Reg)).lower();
203 else
204 llvm_unreachable("Can't print this kind of register yet");
205}
206
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000207static void printReg(unsigned Reg, yaml::StringValue &Dest,
208 const TargetRegisterInfo *TRI) {
209 raw_string_ostream OS(Dest.Value);
210 printReg(Reg, OS, TRI);
211}
212
Alex Lorenz345c1442015-06-15 23:52:35 +0000213void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000214 initRegisterMaskIds(MF);
215
Alex Lorenz345c1442015-06-15 23:52:35 +0000216 yaml::MachineFunction YamlMF;
217 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000218 YamlMF.Alignment = MF.getAlignment();
219 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000220
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000221 YamlMF.Legalized = MF.getProperties().hasProperty(
222 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000223 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
224 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000225 YamlMF.Selected = MF.getProperties().hasProperty(
226 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000227
Alex Lorenz28148ba2015-07-09 22:23:13 +0000228 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000229 ModuleSlotTracker MST(MF.getFunction()->getParent());
230 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000231 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
Matthias Braunef331ef2016-11-30 23:48:50 +0000232 convertStackObjects(YamlMF, MF, MST);
Alex Lorenzab980492015-07-20 20:51:18 +0000233 if (const auto *ConstantPool = MF.getConstantPool())
234 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000235 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
236 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000237 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
238 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000239 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000240 if (IsNewlineNeeded)
241 StrOS << "\n";
242 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
243 .print(MBB);
244 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000245 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000246 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000247 yaml::Output Out(OS);
Vivek Pandya56d87ef2017-06-06 08:16:19 +0000248 if (!SimplifyMIR)
249 Out.setWriteDefaultValues(true);
Alex Lorenz345c1442015-06-15 23:52:35 +0000250 Out << YamlMF;
251}
252
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000253static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS,
254 const TargetRegisterInfo *TRI) {
255 assert(RegMask && "Can't print an empty register mask");
256 OS << StringRef("CustomRegMask(");
257
258 bool IsRegInRegMaskFound = false;
259 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) {
260 // Check whether the register is asserted in regmask.
261 if (RegMask[I / 32] & (1u << (I % 32))) {
262 if (IsRegInRegMaskFound)
263 OS << ',';
264 printReg(I, OS, TRI);
265 IsRegInRegMaskFound = true;
266 }
267 }
268
269 OS << ')';
270}
271
Alex Lorenz54565cf2015-06-24 19:56:10 +0000272void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000273 const MachineRegisterInfo &RegInfo,
274 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000275 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000276
277 // Print the virtual register definitions.
278 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
279 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
280 yaml::VirtualRegisterDefinition VReg;
281 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000282 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000283 VReg.Class =
284 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000285 else if (RegInfo.getRegBankOrNull(Reg))
286 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000287 else {
288 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000289 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000290 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000291 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000292 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
293 if (PreferredReg)
294 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000295 MF.VirtualRegisters.push_back(VReg);
296 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000297
298 // Print the live ins.
299 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
300 yaml::MachineFunctionLiveIn LiveIn;
301 printReg(I->first, LiveIn.Register, TRI);
302 if (I->second)
303 printReg(I->second, LiveIn.VirtualRegister, TRI);
304 MF.LiveIns.push_back(LiveIn);
305 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000306
307 // Prints the callee saved registers.
308 if (RegInfo.isUpdatedCSRsInitialized()) {
309 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs();
310 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
311 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) {
Alex Lorenzc4838082015-08-11 00:32:49 +0000312 yaml::FlowStringValue Reg;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000313 printReg(*I, Reg, TRI);
Alex Lorenzc4838082015-08-11 00:32:49 +0000314 CalleeSavedRegisters.push_back(Reg);
315 }
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000316 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenzc4838082015-08-11 00:32:49 +0000317 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000318}
319
Alex Lorenza6f9a372015-07-29 21:09:09 +0000320void MIRPrinter::convert(ModuleSlotTracker &MST,
321 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000322 const MachineFrameInfo &MFI) {
323 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
324 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
325 YamlMFI.HasStackMap = MFI.hasStackMap();
326 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
327 YamlMFI.StackSize = MFI.getStackSize();
328 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
329 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
330 YamlMFI.AdjustsStack = MFI.adjustsStack();
331 YamlMFI.HasCalls = MFI.hasCalls();
Matthias Braunab9438c2017-05-01 22:32:25 +0000332 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed()
333 ? MFI.getMaxCallFrameSize() : ~0u;
Alex Lorenz60541c12015-07-09 19:55:27 +0000334 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
335 YamlMFI.HasVAStart = MFI.hasVAStart();
336 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000337 if (MFI.getSavePoint()) {
338 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
339 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
340 .printMBBReference(*MFI.getSavePoint());
341 }
342 if (MFI.getRestorePoint()) {
343 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
344 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
345 .printMBBReference(*MFI.getRestorePoint());
346 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000347}
348
Matthias Braunef331ef2016-11-30 23:48:50 +0000349void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF,
350 const MachineFunction &MF,
351 ModuleSlotTracker &MST) {
352 const MachineFrameInfo &MFI = MF.getFrameInfo();
353 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Alex Lorenzde491f02015-07-13 18:07:26 +0000354 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000355 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000356 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
357 if (MFI.isDeadObjectIndex(I))
358 continue;
359
360 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000361 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000362 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
363 ? yaml::FixedMachineStackObject::SpillSlot
364 : yaml::FixedMachineStackObject::DefaultType;
365 YamlObject.Offset = MFI.getObjectOffset(I);
366 YamlObject.Size = MFI.getObjectSize(I);
367 YamlObject.Alignment = MFI.getObjectAlignment(I);
368 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);
394
Matthias Braunef331ef2016-11-30 23:48:50 +0000395 YMF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000396 StackObjectOperandMapping.insert(std::make_pair(
397 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000398 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000399
400 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
401 yaml::StringValue Reg;
402 printReg(CSInfo.getReg(), Reg, TRI);
403 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
404 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
405 "Invalid stack object index");
406 const FrameIndexOperand &StackObject = StackObjectInfo->second;
407 if (StackObject.IsFixed)
Matthias Braunef331ef2016-11-30 23:48:50 +0000408 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000409 else
Matthias Braunef331ef2016-11-30 23:48:50 +0000410 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000411 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000412 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
413 auto LocalObject = MFI.getLocalFrameObjectMap(I);
414 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
415 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
416 "Invalid stack object index");
417 const FrameIndexOperand &StackObject = StackObjectInfo->second;
418 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000419 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000420 }
Alex Lorenza314d812015-08-18 22:26:26 +0000421
422 // Print the stack object references in the frame information class after
423 // converting the stack objects.
424 if (MFI.hasStackProtectorIndex()) {
Matthias Braunef331ef2016-11-30 23:48:50 +0000425 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value);
Alex Lorenza314d812015-08-18 22:26:26 +0000426 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
427 .printStackObjectReference(MFI.getStackProtectorIndex());
428 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000429
430 // Print the debug variable information.
Matthias Braunef331ef2016-11-30 23:48:50 +0000431 for (const MachineFunction::VariableDbgInfo &DebugVar :
432 MF.getVariableDbgInfo()) {
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000433 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
434 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
435 "Invalid stack object index");
436 const FrameIndexOperand &StackObject = StackObjectInfo->second;
437 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
Matthias Braunef331ef2016-11-30 23:48:50 +0000438 auto &Object = YMF.StackObjects[StackObject.ID];
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000439 {
440 raw_string_ostream StrOS(Object.DebugVar.Value);
441 DebugVar.Var->printAsOperand(StrOS, MST);
442 }
443 {
444 raw_string_ostream StrOS(Object.DebugExpr.Value);
445 DebugVar.Expr->printAsOperand(StrOS, MST);
446 }
447 {
448 raw_string_ostream StrOS(Object.DebugLoc.Value);
449 DebugVar.Loc->printAsOperand(StrOS, MST);
450 }
451 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000452}
453
Alex Lorenzab980492015-07-20 20:51:18 +0000454void MIRPrinter::convert(yaml::MachineFunction &MF,
455 const MachineConstantPool &ConstantPool) {
456 unsigned ID = 0;
457 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
458 // TODO: Serialize target specific constant pool entries.
459 if (Constant.isMachineConstantPoolEntry())
460 llvm_unreachable("Can't print target specific constant pool entries yet");
461
462 yaml::MachineConstantPoolValue YamlConstant;
463 std::string Str;
464 raw_string_ostream StrOS(Str);
465 Constant.Val.ConstVal->printAsOperand(StrOS);
466 YamlConstant.ID = ID++;
467 YamlConstant.Value = StrOS.str();
468 YamlConstant.Alignment = Constant.getAlignment();
469 MF.Constants.push_back(YamlConstant);
470 }
471}
472
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000473void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000474 yaml::MachineJumpTable &YamlJTI,
475 const MachineJumpTableInfo &JTI) {
476 YamlJTI.Kind = JTI.getEntryKind();
477 unsigned ID = 0;
478 for (const auto &Table : JTI.getJumpTables()) {
479 std::string Str;
480 yaml::MachineJumpTable::Entry Entry;
481 Entry.ID = ID++;
482 for (const auto *MBB : Table.MBBs) {
483 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000484 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
485 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000486 Entry.Blocks.push_back(StrOS.str());
487 Str.clear();
488 }
489 YamlJTI.Entries.push_back(Entry);
490 }
491}
492
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000493void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
494 const auto *TRI = MF.getSubtarget().getRegisterInfo();
495 unsigned I = 0;
496 for (const uint32_t *Mask : TRI->getRegMasks())
497 RegisterMaskIds.insert(std::make_pair(Mask, I++));
498}
499
Matthias Braun89401142017-05-05 21:09:30 +0000500void llvm::guessSuccessors(const MachineBasicBlock &MBB,
501 SmallVectorImpl<MachineBasicBlock*> &Result,
502 bool &IsFallthrough) {
503 SmallPtrSet<MachineBasicBlock*,8> Seen;
504
505 for (const MachineInstr &MI : MBB) {
506 if (MI.isPHI())
507 continue;
508 for (const MachineOperand &MO : MI.operands()) {
509 if (!MO.isMBB())
510 continue;
511 MachineBasicBlock *Succ = MO.getMBB();
512 auto RP = Seen.insert(Succ);
513 if (RP.second)
514 Result.push_back(Succ);
515 }
516 }
517 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
518 IsFallthrough = I == MBB.end() || !I->isBarrier();
519}
520
521bool
522MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const {
523 if (MBB.succ_size() <= 1)
524 return true;
525 if (!MBB.hasSuccessorProbabilities())
526 return true;
527
528 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(),
529 MBB.Probs.end());
530 BranchProbability::normalizeProbabilities(Normalized.begin(),
531 Normalized.end());
532 SmallVector<BranchProbability,8> Equal(Normalized.size());
533 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end());
534
535 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin());
536}
537
538bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const {
539 SmallVector<MachineBasicBlock*,8> GuessedSuccs;
540 bool GuessedFallthrough;
541 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough);
542 if (GuessedFallthrough) {
543 const MachineFunction &MF = *MBB.getParent();
544 MachineFunction::const_iterator NextI = std::next(MBB.getIterator());
545 if (NextI != MF.end()) {
546 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI);
547 if (!is_contained(GuessedSuccs, Next))
548 GuessedSuccs.push_back(Next);
549 }
550 }
551 if (GuessedSuccs.size() != MBB.succ_size())
552 return false;
553 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin());
554}
555
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000556void MIPrinter::print(const MachineBasicBlock &MBB) {
557 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
558 OS << "bb." << MBB.getNumber();
559 bool HasAttributes = false;
560 if (const auto *BB = MBB.getBasicBlock()) {
561 if (BB->hasName()) {
562 OS << "." << BB->getName();
563 } else {
564 HasAttributes = true;
565 OS << " (";
566 int Slot = MST.getLocalSlot(BB);
567 if (Slot == -1)
568 OS << "<ir-block badref>";
569 else
570 OS << (Twine("%ir-block.") + Twine(Slot)).str();
571 }
572 }
573 if (MBB.hasAddressTaken()) {
574 OS << (HasAttributes ? ", " : " (");
575 OS << "address-taken";
576 HasAttributes = true;
577 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000578 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000579 OS << (HasAttributes ? ", " : " (");
580 OS << "landing-pad";
581 HasAttributes = true;
582 }
583 if (MBB.getAlignment()) {
584 OS << (HasAttributes ? ", " : " (");
585 OS << "align " << MBB.getAlignment();
586 HasAttributes = true;
587 }
588 if (HasAttributes)
589 OS << ")";
590 OS << ":\n";
591
592 bool HasLineAttributes = false;
593 // Print the successors
Matthias Braun89401142017-05-05 21:09:30 +0000594 bool canPredictProbs = canPredictBranchProbabilities(MBB);
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 << ", ";
601 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;
Matthias Braun11723322017-01-05 20:01:19 +0000621 printReg(LI.PhysReg, OS, &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 Lorenz5ef93b02015-08-19 19:05:34 +0000650/// Return true when an instruction has tied register that can't be determined
651/// by the instruction's descriptor.
652static bool hasComplexRegisterTies(const MachineInstr &MI) {
653 const MCInstrDesc &MCID = MI.getDesc();
654 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
655 const auto &Operand = MI.getOperand(I);
656 if (!Operand.isReg() || Operand.isDef())
657 // Ignore the defined registers as MCID marks only the uses as tied.
658 continue;
659 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
660 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
661 if (ExpectedTiedIdx != TiedIdx)
662 return true;
663 }
664 return false;
665}
666
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000667static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
668 SmallBitVector &PrintedTypes,
669 const MachineRegisterInfo &MRI) {
670 const MachineOperand &Op = MI.getOperand(OpIdx);
671 if (!Op.isReg())
672 return LLT{};
673
674 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
675 return MRI.getType(Op.getReg());
676
677 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
678 if (!OpInfo.isGenericType())
679 return MRI.getType(Op.getReg());
680
681 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
682 return LLT{};
683
684 PrintedTypes.set(OpInfo.getGenericTypeIndex());
685 return MRI.getType(Op.getReg());
686}
687
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000688void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000689 const auto *MF = MI.getParent()->getParent();
690 const auto &MRI = MF->getRegInfo();
691 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000692 const auto *TRI = SubTarget.getRegisterInfo();
693 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000694 const auto *TII = SubTarget.getInstrInfo();
695 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000696 if (MI.isCFIInstruction())
697 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000698
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000699 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000700 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000701 unsigned I = 0, E = MI.getNumOperands();
702 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
703 !MI.getOperand(I).isImplicit();
704 ++I) {
705 if (I)
706 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000707 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
708 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000709 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000710 }
711
712 if (I)
713 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000714 if (MI.getFlag(MachineInstr::FrameSetup))
715 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000716 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000717 if (I < E)
718 OS << ' ';
719
720 bool NeedComma = false;
721 for (; I < E; ++I) {
722 if (NeedComma)
723 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000724 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
725 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000726 NeedComma = true;
727 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000728
729 if (MI.getDebugLoc()) {
730 if (NeedComma)
731 OS << ',';
732 OS << " debug-location ";
733 MI.getDebugLoc()->printAsOperand(OS, MST);
734 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000735
736 if (!MI.memoperands_empty()) {
737 OS << " :: ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000738 const LLVMContext &Context = MF->getFunction()->getContext();
Alex Lorenz4af7e612015-08-03 23:08:19 +0000739 bool NeedComma = false;
740 for (const auto *Op : MI.memoperands()) {
741 if (NeedComma)
742 OS << ", ";
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000743 print(Context, *Op);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000744 NeedComma = true;
745 }
746 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000747}
748
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000749void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
750 OS << "%bb." << MBB.getNumber();
751 if (const auto *BB = MBB.getBasicBlock()) {
752 if (BB->hasName())
753 OS << '.' << BB->getName();
754 }
755}
756
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000757static void printIRSlotNumber(raw_ostream &OS, int Slot) {
758 if (Slot == -1)
759 OS << "<badref>";
760 else
761 OS << Slot;
762}
763
Alex Lorenzdeb53492015-07-28 17:28:03 +0000764void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
765 OS << "%ir-block.";
766 if (BB.hasName()) {
767 printLLVMNameWithoutPrefix(OS, BB.getName());
768 return;
769 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000770 const Function *F = BB.getParent();
771 int Slot;
772 if (F == MST.getCurrentFunction()) {
773 Slot = MST.getLocalSlot(&BB);
774 } else {
775 ModuleSlotTracker CustomMST(F->getParent(),
776 /*ShouldInitializeAllMetadata=*/false);
777 CustomMST.incorporateFunction(*F);
778 Slot = CustomMST.getLocalSlot(&BB);
779 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000780 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000781}
782
Alex Lorenz4af7e612015-08-03 23:08:19 +0000783void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000784 if (isa<GlobalValue>(V)) {
785 V.printAsOperand(OS, /*PrintType=*/false, MST);
786 return;
787 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000788 if (isa<Constant>(V)) {
789 // Machine memory operands can load/store to/from constant value pointers.
790 OS << '`';
791 V.printAsOperand(OS, /*PrintType=*/true, MST);
792 OS << '`';
793 return;
794 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000795 OS << "%ir.";
796 if (V.hasName()) {
797 printLLVMNameWithoutPrefix(OS, V.getName());
798 return;
799 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000800 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000801}
802
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000803void MIPrinter::printStackObjectReference(int FrameIndex) {
804 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
805 assert(ObjectInfo != StackObjectOperandMapping.end() &&
806 "Invalid frame index");
807 const FrameIndexOperand &Operand = ObjectInfo->second;
808 if (Operand.IsFixed) {
809 OS << "%fixed-stack." << Operand.ID;
810 return;
811 }
812 OS << "%stack." << Operand.ID;
813 if (!Operand.Name.empty())
814 OS << '.' << Operand.Name;
815}
816
Alex Lorenz5672a892015-08-05 22:26:15 +0000817void MIPrinter::printOffset(int64_t Offset) {
818 if (Offset == 0)
819 return;
820 if (Offset < 0) {
821 OS << " - " << -Offset;
822 return;
823 }
824 OS << " + " << Offset;
825}
826
Alex Lorenz49873a82015-08-06 00:44:07 +0000827static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
828 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
829 for (const auto &I : Flags) {
830 if (I.first == TF) {
831 return I.second;
832 }
833 }
834 return nullptr;
835}
836
837void MIPrinter::printTargetFlags(const MachineOperand &Op) {
838 if (!Op.getTargetFlags())
839 return;
840 const auto *TII =
841 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
842 assert(TII && "expected instruction info");
843 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
844 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000845 const bool HasDirectFlags = Flags.first;
846 const bool HasBitmaskFlags = Flags.second;
847 if (!HasDirectFlags && !HasBitmaskFlags) {
848 OS << "<unknown>) ";
849 return;
850 }
851 if (HasDirectFlags) {
852 if (const auto *Name = getTargetFlagName(TII, Flags.first))
853 OS << Name;
854 else
855 OS << "<unknown target flag>";
856 }
857 if (!HasBitmaskFlags) {
858 OS << ") ";
859 return;
860 }
861 bool IsCommaNeeded = HasDirectFlags;
862 unsigned BitMask = Flags.second;
863 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
864 for (const auto &Mask : BitMasks) {
865 // Check if the flag's bitmask has the bits of the current mask set.
866 if ((BitMask & Mask.first) == Mask.first) {
867 if (IsCommaNeeded)
868 OS << ", ";
869 IsCommaNeeded = true;
870 OS << Mask.second;
871 // Clear the bits which were serialized from the flag's bitmask.
872 BitMask &= ~(Mask.first);
873 }
874 }
875 if (BitMask) {
876 // When the resulting flag's bitmask isn't zero, we know that we didn't
877 // serialize all of the bit flags.
878 if (IsCommaNeeded)
879 OS << ", ";
880 OS << "<unknown bitmask target flag>";
881 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000882 OS << ") ";
883}
884
Alex Lorenzef5c1962015-07-28 23:02:45 +0000885static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
886 const auto *TII = MF.getSubtarget().getInstrInfo();
887 assert(TII && "expected instruction info");
888 auto Indices = TII->getSerializableTargetIndices();
889 for (const auto &I : Indices) {
890 if (I.first == Index) {
891 return I.second;
892 }
893 }
894 return nullptr;
895}
896
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000897void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000898 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
899 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000900 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000901 switch (Op.getType()) {
902 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000903 if (Op.isImplicit())
904 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000905 else if (!IsDef && Op.isDef())
906 // Print the 'def' flag only when the operand is defined after '='.
907 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000908 if (Op.isInternalRead())
909 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000910 if (Op.isDead())
911 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000912 if (Op.isKill())
913 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000914 if (Op.isUndef())
915 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000916 if (Op.isEarlyClobber())
917 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000918 if (Op.isDebug())
919 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000920 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000921 // Print the sub register.
922 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000923 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000924 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
925 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000926 if (TypeToPrint.isValid())
927 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000928 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000929 case MachineOperand::MO_Immediate:
930 OS << Op.getImm();
931 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000932 case MachineOperand::MO_CImmediate:
933 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
934 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000935 case MachineOperand::MO_FPImmediate:
936 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
937 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000938 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000939 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000940 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000941 case MachineOperand::MO_FrameIndex:
942 printStackObjectReference(Op.getIndex());
943 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000944 case MachineOperand::MO_ConstantPoolIndex:
945 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000946 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000947 break;
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000948 case MachineOperand::MO_TargetIndex:
Alex Lorenzef5c1962015-07-28 23:02:45 +0000949 OS << "target-index(";
950 if (const auto *Name = getTargetIndexName(
951 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
952 OS << Name;
953 else
954 OS << "<unknown>";
955 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000956 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000957 break;
Alex Lorenz31d70682015-07-15 23:38:35 +0000958 case MachineOperand::MO_JumpTableIndex:
959 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000960 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000961 case MachineOperand::MO_ExternalSymbol: {
962 StringRef Name = Op.getSymbolName();
Alex Lorenz6ede3742015-07-21 16:59:53 +0000963 OS << '$';
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000964 if (Name.empty()) {
965 OS << "\"\"";
966 } else {
967 printLLVMNameWithoutPrefix(OS, Name);
968 }
Alex Lorenz5672a892015-08-05 22:26:15 +0000969 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000970 break;
Matthias Braun8b5f9e42017-06-06 19:00:58 +0000971 }
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000972 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000973 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000974 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000975 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000976 case MachineOperand::MO_BlockAddress:
977 OS << "blockaddress(";
978 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
979 MST);
980 OS << ", ";
981 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
982 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000983 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000984 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000985 case MachineOperand::MO_RegisterMask: {
986 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
987 if (RegMaskInfo != RegisterMaskIds.end())
988 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
989 else
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000990 printCustomRegMask(Op.getRegMask(), OS, TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000991 break;
992 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000993 case MachineOperand::MO_RegisterLiveOut: {
994 const uint32_t *RegMask = Op.getRegLiveOut();
995 OS << "liveout(";
996 bool IsCommaNeeded = false;
997 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
998 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
999 if (IsCommaNeeded)
1000 OS << ", ";
1001 printReg(Reg, OS, TRI);
1002 IsCommaNeeded = true;
1003 }
1004 }
1005 OS << ")";
1006 break;
1007 }
Alex Lorenz35e44462015-07-22 17:58:46 +00001008 case MachineOperand::MO_Metadata:
1009 Op.getMetadata()->printAsOperand(OS, MST);
1010 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00001011 case MachineOperand::MO_MCSymbol:
1012 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
1013 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001014 case MachineOperand::MO_CFIIndex: {
Matthias Braunf23ef432016-11-30 23:48:42 +00001015 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
1016 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001017 break;
1018 }
Tim Northover6b3bd612016-07-29 20:32:59 +00001019 case MachineOperand::MO_IntrinsicID: {
1020 Intrinsic::ID ID = Op.getIntrinsicID();
1021 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +00001022 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +00001023 else {
1024 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
1025 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1026 OS << "intrinsic(@" << TII->getName(ID) << ')';
1027 }
1028 break;
1029 }
Tim Northoverde3aea0412016-08-17 20:25:25 +00001030 case MachineOperand::MO_Predicate: {
1031 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
1032 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
1033 << CmpInst::getPredicateName(Pred) << ')';
1034 break;
1035 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001036 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +00001037}
1038
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001039void MIPrinter::print(const LLVMContext &Context, const MachineMemOperand &Op) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00001040 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001041 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +00001042 if (Op.isVolatile())
1043 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +00001044 if (Op.isNonTemporal())
1045 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +00001046 if (Op.isDereferenceable())
1047 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00001048 if (Op.isInvariant())
1049 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +00001050 if (Op.isLoad())
1051 OS << "load ";
1052 else {
1053 assert(Op.isStore() && "Non load machine operand must be a store");
1054 OS << "store ";
1055 }
Tim Northoverb73e3092017-02-13 22:14:08 +00001056
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001057 printSyncScope(Context, Op.getSyncScopeID());
Tim Northoverb73e3092017-02-13 22:14:08 +00001058
1059 if (Op.getOrdering() != AtomicOrdering::NotAtomic)
1060 OS << toIRString(Op.getOrdering()) << ' ';
1061 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic)
1062 OS << toIRString(Op.getFailureOrdering()) << ' ';
1063
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001064 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +00001065 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001066 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +00001067 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +00001068 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
1069 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +00001070 assert(PVal && "Expected a pseudo source value");
1071 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00001072 case PseudoSourceValue::Stack:
1073 OS << "stack";
1074 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00001075 case PseudoSourceValue::GOT:
1076 OS << "got";
1077 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00001078 case PseudoSourceValue::JumpTable:
1079 OS << "jump-table";
1080 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001081 case PseudoSourceValue::ConstantPool:
1082 OS << "constant-pool";
1083 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00001084 case PseudoSourceValue::FixedStack:
1085 printStackObjectReference(
1086 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
1087 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +00001088 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001089 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +00001090 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
1091 OS, /*PrintType=*/false, MST);
1092 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001093 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +00001094 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +00001095 printLLVMNameWithoutPrefix(
1096 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +00001097 break;
Tom Stellard7761abb2016-12-17 04:41:53 +00001098 case PseudoSourceValue::TargetCustom:
1099 llvm_unreachable("TargetCustom pseudo source values are not supported");
1100 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00001101 }
1102 }
Alex Lorenz83127732015-08-07 20:26:52 +00001103 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +00001104 if (Op.getBaseAlignment() != Op.getSize())
1105 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +00001106 auto AAInfo = Op.getAAInfo();
1107 if (AAInfo.TBAA) {
1108 OS << ", !tbaa ";
1109 AAInfo.TBAA->printAsOperand(OS, MST);
1110 }
Alex Lorenza16f6242015-08-17 22:06:40 +00001111 if (AAInfo.Scope) {
1112 OS << ", !alias.scope ";
1113 AAInfo.Scope->printAsOperand(OS, MST);
1114 }
Alex Lorenz03e940d2015-08-17 22:08:02 +00001115 if (AAInfo.NoAlias) {
1116 OS << ", !noalias ";
1117 AAInfo.NoAlias->printAsOperand(OS, MST);
1118 }
Alex Lorenzeb625682015-08-17 22:09:52 +00001119 if (Op.getRanges()) {
1120 OS << ", !range ";
1121 Op.getRanges()->printAsOperand(OS, MST);
1122 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00001123 OS << ')';
1124}
1125
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001126void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) {
1127 switch (SSID) {
1128 case SyncScope::System: {
1129 break;
1130 }
1131 default: {
1132 if (SSNs.empty())
1133 Context.getSyncScopeNames(SSNs);
1134
1135 OS << "syncscope(\"";
1136 PrintEscapedString(SSNs[SSID], OS);
1137 OS << "\") ";
1138 break;
1139 }
1140 }
1141}
1142
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001143static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
1144 const TargetRegisterInfo *TRI) {
1145 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
1146 if (Reg == -1) {
1147 OS << "<badreg>";
1148 return;
1149 }
1150 printReg(Reg, OS, TRI);
1151}
1152
1153void MIPrinter::print(const MCCFIInstruction &CFI,
1154 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001155 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001156 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001157 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001158 if (CFI.getLabel())
1159 OS << "<mcsymbol> ";
1160 printCFIRegister(CFI.getRegister(), OS, TRI);
1161 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001162 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001163 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001164 if (CFI.getLabel())
1165 OS << "<mcsymbol> ";
1166 printCFIRegister(CFI.getRegister(), OS, TRI);
1167 OS << ", " << CFI.getOffset();
1168 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001169 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001170 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001171 if (CFI.getLabel())
1172 OS << "<mcsymbol> ";
1173 printCFIRegister(CFI.getRegister(), OS, TRI);
1174 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001175 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001176 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001177 if (CFI.getLabel())
1178 OS << "<mcsymbol> ";
1179 OS << CFI.getOffset();
1180 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001181 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001182 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001183 if (CFI.getLabel())
1184 OS << "<mcsymbol> ";
1185 printCFIRegister(CFI.getRegister(), OS, TRI);
1186 OS << ", " << CFI.getOffset();
1187 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001188 default:
1189 // TODO: Print the other CFI Operations.
1190 OS << "<unserializable cfi operation>";
1191 break;
1192 }
1193}
1194
Alex Lorenz345c1442015-06-15 23:52:35 +00001195void llvm::printMIR(raw_ostream &OS, const Module &M) {
1196 yaml::Output Out(OS);
1197 Out << const_cast<Module &>(M);
1198}
1199
1200void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1201 MIRPrinter Printer(OS);
1202 Printer.print(MF);
1203}