Bug 12456 - please recognize Fortran STRING(K:K) as single character
please recognize Fortran STRING(K:K) as single character
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
3.2
: P3 enhancement
: 4.1.0
Assigned To: fengwang
: missed-optimization
Depends on:
Blocks: 19276 13615
  Show dependency treegraph
 
Reported: 2003-09-30 08:46 UTC by arobb@mva.co.uk
Modified: 2006-01-09 04:25 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-01-02 07:16:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description arobb@mva.co.uk 2003-09-30 08:46:11 UTC
	I would like A(I:I) to be recognized as being a single
character.
	
	The statement:
	A(I:I)=B(J:J)
	is expanded to a full _s_cpy call for each character,
whereas:
	A(1:1)=B(2:2)
	is recognised as a single character assignment.
	Currently I overcome the problem with a call to:
	
	SUBROUTINE CHRCPY(A,B)
	CHARACTER A,B
	A=B
	END
	...
	CALL CHRCPY(A(I:I),B(J:J))
	
	At -O3 this gets inlined as a single character assignment,
	which is what I want. This is not intuitive and only works
within
	a program unit.
	
	Similarly:
	A(I:I).EQ.B(J:J)
	gets expanded to a call to _s_cmp for each character.
	This can be overcome with:
	ICHAR(A(I:I)).EQ.ICHAR(B(J:J))

Environment:
System: SunOS daedelus 5.9 Generic_112233-05 sun4u sparc
SUNW,Ultra-Enterprise
Architecture: sun4

	cygwin and Solaris
host: sparc-sun-solaris2.9
build: sparc-sun-solaris2.9
target: sparc-sun-solaris2.9
configured with: ../configure --with-as=/usr/ccs/bin/as
--with-ld=/usr/ccs/bin/ld --disable-nls

How-To-Repeat:
	PROGRAM test
	CHARACTER*26 A,B
	DATA A/'abcdefghijklmnopqrstuvwxyz'/
	DO I=1,26
 	  B(I:I)=CHAR(ICHAR(A(I:I))+ICHAR('A')-ICHAR('a'))
 	END DO
 	END
