For gcc, the manual explains what -O3
, -Os
, etc. translate to in terms of specific optimisation arguments (-funswitch-loops
, -fcompare-elim
, etc.)
I'm looking for the same info for clang.
I've looked online and in man clang
which only gives general information (-O2
optimises more aggressively than -O1
, -Os
optimises for size, ...) and also looked here on Stack Overflow and found this, but I haven't found anything relevant in the cited source files.
Edit: I found an answer but I'm still interested if anyone has a link to a user-manual documenting all optimisation passes and the passes selected by -Ox
. Currently I just found this list of passes, but nothing on optimisation levels.
ベストアンサー1
I found this related question.
To sum it up, to find out about compiler optimization passes:
llvm-as < /dev/null | opt -O3 -disable-output -debug-pass=Arguments
As pointed out in Geoff Nixon's answer (+1), clang
additionally runs some higher level optimizations, which we can retrieve with:
echo 'int;' | clang -xc -O3 - -o /dev/null -\#\#\#
Documentation of individual passes is available here.
You can compare the effect of changing high-level flags such as -O
like this:
diff -wy --suppress-common-lines \
<(echo 'int;' | clang -xc - -o /dev/null -\#\#\# 2>&1 | tr " " "\n" | grep -v /tmp) \
<(echo 'int;' | clang -xc -O0 - -o /dev/null -\#\#\# 2>&1 | tr " " "\n" | grep -v /tmp)
# will tell you that -O0 is indeed the default.
With version 6.0 the passes are as follow:
baseline (
-O0
):opt
sets: -tti -verify -ee-instrument -targetlibinfo -assumption-cache-tracker -profile-summary-info -forceattrs -basiccg -always-inline -barrierclang
adds : -mdisable-fp-elim -mrelax-all
-O1
is based on-O0
opt
adds: -targetlibinfo -tti -tbaa -scoped-noalias -assumption-cache-tracker -profile-summary-info -forceattrs -inferattrs -ipsccp -called-value-propagation -globalopt -domtree -mem2reg -deadargelim -basicaa -aa -loops -lazy-branch-prob -lazy-block-freq -opt-remark-emitter -instcombine -simplifycfg -basiccg -globals-aa -prune-eh -always-inline -functionattrs -sroa -memoryssa -early-cse-memssa -speculative-execution -lazy-value-info -jump-threading -correlated-propagation -libcalls-shrinkwrap -branch-prob -block-freq -pgo-memop-opt -tailcallelim -reassociate -loop-simplify -lcssa-verification -lcssa -scalar-evolution -loop-rotate -licm -loop-unswitch -indvars -loop-idiom -loop-deletion -loop-unroll -memdep -memcpyopt -sccp -demanded-bits -bdce -dse -postdomtree -adce -barrier -rpo-functionattrs -globaldce -float2int -loop-accesses -loop-distribute -loop-vectorize -loop-load-elim -alignment-from-assumptions -strip-dead-prototypes -loop-sink -instsimplify -div-rem-pairs -verify -ee-instrument -early-cse -lower-expectclang
adds : -momit-leaf-frame-pointerclang
drops : -mdisable-fp-elim -mrelax-all
-O2
is based on-O1
opt
adds: -inline -mldst-motion -gvn -elim-avail-extern -slp-vectorizer -constmergeopt
drops: -always-inlineclang
adds: -vectorize-loops -vectorize-slp
-O3
is based on-O2
opt
adds: -callsite-splitting -argpromotion
-Ofast
is based on-O3
, valid inclang
but not inopt
clang
adds: -fno-signed-zeros -freciprocal-math -ffp-contract=fast -menable-unsafe-fp-math -menable-no-nans -menable-no-infs -mreassociate -fno-trapping-math -ffast-math -ffinite-math-only
-Os
is similar to-O2
opt
drops: -libcalls-shrinkwrap and -pgo-memopt-opt
-Oz
is based on-Os
opt
drops: -slp-vectorizer
With version 3.8 the passes are as follow:
baseline (
-O0
):opt
sets : -targetlibinfo -tti -verifyclang
adds : -mdisable-fp-elim -mrelax-all
-O1
is based on-O0
opt
adds: -globalopt -demanded-bits -branch-prob -inferattrs -ipsccp -dse -loop-simplify -scoped-noalias -barrier -adce -deadargelim -memdep -licm -globals-aa -rpo-functionattrs -basiccg -loop-idiom -forceattrs -mem2reg -simplifycfg -early-cse -instcombine -sccp -loop-unswitch -loop-vectorize -tailcallelim -functionattrs -loop-accesses -memcpyopt -loop-deletion -reassociate -strip-dead-prototypes -loops -basicaa -correlated-propagation -lcssa -domtree -always-inline -aa -block-freq -float2int -lower-expect -sroa -loop-unroll -alignment-from-assumptions -lazy-value-info -prune-eh -jump-threading -loop-rotate -indvars -bdce -scalar-evolution -tbaa -assumption-cache-trackerclang
adds : -momit-leaf-frame-pointerclang
drops : -mdisable-fp-elim -mrelax-all
-O2
is based on-O1
opt
adds: -elim-avail-extern -mldst-motion -slp-vectorizer -gvn -inline -globaldce -constmergeopt
drops: -always-inlineclang
adds: -vectorize-loops -vectorize-slp
-O3
is based on-O2
opt
adds: -argpromotion
-Ofast
is based on-O3
, valid inclang
but not inopt
clang
adds: -fno-signed-zeros -freciprocal-math -ffp-contract=fast -menable-unsafe-fp-math -menable-no-nans -menable-no-infs
-Os
is the same as-O2
-Oz
is based on-Os
opt
drops: -slp-vectorizerclang
drops: -vectorize-loops
----------
With version 3.7 the passes are as follow (parsed output of the command above):
default (-O0): -targetlibinfo -verify -tti
-O1 is based on -O0
- adds: -sccp -loop-simplify -float2int -lazy-value-info -correlated-propagation -bdce -lcssa -deadargelim -loop-unroll -loop-vectorize -barrier -memcpyopt -loop-accesses -assumption-cache-tracker -reassociate -loop-deletion -branch-prob -jump-threading -domtree -dse -loop-rotate -ipsccp -instcombine -scoped-noalias -licm -prune-eh -loop-unswitch -alignment-from-assumptions -early-cse -inline-cost -simplifycfg -strip-dead-prototypes -tbaa -sroa -no-aa -adce -functionattrs -lower-expect -basiccg -loops -loop-idiom -tailcallelim -basicaa -indvars -globalopt -block-freq -scalar-evolution -memdep -always-inline
-O2 is based on -01
- adds: -elim-avail-extern -globaldce -inline -constmerge -mldst-motion -gvn -slp-vectorizer
- removes: -always-inline
-O3 is based on -O2
- adds: -argpromotion -verif
-Os is identical to -O2
-Oz is based on -Os
- removes: -slp-vectorizer
----------
For version 3.6 the passes are as documented in GYUNGMIN KIM's post.
----------
With version 3.5 the passes are as follow (parsed output of the command above):
default (-O0): -targetlibinfo -verify -verify-di
-O1 is based on -O0
- adds: -correlated-propagation -basiccg -simplifycfg -no-aa -jump-threading -sroa -loop-unswitch -ipsccp -instcombine -memdep -memcpyopt -barrier -block-freq -loop-simplify -loop-vectorize -inline-cost -branch-prob -early-cse -lazy-value-info -loop-rotate -strip-dead-prototypes -loop-deletion -tbaa -prune-eh -indvars -loop-unroll -reassociate -loops -sccp -always-inline -basicaa -dse -globalopt -tailcallelim -functionattrs -deadargelim -notti -scalar-evolution -lower-expect -licm -loop-idiom -adce -domtree -lcssa
-O2 is based on -01
- adds: -gvn -constmerge -globaldce -slp-vectorizer -mldst-motion -inline
- removes: -always-inline
-O3 is based on -O2
- adds: -argpromotion
-Os is identical to -O2
-Oz is based on -Os
- removes: -slp-vectorizer
----------
With version 3.4 the passes are as follow (parsed output of the command above):
-O0: -targetlibinfo -preverify -domtree -verify
-O1 is based on -O0
- adds: -adce -always-inline -basicaa -basiccg -correlated-propagation -deadargelim -dse -early-cse -functionattrs -globalopt -indvars -inline-cost -instcombine -ipsccp -jump-threading -lazy-value-info -lcssa -licm -loop-deletion -loop-idiom -loop-rotate -loop-simplify -loop-unroll -loop-unswitch -loops -lower-expect -memcpyopt -memdep -no-aa -notti -prune-eh -reassociate -scalar-evolution -sccp -simplifycfg -sroa -strip-dead-prototypes -tailcallelim -tbaa
-O2 is based on -01
- adds: -barrier -constmerge -domtree -globaldce -gvn -inline -loop-vectorize -preverify -slp-vectorizer -targetlibinfo -verify
- removes: -always-inline
-O3 is based on -O2
- adds: -argpromotion
-Os is identical to -O2
-Oz is based on -O2
- removes: -barrier -loop-vectorize -slp-vectorizer
----------
With version 3.2 the passes are as follow (parsed output of the command above):
-O0: -targetlibinfo -preverify -domtree -verify
-O1 is based on -O0
- adds: -sroa -early-cse -lower-expect -no-aa -tbaa -basicaa -globalopt -ipsccp -deadargelim -instcombine -simplifycfg -basiccg -prune-eh -always-inline -functionattrs -simplify-libcalls -lazy-value-info -jump-threading -correlated-propagation -tailcallelim -reassociate -loops -loop-simplify -lcssa -loop-rotate -licm -loop-unswitch -scalar-evolution -indvars -loop-idiom -loop-deletion -loop-unroll -memdep -memcpyopt -sccp -dse -adce -strip-dead-prototypes
-O2 is based on -01
- adds: -inline -globaldce -constmerge
- removes: -always-inline
-O3 is based on -O2
- adds: -argpromotion
-Os is identical to -O2
-Oz is identical to -Os
-------------
Edit [march 2014] removed duplicates from lists.
Edit [april 2014] added documentation link + options for 3.4
Edit [2014年9月]3.5 のオプションを追加
編集 [2015年12月]3.7 のオプションを追加し、3.6 の既存の回答について言及しました
編集 [2016年5月]3.8 のオプションを opt と clang の両方に追加し、clang (opt と比較) の既存の回答について言及しました。
編集 [2018年11月]6.0のオプションを追加