[PATCH] D138360: [CVP] Eliminate urem when LHS < RHS

Joshua Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 19 12:12:47 PST 2022


caojoshua created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
caojoshua added a comment.
caojoshua edited the summary of this revision.
caojoshua added a reviewer: majnemer.
caojoshua updated this revision to Diff 476697.
caojoshua published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Most arithmetic optimizations that use ConstantRange data defer logic to
ConstantRange::binaryOp. ConstantRange::binaryOp can determine that
[0, 12) % [13, ...) -> [0, 12). If X is the LHS, the urem is just X, but
ConstantRange::binaryOp does not know that.

We can do this in either SCCP or CVP. I don't think we can do this in InstCombine or InstSimplify because they don't have ConstantRange data.


caojoshua added a comment.

Check for vector type


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

X % Y -> X when X < Y


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138360

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


Index: llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
===================================================================
--- llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
+++ llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
@@ -152,6 +152,22 @@
   ret void
 }
 
+declare void @llvm.assume(i1)
+
+define i16 @test7(i16 %x, i16 %y) {
+; CHECK-LABEL: @test7(
+; CHECK: ret i16 %x
+;
+  %above_range = icmp uge i16 %y, 13
+  call void @llvm.assume(i1 %above_range)
+
+  %below_range = icmp ult i16 %x, 13
+  call void @llvm.assume(i1 %below_range)
+
+  %r = urem i16 %x, %y
+  ret i16 %r
+}
+
 define void @non_power_of_2(i24 %n) {
 ; CHECK-LABEL: @non_power_of_2(
 ; CHECK-NEXT:    [[DIV:%.*]] = urem i24 [[N:%.*]], 42
Index: llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -768,9 +768,26 @@
   return true;
 }
 
+static bool processURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
+  assert(Instr->getOpcode() == Instruction::URem);
+  if (Instr->getType()->isVectorTy())
+    return false;
+
+  // X % Y -> X for X < Y
+  if (LVI->getConstantRange(Instr->getOperand(0), Instr)
+          .getUnsignedMax()
+          .ult(LVI->getConstantRange(Instr->getOperand(1), Instr)
+                   .getUnsignedMin())) {
+    Instr->replaceAllUsesWith(Instr->getOperand(0));
+    Instr->eraseFromParent();
+    return true;
+  }
+  return false;
+}
+
 /// Try to shrink a udiv/urem's width down to the smallest power of two that's
 /// sufficient to contain its operands.
-static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
+static bool narrowUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
   assert(Instr->getOpcode() == Instruction::UDiv ||
          Instr->getOpcode() == Instruction::URem);
   if (Instr->getType()->isVectorTy())
@@ -812,6 +829,18 @@
   return true;
 }
 
+static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
+  assert(Instr->getOpcode() == Instruction::UDiv ||
+         Instr->getOpcode() == Instruction::URem);
+  if (Instr->getType()->isVectorTy())
+    return false;
+
+  if (Instr->getOpcode() == Instruction::URem && processURem(Instr, LVI))
+    return true;
+
+  return narrowUDivOrURem(Instr, LVI);
+}
+
 static bool processSRem(BinaryOperator *SDI, LazyValueInfo *LVI) {
   assert(SDI->getOpcode() == Instruction::SRem);
   if (SDI->getType()->isVectorTy())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138360.476697.patch
Type: text/x-patch
Size: 2571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221119/08c8f165/attachment.bin>


More information about the llvm-commits mailing list