+ All Categories
Home > Documents > Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1...

Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1...

Date post: 31-May-2020
Category:
Upload: others
View: 39 times
Download: 0 times
Share this document with a friend
203
Some Generalizations Uday P. Khedker Department of Computer Science and Engineering, Indian Institute of Technology, Bombay May 2011
Transcript
Page 1: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

Some Generalizations

Uday P. Khedker

Department of Computer Science and Engineering,

Indian Institute of Technology, Bombay

May 2011

Page 2: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

Part 1

About These Slides

Page 3: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: About These Slides 1/57

Copyright

These slides constitute the lecture notes for

• MACS L111 Advanced Data Flow Analysis course at CambridgeUniversity, and

• CS 618 Program Analysis course at IIT Bombay.

They have been made available under GNU FDL v1.2 or later (purely foracademic or research use) as teaching material accompanying the book:

• Uday Khedker, Amitabha Sanyal, and Bageshri Karkare. Data Flow

Analysis: Theory and Practice. CRC Press (Taylor and FrancisGroup). 2009.

Apart from the above book, some slides are based on the material fromthe following books

• M. S. Hecht. Flow Analysis of Computer Programs. ElsevierNorth-Holland Inc. 1977.

• F. Nielson, H. R. Nielson, and C. Hankin. Principles of ProgramAnalysis. Springer-Verlag. 1998.

May 2011 Uday Khedker

Page 4: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: About These Slides 2/57

Outline

• Partial Redundancy Elimination

• Introduction to Constant Propagation

• Theoretical Abstractions in Data Flow Analysis◮ The world of data flow values◮ The world of functions and operations that compute data values

(Not today)◮ Results of data flow analysis (Not today)◮ Algorithms for performing data flow analysis (Not today)

May 2011 Uday Khedker

Page 5: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

Part 2

Partial Redundancy Elimination

Page 6: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 3/57

Precursor: Common Subexpression Elimination

1 a ∗ b 1 2 a ∗ b 2

3 a ∗ b 3

0 if () 0

May 2011 Uday Khedker

Page 7: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 3/57

Precursor: Common Subexpression Elimination

1 a ∗ b 1 2 a ∗ b 2

3 a ∗ b 3

0 if () 0 • a and b are not modified alongpaths 1 → 3 and 2 → 3

May 2011 Uday Khedker

Page 8: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 3/57

Precursor: Common Subexpression Elimination

1 a ∗ b 1 2 a ∗ b 2

3 a ∗ b 3

0 if () 0 • a and b are not modified alongpaths 1 → 3 and 2 → 3

• Computation of a ∗ b in 3 isredundant

May 2011 Uday Khedker

Page 9: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 3/57

Precursor: Common Subexpression Elimination

1 t = a ∗ b 12 t = a ∗ b 2

3 t 3

0 if () 0 • a and b are not modified alongpaths 1 → 3 and 2 → 3

• Computation of a ∗ b in 3 isredundant

• Previous value can be used

May 2011 Uday Khedker

Page 10: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 4/57

Partial Redundancy Elimination

1 a ∗ b 1 2 a = 5 2

3 a ∗ b 3

0 if () 0

May 2011 Uday Khedker

Page 11: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 4/57

Partial Redundancy Elimination

1 a ∗ b 1 2 a = 5 2

3 a ∗ b 3

0 if () 0• Computation of a ∗ b in 3 is

May 2011 Uday Khedker

Page 12: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 4/57

Partial Redundancy Elimination

1 a ∗ b 1 2 a = 5 2

3 a ∗ b 3

0 if () 0• Computation of a ∗ b in 3 is

◮ redundant along path 1 → 3,but . . .

May 2011 Uday Khedker

Page 13: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 4/57

Partial Redundancy Elimination

1 a ∗ b 1 2 a = 5 2

3 a ∗ b 3

0 if () 0• Computation of a ∗ b in 3 is

◮ redundant along path 1 → 3,but . . .

◮ not redundant along path 2 → 3

May 2011 Uday Khedker

Page 14: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 5/57

Code Hoisting for Partial Redundancy Elimination

1 a ∗ b 1 2 a = 5 2

3 a ∗ b 3

0 if () 0

May 2011 Uday Khedker

Page 15: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 5/57

Code Hoisting for Partial Redundancy Elimination

1 a ∗ b 1 2a = 5a ∗ b 2

3 a ∗ b 3

0 if () 0

• Computation of a ∗ b in 3becomes totally redundant

• Can be deleted

May 2011 Uday Khedker

Page 16: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 6/57

PRE Subsumes Loop Invariant Movement

1 a = b ∗ c 1

2 a = b ∗ c 1

3 a = b ∗ c 3

May 2011 Uday Khedker

Page 17: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 6/57

PRE Subsumes Loop Invariant Movement

1 a = b ∗ c 1

2 a = b ∗ c 1

3 a = b ∗ c 3

1 a = b ∗ c 1

2 a = b ∗ c 1

2 a = b ∗ c 3

May 2011 Uday Khedker

Page 18: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 6/57

PRE Subsumes Loop Invariant Movement

1 a = b ∗ c 1

2 a = b ∗ c 1

3 a = b ∗ c 3

1 a = b ∗ c 1

2 a = b ∗ c 1

2 a = b ∗ c 3

1 t = b ∗ c 1

2 a = t 1

3 a = b ∗ c 3

May 2011 Uday Khedker

Page 19: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)

t1 = t0 + i ∗ 4a = mem[t1]i = i + 1

May 2011 Uday Khedker

Page 20: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)

t1 = t0 + i ∗ 4a = mem[t1]i = i + 1

i = 0t0 = base(A)t1 = t0 + i ∗ 4

t1 = t1 + 4a = mem[t1]i = i + 1

• ∗ and + in the loop have been replaced by +

• i = i + 1 in the loop has been eliminated

May 2011 Uday Khedker

Page 21: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)

t1 = t0 + i ∗ 4a = mem[t1]i = i + 1

• Delete i = i + 1

May 2011 Uday Khedker

Page 22: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)

t1 = t0 + i ∗ 4a = mem[t1]i = i + 1

• Delete i = i + 1

• Expression t0 + i ∗ 4becomes loop invariant

May 2011 Uday Khedker

Page 23: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)t1 = t0 + i ∗ 4

t1 = t1 + 4a = mem[t1]i = i + 1

• Delete i = i + 1

• Expression t0 + i ∗ 4becomes loop invariant

• Hoist it and increment t1in the loop

May 2011 Uday Khedker

Page 24: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 7/57

PRE Can be Used for Strength Reduction

i = 0t0 = base(A)t1 = t0 + i ∗ 4

t1 = t1 + 4a = mem[t1]i = i + 1

i = 0t0 = base(A)t1 = t0 + i ∗ 4

t1 = t1 + 4a = mem[t1]i = i + 1

• ∗ and + in the loop have been replaced by +

