induction variables

36
Induction Variables Region-Based Analysis Meet and Closure of Transfer Functions Affine Functions of Reference Variables Jeffrey D. Ullman Stanford University

Upload: xia

Post on 22-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Induction Variables. Region-Based Analysis Meet and Closure of Transfer Functions Affine Functions of Reference Variables. Jeffrey D. Ullman Stanford University. Regions. A set of nodes N and edges E is a region if: There is a header h in N that dominates all nodes in N. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Induction Variables

Induction VariablesRegion-Based AnalysisMeet and Closure of Transfer FunctionsAffine Functions of Reference VariablesJeffrey D. UllmanStanford University

Page 2: Induction Variables

2

Regions A set of nodes N and edges E is a region if:

1. There is a header h in N that dominates all nodes in N.

2. If n ≠ h is in N, then all predecessors of n are also in N.

I.e., the header is the sole entry into the region.

3. E consists of all edges between nodes in N, possibly excluding nodes that enter h from somewhere in N.

Note: Every node is a region by itself.

Page 3: Induction Variables

3

T1-T2 Reduction For reducible flow graphs, we can “reduce” the

graph by two region-creating transformations. T1: Remove a self-loop from a node. T2: combine two nodes n and m such that m’s only

predecessor is n, and m is not the entry. Say “n consumes m.” n

m

Page 4: Induction Variables

4

Example: T1-T2 Reduction

1

35

24

T2

T2

Page 5: Induction Variables

5

Example: T1-T2 Reduction

14

5

23 T1

Page 6: Induction Variables

6

Example: T1-T2 Reduction

14

5

23

T2

Page 7: Induction Variables

7

Example: T1-T2 Reduction

1234

5

T2

Page 8: Induction Variables

8

Example: T1-T2 Reduction

12345 T1

Page 9: Induction Variables

9

Example: T1-T2 Reduction

12345

Page 10: Induction Variables

10

Regions Constructed During T1-T2 Reduction As we reduce, each node represents a region of

the original flow graph, and each edge represents one or more edges of the original flow graph.

T2: Take the union of the two regions represented by the two nodes, plus all edges represented by the edge between them.

T1: Add (to the region represented by the node) those edges represented by the loop that was removed.

Page 11: Induction Variables

11

Regions Constructed – (2) Easy inductive proof: every set of nodes and

edges constructed is a region. Key point: When you use T2, the header of the

resulting region is the header of the region that consumes the other.

Page 12: Induction Variables

12

Example: T1-T2 Reduction

1

3

5

24

Page 13: Induction Variables

13

Region-Based Analysis First, compute for each region, smallest-to-

largest, some transfer functions:1. For region R constructed by T2 from S consuming

T: fR,IN[S] = identity function; fR,IN[T] = meet over paths from the beginning of the header of R to the beginning of the header of T, going only through S.

2. For region R with header h constructed from S by T1: fR,IN[S] = meet over paths within R from h to h.

3. For each region R with exit block ( = has an out-edge not in R) B, fR,OUT[B] = meet over paths within R from header of R to end of B.

Page 14: Induction Variables

14

Example: Transfer Functions

1

3

5

24

U

S

fU,IN[S] = f1

fS,OUT[2] = f2(f3f2)*

fU,OUT[2] = f1f2(f3f2)*f U,OUT[4] = f1f4

Like regular expressions.= f2 ∧ f2f3f2 ∧ f2f3f2f3f2 ∧ .. . .

Page 15: Induction Variables

15

Additional Assumptions We can compute the meet of transfer functions:

[f ∧ g](x) = f(x) ∧ g(x). Needed when we combine transfer functions from

header to several exits. We can compute the closure f* for each transfer

function f, given by f* = f0 ∧ f1 ∧ f2 ∧ … Note f0 = identity, f1 = f, f2 = f composed with f, etc.. Needed when we apply T1 to consume a back edge.

Page 16: Induction Variables

16

Example: RD’s Meet of transfer functions f(x) = (x - K) ∪ G and

g(x) = (x - K’) ∪ G’ corresponds to the paths of f and g in parallel.

Thus, [f∧g](x) = (x – (K ∩ K’)) ∪ (G ∪ G’). f*(x) = x ∪ f(x) ∪ f(f(x)) ∪ … = x ∪ (x – K) ∪ G ∪

((x - K ∪ G) – K) ∪ G … = x ∪ G. f(x)

f(f(x))

Page 17: Induction Variables

17

Example: Meet

1

3

5

24

UV

fU,OUT[2] = f1f2(f3f2)*fU,OUT[4] = f1f4

fV,IN[5] = f1f4 ∧ f1f2(f3f2)*Region U consumesnode 5 to makeregion V.

Page 18: Induction Variables

18

Region-Based Analysis: Basis If R is a region consisting of a single node

(block), and f is the transfer function for that block, then fR,OUT[R] = f. Note: we use the same name for the block and the

region consisting of only that block.

Page 19: Induction Variables

19

Region-Based Analysis: Induction Apply T2: region R consumes region S to make T.

1. fT,IN(R) = the identity function.2. fT,IN(S) = meet of fR,OUT[B] for all predecessors B of the

header of S.3. For blocks B in R, fT,OUT[B] = fR,OUT[B].4. For blocks B in S, fT,OUT[B] = fT,IN(S) fS,OUT[B].

Page 20: Induction Variables

20

Induction – (2) Apply T1: add back edges to region S to make R.

1. Compute g = meet of fS,OUT[B] for all predecessors B of the header of S.

2. fR,IN[S] = g*. Notice that the headers of R and S are the same node, but

