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

Duncan Sands baldrick at free.fr
Mon Feb 7 04:50:25 PST 2011


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Prop.cpp
Type: text/x-c++src
Size: 3641 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110207/4d2bcd96/attachment.cpp>


More information about the llvm-dev mailing list