[llvm] eccb9a7 - [InstCombine] fold exact sdiv to ashr (2nd try)
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 8 07:19:14 PDT 2022
Author: Sanjay Patel
Date: 2022-10-08T10:09:44-04:00
New Revision: eccb9a77c6e4222b385d3c214dd28fcc66938023
URL: https://github.com/llvm/llvm-project/commit/eccb9a77c6e4222b385d3c214dd28fcc66938023
DIFF: https://github.com/llvm/llvm-project/commit/eccb9a77c6e4222b385d3c214dd28fcc66938023.diff
LOG: [InstCombine] fold exact sdiv to ashr (2nd try)
The 1st attempt failed to updated the test checks as expected.
Original commit message:
sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative)
https://alive2.llvm.org/ce/z/kB6VF7
It would probably be better to use ValueTracking to replace this
and the existing transform above it, but the analysis does not
account for the no-wrap properly, and it's not immediately clear
to me how to fix it.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 69625c082c5c7..d9fcd436b00e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1199,6 +1199,11 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
return BinaryOperator::CreateExactAShr(Op0, C);
}
+ // sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative)
+ Value *ShAmt;
+ if (match(Op1, m_NSWShl(m_One(), m_Value(ShAmt))))
+ return BinaryOperator::CreateExactAShr(Op0, ShAmt);
+
// sdiv exact X, -1<<C --> -(ashr exact X, C)
if (match(Op1, m_NegatedPower2())) {
Constant *NegPow2C = ConstantExpr::getNeg(cast<Constant>(Op1));
diff --git a/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll b/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
index 6e30f877875d6..997910d171cdd 100644
--- a/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
+++ b/llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll
@@ -70,10 +70,11 @@ define <2 x i8> @n6_vec_negative(<2 x i8> %x) {
ret <2 x i8> %div
}
+; sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative)
+
define i8 @shl1_nsw(i8 %x, i8 %y) {
; CHECK-LABEL: @shl1_nsw(
-; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i8 1, [[Y:%.*]]
-; CHECK-NEXT: [[DIV:%.*]] = sdiv exact i8 [[X:%.*]], [[SHL]]
+; CHECK-NEXT: [[DIV:%.*]] = ashr exact i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[DIV]]
;
%shl = shl nsw i8 1, %y
@@ -81,6 +82,8 @@ define i8 @shl1_nsw(i8 %x, i8 %y) {
ret i8 %div
}
+; negative test - must have nsw
+
define i8 @shl1_nuw(i8 %x, i8 %y) {
; CHECK-LABEL: @shl1_nuw(
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i8 1, [[Y:%.*]]
@@ -92,6 +95,8 @@ define i8 @shl1_nuw(i8 %x, i8 %y) {
ret i8 %div
}
+; negative test - must have exact
+
define i8 @shl1_nsw_not_exact(i8 %x, i8 %y) {
; CHECK-LABEL: @shl1_nsw_not_exact(
; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i8 1, [[Y:%.*]]
More information about the llvm-commits
mailing list