<div dir="ltr"><div>Looks broken to me; I don't think there's UB in the original program.<br><br></div>The fold in visitFAdd() should check if the sitofp is guaranteed to produce an exact result? Ie, if the int value input to the sitofp could possibly be different when converted back using fptosi, then the transform does not work.<br><br>define float @test(i32 %x) {<br>  %mul = mul i32 %x, 58<br>  %conv = sitofp i32 %mul to float<br>  %add = fadd float %conv, 1.0<br>  ret float %add<br>}<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 20, 2017 at 4:25 AM, Artur Pilipenko <span dir="ltr"><<a href="mailto:apilipenko@azul.com" target="_blank">apilipenko@azul.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This C program produces different results with -O0 and -O3 optimization levels.<br>
<br>
#include <stdio.h><br>
float test(unsigned int arg) {<br>
    return (float)((int)(arg * 58)) + 1;<br>
}<br>
int main() {<br>
    printf("%d\n", (int)test((unsigned int)-831710640));<br>
}<br>
<br>
O0 result is -994576896<br>
O3 result is -994576832<br>
<br>
It happens because LLVM (specifically instcombine) does the following transformation:<br>
(float)x + 1.0 => (float)(x + 1)<br>
<br>
For some values the expression before and after yield different results:<br>
                  x = -994576864<br>
         (float)x = -994576896.000000<br>
(float)x + 1.0 = -994576896.000000<br>
(float)(x + 1) = -994576832.000000<br>
<br>
I’m curious if this is a correct transformation and why.<br>
<span class="HOEnZb"><font color="#888888"><br>
Artur<br>
<br>
</font></span></blockquote></div><br></div>