Bug 45636 - Failed to fold simple Fortran string
Summary: Failed to fold simple Fortran string
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2010-09-10 14:48 UTC by H.J. Lu
Modified: 2010-11-24 00:26 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-09-27 06:16:20


Attachments
patch for comment#5 (921 bytes, patch)
2010-09-26 22:20 UTC, Thomas Koenig
Details | Diff
gcc46-pr45636.patch (3.71 KB, patch)
2010-10-07 16:34 UTC, Jakub Jelinek
Details | Diff
Tree dump. (297 bytes, text/plain)
2010-10-16 18:58 UTC, John David Anglin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2010-09-10 14:48:16 UTC
For this simple Fortran string:

[hjl@gnu-6 pr45634]$ cat pr45634.f90 
      SUBROUTINE RCRDRD (VTYP)
      CHARACTER(4), INTENT(OUT) :: VTYP 
      CHARACTER(1), SAVE :: DBL = "D" 
      VTYP = DBL
      END

GCC generates:

[hjl@gnu-6 pr45634]$ cat pr45634.s 
	.file	"pr45634.f90"
	.text
	.p2align 4,,15
	.globl	rcrdrd_
	.type	rcrdrd_, @function
rcrdrd_:
.LFB0:
	.cfi_startproc
	movzbl	dbl.1557(%rip), %eax
	movw	$8224, 1(%rdi)
	movb	$32, 3(%rdi)
	movb	%al, (%rdi)
	ret
	.cfi_endproc
.LFE0:
	.size	rcrdrd_, .-rcrdrd_
	.section	.rodata
	.type	dbl.1557, @object
	.size	dbl.1557, 1
dbl.1557:
	.ascii	"D"
	.ident	"GCC: (GNU) 4.6.0 20100910 (experimental)"
	.section	.note.GNU-stack,"",@progbits

IFORT generates:

[hjl@gnu-6 pr45634]$ cat icc.s
# -- Machine type EFI2
# mark_description "Intel(R) Fortran Compiler XE for applications running on Intel(R) 64, Version 12.0.0 Beta Build 20100512";
# mark_description "-O3 -S";
	.file "pr45634.f90"
	.text
..TXTST0:
# -- Begin  rcrdrd_
# mark_begin;
       .align    16,0x90
	.globl rcrdrd_
rcrdrd_:
# parameter 1: %rdi
# parameter 2: %rsi
..B1.1:                         # Preds ..B1.0
..___tag_value_rcrdrd_.1:                                       #1.18
        movl      $538976324, (%rdi)                            #4.7
        ret                                                     #5.7
Comment 1 kargls 2010-09-10 15:12:09 UTC
I have a slightly different result with your code.

troutmask:sgk[212] gfc4x -c -O g.f90
g.f90: In function 'rcrdrd':
g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.

If I replace the SAVE attribute by PARAMETER (which will most
likely cause the folding you want), everything compiles fine.
The ICE also goes away if I increase the length of DBL from
one to any larger value.

troutmask:sgk[219] gfc4x -v
Using built-in specs.
COLLECT_GCC=/home/sgk/work/4x/bin/gfortran
COLLECT_LTO_WRAPPER=/usr/home/sgk/work/4x/bin/../libexec/gcc/x86_64-unknown-freebsd9.0/4.6.0/lto-wrapper
Target: x86_64-unknown-freebsd9.0
Configured with: ../gcc4x/configure --prefix=/home/sgk/work/4x --enable-languages=c,fortran --disable-libmudflap --disable-bootstrap --with-quad=/home/sgk/work
Thread model: posix
gcc version 4.6.0 20100909 (experimental) (GCC) 


Comment 2 kargls 2010-09-10 15:20:42 UTC
The -fdump-tree-original for HJ's original code look like

rcrdrd (character(kind=1)[1:4] & restrict vtyp, integer(kind=4) _vtyp)
{
  static character(kind=1) dbl[1:1] = "D";

  (MEM[(c_char * {ref-all})vtyp] = MEM[(c_char * {ref-all})&dbl];, (void *) vtyp;);
  __builtin_memset ((void *) vtyp + 1, 32, 3);
}

If I increase the length of DBL to 2, then the dump looks like

