[llvm] r335649 - ConstantFold: Don't fold global address vs. null for addrspace != 0

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 26 11:55:43 PDT 2018


Author: arsenm
Date: Tue Jun 26 11:55:43 2018
New Revision: 335649

URL: http://llvm.org/viewvc/llvm-project?rev=335649&view=rev
Log:
ConstantFold: Don't fold global address vs. null for addrspace != 0

Not sure why this logic seems to be repeated in 2 different places,
one called by the other.

On AMDGPU addrspace(3) globals start allocating at 0, so these
checks will be incorrect (not that real code actually tries
to compare these addresses)

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprNoFold.ll
    llvm/trunk/test/Transforms/InstCombine/constant-fold-address-space-pointer.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=335649&r1=335648&r2=335649&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Tue Jun 26 11:55:43 2018
@@ -1500,7 +1500,8 @@ static ICmpInst::Predicate evaluateICmpR
       assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
       // GlobalVals can never be null unless they have external weak linkage.
       // We don't try to evaluate aliases here.
-      if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
+      if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
+          GV->getType()->getAddressSpace() == 0)
         return ICmpInst::ICMP_NE;
     }
   } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
@@ -1730,7 +1731,8 @@ Constant *llvm::ConstantFoldCompareInstr
   if (C1->isNullValue()) {
     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
       // Don't try to evaluate aliases.  External weak GV can be null.
-      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
+      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
+          GV->getType()->getAddressSpace() == 0) {
         if (pred == ICmpInst::ICMP_EQ)
           return ConstantInt::getFalse(C1->getContext());
         else if (pred == ICmpInst::ICMP_NE)
@@ -1740,7 +1742,8 @@ Constant *llvm::ConstantFoldCompareInstr
   } else if (C2->isNullValue()) {
     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
       // Don't try to evaluate aliases.  External weak GV can be null.
-      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
+      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
+          GV->getType()->getAddressSpace() == 0) {
         if (pred == ICmpInst::ICMP_EQ)
           return ConstantInt::getFalse(C1->getContext());
         else if (pred == ICmpInst::ICMP_NE)

Modified: llvm/trunk/test/Assembler/ConstantExprNoFold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprNoFold.ll?rev=335649&r1=335648&r2=335649&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprNoFold.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprNoFold.ll Tue Jun 26 11:55:43 2018
@@ -42,6 +42,17 @@ target datalayout = "p:32:32"
 @empty.2 = external global [0 x i8], align 1
 @empty.cmp = global i1 icmp eq ([0 x i8]* @empty.1, [0 x i8]* @empty.2)
 
+ at addrspace3 = internal addrspace(3) global i32 undef
+
+; CHECK: @no.fold.addrspace.icmp.eq.gv.null = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+; CHECK: @no.fold.addrspace.icmp.eq.null.gv = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+; CHECK: @no.fold.addrspace.icmp.ne.gv.null = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+; CHECK: @no.fold.addrspace.icmp.ne.null.gv = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+ at no.fold.addrspace.icmp.eq.gv.null = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+ at no.fold.addrspace.icmp.eq.null.gv = global i1 icmp eq (i32 addrspace(3)* null, i32 addrspace(3)* @addrspace3)
+ at no.fold.addrspace.icmp.ne.gv.null = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null)
+ at no.fold.addrspace.icmp.ne.null.gv = global i1 icmp ne (i32 addrspace(3)* null, i32 addrspace(3)* @addrspace3)
+
 ; Don't add an inbounds on @glob.a3, since it's not inbounds.
 ; CHECK: @glob.a3 = alias i32, getelementptr (i32, i32* @glob.a2, i32 1)
 @glob = global i32 0

Modified: llvm/trunk/test/Transforms/InstCombine/constant-fold-address-space-pointer.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/constant-fold-address-space-pointer.ll?rev=335649&r1=335648&r2=335649&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/constant-fold-address-space-pointer.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/constant-fold-address-space-pointer.ll Tue Jun 26 11:55:43 2018
@@ -118,14 +118,14 @@ define i1 @constant_fold_inttoptr_null(i
 
 define i1 @constant_fold_ptrtoint_null() {
 ; CHECK-LABEL: @constant_fold_ptrtoint_null(
-; CHECK-NEXT: ret i1 false
+; CHECK-NEXT: ret i1 icmp eq (i32 addrspace(3)* @g, i32 addrspace(3)* null)
   %x = icmp eq i16 ptrtoint (i32 addrspace(3)* @g to i16), ptrtoint (i32 addrspace(3)* null to i16)
   ret i1 %x
 }
 
 define i1 @constant_fold_ptrtoint_null_2() {
 ; CHECK-LABEL: @constant_fold_ptrtoint_null_2(
-; CHECK-NEXT: ret i1 false
+; CHECK-NEXT: ret i1 icmp eq (i32 addrspace(3)* @g, i32 addrspace(3)* null)
   %x = icmp eq i16 ptrtoint (i32 addrspace(3)* null to i16), ptrtoint (i32 addrspace(3)* @g to i16)
   ret i1 %x
 }




More information about the llvm-commits mailing list