blob: 9081e660d106c872d600e7f635525fdfb4153b05 [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"
Matthias Braund9da1622015-09-09 18:08:03 +000031#include "llvm/Support/Format.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000037
38using namespace llvm;
39
40namespace {
41
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000042/// This structure describes how to print out stack object references.
43struct FrameIndexOperand {
44 std::string Name;
45 unsigned ID;
46 bool IsFixed;
47
48 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
49 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
50
51 /// Return an ordinary stack object reference.
52 static FrameIndexOperand create(StringRef Name, unsigned ID) {
53 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
54 }
55
56 /// Return a fixed stack object reference.
57 static FrameIndexOperand createFixed(unsigned ID) {
58 return FrameIndexOperand("", ID, /*IsFixed=*/true);
59 }
60};
61
Alex Lorenz618b2832015-07-30 16:54:38 +000062} // end anonymous namespace
63
64namespace llvm {
65
Alex Lorenz345c1442015-06-15 23:52:35 +000066/// This class prints out the machine functions using the MIR serialization
67/// format.
68class MIRPrinter {
69 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000070 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000071 /// Maps from stack object indices to operand indices which will be used when
72 /// printing frame index machine operands.
73 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000074
75public:
76 MIRPrinter(raw_ostream &OS) : OS(OS) {}
77
78 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000079
Alex Lorenz28148ba2015-07-09 22:23:13 +000080 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
81 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000082 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
83 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000084 void convert(yaml::MachineFunction &MF,
85 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000086 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
87 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000088 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000089 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
90 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000091 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000092
93private:
94 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000095};
96
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000097/// This class prints out the machine instructions using the MIR serialization
98/// format.
99class MIPrinter {
100 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000101 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000102 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000103 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000104
105public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000106 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000107 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
108 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
109 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
110 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000111
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000112 void print(const MachineBasicBlock &MBB);
113
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000114 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000115 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000116 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000117 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000118 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000119 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000120 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000121 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000122 unsigned I, bool ShouldPrintRegisterTies, 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();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000174 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000175 ModuleSlotTracker MST(MF.getFunction()->getParent());
176 MST.incorporateFunction(*MF.getFunction());
177 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000178 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000179 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000180 if (const auto *ConstantPool = MF.getConstantPool())
181 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000182 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
183 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000184 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
185 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000186 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000187 if (IsNewlineNeeded)
188 StrOS << "\n";
189 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
190 .print(MBB);
191 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000192 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000193 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000194 yaml::Output Out(OS);
195 Out << YamlMF;
196}
197
Alex Lorenz54565cf2015-06-24 19:56:10 +0000198void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000199 const MachineRegisterInfo &RegInfo,
200 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000201 MF.IsSSA = RegInfo.isSSA();
202 MF.TracksRegLiveness = RegInfo.tracksLiveness();
203 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000204
205 // Print the virtual register definitions.
206 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
207 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
208 yaml::VirtualRegisterDefinition VReg;
209 VReg.ID = I;
210 VReg.Class =
211 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000212 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
213 if (PreferredReg)
214 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000215 MF.VirtualRegisters.push_back(VReg);
216 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000217
218 // Print the live ins.
219 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
220 yaml::MachineFunctionLiveIn LiveIn;
221 printReg(I->first, LiveIn.Register, TRI);
222 if (I->second)
223 printReg(I->second, LiveIn.VirtualRegister, TRI);
224 MF.LiveIns.push_back(LiveIn);
225 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000226 // The used physical register mask is printed as an inverted callee saved
227 // register mask.
228 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
229 if (UsedPhysRegMask.none())
230 return;
231 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
232 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
233 if (!UsedPhysRegMask[I]) {
234 yaml::FlowStringValue Reg;
235 printReg(I, Reg, TRI);
236 CalleeSavedRegisters.push_back(Reg);
237 }
238 }
239 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000240}
241
Alex Lorenza6f9a372015-07-29 21:09:09 +0000242void MIRPrinter::convert(ModuleSlotTracker &MST,
243 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000244 const MachineFrameInfo &MFI) {
245 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
246 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
247 YamlMFI.HasStackMap = MFI.hasStackMap();
248 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
249 YamlMFI.StackSize = MFI.getStackSize();
250 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
251 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
252 YamlMFI.AdjustsStack = MFI.adjustsStack();
253 YamlMFI.HasCalls = MFI.hasCalls();
254 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
255 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
256 YamlMFI.HasVAStart = MFI.hasVAStart();
257 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000258 if (MFI.getSavePoint()) {
259 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
260 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
261 .printMBBReference(*MFI.getSavePoint());
262 }
263 if (MFI.getRestorePoint()) {
264 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
265 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
266 .printMBBReference(*MFI.getRestorePoint());
267 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000268}
269
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000270void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000271 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000272 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000273 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000274 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000275 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000276 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000277 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
278 if (MFI.isDeadObjectIndex(I))
279 continue;
280
281 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000282 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000283 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
284 ? yaml::FixedMachineStackObject::SpillSlot
285 : yaml::FixedMachineStackObject::DefaultType;
286 YamlObject.Offset = MFI.getObjectOffset(I);
287 YamlObject.Size = MFI.getObjectSize(I);
288 YamlObject.Alignment = MFI.getObjectAlignment(I);
289 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
290 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
291 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000292 StackObjectOperandMapping.insert(
293 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000294 }
295
296 // Process ordinary stack objects.
297 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000298 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
299 if (MFI.isDeadObjectIndex(I))
300 continue;
301
302 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000303 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000304 if (const auto *Alloca = MFI.getObjectAllocation(I))
305 YamlObject.Name.Value =
306 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000307 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
308 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000309 : MFI.isVariableSizedObjectIndex(I)
310 ? yaml::MachineStackObject::VariableSized
311 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000312 YamlObject.Offset = MFI.getObjectOffset(I);
313 YamlObject.Size = MFI.getObjectSize(I);
314 YamlObject.Alignment = MFI.getObjectAlignment(I);
315
316 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000317 StackObjectOperandMapping.insert(std::make_pair(
318 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000319 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000320
321 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
322 yaml::StringValue Reg;
323 printReg(CSInfo.getReg(), Reg, TRI);
324 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
325 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
326 "Invalid stack object index");
327 const FrameIndexOperand &StackObject = StackObjectInfo->second;
328 if (StackObject.IsFixed)
329 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
330 else
331 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
332 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000333 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
334 auto LocalObject = MFI.getLocalFrameObjectMap(I);
335 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
336 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
337 "Invalid stack object index");
338 const FrameIndexOperand &StackObject = StackObjectInfo->second;
339 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
340 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
341 }
Alex Lorenza314d812015-08-18 22:26:26 +0000342
343 // Print the stack object references in the frame information class after
344 // converting the stack objects.
345 if (MFI.hasStackProtectorIndex()) {
346 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
347 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
348 .printStackObjectReference(MFI.getStackProtectorIndex());
349 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000350
351 // Print the debug variable information.
352 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
353 MMI.getVariableDbgInfo()) {
354 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
355 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
356 "Invalid stack object index");
357 const FrameIndexOperand &StackObject = StackObjectInfo->second;
358 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
359 auto &Object = MF.StackObjects[StackObject.ID];
360 {
361 raw_string_ostream StrOS(Object.DebugVar.Value);
362 DebugVar.Var->printAsOperand(StrOS, MST);
363 }
364 {
365 raw_string_ostream StrOS(Object.DebugExpr.Value);
366 DebugVar.Expr->printAsOperand(StrOS, MST);
367 }
368 {
369 raw_string_ostream StrOS(Object.DebugLoc.Value);
370 DebugVar.Loc->printAsOperand(StrOS, MST);
371 }
372 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000373}
374
Alex Lorenzab980492015-07-20 20:51:18 +0000375void MIRPrinter::convert(yaml::MachineFunction &MF,
376 const MachineConstantPool &ConstantPool) {
377 unsigned ID = 0;
378 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
379 // TODO: Serialize target specific constant pool entries.
380 if (Constant.isMachineConstantPoolEntry())
381 llvm_unreachable("Can't print target specific constant pool entries yet");
382
383 yaml::MachineConstantPoolValue YamlConstant;
384 std::string Str;
385 raw_string_ostream StrOS(Str);
386 Constant.Val.ConstVal->printAsOperand(StrOS);
387 YamlConstant.ID = ID++;
388 YamlConstant.Value = StrOS.str();
389 YamlConstant.Alignment = Constant.getAlignment();
390 MF.Constants.push_back(YamlConstant);
391 }
392}
393
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000394void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000395 yaml::MachineJumpTable &YamlJTI,
396 const MachineJumpTableInfo &JTI) {
397 YamlJTI.Kind = JTI.getEntryKind();
398 unsigned ID = 0;
399 for (const auto &Table : JTI.getJumpTables()) {
400 std::string Str;
401 yaml::MachineJumpTable::Entry Entry;
402 Entry.ID = ID++;
403 for (const auto *MBB : Table.MBBs) {
404 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000405 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
406 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000407 Entry.Blocks.push_back(StrOS.str());
408 Str.clear();
409 }
410 YamlJTI.Entries.push_back(Entry);
411 }
412}
413
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000414void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
415 const auto *TRI = MF.getSubtarget().getRegisterInfo();
416 unsigned I = 0;
417 for (const uint32_t *Mask : TRI->getRegMasks())
418 RegisterMaskIds.insert(std::make_pair(Mask, I++));
419}
420
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000421void MIPrinter::print(const MachineBasicBlock &MBB) {
422 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
423 OS << "bb." << MBB.getNumber();
424 bool HasAttributes = false;
425 if (const auto *BB = MBB.getBasicBlock()) {
426 if (BB->hasName()) {
427 OS << "." << BB->getName();
428 } else {
429 HasAttributes = true;
430 OS << " (";
431 int Slot = MST.getLocalSlot(BB);
432 if (Slot == -1)
433 OS << "<ir-block badref>";
434 else
435 OS << (Twine("%ir-block.") + Twine(Slot)).str();
436 }
437 }
438 if (MBB.hasAddressTaken()) {
439 OS << (HasAttributes ? ", " : " (");
440 OS << "address-taken";
441 HasAttributes = true;
442 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000443 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000444 OS << (HasAttributes ? ", " : " (");
445 OS << "landing-pad";
446 HasAttributes = true;
447 }
448 if (MBB.getAlignment()) {
449 OS << (HasAttributes ? ", " : " (");
450 OS << "align " << MBB.getAlignment();
451 HasAttributes = true;
452 }
453 if (HasAttributes)
454 OS << ")";
455 OS << ":\n";
456
457 bool HasLineAttributes = false;
458 // Print the successors
459 if (!MBB.succ_empty()) {
460 OS.indent(2) << "successors: ";
461 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
462 if (I != MBB.succ_begin())
463 OS << ", ";
464 printMBBReference(**I);
465 if (MBB.hasSuccessorWeights())
466 OS << '(' << MBB.getSuccWeight(I) << ')';
467 }
468 OS << "\n";
469 HasLineAttributes = true;
470 }
471
472 // Print the live in registers.
473 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
474 assert(TRI && "Expected target register info");
475 if (!MBB.livein_empty()) {
476 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000477 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000478 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000479 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000480 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000481 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000482 printReg(LI.PhysReg, OS, TRI);
483 if (LI.LaneMask != ~0u)
484 OS << format(":%08X", LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000485 }
486 OS << "\n";
487 HasLineAttributes = true;
488 }
489
490 if (HasLineAttributes)
491 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000492 bool IsInBundle = false;
493 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
494 const MachineInstr &MI = *I;
495 if (IsInBundle && !MI.isInsideBundle()) {
496 OS.indent(2) << "}\n";
497 IsInBundle = false;
498 }
499 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000500 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000501 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
502 OS << " {";
503 IsInBundle = true;
504 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000505 OS << "\n";
506 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000507 if (IsInBundle)
508 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000509}
510
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000511/// Return true when an instruction has tied register that can't be determined
512/// by the instruction's descriptor.
513static bool hasComplexRegisterTies(const MachineInstr &MI) {
514 const MCInstrDesc &MCID = MI.getDesc();
515 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
516 const auto &Operand = MI.getOperand(I);
517 if (!Operand.isReg() || Operand.isDef())
518 // Ignore the defined registers as MCID marks only the uses as tied.
519 continue;
520 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
521 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
522 if (ExpectedTiedIdx != TiedIdx)
523 return true;
524 }
525 return false;
526}
527
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000528void MIPrinter::print(const MachineInstr &MI) {
529 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000530 const auto *TRI = SubTarget.getRegisterInfo();
531 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000532 const auto *TII = SubTarget.getInstrInfo();
533 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000534 if (MI.isCFIInstruction())
535 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000536
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000537 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000538 unsigned I = 0, E = MI.getNumOperands();
539 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
540 !MI.getOperand(I).isImplicit();
541 ++I) {
542 if (I)
543 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000544 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000545 }
546
547 if (I)
548 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000549 if (MI.getFlag(MachineInstr::FrameSetup))
550 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000551 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000552 if (I < E)
553 OS << ' ';
554
555 bool NeedComma = false;
556 for (; I < E; ++I) {
557 if (NeedComma)
558 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000559 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000560 NeedComma = true;
561 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000562
563 if (MI.getDebugLoc()) {
564 if (NeedComma)
565 OS << ',';
566 OS << " debug-location ";
567 MI.getDebugLoc()->printAsOperand(OS, MST);
568 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000569
570 if (!MI.memoperands_empty()) {
571 OS << " :: ";
572 bool NeedComma = false;
573 for (const auto *Op : MI.memoperands()) {
574 if (NeedComma)
575 OS << ", ";
576 print(*Op);
577 NeedComma = true;
578 }
579 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000580}
581
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000582void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
583 OS << "%bb." << MBB.getNumber();
584 if (const auto *BB = MBB.getBasicBlock()) {
585 if (BB->hasName())
586 OS << '.' << BB->getName();
587 }
588}
589
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000590static void printIRSlotNumber(raw_ostream &OS, int Slot) {
591 if (Slot == -1)
592 OS << "<badref>";
593 else
594 OS << Slot;
595}
596
Alex Lorenzdeb53492015-07-28 17:28:03 +0000597void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
598 OS << "%ir-block.";
599 if (BB.hasName()) {
600 printLLVMNameWithoutPrefix(OS, BB.getName());
601 return;
602 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000603 const Function *F = BB.getParent();
604 int Slot;
605 if (F == MST.getCurrentFunction()) {
606 Slot = MST.getLocalSlot(&BB);
607 } else {
608 ModuleSlotTracker CustomMST(F->getParent(),
609 /*ShouldInitializeAllMetadata=*/false);
610 CustomMST.incorporateFunction(*F);
611 Slot = CustomMST.getLocalSlot(&BB);
612 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000613 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000614}
615
Alex Lorenz4af7e612015-08-03 23:08:19 +0000616void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000617 if (isa<GlobalValue>(V)) {
618 V.printAsOperand(OS, /*PrintType=*/false, MST);
619 return;
620 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000621 if (isa<Constant>(V)) {
622 // Machine memory operands can load/store to/from constant value pointers.
623 OS << '`';
624 V.printAsOperand(OS, /*PrintType=*/true, MST);
625 OS << '`';
626 return;
627 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000628 OS << "%ir.";
629 if (V.hasName()) {
630 printLLVMNameWithoutPrefix(OS, V.getName());
631 return;
632 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000633 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000634}
635
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000636void MIPrinter::printStackObjectReference(int FrameIndex) {
637 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
638 assert(ObjectInfo != StackObjectOperandMapping.end() &&
639 "Invalid frame index");
640 const FrameIndexOperand &Operand = ObjectInfo->second;
641 if (Operand.IsFixed) {
642 OS << "%fixed-stack." << Operand.ID;
643 return;
644 }
645 OS << "%stack." << Operand.ID;
646 if (!Operand.Name.empty())
647 OS << '.' << Operand.Name;
648}
649
Alex Lorenz5672a892015-08-05 22:26:15 +0000650void MIPrinter::printOffset(int64_t Offset) {
651 if (Offset == 0)
652 return;
653 if (Offset < 0) {
654 OS << " - " << -Offset;
655 return;
656 }
657 OS << " + " << Offset;
658}
659
Alex Lorenz49873a82015-08-06 00:44:07 +0000660static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
661 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
662 for (const auto &I : Flags) {
663 if (I.first == TF) {
664 return I.second;
665 }
666 }
667 return nullptr;
668}
669
670void MIPrinter::printTargetFlags(const MachineOperand &Op) {
671 if (!Op.getTargetFlags())
672 return;
673 const auto *TII =
674 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
675 assert(TII && "expected instruction info");
676 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
677 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000678 const bool HasDirectFlags = Flags.first;
679 const bool HasBitmaskFlags = Flags.second;
680 if (!HasDirectFlags && !HasBitmaskFlags) {
681 OS << "<unknown>) ";
682 return;
683 }
684 if (HasDirectFlags) {
685 if (const auto *Name = getTargetFlagName(TII, Flags.first))
686 OS << Name;
687 else
688 OS << "<unknown target flag>";
689 }
690 if (!HasBitmaskFlags) {
691 OS << ") ";
692 return;
693 }
694 bool IsCommaNeeded = HasDirectFlags;
695 unsigned BitMask = Flags.second;
696 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
697 for (const auto &Mask : BitMasks) {
698 // Check if the flag's bitmask has the bits of the current mask set.
699 if ((BitMask & Mask.first) == Mask.first) {
700 if (IsCommaNeeded)
701 OS << ", ";
702 IsCommaNeeded = true;
703 OS << Mask.second;
704 // Clear the bits which were serialized from the flag's bitmask.
705 BitMask &= ~(Mask.first);
706 }
707 }
708 if (BitMask) {
709 // When the resulting flag's bitmask isn't zero, we know that we didn't
710 // serialize all of the bit flags.
711 if (IsCommaNeeded)
712 OS << ", ";
713 OS << "<unknown bitmask target flag>";
714 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000715 OS << ") ";
716}
717
Alex Lorenzef5c1962015-07-28 23:02:45 +0000718static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
719 const auto *TII = MF.getSubtarget().getInstrInfo();
720 assert(TII && "expected instruction info");
721 auto Indices = TII->getSerializableTargetIndices();
722 for (const auto &I : Indices) {
723 if (I.first == Index) {
724 return I.second;
725 }
726 }
727 return nullptr;
728}
729
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000730void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000731 unsigned I, bool ShouldPrintRegisterTies, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000732 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000733 switch (Op.getType()) {
734 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000735 if (Op.isImplicit())
736 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000737 else if (!IsDef && Op.isDef())
738 // Print the 'def' flag only when the operand is defined after '='.
739 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000740 if (Op.isInternalRead())
741 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000742 if (Op.isDead())
743 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000744 if (Op.isKill())
745 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000746 if (Op.isUndef())
747 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000748 if (Op.isEarlyClobber())
749 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000750 if (Op.isDebug())
751 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000752 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000753 // Print the sub register.
754 if (Op.getSubReg() != 0)
755 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000756 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
757 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000758 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000759 case MachineOperand::MO_Immediate:
760 OS << Op.getImm();
761 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000762 case MachineOperand::MO_CImmediate:
763 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
764 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000765 case MachineOperand::MO_FPImmediate:
766 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
767 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000768 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000769 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000770 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000771 case MachineOperand::MO_FrameIndex:
772 printStackObjectReference(Op.getIndex());
773 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000774 case MachineOperand::MO_ConstantPoolIndex:
775 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000776 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000777 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000778 case MachineOperand::MO_TargetIndex: {
779 OS << "target-index(";
780 if (const auto *Name = getTargetIndexName(
781 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
782 OS << Name;
783 else
784 OS << "<unknown>";
785 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000786 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000787 break;
788 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000789 case MachineOperand::MO_JumpTableIndex:
790 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000791 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000792 case MachineOperand::MO_ExternalSymbol:
793 OS << '$';
794 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000795 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000796 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000797 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000798 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000799 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000800 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000801 case MachineOperand::MO_BlockAddress:
802 OS << "blockaddress(";
803 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
804 MST);
805 OS << ", ";
806 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
807 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000808 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000809 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000810 case MachineOperand::MO_RegisterMask: {
811 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
812 if (RegMaskInfo != RegisterMaskIds.end())
813 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
814 else
815 llvm_unreachable("Can't print this machine register mask yet.");
816 break;
817 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000818 case MachineOperand::MO_RegisterLiveOut: {
819 const uint32_t *RegMask = Op.getRegLiveOut();
820 OS << "liveout(";
821 bool IsCommaNeeded = false;
822 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
823 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
824 if (IsCommaNeeded)
825 OS << ", ";
826 printReg(Reg, OS, TRI);
827 IsCommaNeeded = true;
828 }
829 }
830 OS << ")";
831 break;
832 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000833 case MachineOperand::MO_Metadata:
834 Op.getMetadata()->printAsOperand(OS, MST);
835 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000836 case MachineOperand::MO_MCSymbol:
837 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
838 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000839 case MachineOperand::MO_CFIIndex: {
840 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000841 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000842 break;
843 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000844 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000845}
846
Alex Lorenz4af7e612015-08-03 23:08:19 +0000847void MIPrinter::print(const MachineMemOperand &Op) {
848 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000849 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000850 if (Op.isVolatile())
851 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000852 if (Op.isNonTemporal())
853 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000854 if (Op.isInvariant())
855 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000856 if (Op.isLoad())
857 OS << "load ";
858 else {
859 assert(Op.isStore() && "Non load machine operand must be a store");
860 OS << "store ";
861 }
862 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000863 if (const Value *Val = Op.getValue()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000864 printIRValueReference(*Val);
Alex Lorenz91097a32015-08-12 20:33:26 +0000865 } else {
866 const PseudoSourceValue *PVal = Op.getPseudoValue();
867 assert(PVal && "Expected a pseudo source value");
868 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000869 case PseudoSourceValue::Stack:
870 OS << "stack";
871 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000872 case PseudoSourceValue::GOT:
873 OS << "got";
874 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000875 case PseudoSourceValue::JumpTable:
876 OS << "jump-table";
877 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000878 case PseudoSourceValue::ConstantPool:
879 OS << "constant-pool";
880 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000881 case PseudoSourceValue::FixedStack:
882 printStackObjectReference(
883 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
884 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000885 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000886 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000887 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
888 OS, /*PrintType=*/false, MST);
889 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000890 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000891 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000892 printLLVMNameWithoutPrefix(
893 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000894 break;
895 }
896 }
Alex Lorenz83127732015-08-07 20:26:52 +0000897 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000898 if (Op.getBaseAlignment() != Op.getSize())
899 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000900 auto AAInfo = Op.getAAInfo();
901 if (AAInfo.TBAA) {
902 OS << ", !tbaa ";
903 AAInfo.TBAA->printAsOperand(OS, MST);
904 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000905 if (AAInfo.Scope) {
906 OS << ", !alias.scope ";
907 AAInfo.Scope->printAsOperand(OS, MST);
908 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000909 if (AAInfo.NoAlias) {
910 OS << ", !noalias ";
911 AAInfo.NoAlias->printAsOperand(OS, MST);
912 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000913 if (Op.getRanges()) {
914 OS << ", !range ";
915 Op.getRanges()->printAsOperand(OS, MST);
916 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000917 OS << ')';
918}
919
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000920static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
921 const TargetRegisterInfo *TRI) {
922 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
923 if (Reg == -1) {
924 OS << "<badreg>";
925 return;
926 }
927 printReg(Reg, OS, TRI);
928}
929
930void MIPrinter::print(const MCCFIInstruction &CFI,
931 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000932 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000933 case MCCFIInstruction::OpSameValue:
934 OS << ".cfi_same_value ";
935 if (CFI.getLabel())
936 OS << "<mcsymbol> ";
937 printCFIRegister(CFI.getRegister(), OS, TRI);
938 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000939 case MCCFIInstruction::OpOffset:
940 OS << ".cfi_offset ";
941 if (CFI.getLabel())
942 OS << "<mcsymbol> ";
943 printCFIRegister(CFI.getRegister(), OS, TRI);
944 OS << ", " << CFI.getOffset();
945 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000946 case MCCFIInstruction::OpDefCfaRegister:
947 OS << ".cfi_def_cfa_register ";
948 if (CFI.getLabel())
949 OS << "<mcsymbol> ";
950 printCFIRegister(CFI.getRegister(), OS, TRI);
951 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000952 case MCCFIInstruction::OpDefCfaOffset:
953 OS << ".cfi_def_cfa_offset ";
954 if (CFI.getLabel())
955 OS << "<mcsymbol> ";
956 OS << CFI.getOffset();
957 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000958 case MCCFIInstruction::OpDefCfa:
959 OS << ".cfi_def_cfa ";
960 if (CFI.getLabel())
961 OS << "<mcsymbol> ";
962 printCFIRegister(CFI.getRegister(), OS, TRI);
963 OS << ", " << CFI.getOffset();
964 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000965 default:
966 // TODO: Print the other CFI Operations.
967 OS << "<unserializable cfi operation>";
968 break;
969 }
970}
971
Alex Lorenz345c1442015-06-15 23:52:35 +0000972void llvm::printMIR(raw_ostream &OS, const Module &M) {
973 yaml::Output Out(OS);
974 Out << const_cast<Module &>(M);
975}
976
977void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
978 MIRPrinter Printer(OS);
979 Printer.print(MF);
980}