Bug 53642

Summary: Front-end optimization: Wrong string length for deferred-length strings
Product: gcc Reporter: Tobias Burnus <burnus>
Component: fortranAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: burnus, damian, tkoenig
Priority: P3 Keywords: wrong-code
Version: 4.8.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Tobias Burnus 2012-06-12 08:08:54 UTC
As reported by Damian Rouson at http://gcc.gnu.org/ml/fortran/2012-06/msg00069.html

Without optimization, the following program prints - and should print:
           3           3

However, with -ffrontend-optimize the result is, wrongly,
           3           4

Damian: Use -fno-frontend-optimize as work-around.

character(len=4) :: string="123 "
character(:), allocatable :: trimmed
trimmed = trim(string)
print *,len_trim(string),len(trimmed)
end


From the original dump:

(a) Without FE optimization
        _gfortran_string_trim (&len.1, (void * *) &pstr.0, 4, &string);
        D.1864 = len.1;
        if (trimmed != 0B) goto L.1;
        trimmed = (character(kind=1)[1:.trimmed] *)
                       __builtin_malloc ((sizetype) len.1);
        ...
        .trimmed = len.1;

(b) With FE optimization
        if (trimmed != 0B) goto L.1;
        trimmed = (character(kind=1)[1:.trimmed] *) __builtin_malloc (4);
        ...
        .trimmed = 4;
Comment 1 Tobias Burnus 2012-06-12 15:52:34 UTC
I was thinking about the following patch - namely, doing in optimize_assignment the remove_trim only if "!lhs->ts.deferred".

 * * *

However, that does not work; seemingly,  a = trim(b) is replaced by
   a = b(1:len_trim(b))  in such a way, that  len(a) == 0  instead of 3.

The same issue occurs for a manual:
   trimmed = string(1:len_trim(string))

Thus, the follow-up issue is not a FE optimization issue but rather a trans*.c issue. The generated code is [some casting removed]:

        trimmed = __builtin_malloc (MAX_EXPR <NON_LVALUE_EXPR <D.1861>, 0>);

        .trimmed = MAX_EXPR <NON_LVALUE_EXPR <D.1861>, 0>;

        D.1861 = _gfortran_string_len_trim (4, &string);

Which shows the wrong ordering, similar to bug 45170 comment 34, which is fixed by the patch at bug 45170 comment 34. That patch also solves the len_trim issue above.


--- a/gcc/fortran/frontend-passes.c
+++ b/gcc/fortran/frontend-passes.c
@@ -738 +738 @@ optimize_assignment (gfc_code * c)
-  if (lhs->ts.type == BT_CHARACTER)
+  if (lhs->ts.type == BT_CHARACTER && !lhs->ts.deferred)
@@ -740 +740 @@ optimize_assignment (gfc_code * c)
-      /* Optimize away a = trim(b), where a is a character variable.  */
+      /* Optimize  a = trim(b)  to  a = b.  */
@@ -743,4 +743,2 @@ optimize_assignment (gfc_code * c)
-      /* 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)
+      /* Replace a = '   ' by a = '' to optimize away a memcpy.  */
+      if (empty_string(rhs))
@@ -1174 +1172 @@ optimize_trim (gfc_expr *e)
-  /* Build the function call to len_trim(x, gfc_defaul_integer_kind).  */
+  /* Build the function call to len_trim(x, gfc_default_integer_kind).  */
Comment 2 Tobias Burnus 2012-06-16 18:13:43 UTC
Author: burnus
Date: Sat Jun 16 18:13:38 2012
New Revision: 188692

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=188692
Log:
2012-06-16  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53642
        PR fortran/45170
        * frontend-passes.c (optimize_assignment): Don't remove RHS's
        trim when assigning to a deferred-length string.
        * trans-expr.c (gfc_trans_assignment_1): Ensure that the RHS string
        length is evaluated before the deferred-length LHS is reallocated.

2012-06-16  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53642
        PR fortran/45170
        * gfortran.dg/deferred_type_param_8.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/deferred_type_param_8.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/frontend-passes.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Tobias Burnus 2012-06-16 18:16:45 UTC
FIXED on the trunk (4.8).

Damian: Thanks for the bug report!