• i = i + 1 in the loop has been eliminated

May 2011 Uday Khedker

Page 25: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 8/57

Performing Partial Redundancy Elimination

1. Identify partial redundancies

2. Identify program points where computations can be inserted

3. Insert expressions

4. Partial redundancies become total redundancies=⇒ Delete them.

Morel-Renvoise Algorithm (CACM, 1979.)

May 2011 Uday Khedker

Page 26: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 9/57

Defining Hoisting Criteria

• An expression can be safely inserted at a program point p if it is

Available at p Anticipable at pStart

p

End

a ∗ b

a ∗ b

a ∗ b

Start

May 2011 Uday Khedker

Page 27: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 9/57

Defining Hoisting Criteria

• An expression can be safely inserted at a program point p if it is

Available at p Anticipable at pStart

p

End

a ∗ b

a ∗ b

a ∗ b

Start Start

p

End

Start

a ∗ b

a ∗ b

May 2011 Uday Khedker

Page 28: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 9/57

Defining Hoisting Criteria

• An expression can be safely inserted at a program point p if it is

Available at p Anticipable at pStart

p

End

a ∗ b

a ∗ b

a ∗ b

Start Start

p

End

Start

a ∗ b

a ∗ b

◮ If it is available at p, thenthere is no need to insertit at p.

May 2011 Uday Khedker

Page 29: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 9/57

Defining Hoisting Criteria

• An expression can be safely inserted at a program point p if it is

Available at p Anticipable at pStart

p

End

a ∗ b

a ∗ b

a ∗ b

Start Start

p

End

Start

a ∗ b

a ∗ b

◮ If it is available at p, thenthere is no need to insertit at p.

◮ If it is anticipable at pthen all such occurrenceshould be hoisted to p.

May 2011 Uday Khedker

Page 30: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 9/57

Defining Hoisting Criteria

• An expression can be safely inserted at a program point p if it is

Available at p Anticipable at pStart

p

End

a ∗ b

a ∗ b

a ∗ b

Start Start

p

End

Start

a ∗ b

a ∗ b

◮ If it is available at p, thenthere is no need to insertit at p.

◮ If it is anticipable at pthen all such occurrenceshould be hoisted to p.

◮ An expression should be

hoisted to p provided it

can be hoisted to p along

all paths from p to exit.

May 2011 Uday Khedker

Page 31: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

May 2011 Uday Khedker

Page 32: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

b ∗ c

b ∗ c b ∗ c

May 2011 Uday Khedker

Page 33: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, or

May 2011 Uday Khedker

Page 34: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, or

a ∗ c a ∗ ca =

May 2011 Uday Khedker

Page 35: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

a ∗ c a ∗ ca =

May 2011 Uday Khedker

Page 36: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block b ∗ c

May 2011 Uday Khedker

Page 37: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

May 2011 Uday Khedker

Page 38: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

May 2011 Uday Khedker

Page 39: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

b ∗ c b ∗ c

b ∗ c b ∗ c

May 2011 Uday Khedker

Page 40: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 10/57

Hoisting Criteria

• Safety of hoisting to the exit of a block .

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block .Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

b ∗ c

b ∗ c b ∗ c

a ∗ c a ∗ ca =

b ∗ c

b ∗ c b ∗ c

b ∗ c b ∗ c

May 2011 Uday Khedker

Page 41: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

What does this slide show?

• Four examples

• For each example◮ statements in blue

enable hoisting◮ statements in red

prohibit hoisting

May 2011 Uday Khedker

Page 42: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 1)

1

2

3

4

May 2011 Uday Khedker

Page 43: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 1)

1

2

3

4

D.2.b

May 2011 Uday Khedker

Page 44: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 1)

1

2

3

4What if we insert

• a = 2 in 3?

• a = 2 in 3 and a ∗ b in 4?

May 2011 Uday Khedker

Page 45: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 2)

1

2

3

4

May 2011 Uday Khedker

Page 46: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 2)

1

2

3

4

May 2011 Uday Khedker

Page 47: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(Example 2)

1

2

3

4

May 2011 Uday Khedker

Page 48: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 3)

1

2

3

4

May 2011 Uday Khedker

Page 49: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 3)

1

2

3

4

May 2011 Uday Khedker

Page 50: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 4)

1

2

3

4

May 2011 Uday Khedker

Page 51: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 4)

1

2

3

4

May 2011 Uday Khedker

Page 52: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• Safety of hoisting to the exit of a block.

S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Safety of hoisting to the entry of a block.Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

• Desirability of hoisting to the entry of a block.Should be hoisted only if

D.1 it is partially available, andD.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 4)

1

2

3

4

May 2011 Uday Khedker

Page 53: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 11/57

Applying the Hoisting Criteria

• S.1 Should be hoisted only if it can be hoisted tothe entry of all succesors

• Should be hoisted only if

S.2 it is upwards exposed, orS.3 it can be hoisted to its exit and is transparent

in the block

Should be hoisted only if• D.1 it is partially available, and

D.2 For each predecessor

D.2.a it is hoisted to its exit, or

D.2.b is available at its exit.

a ∗ b

a = 5

a ∗ b

a ∗ b

(Example 4)

1

2

3

4

May 2011 Uday Khedker

Page 54: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 12/57

First Level Global Data Flow Properties in PRE

• Partial Availability.

PavInn =

BI n is Start block⋃

p∈pred(n)

PavOutp otherwise

PavOutn = Genn ∪ (PavInn − Killn)

• Total Availability.

AvInn =

BI n is Start block⋂

p∈pred(n)

AvOutp otherwise

AvOutn = Genn ∪ (AvInn − Killn)

May 2011 Uday Khedker

Page 55: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Desirability: D.1

Expressions should be partially available, and

May 2011 Uday Khedker

Page 56: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Safety: S.2

Expressions should be upwards exposed, or

May 2011 Uday Khedker

Page 57: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Safety: S.3

Expressions can be hoisted to the exit and are transparent in the block

May 2011 Uday Khedker

Page 58: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Desirability: D.2.a

For every predecessor, expressions can be hoisted to its exit, or

May 2011 Uday Khedker

Page 59: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Desirability: D.2.b

. . . expressions are available at the exit of the same predecessor

May 2011 Uday Khedker

Page 60: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

Safety: S.1

Expressions should be hoisted to the exit of a blockif they can be hoisted to the entry of all succesors

May 2011 Uday Khedker

Page 61: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 13/57

PRE Data Flow Equations

Inn = PavInn ∩(AntGenn ∪ (Outn − Killn)

)

p∈pred(n)

(Outp ∪ AvOutp

)

Outn =

BI n is End block⋂

s∈succ(n)

Ins otherwise

May 2011 Uday Khedker

Page 62: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 14/57

Deletion Criteria in PRE

• An expression is redundant in node n if

◮ it can be placed at the entry (i.e. can be “hoisted” out) of n, AND◮ it is upwards exposed in node n.

