Skip to content

Commit d687057

Browse files
committed
[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 llvm#71659.
1 parent dc2cfd0 commit d687057

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,13 @@ static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
910910
assert(SDI->getOpcode() == Instruction::SDiv);
911911
assert(!SDI->getType()->isVectorTy());
912912

913+
// Check whether the division folds to a constant.
914+
if (const APInt *Elem = LCR.sdiv(RCR).getSingleElement()) {
915+
SDI->replaceAllUsesWith(ConstantInt::get(SDI->getType(), *Elem));
916+
SDI->eraseFromParent();
917+
return true;
918+
}
919+
913920
struct Operand {
914921
Value *V;
915922
Domain D;

llvm/test/Transforms/CorrelatedValuePropagation/sdiv.ll

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,7 @@ define void @sdiv_zero(ptr %p, i32 %arg) {
638638
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], 5
639639
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 11
640640
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
641-
; CHECK-NEXT: [[DIV_LHS_TRUNC:%.*]] = trunc i32 [[ARG]] to i8
642-
; CHECK-NEXT: [[DIV1:%.*]] = sdiv i8 [[DIV_LHS_TRUNC]], 6
643-
; CHECK-NEXT: [[DIV_SEXT:%.*]] = sext i8 [[DIV1]] to i32
644-
; CHECK-NEXT: store i32 [[DIV_SEXT]], ptr [[P:%.*]], align 4
641+
; CHECK-NEXT: store i32 0, ptr [[P:%.*]], align 4
645642
; CHECK-NEXT: ret void
646643
;
647644
%add = add i32 %arg, 5
@@ -676,10 +673,7 @@ define void @sdiv_pos(ptr %p, i32 %arg) {
676673
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], -12
677674
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 6
678675
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
679-
; CHECK-NEXT: [[DIV1_LHS_TRUNC:%.*]] = trunc i32 [[ARG]] to i8
680-
; CHECK-NEXT: [[DIV12:%.*]] = udiv i8 [[DIV1_LHS_TRUNC]], 6
681-
; CHECK-NEXT: [[DIV1_ZEXT:%.*]] = zext i8 [[DIV12]] to i32
682-
; CHECK-NEXT: store i32 [[DIV1_ZEXT]], ptr [[P:%.*]], align 4
676+
; CHECK-NEXT: store i32 2, ptr [[P:%.*]], align 4
683677
; CHECK-NEXT: ret void
684678
;
685679
%add = add i32 %arg, -12
@@ -695,12 +689,7 @@ define void @sdiv_neg(ptr %p, i32 %arg) {
695689
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ARG:%.*]], 17
696690
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 6
697691
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
698-
; CHECK-NEXT: [[ARG_NONNEG:%.*]] = sub i32 0, [[ARG]]
699-
; CHECK-NEXT: [[DIV1_LHS_TRUNC:%.*]] = trunc i32 [[ARG_NONNEG]] to i8
700-
; CHECK-NEXT: [[DIV12:%.*]] = udiv i8 [[DIV1_LHS_TRUNC]], 6
701-
; CHECK-NEXT: [[DIV1_ZEXT:%.*]] = zext i8 [[DIV12]] to i32
702-
; CHECK-NEXT: [[DIV1_NEG:%.*]] = sub i32 0, [[DIV1_ZEXT]]
703-
; CHECK-NEXT: store i32 [[DIV1_NEG]], ptr [[P:%.*]], align 4
692+
; CHECK-NEXT: store i32 -2, ptr [[P:%.*]], align 4
704693
; CHECK-NEXT: ret void
705694
;
706695
%add = add i32 %arg, 17

0 commit comments

Comments
 (0)