Why does GCC allow O2 / O3 optimization when it explicitly says that it will slow down the program?

Quote from https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html :

-falign tag

-falign-tags = n

Align all the branch targets with the border of the two sides, skipping to n bytes, for example -falign-functions. This option can easily make the code slower , because it should insert dummy operations when the transition goal is reached in a regular code stream.

-fno-align-labels and -falign-labels = 1 are equivalent and mean that the labels are not aligned.

If -falign-loops or -falign-jumps are applicable and more than this value, then their values ​​are used instead.

If n is not specified or is equal to zero, use a machine-dependent default which is likely to be "1", which means no alignment.

It is included at the levels -O2, -O3.

Thinking about this flag further reduces its meaning ... there are consequences of provoking gaps in the code cache, and what is even an enabler when the parameter takes a numerical value (1 ..)?

+7
c ++ compiler-optimization gcc memory-alignment
source share
1 answer

He does not talk about it. It says it can easily make code slower . This means that in certain situations it can make the code slower. In other situations, it can make the code faster.

Alignment slows down the code:

  • increases the size of the code, so the likelihood that the code is not in the cache is higher.
  • added nop code slowdown operations

Alignment can lead to quick code execution: branch prediction, fetching commands and god-knows-what.

In the case of one if it is difficult to say which effect is stronger. It depends on the conditions.

However, for a loop, usually the code gets faster. What for? Since slow factors occur only once, but each cycle of the cycle will be faster.

(My GCC seems to align labels to 8)

+5
source share

All Articles