aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/regalloc/hipe_optimistic_regalloc.erl
blob: 5bad31ade933057f7d2b4719777e296c8744a02a (plain) (blame)
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
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2005-2013. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%-----------------------------------------------------------------------
%% File    : hipe_optimistic_regalloc.erl
%% Authors : NilsOla Linnermark <nilsola@abc.se>
%%           Petter Holmberg <petter.holmberg@usa.net>
%% Purpose : Play paintball with registers on a target machine.  We win
%%           if they are all colored.  This is an optimistic coalescing
%%           register allocator.
%% Created : Spring 2005
%%-----------------------------------------------------------------------

-module(hipe_optimistic_regalloc).
-export([regalloc/5]).

-ifndef(DEBUG).
%%-define(DEBUG,true).
-else.
-ifndef(COMPARE_ITERATED_OPTIMISTIC).
%% If this macro is turned on you can easily compare 
%% each intermediate step in the iterated coalescing
%% register allocator and the optimitsitc coalescing
%% register allocator. This is useful for debugging -
%% many small erlang functions should render the same
%% register allocaton for both allocators.
-define(COMPARE_ITERATED_OPTIMISTIC, true).
-endif.
-endif.
-include("../main/hipe.hrl").
-ifdef(DEBUG_PRINTOUTS).
-define(print_adjacent(IG), hipe_ig:print_adjacent(IG)).
-define(print_degrees(IG), hipe_ig:print_degrees(IG)).
-define(print_spill_costs(IG),  hipe_ig:print_spill_costs(IG)).
-define(mov_print_memberships(MV),  hipe_moves:print_memberships(MV)).
-define(reg_print_memberships(WL), hipe_reg_worklists:print_memberships(WL)).
-define(print_alias(A), printAlias(A)).
-define(print_colors(T,C), printColors(T,C)).
-else.
-define(print_adjacent(IG), no_print).
-define(print_degrees(IG), no_print).
-define(print_spill_costs(IG), no_print).
-define(mov_print_memberships(MV), no_print).
-define(reg_print_memberships(WL), no_print).
-define(print_alias(A), no_print).
-define(print_colors(T,C), no_print).
-endif.


%%-----------------------------------------------------------------------
%% Function:    regalloc
%%
%% Description: Creates a K coloring for a function.
%% Parameters:
%%   CFG         -- A control flow graph
%%   SpillIndex  -- Last index of spill variable
%%   SpillLimit  -- Temporaris with numbers higher than this have
%%                  infinit spill cost. 
%%                  Consider changing this to a set.
%%   Target      -- The module containing the target-specific functions.
%%
%% Returns:
%%   Coloring    -- A coloring for specified CFG
%%   SpillIndex0 -- A new spill index
%%-----------------------------------------------------------------------
-ifdef(COMPARE_ITERATED_OPTIMISTIC).
regalloc(CFG, SpillIndex, SpillLimit, Target, _Options) ->
  ?debug_msg("optimistic ~w\n",[Target]),
  ?debug_msg("CFG: ~p\n",[CFG]),
  %% Build interference graph
  ?debug_msg("Build IG\n",[]),
  IG_O = hipe_ig:build(CFG, Target),
  IG = hipe_ig:build(CFG, Target),
  ?debug_msg("adjlist: ~p\n",[hipe_ig:adj_list(IG)]),
  ?debug_msg("IG:\n",[]),
  ?print_adjacent(IG),
  ?print_degrees(IG),
  ?print_spill_costs(IG),

  SavedSpillCosts = hipe_ig:spill_costs(IG),
  SavedAdjList = hipe_ig:adj_list(IG),

  ?debug_msg("Init\n",[]),
  No_temporaries = Target:number_of_temporaries(CFG),
  ?debug_msg("Coalescing RA: num_temps = ~p~n", [No_temporaries]),
  Allocatable = Target:allocatable(),
  K = length(Allocatable),
  All_colors = colset_from_list(Allocatable),
  ?debug_msg("K: ~w~nAll_colors: ~p\n",[K, All_colors]), 

  %% Add registers with their own coloring
  ?debug_msg("Moves\n",[]),
  Move_sets_O = hipe_moves:new(IG_O),
  Move_sets = hipe_moves:new(IG),
  ?debug_msg("Move_sets:\n ~p\n",[Move_sets]),
  ?mov_print_memberships(Move_sets),

  ?debug_msg("Build Worklist\n",[]),
  Worklists_O = hipe_reg_worklists:new(IG_O, Target, CFG, Move_sets_O, K, No_temporaries),
  ?debug_msg("Worklists:\n ~p\n", [Worklists_O]),
  ?reg_print_memberships(Worklists_O),

  Worklists = hipe_reg_worklists:new(IG, Target, CFG, K, No_temporaries),
  ?debug_msg("New Worklists:\n ~p\n", [Worklists]),
  ?reg_print_memberships(Worklists),

  Alias_O = initAlias(No_temporaries),
  Alias = initAlias(No_temporaries),
  ?print_alias(Alias),

  ?debug_msg("Do coloring\n~p~n",[Worklists_O]),
  {IG0_O, Worklists0_O, Moves0_O, Alias0_O} = 
    do_coloring(IG_O, Worklists_O, Move_sets_O, Alias_O,
		K, SpillLimit, Target),
  ?debug_msg("IG_O after color:\n ~p\n",[IG0_O]),
  ?print_adjacent(IG0_O),
  ?print_degrees(IG0_O),
  ?print_spill_costs(IG0_O),
  ?debug_msg("Move_sets after color:\n ~p\n",[Moves0_O]),
  ?mov_print_memberships(Moves0_O),
  ?debug_msg("Worklists after color:\n ~p\n", [Worklists0_O]),
  ?reg_print_memberships(Worklists0_O),

  {IG0, Moves0, Alias0, Worklists0} = 
    do_coalescing(IG, Worklists, Move_sets, Alias, K, Target),
  ?debug_msg("IG after coalescing:\n",[]),
  ?print_adjacent(IG0),
  ?print_degrees(IG0),
  ?print_spill_costs(IG0),
  ?debug_msg("Move_sets after coalescing:\n ~p\n",[Moves0]),
  ?mov_print_memberships(Moves0),
  ?debug_msg("New Worklists after coalescing:\n ~p\n",
             [Worklists0]),
  ?reg_print_memberships(Worklists0),

  {IG1, Worklists1, Moves1, Alias1} = 
    do_simplify_or_spill(IG0, Worklists0, Moves0, Alias0, 
                         K, SpillLimit, Target),
  ?debug_msg("IG after simplify_or_spill:\n",[]),
  ?print_adjacent(IG1),
  ?print_degrees(IG1),
  ?print_spill_costs(IG1),
  ?debug_msg("Saved spill costs ~p~n", [SavedSpillCosts]),
  ?debug_msg("Move_sets after simplify_or_spill:\n ~p\n",[Moves1]),
  ?mov_print_memberships(Moves1),
  ?debug_msg("New Worklists after simplify_or_spill:\n ~p\n",
             [Worklists1]),
  ?reg_print_memberships(Worklists1),
  ?print_alias(Alias1),

  %% only for testing undoCoalescing and member_coalesced_to
  %test_undoCoalescing(No_temporaries, Alias1, Worklists1),

  %% only for testing fixAdj
  %?debug_msg("adj_lists_before_fixAdj ~n~p~n", [hipe_ig:adj_list(IG1)]),
  %IG2 = test_fixAdj(No_temporaries, SavedAdjList, IG1, Target),
  %?debug_msg("adj_lists__after_fixAdj ~n~p~n", [hipe_ig:adj_list(IG2)]),

  ?debug_msg("Init node sets\n",[]),
  Node_sets = hipe_node_sets:new(),
  %% ?debug_msg("NodeSet: ~w\n NonAlloc ~w\n",[Node_sets,Target:non_alloc(CFG)]),
  ?debug_msg("Default coloring\n",[]),
  {Color0,Node_sets1} = 
    defaultColoring(Target:all_precoloured(),
		    initColor(No_temporaries), Node_sets, Target),
  ?debug_msg("Color0\n",[]),
  ?print_colors(No_temporaries, Color0),

  ?debug_msg("----------------------Assign colors _N\n",[]),

  Stack = hipe_reg_worklists:stack(Worklists1), 
  ?debug_msg("The stack _N ~p~n", [Stack]), 
  %SortedStack = sort_stack(Stack),
  %?debug_msg("The stack _N ~p~n", [SortedStack]), 

  %?debug_msg("Nodes _N ~w~n", [Node_sets1]),

  {Color1,Node_sets2,Alias2} =
    assignColors(Worklists1, Stack, Node_sets1, Color0, 
        	 No_temporaries, SavedAdjList, SavedSpillCosts, IG1, Alias1, All_colors, Target),
  ?print_colors(No_temporaries, Color1),
  ?debug_msg("Nodes:~w\nNodes2:~w\nNo_temporaries:~w\n",[Node_sets,Node_sets2,No_temporaries]),

  ?debug_msg("Build mapping _N ~w\n",[Node_sets2]),
  Coloring = build_namelist(Node_sets2,SpillIndex,Alias2,Color1),
  ?debug_msg("Coloring ~p\n",[Coloring]),
  SortedColoring = { sort_stack(element(1, Coloring)), element(2, Coloring)},
  ?debug_msg("SortedColoring ~p\n",[SortedColoring]),
  %%Coloring.
  ?debug_msg("----------------------Assign colors _O\n",[]),
  {Color1_O,Node_sets2_O} =
    assignColors_O(hipe_reg_worklists:stack(Worklists0_O), Node_sets1, Color0, 
		 Alias0_O, All_colors, Target),
  ?print_colors(No_temporaries, Color1_O),
  ?debug_msg("Nodes:~w\nNodes2:~w\nNo_temporaries:~w\n",[Node_sets,Node_sets2_O,No_temporaries]),

  ?debug_msg("Build mapping ~w\n",[Node_sets2_O]),
  Coloring_O = build_namelist_O(Node_sets2_O,SpillIndex,Alias0_O,Color1_O),
  ?debug_msg("Coloring_O ~p\n",[Coloring_O]),
  SortedColoring_O = {sort_stack(element(1, Coloring_O)), element(2, Coloring_O)},
  ?debug_msg("SortedColoring_O ~p\n",[SortedColoring_O]),
  sanity_compare(SortedColoring_O, SortedColoring),
  Coloring.
