2015-06-15 23:52:35 +00:00
|
|
|
//===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the class that prints out the LLVM IR and machine
|
|
|
|
// functions using the MIR serialization format.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MIRPrinter.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241844 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-09 19:55:27 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2015-06-24 19:56:10 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2015-06-15 23:52:35 +00:00
|
|
|
#include "llvm/CodeGen/MIRYamlMapping.h"
|
2015-06-19 17:43:07 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2015-06-15 23:52:35 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-07-07 23:27:53 +00:00
|
|
|
#include "llvm/IR/ModuleSlotTracker.h"
|
2015-06-15 23:52:35 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2015-06-22 17:02:30 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2015-06-15 23:52:35 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This class prints out the machine functions using the MIR serialization
|
|
|
|
/// format.
|
|
|
|
class MIRPrinter {
|
|
|
|
raw_ostream &OS;
|
2015-06-29 16:57:06 +00:00
|
|
|
DenseMap<const uint32_t *, unsigned> RegisterMaskIds;
|
2015-06-15 23:52:35 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
MIRPrinter(raw_ostream &OS) : OS(OS) {}
|
|
|
|
|
|
|
|
void print(const MachineFunction &MF);
|
2015-06-19 17:43:07 +00:00
|
|
|
|
2015-07-09 22:23:13 +00:00
|
|
|
void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo,
|
|
|
|
const TargetRegisterInfo *TRI);
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241844 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-09 19:55:27 +00:00
|
|
|
void convert(yaml::MachineFrameInfo &YamlMFI, const MachineFrameInfo &MFI);
|
2015-07-07 23:27:53 +00:00
|
|
|
void convert(ModuleSlotTracker &MST, yaml::MachineBasicBlock &YamlMBB,
|
2015-06-26 22:56:48 +00:00
|
|
|
const MachineBasicBlock &MBB);
|
2015-06-29 16:57:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void initRegisterMaskIds(const MachineFunction &MF);
|
2015-06-15 23:52:35 +00:00
|
|
|
};
|
|
|
|
|
2015-06-22 17:02:30 +00:00
|
|
|
/// This class prints out the machine instructions using the MIR serialization
|
|
|
|
/// format.
|
|
|
|
class MIPrinter {
|
|
|
|
raw_ostream &OS;
|
2015-07-07 23:27:53 +00:00
|
|
|
ModuleSlotTracker &MST;
|
2015-06-29 16:57:06 +00:00
|
|
|
const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds;
|
2015-06-22 17:02:30 +00:00
|
|
|
|
|
|
|
public:
|
2015-07-07 23:27:53 +00:00
|
|
|
MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST,
|
2015-06-29 16:57:06 +00:00
|
|
|
const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds)
|
2015-07-07 23:27:53 +00:00
|
|
|
: OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds) {}
|
2015-06-22 17:02:30 +00:00
|
|
|
|
|
|
|
void print(const MachineInstr &MI);
|
2015-06-30 18:00:16 +00:00
|
|
|
void printMBBReference(const MachineBasicBlock &MBB);
|
2015-06-23 16:35:26 +00:00
|
|
|
void print(const MachineOperand &Op, const TargetRegisterInfo *TRI);
|
2015-06-22 17:02:30 +00:00
|
|
|
};
|
|
|
|
|
2015-06-15 23:52:35 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
/// This struct serializes the LLVM IR module.
|
|
|
|
template <> struct BlockScalarTraits<Module> {
|
|
|
|
static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
|
|
|
|
Mod.print(OS, nullptr);
|
|
|
|
}
|
|
|
|
static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
|
|
|
|
llvm_unreachable("LLVM Module is supposed to be parsed separately");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace yaml
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
void MIRPrinter::print(const MachineFunction &MF) {
|
2015-06-29 16:57:06 +00:00
|
|
|
initRegisterMaskIds(MF);
|
|
|
|
|
2015-06-15 23:52:35 +00:00
|
|
|
yaml::MachineFunction YamlMF;
|
|
|
|
YamlMF.Name = MF.getName();
|
2015-06-16 00:10:47 +00:00
|
|
|
YamlMF.Alignment = MF.getAlignment();
|
|
|
|
YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
|
|
|
|
YamlMF.HasInlineAsm = MF.hasInlineAsm();
|
2015-07-09 22:23:13 +00:00
|
|
|
convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo());
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241844 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-09 19:55:27 +00:00
|
|
|
convert(YamlMF.FrameInfo, *MF.getFrameInfo());
|
2015-06-26 16:46:11 +00:00
|
|
|
|
|
|
|
int I = 0;
|
2015-07-07 23:27:53 +00:00
|
|
|
ModuleSlotTracker MST(MF.getFunction()->getParent());
|
2015-06-19 17:43:07 +00:00
|
|
|
for (const auto &MBB : MF) {
|
2015-06-26 16:46:11 +00:00
|
|
|
// TODO: Allow printing of non sequentially numbered MBBs.
|
|
|
|
// This is currently needed as the basic block references get their index
|
|
|
|
// from MBB.getNumber(), thus it should be sequential so that the parser can
|
|
|
|
// map back to the correct MBBs when parsing the output.
|
|
|
|
assert(MBB.getNumber() == I++ &&
|
|
|
|
"Can't print MBBs that aren't sequentially numbered");
|
2015-06-26 17:07:27 +00:00
|
|
|
(void)I;
|
2015-06-19 17:43:07 +00:00
|
|
|
yaml::MachineBasicBlock YamlMBB;
|
2015-07-07 23:27:53 +00:00
|
|
|
convert(MST, YamlMBB, MBB);
|
2015-06-19 17:43:07 +00:00
|
|
|
YamlMF.BasicBlocks.push_back(YamlMBB);
|
|
|
|
}
|
2015-06-15 23:52:35 +00:00
|
|
|
yaml::Output Out(OS);
|
|
|
|
Out << YamlMF;
|
|
|
|
}
|
|
|
|
|
2015-06-24 19:56:10 +00:00
|
|
|
void MIRPrinter::convert(yaml::MachineFunction &MF,
|
2015-07-09 22:23:13 +00:00
|
|
|
const MachineRegisterInfo &RegInfo,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2015-06-24 19:56:10 +00:00
|
|
|
MF.IsSSA = RegInfo.isSSA();
|
|
|
|
MF.TracksRegLiveness = RegInfo.tracksLiveness();
|
|
|
|
MF.TracksSubRegLiveness = RegInfo.subRegLivenessEnabled();
|
2015-07-09 22:23:13 +00:00
|
|
|
|
|
|
|
// Print the virtual register definitions.
|
|
|
|
for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
|
|
|
|
yaml::VirtualRegisterDefinition VReg;
|
|
|
|
VReg.ID = I;
|
|
|
|
VReg.Class =
|
|
|
|
StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
|
|
|
|
MF.VirtualRegisters.push_back(VReg);
|
|
|
|
}
|
2015-06-24 19:56:10 +00:00
|
|
|
}
|
|
|
|
|
MIR Serialization: Serialize the simple MachineFrameInfo attributes.
This commit serializes the 13 scalar boolean and integer attributes from the
MachineFrameInfo class: IsFrameAddressTaken, IsReturnAddressTaken, HasStackMap,
HasPatchPoint, StackSize, OffsetAdjustment, MaxAlignment, AdjustsStack,
HasCalls, MaxCallFrameSize, HasOpaqueSPAdjustment, HasVAStart, and
HasMustTailInVarArgFunc. These attributes are serialized as part
of the frameInfo YAML mapping, which itself is a part of the machine function's
YAML mapping.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241844 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-09 19:55:27 +00:00
|
|
|
void MIRPrinter::convert(yaml::MachineFrameInfo &YamlMFI,
|
|
|
|
const MachineFrameInfo &MFI) {
|
|
|
|
YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken();
|
|
|
|
YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken();
|
|
|
|
YamlMFI.HasStackMap = MFI.hasStackMap();
|
|
|
|
YamlMFI.HasPatchPoint = MFI.hasPatchPoint();
|
|
|
|
YamlMFI.StackSize = MFI.getStackSize();
|
|
|
|
YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment();
|
|
|
|
YamlMFI.MaxAlignment = MFI.getMaxAlignment();
|
|
|
|
YamlMFI.AdjustsStack = MFI.adjustsStack();
|
|
|
|
YamlMFI.HasCalls = MFI.hasCalls();
|
|
|
|
YamlMFI.MaxCallFrameSize = MFI.getMaxCallFrameSize();
|
|
|
|
YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment();
|
|
|
|
YamlMFI.HasVAStart = MFI.hasVAStart();
|
|
|
|
YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc();
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:27:53 +00:00
|
|
|
void MIRPrinter::convert(ModuleSlotTracker &MST,
|
|
|
|
yaml::MachineBasicBlock &YamlMBB,
|
2015-06-19 17:43:07 +00:00
|
|
|
const MachineBasicBlock &MBB) {
|
2015-06-26 16:46:11 +00:00
|
|
|
assert(MBB.getNumber() >= 0 && "Invalid MBB number");
|
|
|
|
YamlMBB.ID = (unsigned)MBB.getNumber();
|
2015-06-19 17:43:07 +00:00
|
|
|
// TODO: Serialize unnamed BB references.
|
|
|
|
if (const auto *BB = MBB.getBasicBlock())
|
2015-07-08 20:22:20 +00:00
|
|
|
YamlMBB.Name.Value = BB->hasName() ? BB->getName() : "<unnamed bb>";
|
2015-06-19 17:43:07 +00:00
|
|
|
else
|
2015-07-08 20:22:20 +00:00
|
|
|
YamlMBB.Name.Value = "";
|
2015-06-19 17:43:07 +00:00
|
|
|
YamlMBB.Alignment = MBB.getAlignment();
|
|
|
|
YamlMBB.AddressTaken = MBB.hasAddressTaken();
|
|
|
|
YamlMBB.IsLandingPad = MBB.isLandingPad();
|
2015-06-30 18:32:02 +00:00
|
|
|
for (const auto *SuccMBB : MBB.successors()) {
|
2015-06-30 18:16:42 +00:00
|
|
|
std::string Str;
|
|
|
|
raw_string_ostream StrOS(Str);
|
2015-07-07 23:27:53 +00:00
|
|
|
MIPrinter(StrOS, MST, RegisterMaskIds).printMBBReference(*SuccMBB);
|
2015-06-30 18:16:42 +00:00
|
|
|
YamlMBB.Successors.push_back(StrOS.str());
|
|
|
|
}
|
2015-06-22 17:02:30 +00:00
|
|
|
|
|
|
|
// Print the machine instructions.
|
|
|
|
YamlMBB.Instructions.reserve(MBB.size());
|
|
|
|
std::string Str;
|
|
|
|
for (const auto &MI : MBB) {
|
|
|
|
raw_string_ostream StrOS(Str);
|
2015-07-07 23:27:53 +00:00
|
|
|
MIPrinter(StrOS, MST, RegisterMaskIds).print(MI);
|
2015-06-22 17:02:30 +00:00
|
|
|
YamlMBB.Instructions.push_back(StrOS.str());
|
|
|
|
Str.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 16:57:06 +00:00
|
|
|
void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) {
|
|
|
|
const auto *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
unsigned I = 0;
|
|
|
|
for (const uint32_t *Mask : TRI->getRegMasks())
|
|
|
|
RegisterMaskIds.insert(std::make_pair(Mask, I++));
|
|
|
|
}
|
|
|
|
|
2015-06-22 17:02:30 +00:00
|
|
|
void MIPrinter::print(const MachineInstr &MI) {
|
|
|
|
const auto &SubTarget = MI.getParent()->getParent()->getSubtarget();
|
2015-06-23 16:35:26 +00:00
|
|
|
const auto *TRI = SubTarget.getRegisterInfo();
|
|
|
|
assert(TRI && "Expected target register info");
|
2015-06-22 17:02:30 +00:00
|
|
|
const auto *TII = SubTarget.getInstrInfo();
|
|
|
|
assert(TII && "Expected target instruction info");
|
|
|
|
|
2015-06-23 16:35:26 +00:00
|
|
|
unsigned I = 0, E = MI.getNumOperands();
|
|
|
|
for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() &&
|
|
|
|
!MI.getOperand(I).isImplicit();
|
|
|
|
++I) {
|
|
|
|
if (I)
|
|
|
|
OS << ", ";
|
|
|
|
print(MI.getOperand(I), TRI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I)
|
|
|
|
OS << " = ";
|
2015-06-22 17:02:30 +00:00
|
|
|
OS << TII->getName(MI.getOpcode());
|
2015-06-23 16:35:26 +00:00
|
|
|
// TODO: Print the instruction flags, machine mem operands.
|
|
|
|
if (I < E)
|
|
|
|
OS << ' ';
|
|
|
|
|
|
|
|
bool NeedComma = false;
|
|
|
|
for (; I < E; ++I) {
|
|
|
|
if (NeedComma)
|
|
|
|
OS << ", ";
|
|
|
|
print(MI.getOperand(I), TRI);
|
|
|
|
NeedComma = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printReg(unsigned Reg, raw_ostream &OS,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
// TODO: Print Stack Slots.
|
|
|
|
// TODO: Print virtual registers.
|
2015-06-24 17:34:58 +00:00
|
|
|
if (!Reg)
|
|
|
|
OS << '_';
|
|
|
|
else if (Reg < TRI->getNumRegs())
|
2015-06-23 16:35:26 +00:00
|
|
|
OS << '%' << StringRef(TRI->getName(Reg)).lower();
|
|
|
|
else
|
|
|
|
llvm_unreachable("Can't print this kind of register yet");
|
|
|
|
}
|
|
|
|
|
2015-06-30 18:00:16 +00:00
|
|
|
void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) {
|
|
|
|
OS << "%bb." << MBB.getNumber();
|
|
|
|
if (const auto *BB = MBB.getBasicBlock()) {
|
|
|
|
if (BB->hasName())
|
|
|
|
OS << '.' << BB->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:35:26 +00:00
|
|
|
void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
|
|
|
|
switch (Op.getType()) {
|
|
|
|
case MachineOperand::MO_Register:
|
2015-07-06 23:07:26 +00:00
|
|
|
// TODO: Print the other register flags.
|
|
|
|
if (Op.isImplicit())
|
|
|
|
OS << (Op.isDef() ? "implicit-def " : "implicit ");
|
2015-07-07 20:34:53 +00:00
|
|
|
if (Op.isDead())
|
|
|
|
OS << "dead ";
|
2015-07-08 21:23:34 +00:00
|
|
|
if (Op.isKill())
|
|
|
|
OS << "killed ";
|
2015-07-08 23:58:31 +00:00
|
|
|
if (Op.isUndef())
|
|
|
|
OS << "undef ";
|
2015-06-23 16:35:26 +00:00
|
|
|
printReg(Op.getReg(), OS, TRI);
|
|
|
|
// TODO: Print sub register.
|
|
|
|
break;
|
2015-06-23 23:42:28 +00:00
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
OS << Op.getImm();
|
|
|
|
break;
|
2015-06-26 16:46:11 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2015-06-30 18:00:16 +00:00
|
|
|
printMBBReference(*Op.getMBB());
|
2015-06-26 16:46:11 +00:00
|
|
|
break;
|
2015-06-26 22:56:48 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2015-07-07 23:27:53 +00:00
|
|
|
Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
|
2015-06-26 22:56:48 +00:00
|
|
|
// TODO: Print offset and target flags.
|
|
|
|
break;
|
2015-06-29 16:57:06 +00:00
|
|
|
case MachineOperand::MO_RegisterMask: {
|
|
|
|
auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask());
|
|
|
|
if (RegMaskInfo != RegisterMaskIds.end())
|
|
|
|
OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
|
|
|
|
else
|
|
|
|
llvm_unreachable("Can't print this machine register mask yet.");
|
|
|
|
break;
|
|
|
|
}
|
2015-06-23 16:35:26 +00:00
|
|
|
default:
|
|
|
|
// TODO: Print the other machine operands.
|
|
|
|
llvm_unreachable("Can't print this machine operand at the moment");
|
|
|
|
}
|
2015-06-19 17:43:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 23:52:35 +00:00
|
|
|
void llvm::printMIR(raw_ostream &OS, const Module &M) {
|
|
|
|
yaml::Output Out(OS);
|
|
|
|
Out << const_cast<Module &>(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
|
|
|
|
MIRPrinter Printer(OS);
|
|
|
|
Printer.print(MF);
|
|
|
|
}
|