Normalized Loop
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a normalized loop (sometimes called well-behaved loop), is a loop in which the loop variable starts at 0 (or any constant) and gets incremented by one at every iteration until the exit condition is met. Normalized loops are very important for
compiler theory In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
,
loop dependence analysis In computer science, loop dependence analysis is a process which can be used to find dependencies within iterations of a loop with the goal of determining different relationships between statements. These dependent relationships are tied to the ord ...
as they simplify the
data dependence A data dependency in computer science is a situation in which a program statement (instruction) refers to the data of a preceding statement. In compiler theory, the technique used to discover data dependencies among statements (or instructions) is c ...
analysis.


Well-behaved loops

A well behaved loop is normally of the form: for ( i = 0; i < MAX; i++ ) a = b + 5; Because the increment is unitary and constant, it's very easy to see that, if both ''a'' and ''b'' are bigger than MAX, this loop will never access memory outside the allocated range.


Non-normalized loops

A non-normalized loop may begin at different indexes, increment by not-unitary amounts and have exit conditions complicated to define. Such loops are hard to optimize, vectorize and even traverse, especially if functions are executed on any part of the loop conditions. A simple example, where it doesn't start at the beginning and increments by more than one: // Example 1 for ( i = 7; i < MAX; i+=3 ) a = b + 5; A more complicated example, with an additional exit condition: // Example 2 for ( i = 7; i < MAX , , i > MIN; i+=3 ) a = b + 5; Loops can also have non-predictable behavior during compilation time, where the exit condition depends on the contents of the data being modified: // Example 3 for ( i = 7; i < MAX && a i+=3 ) a = b + 5; Or even dynamic calculations by means of function calls: // Example 4 for ( i = start(); i < max(); i+=increment() ) a = b + 5; Reverse loops are also very simple, and can be easily normalized: // Example 5 for ( i = MAX; i > 0; i-- ) a = b + 5;


Converting to a normalized loop

If the non-normalized doesn't have dynamic behaviour, it's normally very easy to transform it to a normalized one. For instance, the first example (Example 1) above can easily be converted to: // Example 1 -> normalized for ( i = 0; i < (MAX-7)/3; i++ ) a *3+7= b *3+7+ 5; While the third example can be partially normalized to allow some parallelization, but still lack the ability to know the loop span (how many iterations there will be), making it harder to vectorize by using multi-media hardware. Starting at 7 is not so much of a problem, as long as the increment is regular, preferably one. When multiple statements inside the loop use the index, some private temporary variables may be created to cope with the different iteration paces. The reverse loop (Example 5) is also easy to normalize: // Example 5 -> normalized for ( i = 0; i < MAX; i++ ) a AX-i= b AX-i+ 5; Note that the access is still backwards. In this case, it makes no sense to leave it backwards (as there is no
data dependence A data dependency in computer science is a situation in which a program statement (instruction) refers to the data of a preceding statement. In compiler theory, the technique used to discover data dependencies among statements (or instructions) is c ...
), but where dependences exist, caution must be taken to revert the access as well, as it could disrupt the order of assignments.


Impossible conversions

The Example 4 above makes it impossible to predict anything from that loop. Unless the functions themselves are trivial (constant), there is no way to know where the loop will start, stop and how much it'll increment each iteration. Those loops are not only hard to parallelize, but they also perform horribly. Each iteration, the loop will evaluate two functions (max() and increment()). Even if the functions are inlined, the condition becomes too complex to be worth optimizing. The programmer should take extra care not to create those loops unless strictly necessary (if ever). Another danger of such loops appear if the evaluation depends on the data being modified. For instance, a normal error when using iterators is to remove items from a list while modifying it, or relying on sizes (for exit condition) that are not true any more.


See also

*
Dependence analysis In compiler theory, dependence analysis produces execution-order constraints between statements/instructions. Broadly speaking, a statement ''S2'' depends on ''S1'' if ''S1'' must be executed before ''S2''. Broadly, there are two classes of depend ...
*
Loop transformation In compiler theory, loop optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capabi ...
*
Loop splitting Loop splitting is a compiler optimization technique. It attempts to simplify a loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a compan ...
*
Loop fusion In computer science, loop fission (or loop distribution) is a compiler optimization in which a loop is broken into multiple loops over the same index range with each taking only a part of the original loop's body. The goal is to break down a large l ...
*
Loop interchange In compiler theory, loop interchange is the process of exchanging the order of two iteration variables used by a nested loop. The variable used in the inner loop switches to the outer loop, and vice versa. It is often done to ensure that the element ...
*
Loop skewing Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, an ...
*
Automatic parallelization Automatic may refer to: Music Bands * Automatic (band), Australian rock band * Automatic (American band), American rock band * The Automatic, a Welsh alternative rock band Albums * Automatic (Jack Bruce album), ''Automatic'' (Jack Bruce a ...
*
Automatic vectorization Automatic vectorization, in parallel computing, is a special case of automatic parallelization, where a computer program is converted from a scalar implementation, which processes a single pair of operands at a time, to a vector implementation, wh ...
*
Loop dependence analysis In computer science, loop dependence analysis is a process which can be used to find dependencies within iterations of a loop with the goal of determining different relationships between statements. These dependent relationships are tied to the ord ...


References

{{Reflist Compiler construction