[llvm] r226515 - Utils: Simplify MapMetadata(), NFC
Duncan P. N. Exon Smith
dexonsmith at apple.com
Mon Jan 19 14:44:33 PST 2015
Author: dexonsmith
Date: Mon Jan 19 16:44:32 2015
New Revision: 226515
URL: http://llvm.org/viewvc/llvm-project?rev=226515&view=rev
Log:
Utils: Simplify MapMetadata(), NFC
Extract out the operand remapping loops, which are now very similar.
Modified:
llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=226515&r1=226514&r2=226515&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Mon Jan 19 16:44:32 2015
@@ -203,6 +203,40 @@ static TempUniquableMDNode cloneMDNode(c
}
}
+/// \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 UniquableMDNode *OldNode, UniquableMDNode *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.
@@ -212,15 +246,8 @@ static Metadata *mapDistinctNode(const U
ValueMaterializer *Materializer) {
assert(Node->isDistinct() && "Expected distinct node");
- // Create the node first so it's available for cyclical references.
UniquableMDNode *NewMD = MDNode::replaceWithDistinct(cloneMDNode(Node));
- 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));
-
+ remap(Node, NewMD, VM, Flags, TypeMapper, Materializer);
return NewMD;
}
@@ -235,21 +262,9 @@ static Metadata *mapUniquedNode(const Un
// 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.
+ 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.
More information about the llvm-commits
mailing list