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
Loop unswitching
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!
{{Short description|Compiler optimization for conditionals in loops}} '''Loop unswitching''' is a [[compiler optimization]]. It moves a conditional inside a loop outside of it by duplicating the loop's body, and placing a version of it inside each of the if and else clauses of the conditional.<ref>{{Cite book|url=https://books.google.com/books?id=4yVQFVvsBNAC&q=%22Loop+unswitching%22&pg=PA520|title=Engineering a Compiler|isbn=9781558606982|last1=Cooper|first1=Keith|last2=Torczon|first2=Linda|year=2004|publisher=Elsevier }}</ref> This can improve the parallelization of the loop. Since modern processors can operate quickly on vectors, this improvement increases the speed of the program. Here is a simple example. Suppose we want to add the two arrays ''x'' and ''y'' and also do something depending on the variable ''w''. We have the following [[C (programming language)|C]] code: <syntaxhighlight lang=c> int i, w, x[1000], y[1000]; for (i = 0; i < 1000; i++) { x[i] += y[i]; if (w) y[i] = 0; } </syntaxhighlight> The conditional inside this loop makes it difficult to safely [[automatic parallelization|parallelize]] this loop. When we unswitch the loop, this becomes: <syntaxhighlight lang=c> int i, w, x[1000], y[1000]; if (w) { for (i = 0; i < 1000; i++) { x[i] += y[i]; y[i] = 0; } } else { for (i = 0; i < 1000; i++) { x[i] += y[i]; } } </syntaxhighlight> While the loop unswitching may double the amount of code written, each of these new loops may now be separately optimized. Loop unswitching was introduced in [[GNU Compiler Collection|gcc]] in version 3.4.<ref>{{Cite web|url=https://gcc.gnu.org/gcc-3.4/changes.html|title = GCC 3.4 Release Series β Changes, New Features, and Fixes - GNU Project}}</ref> ==References== {{Reflist}} {{Compiler optimizations}} [[Category:Compiler optimizations]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Compiler optimizations
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)