Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would GCC enable an optimization at O2/O3 when it explicitly says that it will slow the program? [closed]

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

-falign-labels

-falign-labels=n

Align all branch targets to a power-of-two boundary, skipping up to n bytes like -falign-functions. This option can easily make code slower, because it must insert dummy operations for when the branch target is reached in the usual flow of the code.

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

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

If n is not specified or is zero, use a machine-dependent default which is very likely to be ‘1’, meaning no alignment.

Enabled at levels -O2, -O3.

Thinking about this flag more makes it lose even more sense... there are consequences of provoking code cache miss, and what even enabling means when parameter takes numeric value (1..)?

like image 387
LyingOnTheSky Avatar asked Jul 08 '17 20:07

LyingOnTheSky


1 Answers

It doesn't say that. It says that can easily make code slower. It means, that in certain situations, it can make code slower. In other situations, it can make code faster.

Alignment causes to run code slower:

  • increases code size, so there is a higher chance that a code is not in the cache.
  • added nop operations slow down code

Alignment could cause to run code faster: branch prediction, instruction fetch, and god-knows-what.

In the case of a single if, it is hard to say which effect is stronger. It depends on the conditions.

However, for a loop, usually code becomes faster. Why? Because slow factors happen only once, but every cycle of the loop will be executed faster.

(My GCC seems to align labels to 8)

like image 103
geza Avatar answered Oct 03 '22 20:10

geza