• Keine Ergebnisse gefunden

Datenflussanalyse - Klassisch

N/A
N/A
Protected

Academic year: 2022

Aktie "Datenflussanalyse - Klassisch"

Copied!
40
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Datenflussanalyse - Klassisch

Seminar “Progammanalyse” (SS 2009)

Referent: Lorenz Schauer Vortrag: 24.06.2009

(2)

Agenda

Agenda

- Einleitung

- Einführung in die Sprache WHILE - Die Intraprozedurale Analyse

- Available Expressions Analysis - Reaching Definitions Analysis - Very Busy Expressions Analysis - Live Variables Analysis

-

(3)

Einleitung

Was bedeutet “Datenflussanalyse”?

Ermittlung der Zusammenhänge zwischen einzelnen Stellen im Programm und den darin berechneten Werten.

Typische Fragen sind:

o Was genau wird in dem Programmabschnitt berechnet?

o Wie und wo wird eine bestimmte Variable berechnet?

o Welche Werte kann sie annehmen?

o Welche Befehle beinflussen welche Variable zu welchem Zeitpunkt?

(4)

Einleitung

Datenflussanalyse – wie und wozu?

Wozu?

- Primär zur Codeoptimierung

- Gegebenenfalls zur Lösung von entstandenen Problemen

Wie?

- Programmcode analysieren und verstehen (Hilfsmittel: Flussgraph)

- Datenflussprobleme erkennen und Gleichung aufstellen - Code optimieren bzw. Probleme lösen

(5)

Einführung in WHILE

Die Sprache: WHILE - Imperative Sprache

- Programmcode glieder sich in Blöcke

- Jeder Block besitzt ein eindeutiges Etikett (Label)

- Bezeichnen Zuweisungen und boolsche Bedingungen - Haben intern keinen Datenfluss

Folgende Elemente werden definiert:

o Zuweisung [x := a]

o Skip [skip]

o Bedingung if [b] then S1 else S2

l l l

(6)

Intraprozedurale Analyse

Intraprozedurale Analyse

- Beschäftigt sich mit Elementaraussagen

- Gliedert sich in mehrere Datenflussanalysen (spätere Folien) - Unterscheidet Vorwärts- und Rückwärtsanalysen

Flussfunktionen (Um Flussgraph aufzustellen) Fluss-Funktion: flow: Stmt P(Lab x Lab)

Rückflussfunktion: flow : Stmt P(Lab x Lab)

Beschreiben den Fluss zwischen den Lables der Aussagen R

(7)

Intraprozedurale Analyse - Flussfunktion

1

Wie sieht der Flussgraph von folgendem Code aus ?

Die Flussfunktion liefert folgende Menge an Werten:

{(1,2),(2,3),(3,4),(4,2)}

Damit können wir den nachfolgenden Graphen zeichnen…

) ] 1 :

[

; ]

* :

([

] 0 [

; ] 1 :

[ z

1

while x

2

do zz y

3

xx

4

(8)

Intraprozedurale Analyse - Flussgraph

[z:=1]

[x>0]

[z:=z*y]

[x:=x-1]

1

2

3 4

no yes

) ] 1 :

[

; ]

* :

([

] 0 [

; ] 1 :

[ z

1

while x

2

do zz y

3

xx

4

(9)

Available Expressions Analysis

Available Expressions Analysis

o Analyse Algorithmus (Vorwärtsanalyse)

o Beschreibt die Verfügbarkeit einzelner Ausdrücke an bestimmten Programmstellen

o Frage: Welche Ausdrücke / Werte sind an einer bestimmten

Programmstelle gültig?

o Ziel: Codeoptimierung

) ] :

[

; ] 1 :

([

] [

; ]

* : [

; ] :

[x ab 1 y a b 2 while y ab 3do a a 4 x ab 5

(10)

Available Expressions Analysis

Definition von kill- und gen-Funktionen

Kill-Funktion: Ausdruck ist killed, wenn eine der Variablen verändert wird (Innerhalb des Blocks).

killAE : Blocks P(AExp)

Gen-Funktion: Ausdruck wird im Programmbereich generiert und

keine Variable davon innerhalb des Blocks verändert.

genAE : Blocks P(AExp)

(11)

Available Expressions Analysis

kill- und gen-Funktionen bei AEA

)}

