blob: e6c25c0899f298e0298a6544f09bfc41ad3c0fb9 [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);
Ahmed Bougacha24712652016-08-02 16:17:10 +0000183 YamlMF.RegBankSelected = MF.getProperties().hasProperty(
184 MachineFunctionProperties::Property::RegBankSelected);
Ahmed Bougachab109d512016-08-02 16:49:19 +0000185 YamlMF.Selected = MF.getProperties().hasProperty(
186 MachineFunctionProperties::Property::Selected);
Ahmed Bougacha0d7b0cb2016-08-02 15:10:25 +0000187
Alex Lorenz28148ba2015-07-09 22:23:13 +0000188 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000189 ModuleSlotTracker MST(MF.getFunction()->getParent());
190 MST.incorporateFunction(*MF.getFunction());
Matthias Braun941a7052016-07-28 18:40:00 +0000191 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo());
192 convertStackObjects(YamlMF, MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000193 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000194 if (const auto *ConstantPool = MF.getConstantPool())
195 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000196 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
197 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000198 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
199 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000200 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000201 if (IsNewlineNeeded)
202 StrOS << "\n";
203 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
204 .print(MBB);
205 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000206 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000207 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000208 yaml::Output Out(OS);
209 Out << YamlMF;
210}
211
Alex Lorenz54565cf2015-06-24 19:56:10 +0000212void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000213 const MachineRegisterInfo &RegInfo,
214 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000215 MF.IsSSA = RegInfo.isSSA();
216 MF.TracksRegLiveness = RegInfo.tracksLiveness();
217 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000218
219 // Print the virtual register definitions.
220 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
221 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
222 yaml::VirtualRegisterDefinition VReg;
223 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000224 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000225 VReg.Class =
226 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000227 else if (RegInfo.getRegBankOrNull(Reg))
228 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000229 else {
230 VReg.Class = std::string("_");
231 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
232 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000233 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
234 if (PreferredReg)
235 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000236 MF.VirtualRegisters.push_back(VReg);
237 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000238
239 // Print the live ins.
240 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
241 yaml::MachineFunctionLiveIn LiveIn;
242 printReg(I->first, LiveIn.Register, TRI);
243 if (I->second)
244 printReg(I->second, LiveIn.VirtualRegister, TRI);
245 MF.LiveIns.push_back(LiveIn);
246 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000247 // The used physical register mask is printed as an inverted callee saved
248 // register mask.
249 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
250 if (UsedPhysRegMask.none())
251 return;
252 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
253 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
254 if (!UsedPhysRegMask[I]) {
255 yaml::FlowStringValue Reg;
256 printReg(I, Reg, TRI);
257 CalleeSavedRegisters.push_back(Reg);
258 }
259 }
260 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000261}
262
Alex Lorenza6f9a372015-07-29 21:09:09 +0000263void MIRPrinter::convert(ModuleSlotTracker &MST,
264 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000265 const MachineFrameInfo &MFI) {
266 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
267 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
268 YamlMFI.HasStackMap = MFI.hasStackMap();
269 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
270 YamlMFI.StackSize = MFI.getStackSize();
271 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
272 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
273 YamlMFI.AdjustsStack = MFI.adjustsStack();
274 YamlMFI.HasCalls = MFI.hasCalls();
275 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
276 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
277 YamlMFI.HasVAStart = MFI.hasVAStart();
278 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000279 if (MFI.getSavePoint()) {
280 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
281 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
282 .printMBBReference(*MFI.getSavePoint());
283 }
284 if (MFI.getRestorePoint()) {
285 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
286 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
287 .printMBBReference(*MFI.getRestorePoint());
288 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000289}
290
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000291void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000292 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000293 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000294 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000295 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000296 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000297 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000298 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
299 if (MFI.isDeadObjectIndex(I))
300 continue;
301
302 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000303 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000304 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
305 ? yaml::FixedMachineStackObject::SpillSlot
306 : yaml::FixedMachineStackObject::DefaultType;
307 YamlObject.Offset = MFI.getObjectOffset(I);
308 YamlObject.Size = MFI.getObjectSize(I);
309 YamlObject.Alignment = MFI.getObjectAlignment(I);
310 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
311 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
312 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000313 StackObjectOperandMapping.insert(
314 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000315 }
316
317 // Process ordinary stack objects.
318 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000319 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
320 if (MFI.isDeadObjectIndex(I))
321 continue;
322
323 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000324 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000325 if (const auto *Alloca = MFI.getObjectAllocation(I))
326 YamlObject.Name.Value =
327 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000328 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
329 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000330 : MFI.isVariableSizedObjectIndex(I)
331 ? yaml::MachineStackObject::VariableSized
332 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000333 YamlObject.Offset = MFI.getObjectOffset(I);
334 YamlObject.Size = MFI.getObjectSize(I);
335 YamlObject.Alignment = MFI.getObjectAlignment(I);
336
337 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000338 StackObjectOperandMapping.insert(std::make_pair(
339 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000340 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000341
342 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
343 yaml::StringValue Reg;
344 printReg(CSInfo.getReg(), Reg, TRI);
345 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
346 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
347 "Invalid stack object index");
348 const FrameIndexOperand &StackObject = StackObjectInfo->second;
349 if (StackObject.IsFixed)
350 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
351 else
352 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
353 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000354 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
355 auto LocalObject = MFI.getLocalFrameObjectMap(I);
356 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
357 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
358 "Invalid stack object index");
359 const FrameIndexOperand &StackObject = StackObjectInfo->second;
360 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
361 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
362 }
Alex Lorenza314d812015-08-18 22:26:26 +0000363
364 // Print the stack object references in the frame information class after
365 // converting the stack objects.
366 if (MFI.hasStackProtectorIndex()) {
367 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
368 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
369 .printStackObjectReference(MFI.getStackProtectorIndex());
370 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000371
372 // Print the debug variable information.
373 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
374 MMI.getVariableDbgInfo()) {
375 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
376 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
377 "Invalid stack object index");
378 const FrameIndexOperand &StackObject = StackObjectInfo->second;
379 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
380 auto &Object = MF.StackObjects[StackObject.ID];
381 {
382 raw_string_ostream StrOS(Object.DebugVar.Value);
383 DebugVar.Var->printAsOperand(StrOS, MST);
384 }
385 {
386 raw_string_ostream StrOS(Object.DebugExpr.Value);
387 DebugVar.Expr->printAsOperand(StrOS, MST);
388 }
389 {
390 raw_string_ostream StrOS(Object.DebugLoc.Value);
391 DebugVar.Loc->printAsOperand(StrOS, MST);
392 }
393 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000394}
395
Alex Lorenzab980492015-07-20 20:51:18 +0000396void MIRPrinter::convert(yaml::MachineFunction &MF,
397 const MachineConstantPool &ConstantPool) {
398 unsigned ID = 0;
399 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
400 // TODO: Serialize target specific constant pool entries.
401 if (Constant.isMachineConstantPoolEntry())
402 llvm_unreachable("Can't print target specific constant pool entries yet");
403
404 yaml::MachineConstantPoolValue YamlConstant;
405 std::string Str;
406 raw_string_ostream StrOS(Str);
407 Constant.Val.ConstVal->printAsOperand(StrOS);
408 YamlConstant.ID = ID++;
409 YamlConstant.Value = StrOS.str();
410 YamlConstant.Alignment = Constant.getAlignment();
411 MF.Constants.push_back(YamlConstant);
412 }
413}
414
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000415void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000416 yaml::MachineJumpTable &YamlJTI,
417 const MachineJumpTableInfo &JTI) {
418 YamlJTI.Kind = JTI.getEntryKind();
419 unsigned ID = 0;
420 for (const auto &Table : JTI.getJumpTables()) {
421 std::string Str;
422 yaml::MachineJumpTable::Entry Entry;
423 Entry.ID = ID++;
424 for (const auto *MBB : Table.MBBs) {
425 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000426 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
427 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000428 Entry.Blocks.push_back(StrOS.str());
429 Str.clear();
430 }
431 YamlJTI.Entries.push_back(Entry);
432 }
433}
434
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000435void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
436 const auto *TRI = MF.getSubtarget().getRegisterInfo();
437 unsigned I = 0;
438 for (const uint32_t *Mask : TRI->getRegMasks())
439 RegisterMaskIds.insert(std::make_pair(Mask, I++));
440}
441
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000442void MIPrinter::print(const MachineBasicBlock &MBB) {
443 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
444 OS << "bb." << MBB.getNumber();
445 bool HasAttributes = false;
446 if (const auto *BB = MBB.getBasicBlock()) {
447 if (BB->hasName()) {
448 OS << "." << BB->getName();
449 } else {
450 HasAttributes = true;
451 OS << " (";
452 int Slot = MST.getLocalSlot(BB);
453 if (Slot == -1)
454 OS << "<ir-block badref>";
455 else
456 OS << (Twine("%ir-block.") + Twine(Slot)).str();
457 }
458 }
459 if (MBB.hasAddressTaken()) {
460 OS << (HasAttributes ? ", " : " (");
461 OS << "address-taken";
462 HasAttributes = true;
463 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000464 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000465 OS << (HasAttributes ? ", " : " (");
466 OS << "landing-pad";
467 HasAttributes = true;
468 }
469 if (MBB.getAlignment()) {
470 OS << (HasAttributes ? ", " : " (");
471 OS << "align " << MBB.getAlignment();
472 HasAttributes = true;
473 }
474 if (HasAttributes)
475 OS << ")";
476 OS << ":\n";
477
478 bool HasLineAttributes = false;
479 // Print the successors
480 if (!MBB.succ_empty()) {
481 OS.indent(2) << "successors: ";
482 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
483 if (I != MBB.succ_begin())
484 OS << ", ";
485 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000486 if (MBB.hasSuccessorProbabilities())
487 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000488 }
489 OS << "\n";
490 HasLineAttributes = true;
491 }
492
493 // Print the live in registers.
494 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
495 assert(TRI && "Expected target register info");
496 if (!MBB.livein_empty()) {
497 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000498 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000499 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000500 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000501 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000502 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000503 printReg(LI.PhysReg, OS, TRI);
504 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000505 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000506 }
507 OS << "\n";
508 HasLineAttributes = true;
509 }
510
511 if (HasLineAttributes)
512 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000513 bool IsInBundle = false;
514 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
515 const MachineInstr &MI = *I;
516 if (IsInBundle && !MI.isInsideBundle()) {
517 OS.indent(2) << "}\n";
518 IsInBundle = false;
519 }
520 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000522 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
523 OS << " {";
524 IsInBundle = true;
525 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000526 OS << "\n";
527 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000528 if (IsInBundle)
529 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000530}
531
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000532/// Return true when an instruction has tied register that can't be determined
533/// by the instruction's descriptor.
534static bool hasComplexRegisterTies(const MachineInstr &MI) {
535 const MCInstrDesc &MCID = MI.getDesc();
536 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
537 const auto &Operand = MI.getOperand(I);
538 if (!Operand.isReg() || Operand.isDef())
539 // Ignore the defined registers as MCID marks only the uses as tied.
540 continue;
541 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
542 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
543 if (ExpectedTiedIdx != TiedIdx)
544 return true;
545 }
546 return false;
547}
548
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000549void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000550 const auto *MF = MI.getParent()->getParent();
551 const auto &MRI = MF->getRegInfo();
552 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000553 const auto *TRI = SubTarget.getRegisterInfo();
554 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000555 const auto *TII = SubTarget.getInstrInfo();
556 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000557 if (MI.isCFIInstruction())
558 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000559
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000560 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000561 unsigned I = 0, E = MI.getNumOperands();
562 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
563 !MI.getOperand(I).isImplicit();
564 ++I) {
565 if (I)
566 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000567 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
568 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000569 }
570
571 if (I)
572 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000573 if (MI.getFlag(MachineInstr::FrameSetup))
574 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000575 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000576 if (isPreISelGenericOpcode(MI.getOpcode())) {
Tim Northover62ae5682016-07-20 19:09:30 +0000577 assert(MI.getType().isValid() && "Generic instructions must have a type");
Tim Northover26e40bd2016-07-26 17:28:01 +0000578 unsigned NumTypes = MI.getNumTypes();
579 OS << (NumTypes > 1 ? " {" : "") << ' ';
580 for (unsigned i = 0; i < NumTypes; ++i) {
Tim Northover7c9eba92016-07-25 21:01:29 +0000581 MI.getType(i).print(OS);
Tim Northover26e40bd2016-07-26 17:28:01 +0000582 if (i + 1 != NumTypes)
Tim Northover98a56eb2016-07-22 22:13:36 +0000583 OS << ", ";
584 }
Tim Northover26e40bd2016-07-26 17:28:01 +0000585 OS << (NumTypes > 1 ? " }" : "") << ' ';
Quentin Colombet12350a82016-03-08 00:29:15 +0000586 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000587 if (I < E)
588 OS << ' ';
589
590 bool NeedComma = false;
591 for (; I < E; ++I) {
592 if (NeedComma)
593 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000594 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000595 NeedComma = true;
596 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000597
598 if (MI.getDebugLoc()) {
599 if (NeedComma)
600 OS << ',';
601 OS << " debug-location ";
602 MI.getDebugLoc()->printAsOperand(OS, MST);
603 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000604
605 if (!MI.memoperands_empty()) {
606 OS << " :: ";
607 bool NeedComma = false;
608 for (const auto *Op : MI.memoperands()) {
609 if (NeedComma)
610 OS << ", ";
611 print(*Op);
612 NeedComma = true;
613 }
614 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000615}
616
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000617void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
618 OS << "%bb." << MBB.getNumber();
619 if (const auto *BB = MBB.getBasicBlock()) {
620 if (BB->hasName())
621 OS << '.' << BB->getName();
622 }
623}
624
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000625static void printIRSlotNumber(raw_ostream &OS, int Slot) {
626 if (Slot == -1)
627 OS << "<badref>";
628 else
629 OS << Slot;
630}
631
Alex Lorenzdeb53492015-07-28 17:28:03 +0000632void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
633 OS << "%ir-block.";
634 if (BB.hasName()) {
635 printLLVMNameWithoutPrefix(OS, BB.getName());
636 return;
637 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000638 const Function *F = BB.getParent();
639 int Slot;
640 if (F == MST.getCurrentFunction()) {
641 Slot = MST.getLocalSlot(&BB);
642 } else {
643 ModuleSlotTracker CustomMST(F->getParent(),
644 /*ShouldInitializeAllMetadata=*/false);
645 CustomMST.incorporateFunction(*F);
646 Slot = CustomMST.getLocalSlot(&BB);
647 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000648 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000649}
650
Alex Lorenz4af7e612015-08-03 23:08:19 +0000651void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000652 if (isa<GlobalValue>(V)) {
653 V.printAsOperand(OS, /*PrintType=*/false, MST);
654 return;
655 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000656 if (isa<Constant>(V)) {
657 // Machine memory operands can load/store to/from constant value pointers.
658 OS << '`';
659 V.printAsOperand(OS, /*PrintType=*/true, MST);
660 OS << '`';
661 return;
662 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000663 OS << "%ir.";
664 if (V.hasName()) {
665 printLLVMNameWithoutPrefix(OS, V.getName());
666 return;
667 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000668 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000669}
670
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000671void MIPrinter::printStackObjectReference(int FrameIndex) {
672 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
673 assert(ObjectInfo != StackObjectOperandMapping.end() &&
674 "Invalid frame index");
675 const FrameIndexOperand &Operand = ObjectInfo->second;
676 if (Operand.IsFixed) {
677 OS << "%fixed-stack." << Operand.ID;
678 return;
679 }
680 OS << "%stack." << Operand.ID;
681 if (!Operand.Name.empty())
682 OS << '.' << Operand.Name;
683}
684
Alex Lorenz5672a892015-08-05 22:26:15 +0000685void MIPrinter::printOffset(int64_t Offset) {
686 if (Offset == 0)
687 return;
688 if (Offset < 0) {
689 OS << " - " << -Offset;
690 return;
691 }
692 OS << " + " << Offset;
693}
694
Alex Lorenz49873a82015-08-06 00:44:07 +0000695static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
696 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
697 for (const auto &I : Flags) {
698 if (I.first == TF) {
699 return I.second;
700 }
701 }
702 return nullptr;
703}
704
705void MIPrinter::printTargetFlags(const MachineOperand &Op) {
706 if (!Op.getTargetFlags())
707 return;
708 const auto *TII =
709 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
710 assert(TII && "expected instruction info");
711 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
712 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000713 const bool HasDirectFlags = Flags.first;
714 const bool HasBitmaskFlags = Flags.second;
715 if (!HasDirectFlags && !HasBitmaskFlags) {
716 OS << "<unknown>) ";
717 return;
718 }
719 if (HasDirectFlags) {
720 if (const auto *Name = getTargetFlagName(TII, Flags.first))
721 OS << Name;
722 else
723 OS << "<unknown target flag>";
724 }
725 if (!HasBitmaskFlags) {
726 OS << ") ";
727 return;
728 }
729 bool IsCommaNeeded = HasDirectFlags;
730 unsigned BitMask = Flags.second;
731 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
732 for (const auto &Mask : BitMasks) {
733 // Check if the flag's bitmask has the bits of the current mask set.
734 if ((BitMask & Mask.first) == Mask.first) {
735 if (IsCommaNeeded)
736 OS << ", ";
737 IsCommaNeeded = true;
738 OS << Mask.second;
739 // Clear the bits which were serialized from the flag's bitmask.
740 BitMask &= ~(Mask.first);
741 }
742 }
743 if (BitMask) {
744 // When the resulting flag's bitmask isn't zero, we know that we didn't
745 // serialize all of the bit flags.
746 if (IsCommaNeeded)
747 OS << ", ";
748 OS << "<unknown bitmask target flag>";
749 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000750 OS << ") ";
751}
752
Alex Lorenzef5c1962015-07-28 23:02:45 +0000753static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
754 const auto *TII = MF.getSubtarget().getInstrInfo();
755 assert(TII && "expected instruction info");
756 auto Indices = TII->getSerializableTargetIndices();
757 for (const auto &I : Indices) {
758 if (I.first == Index) {
759 return I.second;
760 }
761 }
762 return nullptr;
763}
764
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000765void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000766 unsigned I, bool ShouldPrintRegisterTies,
767 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000768 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000769 switch (Op.getType()) {
770 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000771 if (Op.isImplicit())
772 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000773 else if (!IsDef && Op.isDef())
774 // Print the 'def' flag only when the operand is defined after '='.
775 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000776 if (Op.isInternalRead())
777 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000778 if (Op.isDead())
779 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000780 if (Op.isKill())
781 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000782 if (Op.isUndef())
783 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000784 if (Op.isEarlyClobber())
785 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000786 if (Op.isDebug())
787 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000788 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000789 // Print the sub register.
790 if (Op.getSubReg() != 0)
Matthias Braun333e4682016-07-26 21:49:34 +0000791 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000792 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
793 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000794 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
795 if (IsDef && MRI->getSize(Op.getReg()))
796 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000797 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000798 case MachineOperand::MO_Immediate:
799 OS << Op.getImm();
800 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000801 case MachineOperand::MO_CImmediate:
802 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
803 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000804 case MachineOperand::MO_FPImmediate:
805 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
806 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000807 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000808 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000809 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000810 case MachineOperand::MO_FrameIndex:
811 printStackObjectReference(Op.getIndex());
812 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000813 case MachineOperand::MO_ConstantPoolIndex:
814 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000815 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000816 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000817 case MachineOperand::MO_TargetIndex: {
818 OS << "target-index(";
819 if (const auto *Name = getTargetIndexName(
820 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
821 OS << Name;
822 else
823 OS << "<unknown>";
824 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000825 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000826 break;
827 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000828 case MachineOperand::MO_JumpTableIndex:
829 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000830 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000831 case MachineOperand::MO_ExternalSymbol:
832 OS << '$';
833 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000834 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000835 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000836 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000837 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000838 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000839 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000840 case MachineOperand::MO_BlockAddress:
841 OS << "blockaddress(";
842 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
843 MST);
844 OS << ", ";
845 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
846 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000847 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000848 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000849 case MachineOperand::MO_RegisterMask: {
850 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
851 if (RegMaskInfo != RegisterMaskIds.end())
852 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
853 else
854 llvm_unreachable("Can't print this machine register mask yet.");
855 break;
856 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000857 case MachineOperand::MO_RegisterLiveOut: {
858 const uint32_t *RegMask = Op.getRegLiveOut();
859 OS << "liveout(";
860 bool IsCommaNeeded = false;
861 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
862 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
863 if (IsCommaNeeded)
864 OS << ", ";
865 printReg(Reg, OS, TRI);
866 IsCommaNeeded = true;
867 }
868 }
869 OS << ")";
870 break;
871 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000872 case MachineOperand::MO_Metadata:
873 Op.getMetadata()->printAsOperand(OS, MST);
874 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000875 case MachineOperand::MO_MCSymbol:
876 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
877 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000878 case MachineOperand::MO_CFIIndex: {
879 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000880 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000881 break;
882 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000883 case MachineOperand::MO_IntrinsicID: {
884 Intrinsic::ID ID = Op.getIntrinsicID();
885 if (ID < Intrinsic::num_intrinsics)
886 OS << "intrinsic(@" << Intrinsic::getName(ID) << ')';
887 else {
888 const MachineFunction &MF = *Op.getParent()->getParent()->getParent();
889 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
890 OS << "intrinsic(@" << TII->getName(ID) << ')';
891 }
892 break;
893 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000894 case MachineOperand::MO_Predicate: {
895 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
896 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
897 << CmpInst::getPredicateName(Pred) << ')';
898 break;
899 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000900 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000901}
902
Alex Lorenz4af7e612015-08-03 23:08:19 +0000903void MIPrinter::print(const MachineMemOperand &Op) {
904 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000905 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000906 if (Op.isVolatile())
907 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000908 if (Op.isNonTemporal())
909 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000910 if (Op.isInvariant())
911 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000912 if (Op.isLoad())
913 OS << "load ";
914 else {
915 assert(Op.isStore() && "Non load machine operand must be a store");
916 OS << "store ";
917 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000918 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000919 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000920 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000921 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000922 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
923 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000924 assert(PVal && "Expected a pseudo source value");
925 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000926 case PseudoSourceValue::Stack:
927 OS << "stack";
928 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000929 case PseudoSourceValue::GOT:
930 OS << "got";
931 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000932 case PseudoSourceValue::JumpTable:
933 OS << "jump-table";
934 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000935 case PseudoSourceValue::ConstantPool:
936 OS << "constant-pool";
937 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000938 case PseudoSourceValue::FixedStack:
939 printStackObjectReference(
940 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
941 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000942 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000943 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000944 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
945 OS, /*PrintType=*/false, MST);
946 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000947 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000948 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000949 printLLVMNameWithoutPrefix(
950 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000951 break;
952 }
953 }
Alex Lorenz83127732015-08-07 20:26:52 +0000954 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000955 if (Op.getBaseAlignment() != Op.getSize())
956 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000957 auto AAInfo = Op.getAAInfo();
958 if (AAInfo.TBAA) {
959 OS << ", !tbaa ";
960 AAInfo.TBAA->printAsOperand(OS, MST);
961 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000962 if (AAInfo.Scope) {
963 OS << ", !alias.scope ";
964 AAInfo.Scope->printAsOperand(OS, MST);
965 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000966 if (AAInfo.NoAlias) {
967 OS << ", !noalias ";
968 AAInfo.NoAlias->printAsOperand(OS, MST);
969 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000970 if (Op.getRanges()) {
971 OS << ", !range ";
972 Op.getRanges()->printAsOperand(OS, MST);
973 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000974 OS << ')';
975}
976
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000977static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
978 const TargetRegisterInfo *TRI) {
979 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
980 if (Reg == -1) {
981 OS << "<badreg>";
982 return;
983 }
984 printReg(Reg, OS, TRI);
985}
986
987void MIPrinter::print(const MCCFIInstruction &CFI,
988 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000989 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000990 case MCCFIInstruction::OpSameValue:
Matthias Braunee067922016-07-26 18:20:00 +0000991 OS << "same_value ";
Alex Lorenz577d2712015-08-14 21:55:58 +0000992 if (CFI.getLabel())
993 OS << "<mcsymbol> ";
994 printCFIRegister(CFI.getRegister(), OS, TRI);
995 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000996 case MCCFIInstruction::OpOffset:
Matthias Braunee067922016-07-26 18:20:00 +0000997 OS << "offset ";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000998 if (CFI.getLabel())
999 OS << "<mcsymbol> ";
1000 printCFIRegister(CFI.getRegister(), OS, TRI);
1001 OS << ", " << CFI.getOffset();
1002 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001003 case MCCFIInstruction::OpDefCfaRegister:
Matthias Braunee067922016-07-26 18:20:00 +00001004 OS << "def_cfa_register ";
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001005 if (CFI.getLabel())
1006 OS << "<mcsymbol> ";
1007 printCFIRegister(CFI.getRegister(), OS, TRI);
1008 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001009 case MCCFIInstruction::OpDefCfaOffset:
Matthias Braunee067922016-07-26 18:20:00 +00001010 OS << "def_cfa_offset ";
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001011 if (CFI.getLabel())
1012 OS << "<mcsymbol> ";
1013 OS << CFI.getOffset();
1014 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001015 case MCCFIInstruction::OpDefCfa:
Matthias Braunee067922016-07-26 18:20:00 +00001016 OS << "def_cfa ";
Alex Lorenzb1393232015-07-29 18:57:23 +00001017 if (CFI.getLabel())
1018 OS << "<mcsymbol> ";
1019 printCFIRegister(CFI.getRegister(), OS, TRI);
1020 OS << ", " << CFI.getOffset();
1021 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001022 default:
1023 // TODO: Print the other CFI Operations.
1024 OS << "<unserializable cfi operation>";
1025 break;
1026 }
1027}
1028
Alex Lorenz345c1442015-06-15 23:52:35 +00001029void llvm::printMIR(raw_ostream &OS, const Module &M) {
1030 yaml::Output Out(OS);
1031 Out << const_cast<Module &>(M);
1032}
1033
1034void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1035 MIRPrinter Printer(OS);
1036 Printer.print(MF);
1037}