This optimization finds loop exit values reevaluated after the loop execution and replaces them by the corresponding exit values if they are available. Such sequences can arise after the SimplifyIndVals+LoopStrengthReduce passes. This pass should be run after LoopStrengthReduce.
Stale header comment.
not -> ! [to match our general conventions]
You should do the type-based validity checking prior to calling DT.dominates (which is going to be significantly more expensive).
Also, when do the types not match? Can getSCEVAtScope return an SCEV with a different type than its argument?
I wonder if it might be related to PR24920 (and D12765). Out of curiosity, could you check what happens in your testcase with the patch from there applied?
Also, some comments inline:
This resembles what SCEVExpander does (particularly, findExistingExpansion function). Would it be possible/beneficial to just reuse it here somehow?
It probably needs ; REQUIRES: Asserts