Fix a major source of compile-time slowness at -O0 -g by optimizing

the storage of !dbg metadata kinds in the instruction themselves.
The on-the-side hash table works great for metadata that not-all
instructions get, or for metadata that only exists when optimizing.
But when compile-time is everything, it isn't great.

I'm not super thrilled with the fact that this plops a TrackingVH in
Instruction, because it grows it by 3 words.  I'm investigating 
alternatives, but this should be a step in the right direction in any
case.

llvm-svn: 99957
This commit is contained in:
Chris Lattner 2010-03-30 23:03:27 +00:00
parent 6f6ebb663c
commit c263b42d0d
6 changed files with 60 additions and 17 deletions

View File

@ -17,6 +17,7 @@
#include "llvm/User.h" #include "llvm/User.h"
#include "llvm/ADT/ilist_node.h" #include "llvm/ADT/ilist_node.h"
#include "llvm/Support/ValueHandle.h"
namespace llvm { namespace llvm {
@ -31,6 +32,7 @@ class Instruction : public User, public ilist_node<Instruction> {
Instruction(const Instruction &); // Do not implement Instruction(const Instruction &); // Do not implement
BasicBlock *Parent; BasicBlock *Parent;
TrackingVH<MDNode> DbgInfo; // 'dbg' Metadata cache.
enum { enum {
/// HasMetadataBit - This is a bit stored in the SubClassData field which /// HasMetadataBit - This is a bit stored in the SubClassData field which
@ -123,7 +125,7 @@ public:
/// hasMetadata() - Return true if this instruction has any metadata attached /// hasMetadata() - Return true if this instruction has any metadata attached
/// to it. /// to it.
bool hasMetadata() const { bool hasMetadata() const {
return (getSubclassDataFromValue() & HasMetadataBit) != 0; return DbgInfo != 0 || hasMetadataHashEntry();
} }
/// getMetadata - Get the metadata of given kind attached to this Instruction. /// getMetadata - Get the metadata of given kind attached to this Instruction.
@ -155,6 +157,12 @@ public:
void setMetadata(const char *Kind, MDNode *Node); void setMetadata(const char *Kind, MDNode *Node);
private: private:
/// hasMetadataHashEntry - Return true if we have an entry in the on-the-side
/// metadata hash.
bool hasMetadataHashEntry() const {
return (getSubclassDataFromValue() & HasMetadataBit) != 0;
}
// These are all implemented in Metadata.cpp. // These are all implemented in Metadata.cpp.
MDNode *getMetadataImpl(unsigned KindID) const; MDNode *getMetadataImpl(unsigned KindID) const;
MDNode *getMetadataImpl(const char *Kind) const; MDNode *getMetadataImpl(const char *Kind) const;
@ -315,7 +323,7 @@ private:
return Value::getSubclassDataFromValue(); return Value::getSubclassDataFromValue();
} }
void setHasMetadata(bool V) { void setHasMetadataHashEntry(bool V) {
setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
(V ? HasMetadataBit : 0)); (V ? HasMetadataBit : 0));
} }

View File

@ -36,6 +36,12 @@ public:
LLVMContext(); LLVMContext();
~LLVMContext(); ~LLVMContext();
// Pinned metadata names, which always have the same value. This is a
// compile-time performance optimization, not a correctness optimization.
enum {
MD_dbg = 1 // "dbg" -> 1.
};
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
/// This ID is uniqued across modules in the current LLVMContext. /// This ID is uniqued across modules in the current LLVMContext.
unsigned getMDKindID(StringRef Name) const; unsigned getMDKindID(StringRef Name) const;

View File

@ -302,7 +302,7 @@ class TrackingVH : public ValueHandleBase {
ValueTy *getValPtr() const { ValueTy *getValPtr() const {
CheckValidity(); CheckValidity();
return static_cast<ValueTy*>(ValueHandleBase::getValPtr()); return (ValueTy*)ValueHandleBase::getValPtr();
} }
void setValPtr(ValueTy *P) { void setValPtr(ValueTy *P) {
CheckValidity(); CheckValidity();

View File

@ -22,7 +22,7 @@ using namespace llvm;
Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Instruction *InsertBefore) Instruction *InsertBefore)
: User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), DbgInfo(0) {
// Make sure that we get added to a basicblock // Make sure that we get added to a basicblock
LeakDetector::addGarbageObject(this); LeakDetector::addGarbageObject(this);
@ -36,7 +36,7 @@ Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
BasicBlock *InsertAtEnd) BasicBlock *InsertAtEnd)
: User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) { : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0), DbgInfo(0) {
// Make sure that we get added to a basicblock // Make sure that we get added to a basicblock
LeakDetector::addGarbageObject(this); LeakDetector::addGarbageObject(this);

View File

@ -26,7 +26,11 @@ LLVMContext& llvm::getGlobalContext() {
return *GlobalContext; return *GlobalContext;
} }
LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
// Create the first metadata kind, which is always 'dbg'.
unsigned DbgID = getMDKindID("dbg");
assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
}
LLVMContext::~LLVMContext() { delete pImpl; } LLVMContext::~LLVMContext() { delete pImpl; }

