How to disable all optimizations for gcc?

You may think all the optimisations are turned off thoroughly if you do not provide the -O option for gcc. But according to this post, if you do not give the -O option, it is defaulted to -O0, which means:

Reduce compilation time and make debugging produce the expected results. This is the default.

We can see gcc still do some optimizations to reduce the compilation time. This post talks about the usage of the “-Og” option. However, this compiler switch still cannot completely disable the full optimizations for gcc. You can use the following command to check what optimization options are disabled and what are enabled for an optimization level:

gcc -Q -O0 --help=optimizers

The -Q option is rarely used, which changes the original output of “–help=optimizers”. Specifically, it will show you the status of all optimization features. From the output of this command, we can see there are still a lot of optimization methods are turned on for -O0 and -Og. With the help of this command, we can find what optimization algorithms are still enabled and turn them off by adding the following options when compiling a program:

-O0 -g -fno-exceptions -fno-web -fno-var-tracking-assignments -fno-var-tracking -fno-unwind-tables -fno-tree-scev-cprop  -fno-tree-reassoc -fno-tree-phiprop -fno-tree-loop-optimize -fno-tree-loop-ivcanon -fno-tree-loop-im -fno-tree-loop-if-convert -fno-tree-forwprop -fno-tree-cselim -fno-trapping-math -fthreadsafe-statics -fno-strict-volatile-bitfields -fno-stdarg-opt -fno-ssa-backprop  -fno-split-ivs-in-unroller -fno-signed-zeros -fno-shrink-wrap-separate -fno-short-enums -fno-set-stack-executable -fno-schedule-fusion -fno-sched-stalled-insns-dep -fno-sched-spec-insn-heuristic -fno-sched-spec -fno-sched-rank-heuristic -fno-sched-last-insn-heuristic -fno-sched-interblock  -fno-sched-group-heuristic -fno-sched-dep-count-heuristic -fno-sched-critical-path-heuristic -fno-rtti -fno-rename-registers -fno-reg-struct-return -fno-printf-return-value -fno-prefetch-loop-arrays -fno-plt  -fno-peephole -fno-math-errno -fno-lifetime-dse -fno-jump-tables -fno-ivopts -fno-ira-share-spill-slots -fno-ira-share-save-slots -fno-ira-hoist-pressure -fno-inline-atomics -fno-inline -fno-gcse-lm -fno-function-cse -fno-fp-int-builtin-inexact -fno-early-inlining -fno-dse -fno-delete-null-pointer-checks -fno-dce -fno-auto-inc-dec -fno-asynchronous-unwind-tables -fno-aggressive-loop-optimizations

The -fno-xxxx is used to disable the xxxx feature while -fxxxx is used to enable that feature. Now -O0 option turns off most optimization features, and -fno-xxxxs turn off the remaining active optimization features. With these options specified, all optimizations are totally disabled.  Note that, you must add the -fno-exceptions option, otherwise, it will complain about:

Problem: error `__gxx_personality_sj0′

You may also need to add the -latomic option to your linker to avoid the following error:

undefined reference to `__atomic_load_1′




If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply