[llvm] [InstCombine] Transform (fcmp + fadd + sel) into (fcmp + sel + fadd) (PR #106492)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 05:14:56 PST 2024


================
@@ -0,0 +1,674 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; fcmp OGT + fadd + sel => fcmp OGT + sel => fmaxnum
+
+define float @test_fcmp_ogt_fadd_select_constant(float %in) {
+; CHECK-LABEL: define float @test_fcmp_ogt_fadd_select_constant(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = call nnan nsz float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd nnan nsz float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select nnan nsz i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fcmp_ogt_fadd_select_constant_swapped(float %in) {
+; CHECK-LABEL: define float @test_fcmp_ogt_fadd_select_constant_swapped(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = call nnan nsz float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd nnan nsz float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select nnan nsz i1 %cmp1, float 1.000000e+00, float %add
+  ret float %sel
+}
----------------
artagnon wrote:

This is trivially incorrect.

```
define float @test_fcmp_oge_fadd_select_constant_swapped(float %in) {
#0:
  %cmp1 = fcmp oge float %in, 0.000000
  %add = fadd float %in, 1.000000
  %sel = select nnan nsz i1 %cmp1, float 1.000000, float %add
  ret float %sel
}
=>
define float @test_fcmp_oge_fadd_select_constant_swapped(float %in) {
#0:
  %sel = fmax nsz float %in, 0.000000
  %add = fadd nnan nsz float %sel, 1.000000
  ret float %add
}
Transformation doesn't verify! (unsound)
ERROR: Value mismatch

Example:
float %in = #x3e800001 (0.250000029802?)

Source:
i1 %cmp1 = #x1 (1)
float %add = #x3fa00000 (1.25)
float %sel = #x3f800000 (1)

Target:
float %sel = #x3e800001 (0.250000029802?)
float %add = #x3fa00000 (1.25)
Source value: #x3f800000 (1)
Target value: #x3fa00000 (1.25)
```

https://github.com/llvm/llvm-project/pull/106492


More information about the llvm-commits mailing list