For after egcs-2.0, when everything works and we get bored
Toon Moene
toon@moene.indiv.nluug.nl
Sat Aug 1 11:40:00 GMT 1998
I wrote:
> So, the 64K question is: How do we transform the loop nest into
> the single loop *in the optimisation passes* [and not in the Fortran
> source code]. I will not deal with this question in its entirety,
> because I do not know all steps necessary, but I will outline the
> checks that are necessary on such a loop nest to be able to tell
> whether it *can* be done.
and Richard replied:
> I think this pretty much has to be done in a higher-level
> intermediate language than our current RTL. Anything
> else has too much hair.
Hmm, I'm glad you reached the same conclusion as I did: It's
simply too hard at this point in time (note the reference to
egcs-2.0 in the subject :-)
>> jc = m ! Outer loop count
>> j = 1 ! Outer loop induction var j
> Incidentally, why does the fortran front end generate
> loops in this way? Was the loop inversion code not
> working for you once upon a time?
>
> Seems to me you should just emit
> top = m
> j = 1
> if (j <= top) {
> do {
> ...
> } while (++j <= top);
> }
The Fortran Frontend tries to implement DO loops the way the
Standard intends them to be:
( Quoting the October 1997 working draft )
8.1.4.4.1 Loop initation
When the DO statement is executed, the DO construct becomes active.
If loop-control is
[,] do-variable = scalar-int-expr1, scalar-int-expr2 [,
scalar-int-expr3 ]
the following steps are performed in sequence:
1. The initial parameter m1, the terminal parameter m2, and the
incrementation parameter m3 are of type integer with the same kind
type parameter as the do-variable [ ... ]
2. The DO variable becomes defined with the value of the initial
parameter m1.
3. The iteration count is established and is the value of the
expression (m2 - m1 + m3) / m3, unless that value is negative,
in which case the iteration count is 0.
( End-of-Quote )
[ Note the Fortran Standardese "defined" above, which can loosely be
replaced by "set" ]
Hence, the most straightforward way of implementing a counted DO
loop is to perform the calculation in 3., test whether it's >=0 and
count down from there.
I always assumed that induction variable elimination would take
care of this or any other "superfluous" induction variable.
> In this way, you only have one variable to increment
> around the loop, and the possibility that j may be
> replaced by a giv. As it is, jc can never be eliminated.
jc cannot be eliminated, but others can (e.g. j = m - jc + 1).
Note that a Fortran DO-loop is a purely counted loop - a C for loop
is only a counted loop if no statement in its body modifies the
"loop counter". Not only is it illegal to modify the do-variable in
a Fortran DO loop, in a conforming implementation it should not
even change the number of loop iterations !
Cheers,
Toon.
More information about the Gcc
mailing list