Fixes https://github.com/llvm/llvm-project/issues/61182.
LoopStrengthReduce may sometimes break LCSSA form when applying a rewrite for an instruction used in a PHI.
It happens if:
- The PHI is in a loop exit block,
- The edge from the corresponding exiting block to that exit is critical,
- The PHI has at least two inputs coming from loop blocks,
- The rewritten instruction is inserted in the loop.
An example of initial CFG:
LoopBlock <-- ExitingBlock1 ExitingBlock2 | | v | ExitBlock <----------+
In such case we split the critical edge:
LoopBlock <-- ExitingBlock1 ExitingBlock2 | | v v CritEdge split | | v | ExitBlock <---------+ (not exit anymore)
and then replace PHI inputs with the rewritten instruction. However ExitBlock is no longer a loop exit, so LCSSA form is broken.
This patch fixes it by collecting all inserted instructions for PHIs whose parent block is not a loop exit and then forming LCSSA for them.
I'm curious, is it possible that second value here is *not* an instruction? Might be a good NFC change beforehand.