In GCC 10.2, -O2 optimization enables more than docs suggest

Richard Earnshaw
Wed Jan 20 15:56:12 GMT 2021

On 16/01/2021 20:48, Brent Roman wrote:
> My very old, highly modified Matz Ruby 1.87 interpreter stopped working
> when Debian switched from GCC-9 to GCC-10
> The Ruby interpreter binary output from GCC-10 segfaults.
> However, if I reduce optimization from -O2 to -O1, the resulting
> binaries work fine.
> I'd like to find which specific -O2 optimization is causing the failure
> when run through gcc-10.
> But, when I specified -O1 followed by options explicitly enabling all
> the specific optimizations that are supposed to be enabled by -O2,
> the resulting binary works fine.  Conversely, when I specify -O2,
> followed by explicit options to *disable* all those options, the
> resulting binary fails.
> Does anyone know what -O2 enables aside from the options documented on:
> Here's an example of a gcc invocation with -O2 followed by disabling all
> the -O2 specific optimizations:
> gcc -O2 -g -Wclobbered -fno-stack-protector -fno-align-functions
> -fno-align-jumps  -fno-align-labels  -fno-align-loops -fno-caller-saves 
> -fno-code-hoisting  -fno-crossjumping -fno-cse-follow-jumps 
> -fno-cse-skip-blocks -fno-delete-null-pointer-checks  -fno-devirtualize
> -fno-devirtualize-speculatively  -fno-expensive-optimizations
> -fno-finite-loops  -fno-gcse  -fno-gcse-lm -fno-hoist-adjacent-loads 
> -fno-inline-functions -fno-inline-small-functions 
> -fno-indirect-inlining -fno-ipa-bit-cp  -fno-ipa-cp  -fno-ipa-icf 
> -fno-ipa-ra -fno-ipa-sra  -fno-ipa-vrp
> -fno-isolate-erroneous-paths-dereference  -fno-lra-remat
> -fno-optimize-sibling-calls  -fno-optimize-strlen -fno-partial-inlining 
> -fno-peephole2 -fno-reorder-blocks-and-partition  -fno-reorder-functions
> -fno-rerun-cse-after-loop   -fno-schedule-insns -fno-schedule-insns2 
> -fno-sched-interblock  -fno-sched-spec -fno-store-merging 
> -fno-strict-aliasing  -fno-thread-jumps -fno-tree-builtin-call-dce 
> -fno-tree-pre -fno-tree-switch-conversion  -fno-tree-tail-merge
> -fno-tree-vrp      -DRUBY_EXPORT -D_GNU_SOURCE=1  -I. -I.    -c main.c
> Thanks!

Sorry, it's not as simple as that.  There are places in the compiler
where the optimization level (O1, O2, O3) is just tested with something like

  if (optimize >= level)

for some level.


More information about the Gcc-help mailing list