CSE vs GVN in compiler optimization theory

Viewed 335

I get one question when I read Lecture 11, Common Subexpression Elimination, of Advanced Compilers course of umass amherst.

The question is, on page 12 in Lecture 11, it illustrates that value numbering can't eliminate all subexpressions.

    read(i);
    l = 2*i;
    if(i > 0) goto L1;
    j = 2*i;
    goto L2;
L1: k = 2*i;
L2:  

The explanation is: l’s value is not always equal to j’s or k’s value

What's puzzled me is, all (l / j / k)'s values depend on 2*i , i's value numbering should be the same in all basic blocks as there's no any assignment or re-definition for i in exampled code snippet. Is it correct?

If it's correct, 2*i will get the same value numbering too and the redundant 2*i computations for j and k can be eliminated successfully.

Do I make any mistake for the illustration? please help me to address it if you found.

1 Answers

You are correct in both assumptions. The value number for i and subsequently the value number for 2*i will be the same in the entire function following read(i). Thus the computation of 2*i is redundant when assigning j and k, it could be replaced with l in both cases.

Related