diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index 132c3920397..f5481d31eb8 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -38,15 +38,16 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges)) return VM[V] = const_cast(V); + // Create a dummy node in case we have a metadata cycle. + MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0); + VM[V] = Dummy; + // Check all operands to see if any need to be remapped. for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) { Value *OP = MD->getOperand(i); if (OP == 0 || MapValue(OP, VM, Flags) == OP) continue; - // Ok, at least one operand needs remapping. Create a dummy node in case - // we have a metadata cycle. - MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0); - VM[V] = Dummy; + // Ok, at least one operand needs remapping. SmallVector Elts; Elts.reserve(MD->getNumOperands()); for (i = 0; i != e; ++i) { @@ -55,12 +56,16 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, } MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size()); Dummy->replaceAllUsesWith(NewMD); + VM[V] = NewMD; MDNode::deleteTemporary(Dummy); - return VM[V] = NewMD; + return NewMD; } + VM[V] = const_cast(V); + MDNode::deleteTemporary(Dummy); + // No operands needed remapping. Use an identity mapping. - return VM[V] = const_cast(V); + return const_cast(V); } // Okay, this either must be a constant (which may or may not be mappable) or diff --git a/test/Linker/linkmdnode.ll b/test/Linker/linkmdnode.ll index be7455056cd..5f1158039fc 100644 --- a/test/Linker/linkmdnode.ll +++ b/test/Linker/linkmdnode.ll @@ -10,3 +10,4 @@ define void @foo() { %x = call i8 @llvm.something(metadata !21) ret void } + diff --git a/test/Linker/linkmdnode2.ll b/test/Linker/linkmdnode2.ll index 54a5a578b60..a7d991a8a4f 100644 --- a/test/Linker/linkmdnode2.ll +++ b/test/Linker/linkmdnode2.ll @@ -10,3 +10,13 @@ define void @foo1() { %x = call i8 @llvm.something(metadata !22) ret void } + + + +; PR9015 +define void @test() { + ret void, !abc !0 +} + +!0 = metadata !{metadata !0, i32 42 } +