Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1306,6 +1306,19 @@ // X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1) if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V); + // ((X / C1) << C2) + X => X % C1 + // where C1 = 1 << C2 + const APInt *C1, *C2; + if (match(LHS, m_Shl(m_SDiv(m_Value(A), m_APInt(C1)), m_APInt(C2)))) { + if ((RHS == A) && C1->abs().isPowerOf2() && C1->isNegative()) { + APInt one(C2->getBitWidth(), 1); + if ((C1->abs() == one.shl(*C2)) && C2->sgt(one)) { + Constant *NewRHS = ConstantInt::get(RHS->getType(), C1->abs()); + return replaceInstUsesWith(I, Builder.CreateSRem(RHS, NewRHS, "srem")); + } + } + } + // A+B --> A|B iff A and B have no bits set in common. if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT)) return BinaryOperator::CreateOr(LHS, RHS); Index: llvm/test/Transforms/InstCombine/icmp-div-constant.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-div-constant.ll +++ llvm/test/Transforms/InstCombine/icmp-div-constant.ll @@ -38,6 +38,19 @@ ret i1 %r } +define i1 @is_rem32_pos_decomposed_i8(i8 %x) { +; CHECK-LABEL: @is_rem32_pos_decomposed_i8( +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -97 +; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[TMP1]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %d = sdiv i8 %x, 32 + %m = mul nsw i8 %d, 32 + %s = sub nsw i8 %x, %m + %r = icmp eq i8 %s, 0 + ret i1 %r +} + ; i16 -32765 == 32771 == 0b1000000000000011 define i1 @is_rem4_neg_i16(i16 %x) {