diff mbox

Optimize certain end of loop conditions into min/max operation

Message ID 560CC744.4040704@linaro.org
State New
Headers show

Commit Message

Michael Collison Oct. 1, 2015, 5:40 a.m. UTC
Richard and Marc,

Latest patch attached which incorporates all comments.

2015-09-30  Michael Collison <michael.collison@linaro.org>
         Andrew Pinski <andrew.pinski@caviumnetworks.com>

     * match.pd ((x < y) && (x < z) -> x < min (y,z),
     (x > y) and (x > z) -> x > max (y,z))
     * testsuite/gcc.dg/tree-ssa/minmax-loopend.c: New test.

On 09/30/2015 12:30 PM, Marc Glisse wrote:
>>>>>> On Fri, 18 Sep 2015, Marc Glisse wrote:
>>>>>>>> +(bit_and:c (op @0 @1) (op @0 @2))
>>>>>>>
>>>>>>> :c seems useless here. On the other hand, it might make sense to 
>>>>>>> use op:s
>>>>>>> since this is mostly useful if it removes the 2 original 
>>>>>>> comparisons.
>
> As I was saying, :c is useless.
> (x:c y z)
> is replaced by two copies of the transformation, one with
> (x y z)
> and the other with
> (x z y)
> In your transformation, both versions would be equivalent, so the second
> one is redundant.
>
> Also, if you have:
> a=x<y;
> b=x<z;
> c=a&b;
> reuse(a);
> reuse(b);
>
> (i.e. the comparison results are used for more than just this bit_and)
> then your transformation may make the code more expensive. To avoid
> this, you can write op:s, meaning that the result of op is used only
> once.
>

Comments

Marc Glisse Oct. 1, 2015, 6:42 a.m. UTC | #1
On Wed, 30 Sep 2015, Michael Collison wrote:

> Richard and Marc,
>
> Latest patch attached which incorporates all comments.
>
> 2015-09-30  Michael Collison <michael.collison@linaro.org>
>        Andrew Pinski <andrew.pinski@caviumnetworks.com>
>
>    * match.pd ((x < y) && (x < z) -> x < min (y,z),
>    (x > y) and (x > z) -> x > max (y,z))
>    * testsuite/gcc.dg/tree-ssa/minmax-loopend.c: New test.

You are still missing at least the indentation.

+/* { dg-final { scan-tree-dump "MIN_EXPR" 1 "optimized" } } */

I believe it is only scan-tree-dump-times that takes a number. 
scan-tree-dump seems to have only 2 arguments.
diff mbox

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index bd5c267..ef2e025 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2311,3 +2311,13 @@  along with GCC; see the file COPYING3.  If not see
     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
 	       (convert:utype @4))))))))
+
+/* Transform (@0 < @1 and @0 < @2) to use min, 
+   (@0 > @1 and @0 > @2) to use max */
+(for op (lt le gt ge)
+     ext (min min max max)
+(simplify
+(bit_and (op:s @0 @1) (op:s @0 @2))
+(if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
+(op @0 (ext @1 @2)))))
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-loopend.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-loopend.c
new file mode 100644
index 0000000..dfe6120
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-loopend.c
@@ -0,0 +1,17 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int min_test(long a, long b, long c) {
+  int cmp1 = a < b;
+  int cmp2 = a < c;
+  return cmp1 & cmp2;
+}
+
+int max_test (long a, long b, long c) {
+  int cmp1 = a > b;
+  int cmp2 = a > c;
+  return cmp1 & cmp2;
+}
+
+/* { dg-final { scan-tree-dump "MIN_EXPR" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump "MAX_EXPR" 1 "optimized" } } */
-- 
1.9.1