Refining C/C++ Code
3-41
Optimizing C/C++ Code
3.4.3.1
Trip Count Issues
A trip count is the number of loop iterations executed. The trip counter is the
variable used to count each iteration. When the trip count reaches a limit equal
to the trip count, the loop terminates.
If the compiler can guarantee that at least
n loop iterations will be executed,
then
n is the known minimum trip count. Sometimes the compiler can deter-
mine this information automatically. Alternatively, the user can provide this in-
formation using the MUST_ITERATE and PROB_ITERATE pragma. For more
information about pragmas, see the
TMS320C6000 Optimizing C/C++ Com-
piler User’s Guide (SPRU187).
The minimum safe trip count is the number of iterations of the loop that are nec-
essary to safely execute the software pipelined version of the loop.
All software pipelined loops have a minimum safe trip count requirement. If the
known minimum trip count is not above the minimum safe trip count, redundant
loops will be generated.
The known minimum trip count and the minimum safe trip count for a given
software pipelined loop can be found in the compiler-generated comment
block for that loop.
In general, loops that can be most efficiently software pipelined have loop trip
counters that count down. In most cases, the compiler can transform the loop
to use a trip counter that counts down even if the original code was not written
that way.
For example, the optimizer at levels –o2 and –o3 transforms the loop in
Example 3–25(a) to something like the code in Example 3–25(b).
Example 3–25. Trip Counters
(a) Original code
for (i = 0; i < N; i++) /* i = trip counter, N = trip count */
(b) Optimized code
for (i = N; i != 0; i––) /* Downcounting trip counter */