Redundantn = Inn ∩ AntGenn

• A hoisting path for an expression e begins at n if e ∈ Redundantn

• This hoisting path extends against the control flow.

May 2011 Uday Khedker

Page 63: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 15/57

Insertion Criteria in PRE

• An expression is inserted at the exit of node n is

◮ it can be placed at the exit of n, AND◮ it is not available at the exit of n, AND◮ it cannot be hoisted out of n, OR it is modified in n.

Insertn = Outn ∩ (¬AvOutn) ∩ (¬Inn ∪ Killn)

• A hoisting path for an expression e ends at n if e ∈ Insertn

May 2011 Uday Khedker

Page 64: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 16/57

Performing PRE by Computing In/Out : Simple Cases

2 c = a ∗ b 2 3 a = 5 3

4 d = a ∗ b 3

1 c = a ∗ b 1

⇒ 2 t = a ∗ bc = t 2 3 a = 5

t = a ∗ b 3

4 d = t 4

1 c = a ∗ b 1

1 a = b ∗ c 1

2 a = b ∗ c 2

3 a = b ∗ c 3

1 t = b ∗ c 1

2 a = t 1

3 a = b ∗ c 3

May 2011 Uday Khedker

Page 65: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 16/57

Performing PRE by Computing In/Out : Simple Cases

2 c = a ∗ b 2 3 a = 5 3

4 d = a ∗ b 3

1 c = a ∗ b 1

0

00

1

0

11

0

⇒ 2 t = a ∗ bc = t 2 3 a = 5

t = a ∗ b 3

4 d = t 4

1 c = a ∗ b 1

1 a = b ∗ c 1

2 a = b ∗ c 2

3 a = b ∗ c 3

0

11

00

0

1 t = b ∗ c 1

2 a = t 1

3 a = b ∗ c 3

May 2011 Uday Khedker

Page 66: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 16/57

Performing PRE by Computing In/Out : Simple Cases

2 c = a ∗ b 2 3 a = 5 3

4 d = a ∗ b 3

1 c = a ∗ b 1

0

00

1

0

11

0

⇒ 2 t = a ∗ bc = t 2 3 a = 5

t = a ∗ b 3

4 d = t 4

1 c = a ∗ b 1

1 a = b ∗ c 1

2 a = b ∗ c 2

3 a = b ∗ c 3

0

11

00

0

RedundancyInsertion

1 t = b ∗ c 1

2 a = t 1

3 a = b ∗ c 3

May 2011 Uday Khedker

Page 67: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

May 2011 Uday Khedker

Page 68: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

May 2011 Uday Khedker

Page 69: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

May 2011 Uday Khedker

Page 70: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

May 2011 Uday Khedker

Page 71: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

May 2011 Uday Khedker

Page 72: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

May 2011 Uday Khedker

Page 73: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(d)

1

2

3

4

May 2011 Uday Khedker

Page 74: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(d)

1

2

3

4

0

0

0

01

00

0

May 2011 Uday Khedker

Page 75: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(d)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(e)

1

2

3

4

May 2011 Uday Khedker

Page 76: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(d)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(e)

1

2

3

4

0

1

1

11

11

0

May 2011 Uday Khedker

Page 77: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 17/57

Tutorial Problems for PRE

a ∗ b

a ∗ b

a ∗ b

a ∗ b

(a)

1

2

3

4

0

0

0

0

0

0

0

0

a ∗ b

a = 5

a ∗ b

a ∗ b

(b)

1

2

3

4

0

0

0

1

1

0

0

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(c)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(d)

1

2

3

4

0

0

0

01

00

0

a ∗ b

a ∗ b

a ∗ ba = 5

a ∗ b

(e)

1

2

3

4

0

1

1

11

11

0

Redundancy Insertion

May 2011 Uday Khedker

Page 78: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 18/57

Further Tutorial Problem for PRE

1 b ∗ c 1

2 a ∗ b 2

3 a = . . . 34 d = . . . 4

5 b ∗ c 5

6 b ∗ c 6

Let {a ∗ b, b ∗ c} ≡ bit string 11

Node n Killn AntGenn PavInn AvOutn

1 00 00 00 00

2 00 10 11 10

3 10 00 11 00

4 00 00 11 10

5 00 01 11 01

6 00 00 11 01

• Compute Inn/Outn/Redundantn/Insertn

• Identify hoisting paths

May 2011 Uday Khedker

Page 79: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 19/57

Result of PRE Data Flow Analysis of the Running Example

Bit vector a ∗ b a + b a − b a − c b + c

Block

Global Information

Constantinformation

Iteration # 1 Changes initeration # 2

Changes initeration # 3

PavInn AvOutn Outn Inn Outn Inn Outn Inn

n8 11111 00011 00000 00011 00001

n7 11101 11000 00011 01001 00001

n6 11101 11001 01001 01001 01000

n5 11101 11000 01001 01001 01000

n4 11100 10100 01001 11100 11000

n3 11101 10000 01000 01001 00001

n2 10001 00010 00011 00000 00001

n1 00000 10001 00000 00000

May 2011 Uday Khedker

Page 80: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 81: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 82: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 83: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 84: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 85: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 20/57

Hoisting Paths for Some Expressions in the Running Example

n1b = 4;a = b + c ;d = a ∗ b;

n1

n2 b = a − c ; n2

n3 c = b + c ; n3

n4c = a ∗ b ;f (a − b);

n4

n5 d = a + b ; n5

n6 f ( b + c ); n6

n7 g( a + b ); n7

n8h(a − c);f ( b + c );

n8

May 2011 Uday Khedker

Page 86: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Partial Redundancy Elimination 21/57

Optimized Version of the Running Example

n1

b = 4;t2 = b + c ;a = t2;t0 = a ∗ b;d = t0;

n1

n2

b = c ;f (a − c);t2 = b + c ;

n2

n3c = t2t1 = a + b; n3

n4

c = t0;f (a − b);t2 = b + c ;

n4

n5d = t1;t2 = b + c ; n5

n6 f (t2); n6

n7 g(t1); n7

n8h(a − c);f (t2);

n8

May 2011 Uday Khedker

Page 87: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

Part 3

The Need for a More General Setting

Page 88: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 22/57

What We Have Seen So Far . . .

Analysis EntityAttribute

Pathsat p

Live variables Variables Use Starting at p Some

AvailableExpressions Availability Reaching p All

expressions

Partially availableExpressions Availability Reaching p Some

expressions

AnticipableExpressions Use Starting at p All

expressions

ReachingDefinitions Availability Reaching p Some

definitions

Partial redundancyExpressions

ProfitableInvolving p All

elimination hoistability

May 2011 Uday Khedker

Page 89: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

May 2011 Uday Khedker

Page 90: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

May 2011 Uday Khedker

Page 91: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

May 2011 Uday Khedker

Page 92: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

May 2011 Uday Khedker

