Type flow analysis in C2 could end up with multiple blocks marked as loop head (lphd when printing with -XX:+CIPrintTypeFlow) while the Java program itself has only one loop. As a consequence, the IR contains also multiple LoopNodes. For example, C2 generates initially two LoopNodes for the following simple program (Run with -Xcomp):
public void mainTest() {
int x = 0;
int i = 1;
while (++i < 35) {
x = 6;
if (i == 3) {
x = 7;
} else {
x = 9;
}
}
}
The C2 type flow analysis code does some loop head cloning in ciTypeFlow::clone_loop_head() after the first pass. In this method, the next block for the tail of the loop is set to a clone of the loop head block (marked as backedge copy). We want to ensure that the loop head block is only entered by a single entry block. But in the simple program above, we actually have two loop tails coming from the if or the else block. There is no unique tail. Because of that, we set the original loop head block (which we just cloned) again as loop head while building the loop tree in ciTypeFlow::build_loop_tree() again after the loop head cloning step. This results in having two loop heads and two LoopNodes in the IR.
This seems to be inefficient as it results in more nodes and possibly unneeded loop optimizations compared to just having a unique loop head. This RFE should support the handling for multiple tails with the goal to have a unique block marked as loop head and eventually a single LoopNode in the IR.