33 :
Value(Ty, MetadataAsValueVal), MD(MD) {
37 MetadataAsValue::~MetadataAsValue() {
61 if (!
N ||
N->getNumOperands() != 1)
64 if (!
N->getOperand(0))
68 if (
auto *C = dyn_cast<ConstantAsMetadata>(
N->getOperand(0)))
87 return Store.lookup(MD);
90 void MetadataAsValue::handleChangedMetadata(
Metadata *MD) {
96 Store.erase(this->MD);
101 auto *&Entry =
Store[MD];
113 void MetadataAsValue::track() {
118 void MetadataAsValue::untrack() {
123 void ReplaceableMetadataImpl::addRef(
void *Ref, OwnerTy Owner) {
125 UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
128 assert(WasInserted &&
"Expected to add a reference");
131 assert(NextIndex != 0 &&
"Unexpected overflow");
134 void ReplaceableMetadataImpl::dropRef(
void *Ref) {
135 bool WasErased = UseMap.erase(Ref);
137 assert(WasErased &&
"Expected to drop a reference");
140 void ReplaceableMetadataImpl::moveRef(
void *Ref,
void *New,
142 auto I = UseMap.find(Ref);
143 assert(
I != UseMap.end() &&
"Expected to move a reference");
144 auto OwnerAndIndex =
I->second;
146 bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
148 assert(WasInserted &&
"Expected to add a reference");
152 assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
153 "Reference without owner must be direct");
154 assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
155 "Reference without owner must be direct");
159 assert(!(MD && isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) &&
160 "Expected non-temp node");
166 typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
168 std::sort(Uses.begin(), Uses.end(), [](
const UseTy &L,
const UseTy &R) {
169 return L.second.second < R.second.second;
171 for (
const auto &Pair : Uses) {
174 if (!UseMap.count(Pair.first))
177 OwnerTy Owner = Pair.second.first;
184 UseMap.erase(Pair.first);
197 #define HANDLE_METADATA_LEAF(CLASS) \
198 case Metadata::CLASS##Kind: \
199 cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
201 #include "llvm/IR/Metadata.def"
206 assert(UseMap.empty() &&
"Expected all uses to be replaced");
219 typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
221 std::sort(Uses.begin(), Uses.end(), [](
const UseTy &L,
const UseTy &R) {
222 return L.second.second < R.second.second;
225 for (
const auto &Pair : Uses) {
226 auto Owner = Pair.second.first;
236 if (OwnerMD->isResolved())
238 OwnerMD->decrementUnresolvedOperandCount();
243 assert(V &&
"Expected value");
244 if (
auto *
A = dyn_cast<Argument>(V))
245 return A->getParent();
247 return BB->getParent();
252 assert(V &&
"Unexpected null Value");
257 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
258 "Expected constant or function-local value");
260 "Expected this to be the only metadata use");
262 if (
auto *
C = dyn_cast<Constant>(V))
272 assert(V &&
"Unexpected null Value");
277 assert(V &&
"Expected valid value");
286 assert(MD &&
"Expected valid metadata");
287 assert(MD->
getValue() == V &&
"Expected valid mapping");
296 assert(From &&
"Expected valid value");
297 assert(To &&
"Expected valid value");
298 assert(From != To &&
"Expected changed value");
299 assert(From->
getType() == To->
getType() &&
"Unexpected type change");
302 auto &
Store = Context.pImpl->ValuesAsMetadata;
303 auto I =
Store.find(From);
306 "Expected From not to be used by metadata");
312 "Expected From to be used by metadata");
315 assert(MD &&
"Expected valid metadata");
316 assert(MD->
getValue() == From &&
"Expected valid mapping");
319 if (isa<LocalAsMetadata>(MD)) {
320 if (
auto *
C = dyn_cast<Constant>(To)) {
333 }
else if (!isa<Constant>(To)) {
340 auto *&Entry =
Store[To];
350 "Expected this to be the only metadata use");
368 bool WasInserted =
Store.insert(Entry);
370 assert(WasInserted &&
"Expected entry to be inserted");
371 Entry->second.Entry = Entry;
372 return &Entry->second;
376 assert(Entry &&
"Expected to find string map entry");
377 return Entry->first();
386 #define HANDLE_MDNODE_LEAF(CLASS) \
388 llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment, \
389 "Alignment is insufficient after objects prepended to " #CLASS);
390 #include "llvm/IR/Metadata.def"
392 void *MDNode::operator
new(
size_t Size,
unsigned NumOps) {
393 size_t OpSize = NumOps *
sizeof(
MDOperand);
397 void *Ptr =
reinterpret_cast<char *
>(::operator
new(OpSize + Size)) + OpSize;
399 for (
MDOperand *E = O - NumOps; O != E; --O)
404 void MDNode::operator
delete(
void *Mem) {
406 size_t OpSize = N->NumOperands *
sizeof(
MDOperand);
410 for (
MDOperand *E = O - N->NumOperands; O != E; --O)
412 ::operator
delete(
reinterpret_cast<char *
>(Mem) - OpSize);
418 NumUnresolved(0), Context(Context) {
431 if (!countUnresolvedOperands())
434 this->Context.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
441 #define HANDLE_MDNODE_LEAF(CLASS) \
443 return cast<CLASS>(this)->cloneImpl();
444 #include "llvm/IR/Metadata.def"
449 if (
auto *
N = dyn_cast_or_null<MDNode>(Op))
450 return !
N->isResolved();
454 unsigned MDNode::countUnresolvedOperands() {
455 assert(NumUnresolved == 0 &&
"Expected unresolved ops to be uncounted");
457 return NumUnresolved;
460 void MDNode::makeUniqued() {
461 assert(
isTemporary() &&
"Expected this to be temporary");
462 assert(!
isResolved() &&
"Expected this to be unresolved");
466 Op.reset(Op.get(),
this);
470 if (!countUnresolvedOperands())
473 assert(
isUniqued() &&
"Expected this to be uniqued");
476 void MDNode::makeDistinct() {
477 assert(
isTemporary() &&
"Expected this to be temporary");
478 assert(!
isResolved() &&
"Expected this to be unresolved");
485 assert(
isDistinct() &&
"Expected this to be distinct");
486 assert(
isResolved() &&
"Expected this to be resolved");
489 void MDNode::resolve() {
490 assert(
isUniqued() &&
"Expected this to be uniqued");
491 assert(!
isResolved() &&
"Expected this to be unresolved");
494 auto Uses = Context.takeReplaceableUses();
496 assert(
isResolved() &&
"Expected this to be resolved");
499 Uses->resolveAllUses();
503 assert(NumUnresolved != 0 &&
"Expected unresolved operands");
511 decrementUnresolvedOperandCount();
514 void MDNode::decrementUnresolvedOperandCount() {
515 if (!--NumUnresolved)
529 auto *
N = dyn_cast_or_null<MDNode>(Op);
533 assert(!
N->isTemporary() &&
534 "Expected all forward declarations to be resolved");
535 if (!
N->isResolved())
547 MDNode *MDNode::replaceWithPermanentImpl() {
549 return replaceWithDistinctImpl();
550 return replaceWithUniquedImpl();
553 MDNode *MDNode::replaceWithUniquedImpl() {
555 MDNode *UniquedNode = uniquify();
557 if (UniquedNode ==
this) {
568 MDNode *MDNode::replaceWithDistinctImpl() {
573 void MDTuple::recalculateHash() {
574 setHash(MDTupleInfo::KeyTy::calculateHash(
this));
578 for (
unsigned I = 0, E = NumOperands;
I != E; ++
I)
581 Context.getReplaceableUses()->resolveAllUses(
false);
582 (void)Context.takeReplaceableUses();
586 void MDNode::handleChangedOperand(
void *Ref,
Metadata *New) {
614 resolveAfterOperandChange(Old, New);
626 Context.getReplaceableUses()->replaceAllUsesWith(
Uniqued);
635 void MDNode::deleteAsSubclass() {
639 #define HANDLE_MDNODE_LEAF(CLASS) \
641 delete cast<CLASS>(this); \
643 #include "llvm/IR/Metadata.def"
647 template <
class T,
class InfoT>
656 template <
class NodeTy>
struct MDNode::HasCachedHash {
659 template <
class U, U Val>
struct SFINAE {};
662 static Yes &
check(
SFINAE<
void (U::*)(
unsigned), &U::setHash> *);
663 template <
class U>
static No &
check(...);
665 static const bool value =
sizeof(check<NodeTy>(
nullptr)) ==
sizeof(Yes);
668 MDNode *MDNode::uniquify() {
669 assert(!
hasSelfReference(
this) &&
"Cannot uniquify a self-referencing node");
675 #define HANDLE_MDNODE_LEAF(CLASS) \
676 case CLASS##Kind: { \
677 CLASS *SubclassThis = cast<CLASS>(this); \
678 std::integral_constant<bool, HasCachedHash<CLASS>::value> \
679 ShouldRecalculateHash; \
680 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
681 return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
683 #include "llvm/IR/Metadata.def"
687 void MDNode::eraseFromStore() {
691 #define HANDLE_MDNODE_LEAF(CLASS) \
693 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
695 #include "llvm/IR/Metadata.def"
700 StorageType Storage,
bool ShouldCreate) {
703 MDTupleInfo::KeyTy Key(MDs);
708 Hash = Key.getHash();
710 assert(ShouldCreate &&
"Expected non-uniqued nodes to always be created");
718 assert(N->
isTemporary() &&
"Expected temporary node");
720 N->deleteAsSubclass();
724 assert(
isResolved() &&
"Expected resolved nodes");
731 #define HANDLE_MDNODE_LEAF(CLASS) \
732 case CLASS##Kind: { \
733 std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
734 dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
737 #include "llvm/IR/Metadata.def"
756 assert(I < NumOperands);
769 if (
MDNode *
N = dyn_cast_or_null<MDNode>(Ops[0]))
770 if (
N->getNumOperands() == Ops.
size() &&
N ==
N->getOperand(0)) {
771 for (
unsigned I = 1, E = Ops.
size();
I != E; ++
I)
772 if (Ops[
I] !=
N->getOperand(
I))
846 unsigned Size = EndPoints.
size();
847 APInt LB = EndPoints[Size - 2]->getValue();
848 APInt LE = EndPoints[Size - 1]->getValue();
853 EndPoints[Size - 2] =
855 EndPoints[Size - 1] =
864 if (!EndPoints.
empty())
890 while (AI < AN && BI < BN) {
896 mdconst::extract<ConstantInt>(A->
getOperand(2 * AI + 1)));
900 mdconst::extract<ConstantInt>(B->
getOperand(2 * BI + 1)));
906 mdconst::extract<ConstantInt>(A->
getOperand(2 * AI + 1)));
911 mdconst::extract<ConstantInt>(B->
getOperand(2 * BI + 1)));
917 unsigned Size = EndPoints.
size();
922 for (
unsigned i = 0; i < Size - 2; ++i) {
923 EndPoints[i] = EndPoints[i + 2];
925 EndPoints.
resize(Size - 2);
931 if (EndPoints.
size() == 2) {
932 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
939 for (
auto *
I : EndPoints)
952 NamedMDNode::NamedMDNode(
const Twine &
N)
953 :
Name(N.str()), Parent(nullptr),
956 NamedMDNode::~NamedMDNode() {
962 return (
unsigned)
getNMDOps(Operands).size();
968 return cast_or_null<MDNode>(
N);
994 for (
auto &
I : Attachments)
999 Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
1000 std::make_tuple(&MD));
1008 if (Attachments.back().first ==
ID) {
1009 Attachments.pop_back();
1013 for (
auto I = Attachments.begin(), E = std::prev(Attachments.end());
I != E;
1015 if (
I->first == ID) {
1016 *
I = std::move(Attachments.back());
1017 Attachments.pop_back();
1023 for (
const auto &
I : Attachments)
1031 Result.append(Attachments.begin(), Attachments.end());
1034 if (Result.size() > 1)
1045 return getMetadataImpl(
getContext().getMDKindID(Kind));
1056 if (!hasMetadataHashEntry())
1061 if (KnownSet.
empty()) {
1063 InstructionMetadata.erase(
this);
1064 setHasMetadataHashEntry(
false);
1068 auto &Info = InstructionMetadata[
this];
1069 Info.remove_if([&KnownSet](
const std::pair<unsigned, TrackingMDNodeRef> &
I) {
1070 return !KnownSet.
count(I.first);
1075 InstructionMetadata.erase(
this);
1076 setHasMetadataHashEntry(
false);
1096 assert(!Info.empty() == hasMetadataHashEntry() &&
1097 "HasMetadata bit is wonked");
1099 setHasMetadataHashEntry(
true);
1100 Info.set(KindID, *Node);
1105 assert((hasMetadataHashEntry() ==
1106 (
getContext().pImpl->InstructionMetadata.count(
this) > 0)) &&
1107 "HasMetadata bit out of date!");
1108 if (!hasMetadataHashEntry())
1119 setHasMetadataHashEntry(
false);
1128 MDNode *Instruction::getMetadataImpl(
unsigned KindID)
const {
1133 if (!hasMetadataHashEntry())
1136 assert(!Info.empty() &&
"bit out of sync with hash table");
1138 return Info.lookup(KindID);
1141 void Instruction::getAllMetadataImpl(
1149 if (!hasMetadataHashEntry())
return;
1152 assert(hasMetadataHashEntry() &&
1153 getContext().pImpl->InstructionMetadata.count(
this) &&
1154 "Shouldn't have called this");
1156 assert(!Info.empty() &&
"Shouldn't have called this");
1157 Info.getAll(Result);
1160 void Instruction::getAllMetadataOtherThanDebugLocImpl(
1163 assert(hasMetadataHashEntry() &&
1164 getContext().pImpl->InstructionMetadata.count(
this) &&
1165 "Shouldn't have called this");
1167 assert(!Info.empty() &&
"Shouldn't have called this");
1168 Info.getAll(Result);
1173 void Instruction::clearMetadataHashEntries() {
1174 assert(hasMetadataHashEntry() &&
"Caller should check");
1176 setHasMetadataHashEntry(
false);
1194 setHasMetadataHashEntry(
true);
1205 Store.erase(KindID);
1229 if (KnownIDs.
empty()) {
1238 assert(!
Store.empty());
1240 Store.remove_if([&KnownSet](
const std::pair<unsigned, TrackingMDNodeRef> &
I) {
1241 return !KnownSet.
count(I.first);
1248 void Function::clearMetadata() {
1252 setHasMetadataHashEntry(
false);
StringRef getName() const
void push_back(const T &Elt)
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Tracking metadata reference owned by Metadata.
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
DenseMap< const Function *, MDAttachmentMap > FunctionMetadata
Collection of per-function metadata used in this context.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
MDNode * Scope
The tag for alias scope specification (used with noalias).
MDNode * TBAA
The tag for type-based alias analysis.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Tracking metadata reference.
static MDString * get(LLVMContext &Context, StringRef Str)
DenseSet - This implements a dense probed hash-table based set.
unsigned getNumOperands() const
Return number of MDNode operands.
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
void addOperand(MDNode *M)
void storeDistinctInContext()
op_iterator op_begin() const
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
void setOperand(unsigned I, Metadata *New)
Set an operand.
static Type * getMetadataTy(LLVMContext &C)
void reserve(size_type N)
void setOperand(unsigned I, MDNode *New)
op_iterator op_end() const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
void set(unsigned ID, MDNode &MD)
Set an attachment to a particular node.
DenseMap< const Instruction *, MDAttachmentMap > InstructionMetadata
Collection of per-instruction metadata used in this context.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
const APInt & getValue() const
Return the constant as an APInt value reference.
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
void eraseFromParent()
Drop all references and remove the node from parent module.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void setMetadata(unsigned KindID, MDNode *MD)
Set a particular kind of metadata attachment.
void resolveCycles()
Resolve cycles.
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
static std::error_code check(std::error_code Err)
ConstantRange unionWith(const ConstantRange &CR) const
Return the range that results from the union of this range with another range.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static MDNode * intersect(MDNode *A, MDNode *B)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
size_t size() const
size - Get the array size.
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * >> &Result) const
Copy out all the attachments.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachment, if any.
cmpResult compare(const APFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type. ...
void setAAMetadata(const AAMDNodes &N)
setAAMetadata - Sets the metadata on this instruction from the AAMDNodes structure.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
void dropUnknownMetadata(ArrayRef< unsigned > KnownIDs)
Drop metadata not in the given list.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
bool hasMetadata() const
hasMetadata() - Return true if this instruction has any metadata attached to it.
bool hasMetadata() const
Check if this has any metadata.
MDNode * getOperand(unsigned i) const
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
bool empty() const
empty - Check if the array is empty.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
bool isEmptySet() const
Return true if this set contains no members.
TempMDNode clone() const
Create a (temporary) clone of this.
LLVMContext & getContext() const
All values hold a context through their type.
SmallPtrSet< MDNode *, 1 > DistinctMDNodes
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
mutable_op_range mutable_operands()
LLVMContextImpl *const pImpl
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
std::pair< iterator, bool > insert(const ValueT &V)
This is the shared class of boolean and integer constants.
bool slt(const APInt &RHS) const
Signed less than comparison.
T get() const
get<T>() - Return the value of the specified pointer type.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
MDNode * lookup(unsigned ID) const
Get a particular attachment (if any).
This class represents a range of values.
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Get all current metadata attachments.
const APInt & getLower() const
Return the lower value for this range.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
MDNode * NoAlias
The tag specifying the noalias scope.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Class for arbitrary precision integers.
void erase(unsigned ID)
Remove an attachment.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
MDNode * getAsMDNode() const
Return this as a bar MDNode.
MDOperand * mutable_begin()
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
StringMap< MDString > MDStringCache
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
op_range operands() const
LLVMContext & getContext() const
const APInt & getUpper() const
Return the upper value for this range.
const ARM::ArchExtKind Kind
LLVM Value Representation.
void dropUnknownMetadata()
static const Function * getParent(const Value *V)
unsigned getNumOperands() const
std::string Hash(const Unit &U)
void dropAllReferences()
Remove all uses and clear node vector.
StringRef - Represent a constant reference to a string, i.e.
int is() const
is<T>() return true if the Union currently holds the type matching T.
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
bool isResolved() const
Check if node is fully resolved.
Module * getParent()
Get the module that holds this named metadata collection.
PointerUnion - This implements a discriminated union of two pointer types, and keeps the discriminato...