View File

@ -430,12 +430,19 @@ MDNode *Instruction::getMetadataImpl(const char *Kind) const {
void Instruction::setMetadata(unsigned KindID, MDNode *Node) { void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
if (Node == 0 && !hasMetadata()) return; if (Node == 0 && !hasMetadata()) return;
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (KindID == LLVMContext::MD_dbg) {
DbgInfo = Node;
return;
}
// Handle the case when we're adding/updating metadata on an instruction. // Handle the case when we're adding/updating metadata on an instruction.
if (Node) { if (Node) {
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked"); assert(!Info.empty() == hasMetadataHashEntry() &&
"HasMetadata bit is wonked");
if (Info.empty()) { if (Info.empty()) {
setHasMetadata(true); setHasMetadataHashEntry(true);
} else { } else {
// Handle replacement of an existing value. // Handle replacement of an existing value.
for (unsigned i = 0, e = Info.size(); i != e; ++i) for (unsigned i = 0, e = Info.size(); i != e; ++i)
@ -451,18 +458,19 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
} }
// Otherwise, we're removing metadata from an instruction. // Otherwise, we're removing metadata from an instruction.
assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && assert(hasMetadataHashEntry() &&
getContext().pImpl->MetadataStore.count(this) &&
"HasMetadata bit out of date!"); "HasMetadata bit out of date!");
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
// Common case is removing the only entry. // Common case is removing the only entry.
if (Info.size() == 1 && Info[0].first == KindID) { if (Info.size() == 1 && Info[0].first == KindID) {
getContext().pImpl->MetadataStore.erase(this); getContext().pImpl->MetadataStore.erase(this);
setHasMetadata(false); setHasMetadataHashEntry(false);
return; return;
} }
// Handle replacement of an existing value. // Handle removal of an existing value.
for (unsigned i = 0, e = Info.size(); i != e; ++i) for (unsigned i = 0, e = Info.size(); i != e; ++i)
if (Info[i].first == KindID) { if (Info[i].first == KindID) {
Info[i] = Info.back(); Info[i] = Info.back();
@ -474,8 +482,14 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
} }
MDNode *Instruction::getMetadataImpl(unsigned KindID) const { MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (KindID == LLVMContext::MD_dbg)
return DbgInfo;
if (!hasMetadataHashEntry()) return 0;
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
assert(hasMetadata() && !Info.empty() && "Shouldn't have called this"); assert(!Info.empty() && "bit out of sync with hash table");
for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
I != E; ++I) I != E; ++I)
@ -485,14 +499,22 @@ MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
} }
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
MDNode*> > &Result)const { MDNode*> > &Result) const {
assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && Result.clear();
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (DbgInfo) {
Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, DbgInfo));
if (!hasMetadataHashEntry()) return;
}
assert(hasMetadataHashEntry() &&
getContext().pImpl->MetadataStore.count(this) &&
"Shouldn't have called this"); "Shouldn't have called this");
const LLVMContextImpl::MDMapTy &Info = const LLVMContextImpl::MDMapTy &Info =
getContext().pImpl->MetadataStore.find(this)->second; getContext().pImpl->MetadataStore.find(this)->second;
assert(!Info.empty() && "Shouldn't have called this"); assert(!Info.empty() && "Shouldn't have called this");
Result.clear();
Result.append(Info.begin(), Info.end()); Result.append(Info.begin(), Info.end());
// Sort the resulting array so it is stable. // Sort the resulting array so it is stable.
@ -503,7 +525,10 @@ void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
/// removeAllMetadata - Remove all metadata from this instruction. /// removeAllMetadata - Remove all metadata from this instruction.
void Instruction::removeAllMetadata() { void Instruction::removeAllMetadata() {
assert(hasMetadata() && "Caller should check"); assert(hasMetadata() && "Caller should check");
DbgInfo = 0;
if (hasMetadataHashEntry()) {
getContext().pImpl->MetadataStore.erase(this); getContext().pImpl->MetadataStore.erase(this);
setHasMetadata(false); setHasMetadataHashEntry(false);
}
} }