[llvm] 807b019 - [ConstantFolding] Fixing addo/subo with undef

George Mitenkov via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 31 12:14:45 PDT 2021


Author: George Mitenkov
Date: 2021-03-31T21:47:29+03:00
New Revision: 807b019ca292e53cc40cc76c1f0efd8a7b2efe62

URL: https://github.com/llvm/llvm-project/commit/807b019ca292e53cc40cc76c1f0efd8a7b2efe62
DIFF: https://github.com/llvm/llvm-project/commit/807b019ca292e53cc40cc76c1f0efd8a7b2efe62.diff

LOG: [ConstantFolding] Fixing addo/subo with undef

When folding addo/subo with undef, the current
convention is to use { -1, false } for addo and
{ 0, false } for subo. This was fixed for InstSimplify in
https://reviews.llvm.org/rGf094d65beaa492e845b03561eddd75b5be653a01,
but not in ConstantFolding.

Reviewed By: nikic, lebedev.ri

Differential Revision: https://reviews.llvm.org/D99564

Added: 
    

Modified: 
    llvm/lib/Analysis/ConstantFolding.cpp
    llvm/test/Transforms/InstSimplify/ConstProp/overflow-ops.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index be164a5a29f5a..7f3b909accb80 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2519,16 +2519,19 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
 
     case Intrinsic::usub_with_overflow:
     case Intrinsic::ssub_with_overflow:
+      // X - undef -> { 0, false }
+      // undef - X -> { 0, false }
+      if (!C0 || !C1)
+        return Constant::getNullValue(Ty);
+      LLVM_FALLTHROUGH;
     case Intrinsic::uadd_with_overflow:
     case Intrinsic::sadd_with_overflow:
-      // X - undef -> { undef, false }
-      // undef - X -> { undef, false }
-      // X + undef -> { undef, false }
-      // undef + x -> { undef, false }
+      // X + undef -> { -1, false }
+      // undef + x -> { -1, false }
       if (!C0 || !C1) {
         return ConstantStruct::get(
             cast<StructType>(Ty),
-            {UndefValue::get(Ty->getStructElementType(0)),
+            {Constant::getAllOnesValue(Ty->getStructElementType(0)),
              Constant::getNullValue(Ty->getStructElementType(1))});
       }
       LLVM_FALLTHROUGH;

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/overflow-ops.ll b/llvm/test/Transforms/InstSimplify/ConstProp/overflow-ops.ll
index c97d2e0a03db9..43c71543e1b5d 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/overflow-ops.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/overflow-ops.ll
@@ -31,7 +31,7 @@ define {i8, i1} @uadd_2() nounwind {
 
 define {i8, i1} @uadd_undef() nounwind {
 ; CHECK-LABEL: @uadd_undef(
-; CHECK-NEXT:    ret { i8, i1 } { i8 undef, i1 false }
+; CHECK-NEXT:    ret { i8, i1 } { i8 -1, i1 false }
 ;
   %t = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 142, i8 undef)
   ret {i8, i1} %t
@@ -59,7 +59,7 @@ define {i8, i1} @usub_2() nounwind {
 
 define {i8, i1} @usub_undef() nounwind {
 ; CHECK-LABEL: @usub_undef(
-; CHECK-NEXT:    ret { i8, i1 } { i8 undef, i1 false }
+; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
 ;
   %t = call {i8, i1} @llvm.usub.with.overflow.i8(i8 4, i8 undef)
   ret {i8, i1} %t
@@ -147,7 +147,7 @@ define {i8, i1} @sadd_5() nounwind {
 
 define {i8, i1} @sadd_undef() nounwind {
 ; CHECK-LABEL: @sadd_undef(
-; CHECK-NEXT:    ret { i8, i1 } { i8 undef, i1 false }
+; CHECK-NEXT:    ret { i8, i1 } { i8 -1, i1 false }
 ;
   %t = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 undef, i8 -10)
   ret {i8, i1} %t
@@ -215,7 +215,7 @@ define {i8, i1} @ssub_5() nounwind {
 
 define {i8, i1} @ssub_undef() nounwind {
 ; CHECK-LABEL: @ssub_undef(
-; CHECK-NEXT:    ret { i8, i1 } { i8 undef, i1 false }
+; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
 ;
   %t = call {i8, i1} @llvm.ssub.with.overflow.i8(i8 undef, i8 -10)
   ret {i8, i1} %t


        


More information about the llvm-commits mailing list