-//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
+//===- Metadata.cpp - Implement Metadata classes --------------------------===//
//
// The LLVM Compiler Infrastructure
//
#include "llvm/IR/Metadata.h"
#include "LLVMContextImpl.h"
+#include "MetadataImpl.h"
#include "SymbolTableListTraitsImpl.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/ConstantRange.h"
+#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
Metadata *MD) {
MD = canonicalizeMetadataForValue(Context, MD);
auto &Store = Context.pImpl->MetadataAsValues;
- auto I = Store.find(MD);
- return I == Store.end() ? nullptr : I->second;
+ return Store.lookup(MD);
}
void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
unsigned MDNode::countUnresolvedOperands() {
assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted");
- for (const auto &Op : operands())
- NumUnresolved += unsigned(isOperandUnresolved(Op));
+ NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved);
return NumUnresolved;
}
}
}
+static bool hasSelfReference(MDNode *N) {
+ for (Metadata *MD : N->operands())
+ if (MD == N)
+ return true;
+ return false;
+}
+
+MDNode *MDNode::replaceWithPermanentImpl() {
+ if (hasSelfReference(this))
+ return replaceWithDistinctImpl();
+ return replaceWithUniquedImpl();
+}
+
MDNode *MDNode::replaceWithUniquedImpl() {
// Try to uniquify in place.
MDNode *UniquedNode = uniquify();
+
if (UniquedNode == this) {
makeUniqued();
return this;
setHash(MDTupleInfo::KeyTy::calculateHash(this));
}
-void GenericDebugNode::recalculateHash() {
- setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
-}
-
void MDNode::dropAllReferences() {
for (unsigned I = 0, E = NumOperands; I != E; ++I)
setOperand(I, nullptr);
}
}
-template <class T, class InfoT>
-static T *getUniqued(DenseSet<T *, InfoT> &Store,
- const typename InfoT::KeyTy &Key) {
- auto I = Store.find_as(Key);
- return I == Store.end() ? nullptr : *I;
-}
-
template <class T, class InfoT>
static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
if (T *U = getUniqued(Store, N))
};
MDNode *MDNode::uniquify() {
+ assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
+
// Try to insert into uniquing store.
switch (getMetadataID()) {
default:
}
}
-template <class T, class StoreT>
-T *MDNode::storeImpl(T *N, StorageType Storage, StoreT &Store) {
- switch (Storage) {
- case Uniqued:
- Store.insert(N);
- break;
- case Distinct:
- N->storeDistinctInContext();
- break;
- case Temporary:
- break;
- }
- return N;
-}
-
MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
StorageType Storage, bool ShouldCreate) {
unsigned Hash = 0;
Storage, Context.pImpl->MDTuples);
}
-MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
- unsigned Column, ArrayRef<Metadata *> MDs)
- : MDNode(C, MDLocationKind, Storage, MDs) {
- assert((MDs.size() == 1 || MDs.size() == 2) &&
- "Expected a scope and optional inlined-at");
-
- // Set line and column.
- assert(Line < (1u << 24) && "Expected 24-bit line");
- assert(Column < (1u << 16) && "Expected 16-bit column");
-
- SubclassData32 = Line;
- SubclassData16 = Column;
-}
-
-static void adjustLine(unsigned &Line) {
- // Set to unknown on overflow. Still use 24 bits for now.
- if (Line >= (1u << 24))
- Line = 0;
-}
-
-static void adjustColumn(unsigned &Column) {
- // Set to unknown on overflow. We only have 16 bits to play with here.
- if (Column >= (1u << 16))
- Column = 0;
-}
-
-MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
- unsigned Column, Metadata *Scope,
- Metadata *InlinedAt, StorageType Storage,
- bool ShouldCreate) {
- // Fixup line/column.
- adjustLine(Line);
- adjustColumn(Column);
-
- if (Storage == Uniqued) {
- if (auto *N = getUniqued(
- Context.pImpl->MDLocations,
- MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
- return N;
- if (!ShouldCreate)
- return nullptr;
- } else {
- assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
- }
-
- SmallVector<Metadata *, 2> Ops;
- Ops.push_back(Scope);
- if (InlinedAt)
- Ops.push_back(InlinedAt);
- return storeImpl(new (Ops.size())
- MDLocation(Context, Storage, Line, Column, Ops),
- Storage, Context.pImpl->MDLocations);
-}
-
-GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
- MDString *Header,
- ArrayRef<Metadata *> DwarfOps,
- StorageType Storage,
- bool ShouldCreate) {
- // Canonicalize empty string to a nullptr.
- if (Header && Header->getString().empty())
- Header = nullptr;
-
- unsigned Hash = 0;
- if (Storage == Uniqued) {
- GenericDebugNodeInfo::KeyTy Key(Tag, Header, DwarfOps);
- if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
- return N;
- if (!ShouldCreate)
- return nullptr;
- Hash = Key.getHash();
- } else {
- assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
- }
-
- Metadata *PreOps[] = {Header};
- return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
- Context, Storage, Hash, Tag, PreOps, DwarfOps),
- Storage, Context.pImpl->GenericDebugNodes);
-}
-
void MDNode::deleteTemporary(MDNode *N) {
assert(N->isTemporary() && "Expected temporary node");
N->replaceAllUsesWith(nullptr);
if (!B)
return A;
- SmallVector<Metadata *, 4> MDs(A->getNumOperands() + B->getNumOperands());
-
- unsigned j = 0;
- for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
- MDs[j++] = A->getOperand(i);
- for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
- MDs[j++] = B->getOperand(i);
+ SmallVector<Metadata *, 4> MDs;
+ MDs.reserve(A->getNumOperands() + B->getNumOperands());
+ MDs.append(A->op_begin(), A->op_end());
+ MDs.append(B->op_begin(), B->op_end());
// FIXME: This preserves long-standing behaviour, but is it really the right
// behaviour? Or was that an unintended side-effect of node uniquing?
return nullptr;
SmallVector<Metadata *, 4> MDs;
- for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
- Metadata *MD = A->getOperand(i);
- for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
- if (MD == B->getOperand(j)) {
- MDs.push_back(MD);
- break;
- }
- }
+ for (Metadata *MD : A->operands())
+ if (std::find(B->op_begin(), B->op_end(), MD) != B->op_end())
+ MDs.push_back(MD);
+
+ // FIXME: This preserves long-standing behaviour, but is it really the right
+ // behaviour? Or was that an unintended side-effect of node uniquing?
+ return getOrSelfReference(A->getContext(), MDs);
+}
+
+MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
+ if (!A || !B)
+ return nullptr;
+
+ SmallVector<Metadata *, 4> MDs(B->op_begin(), B->op_end());
+ for (Metadata *MD : A->operands())
+ if (std::find(B->op_begin(), B->op_end(), MD) == B->op_end())
+ MDs.push_back(MD);
// FIXME: This preserves long-standing behaviour, but is it really the right
// behaviour? Or was that an unintended side-effect of node uniquing?