diff mbox series

[committed,PR,tree-optimization/110199] Simplify MIN/MAX more often

Message ID 9603a093-a2e4-4ae9-b9c7-38b6effdbe20@ventanamicro.com
State New
Headers show
Series [committed,PR,tree-optimization/110199] Simplify MIN/MAX more often | expand

Commit Message

Jeff Law March 10, 2024, 6:05 p.m. UTC
So as I mentioned in the BZ, the case of

t = MIN_EXPR (A, B)

where we know something about the relationship between A and B can be 
trivially handled by some existing code in DOM.  That existing code 
would simplify when A == B.  But by testing GE and LE instead of EQ we 
can cover more cases with minimal effort.  When applicable the MIN/MAX 
turns into a simple copy.

I made one other change.  We have other binary operations that we 
simplify when we know something about the relationship between the 
operands.  That code was not canonicalizing the order of operands when 
building the expression to lookup in the hash tables to discover that 
relationship.  Since those paths are only testing for equality, we can 
trivially reverse them and not have to worry about changing codes or 
anything like that.  So extremely safe and avoids having to come back 
and fix that code to match the MIN_EXPR/MAX_EXPR case later.

Bootstrapped on x86 and also tested on the crosses.  I briefly thought 
there was an sh regression, but that was actually the recent fwprop 
changes twiddling code generation for one test.

Pushed to the trunk.

