The /Ol option enables a set of optimizations involving loops. Because loops involve sections of code that are executed repeatedly, they are targets for optimization. These optimizations all involve moving or rewriting code so that it executes faster.
Loop optimization can be turned on with the /Ol option or with the loop_opt pragma. The following line enables loop optimization for all subsequent functions:
#pragma loop_opt( on )
The following line turns it off:
#pragma loop_opt( off )
Summary: The /Ol option removes invariant code.
An optimal loop contains only expressions whose values change through each execution of the loop. Any subexpression whose value is constant should be evaluated before the body of the loop is executed. Unfortunately, these subexpressions are not always readily apparent. The optimizer can remove many of these expressions from the body of a loop at compile time. This example illustrates invariant code in a loop:
i = -100;
while( i < 0 )
{
i += x + y;
}
In the preceding example, the expression x + y does not change in the loop body. Loop optimization removes this subexpression from the body of the loop so that it is only executed once, not every time the loop body is executed. The optimizer will change the code to the following fragment:
i = -100;
t = x + y;
while( i < 0 )
{
i += t;
}
Loop optimization is much more effective when the compiler can assume no aliasing. While you can use loop optimization without the /Oa or /Ow option, use /Oa to ensure that the most options possible are used.
Here is a code fragment that could have an aliasing problem:
i = -100;
while( i < 0 )
{
i += x + y;
*p = i;
}
If you do not specify the /Oa option, the compiler must assume that either x or y could be modified by the assignment to *p. Therefore, the compiler cannot assume the subexpression x + y is constant for each loop iteration. If you specify that you are not doing any aliasing (with the /Oa option), the compiler assumes that modifying *p cannot affect either x or y, and that the subexpression is indeed constant and can be removed from the loop, as in the previous example.
Note:
All loop optimizations specified by the /Ol option or the loop_opt pragma are safe optimizations. To enable aggressive loop optimizations, you must use the enable aggressive optimizations (/Oz) option. While the optimizations enabled by the combination of /Ol and /Oz are not safe for all cases, they will work properly for most programs.
Calling the setjmp or longjmp functions when loop optimization is in effect can cause the compiler to generate incorrect code. Use the loop_opt pragma or the optimize pragma with the g option to disable this optimization in functions that call setjmp and longjmp.