8/12/2019 Dag Code Generationn
1/19
B e t t e r c o d e g e n e r a t i o n
G o a l i s t o p r o d u c e m o r e e c i e n t c o d e f o r e x p r e s s i o n s
W e c o n s i d e r
d i r e c t e d a c y c l i c g r a p h s ( D A G )
\ o p t i m a l " r e g i s t e r a l l o c a t i o n f o r t r e e s
S e t h i - U l l m a n
\ m o r e o p t i m a l " r e g i s t e r a l l o c a t i o n f o r t r e e s
P r o e b s t i n g - F i s c h e r
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1
8/12/2019 Dag Code Generationn
2/19
C o m m o n s u b e x p r e s s i o n s
C o n s i d e r t h e t r e e f o r t h e e x p r e s s i o n
a + a * ( b - c ) + ( b - c ) * d
+
+
a *
*
a -
b c
-
b c
d
B o t h a a n d b - c a r e c o m m o n s u b e x p r e s s i o n s ( c s e )
c o m p u t e t h e s a m e v a l u e
s h o u l d c o m p u t e t h e v a l u e o n c e
A s i m p l e a n d g e n e r a l f o r m o f c o d e i m p r o v e m e n t
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 2
8/12/2019 Dag Code Generationn
3/19
D i r e c t e d a c y c l i c g r a p h s
T h e d i r e c t e d a c y c l i c g r a p h i s a u s e f u l r e p r e s e n t a t i o n
f o r s u c h e x p r e s s i o n s
a + a * ( b - c ) + ( b - c ) * d
+
+ *
*
a -
b c
d
T h e d a g c l e a r l y e x p o s e s t h e c s e s
A h o , S e t h i , a n d U l l m a n , x 5 . 2 , x 9 . 8 ,
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 3
8/12/2019 Dag Code Generationn
4/19
D i r e c t e d a c y c l i c g r a p h s
A d i r e c t e d a c y c l i c g r a p h i s a t r e e w i t h s h a r i n g
a t r e e i s a d i r e c t e d a c y c l i c g r a p h w h e r e e a c h
n o d e h a s a t m o s t o n e p a r e n t
a d a g a l l o w s m u l t i p l e p a r e n t s f o r e a c h n o d e
b o t h a t r e e a n d a d a g h a v e a d i s t i n g u i s h e d r o o t
n o c y c l e s i n t h e g r a p h !
T o n d c o m m o n s u b e x p r e s s i o n s ( w i t h i n a s t a t e m e n t )
b u i l d t h e d a g
g e n e r a t e c o d e f r o m t h e d a g
T h i s s h o u l d l e a d t o f a s t e r e v a l u a t i o n
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 4
8/12/2019 Dag Code Generationn
5/19
D i r e c t e d a c y c l i c g r a p h s
H o w d o w e b u i l d a d a g f o r a n e x p r e s s i o n ?
u s e c o n s t r u c t i o n p r i m i t i v e s f o r b u i l d i n g t r e e
t e a c h p r i m i t i v e s t o c a t c h c s e ' s
| m k l e a f ( ) a n d m k n o d e ( )
| h a s h o n
u n i q u e n a m e f o r e a c h n o d e | i t s v a l u e n u m b e r
A n y w h e r e t h a t w e b u i l d a t r e e , w e c o u l d b u i l d a d a g
i n i t i a l i z e h a s h t a b l e o n e a c h e x p r e s s i o n
c a t c h o n l y c s e s w i t h i n e x p r e s s i o n
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 5
8/12/2019 Dag Code Generationn
6/19
D i r e c t e d a c y c l i c g r a p h s
W h a t a b o u t a s s i g n m e n t ?
c o m p l i c a t e s c s e d e t e c t i o n
e a c h v a l u e h a s a u n i q u e n o d e
a d d s u b s c r i p t s t o v a r i a b l e s
W h i l e b u i l d i n g t h e d a g , a n a s s i g n m e n t
c r e a t e s n e w n o d e f o r l h s | a n e w x
i
k i l l s a l l n o d e s b u i l t f r o m x
i ? 1
E x a m p l e
a
1
a
0
+ b
C a n w e g o b e y o n d a s i n g l e s t a t e m e n t ?
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 6
8/12/2019 Dag Code Generationn
7/19
D i r e c t e d a c y c l i c g r a p h s
U s e a s i n g l e d a g f o r a n e n t i r e b a s i c b l o c k
A d a g f o r a b a s i c b l o c k h a s l a b e l e d n o d e s
1 . l e a v e s a r e l a b e l e d w i t h u n i q u e i d e n t i e r
| e i t h e r v a r i a b l e n a m e s o r c o n s t a n t s
| l v a l u e s o r r v a l u e s ( o b v i o u s b y c o n t e x t )
| l e a v e s r e p r e s e n t v a l u e s o n e n t r y , x
0
2 . i n t e r i o r n o d e s a r e l a b e l e d w i t h o p e r a t o r s
3 . n o d e s h a v e o p t i o n a l i d e n t i e r l a b e l s
| i n t e r i o r n o d e s r e p r e s e n t c o m p u t e d v a l u e s
| i d e n t i e r l a b e l r e p r e s e n t s a s s i g n m e n t
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 7
8/12/2019 Dag Code Generationn
8/19
D i r e c t e d a c y c l i c g r a p h s
E x a m p l e
C o d e A f t e r R e n a m i n g
a b + c a
0
b
0
+ c
0
b a - d b
1
a
0
- d
0
c b + c c
1
b
1
+ c
0
d a - d d
1
a
0
- d
0
+
-
+
b
0
c
0
d
0
c
1
b
1
, d
1
a
0
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 8
8/12/2019 Dag Code Generationn
9/19
D i r e c t e d a c y c l i c g r a p h s
B u i l d i n g a d a g
n o d e ( < i d > ) ! c u r r e n t d a g f o r < i d >
1 . s e t n o d e ( y ) t o u n d e n e d , f o r e a c h s y m b o l y
2 . f o r e a c h s t a t e m e n t x y o p z , r e p e a t s t e p s 3 ,
4 , a n d 5
3 . i f n o d e ( y ) i s u n d e n e d ,
c r e a t e a l e a f f o r y
s e t n o d e ( y ) t o t h e n e w n o d e
d o t h e s a m e f o r z
4 . i f d o e s n ' t e x i s t ,
c r e a t e i t a n d l e t n p o i n t t o t h a t n o d e
5 . d e l e t e x f r o m t h e l i s t o f l a b e l s f o r n o d e ( x )
a p p e n d x t o t h e l i s t o f l a b e l s f o r n
s e t n o d e ( x ) t o n
A h o , S e t h i , a n d U l l m a n , A l g o r i t h m 9 . 2 , i n x 9 8
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 9
8/12/2019 Dag Code Generationn
10/19
D i r e c t e d a c y c l i c g r a p h s
R e a l i t y
D o c o m p i l e r s r e a l l y u s e t h i s s t u ?
T h e d a g c o n s t r u c t i o n a l g o r i t h m i s f a s t e n o u g h
A c o m p i l e r s t h a t u s e s q u a d s w i l l ( o f t e n )
b u i l d a d a g t o n d c s e s
c o n v e r t b a c k t o q u a d s f o r l a t e r p a s s e s
A r e t h e r e m a n y c s e s ? Y e s !
t h e y a r i s e i n a d d r e s s i n g
a r r a y s u b s c r i p t c o d e
e l d a c c e s s i n r e c o r d s
e x p r e s s i o n s b a s e d o n l o o p i n d i c e s
a c c e s s t o p a r a m e t e r s
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 0
8/12/2019 Dag Code Generationn
11/19
O p t i m a l c o d e
A c o m m e n t o n t h e w o r d \ o p t i m a l "
A h o , S e t h i , a n d U l l m a n u s e o p t i m a l a l o t
p a r t i c u l a r l y i n r e g a r d t o c o d e g e n e r a t i o n
l o o k c l o s e l y a t t h e u n d e r l y i n g a s s u m p t i o n s
l o o k f o r s i m p l i c a t i o n s , l i k e \ n o s h a r i n g "
T h e r e c a n ' t b e t h a t m a n y
o p t i m a l c o d e s e q u e n c e s , o r
w a y s o f g e n e r a t i n g t h e m
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 1
8/12/2019 Dag Code Generationn
12/19
M a c h i n e m o d e l
F o r c o d e g e n e r a t i o n , A h o , S e t h i , a n d U l l m a n p r o p o s e
a s i m p l e m a c h i n e m o d e l .
b y t e - a d d r e s s a b l e m a c h i n e w i t h f o u r b y t e w o r d s
n g e n e r a l p u r p o s e r e g i s t e r s
t w o - a d d r e s s i n s t r u c t i o n s | o p s r c , d e s t
M o d e F o r m A d d r e s s A d d e d c o s t
a b s o l u t e M M 1
r e g i s t e r R R 0
i n d e x e d o ( R ) o + c ( R ) 1
i n d . r e g i s t e r * R c ( R ) 0
i n d . i n d e x e d * o ( R ) c ( o + c ( R ) ) 1
A h o , S e t h i , a n d U l l m a n , x 9 2
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 2
8/12/2019 Dag Code Generationn
13/19
8/12/2019 Dag Code Generationn
14/19
P h a s e 1
i f n i s a l e a f t h e n
i f n i s t h e l e f t m o s t c h i l d t h e n
l a b e l ( n ) 1
e l s e l a b e l ( n ) 0
e l s e b e g i n / * n i s a n i n t e r i o r n o d e * /
l e t n
1
; n
2
; : : : ; n
k
b e t h e c h i l d r e n o f n ,
o r d e r e d s o t h a t
l a b e l ( n
1
) l a b e l ( n
2
) l a b e l ( n
k
)
l a b e l ( n ) m a x
1 i k
( l a b e l ( n
i
) + i ? 1 )
C a n c o m p u t e l a b e l s i n p o s t o r d e r
l a b e l i s d e n e d r e c u r s i v e l y a s :
l a b e l ( n ) =
0
B
B
B
@
m a x ( l
1
; l
2
) i f l
1
6= l
2
l
1
+ 1 i f l
1
= l
2
1
C
C
C
A
A h o , S e t h i , a n d U l l m a n , x 9 . 1 0
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 4
8/12/2019 Dag Code Generationn
15/19
P h a s e 2
A s s u m p t i o n s
i n p u t t r e e i s l a b e l e d b y P h a s e 1
r s t a c k i s a s t a c k o f r e g i s t e r s
| i n i t i a l i z e t o r 0 , r 1 , : : : , r k
s w a p ( r s t a c k ) i n t e r c h a n g e s t o p t w o r e g i s t e r s
| e n s u r e s l e f t c h i l d a n d p a r e n t i n s a m e r e g i s t e r
t s t a c k i s a s t a c k o f t e m p o r a r y l o c a t i o n s
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 5
8/12/2019 Dag Code Generationn
16/19
P h a s e 2
C o d e f o r p h a s e 2
p r o c e d u r e g e n c o d e ( n )
/ * c a s e 0 | j u s t l o a d i t * /
i f n i s l e a f \ n a m e " a n d l e f t m o s t c h i l d
g e n ( m o v , n a m e , t o p ( r s t a c k ) )
e l s e i f n i s i n t e r i o r n o d e \ o p n
1
n
2
" t h e n
/ * c a s e 1 | n
1
i n r e g , n
2
i n R A M * /
i f l a b e l ( n
2
) = 0 t h e n
g e n c o d e ( n
1
)
g e n ( o p , n a m e o f n
2
, t o p ( r s t a c k ) )
/ * c a s e 2 | n
1
n e e d s n o s t o r e s * /
/ * b u t n
2
n e e d s m o r e r e g i s t e r s * /
e l s e i f 1 l a b e l ( n
1
) l a b e l ( n
2
)
a n d l a b e l ( n
1
) < r t h e n
s w a p ( r s t a c k )
g e n c o d e ( n
2
)
R p o p ( r s t a c k )
g e n c o d e ( n
1
)
g e n ( o p , R , t o p ( r s t a c k ) )
p u s h ( r s t a c k , R )
s w a p ( r s t a c k )
A h o , S e t h i , & U l l m a n , x 9 . 1 0
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 6
8/12/2019 Dag Code Generationn
17/19
P h a s e 2
/ * c a s e 3 | s y m m e t r i c t o c a s e 2 * /
e l s e i f 1 l a b e l ( n
2
) l a b e l ( n
1
) a n d
l a b e l ( n
2
) < r t h e n
g e n c o d e ( n
1
)
R = p o p ( r s t a c k )
g e n c o d e ( n
2
)
g e n ( o p , t o p ( r s t a c k ) , R )
p u s h ( r s t a c k , R )
/ * c a s e 4 | n e e d a t e m p o r a r y * /
e l s e
g e n c o d e ( n
2
)
T p o p ( t s t a c k )
g e n ( M O V , t o p ( r s t a c k ) , T )
g e n c o d e ( n
1
)
p u s h ( t s t a c k , T )
g e n ( o p , T , t o p ( r s t a c k ) )
A h o , S e t h i , & U l l m a n , x 9 . 1 0
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 7
8/12/2019 Dag Code Generationn
18/19
E x a m p l e
- t 4
2
+ t 1
1
- t 3
2
a
1
b
0
e
1
+ t 2
1
c
1
d
0
g e n c o d e ( t 4 ) c a s e 2
g e n c o d e ( t 3 ) c a s e 3
g e n c o d e ( e ) c a s e 0
m o v e , r 1
g e n c o d e ( t 2 ) c a s e 1
g e n c o d e ( c ) c a s e 0
m o v c , r 0
a d d d , r 0
s u b r 0 , r 1
g e n c o d e ( t 1 ) c a s e 1
g e n c o d e ( a ) c a s e 0
m o v a , r 0
a d d b , r 0
s u b r 1 , r 0
C P S C 4 3 4 L e c t u r e 2 0 , P a g e 1 8
8/12/2019 Dag Code Generationn
19/19
E x t e n s i o n s t o t h e l a b e l i n g s c h e m e
M u l t i p l e r e g i s t e r o p e r a t i o n s
i n c r e a s e b a s e c a s e t o r e s e r v e r e g i s t e r s
p a i r e d r e g i s t e r s m a y r e q u i r e t r i p l e s
A l g e b r a i c p r o p e r t i e s
c o m m u t a t i v i t y , a s s o c i a t i v i t y t o l o w e r l a b e l s
d e e p , n a r r o w , l e f t - b i a s e d t r e e s
C o m m o n s u b e x p r e s s i o n s ( d a g s )
i n c r e a s e s c o m p l e x i t y o f c o d e g e n e r a t i o n
( N P - C o m p l e t e )
p a r t i t i o n i n t o s u b t r e e s t h a t h a v e c s e s a s r o o t s
o r d e r t r e e s a n d a p p l y S e t h i - U l l m a n