]> git.ipfire.org Git - thirdparty/gcc.git/commit
fortran: Factor the evaluation of MINLOC/MAXLOC's BACK argument
authorMikael Morin <morin-mikael@orange.fr>
Thu, 11 Jul 2024 19:55:58 +0000 (21:55 +0200)
committerMikael Morin <mikael@gcc.gnu.org>
Fri, 12 Jul 2024 08:28:15 +0000 (10:28 +0200)
commita55d24b3cf7f4d07492bb8e6fcee557175b47ea3
tree8824085b1ed930f76e7026155b17f77894fbf1e4
parent63d7d5998e3768f6e3703c29e8774e8b54af108c
fortran: Factor the evaluation of MINLOC/MAXLOC's BACK argument

Move the evaluation of the BACK argument out of the loop in the inline code
generated for MINLOC or MAXLOC.  For that, add a new (scalar) element
associated with BACK to the scalarization loop chain, evaluate the argument
with the context of that element, and let the scalarizer do its job.

The problem was not only a missed optimisation, but also a wrong code
one in the cases where the expression associated with BACK is not free of
side-effects, making multiple evaluations observable.

The new tests check the evaluation count of the BACK argument, and try to
cover all the variations (integral or floating-point type, constant or
unknown shape, absent or scalar or array MASK) supported by the inline
implementation of the functions.  Care has been taken to not check the case
of a constant .FALSE. MASK, for which the evaluation of BACK can be elided.

gcc/fortran/ChangeLog:

* trans-intrinsic.cc (gfc_conv_intrinsic_minmaxloc): Create a new
scalar scalarization chain element if BACK is present.  Add it to
the loop.  Set the scalarization chain before evaluating the
argument.

gcc/testsuite/ChangeLog:

* gfortran.dg/maxloc_5.f90: New test.
* gfortran.dg/minloc_5.f90: New test.
gcc/fortran/trans-intrinsic.cc
gcc/testsuite/gfortran.dg/maxloc_5.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/minloc_5.f90 [new file with mode: 0644]