blob: 2b5c789605d04cd81da3b0c081b13a0c400cca4a [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
15#include "MIRPrinter.h"
16#include "llvm/ADT/STLExtras.h"
Tim Northoverd28d3cc2016-09-12 11:20:10 +000017#include "llvm/ADT/SmallBitVector.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000018#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
19#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000022#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000026#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000027#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000028#include "llvm/IR/DebugInfo.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000029#include "llvm/IR/IRPrintingPasses.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000030#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000031#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000032#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000033#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000034#include "llvm/MC/MCSymbol.h"
Geoff Berryb51774a2016-11-18 19:37:24 +000035#include "llvm/Support/Format.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000036#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000037#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000038#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000039#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000040#include "llvm/Target/TargetIntrinsicInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000041#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000042
43using namespace llvm;
44
45namespace {
46
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000047/// This structure describes how to print out stack object references.
48struct FrameIndexOperand {
49 std::string Name;
50 unsigned ID;
51 bool IsFixed;
52
53 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
54 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
55
56 /// Return an ordinary stack object reference.
57 static FrameIndexOperand create(StringRef Name, unsigned ID) {
58 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
59 }
60
61 /// Return a fixed stack object reference.
62 static FrameIndexOperand createFixed(unsigned ID) {
63 return FrameIndexOperand("", ID, /*IsFixed=*/true);
64 }
65};
66
Alex Lorenz618b2832015-07-30 16:54:38 +000067} // end anonymous namespace
68
69namespace llvm {
70
Alex Lorenz345c1442015-06-15 23:52:35 +000071/// This class prints out the machine functions using the MIR serialization
72/// format.
73class MIRPrinter {
74 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000075 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000076 /// Maps from stack object indices to operand indices which will be used when
77 /// printing frame index machine operands.
78 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000079
80public:
81 MIRPrinter(raw_ostream &OS) : OS(OS) {}
82
83 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000084
Alex Lorenz28148ba2015-07-09 22:23:13 +000085 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
86 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000087 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
88 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000089 void convert(yaml::MachineFunction &MF,
90 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000091 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
92 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000093 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000094 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
95 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000096 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000097
98private:
99 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +0000100};
101
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000102/// This class prints out the machine instructions using the MIR serialization
103/// format.
104class MIPrinter {
105 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000106 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000107 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000108 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000109
110public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000111 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000112 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
113 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
114 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
115 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000116
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000117 void print(const MachineBasicBlock &MBB);
118
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000119 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000120 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000121 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000122 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000123 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000124 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000125 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000126 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000127 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000128 LLT TypeToPrint, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000129 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000130
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000131 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000132};
133
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000134} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000135
136namespace llvm {
137namespace yaml {
138
139/// This struct serializes the LLVM IR module.
140template <> struct BlockScalarTraits<Module> {
141 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
142 Mod.print(OS, nullptr);
143 }
144 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
145 llvm_unreachable("LLVM Module is supposed to be parsed separately");
146 return "";
147 }
148};
149
150} // end namespace yaml
151} // end namespace llvm
152
Alex Lorenz15a00a82015-07-14 21:18:25 +0000153static void printReg(unsigned Reg, raw_ostream &OS,
154 const TargetRegisterInfo *TRI) {
155 // TODO: Print Stack Slots.
156 if (!Reg)
157 OS << '_';
158 else if (TargetRegisterInfo::isVirtualRegister(Reg))
159 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
160 else if (Reg < TRI->getNumRegs())
161 OS << '%' << StringRef(TRI->getName(Reg)).lower();
162 else
163 llvm_unreachable("Can't print this kind of register yet");
164}
165
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000166static void printReg(unsigned Reg, yaml::StringValue &Dest,
167 const TargetRegisterInfo *TRI) {
168 raw_string_ostream OS(Dest.Value);
169 printReg(Reg, OS, TRI);
170}
171
Alex Lorenz345c1442015-06-15 23:52:35 +0000172void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000173 initRegisterMaskIds(MF);
174
Alex Lorenz345c1442015-06-15 23:52:35 +0000175 yaml::MachineFunction YamlMF;
176 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000177 YamlMF.Alignment = MF.getAlignment();
178 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000179
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000180 YamlMF.Legalized = MF.getProperties().hasProperty(
181 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000182 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
183 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000184 YamlMF.Selected = MF.getProperties().hasProperty(
185 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000186
Alex Lorenz28148ba2015-07-09 22:23:13 +0000187 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000188 ModuleSlotTracker MST(MF.getFunction()->getParent());
189 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000190 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
191 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000192 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000193 if (const auto *ConstantPool = MF.getConstantPool())
194 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000195 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
196 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000197 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
198 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000199 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000200 if (IsNewlineNeeded)
201 StrOS << "\n";
202 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
203 .print(MBB);
204 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000205 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000206 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000207 yaml::Output Out(OS);
208 Out << YamlMF;
209}
210
Alex Lorenz54565cf2015-06-24 19:56:10 +0000211void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000212 const MachineRegisterInfo &RegInfo,
213 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000214 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000215
216 // Print the virtual register definitions.
217 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
218 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
219 yaml::VirtualRegisterDefinition VReg;
220 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000221 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000222 VReg.Class =
223 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000224 else if (RegInfo.getRegBankOrNull(Reg))
225 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000226 else {
227 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000228 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000229 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000230 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000231 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
232 if (PreferredReg)
233 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000234 MF.VirtualRegisters.push_back(VReg);
235 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000236
237 // Print the live ins.
238 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
239 yaml::MachineFunctionLiveIn LiveIn;
240 printReg(I->first, LiveIn.Register, TRI);
241 if (I->second)
242 printReg(I->second, LiveIn.VirtualRegister, TRI);
243 MF.LiveIns.push_back(LiveIn);
244 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000245 // The used physical register mask is printed as an inverted callee saved
246 // register mask.
247 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
248 if (UsedPhysRegMask.none())
249 return;
250 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
251 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
252 if (!UsedPhysRegMask[I]) {
253 yaml::FlowStringValue Reg;
254 printReg(I, Reg, TRI);
255 CalleeSavedRegisters.push_back(Reg);
256 }
257 }
258 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000259}
260
Alex Lorenza6f9a372015-07-29 21:09:09 +0000261void MIRPrinter::convert(ModuleSlotTracker &MST,
262 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000263 const MachineFrameInfo &MFI) {
264 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
265 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
266 YamlMFI.HasStackMap = MFI.hasStackMap();
267 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
268 YamlMFI.StackSize = MFI.getStackSize();
269 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
270 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
271 YamlMFI.AdjustsStack = MFI.adjustsStack();
272 YamlMFI.HasCalls = MFI.hasCalls();
273 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
274 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
275 YamlMFI.HasVAStart = MFI.hasVAStart();
276 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000277 if (MFI.getSavePoint()) {
278 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
279 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
280 .printMBBReference(*MFI.getSavePoint());
281 }
282 if (MFI.getRestorePoint()) {
283 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
284 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
285 .printMBBReference(*MFI.getRestorePoint());
286 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000287}
288
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000289void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000290 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000291 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000292 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000293 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000294 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000295 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000296 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
297 if (MFI.isDeadObjectIndex(I))
298 continue;
299
300 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000301 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000302 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
303 ? yaml::FixedMachineStackObject::SpillSlot
304 : yaml::FixedMachineStackObject::DefaultType;
305 YamlObject.Offset = MFI.getObjectOffset(I);
306 YamlObject.Size = MFI.getObjectSize(I);
307 YamlObject.Alignment = MFI.getObjectAlignment(I);
308 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
309 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
310 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000311 StackObjectOperandMapping.insert(
312 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000313 }
314
315 // Process ordinary stack objects.
316 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000317 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
318 if (MFI.isDeadObjectIndex(I))
319 continue;
320
321 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000322 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000323 if (const auto *Alloca = MFI.getObjectAllocation(I))
324 YamlObject.Name.Value =
325 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000326 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
327 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000328 : MFI.isVariableSizedObjectIndex(I)
329 ? yaml::MachineStackObject::VariableSized
330 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000331 YamlObject.Offset = MFI.getObjectOffset(I);
332 YamlObject.Size = MFI.getObjectSize(I);
333 YamlObject.Alignment = MFI.getObjectAlignment(I);
334
335 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000336 StackObjectOperandMapping.insert(std::make_pair(
337 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000338 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000339
340 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
341 yaml::StringValue Reg;
342 printReg(CSInfo.getReg(), Reg, TRI);
343 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
344 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
345 "Invalid stack object index");
346 const FrameIndexOperand &StackObject = StackObjectInfo->second;
347 if (StackObject.IsFixed)
348 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
349 else
350 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
351 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000352 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
353 auto LocalObject = MFI.getLocalFrameObjectMap(I);
354 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
355 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
356 "Invalid stack object index");
357 const FrameIndexOperand &StackObject = StackObjectInfo->second;
358 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
359 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
360 }
Alex Lorenza314d812015-08-18 22:26:26 +0000361
362 // Print the stack object references in the frame information class after
363 // converting the stack objects.
364 if (MFI.hasStackProtectorIndex()) {
365 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
366 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
367 .printStackObjectReference(MFI.getStackProtectorIndex());
368 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000369
370 // Print the debug variable information.
371 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
372 MMI.getVariableDbgInfo()) {
373 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
374 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
375 "Invalid stack object index");
376 const FrameIndexOperand &StackObject = StackObjectInfo->second;
377 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
378 auto &Object = MF.StackObjects[StackObject.ID];
379 {
380 raw_string_ostream StrOS(Object.DebugVar.Value);
381 DebugVar.Var->printAsOperand(StrOS, MST);
382 }
383 {
384 raw_string_ostream StrOS(Object.DebugExpr.Value);
385 DebugVar.Expr->printAsOperand(StrOS, MST);
386 }
387 {
388 raw_string_ostream StrOS(Object.DebugLoc.Value);
389 DebugVar.Loc->printAsOperand(StrOS, MST);
390 }
391 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000392}
393
Alex Lorenzab980492015-07-20 20:51:18 +0000394void MIRPrinter::convert(yaml::MachineFunction &MF,
395 const MachineConstantPool &ConstantPool) {
396 unsigned ID = 0;
397 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
398 // TODO: Serialize target specific constant pool entries.
399 if (Constant.isMachineConstantPoolEntry())
400 llvm_unreachable("Can't print target specific constant pool entries yet");
401
402 yaml::MachineConstantPoolValue YamlConstant;
403 std::string Str;
404 raw_string_ostream StrOS(Str);
405 Constant.Val.ConstVal->printAsOperand(StrOS);
406 YamlConstant.ID = ID++;
407 YamlConstant.Value = StrOS.str();
408 YamlConstant.Alignment = Constant.getAlignment();
409 MF.Constants.push_back(YamlConstant);
410 }
411}
412
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000413void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000414 yaml::MachineJumpTable &YamlJTI,
415 const MachineJumpTableInfo &JTI) {
416 YamlJTI.Kind = JTI.getEntryKind();
417 unsigned ID = 0;
418 for (const auto &Table : JTI.getJumpTables()) {
419 std::string Str;
420 yaml::MachineJumpTable::Entry Entry;
421 Entry.ID = ID++;
422 for (const auto *MBB : Table.MBBs) {
423 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000424 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
425 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000426 Entry.Blocks.push_back(StrOS.str());
427 Str.clear();
428 }
429 YamlJTI.Entries.push_back(Entry);
430 }
431}
432
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000433void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
434 const auto *TRI = MF.getSubtarget().getRegisterInfo();
435 unsigned I = 0;
436 for (const uint32_t *Mask : TRI->getRegMasks())
437 RegisterMaskIds.insert(std::make_pair(Mask, I++));
438}
439
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000440void MIPrinter::print(const MachineBasicBlock &MBB) {
441 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
442 OS << "bb." << MBB.getNumber();
443 bool HasAttributes = false;
444 if (const auto *BB = MBB.getBasicBlock()) {
445 if (BB->hasName()) {
446 OS << "." << BB->getName();
447 } else {
448 HasAttributes = true;
449 OS << " (";
450 int Slot = MST.getLocalSlot(BB);
451 if (Slot == -1)
452 OS << "<ir-block badref>";
453 else
454 OS << (Twine("%ir-block.") + Twine(Slot)).str();
455 }
456 }
457 if (MBB.hasAddressTaken()) {
458 OS << (HasAttributes ? ", " : " (");
459 OS << "address-taken";
460 HasAttributes = true;
461 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000462 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000463 OS << (HasAttributes ? ", " : " (");
464 OS << "landing-pad";
465 HasAttributes = true;
466 }
467 if (MBB.getAlignment()) {
468 OS << (HasAttributes ? ", " : " (");
469 OS << "align " << MBB.getAlignment();
470 HasAttributes = true;
471 }
472 if (HasAttributes)
473 OS << ")";
474 OS << ":\n";
475
476 bool HasLineAttributes = false;
477 // Print the successors
478 if (!MBB.succ_empty()) {
479 OS.indent(2) << "successors: ";
480 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
481 if (I != MBB.succ_begin())
482 OS << ", ";
483 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000484 if (MBB.hasSuccessorProbabilities())
Geoff Berryb51774a2016-11-18 19:37:24 +0000485 OS << '('
486 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator())
487 << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000488 }
489 OS << "\n";
490 HasLineAttributes = true;
491 }
492
493 // Print the live in registers.
494 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
495 assert(TRI && "Expected target register info");
496 if (!MBB.livein_empty()) {
497 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000498 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000499 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000500 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000501 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000502 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000503 printReg(LI.PhysReg, OS, TRI);
504 if (LI.LaneMask != ~0u)
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000505 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000506 }
507 OS << "\n";
508 HasLineAttributes = true;
509 }
510
511 if (HasLineAttributes)
512 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000513 bool IsInBundle = false;
514 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
515 const MachineInstr &MI = *I;
516 if (IsInBundle && !MI.isInsideBundle()) {
517 OS.indent(2) << "}\n";
518 IsInBundle = false;
519 }
520 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000522 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
523 OS << " {";
524 IsInBundle = true;
525 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000526 OS << "\n";
527 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000528 if (IsInBundle)
529 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000530}
531
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000532/// Return true when an instruction has tied register that can't be determined
533/// by the instruction's descriptor.
534static bool hasComplexRegisterTies(const MachineInstr &MI) {
535 const MCInstrDesc &MCID = MI.getDesc();
536 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
537 const auto &Operand = MI.getOperand(I);
538 if (!Operand.isReg() || Operand.isDef())
539 // Ignore the defined registers as MCID marks only the uses as tied.
540 continue;
541 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
542 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
543 if (ExpectedTiedIdx != TiedIdx)
544 return true;
545 }
546 return false;
547}
548
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000549static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
550 SmallBitVector &PrintedTypes,
551 const MachineRegisterInfo &MRI) {
552 const MachineOperand &Op = MI.getOperand(OpIdx);
553 if (!Op.isReg())
554 return LLT{};
555
556 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
557 return MRI.getType(Op.getReg());
558
559 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
560 if (!OpInfo.isGenericType())
561 return MRI.getType(Op.getReg());
562
563 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
564 return LLT{};
565
566 PrintedTypes.set(OpInfo.getGenericTypeIndex());
567 return MRI.getType(Op.getReg());
568}
569
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000570void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000571 const auto *MF = MI.getParent()->getParent();
572 const auto &MRI = MF->getRegInfo();
573 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000574 const auto *TRI = SubTarget.getRegisterInfo();
575 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000576 const auto *TII = SubTarget.getInstrInfo();
577 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000578 if (MI.isCFIInstruction())
579 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000580
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000581 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000582 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583 unsigned I = 0, E = MI.getNumOperands();
584 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
585 !MI.getOperand(I).isImplicit();
586 ++I) {
587 if (I)
588 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000589 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
590 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000591 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000592 }
593
594 if (I)
595 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000596 if (MI.getFlag(MachineInstr::FrameSetup))
597 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000598 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000599 if (I < E)
600 OS << ' ';
601
602 bool NeedComma = false;
603 for (; I < E; ++I) {
604 if (NeedComma)
605 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000606 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
607 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000608 NeedComma = true;
609 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000610
611 if (MI.getDebugLoc()) {
612 if (NeedComma)
613 OS << ',';
614 OS << " debug-location ";
615 MI.getDebugLoc()->printAsOperand(OS, MST);
616 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000617
618 if (!MI.memoperands_empty()) {
619 OS << " :: ";
620 bool NeedComma = false;
621 for (const auto *Op : MI.memoperands()) {
622 if (NeedComma)
623 OS << ", ";
624 print(*Op);
625 NeedComma = true;
626 }
627 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000628}
629
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000630void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
631 OS << "%bb." << MBB.getNumber();
632 if (const auto *BB = MBB.getBasicBlock()) {
633 if (BB->hasName())
634 OS << '.' << BB->getName();
635 }
636}
637
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000638static void printIRSlotNumber(raw_ostream &OS, int Slot) {
639 if (Slot == -1)
640 OS << "<badref>";
641 else
642 OS << Slot;
643}
644
Alex Lorenzdeb53492015-07-28 17:28:03 +0000645void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
646 OS << "%ir-block.";
647 if (BB.hasName()) {
648 printLLVMNameWithoutPrefix(OS, BB.getName());
649 return;
650 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000651 const Function *F = BB.getParent();
652 int Slot;
653 if (F == MST.getCurrentFunction()) {
654 Slot = MST.getLocalSlot(&BB);
655 } else {
656 ModuleSlotTracker CustomMST(F->getParent(),
657 /*ShouldInitializeAllMetadata=*/false);
658 CustomMST.incorporateFunction(*F);
659 Slot = CustomMST.getLocalSlot(&BB);
660 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000661 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000662}
663
Alex Lorenz4af7e612015-08-03 23:08:19 +0000664void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000665 if (isa<GlobalValue>(V)) {
666 V.printAsOperand(OS, /*PrintType=*/false, MST);
667 return;
668 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000669 if (isa<Constant>(V)) {
670 // Machine memory operands can load/store to/from constant value pointers.
671 OS << '`';
672 V.printAsOperand(OS, /*PrintType=*/true, MST);
673 OS << '`';
674 return;
675 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000676 OS << "%ir.";
677 if (V.hasName()) {
678 printLLVMNameWithoutPrefix(OS, V.getName());
679 return;
680 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000681 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000682}
683
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000684void MIPrinter::printStackObjectReference(int FrameIndex) {
685 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
686 assert(ObjectInfo != StackObjectOperandMapping.end() &&
687 "Invalid frame index");
688 const FrameIndexOperand &Operand = ObjectInfo->second;
689 if (Operand.IsFixed) {
690 OS << "%fixed-stack." << Operand.ID;
691 return;
692 }
693 OS << "%stack." << Operand.ID;
694 if (!Operand.Name.empty())
695 OS << '.' << Operand.Name;
696}
697
Alex Lorenz5672a892015-08-05 22:26:15 +0000698void MIPrinter::printOffset(int64_t Offset) {
699 if (Offset == 0)
700 return;
701 if (Offset < 0) {
702 OS << " - " << -Offset;
703 return;
704 }
705 OS << " + " << Offset;
706}
707
Alex Lorenz49873a82015-08-06 00:44:07 +0000708static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
709 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
710 for (const auto &I : Flags) {
711 if (I.first == TF) {
712 return I.second;
713 }
714 }
715 return nullptr;
716}
717
718void MIPrinter::printTargetFlags(const MachineOperand &Op) {
719 if (!Op.getTargetFlags())
720 return;
721 const auto *TII =
722 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
723 assert(TII && "expected instruction info");
724 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
725 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000726 const bool HasDirectFlags = Flags.first;
727 const bool HasBitmaskFlags = Flags.second;
728 if (!HasDirectFlags && !HasBitmaskFlags) {
729 OS << "<unknown>) ";
730 return;
731 }
732 if (HasDirectFlags) {
733 if (const auto *Name = getTargetFlagName(TII, Flags.first))
734 OS << Name;
735 else
736 OS << "<unknown target flag>";
737 }
738 if (!HasBitmaskFlags) {
739 OS << ") ";
740 return;
741 }
742 bool IsCommaNeeded = HasDirectFlags;
743 unsigned BitMask = Flags.second;
744 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
745 for (const auto &Mask : BitMasks) {
746 // Check if the flag's bitmask has the bits of the current mask set.
747 if ((BitMask & Mask.first) == Mask.first) {
748 if (IsCommaNeeded)
749 OS << ", ";
750 IsCommaNeeded = true;
751 OS << Mask.second;
752 // Clear the bits which were serialized from the flag's bitmask.
753 BitMask &= ~(Mask.first);
754 }
755 }
756 if (BitMask) {
757 // When the resulting flag's bitmask isn't zero, we know that we didn't
758 // serialize all of the bit flags.
759 if (IsCommaNeeded)
760 OS << ", ";
761 OS << "<unknown bitmask target flag>";
762 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000763 OS << ") ";
764}
765
Alex Lorenzef5c1962015-07-28 23:02:45 +0000766static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
767 const auto *TII = MF.getSubtarget().getInstrInfo();
768 assert(TII && "expected instruction info");
769 auto Indices = TII->getSerializableTargetIndices();
770 for (const auto &I : Indices) {
771 if (I.first == Index) {
772 return I.second;
773 }
774 }
775 return nullptr;
776}
777
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000778void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000779 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
780 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000781 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000782 switch (Op.getType()) {
783 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000784 if (Op.isImplicit())
785 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000786 else if (!IsDef && Op.isDef())
787 // Print the 'def' flag only when the operand is defined after '='.
788 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000789 if (Op.isInternalRead())
790 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000791 if (Op.isDead())
792 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000793 if (Op.isKill())
794 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000795 if (Op.isUndef())
796 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000797 if (Op.isEarlyClobber())
798 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000799 if (Op.isDebug())
800 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000801 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000802 // Print the sub register.
803 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000804 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000805 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
806 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000807 if (TypeToPrint.isValid())
808 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000809 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000810 case MachineOperand::MO_Immediate:
811 OS << Op.getImm();
812 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000813 case MachineOperand::MO_CImmediate:
814 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
815 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000816 case MachineOperand::MO_FPImmediate:
817 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
818 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000819 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000820 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000821 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000822 case MachineOperand::MO_FrameIndex:
823 printStackObjectReference(Op.getIndex());
824 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000825 case MachineOperand::MO_ConstantPoolIndex:
826 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000827 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000828 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000829 case MachineOperand::MO_TargetIndex: {
830 OS << "target-index(";
831 if (const auto *Name = getTargetIndexName(
832 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
833 OS << Name;
834 else
835 OS << "<unknown>";
836 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000837 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000838 break;
839 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000840 case MachineOperand::MO_JumpTableIndex:
841 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000842 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000843 case MachineOperand::MO_ExternalSymbol:
844 OS << '$';
845 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000846 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000847 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000848 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000849 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000850 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000851 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000852 case MachineOperand::MO_BlockAddress:
853 OS << "blockaddress(";
854 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
855 MST);
856 OS << ", ";
857 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
858 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000859 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000860 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000861 case MachineOperand::MO_RegisterMask: {
862 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
863 if (RegMaskInfo != RegisterMaskIds.end())
864 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
865 else
866 llvm_unreachable("Can't print this machine register mask yet.");
867 break;
868 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000869 case MachineOperand::MO_RegisterLiveOut: {
870 const uint32_t *RegMask = Op.getRegLiveOut();
871 OS << "liveout(";
872 bool IsCommaNeeded = false;
873 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
874 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
875 if (IsCommaNeeded)
876 OS << ", ";
877 printReg(Reg, OS, TRI);
878 IsCommaNeeded = true;
879 }
880 }
881 OS << ")";
882 break;
883 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000884 case MachineOperand::MO_Metadata:
885 Op.getMetadata()->printAsOperand(OS, MST);
886 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000887 case MachineOperand::MO_MCSymbol:
888 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
889 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000890 case MachineOperand::MO_CFIIndex: {
891 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000892 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000893 break;
894 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000895 case MachineOperand::MO_IntrinsicID: {
896 Intrinsic::ID ID = Op.getIntrinsicID();
897 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000898 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000899 else {
900 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
901 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
902 OS << "intrinsic(@" << TII->getName(ID) << ')';
903 }
904 break;
905 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000906 case MachineOperand::MO_Predicate: {
907 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
908 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
909 << CmpInst::getPredicateName(Pred) << ')';
910 break;
911 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000912 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000913}
914
Alex Lorenz4af7e612015-08-03 23:08:19 +0000915void MIPrinter::print(const MachineMemOperand &Op) {
916 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000917 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000918 if (Op.isVolatile())
919 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000920 if (Op.isNonTemporal())
921 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000922 if (Op.isDereferenceable())
923 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000924 if (Op.isInvariant())
925 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000926 if (Op.isLoad())
927 OS << "load ";
928 else {
929 assert(Op.isStore() && "Non load machine operand must be a store");
930 OS << "store ";
931 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000932 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000933 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000934 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000935 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000936 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
937 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000938 assert(PVal && "Expected a pseudo source value");
939 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000940 case PseudoSourceValue::Stack:
941 OS << "stack";
942 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000943 case PseudoSourceValue::GOT:
944 OS << "got";
945 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000946 case PseudoSourceValue::JumpTable:
947 OS << "jump-table";
948 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000949 case PseudoSourceValue::ConstantPool:
950 OS << "constant-pool";
951 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000952 case PseudoSourceValue::FixedStack:
953 printStackObjectReference(
954 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
955 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000956 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000957 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000958 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
959 OS, /*PrintType=*/false, MST);
960 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000961 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000962 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000963 printLLVMNameWithoutPrefix(
964 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000965 break;
966 }
967 }
Alex Lorenz83127732015-08-07 20:26:52 +0000968 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000969 if (Op.getBaseAlignment() != Op.getSize())
970 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000971 auto AAInfo = Op.getAAInfo();
972 if (AAInfo.TBAA) {
973 OS << ", !tbaa ";
974 AAInfo.TBAA->printAsOperand(OS, MST);
975 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000976 if (AAInfo.Scope) {
977 OS << ", !alias.scope ";
978 AAInfo.Scope->printAsOperand(OS, MST);
979 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000980 if (AAInfo.NoAlias) {
981 OS << ", !noalias ";
982 AAInfo.NoAlias->printAsOperand(OS, MST);
983 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000984 if (Op.getRanges()) {
985 OS << ", !range ";
986 Op.getRanges()->printAsOperand(OS, MST);
987 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000988 OS << ')';
989}
990
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000991static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
992 const TargetRegisterInfo *TRI) {
993 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
994 if (Reg == -1) {
995 OS << "<badreg>";
996 return;
997 }
998 printReg(Reg, OS, TRI);
999}
1000
1001void MIPrinter::print(const MCCFIInstruction &CFI,
1002 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001003 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001004 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001005 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001006 if (CFI.getLabel())
1007 OS << "<mcsymbol> ";
1008 printCFIRegister(CFI.getRegister(), OS, TRI);
1009 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001010 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001011 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001012 if (CFI.getLabel())
1013 OS << "<mcsymbol> ";
1014 printCFIRegister(CFI.getRegister(), OS, TRI);
1015 OS << ", " << CFI.getOffset();
1016 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001017 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001018 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001019 if (CFI.getLabel())
1020 OS << "<mcsymbol> ";
1021 printCFIRegister(CFI.getRegister(), OS, TRI);
1022 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001023 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001024 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001025 if (CFI.getLabel())
1026 OS << "<mcsymbol> ";
1027 OS << CFI.getOffset();
1028 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001029 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001030 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001031 if (CFI.getLabel())
1032 OS << "<mcsymbol> ";
1033 printCFIRegister(CFI.getRegister(), OS, TRI);
1034 OS << ", " << CFI.getOffset();
1035 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001036 default:
1037 // TODO: Print the other CFI Operations.
1038 OS << "<unserializable cfi operation>";
1039 break;
1040 }
1041}
1042
Alex Lorenz345c1442015-06-15 23:52:35 +00001043void llvm::printMIR(raw_ostream &OS, const Module &M) {
1044 yaml::Output Out(OS);
1045 Out << const_cast<Module &>(M);
1046}
1047
1048void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1049 MIRPrinter Printer(OS);
1050 Printer.print(MF);
1051}