We lifted this code from InstCombine for general usage in:
rL369842
...but it's not safe as-is. There are no existing users that can trigger this bug, but I discovered it via crashing several regression tests when trying to use it for select folding in InstSimplify.
ICmp requires (vector) integer types, so bitcast the constants before trying the match. That matches the definition of "equal or undef" that I was looking for. If someone wants an FP-aware version of equality (deal with NaN, -0.0), that could be a different mode or different function.
Probably not going to work for vectors of pointers though. Is that relevant to your use-case?