-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigerregalloc.sml
1033 lines (908 loc) · 40.1 KB
/
tigerregalloc.sml
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
structure tigerregalloc :> tigerregalloc =
struct
(* simpifyWorklist INV: if v in simplifyWorklist => v not in precolored *)
(* Each temp maps a machine register *)
type allocation = (tigertemp.temp, tigerframe.register) Splaymap.dict
(* move: (a, b), where:
- a: source
- b: destination
*)
type move = tigertemp.temp * tigertemp.temp
type edge = tigertemp.temp * tigertemp.temp
val emptyNodeSet : tigertemp.temp Splayset.set = Splayset.empty String.compare
(* cmpPairs : move * move -> order *)
fun cmpPairs ((s1, d1) : move, (s2, d2) : move) = case String.compare(s1, s2) of
EQUAL => String.compare(d1, d2)
| x => x
val emptyMoveSet : move Splayset.set = Splayset.empty cmpPairs
(* All general-purpose target-machine registers *)
val precolored : tigertemp.temp Splayset.set = Splayset.addList(emptyNodeSet, tigerframe.registers)
val specialRegsSet : tigertemp.temp Splayset.set = Splayset.addList(emptyNodeSet, tigerframe.specialregs)
(* Number of "colors": available target-machine registers to identify temporaries *)
val kColors : tigertemp.temp Splayset.set = Splayset.difference(precolored, specialRegsSet)
val k : int = Splayset.numItems kColors
(* precolored = {%r10,%r11,%r12,%r13,%r14,%r15,%r8,%r9,%rax,%rbp,%rbx,%rcx,%rdi,%rdx,%rsi,%rsp}
All machine registers. In total = 16 registers *)
fun regAlloc (instrList, frame) =
let
(*val _ = print("\nsin colorear = "^(utils.listToString instrList (tigerassem.format tigertemp.makeString))^"\n")*)
(* DATA STRUCTURES *)
(* Node work-lists, sets, and stacks. The following lists and sets are always mutually disjoint
and every node is always in exactly one of the sets or lists *)
(* temporary registers, not precolored and not yet processed *)
val initial : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* list of low-degree non-move-related nodes *)
val simplifyWorklist : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* low-degree move-related nodes *)
val freezeWorklist : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* high-degree nodes *)
val spillWorklist : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* nodes marked for spilling during this round; initially empty *)
val spilledNodes : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* registers that have been coalesced *)
val coalescedNodes : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* nodes successfully colored *)
val coloredNodes : tigertemp.temp Splayset.set ref = ref emptyNodeSet
(* stack containing temps removed from the interference graph *)
val selectStack = ref []
(* Stack methods *)
fun pushStack n = (selectStack := n :: !selectStack)
fun popStack() =
let
val h = List.hd (!selectStack)
val _ = (selectStack := List.tl (!selectStack))
in
h
end
(* End Stack methods *)
(* Move sets. There are 5 sets of move instructions, and every move is in exactly one of these
sets (after Build() through the end of Main()) *)
(* moves that have been coalesced *)
val coalescedMoves : move Splayset.set ref = ref emptyMoveSet
(* moves whose source and target interfiere *)
val constrainedMoves : move Splayset.set ref = ref emptyMoveSet
(* moves that will no longer be considered for coalescing *)
val frozenMoves : move Splayset.set ref = ref emptyMoveSet
(* moves enabled for possible coalescing *)
val worklistMoves : move Splayset.set ref = ref emptyMoveSet
(* moves not yet ready for coalescing *)
val activeMoves : move Splayset.set ref = ref emptyMoveSet
(* Other data structures *)
(* the set of interference edges (u,v) in the, undirected, interference graph *)
val adjSet : edge Splayset.set ref = ref (Splayset.empty cmpPairs)
(* adjacency list representation of the interference graph. For each non-precolored tmp u,
adjList[u] is the set of nodes that interfere with u *)
val adjList : (tigertemp.temp, tigertemp.temp Splayset.set) Splaymap.dict ref = ref (Splaymap.mkDict String.compare)
(* an array containing the current degree of each node *)
val degree : (tigertemp.temp, int) Splaymap.dict ref = ref (Splaymap.mkDict String.compare)
(* a mapping from a node to the list of moves it's associated with *)
val moveList : (tigertemp.temp, move Splayset.set) Splaymap.dict ref = ref (Splaymap.mkDict String.compare)
(* when a move (u, v) has been coalesced, and v put in coalescedNodes, then
alias(v) = u *)
val alias : (tigertemp.temp, tigertemp.temp) Splaymap.dict ref = ref (Splaymap.mkDict String.compare)
(* the color chosen by the algorithm for a node. For precolored nodes this is initialized to the
given color *)
val color : (tigertemp.temp, tigerframe.register) Splaymap.dict ref = ref (List.foldl (fn (reg, dict) =>
Splaymap.insert(dict, reg, reg)) (Splaymap.mkDict String.compare) tigerframe.registers)
(* Init liveness info: instruction graph, and live-out set of tmp registers for each node in
the instruction graph *)
val iGraph : tigerliveness.node Splayset.set ref = ref (Splayset.empty tigerliveness.compareNodes)
val liveOutTable : tigertemp.temp Splayset.set list ref = ref []
(* getLiveOutSetFromInstrNum : int -> tigertemp.temp Splayset.set *)
fun getLiveOutSetFromInstrNum (nodeNum : int) : tigertemp.temp Splayset.set =
List.nth(!liveOutTable, nodeNum)
handle Subscript => raise Fail "Error - regAlloc. getLiveOutSetFromInstrNum(): Subscript error"
(* Prints current iGraph and liveOutTable information *)
(* printIGraphAndLiveOutInfo : unit -> unit *)
fun printIGraphAndLiveOutInfo() =
let
val iGraphList = Splayset.listItems (!iGraph)
val _ = print("\niGraph = \n"^(utils.listToString (iGraphList) (fn node =>
let
val instr = tigerliveness.getInstrFromNode node
val num = tigerliveness.getNumFromNode node
val pair = (instr, num)
in
utils.pairToString pair (tigerassem.format tigertemp.makeString) Int.toString
end))^"\n")
val _ = print("\nliveOutTable = \n"^(utils.listToString (!liveOutTable) (fn tmpSet =>
utils.setToString tmpSet utils.id))^"\n")
in
()
end
(* printDataStructure : string -> unit *)
fun printDataStructure ds =
case ds of
"precolored" => print("\n"^"precolored = "^(utils.setToString precolored utils.id)^"\n")
| "initial" => print("\n"^"initial = "^(utils.setToString (!initial) utils.id)^"\n")
| "simplifyWorklist" => print("\n"^"simplifyWorklist = "^(utils.setToString (!simplifyWorklist) utils.id)^"\n")
| "freezeWorklist" => print("\n"^"freezeWorklist = "^(utils.setToString (!freezeWorklist) utils.id)^"\n")
| "spillWorklist" => print("\n"^"spillWorklist = "^(utils.setToString (!spillWorklist) utils.id)^"\n")
| "spilledNodes" => print("\n"^"spilledNodes = "^(utils.setToString (!spilledNodes) utils.id)^"\n")
| "coalescedNodes" => print("\n"^"coalescedNodes = "^(utils.setToString (!coalescedNodes) utils.id)^"\n")
| "coloredNodes" => print("\n"^"coloredNodes = "^(utils.setToString (!coloredNodes) utils.id)^"\n")
| "selectStack" => print("\n"^"selectStack = "^(utils.listToString (!selectStack) utils.id)^"\n")
| "coalescedMoves" => print("\n"^"coalescedMoves = "^(utils.setToString (!coalescedMoves) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "constrainedMoves" => print("\n"^"constrainedMoves = "^(utils.setToString (!constrainedMoves) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "frozenMoves" => print("\n"^"frozenMoves = "^(utils.setToString (!frozenMoves) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "worklistMoves" => print("\n"^"worklistMoves = "^(utils.setToString (!worklistMoves) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "activeMoves" => print("\n"^"activeMoves = "^(utils.setToString (!activeMoves) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "adjSet" => print("\n"^"adjSet = "^(utils.setToString (!adjSet) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
| "adjList" => let
val adjList' = Splaymap.listItems (!adjList)
in
print("\n"^"adjList = "^(utils.listToString adjList' (fn pair =>
utils.pairToString pair utils.id (fn tmpSet => utils.setToString tmpSet utils.id)))^"\n")
end
| "degree" => let
val degree' = Splaymap.listItems (!degree)
in
print("\n"^"degree = "^(utils.listToString degree' (fn pair =>
utils.pairToString pair utils.id Int.toString))^"\n")
end
| "moveList" => let
val moveList' : (tigertemp.temp * move Splayset.set) list = Splaymap.listItems (!moveList)
val moveList'' : (tigertemp.temp * string) list = List.map (fn (tmp, moveSet) =>
(tmp, utils.setToString moveSet (fn pair =>
utils.pairToString pair utils.id utils.id))) moveList'
in
print("\n"^"moveList = "^(utils.listToString moveList'' (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
end
| "color" => let
val color' = Splaymap.listItems (!color)
in
print("\n"^"color = "^(utils.listToString color' (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
end
| "kColors" => print("\n"^"kColors = "^(utils.setToString kColors utils.id)^"\n")
| "alias" => print("\n"^"alias = "^(utils.dictToString (!alias) utils.id utils.id)^"\n")
| _ => raise Fail "Error - regAlloc. printDataStructure(): data structure not considered"
(* printAllDataStructures : unit -> unit *)
fun printAllDataStructures() =
(print("\n====================\t Begin all data structures \t====================\n");
printDataStructure "precolored";
printDataStructure "initial";
printDataStructure "simplifyWorklist";
printDataStructure "freezeWorklist";
printDataStructure "spillWorklist";
printDataStructure "spilledNodes";
printDataStructure "coalescedNodes";
printDataStructure "coloredNodes";
printDataStructure "selectStack";
printDataStructure "coalescedMoves";
printDataStructure "constrainedMoves";
printDataStructure "frozenMoves";
printDataStructure "worklistMoves";
printDataStructure "activeMoves";
printDataStructure "adjSet";
printDataStructure "adjList";
printDataStructure "degree";
printDataStructure "moveList";
printDataStructure "color";
printDataStructure "kColors";
printDataStructure "alias";
print("\n====================\t End all data structures \t====================\n"))
(*val _ = (print("\nFirst color list = \n");
printDataStructure "color")*)
(* livenessAnalysis : unit -> unit *)
fun livenessAnalysis() =
let
val (liveOutTableList : tigertemp.temp Splayset.set list,
graph : tigerliveness.node Splayset.set) = tigerliveness.calculateLiveOut instrList
(* Update iGraph and live-out info *)
val _ = iGraph := graph
val _ = liveOutTable := liveOutTableList
val graphList : tigerliveness.node list = Splayset.listItems graph
val graphList' : tigertemp.temp Splayset.set list = List.map (fn node =>
let
val instr = tigerliveness.getInstrFromNode node
val useSet = tigerliveness.calculateUseSet instr
val defSet = tigerliveness.calculateDefSet instr
in
Splayset.union(useSet, defSet)
end) graphList
(* Gen union: all temps from the instruction list *)
val allTmpsSet : tigertemp.temp Splayset.set = List.foldl (fn (tmpSet, s) =>
Splayset.union(tmpSet, s)) (Splayset.empty String.compare) graphList'
in
initial := Splayset.difference(allTmpsSet, precolored);
adjList := Splayset.foldl (fn (tmp, dict) =>
Splaymap.insert(dict, tmp, emptyNodeSet)) (!adjList) allTmpsSet;
degree := Splayset.foldl (fn (tmp, dict) =>
Splaymap.insert(dict, tmp, 0)) (!degree) allTmpsSet;
moveList := Splayset.foldl (fn (tmp, dict) =>
Splaymap.insert(dict, tmp, emptyMoveSet)) (!moveList) allTmpsSet
end
(* addEdge : (tigertemp.temp * tigertemp.temp) -> unit *)
fun addEdge(u, v) =
if not(Splayset.member(!adjSet, (u, v))) andalso not(u = v)
then
(adjSet := Splayset.add(Splayset.add(!adjSet, (u, v)), (v, u));
if not(Splayset.member(precolored, u))
then
(let
val adjList_u = case Splaymap.peek(!adjList, u) of
SOME set => set
| NONE => raise Fail "Error - regAlloc. addEdge() peek error"
val singleton = Splayset.singleton String.compare v
in
adjList := Splaymap.insert(!adjList, u, Splayset.union(adjList_u, singleton))
end;
let
val degree_u = case Splaymap.peek(!degree, u) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. addEdge() peek error"
in
degree := Splaymap.insert(!degree, u, degree_u + 1)
end)
else
();
if not(Splayset.member(precolored, v))
then
(let
val adjList_v = case Splaymap.peek(!adjList, v) of
SOME set => set
| NONE => raise Fail "Error - regAlloc. addEdge() peek error"
val singleton = Splayset.singleton String.compare u
in
adjList := Splaymap.insert(!adjList, v, Splayset.union(adjList_v, singleton))
end;
let
val degree_v = case Splaymap.peek(!degree, v) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. addEdge() peek error"
in
degree := Splaymap.insert(!degree, v, degree_v + 1)
end)
else
())
else
()
(* build : unit -> unit *)
fun build() =
Splayset.app (fn node =>
let
val (instr, nodeNum) = (tigerliveness.getInstrFromNode node, tigerliveness.getNumFromNode node)
(*val live = ref (List.nth(!liveOutTable, nodeNum))*)
in
if tigerassem.isMoveInstruction instr
then
((*live := Splayset.difference(!live, tigerliveness.calculateUseSet instr);*)
Splayset.app (fn n =>
let
val moveList_n = case Splaymap.peek(!moveList, n) of
SOME set => set
| NONE => raise Fail "Error - regAlloc. build(): peek error"
val moveSrcDst = tigerassem.getSrcDstFromMoveInstruction instr
in
moveList := Splaymap.insert(!moveList, n, Splayset.add(moveList_n, (#src moveSrcDst, #dst moveSrcDst)))
end) (Splayset.union(tigerliveness.calculateDefSet instr, tigerliveness.calculateUseSet instr));
let
val moveSrcDst = tigerassem.getSrcDstFromMoveInstruction instr
in
worklistMoves := Splayset.add(!worklistMoves, (#src moveSrcDst, #dst moveSrcDst))
end)
else
();
(*live := Splayset.union(!live, tigerliveness.calculateDefSet instr);*)
Splayset.app (fn d =>
Splayset.app (fn l =>
addEdge(l, d)) (getLiveOutSetFromInstrNum nodeNum)) (tigerliveness.calculateDefSet instr)
(*live := Splayset.union(tigerliveness.calculateUseSet instr, Splayset.difference(!live, tigerliveness.calculateDefSet instr))*)
end) (!iGraph)
(* nodeMoves : tigertemp.temp -> move Splayset.set *)
fun nodeMoves n =
let
val union = Splayset.union(!activeMoves, !worklistMoves)
val moveList_n = case Splaymap.peek(!moveList, n) of
SOME set => set
| NONE => raise Fail "Error - regAlloc. nodeMoves() peek error"
in
Splayset.intersection(moveList_n, union)
end
(* moveRelated : tigertemp.temp -> bool *)
fun moveRelated n =
not(Splayset.isEmpty (nodeMoves n))
(* makeWorklist : tigertemp.temp Splayset.set -> unit *)
fun makeWorklist() =
Splayset.app (fn n =>
(initial := Splayset.delete(!initial, n);
let
val degree_n = case Splaymap.peek(!degree, n) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. makeWorklist() peek error"
in
if degree_n >= k
then
spillWorklist := Splayset.add(!spillWorklist, n)
else
if moveRelated n
then
freezeWorklist := Splayset.add(!freezeWorklist, n)
else
simplifyWorklist := Splayset.add(!simplifyWorklist, n)
end)) (!initial)
(* adjacent : tigertemp.temp -> tigertemp.temp Splayset.set *)
fun adjacent n =
let
(*val _ = printDataStructure "selectStack"*)
val adjList_n = case Splaymap.peek(!adjList, n) of
SOME tmpSet => tmpSet
| NONE => raise Fail "Error - regAlloc. adjacent() peek error"
val union = Splayset.union(Splayset.addList(Splayset.empty String.compare, !selectStack), !coalescedNodes)
(*val _ = (print("\nLater... \n");
printDataStructure "selectStack")*)
in
Splayset.difference(adjList_n, union)
end
(* enableMoves : tigertemp.temp Splayset.set -> unit *)
fun enableMoves nodes =
Splayset.app (fn n =>
Splayset.app (fn m =>
if Splayset.member(!activeMoves, m)
then
(activeMoves := Splayset.difference(!activeMoves, Splayset.singleton cmpPairs m);
worklistMoves := Splayset.union(!worklistMoves, Splayset.singleton cmpPairs m))
else
()) (nodeMoves n)) nodes
(* decrementDegree : tigertemp.temp -> unit *)
fun decrementDegree m =
let
val singleton_m = Splayset.singleton String.compare m
val d = case Splaymap.peek(!degree, m) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. decrementDegree() peek error"
val _ = degree := Splaymap.insert(!degree, m, d - 1)
in
if d = k
then
(enableMoves (Splayset.union(singleton_m, adjacent m));
spillWorklist := Splayset.difference(!spillWorklist, singleton_m);
if moveRelated m
then
freezeWorklist := Splayset.union(!freezeWorklist, singleton_m)
else
simplifyWorklist := Splayset.union(!simplifyWorklist, singleton_m))
else
()
end
(* simplify : unit -> unit *)
fun simplify() =
case Splayset.find (fn _ =>
true) (!simplifyWorklist) of
SOME n => (simplifyWorklist := Splayset.difference(!simplifyWorklist, Splayset.singleton String.compare n);
pushStack n;
Splayset.app decrementDegree (adjacent n))
| NONE => ()
(* ok : (tigertemp.temp * tigertemp.temp) -> bool *)
fun ok(t,r) =
let
val degree_t = case Splaymap.peek(!degree, t) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. ok() peek error"
in
degree_t < k orelse Splayset.member(precolored, t) orelse Splayset.member(!adjSet, (t, r))
end
(* addWorkList : tigertemp.temp -> unit *)
fun addWorkList u =
let
val degree_u = case Splaymap.peek(!degree, u) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. addWorkList() peek error"
in
if not(Splayset.member(precolored, u)) andalso not(moveRelated u) andalso degree_u < k
then
let
val singleton_u = Splayset.singleton String.compare u
in
(freezeWorklist := Splayset.difference(!freezeWorklist, singleton_u);
simplifyWorklist := Splayset.union(!simplifyWorklist, singleton_u))
end
else
()
end
(* conservative : tigertemp.temp Splayset.set -> bool *)
fun conservative nodes =
let
val k' = ref 0
val _ = Splayset.app (fn n =>
let
val degree_n = case Splaymap.peek(!degree, n) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. conservative() peek error"
in
if degree_n >= k
then
k' := !k' + 1
else
()
end) nodes
in
!k' < k
end
(* getAlias : tigertemp.temp -> tigertemp.temp *)
fun getAlias n =
if Splayset.member(!coalescedNodes, n)
then
let
val alias_n = case Splaymap.peek(!alias, n) of
SOME a => a
| NONE => raise Fail "Error - regAlloc. getAlias() peek error"
in
getAlias alias_n
end
else
n
(* combine : (tigertemp.temp * tigertemp.temp) -> unit *)
fun combine(u, v) =
(if Splayset.member(!freezeWorklist, v)
then
freezeWorklist := Splayset.difference(!freezeWorklist, Splayset.singleton String.compare v)
else
spillWorklist := Splayset.difference(!spillWorklist, Splayset.singleton String.compare v);
coalescedNodes := Splayset.union(!coalescedNodes, Splayset.singleton String.compare v);
alias := Splaymap.insert(!alias, v, u);
let
val moveList_u = case Splaymap.peek(!moveList, u) of
SOME pair => pair
| NONE => raise Fail "Error - regAlloc. combine() peek error"
val moveList_v = case Splaymap.peek(!moveList, v) of
SOME pair => pair
| NONE => raise Fail "Error - regAlloc. combine() peek error"
in
moveList := Splaymap.insert(!moveList, u, Splayset.union(moveList_u, moveList_v))
end;
enableMoves(Splayset.singleton String.compare v);
Splayset.app (fn t =>
(addEdge(t, u); decrementDegree t)) (adjacent v);
let
val degree_u = case Splaymap.peek(!degree, u) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. combine() peek error"
in
if degree_u >= k andalso Splayset.member(!freezeWorklist, u)
then
(freezeWorklist := Splayset.difference(!freezeWorklist, Splayset.singleton String.compare u);
spillWorklist := Splayset.union(!spillWorklist, Splayset.singleton String.compare u))
else
()
end)
(* coalesce : unit -> unit *)
fun coalesce() =
case Splayset.find (fn _ => true) (!worklistMoves) of
NONE => ()
| SOME (m as (x', y')) => let
val x = getAlias x'
val y = getAlias y'
val (u, v) =
if Splayset.member(precolored, y)
then
(y, x)
else
(x, y)
val singleton_m = Splayset.singleton cmpPairs m
val _ = worklistMoves := Splayset.difference(!worklistMoves, singleton_m)
fun cond(u, v) : bool =
Splayset.foldl (fn (t, b) => b andalso ok(t, u)) true (adjacent v)
in
if u = v
then
(coalescedMoves := Splayset.union(!coalescedMoves, singleton_m);
addWorkList u)
else
if Splayset.member(precolored, v) orelse Splayset.member(!adjSet, (u, v))
then
(constrainedMoves := Splayset.union(!constrainedMoves, singleton_m);
addWorkList u;
addWorkList v)
else
if (Splayset.member(precolored, u) andalso cond(u, v))
orelse
(not(Splayset.member(precolored, u))
andalso conservative(Splayset.union(adjacent u, adjacent v)))
then
(coalescedMoves := Splayset.union(!coalescedMoves, singleton_m);
combine(u, v);
addWorkList u)
else
activeMoves := Splayset.union(!activeMoves, singleton_m)
end
(* freezeMoves : tigertemp.temp -> unit *)
fun freezeMoves u =
Splayset.app (fn (m as (x, y)) =>
let
val v = if (getAlias y) = (getAlias u)
then
getAlias x
else
getAlias y
(*val _ = (print("\nvalue of v = "^v^"\n");
print("\nm = "^(utils.pairToString m utils.id utils.id)^"\n"))*)
in
(activeMoves := Splayset.difference(!activeMoves, Splayset.singleton cmpPairs m);
frozenMoves := Splayset.union(!frozenMoves, Splayset.singleton cmpPairs m);
let
val degree_v = case Splaymap.peek(!degree, v) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. freezeMoves() peek error"
(*val _ = (print("\nisEmpty (nodeMoves v) = "^Bool.toString (Splayset.isEmpty(nodeMoves v))^"\n");
print("\ndegree v = "^(utils.intToString degree_v)^"\n"))*)
(*val _ = print("\nnodeMoves "^v^" = "^(utils.setToString (nodeMoves v) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")
val _ = print("\nnodeMoves "^u^" = "^(utils.setToString (nodeMoves u) (fn pair =>
utils.pairToString pair utils.id utils.id))^"\n")*)
in
if not(Splayset.member(precolored, v)) (* is this ok? Not in original pseudo-code. If we do not check this condition, the algorithm may color precolored registers. *)
then
if Splayset.isEmpty(nodeMoves v) andalso degree_v < k (* if v is a precolored node, degree[v] < k would always holds true *)
then
(freezeWorklist := Splayset.difference(!freezeWorklist, Splayset.singleton String.compare v);
simplifyWorklist := Splayset.union(!simplifyWorklist, Splayset.singleton String.compare v))
else
()
else
()
end)
end) (nodeMoves u)
(* freeze : unit -> unit *)
fun freeze() =
case Splayset.find (fn _ => true) (!freezeWorklist) of
NONE => ()
| SOME u => let
val singleton_u = Splayset.singleton String.compare u
in
(freezeWorklist := Splayset.difference(!freezeWorklist, singleton_u);
simplifyWorklist := Splayset.union(!simplifyWorklist, singleton_u);
freezeMoves u)
end
(* selectSpill : unit -> unit *)
fun selectSpill() =
let
(* Select a temporary from spillWorkList using the following heuristic *)
fun chooseMinIndex (tmp, currentMinIndex) =
let
val tmpIndex = case Int.fromString (String.extract(tmp, 1, NONE)) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. selectSpill(): imposible sustraer índice de temporario"
in
if tmpIndex < currentMinIndex
then
tmpIndex
else
currentMinIndex
end
val minIndex = Splayset.foldl chooseMinIndex (tigertemp.lastTempIndex()) (!spillWorklist)
val m = "T"^Int.toString minIndex
(*val _ = print("\nnode selected from selectSpill() = "^m^"\n")*)
val singleton_m = Splayset.singleton String.compare m
in
(spillWorklist := Splayset.difference(!spillWorklist, singleton_m);
simplifyWorklist := Splayset.union(!simplifyWorklist, singleton_m);
freezeMoves m)
end
(* repeat : unit -> unit *)
fun repeat() =
while not(Splayset.isEmpty (!simplifyWorklist)
andalso Splayset.isEmpty (!worklistMoves)
andalso Splayset.isEmpty (!freezeWorklist)
andalso Splayset.isEmpty (!spillWorklist)) do
(if not(Splayset.isEmpty (!simplifyWorklist))
then
simplify()
else
if not(Splayset.isEmpty (!worklistMoves))
then
coalesce()
else
if not(Splayset.isEmpty (!freezeWorklist))
then
freeze()
else
if not(Splayset.isEmpty (!spillWorklist))
then
selectSpill()
else
())
(* assignColors : unit -> unit *)
fun assignColors() =
(while ((!selectStack) <> []) do
(let
(*val _ = printDataStructure "selectStack"*)
val n = popStack()
(*val _ = print("\nn = "^n^"\n")*)
val okColors = ref kColors
val adjList_n = case Splaymap.peek(!adjList, n) of
SOME s => s
| NONE => raise Fail "Error - regAlloc. assignColors(): adjList peek error"
in
Splayset.app (fn w =>
if Splayset.member(Splayset.union(!coloredNodes, precolored), getAlias w)
then
let
val color_getAlias_w = case Splaymap.peek(!color, getAlias w) of
SOME tmp => tmp
| NONE => raise Fail "Error - regAlloc. assignColors(): color[getAlias w] peek error"
(*val _ = (print("\nw = "^w^"\n");
print("\ngetAlias[w] = "^color_getAlias_w^"\n"))*)
in
okColors := Splayset.difference(!okColors, Splayset.singleton String.compare color_getAlias_w)
end
else
()) adjList_n;
if Splayset.isEmpty (!okColors)
then
spilledNodes := Splayset.union(!spilledNodes, Splayset.singleton String.compare n)
else
(coloredNodes := Splayset.union(!coloredNodes, Splayset.singleton String.compare n);
let
val c = case Splayset.find (fn _ =>
true) (!okColors) of
SOME c' => c'
| NONE => "" (* is this ok? Should not display error not found? *)
(*val _ = (print("\nn = "^n^"\n");
print("\ncolor[n] = "^c^"\n"))*)
in
color := Splaymap.insert(!color, n, c)
end)
end
);
Splayset.app (fn n =>
let
val color_getAlias_n = case Splaymap.peek(!color, getAlias n) of
SOME c => c
| NONE => ""
in
color := Splaymap.insert(!color, n, color_getAlias_n)
end) (!coalescedNodes)
)
(* rewriteProgram() allocates memory locations for the spilled nodes temporaries and
inserts store and fetch instructions to access them. Those stores and fetches are
newly created temporaries (with tiny live ranges). *)
(* rewriteProgram : unit -> tigerassem.instr list *)
fun rewriteProgram() =
let
(* Table for mapping each v in spilledNodes to a memory location *)
val memLocTable : (tigertemp.temp, int) Splaymap.dict ref = ref (Splaymap.mkDict String.compare)
val spilledNodesList : tigertemp.temp list = Splayset.listItems (!spilledNodes)
(* Allocate memory locations for each v in spilledNodes *)
val spilledNodesList' : (tigertemp.temp * int) list = List.map (fn tmp =>
(tmp, tigerframe.getOffsetFromAccess(tigerframe.allocLocal frame true))) spilledNodesList
(* Update memLocTable *)
val _ = memLocTable := List.foldl (fn ((tmp, offset), dict) =>
Splaymap.insert(dict, tmp, offset)) (!memLocTable) spilledNodesList'
val newTemps : tigertemp.temp Splayset.set ref = ref (Splayset.empty String.compare)
(* newStore : tigertemp.temp -> tigertemp.temp -> tigerassem.instr *)
fun newStore oldTmp newTmp =
let
val oldTmpOffset = case Splaymap.peek(!memLocTable, oldTmp) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. rewriteProgram(). newStore() peek error"
in
tigerassem.OPER {
assem = "movq `s0, "^utils.intToString oldTmpOffset^"(%rbp) #\tSPILLED - STORE\n",
src = [newTmp],
dst = [],
jump = NONE
}
end
(* newLoad : tigertemp.temp -> tigertemp.temp -> tigerassem.instr *)
fun newLoad oldTmp newTmp =
let
val oldTmpOffset = case Splaymap.peek(!memLocTable, oldTmp) of
SOME i => i
| NONE => raise Fail "Error - regAlloc. rewriteProgram(). newLoad() peek error"
in
tigerassem.OPER {
assem = "movq "^utils.intToString oldTmpOffset^"(%rbp), `d0 #\tSPILLED - LOAD\n",
src = [],
dst = [newTmp],
jump = NONE
}
end
(* rewriteInstruction : tigerassem.instr -> tigerassem.instr list *)
fun rewriteInstruction (instr as (tigerassem.LABEL _)) = [instr]
| rewriteInstruction instr =
let
val instrUsesList : tigertemp.temp list = tigerassem.instrSrcsToList instr
val instrDefsList : tigertemp.temp list = tigerassem.instrDstsToList instr
(* Just consider spilled nodes *)
val instrUsesSpilledList : tigertemp.temp list = List.filter (fn useTmp =>
if Splayset.member(!spilledNodes, useTmp)
then
true
else
false) instrUsesList
val instrDefsSpilledList : tigertemp.temp list = List.filter (fn defTmp =>
if Splayset.member(!spilledNodes, defTmp)
then
true
else
false) instrDefsList
(* Create a new temporary v_i for each definition and each use; and put them all into newTemps set *)
val instrUsesPairList : (tigertemp.temp * tigertemp.temp) list = List.map (fn tmp =>
let
val newTmp = tigertemp.newtemp()
val _ = newTemps := Splayset.add(!newTemps, newTmp)
in
(tmp, newTmp)
end) instrUsesSpilledList
val instrDefsPairList : (tigertemp.temp * tigertemp.temp) list = List.map (fn tmp =>
let
val newTmp = tigertemp.newtemp()
val _ = newTemps := Splayset.add(!newTemps, newTmp)
in
(tmp, newTmp)
end) instrDefsSpilledList
(* Perform store's and load's *)
val storesList : tigerassem.instr list = List.map (fn (oldTmp, newTmp) =>
newStore oldTmp newTmp) instrDefsPairList
val loadsList : tigerassem.instr list = List.map (fn (oldTmp, newTmp) =>
newLoad oldTmp newTmp) instrUsesPairList
(* getReplaceTmp : tigertemp.temp -> (tigertemp.temp * tigertemp.temp) list -> tigertemp.temp *)
fun getReplaceTmp tmp listOfPairs =
let
val newTmp = case List.find (fn (old, new) =>
tmp = old) listOfPairs of
SOME (_, new) => new
| NONE => tmp
in
newTmp
end
(* getReplace : tigertemp.temp list -> (tigertemp.temp * tigertemp.temp) list -> tigertemp.temp list *)
fun getReplace [] _ = []
| getReplace l listOfPairs =
let
val mapped = List.map (fn tmp =>
getReplaceTmp tmp listOfPairs) l
in
mapped
end
(* Now, rewrite instruction placing new recently created temporaries *)
val newInstr : tigerassem.instr = case instr of
tigerassem.OPER {assem, src, dst, jump} => tigerassem.OPER {
assem = assem,
src = getReplace src instrUsesPairList,
dst = getReplace dst instrDefsPairList,
jump = jump
}
| tigerassem.MOVE {assem, src, dst} => tigerassem.MOVE {
assem = assem,
src = getReplaceTmp src instrUsesPairList,
dst = getReplaceTmp dst instrDefsPairList
}
| x => x
in
(* this has to be a list of tigerassem.instr *)
loadsList
@ [newInstr]
@ storesList
end
(* Get only instructions from iGraph *)
val iGraphList : tigerliveness.node list = Splayset.listItems (!iGraph)
val instrList : tigerassem.instr list = List.map (fn node =>
tigerliveness.getInstrFromNode node) iGraphList
in
List.concat (List.map rewriteInstruction instrList) : tigerassem.instr list
end
(* checkInvariants : unit -> unit *)
fun checkInvariants() =
let
(* INVARIANTS. After makeWorklist(), the following invariants always hold *)
fun degreeInv() =
let
val unionSet = Splayset.union(Splayset.union(!simplifyWorklist, !freezeWorklist), !spillWorklist)
val unionSet' = Splayset.union(unionSet, precolored)
val boolRes = Splayset.foldl (fn (tmp, b) =>
(Splaymap.find(!degree, tmp) = Splayset.numItems(Splayset.intersection(unionSet', Splaymap.find(!adjList, tmp)))) andalso b) true unionSet
handle NotFound => raise Fail "Error - regAlloc. degreeInv(): exception NotFound"
in
if not(boolRes)
then
raise Fail "Error - regAlloc. degreeInv does not hold!"
else
()
end
fun simplifyWorklistInv() =
let
val unionSet = Splayset.union(!activeMoves, !worklistMoves)
val boolRes = Splayset.foldl (fn (tmp, b) =>
Splaymap.find(!degree, tmp) < k andalso Splayset.isEmpty(Splayset.intersection(Splaymap.find(!moveList, tmp), unionSet)) andalso b) true (!simplifyWorklist)
handle NotFound => raise Fail "Error - regAlloc. simplifyWorklistInv(): exception NotFound"
in
if not(boolRes)
then
raise Fail "Error - regAlloc. simplifyWorklistInv does not hold!"
else
()
end
fun freezeWorklistInv() =
let
val unionSet = Splayset.union(!activeMoves, !worklistMoves)
val boolRes = Splayset.foldl (fn (tmp, b) =>
Splaymap.find(!degree, tmp) < k andalso not(Splayset.isEmpty(Splayset.intersection(Splaymap.find(!moveList, tmp), unionSet))) andalso b) true (!freezeWorklist)
handle NotFound => raise Fail "Error - regAlloc. freezeWorklistInv(): exception NotFound"
in
if not(boolRes)
then
raise Fail "Error - regAlloc. freezeWorklistInv does not hold!"
else
()
end
fun spillWorklistInv() =
let
val boolRes = Splayset.foldl (fn (tmp, b) =>
Splaymap.find(!degree, tmp) >= k andalso b) true (!spillWorklist)
handle NotFound => raise Fail "Error - regAlloc. spillWorklistInv(): exception NotFound"
in
if not(boolRes)
then
raise Fail "Error - regAlloc. spillWorklistInv does not hold!"
else
()
end
in
(degreeInv();
simplifyWorklistInv();
freezeWorklistInv();
spillWorklistInv())
end
(* Deletes tigerassem.MOVE's with same src and dst parameters from
list of tigerassem.instr *)
(* deleteMoves : tigerassem.instr list -> tigerassem.instr list *)
fun deleteMoves [] = []
| deleteMoves ((move as tigerassem.MOVE {src, dst, ...}) :: moves) =
let
val src' = case Splaymap.peek(!color, src) of
SOME tmp => tmp
| NONE => let
val _ = print("\nsrc = "^src^"\n")
in
raise Fail "Error - regAlloc. deleteMoves(): src not in color dict"
end
val dst' = case Splaymap.peek(!color, dst) of
SOME tmp => tmp
| NONE => let
val _ = print("\ndst = "^dst^"\n")
in
raise Fail "Error - regAlloc. deleteMoves(): dst not in color dict"
end
in
if src' = dst'
then