[llvm] [ValueTracking] Properly relax operand zero signs for instructions with nsz (PR #207623)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 6 12:50:55 PDT 2026
================
@@ -4941,6 +4941,40 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
FPClassTest InterestedClasses, KnownFPClass &Known,
const SimplifyQuery &Q, unsigned Depth);
+/// Evaluates the known FP classes for an operand of an instruction.
+/// If the instruction has the 'nsz' flag, it non-deterministically relaxes
+/// the zero sign in the known classes.
+static void
+computeKnownFPClassForOperand(const Value *V, const APInt &DemandedElts,
+ FPClassTest InterestedClasses,
+ KnownFPClass &Known, const SimplifyQuery &Q,
+ unsigned Depth, const Operator *CxtI) {
+ computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
+
+ auto *I = dyn_cast_or_null<Instruction>(CxtI);
+ if (!I || !Q.IIQ.hasNoSignedZeros(I))
+ return;
+
+ Type *Ty = V->getType()->getScalarType();
+ if (!Ty->isFloatingPointTy())
+ return;
+
+ const Function *F = I->getFunction();
+ DenormalMode Mode = F ? F->getDenormalMode(Ty->getFltSemantics())
+ : DenormalMode::getDynamic();
+
+ bool NeverPosZero = Known.isKnownNeverLogicalPosZero(Mode);
+ bool NeverNegZero = Known.isKnownNeverLogicalNegZero(Mode);
+
+ if (NeverPosZero != NeverNegZero) {
+ if (NeverPosZero)
+ Known.KnownFPClasses |= fcPosZero;
+ else
+ Known.KnownFPClasses |= fcNegZero;
+ Known.SignBit = std::nullopt;
----------------
nikic wrote:
I do believe this needs to be a fixup on the computeKnownFPClass() result. The computeKnownFPClass() result itself is correct for the return value of the instruction, it just needs to be adjusted if used as an argument to another instruction with nsz.
https://github.com/llvm/llvm-project/pull/207623
More information about the llvm-commits
mailing list