ValueMaterializer *Materializer);
static Metadata *mapMetadataOp(Metadata *Op, ValueToValueMapTy &VM,
- RemapFlags Flags,
- ValueMapTypeRemapper *TypeMapper,
- ValueMaterializer *Materializer) {
+ RemapFlags Flags,
+ ValueMapTypeRemapper *TypeMapper,
+ ValueMaterializer *Materializer) {
if (!Op)
return nullptr;
if (Metadata *MappedOp =
return nullptr;
}
+/// \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, 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");
+
+ Metadata *New = mapMetadataOp(OldNode->getOperand(I), 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,
- ValueToValueMapTy &VM, RemapFlags Flags,
+static Metadata *mapDistinctNode(const MDNode *Node, 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.
- SmallVector<Metadata *, 4> EmptyOps(Node->getNumOperands());
- MDTuple *NewMD = MDTuple::getDistinct(Node->getContext(), EmptyOps);
- mapToMetadata(VM, Node, NewMD);
-
- // Fix the operands.
- for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
- NewMD->replaceOperandWith(I, mapMetadataOp(Node->getOperand(I), VM, Flags,
- TypeMapper, Materializer));
-
+ MDNode *NewMD = MDNode::replaceWithDistinct(Node->clone());
+ remap(Node, NewMD, VM, Flags, TypeMapper, Materializer);
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) {
- assert(!Node->isDistinct() && "Expected uniqued node");
-
- // Create a dummy node in case we have a metadata cycle.
- MDNodeFwdDecl *Dummy = MDNode::getTemporary(Node->getContext(), None);
- mapToMetadata(VM, Node, Dummy);
-
- // Check all operands to see if any need to be remapped.
- for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
- Metadata *Op = Node->getOperand(I);
- if (Op == mapMetadataOp(Op, VM, Flags, TypeMapper, Materializer))
- continue;
-
- // Ok, at least one operand needs remapping.
- SmallVector<Metadata *, 4> Elts;
- Elts.reserve(Node->getNumOperands());
- for (I = 0; I != E; ++I)
- Elts.push_back(mapMetadataOp(Node->getOperand(I), VM, Flags, TypeMapper,
- Materializer));
-
- MDNode *NewMD = MDTuple::get(Node->getContext(), Elts);
- Dummy->replaceAllUsesWith(NewMD);
- MDNode::deleteTemporary(Dummy);
- return mapToMetadata(VM, Node, NewMD);
- }
-
- // No operands needed remapping. Use an identity mapping.
- mapToSelf(VM, Node);
- MDNode::deleteTemporary(Dummy);
- return const_cast<Metadata *>(static_cast<const Metadata *>(Node));
+static Metadata *mapUniquedNode(const MDNode *Node, 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 = Node->clone();
+ if (!remap(Node, ClonedMD.get(), 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.
+ return mapToMetadata(VM, Node,
+ MDNode::replaceWithUniqued(std::move(ClonedMD)));
}
static Metadata *MapMetadataImpl(const Metadata *MD, ValueToValueMapTy &VM,
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
ValueMaterializer *Materializer) {
Metadata *NewMD = MapMetadataImpl(MD, VM, Flags, TypeMapper, Materializer);
if (NewMD && NewMD != MD)
- if (auto *N = dyn_cast<UniquableMDNode>(NewMD))
- N->resolveCycles();
+ if (auto *N = dyn_cast<MDNode>(NewMD))
+ if (!N->isResolved())
+ N->resolveCycles();
return NewMD;
}