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

            Bug ID: 89617
           Summary: memmove used even after runtime guard against overlap
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com
  Target Milestone: ---

For the following example, GCC 8.3 uses memmove unless compiling with
-DRESTRICT.  With -DRESTRICT it optimizes the copy to just 4 mov instructions. 
The copy code is only invoked after checking that the addresses differ, so
moving the call to std::copy out of line just to decorate it with __restrict__
should not be necessary to achieve optimal code generation, given strict
aliasing.

    #include <algorithm>
    #include <iterator>

    typedef int Arr[3];

    inline void Copy(Arr& __restrict__ dest, const Arr& __restrict__ src)
    {
        std::copy(std::begin(src), std::end(src), dest);
    }

    void f2(Arr& dest, const Arr& src)
    {
        if (&dest != &src) // make sure there is no aliasing
        {
    #ifdef RESTRICT
            Copy(dest, src); // generates better code, why?
    #else
            std::copy(std::begin(src), std::end(src), dest);
    #endif
        }
    }

Demo: https://godbolt.org/z/mT18Hb

Discussion:
https://stackoverflow.com/questions/55015151/can-arrays-overlap-alias-or-is-gcc-being-overly-cautious

Reply via email to