[llvm] 6c2ad4c - [SDAG] Extract helper to determine neutral element (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 29 14:05:19 PDT 2020
Author: Nikita Popov
Date: 2020-10-29T22:05:06+01:00
New Revision: 6c2ad4cf8758335e0896ba0540b21bbf6d239fbd
URL: https://github.com/llvm/llvm-project/commit/6c2ad4cf8758335e0896ba0540b21bbf6d239fbd
DIFF: https://github.com/llvm/llvm-project/commit/6c2ad4cf8758335e0896ba0540b21bbf6d239fbd.diff
LOG: [SDAG] Extract helper to determine neutral element (NFC)
Make the existing VECREDUCE based code more generic, but expressing
it in terms of the neutral value of the base opcode instead.
Added:
Modified:
llvm/include/llvm/CodeGen/SelectionDAG.h
llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h
index 4c0412315c19..efbb8bdb5f9b 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAG.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAG.h
@@ -2000,6 +2000,10 @@ class SelectionDAG {
bool shouldOptForSize() const;
+ /// Get the (commutative) neutral element for the given opcode, if it exists.
+ SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT,
+ SDNodeFlags Flags);
+
private:
void InsertNode(SDNode *N);
bool RemoveNodeFromCSEMaps(SDNode *N);
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 333c604690ab..b46ea1be7a30 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -4757,48 +4757,9 @@ SDValue DAGTypeLegalizer::WidenVecOp_VECREDUCE(SDNode *N) {
EVT ElemVT = OrigVT.getVectorElementType();
SDNodeFlags Flags = N->getFlags();
- SDValue NeutralElem;
- switch (N->getOpcode()) {
- case ISD::VECREDUCE_ADD:
- case ISD::VECREDUCE_OR:
- case ISD::VECREDUCE_XOR:
- case ISD::VECREDUCE_UMAX:
- NeutralElem = DAG.getConstant(0, dl, ElemVT);
- break;
- case ISD::VECREDUCE_MUL:
- NeutralElem = DAG.getConstant(1, dl, ElemVT);
- break;
- case ISD::VECREDUCE_AND:
- case ISD::VECREDUCE_UMIN:
- NeutralElem = DAG.getAllOnesConstant(dl, ElemVT);
- break;
- case ISD::VECREDUCE_SMAX:
- NeutralElem = DAG.getConstant(
- APInt::getSignedMinValue(ElemVT.getSizeInBits()), dl, ElemVT);
- break;
- case ISD::VECREDUCE_SMIN:
- NeutralElem = DAG.getConstant(
- APInt::getSignedMaxValue(ElemVT.getSizeInBits()), dl, ElemVT);
- break;
- case ISD::VECREDUCE_FADD:
- NeutralElem = DAG.getConstantFP(-0.0, dl, ElemVT);
- break;
- case ISD::VECREDUCE_FMUL:
- NeutralElem = DAG.getConstantFP(1.0, dl, ElemVT);
- break;
- case ISD::VECREDUCE_FMIN:
- case ISD::VECREDUCE_FMAX: {
- // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
- const fltSemantics &Semantics = DAG.EVTToAPFloatSemantics(ElemVT);
- APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
- !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
- APFloat::getLargest(Semantics);
- if (N->getOpcode() == ISD::VECREDUCE_FMAX)
- NeutralAF.changeSign();
-
- NeutralElem = DAG.getConstantFP(NeutralAF, dl, ElemVT);
- }
- }
+ SDValue NeutralElem = DAG.getNeutralElement(
+ ISD::getVecReduceBaseOpcode(N->getOpcode()), dl, ElemVT, Flags);
+ assert(NeutralElem && "Neutral element must exist");
// Pad the vector with the neutral element.
unsigned OrigElts = OrigVT.getVectorNumElements();
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 8939392708a2..709b0f44f0a5 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -10090,6 +10090,44 @@ SDValue SelectionDAG::getTokenFactor(const SDLoc &DL,
return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
}
+SDValue SelectionDAG::getNeutralElement(unsigned Opcode, const SDLoc &DL,
+ EVT VT, SDNodeFlags Flags) {
+ switch (Opcode) {
+ default:
+ return SDValue();
+ case ISD::ADD:
+ case ISD::OR:
+ case ISD::XOR:
+ case ISD::UMAX:
+ return getConstant(0, DL, VT);
+ case ISD::MUL:
+ return getConstant(1, DL, VT);
+ case ISD::AND:
+ case ISD::UMIN:
+ return getAllOnesConstant(DL, VT);
+ case ISD::SMAX:
+ return getConstant(APInt::getSignedMinValue(VT.getSizeInBits()), DL, VT);
+ case ISD::SMIN:
+ return getConstant(APInt::getSignedMaxValue(VT.getSizeInBits()), DL, VT);
+ case ISD::FADD:
+ return getConstantFP(-0.0, DL, VT);
+ case ISD::FMUL:
+ return getConstantFP(1.0, DL, VT);
+ case ISD::FMINNUM:
+ case ISD::FMAXNUM: {
+ // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
+ const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
+ APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
+ !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
+ APFloat::getLargest(Semantics);
+ if (Opcode == ISD::FMAXNUM)
+ NeutralAF.changeSign();
+
+ return getConstantFP(NeutralAF, DL, VT);
+ }
+ }
+}
+
#ifndef NDEBUG
static void checkForCyclesHelper(const SDNode *N,
SmallPtrSetImpl<const SDNode*> &Visited,
More information about the llvm-commits
mailing list