[llvm] r297086 - [DAG] refactor related div/rem folds; NFCI

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 6 14:32:41 PST 2017


Author: spatel
Date: Mon Mar  6 16:32:40 2017
New Revision: 297086

URL: http://llvm.org/viewvc/llvm-project?rev=297086&view=rev
Log:
[DAG] refactor related div/rem folds; NFCI

This is known incomplete and not called in the right order relative to
other folds, but that's the current behavior. I'm just trying to clean
this up before making actual functional changes to make the patch smaller.

The logic here should mimic the IR equivalents that are in InstSimplify's
simplifyDivRem().

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

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=297086&r1=297085&r2=297086&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Mon Mar  6 16:32:40 2017
@@ -2368,6 +2368,31 @@ SDValue DAGCombiner::useDivRem(SDNode *N
   return combined;
 }
 
+static SDValue simplifyDivRem(SDNode *N, SelectionDAG &DAG) {
+  SDValue N0 = N->getOperand(0);
+  SDValue N1 = N->getOperand(1);
+  EVT VT = N->getValueType(0);
+  SDLoc DL(N);
+
+  // X / undef -> undef
+  // X % undef -> undef
+  if (N1.isUndef())
+    return N1;
+
+  // X / 0 --> undef
+  // X % 0 --> undef
+  // We don't need to preserve faults!
+  if (isNullConstantOrNullSplatConstant(N1))
+    return DAG.getUNDEF(VT);
+
+  // undef / X -> 0
+  // undef % X -> 0
+  if (N0.isUndef())
+    return DAG.getConstant(0, DL, VT);
+
+  return SDValue();
+}
+
 SDValue DAGCombiner::visitSDIV(SDNode *N) {
   SDValue N0 = N->getOperand(0);
   SDValue N1 = N->getOperand(1);
@@ -2457,15 +2482,8 @@ SDValue DAGCombiner::visitSDIV(SDNode *N
     if (SDValue DivRem = useDivRem(N))
         return DivRem;
 
-  // undef / X -> 0
-  if (N0.isUndef())
-    return DAG.getConstant(0, DL, VT);
-  // X / undef -> undef
-  if (N1.isUndef())
-    return N1;
-  // X / 0 --> undef (we don't need to preserve faults!)
-  if (N1C && N1C->isNullValue())
-    return DAG.getUNDEF(VT);
+  if (SDValue V = simplifyDivRem(N, DAG))
+    return V;
 
   return SDValue();
 }
@@ -2535,15 +2553,8 @@ SDValue DAGCombiner::visitUDIV(SDNode *N
     if (SDValue DivRem = useDivRem(N))
         return DivRem;
 
-  // undef / X -> 0
-  if (N0.isUndef())
-    return DAG.getConstant(0, DL, VT);
-  // X / undef -> undef
-  if (N1.isUndef())
-    return N1;
-  // X / 0 --> undef (we don't need to preserve faults!)
-  if (N1C && N1C->isNullValue())
-    return DAG.getUNDEF(VT);
+  if (SDValue V = simplifyDivRem(N, DAG))
+    return V;
 
   return SDValue();
 }
@@ -2618,16 +2629,9 @@ SDValue DAGCombiner::visitREM(SDNode *N)
   if (SDValue DivRem = useDivRem(N))
     return DivRem.getValue(1);
 
-  // undef % X -> 0
-  if (N0.isUndef())
-    return DAG.getConstant(0, DL, VT);
-  // X % undef -> undef
-  if (N1.isUndef())
-    return N1;
-  // X % 0 --> undef (we don't need to preserve faults!)
-  if (N1C && N1C->isNullValue())
-    return DAG.getUNDEF(VT);
-  
+  if (SDValue V = simplifyDivRem(N, DAG))
+    return V;
+
   return SDValue();
 }
 




More information about the llvm-commits mailing list