Both on GIMPLE and on RTL, there are functions available to determine the number of iterations of a loop, with a similar interface. The number of iterations of a loop in GCC is defined as the number of executions of the loop latch. In many cases, it is not possible to determine the number of iterations unconditionally – the determined number is correct only if some assumptions are satisfied. The analysis tries to verify these conditions using the information contained in the program; if it fails, the conditions are returned together with the result. The following information and conditions are provided by the analysis:
assumptions: If this condition is false, the rest of the information is invalid.
may_be_zeroon GIMPLE: If this condition is true, the loop exits in the first iteration.
infinite: If this condition is true, the loop is infinite. This condition is only available on RTL. On GIMPLE, conditions for finiteness of the loop are included in
niteron GIMPLE: The expression that gives number of iterations. The number of iterations is defined as the number of executions of the loop latch.
Both on GIMPLE and on RTL, it necessary for the induction variable
analysis framework to be initialized (SCEV on GIMPLE, loop-iv on RTL).
On GIMPLE, the results are stored to
structure. Number of iterations before the loop is exited through a
given exit can be determined using
function. On RTL, the results are returned in
structure. The corresponding function is named
check_simple_exit. There are also functions that pass through
all the exits of a loop and try to find one with easy to determine
number of iterations –
find_loop_niter on GIMPLE and
find_simple_exit on RTL. Finally, there are functions that
provide the same information, but additionally cache it, so that
repeated calls to number of iterations are not so costly –
number_of_latch_executions on GIMPLE and
Note that some of these functions may behave slightly differently than
others – some of them return only the expression for the number of
iterations, and fail if there are some assumptions. The function
number_of_latch_executions works only for single-exit loops.
number_of_cond_exit_executions can be used to
determine number of executions of the exit condition of a single-exit
loop (i.e., the
number_of_latch_executions increased by one).