specialize-numbers.scm 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2015-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. ;;; 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 (srfi srfi-11)
  51. #:use-module (system base target)
  52. #:use-module (language cps)
  53. #:use-module (language cps intmap)
  54. #:use-module (language cps intset)
  55. #:use-module (language cps renumber)
  56. #:use-module (language cps types)
  57. #:use-module (language cps utils)
  58. #:use-module (language cps with-cps)
  59. #:export (specialize-numbers))
  60. ;; A note on how to represent unboxing and boxing operations. We want
  61. ;; to avoid diamond control flows here, like:
  62. ;;
  63. ;; s64 x = (if (fixnum? x*) (untag-fixnum x*) (untag-bignum x*))
  64. ;;
  65. ;; The reason is that the strategy that this specialize-numbers pass
  66. ;; uses to unbox values is to reify unboxing and boxing conversions
  67. ;; around every newly reified unboxed operation; it then relies heavily
  68. ;; on DCE and CSE to remove redundant conversions. However DCE and CSE
  69. ;; really work best when there's a linear control flow, so instead we
  70. ;; use a mid-level primcall:
  71. ;;
  72. ;; (define (scm->s64 x*)
  73. ;; (if (fixnum? x*) (untag-fixnum x*) (untag-bignum x*)))
  74. ;;
  75. ;; Then, unless we know that we can reduce directly to `untag-fixnum`,
  76. ;; we do:
  77. ;;
  78. ;; s64 x = (scm->s64 x*)
  79. ;;
  80. ;; That way we keep DCE and CSE happy. We can inline scm->s64 at the
  81. ;; backend if we choose to (though we might choose to not do so, for
  82. ;; code size reasons).
  83. (define (simple-primcall cps k src op arg)
  84. (with-cps cps
  85. (build-term
  86. ($continue k src
  87. ($primcall op #f (arg))))))
  88. (define-syntax-rule (define-simple-primcall name)
  89. (define (name cps k src arg) (simple-primcall cps k src 'name arg)))
  90. (define-simple-primcall untag-fixnum)
  91. (define-simple-primcall scm->s64)
  92. (define-simple-primcall tag-fixnum)
  93. (define-simple-primcall s64->scm)
  94. (define-simple-primcall tag-fixnum/unlikely)
  95. (define-simple-primcall s64->scm/unlikely)
  96. (define (fixnum->u64 cps k src fx)
  97. (with-cps cps
  98. (letv s64)
  99. (letk kcvt ($kargs ('s64) (s64)
  100. ($continue k src ($primcall 's64->u64 #f (s64)))))
  101. ($ (untag-fixnum kcvt src fx))))
  102. (define (u64->fixnum cps k src u64)
  103. (with-cps cps
  104. (letv s64)
  105. (let$ tag-body (tag-fixnum k src s64))
  106. (letk ks64 ($kargs ('s64) (s64) ,tag-body))
  107. (build-term
  108. ($continue ks64 src ($primcall 'u64->s64 #f (u64))))))
  109. (define-simple-primcall scm->u64)
  110. (define-simple-primcall u64->scm)
  111. (define-simple-primcall u64->scm/unlikely)
  112. (define-simple-primcall scm->f64)
  113. (define-simple-primcall f64->scm)
  114. (define (fixnum->f64 cps k src fx)
  115. (with-cps cps
  116. (letv s64)
  117. (letk kcvt ($kargs ('s64) (s64)
  118. ($continue k src ($primcall 's64->f64 #f (s64)))))
  119. ($ (untag-fixnum kcvt src fx))))
  120. (define (specialize-unop cps k src op param a unbox-a box-result)
  121. (with-cps cps
  122. (letv a* result)
  123. (let$ box-result-body (box-result k src result))
  124. (letk kbox ($kargs ('result) (result) ,box-result-body))
  125. (letk kop ($kargs ('a) (a*)
  126. ($continue kbox src ($primcall op param (a*)))))
  127. ($ (unbox-a kop src a))))
  128. (define* (specialize-binop cps k src op a b
  129. unbox-a unbox-b box-result)
  130. (with-cps cps
  131. (letv a* b* result)
  132. (let$ box-result-body (box-result k src result))
  133. (letk kbox ($kargs ('result) (result) ,box-result-body))
  134. (letk kop ($kargs ('b) (b*)
  135. ($continue kbox src ($primcall op #f (a* b*)))))
  136. (let$ unbox-b-body (unbox-b kop src b))
  137. (letk kunbox-b ($kargs ('a) (a*) ,unbox-b-body))
  138. ($ (unbox-a kunbox-b src a))))
  139. (define (specialize-comparison cps kf kt src op a b unbox-a unbox-b)
  140. (with-cps cps
  141. (letv a* b*)
  142. (letk kop ($kargs ('b) (b*) ($branch kf kt src op #f (a* b*))))
  143. (let$ unbox-b-body (unbox-b kop src b))
  144. (letk kunbox-b ($kargs ('a) (a*) ,unbox-b-body))
  145. ($ (unbox-a kunbox-b src a))))
  146. (define* (specialize-comparison/immediate cps kf kt src op a imm
  147. unbox-a)
  148. (with-cps cps
  149. (letv ia)
  150. (letk kop ($kargs ('ia) (ia) ($branch kf kt src op imm (ia))))
  151. ($ (unbox-a kop src a))))
  152. (define (specialize-comparison/s64-integer cps kf kt src op a-s64 b-int
  153. unbox-a rebox-a)
  154. (let ((s64-op (match op ('= 's64-=) ('< 's64-<))))
  155. (with-cps cps
  156. (letv a b sunk)
  157. (letk kheap ($kargs ('sunk) (sunk)
  158. ($branch kf kt src op #f (sunk b-int))))
  159. ;; Re-box the variable. FIXME: currently we use a specially
  160. ;; marked s64->scm to avoid CSE from hoisting the allocation
  161. ;; again. Instead we should just use a-s64 directly and implement
  162. ;; an allocation sinking pass that should handle this..
  163. (let$ rebox-a-body (rebox-a kheap src a))
  164. (letk kretag ($kargs () () ,rebox-a-body))
  165. (letk kb ($kargs ('b) (b) ($branch kf kt src s64-op #f (a b))))
  166. (letk kfix ($kargs () ()
  167. ($continue kb src
  168. ($primcall 'untag-fixnum #f (b-int)))))
  169. (letk ka ($kargs ('a) (a)
  170. ($branch kretag kfix src 'fixnum? #f (b-int))))
  171. ($ (unbox-a ka src a-s64)))))
  172. (define (specialize-comparison/integer-s64 cps kf kt src op a-int b-s64
  173. unbox-b rebox-b)
  174. (match op
  175. ('= (specialize-comparison/s64-integer cps kf kt src op b-s64 a-int
  176. unbox-b rebox-b))
  177. ('<
  178. (with-cps cps
  179. (letv a b sunk)
  180. (letk kheap ($kargs ('sunk) (sunk)
  181. ($branch kf kt src '< #f (a-int sunk))))
  182. ;; FIXME: We should just use b-s64 directly and implement an
  183. ;; allocation sinking pass so that the box op that creates b-64
  184. ;; should float down here. Instead, for now we just rebox the
  185. ;; variable, relying on the reboxing op not being available for
  186. ;; CSE.
  187. (let$ rebox-b-body (rebox-b kheap src b))
  188. (letk kretag ($kargs () () ,rebox-b-body))
  189. (letk ka ($kargs ('a) (a) ($branch kf kt src 's64-< #f (a b))))
  190. (letk kfix ($kargs () ()
  191. ($continue ka src
  192. ($primcall 'untag-fixnum #f (a-int)))))
  193. (letk kb ($kargs ('b) (b)
  194. ($branch kretag kfix src 'fixnum? #f (a-int))))
  195. ($ (unbox-b kb src b-s64))))))
  196. (define (specialize-comparison/immediate-s64-integer cps kf kt src op a b-int
  197. compare-integers)
  198. (with-cps cps
  199. (letv b sunk)
  200. (letk kheap ($kargs ('sunk) (sunk) ,(compare-integers kf kt src sunk)))
  201. ;; Re-box the variable. FIXME: currently we use a specially marked
  202. ;; load-const to avoid CSE from hoisting the constant. Instead we
  203. ;; should just use a $const directly and implement an allocation
  204. ;; sinking pass that should handle this..
  205. (letk kretag ($kargs () ()
  206. ($continue kheap src
  207. ($primcall 'load-const/unlikely a ()))))
  208. (letk kb ($kargs ('b) (b)
  209. ($branch kf kt src op a (b))))
  210. (letk kfix ($kargs () ()
  211. ($continue kb src
  212. ($primcall 'untag-fixnum #f (b-int)))))
  213. (build-term ($branch kretag kfix src 'fixnum? #f (b-int)))))
  214. ;; compute-significant-bits solves a flow equation to compute a
  215. ;; least-fixed-point over the lattice VAR -> BITMASK, where X > Y if
  216. ;; X[VAR] > Y[VAR] for any VAR. Adjoining VAR -> BITMASK to X results
  217. ;; in a distinct value X' (in the sense of eq?) if and only if X' > X.
  218. ;; This property is used in compute-significant-bits to know when to
  219. ;; stop iterating, and is ensured by intmaps, provided that the `meet'
  220. ;; function passed to `intmap-add' and so on also preserves this
  221. ;; property.
  222. ;;
  223. ;; The meet function for adding bits is `sigbits-union'; the first
  224. ;; argument is the existing value, and the second is the bitmask to
  225. ;; adjoin. For fixnums, BITMASK' will indeed be distinct if and only if
  226. ;; bits were added. However for bignums it's possible that (= X' X) but
  227. ;; not (eq? X' X). This preserve-eq? helper does the impedance matching
  228. ;; for bignums, returning the first value if the values are =.
  229. (define (preserve-eq? x x*)
  230. (if (= x x*)
  231. x
  232. x*))
  233. (define (sigbits-union x y)
  234. (and x y
  235. (preserve-eq? x (logior x y))))
  236. (define (sigbits-intersect x y)
  237. (cond
  238. ((not x) y)
  239. ((not y) x)
  240. (else (logand x y))))
  241. (define (sigbits-intersect3 a b c)
  242. (sigbits-intersect a (sigbits-intersect b c)))
  243. (define (next-power-of-two n)
  244. (let lp ((out 1))
  245. (if (< n out)
  246. out
  247. (lp (ash out 1)))))
  248. (define (range->sigbits min max)
  249. (cond
  250. ((or (< min 0) (> max #xffffFFFFffffFFFF)) #f)
  251. ((eqv? min max) min)
  252. (else (1- (next-power-of-two max)))))
  253. (define (inferred-sigbits types label var)
  254. (call-with-values (lambda () (lookup-pre-type types label var))
  255. (lambda (type min max)
  256. (and (type<=? type (logior &exact-integer &u64 &s64))
  257. (range->sigbits min max)))))
  258. (define significant-bits-handlers (make-hash-table))
  259. (define-syntax-rule (define-significant-bits-handler
  260. ((primop label types out def ...) arg ...)
  261. body ...)
  262. (hashq-set! significant-bits-handlers 'primop
  263. (lambda (label types out param args defs)
  264. (match args ((arg ...) (match defs ((def ...) body ...)))))))
  265. (define-significant-bits-handler ((logand label types out res) a b)
  266. (let ((sigbits (sigbits-intersect3 (inferred-sigbits types label a)
  267. (inferred-sigbits types label b)
  268. (intmap-ref out res (lambda (_) 0)))))
  269. (intmap-add (intmap-add out a sigbits sigbits-union)
  270. b sigbits sigbits-union)))
  271. (define (significant-bits-handler primop)
  272. (hashq-ref significant-bits-handlers primop))
  273. (define (compute-significant-bits cps types kfun)
  274. "Given the locally inferred types @var{types}, compute a map of VAR ->
  275. BITS indicating the significant bits needed for a variable. BITS may be
  276. #f to indicate all bits, or a non-negative integer indicating a bitmask."
  277. (let ((preds (invert-graph (compute-successors cps kfun))))
  278. (let lp ((worklist (intmap-keys preds)) (visited empty-intset)
  279. (out empty-intmap))
  280. (match (intset-prev worklist)
  281. (#f out)
  282. (label
  283. (let ((worklist (intset-remove worklist label))
  284. (visited* (intset-add visited label)))
  285. (define (continue out*)
  286. (if (and (eq? out out*) (eq? visited visited*))
  287. (lp worklist visited out)
  288. (lp (intset-union worklist (intmap-ref preds label))
  289. visited* out*)))
  290. (define (add-def out var)
  291. (intmap-add out var 0 sigbits-union))
  292. (define (add-defs out vars)
  293. (match vars
  294. (() out)
  295. ((var . vars) (add-defs (add-def out var) vars))))
  296. (define (add-unknown-use out var)
  297. (intmap-add out var (inferred-sigbits types label var)
  298. sigbits-union))
  299. (define (add-unknown-uses out vars)
  300. (match vars
  301. (() out)
  302. ((var . vars)
  303. (add-unknown-uses (add-unknown-use out var) vars))))
  304. (continue
  305. (match (intmap-ref cps label)
  306. (($ $kfun src meta self)
  307. (if self (add-def out self) out))
  308. (($ $kargs names vars term)
  309. (let ((out (add-defs out vars)))
  310. (match term
  311. (($ $continue k src exp)
  312. (match exp
  313. ((or ($ $const) ($ $prim) ($ $fun) ($ $const-fun)
  314. ($ $code) ($ $rec))
  315. ;; No uses, so no info added to sigbits.
  316. out)
  317. (($ $values args)
  318. (match (intmap-ref cps k)
  319. (($ $kargs _ vars)
  320. (if (intset-ref visited k)
  321. (fold (lambda (arg var out)
  322. (intmap-add out arg (intmap-ref out var)
  323. sigbits-union))
  324. out args vars)
  325. out))
  326. (($ $ktail)
  327. (add-unknown-uses out args))))
  328. (($ $call proc args)
  329. (add-unknown-use (add-unknown-uses out args) proc))
  330. (($ $callk label proc args)
  331. (let ((out (add-unknown-uses out args)))
  332. (if proc
  333. (add-unknown-use out proc)
  334. out)))
  335. (($ $calli args callee)
  336. (add-unknown-uses (add-unknown-use out callee) args))
  337. (($ $primcall name param args)
  338. (let ((h (significant-bits-handler name)))
  339. (if h
  340. (match (intmap-ref cps k)
  341. (($ $kargs _ defs)
  342. (h label types out param args defs)))
  343. (add-unknown-uses out args))))))
  344. (($ $branch kf kt src op param args)
  345. (add-unknown-uses out args))
  346. (($ $switch kf kt src arg)
  347. (add-unknown-use out arg))
  348. (($ $prompt k kh src escape? tag)
  349. (add-unknown-use out tag))
  350. (($ $throw src op param args)
  351. (add-unknown-uses out args)))))
  352. (_ out)))))))))
  353. (define (specialize-operations cps)
  354. (define (u6-parameter? param)
  355. (<= 0 param 63))
  356. (define (s64-parameter? param)
  357. (<= (ash -1 63) param (1- (ash 1 63))))
  358. (define (u64-parameter? param)
  359. (<= 0 param (1- (ash 1 64))))
  360. (define (visit-cont label cont cps types sigbits)
  361. (define (operand-in-range? var &type &min &max)
  362. (call-with-values (lambda ()
  363. (lookup-pre-type types label var))
  364. (lambda (type min max)
  365. (and (type<=? type &type) (<= &min min max &max)))))
  366. (define (u64-operand? var)
  367. (operand-in-range? var &exact-integer 0 (1- (ash 1 64))))
  368. (define (u6-operand? var)
  369. ;; This predicate is only used for the "count" argument to
  370. ;; rsh/lsh, which is already unboxed to &u64.
  371. (operand-in-range? var &u64 0 63))
  372. (define (s64-operand? var)
  373. (operand-in-range? var &exact-integer (ash -1 63) (1- (ash 1 63))))
  374. (define (fixnum-operand? var)
  375. (operand-in-range? var &exact-integer
  376. (target-most-negative-fixnum)
  377. (target-most-positive-fixnum)))
  378. (define (exact-integer-operand? var)
  379. (operand-in-range? var &exact-integer -inf.0 +inf.0))
  380. (define (all-u64-bits-set? var)
  381. (operand-in-range? var &exact-integer (1- (ash 1 64)) (1- (ash 1 64))))
  382. (define (only-fixnum-bits-used? var)
  383. (let ((bits (intmap-ref sigbits var)))
  384. (and bits (= bits (logand bits (target-most-positive-fixnum))))))
  385. (define (fixnum-result? result)
  386. (or (only-fixnum-bits-used? result)
  387. (call-with-values
  388. (lambda ()
  389. (lookup-post-type types label result 0))
  390. (lambda (type min max)
  391. (and (type<=? type &exact-integer)
  392. (<= (target-most-negative-fixnum)
  393. min max
  394. (target-most-positive-fixnum)))))))
  395. (define (only-u64-bits-used? var)
  396. (let ((bits (intmap-ref sigbits var)))
  397. (and bits (= bits (logand bits (1- (ash 1 64)))))))
  398. (define (u64-result? result)
  399. (or (only-u64-bits-used? result)
  400. (call-with-values
  401. (lambda ()
  402. (lookup-post-type types label result 0))
  403. (lambda (type min max)
  404. (and (type<=? type &exact-integer)
  405. (<= 0 min max (1- (ash 1 64))))))))
  406. (define (s64-result? result)
  407. (call-with-values
  408. (lambda ()
  409. (lookup-post-type types label result 0))
  410. (lambda (type min max)
  411. (and (type<=? type &exact-integer)
  412. (<= (ash -1 63) min max (1- (ash 1 63)))))))
  413. (define (f64-result? result)
  414. (call-with-values
  415. (lambda ()
  416. (lookup-post-type types label result 0))
  417. (lambda (type min max)
  418. (eqv? type &flonum))))
  419. (define (f64-operands? vara varb)
  420. (let-values (((typea mina maxa) (lookup-pre-type types label vara))
  421. ((typeb minb maxb) (lookup-pre-type types label varb)))
  422. (and (type<=? (logior typea typeb) &real)
  423. (or (eqv? typea &flonum)
  424. (eqv? typeb &flonum)))))
  425. (define (constant-arg arg)
  426. (let-values (((type min max) (lookup-pre-type types label arg)))
  427. (and (= min max) min)))
  428. (define (fixnum-range? min max)
  429. (<= (target-most-negative-fixnum) min max (target-most-positive-fixnum)))
  430. (define (unbox-u64 arg)
  431. (if (fixnum-operand? arg) fixnum->u64 scm->u64))
  432. (define (unbox-s64 arg)
  433. (if (fixnum-operand? arg) untag-fixnum scm->s64))
  434. (define (rebox-s64 arg)
  435. (if (fixnum-operand? arg) tag-fixnum/unlikely s64->scm/unlikely))
  436. (define (unbox-f64 arg)
  437. ;; Could be more precise here.
  438. (if (fixnum-operand? arg) fixnum->f64 scm->f64))
  439. (define (box-s64 result)
  440. (if (fixnum-result? result) tag-fixnum s64->scm))
  441. (define (box-u64 result)
  442. (if (fixnum-result? result) u64->fixnum u64->scm))
  443. (define (box-f64 result)
  444. f64->scm)
  445. (define (specialize-primcall cps k src op param args)
  446. (match (intmap-ref cps k)
  447. (($ $kargs (_) (result))
  448. (match (cons* op result param args)
  449. (((or 'add 'sub 'mul 'div 'atan2)
  450. (? f64-result?) #f a b)
  451. (let ((op (match op
  452. ('add 'fadd) ('sub 'fsub) ('mul 'fmul) ('div 'fdiv)
  453. ('atan2 'fatan2))))
  454. (specialize-binop cps k src op a b
  455. (unbox-f64 a) (unbox-f64 b) (box-f64 result))))
  456. (((or 'sqrt 'abs 'floor 'ceiling 'sin 'cos 'tan 'asin 'acos 'atan)
  457. (? f64-result?) #f a)
  458. (let ((op (match op
  459. ('sqrt 'fsqrt) ('abs 'fabs)
  460. ('floor 'ffloor) ('ceiling 'fceiling)
  461. ('sin 'fsin) ('cos 'fcos) ('tan 'ftan)
  462. ('asin 'fasin) ('acos 'facos) ('atan 'fatan))))
  463. (specialize-unop cps k src op #f a
  464. (unbox-f64 a) (box-f64 result))))
  465. (((or 'add 'sub 'mul 'logand 'logior 'logxor 'logsub)
  466. (? u64-result?) #f (? u64-operand? a) (? u64-operand? b))
  467. (let ((op (match op
  468. ('add 'uadd) ('sub 'usub) ('mul 'umul)
  469. ('logand 'ulogand) ('logior 'ulogior)
  470. ('logxor 'ulogxor) ('logsub 'ulogsub))))
  471. (specialize-binop cps k src op a b
  472. (unbox-u64 a) (unbox-u64 b) (box-u64 result))))
  473. (((or 'logand 'logior 'logxor 'logsub)
  474. (? u64-result?) #f (? s64-operand? a) (? s64-operand? b))
  475. (let ((op (match op
  476. ('logand 'ulogand) ('logior 'ulogior)
  477. ('logxor 'ulogxor) ('logsub 'ulogsub))))
  478. (define (unbox-u64* x)
  479. (let ((unbox-s64 (unbox-s64 x)))
  480. (lambda (cps k src x)
  481. (with-cps cps
  482. (letv s64)
  483. (letk ks64 ($kargs ('s64) (s64)
  484. ($continue k src
  485. ($primcall 's64->u64 #f (s64)))))
  486. ($ (unbox-s64 k src x))))))
  487. (specialize-binop cps k src op a b
  488. (unbox-u64* a) (unbox-u64* b) (box-u64 result))))
  489. (((or 'add 'sub 'mul)
  490. (? s64-result?) #f (? s64-operand? a) (? s64-operand? b))
  491. (let ((op (match op
  492. ('add 'sadd) ('sub 'ssub) ('mul 'smul))))
  493. (specialize-binop cps k src op a b
  494. (unbox-s64 a) (unbox-s64 b) (box-s64 result))))
  495. (('sub/immediate
  496. (? f64-result?) param a)
  497. (specialize-unop cps k src 'fadd/immediate (- param) a
  498. (unbox-f64 a) (box-f64 result)))
  499. (((or 'add/immediate 'mul/immediate)
  500. (? f64-result?) param a)
  501. (let ((op (match op
  502. ('add/immediate 'fadd/immediate)
  503. ('mul/immediate 'fmul/immediate))))
  504. (specialize-unop cps k src op param a
  505. (unbox-f64 a) (box-f64 result))))
  506. (((or 'add/immediate 'sub/immediate 'mul/immediate)
  507. (? u64-result?) (? u64-parameter?) (? u64-operand? a))
  508. (let ((op (match op
  509. ('add/immediate 'uadd/immediate)
  510. ('sub/immediate 'usub/immediate)
  511. ('mul/immediate 'umul/immediate))))
  512. (specialize-unop cps k src op param a
  513. (unbox-u64 a) (box-u64 result))))
  514. (((or 'add/immediate 'sub/immediate 'mul/immediate)
  515. (? s64-result?) (? s64-parameter?) (? s64-operand? a))
  516. (let ((op (match op
  517. ('add/immediate 'sadd/immediate)
  518. ('sub/immediate 'ssub/immediate)
  519. ('mul/immediate 'smul/immediate))))
  520. (specialize-unop cps k src op param a
  521. (unbox-s64 a) (box-s64 result))))
  522. (((or 'lsh 'rsh)
  523. (? u64-result?) #f (? u64-operand? a) (? u6-operand? b))
  524. (let ((op (match op ('lsh 'ulsh) ('rsh 'ursh))))
  525. (define (pass-u64 cps k src b)
  526. (with-cps cps
  527. (build-term ($continue k src ($values (b))))))
  528. (specialize-binop cps k src op a b
  529. (unbox-u64 a) pass-u64 (box-u64 result))))
  530. (((or 'lsh 'rsh)
  531. (? s64-result?) #f (? s64-operand? a) (? u6-operand? b))
  532. (let ((op (match op ('lsh 'slsh) ('rsh 'srsh))))
  533. (define (pass-u64 cps k src b)
  534. (with-cps cps
  535. (build-term ($continue k src ($values (b))))))
  536. (specialize-binop cps k src op a b
  537. (unbox-s64 a) pass-u64 (box-s64 result))))
  538. (((or 'lsh/immediate 'rsh/immediate)
  539. (? u64-result?) (? u6-parameter?) (? u64-operand? a))
  540. (let ((op (match op
  541. ('lsh/immediate 'ulsh/immediate)
  542. ('rsh/immediate 'ursh/immediate))))
  543. (specialize-unop cps k src op param a
  544. (unbox-u64 a) (box-u64 result))))
  545. (((or 'lsh/immediate 'rsh/immediate)
  546. (? s64-result?) (? u6-parameter?) (? s64-operand? a))
  547. (let ((op (match op
  548. ('lsh/immediate 'slsh/immediate)
  549. ('rsh/immediate 'srsh/immediate))))
  550. (specialize-unop cps k src op param a
  551. (unbox-s64 a) (box-s64 result))))
  552. (_ (with-cps cps #f))))
  553. (_ (with-cps cps #f))))
  554. (define (specialize-branch cps kf kt src op param args)
  555. (match (cons op args)
  556. (('<= a b)
  557. (cond
  558. ((f64-operands? a b)
  559. (specialize-comparison cps kf kt src 'f64-<= a b
  560. (unbox-f64 a) (unbox-f64 b)))
  561. ((and (exact-integer-operand? a) (exact-integer-operand? b))
  562. ;; If NaN is impossible, reduce (<= a b) to (not (< b a)) and
  563. ;; try again.
  564. (specialize-branch cps kt kf src '< param (list b a)))
  565. (else
  566. (with-cps cps #f))))
  567. (((or '< '=) a b)
  568. (cond
  569. ((f64-operands? a b)
  570. (let ((op (match op ('= 'f64-=) ('< 'f64-<))))
  571. (specialize-comparison cps kf kt src op a b
  572. (unbox-f64 a) (unbox-f64 b))))
  573. ((and (s64-operand? a) (s64-operand? b))
  574. (cond
  575. ((constant-arg a)
  576. => (lambda (a)
  577. (let ((op (match op ('= 's64-imm-=) ('< 'imm-s64-<))))
  578. (specialize-comparison/immediate cps kf kt src op b a
  579. (unbox-s64 b)))))
  580. ((constant-arg b)
  581. => (lambda (b)
  582. (let ((op (match op ('= 's64-imm-=) ('< 's64-imm-<))))
  583. (specialize-comparison/immediate cps kf kt src op a b
  584. (unbox-s64 a)))))
  585. (else
  586. (let ((op (match op ('= 's64-=) ('< 's64-<))))
  587. (specialize-comparison cps kf kt src op a b
  588. (unbox-s64 a) (unbox-s64 b))))))
  589. ((and (u64-operand? a) (u64-operand? b))
  590. (cond
  591. ((constant-arg a)
  592. => (lambda (a)
  593. (let ((op (match op ('= 'u64-imm-=) ('< 'imm-u64-<))))
  594. (specialize-comparison/immediate cps kf kt src op b a
  595. (unbox-u64 b)))))
  596. ((constant-arg b)
  597. => (lambda (b)
  598. (let ((op (match op ('= 'u64-imm-=) ('< 'u64-imm-<))))
  599. (specialize-comparison/immediate cps kf kt src op a b
  600. (unbox-u64 a)))))
  601. (else
  602. (let ((op (match op ('= 'u64-=) ('< 'u64-<))))
  603. (specialize-comparison cps kf kt src op a b
  604. (unbox-u64 a) (unbox-u64 b))))))
  605. ((and (exact-integer-operand? a) (exact-integer-operand? b))
  606. (cond
  607. ((s64-operand? a)
  608. (cond
  609. ((constant-arg a)
  610. => (lambda (a)
  611. (let ((imm-op (match op ('= 's64-imm-=) ('< 'imm-s64-<))))
  612. (specialize-comparison/immediate-s64-integer
  613. cps kf kt src imm-op a b
  614. (lambda (kf kt src a)
  615. (build-term ($branch kf kt src op #f (a b))))))))
  616. (else
  617. (specialize-comparison/s64-integer cps kf kt src op a b
  618. (unbox-s64 a)
  619. (rebox-s64 a)))))
  620. ((s64-operand? b)
  621. (cond
  622. ((constant-arg b)
  623. => (lambda (b)
  624. (let ((imm-op (match op ('= 's64-imm-=) ('< 's64-imm-<))))
  625. (specialize-comparison/immediate-s64-integer
  626. cps kf kt src imm-op b a
  627. (lambda (kf kt src b)
  628. (build-term ($branch kf kt src op #f (a b))))))))
  629. (else
  630. (specialize-comparison/integer-s64 cps kf kt src op a b
  631. (unbox-s64 b)
  632. (rebox-s64 b)))))
  633. (else (with-cps cps #f))))
  634. (else (with-cps cps #f))))
  635. (_ (with-cps cps #f))))
  636. (match cont
  637. (($ $kfun)
  638. (let* ((types (infer-types cps label))
  639. (sigbits (compute-significant-bits cps types label)))
  640. (values cps types sigbits)))
  641. (($ $kargs names vars ($ $continue k src ($ $primcall op param args)))
  642. (call-with-values
  643. (lambda () (specialize-primcall cps k src op param args))
  644. (lambda (cps term)
  645. (values (if term
  646. (with-cps cps
  647. (setk label ($kargs names vars ,term)))
  648. cps)
  649. types sigbits))))
  650. (($ $kargs names vars ($ $branch kf kt src op param args))
  651. (call-with-values
  652. (lambda () (specialize-branch cps kf kt src op param args))
  653. (lambda (cps term)
  654. (values (if term
  655. (with-cps cps
  656. (setk label ($kargs names vars ,term)))
  657. cps)
  658. types sigbits))))
  659. (_ (values cps types sigbits))))
  660. (values (intmap-fold visit-cont cps cps #f #f)))
  661. ;; Compute a map from VAR -> LABEL, where LABEL indicates the cont that
  662. ;; binds VAR.
  663. (define (compute-defs conts labels)
  664. (intset-fold
  665. (lambda (label defs)
  666. (match (intmap-ref conts label)
  667. (($ $kfun src meta self tail clause)
  668. (if self (intmap-add defs self label) defs))
  669. (($ $kargs names vars)
  670. (fold1 (lambda (var defs)
  671. (intmap-add defs var label))
  672. vars defs))
  673. (_ defs)))
  674. labels empty-intmap))
  675. ;; Compute vars whose definitions are all unboxable and whose uses
  676. ;; include an unbox operation.
  677. (define (compute-specializable-vars cps body preds defs
  678. exp-result-unboxable?
  679. unbox-ops)
  680. ;; Compute a map of VAR->LABEL... indicating the set of labels that
  681. ;; define VAR with unboxable values, given the set of vars
  682. ;; UNBOXABLE-VARS which is known already to be unboxable.
  683. (define (collect-unboxable-def-labels unboxable-vars)
  684. (define (add-unboxable-def unboxable-defs var label)
  685. (intmap-add unboxable-defs var (intset label) intset-union))
  686. (intset-fold (lambda (label unboxable-defs)
  687. (match (intmap-ref cps label)
  688. (($ $kargs _ _ ($ $continue k _ exp))
  689. (match exp
  690. ((? exp-result-unboxable?)
  691. (match (intmap-ref cps k)
  692. (($ $kargs (_) (def))
  693. (add-unboxable-def unboxable-defs def label))))
  694. (($ $values vars)
  695. (match (intmap-ref cps k)
  696. (($ $kargs _ defs)
  697. (fold
  698. (lambda (var def unboxable-defs)
  699. (if (intset-ref unboxable-vars var)
  700. (add-unboxable-def unboxable-defs def label)
  701. unboxable-defs))
  702. unboxable-defs vars defs))
  703. ;; Could be $ktail for $values.
  704. (_ unboxable-defs)))
  705. (_ unboxable-defs)))
  706. (_ unboxable-defs)))
  707. body empty-intmap))
  708. ;; Compute the set of vars which are always unboxable.
  709. (define (compute-unboxable-defs)
  710. (fixpoint
  711. (lambda (unboxable-vars)
  712. (intmap-fold
  713. (lambda (def unboxable-pred-labels unboxable-vars)
  714. (if (and (not (intset-ref unboxable-vars def))
  715. ;; Are all defining expressions unboxable?
  716. (and-map (lambda (pred)
  717. (intset-ref unboxable-pred-labels pred))
  718. (intmap-ref preds (intmap-ref defs def))))
  719. (intset-add unboxable-vars def)
  720. unboxable-vars))
  721. (collect-unboxable-def-labels unboxable-vars)
  722. unboxable-vars))
  723. empty-intset))
  724. ;; Compute the set of vars that may ever be unboxed.
  725. (define (compute-unbox-uses unboxable-defs)
  726. (intset-fold
  727. (lambda (label unbox-uses)
  728. (match (intmap-ref cps label)
  729. (($ $kargs _ _ ($ $continue k _ exp))
  730. (match exp
  731. (($ $primcall (? (lambda (op) (memq op unbox-ops))) #f (var))
  732. (intset-add unbox-uses var))
  733. (($ $values vars)
  734. (match (intmap-ref cps k)
  735. (($ $kargs _ defs)
  736. (fold (lambda (var def unbox-uses)
  737. (if (intset-ref unboxable-defs def)
  738. (intset-add unbox-uses var)
  739. unbox-uses))
  740. unbox-uses vars defs))
  741. (($ $ktail)
  742. ;; Assume return is rare and that any unboxable def can
  743. ;; be reboxed when leaving the procedure.
  744. (fold (lambda (var unbox-uses)
  745. (intset-add unbox-uses var))
  746. unbox-uses vars))))
  747. (_ unbox-uses)))
  748. (_ unbox-uses)))
  749. body empty-intset))
  750. (let ((unboxable-defs (compute-unboxable-defs)))
  751. (intset-intersect unboxable-defs (compute-unbox-uses unboxable-defs))))
  752. ;; Compute vars whose definitions are all inexact reals and whose uses
  753. ;; include an unbox operation.
  754. (define (compute-specializable-f64-vars cps body preds defs)
  755. ;; Can the result of EXP definitely be unboxed as an f64?
  756. (define (exp-result-f64? exp)
  757. (match exp
  758. ((or ($ $primcall 'f64->scm #f (_))
  759. ($ $const (and (? number?) (? inexact?) (? real?))))
  760. #t)
  761. (_ #f)))
  762. (compute-specializable-vars cps body preds defs exp-result-f64? '(scm->f64)))
  763. ;; Compute vars whose definitions are all exact integers in the u64
  764. ;; range and whose uses include an unbox operation.
  765. (define (compute-specializable-u64-vars cps body preds defs)
  766. ;; Can the result of EXP definitely be unboxed as a u64?
  767. (define (exp-result-u64? exp)
  768. (define (u64? n)
  769. (and (number? n) (exact-integer? n)
  770. (<= 0 n #xffffffffffffffff)))
  771. (match exp
  772. ((or ($ $primcall 'u64->scm #f (_))
  773. ($ $primcall 'u64->scm/unlikely #f (_))
  774. ($ $primcall 'load-const/unlikely (? u64?) ())
  775. ($ $const (? u64?)))
  776. #t)
  777. (_ #f)))
  778. (compute-specializable-vars cps body preds defs exp-result-u64?
  779. '(scm->u64 'scm->u64/truncate)))
  780. ;; Compute vars whose definitions are all exact integers in the fixnum
  781. ;; range and whose uses include an untag operation.
  782. (define (compute-specializable-fixnum-vars cps body preds defs)
  783. ;; Is the result of EXP definitely a fixnum?
  784. (define (exp-result-fixnum? exp)
  785. (define (fixnum? n)
  786. (and (number? n) (exact-integer? n)
  787. (<= (target-most-negative-fixnum)
  788. n
  789. (target-most-positive-fixnum))))
  790. (match exp
  791. ((or ($ $primcall 'tag-fixnum #f (_))
  792. ($ $primcall 'tag-fixnum/unlikely #f (_))
  793. ($ $const (? fixnum?))
  794. ($ $primcall 'load-const/unlikely (? fixnum?) ()))
  795. #t)
  796. (_ #f)))
  797. (compute-specializable-vars cps body preds defs exp-result-fixnum?
  798. '(untag-fixnum)))
  799. ;; Compute vars whose definitions are all exact integers in the s64
  800. ;; range and whose uses include an untag operation.
  801. (define (compute-specializable-s64-vars cps body preds defs)
  802. ;; Is the result of EXP definitely a fixnum?
  803. (define (exp-result-fixnum? exp)
  804. (define (s64? n)
  805. (and (number? n) (exact-integer? n)
  806. (<= (ash -1 63) n (1- (ash 1 63)))))
  807. (match exp
  808. ((or ($ $primcall 's64->scm #f (_))
  809. ($ $const (? s64?))
  810. ($ $primcall 'load-const/unlikely (? s64?) ()))
  811. #t)
  812. (_ #f)))
  813. (compute-specializable-vars cps body preds defs exp-result-fixnum?
  814. '(scm->s64)))
  815. (define (compute-phi-vars cps preds)
  816. (intmap-fold (lambda (label preds phis)
  817. (match preds
  818. (() phis)
  819. ((_) phis)
  820. (_
  821. (match (intmap-ref cps label)
  822. (($ $kargs names vars)
  823. (fold1 (lambda (var phis)
  824. (intset-add phis var))
  825. vars phis))
  826. (_ phis)))))
  827. preds empty-intset))
  828. ;; Compute the set of variables which have more than one definition,
  829. ;; whose definitions are always f64-valued or u64-valued, and which have
  830. ;; at least one use that is an unbox operation.
  831. (define (compute-specializable-phis cps body preds defs)
  832. (let ((phi-vars (compute-phi-vars cps preds)))
  833. (fold1 (lambda (in out)
  834. (match in
  835. ((kind vars)
  836. (intset-fold
  837. (lambda (var out)
  838. (intmap-add out var kind (lambda (old new) old)))
  839. (intset-intersect phi-vars vars)
  840. out))))
  841. `((f64 ,(compute-specializable-f64-vars cps body preds defs))
  842. (fx ,(compute-specializable-fixnum-vars cps body preds defs))
  843. (s64 ,(compute-specializable-s64-vars cps body preds defs))
  844. (u64 ,(compute-specializable-u64-vars cps body preds defs)))
  845. empty-intmap)))
  846. ;; Each definition of a f64/u64 variable should unbox that variable.
  847. ;; The cont that binds the variable should re-box it under its original
  848. ;; name, and rely on CSE to remove the boxing as appropriate.
  849. (define (apply-specialization cps kfun body preds defs phis)
  850. (define (compute-unbox-labels)
  851. (intmap-fold (lambda (phi kind labels)
  852. (fold1 (lambda (pred labels)
  853. (intset-add labels pred))
  854. (intmap-ref preds (intmap-ref defs phi))
  855. labels))
  856. phis empty-intset))
  857. (define (unbox-op var)
  858. (match (intmap-ref phis var)
  859. ('f64 'scm->f64)
  860. ('fx 'untag-fixnum)
  861. ('s64 'scm->s64)
  862. ('u64 'scm->u64)))
  863. (define (box-op var)
  864. (match (intmap-ref phis var)
  865. ('f64 'f64->scm)
  866. ('fx 'tag-fixnum)
  867. ('s64 's64->scm)
  868. ('u64 'u64->scm)))
  869. (define (unbox-operands)
  870. (define (unbox-arg cps arg def-var have-arg)
  871. (if (intmap-ref phis def-var (lambda (_) #f))
  872. (with-cps cps
  873. (letv unboxed)
  874. (let$ body (have-arg unboxed))
  875. (letk kunboxed ($kargs ('unboxed) (unboxed) ,body))
  876. (build-term
  877. ($continue kunboxed #f ($primcall (unbox-op def-var) #f (arg)))))
  878. (have-arg cps arg)))
  879. (define (unbox-args cps args def-vars have-args)
  880. (match args
  881. (() (have-args cps '()))
  882. ((arg . args)
  883. (match def-vars
  884. ((def-var . def-vars)
  885. (unbox-arg cps arg def-var
  886. (lambda (cps arg)
  887. (unbox-args cps args def-vars
  888. (lambda (cps args)
  889. (have-args cps (cons arg args)))))))))))
  890. (intset-fold
  891. (lambda (label cps)
  892. (match (intmap-ref cps label)
  893. (($ $kargs names vars ($ $continue k src exp))
  894. (match (intmap-ref cps k)
  895. (($ $kargs _ defs)
  896. (match exp
  897. ;; For expressions that define a single value, we know we need
  898. ;; to unbox that value. For $values though we might have to
  899. ;; unbox just a subset of values.
  900. (($ $values args)
  901. (with-cps cps
  902. (let$ term (unbox-args
  903. args defs
  904. (lambda (cps args)
  905. (with-cps cps
  906. (build-term
  907. ($continue k src ($values args)))))))
  908. (setk label ($kargs names vars ,term))))
  909. (_
  910. (match defs
  911. ((def)
  912. (with-cps cps
  913. (letv boxed)
  914. (letk kunbox ($kargs ('boxed) (boxed)
  915. ($continue k src
  916. ($primcall (unbox-op def) #f (boxed)))))
  917. (setk label ($kargs names vars
  918. ($continue kunbox src ,exp)))))))))))))
  919. (compute-unbox-labels)
  920. cps))
  921. (define (compute-box-labels)
  922. (intmap-fold (lambda (phi kind labels)
  923. (intset-add labels (intmap-ref defs phi)))
  924. phis empty-intset))
  925. (define (box-results cps)
  926. (intset-fold
  927. (lambda (label cps)
  928. (match (intmap-ref cps label)
  929. (($ $kargs names vars term)
  930. (let* ((boxed (fold1 (lambda (var boxed)
  931. (if (intmap-ref phis var (lambda (_) #f))
  932. (intmap-add boxed var (fresh-var))
  933. boxed))
  934. vars empty-intmap))
  935. (bound-vars (map (lambda (var)
  936. (intmap-ref boxed var (lambda (var) var)))
  937. vars)))
  938. (define (box-var cps name var done)
  939. (let ((unboxed (intmap-ref boxed var (lambda (_) #f))))
  940. (if unboxed
  941. (with-cps cps
  942. (let$ term (done))
  943. (letk kboxed ($kargs (name) (var) ,term))
  944. (build-term
  945. ($continue kboxed #f
  946. ($primcall (box-op var) #f (unboxed)))))
  947. (done cps))))
  948. (define (box-vars cps names vars done)
  949. (match vars
  950. (() (done cps))
  951. ((var . vars)
  952. (match names
  953. ((name . names)
  954. (box-var cps name var
  955. (lambda (cps)
  956. (box-vars cps names vars done))))))))
  957. (with-cps cps
  958. (let$ box-term (box-vars names vars
  959. (lambda (cps)
  960. (with-cps cps term))))
  961. (setk label ($kargs names bound-vars ,box-term)))))))
  962. (compute-box-labels)
  963. cps))
  964. (box-results (unbox-operands)))
  965. (define (specialize-phis cps)
  966. (intmap-fold
  967. (lambda (kfun body cps)
  968. (let* ((preds (compute-predecessors cps kfun #:labels body))
  969. (defs (compute-defs cps body))
  970. (phis (compute-specializable-phis cps body preds defs)))
  971. (if (eq? phis empty-intmap)
  972. cps
  973. (apply-specialization cps kfun body preds defs phis))))
  974. (compute-reachable-functions cps)
  975. cps))
  976. (define (specialize-numbers cps)
  977. ;; Type inference wants a renumbered graph; OK.
  978. (let ((cps (renumber cps)))
  979. (with-fresh-name-state cps
  980. (specialize-phis (specialize-operations cps)))))