[llvm] [InstCombineCompares] Replace the sqrt in if-condition (PR #91707)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri May 10 04:18:52 PDT 2024


================
@@ -0,0 +1,87 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S -replace-sqrt-compare-by-square=true | FileCheck %s -check-prefix=OPT-TRUE
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s -check-prefix=OPT-FALSE
+
+define i1 @foo1_fast(float %a, float %b) {
+; OPT-TRUE-LABEL: @foo1_fast(
+; OPT-TRUE-NEXT:    [[FCMP:%.*]] = fcmp fast ogt float %a, %b
+; OPT-TRUE-NEXT:    ret i1 [[FCMP]]
+;
+; OPT-FALSE-LABEL: @foo1_fast(
+; OPT-FALSE-NEXT:    %c = call fast float @llvm.sqrt.f32(float %a)
+; OPT-FALSE-NEXT:    %d = call fast float @llvm.sqrt.f32(float %b)
+; OPT-FALSE-NEXT:    [[FCMP:%.*]] = fcmp fast ogt float %c, %d
+; OPT-FALSE-NEXT:    ret i1 [[FCMP]]
+;
+  %c = call fast float @llvm.sqrt.f32(float %a)
+  %d = call fast float @llvm.sqrt.f32(float %b)
+  %e = fcmp fast ogt float %c, %d
+  ret i1 %e
+}
+
+define i1 @foo2_fast(float %a, float %b) {
+; OPT-TRUE-LABEL: @foo2_fast(
+; OPT-TRUE-NEXT:    [[FMUL:%.*]] = fmul fast float %b, %b
+; OPT-TRUE-NEXT:    [[SIGN:%.*]] = call fast float @llvm.copysign.f32(float [[FMUL]], float %b)
+; OPT-TRUE-NEXT:    [[FCMP:%.*]] = fcmp fast ogt float [[SIGN]], %a
+; OPT-TRUE-NEXT:    ret i1 [[FCMP]]
+;
+; OPT-FALSE-LABEL: @foo2_fast(
+; OPT-FALSE-NEXT:    [[SQRT:%.*]] = call fast float @llvm.sqrt.f32(float %a)
+; OPT-FALSE-NEXT:    [[FCMP:%.*]] = fcmp fast olt float [[SQRT]], %b
+; OPT-FALSE-NEXT:    ret i1 [[FCMP]]
+;
+  %c = call fast float @llvm.sqrt.f32(float %a)
+  %d = fcmp fast ogt float %b, %c
+  ret i1 %d
+}
+
+define i1 @foo3_fast(float %a, float %b) {
+; OPT-TRUE-LABEL: @foo3_fast(
+; OPT-TRUE-NEXT:    [[FMUL:%.*]] = fmul fast float %b, %b
+; OPT-TRUE-NEXT:    [[SIGN:%.*]] = call fast float @llvm.copysign.f32(float [[FMUL]], float %b)
+; OPT-TRUE-NEXT:    [[FCMP:%.*]] = fcmp fast olt float [[SIGN]], %a
+; OPT-TRUE-NEXT:    ret i1 [[FCMP]]
+;
+; OPT-FALSE-LABEL: @foo3_fast(
+; OPT-FALSE-NEXT:    [[SQRT:%.*]] = call fast float @llvm.sqrt.f32(float %a)
+; OPT-FALSE-NEXT:    [[FCMP:%.*]] = fcmp fast ogt float [[SQRT]], %b
+; OPT-FALSE-NEXT:    ret i1 [[FCMP]]
+;
+  %c = call fast float @llvm.sqrt.f32(float %a)
+  %d = fcmp fast ogt float %c, %b
+  ret i1 %d
+}
+
+define i1 @foo1_no_fast(float %a, float %b) {
+; OPT-TRUE-LABEL: @foo1_no_fast(
+; OPT-TRUE:    [[FCMP:%.*]] = fcmp ogt float %c, %d
+; OPT-TRUE-NEXT:    ret i1 [[FCMP]]
+;
+  %c = call float @llvm.sqrt.f32(float %a)
+  %d = call float @llvm.sqrt.f32(float %b)
+  %e = fcmp ogt float %c, %d
----------------
arsenm wrote:

Should test with full range of compare types, particularly unordered compares 

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


More information about the llvm-commits mailing list