[llvm] 75db002 - [ConstantFold] Remove another incorrect icmp of GEP fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 4 03:27:50 PST 2022


Author: Nikita Popov
Date: 2022-01-04T12:27:40+01:00
New Revision: 75db002725156fba9e9c38b7cefe57b7ed713734

URL: https://github.com/llvm/llvm-project/commit/75db002725156fba9e9c38b7cefe57b7ed713734
DIFF: https://github.com/llvm/llvm-project/commit/75db002725156fba9e9c38b7cefe57b7ed713734.diff

LOG: [ConstantFold] Remove another incorrect icmp of GEP fold

This fold is not correct, because indices might evaluate to zero
even if they are not a literal zero integer. Additionally, this
fold would be wrong (in the general case) for non-i8 types as well,
due to index overflow.

Drop this fold and instead let the target-dependent constant
folder compute the actual offset and fold the comparison based
on that.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index c3f3d3c4b4c1..f32d26ba0978 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1539,17 +1539,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
           // so the result is greater-than
           if (!GV->hasExternalWeakLinkage())
             return ICmpInst::ICMP_UGT;
-        } else if (isa<ConstantPointerNull>(CE1Op0)) {
-          // If we are indexing from a null pointer, check to see if we have any
-          // non-zero indices.
-          for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
-            if (!CE1->getOperand(i)->isNullValue())
-              // Offsetting from null, must not be equal.
-              return ICmpInst::ICMP_UGT;
-          // Only zero indexes from null, must still be zero.
-          return ICmpInst::ICMP_EQ;
         }
-        // Otherwise, we can't really say if the first operand is null or not.
       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
         if (isa<ConstantPointerNull>(CE1Op0)) {
           // If its not weak linkage, the GVal must have a non-zero address

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
index b2d6b3f1c7fb..99f1a16c5427 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
@@ -118,9 +118,11 @@ define i1 @global_gep_sgt_null() {
   ret i1 %cmp
 }
 
+; @g2_weak may be null, in which case this is a zero-index GEP and the pointers
+; are equal.
 define i1 @null_gep_ne_null() {
 ; CHECK-LABEL: @null_gep_ne_null(
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 icmp ne (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
 ;
   %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
   %cmp = icmp ne i8* %gep, null
@@ -129,7 +131,7 @@ define i1 @null_gep_ne_null() {
 
 define i1 @null_gep_ugt_null() {
 ; CHECK-LABEL: @null_gep_ugt_null(
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    ret i1 icmp ugt (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null)
 ;
   %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)
   %cmp = icmp ugt i8* %gep, null


        


More information about the llvm-commits mailing list