2009-07-28 21:49:47 +00:00
|
|
|
//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
|
|
|
|
//
|
|
|
|
// 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 Metadata classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Metadata.h"
|
2009-12-28 08:24:16 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2009-07-31 21:35:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2009-07-29 00:33:07 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-09-16 18:09:00 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2009-10-22 19:36:54 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2009-07-29 17:16:17 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
2009-12-28 08:24:16 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-28 21:49:47 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-31 21:35:40 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// MDString implementation.
|
2009-07-31 21:35:40 +00:00
|
|
|
//
|
2009-12-28 08:30:43 +00:00
|
|
|
|
|
|
|
MDString::MDString(LLVMContext &C, StringRef S)
|
|
|
|
: MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
|
|
|
|
|
2009-10-22 00:10:15 +00:00
|
|
|
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
|
2009-07-31 21:35:40 +00:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
StringMapEntry<MDString *> &Entry =
|
|
|
|
pImpl->MDStringCache.GetOrCreateValue(Str);
|
|
|
|
MDString *&S = Entry.getValue();
|
2009-11-26 22:54:26 +00:00
|
|
|
if (!S) S = new MDString(Context, Entry.getKey());
|
|
|
|
return S;
|
2009-07-31 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-11-12 00:50:58 +00:00
|
|
|
MDString *MDString::get(LLVMContext &Context, const char *Str) {
|
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
StringMapEntry<MDString *> &Entry =
|
|
|
|
pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
|
|
|
|
MDString *&S = Entry.getValue();
|
2009-11-27 19:57:53 +00:00
|
|
|
if (!S) S = new MDString(Context, Entry.getKey());
|
2009-11-26 22:54:26 +00:00
|
|
|
return S;
|
2009-11-12 00:50:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MDNodeElement implementation.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Use CallbackVH to hold MDNode elements.
|
|
|
|
namespace llvm {
|
|
|
|
class MDNodeElement : public CallbackVH {
|
|
|
|
MDNode *Parent;
|
|
|
|
public:
|
|
|
|
MDNodeElement() {}
|
|
|
|
MDNodeElement(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
|
|
|
|
~MDNodeElement() {}
|
|
|
|
|
2009-12-28 09:10:16 +00:00
|
|
|
void set(Value *V, MDNode *P) {
|
|
|
|
setValPtr(V);
|
|
|
|
Parent = P;
|
|
|
|
}
|
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
virtual void deleted();
|
|
|
|
virtual void allUsesReplacedWith(Value *NV);
|
|
|
|
};
|
|
|
|
} // end namespace llvm.
|
|
|
|
|
|
|
|
|
|
|
|
void MDNodeElement::deleted() {
|
2009-12-28 09:32:10 +00:00
|
|
|
Parent->replaceElement(this, 0);
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MDNodeElement::allUsesReplacedWith(Value *NV) {
|
2009-12-28 09:32:10 +00:00
|
|
|
Parent->replaceElement(this, NV);
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-28 21:49:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// MDNode implementation.
|
2009-07-28 21:49:47 +00:00
|
|
|
//
|
2009-12-28 07:41:54 +00:00
|
|
|
|
2009-12-28 09:12:35 +00:00
|
|
|
/// ~MDNode - Destroy MDNode.
|
|
|
|
MDNode::~MDNode() {
|
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
|
|
|
delete [] Operands;
|
|
|
|
Operands = NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-16 02:52:09 +00:00
|
|
|
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
|
2009-12-18 20:09:14 +00:00
|
|
|
bool isFunctionLocal)
|
2009-08-13 21:58:54 +00:00
|
|
|
: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
|
2009-12-28 08:48:12 +00:00
|
|
|
NumOperands = NumVals;
|
|
|
|
Operands = new MDNodeElement[NumOperands];
|
2009-12-28 09:12:35 +00:00
|
|
|
|
2009-10-21 23:57:35 +00:00
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
2009-12-28 09:12:35 +00:00
|
|
|
Operands[i].set(Vals[i], this);
|
2009-12-28 08:48:12 +00:00
|
|
|
|
2009-12-18 20:09:14 +00:00
|
|
|
if (isFunctionLocal)
|
2009-12-29 02:14:09 +00:00
|
|
|
setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
|
2009-07-28 21:49:47 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 02:52:09 +00:00
|
|
|
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
|
2009-12-18 20:09:14 +00:00
|
|
|
bool isFunctionLocal) {
|
2009-09-03 01:39:20 +00:00
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
|
|
|
ID.AddPointer(Vals[i]);
|
|
|
|
|
|
|
|
void *InsertPoint;
|
2009-11-26 22:54:26 +00:00
|
|
|
MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
2009-09-03 01:39:20 +00:00
|
|
|
if (!N) {
|
2009-10-19 07:10:59 +00:00
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
2009-12-18 20:09:14 +00:00
|
|
|
N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
|
2009-10-19 07:10:59 +00:00
|
|
|
pImpl->MDNodeSet.InsertNode(N, InsertPoint);
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
return N;
|
2009-07-31 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 09:12:35 +00:00
|
|
|
void MDNode::Profile(FoldingSetNodeID &ID) const {
|
|
|
|
for (unsigned i = 0, e = getNumElements(); i != e; ++i)
|
|
|
|
ID.AddPointer(getElement(i));
|
2009-12-28 19:49:00 +00:00
|
|
|
// HASH TABLE COLLISIONS?
|
|
|
|
// DO NOT REINSERT AFTER AN OPERAND DROPS TO NULL!
|
2009-08-03 22:51:10 +00:00
|
|
|
}
|
2009-08-11 18:01:24 +00:00
|
|
|
|
2009-12-28 09:12:35 +00:00
|
|
|
|
2009-12-28 07:41:54 +00:00
|
|
|
/// getElement - Return specified element.
|
|
|
|
Value *MDNode::getElement(unsigned i) const {
|
|
|
|
assert(i < getNumElements() && "Invalid element number!");
|
2009-12-28 08:48:12 +00:00
|
|
|
return Operands[i];
|
2009-12-28 07:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
// Replace value from this node's element list.
|
2009-12-28 09:32:10 +00:00
|
|
|
void MDNode::replaceElement(MDNodeElement *Op, Value *To) {
|
|
|
|
Value *From = *Op;
|
|
|
|
|
|
|
|
if (From == To)
|
2009-09-03 01:39:20 +00:00
|
|
|
return;
|
|
|
|
|
2009-12-28 09:24:53 +00:00
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
|
|
|
|
// Remove "this" from the context map. FoldingSet doesn't have to reprofile
|
|
|
|
// this node to remove it, so we don't care what state the operands are in.
|
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
2009-12-28 09:32:10 +00:00
|
|
|
|
|
|
|
// Update the operand.
|
|
|
|
Op->set(To, this);
|
2009-09-03 01:39:20 +00:00
|
|
|
|
|
|
|
// Insert updated "this" into the context's folding node set.
|
|
|
|
// If a node with same element list already exist then before inserting
|
|
|
|
// updated "this" into the folding node set, replace all uses of existing
|
|
|
|
// node with updated "this" node.
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
Profile(ID);
|
|
|
|
void *InsertPoint;
|
|
|
|
MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->replaceAllUsesWith(this);
|
|
|
|
delete N;
|
2009-12-28 09:24:53 +00:00
|
|
|
N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
assert(N == 0 && "shouldn't be in the map now!"); (void)N;
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 09:24:53 +00:00
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
|
|
|
pImpl->MDNodeSet.InsertNode(this, InsertPoint);
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 00:33:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// NamedMDNode implementation.
|
2009-07-29 00:33:07 +00:00
|
|
|
//
|
2009-12-28 08:07:14 +00:00
|
|
|
static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
|
|
|
|
return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
|
|
|
|
}
|
|
|
|
|
2009-08-13 21:58:54 +00:00
|
|
|
NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
|
2009-10-19 07:10:59 +00:00
|
|
|
MetadataBase *const *MDs,
|
2009-07-29 21:58:56 +00:00
|
|
|
unsigned NumMDs, Module *ParentModule)
|
2009-08-13 21:58:54 +00:00
|
|
|
: MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
|
2009-07-29 17:16:17 +00:00
|
|
|
setName(N);
|
2009-12-28 08:07:14 +00:00
|
|
|
|
|
|
|
Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
|
|
|
|
|
|
|
|
SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
|
2009-10-21 23:57:35 +00:00
|
|
|
for (unsigned i = 0; i != NumMDs; ++i)
|
2009-10-22 18:25:28 +00:00
|
|
|
Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
|
2009-10-21 23:57:35 +00:00
|
|
|
|
2009-07-29 21:58:56 +00:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getNamedMDList().push_back(this);
|
2009-07-29 00:33:07 +00:00
|
|
|
}
|
2009-08-03 06:19:01 +00:00
|
|
|
|
2009-08-11 18:01:24 +00:00
|
|
|
NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
|
2009-10-19 07:10:59 +00:00
|
|
|
assert(NMD && "Invalid source NamedMDNode!");
|
2009-08-11 18:01:24 +00:00
|
|
|
SmallVector<MetadataBase *, 4> Elems;
|
2009-12-28 08:07:14 +00:00
|
|
|
Elems.reserve(NMD->getNumElements());
|
|
|
|
|
2009-08-11 18:01:24 +00:00
|
|
|
for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
|
|
|
|
Elems.push_back(NMD->getElement(i));
|
2009-08-13 21:58:54 +00:00
|
|
|
return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
|
|
|
|
Elems.data(), Elems.size(), M);
|
2009-08-11 18:01:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 08:07:14 +00:00
|
|
|
NamedMDNode::~NamedMDNode() {
|
|
|
|
dropAllReferences();
|
|
|
|
delete &getNMDOps(Operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getNumElements - Return number of NamedMDNode elements.
|
|
|
|
unsigned NamedMDNode::getNumElements() const {
|
|
|
|
return (unsigned)getNMDOps(Operands).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getElement - Return specified element.
|
|
|
|
MetadataBase *NamedMDNode::getElement(unsigned i) const {
|
|
|
|
assert(i < getNumElements() && "Invalid element number!");
|
|
|
|
return getNMDOps(Operands)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addElement - Add metadata element.
|
|
|
|
void NamedMDNode::addElement(MetadataBase *M) {
|
|
|
|
getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
|
|
|
|
}
|
|
|
|
|
2009-08-03 06:19:01 +00:00
|
|
|
/// eraseFromParent - Drop all references and remove the node from parent
|
|
|
|
/// module.
|
|
|
|
void NamedMDNode::eraseFromParent() {
|
|
|
|
getParent()->getNamedMDList().erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dropAllReferences - Remove all uses and clear node vector.
|
|
|
|
void NamedMDNode::dropAllReferences() {
|
2009-12-28 08:07:14 +00:00
|
|
|
getNMDOps(Operands).clear();
|
2009-08-03 06:19:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 18:09:00 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-22 19:36:54 +00:00
|
|
|
// MetadataContextImpl implementation.
|
2009-09-16 18:09:00 +00:00
|
|
|
//
|
2009-10-22 19:36:54 +00:00
|
|
|
namespace llvm {
|
|
|
|
class MetadataContextImpl {
|
|
|
|
public:
|
|
|
|
typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
|
|
|
|
typedef SmallVector<MDPairTy, 2> MDMapTy;
|
|
|
|
typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
|
|
|
|
friend class BitcodeReader;
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// MetadataStore - Collection of metadata used in this context.
|
|
|
|
MDStoreTy MetadataStore;
|
|
|
|
|
|
|
|
/// MDHandlerNames - Map to hold metadata handler names.
|
|
|
|
StringMap<unsigned> MDHandlerNames;
|
|
|
|
|
|
|
|
public:
|
2009-12-28 23:41:32 +00:00
|
|
|
// Name <-> ID mapping methods.
|
2009-12-28 20:45:51 +00:00
|
|
|
unsigned getMDKindID(StringRef Name);
|
2009-12-28 23:41:32 +00:00
|
|
|
void getMDKindNames(SmallVectorImpl<StringRef> &) const;
|
2009-10-22 19:36:54 +00:00
|
|
|
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// Instruction metadata methods.
|
|
|
|
MDNode *getMetadata(const Instruction *Inst, unsigned Kind);
|
|
|
|
void getAllMetadata(const Instruction *Inst,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const;
|
|
|
|
|
|
|
|
void setMetadata(Instruction *Inst, unsigned Kind, MDNode *Node);
|
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
/// removeAllMetadata - Remove all metadata attached with an instruction.
|
|
|
|
void removeAllMetadata(Instruction *Inst);
|
2009-12-28 23:41:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
/// copyMD - If metadata is attached with Instruction In1 then attach
|
|
|
|
/// the same metadata to In2.
|
|
|
|
void copyMD(Instruction *In1, Instruction *In2);
|
2009-12-28 23:41:32 +00:00
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
|
|
|
|
/// ValueIsDeleted - This handler is used to update metadata store
|
|
|
|
/// when a value is deleted.
|
|
|
|
void ValueIsDeleted(const Value *) {}
|
|
|
|
void ValueIsDeleted(Instruction *Inst) {
|
|
|
|
removeAllMetadata(Inst);
|
|
|
|
}
|
|
|
|
void ValueIsRAUWd(Value *V1, Value *V2);
|
|
|
|
|
|
|
|
/// ValueIsCloned - This handler is used to update metadata store
|
|
|
|
/// when In1 is cloned to create In2.
|
|
|
|
void ValueIsCloned(const Instruction *In1, Instruction *In2);
|
|
|
|
};
|
|
|
|
}
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-12-28 20:45:51 +00:00
|
|
|
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
|
|
|
|
unsigned MetadataContextImpl::getMDKindID(StringRef Name) {
|
|
|
|
unsigned &Entry = MDHandlerNames[Name];
|
2009-09-16 18:09:00 +00:00
|
|
|
|
2009-12-28 20:45:51 +00:00
|
|
|
// If this is new, assign it its ID.
|
|
|
|
if (Entry == 0) Entry = MDHandlerNames.size();
|
|
|
|
return Entry;
|
2009-09-16 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
|
|
|
void MetadataContextImpl::
|
|
|
|
getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
|
|
|
|
Names.resize(MDHandlerNames.size()+1);
|
|
|
|
Names[0] = "";
|
|
|
|
for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
|
|
|
|
E = MDHandlerNames.end(); I != E; ++I)
|
|
|
|
// MD Handlers are numbered from 1.
|
|
|
|
Names[I->second] = I->first();
|
|
|
|
}
|
2009-09-29 20:30:57 +00:00
|
|
|
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
/// getMetadata - Get the metadata of given kind attached to an Instruction.
|
|
|
|
/// If the metadata is not found then return 0.
|
|
|
|
MDNode *MetadataContextImpl::
|
|
|
|
getMetadata(const Instruction *Inst, unsigned MDKind) {
|
|
|
|
MDMapTy &Info = MetadataStore[Inst];
|
|
|
|
assert(Inst->hasMetadata() && !Info.empty() && "Shouldn't have called this");
|
|
|
|
|
|
|
|
for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
|
|
|
|
if (I->first == MDKind)
|
|
|
|
return I->second;
|
|
|
|
return 0;
|
2009-09-16 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
/// getAllMetadata - Get all of the metadata attached to an Instruction.
|
|
|
|
void MetadataContextImpl::
|
|
|
|
getAllMetadata(const Instruction *Inst,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &Result) const {
|
|
|
|
assert(Inst->hasMetadata() && MetadataStore.find(Inst) != MetadataStore.end()
|
|
|
|
&& "Shouldn't have called this");
|
|
|
|
const MDMapTy &Info = MetadataStore.find(Inst)->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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind,
|
|
|
|
MDNode *Node) {
|
|
|
|
// Handle the case when we're adding/updating metadata on an instruction.
|
|
|
|
if (Node) {
|
|
|
|
MDMapTy &Info = MetadataStore[Inst];
|
|
|
|
assert(!Info.empty() == Inst->HasMetadata && "HasMetadata bit is wonked");
|
|
|
|
if (Info.empty()) {
|
|
|
|
Inst->HasMetadata = true;
|
|
|
|
} else {
|
|
|
|
// Handle replacement of an existing value.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
|
|
|
if (Info[i].first == Kind) {
|
|
|
|
Info[i].second = Node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No replacement, just add it to the list.
|
|
|
|
Info.push_back(std::make_pair(Kind, Node));
|
2009-09-29 20:42:25 +00:00
|
|
|
return;
|
2009-12-28 23:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we're removing metadata from an instruction.
|
|
|
|
assert(Inst->HasMetadata && MetadataStore.count(Inst) &&
|
|
|
|
"HasMetadata bit out of date!");
|
|
|
|
MDMapTy &Info = MetadataStore[Inst];
|
2009-09-29 20:42:25 +00:00
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
// Common case is removing the only entry.
|
|
|
|
if (Info.size() == 1 && Info[0].first == Kind) {
|
|
|
|
MetadataStore.erase(Inst);
|
|
|
|
Inst->HasMetadata = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle replacement of an existing value.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i)
|
|
|
|
if (Info[i].first == Kind) {
|
|
|
|
Info[i] = Info.back();
|
|
|
|
Info.pop_back();
|
|
|
|
assert(!Info.empty() && "Removing last entry should be handled above");
|
2009-09-29 20:42:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-28 23:41:32 +00:00
|
|
|
// Otherwise, removing an entry that doesn't exist on the instruction.
|
2009-09-29 20:42:25 +00:00
|
|
|
}
|
2009-11-26 22:54:26 +00:00
|
|
|
|
2009-10-21 17:33:41 +00:00
|
|
|
/// removeAllMetadata - Remove all metadata attached with an instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
|
2009-10-21 17:33:41 +00:00
|
|
|
MetadataStore.erase(Inst);
|
|
|
|
Inst->HasMetadata = false;
|
2009-09-29 20:42:25 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 23:41:32 +00:00
|
|
|
|
2009-10-14 17:02:49 +00:00
|
|
|
/// copyMD - If metadata is attached with Instruction In1 then attach
|
|
|
|
/// the same metadata to In2.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
|
2009-10-19 07:10:59 +00:00
|
|
|
assert(In1 && In2 && "Invalid instruction!");
|
2009-10-21 17:33:41 +00:00
|
|
|
MDMapTy &In1Info = MetadataStore[In1];
|
|
|
|
if (In1Info.empty())
|
2009-10-14 17:02:49 +00:00
|
|
|
return;
|
2009-12-28 23:41:32 +00:00
|
|
|
|
2009-10-14 17:02:49 +00:00
|
|
|
for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
|
2009-12-28 23:41:32 +00:00
|
|
|
In2->setMetadata(I->first, I->second);
|
2009-09-18 19:26:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 18:32:25 +00:00
|
|
|
/// ValueIsCloned - This handler is used to update metadata store
|
|
|
|
/// when In1 is cloned to create In2.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
|
|
|
|
Instruction *In2) {
|
2009-09-23 18:32:25 +00:00
|
|
|
// Find Metadata handles for In1.
|
|
|
|
MDStoreTy::iterator I = MetadataStore.find(In1);
|
2009-10-19 07:10:59 +00:00
|
|
|
assert(I != MetadataStore.end() && "Invalid custom metadata info!");
|
2009-09-23 18:32:25 +00:00
|
|
|
|
2009-12-28 19:49:00 +00:00
|
|
|
// FIXME: Give all metadata handlers a chance to adjust.
|
2009-09-23 18:32:25 +00:00
|
|
|
MDMapTy &In1Info = I->second;
|
|
|
|
for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
|
2009-12-28 23:41:32 +00:00
|
|
|
In2->setMetadata(I->first, I->second);
|
2009-09-23 18:32:25 +00:00
|
|
|
}
|
2009-10-13 17:00:54 +00:00
|
|
|
|
|
|
|
/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
|
|
|
|
/// V2.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
|
2009-10-13 17:00:54 +00:00
|
|
|
Instruction *I1 = dyn_cast<Instruction>(V1);
|
|
|
|
Instruction *I2 = dyn_cast<Instruction>(V2);
|
|
|
|
if (!I1 || !I2)
|
|
|
|
return;
|
|
|
|
|
2009-12-28 19:49:00 +00:00
|
|
|
// FIXME: Give custom handlers a chance to override this.
|
2009-10-13 17:00:54 +00:00
|
|
|
ValueIsCloned(I1, I2);
|
|
|
|
}
|
2009-10-14 17:02:49 +00:00
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MetadataContext implementation.
|
|
|
|
//
|
2009-12-28 19:49:00 +00:00
|
|
|
MetadataContext::MetadataContext() : pImpl(new MetadataContextImpl()) { }
|
2009-10-22 19:36:54 +00:00
|
|
|
MetadataContext::~MetadataContext() { delete pImpl; }
|
|
|
|
|
|
|
|
/// isValidName - Return true if Name is a valid custom metadata handler name.
|
|
|
|
bool MetadataContext::isValidName(StringRef MDName) {
|
|
|
|
if (MDName.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isalpha(MDName[0]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
|
|
|
|
++I) {
|
|
|
|
if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-28 20:45:51 +00:00
|
|
|
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
|
|
|
|
unsigned MetadataContext::getMDKindID(StringRef Name) const {
|
|
|
|
return pImpl->getMDKindID(Name);
|
2009-10-22 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// copyMD - If metadata is attached with Instruction In1 then attach
|
|
|
|
/// the same metadata to In2.
|
|
|
|
void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
|
|
|
|
pImpl->copyMD(In1, In2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
2009-12-28 20:10:43 +00:00
|
|
|
void MetadataContext::getMDKindNames(SmallVectorImpl<StringRef> &N) const {
|
|
|
|
pImpl->getMDKindNames(N);
|
2009-10-22 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ValueIsDeleted - This handler is used to update metadata store
|
|
|
|
/// when a value is deleted.
|
|
|
|
void MetadataContext::ValueIsDeleted(Instruction *Inst) {
|
|
|
|
pImpl->ValueIsDeleted(Inst);
|
|
|
|
}
|
|
|
|
void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
|
|
|
|
pImpl->ValueIsRAUWd(V1, V2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ValueIsCloned - This handler is used to update metadata store
|
|
|
|
/// when In1 is cloned to create In2.
|
|
|
|
void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
|
|
|
|
pImpl->ValueIsCloned(In1, In2);
|
|
|
|
}
|
2009-12-28 23:41:32 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Instruction Metadata method implementations.
|
|
|
|
//
|
|
|
|
|
|
|
|
void Instruction::setMetadata(const char *Kind, MDNode *Node) {
|
|
|
|
if (Node == 0 && !hasMetadata()) return;
|
|
|
|
setMetadata(getContext().getMetadata().getMDKindID(Kind), Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *Instruction::getMetadataImpl(const char *Kind) const {
|
|
|
|
return getMetadataImpl(getContext().getMetadata().getMDKindID(Kind));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setMetadata - Set the metadata of of the specified kind to the specified
|
|
|
|
/// node. This updates/replaces metadata if already present, or removes it if
|
|
|
|
/// Node is null.
|
|
|
|
void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
|
|
|
|
if (Node == 0 && !hasMetadata()) return;
|
|
|
|
|
|
|
|
getContext().getMetadata().pImpl->setMetadata(this, KindID, Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
|
|
|
|
return getContext().getMetadata().pImpl->getMetadata(this, KindID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
|
|
|
|
MDNode*> > &Result)const {
|
|
|
|
getContext().getMetadata().pImpl->getAllMetadata(this, Result);
|
|
|
|
}
|
|
|
|
|