blob: 7b9baa7db0a730e500898de1348ccd3b778f2440 [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"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000017#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
18#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000021#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000025#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000026#include "llvm/IR/Constants.h"
Reid Kleckner28865802016-04-14 18:29:59 +000027#include "llvm/IR/DebugInfo.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000028#include "llvm/IR/IRPrintingPasses.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000029#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000030#include "llvm/IR/Intrinsics.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000031#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000032#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000033#include "llvm/MC/MCSymbol.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000034#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000035#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000036#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000037#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000038#include "llvm/Target/TargetIntrinsicInfo.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000040
41using namespace llvm;
42
43namespace {
44
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000045/// This structure describes how to print out stack object references.
46struct FrameIndexOperand {
47 std::string Name;
48 unsigned ID;
49 bool IsFixed;
50
51 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
52 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
53
54 /// Return an ordinary stack object reference.
55 static FrameIndexOperand create(StringRef Name, unsigned ID) {
56 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
57 }
58
59 /// Return a fixed stack object reference.
60 static FrameIndexOperand createFixed(unsigned ID) {
61 return FrameIndexOperand("", ID, /*IsFixed=*/true);
62 }
63};
64
Alex Lorenz618b2832015-07-30 16:54:38 +000065} // end anonymous namespace
66
67namespace llvm {
68
Alex Lorenz345c1442015-06-15 23:52:35 +000069/// This class prints out the machine functions using the MIR serialization
70/// format.
71class MIRPrinter {
72 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000073 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000074 /// Maps from stack object indices to operand indices which will be used when
75 /// printing frame index machine operands.
76 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000077
78public:
79 MIRPrinter(raw_ostream &OS) : OS(OS) {}
80
81 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000082
Alex Lorenz28148ba2015-07-09 22:23:13 +000083 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
84 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000085 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
86 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000087 void convert(yaml::MachineFunction &MF,
88 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000089 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
90 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000091 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000092 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
93 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000094 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000095
96private:
97 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000098};
99
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000100/// This class prints out the machine instructions using the MIR serialization
101/// format.
102class MIPrinter {
103 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000104 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000105 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000106 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000107
108public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000109 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000110 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
111 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
112 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
113 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000114
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000115 void print(const MachineBasicBlock &MBB);
116
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000117 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000118 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000119 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000120 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000121 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000122 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000123 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000124 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000125 unsigned I, bool ShouldPrintRegisterTies,
126 const MachineRegisterInfo *MRI = nullptr, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000127 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000128
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000129 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000130};
131
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000132} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000133
134namespace llvm {
135namespace yaml {
136
137/// This struct serializes the LLVM IR module.
138template <> struct BlockScalarTraits<Module> {
139 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
140 Mod.print(OS, nullptr);
141 }
142 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
143 llvm_unreachable("LLVM Module is supposed to be parsed separately");
144 return "";
145 }
146};
147
148} // end namespace yaml
149} // end namespace llvm
150
Alex Lorenz15a00a82015-07-14 21:18:25 +0000151static void printReg(unsigned Reg, raw_ostream &OS,
152 const TargetRegisterInfo *TRI) {
153 // TODO: Print Stack Slots.
154 if (!Reg)
155 OS << '_';
156 else if (TargetRegisterInfo::isVirtualRegister(Reg))
157 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
158 else if (Reg < TRI->getNumRegs())
159 OS << '%' << StringRef(TRI->getName(Reg)).lower();
160 else
161 llvm_unreachable("Can't print this kind of register yet");
162}
163
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000164static void printReg(unsigned Reg, yaml::StringValue &Dest,
165 const TargetRegisterInfo *TRI) {
166 raw_string_ostream OS(Dest.Value);
167 printReg(Reg, OS, TRI);
168}
169
Alex Lorenz345c1442015-06-15 23:52:35 +0000170void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000171 initRegisterMaskIds(MF);
172
Alex Lorenz345c1442015-06-15 23:52:35 +0000173 yaml::MachineFunction YamlMF;
174 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000175 YamlMF.Alignment = MF.getAlignment();
176 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
Derek Schuffad154c82016-03-28 17:05:30 +0000177
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000178 YamlMF.Legalized = MF.getProperties().hasProperty(
179 MachineFunctionProperties::Property::Legalized);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000180 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
181 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000182 YamlMF.Selected = MF.getProperties().hasProperty(
183 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000184
Alex Lorenz28148ba2015-07-09 22:23:13 +0000185 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000186 ModuleSlotTracker MST(MF.getFunction()->getParent());
187 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000188 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
189 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000190 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000191 if (const auto *ConstantPool = MF.getConstantPool())
192 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000193 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
194 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000195 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
196 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000197 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000198 if (IsNewlineNeeded)
199 StrOS << "\n";
200 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
201 .print(MBB);
202 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000203 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000204 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000205 yaml::Output Out(OS);
206 Out << YamlMF;
207}
208
Alex Lorenz54565cf2015-06-24 19:56:10 +0000209void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000210 const MachineRegisterInfo &RegInfo,
211 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000212 MF.TracksRegLiveness = RegInfo.tracksLiveness();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000213
214 // Print the virtual register definitions.
215 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
216 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
217 yaml::VirtualRegisterDefinition VReg;
218 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000219 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000220 VReg.Class =
221 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000222 else if (RegInfo.getRegBankOrNull(Reg))
223 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000224 else {
225 VReg.Class = std::string("_");
226 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
227 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000228 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
229 if (PreferredReg)
230 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000231 MF.VirtualRegisters.push_back(VReg);
232 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000233
234 // Print the live ins.
235 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
236 yaml::MachineFunctionLiveIn LiveIn;
237 printReg(I->first, LiveIn.Register, TRI);
238 if (I->second)
239 printReg(I->second, LiveIn.VirtualRegister, TRI);
240 MF.LiveIns.push_back(LiveIn);
241 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000242 // The used physical register mask is printed as an inverted callee saved
243 // register mask.
244 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
245 if (UsedPhysRegMask.none())
246 return;
247 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
248 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
249 if (!UsedPhysRegMask[I]) {
250 yaml::FlowStringValue Reg;
251 printReg(I, Reg, TRI);
252 CalleeSavedRegisters.push_back(Reg);
253 }
254 }
255 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000256}
257
Alex Lorenza6f9a372015-07-29 21:09:09 +0000258void MIRPrinter::convert(ModuleSlotTracker &MST,
259 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000260 const MachineFrameInfo &MFI) {
261 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
262 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
263 YamlMFI.HasStackMap = MFI.hasStackMap();
264 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
265 YamlMFI.StackSize = MFI.getStackSize();
266 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
267 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
268 YamlMFI.AdjustsStack = MFI.adjustsStack();
269 YamlMFI.HasCalls = MFI.hasCalls();
270 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
271 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
272 YamlMFI.HasVAStart = MFI.hasVAStart();
273 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000274 if (MFI.getSavePoint()) {
275 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
276 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
277 .printMBBReference(*MFI.getSavePoint());
278 }
279 if (MFI.getRestorePoint()) {
280 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
281 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
282 .printMBBReference(*MFI.getRestorePoint());
283 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000284}
285
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000286void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000287 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000288 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000289 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000290 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000291 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000292 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000293 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
294 if (MFI.isDeadObjectIndex(I))
295 continue;
296
297 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000298 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000299 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
300 ? yaml::FixedMachineStackObject::SpillSlot
301 : yaml::FixedMachineStackObject::DefaultType;
302 YamlObject.Offset = MFI.getObjectOffset(I);
303 YamlObject.Size = MFI.getObjectSize(I);
304 YamlObject.Alignment = MFI.getObjectAlignment(I);
305 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
306 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
307 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000308 StackObjectOperandMapping.insert(
309 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000310 }
311
312 // Process ordinary stack objects.
313 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000314 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
315 if (MFI.isDeadObjectIndex(I))
316 continue;
317
318 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000319 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000320 if (const auto *Alloca = MFI.getObjectAllocation(I))
321 YamlObject.Name.Value =
322 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000323 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
324 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000325 : MFI.isVariableSizedObjectIndex(I)
326 ? yaml::MachineStackObject::VariableSized
327 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000328 YamlObject.Offset = MFI.getObjectOffset(I);
329 YamlObject.Size = MFI.getObjectSize(I);
330 YamlObject.Alignment = MFI.getObjectAlignment(I);
331
332 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000333 StackObjectOperandMapping.insert(std::make_pair(
334 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000335 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000336
337 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
338 yaml::StringValue Reg;
339 printReg(CSInfo.getReg(), Reg, TRI);
340 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
341 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
342 "Invalid stack object index");
343 const FrameIndexOperand &StackObject = StackObjectInfo->second;
344 if (StackObject.IsFixed)
345 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
346 else
347 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
348 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000349 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
350 auto LocalObject = MFI.getLocalFrameObjectMap(I);
351 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
352 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
353 "Invalid stack object index");
354 const FrameIndexOperand &StackObject = StackObjectInfo->second;
355 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
356 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
357 }
Alex Lorenza314d812015-08-18 22:26:26 +0000358
359 // Print the stack object references in the frame information class after
360 // converting the stack objects.
361 if (MFI.hasStackProtectorIndex()) {
362 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
363 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
364 .printStackObjectReference(MFI.getStackProtectorIndex());
365 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000366
367 // Print the debug variable information.
368 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
369 MMI.getVariableDbgInfo()) {
370 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
371 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
372 "Invalid stack object index");
373 const FrameIndexOperand &StackObject = StackObjectInfo->second;
374 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
375 auto &Object = MF.StackObjects[StackObject.ID];
376 {
377 raw_string_ostream StrOS(Object.DebugVar.Value);
378 DebugVar.Var->printAsOperand(StrOS, MST);
379 }
380 {
381 raw_string_ostream StrOS(Object.DebugExpr.Value);
382 DebugVar.Expr->printAsOperand(StrOS, MST);
383 }
384 {
385 raw_string_ostream StrOS(Object.DebugLoc.Value);
386 DebugVar.Loc->printAsOperand(StrOS, MST);
387 }
388 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000389}
390
Alex Lorenzab980492015-07-20 20:51:18 +0000391void MIRPrinter::convert(yaml::MachineFunction &MF,
392 const MachineConstantPool &ConstantPool) {
393 unsigned ID = 0;
394 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
395 // TODO: Serialize target specific constant pool entries.
396 if (Constant.isMachineConstantPoolEntry())
397 llvm_unreachable("Can't print target specific constant pool entries yet");
398
399 yaml::MachineConstantPoolValue YamlConstant;
400 std::string Str;
401 raw_string_ostream StrOS(Str);
402 Constant.Val.ConstVal->printAsOperand(StrOS);
403 YamlConstant.ID = ID++;
404 YamlConstant.Value = StrOS.str();
405 YamlConstant.Alignment = Constant.getAlignment();
406 MF.Constants.push_back(YamlConstant);
407 }
408}
409
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000410void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000411 yaml::MachineJumpTable &YamlJTI,
412 const MachineJumpTableInfo &JTI) {
413 YamlJTI.Kind = JTI.getEntryKind();
414 unsigned ID = 0;
415 for (const auto &Table : JTI.getJumpTables()) {
416 std::string Str;
417 yaml::MachineJumpTable::Entry Entry;
418 Entry.ID = ID++;
419 for (const auto *MBB : Table.MBBs) {
420 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000421 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
422 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000423 Entry.Blocks.push_back(StrOS.str());
424 Str.clear();
425 }
426 YamlJTI.Entries.push_back(Entry);
427 }
428}
429
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000430void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
431 const auto *TRI = MF.getSubtarget().getRegisterInfo();
432 unsigned I = 0;
433 for (const uint32_t *Mask : TRI->getRegMasks())
434 RegisterMaskIds.insert(std::make_pair(Mask, I++));
435}
436
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000437void MIPrinter::print(const MachineBasicBlock &MBB) {
438 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
439 OS << "bb." << MBB.getNumber();
440 bool HasAttributes = false;
441 if (const auto *BB = MBB.getBasicBlock()) {
442 if (BB->hasName()) {
443 OS << "." << BB->getName();
444 } else {
445 HasAttributes = true;
446 OS << " (";
447 int Slot = MST.getLocalSlot(BB);
448 if (Slot == -1)
449 OS << "<ir-block badref>";
450 else
451 OS << (Twine("%ir-block.") + Twine(Slot)).str();
452 }
453 }
454 if (MBB.hasAddressTaken()) {
455 OS << (HasAttributes ? ", " : " (");
456 OS << "address-taken";
457 HasAttributes = true;
458 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000459 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000460 OS << (HasAttributes ? ", " : " (");
461 OS << "landing-pad";
462 HasAttributes = true;
463 }
464 if (MBB.getAlignment()) {
465 OS << (HasAttributes ? ", " : " (");
466 OS << "align " << MBB.getAlignment();
467 HasAttributes = true;
468 }
469 if (HasAttributes)
470 OS << ")";
471 OS << ":\n";
472
473 bool HasLineAttributes = false;
474 // Print the successors
475 if (!MBB.succ_empty()) {
476 OS.indent(2) << "successors: ";
477 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
478 if (I != MBB.succ_begin())
479 OS << ", ";
480 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000481 if (MBB.hasSuccessorProbabilities())
482 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000483 }
484 OS << "\n";
485 HasLineAttributes = true;
486 }
487
488 // Print the live in registers.
489 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
490 assert(TRI && "Expected target register info");
491 if (!MBB.livein_empty()) {
492 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000493 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000494 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000495 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000496 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000497 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000498 printReg(LI.PhysReg, OS, TRI);
499 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000500 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000501 }
502 OS << "\n";
503 HasLineAttributes = true;
504 }
505
506 if (HasLineAttributes)
507 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000508 bool IsInBundle = false;
509 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
510 const MachineInstr &MI = *I;
511 if (IsInBundle && !MI.isInsideBundle()) {
512 OS.indent(2) << "}\n";
513 IsInBundle = false;
514 }
515 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000516 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000517 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
518 OS << " {";
519 IsInBundle = true;
520 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521 OS << "\n";
522 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000523 if (IsInBundle)
524 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000525}
526
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000527/// Return true when an instruction has tied register that can't be determined
528/// by the instruction's descriptor.
529static bool hasComplexRegisterTies(const MachineInstr &MI) {
530 const MCInstrDesc &MCID = MI.getDesc();
531 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
532 const auto &Operand = MI.getOperand(I);
533 if (!Operand.isReg() || Operand.isDef())
534 // Ignore the defined registers as MCID marks only the uses as tied.
535 continue;
536 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
537 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
538 if (ExpectedTiedIdx != TiedIdx)
539 return true;
540 }
541 return false;
542}
543
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000544void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000545 const auto *MF = MI.getParent()->getParent();
546 const auto &MRI = MF->getRegInfo();
547 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 const auto *TRI = SubTarget.getRegisterInfo();
549 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000550 const auto *TII = SubTarget.getInstrInfo();
551 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000552 if (MI.isCFIInstruction())
553 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000554
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000555 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000556 unsigned I = 0, E = MI.getNumOperands();
557 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
558 !MI.getOperand(I).isImplicit();
559 ++I) {
560 if (I)
561 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000562 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
563 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000564 }
565
566 if (I)
567 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000568 if (MI.getFlag(MachineInstr::FrameSetup))
569 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000570 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000571 if (isPreISelGenericOpcode(MI.getOpcode())) {
Tim Northover62ae5682016-07-20 19:09:30 +0000572 assert(MI.getType().isValid() && "Generic instructions must have a type");
Tim Northover26e40bd2016-07-26 17:28:01 +0000573 unsigned NumTypes = MI.getNumTypes();
574 OS << (NumTypes > 1 ? " {" : "") << ' ';
575 for (unsigned i = 0; i < NumTypes; ++i) {
Tim Northover7c9eba92016-07-25 21:01:29 +0000576 MI.getType(i).print(OS);
Tim Northover26e40bd2016-07-26 17:28:01 +0000577 if (i + 1 != NumTypes)
Tim Northover98a56eb2016-07-22 22:13:36 +0000578 OS << ", ";
579 }
Tim Northover26e40bd2016-07-26 17:28:01 +0000580 OS << (NumTypes > 1 ? " }" : "") << ' ';
Quentin Colombet12350a82016-03-08 00:29:15 +0000581 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000582 if (I < E)
583 OS << ' ';
584
585 bool NeedComma = false;
586 for (; I < E; ++I) {
587 if (NeedComma)
588 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000589 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000590 NeedComma = true;
591 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000592
593 if (MI.getDebugLoc()) {
594 if (NeedComma)
595 OS << ',';
596 OS << " debug-location ";
597 MI.getDebugLoc()->printAsOperand(OS, MST);
598 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000599
600 if (!MI.memoperands_empty()) {
601 OS << " :: ";
602 bool NeedComma = false;
603 for (const auto *Op : MI.memoperands()) {
604 if (NeedComma)
605 OS << ", ";
606 print(*Op);
607 NeedComma = true;
608 }
609 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000610}
611
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000612void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
613 OS << "%bb." << MBB.getNumber();
614 if (const auto *BB = MBB.getBasicBlock()) {
615 if (BB->hasName())
616 OS << '.' << BB->getName();
617 }
618}
619
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000620static void printIRSlotNumber(raw_ostream &OS, int Slot) {
621 if (Slot == -1)
622 OS << "<badref>";
623 else
624 OS << Slot;
625}
626
Alex Lorenzdeb53492015-07-28 17:28:03 +0000627void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
628 OS << "%ir-block.";
629 if (BB.hasName()) {
630 printLLVMNameWithoutPrefix(OS, BB.getName());
631 return;
632 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000633 const Function *F = BB.getParent();
634 int Slot;
635 if (F == MST.getCurrentFunction()) {
636 Slot = MST.getLocalSlot(&BB);
637 } else {
638 ModuleSlotTracker CustomMST(F->getParent(),
639 /*ShouldInitializeAllMetadata=*/false);
640 CustomMST.incorporateFunction(*F);
641 Slot = CustomMST.getLocalSlot(&BB);
642 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000643 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000644}
645
Alex Lorenz4af7e612015-08-03 23:08:19 +0000646void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000647 if (isa<GlobalValue>(V)) {
648 V.printAsOperand(OS, /*PrintType=*/false, MST);
649 return;
650 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000651 if (isa<Constant>(V)) {
652 // Machine memory operands can load/store to/from constant value pointers.
653 OS << '`';
654 V.printAsOperand(OS, /*PrintType=*/true, MST);
655 OS << '`';
656 return;
657 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000658 OS << "%ir.";
659 if (V.hasName()) {
660 printLLVMNameWithoutPrefix(OS, V.getName());
661 return;
662 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000663 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000664}
665
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000666void MIPrinter::printStackObjectReference(int FrameIndex) {
667 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
668 assert(ObjectInfo != StackObjectOperandMapping.end() &&
669 "Invalid frame index");
670 const FrameIndexOperand &Operand = ObjectInfo->second;
671 if (Operand.IsFixed) {
672 OS << "%fixed-stack." << Operand.ID;
673 return;
674 }
675 OS << "%stack." << Operand.ID;
676 if (!Operand.Name.empty())
677 OS << '.' << Operand.Name;
678}
679
Alex Lorenz5672a892015-08-05 22:26:15 +0000680void MIPrinter::printOffset(int64_t Offset) {
681 if (Offset == 0)
682 return;
683 if (Offset < 0) {
684 OS << " - " << -Offset;
685 return;
686 }
687 OS << " + " << Offset;
688}
689
Alex Lorenz49873a82015-08-06 00:44:07 +0000690static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
691 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
692 for (const auto &I : Flags) {
693 if (I.first == TF) {
694 return I.second;
695 }
696 }
697 return nullptr;
698}
699
700void MIPrinter::printTargetFlags(const MachineOperand &Op) {
701 if (!Op.getTargetFlags())
702 return;
703 const auto *TII =
704 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
705 assert(TII && "expected instruction info");
706 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
707 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000708 const bool HasDirectFlags = Flags.first;
709 const bool HasBitmaskFlags = Flags.second;
710 if (!HasDirectFlags && !HasBitmaskFlags) {
711 OS << "<unknown>) ";
712 return;
713 }
714 if (HasDirectFlags) {
715 if (const auto *Name = getTargetFlagName(TII, Flags.first))
716 OS << Name;
717 else
718 OS << "<unknown target flag>";
719 }
720 if (!HasBitmaskFlags) {
721 OS << ") ";
722 return;
723 }
724 bool IsCommaNeeded = HasDirectFlags;
725 unsigned BitMask = Flags.second;
726 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
727 for (const auto &Mask : BitMasks) {
728 // Check if the flag's bitmask has the bits of the current mask set.
729 if ((BitMask & Mask.first) == Mask.first) {
730 if (IsCommaNeeded)
731 OS << ", ";
732 IsCommaNeeded = true;
733 OS << Mask.second;
734 // Clear the bits which were serialized from the flag's bitmask.
735 BitMask &= ~(Mask.first);
736 }
737 }
738 if (BitMask) {
739 // When the resulting flag's bitmask isn't zero, we know that we didn't
740 // serialize all of the bit flags.
741 if (IsCommaNeeded)
742 OS << ", ";
743 OS << "<unknown bitmask target flag>";
744 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000745 OS << ") ";
746}
747
Alex Lorenzef5c1962015-07-28 23:02:45 +0000748static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
749 const auto *TII = MF.getSubtarget().getInstrInfo();
750 assert(TII && "expected instruction info");
751 auto Indices = TII->getSerializableTargetIndices();
752 for (const auto &I : Indices) {
753 if (I.first == Index) {
754 return I.second;
755 }
756 }
757 return nullptr;
758}
759
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000760void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000761 unsigned I, bool ShouldPrintRegisterTies,
762 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000763 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000764 switch (Op.getType()) {
765 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000766 if (Op.isImplicit())
767 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000768 else if (!IsDef && Op.isDef())
769 // Print the 'def' flag only when the operand is defined after '='.
770 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000771 if (Op.isInternalRead())
772 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000773 if (Op.isDead())
774 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000775 if (Op.isKill())
776 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000777 if (Op.isUndef())
778 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000779 if (Op.isEarlyClobber())
780 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000781 if (Op.isDebug())
782 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000783 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000784 // Print the sub register.
785 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000786 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000787 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
788 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000789 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
790 if (IsDef && MRI->getSize(Op.getReg()))
791 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000792 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000793 case MachineOperand::MO_Immediate:
794 OS << Op.getImm();
795 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000796 case MachineOperand::MO_CImmediate:
797 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
798 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000799 case MachineOperand::MO_FPImmediate:
800 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
801 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000802 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000803 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000804 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000805 case MachineOperand::MO_FrameIndex:
806 printStackObjectReference(Op.getIndex());
807 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000808 case MachineOperand::MO_ConstantPoolIndex:
809 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000810 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000811 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000812 case MachineOperand::MO_TargetIndex: {
813 OS << "target-index(";
814 if (const auto *Name = getTargetIndexName(
815 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
816 OS << Name;
817 else
818 OS << "<unknown>";
819 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000820 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000821 break;
822 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000823 case MachineOperand::MO_JumpTableIndex:
824 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000825 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000826 case MachineOperand::MO_ExternalSymbol:
827 OS << '$';
828 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000829 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000830 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000831 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000832 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000833 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000834 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000835 case MachineOperand::MO_BlockAddress:
836 OS << "blockaddress(";
837 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
838 MST);
839 OS << ", ";
840 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
841 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000842 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000843 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000844 case MachineOperand::MO_RegisterMask: {
845 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
846 if (RegMaskInfo != RegisterMaskIds.end())
847 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
848 else
849 llvm_unreachable("Can't print this machine register mask yet.");
850 break;
851 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000852 case MachineOperand::MO_RegisterLiveOut: {
853 const uint32_t *RegMask = Op.getRegLiveOut();
854 OS << "liveout(";
855 bool IsCommaNeeded = false;
856 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
857 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
858 if (IsCommaNeeded)
859 OS << ", ";
860 printReg(Reg, OS, TRI);
861 IsCommaNeeded = true;
862 }
863 }
864 OS << ")";
865 break;
866 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000867 case MachineOperand::MO_Metadata:
868 Op.getMetadata()->printAsOperand(OS, MST);
869 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000870 case MachineOperand::MO_MCSymbol:
871 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
872 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000873 case MachineOperand::MO_CFIIndex: {
874 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000875 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000876 break;
877 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000878 case MachineOperand::MO_IntrinsicID: {
879 Intrinsic::ID ID = Op.getIntrinsicID();
880 if (ID < Intrinsic::num_intrinsics)
Pete Cooper15239252016-08-22 22:27:05 +0000881 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000882 else {
883 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
884 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
885 OS << "intrinsic(@" << TII->getName(ID) << ')';
886 }
887 break;
888 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000889 case MachineOperand::MO_Predicate: {
890 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
891 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
892 << CmpInst::getPredicateName(Pred) << ')';
893 break;
894 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000895 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000896}
897
Alex Lorenz4af7e612015-08-03 23:08:19 +0000898void MIPrinter::print(const MachineMemOperand &Op) {
899 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000900 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000901 if (Op.isVolatile())
902 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000903 if (Op.isNonTemporal())
904 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000905 if (Op.isInvariant())
906 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000907 if (Op.isLoad())
908 OS << "load ";
909 else {
910 assert(Op.isStore() && "Non load machine operand must be a store");
911 OS << "store ";
912 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000913 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000914 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000915 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000916 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000917 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
918 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000919 assert(PVal && "Expected a pseudo source value");
920 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000921 case PseudoSourceValue::Stack:
922 OS << "stack";
923 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000924 case PseudoSourceValue::GOT:
925 OS << "got";
926 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000927 case PseudoSourceValue::JumpTable:
928 OS << "jump-table";
929 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000930 case PseudoSourceValue::ConstantPool:
931 OS << "constant-pool";
932 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000933 case PseudoSourceValue::FixedStack:
934 printStackObjectReference(
935 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
936 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000937 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000938 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000939 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
940 OS, /*PrintType=*/false, MST);
941 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000942 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000943 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000944 printLLVMNameWithoutPrefix(
945 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000946 break;
947 }
948 }
Alex Lorenz83127732015-08-07 20:26:52 +0000949 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000950 if (Op.getBaseAlignment() != Op.getSize())
951 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000952 auto AAInfo = Op.getAAInfo();
953 if (AAInfo.TBAA) {
954 OS << ", !tbaa ";
955 AAInfo.TBAA->printAsOperand(OS, MST);
956 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000957 if (AAInfo.Scope) {
958 OS << ", !alias.scope ";
959 AAInfo.Scope->printAsOperand(OS, MST);
960 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000961 if (AAInfo.NoAlias) {
962 OS << ", !noalias ";
963 AAInfo.NoAlias->printAsOperand(OS, MST);
964 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000965 if (Op.getRanges()) {
966 OS << ", !range ";
967 Op.getRanges()->printAsOperand(OS, MST);
968 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000969 OS << ')';
970}
971
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000972static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
973 const TargetRegisterInfo *TRI) {
974 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
975 if (Reg == -1) {
976 OS << "<badreg>";
977 return;
978 }
979 printReg(Reg, OS, TRI);
980}
981
982void MIPrinter::print(const MCCFIInstruction &CFI,
983 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000984 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000985 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +0000986 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +0000987 if (CFI.getLabel())
988 OS << "<mcsymbol> ";
989 printCFIRegister(CFI.getRegister(), OS, TRI);
990 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000991 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +0000992 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000993 if (CFI.getLabel())
994 OS << "<mcsymbol> ";
995 printCFIRegister(CFI.getRegister(), OS, TRI);
996 OS << ", " << CFI.getOffset();
997 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000998 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +0000999 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001000 if (CFI.getLabel())
1001 OS << "<mcsymbol> ";
1002 printCFIRegister(CFI.getRegister(), OS, TRI);
1003 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001004 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001005 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001006 if (CFI.getLabel())
1007 OS << "<mcsymbol> ";
1008 OS << CFI.getOffset();
1009 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001010 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001011 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001012 if (CFI.getLabel())
1013 OS << "<mcsymbol> ";
1014 printCFIRegister(CFI.getRegister(), OS, TRI);
1015 OS << ", " << CFI.getOffset();
1016 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001017 default:
1018 // TODO: Print the other CFI Operations.
1019 OS << "<unserializable cfi operation>";
1020 break;
1021 }
1022}
1023
Alex Lorenz345c1442015-06-15 23:52:35 +00001024void llvm::printMIR(raw_ostream &OS, const Module &M) {
1025 yaml::Output Out(OS);
1026 Out << const_cast<Module &>(M);
1027}
1028
1029void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1030 MIRPrinter Printer(OS);
1031 Printer.print(MF);
1032}