-else.
regalloc(CFG, SpillIndex, SpillLimit, Target, _Options) ->
  ?debug_msg("optimistic ~w\n",[Target]),
  ?debug_msg("CFG: ~p\n",[CFG]),
  %% Build interference graph
  ?debug_msg("Build IG\n",[]),
  IG = hipe_ig:build(CFG, Target),
  ?debug_msg("adjlist: ~p\n",[hipe_ig:adj_list(IG)]),
  ?debug_msg("IG:\n",[]),
  ?print_adjacent(IG),
  ?print_degrees(IG),
  ?print_spill_costs(IG),

  SavedSpillCosts = hipe_ig:spill_costs(IG),
  SavedAdjList = hipe_ig:adj_list(IG),

  ?debug_msg("Init\n",[]),
  No_temporaries = Target:number_of_temporaries(CFG),
  ?debug_msg("Coalescing RA: num_temps = ~p~n", [No_temporaries]),
  Allocatable = Target:allocatable(),
  K = length(Allocatable),
  All_colors = colset_from_list(Allocatable),
  ?debug_msg("K: ~w~nAll_colors: ~p\n",[K, All_colors]), 

  %% Add registers with their own coloring
  ?debug_msg("Moves\n",[]),
  Move_sets = hipe_moves:new(IG),
  ?debug_msg("Move_sets:\n ~p\n",[Move_sets]),
  ?mov_print_memberships(Move_sets),

  ?debug_msg("Build Worklist\n",[]),

  Worklists = hipe_reg_worklists:new(IG, Target, CFG, K, No_temporaries),
  ?debug_msg("New Worklists:\n ~p\n", [Worklists]),
  ?reg_print_memberships(Worklists),

  Alias = initAlias(No_temporaries),
  ?print_alias(Alias),

  {IG0, Moves0, Alias0, Worklists0} = 
    do_coalescing(IG, Worklists, Move_sets, Alias, K, Target),
  ?debug_msg("IG after coalescing:\n",[]),
  ?print_adjacent(IG0),
  ?print_degrees(IG0),
  ?print_spill_costs(IG0),
  ?debug_msg("Move_sets after coalescing:\n ~p\n",[Moves0]),
  ?mov_print_memberships(Moves0),
  ?debug_msg("New Worklists after coalescing:\n ~p\n",
             [Worklists0]),
  ?reg_print_memberships(Worklists0),

  {IG1, Worklists1, _Moves1, Alias1} = 
    do_simplify_or_spill(IG0, Worklists0, Moves0, Alias0, 
                         K, SpillLimit, Target),
  ?debug_msg("IG after simplify_or_spill:\n",[]),
  ?print_adjacent(IG1),
  ?print_degrees(IG1),
  ?print_spill_costs(IG1),
  ?debug_msg("Saved spill costs ~p~n", [SavedSpillCosts]),
  ?debug_msg("New Worklists after simplify_or_spill:\n ~p\n",
             [Worklists1]),
  ?reg_print_memberships(Worklists1),
  ?print_alias(Alias1),

  %% only for testing undoCoalescing and member_coalesced_to
  %test_undoCoalescing(No_temporaries, Alias1, Worklists1),

  %% only for testing fixAdj
  %?debug_msg("adj_lists_before_fixAdj ~n~p~n", [hipe_ig:adj_list(IG1)]),
  %IG2 = test_fixAdj(No_temporaries, SavedAdjList, IG1, Target),
  %?debug_msg("adj_lists__after_fixAdj ~n~p~n", [hipe_ig:adj_list(IG2)]),

  ?debug_msg("Init node sets\n",[]),
  Node_sets = hipe_node_sets:new(),
  %% ?debug_msg("NodeSet: ~w\n NonAlloc ~w\n",[Node_sets,Target:non_alloc(CFG)]),
  ?debug_msg("Default coloring\n",[]),
  {Color0,Node_sets1} = 
    defaultColoring(Target:all_precoloured(),
		    initColor(No_temporaries), Node_sets, Target),
  ?debug_msg("Color0\n",[]),
  ?print_colors(No_temporaries, Color0),

  ?debug_msg("----------------------Assign colors _N\n",[]),

  Stack = hipe_reg_worklists:stack(Worklists1), 
  ?debug_msg("The stack _N ~p~n", [Stack]), 
  %SortedStack = sort_stack(Stack),
  %?debug_msg("The stack _N ~p~n", [SortedStack]), 

  %?debug_msg("Nodes _N ~w~n", [Node_sets1]),

  {Color1,Node_sets2,Alias2} =
    assignColors(Worklists1, Stack, Node_sets1, Color0, 
        	 No_temporaries, SavedAdjList, SavedSpillCosts, IG1, Alias1, All_colors, Target),
  ?print_colors(No_temporaries, Color1),
  ?debug_msg("Nodes:~w\nNodes2:~w\nNo_temporaries:~w\n",[Node_sets,Node_sets2,No_temporaries]),

  ?debug_msg("Build mapping _N ~w\n",[Node_sets2]),
  Coloring = build_namelist(Node_sets2,SpillIndex,Alias2,Color1),
  ?debug_msg("Coloring ~p\n",[Coloring]),
  Coloring.
-endif.

%%----------------------------------------------------------------------
%% Function:    do_coloring
%%
%% Description: Create a coloring. That is, play paintball.
%% Parameters:
%%   IG          --  An interference graph
%%   Worklists   --  Worklists, that is simplify, spill and freeze
%%   Moves       --  Moves sets, that is coalesced, constrained 
%%                   and so on.
%%   Alias       --  Tells if two temporaries can have their value
%%                   in the same register.
%%   K           --  Want to create a K coloring.
%%   SpillLimit  --  Try not to spill nodes that are above the spill limit.
%%
%% Returns:
%%   IG          --  Updated interference graph
%%   Worklists   --  Updated Worklists structure
%%   Moves       --  Updated Moves structure 
%%   Alias       --  Updates Alias structure
%%   
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
do_coloring(IG, Worklists, Moves, Alias, K, SpillLimit, Target) ->
  Simplify = not(hipe_reg_worklists:is_empty_simplify(Worklists)),
  Coalesce = not(hipe_moves:is_empty_worklist(Moves)),
  Freeze   = not(hipe_reg_worklists:is_empty_freeze(Worklists)),
  Spill    = not(hipe_reg_worklists:is_empty_spill(Worklists)),
  if Simplify =:= true ->
      {IG0, Worklists0, Moves0} = 
	simplify_O(hipe_reg_worklists:simplify(Worklists),
		   IG, 
		   Worklists, 
		   Moves, 
		   K),
      do_coloring(IG0, Worklists0, Moves0, Alias, K, SpillLimit, Target);
     Coalesce =:= true ->
      {Moves0, IG0, Worklists0, Alias0} =
	coalesce_O(Moves, IG, Worklists, Alias, K, Target),
      do_coloring(IG0, Worklists0, Moves0, Alias0, K, SpillLimit, Target);
     Freeze =:= true ->
      {Worklists0, Moves0} = 
	freeze(K, Worklists, Moves, IG, Alias),
      do_coloring(IG, Worklists0, Moves0, Alias, K, SpillLimit, Target);
     Spill =:= true ->
      {Worklists0, Moves0} = 
	selectSpill_O(Worklists, Moves, IG, K, Alias, SpillLimit),
      do_coloring(IG, Worklists0, Moves0, Alias, K, SpillLimit, Target);
     true -> % Catchall case
      {IG, Worklists, Moves, Alias}
    end.
-endif.

%%----------------------------------------------------------------------
%% Function:    do_coalescing
%%
%% Description: Try to coalesce everything (find out later if it was
%%              possible).
%% Parameters:
%%   IG          --  An interference graph
%%   Moves       --  Moves sets, that is coalesced, constrained 
%%                   and so on.
%%   Alias       --  Tells if two temporaries can have their value
%%                   in the same register.
%%
%% Returns:
%%   IG          --  Updated interference graph
%%   Moves       --  Updated Moves structure 
%%   Alias       --  Updates Alias structure
%%   
%%----------------------------------------------------------------------

do_coalescing(IG, Worklists, Moves, Alias, K, Target) ->
  case hipe_moves:is_empty_worklist(Moves) of
    true ->
      {IG, Moves, Alias, Worklists};
    _ ->
      {Moves0, IG0, Alias0, Worklists0} =
	coalesce(Moves, IG, Worklists, Alias, K, Target),
      do_coalescing(IG0, Worklists0, Moves0, Alias0, K, Target)
  end.

%%----------------------------------------------------------------------
%% Function:    do_simplify_or_spill
%%
%% Parameters:
%%   IG          --  An interference graph
%%   Worklists   --  Worklists, that is simplify, spill and freeze
%%   Moves       --  Moves sets, that is coalesced, constrained 
%%                   and so on.
%%   Alias       --  Tells if two temporaries can have their value
%%                   in the same register.
%%   K           --  Want to create a K coloring.
%%   SpillLimit  --  Try not to spill nodes that are above the spill limit.
%%
%% Returns:
%%   IG          --  Updated interference graph
%%   Worklists   --  Updated Worklists structure
%%   Moves       --  Updated Moves structure 
%%   Alias       --  Updates Alias structure
%%   
%%----------------------------------------------------------------------

do_simplify_or_spill(IG, Worklists, Moves, Alias, K, SpillLimit, Target) ->
  Simplify = not(hipe_reg_worklists:is_empty_simplify(Worklists)),
  Spill    = not(hipe_reg_worklists:is_empty_spill(Worklists)),
  if Simplify =:= true ->
      {IG0, Worklists0, Moves0} = 
	simplify(hipe_reg_worklists:simplify(Worklists),
		 IG, 
		 Worklists, 
		 Moves, 
		 K),
      do_simplify_or_spill(IG0, Worklists0, Moves0, Alias,
		  K, SpillLimit, Target);
     Spill =:= true ->
      Worklists0 = 
	selectSpill(Worklists, IG, SpillLimit),
      do_simplify_or_spill(IG, Worklists0, Moves, Alias, 
			   K, SpillLimit, Target);
     true -> % Catchall case
      {IG, Worklists, Moves, Alias}
    end.

%%----------------------------------------------------------------------
%% Function:    adjacent
%%
%% Description: Adjacent nodes that's not coalesced, on the stack or
%%               precoloured.
%% Parameters:
%%   Node        --  Node that you want to adjacents of
%%   IG          --  The interference graph
%%
%%   Returns: 
%%     A set with nodes/temporaries that are not coalesced, on the 
%%      stack or precoloured.
%%----------------------------------------------------------------------

adjacent(Node, IG, Worklists) ->
  Adjacent_edges = hipe_ig:node_adj_list(Node, IG),
  hipe_reg_worklists:non_stacked_or_coalesced_nodes(Adjacent_edges, Worklists).

%%----------------------------------------------------------------------
%% Function:    simplify
%%
%% Description: Simplify graph by removing nodes of low degree. This
%%               function simplify all nodes it can at once.
%% Parameters:
%%   [Node|Nodes]  --  The simplify worklist
%%   IG            --  The interference graph
%%   Worklists     --  The worklists data-structure
%%   Moves         --  The moves data-structure
%%   K             --  Produce a K coloring
%%
%%   Returns: 
%%     IG          --  An updated interference graph
%%     Worklists   --  An updated worklists data-structure
%%     Moves       --  An updated moves data-structure
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
simplify_O([], IG, Worklists, Moves, _K) -> 
  {IG, Worklists, Moves};
simplify_O([Node|Nodes], IG, Worklists, Moves, K) ->
  Worklists0 = hipe_reg_worklists:remove_simplify(Node, Worklists),
  ?debug_msg("putting ~w on stack~n",[Node]),
  Adjacent = adjacent(Node, IG, Worklists0),
  Worklists01 = hipe_reg_worklists:push_stack(Node, Adjacent, Worklists0),
  {New_ig, Worklists1, New_moves} =
    decrement_degree_O(Adjacent, IG, Worklists01, Moves, K),
  simplify_O(Nodes, New_ig, Worklists1, New_moves, K).
