<div dir="ltr">And that is less instructions. So from InstCombine's perspective the multiply is the correct answer. I think this transformation is better left to codegen where we know whether multiply or shift is truly better.</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div>
<br><div class="gmail_quote">On Wed, Sep 13, 2017 at 10:18 AM, Craig Topper <span dir="ltr"><<a href="mailto:craig.topper@gmail.com" target="_blank">craig.topper@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">There is in fact a transform out there somewhere that reverses yours.<div><br></div><div><div>define i64 @foo(i64 %a) {</div><div>  %b = shl i64 %a, 5</div><div>  %c = add i64 %b, %a</div><div>  ret i64 %c</div><div>}</div></div><div><br></div><div>becomes</div><div><br></div><div>







<p class="m_-7750826438740065135gmail-p1"><span class="m_-7750826438740065135gmail-s1">define i64 @foo(i64 %a) {</span></p><p class="m_-7750826438740065135gmail-p1"><span class="m_-7750826438740065135gmail-Apple-converted-space">  </span>%c = mul i64 %a, 33</p>
<p class="m_-7750826438740065135gmail-p1"><span class="m_-7750826438740065135gmail-s1"><span class="m_-7750826438740065135gmail-Apple-converted-space">  </span>ret i64 %c</span></p>
<p class="m_-7750826438740065135gmail-p1"><span class="m_-7750826438740065135gmail-s1">}</span></p></div></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><div class="m_-7750826438740065135gmail_signature" data-smartmail="gmail_signature">~Craig</div></div></font></span><div><div class="h5">
<br><div class="gmail_quote">On Wed, Sep 13, 2017 at 10:11 AM, Craig Topper <span dir="ltr"><<a href="mailto:craig.topper@gmail.com" target="_blank">craig.topper@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Your code seems fine. InstCombine can infinite loop if some other transform is reversing your transform. Can you send the whole patch and a test case?</div><div class="gmail_extra"><span class="m_-7750826438740065135HOEnZb"><font color="#888888"><br clear="all"><div><div class="m_-7750826438740065135m_7470635396692210463gmail_signature" data-smartmail="gmail_signature">~Craig</div></div></font></span><div><div class="m_-7750826438740065135h5">
<br><div class="gmail_quote">On Wed, Sep 13, 2017 at 10:01 AM, Haidl, Michael via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I am working on PR34474 and try to add a new optimization to<br>
InstCombine. Like in other parts of the visitMul function I add a Shl<br>
through the IR builder and create a new BinaryOp which I return from<br>
visitMul. If I understand correctly the new BinaryOp returned from<br>
visitMul should replace the original Instruction in the Worklist.<br>
However, I end up in an infinite loop and the Instruction I try to<br>
replace gets scheduled again and again. What is wrong in my code?<br>
<br>
// Replace X * (2^C+/-1) with (X << C) -/+ X<br>
APInt Plus1 = *IVal + 1;<br>
APInt Minus1 = *IVal - 1;<br>
int isPow2 = Plus1.isPowerOf2() ? 1 : Minus1.isPowerOf2() ? -1 : 0;<br>
<br>
if (isPow2) {<br>
   APInt &Pow2 = isPow2 > 0 ? Plus1 : Minus1;<br>
   Value *Shl = Builder.CreateShl(Op0, Pow2.logBase2());<br>
   return BinaryOperator::Create(isPow2 > 0 ? BinaryOperator::Sub :<br>
BinaryOperator::Add, Shl, Op0);<br>
}<br>
<br>
Thanks,<br>
Michael<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>