My searches have turned up blank on this question... There are plenty of discussions of how const may be helpful in compiler optimization by signaling read-only access of a variable, but I can't find an answer to the question as posed in the title. I'm interested in the question because I want to avoid thinking about using const for optimization if the compiler can do that anyway.
I appreciate that even if the compiler is able to find unchanging values and optimize them to read-only access, there will be cases where use of const would still be helpful. I'm just looking for an answer on compiler capability in broad terms - does the GCC optimizer look for unchanging values without the use of const?
My GCC, with -O3 compiles the following code
to equivalent of
Clearly the first loop was unrolled even without
const-qualifiedcount1, because it has internal linkage.count2has external linkage, and it will be impossible for the compiler to prove that some other translation unit that you link with this translation unit would not modify the static variable in some constructor function beforemainwas executed and the optimization would be disabled.count3isconst-qualified. The compiler knows that no other translation unit can change its value either and the loop will be unrolled, despitecount3having external linkage, and the same variable being visible to other translation units.