Compare gcc or g++ Compiler Optimization Options

Compare gcc or g++ Compiler Optimization Options

Compiler optimization levels enable different groups of optimization options. Examining the differences between optimization levels helps identify which optimizations are introduced, disabled, or modified when switching from one optimization profile to another. This can assist with performance analysis, troubleshooting, and understanding compiler behavior. This tutorial demonstrates how to compare gcc or g++ compiler optimization options.

The following command compares optimization options enabled by -O2 and -O3:

diff -y -W150 --suppress-common-lines \
  <(gcc -Q -O2 --help=optimizers | tr -s '\t' ' ') \
  <(gcc -Q -O3 --help=optimizers | tr -s '\t' ' ')

Other optimization levels, such as -O0, -O1, and -Os, can also be substituted to compare their respective optimization options.

Command breakdown:

  • -Q - displays the current state of compiler options (e.g., enabled/disabled) instead of only listing them.
  • -O2 and -O3 - select the optimization levels whose options are being compared.
  • --help=optimizers - prints optimization-related compiler options.
  • tr -s '\t' ' ' - replaces repeated tab characters with single spaces to produce consistent formatting.
  • diff -y - presents both outputs side by side for easier comparison.
  • -W150 - increases the comparison width to 150 characters, reducing line wrapping.
  • --suppress-common-lines - hides identical entries so that only differing optimization options are displayed.

A sample output is shown below:

-fgcse-after-reload [disabled]       |  -fgcse-after-reload [enabled]
-fipa-cp-clone [disabled]            |  -fipa-cp-clone [enabled]
-floop-interchange [disabled]        |  -floop-interchange [enabled]
-floop-unroll-and-jam [disabled]     |  -floop-unroll-and-jam [enabled]
-fpeel-loops [disabled]              |  -fpeel-loops [enabled]
-fpredictive-commoning [disabled]    |  -fpredictive-commoning [enabled]
-fsplit-loops [disabled]             |  -fsplit-loops [enabled]
...

The same approach works with the g++ compiler.

Leave a Comment

Cancel reply

Your email address will not be published.