[llvm] 7cfc82f - [CVP] Use simpler urem expansion when LHS >= RHS (PR63330)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 19 08:16:58 PDT 2023


Author: Nikita Popov
Date: 2023-06-19T17:16:49+02:00
New Revision: 7cfc82f331182855e2634c21d242c3b418f84bd1

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

LOG: [CVP] Use simpler urem expansion when LHS >= RHS (PR63330)

In this case we don't need to emit the comparison and select.

This is papering over a weakness in CVP in that newly added
instructions don't get revisited. If they were revisited, the
icmp would be folded at that point.

However, even without that it makes sense to handle this explicitly,
because it avoids the need to insert freeze, which may prevent
further analysis of the operation by LVI.

Proofs: https://alive2.llvm.org/ce/z/quyBxp

Fixes https://github.com/llvm/llvm-project/issues/63330.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
    llvm/test/Transforms/CorrelatedValuePropagation/udiv-expansion.ll
    llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 488297110d6fa..49d11b790c7ff 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -763,7 +763,13 @@ static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
 
   IRBuilder<> B(Instr);
   Value *ExpandedOp;
-  if (IsRem) {
+  if (XCR.icmp(ICmpInst::ICMP_UGE, YCR)) {
+    // If X is between Y and 2*Y the result is known.
+    if (IsRem)
+      ExpandedOp = B.CreateNUWSub(X, Y);
+    else
+      ExpandedOp = ConstantInt::get(Instr->getType(), 1);
+  } else if (IsRem) {
     // NOTE: this transformation introduces two uses of X,
     //       but it may be undef so we must freeze it first.
     Value *FrozenX = X;

diff  --git a/llvm/test/Transforms/CorrelatedValuePropagation/udiv-expansion.ll b/llvm/test/Transforms/CorrelatedValuePropagation/udiv-expansion.ll
index 8b796f79e60d0..a2a767084fbff 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/udiv-expansion.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/udiv-expansion.ll
@@ -305,9 +305,7 @@ define i8 @known_uge(i8 noundef %x) {
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP_X_UPPER]])
 ; CHECK-NEXT:    [[CMP_X_LOWER:%.*]] = icmp uge i8 [[X]], 3
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP_X_LOWER]])
-; CHECK-NEXT:    [[DIV_CMP:%.*]] = icmp uge i8 [[X]], 3
-; CHECK-NEXT:    [[DIV:%.*]] = zext i1 [[DIV_CMP]] to i8
-; CHECK-NEXT:    ret i8 [[DIV]]
+; CHECK-NEXT:    ret i8 1
 ;
   %cmp.x.upper = icmp ult i8 %x, 6
   call void @llvm.assume(i1 %cmp.x.upper)

diff  --git a/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll b/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
index da38e5a269e6d..cd0ba2f189dc8 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
@@ -349,9 +349,7 @@ define i8 @known_uge(i8 noundef %x) {
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP_X_UPPER]])
 ; CHECK-NEXT:    [[CMP_X_LOWER:%.*]] = icmp uge i8 [[X]], 3
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP_X_LOWER]])
-; CHECK-NEXT:    [[REM_UREM:%.*]] = sub nuw i8 [[X]], 3
-; CHECK-NEXT:    [[REM_CMP:%.*]] = icmp ult i8 [[X]], 3
-; CHECK-NEXT:    [[REM:%.*]] = select i1 [[REM_CMP]], i8 [[X]], i8 [[REM_UREM]]
+; CHECK-NEXT:    [[REM:%.*]] = sub nuw i8 [[X]], 3
 ; CHECK-NEXT:    ret i8 [[REM]]
 ;
   %cmp.x.upper = icmp ult i8 %x, 6


        


More information about the llvm-commits mailing list