peel-loops.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. ;;; Loop peeling "peels off" one iteration of a loop. When followed by
  19. ;;; common subexpression elimination, it has the effect of moving terms
  20. ;;; to the first peeled iteration, leaving the loop body with fewer
  21. ;;; terms.
  22. ;;;
  23. ;;; Loop peeling is complementary to loop-invariant code motion (LICM).
  24. ;;; LICM will hoist invariant terms that have no side effects, like
  25. ;;; $const, even if they are in branches that are not always taken.
  26. ;;; However LICM won't hoist expressions that might have side effects if
  27. ;;; it can't prove that they are reachable on every iteration. Peeling
  28. ;;; on the other hand arranges for the body to be dominated by one loop
  29. ;;; iteration, so any effect that is reachable on one full iteration can
  30. ;;; be hoisted and eliminated, which is a big boon when we consider
  31. ;;; &type-check effects. For example:
  32. ;;;
  33. ;;; x = cached-toplevel-box map
  34. ;;; y = box-ref x
  35. ;;; z = cached-toplevel-box foo
  36. ;;; w = box-ref z
  37. ;;; ...
  38. ;;;
  39. ;;; In this example, LICM could hoist X, possibly Y as well if it can
  40. ;;; prove that the body doesn't write to variables, but it won't hoist
  41. ;;; Z. In contrast, peeling + CSE will allow Z to be hoisted.
  42. ;;;
  43. ;;; Peeling does cause code growth. If this becomes a problem we will
  44. ;;; need to apply heuristics to limit its applicability.
  45. ;;;
  46. ;;; Implementation-wise, things are complicated by values flowing out of
  47. ;;; the loop. We actually perform this transformation only on loops
  48. ;;; that have a single exit continuation, so that we define values
  49. ;;; flowing out in one place. We rename the loop variables in two
  50. ;;; places internally: one for the peeled iteration, and another for
  51. ;;; the body. The loop variables' original names are then bound in a
  52. ;;; join continuation for use by successor code.
  53. ;;;
  54. ;;; Code:
  55. (define-module (language cps peel-loops)
  56. #:use-module (ice-9 match)
  57. #:use-module ((srfi srfi-1) #:select (fold))
  58. #:use-module (language cps)
  59. #:use-module (language cps utils)
  60. #:use-module (language cps intmap)
  61. #:use-module (language cps intset)
  62. #:export (peel-loops))
  63. (define (intset-map f set)
  64. (persistent-intmap
  65. (intset-fold (lambda (i out) (intmap-add! out i (f i))) set empty-intmap)))
  66. (define (loop-successors scc succs)
  67. (intset-subtract (intset-fold (lambda (label exits)
  68. (intset-union exits (intmap-ref succs label)))
  69. scc empty-intset)
  70. scc))
  71. (define (find-exits scc succs)
  72. (intset-fold (lambda (label exits)
  73. (if (eq? empty-intset
  74. (intset-subtract (intmap-ref succs label) scc))
  75. exits
  76. (intset-add exits label)))
  77. scc
  78. empty-intset))
  79. (define (find-entry scc preds)
  80. (trivial-intset (find-exits scc preds)))
  81. (define (list->intset vars)
  82. (persistent-intset
  83. (fold1 (lambda (var set) (intset-add! set var)) vars empty-intset)))
  84. (define (compute-bailouts cps labels)
  85. (intset-fold (lambda (label bailouts)
  86. (match (intmap-ref cps label)
  87. (($ $kargs () () ($ $throw))
  88. (intset-add bailouts label))
  89. (_ bailouts)))
  90. labels empty-intset))
  91. (define (compute-live-variables cps entry body succs)
  92. (let* ((succs (intset-map (lambda (label)
  93. (intset-intersect (intmap-ref succs label) body))
  94. body))
  95. (init (intset-map (lambda (label) #f) body))
  96. (kill (intset-map (lambda (label) #f) body))
  97. (gen (intset-map (lambda (label)
  98. (match (intmap-ref cps label)
  99. (($ $kargs names vars) (list->intset vars))
  100. (_ empty-intset)))
  101. body))
  102. (in (intmap-replace init entry (intmap-ref gen entry)))
  103. (out init))
  104. (define (subtract in kill) (or in empty-intset))
  105. (define (add in gen) (if in (intset-union in gen) gen))
  106. (define (meet in out) (if in (intset-intersect in out) out))
  107. (call-with-values (lambda ()
  108. (solve-flow-equations succs in out kill gen
  109. subtract add meet
  110. (intset entry)))
  111. (lambda (in out)
  112. out))))
  113. (define (compute-out-vars cps entry body succs exit)
  114. (let ((live (compute-live-variables cps entry body succs)))
  115. (intset-fold-right
  116. cons
  117. (intset-fold (lambda (label live-out)
  118. (if (intset-ref (intmap-ref succs label) exit)
  119. (if live-out
  120. (intset-intersect live-out (intmap-ref live label))
  121. (intmap-ref live label))
  122. live-out))
  123. body #f)
  124. '())))
  125. (define (rename-cont cont fresh-labels fresh-vars)
  126. (define (rename-label label)
  127. (intmap-ref fresh-labels label (lambda (label) label)))
  128. (define (rename-var var)
  129. (intmap-ref fresh-vars var (lambda (var) var)))
  130. (define (rename-exp exp)
  131. (rewrite-exp exp
  132. ((or ($ $const) ($ $prim) ($ $const-fun) ($ $code) ($ $rec ())) ,exp)
  133. (($ $values args)
  134. ($values ,(map rename-var args)))
  135. (($ $call proc args)
  136. ($call (rename-var proc) ,(map rename-var args)))
  137. (($ $callk k proc args)
  138. ($callk k (and proc (rename-var proc)) ,(map rename-var args)))
  139. (($ $primcall name param args)
  140. ($primcall name param ,(map rename-var args)))))
  141. (define (rename-term term)
  142. (rewrite-term term
  143. (($ $continue k src exp)
  144. ($continue (rename-label k) src ,(rename-exp exp)))
  145. (($ $branch kf kt src op param args)
  146. ($branch (rename-label kf) (rename-label kt) src
  147. op param ,(map rename-var args)))
  148. (($ $prompt k kh src escape? tag)
  149. ($prompt (rename-label k) (rename-label kh) src
  150. escape? (rename-var tag)))
  151. (($ $throw src op param args)
  152. ($throw src op param ,(map rename-var args)))))
  153. (rewrite-cont cont
  154. (($ $kargs names vars term)
  155. ($kargs names (map rename-var vars) ,(rename-term term)))
  156. (($ $kreceive ($ $arity req () rest) kargs)
  157. ($kreceive req rest (rename-label kargs)))))
  158. (define (add-renamed-bailout cps label new-label fresh-vars)
  159. ;; We could recognize longer bailout sequences here; for now just
  160. ;; single-term throws.
  161. (define (rename-var var)
  162. (intmap-ref fresh-vars var (lambda (var) var)))
  163. ;; FIXME: Perhaps avoid copying the bailout if it doesn't use any loop
  164. ;; var.
  165. (match (intmap-ref cps label)
  166. (($ $kargs () () ($ $throw src op param args))
  167. (intmap-add cps new-label
  168. (build-cont
  169. ($kargs () ()
  170. ($throw src op param ,(map rename-var args))))))))
  171. (define (compute-var-names conts)
  172. (persistent-intmap
  173. (intmap-fold (lambda (label cont out)
  174. (match cont
  175. (($ $kargs names vars)
  176. (fold (lambda (name var out)
  177. (intmap-add! out var name))
  178. out names vars))
  179. (_ out)))
  180. conts empty-intmap)))
  181. (define (peel-loop cps entry body-labels succs preds bailouts)
  182. (let* ((body-conts (intset-map (lambda (label) (intmap-ref cps label))
  183. body-labels))
  184. (var-names (compute-var-names body-conts))
  185. (loop-exits (loop-successors body-labels succs))
  186. (loop-bailouts (intset-intersect loop-exits bailouts))
  187. ;; All non-bailout loop exits branch to this label.
  188. (exit (trivial-intset (intset-subtract loop-exits loop-bailouts)))
  189. ;; The variables that flow out of the loop, as a list.
  190. (out-vars (compute-out-vars cps entry body-labels succs exit))
  191. (out-names (map (lambda (var) (intmap-ref var-names var)) out-vars))
  192. (join-label (fresh-label))
  193. (join-cont (build-cont
  194. ($kargs out-names out-vars
  195. ($continue exit #f ($values ())))))
  196. (trampoline-cont
  197. ;; A $values predecessor for the join, passing the out-vars
  198. ;; using their original names. These will get renamed in
  199. ;; both the peeled iteration and the body.
  200. (build-cont
  201. ($kargs () ()
  202. ($continue join-label #f ($values out-vars)))))
  203. (fresh-body-labels
  204. ;; Fresh labels for the body.
  205. (intset-map (lambda (old) (fresh-label)) body-labels))
  206. (fresh-body-vars
  207. ;; Fresh vars for the body.
  208. (intmap-map (lambda (var name) (fresh-var)) var-names))
  209. (fresh-body-bailout-labels
  210. ;; Fresh labels for bailouts from body.
  211. (intset-map (lambda (old) (fresh-label)) loop-bailouts))
  212. (fresh-body-entry
  213. ;; The name of the entry, but in the body.
  214. (intmap-ref fresh-body-labels entry))
  215. (fresh-peeled-vars
  216. ;; Fresh names for variables that flow out of the peeled iteration.
  217. (fold1 (lambda (var out) (intmap-add out var (fresh-var)))
  218. out-vars empty-intmap))
  219. (peeled-bailout-labels
  220. ;; Fresh labels for bailouts from peeled iteration.
  221. (intset-map (lambda (old) (fresh-label)) loop-bailouts))
  222. (peeled-trampoline-label
  223. ;; Label for trampoline to pass values out of the peeled
  224. ;; iteration.
  225. (fresh-label))
  226. (peeled-trampoline-cont
  227. ;; Trampoline for the peeled iteration, ready to adjoin to
  228. ;; CPS.
  229. (rename-cont trampoline-cont empty-intmap fresh-peeled-vars))
  230. (peeled-labels
  231. ;; Exit goes to trampoline, back edges to body.
  232. (intmap-add (intmap-add empty-intmap exit peeled-trampoline-label)
  233. entry fresh-body-entry))
  234. (peeled-iteration
  235. ;; The peeled iteration.
  236. (intmap-map (lambda (label cont)
  237. (rename-cont cont
  238. (intmap-union peeled-labels
  239. peeled-bailout-labels)
  240. fresh-peeled-vars))
  241. body-conts))
  242. (body-trampoline-label
  243. ;; Label for trampoline to pass values out of the body.
  244. (fresh-label))
  245. (body-trampoline-cont
  246. ;; Trampoline for the body, ready to adjoin to CPS.
  247. (rename-cont trampoline-cont empty-intmap fresh-body-vars))
  248. (fresh-body
  249. ;; The body, renamed.
  250. (let ((label-map (intmap-union
  251. (intmap-add fresh-body-labels
  252. exit body-trampoline-label)
  253. fresh-body-bailout-labels)))
  254. (persistent-intmap
  255. (intmap-fold
  256. (lambda (label new-label out)
  257. (intmap-add! out new-label
  258. (rename-cont (intmap-ref body-conts label)
  259. label-map fresh-body-vars)))
  260. fresh-body-labels empty-intmap)))))
  261. (let* ((cps (intmap-add! cps join-label join-cont))
  262. (cps (intmap-add! cps peeled-trampoline-label
  263. peeled-trampoline-cont))
  264. (cps (intmap-add! cps body-trampoline-label
  265. body-trampoline-cont))
  266. (cps (intmap-fold (lambda (label cont cps)
  267. (intmap-replace! cps label cont))
  268. peeled-iteration cps))
  269. (cps (intmap-fold
  270. (lambda (old-label new-label cps)
  271. (add-renamed-bailout cps old-label new-label
  272. fresh-peeled-vars))
  273. peeled-bailout-labels cps))
  274. (cps (intmap-fold (lambda (label cont cps)
  275. (intmap-add! cps label cont))
  276. fresh-body cps))
  277. (cps (intmap-fold
  278. (lambda (old-label new-label cps)
  279. (add-renamed-bailout cps old-label new-label
  280. fresh-body-vars))
  281. fresh-body-bailout-labels cps)))
  282. cps)))
  283. (define (peel-loops-in-function kfun body cps)
  284. (let* ((succs (compute-successors cps kfun))
  285. (bailouts (compute-bailouts cps body))
  286. (preds (invert-graph succs)))
  287. ;; We can peel if there is one non-bailout successor to the loop,
  288. ;; and if the loop has no nested functions. (Peeling a nested
  289. ;; function would cause exponential code growth.)
  290. (define (can-peel? body)
  291. (and (trivial-intset (intset-subtract (loop-successors body succs)
  292. bailouts))
  293. (intset-fold (lambda (label peel?)
  294. (match (intmap-ref cps label)
  295. (($ $kargs _ _ ($ $continue _ _ exp))
  296. (match exp
  297. (($ $fun) #f)
  298. (($ $rec (_ . _)) #f)
  299. (_ peel?)))
  300. (_ peel?)))
  301. body #t)))
  302. (intmap-fold
  303. (lambda (id scc cps)
  304. (cond
  305. ((trivial-intset scc) cps)
  306. ((find-entry scc preds)
  307. => (lambda (entry)
  308. (if (can-peel? scc)
  309. (peel-loop cps entry scc succs preds bailouts)
  310. cps)))
  311. (else cps)))
  312. (compute-strongly-connected-components succs kfun)
  313. cps)))
  314. (define (peel-loops cps)
  315. (persistent-intmap
  316. (with-fresh-name-state cps
  317. (intmap-fold peel-loops-in-function
  318. (compute-reachable-functions cps)
  319. cps))))