[LLVMdev] A small pass to constant fold branch conditions in destination blocks

Owen Anderson resistor at mac.com
Mon Feb 7 21:38:42 PST 2011


Duncan,

GVN already does this.  See lines 1669-1689.

--Owen

On Feb 7, 2011, at 4:50 AM, Duncan Sands wrote:

> Hi all, I wrote a little pass (attached) which does the following: if it sees a
> conditional branch instruction then it replaces all occurrences of the condition
> in the true block with "true" and in the false block with "false".  Well, OK, it
> is a bit more sophisticated (and a bit more careful!) than that but you get the
> idea.  It will turn this
>  define i1 @t1(i1 %c) {
>    br i1 %c, label %t, label %f
>  t:
>    ret i1 %c
>  f:
>    ret i1 %c
>  }
> into this
>  define i1 @t1(i1 %c) {
>    br i1 %c, label %t, label %f
>  t:
>    ret i1 true
>  f:
>    ret i1 false
>  }
> for example.  Curiously enough LLVM doesn't seem to have a pass that does this.
> I took a look at the effect on the testsuite by scheduling a run of this pass
> just after each run of -correlated-propagation.  In spite of being so simple
> (not to say simplistic) it has an enormous positive impact on Ada code and a
> substantial positive impact throughout the LLVM test-suite (I didn't check that
> programs still work after running the pass, so it could be that it has such a
> big effect because it is wrong!).
> 
> So... should this kind of logic be incorporated into LLVM?  Perhaps as part of
> an existing pass like -correlated-propagation?
> 
> It would be easy to make the pass a bit more powerful.  For example if the
> condition was "X == 0" then it could also replace X with 0 everywhere in the
> true block.
> 
> Ciao, Duncan.
> 
> PS: This was inspired by PR9004.
> <Prop.cpp>_______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list