cse.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. ;;; 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 (compute-available-expressions succs kfun effects)
  32. "Compute and return a map of LABEL->ANCESTOR..., where ANCESTOR... is
  33. an intset containing ancestor labels whose value is available at LABEL."
  34. (let ((init (intmap-map (lambda (label succs) #f) succs))
  35. (kill (compute-clobber-map effects))
  36. (gen (intmap-map (lambda (label succs) (intset label)) succs))
  37. (subtract (lambda (in-1 kill-1)
  38. (if in-1
  39. (intset-subtract in-1 kill-1)
  40. empty-intset)))
  41. (add intset-union)
  42. (meet (lambda (in-1 in-1*)
  43. (if in-1
  44. (intset-intersect in-1 in-1*)
  45. in-1*))))
  46. (let ((in (intmap-replace init kfun empty-intset))
  47. (out init)
  48. (worklist (intset kfun)))
  49. (solve-flow-equations succs in out kill gen subtract add meet worklist))))
  50. (define (intset-pop set)
  51. (match (intset-next set)
  52. (#f (values set #f))
  53. (i (values (intset-remove set i) i))))
  54. (define-syntax-rule (make-worklist-folder* seed ...)
  55. (lambda (f worklist seed ...)
  56. (let lp ((worklist worklist) (seed seed) ...)
  57. (call-with-values (lambda () (intset-pop worklist))
  58. (lambda (worklist i)
  59. (if i
  60. (call-with-values (lambda () (f i seed ...))
  61. (lambda (i* seed ...)
  62. (let add ((i* i*) (worklist worklist))
  63. (match i*
  64. (() (lp worklist seed ...))
  65. ((i . i*) (add i* (intset-add worklist i)))))))
  66. (values seed ...)))))))
  67. (define worklist-fold*
  68. (case-lambda
  69. ((f worklist seed)
  70. ((make-worklist-folder* seed) f worklist seed))))
  71. (define (compute-truthy-expressions conts kfun)
  72. "Compute a \"truth map\", indicating which expressions can be shown to
  73. be true and/or false at each label in the function starting at KFUN..
  74. Returns an intmap of intsets. The even elements of the intset indicate
  75. labels that may be true, and the odd ones indicate those that may be
  76. false. It could be that both true and false proofs are available."
  77. (define (true-idx label) (ash label 1))
  78. (define (false-idx label) (1+ (ash label 1)))
  79. (define (propagate boolv succ out)
  80. (let* ((in (intmap-ref boolv succ (lambda (_) #f)))
  81. (in* (if in (intset-intersect in out) out)))
  82. (if (eq? in in*)
  83. (values '() boolv)
  84. (values (list succ)
  85. (intmap-add boolv succ in* (lambda (old new) new))))))
  86. (define (visit-cont label boolv)
  87. (let ((in (intmap-ref boolv label)))
  88. (define (propagate0)
  89. (values '() boolv))
  90. (define (propagate1 succ)
  91. (propagate boolv succ in))
  92. (define (propagate2 succ0 succ1)
  93. (let*-values (((changed0 boolv) (propagate boolv succ0 in))
  94. ((changed1 boolv) (propagate boolv succ1 in)))
  95. (values (append changed0 changed1) boolv)))
  96. (define (propagate-branch succ0 succ1)
  97. (let*-values (((changed0 boolv)
  98. (propagate boolv succ0
  99. (intset-add in (false-idx label))))
  100. ((changed1 boolv)
  101. (propagate boolv succ1
  102. (intset-add in (true-idx label)))))
  103. (values (append changed0 changed1) boolv)))
  104. (match (intmap-ref conts label)
  105. (($ $kargs names vars term)
  106. (match term
  107. (($ $continue k) (propagate1 k))
  108. (($ $branch kf kt) (propagate-branch kf kt))
  109. (($ $prompt k kh) (propagate2 k kh))
  110. (($ $throw) (propagate0))))
  111. (($ $kreceive arity k)
  112. (propagate1 k))
  113. (($ $kfun src meta self tail clause)
  114. (if clause
  115. (propagate1 clause)
  116. (propagate0)))
  117. (($ $kclause arity kbody kalt)
  118. (if kalt
  119. (propagate2 kbody kalt)
  120. (propagate1 kbody)))
  121. (($ $ktail) (propagate0)))))
  122. (intset-fold
  123. (lambda (kfun boolv)
  124. (worklist-fold* visit-cont
  125. (intset kfun)
  126. (intmap-add boolv kfun empty-intset)))
  127. (intmap-keys (compute-reachable-functions conts kfun))
  128. empty-intmap))
  129. (define (intset-map f set)
  130. (persistent-intmap
  131. (intset-fold (lambda (i out) (intmap-add! out i (f i)))
  132. set
  133. empty-intmap)))
  134. ;; Returns a map of label-idx -> (var-idx ...) indicating the variables
  135. ;; defined by a given labelled expression.
  136. (define (compute-defs conts kfun)
  137. (intset-map (lambda (label)
  138. (match (intmap-ref conts label)
  139. (($ $kfun src meta self tail clause)
  140. (if self (list self) '()))
  141. (($ $kclause arity body alt)
  142. (match (intmap-ref conts body)
  143. (($ $kargs names vars) vars)))
  144. (($ $kreceive arity kargs)
  145. (match (intmap-ref conts kargs)
  146. (($ $kargs names vars) vars)))
  147. (($ $ktail)
  148. '())
  149. (($ $kargs names vars term)
  150. (match term
  151. (($ $continue k)
  152. (match (intmap-ref conts k)
  153. (($ $kargs names vars) vars)
  154. (_ #f)))
  155. (($ $branch)
  156. '())
  157. ((or ($ $prompt) ($ $throw))
  158. #f)))))
  159. (compute-function-body conts kfun)))
  160. (define (compute-singly-referenced succs)
  161. (define (visit label succs single multiple)
  162. (intset-fold (lambda (label single multiple)
  163. (if (intset-ref single label)
  164. (values single (intset-add! multiple label))
  165. (values (intset-add! single label) multiple)))
  166. succs single multiple))
  167. (call-with-values (lambda ()
  168. (intmap-fold visit succs empty-intset empty-intset))
  169. (lambda (single multiple)
  170. (intset-subtract (persistent-intset single)
  171. (persistent-intset multiple)))))
  172. (define (intmap-select map set)
  173. (intset->intmap (lambda (label) (intmap-ref map label)) set))
  174. (define (compute-equivalent-subexpressions conts kfun)
  175. (define (visit-fun kfun body equiv-labels var-substs)
  176. (let* ((conts (intmap-select conts body))
  177. (effects (synthesize-definition-effects (compute-effects conts)))
  178. (succs (compute-successors conts kfun))
  179. (singly-referenced (compute-singly-referenced succs))
  180. (avail (compute-available-expressions succs kfun effects))
  181. (defs (compute-defs conts kfun))
  182. (equiv-set (make-hash-table)))
  183. (define (subst-var var-substs var)
  184. (intmap-ref var-substs var (lambda (var) var)))
  185. (define (subst-vars var-substs vars)
  186. (let lp ((vars vars))
  187. (match vars
  188. (() '())
  189. ((var . vars) (cons (subst-var var-substs var) (lp vars))))))
  190. (define (compute-term-key var-substs term)
  191. (match term
  192. (($ $continue k src exp)
  193. (match exp
  194. (($ $const val) (cons 'const val))
  195. (($ $prim name) (cons 'prim name))
  196. (($ $fun body) #f)
  197. (($ $rec names syms funs) #f)
  198. (($ $const-fun label) #f)
  199. (($ $code label) (cons 'code label))
  200. (($ $call proc args) #f)
  201. (($ $callk k proc args) #f)
  202. (($ $primcall name param args)
  203. (cons* name param (subst-vars var-substs args)))
  204. (($ $values args) #f)))
  205. (($ $branch kf kt src op param args)
  206. (cons* op param (subst-vars var-substs args)))
  207. ((or ($ $prompt) ($ $throw)) #f)))
  208. (define (add-auxiliary-definitions! label defs var-substs term-key)
  209. (let ((defs (and defs (subst-vars var-substs defs))))
  210. (define (add-def! aux-key var)
  211. (let ((equiv (hash-ref equiv-set aux-key '())))
  212. (hash-set! equiv-set aux-key
  213. (acons label (list var) equiv))))
  214. (define-syntax add-definitions
  215. (syntax-rules (<-)
  216. ((add-definitions)
  217. #f)
  218. ((add-definitions
  219. ((def <- op arg ...) (aux <- op* arg* ...) ...)
  220. . clauses)
  221. (match term-key
  222. (('op arg ...)
  223. (match defs
  224. (#f
  225. ;; If the successor is a control-flow join, don't
  226. ;; pretend to know the values of its defs.
  227. #f)
  228. ((def) (add-def! (list 'op* arg* ...) aux) ...)))
  229. (_ (add-definitions . clauses))))
  230. ((add-definitions
  231. ((op arg ...) (aux <- op* arg* ...) ...)
  232. . clauses)
  233. (match term-key
  234. (('op arg ...)
  235. (add-def! (list 'op* arg* ...) aux) ...)
  236. (_ (add-definitions . clauses))))))
  237. (add-definitions
  238. ((scm-set! p s i x) (x <- scm-ref p s i))
  239. ((scm-set!/tag p s x) (x <- scm-ref/tag p s))
  240. ((scm-set!/immediate p s x) (x <- scm-ref/immediate p s))
  241. ((word-set! p s i x) (x <- word-ref p s i))
  242. ((word-set!/immediate p s x) (x <- word-ref/immediate p s))
  243. ((pointer-set!/immediate p s x) (x <- pointer-ref/immediate p s))
  244. ((u <- scm->f64 #f s) (s <- f64->scm #f u))
  245. ((s <- f64->scm #f u) (u <- scm->f64 #f s))
  246. ((u <- scm->u64 #f s) (s <- u64->scm #f u))
  247. ((s <- u64->scm #f u) (u <- scm->u64 #f s)
  248. (u <- scm->u64/truncate #f s))
  249. ((s <- u64->scm/unlikely #f u) (u <- scm->u64 #f s)
  250. (u <- scm->u64/truncate #f s))
  251. ((u <- scm->s64 #f s) (s <- s64->scm #f u))
  252. ((s <- s64->scm #f u) (u <- scm->s64 #f s))
  253. ((s <- s64->scm/unlikely #f u) (u <- scm->s64 #f s))
  254. ((u <- untag-fixnum #f s) (s <- s64->scm #f u)
  255. (s <- tag-fixnum #f u))
  256. ;; NB: These definitions rely on U having top 2 bits equal to
  257. ;; 3rd (sign) bit.
  258. ((s <- tag-fixnum #f u) (u <- scm->s64 #f s)
  259. (u <- untag-fixnum #f s))
  260. ((s <- u64->s64 #f u) (u <- s64->u64 #f s))
  261. ((u <- s64->u64 #f s) (s <- u64->s64 #f u))
  262. ((u <- untag-char #f s) (s <- tag-char #f u))
  263. ((s <- tag-char #f u) (u <- untag-char #f s)))))
  264. (define (visit-label label equiv-labels var-substs)
  265. (define (term-defs term)
  266. (match term
  267. (($ $continue k)
  268. (and (intset-ref singly-referenced k)
  269. (intmap-ref defs label)))
  270. (($ $branch) '())))
  271. (match (intmap-ref conts label)
  272. (($ $kargs names vars term)
  273. (match (compute-term-key var-substs term)
  274. (#f (values equiv-labels var-substs))
  275. (term-key
  276. (let* ((equiv (hash-ref equiv-set term-key '()))
  277. (fx (intmap-ref effects label))
  278. (avail (intmap-ref avail label)))
  279. (define (finish equiv-labels var-substs defs)
  280. ;; If this expression defines auxiliary definitions,
  281. ;; as `cons' does for the results of `car' and `cdr',
  282. ;; define those. Do so after finding equivalent
  283. ;; expressions, so that we can take advantage of
  284. ;; subst'd output vars.
  285. (add-auxiliary-definitions! label defs var-substs term-key)
  286. (values equiv-labels var-substs))
  287. (let lp ((candidates equiv))
  288. (match candidates
  289. (()
  290. ;; No matching expressions. Add our expression
  291. ;; to the equivalence set, if appropriate. Note
  292. ;; that expressions that allocate a fresh object
  293. ;; or change the current fluid environment can't
  294. ;; be eliminated by CSE (though DCE might do it
  295. ;; if the value proves to be unused, in the
  296. ;; allocation case).
  297. (let ((defs (term-defs term)))
  298. (when (and defs
  299. (not (causes-effect? fx &allocation))
  300. (not (effect-clobbers? fx (&read-object &fluid))))
  301. (hash-set! equiv-set term-key (acons label defs equiv)))
  302. (finish equiv-labels var-substs defs)))
  303. (((and head (candidate . vars)) . candidates)
  304. (cond
  305. ((not (intset-ref avail candidate))
  306. ;; This expression isn't available here; try
  307. ;; the next one.
  308. (lp candidates))
  309. (else
  310. ;; Yay, a match. Mark expression as equivalent. If
  311. ;; we provide the definitions for the successor, mark
  312. ;; the vars for substitution.
  313. (let ((defs (term-defs term)))
  314. (finish (intmap-add equiv-labels label head)
  315. (if defs
  316. (fold (lambda (def var var-substs)
  317. (intmap-add var-substs def var))
  318. var-substs defs vars)
  319. var-substs)
  320. defs)))))))))))
  321. (_ (values equiv-labels var-substs))))
  322. ;; Traverse the labels in fun in reverse post-order, which will
  323. ;; visit definitions before uses first.
  324. (fold2 visit-label
  325. (compute-reverse-post-order succs kfun)
  326. equiv-labels
  327. var-substs)))
  328. (intmap-fold visit-fun
  329. (compute-reachable-functions conts kfun)
  330. empty-intmap
  331. empty-intmap))
  332. (define (apply-cse conts equiv-labels var-substs truthy-labels)
  333. (define (true-idx idx) (ash idx 1))
  334. (define (false-idx idx) (1+ (ash idx 1)))
  335. (define (subst-var var)
  336. (intmap-ref var-substs var (lambda (var) var)))
  337. (define (visit-exp exp)
  338. (rewrite-exp exp
  339. ((or ($ $const) ($ $prim) ($ $fun) ($ $rec) ($ $const-fun) ($ $code)) ,exp)
  340. (($ $call proc args)
  341. ($call (subst-var proc) ,(map subst-var args)))
  342. (($ $callk k proc args)
  343. ($callk k (and proc (subst-var proc)) ,(map subst-var args)))
  344. (($ $primcall name param args)
  345. ($primcall name param ,(map subst-var args)))
  346. (($ $values args)
  347. ($values ,(map subst-var args)))))
  348. (define (visit-term label term)
  349. (match term
  350. (($ $branch kf kt src op param args)
  351. (match (intmap-ref equiv-labels label (lambda (_) #f))
  352. ((equiv) ; A branch defines no values.
  353. (let* ((bool (intmap-ref truthy-labels label))
  354. (t (intset-ref bool (true-idx equiv)))
  355. (f (intset-ref bool (false-idx equiv))))
  356. (if (eqv? t f)
  357. (build-term
  358. ($branch kf kt src op param ,(map subst-var args)))
  359. (build-term
  360. ($continue (if t kt kf) src ($values ()))))))
  361. (#f
  362. (build-term
  363. ($branch kf kt src op param ,(map subst-var args))))))
  364. (($ $continue k src exp)
  365. (match (intmap-ref equiv-labels label (lambda (_) #f))
  366. ((equiv . vars)
  367. (build-term ($continue k src ($values vars))))
  368. (#f
  369. (build-term
  370. ($continue k src ,(visit-exp exp))))))
  371. (($ $prompt k kh src escape? tag)
  372. (build-term
  373. ($prompt k kh src escape? (subst-var tag))))
  374. (($ $throw src op param args)
  375. (build-term
  376. ($throw src op param ,(map subst-var args))))))
  377. (intmap-map
  378. (lambda (label cont)
  379. (rewrite-cont cont
  380. (($ $kargs names vars term)
  381. ($kargs names vars ,(visit-term label term)))
  382. (_ ,cont)))
  383. conts))
  384. (define (eliminate-common-subexpressions conts)
  385. (call-with-values (lambda () (compute-equivalent-subexpressions conts 0))
  386. (lambda (equiv-labels var-substs)
  387. (let ((truthy-labels (compute-truthy-expressions conts 0)))
  388. (apply-cse conts equiv-labels var-substs truthy-labels)))))