Index: llvm/include/llvm/Analysis/ValueTracking.h =================================================================== --- llvm/include/llvm/Analysis/ValueTracking.h +++ llvm/include/llvm/Analysis/ValueTracking.h @@ -249,6 +249,11 @@ return (KnownFPClasses & fcInf) == fcNone; } + /// Return true if it's known this can never be +infinity. + bool isKnownNeverPosInfinity() const { + return (KnownFPClasses & fcPosInf) == fcNone; + } + /// Return true if it's known this can never be a zero. bool isKnownNeverZero() const { return (KnownFPClasses & fcZero) == fcNone; Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -4432,6 +4432,36 @@ Known.copysign(KnownSign); break; } + case Intrinsic::sqrt: { + KnownFPClass KnownSrc; + computeKnownFPClass(II->getArgOperand(0), DemandedElts, + InterestedClasses, KnownSrc, Depth + 1, Q, TLI); + + if (KnownSrc.isKnownNeverPosInfinity()) + Known.knownNot(fcPosInf); + + if (KnownSrc.isKnownNeverNaN() && + (InterestedClasses & fcNan) != fcNone && + cannotBeOrderedLessThanZeroImpl(II->getArgOperand(0), TLI, false, + Depth + 1)) + Known.knownNot(fcNan); + + // Any negative value besides -0 returns a nan. + if (KnownSrc.isKnownNeverNaN() && KnownSrc.SignBit && + !*KnownSrc.SignBit) { + Known.knownNot(fcNan); + Known.SignBit = false; + } + + // The only negative value that can be returned is -0 for -0 inputs. + Known.knownNot(fcNegInf | fcNegSubnormal | fcNegNormal); + if ((KnownSrc.KnownFPClasses & fcNegZero) == fcNone) { + Known.knownNot(fcNegZero); + Known.SignBit = false; + } + + break; + } case Intrinsic::sin: case Intrinsic::cos: { // Return NaN on infinite inputs. Index: llvm/test/Transforms/Attributor/nofpclass-sqrt.ll =================================================================== --- llvm/test/Transforms/Attributor/nofpclass-sqrt.ll +++ llvm/test/Transforms/Attributor/nofpclass-sqrt.ll @@ -4,9 +4,9 @@ declare float @llvm.sqrt.f32(float) define float @ret_sqrt(float %arg0) { -; CHECK-LABEL: define float @ret_sqrt +; CHECK-LABEL: define nofpclass(ninf nsub nnorm) float @ret_sqrt ; CHECK-SAME: (float [[ARG0:%.*]]) #[[ATTR1:[0-9]+]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2:[0-9]+]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(ninf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2:[0-9]+]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -14,9 +14,9 @@ } define float @ret_sqrt_noinf(float nofpclass(inf) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_noinf +; CHECK-LABEL: define nofpclass(inf nsub nnorm) float @ret_sqrt_noinf ; CHECK-SAME: (float nofpclass(inf) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -24,9 +24,9 @@ } define float @ret_sqrt_nopinf(float nofpclass(pinf) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_nopinf +; CHECK-LABEL: define nofpclass(inf nsub nnorm) float @ret_sqrt_nopinf ; CHECK-SAME: (float nofpclass(pinf) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -34,9 +34,9 @@ } define float @ret_sqrt_noninf(float nofpclass(ninf) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_noninf +; CHECK-LABEL: define nofpclass(ninf nsub nnorm) float @ret_sqrt_noninf ; CHECK-SAME: (float nofpclass(ninf) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(ninf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -44,9 +44,9 @@ } define float @ret_sqrt_nonan(float nofpclass(nan) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_nonan +; CHECK-LABEL: define nofpclass(ninf nsub nnorm) float @ret_sqrt_nonan ; CHECK-SAME: (float nofpclass(nan) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(ninf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -54,9 +54,9 @@ } define float @ret_sqrt_nonan_noinf(float nofpclass(nan inf) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_nonan_noinf +; CHECK-LABEL: define nofpclass(inf nsub nnorm) float @ret_sqrt_nonan_noinf ; CHECK-SAME: (float nofpclass(nan inf) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -64,9 +64,9 @@ } define float @ret_sqrt_nonan_noinf_nozero(float nofpclass(nan inf zero) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_nonan_noinf_nozero +; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_sqrt_nonan_noinf_nozero ; CHECK-SAME: (float nofpclass(nan inf zero) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -74,9 +74,9 @@ } define float @ret_sqrt_noinf_nozero(float nofpclass(inf zero) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_noinf_nozero +; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_sqrt_noinf_nozero ; CHECK-SAME: (float nofpclass(inf zero) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -84,9 +84,9 @@ } define float @ret_sqrt_noinf_nonegzero(float nofpclass(inf nzero) %arg0) { -; CHECK-LABEL: define float @ret_sqrt_noinf_nonegzero +; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_sqrt_noinf_nonegzero ; CHECK-SAME: (float nofpclass(inf nzero) [[ARG0:%.*]]) #[[ATTR1]] { -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.sqrt.f32(float [[ARG0]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %call = call float @llvm.sqrt.f32(float %arg0) @@ -94,10 +94,10 @@ } define float @ret_sqrt_positive_source(i32 %arg) { -; CHECK-LABEL: define float @ret_sqrt_positive_source +; CHECK-LABEL: define nofpclass(nan inf nzero nsub nnorm) float @ret_sqrt_positive_source ; CHECK-SAME: (i32 [[ARG:%.*]]) #[[ATTR1]] { ; CHECK-NEXT: [[UITOFP:%.*]] = uitofp i32 [[ARG]] to float -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[UITOFP]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(nan inf nzero nsub nnorm) float @llvm.sqrt.f32(float [[UITOFP]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %uitofp = uitofp i32 %arg to float @@ -107,10 +107,10 @@ ; Could produce a nan because we don't know if the multiply is negative. define float @ret_sqrt_unknown_sign(float nofpclass(nan) %arg0, float nofpclass(nan) %arg1) { -; CHECK-LABEL: define float @ret_sqrt_unknown_sign +; CHECK-LABEL: define nofpclass(ninf nsub nnorm) float @ret_sqrt_unknown_sign ; CHECK-SAME: (float nofpclass(nan) [[ARG0:%.*]], float nofpclass(nan) [[ARG1:%.*]]) #[[ATTR1]] { ; CHECK-NEXT: [[UNKNOWN_SIGN_NOT_NAN:%.*]] = fmul nnan float [[ARG0]], [[ARG1]] -; CHECK-NEXT: [[CALL:%.*]] = call float @llvm.sqrt.f32(float [[UNKNOWN_SIGN_NOT_NAN]]) #[[ATTR2]] +; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(ninf nsub nnorm) float @llvm.sqrt.f32(float [[UNKNOWN_SIGN_NOT_NAN]]) #[[ATTR2]] ; CHECK-NEXT: ret float [[CALL]] ; %unknown.sign.not.nan = fmul nnan float %arg0, %arg1 Index: llvm/unittests/Analysis/ValueTrackingTest.cpp =================================================================== --- llvm/unittests/Analysis/ValueTrackingTest.cpp +++ llvm/unittests/Analysis/ValueTrackingTest.cpp @@ -1498,10 +1498,10 @@ TEST_F(ComputeKnownFPClassTest, CopySignNInfSrc0_NegSign) { parseAssembly( - "declare float @llvm.sqrt.f32(float)\n" + "declare float @llvm.log.f32(float)\n" "declare float @llvm.copysign.f32(float, float)\n" "define float @test(float %arg0, float %arg1) {\n" - " %ninf = call ninf float @llvm.sqrt.f32(float %arg0)" + " %ninf = call ninf float @llvm.log.f32(float %arg0)" " %A = call float @llvm.copysign.f32(float %ninf, float -1.0)" " ret float %A\n" "}\n");