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)
|
|
|
|
SubclassData |= 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:
|
|
|
|
/// registerMDKind - Register a new metadata kind and return its ID.
|
|
|
|
/// A metadata kind can be registered only once.
|
|
|
|
unsigned registerMDKind(StringRef Name);
|
|
|
|
|
|
|
|
/// getMDKind - Return metadata kind. If the requested metadata kind
|
|
|
|
/// is not registered then return 0.
|
|
|
|
unsigned getMDKind(StringRef Name) const;
|
|
|
|
|
|
|
|
/// getMD - Get the metadata of given kind attached to an Instruction.
|
|
|
|
/// If the metadata is not found then return 0.
|
|
|
|
MDNode *getMD(unsigned Kind, const Instruction *Inst);
|
|
|
|
|
|
|
|
/// getMDs - Get the metadata attached to an Instruction.
|
2009-12-28 08:14:54 +00:00
|
|
|
void getMDs(const Instruction *Inst,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
|
2009-10-22 19:36:54 +00:00
|
|
|
|
|
|
|
/// addMD - Attach the metadata of given kind to an Instruction.
|
|
|
|
void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
|
|
|
|
|
2009-11-26 23:19:05 +00:00
|
|
|
/// removeMD - Remove metadata of given kind attached with an instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void removeMD(unsigned Kind, Instruction *Inst);
|
|
|
|
|
|
|
|
/// removeAllMetadata - Remove all metadata attached with an instruction.
|
|
|
|
void removeAllMetadata(Instruction *Inst);
|
|
|
|
|
|
|
|
/// copyMD - If metadata is attached with Instruction In1 then attach
|
|
|
|
/// the same metadata to In2.
|
|
|
|
void copyMD(Instruction *In1, Instruction *In2);
|
|
|
|
|
2009-12-28 20:10:43 +00:00
|
|
|
/// getMDKindNames - Populate client-supplied smallvector using custom
|
2009-10-22 19:36:54 +00:00
|
|
|
/// metadata name and ID.
|
2009-12-28 20:10:43 +00:00
|
|
|
void getMDKindNames(SmallVectorImpl<StringRef> &) const;
|
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-10-20 22:50:27 +00:00
|
|
|
/// registerMDKind - Register a new metadata kind and return its ID.
|
2009-09-16 18:09:00 +00:00
|
|
|
/// A metadata kind can be registered only once.
|
2009-10-22 19:36:54 +00:00
|
|
|
unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
|
2009-09-28 21:14:55 +00:00
|
|
|
unsigned Count = MDHandlerNames.size();
|
2009-10-21 17:33:41 +00:00
|
|
|
assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
|
|
|
|
return MDHandlerNames[Name] = Count + 1;
|
2009-09-16 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getMDKind - Return metadata kind. If the requested metadata kind
|
|
|
|
/// is not registered then return 0.
|
2009-10-22 19:36:54 +00:00
|
|
|
unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
|
2009-10-21 21:57:13 +00:00
|
|
|
StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
|
2009-10-22 19:36:54 +00:00
|
|
|
if (I == MDHandlerNames.end())
|
2009-09-16 18:09:00 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return I->getValue();
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// addMD - Attach the metadata of given kind to an Instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
|
|
|
|
Instruction *Inst) {
|
2009-10-19 07:10:59 +00:00
|
|
|
assert(Node && "Invalid null MDNode");
|
2009-09-16 18:09:00 +00:00
|
|
|
Inst->HasMetadata = true;
|
2009-10-21 17:33:41 +00:00
|
|
|
MDMapTy &Info = MetadataStore[Inst];
|
|
|
|
if (Info.empty()) {
|
2009-09-16 18:09:00 +00:00
|
|
|
Info.push_back(std::make_pair(MDKind, Node));
|
|
|
|
MetadataStore.insert(std::make_pair(Inst, Info));
|
|
|
|
return;
|
|
|
|
}
|
2009-09-29 20:30:57 +00:00
|
|
|
|
|
|
|
// If there is an entry for this MDKind then replace it.
|
|
|
|
for (unsigned i = 0, e = Info.size(); i != e; ++i) {
|
|
|
|
MDPairTy &P = Info[i];
|
|
|
|
if (P.first == MDKind) {
|
|
|
|
Info[i] = std::make_pair(MDKind, Node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise add a new entry.
|
2009-09-16 18:09:00 +00:00
|
|
|
Info.push_back(std::make_pair(MDKind, Node));
|
|
|
|
}
|
|
|
|
|
2009-11-26 23:19:05 +00:00
|
|
|
/// removeMD - Remove metadata of given kind attached with an instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
|
2009-09-29 20:42:25 +00:00
|
|
|
MDStoreTy::iterator I = MetadataStore.find(Inst);
|
|
|
|
if (I == MetadataStore.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MDMapTy &Info = I->second;
|
|
|
|
for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
|
|
|
|
MDPairTy &P = *MI;
|
|
|
|
if (P.first == Kind) {
|
|
|
|
Info.erase(MI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-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;
|
|
|
|
|
|
|
|
for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
|
2009-10-22 18:25:28 +00:00
|
|
|
addMD(I->first, I->second, In2);
|
2009-10-14 17:02:49 +00:00
|
|
|
}
|
2009-09-29 20:42:25 +00:00
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// getMD - Get the metadata of given kind attached to an Instruction.
|
2009-09-16 18:09:00 +00:00
|
|
|
/// If the metadata is not found then return 0.
|
2009-10-22 19:36:54 +00:00
|
|
|
MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
|
2009-10-21 17:33:41 +00:00
|
|
|
MDMapTy &Info = MetadataStore[Inst];
|
|
|
|
if (Info.empty())
|
2009-09-29 20:30:57 +00:00
|
|
|
return NULL;
|
2009-10-21 17:33:41 +00:00
|
|
|
|
2009-09-16 18:09:00 +00:00
|
|
|
for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
|
|
|
|
if (I->first == MDKind)
|
2009-10-22 18:25:28 +00:00
|
|
|
return I->second;
|
2009-09-29 20:30:57 +00:00
|
|
|
return NULL;
|
2009-09-16 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 22:50:27 +00:00
|
|
|
/// getMDs - Get the metadata attached to an Instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::
|
2009-12-28 08:14:54 +00:00
|
|
|
getMDs(const Instruction *Inst,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
|
2009-11-10 01:02:17 +00:00
|
|
|
MDStoreTy::const_iterator I = MetadataStore.find(Inst);
|
2009-09-16 20:21:17 +00:00
|
|
|
if (I == MetadataStore.end())
|
2009-10-22 18:55:16 +00:00
|
|
|
return;
|
2009-10-26 17:09:00 +00:00
|
|
|
MDs.resize(I->second.size());
|
2009-11-10 01:02:17 +00:00
|
|
|
for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
|
2009-10-22 18:55:16 +00:00
|
|
|
MI != ME; ++MI)
|
2009-10-26 17:09:00 +00:00
|
|
|
// MD kinds are numbered from 1.
|
|
|
|
MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
|
2009-09-16 20:21:17 +00:00
|
|
|
}
|
|
|
|
|
2009-10-22 17:40:37 +00:00
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContextImpl::
|
2009-12-28 20:10:43 +00:00
|
|
|
getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
|
|
|
|
Names.resize(MDHandlerNames.size()+1);
|
|
|
|
Names[0] = "";
|
2009-10-22 01:01:24 +00:00
|
|
|
for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
|
|
|
|
E = MDHandlerNames.end(); I != E; ++I)
|
2009-10-26 17:09:00 +00:00
|
|
|
// MD Handlers are numbered from 1.
|
2009-12-28 20:10:43 +00:00
|
|
|
Names[I->second] = I->first();
|
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-10-22 18:25:28 +00:00
|
|
|
addMD(I->first, I->second, In2);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// registerMDKind - Register a new metadata kind and return its ID.
|
|
|
|
/// A metadata kind can be registered only once.
|
|
|
|
unsigned MetadataContext::registerMDKind(StringRef Name) {
|
|
|
|
assert(isValidName(Name) && "Invalid custome metadata name!");
|
|
|
|
return pImpl->registerMDKind(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMDKind - Return metadata kind. If the requested metadata kind
|
|
|
|
/// is not registered then return 0.
|
|
|
|
unsigned MetadataContext::getMDKind(StringRef Name) const {
|
|
|
|
return pImpl->getMDKind(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMD - Get the metadata of given kind attached to an Instruction.
|
|
|
|
/// If the metadata is not found then return 0.
|
|
|
|
MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
|
|
|
|
return pImpl->getMD(Kind, Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMDs - Get the metadata attached to an Instruction.
|
|
|
|
void MetadataContext::
|
|
|
|
getMDs(const Instruction *Inst,
|
2009-12-28 08:14:54 +00:00
|
|
|
SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
|
2009-10-22 19:36:54 +00:00
|
|
|
return pImpl->getMDs(Inst, MDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addMD - Attach the metadata of given kind to an Instruction.
|
|
|
|
void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
|
|
|
|
pImpl->addMD(Kind, Node, Inst);
|
|
|
|
}
|
2009-11-26 22:54:26 +00:00
|
|
|
|
2009-11-26 23:19:05 +00:00
|
|
|
/// removeMD - Remove metadata of given kind attached with an instruction.
|
2009-10-22 19:36:54 +00:00
|
|
|
void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
|
|
|
|
pImpl->removeMD(Kind, Inst);
|
|
|
|
}
|
2009-11-26 22:54:26 +00:00
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
/// removeAllMetadata - Remove all metadata attached with an instruction.
|
|
|
|
void MetadataContext::removeAllMetadata(Instruction *Inst) {
|
|
|
|
pImpl->removeAllMetadata(Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|