Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When __builtin_memcpy is replaced with libc's memcpy

Tags:

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?

like image 782
osgx Avatar asked Jul 31 '12 19:07

osgx


1 Answers

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.

like image 90
C2H5OH Avatar answered Sep 28 '22 11:09

C2H5OH