Page 2 of 2

Re: Using GCC's built-in functions instead of self-written o

Posted: Fri May 06, 2016 3:51 am
by Neroku
Combuster wrote:
I want to let GCC replace the calls to my library functions with its built-in functions whenever possible.
Why don't you just simply compile with optimisations enabled instead of doing all these ifdefs? That way you can't mess things up.
Thanks a lot for the suggestion :D
It is definitely much more elegant not to pass GCC the option -fno-builtins and just do away with these preprocessor directives.
The problem is that I am using the -ffreestanding option and it also implicitly enables the -fno-builtin one. From "C Dialect-Options":
if you wish to enable built-in functions selectively when using -fno-builtin or -ffreestanding, you may define macros such as:

#define abs(n) __builtin_abs ((n))
#define strcpy(d, s) __builtin_strcpy ((d), (s))
Combuster wrote:Why don't you just simply compile with optimisations enabled instead of doing all these ifdefs? That way you can't mess things up.
I am assuming that you are telling me to enable optimizations in order to disable this -fno-builtin option. Where did you actually find whether -fno-builtin is disabled by enabling optimization? Did you find that in GCC's documentation or just by trial and error?
I have been looking around, but I can't find it :cry:


Thank you

Re: Using GCC's built-in functions instead of self-written o

Posted: Fri May 06, 2016 4:12 am
by Combuster
Actually, setting freestanding makes builtins invalid because that is the correct thing to do. You can either drop the freestanding because you actually do have a "C library" at your disposal, or you can override that assumption.

I just tested the following to make sure it does what one would expect.

Code: Select all

-ffreestanding -fbuiltin
Note that builtins actually allow the compiler to use different functions as substitutions, so be aware of undefined symbols appearing if you implemented the complex function but not the simpler degenerate case.

Re: Using GCC's built-in functions instead of self-written o

Posted: Fri May 06, 2016 10:26 am
by Rusky
If you want to enable only some builtin functions, you can leave -ffreestanding on and just #define those functions to their __builtin versions, without all the #ifdef stuff- like you discovered at the beginning of this thread, those will get linked to the real versions if they don't get optimized.

Re: Using GCC's built-in functions instead of self-written o

Posted: Wed May 18, 2016 4:23 am
by Neroku
Combuster wrote:Actually, setting freestanding makes builtins invalid because that is the correct thing to do. You can either drop the freestanding because you actually do have a "C library" at your disposal, or you can override that assumption.

I just tested the following to make sure it does what one would expect.

Code: Select all

-ffreestanding -fbuiltin
Note that builtins actually allow the compiler to use different functions as substitutions, so be aware of undefined symbols appearing if you implemented the complex function but not the simpler degenerate case.
Rusky wrote:If you want to enable only some builtin functions, you can leave -ffreestanding on and just #define those functions to their __builtin versions, without all the #ifdef stuff- like you discovered at the beginning of this thread, those will get linked to the real versions if they don't get optimized.
Both suggested solutions quoted above are indeed good. I'm going to stick to Combuster's proposed one (i.e.: passing GCC the option -fbuiltin) since it requires no modification of the source code at all.


Thank you ever so much! :D

Re: Using GCC's built-in functions instead of self-written o

Posted: Wed May 18, 2016 9:09 am
by Rusky
No, using -fbuiltin is equivalent to using the __builtin_* functions. The compiler will still emit calls to the library functions in some situations.

Re: Using GCC's built-in functions instead of self-written o

Posted: Wed May 18, 2016 9:49 am
by Neroku
Rusky wrote:No, using -fbuiltin is equivalent to using the __builtin_* functions. The compiler will still emit calls to the library functions in some situations.
I'm already aware of that. What I want to achieve is to let GCC use its built-in functions whenever possible.


Thanks

Re: Using GCC's built-in functions instead of self-written o

Posted: Wed May 18, 2016 9:57 am
by Rusky
Ah, I see. :oops: You're saying you wouldn't even need to write "#define memset __builtin_memset", for example. I figured you were comparing it to needing to write the libc functions themselves.