Jeff
commit 8fe27ed193d60f6cd8b34761858a720c95eabbdb
Author: jlaw <jeffreyalaw@gmail.com>
Date:   Sun Mar 10 11:58:00 2024 -0600

    [committed] [PR tree-optimization/110199] Simplify MIN/MAX more often
    
    So as I mentioned in the BZ, the case of
    
    t = MIN_EXPR (A, B)
    
    where we know something about the relationship between A and B can be trivially
    handled by some existing code in DOM.  That existing code would simplify when A
    == B.  But by testing GE and LE instead of EQ we can cover more cases with
    minimal effort.  When applicable the MIN/MAX turns into a simple copy.
    
    I made one other change.  We have other binary operations that we simplify when
    we know something about the relationship between the operands.  That code was
    not canonicalizing the order of operands when building the expression to lookup
    in the hash tables to discover that relationship.  Since those paths are only
    testing for equality, we can trivially reverse them and not have to worry about
    changing codes or anything like that.  So extremely safe and avoids having to
    come back and fix that code to match the MIN_EXPR/MAX_EXPR case later.
    
    Bootstrapped on x86 and also tested on the crosses.  I briefly thought there
    was an sh regression, but that was actually the recent fwprop changes twiddling
    code generation for one test.
    
            PR tree-optimization/110199
    gcc/
            * tree-ssa-scopedtables.cc
            (avail_exprs_stack::simplify_binary_operation): Generalize handling
            of MIN_EXPR/MAX_EXPR to allow additional simplifications.  Canonicalize
            comparison operands for other cases.
    
    gcc/testsuite
    
            * gcc.dg/tree-ssa/minmax-27.c: New test.
            * gcc.dg/tree-ssa/minmax-28.c: New test.
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c
new file mode 100644
index 00000000000..4b94203b0d0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c
@@ -0,0 +1,118 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-dom2" } */
+
+
+int min1(int a, int b)
+{
+    if (a <= b)
+        return a < b ? a : b;
+    return 0;
+}
+
+int min2(int a, int b)
+{
+    if (a <= b)
+        return a > b ? b : a;
+    return 0;
+}
+
+int min3(int a, int b)
+{
+    if (a < b)
+        return a < b ? a : b;
+    return 0;
+}
+
+int min4(int a, int b)
+{
+    if (a < b)
+        return a > b ? b : a;
+    return 0;
+}
+
+int min5(int a, int b)
+{
+    if (a <= b)
+        return a <= b ? a : b;
+    return 0;
+}
+
+int min6(int a, int b)
+{
+    if (a <= b)
+        return a >= b ? b : a;
+    return 0;
+}
+
+int min7(int a, int b)
+{
+    if (a < b)
+        return a <= b ? a : b;
+    return 0;
+}
+
+int min8(int a, int b)
+{
+    if (b > a)
+        return a >= b ? b : a;
+    return 0;
+}
+
+int min9(int a, int b)
+{
+    if (b >= a)
+        return a < b ? a : b;
+    return 0;
+}
+
+int min10(int a, int b)
+{
+    if (b >= a)
+        return a > b ? b : a;
+    return 0;
+}
+
+int min11(int a, int b)
+{
+    if (b > a)
+        return a < b ? a : b;
+    return 0;
+}
+
+int min12(int a, int b)
+{
+    if (b > a)
+        return a > b ? b : a;
+    return 0;
+}
+
+int min13(int a, int b)
+{
+    if (b >= a)
+        return a <= b ? a : b;
+    return 0;
+}
+
+int min14(int a, int b)
+{
+    if (b >= a)
+        return a >= b ? b : a;
+    return 0;
+}
+
+int min15(int a, int b)
+{
+    if (b > a)
+        return a <= b ? a : b;
+    return 0;
+}
+
+int min16(int a, int b)
+{
+    if (b > a)
+        return a >= b ? b : a;
+    return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "MIN_EXPR" "dom2" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c
new file mode 100644
index 00000000000..732126d7449
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c
@@ -0,0 +1,117 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-dom2" } */
+
+int max1(int a, int b)
+{
+    if (a <= b)
+        return a < b ? b : a;
+    return 0;
+}
+
+int max2(int a, int b)
+{
+    if (a <= b)
+        return a > b ? a : b;
+    return 0;
+}
+
+int max3(int a, int b)
+{
+    if (a < b)
+        return a < b ? b : a;
+    return 0;
+}
+
+int max4(int a, int b)
+{
+    if (a < b)
+        return a > b ? a : b;
+    return 0;
+}
+
+int max5(int a, int b)
+{
+    if (a <= b)
+        return a <= b ? b : a;
+    return 0;
+}
+
+int max6(int a, int b)
+{
+    if (a <= b)
+        return a >= b ? a : b;
+    return 0;
+}
+
+int max7(int a, int b)
+{
+    if (a < b)
+        return a <= b ? b : a;
+    return 0;
+}
+
+int max8(int a, int b)
+{
+    if (b > a)
+        return a >= b ? a : b;
+    return 0;
+}
+
+int max9(int a, int b)
+{
+    if (b >= a)
+        return a < b ? b : a;
+    return 0;
+}
+
+int max10(int a, int b)
+{
+    if (b >= a)
+        return a > b ? a : b;
+    return 0;
+}
+
+int max11(int a, int b)
+{
+    if (b > a)
+        return a < b ? b : a;
+    return 0;
+}
+
+int max12(int a, int b)
+{
+    if (b > a)
+        return a > b ? a : b;
+    return 0;
+}
+
+int max13(int a, int b)
+{
+    if (b >= a)
+        return a <= b ? b : a;
+    return 0;
+}
+
+int max14(int a, int b)
+{
+    if (b >= a)
+        return a >= b ? a : b;
+    return 0;
+}
+
+int max15(int a, int b)
+{
+    if (b > a)
+        return a <= b ? b : a;
+    return 0;
+}
+
+int max16(int a, int b)
+{
+    if (b > a)
+        return a >= b ? a : b;
+    return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "MAX_EXPR" "dom2" } } */
+
diff --git a/gcc/tree-ssa-scopedtables.cc b/gcc/tree-ssa-scopedtables.cc
index e53dfd445ea..c367d37fa9b 100644
--- a/gcc/tree-ssa-scopedtables.cc
+++ b/gcc/tree-ssa-scopedtables.cc
@@ -127,10 +127,49 @@  avail_exprs_stack::simplify_binary_operation (gimple *stmt,
 
 	  switch (code)
 	    {
-	    /* For these cases, if we know the operands
-	       are equal, then we know the result.  */
+	    /* For these cases, if we know some relationships
+	       between the operands, then we can simplify.  */
 	    case MIN_EXPR:
 	    case MAX_EXPR:
+	      {
+		/* Build a simple equality expr and query the hash table
+		   for it.  */
+		struct hashable_expr expr;
+		expr.type = boolean_type_node;
+		expr.kind = EXPR_BINARY;
+		expr.ops.binary.op = LE_EXPR;
+		tree rhs1 = gimple_assign_rhs1 (stmt);
+		tree rhs2 = gimple_assign_rhs2 (stmt);
+		if (tree_swap_operands_p (rhs1, rhs2))
+		  std::swap (rhs1, rhs2);
+		expr.ops.binary.opnd0 = rhs1;
+		expr.ops.binary.opnd1 = rhs2;
+		class expr_hash_elt element2 (&expr, NULL_TREE);
+		expr_hash_elt **slot
+		  = m_avail_exprs->find_slot (&element2, NO_INSERT);
+
+		/* If the query was successful and returned a nonzero
+		   result, then we know the result of the MIN/MAX, even
+		   though it is not a constant value.  */
+		if (slot && *slot && integer_onep ((*slot)->lhs ()))
+		  return code == MIN_EXPR ? rhs1 : rhs2;
+
+		/* Try again, this time with GE_EXPR.  */
+		expr.ops.binary.op = GE_EXPR;
+		class expr_hash_elt element3 (&expr, NULL_TREE);
+		slot = m_avail_exprs->find_slot (&element3, NO_INSERT);
+
+		/* If the query was successful and returned a nonzero
+		   result, then we know the result of the MIN/MAX, even
+		   though it is not a constant value.  */
+		if (slot && *slot && integer_onep ((*slot)->lhs ()))
+		  return code == MIN_EXPR ? rhs2 : rhs1;
+
+		break;
+	      }
+
+	    /* For these cases, if we know the operands
+	       are equal, then we know the result.  */
 	    case BIT_IOR_EXPR:
 	    case BIT_AND_EXPR:
 	    case BIT_XOR_EXPR:
@@ -151,8 +190,12 @@  avail_exprs_stack::simplify_binary_operation (gimple *stmt,
 		expr.type = boolean_type_node;
 		expr.kind = EXPR_BINARY;
 		expr.ops.binary.op = EQ_EXPR;
-		expr.ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
-		expr.ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
+		tree rhs1 = gimple_assign_rhs1 (stmt);
+		tree rhs2 = gimple_assign_rhs2 (stmt);
+		if (tree_swap_operands_p (rhs1, rhs2))
+		  std::swap (rhs1, rhs2);
+		expr.ops.binary.opnd0 = rhs1;
+		expr.ops.binary.opnd1 = rhs2;
 		class expr_hash_elt element2 (&expr, NULL_TREE);
 		expr_hash_elt **slot
 		  = m_avail_exprs->find_slot (&element2, NO_INSERT);
@@ -168,8 +211,6 @@  avail_exprs_stack::simplify_binary_operation (gimple *stmt,
 		  {
 		    switch (code)
 		      {
-		      case MIN_EXPR:
-		      case MAX_EXPR:
 		      case BIT_IOR_EXPR:
 		      case BIT_AND_EXPR:
 			return gimple_assign_rhs1 (stmt);