[clang] [llvm] [ValueTracking] use KnownBits to compute fpclass from bitcast (PR #97762)
Alex MacLean via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 29 13:11:03 PDT 2024
================
@@ -5921,6 +5921,61 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
break;
}
+ case Instruction::BitCast: {
+ const Value *Src;
+ if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
+ !Src->getType()->isIntOrIntVectorTy())
+ break;
+
+ const Type *Ty = Op->getType()->getScalarType();
+ KnownBits Bits(Ty->getScalarSizeInBits());
+ computeKnownBits(Src, DemandedElts, Bits, Depth + 1, Q);
+
+ // Transfer information from the sign bit.
+ if (Bits.isNonNegative())
+ Known.signBitMustBeZero();
+ else if (Bits.isNegative())
+ Known.signBitMustBeOne();
+
+ if (Ty->isIEEE()) {
+ // IEEE floats are NaN when all bits of the exponent plus at least one of
+ // the fraction bits are 1. This means:
+ // - If we assume unknown bits are 0 and the value is NaN, it will
+ // always be NaN
+ // - If we assume unknown bits are 1 and the value is not NaN, it can
+ // never be NaN
+ if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
+ Known.KnownFPClasses = fcNan;
+ else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
+ Known.knownNot(fcNan);
+
+ // Build KnownBits representing Inf and check if it must be equal or
+ // unequal to this value.
+ auto InfKB = KnownBits::makeConstant(
+ APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
+ InfKB.Zero.clearSignBit();
+ if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
----------------
AlexMaclean wrote:
If that is the case the values will be:
```
Bits = 1 11111111 00000000000000000000000
InfKB = ? 11111111 00000000000000000000000
```
These may or may not be equal so `std::nullopt` will be returned and no information will be added to the fpclass. I suppose we could handle this case but it will be constant folded anyway so I don't think it is really necessary.
https://github.com/llvm/llvm-project/pull/97762
More information about the cfe-commits
mailing list