Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,12 +1625,13 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {

// ((X s/ C1) << C2) + X => X s% -C1 where -C1 is 1 << C2
const APInt *C1, *C2;
if (match(LHS, m_Shl(m_SDiv(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))) {
if (match(&I, m_c_Add(m_Shl(m_SDiv(m_Value(A), m_APInt(C1)), m_APInt(C2)),
m_Deferred(A)))) {
APInt one(C2->getBitWidth(), 1);
APInt minusC1 = -(*C1);
if (minusC1 == (one << *C2)) {
Constant *NewRHS = ConstantInt::get(RHS->getType(), minusC1);
return BinaryOperator::CreateSRem(RHS, NewRHS);
Constant *NewRHS = ConstantInt::get(A->getType(), minusC1);
return BinaryOperator::CreateSRem(A, NewRHS);
}
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Transforms/InstCombine/add-shl-sdiv-to-srem.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ define i8 @add-shl-sdiv-scalar0(i8 %x) {
ret i8 %rz
}

define i8 @add-shl-sdiv-scalar0_commuted(i8 %x) {
; CHECK-LABEL: @add-shl-sdiv-scalar0_commuted(
; CHECK-NEXT: [[RZ:%.*]] = srem i8 [[X:%.*]], 4
; CHECK-NEXT: ret i8 [[RZ]]
;
%sd = sdiv i8 %x, -4
%sl = shl i8 %sd, 2
%rz = add i8 %x, %sl
ret i8 %rz
}

define i8 @add-shl-sdiv-scalar1(i8 %x) {
; CHECK-LABEL: @add-shl-sdiv-scalar1(
; CHECK-NEXT: [[RZ:%.*]] = srem i8 [[X:%.*]], 64
Expand Down
Loading