[LLVMdev] Moving dependent code into conditional statements

Nicolas Capens nicolas at capens.net
Wed Oct 14 00:21:26 PDT 2009


Hi all,

 

Is there any existing optimization pass that will move as much code into the
body of a forward branch as possible? Consider the following example:

 

int foo(int x)

{

    for(int i = 0; i < 1000000; i++)

    {

        int y = (x + 1) / x;   // Expensive calculation! Result written to
memory.

 

        if(x == 0)   // Forward branch

        {

            x = y;   // Body

        }

    }

  

    return x;

}

 

It appears that LLVM compiles this quite literally, performing the expensive
calculation a million times. Yet it could be rewritten like this:

 

int foo(int x)

{

    for(int i = 0; i < 1000000; i++)

    {

        if(x == 0)   // Unlikely to hit

        {

            int y = (x + 1) / x;

            x = y;

        }

    }

  

    return x;

}

 

This runs way faster.

 

I noticed there's a loop hoisting optimization (moving as many independent
operations out of the body of a backward branch), but I'm looking for its
twin. Did I overlook it or is it not yet supported?

 

Thanks,

 

Nicolas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091014/e7f5f18d/attachment.html>


More information about the llvm-dev mailing list