Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

gcc - When __builtin_memcpy is replaced with libc's memcpy

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...