specialize-numbers.scm 43 KB

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