Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live...

107
Live Variables Analysis Uday Khedker (www.cse.iitb.ac.in/˜uday) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Dec 2017

Transcript of Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live...

Page 1: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

Live Variables Analysis

Uday Khedker

(www.cse.iitb.ac.in/̃ uday)

Department of Computer Science and Engineering,

Indian Institute of Technology, Bombay

Dec 2017

Page 2: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Outline 1/25

Outline

• Live Variables Analysis

• Strongly Live Variables Analysis

• Some Observations

Dec 2017 IIT Bombay

Page 3: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

Part 1

Live Variables Analysis

Page 4: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 2/25

Defining Live Variables Analysis

A variable v is live at a program point p, if somepath from p to program exit contains an r-value oc-currence of v which is not preceded by an l-valueoccurrence of v .

v=a∗b

a=v+2

End

p

Start

v=a∗b

v=a+2

End

p

Start

v = v + 2v=v+2

End

p

Start

Dec 2017 IIT Bombay

Page 5: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 2/25

Defining Live Variables Analysis

A variable v is live at a program point p, if somepath from p to program exit contains an r-value oc-currence of v which is not preceded by an l-valueoccurrence of v .

v is live at p

v=a∗b

a=v+2

End

p

Start

v=a∗b

v=a+2

End

p

Start

v = v + 2v=v+2

End

p

Start

Dec 2017 IIT Bombay

Page 6: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 2/25

Defining Live Variables Analysis

A variable v is live at a program point p, if somepath from p to program exit contains an r-value oc-currence of v which is not preceded by an l-valueoccurrence of v .

v is live at p v is not live at p

v=a∗b

a=v+2

End

p

Start

v=a∗b

v=a+2

End

p

Start

v = v + 2v=v+2

End

p

Start

Dec 2017 IIT Bombay

Page 7: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 2/25

Defining Live Variables Analysis

A variable v is live at a program point p, if somepath from p to program exit contains an r-value oc-currence of v which is not preceded by an l-valueoccurrence of v .

v is live at p v is not live at p v is live at p

v=a∗b

a=v+2

End

p

Start

v=a∗b

v=a+2

End

p

Start

v = v + 2v=v+2

End

p

Start

Dec 2017 IIT Bombay

Page 8: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 2/25

Defining Live Variables Analysis

A variable v is live at a program point p, if somepath from p to program exit contains an r-value oc-currence of v which is not preceded by an l-valueoccurrence of v .

Path basedspecification

v is live at p v is not live at p v is live at p

v=a∗b

a=v+2

End

p

Start

v=a∗b

v=a+2

End

p

Start

v = v + 2v=v+2

End

p

Start

Dec 2017 IIT Bombay

Page 9: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 3/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Dec 2017 IIT Bombay

Page 10: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 3/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Basic Blocks ≡Single statements or Maximal groupsof sequentially executed statements

Dec 2017 IIT Bombay

Page 11: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 3/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Basic Blocks ≡Single statements or Maximal groupsof sequentially executed statements

Control Transfer

Dec 2017 IIT Bombay

Page 12: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 3/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Dec 2017 IIT Bombay

Page 13: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 3/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Local Data Flow Properties

Dec 2017 IIT Bombay

Page 14: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 4/25

Local Data Flow Properties for Live Variables Analysis

Genn = { v | variable v is used in basic block n and

is not preceded by a definition of v }

Killn = { v | basic block n contains a definition of v }

Dec 2017 IIT Bombay

Page 15: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 4/25

Local Data Flow Properties for Live Variables Analysis

Genn = { v | variable v is used in basic block n and

is not preceded by a definition of v }

Killn = { v | basic block n contains a definition of v }

r-value occurrence

Value is only read, e.g. x,y,z in

x.sum = y.data + z.data

Dec 2017 IIT Bombay

Page 16: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 4/25

Local Data Flow Properties for Live Variables Analysis

Genn = { v | variable v is used in basic block n and

is not preceded by a definition of v }

Killn = { v | basic block n contains a definition of v }

r-value occurrence

Value is only read, e.g. x,y,z in

x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in

y = x.lptr

Dec 2017 IIT Bombay

Page 17: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 4/25

Local Data Flow Properties for Live Variables Analysis

Genn = { v | variable v is used in basic block n and

is not preceded by a definition of v }

Killn = { v | basic block n contains a definition of v }

r-value occurrence

Value is only read, e.g. x,y,z in

