[llvm] [InstCombine] Fold copysign of selects from sign comparison to sign operand (PR #85627)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 09:38:36 PDT 2024


================
@@ -109,3 +109,54 @@ define float @fabs_mag(float %x, float %y) {
   %r = call float @llvm.copysign.f32(float %a, float %y)
   ret float %r
 }
+
+define float @copysign_conditional(i1 %x, float %y, float %z) {
+; CHECK-LABEL: @copysign_conditional(
+; CHECK-NEXT:    [[RES:%.*]] = tail call float @llvm.copysign.f32(float [[Z:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %cmp = fcmp olt float %y, 0.000000e+00
+  %and = and i1 %cmp, %x
+  %sel = select i1 %and, float -1.000000e+00, float 1.000000e+00
+  %res = tail call float @llvm.copysign.f32(float %z, float %sel)
+  ret float %res
+}
+
+define float @copysign_conditional2(i1 %x, float %y, float %z) {
+; CHECK-LABEL: @copysign_conditional2(
+; CHECK-NEXT:    [[RES:%.*]] = tail call float @llvm.copysign.f32(float [[Z:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %cmp = fcmp ogt float %y, 0.000000e+00
+  %and = and i1 %cmp, %x
+  %sel = select i1 %and, float -1.000000e+00, float 1.000000e+00
+  %res = tail call float @llvm.copysign.f32(float %z, float %sel)
+  ret float %res
+}
+
+define float @copysign_conditional3(i1 %x, float %y, float %z) {
+; CHECK-LABEL: @copysign_conditional3(
+; CHECK-NEXT:    [[RES:%.*]] = tail call float @llvm.copysign.f32(float [[Z:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %cmp = fcmp uge float %y, 0.000000e+00
+  %and  = and i1 %cmp, %x
+  %sel = select i1 %and, float -1.000000e+00, float 1.000000e+00
+  %res = tail call float @llvm.copysign.f32(float %z, float %sel)
+  ret float %res
+}
+
+define float @copysign_conditional4(i1 %x, float %y, float %z) {
+; CHECK-LABEL: @copysign_conditional4(
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp fast olt float [[Y:%.*]], 0.000000e+00
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[X:%.*]], i1 [[CMP]], i1 false
+; CHECK-NEXT:    [[COND1:%.*]] = select fast i1 [[SEL]], float -1.000000e+00, float 1.000000e+00
+; CHECK-NEXT:    [[RES:%.*]] = tail call fast float @llvm.copysign.f32(float [[Z:%.*]], float [[COND1]])
+; CHECK-NEXT:    ret float [[RES]]
+;
+  %cmp = fcmp fast olt float %y, 0.000000e+00
+  %sel = select i1 %x, i1 %cmp, i1 false
+  %cond1 = select fast i1 %sel, float -1.000000e+00, float 1.000000e+00
+  %res = tail call fast float @llvm.copysign.f32(float %z, float %cond1)
+  ret float %res
+}
----------------
arsenm wrote:

Missing tests for all the compare types, at least ule is missing 

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


More information about the llvm-commits mailing list