[LLVMdev] Surprising SCEV / Constant folding result

Chris Lattner clattner at apple.com
Fri Dec 28 12:54:05 PST 2012


The SCCP pass should recognize the "always zero" case. It may be taught the identity that 0*x == 0.

-Chris

On Dec 28, 2012, at 5:28 PM, Jonas Wagner <jonas.wagner at epfl.ch> wrote:

> Hello,
> 
> I've tested LLVM's ScalarEvolution analysis with different small examples. Here's a result that surprised me:
> 
> void dont_optimize_away(int);
> 
> void foo(int n) {
>   int res = 0;
>   for (int i = 0; i < n; ++i) {
>     // The kind of things that SCEV can recognize is quite astounding...  
>     // It wins over gcc for this one.
>     //res = res + i*i;
> 
>     // Also, the kind of things it can't recognize...
>     res = res * i;
>   }
>   dont_optimize_away(res);
> }
> 
> // Compile using
> $ clang -Wall -std=c99 -O2 -c clang_scev_surprise.c -S -o - -emit-llvm
> 
> 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?
> 
> 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.
> 
> Cheers,
> Jonas
> 
> <clang_scev_surprise.c>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121228/d051b8d9/attachment.html>


More information about the llvm-dev mailing list