generated from dannypsnl-fork/racket-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-value-numbering.rkt
167 lines (158 loc) · 5.57 KB
/
local-value-numbering.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#lang nanopass
(define (op? v)
(member v '(+ - * / ^)))
(define-language IR
(entry Prog)
(terminals
(symbol (name))
(op (op))
(integer (int)))
(Expr (expr)
name
int)
(Inst (inst)
(name expr) => (name = expr)
(name op expr0 expr1) => (name = expr0 op expr1)
)
(Prog (prog)
(inst* ...)))
(define-pass local-value-numbering : (IR Prog) (prog) -> (IR Prog) ()
(definitions
(define m (make-hash)))
(f : Inst (inst) -> Inst ()
[(,name ,expr)
(hash-set! m name (hash-ref m expr expr))
`(,name ,(hash-ref m expr expr))]
[(,name ,op ,expr0 ,expr1)
(define v0 (hash-ref m expr0))
(define v1 (hash-ref m expr1))
(define new-key (list op v0 v1))
(if (hash-ref m new-key #f)
(let ([v (hash-ref m new-key)])
(hash-set! m name v)
`(,name ,v))
(begin
(hash-set! m new-key name)
inst))])
(Prog : Prog (prog) -> Prog ()
[(,inst* ...)
`(,(map f inst*) ...)]))
(define-pass extend-local-value-numbering : (IR Prog) (prog) -> (IR Prog) ()
(definitions
(define m (make-hash))
(define const-value (make-hash))
(define (const? v)
(hash-ref const-value v
(integer? v))))
(f : Inst (inst) -> Inst ()
[(,name ,expr)
(if (const? expr)
(begin
(hash-set! const-value name
(hash-ref const-value expr expr))
`(,name ,(hash-ref const-value name)))
(begin
(hash-set! m name (hash-ref m expr expr))
`(,name ,(hash-ref m expr expr))))]
[(,name ,op ,expr0 ,expr1)
(define v0 (hash-ref m expr0
(hash-ref const-value expr0 expr0)))
(define v1 (hash-ref m expr1
(hash-ref const-value expr1 expr1)))
(if (and (const? expr0) (const? expr1))
(begin
(hash-set! const-value name
(case op
[(+) (+ v0 v1)]
[(-) (- v0 v1)]
[(*) (* v0 v1)]
[(/) (/ v0 v1)]))
`(,name ,(hash-ref const-value name)))
(match (list op v0 v1)
[(list-no-order '+ a 0)
`(,name ,a)]
[(list '- a a)
(hash-set! const-value name 0)
`(,name 0)]
[(list '- a 0) `(,name ,a)]
[(list-no-order '* a 2)
`(,name + ,a ,a)]
[(list-no-order '* a 1)
`(,name ,a)]
[(list-no-order '* a 0)
(hash-set! const-value name 0)
`(,name 0)]
[(list '/ a 1)
`(,name ,a)]
[(list '/ a a) #:when (not (eq? a 0))
(hash-set! const-value name 1)
`(,name 1)]
[_ (define new-key (set op v0 v1))
(if (hash-ref m new-key #f)
(let ([v (hash-ref m new-key)])
(hash-set! m name v)
`(,name ,v))
(begin
(hash-set! m new-key name)
inst))]))])
(Prog : Prog (prog) -> Prog ()
[(,inst* ...)
`(,(map f inst*) ...)]))
(module+ test
(require rackunit)
(define-parser parse IR)
(check-equal? (local-value-numbering
(parse '([a 1]
[b 2]
[c + a b]
[d - a b]
[e + a b]
[f e]
[g + b a])))
(parse '([a 1]
[b 2]
[c + a b]
[d - a b]
[e c]
[f c]
[g + b a])))
(test-case "constant folding"
(check-equal? (extend-local-value-numbering
(parse '([a 1]
[b 2]
[c + a b]
[d - a b]
[e + a b]
[f e]
[g + b a])))
(parse '([a 1]
[b 2]
[c 3]
[d -1]
[e 3]
[f 3]
[g 3]))))
(test-case "commutative operations"
(check-equal? (extend-local-value-numbering
(parse '([c + a b]
[d - a b]
[e + a b]
[f e]
[g + b a])))
(parse '([c + a b]
[d - a b]
[e c]
[f c]
[g c]))))
(test-case "algebraic identities"
(check-equal? (extend-local-value-numbering
(parse '([c + 0 a]
[d - a 0]
[e * a 2]
[f - b b]
[g / b b])))
(parse '([c a]
[d a]
[e + a a]
[f 0]
[g 1])))))