Skip to content

Commit

Permalink
[InstCombine] Fold srem(X, PowerOf2) == C into (X & Mask) == C for po…
Browse files Browse the repository at this point in the history
…sitive C

This diff extends InstCombinerImpl::foldICmpSRemConstant to handle the cases
srem(X, PowerOf2) == C and
srem(X, PowerOf2) != C
for positive C.
This addresses the issue #54650

Differential revision: https://reviews.llvm.org/D122942

Test plan: make check-all
  • Loading branch information
alexander-shaposhnikov committed Apr 3, 2022
1 parent 911cfcd commit 6cf10b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Expand Up @@ -2335,7 +2335,8 @@ Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
// 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)
if (Pred != ICmpInst::ICMP_SGT && Pred != ICmpInst::ICMP_SLT &&
Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
return nullptr;

// TODO: The one-use check is standard because we do not typically want to
Expand All @@ -2345,7 +2346,15 @@ Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
return nullptr;

const APInt *DivisorC;
if (!C.isZero() || !match(SRem->getOperand(1), m_Power2(DivisorC)))
if (!match(SRem->getOperand(1), m_Power2(DivisorC)))
return nullptr;

// For cmp_sgt/cmp_slt only zero valued C is handled.
// For cmp_eq/cmp_ne only positive valued C is handled.
if (((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) &&
!C.isZero()) ||
((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
!C.isStrictlyPositive()))
return nullptr;

// Mask off the sign bit and the modulo bits (low-bits).
Expand All @@ -2354,6 +2363,9 @@ Instruction *InstCombinerImpl::foldICmpSRemConstant(ICmpInst &Cmp,
Constant *MaskC = ConstantInt::get(Ty, SignMask | (*DivisorC - 1));
Value *And = Builder.CreateAnd(SRem->getOperand(0), MaskC);

if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
return new ICmpInst(Pred, And, ConstantInt::get(Ty, C));

// For 'is positive?' check that the sign-bit is clear and at least 1 masked
// bit is set. Example:
// (i8 X % 32) s> 0 --> (X & 159) s> 0
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/InstCombine/rem.ll
Expand Up @@ -742,7 +742,7 @@ define i1 @test28(i32 %A) {

define i1 @positive_and_odd_eq(i32 %A) {
; CHECK-LABEL: @positive_and_odd_eq(
; CHECK-NEXT: [[B:%.*]] = srem i32 [[A:%.*]], 2
; CHECK-NEXT: [[B:%.*]] = and i32 [[A:%.*]], -2147483647
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[B]], 1
; CHECK-NEXT: ret i1 [[C]]
;
Expand All @@ -764,7 +764,7 @@ define i1 @negative_and_odd_eq(i32 %A) {

define i1 @positive_and_odd_ne(i32 %A) {
; CHECK-LABEL: @positive_and_odd_ne(
; CHECK-NEXT: [[B:%.*]] = srem i32 [[A:%.*]], 2
; CHECK-NEXT: [[B:%.*]] = and i32 [[A:%.*]], -2147483647
; CHECK-NEXT: [[C:%.*]] = icmp ne i32 [[B]], 1
; CHECK-NEXT: ret i1 [[C]]
;
Expand Down

0 comments on commit 6cf10b7

Please sign in to comment.