Skip to content

Commit

Permalink
[InstCombine] enhance fold for copysign with known sign arg
Browse files Browse the repository at this point in the history
This is another optimization suggested in PRPR44153:
https://bugs.llvm.org/show_bug.cgi?id=44153
  • Loading branch information
rotateright committed Dec 22, 2019
1 parent dc5b614 commit 9cdcd81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 12 additions & 8 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Expand Up @@ -2286,18 +2286,22 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
break;
}
case Intrinsic::copysign: {
const APFloat *C;
if (match(II->getArgOperand(1), m_APFloat(C))) {
// If we know the sign bit of the sign argument, reduce to FABS/FNABS:
// copysign X, PosC --> fabs X
// copysign X, NegC --> fneg (fabs X)
if (SignBitMustBeZero(II->getArgOperand(1), &TLI)) {
// If we know that the sign argument is positive, reduce to FABS:
// copysign X, Pos --> fabs X
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs,
II->getArgOperand(0), II);
if (C->isNegative())
Fabs = Builder.CreateFNegFMF(Fabs, II);

return replaceInstUsesWith(*II, Fabs);
}
// TODO: There should be a ValueTracking sibling like SignBitMustBeOne.
const APFloat *C;
if (match(II->getArgOperand(1), m_APFloat(C)) && C->isNegative()) {
// If we know that the sign argument is negative, reduce to FNABS:
// copysign X, Neg --> fneg (fabs X)
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs,
II->getArgOperand(0), II);
return replaceInstUsesWith(*II, Builder.CreateFNegFMF(Fabs, II));
}
break;
}
case Intrinsic::fabs: {
Expand Down
10 changes: 4 additions & 6 deletions llvm/test/Transforms/InstCombine/copysign.ll
Expand Up @@ -45,9 +45,8 @@ define <3 x double> @negative_sign_arg_vec_splat(<3 x double> %x) {

define float @known_positive_sign_arg(float %x, float %y) {
; CHECK-LABEL: @known_positive_sign_arg(
; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[Y:%.*]])
; CHECK-NEXT: [[R:%.*]] = call ninf float @llvm.copysign.f32(float [[X:%.*]], float [[FABS]])
; CHECK-NEXT: ret float [[R]]
; CHECK-NEXT: [[TMP1:%.*]] = call ninf float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%fabs = call float @llvm.fabs.f32(float %y)
%r = call ninf float @llvm.copysign.f32(float %x, float %fabs)
Expand All @@ -56,9 +55,8 @@ define float @known_positive_sign_arg(float %x, float %y) {

define <3 x double> @known_positive_sign_arg_vec(<3 x double> %x, <3 x i32> %y) {
; CHECK-LABEL: @known_positive_sign_arg_vec(
; CHECK-NEXT: [[YF:%.*]] = uitofp <3 x i32> [[Y:%.*]] to <3 x double>
; CHECK-NEXT: [[R:%.*]] = call arcp <3 x double> @llvm.copysign.v3f64(<3 x double> [[X:%.*]], <3 x double> [[YF]])
; CHECK-NEXT: ret <3 x double> [[R]]
; CHECK-NEXT: [[TMP1:%.*]] = call arcp <3 x double> @llvm.fabs.v3f64(<3 x double> [[X:%.*]])
; CHECK-NEXT: ret <3 x double> [[TMP1]]
;
%yf = uitofp <3 x i32> %y to <3 x double>
%r = call arcp <3 x double> @llvm.copysign.v3f64(<3 x double> %x, <3 x double> %yf)
Expand Down

0 comments on commit 9cdcd81

Please sign in to comment.