Skip to content

Commit 31a6f4a

Browse files
committed
[InstCombine] Move the logic into foldAddLikeCommutative
1 parent 79c3038 commit 31a6f4a

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,18 @@ Instruction *InstCombinerImpl::foldAddLikeCommutative(Value *LHS, Value *RHS,
13261326
R->setHasNoUnsignedWrap(NUWOut);
13271327
return R;
13281328
}
1329+
1330+
// ((X s/ C1) << C2) + X => X s% -C1 where -C1 is 1 << C2
1331+
const APInt *C1, *C2;
1332+
if (match(LHS, m_Shl(m_SDiv(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))) {
1333+
APInt one(C2->getBitWidth(), 1);
1334+
APInt minusC1 = -(*C1);
1335+
if (minusC1 == (one << *C2)) {
1336+
Constant *NewRHS = ConstantInt::get(RHS->getType(), minusC1);
1337+
return BinaryOperator::CreateSRem(RHS, NewRHS);
1338+
}
1339+
}
1340+
13291341
return nullptr;
13301342
}
13311343

@@ -1623,18 +1635,7 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
16231635
// X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1)
16241636
if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V);
16251637

1626-
// ((X s/ C1) << C2) + X => X s% -C1 where -C1 is 1 << C2
1627-
const APInt *C1, *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)))) {
1630-
APInt one(C2->getBitWidth(), 1);
1631-
APInt minusC1 = -(*C1);
1632-
if (minusC1 == (one << *C2)) {
1633-
Constant *NewRHS = ConstantInt::get(A->getType(), minusC1);
1634-
return BinaryOperator::CreateSRem(A, NewRHS);
1635-
}
1636-
}
1637-
1638+
const APInt *C1;
16381639
// (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit
16391640
if (match(&I, m_c_Add(m_And(m_Value(A), m_APInt(C1)), m_Deferred(A))) &&
16401641
C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countl_zero())) {

0 commit comments

Comments
 (0)