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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-31 21:35:40 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2009-07-28 21:49:47 +00:00
|
|
|
#include "llvm/Metadata.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-07-28 21:49:47 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-03 22:51:10 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-19 07:10:59 +00:00
|
|
|
// MetadataBase implementation.
|
2009-08-03 22:51:10 +00:00
|
|
|
//
|
|
|
|
|
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-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-10-19 07:10:59 +00:00
|
|
|
if (S) return S;
|
|
|
|
|
2009-10-22 00:22:05 +00:00
|
|
|
return S =
|
2009-10-22 18:25:28 +00:00
|
|
|
new MDString(Context, Entry.getKey());
|
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();
|
|
|
|
if (S) return S;
|
|
|
|
|
|
|
|
return S =
|
|
|
|
new MDString(Context, Entry.getKey());
|
|
|
|
}
|
|
|
|
|
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-10-19 07:10:59 +00:00
|
|
|
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
|
2009-08-13 21:58:54 +00:00
|
|
|
: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
|
2009-10-21 21:25:09 +00:00
|
|
|
NodeSize = NumVals;
|
|
|
|
Node = new ElementVH[NodeSize];
|
|
|
|
ElementVH *Ptr = Node;
|
2009-10-21 23:57:35 +00:00
|
|
|
for (unsigned i = 0; i != NumVals; ++i)
|
2009-10-21 21:25:09 +00:00
|
|
|
*Ptr++ = ElementVH(Vals[i], this);
|
2009-07-28 21:49:47 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
void MDNode::Profile(FoldingSetNodeID &ID) const {
|
2009-10-21 21:25:09 +00:00
|
|
|
for (unsigned i = 0, e = getNumElements(); i != e; ++i)
|
|
|
|
ID.AddPointer(getElement(i));
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 21:35:40 +00:00
|
|
|
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
|
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-10-19 07:10:59 +00:00
|
|
|
MDNode *N;
|
|
|
|
{
|
|
|
|
N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
}
|
|
|
|
if (N) return N;
|
2009-09-09 17:30:04 +00:00
|
|
|
|
2009-10-19 07:10:59 +00:00
|
|
|
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.
|
|
|
|
N = new MDNode(Context, Vals, NumVals);
|
|
|
|
pImpl->MDNodeSet.InsertNode(N, InsertPoint);
|
2009-09-03 01:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return N;
|
2009-07-31 21:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 23:57:35 +00:00
|
|
|
/// ~MDNode - Destroy MDNode.
|
2009-08-03 22:51:10 +00:00
|
|
|
MDNode::~MDNode() {
|
2009-09-09 17:44:26 +00:00
|
|
|
{
|
|
|
|
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
|
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
|
|
|
}
|
2009-10-21 23:57:35 +00:00
|
|
|
delete [] Node;
|
|
|
|
Node = NULL;
|
2009-08-03 22:51:10 +00:00
|
|
|
}
|
2009-08-11 18:01:24 +00:00
|
|
|
|
2009-09-03 01:39:20 +00:00
|
|
|
// Replace value from this node's element list.
|
|
|
|
void MDNode::replaceElement(Value *From, Value *To) {
|
|
|
|
if (From == To || !getType())
|
|
|
|
return;
|
|
|
|
LLVMContext &Context = getType()->getContext();
|
|
|
|
LLVMContextImpl *pImpl = Context.pImpl;
|
|
|
|
|
|
|
|
// Find value. This is a linear search, do something if it consumes
|
|
|
|
// lot of time. It is possible that to have multiple instances of
|
|
|
|
// From in this MDNode's element list.
|
|
|
|
SmallVector<unsigned, 4> Indexes;
|
|
|
|
unsigned Index = 0;
|
2009-10-21 21:25:09 +00:00
|
|
|
for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
|
|
|
|
Value *V = getElement(i);
|
2009-09-03 01:39:20 +00:00
|
|
|
if (V && V == From)
|
|
|
|
Indexes.push_back(Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Indexes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove "this" from the context map.
|
2009-10-19 20:11:52 +00:00
|
|
|
pImpl->MDNodeSet.RemoveNode(this);
|
2009-09-03 01:39:20 +00:00
|
|
|
|
|
|
|
// Replace From element(s) in place.
|
|
|
|
for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
unsigned Index = *I;
|
|
|
|
Node[Index] = ElementVH(To, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
N = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-19 20:11:52 +00:00
|
|
|
N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
if (!N) {
|
|
|
|
// InsertPoint will have been set by the FindNodeOrInsertPos call.
|
|
|
|
N = this;
|
|
|
|
pImpl->MDNodeSet.InsertNode(N, 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-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-08-03 22:51:10 +00:00
|
|
|
|
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;
|
|
|
|
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-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() {
|
|
|
|
Node.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedMDNode::~NamedMDNode() {
|
|
|
|
dropAllReferences();
|
|
|
|
}
|
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.
|
|
|
|
void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
|
|
|
|
|
|
|
|
/// addMD - Attach the metadata of given kind to an Instruction.
|
|
|
|
void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
|
|
|
|
|
|
|
|
/// removeMD - Remove metadata of given kind attached with an instuction.
|
|
|
|
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);
|
|
|
|
|
|
|
|
/// getHandlerNames - Populate client supplied smallvector using custome
|
|
|
|
/// metadata name and ID.
|
|
|
|
void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
|
|
|
|
|
|
|
|
/// 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-09-29 20:42:25 +00:00
|
|
|
/// removeMD - Remove metadata of given kind attached with an instuction.
|
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-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-10-22 18:55:16 +00:00
|
|
|
getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &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-10-22 01:01:24 +00:00
|
|
|
getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
|
2009-10-26 17:09:00 +00:00
|
|
|
Names.resize(MDHandlerNames.size());
|
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.
|
|
|
|
Names[I->second - 1] = std::make_pair(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
|
|
|
|
|
|
|
// FIXME : Give all metadata handlers a chance to adjust.
|
|
|
|
|
|
|
|
MDMapTy &In1Info = I->second;
|
|
|
|
MDMapTy In2Info;
|
|
|
|
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;
|
|
|
|
|
|
|
|
// FIXME : Give custom handlers a chance to override this.
|
|
|
|
ValueIsCloned(I1, I2);
|
|
|
|
}
|
2009-10-14 17:02:49 +00:00
|
|
|
|
2009-10-22 19:36:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MetadataContext implementation.
|
|
|
|
//
|
|
|
|
MetadataContext::MetadataContext()
|
|
|
|
: pImpl(new MetadataContextImpl()) { }
|
|
|
|
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,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// removeMD - Remove metadata of given kind attached with an instuction.
|
|
|
|
void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
|
|
|
|
pImpl->removeMD(Kind, Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
void MetadataContext::
|
|
|
|
getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
|
|
|
|
pImpl->getHandlerNames(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|