join.scm 8.5 KB

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