The bug is here: https://llvm.org/bugs/show_bug.cgi?id=31045
The problem occurs when we take a node N and analyze its predecessors (operands of operands). We start from N and recursively go up N = N->getOperand(0).
On some stage of the climbing, we decide to replace one of predecessors with ReplaceAllUsesWith(N, NewNode). The ReplaceAllUsesWith works recursively and at some stage kills parent node.
I added ReplaceOneUseWith() to prevent recursive deletion.
Please let me know if this solution looks reasonable. (I attached the original reproducer, I'll minimize it if the solution itself makes sense.)