Skip to content

[DAG] Lower frem of power-2 using div/trunc/mul+sub #91148

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 5 commits into from
May 10, 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
8 changes: 8 additions & 0 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,10 @@ class SelectionDAG {
/// is set.
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth = 0) const;

/// Test if the given _fp_ value is known to be an integer power-of-2, either
/// positive or negative.
bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth = 0) const;

/// Return the number of times the sign bit of the register is replicated into
/// the other bits. We know that at least 1 bit is always equal to the sign
/// bit (itself), but other cases can give us information. For example,
Expand Down Expand Up @@ -2111,6 +2115,10 @@ class SelectionDAG {
/// Test whether the given SDValue is known to contain non-zero value(s).
bool isKnownNeverZero(SDValue Op, unsigned Depth = 0) const;

/// Test whether the given float value is known to be positive. +0.0, +inf and
/// +nan are considered positive, -0.0, -inf and -nan are not.
bool cannotBeOrderedNegativeFP(SDValue Op) const;

/// Test whether two SDValues are known to compare equal. This
/// is true if they are the same value, or if one is negative zero and the
/// other positive zero.
Expand Down
20 changes: 19 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17365,17 +17365,35 @@ SDValue DAGCombiner::visitFREM(SDNode *N) {
EVT VT = N->getValueType(0);
SDNodeFlags Flags = N->getFlags();
SelectionDAG::FlagInserter FlagsInserter(DAG, N);
SDLoc DL(N);

if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags))
return R;

// fold (frem c1, c2) -> fmod(c1,c2)
if (SDValue C = DAG.FoldConstantArithmetic(ISD::FREM, SDLoc(N), VT, {N0, N1}))
if (SDValue C = DAG.FoldConstantArithmetic(ISD::FREM, DL, VT, {N0, N1}))
return C;

if (SDValue NewSel = foldBinOpIntoSelect(N))
return NewSel;

// Lower frem N0, N1 => x - trunc(N0 / N1) * N1, providing N1 is an integer
// power of 2.
if (!TLI.isOperationLegal(ISD::FREM, VT) &&
TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
TLI.isOperationLegalOrCustom(ISD::FDIV, VT) &&
TLI.isOperationLegalOrCustom(ISD::FTRUNC, VT) &&
DAG.isKnownToBeAPowerOfTwoFP(N1) &&
(Flags.hasNoSignedZeros() || DAG.cannotBeOrderedNegativeFP(N0))) {
SDValue Div = DAG.getNode(ISD::FDIV, DL, VT, N0, N1);
SDValue Rnd = DAG.getNode(ISD::FTRUNC, DL, VT, Div);
if (TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT))
return DAG.getNode(ISD::FMA, DL, VT, DAG.getNode(ISD::FNEG, DL, VT, Rnd),
N1, N0);
SDValue Mul = DAG.getNode(ISD::FMUL, DL, VT, Rnd, N1);
return DAG.getNode(ISD::FSUB, DL, VT, N0, Mul);
}

return SDValue();
}

Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4373,6 +4373,16 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth) const {
return false;
}

bool SelectionDAG::isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth) const {
if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
return C1->getValueAPF().getExactLog2Abs() >= 0;

if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);

return false;
}

unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
EVT VT = Op.getValueType();

Expand Down Expand Up @@ -5555,6 +5565,13 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
return computeKnownBits(Op, Depth).isNonZero();
}

bool SelectionDAG::cannotBeOrderedNegativeFP(SDValue Op) const {
if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
return !C1->isNegative();

return Op.getOpcode() == ISD::FABS;
}

bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
// Check the obvious case.
if (A == B) return true;
Expand Down
Loading
Loading