-endif.

%%----------------------------------------------------------------------
%% Function:    simplify
%%
%% Description: Simplify graph by removing nodes of low degree. This
%%               function simplify all nodes it can at once.
%% Parameters:
%%   [Node|Nodes]  --  The simplify worklist
%%   IG            --  The interference graph
%%   Worklists     --  The worklists data-structure
%%   Moves         --  The moves data-structure
%%   K             --  Produce a K coloring
%%
%%   Returns: 
%%     IG          --  An updated interference graph
%%     Worklists   --  An updated worklists data-structure
%%     Moves       --  An updated moves data-structure
%%----------------------------------------------------------------------

simplify([], IG, Worklists, Moves, _K) -> 
  {IG, Worklists, Moves};
simplify([Node|Nodes], IG, Worklists, Moves, K) ->
  Worklists0 = hipe_reg_worklists:remove_simplify(Node, Worklists),
  ?debug_msg("putting ~w on stack~n",[Node]),
  Adjacent = adjacent(Node, IG, Worklists0),
  Worklists01 = hipe_reg_worklists:push_stack(Node, Adjacent, Worklists0),
  {New_ig, Worklists1} = decrement_degree(Adjacent, IG, Worklists01, K),
  simplify(Nodes, New_ig, Worklists1, Moves, K).

%%----------------------------------------------------------------------
%% Function:    decrement_degree
%%
%% Description: Decrement the degree on a number of nodes/temporaries.
%% Parameters:
%%   [Node|Nodes]  --  Decrement degree on these nodes
%%   IG            --  The interference graph
%%   Worklists     --  The Worklists data structure
%%   Moves         --  The Moves data structure.
%%   K             --  We want to create a coloring with K colors
%%
%%   Returns: 
%%     IG          --  An updated interference graph (the degrees)
%%     Worklists   --  Updated Worklists. Changed if one degree goes
%%                     down to K.
%%     Moves       --  Updated Moves. Changed if a move related temporary
%%                     gets degree K.
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
decrement_degree_O([], IG, Worklists, Moves, _K) -> 
  {IG, Worklists, Moves};
decrement_degree_O([Node|Nodes], IG, Worklists, Moves, K) ->
  PrevDegree = hipe_ig:get_node_degree(Node, IG),
  IG0 = hipe_ig:dec_node_degree(Node, IG),
  case PrevDegree =:= K of
    true ->
      AdjList = hipe_ig:node_adj_list(Node, IG0),
      %% OK since Node (a) is still in IG, and (b) cannot be adjacent to itself.
      Moves00 = enable_moves_active_to_worklist(hipe_moves:node_movelist(Node, Moves),
						Moves),
      Moves0 = enable_moves(AdjList, Worklists, Moves00),
      Worklists0 = hipe_reg_worklists:remove_spill(Node, Worklists),
      case hipe_moves:move_related(Node, Moves0) of
	true ->
	  Worklists1 = hipe_reg_worklists:add_freeze(Node, Worklists0),
	  decrement_degree_O(Nodes, IG0, Worklists1, Moves0, K);
	_ ->
	  Worklists1 = hipe_reg_worklists:add_simplify(Node, Worklists0),
	  decrement_degree_O(Nodes, IG0, Worklists1, Moves0, K)
      end;
     _ ->
      decrement_degree_O(Nodes, IG0, Worklists, Moves, K)
  end.
-endif.

%%----------------------------------------------------------------------
%% Function:    decrement_degree
%%
%% Description: Decrement the degree on a number of nodes/temporaries.
%% Parameters:
%%   [Node|Nodes]  --  Decrement degree on these nodes
%%   IG            --  The interference graph
%%   Worklists     --  The Worklists data structure
%%   Moves         --  The Moves data structure.
%%   K             --  We want to create a coloring with K colors
%%
%%   Returns: 
%%     IG          --  An updated interference graph (the degrees)
%%     Worklists   --  Updated Worklists. Changed if one degree goes
%%                     down to K.
%%     Moves       --  Updated Moves. Changed if a move related temporary
%%                     gets degree K.
%%----------------------------------------------------------------------

decrement_degree([], IG, Worklists, _K) -> 
  {IG, Worklists};
decrement_degree([Node|Nodes], IG, Worklists, K) ->
  PrevDegree = hipe_ig:get_node_degree(Node, IG),
  IG0 = hipe_ig:dec_node_degree(Node, IG),
  case PrevDegree =:= K of
    true ->
      Worklists0 = hipe_reg_worklists:remove_spill(Node, Worklists),
      Worklists1 = hipe_reg_worklists:add_simplify(Node, Worklists0),
      decrement_degree(Nodes, IG0, Worklists1, K);
     _ ->
      decrement_degree(Nodes, IG0, Worklists, K)
  end.
	    
%%----------------------------------------------------------------------
%% Function:    enable_moves
%%
%% Description: Make (move-related) nodes that are not yet considered for
%%               coalescing, ready for possible coalescing.
%%               
%% Parameters:
%%   [Node|Nodes]   --  A list of move nodes
%%   Moves          --  The moves data-structure
%%
%%   Returns: 
%%     An updated moves data-structure
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
enable_moves([], _Worklists, Moves) -> Moves;
enable_moves([Node|Nodes], Worklists, Moves) ->
  case hipe_reg_worklists:member_stack_or_coalesced(Node, Worklists) of
    true -> enable_moves(Nodes, Worklists, Moves);
    _ ->
      %% moveList[n] suffices since we're checking for activeMoves membership
      Node_moves = hipe_moves:node_movelist(Node, Moves),
      New_moves = enable_moves_active_to_worklist(Node_moves, Moves),
      enable_moves(Nodes, Worklists, New_moves)
  end.
-endif.

%%----------------------------------------------------------------------
%% Function:    enable_moves_active_to_worklist
%%
%% Description: Make (move-related) nodes that are not yeat considered for
%%               coalescing, ready for possible coalescing.
%%               
%% Parameters:
%%   [Node|Nodes]   --  A list of move nodes
%%   Moves          --  The moves data-structure
%%
%%   Returns: 
%%     An updated moves data-structure
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
enable_moves_active_to_worklist([], Moves) -> Moves;
enable_moves_active_to_worklist([Node|Nodes], Moves) ->
  case hipe_moves:member_active(Node, Moves) of
    true ->
      New_moves = 
	hipe_moves:add_worklist(Node, hipe_moves:remove_active(Node, Moves)),
      enable_moves_active_to_worklist(Nodes, New_moves);
    _ ->
      enable_moves_active_to_worklist(Nodes, Moves)
  end.
-endif.

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
sanity_compare(Coloring, Coloring_N) ->
  case compare_sanity(Coloring, Coloring_N) of
    false ->
      ?debug_msg("mismatch for coloring: ~n~p~n~p", [Coloring, Coloring_N]);
    _ -> true
  end.
compare_sanity({[], _C}, {[], _C_N}) ->
  ?debug_msg("Sanity - OK!~n", []),
  true;
compare_sanity({_Coloring_list, _C}, {[], _C_N}) ->
  ?debug_msg("Sanity - unequal numbers~n", []),
  false;
compare_sanity({[], _C}, {_Coloring_list_N, _C_N}) ->
  ?debug_msg("Sanity - unequal numbers~n", []),
  false;
compare_sanity({[Color|Coloring_list], C}, {[Color_N|Coloring_list_N], C_N}) ->
  case element(1, Color) =:= element(1, Color_N) of
    false ->
      ?debug_msg("Sanity - unequal measure~n", []),
      false;
    _ -> 
      case element(2, Color) =:= element(2, Color_N) of
        false ->  
	  ?debug_msg("Sanity - unequal color~n", []),
	  false;
	_ ->
	  case C =:= C_N of
	    false ->
	      ?debug_msg("Sanity - unequal last element~n", []),
	      false;
	    _ ->
	      compare_sanity({Coloring_list, C}, {Coloring_list_N, C_N})
	  end
      end
  end.
-endif.
  
  
%% Build the namelists, these functions are fast hacks, they use knowledge 
%% about data representation that they shouldn't know, bad abstraction.

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
build_namelist_O(NodeSets,Index,Alias,Color) ->
  ?debug_msg("NodeSets ~w~n", [NodeSets]),
  ?debug_msg("Building mapping\n",[]),
  ?debug_msg("Vector to list\n",[]),
  AliasList = 
    build_alias_list(aliasToList(Alias),
		     0, %% The first temporary has index 0
		     []), %% Accumulator
  ?debug_msg("Alias list:~p\n",[AliasList]),
  ?debug_msg("Coalesced\n",[]),
  NL1 = build_coalescedlist(AliasList,Color,Alias,[]),
  ?debug_msg("Coalesced list:~p\n",[NL1]),
  ?debug_msg("Regs\n",[]),
  NL2 = build_reglist_O(hipe_node_sets:colored(NodeSets),Color,NL1),
  ?debug_msg("Regs list:~p\n",[NL2]),
  ?debug_msg("Spills\n",[]),
  build_spillist(hipe_node_sets:spilled(NodeSets),Index,NL2).
-endif.

build_namelist(NodeSets,Index,Alias,Color) ->
  ?debug_msg("NodeSets _N ~w~n", [NodeSets]),
  ?debug_msg("Building mapping _N\n",[]),
  ?debug_msg("Vector to list _N\n",[]),
  AliasList = 
    build_alias_list(aliasToList(Alias),
		     0, %% The first temporary has index 0
		     []), %% Accumulator
  ?debug_msg("Alias list _N:~p\n",[AliasList]),
  ?debug_msg("Coalesced\n",[]),
  NL1 = build_coalescedlist(AliasList,Color,Alias,[]),
  ?debug_msg("Coalesced list:~p\n",[NL1]),
  ?debug_msg("Regs _N\n",[]),
  ColoredNodes = hipe_node_sets:colored(NodeSets),
  ?debug_msg("ColoredNodes ~p~n", [ColoredNodes]),
  NL2 = build_reglist_N(ColoredNodes,Color,NL1,NL1),
  ?debug_msg("Regs list _N:~p\n",[NL2]),
  ?debug_msg("Spills _N\n",[]),
  build_spillist(hipe_node_sets:spilled(NodeSets),Index,NL2).

build_spillist([],Index,List) ->
  {List,Index};
build_spillist([Node|Nodes],Index,List) ->
  ?debug_msg("[~p]: Spill ~p to ~p\n", [?MODULE,Node,Index]),
  build_spillist(Nodes,Index+1,[{Node,{spill,Index}}|List]).

build_coalescedlist([],_Color,_Alias,List) ->
  List;
build_coalescedlist([Node|Ns],Color,Alias,List) when is_integer(Node) ->
  ?debug_msg("Alias of ~p is ~p~n",[Node,getAlias(Node,Alias)]),
  AC = getColor(getAlias(Node,Alias),Color),
  build_coalescedlist(Ns,Color,Alias,[{Node,{reg,AC}}|List]).

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
build_reglist_O([],_Color,List) -> 
  List;
build_reglist_O([Node|Ns],Color,List) ->
  build_reglist_O(Ns,Color,[{Node,{reg,getColor(Node,Color)}}|List]).
