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

[InstCombine] Missed select-to-and fold for a low-bit condition expressed as `(x & 1) != 0`

Open maple-syntax opened this issue 1 month ago • 2 comments

Summary

InstCombine folds select (trunc i8 %x to i1), %y, 0 to and i8 %x, %y when %y is noundef and has range [0, 2). However, expressing the same low-bit condition as icmp ne (and %x, 1), 0 prevents this fold. The difference persists with the default O2 pipeline.

LLVM version

  • Version: LLVM 24.0.0git, optimized build with assertions enabled.
  • Tested revision: 6a5b8dc0dedf192fe203c9a4fc79ed61bbb9fac4.
  • Host target: x86_64-unknown-linux-gnu. The reproducer does not specify a target triple or data layout; the reported difference is observed at the LLVM IR level.
  • The reproducer has no explicit target triple or data layout.

Reproducer

Save the following as repro.ll. The first function is a control showing the existing fold; the second is the missed case.

define i8 @trunc_condition(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %cond = trunc i8 %x to i1
  %ret = select i1 %cond, i8 %y, i8 0
  ret i8 %ret
}

define i8 @and_icmp_condition(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %lowbit = and i8 %x, 1
  %cond = icmp ne i8 %lowbit, 0
  %ret = select i1 %cond, i8 %y, i8 0
  ret i8 %ret
}

Commands:

opt -passes=verify -disable-output repro.ll
opt -S -verify-each -passes=instcombine repro.ll -o -
opt -S -verify-each -passes='default<O2>' repro.ll -o -

The verifier succeeds.

Actual InstCombine output

Omitting the module header:

define i8 @trunc_condition(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %ret = and i8 %x, %y
  ret i8 %ret
}

define i8 @and_icmp_condition(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %lowbit = and i8 %x, 1
  %cond.not = icmp eq i8 %lowbit, 0
  %ret = select i1 %cond.not, i8 0, i8 %y
  ret i8 %ret
}

The comparison is inverted and the select arms are swapped, but the select remains.

Expected result

The second function should also fold to:

define i8 @and_icmp_condition(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %ret = and i8 %x, %y
  ret i8 %ret
}

The noundef range(i8 0, 2) premise is preserved in both inputs. On defined inputs, %y is either 0 or 1: when it is 0, the result is 0; when it is 1, the result is the low bit of %x. Thus both condition representations admit the same fold. Operand commutation in the resulting and is also fine.

O2 result

Running the default O2 pipeline on the combined repro.ll above leaves the same difference:

Function Computing instructions, excluding ret Select eliminated
trunc_condition and i8 %y, %x Yes
and_icmp_condition and + icmp + select No

O2 adds inferred return-range and function attributes to both functions, but does not eliminate the select in the second function. This report concerns the IR simplification; machine-code or runtime performance differences have not been measured.

Actual O2 output from the combined reproducer (omitting the module header):

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
define range(i8 0, 2) i8 @trunc_condition(i8 %x, i8 noundef range(i8 0, 2) %y) local_unnamed_addr #0 {
  %ret = and i8 %y, %x
  ret i8 %ret
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
define range(i8 0, 2) i8 @and_icmp_condition(i8 %x, i8 noundef range(i8 0, 2) %y) local_unnamed_addr #0 {
  %lowbit = and i8 %x, 1
  %cond.not = icmp eq i8 %lowbit, 0
  %ret = select i1 %cond.not, i8 0, i8 %y
  ret i8 %ret
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }

Semantic validation

The original trunc-condition seed and its and/icmp variant passed Alive2 refinement checks in both directions, without disabling undef or poison inputs.

For comparison against the expected and expression, the Alive2 revision used here skips LLVM's ConstantRange parameter attributes. I therefore explicitly materialized the existing %y < 2 precondition in both proof-only functions. This is justified by this reproducer's noundef range(i8 0, 2) combination. These assumes are not present in the inputs passed to InstCombine or O2.

Self-contained Alive2 proof

Save as proof.ll:

declare void @llvm.assume(i1)

define i8 @src(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %valid = icmp ult i8 %y, 2
  call void @llvm.assume(i1 %valid)
  %lowbit = and i8 %x, 1
  %cond = icmp ne i8 %lowbit, 0
  %ret = select i1 %cond, i8 %y, i8 0
  ret i8 %ret
}

define i8 @tgt(i8 %x, i8 noundef range(i8 0, 2) %y) {
  %valid = icmp ult i8 %y, 2
  call void @llvm.assume(i1 %valid)
  %ret = and i8 %x, %y
  ret i8 %ret
}

Run:

opt -passes=verify -disable-output proof.ll
alive-tv --bidirectional --always-verify --fail-src-ub --smt-to=30000 proof.ll

Result:

Transformation seems to be correct!

These functions seem to be equivalent!

Tested with Alive2 revision 5d0eb277235b73f71000501c3a4f5ab9b2586915, built against the same LLVM revision, and Z3 5.1.0. Neither LLVM nor Alive2 source was modified.

Related existing fold

The control case comes from fold_select_trunc_and in fold-select-trunc.ll.

The corresponding select-to-and rule in InstCombineSelect.cpp explicitly matches a trunc condition and checks that the selected value has at most one active bit. This appears relevant to the difference, although I have not tested a patch to confirm the complete cause.

This may be related to #172888, which was fixed by PR #178977. That change added a fold from icmp ne (and X, 1), 0 to trunc X to i1.

In this reproducer, however, InstCombine inverts the comparison to icmp eq and swaps the select arms. The condition remains an icmp, so the existing select-to-and fold is not reached. This suggests a possible interaction between predicate canonicalization and pattern matching, although the exact cause has not been confirmed.

maple-syntax avatar Oct 09 '26 03:10 maple-syntax

Hi @maple-syntax,

I've opened a PR that fixes this: https://github.com/llvm/llvm-project/pull/230363

RS-Gits avatar Oct 09 '26 06:10 RS-Gits

4. Hi @maple-syntax, I've opened a PR that fixes this: #230363

Thank you for working on this and submitting the fix! I really appreciate it.

maple-syntax avatar Oct 11 '26 06:10 maple-syntax