p423/p523 compilers single static assignment based...

Post on 05-Jun-2018

234 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

P423/P523 CompilersSingle Static Assignment

Based on material from Static Single Assignment Book

Deyaaeldeen Almahallawi1

1dalmahal@indiana.eduIndiana University

April 23, 2015

Deyaaeldeen Almahallawi P423/P523 1/ 36

History

• Developed by Wegman, Zadeck, Alpern, and Rosen in 1988.

• First used for for efficient computation of dataflow problemssuch as global value numbering, congruence of variables,aggressive deadcode removal, and constant propagation withconditional branches

• Currently used by GCC, Suns HotSpot JVM, IBMsRVM,Chromium V8, Mono, and LLVM

Deyaaeldeen Almahallawi P423/P523 2/ 36

Introduction

Definition

A program is defined to be in SSA form if each variable is a targetof exactly one assignment statement in the program text.

Deyaaeldeen Almahallawi P423/P523 3/ 36

Introduction

Consider the following code:

x = 1 ;y = x +1;x = 2 ;z = x +1;

x1 = 1 ;y = x1 +1;x2 = 2 ;z = x2 +1;

Deyaaeldeen Almahallawi P423/P523 4/ 36

Introduction

Consider the following code:

x = 1 ;y = x +1;x = 2 ;z = x +1;

x1 = 1 ;y = x1 +1;x2 = 2 ;z = x2 +1;

Deyaaeldeen Almahallawi P423/P523 4/ 36

Introduction

What about this code?

x = i n p u t ( ) ;i f ( x == 42)theny = 1 ;e l s ey = x + 2 ;endp r i n t ( y ) ;

Deyaaeldeen Almahallawi P423/P523 5/ 36

Introduction

x = i n p u t ( ) ;i f ( x == 42)theny1 = 1 ;e l s ey2 = x +2;endy3 = φ ( y1 , y2 ) ;p r i n t ( y3 ) ;

Deyaaeldeen Almahallawi P423/P523 6/ 36

Introduction

x = 0 ;y = 0 ;

w h i l e ( x < 10){y = y + x ;x = x + 1 ;

}

p r i n t ( y )

Deyaaeldeen Almahallawi P423/P523 7/ 36

Introduction

Deyaaeldeen Almahallawi P423/P523 8/ 36

Introduction

Deyaaeldeen Almahallawi P423/P523 8/ 36

Introduction

x1 = 0 ;y1 = 0 ;

x2 = φ( x1 , x3 )y2 = φ( y1 , y3 )w h i l e ( x2 < 10){

y3 = y2 + x2 ;x3 = x2 + 1 ;

}

p r i n t ( y2 )

Deyaaeldeen Almahallawi P423/P523 9/ 36

Properties

• Since there is only a single definition for each variable in theprogram text, a variables value is independent of its positionin the program.

• Almost free use-def chains.

• Simplifies def-use chains.

Deyaaeldeen Almahallawi P423/P523 10/ 36

Properties (Def-use)

Deyaaeldeen Almahallawi P423/P523 11/ 36

Properties (Def-use)

Deyaaeldeen Almahallawi P423/P523 11/ 36

Properties

• Since there is only a single definition for each variable in theprogram text, a variables value is independent of its positionin the program.

• Almost free use-def chains.

• Simplifies def-use chains.

• No program point can be reached by two definitions of thesame variable (First phase).

Deyaaeldeen Almahallawi P423/P523 12/ 36

Properties

Single reaching-definition property

A definition D of variable v reaches a point p in the CFG if there ex-ists a path from D to p that does not pass through another definitionof v

Deyaaeldeen Almahallawi P423/P523 13/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion:

performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting

to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming:

assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Minimality property

The minimality of the number of inserted φ-functions.

• φ-function insertion: performs live-range splitting to ensuresthat any use of a given variable v is reached by exactly onedefinition of v.

• Variable renaming: assigns a unique variable name to eachlive-range.

Deyaaeldeen Almahallawi P423/P523 14/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n, i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n, i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n, i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n, i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n,

i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Minimal SSA Construction

Background

• Join sets: For a given set of nodes S in a CFG, the join setS (S) is the set of nodes in S that can be reached by two (ormore) distinct elements of S using disjoint paths.

• Dominance: d dom i if all paths from entry to node i include d

• Strict dominance: d sdom i if d dom i and d 6= i

• Dominance frontier: DF (n) is the border of the CFG regionthat is dominated by n, i.e. it contains all nodes x such that ndominates a predecessor of x but n does not strictly dominatex .

Deyaaeldeen Almahallawi P423/P523 15/ 36

Dominance Frontier

What is the border frontier of y in blocks B and C?

Deyaaeldeen Almahallawi P423/P523 16/ 36

φ-function Insertion (First Phase)

Constructing minimal SSA form requires for each variable v , theinsertion of φ-functions at S (Defs (v))

, where Defs(v) is the setof nodes that have definitions of v .

Deyaaeldeen Almahallawi P423/P523 17/ 36

φ-function Insertion (First Phase)

Constructing minimal SSA form requires for each variable v , theinsertion of φ-functions at S (Defs (v)) , where Defs(v) is the setof nodes that have definitions of v .

Deyaaeldeen Almahallawi P423/P523 17/ 36

φ-function Insertion

Deyaaeldeen Almahallawi P423/P523 18/ 36

φ-function Insertion

Notes

• Because a φ-function is itself a definition,

it may requirefurther φ-functions to be inserted.

• Dominance frontiers of distinct nodes may intersect, but oncea φ-function for a particular variable has been inserted at anode, there is no need to insert another.

Deyaaeldeen Almahallawi P423/P523 19/ 36

φ-function Insertion

Notes

• Because a φ-function is itself a definition, it may requirefurther φ-functions to be inserted.