x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in

y = x.lptr

within n

Dec 2017 IIT Bombay

Page 18: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 4/25

Local Data Flow Properties for Live Variables Analysis

Genn = { v | variable v is used in basic block n and

is not preceded by a definition of v }

Killn = { v | basic block n contains a definition of v }

r-value occurrence

Value is only read, e.g. x,y,z in

x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in

y = x.lptr

within n

anywhere in n

Dec 2017 IIT Bombay

Page 19: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 5/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Dec 2017 IIT Bombay

Page 20: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 5/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Global Data Flow Properties

Dec 2017 IIT Bombay

Page 21: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 5/25

Defining Data Flow Analysis for Live Variables Analysis

INi

Geni , Kill i

OUTi

INj

Genj , Kill j

OUTj

INk = Genk ∪ (OUTk − Killk)

Genk , Killk

OUTk = INi ∪ INj

Global Data Flow PropertiesEdge basedspecifications

Dec 2017 IIT Bombay

Page 22: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 6/25

Data Flow Equations For Live Variables Analysis

INn = (OUTn − Killn) ∪ Genn

OUTn =

BI n is End block⋃

s∈succ(n)

INs otherwise

Dec 2017 IIT Bombay

Page 23: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 6/25

Data Flow Equations For Live Variables Analysis

INn = (OUTn − Killn) ∪ Genn

OUTn =

BI n is End block⋃

s∈succ(n)

INs otherwise

• INn and OUTn are sets of variables

Dec 2017 IIT Bombay

Page 24: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 6/25

Data Flow Equations For Live Variables Analysis

INn = (OUTn − Killn) ∪ Genn

OUTn =

BI n is End block⋃

s∈succ(n)

INs otherwise

• INn and OUTn are sets of variables

• BI is boundary information representing the effect of calling contexts

◮ ∅ for local variables except for the values being returned◮ set of global variables used further in any calling context

(can be safely approximated by the set of all global variables)

Dec 2017 IIT Bombay

Page 25: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 26: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 27: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 28: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 29: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 30: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 31: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 7/25

Data Flow Equations for Our Example

w = x1

while (x.data < max)2

x = x.rptr 3y = x.lptr4

z = New class of z5

y = y.lptr6

z.sum = x.data + y.data7

Cyclic Dependence

IN1 = (OUT1 − Kill1) ∪ Gen1

OUT1 = IN2

IN2 = (OUT2 − Kill2) ∪ Gen2

OUT2 = IN3 ∪ IN4

IN3 = (OUT3 − Kill3) ∪ Gen3

OUT3 = IN2

IN4 = (OUT4 − Kill4) ∪ Gen4

OUT4 = IN5

IN5 = (OUT5 − Kill5) ∪ Gen5

OUT5 = IN6

IN6 = (OUT6 − Kill6) ∪ Gen6

OUT6 = IN7

IN7 = (OUT7 − Kill7) ∪ Gen7

OUT7 = ∅

Dec 2017 IIT Bombay

Page 32: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

Dec 2017 IIT Bombay

Page 33: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

Gen and Kill need not bemutually exclusive

Dec 2017 IIT Bombay

Page 34: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

z is an r-value occurrence andnot an l-value occurrence

Dec 2017 IIT Bombay

Page 35: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

x , y , z are considered to beused based purely on local useeven if the value of z is notused later. A different analy-sis called strongly live variablesanalysis improves on this.

Dec 2017 IIT Bombay

Page 36: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data Initialization

Dec 2017 IIT Bombay

Page 37: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

Traversal

Iteration #1∅

{x , y , z}

{x , y , z}

{x , y , z}

{x , y , z}

{x , y}

{x , y}

{x}

{x}

{x}

{x}

{x}

{x}Ignoring max be-cause we are doinganalysis for pointervariables w, x, y, z

Dec 2017 IIT Bombay

Page 38: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 8/25

Performing Live Variables Analysis

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y}y = x.lptr

Gen=∅, Kill ={z}z = New class of z

Gen={y}, Kill ={y}y = y.lptr

Gen={x , y , z},Kill =∅z.sum = x.data + y.data

Traversal

{x , y , z}

{x , y , z}

{x , y , z}

{x , y , z}

{x , y}

{x , y}

{x}

{x}

{x}

{x}

{x}

{x}Ignoring max be-cause we are doinganalysis for pointervariables w, x, y, z

Iteration #2∅

