[llvm] r347210 - [SelectionDAG] add simplifySelect() to reduce code duplication; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 19 06:35:22 PST 2018


Author: spatel
Date: Mon Nov 19 06:35:22 2018
New Revision: 347210

URL: http://llvm.org/viewvc/llvm-project?rev=347210&view=rev
Log:
[SelectionDAG] add simplifySelect() to reduce code duplication; NFC

This should be extended to handle FP and vectors in follow-up patches.

Modified:
    llvm/trunk/include/llvm/CodeGen/SelectionDAG.h
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Modified: llvm/trunk/include/llvm/CodeGen/SelectionDAG.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SelectionDAG.h?rev=347210&r1=347209&r2=347210&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/SelectionDAG.h (original)
+++ llvm/trunk/include/llvm/CodeGen/SelectionDAG.h Mon Nov 19 06:35:22 2018
@@ -962,6 +962,9 @@ public:
                    False, getCondCode(Cond));
   }
 
+  /// Try to simplify a select/vselect into 1 of its operands or a constant.
+  SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal);
+
   /// VAArg produces a result and token chain, and takes a pointer
   /// and a source value as input.
   SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr,

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=347210&r1=347209&r2=347210&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Mon Nov 19 06:35:22 2018
@@ -7236,24 +7236,8 @@ SDValue DAGCombiner::visitSELECT(SDNode
   EVT VT0 = N0.getValueType();
   SDLoc DL(N);
 
-  // select undef, N1, N2 --> N1 (if it's a constant), otherwise N2
-  if (N0.isUndef())
-    return isa<ConstantSDNode>(N1) ? N1 : N2;
-  // select, ?, undef, N2 --> N2
-  if (N1.isUndef())
-    return N2;
-  // select, ?, N1, undef --> N1
-  if (N2.isUndef())
-    return N1;
-
-  // fold (select true, X, Y) -> X
-  // fold (select false, X, Y) -> Y
-  if (auto *N0C = dyn_cast<const ConstantSDNode>(N0))
-    return N0C->isNullValue() ? N2 : N1;
-
-  // select ?, N1, N1 --> N1
-  if (N1 == N2)
-    return N1;
+  if (SDValue V = DAG.simplifySelect(N0, N1, N2))
+    return V;
 
   // fold (select X, X, Y) -> (or X, Y)
   // fold (select X, 1, Y) -> (or C, Y)

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=347210&r1=347209&r2=347210&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Mon Nov 19 06:35:22 2018
@@ -5078,24 +5078,8 @@ SDValue SelectionDAG::getNode(unsigned O
     break;
   }
   case ISD::SELECT:
-    // select undef, N2, N3 --> N2 (if it's a constant), otherwise N3
-    if (N1.isUndef())
-      return isa<ConstantSDNode>(N2) ? N2 : N3;
-    // select, ?, undef, N3 --> N3
-    if (N2.isUndef())
-      return N3;
-    // select, ?, N2, undef --> N2
-    if (N3.isUndef())
-      return N2;
-
-    // select true, N2, N3 --> N2
-    // select false, N2, N3 --> N3
-    if (auto *N1C = dyn_cast<ConstantSDNode>(N1))
-      return N1C->isNullValue() ? N3 : N2;
-
-    // select ?, N2, N2 --> N2
-    if (N2 == N3)
-      return N2;
+    if (SDValue V = simplifySelect(N1, N2, N3))
+      return V;
     break;
   case ISD::VECTOR_SHUFFLE:
     llvm_unreachable("should use getVectorShuffle constructor!");
@@ -6795,6 +6779,29 @@ SDValue SelectionDAG::getMaskedScatter(S
   return V;
 }
 
+SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {
+  // select undef, T, F --> T (if T is a constant), otherwise F
+  // select, ?, undef, F --> F
+  // select, ?, T, undef --> T
+  if (Cond.isUndef())
+    return isa<ConstantSDNode>(T) ? T : F;
+  if (T.isUndef())
+    return F;
+  if (F.isUndef())
+    return T;
+
+  // fold (select true, T, F) -> T
+  // fold (select false, T, F) -> F
+  if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
+    return CondC->isNullValue() ? F : T;
+
+  // select ?, T, T --> T
+  if (T == F)
+    return T;
+
+  return SDValue();
+}
+
 SDValue SelectionDAG::getVAArg(EVT VT, const SDLoc &dl, SDValue Chain,
                                SDValue Ptr, SDValue SV, unsigned Align) {
   SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };




More information about the llvm-commits mailing list