Sun Studio 12 Update 1: C++ User's Guide

A.2.175.1 Restricted Pointers

In order for a compiler to effectively perform parallel execution of a loop, it needs to determine if certain lvalues designate distinct regions of storage. Aliases are lvalues whose regions of storage are not distinct. Determining if two pointers to objects are aliases is a difficult and time consuming process because it could require analysis of the entire program. Consider function vsq() below:


Example A–3 A Loop With Two Pointers


extern "C"
void vsq(int n, double *a, double *b) {
    int i;
    for (i=0; i<n; i++) {
            b[i] = a[i] * a[i];
    }
}

The compiler can parallelize the execution of the different iterations of the loops if it knows that pointers a and b access different objects. If there is an overlap in objects accessed through pointers a and b then it would be unsafe for the compiler to execute the loops in parallel.

At compile time, the compiler does not know if the objects accessed by a and b overlap by simply analyzing the function vsq(); the compiler may need to analyze the whole program to get this information. You can specify that pointer-valued function parameters be treated as restricted pointers by using the following command line option: -xrestrict[=func1,...,funcn] If a function list is specified, pointer parameters in the specified functions are treated as restricted; otherwise, all pointer parameters in the entire source file are treated as restricted (not recommended). For example, -xrestrict=vsq qualifies the pointers a and b given in the example of the function vsq().

Declaring the pointer arguments as restricted states that the pointers designate distinct objects. The compiler can assume that a and b point to distinct regions of storage. With this alias information, the compiler is able to parallelize the loop.

It is critical that you use -xrestrict correctly. If pointers qualified as restricted pointers point to objects that are not distinct, the compiler can incorrectly parallelize loops resulting in undefined behavior. For example, assume that pointers a and b of function vsq() point to objects that overlap, such that b[i] and a[i+1] are the same object. If a and b are not declared as restricted pointers the loops will be executed serially. If a and b are incorrectly qualified as restricted pointers the compiler may parallelize the execution of the loops, which is not safe, because b[i+1] should only be computed after b[i] is computed