Page 93: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 94: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 95: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 96: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 97: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

May 2011 Uday Khedker

Page 98: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

May 2011 Uday Khedker

Page 99: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

May 2011 Uday Khedker

Page 100: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉〈×,×, 3, 2〉

May 2011 Uday Khedker

Page 101: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

May 2011 Uday Khedker

Page 102: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

May 2011 Uday Khedker

Page 103: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

ExecutionSequence

〈a, b, c , d〉

n1

〈?, ?, ?, ?〉 IN

〈1, 2, 3, ?〉 OUT

n2〈1, 2, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉

n2〈2, 1, 3, 2〉

n3〈2, 1, 3, 2〉 . . .

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 104: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 23/57

An Introduction to Constant Propagation

n1

a = 1b = 2

c = a+ b

n1

n2c = a+ b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a+ b

n3

Summary Values

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈2, 1, 3, 2〉

Desired Solution

May 2011 Uday Khedker

Page 105: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 24/57

Data Flow Values for Constant Propagation

• Tuples of the form 〈ξ1, ξ2, . . . , ξk〉 where ξi is the data flow valuefor i th variable.

Unlike bit vector frameworks, value ξi is not 0 or 1 (i.e. true orfalse). Instead, it is one of the following:

◮ ? indicating that not much is known about the constantness ofvariable vi

◮ × indicating that variable vi does not have a constant value◮ An integer constant c1 if the value of vi is known to be c1 at compile

time

• Alternatively, sets of pairs 〈vi , ξ i〉 for each variable vi .

May 2011 Uday Khedker

Page 106: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 25/57

Confluence Operation for Constant Propagation

• Confluence operation 〈a, c1〉 ⊓ 〈a, c2〉

⊓ 〈a, ?〉 〈a,×〉 〈a, c1〉

〈a, ?〉 〈a, ?〉 〈a,×〉 〈a, c1〉〈a,×〉 〈a,×〉 〈a,×〉 〈a,×〉

〈a, c2〉 〈a, c2〉 〈a,×〉If c1 = c2 〈a, c1〉Otherwise 〈a,×〉

• This is neither ∩ nor ∪.

What are its properties?

May 2011 Uday Khedker

Page 107: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 26/57

Flow Functions for Constant Propagation

• Flow function for r = a1 ∗ a2

mult 〈a1, ?〉 〈a1,×〉 〈a1, c1〉

〈a2, ?〉 〈r , ?〉 〈r ,×〉 〈r , ?〉〈a2,×〉 〈r ,×〉 〈r ,×〉 〈r ,×〉〈a2, c2〉 〈r , ?〉 〈r ,×〉 〈r , (c1 ∗ c2)〉

• This cannot be expressed in the form

fn(X ) = Genn ∪ (X − Killn)

where Genn and Killn are constant effects of block n.

May 2011 Uday Khedker

Page 108: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

May 2011 Uday Khedker

Page 109: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

Iteration#1

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, 2〉

〈1, 2, 3, 2〉

〈2, 1, 3, 2〉

May 2011 Uday Khedker

Page 110: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

Iteration#1

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, 2〉

〈1, 2, 3, 2〉

〈2, 1, 3, 2〉

Iteration#2

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

May 2011 Uday Khedker

Page 111: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

Iteration#1

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, 2〉

〈1, 2, 3, 2〉

〈2, 1, 3, 2〉

Iteration#2

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

Iteration#3

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3,×〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

May 2011 Uday Khedker

Page 112: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

Desiredsolution

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈2, 1, 3, 2〉

Iteration#1

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, 2〉

〈1, 2, 3, 2〉

〈2, 1, 3, 2〉

Iteration#2

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

Iteration#3

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3,×〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

May 2011 Uday Khedker

Page 113: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 27/57

Round Robin Iterative Analysis for Constant Propagation

n1

a = 1b = 2

c = a + b

n1

n2c = a + b

d = a ∗ bn2

n3

d = c − 1a = 2b = 1

c = a + b

n3

Desiredsolution

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈×,×, 3, 2〉

〈2, 1, 3, 2〉

Iteration#1

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, ?〉

〈1, 2, 3, 2〉

〈1, 2, 3, 2〉

〈2, 1, 3, 2〉

Iteration#2

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3, 2〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

Iteration#3

〈?, ?, ?, ?〉

〈1, 2, 3, ?〉

〈×,×, 3,×〉

〈×,×,×,×〉

〈×,×,×,×〉

〈2, 1, 3,×〉

May 2011 Uday Khedker

Page 114: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 28/57

Issues in Data Flow Analysis

Acceptable

Operations

Desired

Solutions

Pract

ical

Algor

ithms

Data

Flow

Val

ues

May 2011 Uday Khedker

Page 115: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 28/57

Issues in Data Flow Analysis

Acceptable

Operations

Desired

Solutions

Pract

ical

Algor

ithms

Data

Flow

Val

ues

• Representation

• Approximation: PartialOrder, Lattices

May 2011 Uday Khedker

Page 116: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 28/57

Issues in Data Flow Analysis

Acceptable

Operations

Desired

Solutions

Pract

ical

Algor

ithms

Data

Flow

Val

ues

• Representation

• Approximation: PartialOrder, Lattices

• Merge: Commutativity,Associativity, Idempotence

• Flow Functions: Monotonicity,Distributivity, Boundedness,Separability

May 2011 Uday Khedker

Page 117: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 28/57

Issues in Data Flow Analysis

Acceptable

Operations

Desired

Solutions

Pract

ical

Algor

ithms

Data

Flow

Val

ues

• Representation

• Approximation: PartialOrder, Lattices

• Merge: Commutativity,Associativity, Idempotence

• Flow Functions: Monotonicity,Distributivity, Boundedness,Separability

• Existence

• Safety (soundness)

• Precision

May 2011 Uday Khedker

Page 118: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: The Need for a More General Setting 28/57

Issues in Data Flow Analysis

Acceptable

Operations

Desired

Solutions

Pract

ical

Algor

ithms

Data

Flow

Val

ues

• Representation

• Approximation: PartialOrder, Lattices

• Merge: Commutativity,Associativity, Idempotence

• Flow Functions: Monotonicity,Distributivity, Boundedness,Separability

• Existence

• Safety (soundness)

• Precision

• Complexity, efficiency

• Convergence

• Initialization

May 2011 Uday Khedker

Page 119: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

Part 4

Data Flow Values: An Overview

Page 120: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 29/57

Data Flow Values: An Outline of Our Discussion

• The need to define the notion of abstraction

• Lattices, variants of lattices

• Relevance of lattices for data flow analysis◮ Partial order relation as approximation of data flow values◮ Meet operations as confluence of data flow values

• Cartesian product of lattices

• Example of lattices

May 2011 Uday Khedker

Page 121: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 30/57

Partially Ordered Sets and Lattices

Partially ordered sets

Partial order ⊑ isreflexive, transitive,and antisymmetric

