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.
I found this related question.
To sum it up, to find out about compiler optimization passes:
As pointed out in Geoff Nixon's answer (+1),
clangadditionally runs some higher level optimizations, which we can retrieve with:Documentation of individual passes is available here.
You can compare the effect of changing high-level flags such as
-Olike this:With version 6.0 the passes are as follow:
baseline (
-O0):optsets: -tti -verify -ee-instrument -targetlibinfo -assumption-cache-tracker -profile-summary-info -forceattrs -basiccg -always-inline -barrierclangadds : -mdisable-fp-elim -mrelax-all-O1is based on-O0optadds: -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-expectclangadds : -momit-leaf-frame-pointerclangdrops : -mdisable-fp-elim -mrelax-all-O2is based on-O1optadds: -inline -mldst-motion -gvn -elim-avail-extern -slp-vectorizer -constmergeoptdrops: -always-inlineclangadds: -vectorize-loops -vectorize-slp-O3is based on-O2optadds: -callsite-splitting -argpromotion-Ofastis based on-O3, valid inclangbut not inoptclangadds: -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-Osis similar to-O2optdrops: -libcalls-shrinkwrap and -pgo-memopt-opt-Ozis based on-Osoptdrops: -slp-vectorizerWith version 3.8 the passes are as follow:
baseline (
-O0):optsets : -targetlibinfo -tti -verifyclangadds : -mdisable-fp-elim -mrelax-all-O1is based on-O0optadds: -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-trackerclangadds : -momit-leaf-frame-pointerclangdrops : -mdisable-fp-elim -mrelax-all-O2is based on-O1optadds: -elim-avail-extern -mldst-motion -slp-vectorizer -gvn -inline -globaldce -constmergeoptdrops: -always-inlineclangadds: -vectorize-loops -vectorize-slp-O3is based on-O2optadds: -argpromotion-Ofastis based on-O3, valid inclangbut not inoptclangadds: -fno-signed-zeros -freciprocal-math -ffp-contract=fast -menable-unsafe-fp-math -menable-no-nans -menable-no-infs-Osis the same as-O2-Ozis based on-Osoptdrops: -slp-vectorizerclangdrops: -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
-O2 is based on -01
-O3 is based on -O2
-Os is identical to -O2
-Oz is based on -Os
----------
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
-O2 is based on -01
-O3 is based on -O2
-Os is identical to -O2
-Oz is based on -Os
----------
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
-O2 is based on -01
-O3 is based on -O2
-Os is identical to -O2
-Oz is based on -O2
----------
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
-O2 is based on -01
-O3 is based on -O2
-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 [september 2014] added options for 3.5
Edit [december 2015] added options for 3.7 and mention existing answer for 3.6
Edit [may 2016] added options for 3.8, for both opt and clang and mention existing answer for clang (versus opt)
Edit [nov 2018] add options for 6.0