utils.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2013, 2014, 2015, 2017, 2018, 2019, 2020, 2021 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. ;;; Helper facilities for working with CPS.
  19. ;;;
  20. ;;; Code:
  21. (define-module (language cps utils)
  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 intset)
  27. #:use-module (language cps intmap)
  28. #:use-module (language cps graphs)
  29. #:export (;; Fresh names.
  30. label-counter var-counter
  31. fresh-label fresh-var
  32. with-fresh-name-state compute-max-label-and-var
  33. let-fresh
  34. ;; Graphs.
  35. compute-function-body
  36. compute-singly-referenced-labels
  37. compute-reachable-functions
  38. compute-successors
  39. compute-predecessors
  40. compute-idoms
  41. compute-dom-edges
  42. compute-defs-and-uses
  43. compute-var-representations)
  44. #:re-export (fold1 fold2
  45. trivial-intset
  46. intmap-map
  47. intmap-keys
  48. invert-bijection invert-partition
  49. intset->intmap
  50. intmap-select
  51. worklist-fold
  52. fixpoint
  53. ;; Flow analysis.
  54. invert-graph
  55. compute-reverse-post-order
  56. compute-strongly-connected-components
  57. compute-sorted-strongly-connected-components
  58. solve-flow-equations))
  59. (define label-counter (make-parameter #f))
  60. (define var-counter (make-parameter #f))
  61. (define (fresh-label)
  62. (let ((count (or (label-counter)
  63. (error "fresh-label outside with-fresh-name-state"))))
  64. (label-counter (1+ count))
  65. count))
  66. (define (fresh-var)
  67. (let ((count (or (var-counter)
  68. (error "fresh-var outside with-fresh-name-state"))))
  69. (var-counter (1+ count))
  70. count))
  71. (define-syntax-rule (let-fresh (label ...) (var ...) body ...)
  72. (let* ((label (fresh-label)) ...
  73. (var (fresh-var)) ...)
  74. body ...))
  75. (define-syntax-rule (with-fresh-name-state fun body ...)
  76. (call-with-values (lambda () (compute-max-label-and-var fun))
  77. (lambda (max-label max-var)
  78. (parameterize ((label-counter (1+ max-label))
  79. (var-counter (1+ max-var)))
  80. body ...))))
  81. (define (compute-max-label-and-var conts)
  82. (values (or (intmap-prev conts) -1)
  83. (intmap-fold (lambda (k cont max-var)
  84. (match cont
  85. (($ $kargs names syms body)
  86. (apply max max-var syms))
  87. (($ $kfun src meta (and self (not #f)))
  88. (max max-var self))
  89. (_ max-var)))
  90. conts
  91. -1)))
  92. (define (compute-function-body conts kfun)
  93. (persistent-intset
  94. (let visit-cont ((label kfun) (labels empty-intset))
  95. (cond
  96. ((intset-ref labels label) labels)
  97. (else
  98. (let ((labels (intset-add! labels label)))
  99. (match (intmap-ref conts label)
  100. (($ $kreceive arity k) (visit-cont k labels))
  101. (($ $kfun src meta self ktail kclause)
  102. (let ((labels (visit-cont ktail labels)))
  103. (if kclause
  104. (visit-cont kclause labels)
  105. labels)))
  106. (($ $ktail) labels)
  107. (($ $kclause arity kbody kalt)
  108. (if kalt
  109. (visit-cont kalt (visit-cont kbody labels))
  110. (visit-cont kbody labels)))
  111. (($ $kargs names syms term)
  112. (match term
  113. (($ $continue k)
  114. (visit-cont k labels))
  115. (($ $branch kf kt)
  116. (visit-cont kf (visit-cont kt labels)))
  117. (($ $switch kf kt*)
  118. (visit-cont kf (fold1 visit-cont kt* labels)))
  119. (($ $prompt k kh)
  120. (visit-cont k (visit-cont kh labels)))
  121. (($ $throw)
  122. labels))))))))))
  123. (define (compute-singly-referenced-labels conts)
  124. "Compute the set of labels in CONTS that have exactly one
  125. predecessor."
  126. (define (add-ref label cont single multiple)
  127. (define (ref k single multiple)
  128. (if (intset-ref single k)
  129. (values single (intset-add! multiple k))
  130. (values (intset-add! single k) multiple)))
  131. (define (ref0) (values single multiple))
  132. (define (ref1 k) (ref k single multiple))
  133. (define (ref2 k k*)
  134. (if k*
  135. (let-values (((single multiple) (ref k single multiple)))
  136. (ref k* single multiple))
  137. (ref1 k)))
  138. (match cont
  139. (($ $kreceive arity k) (ref1 k))
  140. (($ $kfun src meta self ktail kclause) (ref2 ktail kclause))
  141. (($ $ktail) (ref0))
  142. (($ $kclause arity kbody kalt) (ref2 kbody kalt))
  143. (($ $kargs names syms ($ $continue k)) (ref1 k))
  144. (($ $kargs names syms ($ $branch kf kt)) (ref2 kf kt))
  145. (($ $kargs names syms ($ $switch kf kt*))
  146. (fold2 ref (cons kf kt*) single multiple))
  147. (($ $kargs names syms ($ $prompt k kh)) (ref2 k kh))
  148. (($ $kargs names syms ($ $throw)) (ref0))))
  149. (let*-values (((single multiple) (values empty-intset empty-intset))
  150. ((single multiple) (intmap-fold add-ref conts single multiple)))
  151. (intset-subtract (persistent-intset single)
  152. (persistent-intset multiple))))
  153. (define* (compute-reachable-functions conts #:optional (kfun 0))
  154. "Compute a mapping LABEL->LABEL..., where each key is a reachable
  155. $kfun and each associated value is the body of the function, as an
  156. intset."
  157. (define (intset-cons i set) (intset-add set i))
  158. (define (visit-fun kfun body to-visit)
  159. (intset-fold
  160. (lambda (label to-visit)
  161. (define (return kfun*) (fold intset-cons to-visit kfun*))
  162. (define (return1 kfun) (intset-add to-visit kfun))
  163. (define (return0) to-visit)
  164. (match (intmap-ref conts label)
  165. (($ $kargs _ _ ($ $continue _ _ exp))
  166. (match exp
  167. (($ $fun label) (return1 label))
  168. (($ $rec _ _ (($ $fun labels) ...)) (return labels))
  169. (($ $const-fun label) (return1 label))
  170. (($ $code label) (return1 label))
  171. (($ $callk label) (return1 label))
  172. (_ (return0))))
  173. (_ (return0))))
  174. body
  175. to-visit))
  176. (let lp ((to-visit (intset kfun)) (visited empty-intmap))
  177. (let ((to-visit (intset-subtract to-visit (intmap-keys visited))))
  178. (if (eq? to-visit empty-intset)
  179. visited
  180. (call-with-values
  181. (lambda ()
  182. (intset-fold
  183. (lambda (kfun to-visit visited)
  184. (let ((body (compute-function-body conts kfun)))
  185. (values (visit-fun kfun body to-visit)
  186. (intmap-add visited kfun body))))
  187. to-visit
  188. empty-intset
  189. visited))
  190. lp)))))
  191. (define* (compute-successors conts #:optional (kfun (intmap-next conts)))
  192. (define (visit label succs)
  193. (let visit ((label kfun) (succs empty-intmap))
  194. (define (propagate0)
  195. (intmap-add! succs label empty-intset))
  196. (define (propagate1 succ)
  197. (visit succ (intmap-add! succs label (intset succ))))
  198. (define (propagate2 succ0 succ1)
  199. (let ((succs (intmap-add! succs label (intset succ0 succ1))))
  200. (visit succ1 (visit succ0 succs))))
  201. (define (propagate* k*)
  202. (define (list->intset ls)
  203. (fold1 (lambda (elt set) (intset-add set elt)) ls empty-intset))
  204. (fold1 visit k* (intmap-add! succs label (list->intset k*))))
  205. (if (intmap-ref succs label (lambda (_) #f))
  206. succs
  207. (match (intmap-ref conts label)
  208. (($ $kargs names vars term)
  209. (match term
  210. (($ $continue k) (propagate1 k))
  211. (($ $branch kf kt) (propagate2 kf kt))
  212. (($ $switch kf kt*) (propagate* (cons kf kt*)))
  213. (($ $prompt k kh) (propagate2 k kh))
  214. (($ $throw) (propagate0))))
  215. (($ $kreceive arity k)
  216. (propagate1 k))
  217. (($ $kfun src meta self tail clause)
  218. (if clause
  219. (propagate2 clause tail)
  220. (propagate1 tail)))
  221. (($ $kclause arity kbody kalt)
  222. (if kalt
  223. (propagate2 kbody kalt)
  224. (propagate1 kbody)))
  225. (($ $ktail) (propagate0))))))
  226. (persistent-intmap (visit kfun empty-intmap)))
  227. (define* (compute-predecessors conts kfun #:key
  228. (labels (compute-function-body conts kfun)))
  229. (define (meet cdr car)
  230. (cons car cdr))
  231. (define (add-preds label preds)
  232. (define (add-pred k preds)
  233. (intmap-add! preds k label meet))
  234. (match (intmap-ref conts label)
  235. (($ $kreceive arity k)
  236. (add-pred k preds))
  237. (($ $kfun src meta self ktail kclause)
  238. (add-pred ktail (if kclause (add-pred kclause preds) preds)))
  239. (($ $ktail)
  240. preds)
  241. (($ $kclause arity kbody kalt)
  242. (add-pred kbody (if kalt (add-pred kalt preds) preds)))
  243. (($ $kargs names syms term)
  244. (match term
  245. (($ $continue k) (add-pred k preds))
  246. (($ $branch kf kt) (add-pred kf (add-pred kt preds)))
  247. (($ $switch kf kt*) (fold1 add-pred (cons kf kt*) preds))
  248. (($ $prompt k kh) (add-pred k (add-pred kh preds)))
  249. (($ $throw) preds)))))
  250. (persistent-intmap
  251. (intset-fold add-preds labels
  252. (intset->intmap (lambda (label) '()) labels))))
  253. ;; Precondition: For each function in CONTS, the continuation names are
  254. ;; topologically sorted.
  255. (define (compute-idoms conts kfun)
  256. ;; This is the iterative O(n^2) fixpoint algorithm, originally from
  257. ;; Allen and Cocke ("Graph-theoretic constructs for program flow
  258. ;; analysis", 1972). See the discussion in Cooper, Harvey, and
  259. ;; Kennedy's "A Simple, Fast Dominance Algorithm", 2001.
  260. (let ((preds-map (compute-predecessors conts kfun)))
  261. (define (compute-idom idoms preds)
  262. (define (idom-ref label)
  263. (intmap-ref idoms label (lambda (_) #f)))
  264. (match preds
  265. (() -1)
  266. ((pred) pred) ; Shortcut.
  267. ((pred . preds)
  268. (define (common-idom d0 d1)
  269. ;; We exploit the fact that a reverse post-order is a
  270. ;; topological sort, and so the idom of a node is always
  271. ;; numerically less than the node itself.
  272. (let lp ((d0 d0) (d1 d1))
  273. (cond
  274. ;; d0 or d1 can be false on the first iteration.
  275. ((not d0) d1)
  276. ((not d1) d0)
  277. ((= d0 d1) d0)
  278. ((< d0 d1) (lp d0 (idom-ref d1)))
  279. (else (lp (idom-ref d0) d1)))))
  280. (fold1 common-idom preds pred))))
  281. (define (adjoin-idom label preds idoms)
  282. (let ((idom (compute-idom idoms preds)))
  283. ;; Don't use intmap-add! here.
  284. (intmap-add idoms label idom (lambda (old new) new))))
  285. (fixpoint (lambda (idoms)
  286. (intmap-fold adjoin-idom preds-map idoms))
  287. empty-intmap)))
  288. ;; Compute a vector containing, for each node, a list of the nodes that
  289. ;; it immediately dominates. These are the "D" edges in the DJ tree.
  290. (define (compute-dom-edges idoms)
  291. (define (snoc cdr car) (cons car cdr))
  292. (persistent-intmap
  293. (intmap-fold (lambda (label idom doms)
  294. (let ((doms (intmap-add! doms label '())))
  295. (cond
  296. ((< idom 0) doms) ;; No edge to entry.
  297. (else (intmap-add! doms idom label snoc)))))
  298. idoms
  299. empty-intmap)))
  300. (define (compute-defs-and-uses cps)
  301. "Return two LABEL->VAR... maps indicating values defined at and used
  302. by a label, respectively."
  303. (define (vars->intset vars)
  304. (fold (lambda (var set) (intset-add set var)) empty-intset vars))
  305. (define-syntax-rule (persistent-intmap2 exp)
  306. (call-with-values (lambda () exp)
  307. (lambda (a b)
  308. (values (persistent-intmap a) (persistent-intmap b)))))
  309. (persistent-intmap2
  310. (intmap-fold
  311. (lambda (label cont defs uses)
  312. (define (get-defs k)
  313. (match (intmap-ref cps k)
  314. (($ $kargs names vars) (vars->intset vars))
  315. (_ empty-intset)))
  316. (define (return d u)
  317. (values (intmap-add! defs label d)
  318. (intmap-add! uses label u)))
  319. (match cont
  320. (($ $kfun src meta self tail clause)
  321. (return (intset-union
  322. (if clause (get-defs clause) empty-intset)
  323. (if self (intset self) empty-intset))
  324. empty-intset))
  325. (($ $kargs _ _ ($ $continue k src exp))
  326. (match exp
  327. ((or ($ $const) ($ $const-fun) ($ $code))
  328. (return (get-defs k) empty-intset))
  329. (($ $call proc args)
  330. (return (get-defs k) (intset-add (vars->intset args) proc)))
  331. (($ $callk _ proc args)
  332. (let ((args (vars->intset args)))
  333. (return (get-defs k) (if proc (intset-add args proc) args))))
  334. (($ $primcall name param args)
  335. (return (get-defs k) (vars->intset args)))
  336. (($ $values args)
  337. (return (get-defs k) (vars->intset args)))))
  338. (($ $kargs _ _ ($ $branch kf kt src op param args))
  339. (return empty-intset (vars->intset args)))
  340. (($ $kargs _ _ ($ $switch kf kt* src arg))
  341. (return empty-intset (intset arg)))
  342. (($ $kargs _ _ ($ $prompt k kh src escape? tag))
  343. (return empty-intset (intset tag)))
  344. (($ $kargs _ _ ($ $throw src op param args))
  345. (return empty-intset (vars->intset args)))
  346. (($ $kclause arity body alt)
  347. (return (get-defs body) empty-intset))
  348. (($ $kreceive arity kargs)
  349. (return (get-defs kargs) empty-intset))
  350. (($ $ktail)
  351. (return empty-intset empty-intset))))
  352. cps
  353. empty-intmap
  354. empty-intmap)))
  355. (define (compute-var-representations cps)
  356. (define (get-defs k)
  357. (match (intmap-ref cps k)
  358. (($ $kargs names vars) vars)
  359. (_ '())))
  360. (intmap-fold
  361. (lambda (label cont representations)
  362. (match cont
  363. (($ $kargs _ _ ($ $continue k _ exp))
  364. (match (get-defs k)
  365. (() representations)
  366. ((var)
  367. (match exp
  368. (($ $values (arg))
  369. (intmap-add representations var
  370. (intmap-ref representations arg)))
  371. (($ $callk)
  372. (intmap-add representations var 'scm))
  373. (($ $primcall (or 'scm->f64 'load-f64 's64->f64
  374. 'f32-ref 'f64-ref
  375. 'fadd 'fsub 'fmul 'fdiv 'fsqrt 'fabs
  376. 'ffloor 'fceiling
  377. 'fsin 'fcos 'ftan 'fasin 'facos 'fatan 'fatan2))
  378. (intmap-add representations var 'f64))
  379. (($ $primcall (or 'scm->u64 'scm->u64/truncate 'load-u64
  380. 's64->u64
  381. 'assume-u64
  382. 'uadd 'usub 'umul
  383. 'ulogand 'ulogior 'ulogxor 'ulogsub 'ursh 'ulsh
  384. 'uadd/immediate 'usub/immediate 'umul/immediate
  385. 'ursh/immediate 'ulsh/immediate
  386. 'u8-ref 'u16-ref 'u32-ref 'u64-ref
  387. 'word-ref 'word-ref/immediate
  388. 'untag-char))
  389. (intmap-add representations var 'u64))
  390. (($ $primcall (or 'untag-fixnum
  391. 'assume-s64
  392. 'scm->s64 'load-s64 'u64->s64
  393. 'srsh 'srsh/immediate
  394. 's8-ref 's16-ref 's32-ref 's64-ref))
  395. (intmap-add representations var 's64))
  396. (($ $primcall (or 'pointer-ref/immediate
  397. 'tail-pointer-ref/immediate))
  398. (intmap-add representations var 'ptr))
  399. (($ $code)
  400. (intmap-add representations var 'u64))
  401. (_
  402. (intmap-add representations var 'scm))))
  403. (vars
  404. (match exp
  405. (($ $values args)
  406. (fold (lambda (arg var representations)
  407. (intmap-add representations var
  408. (intmap-ref representations arg)))
  409. representations args vars))
  410. (($ $callk)
  411. (fold1 (lambda (var representations)
  412. (intmap-add representations var 'scm))
  413. vars representations))))))
  414. (($ $kargs _ _ (or ($ $branch) ($ $switch) ($ $prompt) ($ $throw)))
  415. representations)
  416. (($ $kfun src meta self tail entry)
  417. (let* ((representations (if self
  418. (intmap-add representations self 'scm)
  419. representations))
  420. (defs (get-defs entry))
  421. (reprs (or (assq-ref meta 'arg-representations)
  422. (map (lambda (_) 'scm) defs))))
  423. (fold (lambda (var repr representations)
  424. (intmap-add representations var repr))
  425. representations defs reprs)))
  426. (($ $kclause arity body alt)
  427. (fold1 (lambda (var representations)
  428. (intmap-add representations var 'scm))
  429. (get-defs body) representations))
  430. (($ $kreceive arity kargs)
  431. (fold1 (lambda (var representations)
  432. (intmap-add representations var 'scm))
  433. (get-defs kargs) representations))
  434. (($ $ktail) representations)))
  435. cps
  436. empty-intmap))