Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Constant folding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== The optimizations in action == Constant folding and propagation are typically used together to achieve many simplifications and reductions, and their interleaved, iterative application continues until those effects cease. Consider this unoptimized pseudocode returning a number unknown pending analysis: <syntaxhighlight lang="c"> int a = 30; int b = 9 - (a / 5); int c = b * 4; if (c > 10) { c = c - 10; } return c * (60 / a); </syntaxhighlight> Applying constant propagation once, followed by constant folding, yields: <syntaxhighlight lang="c"> int a = 30; int b = 3; int c = b * 4; if (c > 10) { c = c - 10; } return c * 2; </syntaxhighlight> Repeating both steps twice produces: <syntaxhighlight lang="c"> int a = 30; int b = 3; int c = 12; if (true) { c = 2; } return c * 2; </syntaxhighlight> Having replaced all uses of variables <code>a</code> and <code>b</code> with constants, the compiler's [[dead-code elimination]] applies to those variables, leaving: <syntaxhighlight lang="c"> int c = 12; if (true) { c = 2; } return c * 2; </syntaxhighlight> (Boolean constructs vary among languages and compilers, but their details—such as the status, origin, and representation of [[Boolean data type|true]]—do not affect these optimization principles.) Traditional constant propagation produces no further optimization; it does not restructure programs. However, a similar optimization, [[sparse conditional constant propagation]], goes further by selecting the appropriate conditional branch,<ref>{{Citation |last1=Wegman |first1=Mark N |last2=Zadeck |first2=F. Kenneth |title=Constant Propagation with Conditional Branches |journal=ACM Transactions on Programming Languages and Systems |volume=13 |issue=2 |date=April 1991 |pages=181–210 |doi= 10.1145/103135.103136|citeseerx=10.1.1.130.3532 |s2cid=52813828 }}</ref> and removing the always-true conditional test. Thus, variable <code>c</code> becomes redundant, and only an operation on a constant remains: <syntaxhighlight lang="c"> return 4; </syntaxhighlight> If that pseudocode constitutes a function body, the compiler knows the function evaluates to integer constant <code>4</code>, allowing replacement of calls to the function with <code>4</code>, and further increasing program efficiency.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)