Open huoyaoyuan opened 2 months ago
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.
BTW the following method hits assert in JIT, when viewing cross compile result for 32bit platforms in Disamo:
public static long Mul(int a, long b)
{
return checked((long)a * (long)b);
}
It also reproduces for checked add.
Generally, we should be able to check the operands of checked addition/multiplication, and turn the node to unchecked if we can prove no overflow can happen.
Hi, I'm interested in taking a look at this issue, what techniques can be used to generally prove no overflow can happen?
It can be much more complex than you may think. I have already solved this particular case, but haven't decided the best way to solve all the similar cases.
Recognizing upcast in operand of multiplication is straight, but too limited. The JIT has a mechanism to track the possible value range of numbers.
For the following code:
Currently on x64 it emits:
There's an overflow check for the IMUL. However on other architectures, including x86/arm/arm64, there's no overflow check.
ARM64:
ARM32:
x86:
My guess is that x64 is the only platform that supports 64bit*64bit=128bit operations, thus long mul isn't decomposed into MulLow and MulHigh, and fail to discover that the overflow check is unnecessary.