Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2)
On 09/06/2017 03:55 AM, Jackson Woodruff wrote: > Hi all, > > A minor improvement came to mind while updating other parts of this patch. > > I've updated a testcase to make it more clear and a condition now uses a > call to is_division_by rather than manually checking those conditions. > > Jackson > > On 08/30/2017 05:32 PM, Jackson Woodruff wrote: >> Hi all, >> >> I've attached a new version of the patch in response to a few of >> Wilco's comments in person. >> >> The end product of the pass is still the same, but I have fixed >> several bugs. >> >> Now tested independently of the other patches. >> >> On 08/15/2017 03:07 PM, Richard Biener wrote: >>> On Thu, Aug 10, 2017 at 4:10 PM, Jackson Woodruff >>>wrote: Hi all, The patch implements the some of the division optimizations discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026 . We now reassociate (as discussed in the bug report): x / (y * y) -> x * (1 / y) * (1 / y) If it is reasonable to do so. This is done with -funsafe-math-optimizations. Bootstrapped and regtested with part (1/2). OK for trunk? >>> >>> I believe your enhancement shows the inherent weakness of >>> CSE of reciprocals in that it works from the defs. It will >>> handle x / (y * y) but not x / (y * y * y). >>> >>> I think a rewrite of this mini-pass is warranted. >> >> I suspect that there might be more to gain by of handling the case of >> x / (y * z) rather than the case of x / (y**n), but I agree that this >> pass could do more. >> >>> >>> Richard. >>> Jackson gcc/ 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * tree-ssa-math-opts (is_division_by_square, is_square_of, insert_sqaure_reciprocals): New. (insert_reciprocals): Change to insert reciprocals before a division by a square. (execute_cse_reciprocals_1): Change to consider division by a square. gcc/testsuite 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * gcc.dg/associate_division_1.c: New. >> >> Thanks, >> >> Jackson. >> >> Updated ChangeLog: >> >> gcc/ >> >> 2017-08-30 Jackson Woodruff >> >> PR 71026/tree-optimization >> * tree-ssa-math-opts (is_division_by_square, is_square_of): New. >> (insert_reciprocals): Change to insert reciprocals >> before a division by a square and to insert the square >> of a reciprocal. >> (execute_cse_reciprocals_1): Change to consider >> division by a square. >> (register_division_in): Add importance parameter. >> >> gcc/testsuite >> >> 2017-08-30 Jackson Woodruff >> >> PR 71026/tree-optimization >> * gcc.dg/extract_recip_3.c: New. >> * gcc.dg/extract_recip_4.c: New. >> * gfortran.dg/extract_recip_1.f: New. OK for the trunk. Sorry for the delays. Do you have commit privileges in GCC? Jeff
Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2)
ping From: Jackson Woodruff <jackson.woodr...@foss.arm.com> Sent: 06 September 2017 10:55 To: Richard Biener Cc: Wilco Dijkstra; kyrylo.tkac...@foss.arm.com; Joseph S. Myers; GCC Patches Subject: Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2) Hi all, A minor improvement came to mind while updating other parts of this patch. I've updated a testcase to make it more clear and a condition now uses a call to is_division_by rather than manually checking those conditions. Jackson On 08/30/2017 05:32 PM, Jackson Woodruff wrote: > Hi all, > > I've attached a new version of the patch in response to a few of Wilco's > comments in person. > > The end product of the pass is still the same, but I have fixed several > bugs. > > Now tested independently of the other patches. > > On 08/15/2017 03:07 PM, Richard Biener wrote: >> On Thu, Aug 10, 2017 at 4:10 PM, Jackson Woodruff >> <jackson.woodr...@foss.arm.com> wrote: >>> Hi all, >>> >>> The patch implements the some of the division optimizations discussed in >>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026 . >>> >>> We now reassociate (as discussed in the bug report): >>> >>> x / (y * y) -> x * (1 / y) * (1 / y) >>> >>> If it is reasonable to do so. This is done with >>> -funsafe-math-optimizations. >>> >>> Bootstrapped and regtested with part (1/2). OK for trunk? >> >> I believe your enhancement shows the inherent weakness of >> CSE of reciprocals in that it works from the defs. It will >> handle x / (y * y) but not x / (y * y * y). >> >> I think a rewrite of this mini-pass is warranted. > > I suspect that there might be more to gain by of handling the case of > x / (y * z) rather than the case of x / (y**n), but I agree that this > pass could do more. > >> >> Richard. >> >>> Jackson >>> >>> gcc/ >>> >>> 2017-08-03 Jackson Woodruff <jackson.woodr...@arm.com> >>> >>> PR 71026/tree-optimization >>> * tree-ssa-math-opts (is_division_by_square, >>> is_square_of, insert_sqaure_reciprocals): New. >>> (insert_reciprocals): Change to insert reciprocals >>> before a division by a square. >>> (execute_cse_reciprocals_1): Change to consider >>> division by a square. >>> >>> >>> gcc/testsuite >>> >>> 2017-08-03 Jackson Woodruff <jackson.woodr...@arm.com> >>> >>> PR 71026/tree-optimization >>> * gcc.dg/associate_division_1.c: New. >>> > > Thanks, > > Jackson. > > Updated ChangeLog: > > gcc/ > > 2017-08-30 Jackson Woodruff <jackson.woodr...@arm.com> > > PR 71026/tree-optimization > * tree-ssa-math-opts (is_division_by_square, is_square_of): New. > (insert_reciprocals): Change to insert reciprocals > before a division by a square and to insert the square > of a reciprocal. > (execute_cse_reciprocals_1): Change to consider > division by a square. > (register_division_in): Add importance parameter. > > gcc/testsuite > > 2017-08-30 Jackson Woodruff <jackson.woodr...@arm.com> > > PR 71026/tree-optimization > * gcc.dg/extract_recip_3.c: New. > * gcc.dg/extract_recip_4.c: New. > * gfortran.dg/extract_recip_1.f: New. diff --git a/gcc/testsuite/gcc.dg/extract_recip_3.c b/gcc/testsuite/gcc.dg/extract_recip_3.c new file mode 100644 index ..ad9f2dc36f1e695ceca1f50bc78f4ac4fbb2e787 --- /dev/null +++ b/gcc/testsuite/gcc.dg/extract_recip_3.c @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +float +extract_square (float *a, float *b, float x, float y) +{ + *a = 3 / (y * y); + *b = 5 / (y * y); + + return x / (y * y); +} + +/* Don't expect the 'powmult' (calculation of y * y) + to be deleted until a later pass, so look for one + more multiplication than strictly necessary. */ +float +extract_recip (float *a, float *b, float x, float y, float z) +{ + *a = 7 / y; + *b = x / (y * y); + + return z / y; +} + +/* 4 For the pointers to a, b, 4 multiplications in 'extract_square', + 4 multiplications in 'extract_recip' expected. */ +/* { dg-final { scan-tree-dump-times " \\* " 12 "optimized" } } */ + +/* 1 division in 'extract_square', 1 division in 'extract_recip'. */ +/* { dg-final { scan-tree-dump-times " / " 2 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/extract_recip
Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2)
Hi all, A minor improvement came to mind while updating other parts of this patch. I've updated a testcase to make it more clear and a condition now uses a call to is_division_by rather than manually checking those conditions. Jackson On 08/30/2017 05:32 PM, Jackson Woodruff wrote: Hi all, I've attached a new version of the patch in response to a few of Wilco's comments in person. The end product of the pass is still the same, but I have fixed several bugs. Now tested independently of the other patches. On 08/15/2017 03:07 PM, Richard Biener wrote: On Thu, Aug 10, 2017 at 4:10 PM, Jackson Woodruffwrote: Hi all, The patch implements the some of the division optimizations discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026 . We now reassociate (as discussed in the bug report): x / (y * y) -> x * (1 / y) * (1 / y) If it is reasonable to do so. This is done with -funsafe-math-optimizations. Bootstrapped and regtested with part (1/2). OK for trunk? I believe your enhancement shows the inherent weakness of CSE of reciprocals in that it works from the defs. It will handle x / (y * y) but not x / (y * y * y). I think a rewrite of this mini-pass is warranted. I suspect that there might be more to gain by of handling the case of x / (y * z) rather than the case of x / (y**n), but I agree that this pass could do more. Richard. Jackson gcc/ 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * tree-ssa-math-opts (is_division_by_square, is_square_of, insert_sqaure_reciprocals): New. (insert_reciprocals): Change to insert reciprocals before a division by a square. (execute_cse_reciprocals_1): Change to consider division by a square. gcc/testsuite 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * gcc.dg/associate_division_1.c: New. Thanks, Jackson. Updated ChangeLog: gcc/ 2017-08-30 Jackson Woodruff PR 71026/tree-optimization * tree-ssa-math-opts (is_division_by_square, is_square_of): New. (insert_reciprocals): Change to insert reciprocals before a division by a square and to insert the square of a reciprocal. (execute_cse_reciprocals_1): Change to consider division by a square. (register_division_in): Add importance parameter. gcc/testsuite 2017-08-30 Jackson Woodruff PR 71026/tree-optimization * gcc.dg/extract_recip_3.c: New. * gcc.dg/extract_recip_4.c: New. * gfortran.dg/extract_recip_1.f: New. diff --git a/gcc/testsuite/gcc.dg/extract_recip_3.c b/gcc/testsuite/gcc.dg/extract_recip_3.c new file mode 100644 index ..ad9f2dc36f1e695ceca1f50bc78f4ac4fbb2e787 --- /dev/null +++ b/gcc/testsuite/gcc.dg/extract_recip_3.c @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +float +extract_square (float *a, float *b, float x, float y) +{ + *a = 3 / (y * y); + *b = 5 / (y * y); + + return x / (y * y); +} + +/* Don't expect the 'powmult' (calculation of y * y) + to be deleted until a later pass, so look for one + more multiplication than strictly necessary. */ +float +extract_recip (float *a, float *b, float x, float y, float z) +{ + *a = 7 / y; + *b = x / (y * y); + + return z / y; +} + +/* 4 For the pointers to a, b, 4 multiplications in 'extract_square', + 4 multiplications in 'extract_recip' expected. */ +/* { dg-final { scan-tree-dump-times " \\* " 12 "optimized" } } */ + +/* 1 division in 'extract_square', 1 division in 'extract_recip'. */ +/* { dg-final { scan-tree-dump-times " / " 2 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/extract_recip_4.c b/gcc/testsuite/gcc.dg/extract_recip_4.c new file mode 100644 index ..83105c60ced5c2671f3793d76482c35502712a2c --- /dev/null +++ b/gcc/testsuite/gcc.dg/extract_recip_4.c @@ -0,0 +1,35 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +/* Don't expect any of these divisions to be extracted. */ +double f (double x, int p) +{ + if (p > 0) +{ + return 1.0/(x * x); +} + + if (p > -1) +{ + return x * x * x; +} + return 1.0 /(x); +} + +/* Expect a reciprocal to be extracted here. */ +double g (double *a, double x, double y) +{ + *a = 3 / y; + double k = x / (y * y); + + if (y * y == 2.0) +return k + 1 / y; + else +return k - 1 / y; +} + +/* Expect 2 divisions in 'f' and 1 in 'g'. */ +/* { dg-final { scan-tree-dump-times " / " 3 "optimized" } } */ +/* Expect 3 multiplications in 'f' and 4 in 'g'. Also + expect one for the point to a. */ +/* { dg-final { scan-tree-dump-times " \\* " 8 "optimized" } } */ diff --git a/gcc/testsuite/gfortran.dg/extract_recip_1.f
Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2)
Hi all, I've attached a new version of the patch in response to a few of Wilco's comments in person. The end product of the pass is still the same, but I have fixed several bugs. Now tested independently of the other patches. On 08/15/2017 03:07 PM, Richard Biener wrote: On Thu, Aug 10, 2017 at 4:10 PM, Jackson Woodruffwrote: Hi all, The patch implements the some of the division optimizations discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026 . We now reassociate (as discussed in the bug report): x / (y * y) -> x * (1 / y) * (1 / y) If it is reasonable to do so. This is done with -funsafe-math-optimizations. Bootstrapped and regtested with part (1/2). OK for trunk? I believe your enhancement shows the inherent weakness of CSE of reciprocals in that it works from the defs. It will handle x / (y * y) but not x / (y * y * y). I think a rewrite of this mini-pass is warranted. I suspect that there might be more to gain by of handling the case of x / (y * z) rather than the case of x / (y**n), but I agree that this pass could do more. Richard. Jackson gcc/ 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * tree-ssa-math-opts (is_division_by_square, is_square_of, insert_sqaure_reciprocals): New. (insert_reciprocals): Change to insert reciprocals before a division by a square. (execute_cse_reciprocals_1): Change to consider division by a square. gcc/testsuite 2017-08-03 Jackson Woodruff PR 71026/tree-optimization * gcc.dg/associate_division_1.c: New. Thanks, Jackson. Updated ChangeLog: gcc/ 2017-08-30 Jackson Woodruff PR 71026/tree-optimization * tree-ssa-math-opts (is_division_by_square, is_square_of): New. (insert_reciprocals): Change to insert reciprocals before a division by a square and to insert the square of a reciprocal. (execute_cse_reciprocals_1): Change to consider division by a square. (register_division_in): Add importance parameter. gcc/testsuite 2017-08-30 Jackson Woodruff PR 71026/tree-optimization * gcc.dg/extract_recip_3.c: New. * gcc.dg/extract_recip_4.c: New. * gfortran.dg/extract_recip_1.f: New. diff --git a/gcc/testsuite/gcc.dg/extract_recip_3.c b/gcc/testsuite/gcc.dg/extract_recip_3.c new file mode 100644 index ..0ea3fdf5cca06a0806a55185ef8b0a4b0507 --- /dev/null +++ b/gcc/testsuite/gcc.dg/extract_recip_3.c @@ -0,0 +1,29 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +float +extract_square (float x, float y, float *a, float *b) +{ + *a = 3 / (y * y); + *b = 5 / (y * y); + + return x / (y * y); +} + +/* Don't expect the 'powmult' (calculation of y * y) + to be deleted until a later pass, so look for one + more multiplication than strictly necessary. */ +float +extract_recip (float *w, float x, float y, float z) +{ + *w = 7 / y; + + return x / (y * y) + z / y; +} + +/* 3 For the pointers to a, b and w, 4 multiplications in 'extract_square', + 5 multiplications in 'extract_recip' expected. */ +/* { dg-final { scan-tree-dump-times " \\* " 12 "optimized" } } */ + +/* 1 division in 'extract_square', 1 division in 'extract_recip'. */ +/* { dg-final { scan-tree-dump-times " / " 2 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/extract_recip_4.c b/gcc/testsuite/gcc.dg/extract_recip_4.c new file mode 100644 index ..5a31d40ed910cdd1914cc1e82358493be428946a --- /dev/null +++ b/gcc/testsuite/gcc.dg/extract_recip_4.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +/* Don't expect any of these divisions to be extracted. */ +double f (double x, int p) +{ + if (p > 0) +{ + return 1.0/(x * x); +} + + if (p > -1) +{ + return x * x * x; +} + return 1.0 /(x); +} + +/* Expect a reciprocal to be extracted here. */ +double g (double x, double y) +{ + double k = x / (y * y); + + if (y * y == 2.0) +return k + 1 / y; + else +return k - 1 / y; +} + +/* Expect 2 divisions in 'f' and 1 in 'g'. */ +/* { dg-final { scan-tree-dump-times " / " 3 "optimized" } } */ +/* Expect 3 multiplications in 'f' and 3 in 'g'. */ +/* { dg-final { scan-tree-dump-times " \\* " 6 "optimized" } } */ diff --git a/gcc/testsuite/gfortran.dg/extract_recip_1.f b/gcc/testsuite/gfortran.dg/extract_recip_1.f new file mode 100644 index ..ecf05189773b6c2f46222857fd88fd010bfdf348 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/extract_recip_1.f @@ -0,0 +1,19 @@ +! { dg-do compile } +! { dg-options "-Ofast -fdump-tree-optimized" } + + SUBROUTINE F(N,X,Y,Z,A,B) + DIMENSION
Re: [PATCH] Factor out division by squares and remove division around comparisons (2/2)
On Thu, Aug 10, 2017 at 4:10 PM, Jackson Woodruffwrote: > Hi all, > > The patch implements the some of the division optimizations discussed in > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026 . > > We now reassociate (as discussed in the bug report): > > x / (y * y) -> x * (1 / y) * (1 / y) > > If it is reasonable to do so. This is done with > -funsafe-math-optimizations. > > Bootstrapped and regtested with part (1/2). OK for trunk? I believe your enhancement shows the inherent weakness of CSE of reciprocals in that it works from the defs. It will handle x / (y * y) but not x / (y * y * y). I think a rewrite of this mini-pass is warranted. Richard. > Jackson > > gcc/ > > 2017-08-03 Jackson Woodruff > > PR 71026/tree-optimization > * tree-ssa-math-opts (is_division_by_square, > is_square_of, insert_sqaure_reciprocals): New. > (insert_reciprocals): Change to insert reciprocals > before a division by a square. > (execute_cse_reciprocals_1): Change to consider > division by a square. > > > gcc/testsuite > > 2017-08-03 Jackson Woodruff > > PR 71026/tree-optimization > * gcc.dg/associate_division_1.c: New. >