I tried to fix this but I am not sure about few things.
- We always convert (X * C1) + (X * C2) to X *(C1 + C2) even if (C1 + C2)
%1 = mul nsw i32 %x, 2147483647 %2 = mul nsw i32 %x, 3 %3 = add nsw i32 %1, %2
%1 = mul i32 %x, -2147483646
Is this what we want. In that case, we can blindly copy nsw from
add instruction to mul instruction. If so, I will update this patch.
- In this patch, I have tried not to combine add to mul for above case
but that is not enough to stop this conversion (getting converted from
some other code). If above conversion is wrong, I can go through code
and fix this. For now, this patch should take care of not dropping nsw
- I haven't added similar checks in visitSub. Actually, all c examples,
I can come up with, was not creating sub with nsw. If is it required to
add similar check, I can update this patch.