• Dominance frontiers of distinct nodes may intersect, but oncea φ-function for a particular variable has been inserted at anode, there is no need to insert another.

Deyaaeldeen Almahallawi P423/P523 19/ 36

φ-function Insertion

Notes

• Because a φ-function is itself a definition, it may requirefurther φ-functions to be inserted.

• Dominance frontiers of distinct nodes may intersect,

but oncea φ-function for a particular variable has been inserted at anode, there is no need to insert another.

Deyaaeldeen Almahallawi P423/P523 19/ 36

φ-function Insertion

Notes

• Because a φ-function is itself a definition, it may requirefurther φ-functions to be inserted.

• Dominance frontiers of distinct nodes may intersect, but oncea φ-function for a particular variable has been inserted at anode, there is no need to insert another.

Deyaaeldeen Almahallawi P423/P523 19/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 20/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 21/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 22/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 23/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 24/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 25/ 36

Example: x

Deyaaeldeen Almahallawi P423/P523 26/ 36

Computing Dominance Frontier

Deyaaeldeen Almahallawi P423/P523 27/ 36

Computing Dominance Frontier

Deyaaeldeen Almahallawi P423/P523 28/ 36

Variable Renaming (Second Phase)

Deyaaeldeen Almahallawi P423/P523 29/ 36

Variable Renaming

Deyaaeldeen Almahallawi P423/P523 30/ 36

SSA Destruction

What’s next?

• No we have a nice code in SSA form, but

it has φ-functions allover the place that we do not know how to implement them.

• Let’s remove them!

• How? rename all φ-related variables (φ-web) to one uniquename.

Deyaaeldeen Almahallawi P423/P523 31/ 36

SSA Destruction

What’s next?

• No we have a nice code in SSA form, but it has φ-functions allover the place that we do not know how to implement them.

• Let’s remove them!

• How? rename all φ-related variables (φ-web) to one uniquename.

Deyaaeldeen Almahallawi P423/P523 31/ 36

SSA Destruction

What’s next?

• No we have a nice code in SSA form, but it has φ-functions allover the place that we do not know how to implement them.

• Let’s remove them!

• How? rename all φ-related variables (φ-web) to one uniquename.

Deyaaeldeen Almahallawi P423/P523 31/ 36

SSA Destruction

What’s next?

• No we have a nice code in SSA form, but it has φ-functions allover the place that we do not know how to implement them.

• Let’s remove them!

• How?

rename all φ-related variables (φ-web) to one uniquename.

Deyaaeldeen Almahallawi P423/P523 31/ 36

SSA Destruction

What’s next?

• No we have a nice code in SSA form, but it has φ-functions allover the place that we do not know how to implement them.

• Let’s remove them!

• How? rename all φ-related variables (φ-web) to one uniquename.

Deyaaeldeen Almahallawi P423/P523 31/ 36

SSA Destruction

Finding φ-webs

Deyaaeldeen Almahallawi P423/P523 32/ 36

Closer Look at Minimal SSA

Is it really minimal?

It can insert a φ-function to merge two values that are never usedafter the merge.

Solution

Construct pruned SSA that uses global data-flow analysis to decidewhere values are live, so it only inserts φ-function at those mergepoints where the analysis indicates that the value is potentially live.

Deyaaeldeen Almahallawi P423/P523 33/ 36

Closer Look at Minimal SSA

Is it really minimal?

It can insert a φ-function to merge two values that are never usedafter the merge.

Solution

Construct pruned SSA that uses global data-flow analysis to decidewhere values are live, so it only inserts φ-function at those mergepoints where the analysis indicates that the value is potentially live.

Deyaaeldeen Almahallawi P423/P523 33/ 36

Closer Look at Minimal SSA

Is it really minimal?

It can insert a φ-function to merge two values that are never usedafter the merge.

Solution

Construct pruned SSA that uses global data-flow analysis to decidewhere values are live, so it only inserts φ-function at those mergepoints where the analysis indicates that the value is potentially live.

Deyaaeldeen Almahallawi P423/P523 33/ 36

Closer Look at Minimal SSA

Is it really minimal?

It can insert a φ-function to merge two values that are never usedafter the merge.

Solution

Construct pruned SSA that uses global data-flow analysis to decidewhere values are live, so it only inserts φ-function at those mergepoints where the analysis indicates that the value is potentially live.

Deyaaeldeen Almahallawi P423/P523 33/ 36

Pruned SSA

Is it really the solution?

• Time-consuming, since computing the live ranges is not trivial.

• Space-consuming, it increases the space requirements for thebuild process.

Deyaaeldeen Almahallawi P423/P523 34/ 36

Pruned SSA

Is it really the solution?

• Time-consuming

, since computing the live ranges is not trivial.

• Space-consuming, it increases the space requirements for thebuild process.

Deyaaeldeen Almahallawi P423/P523 34/ 36

Pruned SSA

Is it really the solution?

• Time-consuming, since computing the live ranges is not trivial.

• Space-consuming, it increases the space requirements for thebuild process.

Deyaaeldeen Almahallawi P423/P523 34/ 36

Pruned SSA

Is it really the solution?

• Time-consuming, since computing the live ranges is not trivial.

• Space-consuming

, it increases the space requirements for thebuild process.

Deyaaeldeen Almahallawi P423/P523 34/ 36

Pruned SSA

Is it really the solution?

• Time-consuming, since computing the live ranges is not trivial.

• Space-consuming, it increases the space requirements for thebuild process.

Deyaaeldeen Almahallawi P423/P523 34/ 36

SSA Flavors Example

Deyaaeldeen Almahallawi P423/P523 35/ 36

Thank you!

Deyaaeldeen Almahallawi P423/P523 36/ 36

top related