-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Make (binop ({s|u}itofp),({s|u}itofp))
transform more flexible to mismatched signs
#84389
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
Closed
goldsteinn
wants to merge
2
commits into
llvm:main
from
goldsteinn:goldsteinn/fp-int-binop-sign-agnostic
Closed
[InstCombine] Make (binop ({s|u}itofp),({s|u}itofp))
transform more flexible to mismatched signs
#84389
Changes from all commits
Commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -1406,41 +1406,27 @@ Value *InstCombinerImpl::dyn_castNegVal(Value *V) const { | |||||
// -> ({s|u}itofp (int_binop x, y)) | ||||||
// 2) (fp_binop ({s|u}itofp x), FpC) | ||||||
// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC))) | ||||||
Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) { | ||||||
Value *IntOps[2] = {nullptr, nullptr}; | ||||||
Constant *Op1FpC = nullptr; | ||||||
|
||||||
// Check for: | ||||||
// 1) (binop ({s|u}itofp x), ({s|u}itofp y)) | ||||||
// 2) (binop ({s|u}itofp x), FpC) | ||||||
if (!match(BO.getOperand(0), m_SIToFP(m_Value(IntOps[0]))) && | ||||||
!match(BO.getOperand(0), m_UIToFP(m_Value(IntOps[0])))) | ||||||
return nullptr; | ||||||
|
||||||
if (!match(BO.getOperand(1), m_Constant(Op1FpC)) && | ||||||
!match(BO.getOperand(1), m_SIToFP(m_Value(IntOps[1]))) && | ||||||
!match(BO.getOperand(1), m_UIToFP(m_Value(IntOps[1])))) | ||||||
return nullptr; | ||||||
// | ||||||
// Assuming the sign of the cast for x/y is `OpsFromSigned`. | ||||||
Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign( | ||||||
BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps, | ||||||
Constant *Op1FpC, SmallVectorImpl<WithCache<const Value *>> &OpsKnown) { | ||||||
|
||||||
Type *FPTy = BO.getType(); | ||||||
Type *IntTy = IntOps[0]->getType(); | ||||||
|
||||||
// Do we have signed casts? | ||||||
bool OpsFromSigned = isa<SIToFPInst>(BO.getOperand(0)); | ||||||
|
||||||
unsigned IntSz = IntTy->getScalarSizeInBits(); | ||||||
// This is the maximum number of inuse bits by the integer where the int -> fp | ||||||
// casts are exact. | ||||||
unsigned MaxRepresentableBits = | ||||||
APFloat::semanticsPrecision(FPTy->getScalarType()->getFltSemantics()); | ||||||
|
||||||
// Cache KnownBits a bit to potentially save some analysis. | ||||||
WithCache<const Value *> OpsKnown[2] = {IntOps[0], IntOps[1]}; | ||||||
|
||||||
// Preserve known number of leading bits. This can allow us to trivial nsw/nuw | ||||||
// checks later on. | ||||||
unsigned NumUsedLeadingBits[2] = {IntSz, IntSz}; | ||||||
|
||||||
// NB: This only comes up is OpsFromSigned is true, so there is no need to | ||||||
// cache is between calls to `foldFBinOpOfIntCastsFromSign`. | ||||||
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.
Suggested change
|
||||||
auto IsNonZero = [&](unsigned OpNo) -> bool { | ||||||
if (OpsKnown[OpNo].hasKnownBits() && | ||||||
OpsKnown[OpNo].getKnownBits(SQ).isNonZero()) | ||||||
|
@@ -1449,14 +1435,19 @@ Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) { | |||||
}; | ||||||
|
||||||
auto IsNonNeg = [&](unsigned OpNo) -> bool { | ||||||
if (OpsKnown[OpNo].hasKnownBits() && | ||||||
OpsKnown[OpNo].getKnownBits(SQ).isNonNegative()) | ||||||
return true; | ||||||
return isKnownNonNegative(IntOps[OpNo], SQ); | ||||||
// NB: This matches the impl in ValueTracking, we just try to use cached | ||||||
// knownbits here. If we ever start supporting WithCache for | ||||||
// `isKnownNonNegative`, change this to an explicitly call. | ||||||
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.
Suggested change
|
||||||
return OpsKnown[OpNo].getKnownBits(SQ).isNonNegative(); | ||||||
}; | ||||||
|
||||||
// Check if we know for certain that ({s|u}itofp op) is exact. | ||||||
auto IsValidPromotion = [&](unsigned OpNo) -> bool { | ||||||
// Can we treat this operand as the desired sign? | ||||||
if (OpsFromSigned != isa<SIToFPInst>(BO.getOperand(OpNo)) && | ||||||
!IsNonNeg(OpNo)) | ||||||
return false; | ||||||
|
||||||
// If fp precision >= bitwidth(op) then its exact. | ||||||
// NB: This is slightly conservative for `sitofp`. For signed conversion, we | ||||||
// can handle `MaxRepresentableBits == IntSz - 1` as the sign bit will be | ||||||
|
@@ -1509,13 +1500,6 @@ Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) { | |||||
return nullptr; | ||||||
|
||||||
if (Op1FpC == nullptr) { | ||||||
if (OpsFromSigned != isa<SIToFPInst>(BO.getOperand(1))) { | ||||||
// If we have a signed + unsigned, see if we can treat both as signed | ||||||
// (uitofp nneg x) == (sitofp nneg x). | ||||||
if (OpsFromSigned ? !IsNonNeg(1) : !IsNonNeg(0)) | ||||||
return nullptr; | ||||||
OpsFromSigned = true; | ||||||
} | ||||||
if (!IsValidPromotion(1)) | ||||||
return nullptr; | ||||||
} | ||||||
|
@@ -1574,6 +1558,39 @@ Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) { | |||||
return new UIToFPInst(IntBinOp, FPTy); | ||||||
} | ||||||
|
||||||
// Try to fold: | ||||||
// 1) (fp_binop ({s|u}itofp x), ({s|u}itofp y)) | ||||||
// -> ({s|u}itofp (int_binop x, y)) | ||||||
// 2) (fp_binop ({s|u}itofp x), FpC) | ||||||
// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC))) | ||||||
Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) { | ||||||
std::array<Value *, 2> IntOps = {nullptr, nullptr}; | ||||||
Constant *Op1FpC = nullptr; | ||||||
// Check for: | ||||||
// 1) (binop ({s|u}itofp x), ({s|u}itofp y)) | ||||||
// 2) (binop ({s|u}itofp x), FpC) | ||||||
if (!match(BO.getOperand(0), m_SIToFP(m_Value(IntOps[0]))) && | ||||||
!match(BO.getOperand(0), m_UIToFP(m_Value(IntOps[0])))) | ||||||
return nullptr; | ||||||
|
||||||
if (!match(BO.getOperand(1), m_Constant(Op1FpC)) && | ||||||
!match(BO.getOperand(1), m_SIToFP(m_Value(IntOps[1]))) && | ||||||
!match(BO.getOperand(1), m_UIToFP(m_Value(IntOps[1])))) | ||||||
return nullptr; | ||||||
|
||||||
// Cache KnownBits a bit to potentially save some analysis. | ||||||
SmallVector<WithCache<const Value *>, 2> OpsKnown = {IntOps[0], IntOps[1]}; | ||||||
|
||||||
// Try treating x/y as coming from both `uitofp` and `sitofp`. There are | ||||||
// different constraints depending on the sign of the cast. | ||||||
// NB: `(uitofp nneg X)` == `(sitofp nneg X)`. | ||||||
if (Instruction *R = foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/false, | ||||||
IntOps, Op1FpC, OpsKnown)) | ||||||
return R; | ||||||
return foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/true, IntOps, | ||||||
Op1FpC, OpsKnown); | ||||||
} | ||||||
|
||||||
/// A binop with a constant operand and a sign-extended boolean operand may be | ||||||
/// converted into a select of constants by applying the binary operation to | ||||||
/// the constant with the two possible values of the extended boolean (0 or -1). | ||||||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.