4.8. MUST_ITERATE and PROB_ITERATE Pragmas and AttributesΒΆ

The compiler can often generate faster code when the compiler knows how many times a loop will execute. Adding this information via the MUST_ITERATE and PROB_ITERATE pragmas and the TI_must_iterate and TI_prob_iterate C++ attributes can help the compiler:

  • Determine if it is profitable to vectorize a loop

  • Determine if it is profitable to perform certain loop optimizations and loop-nest optimizations

  • Determine if a redundant loop is needed (see Redundant Loops, below)

Before vectorizing a loop, the compiler tries to determine if the change will improve performance. It is helpful if the compiler has information about the iteration counts of the loop so the compiler can make better predictions about the profitability of vectorization. In the same way, the compiler also tries to determine if certain loop optimizations and loop-nest optimizations will be profitable and so information about the iterations counts of the loops can be helpful to the compiler.

Note

Note: Do not provide incorrect information about the iteration count in the MUST_ITERATE pragma or TI_must_iterate C++ attribute. If incorrect information is specified in this pragma/attribute, the compiler may create code that produces unexpected and incorrect behavior.

Redundant Loops: In some cases, if the compiler does not know how many times a loop will execute, the compiler generates two different versions of the loop. Software pipelined loops often must execute a certain minimum number of iterations to be legal to execute. If the iteration count of the loop is less than this minimum safe iteration count, the compiler generates a run-time iteration count check and branches to either the software pipelined version of the loop, or a duplicate loop. That is, the compiler generates a "regular" version of the loop (that executes much more slowly).

The minimum safe iteration count depends on how many iterations were scheduled in parallel and how effectively the compiler was able to perform an optimization called stage collapsing. See the Minimum Safe Iteration Count and Stage Collapsing and Load Speculation sections for more information.

The Software Pipeline Information in the comment block in the assembly file specifies the minimum safe iteration count (iteration count) of the loop and states whether the compiler has generated a duplicate loop.

Because the compiler must sometimes generate a redundant loop and the control code necessary to choose between the two loops, it is helpful to tell the compiler the minimum iteration count of the loop with a MUST_ITERATE pragma or TI_must_iterate attribute when it is known, as the redundant loop may not be necessary. This can improve performance, especially when the loop is enclosed within an outer loop and if the compiler can then perform loop collapsing or other loop optimizations with the outer and inner loops.

The following example shows redundant loop generation information in the Software Pipeline Information section of the assembly comment block. See Software Pipelining for more information on the Software Pipeline Information comment block.

;*      Redundant loop generated