HP C Programmer's Guide (92434-90009)

Chapter 4 79
Optimizing HP C Programs
Controlling Specific Optimizer Features
The optimizer generally spills all global data from registers to memory before any
modification to global variables or any loads through pointers. However, you can instruct
the optimizer on how data types interact so that it can generate more efficient code.
If you have the following:
1 int *p;
2 float *q;
3 int a,b,c;
4 float d,e,f;
5 foo()
6{
7 for (i=1;i<10;i) {
8 d=e
9 *p=..
10 e=d+f;
11 f=*q;
12 }
13 }
With +Onoptrs_strongly_typed turned on, the pointers p and q will be assumed to be
disjoint because the types they point to are different types. Without type-inferred aliasing,
*p is assumed to invalidate all the definitions. So, the use of d and f on line 10 have to be
loaded from memory. With type-inferred aliasing, the optimizer can propagate the copy of d
and f and thus avoid two loads and two stores.
This option can be used for any application involving the use of pointers, where those
pointers are type safe. To specify when a subset of types are type-safe, use the
[NO]PTRS_STRONGLY_TYPED pragma. The compiler issues warnings for any
incompatible pointer assignments that may violate the type-inferred aliasing rules
discussed in "Aliasing Options" later in this chapter.
Example 2: Unsafe Type Cast
Any type cast to a different type violates type-inferred aliasing rules. Do not use
+Optrs_strongly_typed with code that has these "unsafe" type casts. Use the
[NO]PTRS_STRONGLY_TYPED pragma to prevent the application of type-inferred
aliasing to the unsafe type casts.
struct foo{
int a;
int b;
} *P;
struct bar {
float a;
int b;
float c;
} *q;
P = (struct foo *) q;
/* Incompatible pointer assignment
through type cast */
Example 3: Generally Applying Type Aliasing