{x , y , z}

{x , y , z}

{x , y , z}

{x , y , z}

{x , y}

{x , y}

{x}

{x}

{x}

{x}

{x}

{x}

{x}

Dec 2017 IIT Bombay

Page 39: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 9/25

Performing Live Variables Analysis

Local data flow properties when basic blocks contain multiple statements

Gen={x}, Kill ={w}w = x

Gen={x}, Kill =∅while (x.data < max)

Gen={x},Kill ={x}x = x.rptr

Gen={x}, Kill ={y , z}y = x.lptr

z = New class of z

y = y.lptrz.sum = x.data + y.data

Dec 2017 IIT Bombay

Page 40: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 10/25

Local Data Flow Properties for Live Variables Analysis

INn = Genn ∪ (OUTn − Killn)

• Genn : Use not preceded by definition

• Killn : Definition anywhere in a block

Dec 2017 IIT Bombay

Page 41: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 10/25

Local Data Flow Properties for Live Variables Analysis

INn = Genn ∪ (OUTn − Killn)

• Genn : Use not preceded by definition

Upwards exposed use

• Killn : Definition anywhere in a block

Stop the effect from being propagated across a block

Dec 2017 IIT Bombay

Page 42: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 11/25

Using Data Flow Information of Live Variables Analysis

• Used for register allocation

If variable x is live in a basic block b, it is a potential candidate forregister allocation

Dec 2017 IIT Bombay

Page 43: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Live Variables Analysis 11/25

Using Data Flow Information of Live Variables Analysis

• Used for register allocation

If variable x is live in a basic block b, it is a potential candidate forregister allocation

• Used for dead code elimination

If variable x is not live after an assignment x = . . ., then the assignment isredundant and can be deleted as dead code

Dec 2017 IIT Bombay

Page 44: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

Part 2

Strongly Live Variables Analysis

Page 45: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 12/25

Strongly Live Variables Analysis

• A variable is strongly live if

◮ it is used in a statement other than assignment statement, or(same as simple liveness)

◮ it is used in an assignment statement defining a variable that isstrongly live(different from simple liveness)

• Killing: An assignment statement, an input statement, or BI

(this is same as killing in simple liveness)

• Generation: A direct use or a use for defining values that are strongly live

(this is different from generation in simple liveness)

Dec 2017 IIT Bombay

Page 46: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 47: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 48: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 49: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 50: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 51: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

y = x

print (z)

Dec 2017 IIT Bombay

Page 52: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 53: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 54: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 55: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 56: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 57: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

Same

Dec 2017 IIT Bombay

Page 58: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

Same Same

Dec 2017 IIT Bombay

Page 59: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

Same Same

Dec 2017 IIT Bombay

Page 60: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

Same Same

Dec 2017 IIT Bombay

Page 61: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

{z}

Same Same

Dec 2017 IIT Bombay

Page 62: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

{z}

{z}

Same Same

Dec 2017 IIT Bombay

Page 63: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

{z}

{z}

SimpleLiveness

{z}

{z , x}

Same Same

Dec 2017 IIT Bombay

Page 64: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 13/25

Understanding Strong Liveness

y = x

print (x)

StrongLiveness

{x}

{x}

SimpleLiveness

{x}

{x}

y = x

print (y)

StrongLiveness

{y}

{x}

SimpleLiveness

{x}

{y}

y = x

print (z)

StrongLiveness

{z}

{z}

SimpleLiveness

{z}

{z , x}

Same Same Different

Dec 2017 IIT Bombay

Page 65: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

Dec 2017 IIT Bombay

Page 66: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

• We want to compute the smallestset of variables that are live

Dec 2017 IIT Bombay

Page 67: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

• We want to compute the smallestset of variables that are live

a = 1; b = 2c = 3; n = 6

B1

if a ≤ n B2

a = a + 1 B3

if a ≤ 11 B4

t1 = a + ba = t1 + cprint ”Hello”

B5

B6 print ”Hi”

T

F

T

F

Dec 2017 IIT Bombay

Page 68: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

• We want to compute the smallestset of variables that are live

• Simple liveness considers everyuse of a variable as useful

a = 1; b = 2c = 3; n = 6

B1

if a ≤ n B2

a = a + 1 B3

if a ≤ 11 B4

t1 = a + ba = t1 + cprint ”Hello”

B5

B6 print ”Hi”

T

F

T

F

