/* Forward declarations. */
static void strip_function_call (gfc_expr *);
+static void optimize_namespace (gfc_namespace *);
static void optimize_assignment (gfc_code *);
static void optimize_expr_0 (gfc_expr *);
static bool optimize_expr (gfc_expr *);
optimization pass is run. */
void
-gfc_run_passes (gfc_namespace * ns)
+gfc_run_passes (gfc_namespace *ns)
{
if (optimize)
- optimize_code (ns->code);
+ optimize_namespace (ns);
+}
+
+/* Optimize a namespace, including all contained namespaces. */
+
+static void
+optimize_namespace (gfc_namespace *ns)
+{
+ optimize_code (ns->code);
+
+ for (ns = ns->contained; ns; ns = ns->sibling)
+ optimize_namespace (ns);
}
static void
--- /dev/null
+! { dg-do run }
+! { dg-options "-O -fdump-tree-original" }
+! Optimize unnecessary TRIMs in contained namespaces too.
+module faz
+ implicit none
+contains
+ subroutine bar
+ character(len=3) :: a
+ character(len=4) :: b,c
+ b = 'abcd'
+ a = trim(b)
+ c = trim(trim(a))
+ if (a /= 'abc') call abort
+ if (c /= 'abc') call abort
+ end subroutine bar
+end module faz
+
+program main
+ use faz
+ implicit none
+ call foo
+ call bar
+contains
+ subroutine foo
+ character(len=3) :: a
+ character(len=4) :: b,c
+ b = 'abcd'
+ a = trim(b)
+ c = trim(trim(a))
+ if (a /= 'abc') call abort
+ if (c /= 'abc') call abort
+ end subroutine foo
+end program main
+
+! { dg-final { scan-tree-dump-times "memmove" 4 "original" } }
+! { dg-final { scan-tree-dump-times "string_trim" 0 "original" } }
+! { dg-final { cleanup-tree-dump "original" } }