LoginSignup
2
2

More than 1 year has passed since last update.

暗黙下に効いているGCCの最適化オプションを確認する

Last updated at Posted at 2023-03-28

gcc -Q --help=optimizerと実行すると、GCCで-O2の様に指定した最適化オプションの裏でどの様なフラグが有効になっているのか確認出来ます。helpの書式は--help={common|optimizers|params|target|warnings|[^]{joined|setarate|undocumented}}[,...]となっており、--help=targetとすると-marchや-mtuneの設定状況も確認できます。

Find out exact gcc implicit options - Stack Overflow

これだけだと短すぎるので各最適化オプションで有効になるフラグを対照表にしておきます。

$ uname -srm
Linux 5.15.0-67-generic x86_64
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
:

GCCのドキュメントの文言を参考にすれば最適化度合いは概ね-O0<-Og<-O1<-Os<-O2<-O3<-Ofastとなっています(-finline-functionsのみ例外)。蛇足ですがデフォルトは-O0で-Oは-O1と等価です。

Optimize Options (Using the GNU Compiler Collection (GCC))

option -O0 -Og -O1 -Os -O2 -O3 -Ofast
-faggressive-loop-optimizations
-falign-functions - - - -
-falign-functions= 16 16 16
-falign-jumps - - - -
-falign-jumps= 16:11:8 16:11:8 16:11:8
-falign-labels - - - -
-falign-labels= 0:0:8 0:0:8 0:0:8
-falign-loops - - - -
-falign-loops= 16:11:8 16:11:8 16:11:8
-fassociative-math - - - - - -
-fassume-phsa
-fasynchronous-unwind-tables
-fauto-inc-dec
-fbranch-count-reg - -
-fbranch-probabilities - - - - - - -
-fbranch-target-load-optimize - - - - - - -
-fbranch-target-load-optimize2 - - - - - - -
-fbtr-bb-exclusive - - - - - - -
-fcaller-saves - - -
-fcode-hoisting - - -
-fcombine-stack-adjustments -
-fcompare-elim -
-fconserve-stack - - - - - - -
-fcprop-registers -
-fcrossjumping - - -
-fcse-follow-jumps - - -
-fcx-fortran-rules - - - - - - -
-fcx-limited-range - - - - - -
-fdce
-fdefer-pop -
-fdelayed-branch - - - - - - -
-fdelete-dead-exceptions - - - - - - -
-fdelete-null-pointer-checks
-fdevirtualize - - -
-fdevirtualize-speculatively - - -
-fdse
-fearly-inlining
-fexceptions - - - - - - -
-fexpensive-optimizations - - -
-ffast-math
-ffinite-math-only - - - - - -
-ffloat-store - - - - - - -
-fforward-propagate -
-ffp-contract=[off|on|fast] fast fast fast fast fast fast fast
-ffp-int-builtin-inexact
-ffunction-cse
-fgcse - - -
-fgcse-after-reload - - - - -
-fgcse-las - - - - - - -
-fgcse-lm
-fgcse-sm - - - - - - -
-fgraphite - - - - - - -
-fgraphite-identity - - - - - - -
-fguess-branch-probability -
-fhandle-exceptions
-fhoist-adjacent-loads - - -
-fif-conversion - -
-fif-conversion2 - -
-findirect-inlining - - -
-finline
-finline-atomics
-finline-functions - - - -
-finline-functions-called-once - -
-finline-small-functions - - -
-fipa-bit-cp - - -
-fipa-cp - - -
-fipa-cp-clone - - - - -
-fipa-icf - - -
-fipa-icf-functions - - -
-fipa-icf-variables - - -
-fipa-profile -
-fipa-pta - - - - - - -
-fipa-pure-const -
-fipa-ra - - -
-fipa-reference -
-fipa-reference-addressable -
-fipa-sra - - -
-fipa-stack-alignment
-fipa-vrp - - -
-fira-algorithm=[CB|priority] CB CB CB CB CB CB CB
-fira-hoist-pressure
-fira-loop-pressure - - - - - - -
-fira-region=[one|all|mixed] [default] [default] [default] [default] [default] [default] [default]
-fira-share-save-slots
-fira-share-spill-slots
-fisolate-erroneous-paths-attribute - - - - - - -
-fisolate-erroneous-paths-dereference - - -
-fivopts
-fjump-tables
-fkeep-gc-roots-live - - - - - - -
-flifetime-dse
-flifetime-dse=<0,2> 2 2 2 2 2 2 2
-flimit-function-alignment - - - - - - -
-flive-patching
-flive-patching=[inline-only-static|inline-clone] [default] [default] [default] [default] [default] [default] [default]
-flive-range-shrinkage - - - - - - -
-floop-interchange - - - - -
-floop-nest-optimize - - - - - - -
-floop-parallelize-all - - - - - - -
-floop-unroll-and-jam - - - - -
-flra-remat - - -
-fmath-errno -
-fmodulo-sched - - - - - - -
-fmodulo-sched-allow-regmoves - - - - - - -
-fmove-loop-invariants - -
-fnon-call-exceptions - - - - - - -
-fnothrow-opt - - - - - - -
-fomit-frame-pointer -
-fopt-info - - - - - - -
-foptimize-sibling-calls - - -
-foptimize-strlen - - - -
-fpack-struct - - - - - - -
-fpack-struct=
-fpartial-inlining - - -
-fpatchable-function-entry=
-fpeel-loops - - - - -
-fpeephole
-fpeephole2 - - -
-fplt
-fpredictive-commoning - - - - -
-fprefetch-loop-arrays
-fprintf-return-value
-freciprocal-math - - - - - -
-freg-struct-return
-frename-registers
-freorder-blocks -
-freorder-blocks-algorithm=[simple|stc] simple simple simple stc stc stc stc
-freorder-blocks-and-partition - - -
-freorder-functions - - -
-frerun-cse-after-loop - - -
-freschedule-modulo-scheduled-loops - - - - - - -
-frounding-math - - - - - - -
-frtti
-fsave-optimization-record - - - - - - -
-fsched-critical-path-heuristic
-fsched-dep-count-heuristic
-fsched-group-heuristic
-fsched-interblock
-fsched-last-insn-heuristic
-fsched-pressure - - - - - - -
-fsched-rank-heuristic
-fsched-spec
-fsched-spec-insn-heuristic
-fsched-spec-load - - - - - - -
-fsched-spec-load-dangerous - - - - - - -
-fsched-stalled-insns - - - - - - -
-fsched-stalled-insns-dep
-fsched-stalled-insns-dep=
-fsched-stalled-insns=
-fsched2-use-superblocks - - - - - - -
-fschedule-fusion
-fschedule-insns - - - - - - -
-fschedule-insns2 - - -
-fsection-anchors - - - - - - -
-fsel-sched-pipelining - - - - - - -
-fsel-sched-pipelining-outer-loops - - - - - - -
-fsel-sched-reschedule-pipelined - - - - - - -
-fselective-scheduling - - - - - - -
-fselective-scheduling2 - - - - - - -
-fshort-enums
-fshort-wchar - - - - - - -
-fshrink-wrap -
-fshrink-wrap-separate
-fsignaling-nans - - - - - - -
-fsigned-zeros -
-fsimd-cost-model=[unlimited|dynamic|cheap] unlimited unlimited unlimited unlimited unlimited unlimited unlimited
-fsingle-precision-constant - - - - - - -
-fsplit-ivs-in-unroller
-fsplit-loops - - - - -
-fsplit-paths - - - - -
-fsplit-wide-types -
-fssa-backprop
-fssa-phiopt - -
-fstack-check=[no|generic|specific]
-fstack-clash-protection - - - - - - -
-fstack-protector - - - - - - -
-fstack-protector-all - - - - - - -
-fstack-protector-explicit - - - - - - -
-fstack-protector-strong - - - - - - -
-fstack-reuse=[all|named_vars|none] all all all all all all all
-fstdarg-opt
-fstore-merging - - -
-fstrict-aliasing - - -
-fstrict-enums - - - - - - -
-fstrict-volatile-bitfields
-fthread-jumps - - -
-fno-threadsafe-statics
-ftracer - - - - - - -
-ftrapping-math -
-ftrapv - - - - - - -
-ftree-bit-ccp - -
-ftree-builtin-call-dce -
-ftree-ccp -
-ftree-ch -
-ftree-coalesce-vars -
-ftree-copy-prop -
-ftree-cselim
-ftree-dce -
-ftree-dominator-opts -
-ftree-dse -
-ftree-forwprop
-ftree-fre -
-ftree-loop-distribute-patterns - - - - -
-ftree-loop-distribution - - - - -
-ftree-loop-if-convert
-ftree-loop-im
-ftree-loop-ivcanon
-ftree-loop-optimize
-ftree-loop-vectorize - - - - -
-ftree-lrs - - - - - - -
-ftree-parallelize-loops= 1 1 1 1 1 1 1
-ftree-partial-pre - - - - -
-ftree-phiprop
-ftree-pre - - -
-ftree-pta - -
-ftree-reassoc
-ftree-scev-cprop
-ftree-sink -
-ftree-slp-vectorize - - - - -
-ftree-slsr -
-ftree-sra - -
-ftree-switch-conversion - - -
-ftree-tail-merge - - -
-ftree-ter -
-ftree-vectorize
-ftree-vrp - - -
-funconstrained-commons - - - - - - -
-funroll-all-loops - - - - - - -
-funroll-loops - - - - - - -
-funsafe-math-optimizations - - - - - -
-funswitch-loops - - - - -
-funwind-tables
-fvar-tracking
-fvar-tracking-assignments
-fvar-tracking-assignments-toggle - - - - - - -
-fvar-tracking-uninit - - - - - - -
-fvariable-expansion-in-unroller - - - - - - -
-fvect-cost-model=[unlimited|dynamic|cheap] [default] [default] [default] cheap cheap dynamic dynamic
-fversion-loops-for-strides - - - - -
-fvpt - - - - - - -
-fweb
-fwrapv - - - - - - -
-fwrapv-pointer - - - - - - -

