Skip to content

Commit 79c3038

Browse files
committed
[InstCombine] Handle commuted pattern for ((X s/ C1) << C2) + X
1 parent 01055b1 commit 79c3038

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,12 +1625,13 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
16251625

16261626
// ((X s/ C1) << C2) + X => X s% -C1 where -C1 is 1 << C2
16271627
const APInt *C1, *C2;
1628-
if (match(LHS, m_Shl(m_SDiv(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))) {
1628+
if (match(&I, m_c_Add(m_Shl(m_SDiv(m_Value(A), m_APInt(C1)), m_APInt(C2)),
1629+
m_Deferred(A)))) {
16291630
APInt one(C2->getBitWidth(), 1);
16301631
APInt minusC1 = -(*C1);
16311632
if (minusC1 == (one << *C2)) {
1632-
Constant *NewRHS = ConstantInt::get(RHS->getType(), minusC1);
1633-
return BinaryOperator::CreateSRem(RHS, NewRHS);
1633+
Constant *NewRHS = ConstantInt::get(A->getType(), minusC1);
1634+
return BinaryOperator::CreateSRem(A, NewRHS);
16341635
}
16351636
}
16361637

llvm/test/Transforms/InstCombine/add-shl-sdiv-to-srem.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ define i8 @add-shl-sdiv-scalar0(i8 %x) {
1414

1515
define i8 @add-shl-sdiv-scalar0_commuted(i8 %x) {
1616
; CHECK-LABEL: @add-shl-sdiv-scalar0_commuted(
17-
; CHECK-NEXT: [[SD:%.*]] = sdiv i8 [[X:%.*]], -4
18-
; CHECK-NEXT: [[SL:%.*]] = shl i8 [[SD]], 2
19-
; CHECK-NEXT: [[RZ:%.*]] = add i8 [[X]], [[SL]]
17+
; CHECK-NEXT: [[RZ:%.*]] = srem i8 [[X:%.*]], 4
2018
; CHECK-NEXT: ret i8 [[RZ]]
2119
;
2220
%sd = sdiv i8 %x, -4

0 commit comments

Comments
 (0)