Skip to content

Commit e02d070

Browse files
committed
[InstCombine] Address review comemnts. NFC.
1 parent 2c7395e commit e02d070

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,13 @@ bool isKnownNeverNaN(const Value *V, unsigned Depth, const SimplifyQuery &SQ);
330330
std::optional<bool> computeKnownFPSignBit(const Value *V, unsigned Depth,
331331
const SimplifyQuery &SQ);
332332

333-
/// Return true if the sign bit of result can be ignored when the result is
334-
/// zero.
335-
bool ignoreSignBitOfZero(Instruction &I);
333+
/// Return true if the sign bit of result can be ignored by the user when the
334+
/// result is zero.
335+
bool ignoreSignBitOfZero(const Use &U);
336336

337-
/// Return true if the sign bit of result can be ignored when the result is NaN.
338-
bool ignoreSignBitOfNaN(Instruction &I);
337+
/// Return true if the sign bit of result can be ignored by the user when the
338+
/// result is NaN.
339+
bool ignoreSignBitOfNaN(const Use &U);
339340

340341
/// If the specified value can be set by repeating the same byte in memory,
341342
/// return the i8 value that it is represented with. This is true for all i8

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6357,17 +6357,10 @@ std::optional<bool> llvm::computeKnownFPSignBit(const Value *V, unsigned Depth,
63576357
return Known.SignBit;
63586358
}
63596359

6360-
/// Return true if the sign bit of result can be ignored when the result is
6361-
/// zero.
6362-
bool llvm::ignoreSignBitOfZero(Instruction &I) {
6363-
if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6364-
if (FPOp->hasNoSignedZeros())
6365-
return true;
6366-
6367-
// Check if the sign bit is ignored by the only user.
6368-
if (!I.hasOneUse())
6369-
return false;
6370-
Instruction *User = I.user_back();
6360+
/// Return true if the sign bit of result can be ignored by the user when the
6361+
/// result is zero.
6362+
bool llvm::ignoreSignBitOfZero(const Use &U) {
6363+
auto *User = cast<Instruction>(U.getUser());
63716364
if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
63726365
if (FPOp->hasNoSignedZeros())
63736366
return true;
@@ -6386,7 +6379,7 @@ bool llvm::ignoreSignBitOfZero(Instruction &I) {
63866379
case Intrinsic::fabs:
63876380
return true;
63886381
case Intrinsic::copysign:
6389-
return II->getArgOperand(0) == &I;
6382+
return U.getOperandNo() == 0;
63906383
case Intrinsic::is_fpclass:
63916384
case Intrinsic::vp_is_fpclass: {
63926385
auto Test =
@@ -6405,15 +6398,10 @@ bool llvm::ignoreSignBitOfZero(Instruction &I) {
64056398
}
64066399
}
64076400

6408-
bool llvm::ignoreSignBitOfNaN(Instruction &I) {
6409-
if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6410-
if (FPOp->hasNoNaNs())
6411-
return true;
6412-
6413-
// Check if the sign bit is ignored by the only user.
6414-
if (!I.hasOneUse())
6415-
return false;
6416-
Instruction *User = I.user_back();
6401+
/// Return true if the sign bit of result can be ignored by the user when the
6402+
/// result is NaN.
6403+
bool llvm::ignoreSignBitOfNaN(const Use &U) {
6404+
auto *User = cast<Instruction>(U.getUser());
64176405
if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
64186406
if (FPOp->hasNoNaNs())
64196407
return true;
@@ -6439,7 +6427,7 @@ bool llvm::ignoreSignBitOfNaN(Instruction &I) {
64396427
case Instruction::PHI:
64406428
return false;
64416429
case Instruction::Ret:
6442-
return I.getFunction()->getAttributes().getRetNoFPClass() &
6430+
return User->getFunction()->getAttributes().getRetNoFPClass() &
64436431
FPClassTest::fcNan;
64446432
case Instruction::Call:
64456433
case Instruction::Invoke: {
@@ -6448,7 +6436,7 @@ bool llvm::ignoreSignBitOfNaN(Instruction &I) {
64486436
case Intrinsic::fabs:
64496437
return true;
64506438
case Intrinsic::copysign:
6451-
return II->getArgOperand(0) == &I;
6439+
return U.getOperandNo() == 0;
64526440
// Other proper FP math intrinsics ignore the sign bit of NaN.
64536441
case Intrinsic::maxnum:
64546442
case Intrinsic::minnum:
@@ -6472,8 +6460,8 @@ bool llvm::ignoreSignBitOfNaN(Instruction &I) {
64726460
}
64736461
}
64746462

6475-
FPClassTest NoFPClass = cast<CallBase>(User)->getParamNoFPClass(
6476-
I.uses().begin()->getOperandNo());
6463+
FPClassTest NoFPClass =
6464+
cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
64776465
return NoFPClass & FPClassTest::fcNan;
64786466
}
64796467
default:

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,8 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
27972797
// of NAN, but IEEE-754 specifies the signbit of NAN values with
27982798
// fneg/fabs operations.
27992799
if (match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X))) &&
2800-
(cast<FPMathOperator>(CondVal)->hasNoNaNs() || ignoreSignBitOfNaN(SI) ||
2800+
(cast<FPMathOperator>(CondVal)->hasNoNaNs() || SI.hasNoNaNs() ||
2801+
(SI.hasOneUse() && ignoreSignBitOfNaN(*SI.use_begin())) ||
28012802
isKnownNeverNaN(X, /*Depth=*/0,
28022803
IC.getSimplifyQuery().getWithInstruction(
28032804
cast<Instruction>(CondVal))))) {
@@ -2844,7 +2845,11 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
28442845
// Note: We require "nnan" for this fold because fcmp ignores the signbit
28452846
// of NAN, but IEEE-754 specifies the signbit of NAN values with
28462847
// fneg/fabs operations.
2847-
if (!ignoreSignBitOfZero(SI) || !ignoreSignBitOfNaN(SI))
2848+
if (!SI.hasNoSignedZeros() &&
2849+
!(SI.hasOneUse() && ignoreSignBitOfZero(*SI.use_begin())))
2850+
return nullptr;
2851+
if (!SI.hasNoNaNs() &&
2852+
!(SI.hasOneUse() && ignoreSignBitOfNaN(*SI.use_begin())))
28482853
return nullptr;
28492854

28502855
if (Swap)

0 commit comments

Comments
 (0)