-endif.

build_reglist_N([],_Color,List,_OrgList) -> 
  List;
build_reglist_N([Node|Ns],Color,List,OrgList) ->
  %% XXX this could be done more efficiently if both lists were sorted
  case is_already_in_list(Node, OrgList) of
    true -> build_reglist_N(Ns, Color, List, OrgList);
    _ -> build_reglist_N(Ns,Color,[{Node,{reg,getColor(Node,Color)}}|List], OrgList)
  end.

is_already_in_list(_Node, []) ->
  false;
is_already_in_list(Node, [L|List]) ->
  ?debug_msg("---test--- Node ~w  element ~w~n", [Node, element(1, L)]),
  case Node =:= element(1, L) of
    true -> true;
    _ -> is_already_in_list(Node, List)
  end.

build_alias_list([], _I, List) ->
  List;
build_alias_list([Alias|Aliases], I, List) when is_integer(Alias) ->
  build_alias_list(Aliases, I+1, [I|List]);
build_alias_list([_Alias|Aliases], I, List) ->
  build_alias_list(Aliases, I+1, List).

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
sort_stack([]) -> [];
sort_stack([Pivot|Rest]) ->
  {Smaller, Bigger} = sort_stack_split(Pivot, Rest),
  lists:append(sort_stack(Smaller), [Pivot|sort_stack(Bigger)]).

sort_stack_split(Pivot, L) ->
  sort_stack_split(Pivot, L, [], []).

sort_stack_split(_Pivot, [], Smaller, Bigger) -> 
  {Smaller, Bigger};
sort_stack_split(Pivot, [H|T], Smaller, Bigger) when element(1, H) > element(1, Pivot) ->
  sort_stack_split(Pivot, T, [H|Smaller], Bigger);
sort_stack_split(Pivot, [H|T], Smaller, Bigger) ->
  sort_stack_split(Pivot, T, Smaller, [H|Bigger]).
-endif.

%sort([]) -> [];
%sort([Pivot|Rest]) ->
%  {Smaller, Bigger} = sort_split(Pivot, Rest),
%  lists:append(sort(Smaller), [Pivot|sort(Bigger)]).
%
%sort_split(Pivot, L) ->
%  sort_split(Pivot, L, [], []).
%
%sort_split(_Pivot, [], Smaller, Bigger) -> {Smaller, Bigger};
%sort_split(Pivot, [H|T], Smaller, Bigger) when H > Pivot ->
%  sort_split(Pivot, T, [H|Smaller], Bigger);
%sort_split(Pivot, [H|T], Smaller, Bigger) ->
%  sort_split(Pivot, T, Smaller, [H|Bigger]).

%%----------------------------------------------------------------------
%% Function:    assignColors
%%
%% Description: Tries to assign colors to nodes in a stack.
%% Parameters:
%%   Worklists       --  The Worklists data structure.
%%   Stack           --  The SelectStack built by the Select function, 
%%                       this stack contains tuples in the form {Node,Edges} 
%%                       where Node is the Node number and Edges is an ordset 
%%                       containing the numbers of all the adjacent nodes.
%%   NodeSets        --  This is a record containing all the different node 
%%                       sets that are used in the register allocator.
%%   Color           --  A mapping from nodes to their respective color.
%%   No_temporaries  --  Number of temporaries.
%%   SavedAdjList    --  Saved adjacency list (from before coalescing).
%%   SavedSpillCosts --  Saved spill costs (from before coalescing).
%%   IG              --  The interference graph.
%%   Alias           --  This is a mapping from nodes to nodes. If a node has 
%%                       been coalesced, this mapping shows the alias for that 
%%                       node.
%%   AllColors       --  This is an ordset containing all the available colors
%%   Target          --  The module containing the target-specific functions.
%%
%% Returns:
%%   Color          --  A mapping from nodes to their respective color.
%%   NodeSets       --  The updated node sets.
%%   Alias          --  The updated aliases.
%%----------------------------------------------------------------------

assignColors(Worklists, Stack, NodeSets, Color, No_Temporaries,
	     SavedAdjList, SavedSpillCosts, IG, Alias, AllColors, Target) ->
  case Stack of
    [] ->
      {Color,NodeSets,Alias};
    [{Node,Edges}|Stack1] ->
      ?debug_msg("Coloring Node: ~p~n",[Node]),
      ?IF_DEBUG(lists:foreach(fun (_E) ->
				  ?msg("  Edge ~w-><~w>->~w~n",
				       begin A = getAlias(_E,Alias),
					     [_E,A,getColor(A,Color)]
				       end)
			      end, Edges),
		[]),
      %% When debugging, check that Node isn't precoloured.
      OkColors = findOkColors(Edges, AllColors, Color, Alias),
      case colset_is_empty(OkColors) of
	true -> % Spill case
	  case hipe_reg_worklists:member_coalesced_to(Node, Worklists) of
	    true ->
	      ?debug_msg("Alias case. Undoing coalescing.~n", []),
              {Alias1, IG1, NodeSets1, Color1, Stack2} = tryPrimitiveNodes(Node, Stack1, NodeSets, AllColors, Color, No_Temporaries, SavedAdjList, SavedSpillCosts, IG, Alias, Target),
              %{Alias1, IG1, NodeSets1, Color1, Stack2} = {Alias, IG, NodeSets, Color, Stack1},
	      assignColors(Worklists, Stack2, NodeSets1, Color1, No_Temporaries, SavedAdjList, SavedSpillCosts, IG1, Alias1, AllColors, Target);
	    false ->
	      ?debug_msg("Spill case. Spilling node.~n", []),
	      NodeSets1 = hipe_node_sets:add_spilled(Node, NodeSets),
	      assignColors(Worklists, Stack1, NodeSets1, Color, No_Temporaries, SavedAdjList, SavedSpillCosts, IG, Alias, AllColors, Target)
	  end;
	false -> % Color case
	  Col = colset_smallest(OkColors),
	  NodeSets1 = hipe_node_sets:add_colored(Node, NodeSets),
	  Color1 = setColor(Node, Target:physical_name(Col), Color),
	  ?debug_msg("Color case. Assigning color ~p to node.~n", [Col]),
	  assignColors(Worklists, Stack1, NodeSets1, Color1, No_Temporaries, SavedAdjList, SavedSpillCosts, IG, Alias, AllColors, Target)
      end
  end.

%%----------------------------------------------------------------------
%% Function:    tryPrimitiveNodes
%%
%% Description: Undoes coalescing of a non-colorable coalesced node and tries
%%              to assign colors to its primitives, such that the cheapest
%%              potential spill cost is achieved.
%% Parameters:
%%   Node            --  The representative node to undo coalescing for.
%%   Stack           --  The SelectStack built by the Select function, 
%%                       this stack contains tuples in the form {Node,Edges} 
%%                       where Node is the Node number and Edges is an ordset 
%%                       containing the numbers of all the adjacent nodes.
%%   NodeSets        --  This is a record containing all the different node 
%%                       sets that are used in the register allocator.
%%   AllColors       --  This is an ordset containing all the available colors.
%%   No_temporaries  --  Number of temporaries.
%%   SavedAdjList    --  Saved adjacency list (from before coalescing).
%%   SavedSpillCosts --  Saved spill costs (from before coalescing).
%%   IG              --  The interference graph.
%%   Alias           --  This is a mapping from nodes to nodes. If a node has 
%%                       been coalesced, this mapping shows the alias for that 
%%                       node.
%%   Target          --  The module containing the target-specific functions.
%%
%% Returns:
%%   Alias           --  The restored aliases after the uncoalescing.
%%   IG              --  An updated interference graph after the uncoalescing.
%%   NodeSets        --  The updated node sets.
%%   Color           --  A mapping from nodes to their respective color.
%%   Stack           --  The updated SelectStack with non-colored primitives
%%                       placed at the bottom.
%%----------------------------------------------------------------------

tryPrimitiveNodes(Node, Stack, NodeSets, AllColors, Color, No_temporaries, SavedAdjList, SavedSpillCosts, IG, Alias, Target) ->
  ?debug_msg("Undoing coalescing of node ~p.~n", [Node]),
  {PrimitiveNodes, Alias1, IG1} = undoCoalescing(Node, No_temporaries, Alias, SavedAdjList, IG, Target),
  ?debug_msg("Spilling non-colorable primitives.~n", []),
  {ColorableNodes, NodeSets1} = spillNonColorablePrimitives([], PrimitiveNodes, NodeSets, AllColors, Color, SavedAdjList, Alias1),
  ?debug_msg("Generating splits of colorable nodes.~n", []),
  Splits = splits(ColorableNodes, SavedSpillCosts),
  {NodeSets2, Color1, Stack1} = processSplits(Splits, AllColors, IG1, Color, NodeSets1, Alias1, Target, Stack),
  {Alias1, IG1, NodeSets2, Color1, Stack1}.

%% Spill all non-colorable primitives and return the remaining set of nodes.

spillNonColorablePrimitives(ColorableNodes, [], NodeSets, _AllColors, _Color, _SavedAdjList, _Alias) ->
  {ColorableNodes, NodeSets};
spillNonColorablePrimitives(ColorableNodes, [Primitive|Primitives], NodeSets, AllColors, Color, SavedAdjList, Alias) ->
  OkColors = findOkColors(hipe_adj_list:edges(Primitive, SavedAdjList), AllColors, Color, Alias),
  case colset_is_empty(OkColors) of
    true -> % Spill case
      ?debug_msg("  Spilling primitive node ~p.~n", [Primitive]),
      NodeSets1 = hipe_node_sets:add_spilled(Primitive, NodeSets),
      spillNonColorablePrimitives(ColorableNodes, Primitives, NodeSets1, AllColors, Color, SavedAdjList, Alias);
    false -> % Colorable case
      ?debug_msg("  Primitive node ~p is colorable.~n", [Primitive]),
      spillNonColorablePrimitives([Primitive|ColorableNodes], Primitives, NodeSets, AllColors, Color, SavedAdjList, Alias)
  end.

%% Generate all splits of colorable primitives, sorted in spill cost order.

splits([], _SavedSpillCosts) ->
  [{[], [], 0}];
splits([L|Ls], SavedSpillCosts) ->
  Spl = splits(Ls, SavedSpillCosts),
  SpillCost = hipe_spillcost:spill_cost(L, SavedSpillCosts),
  Spl1 = [splits_1(S, L) || S <- Spl],
  Spl2 = [splits_2(S, L, SpillCost) || S <- Spl],
  spillCostOrderedMerge(Spl1, Spl2, []).

splits_1({Cols, NonCols, OldSpillCost}, L) ->
    {[L|Cols], NonCols, OldSpillCost}.

splits_2({Cols, NonCols, OldSpillCost}, L, SpillCost) ->
    {Cols, [L|NonCols], OldSpillCost + SpillCost}.
 
%% Merge two ordered sub-splits into one.

spillCostOrderedMerge(Spl1, [], Spl) ->
  lists:reverse(Spl) ++ Spl1;
spillCostOrderedMerge([], Spl2, Spl) ->
  lists:reverse(Spl) ++ Spl2;
