Incomputing,aliasing describes a situation in which a data location inmemory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated with all aliased names, which may not be expected by the programmer. As a result, aliasing makes it particularly difficult to understand, analyze and optimize programs.Aliasing analysers intend to make and compute useful information for understanding aliasing in programs.
Aliasing can occur in any language that can refer to one location in memory with more than one name (for example, withpointers). This is a common problem with functions that accept pointer arguments, and their tolerance (or the lack thereof) for aliasing must be carefully documented, particularly for functions that perform complex manipulations on memory areas passed to them.
Controlled aliasing behaviour may be desirable in some cases (that is, aliasing behaviour that is specified, unlike that enabled by memory layout in C). It is common practice inFortran. ThePerlprogramming language specifies, in some constructs, aliasing behaviour, such as inforeach loops. This allows certain data structures to be modified directly with less code. For example,
my@array=(1,2,3);foreachmy$element(@array){# Increment $element, thus automatically# modifying @array, since $element is ''aliased''# to each of @array's elements in turn.$element++;}print"@array \n";
will print out "2 3 4" as a result. If one wanted to bypass aliasing effects, one could copy the contents of the index variable into another and change the copy.
Optimizers often have to make conservative assumptions about variables when aliasing is possible. For example, knowing the value of a variable (such asx is 5) normally allows certain optimizations (such asconstant propagation). However, the compiler cannot use this information after an assignment to another variable (for example, in C,*y = 10) because it could be that*y is an alias ofx. This could be the case after an assignment likey = &x. As an effect of this assignment to*y, the value ofx would be changed as well, so propagating the information thatx is 5 to the statements following*y = 10 would be potentially wrong (if*y is indeed an alias ofx). However, if there is information about pointers, the constant propagation process could make a query like: canx 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 thatx is not aliased by*y, then code that uses or changes the value ofx can be moved before the assignment*y = 10, if this would improvescheduling or enable moreloop optimizations to be carried out.
To enable such optimizations in a predictable manner,the ISO standard for theC programming language (including its newerC99 edition, see section 6.5, paragraph 7) specifies that it is illegal (with some exceptions) to access the same memory location using pointers of different types. A compiler may therefore assume that such pointers do not alias. This rule, known as thestrict aliasing rule, sometimes allows for impressive increases in performance,[1] 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 implementreference counting,[2] and required changes to the basic object structs in Python 3 to enable this optimization. TheLinux kernel does this because strict aliasing causes problems with optimization of inlined code.[3] In such cases, when compiled withgcc, the option-fno-strict-aliasing is invoked to prevent unwanted optimizations that could yield unexpected code.
The termaliasing is also used to describe the situation where, due to either a hardware design choice or a hardware failure, one or more of the available address bits is not used in the memory selection process.[4] This may be a design decision if there are more address bits available than are necessary to support the installed memory device(s). In a failure, one or more address bits may be shorted together, or may be forced toground (logic 0) or the supply voltage (logic 1).
For this example, assuming a memory design with 8 locations, requiring only 3 address lines (orbits, since 23 = 8). Address bits (named A2 through A0) are decoded to select unique memory locations as follows, in standardbinary counter fashion:
| A2 | A1 | A0 | Memory location |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 2 |
| 0 | 1 | 1 | 3 |
| 1 | 0 | 0 | 4 |
| 1 | 0 | 1 | 5 |
| 1 | 1 | 0 | 6 |
| 1 | 1 | 1 | 7 |
In the table above, each of the 8 unique combinations of address bits selects a different memory location. However, if one address bit (say A2) were to be shorted to ground, the table would be modified as follows:
| A2 | A1 | A0 | Memory location |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 2 |
| 0 | 1 | 1 | 3 |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 2 |
| 0 | 1 | 1 | 3 |
In this case, with A2 always being zero, the first four memory locations are duplicated and appear again as the second four. Memory locations 4 through 7 have become inaccessible.
If this change occurred to a different address bit, the decoding results would be different, but in general the effect would be the same: the loss of a single address bit cuts the available memory space in half, with resulting duplication (aliasing) of the remaining space.