7

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.)

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Yuri Albuquerque
  • 444
  • 2
  • 14
  • Are you saying `-O0` is still optimizing? – Eugene Sh. Jul 16 '19 at 17:17
  • I'm saying that even passing this flag, the function is optimized – Yuri Albuquerque Jul 16 '19 at 17:18
  • Does it happen if you enable debugging symbols with `-g`? – Barmar Jul 16 '19 at 17:18
  • 1
    Looks like it is the case.. https://godbolt.org/z/NT0Cr1 Well, I would first ask - why do you need this? – Eugene Sh. Jul 16 '19 at 17:20
  • I just tried with `-g`, the answer to my question is yes, it still optimizes. – Barmar Jul 16 '19 at 17:21
  • 4
    Does `-fno-builtin` work? Of course, that will probably effect a lot more than `printf()` optimizations. – Andrew Henle Jul 16 '19 at 17:22
  • 2
    @AndrewHenle This works https://godbolt.org/z/FdXIGH – Eugene Sh. Jul 16 '19 at 17:23
  • 4
    `-fno-builtin-printf` if you want to disable only that one. Documented in the C dialect options. – Marc Glisse Jul 16 '19 at 17:25
  • 1
    @EugeneSh.: Yes, `-O0` will even optimize `x / 10` to a multiplicative inverse. See [Disable all optimization options in GCC](//stackoverflow.com/a/33284629). 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, or most other compilers with `-O0`. – Peter Cordes Jul 16 '19 at 17:27
  • @PeterCordes Yeah, my comment was about this specific optimization, I just saved few words making it a bit too general... – Eugene Sh. Jul 16 '19 at 17:28
  • @Barmar: `-g` never has any effect on code-gen for gcc, just how much debug info is emitted as metadata. – Peter Cordes Jul 16 '19 at 17:28
  • @PeterCordes I was hoping that it would impact optimization so you can find the function call when debugging, at least with low optimization levels. – Barmar Jul 16 '19 at 17:30
  • @EugeneSh.: Yeah, I was a bit surprised that gcc looked for printf->putchar at `-O0` (in fact I didn't know it did that at all). But I wasn't at all shocked; I did remember that it does the normal printf->puts optimization even at `-O0`. – Peter Cordes Jul 16 '19 at 17:30
  • There are many really simple optimizations that are almost like macro expansions. – Barmar Jul 16 '19 at 17:31
  • 1
    @Barmar: `-O0` makes sure function calls aren't optimized away. You can still set a breakpoint on the callsite and step into the call to see where it goes. And like Andrew and Marc commented, if you want to treat standard functions as not just part of the implementation, there's `-fno-builtin` or `-fno-builtin-funcname`. – Peter Cordes Jul 16 '19 at 17:31
  • 1
    Someone should post `-fno-builtin` as an answer. – Barmar Jul 16 '19 at 17:32
  • @Barmar: yup, you're right. Collected some of my comments into an answer. – Peter Cordes Jul 16 '19 at 17:41
  • Thank you all for the comments – Yuri Albuquerque Jul 16 '19 at 17:41

1 Answers1

7

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.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
  • "You can't disable all optimizations." This may not be correct. That division to multiplicative inverse optimization appears to be disabled in `-Os`. Then we can use `gcc -Os -Q --help=optimizers | grep enabled` to determine what optimizations that are enabled by `-Os` and explicitly disable each one of them using the corresponding flag. (I'm not sure if there is a flag dedicated to optimizing divisions). `-fno-builtin` has to also be used (or even `-ffreestanding`). I don't know whether this will disable all optimizations, but I don't agree with that answer you linked. – Hadi Brais Jul 16 '19 at 19:13
  • @HadiBrais: I think GCC is always going to at least combine `x = y + 1 + 2 +3` into `x = y + 6` with any options, if `y` is an integer type. The general point about GCC transforming through an internal representation is good. – Peter Cordes Jul 16 '19 at 19:19
  • Maybe. But the point I'm making is that it doesn't have to do that transformation to emit native code. I'm not very familiar with gcc's source code, but we have to look at the code and see what all flags and macros being used to enable/disable every single optimization/compilation pass. Hopefully a gcc developer will see this post and tell us for sure. – Hadi Brais Jul 16 '19 at 19:23
  • @HadiBrais: you could ping \@MarcGlisse in the comments on the question, he's a GCC dev. – Peter Cordes Jul 16 '19 at 19:29