' (

| '

{ )

] :

([ x a a AExp

*

x FV a

kill

AE

l

  

{}

) ]

([

l

AE

skip kill

{}

) ]

([

l

AE

b

kill

) ( )

] ([

{}

) ] ([

)}

' (

| ) ( '

{ )

] :

([

b AExp b

gen

skip gen

a FV x

a AExp a

a x

gen

l AE

l AE

l AE

(12)

Available Expressions Analysis

Entry- & Exit-Funktionen

Durch diese beiden Funktionen wird die Analyse bestimmt:

) (

:

, AE Lab

*

P AExp

*

AE

entry exit

)}

( )

, ' (

| ) ' ( {

) (

{}

) (

S

*

flow l

l l

AE l

AE

l AE

exit entry

entry

) ( S

*

init l

Falls:

) (

) (

)) (

\ ) ( (

) (

S

*

blocks B

B gen

B kill

l AE

l AE

l

l AE

l AE

entry exit

(13)

Available Expressions Analysis – Beispiel1

Beispiel 1:

o Nur Block 1 generiert den Ausdruck: {x+y}

o Andere generieren / killen keine Ausdrücke o Der Flussgraph sieht wie folgt aus:

''

'

[ ]

] [

; ] :

[ zxy

l

while true

l

do skip

l

[…]

[…]

[…]

l ' l

'' l

no yes

(14)

Available Expressions Analysis – Beispiel1

Gesucht wird: größte Lösung der Gleichung für AEentry

Bestimmen der AE-Gleichungen:

) ' ' ( )

' ' (

) ' ( )

' (

} {

) ( )

(

) ' ( )

' ' (

) ' ' ( )

( )

' (

{}

) (

l AE

l AE

l AE

l AE

y x

l AE

l AE

l AE

l AE

l A

l AE

l AE

l AE

entry exit

entry exit

entry exit

exit entry

exit exit

entry entry

''

'

[ ]

] [

; ] :

[ zxy

l

while true

l

do skip

l

(15)

Available Expressions Analysis – Beispiel1

Wir erhalten:

Deutung:

o Es gibt 2 Lösungen für die Gleichung:

- {x+y}

- die leere Menge

o Informativ aber nur {x+y}

o {x+y} stellt die größte Lösung der Gleichung dar.

=> Der Ausdruck {x+y} ist jedesmal verfügbar, wenn

) ' ( }

{ ) '

(

l x y AE l

AEentry    entry

(16)

Available Expressions Analysis – Beispiel2

Beispiel 2:

Aufstellen der kill & gen Funktionen:

---

) ] :

[

; ] 1 :

([

] [

; ]

* :

[

; ] :

[ xab

1

ya b

2

while yab

3

do aa

4

xab

5

5 4 3 2 1

l

{}

} 1 ,

* , {

{}

{}

{}

) (

b a b a a

l killAE

} {

{}

} {

}

* {

} {

) (

b a

b a

b a

b a

l genAE

(17)

Available Expressions Analysis – Beispiel2

Aufstellen der AE-Funktionen:

) 4 ( )

5 (

) 3 ( )

4 (

) 5 ( )

2 ( )

3 (

) 1 ( )

2 (

{}

) 1 (

exit entry

exit entry

exit exit

entry

exit entry

entry

AE AE

AE AE

AE AE

AE

AE AE

AE

} {

) 5 ( )

5 (

} 1 ,

* , {

\ ) 4 ( )

4 (

} {

) 3 ( )

3 (

}

* { ) 2 ( )

2 (

} {

) 1 ( )

1 (

b a AE

AE

a b a b a AE

AE

b a AE

AE

b a AE

AE

b a AE

AE

entry exit

entry exit

entry exit

entry exit

entry exit

(18)

Available Expressions Analysis – Beispiel2

Als Ergebnis erhält man folgende Tabelle:

---

5 4 3 2 1

l

{}

} {

} {

} {

{}

) (

b a

b a

b a

l AE

entry

} {

{}

} {

}

* , {

} {

) (

b a

b a

b a b a

b a

l AE

exit

) ] :

