InstCombine currently has odd rules for folding insert-extract chains to shuffles, so we miss collapsing seemingly simple cases as shown in the tests here.
But poison makes this not quite as easy as we might have guessed. Alive2 tests to show the subtle difference (similar to the regression tests):
https://alive2.llvm.org/ce/z/hp4hv3 (this is ok)
https://alive2.llvm.org/ce/z/ehEWaN (poison leakage)
SLP tends to create these patterns (as shown in the SLP tests), and this could help with solving PR16739.
There's a logic bug with this patch as committed (and subsequently reverted - thanks @bkramer).
We are recursively calling SimplifyDemandedVectorElts() here and that can change UndefElts to a value that does not correspond to the value that we are returning in the block of code added with this patch.
The fix is to try the PreInsertDemandedElts fold first (just re-order the code). The existing fold below that removes the insertelt seems to be immune to that problem, but I might move that too to be safer.