-
Notifications
You must be signed in to change notification settings - Fork 0
/
ra_main.ml
executable file
·1335 lines (1188 loc) · 38 KB
/
ra_main.ml
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
(*
* The register allocator performs the following steps:
* 1. Code generation
* 2. Register assignment
* 3. Move coalescing
*
* This allocator is based directly on Appel.
*
* ----------------------------------------------------------------
*
* Copyright (C) Kai Chen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*)
open Format
open Debug
open Symbol
open Backend
open Symbol_matrix
open Ra_live
open Ra_state
module type RegAllocSig =
sig
type block
type spset
val compile : spset -> block list -> spset * block list
end
module RegAlloc (Backend : BackendSig)
: RegAllocSig
with type block = Backend.block
with type spset = Backend.spset =
struct
type block = Backend.block
type spset = Backend.spset
(*
* Modules.
*)
module Live = Live (Backend)
(*
* Classification for nodes.
* A node can be:
* Precolored: a machine register
* SimpWL: on the simplify worklist
* SpillWL: on the spill worklist
* Spilled: spilled
* Coalesced: coalesced (set equal to another node)
* Colored: assigned to a machine register
* Stack: placed on the allocation stack
*
* We want to support two functions efficiently:
* reclassify node class: reclassify a node
* nodes_in_class class: all the nodes with a given class
*
* Reclassify takes constant time.
*)
type node_class =
NodePrecolored
| NodeInitial
| NodeSimpWL
| NodeFreezeWL
| NodeSpillWL
| NodeSpilled
| NodeCoalesced
| NodeColored
| NodeStack
(*
* Moves are also classified.
* Coalesced: move has been coalesced
* Constrained: the nodes in the move interfere
* Frozen: move is not being considered for coalescing
* WL: on the move worklist for consideration
* Active: may be considered for coalescing eventually
*)
type move_class =
MoveCoalesced
| MoveConstrained
| MoveFrozen
| MoveWL
| MoveActive
(*
* For each node we keep
* node_node: the classification of the node
* node_class: the current classification of the node
* node_degree: the number of neighbors
* node_alias: a new name for the node
* node_color: color that is assigned to a node
* node_moves: all the moves the node is in
*)
type node =
{ node_name : var;
mutable node_class : node_class;
mutable node_degree : int;
mutable node_alias : node option;
mutable node_color : var option;
mutable node_moves : move list;
mutable node_neighbors : node list;
(* Linked list *)
mutable node_pred : node option ref;
node_succ : node option ref
}
(*
* For the move
* move_dst: dst of the move
* move_src: src of the move
* move_class: current classification of the move
*)
and move =
{ move_dst : node;
move_src : node;
mutable move_class : move_class;
(* Linked list *)
mutable move_pred : move option ref;
move_succ : move option ref
}
(*
* All this stuff is defined in Appel,
* "Modern Compiler Implementation in ML",
* 1998, Cambridge University Press,
* pages 242-243.
*)
type ra =
{ (* Node worklists *)
ra_precolored : node option ref;
ra_initial : node option ref;
ra_simp_wl : node option ref;
ra_freeze_wl : node option ref;
ra_spill_wl : node option ref;
ra_spilled : node option ref;
ra_coalesced : node option ref;
ra_colored : node option ref;
ra_stack : node option ref;
(* Move worklists *)
mv_coalesced : move option ref;
mv_constrained : move option ref;
mv_frozen : move option ref;
mv_wl : move option ref;
mv_active : move option ref;
(* Explicit representation of the graph *)
ra_edges : SymbolMatrix.t
}
(************************************************************************
* NODE OPERATIONS
************************************************************************)
(*
* Get the list pointer for a node class.
*)
let node_worklist ra_nodes = function
NodePrecolored -> ra_nodes.ra_precolored
| NodeInitial -> ra_nodes.ra_initial
| NodeSimpWL -> ra_nodes.ra_simp_wl
| NodeFreezeWL -> ra_nodes.ra_freeze_wl
| NodeSpillWL -> ra_nodes.ra_spill_wl
| NodeSpilled -> ra_nodes.ra_spilled
| NodeCoalesced -> ra_nodes.ra_coalesced
| NodeColored -> ra_nodes.ra_colored
| NodeStack -> ra_nodes.ra_stack
(*
* Test for empty.
*)
let node_list_is_empty ra cl =
let l = node_worklist ra cl in
match !l with
Some _ -> false
| None -> true
(*
* Get the head of a list.
*)
let node_list_head ra cl =
let l = node_worklist ra cl in
match !l with
Some node -> node
| None -> raise (Invalid_argument "node_list_head")
(*
* Iterate through the node list.
*)
let node_iter ra cl f =
let rec iter = function
Some node ->
let next = !(node.node_succ) in
f node;
iter next
| None ->
()
in
iter (!(node_worklist ra cl))
let node_fold ra cl f x =
let rec fold x node =
match node with
Some node ->
let next = !(node.node_succ) in
let x = f x node in
fold x next
| None ->
x
in
fold x (!(node_worklist ra cl))
let node_to_list ra cl =
let rec collect = function
Some node ->
node :: collect (!(node.node_succ))
| None ->
[]
in
collect (!(node_worklist ra cl))
(*
* Get successor node.
*)
let node_succ node =
match !(node.node_succ) with
Some node -> node
| None -> raise (Invalid_argument "node_succ")
(*
* Has a succ?
*)
let node_has_no_succ node =
match !(node.node_succ) with
Some _ -> false
| None -> true
(*
* Create a node in a class.
*)
let new_node ra v cl =
let l = node_worklist ra cl in
let next = !l in
let succ = ref next in
(* Postpone computation of neighbors *)
let node' =
{ node_name = v;
node_class = cl;
node_degree = 0;
node_alias = None;
node_color = None;
node_neighbors = [];
node_moves = [];
node_pred = l;
node_succ = succ
}
in
let _ =
(* Link it into the list *)
l := Some node';
match next with
Some next -> next.node_pred <- succ
| None -> ()
in
node'
(*
* Reclassify the node.
*)
let node_reclassify ra node cl =
(* Delete the node from its current list *)
let pred = node.node_pred in
let succ = !(node.node_succ) in
let _ =
pred := succ;
match succ with
Some next -> next.node_pred <- pred
| None -> ()
in
(* Add to the new list *)
let l = node_worklist ra cl in
let next = !l in
l := Some node;
assert(node.node_class <> NodePrecolored);
node.node_class <- cl;
node.node_pred <- l;
node.node_succ := next;
match next with
Some next -> next.node_pred <- node.node_succ
| None -> ()
(*
* Test for equality.
*)
let node_eq node1 node2 =
Symbol.eq node1.node_name node2.node_name
(*
* Get the moves for a node.
*)
let node_moves ra node =
List.filter (fun move ->
match move.move_class with
MoveActive
| MoveWL ->
true
| MoveFrozen
| MoveCoalesced
| MoveConstrained ->
false) node.node_moves
(*
* Get the node's neighbors.
*)
let node_neighbors ra node =
List.filter (fun node ->
match node.node_class with
NodeStack
| NodeCoalesced ->
false
| _ ->
true) node.node_neighbors
(*
* Spilled neighbors.
*)
let node_spill_neighbors ra node =
List.filter (fun node ->
match node.node_class with
NodeSpilled ->
true
| _ ->
false) node.node_neighbors
(*
* Say if a node is move related.
*)
let node_moves node =
List.filter (fun move ->
match move.move_class with
MoveWL
| MoveActive ->
true
| MoveCoalesced
| MoveFrozen
| MoveConstrained ->
false) node.node_moves
let node_is_move_related node =
List.exists (fun move ->
match move.move_class with
MoveWL
| MoveActive ->
true
| MoveCoalesced
| MoveFrozen
| MoveConstrained ->
false) node.node_moves
(*
* Union of two node lists.
*)
let node_list_union nodes1 nodes2 =
let rec mem node1 = function
node2 :: nodes2 ->
node_eq node1 node2 || mem node1 nodes2
| [] ->
false
in
let rec collect nodes1 nodes2 =
match nodes1 with
node1 :: nodes1 ->
if mem node1 nodes2 then
collect nodes1 nodes2
else
node1 :: collect nodes1 nodes2
| [] ->
nodes2
in
collect nodes1 nodes2
(*
* Get string representation (var) of a node.
*)
let var_of_node ra node =
node.node_name
let string_of_node ra node =
string_of_symbol (var_of_node ra node)
(************************************************************************
* MOVE OPERATIONS
************************************************************************)
(*
* Get the list pointer for a node class.
*)
let move_worklist ra_moves = function
MoveCoalesced -> ra_moves.mv_coalesced
| MoveConstrained -> ra_moves.mv_constrained
| MoveFrozen -> ra_moves.mv_frozen
| MoveWL -> ra_moves.mv_wl
| MoveActive -> ra_moves.mv_active
(*
* Get the head of a list.
*)
let move_list_head ra cl =
let l = move_worklist ra cl in
match !l with
Some move -> move
| None -> raise (Invalid_argument "move_list_head")
(*
* Iterate through the node list.
*)
let move_iter ra cl f =
let rec iter = function
Some move ->
let next = !(move.move_succ) in
f move;
iter next
| None ->
()
in
iter (!(move_worklist ra cl))
let move_fold ra cl f x =
let rec fold x move =
match move with
Some move ->
let next = !(move.move_succ) in
let x = f x move in
fold x next
| None ->
x
in
fold x (!(move_worklist ra cl))
(*
* Create a node in a class.
*)
let new_move ra_moves dst src cl =
let l = move_worklist ra_moves cl in
let next = !l in
let succ = ref next in
let node' =
{ move_dst = dst;
move_src = src;
move_class = cl;
move_pred = l;
move_succ = succ
}
in
let _ =
(* Link it into the list *)
l := Some node';
match next with
Some next -> next.move_pred <- succ
| None -> ()
in
node'
(*
* Reclassify the node.
*)
let move_reclassify ra move cl =
(* Delete the move from its current list *)
let pred = move.move_pred in
let succ = !(move.move_succ) in
let _ =
pred := succ;
match succ with
Some next -> next.move_pred <- pred
| None -> ()
in
(* Add to the new list *)
let l = move_worklist ra cl in
let next = !l in
l := Some move;
move.move_class <- cl;
move.move_pred <- l;
move.move_succ := next;
match next with
Some next -> next.move_pred <- move.move_succ
| None -> ()
(************************************************************************
* PRINTING
************************************************************************)
(*
* Print the register allocator state.
*)
let print_ra ra =
node_iter ra NodePrecolored (fun node ->
let v = var_of_node ra node in
match node.node_color with
Some v' ->
eprintf "Colored: %s->%s@." (string_of_symbol v) (string_of_symbol v')
| None ->
raise (Invalid_argument "Register_alloc.set_colors"));
node_iter ra NodeInitial (fun node ->
eprintf "Initial: %s@." (string_of_node ra node));
node_iter ra NodeSimpWL (fun node ->
eprintf "Simp WL: %s@." (string_of_node ra node));
node_iter ra NodeFreezeWL (fun node ->
eprintf "Freeze WL: %s@." (string_of_node ra node));
node_iter ra NodeSpillWL (fun node ->
eprintf "Spill WL: %s@." (string_of_node ra node));
node_iter ra NodeSpilled (fun node ->
eprintf "Spilled: %s@." (string_of_node ra node));
node_iter ra NodeCoalesced (fun node ->
eprintf "Coalesced: %s@." (string_of_node ra node));
node_iter ra NodeStack (fun node ->
eprintf "Stack: %s@." (string_of_node ra node));
node_iter ra NodeColored (fun node ->
let v = var_of_node ra node in
match node.node_color with
Some v' ->
eprintf "Colored: %s->%s@." (string_of_symbol v) (string_of_symbol v')
| None ->
raise (Invalid_argument "Register_alloc.set_colors"))
(************************************************************************
* CLASSIFY NODES
************************************************************************)
(*
* Precolored nodes.
*
* Number of colors assigned to registers.
*)
let max_colors = List.length Backend.registers
(*
* Classify the nodes.
* Two parts:
* Part 1: create all the nodes
* Part 2: add all the edges
*)
let classify_nodes ra graph =
if debug debug_regalloc then
eprintf "Classify nodes@.";
let add_edge node1 node2 =
SymbolMatrix.add ra.ra_edges node1.node_name node2.node_name
in
let nodes =
SymbolTable.mapi (fun v _ ->
if List.mem v Backend.registers then
let node = new_node ra v NodePrecolored in
node.node_color <- Some v;
node
else
new_node ra v NodeInitial) graph
in
let _ =
(* Now add all the edges *)
SymbolTable.iter (fun v node ->
(* Convert neighbor list to a node list *)
let neighbors = SymbolTable.find graph v in
let neighbors =
SymbolSet.fold (fun neighbors v ->
let node = SymbolTable.find nodes v in
node :: neighbors) [] neighbors
in
if node.node_class <> NodePrecolored then
begin
node.node_degree <- List.length neighbors;
node.node_neighbors <- neighbors
end;
List.iter (add_edge node) neighbors) nodes
in
nodes
(*
* Reclassify all the nodes.
*)
let reclassify_nodes ra =
if !debug_regalloc then
eprintf "Reclassify nodes@.";
node_iter ra NodeInitial (fun node ->
let state =
if node.node_degree >= max_colors then
NodeSpillWL
else if node_is_move_related node then
NodeFreezeWL
else
NodeSimpWL
in
node_reclassify ra node state)
(*
* Compute on the moves.
*)
let classify_moves ra nodes moves =
if !debug_regalloc then
eprintf "Classify moves@.";
SymbolMatrix.iter (fun dst src ->
let dst_node = SymbolTable.find nodes dst in
let src_node = SymbolTable.find nodes src in
let move = new_move ra dst_node src_node MoveWL in
dst_node.node_moves <- move :: dst_node.node_moves;
src_node.node_moves <- move :: src_node.node_moves) moves
(*
* Classify nodes/moves into worklists.
*)
let create igraph =
let { igraph_neighbors = neighbors;
igraph_moves = moves
} = igraph
in
let _ =
if !debug_regalloc then
eprintf "Register alloc create@."
in
let size =
SymbolTable.fold (fun size _ live ->
size + SymbolSet.cardinal live) 0 neighbors
in
let ra =
{ ra_precolored = ref None;
ra_initial = ref None;
ra_simp_wl = ref None;
ra_freeze_wl = ref None;
ra_spill_wl = ref None;
ra_spilled = ref None;
ra_coalesced = ref None;
ra_colored = ref None;
ra_stack = ref None;
mv_coalesced = ref None;
mv_constrained = ref None;
mv_frozen = ref None;
mv_wl = ref None;
mv_active = ref None;
ra_edges = SymbolMatrix.create (size / 3)
}
in
(* Classify all the nodes *)
let nodes = classify_nodes ra neighbors in
(* Classify all the moves *)
classify_moves ra nodes moves;
(* Reclassify all the nodes *)
reclassify_nodes ra;
(* Print it *)
if !debug_regalloc then
print_ra ra;
ra
(************************************************************************
* UTILITIES
************************************************************************)
(*
* Query the graph for an edge.
*)
let query ra node1 node2 =
SymbolMatrix.query ra.ra_edges node1.node_name node2.node_name
(*
* Add an edge to the graph.
*)
let add_edge ra node1 node2 =
let name1 = node1.node_name in
let name2 = node2.node_name in
if not (query ra node1 node2) && not (Symbol.eq name1 name2) then
begin
SymbolMatrix.add ra.ra_edges name1 name2;
if node1.node_class <> NodePrecolored then
begin
node1.node_neighbors <- node2 :: node1.node_neighbors;
node1.node_degree <- succ node1.node_degree
end;
if node2.node_class <> NodePrecolored then
begin
node2.node_neighbors <- node1 :: node2.node_neighbors;
node2.node_degree <- succ node2.node_degree
end
end
(*
* Enable the moves in the nodes.
*)
let enable_moves ra nodes =
List.iter (fun node ->
List.iter (fun move ->
if move.move_class = MoveActive then
move_reclassify ra move MoveWL) (node_moves node)) nodes
(*
* Decrement the degree of a node.
* This may enable some previously blocked moves.
*)
let decrement_degree ra node =
if node.node_class <> NodePrecolored then
let degree = node.node_degree in
node.node_degree <- pred degree;
if degree = max_colors then
begin
enable_moves ra (node :: node_neighbors ra node);
if node_is_move_related node then
node_reclassify ra node NodeFreezeWL
else
node_reclassify ra node NodeSimpWL
end
(*
* Get the alias for a node.
*)
let rec node_alias node =
match node.node_alias with
Some node ->
node_alias node
| None ->
node
(*
* Add a node to a worklist.
*)
let add_worklist ra node =
if node.node_class <> NodePrecolored
&& not (node_is_move_related node)
&& node.node_degree < max_colors
then
node_reclassify ra node NodeSimpWL
(************************************************************************
* SIMPLIFY
************************************************************************)
(*
* Simplify.
*
* Implemented by: turtles
*)
let simplify ra =
let node = node_list_head ra NodeSimpWL in
node_reclassify ra node NodeStack;
List.iter (decrement_degree ra) (node_neighbors ra node)
(************************************************************************
* COALESCE
************************************************************************)
(*
* Coalesce two nodes.
* Given in the lecture slides, for the most part.
*
* Implemented by: turtles
*)
let combine_normal ra u v =
(* Join/combine the move lists. *)
u.node_moves <- u.node_moves @ v.node_moves;
(* Update the interferences of u now. *)
let iterator = fun t ->
add_edge ra t u;
(* We need to counteract the degree increment from add_edge. *)
decrement_degree ra t
in
List.iter iterator (node_neighbors ra v);
(* Update the alias for v and its status. *)
node_reclassify ra v NodeCoalesced;
v.node_alias <- Some u;
(* Possibly reclassify u. *)
if u.node_degree >= max_colors && u.node_class = NodeFreezeWL then
node_reclassify ra u NodeSpillWL
(*
* This case is almost the same as combine_normal, but u is precolored.
* We don't need to combine the moves (according to the labs list).
* The important difference is we need to call node_alias on the neighbors.
* (We also shouldn't try to spill u...)
*
* Implemented by: turtles
*)
let combine_precolored ra u v =
(* Update the interferences of u now. *)
let iterator = fun t ->
(* We need to find the aliases for v's neighbors. *)
let t = node_alias t in
add_edge ra t u;
(* We need to counteract the degree increment from add_edge. *)
decrement_degree ra t
in
List.iter iterator (node_neighbors ra v);
(* We need to update the aliases and status for v. *)
node_reclassify ra v NodeCoalesced;
v.node_alias <- Some u
(**** Helper functions: conservative tests for coalescing. ****)
(*
* Briggs test.
* Two nodes u, v can be coalesced if the resulting node uv will have
* fewer than max_colors neighbors of significant degree.
*
* Implemented by: turtle
*)
let briggs_test ra u v =
(* Compute the neighbors of the node after coalescing. *)
let u_neighbors = node_neighbors ra u in
let v_neighbors = node_neighbors ra v in
let neighbors = node_list_union u_neighbors v_neighbors in
(* Count the significant neighbors in the union *)
let iterator = fun count node ->
if node.node_degree >= max_colors then
succ count
else
count
in
let significant_neighbors = List.fold_left iterator 0 neighbors in
significant_neighbors < max_colors
(*
* George test.
* Two nodes u, v can be coalesced if, for every neighbor t of v,
* either t already interferes with v, or t is of insignificant degree.
*
* Implemented by: turtles
*)
let george_test ra u v =
let check = fun node ->
node.node_degree < max_colors || query ra node u
in
List.for_all check (node_neighbors ra v)
(*
* Coalesce.
*
* Implemented by: turtles
*)
let coalesce ra =
(* Pick a move. *)
let move = move_list_head ra MoveWL in
(* Get aliases of dst and src for the move. *)
let x = node_alias move.move_dst in
let y = node_alias move.move_src in
(* If one of them is precolored, make sure u is that one. *)
let u, v =
if y.node_class = NodePrecolored then
y, x
else
x, y
in
(* Equal nodes, so reclassify the move. *)
if node_eq u v then
begin
move_reclassify ra move MoveCoalesced;
add_worklist ra u
end
(* Check for a constrained move. *)
else if v.node_class = NodePrecolored || query ra u v then
begin
move_reclassify ra move MoveConstrained;
add_worklist ra u;
add_worklist ra v
end
(* If u is precolored and it passes George test, coalesce.
* Since u is precolored, we do not add it to simplify worklist. *)
else if u.node_class = NodePrecolored && george_test ra u v then
begin
move_reclassify ra move MoveCoalesced;
combine_precolored ra u v
end
(* If u is not precolored and it passes Briggs test, coalesce. *)
else if u.node_class <> NodePrecolored && briggs_test ra u v then
begin
move_reclassify ra move MoveCoalesced;
combine_normal ra u v;
add_worklist ra u
end
(* Otherwise, reclassify the move as active. *)
else
move_reclassify ra move MoveActive
(*
* Aggresive coalesce for spilled nodes.
*)
let coalesce_spills ra spset =
if !debug_regalloc then
eprintf "CoalesceSpills@.";
move_fold ra MoveFrozen (fun spset move ->
let u = node_alias move.move_dst in
let v = node_alias move.move_src in
if not (node_eq u v) && u.node_class = NodeSpilled && v.node_class = NodeSpilled then
let spset = Backend.spset_add spset (var_of_node ra v) (var_of_node ra u) in
combine_precolored ra u v;
spset
else
spset) spset
(************************************************************************
* FREEZE
************************************************************************)
(*
* Freeze the moves for a node.
*
* Implemented by: turtles
*)
let freeze_moves ra node =
(* Get the node that u aliases, and its moves. *)
let u = node_alias node in
let moves = node_moves node in
(* Iterate over all the moves now. We want to try to simplify
* a node as a result of this, if possible. *)
let iterator = fun move ->
let y = node_alias move.move_src in
let v =
if node_eq y u then
node_alias move.move_dst
else
y
in
move_reclassify ra move MoveFrozen;
if not (node_is_move_related v) && v.node_degree < max_colors then
node_reclassify ra node NodeSimpWL
in
List.iter iterator moves
(*
* Freeze.
*)
let freeze ra =
if !debug_regalloc then
eprintf "Freeze@.";
let node = node_list_head ra NodeFreezeWL in
node_reclassify ra node NodeSimpWL;
freeze_moves ra node
(************************************************************************
* SPILL
************************************************************************)
(*
* Select a node for spilling.
* We pick a variable with max interference to spill.
*
* Implemented by: turtles
*)
(*
let spill ra vars =
(* search for the node with max interference *)