[llvm] [DAGCombiner] Turn `(neg (max x, (neg x)))` into `(min x, (neg x))` (PR #120666)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 23 09:51:34 PST 2024


================
@@ -3949,6 +3949,40 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
       if (SDValue Result = TLI.expandABS(N1.getNode(), DAG, true))
         return Result;
 
+    // Similar to the previous rule, but this time targeting an expanded abs.
+    // (sub 0, (max X, (sub 0, X))) --> (min X, (sub 0, X))
+    // as well as
+    // (sub 0, (min X, (sub 0, X))) --> (max X, (sub 0, X))
+    // Note that these two are applicable to both signed and unsigned min/max.
+    SDValue X;
+    SDValue S0;
+    auto NegPat = m_AllOf(m_Neg(m_Deferred(X)), m_Value(S0));
+    if (LegalOperations &&
+        sd_match(N1, m_OneUse(m_AnyOf(m_SMax(m_Value(X), NegPat),
+                                      m_UMax(m_Value(X), NegPat),
+                                      m_SMin(m_Value(X), NegPat),
+                                      m_UMin(m_Value(X), NegPat))))) {
+      unsigned NewOpc = 0;
+      switch (N1->getOpcode()) {
+      case ISD::SMAX:
+        NewOpc = ISD::SMIN;
+        break;
+      case ISD::UMAX:
+        NewOpc = ISD::UMIN;
+        break;
+      case ISD::SMIN:
+        NewOpc = ISD::SMAX;
+        break;
+      case ISD::UMIN:
+        NewOpc = ISD::UMAX;
+        break;
+      default:
+        llvm_unreachable("unrecognized opcode");
+      }
----------------
mshockwave wrote:

Done. I'd extracted them into `ISD::getInverseMinMaxOpcode`.

https://github.com/llvm/llvm-project/pull/120666


More information about the llvm-commits mailing list