add assignments to undeclared loop iterators
commit0ef3a6de81a4b9b70a8d42830acc41c0d833d571
authorSven Verdoolaege <skimo@kotnet.org>
Wed, 23 Apr 2014 15:20:49 +0000 (23 17:20 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Wed, 9 Mar 2016 09:11:43 +0000 (9 10:11 +0100)
treec7d0d40401abe8819d681a210b5d0938e2f2e5a0
parent6b78923985eb1e496ec31a0126069ef02e6fcc6a
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>
72 files changed:
tests/QR.scop
tests/arg.scop
tests/autodetect/empty2.scop
tests/break.scop
tests/break2.scop
tests/break3.scop
tests/break4.scop
tests/break5.scop
tests/ceild.scop
tests/conditional_assignment.scop
tests/conditional_assignment2.scop
tests/continue.scop
tests/continue2.scop
tests/continue3.scop
tests/continue4.scop [copied from tests/continue3.scop with 60% similarity]
tests/continue5.scop
tests/data_dependent.scop
tests/data_dependent2.scop
tests/dec.scop
tests/dec2.scop
tests/dec3.scop
tests/dec4.scop
tests/decl.scop
tests/div_mod.scop
tests/dynamic_condition.scop
tests/empty_domain.scop
tests/encapsulate/dynamic_condition.scop
tests/encapsulate/while_affine.scop
tests/floord.scop
tests/floord2.scop
tests/generic_condition.scop
tests/generic_condition2.scop
tests/implicit_condition.scop
tests/inc.scop
tests/inc2.scop
tests/inc3.scop
tests/inc4.scop
tests/inf.scop
tests/inf2.scop
tests/inf4.scop
tests/inline6.scop
tests/label2.scop [copied from tests/autodetect/empty2.scop with 50% similarity]
tests/loop.scop [copied from tests/autodetect/empty2.scop with 51% similarity]
tests/loop2.scop [copied from tests/dynamic_condition.scop with 52% similarity]
tests/loop3.scop [copied from tests/dynamic_condition.scop with 52% similarity]
tests/loop4.scop [copied from tests/dynamic_condition.scop with 52% similarity]
tests/loop8.scop
tests/matmul.scop
tests/max.scop
tests/min.scop
tests/min2.scop
tests/mod.scop
tests/mod2.scop
tests/omega.scop
tests/pencil_max.scop
tests/pencil_min.scop
tests/piecewise.scop
tests/piecewise2.scop
tests/propagate.scop
tests/quasi_affine.scop
tests/ternary.scop
tests/tobi1.scop
tests/tobi2.scop
tests/unsigned1.scop
tests/unsigned2.scop
tests/unsigned3.scop
tests/unsigned_break1.scop
tests/unsigned_break2.scop
tests/wdp.scop
tests/while_affine.scop
tests/while_break2.scop
tree2scop.c