[ConstantFold] Handle icmp of global and null consistently
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 8 Mar 2021 16:18:01 +0000 (17:18 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 8 Mar 2021 16:18:01 +0000 (17:18 +0100)
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.

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

index 2e0ea4c..3701769 100644 (file)
@@ -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.
index 41a6420..8698132 100644 (file)
@@ -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