We restrained from replacing uses of IV with IV.next if IV.next is potentially poison.
This patch loosens this restricton for one important particular case: memory instruction
is dominated by loop exit by condition icmp iV.next, X. In this case we can safely assume
that, whenever IV.next is poison, we exit the loop at this point. So only non-poisoned
values may reach the memory instruction.
Note that this transform reduces overlap of live ranges of IV and IV.next, so potentially it
can lead to better opt decisions overall.
The code that follows looks analogous to programUndefinedIfPoison from ValueTracking.hpp. (Though you handle slightly different cases.) I'd encourage you to split this into a helper function and use analogous naming conventions to make it easier to follow.
Side note - I'd suggest trying the existing implementation from ValueTracking. While the reason is definitely different, if that's enough to catch your interesting cases, you can save a bunch of work. :)