HP C Programmer's Guide (92434-90009)

Chapter 4 99
Optimizing HP C Programs
Optimizer Assumptions
Optimizer Assumptions
During optimization, the compiler gathers information about the use of variables and
passes this information to the optimizer. The optimizer uses this information to ensure
that every code transformation maintains the correctness of the program, at least to the
extent that the original unoptimized program is correct.
When gathering this information, the HP C compiler makes the following assumption:
while inside a function, the only variables that can be accessed indirectly through a
pointer or by another function call are:
Global variables, that is, all variables with file scope.
Local variables that have had their addresses taken either explicitly by the & operator,
or implicitly by the automatic conversion of array references to pointers.
In general, you do not need to be concerned about this assumption. Standard
conformant C programs do not violate this assumption. However, if you have code that
does violate this assumption, the optimizer can change the behavior of the program in
an undesired way. In particular, you should avoid the following coding practices to
ensure correct program execution for optimized code:
Avoid referencing outside the bounds of an array.
Avoid passing incorrect number of arguments to functions.
Avoid accessing an array other than the one being subscripted. For example, the
construct a[b-a] where a and b are the same type of array actually references the array
b, because it is equivalent to *(a+(b- a)), which is equivalent to *b. Using this
construct might yield unexpected optimization results.
Avoid referencing outside the bounds of the objects a pointer is pointing to. All
references of the form *(p+i) are assumed to remain within the bounds of the variable
or variables that p was assigned to point to.
Avoid using variables that are accessed by external processes. Unless a variable is
declared with the volatile attribute, the compiler will assume that a program's data
structures are accessed only by that program. Using the volatile attribute may
significantly slow down a program.
Avoid using local variables before they are initialized. When you request optimization
level 2, 3, or 4, the compiler tries to detect and flag violations of this rule.
Avoid relying on the memory layout scheme when manipulating pointers; incorrect
optimizations may result. For example, if p is pointing to the first member of structure ,
it should not be assumed that p1 points to the second member of the structure. Another
example: if p is pointing to the first in a list of declared variables, p1 should not be
assumed to be pointing to the second variable in the list.