FORTRAN 77 Language Reference

Optimization and Pointers

Pointers have the annoying side effect of reducing the assumptions that the global optimizer can make. For one thing, compare the following:

Therefore, the optimizer must assume that a variable passed as an argument in a subroutine or function call can be changed by any other call. Such an unrestricted use of pointers would degrade optimization for the vast majority of programs that do not use pointers.

General Guidelines

There are two alternatives for optimization with pointers.

The second choice also has a suboption: localize pointers to one routine and do not optimize it, but do optimize the routines that do the calculations. If you put the calling the routines on different files, you can optimize one and not optimize the other.

Example: A relatively "safe" kind of coding with -O3 or -O4:


	REAL A, B, V(100,100) 	 This programming unit does
	POINTER ( P, V )      	   nothing else with P other than  
	P = MALLOC(10000)     	 getting the address and passing it.
	... 
	CALL CALC ( P, A )
	...
	END

	SUBROUTINE CALC ( ARRAY, X )
	...
	RETURN
	END

If you want to optimize only CALC at level -O4, then avoid using pointers in CALC.

Some Problematic Code Practices

Any of the following coding practices, and many others, could cause problems with an optimization level of -O3 or -O4:

Example: Code that could cause trouble with -O3 or -O4:


	COMMON A, B, C 
	POINTER ( P, V ) 
	P = LOC(A) + 4 			  Possible problems here if optimized
	... 

The compiler assumes that a reference through P may change A, but not B; this assumption could produce incorrect code.