Search code examples
cgcc

How do I force gcc to call memcpy under -Os


Under -Os, gcc likes to inline memcpy into rep movsb (x86-64). How can I make it actually call memcpy while keeping the -Os compiler option?

Is there anything more local than -fno-builtin-memcpy that makes it do that? I would prefer something I could do from inside a C file and something which generates a direct call (calling memcpy through a volatile pointer also succeeds in preventing inlining, but it does not generate a direct call).

https://godbolt.org/z/EjYbPaGzf


Solution

  • The following:

    __attribute__((__optimize__("O2")))
    void *my_memcpy2(void *d, void *s, size_t c){
        return memcpy(d,s,c);
    }
    

    Outputs on godbolt:

    my_memcpy2:
            jmp     memcpy