Aussie AI
Loop Code Motion
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Loop Code Motion
Loop code motion is moving loop-invariant code from inside the loop body to the pre-initialization code for the loop. Any code that has the same value should not be performed inside the loop body. Instead, it should be pre-calculated before the loop, and stored in a temporary variable. This is sometimes called “hoisting” the code out of the loop.
Example: Loop Code Motion: One common example of unnecessary recalculation of loop-invariant values is in the loop test. The code in the Boolean test for the loop is actually part of the loop body.
An example of code that re-calculates the loop limit:
for (i = 0; i < vec.num_elements(); i++) { // ... }
The “num_elements
” call is probably loop-invariant, assuming the vector
doesn't change size during processing.
Maybe the “num_elements
” function is declared “inline
” and the C++ compiler will fix it anyway.
Nevertheless, this is a candidate for loop code motion, using a temporary variable instead:
int n = vec.num_elements(); // Loop-invariant value for (i = 0; i < n; i++) { // ... }
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |