Issue 240229
Summary [InstCombine] fcmp and fptrunc-shrinking folds ignore the narrow type's input denormal mode
Labels new issue
Assignees
Reporter 20000419
    Three InstSimplify/InstCombine folds reason with IEEE values for a type whose input denormal mode flushes. Each gives a result that differs for normal inputs, not only for subnormal results. Fold 1 is reachable from C with `-ffast-math` (or an equivalent flushing denormal mode); folds 2 and 3 need a float-only flush mode and are shown in IR.

```c
#include <stdio.h>
#include <math.h>
__attribute__((noinline)) int f(float x) { return fabsf(x) == -0x1p-149f; }
int main(void) { volatile float z = 0.0f; printf("%d\n", f(z)); return 0; }
```

`clang -O0 -ffast-math t-x86-fastmath.c && ./a.out` prints `1` (the flushed constant is a logical `-0.0`, so `fabs(0) == -0` is true). `clang -O2 -ffast-math` prints `0` because InstSimplify folds the compare to false. Godbolt (clang trunk 0fc5afa04c23, 2026-11-09): https://godbolt.org/z/as1KP8YPz

```llvm
; 1. A negative subnormal constant is a logical -0.0 under preservesign, so fabs(x) == C is true for x == 0.
define i1 @fabs_oeq_negsub(float %x) denormal_fpenv(ieee|preservesign) {
  %a = call float @llvm.fabs.f32(float %x)
  %c = fcmp oeq float %a, 0xB6A0000000000000 ; -2^-149
  ret i1 %c
}

; 2. Only float flushes. A double that rounds to a float subnormal compares as 0.0 > 0.0, which is false.
define i1 @fptrunc_ogt(double %x) denormal_fpenv(ieee|ieee, float: ieee|preservesign) {
  %f = fptrunc double %x to float
  %c = fcmp ogt float %f, 0.0
  ret i1 %c
}

; 3. 2^-127 is normal in double but a flushed float input once the fmul is narrowed.
define float @narrow(float %x) denormal_fpenv(ieee|ieee, float: ieee|preservesign) {
  %e = fpext float %x to double
  %p = fmul double %e, 0x3800000000000000
  %t = fptrunc double %p to float
  ret float %t
}
```

`opt -S -passes=instcombine` on trunk (Godbolt opttrunk 2026-11-09): https://godbolt.org/z/ej9WGrbf3

```llvm
define i1 @fabs_oeq_negsub(float %x) {
  ret i1 false                                  ; expected: fcmp oeq float %x, 0.0
}
define i1 @fptrunc_ogt(double %x) {
  %c = fcmp ogt double %x, f0x3690000000000000  ; true for x = 2^-140; original is false
  ret i1 %c
}
define float @narrow(float %x) {
  %t = fmul float %x, f0x00400000               ; always ±0 under float preservesign
  ret float %t                                  ; original returns 2^-127 for x = 1.0
}
```

- `@fabs_oeq_negsub`: already folded by `-passes=instsimplify`. For `x = 0` the original compares `+0 == -0` (the flushed constant), which is true. The fold to `false` is in `simplifyFCmpInst` (`InstructionSimplify.cpp`, the `if (C->isNegative() && !C->isNegZero())` / `FCMP_OEQ` path): it uses the sign of the raw constant and `cannotBeOrderedLessThanZero` on `fabs(x)`, but never flushes `C` with the function's input mode.
- `@fptrunc_ogt`: `foldFCmpFpTrunc` moves the compare to the double type, where nothing is flushed.
- `@narrow`: `visitFPTrunc` shrinks the double `fmul` to float and narrows the constant to the float subnormal 2^-127, which is then a flushed input.

Versions rechecked 2026-11-09: clang trunk `24.0.0git` `0fc5afa04c23` still wrong for the C case at `-O1+ -ffast-math` (`0`); `-O0 -ffast-math` prints `1`. Local clang-24 (`cf44dd9e317e`, 2026-10-31) same. opt trunk still folds all three IR cases as above. `@fptrunc_ogt` wrong from clang/opt 22.1 onward (fold from #99475); folds 1 and 3 wrong on 19–trunk.

Possible fix (not tested): check `F.getDenormalMode(Ty)` for the narrow type (or the constant's type) and skip these folds when its input mode is not IEEE. For case 1, flush the constant first, like `ConstantFoldCompareInstruction` callers already do.

Related but different: #237855 / #238204 (floor/ceil), #236656 (fpext/fptrunc demanded classes), #181899 (fcmp→icmp already restricted for flushing modes), #236190 (constant-only fcmp baselines). None covers these three folds. #233675 is llubi positivezero mode, not InstCombine.

cc @arsenm

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

Assisted-by: Grok

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to