specialize-numbers.scm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. ;;; Continuation-passing style (CPS) intermediate language (IL)
  2. ;; Copyright (C) 2015, 2016, 2017, 2018, 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. ;;; 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. (define (sigbits-union x y)
  215. (and x y (logior x y)))
  216. (define (sigbits-intersect x y)
  217. (cond
  218. ((not x) y)
  219. ((not y) x)
  220. (else (logand x y))))
  221. (define (sigbits-intersect3 a b c)
  222. (sigbits-intersect a (sigbits-intersect b c)))
  223. (define (next-power-of-two n)
  224. (let lp ((out 1))
  225. (if (< n out)
  226. out
  227. (lp (ash out 1)))))
  228. (define (range->sigbits min max)
  229. (cond
  230. ((or (< min 0) (> max #xffffFFFFffffFFFF)) #f)
  231. ((eqv? min max) min)
  232. (else (1- (next-power-of-two max)))))
  233. (define (inferred-sigbits types label var)
  234. (call-with-values (lambda () (lookup-pre-type types label var))
  235. (lambda (type min max)
  236. (and (type<=? type (logior &exact-integer &u64 &s64))
  237. (range->sigbits min max)))))
  238. (define significant-bits-handlers (make-hash-table))
  239. (define-syntax-rule (define-significant-bits-handler
  240. ((primop label types out def ...) arg ...)
  241. body ...)
  242. (hashq-set! significant-bits-handlers 'primop
  243. (lambda (label types out param args defs)
  244. (match args ((arg ...) (match defs ((def ...) body ...)))))))
  245. (define-significant-bits-handler ((logand label types out res) a b)
  246. (let ((sigbits (sigbits-intersect3 (inferred-sigbits types label a)
  247. (inferred-sigbits types label b)
  248. (intmap-ref out res (lambda (_) 0)))))
  249. (intmap-add (intmap-add out a sigbits sigbits-union)
  250. b sigbits sigbits-union)))
  251. (define (significant-bits-handler primop)
  252. (hashq-ref significant-bits-handlers primop))
  253. (define (compute-significant-bits cps types kfun)
  254. "Given the locally inferred types @var{types}, compute a map of VAR ->
  255. BITS indicating the significant bits needed for a variable. BITS may be
  256. #f to indicate all bits, or a non-negative integer indicating a bitmask."
  257. (let ((preds (invert-graph (compute-successors cps kfun))))
  258. (let lp ((worklist (intmap-keys preds)) (visited empty-intset)
  259. (out empty-intmap))
  260. (match (intset-prev worklist)
  261. (#f out)
  262. (label
  263. (let ((worklist (intset-remove worklist label))
  264. (visited* (intset-add visited label)))
  265. (define (continue out*)
  266. (if (and (eq? out out*) (eq? visited visited*))
  267. (lp worklist visited out)
  268. (lp (intset-union worklist (intmap-ref preds label))
  269. visited* out*)))
  270. (define (add-def out var)
  271. (intmap-add out var 0 sigbits-union))
  272. (define (add-defs out vars)
  273. (match vars
  274. (() out)
  275. ((var . vars) (add-defs (add-def out var) vars))))
  276. (define (add-unknown-use out var)
  277. (intmap-add out var (inferred-sigbits types label var)
  278. sigbits-union))
  279. (define (add-unknown-uses out vars)
  280. (match vars
  281. (() out)
  282. ((var . vars)
  283. (add-unknown-uses (add-unknown-use out var) vars))))
  284. (continue
  285. (match (intmap-ref cps label)
  286. (($ $kfun src meta self)
  287. (if self (add-def out self) out))
  288. (($ $kargs names vars term)
  289. (let ((out (add-defs out vars)))
  290. (match term
  291. (($ $continue k src exp)
  292. (match exp
  293. ((or ($ $const) ($ $prim) ($ $fun) ($ $const-fun)
  294. ($ $code) ($ $rec))
  295. ;; No uses, so no info added to sigbits.
  296. out)
  297. (($ $values args)
  298. (match (intmap-ref cps k)
  299. (($ $kargs _ vars)
  300. (if (intset-ref visited k)
  301. (fold (lambda (arg var out)
  302. (intmap-add out arg (intmap-ref out var)
  303. sigbits-union))
  304. out args vars)
  305. out))
  306. (($ $ktail)
  307. (add-unknown-uses out args))))
  308. (($ $call proc args)
  309. (add-unknown-use (add-unknown-uses out args) proc))
  310. (($ $callk label proc args)
  311. (let ((out (add-unknown-uses out args)))
  312. (if proc
  313. (add-unknown-use out proc)
  314. out)))
  315. (($ $primcall name param args)
  316. (let ((h (significant-bits-handler name)))
  317. (if h
  318. (match (intmap-ref cps k)
  319. (($ $kargs _ defs)
  320. (h label types out param args defs)))
  321. (add-unknown-uses out args))))))
  322. (($ $branch kf kt src op param args)
  323. (add-unknown-uses out args))
  324. (($ $prompt k kh src escape? tag)
  325. (add-unknown-use out tag))
  326. (($ $throw src op param args)
  327. (add-unknown-uses out args)))))
  328. (_ out)))))))))
  329. (define (specialize-operations cps)
  330. (define (u6-parameter? param)
  331. (<= 0 param 63))
  332. (define (s64-parameter? param)
  333. (<= (ash -1 63) param (1- (ash 1 63))))
  334. (define (u64-parameter? param)
  335. (<= 0 param (1- (ash 1 64))))
  336. (define (visit-cont label cont cps types sigbits)
  337. (define (operand-in-range? var &type &min &max)
  338. (call-with-values (lambda ()
  339. (lookup-pre-type types label var))
  340. (lambda (type min max)
  341. (and (type<=? type &type) (<= &min min max &max)))))
  342. (define (u64-operand? var)
  343. (operand-in-range? var &exact-integer 0 (1- (ash 1 64))))
  344. (define (u6-operand? var)
  345. ;; This predicate is only used for the "count" argument to
  346. ;; rsh/lsh, which is already unboxed to &u64.
  347. (operand-in-range? var &u64 0 63))
  348. (define (s64-operand? var)
  349. (operand-in-range? var &exact-integer (ash -1 63) (1- (ash 1 63))))
  350. (define (fixnum-operand? var)
  351. (operand-in-range? var &exact-integer
  352. (target-most-negative-fixnum)
  353. (target-most-positive-fixnum)))
  354. (define (exact-integer-operand? var)
  355. (operand-in-range? var &exact-integer -inf.0 +inf.0))
  356. (define (all-u64-bits-set? var)
  357. (operand-in-range? var &exact-integer (1- (ash 1 64)) (1- (ash 1 64))))
  358. (define (only-fixnum-bits-used? var)
  359. (let ((bits (intmap-ref sigbits var)))
  360. (and bits (= bits (logand bits (target-most-positive-fixnum))))))
  361. (define (fixnum-result? result)
  362. (or (only-fixnum-bits-used? result)
  363. (call-with-values
  364. (lambda ()
  365. (lookup-post-type types label result 0))
  366. (lambda (type min max)
  367. (and (type<=? type &exact-integer)
  368. (<= (target-most-negative-fixnum)
  369. min max
  370. (target-most-positive-fixnum)))))))
  371. (define (only-u64-bits-used? var)
  372. (let ((bits (intmap-ref sigbits var)))
  373. (and bits (= bits (logand bits (1- (ash 1 64)))))))
  374. (define (u64-result? result)
  375. (or (only-u64-bits-used? result)
  376. (call-with-values
  377. (lambda ()
  378. (lookup-post-type types label result 0))
  379. (lambda (type min max)
  380. (and (type<=? type &exact-integer)
  381. (<= 0 min max (1- (ash 1 64))))))))
  382. (define (s64-result? result)
  383. (call-with-values
  384. (lambda ()
  385. (lookup-post-type types label result 0))
  386. (lambda (type min max)
  387. (and (type<=? type &exact-integer)
  388. (<= (ash -1 63) min max (1- (ash 1 63)))))))
  389. (define (f64-result? result)
  390. (call-with-values
  391. (lambda ()
  392. (lookup-post-type types label result 0))
  393. (lambda (type min max)
  394. (eqv? type &flonum))))
  395. (define (f64-operands? vara varb)
  396. (let-values (((typea mina maxa) (lookup-pre-type types label vara))
  397. ((typeb minb maxb) (lookup-pre-type types label varb)))
  398. (and (type<=? (logior typea typeb) &real)
  399. (or (eqv? typea &flonum)
  400. (eqv? typeb &flonum)))))
  401. (define (constant-arg arg)
  402. (let-values (((type min max) (lookup-pre-type types label arg)))
  403. (and (= min max) min)))
  404. (define (fixnum-range? min max)
  405. (<= (target-most-negative-fixnum) min max (target-most-positive-fixnum)))
  406. (define (unbox-u64 arg)
  407. (if (fixnum-operand? arg) fixnum->u64 scm->u64))
  408. (define (unbox-s64 arg)
  409. (if (fixnum-operand? arg) untag-fixnum scm->s64))
  410. (define (rebox-s64 arg)
  411. (if (fixnum-operand? arg) tag-fixnum/unlikely s64->scm/unlikely))
  412. (define (unbox-f64 arg)
  413. ;; Could be more precise here.
  414. (if (fixnum-operand? arg) fixnum->f64 scm->f64))
  415. (define (box-s64 result)
  416. (if (fixnum-result? result) tag-fixnum s64->scm))
  417. (define (box-u64 result)
  418. (if (fixnum-result? result) u64->fixnum u64->scm))
  419. (define (box-f64 result)
  420. f64->scm)
  421. (define (specialize-primcall cps k src op param args)
  422. (match (intmap-ref cps k)
  423. (($ $kargs (_) (result))
  424. (match (cons* op result param args)
  425. (((or 'add 'sub 'mul 'div 'atan2)
  426. (? f64-result?) #f a b)
  427. (let ((op (match op
  428. ('add 'fadd) ('sub 'fsub) ('mul 'fmul) ('div 'fdiv)
  429. ('atan2 'fatan2))))
  430. (specialize-binop cps k src op a b
  431. (unbox-f64 a) (unbox-f64 b) (box-f64 result))))
  432. (((or 'sqrt 'abs 'floor 'ceiling 'sin 'cos 'tan 'asin 'acos 'atan)
  433. (? f64-result?) #f a)
  434. (let ((op (match op
  435. ('sqrt 'fsqrt) ('abs 'fabs)
  436. ('floor 'ffloor) ('ceiling 'fceiling)
  437. ('sin 'fsin) ('cos 'fcos) ('tan 'ftan)
  438. ('asin 'fasin) ('acos 'facos) ('atan 'fatan))))
  439. (specialize-unop cps k src op #f a
  440. (unbox-f64 a) (box-f64 result))))
  441. (((or 'add 'sub 'mul 'logand 'logior 'logxor 'logsub)
  442. (? u64-result?) #f (? u64-operand? a) (? u64-operand? b))
  443. (let ((op (match op
  444. ('add 'uadd) ('sub 'usub) ('mul 'umul)
  445. ('logand 'ulogand) ('logior 'ulogior)
  446. ('logxor 'ulogxor) ('logsub 'ulogsub))))
  447. (specialize-binop cps k src op a b
  448. (unbox-u64 a) (unbox-u64 b) (box-u64 result))))
  449. (((or 'logand 'logior 'logxor 'logsub)
  450. (? u64-result?) #f (? s64-operand? a) (? s64-operand? b))
  451. (let ((op (match op
  452. ('logand 'ulogand) ('logior 'ulogior)
  453. ('logxor 'ulogxor) ('logsub 'ulogsub))))
  454. (define (unbox-u64* x)
  455. (let ((unbox-s64 (unbox-s64 x)))
  456. (lambda (cps k src x)
  457. (with-cps cps
  458. (letv s64)
  459. (letk ks64 ($kargs ('s64) (s64)
  460. ($continue k src
  461. ($primcall 's64->u64 #f (s64)))))
  462. ($ (unbox-s64 k src x))))))
  463. (specialize-binop cps k src op a b
  464. (unbox-u64* a) (unbox-u64* b) (box-u64 result))))
  465. (((or 'add 'sub 'mul)
  466. (? s64-result?) #f (? s64-operand? a) (? s64-operand? b))
  467. (let ((op (match op
  468. ('add 'sadd) ('sub 'ssub) ('mul 'smul))))
  469. (specialize-binop cps k src op a b
  470. (unbox-s64 a) (unbox-s64 b) (box-s64 result))))
  471. (('sub/immediate
  472. (? f64-result?) param a)
  473. (specialize-unop cps k src 'fadd/immediate (- param) a
  474. (unbox-f64 a) (box-f64 result)))
  475. (((or 'add/immediate 'mul/immediate)
  476. (? f64-result?) param a)
  477. (let ((op (match op
  478. ('add/immediate 'fadd/immediate)
  479. ('mul/immediate 'fmul/immediate))))
  480. (specialize-unop cps k src op param a
  481. (unbox-f64 a) (box-f64 result))))
  482. (((or 'add/immediate 'sub/immediate 'mul/immediate)
  483. (? u64-result?) (? u64-parameter?) (? u64-operand? a))
  484. (let ((op (match op
  485. ('add/immediate 'uadd/immediate)
  486. ('sub/immediate 'usub/immediate)
  487. ('mul/immediate 'umul/immediate))))
  488. (specialize-unop cps k src op param a
  489. (unbox-u64 a) (box-u64 result))))
  490. (((or 'add/immediate 'sub/immediate 'mul/immediate)
  491. (? s64-result?) (? s64-parameter?) (? s64-operand? a))
  492. (let ((op (match op
  493. ('add/immediate 'sadd/immediate)
  494. ('sub/immediate 'ssub/immediate)
  495. ('mul/immediate 'smul/immediate))))
  496. (specialize-unop cps k src op param a
  497. (unbox-s64 a) (box-s64 result))))
  498. (((or 'lsh 'rsh)
  499. (? u64-result?) #f (? u64-operand? a) (? u6-operand? b))
  500. (let ((op (match op ('lsh 'ulsh) ('rsh 'ursh))))
  501. (define (pass-u64 cps k src b)
  502. (with-cps cps
  503. (build-term ($continue k src ($values (b))))))
  504. (specialize-binop cps k src op a b
  505. (unbox-u64 a) pass-u64 (box-u64 result))))
  506. (((or 'lsh 'rsh)
  507. (? s64-result?) #f (? s64-operand? a) (? u6-operand? b))
  508. (let ((op (match op ('lsh 'slsh) ('rsh 'srsh))))
  509. (define (pass-u64 cps k src b)
  510. (with-cps cps
  511. (build-term ($continue k src ($values (b))))))
  512. (specialize-binop cps k src op a b
  513. (unbox-s64 a) pass-u64 (box-s64 result))))
  514. (((or 'lsh/immediate 'rsh/immediate)
  515. (? u64-result?) (? u6-parameter?) (? u64-operand? a))
  516. (let ((op (match op
  517. ('lsh/immediate 'ulsh/immediate)
  518. ('rsh/immediate 'ursh/immediate))))
  519. (specialize-unop cps k src op param a
  520. (unbox-u64 a) (box-u64 result))))
  521. (((or 'lsh/immediate 'rsh/immediate)
  522. (? s64-result?) (? u6-parameter?) (? s64-operand? a))
  523. (let ((op (match op
  524. ('lsh/immediate 'slsh/immediate)
  525. ('rsh/immediate 'srsh/immediate))))
  526. (specialize-unop cps k src op param a
  527. (unbox-s64 a) (box-s64 result))))
  528. (_ (with-cps cps #f))))
  529. (_ (with-cps cps #f))))
  530. (define (specialize-branch cps kf kt src op param args)
  531. (match (cons op args)
  532. (('<= a b)
  533. (cond
  534. ((f64-operands? a b)
  535. (specialize-comparison cps kf kt src 'f64-<= a b
  536. (unbox-f64 a) (unbox-f64 b)))
  537. ((and (exact-integer-operand? a) (exact-integer-operand? b))
  538. ;; If NaN is impossible, reduce (<= a b) to (not (< b a)) and
  539. ;; try again.
  540. (specialize-branch cps kt kf src '< param (list b a)))
  541. (else
  542. (with-cps cps #f))))
  543. (((or '< '=) a b)
  544. (cond
  545. ((f64-operands? a b)
  546. (let ((op (match op ('= 'f64-=) ('< 'f64-<))))
  547. (specialize-comparison cps kf kt src op a b
  548. (unbox-f64 a) (unbox-f64 b))))
  549. ((and (s64-operand? a) (s64-operand? b))
  550. (cond
  551. ((constant-arg a)
  552. => (lambda (a)
  553. (let ((op (match op ('= 's64-imm-=) ('< 'imm-s64-<))))
  554. (specialize-comparison/immediate cps kf kt src op b a
  555. (unbox-s64 b)))))
  556. ((constant-arg b)
  557. => (lambda (b)
  558. (let ((op (match op ('= 's64-imm-=) ('< 's64-imm-<))))
  559. (specialize-comparison/immediate cps kf kt src op a b
  560. (unbox-s64 a)))))
  561. (else
  562. (let ((op (match op ('= 's64-=) ('< 's64-<))))
  563. (specialize-comparison cps kf kt src op a b
  564. (unbox-s64 a) (unbox-s64 b))))))
  565. ((and (u64-operand? a) (u64-operand? b))
  566. (cond
  567. ((constant-arg a)
  568. => (lambda (a)
  569. (let ((op (match op ('= 'u64-imm-=) ('< 'imm-u64-<))))
  570. (specialize-comparison/immediate cps kf kt src op b a
  571. (unbox-u64 b)))))
  572. ((constant-arg b)
  573. => (lambda (b)
  574. (let ((op (match op ('= 'u64-imm-=) ('< 'u64-imm-<))))
  575. (specialize-comparison/immediate cps kf kt src op a b
  576. (unbox-u64 a)))))
  577. (else
  578. (let ((op (match op ('= 'u64-=) ('< 'u64-<))))
  579. (specialize-comparison cps kf kt src op a b
  580. (unbox-u64 a) (unbox-u64 b))))))
  581. ((and (exact-integer-operand? a) (exact-integer-operand? b))
  582. (cond
  583. ((s64-operand? a)
  584. (cond
  585. ((constant-arg a)
  586. => (lambda (a)
  587. (let ((imm-op (match op ('= 's64-imm-=) ('< 'imm-s64-<))))
  588. (specialize-comparison/immediate-s64-integer
  589. cps kf kt src imm-op a b
  590. (lambda (kf kt src a)
  591. (build-term ($branch kf kt src op #f (a b))))))))
  592. (else
  593. (specialize-comparison/s64-integer cps kf kt src op a b
  594. (unbox-s64 a)
  595. (rebox-s64 a)))))
  596. ((s64-operand? b)
  597. (cond
  598. ((constant-arg b)
  599. => (lambda (b)
  600. (let ((imm-op (match op ('= 's64-imm-=) ('< 's64-imm-<))))
  601. (specialize-comparison/immediate-s64-integer
  602. cps kf kt src imm-op b a
  603. (lambda (kf kt src b)
  604. (build-term ($branch kf kt src op #f (a b))))))))
  605. (else
  606. (specialize-comparison/integer-s64 cps kf kt src op a b
  607. (unbox-s64 b)
  608. (rebox-s64 b)))))
  609. (else (with-cps cps #f))))
  610. (else (with-cps cps #f))))
  611. (_ (with-cps cps #f))))
  612. (match cont
  613. (($ $kfun)
  614. (let* ((types (infer-types cps label))
  615. (sigbits (compute-significant-bits cps types label)))
  616. (values cps types sigbits)))
  617. (($ $kargs names vars ($ $continue k src ($ $primcall op param args)))
  618. (call-with-values
  619. (lambda () (specialize-primcall cps k src op param args))
  620. (lambda (cps term)
  621. (values (if term
  622. (with-cps cps
  623. (setk label ($kargs names vars ,term)))
  624. cps)
  625. types sigbits))))
  626. (($ $kargs names vars ($ $branch kf kt src op param args))
  627. (call-with-values
  628. (lambda () (specialize-branch cps kf kt src op param args))
  629. (lambda (cps term)
  630. (values (if term
  631. (with-cps cps
  632. (setk label ($kargs names vars ,term)))
  633. cps)
  634. types sigbits))))
  635. (_ (values cps types sigbits))))
  636. (values (intmap-fold visit-cont cps cps #f #f)))
  637. ;; Compute a map from VAR -> LABEL, where LABEL indicates the cont that
  638. ;; binds VAR.
  639. (define (compute-defs conts labels)
  640. (intset-fold
  641. (lambda (label defs)
  642. (match (intmap-ref conts label)
  643. (($ $kfun src meta self tail clause)
  644. (if self (intmap-add defs self label) defs))
  645. (($ $kargs names vars)
  646. (fold1 (lambda (var defs)
  647. (intmap-add defs var label))
  648. vars defs))
  649. (_ defs)))
  650. labels empty-intmap))
  651. ;; Compute vars whose definitions are all unboxable and whose uses
  652. ;; include an unbox operation.
  653. (define (compute-specializable-vars cps body preds defs
  654. exp-result-unboxable?
  655. unbox-ops)
  656. ;; Compute a map of VAR->LABEL... indicating the set of labels that
  657. ;; define VAR with unboxable values, given the set of vars
  658. ;; UNBOXABLE-VARS which is known already to be unboxable.
  659. (define (collect-unboxable-def-labels unboxable-vars)
  660. (define (add-unboxable-def unboxable-defs var label)
  661. (intmap-add unboxable-defs var (intset label) intset-union))
  662. (intset-fold (lambda (label unboxable-defs)
  663. (match (intmap-ref cps label)
  664. (($ $kargs _ _ ($ $continue k _ exp))
  665. (match exp
  666. ((? exp-result-unboxable?)
  667. (match (intmap-ref cps k)
  668. (($ $kargs (_) (def))
  669. (add-unboxable-def unboxable-defs def label))))
  670. (($ $values vars)
  671. (match (intmap-ref cps k)
  672. (($ $kargs _ defs)
  673. (fold
  674. (lambda (var def unboxable-defs)
  675. (if (intset-ref unboxable-vars var)
  676. (add-unboxable-def unboxable-defs def label)
  677. unboxable-defs))
  678. unboxable-defs vars defs))
  679. ;; Could be $ktail for $values.
  680. (_ unboxable-defs)))
  681. (_ unboxable-defs)))
  682. (_ unboxable-defs)))
  683. body empty-intmap))
  684. ;; Compute the set of vars which are always unboxable.
  685. (define (compute-unboxable-defs)
  686. (fixpoint
  687. (lambda (unboxable-vars)
  688. (intmap-fold
  689. (lambda (def unboxable-pred-labels unboxable-vars)
  690. (if (and (not (intset-ref unboxable-vars def))
  691. ;; Are all defining expressions unboxable?
  692. (and-map (lambda (pred)
  693. (intset-ref unboxable-pred-labels pred))
  694. (intmap-ref preds (intmap-ref defs def))))
  695. (intset-add unboxable-vars def)
  696. unboxable-vars))
  697. (collect-unboxable-def-labels unboxable-vars)
  698. unboxable-vars))
  699. empty-intset))
  700. ;; Compute the set of vars that may ever be unboxed.
  701. (define (compute-unbox-uses unboxable-defs)
  702. (intset-fold
  703. (lambda (label unbox-uses)
  704. (match (intmap-ref cps label)
  705. (($ $kargs _ _ ($ $continue k _ exp))
  706. (match exp
  707. (($ $primcall (? (lambda (op) (memq op unbox-ops))) #f (var))
  708. (intset-add unbox-uses var))
  709. (($ $values vars)
  710. (match (intmap-ref cps k)
  711. (($ $kargs _ defs)
  712. (fold (lambda (var def unbox-uses)
  713. (if (intset-ref unboxable-defs def)
  714. (intset-add unbox-uses var)
  715. unbox-uses))
  716. unbox-uses vars defs))
  717. (($ $ktail)
  718. ;; Assume return is rare and that any unboxable def can
  719. ;; be reboxed when leaving the procedure.
  720. (fold (lambda (var unbox-uses)
  721. (intset-add unbox-uses var))
  722. unbox-uses vars))))
  723. (_ unbox-uses)))
  724. (_ unbox-uses)))
  725. body empty-intset))
  726. (let ((unboxable-defs (compute-unboxable-defs)))
  727. (intset-intersect unboxable-defs (compute-unbox-uses unboxable-defs))))
  728. ;; Compute vars whose definitions are all inexact reals and whose uses
  729. ;; include an unbox operation.
  730. (define (compute-specializable-f64-vars cps body preds defs)
  731. ;; Can the result of EXP definitely be unboxed as an f64?
  732. (define (exp-result-f64? exp)
  733. (match exp
  734. ((or ($ $primcall 'f64->scm #f (_))
  735. ($ $const (and (? number?) (? inexact?) (? real?))))
  736. #t)
  737. (_ #f)))
  738. (compute-specializable-vars cps body preds defs exp-result-f64? '(scm->f64)))
  739. ;; Compute vars whose definitions are all exact integers in the u64
  740. ;; range and whose uses include an unbox operation.
  741. (define (compute-specializable-u64-vars cps body preds defs)
  742. ;; Can the result of EXP definitely be unboxed as a u64?
  743. (define (exp-result-u64? exp)
  744. (define (u64? n)
  745. (and (number? n) (exact-integer? n)
  746. (<= 0 n #xffffffffffffffff)))
  747. (match exp
  748. ((or ($ $primcall 'u64->scm #f (_))
  749. ($ $primcall 'u64->scm/unlikely #f (_))
  750. ($ $primcall 'load-const/unlikely (? u64?) ())
  751. ($ $const (? u64?)))
  752. #t)
  753. (_ #f)))
  754. (compute-specializable-vars cps body preds defs exp-result-u64?
  755. '(scm->u64 'scm->u64/truncate)))
  756. ;; Compute vars whose definitions are all exact integers in the fixnum
  757. ;; range and whose uses include an untag operation.
  758. (define (compute-specializable-fixnum-vars cps body preds defs)
  759. ;; Is the result of EXP definitely a fixnum?
  760. (define (exp-result-fixnum? exp)
  761. (define (fixnum? n)
  762. (and (number? n) (exact-integer? n)
  763. (<= (target-most-negative-fixnum)
  764. n
  765. (target-most-positive-fixnum))))
  766. (match exp
  767. ((or ($ $primcall 'tag-fixnum #f (_))
  768. ($ $primcall 'tag-fixnum/unlikely #f (_))
  769. ($ $const (? fixnum?))
  770. ($ $primcall 'load-const/unlikely (? fixnum?) ()))
  771. #t)
  772. (_ #f)))
  773. (compute-specializable-vars cps body preds defs exp-result-fixnum?
  774. '(untag-fixnum)))
  775. ;; Compute vars whose definitions are all exact integers in the s64
  776. ;; range and whose uses include an untag operation.
  777. (define (compute-specializable-s64-vars cps body preds defs)
  778. ;; Is the result of EXP definitely a fixnum?
  779. (define (exp-result-fixnum? exp)
  780. (define (s64? n)
  781. (and (number? n) (exact-integer? n)
  782. (<= (ash -1 63) n (1- (ash 1 63)))))
  783. (match exp
  784. ((or ($ $primcall 's64->scm #f (_))
  785. ($ $const (? s64?))
  786. ($ $primcall 'load-const/unlikely (? s64?) ()))
  787. #t)
  788. (_ #f)))
  789. (compute-specializable-vars cps body preds defs exp-result-fixnum?
  790. '(scm->s64)))
  791. (define (compute-phi-vars cps preds)
  792. (intmap-fold (lambda (label preds phis)
  793. (match preds
  794. (() phis)
  795. ((_) phis)
  796. (_
  797. (match (intmap-ref cps label)
  798. (($ $kargs names vars)
  799. (fold1 (lambda (var phis)
  800. (intset-add phis var))
  801. vars phis))
  802. (_ phis)))))
  803. preds empty-intset))
  804. ;; Compute the set of variables which have more than one definition,
  805. ;; whose definitions are always f64-valued or u64-valued, and which have
  806. ;; at least one use that is an unbox operation.
  807. (define (compute-specializable-phis cps body preds defs)
  808. (let ((phi-vars (compute-phi-vars cps preds)))
  809. (fold1 (lambda (in out)
  810. (match in
  811. ((kind vars)
  812. (intset-fold
  813. (lambda (var out)
  814. (intmap-add out var kind (lambda (old new) old)))
  815. (intset-intersect phi-vars vars)
  816. out))))
  817. `((f64 ,(compute-specializable-f64-vars cps body preds defs))
  818. (fx ,(compute-specializable-fixnum-vars cps body preds defs))
  819. (s64 ,(compute-specializable-s64-vars cps body preds defs))
  820. (u64 ,(compute-specializable-u64-vars cps body preds defs)))
  821. empty-intmap)))
  822. ;; Each definition of a f64/u64 variable should unbox that variable.
  823. ;; The cont that binds the variable should re-box it under its original
  824. ;; name, and rely on CSE to remove the boxing as appropriate.
  825. (define (apply-specialization cps kfun body preds defs phis)
  826. (define (compute-unbox-labels)
  827. (intmap-fold (lambda (phi kind labels)
  828. (fold1 (lambda (pred labels)
  829. (intset-add labels pred))
  830. (intmap-ref preds (intmap-ref defs phi))
  831. labels))
  832. phis empty-intset))
  833. (define (unbox-op var)
  834. (match (intmap-ref phis var)
  835. ('f64 'scm->f64)
  836. ('fx 'untag-fixnum)
  837. ('s64 'scm->s64)
  838. ('u64 'scm->u64)))
  839. (define (box-op var)
  840. (match (intmap-ref phis var)
  841. ('f64 'f64->scm)
  842. ('fx 'tag-fixnum)
  843. ('s64 's64->scm)
  844. ('u64 'u64->scm)))
  845. (define (unbox-operands)
  846. (define (unbox-arg cps arg def-var have-arg)
  847. (if (intmap-ref phis def-var (lambda (_) #f))
  848. (with-cps cps
  849. (letv unboxed)
  850. (let$ body (have-arg unboxed))
  851. (letk kunboxed ($kargs ('unboxed) (unboxed) ,body))
  852. (build-term
  853. ($continue kunboxed #f ($primcall (unbox-op def-var) #f (arg)))))
  854. (have-arg cps arg)))
  855. (define (unbox-args cps args def-vars have-args)
  856. (match args
  857. (() (have-args cps '()))
  858. ((arg . args)
  859. (match def-vars
  860. ((def-var . def-vars)
  861. (unbox-arg cps arg def-var
  862. (lambda (cps arg)
  863. (unbox-args cps args def-vars
  864. (lambda (cps args)
  865. (have-args cps (cons arg args)))))))))))
  866. (intset-fold
  867. (lambda (label cps)
  868. (match (intmap-ref cps label)
  869. (($ $kargs names vars ($ $continue k src exp))
  870. (match (intmap-ref cps k)
  871. (($ $kargs _ defs)
  872. (match exp
  873. ;; For expressions that define a single value, we know we need
  874. ;; to unbox that value. For $values though we might have to
  875. ;; unbox just a subset of values.
  876. (($ $values args)
  877. (with-cps cps
  878. (let$ term (unbox-args
  879. args defs
  880. (lambda (cps args)
  881. (with-cps cps
  882. (build-term
  883. ($continue k src ($values args)))))))
  884. (setk label ($kargs names vars ,term))))
  885. (_
  886. (match defs
  887. ((def)
  888. (with-cps cps
  889. (letv boxed)
  890. (letk kunbox ($kargs ('boxed) (boxed)
  891. ($continue k src
  892. ($primcall (unbox-op def) #f (boxed)))))
  893. (setk label ($kargs names vars
  894. ($continue kunbox src ,exp)))))))))))))
  895. (compute-unbox-labels)
  896. cps))
  897. (define (compute-box-labels)
  898. (intmap-fold (lambda (phi kind labels)
  899. (intset-add labels (intmap-ref defs phi)))
  900. phis empty-intset))
  901. (define (box-results cps)
  902. (intset-fold
  903. (lambda (label cps)
  904. (match (intmap-ref cps label)
  905. (($ $kargs names vars term)
  906. (let* ((boxed (fold1 (lambda (var boxed)
  907. (if (intmap-ref phis var (lambda (_) #f))
  908. (intmap-add boxed var (fresh-var))
  909. boxed))
  910. vars empty-intmap))
  911. (bound-vars (map (lambda (var)
  912. (intmap-ref boxed var (lambda (var) var)))
  913. vars)))
  914. (define (box-var cps name var done)
  915. (let ((unboxed (intmap-ref boxed var (lambda (_) #f))))
  916. (if unboxed
  917. (with-cps cps
  918. (let$ term (done))
  919. (letk kboxed ($kargs (name) (var) ,term))
  920. (build-term
  921. ($continue kboxed #f
  922. ($primcall (box-op var) #f (unboxed)))))
  923. (done cps))))
  924. (define (box-vars cps names vars done)
  925. (match vars
  926. (() (done cps))
  927. ((var . vars)
  928. (match names
  929. ((name . names)
  930. (box-var cps name var
  931. (lambda (cps)
  932. (box-vars cps names vars done))))))))
  933. (with-cps cps
  934. (let$ box-term (box-vars names vars
  935. (lambda (cps)
  936. (with-cps cps term))))
  937. (setk label ($kargs names bound-vars ,box-term)))))))
  938. (compute-box-labels)
  939. cps))
  940. (box-results (unbox-operands)))
  941. (define (specialize-phis cps)
  942. (intmap-fold
  943. (lambda (kfun body cps)
  944. (let* ((preds (compute-predecessors cps kfun #:labels body))
  945. (defs (compute-defs cps body))
  946. (phis (compute-specializable-phis cps body preds defs)))
  947. (if (eq? phis empty-intmap)
  948. cps
  949. (apply-specialization cps kfun body preds defs phis))))
  950. (compute-reachable-functions cps)
  951. cps))
  952. (define (specialize-numbers cps)
  953. ;; Type inference wants a renumbered graph; OK.
  954. (let ((cps (renumber cps)))
  955. (with-fresh-name-state cps
  956. (specialize-phis (specialize-operations cps)))))