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

1

7

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..)?

Anthropometry answered 8/7, 2017 at 20:7 Comment(4)
"If n is not specified or is zero, use a machine-dependent default which is very likely to be ‘1’, meaning no alignment." So by default, gcc uses the machine-dependent valueStalker
IIRC it is a size optimization.Fenestrated
This is an interesting question; thanks for posting.Woolgrower
@stybl How can an optimization that requires adding no-ops possibly be a size optimization?Donatus
R
5

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)

Rind answered 9/7, 2017 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.