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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 26 04:51:07 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 &&
----------------
arsenm wrote:

It depends in which combiner phase, eventually only legal operations can be emitted 

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


More information about the llvm-commits mailing list