#include "llvm/IR/LeakDetector.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueHandle.h"
+
using namespace llvm;
+Metadata::Metadata(LLVMContext &Context, unsigned ID)
+ : Value(Type::getMetadataTy(Context), ID) {}
+
//===----------------------------------------------------------------------===//
// MDString implementation.
//
: Value(Type::getMetadataTy(C), Value::MDStringVal) {}
MDString *MDString::get(LLVMContext &Context, StringRef Str) {
- LLVMContextImpl *pImpl = Context.pImpl;
- StringMapEntry<Value*> &Entry =
- pImpl->MDStringCache.GetOrCreateValue(Str);
- Value *&S = Entry.getValue();
- if (!S) S = new MDString(Context);
- S->setValueName(&Entry);
- return cast<MDString>(S);
+ auto &Store = Context.pImpl->MDStringCache;
+ auto I = Store.find(Str);
+ if (I != Store.end())
+ return &I->second;
+
+ auto *Entry =
+ StringMapEntry<MDString>::Create(Str, Store.getAllocator(), Context);
+ bool WasInserted = Store.insert(Entry);
+ (void)WasInserted;
+ assert(WasInserted && "Expected entry to be inserted");
+ return &Entry->second;
+}
+
+StringRef MDString::getString() const {
+ return StringMapEntry<MDString>::GetStringMapEntryFromValue(*this).first();
}
//===----------------------------------------------------------------------===//
replaceOperand(Op, Val);
}
-MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
-: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
+MDNode::MDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
+ : Metadata(C, Value::MDNodeVal) {
NumOperands = Vals.size();
if (isFunctionLocal)
// NamedMDNode implementation.
//
-static SmallVector<TrackingVH<Value>, 4> &getNMDOps(void *Operands) {
- return *(SmallVector<TrackingVH<Value>, 4> *)Operands;
+static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
+ return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
}
NamedMDNode::NamedMDNode(const Twine &N)
: Name(N.str()), Parent(nullptr),
- Operands(new SmallVector<TrackingVH<Value>, 4>()) {}
+ Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
NamedMDNode::~NamedMDNode() {
dropAllReferences();
return (unsigned)getNMDOps(Operands).size();
}
-Value *NamedMDNode::getOperand(unsigned i) const {
+MDNode *NamedMDNode::getOperand(unsigned i) const {
assert(i < getNumOperands() && "Invalid Operand number!");
return &*getNMDOps(Operands)[i];
}
-void NamedMDNode::addOperand(Value *V) {
- auto *M = cast<MDNode>(V);
+void NamedMDNode::addOperand(MDNode *M) {
assert(!M->isFunctionLocal() &&
"NamedMDNode operands must not be function-local!");
- getNMDOps(Operands).push_back(TrackingVH<Value>(M));
+ getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
}
void NamedMDNode::eraseFromParent() {
// Instruction Metadata method implementations.
//
-void Instruction::setMetadata(StringRef Kind, Value *MD) {
- if (!MD && !hasMetadata()) return;
- setMetadata(getContext().getMDKindID(Kind), MD);
+void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
+ if (!Node && !hasMetadata())
+ return;
+ setMetadata(getContext().getMDKindID(Kind), Node);
}
-Value *Instruction::getMetadataImpl(StringRef Kind) const {
+MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
return getMetadataImpl(getContext().getMDKindID(Kind));
}
-MDNode *Instruction::getMDNodeImpl(unsigned KindID) const {
- return cast_or_null<MDNode>(getMetadataImpl(KindID));
-}
-
-MDNode *Instruction::getMDNodeImpl(StringRef Kind) const {
- return cast_or_null<MDNode>(getMetadataImpl(Kind));
-}
-
void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
SmallSet<unsigned, 5> KnownSet;
KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
/// setMetadata - Set the metadata of of the specified kind to the specified
/// node. This updates/replaces metadata if already present, or removes it if
-/// MD is null.
-void Instruction::setMetadata(unsigned KindID, Value *MD) {
- if (!MD && !hasMetadata()) return;
-
- // For now, we only expect MDNodes here.
- MDNode *Node = cast_or_null<MDNode>(MD);
+/// Node is null.
+void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
+ if (!Node && !hasMetadata())
+ return;
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (KindID == LLVMContext::MD_dbg) {
setMetadata(LLVMContext::MD_noalias, N.NoAlias);
}
-Value *Instruction::getMetadataImpl(unsigned KindID) const {
+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 DbgLoc.getAsMDNode(getContext());
}
void Instruction::getAllMetadataImpl(
- SmallVectorImpl<std::pair<unsigned, Value *>> &Result) const {
+ SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Result.clear();
// Handle 'dbg' as a special case since it is not stored in the hash table.
}
void Instruction::getAllMetadataOtherThanDebugLocImpl(
- SmallVectorImpl<std::pair<unsigned, Value *>> &Result) const {
+ SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
Result.clear();
assert(hasMetadataHashEntry() &&
getContext().pImpl->MetadataStore.count(this) &&