分类:
2008-10-13 16:30:16
A optimisation technique where constant subexpressions are evaluated at compile time. This is usually only applied to built-in numerical and operators whereas is more general in that expressions involving user-defined functions may also be evaluated at compile time.
Constant propagation is the process of substituting the values of known constants in expressions at compile time. Such constants include those defined above, as well as applied to constant values. Consider the following pseudocode:
int x = 14; int y = 7 - x / 2; return y * (28 / x + 2);
Applying constant propagation once yields:
int x = 14; int y = 7 - 14 / 2; return y * (28 / 14 + 2);
My Misunderstanding: Previously I thought Const Folding will store different const vairables with same value in the same memory at run time.
But I'm wrong, the actual case is:
Const Folding doesn't mean that the different const vairable with the same value in different compilaton unit will be
stored in the same memory. I can give an example to show that. By default, const objects are local to the file
in which they are defined, so it is legal to put their definition in a header file.There is one important implication of
this behavior. When we define a const in a header file, every source file that includes that header has its own const
variable with the same name and value. But they are not saved in the same storage.