Skip to content

[InstCombine] fold unsigned predicates on srem result #122520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,10 +2674,41 @@ Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
BinaryOperator *SRem,
const APInt &C) {
const ICmpInst::Predicate Pred = Cmp.getPredicate();
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_ULT) {
// Canonicalize unsigned predicates to signed:
// (X s% DivisorC) u> C -> (X s% DivisorC) s< 0
// iff (C s< 0 ? ~C : C) u>= abs(DivisorC)-1
// (X s% DivisorC) u< C+1 -> (X s% DivisorC) s> -1
// iff (C+1 s< 0 ? ~C : C) u>= abs(DivisorC)-1

const APInt *DivisorC;
if (!match(SRem->getOperand(1), m_APInt(DivisorC)))
return nullptr;

APInt NormalizedC = C;
if (Pred == ICmpInst::ICMP_ULT) {
assert(!NormalizedC.isZero() &&
"ult X, 0 should have been simplified already.");
--NormalizedC;
}
if (C.isNegative())
NormalizedC.flipAllBits();
assert(!DivisorC->isZero() &&
"srem X, 0 should have been simplified already.");
if (!NormalizedC.uge(DivisorC->abs() - 1))
return nullptr;

Type *Ty = SRem->getType();
if (Pred == ICmpInst::ICMP_UGT)
return new ICmpInst(ICmpInst::ICMP_SLT, SRem,
ConstantInt::getNullValue(Ty));
return new ICmpInst(ICmpInst::ICMP_SGT, SRem,
ConstantInt::getAllOnesValue(Ty));
}
// Match an 'is positive' or 'is negative' comparison of remainder by a
// constant power-of-2 value:
// (X % pow2C) sgt/slt 0
const ICmpInst::Predicate Pred = Cmp.getPredicate();
if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
return nullptr;
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3018,6 +3018,32 @@ define i32 @floor_sdiv_wrong_op(i32 %x, i32 %y) {
ret i32 %r
}

define i32 @floor_sdiv_using_srem_by_8(i32 %x) {
; CHECK-LABEL: @floor_sdiv_using_srem_by_8(
; CHECK-NEXT: [[F:%.*]] = ashr i32 [[X:%.*]], 3
; CHECK-NEXT: ret i32 [[F]]
;
%d = sdiv i32 %x, 8
%r = srem i32 %x, 8
%i = icmp ugt i32 %r, -2147483648
%s = sext i1 %i to i32
%f = add i32 %d, %s
ret i32 %f
}

define i32 @floor_sdiv_using_srem_by_2(i32 %x) {
; CHECK-LABEL: @floor_sdiv_using_srem_by_2(
; CHECK-NEXT: [[F:%.*]] = ashr i32 [[X:%.*]], 1
; CHECK-NEXT: ret i32 [[F]]
;
%d = sdiv i32 %x, 2
%r = srem i32 %x, 2
%i = icmp ugt i32 %r, -2147483648
%s = sext i1 %i to i32
%f = add i32 %d, %s
ret i32 %f
}

; (X s>> (BW - 1)) + (zext (X s> 0)) --> (X s>> (BW - 1)) | (zext (X != 0))

define i8 @signum_i8_i8(i8 %x) {
Expand Down
Loading
Loading