public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-5639] Fortran: improve expansion of constant array expressions within constructors
@ 2021-11-30 21:10 Harald Anlauf
  0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2021-11-30 21:10 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6b8ecbc6d6652d061d7c72c64352d51eca2df6ca

commit r12-5639-g6b8ecbc6d6652d061d7c72c64352d51eca2df6ca
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sat Nov 27 21:43:52 2021 +0100

    Fortran: improve expansion of constant array expressions within constructors
    
    gcc/fortran/ChangeLog:
    
            PR fortran/102787
            * array.c (expand_constructor): When encountering a constant array
            expression or array section within a constructor, simplify it to
            enable better expansion.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/array_constructor_54.f90: New test.

Diff:
---
 gcc/fortran/array.c                                |  6 ++++++
 gcc/testsuite/gfortran.dg/array_constructor_54.f90 | 23 ++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c
index 6552eaf3b0c..fbc66097c80 100644
--- a/gcc/fortran/array.c
+++ b/gcc/fortran/array.c
@@ -1804,6 +1804,12 @@ expand_constructor (gfc_constructor_base base)
       if (empty_constructor)
 	empty_ts = e->ts;
 
+      /* Simplify constant array expression/section within constructor.  */
+      if (e->expr_type == EXPR_VARIABLE && e->rank > 0 && e->ref
+	  && e->symtree && e->symtree->n.sym
+	  && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
+	gfc_simplify_expr (e, 0);
+
       if (e->expr_type == EXPR_ARRAY)
 	{
 	  if (!expand_constructor (e->value.constructor))
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_54.f90 b/gcc/testsuite/gfortran.dg/array_constructor_54.f90
new file mode 100644
index 00000000000..44d2f9fdf42
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_constructor_54.f90
@@ -0,0 +1,23 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original -Warray-temporaries" }
+! { dg-final { scan-tree-dump-not "stride" "original" } }
+! Verify that no temporary array is generated for a constant array constructor
+! See e.g. PR fortran/102717, PR fortran/102787
+
+program p
+  integer, parameter :: a(*)   = [1,2,3,4]
+  integer, parameter :: b(2,3) = reshape([1,2,3,4,5,6], shape (b))
+  print *, [a]
+  print *, [a( : ) ]
+  print *, [a( ::1)]
+  print *, [a( ::2)]
+  print *, [a(1:2:1)]
+  print *, [a(4:1:-2)]
+  print *, [a([3,2])]
+  print *, [a,1]
+  print *, [1,a]
+  print *, [a,a]
+  print *, [b(:,3:1:-2)]
+  print *, [1,b(1,[2,1,3])]
+  print *, [a,b]
+end


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-11-30 21:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 21:10 [gcc r12-5639] Fortran: improve expansion of constant array expressions within constructors Harald Anlauf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).