[llvm] [InstCombine] Fold copysign of selects from sign comparison to sign operand (PR #85627)
Krishna Narayanan via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 29 11:13:53 PDT 2024
================
@@ -109,3 +109,18 @@ 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 noundef zeroext %x, float %y, float %z) {
+; CHECK-LABEL: @copysign_conditional(
+; CHECK-NEXT: [[CMP:%.*]] = fcmp olt float [[Y:%.*]], 0.000000e+00
+; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP]], [[X:%.*]]
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[AND]], float -1.000000e+00, float 1.000000e+00
+; CHECK-NEXT: [[RES:%.*]] = tail call float @llvm.copysign.f32(float [[Z:%.*]], float [[SEL]])
+; 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
+}
----------------
Krishna-13-cyber wrote:
1. I tested it for a set of predicates, the case where there’s a bit of deviation is in the oeq case, tried to handle it with specific values. On adding the below patch, this leads to a different folding (fabs and fneg) and changes results for all the predicates.
```
/*
oeq
copysign(Mag, B & (A == 0.0) ? -TC : TC) --> copysign(Mag, 1) B->true.
copysign(Mag, B & (A == 0.0) ? -TC : TC) --> copysign(Mag, -1) B->false.
*/
Value *One;
One = ConstantFP::get(Mag->getType(), 1.0);
if (Pred == CmpInst::FCMP_OEQ)
if (match(B, m_Zero()) && TC->isNegative())
return replaceOperand(*II, 1, One);
if (!match(B, m_Zero()) && TC->isNegative())
One = Builder.CreateFNeg(One);
return replaceOperand(*II, 1, One);
```
2. Added test.
3. For identical constants(cases), suppose if we have both as negative or positive, it's folded into fabs.
https://godbolt.org/z/qrfWvr465
https://github.com/llvm/llvm-project/pull/85627
More information about the llvm-commits
mailing list