Aliasing (computing) - Conflicts With Optimization

Conflicts With Optimization

Optimizers often have to make conservative assumptions about variables in the presence of pointers. For example, a constant propagation process that knows the value of variable x is 5 would not be able to keep using this information after an assignment to another variable (for example, *y = 10) because it could be that *y is an alias of x. This could be the case after an assignment like y = &x.

As an effect of the assignment to *y, the value of x would be changed as well, so propagating the information that x is 5 to the statements following *y = 10 would be potentially wrong (if *y is indeed an alias of x). However, if we have information about pointers, the constant propagation process could make a query like: can x be an alias of *y? Then, if the answer is no, x = 5 can be propagated safely.

Another optimization impacted by aliasing is code reordering. If the compiler decides that x is not aliased by *y, then code that uses or changes the value of x can be moved before the assignment *y = 10, if this would improve scheduling or enable more loop optimizations to be carried out.

To enable such optimizations in a predictable manner, the ISO standard for the C programming language (including its newer C99 edition, see section 6.5, paragraph 7) specifies that it is illegal (with some exceptions) for pointers of different types to reference the same memory location. This rule, known as "strict aliasing", sometime allows for impressive increases in performance, but has been known to break some otherwise valid code. Several software projects intentionally violate this portion of the C99 standard. For example, Python 2.x did so to implement reference counting, and required changes to the basic object structs in Python 3 to enable this optimisation. The Linux kernel does this because strict aliasing causes problems with optimization of inlined code. In such cases, when compiled with gcc, the option -fno-strict-aliasing is invoked to prevent unwanted optimizations that could yield unexpected code.

Read more about this topic:  Aliasing (computing)

Famous quotes containing the words conflicts with and/or conflicts:

    In motherhood, where seemingly opposite realities can be simultaneously true, the role of nurturer invariably conflicts with the role of socializer. When trouble came as it surely must, was I the good cop who understood, the bad cop who terrorized, or both?
    Mary Kay Blakely (20th century)

    In motherhood, where seemingly opposite realities can be simultaneously true, the role of nurturer invariably conflicts with the role of socializer. When trouble came as it surely must, was I the good cop who understood, the bad cop who terrorized, or both?
    Mary Kay Blakely (20th century)