From bf9197df810b0b0ac032c7e480d5a8b24c22f973 Mon Sep 17 00:00:00 2001 From: Janus Weil Date: Fri, 10 Aug 2018 16:08:53 +0200 Subject: [PATCH] re PR fortran/57160 (short-circuit IF only with -ffrontend-optimize) 2018-08-10 Janus Weil PR fortran/57160 * invoke.texi (frontend-optimize): Mention short-circuiting. * options.c (gfc_post_options): Disable -ffrontend-optimize with -Og. * resolve.c (resolve_operator): Warn about short-circuiting only with -ffrontend-optimize. * trans-expr.c (gfc_conv_expr_op): Use short-circuiting operators only with -ffrontend-optimize. Without that flag, make sure that both operands are evaluated. 2018-08-10 Janus Weil PR fortran/57160 * gfortran.dg/actual_pointer_function_1.f90: Fix invalid test case. * gfortran.dg/inline_matmul_23.f90: Add option "-ffrontend-optimize". * gfortran.dg/short_circuiting_2.f90: New test case. * gfortran.dg/short_circuiting_3.f90: New test case. From-SVN: r263471 --- gcc/fortran/ChangeLog | 11 +++++++++ gcc/fortran/invoke.texi | 18 ++++++++------ gcc/fortran/options.c | 2 +- gcc/fortran/resolve.c | 3 ++- gcc/fortran/trans-expr.c | 4 ++-- gcc/testsuite/ChangeLog | 8 +++++++ .../gfortran.dg/actual_pointer_function_1.f90 | 6 ++++- gcc/testsuite/gfortran.dg/inline_matmul_23.f90 | 2 +- gcc/testsuite/gfortran.dg/short_circuiting_2.f90 | 28 ++++++++++++++++++++++ gcc/testsuite/gfortran.dg/short_circuiting_3.f90 | 28 ++++++++++++++++++++++ 10 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/short_circuiting_2.f90 create mode 100644 gcc/testsuite/gfortran.dg/short_circuiting_3.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 7051509..26ff784 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,14 @@ +2018-08-10 Janus Weil + + PR fortran/57160 + * invoke.texi (frontend-optimize): Mention short-circuiting. + * options.c (gfc_post_options): Disable -ffrontend-optimize with -Og. + * resolve.c (resolve_operator): Warn about short-circuiting only with + -ffrontend-optimize. + * trans-expr.c (gfc_conv_expr_op): Use short-circuiting operators only + with -ffrontend-optimize. Without that flag, make sure that both + operands are evaluated. + 2018-08-08 Nathan Sidwell * cpp.c (cb_file_change): Use linemap_included_from. diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi index 093864b..ee84a0b 100644 --- a/gcc/fortran/invoke.texi +++ b/gcc/fortran/invoke.texi @@ -1793,13 +1793,17 @@ if @option{-ffrontend-optimize} is in effect. @opindex @code{frontend-optimize} @cindex Front-end optimization This option performs front-end optimization, based on manipulating -parts the Fortran parse tree. Enabled by default by any @option{-O} -option. Optimizations enabled by this option include inlining calls -to @code{MATMUL}, elimination of identical function calls within -expressions, removing unnecessary calls to @code{TRIM} in comparisons -and assignments and replacing @code{TRIM(a)} with -@code{a(1:LEN_TRIM(a))}. It can be deselected by specifying -@option{-fno-frontend-optimize}. +parts the Fortran parse tree. Enabled by default by any @option{-O} option +except @option{-O0} and @option{-Og}. Optimizations enabled by this option +include: +@itemize @bullet +@item inlining calls to @code{MATMUL}, +@item elimination of identical function calls within expressions, +@item removing unnecessary calls to @code{TRIM} in comparisons and assignments, +@item replacing @code{TRIM(a)} with @code{a(1:LEN_TRIM(a))} and +@item short-circuiting of logical operators (@code{.AND.} and @code{.OR.}). +@end itemize +It can be deselected by specifying @option{-fno-frontend-optimize}. @item -ffrontend-loop-interchange @opindex @code{frontend-loop-interchange} diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c index af93751..e8db54d 100644 --- a/gcc/fortran/options.c +++ b/gcc/fortran/options.c @@ -417,7 +417,7 @@ gfc_post_options (const char **pfilename) specified it directly. */ if (flag_frontend_optimize == -1) - flag_frontend_optimize = optimize; + flag_frontend_optimize = optimize && !optimize_debug; /* Same for front end loop interchange. */ diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 3035e02..16146e6 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -3982,7 +3982,8 @@ resolve_operator (gfc_expr *e) else if (op2->ts.kind < e->ts.kind) gfc_convert_type (op2, &e->ts, 2); - if (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR) + if (flag_frontend_optimize && + (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR)) { /* Warn about short-circuiting with impure function as second operand. */ diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c index dfc44f7..54e318e 100644 --- a/gcc/fortran/trans-expr.c +++ b/gcc/fortran/trans-expr.c @@ -3348,12 +3348,12 @@ gfc_conv_expr_op (gfc_se * se, gfc_expr * expr) return; case INTRINSIC_AND: - code = TRUTH_ANDIF_EXPR; + code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR; lop = 1; break; case INTRINSIC_OR: - code = TRUTH_ORIF_EXPR; + code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR; lop = 1; break; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 174137e..ffa2035 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2018-08-10 Janus Weil + + PR fortran/57160 + * gfortran.dg/actual_pointer_function_1.f90: Fix invalid test case. + * gfortran.dg/inline_matmul_23.f90: Add option "-ffrontend-optimize". + * gfortran.dg/short_circuiting_2.f90: New test case. + * gfortran.dg/short_circuiting_3.f90: New test case. + 2018-08-10 Alexander Monakov PR target/82418 diff --git a/gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90 b/gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90 index 01213fd..ecb5cbb 100644 --- a/gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90 +++ b/gcc/testsuite/gfortran.dg/actual_pointer_function_1.f90 @@ -17,7 +17,11 @@ CONTAINS logical function cp_logger_log(logger) TYPE(cp_logger_type), POINTER ::logger - cp_logger_log = associated (logger) .and. (logger%a .eq. 42) + if (associated (logger)) then + cp_logger_log = (logger%a .eq. 42) + else + cp_logger_log = .false. + end if END function FUNCTION cp_get_default_logger(v) RESULT(res) diff --git a/gcc/testsuite/gfortran.dg/inline_matmul_23.f90 b/gcc/testsuite/gfortran.dg/inline_matmul_23.f90 index 05633bc..bb7e868 100644 --- a/gcc/testsuite/gfortran.dg/inline_matmul_23.f90 +++ b/gcc/testsuite/gfortran.dg/inline_matmul_23.f90 @@ -1,5 +1,5 @@ ! { dg-do compile } -! { dg-options "-Og -fcheck=bounds -fdump-tree-optimized" } +! { dg-options "-Og -ffrontend-optimize -fcheck=bounds -fdump-tree-optimized" } ! Check that bounds checking is done only before the matrix ! multiplication. diff --git a/gcc/testsuite/gfortran.dg/short_circuiting_2.f90 b/gcc/testsuite/gfortran.dg/short_circuiting_2.f90 new file mode 100644 index 0000000..765c8e7 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/short_circuiting_2.f90 @@ -0,0 +1,28 @@ +! { dg-do run } +! { dg-options "-O0" } +! +! PR 57160: short-circuit IF only with -ffrontend-optimize +! +! this checks that short-circuiting is not done with -O0 +! +! Contributed by Janus Weil + +program short_circuit + + integer, save :: i = 0 + logical :: flag + + flag = .false. + flag = check() .and. flag + flag = flag .and. check() + + if (i /= 2) stop 1 + +contains + + logical function check() + i = i + 1 + check = .true. + end function + +end diff --git a/gcc/testsuite/gfortran.dg/short_circuiting_3.f90 b/gcc/testsuite/gfortran.dg/short_circuiting_3.f90 new file mode 100644 index 0000000..069f3f8 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/short_circuiting_3.f90 @@ -0,0 +1,28 @@ +! { dg-do run } +! { dg-options "-O3" } +! +! PR 57160: short-circuit IF only with -ffrontend-optimize +! +! this checks that short-circuiting is done with -O3 +! +! Contributed by Janus Weil + +program short_circuit + + integer, save :: i = 0 + logical :: flag + + flag = .false. + flag = check() .and. flag + flag = flag .and. check() + + if (i /= 1) stop 1 + +contains + + logical function check() + i = i + 1 + check = .true. + end function + +end -- 2.7.4