summaryrefslogtreecommitdiff
path: root/module/language/cps/type-fold.scm
blob: 63d47db8f7928dd11d73a4720afedef4b2839324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
;;; Abstract constant folding on CPS
;;; Copyright (C) 2014-2020, 2023 Free Software Foundation, Inc.
;;;
;;; This library is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;; 
;;; This library is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;; 
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;;; This pass uses the abstract interpretation provided by type analysis
;;; to fold constant values and type predicates.  It is most profitably
;;; run after CSE, to take advantage of scalar replacement.
;;;
;;; Code:

(define-module (language cps type-fold)
  #:use-module (ice-9 match)
  #:use-module (language cps)
  #:use-module (language cps utils)
  #:use-module (language cps renumber)
  #:use-module (language cps types)
  #:use-module (language cps with-cps)
  #:use-module (language cps intmap)
  #:use-module (language cps intset)
  #:use-module (system base target)
  #:export (type-fold))




;; Branch folders.

(define &scalar-types
  (logior &fixnum &bignum &flonum &char &special-immediate))

(define (materialize-constant type min max kt kf)
  (cond
   ((zero? type) (kf))
   ((not (and (zero? (logand type (1- type)))
              (zero? (logand type (lognot &scalar-types)))
              (eqv? min max))) (kf))
   ((eqv? type &fixnum) (kt min))
   ((eqv? type &bignum) (kt min))
   ((eqv? type &flonum) (kt (exact->inexact min)))
   ((eqv? type &char) (kt (integer->char min)))
   ((eqv? type &special-immediate)
    (cond
     ((eqv? min &null) (kt '()))
     ((eqv? min &nil) (kt #nil))
     ((eqv? min &false) (kt #f))
     ((eqv? min &true) (kt #t))
     ((eqv? min &unspecified) (kt *unspecified*))
     ;; FIXME: &undefined here
     ((eqv? min &eof) (kt the-eof-object))
     (else (kf))))
   (else (kf))))

(define *branch-folders* (make-hash-table))

(define-syntax-rule (define-branch-folder op f)
  (hashq-set! *branch-folders* 'op f))

(define-syntax-rule (define-branch-folder-alias to from)
  (hashq-set! *branch-folders* 'to (hashq-ref *branch-folders* 'from)))

(define-syntax-rule (define-unary-branch-folder* (op param arg min max)
                      body ...)
  (define-branch-folder op (lambda (param arg min max) body ...)))

(define-syntax-rule (define-unary-branch-folder (op arg min max) body ...)
  (define-unary-branch-folder* (op param arg min max) body ...))

(define-syntax-rule (define-binary-branch-folder (op arg0 min0 max0
                                                       arg1 min1 max1)
                      body ...)
  (define-branch-folder op (lambda (param arg0 min0 max0 arg1 min1 max1) body ...)))

(define (fold-eq-constant? ctype cval type min max)
  (cond
   ((zero? (logand type ctype)) (values #t #f))
   ((eqv? type ctype)
    (cond
     ((or (< cval min) (< max cval)) (values #t #f))
     ((= cval min max) (values #t #t))
     (else (values #f #f))))
   (else (values #f #f))))
(define-unary-branch-folder* (eq-constant? param type min max)
  (call-with-values (lambda () (constant-type param))
    (lambda (ctype cval cval*)
      ;; cval either equals cval* or is meaningless.
      (fold-eq-constant? ctype cval type min max))))

(define-unary-branch-folder (undefined? type min max)
  (fold-eq-constant? &special-immediate &undefined type min max))

(define-syntax-rule (define-nullish-predicate-folder op imin imax)
  (define-unary-branch-folder (op type min max)
    (let ((type* (logand type &special-immediate)))
      (cond
       ((zero? (logand type &special-immediate)) (values #t #f))
       ((eqv? type &special-immediate)
        (cond
         ((or (< imax min) (< max imin)) (values #t #f))
         ((<= imin min max imax) (values #t #t))
         (else (values #f #f))))
       (else (values #f #f))))))

(define-nullish-predicate-folder null? &null &nil)
(define-nullish-predicate-folder false? &nil &false)
(define-nullish-predicate-folder nil? &null &false) ;; &nil in middle

(define-syntax-rule (define-unary-type-predicate-folder op &type)
  (define-unary-branch-folder (op type min max)
    (let ((type* (logand type &type)))
      (cond
       ((zero? type*) (values #t #f))
       ((eqv? type type*) (values #t #t))
       (else (values #f #f))))))

(define-unary-branch-folder (heap-object? type min max)
  (define &immediate-types (logior &fixnum &char &special-immediate))
  (cond
   ((zero? (logand type &immediate-types)) (values #t #t))
   ((type<=? type &immediate-types) (values #t #f))
   (else (values #f #f))))

;; All the cases that are in compile-bytecode.
(define-unary-type-predicate-folder bignum? &bignum)
(define-unary-type-predicate-folder bitvector? &bitvector)
(define-unary-type-predicate-folder bytevector? &bytevector)
(define-unary-type-predicate-folder char? &char)
(define-unary-type-predicate-folder compnum? &complex)
(define-unary-type-predicate-folder fixnum? &fixnum)
(define-unary-type-predicate-folder flonum? &flonum)
(define-unary-type-predicate-folder fluid? &fluid)
(define-unary-type-predicate-folder fracnum? &fraction)
(define-unary-type-predicate-folder immutable-vector? &immutable-vector)
(define-unary-type-predicate-folder keyword? &keyword)
(define-unary-type-predicate-folder mutable-vector? &mutable-vector)
(define-unary-type-predicate-folder pair? &pair)
(define-unary-type-predicate-folder pointer? &pointer)
(define-unary-type-predicate-folder program? &procedure)
(define-unary-type-predicate-folder string? &string)
(define-unary-type-predicate-folder struct? &struct)
(define-unary-type-predicate-folder symbol? &symbol)
(define-unary-type-predicate-folder syntax? &syntax)
(define-unary-type-predicate-folder variable? &box)

(define-unary-branch-folder (vector? type min max)
  (cond
   ((zero? (logand type &vector)) (values #t #f))
   ((type<=? type &vector) (values #t #t))
   (else (values #f #f))))

(define-unary-branch-folder (procedure? type min max)
  (define applicable-types (logior &procedure &struct &other-heap-object))
  (cond
   ((zero? (logand type applicable-types)) (values #t #f))
   ((= type &procedure) (values #t #t))
   (else (values #f #f))))

(let ((&heap-number (logior &bignum &flonum &fraction &complex)))
  (define-unary-type-predicate-folder heap-number? &heap-number))
(define-unary-type-predicate-folder number? &number)
(define-unary-type-predicate-folder complex? &number)
(define-unary-type-predicate-folder real? &real)
(define-unary-type-predicate-folder exact-integer? &exact-integer)
(define-unary-type-predicate-folder exact? &exact-number)
(let ((&inexact (logior &flonum &complex)))
  (define-unary-type-predicate-folder inexact? &inexact))

(define-unary-branch-folder (rational? type min max)
  (cond
   ((zero? (logand type &number)) (values #t #f))
   ((eqv? type (logand type &exact-number)) (values #t #t))
   (else (values #f #f))))

(define-unary-branch-folder (integer? type min max)
  (cond
   ((zero? (logand type &number)) (values #t #f))
   ((eqv? type (logand type &exact-integer)) (values #t #t))
   (else (values #f #f))))

(define-binary-branch-folder (eq? type0 min0 max0 type1 min1 max1)
  (cond
   ((or (zero? (logand type0 type1)) (< max0 min1) (< max1 min0))
    (values #t #f))
   ((and (eqv? type0 type1)
         (eqv? min0 min1 max0 max1)
         (zero? (logand type0 (1- type0)))
         (not (zero? (logand type0 &scalar-types))))
    (values #t #t))
   (else
    (values #f #f))))
(define-branch-folder-alias heap-numbers-equal? eq?)

(define (compare-exact-ranges min0 max0 min1 max1)
  (and (cond ((< max0 min1) '<)
             ((> min0 max1) '>)
             ((= min0 max0 min1 max1) '=)
             ((<= max0 min1) '<=)
             ((>= min0 max1) '>=)
             (else #f))))

(define-binary-branch-folder (< type0 min0 max0 type1 min1 max1)
  (if (type<=? (logior type0 type1) &exact-number)
      (case (compare-exact-ranges min0 max0 min1 max1)
        ((<) (values #t #t))
        ((= >= >) (values #t #f))
        (else (values #f #f)))
      (values #f #f)))
(define-binary-branch-folder (u64-< type0 min0 max0 type1 min1 max1)
  (case (compare-exact-ranges min0 max0 min1 max1)
    ((<) (values #t #t))
    ((= >= >) (values #t #f))
    (else (values #f #f))))
(define-branch-folder-alias s64-< u64-<)
;; We currently cannot define branch folders for floating point
;; comparison ops like the commented one below because we can't prove
;; there are no nans involved.
;;
;; (define-branch-folder-alias f64-< <)

(define-binary-branch-folder (<= type0 min0 max0 type1 min1 max1)
  (if (type<=? (logior type0 type1) &exact-number)
      (case (compare-exact-ranges min0 max0 min1 max1)
        ((< <= =) (values #t #t))
        ((>) (values #t #f))
        (else (values #f #f)))
      (values #f #f)))

(define-unary-branch-folder* (u64-imm-= c type min max)
  (cond
   ((= c min max) (values #t #t))
   ((<= min c max) (values #f #f))
   (else (values #t #f))))
(define-branch-folder-alias s64-imm-= u64-imm-=)

(define-unary-branch-folder* (u64-imm-< c type min max)
  (cond
   ((< max c) (values #t #t))
   ((>= min c) (values #t #f))
   (else (values #f #f))))
(define-branch-folder-alias s64-imm-< u64-imm-<)

(define-unary-branch-folder* (imm-u64-< c type min max)
  (cond
   ((< c min) (values #t #t))
   ((>= c max) (values #t #f))
   (else (values #f #f))))
(define-branch-folder-alias imm-s64-< imm-u64-<)

(define-binary-branch-folder (= type0 min0 max0 type1 min1 max1)
  (cond
   ((not (type<=? (logior type0 type1) &exact-number))
    (values #f #f))
   ((zero? (logand type0 type1))
    ;; If both values are exact but of different types, they are not
    ;; equal.
    (values #t #f))
   (else
    (case (compare-exact-ranges min0 max0 min1 max1)
      ((=) (values #t #t))
      ((< >) (values #t #f))
      (else (values #f #f))))))
(define-binary-branch-folder (u64-= type0 min0 max0 type1 min1 max1)
  (case (compare-exact-ranges min0 max0 min1 max1)
    ((=) (values #t #t))
    ((< >) (values #t #f))
    (else (values #f #f))))
(define-branch-folder-alias s64-= u64-=)




(define *branch-reducers* (make-hash-table))

(define-syntax-rule (define-branch-reducer op f)
  (hashq-set! *branch-reducers* 'op f))

(define-syntax-rule (define-branch-reducer-aliases def use ...)
  (let ((proc (or (hashq-ref *branch-reducers* 'def)
                  (error "not found" 'def))))
    (define-branch-reducer use proc)
    ...))

(define-syntax-rule (define-unary-branch-reducer
                      (op cps kf kt src arg type min max)
                      body ...)
  (define-branch-reducer op
    (lambda (cps kf kt src param arg type min max)
      body ...)))

(define-syntax-rule (define-binary-branch-reducer
                      (op cps kf kt src
                          arg0 type0 min0 max0
                          arg1 type1 min1 max1)
                      body ...)
  (define-branch-reducer op
    (lambda (cps kf kt src param arg0 type0 min0 max0 arg1 type1 min1 max1)
      body ...)))

(define-unary-branch-reducer (number? cps kf kt src arg type min max)
  (let ((number-types (logand type &number)))
    (when (or (zero? number-types) (eqv? type number-types))
      (error "should have folded!"))
    (define-syntax-rule (define-heap-number-test test &type pred next-test)
      (define (test cps)
        (if (logtest type &type)
            (with-cps cps
              (let$ kf (next-test))
              (letk k ($kargs () ()
                        ($branch kf kt src 'pred #f (arg))))
              k)
            (next-test cps))))
    (define (done cps) (with-cps cps kf))
    (define-heap-number-test compnum-test &complex compnum? done)
    (define-heap-number-test fracnum-test &fraction fracnum? compnum-test)
    (define-heap-number-test bignum-test &bignum bignum? fracnum-test)
    (define-heap-number-test flonum-test &flonum flonum? bignum-test)
    (define (heap-number-tests cps) (flonum-test cps))
    (cond
     ((eqv? number-types &number)
      ;; Generic: no reduction.
      (with-cps cps #f))
     ((eqv? number-types &fixnum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'fixnum? #f (arg)))))
     ((logtest type &fixnum)
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (letk kheap ($kargs () ()
                      ($branch kf ktest src 'heap-object? #f (arg))))
        (build-term
          ($branch kheap kt src 'fixnum? #f (arg)))))
     (else
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (build-term
          ($branch kf ktest src 'heap-object? #f (arg))))))))
(define-branch-reducer-aliases number? complex?)

(define-unary-branch-reducer (real? cps kf kt src arg type min max)
  (let ((real-types (logand type &real)))
    (when (or (zero? real-types) (eqv? type real-types))
      (error "should have folded!"))
    (define-syntax-rule (define-heap-number-test test &type pred next-test)
      (define (test cps)
        (if (logtest type &type)
            (with-cps cps
              (let$ kf (next-test))
              (letk k ($kargs () ()
                        ($branch kf kt src 'pred #f (arg))))
              k)
            (next-test cps))))
    (define (done cps) (with-cps cps kf))
    (define-heap-number-test fracnum-test &fraction fracnum? done)
    (define-heap-number-test bignum-test &bignum bignum? fracnum-test)
    (define-heap-number-test flonum-test &flonum flonum? bignum-test)
    (define (heap-number-tests cps) (flonum-test cps))
    (cond
     ((eqv? real-types &real)
      ;; Generic: no reduction.
      (with-cps cps #f))
     ((eqv? real-types &fixnum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'fixnum? #f (arg)))))
     ((logtest type &fixnum)
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (letk kheap ($kargs () ()
                      ($branch kf ktest src 'heap-object? #f (arg))))
        (build-term
          ($branch kheap kt src 'fixnum? #f (arg)))))
     (else
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (build-term
          ($branch kf ktest src 'heap-object? #f (arg))))))))

(define-unary-branch-reducer (rational? cps kf kt src arg type min max)
  (let ((number-types (logand type &number)))
    (when (or (zero? number-types) (eqv? type (logand type &exact-number)))
      (error "should have folded!"))
    (define-syntax-rule (define-heap-number-test test &type pred next-test)
      (define (test cps)
        (if (logtest type &type)
            (with-cps cps
              (let$ kf (next-test))
              (letk k ($kargs () ()
                        ($branch kf kt src 'pred #f (arg))))
              k)
            (next-test cps))))
    (define (done cps) (with-cps cps kf))
    (define-heap-number-test fracnum-test &fraction fracnum? done)
    (define-heap-number-test bignum-test &bignum bignum? fracnum-test)
    (define (heap-number-tests cps) (bignum-test cps))
    (cond
     ((logtest type (logior &complex &flonum))
      ;; Too annoying to inline inf / nan tests.
      (with-cps cps #f))
     ((eqv? number-types &fixnum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'fixnum? #f (arg)))))
     ((logtest type &fixnum)
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (letk kheap ($kargs () ()
                      ($branch kf ktest src 'heap-object? #f (arg))))
        (build-term
          ($branch kheap kt src 'fixnum? #f (arg)))))
     (else
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (build-term
          ($branch kf ktest src 'heap-object? #f (arg))))))))

(define-unary-branch-reducer (integer? cps kf kt src arg type min max)
  (define &integer-types (logior &fixnum &bignum &flonum &complex))
  (let ((integer-types (logand type &integer-types)))
    (when (or (zero? integer-types) (eqv? type (logand type &exact-integer)))
      (error "should have folded!"))
    (define-syntax-rule (define-heap-number-test test &type pred next-test)
      (define (test cps)
        (if (logtest type &type)
            (with-cps cps
              (let$ kf (next-test))
              (letk k ($kargs () ()
                        ($branch kf kt src 'pred #f (arg))))
              k)
            (next-test cps))))
    (define (done cps) (with-cps cps kf))
    (define-heap-number-test bignum-test &bignum bignum? done)
    (define (heap-number-tests cps) (bignum-test cps))
    (cond
     ((logtest type (logior &complex &flonum))
      ;; Too annoying to inline integer tests.
      (with-cps cps #f))
     ((eqv? integer-types &fixnum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'fixnum? #f (arg)))))
     ((logtest type &fixnum)
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (letk kheap ($kargs () ()
                      ($branch kf ktest src 'heap-object? #f (arg))))
        (build-term
          ($branch kheap kt src 'fixnum? #f (arg)))))
     (else
      (with-cps cps
        (let$ ktest (heap-number-tests))
        (build-term
          ($branch kf ktest src 'heap-object? #f (arg))))))))

(define-unary-branch-reducer (exact-integer? cps kf kt src arg type min max)
  (let ((integer-types (logand type &exact-integer)))
    (when (or (zero? integer-types) (eqv? type integer-types))
      (error "should have folded!"))
    (cond
     ((eqv? integer-types &fixnum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'fixnum? #f (arg)))))
     ((eqv? integer-types &bignum)
      (with-cps cps
        (letk kbig? ($kargs () ()
                      ($branch kf kt src 'bignum? #f (arg))))
        (build-term
          ($branch kf kbig? src 'heap-object? #f (arg)))))
     (else
      ;; No reduction.
      (with-cps cps #f)))))

(define-unary-branch-reducer (exact? cps kf kt src arg type min max)
  (let ((exact-types (logand type &exact-number)))
    (when (or (zero? exact-types) (eqv? type exact-types))
      (error "should have folded!"))
    ;; We have already passed a number? check, so we can assume either
    ;; fixnum or heap number.
    (define-syntax-rule (define-number-test test &type pred next-test)
      (define (test cps)
        (if (logtest type &type)
            (with-cps cps
              (let$ kf (next-test))
              (letk k ($kargs () ()
                        ($branch kf kt src 'pred #f (arg))))
              k)
            (next-test cps))))
    (define (done cps) (with-cps cps kf))
    (define-number-test fracnum-test &fraction fracnum? done)
    (define-number-test bignum-test &bignum bignum? fracnum-test)
    (define-number-test fixnum-test &fixnum fixnum? bignum-test)
    (define (number-tests cps) (fixnum-test cps))
    (cond
     ((eqv? exact-types &exact-number)
      ;; Generic: no reduction.
      (with-cps cps #f))
     (else
      (with-cps cps
        (let$ ktest (number-tests))
        (build-term
          ($continue ktest #f ($values ()))))))))

(define-unary-branch-reducer (inexact? cps kf kt src arg type min max)
  (define &inexact-number (logior &flonum &complex))
  (let ((inexact-types (logand type &inexact-number)))
    (when (or (zero? inexact-types) (eqv? type inexact-types))
      (error "should have folded!"))
    ;; We have already passed a number? check, so we can assume either
    ;; fixnum or heap number.
    (cond
     ((eqv? (logand type &exact-number) &fixnum)
      (with-cps cps
        (build-term
          ($branch kt kf src 'fixnum? #f (arg)))))
     ((logtest type &fixnum)
      (cond
       ((eqv? inexact-types &flonum)
        (with-cps cps
          (letk kflo ($kargs () ()
                       ($branch kf kt src 'flonum? #f (arg))))
          (build-term
            ($branch kflo kf src 'fixnum? #f (arg)))))
       ((eqv? inexact-types &complex)
        (with-cps cps
          (letk kcomp ($kargs () ()
                        ($branch kf kt src 'compnum? #f (arg))))
          (build-term
            ($branch kcomp kf src 'fixnum? #f (arg)))))
       (else
        ;; Generic: no reduction.
        (with-cps cps #f))))
     ((eqv? inexact-types &flonum)
      (with-cps cps
        (build-term
          ($branch kf kt src 'flonum? #f (arg)))))
     ((eqv? inexact-types &complex)
      (with-cps cps
        (build-term
          ($branch kf kt src 'compnum? #f (arg)))))
     (else
      ;; Still specialize, as we avoid heap-object?.
      (with-cps cps
        (letk kcomp ($kargs () ()
                      ($branch kf kt src 'compnum? #f (arg))))
        (build-term
          ($branch kcomp kt src 'flonum? #f (arg))))))))

(define-binary-branch-reducer (eq? cps kf kt src
                                   arg0 type0 min0 max0
                                   arg1 type1 min1 max1)
  (materialize-constant
   type0 min0 max0
   (lambda (const)
     (with-cps cps
       (build-term
         ($branch kf kt src 'eq-constant? const (arg1)))))
   (lambda ()
     (materialize-constant
      type1 min1 max1
      (lambda (const)
        (with-cps cps
          (build-term
            ($branch kf kt src 'eq-constant? const (arg0)))))
      (lambda () (with-cps cps #f))))))




;; Convert e.g. rsh to rsh/immediate.

(define *primcall-macro-reducers* (make-hash-table))

(define-syntax-rule (define-primcall-macro-reducer op f)
  (hashq-set! *primcall-macro-reducers* 'op f))

(define-syntax-rule (define-unary-primcall-macro-reducer (op cps k src
                                                             arg type min max)
                      body ...)
  (define-primcall-macro-reducer op
    (lambda (cps k src param arg type min max)
      body ...)))

(define-syntax-rule (define-binary-primcall-macro-reducer
                      (op cps k src
                          arg0 type0 min0 max0
                          arg1 type1 min1 max1)
                      body ...)
  (define-primcall-macro-reducer op
    (lambda (cps k src param arg0 type0 min0 max0 arg1 type1 min1 max1)
      body ...)))

(define-binary-primcall-macro-reducer (mul cps k src
                                           arg0 type0 min0 max0
                                           arg1 type1 min1 max1)
  (cond
   ((and (type<=? type0 &exact-integer) (= min0 max0))
    (with-cps cps
      (build-term
        ($continue k src ($primcall 'mul/immediate min0 (arg1))))))
   ((and (type<=? type1 &exact-integer) (= min1 max1))
    (with-cps cps
      (build-term
        ($continue k src ($primcall 'mul/immediate min1 (arg0))))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-macro-reducer (lsh cps k src
                                           arg0 type0 min0 max0
                                           arg1 type1 min1 max1)
  (cond
   ((= min1 max1)
    (with-cps cps
      (build-term
        ($continue k src ($primcall 'lsh/immediate min1 (arg0))))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-macro-reducer (rsh cps k src
                                           arg0 type0 min0 max0
                                           arg1 type1 min1 max1)
  (cond
   ((= min1 max1)
    (with-cps cps
      (build-term
        ($continue k src ($primcall 'rsh/immediate min1 (arg0))))))
   (else
    (with-cps cps #f))))



;; Strength reduction.

(define *primcall-reducers* (make-hash-table))

(define-syntax-rule (define-primcall-reducer op f)
  (hashq-set! *primcall-reducers* 'op f))

(define-syntax-rule (define-unary-primcall-reducer (op cps k src param
                                                    arg type min max)
                      body ...)
  (define-primcall-reducer op
    (lambda (cps k src param arg type min max)
      body ...)))

(define-syntax-rule (define-binary-primcall-reducer (op cps k src param
                                                     arg0 type0 min0 max0
                                                     arg1 type1 min1 max1)
                      body ...)
  (define-primcall-reducer op
    (lambda (cps k src param arg0 type0 min0 max0 arg1 type1 min1 max1)
      body ...)))

(define (power-of-two? constant)
  (and (positive? constant)
       (zero? (logand constant (1- constant)))))

(define-binary-primcall-reducer (quo cps k src param
                                     arg0 type0 min0 max0
                                     arg1 type1 min1 max1)
  (cond
   ((not (type<=? (logior type0 type1) &exact-integer))
    (with-cps cps #f))
   ((and (eqv? type1 &fixnum) (eqv? min1 max1) (power-of-two? min1))
    (with-cps cps
      (build-term
        ($continue k src
          ($primcall 'rsh/immediate (logcount (1- min1)) (arg0))))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-reducer (rem cps k src param
                                     arg0 type0 min0 max0
                                     arg1 type1 min1 max1)
  (cond
   ((not (type<=? (logior type0 type1) &exact-integer))
    (with-cps cps #f))
   ((and (eqv? type1 &fixnum) (eqv? min1 max1) (power-of-two? min1)
         (<= 0 min0))
    (with-cps cps
      (build-term
        ($continue k src
          ($primcall 'logand/immediate (1- min1) (arg0))))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-reducer (mod cps k src param
                                     arg0 type0 min0 max0
                                     arg1 type1 min1 max1)
  (cond
   ((not (type<=? (logior type0 type1) &exact-integer))
    (with-cps cps #f))
   ((and (eqv? type1 &fixnum) (eqv? min1 max1) (power-of-two? min1))
    (with-cps cps
      (build-term
        ($continue k src
          ($primcall 'logand/immediate (1- min1) (arg0))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (mul/immediate cps k src constant
                                              arg type min max)
  (cond
   ((not (type<=? type &number))
    (with-cps cps #f))
   ((eqv? constant -1)
    ;; (* arg -1) -> (- 0 arg)
    (with-cps cps
      ($ (with-cps-constants ((zero 0))
           (build-term
             ($continue k src ($primcall 'sub #f (zero arg))))))))
   ((and (eqv? constant 0) (type<=? type &exact-number))
    ;; (* arg 0) -> 0 if arg is exact
    (with-cps cps
      (build-term ($continue k src ($const 0)))))
   ((eqv? constant 1)
    ;; (* arg 1) -> arg
    (with-cps cps
      (build-term ($continue k src ($values (arg))))))
   ((eqv? constant 2)
    ;; (* arg 2) -> (+ arg arg)
    (with-cps cps
      (build-term ($continue k src ($primcall 'add #f (arg arg))))))
   ((and (type<=? type &exact-integer)
         (positive? constant)
         (zero? (logand constant (1- constant))))
    ;; (* arg power-of-2) -> (lsh arg (log2 power-of-2))
    (let ((n (let lp ((bits 0) (constant constant))
               (if (= constant 1) bits (lp (1+ bits) (ash constant -1))))))
      (with-cps cps
        (build-term ($continue k src ($primcall 'lsh/immediate n (arg)))))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-reducer (logbit? cps k src param
                                         arg0 type0 min0 max0
                                         arg1 type1 min1 max1)
  (define (compute-mask cps kmask src)
    (if (eq? min0 max0)
        (with-cps cps
          (build-term
            ($continue kmask src ($const (ash 1 min0)))))
        (with-cps cps
          ($ (with-cps-constants ((one 1))
               (letv n)
               (letk kn ($kargs ('n) (n)
                          ($continue kmask src
                            ($primcall 'lsh #f (one n)))))
               (build-term
                 ($continue kn src ($primcall 'untag-fixnum #f (arg0)))))))))
  (cond
   ((and (type<=? type0 &exact-integer)
         (<= 0 min0 (target-most-positive-fixnum))
         (<= 0 max0 (target-most-positive-fixnum)))
    (with-cps cps
      (letv mask res u64)
      (letk kt ($kargs () () ($continue k src ($const #t))))
      (letk kf ($kargs () () ($continue k src ($const #f))))
      (letk ku64 ($kargs (#f) (u64)
                   ($branch kt kf src 's64-imm-= 0 (u64))))
      (letk kand ($kargs (#f) (res)
                   ($continue ku64 src ($primcall 'untag-fixnum #f (res)))))
      (letk kmask ($kargs (#f) (mask)
                    ($continue kand src
                      ($primcall 'logand #f (mask arg1)))))
      ($ (compute-mask kmask src))))
   (else
    (with-cps cps #f))))

(define-binary-primcall-reducer (logior cps k src param
                                        arg0 type0 min0 max0
                                        arg1 type1 min1 max1)
  (cond
   ((type<=? (logior type0 type1) &exact-integer)
    (cond
     ((= 0 min0 max0)
      (with-cps cps
        (build-term
          ($continue k src ($values (arg1))))))
     ((= 0 min1 max1)
      (with-cps cps
        (build-term
          ($continue k src ($values (arg0))))))
     (else
      (with-cps cps #f))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (u64->scm cps k src constant arg type min max)
  (cond
   ((<= max (target-most-positive-fixnum))
    (with-cps cps
      (letv s64)
      (letk ks64 ($kargs ('s64) (s64)
                   ($continue k src
                     ($primcall 'tag-fixnum #f (s64)))))
      (build-term
        ($continue ks64 src
          ($primcall 'u64->s64 #f (arg))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (s64->scm cps k src constant arg type min max)
  (cond
   ((<= (target-most-negative-fixnum) min max (target-most-positive-fixnum))
    (with-cps cps
      (build-term
        ($continue k src
          ($primcall 'tag-fixnum #f (arg))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (scm->s64 cps k src constant arg type min max)
  (cond
   ((and (type<=? type &exact-integer)
         (<= (target-most-negative-fixnum) min max (target-most-positive-fixnum)))
    (with-cps cps
      (build-term
        ($continue k src
          ($primcall 'untag-fixnum #f (arg))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (scm->u64 cps k src constant arg type min max)
  (cond
   ((and (type<=? type &exact-integer)
         (<= 0 min max (target-most-positive-fixnum)))
    (with-cps cps
      (letv s64)
      (letk ks64 ($kargs ('s64) (s64)
                   ($continue k src
                     ($primcall 's64->u64 #f (s64)))))
      (build-term
        ($continue ks64 src
          ($primcall 'untag-fixnum #f (arg))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (scm->f64 cps k src constant arg type min max)
  (cond
   ((and (type<=? type &exact-integer)
         (<= (target-most-negative-fixnum) min max (target-most-positive-fixnum)))
    (with-cps cps
      (letv s64)
      (letk ks64 ($kargs ('s64) (s64)
                   ($continue k src
                     ($primcall 's64->f64 #f (s64)))))
      (build-term
        ($continue ks64 src
          ($primcall 'untag-fixnum #f (arg))))))
   (else
    (with-cps cps #f))))

(define-unary-primcall-reducer (inexact cps k src constant arg type min max)
  (cond
   ((and (type<=? type &exact-integer)
         (<= (target-most-negative-fixnum) min max (target-most-positive-fixnum)))
    (with-cps cps
      (letv s64 f64)
      (letk kf64 ($kargs ('f64) (f64)
                   ($continue k src
                     ($primcall 'f64->scm #f (f64)))))
      (letk ks64 ($kargs ('s64) (s64)
                   ($continue kf64 src
                     ($primcall 's64->f64 #f (s64)))))
      (build-term
        ($continue ks64 src
          ($primcall 'untag-fixnum #f (arg))))))
   ((type<=? type &flonum)
    (with-cps cps
      (build-term
        ($continue k src ($primcall 'values #f (arg))))))
   (else
    (with-cps cps #f))))



(define (local-type-fold start end cps)
  (let ((types (infer-types cps start)))
    (define (fold-primcall cps label names vars k src op param args def)
      (call-with-values (lambda () (lookup-post-type types label def 0))
        (lambda (type min max)
          (materialize-constant
           type min max
           (lambda (val)
             ;; (pk 'folded src op args val)
             (with-cps cps
               (letv v*)
               (letk k* ($kargs (#f) (v*)
                          ($continue k src ($const val))))
               ;; Rely on DCE to elide this expression, if possible.
               (setk label
                     ($kargs names vars
                       ($continue k* src ($primcall op param args))))))
           (lambda () #f)))))
    (define (transform-primcall f cps label names vars k src op param args)
      (and f
           (match args
             ((arg0)
              (call-with-values (lambda () (lookup-pre-type types label arg0))
                (lambda (type0 min0 max0)
                  (call-with-values (lambda ()
                                      (f cps k src param arg0 type0 min0 max0))
                    (lambda (cps term)
                      (and term
                           (with-cps cps
                             (setk label ($kargs names vars ,term)))))))))
             ((arg0 arg1)
              (call-with-values (lambda () (lookup-pre-type types label arg0))
                (lambda (type0 min0 max0)
                  (call-with-values (lambda () (lookup-pre-type types label arg1))
                    (lambda (type1 min1 max1)
                      (call-with-values (lambda ()
                                          (f cps k src param arg0 type0 min0 max0
                                             arg1 type1 min1 max1))
                        (lambda (cps term)
                          (and term
                               (with-cps cps
                                 (setk label ($kargs names vars ,term)))))))))))
             (_ #f))))
    (define (reduce-primcall cps label names vars k src op param args)
      (cond
       ((transform-primcall (hashq-ref *primcall-macro-reducers* op)
                            cps label names vars k src op param args)
        => (lambda (cps)
             (match (intmap-ref cps label)
               (($ $kargs names vars
                   ($ $continue k src ($ $primcall op param args)))
                (reduce-primcall cps label names vars k src op param args)))))
       ((transform-primcall (hashq-ref *primcall-reducers* op)
                            cps label names vars k src op param args))
       (else cps)))
    (define (reduce-branch cps label names vars kf kt src op param args)
      (and=>
       (hashq-ref *branch-reducers* op)
       (lambda (reducer)
         (match args
           ((arg0)
            (call-with-values (lambda () (lookup-pre-type types label arg0))
              (lambda (type0 min0 max0)
                (call-with-values (lambda ()
                                    (reducer cps kf kt src param
                                             arg0 type0 min0 max0))
                  (lambda (cps term)
                    (and term
                         (with-cps cps
                           (setk label
                                 ($kargs names vars ,term)))))))))
           ((arg0 arg1)
            (call-with-values (lambda () (lookup-pre-type types label arg0))
              (lambda (type0 min0 max0)
                (call-with-values (lambda () (lookup-pre-type types label arg1))
                  (lambda (type1 min1 max1)
                    (call-with-values (lambda ()
                                        (reducer cps kf kt src param
                                                 arg0 type0 min0 max0
                                                 arg1 type1 min1 max1))
                      (lambda (cps term)
                        (and term
                             (with-cps cps
                               (setk label
                                     ($kargs names vars ,term)))))))))))))))
    (define (branch-folded cps label names vars src k)
      (with-cps cps
        (setk label
              ($kargs names vars
                ($continue k src ($values ()))))))
    (define (fold-unary-branch cps label names vars kf kt src op param arg)
      (and=>
       (hashq-ref *branch-folders* op)
       (lambda (folder)
         (call-with-values (lambda () (lookup-pre-type types label arg))
           (lambda (type min max)
             (call-with-values (lambda () (folder param type min max))
               (lambda (f? v)
                 ;; (when f? (pk 'folded-unary-branch label op arg v))
                 (and f?
                      (branch-folded cps label names vars src
                                     (if v kt kf))))))))))
    (define (fold-binary-branch cps label names vars kf kt src op param arg0 arg1)
      (and=>
       (hashq-ref *branch-folders* op)
       (lambda (folder)
         (call-with-values (lambda () (lookup-pre-type types label arg0))
           (lambda (type0 min0 max0)
             (call-with-values (lambda () (lookup-pre-type types label arg1))
               (lambda (type1 min1 max1)
                 (call-with-values (lambda ()
                                     (folder param type0 min0 max0 type1 min1 max1))
                   (lambda (f? v)
                     ;; (when f? (pk 'folded-binary-branch label op arg0 arg1 v))
                     (and f?
                          (branch-folded cps label names vars src
                                         (if v kt kf))))))))))))
    (define (fold-branch cps label names vars kf kt src op param args)
      (match args
        ((x)
         (fold-unary-branch cps label names vars kf kt src op param x))
        ((x y)
         (fold-binary-branch cps label names vars kf kt src op param x y))))
    (define (visit-primcall cps label names vars k src op param args)
      ;; We might be able to fold primcalls that define a value.
      (match (intmap-ref cps k)
        (($ $kargs (_) (def))
         (or (fold-primcall cps label names vars k src op param args def)
             (reduce-primcall cps label names vars k src op param args)))
        (_
         (reduce-primcall cps label names vars k src op param args))))
    (define (visit-branch cps label names vars kf kt src op param args)
      ;; We might be able to fold primcalls that branch.
      (or (fold-branch cps label names vars kf kt src op param args)
          (reduce-branch cps label names vars kf kt src op param args)
          cps))
    (define (visit-switch cps label names vars kf kt* src arg)
      ;; We might be able to fold or reduce a switch.
      (let ((ntargets (length kt*)))
        (call-with-values (lambda () (lookup-pre-type types label arg))
          (lambda (type min max)
            (cond
             ((<= ntargets min)
              (branch-folded cps label names vars src kf))
             ((= min max)
              (branch-folded cps label names vars src (list-ref kt* min)))
             (else
              ;; There are two more optimizations we could do here: one,
              ;; if max is less than ntargets, we can prune targets at
              ;; the end of the switch, and perhaps reduce the switch
              ;; back to a branch; and two, if min is greater than 0,
              ;; then we can subtract off min and prune targets at the
              ;; beginning.  Not done yet though.
              cps))))))
    (let lp ((label start) (cps cps))
      (if (<= label end)
          (lp (1+ label)
              (match (intmap-ref cps label)
                (($ $kargs names vars ($ $continue k src
                                         ($ $primcall op param args)))
                 (visit-primcall cps label names vars k src op param args))
                (($ $kargs names vars ($ $branch kf kt src op param args))
                 (visit-branch cps label names vars kf kt src op param args))
                (($ $kargs names vars ($ $switch kf kt* src arg))
                 (visit-switch cps label names vars kf kt* src arg))
                (_ cps)))
          cps))))

(define (fold-functions-in-renumbered-program f conts seed)
  (let* ((conts (persistent-intmap conts))
         (end (1+ (intmap-prev conts))))
    (let lp ((label 0) (seed seed))
      (if (eqv? label end)
          seed
          (match (intmap-ref conts label)
            (($ $kfun src meta self tail clause)
             (lp (1+ tail) (f label tail seed))))))))

(define (type-fold conts)
  ;; Type analysis wants a program whose labels are sorted.
  (let ((conts (renumber conts)))
    (with-fresh-name-state conts
      (persistent-intmap
       (fold-functions-in-renumbered-program local-type-fold conts conts)))))