spillCostOrderedMerge(Spl1, Spl2, Spl) ->
  {_, _, SpillCost1} = hd(Spl1),
  {_, _, SpillCost2} = hd(Spl2),
  case SpillCost1 =< SpillCost2 of
    true ->
      spillCostOrderedMerge(tl(Spl1), Spl2, [hd(Spl1)|Spl]);
    false ->
      spillCostOrderedMerge(Spl1, tl(Spl2), [hd(Spl2)|Spl])
  end.

%% Process splits, finding the one with the smallest spill cost that
%% can be assigned one color.

processSplits([], _AllColors, _IG, Color, NodeSets, _Alias, _Target, Stack) ->
  {NodeSets, Color, Stack};
processSplits([{Cols, NonCols, _SpillCost}|Splits], AllColors, IG, Color, NodeSets, Alias, Target, Stack) ->
  OkColors = findCommonColors(Cols, IG, Color, Alias, AllColors),
  case colset_is_empty(OkColors) of
    false -> % This split can be colored with one color - use it
      ?debug_msg("Found a colorable split.~n", []),
      Col = colset_smallest(OkColors),
      {NodeSets1, Color1} = colorSplit(Cols, Col, NodeSets, Color, Target),
      Stack1 = enqueueSplit(NonCols, IG, Stack),
      {NodeSets1, Color1, Stack1};
    true -> % This split cannot be colored with one color - try another
      ?debug_msg("Unable to color split.~n", []),
      processSplits(Splits, AllColors, IG, Color, NodeSets, Alias, Target, Stack)
  end.

%% Find the set of colors that can be assigned to one split.

findCommonColors([], _IG, _Color, _Alias, OkColors) ->
  OkColors;
findCommonColors([Primitive|Primitives], IG, Color, Alias, OkColors) ->
  OkColors1 = findOkColors(hipe_ig:node_adj_list(Primitive, IG), OkColors, Color, Alias),
  findCommonColors(Primitives, IG, Color, Alias, OkColors1).

%% Color nodes in a split.

colorSplit([], _Col, NodeSets, Color, _Target) ->
  {NodeSets, Color};
colorSplit([Node|Nodes], Col, NodeSets, Color, Target) ->
  ?debug_msg("  Coloring node ~p with color ~p.~n", [Node, Col]),
  NodeSets1 = hipe_node_sets:add_colored(Node, NodeSets),
  Color1 = setColor(Node, Target:physical_name(Col), Color),
  colorSplit(Nodes, Col, NodeSets1, Color1, Target).

%% Place non-colorable nodes in a split at the bottom of the SelectStack.

enqueueSplit([], _IG, Stack) ->
  Stack;
enqueueSplit([Node|Nodes], IG, Stack) ->
  ?debug_msg("  Placing node ~p at the bottom of the stack.~n", [Node]),
  Edges = hipe_ig:node_adj_list(Node, IG),
  Stack1 = Stack ++ [{Node, Edges}],
  enqueueSplit(Nodes, IG, Stack1).

%%----------------------------------------------------------------------
%% Function:    assignColors
%%
%% Description: Tries to assign colors to nodes in a stack.
%% Parameters:
%%   Stack          --  The SelectStack built by the Select function, 
%%                      this stack contains tuples in the form {Node,Edges} 
%%                      where  Node is the Node number and Edges is an ordset 
%%                      containing the numbers of all the adjacent nodes.
%%   NodeSets       --  This is a record containing all the different node 
%%                      sets that are used in the register allocator.
%%   Alias          --  This is a mapping from nodes to nodes, if a node has 
%%                      been coalesced this mapping shows the alias for that 
%%                      node.
%%   AllColors      --  This is an ordset containing all the available colors
%%
%%   Target         --  The module containing the target-specific functions.
%%
%% Returns:
%%   Color          --  A mapping from nodes to their respective color.
%%   NodeSets       --  The updated node sets.
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
assignColors_O(Stack,NodeSets,Color,Alias,AllColors,Target) ->
  case Stack of
    [] ->
      {Color,NodeSets};
    [{Node,Edges}|Stack1] ->
      ?debug_msg("Coloring Node: ~p~n",[Node]),
      ?IF_DEBUG(lists:foreach(fun (_E) ->
				  ?msg("  Edge ~w-><~w>->~w~n",
				       begin A = getAlias(_E,Alias),
					     [_E,A,getColor(A,Color)]
				       end)
			      end, Edges),
		[]),
      %% When debugging, check that Node isn't precoloured.
      OkColors = findOkColors(Edges, AllColors, Color, Alias),
      case colset_is_empty(OkColors) of
	true -> % Spill case
	  NodeSets1 = hipe_node_sets:add_spilled(Node, NodeSets),
	  assignColors_O(Stack1, NodeSets1, Color, Alias, AllColors, Target);
	false -> % Colour case
	  Col = colset_smallest(OkColors),
	  NodeSets1 = hipe_node_sets:add_colored(Node, NodeSets),
	  Color1 = setColor(Node, Target:physical_name(Col), Color),
	  assignColors_O(Stack1, NodeSets1, Color1, Alias, AllColors, Target)
      end
  end.
-endif.

%%---------------------------------------------------------------------
%% Function:     defaultColoring
%% 
%% Description: Make the default coloring
%% Parameters:
%%   Regs           -- The list of registers to be default colored
%%   Color          -- The color mapping that shall be changed
%%   NodeSets       -- The node sets that shall be updated
%%   Target         -- The module containing the target-specific functions.
%%
%% Returns:
%%   NewColor       -- The updated color mapping
%%   NewNodeSets    -- The updated node sets
%%---------------------------------------------------------------------

defaultColoring([], Color, NodeSets, _Target) ->
  {Color,NodeSets};
defaultColoring([Reg|Regs], Color, NodeSets, Target) ->
  Color1 = setColor(Reg,Target:physical_name(Reg), Color),
  NodeSets1 = hipe_node_sets:add_colored(Reg, NodeSets),
  defaultColoring(Regs, Color1, NodeSets1, Target).

%% Find the colors that are OK for a node with certain edges.

findOkColors(Edges, AllColors, Color, Alias) ->
  find(Edges, AllColors, Color, Alias).

%% Find all the colors of the nodes in the list [Node|Nodes] and remove them 
%% from the set OkColors, when the list is empty, return OkColors.

find([], OkColors, _Color, _Alias) ->
  OkColors;
find([Node0|Nodes], OkColors, Color, Alias) ->
  Node = getAlias(Node0, Alias),
  case getColor(Node, Color) of
    [] ->
      find(Nodes, OkColors, Color, Alias);
    Col ->
      OkColors1 = colset_del_element(Col, OkColors),
      find(Nodes, OkColors1, Color, Alias)
  end.

%%%
%%% ColSet -- ADT for the set of available colours while
%%% assigning colours.
%%%
-ifdef(notdef). % old ordsets-based implementation
colset_from_list(Allocatable) ->
  ordsets:from_list(Allocatable).

colset_del_element(Colour, ColSet) ->
  ordsets:del_element(Colour, ColSet).

colset_is_empty(ColSet) ->
  case ColSet of
    [] -> true;
    [_|_] -> false
  end.

colset_smallest([Colour|_]) ->
  Colour.
-endif.

-ifdef(notdef). % new gb_sets-based implementation
colset_from_list(Allocatable) ->
  gb_sets:from_list(Allocatable).

colset_del_element(Colour, ColSet) ->
  %% Must use gb_sets:delete_any/2 since gb_sets:del_element/2
  %% fails if the element isn't present. Bummer.
  gb_sets:delete_any(Colour, ColSet).

colset_is_empty(ColSet) ->
  gb_sets:is_empty(ColSet).

colset_smallest(ColSet) ->
  gb_sets:smallest(ColSet).
-endif.

%%-ifdef(notdef). % new bitmask-based implementation
colset_from_list(Allocatable) ->
  colset_from_list(Allocatable, 0).

colset_from_list([], ColSet) ->
  ColSet;
colset_from_list([Colour|Allocatable], ColSet) ->
  colset_from_list(Allocatable, ColSet bor (1 bsl Colour)).

colset_del_element(Colour, ColSet) ->
  ColSet band bnot(1 bsl Colour).

colset_is_empty(0) -> true;
colset_is_empty(_) -> false.

colset_smallest(ColSet) ->
  bitN_log2(ColSet band -ColSet, 0).

bitN_log2(BitN, ShiftN) ->
  case BitN > 16#ffff of
    true ->
      bitN_log2(BitN bsr 16, ShiftN + 16);
    _ ->
      ShiftN + hweight16(BitN - 1)
  end.

