1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that prints out the LLVM IR and machine
11 // functions using the MIR serialization format.
13 //===----------------------------------------------------------------------===//
15 #include "MIRPrinter.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/MIRYamlMapping.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ModuleSlotTracker.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetSubtargetInfo.h"
34 /// This class prints out the machine functions using the MIR serialization
38 DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
41 MIRPrinter(raw_ostream &OS) : OS(OS) {}
43 void print(const MachineFunction &MF);
45 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
46 const TargetRegisterInfo *TRI);
47 void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
48 void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
49 const MachineBasicBlock &MBB);
50 void convertStackObjects(yaml::MachineFunction &MF,
51 const MachineFrameInfo &MFI);
54 void initRegisterMaskIds(const MachineFunction &MF);
57 /// This class prints out the machine instructions using the MIR serialization
61 ModuleSlotTracker &MST;
62 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
65 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
66 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
67 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
69 void print(const MachineInstr &MI);
70 void printMBBReference(const MachineBasicBlock &MBB);
71 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
74 } // end anonymous namespace
79 /// This struct serializes the LLVM IR module.
80 template <> struct BlockScalarTraits<Module> {
81 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
82 Mod.print(OS, nullptr);
84 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
85 llvm_unreachable("LLVM Module is supposed to be parsed separately");
90 } // end namespace yaml
91 } // end namespace llvm
93 static void printReg(unsigned Reg, raw_ostream &OS,
94 const TargetRegisterInfo *TRI) {
95 // TODO: Print Stack Slots.
98 else if (TargetRegisterInfo::isVirtualRegister(Reg))
99 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
100 else if (Reg < TRI->getNumRegs())
101 OS << '%' << StringRef(TRI->getName(Reg)).lower();
103 llvm_unreachable("Can't print this kind of register yet");
106 void MIRPrinter::print(const MachineFunction &MF) {
107 initRegisterMaskIds(MF);
109 yaml::MachineFunction YamlMF;
110 YamlMF.Name = MF.getName();
111 YamlMF.Alignment = MF.getAlignment();
112 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
113 YamlMF.HasInlineAsm = MF.hasInlineAsm();
114 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
115 convert(YamlMF.FrameInfo, *MF.getFrameInfo());
116 convertStackObjects(YamlMF, *MF.getFrameInfo());
119 ModuleSlotTracker MST(MF.getFunction()->getParent());
120 for (const auto &MBB : MF) {
121 // TODO: Allow printing of non sequentially numbered MBBs.
122 // This is currently needed as the basic block references get their index
123 // from MBB.getNumber(), thus it should be sequential so that the parser can
124 // map back to the correct MBBs when parsing the output.
125 assert(MBB.getNumber() == I++ &&
126 "Can't print MBBs that aren't sequentially numbered");
128 yaml::MachineBasicBlock YamlMBB;
129 convert(MST, YamlMBB, MBB);
130 YamlMF.BasicBlocks.push_back(YamlMBB);
132 yaml::Output Out(OS);
136 void MIRPrinter::convert(yaml::MachineFunction &MF,
137 const MachineRegisterInfo &RegInfo,
138 const TargetRegisterInfo *TRI) {
139 MF.IsSSA = RegInfo.isSSA();
140 MF.TracksRegLiveness = RegInfo.tracksLiveness();
141 MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
143 // Print the virtual register definitions.
144 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
145 unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
146 yaml::VirtualRegisterDefinition VReg;
149 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
150 MF.VirtualRegisters.push_back(VReg);
154 void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
155 const MachineFrameInfo &MFI) {
156 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
157 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
158 YamlMFI.HasStackMap = MFI.hasStackMap();
159 YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
160 YamlMFI.StackSize = MFI.getStackSize();
161 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
162 YamlMFI.MaxAlignment = MFI.getMaxAlignment();
163 YamlMFI.AdjustsStack = MFI.adjustsStack();
164 YamlMFI.HasCalls = MFI.hasCalls();
165 YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
166 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
167 YamlMFI.HasVAStart = MFI.hasVAStart();
168 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
171 void MIRPrinter::convertStackObjects(yaml::MachineFunction &MF,
172 const MachineFrameInfo &MFI) {
173 // Process fixed stack objects.
175 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
176 if (MFI.isDeadObjectIndex(I))
179 yaml::FixedMachineStackObject YamlObject;
180 YamlObject.ID = ID++;
181 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
182 ? yaml::FixedMachineStackObject::SpillSlot
183 : yaml::FixedMachineStackObject::DefaultType;
184 YamlObject.Offset = MFI.getObjectOffset(I);
185 YamlObject.Size = MFI.getObjectSize(I);
186 YamlObject.Alignment = MFI.getObjectAlignment(I);
187 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I);
188 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I);
189 MF.FixedStackObjects.push_back(YamlObject);
190 // TODO: Store the mapping between fixed object IDs and object indices to
191 // print the fixed stack object references correctly.
194 // Process ordinary stack objects.
196 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) {
197 if (MFI.isDeadObjectIndex(I))
200 yaml::MachineStackObject YamlObject;
201 YamlObject.ID = ID++;
202 YamlObject.Type = MFI.isSpillSlotObjectIndex(I)
203 ? yaml::MachineStackObject::SpillSlot
204 : MFI.isVariableSizedObjectIndex(I)
205 ? yaml::MachineStackObject::VariableSized
206 : yaml::MachineStackObject::DefaultType;
207 YamlObject.Offset = MFI.getObjectOffset(I);
208 YamlObject.Size = MFI.getObjectSize(I);
209 YamlObject.Alignment = MFI.getObjectAlignment(I);
211 MF.StackObjects.push_back(YamlObject);
212 // TODO: Store the mapping between object IDs and object indices to print
213 // the stack object references correctly.
217 void MIRPrinter::convert(ModuleSlotTracker &MST,
218 yaml::MachineBasicBlock &YamlMBB,
219 const MachineBasicBlock &MBB) {
220 assert(MBB.getNumber() >= 0 && "Invalid MBB number");
221 YamlMBB.ID = (unsigned)MBB.getNumber();
222 // TODO: Serialize unnamed BB references.
223 if (const auto *BB = MBB.getBasicBlock())
224 YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
226 YamlMBB.Name.Value = "";
227 YamlMBB.Alignment = MBB.getAlignment();
228 YamlMBB.AddressTaken = MBB.hasAddressTaken();
229 YamlMBB.IsLandingPad = MBB.isLandingPad();
230 for (const auto *SuccMBB : MBB.successors()) {
232 raw_string_ostream StrOS(Str);
233 MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
234 YamlMBB.Successors.push_back(StrOS.str());
237 // Print the machine instructions.
238 YamlMBB.Instructions.reserve(MBB.size());
240 for (const auto &MI : MBB) {
241 raw_string_ostream StrOS(Str);
242 MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
243 YamlMBB.Instructions.push_back(StrOS.str());
248 void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
249 const auto *TRI = MF.getSubtarget().getRegisterInfo();
251 for (const uint32_t *Mask : TRI->getRegMasks())
252 RegisterMaskIds.insert(std::make_pair(Mask, I++));
255 void MIPrinter::print(const MachineInstr &MI) {
256 const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
257 const auto *TRI = SubTarget.getRegisterInfo();
258 assert(TRI && "Expected target register info");
259 const auto *TII = SubTarget.getInstrInfo();
260 assert(TII && "Expected target instruction info");
262 unsigned I = 0, E = MI.getNumOperands();
263 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
264 !MI.getOperand(I).isImplicit();
268 print(MI.getOperand(I), TRI);
273 OS << TII->getName(MI.getOpcode());
274 // TODO: Print the instruction flags, machine mem operands.
278 bool NeedComma = false;
282 print(MI.getOperand(I), TRI);
287 void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
288 OS << "%bb." << MBB.getNumber();
289 if (const auto *BB = MBB.getBasicBlock()) {
291 OS << '.' << BB->getName();
295 void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
296 switch (Op.getType()) {
297 case MachineOperand::MO_Register:
298 // TODO: Print the other register flags.
300 OS << (Op.isDef() ? "implicit-def " : "implicit ");
307 printReg(Op.getReg(), OS, TRI);
308 // Print the sub register.
309 if (Op.getSubReg() != 0)
310 OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
312 case MachineOperand::MO_Immediate:
315 case MachineOperand::MO_MachineBasicBlock:
316 printMBBReference(*Op.getMBB());
318 case MachineOperand::MO_GlobalAddress:
319 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
320 // TODO: Print offset and target flags.
322 case MachineOperand::MO_RegisterMask: {
323 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
324 if (RegMaskInfo != RegisterMaskIds.end())
325 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
327 llvm_unreachable("Can't print this machine register mask yet.");
331 // TODO: Print the other machine operands.
332 llvm_unreachable("Can't print this machine operand at the moment");
336 void llvm::printMIR(raw_ostream &OS, const Module &M) {
337 yaml::Output Out(OS);
338 Out << const_cast<Module &>(M);
341 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
342 MIRPrinter Printer(OS);