add assignments to undeclared loop iterators
When the loop iterator of a for loop is not declared inside
the for loop itself, the value of this loop iterator persists
after the execution of the loop and could in principle be used
by subsequent statements. Introduce assignments to the corresponding
variable that ensure the variable contains the correct value
after the execution of the loop.
In particular, for a loop of the form
for (i = init; f(i); i += inc)
body
introduce two assignments to the variable i,
i = value(init);
before the loop and
i = value(i + inc);
at the end of the body of the loop. Note that the "i" on
the right hand side refers to a dimension of the index set
of the statement, while the "i" on the left refers to
a variable.
In principle, it would be possible to precompute the last
value that is assigned by an affine loop. For example,
for the loop
for (i = 0; i < n; ++i)
body
the above assignments could be replaced by
i = n >= 0 ? n : 0;
However, dead code elimination (performed by users of pet)
can achieve essentially the same effect.
In fact, in most cases, the value of the loop iterator is
not used outside of the loop. If the variable is declared
inside the scope containing the scop, then it will also
be killed, meaning that all assignments can be removed
completely.
Note that the introduction of extra statements along the way
means that other statements may have different names (if autogenerated)
after this commit.
Besides the obvious effects of adding extra statements and renaming
most others, this commit also results in the following changes
to the test cases.
In tests/generic_condition.c, the loop is executed an infinite
number of times if N < 0. Due to the explicit increment statement
introduced by this commit, this now results in undefined behavior
(signed integer overflow), meaning that these values of N are removed
from the context and that the body is never executed.
In tests/inc2.c, the explicit increment causes undefined behavior
for N =
2147483647, resulting in this value being removed from
the context.
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>