May 2011 Uday Khedker

Page 122: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 30/57

Partially Ordered Sets and Lattices

Partially ordered sets

Partial order ⊑ isreflexive, transitive,and antisymmetric

A lower bound ofx , y is u s.t. u ⊑ x

and u ⊑ y

An upper bound ofx , y is u s.t. x ⊑ u

and y ⊑ u

May 2011 Uday Khedker

Page 123: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 30/57

Partially Ordered Sets and Lattices

Partially ordered sets

Partial order ⊑ isreflexive, transitive,and antisymmetric

Lattices

Every non-empty finitesubset has a greatestlower bound (glb) and aleast upper bound (lub)

May 2011 Uday Khedker

Page 124: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 31/57

Partially Ordered Sets

Set {1, 2, 3, 4, 9} with ⊑ relation as “divides” (i.e. a ⊑ b iff a divides b)

May 2011 Uday Khedker

Page 125: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 31/57

Partially Ordered Sets

Set {1, 2, 3, 4, 9} with ⊑ relation as “divides” (i.e. a ⊑ b iff a divides b)

4 9

2 3

1

May 2011 Uday Khedker

Page 126: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 31/57

Partially Ordered Sets

Set {1, 2, 3, 4, 9} with ⊑ relation as “divides” (i.e. a ⊑ b iff a divides b)

4 9

2 3

1

Subsets {4, 9} and {2, 3} do not have an upper bound in the set

May 2011 Uday Khedker

Page 127: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 32/57

Lattice

Set {1, 2, 3, 4, 9, 36} with ⊑ relation as “divides” (i.e. a ⊑ b iff a divides b)

36

4 9

2 3

1

May 2011 Uday Khedker

Page 128: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 33/57

Complete Lattice

• Lattice: A partially ordered set such that every non-empty finitesubset has a glb and a lub.

Example:Lattice Z of integers under ≤ relation. All finite subsets have a glband a lub. Infinite subsets do not have a glb or a lub.

May 2011 Uday Khedker

Page 129: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 33/57

Complete Lattice

• Lattice: A partially ordered set such that every non-empty finitesubset has a glb and a lub.

Example:Lattice Z of integers under ≤ relation. All finite subsets have a glband a lub. Infinite subsets do not have a glb or a lub.

• Complete Lattice: A lattice in which even ∅ and infinite subsetshave a glb and a lub.

May 2011 Uday Khedker

Page 130: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 33/57

Complete Lattice

• Lattice: A partially ordered set such that every non-empty finitesubset has a glb and a lub.

Example:Lattice Z of integers under ≤ relation. All finite subsets have a glband a lub. Infinite subsets do not have a glb or a lub.

• Complete Lattice: A lattice in which even ∅ and infinite subsetshave a glb and a lub.

Example:Lattice Z of integers under ≤ relation with ∞ and −∞.

May 2011 Uday Khedker

Page 131: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 33/57

Complete Lattice

• Lattice: A partially ordered set such that every non-empty finitesubset has a glb and a lub.

Example:Lattice Z of integers under ≤ relation. All finite subsets have a glband a lub. Infinite subsets do not have a glb or a lub.

• Complete Lattice: A lattice in which even ∅ and infinite subsetshave a glb and a lub.

Example:Lattice Z of integers under ≤ relation with ∞ and −∞.

◮ ∞ is the top element denoted ⊤: ∀i ∈ Z, i ≤ ⊤.◮ −∞ is the bottom element denoted ⊥: ∀i ∈ Z, ⊥ ≤ i .

May 2011 Uday Khedker

Page 132: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

May 2011 Uday Khedker

Page 133: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

• What about the empty set?

May 2011 Uday Khedker

Page 134: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

• What about the empty set?

◮ glb(∅) is ⊤

May 2011 Uday Khedker

Page 135: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

• What about the empty set?

◮ glb(∅) is ⊤

Every element of Z ∪ {∞,−∞} is vacuously a lower bound of anelement in ∅ (because there is no element in ∅).

May 2011 Uday Khedker

Page 136: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

• What about the empty set?

◮ glb(∅) is ⊤

Every element of Z ∪ {∞,−∞} is vacuously a lower bound of anelement in ∅ (because there is no element in ∅).The greatest among these lower bounds is ⊤.

May 2011 Uday Khedker

Page 137: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 34/57

Z ∪ {∞,−∞} is a Complete Lattice

• Infinite subsets of Z ∪ {∞,−∞} have a glb and lub.

• What about the empty set?

◮ glb(∅) is ⊤

Every element of Z ∪ {∞,−∞} is vacuously a lower bound of anelement in ∅ (because there is no element in ∅).The greatest among these lower bounds is ⊤.

◮ lub(∅) is ⊥

May 2011 Uday Khedker

Page 138: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 35/57

Finite Lattices are Complete

• Any given set of elements has a glb and a lub

Available Expressions Partially AvailableAnalysis Expressions Analysis

{e1, e2, e3}

(⊤)

{e1, e2} {e1, e3} {e2, e3}

{e1} {e2} {e3}

∅(⊥)

(⊤)

{e1} {e2} {e3}

{e1, e2} {e1, e3} {e2, e3}

{e1, e2, e3}

(⊥)

May 2011 Uday Khedker

Page 139: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 36/57

Lattice for May-Must Analysis

• There is no ⊤ among the natural values

May

MustNo

Interpreting data flow values

− No. Information does not hold along any path

− Must. Information must hold along all paths

− May. Information may hold along some path

• An artificial ⊤ can be addedHowever, a lub may not exist for arbitrary sets

May 2011 Uday Khedker

Page 140: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 37/57

Some Variants of Lattices

A poset L is

• A lattice iff each non-empty finite subset of L has a glb and lub.

• A complete lattice iff each subset of L has a glb and lub.

• A meet semilattice iff each non-empty finite subset of L has a glb.

• A join semilattice iff each non-empty finite subset of L has a lub.

May 2011 Uday Khedker

Page 141: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 38/57

Ascending and Descending Chains

• Strictly ascending chain. x ⊏ y ⊏ · · · ⊏ z

• Strictly descending chain. x ⊐ y ⊐ · · · ⊐ z

• DCC: Descending Chain ConditionAll strictly descending chains are finite.

• ACC: Ascending Chain ConditionAll strictly ascending chains are finite.

May 2011 Uday Khedker

Page 142: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 39/57

Complete Lattice and Ascending and Descending Chains

• If L satisfies acc and dcc, then◮ L has finite height, and◮ L is complete.

• A complete lattice need not have finite height (i.e. strict chains maynot be finite).Example:Lattice of integers under ≤ relation with ∞ as ⊤ and −∞ as ⊥.

May 2011 Uday Khedker

Page 143: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)36

4 9

2 3

1

May 2011 Uday Khedker

Page 144: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

36

4 9

2 3

1

May 2011 Uday Khedker

