GCC 4.7の最適化オプション比較表

GCCの各オプティマイズレベルで有効になる最適化を調べる - Life is very short


GCC 4.7版。"-Ofast"を追加しました。"-Ofast"は規約を一部
無視した最適化を行うようです。"-O3"より高速になるよう意図
されています。


なお GCC 4.8では "-Og"という最適化とデバッグ機能を兼ね備えた
オプションが追加されるそうです。需要がわかりづらいですが・・・。

表生成スクリプト

#!/usr/bin/env perl
use strict;
use warnings;

$ENV{LANG} = 'C';

my @levels = qw(-O0 -O1 -O2 -O3 -Os -Ofast);

my @base = qw(gcc -- -Q --help=optimizers);

my %optimize;
for my $level (@levels) {
    my @cmd = @base;
    $cmd[1] = $level;

    open my $fh, "-|", @cmd or die "Can't exec @cmd";

    while (my $line = <$fh>) {
        chomp $line;

        next unless $line =~ m{(?:enabled|disabled)};

        if ($line =~ m{(\S+) \s+ \[ ([^\]]+) \]}xms) {
            my ($option, $flag) = ($1, $2);

            if ($flag eq 'enabled') {
                $optimize{$level}->{$option} = 1;
            } else {
                $optimize{$level}->{$option} = 0;
            }
        }
    }
    close $fh;
}

print "|*|";
print "*$_|" for @levels;
print "\n";

for my $key (sort keys %{$optimize{'-O0'}}) {
    print "|$key|";
    for my $level (@levels) {
        printf "%s|",  $optimize{$level}->{$key} == 1 ? "o" : 'x';
    }
    print "\n";
}

比較表

