-
Notifications
You must be signed in to change notification settings - Fork 14k
[ValueTracking][InstCombine] Generalize ignoreSignBitOfZero/NaN to handle more cases #141015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db7e339
[InstCombine] Add pre-commit tests. NFC.
dtcxzyw 4ac3c53
[InstCombine] Generalize ignoreSignBitOfZero/NaN to handle more cases
dtcxzyw b314ab2
[InstCombine] Simplify code. NFC.
dtcxzyw 473b9ed
[InstCombine] Add support for ret nofpclass
dtcxzyw a8e88b1
[InstCombine] Remove unused header
dtcxzyw f0cea1f
[InstCombine] Move helper functions into ValueTracking
dtcxzyw 2c7395e
[InstCombine] Add more tests. NFC.
dtcxzyw e02d070
[InstCombine] Address review comemnts. NFC.
dtcxzyw bf96e74
Apply de-morgan
dtcxzyw 205c586
[ValueTracking] Address review comments. NFC.
dtcxzyw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6357,6 +6357,130 @@ std::optional<bool> llvm::computeKnownFPSignBit(const Value *V, unsigned Depth, | |
return Known.SignBit; | ||
} | ||
|
||
/// Return true if the sign bit of result can be ignored when the result is | ||
/// zero. | ||
bool llvm::ignoreSignBitOfZero(Instruction &I) { | ||
if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) | ||
if (FPOp->hasNoSignedZeros()) | ||
return true; | ||
|
||
// Check if the sign bit is ignored by the only user. | ||
if (!I.hasOneUse()) | ||
return false; | ||
Instruction *User = I.user_back(); | ||
if (auto *FPOp = dyn_cast<FPMathOperator>(User)) { | ||
if (FPOp->hasNoSignedZeros()) | ||
return true; | ||
} | ||
|
||
switch (User->getOpcode()) { | ||
case Instruction::FPToSI: | ||
case Instruction::FPToUI: | ||
return true; | ||
case Instruction::FCmp: | ||
// fcmp treats both positive and negative zero as equal. | ||
return true; | ||
case Instruction::Call: | ||
if (auto *II = dyn_cast<IntrinsicInst>(User)) { | ||
switch (II->getIntrinsicID()) { | ||
case Intrinsic::fabs: | ||
return true; | ||
case Intrinsic::copysign: | ||
return II->getArgOperand(0) == &I; | ||
case Intrinsic::is_fpclass: | ||
case Intrinsic::vp_is_fpclass: { | ||
auto Test = | ||
static_cast<FPClassTest>( | ||
cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) & | ||
FPClassTest::fcZero; | ||
return Test == FPClassTest::fcZero || Test == FPClassTest::fcNone; | ||
} | ||
default: | ||
return false; | ||
} | ||
} | ||
return false; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
bool llvm::ignoreSignBitOfNaN(Instruction &I) { | ||
if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) | ||
if (FPOp->hasNoNaNs()) | ||
return true; | ||
|
||
// Check if the sign bit is ignored by the only user. | ||
if (!I.hasOneUse()) | ||
return false; | ||
Instruction *User = I.user_back(); | ||
if (auto *FPOp = dyn_cast<FPMathOperator>(User)) { | ||
if (FPOp->hasNoNaNs()) | ||
return true; | ||
} | ||
|
||
switch (User->getOpcode()) { | ||
case Instruction::FPToSI: | ||
case Instruction::FPToUI: | ||
return true; | ||
// Proper FP math operations ignore the sign bit of NaN. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference: https://llvm.org/docs/LangRef.html#behavior-of-floating-point-nan-values
|
||
case Instruction::FAdd: | ||
case Instruction::FSub: | ||
case Instruction::FMul: | ||
case Instruction::FDiv: | ||
case Instruction::FRem: | ||
case Instruction::FPTrunc: | ||
case Instruction::FPExt: | ||
case Instruction::FCmp: | ||
return true; | ||
// Bitwise FP operations should preserve the sign bit of NaN. | ||
case Instruction::FNeg: | ||
case Instruction::Select: | ||
case Instruction::PHI: | ||
return false; | ||
case Instruction::Ret: | ||
return I.getFunction()->getAttributes().getRetNoFPClass() & | ||
FPClassTest::fcNan; | ||
case Instruction::Call: | ||
case Instruction::Invoke: { | ||
if (auto *II = dyn_cast<IntrinsicInst>(User)) { | ||
switch (II->getIntrinsicID()) { | ||
case Intrinsic::fabs: | ||
return true; | ||
case Intrinsic::copysign: | ||
return II->getArgOperand(0) == &I; | ||
// Other proper FP math intrinsics ignore the sign bit of NaN. | ||
case Intrinsic::maxnum: | ||
case Intrinsic::minnum: | ||
case Intrinsic::maximum: | ||
case Intrinsic::minimum: | ||
case Intrinsic::maximumnum: | ||
case Intrinsic::minimumnum: | ||
case Intrinsic::canonicalize: | ||
case Intrinsic::fma: | ||
case Intrinsic::fmuladd: | ||
case Intrinsic::sqrt: | ||
case Intrinsic::pow: | ||
case Intrinsic::powi: | ||
case Intrinsic::fptoui_sat: | ||
case Intrinsic::fptosi_sat: | ||
case Intrinsic::is_fpclass: | ||
case Intrinsic::vp_is_fpclass: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
FPClassTest NoFPClass = cast<CallBase>(User)->getParamNoFPClass( | ||
I.uses().begin()->getOperandNo()); | ||
return NoFPClass & FPClassTest::fcNan; | ||
} | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { | ||
|
||
// All byte-wide stores are splatable, even of arbitrary variables. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.