Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the gcc compiler to not optimize a standard library function call like printf?

Out of curiosity, I was wondering if there is any way that gcc does not optimize any function calls?

In the generated assembly code, the printf function is replaced by putchar. This happens even with the default -O0 minimal optimization flag.

#include <stdio.h>

int main(void) {
    printf("a");
    return 0;
}

(Godbolt showing GCC9 doing it, clang8 keeping it unchanged.)

like image 604
Yuri Albuquerque Avatar asked Jul 16 '19 17:07

Yuri Albuquerque


People also ask

How do I disable compiler optimization gcc?

Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command-line options.

Does gcc optimize by default?

GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).

How do I know if gcc is not optimized?

Compiler specific pragma gcc provides pragma GCC as a way to control temporarily the compiler behavior. By using pragma GCC optimize("O0") , the optimization level can be set to zero, which means absolutely no optimize for gcc.

What is gcc optimize?

The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.


1 Answers

Use -fno-builtin to disable all replacement and inlining of standard C functions with equivalents.

Or use -fno-builtin-FUNCNAME for a specific function, like -fno-builtin-printf.

By default, some commonly-used standard C functions are handled as builtin functions, similar to __builtin_popcount. The handler for printf replaces it with putchar or puts if possible. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

The implementation details of a C statement like printf("a") are not considered a visible side-effect by default, so aren't something that gets preserved. You can still set a breakpoint at the call-site and step into the function (at least in asm, or in source mode if you have debug symbols installed).


To disable other kinds of optimizations for a single function, see __attribute__((optimize(0))) on a function or #pragma GCC optimize. But beware:

The optimize attribute should be used for debugging purposes only. It is not suitable in production code.


You can't disable all optimizations. Some optimization is inherent in the way gcc transforms through an internal representation on the way to asm. See Disable all optimization options in GCC.

e.g. even at -O0 gcc will optimize x / 10 to a multiplicative inverse.

It still stores everything to memory between C statements (for consistent debugging; that's what -O0 really means); gcc doesn't have a "fully dumb" mode that tries to transliterate C to asm as naively as possible. Use tcc for that. Clang and ICC with -O0 are somewhat more literal than gcc, and so is MSVC debug mode.

Note that -g never has any effect on code-gen, only on the metadata emitted. GCC uses other options (mostly -O, -f*, and -m*) to control code-gen, so you can always safely enable -g without hurting performance, other than a larger binary. It's not debug mode (that's -O0), it's just debug symbols.

like image 82
Peter Cordes Avatar answered Oct 24 '22 18:10

Peter Cordes