Debian Codegraph @

scheme/ikarus.compiler.optimize-letrec.ss

raw
1
2
3 (module (debug-scc optimize-letrec current-letrec-pass)
4
5
6 (define (unique-prelex x)
7 (let ([x (make-prelex (prelex-name x) (prelex-operand x))])
8 (set-prelex-source-referenced?! x #t)
9 x))
10
11 (define (build-assign* lhs* rhs* body)
12 (define (mark-assigned! lhs)
13 ;;; FIXME: this is very fragile
14 (unless (prelex-source-assigned? lhs)
15 (set-prelex-source-assigned?! lhs
16 (or (prelex-global-location lhs) #t))))
17 (for-each mark-assigned! lhs*)
18 (let f ([lhs* lhs*] [rhs* rhs*])
19 (cond
20 [(null? lhs*) body]
21 [else
22 (make-seq
23 (make-assign (car lhs*) (car rhs*))
24 (f (cdr lhs*) (cdr rhs*)))])))
25
26 (define (optimize-letrec/basic x)
27 (define who 'optimize-letrec/basic)
28 (define (do-rec*bind lhs* rhs* body)
29 (make-bind lhs* (map (lambda (x) (make-constant #f)) lhs*)
30 (build-assign* lhs* rhs* body)))
31 (define (do-recbind lhs* rhs* body)
32 (let ([t* (map unique-prelex lhs*)])
33 (make-bind lhs* (map (lambda (x) (make-constant #f)) lhs*)
34 (make-bind t* rhs*
35 (build-assign* lhs* t* body)))))
36 (define (L x)
37 (struct-case x
38 [(clambda g cls* cp free name)
39 (make-clambda g
40 (map (lambda (x)
41 (struct-case x
42 [(clambda-case info body)
43 (make-clambda-case info (E body))]))
44 cls*)
45 cp free name)]))
46 (define (E x)
47 (struct-case x
48 [(constant) x]
49 [(prelex)
50 (assert (prelex-source-referenced? x))
51 x]
52 [(assign lhs rhs)
53 (assert (prelex-source-assigned? lhs))
54 (make-assign lhs (E rhs))]
55 [(primref) x]
56 [(bind lhs* rhs* body)
57 (if (null? lhs*)
58 (E body)
59 (make-bind lhs* (map E rhs*) (E body)))]
60 [(recbind lhs* rhs* body)
61 (if (null? lhs*)
62 (E body)
63 (do-recbind lhs* (map E rhs*) (E body)))]
64 [(rec*bind lhs* rhs* body)
65 (if (null? lhs*)
66 (E body)
67 (do-rec*bind lhs* (map E rhs*) (E body)))]
68 [(conditional e0 e1 e2)
69 (make-conditional (E e0) (E e1) (E e2))]
70 [(seq e0 e1) (make-seq (E e0) (E e1))]
71 [(clambda g cls* cp free name)
72 (L x)]
73 [(funcall rator rand*)
74 (make-funcall (E rator) (map E rand*))]
75 [(mvcall p c)
76 (make-mvcall (E p) (E c))]
77 [(forcall rator rand*)
78 (make-forcall rator (map E rand*))]
79 [else (error who "invalid expression" (unparse x))]))
80 (E x))
81
82
83 (define (optimize-letrec/waddell x)
84 (define simple-primitives '())
85 (define who 'optimize-letrec/waddell)
86 (define (extend-hash lhs* h ref)
87 (for-each (lambda (lhs) (hashtable-set! h lhs #t)) lhs*)
88 (lambda (x)
89 (unless (hashtable-ref h x #f)
90 (hashtable-set! h x #t)
91 (ref x))))
92 (define (E* x* ref comp)
93 (cond
94 [(null? x*) '()]
95 [else
96 (cons (E (car x*) ref comp)
97 (E* (cdr x*) ref comp))]))
98 (define (do-rhs* i lhs* rhs* ref comp vref vcomp)
99 (cond
100 [(null? rhs*) '()]
101 [else
102 (let ([h (make-eq-hashtable)]
103 [rest (do-rhs* (fxadd1 i) lhs* (cdr rhs*) ref comp vref vcomp)])
104 (let ([ref
105 (lambda (x)
106 (unless (hashtable-ref h x #f)
107 (hashtable-set! h x #t)
108 (ref x)
109 (when (memq x lhs*)
110 (vector-set! vref i #t))))]
111 [comp
112 (lambda ()
113 (vector-set! vcomp i #t)
114 (comp))])
115 (cons (E (car rhs*) ref comp) rest)))]))
116 (define (partition-rhs* i lhs* rhs* vref vcomp)
117 (cond
118 [(null? lhs*) (values '() '() '() '() '() '())]
119 [else
120 (let-values
121 ([(slhs* srhs* llhs* lrhs* clhs* crhs*)
122 (partition-rhs* (fxadd1 i) (cdr lhs*) (cdr rhs*) vref vcomp)]
123 [(lhs rhs) (values (car lhs*) (car rhs*))])
124 (cond
125 [(prelex-source-assigned? lhs)
126 (values slhs* srhs* llhs* lrhs* (cons lhs clhs*) (cons rhs crhs*))]
127 [(clambda? rhs)
128 (values slhs* srhs* (cons lhs llhs*) (cons rhs lrhs*) clhs* crhs*)]
129 [(or (vector-ref vref i) (vector-ref vcomp i))
130 (values slhs* srhs* llhs* lrhs* (cons lhs clhs*) (cons rhs crhs*))]
131 [else
132 (values (cons lhs slhs*) (cons rhs srhs*) llhs* lrhs* clhs* crhs*)]
133 ))]))
134 (define (do-recbind lhs* rhs* body ref comp letrec?)
135 (let ([h (make-eq-hashtable)]
136 [vref (make-vector (length lhs*) #f)]
137 [vcomp (make-vector (length lhs*) #f)])
138 (let* ([ref (extend-hash lhs* h ref)]
139 [body (E body ref comp)])
140 (let ([rhs* (do-rhs* 0 lhs* rhs* ref comp vref vcomp)])
141 (let-values ([(slhs* srhs* llhs* lrhs* clhs* crhs*)
142 (partition-rhs* 0 lhs* rhs* vref vcomp)])
143 ;;; (let ([made-complex
144 ;;; (filter (lambda (x) (not (var-assigned x)))
145 ;;; clhs*)])
146 ;;; (unless (null? made-complex)
147 ;;; (set! complex-count
148 ;;; (+ complex-count (length made-complex)))
149 ;;; (printf "COMPLEX (~s) = ~s\n"
150 ;;; complex-count
151 ;;; (map unparse made-complex))))
152 (let ([void* (map (lambda (x) (make-constant (void))) clhs*)])
153 (make-bind slhs* srhs*
154 (make-bind clhs* void*
155 (make-fix llhs* lrhs*
156 (if letrec?
157 (let ([t* (map unique-prelex clhs*)])
158 (make-bind t* crhs*
159 (build-assign* clhs* t* body)))
160 (build-assign* clhs* crhs* body)))))))))))
161 (define (E x ref comp)
162 (struct-case x
163 [(constant) x]
164 [(prelex) (ref x) x]
165 [(assign lhs rhs)
166 (ref lhs)
167 (comp)
168 (make-assign lhs (E rhs ref comp))]
169 [(primref) x]
170 [(bind lhs* rhs* body)
171 (let ([rhs* (E* rhs* ref comp)])
172 (let ([h (make-eq-hashtable)])
173 (let ([body (E body (extend-hash lhs* h ref) comp)])
174 (make-bind lhs* rhs* body))))]
175 [(recbind lhs* rhs* body)
176 (if (null? lhs*)
177 (E body ref comp)
178 (do-recbind lhs* rhs* body ref comp #t))]
179 [(rec*bind lhs* rhs* body)
180 (if (null? lhs*)
181 (E body ref comp)
182 (do-recbind lhs* rhs* body ref comp #f))]
183 [(conditional e0 e1 e2)
184 (make-conditional (E e0 ref comp) (E e1 ref comp) (E e2 ref comp))]
185 [(seq e0 e1) (make-seq (E e0 ref comp) (E e1 ref comp))]
186 [(clambda g cls* cp free name)
187 (make-clambda g
188 (map (lambda (x)
189 (struct-case x
190 [(clambda-case info body)
191 (let ([h (make-eq-hashtable)])
192 (let ([body (E body (extend-hash (case-info-args info) h ref) void)])
193 (make-clambda-case info body)))]))
194 cls*)
195 cp free name)]
196 [(funcall rator rand*)
197 (let ([rator (E rator ref comp)] [rand* (E* rand* ref comp)])
198 (struct-case rator
199 [(primref op)
200 (unless (memq op simple-primitives)
201 (comp))]
202 [else
203 (comp)])
204 (make-funcall rator rand*))]
205 [(mvcall p c)
206 (let ([p (E p ref comp)] [c (E c ref comp)])
207 (comp)
208 (make-mvcall p c))]
209 [(forcall rator rand*)
210 (make-forcall rator (E* rand* ref comp))]
211 [else (error who "invalid expression" (unparse x))]))
212 (E x (lambda (x) (error who "free var found" x))
213 void))
214
215 (define (optimize-letrec/scc x)
216 (define who 'optimize-letrec/scc)
217 (module (get-sccs-in-order)
218 (define-struct node (data link* lowlink root done collection))
219 (define (create-graph v* e** data*)
220 (define h (make-eq-hashtable))
221 (let ([v*
222 (let f ([v* v*] [data* data*])
223 (cond
224 [(null? v*) '()]
225 [else
226 (let ([node (make-node (car data*) '() #f #f #f #f)])
227 (hashtable-set! h (car v*) node)
228 (cons node (f (cdr v*) (cdr data*))))]))])
229 (for-each
230 (lambda (v e*)
231 (set-node-link*! v
232 (map (lambda (f)
233 (or (hashtable-ref h f #f)
234 (error who "invalid node" f)))
235 e*)))
236 v* e**)
237 v*))
238 (define (compute-sccs v*) ; Tarjan's algorithm
239 (define scc* '())
240 (define (compute-sccs v)
241 (define index 0)
242 (define stack '())
243 (define (tarjan v)
244 (let ([v-index index])
245 (set-node-root! v v-index)
246 (set! stack (cons v stack))
247 (set! index (fx+ index 1))
248 (for-each
249 (lambda (v^)
250 (unless (node-done v^)
251 (unless (node-root v^) (tarjan v^))
252 (set-node-root! v (fxmin (node-root v) (node-root v^)))))
253 (node-link* v))
254 (when (fx= (node-root v) v-index)
255 (set! scc*
256 (cons
257 (let f ([ls stack])
258 (let ([v^ (car ls)])
259 (set-node-done! v^ #t)
260 (cons v^ (if (eq? v^ v)
261 (begin (set! stack (cdr ls)) '())
262 (f (cdr ls))))))
263 scc*)))))
264 (tarjan v))
265 (for-each (lambda (v) (unless (node-done v) (compute-sccs v))) v*)
266 (reverse scc*))
267 (define (get-sccs-in-order n* e** data*)
268 (let ([G (create-graph n* e** data*)])
269 (let ([sccs (compute-sccs G)])
270 (map (lambda (scc) (map node-data scc)) sccs)))))
271 (define (gen-letrecs scc* ordered? body)
272 (define (mkfix b* body)
273 (if (null? b*)
274 body
275 (make-fix (map binding-lhs b*)
276 (map binding-rhs b*)
277 body)))
278 (define (gen-letrec scc fix* body)
279 (define (mklet lhs* rhs* body)
280 (if (null? lhs*)
281 body
282 (make-bind lhs* rhs* body)))
283 (define (lambda-binding? x)
284 (and (not (prelex-source-assigned? (binding-lhs x)))
285 (clambda? (binding-rhs x))))
286 (define (mkset!s b* body)
287 (cond
288 [(null? b*) body]
289 [else
290 (let* ([b (car b*)]
291 [lhs (binding-lhs b)])
292 (unless (prelex-source-assigned? lhs)
293 (when (debug-scc)
294 (printf "MADE COMPLEX ~s\n" (unparse lhs)))
295 (set-prelex-source-assigned?! lhs
296 (or (prelex-global-location lhs) #t)))
297 (make-seq
298 (make-assign lhs (binding-rhs b))
299 (mkset!s (cdr b*) body)))]))
300 (cond
301 [(null? (cdr scc))
302 (let ([b (car scc)])
303 (cond
304 [(lambda-binding? b)
305 (values (cons b fix*) body)]
306 [(not (memq b (binding-free* b)))
307 (values '()
308 (mklet (list (binding-lhs b))
309 (list (binding-rhs b))
310 (mkfix fix* body)))]
311 [else
312 (values '()
313 (mklet (list (binding-lhs b))
314 (list (make-funcall (make-primref 'void) '()))
315 (mkset!s scc
316 (mkfix fix* body))))]))]
317 [else
318 (let-values ([(lambda* complex*)
319 (partition lambda-binding? scc)])
320 (cond
321 [(null? complex*)
322 (values (append lambda* fix*) body)]
323 [else
324 (let ([complex*
325 (if ordered? (sort-bindings complex*) complex*)])
326 (values '()
327 (mklet (map binding-lhs complex*)
328 (map (lambda (x)
329 (make-funcall (make-primref 'void) '()))
330 complex*)
331 (mkfix (append lambda* fix*)
332 (mkset!s complex* body)))))]))]))
333 (let-values ([(fix* body)
334 (let f ([scc* scc*])
335 (cond
336 [(null? scc*) (values '() body)]
337 [else
338 (let-values ([(fix* body) (f (cdr scc*))])
339 (gen-letrec (car scc*) fix* body))]))])
340 (mkfix fix* body)))
341 (define (do-recbind lhs* rhs* body bc ordered?)
342 (define (make-bindings lhs* rhs* bc i)
343 (cond
344 [(null? lhs*) '()]
345 [else
346 (let ([b (make-binding i (car lhs*) (car rhs*) #f bc '())])
347 (set-prelex-operand! (car lhs*) b)
348 (cons b (make-bindings (cdr lhs*) (cdr rhs*) bc (+ i 1))))]))
349 (define (complex? x)
350 (or (binding-complex x)
351 (prelex-source-assigned? (binding-lhs x))))
352 (define (insert-order-edges b*)
353 (define (mark pb b*)
354 (unless (null? b*)
355 (let ([b (car b*)])
356 (if (complex? b)
357 (let ([free* (binding-free* b)])
358 (unless (memq pb free*)
359 (set-binding-free*! b (cons pb free*)))
360 (mark b (cdr b*)))
361 (mark pb (cdr b*))))))
362 (unless (null? b*)
363 (let ([b (car b*)])
364 (if (complex? b)
365 (mark b (cdr b*))
366 (insert-order-edges (cdr b*))))))
367 (let ([b* (make-bindings lhs* rhs* bc 0)])
368 (for-each (lambda (b) (set-binding-rhs! b (E (binding-rhs b) b))) b*)
369 (for-each (lambda (x) (set-prelex-operand! x #f)) lhs*)
370 (let ([body (E body bc)])
371 (when ordered? (insert-order-edges b*))
372 (let ([scc* (get-sccs-in-order b* (map binding-free* b*) b*)])
373 (when (debug-scc)
374 (printf "SCCS:\n")
375 (for-each
376 (lambda (scc)
377 (printf " ~s\n"
378 (map unparse (map binding-lhs scc))))
379 scc*))
380 (gen-letrecs scc* ordered? body)))))
381 (define (sort-bindings ls)
382 (list-sort
383 (lambda (x y) (< (binding-serial x) (binding-serial y)))
384 ls))
385 (define-struct binding (serial lhs rhs complex prev free*))
386 (define (mark-complex bc)
387 (unless (binding-complex bc)
388 (set-binding-complex! bc #t)
389 (mark-complex (binding-prev bc))))
390 (define (mark-free var bc)
391 (let ([rb (prelex-operand var)])
392 (when rb
393 (let ([lb
394 (let ([pr (binding-prev rb)])
395 (let f ([bc bc])
396 (let ([bcp (binding-prev bc)])
397 (cond
398 [(eq? bcp pr) bc]
399 [else (f bcp)]))))])
400 (let ([free* (binding-free* lb)])
401 (unless (memq rb free*)
402 ;(printf "MARK FREE ~s in ~s\n"
403 ; (unparse (binding-lhs rb))
404 ; (unparse (binding-lhs lb)))
405 (set-binding-free*! lb (cons rb free*))))))))
406 (define (E* x* bc)
407 (map (lambda (x) (E x bc)) x*))
408 (define (L x bc)
409 (struct-case x
410 [(clambda g cls* cp free name)
411 (let ([bc (make-binding #f #f #f #t bc '())])
412 (make-clambda g
413 (map (lambda (x)
414 (struct-case x
415 [(clambda-case info body)
416 (make-clambda-case info (E body bc))]))
417 cls*)
418 cp free name))]))
419 (define (E x bc)
420 (struct-case x
421 [(constant) x]
422 [(prelex)
423 (assert (prelex-source-referenced? x))
424 (mark-free x bc)
425 (when (prelex-source-assigned? x)
426 (mark-complex bc))
427 x]
428 [(assign lhs rhs)
429 (assert (prelex-source-assigned? lhs))
430 ;(set-prelex-source-assigned?! lhs #t)
431 (mark-free lhs bc)
432 (mark-complex bc)
433 (make-assign lhs (E rhs bc))]
434 [(primref) x]
435 [(bind lhs* rhs* body)
436 (if (null? lhs*)
437 (E body bc)
438 (make-bind lhs* (E* rhs* bc) (E body bc)))]
439 [(recbind lhs* rhs* body)
440 (if (null? lhs*)
441 (E body bc)
442 (do-recbind lhs* rhs* body bc #f))]
443 [(rec*bind lhs* rhs* body)
444 (if (null? lhs*)
445 (E body bc)
446 (do-recbind lhs* rhs* body bc #t))]
447 [(conditional e0 e1 e2)
448 (make-conditional (E e0 bc) (E e1 bc) (E e2 bc))]
449 [(seq e0 e1) (make-seq (E e0 bc) (E e1 bc))]
450 [(clambda g cls* cp free name)
451 (L x bc)]
452 [(funcall rator rand*)
453 (mark-complex bc)
454 (make-funcall (E rator bc) (E* rand* bc))]
455 [(mvcall p c)
456 (mark-complex bc)
457 (make-mvcall (E p bc) (E c bc))]
458 [(forcall rator rand*)
459 (mark-complex bc)
460 (make-forcall rator (E* rand* bc))]
461 [else (error who "invalid expression" (unparse x))]))
462 ;(printf "===========================================\n")
463 (let ([x (E x (make-binding #f #f #f #t #t '()))])
464 ;(pretty-print (unparse x))
465 x))
466
467
468 (define debug-scc (make-parameter #f))
469
470 (define current-letrec-pass
471 (make-parameter 'scc
472 (lambda (x)
473 (if (memq x '(scc waddell basic))
474 x
475 (die 'current-letrec-pass "invalid" x)))))
476
477
478 (define (optimize-letrec x)
479 (case (current-letrec-pass)
480 [(scc) (optimize-letrec/scc x)]
481 [(waddell) (optimize-letrec/waddell x)]
482 [(basic) (optimize-letrec/basic x)]
483 [else (die 'optimize-letrec "invalid" (current-letrec-pass))]))
484 )
485
486
487
488