blob: fdd7a42ac62bf1f195ee3a48ab182e9c36eeedc9 [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();
177 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Derek Schuffad154c82016-03-28 17:05:30 +0000178 YamlMF.AllVRegsAllocated = MF.getProperties().hasProperty(
179 MachineFunctionProperties::Property::AllVRegsAllocated);
180
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000181 YamlMF.Legalized = MF.getProperties().hasProperty(
182 MachineFunctionProperties::Property::Legalized);
183
Alex Lorenz28148ba2015-07-09 22:23:13 +0000184 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000185 ModuleSlotTracker MST(MF.getFunction()->getParent());
186 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000187 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
188 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000189 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000190 if (const auto *ConstantPool = MF.getConstantPool())
191 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000192 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
193 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000194 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
195 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000196 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000197 if (IsNewlineNeeded)
198 StrOS << "\n";
199 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
200 .print(MBB);
201 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000202 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000203 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000204 yaml::Output Out(OS);
205 Out << YamlMF;
206}
207
Alex Lorenz54565cf2015-06-24 19:56:10 +0000208void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000209 const MachineRegisterInfo &RegInfo,
210 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000211 MF.IsSSA = RegInfo.isSSA();
212 MF.TracksRegLiveness = RegInfo.tracksLiveness();
213 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
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("_");
227 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
228 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000229 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
230 if (PreferredReg)
231 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000232 MF.VirtualRegisters.push_back(VReg);
233 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000234
235 // Print the live ins.
236 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
237 yaml::MachineFunctionLiveIn LiveIn;
238 printReg(I->first, LiveIn.Register, TRI);
239 if (I->second)
240 printReg(I->second, LiveIn.VirtualRegister, TRI);
241 MF.LiveIns.push_back(LiveIn);
242 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000243 // The used physical register mask is printed as an inverted callee saved
244 // register mask.
245 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
246 if (UsedPhysRegMask.none())
247 return;
248 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
249 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
250 if (!UsedPhysRegMask[I]) {
251 yaml::FlowStringValue Reg;
252 printReg(I, Reg, TRI);
253 CalleeSavedRegisters.push_back(Reg);
254 }
255 }
256 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000257}
258
Alex Lorenza6f9a372015-07-29 21:09:09 +0000259void MIRPrinter::convert(ModuleSlotTracker &MST,
260 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000261 const MachineFrameInfo &MFI) {
262 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
263 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
264 YamlMFI.HasStackMap = MFI.hasStackMap();
265 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
266 YamlMFI.StackSize = MFI.getStackSize();
267 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
268 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
269 YamlMFI.AdjustsStack = MFI.adjustsStack();
270 YamlMFI.HasCalls = MFI.hasCalls();
271 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
272 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
273 YamlMFI.HasVAStart = MFI.hasVAStart();
274 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000275 if (MFI.getSavePoint()) {
276 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
277 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
278 .printMBBReference(*MFI.getSavePoint());
279 }
280 if (MFI.getRestorePoint()) {
281 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
282 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
283 .printMBBReference(*MFI.getRestorePoint());
284 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000285}
286
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000287void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000288 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000289 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000290 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000291 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000292 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000293 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000294 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
295 if (MFI.isDeadObjectIndex(I))
296 continue;
297
298 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000299 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000300 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
301 ? yaml::FixedMachineStackObject::SpillSlot
302 : yaml::FixedMachineStackObject::DefaultType;
303 YamlObject.Offset = MFI.getObjectOffset(I);
304 YamlObject.Size = MFI.getObjectSize(I);
305 YamlObject.Alignment = MFI.getObjectAlignment(I);
306 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
307 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
308 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000309 StackObjectOperandMapping.insert(
310 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000311 }
312
313 // Process ordinary stack objects.
314 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000315 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
316 if (MFI.isDeadObjectIndex(I))
317 continue;
318
319 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000320 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000321 if (const auto *Alloca = MFI.getObjectAllocation(I))
322 YamlObject.Name.Value =
323 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000324 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
325 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000326 : MFI.isVariableSizedObjectIndex(I)
327 ? yaml::MachineStackObject::VariableSized
328 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000329 YamlObject.Offset = MFI.getObjectOffset(I);
330 YamlObject.Size = MFI.getObjectSize(I);
331 YamlObject.Alignment = MFI.getObjectAlignment(I);
332
333 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000334 StackObjectOperandMapping.insert(std::make_pair(
335 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000336 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000337
338 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
339 yaml::StringValue Reg;
340 printReg(CSInfo.getReg(), Reg, TRI);
341 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
342 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
343 "Invalid stack object index");
344 const FrameIndexOperand &StackObject = StackObjectInfo->second;
345 if (StackObject.IsFixed)
346 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
347 else
348 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
349 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000350 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
351 auto LocalObject = MFI.getLocalFrameObjectMap(I);
352 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
353 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
354 "Invalid stack object index");
355 const FrameIndexOperand &StackObject = StackObjectInfo->second;
356 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
357 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
358 }
Alex Lorenza314d812015-08-18 22:26:26 +0000359
360 // Print the stack object references in the frame information class after
361 // converting the stack objects.
362 if (MFI.hasStackProtectorIndex()) {
363 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
364 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
365 .printStackObjectReference(MFI.getStackProtectorIndex());
366 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000367
368 // Print the debug variable information.
369 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
370 MMI.getVariableDbgInfo()) {
371 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
372 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
373 "Invalid stack object index");
374 const FrameIndexOperand &StackObject = StackObjectInfo->second;
375 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
376 auto &Object = MF.StackObjects[StackObject.ID];
377 {
378 raw_string_ostream StrOS(Object.DebugVar.Value);
379 DebugVar.Var->printAsOperand(StrOS, MST);
380 }
381 {
382 raw_string_ostream StrOS(Object.DebugExpr.Value);
383 DebugVar.Expr->printAsOperand(StrOS, MST);
384 }
385 {
386 raw_string_ostream StrOS(Object.DebugLoc.Value);
387 DebugVar.Loc->printAsOperand(StrOS, MST);
388 }
389 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000390}
391
Alex Lorenzab980492015-07-20 20:51:18 +0000392void MIRPrinter::convert(yaml::MachineFunction &MF,
393 const MachineConstantPool &ConstantPool) {
394 unsigned ID = 0;
395 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
396 // TODO: Serialize target specific constant pool entries.
397 if (Constant.isMachineConstantPoolEntry())
398 llvm_unreachable("Can't print target specific constant pool entries yet");
399
400 yaml::MachineConstantPoolValue YamlConstant;
401 std::string Str;
402 raw_string_ostream StrOS(Str);
403 Constant.Val.ConstVal->printAsOperand(StrOS);
404 YamlConstant.ID = ID++;
405 YamlConstant.Value = StrOS.str();
406 YamlConstant.Alignment = Constant.getAlignment();
407 MF.Constants.push_back(YamlConstant);
408 }
409}
410
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000411void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000412 yaml::MachineJumpTable &YamlJTI,
413 const MachineJumpTableInfo &JTI) {
414 YamlJTI.Kind = JTI.getEntryKind();
415 unsigned ID = 0;
416 for (const auto &Table : JTI.getJumpTables()) {
417 std::string Str;
418 yaml::MachineJumpTable::Entry Entry;
419 Entry.ID = ID++;
420 for (const auto *MBB : Table.MBBs) {
421 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000422 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
423 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000424 Entry.Blocks.push_back(StrOS.str());
425 Str.clear();
426 }
427 YamlJTI.Entries.push_back(Entry);
428 }
429}
430
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000431void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
432 const auto *TRI = MF.getSubtarget().getRegisterInfo();
433 unsigned I = 0;
434 for (const uint32_t *Mask : TRI->getRegMasks())
435 RegisterMaskIds.insert(std::make_pair(Mask, I++));
436}
437
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000438void MIPrinter::print(const MachineBasicBlock &MBB) {
439 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
440 OS << "bb." << MBB.getNumber();
441 bool HasAttributes = false;
442 if (const auto *BB = MBB.getBasicBlock()) {
443 if (BB->hasName()) {
444 OS << "." << BB->getName();
445 } else {
446 HasAttributes = true;
447 OS << " (";
448 int Slot = MST.getLocalSlot(BB);
449 if (Slot == -1)
450 OS << "<ir-block badref>";
451 else
452 OS << (Twine("%ir-block.") + Twine(Slot)).str();
453 }
454 }
455 if (MBB.hasAddressTaken()) {
456 OS << (HasAttributes ? ", " : " (");
457 OS << "address-taken";
458 HasAttributes = true;
459 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000460 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000461 OS << (HasAttributes ? ", " : " (");
462 OS << "landing-pad";
463 HasAttributes = true;
464 }
465 if (MBB.getAlignment()) {
466 OS << (HasAttributes ? ", " : " (");
467 OS << "align " << MBB.getAlignment();
468 HasAttributes = true;
469 }
470 if (HasAttributes)
471 OS << ")";
472 OS << ":\n";
473
474 bool HasLineAttributes = false;
475 // Print the successors
476 if (!MBB.succ_empty()) {
477 OS.indent(2) << "successors: ";
478 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
479 if (I != MBB.succ_begin())
480 OS << ", ";
481 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000482 if (MBB.hasSuccessorProbabilities())
483 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000484 }
485 OS << "\n";
486 HasLineAttributes = true;
487 }
488
489 // Print the live in registers.
490 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
491 assert(TRI && "Expected target register info");
492 if (!MBB.livein_empty()) {
493 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000494 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000495 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000496 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000497 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000498 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000499 printReg(LI.PhysReg, OS, TRI);
500 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000501 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000502 }
503 OS << "\n";
504 HasLineAttributes = true;
505 }
506
507 if (HasLineAttributes)
508 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000509 bool IsInBundle = false;
510 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
511 const MachineInstr &MI = *I;
512 if (IsInBundle && !MI.isInsideBundle()) {
513 OS.indent(2) << "}\n";
514 IsInBundle = false;
515 }
516 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000517 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000518 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
519 OS << " {";
520 IsInBundle = true;
521 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000522 OS << "\n";
523 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000524 if (IsInBundle)
525 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000526}
527
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000528/// Return true when an instruction has tied register that can't be determined
529/// by the instruction's descriptor.
530static bool hasComplexRegisterTies(const MachineInstr &MI) {
531 const MCInstrDesc &MCID = MI.getDesc();
532 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
533 const auto &Operand = MI.getOperand(I);
534 if (!Operand.isReg() || Operand.isDef())
535 // Ignore the defined registers as MCID marks only the uses as tied.
536 continue;
537 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
538 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
539 if (ExpectedTiedIdx != TiedIdx)
540 return true;
541 }
542 return false;
543}
544
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000545void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000546 const auto *MF = MI.getParent()->getParent();
547 const auto &MRI = MF->getRegInfo();
548 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000549 const auto *TRI = SubTarget.getRegisterInfo();
550 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000551 const auto *TII = SubTarget.getInstrInfo();
552 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000553 if (MI.isCFIInstruction())
554 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000555
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000556 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000557 unsigned I = 0, E = MI.getNumOperands();
558 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
559 !MI.getOperand(I).isImplicit();
560 ++I) {
561 if (I)
562 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000563 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
564 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000565 }
566
567 if (I)
568 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000569 if (MI.getFlag(MachineInstr::FrameSetup))
570 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000571 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000572 if (isPreISelGenericOpcode(MI.getOpcode())) {
Tim Northover62ae5682016-07-20 19:09:30 +0000573 assert(MI.getType().isValid() && "Generic instructions must have a type");
Tim Northover26e40bd2016-07-26 17:28:01 +0000574 unsigned NumTypes = MI.getNumTypes();
575 OS << (NumTypes > 1 ? " {" : "") << ' ';
576 for (unsigned i = 0; i < NumTypes; ++i) {
Tim Northover7c9eba92016-07-25 21:01:29 +0000577 MI.getType(i).print(OS);
Tim Northover26e40bd2016-07-26 17:28:01 +0000578 if (i + 1 != NumTypes)
Tim Northover98a56eb2016-07-22 22:13:36 +0000579 OS << ", ";
580 }
Tim Northover26e40bd2016-07-26 17:28:01 +0000581 OS << (NumTypes > 1 ? " }" : "") << ' ';
Quentin Colombet12350a82016-03-08 00:29:15 +0000582 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000583 if (I < E)
584 OS << ' ';
585
586 bool NeedComma = false;
587 for (; I < E; ++I) {
588 if (NeedComma)
589 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000590 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000591 NeedComma = true;
592 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000593
594 if (MI.getDebugLoc()) {
595 if (NeedComma)
596 OS << ',';
597 OS << " debug-location ";
598 MI.getDebugLoc()->printAsOperand(OS, MST);
599 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000600
601 if (!MI.memoperands_empty()) {
602 OS << " :: ";
603 bool NeedComma = false;
604 for (const auto *Op : MI.memoperands()) {
605 if (NeedComma)
606 OS << ", ";
607 print(*Op);
608 NeedComma = true;
609 }
610 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000611}
612
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000613void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
614 OS << "%bb." << MBB.getNumber();
615 if (const auto *BB = MBB.getBasicBlock()) {
616 if (BB->hasName())
617 OS << '.' << BB->getName();
618 }
619}
620
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000621static void printIRSlotNumber(raw_ostream &OS, int Slot) {
622 if (Slot == -1)
623 OS << "<badref>";
624 else
625 OS << Slot;
626}
627
Alex Lorenzdeb53492015-07-28 17:28:03 +0000628void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
629 OS << "%ir-block.";
630 if (BB.hasName()) {
631 printLLVMNameWithoutPrefix(OS, BB.getName());
632 return;
633 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000634 const Function *F = BB.getParent();
635 int Slot;
636 if (F == MST.getCurrentFunction()) {
637 Slot = MST.getLocalSlot(&BB);
638 } else {
639 ModuleSlotTracker CustomMST(F->getParent(),
640 /*ShouldInitializeAllMetadata=*/false);
641 CustomMST.incorporateFunction(*F);
642 Slot = CustomMST.getLocalSlot(&BB);
643 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000644 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000645}
646
Alex Lorenz4af7e612015-08-03 23:08:19 +0000647void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000648 if (isa<GlobalValue>(V)) {
649 V.printAsOperand(OS, /*PrintType=*/false, MST);
650 return;
651 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000652 if (isa<Constant>(V)) {
653 // Machine memory operands can load/store to/from constant value pointers.
654 OS << '`';
655 V.printAsOperand(OS, /*PrintType=*/true, MST);
656 OS << '`';
657 return;
658 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000659 OS << "%ir.";
660 if (V.hasName()) {
661 printLLVMNameWithoutPrefix(OS, V.getName());
662 return;
663 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000664 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000665}
666
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000667void MIPrinter::printStackObjectReference(int FrameIndex) {
668 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
669 assert(ObjectInfo != StackObjectOperandMapping.end() &&
670 "Invalid frame index");
671 const FrameIndexOperand &Operand = ObjectInfo->second;
672 if (Operand.IsFixed) {
673 OS << "%fixed-stack." << Operand.ID;
674 return;
675 }
676 OS << "%stack." << Operand.ID;
677 if (!Operand.Name.empty())
678 OS << '.' << Operand.Name;
679}
680
Alex Lorenz5672a892015-08-05 22:26:15 +0000681void MIPrinter::printOffset(int64_t Offset) {
682 if (Offset == 0)
683 return;
684 if (Offset < 0) {
685 OS << " - " << -Offset;
686 return;
687 }
688 OS << " + " << Offset;
689}
690
Alex Lorenz49873a82015-08-06 00:44:07 +0000691static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
692 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
693 for (const auto &I : Flags) {
694 if (I.first == TF) {
695 return I.second;
696 }
697 }
698 return nullptr;
699}
700
701void MIPrinter::printTargetFlags(const MachineOperand &Op) {
702 if (!Op.getTargetFlags())
703 return;
704 const auto *TII =
705 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
706 assert(TII && "expected instruction info");
707 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
708 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000709 const bool HasDirectFlags = Flags.first;
710 const bool HasBitmaskFlags = Flags.second;
711 if (!HasDirectFlags && !HasBitmaskFlags) {
712 OS << "<unknown>) ";
713 return;
714 }
715 if (HasDirectFlags) {
716 if (const auto *Name = getTargetFlagName(TII, Flags.first))
717 OS << Name;
718 else
719 OS << "<unknown target flag>";
720 }
721 if (!HasBitmaskFlags) {
722 OS << ") ";
723 return;
724 }
725 bool IsCommaNeeded = HasDirectFlags;
726 unsigned BitMask = Flags.second;
727 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
728 for (const auto &Mask : BitMasks) {
729 // Check if the flag's bitmask has the bits of the current mask set.
730 if ((BitMask & Mask.first) == Mask.first) {
731 if (IsCommaNeeded)
732 OS << ", ";
733 IsCommaNeeded = true;
734 OS << Mask.second;
735 // Clear the bits which were serialized from the flag's bitmask.
736 BitMask &= ~(Mask.first);
737 }
738 }
739 if (BitMask) {
740 // When the resulting flag's bitmask isn't zero, we know that we didn't
741 // serialize all of the bit flags.
742 if (IsCommaNeeded)
743 OS << ", ";
744 OS << "<unknown bitmask target flag>";
745 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000746 OS << ") ";
747}
748
Alex Lorenzef5c1962015-07-28 23:02:45 +0000749static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
750 const auto *TII = MF.getSubtarget().getInstrInfo();
751 assert(TII && "expected instruction info");
752 auto Indices = TII->getSerializableTargetIndices();
753 for (const auto &I : Indices) {
754 if (I.first == Index) {
755 return I.second;
756 }
757 }
758 return nullptr;
759}
760
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000761void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000762 unsigned I, bool ShouldPrintRegisterTies,
763 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000764 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000765 switch (Op.getType()) {
766 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000767 if (Op.isImplicit())
768 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000769 else if (!IsDef && Op.isDef())
770 // Print the 'def' flag only when the operand is defined after '='.
771 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000772 if (Op.isInternalRead())
773 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000774 if (Op.isDead())
775 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000776 if (Op.isKill())
777 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000778 if (Op.isUndef())
779 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000780 if (Op.isEarlyClobber())
781 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000782 if (Op.isDebug())
783 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000784 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000785 // Print the sub register.
786 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000787 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000788 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
789 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000790 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
791 if (IsDef && MRI->getSize(Op.getReg()))
792 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000793 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000794 case MachineOperand::MO_Immediate:
795 OS << Op.getImm();
796 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000797 case MachineOperand::MO_CImmediate:
798 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
799 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000800 case MachineOperand::MO_FPImmediate:
801 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
802 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000803 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000804 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000805 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000806 case MachineOperand::MO_FrameIndex:
807 printStackObjectReference(Op.getIndex());
808 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000809 case MachineOperand::MO_ConstantPoolIndex:
810 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000811 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000812 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000813 case MachineOperand::MO_TargetIndex: {
814 OS << "target-index(";
815 if (const auto *Name = getTargetIndexName(
816 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
817 OS << Name;
818 else
819 OS << "<unknown>";
820 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000821 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000822 break;
823 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000824 case MachineOperand::MO_JumpTableIndex:
825 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000826 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000827 case MachineOperand::MO_ExternalSymbol:
828 OS << '$';
829 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000830 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000831 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000832 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000833 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000834 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000835 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000836 case MachineOperand::MO_BlockAddress:
837 OS << "blockaddress(";
838 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
839 MST);
840 OS << ", ";
841 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
842 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000843 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000844 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000845 case MachineOperand::MO_RegisterMask: {
846 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
847 if (RegMaskInfo != RegisterMaskIds.end())
848 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
849 else
850 llvm_unreachable("Can't print this machine register mask yet.");
851 break;
852 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000853 case MachineOperand::MO_RegisterLiveOut: {
854 const uint32_t *RegMask = Op.getRegLiveOut();
855 OS << "liveout(";
856 bool IsCommaNeeded = false;
857 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
858 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
859 if (IsCommaNeeded)
860 OS << ", ";
861 printReg(Reg, OS, TRI);
862 IsCommaNeeded = true;
863 }
864 }
865 OS << ")";
866 break;
867 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000868 case MachineOperand::MO_Metadata:
869 Op.getMetadata()->printAsOperand(OS, MST);
870 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000871 case MachineOperand::MO_MCSymbol:
872 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
873 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000874 case MachineOperand::MO_CFIIndex: {
875 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000876 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000877 break;
878 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000879 case MachineOperand::MO_IntrinsicID: {
880 Intrinsic::ID ID = Op.getIntrinsicID();
881 if (ID < Intrinsic::num_intrinsics)
882 OS << "intrinsic(@" << Intrinsic::getName(ID) << ')';
883 else {
884 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
885 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
886 OS << "intrinsic(@" << TII->getName(ID) << ')';
887 }
888 break;
889 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000890 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000891}
892
Alex Lorenz4af7e612015-08-03 23:08:19 +0000893void MIPrinter::print(const MachineMemOperand &Op) {
894 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000895 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000896 if (Op.isVolatile())
897 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000898 if (Op.isNonTemporal())
899 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000900 if (Op.isInvariant())
901 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000902 if (Op.isLoad())
903 OS << "load ";
904 else {
905 assert(Op.isStore() && "Non load machine operand must be a store");
906 OS << "store ";
907 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000908 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000909 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000910 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000911 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000912 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
913 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000914 assert(PVal && "Expected a pseudo source value");
915 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000916 case PseudoSourceValue::Stack:
917 OS << "stack";
918 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000919 case PseudoSourceValue::GOT:
920 OS << "got";
921 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000922 case PseudoSourceValue::JumpTable:
923 OS << "jump-table";
924 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000925 case PseudoSourceValue::ConstantPool:
926 OS << "constant-pool";
927 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000928 case PseudoSourceValue::FixedStack:
929 printStackObjectReference(
930 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
931 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000932 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000933 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000934 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
935 OS, /*PrintType=*/false, MST);
936 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000937 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000938 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000939 printLLVMNameWithoutPrefix(
940 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000941 break;
942 }
943 }
Alex Lorenz83127732015-08-07 20:26:52 +0000944 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000945 if (Op.getBaseAlignment() != Op.getSize())
946 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000947 auto AAInfo = Op.getAAInfo();
948 if (AAInfo.TBAA) {
949 OS << ", !tbaa ";
950 AAInfo.TBAA->printAsOperand(OS, MST);
951 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000952 if (AAInfo.Scope) {
953 OS << ", !alias.scope ";
954 AAInfo.Scope->printAsOperand(OS, MST);
955 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000956 if (AAInfo.NoAlias) {
957 OS << ", !noalias ";
958 AAInfo.NoAlias->printAsOperand(OS, MST);
959 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000960 if (Op.getRanges()) {
961 OS << ", !range ";
962 Op.getRanges()->printAsOperand(OS, MST);
963 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000964 OS << ')';
965}
966
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000967static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
968 const TargetRegisterInfo *TRI) {
969 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
970 if (Reg == -1) {
971 OS << "<badreg>";
972 return;
973 }
974 printReg(Reg, OS, TRI);
975}
976
977void MIPrinter::print(const MCCFIInstruction &CFI,
978 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000979 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000980 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +0000981 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +0000982 if (CFI.getLabel())
983 OS << "<mcsymbol> ";
984 printCFIRegister(CFI.getRegister(), OS, TRI);
985 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000986 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +0000987 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000988 if (CFI.getLabel())
989 OS << "<mcsymbol> ";
990 printCFIRegister(CFI.getRegister(), OS, TRI);
991 OS << ", " << CFI.getOffset();
992 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000993 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +0000994 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000995 if (CFI.getLabel())
996 OS << "<mcsymbol> ";
997 printCFIRegister(CFI.getRegister(), OS, TRI);
998 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000999 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001000 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001001 if (CFI.getLabel())
1002 OS << "<mcsymbol> ";
1003 OS << CFI.getOffset();
1004 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001005 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001006 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001007 if (CFI.getLabel())
1008 OS << "<mcsymbol> ";
1009 printCFIRegister(CFI.getRegister(), OS, TRI);
1010 OS << ", " << CFI.getOffset();
1011 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001012 default:
1013 // TODO: Print the other CFI Operations.
1014 OS << "<unserializable cfi operation>";
1015 break;
1016 }
1017}
1018
Alex Lorenz345c1442015-06-15 23:52:35 +00001019void llvm::printMIR(raw_ostream &OS, const Module &M) {
1020 yaml::Output Out(OS);
1021 Out << const_cast<Module &>(M);
1022}
1023
1024void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1025 MIRPrinter Printer(OS);
1026 Printer.print(MF);
1027}