[llvm] 82a1905 - InstCombine: Pass SimplifyQuery through SimplifyDemandedFPClass (#184096)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 2 04:00:30 PST 2026
Author: Matt Arsenault
Date: 2026-03-02T12:00:25Z
New Revision: 82a1905c4bd33c26d8d91c3ba892652d978f7b74
URL: https://github.com/llvm/llvm-project/commit/82a1905c4bd33c26d8d91c3ba892652d978f7b74
DIFF: https://github.com/llvm/llvm-project/commit/82a1905c4bd33c26d8d91c3ba892652d978f7b74.diff
LOG: InstCombine: Pass SimplifyQuery through SimplifyDemandedFPClass (#184096)
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 55a70115feb36..4c8144f088faa 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1165,7 +1165,8 @@ Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) {
return replaceInstUsesWith(II, FCmp);
}
- KnownFPClass Known = computeKnownFPClass(Src0, Mask, &II);
+ KnownFPClass Known =
+ computeKnownFPClass(Src0, Mask, SQ.getWithInstruction(&II));
// Clear test bits we know must be false from the source value.
// fp_class (nnan x), qnan|snan|other -> fp_class (nnan x), other
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 2e7758e952eaf..053a3a2b85b36 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -203,23 +203,6 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
const Twine &Suffix = "");
- KnownFPClass computeKnownFPClass(Value *Val, FastMathFlags FMF,
- FPClassTest Interested = fcAllFlags,
- const Instruction *CtxI = nullptr,
- unsigned Depth = 0) const {
- return llvm::computeKnownFPClass(
- Val, FMF, Interested, getSimplifyQuery().getWithInstruction(CtxI),
- Depth);
- }
-
- KnownFPClass computeKnownFPClass(Value *Val,
- FPClassTest Interested = fcAllFlags,
- const Instruction *CtxI = nullptr,
- unsigned Depth = 0) const {
- return llvm::computeKnownFPClass(
- Val, Interested, getSimplifyQuery().getWithInstruction(CtxI), Depth);
- }
-
/// Check if fmul \p MulVal, +0.0 will yield +0.0 (or signed zero is
/// ignorable).
bool fmulByZeroIsZero(Value *MulVal, FastMathFlags FMF,
@@ -613,12 +596,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
/// Attempts to replace V with a simpler value based on the demanded
/// floating-point classes
Value *SimplifyDemandedUseFPClass(Instruction *I, FPClassTest DemandedMask,
- KnownFPClass &Known, Instruction *CxtI,
+ KnownFPClass &Known, const SimplifyQuery &Q,
unsigned Depth = 0);
Value *SimplifyMultipleUseDemandedFPClass(Instruction *I,
FPClassTest DemandedMask,
KnownFPClass &Known,
- Instruction *CxtI, unsigned Depth);
+ const SimplifyQuery &Q,
+ unsigned Depth);
bool SimplifyDemandedFPClass(Instruction *I, unsigned Op,
FPClassTest DemandedMask, KnownFPClass &Known,
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 6707d1abf5ca0..ebdccf88b9222 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -608,7 +608,8 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
// have preserved the exact NaN bit-pattern.
// Avoid the folding if the false value might be a NaN.
if (isa<FPMathOperator>(&SI) &&
- !computeKnownFPClass(FalseVal, FMF, fcNan, &SI).isKnownNeverNaN())
+ !computeKnownFPClass(FalseVal, FMF, fcNan, SQ.getWithInstruction(&SI))
+ .isKnownNeverNaN())
return nullptr;
Value *NewSel = Builder.CreateSelect(SI.getCondition(), Swapped ? C : OOp,
@@ -4114,7 +4115,8 @@ Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
bool InstCombinerImpl::fmulByZeroIsZero(Value *MulVal, FastMathFlags FMF,
const Instruction *CtxI) const {
- KnownFPClass Known = computeKnownFPClass(MulVal, FMF, fcNegative, CtxI);
+ KnownFPClass Known =
+ computeKnownFPClass(MulVal, FMF, fcNegative, SQ.getWithInstruction(CtxI));
return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity() &&
(FMF.noSignedZeros() || Known.signBitIsZeroOrNaN());
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index e7ab17f82d7be..456d39ecc30cb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -150,8 +150,8 @@ bool InstCombinerImpl::SimplifyDemandedInstructionBits(Instruction &Inst) {
bool InstCombinerImpl::SimplifyDemandedInstructionFPClass(Instruction &Inst) {
KnownFPClass Known;
- Value *V =
- SimplifyDemandedUseFPClass(&Inst, fcAllFlags, Known, /*CtxI=*/&Inst);
+ Value *V = SimplifyDemandedUseFPClass(&Inst, fcAllFlags, Known,
+ SQ.getWithInstruction(&Inst));
if (!V)
return false;
if (V == &Inst)
@@ -2318,7 +2318,7 @@ simplifyDemandedUseFPClassFPTrunc(InstCombinerImpl &IC, Instruction &I,
Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
FPClassTest DemandedMask,
KnownFPClass &Known,
- Instruction *CxtI,
+ const SimplifyQuery &SQ,
unsigned Depth) {
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
assert(Known == KnownFPClass() && "expected uninitialized state");
@@ -2384,7 +2384,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
// fadd x, x can be handled more aggressively.
if (I->getOperand(0) == I->getOperand(1) &&
I->getOpcode() == Instruction::FAdd &&
- isGuaranteedNotToBeUndef(I->getOperand(0), SQ.AC, CxtI, SQ.DT,
+ isGuaranteedNotToBeUndef(I->getOperand(0), SQ.AC, SQ.CxtI, SQ.DT,
Depth + 1)) {
Type *EltTy = VTy->getScalarType();
DenormalMode Mode = F.getDenormalMode(EltTy->getFltSemantics());
@@ -2506,7 +2506,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
if (DemandedMask & fcZero)
SrcDemandedMask |= fcNormal | fcSubnormal;
- if (X == Y && isGuaranteedNotToBeUndef(X, SQ.AC, CxtI, SQ.DT, Depth + 1)) {
+ if (X == Y &&
+ isGuaranteedNotToBeUndef(X, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1)) {
if (SimplifyDemandedFPClass(I, 0, SrcDemandedMask, KnownLHS, Depth + 1))
return I;
Type *EltTy = VTy->getScalarType();
@@ -2648,7 +2649,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
case Instruction::FDiv: {
Value *X = I->getOperand(0);
Value *Y = I->getOperand(1);
- if (X == Y && isGuaranteedNotToBeUndef(X, SQ.AC, CxtI, SQ.DT, Depth + 1)) {
+ if (X == Y &&
+ isGuaranteedNotToBeUndef(X, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1)) {
// If the source is 0, inf or nan, the result is a nan
IRBuilderBase::InsertPointGuard Guard(Builder);
Builder.SetInsertPoint(I);
@@ -2851,8 +2853,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
FMF.noSignedZeros()))
return Simplified;
- KnownFPClass KnownSign = computeKnownFPClass(CI->getArgOperand(1),
- fcAllFlags, CxtI, Depth + 1);
+ KnownFPClass KnownSign =
+ computeKnownFPClass(CI->getArgOperand(1), fcAllFlags, SQ, Depth + 1);
if (KnownMag.SignBit && KnownSign.SignBit &&
*KnownMag.SignBit == *KnownSign.SignBit)
return CI->getOperand(0);
@@ -2890,7 +2892,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
Type *EltTy = VTy->getScalarType();
if (CI->getArgOperand(0) == CI->getArgOperand(1) &&
- isGuaranteedNotToBeUndef(CI->getArgOperand(0), SQ.AC, CxtI, SQ.DT,
+ isGuaranteedNotToBeUndef(CI->getArgOperand(0), SQ.AC, SQ.CxtI, SQ.DT,
Depth + 1)) {
if (SimplifyDemandedFPClass(CI, 0, SrcDemandedMask, KnownSrc[0],
Depth + 1) ||
@@ -3355,7 +3357,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
[[fallthrough]];
}
default:
- Known = computeKnownFPClass(I, DemandedMask, CxtI, Depth + 1);
+ Known = computeKnownFPClass(I, DemandedMask, SQ, Depth + 1);
Known.knownNot(~DemandedMask);
break;
}
@@ -3459,7 +3461,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
[[fallthrough]];
}
default:
- Known = computeKnownFPClass(I, DemandedMask, CxtI, Depth + 1);
+ Known = computeKnownFPClass(I, DemandedMask, SQ, Depth + 1);
Known.knownNot(~DemandedMask);
break;
}
@@ -3472,7 +3474,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
/// done based on DemandedMask, but without modifying the Instruction.
Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
Instruction *I, FPClassTest DemandedMask, KnownFPClass &Known,
- Instruction *CxtI, unsigned Depth) {
+ const SimplifyQuery &SQ, unsigned Depth) {
FastMathFlags FMF;
if (auto *FPOp = dyn_cast<FPMathOperator>(I)) {
FMF = FPOp->getFastMathFlags();
@@ -3484,12 +3486,12 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
// TODO: Can we infer which side it came from based on adjusted result
// class?
KnownFPClass KnownRHS =
- computeKnownFPClass(I->getOperand(2), DemandedMask, CxtI, Depth + 1);
+ computeKnownFPClass(I->getOperand(2), DemandedMask, SQ, Depth + 1);
if (KnownRHS.isKnownNever(DemandedMask))
return I->getOperand(1);
KnownFPClass KnownLHS =
- computeKnownFPClass(I->getOperand(1), DemandedMask, CxtI, Depth + 1);
+ computeKnownFPClass(I->getOperand(1), DemandedMask, SQ, Depth + 1);
if (KnownLHS.isKnownNever(DemandedMask))
return I->getOperand(2);
@@ -3507,12 +3509,11 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
Value *FNegSrc = I->getOperand(0);
if (!match(FNegSrc, m_FAbs(m_Value(Src)))) {
- Known = computeKnownFPClass(I, DemandedMask, CxtI, Depth + 1);
+ Known = computeKnownFPClass(I, DemandedMask, SQ, Depth + 1);
break;
}
- KnownFPClass KnownSrc =
- computeKnownFPClass(Src, fcAllFlags, CxtI, Depth + 1);
+ KnownFPClass KnownSrc = computeKnownFPClass(Src, fcAllFlags, SQ, Depth + 1);
FastMathFlags FabsFMF = cast<FPMathOperator>(FNegSrc)->getFastMathFlags();
FPClassTest ThisDemandedMask =
@@ -3532,7 +3533,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
case Intrinsic::fabs: {
Value *Src = CI->getArgOperand(0);
KnownFPClass KnownSrc =
- computeKnownFPClass(Src, fcAllFlags, CxtI, Depth + 1);
+ computeKnownFPClass(Src, fcAllFlags, SQ, Depth + 1);
// NSZ cannot be applied in multiple use case (maybe it could if all uses
// were known nsz)
@@ -3546,7 +3547,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
Value *Mag = CI->getArgOperand(0);
Value *Sign = CI->getArgOperand(1);
KnownFPClass KnownMag =
- computeKnownFPClass(Mag, fcAllFlags, CxtI, Depth + 1);
+ computeKnownFPClass(Mag, fcAllFlags, SQ, Depth + 1);
// Rule out some cases by magnitude, which may help prove the sign bit is
// one direction or the other.
@@ -3558,7 +3559,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
return Simplified;
KnownFPClass KnownSign =
- computeKnownFPClass(Sign, fcAllFlags, CxtI, Depth + 1);
+ computeKnownFPClass(Sign, fcAllFlags, SQ, Depth + 1);
if (FMF.noInfs())
KnownSign.knownNot(fcInf);
@@ -3578,13 +3579,13 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
case Intrinsic::minimum:
case Intrinsic::maximumnum:
case Intrinsic::minimumnum: {
- KnownFPClass KnownRHS = computeKnownFPClass(
- CI->getArgOperand(1), DemandedMask, CxtI, Depth + 1);
+ KnownFPClass KnownRHS = computeKnownFPClass(CI->getArgOperand(1),
+ DemandedMask, SQ, Depth + 1);
if (KnownRHS.isUnknown())
return nullptr;
- KnownFPClass KnownLHS = computeKnownFPClass(
- CI->getArgOperand(0), DemandedMask, CxtI, Depth + 1);
+ KnownFPClass KnownLHS = computeKnownFPClass(CI->getArgOperand(0),
+ DemandedMask, SQ, Depth + 1);
// Cannot use NSZ in the multiple use case.
return simplifyDemandedFPClassMinMax(Known, IID, CI, DemandedMask,
@@ -3598,7 +3599,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedFPClass(
[[fallthrough]];
}
default:
- Known = computeKnownFPClass(I, DemandedMask, CxtI, Depth + 1);
+ Known = computeKnownFPClass(I, DemandedMask, SQ, Depth + 1);
Known.knownNot(~DemandedMask);
break;
}
@@ -3625,7 +3626,7 @@ bool InstCombinerImpl::SimplifyDemandedFPClass(Instruction *I, unsigned OpNo,
Instruction *VInst = dyn_cast<Instruction>(V);
if (!VInst) {
// Handle constants and arguments
- Known = computeKnownFPClass(V, fcAllFlags, I, Depth);
+ Known = computeKnownFPClass(V, fcAllFlags, SQ.getWithInstruction(I), Depth);
Known.knownNot(~DemandedMask);
if (Known.KnownFPClasses == fcNone) {
@@ -3658,12 +3659,13 @@ bool InstCombinerImpl::SimplifyDemandedFPClass(Instruction *I, unsigned OpNo,
if (VInst->hasOneUse()) {
// If the instruction has one use, we can directly simplify it.
- NewVal = SimplifyDemandedUseFPClass(VInst, DemandedMask, Known, I, Depth);
+ NewVal = SimplifyDemandedUseFPClass(VInst, DemandedMask, Known,
+ SQ.getWithInstruction(I), Depth);
} else {
// If there are multiple uses of this instruction, then we can simplify
// VInst to some other value, but not modify the instruction.
- NewVal = SimplifyMultipleUseDemandedFPClass(VInst, DemandedMask, Known, I,
- Depth);
+ NewVal = SimplifyMultipleUseDemandedFPClass(
+ VInst, DemandedMask, Known, SQ.getWithInstruction(I), Depth);
}
if (!NewVal)
More information about the llvm-commits
mailing list