Datenflussanalyse - Klassisch
Seminar “Progammanalyse” (SS 2009)Referent: Lorenz Schauer Vortrag: 24.06.2009
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
-
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?
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
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
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
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
1while x
2do z z y
3x x
4Intraprozedurale Analyse - Flussgraph
[z:=1]
[x>0]
[z:=z*y]
[x:=x-1]
1
2
3 4
no yes
) ] 1 :
[
; ]
* :
([
] 0 [
; ] 1 :
[ z
1while x
2do z z y
3x x
4Available 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
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)
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
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
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:
''
'
[ ]
] [
; ] :
[ z x y
lwhile true
ldo skip
l[…]
[…]
[…]
l ' l
'' l
no yes
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
''
'
[ ]
] [
; ] :
[ z x y
lwhile true
ldo skip
lAvailable 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 lAEentry entry
Available Expressions Analysis – Beispiel2
Beispiel 2:
Aufstellen der kill & gen Funktionen:
---
) ] :
[
; ] 1 :
([
] [
; ]
* :
[
; ] :
[ x a b
1y a b
2while y a b
3do a a
4x a b
55 4 3 2 1
l
{}
} 1 ,
* , {
{}
{}
{}
) (
b a b a a
l killAE
} {
{}
} {
}
* {
} {
) (
b a
b a
b a
b a
l genAE
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
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 :
([
] [
; ]
* :
[
; ] :
[ x a b
1y a b
2while y a b
3do a a
4x a b
5[x:=a+b]
[y:=a*b]
[y>a+b]
[a:=a+1]
[x:=a+b]
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
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
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
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
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 :
[x 1 y 2 while x 3do y x y 4 x x 5
)}
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
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 :
[x 1 y 2 while x 3do y x y 4 x x 5
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
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 :
[x 1 y 2 while x 3do y x y 4 x x 5
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 :
[x 1 y 2 while x 3do y x y 4 x x 5
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 :
[x 1 y 2 while x 3do y x y 4 x x 5
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
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
1then x b a
2y a b
3else y b a
4x a b
5if
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
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
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
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 :
[
x y x if y x then z y else z y y x zLive 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
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
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
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
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 y y z z
) ( )
(
~ 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
Servus