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

            Bug ID: 92698
           Summary: Unnecessary copy in overlapping array assignment
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mjr19 at cam dot ac.uk
  Target Milestone: ---

subroutine cpy(a,src,dest,len)
  integer, intent(in) :: src,dest,len
  real(kind(1d0)), intent(inout) :: a(:)

  a(dest:dest+len-1)=a(src:src+len-1)

end subroutine cpy


seems to compile to malloc tmp array, inline copy to tmp, inline copy from tmp,
free tmp in gfortran 7.4 and 8.3. Gfortran 9.2 modifies this by replacing the
inline copies with memcpy at -O3.

Fortran permits the source and destination to overlap, so a single call to
memcpy would be wrong. However, it is always possible to do an overlapping copy
in place either by incrementing from the start or by decrementing from the end,
as glibc's memmove does. The use of a temporary array, whilst given as an
example in the Fortran standard to illustrate that overlap is permitted, seems
in practice unnecessary. Would it not be better to call memmove once rather
than memcpy twice? Or perhaps a single inline copy, direction to be determined
by the nature of the overlap?

(Above observations from Linux / x86_64)

Reply via email to