@@ -3706,205 +3706,6 @@ Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB,
3706
3706
return Intrinsic::not_intrinsic;
3707
3707
}
3708
3708
3709
- // / Deprecated, use computeKnownFPClass instead.
3710
- // /
3711
- // / If \p SignBitOnly is true, test for a known 0 sign bit rather than a
3712
- // / standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
3713
- // / bit despite comparing equal.
3714
- static bool cannotBeOrderedLessThanZeroImpl (const Value *V,
3715
- const DataLayout &DL,
3716
- const TargetLibraryInfo *TLI,
3717
- bool SignBitOnly, unsigned Depth) {
3718
- // TODO: This function does not do the right thing when SignBitOnly is true
3719
- // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
3720
- // which flips the sign bits of NaNs. See
3721
- // https://llvm.org/bugs/show_bug.cgi?id=31702.
3722
-
3723
- if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
3724
- return !CFP->getValueAPF ().isNegative () ||
3725
- (!SignBitOnly && CFP->getValueAPF ().isZero ());
3726
- }
3727
-
3728
- // Handle vector of constants.
3729
- if (auto *CV = dyn_cast<Constant>(V)) {
3730
- if (auto *CVFVTy = dyn_cast<FixedVectorType>(CV->getType ())) {
3731
- unsigned NumElts = CVFVTy->getNumElements ();
3732
- for (unsigned i = 0 ; i != NumElts; ++i) {
3733
- auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement (i));
3734
- if (!CFP)
3735
- return false ;
3736
- if (CFP->getValueAPF ().isNegative () &&
3737
- (SignBitOnly || !CFP->getValueAPF ().isZero ()))
3738
- return false ;
3739
- }
3740
-
3741
- // All non-negative ConstantFPs.
3742
- return true ;
3743
- }
3744
- }
3745
-
3746
- if (Depth == MaxAnalysisRecursionDepth)
3747
- return false ;
3748
-
3749
- const Operator *I = dyn_cast<Operator>(V);
3750
- if (!I)
3751
- return false ;
3752
-
3753
- switch (I->getOpcode ()) {
3754
- default :
3755
- break ;
3756
- // Unsigned integers are always nonnegative.
3757
- case Instruction::UIToFP:
3758
- return true ;
3759
- case Instruction::FDiv:
3760
- // X / X is always exactly 1.0 or a NaN.
3761
- if (I->getOperand (0 ) == I->getOperand (1 ) &&
3762
- (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs ()))
3763
- return true ;
3764
-
3765
- // Set SignBitOnly for RHS, because X / -0.0 is -Inf (or NaN).
3766
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3767
- SignBitOnly, Depth + 1 ) &&
3768
- cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI,
3769
- /* SignBitOnly*/ true , Depth + 1 );
3770
- case Instruction::FMul:
3771
- // X * X is always non-negative or a NaN.
3772
- if (I->getOperand (0 ) == I->getOperand (1 ) &&
3773
- (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs ()))
3774
- return true ;
3775
-
3776
- [[fallthrough]];
3777
- case Instruction::FAdd:
3778
- case Instruction::FRem:
3779
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3780
- SignBitOnly, Depth + 1 ) &&
3781
- cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI,
3782
- SignBitOnly, Depth + 1 );
3783
- case Instruction::Select:
3784
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI,
3785
- SignBitOnly, Depth + 1 ) &&
3786
- cannotBeOrderedLessThanZeroImpl (I->getOperand (2 ), DL, TLI,
3787
- SignBitOnly, Depth + 1 );
3788
- case Instruction::FPExt:
3789
- case Instruction::FPTrunc:
3790
- // Widening/narrowing never change sign.
3791
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3792
- SignBitOnly, Depth + 1 );
3793
- case Instruction::ExtractElement:
3794
- // Look through extract element. At the moment we keep this simple and skip
3795
- // tracking the specific element. But at least we might find information
3796
- // valid for all elements of the vector.
3797
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3798
- SignBitOnly, Depth + 1 );
3799
- case Instruction::Call:
3800
- const auto *CI = cast<CallInst>(I);
3801
- Intrinsic::ID IID = getIntrinsicForCallSite (*CI, TLI);
3802
- switch (IID) {
3803
- default :
3804
- break ;
3805
- case Intrinsic::canonicalize:
3806
- case Intrinsic::arithmetic_fence:
3807
- case Intrinsic::floor :
3808
- case Intrinsic::ceil :
3809
- case Intrinsic::trunc :
3810
- case Intrinsic::rint :
3811
- case Intrinsic::nearbyint :
3812
- case Intrinsic::round :
3813
- case Intrinsic::roundeven:
3814
- case Intrinsic::fptrunc_round:
3815
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3816
- SignBitOnly, Depth + 1 );
3817
- case Intrinsic::maxnum: {
3818
- Value *V0 = I->getOperand (0 ), *V1 = I->getOperand (1 );
3819
- auto isPositiveNum = [&](Value *V) {
3820
- if (SignBitOnly) {
3821
- // With SignBitOnly, this is tricky because the result of
3822
- // maxnum(+0.0, -0.0) is unspecified. Just check if the operand is
3823
- // a constant strictly greater than 0.0.
3824
- const APFloat *C;
3825
- return match (V, m_APFloat (C)) &&
3826
- *C > APFloat::getZero (C->getSemantics ());
3827
- }
3828
-
3829
- // -0.0 compares equal to 0.0, so if this operand is at least -0.0,
3830
- // maxnum can't be ordered-less-than-zero.
3831
- return isKnownNeverNaN (V, DL, TLI) &&
3832
- cannotBeOrderedLessThanZeroImpl (V, DL, TLI, false , Depth + 1 );
3833
- };
3834
-
3835
- // TODO: This could be improved. We could also check that neither operand
3836
- // has its sign bit set (and at least 1 is not-NAN?).
3837
- return isPositiveNum (V0) || isPositiveNum (V1);
3838
- }
3839
-
3840
- case Intrinsic::maximum:
3841
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3842
- SignBitOnly, Depth + 1 ) ||
3843
- cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI,
3844
- SignBitOnly, Depth + 1 );
3845
- case Intrinsic::minnum:
3846
- case Intrinsic::minimum:
3847
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3848
- SignBitOnly, Depth + 1 ) &&
3849
- cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI,
3850
- SignBitOnly, Depth + 1 );
3851
- case Intrinsic::exp :
3852
- case Intrinsic::exp2 :
3853
- case Intrinsic::fabs :
3854
- return true ;
3855
- case Intrinsic::copysign :
3856
- // Only the sign operand matters.
3857
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (1 ), DL, TLI, true ,
3858
- Depth + 1 );
3859
- case Intrinsic::sqrt :
3860
- // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0.
3861
- if (!SignBitOnly)
3862
- return true ;
3863
- return CI->hasNoNaNs () &&
3864
- (CI->hasNoSignedZeros () ||
3865
- cannotBeNegativeZero (CI->getOperand (0 ), DL, TLI));
3866
-
3867
- case Intrinsic::powi:
3868
- if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand (1 ))) {
3869
- // powi(x,n) is non-negative if n is even.
3870
- if (Exponent->getBitWidth () <= 64 && Exponent->getSExtValue () % 2u == 0 )
3871
- return true ;
3872
- }
3873
- // TODO: This is not correct. Given that exp is an integer, here are the
3874
- // ways that pow can return a negative value:
3875
- //
3876
- // pow(x, exp) --> negative if exp is odd and x is negative.
3877
- // pow(-0, exp) --> -inf if exp is negative odd.
3878
- // pow(-0, exp) --> -0 if exp is positive odd.
3879
- // pow(-inf, exp) --> -0 if exp is negative odd.
3880
- // pow(-inf, exp) --> -inf if exp is positive odd.
3881
- //
3882
- // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
3883
- // but we must return false if x == -0. Unfortunately we do not currently
3884
- // have a way of expressing this constraint. See details in
3885
- // https://llvm.org/bugs/show_bug.cgi?id=31702.
3886
- return cannotBeOrderedLessThanZeroImpl (I->getOperand (0 ), DL, TLI,
3887
- SignBitOnly, Depth + 1 );
3888
-
3889
- case Intrinsic::fma :
3890
- case Intrinsic::fmuladd:
3891
- // x*x+y is non-negative if y is non-negative.
3892
- return I->getOperand (0 ) == I->getOperand (1 ) &&
3893
- (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs ()) &&
3894
- cannotBeOrderedLessThanZeroImpl (I->getOperand (2 ), DL, TLI,
3895
- SignBitOnly, Depth + 1 );
3896
- }
3897
- break ;
3898
- }
3899
- return false ;
3900
- }
3901
-
3902
- bool llvm::SignBitMustBeZero (const Value *V, const DataLayout &DL,
3903
- const TargetLibraryInfo *TLI) {
3904
- // FIXME: Use computeKnownFPClass and pass all arguments
3905
- return cannotBeOrderedLessThanZeroImpl (V, DL, TLI, true , 0 );
3906
- }
3907
-
3908
3709
// / Return true if it's possible to assume IEEE treatment of input denormals in
3909
3710
// / \p F for \p Val.
3910
3711
static bool inputDenormalIsIEEE (const Function &F, const Type *Ty) {
@@ -4489,7 +4290,6 @@ static void computeKnownFPClassForFPTrunc(const Operator *Op,
4489
4290
// Infinity needs a range check.
4490
4291
}
4491
4292
4492
- // TODO: Merge implementation of cannotBeOrderedLessThanZero into here.
4493
4293
void computeKnownFPClass (const Value *V, const APInt &DemandedElts,
4494
4294
FPClassTest InterestedClasses, KnownFPClass &Known,
4495
4295
unsigned Depth, const SimplifyQuery &Q) {
@@ -4514,6 +4314,8 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4514
4314
const Constant *CV = dyn_cast<Constant>(V);
4515
4315
if (VFVTy && CV) {
4516
4316
Known.KnownFPClasses = fcNone;
4317
+ bool SignBitAllZero = true ;
4318
+ bool SignBitAllOne = true ;
4517
4319
4518
4320
// For vectors, verify that each element is not NaN.
4519
4321
unsigned NumElts = VFVTy->getNumElements ();
@@ -4531,10 +4333,15 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4531
4333
return ;
4532
4334
}
4533
4335
4534
- KnownFPClass KnownElt{CElt->getValueAPF ().classify (), CElt->isNegative ()};
4535
- Known |= KnownElt;
4336
+ const APFloat &C = CElt->getValueAPF ();
4337
+ Known.KnownFPClasses |= C.classify ();
4338
+ if (C.isNegative ())
4339
+ SignBitAllZero = false ;
4340
+ else
4341
+ SignBitAllOne = false ;
4536
4342
}
4537
-
4343
+ if (SignBitAllOne != SignBitAllZero)
4344
+ Known.SignBit = SignBitAllOne;
4538
4345
return ;
4539
4346
}
4540
4347
@@ -4673,7 +4480,6 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4673
4480
computeKnownFPClass (II->getArgOperand (2 ), DemandedElts, InterestedClasses,
4674
4481
KnownAddend, Depth + 1 , Q);
4675
4482
4676
- // TODO: Known sign bit with no nans
4677
4483
if (KnownAddend.cannotBeOrderedLessThanZero ())
4678
4484
Known.knownNot (fcNegative);
4679
4485
break ;
@@ -4707,7 +4513,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4707
4513
(F && KnownSrc.isKnownNeverLogicalNegZero (*F, II->getType ()))) {
4708
4514
Known.knownNot (fcNegZero);
4709
4515
if (KnownSrc.isKnownNeverNaN ())
4710
- Known.SignBit = false ;
4516
+ Known.signBitMustBeZero () ;
4711
4517
}
4712
4518
4713
4519
break ;
@@ -4777,7 +4583,6 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4777
4583
// subtargets on AMDGPU the min/max instructions would not flush the
4778
4584
// output and return the original value.
4779
4585
//
4780
- // TODO: This could be refined based on the sign
4781
4586
if ((Known.KnownFPClasses & fcZero) != fcNone &&
4782
4587
!Known.isKnownNeverSubnormal ()) {
4783
4588
const Function *Parent = II->getFunction ();
@@ -4790,6 +4595,26 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4790
4595
Known.KnownFPClasses |= fcZero;
4791
4596
}
4792
4597
4598
+ if (Known.isKnownNeverNaN ()) {
4599
+ if (KnownLHS.SignBit && KnownRHS.SignBit &&
4600
+ *KnownLHS.SignBit == *KnownRHS.SignBit ) {
4601
+ if (*KnownLHS.SignBit )
4602
+ Known.signBitMustBeOne ();
4603
+ else
4604
+ Known.signBitMustBeZero ();
4605
+ } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
4606
+ ((KnownLHS.isKnownNeverNegZero () ||
4607
+ KnownRHS.isKnownNeverPosZero ()) &&
4608
+ (KnownLHS.isKnownNeverPosZero () ||
4609
+ KnownRHS.isKnownNeverNegZero ()))) {
4610
+ if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
4611
+ (KnownLHS.SignBit == false || KnownRHS.SignBit == false ))
4612
+ Known.signBitMustBeZero ();
4613
+ else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
4614
+ (KnownLHS.SignBit == true || KnownRHS.SignBit == true ))
4615
+ Known.signBitMustBeOne ();
4616
+ }
4617
+ }
4793
4618
break ;
4794
4619
}
4795
4620
case Intrinsic::canonicalize: {
@@ -4889,7 +4714,7 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4889
4714
KnownSrc, Depth + 1 , Q);
4890
4715
if (KnownSrc.isKnownNeverNaN ()) {
4891
4716
Known.knownNot (fcNan);
4892
- Known.SignBit = false ;
4717
+ Known.signBitMustBeZero () ;
4893
4718
}
4894
4719
4895
4720
break ;
@@ -5139,6 +4964,13 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5139
4964
if (!KnownLHS.isKnownNeverNaN ())
5140
4965
break ;
5141
4966
4967
+ if (KnownLHS.SignBit && KnownRHS.SignBit ) {
4968
+ if (*KnownLHS.SignBit == *KnownRHS.SignBit )
4969
+ Known.signBitMustBeZero ();
4970
+ else
4971
+ Known.signBitMustBeOne ();
4972
+ }
4973
+
5142
4974
// If 0 * +/-inf produces NaN.
5143
4975
if (KnownLHS.isKnownNeverInfinity () && KnownRHS.isKnownNeverInfinity ()) {
5144
4976
Known.knownNot (fcNan);
0 commit comments