clangcompiler-optimizationuser-manual

Clang optimization levels


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.


Solution

  • 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:


    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)

    Edit [nov 2018] add options for 6.0