4.6. Scalar Optimization Directives

The following directives control aspects of scalar optimization:

The following subsections describe these directives.

4.6.1. Control Loop Interchange: INTERCHANGE and NOINTERCHANGE

The loop interchange control directives specify whether or not the order of the following two or more loops should be interchanged. These directives apply to the loops that they immediately precede.

The formats of these directives are as follows:

!DIR$ INTERCHANGE (do_variable1,do_variable2 [,do_variable3]...)

!DIR$ NOINTERCHANGE

do_variable 

Specifies two or more do_variable names. The do_variable names can be specified in any order, and the compiler reorders the loops. The loops must be perfectly nested. If the loops are not perfectly nested, you may receive unexpected results.

The compiler reorders the loops such that the loop with do_variable1 is outermost, then loop do_variable2, then loop do_variable3.

The NOINTERCHANGE directive inhibits loop interchange on the loop that immediately follows the directive.

Example: The following code has an INTERCHANGE directive:

!DIR$ INTERCHANGE (I,J,K)
      DO K = 1,NSIZE1
        DO J = 1,NSIZE1
          DO I = 1,NSIZE1
             X(I,J) = X(I,J) + Y(I,K) * Z(K,J)
          ENDDO
        ENDDO
      ENDDO

The following code results when the INTERCHANGE directive is used on the preceding code:

!DIR$ INTERCHANGE (I,J,K)
      DO I = 1,NSIZE1
        DO J = 1,NSIZE1
          DO K = 1,NSIZE1
             X(I,J) = X(I,J) + Y(I,K) * Z(K,J)
          ENDDO
        ENDDO
      ENDDO

4.6.2. Determine Register Storage: NOSIDEEFFECTS

The NOSIDEEFFECTS directive allows the compiler to keep information in registers across a single call to a subprogram without reloading the information from memory after returning from the subprogram. The directive is not needed for intrinsic functions and VFUNCTIONs.

NOSIDEEFFECTS declares that a called subprogram does not redefine any variables that meet the following conditions:

The format of this directive is as follows:

!DIR$ NOSIDEEFFECTS f [, f ] ...

f 

Symbolic name of a subprogram that the user is sure has no side effects. f must not be the name of a dummy procedure, module procedure, or internal procedure.

A procedure declared NOSIDEEFFECTS should not define variables in a common block or module shared by a program unit in the calling chain. All arguments should have the INTENT(IN) attribute; that is, the procedure must not modify its arguments. If these conditions are not met, results are unpredictable.

The NOSIDEEFFECTS directive must appear in the specification part of a program unit and must appear before the first executable statement.

The compiler may move invokations of a NOSIDEEFFECTS subprogram from the body of a DO loop to the loop preamble if the arguments to that function are invariant in the loop. This may affect the results of the program, particularly if the NOSIDEEFFECTS subprogram calls functions such as the random number generator or the real-time clock.

The effects of the NOSIDEEFFECTS directive are similar to those that can be obtained by specifying the PURE prefix on a function or a subroutine declaration. For more information on the PURE prefix, see Fortran Language Reference Manual, Volume 2.

4.6.3. Suppress Scalar Optimization: SUPPRESS

The SUPPRESS directive suppresses scalar optimization for all variables or only for those specified at the point where the directive appears. This often prevents or adversely affects vectorization of any loop that contains SUPPRESS. The format of this directive is as follows:

!DIR$ SUPPRESS [ var [, var ] ... ]

var 

Variable that is to be stored to memory. If no variables are listed, all variables in the program unit are stored. If more than one variable is specified, use a comma to separate vars.

At the point at which !DIR$ SUPPRESS appears in the source code, variables in registers are stored to memory (to be read out at their next reference), and expressions containing any of the affected variables are recomputed at their next reference after !DIR$ SUPPRESS. The effect on optimization is equivalent to that of an external subroutine call with an argument list that includes the variables specified by !DIR$ SUPPRESS (or, if no variable list is included, all variables in the program unit).

SUPPRESS takes effect only if it is on an execution path. Optimization proceeds normally if the directive path is not executed because of a GOTO or IF.

Example:

      SUBROUTINE SUB (L)
      LOGICAL L
      A = 1.0          ! A is local
      IF (L) THEN
!DIR$ SUPPRESS         ! Has no effect if L is false
        CALL ROUTINE()
      ELSE
        PRINT *, A
      END IF
      END

In this example, optimization replaces the reference to A in the PRINT statement with the constant 1.0, even though !DIR$ SUPPRESS appears between A=1.0 and the PRINT statement. The IF statement can cause the execution path to bypass !DIR$ SUPPRESS. If SUPPRESS appears before the IF statement, A in PRINT * is not replaced by the constant 1.0.