<div dir="ltr">Hmm, I believe you are right there.<div><br></div><div>There is no representable respective positive value for INT_MIN in i32 , which means the transformation is not possible in this case (in particular seems ConstantExpr::getNeg(C) in this case == C itself).</div>
<div><br></div><div>It should be checked that C->isMinValue(true) does not return true before applying the optimization I believe (C should be always a ConstantInt) .</div><div>I'm not an expert on this though :)</div>
<div><br></div><div>Marcello</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-07-01 23:01 GMT+02:00 David Menendez <span dir="ltr"><<a href="mailto:davemm@cs.rutgers.edu" target="_blank">davemm@cs.rutgers.edu</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I've found what appears to be a bug in instcombine. Specifically, the transformation of -(X/C) to X/(-C) is invalid if C == INT_MIN.<br>

<br>
Specifically, if I have<br>
<br>
> define i32 @foo(i32 %x) #0 {<br>
> entry:<br>
>  %div = sdiv i32 %x, -<a href="tel:2147483648" value="+12147483648">2147483648</a><br>
>  %sub = sub nsw i32 0, %div<br>
>  ret i32 %sub<br>
> }<br>
<br>
then opt -instcombine will produce<br>
<br>
> define i32 @foo(i32 %x) #0 {<br>
> entry:<br>
>  %sub = sdiv i32 %x, -2147483648<br>
>  ret i32 %sub<br>
> }<br>
<br>
<br>
You can observe this with the following test case:<br>
<br>
> #include <stdio.h><br>
> #include <limits.h><br>
><br>
> int foo(int x)<br>
> {<br>
>  return -(x/INT_MIN);<br>
> }<br>
><br>
> int main (void)<br>
> {<br>
>  printf ("%d\n", foo(INT_MIN));<br>
> }<br>
<br>
This will print -1 or 1, depending on the optimization level.<br>
<br>
This appears to be the relevant code:<br>
<br>
InstCombineAddSub.cpp:1556<br>
>     // 0 - (X sdiv C)  -> (X sdiv -C)<br>
>     if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) &&<br>
>         match(Op0, m_Zero()))<br>
>       return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));<br>
<br>
- David Menendez<br>
<br>
<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</blockquote></div><br></div>