To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Induction variable

From Wikipedia, the free encyclopedia

In computer science, an induction variable is a variable that gets increased or decreased by a fixed amount on every iteration of a loop or is a linear function of another induction variable.[1]

For example, in the following loop, i and j are induction variables:

for (i = 0; i < 10; ++i) {
    j = 17 * i;
}

YouTube Encyclopedic

  • 1/3
    Views:
    4 175
    11 082
    3 240
  • Mod-10 Lec-16 Machine-Independent Optimizations
  • Programming Style and Your Brain
  • Mod-10 Lec-17 Machine-Independent Optimizations-Part 2

Transcription

Application to strength reduction

A common compiler optimization is to recognize the existence of induction variables and replace them with simpler computations; for example, the code above could be rewritten by the compiler as follows, on the assumption that the addition of a constant will be cheaper than a multiplication.

j = -17;

for (i = 0; i < 10; ++i) {
    j = j + 17;
}

This optimization is a special case of strength reduction.

Application to reduce register pressure

In some cases, it is possible to reverse this optimization in order to remove an induction variable from the code entirely. For example:

extern int sum;

int foo(int n) {
    int j = 5;

    for (int i = 0; i < n; ++i) {
        j += 2;
        sum += j;
    }

    return sum;
}

This function's loop has two induction variables: i and j. Either one can be rewritten as a linear function of the other; therefore, the compiler may optimize this code as if it had been written

extern int sum;

int foo(int n) {
    for (int i = 0; i < n; ++i) {
        sum += 5 + 2 * (i + 1);
    }

    return sum;
}

Induction variable substitution

Induction variable substitution is a compiler transformation to recognize variables which can be expressed as functions of the indices of enclosing loops and replace them with expressions involving loop indices.

This transformation makes the relationship between the variables and loop indices explicit, which helps other compiler analysis, such as dependence analysis.

Example:

Input code:

int c = 10;

for (int i = 0; i < 10; i++) {
    c = c + 5; // c is incremented by 5 for each loop iteration
}

Output code

int c = 10;

for (int i = 0; i < 10; i++) {
    c = 10 + 5 * (i + 1);  // c is explicitly expressed as a function of loop index
}

Non-linear induction variables

The same optimizations can be applied to induction variables that are not necessarily linear functions of the loop counter; for example, the loop

j = 1;

for (i = 0; i < 10; ++i) {
    j = j << 1;
}

may be converted to

for (i = 0; i < 10; ++i) {
    j = 1 << (i+1);
}

See also

References

  1. ^ Steven Muchnick; Muchnick and Associates (15 August 1997). Advanced Compiler Design Implementation. Morgan Kaufmann. ISBN 978-1-55860-320-2. induction variable.

Further reading

This page was last edited on 12 August 2023, at 16:12
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.