<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>