there are paths within R that are not in S, yet take you to the header of S.

3. For all blocks B in S (and therefore in R) let fR,OUT[B] = fR,IN[S]fS,OUT[B].

Page 21: Induction Variables

21

Now We Work Large-to-Small Let e be the header of the region that is the

entire flow graph. Start with IN(e) = vENTRY. Proceed inward, from larger regions to smaller.

1. If R was constructed from S by T1, and both have header h, replace IN(h) by fR,IN(S)(IN(h)).

2. If T, with header h’ was constructed from R and S, with R consuming S, and S has header h, initialize IN(h) to fT,IN(S)(IN(h’).

Page 22: Induction Variables

22

Examples: Top-Down Finale

1

3

5

24R

IN[1] = fR,IN[V](vENTRY) = f*V,OUT[5] ](vENTRY)

UV

IN[5] = fV,IN[5](IN(1))

Page 23: Induction Variables

23

New Topic: Symbolic Analysis Values V are mappings from variables to

expressions involving reference variables. Example: m(a) = 2i; m(b) = i + j + 1.

i and j are the reference variables. We are going to deal with two complicated

types of functions:1. Mappings from variables to expressions, as above.2. Transfer functions from mappings (in) to mappings

(out).

Page 24: Induction Variables

24

Application to Induction Variables

Reference variables are basic induction variables = counts of the number of times around some loop.

This count is well-defined for “natural loops.” Set of values V for the framework = affine

mappings = each variable is mapped to either:1. A linear function of the reference variables, or2. NAA = “Not an affine” mapping, or3. UNDEF = top element = “nothing known.”

Page 25: Induction Variables

25

Example: Affine Mappingm(a) = 2i + 3j + 4m(b) = NAAm(c) = 4i + 1m(d) = UNDEF

Page 26: Induction Variables

26

Lattice for Each Variable

UNDEF

All affine functions of reference variables

NAA

LessIgnorance

Note: The lattice for V is the productof one of these for each variable.

Page 27: Induction Variables

27

Induction Variable Discovery Natural application of region-based analysis. Each loop is a region, and its induction variables

can be discovered by a framework based on affine mappings.

Page 28: Induction Variables

28

Example: A Transfer Function Let f be the transfer function associated with a

block containing only a = a+10. Let m be the input mapping for the block; then

f(m) = m’, where:

m’(a) = m(a) + 10. m’(x) = m(x) for all x ≠ a.

Book uses the notation f(m)(a) = m(a) + 10, etc., to avoid having to name f(m).

a = a + 10

m

m'

Question: What if m(a)is NAA or UNDEF?

Page 29: Induction Variables

29

The Meet Operator (f ∧ g)(m)(x) =

f(m)(x) if f(m)(x) = g(m)(x). f(m)(x) if g(m)(x) = UNDEF. g(m)(x) if f(m)(x) = UNDEF. NAA otherwise.

Page 30: Induction Variables

30

Example: Some Analysis

a=b+3

b=a+2

a=a+5

B1

B5

B2

B4

B3 fB3(m)(a) = m(a)(b) = m(a)+2

fB2(m)(a) = m(a)+5(b) = m(b)

fB4(m)(a) = m(b)+3(b) = m(b)

R

fR,OUT[B4](m)(a) = m(a)+5(b) = m(a)+2

Page 31: Induction Variables

31

Example: Meet

a=b+3

b=a+2

a=a+5

B1

B5

B2

B4

B3

fB2(m)(a) = m(a)+5(b) = m(b)

R

fR,OUT[B4](m)(a) = m(a)+5(b) = m(a)+2

S

fS,OUT[B5](m)(a) = m(a)+5(b) = NAA

Page 32: Induction Variables

32

Handling Loop Regions Treat the iteration count i as a basic induction

variable. If f(m)(x) = m(x)+c, then fi(m)(x) = m(x)+ci. Some other cases, e.g., where m(x) is an affine

function of basic induction variables, in book.

Page 33: Induction Variables

33

Example: The Entire Loop

a=b+3

b=a+2

a=a+5

B1

B5

B2

B4

B3

S

fS,OUT[B5](m)(a) = m(a)+5(b) = NAA

TfS,OUT[B5](m)(a) = m(a)

+5i(b) = NAA

i

OUTi[B3](m)(a) = m(a)+5i(b) = m(a)+5i+2

OUTi[B4](m)(a) = m(a)+5i+5(b) = m(a)+5i+2

OUTi[B2](m)(a) = m(a)+5i+5(b) = NAA

OUTi[B5](m)(a) = m(a)+5i+5(b) = NAA

Page 34: Induction Variables

34

Taking Advantage of Affine Expressions

Replace the loop counter variable by one of the induction variables (variables that are mapped to an affine expression of the loop count at the point were the loop count is tested).

Page 35: Induction Variables

35

Example: Assume i is Loop Counter

i<10?

a=b+3

b=a+2

a=a+5

i=i+1

B1

B5

B2

B4

B3

i=0 a=30 Here, the value of a is

m(a)+5i, where m(a) is thevalue of a before enteringthe loop, and i is the numberof iterations of the loop so far.

Since a=30 when i=0,replace “i<10?” by“a<80?”.

If i is dead on exit fromthe loop, delete “i=i+1”and “i=0”.

And since i is an inductionvariable, if not dead we canjust assign i = 10 at the exit.

Page 36: Induction Variables

36

Example: Revised Code

a<80?

a=b+3

b=a+2

a=a+5

B1

B5

B2

B4

B3

a=30