{ }

}{

}{a, {a, , n}

{a, , n}, n}{a,

, n}{a,

, n}{a,

a, b, c

a, b, c

b, c

b, c

b, c

b, c

b, c

b, c

Dec 2017 IIT Bombay

Page 69: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

• We want to compute the smallestset of variables that are live

• Simple liveness considers everyuse of a variable as useful

• Strong liveness checks the livenessof the result before declaring theoperands to be live

a = 1; b = 2c = 3; n = 6

B1

if a ≤ n B2

a = a + 1 B3

if a ≤ 11 B4

t1 = a + ba = t1 + cprint ”Hello”

B5

B6 print ”Hi”

T

F

T

F

{ }

}{

}{a, {a, , n}

{a, , n}, n}{a,

, n}{a,

, n}{a,

///////a, b, c

///////a, b, c

////b, c

////b, c

////b, c

////b, c

////b, c

////b, c

Dec 2017 IIT Bombay

Page 70: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 14/25

Live Variables Analysis: Simple and Strong Liveness

• A variable is live at a programpoint if its current value is likelyto be used later

• We want to compute the smallestset of variables that are live

• Simple liveness considers everyuse of a variable as useful

• Strong liveness checks the livenessof the result before declaring theoperands to be live

• Strong liveness is more precisethan simple liveness

a = 1; b = 2c = 3; n = 6

B1

if a ≤ n B2

a = a + 1 B3

if a ≤ 11 B4

t1 = a + ba = t1 + cprint ”Hello”

B5

B6 print ”Hi”

T

F

T

F

{ }

}{

}{a, {a, , n}

{a, , n}, n}{a,

, n}{a,

, n}{a,

///////a, b, c

///////a, b, c

////b, c

////b, c

////b, c

////b, c

////b, c

////b, c

Dec 2017 IIT Bombay

Page 71: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 15/25

Data Flow Equations for Strongly Live Variables Analysis

INn = fn(OUTn)

OUTn =

BI n is End⋃

s∈succ(n)

INs otherwise

where,

fn(X ) =

(X − {y}) ∪ (Opd(e) ∩Var) n is y = e, e ∈ Expr, y ∈ X

X − {y} n is input(y)X ∪ {y} n is use(y)X otherwise

Dec 2017 IIT Bombay

Page 72: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Strongly Live Variables Analysis 15/25

Data Flow Equations for Strongly Live Variables Analysis

INn = fn(OUTn)

OUTn =

BI n is End⋃

s∈succ(n)

INs otherwise

where,

fn(X ) =

(X − {y}) ∪ (Opd(e) ∩Var) n is y = e, e ∈ Expr, y ∈ X

X − {y} n is input(y)X ∪ {y} n is use(y)X otherwise

If y is not strongly live, theassignment is skipped usingthe “otherwise” clause

Dec 2017 IIT Bombay

Page 73: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

Part 3

Some Observations

Page 74: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 16/25

What Does Data Flow Analysis Involve?

• Defining the analysis.

• Formulating the analysis.

• Performing the analysis.

Dec 2017 IIT Bombay

Page 75: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 16/25

What Does Data Flow Analysis Involve?

• Defining the analysis. Define the properties of execution paths

• Formulating the analysis.

• Performing the analysis.

Dec 2017 IIT Bombay

Page 76: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 16/25

What Does Data Flow Analysis Involve?

• Defining the analysis. Define the properties of execution paths

• Formulating the analysis. Define data flow equations

◮ Linear simultaneous equations on sets rather than numbers◮ Later we will generalize the domain of values

• Performing the analysis.

Dec 2017 IIT Bombay

Page 77: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 16/25

What Does Data Flow Analysis Involve?

• Defining the analysis. Define the properties of execution paths

• Formulating the analysis. Define data flow equations

◮ Linear simultaneous equations on sets rather than numbers◮ Later we will generalize the domain of values

• Performing the analysis. Solve data flow equations for the given programflow graph

Dec 2017 IIT Bombay

Page 78: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 16/25

What Does Data Flow Analysis Involve?

• Defining the analysis. Define the properties of execution paths

• Formulating the analysis. Define data flow equations

◮ Linear simultaneous equations on sets rather than numbers◮ Later we will generalize the domain of values

• Performing the analysis. Solve data flow equations for the given programflow graph

• Many unanswered questions

Initial value? Termination? Complexity? Properties of Solutions?

Dec 2017 IIT Bombay

Page 79: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 17/25

Iterative Solution of Linear Simultaneous Equations

• Simultaneous equations represented in the form of the product of a matrixof coefficients (A) with the vector of unknowns (x)

Ax = b

• Start with approximate values

• Compute new values repeatedly from old values

• Two classical methods

◮ Gauss-Seidel Method (Gauss: 1823, 1826), (Seidel: 1874)◮ Jacobi Method (Jacobi: 1845)

Dec 2017 IIT Bombay

Page 80: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 18/25

Our Method of Performing Data Flow Analysis

• Round robin iteration

• Essentially Jacobi method

• Unknowns are the data flow variables INi and OUTi

• Domain of values is not numbers

• Computation in a fixed order

◮ either forward (reverse post order) traversal, or◮ backward (post order) traversal

over the control flow graph

Dec 2017 IIT Bombay

Page 81: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

Dec 2017 IIT Bombay

Page 82: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

Dec 2017 IIT Bombay

Page 83: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

◮ A dead assignment may not be eliminated◮ Solution is sound but may be imprecise

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

Dec 2017 IIT Bombay

Page 84: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

◮ A dead assignment may not be eliminated◮ Solution is sound but may be imprecise

• Spurious exclusion of a live variable

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

x = z + 10

print x , y

print y

i

j

End

OUTi = {y}

Dec 2017 IIT Bombay

Page 85: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

◮ A dead assignment may not be eliminated◮ Solution is sound but may be imprecise

• Spurious exclusion of a live variable

◮ A useful assignment may be eliminated◮ Solution is unsound

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

x = z + 10

print x , y

print y

i

j

End

OUTi = {y}

Dec 2017 IIT Bombay

Page 86: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

◮ A dead assignment may not be eliminated◮ Solution is sound but may be imprecise

• Spurious exclusion of a live variable

◮ A useful assignment may be eliminated◮ Solution is unsound

• Given L2 ⊇ L1 representing liveness information

◮ Using L2 in place of L1 is sound◮ Using L1 in place of L2 may not be sound

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

x = z + 10

print x , y

print y

i

j

End

OUTi = {y}

Dec 2017 IIT Bombay

Page 87: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 19/25

Soundness and Precision of Live Variables Analysis

Consider dead code elimination based on liveness information

• Spurious inclusion of a non-live variable

◮ A dead assignment may not be eliminated◮ Solution is sound but may be imprecise

• Spurious exclusion of a live variable

◮ A useful assignment may be eliminated◮ Solution is unsound

• Given L2 ⊇ L1 representing liveness information

◮ Using L2 in place of L1 is sound◮ Using L1 in place of L2 may not be sound

• The smallest set of all live variables is most precise

◮ Since liveness sets grow (confluence is ∪), wechoose ∅ as the initial conservative value

x = y + 10

print y

print y

i

j

End

OUTi = {x , y}

x = z + 10

print x , y

print y

i

j

End

OUTi = {y}

Dec 2017 IIT Bombay

Page 88: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 20/25

Termination, Convergence, and Complexity

• For live variables analysis,

◮ The set of all variables is finite, and◮ the confluence operation (i.e. meet) is union, hence◮ the set associated with a data flow variable can only grow

⇒ Termination is guaranteed

Dec 2017 IIT Bombay

Page 89: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 20/25

Termination, Convergence, and Complexity

• For live variables analysis,

◮ The set of all variables is finite, and◮ the confluence operation (i.e. meet) is union, hence◮ the set associated with a data flow variable can only grow

⇒ Termination is guaranteed

• Since initial value is ∅, live variables analysis converges on the smallest set

Dec 2017 IIT Bombay

Page 90: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 20/25

Termination, Convergence, and Complexity

• For live variables analysis,

◮ The set of all variables is finite, and◮ the confluence operation (i.e. meet) is union, hence◮ the set associated with a data flow variable can only grow

⇒ Termination is guaranteed

• Since initial value is ∅, live variables analysis converges on the smallest set

• How many iterations do we need for reaching the convergence?

Dec 2017 IIT Bombay

Page 91: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 20/25

Termination, Convergence, and Complexity

• For live variables analysis,

◮ The set of all variables is finite, and◮ the confluence operation (i.e. meet) is union, hence◮ the set associated with a data flow variable can only grow

⇒ Termination is guaranteed

• Since initial value is ∅, live variables analysis converges on the smallest set

• How many iterations do we need for reaching the convergence?

• Going beyond live variables analysis

◮ Do the sets always grow for other data flow frameworks?◮ What is the complexity of round robin analysis for other analyses?

Dec 2017 IIT Bombay

Page 92: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

Dec 2017 IIT Bombay

Page 93: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

• abs(n) returns the absolute value of n

Dec 2017 IIT Bombay

Page 94: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

• abs(n) returns the absolute value of n

• Is y live on entry to block b2?

Dec 2017 IIT Bombay

Page 95: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

• abs(n) returns the absolute value of n

• Is y live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b3 is an infeasibleexecution path

Dec 2017 IIT Bombay

Page 96: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

• abs(n) returns the absolute value of n

• Is y live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b3 is an infeasibleexecution path

• A compiler makes conservativeassumptions:

All branch outcomes are possible

⇒ Consider every path in CFG as a po-tential execution path

Dec 2017 IIT Bombay

Page 97: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 21/25

Conservative Nature of Analysis (1)

x=abs(x)b1

if (x < 0)b2

x=a+yb3 x=a+z b4

x=a+zb5

T F

• abs(n) returns the absolute value of n

• Is y live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b3 is an infeasibleexecution path

• A compiler makes conservativeassumptions:

All branch outcomes are possible

⇒ Consider every path in CFG as a po-tential execution path

• Our analysis concludes that y is live onentry to block b2

Dec 2017 IIT Bombay

Page 98: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

Dec 2017 IIT Bombay

Page 99: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

Dec 2017 IIT Bombay

Page 100: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b4→b6 is an infeasibleexecution path

Dec 2017 IIT Bombay

Page 101: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b4→b6 is an infeasibleexecution path

• Is c live on entry to block b3?

Path b1→b3→b4→b6 is a feasibleexecution path

Dec 2017 IIT Bombay

Page 102: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b4→b6 is an infeasibleexecution path

• Is c live on entry to block b3?

Path b1→b3→b4→b6 is a feasibleexecution path

• A compiler make conservative assumptions⇒ our analysis is path insensitive

Note: It is flow sensitive (i.e. informationis computed for every control flow points)

Dec 2017 IIT Bombay

Page 103: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b4→b6 is an infeasibleexecution path

• Is c live on entry to block b3?

Path b1→b3→b4→b6 is a feasibleexecution path

• A compiler make conservative assumptions⇒ our analysis is path insensitive

Note: It is flow sensitive (i.e. informationis computed for every control flow points)

• Our analysis concludes that b is live at theentry of b2

Dec 2017 IIT Bombay

Page 104: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 22/25

Conservative Nature of Analysis (2)

if (x < 0)b1

a=a+yb2 x=a+z b3

if (x < 0)b4

x=c+1b5 x=b+1 b6

if (x < 0)b7

T F

T F

• Is b live on entry to block b2?

• By execution semantics, NO

Path b1→b2→b4→b6 is an infeasibleexecution path

• Is c live on entry to block b3?

Path b1→b3→b4→b6 is a feasibleexecution path

• A compiler make conservative assumptions⇒ our analysis is path insensitive

Note: It is flow sensitive (i.e. informationis computed for every control flow points)

• Our analysis concludes that b is live at theentry of b2

• Is c live at the entry of b3?

Dec 2017 IIT Bombay

Page 105: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 23/25

Conservative Nature of Analysis at Intraprocedural Level

• We assume that all paths are potentially executable

• Our analysis is path insensitive

◮ The data flow information at a program point p is path insensitive

◦ information at p is merged along all paths reaching p

◮ The data flow information reaching p is computed path insensitively

◦ information is merged at all shared points in paths reaching p

◦ may generate spurious information due to non-distributive flowfunctions

Dec 2017 IIT Bombay

Page 106: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 24/25

Conservative Nature of Analysis at Interprocedural Level

• Context insensitivity

◮ Merges of information across all calling contexts

• Flow insensitivity

◮ Disregards the control flow

More about it later

Dec 2017 IIT Bombay

Page 107: Live Variables Analysis - SIGSOFT...Part1 LiveVariablesAnalysis WSSE Pune Liveness Analysis: Live Variables Analysis 2/25 Defining Live Variables Analysis A variable v is live at

WSSE Pune Liveness Analysis: Some Observations 25/25

Looking Ahead

The exciting word of pointer analysis beckons us

Dec 2017 IIT Bombay