specialize-numbers.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 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. ;;; Some arithmetic operations have multiple implementations: one
  19. ;;; polymorphic implementation that works on all kinds of numbers, like
  20. ;;; `add', and one or more specialized variants for unboxed numbers of
  21. ;;; some kind, like `fadd'. If we can replace a polymorphic
  22. ;;; implementation with a monomorphic implementation, we should do so --
  23. ;;; it will speed up the runtime and avoid boxing numbers.
  24. ;;;
  25. ;;; A polymorphic operation can be specialized if its result is
  26. ;;; specialized. To specialize an operation, we manually unbox its
  27. ;;; arguments and box its return value, relying on CSE to remove boxes
  28. ;;; where possible.
  29. ;;;
  30. ;;; We also want to specialize phi variables. A phi variable is bound
  31. ;;; by a continuation with more than one predecessor. For example in
  32. ;;; this code:
  33. ;;;
  34. ;;; (+ 1.0 (if a 2.0 3.0))
  35. ;;;
  36. ;;; We want to specialize this code to:
  37. ;;;
  38. ;;; (f64->scm (fl+ (scm->f64 1.0) (if a (scm->f64 2.0) (scm->f64 3.0))))
  39. ;;;
  40. ;;; Hopefully later passes will remove the conversions. In any case,
  41. ;;; specialization will likely result in a lower heap-number allocation
  42. ;;; rate, and that cost is higher than the extra opcodes to do
  43. ;;; conversions. This transformation is especially important for loop
  44. ;;; variables.
  45. ;;;
  46. ;;; Code:
  47. (define-module (language cps specialize-numbers)
  48. #:use-module (ice-9 match)
  49. #:use-module (srfi srfi-1)
  50. #:use-module (language cps)
  51. #:use-module (language cps intmap)
  52. #:use-module (language cps intset)
  53. #:use-module (language cps renumber)
  54. #:use-module (language cps types)
  55. #:use-module (language cps utils)
  56. #:use-module (language cps with-cps)
  57. #:export (specialize-numbers))
  58. (define (specialize-f64-binop cps k src op a b)
  59. (let ((fop (match op
  60. ('add 'fadd)
  61. ('sub 'fsub)
  62. ('mul 'fmul)
  63. ('div 'fdiv))))
  64. (with-cps cps
  65. (letv f64-a f64-b result)
  66. (letk kbox ($kargs ('result) (result)
  67. ($continue k src
  68. ($primcall 'f64->scm (result)))))
  69. (letk kop ($kargs ('f64-b) (f64-b)
  70. ($continue kbox src
  71. ($primcall fop (f64-a f64-b)))))
  72. (letk kunbox-b ($kargs ('f64-a) (f64-a)
  73. ($continue kop src
  74. ($primcall 'scm->f64 (b)))))
  75. (build-term
  76. ($continue kunbox-b src
  77. ($primcall 'scm->f64 (a)))))))
  78. (define* (specialize-u64-binop cps k src op a b #:key
  79. (unbox-a 'scm->u64)
  80. (unbox-b 'scm->u64))
  81. (let ((uop (match op
  82. ('add 'uadd)
  83. ('sub 'usub)
  84. ('mul 'umul)
  85. ('logand 'ulogand)
  86. ('logior 'ulogior)
  87. ('logxor 'ulogxor)
  88. ('logsub 'ulogsub)
  89. ('rsh 'ursh)
  90. ('lsh 'ulsh))))
  91. (with-cps cps
  92. (letv u64-a u64-b result)
  93. (letk kbox ($kargs ('result) (result)
  94. ($continue k src
  95. ($primcall 'u64->scm (result)))))
  96. (letk kop ($kargs ('u64-b) (u64-b)
  97. ($continue kbox src
  98. ($primcall uop (u64-a u64-b)))))
  99. (letk kunbox-b ($kargs ('u64-a) (u64-a)
  100. ($continue kop src
  101. ($primcall unbox-b (b)))))
  102. (build-term
  103. ($continue kunbox-b src
  104. ($primcall unbox-a (a)))))))
  105. (define (truncate-u64 cps k src scm)
  106. (with-cps cps
  107. (letv u64)
  108. (letk kbox ($kargs ('u64) (u64)
  109. ($continue k src
  110. ($primcall 'u64->scm (u64)))))
  111. (build-term
  112. ($continue kbox src
  113. ($primcall 'scm->u64/truncate (scm))))))
  114. (define (specialize-u64-comparison cps kf kt src op a b)
  115. (let ((op (symbol-append 'u64- op)))
  116. (with-cps cps
  117. (letv u64-a u64-b)
  118. (letk kop ($kargs ('u64-b) (u64-b)
  119. ($continue kf src
  120. ($branch kt ($primcall op (u64-a u64-b))))))
  121. (letk kunbox-b ($kargs ('u64-a) (u64-a)
  122. ($continue kop src
  123. ($primcall 'scm->u64 (b)))))
  124. (build-term
  125. ($continue kunbox-b src
  126. ($primcall 'scm->u64 (a)))))))
  127. (define (specialize-u64-scm-comparison cps kf kt src op a-u64 b-scm)
  128. (let ((op (symbol-append 'u64- op '-scm)))
  129. (with-cps cps
  130. (letv u64)
  131. (letk kop ($kargs ('u64) (u64)
  132. ($continue kf src
  133. ($branch kt ($primcall op (u64 b-scm))))))
  134. (build-term
  135. ($continue kop src
  136. ($primcall 'scm->u64 (a-u64)))))))
  137. (define (sigbits-union x y)
  138. (and x y (logior x y)))
  139. (define (sigbits-intersect x y)
  140. (cond
  141. ((not x) y)
  142. ((not y) x)
  143. (else (logand x y))))
  144. (define (sigbits-intersect3 a b c)
  145. (sigbits-intersect a (sigbits-intersect b c)))
  146. (define (next-power-of-two n)
  147. (let lp ((out 1))
  148. (if (< n out)
  149. out
  150. (lp (ash out 1)))))
  151. (define (range->sigbits min max)
  152. (cond
  153. ((or (< min 0) (> max #xffffFFFFffffFFFF)) #f)
  154. ((eqv? min max) min)
  155. (else (1- (next-power-of-two max)))))
  156. (define (inferred-sigbits types label var)
  157. (call-with-values (lambda () (lookup-pre-type types label var))
  158. (lambda (type min max)
  159. (and (or (eqv? type &exact-integer) (eqv? type &u64))
  160. (range->sigbits min max)))))
  161. (define significant-bits-handlers (make-hash-table))
  162. (define-syntax-rule (define-significant-bits-handler
  163. ((primop label types out def ...) arg ...)
  164. body ...)
  165. (hashq-set! significant-bits-handlers 'primop
  166. (lambda (label types out args defs)
  167. (match args ((arg ...) (match defs ((def ...) body ...)))))))
  168. (define-significant-bits-handler ((logand label types out res) a b)
  169. (let ((sigbits (sigbits-intersect3 (inferred-sigbits types label a)
  170. (inferred-sigbits types label b)
  171. (intmap-ref out res (lambda (_) 0)))))
  172. (intmap-add (intmap-add out a sigbits sigbits-union)
  173. b sigbits sigbits-union)))
  174. (define (significant-bits-handler primop)
  175. (hashq-ref significant-bits-handlers primop))
  176. (define (compute-significant-bits cps types kfun)
  177. "Given the locally inferred types @var{types}, compute a map of VAR ->
  178. BITS indicating the significant bits needed for a variable. BITS may be
  179. #f to indicate all bits, or a non-negative integer indicating a bitmask."
  180. (let ((preds (invert-graph (compute-successors cps kfun))))
  181. (let lp ((worklist (intmap-keys preds)) (out empty-intmap))
  182. (match (intset-prev worklist)
  183. (#f out)
  184. (label
  185. (let ((worklist (intset-remove worklist label)))
  186. (define (continue out*)
  187. (if (eq? out out*)
  188. (lp worklist out)
  189. (lp (intset-union worklist (intmap-ref preds label)) out*)))
  190. (define (add-def out var)
  191. (intmap-add out var 0 sigbits-union))
  192. (define (add-defs out vars)
  193. (match vars
  194. (() out)
  195. ((var . vars) (add-defs (add-def out var) vars))))
  196. (define (add-unknown-use out var)
  197. (intmap-add out var (inferred-sigbits types label var)
  198. sigbits-union))
  199. (define (add-unknown-uses out vars)
  200. (match vars
  201. (() out)
  202. ((var . vars)
  203. (add-unknown-uses (add-unknown-use out var) vars))))
  204. (continue
  205. (match (intmap-ref cps label)
  206. (($ $kfun src meta self)
  207. (add-def out self))
  208. (($ $kargs names vars ($ $continue k src exp))
  209. (let ((out (add-defs out vars)))
  210. (match exp
  211. ((or ($ $const) ($ $prim) ($ $fun) ($ $closure) ($ $rec))
  212. ;; No uses, so no info added to sigbits.
  213. out)
  214. (($ $values args)
  215. (match (intmap-ref cps k)
  216. (($ $kargs _ vars)
  217. (fold (lambda (arg var out)
  218. (intmap-add out arg (intmap-ref out var
  219. (lambda (_) 0))
  220. sigbits-union))
  221. out args vars))
  222. (($ $ktail)
  223. (add-unknown-uses out args))))
  224. (($ $call proc args)
  225. (add-unknown-use (add-unknown-uses out args) proc))
  226. (($ $callk label proc args)
  227. (add-unknown-use (add-unknown-uses out args) proc))
  228. (($ $branch kt ($ $values (arg)))
  229. (add-unknown-use out arg))
  230. (($ $branch kt ($ $primcall name args))
  231. (add-unknown-uses out args))
  232. (($ $primcall name args)
  233. (let ((h (significant-bits-handler name)))
  234. (if h
  235. (match (intmap-ref cps k)
  236. (($ $kargs _ defs)
  237. (h label types out args defs)))
  238. (add-unknown-uses out args))))
  239. (($ $prompt escape? tag handler)
  240. (add-unknown-use out tag)))))
  241. (_ out)))))))))
  242. (define (specialize-operations cps)
  243. (define (visit-cont label cont cps types sigbits)
  244. (define (operand-in-range? var &type &min &max)
  245. (call-with-values (lambda ()
  246. (lookup-pre-type types label var))
  247. (lambda (type min max)
  248. (and (eqv? type &type) (<= &min min max &max)))))
  249. (define (u64-operand? var)
  250. (operand-in-range? var &exact-integer 0 #xffffffffffffffff))
  251. (define (all-u64-bits-set? var)
  252. (operand-in-range? var &exact-integer
  253. #xffffffffffffffff
  254. #xffffffffffffffff))
  255. (define (only-u64-bits-used? var)
  256. (let ((bits (intmap-ref sigbits var)))
  257. (and bits (= bits (logand bits #xffffFFFFffffFFFF)))))
  258. (define (u64-result? result)
  259. (or (only-u64-bits-used? result)
  260. (call-with-values
  261. (lambda ()
  262. (lookup-post-type types label result 0))
  263. (lambda (type min max)
  264. (and (eqv? type &exact-integer)
  265. (<= 0 min max #xffffffffffffffff))))))
  266. (match cont
  267. (($ $kfun)
  268. (let ((types (infer-types cps label)))
  269. (values cps types (compute-significant-bits cps types label))))
  270. (($ $kargs names vars
  271. ($ $continue k src
  272. ($ $primcall (and op (or 'add 'sub 'mul 'div)) (a b))))
  273. (match (intmap-ref cps k)
  274. (($ $kargs (_) (result))
  275. (call-with-values (lambda ()
  276. (lookup-post-type types label result 0))
  277. (lambda (type min max)
  278. (values
  279. (cond
  280. ((eqv? type &flonum)
  281. (with-cps cps
  282. (let$ body (specialize-f64-binop k src op a b))
  283. (setk label ($kargs names vars ,body))))
  284. ((and (eqv? type &exact-integer)
  285. (or (<= 0 min max #xffffffffffffffff)
  286. (only-u64-bits-used? result))
  287. (u64-operand? a) (u64-operand? b)
  288. (not (eq? op 'div)))
  289. (with-cps cps
  290. (let$ body (specialize-u64-binop k src op a b))
  291. (setk label ($kargs names vars ,body))))
  292. (else
  293. cps))
  294. types
  295. sigbits))))))
  296. (($ $kargs names vars
  297. ($ $continue k src ($ $primcall 'ash (a b))))
  298. (match (intmap-ref cps k)
  299. (($ $kargs (_) (result))
  300. (call-with-values (lambda ()
  301. (lookup-pre-type types label b))
  302. (lambda (b-type b-min b-max)
  303. (values
  304. (cond
  305. ((or (not (u64-result? result))
  306. (not (u64-operand? a))
  307. (not (eqv? b-type &exact-integer))
  308. (< b-min 0 b-max)
  309. (<= b-min -64)
  310. (<= 64 b-max))
  311. cps)
  312. ((and (< b-min 0) (= b-min b-max))
  313. (with-cps cps
  314. (let$ body
  315. (with-cps-constants ((bits (- b-min)))
  316. ($ (specialize-u64-binop k src 'rsh a bits))))
  317. (setk label ($kargs names vars ,body))))
  318. ((< b-min 0)
  319. (with-cps cps
  320. (let$ body
  321. (with-cps-constants ((zero 0))
  322. (letv bits)
  323. (let$ body
  324. (specialize-u64-binop k src 'rsh a bits))
  325. (letk kneg ($kargs ('bits) (bits) ,body))
  326. (build-term
  327. ($continue kneg src
  328. ($primcall 'sub (zero b))))))
  329. (setk label ($kargs names vars ,body))))
  330. (else
  331. (with-cps cps
  332. (let$ body (specialize-u64-binop k src 'lsh a b))
  333. (setk label ($kargs names vars ,body)))))
  334. types
  335. sigbits))))))
  336. (($ $kargs names vars
  337. ($ $continue k src
  338. ($ $primcall (and op (or 'logand 'logior 'logsub 'logxor)) (a b))))
  339. (match (intmap-ref cps k)
  340. (($ $kargs (_) (result))
  341. (values
  342. (cond
  343. ((u64-result? result)
  344. ;; Given that we know the result can be unboxed to a u64,
  345. ;; any out-of-range bits won't affect the result and so we
  346. ;; can unconditionally project the operands onto u64.
  347. (cond
  348. ((and (eq? op 'logand) (all-u64-bits-set? a))
  349. (with-cps cps
  350. (let$ body (truncate-u64 k src b))
  351. (setk label ($kargs names vars ,body))))
  352. ((and (eq? op 'logand) (all-u64-bits-set? b))
  353. (with-cps cps
  354. (let$ body (truncate-u64 k src a))
  355. (setk label ($kargs names vars ,body))))
  356. (else
  357. (with-cps cps
  358. (let$ body (specialize-u64-binop k src op a b
  359. #:unbox-a
  360. 'scm->u64/truncate
  361. #:unbox-b
  362. 'scm->u64/truncate))
  363. (setk label ($kargs names vars ,body))))))
  364. (else cps))
  365. types sigbits))))
  366. (($ $kargs names vars
  367. ($ $continue k src
  368. ($ $branch kt ($ $primcall (and op (or '< '<= '= '>= '>)) (a b)))))
  369. (values
  370. (if (u64-operand? a)
  371. (let ((specialize (if (u64-operand? b)
  372. specialize-u64-comparison
  373. specialize-u64-scm-comparison)))
  374. (with-cps cps
  375. (let$ body (specialize k kt src op a b))
  376. (setk label ($kargs names vars ,body))))
  377. (if (u64-operand? b)
  378. (let ((op (match op
  379. ('< '>) ('<= '>=) ('= '=) ('>= '<=) ('> '<))))
  380. (with-cps cps
  381. (let$ body (specialize-u64-scm-comparison k kt src op b a))
  382. (setk label ($kargs names vars ,body))))
  383. cps))
  384. types
  385. sigbits))
  386. (_ (values cps types sigbits))))
  387. (values (intmap-fold visit-cont cps cps #f #f)))
  388. ;; Compute a map from VAR -> LABEL, where LABEL indicates the cont that
  389. ;; binds VAR.
  390. (define (compute-defs conts labels)
  391. (intset-fold
  392. (lambda (label defs)
  393. (match (intmap-ref conts label)
  394. (($ $kfun src meta self tail clause)
  395. (intmap-add defs self label))
  396. (($ $kargs names vars)
  397. (fold1 (lambda (var defs)
  398. (intmap-add defs var label))
  399. vars defs))
  400. (_ defs)))
  401. labels empty-intmap))
  402. ;; Compute vars whose definitions are all unboxable and whose uses
  403. ;; include an unbox operation.
  404. (define (compute-specializable-vars cps body preds defs
  405. exp-result-unboxable?
  406. unbox-ops)
  407. ;; Compute a map of VAR->LABEL... indicating the set of labels that
  408. ;; define VAR with unboxable values, given the set of vars
  409. ;; UNBOXABLE-VARS which is known already to be unboxable.
  410. (define (collect-unboxable-def-labels unboxable-vars)
  411. (define (add-unboxable-def unboxable-defs var label)
  412. (intmap-add unboxable-defs var (intset label) intset-union))
  413. (intset-fold (lambda (label unboxable-defs)
  414. (match (intmap-ref cps label)
  415. (($ $kargs _ _ ($ $continue k _ exp))
  416. (match exp
  417. ((? exp-result-unboxable?)
  418. (match (intmap-ref cps k)
  419. (($ $kargs (_) (def))
  420. (add-unboxable-def unboxable-defs def label))))
  421. (($ $values vars)
  422. (match (intmap-ref cps k)
  423. (($ $kargs _ defs)
  424. (fold
  425. (lambda (var def unboxable-defs)
  426. (if (intset-ref unboxable-vars var)
  427. (add-unboxable-def unboxable-defs def label)
  428. unboxable-defs))
  429. unboxable-defs vars defs))
  430. ;; Could be $ktail for $values.
  431. (_ unboxable-defs)))
  432. (_ unboxable-defs)))
  433. (_ unboxable-defs)))
  434. body empty-intmap))
  435. ;; Compute the set of vars which are always unboxable.
  436. (define (compute-unboxable-defs)
  437. (fixpoint
  438. (lambda (unboxable-vars)
  439. (intmap-fold
  440. (lambda (def unboxable-pred-labels unboxable-vars)
  441. (if (and (not (intset-ref unboxable-vars def))
  442. ;; Are all defining expressions unboxable?
  443. (and-map (lambda (pred)
  444. (intset-ref unboxable-pred-labels pred))
  445. (intmap-ref preds (intmap-ref defs def))))
  446. (intset-add unboxable-vars def)
  447. unboxable-vars))
  448. (collect-unboxable-def-labels unboxable-vars)
  449. unboxable-vars))
  450. empty-intset))
  451. ;; Compute the set of vars that may ever be unboxed.
  452. (define (compute-unbox-uses unboxable-defs)
  453. (intset-fold
  454. (lambda (label unbox-uses)
  455. (match (intmap-ref cps label)
  456. (($ $kargs _ _ ($ $continue k _ exp))
  457. (match exp
  458. (($ $primcall (? (lambda (op) (memq op unbox-ops))) (var))
  459. (intset-add unbox-uses var))
  460. (($ $values vars)
  461. (match (intmap-ref cps k)
  462. (($ $kargs _ defs)
  463. (fold (lambda (var def unbox-uses)
  464. (if (intset-ref unboxable-defs def)
  465. (intset-add unbox-uses var)
  466. unbox-uses))
  467. unbox-uses vars defs))
  468. (($ $ktail)
  469. ;; Assume return is rare and that any unboxable def can
  470. ;; be reboxed when leaving the procedure.
  471. (fold (lambda (var unbox-uses)
  472. (intset-add unbox-uses var))
  473. unbox-uses vars))))
  474. (_ unbox-uses)))
  475. (_ unbox-uses)))
  476. body empty-intset))
  477. (let ((unboxable-defs (compute-unboxable-defs)))
  478. (intset-intersect unboxable-defs (compute-unbox-uses unboxable-defs))))
  479. ;; Compute vars whose definitions are all inexact reals and whose uses
  480. ;; include an unbox operation.
  481. (define (compute-specializable-f64-vars cps body preds defs)
  482. ;; Can the result of EXP definitely be unboxed as an f64?
  483. (define (exp-result-f64? exp)
  484. (match exp
  485. ((or ($ $primcall 'f64->scm (_))
  486. ($ $const (and (? number?) (? inexact?) (? real?))))
  487. #t)
  488. (_ #f)))
  489. (compute-specializable-vars cps body preds defs exp-result-f64? '(scm->f64)))
  490. ;; Compute vars whose definitions are all exact integers in the u64
  491. ;; range and whose uses include an unbox operation.
  492. (define (compute-specializable-u64-vars cps body preds defs)
  493. ;; Can the result of EXP definitely be unboxed as a u64?
  494. (define (exp-result-u64? exp)
  495. (match exp
  496. ((or ($ $primcall 'u64->scm (_))
  497. ($ $const (and (? number?) (? exact-integer?)
  498. (? (lambda (n) (<= 0 n #xffffffffffffffff))))))
  499. #t)
  500. (_ #f)))
  501. (compute-specializable-vars cps body preds defs exp-result-u64?
  502. '(scm->u64 'scm->u64/truncate)))
  503. (define (compute-phi-vars cps preds)
  504. (intmap-fold (lambda (label preds phis)
  505. (match preds
  506. (() phis)
  507. ((_) phis)
  508. (_
  509. (match (intmap-ref cps label)
  510. (($ $kargs names vars)
  511. (fold1 (lambda (var phis)
  512. (intset-add phis var))
  513. vars phis))
  514. (_ phis)))))
  515. preds empty-intset))
  516. ;; Compute the set of variables which have more than one definition,
  517. ;; whose definitions are always f64-valued or u64-valued, and which have
  518. ;; at least one use that is an unbox operation.
  519. (define (compute-specializable-phis cps body preds defs)
  520. (let ((f64-vars (compute-specializable-f64-vars cps body preds defs))
  521. (u64-vars (compute-specializable-u64-vars cps body preds defs))
  522. (phi-vars (compute-phi-vars cps preds)))
  523. (unless (eq? empty-intset (intset-intersect f64-vars u64-vars))
  524. (error "expected f64 and u64 vars to be disjoint sets"))
  525. (intset-fold (lambda (var out) (intmap-add out var 'u64))
  526. (intset-intersect u64-vars phi-vars)
  527. (intset-fold (lambda (var out) (intmap-add out var 'f64))
  528. (intset-intersect f64-vars phi-vars)
  529. empty-intmap))))
  530. ;; Each definition of an f64/u64 variable should unbox that variable.
  531. ;; The cont that binds the variable should re-box it under its original
  532. ;; name, and rely on CSE to remove the boxing as appropriate.
  533. (define (apply-specialization cps kfun body preds defs phis)
  534. (define (compute-unbox-labels)
  535. (intmap-fold (lambda (phi kind labels)
  536. (fold1 (lambda (pred labels)
  537. (intset-add labels pred))
  538. (intmap-ref preds (intmap-ref defs phi))
  539. labels))
  540. phis empty-intset))
  541. (define (unbox-op var)
  542. (match (intmap-ref phis var)
  543. ('f64 'scm->f64)
  544. ('u64 'scm->u64)))
  545. (define (box-op var)
  546. (match (intmap-ref phis var)
  547. ('f64 'f64->scm)
  548. ('u64 'u64->scm)))
  549. (define (unbox-operands)
  550. (define (unbox-arg cps arg def-var have-arg)
  551. (if (intmap-ref phis def-var (lambda (_) #f))
  552. (with-cps cps
  553. (letv unboxed)
  554. (let$ body (have-arg unboxed))
  555. (letk kunboxed ($kargs ('unboxed) (unboxed) ,body))
  556. (build-term
  557. ($continue kunboxed #f ($primcall (unbox-op def-var) (arg)))))
  558. (have-arg cps arg)))
  559. (define (unbox-args cps args def-vars have-args)
  560. (match args
  561. (() (have-args cps '()))
  562. ((arg . args)
  563. (match def-vars
  564. ((def-var . def-vars)
  565. (unbox-arg cps arg def-var
  566. (lambda (cps arg)
  567. (unbox-args cps args def-vars
  568. (lambda (cps args)
  569. (have-args cps (cons arg args)))))))))))
  570. (intset-fold
  571. (lambda (label cps)
  572. (match (intmap-ref cps label)
  573. (($ $kargs names vars ($ $continue k src exp))
  574. (match (intmap-ref cps k)
  575. (($ $kargs _ defs)
  576. (match exp
  577. ;; For expressions that define a single value, we know we need
  578. ;; to unbox that value. For $values though we might have to
  579. ;; unbox just a subset of values.
  580. (($ $values args)
  581. (with-cps cps
  582. (let$ term (unbox-args
  583. args defs
  584. (lambda (cps args)
  585. (with-cps cps
  586. (build-term
  587. ($continue k src ($values args)))))))
  588. (setk label ($kargs names vars ,term))))
  589. (_
  590. (match defs
  591. ((def)
  592. (with-cps cps
  593. (letv boxed)
  594. (letk kunbox ($kargs ('boxed) (boxed)
  595. ($continue k src
  596. ($primcall (unbox-op def) (boxed)))))
  597. (setk label ($kargs names vars
  598. ($continue kunbox src ,exp)))))))))))))
  599. (compute-unbox-labels)
  600. cps))
  601. (define (compute-box-labels)
  602. (intmap-fold (lambda (phi kind labels)
  603. (intset-add labels (intmap-ref defs phi)))
  604. phis empty-intset))
  605. (define (box-results cps)
  606. (intset-fold
  607. (lambda (label cps)
  608. (match (intmap-ref cps label)
  609. (($ $kargs names vars term)
  610. (let* ((boxed (fold1 (lambda (var boxed)
  611. (if (intmap-ref phis var (lambda (_) #f))
  612. (intmap-add boxed var (fresh-var))
  613. boxed))
  614. vars empty-intmap))
  615. (bound-vars (map (lambda (var)
  616. (intmap-ref boxed var (lambda (var) var)))
  617. vars)))
  618. (define (box-var cps name var done)
  619. (let ((unboxed (intmap-ref boxed var (lambda (_) #f))))
  620. (if unboxed
  621. (with-cps cps
  622. (let$ term (done))
  623. (letk kboxed ($kargs (name) (var) ,term))
  624. (build-term
  625. ($continue kboxed #f
  626. ($primcall (box-op var) (unboxed)))))
  627. (done cps))))
  628. (define (box-vars cps names vars done)
  629. (match vars
  630. (() (done cps))
  631. ((var . vars)
  632. (match names
  633. ((name . names)
  634. (box-var cps name var
  635. (lambda (cps)
  636. (box-vars cps names vars done))))))))
  637. (with-cps cps
  638. (let$ box-term (box-vars names vars
  639. (lambda (cps)
  640. (with-cps cps term))))
  641. (setk label ($kargs names bound-vars ,box-term)))))))
  642. (compute-box-labels)
  643. cps))
  644. (box-results (unbox-operands)))
  645. (define (specialize-phis cps)
  646. (intmap-fold
  647. (lambda (kfun body cps)
  648. (let* ((preds (compute-predecessors cps kfun #:labels body))
  649. (defs (compute-defs cps body))
  650. (phis (compute-specializable-phis cps body preds defs)))
  651. (if (eq? phis empty-intmap)
  652. cps
  653. (apply-specialization cps kfun body preds defs phis))))
  654. (compute-reachable-functions cps)
  655. cps))
  656. (define (specialize-numbers cps)
  657. ;; Type inference wants a renumbered graph; OK.
  658. (let ((cps (renumber cps)))
  659. (with-fresh-name-state cps
  660. (specialize-phis (specialize-operations cps)))))