Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1234,6 +1234,14 @@ return &I; } + // X / fabs(X) -> copysign(1.0, X) + if (I.hasNoNaNs() && I.hasNoInfs() && + match(Op1, m_Intrinsic(m_Specific(Op0))) && + Op1->hasOneUse()) { + Value *V = Builder.CreateBinaryIntrinsic( + Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), Op0, &I); + return replaceInstUsesWith(I, V); + } return nullptr; } Index: test/Transforms/InstCombine/fabs-copysign.ll =================================================================== --- test/Transforms/InstCombine/fabs-copysign.ll +++ test/Transforms/InstCombine/fabs-copysign.ll @@ -10,9 +10,8 @@ define double @fabs_copysign(double %x) { ; CHECK-LABEL: @fabs_copysign( -; CHECK-NEXT: [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]]) -; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf double [[X]], [[F]] -; CHECK-NEXT: ret double [[DIV]] +; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf double @llvm.copysign.f64(double 1.000000e+00, double [[X:%.*]]) +; CHECK-NEXT: ret double [[TMP1]] ; %f = tail call double @llvm.fabs.f64(double %x) %div = fdiv nnan ninf double %x, %f @@ -22,9 +21,8 @@ define float @fabs_copysignf(float %x) { ; CHECK-LABEL: @fabs_copysignf( -; CHECK-NEXT: [[F:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]]) -; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf float [[X]], [[F]] -; CHECK-NEXT: ret float [[DIV]] +; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf float @llvm.copysign.f32(float 1.000000e+00, float [[X:%.*]]) +; CHECK-NEXT: ret float [[TMP1]] ; %f = tail call float @llvm.fabs.f32(float %x) %div = fdiv nnan ninf float %x, %f @@ -33,9 +31,8 @@ define double @fabs_copysign_use(double %x) { ; CHECK-LABEL: @fabs_copysign_use( -; CHECK-NEXT: [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]]) -; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf double [[X]], [[F]] -; CHECK-NEXT: ret double [[DIV]] +; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf double @llvm.copysign.f64(double 1.000000e+00, double [[X:%.*]]) +; CHECK-NEXT: ret double [[TMP1]] ; %f = tail call double @llvm.fabs.f64(double %x) %div = fdiv nnan ninf double %x, %f