22

With IBM's XL compiler family it is possible to supply two options (-qreport and -qlist) to generate reports for each source file that include information on which optimizations were applied, or which parts of the code could not be optimized (and why).

Is it possible to get a similar reporting for GNU's g++ - and if yes, how to do it?

3 Answers3

16

Have a look at the -fdump-tree-[switch] flags. You can use -fdump-tree-all to get loads of information.

Also in trunk gcc -fopt-info-[options] will give you access higher level optimization information e.g. when particular optimizations were applied, missed etc e.g.

-fopt-info-inline-optimized-missed

Prints all successful and missed inlining optimizations (to stderr in this case). This is obviously pretty new functionality so I'm not sure how well supported it is yet.

In earlier releases they had -ftree-vectorizer-verbose=n which is now being deprecated in favor of opt-info.

All these options are listed here https://gcc.gnu.org/onlinedocs/gcc/Developer-Options.html though it can be a bit tricky to pick out the useful ones.

malat
  • 12,152
  • 13
  • 89
  • 158
jmetcalfe
  • 1,296
  • 9
  • 17
  • As far as I can tell, the "-fdump-tree-[switch]" output only gives me the results of the compiliation process (i.e. the "what") at various stages, but not the "how" and "why". Is there any way to get some high-level information on what the compiler does (maybe with appropriate postprocessing)? – Michael Schlottke-Lakemper Feb 07 '13 at 09:27
  • @Michael I added some more useful flags but it depends how bleeding edge your gcc is. On the plus side, it seems like this is improving quite a lot. – jmetcalfe Feb 07 '13 at 10:06
  • This seems to be exactly what I'm looking for, thank you. Exactly how bleeding edge is this feature? 4.7.2 (our version) does not have it, so I guess it's only in the development version so far. – Michael Schlottke-Lakemper Feb 07 '13 at 10:47
  • 1
    Yes, seems to only be in trunk. There is no mention of it on 4.8 change list either. I've only played with it briefly so not sure how well it works for all passes, but it worked well for a few simple tests. – jmetcalfe Feb 07 '13 at 11:14
7

Use -S -fverbose-asm to list every silently applied option (including optimization ones) in assembler output header.

Konstantin Vladimirov
  • 6,791
  • 1
  • 27
  • 36
3

From https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Optimize-Options.html#Optimize-Options:

You can invoke GCC with -Q --help=optimizers to find out the exact set of optimizations that are enabled at each level.

Example: (count number of optimization options enabled) A file is not necessary.

$ g++ -std=c++17 -O2 -Q --help=optimizers 2>&1 |grep enabled |wc -l
135

Note that many optimizations enabled by -O1/2/3 have no individual flags (see also: c++ - g++ O1 is not equal to O0 with all related optimization flags - Stack Overflow)

user202729
  • 3,358
  • 3
  • 25
  • 36
  • I'm not sure if it's a bug or this option is supposed to do something else, but when I use that option to gather all options a `-Og` adds to bisect to the ones that make inlining optiimization work *(which is not `-finline-functions` btw, at least not alone)*, and then I use them explicitly, the optimization suddenly stops working ‍♂️ So it clearly doesn't list all of the optimizations applied by `-Og`. – Hi-Angel Jan 20 '23 at 14:43
  • Oh, your last link explains, apparently it is a problem in GCC that individual optimization flags are ignored with `-O0` or without any `-Ox` added. So no way to make a custom `-Og` build that is actually a `-Og` *(the `-Og` option GCC provides is infamous for not doing what it's documented to)*. – Hi-Angel Jan 20 '23 at 14:52