[InstCombine] enhance fold for copysign with known sign arg

This is another optimization suggested in PRPR44153:
https://bugs.llvm.org/show_bug.cgi?id=44153
This commit is contained in:
Sanjay Patel 2019-12-22 10:05:28 -05:00
parent dc5b614fa9
commit 9cdcd81d3f
2 changed files with 16 additions and 14 deletions

View File

@ -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: {

View File

@ -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)
@ -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)