// anymore. This commonly occurs during destruction, and uniquing these
// brings little reuse. Also, this means we don't need to include
// isFunctionLocal bits in the hash for MDNodes.
- if (!To) {
+ //
+ // Also drop uniquing if this has a reference to itself.
+ if (!To || To == this) {
setIsNotUniqued();
return;
}
}
}
+ // Handle alias scope self-references specially.
+ //
+ // FIXME: This preserves long-standing behaviour, but is it really the right
+ // behaviour? Or was that an unintended side-effect of node uniquing?
+ if (!Vals.empty())
+ if (MDNode *N = dyn_cast_or_null<MDNode>(Vals[0]))
+ if (N->getNumOperands() == Vals.size() && N == N->getOperand(0)) {
+ for (unsigned I = 1, E = Vals.size(); I != E; ++I)
+ if (Vals[I] != N->getOperand(I))
+ return MDNode::get(A->getContext(), Vals);
+ return N;
+ }
return MDNode::get(A->getContext(), Vals);
}
delete I;
}
+TEST_F(MDNodeTest, SelfReference) {
+ // !0 = metadata !{metadata !0}
+ // !1 = metadata !{metadata !0}
+ {
+ MDNode *Temp = MDNode::getTemporary(Context, None);
+ Value *Args[] = {Temp};
+ MDNode *Self = MDNode::get(Context, Args);
+ Self->replaceOperandWith(0, Self);
+ MDNode::deleteTemporary(Temp);
+ ASSERT_EQ(Self, Self->getOperand(0));
+
+ // Self-references should be distinct, so MDNode::get() should grab a
+ // uniqued node that references Self, not Self.
+ Args[0] = Self;
+ MDNode *Ref1 = MDNode::get(Context, Args);
+ MDNode *Ref2 = MDNode::get(Context, Args);
+ EXPECT_NE(Self, Ref1);
+ EXPECT_EQ(Ref1, Ref2);
+ }
+
+ // !0 = metadata !{metadata !0, metadata !{}}
+ // !1 = metadata !{metadata !0, metadata !{}}
+ {
+ MDNode *Temp = MDNode::getTemporary(Context, None);
+ Value *Args[] = {Temp, MDNode::get(Context, None)};
+ MDNode *Self = MDNode::get(Context, Args);
+ Self->replaceOperandWith(0, Self);
+ MDNode::deleteTemporary(Temp);
+ ASSERT_EQ(Self, Self->getOperand(0));
+
+ // Self-references should be distinct, so MDNode::get() should grab a
+ // uniqued node that references Self, not Self itself.
+ Args[0] = Self;
+ MDNode *Ref1 = MDNode::get(Context, Args);
+ MDNode *Ref2 = MDNode::get(Context, Args);
+ EXPECT_NE(Self, Ref1);
+ EXPECT_EQ(Ref1, Ref2);
+ }
+}
+
TEST(NamedMDNodeTest, Search) {
LLVMContext Context;
Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);