Find out exact gcc implicit options
Asked Answered
F

1

2

I have a program which works only when compiled with a specific version of gcc, and using some optimization (-O1 or higher, but not -O0).

This program is incorrect in the C sense, but nevertheless I need to understand why it does not work with -O0. It is too large to be manually inspected by comparing the generated assembly codes, so I'd like GCC to tell me the exact flags that correspond to the execution of, say, -O1, but without using -O1 itself. The idea being that, by removing each optimization one at a time, I'll be able to better understand the problem.

I went to my specific gcc's optimization options page and tried adding all -f options that, according to the doc, are enabled by -O1, but that did not suffice (the program still behaves like -O0).

Is there a way to obtain some sort of normalized command line based on "lower-level" options -f* that correspond to what GCC does internally?

Firooc answered 20/6, 2016 at 6:11 Comment(3)
Note this problem sounds like there's some undefined behaviour in you code. Might want to try -fsanitize=undefined and see if that pops up anything.Sciential
NO, there is no way to get the equivalent of -O1 with a list of -f flags. gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html "Not all optimizations are controlled directly by a flag." "Most optimizations are only enabled if an -O level is set on the command line. Otherwise they are disabled, even if individual optimization flags are specified." You can disable quite a few optimizations with -O1 -fno-*, maybe some -fdisable-*, but I still don't believe that will help you much.Benedictus
@Sciential I agree completely, it's an incorrect program, but it deals with all kinds of nasty assembly and hardware-level interactions anyway, so I cannot expect to fix it. Still, this -O-related behavior needs to be understood somehow, so I guess I'll have to patch gcc itself to get some extra information.Firooc
A
2

You can use the flags Q --help=target,optimizer in combination with any optimizer flags ( e.g. -O2 or -O1) to view the applied flags. This is documented here

If you are using bash, you can use the below oneliner to view the difference between e.g. -O0 and -O1)

$ diff -u  <(gcc -O0  -Q --help=optimizer ) <(gcc -Q --help=optimizer -O1)
--- /dev/fd/63  2016-06-20 08:20:36.957943807 +0200
+++ /dev/fd/62  2016-06-20 08:20:36.958943794 +0200
@@ -11,22 +11,22 @@
   -fassociative-math                   [disabled]
   -fasynchronous-unwind-tables                 [enabled]
   -fauto-inc-dec                       [enabled]
-  -fbranch-count-reg                   [disabled]
+  -fbranch-count-reg                   [enabled]
   -fbranch-probabilities               [disabled]
   -fbranch-target-load-optimize        [disabled]
   -fbranch-target-load-optimize2       [disabled]
   -fbtr-bb-exclusive                   [disabled]
   -fcaller-saves                       [disabled]
-  -fcombine-stack-adjustments          [disabled]
-  -fcompare-elim                       [disabled]
+  -fcombine-stack-adjustments          [enabled]
+  -fcompare-elim                       [enabled]
   -fconserve-stack                     [disabled]
-  -fcprop-registers                    [disabled]
+  -fcprop-registers                    [enabled]
   -fcrossjumping                       [disabled]
   -fcse-follow-jumps                   [disabled]
   -fcx-fortran-rules                   [disabled]
   -fcx-limited-range                   [disabled]
   -fdce                                [enabled]
-  -fdefer-pop                          [disabled]
+  -fdefer-pop                          [enabled]
   -fdelayed-branch                     [disabled]
   -fdelete-dead-exceptions             [disabled]
   -fdelete-null-pointer-checks                 [enabled]
@@ -38,7 +38,7 @@
   -fexpensive-optimizations            [disabled]
   -ffinite-math-only                   [disabled]
   -ffloat-store                        [disabled]
-  -fforward-propagate                  [disabled]
+  -fforward-propagate                  [enabled]
   -ffp-contract=                       fast
   -ffunction-cse                       [enabled]
   -fgcse                               [disabled]
@@ -48,27 +48,27 @@
   -fgcse-sm                            [disabled]
   -fgraphite                           [disabled]
   -fgraphite-identity                  [disabled]
-  -fguess-branch-probability           [disabled]
+  -fguess-branch-probability           [enabled]
   -fhandle-exceptions
   -fhoist-adjacent-loads               [disabled]
-  -fif-conversion                      [disabled]
-  -fif-conversion2                     [disabled]
+  -fif-conversion                      [enabled]
+  -fif-conversion2                     [enabled]
   -findirect-inlining                  [disabled]
   -finline                             [enabled]
   -finline-atomics                     [enabled]
   -finline-functions                   [disabled]
-  -finline-functions-called-once       [disabled]
+  -finline-functions-called-once       [enabled]
   -finline-small-functions             [disabled]
   -fipa-cp                             [disabled]
   -fipa-cp-alignment                   [disabled]
   -fipa-cp-clone                       [disabled]
   -fipa-icf                            [disabled]
   -fipa-icf-functions                  [disabled]
