When __builtin_memcpy is replaced with libc's memcpy

osgx picture osgx · Jul 31, 2012 · Viewed 16k times · Source

There is a version of C99/posix memcpy function in GCC: __builtin_memcpy.

Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here:

Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call.

What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)?

When I should prefer of using __builtin_memcpy over plain memcpy?

Answer

C2H5OH picture C2H5OH · Sep 5, 2012

I had been experimenting with the builtin replacement some time ago and I found out that the <string.h> functions are only replaced when the size of the source argument can be known at compile time. In which case the call to libc is replaced directly by unrolled code.

Unless you compile with -fno-builtin, -ansi, -std=c89 or something similar, it actually doesn't matter wether you use the __builtin_ prefix or not.

Although it's hard to follow, the code that deciedes whether to emit a library call or a chunk of code seems to be here.