Você está na página 1de 33

More Code Generation

and Optimization
Pat Morin
COMP 3002
2
Outline
DAG representation of basic blocks
Peephole optimization
Register allocation by graph coloring
3
Basic Blocks as DAGs
4
Basic Blocks as DAGs
Like expressions, we can represent basic blocks
as DAGs
Create a node for the initial al!e of each ariable that
appears in the block
"or each statement s, create a node N whose children are
the nodes defining the operands of s
#ach node N is labelled by the operation !sed
#ach node N has a list of ariables that it defines
A node $ is marked as an o!tp!t node if its al!e is !sed
o!tside the basic block
5
Basic Block Example
a := b + c
b := a - d
c := b + c
d := a - d
b0 c0
d0
+ (a)
- (b,d)
+ (c)
6
Basic Block Example
a := b + c
b := b - d
c := c + d
e := b + c
Draw the DAG for this basic block
7
Dead Code Elimination
Dead code is code that comp!tes a al!e that is
neer !sed
%nspecting the flow graph we can tell if the al!e
of a ariable comp!ted within a basic block is
!sed o!tside that block
&hese are called lie ariables or lie on exit
Algorithm
'hile some node N has no ancestor and no lie ariable
attached
Delete N and its o!tgoing edges
8
Dead Code Elimination - Example
Remoe dead code ass!ming c is the only lie
ariable
b0 c0
d0
+ (a)
- (b,d)
+ (c)
+ (x)
+ (y)
9
Using Algebraic Identities
&he DAG representation can allow constant
folding
'hen a s!bexpression inoles ( or more constants its
al!e can sometimes be comp!ted at compile time
Comm!tatiity) x * y + y * x
'hen creating a node for x * y we sho!ld check if there
already exists a node named x * y or a node named y * x
Associatiity) ,a - b. - c + a - ,b - c.
10
Associatiit!
Source Code:
a = b + c;
e + c + d + b;
3-AI (slow):
a := b + c
t := c + d
e := t + b
3-AI (fast):
a := b + c
e := a + b
An example of !sing associatiity
11
Arra! "e#erences
Array references are a different kind of operator
Assigning to an array location can change can
change any element of the array
x = a[!
a["! = y
# = a[!
a0 0
=[! (x,#)
"0
[!= (a["!)
y0
WRONG
12
Arra!s "e#erences
&he right way
x + a/i0 creates a node with two children ,a and i. that defines
x
a/10 + y creates a node with 2 children
%t has the side3effect of killing any node whose al!e
depends on a
x = a[!
a["! = y
# = a[!
a0 0
=[! (x)
"0
[!=
y0
=[! #
13
$ointer "e#erences
Pointer references are similar to array references
assigning to a pointer kills all nodes
changing any ariable kills all pointers references
x = $%
t = a + b
# = $% ; &aybe % %o'ts to t
$( = y
x = a + b ; &aybe ( %o'ts to a or b
14
"eassembling Basic Blocks
After conerting a basic block into a DAG and
applying optimizations we m!st reassemble the
basic block
R!les)
4. &he order of instr!ctions m!st obey the order of the DAG
(.Assigning to an array m!st follow any preio!s assignments
to the same array
2.#al!ations of an array m!st follow any preio!s
assignments to the same array
5.Any !se of a ariable m!st follow any preio!s indirect
,pointer. assignments
6.Any indirect assignment m!st follow all preio!s eal!ations
of ariables
15
"earrangement Order Example
a0 0
=[! (x)
"0
[!=
y0
=[! #
x = a[!
a["! = y
# = a[!
Add extra edges to the DAG to implement r!les
(36
16
Basic Blocks as DAGs - Examples
"or each of the following lines of C
x + a - b * c7
x + a 8 ,b-c. 3 d * ,e-f.7
x + a/i0 - 47
a/i0 + b/c/i007
*p-- + *9--7
'rite a basic block of 23address instr!ctions
Conert the basic block into a DAG
Conert the DAG back into a basic block
17
$eep%ole Optimization
18
$eep%ole Optimization
Peephole optimization is done by looking at a
small n!mber of lines of code at a time
'e are looking at a small piece of code ,the peephole.
:ome r!les are applied to simplify or speed !p
ario!s patterns
Can be applied to intermediate code or machine
code
19
"edundant &oads and 'tores
An ad1acent pair of load and store operations ,to
the same ariable. is !nnecessary and can be
eliminated
&o) x, *0
&o) *0, x
fload +
fstore +
fload +
%o%
20
Unreac%able Code
#xamining the code s!rro!nding 1!mp statements
can often help
f debu, == - ,oto .-
,oto ./
.-: so&e debu, code000
./:
f debu, 1= - ,oto ./
so&e debu, code000
./:
f 0 1= - ,oto ./
so&e debu, code000
./:
,oto ./
so&e debu, code000
./:
,oto ./
./:
21
(lo) o# Control Optimization
,oto .-
000
.-: ,oto ./
,oto ./
000
.-: ,oto ./
f a 2 b ,oto .-
000
.-: ,oto ./
f a 2 b ,oto ./
000
.-: ,oto ./
,oto .-
000
.-: f a 2 b ,oto ./
.3:
f a 2 b ,oto ./
,oto .3
000
.3:
[sa)es a ,oto w3e' a 2 b!
22
Algebraic 'impli#ication and "eduction
in 'trengt%
Peephole optimization may recognize algebraic
simplification and red!ction in strength
x + x - 4 +; inc x /machine instr!ction0
x + x - < +; !nnecessary
x + x * 4 +; !nnecessary
x + ( * x +; x + x - x
&o do this in the =>? we need a 2 line peephole
23
"egister Allocation b! Grap% Coloring
24
"egister Allocation B! Grap% Coloring
'e hae seen how to manage registers within a
basic block
lazy load and store algorithm
spill ,or fl!sh. a register back to memory only when
necessary
'e hae seen how to assign @register ariables@
across blocks ,saings calc!lations.
Aoth these methods are he!ristics
25
"egister Allocation b! Grap% Coloring
:tep 4) Generate code as if we hae infinitely
many registers R4,R(,R2,BBBB
R4, R(, R2,BBB are 1!st like ariable names
:tep () Create the register interference graph
$odes are R4, R(, R2, BBBB
&wo nodes are ad1acent if they are actie at the same time
:tep 2) Properly color the register interference
graph with the @colors@ 4,(,2,BBB,k where k is as
small as possible
26
"egister Allocation b! Grap% Coloring
A irt!al register Ri that is colored with color c is
assigned to the real register R,c mod r. where r is
the n!mber of real registers
%f k C+ r then there are no conflicts and eery
ariable gets its own register
27
*%e "egister Inter#erence Grap%
"or each register Ri, determine which lines of
which basic blocks the register Ri is actie in
Ri conflicts with R1 if they are actie at the same
time
28
Coloring a Grap%
Coloring a graph with the fewest colors in $P3hard
&he following he!ristic is !sed to color a graph
G+,V,E. with the colors 4,(,2,BBB
"ind the ertex v with smallest degree ,say d.
Rec!rsiely color the graph G D EvF
Color with smallest color not !sed by one of GGs
neighbo!rs
,$ote) will receie color at most d-4.
29
Grap% Color Example
30
Grap% Coloring Example
31
Grap% Coloring Example
32
+otes on Grap% Coloring
'hat we are act!ally trying to find is a balanced
ordering on the ertices of G
%f at most d neighbo!rs of v appear before v in the ordering
then v will be colored with color at most d-4
&hor!p ,4HH6. has shown that str!ct!red
programming lang!ages generate register
interference graphs with small treewidth
Iptimally coloring a graph with tree width t can be done in
O,f,t. n. where f,t. is a really fast growing f!nction of tB
Iptimal graph coloring can be done in linear timeJ
33
'ummar!
'eGe seen some more code generation and
optimization techni9!es
Representing basic blocks as DAGs allows)
Common s!bexpression elimination
Dead code elimination
Kse of algebraic identities
Reordering of instr!ctions
Peephole optimization allows)
Kseless code elimination
Kse of algebraic identities and machine idioms
Register allocation by graph color
Can do @optimal@ register allocation

Você também pode gostar