-  -fipa-profile                        [disabled]
+  -fipa-profile                        [enabled]
   -fipa-pta                            [disabled]
-  -fipa-pure-const                     [disabled]
+  -fipa-pure-const                     [enabled]
   -fipa-ra                             [disabled]
-  -fipa-reference                      [disabled]
+  -fipa-reference                      [enabled]
   -fipa-sra                            [disabled]
   -fira-algorithm=                     CB
   -fira-hoist-pressure                 [enabled]
@@ -92,7 +92,7 @@
   -fmath-errno                         [enabled]
   -fmodulo-sched                       [disabled]
   -fmodulo-sched-allow-regmoves        [disabled]
-  -fmove-loop-invariants               [disabled]
+  -fmove-loop-invariants               [enabled]
   -fnon-call-exceptions                [disabled]
   -fnothrow-opt                        [disabled]
   -fomit-frame-pointer                 [disabled]
@@ -145,14 +145,14 @@
   -fshort-double                       [disabled]
   -fshort-enums                        [enabled]
   -fshort-wchar                        [disabled]
-  -fshrink-wrap                        [disabled]
+  -fshrink-wrap                        [enabled]
   -fsignaling-nans                     [disabled]
   -fsigned-zeros                       [enabled]
   -fsimd-cost-model=                   unlimited
   -fsingle-precision-constant          [disabled]
   -fsplit-ivs-in-unroller              [enabled]
-  -fsplit-wide-types                   [disabled]
-  -fssa-phiopt                         [disabled]
+  -fsplit-wide-types                   [enabled]
+  -fssa-phiopt                         [enabled]
   -fstack-reuse=                       all
   -fstdarg-opt                         [enabled]
   -fstrict-aliasing                    [disabled]
@@ -164,20 +164,20 @@
   -ftracer                             [disabled]
   -ftrapping-math                      [enabled]
   -ftrapv                              [disabled]
-  -ftree-bit-ccp                       [disabled]
+  -ftree-bit-ccp                       [enabled]
   -ftree-builtin-call-dce              [disabled]
-  -ftree-ccp                           [disabled]
-  -ftree-ch                            [disabled]
+  -ftree-ccp                           [enabled]
+  -ftree-ch                            [enabled]
   -ftree-coalesce-inlined-vars                 [disabled]
   -ftree-coalesce-vars                 [enabled]
-  -ftree-copy-prop                     [disabled]
-  -ftree-copyrename                    [disabled]
+  -ftree-copy-prop                     [enabled]
+  -ftree-copyrename                    [enabled]
   -ftree-cselim                        [enabled]
-  -ftree-dce                           [disabled]
-  -ftree-dominator-opts                [disabled]
-  -ftree-dse                           [disabled]
+  -ftree-dce                           [enabled]
+  -ftree-dominator-opts                [enabled]
+  -ftree-dse                           [enabled]
   -ftree-forwprop                      [enabled]
-  -ftree-fre                           [disabled]
+  -ftree-fre                           [enabled]
   -ftree-loop-distribute-patterns      [disabled]
   -ftree-loop-distribution             [disabled]
   -ftree-loop-if-convert               [enabled]
@@ -191,16 +191,16 @@
   -ftree-partial-pre                   [disabled]
   -ftree-phiprop                       [enabled]
   -ftree-pre                           [disabled]
-  -ftree-pta                           [disabled]
+  -ftree-pta                           [enabled]
   -ftree-reassoc                       [enabled]
   -ftree-scev-cprop                    [enabled]
-  -ftree-sink                          [disabled]
+  -ftree-sink                          [enabled]
   -ftree-slp-vectorize                 [disabled]
-  -ftree-slsr                          [disabled]
-  -ftree-sra                           [disabled]
+  -ftree-slsr                          [enabled]
+  -ftree-sra                           [enabled]
   -ftree-switch-conversion             [disabled]
   -ftree-tail-merge                    [disabled]
-  -ftree-ter                           [disabled]
+  -ftree-ter                           [enabled]
   -ftree-vectorize                     [disabled]
   -ftree-vrp                           [disabled]
   -funroll-all-loops                   [disabled]
Armoire answered 20/6, 2016 at 6:21 Comment(2)
That's a very useful option indeed! Unfortunately it did not yet exist on gcc 3.4.3, which is the one I need to test (I mean, the -Q option exists, but not --help=optimize), but from what I've searched and Marc Glisse's comment, there is no better option anyway, so I'll accept yours since it's very useful for more recent versions of gcc.Firooc
Using --help=target,optimizer gives an error. Using --help=optimizers is what is documented in the link you provided.Canakin

© 2022 - 2024 — McMap. All rights reserved.