PHI / Select Simplification

Hi all,

I’m working on some LLVM 7 code and I was wondering if there exists an optimization pass (perhaps that exists in a later version of LLVM) that does the following. Or if not, a good pass for me to implement this inside of may be.

Suppose you have the following loop:

for: ; preds = %entry, %for
%phi = phi double [ %entry_value, %entry ], [ %selected_value, %for ]
%condition = …
%selected_value = select i1 %condition, double %inner_value, double %any_value
br i1 %condition, label %for, label %loop_exit

In this case, (and similarly cases where the loop condition is the same as the select condition), we may simply replace %selected_value in the for loop with %inner_value from the select (and therefore remove the select).

This type of structure comes up frequently in some codes I’m looking at and figured I’d check if anyone has something to deal with it already before I implement my own (and try to upstream).


Billy Moses

This seems to be done by a combination of Correlated Value Propagation
(which forwards %inner_value to the phi) and InstCombine (which
removes the now-dead select) on a simple example I fleshed out
(attached here). You can try it out with "opt -correlated-propagation
-instcombine -S simple.ll".

It's possible the value propagation isn't clever enough for your real
code, in which case it's probably still the best place to look at
fixing things in LLVM rather than adding a new pass.



simple.ll (473 Bytes)