/
remove-unused.fun
1259 lines (1201 loc) · 50.6 KB
/
remove-unused.fun
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
(* Copyright (C) 2009,2020 Matthew Fluet.
* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
* Copyright (C) 1997-2000 NEC Research Institute.
*
* MLton is released under a HPND-style license.
* See the file MLton-LICENSE for details.
*)
functor RemoveUnused (S: SSA_TRANSFORM_STRUCTS): SSA_TRANSFORM =
struct
open S
open Exp Transfer
structure Used =
struct
structure L = TwoPointLattice (val bottom = "unused"
val top = "used")
open L
val use = makeTop
val isUsed = isTop
val whenUsed = addHandler
end
structure Coned =
struct
structure L = TwoPointLattice (val bottom = "not coned"
val top = "coned")
open L
val con = makeTop
val isConed = isTop
val whenConed = addHandler
end
structure Deconed =
struct
structure L = TwoPointLattice (val bottom = "not deconed"
val top = "deconed")
open L
val decon = makeTop
val isDeconed = isTop
end
structure MayReturn =
struct
structure L = TwoPointLattice (val bottom = "does not return"
val top = "may return")
open L
val return = makeTop
val mayReturn = isTop
val whenReturns = addHandler
end
structure MayRaise =
struct
structure L = TwoPointLattice (val bottom = "does not raise"
val top = "may raise")
open L
val raisee = makeTop
val mayRaise = isTop
val whenRaises = addHandler
end
structure VarInfo =
struct
datatype t = T of {ty: Type.t,
used: Used.t}
fun layout (T {used, ...}) = Used.layout used
local
fun make f (T r) = f r
in
val ty = make #ty
val used = make #used
end
fun new (ty : Type.t): t = T {ty = ty,
used = Used.new ()}
val use = Used.use o used
val isUsed = Used.isUsed o used
fun whenUsed (vi, th) = Used.whenUsed (used vi, th)
end
structure ConInfo =
struct
datatype t = T of {args: (VarInfo.t * Type.t) vector,
coned: Coned.t,
deconed: Deconed.t,
dummy: {con: Con.t, args: Type.t vector,
exp: Exp.t}}
fun layout (T {args, coned, deconed, ...}) =
Layout.record [("args", Vector.layout (VarInfo.layout o #1) args),
("coned", Coned.layout coned),
("deconed", Deconed.layout deconed)]
local
fun make f (T r) = f r
in
val args = make #args
val coned = make #coned
val deconed = make #deconed
val dummy = make #dummy
end
val con = Coned.con o coned
val isConed = Coned.isConed o coned
fun whenConed (ci, th) = Coned.whenConed (coned ci, th)
val decon = Deconed.decon o deconed
val isDeconed = Deconed.isDeconed o deconed
fun new {args: Type.t vector,
dummy: {con: Con.t, args: Type.t vector
, exp: Exp.t}}: t =
T {args = Vector.map (args, fn ty => (VarInfo.new ty, ty)),
coned = Coned.new (),
deconed = Deconed.new (),
dummy = dummy}
end
structure TyconInfo =
struct
datatype t = T of {cons: Con.t vector,
dummy: {con: Con.t, args: Type.t vector},
numCons: int ref,
used: Used.t}
fun layout (T {used, ...}) =
Layout.record [("used", Used.layout used)]
local
fun make f (T r) = f r
fun make' f = (make f, ! o (make f))
in
val cons = make #cons
val dummy = make #dummy
val (numCons', numCons) = make' #numCons
val used = make #used
end
fun new {cons: Con.t vector,
dummy: {con: Con.t, args: Type.t vector}}: t =
T {cons = cons,
dummy = dummy,
numCons = ref ~1,
used = Used.new ()}
end
structure TypeInfo =
struct
datatype t = T of {deconed: bool ref,
simplify: Type.t option ref,
used: bool ref}
local
fun make f (T r) = f r
fun make' f = (make f, ! o (make f))
in
val (deconed', _) = make' #deconed
val (simplify', _) = make' #simplify
val (used', _) = make' #used
end
fun new (): t = T {deconed = ref false,
simplify = ref NONE,
used = ref false}
end
structure FuncInfo =
struct
datatype t = T of {args: (VarInfo.t * Type.t) vector,
bugLabel: Label.t option ref,
mayRaise: MayRaise.t,
mayReturn: MayReturn.t,
raiseLabel: Label.t option ref,
raises: (VarInfo.t * Type.t) vector option,
returnLabel: Label.t option ref,
returns: (VarInfo.t * Type.t) vector option,
used: Used.t,
wrappers: Block.t list ref}
fun layout (T {args,
mayRaise, mayReturn,
raises, returns,
used,
...}) =
Layout.record [("args", Vector.layout
(Layout.tuple2 (VarInfo.layout, Type.layout))
args),
("mayRaise", MayRaise.layout mayRaise),
("mayReturn", MayReturn.layout mayReturn),
("raises", Option.layout
(Vector.layout
(Layout.tuple2 (VarInfo.layout, Type.layout)))
raises),
("returns", Option.layout
(Vector.layout
(Layout.tuple2 (VarInfo.layout, Type.layout)))
returns),
("used", Used.layout used)]
local
fun make f (T r) = f r
fun make' f = (make f, ! o (make f))
in
val args = make #args
val mayRaise' = make #mayRaise
val mayReturn' = make #mayReturn
val raiseLabel = make #raiseLabel
val raises = make #raises
val returnLabel = make #returnLabel
val returns = make #returns
val used = make #used
val (wrappers', wrappers) = make' #wrappers
end
val raisee = MayRaise.raisee o mayRaise'
val mayRaise = MayRaise.mayRaise o mayRaise'
fun whenRaises (fi, th) = MayRaise.whenRaises (mayRaise' fi, th)
fun flowRaises (fi, fi') = MayRaise.<= (mayRaise' fi, mayRaise' fi')
val return = MayReturn.return o mayReturn'
fun whenReturns (fi, th) = MayReturn.whenReturns (mayReturn' fi, th)
val mayReturn = MayReturn.mayReturn o mayReturn'
fun flowReturns (fi, fi') = MayReturn.<= (mayReturn' fi, mayReturn' fi')
val use = Used.use o used
val isUsed = Used.isUsed o used
fun whenUsed (fi, th) = Used.whenUsed (used fi, th)
fun new {args: (VarInfo.t * Type.t) vector,
raises: (VarInfo.t * Type.t) vector option,
returns: (VarInfo.t * Type.t) vector option}: t =
T {args = args,
bugLabel = ref NONE,
mayRaise = MayRaise.new (),
mayReturn = MayReturn.new (),
raiseLabel = ref NONE,
raises = raises,
returnLabel = ref NONE,
returns = returns,
used = Used.new (),
wrappers = ref []}
end
structure LabelInfo =
struct
datatype t = T of {args: (VarInfo.t * Type.t) vector,
func: FuncInfo.t,
used: Used.t,
wrappers: (Type.t vector * Label.t) list ref}
fun layout (T {args, used, ...}) =
Layout.record [("args", Vector.layout (VarInfo.layout o #1) args),
("used", Used.layout used)]
fun new {args: (VarInfo.t * Type.t) vector, func: FuncInfo.t}: t =
T {args = args,
func = func,
used = Used.new (),
wrappers = ref []}
local
fun make f (T r) = f r
fun make' f = (make f, ! o (make f))
in
val args = make #args
val func = make #func
val used = make #used
val (wrappers', wrappers) = make' #wrappers
end
val use = Used.use o used
val isUsed = Used.isUsed o used
fun whenUsed (li, th) = Used.whenUsed (used li, th)
end
fun transform (Program.T {datatypes, globals, functions, main}) =
let
val {get = conInfo: Con.t -> ConInfo.t,
set = setConInfo, ...} =
Property.getSetOnce
(Con.plist,
Property.initRaise ("RemoveUnused.conInfo", Con.layout))
fun newConInfo (con, args, dummy) =
setConInfo (con, ConInfo.new {args = args, dummy = dummy})
val {get = tyconInfo: Tycon.t -> TyconInfo.t,
set = setTyconInfo, ...} =
Property.getSetOnce
(Tycon.plist,
Property.initRaise ("RemoveUnused.tyconInfo", Tycon.layout))
fun newTyconInfo (tycon, cons, dummy) =
setTyconInfo (tycon, TyconInfo.new {cons = cons, dummy = dummy})
val {get = typeInfo: Type.t -> TypeInfo.t,
destroy, ...} =
Property.destGet
(Type.plist,
Property.initFun (fn _ => TypeInfo.new ()))
val {get = varInfo: Var.t -> VarInfo.t,
set = setVarInfo, ...} =
Property.getSetOnce
(Var.plist,
Property.initRaise ("RemoveUnused.varInfo", Var.layout))
fun newVarInfo (var, ty) =
setVarInfo (var, VarInfo.new ty)
val {get = labelInfo: Label.t -> LabelInfo.t,
set = setLabelInfo, ...} =
Property.getSetOnce
(Label.plist,
Property.initRaise ("RemoveUnused.labelInfo", Label.layout))
val {get = funcInfo: Func.t -> FuncInfo.t,
set = setFuncInfo, ...} =
Property.getSetOnce
(Func.plist,
Property.initRaise ("RemoveUnused.funcInfo", Func.layout))
val usedTycon = TyconInfo.used o tyconInfo
val useTycon = Used.use o usedTycon
fun visitTycon (tycon: Tycon.t) = useTycon tycon
val isUsedTycon = Used.isUsed o usedTycon
fun visitType (ty: Type.t) =
let
val ti = typeInfo ty
val used = TypeInfo.used' ti
in
if !used
then ()
else let
val () = used := true
datatype z = datatype Type.dest
val () =
case Type.dest ty of
Array ty => visitType ty
| Datatype tycon => visitTycon tycon
| Ref ty => visitType ty
| Tuple tys => Vector.foreach (tys, visitType)
| Vector ty => visitType ty
| Weak ty => visitType ty
| _ => ()
in
()
end
end
val visitTypeTh = fn ty => fn () => visitType ty
val tyVar = VarInfo.ty o varInfo
val usedVar = VarInfo.used o varInfo
val useVar = Used.use o usedVar
val isUsedVar = Used.isUsed o usedVar
val whenUsedVar = fn (var, th) => VarInfo.whenUsed (varInfo var, th)
fun flowVarInfoTyVarInfoTy ((vi, _), (vi', _)) =
Used.<= (VarInfo.used vi, VarInfo.used vi')
fun flowVarInfoTysVarInfoTys (xs, ys) =
Vector.foreach2 (xs, ys, flowVarInfoTyVarInfoTy)
fun flowVarInfoTyVar ((vi, _), x) =
Used.<= (VarInfo.used vi, usedVar x)
fun flowVarInfoTysVars (xs, ys) =
Vector.foreach2 (xs, ys, flowVarInfoTyVar)
val newVarInfo = fn (var, ty) =>
(newVarInfo (var, ty)
; whenUsedVar (var, visitTypeTh ty))
val visitLabelInfo = LabelInfo.use
val visitLabelInfoTh = fn li => fn () => visitLabelInfo li
val visitLabel = visitLabelInfo o labelInfo
val visitLabelTh = fn l => fn () => visitLabel l
val visitFuncInfo = FuncInfo.use
val visitFunc = visitFuncInfo o funcInfo
fun visitVar (x: Var.t) = useVar x
fun visitVars (xs: Var.t Vector.t) = Vector.foreach (xs, visitVar)
fun visitExp (e: Exp.t) =
case e of
ConApp {con, args} =>
let
val ci = conInfo con
val () = ConInfo.con ci
val () = flowVarInfoTysVars (ConInfo.args ci, args)
in
()
end
| Const _ => ()
| PrimApp {prim, args, ...} =>
let
val () = visitVars args
datatype z = datatype Type.dest
fun deconType (ty: Type.t) =
let
val ti = typeInfo ty
val deconed = TypeInfo.deconed' ti
in
if !deconed
then ()
else let
val () = deconed := true
val () =
case Type.dest ty of
Datatype t =>
Vector.foreach
(TyconInfo.cons (tyconInfo t),
fn con => deconCon con)
| Tuple ts => Vector.foreach (ts, deconType)
| Vector t => deconType t
| _ => ()
in
()
end
end
and deconCon con =
let
val ci = conInfo con
val () = ConInfo.decon ci
val () =
Vector.foreach
(ConInfo.args ci, fn (x, t) =>
(VarInfo.use x
; deconType t))
in
()
end
val () =
case prim of
Prim.MLton_eq =>
(* MLton_eq may be used on datatypes used as enums. *)
deconType (tyVar (Vector.first args))
| Prim.MLton_equal =>
(* MLton_equal will be expanded by poly-equal into uses
* of constructors as patterns.
*)
deconType (tyVar (Vector.first args))
| Prim.MLton_hash =>
(* MLton_hash will be expanded by poly-hash into uses
* of constructors as patterns.
*)
deconType (tyVar (Vector.first args))
(*
| Prim.MLton_size =>
deconType (tyVar (Vector.first args))
*)
| _ => ()
in
()
end
| Profile _ => ()
| Select {tuple, ...} => visitVar tuple
| Tuple xs => visitVars xs
| Var x => visitVar x
val visitExpTh = fn e => fn () => visitExp e
fun maybeVisitVarExp (var, exp) =
Option.app (var, fn var =>
VarInfo.whenUsed (varInfo var, visitExpTh exp))
fun visitStatement (Statement.T {exp, var, ty, ...}) =
(Option.app (var, fn var => newVarInfo (var, ty))
; if Exp.maySideEffect exp
then (visitType ty
; visitExp exp)
else maybeVisitVarExp (var, exp))
fun visitTransfer (t: Transfer.t, fi: FuncInfo.t) =
case t of
Bug => ()
| Call {args, func, return} =>
let
datatype u = None
| Caller
| Some of Label.t
val (cont, handler) =
case return of
Return.Dead => (None, None)
| Return.NonTail {cont, handler} =>
(Some cont,
case handler of
Handler.Caller => Caller
| Handler.Dead => None
| Handler.Handle h => Some h)
| Return.Tail => (Caller, Caller)
val fi' = funcInfo func
val () = flowVarInfoTysVars (FuncInfo.args fi', args)
val () =
case cont of
None => ()
| Caller =>
let
val () =
case (FuncInfo.returns fi,
FuncInfo.returns fi') of
(SOME xts, SOME xts') =>
flowVarInfoTysVarInfoTys (xts, xts')
| _ => ()
val () = FuncInfo.flowReturns (fi', fi)
in
()
end
| Some l =>
let
val li = labelInfo l
val () =
Option.app
(FuncInfo.returns fi', fn xts =>
flowVarInfoTysVarInfoTys
(LabelInfo.args li, xts))
val () =
FuncInfo.whenReturns
(fi', visitLabelInfoTh li)
in
()
end
val () =
case handler of
None => ()
| Caller =>
let
val () =
case (FuncInfo.raises fi,
FuncInfo.raises fi') of
(SOME xts, SOME xts') =>
flowVarInfoTysVarInfoTys (xts, xts')
| _ => ()
val () = FuncInfo.flowRaises (fi', fi)
in
()
end
| Some l =>
let
val li = labelInfo l
val () =
Option.app
(FuncInfo.raises fi', fn xts =>
flowVarInfoTysVarInfoTys
(LabelInfo.args li, xts))
val () =
FuncInfo.whenRaises (fi', visitLabelInfoTh li)
in
()
end
val () = visitFuncInfo fi'
in
()
end
| Case {test, cases, default} =>
let
val () = visitVar test
in
case cases of
Cases.Word (_, cs) =>
(Vector.foreach (cs, visitLabel o #2)
; Option.app (default, visitLabel))
| Cases.Con cases =>
if Vector.isEmpty cases
then Option.app (default, visitLabel)
else let
val () =
Vector.foreach
(cases, fn (con, l) =>
let
val ci = conInfo con
val () = ConInfo.decon ci
val li = labelInfo l
val () =
flowVarInfoTysVarInfoTys
(LabelInfo.args li, ConInfo.args ci)
val () =
ConInfo.whenConed
(ci, visitLabelTh l)
in
()
end)
val tycon =
case Type.dest (tyVar test) of
Type.Datatype tycon => tycon
| _ => Error.bug "RemoveUnused.visitTransfer: Case:non-Datatype"
val cons = TyconInfo.cons (tyconInfo tycon)
in
case default of
NONE => ()
| SOME l =>
Vector.foreach
(cons, fn con =>
if Vector.exists
(cases, fn (c, _) =>
Con.equals(c, con))
then ()
else
ConInfo.whenConed
(conInfo con, visitLabelTh l))
end
end
| Goto {dst, args} =>
let
val li = labelInfo dst
val () = flowVarInfoTysVars (LabelInfo.args li, args)
val () = visitLabelInfo li
in
()
end
| Raise xs =>
(FuncInfo.raisee fi
; flowVarInfoTysVars (valOf (FuncInfo.raises fi), xs))
| Return xs =>
(FuncInfo.return fi
; flowVarInfoTysVars (valOf (FuncInfo.returns fi), xs))
| Runtime {args, return, ...} =>
(visitVars args
; visitLabel return)
fun visitBlock (Block.T {statements, transfer, ...}, fi: FuncInfo.t) =
(Vector.foreach (statements, visitStatement)
; visitTransfer (transfer, fi))
val visitBlockTh = fn (b, fi) => fn () => visitBlock (b, fi)
(* Visit all reachable expressions. *)
val () =
Vector.foreach
(datatypes, fn Datatype.T {tycon, cons} =>
let
val dummyCon = Con.newString "dummy"
val dummyArgs = Vector.new0 ()
val dummy = {con = dummyCon, args = dummyArgs}
val () =
newTyconInfo
(tycon, Vector.map (cons, fn {con, ...} => con), dummy)
val dummyExp = ConApp {args = Vector.new0 (),
con = dummyCon}
val dummy = {con = dummyCon, args = dummyArgs, exp = dummyExp}
val () =
Vector.foreach
(cons, fn {con, args} =>
newConInfo (con, args, dummy))
in
()
end)
val () =
let
fun doitCon c =
let
val ci = conInfo c
in
ConInfo.con ci
; ConInfo.decon ci
end
in
useTycon Tycon.bool
; doitCon Con.truee
; doitCon Con.falsee
end
val () =
Vector.foreach (globals, visitStatement)
val () =
List.foreach
(functions, fn function =>
let
val {name, args, raises, returns, start, blocks, ...} =
Function.dest function
val () = Vector.foreach (args, newVarInfo)
local
fun doitVarTys vts =
Vector.map (vts, fn (x, t) => (varInfo x, t))
fun doitTys ts =
Vector.map (ts, fn t => (VarInfo.new t, t))
fun doitTys' ts =
Option.map (ts, doitTys)
in
val fi =
FuncInfo.new
{args = doitVarTys args,
raises = doitTys' raises,
returns = doitTys' returns}
end
val () = setFuncInfo (name, fi)
val () = FuncInfo.whenUsed (fi, visitLabelTh start)
val () =
Vector.foreach
(blocks, fn block as Block.T {label, args, ...} =>
let
val () = Vector.foreach (args, newVarInfo)
local
fun doitVarTys vts =
Vector.map (vts, fn (x, t) => (varInfo x, t))
in
val li =
LabelInfo.new
{args = doitVarTys args,
func = fi}
end
val () = setLabelInfo (label, li)
val () = LabelInfo.whenUsed (li, visitBlockTh (block, fi))
in
()
end)
in
()
end)
val () = visitFunc main
(* Diagnostics *)
val () =
Control.diagnostics
(fn display =>
let open Layout
in
Vector.foreach
(datatypes, fn Datatype.T {tycon, cons} =>
display (seq [Tycon.layout tycon,
str ": ",
TyconInfo.layout (tyconInfo tycon),
str ": ",
Vector.layout
(fn {con, ...} =>
seq [Con.layout con,
str " ",
ConInfo.layout (conInfo con)])
cons]));
display (str "\n");
List.foreach
(functions, fn f =>
let
val {name, blocks, ...} = Function.dest f
in
display (seq [Func.layout name,
str ": ",
FuncInfo.layout (funcInfo name)]);
Vector.foreach
(blocks, fn Block.T {label, ...} =>
display (seq [Label.layout label,
str ": ",
LabelInfo.layout (labelInfo label)]));
display (str "\n")
end)
end)
(* Analysis is done, Now build the resulting program. *)
fun getWrapperLabel (l: Label.t,
args: (VarInfo.t * Type.t) vector) =
let
val li = labelInfo l
in
if Vector.forall2 (args, LabelInfo.args li, fn ((x, _), (y, _)) =>
VarInfo.isUsed x = VarInfo.isUsed y)
then l
else let
val tys =
Vector.keepAllMap (args, fn (x, ty) =>
if VarInfo.isUsed x
then SOME ty
else NONE)
in
case List.peek
(LabelInfo.wrappers li, fn (args', _) =>
Vector.length args' = Vector.length tys
andalso
Vector.forall2 (args', tys, fn (ty', ty) =>
Type.equals (ty', ty))) of
NONE =>
let
val liArgs = LabelInfo.args li
val l' = Label.newNoname ()
val (args', args'') =
Vector.unzip
(Vector.map2
(args, liArgs, fn ((x, ty), (y, _)) =>
let
val z = Var.newNoname ()
in
(if VarInfo.isUsed x
then SOME (z, ty) else NONE,
if VarInfo.isUsed y
then SOME z else NONE)
end))
val args' =
Vector.keepAllMap (args', fn x => x)
val (_, tys') = Vector.unzip args'
val args'' =
Vector.keepAllMap (args'', fn x => x)
val block =
Block.T {label = l',
args = args',
statements = Vector.new0 (),
transfer = Goto {dst = l,
args = args''}}
val () =
List.push (LabelInfo.wrappers' li,
(tys', l'))
val () =
List.push (FuncInfo.wrappers' (LabelInfo.func li),
block)
in
l'
end
| SOME (_, l') => l'
end
end
val getConWrapperLabel = getWrapperLabel
val getContWrapperLabel = getWrapperLabel
val getHandlerWrapperLabel = getWrapperLabel
fun getOriginalWrapperLabel l =
getWrapperLabel
(l, Vector.map (LabelInfo.args (labelInfo l), fn (_, t) =>
let
val x = VarInfo.new t
val () = VarInfo.use x
in
(x, t)
end))
val getRuntimeWrapperLabel = getOriginalWrapperLabel
fun getBugFunc (fi: FuncInfo.t): Label.t =
(* Can't share the Bug block across different places because the
* profile sourceInfo stack might be different.
*)
let
val l = Label.newNoname ()
val block = Block.T {label = l,
args = Vector.new0 (),
statements = Vector.new0 (),
transfer = Bug}
val () = List.push (FuncInfo.wrappers' fi, block)
in
l
end
fun getReturnFunc (fi: FuncInfo.t): Label.t =
let
val r = FuncInfo.returnLabel fi
in
case !r of
NONE =>
let
val l = Label.newNoname ()
val returns = valOf (FuncInfo.returns fi)
val args =
Vector.keepAllMap
(returns, fn (vi, ty) =>
if VarInfo.isUsed vi
then SOME (Var.newNoname (), ty)
else NONE)
val xs = Vector.map (args, #1)
val block = Block.T {label = l,
args = args,
statements = Vector.new0 (),
transfer = Return xs}
val () = r := SOME l
val () = List.push (FuncInfo.wrappers' fi, block)
val () = setLabelInfo (l, LabelInfo.new {func = fi,
args = returns})
in
l
end
| SOME l => l
end
fun getReturnContFunc (fi, args) =
getWrapperLabel (getReturnFunc fi, args)
fun getRaiseFunc (fi: FuncInfo.t): Label.t =
let
val r = FuncInfo.raiseLabel fi
in
case !r of
NONE =>
let
val l = Label.newNoname ()
val raises = valOf (FuncInfo.raises fi)
val args =
Vector.keepAllMap
(raises, fn (vi, ty) =>
if VarInfo.isUsed vi
then SOME (Var.newNoname (), ty)
else NONE)
val xs = Vector.map (args, #1)
val block = Block.T {label = l,
args = args,
statements = Vector.new0 (),
transfer = Raise xs}
val () = r := SOME l
val () = List.push (FuncInfo.wrappers' fi, block)
val () = setLabelInfo (l, LabelInfo.new {func = fi,
args = raises})
in
l
end
| SOME l => l
end
fun getRaiseHandlerFunc (fi, args) =
getWrapperLabel (getRaiseFunc fi, args)
fun simplifyType (ty: Type.t): Type.t =
let
val ti = typeInfo ty
val simplify = TypeInfo.simplify' ti
in
case !simplify of
NONE => let
datatype z = datatype Type.dest
val ty =
case Type.dest ty of
Array ty => Type.array (simplifyType ty)
| Ref ty => Type.reff (simplifyType ty)
| Tuple tys => Type.tuple (Vector.map (tys, simplifyType))
| Vector ty => Type.vector (simplifyType ty)
| Weak ty => Type.weak (simplifyType ty)
| _ => ty
in
simplify := SOME ty
; ty
end
| SOME ty => ty
end
val datatypes =
Vector.keepAllMap
(datatypes, fn Datatype.T {tycon, cons} =>
if isUsedTycon tycon
then let
val needsDummy : bool ref = ref false
val cons =
Vector.keepAllMap
(cons, fn {con, ...} =>
let
val ci = conInfo con
fun addDummy () =
if !needsDummy
then NONE
else let
val () = needsDummy := true
in
SOME (TyconInfo.dummy (tyconInfo tycon))
end
in
case (ConInfo.isConed ci,
ConInfo.isDeconed ci) of
(false, _) => NONE
| (true, true) =>
SOME {args = Vector.keepAllMap
(ConInfo.args ci, fn (x, ty) =>
if VarInfo.isUsed x
then SOME (simplifyType ty)
else NONE),
con = con}
| (true, false) =>
addDummy ()
end)
val num = Vector.length cons
val () = TyconInfo.numCons' (tyconInfo tycon) := num
in
SOME (Datatype.T {tycon = tycon, cons = cons})
end
else NONE)
fun simplifyExp (e: Exp.t): Exp.t =
case e of
ConApp {con, args} =>
let
val ci = conInfo con
in
if ConInfo.isDeconed ci
then let
val ciArgs =
ConInfo.args ci
in
ConApp {args = (Vector.keepAllMap2
(args, ciArgs,
fn (x, (y, _)) =>
if VarInfo.isUsed y
then SOME x
else NONE)),
con = con}
end
else #exp (ConInfo.dummy ci)
end
| PrimApp {prim, targs, args} =>
PrimApp {prim = prim,
targs = Vector.map (targs, simplifyType),
args = args}
| _ => e
fun simplifyStatement (s as Statement.T {var, ty, exp}) : Statement.t option =
case exp of
Profile _ => SOME s
| _ => let
fun doit' var =
SOME (Statement.T
{var = var,
ty = simplifyType ty,
exp = simplifyExp exp})
fun doit var' =
if Exp.maySideEffect exp
then doit' var
else if isSome var'
then doit' var'
else NONE
in
case var of
SOME var => if isUsedVar var
then doit (SOME var)
else doit NONE