Академический Документы
Профессиональный Документы
Культура Документы
MOTIVATION :
To produ
e target programs with high exe
ution
e
ien
y.
Constraints :
(a) Maintain semanti
equivalen
e with sour
e program.
(b) Improve a program without
hanging the algorithm.
Need :
(a) `Permissive' programming languages provide many
ex-
ibilities, often leading to ine
ient
oding. For example,
a := a + 1; where a is `real' requires a type
onversion of
`1' to `1.0'. An optimising
ompiler
an avoid type
on-
version during the exe
ution of the program by using
the
onstant `1.0' instead of `1'.
(b) Due to the in
reasing
ost of programmer time, pro-
grammers do not pay su
ient attention to exe
ution
e
ien
y of programs. Hen
e the need for optimisation
during
ompilation.
Code Optimisation : 1
CODE OPTIMISATION
EFFECTIVENESS :
Contemporary s
enario
Improvements due to optimistion may be more dra-
mati
in
ontemporary
ompilers be
ause
More ee
tive optimisation te
hniques are available.
Today, less emphasis is put on exe
ution e
ien
y than on
other attributes of a program like stru
ture, maintainabil-
ity, reusability of a program, et
. Code sharing and re-use
leads to a `bla
k box' view of programs, whi
h further de-
emphasises exe
ution e
ien
y.
Exploitation of advan
ed ar
hite
tural features like instru
-
tion pipelining requires `smart'
ode generation, whi
h is
only possible in an optimising
ompiler.
Code Optimisation : 2
CODE OPTIMISATION
Q : Can a programmer out-perform an optimiser ?
Also, NO ! Be
ause
(i) It takes too mu
h time to perform some optimisations
by hand, viz. strength redu
tion,
opy propagation,
dead
ode elimination.
(ii) Certain ma
hine level details are beyond the
ontrol
of a programmer, viz. instru
tions and addressing
modes supported by the target ma
hine.
Code Optimisation : 3
CODE OPTIMISATION
LEVELS OF OPTIMISATION
Code Optimisation : 4
CODE OPTIMISATION
MACHINE INDEPENDENT OPTIMISATION
Code Optimisation : 5
CODE OPTIMISATION
Why separate Lo
al and Global optimisation ?
a b
ab ( assumed eliminated
by lo
al optimisation
Code Optimisation : 6
OPTIMISING TRANSFORMATIONS
1. COMPILE-TIME EVALUATION
(a) Folding
Evaluation of an expression with
onstant operands
at
ompilation time. In ee
t, an expression is repla
ed by a
single value (hen
e the term `folding').
Example :
area = (22:0=7:0)*r**2
22.0/7.0
an be performed during
ompilation itself.
Note :
Typi
al appli
ations of folding are in address
al
ula-
tion for array referen
es, where produ
ts of many
onstants are
`folded' into single
onstant values.
Code Optimisation : 7
OPTIMISING TRANSFORMATIONS
1. COMPILE-TIME EVALUATION (Contd.)
Code Optimisation : 8
OPTIMISING TRANSFORMATIONS
1(b). CONSTANT PROPAGATION (Contd.)
a := 5:2 1
R
2 5
?? R ?
3 x := 5 6 x := 5 7
? R
a := b 4 8
?
x + 17 9
R
a +7 10
Code Optimisation : 9
OPTIMISING TRANSFORMATIONS
2. COMMON SUB-EXPRESSION ELIMINATION (CSE)
An expression need not be evaluated if an equivalent
value is available and
an be used.
temp := b
;
a := b
; a := temp;
)
x := b
+ 5; x := temp + 5;
Code Optimisation : 10
OPTIMISING TRANSFORMATIONS
2. COMMON SUB-EXPRESSION ELIMINATION (Contd.)
a b 1
R
:= a 2 x +y 5
?? R
3 x := z 6 7
? R
b 4 x +y 8
?
9
R
a b 10
Code Optimisation : 11
OPTIMISING TRANSFORMATIONS
3. VARIABLE PROPAGATION
Code Optimisation : 12
OPTIMISING TRANSFORMATIONS
3. VARIABLE PROPAGATION (Contd.)
a :=
1
R
2 x := z 5
?? R ?
3 6 7
? R
:= 10 4 x +y 8
?
9
R
a b 10
Code Optimisation : 13
OPTIMISING TRANSFORMATIONS
4. CODE MOVEMENT OPTIMISATION
temp := x " 2;
if a<b then if a<b then
z := x " 2; z := temp;
)
else else
y := x " 2 + 19; y := temp + 19;
Code Optimisation : 14
OPTIMISING TRANSFORMATIONS
4. CODE MOVEMENT OPTIMISATION (Contd.)
Code Optimisation : 15
OPTIMISING TRANSFORMATIONS
4. CODE MOVEMENT OPTIMISATION (Contd.)
Code Optimisation : 16
OPTIMISING TRANSFORMATIONS
4. CODE MOVEMENT OPTIMISATION (Contd.)
temp := x y;
for i := 1 to 10; for i := 1 to 10;
z := x y; ) z := temp;
end ; end ;
Code Optimisation : 17
OPTIMISING TRANSFORMATIONS
4(b). Loop optimisation (Contd.)
1
R
2 5
?? R ?
3 6 x +y 7
? R
a b 4 8
?
9
R
10
Code Optimisation : 18
OPTIMISING TRANSFORMATIONS
5. STRENGTH REDUCTION OPTIMISATION
Repla
ement of a high strength operator by a (possibly
repeated) appli
ation of a low strength operator.
for i := 1 to 50;
a[i := :::; f Ea
h element = 4 bytes g
end ;
Code Optimisation : 19
OPTIMISING TRANSFORMATIONS
5. STRENGTH REDUCTION OPTIMISATION (Contd.)
Strength redu
tion is typi
ally applied to integer ex-
pressions involving an indu
tion variable and a high strength op-
erator.
Note :
Strength redu
tion is not performed for
oating point
expressions be
ause a strength redu
ed program may produ
e
dierent results than the original program.
Q : Why ?
A : Consider nite pre
ision of
omputer arithmeti
.
Code Optimisation : 20
OPTIMISING TRANSFORMATIONS
6. LOOP TEST REPLACEMENT
Repla
e a loop termination test phrased in terms of
one variable, by a test phrased in terms of another variable.
This may open up the possibilities of dead
ode elimination.
Typi
ally useful following strength redu
tion.
temp := 5; temp := 5;
i := 1; i := 1;
loop : x := temp; loop : x := temp;
i := i + 1; ) i := i + 1;
temp := temp + 5; temp := temp + 5;
if i 10 then if temp 50 then
goto loop; goto loop;
Code Optimisation : 21
OPTIMISING TRANSFORMATIONS
7. DEAD CODE ELIMINATION
Preliminaries
1. A variable is said to be dead at a pla
e in a program if the
value
ontained in the variable at that pla
e is not used
anywhere in the program.
2. If an assignment is made to a variable v at a pla
e where
v is dead, then the assignment is a dead assignment.
Code Optimisation : 22
OPTIMISING TRANSFORMATIONS
7. DEAD CODE ELIMINATION (Contd.)
a :=
1
R
2 x := y 5 5
?? R ?
3 6 7
? R
a b 4 8
?
9
R
10
Code Optimisation : 23
LOCAL OPTIMISATION
Basi
Blo
k
A basi
blo
k b of a program P is a sequen
e of
program statements (instru
tions) = (i1; i2; : : : im) su
h that
(i) only i1 in b
an be the destination of a transfer of
ontrol instru
tion,
(ii) only im in b
an be a transfer instru
tion itself.
Example :
a := x y;
z := x;
b := z y ;
Code Optimisation : 24
LOCAL OPTIMISATION
1. DAG BASED OPTIMISATION
Example :
stmt no. statement
1. a := x y;
2. z := x;
3. b := z y ;
4. x := b;
5. g := x y ;
Code Optimisation : 25
LOCAL OPTIMISATION
1. DAG BASED OPTIMISATION (Contd.)
Example :
stmt no. statement
1. a := x y;
2. z := x;
3. b := z y ;
4. x := b;
5. g := x y ;
#
a
"!
* HH
HH
HH
# H HHH
HH # #
"! x0 y0
"! z0
"!
Note : and z0 represent the initial values of variables
x 0 ; y0 x; y
and z respe
tively.
Code Optimisation : 26
LOCAL OPTIMISATION
1. DAG BASED OPTIMISATION (Contd.)
Example :
stmt no. statement
1. a := x y;
2. z := x;
3. b := z y ;
4. x := b;
5. g := x y ;
#
a; b
"!
* HH
HH
HHH
# HHH
HH #
"! x0 ; z y0
"!
Note : Variable propagation has o
urred for variable z , while
ommon sub-expression elimination has been ee
ted for a b.
Code Optimisation : 27
LOCAL OPTIMISATION
1. DAG BASED OPTIMISATION (Contd.)
Example :
stmt no. statement
1. a := x y;
2. z := x;
3. b := z y ;
4. x := b;
5. g := x y ;
#
g
"!
*
AA
# A
AA
AA
a; b; x
"!
* HH
HH
HHH
A
AA
# HHH A
HH A #
"! z y0
"!
Code Optimisation : 28
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS
Code Optimisation : 29
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS
Notes :
1. The result name in a quadruple is not ne
essarily a `tem-
porary lo
ation'. It simply provides a
onvenient means to
refer to the partial result represented by the quadruple.
2. The
ompiler
an maintain a hash table of the rst three
elds of quadruples to ensure uniqueness of the result name.
3. The simpli
ation resulting from unique result names is as
follows : In the above example, if the se
ond o
urren
e of
a b is redundant, it
an simply be repla
ed by the result
name in the quadruple, viz. temp1. This
an be done by
simply examining the result name of the quadruple, and
without having to know the other o
urren
es of a b.
4. The result name be
omes a temporary lo
ation only when
some o
urren
es of the expression
an be eliminated.
Code Optimisation : 30
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS (Contd.)
Code Optimisation : 31
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS (Contd.)
Code Optimisation : 32
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS (Contd.)
Example :
stmt no. statement
5. a := 29:3 d;
17. b := 24:5;
31.
:= a b + w;
49. x := a b + y ;
Code Optimisation : 33
LOCAL OPTIMISATION
2. QUADRUPLE BASED OPTIMISATION USING VALUE
NUMBERS (Contd.)
Example :
stmt no. statement
5. a := 29:3 d;
17. b := 24:5;
31.
:= a b + w;
49. x := a b + y ;
Code Optimisation : 34
GLOBAL OPTIMISATION
S
ope
The s
ope of global optimisation is generally a
program unit, viz. a pro
edure or fun
tion body.
Program Representation
The program is represented in the form of a Control
Flow Graph (CFG). The nodes of the graph represent the ba-
si
blo
ks in the program, and the edges represent the
ow of
ontrol during the exe
ution of the program.
Code Optimisation : 35
GLOBAL OPTIMISATION
CONTROL FLOW ANALYSIS
Con
epts and Denitions
Program Point
A program point wj is the instant between the end of
exe
ution of instru
tion ij , and the beginning of the exe
ution
of the instru
tion ij +1. The ee
t of exe
ution of instru
tion ij
is said to be
ompletely realised at program point wj .
Paths
A sequen
e of edges (e1 , e2; : : : el ), su
h that the
terminal node of ei is the initial node of ei+1, is known as a
path in G.
Code Optimisation : 36
GLOBAL OPTIMISATION
CONTROL FLOW ANALYSIS
Con
epts and Denitions (Contd.)
Dominators
A blo
k bi is said to be a dominator of blo
k bj if every
path from n0 to bj in G passes through bi.
bi is a post-dominator of bj if every path starting on bj
passes through bi before rea
hing an exit node of G.
Regions
A region R = (V; E ; V ) is a
onne
ted subgraph of G,
0 0
Code Optimisation : 37
GLOBAL OPTIMISATION
CONTROL FLOW ANALYSIS
Con
epts and Denitions (Contd.)
Code Optimisation : 38
DATA FLOW ANALYSIS
Determines useful information for the purpose of
optimisation.
Code Optimisation : 39
DATA FLOW ANALYSIS
PRELIMINARIES
1 w1 a: b
??
2
?
3 w5 x: := y
?
4
Code Optimisation : 40
DATA FLOW ANALYSIS
FUNDAMENTAL DATA FLOW PROPERTIES
1. Available Expressions
Expression e is available at program point w, i along
all paths rea
hing w
(i) there exists an evaluation point for e,
(ii) no denition of any operand of e follows its last
evaluation along the path.
Code Optimisation : 41
DATA FLOW ANALYSIS
1. Available Expressions (Contd.)
a b 1
R
2
+d 5
?? R ?
+d 3 6 7
? R
a := 4 8
?
9
R
10
Code Optimisation : 42
DATA FLOW ANALYSIS
FUNDAMENTAL DATA FLOW PROPERTIES (Contd.)
Code Optimisation : 43
DATA FLOW ANALYSIS
2. Rea
hing Denitions (Contd.)
a := 7 1
R
2 x := y 5
?? R ?
3 6 7
? R
a := b 4 x := 3 8
?
9
R
10
Code Optimisation : 44
DATA FLOW ANALYSIS
FUNDAMENTAL DATA FLOW PROPERTIES (Contd.)
3. Live Variables
A variable v is live at a program point wi i
(i) v is referen
ed along some path wi : : : wj starting on pro-
gram point wi, and
(ii) no assignment to v o
urs before its referen
e along
the path.
Usage :
Code Optimisation : 45
DATA FLOW ANALYSIS
3. Live Variables (Contd.)
a := g 1
R
b := 5 2 5
?? R ?
3 a +w 6 7
? R
4 x := 3 8
?
x y 9
R
b :=
10
Code Optimisation : 46
DATA FLOW ANALYSIS
FUNDAMENTAL DATA FLOW PROPERTIES (Contd.)
4. Busy Expressions
An expression e is busy at a program point wi i
(i) an evaluation of e exists along some path wi : : : wj starting
on program point wi, and
(ii) no denition of any operand of e exists before its eval-
uation along the path
Code Optimisation : 47
DATA FLOW ANALYSIS
4. Busy Expressions (Contd.)
1
R
2 5
?? R ?
x +y 3 6 a b 7
? R
4 8
?
9
R
10
Code Optimisation : 48
DATA FLOW ANALYSIS
REPRESENTING DATA FLOW INFORMATION
Data
ow information
an be represented by a set of
properties , or by a bit ve
tor with ea
h bit representing a property.
The former is more general, while the latter is more
onvenient
in pra
ti
e. Unless otherwise stated, these representations
an
be used inter
hangeably in our dis
ussions.
Example : Available expressions
(a) Set Representation
f a b,
+ d, x y, g
a b
+d
? ? ? ?
1 0 1 1 0
Code Optimisation : 49
DATA FLOW ANALYSIS
OBTAINING DATA FLOW INFORMATION
Data
ow information of a node has the following
om-
ponents :
(a) Data
ow information generated in a node, viz. an
expression be
omes available following its
omputation
in the node.
(b) Data
ow information killed in a node, viz. a denition
of variable v kills all expressions involving v.
(
) Data
ow information obtained from neighbouring nodes,
viz. a denition rea
hing the exit of a prede
essor also
rea
hes the entry of a node.
Code Optimisation : 50
DATA FLOW ANALYSIS
LOCAL DATA FLOW INFORMATION
1
2 R
+ d x y 5
?? 3 6 R ? 7
a + b a +b
? R
a := 4b x
8 := 3
?
+ 9
a b
R 10
a b
Code Optimisation : 51
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
1. Consider all paths rea
hing node i (and starting on the pro-
gram entry node n0).
2. Compute the set of available expressions along ea
h path
from n0 to a prede
essor of node i. (Assume that the set of
available expressions at the entry of n0 is .)
3. Sin
e available expressions is an all paths problem, take the
interse
tion of available expressions along ea
h path. This
gives the set of available expressions at the entry of node i.
Code Optimisation : 52
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
? R
a := 4b x
8 := 3
?
+ 9
a b
R 10
a b
Code Optimisation : 53
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
a b
+d
a := : : : 4
Code Optimisation : 54
DATA FLOW ANALYSIS
FORWARD DATA FLOW PROBLEMS
Code Optimisation : 55
DATA FLOW ANALYSIS
RECOGNIZING FORWARD/BACKWARD DATA FLOW
PROBLEMS
(a) Forward Data
ow problems
We
an re
ognise a data
ow problem to be forward
when the data
ow information for a program point wi depends
on the
omputations pla
ed along one or more paths rea
hing wi.
In this
ase, the information must
ow along the dire
tion of
ow of
ontrol in the program.
Code Optimisation : 56
DATA FLOW ANALYSIS
Live Variables : A ba
kward data
ow problem
A variable v is live at a program point wi i v is ref-
eren
ed along some path wi : : : wj starting on wi, and : : :
(a) Generated information : A variable v be
omes live when
it is referen
ed (i.e. used) in an expression. Sin
e the
data
ow is ba
kwards, the liveness due to a use in an
expression extends ba
kwards within the basi
blo
k, till
a denition of v. Hen
e a live variable is generated in
a basi
blo
k due to an upwards exposed referen
e of a
variable v, i.e. a use of v not pre
eded by a denition
within the basi
blo
k.
Example :
a := : : :
::: := a b 5
::: := v 3
Here Gen5 = f v, b g. Note that the referen
e of a is not
upwards exposed.
(b) Killed information : A denition of a variable (i.e. an
assignment to it) kills its liveness.
(
) Merging of information : Merging is done using the set union
operation [ or the bitwise `or' operation P sin
e this is
an any path problem.
Code Optimisation : 57
DATA FLOW ANALYSIS
SUMMARY OF DATA FLOW PROBLEMS
Code Optimisation : 58
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
Code Optimisation : 59
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
Code Optimisation : 60
SETTING UP DATA FLOW EQUATIONS
AVINi
R ?
AVGENi = fa + b; ::: g
:= +
a
HY
b
AVKILLi = f
d;
g
e; : : :
?RHHHH
HH
HHH AVOUT
i
Code Optimisation : 61
DATA FLOW ANALYSIS
COMPUTING GLOBAL DATA FLOW INFORMATION
R ? AVINi = \8p AVOUTp
w : x := ; exp
Code Optimisation : 62
GENERAL FORM OF DATA FLOW EQUATIONS
1. Forward Data Flow Problems
INi = 8pOUTp
OUTi = INi KILLi [ GENi
where,
is the
on
uen
e operator [ or \,
IN/OUT indi
ate information at entry/exit,
KILL indi
ates information killed in node,
GEN indi
ates information generated in node.
OUTi = INs
8s
INi = OUTi KILLi [ GENi
where,
is the
on
uen
e operator [ or \,
IN/OUT indi
ate information at entry/exit,
KILL indi
ates information killed in node,
GEN indi
ates information generated in node.
Code Optimisation : 63
COMMON DATA FLOW PROBLEMS
1. FORWARD DATA FLOW PROBLEMS
Code Optimisation : 64
COMMON DATA FLOW PROBLEMS
2. BACKWARD DATA FLOW PROBLEMS
Code Optimisation : 65
DATA FLOW ANALYSIS
SETTING UP DATA FLOW EQUATIONS
We will
onsider the pro
ess of setting up data
ow
equations to
olle
t the information required for an optimisa-
tion. Consider the following spe
i
ation of an optimisation.
Copy Propagation
We
an repla
e a by b in the following statement if a
is a
opy of b at program point w (i.e. if a has the same value as
b at w).
w : z := a + y;
Approa
h :
1. De
ide on what information is adequate to perform the
desired substitution. (Note : You may dene your own
on
epts and notations for this purpose.)
2. Analyse the nature of the information to de
ide how it
may be
olle
ted.
3. Design a data
ow problem to
olle
t the information.
Develop data
ow equations for the same.
Code Optimisation : 66
SETTING UP DATA FLOW EQUATIONS
Example (Contd.) :
COPIESw = f g
w : z := a + y;
Code Optimisation : 67
SETTING UP DATA FLOW EQUATIONS
Example (Contd.) :
COPIESw = f g
w : z := a + y;
Code Optimisation : 68
SETTING UP DATA FLOW EQUATIONS
Example (Contd.) :
Questions :
Code Optimisation : 69
SETTING UP DATA FLOW EQUATIONS
Example (Contd.) :
w : x := y ;
Code Optimisation : 70
DATA FLOW ANALYSIS
A solution of the data
ow equations
onsists of an
assignment of values to the IN and OUT sets of ea
h node in
the
ontrol
ow graph.
To be useful for optimisation, the information
on-
tained in a solution must satisfy the following
onditions :
(a) Self-
onsisten
y : The values assigned to the dierent
nodes must be mutually
onsistent (else the solution is
in
orre
t !). Su
h a
onsistent solution is known as a
xed point of the data
ow equations.
(b) Conservative information : The data
ow information
must be
onservative in that optimisation using this
information should not
hange the meaning of a program
under any
ir
umstan
es.
(
) Meaningfulness : The
omputed values of the properties
should provide meaningful (in fa
t, maximum) oppor-
tunities for optimisation. This is important, sin
e not
performing any optimisation is
onservative but hardly
meaningful in an optimising
ompiler !
Code Optimisation : 71
DATA FLOW ANALYSIS
CONSERVATIVE SOLUTION OF DATA FLOW EQUATIONS
Code Optimisation : 72
DATA FLOW ANALYSIS
CONSERVATIVE ESTIMATES OF DF PROPERTIES
Code Optimisation : 73
DATA FLOW ANALYSIS
CONSERVATIVE ESTIMATES OF DF PROPERTIES
Code Optimisation : 74
DATA FLOW ANALYSIS
ALIASING
Example :
p := &x;
Question :
Set up a data
ow problem to
olle
t the set of vari-
ables whose values
an
hange as a result of the pointer based
assignment
p := : : : ;
Code Optimisation : 75
SOLVING DATA FLOW EQUATIONS
ITERATIVE DATA FLOW ANALYSIS
Note :
Boundary
onditions hold for the program entry / exit
nodes (for forward and ba
kward data
ow problems, respe
-
tively). Unless interpro
edural analysis is performed, no data
ow information
an be assumed to be available at the bound-
aries.
Code Optimisation : 76
SOLVING DATA FLOW EQUATIONS
ITERATIVE DATA FLOW ANALYSIS
The solution of iterative data
ow analysis is not unique.
Example :
1 e1
??
2
?
3
?
4
Code Optimisation : 77
SOLVING DATA FLOW EQUATIONS
(ROUND ROBIN) ITERATIVE DATA FLOW ANALYSIS
AVAILABLE EXPRESSIONS
/* Initialisations */
AVINn0 := fg; AVOUTn0 := AVGENn0 ;
8 i 2 N fn0g AVOUTi := U AVKILLi[ AVGENi;
/* U is the universal set of expressions */
/* Iteration */
hange := true;
while
hange do begin
hange := false;
8 i 2 N n0 do begin
AVINi := \8p AVOUTp;
oldouti := AVOUTi
AVOUTi := (AVINi AVKILLi) [ AVGENi;
if AVOUTi 6= oldout then
hange := true;
end;
end;
Code Optimisation : 78
SOLVING DATA FLOW EQUATIONS
MEANINGFUL SOLUTION OF DATA FLOW EQUATIONS
For the problem of available expressions, initialising
IN and OUT properties of all nodes to fg leads to a trivial solu-
tion of the data
ow equations. We must avoid trivial solutions
and try to obtain the most meaningful solution, i.e. the largest
possible solution to the equations (also
alled the maximum xed
point (MFP)).
Code Optimisation : 79
DATA FLOW ANALYSIS
CONVERGENCE OF ITERATIVE DATA FLOW ANALYSIS
R? AVINi = \8p AVOUTp
w : x := ; exp
AVOUTi =AVGENi [ (AVINi AVKILLi)
Code Optimisation : 80
SOLVING DATA FLOW EQUATIONS
COMPLEXITY OF ITERATIVE DATA FLOW ANALYSIS
Code Optimisation : 81
SOLVING DATA FLOW EQUATIONS
COMPLEXITY OF ITERATIVE DATA FLOW ANALYSIS
Example :
1 a b
2
??
3
??
a := :::
depth = 1
4
? nesting depth = 2
5
?
6
?
Code Optimisation : 82
SOLVING DATA FLOW EQUATIONS
COMPLEXITY OF ITERATIVE DATA FLOW ANALYSIS
Code Optimisation : 83
SOLVING DATA FLOW EQUATIONS
COMPLEXITY OF ITERATIVE DATA FLOW ANALYSIS
2
??
3
??
depth = 2
4
?
5
?
a := :::
6
?
Code Optimisation : 84
SOLVING DATA FLOW EQUATIONS
WORKLIST ITERATIVE DF ANALYSIS (adapted: Mu
hni
k)
AVAILABLE EXPRESSIONS
/* Initialisations */
AVINn0 := fg; AVOUTn0 := AVGENn0 ;
8 i 2 N fn0g AVINi := AVOUTi := U;
/* U is the universal set of expressions */
Worklist := N fn0g;
/* Iteration */
while worklist not empty do begin
Remove rst node from worklist, let it be ni
AVINi := \8p AVOUTp;
oldout := AVOUTi
AVOUTi := (AVINi AVKILLi) [ AVGENi;
if AVOUTi 6= oldout then
Add all su
essors of ni to worklist;
end;
end;
Code Optimisation : 85
REGISTER ASSIGNMENT & ALLOCATION
A note on terminology
Register assignment and allo
ation are two distin
t
phases in the work aimed at making ee
tive utilisation of
registers of the target ma
hine (by redu
ing the number of
Load/Store instru
tions). The distin
tion between the two
terms has not always been maintained in the literature.
We will, however, make a distin
tion between the two
terms and use them with the meanings des
ribed in the following
two transparen
ies.
Code Optimisation : 86
REGISTER ASSIGNMENT & ALLOCATION
Register assignment
Managing the use of hypotheti
al registers.
{ A hypotheti
al register is assumed to be available for ev-
ery data item / expression. During register assignment,
we de
ide where to pla
e Load / Store instru
tions so
that the value of the data item / expression is available
in a register at every usage point, and is available in the
memory
ell allo
ated for that data item / expression at
all other points.
{ An unbounded number of hepotheti
al registers is as-
sumed to be available for the purpose of assignment.
Code Optimisation : 87
REGISTER ASSIGNMENT & ALLOCATION
Register allo
ation
Managing the use of real registers in a target ma
hine.
{ The use of hypotheti
al registers is mapped into the use
of real registers existing in the target ma
hine.
{ The motivation is to hold frequently used data values in
registers instead of memory lo
ations in the interests of
exe
ution e
ien
y of the target program.
S
ope :
(i) Lo
al Register Allo
ation : Allo
ation of registers to data
items / expressions within a basi
blo
k.
(ii) Global Register Allo
ation : Allo
ation of registers to
data items / expressions over regions of a program.
Code Optimisation : 88
LOCAL REGISTER ALLOCATION
... allo
ation of registers to date items / expressions
within a basi
blo
k (i.e. in straight line
ode).
Code Optimisation : 89
LOCAL REGISTER ALLOCATION
Register Referen
e String
::: represents the sequen
e in whi
h hypotheti
al reg-
isters are used in a basi
blo
k. This provides the basis for
allo
ation de
isions. ( Note : We use r1, r2, et
. to represent
hypotheti
al registers, and R1, R2, et
. to represent ma
hine,
i.e. real, registers.)
Example
Code Optimisation : 90
LOCAL REGISTER ALLOCATION
Managing the registers over a basi
blo
k
When all ma
hine registers hold useful values and a
new register is required for
al
ulations, we free one of the ma-
hine registers (say, Ri). This is
alled pre-emption of a register.
Let Ri
ontain the value represented by a hypotheti
al
register rj .
If rj (i.e. the data item represented by it) has been modied
sin
e it was last loaded from the memory
ell, then we need
to store its value in the memory
ell while freeing it for
another purpose.
(a) Belady proposed that the value whose next use lies
farthest in the register referen
e string should be
pre-empted from the ma
hine register.
(b) Kennedy, Horowitz, Fis
her dierentiate between
{ a referen
e of a hypotheti
al register, and
{ a referen
e-and-modi
ation of a hypotheti
al reg-
ister
during pre-emption, sin
e the latter requires a Store
while the former does not. They
onsider alterna-
tive pre-emption de
isions and
ompute the total
ost of the register allo
ation for the basi
blo
k.
The lowest
ost alternative is sele
ted.
Code Optimisation : 91
LOCAL REGISTER ALLOCATION
Example :
Consider lo
al register allo
ation in a 2-register
ma
hine for the referen
e string :
r1 r1 | 1
r2 r1 r2 1
r3 r1 r3 2
r1 r1 r3 0
r2 r1 r2 1
|||
total
ost = 5
Note :
Load and Store instru
tions are introdu
ed at
appropriate points, i.e. whenever the
ontents of a ma
hine
register are
hanged. Both are assumed to
ost 1 unit ea
h.
Code Optimisation : 92
LOCAL REGISTER ALLOCATION
Example :
Consider lo
al register allo
ation in a 2-register
ma
hine for the referen
e string :
Kennedy et al Algorithm :
Register Ma
hine Cost
referen
e Register
R1 R2
r1 r1 | 1
r2 r1 r2 1
r3 r3 r2 1
r1 r1 r2 1
r2 r1 r2 0
|||
total
ost = 4
Note :
Load and Store instru
tions are introdu
ed at
appropriate points, i.e. whenever the
ontents of a ma
hine
register are
hanged. Both are assumed to
ost 1 unit ea
h.
Code Optimisation : 93
LOCAL REGISTER ALLOCATION
Comparison of algorithms
The algorithm by Kennedy et al is an optimal algo-
rithm, in that it always nds the least
ost allo
ation. However,
this algorithm is
omputationally expensive sin
e it
onsiders
the alternative pre-emption de
isions and sele
ts the best one.
Belady's algorithm is not an optimal algorithm in that
it does not guarantee least
ost allo
ation. However, it it
om-
putationally e
ient.
The manner of variation of the register allo
ation ex-
penses with the size of the register referen
e string is very im-
portant from the viewpoint of
ompilation e
ien
y. As pro-
grams are
ontinually in
reasing in size, it is useful that an
algorithm used in the
ompiler should be linear in nature. If
this is not possible, it should at least not be exponential in its
behaviour. Belady's algorithm is more pra
ti
al from this view-
point.
Code Optimisation : 94
GLOBAL REGISTER ALLOCATION
Notation :
Code Optimisation : 95
GLOBAL REGISTER ALLOCATION
Nature of global allo
ation
Code Optimisation : 96
GLOBAL REGISTER ALLOCATION
Allo
ation of registers a
ross basi
blo
k boundaries.
Preliminaries
Code Optimisation : 97
GLOBAL REGISTER ASSIGNMENT
The prot of a live range
The prot of a live range lrx is the number of
exe
utions of Load/Store instru
tions of x eliminated by holding
its value in a register.
where
M Plr is the maximum prot for live range lr ,
Plr is the realisable prot for live range lr ,
#o
b is the number of Loads/Stores in b,
nb is the stati
nesting level of b, and
w is the nesting weightage (usually 5 or 10). (wnb indi-
ates how many times b may exe
ute in a run)
Code Optimisation : 98
GLOBAL REGISTER ASSIGNMENT
Live Range Examples
1 a :=
?
2
??
3 := a
?
4 := b
?
5
Code Optimisation : 99
GLOBAL REGISTER ASSIGNMENT
METHODS OF LIVE RANGE IDENTIFICATION
Chow-Hennessy Approa
h
A basi
blo
k of the program belongs to the live range
of a value if the value is live within that basi
blo
k and a refer-
en
e or a denition of the value rea
hes it. The live range is the
set of su
h basi
blo
ks.
1 a :=
?
2
??
3 := a
?
4 := b
?
5
1 a :=
?
2
??
3 := a
?
4 := b
?
5
Example
'$
&%
1
'$ '$
&%
2
&%
3
'$
&%
4
Graph Colouring
The problem of graph
olouring is dened as :
(a) give dierent
olours to nodes lri, lrj if the edge
(lri, lrj ) exists in IG,
(b) use minimum number of
olours
Example
'$
&%
1
'$
'$
&%
2
&%
3
'$
&%
4
'$ '$
&%
2
&%
3
'$
&%
4
22 ll
2
3 )
21
3
4
4
Algorithm outline
1. Separate
onstrained and un
onstrained live ranges.
2. While a
onstrained live range and an allo
atable register
exists
(a) Compute the priorities Plr for all live ranges, if
not already done.
(b) Find the lr with highest Plr
Colour this lr with a
olour not present in its
forbidden set.
Update the forbidden set for the neighbours of
this live range in IG.
Split the neighbours of this lr, if ne
essary.
(This is done when the forbidden set of a neigh-
bour is `full', i.e. it
ontains all possible
olours.)
3. Colour the un
onstrained live ranges.
1
a := : : :
b := : : :
??
52
2
x := :
y := 94 :
QQ
QQ
+ QQs
:= a
:=
::: b
3
:= 35
:
::: a b
4
QQ
QQ
QQs +
5 x := +
y b
?
6
::: := a b
d :=
2
?