Hello,here are a few simple transformations, mostly useful for types with undefined overflow where we do not have reassoc.
I did not name the testcase reassoc-* to leave that namespace to the realloc pass, and -fno-tree-reassoc is just in case someone ever enhances that pass...
Bootstrap + testsuite on powerpc64le-unknown-linux-gnu. 2017-06-23 Marc Glisse <marc.gli...@inria.fr> gcc/ * match.pd ((A+-B)+(C-A), (A+B)-(A-C)): New transformations. gcc/testsuite/ * gcc.dg/tree-ssa/assoc-1.c: New file. -- Marc Glisse
Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 249585) +++ gcc/match.pd (working copy) @@ -1314,20 +1314,32 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (negate @1)) (simplify (plus:c (minus @0 @1) @1) @0) (simplify (minus @0 (plus:c @0 @1)) (negate @1)) (simplify (minus @0 (minus @0 @1)) @1) + /* (A +- B) + (C - A) -> C +- B */ + /* (A + B) - (A - C) -> B + C */ + /* More cases are handled with comparisons. */ + (simplify + (plus:c (plus:c @0 @1) (minus @2 @0)) + (plus @2 @1)) + (simplify + (plus:c (minus @0 @1) (minus @2 @0)) + (minus @2 @1)) + (simplify + (minus (plus:c @0 @1) (minus @0 @2)) + (plus @1 @2)) /* (A +- CST1) +- CST2 -> A + CST3 Use view_convert because it is safe for vectors and equivalent for scalars. */ (for outer_op (plus minus) (for inner_op (plus minus) neg_inner_op (minus plus) (simplify (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1)) CONSTANT_CLASS_P@2) Index: gcc/testsuite/gcc.dg/tree-ssa/assoc-1.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/assoc-1.c (nonexistent) +++ gcc/testsuite/gcc.dg/tree-ssa/assoc-1.c (working copy) @@ -0,0 +1,31 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized-raw -fno-tree-reassoc" } */ + +int f0(int a,int b,int c){ + int d = a + b; + int e = c + b; + return d - e; +} +int f1(int a,int b,int c){ + int d = a + b; + int e = b - c; + return d - e; +} +int f2(int a,int b,int c){ + int d = a + b; + int e = c - b; + return e + d; +} +int f3(int a,int b,int c){ + int d = a - b; + int e = c - b; + return d - e; +} +int f4(int a,int b,int c){ + int d = b - a; + int e = c - b; + return e + d; +} + +/* { dg-final { scan-tree-dump-times "plus_expr" 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "minus_expr" 3 "optimized" } } */