ALF
Optimisations
Bibliographie pour aujourd'hui
Keith Cooper, Linda Torczon, Engineering a Compiler
– Chapitre 8, 9, 10
• 8.4
• 9.3
• 8.6
• 9.2
• 10.2
Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman,
Compilers: Principles, Techniques, and Tools (2nd Edition)
– Chapitre 8
• 8.5
• 8.6
Contenu
• Pliage de constants
• Variables inutilisées
– fonctions inutilisées
• Static Single Assigment
(SSA)
• Operations Identités
• Value Numbering
• Tree Height Balancing
Andrei Alexandrescu
• Roumain
• Universitatea
Politehnica din
București
• University of
Washington
• Modern C++ Design
• Architect de Langage D
Langages
Pièces de compilation
Frontent Optimiseur Backend
Source
AST
Source
d’assemblage
Backend
AST
Générateur de
code
Allocateur de
mémoire
Allocateur de
registres
Source
d’assemblage
PLIAGE DE CONSTANTS
Left Value (lvalue)
• la partie gauche d’un attribution
n = s + t;
Right Value (lvalue)
• la partie droite d’un attribution
• paramètre de fonction
• index d’un array
n = s + t;
f(t)
v[p]
Pliage de constants
• Calculez les constantes et remplacez-les par la
valeur du résultat
– Les calculs doivent être effectués de la même
manière que la CPU les ferait
• Compilez et exécuter
• Interprétation
Pliage de constants
n = 2*7
s = “hello” + ” world”
v = 5 * n + 5
n = 14
s = “hello world”
v = 75
Algorithme
• Toutes les variables sont constantes
• Prend toute les instructions de contexte de
variable
– Si une variable este lvalue la variable n’est pas
constante
– Si une adresse de variable (&v) este un paramètre
de un fonction la variable n’est pas constante
DIAGRAMME DE FLUX DE
CONTRÔLE
Diagramme de flux de contrôle
• Organisation de Three Address Code a
diagramme
• Start
• Stop
• Basic Block
– le flux d'instructions n'est pas interrompu par un saut
– il n'y a pas d'instruction d'étiquette (sauf la première
instruction)
– leader
Selection de leader
• premier instruction
• étiquette
• instruction après un saute (if, ifFalse, goto)
Exemple
var n = 0;
for (var i = 0; i < sqrt(n); i++)
{
if (n % i == 0) n = n + 1;
}
print (n);
Exemple
var n = 0;
for (var i = 0; i < sqrt(n); i++)
{
if (n % i == 0) n = n + 1;
}
print (n);
n = 0
i = 0
label for
param n
n1 = call sqrt, 1
n2 = i < n1
ifFalse e2 goto endfor
n4 = n % i
n5 = n4 == 0
ifFalse n5 goto endif
n = n + 1
label endif
i = i + 1
goto for
label endfor
param n
call print, 1
Exemple
n = 0
i = 0
label for
param n
n1 = call sqrt, 1
n2 = i < n1
ifFalse e2 goto endfor
n4 = n % i
n5 = n4 == 0
ifFalse n5 goto endif
n = n + 1
label endif
i = i + 1
goto for
label endfor
param n
call print, 1
Start
Stop
LIVENESS
Liveness
• Liveness
– Apres chaque instruction
– Une variable est considérée live si elle est utilisée dans
une autre instruction ci-dessous
– Une variable est considérée non live si la valeur
qu'elle stocke est remplacée sans être utilisée
• Usage prochaine
– Chaque instruction
– L’instruction dessous ou une variable est utilise
Tableau de symboles
• Variable
– liveness: not-live / live
– used next: instruction ou la variables est utilisé la
prochain fois
Algorythme
• Toutes les variables sont considérées comme
étant live
• À partir de la fin du bloc
– Pour une instruction s = x + y
– Définissez les informations de live et d'utilisation
suivantes de l'instruction dans le tableau des
symboles pour s, x et y
– Dans le tableau des symboles, définissez s sur "not
live" et "not used next"
– Dans la table des symboles, définissez x et y à "live" et
"used next" au instruction curent
Example
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x not live not used next
y not live not used next
a not live not used next
b not live not used next
s not live not used next
Example
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x live 3
y live 3
a not live not used next
b not live not used next
s not live not used next
Example
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x live 2
y live 2
a not live not used next
b not live not used next
s not live not used next
Example
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x not live 2
y live 2
a live 1
b live 1
s not live not used next
Example
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x not live 2
y live 2
a live 1
b live 1
s not live not used next
Example
(1) x=a+y
(2) s=x+y
(3) x=x+y+12
(1) x=a+b
(2) s=x+y
(3) x=x+y+10*2
Variable Liveness Used Next
x not live 2
y live 2
a live 1
b live 1
s not live not used next
STATIC SINGLE ASSIGNMENT
Static Single Assignment
• Three Address Code avec plus de regles
• chaque variable n'est assigne qu'une seule fois
• fonction phi
– avant chaque basic block
Example
a = a + b;
s = 2 + a;
a = a + 1;
print (a);
a1 = a0 + b0
s1 = 2 + a1
a2 = a1 + 1
param a2
call print, 1
Example
if (a > 0) {
a = a + 1;
}
else
{
a = 12;
}
print (a);
t1 = a > 0
ifFalse t1 goto else
a = a + 1
goto endif
label else
a = 12
label endif
param a
call print, 1
Example
if (a > 0) {
a = a + 1;
}
else
{
a = 12;
}
print (a);
t1 = a0 > 0
ifFalse t1 goto else
a1 = a0 + 1
goto endif
label else
a2 = 12
a3 = phi (a1, a2)
label endif
param
call print, 1
1
2
3
4
a3a?
Boucle
n = 1;
for (int p = 0; p < 120; p++)
{
n = n * a
}
print (n)
n0 = 1
p0 = 0
label for
t = p0 < 120
ifFalse goto endfor
n1 = n0 * a0
p1 = p0 + 1
goto for
label endfor
param n1
call print, 1
Boucle
n = 1;
for (int p = 0; p < 120; p++)
{
n = n * a
}
print (n)
n0 = 1
p0 = 0
p2 = phi (p0, p1)
n2 = phi (n0, n1)
label for
t = p2 < 120
ifFalse goto endfor
n1 = n2 * a0
p1 = p2 + 1
goto for
label endfor
param n2
call print, 1
1
2
3
4
Example
n = 0;
while (n < 300)
{
if (n%3 == 0) {
n = n + 1;
}
else
{
n = n + 5;
}
}
print (n)
OPERATIONS IDENTITÉS
Operations Identités
n = a * 1
n = a * 0
n = a + 0
n = a and a
n = a or a
n = a - a
n = a / a
n = a / 1
n = a2
n = a * 2pow
n = a / 2pow
n = a
n = 0
n = a
n = a
n = a
n = 0
n = 1 (if a != 0)
n = a
n = a * a
n = a << pow
n = a >> pow
VALUE NUMBERING
Value Numbering
• Optimisation locale (dans le bloc)
• Prend les calcules redondantes
• Prend les les instructions inutiles
Value Numbering
s = x + y
t = x + y
x = u - 2
s = x + 2
t = x + y + 2
s0 = x0 + y0
t0 = x0 + y0
x1 = u0 - 2
s1 = x1 + 2
t1 = x1 + y0 + 2
Value Numbering
s = x + y
t = x + y
x = u + 2
s = x + 2
t = x + y + 2
s02 = x00 + y01
t02 = x00 + y01
x15 = u03 + 24
s19 = x15 + 24
t110 = x15 + y01 + 24
Value Numbering
s = x + y
t = x + y
x = u + 2
s = x + 2
t = x + y + 2
s02 = x00 + y01
t02 = s02
x15 = u03 + 24
s19 = x15 + 24
t110 = s19 + y01
Value Numbering
s = x + y
t = x + y
x = u + 2
s = x + 2
t = x + y + 2
s0 = x0 + y
t0 = s0
x = u + 2
s = x + 2
t = s + y
Instructions inutilisées
s = x + y
t = x + y
x = u + 2
s = x + 2
t = x + y + 2
s0 = x0 + y
t0 = s0
x = u + 2
s = x + 2
t = s + y
TREE HEIGHT BALANCING
Tree Height Balancing
s = 2+3+4+7+9+3; +
+
+
+
+
2
3
4
7
9
3
Tree Height Balancing
s = 2+3+4+7+9+3;
plusieurs de unités de
processeur
out of order execution
+
+
+
2
3
+
4
7
+
9
3
Questions

