#include "llvm/User.h"
#include "llvm/ADT/ilist_node.h"
+#include "llvm/Support/ValueHandle.h"
namespace llvm {
Instruction(const Instruction &); // Do not implement
BasicBlock *Parent;
+ TrackingVH<MDNode> DbgInfo; // 'dbg' Metadata cache.
enum {
/// HasMetadataBit - This is a bit stored in the SubClassData field which
/// hasMetadata() - Return true if this instruction has any metadata attached
/// to it.
bool hasMetadata() const {
- return (getSubclassDataFromValue() & HasMetadataBit) != 0;
+ return DbgInfo != 0 || hasMetadataHashEntry();
}
/// getMetadata - Get the metadata of given kind attached to this Instruction.
void setMetadata(const char *Kind, MDNode *Node);
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.
MDNode *getMetadataImpl(unsigned KindID) const;
MDNode *getMetadataImpl(const char *Kind) const;
return Value::getSubclassDataFromValue();
}
- void setHasMetadata(bool V) {
+ void setHasMetadataHashEntry(bool V) {
setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
(V ? HasMetadataBit : 0));
}
Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
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
LeakDetector::addGarbageObject(this);
Instruction::Instruction(const Type *ty, unsigned it, Use *Ops, unsigned NumOps,
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
LeakDetector::addGarbageObject(this);
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
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.
if (Node) {
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()) {
- setHasMetadata(true);
+ setHasMetadataHashEntry(true);
} else {
// Handle replacement of an existing value.
for (unsigned i = 0, e = Info.size(); i != e; ++i)
}
// 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!");
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
// Common case is removing the only entry.
if (Info.size() == 1 && Info[0].first == KindID) {
getContext().pImpl->MetadataStore.erase(this);
- setHasMetadata(false);
+ setHasMetadataHashEntry(false);
return;
}
- // Handle replacement of an existing value.
+ // Handle removal of an existing value.
for (unsigned i = 0, e = Info.size(); i != e; ++i)
if (Info[i].first == KindID) {
Info[i] = Info.back();
}
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];
- 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();
I != E; ++I)
}
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
- MDNode*> > &Result)const {
- assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
+ MDNode*> > &Result) const {
+ 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");
const LLVMContextImpl::MDMapTy &Info =
getContext().pImpl->MetadataStore.find(this)->second;
assert(!Info.empty() && "Shouldn't have called this");
- Result.clear();
Result.append(Info.begin(), Info.end());
// Sort the resulting array so it is stable.
/// removeAllMetadata - Remove all metadata from this instruction.
void Instruction::removeAllMetadata() {
assert(hasMetadata() && "Caller should check");
- getContext().pImpl->MetadataStore.erase(this);
- setHasMetadata(false);
+ DbgInfo = 0;
+ if (hasMetadataHashEntry()) {
+ getContext().pImpl->MetadataStore.erase(this);
+ setHasMetadataHashEntry(false);
+ }
}