Eliminating PHI with Identical Inputs

Dear All,

Is there a pass (or set of passes) that will replace a phi whose input operands all compute the same value with an instruction that computes that value? In other words, something that will convert:

define internal i32 @function(i32 %x) {
...
bb1:
%y = add %x, 10
...
bb2:
%z = add %x, 10
...
bb3:
%phi = [bb1, %y], [bb2, %z]

into

define internal i32 @function(i32 %x) {
...
bb1:
...
bb2:
...
bb3:
%phi = add %x, 10

-- John T.

Hi John,

Dear All,

Is there a pass (or set of passes) that will replace a phi whose input operands
all compute the same value with an instruction that computes that value? In
other words, something that will convert:

define internal i32 @function(i32 %x) {
...
bb1:
%y = add %x, 10
...
bb2:
%z = add %x, 10
...
bb3:
%phi = [bb1, %y], [bb2, %z]

into

define internal i32 @function(i32 %x) {
...
bb1:
...
bb2:
...
bb3:
%phi = add %x, 10

yes, GVN should replace %y and %z with the same register, giving something like

  %same = add %x, 10
...
  bb1:
  ...
  bb2:
  ...
  bb3:
  %phi = [bb1, %same], [bb2, %same]

at which point the instcombine pass should zap the phi, though maybe GVN will
get it already.

Ciao, Duncan.

Hi John,

Dear All,

Is there a pass (or set of passes) that will replace a phi whose input operands
all compute the same value with an instruction that computes that value? In
other words, something that will convert:

define internal i32 @function(i32 %x) {
...
bb1:
%y = add %x, 10
...
bb2:
%z = add %x, 10
...
bb3:
%phi = [bb1, %y], [bb2, %z]

into

define internal i32 @function(i32 %x) {
...
bb1:
...
bb2:
...
bb3:
%phi = add %x, 10

yes, GVN should replace %y and %z with the same register, giving something like

%same = add %x, 10
...
bb1:
...
bb2:
...
bb3:
%phi = [bb1, %same], [bb2, %same]

at which point the instcombine pass should zap the phi, though maybe GVN will
get it already.

Odd. I'm running GVN and then InstCombine, and it doesn't fix the problem.

Is this new behavior added since LLVM 3.2, or is this something that GVN has been doing for awhile?

-- John T.

Hi John,

Hi John,

Dear All,

Is there a pass (or set of passes) that will replace a phi whose input operands
all compute the same value with an instruction that computes that value? In
other words, something that will convert:

define internal i32 @function(i32 %x) {
...
bb1:
%y = add %x, 10
...
bb2:
%z = add %x, 10
...
bb3:
%phi = [bb1, %y], [bb2, %z]

into

define internal i32 @function(i32 %x) {
...
bb1:
...
bb2:
...
bb3:
%phi = add %x, 10

yes, GVN should replace %y and %z with the same register, giving something like

%same = add %x, 10
...
bb1:
...
bb2:
...
bb3:
%phi = [bb1, %same], [bb2, %same]

at which point the instcombine pass should zap the phi, though maybe GVN will
get it already.

Odd. I'm running GVN and then InstCombine, and it doesn't fix the problem.

Is this new behavior added since LLVM 3.2, or is this something that GVN has
been doing for awhile?

I think it's been doing it forever. If it isn't doing it, then maybe I'm wrong
about GVN doing this, but it could also be due to the topology of the CFG.
After all, it has to put %same somewhere. If there isn't a good basic block to
drop it in, then it may just give up. Can you provide a complete example that
shows this problem?

Ciao, Duncan.