24

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?

2
  • 6
    I can't imagine GCC can't do something IBM's compiler can (apart from crashing). Commented Feb 6, 2013 at 19:47
  • 2
    I just updated my question to include the "how", not just the "if". Commented Feb 6, 2013 at 20:01

4 Answers 4

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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

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

3 Comments

Isn't this the default with -S?
Of course no. Just try with -fverbose-asm and without and look at the difference.
Just verified with gcc 4.8.2 and 4.1.2 on linux, works well, this trick not limited to optimization option but all implicit option. I'd check assembly listing many times but never noticed the header contains such a useful info.
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)

2 Comments

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

Took me way too long to find this but the key is the little-documented -fdump-passes option like so:

echo | gcc -O3 -fdump-passes -c -o /dev/null -x c -

The above gives a dump of the real effective optimizations and some warnings:

   *warn_unused_result                                 :  ON
   *diagnose_omp_blocks                                :  OFF
   *diagnose_tm_blocks                                 :  OFF
   tree-omp_oacc_kernels_decompose                     :  OFF
   tree-omplower                                       :  ON
   tree-lower                                          :  ON
   tree-tmlower                                        :  OFF
   tree-ehopt                                          :  OFF
   tree-eh                                             :  ON
   tree-coro-lower-builtins                            :  OFF
   tree-cfg                                            :  ON
   *warn_function_return                               :  ON
   tree-coro-early-expand-ifns                         :  OFF
   tree-ompexp                                         :  ON
   *build_cgraph_edges                                 :  ON
   *free_lang_data                                     :  ON
   ipa-visibility                                      :  ON
   ipa-strubm                                          :  OFF
   ipa-build_ssa_passes                                :  ON
      tree-fixup_cfg1                                  :  ON
      tree-ssa                                         :  ON
      tree-walloca1                                    :  ON
      tree-warn-printf                                 :  OFF
      *nonnullcmp                                      :  OFF
      tree-early_uninit                                :  OFF
      tree-waccess1                                    :  ON
      tree-ubsan                                       :  OFF
      tree-nothrow                                     :  ON
      *rebuild_cgraph_edges                            :  ON
   ipa-opt_local_passes                                :  ON
      tree-fixup_cfg2                                  :  ON
      *rebuild_cgraph_edges                            :  ON
      tree-local-fnsummary1                            :  ON
      tree-einline                                     :  ON
      *infinite-recursion                              :  OFF
      tree-early_optimizations                         :  ON
         *remove_cgraph_callee_edges                   :  ON
         tree-early_objsz                              :  ON
         tree-ccp1                                     :  ON
         tree-forwprop1                                :  ON
         tree-ethread                                  :  ON
         tree-esra                                     :  ON
         tree-ealias                                   :  ON
         tree-phiprop1                                 :  ON
         tree-fre1                                     :  ON
         tree-evrp                                     :  ON
         tree-mergephi1                                :  ON
         tree-dse1                                     :  ON
         tree-cddce1                                   :  ON
         tree-phiopt1                                  :  ON
         tree-tailr1                                   :  ON
         tree-iftoswitch                               :  ON
         tree-switchconv                               :  ON
         tree-ehcleanup1                               :  OFF
         tree-sccopy1                                  :  ON
         tree-profile_estimate                         :  ON
         tree-local-pure-const1                        :  ON
         tree-modref1                                  :  ON
         tree-fnsplit                                  :  ON
         *strip_predict_hints                          :  ON
      tree-release_ssa                                 :  ON
      *rebuild_cgraph_edges                            :  ON
      tree-local-fnsummary2                            :  ON
   ipa-remove_symbols                                  :  ON
   ipa-strub                                           :  OFF
   ipa-ipa_oacc                                        :  OFF
      ipa-pta1                                         :  OFF
      ipa-ipa_oacc_kernels                             :  ON
         tree-oacc_kernels                             :  OFF
            tree-ch1                                   :  ON
            tree-fre2                                  :  ON
            tree-lim1                                  :  ON
            tree-dom1                                  :  ON
            tree-dce1                                  :  ON
            tree-parloops1                             :  OFF
            tree-ompexpssa1                            :  ON
            *rebuild_cgraph_edges                      :  ON
   ipa-targetclone                                     :  ON
   ipa-afdo                                            :  OFF
   ipa-profile                                         :  OFF
      tree-feedback_fnsplit                            :  OFF
   ipa-free-fnsummary1                                 :  ON
   ipa-increase_alignment                              :  OFF
   ipa-tmipa                                           :  OFF
   ipa-emutls                                          :  OFF
   ipa-analyzer                                        :  OFF
   ipa-odr                                             :  OFF
   ipa-whole-program                                   :  ON
   ipa-profile_estimate                                :  ON
   ipa-icf                                             :  ON
   ipa-devirt                                          :  ON
   ipa-cdtor                                           :  OFF
   ipa-cp                                              :  ON
   ipa-sra                                             :  ON
   ipa-fnsummary                                       :  ON
   ipa-inline                                          :  ON
   ipa-locality-clone                                  :  OFF
   ipa-pure-const                                      :  ON
   ipa-modref                                          :  ON
   ipa-free-fnsummary2                                 :  ON
   ipa-static-var                                      :  ON
   ipa-single-use                                      :  ON
   ipa-comdats                                         :  ON
   ipa-pta2                                            :  OFF
   ipa-simdclone                                       :  ON
   tree-fixup_cfg3                                     :  ON
   tree-ehdisp                                         :  OFF
   tree-oaccloops                                      :  OFF
   tree-omp_oacc_neuter_broadcast                      :  OFF
   tree-oaccdevlow                                     :  OFF
   tree-ompdevlow                                      :  ON
   tree-omptargetlink                                  :  OFF
   tree-adjust_alignment                               :  ON
   tree-hardcfr                                        :  OFF
   *all_optimizations                                  :  ON
      *remove_cgraph_callee_edges                      :  ON
      *strip_predict_hints                             :  ON
      tree-ccp2                                        :  ON
      tree-objsz1                                      :  ON
      tree-post_ipa_warn1                              :  OFF
      tree-waccess2                                    :  ON
      tree-rebuild_frequencies1                        :  ON
      tree-cunrolli                                    :  ON
      tree-backprop                                    :  ON
      tree-phiprop2                                    :  ON
      tree-forwprop2                                   :  ON
      tree-alias                                       :  ON
      tree-retslot                                     :  ON
      tree-fre3                                        :  ON
      tree-mergephi2                                   :  ON
      tree-threadfull1                                 :  ON
      tree-vrp1                                        :  ON
      tree-bounds                                      :  OFF
      tree-dse2                                        :  ON
      tree-dce2                                        :  ON
      tree-stdarg                                      :  ON
      tree-cdce                                        :  ON
      tree-cselim                                      :  ON
      tree-copyprop1                                   :  ON
      tree-ifcombine                                   :  ON
      tree-mergephi3                                   :  ON
      tree-phiopt2                                     :  ON
      tree-tailr2                                      :  ON
      tree-ch2                                         :  ON
      tree-cplxlower1                                  :  ON
      tree-bitintlower1                                :  ON
      tree-sra                                         :  ON
      tree-thread1                                     :  ON
      tree-dom2                                        :  ON
      tree-copyprop2                                   :  ON
      tree-isolate-paths                               :  ON
      tree-reassoc1                                    :  ON
      tree-dce3                                        :  ON
      tree-forwprop3                                   :  ON
      tree-phiopt3                                     :  ON
      tree-ccp3                                        :  ON
      tree-pow                                         :  ON
      tree-bswap                                       :  ON
      tree-laddress                                    :  ON
      tree-lim2                                        :  ON
      tree-walloca2                                    :  ON
      tree-pre                                         :  ON
      tree-sink1                                       :  ON
      tree-sancov1                                     :  OFF
      tree-asan1                                       :  OFF
      tree-tsan1                                       :  OFF
      tree-dse3                                        :  ON
      tree-dce4                                        :  ON
      tree-fix_loops                                   :  ON
      tree-loop                                        :  ON
         tree-loopinit                                 :  ON
         tree-unswitch                                 :  ON
         tree-lsplit                                   :  ON
         tree-sccp                                     :  ON
         tree-lversion                                 :  ON
         tree-unrolljam                                :  ON
         tree-cddce2                                   :  ON
         tree-ivcanon                                  :  ON
         tree-ldist                                    :  ON
         tree-crc                                      :  ON
         tree-linterchange                             :  ON
         tree-copyprop3                                :  ON
         tree-graphite0                                :  OFF
            tree-graphite                              :  OFF
            tree-lim3                                  :  ON
            tree-copyprop4                             :  ON
            tree-dce5                                  :  ON
         tree-parloops2                                :  OFF
         tree-ompexpssa2                               :  ON
         tree-ch_vect                                  :  ON
         tree-ifcvt                                    :  ON
         tree-vect                                     :  ON
            tree-dce6                                  :  ON
         tree-pcom                                     :  ON
         tree-cunroll                                  :  ON
         *pre_slp_scalar_cleanup                       :  OFF
            tree-fre4                                  :  ON
            tree-dse4                                  :  ON
         tree-slp1                                     :  ON
         tree-aprefetch                                :  OFF
         tree-ivopts                                   :  ON
         tree-lim4                                     :  ON
         tree-loopdone                                 :  ON
      tree-no_loop                                     :  OFF
         tree-slp2                                     :  ON
      tree-simduid1                                    :  OFF
      tree-veclower21                                  :  ON
      tree-switchlower1                                :  ON
      tree-sincos                                      :  ON
      tree-recip                                       :  OFF
      tree-reassoc2                                    :  ON
      tree-slsr                                        :  ON
      tree-split-paths                                 :  ON
      tree-tracer                                      :  OFF
      tree-fre5                                        :  ON
      tree-thread2                                     :  ON
      tree-dom3                                        :  ON
      tree-strlen1                                     :  ON
      tree-threadfull2                                 :  ON
      tree-vrp2                                        :  ON
      tree-ccp4                                        :  ON
      tree-wrestrict                                   :  ON
      tree-dse5                                        :  ON
      tree-dce7                                        :  ON
      tree-forwprop4                                   :  ON
      tree-sink2                                       :  ON
      tree-phiopt4                                     :  ON
      tree-fab1                                        :  ON
      tree-widening_mul                                :  ON
      tree-store-merging                               :  ON
      tree-cddce3                                      :  ON
      tree-sccopy2                                     :  ON
      tree-tailc                                       :  ON
      tree-crited1                                     :  ON
      tree-uninit1                                     :  OFF
      tree-local-pure-const2                           :  ON
      tree-modref2                                     :  ON
      tree-uncprop1                                    :  ON
   *all_optimizations_g                                :  OFF
      *remove_cgraph_callee_edges                      :  ON
      *strip_predict_hints                             :  ON
      tree-cplxlower2                                  :  ON
      tree-bitintlower2                                :  ON
      tree-veclower22                                  :  ON
      tree-switchlower2                                :  ON
      tree-ccp5                                        :  ON
      tree-post_ipa_warn2                              :  OFF
      tree-objsz2                                      :  ON
      tree-fab2                                        :  ON
      tree-strlen2                                     :  ON
      tree-copyprop5                                   :  ON
      tree-dce8                                        :  ON
      tree-rebuild_frequencies2                        :  ON
      tree-sancov2                                     :  OFF
      tree-asan2                                       :  OFF
      tree-tsan2                                       :  OFF
      tree-crited2                                     :  ON
      tree-uninit2                                     :  OFF
      tree-uncprop2                                    :  ON
   tree-assumptions                                    :  OFF
   *tminit                                             :  OFF
      tree-tmmark                                      :  ON
      tree-tmmemopt                                    :  OFF
      tree-tmedge                                      :  ON
   tree-simduid2                                       :  OFF
   tree-vtable-verify                                  :  OFF
   tree-lower_vaarg                                    :  ON
   tree-veclower                                       :  ON
   tree-cplxlower0                                     :  ON
   tree-bitintlower0                                   :  ON
   tree-sancov_O0                                      :  OFF
   tree-switchlower_O0                                 :  OFF
   tree-asan0                                          :  OFF
   tree-tsan0                                          :  OFF
   tree-sanopt                                         :  OFF
   tree-ehcleanup2                                     :  OFF
   tree-musttail                                       :  OFF
   tree-resx                                           :  OFF
   tree-nrv                                            :  ON
   tree-isel                                           :  ON
   tree-hardcbr                                        :  OFF
   tree-hardcmp                                        :  OFF
   tree-waccess3                                       :  ON
   tree-optimized                                      :  ON
   *warn_function_noreturn                             :  OFF
   rtl-expand                                          :  ON
   *rest_of_compilation                                :  ON
      rtl-vregs                                        :  ON
      rtl-into_cfglayout                               :  ON
      rtl-jump                                         :  ON
      rtl-subreg1                                      :  ON
      rtl-dfinit                                       :  ON
      rtl-cse1                                         :  ON
      rtl-fwprop1                                      :  ON
      rtl-cprop1                                       :  ON
      rtl-rtl pre                                      :  ON
      rtl-hoist                                        :  OFF
      rtl-hardreg_pre                                  :  OFF
      rtl-cprop2                                       :  ON
      rtl-store_motion                                 :  OFF
      rtl-cse_local                                    :  OFF
      rtl-ce1                                          :  ON
      rtl-apx_nfcvt                                    :  OFF
      rtl-reginfo                                      :  ON
      rtl-loop2                                        :  ON
         rtl-loop2_init                                :  ON
         rtl-loop2_invariant                           :  ON
         rtl-loop2_unroll                              :  ON
         rtl-loop2_doloop                              :  OFF
         rtl-loop2_done                                :  ON
      rtl-subreg2                                      :  OFF
      rtl-web                                          :  OFF
      rtl-cprop3                                       :  ON
      rtl-stv1                                         :  ON
      rtl-cse2                                         :  ON
      rtl-dse1                                         :  ON
      rtl-fwprop2                                      :  ON
      rtl-auto_inc_dec                                 :  OFF
      rtl-init-regs                                    :  ON
      rtl-ud_dce                                       :  ON
      rtl-ext_dce                                      :  ON
      rtl-combine                                      :  ON
      rtl-late_combine1                                :  ON
      rtl-rpad                                         :  OFF
      rtl-stv2                                         :  ON
      rtl-ce2                                          :  ON
      rtl-jump_after_combine                           :  ON
      rtl-bbpart                                       :  ON
      rtl-outof_cfglayout                              :  ON
      rtl-split1                                       :  ON
      rtl-subreg3                                      :  ON
      rtl-no-opt dfinit                                :  OFF
      *stack_ptr_mod                                   :  ON
      rtl-mode_sw                                      :  ON
      rtl-asmcons                                      :  ON
      rtl-sms                                          :  OFF
      rtl-lr_shrinkage                                 :  OFF
      rtl-sched1                                       :  OFF
      rtl-avoid_store_forwarding                       :  OFF
      rtl-early_remat                                  :  OFF
      rtl-ira                                          :  ON
      rtl-reload                                       :  ON
      *all-postreload                                  :  OFF
         rtl-postreload                                :  OFF
         rtl-vzeroupper                                :  OFF
         rtl-late_combine2                             :  ON
         rtl-gcse2                                     :  ON
         rtl-split2                                    :  ON
         rtl-ree                                       :  ON
         rtl-cmpelim                                   :  ON
         rtl-pro_and_epilogue                          :  ON
         rtl-dse2                                      :  ON
         rtl-csa                                       :  ON
         rtl-jump2                                     :  ON
         rtl-compgotos                                 :  ON
         rtl-sched_fusion                              :  OFF
         rtl-peephole2                                 :  ON
         rtl-ce3                                       :  ON
         rtl-rnreg                                     :  OFF
         rtl-fold_mem_offsets                          :  ON
         rtl-cprop_hardreg                             :  ON
         rtl-rtl_dce                                   :  ON
         rtl-bbro                                      :  ON
         *leaf_regs                                    :  ON
         rtl-split3                                    :  ON
         rtl-sched2                                    :  ON
         *stack_regs                                   :  ON
            rtl-split4                                 :  OFF
            rtl-stack                                  :  ON
      rtl-late_pro_and_epilogue                        :  OFF
      *all-late_compilation                            :  OFF
         rtl-zero_call_used_regs                       :  ON
         rtl-alignments                                :  ON
         rtl-vartrack                                  :  OFF
         *free_cfg                                     :  ON
         rtl-mach                                      :  ON
         rtl-barriers                                  :  ON
         rtl-dbr                                       :  OFF
         rtl-split5                                    :  OFF
         rtl-eh_ranges                                 :  OFF
         rtl-endbr_and_patchable_area                  :  OFF
         rtl-align_tight_loops                         :  ON
         rtl-shorten                                   :  ON
         rtl-nothrow                                   :  ON
         rtl-dwarf2                                    :  ON
         rtl-final                                     :  ON
      rtl-dfinish                                      :  ON
   *clean_state                                        :  ON
Compiler returned: 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.