[PATCH] D140405: [CVP] Simplify SRem when constantrange abs(lhs) < abs(rhs)

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 20 08:07:34 PST 2022


StephenFan created this revision.
StephenFan added reviewers: nikic, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
StephenFan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For `srem x, y`, if abs(constant range of x) less than abs(constant
range of y), we can simplify it as:
`srem x, y => x` if y is guaranteed to be positive.
'srem x, y => -x' if y is guaranteed to be negative.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140405

Files:
  llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
  llvm/test/Transforms/CorrelatedValuePropagation/srem.ll


Index: llvm/test/Transforms/CorrelatedValuePropagation/srem.ll
===================================================================
--- llvm/test/Transforms/CorrelatedValuePropagation/srem.ll
+++ llvm/test/Transforms/CorrelatedValuePropagation/srem.ll
@@ -523,8 +523,7 @@
 ; CHECK-NEXT:    [[AND_COND:%.*]] = and i1 [[AND_X]], [[Y_CMP]]
 ; CHECK-NEXT:    br i1 [[AND_COND]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:       if.else:
-; CHECK-NEXT:    [[REM:%.*]] = srem i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[REM]], [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X]], [[X]]
 ; CHECK-NEXT:    ret void
 ; CHECK:       if.then:
 ; CHECK-NEXT:    ret void
@@ -558,8 +557,7 @@
 ; CHECK-NEXT:    [[AND_COND:%.*]] = and i1 [[AND_X]], [[AND_Y]]
 ; CHECK-NEXT:    br i1 [[AND_COND]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:       if.else:
-; CHECK-NEXT:    [[REM:%.*]] = srem i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[REM]], [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X]], [[X]]
 ; CHECK-NEXT:    ret void
 ; CHECK:       if.then:
 ; CHECK-NEXT:    ret void
@@ -595,8 +593,8 @@
 ; CHECK-NEXT:    [[AND_COND:%.*]] = and i1 [[AND_X]], [[AND_Y]]
 ; CHECK-NEXT:    br i1 [[AND_COND]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:       if.else:
-; CHECK-NEXT:    [[REM:%.*]] = srem i8 [[X]], [[Y]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[REM]], [[X]]
+; CHECK-NEXT:    [[X_NEG:%.*]] = sub i8 0, [[X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X_NEG]], [[X]]
 ; CHECK-NEXT:    ret void
 ; CHECK:       if.then:
 ; CHECK-NEXT:    ret void
Index: llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -843,6 +843,25 @@
   if (SDI->getType()->isVectorTy())
     return false;
 
+  ConstantRange LCR = LVI->getConstantRange(SDI->getOperand(0), SDI);
+  ConstantRange RCR = LVI->getConstantRange(SDI->getOperand(1), SDI);
+
+  if (LCR.abs().icmp(CmpInst::ICMP_ULT, RCR.abs())) {
+    Value *NewV = nullptr;
+    Domain Divisor = getDomain(SDI->getOperand(1), LVI, SDI);
+    if (Divisor == Domain::NonNegative)
+      NewV = SDI->getOperand(0);
+    else if (Divisor == Domain::NonPositive)
+      NewV = BinaryOperator::CreateNeg(
+          SDI->getOperand(0), SDI->getOperand(0)->getName() + ".neg", SDI);
+
+    if (NewV) {
+      SDI->replaceAllUsesWith(NewV);
+      SDI->eraseFromParent();
+      return true;
+    }
+  }
+
   struct Operand {
     Value *V;
     Domain D;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140405.484279.patch
Type: text/x-patch
Size: 2659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221220/a210145b/attachment.bin>


More information about the llvm-commits mailing list