Problemcore
Induction variable analysis
Also called induction variable detection.
Find the variables of a loop that change by a fixed amount each iteration, or are affine in such variables.
In plain words
Inside a loop, some variables move in a regular way: a counter goes up by one each time, a pointer moves by the size of an element, an address is "four times the counter plus eight". Induction variable analysis finds these variables and writes down how each one depends on the number of the current iteration. With that, a compiler can replace a multiplication by a cheaper addition, remove variables that only shadow the counter, compute how many times the loop runs, and turn array accesses into the simple form that dependence tests need. Some variables move in less regular ways, by a growing step or by doubling, or they take turns between two values, and good analyses recognize these too. A variable that changes only under a condition, or that overflows, needs care.
Definition
Let be a loop (a natural loop) with header , in SSA form. Number the visits of the header : visit is the start of iteration . A name defined in has the value in iteration (for a name defined in the body, the value it gets in that iteration). An expression is loop invariant if its value does not change during the execution of the loop.
- A basic induction variable is a name defined by a φ-function at the header, , where every path around the loop computes for one loop invariant , the step. Then .
- A derived induction variable (also dependent induction variable) is a name with for a basic induction variable and loop invariant , . Then .
- An induction variable is a basic or derived one: a name whose value is an affine function of the iteration number with loop invariant and .
The problem. Given the loop, find every induction variable and its closed form , as an expression in loop-invariant values. The extended problem also finds names whose values follow other closed forms of (see Variants).
Before SSA, the classic definition is: is a basic induction variable if its only assignments in the loop have the form with loop-invariant , and the analysis finds families of derived variables of the form that belong to each basic variable.
Variants
Classes of sequences. Gerlek, Stoltz and Wolfe classify the names of a loop by the sequence :
| class | closed form or behavior | typical source |
|---|---|---|
| linear | i = i + 1, p = p + 4 |
|
| polynomial | a polynomial in of degree | j = j + i with linear |
| geometric | , or plus a polynomial | k = 2 * k |
| wrap-around | one special first value, then an induction variable shifted by one iteration | w = i at the end of the body, used at the top |
| periodic | repeats with a fixed period | f = 1 - f, t = a; a = b; b = t |
| monotonic | only known to grow (or fall) | if (c) x = x + 1 |
Linear, polynomial and geometric sequences have closed forms, which chains of recurrences compute; LLVM's scalar evolution analysis represents them in that form.
Unknown or symbolic steps. The step can be a loop-invariant name, such as in i = i + s; the closed form is then symbolic, and it matters whether is positive.
Machine integers. On -bit integers, i = i + 1 wraps around at . The closed form is then (or undefined behavior for signed overflow in C), and many uses of the closed form, such as trip counts and dependence tests, need a proof that no wrap happens.
Trip count. A related question asks for the number of iterations, the least for which the exit test fails, from the closed form of the variable in the test.
Examples
A loop in SSA form
B0: i0 = 0; j0 = 5; k0 = 1; w0 = 99; f0 = 0
B1: i1 = φ(i0, i2)
j1 = φ(j0, j2)
k1 = φ(k0, k2)
w1 = φ(w0, i1)
f1 = φ(f0, f2)
t = 4 * i1 + 8
A[t] = j1 + k1 + w1 + f1
j2 = j1 + i1
k2 = 2 * k1
f2 = 1 - f1
i2 = i1 + 1
if i2 < N goto B1
The values at the start of iteration :
| name | class | closed form | |
|---|---|---|---|
i1 |
basic, step 1 | ||
t |
derived from i1 |
||
j1 |
polynomial | ||
k1 |
geometric | ||
w1 |
wrap-around | for , then | |
f1 |
periodic, period 2 |
The cycle of i1 in the SSA graph is i1 → i2 → i1, with one addition of the constant 1. The cycle of j1 adds i1, which is not loop invariant, so j1 is not linear; its increments are the linear sequence , so j1 is a polynomial of degree 2. The name w1 takes i1 around the back edge, so it holds the value of i1 from the previous iteration. A simple pass that only looks for v = v + c finds i1 and t and treats the other four as unknown.
Strength reduction
The derived variable t is , so it can be computed as its own basic variable, t1 = φ(8, t2) and t2 = t1 + 4, which replaces a multiplication by an addition (strength reduction). If i1 has no other use than the exit test, the test i2 < N can be rewritten as t2 < 4*N + 8, and i disappears (linear-function test replacement), provided does not overflow.
Non-examples
if (A[i] > 0) c = c + 1: the step depends on the data.cis monotonic, not linear, and has no closed form.x = x + ywhereyis changed in the loop by a non-inductive assignment: not an induction variable.i = i + 1on an 8-bit unsigned integer starting at 250 with step 3: the values are ; the closed form holds only modulo 256.
Known algorithms
- Classic dataflow detection. With reaching definitions and loop-invariance information, find the names whose only definitions in the loop are
v = v ± s, then grow families of derived variablesa*v + bone assignment at a time. This is the method of the dragon book and of early optimizing compilers. - SCCs of the SSA graph. In SSA form, a basic induction variable is a strongly connected component of the def-use graph of the loop that contains one header φ and otherwise only additions and subtractions of loop-invariant values. Tarjan's algorithm finds the components in one pass, in an order where each component comes after the components it uses, so derived variables can be classified in the same pass. Cooper, Simpson and Vick use this for operator strength reduction.
- Classification of sequences. Gerlek, Stoltz and Wolfe extend the SCC method to the classes above: a component whose increments are themselves linear gives a polynomial, a multiplication on the cycle gives a geometric sequence, a φ whose back-edge argument is an induction variable gives a wrap-around sequence, and a cycle of φ-functions gives a periodic one.
- Chains of recurrences. Represent each name by a chain of recurrences such as for
i1, and compute with the rules of the algebra: , . The closed form follows from the chain. LLVM's scalar evolution works this way. - Abstract interpretation. A numeric abstract interpretation with relational domains finds affine relations between variables, including ones that the syntactic methods miss, at a higher cost.
Bounds
Linear time for the syntactic problem. Over the SSA graph of a loop with names and def-use edges, the SCC method runs in time: Tarjan's algorithm is linear, and classifying a component reads each of its operations once. The classic dataflow method needs reaching definitions first, which costs more in the worst case.
The semantic problem is undecidable. Whether the values of a name form an affine sequence is a property of what the program computes, so by Rice's theorem no algorithm decides it for all loops. A direct reduction from the halting problem: given a program , build the loop whose iteration simulates one more step of and then runs v = v + 1, plus one more v = v + 1 in the iteration in which halts. If never halts, , an induction variable. If halts in step , then up to step and after it, which is not affine. So every practical analysis is conservative: it recognizes sufficient syntactic patterns, and treats every other name as unknown.
Reductions
- To strongly connected components. Basic induction variables are SCCs of the SSA def-use graph with a special shape (Tarjan).
- To recurrence solving. A name defined by a cycle with additions and multiplications is a linear recurrence in . Chains of recurrences give the closed forms for the classes that have one.
- Dependence testing needs it. A subscript
A[k]wherek = k + 2is not affine in the loop index as written; replacingkby its closed form (induction-variable substitution) makes it affine, and the dependence tests apply.
Applications
- Strength reduction of multiplications in derived variables, mainly address computations, and its SSA version, operator strength reduction.
- Induction variable elimination and linear-function test replacement, which remove counters that are only used to control the loop.
- Induction variable substitution before dependence analysis, so that subscripts become affine functions of the loop indices, and loop normalization.
- Trip counts for loop unrolling, vectorization and loop-invariant code motion with known iteration counts (loop-invariant code motion, vectorization).
- Bounds check elimination: a check
0 <= i < lenon a linearican be moved out of the loop and tested once at the ends of the range.
References
- Michael P. Gerlek, Eric Stoltz and Michael Wolfe, Beyond induction variables: detecting and classifying sequences using a demand-driven SSA form, ACM Transactions on Programming Languages and Systems 17(1), 1995, the strongly connected regions of the SSA graph, and the classes of linear, polynomial, geometric, wrap-around, periodic and monotonic sequences
- Keith D. Cooper, L. Taylor Simpson and Christopher A. Vick, Operator strength reduction, ACM Transactions on Programming Languages and Systems 23(5), 2001, the detection of induction variables as strongly connected components of the SSA graph, found with Tarjan's algorithm, and their use for strength reduction
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Chapter 9 (induction variables and reduction in strength among the machine-independent optimizations)
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 4 (preliminary transformations, induction-variable substitution so that subscripts become affine in the loop indices)
- Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, the chapter on loop optimizations (identifying basic and dependent induction variables, strength reduction and linear-function test replacement)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W0 stub |
|
Tag TMQVV2 Edit this page History Report an error Markdown