[llvm] d687057 - [CVP] Try to fold sdiv to constant
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 8 05:49:42 PST 2023
Author: Nikita Popov
Date: 2023-11-08T14:49:24+01:00
New Revision: d687057de8babc215d1cc883514085704ede5ab4
URL: https://github.com/llvm/llvm-project/commit/d687057de8babc215d1cc883514085704ede5ab4
DIFF: https://github.com/llvm/llvm-project/commit/d687057de8babc215d1cc883514085704ede5ab4.diff
LOG: [CVP] Try to fold sdiv to constant
If we know that the sdiv result is a single constant, directly
use that instead of performing narrowing.
Fixes https://github.com/llvm/llvm-project/issues/71659.
Added:
Modified:
llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 99b4628cc68dd00..d58ea126f294b60 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -910,6 +910,13 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
assert(SDI->getOpcode() == Instruction::SDiv);
assert(!SDI->getType()->isVectorTy());
+ // Check whether the division folds to a constant.
+ if (const APInt *Elem = LCR.sdiv(RCR).getSingleElement()) {
+ SDI->replaceAllUsesWith(ConstantInt::get(SDI->getType(), *Elem));
+ SDI->eraseFromParent();
+ return true;
+ }
+
struct Operand {
Value *V;
Domain D;
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll b/llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll
index 470e99d31ef81db..d88fe358a0aa2cd 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll
@@ -638,10 +638,7 @@ define void @sdiv_zero(ptr %p, i32 %arg) {
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], 5
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 11
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
-; CHECK-NEXT: [[DIV_LHS_TRUNC:%.*]] = trunc i32 [[ARG]] to i8
-; CHECK-NEXT: [[DIV1:%.*]] = sdiv i8 [[DIV_LHS_TRUNC]], 6
-; CHECK-NEXT: [[DIV_SEXT:%.*]] = sext i8 [[DIV1]] to i32
-; CHECK-NEXT: store i32 [[DIV_SEXT]], ptr [[P:%.*]], align 4
+; CHECK-NEXT: store i32 0, ptr [[P:%.*]], align 4
; CHECK-NEXT: ret void
;
%add = add i32 %arg, 5
@@ -676,10 +673,7 @@ define void @sdiv_pos(ptr %p, i32 %arg) {
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], -12
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 6
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
-; CHECK-NEXT: [[DIV1_LHS_TRUNC:%.*]] = trunc i32 [[ARG]] to i8
-; CHECK-NEXT: [[DIV12:%.*]] = udiv i8 [[DIV1_LHS_TRUNC]], 6
-; CHECK-NEXT: [[DIV1_ZEXT:%.*]] = zext i8 [[DIV12]] to i32
-; CHECK-NEXT: store i32 [[DIV1_ZEXT]], ptr [[P:%.*]], align 4
+; CHECK-NEXT: store i32 2, ptr [[P:%.*]], align 4
; CHECK-NEXT: ret void
;
%add = add i32 %arg, -12
@@ -695,12 +689,7 @@ define void @sdiv_neg(ptr %p, i32 %arg) {
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], 17
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 6
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
-; CHECK-NEXT: [[ARG_NONNEG:%.*]] = sub i32 0, [[ARG]]
-; CHECK-NEXT: [[DIV1_LHS_TRUNC:%.*]] = trunc i32 [[ARG_NONNEG]] to i8
-; CHECK-NEXT: [[DIV12:%.*]] = udiv i8 [[DIV1_LHS_TRUNC]], 6
-; CHECK-NEXT: [[DIV1_ZEXT:%.*]] = zext i8 [[DIV12]] to i32
-; CHECK-NEXT: [[DIV1_NEG:%.*]] = sub i32 0, [[DIV1_ZEXT]]
-; CHECK-NEXT: store i32 [[DIV1_NEG]], ptr [[P:%.*]], align 4
+; CHECK-NEXT: store i32 -2, ptr [[P:%.*]], align 4
; CHECK-NEXT: ret void
;
%add = add i32 %arg, 17
More information about the llvm-commits
mailing list