[llvm] r223585 - ConstantFold: Don't optimize comparisons with weak linkage objects

David Majnemer david.majnemer at gmail.com
Sat Dec 6 03:58:34 PST 2014


Author: majnemer
Date: Sat Dec  6 05:58:33 2014
New Revision: 223585

URL: http://llvm.org/viewvc/llvm-project?rev=223585&view=rev
Log:
ConstantFold: Don't optimize comparisons with weak linkage objects

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.

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprNoFold.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=223585&r1=223584&r2=223585&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Sat Dec  6 05:58:33 2014
@@ -1348,9 +1348,12 @@ static FCmpInst::Predicate evaluateFCmpR
 
 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;
 }

Modified: llvm/trunk/test/Assembler/ConstantExprNoFold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprNoFold.ll?rev=223585&r1=223584&r2=223585&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprNoFold.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprNoFold.ll Sat Dec  6 05:58:33 2014
@@ -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)
+ at F = global i1 icmp eq (i32* @weakany, i32* @glob)
+ at 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





More information about the llvm-commits mailing list