HiveBrain v1.2.0
Get Started
← Back to all entries
principleMinor

Constant folding vs. Constant propogation

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
propogationfoldingconstant

Problem

What is the difference between constant folding and constant propogation? They both seem to do the same thing, instead of saving constants into stack or evaluating a full arithmetic expression, they simply replace it with the result which can be obtained at compile time. What is the difference between two?

Solution

Based on these two websites: http://www.compileroptimizations.com/category/constant_folding.htm and
http://www.compileroptimizations.com/category/constant_propagation.htm

The difference is that constant propogation is not saving a variable to the stack, because we know its a constant (like x = 10;) and can simply plug it in everywhere (say if you had an expression y = x + x + x => y = 10 + 10 + 10) it is used in machine code. Whereas, constant folding is simply evaluating expressions that use constants and substituting the result into the machine code (like y = 10 + 10 + 10; => y = 30;)

Context

StackExchange Computer Science Q#79824, answer score: 9

Revisions (0)

No revisions yet.