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
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
|
/* IRA hard register and memory cost calculation for allocnos or pseudos.
Copyright (C) 2006-2024 Free Software Foundation, Inc.
Contributed by Vladimir Makarov <vmakarov@redhat.com>.
This file is part of GCC.
GCC 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 3, or (at your option) any later
version.
GCC 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 GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "predict.h"
#include "memmodel.h"
#include "tm_p.h"
#include "insn-config.h"
#include "regs.h"
#include "regset.h"
#include "ira.h"
#include "ira-int.h"
#include "addresses.h"
#include "reload.h"
#include "print-rtl.h"
/* The flags is set up every time when we calculate pseudo register
classes through function ira_set_pseudo_classes. */
static bool pseudo_classes_defined_p = false;
/* TRUE if we work with allocnos. Otherwise we work with pseudos. */
static bool allocno_p;
/* Number of elements in array `costs'. */
static int cost_elements_num;
/* The `costs' struct records the cost of using hard registers of each
class considered for the calculation and of using memory for each
allocno or pseudo. */
struct costs
{
int mem_cost;
/* Costs for register classes start here. We process only some
allocno classes. */
int cost[1];
};
#define max_struct_costs_size \
(this_target_ira_int->x_max_struct_costs_size)
#define init_cost \
(this_target_ira_int->x_init_cost)
#define temp_costs \
(this_target_ira_int->x_temp_costs)
#define op_costs \
(this_target_ira_int->x_op_costs)
#define this_op_costs \
(this_target_ira_int->x_this_op_costs)
/* Costs of each class for each allocno or pseudo. */
static struct costs *costs;
/* Accumulated costs of each class for each allocno. */
static struct costs *total_allocno_costs;
/* It is the current size of struct costs. */
static size_t struct_costs_size;
/* Return pointer to structure containing costs of allocno or pseudo
with given NUM in array ARR. */
#define COSTS(arr, num) \
((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
/* Return index in COSTS when processing reg with REGNO. */
#define COST_INDEX(regno) (allocno_p \
? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
: (int) regno)
/* Record register class preferences of each allocno or pseudo. Null
value means no preferences. It happens on the 1st iteration of the
cost calculation. */
static enum reg_class *pref;
/* Allocated buffers for pref. */
static enum reg_class *pref_buffer;
/* Record allocno class of each allocno with the same regno. */
static enum reg_class *regno_aclass;
/* Record cost gains for not allocating a register with an invariant
equivalence. */
static int *regno_equiv_gains;
/* Execution frequency of the current insn. */
static int frequency;
/* Info about reg classes whose costs are calculated for a pseudo. */
struct cost_classes
{
/* Number of the cost classes in the subsequent array. */
int num;
/* Container of the cost classes. */
enum reg_class classes[N_REG_CLASSES];
/* Map reg class -> index of the reg class in the previous array.
-1 if it is not a cost class. */
int index[N_REG_CLASSES];
/* Map hard regno index of first class in array CLASSES containing
the hard regno, -1 otherwise. */
int hard_regno_index[FIRST_PSEUDO_REGISTER];
};
/* Types of pointers to the structure above. */
typedef struct cost_classes *cost_classes_t;
typedef const struct cost_classes *const_cost_classes_t;
/* Info about cost classes for each pseudo. */
static cost_classes_t *regno_cost_classes;
/* Helper for cost_classes hashing. */
struct cost_classes_hasher : pointer_hash <cost_classes>
{
static inline hashval_t hash (const cost_classes *);
static inline bool equal (const cost_classes *, const cost_classes *);
static inline void remove (cost_classes *);
};
/* Returns hash value for cost classes info HV. */
inline hashval_t
cost_classes_hasher::hash (const cost_classes *hv)
{
return iterative_hash (&hv->classes, sizeof (enum reg_class) * hv->num, 0);
}
/* Compares cost classes info HV1 and HV2. */
inline bool
cost_classes_hasher::equal (const cost_classes *hv1, const cost_classes *hv2)
{
return (hv1->num == hv2->num
&& memcmp (hv1->classes, hv2->classes,
sizeof (enum reg_class) * hv1->num) == 0);
}
/* Delete cost classes info V from the hash table. */
inline void
cost_classes_hasher::remove (cost_classes *v)
{
ira_free (v);
}
/* Hash table of unique cost classes. */
static hash_table<cost_classes_hasher> *cost_classes_htab;
/* Map allocno class -> cost classes for pseudo of given allocno
class. */
static cost_classes_t cost_classes_aclass_cache[N_REG_CLASSES];
/* Map mode -> cost classes for pseudo of give mode. */
static cost_classes_t cost_classes_mode_cache[MAX_MACHINE_MODE];
/* Cost classes that include all classes in ira_important_classes. */
static cost_classes all_cost_classes;
/* Use the array of classes in CLASSES_PTR to fill out the rest of
the structure. */
static void
complete_cost_classes (cost_classes_t classes_ptr)
{
for (int i = 0; i < N_REG_CLASSES; i++)
classes_ptr->index[i] = -1;
for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
classes_ptr->hard_regno_index[i] = -1;
for (int i = 0; i < classes_ptr->num; i++)
{
enum reg_class cl = classes_ptr->classes[i];
classes_ptr->index[cl] = i;
for (int j = ira_class_hard_regs_num[cl] - 1; j >= 0; j--)
{
unsigned int hard_regno = ira_class_hard_regs[cl][j];
if (classes_ptr->hard_regno_index[hard_regno] < 0)
classes_ptr->hard_regno_index[hard_regno] = i;
}
}
}
/* Initialize info about the cost classes for each pseudo. */
static void
initiate_regno_cost_classes (void)
{
int size = sizeof (cost_classes_t) * max_reg_num ();
regno_cost_classes = (cost_classes_t *) ira_allocate (size);
memset (regno_cost_classes, 0, size);
memset (cost_classes_aclass_cache, 0,
sizeof (cost_classes_t) * N_REG_CLASSES);
memset (cost_classes_mode_cache, 0,
sizeof (cost_classes_t) * MAX_MACHINE_MODE);
cost_classes_htab = new hash_table<cost_classes_hasher> (200);
all_cost_classes.num = ira_important_classes_num;
for (int i = 0; i < ira_important_classes_num; i++)
all_cost_classes.classes[i] = ira_important_classes[i];
complete_cost_classes (&all_cost_classes);
}
/* Create new cost classes from cost classes FROM and set up members
index and hard_regno_index. Return the new classes. The function
implements some common code of two functions
setup_regno_cost_classes_by_aclass and
setup_regno_cost_classes_by_mode. */
static cost_classes_t
setup_cost_classes (cost_classes_t from)
{
cost_classes_t classes_ptr;
classes_ptr = (cost_classes_t) ira_allocate (sizeof (struct cost_classes));
classes_ptr->num = from->num;
for (int i = 0; i < from->num; i++)
classes_ptr->classes[i] = from->classes[i];
complete_cost_classes (classes_ptr);
return classes_ptr;
}
/* Return a version of FULL that only considers registers in REGS that are
valid for mode MODE. Both FULL and the returned class are globally
allocated. */
static cost_classes_t
restrict_cost_classes (cost_classes_t full, machine_mode mode,
const_hard_reg_set regs)
{
static struct cost_classes narrow;
int map[N_REG_CLASSES];
narrow.num = 0;
for (int i = 0; i < full->num; i++)
{
/* Assume that we'll drop the class. */
map[i] = -1;
/* Ignore classes that are too small for the mode. */
enum reg_class cl = full->classes[i];
if (!contains_reg_of_mode[cl][mode])
continue;
/* Calculate the set of registers in CL that belong to REGS and
are valid for MODE. */
HARD_REG_SET valid_for_cl = reg_class_contents[cl] & regs;
valid_for_cl &= ~(ira_prohibited_class_mode_regs[cl][mode]
| ira_no_alloc_regs);
if (hard_reg_set_empty_p (valid_for_cl))
continue;
/* Don't use this class if the set of valid registers is a subset
of an existing class. For example, suppose we have two classes
GR_REGS and FR_REGS and a union class GR_AND_FR_REGS. Suppose
that the mode changes allowed by FR_REGS are not as general as
the mode changes allowed by GR_REGS.
In this situation, the mode changes for GR_AND_FR_REGS could
either be seen as the union or the intersection of the mode
changes allowed by the two subclasses. The justification for
the union-based definition would be that, if you want a mode
change that's only allowed by GR_REGS, you can pick a register
from the GR_REGS subclass. The justification for the
intersection-based definition would be that every register
from the class would allow the mode change.
However, if we have a register that needs to be in GR_REGS,
using GR_AND_FR_REGS with the intersection-based definition
would be too pessimistic, since it would bring in restrictions
that only apply to FR_REGS. Conversely, if we have a register
that needs to be in FR_REGS, using GR_AND_FR_REGS with the
union-based definition would lose the extra restrictions
placed on FR_REGS. GR_AND_FR_REGS is therefore only useful
for cases where GR_REGS and FP_REGS are both valid. */
int pos;
for (pos = 0; pos < narrow.num; ++pos)
{
enum reg_class cl2 = narrow.classes[pos];
if (hard_reg_set_subset_p (valid_for_cl, reg_class_contents[cl2]))
break;
}
map[i] = pos;
if (pos == narrow.num)
{
/* If several classes are equivalent, prefer to use the one
that was chosen as the allocno class. */
enum reg_class cl2 = ira_allocno_class_translate[cl];
if (ira_class_hard_regs_num[cl] == ira_class_hard_regs_num[cl2])
cl = cl2;
narrow.classes[narrow.num++] = cl;
}
}
if (narrow.num == full->num)
return full;
cost_classes **slot = cost_classes_htab->find_slot (&narrow, INSERT);
if (*slot == NULL)
{
cost_classes_t classes = setup_cost_classes (&narrow);
/* Map equivalent classes to the representative that we chose above. */
for (int i = 0; i < ira_important_classes_num; i++)
{
enum reg_class cl = ira_important_classes[i];
int index = full->index[cl];
if (index >= 0)
classes->index[cl] = map[index];
}
*slot = classes;
}
return *slot;
}
/* Setup cost classes for pseudo REGNO whose allocno class is ACLASS.
This function is used when we know an initial approximation of
allocno class of the pseudo already, e.g. on the second iteration
of class cost calculation or after class cost calculation in
register-pressure sensitive insn scheduling or register-pressure
sensitive loop-invariant motion. */
static void
setup_regno_cost_classes_by_aclass (int regno, enum reg_class aclass)
{
static struct cost_classes classes;
cost_classes_t classes_ptr;
enum reg_class cl;
int i;
cost_classes **slot;
HARD_REG_SET temp, temp2;
bool exclude_p;
if ((classes_ptr = cost_classes_aclass_cache[aclass]) == NULL)
{
temp = reg_class_contents[aclass] & ~ira_no_alloc_regs;
/* We exclude classes from consideration which are subsets of
ACLASS only if ACLASS is an uniform class. */
exclude_p = ira_uniform_class_p[aclass];
classes.num = 0;
for (i = 0; i < ira_important_classes_num; i++)
{
cl = ira_important_classes[i];
if (exclude_p)
{
/* Exclude non-uniform classes which are subsets of
ACLASS. */
temp2 = reg_class_contents[cl] & ~ira_no_alloc_regs;
if (hard_reg_set_subset_p (temp2, temp) && cl != aclass)
continue;
}
classes.classes[classes.num++] = cl;
}
slot = cost_classes_htab->find_slot (&classes, INSERT);
if (*slot == NULL)
{
classes_ptr = setup_cost_classes (&classes);
*slot = classes_ptr;
}
classes_ptr = cost_classes_aclass_cache[aclass] = (cost_classes_t) *slot;
}
if (regno_reg_rtx[regno] != NULL_RTX)
{
/* Restrict the classes to those that are valid for REGNO's mode
(which might for example exclude singleton classes if the mode
requires two registers). Also restrict the classes to those that
are valid for subregs of REGNO. */
const HARD_REG_SET *valid_regs = valid_mode_changes_for_regno (regno);
if (!valid_regs)
valid_regs = ®_class_contents[ALL_REGS];
classes_ptr = restrict_cost_classes (classes_ptr,
PSEUDO_REGNO_MODE (regno),
*valid_regs);
}
regno_cost_classes[regno] = classes_ptr;
}
/* Setup cost classes for pseudo REGNO with MODE. Usage of MODE can
decrease number of cost classes for the pseudo, if hard registers
of some important classes cannot hold a value of MODE. So the
pseudo cannot get hard register of some important classes and cost
calculation for such important classes is only wasting CPU
time. */
static void
setup_regno_cost_classes_by_mode (int regno, machine_mode mode)
{
if (const HARD_REG_SET *valid_regs = valid_mode_changes_for_regno (regno))
regno_cost_classes[regno] = restrict_cost_classes (&all_cost_classes,
mode, *valid_regs);
else
{
if (cost_classes_mode_cache[mode] == NULL)
cost_classes_mode_cache[mode]
= restrict_cost_classes (&all_cost_classes, mode,
reg_class_contents[ALL_REGS]);
regno_cost_classes[regno] = cost_classes_mode_cache[mode];
}
}
/* Finalize info about the cost classes for each pseudo. */
static void
finish_regno_cost_classes (void)
{
ira_free (regno_cost_classes);
delete cost_classes_htab;
cost_classes_htab = NULL;
}
/* Compute the cost of loading X into (if TO_P is TRUE) or from (if
TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
be a pseudo register. */
static int
copy_cost (rtx x, machine_mode mode, reg_class_t rclass, bool to_p,
secondary_reload_info *prev_sri)
{
secondary_reload_info sri;
reg_class_t secondary_class = NO_REGS;
/* If X is a SCRATCH, there is actually nothing to move since we are
assuming optimal allocation. */
if (GET_CODE (x) == SCRATCH)
return 0;
/* Get the class we will actually use for a reload. */
rclass = targetm.preferred_reload_class (x, rclass);
/* If we need a secondary reload for an intermediate, the cost is
that to load the input into the intermediate register, then to
copy it. */
sri.prev_sri = prev_sri;
sri.extra_cost = 0;
/* PR 68770: Secondary reload might examine the t_icode field. */
sri.t_icode = CODE_FOR_nothing;
secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
if (secondary_class != NO_REGS)
{
ira_init_register_move_cost_if_necessary (mode);
return (ira_register_move_cost[mode][(int) secondary_class][(int) rclass]
+ sri.extra_cost
+ copy_cost (x, mode, secondary_class, to_p, &sri));
}
/* For memory, use the memory move cost, for (hard) registers, use
the cost to move between the register classes, and use 2 for
everything else (constants). */
if (MEM_P (x) || rclass == NO_REGS)
return sri.extra_cost
+ ira_memory_move_cost[mode][(int) rclass][to_p != 0];
else if (REG_P (x))
{
reg_class_t x_class = REGNO_REG_CLASS (REGNO (x));
ira_init_register_move_cost_if_necessary (mode);
return (sri.extra_cost
+ ira_register_move_cost[mode][(int) x_class][(int) rclass]);
}
else
/* If this is a constant, we may eventually want to call rtx_cost
here. */
return sri.extra_cost + COSTS_N_INSNS (1);
}
/* Record the cost of using memory or hard registers of various
classes for the operands in INSN.
N_ALTS is the number of alternatives.
N_OPS is the number of operands.
OPS is an array of the operands.
MODES are the modes of the operands, in case any are VOIDmode.
CONSTRAINTS are the constraints to use for the operands. This array
is modified by this procedure.
This procedure works alternative by alternative. For each
alternative we assume that we will be able to allocate all allocnos
to their ideal register class and calculate the cost of using that
alternative. Then we compute, for each operand that is a
pseudo-register, the cost of having the allocno allocated to each
register class and using it in that alternative. To this cost is
added the cost of the alternative.
The cost of each class for this insn is its lowest cost among all
the alternatives. */
static void
record_reg_classes (int n_alts, int n_ops, rtx *ops,
machine_mode *modes, const char **constraints,
rtx_insn *insn, enum reg_class *pref)
{
int alt;
int i, j, k;
int insn_allows_mem[MAX_RECOG_OPERANDS];
move_table *move_in_cost, *move_out_cost;
short (*mem_cost)[2];
const char *p;
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
{
fprintf (ira_dump_file, " Processing insn %u", INSN_UID (insn));
if (INSN_CODE (insn) >= 0
&& (p = get_insn_name (INSN_CODE (insn))) != NULL)
fprintf (ira_dump_file, " {%s}", p);
fprintf (ira_dump_file, " (freq=%d)\n",
REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)));
dump_insn_slim (ira_dump_file, insn);
}
for (i = 0; i < n_ops; i++)
insn_allows_mem[i] = 0;
/* Process each alternative, each time minimizing an operand's cost
with the cost for each operand in that alternative. */
alternative_mask preferred = get_preferred_alternatives (insn);
for (alt = 0; alt < n_alts; alt++)
{
enum reg_class classes[MAX_RECOG_OPERANDS];
int allows_mem[MAX_RECOG_OPERANDS];
enum reg_class rclass;
int alt_fail = 0;
int alt_cost = 0, op_cost_add;
if (!TEST_BIT (preferred, alt))
{
for (i = 0; i < recog_data.n_operands; i++)
constraints[i] = skip_alternative (constraints[i]);
continue;
}
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
{
fprintf (ira_dump_file, " Alt %d:", alt);
for (i = 0; i < n_ops; i++)
{
p = constraints[i];
if (*p == '\0')
continue;
fprintf (ira_dump_file, " (%d) ", i);
for (; *p != '\0' && *p != ',' && *p != '#'; p++)
fputc (*p, ira_dump_file);
}
fprintf (ira_dump_file, "\n");
}
for (i = 0; i < n_ops; i++)
{
unsigned char c;
const char *p = constraints[i];
rtx op = ops[i];
machine_mode mode = modes[i];
int allows_addr = 0;
int win = 0;
/* Initially show we know nothing about the register class. */
classes[i] = NO_REGS;
allows_mem[i] = 0;
/* If this operand has no constraints at all, we can
conclude nothing about it since anything is valid. */
if (*p == 0)
{
if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
memset (this_op_costs[i], 0, struct_costs_size);
continue;
}
/* If this alternative is only relevant when this operand
matches a previous operand, we do different things
depending on whether this operand is a allocno-reg or not.
We must process any modifiers for the operand before we
can make this test. */
while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
p++;
if (p[0] >= '0' && p[0] <= '0' + i)
{
/* Copy class and whether memory is allowed from the
matching alternative. Then perform any needed cost
computations and/or adjustments. */
j = p[0] - '0';
classes[i] = classes[j];
allows_mem[i] = allows_mem[j];
if (allows_mem[i])
insn_allows_mem[i] = 1;
if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
{
/* If this matches the other operand, we have no
added cost and we win. */
if (rtx_equal_p (ops[j], op))
win = 1;
/* If we can put the other operand into a register,
add to the cost of this alternative the cost to
copy this operand to the register used for the
other operand. */
else if (classes[j] != NO_REGS)
{
alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
win = 1;
}
}
else if (! REG_P (ops[j])
|| REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
{
/* This op is an allocno but the one it matches is
not. */
/* If we can't put the other operand into a
register, this alternative can't be used. */
if (classes[j] == NO_REGS)
{
alt_fail = 1;
}
else
/* Otherwise, add to the cost of this alternative the cost
to copy the other operand to the hard register used for
this operand. */
{
alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
}
}
else
{
/* The costs of this operand are not the same as the
other operand since move costs are not symmetric.
Moreover, if we cannot tie them, this alternative
needs to do a copy, which is one insn. */
struct costs *pp = this_op_costs[i];
int *pp_costs = pp->cost;
cost_classes_t cost_classes_ptr
= regno_cost_classes[REGNO (op)];
enum reg_class *cost_classes = cost_classes_ptr->classes;
bool in_p = recog_data.operand_type[i] != OP_OUT;
bool out_p = recog_data.operand_type[i] != OP_IN;
enum reg_class op_class = classes[i];
ira_init_register_move_cost_if_necessary (mode);
if (! in_p)
{
ira_assert (out_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][0] * frequency;
}
}
else
{
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_out_cost[op_class][rclass] * frequency;
}
}
}
else if (! out_p)
{
ira_assert (in_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][1] * frequency;
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_in_cost[rclass][op_class] * frequency;
}
}
}
else
{
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((mem_cost[rclass][0]
+ mem_cost[rclass][1])
* frequency);
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((move_in_cost[rclass][op_class]
+ move_out_cost[op_class][rclass])
* frequency);
}
}
}
/* If the alternative actually allows memory, make
things a bit cheaper since we won't need an extra
insn to load it. */
pp->mem_cost
= ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
- allows_mem[i]) * frequency;
/* If we have assigned a class to this allocno in
our first pass, add a cost to this alternative
corresponding to what we would add if this
allocno were not in the appropriate class. */
if (pref)
{
enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
if (pref_class == NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p
? ira_memory_move_cost[mode][op_class][1]
: 0));
else if (ira_reg_class_intersect
[pref_class][op_class] == NO_REGS)
alt_cost
+= ira_register_move_cost[mode][pref_class][op_class];
}
if (REGNO (ops[i]) != REGNO (ops[j])
&& ! find_reg_note (insn, REG_DEAD, op))
alt_cost += 2;
p++;
}
}
/* Scan all the constraint letters. See if the operand
matches any of the constraints. Collect the valid
register classes and see if this operand accepts
memory. */
while ((c = *p))
{
switch (c)
{
case '*':
/* Ignore the next letter for this pass. */
c = *++p;
break;
case '^':
alt_cost += 2;
break;
case '?':
alt_cost += 2;
break;
case 'g':
if (MEM_P (op)
|| (CONSTANT_P (op)
&& (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
win = 1;
insn_allows_mem[i] = allows_mem[i] = 1;
classes[i] = ira_reg_class_subunion[classes[i]][GENERAL_REGS];
break;
default:
enum constraint_num cn = lookup_constraint (p);
enum reg_class cl;
switch (get_constraint_type (cn))
{
case CT_REGISTER:
cl = reg_class_for_constraint (cn);
if (cl != NO_REGS)
classes[i] = ira_reg_class_subunion[classes[i]][cl];
break;
case CT_CONST_INT:
if (CONST_INT_P (op)
&& insn_const_int_ok_for_constraint (INTVAL (op), cn))
win = 1;
break;
case CT_MEMORY:
case CT_RELAXED_MEMORY:
/* Every MEM can be reloaded to fit. */
insn_allows_mem[i] = allows_mem[i] = 1;
if (MEM_P (op))
win = 1;
break;
case CT_SPECIAL_MEMORY:
insn_allows_mem[i] = allows_mem[i] = 1;
if (MEM_P (extract_mem_from_operand (op))
&& constraint_satisfied_p (op, cn))
win = 1;
break;
case CT_ADDRESS:
/* Every address can be reloaded to fit. */
allows_addr = 1;
if (address_operand (op, GET_MODE (op))
|| constraint_satisfied_p (op, cn))
win = 1;
/* We know this operand is an address, so we
want it to be allocated to a hard register
that can be the base of an address,
i.e. BASE_REG_CLASS. */
classes[i]
= ira_reg_class_subunion[classes[i]]
[base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
ADDRESS, SCRATCH)];
break;
case CT_FIXED_FORM:
if (constraint_satisfied_p (op, cn))
win = 1;
break;
}
break;
}
p += CONSTRAINT_LEN (c, p);
if (c == ',')
break;
}
constraints[i] = p;
if (alt_fail)
break;
/* How we account for this operand now depends on whether it
is a pseudo register or not. If it is, we first check if
any register classes are valid. If not, we ignore this
alternative, since we want to assume that all allocnos get
allocated for register preferencing. If some register
class is valid, compute the costs of moving the allocno
into that class. */
if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
{
if (classes[i] == NO_REGS && ! allows_mem[i])
{
/* We must always fail if the operand is a REG, but
we did not find a suitable class and memory is
not allowed.
Otherwise we may perform an uninitialized read
from this_op_costs after the `continue' statement
below. */
alt_fail = 1;
}
else
{
unsigned int regno = REGNO (op);
struct costs *pp = this_op_costs[i];
int *pp_costs = pp->cost;
cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
enum reg_class *cost_classes = cost_classes_ptr->classes;
bool in_p = recog_data.operand_type[i] != OP_OUT;
bool out_p = recog_data.operand_type[i] != OP_IN;
enum reg_class op_class = classes[i];
ira_init_register_move_cost_if_necessary (mode);
if (! in_p)
{
ira_assert (out_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][0] * frequency;
}
}
else
{
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_out_cost[op_class][rclass] * frequency;
}
}
}
else if (! out_p)
{
ira_assert (in_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][1] * frequency;
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_in_cost[rclass][op_class] * frequency;
}
}
}
else
{
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((mem_cost[rclass][0]
+ mem_cost[rclass][1])
* frequency);
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((move_in_cost[rclass][op_class]
+ move_out_cost[op_class][rclass])
* frequency);
}
}
}
if (op_class == NO_REGS)
/* Although we don't need insn to reload from
memory, still accessing memory is usually more
expensive than a register. */
pp->mem_cost = frequency;
else
/* If the alternative actually allows memory, make
things a bit cheaper since we won't need an
extra insn to load it. */
pp->mem_cost
= ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
- allows_mem[i]) * frequency;
/* If we have assigned a class to this allocno in
our first pass, add a cost to this alternative
corresponding to what we would add if this
allocno were not in the appropriate class. */
if (pref)
{
enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
if (pref_class == NO_REGS)
{
if (op_class != NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][op_class][0]
: 0)
+ (in_p
? ira_memory_move_cost[mode][op_class][1]
: 0));
}
else if (op_class == NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][pref_class][1]
: 0)
+ (in_p
? ira_memory_move_cost[mode][pref_class][0]
: 0));
else if (ira_reg_class_intersect[pref_class][op_class]
== NO_REGS)
alt_cost += (ira_register_move_cost
[mode][pref_class][op_class]);
}
}
}
/* Otherwise, if this alternative wins, either because we
have already determined that or if we have a hard
register of the proper class, there is no cost for this
alternative. */
else if (win || (REG_P (op)
&& reg_fits_class_p (op, classes[i],
0, GET_MODE (op))))
;
/* If registers are valid, the cost of this alternative
includes copying the object to and/or from a
register. */
else if (classes[i] != NO_REGS)
{
if (recog_data.operand_type[i] != OP_OUT)
alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
if (recog_data.operand_type[i] != OP_IN)
alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
}
/* The only other way this alternative can be used is if
this is a constant that could be placed into memory. */
else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
alt_cost += ira_memory_move_cost[mode][classes[i]][1];
else
alt_fail = 1;
if (alt_fail)
break;
}
if (alt_fail)
{
/* The loop above might have exited early once the failure
was seen. Skip over the constraints for the remaining
operands. */
i += 1;
for (; i < n_ops; ++i)
constraints[i] = skip_alternative (constraints[i]);
continue;
}
op_cost_add = alt_cost * frequency;
/* Finally, update the costs with the information we've
calculated about this alternative. */
for (i = 0; i < n_ops; i++)
if (REG_P (ops[i]) && REGNO (ops[i]) >= FIRST_PSEUDO_REGISTER)
{
int old_cost;
bool cost_change_p = false;
struct costs *pp = op_costs[i], *qq = this_op_costs[i];
int *pp_costs = pp->cost, *qq_costs = qq->cost;
int scale = 1 + (recog_data.operand_type[i] == OP_INOUT);
cost_classes_t cost_classes_ptr
= regno_cost_classes[REGNO (ops[i])];
old_cost = pp->mem_cost;
pp->mem_cost = MIN (old_cost,
(qq->mem_cost + op_cost_add) * scale);
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5
&& pp->mem_cost < old_cost)
{
cost_change_p = true;
fprintf (ira_dump_file, " op %d(r=%u) new costs MEM:%d",
i, REGNO(ops[i]), pp->mem_cost);
}
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
old_cost = pp_costs[k];
pp_costs[k]
= MIN (old_cost, (qq_costs[k] + op_cost_add) * scale);
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5
&& pp_costs[k] < old_cost)
{
if (!cost_change_p)
fprintf (ira_dump_file, " op %d(r=%u) new costs",
i, REGNO(ops[i]));
cost_change_p = true;
fprintf (ira_dump_file, " %s:%d",
reg_class_names[cost_classes_ptr->classes[k]],
pp_costs[k]);
}
}
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5
&& cost_change_p)
fprintf (ira_dump_file, "\n");
}
}
if (allocno_p)
for (i = 0; i < n_ops; i++)
{
ira_allocno_t a;
rtx op = ops[i];
if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
continue;
a = ira_curr_regno_allocno_map [REGNO (op)];
if (! ALLOCNO_BAD_SPILL_P (a) && insn_allows_mem[i] == 0)
ALLOCNO_BAD_SPILL_P (a) = true;
}
}
/* Wrapper around REGNO_OK_FOR_INDEX_P, to allow pseudo registers. */
static inline bool
ok_for_index_p_nonstrict (rtx reg)
{
unsigned regno = REGNO (reg);
return regno >= FIRST_PSEUDO_REGISTER || REGNO_OK_FOR_INDEX_P (regno);
}
/* A version of regno_ok_for_base_p for use here, when all
pseudo-registers should count as OK. Arguments as for
regno_ok_for_base_p. */
static inline bool
ok_for_base_p_nonstrict (rtx reg, machine_mode mode, addr_space_t as,
enum rtx_code outer_code, enum rtx_code index_code)
{
unsigned regno = REGNO (reg);
if (regno >= FIRST_PSEUDO_REGISTER)
return true;
return ok_for_base_p_1 (regno, mode, as, outer_code, index_code);
}
/* Record the pseudo registers we must reload into hard registers in a
subexpression of a memory address, X.
If CONTEXT is 0, we are looking at the base part of an address,
otherwise we are looking at the index part.
MODE and AS are the mode and address space of the memory reference;
OUTER_CODE and INDEX_CODE give the context that the rtx appears in.
These four arguments are passed down to base_reg_class.
SCALE is twice the amount to multiply the cost by (it is twice so
we can represent half-cost adjustments). */
static void
record_address_regs (machine_mode mode, addr_space_t as, rtx x,
int context, enum rtx_code outer_code,
enum rtx_code index_code, int scale)
{
enum rtx_code code = GET_CODE (x);
enum reg_class rclass;
if (context == 1)
rclass = INDEX_REG_CLASS;
else
rclass = base_reg_class (mode, as, outer_code, index_code);
switch (code)
{
case CONST_INT:
case CONST:
case PC:
case SYMBOL_REF:
case LABEL_REF:
return;
case PLUS:
/* When we have an address that is a sum, we must determine
whether registers are "base" or "index" regs. If there is a
sum of two registers, we must choose one to be the "base".
Luckily, we can use the REG_POINTER to make a good choice
most of the time. We only need to do this on machines that
can have two registers in an address and where the base and
index register classes are different.
??? This code used to set REGNO_POINTER_FLAG in some cases,
but that seems bogus since it should only be set when we are
sure the register is being used as a pointer. */
{
rtx arg0 = XEXP (x, 0);
rtx arg1 = XEXP (x, 1);
enum rtx_code code0 = GET_CODE (arg0);
enum rtx_code code1 = GET_CODE (arg1);
/* Look inside subregs. */
if (code0 == SUBREG)
arg0 = SUBREG_REG (arg0), code0 = GET_CODE (arg0);
if (code1 == SUBREG)
arg1 = SUBREG_REG (arg1), code1 = GET_CODE (arg1);
/* If index registers do not appear, or coincide with base registers,
just record registers in any non-constant operands. We
assume here, as well as in the tests below, that all
addresses are in canonical form. */
if (MAX_REGS_PER_ADDRESS == 1
|| INDEX_REG_CLASS == base_reg_class (VOIDmode, as, PLUS, SCRATCH))
{
record_address_regs (mode, as, arg0, context, PLUS, code1, scale);
if (! CONSTANT_P (arg1))
record_address_regs (mode, as, arg1, context, PLUS, code0, scale);
}
/* If the second operand is a constant integer, it doesn't
change what class the first operand must be. */
else if (CONST_SCALAR_INT_P (arg1))
record_address_regs (mode, as, arg0, context, PLUS, code1, scale);
/* If the second operand is a symbolic constant, the first
operand must be an index register. */
else if (code1 == SYMBOL_REF || code1 == CONST || code1 == LABEL_REF)
record_address_regs (mode, as, arg0, 1, PLUS, code1, scale);
/* If both operands are registers but one is already a hard
register of index or reg-base class, give the other the
class that the hard register is not. */
else if (code0 == REG && code1 == REG
&& REGNO (arg0) < FIRST_PSEUDO_REGISTER
&& (ok_for_base_p_nonstrict (arg0, mode, as, PLUS, REG)
|| ok_for_index_p_nonstrict (arg0)))
record_address_regs (mode, as, arg1,
ok_for_base_p_nonstrict (arg0, mode, as,
PLUS, REG) ? 1 : 0,
PLUS, REG, scale);
else if (code0 == REG && code1 == REG
&& REGNO (arg1) < FIRST_PSEUDO_REGISTER
&& (ok_for_base_p_nonstrict (arg1, mode, as, PLUS, REG)
|| ok_for_index_p_nonstrict (arg1)))
record_address_regs (mode, as, arg0,
ok_for_base_p_nonstrict (arg1, mode, as,
PLUS, REG) ? 1 : 0,
PLUS, REG, scale);
/* If one operand is known to be a pointer, it must be the
base with the other operand the index. Likewise if the
other operand is a MULT. */
else if ((code0 == REG && REG_POINTER (arg0)) || code1 == MULT)
{
record_address_regs (mode, as, arg0, 0, PLUS, code1, scale);
record_address_regs (mode, as, arg1, 1, PLUS, code0, scale);
}
else if ((code1 == REG && REG_POINTER (arg1)) || code0 == MULT)
{
record_address_regs (mode, as, arg0, 1, PLUS, code1, scale);
record_address_regs (mode, as, arg1, 0, PLUS, code0, scale);
}
/* Otherwise, count equal chances that each might be a base or
index register. This case should be rare. */
else
{
record_address_regs (mode, as, arg0, 0, PLUS, code1, scale / 2);
record_address_regs (mode, as, arg0, 1, PLUS, code1, scale / 2);
record_address_regs (mode, as, arg1, 0, PLUS, code0, scale / 2);
record_address_regs (mode, as, arg1, 1, PLUS, code0, scale / 2);
}
}
break;
/* Double the importance of an allocno that is incremented or
decremented, since it would take two extra insns if it ends
up in the wrong place. */
case POST_MODIFY:
case PRE_MODIFY:
record_address_regs (mode, as, XEXP (x, 0), 0, code,
GET_CODE (XEXP (XEXP (x, 1), 1)), 2 * scale);
if (REG_P (XEXP (XEXP (x, 1), 1)))
record_address_regs (mode, as, XEXP (XEXP (x, 1), 1), 1, code, REG,
2 * scale);
break;
case POST_INC:
case PRE_INC:
case POST_DEC:
case PRE_DEC:
/* Double the importance of an allocno that is incremented or
decremented, since it would take two extra insns if it ends
up in the wrong place. */
record_address_regs (mode, as, XEXP (x, 0), 0, code, SCRATCH, 2 * scale);
break;
case REG:
{
struct costs *pp;
int *pp_costs;
enum reg_class i;
int k, regno, add_cost;
cost_classes_t cost_classes_ptr;
enum reg_class *cost_classes;
move_table *move_in_cost;
if (REGNO (x) < FIRST_PSEUDO_REGISTER)
break;
regno = REGNO (x);
if (allocno_p)
ALLOCNO_BAD_SPILL_P (ira_curr_regno_allocno_map[regno]) = true;
pp = COSTS (costs, COST_INDEX (regno));
add_cost = (ira_memory_move_cost[Pmode][rclass][1] * scale) / 2;
if (INT_MAX - add_cost < pp->mem_cost)
pp->mem_cost = INT_MAX;
else
pp->mem_cost += add_cost;
cost_classes_ptr = regno_cost_classes[regno];
cost_classes = cost_classes_ptr->classes;
pp_costs = pp->cost;
ira_init_register_move_cost_if_necessary (Pmode);
move_in_cost = ira_may_move_in_cost[Pmode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
i = cost_classes[k];
add_cost = (move_in_cost[i][rclass] * scale) / 2;
if (INT_MAX - add_cost < pp_costs[k])
pp_costs[k] = INT_MAX;
else
pp_costs[k] += add_cost;
}
}
break;
default:
{
const char *fmt = GET_RTX_FORMAT (code);
int i;
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
if (fmt[i] == 'e')
record_address_regs (mode, as, XEXP (x, i), context, code, SCRATCH,
scale);
}
}
}
/* Calculate the costs of insn operands. */
static void
record_operand_costs (rtx_insn *insn, enum reg_class *pref)
{
const char *constraints[MAX_RECOG_OPERANDS];
machine_mode modes[MAX_RECOG_OPERANDS];
rtx set;
int i;
if ((set = single_set (insn)) != NULL_RTX
/* In rare cases the single set insn might have less 2 operands
as the source can be a fixed special reg. */
&& recog_data.n_operands > 1
&& recog_data.operand[0] == SET_DEST (set)
&& recog_data.operand[1] == SET_SRC (set))
{
int regno, other_regno;
rtx dest = SET_DEST (set);
rtx src = SET_SRC (set);
if (GET_CODE (dest) == SUBREG
&& known_eq (GET_MODE_SIZE (GET_MODE (dest)),
GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))))
dest = SUBREG_REG (dest);
if (GET_CODE (src) == SUBREG
&& known_eq (GET_MODE_SIZE (GET_MODE (src)),
GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
src = SUBREG_REG (src);
if (REG_P (src) && REG_P (dest)
&& (((regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
&& (other_regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER)
|| ((regno = REGNO (dest)) >= FIRST_PSEUDO_REGISTER
&& (other_regno = REGNO (src)) < FIRST_PSEUDO_REGISTER)))
{
machine_mode mode = GET_MODE (SET_SRC (set)), cost_mode = mode;
machine_mode hard_reg_mode = GET_MODE(regno_reg_rtx[other_regno]);
poly_int64 pmode_size = GET_MODE_SIZE (mode);
poly_int64 phard_reg_mode_size = GET_MODE_SIZE (hard_reg_mode);
HOST_WIDE_INT mode_size, hard_reg_mode_size;
cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
enum reg_class *cost_classes = cost_classes_ptr->classes;
reg_class_t rclass, hard_reg_class, bigger_hard_reg_class;
int cost_factor = 1, cost, k;
move_table *move_costs;
bool dead_p = find_regno_note (insn, REG_DEAD, REGNO (src));
hard_reg_class = REGNO_REG_CLASS (other_regno);
bigger_hard_reg_class = ira_pressure_class_translate[hard_reg_class];
/* Target code may return any cost for mode which does not fit the
hard reg class (e.g. DImode for AREG on i386). Check this and use
a bigger class to get the right cost. */
if (bigger_hard_reg_class != NO_REGS
&& ! ira_hard_reg_in_set_p (other_regno, mode,
reg_class_contents[hard_reg_class]))
hard_reg_class = bigger_hard_reg_class;
ira_init_register_move_cost_if_necessary (mode);
ira_init_register_move_cost_if_necessary (hard_reg_mode);
/* Use smaller movement cost for natural hard reg mode or its mode as
operand. */
if (pmode_size.is_constant (&mode_size)
&& phard_reg_mode_size.is_constant (&hard_reg_mode_size))
{
/* Assume we are moving in the natural modes: */
cost_factor = mode_size / hard_reg_mode_size;
if (mode_size % hard_reg_mode_size != 0)
cost_factor++;
if (cost_factor
* (ira_register_move_cost
[hard_reg_mode][hard_reg_class][hard_reg_class])
< (ira_register_move_cost
[mode][hard_reg_class][hard_reg_class]))
cost_mode = hard_reg_mode;
else
cost_factor = 1;
}
move_costs = ira_register_move_cost[cost_mode];
i = regno == (int) REGNO (src) ? 1 : 0;
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
cost = (i == 0
? move_costs[hard_reg_class][rclass]
: move_costs[rclass][hard_reg_class]);
cost *= cost_factor;
op_costs[i]->cost[k] = cost * frequency;
/* If this insn is a single set copying operand 1 to
operand 0 and one operand is an allocno with the
other a hard reg or an allocno that prefers a hard
register that is in its own register class then we
may want to adjust the cost of that register class to
-1.
Avoid the adjustment if the source does not die to
avoid stressing of register allocator by preferencing
two colliding registers into single class. */
if (dead_p
&& TEST_HARD_REG_BIT (reg_class_contents[rclass], other_regno)
&& (reg_class_size[(int) rclass]
== (ira_reg_class_max_nregs
[(int) rclass][(int) GET_MODE(src)])))
{
if (reg_class_size[rclass] == 1)
op_costs[i]->cost[k] = -frequency;
else if (in_hard_reg_set_p (reg_class_contents[rclass],
GET_MODE(src), other_regno))
op_costs[i]->cost[k] = -frequency;
}
}
op_costs[i]->mem_cost
= ira_memory_move_cost[mode][hard_reg_class][i] * frequency;
return;
}
}
for (i = 0; i < recog_data.n_operands; i++)
{
constraints[i] = recog_data.constraints[i];
modes[i] = recog_data.operand_mode[i];
}
/* If we get here, we are set up to record the costs of all the
operands for this insn. Start by initializing the costs. Then
handle any address registers. Finally record the desired classes
for any allocnos, doing it twice if some pair of operands are
commutative. */
for (i = 0; i < recog_data.n_operands; i++)
{
rtx op_mem = extract_mem_from_operand (recog_data.operand[i]);
memcpy (op_costs[i], init_cost, struct_costs_size);
if (GET_CODE (recog_data.operand[i]) == SUBREG)
recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
if (MEM_P (op_mem))
record_address_regs (GET_MODE (op_mem),
MEM_ADDR_SPACE (op_mem),
XEXP (op_mem, 0),
0, MEM, SCRATCH, frequency * 2);
else if (constraints[i][0] == 'p'
|| (insn_extra_address_constraint
(lookup_constraint (constraints[i]))))
record_address_regs (VOIDmode, ADDR_SPACE_GENERIC,
recog_data.operand[i], 0, ADDRESS, SCRATCH,
frequency * 2);
}
/* Check for commutative in a separate loop so everything will have
been initialized. We must do this even if one operand is a
constant--see addsi3 in m68k.md. */
for (i = 0; i < (int) recog_data.n_operands - 1; i++)
if (constraints[i][0] == '%')
{
const char *xconstraints[MAX_RECOG_OPERANDS];
int j;
/* Handle commutative operands by swapping the
constraints. We assume the modes are the same. */
for (j = 0; j < recog_data.n_operands; j++)
xconstraints[j] = constraints[j];
xconstraints[i] = constraints[i+1];
xconstraints[i+1] = constraints[i];
record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
recog_data.operand, modes,
xconstraints, insn, pref);
}
record_reg_classes (recog_data.n_alternatives, recog_data.n_operands,
recog_data.operand, modes,
constraints, insn, pref);
}
/* Process one insn INSN. Scan it and record each time it would save
code to put a certain allocnos in a certain class. Return the last
insn processed, so that the scan can be continued from there. */
static rtx_insn *
scan_one_insn (rtx_insn *insn)
{
enum rtx_code pat_code;
rtx set, note;
int i, k;
bool counted_mem;
if (!NONDEBUG_INSN_P (insn))
return insn;
pat_code = GET_CODE (PATTERN (insn));
if (pat_code == ASM_INPUT)
return insn;
/* If INSN is a USE/CLOBBER of a pseudo in a mode M then go ahead
and initialize the register move costs of mode M.
The pseudo may be related to another pseudo via a copy (implicit or
explicit) and if there are no mode M uses/sets of the original
pseudo, then we may leave the register move costs uninitialized for
mode M. */
if (pat_code == USE || pat_code == CLOBBER)
{
rtx x = XEXP (PATTERN (insn), 0);
if (GET_CODE (x) == REG
&& REGNO (x) >= FIRST_PSEUDO_REGISTER
&& have_regs_of_mode[GET_MODE (x)])
ira_init_register_move_cost_if_necessary (GET_MODE (x));
return insn;
}
counted_mem = false;
set = single_set (insn);
extract_insn (insn);
/* If this insn loads a parameter from its stack slot, then it
represents a savings, rather than a cost, if the parameter is
stored in memory. Record this fact.
Similarly if we're loading other constants from memory (constant
pool, TOC references, small data areas, etc) and this is the only
assignment to the destination pseudo.
Don't do this if SET_SRC (set) isn't a general operand, if it is
a memory requiring special instructions to load it, decreasing
mem_cost might result in it being loaded using the specialized
instruction into a register, then stored into stack and loaded
again from the stack. See PR52208.
Don't do this if SET_SRC (set) has side effect. See PR56124. */
if (set != 0 && REG_P (SET_DEST (set)) && MEM_P (SET_SRC (set))
&& (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL_RTX
&& ((MEM_P (XEXP (note, 0))
&& !side_effects_p (SET_SRC (set)))
|| (CONSTANT_P (XEXP (note, 0))
&& targetm.legitimate_constant_p (GET_MODE (SET_DEST (set)),
XEXP (note, 0))
&& REG_N_SETS (REGNO (SET_DEST (set))) == 1))
&& general_operand (SET_SRC (set), GET_MODE (SET_SRC (set)))
/* LRA does not use equiv with a symbol for PIC code. */
&& (! ira_use_lra_p || ! pic_offset_table_rtx
|| ! contains_symbol_ref_p (XEXP (note, 0))))
{
enum reg_class cl = GENERAL_REGS;
rtx reg = SET_DEST (set);
int num = COST_INDEX (REGNO (reg));
/* Costs for NO_REGS are used in cost calculation on the
1st pass when the preferred register classes are not
known yet. In this case we take the best scenario when
mode can't be put into GENERAL_REGS. */
if (!targetm.hard_regno_mode_ok (ira_class_hard_regs[cl][0],
GET_MODE (reg)))
cl = NO_REGS;
COSTS (costs, num)->mem_cost
-= ira_memory_move_cost[GET_MODE (reg)][cl][1] * frequency;
record_address_regs (GET_MODE (SET_SRC (set)),
MEM_ADDR_SPACE (SET_SRC (set)),
XEXP (SET_SRC (set), 0), 0, MEM, SCRATCH,
frequency * 2);
counted_mem = true;
}
record_operand_costs (insn, pref);
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
{
const char *p;
fprintf (ira_dump_file, " Final costs after insn %u", INSN_UID (insn));
if (INSN_CODE (insn) >= 0
&& (p = get_insn_name (INSN_CODE (insn))) != NULL)
fprintf (ira_dump_file, " {%s}", p);
fprintf (ira_dump_file, " (freq=%d)\n",
REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)));
dump_insn_slim (ira_dump_file, insn);
}
/* Now add the cost for each operand to the total costs for its
allocno. */
for (i = 0; i < recog_data.n_operands; i++)
{
rtx op = recog_data.operand[i];
if (GET_CODE (op) == SUBREG)
op = SUBREG_REG (op);
if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
{
int regno = REGNO (op);
struct costs *p = COSTS (costs, COST_INDEX (regno));
struct costs *q = op_costs[i];
int *p_costs = p->cost, *q_costs = q->cost;
cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
int add_cost = 0;
/* If the already accounted for the memory "cost" above, don't
do so again. */
if (!counted_mem)
{
add_cost = q->mem_cost;
if (add_cost > 0 && INT_MAX - add_cost < p->mem_cost)
p->mem_cost = INT_MAX;
else
p->mem_cost += add_cost;
}
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
{
fprintf (ira_dump_file, " op %d(r=%u) MEM:%d(+%d)",
i, REGNO(op), p->mem_cost, add_cost);
}
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
add_cost = q_costs[k];
if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
p_costs[k] = INT_MAX;
else
p_costs[k] += add_cost;
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
{
fprintf (ira_dump_file, " %s:%d(+%d)",
reg_class_names[cost_classes_ptr->classes[k]],
p_costs[k], add_cost);
}
}
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
fprintf (ira_dump_file, "\n");
}
}
return insn;
}
/* Print allocnos costs to the dump file. */
static void
print_allocno_costs (void)
{
int k;
ira_allocno_t a;
ira_allocno_iterator ai;
ira_assert (allocno_p);
fprintf (ira_dump_file, "\n");
FOR_EACH_ALLOCNO (a, ai)
{
int i, rclass;
basic_block bb;
int regno = ALLOCNO_REGNO (a);
cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
enum reg_class *cost_classes = cost_classes_ptr->classes;
i = ALLOCNO_NUM (a);
fprintf (ira_dump_file, " a%d(r%d,", i, regno);
if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
fprintf (ira_dump_file, "b%d", bb->index);
else
fprintf (ira_dump_file, "l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
fprintf (ira_dump_file, ") costs:");
for (k = 0; k < cost_classes_ptr->num; k++)
{
rclass = cost_classes[k];
fprintf (ira_dump_file, " %s:%d", reg_class_names[rclass],
COSTS (costs, i)->cost[k]);
if (flag_ira_region == IRA_REGION_ALL
|| flag_ira_region == IRA_REGION_MIXED)
fprintf (ira_dump_file, ",%d",
COSTS (total_allocno_costs, i)->cost[k]);
}
fprintf (ira_dump_file, " MEM:%i", COSTS (costs, i)->mem_cost);
if (flag_ira_region == IRA_REGION_ALL
|| flag_ira_region == IRA_REGION_MIXED)
fprintf (ira_dump_file, ",%d",
COSTS (total_allocno_costs, i)->mem_cost);
fprintf (ira_dump_file, "\n");
}
}
/* Print pseudo costs to the dump file. */
static void
print_pseudo_costs (void)
{
int regno, k;
int rclass;
cost_classes_t cost_classes_ptr;
enum reg_class *cost_classes;
ira_assert (! allocno_p);
fprintf (ira_dump_file, "\n");
for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
{
if (REG_N_REFS (regno) <= 0)
continue;
cost_classes_ptr = regno_cost_classes[regno];
cost_classes = cost_classes_ptr->classes;
fprintf (ira_dump_file, " r%d costs:", regno);
for (k = 0; k < cost_classes_ptr->num; k++)
{
rclass = cost_classes[k];
fprintf (ira_dump_file, " %s:%d", reg_class_names[rclass],
COSTS (costs, regno)->cost[k]);
}
fprintf (ira_dump_file, " MEM:%i\n", COSTS (costs, regno)->mem_cost);
}
}
/* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
costs. */
static void
process_bb_for_costs (basic_block bb)
{
rtx_insn *insn;
frequency = REG_FREQ_FROM_BB (bb);
if (frequency == 0)
frequency = 1;
FOR_BB_INSNS (bb, insn)
insn = scan_one_insn (insn);
}
/* Traverse the BB represented by LOOP_TREE_NODE to update the allocno
costs. */
static void
process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node)
{
basic_block bb;
bb = loop_tree_node->bb;
if (bb != NULL)
process_bb_for_costs (bb);
}
/* Return true if all autoinc rtx in X change only a register and memory is
valid. */
static bool
validate_autoinc_and_mem_addr_p (rtx x)
{
enum rtx_code code = GET_CODE (x);
if (GET_RTX_CLASS (code) == RTX_AUTOINC)
return REG_P (XEXP (x, 0));
const char *fmt = GET_RTX_FORMAT (code);
for (int i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
if (fmt[i] == 'e')
{
if (!validate_autoinc_and_mem_addr_p (XEXP (x, i)))
return false;
}
else if (fmt[i] == 'E')
{
for (int j = 0; j < XVECLEN (x, i); j++)
if (!validate_autoinc_and_mem_addr_p (XVECEXP (x, i, j)))
return false;
}
/* Check memory after checking autoinc to guarantee that autoinc is already
valid for machine-dependent code checking memory address. */
return (!MEM_P (x)
|| memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
MEM_ADDR_SPACE (x)));
}
/* Check that reg REGNO can be changed by TO in INSN. Return true in case the
result insn would be valid one. */
static bool
equiv_can_be_consumed_p (int regno, rtx to, rtx_insn *insn)
{
validate_replace_src_group (regno_reg_rtx[regno], to, insn);
/* We can change register to equivalent memory in autoinc rtl. Some code
including verify_changes assumes that autoinc contains only a register.
So check this first. */
bool res = validate_autoinc_and_mem_addr_p (PATTERN (insn));
if (res)
res = verify_changes (0);
cancel_changes (0);
return res;
}
/* Return true if X contains a pseudo with equivalence. In this case also
return the pseudo through parameter REG. If the pseudo is a part of subreg,
return the subreg through parameter SUBREG. */
static bool
get_equiv_regno (rtx x, int ®no, rtx &subreg)
{
subreg = NULL_RTX;
if (GET_CODE (x) == SUBREG)
{
subreg = x;
x = SUBREG_REG (x);
}
if (REG_P (x)
&& (ira_reg_equiv[REGNO (x)].memory != NULL
|| ira_reg_equiv[REGNO (x)].invariant != NULL
|| ira_reg_equiv[REGNO (x)].constant != NULL))
{
regno = REGNO (x);
return true;
}
RTX_CODE code = GET_CODE (x);
const char *fmt = GET_RTX_FORMAT (code);
for (int i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
if (fmt[i] == 'e')
{
if (get_equiv_regno (XEXP (x, i), regno, subreg))
return true;
}
else if (fmt[i] == 'E')
{
for (int j = 0; j < XVECLEN (x, i); j++)
if (get_equiv_regno (XVECEXP (x, i, j), regno, subreg))
return true;
}
return false;
}
/* A pass through the current function insns. Calculate costs of using
equivalences for pseudos and store them in regno_equiv_gains. */
static void
calculate_equiv_gains (void)
{
basic_block bb;
int regno, freq, cost;
rtx subreg;
rtx_insn *insn;
machine_mode mode;
enum reg_class rclass;
bitmap_head equiv_pseudos;
ira_assert (allocno_p);
bitmap_initialize (&equiv_pseudos, ®_obstack);
for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
if (ira_reg_equiv[regno].init_insns != NULL
&& (ira_reg_equiv[regno].memory != NULL
|| ira_reg_equiv[regno].invariant != NULL
|| (ira_reg_equiv[regno].constant != NULL
/* Ignore complicated constants which probably will be placed
in memory: */
&& GET_CODE (ira_reg_equiv[regno].constant) != CONST_DOUBLE
&& GET_CODE (ira_reg_equiv[regno].constant) != CONST_VECTOR
&& GET_CODE (ira_reg_equiv[regno].constant) != LABEL_REF)))
{
rtx_insn_list *x;
for (x = ira_reg_equiv[regno].init_insns; x != NULL; x = x->next ())
{
insn = x->insn ();
rtx set = single_set (insn);
if (set == NULL_RTX || SET_DEST (set) != regno_reg_rtx[regno])
break;
bb = BLOCK_FOR_INSN (insn);
ira_curr_regno_allocno_map
= ira_bb_nodes[bb->index].parent->regno_allocno_map;
mode = PSEUDO_REGNO_MODE (regno);
rclass = pref[COST_INDEX (regno)];
ira_init_register_move_cost_if_necessary (mode);
if (ira_reg_equiv[regno].memory != NULL)
cost = ira_memory_move_cost[mode][rclass][1];
else
cost = ira_register_move_cost[mode][rclass][rclass];
freq = REG_FREQ_FROM_BB (bb);
regno_equiv_gains[regno] += cost * freq;
}
if (x != NULL)
/* We found complicated equiv or reverse equiv mem=reg. Ignore
them. */
regno_equiv_gains[regno] = 0;
else
bitmap_set_bit (&equiv_pseudos, regno);
}
FOR_EACH_BB_FN (bb, cfun)
{
freq = REG_FREQ_FROM_BB (bb);
ira_curr_regno_allocno_map
= ira_bb_nodes[bb->index].parent->regno_allocno_map;
FOR_BB_INSNS (bb, insn)
{
if (!NONDEBUG_INSN_P (insn)
|| !get_equiv_regno (PATTERN (insn), regno, subreg)
|| !bitmap_bit_p (&equiv_pseudos, regno))
continue;
rtx subst = ira_reg_equiv[regno].memory;
if (subst == NULL)
subst = ira_reg_equiv[regno].constant;
if (subst == NULL)
subst = ira_reg_equiv[regno].invariant;
ira_assert (subst != NULL);
mode = PSEUDO_REGNO_MODE (regno);
ira_init_register_move_cost_if_necessary (mode);
bool consumed_p = equiv_can_be_consumed_p (regno, subst, insn);
rclass = pref[COST_INDEX (regno)];
if (MEM_P (subst)
/* If it is a change of constant into double for example, the
result constant probably will be placed in memory. */
|| (subreg != NULL_RTX && !INTEGRAL_MODE_P (GET_MODE (subreg))))
cost = ira_memory_move_cost[mode][rclass][1] + (consumed_p ? 0 : 1);
else if (consumed_p)
continue;
else
cost = ira_register_move_cost[mode][rclass][rclass];
regno_equiv_gains[regno] -= cost * freq;
}
}
bitmap_clear (&equiv_pseudos);
}
/* Find costs of register classes and memory for allocnos or pseudos
and their best costs. Set up preferred, alternative and allocno
classes for pseudos. */
static void
find_costs_and_classes (void)
{
int i, k, start, max_cost_classes_num;
int pass;
basic_block bb;
enum reg_class *regno_best_class, new_class;
init_recog ();
regno_best_class
= (enum reg_class *) ira_allocate (max_reg_num ()
* sizeof (enum reg_class));
for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
regno_best_class[i] = NO_REGS;
if (!resize_reg_info () && allocno_p
&& pseudo_classes_defined_p && flag_expensive_optimizations)
{
ira_allocno_t a;
ira_allocno_iterator ai;
pref = pref_buffer;
max_cost_classes_num = 1;
FOR_EACH_ALLOCNO (a, ai)
{
pref[ALLOCNO_NUM (a)] = reg_preferred_class (ALLOCNO_REGNO (a));
setup_regno_cost_classes_by_aclass
(ALLOCNO_REGNO (a), pref[ALLOCNO_NUM (a)]);
max_cost_classes_num
= MAX (max_cost_classes_num,
regno_cost_classes[ALLOCNO_REGNO (a)]->num);
}
start = 1;
}
else
{
pref = NULL;
max_cost_classes_num = ira_important_classes_num;
for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
if (regno_reg_rtx[i] != NULL_RTX)
setup_regno_cost_classes_by_mode (i, PSEUDO_REGNO_MODE (i));
else
setup_regno_cost_classes_by_aclass (i, ALL_REGS);
start = 0;
}
if (allocno_p)
/* Clear the flag for the next compiled function. */
pseudo_classes_defined_p = false;
/* Normally we scan the insns once and determine the best class to
use for each allocno. However, if -fexpensive-optimizations are
on, we do so twice, the second time using the tentative best
classes to guide the selection. */
for (pass = start; pass <= flag_expensive_optimizations; pass++)
{
if ((!allocno_p || internal_flag_ira_verbose > 0) && ira_dump_file)
fprintf (ira_dump_file,
"\nPass %i for finding pseudo/allocno costs\n\n", pass);
if (pass != start)
{
max_cost_classes_num = 1;
for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
{
setup_regno_cost_classes_by_aclass (i, regno_best_class[i]);
max_cost_classes_num
= MAX (max_cost_classes_num, regno_cost_classes[i]->num);
}
}
struct_costs_size
= sizeof (struct costs) + sizeof (int) * (max_cost_classes_num - 1);
/* Zero out our accumulation of the cost of each class for each
allocno. */
memset (costs, 0, cost_elements_num * struct_costs_size);
if (allocno_p)
{
/* Scan the instructions and record each time it would save code
to put a certain allocno in a certain class. */
ira_traverse_loop_tree (true, ira_loop_tree_root,
process_bb_node_for_costs, NULL);
memcpy (total_allocno_costs, costs,
max_struct_costs_size * ira_allocnos_num);
}
else
{
basic_block bb;
FOR_EACH_BB_FN (bb, cfun)
process_bb_for_costs (bb);
}
if (pass == 0)
pref = pref_buffer;
if (ira_use_lra_p && allocno_p && pass == 1)
/* It is a pass through all insns. So do it once and only for RA (not
for insn scheduler) when we already found preferable pseudo register
classes on the previous pass. */
calculate_equiv_gains ();
/* Now for each allocno look at how desirable each class is and
find which class is preferred. */
for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
{
ira_allocno_t a, parent_a;
int rclass, a_num, parent_a_num, add_cost;
ira_loop_tree_node_t parent;
int best_cost, allocno_cost;
enum reg_class best, alt_class;
cost_classes_t cost_classes_ptr = regno_cost_classes[i];
enum reg_class *cost_classes;
int *i_costs = temp_costs->cost;
int i_mem_cost;
int equiv_savings = regno_equiv_gains[i];
if (! allocno_p)
{
if (regno_reg_rtx[i] == NULL_RTX)
continue;
memcpy (temp_costs, COSTS (costs, i), struct_costs_size);
i_mem_cost = temp_costs->mem_cost;
cost_classes = cost_classes_ptr->classes;
}
else
{
if (ira_regno_allocno_map[i] == NULL)
continue;
memset (temp_costs, 0, struct_costs_size);
i_mem_cost = 0;
cost_classes = cost_classes_ptr->classes;
/* Find cost of all allocnos with the same regno. */
for (a = ira_regno_allocno_map[i];
a != NULL;
a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
{
int *a_costs, *p_costs;
a_num = ALLOCNO_NUM (a);
if ((flag_ira_region == IRA_REGION_ALL
|| flag_ira_region == IRA_REGION_MIXED)
&& (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
&& (parent_a = parent->regno_allocno_map[i]) != NULL
/* There are no caps yet. */
&& bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE
(a)->border_allocnos,
ALLOCNO_NUM (a)))
{
/* Propagate costs to upper levels in the region
tree. */
parent_a_num = ALLOCNO_NUM (parent_a);
a_costs = COSTS (total_allocno_costs, a_num)->cost;
p_costs = COSTS (total_allocno_costs, parent_a_num)->cost;
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
add_cost = a_costs[k];
if (add_cost > 0 && INT_MAX - add_cost < p_costs[k])
p_costs[k] = INT_MAX;
else
p_costs[k] += add_cost;
}
add_cost = COSTS (total_allocno_costs, a_num)->mem_cost;
if (add_cost > 0
&& (INT_MAX - add_cost
< COSTS (total_allocno_costs,
parent_a_num)->mem_cost))
COSTS (total_allocno_costs, parent_a_num)->mem_cost
= INT_MAX;
else
COSTS (total_allocno_costs, parent_a_num)->mem_cost
+= add_cost;
if (i >= first_moveable_pseudo && i < last_moveable_pseudo)
COSTS (total_allocno_costs, parent_a_num)->mem_cost = 0;
}
a_costs = COSTS (costs, a_num)->cost;
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
add_cost = a_costs[k];
if (add_cost > 0 && INT_MAX - add_cost < i_costs[k])
i_costs[k] = INT_MAX;
else
i_costs[k] += add_cost;
}
add_cost = COSTS (costs, a_num)->mem_cost;
if (add_cost > 0 && INT_MAX - add_cost < i_mem_cost)
i_mem_cost = INT_MAX;
else
i_mem_cost += add_cost;
}
}
if (i >= first_moveable_pseudo && i < last_moveable_pseudo)
i_mem_cost = 0;
else if (ira_use_lra_p)
{
if (equiv_savings > 0)
{
i_mem_cost = 0;
if (ira_dump_file != NULL && internal_flag_ira_verbose > 5)
fprintf (ira_dump_file,
" Use MEM for r%d as the equiv savings is %d\n",
i, equiv_savings);
}
}
else if (equiv_savings < 0)
i_mem_cost = -equiv_savings;
else if (equiv_savings > 0)
{
i_mem_cost = 0;
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
i_costs[k] += equiv_savings;
}
best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
best = ALL_REGS;
alt_class = NO_REGS;
/* Find best common class for all allocnos with the same
regno. */
for (k = 0; k < cost_classes_ptr->num; k++)
{
rclass = cost_classes[k];
if (i_costs[k] < best_cost)
{
best_cost = i_costs[k];
best = (enum reg_class) rclass;
}
else if (i_costs[k] == best_cost)
best = ira_reg_class_subunion[best][rclass];
if (pass == flag_expensive_optimizations
/* We still prefer registers to memory even at this
stage if their costs are the same. We will make
a final decision during assigning hard registers
when we have all info including more accurate
costs which might be affected by assigning hard
registers to other pseudos because the pseudos
involved in moves can be coalesced. */
&& i_costs[k] <= i_mem_cost
&& (reg_class_size[reg_class_subunion[alt_class][rclass]]
> reg_class_size[alt_class]))
alt_class = reg_class_subunion[alt_class][rclass];
}
alt_class = ira_allocno_class_translate[alt_class];
if (best_cost > i_mem_cost
&& ! non_spilled_static_chain_regno_p (i))
regno_aclass[i] = NO_REGS;
else if (!optimize && !targetm.class_likely_spilled_p (best))
/* Registers in the alternative class are likely to need
longer or slower sequences than registers in the best class.
When optimizing we make some effort to use the best class
over the alternative class where possible, but at -O0 we
effectively give the alternative class equal weight.
We then run the risk of using slower alternative registers
when plenty of registers from the best class are still free.
This is especially true because live ranges tend to be very
short in -O0 code and so register pressure tends to be low.
Avoid that by ignoring the alternative class if the best
class has plenty of registers.
The union class arrays give important classes and only
part of it are allocno classes. So translate them into
allocno classes. */
regno_aclass[i] = ira_allocno_class_translate[best];
else
{
/* Make the common class the biggest class of best and
alt_class. Translate the common class into an
allocno class too. */
regno_aclass[i] = (ira_allocno_class_translate
[ira_reg_class_superunion[best][alt_class]]);
ira_assert (regno_aclass[i] != NO_REGS
&& ira_reg_allocno_class_p[regno_aclass[i]]);
}
if (pic_offset_table_rtx != NULL
&& i == (int) REGNO (pic_offset_table_rtx))
{
/* For some targets, integer pseudos can be assigned to fp
regs. As we don't want reload pic offset table pseudo, we
should avoid using non-integer regs. */
regno_aclass[i]
= ira_reg_class_intersect[regno_aclass[i]][GENERAL_REGS];
alt_class = ira_reg_class_intersect[alt_class][GENERAL_REGS];
}
if ((new_class
= (reg_class) (targetm.ira_change_pseudo_allocno_class
(i, regno_aclass[i], best))) != regno_aclass[i])
{
regno_aclass[i] = new_class;
if (hard_reg_set_subset_p (reg_class_contents[new_class],
reg_class_contents[best]))
best = new_class;
if (hard_reg_set_subset_p (reg_class_contents[new_class],
reg_class_contents[alt_class]))
alt_class = new_class;
}
if (pass == flag_expensive_optimizations)
{
if (best_cost > i_mem_cost
/* Do not assign NO_REGS to static chain pointer
pseudo when non-local goto is used. */
&& ! non_spilled_static_chain_regno_p (i))
best = alt_class = NO_REGS;
else if (best == alt_class)
alt_class = NO_REGS;
setup_reg_classes (i, best, alt_class, regno_aclass[i]);
if ((!allocno_p || internal_flag_ira_verbose > 2)
&& ira_dump_file != NULL)
fprintf (ira_dump_file,
" r%d: preferred %s, alternative %s, allocno %s\n",
i, reg_class_names[best], reg_class_names[alt_class],
reg_class_names[regno_aclass[i]]);
}
regno_best_class[i] = best;
if (! allocno_p)
{
pref[i] = (best_cost > i_mem_cost
&& ! non_spilled_static_chain_regno_p (i)
? NO_REGS : best);
continue;
}
for (a = ira_regno_allocno_map[i];
a != NULL;
a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
{
enum reg_class aclass = regno_aclass[i];
int a_num = ALLOCNO_NUM (a);
int *total_a_costs = COSTS (total_allocno_costs, a_num)->cost;
int *a_costs = COSTS (costs, a_num)->cost;
if (aclass == NO_REGS)
best = NO_REGS;
else
{
/* Finding best class which is subset of the common
class. */
best_cost = (1 << (HOST_BITS_PER_INT - 2)) - 1;
allocno_cost = best_cost;
best = ALL_REGS;
for (k = 0; k < cost_classes_ptr->num; k++)
{
rclass = cost_classes[k];
if (! ira_class_subset_p[rclass][aclass])
continue;
if (total_a_costs[k] < best_cost)
{
best_cost = total_a_costs[k];
allocno_cost = a_costs[k];
best = (enum reg_class) rclass;
}
else if (total_a_costs[k] == best_cost)
{
best = ira_reg_class_subunion[best][rclass];
allocno_cost = MAX (allocno_cost, a_costs[k]);
}
}
ALLOCNO_CLASS_COST (a) = allocno_cost;
}
if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL
&& (pass == 0 || pref[a_num] != best))
{
fprintf (ira_dump_file, " a%d (r%d,", a_num, i);
if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
fprintf (ira_dump_file, "b%d", bb->index);
else
fprintf (ira_dump_file, "l%d",
ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
fprintf (ira_dump_file, ") best %s, allocno %s\n",
reg_class_names[best],
reg_class_names[aclass]);
}
pref[a_num] = best;
if (pass == flag_expensive_optimizations && best != aclass
&& ira_class_hard_regs_num[best] > 0
&& (ira_reg_class_max_nregs[best][ALLOCNO_MODE (a)]
>= ira_class_hard_regs_num[best]))
{
int ind = cost_classes_ptr->index[aclass];
ira_assert (ind >= 0);
ira_init_register_move_cost_if_necessary (ALLOCNO_MODE (a));
ira_add_allocno_pref (a, ira_class_hard_regs[best][0],
(a_costs[ind] - ALLOCNO_CLASS_COST (a))
/ (ira_register_move_cost
[ALLOCNO_MODE (a)][best][aclass]));
for (k = 0; k < cost_classes_ptr->num; k++)
if (ira_class_subset_p[cost_classes[k]][best])
a_costs[k] = a_costs[ind];
}
}
}
if (internal_flag_ira_verbose > 4 && ira_dump_file)
{
if (allocno_p)
print_allocno_costs ();
else
print_pseudo_costs ();
fprintf (ira_dump_file,"\n");
}
}
ira_free (regno_best_class);
}
/* Process moves involving hard regs to modify allocno hard register
costs. We can do this only after determining allocno class. If a
hard register forms a register class, then moves with the hard
register are already taken into account in class costs for the
allocno. */
static void
process_bb_node_for_hard_reg_moves (ira_loop_tree_node_t loop_tree_node)
{
int i, freq, src_regno, dst_regno, hard_regno, a_regno;
bool to_p;
ira_allocno_t a, curr_a;
ira_loop_tree_node_t curr_loop_tree_node;
enum reg_class rclass;
basic_block bb;
rtx_insn *insn;
rtx set, src, dst;
bb = loop_tree_node->bb;
if (bb == NULL)
return;
freq = REG_FREQ_FROM_BB (bb);
if (freq == 0)
freq = 1;
FOR_BB_INSNS (bb, insn)
{
if (!NONDEBUG_INSN_P (insn))
continue;
set = single_set (insn);
if (set == NULL_RTX)
continue;
dst = SET_DEST (set);
src = SET_SRC (set);
if (! REG_P (dst) || ! REG_P (src))
continue;
dst_regno = REGNO (dst);
src_regno = REGNO (src);
if (dst_regno >= FIRST_PSEUDO_REGISTER
&& src_regno < FIRST_PSEUDO_REGISTER)
{
hard_regno = src_regno;
a = ira_curr_regno_allocno_map[dst_regno];
to_p = true;
}
else if (src_regno >= FIRST_PSEUDO_REGISTER
&& dst_regno < FIRST_PSEUDO_REGISTER)
{
hard_regno = dst_regno;
a = ira_curr_regno_allocno_map[src_regno];
to_p = false;
}
else
continue;
if (reg_class_size[(int) REGNO_REG_CLASS (hard_regno)]
== (ira_reg_class_max_nregs
[REGNO_REG_CLASS (hard_regno)][(int) ALLOCNO_MODE(a)]))
/* If the class can provide only one hard reg to the allocno,
we processed the insn record_operand_costs already and we
actually updated the hard reg cost there. */
continue;
rclass = ALLOCNO_CLASS (a);
if (! TEST_HARD_REG_BIT (reg_class_contents[rclass], hard_regno))
continue;
i = ira_class_hard_reg_index[rclass][hard_regno];
if (i < 0)
continue;
a_regno = ALLOCNO_REGNO (a);
for (curr_loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
curr_loop_tree_node != NULL;
curr_loop_tree_node = curr_loop_tree_node->parent)
if ((curr_a = curr_loop_tree_node->regno_allocno_map[a_regno]) != NULL)
ira_add_allocno_pref (curr_a, hard_regno, freq);
{
int cost;
enum reg_class hard_reg_class;
machine_mode mode;
mode = ALLOCNO_MODE (a);
hard_reg_class = REGNO_REG_CLASS (hard_regno);
ira_init_register_move_cost_if_necessary (mode);
cost = (to_p ? ira_register_move_cost[mode][hard_reg_class][rclass]
: ira_register_move_cost[mode][rclass][hard_reg_class]) * freq;
ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (a), rclass,
ALLOCNO_CLASS_COST (a));
ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
rclass, 0);
ALLOCNO_HARD_REG_COSTS (a)[i] -= cost;
ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[i] -= cost;
ALLOCNO_CLASS_COST (a) = MIN (ALLOCNO_CLASS_COST (a),
ALLOCNO_HARD_REG_COSTS (a)[i]);
}
}
}
/* After we find hard register and memory costs for allocnos, define
its class and modify hard register cost because insns moving
allocno to/from hard registers. */
static void
setup_allocno_class_and_costs (void)
{
int i, j, n, regno, hard_regno, num;
int *reg_costs;
enum reg_class aclass, rclass;
ira_allocno_t a;
ira_allocno_iterator ai;
cost_classes_t cost_classes_ptr;
ira_assert (allocno_p);
FOR_EACH_ALLOCNO (a, ai)
{
i = ALLOCNO_NUM (a);
regno = ALLOCNO_REGNO (a);
aclass = regno_aclass[regno];
cost_classes_ptr = regno_cost_classes[regno];
ira_assert (pref[i] == NO_REGS || aclass != NO_REGS);
ALLOCNO_MEMORY_COST (a) = COSTS (costs, i)->mem_cost;
ira_set_allocno_class (a, aclass);
if (aclass == NO_REGS)
continue;
if (optimize && ALLOCNO_CLASS (a) != pref[i])
{
n = ira_class_hard_regs_num[aclass];
ALLOCNO_HARD_REG_COSTS (a)
= reg_costs = ira_allocate_cost_vector (aclass);
for (j = n - 1; j >= 0; j--)
{
hard_regno = ira_class_hard_regs[aclass][j];
if (TEST_HARD_REG_BIT (reg_class_contents[pref[i]], hard_regno))
reg_costs[j] = ALLOCNO_CLASS_COST (a);
else
{
rclass = REGNO_REG_CLASS (hard_regno);
num = cost_classes_ptr->index[rclass];
if (num < 0)
{
num = cost_classes_ptr->hard_regno_index[hard_regno];
ira_assert (num >= 0);
}
reg_costs[j] = COSTS (costs, i)->cost[num];
}
}
}
}
if (optimize)
ira_traverse_loop_tree (true, ira_loop_tree_root,
process_bb_node_for_hard_reg_moves, NULL);
}
/* Function called once during compiler work. */
void
ira_init_costs_once (void)
{
int i;
init_cost = NULL;
for (i = 0; i < MAX_RECOG_OPERANDS; i++)
{
op_costs[i] = NULL;
this_op_costs[i] = NULL;
}
temp_costs = NULL;
}
/* Free allocated temporary cost vectors. */
void
target_ira_int::free_ira_costs ()
{
int i;
free (x_init_cost);
x_init_cost = NULL;
for (i = 0; i < MAX_RECOG_OPERANDS; i++)
{
free (x_op_costs[i]);
free (x_this_op_costs[i]);
x_op_costs[i] = x_this_op_costs[i] = NULL;
}
free (x_temp_costs);
x_temp_costs = NULL;
}
/* This is called each time register related information is
changed. */
void
ira_init_costs (void)
{
int i;
this_target_ira_int->free_ira_costs ();
max_struct_costs_size
= sizeof (struct costs) + sizeof (int) * (ira_important_classes_num - 1);
/* Don't use ira_allocate because vectors live through several IRA
calls. */
init_cost = (struct costs *) xmalloc (max_struct_costs_size);
init_cost->mem_cost = 1000000;
for (i = 0; i < ira_important_classes_num; i++)
init_cost->cost[i] = 1000000;
for (i = 0; i < MAX_RECOG_OPERANDS; i++)
{
op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
this_op_costs[i] = (struct costs *) xmalloc (max_struct_costs_size);
}
temp_costs = (struct costs *) xmalloc (max_struct_costs_size);
}
/* Common initialization function for ira_costs and
ira_set_pseudo_classes. */
static void
init_costs (void)
{
init_subregs_of_mode ();
costs = (struct costs *) ira_allocate (max_struct_costs_size
* cost_elements_num);
pref_buffer = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
* cost_elements_num);
regno_aclass = (enum reg_class *) ira_allocate (sizeof (enum reg_class)
* max_reg_num ());
regno_equiv_gains = (int *) ira_allocate (sizeof (int) * max_reg_num ());
memset (regno_equiv_gains, 0, sizeof (int) * max_reg_num ());
}
/* Common finalization function for ira_costs and
ira_set_pseudo_classes. */
static void
finish_costs (void)
{
finish_subregs_of_mode ();
ira_free (regno_equiv_gains);
ira_free (regno_aclass);
ira_free (pref_buffer);
ira_free (costs);
}
/* Entry function which defines register class, memory and hard
register costs for each allocno. */
void
ira_costs (void)
{
allocno_p = true;
cost_elements_num = ira_allocnos_num;
init_costs ();
total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size
* ira_allocnos_num);
initiate_regno_cost_classes ();
if (!ira_use_lra_p)
/* Process equivs in reload to update costs through hook
ira_adjust_equiv_reg_cost. */
calculate_elim_costs_all_insns ();
find_costs_and_classes ();
setup_allocno_class_and_costs ();
finish_regno_cost_classes ();
finish_costs ();
ira_free (total_allocno_costs);
}
/* Entry function which defines classes for pseudos.
Set pseudo_classes_defined_p only if DEFINE_PSEUDO_CLASSES is true. */
void
ira_set_pseudo_classes (bool define_pseudo_classes, FILE *dump_file)
{
FILE *saved_file = ira_dump_file;
allocno_p = false;
internal_flag_ira_verbose = flag_ira_verbose;
ira_dump_file = dump_file;
cost_elements_num = max_reg_num ();
init_costs ();
initiate_regno_cost_classes ();
find_costs_and_classes ();
finish_regno_cost_classes ();
if (define_pseudo_classes)
pseudo_classes_defined_p = true;
finish_costs ();
ira_dump_file = saved_file;
}
/* Change hard register costs for allocnos which lives through
function calls. This is called only when we found all intersected
calls during building allocno live ranges. */
void
ira_tune_allocno_costs (void)
{
int j, n, regno;
int cost, min_cost, *reg_costs;
enum reg_class aclass;
machine_mode mode;
ira_allocno_t a;
ira_allocno_iterator ai;
ira_allocno_object_iterator oi;
ira_object_t obj;
bool skip_p;
FOR_EACH_ALLOCNO (a, ai)
{
aclass = ALLOCNO_CLASS (a);
if (aclass == NO_REGS)
continue;
mode = ALLOCNO_MODE (a);
n = ira_class_hard_regs_num[aclass];
min_cost = INT_MAX;
if (ALLOCNO_CALLS_CROSSED_NUM (a)
!= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
{
ira_allocate_and_set_costs
(&ALLOCNO_HARD_REG_COSTS (a), aclass,
ALLOCNO_CLASS_COST (a));
reg_costs = ALLOCNO_HARD_REG_COSTS (a);
for (j = n - 1; j >= 0; j--)
{
regno = ira_class_hard_regs[aclass][j];
skip_p = false;
FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
{
if (ira_hard_reg_set_intersection_p (regno, mode,
OBJECT_CONFLICT_HARD_REGS
(obj)))
{
skip_p = true;
break;
}
}
if (skip_p)
continue;
cost = 0;
if (ira_need_caller_save_p (a, regno))
cost += ira_caller_save_cost (a);
#ifdef IRA_HARD_REGNO_ADD_COST_MULTIPLIER
{
auto rclass = REGNO_REG_CLASS (regno);
cost += ((ira_memory_move_cost[mode][rclass][0]
+ ira_memory_move_cost[mode][rclass][1])
* ALLOCNO_FREQ (a)
* IRA_HARD_REGNO_ADD_COST_MULTIPLIER (regno) / 2);
}
#endif
if (INT_MAX - cost < reg_costs[j])
reg_costs[j] = INT_MAX;
else
reg_costs[j] += cost;
if (min_cost > reg_costs[j])
min_cost = reg_costs[j];
}
}
if (min_cost != INT_MAX)
ALLOCNO_CLASS_COST (a) = min_cost;
/* Some targets allow pseudos to be allocated to unaligned sequences
of hard registers. However, selecting an unaligned sequence can
unnecessarily restrict later allocations. So increase the cost of
unaligned hard regs to encourage the use of aligned hard regs. */
{
const int nregs = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
if (nregs > 1)
{
ira_allocate_and_set_costs
(&ALLOCNO_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a));
reg_costs = ALLOCNO_HARD_REG_COSTS (a);
for (j = n - 1; j >= 0; j--)
{
regno = ira_non_ordered_class_hard_regs[aclass][j];
if ((regno % nregs) != 0)
{
int index = ira_class_hard_reg_index[aclass][regno];
ira_assert (index != -1);
reg_costs[index] += ALLOCNO_FREQ (a);
}
}
}
}
}
}
/* A hook from the reload pass. Add COST to the estimated gain for eliminating
REGNO with its equivalence. If COST is zero, record that no such
elimination is possible. */
void
ira_adjust_equiv_reg_cost (unsigned regno, int cost)
{
ira_assert (!ira_use_lra_p);
if (cost == 0)
regno_equiv_gains[regno] = 0;
else
regno_equiv_gains[regno] += cost;
}
void
ira_costs_cc_finalize (void)
{
this_target_ira_int->free_ira_costs ();
}
|