[llvm] r223684 - ConstantFold: Zero-sized globals might land on top of another global

David Majnemer david.majnemer at gmail.com
Mon Dec 8 11:35:31 PST 2014


Author: majnemer
Date: Mon Dec  8 13:35:31 2014
New Revision: 223684

URL: http://llvm.org/viewvc/llvm-project?rev=223684&view=rev
Log:
ConstantFold: Zero-sized globals might land on top of another global

A zero sized array is zero sized and might share its address with
another global.

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=223684&r1=223683&r2=223684&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Mon Dec  8 13:35:31 2014
@@ -1348,12 +1348,24 @@ static FCmpInst::Predicate evaluateFCmpR
 
 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
                                                       const GlobalValue *GV2) {
-  auto isLinkageUnsafeForEquality = [](const GlobalValue *GV) {
-    return GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage();
+  auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
+    if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
+      return true;
+    if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
+      Type *Ty = GVar->getType()->getPointerElementType();
+      // A global with opaque type might end up being zero sized.
+      if (!Ty->isSized())
+        return true;
+      // A global with an empty type might lie at the address of any other
+      // global.
+      if (Ty->isEmptyTy())
+        return true;
+    }
+    return false;
   };
   // Don't try to decide equality of aliases.
   if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
-    if (!isLinkageUnsafeForEquality(GV1) && !isLinkageUnsafeForEquality(GV2))
+    if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(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=223684&r1=223683&r2=223684&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprNoFold.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprNoFold.ll Mon Dec  8 13:35:31 2014
@@ -36,6 +36,12 @@ target datalayout = "p:32:32"
 @F = global i1 icmp eq (i32* @weakany, i32* @glob)
 @weakany = weak global i32 0
 
+; Empty globals might end up anywhere, even on top of another global.
+; CHECK: @empty.cmp = global i1 icmp eq ([0 x i8]* @empty.1, [0 x i8]* @empty.2)
+ at empty.1 = external global [0 x i8], align 1
+ at empty.2 = external global [0 x i8], align 1
+ at empty.cmp = global i1 icmp eq ([0 x i8]* @empty.1, [0 x i8]* @empty.2)
+
 ; 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