Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force gcc to call memcpy under -Os

Tags:

c

gcc

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

like image 205
PSkocik Avatar asked Dec 27 '25 14:12

PSkocik


1 Answers

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
like image 95
KamilCuk Avatar answered Dec 31 '25 18:12

KamilCuk