llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstCombine] Fold equality with self-dependent select to constant set membership

Open mariiakrasniuk opened this issue 2 months ago • 0 comments

Description

InstCombine appears to miss:

X == select(X >s 0, C1, C2)
  ->
(X == C1) || (X == C2)

when C1 >s 0 and C2 <=s 0.

Example:

define i1 @src(i32 %x) {
  %cond = icmp sgt i32 %x, 0
  %s = select i1 %cond, i32 2, i32 0
  %r = icmp eq i32 %x, %s
  ret i1 %r
}

define i1 @tgt(i32 %x) {
  %a = icmp eq i32 %x, 0
  %b = icmp eq i32 %x, 2
  %r = or i1 %a, %b
  ret i1 %r
}

Alive2: https://alive2.llvm.org/ce/z/LMpt7A

Generalized Alive2 proof: https://alive2.llvm.org/ce/z/ZMrWVm

The source is true exactly for X = 0 or X = 2, so the select can be replaced by a constant set-membership check.

mariiakrasniuk avatar Sep 13 '26 14:09 mariiakrasniuk