Since signed integer overflow is undefined behaviour I expected the three functions below to compile to the same or similar assembly. This is however not the case. test2 is slightly different from test1 and test3 uses two imul instructions that are not required for the other examples.
int test1(int x)
{
return x * 5 / 2;
}
int test2(int x)
{
return x * 10 / 4;
}
int test3(int x)
{
return x * 50 / 20;
}
comparison on compiler explorer
Is there a reason why compilers don't perform this kind of optimizaion?