Patchwork [Fortran] PR 50960 - Mark PARAMETER as TREE_READONLY

login
register
mail settings
Submitter Tobias Burnus
Date Nov. 3, 2011, 10:06 p.m.
Message ID <4EB31057.90307@net-b.de>
Download mbox | patch
Permalink /patch/123515/
State New
Headers show

Comments

Tobias Burnus - Nov. 3, 2011, 10:06 p.m.
Dear all,

gfortran stores PARAMETERS which are array or derived types as global 
static variable. This prevents the compiler from optimizing those 
parameters.

Using TREE_READONLY, one can allow the compiler to make use of the 
values and do further optimizations.

Build and regtested on x86_64-linux.
OK for the trunk?

Tobias
Steve Kargl - Nov. 3, 2011, 10:26 p.m.
On Thu, Nov 03, 2011 at 11:06:15PM +0100, Tobias Burnus wrote:
> Dear all,
> 
> gfortran stores PARAMETERS which are array or derived types as global 
> static variable. This prevents the compiler from optimizing those 
> parameters.
> 
> Using TREE_READONLY, one can allow the compiler to make use of the 
> values and do further optimizations.
> 
> Build and regtested on x86_64-linux.
> OK for the trunk?

OK.

Patch

2011-11-03  Tobias Burnus  <burnus@net-b.de>

	PR fortran/50960
	* trans-decl.c (gfc_finish_var_decl): Mark PARAMETER as TREE_READONLY.

2011-11-03  Tobias Burnus  <burnus@net-b.de>

	PR fortran/50960
	* gfortran.dg/module_parameter_array_refs_2.f90: New.

diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index b7460b7..b90b0ab 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -517,6 +517,10 @@  gfc_finish_var_decl (tree decl, gfc_symbol * sym)
   /* If it wasn't used we wouldn't be getting it.  */
   TREE_USED (decl) = 1;
 
+  if (sym->attr.flavor == FL_PARAMETER
+      && (sym->attr.dimension || sym->ts.type == BT_DERIVED))
+    TREE_READONLY (decl) = 1;
+
   /* Chain this decl to the pending declarations.  Don't do pushdecl()
      because this would add them to the current scope rather than the
      function scope.  */
--- /dev/null	2011-11-01 08:51:46.775606868 +0100
+++ gcc/gcc/testsuite/gfortran.dg/module_parameter_array_refs_2.f90	2011-11-03 21:59:20.000000000 +0100
@@ -0,0 +1,23 @@ 
+! { dg-do compile }
+! { dg-options "-O" }
+! { dg-final { scan-assembler-not "i_am_optimized_away" } }
+!
+! PR fortran/50960
+!
+! PARAMETER arrays and derived types exists as static variables.
+! Check that the their read-only nature is taken into account
+! when optimizations are done.
+!
+
+module m
+  integer, parameter :: PARA(*) = [1,2,3,4,5,6,7,8,9,10]
+end module m
+
+subroutine test()
+use m
+integer :: i
+i = 1
+if (para(i) /= 1) call i_am_optimized_away()
+end
+
+! { dg-final { cleanup-modules "m" } }