blob: 703c99d9edd3d53ee860ff69f539d22a2b61baf6 [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"
Alex Lorenz345c1442015-06-15 23:52:35 +000030#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000031#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000032#include "llvm/MC/MCSymbol.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000033#include "llvm/Support/MemoryBuffer.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000034#include "llvm/Support/YAMLTraits.h"
Quentin Colombetfab1cfe2016-04-08 16:26:22 +000035#include "llvm/Support/raw_ostream.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000038
39using namespace llvm;
40
41namespace {
42
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000043/// This structure describes how to print out stack object references.
44struct FrameIndexOperand {
45 std::string Name;
46 unsigned ID;
47 bool IsFixed;
48
49 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
50 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
51
52 /// Return an ordinary stack object reference.
53 static FrameIndexOperand create(StringRef Name, unsigned ID) {
54 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
55 }
56
57 /// Return a fixed stack object reference.
58 static FrameIndexOperand createFixed(unsigned ID) {
59 return FrameIndexOperand("", ID, /*IsFixed=*/true);
60 }
61};
62
Alex Lorenz618b2832015-07-30 16:54:38 +000063} // end anonymous namespace
64
65namespace llvm {
66
Alex Lorenz345c1442015-06-15 23:52:35 +000067/// This class prints out the machine functions using the MIR serialization
68/// format.
69class MIRPrinter {
70 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000071 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000072 /// Maps from stack object indices to operand indices which will be used when
73 /// printing frame index machine operands.
74 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000075
76public:
77 MIRPrinter(raw_ostream &OS) : OS(OS) {}
78
79 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000080
Alex Lorenz28148ba2015-07-09 22:23:13 +000081 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
82 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000083 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
84 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000085 void convert(yaml::MachineFunction &MF,
86 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000087 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
88 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000089 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000090 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
91 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000092 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000093
94private:
95 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000096};
97
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000098/// This class prints out the machine instructions using the MIR serialization
99/// format.
100class MIPrinter {
101 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000102 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000103 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000104 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000105
106public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000107 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000108 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
109 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
110 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
111 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000112
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000113 void print(const MachineBasicBlock &MBB);
114
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000115 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000116 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000117 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000118 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000119 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000120 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000121 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000122 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000123 unsigned I, bool ShouldPrintRegisterTies,
124 const MachineRegisterInfo *MRI = nullptr, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000125 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000126
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000127 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000128};
129
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000130} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000131
132namespace llvm {
133namespace yaml {
134
135/// This struct serializes the LLVM IR module.
136template <> struct BlockScalarTraits<Module> {
137 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
138 Mod.print(OS, nullptr);
139 }
140 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
141 llvm_unreachable("LLVM Module is supposed to be parsed separately");
142 return "";
143 }
144};
145
146} // end namespace yaml
147} // end namespace llvm
148
Alex Lorenz15a00a82015-07-14 21:18:25 +0000149static void printReg(unsigned Reg, raw_ostream &OS,
150 const TargetRegisterInfo *TRI) {
151 // TODO: Print Stack Slots.
152 if (!Reg)
153 OS << '_';
154 else if (TargetRegisterInfo::isVirtualRegister(Reg))
155 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
156 else if (Reg < TRI->getNumRegs())
157 OS << '%' << StringRef(TRI->getName(Reg)).lower();
158 else
159 llvm_unreachable("Can't print this kind of register yet");
160}
161
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000162static void printReg(unsigned Reg, yaml::StringValue &Dest,
163 const TargetRegisterInfo *TRI) {
164 raw_string_ostream OS(Dest.Value);
165 printReg(Reg, OS, TRI);
166}
167
Alex Lorenz345c1442015-06-15 23:52:35 +0000168void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000169 initRegisterMaskIds(MF);
170
Alex Lorenz345c1442015-06-15 23:52:35 +0000171 yaml::MachineFunction YamlMF;
172 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000173 YamlMF.Alignment = MF.getAlignment();
174 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
175 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Derek Schuffad154c82016-03-28 17:05:30 +0000176 YamlMF.AllVRegsAllocated = MF.getProperties().hasProperty(
177 MachineFunctionProperties::Property::AllVRegsAllocated);
178
Alex Lorenz28148ba2015-07-09 22:23:13 +0000179 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000180 ModuleSlotTracker MST(MF.getFunction()->getParent());
181 MST.incorporateFunction(*MF.getFunction());
182 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000183 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000184 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000185 if (const auto *ConstantPool = MF.getConstantPool())
186 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000187 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
188 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000189 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
190 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000191 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000192 if (IsNewlineNeeded)
193 StrOS << "\n";
194 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
195 .print(MBB);
196 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000197 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000198 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000199 yaml::Output Out(OS);
200 Out << YamlMF;
201}
202
Alex Lorenz54565cf2015-06-24 19:56:10 +0000203void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000204 const MachineRegisterInfo &RegInfo,
205 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000206 MF.IsSSA = RegInfo.isSSA();
207 MF.TracksRegLiveness = RegInfo.tracksLiveness();
208 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000209
210 // Print the virtual register definitions.
211 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
212 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
213 yaml::VirtualRegisterDefinition VReg;
214 VReg.ID = I;
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000215 if (RegInfo.getRegClassOrNull(Reg))
Quentin Colombet050b2112016-03-08 01:17:03 +0000216 VReg.Class =
217 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Quentin Colombetfab1cfe2016-04-08 16:26:22 +0000218 else if (RegInfo.getRegBankOrNull(Reg))
219 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
Quentin Colombet050b2112016-03-08 01:17:03 +0000220 else {
221 VReg.Class = std::string("_");
222 assert(RegInfo.getSize(Reg) && "Generic registers must have a size");
223 }
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000224 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
225 if (PreferredReg)
226 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000227 MF.VirtualRegisters.push_back(VReg);
228 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000229
230 // Print the live ins.
231 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
232 yaml::MachineFunctionLiveIn LiveIn;
233 printReg(I->first, LiveIn.Register, TRI);
234 if (I->second)
235 printReg(I->second, LiveIn.VirtualRegister, TRI);
236 MF.LiveIns.push_back(LiveIn);
237 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000238 // The used physical register mask is printed as an inverted callee saved
239 // register mask.
240 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
241 if (UsedPhysRegMask.none())
242 return;
243 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
244 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
245 if (!UsedPhysRegMask[I]) {
246 yaml::FlowStringValue Reg;
247 printReg(I, Reg, TRI);
248 CalleeSavedRegisters.push_back(Reg);
249 }
250 }
251 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000252}
253
Alex Lorenza6f9a372015-07-29 21:09:09 +0000254void MIRPrinter::convert(ModuleSlotTracker &MST,
255 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000256 const MachineFrameInfo &MFI) {
257 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
258 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
259 YamlMFI.HasStackMap = MFI.hasStackMap();
260 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
261 YamlMFI.StackSize = MFI.getStackSize();
262 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
263 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
264 YamlMFI.AdjustsStack = MFI.adjustsStack();
265 YamlMFI.HasCalls = MFI.hasCalls();
266 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
267 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
268 YamlMFI.HasVAStart = MFI.hasVAStart();
269 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000270 if (MFI.getSavePoint()) {
271 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
272 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
273 .printMBBReference(*MFI.getSavePoint());
274 }
275 if (MFI.getRestorePoint()) {
276 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
277 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
278 .printMBBReference(*MFI.getRestorePoint());
279 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000280}
281
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000282void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000283 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000284 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000285 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000286 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000287 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000288 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000289 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
290 if (MFI.isDeadObjectIndex(I))
291 continue;
292
293 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000294 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000295 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
296 ? yaml::FixedMachineStackObject::SpillSlot
297 : yaml::FixedMachineStackObject::DefaultType;
298 YamlObject.Offset = MFI.getObjectOffset(I);
299 YamlObject.Size = MFI.getObjectSize(I);
300 YamlObject.Alignment = MFI.getObjectAlignment(I);
301 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
302 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
303 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000304 StackObjectOperandMapping.insert(
305 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000306 }
307
308 // Process ordinary stack objects.
309 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000310 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
311 if (MFI.isDeadObjectIndex(I))
312 continue;
313
314 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000315 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000316 if (const auto *Alloca = MFI.getObjectAllocation(I))
317 YamlObject.Name.Value =
318 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000319 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
320 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000321 : MFI.isVariableSizedObjectIndex(I)
322 ? yaml::MachineStackObject::VariableSized
323 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000324 YamlObject.Offset = MFI.getObjectOffset(I);
325 YamlObject.Size = MFI.getObjectSize(I);
326 YamlObject.Alignment = MFI.getObjectAlignment(I);
327
328 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000329 StackObjectOperandMapping.insert(std::make_pair(
330 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000331 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000332
333 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
334 yaml::StringValue Reg;
335 printReg(CSInfo.getReg(), Reg, TRI);
336 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
337 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
338 "Invalid stack object index");
339 const FrameIndexOperand &StackObject = StackObjectInfo->second;
340 if (StackObject.IsFixed)
341 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
342 else
343 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
344 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000345 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
346 auto LocalObject = MFI.getLocalFrameObjectMap(I);
347 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
348 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
349 "Invalid stack object index");
350 const FrameIndexOperand &StackObject = StackObjectInfo->second;
351 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
352 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
353 }
Alex Lorenza314d812015-08-18 22:26:26 +0000354
355 // Print the stack object references in the frame information class after
356 // converting the stack objects.
357 if (MFI.hasStackProtectorIndex()) {
358 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
359 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
360 .printStackObjectReference(MFI.getStackProtectorIndex());
361 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000362
363 // Print the debug variable information.
364 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
365 MMI.getVariableDbgInfo()) {
366 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
367 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
368 "Invalid stack object index");
369 const FrameIndexOperand &StackObject = StackObjectInfo->second;
370 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
371 auto &Object = MF.StackObjects[StackObject.ID];
372 {
373 raw_string_ostream StrOS(Object.DebugVar.Value);
374 DebugVar.Var->printAsOperand(StrOS, MST);
375 }
376 {
377 raw_string_ostream StrOS(Object.DebugExpr.Value);
378 DebugVar.Expr->printAsOperand(StrOS, MST);
379 }
380 {
381 raw_string_ostream StrOS(Object.DebugLoc.Value);
382 DebugVar.Loc->printAsOperand(StrOS, MST);
383 }
384 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000385}
386
Alex Lorenzab980492015-07-20 20:51:18 +0000387void MIRPrinter::convert(yaml::MachineFunction &MF,
388 const MachineConstantPool &ConstantPool) {
389 unsigned ID = 0;
390 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
391 // TODO: Serialize target specific constant pool entries.
392 if (Constant.isMachineConstantPoolEntry())
393 llvm_unreachable("Can't print target specific constant pool entries yet");
394
395 yaml::MachineConstantPoolValue YamlConstant;
396 std::string Str;
397 raw_string_ostream StrOS(Str);
398 Constant.Val.ConstVal->printAsOperand(StrOS);
399 YamlConstant.ID = ID++;
400 YamlConstant.Value = StrOS.str();
401 YamlConstant.Alignment = Constant.getAlignment();
402 MF.Constants.push_back(YamlConstant);
403 }
404}
405
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000406void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000407 yaml::MachineJumpTable &YamlJTI,
408 const MachineJumpTableInfo &JTI) {
409 YamlJTI.Kind = JTI.getEntryKind();
410 unsigned ID = 0;
411 for (const auto &Table : JTI.getJumpTables()) {
412 std::string Str;
413 yaml::MachineJumpTable::Entry Entry;
414 Entry.ID = ID++;
415 for (const auto *MBB : Table.MBBs) {
416 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000417 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
418 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000419 Entry.Blocks.push_back(StrOS.str());
420 Str.clear();
421 }
422 YamlJTI.Entries.push_back(Entry);
423 }
424}
425
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000426void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
427 const auto *TRI = MF.getSubtarget().getRegisterInfo();
428 unsigned I = 0;
429 for (const uint32_t *Mask : TRI->getRegMasks())
430 RegisterMaskIds.insert(std::make_pair(Mask, I++));
431}
432
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000433void MIPrinter::print(const MachineBasicBlock &MBB) {
434 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
435 OS << "bb." << MBB.getNumber();
436 bool HasAttributes = false;
437 if (const auto *BB = MBB.getBasicBlock()) {
438 if (BB->hasName()) {
439 OS << "." << BB->getName();
440 } else {
441 HasAttributes = true;
442 OS << " (";
443 int Slot = MST.getLocalSlot(BB);
444 if (Slot == -1)
445 OS << "<ir-block badref>";
446 else
447 OS << (Twine("%ir-block.") + Twine(Slot)).str();
448 }
449 }
450 if (MBB.hasAddressTaken()) {
451 OS << (HasAttributes ? ", " : " (");
452 OS << "address-taken";
453 HasAttributes = true;
454 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000455 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000456 OS << (HasAttributes ? ", " : " (");
457 OS << "landing-pad";
458 HasAttributes = true;
459 }
460 if (MBB.getAlignment()) {
461 OS << (HasAttributes ? ", " : " (");
462 OS << "align " << MBB.getAlignment();
463 HasAttributes = true;
464 }
465 if (HasAttributes)
466 OS << ")";
467 OS << ":\n";
468
469 bool HasLineAttributes = false;
470 // Print the successors
471 if (!MBB.succ_empty()) {
472 OS.indent(2) << "successors: ";
473 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
474 if (I != MBB.succ_begin())
475 OS << ", ";
476 printMBBReference(**I);
Cong Houd97c1002015-12-01 05:29:22 +0000477 if (MBB.hasSuccessorProbabilities())
478 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000479 }
480 OS << "\n";
481 HasLineAttributes = true;
482 }
483
484 // Print the live in registers.
485 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
486 assert(TRI && "Expected target register info");
487 if (!MBB.livein_empty()) {
488 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000489 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000490 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000491 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000492 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000493 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000494 printReg(LI.PhysReg, OS, TRI);
495 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000496 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000497 }
498 OS << "\n";
499 HasLineAttributes = true;
500 }
501
502 if (HasLineAttributes)
503 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000504 bool IsInBundle = false;
505 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
506 const MachineInstr &MI = *I;
507 if (IsInBundle && !MI.isInsideBundle()) {
508 OS.indent(2) << "}\n";
509 IsInBundle = false;
510 }
511 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000512 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000513 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
514 OS << " {";
515 IsInBundle = true;
516 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000517 OS << "\n";
518 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000519 if (IsInBundle)
520 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000521}
522
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000523/// Return true when an instruction has tied register that can't be determined
524/// by the instruction's descriptor.
525static bool hasComplexRegisterTies(const MachineInstr &MI) {
526 const MCInstrDesc &MCID = MI.getDesc();
527 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
528 const auto &Operand = MI.getOperand(I);
529 if (!Operand.isReg() || Operand.isDef())
530 // Ignore the defined registers as MCID marks only the uses as tied.
531 continue;
532 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
533 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
534 if (ExpectedTiedIdx != TiedIdx)
535 return true;
536 }
537 return false;
538}
539
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000540void MIPrinter::print(const MachineInstr &MI) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000541 const auto *MF = MI.getParent()->getParent();
542 const auto &MRI = MF->getRegInfo();
543 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000544 const auto *TRI = SubTarget.getRegisterInfo();
545 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000546 const auto *TII = SubTarget.getInstrInfo();
547 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000548 if (MI.isCFIInstruction())
549 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000550
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000551 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000552 unsigned I = 0, E = MI.getNumOperands();
553 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
554 !MI.getOperand(I).isImplicit();
555 ++I) {
556 if (I)
557 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000558 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
559 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000560 }
561
562 if (I)
563 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000564 if (MI.getFlag(MachineInstr::FrameSetup))
565 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000566 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000567 if (isPreISelGenericOpcode(MI.getOpcode())) {
568 assert(MI.getType() && "Generic instructions must have a type");
Quentin Colombetd6554832016-03-08 00:38:01 +0000569 OS << ' ';
570 MI.getType()->print(OS, /*IsForDebug*/ false, /*NoDetails*/ true);
Quentin Colombet12350a82016-03-08 00:29:15 +0000571 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000572 if (I < E)
573 OS << ' ';
574
575 bool NeedComma = false;
576 for (; I < E; ++I) {
577 if (NeedComma)
578 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000579 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580 NeedComma = true;
581 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000582
583 if (MI.getDebugLoc()) {
584 if (NeedComma)
585 OS << ',';
586 OS << " debug-location ";
587 MI.getDebugLoc()->printAsOperand(OS, MST);
588 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000589
590 if (!MI.memoperands_empty()) {
591 OS << " :: ";
592 bool NeedComma = false;
593 for (const auto *Op : MI.memoperands()) {
594 if (NeedComma)
595 OS << ", ";
596 print(*Op);
597 NeedComma = true;
598 }
599 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000600}
601
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000602void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
603 OS << "%bb." << MBB.getNumber();
604 if (const auto *BB = MBB.getBasicBlock()) {
605 if (BB->hasName())
606 OS << '.' << BB->getName();
607 }
608}
609
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000610static void printIRSlotNumber(raw_ostream &OS, int Slot) {
611 if (Slot == -1)
612 OS << "<badref>";
613 else
614 OS << Slot;
615}
616
Alex Lorenzdeb53492015-07-28 17:28:03 +0000617void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
618 OS << "%ir-block.";
619 if (BB.hasName()) {
620 printLLVMNameWithoutPrefix(OS, BB.getName());
621 return;
622 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000623 const Function *F = BB.getParent();
624 int Slot;
625 if (F == MST.getCurrentFunction()) {
626 Slot = MST.getLocalSlot(&BB);
627 } else {
628 ModuleSlotTracker CustomMST(F->getParent(),
629 /*ShouldInitializeAllMetadata=*/false);
630 CustomMST.incorporateFunction(*F);
631 Slot = CustomMST.getLocalSlot(&BB);
632 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000633 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000634}
635
Alex Lorenz4af7e612015-08-03 23:08:19 +0000636void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000637 if (isa<GlobalValue>(V)) {
638 V.printAsOperand(OS, /*PrintType=*/false, MST);
639 return;
640 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000641 if (isa<Constant>(V)) {
642 // Machine memory operands can load/store to/from constant value pointers.
643 OS << '`';
644 V.printAsOperand(OS, /*PrintType=*/true, MST);
645 OS << '`';
646 return;
647 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000648 OS << "%ir.";
649 if (V.hasName()) {
650 printLLVMNameWithoutPrefix(OS, V.getName());
651 return;
652 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000653 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000654}
655
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000656void MIPrinter::printStackObjectReference(int FrameIndex) {
657 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
658 assert(ObjectInfo != StackObjectOperandMapping.end() &&
659 "Invalid frame index");
660 const FrameIndexOperand &Operand = ObjectInfo->second;
661 if (Operand.IsFixed) {
662 OS << "%fixed-stack." << Operand.ID;
663 return;
664 }
665 OS << "%stack." << Operand.ID;
666 if (!Operand.Name.empty())
667 OS << '.' << Operand.Name;
668}
669
Alex Lorenz5672a892015-08-05 22:26:15 +0000670void MIPrinter::printOffset(int64_t Offset) {
671 if (Offset == 0)
672 return;
673 if (Offset < 0) {
674 OS << " - " << -Offset;
675 return;
676 }
677 OS << " + " << Offset;
678}
679
Alex Lorenz49873a82015-08-06 00:44:07 +0000680static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
681 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
682 for (const auto &I : Flags) {
683 if (I.first == TF) {
684 return I.second;
685 }
686 }
687 return nullptr;
688}
689
690void MIPrinter::printTargetFlags(const MachineOperand &Op) {
691 if (!Op.getTargetFlags())
692 return;
693 const auto *TII =
694 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
695 assert(TII && "expected instruction info");
696 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
697 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000698 const bool HasDirectFlags = Flags.first;
699 const bool HasBitmaskFlags = Flags.second;
700 if (!HasDirectFlags && !HasBitmaskFlags) {
701 OS << "<unknown>) ";
702 return;
703 }
704 if (HasDirectFlags) {
705 if (const auto *Name = getTargetFlagName(TII, Flags.first))
706 OS << Name;
707 else
708 OS << "<unknown target flag>";
709 }
710 if (!HasBitmaskFlags) {
711 OS << ") ";
712 return;
713 }
714 bool IsCommaNeeded = HasDirectFlags;
715 unsigned BitMask = Flags.second;
716 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
717 for (const auto &Mask : BitMasks) {
718 // Check if the flag's bitmask has the bits of the current mask set.
719 if ((BitMask & Mask.first) == Mask.first) {
720 if (IsCommaNeeded)
721 OS << ", ";
722 IsCommaNeeded = true;
723 OS << Mask.second;
724 // Clear the bits which were serialized from the flag's bitmask.
725 BitMask &= ~(Mask.first);
726 }
727 }
728 if (BitMask) {
729 // When the resulting flag's bitmask isn't zero, we know that we didn't
730 // serialize all of the bit flags.
731 if (IsCommaNeeded)
732 OS << ", ";
733 OS << "<unknown bitmask target flag>";
734 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000735 OS << ") ";
736}
737
Alex Lorenzef5c1962015-07-28 23:02:45 +0000738static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
739 const auto *TII = MF.getSubtarget().getInstrInfo();
740 assert(TII && "expected instruction info");
741 auto Indices = TII->getSerializableTargetIndices();
742 for (const auto &I : Indices) {
743 if (I.first == Index) {
744 return I.second;
745 }
746 }
747 return nullptr;
748}
749
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000750void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000751 unsigned I, bool ShouldPrintRegisterTies,
752 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000753 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000754 switch (Op.getType()) {
755 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000756 if (Op.isImplicit())
757 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000758 else if (!IsDef && Op.isDef())
759 // Print the 'def' flag only when the operand is defined after '='.
760 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000761 if (Op.isInternalRead())
762 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000763 if (Op.isDead())
764 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000765 if (Op.isKill())
766 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000767 if (Op.isUndef())
768 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000769 if (Op.isEarlyClobber())
770 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000771 if (Op.isDebug())
772 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000773 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000774 // Print the sub register.
775 if (Op.getSubReg() != 0)
776 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000777 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
778 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000779 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
780 if (IsDef && MRI->getSize(Op.getReg()))
781 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000782 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000783 case MachineOperand::MO_Immediate:
784 OS << Op.getImm();
785 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000786 case MachineOperand::MO_CImmediate:
787 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
788 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000789 case MachineOperand::MO_FPImmediate:
790 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
791 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000792 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000793 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000794 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000795 case MachineOperand::MO_FrameIndex:
796 printStackObjectReference(Op.getIndex());
797 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000798 case MachineOperand::MO_ConstantPoolIndex:
799 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000800 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000801 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000802 case MachineOperand::MO_TargetIndex: {
803 OS << "target-index(";
804 if (const auto *Name = getTargetIndexName(
805 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
806 OS << Name;
807 else
808 OS << "<unknown>";
809 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000810 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000811 break;
812 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000813 case MachineOperand::MO_JumpTableIndex:
814 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000815 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000816 case MachineOperand::MO_ExternalSymbol:
817 OS << '$';
818 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000819 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000820 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000821 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000822 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000823 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000824 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000825 case MachineOperand::MO_BlockAddress:
826 OS << "blockaddress(";
827 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
828 MST);
829 OS << ", ";
830 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
831 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000832 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000833 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000834 case MachineOperand::MO_RegisterMask: {
835 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
836 if (RegMaskInfo != RegisterMaskIds.end())
837 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
838 else
839 llvm_unreachable("Can't print this machine register mask yet.");
840 break;
841 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000842 case MachineOperand::MO_RegisterLiveOut: {
843 const uint32_t *RegMask = Op.getRegLiveOut();
844 OS << "liveout(";
845 bool IsCommaNeeded = false;
846 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
847 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
848 if (IsCommaNeeded)
849 OS << ", ";
850 printReg(Reg, OS, TRI);
851 IsCommaNeeded = true;
852 }
853 }
854 OS << ")";
855 break;
856 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000857 case MachineOperand::MO_Metadata:
858 Op.getMetadata()->printAsOperand(OS, MST);
859 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000860 case MachineOperand::MO_MCSymbol:
861 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
862 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000863 case MachineOperand::MO_CFIIndex: {
864 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000865 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000866 break;
867 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000868 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000869}
870
Alex Lorenz4af7e612015-08-03 23:08:19 +0000871void MIPrinter::print(const MachineMemOperand &Op) {
872 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000873 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000874 if (Op.isVolatile())
875 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000876 if (Op.isNonTemporal())
877 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000878 if (Op.isInvariant())
879 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000880 if (Op.isLoad())
881 OS << "load ";
882 else {
883 assert(Op.isStore() && "Non load machine operand must be a store");
884 OS << "store ";
885 }
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000886 OS << Op.getSize();
Alex Lorenz91097a32015-08-12 20:33:26 +0000887 if (const Value *Val = Op.getValue()) {
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000888 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz4af7e612015-08-03 23:08:19 +0000889 printIRValueReference(*Val);
Matthias Braunc25c9cc2016-06-04 00:06:31 +0000890 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) {
891 OS << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000892 assert(PVal && "Expected a pseudo source value");
893 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000894 case PseudoSourceValue::Stack:
895 OS << "stack";
896 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000897 case PseudoSourceValue::GOT:
898 OS << "got";
899 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000900 case PseudoSourceValue::JumpTable:
901 OS << "jump-table";
902 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000903 case PseudoSourceValue::ConstantPool:
904 OS << "constant-pool";
905 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000906 case PseudoSourceValue::FixedStack:
907 printStackObjectReference(
908 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
909 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000910 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000911 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000912 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
913 OS, /*PrintType=*/false, MST);
914 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000915 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000916 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000917 printLLVMNameWithoutPrefix(
918 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000919 break;
920 }
921 }
Alex Lorenz83127732015-08-07 20:26:52 +0000922 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000923 if (Op.getBaseAlignment() != Op.getSize())
924 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000925 auto AAInfo = Op.getAAInfo();
926 if (AAInfo.TBAA) {
927 OS << ", !tbaa ";
928 AAInfo.TBAA->printAsOperand(OS, MST);
929 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000930 if (AAInfo.Scope) {
931 OS << ", !alias.scope ";
932 AAInfo.Scope->printAsOperand(OS, MST);
933 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000934 if (AAInfo.NoAlias) {
935 OS << ", !noalias ";
936 AAInfo.NoAlias->printAsOperand(OS, MST);
937 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000938 if (Op.getRanges()) {
939 OS << ", !range ";
940 Op.getRanges()->printAsOperand(OS, MST);
941 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000942 OS << ')';
943}
944
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000945static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
946 const TargetRegisterInfo *TRI) {
947 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
948 if (Reg == -1) {
949 OS << "<badreg>";
950 return;
951 }
952 printReg(Reg, OS, TRI);
953}
954
955void MIPrinter::print(const MCCFIInstruction &CFI,
956 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000957 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000958 case MCCFIInstruction::OpSameValue:
959 OS << ".cfi_same_value ";
960 if (CFI.getLabel())
961 OS << "<mcsymbol> ";
962 printCFIRegister(CFI.getRegister(), OS, TRI);
963 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000964 case MCCFIInstruction::OpOffset:
965 OS << ".cfi_offset ";
966 if (CFI.getLabel())
967 OS << "<mcsymbol> ";
968 printCFIRegister(CFI.getRegister(), OS, TRI);
969 OS << ", " << CFI.getOffset();
970 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000971 case MCCFIInstruction::OpDefCfaRegister:
972 OS << ".cfi_def_cfa_register ";
973 if (CFI.getLabel())
974 OS << "<mcsymbol> ";
975 printCFIRegister(CFI.getRegister(), OS, TRI);
976 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000977 case MCCFIInstruction::OpDefCfaOffset:
978 OS << ".cfi_def_cfa_offset ";
979 if (CFI.getLabel())
980 OS << "<mcsymbol> ";
981 OS << CFI.getOffset();
982 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000983 case MCCFIInstruction::OpDefCfa:
984 OS << ".cfi_def_cfa ";
985 if (CFI.getLabel())
986 OS << "<mcsymbol> ";
987 printCFIRegister(CFI.getRegister(), OS, TRI);
988 OS << ", " << CFI.getOffset();
989 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000990 default:
991 // TODO: Print the other CFI Operations.
992 OS << "<unserializable cfi operation>";
993 break;
994 }
995}
996
Alex Lorenz345c1442015-06-15 23:52:35 +0000997void llvm::printMIR(raw_ostream &OS, const Module &M) {
998 yaml::Output Out(OS);
999 Out << const_cast<Module &>(M);
1000}
1001
1002void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1003 MIRPrinter Printer(OS);
1004 Printer.print(MF);
1005}