Pointers have the annoying side effect of reducing the assumptions that the global optimizer can make. For one thing, compare the following:
Without pointers, if you call a subroutine or function, the optimizer knows that the call will change only variables in common or those passed as arguments to that call.
With pointers, this is no longer valid, since a routine can take the address of an argument and save it in a pointer in common for use in a subsequent call to itself or to another routine.
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.
There are two alternatives for optimization with pointers.
Do not use pointers with optimization level -O4.
Use a pointer only to identify the location of the data for calculations and pass the pointer to a subprogram. Almost anything else you do to the pointer can yield incorrect results.
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.
Any of the following coding practices, and many others, could cause problems with an optimization level of -O3 or -O4:
A program unit does arithmetic with the pointer.
A subprogram saves the address of any of its arguments between calls.
A function returns the address of any of its arguments, although it can return the value of a pointer argument.
A variable is referenced through a pointer, but the address of the variable is not explicitly taken with the LOC() or MALLOC() functions.
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.