There are a couple of issues around MergeBlockIntoPredecessor and MSSA update.
Both issues show up when loop-simplify-cfg is used after simple-loop-unswitch.
- moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start) expects to be called after all instructions from From were moved to To. Specifically, it assumes that Start belongs to To basic block and iterates over make_range(Start->getIterator(), To->end()) range (see moveAllAccesses). MergeBlockIntoPredecessor currently calls moveAllAfterMergeBlocks before instructions are moved into To and passes Start which belongs to From.
The fix is to call moveAllAfterMergeBlocks after the instructions are moved from From to To.
- After simple-loop-unswitch some blocks might end up with trivial phis with one incoming value. When such a block is merged with its predecessor (by loop-simplify-cfg) the phi node is left intact after the block is removed. This results in a dangling reference to a phi without a block. unswitch_simplify_mssa_update.ll test case fails with VerifyMemorySSA && EXPENSIVE_CHECKS on this assertion in verifyPrevDefInPhis: assert(LastAcc == IncAcc && "Incorrect incoming access into phi.");
I fixed this by calling tryRemoveTrivialPhi for From block in moveAllAfterMergeBlocks. I'm not certain that this is the right approach. May be we shouldn't have trivial phis to begin with? Please advise.