blob: b42d45b5b5fb8436732ecd27539c362d382412ea [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
Alex Lorenz28148ba2015-07-09 22:23:13 +0000181 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000182 ModuleSlotTracker MST(MF.getFunction()->getParent());
183 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000184 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
185 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000186 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000187 if (const auto *ConstantPool = MF.getConstantPool())
188 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000189 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
190 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000191 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
192 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000193 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000194 if (IsNewlineNeeded)
195 StrOS << "\n";
196 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
197 .print(MBB);
198 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000199 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000200 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000201 yaml::Output Out(OS);
202 Out << YamlMF;
203}
204
Alex Lorenz54565cf2015-06-24 19:56:10 +0000205void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000206 const MachineRegisterInfo &RegInfo,
207 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000208 MF.IsSSA = RegInfo.isSSA();
209 MF.TracksRegLiveness = RegInfo.tracksLiveness();
210 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000211
212 // Print the virtual register definitions.
213 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
214 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
215 yaml::VirtualRegisterDefinition VReg;
216 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000217 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000218 VReg.Class =
219 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000220 else if (RegInfo.getRegBankOrNull(Reg))
221 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000222 else {
223 VReg.Class = std::string("_");
224 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
225 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000226 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
227 if (PreferredReg)
228 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000229 MF.VirtualRegisters.push_back(VReg);
230 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000231
232 // Print the live ins.
233 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
234 yaml::MachineFunctionLiveIn LiveIn;
235 printReg(I->first, LiveIn.Register, TRI);
236 if (I->second)
237 printReg(I->second, LiveIn.VirtualRegister, TRI);
238 MF.LiveIns.push_back(LiveIn);
239 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000240 // The used physical register mask is printed as an inverted callee saved
241 // register mask.
242 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
243 if (UsedPhysRegMask.none())
244 return;
245 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
246 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
247 if (!UsedPhysRegMask[I]) {
248 yaml::FlowStringValue Reg;
249 printReg(I, Reg, TRI);
250 CalleeSavedRegisters.push_back(Reg);
251 }
252 }
253 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000254}
255
Alex Lorenza6f9a372015-07-29 21:09:09 +0000256void MIRPrinter::convert(ModuleSlotTracker &MST,
257 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000258 const MachineFrameInfo &MFI) {
259 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
260 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
261 YamlMFI.HasStackMap = MFI.hasStackMap();
262 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
263 YamlMFI.StackSize = MFI.getStackSize();
264 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
265 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
266 YamlMFI.AdjustsStack = MFI.adjustsStack();
267 YamlMFI.HasCalls = MFI.hasCalls();
268 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
269 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
270 YamlMFI.HasVAStart = MFI.hasVAStart();
271 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000272 if (MFI.getSavePoint()) {
273 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
274 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
275 .printMBBReference(*MFI.getSavePoint());
276 }
277 if (MFI.getRestorePoint()) {
278 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
279 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
280 .printMBBReference(*MFI.getRestorePoint());
281 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000282}
283
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000284void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000285 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000286 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000287 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000288 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000289 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000290 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000291 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
292 if (MFI.isDeadObjectIndex(I))
293 continue;
294
295 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000296 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000297 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
298 ? yaml::FixedMachineStackObject::SpillSlot
299 : yaml::FixedMachineStackObject::DefaultType;
300 YamlObject.Offset = MFI.getObjectOffset(I);
301 YamlObject.Size = MFI.getObjectSize(I);
302 YamlObject.Alignment = MFI.getObjectAlignment(I);
303 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
304 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
305 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000306 StackObjectOperandMapping.insert(
307 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000308 }
309
310 // Process ordinary stack objects.
311 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000312 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
313 if (MFI.isDeadObjectIndex(I))
314 continue;
315
316 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000317 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000318 if (const auto *Alloca = MFI.getObjectAllocation(I))
319 YamlObject.Name.Value =
320 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000321 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
322 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000323 : MFI.isVariableSizedObjectIndex(I)
324 ? yaml::MachineStackObject::VariableSized
325 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000326 YamlObject.Offset = MFI.getObjectOffset(I);
327 YamlObject.Size = MFI.getObjectSize(I);
328 YamlObject.Alignment = MFI.getObjectAlignment(I);
329
330 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000331 StackObjectOperandMapping.insert(std::make_pair(
332 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000333 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000334
335 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
336 yaml::StringValue Reg;
337 printReg(CSInfo.getReg(), Reg, TRI);
338 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
339 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
340 "Invalid stack object index");
341 const FrameIndexOperand &StackObject = StackObjectInfo->second;
342 if (StackObject.IsFixed)
343 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
344 else
345 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
346 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000347 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
348 auto LocalObject = MFI.getLocalFrameObjectMap(I);
349 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
350 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
351 "Invalid stack object index");
352 const FrameIndexOperand &StackObject = StackObjectInfo->second;
353 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
354 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
355 }
Alex Lorenza314d812015-08-18 22:26:26 +0000356
357 // Print the stack object references in the frame information class after
358 // converting the stack objects.
359 if (MFI.hasStackProtectorIndex()) {
360 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
361 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
362 .printStackObjectReference(MFI.getStackProtectorIndex());
363 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000364
365 // Print the debug variable information.
366 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
367 MMI.getVariableDbgInfo()) {
368 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
369 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
370 "Invalid stack object index");
371 const FrameIndexOperand &StackObject = StackObjectInfo->second;
372 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
373 auto &Object = MF.StackObjects[StackObject.ID];
374 {
375 raw_string_ostream StrOS(Object.DebugVar.Value);
376 DebugVar.Var->printAsOperand(StrOS, MST);
377 }
378 {
379 raw_string_ostream StrOS(Object.DebugExpr.Value);
380 DebugVar.Expr->printAsOperand(StrOS, MST);
381 }
382 {
383 raw_string_ostream StrOS(Object.DebugLoc.Value);
384 DebugVar.Loc->printAsOperand(StrOS, MST);
385 }
386 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000387}
388
Alex Lorenzab980492015-07-20 20:51:18 +0000389void MIRPrinter::convert(yaml::MachineFunction &MF,
390 const MachineConstantPool &ConstantPool) {
391 unsigned ID = 0;
392 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
393 // TODO: Serialize target specific constant pool entries.
394 if (Constant.isMachineConstantPoolEntry())
395 llvm_unreachable("Can't print target specific constant pool entries yet");
396
397 yaml::MachineConstantPoolValue YamlConstant;
398 std::string Str;
399 raw_string_ostream StrOS(Str);
400 Constant.Val.ConstVal->printAsOperand(StrOS);
401 YamlConstant.ID = ID++;
402 YamlConstant.Value = StrOS.str();
403 YamlConstant.Alignment = Constant.getAlignment();
404 MF.Constants.push_back(YamlConstant);
405 }
406}
407
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000408void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000409 yaml::MachineJumpTable &YamlJTI,
410 const MachineJumpTableInfo &JTI) {
411 YamlJTI.Kind = JTI.getEntryKind();
412 unsigned ID = 0;
413 for (const auto &Table : JTI.getJumpTables()) {
414 std::string Str;
415 yaml::MachineJumpTable::Entry Entry;
416 Entry.ID = ID++;
417 for (const auto *MBB : Table.MBBs) {
418 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000419 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
420 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000421 Entry.Blocks.push_back(StrOS.str());
422 Str.clear();
423 }
424 YamlJTI.Entries.push_back(Entry);
425 }
426}
427
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000428void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
429 const auto *TRI = MF.getSubtarget().getRegisterInfo();
430 unsigned I = 0;
431 for (const uint32_t *Mask : TRI->getRegMasks())
432 RegisterMaskIds.insert(std::make_pair(Mask, I++));
433}
434
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000435void MIPrinter::print(const MachineBasicBlock &MBB) {
436 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
437 OS << "bb." << MBB.getNumber();
438 bool HasAttributes = false;
439 if (const auto *BB = MBB.getBasicBlock()) {
440 if (BB->hasName()) {
441 OS << "." << BB->getName();
442 } else {
443 HasAttributes = true;
444 OS << " (";
445 int Slot = MST.getLocalSlot(BB);
446 if (Slot == -1)
447 OS << "<ir-block badref>";
448 else
449 OS << (Twine("%ir-block.") + Twine(Slot)).str();
450 }
451 }
452 if (MBB.hasAddressTaken()) {
453 OS << (HasAttributes ? ", " : " (");
454 OS << "address-taken";
455 HasAttributes = true;
456 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000457 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000458 OS << (HasAttributes ? ", " : " (");
459 OS << "landing-pad";
460 HasAttributes = true;
461 }
462 if (MBB.getAlignment()) {
463 OS << (HasAttributes ? ", " : " (");
464 OS << "align " << MBB.getAlignment();
465 HasAttributes = true;
466 }
467 if (HasAttributes)
468 OS << ")";
469 OS << ":\n";
470
471 bool HasLineAttributes = false;
472 // Print the successors
473 if (!MBB.succ_empty()) {
474 OS.indent(2) << "successors: ";
475 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
476 if (I != MBB.succ_begin())
477 OS << ", ";
478 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000479 if (MBB.hasSuccessorProbabilities())
480 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000481 }
482 OS << "\n";
483 HasLineAttributes = true;
484 }
485
486 // Print the live in registers.
487 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
488 assert(TRI && "Expected target register info");
489 if (!MBB.livein_empty()) {
490 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000491 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000492 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000493 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000494 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000495 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000496 printReg(LI.PhysReg, OS, TRI);
497 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000498 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000499 }
500 OS << "\n";
501 HasLineAttributes = true;
502 }
503
504 if (HasLineAttributes)
505 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000506 bool IsInBundle = false;
507 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
508 const MachineInstr &MI = *I;
509 if (IsInBundle && !MI.isInsideBundle()) {
510 OS.indent(2) << "}\n";
511 IsInBundle = false;
512 }
513 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000514 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000515 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
516 OS << " {";
517 IsInBundle = true;
518 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000519 OS << "\n";
520 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000521 if (IsInBundle)
522 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000523}
524
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000525/// Return true when an instruction has tied register that can't be determined
526/// by the instruction's descriptor.
527static bool hasComplexRegisterTies(const MachineInstr &MI) {
528 const MCInstrDesc &MCID = MI.getDesc();
529 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
530 const auto &Operand = MI.getOperand(I);
531 if (!Operand.isReg() || Operand.isDef())
532 // Ignore the defined registers as MCID marks only the uses as tied.
533 continue;
534 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
535 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
536 if (ExpectedTiedIdx != TiedIdx)
537 return true;
538 }
539 return false;
540}
541
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000542void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000543 const auto *MF = MI.getParent()->getParent();
544 const auto &MRI = MF->getRegInfo();
545 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000546 const auto *TRI = SubTarget.getRegisterInfo();
547 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000548 const auto *TII = SubTarget.getInstrInfo();
549 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000550 if (MI.isCFIInstruction())
551 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000552
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000553 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000554 unsigned I = 0, E = MI.getNumOperands();
555 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
556 !MI.getOperand(I).isImplicit();
557 ++I) {
558 if (I)
559 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000560 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
561 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000562 }
563
564 if (I)
565 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000566 if (MI.getFlag(MachineInstr::FrameSetup))
567 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000568 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000569 if (isPreISelGenericOpcode(MI.getOpcode())) {
Tim Northover62ae5682016-07-20 19:09:30 +0000570 assert(MI.getType().isValid() && "Generic instructions must have a type");
Tim Northover26e40bd2016-07-26 17:28:01 +0000571 unsigned NumTypes = MI.getNumTypes();
572 OS << (NumTypes > 1 ? " {" : "") << ' ';
573 for (unsigned i = 0; i < NumTypes; ++i) {
Tim Northover7c9eba92016-07-25 21:01:29 +0000574 MI.getType(i).print(OS);
Tim Northover26e40bd2016-07-26 17:28:01 +0000575 if (i + 1 != NumTypes)
Tim Northover98a56eb2016-07-22 22:13:36 +0000576 OS << ", ";
577 }
Tim Northover26e40bd2016-07-26 17:28:01 +0000578 OS << (NumTypes > 1 ? " }" : "") << ' ';
Quentin Colombet12350a82016-03-08 00:29:15 +0000579 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 if (I < E)
581 OS << ' ';
582
583 bool NeedComma = false;
584 for (; I < E; ++I) {
585 if (NeedComma)
586 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000587 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000588 NeedComma = true;
589 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000590
591 if (MI.getDebugLoc()) {
592 if (NeedComma)
593 OS << ',';
594 OS << " debug-location ";
595 MI.getDebugLoc()->printAsOperand(OS, MST);
596 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000597
598 if (!MI.memoperands_empty()) {
599 OS << " :: ";
600 bool NeedComma = false;
601 for (const auto *Op : MI.memoperands()) {
602 if (NeedComma)
603 OS << ", ";
604 print(*Op);
605 NeedComma = true;
606 }
607 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000608}
609
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000610void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
611 OS << "%bb." << MBB.getNumber();
612 if (const auto *BB = MBB.getBasicBlock()) {
613 if (BB->hasName())
614 OS << '.' << BB->getName();
615 }
616}
617
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000618static void printIRSlotNumber(raw_ostream &OS, int Slot) {
619 if (Slot == -1)
620 OS << "<badref>";
621 else
622 OS << Slot;
623}
624
Alex Lorenzdeb53492015-07-28 17:28:03 +0000625void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
626 OS << "%ir-block.";
627 if (BB.hasName()) {
628 printLLVMNameWithoutPrefix(OS, BB.getName());
629 return;
630 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000631 const Function *F = BB.getParent();
632 int Slot;
633 if (F == MST.getCurrentFunction()) {
634 Slot = MST.getLocalSlot(&BB);
635 } else {
636 ModuleSlotTracker CustomMST(F->getParent(),
637 /*ShouldInitializeAllMetadata=*/false);
638 CustomMST.incorporateFunction(*F);
639 Slot = CustomMST.getLocalSlot(&BB);
640 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000641 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000642}
643
Alex Lorenz4af7e612015-08-03 23:08:19 +0000644void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000645 if (isa<GlobalValue>(V)) {
646 V.printAsOperand(OS, /*PrintType=*/false, MST);
647 return;
648 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000649 if (isa<Constant>(V)) {
650 // Machine memory operands can load/store to/from constant value pointers.
651 OS << '`';
652 V.printAsOperand(OS, /*PrintType=*/true, MST);
653 OS << '`';
654 return;
655 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000656 OS << "%ir.";
657 if (V.hasName()) {
658 printLLVMNameWithoutPrefix(OS, V.getName());
659 return;
660 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000661 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000662}
663
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000664void MIPrinter::printStackObjectReference(int FrameIndex) {
665 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
666 assert(ObjectInfo != StackObjectOperandMapping.end() &&
667 "Invalid frame index");
668 const FrameIndexOperand &Operand = ObjectInfo->second;
669 if (Operand.IsFixed) {
670 OS << "%fixed-stack." << Operand.ID;
671 return;
672 }
673 OS << "%stack." << Operand.ID;
674 if (!Operand.Name.empty())
675 OS << '.' << Operand.Name;
676}
677
Alex Lorenz5672a892015-08-05 22:26:15 +0000678void MIPrinter::printOffset(int64_t Offset) {
679 if (Offset == 0)
680 return;
681 if (Offset < 0) {
682 OS << " - " << -Offset;
683 return;
684 }
685 OS << " + " << Offset;
686}
687
Alex Lorenz49873a82015-08-06 00:44:07 +0000688static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
689 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
690 for (const auto &I : Flags) {
691 if (I.first == TF) {
692 return I.second;
693 }
694 }
695 return nullptr;
696}
697
698void MIPrinter::printTargetFlags(const MachineOperand &Op) {
699 if (!Op.getTargetFlags())
700 return;
701 const auto *TII =
702 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
703 assert(TII && "expected instruction info");
704 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
705 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000706 const bool HasDirectFlags = Flags.first;
707 const bool HasBitmaskFlags = Flags.second;
708 if (!HasDirectFlags && !HasBitmaskFlags) {
709 OS << "<unknown>) ";
710 return;
711 }
712 if (HasDirectFlags) {
713 if (const auto *Name = getTargetFlagName(TII, Flags.first))
714 OS << Name;
715 else
716 OS << "<unknown target flag>";
717 }
718 if (!HasBitmaskFlags) {
719 OS << ") ";
720 return;
721 }
722 bool IsCommaNeeded = HasDirectFlags;
723 unsigned BitMask = Flags.second;
724 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
725 for (const auto &Mask : BitMasks) {
726 // Check if the flag's bitmask has the bits of the current mask set.
727 if ((BitMask & Mask.first) == Mask.first) {
728 if (IsCommaNeeded)
729 OS << ", ";
730 IsCommaNeeded = true;
731 OS << Mask.second;
732 // Clear the bits which were serialized from the flag's bitmask.
733 BitMask &= ~(Mask.first);
734 }
735 }
736 if (BitMask) {
737 // When the resulting flag's bitmask isn't zero, we know that we didn't
738 // serialize all of the bit flags.
739 if (IsCommaNeeded)
740 OS << ", ";
741 OS << "<unknown bitmask target flag>";
742 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000743 OS << ") ";
744}
745
Alex Lorenzef5c1962015-07-28 23:02:45 +0000746static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
747 const auto *TII = MF.getSubtarget().getInstrInfo();
748 assert(TII && "expected instruction info");
749 auto Indices = TII->getSerializableTargetIndices();
750 for (const auto &I : Indices) {
751 if (I.first == Index) {
752 return I.second;
753 }
754 }
755 return nullptr;
756}
757
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000758void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000759 unsigned I, bool ShouldPrintRegisterTies,
760 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000761 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000762 switch (Op.getType()) {
763 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000764 if (Op.isImplicit())
765 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000766 else if (!IsDef && Op.isDef())
767 // Print the 'def' flag only when the operand is defined after '='.
768 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000769 if (Op.isInternalRead())
770 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000771 if (Op.isDead())
772 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000773 if (Op.isKill())
774 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000775 if (Op.isUndef())
776 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000777 if (Op.isEarlyClobber())
778 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000779 if (Op.isDebug())
780 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000781 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000782 // Print the sub register.
783 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000784 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000785 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
786 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000787 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
788 if (IsDef && MRI->getSize(Op.getReg()))
789 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000790 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000791 case MachineOperand::MO_Immediate:
792 OS << Op.getImm();
793 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000794 case MachineOperand::MO_CImmediate:
795 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
796 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000797 case MachineOperand::MO_FPImmediate:
798 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
799 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000800 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000801 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000802 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000803 case MachineOperand::MO_FrameIndex:
804 printStackObjectReference(Op.getIndex());
805 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000806 case MachineOperand::MO_ConstantPoolIndex:
807 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000808 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000809 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000810 case MachineOperand::MO_TargetIndex: {
811 OS << "target-index(";
812 if (const auto *Name = getTargetIndexName(
813 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
814 OS << Name;
815 else
816 OS << "<unknown>";
817 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000818 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000819 break;
820 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000821 case MachineOperand::MO_JumpTableIndex:
822 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000823 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000824 case MachineOperand::MO_ExternalSymbol:
825 OS << '$';
826 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000827 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000828 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000829 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000830 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000831 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000832 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000833 case MachineOperand::MO_BlockAddress:
834 OS << "blockaddress(";
835 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
836 MST);
837 OS << ", ";
838 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
839 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000840 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000841 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000842 case MachineOperand::MO_RegisterMask: {
843 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
844 if (RegMaskInfo != RegisterMaskIds.end())
845 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
846 else
847 llvm_unreachable("Can't print this machine register mask yet.");
848 break;
849 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000850 case MachineOperand::MO_RegisterLiveOut: {
851 const uint32_t *RegMask = Op.getRegLiveOut();
852 OS << "liveout(";
853 bool IsCommaNeeded = false;
854 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
855 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
856 if (IsCommaNeeded)
857 OS << ", ";
858 printReg(Reg, OS, TRI);
859 IsCommaNeeded = true;
860 }
861 }
862 OS << ")";
863 break;
864 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000865 case MachineOperand::MO_Metadata:
866 Op.getMetadata()->printAsOperand(OS, MST);
867 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000868 case MachineOperand::MO_MCSymbol:
869 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
870 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000871 case MachineOperand::MO_CFIIndex: {
872 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000873 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000874 break;
875 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000876 case MachineOperand::MO_IntrinsicID: {
877 Intrinsic::ID ID = Op.getIntrinsicID();
878 if (ID < Intrinsic::num_intrinsics)
879 OS << "intrinsic(@" << Intrinsic::getName(ID) << ')';
880 else {
881 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
882 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
883 OS << "intrinsic(@" << TII->getName(ID) << ')';
884 }
885 break;
886 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000887 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000888}
889
Alex Lorenz4af7e612015-08-03 23:08:19 +0000890void MIPrinter::print(const MachineMemOperand &Op) {
891 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000892 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000893 if (Op.isVolatile())
894 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000895 if (Op.isNonTemporal())
896 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000897 if (Op.isInvariant())
898 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000899 if (Op.isLoad())
900 OS << "load ";
901 else {
902 assert(Op.isStore() && "Non load machine operand must be a store");
903 OS << "store ";
904 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000905 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000906 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000907 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000908 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000909 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
910 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000911 assert(PVal && "Expected a pseudo source value");
912 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000913 case PseudoSourceValue::Stack:
914 OS << "stack";
915 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000916 case PseudoSourceValue::GOT:
917 OS << "got";
918 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000919 case PseudoSourceValue::JumpTable:
920 OS << "jump-table";
921 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000922 case PseudoSourceValue::ConstantPool:
923 OS << "constant-pool";
924 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000925 case PseudoSourceValue::FixedStack:
926 printStackObjectReference(
927 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
928 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000929 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000930 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000931 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
932 OS, /*PrintType=*/false, MST);
933 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000934 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000935 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000936 printLLVMNameWithoutPrefix(
937 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000938 break;
939 }
940 }
Alex Lorenz83127732015-08-07 20:26:52 +0000941 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000942 if (Op.getBaseAlignment() != Op.getSize())
943 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000944 auto AAInfo = Op.getAAInfo();
945 if (AAInfo.TBAA) {
946 OS << ", !tbaa ";
947 AAInfo.TBAA->printAsOperand(OS, MST);
948 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000949 if (AAInfo.Scope) {
950 OS << ", !alias.scope ";
951 AAInfo.Scope->printAsOperand(OS, MST);
952 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000953 if (AAInfo.NoAlias) {
954 OS << ", !noalias ";
955 AAInfo.NoAlias->printAsOperand(OS, MST);
956 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000957 if (Op.getRanges()) {
958 OS << ", !range ";
959 Op.getRanges()->printAsOperand(OS, MST);
960 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000961 OS << ')';
962}
963
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000964static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
965 const TargetRegisterInfo *TRI) {
966 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
967 if (Reg == -1) {
968 OS << "<badreg>";
969 return;
970 }
971 printReg(Reg, OS, TRI);
972}
973
974void MIPrinter::print(const MCCFIInstruction &CFI,
975 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000976 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000977 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +0000978 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +0000979 if (CFI.getLabel())
980 OS << "<mcsymbol> ";
981 printCFIRegister(CFI.getRegister(), OS, TRI);
982 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000983 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +0000984 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000985 if (CFI.getLabel())
986 OS << "<mcsymbol> ";
987 printCFIRegister(CFI.getRegister(), OS, TRI);
988 OS << ", " << CFI.getOffset();
989 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000990 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +0000991 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000992 if (CFI.getLabel())
993 OS << "<mcsymbol> ";
994 printCFIRegister(CFI.getRegister(), OS, TRI);
995 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000996 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +0000997 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000998 if (CFI.getLabel())
999 OS << "<mcsymbol> ";
1000 OS << CFI.getOffset();
1001 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001002 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001003 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001004 if (CFI.getLabel())
1005 OS << "<mcsymbol> ";
1006 printCFIRegister(CFI.getRegister(), OS, TRI);
1007 OS << ", " << CFI.getOffset();
1008 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001009 default:
1010 // TODO: Print the other CFI Operations.
1011 OS << "<unserializable cfi operation>";
1012 break;
1013 }
1014}
1015
Alex Lorenz345c1442015-06-15 23:52:35 +00001016void llvm::printMIR(raw_ostream &OS, const Module &M) {
1017 yaml::Output Out(OS);
1018 Out << const_cast<Module &>(M);
1019}
1020
1021void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1022 MIRPrinter Printer(OS);
1023 Printer.print(MF);
1024}