<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>The SCCP pass should recognize the "always zero" case. It may be taught the identity that 0*x == 0.</div><div><br><div>-Chris</div></div><div><br>On Dec 28, 2012, at 5:28 PM, Jonas Wagner <<a href="mailto:jonas.wagner@epfl.ch">jonas.wagner@epfl.ch</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hello,<br><div class="gmail_quote"><div dir="ltr"><div><br></div><div>I've tested LLVM's ScalarEvolution analysis with different small examples. Here's a result that surprised me:</div><div><br></div>
<div><div><font face="courier new, monospace">void dont_optimize_away(int);</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">void foo(int n) {</font></div><div><font face="courier new, monospace"> int res = 0;</font></div><div><font face="courier new, monospace"> for (int i = 0; i < n; ++i) {</font></div>
<div><font face="courier new, monospace"> // The kind of things that SCEV can recognize is quite astounding... </font></div><div><font face="courier new, monospace"> // It wins over gcc for this one.</font></div><div>
<font face="courier new, monospace"> //res = res + i*i;</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> // Also, the kind of things it can't recognize...</font></div>
<div><font face="courier new, monospace"> res = res * i;</font></div><div><font face="courier new, monospace"> }</font></div><div><font face="courier new, monospace"> dont_optimize_away(res);</font></div><div><font face="courier new, monospace">}</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">// Compile using</font></div><div><font face="courier new, monospace">$ clang -Wall -std=c99 -O2 -c clang_scev_surprise.c -S -o - -emit-llvm</font></div>
<div><br></div><div>In this loop, the value res is always zero, yet LLVM does not optimize the loop away at -O2 (contrary to GCC). Is there a simple explanation for this?</div><div><br></div><div>Is this a case that is general enough that we could include it in LLVM? It looks like something that ScalarEvolution could recognize. Or maybe we could constant-fold the particular pattern of having res = phi(0, res) as operand in a multiplication.</div>
<div><br></div><div>Cheers,<br>Jonas</div></div></div>
</div><br></div>
</div></blockquote><blockquote type="cite"><div><clang_scev_surprise.c></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>LLVM Developers mailing list</span><br><span><a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a></span><br><span><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a></span><br></div></blockquote></body></html>