1

Must the processor get constant variables' values from memory every time they are used? If constant variables can't be changed, the compiler can replace them with their values, can't it?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user11655900
  • 420
  • 7
  • 15

3 Answers3

4

Yes. Any decent compiler will optimize those loads out and just replace them. For example, with Clang 8.0.0 this source code:

#include <stdio.h>
const int a = 34;

int main()
{
    int z = a;
    printf("%d", z);
}

Gives me this asm:

main:                                   # @main
        push    rax
        mov     edi, offset .L.str
        mov     esi, 34
        xor     eax, eax
        call    printf
        xor     eax, eax
        pop     rcx
        ret
.L.str:
        .asciz  "%d"

Notice how a doesn't exist in the asm, only 34 exists.

In this simple example, a doesn't even have to be const for the compiler to notice it'll never be altered. Removing it would still have the same effect if compiled under optimizations.

Do note that this is not always the case and thus helping the compiler adding const / constexpr is a good thing to do.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

It depends, as there are two types of constants:

  1. Compile-time constants (const variables which are initialized at compilation time, or constexpr variables).
  2. Run-time constants (which are things like arguments that are marked as const).

For the first type (compile-time constants) the compiler can indeed "replace" the variables with the actual values, and that's what all major compilers will do.

For the second type, it's not possible since the values aren't known until run-time.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Even if the variable is not maked as `const` or `constexpr`, it can (and usually does) replace it, depending on the optimization flags. – Paul92 Jul 17 '19 at 15:19
  • @Paul92 Only if the compiler can deduce with 100% certainty that the variable haven't been changed before its use. And such analysis is very hard, so there are many cases where the compiler simply can't do it. – Some programmer dude Jul 17 '19 at 15:21
  • I agree. I was just mentioning that is possible, sometimes, to do this. At least from my experiments, in most of the toy examples, the value was replaced (gcc9 -O3). – Paul92 Jul 17 '19 at 15:25
-1

Another aspect to consider is if that const variable is independent: if the const variable is not used elsewhere by a const pointer or reference then the compiler can optimize it.

But like this case where the variable a is used by a pointer b, the compiler cannot optimize the variable, since the pointer still needs an address to point to.

const int a = 5;
const int* b = &a;
Youcef4k
  • 338
  • 2
  • 13
  • Even this could be optimized depending on what you are doing. see https://godbolt.org/z/qVGNQY. The as-if rule allows a lot of optimizations. – NathanOliver Jul 17 '19 at 15:25
  • The thing about inlining is that it doesn't have to be all or nothing. The constant can be inlined in certain places where it makes sense but still allow the const object to be preserved if it's address is needed. This is one of the reasons `inline` doesn't make sense for inlining decisions, each use case has it's own merits. – François Andrieux Jul 17 '19 at 15:34