[llvm] [InstCombine] Optimize redundant floating point comparisons in `or`/`and` inst's (PR #158097)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 14 07:59:50 PDT 2025


================
@@ -4657,3 +4657,107 @@ define i1 @or_fcmp_reassoc4(i1 %x, double %a, double %b) {
   %retval = or i1 %cmp1, %or
   ret i1 %retval
 }
+
+define i1 @or_fcmp_redundant_or1(ptr %arg0) {
+; CHECK-LABEL: @or_fcmp_redundant_or1(
+; CHECK-NEXT:    [[V0:%.*]] = load double, ptr [[ARG0:%.*]], align 8
+; CHECK-NEXT:    [[V2:%.*]] = fcmp nsz olt double [[V0]], 1.990000e+00
+; CHECK-NEXT:    ret i1 [[V2]]
+;
+  %v0 = load double, ptr %arg0, align 8
+  %v1 = fcmp nsz olt double %v0, 1.000000e-02
+  %v2 = fcmp nsz olt double %v0, 1.990000e+00
+  %v3 = or i1 %v1, %v2
+  ret i1 %v3
+}
----------------
dtcxzyw wrote:

```suggestion
define i1 @or_fcmp_redundant_or1(double %v0) {
; CHECK-LABEL: @or_fcmp_redundant_or1(
; CHECK-NEXT:    [[V0:%.*]] = load double, ptr [[ARG0:%.*]], align 8
; CHECK-NEXT:    [[V2:%.*]] = fcmp nsz olt double [[V0]], 1.990000e+00
; CHECK-NEXT:    ret i1 [[V2]]
;
  %v1 = fcmp olt double %v0, 1.000000e-02
  %v2 = fcmp olt double %v0, 1.990000e+00
  %v3 = or i1 %v1, %v2
  ret i1 %v3
}
```
Can you also add some tests with logical and/or?

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


More information about the llvm-commits mailing list