[Bug middle-end/82898] Aliasing knowledge is not used to replace memmove with memcpy

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Nov 8 16:06:00 GMT 2017


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
(In reply to Antony Polukhin from comment #0)

Declaring the pointers restrict has the effect you are looking for (calling
memcpy).

Without the restrict keyword the pointers cannot be assumed not to alias one
another even if they point to different types because the C standard aliasing
rules allow objects of incompatible types to be accessed via an lvalue of a
character type (e.g., such as by memmove).

For the case of

  void foo(int* i, const float* f)
  {
    i[0] = f[0]; // Does it triggers the aliasing logic?
    __builtin_memmove(i, f, 1024*1024);
  }

the assignment implies that the objects pointed to by i and f are distinct
(otherwise the access would be undefined) and so the memove call could be
replaced by memcpy.  Though whether or not it would gain much, if anything, is
a separate question.


More information about the Gcc-bugs mailing list