I am working on a homework compiler project and I have done the mem2reg pass and got some phi instructions in my LLVM IR.
However, I am confused about how to eliminate the phi nodes (phi destruction). After searching online and reading some chapters of Single Static Assignment Book and Data Flow Analysis, I know a few concepts and several method to eliminate the phi nodes.
- Critical Edge Spliting
- Lost of Copy Problem
- Swap Problem
Traditional ways to eliminate phi nodes can just be like this
block 1
//x3 = phi(x1,x2)
block 2
x1 = ...
move x3 x1
block 3
x2 = ...
move x3 x2
I know such way would cause the lost of copy problem (when copy propagation) and swap problem, and I found a way slightly different from this
block 1
x3 = x4
//x3 = phi(x1,x2)
block 2
x1 = ...
move x4 x1
block 3
x2 = ...
move x4 x2
Can this way of phi's elimination do a fully good job on transformed SSA ? Or in what condition this method would cause errors ?
May be it can not, because I always see "cirtical edges spliting" and I can't fully understand it.
When adding a empty block in a cirtical edge, the movement about phi's elimination seems to be the end of the empty block ? And I try to stimulate it, I found that it woundn't solve the swap problem.