Skip to content

Commit

Permalink
[IR] Replace calls to ConstantFP::getNullValue with ConstantFP::getZe…
Browse files Browse the repository at this point in the history
…ro. NFC

There is no getNullValue in ConstantFP. Due to inheritance, we're calling
Constant::getNullValue which handles any type including FP.
Since we already know we want an FP constant we can use ConstantFP::getZero
which might be faster and is a more readable name for an FP zero.
  • Loading branch information
topperc committed Apr 4, 2023
1 parent 56876fd commit 1f60c8d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,7 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
// The legacy behaviour is that multiplying +/- 0.0 by anything, even
// NaN or infinity, gives +0.0.
if (Op1V.isZero() || Op2V.isZero())
return ConstantFP::getNullValue(Ty);
return ConstantFP::getZero(Ty);
return ConstantFP::get(Ty->getContext(), Op1V * Op2V);
}

Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5508,11 +5508,11 @@ simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
// X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
return ConstantFP::getNullValue(Op0->getType());
return ConstantFP::getZero(Op0->getType());

if (match(Op0, m_FNeg(m_Specific(Op1))) ||
match(Op1, m_FNeg(m_Specific(Op0))))
return ConstantFP::getNullValue(Op0->getType());
return ConstantFP::getZero(Op0->getType());
}

// (X - Y) + Y --> X
Expand Down Expand Up @@ -5616,7 +5616,7 @@ static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
if (match(Op1, m_AnyZeroFP())) {
// X * 0.0 --> 0.0 (with nnan and nsz)
if (FMF.noNaNs() && FMF.noSignedZeros())
return ConstantFP::getNullValue(Op0->getType());
return ConstantFP::getZero(Op0->getType());

// +normal number * (-)0.0 --> (-)0.0
if (isKnownNeverInfinity(Op0, Q.TLI) && isKnownNeverNaN(Op0, Q.TLI) &&
Expand Down Expand Up @@ -5705,7 +5705,7 @@ simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
// Requires that NaNs are off (X could be zero) and signed zeroes are
// ignored (X could be positive or negative, so the output sign is unknown).
if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
return ConstantFP::getNullValue(Op0->getType());
return ConstantFP::getZero(Op0->getType());

if (FMF.noNaNs()) {
// X / X -> 1.0 is legal when NaNs are ignored.
Expand Down Expand Up @@ -5762,7 +5762,7 @@ simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (FMF.noNaNs()) {
// +0 % X -> 0
if (match(Op0, m_PosZeroFP()))
return ConstantFP::getNullValue(Op0->getType());
return ConstantFP::getZero(Op0->getType());
// -0 % X -> -0
if (match(Op0, m_NegZeroFP()))
return ConstantFP::getNegativeZero(Op0->getType());
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
// TODO: Move to InstSimplify?
if (match(Op0, PatternMatch::m_AnyZeroFP()) ||
match(Op1, PatternMatch::m_AnyZeroFP()))
return IC.replaceInstUsesWith(II, ConstantFP::getNullValue(II.getType()));
return IC.replaceInstUsesWith(II, ConstantFP::getZero(II.getType()));

// If we can prove we don't have one of the special cases then we can use a
// normal fmul instruction instead.
Expand All @@ -1024,7 +1024,7 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
match(Op1, PatternMatch::m_AnyZeroFP())) {
// It's tempting to just return Op2 here, but that would give the wrong
// result if Op2 was -0.0.
auto *Zero = ConstantFP::getNullValue(II.getType());
auto *Zero = ConstantFP::getZero(II.getType());
auto *FAdd = IC.Builder.CreateFAddFMF(Zero, Op2, &II);
FAdd->takeName(&II);
return IC.replaceInstUsesWith(II, FAdd);
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6901,7 +6901,7 @@ static Instruction *foldFabsWithFcmpZero(FCmpInst &I, InstCombinerImpl &IC) {
Mode.Input == DenormalMode::PositiveZero) {

auto replaceFCmp = [](FCmpInst *I, FCmpInst::Predicate P, Value *X) {
Constant *Zero = ConstantFP::getNullValue(X->getType());
Constant *Zero = ConstantFP::getZero(X->getType());
return new FCmpInst(P, X, Zero, "", I);
};

Expand Down Expand Up @@ -6997,7 +6997,7 @@ static Instruction *foldFCmpFNegCommonOp(FCmpInst &I) {

// Replace the negated operand with 0.0:
// fcmp Pred Op0, -Op0 --> fcmp Pred Op0, 0.0
Constant *Zero = ConstantFP::getNullValue(Op0->getType());
Constant *Zero = ConstantFP::getZero(Op0->getType());
return new FCmpInst(Pred, Op0, Zero, "", &I);
}

Expand Down Expand Up @@ -7048,10 +7048,10 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
// then canonicalize the operand to 0.0.
if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) {
if (!match(Op0, m_PosZeroFP()) && isKnownNeverNaN(Op0, &TLI))
return replaceOperand(I, 0, ConstantFP::getNullValue(OpType));
return replaceOperand(I, 0, ConstantFP::getZero(OpType));

if (!match(Op1, m_PosZeroFP()) && isKnownNeverNaN(Op1, &TLI))
return replaceOperand(I, 1, ConstantFP::getNullValue(OpType));
return replaceOperand(I, 1, ConstantFP::getZero(OpType));
}

// fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y
Expand Down Expand Up @@ -7080,7 +7080,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
// The sign of 0.0 is ignored by fcmp, so canonicalize to +0.0:
// fcmp Pred X, -0.0 --> fcmp Pred X, 0.0
if (match(Op1, m_AnyZeroFP()) && !match(Op1, m_PosZeroFP()))
return replaceOperand(I, 1, ConstantFP::getNullValue(OpType));
return replaceOperand(I, 1, ConstantFP::getZero(OpType));

// Ignore signbit of bitcasted int when comparing equality to FP 0.0:
// fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
Expand Down Expand Up @@ -7169,11 +7169,11 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
case FCmpInst::FCMP_ONE:
// X is ordered and not equal to an impossible constant --> ordered
return new FCmpInst(FCmpInst::FCMP_ORD, X,
ConstantFP::getNullValue(X->getType()));
ConstantFP::getZero(X->getType()));
case FCmpInst::FCMP_UEQ:
// X is unordered or equal to an impossible constant --> unordered
return new FCmpInst(FCmpInst::FCMP_UNO, X,
ConstantFP::getNullValue(X->getType()));
ConstantFP::getZero(X->getType()));
case FCmpInst::FCMP_UNE:
// X is unordered or not equal to an impossible constant --> true
return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
Expand Down
6 changes: 3 additions & 3 deletions llvm/tools/llvm-stress/llvm-stress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ struct Modifier {
} else if (Tp->isFloatingPointTy()) {
if (getRandom() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
return ConstantFP::getZero(Tp);
}
return UndefValue::get(Tp);
}
Expand All @@ -244,7 +244,7 @@ struct Modifier {
} else if (Tp->isFloatingPointTy()) {
if (getRandom() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
return ConstantFP::getZero(Tp);
} else if (auto *VTp = dyn_cast<FixedVectorType>(Tp)) {
std::vector<Constant*> TempValues;
TempValues.reserve(VTp->getNumElements());
Expand Down Expand Up @@ -442,7 +442,7 @@ struct ConstModifier: public Modifier {
APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);

if (getRandom() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
return PT->push_back(ConstantFP::getZero(Ty));
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
}

Expand Down

0 comments on commit 1f60c8d

Please sign in to comment.