-O0 -O1 -O2 -O3 -Os -Ofast
-falign-functions x x o o o o
-falign-jumps x x o o o o
-falign-labels x x o o o o
-falign-loops x x o o o o
-fasynchronous-unwind-tables o o o o o o
-fbranch-count-reg o o o o o o
-fbranch-probabilities x x x x x x
-fbranch-target-load-optimize x x x x x x
-fbranch-target-load-optimize2 x x x x x x
-fbtr-bb-exclusive x x x x x x
-fcaller-saves x x o o o o
-fcombine-stack-adjustments x o o o o o
-fcommon o o o o o o
-fcompare-elim x o o o o o
-fconserve-stack x x x x x x
-fcprop-registers x o o o o o
-fcrossjumping x x o o o o
-fcse-follow-jumps x x o o o o
-fcx-fortran-rules x x x x x x
-fcx-limited-range x x x x x o
-fdata-sections x x x x x x
-fdce o o o o o o
-fdefer-pop x o o o o o
-fdelayed-branch x x x x x x
-fdelete-null-pointer-checks o o o o o o
-fdevirtualize x x o o o o
-fdse o o o o o o
-fearly-inlining o o o o o o
-fexceptions x x x x x x
-fexpensive-optimizations x x o o o o
-ffinite-math-only x x x x x o
-ffloat-store x x x x x x
-fforward-propagate x o o o o o
-fgcse x x o o o o
-fgcse-after-reload x x x o x o
-fgcse-las x x x x x x
-fgcse-lm o o o o o o
-fgcse-sm x x x x x x
-fgraphite-identity x x x x x x
-fguess-branch-probability x o o o o o
-fif-conversion x o o o o o
-fif-conversion2 x o o o o o
-finline-atomics o o o o o o
-finline-functions x x x o o o
-finline-functions-called-once x o o o o o
-finline-small-functions x x o o o o
-fipa-cp x x o o o o
-fipa-cp-clone x x x o x o
-fipa-matrix-reorg x x x x x x
-fipa-profile x o o o o o
-fipa-pta x x x x x x
-fipa-pure-const x o o o o o
-fipa-reference x o o o o o
-fipa-sra x x o o o o
-fivopts o o o o o o
-fjump-tables o o o o o o
-floop-block x x x x x x
-floop-flatten x x x x x x
-floop-interchange x x x x x x
-floop-parallelize-all x x x x x x
-floop-strip-mine x x x x x x
-fmath-errno o o o o o x
-fmerge-all-constants x x x x x x
-fmerge-constants x o o o o o
-fmodulo-sched x x x x x x
-fmove-loop-invariants o o o o o o
-fno-threadsafe-statics o o o o o o
-fnon-call-exceptions x x x x x x
-fnothrow-opt x x x x x x
-fomit-frame-pointer x x x x x x
-foptimize-register-move x x o o o o
-foptimize-sibling-calls x x o o o o
-foptimize-strlen x x o o x o
-fpack-struct x x x x x x
-fpeel-loops x x x x x x
-fpeephole o o o o o o
-fpeephole2 x x o o o o
-fpredictive-commoning x x x o x o
-fprefetch-loop-arrays o o o o o o
-freg-struct-return x x x x x x
-fregmove x x o o o o
-frename-registers o o o o o o
-freorder-blocks x x o o o o
-freorder-blocks-and-partition x x x x x x
-freorder-functions x x o o o o
-frerun-cse-after-loop x x o o o o
-freschedule-modulo-scheduled-loops x x x x x x
-frounding-math x x x x x x
-frtti o o o o o o
-fsched-critical-path-heuristic o o o o o o
-fsched-dep-count-heuristic o o o o o o
-fsched-group-heuristic o o o o o o
-fsched-interblock o o o o o o
-fsched-last-insn-heuristic o o o o o o
-fsched-pressure x x x x x x
-fsched-rank-heuristic o o o o o o
-fsched-spec o o o o o o
-fsched-spec-insn-heuristic o o o o o o
-fsched-spec-load x x x x x x
-fsched-spec-load-dangerous x x x x x x
-fsched-stalled-insns x x x x x x
-fsched-stalled-insns-dep o o o o o o
-fsched2-use-superblocks x x x x x x
-fschedule-insns x x x x x x
-fschedule-insns2 x x o o o o
-fsection-anchors x x x x x x
-fsel-sched-pipelining x x x x x x
-fsel-sched-pipelining-outer-loops x x x x x x
-fsel-sched-reschedule-pipelined x x x x x x
-fselective-scheduling x x x x x x
-fselective-scheduling2 x x x x x x
-fshort-double x x x x x x
-fshort-enums o o o o o o
-fshort-wchar x x x x x x
-fshrink-wrap x o o o o o
-fsignaling-nans x x x x x x
-fsigned-zeros o o o o o x
-fsingle-precision-constant x x x x x x
-fsplit-ivs-in-unroller o o o o o o
-fsplit-wide-types x o o o o o
-fstrict-aliasing x x o o o o
-fstrict-enums x x x x x x
-fthread-jumps x x o o o o
-ftoplevel-reorder o o o o o o
-ftrapping-math o o o o o x
-ftrapv x x x x x x
-ftree-bit-ccp x o o o o o
-ftree-builtin-call-dce x x o o o o
-ftree-ccp x o o o o o
-ftree-ch x o o o o o
-ftree-copy-prop x o o o o o
-ftree-copyrename x o o o o o
-ftree-cselim o o o o o o
-ftree-dce x o o o o o
-ftree-dominator-opts x o o o o o
-ftree-dse x o o o o o
-ftree-forwprop o o o o o o
-ftree-fre x o o o o o
-ftree-loop-distribute-patterns x x x o x o
-ftree-loop-distribution x x x x x x
-ftree-loop-if-convert o o o o o o
-ftree-loop-if-convert-stores x x x x x x
-ftree-loop-im o o o o o o
-ftree-loop-ivcanon o o o o o o
-ftree-loop-optimize o o o o o o
-ftree-lrs x x x x x x
-ftree-partial-pre x x x o x o
-ftree-phiprop o o o o o o
-ftree-pre x x o o o o
-ftree-pta o o o o o o
-ftree-reassoc o o o o o o
-ftree-scev-cprop o o o o o o
-ftree-sink x o o o o o
-ftree-slp-vectorize o o o o o o
-ftree-sra x o o o o o
-ftree-switch-conversion x x o o o o
-ftree-tail-merge x x o o o o
-ftree-ter x o o o o o
-ftree-vect-loop-version o o o o o o
-ftree-vectorize x x x o x o
-ftree-vrp x x o o o o
-funit-at-a-time o o o o o o
-funroll-all-loops x x x x x x
-funroll-loops x x x x x x
-funsafe-loop-optimizations x x x x x x
-funsafe-math-optimizations x x x x x o
-funswitch-loops x x x o x o
-funwind-tables x x x x x x
-fvar-tracking o o o o o o
-fvar-tracking-assignments o o o o o o
-fvar-tracking-assignments-toggle x x x x x x
-fvar-tracking-uninit x x x x x x
-fvariable-expansion-in-unroller x x x x x x
-fvect-cost-model o o o o o o
-fvpt x x x x x x
-fweb o o o o o o
-fwhole-program x x x x x x
-fwrapv x x x x x x