Page 145: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

◮ x ⊔ y computes the lub of x and y .z = x ⊔ y ⇒ z ⊒ x ∧ z ⊒ y

36

4 9

2 3

1

May 2011 Uday Khedker

Page 146: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

◮ x ⊔ y computes the lub of x and y .z = x ⊔ y ⇒ z ⊒ x ∧ z ⊒ y

◮ ⊓ and ⊔ are commutative, associative,and idempotent.

36

4 9

2 3

1

May 2011 Uday Khedker

Page 147: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

◮ x ⊔ y computes the lub of x and y .z = x ⊔ y ⇒ z ⊒ x ∧ z ⊒ y

◮ ⊓ and ⊔ are commutative, associative,and idempotent.

• Top (⊤) and Bottom (⊥) elements

∀x ∈ L, x ⊓ ⊤ = x

∀x ∈ L, x ⊔ ⊤ = ⊤∀x ∈ L, x ⊓ ⊥ = ⊥∀x ∈ L, x ⊔ ⊥ = x

36

4 9

2 3

1

May 2011 Uday Khedker

Page 148: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

◮ x ⊔ y computes the lub of x and y .z = x ⊔ y ⇒ z ⊒ x ∧ z ⊒ y

◮ ⊓ and ⊔ are commutative, associative,and idempotent.

• Top (⊤) and Bottom (⊥) elements

∀x ∈ L, x ⊓ ⊤ = x

∀x ∈ L, x ⊔ ⊤ = ⊤∀x ∈ L, x ⊓ ⊥ = ⊥∀x ∈ L, x ⊔ ⊥ = x

36

4 9

2 3

1

x ⊓ y = gcd ′(x , y)

Greatest common divisor (or highestcommon factor) in the lattice

May 2011 Uday Khedker

Page 149: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 40/57

Operations on Lattices

• Meet (⊓) and Join (⊔)

◮ x ⊓ y computes the glb of x and y .z = x ⊓ y ⇒ z ⊑ x ∧ z ⊑ y

◮ x ⊔ y computes the lub of x and y .z = x ⊔ y ⇒ z ⊒ x ∧ z ⊒ y

◮ ⊓ and ⊔ are commutative, associative,and idempotent.

• Top (⊤) and Bottom (⊥) elements

∀x ∈ L, x ⊓ ⊤ = x

∀x ∈ L, x ⊔ ⊤ = ⊤∀x ∈ L, x ⊓ ⊥ = ⊥∀x ∈ L, x ⊔ ⊥ = x

36

4 9

2 3

1

x ⊓ y = gcd ′(x , y)

Greatest common divisor (or highestcommon factor) in the lattice

x ⊔ y = lcm′(x , y)

Lowest common multiple in the lattice

May 2011 Uday Khedker

Page 150: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

May 2011 Uday Khedker

Page 151: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

May 2011 Uday Khedker

Page 152: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

May 2011 Uday Khedker

Page 153: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

May 2011 Uday Khedker

Page 154: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

May 2011 Uday Khedker

Page 155: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

May 2011 Uday Khedker

Page 156: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

May 2011 Uday Khedker

Page 157: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

〈LC ,⊑C ,⊓C ,⊔C 〉

May 2011 Uday Khedker

Page 158: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 41/57

Cartesian Product of Lattice

1

2 3

4

〈LN ,⊑N ,⊓N ,⊔N〉

×a

b

〈LA,⊑A,⊓A,⊔A〉

=

〈1,a〉

〈2,a〉 〈3,a〉

〈4,a〉

〈1,b〉

〈2,b〉 〈3,b〉

〈4,b〉

〈LC ,⊑C ,⊓C ,⊔C 〉

〈x1, y1〉 ⊑C 〈x2, y2〉 ⇔ x1 ⊑N x2 ∧ y1 ⊑A y2

〈x1, y1〉 ⊓C 〈x2, y2〉 = 〈x1 ⊓N x2, y1 ⊓A y2〉

〈x1, y1〉 ⊔C 〈x2, y2〉 = 〈x1 ⊔N x2, y1 ⊔A y2〉

May 2011 Uday Khedker

Page 159: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

May 2011 Uday Khedker

Page 160: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

May 2011 Uday Khedker

Page 161: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

May 2011 Uday Khedker

Page 162: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

• ⊤ may not exist. Can be added artificially.

May 2011 Uday Khedker

Page 163: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

◮ Assume that two maximal descending chains terminate attwo incomparable elements x1 and x2

• ⊤ may not exist. Can be added artificially.

May 2011 Uday Khedker

Page 164: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

◮ Assume that two maximal descending chains terminate attwo incomparable elements x1 and x2

◮ Since this is a meet semilattice, glb of {x1, x2} must exist (say z).

• ⊤ may not exist. Can be added artificially.

May 2011 Uday Khedker

Page 165: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

◮ Assume that two maximal descending chains terminate attwo incomparable elements x1 and x2

◮ Since this is a meet semilattice, glb of {x1, x2} must exist (say z).⇒ Neither of the chains is maximal.⇒ Both of them can be extended to include z .

• ⊤ may not exist. Can be added artificially.

May 2011 Uday Khedker

Page 166: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

◮ Assume that two maximal descending chains terminate attwo incomparable elements x1 and x2

◮ Since this is a meet semilattice, glb of {x1, x2} must exist (say z).⇒ Neither of the chains is maximal.⇒ Both of them can be extended to include z .

◮ Extending this argument to all strictly descending chains,it is easy to see that ⊥ must exist.

• ⊤ may not exist. Can be added artificially.

May 2011 Uday Khedker

Page 167: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 42/57

The Set of Data Flow Values

Meet semilattices satisfying the descending chain condition

• glb must exist for all non-empty finite subsets

• ⊥ must existWhat guarantees the presence of ⊥?

◮ Assume that two maximal descending chains terminate attwo incomparable elements x1 and x2

◮ Since this is a meet semilattice, glb of {x1, x2} must exist (say z).⇒ Neither of the chains is maximal.⇒ Both of them can be extended to include z .

◮ Extending this argument to all strictly descending chains,it is easy to see that ⊥ must exist.

• ⊤ may not exist. Can be added artificially.

◮ lub of arbitrary elements may not exist

May 2011 Uday Khedker

Page 168: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 43/57

The Set of Data Flow Values For Available ExpressionsAnalysis

• The powerset of the universal set of expressions

• Partial order is the subset relation

{e1, e2, e3}

{e1, e2} {e1, e3} {e2, e3}

{e1} {e2} {e3}

Set View of the Lattice

May 2011 Uday Khedker

Page 169: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 43/57

The Set of Data Flow Values For Available ExpressionsAnalysis

• The powerset of the universal set of expressions

• Partial order is the subset relation

{e1, e2, e3}

{e1, e2} {e1, e3} {e2, e3}

{e1} {e2} {e3}

Set View of the Lattice

Y

X

