This is a fix in legality check.
We already know that we need to check whether lcssa phis are supported in inner loop exit block or in outer loop exit block, and we have logic to check them already. Presumably the inner loop latch does not have lcssa phis and there is no code that deals with lcssa phis in the inner loop latch.
However, that assumption is not true, when we have loops with more than two-level nesting. Take a triply nested loop as an example (which is provided as the test case in this patch). Suppose we do interchange two times for the innermost/middle loop, and the (new) middle/outermost loop. I'll use the following terminologies:
The old innermost/middle/outermost header/latch -> the very initial loop nest.
The intermediate innermost/middle/outermost header/latch -> the loop nest after 1st interchange.
The final innermost/middle/outermost header/latch -> the loop nest after 2nd interchange.
If the old innermost latch uses a value defined in the old middle header, then after 1st interchange, it becomes that the intermediate middle latch uses a value defined in the intermediate inner header. This is exactly when there is an lcssa phi node in "BasicBlock* InnerLooplatch". Note that since we are in the stage of doing the 2nd interchange, "BasicBlock* InnerLooplatch" is the intermediate middle latch.
This patch added a function areInnerLoopLatchPHIsSupported() to check the legality in InnerLoopLatch (just to clarify again, currently "BasicBlock* InnerLooplatch" is the intermediate middle latch). Note that the intermediate middle latch will become the final outermost latch, and in certain cases the values in the intermediate innermost loop will not be available in the final outermost latch, e.g., when the old outermost header can jump directly to the old outermost latch.
As an example, for the test case provided in this patch, it will run the 1st interchange without any problem, but in the 2nd interchange it will crash or hit assertion errors with the current trunk. The legality check added in this patch catches the IR after the 1st interchange and bail out the 2nd interchange.
Can user of PHI be not an instruction? If not, change dyn_cast to cast.