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

[InstCombine] and (fcmp ord x, 0), (fcmp u* x, C) is folded to fcmp o* x, C only when C is inf

Open thoughtafter opened this issue 2 months ago • 2 comments

Missed Optimization: InstCombine folds a NaN check combined with an unordered compare into one ordered compare, but only when the constant is infinity. For any other constant that is not NaN, the same fold is valid, and the two compares stay. The or dual is not folded either.

Reproducer

define i1 @and_ord_ult(double %x) {
  %ord = fcmp ord double %x, 0.0
  %lt = fcmp ult double %x, 0x43E0000000000000   ; 2^63
  %r = and i1 %ord, %lt
  ret i1 %r
}

define i1 @or_uno_olt(double %x) {
  %uno = fcmp uno double %x, 0.0
  %lt = fcmp olt double %x, 0x43E0000000000000
  %r = or i1 %uno, %lt
  ret i1 %r
}

define i1 @and_ord_ult_inf(double %x) {
  %ord = fcmp ord double %x, 0.0
  %lt = fcmp ult double %x, 0x7FF0000000000000   ; +inf
  %r = and i1 %ord, %lt
  ret i1 %r
}

Results

opt -passes=instcombine (the same with default<O3>):

function result expected
@and_ord_ult unchanged fcmp olt double %x, 2^63
@or_uno_olt unchanged fcmp ult double %x, 2^63
@and_ord_ult_inf fcmp one double %x, +inf (folded)

Alive2 (alive-tv) proves the expected results correct in both directions:

  • and (fcmp ord x, 0.0), (fcmp ult x, 2^63) <-> fcmp olt x, 2^63: "Transformation seems to be correct!" both ways
  • or (fcmp uno x, 0.0), (fcmp olt x, 2^63) <-> fcmp ult x, 2^63: "Transformation seems to be correct!" both ways
  • Control: and (fcmp ord x, 0.0), (fcmp ult x, 2^63) -> fcmp ole x, 2^63: "Transformation doesn't verify! ERROR: Value mismatch"

Why it should hold for any non-NaN constant

fcmp ord x, 0.0 is true exactly when x is not NaN. fcmp ult x, C is x < C or unordered. When C is not NaN, the pair is unordered only when x is NaN, and the ord compare removes that case. So the and is fcmp olt x, C. The same holds for every unordered predicate. The or dual follows the same way: uno x | o* x, C is u* x, C.

Possible cause

foldLogicOfFCmps in llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp has this fold only through matchIsFiniteTest:

/// and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf
///
/// Clang emits this pattern for doing an isfinite check in __builtin_isnormal.

and matchUnorderedInfCompare requires the constant to be infinity:

return FCmpInst::isUnordered(P) && match(RHS, m_Inf());

Relaxing m_Inf() to any constant that is not NaN, or to an operand known never to be NaN, would cover these cases. The or (fcmp uno x, 0), (fcmp o* x, C) dual would need a matching rule. As with #136851, it would not apply to the logical-select form.

The general predicate-combining fold at the top of foldLogicOfFCmps cannot catch this. It needs both compares to have the same operands, and FCmp canonicalization rewrites fcmp ord x, C to fcmp ord x, 0.0, so the operands differ.

Where this shows up

A range check min <= x < max lowered through a total-order three-way compare, where NaN is handled explicitly with fcmp ord/uno, stays as three compares where two would do:

%ord = fcmp ord double %x, 0.0
%ge = fcmp uge double %x, 0xC3E0000000000000   ; -2^63
%lt = fcmp ult double %x, 0x43E0000000000000   ;  2^63
%a = and i1 %ord, %lt
%r = and i1 %ge, %a

Related

  • #81027 simplifies and (fcmp ord x, 0), (fcmp o* x, y) to the ordered compare in InstSimplify. That is the case where the ord is redundant. Here it is not, and a new instruction is needed.
  • #136851 stopped the inf fold for logical selects.
  • #205076 covers ord x & ord y / uno x | uno y in logical selects.

Version

Ubuntu LLVM 23.1.2. The code above is unchanged on main as of 6c5ea95816 (2026-09-25).

thoughtafter avatar Sep 28 '26 07:09 thoughtafter

Hey @thoughtafter ! Are you planning to work on this yourself, or would it be okay if I gave it a go? :)

martonmoro avatar Sep 28 '26 12:09 martonmoro

@martonmoro Give it a shot. I've got more than enough on my plate. This isn't a blocker for me but it seemed like it was worth improving.

thoughtafter avatar Sep 28 '26 17:09 thoughtafter