rcrdrd (character(kind=1)[1:4] & restrict vtyp, integer(kind=4) _vtyp)
{
  static character(kind=1) dbl[1:2] = "D ";

  __builtin_memmove ((void *) vtyp, (void *) &dbl, 2);
  __builtin_memset ((void *) vtyp + 2, 32, 2);
}

Comment 3 H.J. Lu 2010-09-10 15:32:47 UTC
(In reply to comment #1)
> I have a slightly different result with your code.
> 
> troutmask:sgk[212] gfc4x -c -O g.f90
> g.f90: In function 'rcrdrd':
> g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218
> Please submit a full bug report,
> with preprocessed source if appropriate.
> See <http://gcc.gnu.org/bugs.html> for instructions.
> 
>

It is fixed by

http://gcc.gnu.org/ml/gcc-cvs/2010-09/msg00475.html
Comment 4 kargls 2010-09-10 15:34:37 UTC
(In reply to comment #3)
> (In reply to comment #1)
> > I have a slightly different result with your code.
> > 
> > troutmask:sgk[212] gfc4x -c -O g.f90
> > g.f90: In function 'rcrdrd':
> > g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218
> > Please submit a full bug report,
> > with preprocessed source if appropriate.
> > See <http://gcc.gnu.org/bugs.html> for instructions.
> > 
> >
> 
> It is fixed by
> 
> http://gcc.gnu.org/ml/gcc-cvs/2010-09/msg00475.html
> 

HJ,

Thanks.  I updated by trunk right before your patch,
so I missed it my most recent build.


Comment 5 Jakub Jelinek 2010-09-10 15:47:30 UTC
For arbitrary lengths (both of the constant string and of the padding) the memmove (which will be optimized to memcpy as the source is read-only) + memset is the best thing to do, replacing say
memmove (x, "900 bytes long string", 900);
memset (x + 900, ' ', 100);
would be very .rodata size unfriendly.
So, the question is, do we want to optimize this for very small sizes of both (what sizes?  Should we call can_store_by_pieces to determine that from the FE?)
in the FE by transforming that say
memmove (x, "ABCDE", 5);
memset (x + 5, ' ', 3);
into
memcpy (x, "ABCDE   ", 8);
or should we do this generically in the middle-end, where we'd do this transformation for such cases even for other languages?
Comment 6 Thomas Koenig 2010-09-26 22:20:23 UTC
Created attachment 21891 [details]
patch for comment#5

This patch fixes the issue in comment #5 in the Fortran front end.

The constant for padding is arbitrary, I don't know a better way to set it.
Comment 7 Thomas Koenig 2010-09-27 06:16:20 UTC
(In reply to comment #6)
> Created attachment 21891 [details]
> patch for comment#5
> 
> This patch fixes the issue in comment #5 in the Fortran front end.

... but also causes regressions.  I'll investigate.

Confirmed, BTW.
Comment 8 Jakub Jelinek 2010-10-07 16:34:17 UTC
Created attachment 21991 [details]
gcc46-pr45636.patch

So far untested patch to optimize memcpy + memset in forwprop.
Works on the attached testcase, but ICEs on:
! { dg-do compile }
! { dg-options "-O2 -fdump-tree-forwprop2" }
! PR 45636 - make sure no memset is needed for a short right-hand side.
program main
  character(len=2), parameter :: x='a '
  character(len=4) :: a
  a = x
  call sub(a)
end program main
! { dg-final { scan-tree-dump-times "memset" 0 "forwprop2" } }
! { dg-final { cleanup-tree-dump "forwprop2" } }

I'm afraid I'm missing somewhere some magic call to update vops after the call removal.  And, it unfortunately doesn't handle the case where the first memcpy is of size 1 (or memset is of size 1), because then folding changes the calls into assignments.
Comment 9 Jakub Jelinek 2010-10-12 22:01:07 UTC
Author: jakub
Date: Tue Oct 12 22:01:04 2010
New Revision: 165401

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=165401
Log:
	PR fortran/45636
	* tree-ssa-forwprop.c: Include expr.h.
	(constant_pointer_difference, simplify_builtin_call): New functions.
	(tree_ssa_forward_propagate_single_use_vars): Call
	simplify_builtin_call on builtin calls.

	* gcc.c-torture/execute/pr45636.c: New test.
	* gfortran.dg/pr45636.f90: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr45636.c
    trunk/gcc/testsuite/gfortran.dg/pr45636.f90
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/Makefile.in
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-forwprop.c
Comment 10 John David Anglin 2010-10-16 18:58:57 UTC
Created attachment 22064 [details]
Tree dump.

gfortran.dg/pr45636.f90 fails on hppa-unknown-linux-gnu.  Tree dump
attached.
Comment 11 dave 2010-10-17 15:41:54 UTC
In addition, although gcc.c-torture/execute/pr45636.c doesn't fail on
hppa-unknown-linux-gnu, it does fail on hpux:

Executing on host: /test/gnu/gcc/objdir/gcc/xgcc -B/test/gnu/gcc/objdir/gcc/ /test/gnu/gcc/gcc/gcc/testsuite/gcc.c-torture/execute/pr45636.c  -w  -O0   -lm   -o /test/gnu/gcc/objdir/gcc/testsuite/gcc/pr45636.x0    (timeout = 300)
/usr/ccs/bin/ld: Unsatisfied symbols:
   mempcpy (first referenced in /var/tmp//ccT7jpSk.o) (code)

Looking at the hppa-linux .s, there are still many calls to various 'mem*'
functions.  I don't see any on x86.

Dave
Comment 12 Rainer Orth 2010-10-28 17:54:17 UTC
gcc.c-torture/execute/pr45636.c also fails on Solaris 2/SPARC:

FAIL: gcc.c-torture/execute/pr45636.c compilation,  -Os 
UNRESOLVED: gcc.c-torture/execute/pr45636.c execution,  -Os 

Undefined			first referenced

 symbol  			    in file

mempcpy                             /var/tmp//ccJduPWN.o

ld: fatal: Symbol referencing errors. No output written to /var/gcc/regression/trunk/10-gcc-gas/build/gcc/testsuite/gcc2/pr45636.x7

collect2: ld returned 1 exit status
Comment 13 Steve Ellcey 2010-11-05 21:12:19 UTC
I have moved gcc.c-torture/execute/pr45636.c to gcc.dg/torture/pr45636.c
and added a check for the mempcpy function so this should not fail on HP-UX or Solaris which don't have a mempcpy function.

The change was r166378 and if the test failures are the only reason to keep this bug report open then it we should be able to close it now.
Comment 14 postmaster 2010-11-06 01:17:46 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/7/2010 4:12:41 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 15 postmaster 2010-11-06 05:26:11 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/7/2010 8:18:00 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 16 postmaster 2010-11-06 09:34:34 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 12:26:28 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 17 postmaster 2010-11-06 13:42:59 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 4:35:13 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 18 postmaster 2010-11-06 17:51:23 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 8:43:46 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 19 postmaster 2010-11-06 21:59:48 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 12:52:18 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 20 postmaster 2010-11-07 02:08:13 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 5:00:28 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 21 postmaster 2010-11-07 02:49:40 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 5:46:34 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 22 postmaster 2010-11-07 07:16:24 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 9:50:00 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 23 postmaster 2010-11-07 07:16:25 UTC
Delivery is delayed to these recipients or distribution lists:

fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca>

Subject: [Bug fortran/45636] Failed to fold simple Fortran string

This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf.

Delivery of this message will be attempted until 11/8/2010 9:08:26 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time.

________________________________
Sent by Microsoft Exchange Server 2007
Comment 24 Tobias Burnus 2010-11-07 07:46:24 UTC
remove Danglin to avoid mail spamage
Comment 25 dave 2010-11-07 19:44:07 UTC
> The change was r166378 and if the test failures are the only reason to keep
> this bug report open then it we should be able to close it now.

Closing would be ok if there is a reason the call to mempcpy shouldn't
be eliminated.  On darwin, it was eliminated.

Dave
Comment 26 Steve Ellcey 2010-11-08 16:08:19 UTC
The mempcpy is not inlined with -Os.  Presumbably because that would increase the size of the resulting object.
Comment 27 John David Anglin 2010-11-23 23:45:32 UTC
(In reply to comment #26)
> The mempcpy is not inlined with -Os.  Presumbably because that would increase
> the size of the resulting object.

Possibly, but based on Rainer's comment, the issue would appear to
affect targets with strict alignment.

Sorry for the mail spamage.  It was caused by a hardware failure at NRC.
Comment 28 John David Anglin 2010-11-24 00:26:15 UTC
Sorry, I didn't intend to reopen.  The web interface seems to
unexpected change fields sometimes.