Bug 56342 - MATMUL with PARAMETER: Simplification usually doesn't work
Summary: MATMUL with PARAMETER: Simplification usually doesn't work
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords: missed-optimization
Depends on: 56318
Blocks:
  Show dependency treegraph
 
Reported: 2013-02-15 14:27 UTC by Tobias Burnus
Modified: 2017-10-27 17:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-01-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-02-15 14:27:42 UTC
This is a follow up to PR 56318, reported by Alberto Luaces at http://gcc.gnu.org/ml/fortran/2013-02/msg00074.html

The wrong-code issue has been fixed, however, it was noted there that MATMUL with parameters is not get optimized.


Simplified test case:
!------------------------------------
integer, parameter :: A(3,2) = reshape([1,2,3,4,5,6],[3,2])
integer, parameter :: B(2,3) = reshape([1,1,1,1,1,1],[2,3])
integer, parameter :: m1 = 1

print '(3i3)',    matmul(A,B) ! Does not get optimized
print '(3i3)', m1*matmul(A,B) ! Is optimized
end
!------------------------------------

(See the other PR for the original test case - and gfortran.dg/matmul_9.f90; they all use the multiplication - which lead to simplification and hit a now fixed wrong-code bug.)



The "m1*" is crucial. When gfc_simplify_matmul is called initially, it returns
NULL as  "is_constant_array_expr (matrix_a)" is false.

The "m1*" causes a re-evaluation of the RHS expression, namely in
gfc_simplify_expr:
      for (ap = p->value.function.actual; ap; ap = ap->next)
        if (gfc_simplify_expr (ap->expr, type) == FAILURE)

That converts an EXPR_VARIABLE with flavor FL_PARAMETER into an EXPR_ARRAY,
which can then be processed by calling gfc_simplify_matmul.



It seems as if one has to run "gfc_simplify_expr" – the question is whether that shouldn't then also be done for other intrinsics?
Comment 1 Tobias Burnus 2013-07-08 12:40:34 UTC
Another example: http://gcc.gnu.org/ml/fortran/2013-07/msg00005.html - Here, the SUM is not simplified.
Comment 2 Dominique d'Humieres 2014-01-07 10:38:07 UTC
Still true at r206385.
Comment 3 Thomas Koenig 2017-10-22 17:11:21 UTC
Yes, simplification is the key.
Comment 4 Thomas Koenig 2017-10-27 17:12:13 UTC
Author: tkoenig
Date: Fri Oct 27 17:11:42 2017
New Revision: 254157

URL: https://gcc.gnu.org/viewcvs?rev=254157&root=gcc&view=rev
Log:
2017-10-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/56342
	* simplify.c (is_constant_array_expr): If the expression is
	a parameter array, call gfc_simplify_expr.

2017-10-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/56342
	* gfortran.dg/matmul_const.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/matmul_const.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Thomas Koenig 2017-10-27 17:16:16 UTC
Fixed on trunk, closing.