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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 01:10:37 PDT 2024


================
@@ -0,0 +1,245 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Check for fcmp + sel pattern which later lowered into fmax
+define float @test_fmax_pos1(float %in) {
+; CHECK-LABEL: define float @test_fmax_pos1(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd 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 i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmax_pos2(float %in) {
+; CHECK-LABEL: define float @test_fmax_pos2(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd 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 i1 %cmp1, float 1.000000e+00, float %add
+  ret float %sel
+}
+
+define float @test_fmax_pos3(float %in) {
+; CHECK-LABEL: define float @test_fmax_pos3(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float 1.000000e+00, %in
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmax_pos4(float %in) {
+; CHECK-LABEL: define float @test_fmax_pos4(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float 1.000000e+00, %in
+  %sel = select i1 %cmp1, float 1.000000e+00, float %add
+  ret float %sel
+}
+
+define float @test_fmax_pos5(float %in) {
+; CHECK-LABEL: define float @test_fmax_pos5(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 2.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float 2.000000e+00, %in
+  %sel = select i1 %cmp1, float 2.000000e+00, float %add
+  ret float %sel
+}
+
+
+; Check for fcmp + sel pattern which later lowered into fmin
+define float @test_fmin_pos1(float %in) {
+; CHECK-LABEL: define float @test_fmin_pos1(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp olt float %in, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmin_pos2(float %in) {
+; CHECK-LABEL: define float @test_fmin_pos2(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp olt float %in, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float 1.000000e+00, float %add
+  ret float %sel
+}
+
+define float @test_fmin_pos3(float %in) {
+; CHECK-LABEL: define float @test_fmin_pos3(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp olt float %in, 0.000000e+00
+  %add = fadd float 1.000000e+00, %in
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmin_pos4(float %in) {
+; CHECK-LABEL: define float @test_fmin_pos4(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 1.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp olt float %in, 0.000000e+00
+  %add = fadd float 1.000000e+00, %in
+  %sel = select i1 %cmp1, float 1.000000e+00, float %add
+  ret float %sel
+}
+
+define float @test_fmin_pos5(float %in) {
+; CHECK-LABEL: define float @test_fmin_pos5(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[SEL_NEW:%.*]] = select i1 [[CMP1]], float [[IN]], float 0.000000e+00
+; CHECK-NEXT:    [[ADD_NEW:%.*]] = fadd float [[SEL_NEW]], 2.000000e+00
+; CHECK-NEXT:    ret float [[ADD_NEW]]
+;
+  %cmp1 = fcmp olt float %in, 0.000000e+00
+  %add = fadd float 2.000000e+00, %in
+  %sel = select i1 %cmp1, float 2.000000e+00, float %add
+  ret float %sel
+}
+
+
+; Check for fmax scenarios that shouldn't be transformed.
+define float @test_fmax_neg1(float %in, float %in2) {
+; CHECK-LABEL: define float @test_fmax_neg1(
+; CHECK-SAME: float [[IN:%.*]], float [[IN2:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN2]], 0.000000e+00
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP1]], float [[ADD]], float 1.000000e+00
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %cmp1 = fcmp ogt float %in2, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmax_neg2(float %in) {
+; CHECK-LABEL: define float @test_fmax_neg2(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP1]], float [[ADD]], float 1.000000e+00
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %cmp1 = fcmp ogt float %in, 1.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmax_neg3(float %in) {
+; CHECK-LABEL: define float @test_fmax_neg3(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt float [[IN]], 0.000000e+00
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[ADD_2:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[SEL_1:%.*]] = select i1 [[CMP1]], float [[ADD]], float 1.000000e+00
+; CHECK-NEXT:    [[SEL_2:%.*]] = select i1 [[CMP1]], float 2.000000e+00, float [[ADD_2]]
+; CHECK-NEXT:    [[RES:%.*]] = fadd float [[SEL_1]], [[SEL_2]]
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %cmp1 = fcmp ogt float %in, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %add.2 = fadd float %in, 1.000000e+00
+  %sel.1 = select i1 %cmp1, float %add, float 1.000000e+00
+  %sel.2 = select i1 %cmp1, float 2.000000e+00, float %add.2
+  %res = fadd float %sel.1, %sel.2
+  ret float %res
+}
+
+
+; Check for fmin scenarios that shouldn't be transformed.
+define float @test_fmin_neg1(float %in, float %in2) {
+; CHECK-LABEL: define float @test_fmin_neg1(
+; CHECK-SAME: float [[IN:%.*]], float [[IN2:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN2]], 0.000000e+00
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP1]], float [[ADD]], float 1.000000e+00
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %cmp1 = fcmp olt float %in2, 0.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmin_neg2(float %in) {
+; CHECK-LABEL: define float @test_fmin_neg2(
+; CHECK-SAME: float [[IN:%.*]]) {
+; CHECK-NEXT:    [[CMP1:%.*]] = fcmp olt float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[ADD:%.*]] = fadd float [[IN]], 1.000000e+00
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP1]], float [[ADD]], float 1.000000e+00
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %cmp1 = fcmp olt float %in, 1.000000e+00
+  %add = fadd float %in, 1.000000e+00
+  %sel = select i1 %cmp1, float %add, float 1.000000e+00
+  ret float %sel
+}
+
+define float @test_fmin_neg3(float %in) {
----------------
arsenm wrote:

Can you name the test functions, or at least comment why they are supposed to be negative tests 

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


More information about the llvm-commits mailing list