[

; ] 1 :

([

] [

; ]

* :

[

; ] :

[ xab

1

ya b

2

while yab

3

do aa

4

xab

5

[x:=a+b]

[y:=a*b]

[y>a+b]

[a:=a+1]

[x:=a+b]

(19)

Available Expressions Analysis – Beispiel2

Deutung:

o Der Ausdruck a wird innerhalb der Schleife neu definiert o Der Ausdruck {a+b} ist immer am Schleifeneingang verfügbar

o {a*b} nur beim ersten Schleifeneingang verfügbar wird vor dem nächsten Durchlauf gekilled

(20)

Reaching Definition Analysis

Reaching Definition Analysis

o sehr ähnlich zur AE-Analyse (Vorwärtsanalyse) o Frage: Welche Definition/Zuweisung ist bei einer

bestimmten Stelle im Programm gültig?

o Ziele: Kenntnis über die jeweilige Gültigkeit einer Zuweisung o Beispiel:

Nach Label 2: (x,1),(y,2)

Nach Label 3: (x,1),(y,2),(a,4),(x,5)

) ] :

[

; ] 1 :

([

] [

; ]

* : [

; ] :

[x ab 1 y a b 2 while y ab 3do a a 4 x ab 5

(21)

Reaching Definition Analysis - Vorgehensweise

Vorgehensweise

o Definieren der killRD, genRD, RDentry und RDexit funktionen:

{}

) ] ([

{}

) ] ([

)}

, {(

) ] : ([

{}

) ] ([

{}

) ] ([

)}

( _ _

| ) ' , {(

?)}

, {(

) ] :

([

' *

l RD

l RD

l RD

l RD

l RD

l l

RD

b gen

skip gen

l x a

x gen

b kill

skip kill

S x to asgm B

l x x

a x

kill

(22)

Reaching Definition Analysis - Vorgehensweise

) (

) ( ))

(

\ ) ( (

) (

)}

( )

,' (

| ) ' ( {

) ( )}

(

|

?) , {(

) (

*

*

*

*

S blocks B

wo

B gen

B kill

l RD

l RD

S flow l

l l

RD

S init l

if S

FV x

x l

RD

l

l RD l

RD entry

exit

entry entry

(23)

Reaching Definition Analysis - Beispiel

o Funktionen am Programmteil anwenden:

Beispiel:

Es entsteht folgende Tabelle der kill & gen Funktionen:

--- 1

2 3 4 5

) ] 1 :

[

; ]

* :

([

] 1 [

; ] 1 : [

; ] 5 :

[x1 y2 while x3do yx y 4 xx5

)}

5 , ( ), 1 , (

?), , {(

)}

4 , ( ), 2 , (

?), , {(

{}

)}

4 , ( ), 2 , (

?), , {(

)}

5 , ( ), 1 , (

?), , {(

) (

x x

x

y y

y

y y

y

x x

x

l killRD

)}

5 , {(

)}

4 , {(

{}

)}

2 , {(

)}

1 , {(

) (

x y y x

l genRD

(24)

Reaching Definition Analysis - Beispiel

Damit ergibt sich für die RD Funktionen (1):

_______________________________________________________

1 2 3 4

5 (4) ) 3 (

) 5 ( )

2 (

) 1 (

?)}

, (

?), , {(

) (

exit exit

exit exit

exit entry

RD RD

RD RD

RD

y x

l RD

)}

