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

[InstCombine] Fold range-with-hole test to direct inequality checks

Open mariiakrasniuk opened this issue 2 months ago • 3 comments

Description

InstCombine appears to miss:

(X - C) >u N || (X - C) == K

when the complement is a small constant set.

Example:

define i1 @src(i8 %x) {
  %v1 = add i8 %x, -8
  %v3 = icmp ugt i8 %v1, 2
  %v5 = icmp eq i8 %v1, 1
  %r = or i1 %v3, %v5
  ret i1 %r
}

define i1 @tgt(i8 %x) {
  %a = icmp ne i8 %x, 8
  %b = icmp ne i8 %x, 10
  %r = and i1 %a, %b
  ret i1 %r
}

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

The source is false only when:

x - 8 ∈ {0, 2}

i.e. when:

x ∈ {8, 10}

Therefore it is equivalent to:

x != 8 && x != 10

This removes the arithmetic and exposes a simpler constant set-membership test.

mariiakrasniuk avatar Sep 13 '26 15:09 mariiakrasniuk

@mariiakrasniuk: Please highlight code snippets as code blocks with relevant language.

EugeneZelenko avatar Sep 13 '26 15:09 EugeneZelenko

@EugeneZelenko done!

mariiakrasniuk avatar Sep 13 '26 15:09 mariiakrasniuk

Hi @EugeneZelenko, I’d like to work on this issue. My approach would be to handle the pattern in InstCombine, detect the unsigned range condition with a small complement set, and fold it into icmp ne comparisons while using APInt for correct wrapping arithmetic. I'll also add regression tests.

Could you please assign this issue to me?

nos1dot618 avatar Sep 13 '26 16:09 nos1dot618