jump.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Mike Sperber, Eric Knauel
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/front/jump.scm
  8. ;;;
  9. ;;; Code to turn PROC lambdas into JUMP lambdas.
  10. ;;;
  11. ;;; FIND-JUMP-PROCS returns two lists. The first contains lists of the form
  12. ;;; (<continuation> <proc-lambda> ...)
  13. ;;; indicating that these lambda nodes are all
  14. ;;; called with <continuation> as their only continuation. The second list
  15. ;;; is of procedures that are called only by each other. The procedures in
  16. ;;; the second list are deleted. Those in the first list are converted to
  17. ;;; JUMP lambdas
  18. ;;;
  19. ;;; INTEGRATE-JUMP-PROCS! returns #T if any change is made to the program.
  20. (define-module (ps-compiler front jump)
  21. #:use-module (srfi srfi-9)
  22. #:use-module (prescheme scheme48)
  23. #:use-module (prescheme record-discloser)
  24. #:use-module (ps-compiler node arch)
  25. #:use-module (ps-compiler node node)
  26. #:use-module (ps-compiler node node-equal)
  27. #:use-module (ps-compiler node node-letrec)
  28. #:use-module (ps-compiler node node-util)
  29. #:use-module (ps-compiler node primop)
  30. #:use-module (ps-compiler param)
  31. #:use-module (ps-compiler util ssa)
  32. #:use-module (ps-compiler util util)
  33. #:export (integrate-jump-procs!
  34. find-jump-procs
  35. procs->jumps))
  36. (define (integrate-jump-procs! node)
  37. (receive (hits useless)
  38. (find-jump-procs (filter proc-lambda?
  39. (make-lambda-list))
  40. find-calls)
  41. (remove-unused-procedures! useless)
  42. (let loop ((changed? #f))
  43. (receive (hits useless)
  44. (find-jump-procs (filter proc-lambda?
  45. (make-lambda-list))
  46. find-calls)
  47. (if (null? hits)
  48. changed?
  49. (let ((p (car hits)))
  50. (procs->jumps (cdr p)
  51. (map bound-to-variable (cdr p))
  52. (car p))
  53. (loop #t)))))))
  54. ;; We want to find subsets of ALL-PROCS such that all elements of a subset
  55. ;; are always called with the same continuation. (PROC->USES <proc>) returns
  56. ;; the references to <proc> that are calls, or #f if there are references that
  57. ;; are not calls.
  58. ;;
  59. ;; We proceed as follows:
  60. ;; 1. Partition the procs depending on whether all their calls are known or not.
  61. ;; 2. Build a call graph:
  62. ;; Nodes represent either procedures or continuations. If there is a
  63. ;; tail-recursive call to procedure B in procedure A, then there is an
  64. ;; edge from A to B. For continuation C such that there is a call in
  65. ;; procedure A to procedure B with that continuation, there are edges
  66. ;; from A to C and from C to B.
  67. ;; In other words, it is a call graph where the edges that represent
  68. ;; non-tail-recursive calls are replaced by two edges, with a node for
  69. ;; the continuation in between.
  70. ;; There is a special root node (representing `outside'), that has
  71. ;; edges to the nodes representing procedures whose call sites have not
  72. ;; been identified.
  73. ;; 3. Determine the dominance frontiers in the graph.
  74. ;; 4. Find the nodes in the graph that are reachable from more than one
  75. ;; continuation (the joins).
  76. ;; 5. Starting from each node that represents a continuation (the joins,
  77. ;; procs whose calls aren't known, and the continuations themselves),
  78. ;; find the set of nodes reachable from that node without going through
  79. ;; some other continuation node.
  80. (define (find-jump-procs all-procs proc->uses)
  81. (for-each (lambda (l)
  82. (set-lambda-block! l (make-node l #f)))
  83. all-procs)
  84. (receive (known unknown)
  85. (partition-list calls-known? all-procs)
  86. (let ((root (make-node #f #f))
  87. (conts-cell (list '()))
  88. (known-blocks (map lambda-block known))
  89. (procs-cell (list (map lambda-block unknown))))
  90. (note-calls! known conts-cell procs-cell proc->uses)
  91. (let ((unknown-blocks (car procs-cell))
  92. (conts (car conts-cell)))
  93. (set-node-successors! root unknown-blocks)
  94. (graph->ssa-graph! root node-successors node-temp set-node-temp!)
  95. (let ((joins (find-joins (append conts unknown-blocks) node-temp)))
  96. (for-each (lambda (n)
  97. (set-node-join?! n #t))
  98. joins)
  99. (let* ((mergable (filter-map find-mergable
  100. (append joins unknown-blocks conts)))
  101. (useless (filter (lambda (p)
  102. (not (or (node-join? (lambda-block p))
  103. (node-merged? (lambda-block p)))))
  104. known)))
  105. (for-each (lambda (p)
  106. (set-lambda-block! p #f))
  107. all-procs)
  108. (values mergable useless)))))))
  109. ;; Make a call graph with extra nodes inserted for continuations:
  110. ;;
  111. ;; If F calls G tail-recursively, add an edge F->G
  112. ;; If F calls G ... with continuation K, add a node K and edges F->K, K->G ...
  113. ;;
  114. ;; Then FIND-JOINS will return a list of the nodes that are passed two or
  115. ;; more distinct continuations. The rest can be merged with their callers.
  116. ;;
  117. ;; Need a root node, so make one that points to all procs with unknown calls.
  118. (define-record-type :node
  119. (really-make-node proc cont successors join? merged?)
  120. node?
  121. (proc node-proc) ;; lambda node (or #f for continuation holders)
  122. (cont node-cont) ;; lambda node (or #f for procs)
  123. (successors node-successors set-node-successors!)
  124. (temp node-temp set-node-temp!)
  125. (join? node-join? set-node-join?!)
  126. (merged? node-merged? set-node-merged?!))
  127. (define (make-node proc cont)
  128. (really-make-node proc cont '() #f #f))
  129. (define-record-discloser :node
  130. (lambda (node)
  131. (list 'node (node-proc node) (node-cont node))))
  132. (define (add-child! parent child)
  133. (if (not (memq? child (node-successors parent)))
  134. (set-node-successors! parent
  135. (cons child
  136. (node-successors parent)))))
  137. ;; Walk KNOWN-PROCS adding edges to the call graph.
  138. (define (note-calls! known-procs conts-cell procs-cell proc->uses)
  139. (for-each (lambda (proc)
  140. (for-each (lambda (ref)
  141. (note-call! (lambda-block proc)
  142. ref
  143. conts-cell procs-cell))
  144. (proc->uses proc)))
  145. known-procs))
  146. ;; Add an edge from the node containing REF to PROC-NODE. Tail calls add an
  147. ;; edge directly from the calling node, non-tail calls add an edge from the
  148. ;; successor to the calling node that represents the call's continuation.
  149. (define (note-call! proc-node ref conts-cell procs-cell)
  150. (let ((caller (get-lambda-block (containing-procedure ref) procs-cell)))
  151. (add-child! (if (calls-this-primop? (node-parent ref) 'tail-call)
  152. caller
  153. (get-cont-block caller
  154. (call-arg (node-parent ref) 0)
  155. conts-cell))
  156. proc-node)))
  157. ;; Get the block for lambda-node PROC, making a new one if necessary.
  158. (define (get-lambda-block proc procs-cell)
  159. (let ((block (lambda-block proc)))
  160. (if (node? block)
  161. block
  162. (let ((new (make-node proc #f)))
  163. (set-lambda-block! proc new)
  164. (set-car! procs-cell (cons new (car procs-cell)))
  165. new))))
  166. ;; Get the successor to CALLER containing CONT, making it if necessary.
  167. (define (get-cont-block caller cont conts-cell)
  168. (or (any (lambda (node)
  169. (and (node-cont node)
  170. (node-equal? cont (node-cont node))))
  171. (node-successors caller))
  172. (let ((cont-node (make-node #f cont)))
  173. (set-car! conts-cell (cons cont-node (car conts-cell)))
  174. (add-child! caller cont-node)
  175. cont-node)))
  176. ;;----------------
  177. (define (find-mergable node)
  178. (let ((mergable (really-find-mergable node)))
  179. (if (null? mergable)
  180. #f
  181. (cons (or (node-cont node)
  182. (car (variable-refs
  183. (car (lambda-variables (node-proc node))))))
  184. mergable))))
  185. (define (really-find-mergable node)
  186. (let recur ((nodes (node-successors node)) (res '()))
  187. (if (null? nodes)
  188. res
  189. (recur (cdr nodes)
  190. (let ((node (car nodes)))
  191. (cond ((or (node-join? node) ;; gets two or more continuations
  192. (node-merged? node) ;; already merged
  193. (node-cont node)) ;; different continuation
  194. res)
  195. ;; ((node-cont node) ;; not a lambda
  196. ;; (recur (node-successors node) res))
  197. (else
  198. (set-node-merged?! node #t)
  199. (recur (node-successors node)
  200. (cons (node-proc node) res)))))))))
  201. ;;----------------
  202. ;; Part 2. PROCS is a list of procedures that are only called by each other;
  203. ;; with no entry point they are useless and can be removed.
  204. (define (remove-unused-procedures! procs)
  205. (for-each (lambda (proc)
  206. (let ((var (bound-to-variable proc)))
  207. (if (not var)
  208. (bug "known procedure has no variable ~S" proc))
  209. (format #t "Removing unused procedure: ~S_~S~%"
  210. (variable-name var) (variable-id var))
  211. (let ((parent (node-parent proc)))
  212. (mark-changed proc)
  213. (detach-bound-value var proc)
  214. (erase proc))))
  215. procs))
  216. ;;----------------
  217. ;; Part 3. Turn JUMP-PROCS from procs to jumps. CONT is the continuation they
  218. ;; all receive, and is also turned into a jump.
  219. ;; This creates a LETREC to bind all CONT and any of JUMP-PROCS that are
  220. ;; passed CONT directly and are bound above the LCA of all calls to JUMP-PROCS
  221. ;; that use CONT. Then every jump-proc is changed from a proc lambda to a
  222. ;; jump lambda and has its continuation removed. Returns are replaced with
  223. ;; jumps to CONT. If CONT is not a variable some protocol adjustment may be
  224. ;; required.
  225. (define (procs->jumps jump-procs vars cont)
  226. (receive (called-vars called-procs lca)
  227. (find-cont-uses cont vars jump-procs)
  228. (let ((proc (containing-procedure cont))
  229. (lca (if (call-node? lca) lca (node-parent lca)))
  230. (cvar (if (lambda-node? cont)
  231. (make-variable 'w (node-type cont))
  232. #f)))
  233. (receive (called-vars called-procs)
  234. (bound-above? lca called-vars called-procs)
  235. (receive (called-vars called-procs)
  236. (filter-ancestors called-vars called-procs)
  237. (for-each detach-bound-value called-vars called-procs)
  238. (cond ((lambda-node? cont)
  239. (determine-continuation-protocol cont jump-procs)
  240. (let ((cont-copy (copy-node-tree cont)))
  241. (change-lambda-type cont-copy 'jump)
  242. (put-in-letrec (cons cvar
  243. called-vars)
  244. (cons cont-copy
  245. called-procs)
  246. lca)))
  247. (else
  248. (put-in-letrec called-vars called-procs lca))))
  249. (for-each proc-calls->jumps jump-procs)
  250. (for-each (lambda (p)
  251. (let* ((v (car (lambda-variables p)))
  252. (refs (variable-refs v)))
  253. (set-variable-refs! v '())
  254. (for-each (lambda (r)
  255. (if (lambda-node? cont)
  256. (return->jump (node-parent r) cvar)
  257. (replace r (make-reference-node
  258. (car (lambda-variables proc))))))
  259. refs)
  260. (remove-variable p v)))
  261. jump-procs)
  262. (values)))))
  263. ;; Returns those of VARS and VALS where there is a call to the variable that
  264. ;; passes CONT as a continuation, or where the variable is not bound. The
  265. ;; third value returned is the least-common-ancestor of all calls to VARS
  266. ;; that use CONT.
  267. ;;
  268. ;; Why exclude uncalled variables just because they are bound?
  269. (define (find-cont-uses cont vars vals)
  270. (let loop ((vars vars) (vals vals) (r-vars '()) (r-vals '()) (uses '()))
  271. (if (null? vars)
  272. (values r-vars
  273. r-vals
  274. (least-common-ancestor uses))
  275. (let ref-loop ((refs (variable-refs (car vars))) (my-uses uses))
  276. (cond ((not (null? refs))
  277. (ref-loop (cdr refs)
  278. (if (node-equal? cont
  279. (call-arg (node-parent (car refs))
  280. 0))
  281. (cons (car refs) my-uses)
  282. my-uses)))
  283. ;; Why was this here? It breaks for some examples.
  284. ;; ((and (variable-binder (car vars))
  285. ;; (eq? my-uses uses))
  286. ;; (loop (cdr vars) (cdr vals) r-vars r-vals uses))
  287. (else
  288. (loop (cdr vars) (cdr vals)
  289. (cons (car vars) r-vars)
  290. (cons (car vals) r-vals)
  291. my-uses)))))))
  292. ;; Return the list of VARS and VALS where the variable is either global
  293. ;; or bound above CALL.
  294. (define (bound-above? call vars vals)
  295. (set-node-flag! call #t)
  296. (let loop ((vars vars) (vals vals) (r-vars '()) (r-vals '()))
  297. (cond ((null? vars)
  298. (set-node-flag! call #f)
  299. (values r-vars r-vals))
  300. ((and (variable-binder (car vars))
  301. (marked-ancestor (variable-binder (car vars))))
  302. (loop (cdr vars) (cdr vals) r-vars r-vals))
  303. (else
  304. (loop (cdr vars) (cdr vals)
  305. (cons (car vars) r-vars)
  306. (cons (car vals) r-vals))))))
  307. ;; Filter the list of VARS and VALS so that none of the nodes
  308. ;; contained in any of the other is included.
  309. ;; If we didn't do this, we might hoist them and mess with the scoping.
  310. (define (filter-ancestors all-vars all-vals)
  311. (let loop ((vars all-vars) (vals all-vals) (r-vars '()) (r-vals '()))
  312. (cond
  313. ((null? vars)
  314. (values r-vars r-vals))
  315. ((any (lambda (other-val)
  316. (and (not (eq? (car vals) other-val))
  317. (node-ancestor? other-val (car vals))))
  318. all-vals)
  319. (loop (cdr vars) (cdr vals) r-vars r-vals))
  320. (else
  321. (loop (cdr vars) (cdr vals)
  322. (cons (car vars) r-vars)
  323. (cons (car vals) r-vals))))))
  324. (define (detach-bound-value var node)
  325. (if (variable-binder var)
  326. (let ((binder (variable-binder var))
  327. (parent (node-parent node))
  328. (index (node-index node)))
  329. (set-lambda-variables! binder (delq! var (lambda-variables binder)))
  330. (detach node)
  331. (remove-call-arg parent index))))
  332. ;; Turn all calls to PROC into jumps.
  333. (define (proc-calls->jumps proc)
  334. (for-each (lambda (n)
  335. (call->jump (node-parent n)))
  336. (find-calls proc))
  337. (change-lambda-type proc 'jump))
  338. ;; Change a call to a jump by changing the primop and removing the continuation.
  339. (define (call->jump call)
  340. (case (primop-id (call-primop call))
  341. ((call tail-call)
  342. (set-call-primop! call (get-primop (enum primop-enum jump)))
  343. (remove-call-arg call 0))
  344. (else
  345. (bug "odd call primop ~S" (call-primop call)))))
  346. ;; Change a return to a jump. VAR is a variable bound to JUMP, the lambda
  347. ;; being jumped to.
  348. (define (return->jump call var)
  349. (case (primop-id (call-primop call))
  350. ((return)
  351. (set-call-primop! call (get-primop (enum primop-enum jump)))
  352. (replace (call-arg call 0) (make-reference-node var)))
  353. (else
  354. (bug "odd return primop ~S" (call-primop call)))))