5 , {(

)}) 5 , ( ), 1 , (

?), , {(

\ ) 5 ( (

)}

4 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 4 ( (

) 3 (

)}

2 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 2 ( (

)}

1 , {(

)}) 5 , ( ), 1 , (

?), , {(

\ ) 1 ( (

) (

x x

x x

RD

y y

y y

RD RD

y y

y y

RD

x x

x x

RD l RD

entry entry entry

entry entry exit

) ] 1 :

[

; ]

* :

([

] 1 [

; ] 1 : [

; ] 5 :

[x1 y2 while x3do yx y 4 xx5

(25)

Reaching Definition Analysis - Beispiel

Damit ergibt sich für die RD Funktionen (2):

_______________________________________________________

1 2 3 4

5( (4)\{( ,?),( ,2),( ,4)}) {( ,4)}

) 3 (

)}

5 , {(

)}) 5 , ( ), 1, (

?), , {(

\ ) 5 ( (

)}

2 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 2 ( (

)}

1, {(

)}) 5 , ( ), 1, (

?), , {(

\ ) 1 ( (

?)}

, (

?), , {(

) (

y y

y y

RD RD

x x

x x RD

y y

y y

RD

x x

x x RD

y x

l RD

entry entry

entry entry entry entry

)}

5 , {(

)}) 5 , ( ), 1, (

?), , {(

\ ) 4 ( (

)}

4 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 3 ( (

) 5 ( )

2 (

)}

2 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 1 ( (

)}

1, {(

)}) 5 , ( ), 1, (

?), , {(

\

?)}

, (

?), , ({(

) (

x x

x x

RD

y y

y y

RD

RD RD

y y

y y

RD

x x

x x

y x

l RD

exit exit

exit exit

exit exit

(26)

Reaching Definition Analysis - Beispiel

Damit ergibt sich für die RD Funktionen (3):

_______________________________________________________

1 2 3

4

5 ( (3)\{( ,?),( ,2),( ,4)}) {( ,4)}

) 3 (

)}

5 , {(

)}

4 , {(

)}) 4 , ( ), 2 , (

?), , ( ), 5 , ( ), 1, (

?), , {(

\ ) 4 ( (

)}

2 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ )}

1, (

?), , ({(

)}

1, {(

)}) 5 , ( ), 1, (

?), , {(

\

?)}

, (

?), , ({(

?)}

, (

?), , {(

) (

y y

y y

RD RD

x y

y y

y x

x x

RD

y y

y y

x y

x x

x x

y x

y x

l RD

entry entry

entry entry

)}

5 , {(

)}) 5 , ( ), 1, (

?), , {(

\ ) 4 ( (

)}

4 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ ) 5 ( )}

2 , ( ), 1, ({(

) 5 ( )}

2 , ( ), 1, {(

)}

2 , {(

)}) 4 , ( ), 2 , (

?), , {(

\ )}

