blob: 10013a94ddb777b11ee53e0903984720b11041bf [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"
Alex Lorenzab980492015-07-20 20:51:18 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000018#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000023#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000024#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000025#include "llvm/IR/Constants.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000026#include "llvm/IR/Instructions.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000027#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000028#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000030#include "llvm/MC/MCSymbol.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000036
37using namespace llvm;
38
39namespace {
40
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000041/// This structure describes how to print out stack object references.
42struct FrameIndexOperand {
43 std::string Name;
44 unsigned ID;
45 bool IsFixed;
46
47 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
48 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
49
50 /// Return an ordinary stack object reference.
51 static FrameIndexOperand create(StringRef Name, unsigned ID) {
52 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
53 }
54
55 /// Return a fixed stack object reference.
56 static FrameIndexOperand createFixed(unsigned ID) {
57 return FrameIndexOperand("", ID, /*IsFixed=*/true);
58 }
59};
60
Alex Lorenz618b2832015-07-30 16:54:38 +000061} // end anonymous namespace
62
63namespace llvm {
64
Alex Lorenz345c1442015-06-15 23:52:35 +000065/// This class prints out the machine functions using the MIR serialization
66/// format.
67class MIRPrinter {
68 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000069 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000070 /// Maps from stack object indices to operand indices which will be used when
71 /// printing frame index machine operands.
72 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000073
74public:
75 MIRPrinter(raw_ostream &OS) : OS(OS) {}
76
77 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000078
Alex Lorenz28148ba2015-07-09 22:23:13 +000079 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
80 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000081 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
82 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000083 void convert(yaml::MachineFunction &MF,
84 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000085 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
86 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000087 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000088 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
89 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000090 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000091
92private:
93 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000094};
95
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000096/// This class prints out the machine instructions using the MIR serialization
97/// format.
98class MIPrinter {
99 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000100 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000101 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000102 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000103
104public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000105 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000106 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
107 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
108 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
109 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000110
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000111 void print(const MachineBasicBlock &MBB);
112
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000114 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000115 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000116 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000117 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000118 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000119 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000120 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000121 unsigned I, bool ShouldPrintRegisterTies,
122 const MachineRegisterInfo *MRI = nullptr, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000123 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000124
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000125 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126};
127
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000128} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000129
130namespace llvm {
131namespace yaml {
132
133/// This struct serializes the LLVM IR module.
134template <> struct BlockScalarTraits<Module> {
135 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
136 Mod.print(OS, nullptr);
137 }
138 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
139 llvm_unreachable("LLVM Module is supposed to be parsed separately");
140 return "";
141 }
142};
143
144} // end namespace yaml
145} // end namespace llvm
146
Alex Lorenz15a00a82015-07-14 21:18:25 +0000147static void printReg(unsigned Reg, raw_ostream &OS,
148 const TargetRegisterInfo *TRI) {
149 // TODO: Print Stack Slots.
150 if (!Reg)
151 OS << '_';
152 else if (TargetRegisterInfo::isVirtualRegister(Reg))
153 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
154 else if (Reg < TRI->getNumRegs())
155 OS << '%' << StringRef(TRI->getName(Reg)).lower();
156 else
157 llvm_unreachable("Can't print this kind of register yet");
158}
159
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000160static void printReg(unsigned Reg, yaml::StringValue &Dest,
161 const TargetRegisterInfo *TRI) {
162 raw_string_ostream OS(Dest.Value);
163 printReg(Reg, OS, TRI);
164}
165
Alex Lorenz345c1442015-06-15 23:52:35 +0000166void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000167 initRegisterMaskIds(MF);
168
Alex Lorenz345c1442015-06-15 23:52:35 +0000169 yaml::MachineFunction YamlMF;
170 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000171 YamlMF.Alignment = MF.getAlignment();
172 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
173 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Derek Schuffad154c82016-03-28 17:05:30 +0000174 YamlMF.AllVRegsAllocated = MF.getProperties().hasProperty(
175 MachineFunctionProperties::Property::AllVRegsAllocated);
176
Alex Lorenz28148ba2015-07-09 22:23:13 +0000177 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000178 ModuleSlotTracker MST(MF.getFunction()->getParent());
179 MST.incorporateFunction(*MF.getFunction());
180 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000181 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000182 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000183 if (const auto *ConstantPool = MF.getConstantPool())
184 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000185 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
186 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000187 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
188 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000189 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000190 if (IsNewlineNeeded)
191 StrOS << "\n";
192 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
193 .print(MBB);
194 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000195 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000196 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000197 yaml::Output Out(OS);
198 Out << YamlMF;
199}
200
Alex Lorenz54565cf2015-06-24 19:56:10 +0000201void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000202 const MachineRegisterInfo &RegInfo,
203 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000204 MF.IsSSA = RegInfo.isSSA();
205 MF.TracksRegLiveness = RegInfo.tracksLiveness();
206 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000207
208 // Print the virtual register definitions.
209 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
210 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
211 yaml::VirtualRegisterDefinition VReg;
212 VReg.ID = I;
Quentin Colombet050b2112016-03-08 01:17:03 +0000213 if (RegInfo.getRegClass(Reg))
214 VReg.Class =
215 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
216 else {
217 VReg.Class = std::string("_");
218 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
219 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000220 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
221 if (PreferredReg)
222 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000223 MF.VirtualRegisters.push_back(VReg);
224 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000225
226 // Print the live ins.
227 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
228 yaml::MachineFunctionLiveIn LiveIn;
229 printReg(I->first, LiveIn.Register, TRI);
230 if (I->second)
231 printReg(I->second, LiveIn.VirtualRegister, TRI);
232 MF.LiveIns.push_back(LiveIn);
233 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000234 // The used physical register mask is printed as an inverted callee saved
235 // register mask.
236 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
237 if (UsedPhysRegMask.none())
238 return;
239 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
240 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
241 if (!UsedPhysRegMask[I]) {
242 yaml::FlowStringValue Reg;
243 printReg(I, Reg, TRI);
244 CalleeSavedRegisters.push_back(Reg);
245 }
246 }
247 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000248}
249
Alex Lorenza6f9a372015-07-29 21:09:09 +0000250void MIRPrinter::convert(ModuleSlotTracker &MST,
251 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000252 const MachineFrameInfo &MFI) {
253 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
254 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
255 YamlMFI.HasStackMap = MFI.hasStackMap();
256 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
257 YamlMFI.StackSize = MFI.getStackSize();
258 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
259 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
260 YamlMFI.AdjustsStack = MFI.adjustsStack();
261 YamlMFI.HasCalls = MFI.hasCalls();
262 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
263 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
264 YamlMFI.HasVAStart = MFI.hasVAStart();
265 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000266 if (MFI.getSavePoint()) {
267 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
268 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
269 .printMBBReference(*MFI.getSavePoint());
270 }
271 if (MFI.getRestorePoint()) {
272 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
273 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
274 .printMBBReference(*MFI.getRestorePoint());
275 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000276}
277
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000278void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000279 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000280 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000281 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000282 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000283 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000284 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000285 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
286 if (MFI.isDeadObjectIndex(I))
287 continue;
288
289 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000290 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000291 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
292 ? yaml::FixedMachineStackObject::SpillSlot
293 : yaml::FixedMachineStackObject::DefaultType;
294 YamlObject.Offset = MFI.getObjectOffset(I);
295 YamlObject.Size = MFI.getObjectSize(I);
296 YamlObject.Alignment = MFI.getObjectAlignment(I);
297 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
298 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
299 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000300 StackObjectOperandMapping.insert(
301 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000302 }
303
304 // Process ordinary stack objects.
305 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000306 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
307 if (MFI.isDeadObjectIndex(I))
308 continue;
309
310 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000311 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000312 if (const auto *Alloca = MFI.getObjectAllocation(I))
313 YamlObject.Name.Value =
314 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000315 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
316 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000317 : MFI.isVariableSizedObjectIndex(I)
318 ? yaml::MachineStackObject::VariableSized
319 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000320 YamlObject.Offset = MFI.getObjectOffset(I);
321 YamlObject.Size = MFI.getObjectSize(I);
322 YamlObject.Alignment = MFI.getObjectAlignment(I);
323
324 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000325 StackObjectOperandMapping.insert(std::make_pair(
326 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000327 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000328
329 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
330 yaml::StringValue Reg;
331 printReg(CSInfo.getReg(), Reg, TRI);
332 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
333 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
334 "Invalid stack object index");
335 const FrameIndexOperand &StackObject = StackObjectInfo->second;
336 if (StackObject.IsFixed)
337 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
338 else
339 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
340 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000341 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
342 auto LocalObject = MFI.getLocalFrameObjectMap(I);
343 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
344 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
345 "Invalid stack object index");
346 const FrameIndexOperand &StackObject = StackObjectInfo->second;
347 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
348 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
349 }
Alex Lorenza314d812015-08-18 22:26:26 +0000350
351 // Print the stack object references in the frame information class after
352 // converting the stack objects.
353 if (MFI.hasStackProtectorIndex()) {
354 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
355 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
356 .printStackObjectReference(MFI.getStackProtectorIndex());
357 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000358
359 // Print the debug variable information.
360 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
361 MMI.getVariableDbgInfo()) {
362 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
363 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
364 "Invalid stack object index");
365 const FrameIndexOperand &StackObject = StackObjectInfo->second;
366 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
367 auto &Object = MF.StackObjects[StackObject.ID];
368 {
369 raw_string_ostream StrOS(Object.DebugVar.Value);
370 DebugVar.Var->printAsOperand(StrOS, MST);
371 }
372 {
373 raw_string_ostream StrOS(Object.DebugExpr.Value);
374 DebugVar.Expr->printAsOperand(StrOS, MST);
375 }
376 {
377 raw_string_ostream StrOS(Object.DebugLoc.Value);
378 DebugVar.Loc->printAsOperand(StrOS, MST);
379 }
380 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000381}
382
Alex Lorenzab980492015-07-20 20:51:18 +0000383void MIRPrinter::convert(yaml::MachineFunction &MF,
384 const MachineConstantPool &ConstantPool) {
385 unsigned ID = 0;
386 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
387 // TODO: Serialize target specific constant pool entries.
388 if (Constant.isMachineConstantPoolEntry())
389 llvm_unreachable("Can't print target specific constant pool entries yet");
390
391 yaml::MachineConstantPoolValue YamlConstant;
392 std::string Str;
393 raw_string_ostream StrOS(Str);
394 Constant.Val.ConstVal->printAsOperand(StrOS);
395 YamlConstant.ID = ID++;
396 YamlConstant.Value = StrOS.str();
397 YamlConstant.Alignment = Constant.getAlignment();
398 MF.Constants.push_back(YamlConstant);
399 }
400}
401
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000402void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000403 yaml::MachineJumpTable &YamlJTI,
404 const MachineJumpTableInfo &JTI) {
405 YamlJTI.Kind = JTI.getEntryKind();
406 unsigned ID = 0;
407 for (const auto &Table : JTI.getJumpTables()) {
408 std::string Str;
409 yaml::MachineJumpTable::Entry Entry;
410 Entry.ID = ID++;
411 for (const auto *MBB : Table.MBBs) {
412 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000413 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
414 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000415 Entry.Blocks.push_back(StrOS.str());
416 Str.clear();
417 }
418 YamlJTI.Entries.push_back(Entry);
419 }
420}
421
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000422void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
423 const auto *TRI = MF.getSubtarget().getRegisterInfo();
424 unsigned I = 0;
425 for (const uint32_t *Mask : TRI->getRegMasks())
426 RegisterMaskIds.insert(std::make_pair(Mask, I++));
427}
428
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000429void MIPrinter::print(const MachineBasicBlock &MBB) {
430 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
431 OS << "bb." << MBB.getNumber();
432 bool HasAttributes = false;
433 if (const auto *BB = MBB.getBasicBlock()) {
434 if (BB->hasName()) {
435 OS << "." << BB->getName();
436 } else {
437 HasAttributes = true;
438 OS << " (";
439 int Slot = MST.getLocalSlot(BB);
440 if (Slot == -1)
441 OS << "<ir-block badref>";
442 else
443 OS << (Twine("%ir-block.") + Twine(Slot)).str();
444 }
445 }
446 if (MBB.hasAddressTaken()) {
447 OS << (HasAttributes ? ", " : " (");
448 OS << "address-taken";
449 HasAttributes = true;
450 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000451 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000452 OS << (HasAttributes ? ", " : " (");
453 OS << "landing-pad";
454 HasAttributes = true;
455 }
456 if (MBB.getAlignment()) {
457 OS << (HasAttributes ? ", " : " (");
458 OS << "align " << MBB.getAlignment();
459 HasAttributes = true;
460 }
461 if (HasAttributes)
462 OS << ")";
463 OS << ":\n";
464
465 bool HasLineAttributes = false;
466 // Print the successors
467 if (!MBB.succ_empty()) {
468 OS.indent(2) << "successors: ";
469 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
470 if (I != MBB.succ_begin())
471 OS << ", ";
472 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000473 if (MBB.hasSuccessorProbabilities())
474 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000475 }
476 OS << "\n";
477 HasLineAttributes = true;
478 }
479
480 // Print the live in registers.
481 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
482 assert(TRI && "Expected target register info");
483 if (!MBB.livein_empty()) {
484 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000485 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000486 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000487 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000488 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000489 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000490 printReg(LI.PhysReg, OS, TRI);
491 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000492 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000493 }
494 OS << "\n";
495 HasLineAttributes = true;
496 }
497
498 if (HasLineAttributes)
499 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000500 bool IsInBundle = false;
501 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
502 const MachineInstr &MI = *I;
503 if (IsInBundle && !MI.isInsideBundle()) {
504 OS.indent(2) << "}\n";
505 IsInBundle = false;
506 }
507 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000508 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000509 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
510 OS << " {";
511 IsInBundle = true;
512 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000513 OS << "\n";
514 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000515 if (IsInBundle)
516 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000517}
518
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000519/// Return true when an instruction has tied register that can't be determined
520/// by the instruction's descriptor.
521static bool hasComplexRegisterTies(const MachineInstr &MI) {
522 const MCInstrDesc &MCID = MI.getDesc();
523 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
524 const auto &Operand = MI.getOperand(I);
525 if (!Operand.isReg() || Operand.isDef())
526 // Ignore the defined registers as MCID marks only the uses as tied.
527 continue;
528 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
529 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
530 if (ExpectedTiedIdx != TiedIdx)
531 return true;
532 }
533 return false;
534}
535
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000536void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000537 const auto *MF = MI.getParent()->getParent();
538 const auto &MRI = MF->getRegInfo();
539 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000540 const auto *TRI = SubTarget.getRegisterInfo();
541 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000542 const auto *TII = SubTarget.getInstrInfo();
543 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000544 if (MI.isCFIInstruction())
545 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000546
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000547 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 unsigned I = 0, E = MI.getNumOperands();
549 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
550 !MI.getOperand(I).isImplicit();
551 ++I) {
552 if (I)
553 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000554 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
555 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000556 }
557
558 if (I)
559 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000560 if (MI.getFlag(MachineInstr::FrameSetup))
561 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000562 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000563 if (isPreISelGenericOpcode(MI.getOpcode())) {
564 assert(MI.getType() && "Generic instructions must have a type");
Quentin Colombetd6554832016-03-08 00:38:01 +0000565 OS << ' ';
566 MI.getType()->print(OS, /*IsForDebug*/ false, /*NoDetails*/ true);
Quentin Colombet12350a82016-03-08 00:29:15 +0000567 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000568 if (I < E)
569 OS << ' ';
570
571 bool NeedComma = false;
572 for (; I < E; ++I) {
573 if (NeedComma)
574 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000575 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000576 NeedComma = true;
577 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000578
579 if (MI.getDebugLoc()) {
580 if (NeedComma)
581 OS << ',';
582 OS << " debug-location ";
583 MI.getDebugLoc()->printAsOperand(OS, MST);
584 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000585
586 if (!MI.memoperands_empty()) {
587 OS << " :: ";
588 bool NeedComma = false;
589 for (const auto *Op : MI.memoperands()) {
590 if (NeedComma)
591 OS << ", ";
592 print(*Op);
593 NeedComma = true;
594 }
595 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000596}
597
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000598void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
599 OS << "%bb." << MBB.getNumber();
600 if (const auto *BB = MBB.getBasicBlock()) {
601 if (BB->hasName())
602 OS << '.' << BB->getName();
603 }
604}
605
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000606static void printIRSlotNumber(raw_ostream &OS, int Slot) {
607 if (Slot == -1)
608 OS << "<badref>";
609 else
610 OS << Slot;
611}
612
Alex Lorenzdeb53492015-07-28 17:28:03 +0000613void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
614 OS << "%ir-block.";
615 if (BB.hasName()) {
616 printLLVMNameWithoutPrefix(OS, BB.getName());
617 return;
618 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000619 const Function *F = BB.getParent();
620 int Slot;
621 if (F == MST.getCurrentFunction()) {
622 Slot = MST.getLocalSlot(&BB);
623 } else {
624 ModuleSlotTracker CustomMST(F->getParent(),
625 /*ShouldInitializeAllMetadata=*/false);
626 CustomMST.incorporateFunction(*F);
627 Slot = CustomMST.getLocalSlot(&BB);
628 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000629 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000630}
631
Alex Lorenz4af7e612015-08-03 23:08:19 +0000632void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000633 if (isa<GlobalValue>(V)) {
634 V.printAsOperand(OS, /*PrintType=*/false, MST);
635 return;
636 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000637 if (isa<Constant>(V)) {
638 // Machine memory operands can load/store to/from constant value pointers.
639 OS << '`';
640 V.printAsOperand(OS, /*PrintType=*/true, MST);
641 OS << '`';
642 return;
643 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000644 OS << "%ir.";
645 if (V.hasName()) {
646 printLLVMNameWithoutPrefix(OS, V.getName());
647 return;
648 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000649 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000650}
651
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000652void MIPrinter::printStackObjectReference(int FrameIndex) {
653 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
654 assert(ObjectInfo != StackObjectOperandMapping.end() &&
655 "Invalid frame index");
656 const FrameIndexOperand &Operand = ObjectInfo->second;
657 if (Operand.IsFixed) {
658 OS << "%fixed-stack." << Operand.ID;
659 return;
660 }
661 OS << "%stack." << Operand.ID;
662 if (!Operand.Name.empty())
663 OS << '.' << Operand.Name;
664}
665
Alex Lorenz5672a892015-08-05 22:26:15 +0000666void MIPrinter::printOffset(int64_t Offset) {
667 if (Offset == 0)
668 return;
669 if (Offset < 0) {
670 OS << " - " << -Offset;
671 return;
672 }
673 OS << " + " << Offset;
674}
675
Alex Lorenz49873a82015-08-06 00:44:07 +0000676static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
677 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
678 for (const auto &I : Flags) {
679 if (I.first == TF) {
680 return I.second;
681 }
682 }
683 return nullptr;
684}
685
686void MIPrinter::printTargetFlags(const MachineOperand &Op) {
687 if (!Op.getTargetFlags())
688 return;
689 const auto *TII =
690 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
691 assert(TII && "expected instruction info");
692 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
693 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000694 const bool HasDirectFlags = Flags.first;
695 const bool HasBitmaskFlags = Flags.second;
696 if (!HasDirectFlags && !HasBitmaskFlags) {
697 OS << "<unknown>) ";
698 return;
699 }
700 if (HasDirectFlags) {
701 if (const auto *Name = getTargetFlagName(TII, Flags.first))
702 OS << Name;
703 else
704 OS << "<unknown target flag>";
705 }
706 if (!HasBitmaskFlags) {
707 OS << ") ";
708 return;
709 }
710 bool IsCommaNeeded = HasDirectFlags;
711 unsigned BitMask = Flags.second;
712 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
713 for (const auto &Mask : BitMasks) {
714 // Check if the flag's bitmask has the bits of the current mask set.
715 if ((BitMask & Mask.first) == Mask.first) {
716 if (IsCommaNeeded)
717 OS << ", ";
718 IsCommaNeeded = true;
719 OS << Mask.second;
720 // Clear the bits which were serialized from the flag's bitmask.
721 BitMask &= ~(Mask.first);
722 }
723 }
724 if (BitMask) {
725 // When the resulting flag's bitmask isn't zero, we know that we didn't
726 // serialize all of the bit flags.
727 if (IsCommaNeeded)
728 OS << ", ";
729 OS << "<unknown bitmask target flag>";
730 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000731 OS << ") ";
732}
733
Alex Lorenzef5c1962015-07-28 23:02:45 +0000734static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
735 const auto *TII = MF.getSubtarget().getInstrInfo();
736 assert(TII && "expected instruction info");
737 auto Indices = TII->getSerializableTargetIndices();
738 for (const auto &I : Indices) {
739 if (I.first == Index) {
740 return I.second;
741 }
742 }
743 return nullptr;
744}
745
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000746void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000747 unsigned I, bool ShouldPrintRegisterTies,
748 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000749 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000750 switch (Op.getType()) {
751 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000752 if (Op.isImplicit())
753 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000754 else if (!IsDef && Op.isDef())
755 // Print the 'def' flag only when the operand is defined after '='.
756 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000757 if (Op.isInternalRead())
758 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000759 if (Op.isDead())
760 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000761 if (Op.isKill())
762 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000763 if (Op.isUndef())
764 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000765 if (Op.isEarlyClobber())
766 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000767 if (Op.isDebug())
768 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000769 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000770 // Print the sub register.
771 if (Op.getSubReg() != 0)
772 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000773 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
774 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000775 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
776 if (IsDef && MRI->getSize(Op.getReg()))
777 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000778 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000779 case MachineOperand::MO_Immediate:
780 OS << Op.getImm();
781 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000782 case MachineOperand::MO_CImmediate:
783 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
784 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000785 case MachineOperand::MO_FPImmediate:
786 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
787 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000788 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000789 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000790 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000791 case MachineOperand::MO_FrameIndex:
792 printStackObjectReference(Op.getIndex());
793 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000794 case MachineOperand::MO_ConstantPoolIndex:
795 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000796 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000797 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000798 case MachineOperand::MO_TargetIndex: {
799 OS << "target-index(";
800 if (const auto *Name = getTargetIndexName(
801 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
802 OS << Name;
803 else
804 OS << "<unknown>";
805 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000806 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000807 break;
808 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000809 case MachineOperand::MO_JumpTableIndex:
810 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000811 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000812 case MachineOperand::MO_ExternalSymbol:
813 OS << '$';
814 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000815 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000816 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000817 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000818 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000819 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000820 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000821 case MachineOperand::MO_BlockAddress:
822 OS << "blockaddress(";
823 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
824 MST);
825 OS << ", ";
826 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
827 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000828 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000829 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000830 case MachineOperand::MO_RegisterMask: {
831 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
832 if (RegMaskInfo != RegisterMaskIds.end())
833 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
834 else
835 llvm_unreachable("Can't print this machine register mask yet.");
836 break;
837 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000838 case MachineOperand::MO_RegisterLiveOut: {
839 const uint32_t *RegMask = Op.getRegLiveOut();
840 OS << "liveout(";
841 bool IsCommaNeeded = false;
842 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
843 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
844 if (IsCommaNeeded)
845 OS << ", ";
846 printReg(Reg, OS, TRI);
847 IsCommaNeeded = true;
848 }
849 }
850 OS << ")";
851 break;
852 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000853 case MachineOperand::MO_Metadata:
854 Op.getMetadata()->printAsOperand(OS, MST);
855 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000856 case MachineOperand::MO_MCSymbol:
857 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
858 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000859 case MachineOperand::MO_CFIIndex: {
860 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000861 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000862 break;
863 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000864 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000865}
866
Alex Lorenz4af7e612015-08-03 23:08:19 +0000867void MIPrinter::print(const MachineMemOperand &Op) {
868 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000869 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000870 if (Op.isVolatile())
871 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000872 if (Op.isNonTemporal())
873 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000874 if (Op.isInvariant())
875 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000876 if (Op.isLoad())
877 OS << "load ";
878 else {
879 assert(Op.isStore() && "Non load machine operand must be a store");
880 OS << "store ";
881 }
882 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000883 if (const Value *Val = Op.getValue()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000884 printIRValueReference(*Val);
Alex Lorenz91097a32015-08-12 20:33:26 +0000885 } else {
886 const PseudoSourceValue *PVal = Op.getPseudoValue();
887 assert(PVal && "Expected a pseudo source value");
888 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000889 case PseudoSourceValue::Stack:
890 OS << "stack";
891 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000892 case PseudoSourceValue::GOT:
893 OS << "got";
894 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000895 case PseudoSourceValue::JumpTable:
896 OS << "jump-table";
897 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000898 case PseudoSourceValue::ConstantPool:
899 OS << "constant-pool";
900 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000901 case PseudoSourceValue::FixedStack:
902 printStackObjectReference(
903 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
904 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000905 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000906 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000907 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
908 OS, /*PrintType=*/false, MST);
909 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000910 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000911 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000912 printLLVMNameWithoutPrefix(
913 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000914 break;
915 }
916 }
Alex Lorenz83127732015-08-07 20:26:52 +0000917 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000918 if (Op.getBaseAlignment() != Op.getSize())
919 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000920 auto AAInfo = Op.getAAInfo();
921 if (AAInfo.TBAA) {
922 OS << ", !tbaa ";
923 AAInfo.TBAA->printAsOperand(OS, MST);
924 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000925 if (AAInfo.Scope) {
926 OS << ", !alias.scope ";
927 AAInfo.Scope->printAsOperand(OS, MST);
928 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000929 if (AAInfo.NoAlias) {
930 OS << ", !noalias ";
931 AAInfo.NoAlias->printAsOperand(OS, MST);
932 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000933 if (Op.getRanges()) {
934 OS << ", !range ";
935 Op.getRanges()->printAsOperand(OS, MST);
936 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000937 OS << ')';
938}
939
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000940static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
941 const TargetRegisterInfo *TRI) {
942 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
943 if (Reg == -1) {
944 OS << "<badreg>";
945 return;
946 }
947 printReg(Reg, OS, TRI);
948}
949
950void MIPrinter::print(const MCCFIInstruction &CFI,
951 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000952 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000953 case MCCFIInstruction::OpSameValue:
954 OS << ".cfi_same_value ";
955 if (CFI.getLabel())
956 OS << "<mcsymbol> ";
957 printCFIRegister(CFI.getRegister(), OS, TRI);
958 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000959 case MCCFIInstruction::OpOffset:
960 OS << ".cfi_offset ";
961 if (CFI.getLabel())
962 OS << "<mcsymbol> ";
963 printCFIRegister(CFI.getRegister(), OS, TRI);
964 OS << ", " << CFI.getOffset();
965 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000966 case MCCFIInstruction::OpDefCfaRegister:
967 OS << ".cfi_def_cfa_register ";
968 if (CFI.getLabel())
969 OS << "<mcsymbol> ";
970 printCFIRegister(CFI.getRegister(), OS, TRI);
971 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000972 case MCCFIInstruction::OpDefCfaOffset:
973 OS << ".cfi_def_cfa_offset ";
974 if (CFI.getLabel())
975 OS << "<mcsymbol> ";
976 OS << CFI.getOffset();
977 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000978 case MCCFIInstruction::OpDefCfa:
979 OS << ".cfi_def_cfa ";
980 if (CFI.getLabel())
981 OS << "<mcsymbol> ";
982 printCFIRegister(CFI.getRegister(), OS, TRI);
983 OS << ", " << CFI.getOffset();
984 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000985 default:
986 // TODO: Print the other CFI Operations.
987 OS << "<unserializable cfi operation>";
988 break;
989 }
990}
991
Alex Lorenz345c1442015-06-15 23:52:35 +0000992void llvm::printMIR(raw_ostream &OS, const Module &M) {
993 yaml::Output Out(OS);
994 Out << const_cast<Module &>(M);
995}
996
997void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
998 MIRPrinter Printer(OS);
999 Printer.print(MF);
1000}