Skip to content
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

[InstCombine] Fold fcmp pred (x - y), 0 into fcmp pred x, y #85506

Merged
merged 19 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7906,6 +7906,53 @@ static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) {
return new FCmpInst(Pred, Op0, Zero, "", &I);
}

static Instruction *foldFCmpFSubIntoFCmp(FCmpInst &I, Instruction *LHSI,
Constant *RHSC, InstCombinerImpl &CI) {
const CmpInst::Predicate Pred = I.getPredicate();
Value *X = LHSI->getOperand(0);
Value *Y = LHSI->getOperand(1);
switch (Pred) {
default:
break;
case FCmpInst::FCMP_UGT:
case FCmpInst::FCMP_ULT:
case FCmpInst::FCMP_UNE:
case FCmpInst::FCMP_OEQ:
case FCmpInst::FCMP_OGE:
case FCmpInst::FCMP_OLE:
// The optimization is not valid if X and Y are infinities of the same
// sign, i.e. the inf - inf = nan case. If the fsub has the ninf or nnan
// flag then we can assume we do not have that case. Otherwise we might be
// able to prove that either X or Y is not infinity.
if (!LHSI->hasNoNaNs() && !LHSI->hasNoInfs() &&
!isKnownNeverInfinity(Y, /*Depth=*/0,
CI.getSimplifyQuery().getWithInstruction(&I)) &&
!isKnownNeverInfinity(X, /*Depth=*/0,
CI.getSimplifyQuery().getWithInstruction(&I)))
break;

[[fallthrough]];
case FCmpInst::FCMP_OGT:
case FCmpInst::FCMP_OLT:
case FCmpInst::FCMP_ONE:
case FCmpInst::FCMP_UEQ:
case FCmpInst::FCMP_UGE:
case FCmpInst::FCMP_ULE:
// fcmp pred (x - y), 0 --> fcmp pred x, y
if (match(RHSC, m_AnyZeroFP()) &&
I.getFunction()->getDenormalMode(
LHSI->getType()->getScalarType()->getFltSemantics()) ==
DenormalMode::getIEEE()) {
CI.replaceOperand(I, 0, X);
CI.replaceOperand(I, 1, Y);
return &I;
}
break;
}

return nullptr;
}

Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
bool Changed = false;

Expand Down Expand Up @@ -8076,6 +8123,11 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
return NV;
break;
case Instruction::FSub:
if (LHSI->hasOneUse())
if (Instruction *NV = foldFCmpFSubIntoFCmp(I, LHSI, RHSC, *this))
return NV;
break;
case Instruction::PHI:
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
return NV;
Expand Down
Loading
Loading