[comp.lang.c] Flow Analysis With Gotos

bright@Data-IO.COM (Walter Bright) (03/10/88)

In article <17350@glacier.STANFORD.EDU> jbn@glacier.UUCP (John B. Nagle) writes:
#     Provided that goto operations are limited to forward and outward 
#transfers of control, they are relatively harmless, although they do
#make flow analysis within an optimizing compiler more difficult.

Not quite true. The use of gotos precludes simplistic flow analysis, such
as what lint does (to detect unreachable code). Using more general
techniques (such as described in Aho and Ullman's book), whether you
use gotos or not is irrelevent. Those techniques will work with the worst
spaghetti code.

In fact, the way my optimizer works is first to convert everything internally
into gotos, and then do flow analysis from that! Loops are discovered by
analyzing the flow graph, NOT by looking at for's, while's and do's (like
some other optimizers do). This means that the programmer can write his
code using any constructs he likes, and the optimizer will figure it out
regardless.

The trouble with goto's is that it is possible to create an 'irreducible
flow graph'. Put into programmer's terms, it is a loop with more than
one entry point:
	if (a)
	{	i = b();
		goto L1;
	}
	for (i = 0; i < 10; i++)
	{
	    L1:	c(i);
	}
Some optimizations depend on the flow graph being irreducible. There are,
however, well-understood techniques for dealing with this which are also
described in Aho and Ullman's book. Also, in real code, irreducible flow
graphs occur so rarely that an optimizer can get away with simply detecting
such a graph and skipping the optimizations that depend on irreducible
graphs.

One thing about goto's that most people seem to miss: there are starting
to be a lot of translators, programming systems, etc. where the output
is C code that is to be run through a C compiler. The C code is the
'intermediate file format', and is never seen by mere mortals. Examples
of this are: C++, Lotus-to-C translators, dbase-to-C translators, YACC,
etc. Some code generation algorithms are much simpler to write if goto's
are available.