可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
On 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 than -O1
, -Os
optimises for speed, …) 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.
With version 3.8 the passes are as follow:
With version 3.7 the passes are as follow (parsed output of the command above):
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):
With version 3.4 the passes are as follow (parsed output of the command above):
With version 3.2 the passes are as follow (parsed output of the command above):
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)
回答2:
@Antoine's answer (and the other question linked) accurately describe the LLVM optimizations that are enabled, but there are a few other Clang-specific options (i.e., those that affect lowering to the AST) that affected by the -O[0|1|2|3|fast]
flags.
You can take a look at these with:
echo 'int;' | clang -xc -O0 - -o /dev/null -\#\#\#
echo 'int;' | clang -xc -O1 - -o /dev/null -\#\#\#
echo 'int;' | clang -xc -O2 - -o /dev/null -\#\#\#
echo 'int;' | clang -xc -O3 - -o /dev/null -\#\#\#
echo 'int;' | clang -xc -Ofast - -o /dev/null -\#\#\#
For example, -O0
enables -mrelax-all
, -O1
enables -vectorize-loops
and -vectorize-slp
, and -Ofast
enables -menable-no-infs
, -menable-no-nans
, -menable-unsafe-fp-math
, -ffp-contract=fast
and -ffast-math
.
@Techogrebo:
Yes, no don't necessarily need the other LLVM tools. Try:
echo 'int;' | clang -xc - -o /dev/null -mllvm -print-all-options
Also, there are a lot more detailed options you can examine/modify with Clang alone... you just need to know how to get to them!
Try a few of:
clang -help
clang -cc1 -help
clang -cc1 -mllvm -help
clang -cc1 -mllvm -help-list-hidden
clang -cc1as -help
回答3:
LLVM 3.6 -O1
Pass Arguments: -targetlibinfo -no-aa -tbaa -scoped-noalias -assumption-cache-tracker -basicaa -notti -verify-di -ipsccp -globalopt -deadargelim -domtree -instcombine -simplifycfg -basiccg -prune-eh -inline-cost -always-inline -functionattrs -sroa -domtree -early-cse -lazy-value-info -jump-threading -correlated-propagation -simplifycfg -domtree -instcombine -tailcallelim -simplifycfg -reassociate -domtree -loops -loop-simplify -lcssa -loop-rotate -licm -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -indvars -loop-idiom -loop-deletion -function_tti -loop-unroll -memdep -memcpyopt -sccp -domtree -instcombine -lazy-value-info -jump-threading -correlated-propagation -domtree -memdep -dse -adce -simplifycfg -domtree -instcombine -barrier -domtree -loops -loop-simplify -lcssa -branch-prob -block-freq -scalar-evolution -loop-vectorize -instcombine -simplifycfg -domtree -instcombine -loops -loop-simplify -lcssa -scalar-evolution -function_tti -loop-unroll -alignment-from-assumptions -strip-dead-prototypes -verify -verify-di
-O2 base on -O1
add : -inline -mldst-motion -domtree -memdep -gvn -memdep -scalar-evolution -slp-vectorizer -globaldce -constmerge
and removes: -always-inline
-O3 based on -O2
add: -argpromotion