return mapToMetadata(VM, MD, const_cast<Metadata *>(MD));
}
-static Metadata *MapMetadataImpl(const Metadata *MD, ValueToValueMapTy &VM,
- RemapFlags Flags,
+static Metadata *MapMetadataImpl(const Metadata *MD,
+ SmallVectorImpl<MDNode *> &Cycles,
+ ValueToValueMapTy &VM, RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer);
-static Metadata *mapMetadataOp(Metadata *Op, ValueToValueMapTy &VM,
- RemapFlags Flags,
+static Metadata *mapMetadataOp(Metadata *Op, SmallVectorImpl<MDNode *> &Cycles,
+ ValueToValueMapTy &VM, RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
if (!Op)
return nullptr;
if (Metadata *MappedOp =
- MapMetadataImpl(Op, VM, Flags, TypeMapper, Materializer))
+ MapMetadataImpl(Op, Cycles, VM, Flags, TypeMapper, Materializer))
return MappedOp;
// Use identity map if MappedOp is null and we can ignore missing entries.
if (Flags & RF_IgnoreMissingEntries)
return nullptr;
}
-static TempMDTuple cloneMDTuple(const MDTuple *Node) {
- SmallVector<Metadata *, 4> Elts;
- Elts.append(Node->op_begin(), Node->op_end());
- return MDTuple::getTemporary(Node->getContext(), Elts);
-}
-
-static TempMDLocation cloneMDLocation(const MDLocation *Node) {
- return MDLocation::getTemporary(Node->getContext(), Node->getLine(),
- Node->getColumn(), Node->getScope(),
- Node->getInlinedAt());
-}
+/// \brief Remap nodes.
+///
+/// Insert \c NewNode in the value map, and then remap \c OldNode's operands.
+/// Assumes that \c NewNode is already a clone of \c OldNode.
+///
+/// \pre \c NewNode is a clone of \c OldNode.
+static bool remap(const MDNode *OldNode, MDNode *NewNode,
+ SmallVectorImpl<MDNode *> &Cycles, ValueToValueMapTy &VM,
+ RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
+ ValueMaterializer *Materializer) {
+ assert(OldNode->getNumOperands() == NewNode->getNumOperands() &&
+ "Expected nodes to match");
+ assert(OldNode->isResolved() && "Expected resolved node");
+ assert(!NewNode->isUniqued() && "Expected non-uniqued node");
+
+ // Map the node upfront so it's available for cyclic references.
+ mapToMetadata(VM, OldNode, NewNode);
+ bool AnyChanged = false;
+ for (unsigned I = 0, E = OldNode->getNumOperands(); I != E; ++I) {
+ Metadata *Old = OldNode->getOperand(I);
+ assert(NewNode->getOperand(I) == Old &&
+ "Expected old operands to already be in place");
-static TempUniquableMDNode cloneMDNode(const UniquableMDNode *Node) {
- switch (Node->getMetadataID()) {
- default:
- llvm_unreachable("Invalid UniquableMDNode subclass");
-#define HANDLE_UNIQUABLE_LEAF(CLASS) \
- case Metadata::CLASS##Kind: \
- return clone##CLASS(cast<CLASS>(Node));
-#include "llvm/IR/Metadata.def"
+ Metadata *New = mapMetadataOp(OldNode->getOperand(I), Cycles, VM, Flags,
+ TypeMapper, Materializer);
+ if (Old != New) {
+ AnyChanged = true;
+ NewNode->replaceOperandWith(I, New);
+ }
}
+
+ return AnyChanged;
}
/// \brief Map a distinct MDNode.
///
/// Distinct nodes are not uniqued, so they must always recreated.
-static Metadata *mapDistinctNode(const UniquableMDNode *Node,
+static Metadata *mapDistinctNode(const MDNode *Node,
+ SmallVectorImpl<MDNode *> &Cycles,
ValueToValueMapTy &VM, RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
assert(Node->isDistinct() && "Expected distinct node");
- // Create the node first so it's available for cyclical references.
- MDNode *NewMD = MDNode::replaceWithDistinct(cloneMDNode(Node));
- mapToMetadata(VM, Node, NewMD);
+ MDNode *NewMD = MDNode::replaceWithDistinct(Node->clone());
+ remap(Node, NewMD, Cycles, VM, Flags, TypeMapper, Materializer);
- // Fix the operands.
- for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
- NewMD->replaceOperandWith(I, mapMetadataOp(Node->getOperand(I), VM, Flags,
- TypeMapper, Materializer));
+ // Track any cycles beneath this node.
+ for (Metadata *Op : NewMD->operands())
+ if (auto *Node = dyn_cast_or_null<MDNode>(Op))
+ if (!Node->isResolved())
+ Cycles.push_back(Node);
return NewMD;
}
/// \brief Map a uniqued MDNode.
///
/// Uniqued nodes may not need to be recreated (they may map to themselves).
-static Metadata *mapUniquedNode(const UniquableMDNode *Node,
- ValueToValueMapTy &VM, RemapFlags Flags,
- ValueMapTypeRemapper *TypeMapper,
- ValueMaterializer *Materializer) {
+static Metadata *mapUniquedNode(const MDNode *Node,
+ SmallVectorImpl<MDNode *> &Cycles,
+ ValueToValueMapTy &VM, RemapFlags Flags,
+ ValueMapTypeRemapper *TypeMapper,
+ ValueMaterializer *Materializer) {
assert(Node->isUniqued() && "Expected uniqued node");
// Create a temporary node upfront in case we have a metadata cycle.
- auto ClonedMD = cloneMDNode(Node);
- mapToMetadata(VM, Node, ClonedMD.get());
-
- // Remap the operands, keeping track of whether any changed.
- bool AnyChanged = false;
- for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
- Metadata *Old = Node->getOperand(I);
- Metadata *New = mapMetadataOp(Old, VM, Flags, TypeMapper, Materializer);
- if (Old != New) {
- ClonedMD->replaceOperandWith(I, New);
- AnyChanged = true;
- }
- }
-
- if (!AnyChanged)
- // Use an identity mapping.
+ auto ClonedMD = Node->clone();
+ if (!remap(Node, ClonedMD.get(), Cycles, VM, Flags, TypeMapper, Materializer))
+ // No operands changed, so use the identity mapping.
return mapToSelf(VM, Node);
// At least one operand has changed, so uniquify the cloned node.
MDNode::replaceWithUniqued(std::move(ClonedMD)));
}
-static Metadata *MapMetadataImpl(const Metadata *MD, ValueToValueMapTy &VM,
- RemapFlags Flags,
+static Metadata *MapMetadataImpl(const Metadata *MD,
+ SmallVectorImpl<MDNode *> &Cycles,
+ ValueToValueMapTy &VM, RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
// If the value already exists in the map, use it.
return nullptr;
}
- const UniquableMDNode *Node = cast<UniquableMDNode>(MD);
+ const MDNode *Node = cast<MDNode>(MD);
assert(Node->isResolved() && "Unexpected unresolved node");
// If this is a module-level metadata and we know that nothing at the
return mapToSelf(VM, MD);
if (Node->isDistinct())
- return mapDistinctNode(Node, VM, Flags, TypeMapper, Materializer);
+ return mapDistinctNode(Node, Cycles, VM, Flags, TypeMapper, Materializer);
- return mapUniquedNode(Node, VM, Flags, TypeMapper, Materializer);
+ return mapUniquedNode(Node, Cycles, VM, Flags, TypeMapper, Materializer);
}
Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
- Metadata *NewMD = MapMetadataImpl(MD, VM, Flags, TypeMapper, Materializer);
- if (NewMD && NewMD != MD)
- if (auto *N = dyn_cast<UniquableMDNode>(NewMD))
- N->resolveCycles();
+ SmallVector<MDNode *, 8> Cycles;
+ Metadata *NewMD =
+ MapMetadataImpl(MD, Cycles, VM, Flags, TypeMapper, Materializer);
+
+ // Resolve cycles underneath MD.
+ if (NewMD && NewMD != MD) {
+ if (auto *N = dyn_cast<MDNode>(NewMD))
+ if (!N->isResolved())
+ N->resolveCycles();
+
+ for (MDNode *N : Cycles)
+ if (!N->isResolved())
+ N->resolveCycles();
+ } else {
+ // Shouldn't get unresolved cycles if nothing was remapped.
+ assert(Cycles.empty() && "Expected no unresolved cycles");
+ }
+
return NewMD;
}