[llvm] 7faad5c - [ConstantFold] Handle icmp of global and null consistently

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 8 08:19:58 PST 2021


Author: Nikita Popov
Date: 2021-03-08T17:18:01+01:00
New Revision: 7faad5c90033c56a32b07eac527a456d60ed9a70

URL: https://github.com/llvm/llvm-project/commit/7faad5c90033c56a32b07eac527a456d60ed9a70
DIFF: https://github.com/llvm/llvm-project/commit/7faad5c90033c56a32b07eac527a456d60ed9a70.diff

LOG: [ConstantFold] Handle icmp of global and null consistently

Return UGT rather than NE for icmp @g, null, which is slightly
stronger. This is consistent with what we do for more complex
folds. It is somewhat silly that @g ugt null does not get folded
while (gep @g) ugt null does.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 2e0ea4c50e79..37017697f4e1 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1764,7 +1764,7 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
       if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
           !NullPointerIsDefined(nullptr /* F */,
                                 GV->getType()->getAddressSpace()))
-        return ICmpInst::ICMP_NE;
+        return ICmpInst::ICMP_UGT;
     }
   } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-null.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-null.ll
index 41a64205633e..8698132b87f9 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-null.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-null.ll
@@ -37,7 +37,7 @@ define i1 @ult_constexpr_null(i8* %x) {
 
 define i1 @ule_constexpr_null(i8* %x) {
 ; CHECK-LABEL: @ule_constexpr_null(
-; CHECK-NEXT:    ret i1 icmp ule (i8 (...)* bitcast (i1 (i8*)* @ugt_null_constexpr to i8 (...)*), i8 (...)* null)
+; CHECK-NEXT:    ret i1 false
 ;
   %cmp = icmp ule i8 (...)* bitcast (i1 (i8*)* @ugt_null_constexpr to i8 (...)*), null
   ret i1 %cmp
@@ -76,7 +76,7 @@ define i1 @global_ne_null() {
 
 define i1 @global_ugt_null() {
 ; CHECK-LABEL: @global_ugt_null(
-; CHECK-NEXT:    ret i1 icmp ugt ([2 x i32]* @g, [2 x i32]* null)
+; CHECK-NEXT:    ret i1 true
 ;
   %cmp = icmp ugt [2 x i32]* @g, null
   ret i1 %cmp


        


More information about the llvm-commits mailing list