hweight16(W) ->
  Res1 = (   W band 16#5555) + ((   W bsr 1) band 16#5555),
  Res2 = (Res1 band 16#3333) + ((Res1 bsr 2) band 16#3333),
  Res3 = (Res2 band 16#0F0F) + ((Res2 bsr 4) band 16#0F0F),
         (Res3 band 16#00FF) + ((Res3 bsr 8) band 16#00FF).
%%-endif.

%%%
%%% Colour ADT providing a partial mapping from nodes to colours.
%%%

initColor(NrNodes) ->
  {colmap, hipe_bifs:array(NrNodes, [])}.

getColor(Node, {colmap, ColMap}) ->
  hipe_bifs:array_sub(ColMap, Node).

setColor(Node, Color, {colmap, ColMap} = C) ->
  hipe_bifs:array_update(ColMap, Node, Color),  
  C.

-ifdef(DEBUG_PRINTOUTS).
printColors(0, _) ->
  true;
printColors(Node, {colmap, ColMap} = C) ->
  NextNode = Node - 1,
  ?debug_msg("node ~w  color ~w~n", [NextNode, hipe_bifs:array_sub(ColMap, NextNode)]),
  printColors(NextNode, C).
-endif.

%%%
%%% Alias ADT providing a partial mapping from nodes to nodes.
%%%

initAlias(NrNodes) ->
  {alias, hipe_bifs:array(NrNodes, [])}.

%% Get alias for a node. 
%% Note that non-aliased nodes could be represented in
%% two ways, either not aliased or aliased to itself. 
%% Including the latter case prevents looping bugs.
getAlias(Node, {alias, AliasMap} = Alias) ->
  case hipe_bifs:array_sub(AliasMap, Node) of
    [] ->
      Node;
    Node ->
      Node;
    AliasNode ->
      getAlias(AliasNode, Alias)
  end.

-ifdef(DEBUG_PRINTOUTS).
printAlias({alias, AliasMap} = Alias) ->
  ?debug_msg("Aliases:\n",[]),
  printAlias(hipe_bifs:array_length(AliasMap), Alias).

printAlias(0, {alias, _}) ->
  true ;
printAlias(Node, {alias, _AliasMap} = Alias) ->
  ?debug_msg("alias ~p ~p\n", [Node - 1, getAlias(Node - 1, Alias)]),
  printAlias(Node - 1, Alias).
-endif.

setAlias(Node, AliasNode, {alias, AliasMap} = Alias) ->
  hipe_bifs:array_update(AliasMap, Node, AliasNode),
  Alias.

aliasToList({alias, AliasMap}) ->
  aliasToList(AliasMap, hipe_bifs:array_length(AliasMap), []).

aliasToList(AliasMap, I1, Tail) ->
  I0 = I1 - 1,
  case I0 >= 0 of
    true ->
      aliasToList(AliasMap, I0, [hipe_bifs:array_sub(AliasMap, I0)|Tail]);
    _ ->
      Tail
  end.

%%----------------------------------------------------------------------
%% Function:    coalesce
%%
%% Description: Coalesces nodes in worklist
%% Parameters:
%%   Moves       -- Current move information
%%   IG          -- Interference graph
%%   Worklists   -- Current worklists
%%   Alias       -- Current aliases for temporaries
%%   K           -- Number of registers
%%   
%% Returns:
%%   {Moves, IG, Worklists, Alias}
%%         (Updated versions of above structures, after coalescing)
%%----------------------------------------------------------------------

coalesce(Moves, IG, Worklists, Alias, K, Target) ->
  case hipe_moves:worklist_get_and_remove(Moves) of
    {[],Moves0} ->
      %% Moves marked for removal from worklistMoves by FreezeMoves()
      %% are removed by worklist_get_and_remove(). This case is unlikely,
      %% but can occur if only stale moves remain in worklistMoves.
      {Moves0, IG, Alias};
    {Move,Moves0} ->
      {Dest,Source} = hipe_moves:get_move(Move, Moves0),
      ?debug_msg("Testing nodes ~p and ~p for coalescing~n",[Dest,Source]),
      Alias_src = getAlias(Source, Alias),
      Alias_dst = getAlias(Dest, Alias),
      {U,V} = case Target:is_precoloured(Alias_dst) of
		true -> {Alias_dst, Alias_src};
		false -> {Alias_src, Alias_dst}
	      end,
      %% When debugging, check that neither V nor U is on the stack.
      case U =:= V of
        true ->
	  %% drop coalesced move Move
	  {Moves0, IG, Alias, Worklists};
	_ ->
	  case (Target:is_precoloured(V) orelse
		hipe_ig:nodes_are_adjacent(U, V, IG)) of 
	    true ->
	      %% drop constrained move Move
	      {Moves0, IG, Alias, Worklists};
	    false ->
	      case (case Target:is_precoloured(U) of
		      true ->
			AdjV = hipe_ig:node_adj_list(V, IG),
			all_adjacent_ok(AdjV, U, Worklists, IG, K, Target);
		      false ->
			AdjV = hipe_ig:node_adj_list(V, IG),
			AdjU = hipe_ig:node_adj_list(U, IG),
			conservative(AdjU, AdjV, U, Worklists, IG, K)
		    end) of
		true ->
		  %% drop coalesced move Move
		  {IG1, Alias1, Worklists1} = 
		    combine(U, V, IG, Alias, Worklists, K, Target),
		  {Moves0, IG1, Alias1, Worklists1};
		false ->
		  Moves1 = hipe_moves:add_active(Move, Moves0),
		  {Moves1, IG, Alias, Worklists}
	      end
	  end
      end
  end.

%%----------------------------------------------------------------------
%% Function:    coalesce_O
%%
%% Description: Coalesces nodes in worklist
%% Parameters:
%%   Moves       -- Current move information
%%   IG          -- Interference graph
%%   Worklists   -- Current worklists
%%   Alias       -- Current aliases for temporaries
%%   K           -- Number of registers
%%   
%% Returns:
%%   {Moves, IG, Worklists, Alias}
%%         (Updated versions of above structures, after coalescing)
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
coalesce_O(Moves, IG, Worklists, Alias, K, Target) ->
  case hipe_moves:worklist_get_and_remove(Moves) of
    {[],Moves0} ->
      %% Moves marked for removal from worklistMoves by FreezeMoves()
      %% are removed by worklist_get_and_remove(). This case is unlikely,
      %% but can occur if only stale moves remain in worklistMoves.
      {Moves0,IG,Worklists,Alias};
    {Move,Moves0} ->
      {Dest,Source} = hipe_moves:get_move(Move, Moves0),
      ?debug_msg("Testing nodes ~p and ~p for coalescing~n",[Dest,Source]),
      Alias_src = getAlias(Source, Alias),
      Alias_dst = getAlias(Dest, Alias),
      {U,V} = case Target:is_precoloured(Alias_dst) of
		true -> {Alias_dst, Alias_src};
		false -> {Alias_src, Alias_dst}
	      end,
      %% When debugging, check that neither V nor U is on the stack.
      case U =:= V of
         true ->
	  Moves1 = Moves0, % drop coalesced move Move
	  Worklists1 = add_worklist(Worklists, U, K, Moves1, IG, Target),
	  {Moves1, IG, Worklists1, Alias};
	 _ ->
	  case (Target:is_precoloured(V) orelse
		hipe_ig:nodes_are_adjacent(U, V, IG)) of 
	    true ->
	      Moves1 = Moves0, % drop constrained move Move
	      Worklists1 = add_worklist(Worklists, U, K, Moves1, IG, Target),
	      Worklists2 = add_worklist(Worklists1, V, K, Moves1, IG, Target),
	      {Moves1, IG, Worklists2, Alias};
	    false ->
	      case (case Target:is_precoloured(U) of
		      true ->
			AdjV = hipe_ig:node_adj_list(V, IG),
			all_adjacent_ok(AdjV, U, Worklists, IG, K, Target);
		      false ->
			AdjV = hipe_ig:node_adj_list(V, IG),
			AdjU = hipe_ig:node_adj_list(U, IG),
			conservative(AdjU, AdjV, U, Worklists, IG, K)
		    end) of
		true ->
		  Moves1 = Moves0, % drop coalesced move Move
		  {IG1,Worklists1,Moves2,Alias1} =
		    combine_O(U, V, IG, Worklists, Moves1, Alias, K, Target),
		  Worklists2 = add_worklist(Worklists1, U, K, Moves2, IG1, Target),
		  {Moves2, IG1, Worklists2, Alias1};
		false ->
		  Moves1 = hipe_moves:add_active(Move, Moves0),
		  {Moves1, IG, Worklists, Alias}
	      end
	  end
      end
  end.
-endif.

%%----------------------------------------------------------------------
%% Function:    add_worklist
%%
%% Description: Builds new worklists where U is transferred from freeze
%%              to simplify, if possible
%%
%% Parameters:
%%   Worklists     -- Current worklists
%%   U             -- Node to operate on
%%   K             -- Number of registers
%%   Moves         -- Current move information
%%   IG            -- Interference graph
%%   Target        -- The containing the target-specific functions
%%   
%% Returns:
%%   Worklists (updated)
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
add_worklist(Worklists, U, K, Moves, IG, Target) ->
  case (not(Target:is_precoloured(U))
	andalso not(hipe_moves:move_related(U, Moves))
	andalso (hipe_ig:is_trivially_colourable(U, K, IG))) of
    true ->
      hipe_reg_worklists:transfer_freeze_simplify(U, Worklists);
    false ->
      Worklists
  end.
-endif.

%%----------------------------------------------------------------------
%% Function:    combine
%%
%% Description: Combines two nodes into one (used when coalescing)
%%
%% Parameters:
%%   U          -- First node to operate on
%%   V          -- Second node to operate on
%%   IG         -- Interference graph
%%   Worklists  -- Current worklists
%%   Moves      -- Current move information
%%   Alias      -- Current aliases for temporaries
%%   K          -- Number of registers
%%
%% Returns:
%%   {IG, Worklists, Moves, Alias} (updated)
%%----------------------------------------------------------------------
       
-ifdef(COMPARE_ITERATED_OPTIMISTIC).
combine_O(U, V, IG, Worklists, Moves, Alias, K, Target) ->
  Worklists1 = case hipe_reg_worklists:member_freeze(V, Worklists) of
		 true -> hipe_reg_worklists:remove_freeze(V, Worklists);
		 false -> hipe_reg_worklists:remove_spill(V, Worklists)
	       end,
  Worklists11 = hipe_reg_worklists:add_coalesced(V, Worklists1),
  
  ?debug_msg("Coalescing ~p and ~p to ~p~n",[V,U,U]),
  
  Alias1 = setAlias(V, U, Alias),
  
  %% Typo in published algorithm: s/nodeMoves/moveList/g to fix.
  %% XXX: moveList[u] \union moveList[v] OR NodeMoves(u) \union NodeMoves(v) ???
  %% XXX: NodeMoves() is correct, but unnecessarily strict. The ordsets:union
  %% constrains NodeMoves() to return an ordset.
  Moves1 = hipe_moves:update_movelist(U,
				      ordsets:union(hipe_moves:node_moves(U, Moves),
						    hipe_moves:node_moves(V, Moves)),
				      Moves),
  %% Missing in published algorithm. From Tiger book Errata.
  Moves2 = enable_moves_active_to_worklist(hipe_moves:node_movelist(V, Moves1), Moves1),
  AdjV = hipe_ig:node_adj_list(V, IG),
  
  {IG1, Worklists2, Moves3} =
    combine_edges_O(AdjV, U, IG, Worklists11, Moves2, K, Target),

  New_worklists = case (not(hipe_ig:is_trivially_colourable(U, K, IG1))
			andalso hipe_reg_worklists:member_freeze(U, Worklists2)) of
		    true -> hipe_reg_worklists:transfer_freeze_spill(U, Worklists2);
		    false -> Worklists2
		  end,
  {IG1, New_worklists, Moves3, Alias1}.
-endif.

%%----------------------------------------------------------------------
%% Function:    combine
%%
%% Description: Combines two nodes into one (used when coalescing)
%%
%% Parameters:
%%   U          -- First node to operate on
%%   V          -- Second node to operate on
%%   IG         -- Interference graph
%%   Worklists  -- Current worklists
%%   Moves      -- Current move information
%%   Alias      -- Current aliases for temporaries
%%   K          -- Number of registers
%%
%% Returns:
%%   {IG, Worklists, Moves, Alias} (updated)
%%----------------------------------------------------------------------
       
combine(U, V, IG, Alias, Worklists, K, Target) ->
  ?debug_msg("N_Coalescing ~p and ~p to ~p~n",[V,U,U]),
  Worklists1 = hipe_reg_worklists:add_coalesced(V, U, Worklists),
  Alias1 = setAlias(V, U, Alias),
  AdjV = hipe_ig:node_adj_list(V, IG),
  IG1 = combine_edges(AdjV, U, IG, Worklists1, K, Target),
  {IG1, Alias1, Worklists1}.

%%----------------------------------------------------------------------
%% Function:    combine_edges
%%
%% Description: For each node in a list, make an edge between that node
%%              and node U, and decrement its degree by 1
%%              (Used when two nodes are coalesced, to connect all nodes
%%              adjacent to one node to the other node)
%%
%% Parameters:
%%   [T|Ts]      -- List of nodes to make edges to
%%   U           -- Node to make edges from
%%   IG          -- Interference graph
%%   Worklists   -- Current worklists
%%   Moves       -- Current move information
%%   K           -- Number of registers
%%
%% Returns:
%%   {IG, Worklists, Moves} (updated)
%%----------------------------------------------------------------------

combine_edges([], _U, IG, _Worklists, _K, _Target) ->
  IG;
combine_edges([T|Ts], U, IG, Worklists, K, Target) ->
  case hipe_reg_worklists:member_stack_or_coalesced(T, Worklists) of
    true -> combine_edges(Ts, U, IG, Worklists, K, Target);
    _ ->
      IG1 = hipe_ig:add_edge(T, U, IG, Target),
      IG2 = case Target:is_precoloured(T) of
	      true -> IG1;
	      false -> hipe_ig:dec_node_degree(T, IG1)
	    end,
      combine_edges(Ts, U, IG2, Worklists, K, Target)
  end.

%%----------------------------------------------------------------------
%% Function:    combine_edges
%%
%% Description: For each node in a list, make an edge between that node
%%              and node U, and decrement its degree by 1
%%              (Used when two nodes are coalesced, to connect all nodes
%%              adjacent to one node to the other node)
%%
%% Parameters:
%%   [T|Ts]      -- List of nodes to make edges to
%%   U           -- Node to make edges from
%%   IG          -- Interference graph
%%   Worklists   -- Current worklists
%%   Moves       -- Current move information
%%   K           -- Number of registers
%%
%% Returns:
%%   {IG, Worklists, Moves} (updated)
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
combine_edges_O([], _U, IG, Worklists, Moves, _K, _Target) ->
  {IG, Worklists, Moves};
combine_edges_O([T|Ts], U, IG, Worklists, Moves, K, Target) ->
  case hipe_reg_worklists:member_stack_or_coalesced(T, Worklists) of
    true -> combine_edges_O(Ts, U, IG, Worklists, Moves, K, Target);
    _ ->
      %% XXX: The issue below occurs because the T->V edge isn't removed.
      %% This causes adjList[T] to contain stale entries, to possibly grow
      %% (if T isn't already adjacent to U), and degree[T] to possibly
      %% increase (again, if T isn't already adjacent to U).
      %% The decrement_degree() call repairs degree[T] but not adjList[T].
      %% It would be better to physically replace T->V with T->U, and only
      %% decrement_degree(T) if T->U already existed.
      %%
      %% add_edge() may change a low-degree move-related node to be of
      %% significant degree. In this case the node belongs in the spill
      %% worklist, and that's where decrement_degree() expects to find it.
      %% This issue is not covered in the published algorithm.
      OldDegree = hipe_ig:get_node_degree(T, IG),
      IG1 = hipe_ig:add_edge(T, U, IG, Target),
      NewDegree = hipe_ig:get_node_degree(T, IG1),
      Worklists0 =
	if NewDegree =:= K, OldDegree =:= K-1 ->
	    %% ?debug_msg("~w:combine_edges_O(): repairing worklist membership for node ~w\n", [?MODULE,T]),
	    %% The node T must be on the freeze worklist:
	    %% 1. Since we're coalescing, the simplify worklist must have been
	    %%    empty when combine_edges_O() started.
	    %% 2. decrement_degree() may put the node T back on the simplify
	    %%    worklist, but that occurs after the worklists repair step.
	    %% 3. There are no duplicates among the edges.
	    Worklists00 = hipe_reg_worklists:remove_freeze(T, Worklists),
	    hipe_reg_worklists:add_spill(T, Worklists00);
	   true ->
	    Worklists
	end,
      {IG2, Worklists1, Moves1} =
	decrement_degree_O([T], IG1, Worklists0, Moves, K),
      combine_edges_O(Ts, U, IG2, Worklists1, Moves1, K, Target)
  end.
-endif.

%%----------------------------------------------------------------------
%% Function:    undoCoalescing
%%
%% Description: Returns necessary information for a coalesced node
%%
%% Parameters:
%%   N              -- The node to uncoalesce
%%   No_temporaries -- Number of temporaries
%%   Alias          -- The Alias vector before undoing
%%   SavedAdj       -- Saved adjacency list
%%   IG             -- Interference graph
%%   Target         -- The module containing the target-specific functions.
%%   
%% Returns:
%%   list of primitive nodes, that is all nodes that were previously
%%           coalesced to N
%%   updated alias vector
%%   updated Interferece graph
%%----------------------------------------------------------------------
undoCoalescing(N, No_temporaries, Alias, SavedAdj, IG, Target) ->
  Primitives = findPrimitiveNodes(No_temporaries, N, Alias),
  Alias1 = restoreAliases(Primitives, Alias),
  IG1 = fixAdj(N, SavedAdj, IG, Target),
  {Primitives, Alias1, IG1}.

%% Restore aliasinfo for primitive nodes, that is 
%% unalias the node sthat were aliased to the primitive
%% nodes. Note that an unaliased node could be
%% represented in two ways, either not aliased or aliased
%% to itself. See also getAlias
restoreAliases([], Alias) ->
  Alias;
restoreAliases([Primitive|Primitives], Alias) ->
  Alias1 = setAlias(Primitive, Primitive, Alias),
  restoreAliases(Primitives, Alias1).

%% find the primitive nodes to N, that is find all
%% nodes that are aliased to N
findPrimitiveNodes(No_temporaries, N, Alias) ->
  findPrimitiveNodes(No_temporaries, N, Alias, []).

findPrimitiveNodes(0, _N, _Alias, PrimitiveNodes) ->
  PrimitiveNodes;
findPrimitiveNodes(Node, N, Alias, PrimitiveNodes) ->
  NextNode = Node - 1,
  case (getAlias(NextNode, Alias) =:= N) of
    true -> findPrimitiveNodes(NextNode, N, Alias, [NextNode | PrimitiveNodes]);
    _ -> findPrimitiveNodes(NextNode, N, Alias, PrimitiveNodes)
  end.

%test_undoCoalescing(No_temporaries, Alias, Worklists) ->
%  test_undoCoalescing(No_temporaries, No_temporaries, Alias, Worklists).
%
%test_undoCoalescing(0, _No_temporaries, _Alias, _Worklists) ->
%  true;
%test_undoCoalescing(Node, No_temporaries, Alias, Worklists) ->
%  %?debug_msg("++ the adj list: ~p~n", [SavedAdj]),
%  %?debug_msg("Node ~p~n", [Node]),
%  NextNode = Node - 1,
%  Coalesced_to = hipe_reg_worklists:member_coalesced_to(NextNode, Worklists),
%  ?debug_msg("³³-- member coalesced: ~p~n", [Coalesced_to]),
%  {Primitives, Alias1} = undoCoalescing(NextNode, No_temporaries, Alias),
%  ?debug_msg("½½-- primitivenodes ~w\n", [Primitives]),
%  case (Coalesced_to) of
%    true -> printAlias(Alias1);
%    _ -> true
%  end,
%  test_undoCoalescing(NextNode, No_temporaries, Alias, Worklists).

%%----------------------------------------------------------------------
%% Function:    fixAdj
%%
%% Description: Fixes adajency set and adjacency list when undoing coalescing
%%
%% Parameters:
%%   N             -- Node that should be uncoalesced
%%   SavedAdj      -- Saved adjacency list
%%   IG            -- Interference graph
%%   Target        -- The module containing the target-specific functions.
%%   
%% Returns:
%%   updated Interferece graph
%%----------------------------------------------------------------------
fixAdj(N, SavedAdj, IG, Target) ->
  %Saved = hipe_vectors:get(SavedAdj, N),
  Saved = hipe_adj_list:edges(N, SavedAdj),
  ?debug_msg("§§--adj to ~p: ~p~n", [N, Saved]),
  Adj = hipe_ig:node_adj_list(N, IG),
  ?debug_msg("««--adj to ~p: ~p~n", [N, Adj]),
  New = findNew(Adj, Saved),
  ?debug_msg("++--new adj to ~p: ~p~n", [N, New]),
  removeAdj(New, N, IG, Target),
  %% XXX the following lines seems to make double nodes in
  %% some adj_lists, which is a bug, apart from that they
  %% don't seem to make any difference at all (even though
  %% they are in the pseudocode of "optimistic coalescing")
  %% addedge for all in the restored adj_list
  %%RestoredAdj = hipe_ig:node_adj_list(N, IG),
  %%?debug_msg("adj_lists_before_restore_o ~n~p~n", [hipe_ig:adj_list(IG)]),
  %%restoreAdj(RestoredAdj, N, IG, Alias, Target).
  IG.

removeAdj([], _N, _IG, _Target) ->
  true;
removeAdj([V| New], N, IG, Target) ->
  hipe_ig:remove_edge(V, N, IG, Target),
  removeAdj(New, N, IG, Target).

%%restoreAdj([], _N, IG, _Alias, _Target) ->
%%  %%?debug_msg("adj_lists__after_restore_o ~n~p~n", [hipe_ig:adj_list(IG)]),
%%  IG;
%%restoreAdj([V| AdjToN], N, IG, Alias, Target) ->
%%  AliasToV = getAlias(V, Alias),
%%  IG1 = hipe_ig:add_edge(N, AliasToV, IG, Target),
%%  restoreAdj(AdjToN, N, IG1, Alias, Target).

%% XXX This is probably a clumsy way of doing it
%% better to assure the lists are sorted from the beginning
%% also coalesce findNew and removeAdj should improve performance
findNew(Adj, Saved) ->
  findNew(Adj, Saved, []).

findNew([], _Saved, New) ->
  New;
findNew([A| Adj], Saved, New) ->
  case lists:member(A, Saved) of
    true -> findNew(Adj, Saved, New);
    _ -> findNew(Adj, Saved, [A| New])
  end.

%test_fixAdj(0, _SavedAdj, IG, _Target) ->
%  IG;
%test_fixAdj(Node, SavedAdj, IG, Target) ->
%  NextNode = Node - 1,
%  IG1 = fixAdj(NextNode, SavedAdj, IG, Target),
%  test_fixAdj(NextNode, SavedAdj, IG1, Target).
%%----------------------------------------------------------------------
%% Function:    ok
%%
%% Description: Checks if a node T is suitable to coalesce with R
%%
%% Parameters:
%%   T             -- Node to test
%%   R             -- Other node to test
%%   IG            -- Interference graph
%%   K             -- Number of registers
%%   Target        -- The module containing the target-specific functions
%%   
%% Returns:
%%   true iff coalescing is OK
%%----------------------------------------------------------------------

ok(T, R, IG, K, Target) ->
  ((hipe_ig:is_trivially_colourable(T, K, IG))
   orelse Target:is_precoloured(T)
   orelse hipe_ig:nodes_are_adjacent(T, R, IG)).

%%----------------------------------------------------------------------
%% Function:    all_ok
%%
%% Description: True iff, for every T in the list, OK(T,U)
%%
%% Parameters:
%%   [T|Ts]        -- Nodes to test
%%   U             -- Node to test for coalescing
%%   IG            -- Interference graph
%%   K             -- Number of registers
%%   Target        -- The module containing the target-specific functions
%%   
%% Returns:
%%   true iff coalescing is OK for all nodes in the list
%%----------------------------------------------------------------------

all_adjacent_ok([], _U, _Worklists, _IG, _K, _Target) -> true;
all_adjacent_ok([T|Ts], U, Worklists, IG, K, Target) ->
  case hipe_reg_worklists:member_stack_or_coalesced(T, Worklists) of
    true -> all_adjacent_ok(Ts, U, Worklists, IG, K, Target);
    _ ->
      %% 'andalso' does not preserve tail-recursion
      case ok(T, U, IG, K, Target) of
	true -> all_adjacent_ok(Ts, U, Worklists, IG, K, Target);
	false -> false
      end
  end.

%%----------------------------------------------------------------------
%% Function:    conservative
%%
%% Description: Checks if nodes can be safely coalesced according to
%%              the Briggs' conservative coalescing heuristic
%%
%% Parameters:
%%   Nodes         -- Adjacent nodes
%%   IG            -- Interference graph
%%   K             -- Number of registers
%%   
%% Returns:
%%   true iff coalescing is safe
%%----------------------------------------------------------------------

conservative(AdjU, AdjV, U, Worklists, IG, K) ->
  conservative_countU(AdjU, AdjV, U, Worklists, IG, K, 0).

%%----------------------------------------------------------------------
%% Function:    conservative_count
%%
%% Description: Counts degrees for conservative (Briggs' heuristics)
%%
%% Parameters:
%%   Nodes         -- (Remaining) adjacent nodes
%%   IG            -- Interference graph
%%   K             -- Number of registers
%%   Cnt           -- Accumulator for counting
%%   
%% Returns:
%%   Final value of accumulator
%%----------------------------------------------------------------------

conservative_countU([], AdjV, U, Worklists, IG, K, Cnt) ->
  conservative_countV(AdjV, U, Worklists, IG, K, Cnt);
conservative_countU([Node|AdjU], AdjV, U, Worklists, IG, K, Cnt) ->
  case hipe_reg_worklists:member_stack_or_coalesced(Node, Worklists) of
    true -> conservative_countU(AdjU, AdjV, U, Worklists, IG, K, Cnt);
    _ ->
      case hipe_ig:is_trivially_colourable(Node, K, IG) of
	true -> conservative_countU(AdjU, AdjV, U, Worklists, IG, K, Cnt);
	_ ->
	  Cnt1 = Cnt + 1,
	  if Cnt1 < K -> conservative_countU(AdjU, AdjV, U, Worklists, IG, K, Cnt1);
	     true -> false
	  end
      end
  end.

conservative_countV([], _U, _Worklists, _IG, _K, _Cnt) -> true;
conservative_countV([Node|AdjV], U, Worklists, IG, K, Cnt) ->
  case hipe_reg_worklists:member_stack_or_coalesced(Node, Worklists) of
    true -> conservative_countV(AdjV, U, Worklists, IG, K, Cnt);
    _ ->
      case hipe_ig:nodes_are_adjacent(Node, U, IG) of
	true -> conservative_countV(AdjV, U, Worklists, IG, K, Cnt);
	_ ->
	  case hipe_ig:is_trivially_colourable(Node, K, IG) of
	    true -> conservative_countV(AdjV, U, Worklists, IG, K, Cnt);
	    _ ->
	      Cnt1 = Cnt + 1,
	      if Cnt1 < K -> conservative_countV(AdjV, U, Worklists, IG, K, Cnt1);
		 true -> false
	      end
	  end
      end
  end.

%%---------------------------------------------------------------------
%% Function:    selectSpill
%% 
%% Description: Select the node to spill and spill it
%% Parameters:
%%   WorkLists      -- A datatype containing the different worklists
%%   IG             -- The interference graph
%%   K              -- The number of available registers
%%   Alias          -- The alias mapping
%%   SpillLimit     -- Try not to spill any nodes above the spill limit
%%
%% Returns:
%%   WorkLists      -- The updated worklists
%%---------------------------------------------------------------------

selectSpill(WorkLists, IG, SpillLimit) ->
  [CAR|CDR] = hipe_reg_worklists:spill(WorkLists),
  SpillCost = getCost(CAR, IG, SpillLimit),
  M = findCheapest(CDR, IG, SpillCost, CAR, SpillLimit),
  WorkLists1 = hipe_reg_worklists:remove_spill(M, WorkLists),
  hipe_reg_worklists:add_simplify(M, WorkLists1).

%%---------------------------------------------------------------------
%% Function:    selectSpill
%% 
%% Description: Select the node to spill and spill it
%% Parameters:
%%   WorkLists      -- A datatype containing the different worklists
%%   Moves          -- A datatype containing the move sets
%%   IG             -- The interference graph
%%   K              -- The number of available registers
%%   Alias          -- The alias mapping
%%   SpillLimit     -- Try not to spill any nodes above the spill limit
%%
%% Returns:
%%   WorkLists      -- The updated worklists
%%   Moves          -- The updated moves
%%---------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
selectSpill_O(WorkLists, Moves, IG, K, Alias, SpillLimit) ->
  [CAR|CDR] = hipe_reg_worklists:spill(WorkLists),
  
  SpillCost = getCost(CAR, IG, SpillLimit),
  M = findCheapest(CDR, IG, SpillCost, CAR, SpillLimit),
  
  WorkLists1 = hipe_reg_worklists:remove_spill(M, WorkLists),
  %% The published algorithm adds M to the simplify worklist
  %% before the freezeMoves() call. That breaks the worklist
  %% invariants, which is why the order is switched here.
  {WorkLists2,Moves1} = freezeMoves(M, K, WorkLists1, Moves, IG, Alias),
  WorkLists3 = hipe_reg_worklists:add_simplify(M, WorkLists2),
  {WorkLists3,Moves1}.
-endif.

%% Find the node that is cheapest to spill

findCheapest([], _IG, _Cost, Cheapest, _SpillLimit) ->
  Cheapest;
findCheapest([Node|Nodes], IG, Cost, Cheapest, SpillLimit) ->
  ThisCost = getCost(Node, IG, SpillLimit),
  case ThisCost < Cost of
    true ->
      findCheapest(Nodes, IG, ThisCost, Node, SpillLimit);
    false ->
      findCheapest(Nodes, IG, Cost, Cheapest, SpillLimit)
  end.

%% Get the cost for spilling a certain node, node numbers above the spill 
%% limit are extremely expensive.

getCost(Node, IG, SpillLimit) ->
  case Node > SpillLimit of
    true -> inf;
    false ->
      SpillCost = hipe_ig:node_spill_cost(Node, IG),
      ?debug_msg("Actual spillcost f node ~w is ~w~n", [Node, SpillCost]),
      SpillCost
  end.

%%----------------------------------------------------------------------
%% Function:    freeze
%%
%% Description: When both simplifying and coalescing is impossible we 
%%              rather freezes a node in stead of spilling, this function
%%              selects a node for freezing (it just picks the first one in
%%              the list)
%%
%% Parameters:
%%   K              -- The number of available registers
%%   WorkLists      -- A datatype containing the different worklists
%%   Moves          -- A datatype containing the different movelists
%%   IG             -- Interference graph
%%   Alias          -- An alias mapping, shows the alias of all coalesced 
%%                      nodes  
%%
%% Returns:
%%   WorkLists      -- The updated worklists
%%   Moves          -- The updated movelists
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
freeze(K, WorkLists, Moves, IG, Alias) ->
  [U|_] = hipe_reg_worklists:freeze(WorkLists),         % Smarter routine?
  ?debug_msg("freezing node ~p~n", [U]),
  WorkLists0 = hipe_reg_worklists:remove_freeze(U, WorkLists),
  %% The published algorithm adds U to the simplify worklist
  %% before the freezeMoves() call. That breaks the worklist
  %% invariants, which is why the order is switched here.
  {WorkLists1, Moves1} = freezeMoves(U, K, WorkLists0, Moves, IG, Alias),
  WorkLists2 = hipe_reg_worklists:add_simplify(U, WorkLists1),
  {WorkLists2, Moves1}.
-endif.

%%----------------------------------------------------------------------
%% Function:    freezeMoves
%%
%% Description: Make all move related interferences for a certain node 
%%              into ordinary interference arcs.
%%              
%% Parameters:
%%   U              -- The node we want to freeze
%%   K              -- The number of available registers
%%   WorkLists      -- A datatype containing the different worklists
%%   Moves          -- A datatype containing the different movelists
%%   IG             -- Interference graph
%%   Alias          -- An alias mapping, shows the alias of all coalesced 
%%                     nodes  
%%
%% Returns:
%%   WorkLists      -- The updated worklists
%%   Moves          -- The updated movelists
%%----------------------------------------------------------------------

-ifdef(COMPARE_ITERATED_OPTIMISTIC).
freezeMoves(U, K, WorkLists, Moves, IG, Alias) ->
  Nodes = hipe_moves:node_moves(U, Moves),
  freezeEm(U, Nodes, K, WorkLists, Moves, IG, Alias).

%% Find what the other value in a copy instruction is, return false if 
%% the instruction isn't a move with the first argument in it.

moves(U, Move, Alias, Moves) ->
  {X,Y} = hipe_moves:get_move(Move, Moves),
  %% The old code (which followed the published algorithm) did
  %% not follow aliases before looking for "the other" node.
  %% This caused moves() to skip some moves, making some nodes
  %% still move-related after freezeMoves(). These move-related
  %% nodes were then added to the simplify worklist (by freeze()
  %% or selectSpill()), breaking the worklist invariants. Nodes
  %% already simplified appeared in coalesce_O(), were re-added to
  %% the simplify worklist by add_worklist(), simplified again,
  %% and coloured multiple times by assignColors(). Ouch!
  X1 = getAlias(X, Alias),
  Y1 = getAlias(Y, Alias),
  if U =:= X1 -> Y1;
     U =:= Y1 -> X1;
     true -> exit({?MODULE,moves}) % XXX: shouldn't happen
  end.

freezeEm(_U, [], _K, WorkLists, Moves, _IG, _Alias) -> 
  {WorkLists,Moves};
freezeEm(U, [M|Ms], K, WorkLists, Moves, IG, Alias) ->
  V = moves(U, M, Alias, Moves),
  {WorkLists2,Moves2} = freezeEm2(U, V, M, K, WorkLists, Moves, IG, Alias),
  freezeEm(U, Ms, K, WorkLists2, Moves2, IG, Alias).

freezeEm2(U, V, M, K, WorkLists, Moves, IG, Alias) ->
  case hipe_moves:member_active(M, Moves) of
    true ->
      Moves1 = hipe_moves:remove_active(M, Moves),
      freezeEm3(U, V, M, K, WorkLists, Moves1, IG, Alias);	
    false ->
      Moves1 = hipe_moves:remove_worklist(M, Moves),
      freezeEm3(U, V, M, K, WorkLists, Moves1, IG, Alias)
  end.

freezeEm3(_U,V,_M,K,WorkLists,Moves,IG,_Alias) ->
  Moves1 = Moves, % drop frozen move M
  V1 = V, % getAlias(V,Alias),
  %% "not MoveRelated(v)" is cheaper than "NodeMoves(v) = {}"
  case ((not hipe_moves:move_related(V1,Moves1)) andalso
	hipe_ig:is_trivially_colourable(V1,K,IG)) of
    true ->
      ?debug_msg("freezing move to ~p~n", [V]),
      Worklists1 = hipe_reg_worklists:transfer_freeze_simplify(V1, WorkLists),
      {Worklists1,Moves1};
    false ->
      {WorkLists,Moves1}
  end.
-endif.