public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/30398]  New: memmove for string operations
@ 2007-01-06 22:05 tkoenig at gcc dot gnu dot org
  2007-01-06 22:07 ` [Bug fortran/30398] " tkoenig at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu dot org @ 2007-01-06 22:05 UTC (permalink / raw)
  To: gcc-bugs

The compiler should be able to detect that s and c
are not aliased, so a call to memcpy instead of memmove
could be issued.

$ cat memmove.f90
program main
  character(len=1) :: s
  character(len=2) :: c
  s = 'a'
  c = repeat(s,2)
end program main
$ gfortran -fdump-tree-original memmove.f90
$ cat memmove.f90.003t.original
MAIN__ ()
{
  char c[1:2];
  char s[1:1];

  _gfortran_set_std (70, 127, 0);
  s[1]{lb: 1 sz: 1} = "a"[1]{lb: 1 sz: 1};
  {
    char str.0[2];

    _gfortran_string_repeat ((char[1:] *) &str.0, 1, &s, 2);
    __builtin_memmove (&c, (char[1:] *) &str.0, 2);
  }
}


-- 
           Summary: memmove for string operations
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tkoenig at gcc dot gnu dot org


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/30398] memmove for string operations
  2007-01-06 22:05 [Bug fortran/30398] New: memmove for string operations tkoenig at gcc dot gnu dot org
@ 2007-01-06 22:07 ` tkoenig at gcc dot gnu dot org
  2007-01-22 10:03 ` fxcoudert at gcc dot gnu dot org
  2007-04-13 15:27 ` [Bug tree-optimization/30398] " tobi at gcc dot gnu dot org
  2 siblings, 0 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu dot org @ 2007-01-06 22:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from tkoenig at gcc dot gnu dot org  2007-01-06 22:07 -------
(In reply to comment #0)
> The compiler should be able to detect that s and c
> are not aliased, so a call to memcpy instead of memmove
> could be issued.

Or, even better, the memmove/memcpy could be ommitted completely,
by using the variable directly as the target.


-- 


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/30398] memmove for string operations
  2007-01-06 22:05 [Bug fortran/30398] New: memmove for string operations tkoenig at gcc dot gnu dot org
  2007-01-06 22:07 ` [Bug fortran/30398] " tkoenig at gcc dot gnu dot org
@ 2007-01-22 10:03 ` fxcoudert at gcc dot gnu dot org
  2007-04-13 15:27 ` [Bug tree-optimization/30398] " tobi at gcc dot gnu dot org
  2 siblings, 0 replies; 5+ messages in thread
From: fxcoudert at gcc dot gnu dot org @ 2007-01-22 10:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from fxcoudert at gcc dot gnu dot org  2007-01-22 10:02 -------
(In reply to comment #1)
> Or, even better, the memmove/memcpy could be ommitted completely,
> by using the variable directly as the target.

The string_repeat() function could be generated directly by the front-end,
because it's really simple code:

void
string_repeat (char * dest, GFC_INTEGER_4 slen,
               const char * src, GFC_INTEGER_4 ncopies)
{
  int i;

  /* See if ncopies is valid.  */
  if (ncopies < 0)
    {
      /* The error is already reported.  */
      runtime_error ("Augument NCOPIES is negative.");
    }

  /* Copy characters.  */
  for (i = 0; i < ncopies; i++)
    {
      memmove (dest + (i * slen), src, slen);
    }
}


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2007-01-22 10:02:53
               date|                            |


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/30398] memmove for string operations
  2007-01-06 22:05 [Bug fortran/30398] New: memmove for string operations tkoenig at gcc dot gnu dot org
  2007-01-06 22:07 ` [Bug fortran/30398] " tkoenig at gcc dot gnu dot org
  2007-01-22 10:03 ` fxcoudert at gcc dot gnu dot org
@ 2007-04-13 15:27 ` tobi at gcc dot gnu dot org
  2 siblings, 0 replies; 5+ messages in thread
From: tobi at gcc dot gnu dot org @ 2007-04-13 15:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from tobi at gcc dot gnu dot org  2007-04-13 16:27 -------
With FX' patch to inline REPEAT (see PR31304), we are left with this as final
dump after a -O compilation:
;; Function MAIN__ (MAIN__)

MAIN__ ()
{
  char[1:] * pstr.1;
  char s[1:1];
  char c[1:2];
  void * D.1004;

<bb 2>:
  _gfortran_set_std (68, 127, 0, 0, 0);
  s[1]{lb: 1 sz: 1} = 97;
  D.1004 = _gfortran_internal_malloc (2);
  pstr.1 = (char[1:] *) D.1004;
  __builtin_memcpy ((char *) pstr.1, &s, 1);
  __builtin_memcpy (pstr.1 + 1B, &s, 1);
  __builtin_memmove (&c, pstr.1, 2);
  _gfortran_internal_free (pstr.1);
  return;

}

The call to memmove makes it way into the assembly, even though pstr and c
can't alias.  This is now an optimizer issue, moving to component
tree-optimization, as the tree-ssa optimizers should be able to eliminate the
memmove in favor of a memcpy.  Or even better, get rid of the whole copying
business and predict the correct results.  If that's possible it would be great
to have a way to get rid of the then unnecessary temporary allocation.


-- 

tobi at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tobi at gcc dot gnu dot org,
                   |                            |fxcoudert at gcc dot gnu dot
                   |                            |org
          Component|fortran                     |tree-optimization


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug tree-optimization/30398] memmove for string operations
       [not found] <bug-30398-4@http.gcc.gnu.org/bugzilla/>
@ 2020-11-11  9:31 ` tkoenig at gcc dot gnu.org
  0 siblings, 0 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-11-11  9:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30398

Thomas Koenig <tkoenig at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
This is what we generated now for

program main
  character(len=1) :: s
  character(len=2) :: c
  s = 'a'
  c = repeat(s,2)
  call foo(c)
end program main

:

;; Function main (main, funcdef_no=1, decl_uid=3926, cgraph_uid=2,
symbol_order=1) (executed once)

__attribute__((externally_visible))
main (integer(kind=4) argc, character(kind=1) * * argv)
{
  character(kind=1) c[1:2];
  static integer(kind=4) options.3[7] = {2116, 4095, 0, 1, 1, 0, 31};

  <bb 2> [local count: 1073741825]:
  _gfortran_set_args (argc_2(D), argv_3(D));
  _gfortran_set_options (7, &options.3[0]);
  MEM <unsigned short> [(c_char * {ref-all})&c] = 24929;
  foo (&c, 2);
  c ={v} {CLOBBER};
  return 0;

}

So, everything that should be optimized is now optimized.

Fixed.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-11  9:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-06 22:05 [Bug fortran/30398] New: memmove for string operations tkoenig at gcc dot gnu dot org
2007-01-06 22:07 ` [Bug fortran/30398] " tkoenig at gcc dot gnu dot org
2007-01-22 10:03 ` fxcoudert at gcc dot gnu dot org
2007-04-13 15:27 ` [Bug tree-optimization/30398] " tobi at gcc dot gnu dot org
     [not found] <bug-30398-4@http.gcc.gnu.org/bugzilla/>
2020-11-11  9:31 ` tkoenig at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).