diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index 30880e1969bdfc..c9754f5437c450 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -5217,13 +5217,10 @@ bool Lowering::LowerUnsignedDivOrMod(GenTreeOp* divMod) unsigned bits = type == TYP_INT ? 32 : 64; // if the dividend operand is AND or RSZ with a constant then the number of input bits can be reduced - if (dividend->OperIs(GT_AND)) + if (dividend->OperIs(GT_AND) && dividend->gtGetOp2()->IsCnsIntOrI()) { - size_t maskCns = static_cast( - dividend->gtGetOp1()->IsCnsIntOrI() - ? dividend->gtGetOp1()->AsIntCon()->IconValue() - : dividend->gtGetOp2()->IsCnsIntOrI() ? dividend->gtGetOp2()->AsIntCon()->IconValue() : 0); - if (maskCns) + size_t maskCns = static_cast(dividend->gtGetOp2()->AsIntCon()->IconValue()); + if (maskCns != 0) { unsigned maskBits = 1; while (maskCns >>= 1) @@ -5236,7 +5233,9 @@ bool Lowering::LowerUnsignedDivOrMod(GenTreeOp* divMod) { size_t shiftCns = static_cast(dividend->gtGetOp2()->AsIntCon()->IconValue()); if (shiftCns < bits) + { bits -= static_cast(shiftCns); + } } if (type == TYP_INT) diff --git a/src/coreclr/jit/utils.cpp b/src/coreclr/jit/utils.cpp index d5b7da8c829795..e454eb338682bc 100644 --- a/src/coreclr/jit/utils.cpp +++ b/src/coreclr/jit/utils.cpp @@ -2417,6 +2417,7 @@ T GetUnsignedMagic(T d, bool* increment /*out*/, int* preShift /*out*/, int* pos uint32_t GetUnsigned32Magic( uint32_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits) { + assert(bits <= 32); return GetUnsignedMagic(d, increment, preShift, postShift, bits); } @@ -2424,6 +2425,7 @@ uint32_t GetUnsigned32Magic( uint64_t GetUnsigned64Magic( uint64_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits) { + assert(bits <= 64); return GetUnsignedMagic(d, increment, preShift, postShift, bits); } #endif diff --git a/src/coreclr/jit/utils.h b/src/coreclr/jit/utils.h index 6ed392ab2b16f9..44521010288426 100644 --- a/src/coreclr/jit/utils.h +++ b/src/coreclr/jit/utils.h @@ -762,10 +762,10 @@ class CritSecHolder namespace MagicDivide { uint32_t GetUnsigned32Magic( - uint32_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits = 32); + uint32_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits); #ifdef TARGET_64BIT uint64_t GetUnsigned64Magic( - uint64_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits = 64); + uint64_t d, bool* increment /*out*/, int* preShift /*out*/, int* postShift /*out*/, unsigned bits); #endif int32_t GetSigned32Magic(int32_t d, int* shift /*out*/); #ifdef TARGET_64BIT