May 2011 Uday Khedker

Page 170: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 43/57

The Set of Data Flow Values For Available ExpressionsAnalysis

• The powerset of the universal set of expressions

• Partial order is the subset relation

{e1, e2, e3}

{e1, e2} {e1, e3} {e2, e3}

{e1} {e2} {e3}

Set View of the Lattice

Y

X

111

110 101 011

100 010 001

000

Bit Vector View

May 2011 Uday Khedker

Page 171: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 44/57

The Concept of Approximation

• x approximates y iff

x can be used in place of y without causing any problems.

• Validity of approximation is context specific

x may be approximated by y in one context and by z in another

◮ Earnings : Rs. 1050 can be safely approximated by Rs. 1000.

◮ Expenses : Rs. 1050 can be safely approximated by Rs. 1100.

May 2011 Uday Khedker

Page 172: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 45/57

Two Important Objectives in Data Flow Analysis

• The discovered data flow information should be

◮ Exhaustive. No optimization opportunity should be missed.

◮ Safe. Optimizations which do not preserve semantics should not beenabled.

May 2011 Uday Khedker

Page 173: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 45/57

Two Important Objectives in Data Flow Analysis

• The discovered data flow information should be

◮ Exhaustive. No optimization opportunity should be missed.

◮ Safe. Optimizations which do not preserve semantics should not beenabled.

• Conservative approximations of these objectives are allowed

May 2011 Uday Khedker

Page 174: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 45/57

Two Important Objectives in Data Flow Analysis

• The discovered data flow information should be

◮ Exhaustive. No optimization opportunity should be missed.

◮ Safe. Optimizations which do not preserve semantics should not beenabled.

• Conservative approximations of these objectives are allowed

• The intended use of data flow information (≡ context) determinesvalidity of approximations

May 2011 Uday Khedker

Page 175: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 46/57

Context Determines the Validity of Approximations

May prohibit correct optimization May enable wrong optimization

Analysis Application SafeApproximation

ExhaustiveApproximation

May 2011 Uday Khedker

Page 176: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 46/57

Context Determines the Validity of Approximations

May prohibit correct optimization May enable wrong optimization

Analysis Application SafeApproximation

ExhaustiveApproximation

Live variables Dead codeelimination

A dead variableis considered live

A live variable isconsidered dead

May 2011 Uday Khedker

Page 177: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 46/57

Context Determines the Validity of Approximations

May prohibit correct optimization May enable wrong optimization

Analysis Application SafeApproximation

ExhaustiveApproximation

Live variables Dead codeelimination

A dead variableis considered live

A live variable isconsidered dead

Availableexpressions

Commonsubexpressionelimination

An availableexpression isconsiderednon-available

A non-availableexpression isconsideredavailable

May 2011 Uday Khedker

Page 178: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 46/57

Context Determines the Validity of Approximations

May prohibit correct optimization May enable wrong optimization

Analysis Application SafeApproximation

ExhaustiveApproximation

Live variables Dead codeelimination

A dead variableis considered live

A live variable isconsidered dead

Availableexpressions

Commonsubexpressionelimination

An availableexpression isconsiderednon-available

A non-availableexpression isconsideredavailable

Spurious Inclusion Spurious Exclusion

May 2011 Uday Khedker

Page 179: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 47/57

Partial Order Captures Approximation

• ⊑ captures valid approximations for safety

x ⊑ y ⇒ x is weaker than y

◮ The data flow information represented by x can be safely used inplace of the data flow information represented by y

◮ It may be imprecise, though.

May 2011 Uday Khedker

Page 180: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 47/57

Partial Order Captures Approximation

• ⊑ captures valid approximations for safety

x ⊑ y ⇒ x is weaker than y

◮ The data flow information represented by x can be safely used inplace of the data flow information represented by y

◮ It may be imprecise, though.

• ⊒ captures valid approximations for exhaustiveness

x ⊒ y ⇒ x is stronger than y

◮ The data flow information represented by x contains every valuecontained in the data flow information represented by y

◮ It may be unsafe, though.

May 2011 Uday Khedker

Page 181: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 47/57

Partial Order Captures Approximation

• ⊑ captures valid approximations for safety

x ⊑ y ⇒ x is weaker than y

◮ The data flow information represented by x can be safely used inplace of the data flow information represented by y

◮ It may be imprecise, though.

• ⊒ captures valid approximations for exhaustiveness

x ⊒ y ⇒ x is stronger than y

◮ The data flow information represented by x contains every valuecontained in the data flow information represented by y

◮ It may be unsafe, though.

We want most exhaustive information which is also safe.

May 2011 Uday Khedker

Page 182: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

May 2011 Uday Khedker

Page 183: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

◮ Using ⊤ in place of any data flow value will never miss out (or ruleout) any possible value.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

May 2011 Uday Khedker

Page 184: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

◮ Using ⊤ in place of any data flow value will never miss out (or ruleout) any possible value.

◮ The consequences may be sematically unsafe, or incorrect.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

May 2011 Uday Khedker

Page 185: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

◮ Using ⊤ in place of any data flow value will never miss out (or ruleout) any possible value.

◮ The consequences may be sematically unsafe, or incorrect.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

◮ Using ⊥ in place of any data flow value will never be unsafe, orincorrect.

May 2011 Uday Khedker

Page 186: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

◮ Using ⊤ in place of any data flow value will never miss out (or ruleout) any possible value.

◮ The consequences may be sematically unsafe, or incorrect.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

◮ Using ⊥ in place of any data flow value will never be unsafe, orincorrect.

◮ The consequences may be undefined or useless because thisreplacement might miss out valid values.

May 2011 Uday Khedker

Page 187: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 48/57

Most Approximate Values in a Complete Lattice

• Top. ∀x ∈ L, x ⊑ ⊤. The most exhaustive value.

◮ Using ⊤ in place of any data flow value will never miss out (or ruleout) any possible value.

◮ The consequences may be sematically unsafe, or incorrect.

• Bottom. ∀x ∈ L, ⊥ ⊑ x . The safest value.

◮ Using ⊥ in place of any data flow value will never be unsafe, orincorrect.

◮ The consequences may be undefined or useless because thisreplacement might miss out valid values.

Appropriate orientation chosen by design.

May 2011 Uday Khedker

Page 188: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 49/57

Setting Up Lattices

Available Expressions Analysis Live Variables Analysis

{e1, e2, e3}

{e1, e2} {e1, e3} {e2, e3}

{e1} {e2} {e3}

{v1} {v2} {v3}

{v1, v2} {v1, v3} {v2, v3}

{v1, v2, v3}

⊑ is ⊆ ⊑ is ⊇

⊓ is ∩ ⊓ is ∪

May 2011 Uday Khedker

Page 189: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 50/57

Partial Order Relation

Reflexive x ⊑ x

Transitive x ⊑ y , y ⊑ z

⇒ x ⊑ z

