Giovanni Funchal
Giovanni Funchal

Reputation: 9170

Influencing function cloning/duplication/constant propagation in gcc

When running gcc with optimizations-on, it clones (duplicates) C functions when it considers that the function is in a hot path or there's constants propagating to the function arguments.

More specifically, this seems to be controlled by the fipa-cp-clone option.

Is there any way to influence this? For instance mark one parameter with some attribute, as a compile-time constant (like you can do in C++ with a template parameter) which will cause the function to be cloned?

Upvotes: 6

Views: 1631

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215201

What matters is whether the function is called with a constant argument (either an actual constant expression, or something determined to be constant by the compiler via constant propagation). In this case, GCC will clone the function unless it determines doing so would be too costly or have too little benefit; I don't know a way to influence that metric. Be aware that constant propagation happens only within a single translation unit (source file) unless you're compiling the whole program at once or using link-time optimization, and I'm not sure whether cloning can still happen at that point or not.

My best guess, if cloning isn't happening when you expect that it should, is that GCC is never seeing a constant argument where the function is called. Even if you know it will be constant, the compiler might not be able to prove it is.

Upvotes: 7

Related Questions