blob: 884defb9dbf34d0e250973654a8342565b45f4bf [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"
Alex Lorenz345c1442015-06-15 23:52:35 +000035#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000036#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000037#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000038#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000039#include "llvm/Target/TargetIntrinsicInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000040#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000041
42using namespace llvm;
43
44namespace {
45
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000046/// This structure describes how to print out stack object references.
47struct FrameIndexOperand {
48 std::string Name;
49 unsigned ID;
50 bool IsFixed;
51
52 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
53 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
54
55 /// Return an ordinary stack object reference.
56 static FrameIndexOperand create(StringRef Name, unsigned ID) {
57 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
58 }
59
60 /// Return a fixed stack object reference.
61 static FrameIndexOperand createFixed(unsigned ID) {
62 return FrameIndexOperand("", ID, /*IsFixed=*/true);
63 }
64};
65
Alex Lorenz618b2832015-07-30 16:54:38 +000066} // end anonymous namespace
67
68namespace llvm {
69
Alex Lorenz345c1442015-06-15 23:52:35 +000070/// This class prints out the machine functions using the MIR serialization
71/// format.
72class MIRPrinter {
73 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000074 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000075 /// Maps from stack object indices to operand indices which will be used when
76 /// printing frame index machine operands.
77 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000078
79public:
80 MIRPrinter(raw_ostream &OS) : OS(OS) {}
81
82 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000083
Alex Lorenz28148ba2015-07-09 22:23:13 +000084 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
85 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000086 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
87 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000088 void convert(yaml::MachineFunction &MF,
89 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000090 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
91 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000092 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000093 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
94 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000095 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000096
97private:
98 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000099};
100
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000101/// This class prints out the machine instructions using the MIR serialization
102/// format.
103class MIPrinter {
104 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000105 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000106 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000107 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000108
109public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000110 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000111 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
112 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
113 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
114 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000115
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000116 void print(const MachineBasicBlock &MBB);
117
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000118 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000119 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000120 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000121 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000122 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000123 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000124 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000125 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000126 unsigned I, bool ShouldPrintRegisterTies,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000127 LLT TypeToPrint, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000128 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000129
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000130 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000131};
132
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000133} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000134
135namespace llvm {
136namespace yaml {
137
138/// This struct serializes the LLVM IR module.
139template <> struct BlockScalarTraits<Module> {
140 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
141 Mod.print(OS, nullptr);
142 }
143 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
144 llvm_unreachable("LLVM Module is supposed to be parsed separately");
145 return "";
146 }
147};
148
149} // end namespace yaml
150} // end namespace llvm
151
Alex Lorenz15a00a82015-07-14 21:18:25 +0000152static void printReg(unsigned Reg, raw_ostream &OS,
153 const TargetRegisterInfo *TRI) {
154 // TODO: Print Stack Slots.
155 if (!Reg)
156 OS << '_';
157 else if (TargetRegisterInfo::isVirtualRegister(Reg))
158 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
159 else if (Reg < TRI->getNumRegs())
160 OS << '%' << StringRef(TRI->getName(Reg)).lower();
161 else
162 llvm_unreachable("Can't print this kind of register yet");
163}
164
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000165static void printReg(unsigned Reg, yaml::StringValue &Dest,
166 const TargetRegisterInfo *TRI) {
167 raw_string_ostream OS(Dest.Value);
168 printReg(Reg, OS, TRI);
169}
170
Alex Lorenz345c1442015-06-15 23:52:35 +0000171void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000172 initRegisterMaskIds(MF);
173
Alex Lorenz345c1442015-06-15 23:52:35 +0000174 yaml::MachineFunction YamlMF;
175 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000176 YamlMF.Alignment = MF.getAlignment();
177 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000178
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000179 YamlMF.Legalized = MF.getProperties().hasProperty(
180 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000181 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
182 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000183 YamlMF.Selected = MF.getProperties().hasProperty(
184 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000185
Alex Lorenz28148ba2015-07-09 22:23:13 +0000186 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000187 ModuleSlotTracker MST(MF.getFunction()->getParent());
188 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000189 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
190 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000191 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000192 if (const auto *ConstantPool = MF.getConstantPool())
193 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000194 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
195 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000196 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
197 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000198 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000199 if (IsNewlineNeeded)
200 StrOS << "\n";
201 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
202 .print(MBB);
203 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000204 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000205 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000206 yaml::Output Out(OS);
207 Out << YamlMF;
208}
209
Alex Lorenz54565cf2015-06-24 19:56:10 +0000210void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000211 const MachineRegisterInfo &RegInfo,
212 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000213 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000214
215 // Print the virtual register definitions.
216 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
217 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
218 yaml::VirtualRegisterDefinition VReg;
219 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000220 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000221 VReg.Class =
222 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000223 else if (RegInfo.getRegBankOrNull(Reg))
224 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000225 else {
226 VReg.Class = std::string("_");
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000227 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
Tim Northover0f140c72016-09-09 11:46:34 +0000228 "Generic registers must have a valid type");
Quentin Colombet050b2112016-03-08 01:17:03 +0000229 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000230 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
231 if (PreferredReg)
232 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000233 MF.VirtualRegisters.push_back(VReg);
234 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000235
236 // Print the live ins.
237 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
238 yaml::MachineFunctionLiveIn LiveIn;
239 printReg(I->first, LiveIn.Register, TRI);
240 if (I->second)
241 printReg(I->second, LiveIn.VirtualRegister, TRI);
242 MF.LiveIns.push_back(LiveIn);
243 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000244 // The used physical register mask is printed as an inverted callee saved
245 // register mask.
246 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
247 if (UsedPhysRegMask.none())
248 return;
249 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
250 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
251 if (!UsedPhysRegMask[I]) {
252 yaml::FlowStringValue Reg;
253 printReg(I, Reg, TRI);
254 CalleeSavedRegisters.push_back(Reg);
255 }
256 }
257 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000258}
259
Alex Lorenza6f9a372015-07-29 21:09:09 +0000260void MIRPrinter::convert(ModuleSlotTracker &MST,
261 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000262 const MachineFrameInfo &MFI) {
263 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
264 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
265 YamlMFI.HasStackMap = MFI.hasStackMap();
266 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
267 YamlMFI.StackSize = MFI.getStackSize();
268 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
269 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
270 YamlMFI.AdjustsStack = MFI.adjustsStack();
271 YamlMFI.HasCalls = MFI.hasCalls();
272 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
273 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
274 YamlMFI.HasVAStart = MFI.hasVAStart();
275 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000276 if (MFI.getSavePoint()) {
277 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
278 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
279 .printMBBReference(*MFI.getSavePoint());
280 }
281 if (MFI.getRestorePoint()) {
282 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
283 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
284 .printMBBReference(*MFI.getRestorePoint());
285 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000286}
287
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000288void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000289 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000290 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000291 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000292 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000293 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000294 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000295 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
296 if (MFI.isDeadObjectIndex(I))
297 continue;
298
299 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000300 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000301 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
302 ? yaml::FixedMachineStackObject::SpillSlot
303 : yaml::FixedMachineStackObject::DefaultType;
304 YamlObject.Offset = MFI.getObjectOffset(I);
305 YamlObject.Size = MFI.getObjectSize(I);
306 YamlObject.Alignment = MFI.getObjectAlignment(I);
307 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
308 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
309 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000310 StackObjectOperandMapping.insert(
311 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000312 }
313
314 // Process ordinary stack objects.
315 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000316 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
317 if (MFI.isDeadObjectIndex(I))
318 continue;
319
320 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000321 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000322 if (const auto *Alloca = MFI.getObjectAllocation(I))
323 YamlObject.Name.Value =
324 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000325 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
326 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000327 : MFI.isVariableSizedObjectIndex(I)
328 ? yaml::MachineStackObject::VariableSized
329 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000330 YamlObject.Offset = MFI.getObjectOffset(I);
331 YamlObject.Size = MFI.getObjectSize(I);
332 YamlObject.Alignment = MFI.getObjectAlignment(I);
333
334 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000335 StackObjectOperandMapping.insert(std::make_pair(
336 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000337 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000338
339 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
340 yaml::StringValue Reg;
341 printReg(CSInfo.getReg(), Reg, TRI);
342 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
343 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
344 "Invalid stack object index");
345 const FrameIndexOperand &StackObject = StackObjectInfo->second;
346 if (StackObject.IsFixed)
347 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
348 else
349 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
350 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000351 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
352 auto LocalObject = MFI.getLocalFrameObjectMap(I);
353 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
354 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
355 "Invalid stack object index");
356 const FrameIndexOperand &StackObject = StackObjectInfo->second;
357 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
358 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
359 }
Alex Lorenza314d812015-08-18 22:26:26 +0000360
361 // Print the stack object references in the frame information class after
362 // converting the stack objects.
363 if (MFI.hasStackProtectorIndex()) {
364 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
365 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
366 .printStackObjectReference(MFI.getStackProtectorIndex());
367 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000368
369 // Print the debug variable information.
370 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
371 MMI.getVariableDbgInfo()) {
372 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
373 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
374 "Invalid stack object index");
375 const FrameIndexOperand &StackObject = StackObjectInfo->second;
376 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
377 auto &Object = MF.StackObjects[StackObject.ID];
378 {
379 raw_string_ostream StrOS(Object.DebugVar.Value);
380 DebugVar.Var->printAsOperand(StrOS, MST);
381 }
382 {
383 raw_string_ostream StrOS(Object.DebugExpr.Value);
384 DebugVar.Expr->printAsOperand(StrOS, MST);
385 }
386 {
387 raw_string_ostream StrOS(Object.DebugLoc.Value);
388 DebugVar.Loc->printAsOperand(StrOS, MST);
389 }
390 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000391}
392
Alex Lorenzab980492015-07-20 20:51:18 +0000393void MIRPrinter::convert(yaml::MachineFunction &MF,
394 const MachineConstantPool &ConstantPool) {
395 unsigned ID = 0;
396 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
397 // TODO: Serialize target specific constant pool entries.
398 if (Constant.isMachineConstantPoolEntry())
399 llvm_unreachable("Can't print target specific constant pool entries yet");
400
401 yaml::MachineConstantPoolValue YamlConstant;
402 std::string Str;
403 raw_string_ostream StrOS(Str);
404 Constant.Val.ConstVal->printAsOperand(StrOS);
405 YamlConstant.ID = ID++;
406 YamlConstant.Value = StrOS.str();
407 YamlConstant.Alignment = Constant.getAlignment();
408 MF.Constants.push_back(YamlConstant);
409 }
410}
411
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000412void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000413 yaml::MachineJumpTable &YamlJTI,
414 const MachineJumpTableInfo &JTI) {
415 YamlJTI.Kind = JTI.getEntryKind();
416 unsigned ID = 0;
417 for (const auto &Table : JTI.getJumpTables()) {
418 std::string Str;
419 yaml::MachineJumpTable::Entry Entry;
420 Entry.ID = ID++;
421 for (const auto *MBB : Table.MBBs) {
422 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000423 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
424 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000425 Entry.Blocks.push_back(StrOS.str());
426 Str.clear();
427 }
428 YamlJTI.Entries.push_back(Entry);
429 }
430}
431
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000432void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
433 const auto *TRI = MF.getSubtarget().getRegisterInfo();
434 unsigned I = 0;
435 for (const uint32_t *Mask : TRI->getRegMasks())
436 RegisterMaskIds.insert(std::make_pair(Mask, I++));
437}
438
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000439void MIPrinter::print(const MachineBasicBlock &MBB) {
440 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
441 OS << "bb." << MBB.getNumber();
442 bool HasAttributes = false;
443 if (const auto *BB = MBB.getBasicBlock()) {
444 if (BB->hasName()) {
445 OS << "." << BB->getName();
446 } else {
447 HasAttributes = true;
448 OS << " (";
449 int Slot = MST.getLocalSlot(BB);
450 if (Slot == -1)
451 OS << "<ir-block badref>";
452 else
453 OS << (Twine("%ir-block.") + Twine(Slot)).str();
454 }
455 }
456 if (MBB.hasAddressTaken()) {
457 OS << (HasAttributes ? ", " : " (");
458 OS << "address-taken";
459 HasAttributes = true;
460 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000461 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000462 OS << (HasAttributes ? ", " : " (");
463 OS << "landing-pad";
464 HasAttributes = true;
465 }
466 if (MBB.getAlignment()) {
467 OS << (HasAttributes ? ", " : " (");
468 OS << "align " << MBB.getAlignment();
469 HasAttributes = true;
470 }
471 if (HasAttributes)
472 OS << ")";
473 OS << ":\n";
474
475 bool HasLineAttributes = false;
476 // Print the successors
477 if (!MBB.succ_empty()) {
478 OS.indent(2) << "successors: ";
479 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
480 if (I != MBB.succ_begin())
481 OS << ", ";
482 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000483 if (MBB.hasSuccessorProbabilities())
484 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000485 }
486 OS << "\n";
487 HasLineAttributes = true;
488 }
489
490 // Print the live in registers.
491 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
492 assert(TRI && "Expected target register info");
493 if (!MBB.livein_empty()) {
494 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000495 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000496 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000497 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000498 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000499 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000500 printReg(LI.PhysReg, OS, TRI);
501 if (LI.LaneMask != ~0u)
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000502 OS << ":0x" << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000503 }
504 OS << "\n";
505 HasLineAttributes = true;
506 }
507
508 if (HasLineAttributes)
509 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000510 bool IsInBundle = false;
511 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
512 const MachineInstr &MI = *I;
513 if (IsInBundle && !MI.isInsideBundle()) {
514 OS.indent(2) << "}\n";
515 IsInBundle = false;
516 }
517 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000518 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000519 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
520 OS << " {";
521 IsInBundle = true;
522 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000523 OS << "\n";
524 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000525 if (IsInBundle)
526 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000527}
528
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000529/// Return true when an instruction has tied register that can't be determined
530/// by the instruction's descriptor.
531static bool hasComplexRegisterTies(const MachineInstr &MI) {
532 const MCInstrDesc &MCID = MI.getDesc();
533 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
534 const auto &Operand = MI.getOperand(I);
535 if (!Operand.isReg() || Operand.isDef())
536 // Ignore the defined registers as MCID marks only the uses as tied.
537 continue;
538 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
539 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
540 if (ExpectedTiedIdx != TiedIdx)
541 return true;
542 }
543 return false;
544}
545
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000546static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx,
547 SmallBitVector &PrintedTypes,
548 const MachineRegisterInfo &MRI) {
549 const MachineOperand &Op = MI.getOperand(OpIdx);
550 if (!Op.isReg())
551 return LLT{};
552
553 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands())
554 return MRI.getType(Op.getReg());
555
556 auto &OpInfo = MI.getDesc().OpInfo[OpIdx];
557 if (!OpInfo.isGenericType())
558 return MRI.getType(Op.getReg());
559
560 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
561 return LLT{};
562
563 PrintedTypes.set(OpInfo.getGenericTypeIndex());
564 return MRI.getType(Op.getReg());
565}
566
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000567void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000568 const auto *MF = MI.getParent()->getParent();
569 const auto &MRI = MF->getRegInfo();
570 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000571 const auto *TRI = SubTarget.getRegisterInfo();
572 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000573 const auto *TII = SubTarget.getInstrInfo();
574 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000575 if (MI.isCFIInstruction())
576 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000577
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000578 SmallBitVector PrintedTypes(8);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000579 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 unsigned I = 0, E = MI.getNumOperands();
581 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
582 !MI.getOperand(I).isImplicit();
583 ++I) {
584 if (I)
585 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000586 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
587 getTypeToPrint(MI, I, PrintedTypes, MRI),
Quentin Colombet4e14a492016-03-07 21:57:52 +0000588 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000589 }
590
591 if (I)
592 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000593 if (MI.getFlag(MachineInstr::FrameSetup))
594 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000595 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000596 if (I < E)
597 OS << ' ';
598
599 bool NeedComma = false;
600 for (; I < E; ++I) {
601 if (NeedComma)
602 OS << ", ";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000603 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies,
604 getTypeToPrint(MI, I, PrintedTypes, MRI));
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000605 NeedComma = true;
606 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000607
608 if (MI.getDebugLoc()) {
609 if (NeedComma)
610 OS << ',';
611 OS << " debug-location ";
612 MI.getDebugLoc()->printAsOperand(OS, MST);
613 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000614
615 if (!MI.memoperands_empty()) {
616 OS << " :: ";
617 bool NeedComma = false;
618 for (const auto *Op : MI.memoperands()) {
619 if (NeedComma)
620 OS << ", ";
621 print(*Op);
622 NeedComma = true;
623 }
624 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000625}
626
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000627void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
628 OS << "%bb." << MBB.getNumber();
629 if (const auto *BB = MBB.getBasicBlock()) {
630 if (BB->hasName())
631 OS << '.' << BB->getName();
632 }
633}
634
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000635static void printIRSlotNumber(raw_ostream &OS, int Slot) {
636 if (Slot == -1)
637 OS << "<badref>";
638 else
639 OS << Slot;
640}
641
Alex Lorenzdeb53492015-07-28 17:28:03 +0000642void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
643 OS << "%ir-block.";
644 if (BB.hasName()) {
645 printLLVMNameWithoutPrefix(OS, BB.getName());
646 return;
647 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000648 const Function *F = BB.getParent();
649 int Slot;
650 if (F == MST.getCurrentFunction()) {
651 Slot = MST.getLocalSlot(&BB);
652 } else {
653 ModuleSlotTracker CustomMST(F->getParent(),
654 /*ShouldInitializeAllMetadata=*/false);
655 CustomMST.incorporateFunction(*F);
656 Slot = CustomMST.getLocalSlot(&BB);
657 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000658 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000659}
660
Alex Lorenz4af7e612015-08-03 23:08:19 +0000661void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000662 if (isa<GlobalValue>(V)) {
663 V.printAsOperand(OS, /*PrintType=*/false, MST);
664 return;
665 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000666 if (isa<Constant>(V)) {
667 // Machine memory operands can load/store to/from constant value pointers.
668 OS << '`';
669 V.printAsOperand(OS, /*PrintType=*/true, MST);
670 OS << '`';
671 return;
672 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000673 OS << "%ir.";
674 if (V.hasName()) {
675 printLLVMNameWithoutPrefix(OS, V.getName());
676 return;
677 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000678 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000679}
680
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000681void MIPrinter::printStackObjectReference(int FrameIndex) {
682 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
683 assert(ObjectInfo != StackObjectOperandMapping.end() &&
684 "Invalid frame index");
685 const FrameIndexOperand &Operand = ObjectInfo->second;
686 if (Operand.IsFixed) {
687 OS << "%fixed-stack." << Operand.ID;
688 return;
689 }
690 OS << "%stack." << Operand.ID;
691 if (!Operand.Name.empty())
692 OS << '.' << Operand.Name;
693}
694
Alex Lorenz5672a892015-08-05 22:26:15 +0000695void MIPrinter::printOffset(int64_t Offset) {
696 if (Offset == 0)
697 return;
698 if (Offset < 0) {
699 OS << " - " << -Offset;
700 return;
701 }
702 OS << " + " << Offset;
703}
704
Alex Lorenz49873a82015-08-06 00:44:07 +0000705static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
706 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
707 for (const auto &I : Flags) {
708 if (I.first == TF) {
709 return I.second;
710 }
711 }
712 return nullptr;
713}
714
715void MIPrinter::printTargetFlags(const MachineOperand &Op) {
716 if (!Op.getTargetFlags())
717 return;
718 const auto *TII =
719 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
720 assert(TII && "expected instruction info");
721 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
722 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000723 const bool HasDirectFlags = Flags.first;
724 const bool HasBitmaskFlags = Flags.second;
725 if (!HasDirectFlags && !HasBitmaskFlags) {
726 OS << "<unknown>) ";
727 return;
728 }
729 if (HasDirectFlags) {
730 if (const auto *Name = getTargetFlagName(TII, Flags.first))
731 OS << Name;
732 else
733 OS << "<unknown target flag>";
734 }
735 if (!HasBitmaskFlags) {
736 OS << ") ";
737 return;
738 }
739 bool IsCommaNeeded = HasDirectFlags;
740 unsigned BitMask = Flags.second;
741 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
742 for (const auto &Mask : BitMasks) {
743 // Check if the flag's bitmask has the bits of the current mask set.
744 if ((BitMask & Mask.first) == Mask.first) {
745 if (IsCommaNeeded)
746 OS << ", ";
747 IsCommaNeeded = true;
748 OS << Mask.second;
749 // Clear the bits which were serialized from the flag's bitmask.
750 BitMask &= ~(Mask.first);
751 }
752 }
753 if (BitMask) {
754 // When the resulting flag's bitmask isn't zero, we know that we didn't
755 // serialize all of the bit flags.
756 if (IsCommaNeeded)
757 OS << ", ";
758 OS << "<unknown bitmask target flag>";
759 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000760 OS << ") ";
761}
762
Alex Lorenzef5c1962015-07-28 23:02:45 +0000763static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
764 const auto *TII = MF.getSubtarget().getInstrInfo();
765 assert(TII && "expected instruction info");
766 auto Indices = TII->getSerializableTargetIndices();
767 for (const auto &I : Indices) {
768 if (I.first == Index) {
769 return I.second;
770 }
771 }
772 return nullptr;
773}
774
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000775void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000776 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint,
777 bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000778 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000779 switch (Op.getType()) {
780 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000781 if (Op.isImplicit())
782 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000783 else if (!IsDef && Op.isDef())
784 // Print the 'def' flag only when the operand is defined after '='.
785 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000786 if (Op.isInternalRead())
787 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000788 if (Op.isDead())
789 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000790 if (Op.isKill())
791 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000792 if (Op.isUndef())
793 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000794 if (Op.isEarlyClobber())
795 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000796 if (Op.isDebug())
797 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000798 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000799 // Print the sub register.
800 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000801 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000802 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
803 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Tim Northoverd28d3cc2016-09-12 11:20:10 +0000804 if (TypeToPrint.isValid())
805 OS << '(' << TypeToPrint << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000806 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000807 case MachineOperand::MO_Immediate:
808 OS << Op.getImm();
809 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000810 case MachineOperand::MO_CImmediate:
811 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
812 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000813 case MachineOperand::MO_FPImmediate:
814 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
815 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000816 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000817 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000818 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000819 case MachineOperand::MO_FrameIndex:
820 printStackObjectReference(Op.getIndex());
821 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000822 case MachineOperand::MO_ConstantPoolIndex:
823 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000824 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000825 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000826 case MachineOperand::MO_TargetIndex: {
827 OS << "target-index(";
828 if (const auto *Name = getTargetIndexName(
829 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
830 OS << Name;
831 else
832 OS << "<unknown>";
833 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000834 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000835 break;
836 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000837 case MachineOperand::MO_JumpTableIndex:
838 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000839 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000840 case MachineOperand::MO_ExternalSymbol:
841 OS << '$';
842 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000843 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000844 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000845 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000846 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000847 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000848 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000849 case MachineOperand::MO_BlockAddress:
850 OS << "blockaddress(";
851 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
852 MST);
853 OS << ", ";
854 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
855 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000856 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000857 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000858 case MachineOperand::MO_RegisterMask: {
859 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
860 if (RegMaskInfo != RegisterMaskIds.end())
861 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
862 else
863 llvm_unreachable("Can't print this machine register mask yet.");
864 break;
865 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000866 case MachineOperand::MO_RegisterLiveOut: {
867 const uint32_t *RegMask = Op.getRegLiveOut();
868 OS << "liveout(";
869 bool IsCommaNeeded = false;
870 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
871 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
872 if (IsCommaNeeded)
873 OS << ", ";
874 printReg(Reg, OS, TRI);
875 IsCommaNeeded = true;
876 }
877 }
878 OS << ")";
879 break;
880 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000881 case MachineOperand::MO_Metadata:
882 Op.getMetadata()->printAsOperand(OS, MST);
883 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000884 case MachineOperand::MO_MCSymbol:
885 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
886 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000887 case MachineOperand::MO_CFIIndex: {
888 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000889 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000890 break;
891 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000892 case MachineOperand::MO_IntrinsicID: {
893 Intrinsic::ID ID = Op.getIntrinsicID();
894 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000895 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000896 else {
897 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
898 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
899 OS << "intrinsic(@" << TII->getName(ID) << ')';
900 }
901 break;
902 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000903 case MachineOperand::MO_Predicate: {
904 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
905 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
906 << CmpInst::getPredicateName(Pred) << ')';
907 break;
908 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000909 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000910}
911
Alex Lorenz4af7e612015-08-03 23:08:19 +0000912void MIPrinter::print(const MachineMemOperand &Op) {
913 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000914 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000915 if (Op.isVolatile())
916 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000917 if (Op.isNonTemporal())
918 OS << "non-temporal ";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000919 if (Op.isDereferenceable())
920 OS << "dereferenceable ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000921 if (Op.isInvariant())
922 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000923 if (Op.isLoad())
924 OS << "load ";
925 else {
926 assert(Op.isStore() && "Non load machine operand must be a store");
927 OS << "store ";
928 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000929 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000930 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000931 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000932 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000933 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
934 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000935 assert(PVal && "Expected a pseudo source value");
936 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000937 case PseudoSourceValue::Stack:
938 OS << "stack";
939 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000940 case PseudoSourceValue::GOT:
941 OS << "got";
942 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000943 case PseudoSourceValue::JumpTable:
944 OS << "jump-table";
945 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000946 case PseudoSourceValue::ConstantPool:
947 OS << "constant-pool";
948 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000949 case PseudoSourceValue::FixedStack:
950 printStackObjectReference(
951 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
952 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000953 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000954 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000955 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
956 OS, /*PrintType=*/false, MST);
957 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000958 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000959 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000960 printLLVMNameWithoutPrefix(
961 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000962 break;
963 }
964 }
Alex Lorenz83127732015-08-07 20:26:52 +0000965 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000966 if (Op.getBaseAlignment() != Op.getSize())
967 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000968 auto AAInfo = Op.getAAInfo();
969 if (AAInfo.TBAA) {
970 OS << ", !tbaa ";
971 AAInfo.TBAA->printAsOperand(OS, MST);
972 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000973 if (AAInfo.Scope) {
974 OS << ", !alias.scope ";
975 AAInfo.Scope->printAsOperand(OS, MST);
976 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000977 if (AAInfo.NoAlias) {
978 OS << ", !noalias ";
979 AAInfo.NoAlias->printAsOperand(OS, MST);
980 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000981 if (Op.getRanges()) {
982 OS << ", !range ";
983 Op.getRanges()->printAsOperand(OS, MST);
984 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000985 OS << ')';
986}
987
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000988static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
989 const TargetRegisterInfo *TRI) {
990 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
991 if (Reg == -1) {
992 OS << "<badreg>";
993 return;
994 }
995 printReg(Reg, OS, TRI);
996}
997
998void MIPrinter::print(const MCCFIInstruction &CFI,
999 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001000 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001001 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +00001002 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +00001003 if (CFI.getLabel())
1004 OS << "<mcsymbol> ";
1005 printCFIRegister(CFI.getRegister(), OS, TRI);
1006 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001007 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001008 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001009 if (CFI.getLabel())
1010 OS << "<mcsymbol> ";
1011 printCFIRegister(CFI.getRegister(), OS, TRI);
1012 OS << ", " << CFI.getOffset();
1013 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001014 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001015 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001016 if (CFI.getLabel())
1017 OS << "<mcsymbol> ";
1018 printCFIRegister(CFI.getRegister(), OS, TRI);
1019 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001020 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001021 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001022 if (CFI.getLabel())
1023 OS << "<mcsymbol> ";
1024 OS << CFI.getOffset();
1025 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001026 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001027 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001028 if (CFI.getLabel())
1029 OS << "<mcsymbol> ";
1030 printCFIRegister(CFI.getRegister(), OS, TRI);
1031 OS << ", " << CFI.getOffset();
1032 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001033 default:
1034 // TODO: Print the other CFI Operations.
1035 OS << "<unserializable cfi operation>";
1036 break;
1037 }
1038}
1039
Alex Lorenz345c1442015-06-15 23:52:35 +00001040void llvm::printMIR(raw_ostream &OS, const Module &M) {
1041 yaml::Output Out(OS);
1042 Out << const_cast<Module &>(M);
1043}
1044
1045void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1046 MIRPrinter Printer(OS);
1047 Printer.print(MF);
1048}