📜 ⬆️ ⬇️

Optimizing GCC Compilation by Gentoo Example

Optimization of the assembly is one of the main delights of Gentoo, but everything described above is applicable to any case of compiling software from source codes. All build parameters in Gentoo are set in the make.conf file.
In fact, we are only interested in the variable CFLAGS . CXXFLAGS should be equal to CXXFLAGS="${CFLAGS}" , and MAKEOPTS only indicates the number of parallelly run compilation processes (usually the total number of processor cores is + 1).

The default is "-O2 -pipe" . The -O parameter indicates the level of optimization used. The second level is considered the highest safe, with the third level is often manifested instability of work. The -pipe parameter instructs gcc to use in-memory channels instead of temporary files to exchange data between different stages of compilation. With the default value, binary files are built under the generic architecture, they work on different processor models, but do not take advantage of one or another model.
You can view secure sets of optimization flags for various processor types here: http://en.gentoo-wiki.com/wiki/Safe_Cflags . Binaries compiled with the flags specified there should work without any problems on the corresponding processor and at the same time use most of its advantages.
In gcc, starting with version 4.2, the -march=native flag has appeared. With this flag, gcc automatically detects the type of processor, supported features, and uses them.
In general, to optimize, do the following:

As a result, see what parameters gcc will use with your optimizations:
  gcc <your_options> -Q --help = target -fverbose-asm 

  gcc <your_options> -Q --help = optimizers -fverbose-asm 

Without the -fverbose-asm flag, gcc shows incorrect data. This is discussed here: http://www.gentoo.ru/node/14818 .

And this script will show that it will go to the compiler command line: gcc <your_options> -E -v - </dev/null 2>&1 | sed -n 's/.* -v - //p' gcc <your_options> -E -v - </dev/null 2>&1 | sed -n 's/.* -v - //p' . Interest is, perhaps, only for developers.

')

Source: https://habr.com/ru/post/108311/


All Articles