Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Constant folding
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Constant folding
(section)
Add topic