ConstantFold: Don't optimize comparisons with weak linkage objects
authorDavid Majnemer <david.majnemer@gmail.com>
Sat, 6 Dec 2014 11:58:33 +0000 (11:58 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Sat, 6 Dec 2014 11:58:33 +0000 (11:58 +0000)
Consider:
void f() {}
void __attribute__((weak)) g() {}
bool b = &f != &g;

It's possble for g to resolve to f if --defsym=g=f is passed on to the
linker.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223585 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/ConstantFold.cpp
test/Assembler/ConstantExprNoFold.ll

index cdfb41f7dcce4b81de3161d01a3f9d2bfae1abd4..719a3a4b4c85ba2ac25cdbde5f49353c0e4fd45e 100644 (file)
@@ -1348,9 +1348,12 @@ static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
 
 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
                                                       const GlobalValue *GV2) {
+  auto isLinkageUnsafeForEquality = [](const GlobalValue *GV) {
+    return GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage();
+  };
   // Don't try to decide equality of aliases.
   if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
-    if (!GV1->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
+    if (!isLinkageUnsafeForEquality(GV1) && !isLinkageUnsafeForEquality(GV2))
       return ICmpInst::ICMP_NE;
   return ICmpInst::BAD_ICMP_PREDICATE;
 }
index 8d03e7a320b3ec5c31c9d3f3de17f555f6e93a82..bed58c52ad9f3980b4b16d44538f0c3b48e4da66 100644 (file)
@@ -31,6 +31,11 @@ target datalayout = "p:32:32"
 @weak.gep = global i32* getelementptr (i32* @weak, i32 1)
 @weak = extern_weak global i32
 
+; An object with weak linkage cannot have it's identity determined at compile time.
+; CHECK: @F = global i1 icmp eq (i32* @weakany, i32* @glob)
+@F = global i1 icmp eq (i32* @weakany, i32* @glob)
+@weakany = weak global i32 0
+
 ; Don't add an inbounds on @glob.a3, since it's not inbounds.
 ; CHECK: @glob.a3 = alias getelementptr (i32* @glob.a2, i32 1)
 @glob = global i32 0