参考 - データ取得に使ったスクリプト

粗があるのはご容赦願います。

#!/bin/bash

gcc -Q --help=optimizer     | tail -n +6 | awk '{print $3}' >> blank
echo "option" > header
echo ":---" >> header
gcc -Q --help=optimizer -O0 | tail -n +6 | awk '{print $1}' >> header
sed -i -e "s/|/\\\|/g" header
echo "-O0" > O0
echo ":--:" >> O0
gcc -Q --help=optimizer -O0 | tail -n +6 | awk '{print $2}' >> O0
echo "-Og" > Og
echo ":--:" >> Og
gcc -Q --help=optimizer -Og | tail -n +6 | awk '{print $2}' >> Og
echo "-O1" > O1
echo ":--:" >> O1
gcc -Q --help=optimizer -O1 | tail -n +6 | awk '{print $2}' >> O1
echo "-Os" > Os
echo ":--:" >> Os
gcc -Q --help=optimizer -Os | tail -n +6 | awk '{print $2}' >> Os
echo "-O2" > O2
echo ":--:" >> O2
gcc -Q --help=optimizer -O2 | tail -n +6 | awk '{print $2}' >> O2
echo "-O3" > O3
echo ":--:" >> O3
gcc -Q --help=optimizer -O3 | tail -n +6 | awk '{print $2}' >> O3
echo "-Ofast" > Ofast
echo ":--:" >> Ofast
gcc -Q --help=optimizer -Ofast | tail -n +6 | awk '{print $2}' >> Ofast
paste -d "|" blank header O0 Og O1 Os O2 O3 Ofast blank > opt_table.txt
sed -i -e "s/\[enabled\]/○/g" opt_table.txt
sed -i -e "s/\[disabled\]/-/g" opt_table.txt
rm blank header O0 Og O1 Os O2 O3 Ofast
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2