1, (

?), , ({(

)}

1, (

?), , {(

) (

x x

x x

RD y

y y

y RD

y x

RD y

x

y y

y y

x y

x y

l RD

exit

exit exit exit

) ] 1 :

[

; ]

* :

([

] 1 [

; ] 1 : [

; ] 5 :

[x1 y2 while x3do yx y 4 xx5

(27)

Reaching Definition Analysis - Beispiel

Damit ergibt sich für die RD Funktionen (4):

_______________________________________________________

1 2 3 4

5 ( (3)\{( ,?),( ,2),( ,4)}) {( ,4)}

)}

5 , ( ), 4 , ( ), 2 , ( ), 1, {(

)}

5 , ( ), 4 , ( ), 2 , ( ), 1, {(

)}

1, (

?), , {(

?)}

, (

?), , {(

) (

y y

y y

RD

x y

y x

x y

y x

x y

y x

l RD

entry entry

 ( (4)\{( ,?),( 1,),( ,5)}) {( ,5)}

)}

5 , ( ), 4 , ( ), 1, {(

) 5 ( )}

2 , ( ), 1, {(

)}

2 , ( ), 1, {(

)}

1, (

?), , {(

) (

x x

x x

RD

x y

x

RD y

x

y x

x y

l RD

exit

exit exit

) ] 1 :

[

; ]

* :

([

] 1 [

; ] 1 : [

; ] 5 :

[x1 y2 while x3do yx y 4 xx5

(28)

Reaching Definition Analysis - Beispiel

Damit ergibt sich für die RD Funktionen (5):

_______________________________________________________

1 2 3 4 5

Somit sind alle RD-Funktionen aufgelöst.

Deutung: Man erkennt nun, welche Zuweisung an welcher Stelle existiert!

)}

5 , ( ), 4 , ( ), 1, {(

)}

5 , ( ), 4 , ( ), 2 , ( ), 1, {(

)}

5 , ( ), 4 , ( ), 2 , ( ), 1, {(

)}

1, (

?), , {(

?)}

, (

?), , {(

) (

x y

x

x y

y x

x y

y x

x y

y x

l RDentry

)}

5 , ( ), 4 , {(

)}

5 , ( ), 4 , ( ), 1, {(

)}

5 , ( ), 4 , ( ), 2 , ( ), 1, {(

)}

2 , ( ), 1, {(

)}

1, (

?), , {(

) (

x y

x y

x

x y

y x

y x

x y

l RDexit

) ] 1 :

[

; ]

* :

([

] 1 [

; ] 1 : [

; ] 5 :

[x1 y2 while x3do yx y 4 xx5

(29)

Very Busy Expressions Analysis

Very Busy Expressions Analysis

o Ein Ausdruck wird als „very busy“ bezeichnet, wenn:

- am Ende eines Labels sichergestellt ist, dass er im weiteren Verlauf auf jeden Fall gebraucht wird.

=> Wichtig: Der Ausdruck muss verwendet bzw.

betrachtet werden

bevor einer seiner Werte verändert wird!

o Es handelt sich um eine Rückwärtsanalyse

(30)

Very Busy Expressions Analysis

Veranschaulichung und Analyse am Beispiel:

o Die Ausrücke {a-b} und {b-a} sind „very busy“ zum Zeitpunkt 1

o Um das Programm zu analysieren müssen wieder alle Funktionen

aufgestellt werden!

) ] :

[

; ] :

([

) ] :

[

; ] :

([

]

[ a b

1

then x b a

2

y a b

3

else y b a

4

x a b

5

if         

(31)

Very Busy Expressions Analysis

o Hier gleich angewendet (allgemeine Formeln nachlesen!) o kill & gen Funktionen:

o VB Funktionen:

} {

{}

5

} {

{}

4

} {

{}

3

} {

{}

2

{}

{}

1

) ( )

(

b a

a b

b a

a b

l gen

l kill

l VB VB

{}

} {

3

) 3 ( }

{ ) 2 ( 2

) 4 ( )

2 ( )

1 ( 1

) ( )

(

b a

VB a

b VB

VB VB

VB

l VB l

VB l

entry exit

entry entry

exit

exit entry

(32)

Very Busy Expressions Analysis

Nach Vereinfachung erhält man:

o Deutung: Aus dieser Tabelle lässt sich nun jeder Ausdruck, der

zu einem bestimmten Zeitpunkt „very busy“ ist, ablesen.

{}

} {

5

} {

} ,

{ 4

{}

} {

3

} {

} ,

{ 2

} ,

{ } ,

{ 1

) ( )

(

b a

b a a

b b a

b a

b a a

b b a

a b b a a

b b a

l VB l

VB

l entry exit

(33)

Live Variables Analysis

Live Variables Analysis

o Eine Variable wird als „live“ bezeichnet, wenn:

- sie am Ende eines Labels einen Wert enthält, der später benötigt werden könnte.

o Rückwärtsanalyse o Verwendung:

- Dead code elimination

(Variable nicht live – Zuweisungsblock löschen) - Registerzuweisung

(34)

Live Variables Analysis

Beispiel:

Frage: Welche variablen sind zu welchem Zeitpunkt „live“?

Antwort: x nach Label 1 ist nicht live x nach Label 3 live

y nach Label 2 live

=> Erste Zuweisung ist Redundant, kann also gelöscht werden.

7 6

5 4

3 2

1

; [ : 4 ] ; [ : 1 ] ; ( [ ] [ : ] [ : * ] ); [ : ]

] 2 :

[

xyxif yx then zy else zy y xz

(35)

Live Variables Analysis

Definitionen der kill, gen und LV Funktionen:

)}

( )

,' (

| ) ' ( {

) ( ) {}

