diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1763,7 +1763,9 @@ static Instruction *simplifyIRemMulShl(BinaryOperator &I, InstCombinerImpl &IC) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *X = nullptr; + APInt const *MatchY, *MatchZ; APInt Y, Z; + bool ShiftX = false; // If V is not nullptr, it will be matched using m_Specific. auto MatchShiftOrMul = [](Value *Op, Value *&V, APInt &C) -> bool { @@ -1777,8 +1779,16 @@ return Tmp != nullptr; }; - if (!MatchShiftOrMul(Op0, X, Y) || !MatchShiftOrMul(Op1, X, Z)) + if (MatchShiftOrMul(Op0, X, Y) && MatchShiftOrMul(Op1, X, Z)) { + // pass + } else if (match(Op0, m_Shl(m_APInt(MatchY), m_Value(X))) && + match(Op1, m_Shl(m_APInt(MatchZ), m_Specific(X)))) { + Y = *MatchY; + Z = *MatchZ; + ShiftX = true; + } else { return nullptr; + } bool IsSRem = I.getOpcode() == Instruction::SRem; @@ -1796,6 +1806,11 @@ if (RemYZ.isZero() && BO0NoWrap) return IC.replaceInstUsesWith(I, ConstantInt::getNullValue(I.getType())); + auto GetBinOpOut = [&](Value *RemSimplification) -> BinaryOperator * { + return ShiftX ? BinaryOperator::CreateShl(RemSimplification, X) + : BinaryOperator::CreateMul(X, RemSimplification); + }; + OverflowingBinaryOperator *BO1 = cast(Op1); bool BO1HasNSW = BO1->hasNoSignedWrap(); bool BO1HasNUW = BO1->hasNoUnsignedWrap(); @@ -1804,8 +1819,7 @@ // if (rem Y, Z) == Y // -> (mul nuw/nsw X, Y) if (RemYZ == Y && BO1NoWrap) { - BinaryOperator *BO = - BinaryOperator::CreateMul(X, ConstantInt::get(I.getType(), Y)); + BinaryOperator *BO = GetBinOpOut(ConstantInt::get(I.getType(), Y)); // Copy any overflow flags from Op0. BO->setHasNoSignedWrap(IsSRem || BO0HasNSW); BO->setHasNoUnsignedWrap(!IsSRem || BO0HasNUW); @@ -1816,8 +1830,7 @@ // if Y >= Z // -> (mul {nuw} nsw X, (rem Y, Z)) if (Y.uge(Z) && (IsSRem ? (BO0HasNSW && BO1HasNSW) : BO0HasNUW)) { - BinaryOperator *BO = - BinaryOperator::CreateMul(X, ConstantInt::get(I.getType(), RemYZ)); + BinaryOperator *BO = GetBinOpOut(ConstantInt::get(I.getType(), RemYZ)); BO->setHasNoSignedWrap(); BO->setHasNoUnsignedWrap(BO0HasNUW); return BO; diff --git a/llvm/test/Transforms/InstCombine/rem-mul-shl.ll b/llvm/test/Transforms/InstCombine/rem-mul-shl.ll --- a/llvm/test/Transforms/InstCombine/rem-mul-shl.ll +++ b/llvm/test/Transforms/InstCombine/rem-mul-shl.ll @@ -51,10 +51,7 @@ define i8 @urem_XY_XZ_with_CY_rem_CZ_eq_0_with_shl(i8 %X) { ; CHECK-LABEL: @urem_XY_XZ_with_CY_rem_CZ_eq_0_with_shl( -; CHECK-NEXT: [[BO0:%.*]] = shl nuw i8 15, [[X:%.*]] -; CHECK-NEXT: [[BO1:%.*]] = shl i8 5, [[X]] -; CHECK-NEXT: [[R:%.*]] = urem i8 [[BO0]], [[BO1]] -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %BO0 = shl nuw i8 15, %X %BO1 = shl i8 5, %X @@ -88,9 +85,7 @@ define i8 @urem_XY_XZ_with_CY_lt_CZ_with_shl(i8 %X) { ; CHECK-LABEL: @urem_XY_XZ_with_CY_lt_CZ_with_shl( -; CHECK-NEXT: [[BO0:%.*]] = shl i8 3, [[X:%.*]] -; CHECK-NEXT: [[BO1:%.*]] = shl nuw i8 12, [[X]] -; CHECK-NEXT: [[R:%.*]] = urem i8 [[BO0]], [[BO1]] +; CHECK-NEXT: [[R:%.*]] = shl nuw i8 3, [[X:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %BO0 = shl i8 3, %X @@ -309,9 +304,7 @@ define <2 x i8> @srem_XY_XZ_with_CY_lt_CZ_with_nuw_out_with_shl(<2 x i8> %X) { ; CHECK-LABEL: @srem_XY_XZ_with_CY_lt_CZ_with_nuw_out_with_shl( -; CHECK-NEXT: [[BO0:%.*]] = shl nuw <2 x i8> , [[X:%.*]] -; CHECK-NEXT: [[BO1:%.*]] = shl nsw <2 x i8> , [[X]] -; CHECK-NEXT: [[R:%.*]] = srem <2 x i8> [[BO0]], [[BO1]] +; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i8> , [[X:%.*]] ; CHECK-NEXT: ret <2 x i8> [[R]] ; %BO0 = shl nuw <2 x i8> , %X