Antisymmetric x ⊑ y , y ⊑ x

⇔ x = y

May 2011 Uday Khedker

Page 190: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 50/57

Partial Order Relation

Reflexive x ⊑ x x can be safely used in place of x

Transitive x ⊑ y , y ⊑ z

⇒ x ⊑ z

If x can be safely used in place of y

and y can be safely used in place of z ,

then x can be safely used in place of z

Antisymmetric x ⊑ y , y ⊑ x

⇔ x = y

If x can be safely used in place of y

and y can be safely used in place of x ,

then x must be same as y

May 2011 Uday Khedker

Page 191: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 51/57

Merging Information

• x ⊓ y computes the greatest lower bound of x and y i.e.largest z such that z ⊑ x and z ⊑ y

The largest safe approximation of combining data flow information x and y

May 2011 Uday Khedker

Page 192: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 51/57

Merging Information

• x ⊓ y computes the greatest lower bound of x and y i.e.largest z such that z ⊑ x and z ⊑ y

The largest safe approximation of combining data flow information x and y

• Commutative x ⊓ y = y ⊓ x

Associative x ⊓ (y ⊓ z) = (x ⊓ y) ⊓ z

Idempotent x ⊓ x = x

May 2011 Uday Khedker

Page 193: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 51/57

Merging Information

• x ⊓ y computes the greatest lower bound of x and y i.e.largest z such that z ⊑ x and z ⊑ y

The largest safe approximation of combining data flow information x and y

• Commutative x ⊓ y = y ⊓ x The order in which the data

flow information is merged,

does not matter

Associative x ⊓ (y ⊓ z) = (x ⊓ y) ⊓ z Allow n-ary merging without

any restriction on the order

Idempotent x ⊓ x = x No loss of information if x is

merged with itself

May 2011 Uday Khedker

Page 194: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 51/57

Merging Information

• x ⊓ y computes the greatest lower bound of x and y i.e.largest z such that z ⊑ x and z ⊑ y

The largest safe approximation of combining data flow information x and y

• Commutative x ⊓ y = y ⊓ x The order in which the data

flow information is merged,

does not matter

Associative x ⊓ (y ⊓ z) = (x ⊓ y) ⊓ z Allow n-ary merging without

any restriction on the order

Idempotent x ⊓ x = x No loss of information if x is

merged with itself

• ⊤ is the identity of ⊓

◮ Presence of loops ⇒ self dependence of data flow information◮ Using ⊤ as the initial value ensure exhaustiveness

May 2011 Uday Khedker

Page 195: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 52/57

More on Lattices in Data Flow Analysis

L = Lattice for all expressions L̂ = Lattice for a single expression

111

110 101 011

100 010 001

000

(Expression e is available)

1 or {e}

0 or ∅

(Expressions e is not available)

May 2011 Uday Khedker

Page 196: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 52/57

More on Lattices in Data Flow Analysis

L = Lattice for all expressions L̂ = Lattice for a single expression

111

110 101 011

100 010 001

000

(Expression e is available)

1 or {e}

0 or ∅

(Expressions e is not available)

Cartesian products if sets are used, vectors (or tuples) if bit are used.

• L = L̂× L̂× L̂ and x = 〈x̂1, x̂2, x̂3〉 ∈ L where x̂ i ∈ L̂

• ⊑= ⊑̂ × ⊑̂ × ⊑̂ and ⊓ = ⊓̂ × ⊓̂ × ⊓̂

• ⊤ = ⊤̂ × ⊤̂ × ⊤̂ and ⊥ = ⊥̂ × ⊥̂ × ⊥̂

May 2011 Uday Khedker

Page 197: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 53/57

Component Lattice for Data Flow Information RepresentedBy Bit Vectors

(⊤̂)

1

0

(⊥̂)

⊓ is ∩ or Boolean AND

(⊤̂)

0

1

(⊥̂)

⊓ is ∪ or Boolean OR

May 2011 Uday Khedker

Page 198: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 54/57

Component Lattice for Integer Constant Propagation

(⊤̂)undef or ud

−∞ . . . −1−2 0 1 2 . . . ∞

(⊥̂)

nonconst or nc

• Overall lattice L is the product of L̂ for all variables.

• ⊓ and ⊓̂ get defined by ⊑ and ⊑̂.

⊓̂ 〈a, ud〉 〈a, nc〉 〈a, c1〉

〈a, ud〉 〈a, ud〉 〈a, nc〉 〈a, c1〉〈a, nc〉 〈a, nc〉 〈a, nc〉 〈a, nc〉〈a, c2〉 〈a, c2〉 〈a, nc〉 If c1 = c2 then 〈a, c1〉 else 〈a, nc〉

May 2011 Uday Khedker

Page 199: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 55/57

Component Lattice for May Points-To Analysis

• Relation between pointer variables and locations in the memory.

May 2011 Uday Khedker

Page 200: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 55/57

Component Lattice for May Points-To Analysis

• Relation between pointer variables and locations in the memory.

• Assuming three locations l1, l2, and l3, the component lattice forpointer p is.

(⊤̂)∅

{p l1} {p l2} {p l3}

{p l1,p l2} {p l1 ,p l3} {p l2,p l3}

{p l1, p l2, p l2}

(⊥̂)

May 2011 Uday Khedker

Page 201: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 55/57

Component Lattice for May Points-To Analysis

• Relation between pointer variables and locations in the memory.

• Assuming three locations l1, l2, and l3, the component lattice forpointer p is.

(⊤̂)∅

{p l1} {p l2} {p l3}

{p l1,p l2} {p l1 ,p l3} {p l2,p l3}

{p l1, p l2, p l2}

(⊥̂)

(⊤̂)Alternatively,

{l1} {l2} {l3}

{l1, l2} {l1, l3} {l2, l3}

{l1, l2, l2}

(⊥̂)

May 2011 Uday Khedker

Page 202: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 56/57

Component Lattice for Must Points-To Analysis

• A pointer can point to at most one location.

(⊤̂)undef

p l1 p l2 p l3

none

(⊥̂)

Alternatively, (⊤̂)undef

l1 l2 l3

none

(⊥̂)

May 2011 Uday Khedker

Page 203: Uday P. Khedker - University of Cambridge · • Safety of hoisting to the exit of a block. S.1 Should be hoisted only if it can be hoisted to the entry of all succesors • Safety

MACS L111 Generalizations-1: Data Flow Values: An Overview 57/57

General Lattice for May-Must Analysis

Unknown

May

MustNo

Interpreting data flow values

− Unknown. Nothing is known as yet

− No. Information does not hold along any path

− Must. Information must hold along all paths

− May. Information may hold along some path

Possible Applications

• Pointer Analysis : No need of separate of May and Must analyseseg. (p l ,May), (p l ,Must), (p l ,No), or (p l ,Unknown).

• Type Inferencing for Dynamically Checked Languages

May 2011 Uday Khedker


Recommended