Comment 1 arobb@mva.co.uk 2003-09-30 08:46:11 UTC
Fix:
	CALL CHRCPY(B(I:I),CHAR(ICHAR(A(I:I)+ICHAR('A')-ICHAR('a')))
	and compile with -finline-functions
	For comparisons use:
	ICHAR(A(I:I)).EQ.ICHAR(B(I:I))

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
Comment 2 bdavis9659 2003-09-30 12:46:50 UTC
Is this a performance issue or a "correctness" issue ? 
Comment 3 arobb@mva.co.uk 2003-09-30 13:19:32 UTC
Subject: Re:  please recognize Fortran STRING(K:K)
	as single

Thanks for getting back to me,
This is a performance issue.
Changing a character in a FORTRAN string currently requires a call to
_s_cpy.
(Whereas changing a character in an array does not.)
I am looking for similar compiled code for:
      CHARACTER A(6)
      DO I=1,6
        A(I)=CHAR(I)
      ENDDO
and:
      CHARACTER A*6
      DO I=1,6
        A(I:I)=CHAR(I)
      ENDDO
Apart from being orders of magnitude slower, this is not a bug.
I tried to log this with the lowest of priorities.
Regards,
Andy Robb.
>>> "bdavis9659 at comcast dot net" <gcc-bugzilla@gcc.gnu.org>
30/09/03 13:46:52 >>>
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT*
gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12456 



------- Additional Comments From bdavis9659 at comcast dot net 
2003-09-30 12:46 -------
Is this a performance issue or a "correctness" issue ? 



------- You are receiving this mail because: -------
You reported the bug, or are watching the reporter.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a
proactive
anti-virus service working around the clock, around the globe,
visit:
http://www.star.net.uk 
________________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
Comment 4 Andrew Pinski 2003-09-30 18:15:39 UTC
This is most likely will not be done before the Fortran 95 compiler comes in, which is scheduled 
for 3.5 (some time in 2004).
Comment 5 Toon Moene 2003-10-07 18:50:35 UTC
... to mark as duplicate of 11522

*** This bug has been marked as a duplicate of 11522 ***
Comment 6 Toon Moene 2003-10-07 18:53:43 UTC
Sorry, operator error.

Toon Moene
Comment 7 arobb@mva.co.uk 2003-10-08 08:33:05 UTC
I recognize that
      A(I:I).EQ.B(J:J)
is a GNU extension that means something like
      A(I:I).LEQ.B(J:J)
and that
      ICHAR(A(I:I)).EQ.ICHAR(B(J:J))
is an ansi way to do it.

This still leaves the problem of A(I:I)='C' calling _s_cpy
Comment 8 Andrew Pinski 2003-10-19 23:11:36 UTC
This also happens on the tree-ssa branch with f95:
          _gfortran_copy_string (1, &b[i], 1, &char.2);
Comment 9 Andrew Pinski 2004-05-04 03:35:38 UTC
The fix needs to check if len1 and len2 are constant and equal to one in 
gfc_conv_statement_function around line 1200 in trans-expr.c.
Comment 10 fengwang 2005-10-25 05:31:11 UTC
This will improve 168.wupwise at lease 1%. And I have partially fixed. Patches are coming soon.
Comment 11 Andrew Pinski 2005-12-25 04:29:10 UTC
This will also improve LAPACK too.

As what shows up there is:
CHARACTER a
LOGICAL b

a = 'a'
Comment 12 fengwang 2006-01-09 02:27:51 UTC
Subject: Bug 12456

Author: fengwang
Date: Mon Jan  9 02:27:45 2006
New Revision: 109489

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=109489
Log:
fortran ChangeLog entry:
2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

	PR fortran/12456
	* trans-expr.c (gfc_to_single_character): New function that converts
	string to single character if its length is 1.
	(gfc_build_compare_string):New function that compare string and handle
	single character specially.
	(gfc_conv_expr_op): Use gfc_build_compare_string.
	(gfc_trans_string_copy): Use gfc_to_single_character.
	* trans-intrinsic.c (gfc_conv_intrinsic_strcmp): Use
	gfc_build_compare_string.
	* trans.h (gfc_build_compare_string): Add prototype.

testsuite ChangeLog entry:
2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

	PR fortran/12456
	* gfortran.dg/single_char_string.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/single_char_string.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 13 fengwang 2006-01-09 02:54:32 UTC
Subject: Bug 12456

Author: fengwang
Date: Mon Jan  9 02:54:25 2006
New Revision: 109491

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=109491
Log:
fortran
2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

        PR fortran/12456
        * trans-expr.c (gfc_to_single_character): New function that converts
        string to single character if its length is 1.
        (gfc_build_compare_string):New function that compare string and handle
        single character specially.
        (gfc_conv_expr_op): Use gfc_build_compare_string.
        (gfc_trans_string_copy): Use gfc_to_single_character.
        * trans-intrinsic.c (gfc_conv_intrinsic_strcmp): Use
        gfc_build_compare_string.
        * trans.h (gfc_build_compare_string): Add prototype.

2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

        * simplify.c (gfc_simplify_char): Use UCHAR_MAX instead of literal
        constant.
        (gfc_simplify_ichar): Get the result from unsinged char and in the
        range 0 to UCHAR_MAX instead of CHAR_MIN to CHAR_MAX.

testsuite
2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

        PR fortran/12456
        * gfortran.dg/single_char_string.f90: New test.

2006-01-09  Feng Wang  <fengwang@nudt.edu.cn>

        * gfortran.dg/ichar2.f90: New test.


Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/ichar_2.f90
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/single_char_string.f90
Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/simplify.c
    branches/gcc-4_1-branch/gcc/fortran/trans-expr.c
    branches/gcc-4_1-branch/gcc/fortran/trans-intrinsic.c
    branches/gcc-4_1-branch/gcc/fortran/trans.h
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 14 Andrew Pinski 2006-01-09 04:25:54 UTC
Fixed.