ALF 12 - Optimisations

  • 1.
  • 2.
    Bibliographie pour aujourd'hui KeithCooper, Linda Torczon, Engineering a Compiler – Chapitre 8, 9, 10 • 8.4 • 9.3 • 8.6 • 9.2 • 10.2 Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools (2nd Edition) – Chapitre 8 • 8.5 • 8.6
  • 3.
    Contenu • Pliage deconstants • Variables inutilisées – fonctions inutilisées • Static Single Assigment (SSA) • Operations Identités • Value Numbering • Tree Height Balancing
  • 4.
    Andrei Alexandrescu • Roumain •Universitatea Politehnica din București • University of Washington • Modern C++ Design • Architect de Langage D
  • 5.
  • 6.
    Pièces de compilation FrontentOptimiseur Backend Source AST Source d’assemblage
  • 7.
  • 8.
  • 9.
    Left Value (lvalue) •la partie gauche d’un attribution n = s + t;
  • 10.
    Right Value (lvalue) •la partie droite d’un attribution • paramètre de fonction • index d’un array n = s + t; f(t) v[p]
  • 11.
    Pliage de constants •Calculez les constantes et remplacez-les par la valeur du résultat – Les calculs doivent être effectués de la même manière que la CPU les ferait • Compilez et exécuter • Interprétation
  • 12.
    Pliage de constants n= 2*7 s = “hello” + ” world” v = 5 * n + 5 n = 14 s = “hello world” v = 75
  • 13.
    Algorithme • Toutes lesvariables sont constantes • Prend toute les instructions de contexte de variable – Si une variable este lvalue la variable n’est pas constante – Si une adresse de variable (&v) este un paramètre de un fonction la variable n’est pas constante
  • 14.
    DIAGRAMME DE FLUXDE CONTRÔLE
  • 15.
    Diagramme de fluxde contrôle • Organisation de Three Address Code a diagramme • Start • Stop • Basic Block – le flux d'instructions n'est pas interrompu par un saut – il n'y a pas d'instruction d'étiquette (sauf la première instruction) – leader
  • 16.
    Selection de leader •premier instruction • étiquette • instruction après un saute (if, ifFalse, goto)
  • 17.
    Exemple var n =0; for (var i = 0; i < sqrt(n); i++) { if (n % i == 0) n = n + 1; } print (n);
  • 18.
    Exemple var n =0; for (var i = 0; i < sqrt(n); i++) { if (n % i == 0) n = n + 1; } print (n); n = 0 i = 0 label for param n n1 = call sqrt, 1 n2 = i < n1 ifFalse e2 goto endfor n4 = n % i n5 = n4 == 0 ifFalse n5 goto endif n = n + 1 label endif i = i + 1 goto for label endfor param n call print, 1
  • 19.
    Exemple n = 0 i= 0 label for param n n1 = call sqrt, 1 n2 = i < n1 ifFalse e2 goto endfor n4 = n % i n5 = n4 == 0 ifFalse n5 goto endif n = n + 1 label endif i = i + 1 goto for label endfor param n call print, 1 Start Stop
  • 20.
  • 21.
    Liveness • Liveness – Apreschaque instruction – Une variable est considérée live si elle est utilisée dans une autre instruction ci-dessous – Une variable est considérée non live si la valeur qu'elle stocke est remplacée sans être utilisée • Usage prochaine – Chaque instruction – L’instruction dessous ou une variable est utilise
  • 22.
    Tableau de symboles •Variable – liveness: not-live / live – used next: instruction ou la variables est utilisé la prochain fois
  • 23.
    Algorythme • Toutes lesvariables sont considérées comme étant live • À partir de la fin du bloc – Pour une instruction s = x + y – Définissez les informations de live et d'utilisation suivantes de l'instruction dans le tableau des symboles pour s, x et y – Dans le tableau des symboles, définissez s sur "not live" et "not used next" – Dans la table des symboles, définissez x et y à "live" et "used next" au instruction curent
  • 24.
    Example (1) x=a+b (2) s=x+y (3)x=x+y+10*2 Variable Liveness Used Next x not live not used next y not live not used next a not live not used next b not live not used next s not live not used next
  • 25.
    Example (1) x=a+b (2) s=x+y (3)x=x+y+10*2 Variable Liveness Used Next x live 3 y live 3 a not live not used next b not live not used next s not live not used next
  • 26.
    Example (1) x=a+b (2) s=x+y (3)x=x+y+10*2 Variable Liveness Used Next x live 2 y live 2 a not live not used next b not live not used next s not live not used next
  • 27.
    Example (1) x=a+b (2) s=x+y (3)x=x+y+10*2 Variable Liveness Used Next x not live 2 y live 2 a live 1 b live 1 s not live not used next
  • 28.
    Example (1) x=a+b (2) s=x+y (3)x=x+y+10*2 Variable Liveness Used Next x not live 2 y live 2 a live 1 b live 1 s not live not used next
  • 29.
    Example (1) x=a+y (2) s=x+y (3)x=x+y+12 (1) x=a+b (2) s=x+y (3) x=x+y+10*2 Variable Liveness Used Next x not live 2 y live 2 a live 1 b live 1 s not live not used next
  • 30.
  • 31.
    Static Single Assignment •Three Address Code avec plus de regles • chaque variable n'est assigne qu'une seule fois • fonction phi – avant chaque basic block
  • 32.
    Example a = a+ b; s = 2 + a; a = a + 1; print (a); a1 = a0 + b0 s1 = 2 + a1 a2 = a1 + 1 param a2 call print, 1
  • 33.
    Example if (a >0) { a = a + 1; } else { a = 12; } print (a); t1 = a > 0 ifFalse t1 goto else a = a + 1 goto endif label else a = 12 label endif param a call print, 1
  • 34.
    Example if (a >0) { a = a + 1; } else { a = 12; } print (a); t1 = a0 > 0 ifFalse t1 goto else a1 = a0 + 1 goto endif label else a2 = 12 a3 = phi (a1, a2) label endif param call print, 1 1 2 3 4 a3a?
  • 35.
    Boucle n = 1; for(int p = 0; p < 120; p++) { n = n * a } print (n) n0 = 1 p0 = 0 label for t = p0 < 120 ifFalse goto endfor n1 = n0 * a0 p1 = p0 + 1 goto for label endfor param n1 call print, 1
  • 36.
    Boucle n = 1; for(int p = 0; p < 120; p++) { n = n * a } print (n) n0 = 1 p0 = 0 p2 = phi (p0, p1) n2 = phi (n0, n1) label for t = p2 < 120 ifFalse goto endfor n1 = n2 * a0 p1 = p2 + 1 goto for label endfor param n2 call print, 1 1 2 3 4
  • 37.
    Example n = 0; while(n < 300) { if (n%3 == 0) { n = n + 1; } else { n = n + 5; } } print (n)
  • 38.
  • 39.
    Operations Identités n =a * 1 n = a * 0 n = a + 0 n = a and a n = a or a n = a - a n = a / a n = a / 1 n = a2 n = a * 2pow n = a / 2pow n = a n = 0 n = a n = a n = a n = 0 n = 1 (if a != 0) n = a n = a * a n = a << pow n = a >> pow
  • 40.
  • 41.
    Value Numbering • Optimisationlocale (dans le bloc) • Prend les calcules redondantes • Prend les les instructions inutiles
  • 42.
    Value Numbering s =x + y t = x + y x = u - 2 s = x + 2 t = x + y + 2 s0 = x0 + y0 t0 = x0 + y0 x1 = u0 - 2 s1 = x1 + 2 t1 = x1 + y0 + 2
  • 43.
    Value Numbering s =x + y t = x + y x = u + 2 s = x + 2 t = x + y + 2 s02 = x00 + y01 t02 = x00 + y01 x15 = u03 + 24 s19 = x15 + 24 t110 = x15 + y01 + 24
  • 44.
    Value Numbering s =x + y t = x + y x = u + 2 s = x + 2 t = x + y + 2 s02 = x00 + y01 t02 = s02 x15 = u03 + 24 s19 = x15 + 24 t110 = s19 + y01
  • 45.
    Value Numbering s =x + y t = x + y x = u + 2 s = x + 2 t = x + y + 2 s0 = x0 + y t0 = s0 x = u + 2 s = x + 2 t = s + y
  • 46.
    Instructions inutilisées s =x + y t = x + y x = u + 2 s = x + 2 t = x + y + 2 s0 = x0 + y t0 = s0 x = u + 2 s = x + 2 t = s + y
  • 47.
  • 48.
    Tree Height Balancing s= 2+3+4+7+9+3; + + + + + 2 3 4 7 9 3
  • 49.
    Tree Height Balancing s= 2+3+4+7+9+3; plusieurs de unités de processeur out of order execution + + + 2 3 + 4 7 + 9 3
  • 50.