Path: utzoo!utgpu!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!labrea!rutgers!ukma!uflorida!haven!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.fortran
Subject: Function call optimisations (was Re: i++, i+=1, i=i+1)
Message-ID: <13675@mimsy.UUCP>
Date: 21 Sep 88 18:43:04 GMT
References: <3976@h.cc.purdue.edu> <3659@lanl.gov> <561@hudson.acc.virginia.edu> <6370@batcomputer.tn.cornell.edu>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 45

>In article <561@hudson.acc.virginia.edu> gl8f@bessel.acc.Virginia.EDU
>(Greg Lindahl) claims:
>>VAX/VMS fortran doesn't work this way, even with math library functions
>>which are known to have no side-effects. if you write
>>	a = sin(x)
>>	b = sin(x)
>>	c = sin(x)
>>it will call sin() 3 times.

In article <6370@batcomputer.tn.cornell.edu>
thompson@batcomputer.tn.cornell.edu (Steve Thompson) writes:
>I couldn't believe this when I saw it, so I checked it on my VMS V4.7/
>FORTRAN V4.8 system. Sin(x) does get called only ONCE, as you can
>easily verify by looking at the machine code listing.

Of course, none of this is relevant with regard to the intended subject
(corrected above), since `SIN' is an intrinsic in Fortran 77.

To answer that question, consider the following:

	C program 1.
		PROGRAM P
		INTEGER F
		PRINT *, F(1), F(1)
		STOP
		END

and in a separate file, with the compiler prevented (artificially if
necessary) from seeing the code for function F in program P:

	C the identity function
		INTEGER FUNCTION F(I)
		INTEGER I
		F = I
		RETURN
		END

Given this situation, may a Fortran compiler legally compile program P
such that it in fact calls function F only once?

It is true that the *order* of the calls to F is undefined, but I
do not know whether the *number* of calls to F is undefined.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris