[PATCH] D48588: ConstantFold: Don't fold global address vs. null for addrspace != 0

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 26 01:55:04 PDT 2018


arsenm created this revision.
arsenm added a reviewer: efriedma.
Herald added subscribers: tpr, wdng.

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)


https://reviews.llvm.org/D48588

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


Index: test/Transforms/InstCombine/constant-fold-address-space-pointer.ll
===================================================================
--- test/Transforms/InstCombine/constant-fold-address-space-pointer.ll
+++ test/Transforms/InstCombine/constant-fold-address-space-pointer.ll
@@ -118,14 +118,14 @@
 
 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
 }
Index: test/Assembler/ConstantExprNoFold.ll
===================================================================
--- test/Assembler/ConstantExprNoFold.ll
+++ test/Assembler/ConstantExprNoFold.ll
@@ -42,6 +42,17 @@
 @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
Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -1500,7 +1500,8 @@
       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 @@
   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 @@
   } 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48588.152851.patch
Type: text/x-patch
Size: 4140 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180626/21679600/attachment.bin>


More information about the llvm-commits mailing list