This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran] PR 15273 pure subroutines get optimized away

This is a minimal fix for PR15273. The problem was that setting DECL_IS_PURE for a procedure means that calls can be optimized away if the procedure's return value is not used. A subroutine has no return value (unless alternate returns are used), therefor marking it with DECL_IS_PURE ensures that the subroutine is never called, which is clearly not the intention. Hence this minimal fix, it's only two lines, and I believe it's not copyrightable.

I'm not sure if TREE_SIDE_EFFECTS should be set to zero in these cases or not, it didn't seem to have adverse effects that it is set in the second hunk, so I set it in the first hunk as well.

This likely brings a slight pessimation for subroutines which are marked pure, compared to a real implementation of that attribute, but it's better than our current state.

To answer the question asked in the original TODO: a FUNCTION can't, a SUBROUTINE can. Unless the arguments are POINTERs. See Section 12.6 of the Fortran 95 standard.

Tested on i686-pc-linux with no new testsuite failures.

- Tobi

2004-05-04  Tobias Schlüter <>
	* trans-decl.c (gfc_get_extern_function_decl): Set DECL_IS_PURE
	only for functions.
	(gfc_build_function_decl): Likewise.

Index: trans-decl.c
RCS file: /cvsroot/gcc/gcc/gcc/fortran/Attic/trans-decl.c,v
retrieving revision
diff -u -p -r1.1.2.37 trans-decl.c
--- trans-decl.c 24 Apr 2004 12:19:27 -0000
+++ trans-decl.c 4 May 2004 19:29:13 -0000
@@ -952,9 +952,13 @@ gfc_get_extern_function_decl (gfc_symbol
sense. */
if (sym->attr.pure || sym->attr.elemental)
- DECL_IS_PURE (fndecl) = 1;
-/* TODO: check if pure/elemental procedures can have INTENT(OUT) parameters.
- TREE_SIDE_EFFECTS (fndecl) = 0;*/
+ if (sym->attr.function)
+ DECL_IS_PURE (fndecl) = 1;
+ /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
+ parameters and don't use alternate returns (is this
+ allowed?). In that case, calls to them are meaningless, and
+ can be optimized away. See also in gfc_build_function_decl(). */
+ TREE_SIDE_EFFECTS (fndecl) = 0;

   sym->backend_decl = fndecl;
@@ -1061,7 +1065,11 @@ gfc_build_function_decl (gfc_symbol * sy
      sense.  */
   if (attr.pure || attr.elemental)
-      DECL_IS_PURE (fndecl) = 1;
+      /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
+        including a alternate return. In that case it can also be
+        marked as PURE. See also in gfc_get_extern_fucntion_decl().  */
+      if (attr.function)
+       DECL_IS_PURE (fndecl) = 1;
       TREE_SIDE_EFFECTS (fndecl) = 0;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]