blob: 0be7807064fb411de0303fa85bcaea5bc743dfce [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,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000121 unsigned I, bool ShouldPrintRegisterTies, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000122 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000123
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000124 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000125};
126
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000127} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000128
129namespace llvm {
130namespace yaml {
131
132/// This struct serializes the LLVM IR module.
133template <> struct BlockScalarTraits<Module> {
134 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
135 Mod.print(OS, nullptr);
136 }
137 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
138 llvm_unreachable("LLVM Module is supposed to be parsed separately");
139 return "";
140 }
141};
142
143} // end namespace yaml
144} // end namespace llvm
145
Alex Lorenz15a00a82015-07-14 21:18:25 +0000146static void printReg(unsigned Reg, raw_ostream &OS,
147 const TargetRegisterInfo *TRI) {
148 // TODO: Print Stack Slots.
149 if (!Reg)
150 OS << '_';
151 else if (TargetRegisterInfo::isVirtualRegister(Reg))
152 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
153 else if (Reg < TRI->getNumRegs())
154 OS << '%' << StringRef(TRI->getName(Reg)).lower();
155 else
156 llvm_unreachable("Can't print this kind of register yet");
157}
158
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000159static void printReg(unsigned Reg, yaml::StringValue &Dest,
160 const TargetRegisterInfo *TRI) {
161 raw_string_ostream OS(Dest.Value);
162 printReg(Reg, OS, TRI);
163}
164
Alex Lorenz345c1442015-06-15 23:52:35 +0000165void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000166 initRegisterMaskIds(MF);
167
Alex Lorenz345c1442015-06-15 23:52:35 +0000168 yaml::MachineFunction YamlMF;
169 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000170 YamlMF.Alignment = MF.getAlignment();
171 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
172 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000173 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000174 ModuleSlotTracker MST(MF.getFunction()->getParent());
175 MST.incorporateFunction(*MF.getFunction());
176 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000177 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000178 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000179 if (const auto *ConstantPool = MF.getConstantPool())
180 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000181 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
182 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000183 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
184 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000185 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000186 if (IsNewlineNeeded)
187 StrOS << "\n";
188 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
189 .print(MBB);
190 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000191 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000192 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000193 yaml::Output Out(OS);
194 Out << YamlMF;
195}
196
Alex Lorenz54565cf2015-06-24 19:56:10 +0000197void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000198 const MachineRegisterInfo &RegInfo,
199 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000200 MF.IsSSA = RegInfo.isSSA();
201 MF.TracksRegLiveness = RegInfo.tracksLiveness();
202 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000203
204 // Print the virtual register definitions.
205 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
206 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
207 yaml::VirtualRegisterDefinition VReg;
208 VReg.ID = I;
209 VReg.Class =
210 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000211 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
212 if (PreferredReg)
213 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000214 MF.VirtualRegisters.push_back(VReg);
215 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000216
217 // Print the live ins.
218 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
219 yaml::MachineFunctionLiveIn LiveIn;
220 printReg(I->first, LiveIn.Register, TRI);
221 if (I->second)
222 printReg(I->second, LiveIn.VirtualRegister, TRI);
223 MF.LiveIns.push_back(LiveIn);
224 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000225 // The used physical register mask is printed as an inverted callee saved
226 // register mask.
227 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
228 if (UsedPhysRegMask.none())
229 return;
230 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
231 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
232 if (!UsedPhysRegMask[I]) {
233 yaml::FlowStringValue Reg;
234 printReg(I, Reg, TRI);
235 CalleeSavedRegisters.push_back(Reg);
236 }
237 }
238 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000239}
240
Alex Lorenza6f9a372015-07-29 21:09:09 +0000241void MIRPrinter::convert(ModuleSlotTracker &MST,
242 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000243 const MachineFrameInfo &MFI) {
244 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
245 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
246 YamlMFI.HasStackMap = MFI.hasStackMap();
247 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
248 YamlMFI.StackSize = MFI.getStackSize();
249 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
250 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
251 YamlMFI.AdjustsStack = MFI.adjustsStack();
252 YamlMFI.HasCalls = MFI.hasCalls();
253 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
254 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
255 YamlMFI.HasVAStart = MFI.hasVAStart();
256 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000257 if (MFI.getSavePoint()) {
258 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
259 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
260 .printMBBReference(*MFI.getSavePoint());
261 }
262 if (MFI.getRestorePoint()) {
263 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
264 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
265 .printMBBReference(*MFI.getRestorePoint());
266 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000267}
268
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000269void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000270 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000271 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000272 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000273 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000274 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000275 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000276 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
277 if (MFI.isDeadObjectIndex(I))
278 continue;
279
280 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000281 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000282 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
283 ? yaml::FixedMachineStackObject::SpillSlot
284 : yaml::FixedMachineStackObject::DefaultType;
285 YamlObject.Offset = MFI.getObjectOffset(I);
286 YamlObject.Size = MFI.getObjectSize(I);
287 YamlObject.Alignment = MFI.getObjectAlignment(I);
288 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
289 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
290 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000291 StackObjectOperandMapping.insert(
292 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000293 }
294
295 // Process ordinary stack objects.
296 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000297 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
298 if (MFI.isDeadObjectIndex(I))
299 continue;
300
301 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000302 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000303 if (const auto *Alloca = MFI.getObjectAllocation(I))
304 YamlObject.Name.Value =
305 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000306 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
307 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000308 : MFI.isVariableSizedObjectIndex(I)
309 ? yaml::MachineStackObject::VariableSized
310 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000311 YamlObject.Offset = MFI.getObjectOffset(I);
312 YamlObject.Size = MFI.getObjectSize(I);
313 YamlObject.Alignment = MFI.getObjectAlignment(I);
314
315 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000316 StackObjectOperandMapping.insert(std::make_pair(
317 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000318 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000319
320 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
321 yaml::StringValue Reg;
322 printReg(CSInfo.getReg(), Reg, TRI);
323 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
324 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
325 "Invalid stack object index");
326 const FrameIndexOperand &StackObject = StackObjectInfo->second;
327 if (StackObject.IsFixed)
328 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
329 else
330 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
331 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000332 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
333 auto LocalObject = MFI.getLocalFrameObjectMap(I);
334 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
335 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
336 "Invalid stack object index");
337 const FrameIndexOperand &StackObject = StackObjectInfo->second;
338 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
339 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
340 }
Alex Lorenza314d812015-08-18 22:26:26 +0000341
342 // Print the stack object references in the frame information class after
343 // converting the stack objects.
344 if (MFI.hasStackProtectorIndex()) {
345 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
346 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
347 .printStackObjectReference(MFI.getStackProtectorIndex());
348 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000349
350 // Print the debug variable information.
351 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
352 MMI.getVariableDbgInfo()) {
353 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
354 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
355 "Invalid stack object index");
356 const FrameIndexOperand &StackObject = StackObjectInfo->second;
357 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
358 auto &Object = MF.StackObjects[StackObject.ID];
359 {
360 raw_string_ostream StrOS(Object.DebugVar.Value);
361 DebugVar.Var->printAsOperand(StrOS, MST);
362 }
363 {
364 raw_string_ostream StrOS(Object.DebugExpr.Value);
365 DebugVar.Expr->printAsOperand(StrOS, MST);
366 }
367 {
368 raw_string_ostream StrOS(Object.DebugLoc.Value);
369 DebugVar.Loc->printAsOperand(StrOS, MST);
370 }
371 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000372}
373
Alex Lorenzab980492015-07-20 20:51:18 +0000374void MIRPrinter::convert(yaml::MachineFunction &MF,
375 const MachineConstantPool &ConstantPool) {
376 unsigned ID = 0;
377 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
378 // TODO: Serialize target specific constant pool entries.
379 if (Constant.isMachineConstantPoolEntry())
380 llvm_unreachable("Can't print target specific constant pool entries yet");
381
382 yaml::MachineConstantPoolValue YamlConstant;
383 std::string Str;
384 raw_string_ostream StrOS(Str);
385 Constant.Val.ConstVal->printAsOperand(StrOS);
386 YamlConstant.ID = ID++;
387 YamlConstant.Value = StrOS.str();
388 YamlConstant.Alignment = Constant.getAlignment();
389 MF.Constants.push_back(YamlConstant);
390 }
391}
392
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000393void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000394 yaml::MachineJumpTable &YamlJTI,
395 const MachineJumpTableInfo &JTI) {
396 YamlJTI.Kind = JTI.getEntryKind();
397 unsigned ID = 0;
398 for (const auto &Table : JTI.getJumpTables()) {
399 std::string Str;
400 yaml::MachineJumpTable::Entry Entry;
401 Entry.ID = ID++;
402 for (const auto *MBB : Table.MBBs) {
403 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000404 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
405 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000406 Entry.Blocks.push_back(StrOS.str());
407 Str.clear();
408 }
409 YamlJTI.Entries.push_back(Entry);
410 }
411}
412
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000413void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
414 const auto *TRI = MF.getSubtarget().getRegisterInfo();
415 unsigned I = 0;
416 for (const uint32_t *Mask : TRI->getRegMasks())
417 RegisterMaskIds.insert(std::make_pair(Mask, I++));
418}
419
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000420void MIPrinter::print(const MachineBasicBlock &MBB) {
421 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
422 OS << "bb." << MBB.getNumber();
423 bool HasAttributes = false;
424 if (const auto *BB = MBB.getBasicBlock()) {
425 if (BB->hasName()) {
426 OS << "." << BB->getName();
427 } else {
428 HasAttributes = true;
429 OS << " (";
430 int Slot = MST.getLocalSlot(BB);
431 if (Slot == -1)
432 OS << "<ir-block badref>";
433 else
434 OS << (Twine("%ir-block.") + Twine(Slot)).str();
435 }
436 }
437 if (MBB.hasAddressTaken()) {
438 OS << (HasAttributes ? ", " : " (");
439 OS << "address-taken";
440 HasAttributes = true;
441 }
Reid Kleckner0e288232015-08-27 23:27:47 +0000442 if (MBB.isEHPad()) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000443 OS << (HasAttributes ? ", " : " (");
444 OS << "landing-pad";
445 HasAttributes = true;
446 }
447 if (MBB.getAlignment()) {
448 OS << (HasAttributes ? ", " : " (");
449 OS << "align " << MBB.getAlignment();
450 HasAttributes = true;
451 }
452 if (HasAttributes)
453 OS << ")";
454 OS << ":\n";
455
456 bool HasLineAttributes = false;
457 // Print the successors
458 if (!MBB.succ_empty()) {
459 OS.indent(2) << "successors: ";
460 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
461 if (I != MBB.succ_begin())
462 OS << ", ";
463 printMBBReference(**I);
464 if (MBB.hasSuccessorWeights())
465 OS << '(' << MBB.getSuccWeight(I) << ')';
466 }
467 OS << "\n";
468 HasLineAttributes = true;
469 }
470
471 // Print the live in registers.
472 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
473 assert(TRI && "Expected target register info");
474 if (!MBB.livein_empty()) {
475 OS.indent(2) << "liveins: ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000476 bool First = true;
Matthias Braund9da1622015-09-09 18:08:03 +0000477 for (const auto &LI : MBB.liveins()) {
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000478 if (!First)
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000479 OS << ", ";
Matthias Braunb2b7ef12015-08-24 22:59:52 +0000480 First = false;
Matthias Braund9da1622015-09-09 18:08:03 +0000481 printReg(LI.PhysReg, OS, TRI);
482 if (LI.LaneMask != ~0u)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000483 OS << ':' << PrintLaneMask(LI.LaneMask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000484 }
485 OS << "\n";
486 HasLineAttributes = true;
487 }
488
489 if (HasLineAttributes)
490 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000491 bool IsInBundle = false;
492 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
493 const MachineInstr &MI = *I;
494 if (IsInBundle && !MI.isInsideBundle()) {
495 OS.indent(2) << "}\n";
496 IsInBundle = false;
497 }
498 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000499 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000500 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
501 OS << " {";
502 IsInBundle = true;
503 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000504 OS << "\n";
505 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000506 if (IsInBundle)
507 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000508}
509
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000510/// Return true when an instruction has tied register that can't be determined
511/// by the instruction's descriptor.
512static bool hasComplexRegisterTies(const MachineInstr &MI) {
513 const MCInstrDesc &MCID = MI.getDesc();
514 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
515 const auto &Operand = MI.getOperand(I);
516 if (!Operand.isReg() || Operand.isDef())
517 // Ignore the defined registers as MCID marks only the uses as tied.
518 continue;
519 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
520 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
521 if (ExpectedTiedIdx != TiedIdx)
522 return true;
523 }
524 return false;
525}
526
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000527void MIPrinter::print(const MachineInstr &MI) {
528 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000529 const auto *TRI = SubTarget.getRegisterInfo();
530 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000531 const auto *TII = SubTarget.getInstrInfo();
532 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000533 if (MI.isCFIInstruction())
534 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000535
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000536 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000537 unsigned I = 0, E = MI.getNumOperands();
538 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
539 !MI.getOperand(I).isImplicit();
540 ++I) {
541 if (I)
542 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000543 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000544 }
545
546 if (I)
547 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000548 if (MI.getFlag(MachineInstr::FrameSetup))
549 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000550 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000551 if (I < E)
552 OS << ' ';
553
554 bool NeedComma = false;
555 for (; I < E; ++I) {
556 if (NeedComma)
557 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000558 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000559 NeedComma = true;
560 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000561
562 if (MI.getDebugLoc()) {
563 if (NeedComma)
564 OS << ',';
565 OS << " debug-location ";
566 MI.getDebugLoc()->printAsOperand(OS, MST);
567 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000568
569 if (!MI.memoperands_empty()) {
570 OS << " :: ";
571 bool NeedComma = false;
572 for (const auto *Op : MI.memoperands()) {
573 if (NeedComma)
574 OS << ", ";
575 print(*Op);
576 NeedComma = true;
577 }
578 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000579}
580
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000581void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
582 OS << "%bb." << MBB.getNumber();
583 if (const auto *BB = MBB.getBasicBlock()) {
584 if (BB->hasName())
585 OS << '.' << BB->getName();
586 }
587}
588
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000589static void printIRSlotNumber(raw_ostream &OS, int Slot) {
590 if (Slot == -1)
591 OS << "<badref>";
592 else
593 OS << Slot;
594}
595
Alex Lorenzdeb53492015-07-28 17:28:03 +0000596void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
597 OS << "%ir-block.";
598 if (BB.hasName()) {
599 printLLVMNameWithoutPrefix(OS, BB.getName());
600 return;
601 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000602 const Function *F = BB.getParent();
603 int Slot;
604 if (F == MST.getCurrentFunction()) {
605 Slot = MST.getLocalSlot(&BB);
606 } else {
607 ModuleSlotTracker CustomMST(F->getParent(),
608 /*ShouldInitializeAllMetadata=*/false);
609 CustomMST.incorporateFunction(*F);
610 Slot = CustomMST.getLocalSlot(&BB);
611 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000612 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000613}
614
Alex Lorenz4af7e612015-08-03 23:08:19 +0000615void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000616 if (isa<GlobalValue>(V)) {
617 V.printAsOperand(OS, /*PrintType=*/false, MST);
618 return;
619 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000620 if (isa<Constant>(V)) {
621 // Machine memory operands can load/store to/from constant value pointers.
622 OS << '`';
623 V.printAsOperand(OS, /*PrintType=*/true, MST);
624 OS << '`';
625 return;
626 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000627 OS << "%ir.";
628 if (V.hasName()) {
629 printLLVMNameWithoutPrefix(OS, V.getName());
630 return;
631 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000632 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000633}
634
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000635void MIPrinter::printStackObjectReference(int FrameIndex) {
636 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
637 assert(ObjectInfo != StackObjectOperandMapping.end() &&
638 "Invalid frame index");
639 const FrameIndexOperand &Operand = ObjectInfo->second;
640 if (Operand.IsFixed) {
641 OS << "%fixed-stack." << Operand.ID;
642 return;
643 }
644 OS << "%stack." << Operand.ID;
645 if (!Operand.Name.empty())
646 OS << '.' << Operand.Name;
647}
648
Alex Lorenz5672a892015-08-05 22:26:15 +0000649void MIPrinter::printOffset(int64_t Offset) {
650 if (Offset == 0)
651 return;
652 if (Offset < 0) {
653 OS << " - " << -Offset;
654 return;
655 }
656 OS << " + " << Offset;
657}
658
Alex Lorenz49873a82015-08-06 00:44:07 +0000659static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
660 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
661 for (const auto &I : Flags) {
662 if (I.first == TF) {
663 return I.second;
664 }
665 }
666 return nullptr;
667}
668
669void MIPrinter::printTargetFlags(const MachineOperand &Op) {
670 if (!Op.getTargetFlags())
671 return;
672 const auto *TII =
673 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
674 assert(TII && "expected instruction info");
675 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
676 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000677 const bool HasDirectFlags = Flags.first;
678 const bool HasBitmaskFlags = Flags.second;
679 if (!HasDirectFlags && !HasBitmaskFlags) {
680 OS << "<unknown>) ";
681 return;
682 }
683 if (HasDirectFlags) {
684 if (const auto *Name = getTargetFlagName(TII, Flags.first))
685 OS << Name;
686 else
687 OS << "<unknown target flag>";
688 }
689 if (!HasBitmaskFlags) {
690 OS << ") ";
691 return;
692 }
693 bool IsCommaNeeded = HasDirectFlags;
694 unsigned BitMask = Flags.second;
695 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
696 for (const auto &Mask : BitMasks) {
697 // Check if the flag's bitmask has the bits of the current mask set.
698 if ((BitMask & Mask.first) == Mask.first) {
699 if (IsCommaNeeded)
700 OS << ", ";
701 IsCommaNeeded = true;
702 OS << Mask.second;
703 // Clear the bits which were serialized from the flag's bitmask.
704 BitMask &= ~(Mask.first);
705 }
706 }
707 if (BitMask) {
708 // When the resulting flag's bitmask isn't zero, we know that we didn't
709 // serialize all of the bit flags.
710 if (IsCommaNeeded)
711 OS << ", ";
712 OS << "<unknown bitmask target flag>";
713 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000714 OS << ") ";
715}
716
Alex Lorenzef5c1962015-07-28 23:02:45 +0000717static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
718 const auto *TII = MF.getSubtarget().getInstrInfo();
719 assert(TII && "expected instruction info");
720 auto Indices = TII->getSerializableTargetIndices();
721 for (const auto &I : Indices) {
722 if (I.first == Index) {
723 return I.second;
724 }
725 }
726 return nullptr;
727}
728
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000729void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000730 unsigned I, bool ShouldPrintRegisterTies, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000731 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000732 switch (Op.getType()) {
733 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000734 if (Op.isImplicit())
735 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000736 else if (!IsDef && Op.isDef())
737 // Print the 'def' flag only when the operand is defined after '='.
738 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000739 if (Op.isInternalRead())
740 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000741 if (Op.isDead())
742 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000743 if (Op.isKill())
744 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000745 if (Op.isUndef())
746 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000747 if (Op.isEarlyClobber())
748 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000749 if (Op.isDebug())
750 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000751 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000752 // Print the sub register.
753 if (Op.getSubReg() != 0)
754 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000755 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
756 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000757 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000758 case MachineOperand::MO_Immediate:
759 OS << Op.getImm();
760 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000761 case MachineOperand::MO_CImmediate:
762 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
763 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000764 case MachineOperand::MO_FPImmediate:
765 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
766 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000767 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000768 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000769 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000770 case MachineOperand::MO_FrameIndex:
771 printStackObjectReference(Op.getIndex());
772 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000773 case MachineOperand::MO_ConstantPoolIndex:
774 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000775 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000776 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000777 case MachineOperand::MO_TargetIndex: {
778 OS << "target-index(";
779 if (const auto *Name = getTargetIndexName(
780 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
781 OS << Name;
782 else
783 OS << "<unknown>";
784 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000785 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000786 break;
787 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000788 case MachineOperand::MO_JumpTableIndex:
789 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000790 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000791 case MachineOperand::MO_ExternalSymbol:
792 OS << '$';
793 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000794 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000795 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000796 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000797 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000798 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000799 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000800 case MachineOperand::MO_BlockAddress:
801 OS << "blockaddress(";
802 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
803 MST);
804 OS << ", ";
805 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
806 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000807 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000808 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000809 case MachineOperand::MO_RegisterMask: {
810 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
811 if (RegMaskInfo != RegisterMaskIds.end())
812 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
813 else
814 llvm_unreachable("Can't print this machine register mask yet.");
815 break;
816 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000817 case MachineOperand::MO_RegisterLiveOut: {
818 const uint32_t *RegMask = Op.getRegLiveOut();
819 OS << "liveout(";
820 bool IsCommaNeeded = false;
821 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
822 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
823 if (IsCommaNeeded)
824 OS << ", ";
825 printReg(Reg, OS, TRI);
826 IsCommaNeeded = true;
827 }
828 }
829 OS << ")";
830 break;
831 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000832 case MachineOperand::MO_Metadata:
833 Op.getMetadata()->printAsOperand(OS, MST);
834 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000835 case MachineOperand::MO_MCSymbol:
836 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
837 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000838 case MachineOperand::MO_CFIIndex: {
839 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000840 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000841 break;
842 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000843 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000844}
845
Alex Lorenz4af7e612015-08-03 23:08:19 +0000846void MIPrinter::print(const MachineMemOperand &Op) {
847 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000848 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000849 if (Op.isVolatile())
850 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000851 if (Op.isNonTemporal())
852 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000853 if (Op.isInvariant())
854 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000855 if (Op.isLoad())
856 OS << "load ";
857 else {
858 assert(Op.isStore() && "Non load machine operand must be a store");
859 OS << "store ";
860 }
861 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000862 if (const Value *Val = Op.getValue()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000863 printIRValueReference(*Val);
Alex Lorenz91097a32015-08-12 20:33:26 +0000864 } else {
865 const PseudoSourceValue *PVal = Op.getPseudoValue();
866 assert(PVal && "Expected a pseudo source value");
867 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000868 case PseudoSourceValue::Stack:
869 OS << "stack";
870 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000871 case PseudoSourceValue::GOT:
872 OS << "got";
873 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000874 case PseudoSourceValue::JumpTable:
875 OS << "jump-table";
876 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000877 case PseudoSourceValue::ConstantPool:
878 OS << "constant-pool";
879 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000880 case PseudoSourceValue::FixedStack:
881 printStackObjectReference(
882 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
883 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000884 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000885 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000886 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
887 OS, /*PrintType=*/false, MST);
888 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000889 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000890 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000891 printLLVMNameWithoutPrefix(
892 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000893 break;
894 }
895 }
Alex Lorenz83127732015-08-07 20:26:52 +0000896 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000897 if (Op.getBaseAlignment() != Op.getSize())
898 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000899 auto AAInfo = Op.getAAInfo();
900 if (AAInfo.TBAA) {
901 OS << ", !tbaa ";
902 AAInfo.TBAA->printAsOperand(OS, MST);
903 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000904 if (AAInfo.Scope) {
905 OS << ", !alias.scope ";
906 AAInfo.Scope->printAsOperand(OS, MST);
907 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000908 if (AAInfo.NoAlias) {
909 OS << ", !noalias ";
910 AAInfo.NoAlias->printAsOperand(OS, MST);
911 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000912 if (Op.getRanges()) {
913 OS << ", !range ";
914 Op.getRanges()->printAsOperand(OS, MST);
915 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000916 OS << ')';
917}
918
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000919static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
920 const TargetRegisterInfo *TRI) {
921 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
922 if (Reg == -1) {
923 OS << "<badreg>";
924 return;
925 }
926 printReg(Reg, OS, TRI);
927}
928
929void MIPrinter::print(const MCCFIInstruction &CFI,
930 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000931 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000932 case MCCFIInstruction::OpSameValue:
933 OS << ".cfi_same_value ";
934 if (CFI.getLabel())
935 OS << "<mcsymbol> ";
936 printCFIRegister(CFI.getRegister(), OS, TRI);
937 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000938 case MCCFIInstruction::OpOffset:
939 OS << ".cfi_offset ";
940 if (CFI.getLabel())
941 OS << "<mcsymbol> ";
942 printCFIRegister(CFI.getRegister(), OS, TRI);
943 OS << ", " << CFI.getOffset();
944 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000945 case MCCFIInstruction::OpDefCfaRegister:
946 OS << ".cfi_def_cfa_register ";
947 if (CFI.getLabel())
948 OS << "<mcsymbol> ";
949 printCFIRegister(CFI.getRegister(), OS, TRI);
950 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000951 case MCCFIInstruction::OpDefCfaOffset:
952 OS << ".cfi_def_cfa_offset ";
953 if (CFI.getLabel())
954 OS << "<mcsymbol> ";
955 OS << CFI.getOffset();
956 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000957 case MCCFIInstruction::OpDefCfa:
958 OS << ".cfi_def_cfa ";
959 if (CFI.getLabel())
960 OS << "<mcsymbol> ";
961 printCFIRegister(CFI.getRegister(), OS, TRI);
962 OS << ", " << CFI.getOffset();
963 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000964 default:
965 // TODO: Print the other CFI Operations.
966 OS << "<unserializable cfi operation>";
967 break;
968 }
969}
970
Alex Lorenz345c1442015-06-15 23:52:35 +0000971void llvm::printMIR(raw_ostream &OS, const Module &M) {
972 yaml::Output Out(OS);
973 Out << const_cast<Module &>(M);
974}
975
976void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
977 MIRPrinter Printer(OS);
978 Printer.print(MF);
979}