smart compilers

jlg at lanl.ARPA jlg at lanl.ARPA
Fri Dec 21 11:29:14 AEST 1984


> However, the IBM H Level FORTRAN Programmer's Guide does warn that code such
> as
> 
> 	DO 100 I = 1 TO 10
> 	IF (Y .GT. 0) X(I) = SQRT(Y)
>     100 CONTINUE
> 
> (with X appropriately declared as an array of course) can cause an error
> because it tries to take the square root of a negative number.  The optimizer
> compiles this as if it were
> 
> 	TEMP = SQRT(Y)
> 	DO 100 I = 1 TO 10
> 	IF (Y .GT. 0) X(I) = TEMP
>     100 CONTINUE

This particular example should be optimized as

      IF ( Y.GT.0 ) TEMP=SQRT(Y)
      DO 100 I = 1,10         !THERE ARE NO 'TO' DELIMITERS IN FORTRAN
         X(I)=TEMP            !FOLLOW INDENTATION STANDARDS PLEASE
100   CONTINUE

A good data flow analysis would have seen this.  However, in general these
types of optimizations are called 'unsafe'; for obvious reasons.  I don't
think unsafe optimizations are allowed by the standard since they change
the meaning of the code (in particular, they cause errors when none would
have normally occurred).  IBM has simply stepped out of line here.  IBM is
not alone, most vendors tend to have some violations of the standard lurking
around in the compilers.

The given example has a safe optimization available to it.  It is possible
to come up with cases that don't, so in general this sort of optimization
should be considered unsafe (unless you have a really good data flow
analyzer available to guarantee safety).

------------------------------------------------------------------------------
The greatest derangement of the mind is to believe in something
because one wishes it to be so - Louis Pasteur

                                              James Giles



More information about the Comp.unix.wizards mailing list