#include "llvm/Instruction.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/SmallString.h"
#include "SymbolTableListTraitsImpl.h"
#include "llvm/Support/ValueHandle.h"
using namespace llvm;
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
LLVMContextImpl *pImpl = Context.pImpl;
- StringMapEntry<MDString *> &Entry =
+ StringMapEntry<MDString *> &Entry =
pImpl->MDStringCache.GetOrCreateValue(Str);
MDString *&S = Entry.getValue();
if (!S) S = new MDString(Context, Entry.getKey());
MDString *MDString::get(LLVMContext &Context, const char *Str) {
LLVMContextImpl *pImpl = Context.pImpl;
- StringMapEntry<MDString *> &Entry =
+ StringMapEntry<MDString *> &Entry =
pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
MDString *&S = Entry.getValue();
if (!S) S = new MDString(Context, Entry.getKey());
public:
MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
~MDNodeOperand() {}
-
+
void set(Value *V) {
setValPtr(V);
}
-
+
virtual void deleted();
virtual void allUsesReplacedWith(Value *NV);
};
bool isFunctionLocal)
: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
NumOperands = NumVals;
-
+
if (isFunctionLocal)
setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
/// ~MDNode - Destroy MDNode.
MDNode::~MDNode() {
- assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
+ assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
"Not being destroyed through destroy()?");
if (!isNotUniqued()) {
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
pImpl->MDNodeSet.RemoveNode(this);
}
-
+
// Destroy the operands.
for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Op != E; ++Op)
Value *V = Vals[i];
if (!V) continue;
if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
- isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()) {
+ (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
isFunctionLocal = true;
break;
}
// Coallocate space for the node and Operands together, then placement new.
void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
-
+
// InsertPoint will have been set by the FindNodeOrInsertPos call.
pImpl->MDNodeSet.InsertNode(N, InsertPoint);
}
// Replace value from this node's operand list.
void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Value *From = *Op;
-
+
if (From == To)
return;
// If this node is already not being uniqued (because one of the operands
// already went to null), then there is nothing else to do here.
if (isNotUniqued()) return;
-
+
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
// Remove "this" from the context map. FoldingSet doesn't have to reprofile
setIsNotUniqued();
return;
}
-
+
// Now that the node is out of the folding set, get ready to reinsert it.
// First, check to see if another node with the same operands already exists
// in the set. If it doesn't exist, this returns the position to insert it.
//===----------------------------------------------------------------------===//
// NamedMDNode implementation.
//
+
+namespace llvm {
+// SymbolTableListTraits specialization for MDSymbolTable.
+void ilist_traits<NamedMDNode>
+::addNodeToList(NamedMDNode *N) {
+ assert(N->getParent() == 0 && "Value already in a container!!");
+ Module *Owner = getListOwner();
+ N->setParent(Owner);
+ MDSymbolTable &ST = Owner->getMDSymbolTable();
+ ST.insert(N->getName(), N);
+}
+
+void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
+ N->setParent(0);
+ Module *Owner = getListOwner();
+ MDSymbolTable &ST = Owner->getMDSymbolTable();
+ ST.remove(N->getName());
+}
+}
+
static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
return *(SmallVector<WeakVH, 4>*)Operands;
}
-NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N,
- MDNode *const *MDs,
+NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
+ MDNode *const *MDs,
unsigned NumMDs, Module *ParentModule)
: Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
setName(N);
Operands = new SmallVector<WeakVH, 4>();
-
+
SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
for (unsigned i = 0; i != NumMDs; ++i)
Node.push_back(WeakVH(MDs[i]));
- if (ParentModule) {
+ if (ParentModule)
ParentModule->getNamedMDList().push_back(this);
- ParentModule->addMDNodeName(N, this);
- }
}
NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
assert(NMD && "Invalid source NamedMDNode!");
SmallVector<MDNode *, 4> Elems;
Elems.reserve(NMD->getNumOperands());
-
+
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
Elems.push_back(NMD->getOperand(i));
return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
/// eraseFromParent - Drop all references and remove the node from parent
/// module.
void NamedMDNode::eraseFromParent() {
- getParent()->getMDSymbolTable().remove(getName());
getParent()->getNamedMDList().erase(this);
}
}
/// setName - Set the name of this named metadata.
-void NamedMDNode::setName(StringRef N) {
- if (!N.empty())
- Name = N.str();
+void NamedMDNode::setName(const Twine &NewName) {
+ assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
+
+ SmallString<256> NameData;
+ NewName.toVector(NameData);
+
+ const char *NameStr = NameData.data();
+ unsigned NameLen = NameData.size();
+
+ StringRef NameRef = StringRef(NameStr, NameLen);
+ // Name isn't changing?
+ if (getName() == NameRef)
+ return;
+
+ Name = NameRef.str();
+ if (Parent)
+ Parent->getMDSymbolTable().insert(NameRef, this);
}
/// getName - Return a constant reference to this named metadata's name.
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
unsigned LLVMContext::getMDKindID(StringRef Name) const {
assert(isValidName(Name) && "Invalid MDNode name");
-
+
unsigned &Entry = pImpl->CustomMDKindNames[Name];
-
+
// If this is new, assign it its ID.
if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
return Entry;
Names.resize(pImpl->CustomMDKindNames.size()+1);
Names[0] = "";
for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
- E = pImpl->CustomMDKindNames.end(); I != E; ++I)
+ E = pImpl->CustomMDKindNames.end(); I != E; ++I)
// MD Handlers are numbered from 1.
Names[I->second] = I->first();
}
/// Node is null.
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
if (Node == 0 && !hasMetadata()) return;
-
+
// Handle the case when we're adding/updating metadata on an instruction.
if (Node) {
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
return;
}
}
-
+
// No replacement, just add it to the list.
Info.push_back(std::make_pair(KindID, Node));
return;
}
-
+
// Otherwise, we're removing metadata from an instruction.
assert(hasMetadata() && 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);
return;
}
-
+
// Handle replacement of an existing value.
for (unsigned i = 0, e = Info.size(); i != e; ++i)
if (Info[i].first == KindID) {
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
-
+
for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
I != E; ++I)
if (I->first == KindID)
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.
if (Result.size() > 1)
array_pod_sort(Result.begin(), Result.end());