blob: 6c3c63f919af1beefdc3540042eefbaf27fde400 [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the class that prints out the LLVM IR and machine
11// functions using the MIR serialization format.
12//
13//===----------------------------------------------------------------------===//
14
15#include "MIRPrinter.h"
16#include "llvm/ADT/STLExtras.h"
Alex Lorenzab980492015-07-20 20:51:18 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000018#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Alex Lorenzf4baeb52015-07-21 22:28:27 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000023#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000024#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000025#include "llvm/IR/Constants.h"
Alex Lorenz37643a02015-07-15 22:14:49 +000026#include "llvm/IR/Instructions.h"
Alex Lorenz6ede3742015-07-21 16:59:53 +000027#include "llvm/IR/IRPrintingPasses.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000028#include "llvm/IR/Module.h"
Alex Lorenz900b5cb2015-07-07 23:27:53 +000029#include "llvm/IR/ModuleSlotTracker.h"
Alex Lorenzf22ca8a2015-08-21 21:12:44 +000030#include "llvm/MC/MCSymbol.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000034#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000036
37using namespace llvm;
38
39namespace {
40
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000041/// This structure describes how to print out stack object references.
42struct FrameIndexOperand {
43 std::string Name;
44 unsigned ID;
45 bool IsFixed;
46
47 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
48 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
49
50 /// Return an ordinary stack object reference.
51 static FrameIndexOperand create(StringRef Name, unsigned ID) {
52 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
53 }
54
55 /// Return a fixed stack object reference.
56 static FrameIndexOperand createFixed(unsigned ID) {
57 return FrameIndexOperand("", ID, /*IsFixed=*/true);
58 }
59};
60
Alex Lorenz618b2832015-07-30 16:54:38 +000061} // end anonymous namespace
62
63namespace llvm {
64
Alex Lorenz345c1442015-06-15 23:52:35 +000065/// This class prints out the machine functions using the MIR serialization
66/// format.
67class MIRPrinter {
68 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000069 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000070 /// Maps from stack object indices to operand indices which will be used when
71 /// printing frame index machine operands.
72 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000073
74public:
75 MIRPrinter(raw_ostream &OS) : OS(OS) {}
76
77 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000078
Alex Lorenz28148ba2015-07-09 22:23:13 +000079 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
80 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000081 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
82 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000083 void convert(yaml::MachineFunction &MF,
84 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000085 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
86 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000087 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000088 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
89 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000090 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000091
92private:
93 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000094};
95
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000096/// This class prints out the machine instructions using the MIR serialization
97/// format.
98class MIPrinter {
99 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000100 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000101 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000102 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000103
104public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000105 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000106 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
107 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
108 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
109 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000110
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000111 void print(const MachineBasicBlock &MBB);
112
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000114 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000115 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000116 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000117 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000118 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000119 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000120 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000121 unsigned I, bool ShouldPrintRegisterTies,
122 const MachineRegisterInfo *MRI = nullptr, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000123 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000124
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000125 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000126};
127
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000128} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000129
130namespace llvm {
131namespace yaml {
132
133/// This struct serializes the LLVM IR module.
134template <> struct BlockScalarTraits<Module> {
135 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
136 Mod.print(OS, nullptr);
137 }
138 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
139 llvm_unreachable("LLVM Module is supposed to be parsed separately");
140 return "";
141 }
142};
143
144} // end namespace yaml
145} // end namespace llvm
146
Alex Lorenz15a00a82015-07-14 21:18:25 +0000147static void printReg(unsigned Reg, raw_ostream &OS,
148 const TargetRegisterInfo *TRI) {
149 // TODO: Print Stack Slots.
150 if (!Reg)
151 OS << '_';
152 else if (TargetRegisterInfo::isVirtualRegister(Reg))
153 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
154 else if (Reg < TRI->getNumRegs())
155 OS << '%' << StringRef(TRI->getName(Reg)).lower();
156 else
157 llvm_unreachable("Can't print this kind of register yet");
158}
159
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000160static void printReg(unsigned Reg, yaml::StringValue &Dest,
161 const TargetRegisterInfo *TRI) {
162 raw_string_ostream OS(Dest.Value);
163 printReg(Reg, OS, TRI);
164}
165
Alex Lorenz345c1442015-06-15 23:52:35 +0000166void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000167 initRegisterMaskIds(MF);
168
Alex Lorenz345c1442015-06-15 23:52:35 +0000169 yaml::MachineFunction YamlMF;
170 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000171 YamlMF.Alignment = MF.getAlignment();
172 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
173 YamlMF.HasInlineAsm = MF.hasInlineAsm();
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);
Cong Houd97c1002015-12-01 05:29:22 +0000465 if (MBB.hasSuccessorProbabilities())
466 OS << '(' << MBB.getSuccProbability(I) << ')';
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000467 }
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)
Matthias Braunc804cdb2015-09-25 21:51:24 +0000484 OS << ':' << PrintLaneMask(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) {
Quentin Colombet4e14a492016-03-07 21:57:52 +0000529 const auto *MF = MI.getParent()->getParent();
530 const auto &MRI = MF->getRegInfo();
531 const auto &SubTarget = MF->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000532 const auto *TRI = SubTarget.getRegisterInfo();
533 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000534 const auto *TII = SubTarget.getInstrInfo();
535 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000536 if (MI.isCFIInstruction())
537 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000538
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000539 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000540 unsigned I = 0, E = MI.getNumOperands();
541 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
542 !MI.getOperand(I).isImplicit();
543 ++I) {
544 if (I)
545 OS << ", ";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000546 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, &MRI,
547 /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000548 }
549
550 if (I)
551 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000552 if (MI.getFlag(MachineInstr::FrameSetup))
553 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000554 OS << TII->getName(MI.getOpcode());
Quentin Colombet12350a82016-03-08 00:29:15 +0000555 if (isPreISelGenericOpcode(MI.getOpcode())) {
556 assert(MI.getType() && "Generic instructions must have a type");
557 OS << ' ' << *MI.getType();
558 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000559 if (I < E)
560 OS << ' ';
561
562 bool NeedComma = false;
563 for (; I < E; ++I) {
564 if (NeedComma)
565 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000566 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000567 NeedComma = true;
568 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000569
570 if (MI.getDebugLoc()) {
571 if (NeedComma)
572 OS << ',';
573 OS << " debug-location ";
574 MI.getDebugLoc()->printAsOperand(OS, MST);
575 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000576
577 if (!MI.memoperands_empty()) {
578 OS << " :: ";
579 bool NeedComma = false;
580 for (const auto *Op : MI.memoperands()) {
581 if (NeedComma)
582 OS << ", ";
583 print(*Op);
584 NeedComma = true;
585 }
586 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000587}
588
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000589void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
590 OS << "%bb." << MBB.getNumber();
591 if (const auto *BB = MBB.getBasicBlock()) {
592 if (BB->hasName())
593 OS << '.' << BB->getName();
594 }
595}
596
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000597static void printIRSlotNumber(raw_ostream &OS, int Slot) {
598 if (Slot == -1)
599 OS << "<badref>";
600 else
601 OS << Slot;
602}
603
Alex Lorenzdeb53492015-07-28 17:28:03 +0000604void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
605 OS << "%ir-block.";
606 if (BB.hasName()) {
607 printLLVMNameWithoutPrefix(OS, BB.getName());
608 return;
609 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000610 const Function *F = BB.getParent();
611 int Slot;
612 if (F == MST.getCurrentFunction()) {
613 Slot = MST.getLocalSlot(&BB);
614 } else {
615 ModuleSlotTracker CustomMST(F->getParent(),
616 /*ShouldInitializeAllMetadata=*/false);
617 CustomMST.incorporateFunction(*F);
618 Slot = CustomMST.getLocalSlot(&BB);
619 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000620 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000621}
622
Alex Lorenz4af7e612015-08-03 23:08:19 +0000623void MIPrinter::printIRValueReference(const Value &V) {
Alex Lorenz36efd382015-08-20 00:20:03 +0000624 if (isa<GlobalValue>(V)) {
625 V.printAsOperand(OS, /*PrintType=*/false, MST);
626 return;
627 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000628 if (isa<Constant>(V)) {
629 // Machine memory operands can load/store to/from constant value pointers.
630 OS << '`';
631 V.printAsOperand(OS, /*PrintType=*/true, MST);
632 OS << '`';
633 return;
634 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000635 OS << "%ir.";
636 if (V.hasName()) {
637 printLLVMNameWithoutPrefix(OS, V.getName());
638 return;
639 }
Alex Lorenzdd13be02015-08-19 23:31:05 +0000640 printIRSlotNumber(OS, MST.getLocalSlot(&V));
Alex Lorenz4af7e612015-08-03 23:08:19 +0000641}
642
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000643void MIPrinter::printStackObjectReference(int FrameIndex) {
644 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
645 assert(ObjectInfo != StackObjectOperandMapping.end() &&
646 "Invalid frame index");
647 const FrameIndexOperand &Operand = ObjectInfo->second;
648 if (Operand.IsFixed) {
649 OS << "%fixed-stack." << Operand.ID;
650 return;
651 }
652 OS << "%stack." << Operand.ID;
653 if (!Operand.Name.empty())
654 OS << '.' << Operand.Name;
655}
656
Alex Lorenz5672a892015-08-05 22:26:15 +0000657void MIPrinter::printOffset(int64_t Offset) {
658 if (Offset == 0)
659 return;
660 if (Offset < 0) {
661 OS << " - " << -Offset;
662 return;
663 }
664 OS << " + " << Offset;
665}
666
Alex Lorenz49873a82015-08-06 00:44:07 +0000667static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
668 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
669 for (const auto &I : Flags) {
670 if (I.first == TF) {
671 return I.second;
672 }
673 }
674 return nullptr;
675}
676
677void MIPrinter::printTargetFlags(const MachineOperand &Op) {
678 if (!Op.getTargetFlags())
679 return;
680 const auto *TII =
681 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
682 assert(TII && "expected instruction info");
683 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
684 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000685 const bool HasDirectFlags = Flags.first;
686 const bool HasBitmaskFlags = Flags.second;
687 if (!HasDirectFlags && !HasBitmaskFlags) {
688 OS << "<unknown>) ";
689 return;
690 }
691 if (HasDirectFlags) {
692 if (const auto *Name = getTargetFlagName(TII, Flags.first))
693 OS << Name;
694 else
695 OS << "<unknown target flag>";
696 }
697 if (!HasBitmaskFlags) {
698 OS << ") ";
699 return;
700 }
701 bool IsCommaNeeded = HasDirectFlags;
702 unsigned BitMask = Flags.second;
703 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
704 for (const auto &Mask : BitMasks) {
705 // Check if the flag's bitmask has the bits of the current mask set.
706 if ((BitMask & Mask.first) == Mask.first) {
707 if (IsCommaNeeded)
708 OS << ", ";
709 IsCommaNeeded = true;
710 OS << Mask.second;
711 // Clear the bits which were serialized from the flag's bitmask.
712 BitMask &= ~(Mask.first);
713 }
714 }
715 if (BitMask) {
716 // When the resulting flag's bitmask isn't zero, we know that we didn't
717 // serialize all of the bit flags.
718 if (IsCommaNeeded)
719 OS << ", ";
720 OS << "<unknown bitmask target flag>";
721 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000722 OS << ") ";
723}
724
Alex Lorenzef5c1962015-07-28 23:02:45 +0000725static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
726 const auto *TII = MF.getSubtarget().getInstrInfo();
727 assert(TII && "expected instruction info");
728 auto Indices = TII->getSerializableTargetIndices();
729 for (const auto &I : Indices) {
730 if (I.first == Index) {
731 return I.second;
732 }
733 }
734 return nullptr;
735}
736
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000737void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Quentin Colombet4e14a492016-03-07 21:57:52 +0000738 unsigned I, bool ShouldPrintRegisterTies,
739 const MachineRegisterInfo *MRI, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000740 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000741 switch (Op.getType()) {
742 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000743 if (Op.isImplicit())
744 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000745 else if (!IsDef && Op.isDef())
746 // Print the 'def' flag only when the operand is defined after '='.
747 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000748 if (Op.isInternalRead())
749 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000750 if (Op.isDead())
751 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000752 if (Op.isKill())
753 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000754 if (Op.isUndef())
755 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000756 if (Op.isEarlyClobber())
757 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000758 if (Op.isDebug())
759 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000760 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000761 // Print the sub register.
762 if (Op.getSubReg() != 0)
763 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000764 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
765 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Quentin Colombet4e14a492016-03-07 21:57:52 +0000766 assert((!IsDef || MRI) && "for IsDef, MRI must be provided");
767 if (IsDef && MRI->getSize(Op.getReg()))
768 OS << '(' << MRI->getSize(Op.getReg()) << ')';
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000769 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000770 case MachineOperand::MO_Immediate:
771 OS << Op.getImm();
772 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000773 case MachineOperand::MO_CImmediate:
774 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
775 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000776 case MachineOperand::MO_FPImmediate:
777 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
778 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000779 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000780 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000781 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000782 case MachineOperand::MO_FrameIndex:
783 printStackObjectReference(Op.getIndex());
784 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000785 case MachineOperand::MO_ConstantPoolIndex:
786 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000787 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000788 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000789 case MachineOperand::MO_TargetIndex: {
790 OS << "target-index(";
791 if (const auto *Name = getTargetIndexName(
792 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
793 OS << Name;
794 else
795 OS << "<unknown>";
796 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000797 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000798 break;
799 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000800 case MachineOperand::MO_JumpTableIndex:
801 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000802 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000803 case MachineOperand::MO_ExternalSymbol:
804 OS << '$';
805 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000806 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000807 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000808 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000809 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000810 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000811 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000812 case MachineOperand::MO_BlockAddress:
813 OS << "blockaddress(";
814 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
815 MST);
816 OS << ", ";
817 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
818 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000819 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000820 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000821 case MachineOperand::MO_RegisterMask: {
822 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
823 if (RegMaskInfo != RegisterMaskIds.end())
824 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
825 else
826 llvm_unreachable("Can't print this machine register mask yet.");
827 break;
828 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000829 case MachineOperand::MO_RegisterLiveOut: {
830 const uint32_t *RegMask = Op.getRegLiveOut();
831 OS << "liveout(";
832 bool IsCommaNeeded = false;
833 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
834 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
835 if (IsCommaNeeded)
836 OS << ", ";
837 printReg(Reg, OS, TRI);
838 IsCommaNeeded = true;
839 }
840 }
841 OS << ")";
842 break;
843 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000844 case MachineOperand::MO_Metadata:
845 Op.getMetadata()->printAsOperand(OS, MST);
846 break;
Alex Lorenzf22ca8a2015-08-21 21:12:44 +0000847 case MachineOperand::MO_MCSymbol:
848 OS << "<mcsymbol " << *Op.getMCSymbol() << ">";
849 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000850 case MachineOperand::MO_CFIIndex: {
851 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000852 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000853 break;
854 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000855 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000856}
857
Alex Lorenz4af7e612015-08-03 23:08:19 +0000858void MIPrinter::print(const MachineMemOperand &Op) {
859 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000860 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000861 if (Op.isVolatile())
862 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000863 if (Op.isNonTemporal())
864 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000865 if (Op.isInvariant())
866 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000867 if (Op.isLoad())
868 OS << "load ";
869 else {
870 assert(Op.isStore() && "Non load machine operand must be a store");
871 OS << "store ";
872 }
873 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000874 if (const Value *Val = Op.getValue()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000875 printIRValueReference(*Val);
Alex Lorenz91097a32015-08-12 20:33:26 +0000876 } else {
877 const PseudoSourceValue *PVal = Op.getPseudoValue();
878 assert(PVal && "Expected a pseudo source value");
879 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000880 case PseudoSourceValue::Stack:
881 OS << "stack";
882 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000883 case PseudoSourceValue::GOT:
884 OS << "got";
885 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000886 case PseudoSourceValue::JumpTable:
887 OS << "jump-table";
888 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000889 case PseudoSourceValue::ConstantPool:
890 OS << "constant-pool";
891 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000892 case PseudoSourceValue::FixedStack:
893 printStackObjectReference(
894 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
895 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000896 case PseudoSourceValue::GlobalValueCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000897 OS << "call-entry ";
Alex Lorenz50b826f2015-08-14 21:08:30 +0000898 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
899 OS, /*PrintType=*/false, MST);
900 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000901 case PseudoSourceValue::ExternalSymbolCallEntry:
Alex Lorenz0d009642015-08-20 00:12:57 +0000902 OS << "call-entry $";
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000903 printLLVMNameWithoutPrefix(
904 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000905 break;
906 }
907 }
Alex Lorenz83127732015-08-07 20:26:52 +0000908 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000909 if (Op.getBaseAlignment() != Op.getSize())
910 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000911 auto AAInfo = Op.getAAInfo();
912 if (AAInfo.TBAA) {
913 OS << ", !tbaa ";
914 AAInfo.TBAA->printAsOperand(OS, MST);
915 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000916 if (AAInfo.Scope) {
917 OS << ", !alias.scope ";
918 AAInfo.Scope->printAsOperand(OS, MST);
919 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000920 if (AAInfo.NoAlias) {
921 OS << ", !noalias ";
922 AAInfo.NoAlias->printAsOperand(OS, MST);
923 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000924 if (Op.getRanges()) {
925 OS << ", !range ";
926 Op.getRanges()->printAsOperand(OS, MST);
927 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000928 OS << ')';
929}
930
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000931static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
932 const TargetRegisterInfo *TRI) {
933 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
934 if (Reg == -1) {
935 OS << "<badreg>";
936 return;
937 }
938 printReg(Reg, OS, TRI);
939}
940
941void MIPrinter::print(const MCCFIInstruction &CFI,
942 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000943 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000944 case MCCFIInstruction::OpSameValue:
945 OS << ".cfi_same_value ";
946 if (CFI.getLabel())
947 OS << "<mcsymbol> ";
948 printCFIRegister(CFI.getRegister(), OS, TRI);
949 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000950 case MCCFIInstruction::OpOffset:
951 OS << ".cfi_offset ";
952 if (CFI.getLabel())
953 OS << "<mcsymbol> ";
954 printCFIRegister(CFI.getRegister(), OS, TRI);
955 OS << ", " << CFI.getOffset();
956 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000957 case MCCFIInstruction::OpDefCfaRegister:
958 OS << ".cfi_def_cfa_register ";
959 if (CFI.getLabel())
960 OS << "<mcsymbol> ";
961 printCFIRegister(CFI.getRegister(), OS, TRI);
962 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000963 case MCCFIInstruction::OpDefCfaOffset:
964 OS << ".cfi_def_cfa_offset ";
965 if (CFI.getLabel())
966 OS << "<mcsymbol> ";
967 OS << CFI.getOffset();
968 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000969 case MCCFIInstruction::OpDefCfa:
970 OS << ".cfi_def_cfa ";
971 if (CFI.getLabel())
972 OS << "<mcsymbol> ";
973 printCFIRegister(CFI.getRegister(), OS, TRI);
974 OS << ", " << CFI.getOffset();
975 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000976 default:
977 // TODO: Print the other CFI Operations.
978 OS << "<unserializable cfi operation>";
979 break;
980 }
981}
982
Alex Lorenz345c1442015-06-15 23:52:35 +0000983void llvm::printMIR(raw_ostream &OS, const Module &M) {
984 yaml::Output Out(OS);
985 Out << const_cast<Module &>(M);
986}
987
988void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
989 MIRPrinter Printer(OS);
990 Printer.print(MF);
991}