join.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey
  3. ; Call JOIN-SUBSTITUTE on all variable/value pairs.
  4. (define (substitute-join-arguments lambda-proc call)
  5. (let ((vec (call-args call))
  6. (vars (lambda-variables lambda-proc)))
  7. (do ((vars vars (cdr vars))
  8. (i 1 (+ i 1))
  9. (c? #f (or (join-substitute (car vars) (vector-ref vec i))
  10. c?)))
  11. ((null? vars) c?))))
  12. ; Does VAL take only one argument and is that argument passed to $TEST?
  13. ; Is VAR applied to constants?
  14. ; Then two possiblities are checked for:
  15. ; Does the tree rooted at the least-common-ancestor of VAR's references
  16. ; contain no side-effects and necessarily passed control to VAR?
  17. ; or
  18. ; Does the join point contain no side-effects above the test?
  19. ;
  20. ; If so, make the transformation described below.
  21. (define (join-substitute var val)
  22. (let ((ref (and (lambda-node? val)
  23. (simple-test-procedure val))))
  24. (and ref
  25. (applied-to-useful-value? var ref)
  26. (let ((lca (least-common-ancestor (variable-refs var))))
  27. (cond ((or (suitable-join-conditional? lca var)
  28. (suitable-join-point? val (node-parent ref)))
  29. (really-join-substitute var val lca (node-parent ref))
  30. #t)
  31. (else #f))))))
  32. ; Check that VAL (a lambda-node) takes one argument, is jumped to, tests its
  33. ; argument, and that all references to the argument are at or below the test.
  34. (define (simple-test-procedure val)
  35. (let ((vars (lambda-variables val)))
  36. (if (or (null? vars)
  37. (not (null? (cdr vars)))
  38. (not (car vars))
  39. (not (calls-known? val))
  40. (neq? 'jump (lambda-type val)))
  41. #f
  42. (let* ((var (car vars))
  43. (ref (any simple-cond-ref (variable-refs var))))
  44. (if (and ref (all-refs-below? var (node-parent ref)))
  45. ref
  46. #f)))))
  47. (define (simple-cond-ref ref)
  48. (if (primop-conditional? (call-primop (node-parent ref)))
  49. ref
  50. #f))
  51. (define (all-refs-below? var node)
  52. (set-node-flag! node #t)
  53. (set-node-flag! (variable-binder var) #t)
  54. (let ((res (every? (lambda (r)
  55. (eq? node (marked-ancestor r)))
  56. (variable-refs var))))
  57. (set-node-flag! node #f)
  58. (set-node-flag! (variable-binder var) #f)
  59. res))
  60. ; Is VAR applied to something that can be used to simplify the conditional?
  61. (define (applied-to-useful-value? var ref)
  62. (let ((call (node-parent ref))
  63. (index (node-index ref)))
  64. (any? (lambda (r)
  65. (simplify-conditional? call index (call-arg (node-parent r) 1)))
  66. (variable-refs var))))
  67. ; CALL is the least-common-ancestor of the references to VAR. Check that
  68. ; the tree rooted at CALL contains no side-effects and that the control flow
  69. ; necessarily passes to VAR. (Could check for undefined-effect here...)
  70. ; could do check that jumped-to proc if not VAR jumped to VAR eventually
  71. (define (suitable-join-conditional? call var)
  72. (let label ((call call))
  73. (cond ((call-side-effects? call)
  74. #f)
  75. ((= 0 (call-exits call))
  76. (and (eq? 'jump (primop-id (call-primop call)))
  77. (eq? var (reference-variable (called-node call)))))
  78. (else
  79. (let loop ((i 0))
  80. (cond ((>= i (call-exits call))
  81. #t)
  82. ((not (label (lambda-body (call-arg call i))))
  83. #f)
  84. (else
  85. (loop (+ i 1)))))))))
  86. ; #t if CALL performs side-effects. The continuations to CALL are ignored.
  87. (define (call-side-effects? call)
  88. (or (primop-side-effects (call-primop call))
  89. (let loop ((i (call-exits call)))
  90. (cond ((>= i (call-arg-count call))
  91. #f)
  92. ((side-effects? (call-arg call i))
  93. #t)
  94. (else
  95. (loop (+ i 1)))))))
  96. ; The alternative to the above test: does the join point contain no side-effects
  97. ; above the test?
  98. (define (suitable-join-point? join test)
  99. (let label ((call (lambda-body join)))
  100. (cond ((eq? call test)
  101. #t)
  102. ((call-side-effects? call)
  103. #f)
  104. (else
  105. (let loop ((i 0))
  106. (cond ((>= i (call-exits call))
  107. #t)
  108. ((not (label (lambda-body (call-arg call i))))
  109. #f)
  110. (else
  111. (loop (+ i 1)))))))))
  112. ; (let ((j (lambda (v) ; VAR VAL
  113. ; .a.
  114. ; ($test c1 c2 ... v ...) ; TEST
  115. ; .b.)))
  116. ; .c.
  117. ; (... (j x) ...) ; CALL
  118. ; .d.)
  119. ; ==>
  120. ; .c.
  121. ; (.a.
  122. ; (let ((v1 (lambda (x) c1[x/v]))
  123. ; (v2 (lambda (x) c2[x/v])))
  124. ; (... ((lambda (v)
  125. ; ($test (lambda () (v1 v)) (lambda () (v2 v)) ... v ...))
  126. ; x)
  127. ; ...))
  128. ; .b.)
  129. ; .d.
  130. ;
  131. ; CALL is the least common ancestor of the references to VAR, which is bound to
  132. ; VAL, a procedure. TEST is a conditional that tests the argument passed to
  133. ; VAL.
  134. ;
  135. ; (lambda-body VAL) is moved to where CALL is.
  136. ; In the body of VAL, TEST is replaced by a LET that binds TEST's continuations
  137. ; and then executes CALL. TEST's continuations are replaced by calls to
  138. ; the variables bound by the LET.
  139. ; Finally, references to VAR are replaced by a procedure whose body is TEST,
  140. ; which is the point of the whole exercise.
  141. (define (really-join-substitute var val call test)
  142. (let ((value-var (car (lambda-variables val))))
  143. (receive (cont-call conts)
  144. (move-continuations test call value-var)
  145. (let ((test-parent (node-parent test))
  146. (val-parent (node-parent val))
  147. (val-index (node-index val)))
  148. (parameterize-continuations conts value-var)
  149. (detach-body test)
  150. (move-body cont-call
  151. (lambda (cont-call)
  152. (attach-body test-parent cont-call)
  153. (detach-body (lambda-body val))))
  154. (attach-body val test)
  155. (mark-changed (call-arg test 1)) ; marks test as changed.
  156. (mark-changed cont-call)
  157. (substitute var val #t)
  158. (attach val-parent val-index (make-literal-node #f #f))
  159. (values)))))
  160. ; Move the continuations of CALL to a LET call just above TO. Returns a list
  161. ; of the variables now bound to the continuations and the continuations
  162. ; themselves.
  163. (define (move-continuations call to arg-var)
  164. (let ((count (call-exits call)))
  165. (let loop ((i (- count 1)) (vs '()) (es '()))
  166. (cond ((< i 0)
  167. (let ((new-call (make-call-node (get-primop (enum primop let))
  168. (+ count 1)
  169. 1))
  170. (new-proc (make-lambda-node 'j 'cont vs)))
  171. (attach-call-args new-call (cons new-proc es))
  172. (insert-body new-call new-proc (node-parent to))
  173. (values new-call es)))
  174. (else
  175. (let ((var (make-variable 'e (node-type (call-arg call i))))
  176. (cont (detach (call-arg call i))))
  177. (let-nodes ((new-cont () c1)
  178. (c1 (jump 0 (* var) (* arg-var))))
  179. (attach call i new-cont))
  180. (change-lambda-type cont 'jump)
  181. (loop (- i 1) (cons var vs) (cons cont es))))))))
  182. ; Add a new variable to each of CONTS and substitute a reference to the correct
  183. ; variable for each reference to VAR within CONTS.
  184. (define (parameterize-continuations conts var)
  185. (for-each (lambda (n)
  186. (let ((var (copy-variable var)))
  187. (set-lambda-variables! n (cons var (lambda-variables n)))
  188. (set-variable-binder! var n)
  189. (set-node-flag! n #t)))
  190. conts)
  191. (let ((backstop (variable-binder var)))
  192. (set-node-flag! backstop #t)
  193. (walk-refs-safely
  194. (lambda (n)
  195. (let ((cont (marked-ancestor n)))
  196. (if (not (eq? cont backstop))
  197. (replace n (make-reference-node (car (lambda-variables cont)))))))
  198. var)
  199. (set-node-flag! backstop #f)
  200. (for-each (lambda (n) (set-node-flag! n #f)) conts)
  201. (values)))