blob: df2f441a031c5dc18042d89d535610abbd716501 [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 Lorenz345c1442015-06-15 23:52:35 +000030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/YAMLTraits.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetSubtargetInfo.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000035
36using namespace llvm;
37
38namespace {
39
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000040/// This structure describes how to print out stack object references.
41struct FrameIndexOperand {
42 std::string Name;
43 unsigned ID;
44 bool IsFixed;
45
46 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed)
47 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {}
48
49 /// Return an ordinary stack object reference.
50 static FrameIndexOperand create(StringRef Name, unsigned ID) {
51 return FrameIndexOperand(Name, ID, /*IsFixed=*/false);
52 }
53
54 /// Return a fixed stack object reference.
55 static FrameIndexOperand createFixed(unsigned ID) {
56 return FrameIndexOperand("", ID, /*IsFixed=*/true);
57 }
58};
59
Alex Lorenz618b2832015-07-30 16:54:38 +000060} // end anonymous namespace
61
62namespace llvm {
63
Alex Lorenz345c1442015-06-15 23:52:35 +000064/// This class prints out the machine functions using the MIR serialization
65/// format.
66class MIRPrinter {
67 raw_ostream &OS;
Alex Lorenz8f6f4282015-06-29 16:57:06 +000068 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000069 /// Maps from stack object indices to operand indices which will be used when
70 /// printing frame index machine operands.
71 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping;
Alex Lorenz345c1442015-06-15 23:52:35 +000072
73public:
74 MIRPrinter(raw_ostream &OS) : OS(OS) {}
75
76 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000077
Alex Lorenz28148ba2015-07-09 22:23:13 +000078 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
79 const TargetRegisterInfo *TRI);
Alex Lorenza6f9a372015-07-29 21:09:09 +000080 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI,
81 const MachineFrameInfo &MFI);
Alex Lorenzab980492015-07-20 20:51:18 +000082 void convert(yaml::MachineFunction &MF,
83 const MachineConstantPool &ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +000084 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI,
85 const MachineJumpTableInfo &JTI);
Alex Lorenzf6bc8662015-07-10 18:13:57 +000086 void convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +000087 const MachineFrameInfo &MFI, MachineModuleInfo &MMI,
88 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +000089 const TargetRegisterInfo *TRI);
Alex Lorenz8f6f4282015-06-29 16:57:06 +000090
91private:
92 void initRegisterMaskIds(const MachineFunction &MF);
Alex Lorenz345c1442015-06-15 23:52:35 +000093};
94
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000095/// This class prints out the machine instructions using the MIR serialization
96/// format.
97class MIPrinter {
98 raw_ostream &OS;
Alex Lorenz900b5cb2015-07-07 23:27:53 +000099 ModuleSlotTracker &MST;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000100 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000101 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000102
103public:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000104 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000105 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds,
106 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping)
107 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds),
108 StackObjectOperandMapping(StackObjectOperandMapping) {}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000109
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000110 void print(const MachineBasicBlock &MBB);
111
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000112 void print(const MachineInstr &MI);
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000113 void printMBBReference(const MachineBasicBlock &MBB);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000114 void printIRBlockReference(const BasicBlock &BB);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000115 void printIRValueReference(const Value &V);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000116 void printStackObjectReference(int FrameIndex);
Alex Lorenz5672a892015-08-05 22:26:15 +0000117 void printOffset(int64_t Offset);
Alex Lorenz49873a82015-08-06 00:44:07 +0000118 void printTargetFlags(const MachineOperand &Op);
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000119 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000120 unsigned I, bool ShouldPrintRegisterTies, bool IsDef = false);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000121 void print(const MachineMemOperand &Op);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000122
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000123 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000124};
125
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000126} // end namespace llvm
Alex Lorenz345c1442015-06-15 23:52:35 +0000127
128namespace llvm {
129namespace yaml {
130
131/// This struct serializes the LLVM IR module.
132template <> struct BlockScalarTraits<Module> {
133 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
134 Mod.print(OS, nullptr);
135 }
136 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
137 llvm_unreachable("LLVM Module is supposed to be parsed separately");
138 return "";
139 }
140};
141
142} // end namespace yaml
143} // end namespace llvm
144
Alex Lorenz15a00a82015-07-14 21:18:25 +0000145static void printReg(unsigned Reg, raw_ostream &OS,
146 const TargetRegisterInfo *TRI) {
147 // TODO: Print Stack Slots.
148 if (!Reg)
149 OS << '_';
150 else if (TargetRegisterInfo::isVirtualRegister(Reg))
151 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
152 else if (Reg < TRI->getNumRegs())
153 OS << '%' << StringRef(TRI->getName(Reg)).lower();
154 else
155 llvm_unreachable("Can't print this kind of register yet");
156}
157
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000158static void printReg(unsigned Reg, yaml::StringValue &Dest,
159 const TargetRegisterInfo *TRI) {
160 raw_string_ostream OS(Dest.Value);
161 printReg(Reg, OS, TRI);
162}
163
Alex Lorenz345c1442015-06-15 23:52:35 +0000164void MIRPrinter::print(const MachineFunction &MF) {
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000165 initRegisterMaskIds(MF);
166
Alex Lorenz345c1442015-06-15 23:52:35 +0000167 yaml::MachineFunction YamlMF;
168 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000169 YamlMF.Alignment = MF.getAlignment();
170 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
171 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000172 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
Alex Lorenza6f9a372015-07-29 21:09:09 +0000173 ModuleSlotTracker MST(MF.getFunction()->getParent());
174 MST.incorporateFunction(*MF.getFunction());
175 convert(MST, YamlMF.FrameInfo, *MF.getFrameInfo());
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000176 convertStackObjects(YamlMF, *MF.getFrameInfo(), MF.getMMI(), MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000177 MF.getSubtarget().getRegisterInfo());
Alex Lorenzab980492015-07-20 20:51:18 +0000178 if (const auto *ConstantPool = MF.getConstantPool())
179 convert(YamlMF, *ConstantPool);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000180 if (const auto *JumpTableInfo = MF.getJumpTableInfo())
181 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000182 raw_string_ostream StrOS(YamlMF.Body.Value.Value);
183 bool IsNewlineNeeded = false;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000184 for (const auto &MBB : MF) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000185 if (IsNewlineNeeded)
186 StrOS << "\n";
187 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
188 .print(MBB);
189 IsNewlineNeeded = true;
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000190 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000191 StrOS.flush();
Alex Lorenz345c1442015-06-15 23:52:35 +0000192 yaml::Output Out(OS);
193 Out << YamlMF;
194}
195
Alex Lorenz54565cf2015-06-24 19:56:10 +0000196void MIRPrinter::convert(yaml::MachineFunction &MF,
Alex Lorenz28148ba2015-07-09 22:23:13 +0000197 const MachineRegisterInfo &RegInfo,
198 const TargetRegisterInfo *TRI) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000199 MF.IsSSA = RegInfo.isSSA();
200 MF.TracksRegLiveness = RegInfo.tracksLiveness();
201 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
Alex Lorenz28148ba2015-07-09 22:23:13 +0000202
203 // Print the virtual register definitions.
204 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
205 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
206 yaml::VirtualRegisterDefinition VReg;
207 VReg.ID = I;
208 VReg.Class =
209 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000210 unsigned PreferredReg = RegInfo.getSimpleHint(Reg);
211 if (PreferredReg)
212 printReg(PreferredReg, VReg.PreferredRegister, TRI);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000213 MF.VirtualRegisters.push_back(VReg);
214 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000215
216 // Print the live ins.
217 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) {
218 yaml::MachineFunctionLiveIn LiveIn;
219 printReg(I->first, LiveIn.Register, TRI);
220 if (I->second)
221 printReg(I->second, LiveIn.VirtualRegister, TRI);
222 MF.LiveIns.push_back(LiveIn);
223 }
Alex Lorenzc4838082015-08-11 00:32:49 +0000224 // The used physical register mask is printed as an inverted callee saved
225 // register mask.
226 const BitVector &UsedPhysRegMask = RegInfo.getUsedPhysRegsMask();
227 if (UsedPhysRegMask.none())
228 return;
229 std::vector<yaml::FlowStringValue> CalleeSavedRegisters;
230 for (unsigned I = 0, E = UsedPhysRegMask.size(); I != E; ++I) {
231 if (!UsedPhysRegMask[I]) {
232 yaml::FlowStringValue Reg;
233 printReg(I, Reg, TRI);
234 CalleeSavedRegisters.push_back(Reg);
235 }
236 }
237 MF.CalleeSavedRegisters = CalleeSavedRegisters;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000238}
239
Alex Lorenza6f9a372015-07-29 21:09:09 +0000240void MIRPrinter::convert(ModuleSlotTracker &MST,
241 yaml::MachineFrameInfo &YamlMFI,
Alex Lorenz60541c12015-07-09 19:55:27 +0000242 const MachineFrameInfo &MFI) {
243 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
244 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
245 YamlMFI.HasStackMap = MFI.hasStackMap();
246 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
247 YamlMFI.StackSize = MFI.getStackSize();
248 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
249 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
250 YamlMFI.AdjustsStack = MFI.adjustsStack();
251 YamlMFI.HasCalls = MFI.hasCalls();
252 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
253 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
254 YamlMFI.HasVAStart = MFI.hasVAStart();
255 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
Alex Lorenza6f9a372015-07-29 21:09:09 +0000256 if (MFI.getSavePoint()) {
257 raw_string_ostream StrOS(YamlMFI.SavePoint.Value);
258 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
259 .printMBBReference(*MFI.getSavePoint());
260 }
261 if (MFI.getRestorePoint()) {
262 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value);
263 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
264 .printMBBReference(*MFI.getRestorePoint());
265 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000266}
267
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000268void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000269 const MachineFrameInfo &MFI,
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000270 MachineModuleInfo &MMI,
Alex Lorenza314d812015-08-18 22:26:26 +0000271 ModuleSlotTracker &MST,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000272 const TargetRegisterInfo *TRI) {
Alex Lorenzde491f02015-07-13 18:07:26 +0000273 // Process fixed stack objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000274 unsigned ID = 0;
Alex Lorenzde491f02015-07-13 18:07:26 +0000275 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
276 if (MFI.isDeadObjectIndex(I))
277 continue;
278
279 yaml::FixedMachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000280 YamlObject.ID = ID;
Alex Lorenzde491f02015-07-13 18:07:26 +0000281 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
282 ? yaml::FixedMachineStackObject::SpillSlot
283 : yaml::FixedMachineStackObject::DefaultType;
284 YamlObject.Offset = MFI.getObjectOffset(I);
285 YamlObject.Size = MFI.getObjectSize(I);
286 YamlObject.Alignment = MFI.getObjectAlignment(I);
287 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
288 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
289 MF.FixedStackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000290 StackObjectOperandMapping.insert(
291 std::make_pair(I, FrameIndexOperand::createFixed(ID++)));
Alex Lorenzde491f02015-07-13 18:07:26 +0000292 }
293
294 // Process ordinary stack objects.
295 ID = 0;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000296 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
297 if (MFI.isDeadObjectIndex(I))
298 continue;
299
300 yaml::MachineStackObject YamlObject;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000301 YamlObject.ID = ID;
Alex Lorenz37643a02015-07-15 22:14:49 +0000302 if (const auto *Alloca = MFI.getObjectAllocation(I))
303 YamlObject.Name.Value =
304 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>";
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000305 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
306 ? yaml::MachineStackObject::SpillSlot
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000307 : MFI.isVariableSizedObjectIndex(I)
308 ? yaml::MachineStackObject::VariableSized
309 : yaml::MachineStackObject::DefaultType;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000310 YamlObject.Offset = MFI.getObjectOffset(I);
311 YamlObject.Size = MFI.getObjectSize(I);
312 YamlObject.Alignment = MFI.getObjectAlignment(I);
313
314 MF.StackObjects.push_back(YamlObject);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000315 StackObjectOperandMapping.insert(std::make_pair(
316 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++)));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000317 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000318
319 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) {
320 yaml::StringValue Reg;
321 printReg(CSInfo.getReg(), Reg, TRI);
322 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx());
323 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
324 "Invalid stack object index");
325 const FrameIndexOperand &StackObject = StackObjectInfo->second;
326 if (StackObject.IsFixed)
327 MF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg;
328 else
329 MF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg;
330 }
Alex Lorenza56ba6a2015-08-17 22:17:42 +0000331 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) {
332 auto LocalObject = MFI.getLocalFrameObjectMap(I);
333 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first);
334 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
335 "Invalid stack object index");
336 const FrameIndexOperand &StackObject = StackObjectInfo->second;
337 assert(!StackObject.IsFixed && "Expected a locally mapped stack object");
338 MF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second;
339 }
Alex Lorenza314d812015-08-18 22:26:26 +0000340
341 // Print the stack object references in the frame information class after
342 // converting the stack objects.
343 if (MFI.hasStackProtectorIndex()) {
344 raw_string_ostream StrOS(MF.FrameInfo.StackProtector.Value);
345 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
346 .printStackObjectReference(MFI.getStackProtectorIndex());
347 }
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000348
349 // Print the debug variable information.
350 for (MachineModuleInfo::VariableDbgInfo &DebugVar :
351 MMI.getVariableDbgInfo()) {
352 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot);
353 assert(StackObjectInfo != StackObjectOperandMapping.end() &&
354 "Invalid stack object index");
355 const FrameIndexOperand &StackObject = StackObjectInfo->second;
356 assert(!StackObject.IsFixed && "Expected a non-fixed stack object");
357 auto &Object = MF.StackObjects[StackObject.ID];
358 {
359 raw_string_ostream StrOS(Object.DebugVar.Value);
360 DebugVar.Var->printAsOperand(StrOS, MST);
361 }
362 {
363 raw_string_ostream StrOS(Object.DebugExpr.Value);
364 DebugVar.Expr->printAsOperand(StrOS, MST);
365 }
366 {
367 raw_string_ostream StrOS(Object.DebugLoc.Value);
368 DebugVar.Loc->printAsOperand(StrOS, MST);
369 }
370 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000371}
372
Alex Lorenzab980492015-07-20 20:51:18 +0000373void MIRPrinter::convert(yaml::MachineFunction &MF,
374 const MachineConstantPool &ConstantPool) {
375 unsigned ID = 0;
376 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) {
377 // TODO: Serialize target specific constant pool entries.
378 if (Constant.isMachineConstantPoolEntry())
379 llvm_unreachable("Can't print target specific constant pool entries yet");
380
381 yaml::MachineConstantPoolValue YamlConstant;
382 std::string Str;
383 raw_string_ostream StrOS(Str);
384 Constant.Val.ConstVal->printAsOperand(StrOS);
385 YamlConstant.ID = ID++;
386 YamlConstant.Value = StrOS.str();
387 YamlConstant.Alignment = Constant.getAlignment();
388 MF.Constants.push_back(YamlConstant);
389 }
390}
391
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000392void MIRPrinter::convert(ModuleSlotTracker &MST,
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000393 yaml::MachineJumpTable &YamlJTI,
394 const MachineJumpTableInfo &JTI) {
395 YamlJTI.Kind = JTI.getEntryKind();
396 unsigned ID = 0;
397 for (const auto &Table : JTI.getJumpTables()) {
398 std::string Str;
399 yaml::MachineJumpTable::Entry Entry;
400 Entry.ID = ID++;
401 for (const auto *MBB : Table.MBBs) {
402 raw_string_ostream StrOS(Str);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000403 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping)
404 .printMBBReference(*MBB);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000405 Entry.Blocks.push_back(StrOS.str());
406 Str.clear();
407 }
408 YamlJTI.Entries.push_back(Entry);
409 }
410}
411
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000412void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
413 const auto *TRI = MF.getSubtarget().getRegisterInfo();
414 unsigned I = 0;
415 for (const uint32_t *Mask : TRI->getRegMasks())
416 RegisterMaskIds.insert(std::make_pair(Mask, I++));
417}
418
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000419void MIPrinter::print(const MachineBasicBlock &MBB) {
420 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
421 OS << "bb." << MBB.getNumber();
422 bool HasAttributes = false;
423 if (const auto *BB = MBB.getBasicBlock()) {
424 if (BB->hasName()) {
425 OS << "." << BB->getName();
426 } else {
427 HasAttributes = true;
428 OS << " (";
429 int Slot = MST.getLocalSlot(BB);
430 if (Slot == -1)
431 OS << "<ir-block badref>";
432 else
433 OS << (Twine("%ir-block.") + Twine(Slot)).str();
434 }
435 }
436 if (MBB.hasAddressTaken()) {
437 OS << (HasAttributes ? ", " : " (");
438 OS << "address-taken";
439 HasAttributes = true;
440 }
441 if (MBB.isLandingPad()) {
442 OS << (HasAttributes ? ", " : " (");
443 OS << "landing-pad";
444 HasAttributes = true;
445 }
446 if (MBB.getAlignment()) {
447 OS << (HasAttributes ? ", " : " (");
448 OS << "align " << MBB.getAlignment();
449 HasAttributes = true;
450 }
451 if (HasAttributes)
452 OS << ")";
453 OS << ":\n";
454
455 bool HasLineAttributes = false;
456 // Print the successors
457 if (!MBB.succ_empty()) {
458 OS.indent(2) << "successors: ";
459 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) {
460 if (I != MBB.succ_begin())
461 OS << ", ";
462 printMBBReference(**I);
463 if (MBB.hasSuccessorWeights())
464 OS << '(' << MBB.getSuccWeight(I) << ')';
465 }
466 OS << "\n";
467 HasLineAttributes = true;
468 }
469
470 // Print the live in registers.
471 const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
472 assert(TRI && "Expected target register info");
473 if (!MBB.livein_empty()) {
474 OS.indent(2) << "liveins: ";
475 for (auto I = MBB.livein_begin(), E = MBB.livein_end(); I != E; ++I) {
476 if (I != MBB.livein_begin())
477 OS << ", ";
478 printReg(*I, OS, TRI);
479 }
480 OS << "\n";
481 HasLineAttributes = true;
482 }
483
484 if (HasLineAttributes)
485 OS << "\n";
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000486 bool IsInBundle = false;
487 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) {
488 const MachineInstr &MI = *I;
489 if (IsInBundle && !MI.isInsideBundle()) {
490 OS.indent(2) << "}\n";
491 IsInBundle = false;
492 }
493 OS.indent(IsInBundle ? 4 : 2);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000494 print(MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000495 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) {
496 OS << " {";
497 IsInBundle = true;
498 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000499 OS << "\n";
500 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000501 if (IsInBundle)
502 OS.indent(2) << "}\n";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000503}
504
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000505/// Return true when an instruction has tied register that can't be determined
506/// by the instruction's descriptor.
507static bool hasComplexRegisterTies(const MachineInstr &MI) {
508 const MCInstrDesc &MCID = MI.getDesc();
509 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
510 const auto &Operand = MI.getOperand(I);
511 if (!Operand.isReg() || Operand.isDef())
512 // Ignore the defined registers as MCID marks only the uses as tied.
513 continue;
514 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
515 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1;
516 if (ExpectedTiedIdx != TiedIdx)
517 return true;
518 }
519 return false;
520}
521
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000522void MIPrinter::print(const MachineInstr &MI) {
523 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000524 const auto *TRI = SubTarget.getRegisterInfo();
525 assert(TRI && "Expected target register info");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000526 const auto *TII = SubTarget.getInstrInfo();
527 assert(TII && "Expected target instruction info");
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000528 if (MI.isCFIInstruction())
529 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000530
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000531 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000532 unsigned I = 0, E = MI.getNumOperands();
533 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
534 !MI.getOperand(I).isImplicit();
535 ++I) {
536 if (I)
537 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000538 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, /*IsDef=*/true);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000539 }
540
541 if (I)
542 OS << " = ";
Alex Lorenze5a44662015-07-17 00:24:15 +0000543 if (MI.getFlag(MachineInstr::FrameSetup))
544 OS << "frame-setup ";
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000545 OS << TII->getName(MI.getOpcode());
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000546 if (I < E)
547 OS << ' ';
548
549 bool NeedComma = false;
550 for (; I < E; ++I) {
551 if (NeedComma)
552 OS << ", ";
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000553 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000554 NeedComma = true;
555 }
Alex Lorenz46d760d2015-07-22 21:15:11 +0000556
557 if (MI.getDebugLoc()) {
558 if (NeedComma)
559 OS << ',';
560 OS << " debug-location ";
561 MI.getDebugLoc()->printAsOperand(OS, MST);
562 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000563
564 if (!MI.memoperands_empty()) {
565 OS << " :: ";
566 bool NeedComma = false;
567 for (const auto *Op : MI.memoperands()) {
568 if (NeedComma)
569 OS << ", ";
570 print(*Op);
571 NeedComma = true;
572 }
573 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000574}
575
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000576void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
577 OS << "%bb." << MBB.getNumber();
578 if (const auto *BB = MBB.getBasicBlock()) {
579 if (BB->hasName())
580 OS << '.' << BB->getName();
581 }
582}
583
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000584static void printIRSlotNumber(raw_ostream &OS, int Slot) {
585 if (Slot == -1)
586 OS << "<badref>";
587 else
588 OS << Slot;
589}
590
Alex Lorenzdeb53492015-07-28 17:28:03 +0000591void MIPrinter::printIRBlockReference(const BasicBlock &BB) {
592 OS << "%ir-block.";
593 if (BB.hasName()) {
594 printLLVMNameWithoutPrefix(OS, BB.getName());
595 return;
596 }
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000597 const Function *F = BB.getParent();
598 int Slot;
599 if (F == MST.getCurrentFunction()) {
600 Slot = MST.getLocalSlot(&BB);
601 } else {
602 ModuleSlotTracker CustomMST(F->getParent(),
603 /*ShouldInitializeAllMetadata=*/false);
604 CustomMST.incorporateFunction(*F);
605 Slot = CustomMST.getLocalSlot(&BB);
606 }
Alex Lorenz55dc6f82015-08-19 23:24:37 +0000607 printIRSlotNumber(OS, Slot);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000608}
609
Alex Lorenz4af7e612015-08-03 23:08:19 +0000610void MIPrinter::printIRValueReference(const Value &V) {
611 OS << "%ir.";
612 if (V.hasName()) {
613 printLLVMNameWithoutPrefix(OS, V.getName());
614 return;
615 }
616 // TODO: Serialize the unnamed IR value references.
617 OS << "<unserializable ir value>";
618}
619
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000620void MIPrinter::printStackObjectReference(int FrameIndex) {
621 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex);
622 assert(ObjectInfo != StackObjectOperandMapping.end() &&
623 "Invalid frame index");
624 const FrameIndexOperand &Operand = ObjectInfo->second;
625 if (Operand.IsFixed) {
626 OS << "%fixed-stack." << Operand.ID;
627 return;
628 }
629 OS << "%stack." << Operand.ID;
630 if (!Operand.Name.empty())
631 OS << '.' << Operand.Name;
632}
633
Alex Lorenz5672a892015-08-05 22:26:15 +0000634void MIPrinter::printOffset(int64_t Offset) {
635 if (Offset == 0)
636 return;
637 if (Offset < 0) {
638 OS << " - " << -Offset;
639 return;
640 }
641 OS << " + " << Offset;
642}
643
Alex Lorenz49873a82015-08-06 00:44:07 +0000644static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) {
645 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
646 for (const auto &I : Flags) {
647 if (I.first == TF) {
648 return I.second;
649 }
650 }
651 return nullptr;
652}
653
654void MIPrinter::printTargetFlags(const MachineOperand &Op) {
655 if (!Op.getTargetFlags())
656 return;
657 const auto *TII =
658 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo();
659 assert(TII && "expected instruction info");
660 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags());
661 OS << "target-flags(";
Alex Lorenzf3630112015-08-18 22:52:15 +0000662 const bool HasDirectFlags = Flags.first;
663 const bool HasBitmaskFlags = Flags.second;
664 if (!HasDirectFlags && !HasBitmaskFlags) {
665 OS << "<unknown>) ";
666 return;
667 }
668 if (HasDirectFlags) {
669 if (const auto *Name = getTargetFlagName(TII, Flags.first))
670 OS << Name;
671 else
672 OS << "<unknown target flag>";
673 }
674 if (!HasBitmaskFlags) {
675 OS << ") ";
676 return;
677 }
678 bool IsCommaNeeded = HasDirectFlags;
679 unsigned BitMask = Flags.second;
680 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags();
681 for (const auto &Mask : BitMasks) {
682 // Check if the flag's bitmask has the bits of the current mask set.
683 if ((BitMask & Mask.first) == Mask.first) {
684 if (IsCommaNeeded)
685 OS << ", ";
686 IsCommaNeeded = true;
687 OS << Mask.second;
688 // Clear the bits which were serialized from the flag's bitmask.
689 BitMask &= ~(Mask.first);
690 }
691 }
692 if (BitMask) {
693 // When the resulting flag's bitmask isn't zero, we know that we didn't
694 // serialize all of the bit flags.
695 if (IsCommaNeeded)
696 OS << ", ";
697 OS << "<unknown bitmask target flag>";
698 }
Alex Lorenz49873a82015-08-06 00:44:07 +0000699 OS << ") ";
700}
701
Alex Lorenzef5c1962015-07-28 23:02:45 +0000702static const char *getTargetIndexName(const MachineFunction &MF, int Index) {
703 const auto *TII = MF.getSubtarget().getInstrInfo();
704 assert(TII && "expected instruction info");
705 auto Indices = TII->getSerializableTargetIndices();
706 for (const auto &I : Indices) {
707 if (I.first == Index) {
708 return I.second;
709 }
710 }
711 return nullptr;
712}
713
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000714void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000715 unsigned I, bool ShouldPrintRegisterTies, bool IsDef) {
Alex Lorenz49873a82015-08-06 00:44:07 +0000716 printTargetFlags(Op);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000717 switch (Op.getType()) {
718 case MachineOperand::MO_Register:
Alex Lorenzcb268d42015-07-06 23:07:26 +0000719 if (Op.isImplicit())
720 OS << (Op.isDef() ? "implicit-def " : "implicit ");
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000721 else if (!IsDef && Op.isDef())
722 // Print the 'def' flag only when the operand is defined after '='.
723 OS << "def ";
Alex Lorenz1039fd12015-08-14 19:07:07 +0000724 if (Op.isInternalRead())
725 OS << "internal ";
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +0000726 if (Op.isDead())
727 OS << "dead ";
Alex Lorenz495ad872015-07-08 21:23:34 +0000728 if (Op.isKill())
729 OS << "killed ";
Alex Lorenz4d026b892015-07-08 23:58:31 +0000730 if (Op.isUndef())
731 OS << "undef ";
Alex Lorenz01c1a5e2015-08-05 17:49:03 +0000732 if (Op.isEarlyClobber())
733 OS << "early-clobber ";
Alex Lorenz90752582015-08-05 17:41:17 +0000734 if (Op.isDebug())
735 OS << "debug-use ";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000736 printReg(Op.getReg(), OS, TRI);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000737 // Print the sub register.
738 if (Op.getSubReg() != 0)
739 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000740 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef())
741 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")";
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000742 break;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000743 case MachineOperand::MO_Immediate:
744 OS << Op.getImm();
745 break;
Alex Lorenz05e38822015-08-05 18:52:21 +0000746 case MachineOperand::MO_CImmediate:
747 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST);
748 break;
Alex Lorenzad156fb2015-07-31 20:49:21 +0000749 case MachineOperand::MO_FPImmediate:
750 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST);
751 break;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000752 case MachineOperand::MO_MachineBasicBlock:
Alex Lorenz5d26fa82015-06-30 18:00:16 +0000753 printMBBReference(*Op.getMBB());
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000754 break;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000755 case MachineOperand::MO_FrameIndex:
756 printStackObjectReference(Op.getIndex());
757 break;
Alex Lorenzab980492015-07-20 20:51:18 +0000758 case MachineOperand::MO_ConstantPoolIndex:
759 OS << "%const." << Op.getIndex();
Alex Lorenz5672a892015-08-05 22:26:15 +0000760 printOffset(Op.getOffset());
Alex Lorenzab980492015-07-20 20:51:18 +0000761 break;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000762 case MachineOperand::MO_TargetIndex: {
763 OS << "target-index(";
764 if (const auto *Name = getTargetIndexName(
765 *Op.getParent()->getParent()->getParent(), Op.getIndex()))
766 OS << Name;
767 else
768 OS << "<unknown>";
769 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000770 printOffset(Op.getOffset());
Alex Lorenzef5c1962015-07-28 23:02:45 +0000771 break;
772 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000773 case MachineOperand::MO_JumpTableIndex:
774 OS << "%jump-table." << Op.getIndex();
Alex Lorenz31d70682015-07-15 23:38:35 +0000775 break;
Alex Lorenz6ede3742015-07-21 16:59:53 +0000776 case MachineOperand::MO_ExternalSymbol:
777 OS << '$';
778 printLLVMNameWithoutPrefix(OS, Op.getSymbolName());
Alex Lorenz5672a892015-08-05 22:26:15 +0000779 printOffset(Op.getOffset());
Alex Lorenz6ede3742015-07-21 16:59:53 +0000780 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000781 case MachineOperand::MO_GlobalAddress:
Alex Lorenz900b5cb2015-07-07 23:27:53 +0000782 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Alex Lorenz5672a892015-08-05 22:26:15 +0000783 printOffset(Op.getOffset());
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000784 break;
Alex Lorenzdeb53492015-07-28 17:28:03 +0000785 case MachineOperand::MO_BlockAddress:
786 OS << "blockaddress(";
787 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false,
788 MST);
789 OS << ", ";
790 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock());
791 OS << ')';
Alex Lorenz5672a892015-08-05 22:26:15 +0000792 printOffset(Op.getOffset());
Alex Lorenzdeb53492015-07-28 17:28:03 +0000793 break;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000794 case MachineOperand::MO_RegisterMask: {
795 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
796 if (RegMaskInfo != RegisterMaskIds.end())
797 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
798 else
799 llvm_unreachable("Can't print this machine register mask yet.");
800 break;
801 }
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000802 case MachineOperand::MO_RegisterLiveOut: {
803 const uint32_t *RegMask = Op.getRegLiveOut();
804 OS << "liveout(";
805 bool IsCommaNeeded = false;
806 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) {
807 if (RegMask[Reg / 32] & (1U << (Reg % 32))) {
808 if (IsCommaNeeded)
809 OS << ", ";
810 printReg(Reg, OS, TRI);
811 IsCommaNeeded = true;
812 }
813 }
814 OS << ")";
815 break;
816 }
Alex Lorenz35e44462015-07-22 17:58:46 +0000817 case MachineOperand::MO_Metadata:
818 Op.getMetadata()->printAsOperand(OS, MST);
819 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000820 case MachineOperand::MO_CFIIndex: {
821 const auto &MMI = Op.getParent()->getParent()->getParent()->getMMI();
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000822 print(MMI.getFrameInstructions()[Op.getCFIIndex()], TRI);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000823 break;
824 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000825 default:
826 // TODO: Print the other machine operands.
827 llvm_unreachable("Can't print this machine operand at the moment");
828 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000829}
830
Alex Lorenz4af7e612015-08-03 23:08:19 +0000831void MIPrinter::print(const MachineMemOperand &Op) {
832 OS << '(';
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000833 // TODO: Print operand's target specific flags.
Alex Lorenza518b792015-08-04 00:24:45 +0000834 if (Op.isVolatile())
835 OS << "volatile ";
Alex Lorenz10fd0382015-08-06 16:49:30 +0000836 if (Op.isNonTemporal())
837 OS << "non-temporal ";
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000838 if (Op.isInvariant())
839 OS << "invariant ";
Alex Lorenz4af7e612015-08-03 23:08:19 +0000840 if (Op.isLoad())
841 OS << "load ";
842 else {
843 assert(Op.isStore() && "Non load machine operand must be a store");
844 OS << "store ";
845 }
846 OS << Op.getSize() << (Op.isLoad() ? " from " : " into ");
Alex Lorenz91097a32015-08-12 20:33:26 +0000847 if (const Value *Val = Op.getValue()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000848 printIRValueReference(*Val);
Alex Lorenz91097a32015-08-12 20:33:26 +0000849 } else {
850 const PseudoSourceValue *PVal = Op.getPseudoValue();
851 assert(PVal && "Expected a pseudo source value");
852 switch (PVal->kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +0000853 case PseudoSourceValue::Stack:
854 OS << "stack";
855 break;
Alex Lorenzd858f872015-08-12 21:00:22 +0000856 case PseudoSourceValue::GOT:
857 OS << "got";
858 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +0000859 case PseudoSourceValue::JumpTable:
860 OS << "jump-table";
861 break;
Alex Lorenz91097a32015-08-12 20:33:26 +0000862 case PseudoSourceValue::ConstantPool:
863 OS << "constant-pool";
864 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +0000865 case PseudoSourceValue::FixedStack:
866 printStackObjectReference(
867 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex());
868 break;
Alex Lorenz50b826f2015-08-14 21:08:30 +0000869 case PseudoSourceValue::GlobalValueCallEntry:
870 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand(
871 OS, /*PrintType=*/false, MST);
872 break;
Alex Lorenzc3ba7502015-08-14 21:14:50 +0000873 case PseudoSourceValue::ExternalSymbolCallEntry:
874 OS << '$';
875 printLLVMNameWithoutPrefix(
876 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
Alex Lorenz91097a32015-08-12 20:33:26 +0000877 break;
878 }
879 }
Alex Lorenz83127732015-08-07 20:26:52 +0000880 printOffset(Op.getOffset());
Alex Lorenz61420f72015-08-07 20:48:30 +0000881 if (Op.getBaseAlignment() != Op.getSize())
882 OS << ", align " << Op.getBaseAlignment();
Alex Lorenza617c912015-08-17 22:05:15 +0000883 auto AAInfo = Op.getAAInfo();
884 if (AAInfo.TBAA) {
885 OS << ", !tbaa ";
886 AAInfo.TBAA->printAsOperand(OS, MST);
887 }
Alex Lorenza16f6242015-08-17 22:06:40 +0000888 if (AAInfo.Scope) {
889 OS << ", !alias.scope ";
890 AAInfo.Scope->printAsOperand(OS, MST);
891 }
Alex Lorenz03e940d2015-08-17 22:08:02 +0000892 if (AAInfo.NoAlias) {
893 OS << ", !noalias ";
894 AAInfo.NoAlias->printAsOperand(OS, MST);
895 }
Alex Lorenzeb625682015-08-17 22:09:52 +0000896 if (Op.getRanges()) {
897 OS << ", !range ";
898 Op.getRanges()->printAsOperand(OS, MST);
899 }
Alex Lorenz4af7e612015-08-03 23:08:19 +0000900 OS << ')';
901}
902
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000903static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
904 const TargetRegisterInfo *TRI) {
905 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
906 if (Reg == -1) {
907 OS << "<badreg>";
908 return;
909 }
910 printReg(Reg, OS, TRI);
911}
912
913void MIPrinter::print(const MCCFIInstruction &CFI,
914 const TargetRegisterInfo *TRI) {
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000915 switch (CFI.getOperation()) {
Alex Lorenz577d2712015-08-14 21:55:58 +0000916 case MCCFIInstruction::OpSameValue:
917 OS << ".cfi_same_value ";
918 if (CFI.getLabel())
919 OS << "<mcsymbol> ";
920 printCFIRegister(CFI.getRegister(), OS, TRI);
921 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000922 case MCCFIInstruction::OpOffset:
923 OS << ".cfi_offset ";
924 if (CFI.getLabel())
925 OS << "<mcsymbol> ";
926 printCFIRegister(CFI.getRegister(), OS, TRI);
927 OS << ", " << CFI.getOffset();
928 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +0000929 case MCCFIInstruction::OpDefCfaRegister:
930 OS << ".cfi_def_cfa_register ";
931 if (CFI.getLabel())
932 OS << "<mcsymbol> ";
933 printCFIRegister(CFI.getRegister(), OS, TRI);
934 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000935 case MCCFIInstruction::OpDefCfaOffset:
936 OS << ".cfi_def_cfa_offset ";
937 if (CFI.getLabel())
938 OS << "<mcsymbol> ";
939 OS << CFI.getOffset();
940 break;
Alex Lorenzb1393232015-07-29 18:57:23 +0000941 case MCCFIInstruction::OpDefCfa:
942 OS << ".cfi_def_cfa ";
943 if (CFI.getLabel())
944 OS << "<mcsymbol> ";
945 printCFIRegister(CFI.getRegister(), OS, TRI);
946 OS << ", " << CFI.getOffset();
947 break;
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000948 default:
949 // TODO: Print the other CFI Operations.
950 OS << "<unserializable cfi operation>";
951 break;
952 }
953}
954
Alex Lorenz345c1442015-06-15 23:52:35 +0000955void llvm::printMIR(raw_ostream &OS, const Module &M) {
956 yaml::Output Out(OS);
957 Out << const_cast<Module &>(M);
958}
959
960void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
961 MIRPrinter Printer(OS);
962 Printer.print(MF);
963}