(

) ( )

] ([

{}

) ] ([

) ( )

] : ([

{}

) ] ([

{}

) ] ([

} { ) ] : ([

*

*

S flow l

l l

LV

S final l

falls l

LV

a FV b

gen

skip gen

a FV a

x gen

b kill

skip kill

x a

x kill

R entry

exit

l LV

l LV

l LV

l LV

l LV

l LV

 

(36)

Live Variables Analysis

Analyse nach Aufstellen der gen, kill und LV Funktionen ergibt folgendes:

} { }

{ 7

} { }

{ 6

} { }

{ 5

} , { 4

} { 3

} { 2

} { 1

) ( )

(

z x

y z

y z

y x x

y x

l gen

l kill

l

LV LV

 }

{ 7

) 7 ( }

{ }) {

\ ) 6 ( (

6

) 7 ( }

{ }) {

\ ) 5 ( (

5

) 6 ( )

5 ( }

, { ) 4 ( 4

) 4 ( }

{

\ ) 3 ( 3

) 3 ( }

{

\ ) 2 ( 2

) 2 ( }

{

\ ) 1 ( 1

) ( )

(

z

LV y

z LV

LV y

z LV

LV LV

y x LV

LV x

LV

LV y

LV

LV x

LV

l LV l

LV l

entry exit

entry exit

entry entry

exit

entry exit

entry exit

entry exit

exit entry

(37)

Live Variables Analysis

Durch einsetzten und vereinfachen erhält man wieder die Ergebnistabelle:

Deutung: - Alle „live“-Variablen bzgl ihrem Programmpunkt ablesbar

{}

} { 7

} { }

{ 6

} { }

{ 5

} { }

, { 4

} , { }

{ 3

} { {}

2

{}

{}

1

) ( )

(

z

z y

z y

y y

x

y x y

y l LV

l LV

l entry exit

(38)

Live Variables Analysis - Richtigkeit

Die Richtigkeit der Live Variables Analysis

o Um Richtigkeit zu zeigen, benutzen wir folgendes Theorem:

1.

2.

' )) 2 ( ( '

1 '

2 2

' 2

2 )) ( ( 1

' 1 1

' )) 2 ( ( '

1 '

2 ' 2

' 2

2 )) ( ( 1

' 1 ' 1

~ ,

:

~ ,

~ ,

, :

~ ,

,

'

S init X

S init N

S init N

S init N

und S

dann und

S falls

und und

S S

dann und

S S

falls

(39)

Live Variables Analysis - Richtigkeit

Veranschaulichung am Beispiel:

, V1 = {y,z} und V2 = {x}

o o o

o Nach Theorem und

folgt, dass wenn V2 nach dem Block live ist,

[

x

:

y z

]

l

, 

1

) ( )

( )

( )

(

~ 1 2 1 2 1 2

1 V   yyzz

    

) ( )

(

~ 2 2 1 2

1 V

 

x

x

 

' 2 2

' 1

1

[ : ] ,

, ] :

[

 

 

  

 

x y z l x y z l

' 2 2 '

1 2

1

1 ~   ~ 

V  V

(40)

Servus

Vielen Dank für die Aufmerksamkeit!

Referenzen

ÄHNLICHE DOKUMENTE

Following the discussions, we have assembled in Fig- ure 1 a conceptual model with scenarios integrating smart products with different combinations of what func- tionality is

On the one hand, police and military use of lethal force is morally justified in part by recourse to fundamental human moral rights and obligations, especially the right to

Under the title “Drone Penalty,” David Wills analyzes the pow- erful link between the history of slave-trade, the perseverance of the death penalty in the US, and the

Er küsst mich wie der, als wäre mei ne Be mer kung nicht der Rede wert, als wür de sich sei ne Hose nicht aus beu len, als wäre die Schwel lung hin ter dem Reiß ver schluss nicht

• Formal unification of strong and electroweak interactions Formal unification of strong and electroweak interactions. Wh ere is the Da rk m

Auch nach der Schießerei bei Cattle Corner steht Harmonica wie ein Wunder von den Toten auf, denn er hat immer noch etwas zu erledigen (Walker 2001, Seite 239), genauso wie

We determine the finite simple groups of Lie type of charac- teristic p, for which the Steinberg character lies in the principal `-block for every prime ` 6= p dividing the order of

17 Schüler (3,9 Prozent; davon eine weibliche Per- son) bekundeten, in der Schule selbst schon einmal von einer Waffe Gebrauch gemacht zu haben; 13 Schüler (drei Prozent; davon