IR: Expose ModuleSlotTracker in Value::print()

Allow callers of `Value::print()` and `Metadata::print()` to pass in a
`ModuleSlotTracker`.  This allows them to pay only once for calculating
module-level slots (such as Metadata).

This is related to PR23865, where there was a huge cost for
`MachineFunction::print()`.  Although I don't have a *particular* user
in mind for this new code, I have hit big slowdowns before when running
`opt -debug`, and I think this will be useful.  Going forward, if
someone hits a big slowdown with `print()` statements, they can create a
`ModuleSlotTracker` and send it through.  Similarly, adding support to
`Value::dump()` and `Metadata::dump()` should be trivial.

I added unit tests to be sure the `print()` functions actually behave
the same way with and without the slot tracker.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240867 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith
2015-06-27 00:38:26 +00:00
parent e92934e078
commit 2a5fda92b4
5 changed files with 118 additions and 10 deletions

View File

@ -3193,21 +3193,35 @@ static bool isReferencingMDNode(const Instruction &I) {
}
void Value::print(raw_ostream &ROS) const {
bool ShouldInitializeAllMetadata = false;
if (auto *I = dyn_cast<Instruction>(this))
ShouldInitializeAllMetadata = isReferencingMDNode(*I);
else if (isa<Function>(this) || isa<MetadataAsValue>(this))
ShouldInitializeAllMetadata = true;
ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
print(ROS, MST);
}
void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST) const {
formatted_raw_ostream OS(ROS);
SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
SlotTracker &SlotTable =
MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
auto incorporateFunction = [&](const Function *F) {
if (F)
MST.incorporateFunction(*F);
};
if (const Instruction *I = dyn_cast<Instruction>(this)) {
const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr;
SlotTracker SlotTable(
F,
/* ShouldInitializeAllMetadata */ isReferencingMDNode(*I));
incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr);
W.printInstruction(*I);
} else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
SlotTracker SlotTable(BB->getParent());
incorporateFunction(BB->getParent());
AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr);
W.printBasicBlock(BB);
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
SlotTracker SlotTable(GV->getParent(),
/* ShouldInitializeAllMetadata */ isa<Function>(GV));
AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr);
if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
W.printGlobal(V);
@ -3216,14 +3230,14 @@ void Value::print(raw_ostream &ROS) const {
else
W.printAlias(cast<GlobalAlias>(GV));
} else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
V->getMetadata()->print(ROS, getModuleFromVal(V));
V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
} else if (const Constant *C = dyn_cast<Constant>(this)) {
TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS);
OS << ' ';
WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
} else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
this->printAsOperand(OS);
this->printAsOperand(OS, /* PrintType */ true, MST);
} else {
llvm_unreachable("Unknown value to print out!");
}
@ -3315,6 +3329,11 @@ void Metadata::print(raw_ostream &OS, const Module *M) const {
printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
}
void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
const Module *M) const {
printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
}
// Value::dump - allow easy printing of Values from the debugger.
LLVM_DUMP_METHOD
void Value::dump() const { print(dbgs()); dbgs() << '\n'; }