<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 3, 2014 at 2:08 AM, suyog sarda <span dir="ltr"><<a href="mailto:sardask01@gmail.com" target="_blank">sardask01@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div class="">On Mon, Jun 2, 2014 at 8:17 PM, Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">BTW, I was curious why the tests looked more complicated than needed. It turns out we were not just adding nsw based on WillNotOverflowSignedAdd. I changed that in r210029. It should allow you to simplify the tests further.</div>

<div><div>
<div class="gmail_extra"><br></div></div></div></blockquote><div><br></div></div><div>I am afraid if this is correct. Its adding 'nsw' to all additions, even where it is sure not to add 'nsw'.<br><br></div>
<div>
ex:-<br> <i><br>int foo(short x){<br>short v =  (x & ~4) ;<br>int u = (int)v + 8;<br>return u;<br>}<br></i> <br></div></div>In this example we cannot determine if the addition will overflow of not, because, one of the operand (x & ~4) has 0 bit at position 2 from LSB (LSB position considered 0) while other operand 8 has 1 bit at position 3, which is at higher significant position than position of 0. In this case its giving IR as 'add nsw' which it should not.<br>

<br></div></div></blockquote><div><br></div><div>Hi Rafael,  <br></div><div> </div><div>More info on above test case with your patch. <br></div></div><br><i><b>~$ llvm/llvm/build/bin/clang -O0 -S -emit-llvm 1.c -fwrapv</b><br>
<b>~$ llvm/llvm/build/bin/opt -S -mem2reg 1.ll -o 2.ll</b><br><b>~$ cat 2.ll</b><br><br>; ModuleID = '1.ll'<br>target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"<br>target triple = "i386-pc-linux-gnu"<br>
<br>; Function Attrs: nounwind<br>define i32 @foo(i16 signext %x) #0 {<br>  %1 = sext i16 %x to i32<br>  %2 = and i32 %1, -5<br>  %3 = trunc i32 %2 to i16<br>  %4 = sext i16 %3 to i32<br><b>  %5 = add i32 %4, 8</b><br>  ret i32 %5<br>
}<br><br><b>~$ llvm/llvm/build/bin/opt -S -instcombine 2.ll</b><br><br>; ModuleID = '2.ll'<br>target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"<br>target triple = "i386-pc-linux-gnu"<br>
<br>; Function Attrs: nounwind<br>define i32 @foo(i16 signext %x) #0 {<br>  %1 = and i16 %x, -5<br>  %2 = sext i16 %1 to i32<br><b>  %3 = add nsw i32 %2, 8</b><br>  ret i32 %3<br>}</i><br><br></div><div class="gmail_extra">
As we can see, add is being replaced by 'add nsw', which it should not. <br></div><div class="gmail_extra">Thanks !<br></div><div class="gmail_extra"><br><br>With regards,<br>Suyog Sarda<br>
</div></div>