[all-commits] [llvm/llvm-project] 268e5b: [InstCombine] Fold icmp eq/ne X, select(icmp pred ...

Addmisol via All-commits all-commits at lists.llvm.org
Wed Sep 30 10:34:47 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 268e5ba1d99152e5f00e1d8432969a6ba11245fd
      https://github.com/llvm/llvm-project/commit/268e5ba1d99152e5f00e1d8432969a6ba11245fd
  Author: Addmisol <addmisol9 at gmail.com>
  Date:   2026-09-30 (Wed, 30 Sep 2026)

  Changed paths:
    M llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    A llvm/test/Transforms/InstCombine/icmp-eq-select-self.ll

  Log Message:
  -----------
  [InstCombine] Fold icmp eq/ne X, select(icmp pred X, P, C1, C2) to set membership (#226756)

## Summary
- Add InstCombine fold for `icmp eq/ne X, select(icmp pred X, P, C1,
C2)` pattern
- Transforms self-referential select comparisons into simple set
membership tests
- Handles all icmp predicates (signed, unsigned) and commuted operands

## Details

When we have code like:
```llvm
%cond = icmp sgt i32 %x, 0
%s = select i1 %cond, i32 2, i32 0
%r = icmp eq i32 %x, %s

The comparison can only be true when X equals one of the select's constant arms. This patch recognizes when:
1. The select condition compares X with a constant P
2. C1 (true arm) satisfies the condition
3. C2 (false arm) does not satisfy the condition

And folds to (X == C1) | (X == C2) for eq, or (X != C1) & (X != C2) for ne.
```

## Proofs
Alive2 proofs:
- Basic case: https://alive2.llvm.org/ce/z/LMpt7A
- Generalized proof with symbolic constants:
https://alive2.llvm.org/ce/z/ZMrWVm

The generalized proof shows correctness for any C1, C2, P where C1
satisfies the condition and C2 does not.

Fixes https://github.com/llvm/llvm-project/issues/223249

---------

Signed-off-by: addmisol <addmisol9 at gmail.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list