cse.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015 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. ;;; Common subexpression elimination for CPS.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps cse)
  22. #:use-module (ice-9 match)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (language cps)
  26. #:use-module (language cps utils)
  27. #:use-module (language cps effects-analysis)
  28. #:use-module (language cps intmap)
  29. #:use-module (language cps intset)
  30. #:export (eliminate-common-subexpressions))
  31. (define (intset-pop set)
  32. (match (intset-next set)
  33. (#f (values set #f))
  34. (i (values (intset-remove set i) i))))
  35. (define-syntax-rule (make-worklist-folder* seed ...)
  36. (lambda (f worklist seed ...)
  37. (let lp ((worklist worklist) (seed seed) ...)
  38. (call-with-values (lambda () (intset-pop worklist))
  39. (lambda (worklist i)
  40. (if i
  41. (call-with-values (lambda () (f i seed ...))
  42. (lambda (i* seed ...)
  43. (let add ((i* i*) (worklist worklist))
  44. (match i*
  45. (() (lp worklist seed ...))
  46. ((i . i*) (add i* (intset-add worklist i)))))))
  47. (values seed ...)))))))
  48. (define worklist-fold*
  49. (case-lambda
  50. ((f worklist seed)
  51. ((make-worklist-folder* seed) f worklist seed))))
  52. (define (compute-available-expressions conts kfun effects)
  53. "Compute and return a map of LABEL->ANCESTOR..., where ANCESTOR... is
  54. an intset containing ancestor labels whose value is available at LABEL."
  55. (define (propagate avail succ out)
  56. (let* ((in (intmap-ref avail succ (lambda (_) #f)))
  57. (in* (if in (intset-intersect in out) out)))
  58. (if (eq? in in*)
  59. (values '() avail)
  60. (values (list succ)
  61. (intmap-add avail succ in* (lambda (old new) new))))))
  62. (define (clobber label in)
  63. (let ((fx (intmap-ref effects label)))
  64. (cond
  65. ((not (causes-effect? fx &write))
  66. ;; Fast-path if this expression clobbers nothing.
  67. in)
  68. (else
  69. ;; Kill clobbered expressions. FIXME: there is no need to check
  70. ;; on any label before than the last dominating label that
  71. ;; clobbered everything. Another way to speed things up would
  72. ;; be to compute a clobber set per-effect, which we could
  73. ;; subtract from "in".
  74. (let lp ((label 0) (in in))
  75. (cond
  76. ((intset-next in label)
  77. => (lambda (label)
  78. (if (effect-clobbers? fx (intmap-ref effects label))
  79. (lp (1+ label) (intset-remove in label))
  80. (lp (1+ label) in))))
  81. (else in)))))))
  82. (define (visit-cont label avail)
  83. (let* ((in (intmap-ref avail label))
  84. (out (intset-add (clobber label in) label)))
  85. (define (propagate0)
  86. (values '() avail))
  87. (define (propagate1 succ)
  88. (propagate avail succ out))
  89. (define (propagate2 succ0 succ1)
  90. (let*-values (((changed0 avail) (propagate avail succ0 out))
  91. ((changed1 avail) (propagate avail succ1 out)))
  92. (values (append changed0 changed1) avail)))
  93. (match (intmap-ref conts label)
  94. (($ $kargs names vars ($ $continue k src exp))
  95. (match exp
  96. (($ $branch kt) (propagate2 k kt))
  97. (($ $prompt escape? tag handler) (propagate2 k handler))
  98. (_ (propagate1 k))))
  99. (($ $kreceive arity k)
  100. (propagate1 k))
  101. (($ $kfun src meta self tail clause)
  102. (if clause
  103. (propagate1 clause)
  104. (propagate0)))
  105. (($ $kclause arity kbody kalt)
  106. (if kalt
  107. (propagate2 kbody kalt)
  108. (propagate1 kbody)))
  109. (($ $ktail) (propagate0)))))
  110. (worklist-fold* visit-cont
  111. (intset kfun)
  112. (intmap-add empty-intmap kfun empty-intset)))
  113. (define (compute-truthy-expressions conts kfun)
  114. "Compute a \"truth map\", indicating which expressions can be shown to
  115. be true and/or false at each label in the function starting at KFUN..
  116. Returns an intmap of intsets. The even elements of the intset indicate
  117. labels that may be true, and the odd ones indicate those that may be
  118. false. It could be that both true and false proofs are available."
  119. (define (true-idx label) (ash label 1))
  120. (define (false-idx label) (1+ (ash label 1)))
  121. (define (propagate boolv succ out)
  122. (let* ((in (intmap-ref boolv succ (lambda (_) #f)))
  123. (in* (if in (intset-intersect in out) out)))
  124. (if (eq? in in*)
  125. (values '() boolv)
  126. (values (list succ)
  127. (intmap-add boolv succ in* (lambda (old new) new))))))
  128. (define (visit-cont label boolv)
  129. (let ((in (intmap-ref boolv label)))
  130. (define (propagate0)
  131. (values '() boolv))
  132. (define (propagate1 succ)
  133. (propagate boolv succ in))
  134. (define (propagate2 succ0 succ1)
  135. (let*-values (((changed0 boolv) (propagate boolv succ0 in))
  136. ((changed1 boolv) (propagate boolv succ1 in)))
  137. (values (append changed0 changed1) boolv)))
  138. (define (propagate-branch succ0 succ1)
  139. (let*-values (((changed0 boolv)
  140. (propagate boolv succ0
  141. (intset-add in (false-idx label))))
  142. ((changed1 boolv)
  143. (propagate boolv succ1
  144. (intset-add in (true-idx label)))))
  145. (values (append changed0 changed1) boolv)))
  146. (match (intmap-ref conts label)
  147. (($ $kargs names vars ($ $continue k src exp))
  148. (match exp
  149. (($ $branch kt) (propagate-branch k kt))
  150. (($ $prompt escape? tag handler) (propagate2 k handler))
  151. (_ (propagate1 k))))
  152. (($ $kreceive arity k)
  153. (propagate1 k))
  154. (($ $kfun src meta self tail clause)
  155. (if clause
  156. (propagate1 clause)
  157. (propagate0)))
  158. (($ $kclause arity kbody kalt)
  159. (if kalt
  160. (propagate2 kbody kalt)
  161. (propagate1 kbody)))
  162. (($ $ktail) (propagate0)))))
  163. (intset-fold
  164. (lambda (kfun boolv)
  165. (worklist-fold* visit-cont
  166. (intset kfun)
  167. (intmap-add boolv kfun empty-intset)))
  168. (intmap-keys (compute-reachable-functions conts kfun))
  169. empty-intmap))
  170. (define (intset-map f set)
  171. (persistent-intmap
  172. (intset-fold (lambda (i out) (intmap-add! out i (f i)))
  173. set
  174. empty-intmap)))
  175. ;; Returns a map of label-idx -> (var-idx ...) indicating the variables
  176. ;; defined by a given labelled expression.
  177. (define (compute-defs conts kfun)
  178. (intset-map (lambda (label)
  179. (match (intmap-ref conts label)
  180. (($ $kfun src meta self tail clause)
  181. (list self))
  182. (($ $kclause arity body alt)
  183. (match (intmap-ref conts body)
  184. (($ $kargs names vars) vars)))
  185. (($ $kreceive arity kargs)
  186. (match (intmap-ref conts kargs)
  187. (($ $kargs names vars) vars)))
  188. (($ $ktail)
  189. '())
  190. (($ $kargs names vars ($ $continue k))
  191. (match (intmap-ref conts k)
  192. (($ $kargs names vars) vars)
  193. (_ #f)))))
  194. (compute-function-body conts kfun)))
  195. (define (compute-singly-referenced succs)
  196. (define (visit label succs single multiple)
  197. (intset-fold (lambda (label single multiple)
  198. (if (intset-ref single label)
  199. (values single (intset-add! multiple label))
  200. (values (intset-add! single label) multiple)))
  201. succs single multiple))
  202. (call-with-values (lambda ()
  203. (intmap-fold visit succs empty-intset empty-intset))
  204. (lambda (single multiple)
  205. (intset-subtract (persistent-intset single)
  206. (persistent-intset multiple)))))
  207. (define (compute-equivalent-subexpressions conts kfun effects)
  208. (define (visit-fun kfun equiv-labels var-substs)
  209. (let* ((succs (compute-successors conts kfun))
  210. (singly-referenced (compute-singly-referenced succs))
  211. (avail (compute-available-expressions conts kfun effects))
  212. (defs (compute-defs conts kfun))
  213. (equiv-set (make-hash-table)))
  214. (define (subst-var var-substs var)
  215. (intmap-ref var-substs var (lambda (var) var)))
  216. (define (subst-vars var-substs vars)
  217. (let lp ((vars vars))
  218. (match vars
  219. (() '())
  220. ((var . vars) (cons (subst-var var-substs var) (lp vars))))))
  221. (define (compute-exp-key var-substs exp)
  222. (match exp
  223. (($ $const val) (cons 'const val))
  224. (($ $prim name) (cons 'prim name))
  225. (($ $fun body) #f)
  226. (($ $rec names syms funs) #f)
  227. (($ $closure label nfree) #f)
  228. (($ $call proc args) #f)
  229. (($ $callk k proc args) #f)
  230. (($ $primcall name args)
  231. (cons* 'primcall name (subst-vars var-substs args)))
  232. (($ $branch _ ($ $primcall name args))
  233. (cons* 'primcall name (subst-vars var-substs args)))
  234. (($ $branch) #f)
  235. (($ $values args) #f)
  236. (($ $prompt escape? tag handler) #f)))
  237. (define (add-auxiliary-definitions! label var-substs exp-key)
  238. (define (subst var)
  239. (subst-var var-substs var))
  240. (let ((defs (intmap-ref defs label)))
  241. (define (add-def! aux-key var)
  242. (let ((equiv (hash-ref equiv-set aux-key '())))
  243. (hash-set! equiv-set aux-key
  244. (acons label (list var) equiv))))
  245. (match exp-key
  246. (('primcall 'box val)
  247. (match defs
  248. ((box)
  249. (add-def! `(primcall box-ref ,(subst box)) val))))
  250. (('primcall 'box-set! box val)
  251. (add-def! `(primcall box-ref ,box) val))
  252. (('primcall 'cons car cdr)
  253. (match defs
  254. ((pair)
  255. (add-def! `(primcall car ,(subst pair)) car)
  256. (add-def! `(primcall cdr ,(subst pair)) cdr))))
  257. (('primcall 'set-car! pair car)
  258. (add-def! `(primcall car ,pair) car))
  259. (('primcall 'set-cdr! pair cdr)
  260. (add-def! `(primcall cdr ,pair) cdr))
  261. (('primcall (or 'make-vector 'make-vector/immediate) len fill)
  262. (match defs
  263. ((vec)
  264. (add-def! `(primcall vector-length ,(subst vec)) len))))
  265. (('primcall 'vector-set! vec idx val)
  266. (add-def! `(primcall vector-ref ,vec ,idx) val))
  267. (('primcall 'vector-set!/immediate vec idx val)
  268. (add-def! `(primcall vector-ref/immediate ,vec ,idx) val))
  269. (('primcall (or 'allocate-struct 'allocate-struct/immediate)
  270. vtable size)
  271. (match defs
  272. ((struct)
  273. (add-def! `(primcall struct-vtable ,(subst struct))
  274. vtable))))
  275. (('primcall 'struct-set! struct n val)
  276. (add-def! `(primcall struct-ref ,struct ,n) val))
  277. (('primcall 'struct-set!/immediate struct n val)
  278. (add-def! `(primcall struct-ref/immediate ,struct ,n) val))
  279. (('primcall 'scm->f64 scm)
  280. (match defs
  281. ((f64)
  282. (add-def! `(primcall f64->scm ,f64) scm))))
  283. (('primcall 'f64->scm f64)
  284. (match defs
  285. ((scm)
  286. (add-def! `(primcall scm->f64 ,scm) f64))))
  287. (('primcall 'scm->u64 scm)
  288. (match defs
  289. ((u64)
  290. (add-def! `(primcall u64->scm ,u64) scm))))
  291. (('primcall 'u64->scm u64)
  292. (match defs
  293. ((scm)
  294. (add-def! `(primcall scm->u64 ,scm) u64))))
  295. (('primcall 'scm->s64 scm)
  296. (match defs
  297. ((s64)
  298. (add-def! `(primcall s64->scm ,s64) scm))))
  299. (('primcall 's64->scm s64)
  300. (match defs
  301. ((scm)
  302. (add-def! `(primcall scm->s64 ,scm) s64))))
  303. (_ #t))))
  304. (define (visit-label label equiv-labels var-substs)
  305. (match (intmap-ref conts label)
  306. (($ $kargs names vars ($ $continue k src exp))
  307. (let* ((exp-key (compute-exp-key var-substs exp))
  308. (equiv (hash-ref equiv-set exp-key '()))
  309. (fx (intmap-ref effects label))
  310. (avail (intmap-ref avail label)))
  311. (define (finish equiv-labels var-substs)
  312. ;; If this expression defines auxiliary definitions,
  313. ;; as `cons' does for the results of `car' and `cdr',
  314. ;; define those. Do so after finding equivalent
  315. ;; expressions, so that we can take advantage of
  316. ;; subst'd output vars.
  317. (add-auxiliary-definitions! label var-substs exp-key)
  318. (values equiv-labels var-substs))
  319. (let lp ((candidates equiv))
  320. (match candidates
  321. (()
  322. ;; No matching expressions. Add our expression
  323. ;; to the equivalence set, if appropriate. Note
  324. ;; that expressions that allocate a fresh object
  325. ;; or change the current fluid environment can't
  326. ;; be eliminated by CSE (though DCE might do it
  327. ;; if the value proves to be unused, in the
  328. ;; allocation case).
  329. (when (and exp-key
  330. (not (causes-effect? fx &allocation))
  331. (not (effect-clobbers? fx (&read-object &fluid))))
  332. (let ((defs (and (intset-ref singly-referenced k)
  333. (intmap-ref defs label))))
  334. (when defs
  335. (hash-set! equiv-set exp-key
  336. (acons label defs equiv)))))
  337. (finish equiv-labels var-substs))
  338. (((and head (candidate . vars)) . candidates)
  339. (cond
  340. ((not (intset-ref avail candidate))
  341. ;; This expression isn't available here; try
  342. ;; the next one.
  343. (lp candidates))
  344. (else
  345. ;; Yay, a match. Mark expression as equivalent. If
  346. ;; we provide the definitions for the successor, mark
  347. ;; the vars for substitution.
  348. (finish (intmap-add equiv-labels label head)
  349. (let ((defs (and (intset-ref singly-referenced k)
  350. (intmap-ref defs label))))
  351. (if defs
  352. (fold (lambda (def var var-substs)
  353. (intmap-add var-substs def var))
  354. var-substs defs vars)
  355. var-substs))))))))))
  356. (_ (values equiv-labels var-substs))))
  357. ;; Traverse the labels in fun in reverse post-order, which will
  358. ;; visit definitions before uses first.
  359. (fold2 visit-label
  360. (compute-reverse-post-order succs kfun)
  361. equiv-labels
  362. var-substs)))
  363. (intset-fold visit-fun
  364. (intmap-keys (compute-reachable-functions conts kfun))
  365. empty-intmap
  366. empty-intmap))
  367. (define (apply-cse conts equiv-labels var-substs truthy-labels)
  368. (define (true-idx idx) (ash idx 1))
  369. (define (false-idx idx) (1+ (ash idx 1)))
  370. (define (subst-var var)
  371. (intmap-ref var-substs var (lambda (var) var)))
  372. (define (visit-exp exp)
  373. (rewrite-exp exp
  374. ((or ($ $const) ($ $prim) ($ $fun) ($ $rec) ($ $closure)) ,exp)
  375. (($ $call proc args)
  376. ($call (subst-var proc) ,(map subst-var args)))
  377. (($ $callk k proc args)
  378. ($callk k (subst-var proc) ,(map subst-var args)))
  379. (($ $primcall name args)
  380. ($primcall name ,(map subst-var args)))
  381. (($ $branch k exp)
  382. ($branch k ,(visit-exp exp)))
  383. (($ $values args)
  384. ($values ,(map subst-var args)))
  385. (($ $prompt escape? tag handler)
  386. ($prompt escape? (subst-var tag) handler))))
  387. (intmap-map
  388. (lambda (label cont)
  389. (match cont
  390. (($ $kargs names vars ($ $continue k src exp))
  391. (build-cont
  392. ($kargs names vars
  393. ,(match (intmap-ref equiv-labels label (lambda (_) #f))
  394. ((equiv . vars)
  395. (match exp
  396. (($ $branch kt exp)
  397. (let* ((bool (intmap-ref truthy-labels label))
  398. (t (intset-ref bool (true-idx equiv)))
  399. (f (intset-ref bool (false-idx equiv))))
  400. (if (eqv? t f)
  401. (build-term
  402. ($continue k src
  403. ($branch kt ,(visit-exp exp))))
  404. (build-term
  405. ($continue (if t kt k) src ($values ()))))))
  406. (_
  407. ;; For better or for worse, we only replace primcalls
  408. ;; if they have an associated VM op, which allows
  409. ;; them to continue to $kargs and thus we know their
  410. ;; defs and can use a $values expression instead of a
  411. ;; values primcall.
  412. (build-term
  413. ($continue k src ($values vars))))))
  414. (#f
  415. (build-term
  416. ($continue k src ,(visit-exp exp))))))))
  417. (_ cont)))
  418. conts))
  419. (define (eliminate-common-subexpressions conts)
  420. (call-with-values
  421. (lambda ()
  422. (let ((effects (synthesize-definition-effects (compute-effects conts))))
  423. (compute-equivalent-subexpressions conts 0 effects)))
  424. (lambda (equiv-labels var-substs)
  425. (let ((truthy-labels (compute-truthy-expressions conts 0)))
  426. (apply-cse conts equiv-labels var-substs truthy-labels)))))