rotate-loops.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013-2019 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Commentary:
  17. ;;;
  18. ;;; Rotate loops so that they end with conditional jumps, if possible.
  19. ;;; The result goes from:
  20. ;;;
  21. ;;; loop:
  22. ;;; if x < 5 goto done;
  23. ;;; x = x + 1;
  24. ;;; goto loop;
  25. ;;; done:
  26. ;;;
  27. ;;; if x < 5 goto done;
  28. ;;; loop:
  29. ;;; x = x + 1;
  30. ;;; if x < 5 goto done;
  31. ;;; done:
  32. ;;;
  33. ;;; It's more code but there are fewer instructions in the body. Note
  34. ;;; that this transformation isn't guaranteed to produce a loop that
  35. ;;; ends in a conditional jump, because usually your loop has some state
  36. ;;; that it's shuffling around and for now that shuffle is reified with
  37. ;;; the test, not the loop header. Alack.
  38. ;;;
  39. ;;; Implementation-wise, things are complicated by values flowing out of
  40. ;;; the loop. We actually perform this transformation only on loops
  41. ;;; that have a single exit continuation, so that we define values
  42. ;;; flowing out in one place. We rename the loop variables in two
  43. ;;; places internally: one for the peeled comparison, and another for
  44. ;;; the body. The loop variables' original names are then bound in a
  45. ;;; join continuation for use by successor code.
  46. ;;;
  47. ;;; Code:
  48. (define-module (language cps rotate-loops)
  49. #:use-module (ice-9 match)
  50. #:use-module (language cps)
  51. #:use-module (language cps utils)
  52. #:use-module (language cps intmap)
  53. #:use-module (language cps intset)
  54. #:use-module (language cps with-cps)
  55. #:export (rotate-loops))
  56. (define (loop-successors scc succs)
  57. (intset-subtract (intset-fold (lambda (label exits)
  58. (intset-union exits (intmap-ref succs label)))
  59. scc empty-intset)
  60. scc))
  61. (define (find-exits scc succs)
  62. (intset-fold (lambda (label exits)
  63. (if (eq? empty-intset
  64. (intset-subtract (intmap-ref succs label) scc))
  65. exits
  66. (intset-add exits label)))
  67. scc
  68. empty-intset))
  69. (define (find-entry scc preds)
  70. (trivial-intset (find-exits scc preds)))
  71. (define (rotate-loop cps entry-label body-labels succs preds back-edges)
  72. (match (intmap-ref cps entry-label)
  73. ((and entry-cont
  74. ($ $kargs entry-names entry-vars
  75. ($ $branch entry-kf entry-kt entry-src
  76. entry-op entry-param entry-args)))
  77. (let* ((exit-if-true? (intset-ref body-labels entry-kf))
  78. (loop-exits (find-exits body-labels succs))
  79. (exit (if exit-if-true? entry-kt entry-kf))
  80. (new-entry-label (if exit-if-true? entry-kf entry-kt))
  81. (join-label (fresh-label))
  82. (join-cont (build-cont
  83. ($kargs entry-names entry-vars
  84. ($continue exit entry-src ($values ())))))
  85. (cps (intmap-add! cps join-label join-cont)))
  86. (define (make-fresh-vars)
  87. (map (lambda (_) (fresh-var)) entry-vars))
  88. (define (make-trampoline k src values)
  89. (build-cont ($kargs () () ($continue k src ($values values)))))
  90. (define (rename-var var replacements)
  91. "If VAR refers to a member of ENTRY-VARS, replace with a
  92. corresponding var from REPLACEMENTS; otherwise return VAR."
  93. (match (list-index entry-vars var)
  94. (#f var)
  95. (idx (list-ref replacements idx))))
  96. (define (rename-vars vars replacements)
  97. (map (lambda (var) (rename-var var replacements)) vars))
  98. (define (rename-term term replacements)
  99. (define (rename arg) (rename-var arg replacements))
  100. (define (rename* arg) (rename-vars arg replacements))
  101. (rewrite-term term
  102. (($ $continue k src exp)
  103. ($continue k src
  104. ,(rewrite-exp exp
  105. ((or ($ $const) ($ $prim) ($ $const-fun) ($ $code)) ,exp)
  106. (($ $values args)
  107. ($values ,(rename* args)))
  108. (($ $call proc args)
  109. ($call (rename proc) ,(rename* args)))
  110. (($ $callk k proc args)
  111. ($callk k (and proc (rename proc)) ,(rename* args)))
  112. (($ $primcall name param args)
  113. ($primcall name param ,(rename* args))))))
  114. (($ $branch kf kt src op param args)
  115. ($branch kf kt src op param ,(rename* args)))
  116. (($ $prompt k kh src escape? tag)
  117. ($prompt k kh src escape? (rename tag)))
  118. (($ $throw src op param args)
  119. ($throw src op param ,(rename* args)))))
  120. (define (attach-trampoline cps label src names vars args)
  121. (with-cps cps
  122. (letk ktramp-out ,(make-trampoline join-label src args))
  123. (letk ktramp-in ,(make-trampoline new-entry-label src args))
  124. (setk label
  125. ($kargs names vars
  126. ($branch (if exit-if-true? ktramp-in ktramp-out)
  127. (if exit-if-true? ktramp-out ktramp-in)
  128. entry-src
  129. entry-op entry-param ,(rename-vars entry-args args))))))
  130. ;; Rewrite the targets of the entry branch to go to
  131. ;; trampolines. One will pass values out of the loop, and
  132. ;; one will pass values into the loop.
  133. (let* ((pre-header-vars (make-fresh-vars))
  134. (body-vars (make-fresh-vars))
  135. (cps (attach-trampoline cps entry-label entry-src
  136. entry-names pre-header-vars
  137. pre-header-vars))
  138. (new-entry-cont (build-cont
  139. ($kargs entry-names body-vars
  140. ,(match (intmap-ref cps new-entry-label)
  141. (($ $kargs () () term) term)))))
  142. (cps (intmap-replace! cps new-entry-label new-entry-cont)))
  143. (intset-fold
  144. (lambda (label cps)
  145. (cond
  146. ((intset-ref back-edges label)
  147. (match (intmap-ref cps label)
  148. (($ $kargs names vars term)
  149. (match (rename-term term body-vars)
  150. (($ $continue _ src ($ $values args))
  151. (attach-trampoline cps label src names vars args))
  152. (($ $continue _ src exp)
  153. (let* ((args (make-fresh-vars))
  154. (bind-label (fresh-label))
  155. (edge* (build-cont
  156. ($kargs names vars
  157. ($continue bind-label src ,exp))))
  158. (cps (intmap-replace! cps label edge*))
  159. ;; attach-trampoline uses setk.
  160. (cps (intmap-add! cps bind-label #f)))
  161. (attach-trampoline cps bind-label src
  162. entry-names args args)))))))
  163. ((intset-ref loop-exits label)
  164. (match (intmap-ref cps label)
  165. (($ $kargs names vars ($ $branch kf kt src op param args))
  166. (with-cps cps
  167. (letk ktramp-out ,(make-trampoline join-label src body-vars))
  168. (setk label
  169. ($kargs names vars
  170. ($branch (if (eqv? kf exit) ktramp-out kf)
  171. (if (eqv? kt exit) ktramp-out kt)
  172. src
  173. op param ,(rename-vars args body-vars))))))))
  174. (else
  175. (match (intmap-ref cps label)
  176. (($ $kargs names vars term)
  177. (with-cps cps
  178. (setk label ($kargs names vars
  179. ,(rename-term term body-vars)))))
  180. (($ $kreceive) cps)))))
  181. (intset-remove body-labels entry-label)
  182. cps))))))
  183. (define (rotate-loops-in-function kfun body cps)
  184. (define (can-rotate? edges)
  185. (intset-fold (lambda (label rotate?)
  186. (match (intmap-ref cps label)
  187. (($ $kreceive) #f)
  188. (($ $kargs _ _ ($ $branch)) #f)
  189. (($ $kargs _ _ ($ $continue)) rotate?)))
  190. edges #t))
  191. (let* ((succs (compute-successors cps kfun))
  192. (preds (invert-graph succs)))
  193. (intmap-fold
  194. (lambda (id scc cps)
  195. (cond
  196. ((trivial-intset scc) cps)
  197. ((find-entry scc preds)
  198. => (lambda (entry)
  199. (let ((back-edges (intset-intersect scc
  200. (intmap-ref preds entry))))
  201. (if (and (can-rotate? back-edges)
  202. (trivial-intset
  203. (intset-subtract (intmap-ref succs entry) scc))
  204. (trivial-intset (loop-successors scc succs))
  205. (match (intmap-ref cps entry)
  206. ;; Can't rotate $prompt out of loop header.
  207. (($ $kargs _ _ ($ $prompt)) #f)
  208. (_ #t)))
  209. ;; Loop header is an exit, and there is only one
  210. ;; exit continuation. Loop header isn't a prompt,
  211. ;; so it must be a conditional branch and only one
  212. ;; successor is an exit. The values flowing out of
  213. ;; the loop are the loop variables.
  214. (rotate-loop cps entry scc succs preds back-edges)
  215. cps))))
  216. (else cps)))
  217. (compute-strongly-connected-components succs kfun)
  218. cps)))
  219. (define (rotate-loops cps)
  220. (persistent-intmap
  221. (with-fresh-name-state cps
  222. (intmap-fold rotate-loops-in-function
  223. (compute-reachable-functions cps)
  224. cps))))