re PR fortran/52861 ((missed optimisation) missed transformation to memset with -O3)
authorThomas Koenig <tkoenig@gcc.gnu.org>
Thu, 7 Jun 2012 14:33:51 +0000 (14:33 +0000)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Thu, 7 Jun 2012 14:33:51 +0000 (14:33 +0000)
2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>

PR fortran/52861
* frontend-passes.c (optimize_assignment):  Don't set the
length of an empty string for deferred-length character
variables.

2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>

PR fortran/52861
* gfortran.dg/string_assign_2.f90:  New test case.

From-SVN: r188305

gcc/fortran/ChangeLog
gcc/fortran/frontend-passes.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/string_assign_2.f90 [new file with mode: 0644]

index c882a416a9f713f002934244daa36bb0703e0eea..e51d5071d8343ff3e58139d99ef6433dc8690659 100644 (file)
@@ -1,7 +1,14 @@
 2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>
 
        PR fortran/52861
-       * frontend-passes (empty_string):  Add prototype.
+       * frontend-passes.c (optimize_assignment):  Don't set the
+       length of an empty string for deferred-length character
+       variables.
+
+2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>
+
+       PR fortran/52861
+       * frontend-passes.c (empty_string):  Add prototype.
        (optimize_assignment):  Set the length of an empty string
        constant to zero.
 
index 4fd24c2947e44a694437f0437524b830f3c1d049..bcc1bdc323b9b095e1b1669e09fe2803a8e92345 100644 (file)
@@ -740,8 +740,10 @@ optimize_assignment (gfc_code * c)
       /* Optimize away a = trim(b), where a is a character variable.  */
       remove_trim (rhs);
 
-      /* Replace a = '   ' by a = '' to optimize away a memcpy.  */
-      if (empty_string(rhs))
+      /* Replace a = '   ' by a = '' to optimize away a memcpy, but only
+        for strings with non-deferred length (otherwise we would
+        reallocate the length.  */
+      if (empty_string(rhs) && ! lhs->ts.deferred)
        rhs->value.character.length = 0;
     }
 
index 5819c0067c40d79d71d70a2afbc454de83c23306..776012c569b8a5e51e013dc3a652fc8a5574e4d6 100644 (file)
@@ -1,3 +1,8 @@
+2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>
+
+       PR fortran/52861
+       * gfortran.dg/string_assign_2.f90:  New test case.
+
 2012-06-07  Thomas König  <tkoenig@gcc.gnu.org>
 
        PR fortran/52861
diff --git a/gcc/testsuite/gfortran.dg/string_assign_2.f90 b/gcc/testsuite/gfortran.dg/string_assign_2.f90
new file mode 100644 (file)
index 0000000..f3cfa45
--- /dev/null
@@ -0,0 +1,9 @@
+! { dg-do run }
+! { dg-options "-ffrontend-optimize" }
+program main
+  character (len=:), allocatable :: a
+  a = 'a'
+  if (len(a) /= 1) call abort
+  a = '  '
+  if (len(a) /= 2) call abort
+end program main